]> code.delx.au - gnu-emacs/blob - src/xterm.c
(x_encode_char): Call ccl_driver with the last arg Qnil.
[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 (! EQ ((c = Fgethash (make_number (keysym),
10575 Vx_keysym_table, Qnil)),
10576 Qnil))
10577 {
10578 bufp->kind = (ASCII_CHAR_P (c)
10579 ? ascii_keystroke
10580 : multibyte_char_keystroke);
10581 bufp->code = c;
10582 XSETFRAME (bufp->frame_or_window, f);
10583 bufp->arg = Qnil;
10584 bufp->modifiers
10585 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10586 modifiers);
10587 bufp->timestamp = event.xkey.time;
10588 bufp++;
10589 count++;
10590 numchars--;
10591 }
10592 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10593 || keysym == XK_Delete
10594 #ifdef XK_ISO_Left_Tab
10595 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10596 #endif
10597 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10598 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10599 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10600 #ifdef HPUX
10601 /* This recognizes the "extended function
10602 keys". It seems there's no cleaner way.
10603 Test IsModifierKey to avoid handling
10604 mode_switch incorrectly. */
10605 || ((unsigned) (keysym) >= XK_Select
10606 && (unsigned)(keysym) < XK_KP_Space)
10607 #endif
10608 #ifdef XK_dead_circumflex
10609 || orig_keysym == XK_dead_circumflex
10610 #endif
10611 #ifdef XK_dead_grave
10612 || orig_keysym == XK_dead_grave
10613 #endif
10614 #ifdef XK_dead_tilde
10615 || orig_keysym == XK_dead_tilde
10616 #endif
10617 #ifdef XK_dead_diaeresis
10618 || orig_keysym == XK_dead_diaeresis
10619 #endif
10620 #ifdef XK_dead_macron
10621 || orig_keysym == XK_dead_macron
10622 #endif
10623 #ifdef XK_dead_degree
10624 || orig_keysym == XK_dead_degree
10625 #endif
10626 #ifdef XK_dead_acute
10627 || orig_keysym == XK_dead_acute
10628 #endif
10629 #ifdef XK_dead_cedilla
10630 || orig_keysym == XK_dead_cedilla
10631 #endif
10632 #ifdef XK_dead_breve
10633 || orig_keysym == XK_dead_breve
10634 #endif
10635 #ifdef XK_dead_ogonek
10636 || orig_keysym == XK_dead_ogonek
10637 #endif
10638 #ifdef XK_dead_caron
10639 || orig_keysym == XK_dead_caron
10640 #endif
10641 #ifdef XK_dead_doubleacute
10642 || orig_keysym == XK_dead_doubleacute
10643 #endif
10644 #ifdef XK_dead_abovedot
10645 || orig_keysym == XK_dead_abovedot
10646 #endif
10647 #ifdef XK_dead_abovering
10648 || orig_keysym == XK_dead_abovering
10649 #endif
10650 #ifdef XK_dead_iota
10651 || orig_keysym == XK_dead_iota
10652 #endif
10653 #ifdef XK_dead_belowdot
10654 || orig_keysym == XK_dead_belowdot
10655 #endif
10656 #ifdef XK_dead_voiced_sound
10657 || orig_keysym == XK_dead_voiced_sound
10658 #endif
10659 #ifdef XK_dead_semivoiced_sound
10660 || orig_keysym == XK_dead_semivoiced_sound
10661 #endif
10662 #ifdef XK_dead_hook
10663 || orig_keysym == XK_dead_hook
10664 #endif
10665 #ifdef XK_dead_horn
10666 || orig_keysym == XK_dead_horn
10667 #endif
10668 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10669 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10670 /* Any "vendor-specific" key is ok. */
10671 || (orig_keysym & (1 << 28))
10672 || (keysym != NoSymbol && nbytes == 0))
10673 && ! (IsModifierKey (orig_keysym)
10674 #ifndef HAVE_X11R5
10675 #ifdef XK_Mode_switch
10676 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10677 #endif
10678 #ifdef XK_Num_Lock
10679 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10680 #endif
10681 #endif /* not HAVE_X11R5 */
10682 /* The symbols from XK_ISO_Lock to
10683 XK_ISO_Last_Group_Lock doesn't have
10684 real modifiers but should be treated
10685 similarly to Mode_switch by Emacs. */
10686 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10687 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10688 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10689 #endif
10690 ))
10691 {
10692 if (temp_index == sizeof temp_buffer / sizeof (short))
10693 temp_index = 0;
10694 temp_buffer[temp_index++] = keysym;
10695 /* make_lispy_event will convert this to a symbolic
10696 key. */
10697 bufp->kind = non_ascii_keystroke;
10698 bufp->code = keysym;
10699 XSETFRAME (bufp->frame_or_window, f);
10700 bufp->arg = Qnil;
10701 bufp->modifiers
10702 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10703 modifiers);
10704 bufp->timestamp = event.xkey.time;
10705 bufp++;
10706 count++;
10707 numchars--;
10708 }
10709 else if (numchars > nbytes)
10710 { /* Raw characters, not keysym. */
10711 register int i;
10712 register int c;
10713 int nchars, len;
10714
10715 for (i = 0; i < nbytes; i++)
10716 {
10717 if (temp_index == (sizeof temp_buffer
10718 / sizeof (short)))
10719 temp_index = 0;
10720 temp_buffer[temp_index++] = copy_bufptr[i];
10721 }
10722
10723 {
10724 /* Decode the input data. */
10725 coding.destination
10726 = (unsigned char *) malloc (nbytes);
10727 if (! coding.destination)
10728 break;
10729 coding.dst_bytes = nbytes;
10730 coding.mode |= CODING_MODE_LAST_BLOCK;
10731 decode_coding_c_string (&coding, copy_bufptr,
10732 nbytes, Qnil);
10733 nbytes = coding.produced;
10734 nchars = coding.produced_char;
10735 if (copy_bufsiz < nbytes)
10736 {
10737 copy_bufsiz = nbytes;
10738 copy_bufptr = (char *) alloca (nbytes);
10739 }
10740 bcopy (coding.destination, copy_bufptr, nbytes);
10741 free (coding.destination);
10742 }
10743
10744 /* Convert the input data to a sequence of
10745 character events. */
10746 for (i = 0; i < nbytes; i += len)
10747 {
10748 if (nchars == nbytes)
10749 c = copy_bufptr[i], len = 1;
10750 else
10751 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10752 nbytes - i, len);
10753
10754 bufp->kind = (ASCII_CHAR_P (c)
10755 ? ascii_keystroke
10756 : multibyte_char_keystroke);
10757 bufp->code = c;
10758 XSETFRAME (bufp->frame_or_window, f);
10759 bufp->arg = Qnil;
10760 bufp->modifiers
10761 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10762 modifiers);
10763 bufp->timestamp = event.xkey.time;
10764 bufp++;
10765 }
10766
10767 count += nchars;
10768 numchars -= nchars;
10769
10770 if (keysym == NoSymbol)
10771 break;
10772 }
10773 else
10774 abort ();
10775 }
10776 else
10777 abort ();
10778 }
10779 #ifdef HAVE_X_I18N
10780 /* Don't dispatch this event since XtDispatchEvent calls
10781 XFilterEvent, and two calls in a row may freeze the
10782 client. */
10783 break;
10784 #else
10785 goto OTHER;
10786 #endif
10787
10788 case KeyRelease:
10789 #ifdef HAVE_X_I18N
10790 /* Don't dispatch this event since XtDispatchEvent calls
10791 XFilterEvent, and two calls in a row may freeze the
10792 client. */
10793 break;
10794 #else
10795 goto OTHER;
10796 #endif
10797
10798 /* Here's a possible interpretation of the whole
10799 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10800 you get a FocusIn event, you have to get a FocusOut
10801 event before you relinquish the focus. If you
10802 haven't received a FocusIn event, then a mere
10803 LeaveNotify is enough to free you. */
10804
10805 case EnterNotify:
10806 {
10807 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10808
10809 #if 0
10810 if (event.xcrossing.focus)
10811 {
10812 /* Avoid nasty pop/raise loops. */
10813 if (f && (!(f->auto_raise)
10814 || !(f->auto_lower)
10815 || (event.xcrossing.time - enter_timestamp) > 500))
10816 {
10817 x_new_focus_frame (dpyinfo, f);
10818 enter_timestamp = event.xcrossing.time;
10819 }
10820 }
10821 else if (f == dpyinfo->x_focus_frame)
10822 x_new_focus_frame (dpyinfo, 0);
10823 #endif
10824
10825 /* EnterNotify counts as mouse movement,
10826 so update things that depend on mouse position. */
10827 if (f && !f->output_data.x->hourglass_p)
10828 note_mouse_movement (f, &event.xmotion);
10829 goto OTHER;
10830 }
10831
10832 case FocusIn:
10833 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10834 if (event.xfocus.detail != NotifyPointer)
10835 dpyinfo->x_focus_event_frame = f;
10836 if (f)
10837 {
10838 x_new_focus_frame (dpyinfo, f);
10839
10840 /* Don't stop displaying the initial startup message
10841 for a switch-frame event we don't need. */
10842 if (GC_NILP (Vterminal_frame)
10843 && GC_CONSP (Vframe_list)
10844 && !GC_NILP (XCDR (Vframe_list)))
10845 {
10846 bufp->kind = FOCUS_IN_EVENT;
10847 XSETFRAME (bufp->frame_or_window, f);
10848 bufp->arg = Qnil;
10849 ++bufp, ++count, --numchars;
10850 }
10851 }
10852
10853 #ifdef HAVE_X_I18N
10854 if (f && FRAME_XIC (f))
10855 XSetICFocus (FRAME_XIC (f));
10856 #endif
10857
10858 goto OTHER;
10859
10860 case LeaveNotify:
10861 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10862 if (f)
10863 {
10864 if (f == dpyinfo->mouse_face_mouse_frame)
10865 {
10866 /* If we move outside the frame, then we're
10867 certainly no longer on any text in the frame. */
10868 clear_mouse_face (dpyinfo);
10869 dpyinfo->mouse_face_mouse_frame = 0;
10870 }
10871
10872 /* Generate a nil HELP_EVENT to cancel a help-echo.
10873 Do it only if there's something to cancel.
10874 Otherwise, the startup message is cleared when
10875 the mouse leaves the frame. */
10876 if (any_help_event_p)
10877 {
10878 Lisp_Object frame;
10879 int n;
10880
10881 XSETFRAME (frame, f);
10882 help_echo = Qnil;
10883 n = gen_help_event (bufp, numchars,
10884 Qnil, frame, Qnil, Qnil, 0);
10885 bufp += n, count += n, numchars -= n;
10886 }
10887
10888 #if 0
10889 if (event.xcrossing.focus)
10890 x_mouse_leave (dpyinfo);
10891 else
10892 {
10893 if (f == dpyinfo->x_focus_event_frame)
10894 dpyinfo->x_focus_event_frame = 0;
10895 if (f == dpyinfo->x_focus_frame)
10896 x_new_focus_frame (dpyinfo, 0);
10897 }
10898 #endif
10899 }
10900 goto OTHER;
10901
10902 case FocusOut:
10903 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10904 if (event.xfocus.detail != NotifyPointer
10905 && f == dpyinfo->x_focus_event_frame)
10906 dpyinfo->x_focus_event_frame = 0;
10907 if (f && f == dpyinfo->x_focus_frame)
10908 x_new_focus_frame (dpyinfo, 0);
10909
10910 #ifdef HAVE_X_I18N
10911 if (f && FRAME_XIC (f))
10912 XUnsetICFocus (FRAME_XIC (f));
10913 #endif
10914
10915 goto OTHER;
10916
10917 case MotionNotify:
10918 {
10919 previous_help_echo = help_echo;
10920 help_echo = help_echo_object = help_echo_window = Qnil;
10921 help_echo_pos = -1;
10922
10923 if (dpyinfo->grabbed && last_mouse_frame
10924 && FRAME_LIVE_P (last_mouse_frame))
10925 f = last_mouse_frame;
10926 else
10927 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10928
10929 if (dpyinfo->mouse_face_hidden)
10930 {
10931 dpyinfo->mouse_face_hidden = 0;
10932 clear_mouse_face (dpyinfo);
10933 }
10934
10935 if (f)
10936 note_mouse_movement (f, &event.xmotion);
10937 else
10938 {
10939 #ifndef USE_TOOLKIT_SCROLL_BARS
10940 struct scroll_bar *bar
10941 = x_window_to_scroll_bar (event.xmotion.window);
10942
10943 if (bar)
10944 x_scroll_bar_note_movement (bar, &event);
10945 #endif /* USE_TOOLKIT_SCROLL_BARS */
10946
10947 /* If we move outside the frame, then we're
10948 certainly no longer on any text in the frame. */
10949 clear_mouse_face (dpyinfo);
10950 }
10951
10952 /* If the contents of the global variable help_echo
10953 has changed, generate a HELP_EVENT. */
10954 if (!NILP (help_echo)
10955 || !NILP (previous_help_echo))
10956 {
10957 Lisp_Object frame;
10958 int n;
10959
10960 if (f)
10961 XSETFRAME (frame, f);
10962 else
10963 frame = Qnil;
10964
10965 any_help_event_p = 1;
10966 n = gen_help_event (bufp, numchars, help_echo, frame,
10967 help_echo_window, help_echo_object,
10968 help_echo_pos);
10969 bufp += n, count += n, numchars -= n;
10970 }
10971
10972 goto OTHER;
10973 }
10974
10975 case ConfigureNotify:
10976 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10977 if (f)
10978 {
10979 #ifndef USE_X_TOOLKIT
10980 /* If there is a pending resize for fullscreen, don't
10981 do this one, the right one will come later.
10982 The toolkit version doesn't seem to need this, but we
10983 need to reset it below. */
10984 int dont_resize =
10985 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10986 && FRAME_NEW_WIDTH (f) != 0);
10987 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10988 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10989 if (dont_resize)
10990 goto OTHER;
10991
10992 /* In the toolkit version, change_frame_size
10993 is called by the code that handles resizing
10994 of the EmacsFrame widget. */
10995
10996 /* Even if the number of character rows and columns has
10997 not changed, the font size may have changed, so we need
10998 to check the pixel dimensions as well. */
10999 if (columns != f->width
11000 || rows != f->height
11001 || event.xconfigure.width != f->output_data.x->pixel_width
11002 || event.xconfigure.height != f->output_data.x->pixel_height)
11003 {
11004 change_frame_size (f, rows, columns, 0, 1, 0);
11005 SET_FRAME_GARBAGED (f);
11006 cancel_mouse_face (f);
11007 }
11008 #endif
11009
11010 f->output_data.x->pixel_width = event.xconfigure.width;
11011 f->output_data.x->pixel_height = event.xconfigure.height;
11012
11013 /* What we have now is the position of Emacs's own window.
11014 Convert that to the position of the window manager window. */
11015 x_real_positions (f, &f->output_data.x->left_pos,
11016 &f->output_data.x->top_pos);
11017
11018 x_check_fullscreen_move(f);
11019 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11020 f->output_data.x->want_fullscreen &=
11021 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
11022 #ifdef HAVE_X_I18N
11023 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11024 xic_set_statusarea (f);
11025 #endif
11026
11027 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11028 {
11029 /* Since the WM decorations come below top_pos now,
11030 we must put them below top_pos in the future. */
11031 f->output_data.x->win_gravity = NorthWestGravity;
11032 x_wm_set_size_hint (f, (long) 0, 0);
11033 }
11034 #ifdef USE_MOTIF
11035 /* Some window managers pass (0,0) as the location of
11036 the window, and the Motif event handler stores it
11037 in the emacs widget, which messes up Motif menus. */
11038 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11039 {
11040 event.xconfigure.x = f->output_data.x->widget->core.x;
11041 event.xconfigure.y = f->output_data.x->widget->core.y;
11042 }
11043 #endif /* USE_MOTIF */
11044 }
11045 goto OTHER;
11046
11047 case ButtonPress:
11048 case ButtonRelease:
11049 {
11050 /* If we decide we want to generate an event to be seen
11051 by the rest of Emacs, we put it here. */
11052 struct input_event emacs_event;
11053 int tool_bar_p = 0;
11054
11055 emacs_event.kind = no_event;
11056 bzero (&compose_status, sizeof (compose_status));
11057
11058 if (dpyinfo->grabbed
11059 && last_mouse_frame
11060 && FRAME_LIVE_P (last_mouse_frame))
11061 f = last_mouse_frame;
11062 else
11063 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11064
11065 if (f)
11066 {
11067 /* Is this in the tool-bar? */
11068 if (WINDOWP (f->tool_bar_window)
11069 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11070 {
11071 Lisp_Object window;
11072 int p, x, y;
11073
11074 x = event.xbutton.x;
11075 y = event.xbutton.y;
11076
11077 /* Set x and y. */
11078 window = window_from_coordinates (f, x, y, &p, 1);
11079 if (EQ (window, f->tool_bar_window))
11080 {
11081 x_handle_tool_bar_click (f, &event.xbutton);
11082 tool_bar_p = 1;
11083 }
11084 }
11085
11086 if (!tool_bar_p)
11087 if (!dpyinfo->x_focus_frame
11088 || f == dpyinfo->x_focus_frame)
11089 construct_mouse_click (&emacs_event, &event, f);
11090 }
11091 else
11092 {
11093 #ifndef USE_TOOLKIT_SCROLL_BARS
11094 struct scroll_bar *bar
11095 = x_window_to_scroll_bar (event.xbutton.window);
11096
11097 if (bar)
11098 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11099 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11100 }
11101
11102 if (event.type == ButtonPress)
11103 {
11104 dpyinfo->grabbed |= (1 << event.xbutton.button);
11105 last_mouse_frame = f;
11106 /* Ignore any mouse motion that happened
11107 before this event; any subsequent mouse-movement
11108 Emacs events should reflect only motion after
11109 the ButtonPress. */
11110 if (f != 0)
11111 f->mouse_moved = 0;
11112
11113 if (!tool_bar_p)
11114 last_tool_bar_item = -1;
11115 }
11116 else
11117 {
11118 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11119 }
11120
11121 if (numchars >= 1 && emacs_event.kind != no_event)
11122 {
11123 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11124 bufp++;
11125 count++;
11126 numchars--;
11127 }
11128
11129 #ifdef USE_X_TOOLKIT
11130 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11131 /* For a down-event in the menu bar,
11132 don't pass it to Xt right now.
11133 Instead, save it away
11134 and we will pass it to Xt from kbd_buffer_get_event.
11135 That way, we can run some Lisp code first. */
11136 if (f && event.type == ButtonPress
11137 /* Verify the event is really within the menu bar
11138 and not just sent to it due to grabbing. */
11139 && event.xbutton.x >= 0
11140 && event.xbutton.x < f->output_data.x->pixel_width
11141 && event.xbutton.y >= 0
11142 && event.xbutton.y < f->output_data.x->menubar_height
11143 && event.xbutton.same_screen)
11144 {
11145 SET_SAVED_BUTTON_EVENT;
11146 XSETFRAME (last_mouse_press_frame, f);
11147 }
11148 else if (event.type == ButtonPress)
11149 {
11150 last_mouse_press_frame = Qnil;
11151 goto OTHER;
11152 }
11153
11154 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11155 but I am trying to be cautious. */
11156 else if (event.type == ButtonRelease)
11157 {
11158 if (!NILP (last_mouse_press_frame))
11159 {
11160 f = XFRAME (last_mouse_press_frame);
11161 if (f->output_data.x)
11162 SET_SAVED_BUTTON_EVENT;
11163 }
11164 else
11165 goto OTHER;
11166 }
11167 #endif /* USE_MOTIF */
11168 else
11169 goto OTHER;
11170 #endif /* USE_X_TOOLKIT */
11171 }
11172 break;
11173
11174 case CirculateNotify:
11175 goto OTHER;
11176
11177 case CirculateRequest:
11178 goto OTHER;
11179
11180 case VisibilityNotify:
11181 goto OTHER;
11182
11183 case MappingNotify:
11184 /* Someone has changed the keyboard mapping - update the
11185 local cache. */
11186 switch (event.xmapping.request)
11187 {
11188 case MappingModifier:
11189 x_find_modifier_meanings (dpyinfo);
11190 /* This is meant to fall through. */
11191 case MappingKeyboard:
11192 XRefreshKeyboardMapping (&event.xmapping);
11193 }
11194 goto OTHER;
11195
11196 default:
11197 OTHER:
11198 #ifdef USE_X_TOOLKIT
11199 BLOCK_INPUT;
11200 XtDispatchEvent (&event);
11201 UNBLOCK_INPUT;
11202 #endif /* USE_X_TOOLKIT */
11203 break;
11204 }
11205 }
11206 }
11207
11208 out:;
11209
11210 /* On some systems, an X bug causes Emacs to get no more events
11211 when the window is destroyed. Detect that. (1994.) */
11212 if (! event_found)
11213 {
11214 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11215 One XNOOP in 100 loops will make Emacs terminate.
11216 B. Bretthauer, 1994 */
11217 x_noop_count++;
11218 if (x_noop_count >= 100)
11219 {
11220 x_noop_count=0;
11221
11222 if (next_noop_dpyinfo == 0)
11223 next_noop_dpyinfo = x_display_list;
11224
11225 XNoOp (next_noop_dpyinfo->display);
11226
11227 /* Each time we get here, cycle through the displays now open. */
11228 next_noop_dpyinfo = next_noop_dpyinfo->next;
11229 }
11230 }
11231
11232 /* If the focus was just given to an auto-raising frame,
11233 raise it now. */
11234 /* ??? This ought to be able to handle more than one such frame. */
11235 if (pending_autoraise_frame)
11236 {
11237 x_raise_frame (pending_autoraise_frame);
11238 pending_autoraise_frame = 0;
11239 }
11240
11241 UNBLOCK_INPUT;
11242 --handling_signal;
11243 return count;
11244 }
11245
11246
11247
11248 \f
11249 /***********************************************************************
11250 Text Cursor
11251 ***********************************************************************/
11252
11253 /* Notice when the text cursor of window W has been completely
11254 overwritten by a drawing operation that outputs glyphs in AREA
11255 starting at X0 and ending at X1 in the line starting at Y0 and
11256 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11257 the rest of the line after X0 has been written. Y coordinates
11258 are window-relative. */
11259
11260 static void
11261 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11262 struct window *w;
11263 enum glyph_row_area area;
11264 int x0, y0, x1, y1;
11265 {
11266 if (area == TEXT_AREA
11267 && w->phys_cursor_on_p
11268 && y0 <= w->phys_cursor.y
11269 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11270 && x0 <= w->phys_cursor.x
11271 && (x1 < 0 || x1 > w->phys_cursor.x))
11272 w->phys_cursor_on_p = 0;
11273 }
11274
11275
11276 /* Set clipping for output in glyph row ROW. W is the window in which
11277 we operate. GC is the graphics context to set clipping in.
11278 WHOLE_LINE_P non-zero means include the areas used for truncation
11279 mark display and alike in the clipping rectangle.
11280
11281 ROW may be a text row or, e.g., a mode line. Text rows must be
11282 clipped to the interior of the window dedicated to text display,
11283 mode lines must be clipped to the whole window. */
11284
11285 static void
11286 x_clip_to_row (w, row, gc, whole_line_p)
11287 struct window *w;
11288 struct glyph_row *row;
11289 GC gc;
11290 int whole_line_p;
11291 {
11292 struct frame *f = XFRAME (WINDOW_FRAME (w));
11293 XRectangle clip_rect;
11294 int window_x, window_y, window_width, window_height;
11295
11296 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11297
11298 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11299 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11300 clip_rect.y = max (clip_rect.y, window_y);
11301 clip_rect.width = window_width;
11302 clip_rect.height = row->visible_height;
11303
11304 /* If clipping to the whole line, including trunc marks, extend
11305 the rectangle to the left and increase its width. */
11306 if (whole_line_p)
11307 {
11308 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11309 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11310 }
11311
11312 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11313 }
11314
11315
11316 /* Draw a hollow box cursor on window W in glyph row ROW. */
11317
11318 static void
11319 x_draw_hollow_cursor (w, row)
11320 struct window *w;
11321 struct glyph_row *row;
11322 {
11323 struct frame *f = XFRAME (WINDOW_FRAME (w));
11324 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11325 Display *dpy = FRAME_X_DISPLAY (f);
11326 int x, y, wd, h;
11327 XGCValues xgcv;
11328 struct glyph *cursor_glyph;
11329 GC gc;
11330
11331 /* Compute frame-relative coordinates from window-relative
11332 coordinates. */
11333 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11334 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11335 + row->ascent - w->phys_cursor_ascent);
11336 h = row->height - 1;
11337
11338 /* Get the glyph the cursor is on. If we can't tell because
11339 the current matrix is invalid or such, give up. */
11340 cursor_glyph = get_phys_cursor_glyph (w);
11341 if (cursor_glyph == NULL)
11342 return;
11343
11344 /* Compute the width of the rectangle to draw. If on a stretch
11345 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11346 rectangle as wide as the glyph, but use a canonical character
11347 width instead. */
11348 wd = cursor_glyph->pixel_width - 1;
11349 if (cursor_glyph->type == STRETCH_GLYPH
11350 && !x_stretch_cursor_p)
11351 wd = min (CANON_X_UNIT (f), wd);
11352
11353 /* The foreground of cursor_gc is typically the same as the normal
11354 background color, which can cause the cursor box to be invisible. */
11355 xgcv.foreground = f->output_data.x->cursor_pixel;
11356 if (dpyinfo->scratch_cursor_gc)
11357 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11358 else
11359 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11360 GCForeground, &xgcv);
11361 gc = dpyinfo->scratch_cursor_gc;
11362
11363 /* Set clipping, draw the rectangle, and reset clipping again. */
11364 x_clip_to_row (w, row, gc, 0);
11365 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11366 XSetClipMask (dpy, gc, None);
11367 }
11368
11369
11370 /* Draw a bar cursor on window W in glyph row ROW.
11371
11372 Implementation note: One would like to draw a bar cursor with an
11373 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11374 Unfortunately, I didn't find a font yet that has this property set.
11375 --gerd. */
11376
11377 static void
11378 x_draw_bar_cursor (w, row, width)
11379 struct window *w;
11380 struct glyph_row *row;
11381 int width;
11382 {
11383 struct frame *f = XFRAME (w->frame);
11384 struct glyph *cursor_glyph;
11385
11386 /* If cursor is out of bounds, don't draw garbage. This can happen
11387 in mini-buffer windows when switching between echo area glyphs
11388 and mini-buffer. */
11389 cursor_glyph = get_phys_cursor_glyph (w);
11390 if (cursor_glyph == NULL)
11391 return;
11392
11393 /* If on an image, draw like a normal cursor. That's usually better
11394 visible than drawing a bar, esp. if the image is large so that
11395 the bar might not be in the window. */
11396 if (cursor_glyph->type == IMAGE_GLYPH)
11397 {
11398 struct glyph_row *row;
11399 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11400 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11401 }
11402 else
11403 {
11404 Display *dpy = FRAME_X_DISPLAY (f);
11405 Window window = FRAME_X_WINDOW (f);
11406 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11407 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11408 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11409 XGCValues xgcv;
11410
11411 /* If the glyph's background equals the color we normally draw
11412 the bar cursor in, the bar cursor in its normal color is
11413 invisible. Use the glyph's foreground color instead in this
11414 case, on the assumption that the glyph's colors are chosen so
11415 that the glyph is legible. */
11416 if (face->background == f->output_data.x->cursor_pixel)
11417 xgcv.background = xgcv.foreground = face->foreground;
11418 else
11419 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11420 xgcv.graphics_exposures = 0;
11421
11422 if (gc)
11423 XChangeGC (dpy, gc, mask, &xgcv);
11424 else
11425 {
11426 gc = XCreateGC (dpy, window, mask, &xgcv);
11427 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11428 }
11429
11430 if (width < 0)
11431 width = f->output_data.x->cursor_width;
11432 width = min (cursor_glyph->pixel_width, width);
11433
11434 x_clip_to_row (w, row, gc, 0);
11435 XFillRectangle (dpy, window, gc,
11436 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11437 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11438 width, row->height);
11439 XSetClipMask (dpy, gc, None);
11440 }
11441 }
11442
11443
11444 /* Clear the cursor of window W to background color, and mark the
11445 cursor as not shown. This is used when the text where the cursor
11446 is is about to be rewritten. */
11447
11448 static void
11449 x_clear_cursor (w)
11450 struct window *w;
11451 {
11452 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11453 x_update_window_cursor (w, 0);
11454 }
11455
11456
11457 /* Draw the cursor glyph of window W in glyph row ROW. See the
11458 comment of x_draw_glyphs for the meaning of HL. */
11459
11460 static void
11461 x_draw_phys_cursor_glyph (w, row, hl)
11462 struct window *w;
11463 struct glyph_row *row;
11464 enum draw_glyphs_face hl;
11465 {
11466 /* If cursor hpos is out of bounds, don't draw garbage. This can
11467 happen in mini-buffer windows when switching between echo area
11468 glyphs and mini-buffer. */
11469 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11470 {
11471 int on_p = w->phys_cursor_on_p;
11472
11473 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11474 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11475 hl, 0);
11476 w->phys_cursor_on_p = on_p;
11477
11478 /* When we erase the cursor, and ROW is overlapped by other
11479 rows, make sure that these overlapping parts of other rows
11480 are redrawn. */
11481 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11482 {
11483 if (row > w->current_matrix->rows
11484 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11485 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11486
11487 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11488 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11489 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11490 }
11491 }
11492 }
11493
11494
11495 /* Erase the image of a cursor of window W from the screen. */
11496
11497 static void
11498 x_erase_phys_cursor (w)
11499 struct window *w;
11500 {
11501 struct frame *f = XFRAME (w->frame);
11502 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11503 int hpos = w->phys_cursor.hpos;
11504 int vpos = w->phys_cursor.vpos;
11505 int mouse_face_here_p = 0;
11506 struct glyph_matrix *active_glyphs = w->current_matrix;
11507 struct glyph_row *cursor_row;
11508 struct glyph *cursor_glyph;
11509 enum draw_glyphs_face hl;
11510
11511 /* No cursor displayed or row invalidated => nothing to do on the
11512 screen. */
11513 if (w->phys_cursor_type == NO_CURSOR)
11514 goto mark_cursor_off;
11515
11516 /* VPOS >= active_glyphs->nrows means that window has been resized.
11517 Don't bother to erase the cursor. */
11518 if (vpos >= active_glyphs->nrows)
11519 goto mark_cursor_off;
11520
11521 /* If row containing cursor is marked invalid, there is nothing we
11522 can do. */
11523 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11524 if (!cursor_row->enabled_p)
11525 goto mark_cursor_off;
11526
11527 /* If row is completely invisible, don't attempt to delete a cursor which
11528 isn't there. This can happen if cursor is at top of a window, and
11529 we switch to a buffer with a header line in that window. */
11530 if (cursor_row->visible_height <= 0)
11531 goto mark_cursor_off;
11532
11533 /* This can happen when the new row is shorter than the old one.
11534 In this case, either x_draw_glyphs or clear_end_of_line
11535 should have cleared the cursor. Note that we wouldn't be
11536 able to erase the cursor in this case because we don't have a
11537 cursor glyph at hand. */
11538 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11539 goto mark_cursor_off;
11540
11541 /* If the cursor is in the mouse face area, redisplay that when
11542 we clear the cursor. */
11543 if (! NILP (dpyinfo->mouse_face_window)
11544 && w == XWINDOW (dpyinfo->mouse_face_window)
11545 && (vpos > dpyinfo->mouse_face_beg_row
11546 || (vpos == dpyinfo->mouse_face_beg_row
11547 && hpos >= dpyinfo->mouse_face_beg_col))
11548 && (vpos < dpyinfo->mouse_face_end_row
11549 || (vpos == dpyinfo->mouse_face_end_row
11550 && hpos < dpyinfo->mouse_face_end_col))
11551 /* Don't redraw the cursor's spot in mouse face if it is at the
11552 end of a line (on a newline). The cursor appears there, but
11553 mouse highlighting does not. */
11554 && cursor_row->used[TEXT_AREA] > hpos)
11555 mouse_face_here_p = 1;
11556
11557 /* Maybe clear the display under the cursor. */
11558 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11559 {
11560 int x;
11561 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11562
11563 cursor_glyph = get_phys_cursor_glyph (w);
11564 if (cursor_glyph == NULL)
11565 goto mark_cursor_off;
11566
11567 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11568
11569 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11570 x,
11571 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11572 cursor_row->y)),
11573 cursor_glyph->pixel_width,
11574 cursor_row->visible_height,
11575 False);
11576 }
11577
11578 /* Erase the cursor by redrawing the character underneath it. */
11579 if (mouse_face_here_p)
11580 hl = DRAW_MOUSE_FACE;
11581 else
11582 hl = DRAW_NORMAL_TEXT;
11583 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11584
11585 mark_cursor_off:
11586 w->phys_cursor_on_p = 0;
11587 w->phys_cursor_type = NO_CURSOR;
11588 }
11589
11590
11591 /* Non-zero if physical cursor of window W is within mouse face. */
11592
11593 static int
11594 cursor_in_mouse_face_p (w)
11595 struct window *w;
11596 {
11597 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11598 int in_mouse_face = 0;
11599
11600 if (WINDOWP (dpyinfo->mouse_face_window)
11601 && XWINDOW (dpyinfo->mouse_face_window) == w)
11602 {
11603 int hpos = w->phys_cursor.hpos;
11604 int vpos = w->phys_cursor.vpos;
11605
11606 if (vpos >= dpyinfo->mouse_face_beg_row
11607 && vpos <= dpyinfo->mouse_face_end_row
11608 && (vpos > dpyinfo->mouse_face_beg_row
11609 || hpos >= dpyinfo->mouse_face_beg_col)
11610 && (vpos < dpyinfo->mouse_face_end_row
11611 || hpos < dpyinfo->mouse_face_end_col
11612 || dpyinfo->mouse_face_past_end))
11613 in_mouse_face = 1;
11614 }
11615
11616 return in_mouse_face;
11617 }
11618
11619
11620 /* Display or clear cursor of window W. If ON is zero, clear the
11621 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11622 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11623
11624 void
11625 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11626 struct window *w;
11627 int on, hpos, vpos, x, y;
11628 {
11629 struct frame *f = XFRAME (w->frame);
11630 int new_cursor_type;
11631 int new_cursor_width;
11632 struct glyph_matrix *current_glyphs;
11633 struct glyph_row *glyph_row;
11634 struct glyph *glyph;
11635 int cursor_non_selected;
11636
11637 /* This is pointless on invisible frames, and dangerous on garbaged
11638 windows and frames; in the latter case, the frame or window may
11639 be in the midst of changing its size, and x and y may be off the
11640 window. */
11641 if (! FRAME_VISIBLE_P (f)
11642 || FRAME_GARBAGED_P (f)
11643 || vpos >= w->current_matrix->nrows
11644 || hpos >= w->current_matrix->matrix_w)
11645 return;
11646
11647 /* If cursor is off and we want it off, return quickly. */
11648 if (!on && !w->phys_cursor_on_p)
11649 return;
11650
11651 current_glyphs = w->current_matrix;
11652 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11653 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11654
11655 /* If cursor row is not enabled, we don't really know where to
11656 display the cursor. */
11657 if (!glyph_row->enabled_p)
11658 {
11659 w->phys_cursor_on_p = 0;
11660 return;
11661 }
11662
11663 xassert (interrupt_input_blocked);
11664
11665 /* Set new_cursor_type to the cursor we want to be displayed. In a
11666 mini-buffer window, we want the cursor only to appear if we are
11667 reading input from this window. For the selected window, we want
11668 the cursor type given by the frame parameter. If explicitly
11669 marked off, draw no cursor. In all other cases, we want a hollow
11670 box cursor. */
11671 cursor_non_selected
11672 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11673 w->buffer));
11674 new_cursor_width = -1;
11675 if (cursor_in_echo_area
11676 && FRAME_HAS_MINIBUF_P (f)
11677 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11678 {
11679 if (w == XWINDOW (echo_area_window))
11680 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11681 else if (cursor_non_selected)
11682 new_cursor_type = HOLLOW_BOX_CURSOR;
11683 else
11684 new_cursor_type = NO_CURSOR;
11685 }
11686 else
11687 {
11688 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11689 || w != XWINDOW (f->selected_window))
11690 {
11691 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11692 || !cursor_non_selected
11693 || NILP (XBUFFER (w->buffer)->cursor_type))
11694 new_cursor_type = NO_CURSOR;
11695 else
11696 new_cursor_type = HOLLOW_BOX_CURSOR;
11697 }
11698 else if (w->cursor_off_p)
11699 new_cursor_type = NO_CURSOR;
11700 else
11701 {
11702 struct buffer *b = XBUFFER (w->buffer);
11703
11704 if (EQ (b->cursor_type, Qt))
11705 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11706 else
11707 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11708 &new_cursor_width);
11709 }
11710 }
11711
11712 /* If cursor is currently being shown and we don't want it to be or
11713 it is in the wrong place, or the cursor type is not what we want,
11714 erase it. */
11715 if (w->phys_cursor_on_p
11716 && (!on
11717 || w->phys_cursor.x != x
11718 || w->phys_cursor.y != y
11719 || new_cursor_type != w->phys_cursor_type))
11720 x_erase_phys_cursor (w);
11721
11722 /* If the cursor is now invisible and we want it to be visible,
11723 display it. */
11724 if (on && !w->phys_cursor_on_p)
11725 {
11726 w->phys_cursor_ascent = glyph_row->ascent;
11727 w->phys_cursor_height = glyph_row->height;
11728
11729 /* Set phys_cursor_.* before x_draw_.* is called because some
11730 of them may need the information. */
11731 w->phys_cursor.x = x;
11732 w->phys_cursor.y = glyph_row->y;
11733 w->phys_cursor.hpos = hpos;
11734 w->phys_cursor.vpos = vpos;
11735 w->phys_cursor_type = new_cursor_type;
11736 w->phys_cursor_on_p = 1;
11737
11738 switch (new_cursor_type)
11739 {
11740 case HOLLOW_BOX_CURSOR:
11741 x_draw_hollow_cursor (w, glyph_row);
11742 break;
11743
11744 case FILLED_BOX_CURSOR:
11745 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11746 break;
11747
11748 case BAR_CURSOR:
11749 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11750 break;
11751
11752 case NO_CURSOR:
11753 break;
11754
11755 default:
11756 abort ();
11757 }
11758
11759 #ifdef HAVE_X_I18N
11760 if (w == XWINDOW (f->selected_window))
11761 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11762 xic_set_preeditarea (w, x, y);
11763 #endif
11764 }
11765
11766 #ifndef XFlush
11767 if (updating_frame != f)
11768 XFlush (FRAME_X_DISPLAY (f));
11769 #endif
11770 }
11771
11772
11773 /* Display the cursor on window W, or clear it. X and Y are window
11774 relative pixel coordinates. HPOS and VPOS are glyph matrix
11775 positions. If W is not the selected window, display a hollow
11776 cursor. ON non-zero means display the cursor at X, Y which
11777 correspond to HPOS, VPOS, otherwise it is cleared. */
11778
11779 void
11780 x_display_cursor (w, on, hpos, vpos, x, y)
11781 struct window *w;
11782 int on, hpos, vpos, x, y;
11783 {
11784 BLOCK_INPUT;
11785 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11786 UNBLOCK_INPUT;
11787 }
11788
11789
11790 /* Display the cursor on window W, or clear it, according to ON_P.
11791 Don't change the cursor's position. */
11792
11793 void
11794 x_update_cursor (f, on_p)
11795 struct frame *f;
11796 {
11797 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11798 }
11799
11800
11801 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11802 in the window tree rooted at W. */
11803
11804 static void
11805 x_update_cursor_in_window_tree (w, on_p)
11806 struct window *w;
11807 int on_p;
11808 {
11809 while (w)
11810 {
11811 if (!NILP (w->hchild))
11812 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11813 else if (!NILP (w->vchild))
11814 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11815 else
11816 x_update_window_cursor (w, on_p);
11817
11818 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11819 }
11820 }
11821
11822
11823 /* Switch the display of W's cursor on or off, according to the value
11824 of ON. */
11825
11826 static void
11827 x_update_window_cursor (w, on)
11828 struct window *w;
11829 int on;
11830 {
11831 /* Don't update cursor in windows whose frame is in the process
11832 of being deleted. */
11833 if (w->current_matrix)
11834 {
11835 BLOCK_INPUT;
11836 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11837 w->phys_cursor.x, w->phys_cursor.y);
11838 UNBLOCK_INPUT;
11839 }
11840 }
11841
11842
11843
11844 \f
11845 /* Icons. */
11846
11847 /* Make the x-window of frame F use the gnu icon bitmap. */
11848
11849 int
11850 x_bitmap_icon (f, file)
11851 struct frame *f;
11852 Lisp_Object file;
11853 {
11854 int bitmap_id;
11855
11856 if (FRAME_X_WINDOW (f) == 0)
11857 return 1;
11858
11859 /* Free up our existing icon bitmap if any. */
11860 if (f->output_data.x->icon_bitmap > 0)
11861 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11862 f->output_data.x->icon_bitmap = 0;
11863
11864 if (STRINGP (file))
11865 bitmap_id = x_create_bitmap_from_file (f, file);
11866 else
11867 {
11868 /* Create the GNU bitmap if necessary. */
11869 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11870 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11871 = x_create_bitmap_from_data (f, gnu_bits,
11872 gnu_width, gnu_height);
11873
11874 /* The first time we create the GNU bitmap,
11875 this increments the ref-count one extra time.
11876 As a result, the GNU bitmap is never freed.
11877 That way, we don't have to worry about allocating it again. */
11878 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11879
11880 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11881 }
11882
11883 x_wm_set_icon_pixmap (f, bitmap_id);
11884 f->output_data.x->icon_bitmap = bitmap_id;
11885
11886 return 0;
11887 }
11888
11889
11890 /* Make the x-window of frame F use a rectangle with text.
11891 Use ICON_NAME as the text. */
11892
11893 int
11894 x_text_icon (f, icon_name)
11895 struct frame *f;
11896 char *icon_name;
11897 {
11898 if (FRAME_X_WINDOW (f) == 0)
11899 return 1;
11900
11901 #ifdef HAVE_X11R4
11902 {
11903 XTextProperty text;
11904 text.value = (unsigned char *) icon_name;
11905 text.encoding = XA_STRING;
11906 text.format = 8;
11907 text.nitems = strlen (icon_name);
11908 #ifdef USE_X_TOOLKIT
11909 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11910 &text);
11911 #else /* not USE_X_TOOLKIT */
11912 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11913 #endif /* not USE_X_TOOLKIT */
11914 }
11915 #else /* not HAVE_X11R4 */
11916 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11917 #endif /* not HAVE_X11R4 */
11918
11919 if (f->output_data.x->icon_bitmap > 0)
11920 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11921 f->output_data.x->icon_bitmap = 0;
11922 x_wm_set_icon_pixmap (f, 0);
11923
11924 return 0;
11925 }
11926 \f
11927 #define X_ERROR_MESSAGE_SIZE 200
11928
11929 /* If non-nil, this should be a string.
11930 It means catch X errors and store the error message in this string. */
11931
11932 static Lisp_Object x_error_message_string;
11933
11934 /* An X error handler which stores the error message in
11935 x_error_message_string. This is called from x_error_handler if
11936 x_catch_errors is in effect. */
11937
11938 static void
11939 x_error_catcher (display, error)
11940 Display *display;
11941 XErrorEvent *error;
11942 {
11943 XGetErrorText (display, error->error_code,
11944 XSTRING (x_error_message_string)->data,
11945 X_ERROR_MESSAGE_SIZE);
11946 }
11947
11948 /* Begin trapping X errors for display DPY. Actually we trap X errors
11949 for all displays, but DPY should be the display you are actually
11950 operating on.
11951
11952 After calling this function, X protocol errors no longer cause
11953 Emacs to exit; instead, they are recorded in the string
11954 stored in x_error_message_string.
11955
11956 Calling x_check_errors signals an Emacs error if an X error has
11957 occurred since the last call to x_catch_errors or x_check_errors.
11958
11959 Calling x_uncatch_errors resumes the normal error handling. */
11960
11961 void x_check_errors ();
11962 static Lisp_Object x_catch_errors_unwind ();
11963
11964 int
11965 x_catch_errors (dpy)
11966 Display *dpy;
11967 {
11968 int count = specpdl_ptr - specpdl;
11969
11970 /* Make sure any errors from previous requests have been dealt with. */
11971 XSync (dpy, False);
11972
11973 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11974
11975 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11976 XSTRING (x_error_message_string)->data[0] = 0;
11977
11978 return count;
11979 }
11980
11981 /* Unbind the binding that we made to check for X errors. */
11982
11983 static Lisp_Object
11984 x_catch_errors_unwind (old_val)
11985 Lisp_Object old_val;
11986 {
11987 x_error_message_string = old_val;
11988 return Qnil;
11989 }
11990
11991 /* If any X protocol errors have arrived since the last call to
11992 x_catch_errors or x_check_errors, signal an Emacs error using
11993 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11994
11995 void
11996 x_check_errors (dpy, format)
11997 Display *dpy;
11998 char *format;
11999 {
12000 /* Make sure to catch any errors incurred so far. */
12001 XSync (dpy, False);
12002
12003 if (XSTRING (x_error_message_string)->data[0])
12004 error (format, XSTRING (x_error_message_string)->data);
12005 }
12006
12007 /* Nonzero if we had any X protocol errors
12008 since we did x_catch_errors on DPY. */
12009
12010 int
12011 x_had_errors_p (dpy)
12012 Display *dpy;
12013 {
12014 /* Make sure to catch any errors incurred so far. */
12015 XSync (dpy, False);
12016
12017 return XSTRING (x_error_message_string)->data[0] != 0;
12018 }
12019
12020 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12021
12022 void
12023 x_clear_errors (dpy)
12024 Display *dpy;
12025 {
12026 XSTRING (x_error_message_string)->data[0] = 0;
12027 }
12028
12029 /* Stop catching X protocol errors and let them make Emacs die.
12030 DPY should be the display that was passed to x_catch_errors.
12031 COUNT should be the value that was returned by
12032 the corresponding call to x_catch_errors. */
12033
12034 void
12035 x_uncatch_errors (dpy, count)
12036 Display *dpy;
12037 int count;
12038 {
12039 unbind_to (count, Qnil);
12040 }
12041
12042 #if 0
12043 static unsigned int x_wire_count;
12044 x_trace_wire ()
12045 {
12046 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12047 }
12048 #endif /* ! 0 */
12049
12050 \f
12051 /* Handle SIGPIPE, which can happen when the connection to a server
12052 simply goes away. SIGPIPE is handled by x_connection_signal.
12053 Don't need to do anything, because the write which caused the
12054 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12055 which will do the appropriate cleanup for us. */
12056
12057 static SIGTYPE
12058 x_connection_signal (signalnum) /* If we don't have an argument, */
12059 int signalnum; /* some compilers complain in signal calls. */
12060 {
12061 #ifdef USG
12062 /* USG systems forget handlers when they are used;
12063 must reestablish each time */
12064 signal (signalnum, x_connection_signal);
12065 #endif /* USG */
12066 }
12067
12068 \f
12069 /************************************************************************
12070 Handling X errors
12071 ************************************************************************/
12072
12073 /* Error message passed to x_connection_closed. */
12074
12075 static char *error_msg;
12076
12077 /* Function installed as fatal_error_signal_hook in
12078 x_connection_closed. Print the X error message, and exit normally,
12079 instead of dumping core when XtCloseDisplay fails. */
12080
12081 static void
12082 x_fatal_error_signal ()
12083 {
12084 fprintf (stderr, "%s\n", error_msg);
12085 exit (70);
12086 }
12087
12088 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12089 the text of an error message that lead to the connection loss. */
12090
12091 static SIGTYPE
12092 x_connection_closed (dpy, error_message)
12093 Display *dpy;
12094 char *error_message;
12095 {
12096 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12097 Lisp_Object frame, tail;
12098 int count;
12099
12100 error_msg = (char *) alloca (strlen (error_message) + 1);
12101 strcpy (error_msg, error_message);
12102 handling_signal = 0;
12103
12104 /* Prevent being called recursively because of an error condition
12105 below. Otherwise, we might end up with printing ``can't find per
12106 display information'' in the recursive call instead of printing
12107 the original message here. */
12108 count = x_catch_errors (dpy);
12109
12110 /* We have to close the display to inform Xt that it doesn't
12111 exist anymore. If we don't, Xt will continue to wait for
12112 events from the display. As a consequence, a sequence of
12113
12114 M-x make-frame-on-display RET :1 RET
12115 ...kill the new frame, so that we get an IO error...
12116 M-x make-frame-on-display RET :1 RET
12117
12118 will indefinitely wait in Xt for events for display `:1', opened
12119 in the first class to make-frame-on-display.
12120
12121 Closing the display is reported to lead to a bus error on
12122 OpenWindows in certain situations. I suspect that is a bug
12123 in OpenWindows. I don't know how to cicumvent it here. */
12124
12125 #ifdef USE_X_TOOLKIT
12126 /* If DPYINFO is null, this means we didn't open the display
12127 in the first place, so don't try to close it. */
12128 if (dpyinfo)
12129 {
12130 extern void (*fatal_error_signal_hook) P_ ((void));
12131 fatal_error_signal_hook = x_fatal_error_signal;
12132 XtCloseDisplay (dpy);
12133 fatal_error_signal_hook = NULL;
12134 }
12135 #endif
12136
12137 /* Indicate that this display is dead. */
12138 if (dpyinfo)
12139 dpyinfo->display = 0;
12140
12141 /* First delete frames whose mini-buffers are on frames
12142 that are on the dead display. */
12143 FOR_EACH_FRAME (tail, frame)
12144 {
12145 Lisp_Object minibuf_frame;
12146 minibuf_frame
12147 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12148 if (FRAME_X_P (XFRAME (frame))
12149 && FRAME_X_P (XFRAME (minibuf_frame))
12150 && ! EQ (frame, minibuf_frame)
12151 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12152 Fdelete_frame (frame, Qt);
12153 }
12154
12155 /* Now delete all remaining frames on the dead display.
12156 We are now sure none of these is used as the mini-buffer
12157 for another frame that we need to delete. */
12158 FOR_EACH_FRAME (tail, frame)
12159 if (FRAME_X_P (XFRAME (frame))
12160 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12161 {
12162 /* Set this to t so that Fdelete_frame won't get confused
12163 trying to find a replacement. */
12164 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12165 Fdelete_frame (frame, Qt);
12166 }
12167
12168 if (dpyinfo)
12169 x_delete_display (dpyinfo);
12170
12171 x_uncatch_errors (dpy, count);
12172
12173 if (x_display_list == 0)
12174 {
12175 fprintf (stderr, "%s\n", error_msg);
12176 shut_down_emacs (0, 0, Qnil);
12177 exit (70);
12178 }
12179
12180 /* Ordinary stack unwind doesn't deal with these. */
12181 #ifdef SIGIO
12182 sigunblock (sigmask (SIGIO));
12183 #endif
12184 sigunblock (sigmask (SIGALRM));
12185 TOTALLY_UNBLOCK_INPUT;
12186
12187 clear_waiting_for_input ();
12188 error ("%s", error_msg);
12189 }
12190
12191
12192 /* This is the usual handler for X protocol errors.
12193 It kills all frames on the display that we got the error for.
12194 If that was the only one, it prints an error message and kills Emacs. */
12195
12196 static void
12197 x_error_quitter (display, error)
12198 Display *display;
12199 XErrorEvent *error;
12200 {
12201 char buf[256], buf1[356];
12202
12203 /* Note that there is no real way portable across R3/R4 to get the
12204 original error handler. */
12205
12206 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12207 sprintf (buf1, "X protocol error: %s on protocol request %d",
12208 buf, error->request_code);
12209 x_connection_closed (display, buf1);
12210 }
12211
12212
12213 /* This is the first-level handler for X protocol errors.
12214 It calls x_error_quitter or x_error_catcher. */
12215
12216 static int
12217 x_error_handler (display, error)
12218 Display *display;
12219 XErrorEvent *error;
12220 {
12221 if (! NILP (x_error_message_string))
12222 x_error_catcher (display, error);
12223 else
12224 x_error_quitter (display, error);
12225 return 0;
12226 }
12227
12228 /* This is the handler for X IO errors, always.
12229 It kills all frames on the display that we lost touch with.
12230 If that was the only one, it prints an error message and kills Emacs. */
12231
12232 static int
12233 x_io_error_quitter (display)
12234 Display *display;
12235 {
12236 char buf[256];
12237
12238 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12239 x_connection_closed (display, buf);
12240 return 0;
12241 }
12242 \f
12243 /* Changing the font of the frame. */
12244
12245 /* Give frame F the font named FONTNAME as its default font, and
12246 return the full name of that font. FONTNAME may be a wildcard
12247 pattern; in that case, we choose some font that fits the pattern.
12248 The return value shows which font we chose. */
12249
12250 Lisp_Object
12251 x_new_font (f, fontname)
12252 struct frame *f;
12253 register char *fontname;
12254 {
12255 struct font_info *fontp
12256 = FS_LOAD_FONT (f, fontname);
12257
12258 if (!fontp)
12259 return Qnil;
12260
12261 if (f->output_data.x->font == (XFontStruct *) (fontp->font))
12262 /* This font is already set in frame F. There's nothing more to
12263 do. */
12264 return build_string (fontp->full_name);
12265
12266 f->output_data.x->font = (XFontStruct *) (fontp->font);
12267 f->output_data.x->baseline_offset = fontp->baseline_offset;
12268 f->output_data.x->fontset = -1;
12269
12270 x_compute_fringe_widths (f, 1);
12271
12272 /* Compute the scroll bar width in character columns. */
12273 if (f->scroll_bar_pixel_width > 0)
12274 {
12275 int wid = FONT_WIDTH (f->output_data.x->font);
12276 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12277 }
12278 else
12279 {
12280 int wid = FONT_WIDTH (f->output_data.x->font);
12281 f->scroll_bar_cols = (14 + wid - 1) / wid;
12282 }
12283
12284 /* Now make the frame display the given font. */
12285 if (FRAME_X_WINDOW (f) != 0)
12286 {
12287 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12288 f->output_data.x->font->fid);
12289 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12290 f->output_data.x->font->fid);
12291 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12292 f->output_data.x->font->fid);
12293
12294 frame_update_line_height (f);
12295
12296 /* Don't change the size of a tip frame; there's no point in
12297 doing it because it's done in Fx_show_tip, and it leads to
12298 problems because the tip frame has no widget. */
12299 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12300 x_set_window_size (f, 0, f->width, f->height);
12301 }
12302 else
12303 /* If we are setting a new frame's font for the first time,
12304 there are no faces yet, so this font's height is the line height. */
12305 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12306
12307 return build_string (fontp->full_name);
12308 }
12309
12310 /* Give frame F the fontset named FONTSETNAME as its default fontset,
12311 and return the full name of that fontset. FONTSETNAME may be a
12312 wildcard pattern; in that case, we choose some fontset that fits
12313 the pattern. FONTSETNAME may be a font name for ASCII characters;
12314 in that case, we create a fontset from that font name.
12315
12316 The return value shows which fontset we chose.
12317 If FONTSETNAME specifies the default fontset, return Qt.
12318 If an ASCII font in the specified fontset can't be loaded, return
12319 Qnil. */
12320
12321 Lisp_Object
12322 x_new_fontset (f, fontsetname)
12323 struct frame *f;
12324 Lisp_Object fontsetname;
12325 {
12326 int fontset = fs_query_fontset (fontsetname, 0);
12327 Lisp_Object result;
12328
12329 if (fontset > 0 && f->output_data.x->fontset == fontset)
12330 /* This fontset is already set in frame F. There's nothing more
12331 to do. */
12332 return fontset_name (fontset);
12333 else if (fontset == 0)
12334 /* The default fontset can't be the default font. */
12335 return Qt;
12336
12337 if (fontset >= 0)
12338 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12339 else
12340 result = x_new_font (f, XSTRING (fontsetname)->data);
12341
12342 if (!STRINGP (result))
12343 /* Can't load ASCII font. */
12344 return Qnil;
12345
12346 if (fontset < 0)
12347 fontset = new_fontset_from_font_name (result);
12348
12349 /* Since x_new_font doesn't update any fontset information, do it now. */
12350 f->output_data.x->fontset = fontset;
12351
12352 #ifdef HAVE_X_I18N
12353 if (FRAME_XIC (f)
12354 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12355 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12356 #endif
12357
12358 return fontset_name (fontset);
12359 }
12360
12361 /* Compute actual fringe widths */
12362
12363 void
12364 x_compute_fringe_widths (f, redraw)
12365 struct frame *f;
12366 int redraw;
12367 {
12368 int o_left = f->output_data.x->left_fringe_width;
12369 int o_right = f->output_data.x->right_fringe_width;
12370 int o_cols = f->output_data.x->fringe_cols;
12371
12372 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12373 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12374 int left_fringe_width, right_fringe_width;
12375
12376 if (!NILP (left_fringe))
12377 left_fringe = Fcdr (left_fringe);
12378 if (!NILP (right_fringe))
12379 right_fringe = Fcdr (right_fringe);
12380
12381 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12382 XINT (left_fringe));
12383 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12384 XINT (right_fringe));
12385
12386 if (left_fringe_width || right_fringe_width)
12387 {
12388 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12389 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12390 int conf_wid = left_wid + right_wid;
12391 int font_wid = FONT_WIDTH (f->output_data.x->font);
12392 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12393 int real_wid = cols * font_wid;
12394 if (left_wid && right_wid)
12395 {
12396 if (left_fringe_width < 0)
12397 {
12398 /* Left fringe width is fixed, adjust right fringe if necessary */
12399 f->output_data.x->left_fringe_width = left_wid;
12400 f->output_data.x->right_fringe_width = real_wid - left_wid;
12401 }
12402 else if (right_fringe_width < 0)
12403 {
12404 /* Right fringe width is fixed, adjust left fringe if necessary */
12405 f->output_data.x->left_fringe_width = real_wid - right_wid;
12406 f->output_data.x->right_fringe_width = right_wid;
12407 }
12408 else
12409 {
12410 /* Adjust both fringes with an equal amount.
12411 Note that we are doing integer arithmetic here, so don't
12412 lose a pixel if the total width is an odd number. */
12413 int fill = real_wid - conf_wid;
12414 f->output_data.x->left_fringe_width = left_wid + fill/2;
12415 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12416 }
12417 }
12418 else if (left_fringe_width)
12419 {
12420 f->output_data.x->left_fringe_width = real_wid;
12421 f->output_data.x->right_fringe_width = 0;
12422 }
12423 else
12424 {
12425 f->output_data.x->left_fringe_width = 0;
12426 f->output_data.x->right_fringe_width = real_wid;
12427 }
12428 f->output_data.x->fringe_cols = cols;
12429 f->output_data.x->fringes_extra = real_wid;
12430 }
12431 else
12432 {
12433 f->output_data.x->left_fringe_width = 0;
12434 f->output_data.x->right_fringe_width = 0;
12435 f->output_data.x->fringe_cols = 0;
12436 f->output_data.x->fringes_extra = 0;
12437 }
12438
12439 if (redraw && FRAME_VISIBLE_P (f))
12440 if (o_left != f->output_data.x->left_fringe_width ||
12441 o_right != f->output_data.x->right_fringe_width ||
12442 o_cols != f->output_data.x->fringe_cols)
12443 redraw_frame (f);
12444 }
12445 \f
12446 /***********************************************************************
12447 X Input Methods
12448 ***********************************************************************/
12449
12450 #ifdef HAVE_X_I18N
12451
12452 #ifdef HAVE_X11R6
12453
12454 /* XIM destroy callback function, which is called whenever the
12455 connection to input method XIM dies. CLIENT_DATA contains a
12456 pointer to the x_display_info structure corresponding to XIM. */
12457
12458 static void
12459 xim_destroy_callback (xim, client_data, call_data)
12460 XIM xim;
12461 XPointer client_data;
12462 XPointer call_data;
12463 {
12464 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12465 Lisp_Object frame, tail;
12466
12467 BLOCK_INPUT;
12468
12469 /* No need to call XDestroyIC.. */
12470 FOR_EACH_FRAME (tail, frame)
12471 {
12472 struct frame *f = XFRAME (frame);
12473 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12474 {
12475 FRAME_XIC (f) = NULL;
12476 if (FRAME_XIC_FONTSET (f))
12477 {
12478 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12479 FRAME_XIC_FONTSET (f) = NULL;
12480 }
12481 }
12482 }
12483
12484 /* No need to call XCloseIM. */
12485 dpyinfo->xim = NULL;
12486 XFree (dpyinfo->xim_styles);
12487 UNBLOCK_INPUT;
12488 }
12489
12490 #endif /* HAVE_X11R6 */
12491
12492 /* Open the connection to the XIM server on display DPYINFO.
12493 RESOURCE_NAME is the resource name Emacs uses. */
12494
12495 static void
12496 xim_open_dpy (dpyinfo, resource_name)
12497 struct x_display_info *dpyinfo;
12498 char *resource_name;
12499 {
12500 XIM xim;
12501
12502 if (use_xim)
12503 {
12504 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
12505 EMACS_CLASS);
12506 dpyinfo->xim = xim;
12507
12508 if (xim)
12509 {
12510 #ifdef HAVE_X11R6
12511 XIMCallback destroy;
12512 #endif
12513
12514 /* Get supported styles and XIM values. */
12515 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12516
12517 #ifdef HAVE_X11R6
12518 destroy.callback = xim_destroy_callback;
12519 destroy.client_data = (XPointer)dpyinfo;
12520 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12521 #endif
12522 }
12523 }
12524 else
12525 dpyinfo->xim = NULL;
12526 }
12527
12528
12529 #ifdef HAVE_X11R6_XIM
12530
12531 struct xim_inst_t
12532 {
12533 struct x_display_info *dpyinfo;
12534 char *resource_name;
12535 };
12536
12537 /* XIM instantiate callback function, which is called whenever an XIM
12538 server is available. DISPLAY is teh display of the XIM.
12539 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12540 when the callback was registered. */
12541
12542 static void
12543 xim_instantiate_callback (display, client_data, call_data)
12544 Display *display;
12545 XPointer client_data;
12546 XPointer call_data;
12547 {
12548 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12549 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12550
12551 /* We don't support multiple XIM connections. */
12552 if (dpyinfo->xim)
12553 return;
12554
12555 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12556
12557 /* Create XIC for the existing frames on the same display, as long
12558 as they have no XIC. */
12559 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12560 {
12561 Lisp_Object tail, frame;
12562
12563 BLOCK_INPUT;
12564 FOR_EACH_FRAME (tail, frame)
12565 {
12566 struct frame *f = XFRAME (frame);
12567
12568 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12569 if (FRAME_XIC (f) == NULL)
12570 {
12571 create_frame_xic (f);
12572 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12573 xic_set_statusarea (f);
12574 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12575 {
12576 struct window *w = XWINDOW (f->selected_window);
12577 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12578 }
12579 }
12580 }
12581
12582 UNBLOCK_INPUT;
12583 }
12584 }
12585
12586 #endif /* HAVE_X11R6_XIM */
12587
12588
12589 /* Open a connection to the XIM server on display DPYINFO.
12590 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12591 connection only at the first time. On X11R6, open the connection
12592 in the XIM instantiate callback function. */
12593
12594 static void
12595 xim_initialize (dpyinfo, resource_name)
12596 struct x_display_info *dpyinfo;
12597 char *resource_name;
12598 {
12599 if (use_xim)
12600 {
12601 #ifdef HAVE_X11R6_XIM
12602 struct xim_inst_t *xim_inst;
12603 int len;
12604
12605 dpyinfo->xim = NULL;
12606 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12607 xim_inst->dpyinfo = dpyinfo;
12608 len = strlen (resource_name);
12609 xim_inst->resource_name = (char *) xmalloc (len + 1);
12610 bcopy (resource_name, xim_inst->resource_name, len + 1);
12611 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12612 resource_name, EMACS_CLASS,
12613 xim_instantiate_callback,
12614 /* Fixme: This is XPointer in
12615 XFree86 but (XPointer *) on
12616 Tru64, at least. */
12617 (XPointer) xim_inst);
12618 #else /* not HAVE_X11R6_XIM */
12619 dpyinfo->xim = NULL;
12620 xim_open_dpy (dpyinfo, resource_name);
12621 #endif /* not HAVE_X11R6_XIM */
12622 }
12623 else
12624 dpyinfo->xim = NULL;
12625 }
12626
12627
12628 /* Close the connection to the XIM server on display DPYINFO. */
12629
12630 static void
12631 xim_close_dpy (dpyinfo)
12632 struct x_display_info *dpyinfo;
12633 {
12634 if (use_xim)
12635 {
12636 #ifdef HAVE_X11R6_XIM
12637 if (dpyinfo->display)
12638 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12639 NULL, EMACS_CLASS,
12640 xim_instantiate_callback, NULL);
12641 #endif /* not HAVE_X11R6_XIM */
12642 if (dpyinfo->display)
12643 XCloseIM (dpyinfo->xim);
12644 dpyinfo->xim = NULL;
12645 XFree (dpyinfo->xim_styles);
12646 }
12647 }
12648
12649 #endif /* not HAVE_X11R6_XIM */
12650
12651
12652 \f
12653 /* Calculate the absolute position in frame F
12654 from its current recorded position values and gravity. */
12655
12656 void
12657 x_calc_absolute_position (f)
12658 struct frame *f;
12659 {
12660 Window child;
12661 int win_x = 0, win_y = 0;
12662 int flags = f->output_data.x->size_hint_flags;
12663 int this_window;
12664
12665 /* We have nothing to do if the current position
12666 is already for the top-left corner. */
12667 if (! ((flags & XNegative) || (flags & YNegative)))
12668 return;
12669
12670 #ifdef USE_X_TOOLKIT
12671 this_window = XtWindow (f->output_data.x->widget);
12672 #else
12673 this_window = FRAME_X_WINDOW (f);
12674 #endif
12675
12676 /* Find the position of the outside upper-left corner of
12677 the inner window, with respect to the outer window.
12678 But do this only if we will need the results. */
12679 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12680 {
12681 int count;
12682
12683 BLOCK_INPUT;
12684 count = x_catch_errors (FRAME_X_DISPLAY (f));
12685 while (1)
12686 {
12687 x_clear_errors (FRAME_X_DISPLAY (f));
12688 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12689
12690 /* From-window, to-window. */
12691 this_window,
12692 f->output_data.x->parent_desc,
12693
12694 /* From-position, to-position. */
12695 0, 0, &win_x, &win_y,
12696
12697 /* Child of win. */
12698 &child);
12699 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12700 {
12701 Window newroot, newparent = 0xdeadbeef;
12702 Window *newchildren;
12703 unsigned int nchildren;
12704
12705 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12706 &newparent, &newchildren, &nchildren))
12707 break;
12708
12709 XFree ((char *) newchildren);
12710
12711 f->output_data.x->parent_desc = newparent;
12712 }
12713 else
12714 break;
12715 }
12716
12717 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12718 UNBLOCK_INPUT;
12719 }
12720
12721 /* Treat negative positions as relative to the leftmost bottommost
12722 position that fits on the screen. */
12723 if (flags & XNegative)
12724 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12725 - 2 * f->output_data.x->border_width - win_x
12726 - PIXEL_WIDTH (f)
12727 + f->output_data.x->left_pos);
12728
12729 {
12730 int height = PIXEL_HEIGHT (f);
12731
12732 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12733 /* Something is fishy here. When using Motif, starting Emacs with
12734 `-g -0-0', the frame appears too low by a few pixels.
12735
12736 This seems to be so because initially, while Emacs is starting,
12737 the column widget's height and the frame's pixel height are
12738 different. The column widget's height is the right one. In
12739 later invocations, when Emacs is up, the frame's pixel height
12740 is right, though.
12741
12742 It's not obvious where the initial small difference comes from.
12743 2000-12-01, gerd. */
12744
12745 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12746 #endif
12747
12748 if (flags & YNegative)
12749 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12750 - 2 * f->output_data.x->border_width
12751 - win_y
12752 - height
12753 + f->output_data.x->top_pos);
12754 }
12755
12756 /* The left_pos and top_pos
12757 are now relative to the top and left screen edges,
12758 so the flags should correspond. */
12759 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12760 }
12761
12762 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12763 to really change the position, and 0 when calling from
12764 x_make_frame_visible (in that case, XOFF and YOFF are the current
12765 position values). It is -1 when calling from x_set_frame_parameters,
12766 which means, do adjust for borders but don't change the gravity. */
12767
12768 void
12769 x_set_offset (f, xoff, yoff, change_gravity)
12770 struct frame *f;
12771 register int xoff, yoff;
12772 int change_gravity;
12773 {
12774 int modified_top, modified_left;
12775
12776 if (change_gravity > 0)
12777 {
12778 f->output_data.x->top_pos = yoff;
12779 f->output_data.x->left_pos = xoff;
12780 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12781 if (xoff < 0)
12782 f->output_data.x->size_hint_flags |= XNegative;
12783 if (yoff < 0)
12784 f->output_data.x->size_hint_flags |= YNegative;
12785 f->output_data.x->win_gravity = NorthWestGravity;
12786 }
12787 x_calc_absolute_position (f);
12788
12789 BLOCK_INPUT;
12790 x_wm_set_size_hint (f, (long) 0, 0);
12791
12792 modified_left = f->output_data.x->left_pos;
12793 modified_top = f->output_data.x->top_pos;
12794 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12795 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12796 /* It is a mystery why we need to add the border_width here
12797 when the frame is already visible, but experiment says we do. */
12798 if (change_gravity != 0)
12799 {
12800 modified_left += f->output_data.x->border_width;
12801 modified_top += f->output_data.x->border_width;
12802 }
12803 #endif
12804
12805 #ifdef USE_X_TOOLKIT
12806 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12807 modified_left, modified_top);
12808 #else /* not USE_X_TOOLKIT */
12809 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12810 modified_left, modified_top);
12811 #endif /* not USE_X_TOOLKIT */
12812 UNBLOCK_INPUT;
12813 }
12814
12815 /* Check if we need to resize the frame due to a fullscreen request.
12816 If so needed, resize the frame. */
12817 static void
12818 x_check_fullscreen (f)
12819 struct frame *f;
12820 {
12821 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12822 {
12823 int width, height, ign;
12824
12825 x_real_positions (f, &f->output_data.x->left_pos,
12826 &f->output_data.x->top_pos);
12827
12828 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12829
12830 /* We do not need to move the window, it shall be taken care of
12831 when setting WM manager hints.
12832 If the frame is visible already, the position is checked by
12833 x_check_fullscreen_move. */
12834 if (f->width != width || f->height != height)
12835 {
12836 change_frame_size (f, height, width, 0, 1, 0);
12837 SET_FRAME_GARBAGED (f);
12838 cancel_mouse_face (f);
12839
12840 /* Wait for the change of frame size to occur */
12841 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12842
12843 }
12844 }
12845 }
12846
12847 /* If frame parameters are set after the frame is mapped, we need to move
12848 the window. This is done in xfns.c.
12849 Some window managers moves the window to the right position, some
12850 moves the outer window manager window to the specified position.
12851 Here we check that we are in the right spot. If not, make a second
12852 move, assuming we are dealing with the second kind of window manager. */
12853 static void
12854 x_check_fullscreen_move (f)
12855 struct frame *f;
12856 {
12857 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12858 {
12859 int expect_top = f->output_data.x->top_pos;
12860 int expect_left = f->output_data.x->left_pos;
12861
12862 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12863 expect_top = 0;
12864 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12865 expect_left = 0;
12866
12867 if (expect_top != f->output_data.x->top_pos
12868 || expect_left != f->output_data.x->left_pos)
12869 x_set_offset (f, expect_left, expect_top, 1);
12870
12871 /* Just do this once */
12872 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12873 }
12874 }
12875
12876
12877 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12878 wanted positions of the WM window (not emacs window).
12879 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12880 window (FRAME_X_WINDOW).
12881 */
12882 void
12883 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12884 struct frame *f;
12885 int *width;
12886 int *height;
12887 int *top_pos;
12888 int *left_pos;
12889 {
12890 int newwidth = f->width, newheight = f->height;
12891
12892 *top_pos = f->output_data.x->top_pos;
12893 *left_pos = f->output_data.x->left_pos;
12894
12895 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12896 {
12897 int ph;
12898
12899 ph = FRAME_X_DISPLAY_INFO (f)->height;
12900 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12901 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12902 - f->output_data.x->y_pixels_diff;
12903 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12904 *top_pos = 0;
12905 }
12906
12907 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12908 {
12909 int pw;
12910
12911 pw = FRAME_X_DISPLAY_INFO (f)->width;
12912 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12913 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12914 - f->output_data.x->x_pixels_diff;
12915 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12916 *left_pos = 0;
12917 }
12918
12919 *width = newwidth;
12920 *height = newheight;
12921 }
12922
12923
12924 /* Change the size of frame F's X window to COLS/ROWS in the case F
12925 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12926 top-left-corner window gravity for this size change and subsequent
12927 size changes. Otherwise we leave the window gravity unchanged. */
12928
12929 static void
12930 x_set_window_size_1 (f, change_gravity, cols, rows)
12931 struct frame *f;
12932 int change_gravity;
12933 int cols, rows;
12934 {
12935 int pixelwidth, pixelheight;
12936
12937 check_frame_size (f, &rows, &cols);
12938 f->output_data.x->vertical_scroll_bar_extra
12939 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12940 ? 0
12941 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12942 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12943 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12944
12945 x_compute_fringe_widths (f, 0);
12946
12947 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12948 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12949
12950 f->output_data.x->win_gravity = NorthWestGravity;
12951 x_wm_set_size_hint (f, (long) 0, 0);
12952
12953 XSync (FRAME_X_DISPLAY (f), False);
12954 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12955 pixelwidth, pixelheight);
12956
12957 /* Now, strictly speaking, we can't be sure that this is accurate,
12958 but the window manager will get around to dealing with the size
12959 change request eventually, and we'll hear how it went when the
12960 ConfigureNotify event gets here.
12961
12962 We could just not bother storing any of this information here,
12963 and let the ConfigureNotify event set everything up, but that
12964 might be kind of confusing to the Lisp code, since size changes
12965 wouldn't be reported in the frame parameters until some random
12966 point in the future when the ConfigureNotify event arrives.
12967
12968 We pass 1 for DELAY since we can't run Lisp code inside of
12969 a BLOCK_INPUT. */
12970 change_frame_size (f, rows, cols, 0, 1, 0);
12971 PIXEL_WIDTH (f) = pixelwidth;
12972 PIXEL_HEIGHT (f) = pixelheight;
12973
12974 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12975 receive in the ConfigureNotify event; if we get what we asked
12976 for, then the event won't cause the screen to become garbaged, so
12977 we have to make sure to do it here. */
12978 SET_FRAME_GARBAGED (f);
12979
12980 XFlush (FRAME_X_DISPLAY (f));
12981 }
12982
12983
12984 /* Call this to change the size of frame F's x-window.
12985 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12986 for this size change and subsequent size changes.
12987 Otherwise we leave the window gravity unchanged. */
12988
12989 void
12990 x_set_window_size (f, change_gravity, cols, rows)
12991 struct frame *f;
12992 int change_gravity;
12993 int cols, rows;
12994 {
12995 BLOCK_INPUT;
12996
12997 #ifdef USE_X_TOOLKIT
12998
12999 if (f->output_data.x->widget != NULL)
13000 {
13001 /* The x and y position of the widget is clobbered by the
13002 call to XtSetValues within EmacsFrameSetCharSize.
13003 This is a real kludge, but I don't understand Xt so I can't
13004 figure out a correct fix. Can anyone else tell me? -- rms. */
13005 int xpos = f->output_data.x->widget->core.x;
13006 int ypos = f->output_data.x->widget->core.y;
13007 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13008 f->output_data.x->widget->core.x = xpos;
13009 f->output_data.x->widget->core.y = ypos;
13010 }
13011 else
13012 x_set_window_size_1 (f, change_gravity, cols, rows);
13013
13014 #else /* not USE_X_TOOLKIT */
13015
13016 x_set_window_size_1 (f, change_gravity, cols, rows);
13017
13018 #endif /* not USE_X_TOOLKIT */
13019
13020 /* If cursor was outside the new size, mark it as off. */
13021 mark_window_cursors_off (XWINDOW (f->root_window));
13022
13023 /* Clear out any recollection of where the mouse highlighting was,
13024 since it might be in a place that's outside the new frame size.
13025 Actually checking whether it is outside is a pain in the neck,
13026 so don't try--just let the highlighting be done afresh with new size. */
13027 cancel_mouse_face (f);
13028
13029 UNBLOCK_INPUT;
13030 }
13031 \f
13032 /* Mouse warping. */
13033
13034 void
13035 x_set_mouse_position (f, x, y)
13036 struct frame *f;
13037 int x, y;
13038 {
13039 int pix_x, pix_y;
13040
13041 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13042 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13043
13044 if (pix_x < 0) pix_x = 0;
13045 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13046
13047 if (pix_y < 0) pix_y = 0;
13048 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13049
13050 BLOCK_INPUT;
13051
13052 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13053 0, 0, 0, 0, pix_x, pix_y);
13054 UNBLOCK_INPUT;
13055 }
13056
13057 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13058
13059 void
13060 x_set_mouse_pixel_position (f, pix_x, pix_y)
13061 struct frame *f;
13062 int pix_x, pix_y;
13063 {
13064 BLOCK_INPUT;
13065
13066 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13067 0, 0, 0, 0, pix_x, pix_y);
13068 UNBLOCK_INPUT;
13069 }
13070 \f
13071 /* focus shifting, raising and lowering. */
13072
13073 void
13074 x_focus_on_frame (f)
13075 struct frame *f;
13076 {
13077 #if 0 /* This proves to be unpleasant. */
13078 x_raise_frame (f);
13079 #endif
13080 #if 0
13081 /* I don't think that the ICCCM allows programs to do things like this
13082 without the interaction of the window manager. Whatever you end up
13083 doing with this code, do it to x_unfocus_frame too. */
13084 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13085 RevertToPointerRoot, CurrentTime);
13086 #endif /* ! 0 */
13087 }
13088
13089 void
13090 x_unfocus_frame (f)
13091 struct frame *f;
13092 {
13093 #if 0
13094 /* Look at the remarks in x_focus_on_frame. */
13095 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13096 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13097 RevertToPointerRoot, CurrentTime);
13098 #endif /* ! 0 */
13099 }
13100
13101 /* Raise frame F. */
13102
13103 void
13104 x_raise_frame (f)
13105 struct frame *f;
13106 {
13107 if (f->async_visible)
13108 {
13109 BLOCK_INPUT;
13110 #ifdef USE_X_TOOLKIT
13111 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13112 #else /* not USE_X_TOOLKIT */
13113 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13114 #endif /* not USE_X_TOOLKIT */
13115 XFlush (FRAME_X_DISPLAY (f));
13116 UNBLOCK_INPUT;
13117 }
13118 }
13119
13120 /* Lower frame F. */
13121
13122 void
13123 x_lower_frame (f)
13124 struct frame *f;
13125 {
13126 if (f->async_visible)
13127 {
13128 BLOCK_INPUT;
13129 #ifdef USE_X_TOOLKIT
13130 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13131 #else /* not USE_X_TOOLKIT */
13132 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13133 #endif /* not USE_X_TOOLKIT */
13134 XFlush (FRAME_X_DISPLAY (f));
13135 UNBLOCK_INPUT;
13136 }
13137 }
13138
13139 static void
13140 XTframe_raise_lower (f, raise_flag)
13141 FRAME_PTR f;
13142 int raise_flag;
13143 {
13144 if (raise_flag)
13145 x_raise_frame (f);
13146 else
13147 x_lower_frame (f);
13148 }
13149 \f
13150 /* Change of visibility. */
13151
13152 /* This tries to wait until the frame is really visible.
13153 However, if the window manager asks the user where to position
13154 the frame, this will return before the user finishes doing that.
13155 The frame will not actually be visible at that time,
13156 but it will become visible later when the window manager
13157 finishes with it. */
13158
13159 void
13160 x_make_frame_visible (f)
13161 struct frame *f;
13162 {
13163 Lisp_Object type;
13164 int original_top, original_left;
13165 int retry_count = 2;
13166
13167 retry:
13168
13169 BLOCK_INPUT;
13170
13171 type = x_icon_type (f);
13172 if (!NILP (type))
13173 x_bitmap_icon (f, type);
13174
13175 if (! FRAME_VISIBLE_P (f))
13176 {
13177 /* We test FRAME_GARBAGED_P here to make sure we don't
13178 call x_set_offset a second time
13179 if we get to x_make_frame_visible a second time
13180 before the window gets really visible. */
13181 if (! FRAME_ICONIFIED_P (f)
13182 && ! f->output_data.x->asked_for_visible)
13183 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13184
13185 f->output_data.x->asked_for_visible = 1;
13186
13187 if (! EQ (Vx_no_window_manager, Qt))
13188 x_wm_set_window_state (f, NormalState);
13189 #ifdef USE_X_TOOLKIT
13190 /* This was XtPopup, but that did nothing for an iconified frame. */
13191 XtMapWidget (f->output_data.x->widget);
13192 #else /* not USE_X_TOOLKIT */
13193 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13194 #endif /* not USE_X_TOOLKIT */
13195 #if 0 /* This seems to bring back scroll bars in the wrong places
13196 if the window configuration has changed. They seem
13197 to come back ok without this. */
13198 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13199 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13200 #endif
13201 }
13202
13203 XFlush (FRAME_X_DISPLAY (f));
13204
13205 /* Synchronize to ensure Emacs knows the frame is visible
13206 before we do anything else. We do this loop with input not blocked
13207 so that incoming events are handled. */
13208 {
13209 Lisp_Object frame;
13210 int count;
13211 /* This must be before UNBLOCK_INPUT
13212 since events that arrive in response to the actions above
13213 will set it when they are handled. */
13214 int previously_visible = f->output_data.x->has_been_visible;
13215
13216 original_left = f->output_data.x->left_pos;
13217 original_top = f->output_data.x->top_pos;
13218
13219 /* This must come after we set COUNT. */
13220 UNBLOCK_INPUT;
13221
13222 /* We unblock here so that arriving X events are processed. */
13223
13224 /* Now move the window back to where it was "supposed to be".
13225 But don't do it if the gravity is negative.
13226 When the gravity is negative, this uses a position
13227 that is 3 pixels too low. Perhaps that's really the border width.
13228
13229 Don't do this if the window has never been visible before,
13230 because the window manager may choose the position
13231 and we don't want to override it. */
13232
13233 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13234 && f->output_data.x->win_gravity == NorthWestGravity
13235 && previously_visible)
13236 {
13237 Drawable rootw;
13238 int x, y;
13239 unsigned int width, height, border, depth;
13240
13241 BLOCK_INPUT;
13242
13243 /* On some window managers (such as FVWM) moving an existing
13244 window, even to the same place, causes the window manager
13245 to introduce an offset. This can cause the window to move
13246 to an unexpected location. Check the geometry (a little
13247 slow here) and then verify that the window is in the right
13248 place. If the window is not in the right place, move it
13249 there, and take the potential window manager hit. */
13250 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13251 &rootw, &x, &y, &width, &height, &border, &depth);
13252
13253 if (original_left != x || original_top != y)
13254 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13255 original_left, original_top);
13256
13257 UNBLOCK_INPUT;
13258 }
13259
13260 XSETFRAME (frame, f);
13261
13262 /* Wait until the frame is visible. Process X events until a
13263 MapNotify event has been seen, or until we think we won't get a
13264 MapNotify at all.. */
13265 for (count = input_signal_count + 10;
13266 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13267 {
13268 /* Force processing of queued events. */
13269 x_sync (f);
13270
13271 /* Machines that do polling rather than SIGIO have been
13272 observed to go into a busy-wait here. So we'll fake an
13273 alarm signal to let the handler know that there's something
13274 to be read. We used to raise a real alarm, but it seems
13275 that the handler isn't always enabled here. This is
13276 probably a bug. */
13277 if (input_polling_used ())
13278 {
13279 /* It could be confusing if a real alarm arrives while
13280 processing the fake one. Turn it off and let the
13281 handler reset it. */
13282 extern void poll_for_input_1 P_ ((void));
13283 int old_poll_suppress_count = poll_suppress_count;
13284 poll_suppress_count = 1;
13285 poll_for_input_1 ();
13286 poll_suppress_count = old_poll_suppress_count;
13287 }
13288
13289 /* See if a MapNotify event has been processed. */
13290 FRAME_SAMPLE_VISIBILITY (f);
13291 }
13292
13293 /* 2000-09-28: In
13294
13295 (let ((f (selected-frame)))
13296 (iconify-frame f)
13297 (raise-frame f))
13298
13299 the frame is not raised with various window managers on
13300 FreeBSD, Linux and Solaris. It turns out that, for some
13301 unknown reason, the call to XtMapWidget is completely ignored.
13302 Mapping the widget a second time works. */
13303
13304 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13305 goto retry;
13306 }
13307 }
13308
13309 /* Change from mapped state to withdrawn state. */
13310
13311 /* Make the frame visible (mapped and not iconified). */
13312
13313 void
13314 x_make_frame_invisible (f)
13315 struct frame *f;
13316 {
13317 Window window;
13318
13319 #ifdef USE_X_TOOLKIT
13320 /* Use the frame's outermost window, not the one we normally draw on. */
13321 window = XtWindow (f->output_data.x->widget);
13322 #else /* not USE_X_TOOLKIT */
13323 window = FRAME_X_WINDOW (f);
13324 #endif /* not USE_X_TOOLKIT */
13325
13326 /* Don't keep the highlight on an invisible frame. */
13327 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13328 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13329
13330 #if 0/* This might add unreliability; I don't trust it -- rms. */
13331 if (! f->async_visible && ! f->async_iconified)
13332 return;
13333 #endif
13334
13335 BLOCK_INPUT;
13336
13337 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13338 that the current position of the window is user-specified, rather than
13339 program-specified, so that when the window is mapped again, it will be
13340 placed at the same location, without forcing the user to position it
13341 by hand again (they have already done that once for this window.) */
13342 x_wm_set_size_hint (f, (long) 0, 1);
13343
13344 #ifdef HAVE_X11R4
13345
13346 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13347 DefaultScreen (FRAME_X_DISPLAY (f))))
13348 {
13349 UNBLOCK_INPUT_RESIGNAL;
13350 error ("Can't notify window manager of window withdrawal");
13351 }
13352 #else /* ! defined (HAVE_X11R4) */
13353
13354 /* Tell the window manager what we're going to do. */
13355 if (! EQ (Vx_no_window_manager, Qt))
13356 {
13357 XEvent unmap;
13358
13359 unmap.xunmap.type = UnmapNotify;
13360 unmap.xunmap.window = window;
13361 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13362 unmap.xunmap.from_configure = False;
13363 if (! XSendEvent (FRAME_X_DISPLAY (f),
13364 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13365 False,
13366 SubstructureRedirectMaskSubstructureNotifyMask,
13367 &unmap))
13368 {
13369 UNBLOCK_INPUT_RESIGNAL;
13370 error ("Can't notify window manager of withdrawal");
13371 }
13372 }
13373
13374 /* Unmap the window ourselves. Cheeky! */
13375 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13376 #endif /* ! defined (HAVE_X11R4) */
13377
13378 /* We can't distinguish this from iconification
13379 just by the event that we get from the server.
13380 So we can't win using the usual strategy of letting
13381 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13382 and synchronize with the server to make sure we agree. */
13383 f->visible = 0;
13384 FRAME_ICONIFIED_P (f) = 0;
13385 f->async_visible = 0;
13386 f->async_iconified = 0;
13387
13388 x_sync (f);
13389
13390 UNBLOCK_INPUT;
13391 }
13392
13393 /* Change window state from mapped to iconified. */
13394
13395 void
13396 x_iconify_frame (f)
13397 struct frame *f;
13398 {
13399 int result;
13400 Lisp_Object type;
13401
13402 /* Don't keep the highlight on an invisible frame. */
13403 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13404 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13405
13406 if (f->async_iconified)
13407 return;
13408
13409 BLOCK_INPUT;
13410
13411 FRAME_SAMPLE_VISIBILITY (f);
13412
13413 type = x_icon_type (f);
13414 if (!NILP (type))
13415 x_bitmap_icon (f, type);
13416
13417 #ifdef USE_X_TOOLKIT
13418
13419 if (! FRAME_VISIBLE_P (f))
13420 {
13421 if (! EQ (Vx_no_window_manager, Qt))
13422 x_wm_set_window_state (f, IconicState);
13423 /* This was XtPopup, but that did nothing for an iconified frame. */
13424 XtMapWidget (f->output_data.x->widget);
13425 /* The server won't give us any event to indicate
13426 that an invisible frame was changed to an icon,
13427 so we have to record it here. */
13428 f->iconified = 1;
13429 f->visible = 1;
13430 f->async_iconified = 1;
13431 f->async_visible = 0;
13432 UNBLOCK_INPUT;
13433 return;
13434 }
13435
13436 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13437 XtWindow (f->output_data.x->widget),
13438 DefaultScreen (FRAME_X_DISPLAY (f)));
13439 UNBLOCK_INPUT;
13440
13441 if (!result)
13442 error ("Can't notify window manager of iconification");
13443
13444 f->async_iconified = 1;
13445 f->async_visible = 0;
13446
13447
13448 BLOCK_INPUT;
13449 XFlush (FRAME_X_DISPLAY (f));
13450 UNBLOCK_INPUT;
13451 #else /* not USE_X_TOOLKIT */
13452
13453 /* Make sure the X server knows where the window should be positioned,
13454 in case the user deiconifies with the window manager. */
13455 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13456 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13457
13458 /* Since we don't know which revision of X we're running, we'll use both
13459 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13460
13461 /* X11R4: send a ClientMessage to the window manager using the
13462 WM_CHANGE_STATE type. */
13463 {
13464 XEvent message;
13465
13466 message.xclient.window = FRAME_X_WINDOW (f);
13467 message.xclient.type = ClientMessage;
13468 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13469 message.xclient.format = 32;
13470 message.xclient.data.l[0] = IconicState;
13471
13472 if (! XSendEvent (FRAME_X_DISPLAY (f),
13473 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13474 False,
13475 SubstructureRedirectMask | SubstructureNotifyMask,
13476 &message))
13477 {
13478 UNBLOCK_INPUT_RESIGNAL;
13479 error ("Can't notify window manager of iconification");
13480 }
13481 }
13482
13483 /* X11R3: set the initial_state field of the window manager hints to
13484 IconicState. */
13485 x_wm_set_window_state (f, IconicState);
13486
13487 if (!FRAME_VISIBLE_P (f))
13488 {
13489 /* If the frame was withdrawn, before, we must map it. */
13490 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13491 }
13492
13493 f->async_iconified = 1;
13494 f->async_visible = 0;
13495
13496 XFlush (FRAME_X_DISPLAY (f));
13497 UNBLOCK_INPUT;
13498 #endif /* not USE_X_TOOLKIT */
13499 }
13500
13501 \f
13502 /* Free X resources of frame F. */
13503
13504 void
13505 x_free_frame_resources (f)
13506 struct frame *f;
13507 {
13508 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13509 Lisp_Object bar;
13510 struct scroll_bar *b;
13511
13512 BLOCK_INPUT;
13513
13514 /* If a display connection is dead, don't try sending more
13515 commands to the X server. */
13516 if (dpyinfo->display)
13517 {
13518 if (f->output_data.x->icon_desc)
13519 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13520
13521 #ifdef USE_X_TOOLKIT
13522 /* Explicitly destroy the scroll bars of the frame. Without
13523 this, we get "BadDrawable" errors from the toolkit later on,
13524 presumably from expose events generated for the disappearing
13525 toolkit scroll bars. */
13526 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13527 {
13528 b = XSCROLL_BAR (bar);
13529 x_scroll_bar_remove (b);
13530 }
13531 #endif
13532
13533 #ifdef HAVE_X_I18N
13534 if (FRAME_XIC (f))
13535 free_frame_xic (f);
13536 #endif
13537
13538 #ifdef USE_X_TOOLKIT
13539 if (f->output_data.x->widget)
13540 {
13541 XtDestroyWidget (f->output_data.x->widget);
13542 f->output_data.x->widget = NULL;
13543 }
13544 /* Tooltips don't have widgets, only a simple X window, even if
13545 we are using a toolkit. */
13546 else if (FRAME_X_WINDOW (f))
13547 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13548
13549 free_frame_menubar (f);
13550 #else /* !USE_X_TOOLKIT */
13551 if (FRAME_X_WINDOW (f))
13552 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13553 #endif /* !USE_X_TOOLKIT */
13554
13555 unload_color (f, f->output_data.x->foreground_pixel);
13556 unload_color (f, f->output_data.x->background_pixel);
13557 unload_color (f, f->output_data.x->cursor_pixel);
13558 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13559 unload_color (f, f->output_data.x->border_pixel);
13560 unload_color (f, f->output_data.x->mouse_pixel);
13561
13562 if (f->output_data.x->scroll_bar_background_pixel != -1)
13563 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13564 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13565 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13566 #ifdef USE_TOOLKIT_SCROLL_BARS
13567 /* Scrollbar shadow colors. */
13568 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13569 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13570 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13571 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13572 #endif /* USE_TOOLKIT_SCROLL_BARS */
13573 if (f->output_data.x->white_relief.allocated_p)
13574 unload_color (f, f->output_data.x->white_relief.pixel);
13575 if (f->output_data.x->black_relief.allocated_p)
13576 unload_color (f, f->output_data.x->black_relief.pixel);
13577
13578 if (FRAME_FACE_CACHE (f))
13579 free_frame_faces (f);
13580
13581 x_free_gcs (f);
13582 XFlush (FRAME_X_DISPLAY (f));
13583 }
13584
13585 if (f->output_data.x->saved_menu_event)
13586 xfree (f->output_data.x->saved_menu_event);
13587
13588 xfree (f->output_data.x);
13589 f->output_data.x = NULL;
13590
13591 if (f == dpyinfo->x_focus_frame)
13592 dpyinfo->x_focus_frame = 0;
13593 if (f == dpyinfo->x_focus_event_frame)
13594 dpyinfo->x_focus_event_frame = 0;
13595 if (f == dpyinfo->x_highlight_frame)
13596 dpyinfo->x_highlight_frame = 0;
13597
13598 if (f == dpyinfo->mouse_face_mouse_frame)
13599 {
13600 dpyinfo->mouse_face_beg_row
13601 = dpyinfo->mouse_face_beg_col = -1;
13602 dpyinfo->mouse_face_end_row
13603 = dpyinfo->mouse_face_end_col = -1;
13604 dpyinfo->mouse_face_window = Qnil;
13605 dpyinfo->mouse_face_deferred_gc = 0;
13606 dpyinfo->mouse_face_mouse_frame = 0;
13607 }
13608
13609 UNBLOCK_INPUT;
13610 }
13611
13612
13613 /* Destroy the X window of frame F. */
13614
13615 void
13616 x_destroy_window (f)
13617 struct frame *f;
13618 {
13619 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13620
13621 /* If a display connection is dead, don't try sending more
13622 commands to the X server. */
13623 if (dpyinfo->display != 0)
13624 x_free_frame_resources (f);
13625
13626 dpyinfo->reference_count--;
13627 }
13628
13629 \f
13630 /* Setting window manager hints. */
13631
13632 /* Set the normal size hints for the window manager, for frame F.
13633 FLAGS is the flags word to use--or 0 meaning preserve the flags
13634 that the window now has.
13635 If USER_POSITION is nonzero, we set the USPosition
13636 flag (this is useful when FLAGS is 0). */
13637
13638 void
13639 x_wm_set_size_hint (f, flags, user_position)
13640 struct frame *f;
13641 long flags;
13642 int user_position;
13643 {
13644 XSizeHints size_hints;
13645
13646 #ifdef USE_X_TOOLKIT
13647 Arg al[2];
13648 int ac = 0;
13649 Dimension widget_width, widget_height;
13650 Window window = XtWindow (f->output_data.x->widget);
13651 #else /* not USE_X_TOOLKIT */
13652 Window window = FRAME_X_WINDOW (f);
13653 #endif /* not USE_X_TOOLKIT */
13654
13655 /* Setting PMaxSize caused various problems. */
13656 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13657
13658 size_hints.x = f->output_data.x->left_pos;
13659 size_hints.y = f->output_data.x->top_pos;
13660
13661 #ifdef USE_X_TOOLKIT
13662 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13663 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13664 XtGetValues (f->output_data.x->widget, al, ac);
13665 size_hints.height = widget_height;
13666 size_hints.width = widget_width;
13667 #else /* not USE_X_TOOLKIT */
13668 size_hints.height = PIXEL_HEIGHT (f);
13669 size_hints.width = PIXEL_WIDTH (f);
13670 #endif /* not USE_X_TOOLKIT */
13671
13672 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13673 size_hints.height_inc = f->output_data.x->line_height;
13674 size_hints.max_width
13675 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13676 size_hints.max_height
13677 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13678
13679 /* Calculate the base and minimum sizes.
13680
13681 (When we use the X toolkit, we don't do it here.
13682 Instead we copy the values that the widgets are using, below.) */
13683 #ifndef USE_X_TOOLKIT
13684 {
13685 int base_width, base_height;
13686 int min_rows = 0, min_cols = 0;
13687
13688 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13689 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13690
13691 check_frame_size (f, &min_rows, &min_cols);
13692
13693 /* The window manager uses the base width hints to calculate the
13694 current number of rows and columns in the frame while
13695 resizing; min_width and min_height aren't useful for this
13696 purpose, since they might not give the dimensions for a
13697 zero-row, zero-column frame.
13698
13699 We use the base_width and base_height members if we have
13700 them; otherwise, we set the min_width and min_height members
13701 to the size for a zero x zero frame. */
13702
13703 #ifdef HAVE_X11R4
13704 size_hints.flags |= PBaseSize;
13705 size_hints.base_width = base_width;
13706 size_hints.base_height = base_height;
13707 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13708 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13709 #else
13710 size_hints.min_width = base_width;
13711 size_hints.min_height = base_height;
13712 #endif
13713 }
13714
13715 /* If we don't need the old flags, we don't need the old hint at all. */
13716 if (flags)
13717 {
13718 size_hints.flags |= flags;
13719 goto no_read;
13720 }
13721 #endif /* not USE_X_TOOLKIT */
13722
13723 {
13724 XSizeHints hints; /* Sometimes I hate X Windows... */
13725 long supplied_return;
13726 int value;
13727
13728 #ifdef HAVE_X11R4
13729 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13730 &supplied_return);
13731 #else
13732 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13733 #endif
13734
13735 #ifdef USE_X_TOOLKIT
13736 size_hints.base_height = hints.base_height;
13737 size_hints.base_width = hints.base_width;
13738 size_hints.min_height = hints.min_height;
13739 size_hints.min_width = hints.min_width;
13740 #endif
13741
13742 if (flags)
13743 size_hints.flags |= flags;
13744 else
13745 {
13746 if (value == 0)
13747 hints.flags = 0;
13748 if (hints.flags & PSize)
13749 size_hints.flags |= PSize;
13750 if (hints.flags & PPosition)
13751 size_hints.flags |= PPosition;
13752 if (hints.flags & USPosition)
13753 size_hints.flags |= USPosition;
13754 if (hints.flags & USSize)
13755 size_hints.flags |= USSize;
13756 }
13757 }
13758
13759 #ifndef USE_X_TOOLKIT
13760 no_read:
13761 #endif
13762
13763 #ifdef PWinGravity
13764 size_hints.win_gravity = f->output_data.x->win_gravity;
13765 size_hints.flags |= PWinGravity;
13766
13767 if (user_position)
13768 {
13769 size_hints.flags &= ~ PPosition;
13770 size_hints.flags |= USPosition;
13771 }
13772 #endif /* PWinGravity */
13773
13774 #ifdef HAVE_X11R4
13775 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13776 #else
13777 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13778 #endif
13779 }
13780
13781 /* Used for IconicState or NormalState */
13782
13783 void
13784 x_wm_set_window_state (f, state)
13785 struct frame *f;
13786 int state;
13787 {
13788 #ifdef USE_X_TOOLKIT
13789 Arg al[1];
13790
13791 XtSetArg (al[0], XtNinitialState, state);
13792 XtSetValues (f->output_data.x->widget, al, 1);
13793 #else /* not USE_X_TOOLKIT */
13794 Window window = FRAME_X_WINDOW (f);
13795
13796 f->output_data.x->wm_hints.flags |= StateHint;
13797 f->output_data.x->wm_hints.initial_state = state;
13798
13799 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13800 #endif /* not USE_X_TOOLKIT */
13801 }
13802
13803 void
13804 x_wm_set_icon_pixmap (f, pixmap_id)
13805 struct frame *f;
13806 int pixmap_id;
13807 {
13808 Pixmap icon_pixmap;
13809
13810 #ifndef USE_X_TOOLKIT
13811 Window window = FRAME_X_WINDOW (f);
13812 #endif
13813
13814 if (pixmap_id > 0)
13815 {
13816 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13817 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13818 }
13819 else
13820 {
13821 /* It seems there is no way to turn off use of an icon pixmap.
13822 The following line does it, only if no icon has yet been created,
13823 for some window managers. But with mwm it crashes.
13824 Some people say it should clear the IconPixmapHint bit in this case,
13825 but that doesn't work, and the X consortium said it isn't the
13826 right thing at all. Since there is no way to win,
13827 best to explicitly give up. */
13828 #if 0
13829 f->output_data.x->wm_hints.icon_pixmap = None;
13830 #else
13831 return;
13832 #endif
13833 }
13834
13835 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13836
13837 {
13838 Arg al[1];
13839 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13840 XtSetValues (f->output_data.x->widget, al, 1);
13841 }
13842
13843 #else /* not USE_X_TOOLKIT */
13844
13845 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13846 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13847
13848 #endif /* not USE_X_TOOLKIT */
13849 }
13850
13851 void
13852 x_wm_set_icon_position (f, icon_x, icon_y)
13853 struct frame *f;
13854 int icon_x, icon_y;
13855 {
13856 #ifdef USE_X_TOOLKIT
13857 Window window = XtWindow (f->output_data.x->widget);
13858 #else
13859 Window window = FRAME_X_WINDOW (f);
13860 #endif
13861
13862 f->output_data.x->wm_hints.flags |= IconPositionHint;
13863 f->output_data.x->wm_hints.icon_x = icon_x;
13864 f->output_data.x->wm_hints.icon_y = icon_y;
13865
13866 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13867 }
13868
13869 \f
13870 /***********************************************************************
13871 Fonts
13872 ***********************************************************************/
13873
13874 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13875
13876 struct font_info *
13877 x_get_font_info (f, font_idx)
13878 FRAME_PTR f;
13879 int font_idx;
13880 {
13881 return (FRAME_X_FONT_TABLE (f) + font_idx);
13882 }
13883
13884
13885 /* Return a list of names of available fonts matching PATTERN on frame F.
13886
13887 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13888 to be listed.
13889
13890 SIZE < 0 means include scalable fonts.
13891
13892 Frame F null means we have not yet created any frame on X, and
13893 consult the first display in x_display_list. MAXNAMES sets a limit
13894 on how many fonts to match. */
13895
13896 Lisp_Object
13897 x_list_fonts (f, pattern, size, maxnames)
13898 struct frame *f;
13899 Lisp_Object pattern;
13900 int size;
13901 int maxnames;
13902 {
13903 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13904 Lisp_Object tem, second_best;
13905 struct x_display_info *dpyinfo
13906 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13907 Display *dpy = dpyinfo->display;
13908 int try_XLoadQueryFont = 0;
13909 int count;
13910 int allow_scalable_fonts_p = 0;
13911
13912 if (size < 0)
13913 {
13914 allow_scalable_fonts_p = 1;
13915 size = 0;
13916 }
13917
13918 patterns = Fassoc (pattern, Valternate_fontname_alist);
13919 if (NILP (patterns))
13920 patterns = Fcons (pattern, Qnil);
13921
13922 if (maxnames == 1 && !size)
13923 /* We can return any single font matching PATTERN. */
13924 try_XLoadQueryFont = 1;
13925
13926 for (; CONSP (patterns); patterns = XCDR (patterns))
13927 {
13928 int num_fonts;
13929 char **names = NULL;
13930
13931 pattern = XCAR (patterns);
13932 /* See if we cached the result for this particular query.
13933 The cache is an alist of the form:
13934 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13935 tem = XCDR (dpyinfo->name_list_element);
13936 key = Fcons (Fcons (pattern, make_number (maxnames)),
13937 allow_scalable_fonts_p ? Qt : Qnil);
13938 list = Fassoc (key, tem);
13939 if (!NILP (list))
13940 {
13941 list = Fcdr_safe (list);
13942 /* We have a cashed list. Don't have to get the list again. */
13943 goto label_cached;
13944 }
13945
13946 /* At first, put PATTERN in the cache. */
13947
13948 BLOCK_INPUT;
13949 count = x_catch_errors (dpy);
13950
13951 if (try_XLoadQueryFont)
13952 {
13953 XFontStruct *font;
13954 unsigned long value;
13955
13956 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13957 if (x_had_errors_p (dpy))
13958 {
13959 /* This error is perhaps due to insufficient memory on X
13960 server. Let's just ignore it. */
13961 font = NULL;
13962 x_clear_errors (dpy);
13963 }
13964
13965 if (font
13966 && XGetFontProperty (font, XA_FONT, &value))
13967 {
13968 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13969 int len = strlen (name);
13970 char *tmp;
13971
13972 /* If DXPC (a Differential X Protocol Compressor)
13973 Ver.3.7 is running, XGetAtomName will return null
13974 string. We must avoid such a name. */
13975 if (len == 0)
13976 try_XLoadQueryFont = 0;
13977 else
13978 {
13979 num_fonts = 1;
13980 names = (char **) alloca (sizeof (char *));
13981 /* Some systems only allow alloca assigned to a
13982 simple var. */
13983 tmp = (char *) alloca (len + 1); names[0] = tmp;
13984 bcopy (name, names[0], len + 1);
13985 XFree (name);
13986 }
13987 }
13988 else
13989 try_XLoadQueryFont = 0;
13990
13991 if (font)
13992 XFreeFont (dpy, font);
13993 }
13994
13995 if (!try_XLoadQueryFont)
13996 {
13997 /* We try at least 10 fonts because XListFonts will return
13998 auto-scaled fonts at the head. */
13999 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
14000 &num_fonts);
14001 if (x_had_errors_p (dpy))
14002 {
14003 /* This error is perhaps due to insufficient memory on X
14004 server. Let's just ignore it. */
14005 names = NULL;
14006 x_clear_errors (dpy);
14007 }
14008 }
14009
14010 x_uncatch_errors (dpy, count);
14011 UNBLOCK_INPUT;
14012
14013 if (names)
14014 {
14015 int i;
14016
14017 /* Make a list of all the fonts we got back.
14018 Store that in the font cache for the display. */
14019 for (i = 0; i < num_fonts; i++)
14020 {
14021 int width = 0;
14022 char *p = names[i];
14023 int average_width = -1, dashes = 0;
14024
14025 /* Count the number of dashes in NAMES[I]. If there are
14026 14 dashes, and the field value following 12th dash
14027 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14028 is usually too ugly to be used for editing. Let's
14029 ignore it. */
14030 while (*p)
14031 if (*p++ == '-')
14032 {
14033 dashes++;
14034 if (dashes == 7) /* PIXEL_SIZE field */
14035 width = atoi (p);
14036 else if (dashes == 12) /* AVERAGE_WIDTH field */
14037 average_width = atoi (p);
14038 }
14039
14040 if (allow_scalable_fonts_p
14041 || dashes < 14 || average_width != 0)
14042 {
14043 tem = build_string (names[i]);
14044 if (NILP (Fassoc (tem, list)))
14045 {
14046 if (STRINGP (Vx_pixel_size_width_font_regexp)
14047 && ((fast_c_string_match_ignore_case
14048 (Vx_pixel_size_width_font_regexp, names[i]))
14049 >= 0))
14050 /* We can set the value of PIXEL_SIZE to the
14051 width of this font. */
14052 list = Fcons (Fcons (tem, make_number (width)), list);
14053 else
14054 /* For the moment, width is not known. */
14055 list = Fcons (Fcons (tem, Qnil), list);
14056 }
14057 }
14058 }
14059
14060 if (!try_XLoadQueryFont)
14061 {
14062 BLOCK_INPUT;
14063 XFreeFontNames (names);
14064 UNBLOCK_INPUT;
14065 }
14066 }
14067
14068 /* Now store the result in the cache. */
14069 XSETCDR (dpyinfo->name_list_element,
14070 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14071
14072 label_cached:
14073 if (NILP (list)) continue; /* Try the remaining alternatives. */
14074
14075 newlist = second_best = Qnil;
14076 /* Make a list of the fonts that have the right width. */
14077 for (; CONSP (list); list = XCDR (list))
14078 {
14079 int found_size;
14080
14081 tem = XCAR (list);
14082
14083 if (!CONSP (tem) || NILP (XCAR (tem)))
14084 continue;
14085 if (!size)
14086 {
14087 newlist = Fcons (XCAR (tem), newlist);
14088 continue;
14089 }
14090
14091 if (!INTEGERP (XCDR (tem)))
14092 {
14093 /* Since we have not yet known the size of this font, we
14094 must try slow function call XLoadQueryFont. */
14095 XFontStruct *thisinfo;
14096
14097 BLOCK_INPUT;
14098 count = x_catch_errors (dpy);
14099 thisinfo = XLoadQueryFont (dpy,
14100 XSTRING (XCAR (tem))->data);
14101 if (x_had_errors_p (dpy))
14102 {
14103 /* This error is perhaps due to insufficient memory on X
14104 server. Let's just ignore it. */
14105 thisinfo = NULL;
14106 x_clear_errors (dpy);
14107 }
14108 x_uncatch_errors (dpy, count);
14109 UNBLOCK_INPUT;
14110
14111 if (thisinfo)
14112 {
14113 XSETCDR (tem,
14114 (thisinfo->min_bounds.width == 0
14115 ? make_number (0)
14116 : make_number (thisinfo->max_bounds.width)));
14117 BLOCK_INPUT;
14118 XFreeFont (dpy, thisinfo);
14119 UNBLOCK_INPUT;
14120 }
14121 else
14122 /* For unknown reason, the previous call of XListFont had
14123 returned a font which can't be opened. Record the size
14124 as 0 not to try to open it again. */
14125 XSETCDR (tem, make_number (0));
14126 }
14127
14128 found_size = XINT (XCDR (tem));
14129 if (found_size == size)
14130 newlist = Fcons (XCAR (tem), newlist);
14131 else if (found_size > 0)
14132 {
14133 if (NILP (second_best))
14134 second_best = tem;
14135 else if (found_size < size)
14136 {
14137 if (XINT (XCDR (second_best)) > size
14138 || XINT (XCDR (second_best)) < found_size)
14139 second_best = tem;
14140 }
14141 else
14142 {
14143 if (XINT (XCDR (second_best)) > size
14144 && XINT (XCDR (second_best)) > found_size)
14145 second_best = tem;
14146 }
14147 }
14148 }
14149 if (!NILP (newlist))
14150 break;
14151 else if (!NILP (second_best))
14152 {
14153 newlist = Fcons (XCAR (second_best), Qnil);
14154 break;
14155 }
14156 }
14157
14158 return newlist;
14159 }
14160
14161
14162 #if GLYPH_DEBUG
14163
14164 /* Check that FONT is valid on frame F. It is if it can be found in F's
14165 font table. */
14166
14167 static void
14168 x_check_font (f, font)
14169 struct frame *f;
14170 XFontStruct *font;
14171 {
14172 int i;
14173 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14174
14175 xassert (font != NULL);
14176
14177 for (i = 0; i < dpyinfo->n_fonts; i++)
14178 if (dpyinfo->font_table[i].name
14179 && font == dpyinfo->font_table[i].font)
14180 break;
14181
14182 xassert (i < dpyinfo->n_fonts);
14183 }
14184
14185 #endif /* GLYPH_DEBUG != 0 */
14186
14187 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14188 Note: There are (broken) X fonts out there with invalid XFontStruct
14189 min_bounds contents. For example, handa@etl.go.jp reports that
14190 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14191 have font->min_bounds.width == 0. */
14192
14193 static INLINE void
14194 x_font_min_bounds (font, w, h)
14195 XFontStruct *font;
14196 int *w, *h;
14197 {
14198 *h = FONT_HEIGHT (font);
14199 *w = font->min_bounds.width;
14200
14201 /* Try to handle the case where FONT->min_bounds has invalid
14202 contents. Since the only font known to have invalid min_bounds
14203 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14204 if (*w <= 0)
14205 *w = font->max_bounds.width;
14206 }
14207
14208
14209 /* Compute the smallest character width and smallest font height over
14210 all fonts available on frame F. Set the members smallest_char_width
14211 and smallest_font_height in F's x_display_info structure to
14212 the values computed. Value is non-zero if smallest_font_height or
14213 smallest_char_width become smaller than they were before. */
14214
14215 static int
14216 x_compute_min_glyph_bounds (f)
14217 struct frame *f;
14218 {
14219 int i;
14220 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14221 XFontStruct *font;
14222 int old_width = dpyinfo->smallest_char_width;
14223 int old_height = dpyinfo->smallest_font_height;
14224
14225 dpyinfo->smallest_font_height = 100000;
14226 dpyinfo->smallest_char_width = 100000;
14227
14228 for (i = 0; i < dpyinfo->n_fonts; ++i)
14229 if (dpyinfo->font_table[i].name)
14230 {
14231 struct font_info *fontp = dpyinfo->font_table + i;
14232 int w, h;
14233
14234 font = (XFontStruct *) fontp->font;
14235 xassert (font != (XFontStruct *) ~0);
14236 x_font_min_bounds (font, &w, &h);
14237
14238 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14239 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14240 }
14241
14242 xassert (dpyinfo->smallest_char_width > 0
14243 && dpyinfo->smallest_font_height > 0);
14244
14245 return (dpyinfo->n_fonts == 1
14246 || dpyinfo->smallest_char_width < old_width
14247 || dpyinfo->smallest_font_height < old_height);
14248 }
14249
14250
14251 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14252 pointer to the structure font_info while allocating it dynamically.
14253 If SIZE is 0, load any size of font.
14254 If loading is failed, return NULL. */
14255
14256 struct font_info *
14257 x_load_font (f, fontname, size)
14258 struct frame *f;
14259 register char *fontname;
14260 int size;
14261 {
14262 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14263 Lisp_Object font_names;
14264 int count;
14265
14266 /* Get a list of all the fonts that match this name. Once we
14267 have a list of matching fonts, we compare them against the fonts
14268 we already have by comparing names. */
14269 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14270
14271 if (!NILP (font_names))
14272 {
14273 Lisp_Object tail;
14274 int i;
14275
14276 for (i = 0; i < dpyinfo->n_fonts; i++)
14277 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14278 if (dpyinfo->font_table[i].name
14279 && (!strcmp (dpyinfo->font_table[i].name,
14280 XSTRING (XCAR (tail))->data)
14281 || !strcmp (dpyinfo->font_table[i].full_name,
14282 XSTRING (XCAR (tail))->data)))
14283 return (dpyinfo->font_table + i);
14284 }
14285
14286 /* Load the font and add it to the table. */
14287 {
14288 char *full_name;
14289 XFontStruct *font;
14290 struct font_info *fontp;
14291 unsigned long value;
14292 int i;
14293
14294 /* If we have found fonts by x_list_font, load one of them. If
14295 not, we still try to load a font by the name given as FONTNAME
14296 because XListFonts (called in x_list_font) of some X server has
14297 a bug of not finding a font even if the font surely exists and
14298 is loadable by XLoadQueryFont. */
14299 if (size > 0 && !NILP (font_names))
14300 fontname = (char *) XSTRING (XCAR (font_names))->data;
14301
14302 BLOCK_INPUT;
14303 count = x_catch_errors (FRAME_X_DISPLAY (f));
14304 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14305 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14306 {
14307 /* This error is perhaps due to insufficient memory on X
14308 server. Let's just ignore it. */
14309 font = NULL;
14310 x_clear_errors (FRAME_X_DISPLAY (f));
14311 }
14312 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14313 UNBLOCK_INPUT;
14314 if (!font)
14315 return NULL;
14316
14317 /* Find a free slot in the font table. */
14318 for (i = 0; i < dpyinfo->n_fonts; ++i)
14319 if (dpyinfo->font_table[i].name == NULL)
14320 break;
14321
14322 /* If no free slot found, maybe enlarge the font table. */
14323 if (i == dpyinfo->n_fonts
14324 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14325 {
14326 int sz;
14327 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14328 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14329 dpyinfo->font_table
14330 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14331 }
14332
14333 fontp = dpyinfo->font_table + i;
14334 if (i == dpyinfo->n_fonts)
14335 ++dpyinfo->n_fonts;
14336
14337 /* Now fill in the slots of *FONTP. */
14338 BLOCK_INPUT;
14339 fontp->font = font;
14340 fontp->font_idx = i;
14341 fontp->charset = -1; /* fs_load_font sets it. */
14342 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14343 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14344
14345 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14346 full_name = 0;
14347 if (XGetFontProperty (font, XA_FONT, &value))
14348 {
14349 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14350 char *p = name;
14351 int dashes = 0;
14352
14353 /* Count the number of dashes in the "full name".
14354 If it is too few, this isn't really the font's full name,
14355 so don't use it.
14356 In X11R4, the fonts did not come with their canonical names
14357 stored in them. */
14358 while (*p)
14359 {
14360 if (*p == '-')
14361 dashes++;
14362 p++;
14363 }
14364
14365 if (dashes >= 13)
14366 {
14367 full_name = (char *) xmalloc (p - name + 1);
14368 bcopy (name, full_name, p - name + 1);
14369 }
14370
14371 XFree (name);
14372 }
14373
14374 if (full_name != 0)
14375 fontp->full_name = full_name;
14376 else
14377 fontp->full_name = fontp->name;
14378
14379 fontp->size = font->max_bounds.width;
14380 fontp->height = FONT_HEIGHT (font);
14381
14382 if (NILP (font_names))
14383 {
14384 /* We come here because of a bug of XListFonts mentioned at
14385 the head of this block. Let's store this information in
14386 the cache for x_list_fonts. */
14387 Lisp_Object lispy_name = build_string (fontname);
14388 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14389 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14390 Qnil);
14391
14392 XSETCDR (dpyinfo->name_list_element,
14393 Fcons (Fcons (key,
14394 Fcons (Fcons (lispy_full_name,
14395 make_number (fontp->size)),
14396 Qnil)),
14397 XCDR (dpyinfo->name_list_element)));
14398 if (full_name)
14399 {
14400 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14401 Qnil);
14402 XSETCDR (dpyinfo->name_list_element,
14403 Fcons (Fcons (key,
14404 Fcons (Fcons (lispy_full_name,
14405 make_number (fontp->size)),
14406 Qnil)),
14407 XCDR (dpyinfo->name_list_element)));
14408 }
14409 }
14410
14411 /* The slot `encoding' specifies how to map a character
14412 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14413 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14414 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14415 2:0xA020..0xFF7F). For the moment, we don't know which charset
14416 uses this font. So, we set information in fontp->encoding[1]
14417 which is never used by any charset. If mapping can't be
14418 decided, set FONT_ENCODING_NOT_DECIDED. */
14419 fontp->encoding_type
14420 = (font->max_byte1 == 0
14421 /* 1-byte font */
14422 ? (font->min_char_or_byte2 < 0x80
14423 ? (font->max_char_or_byte2 < 0x80
14424 ? 0 /* 0x20..0x7F */
14425 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14426 : 1) /* 0xA0..0xFF */
14427 /* 2-byte font */
14428 : (font->min_byte1 < 0x80
14429 ? (font->max_byte1 < 0x80
14430 ? (font->min_char_or_byte2 < 0x80
14431 ? (font->max_char_or_byte2 < 0x80
14432 ? 0 /* 0x2020..0x7F7F */
14433 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14434 : 3) /* 0x20A0..0x7FFF */
14435 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14436 : (font->min_char_or_byte2 < 0x80
14437 ? (font->max_char_or_byte2 < 0x80
14438 ? 2 /* 0xA020..0xFF7F */
14439 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14440 : 1))); /* 0xA0A0..0xFFFF */
14441
14442 fontp->baseline_offset
14443 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14444 ? (long) value : 0);
14445 fontp->relative_compose
14446 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14447 ? (long) value : 0);
14448 fontp->default_ascent
14449 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14450 ? (long) value : 0);
14451
14452 /* Set global flag fonts_changed_p to non-zero if the font loaded
14453 has a character with a smaller width than any other character
14454 before, or if the font loaded has a smaller height than any
14455 other font loaded before. If this happens, it will make a
14456 glyph matrix reallocation necessary. */
14457 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14458 UNBLOCK_INPUT;
14459 return fontp;
14460 }
14461 }
14462
14463
14464 /* Return a pointer to struct font_info of a font named FONTNAME for
14465 frame F. If no such font is loaded, return NULL. */
14466
14467 struct font_info *
14468 x_query_font (f, fontname)
14469 struct frame *f;
14470 register char *fontname;
14471 {
14472 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14473 int i;
14474
14475 for (i = 0; i < dpyinfo->n_fonts; i++)
14476 if (dpyinfo->font_table[i].name
14477 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14478 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14479 return (dpyinfo->font_table + i);
14480 return NULL;
14481 }
14482
14483
14484 /* Find a CCL program for a font specified by FONTP, and set the member
14485 `encoder' of the structure. */
14486
14487 void
14488 x_find_ccl_program (fontp)
14489 struct font_info *fontp;
14490 {
14491 Lisp_Object list, elt;
14492
14493 elt = Qnil;
14494 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14495 {
14496 elt = XCAR (list);
14497 if (CONSP (elt)
14498 && STRINGP (XCAR (elt))
14499 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14500 >= 0)
14501 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14502 >= 0)))
14503 break;
14504 }
14505
14506 if (! NILP (list))
14507 {
14508 struct ccl_program *ccl
14509 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14510
14511 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14512 xfree (ccl);
14513 else
14514 fontp->font_encoder = ccl;
14515 }
14516 }
14517
14518
14519 /* Return a char-table whose elements are t if the font FONT_INFO
14520 contains a glyph for the corresponding character, and nil if not.
14521
14522 Fixme: For the moment, this function works only for fonts whose
14523 glyph encoding is the same as Unicode (e.g. ISO10646-1 fonts). */
14524
14525 Lisp_Object
14526 x_get_font_repertory (f, font_info)
14527 FRAME_PTR f;
14528 struct font_info *font_info;
14529 {
14530 XFontStruct *font = (XFontStruct *) font_info->font;
14531 Lisp_Object table;
14532 int min_byte1, max_byte1, min_byte2, max_byte2;
14533
14534 table = Fmake_char_table (Qnil, Qnil);
14535
14536 min_byte1 = font->min_byte1;
14537 max_byte1 = font->max_byte1;
14538 min_byte2 = font->min_char_or_byte2;
14539 max_byte2 = font->max_char_or_byte2;
14540 if (min_byte1 == 0 && max_byte1 == 0)
14541 {
14542 if (! font->per_char || font->all_chars_exist == True)
14543 char_table_set_range (table, min_byte2, max_byte2, Qt);
14544 else
14545 {
14546 XCharStruct *pcm = font->per_char;
14547 int from = -1;
14548 int i;
14549
14550 for (i = min_byte2; i <= max_byte2; i++, pcm++)
14551 {
14552 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14553 {
14554 if (from >= 0)
14555 {
14556 char_table_set_range (table, from, i - 1, Qt);
14557 from = -1;
14558 }
14559 }
14560 else if (from < 0)
14561 from = i;
14562 }
14563 if (from >= 0)
14564 char_table_set_range (table, from, i - 1, Qt);
14565 }
14566 }
14567 else
14568 {
14569 if (! font->per_char || font->all_chars_exist == True)
14570 {
14571 int i;
14572
14573 for (i = min_byte1; i <= max_byte1; i++)
14574 char_table_set_range (table,
14575 (i << 8) | min_byte2, (i << 8) | max_byte2,
14576 Qt);
14577 }
14578 else
14579 {
14580 XCharStruct *pcm = font->per_char;
14581 int i;
14582
14583 for (i = min_byte1; i <= max_byte1; i++)
14584 {
14585 int from = -1;
14586 int j;
14587
14588 for (j = min_byte2; j <= max_byte2; j++, pcm++)
14589 {
14590 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14591 {
14592 if (from >= 0)
14593 {
14594 char_table_set_range (table, (i << 8) | from,
14595 (i << 8) | (j - 1), Qt);
14596 from = -1;
14597 }
14598 }
14599 else if (from < 0)
14600 from = j;
14601 }
14602 if (from >= 0)
14603 char_table_set_range (table, (i << 8) | from,
14604 (i << 8) | (j - 1), Qt);
14605 }
14606 }
14607 }
14608
14609 return table;
14610 }
14611
14612
14613 \f
14614 /***********************************************************************
14615 Initialization
14616 ***********************************************************************/
14617
14618 #ifdef USE_X_TOOLKIT
14619 static XrmOptionDescRec emacs_options[] = {
14620 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14621 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14622
14623 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14624 XrmoptionSepArg, NULL},
14625 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14626
14627 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14628 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14629 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14630 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14631 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14632 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14633 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14634 };
14635 #endif /* USE_X_TOOLKIT */
14636
14637 static int x_initialized;
14638
14639 #ifdef MULTI_KBOARD
14640 /* Test whether two display-name strings agree up to the dot that separates
14641 the screen number from the server number. */
14642 static int
14643 same_x_server (name1, name2)
14644 char *name1, *name2;
14645 {
14646 int seen_colon = 0;
14647 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14648 int system_name_length = strlen (system_name);
14649 int length_until_period = 0;
14650
14651 while (system_name[length_until_period] != 0
14652 && system_name[length_until_period] != '.')
14653 length_until_period++;
14654
14655 /* Treat `unix' like an empty host name. */
14656 if (! strncmp (name1, "unix:", 5))
14657 name1 += 4;
14658 if (! strncmp (name2, "unix:", 5))
14659 name2 += 4;
14660 /* Treat this host's name like an empty host name. */
14661 if (! strncmp (name1, system_name, system_name_length)
14662 && name1[system_name_length] == ':')
14663 name1 += system_name_length;
14664 if (! strncmp (name2, system_name, system_name_length)
14665 && name2[system_name_length] == ':')
14666 name2 += system_name_length;
14667 /* Treat this host's domainless name like an empty host name. */
14668 if (! strncmp (name1, system_name, length_until_period)
14669 && name1[length_until_period] == ':')
14670 name1 += length_until_period;
14671 if (! strncmp (name2, system_name, length_until_period)
14672 && name2[length_until_period] == ':')
14673 name2 += length_until_period;
14674
14675 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14676 {
14677 if (*name1 == ':')
14678 seen_colon++;
14679 if (seen_colon && *name1 == '.')
14680 return 1;
14681 }
14682 return (seen_colon
14683 && (*name1 == '.' || *name1 == '\0')
14684 && (*name2 == '.' || *name2 == '\0'));
14685 }
14686 #endif
14687
14688 struct x_display_info *
14689 x_term_init (display_name, xrm_option, resource_name)
14690 Lisp_Object display_name;
14691 char *xrm_option;
14692 char *resource_name;
14693 {
14694 int connection;
14695 Display *dpy;
14696 struct x_display_info *dpyinfo;
14697 XrmDatabase xrdb;
14698
14699 BLOCK_INPUT;
14700
14701 if (!x_initialized)
14702 {
14703 x_initialize ();
14704 x_initialized = 1;
14705 }
14706
14707 #ifdef USE_X_TOOLKIT
14708 /* weiner@footloose.sps.mot.com reports that this causes
14709 errors with X11R5:
14710 X protocol error: BadAtom (invalid Atom parameter)
14711 on protocol request 18skiloaf.
14712 So let's not use it until R6. */
14713 #ifdef HAVE_X11XTR6
14714 XtSetLanguageProc (NULL, NULL, NULL);
14715 #endif
14716
14717 {
14718 int argc = 0;
14719 char *argv[3];
14720
14721 argv[0] = "";
14722 argc = 1;
14723 if (xrm_option)
14724 {
14725 argv[argc++] = "-xrm";
14726 argv[argc++] = xrm_option;
14727 }
14728 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14729 resource_name, EMACS_CLASS,
14730 emacs_options, XtNumber (emacs_options),
14731 &argc, argv);
14732
14733 #ifdef HAVE_X11XTR6
14734 /* I think this is to compensate for XtSetLanguageProc. */
14735 fixup_locale ();
14736 #endif
14737 }
14738
14739 #else /* not USE_X_TOOLKIT */
14740 #ifdef HAVE_X11R5
14741 XSetLocaleModifiers ("");
14742 #endif
14743 dpy = XOpenDisplay (XSTRING (display_name)->data);
14744 #endif /* not USE_X_TOOLKIT */
14745
14746 /* Detect failure. */
14747 if (dpy == 0)
14748 {
14749 UNBLOCK_INPUT;
14750 return 0;
14751 }
14752
14753 /* We have definitely succeeded. Record the new connection. */
14754
14755 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14756 bzero (dpyinfo, sizeof *dpyinfo);
14757
14758 #ifdef MULTI_KBOARD
14759 {
14760 struct x_display_info *share;
14761 Lisp_Object tail;
14762
14763 for (share = x_display_list, tail = x_display_name_list; share;
14764 share = share->next, tail = XCDR (tail))
14765 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14766 XSTRING (display_name)->data))
14767 break;
14768 if (share)
14769 dpyinfo->kboard = share->kboard;
14770 else
14771 {
14772 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14773 init_kboard (dpyinfo->kboard);
14774 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14775 {
14776 char *vendor = ServerVendor (dpy);
14777 UNBLOCK_INPUT;
14778 dpyinfo->kboard->Vsystem_key_alist
14779 = call1 (Qvendor_specific_keysyms,
14780 build_string (vendor ? vendor : ""));
14781 BLOCK_INPUT;
14782 }
14783
14784 dpyinfo->kboard->next_kboard = all_kboards;
14785 all_kboards = dpyinfo->kboard;
14786 /* Don't let the initial kboard remain current longer than necessary.
14787 That would cause problems if a file loaded on startup tries to
14788 prompt in the mini-buffer. */
14789 if (current_kboard == initial_kboard)
14790 current_kboard = dpyinfo->kboard;
14791 }
14792 dpyinfo->kboard->reference_count++;
14793 }
14794 #endif
14795
14796 /* Put this display on the chain. */
14797 dpyinfo->next = x_display_list;
14798 x_display_list = dpyinfo;
14799
14800 /* Put it on x_display_name_list as well, to keep them parallel. */
14801 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14802 x_display_name_list);
14803 dpyinfo->name_list_element = XCAR (x_display_name_list);
14804
14805 dpyinfo->display = dpy;
14806
14807 #if 0
14808 XSetAfterFunction (x_current_display, x_trace_wire);
14809 #endif /* ! 0 */
14810
14811 dpyinfo->x_id_name
14812 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14813 + STRING_BYTES (XSTRING (Vsystem_name))
14814 + 2);
14815 sprintf (dpyinfo->x_id_name, "%s@%s",
14816 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14817
14818 /* Figure out which modifier bits mean what. */
14819 x_find_modifier_meanings (dpyinfo);
14820
14821 /* Get the scroll bar cursor. */
14822 dpyinfo->vertical_scroll_bar_cursor
14823 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14824
14825 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14826 resource_name, EMACS_CLASS);
14827 #ifdef HAVE_XRMSETDATABASE
14828 XrmSetDatabase (dpyinfo->display, xrdb);
14829 #else
14830 dpyinfo->display->db = xrdb;
14831 #endif
14832 /* Put the rdb where we can find it in a way that works on
14833 all versions. */
14834 dpyinfo->xrdb = xrdb;
14835
14836 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14837 DefaultScreen (dpyinfo->display));
14838 select_visual (dpyinfo);
14839 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14840 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14841 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14842 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14843 dpyinfo->grabbed = 0;
14844 dpyinfo->reference_count = 0;
14845 dpyinfo->icon_bitmap_id = -1;
14846 dpyinfo->font_table = NULL;
14847 dpyinfo->n_fonts = 0;
14848 dpyinfo->font_table_size = 0;
14849 dpyinfo->bitmaps = 0;
14850 dpyinfo->bitmaps_size = 0;
14851 dpyinfo->bitmaps_last = 0;
14852 dpyinfo->scratch_cursor_gc = 0;
14853 dpyinfo->mouse_face_mouse_frame = 0;
14854 dpyinfo->mouse_face_deferred_gc = 0;
14855 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14856 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14857 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14858 dpyinfo->mouse_face_window = Qnil;
14859 dpyinfo->mouse_face_overlay = Qnil;
14860 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14861 dpyinfo->mouse_face_defer = 0;
14862 dpyinfo->mouse_face_hidden = 0;
14863 dpyinfo->x_focus_frame = 0;
14864 dpyinfo->x_focus_event_frame = 0;
14865 dpyinfo->x_highlight_frame = 0;
14866 dpyinfo->image_cache = make_image_cache ();
14867
14868 /* See if a private colormap is requested. */
14869 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14870 {
14871 if (dpyinfo->visual->class == PseudoColor)
14872 {
14873 Lisp_Object value;
14874 value = display_x_get_resource (dpyinfo,
14875 build_string ("privateColormap"),
14876 build_string ("PrivateColormap"),
14877 Qnil, Qnil);
14878 if (STRINGP (value)
14879 && (!strcmp (XSTRING (value)->data, "true")
14880 || !strcmp (XSTRING (value)->data, "on")))
14881 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14882 }
14883 }
14884 else
14885 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14886 dpyinfo->visual, AllocNone);
14887
14888 {
14889 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14890 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14891 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14892 dpyinfo->resy = pixels * 25.4 / mm;
14893 pixels = DisplayWidth (dpyinfo->display, screen_number);
14894 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14895 dpyinfo->resx = pixels * 25.4 / mm;
14896 }
14897
14898 dpyinfo->Xatom_wm_protocols
14899 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14900 dpyinfo->Xatom_wm_take_focus
14901 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14902 dpyinfo->Xatom_wm_save_yourself
14903 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14904 dpyinfo->Xatom_wm_delete_window
14905 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14906 dpyinfo->Xatom_wm_change_state
14907 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14908 dpyinfo->Xatom_wm_configure_denied
14909 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14910 dpyinfo->Xatom_wm_window_moved
14911 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14912 dpyinfo->Xatom_editres
14913 = XInternAtom (dpyinfo->display, "Editres", False);
14914 dpyinfo->Xatom_CLIPBOARD
14915 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14916 dpyinfo->Xatom_TIMESTAMP
14917 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14918 dpyinfo->Xatom_TEXT
14919 = XInternAtom (dpyinfo->display, "TEXT", False);
14920 dpyinfo->Xatom_COMPOUND_TEXT
14921 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14922 dpyinfo->Xatom_DELETE
14923 = XInternAtom (dpyinfo->display, "DELETE", False);
14924 dpyinfo->Xatom_MULTIPLE
14925 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14926 dpyinfo->Xatom_INCR
14927 = XInternAtom (dpyinfo->display, "INCR", False);
14928 dpyinfo->Xatom_EMACS_TMP
14929 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14930 dpyinfo->Xatom_TARGETS
14931 = XInternAtom (dpyinfo->display, "TARGETS", False);
14932 dpyinfo->Xatom_NULL
14933 = XInternAtom (dpyinfo->display, "NULL", False);
14934 dpyinfo->Xatom_ATOM_PAIR
14935 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14936 /* For properties of font. */
14937 dpyinfo->Xatom_PIXEL_SIZE
14938 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14939 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14940 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14941 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14942 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14943 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14944 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14945
14946 /* Ghostscript support. */
14947 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14948 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14949
14950 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14951 False);
14952
14953 dpyinfo->cut_buffers_initialized = 0;
14954
14955 connection = ConnectionNumber (dpyinfo->display);
14956 dpyinfo->connection = connection;
14957
14958 {
14959 char null_bits[1];
14960
14961 null_bits[0] = 0x00;
14962
14963 dpyinfo->null_pixel
14964 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14965 null_bits, 1, 1, (long) 0, (long) 0,
14966 1);
14967 }
14968
14969 {
14970 extern int gray_bitmap_width, gray_bitmap_height;
14971 extern char *gray_bitmap_bits;
14972 dpyinfo->gray
14973 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14974 gray_bitmap_bits,
14975 gray_bitmap_width, gray_bitmap_height,
14976 (unsigned long) 1, (unsigned long) 0, 1);
14977 }
14978
14979 #ifdef HAVE_X_I18N
14980 xim_initialize (dpyinfo, resource_name);
14981 #endif
14982
14983 #ifdef subprocesses
14984 /* This is only needed for distinguishing keyboard and process input. */
14985 if (connection != 0)
14986 add_keyboard_wait_descriptor (connection);
14987 #endif
14988
14989 #ifndef F_SETOWN_BUG
14990 #ifdef F_SETOWN
14991 #ifdef F_SETOWN_SOCK_NEG
14992 /* stdin is a socket here */
14993 fcntl (connection, F_SETOWN, -getpid ());
14994 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14995 fcntl (connection, F_SETOWN, getpid ());
14996 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14997 #endif /* ! defined (F_SETOWN) */
14998 #endif /* F_SETOWN_BUG */
14999
15000 #ifdef SIGIO
15001 if (interrupt_input)
15002 init_sigio (connection);
15003 #endif /* ! defined (SIGIO) */
15004
15005 #ifdef USE_LUCID
15006 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
15007 /* Make sure that we have a valid font for dialog boxes
15008 so that Xt does not crash. */
15009 {
15010 Display *dpy = dpyinfo->display;
15011 XrmValue d, fr, to;
15012 Font font;
15013 int count;
15014
15015 d.addr = (XPointer)&dpy;
15016 d.size = sizeof (Display *);
15017 fr.addr = XtDefaultFont;
15018 fr.size = sizeof (XtDefaultFont);
15019 to.size = sizeof (Font *);
15020 to.addr = (XPointer)&font;
15021 count = x_catch_errors (dpy);
15022 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15023 abort ();
15024 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15025 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
15026 x_uncatch_errors (dpy, count);
15027 }
15028 #endif
15029 #endif
15030
15031 /* See if we should run in synchronous mode. This is useful
15032 for debugging X code. */
15033 {
15034 Lisp_Object value;
15035 value = display_x_get_resource (dpyinfo,
15036 build_string ("synchronous"),
15037 build_string ("Synchronous"),
15038 Qnil, Qnil);
15039 if (STRINGP (value)
15040 && (!strcmp (XSTRING (value)->data, "true")
15041 || !strcmp (XSTRING (value)->data, "on")))
15042 XSynchronize (dpyinfo->display, True);
15043 }
15044
15045 {
15046 Lisp_Object value;
15047 value = display_x_get_resource (dpyinfo,
15048 build_string ("useXIM"),
15049 build_string ("UseXIM"),
15050 Qnil, Qnil);
15051 if (STRINGP (value)
15052 && (!strcmp (XSTRING (value)->data, "false")
15053 || !strcmp (XSTRING (value)->data, "off")))
15054 use_xim = 0;
15055 }
15056
15057 UNBLOCK_INPUT;
15058
15059 return dpyinfo;
15060 }
15061 \f
15062 /* Get rid of display DPYINFO, assuming all frames are already gone,
15063 and without sending any more commands to the X server. */
15064
15065 void
15066 x_delete_display (dpyinfo)
15067 struct x_display_info *dpyinfo;
15068 {
15069 delete_keyboard_wait_descriptor (dpyinfo->connection);
15070
15071 /* Discard this display from x_display_name_list and x_display_list.
15072 We can't use Fdelq because that can quit. */
15073 if (! NILP (x_display_name_list)
15074 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15075 x_display_name_list = XCDR (x_display_name_list);
15076 else
15077 {
15078 Lisp_Object tail;
15079
15080 tail = x_display_name_list;
15081 while (CONSP (tail) && CONSP (XCDR (tail)))
15082 {
15083 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
15084 {
15085 XSETCDR (tail, XCDR (XCDR (tail)));
15086 break;
15087 }
15088 tail = XCDR (tail);
15089 }
15090 }
15091
15092 if (next_noop_dpyinfo == dpyinfo)
15093 next_noop_dpyinfo = dpyinfo->next;
15094
15095 if (x_display_list == dpyinfo)
15096 x_display_list = dpyinfo->next;
15097 else
15098 {
15099 struct x_display_info *tail;
15100
15101 for (tail = x_display_list; tail; tail = tail->next)
15102 if (tail->next == dpyinfo)
15103 tail->next = tail->next->next;
15104 }
15105
15106 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15107 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15108 XrmDestroyDatabase (dpyinfo->xrdb);
15109 #endif
15110 #endif
15111 #ifdef MULTI_KBOARD
15112 if (--dpyinfo->kboard->reference_count == 0)
15113 delete_kboard (dpyinfo->kboard);
15114 #endif
15115 #ifdef HAVE_X_I18N
15116 if (dpyinfo->xim)
15117 xim_close_dpy (dpyinfo);
15118 #endif
15119
15120 xfree (dpyinfo->font_table);
15121 xfree (dpyinfo->x_id_name);
15122 xfree (dpyinfo->color_cells);
15123 xfree (dpyinfo);
15124 }
15125
15126 \f
15127 /* Set up use of X before we make the first connection. */
15128
15129 static struct redisplay_interface x_redisplay_interface =
15130 {
15131 x_produce_glyphs,
15132 x_write_glyphs,
15133 x_insert_glyphs,
15134 x_clear_end_of_line,
15135 x_scroll_run,
15136 x_after_update_window_line,
15137 x_update_window_begin,
15138 x_update_window_end,
15139 XTcursor_to,
15140 x_flush,
15141 x_clear_mouse_face,
15142 x_get_glyph_overhangs,
15143 x_fix_overlapping_area
15144 };
15145
15146 void
15147 x_initialize ()
15148 {
15149 rif = &x_redisplay_interface;
15150
15151 clear_frame_hook = x_clear_frame;
15152 ins_del_lines_hook = x_ins_del_lines;
15153 delete_glyphs_hook = x_delete_glyphs;
15154 ring_bell_hook = XTring_bell;
15155 reset_terminal_modes_hook = XTreset_terminal_modes;
15156 set_terminal_modes_hook = XTset_terminal_modes;
15157 update_begin_hook = x_update_begin;
15158 update_end_hook = x_update_end;
15159 set_terminal_window_hook = XTset_terminal_window;
15160 read_socket_hook = XTread_socket;
15161 frame_up_to_date_hook = XTframe_up_to_date;
15162 mouse_position_hook = XTmouse_position;
15163 frame_rehighlight_hook = XTframe_rehighlight;
15164 frame_raise_lower_hook = XTframe_raise_lower;
15165 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15166 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15167 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15168 judge_scroll_bars_hook = XTjudge_scroll_bars;
15169 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15170
15171 scroll_region_ok = 1; /* we'll scroll partial frames */
15172 char_ins_del_ok = 1;
15173 line_ins_del_ok = 1; /* we'll just blt 'em */
15174 fast_clear_end_of_line = 1; /* X does this well */
15175 memory_below_frame = 0; /* we don't remember what scrolls
15176 off the bottom */
15177 baud_rate = 19200;
15178
15179 x_noop_count = 0;
15180 last_tool_bar_item = -1;
15181 any_help_event_p = 0;
15182
15183 /* Try to use interrupt input; if we can't, then start polling. */
15184 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15185
15186 #ifdef USE_X_TOOLKIT
15187 XtToolkitInitialize ();
15188
15189 Xt_app_con = XtCreateApplicationContext ();
15190
15191 /* Register a converter from strings to pixels, which uses
15192 Emacs' color allocation infrastructure. */
15193 XtAppSetTypeConverter (Xt_app_con,
15194 XtRString, XtRPixel, cvt_string_to_pixel,
15195 cvt_string_to_pixel_args,
15196 XtNumber (cvt_string_to_pixel_args),
15197 XtCacheByDisplay, cvt_pixel_dtor);
15198
15199 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15200
15201 /* Install an asynchronous timer that processes Xt timeout events
15202 every 0.1s. This is necessary because some widget sets use
15203 timeouts internally, for example the LessTif menu bar, or the
15204 Xaw3d scroll bar. When Xt timouts aren't processed, these
15205 widgets don't behave normally. */
15206 {
15207 EMACS_TIME interval;
15208 EMACS_SET_SECS_USECS (interval, 0, 100000);
15209 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15210 }
15211 #endif
15212
15213 #ifdef USE_TOOLKIT_SCROLL_BARS
15214 xaw3d_arrow_scroll = False;
15215 xaw3d_pick_top = True;
15216 #endif
15217
15218 /* Note that there is no real way portable across R3/R4 to get the
15219 original error handler. */
15220 XSetErrorHandler (x_error_handler);
15221 XSetIOErrorHandler (x_io_error_quitter);
15222
15223 /* Disable Window Change signals; they are handled by X events. */
15224 #ifdef SIGWINCH
15225 signal (SIGWINCH, SIG_DFL);
15226 #endif /* ! defined (SIGWINCH) */
15227
15228 signal (SIGPIPE, x_connection_signal);
15229 }
15230
15231
15232 void
15233 syms_of_xterm ()
15234 {
15235 staticpro (&x_error_message_string);
15236 x_error_message_string = Qnil;
15237
15238 staticpro (&x_display_name_list);
15239 x_display_name_list = Qnil;
15240
15241 staticpro (&last_mouse_scroll_bar);
15242 last_mouse_scroll_bar = Qnil;
15243
15244 staticpro (&Qvendor_specific_keysyms);
15245 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15246
15247 staticpro (&last_mouse_press_frame);
15248 last_mouse_press_frame = Qnil;
15249
15250 help_echo = Qnil;
15251 staticpro (&help_echo);
15252 help_echo_object = Qnil;
15253 staticpro (&help_echo_object);
15254 help_echo_window = Qnil;
15255 staticpro (&help_echo_window);
15256 previous_help_echo = Qnil;
15257 staticpro (&previous_help_echo);
15258 help_echo_pos = -1;
15259
15260 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
15261 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15262 x_autoselect_window_p = 0;
15263
15264 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15265 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15266 For example, if a block cursor is over a tab, it will be drawn as
15267 wide as that tab on the display. */);
15268 x_stretch_cursor_p = 0;
15269
15270 DEFVAR_BOOL ("x-use-underline-position-properties",
15271 &x_use_underline_position_properties,
15272 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15273 nil means ignore them. If you encounter fonts with bogus
15274 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15275 to 4.1, set this to nil. */);
15276 x_use_underline_position_properties = 1;
15277
15278 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15279 doc: /* What X toolkit scroll bars Emacs uses.
15280 A value of nil means Emacs doesn't use X toolkit scroll bars.
15281 Otherwise, value is a symbol describing the X toolkit. */);
15282 #ifdef USE_TOOLKIT_SCROLL_BARS
15283 #ifdef USE_MOTIF
15284 Vx_toolkit_scroll_bars = intern ("motif");
15285 #elif defined HAVE_XAW3D
15286 Vx_toolkit_scroll_bars = intern ("xaw3d");
15287 #else
15288 Vx_toolkit_scroll_bars = intern ("xaw");
15289 #endif
15290 #else
15291 Vx_toolkit_scroll_bars = Qnil;
15292 #endif
15293
15294 staticpro (&last_mouse_motion_frame);
15295 last_mouse_motion_frame = Qnil;
15296
15297 Qmodifier_value = intern ("modifier-value");
15298 Qalt = intern ("alt");
15299 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15300 Qhyper = intern ("hyper");
15301 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15302 Qmeta = intern ("meta");
15303 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15304 Qsuper = intern ("super");
15305 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15306
15307 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15308 doc: /* Which keys Emacs uses for the alt modifier.
15309 This should be one of the symbols `alt', `hyper', `meta', `super'.
15310 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15311 is nil, which is the same as `alt'. */);
15312 Vx_alt_keysym = Qnil;
15313
15314 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15315 doc: /* Which keys Emacs uses for the hyper modifier.
15316 This should be one of the symbols `alt', `hyper', `meta', `super'.
15317 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15318 default is nil, which is the same as `hyper'. */);
15319 Vx_hyper_keysym = Qnil;
15320
15321 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15322 doc: /* Which keys Emacs uses for the meta modifier.
15323 This should be one of the symbols `alt', `hyper', `meta', `super'.
15324 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15325 default is nil, which is the same as `meta'. */);
15326 Vx_meta_keysym = Qnil;
15327
15328 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15329 doc: /* Which keys Emacs uses for the super modifier.
15330 This should be one of the symbols `alt', `hyper', `meta', `super'.
15331 For example, `super' means use the Super_L and Super_R keysyms. The
15332 default is nil, which is the same as `super'. */);
15333 Vx_super_keysym = Qnil;
15334
15335 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
15336 doc: /* Hash table of character codes indexed by X keysym codes. */);
15337 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
15338 make_float (DEFAULT_REHASH_SIZE),
15339 make_float (DEFAULT_REHASH_THRESHOLD),
15340 Qnil, Qnil, Qnil);
15341 }
15342
15343 #endif /* HAVE_X_WINDOWS */