]> code.delx.au - gnu-emacs/blob - src/xterm.c
(x_draw_glyph_string_box): Don't draw a full-width
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
91
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
95
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
102
103 #ifdef USE_X_TOOLKIT
104
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
108
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
113
114 /* Include toolkit specific headers for the scroll bar widget. */
115
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #else /* !USE_MOTIF i.e. use Xaw */
121
122 #ifdef HAVE_XAW3D
123 #include <X11/Xaw3d/Simple.h>
124 #include <X11/Xaw3d/Scrollbar.h>
125 #define ARROW_SCROLLBAR
126 #include <X11/Xaw3d/ScrollbarP.h>
127 #else /* !HAVE_XAW3D */
128 #include <X11/Xaw/Simple.h>
129 #include <X11/Xaw/Scrollbar.h>
130 #endif /* !HAVE_XAW3D */
131 #ifndef XtNpickTop
132 #define XtNpickTop "pickTop"
133 #endif /* !XtNpickTop */
134 #endif /* !USE_MOTIF */
135 #endif /* USE_TOOLKIT_SCROLL_BARS */
136
137 #endif /* USE_X_TOOLKIT */
138
139 #ifndef USE_X_TOOLKIT
140 #define x_any_window_to_frame x_window_to_frame
141 #define x_top_window_to_frame x_window_to_frame
142 #endif
143
144 #ifdef USE_X_TOOLKIT
145 #include "widget.h"
146 #ifndef XtNinitialState
147 #define XtNinitialState "initialState"
148 #endif
149 #endif
150
151 #ifndef min
152 #define min(a,b) ((a) < (b) ? (a) : (b))
153 #endif
154 #ifndef max
155 #define max(a,b) ((a) > (b) ? (a) : (b))
156 #endif
157
158 #define abs(x) ((x) < 0 ? -(x) : (x))
159
160 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
161
162 \f
163 /* Bitmaps for truncated lines. */
164
165 enum bitmap_type
166 {
167 NO_BITMAP,
168 LEFT_TRUNCATION_BITMAP,
169 RIGHT_TRUNCATION_BITMAP,
170 OVERLAY_ARROW_BITMAP,
171 CONTINUED_LINE_BITMAP,
172 CONTINUATION_LINE_BITMAP,
173 ZV_LINE_BITMAP
174 };
175
176 /* Bitmap drawn to indicate lines not displaying text if
177 `indicate-empty-lines' is non-nil. */
178
179 #define zv_width 8
180 #define zv_height 8
181 static unsigned char zv_bits[] = {
182 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
183
184 /* An arrow like this: `<-'. */
185
186 #define left_width 8
187 #define left_height 8
188 static unsigned char left_bits[] = {
189 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
190
191 /* Right truncation arrow bitmap `->'. */
192
193 #define right_width 8
194 #define right_height 8
195 static unsigned char right_bits[] = {
196 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
197
198 /* Marker for continued lines. */
199
200 #define continued_width 8
201 #define continued_height 8
202 static unsigned char continued_bits[] = {
203 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
204
205 /* Marker for continuation lines. */
206
207 #define continuation_width 8
208 #define continuation_height 8
209 static unsigned char continuation_bits[] = {
210 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
211
212 /* Overlay arrow bitmap. */
213
214 #if 0
215 /* A bomb. */
216 #define ov_width 8
217 #define ov_height 8
218 static unsigned char ov_bits[] = {
219 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
220 #else
221 /* A triangular arrow. */
222 #define ov_width 8
223 #define ov_height 8
224 static unsigned char ov_bits[] = {
225 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
226
227 #endif
228
229 extern Lisp_Object Qhelp_echo;
230
231 \f
232 /* Non-nil means Emacs uses toolkit scroll bars. */
233
234 Lisp_Object Vx_toolkit_scroll_bars;
235
236 /* If a string, XTread_socket generates an event to display that string.
237 (The display is done in read_char.) */
238
239 static Lisp_Object help_echo;
240 static Lisp_Object help_echo_window;
241 static Lisp_Object help_echo_object;
242 static int help_echo_pos;
243
244 /* Temporary variable for XTread_socket. */
245
246 static Lisp_Object previous_help_echo;
247
248 /* Non-zero means that a HELP_EVENT has been generated since Emacs
249 start. */
250
251 static int any_help_event_p;
252
253 /* Non-zero means draw block and hollow cursor as wide as the glyph
254 under it. For example, if a block cursor is over a tab, it will be
255 drawn as wide as that tab on the display. */
256
257 int x_stretch_cursor_p;
258
259 /* This is a chain of structures for all the X displays currently in
260 use. */
261
262 struct x_display_info *x_display_list;
263
264 /* This is a list of cons cells, each of the form (NAME
265 . FONT-LIST-CACHE), one for each element of x_display_list and in
266 the same order. NAME is the name of the frame. FONT-LIST-CACHE
267 records previous values returned by x-list-fonts. */
268
269 Lisp_Object x_display_name_list;
270
271 /* Frame being updated by update_frame. This is declared in term.c.
272 This is set by update_begin and looked at by all the XT functions.
273 It is zero while not inside an update. In that case, the XT
274 functions assume that `selected_frame' is the frame to apply to. */
275
276 extern struct frame *updating_frame;
277
278 extern int waiting_for_input;
279
280 /* This is a frame waiting to be auto-raised, within XTread_socket. */
281
282 struct frame *pending_autoraise_frame;
283
284 #ifdef USE_X_TOOLKIT
285 /* The application context for Xt use. */
286 XtAppContext Xt_app_con;
287 static String Xt_default_resources[] = {0};
288 #endif /* USE_X_TOOLKIT */
289
290 /* Nominal cursor position -- where to draw output.
291 HPOS and VPOS are window relative glyph matrix coordinates.
292 X and Y are window relative pixel coordinates. */
293
294 struct cursor_pos output_cursor;
295
296 /* Non-zero means user is interacting with a toolkit scroll bar. */
297
298 static int toolkit_scroll_bar_interaction;
299
300 /* Mouse movement.
301
302 Formerly, we used PointerMotionHintMask (in standard_event_mask)
303 so that we would have to call XQueryPointer after each MotionNotify
304 event to ask for another such event. However, this made mouse tracking
305 slow, and there was a bug that made it eventually stop.
306
307 Simply asking for MotionNotify all the time seems to work better.
308
309 In order to avoid asking for motion events and then throwing most
310 of them away or busy-polling the server for mouse positions, we ask
311 the server for pointer motion hints. This means that we get only
312 one event per group of mouse movements. "Groups" are delimited by
313 other kinds of events (focus changes and button clicks, for
314 example), or by XQueryPointer calls; when one of these happens, we
315 get another MotionNotify event the next time the mouse moves. This
316 is at least as efficient as getting motion events when mouse
317 tracking is on, and I suspect only negligibly worse when tracking
318 is off. */
319
320 /* Where the mouse was last time we reported a mouse event. */
321
322 FRAME_PTR last_mouse_frame;
323 static XRectangle last_mouse_glyph;
324 static Lisp_Object last_mouse_press_frame;
325
326 /* The scroll bar in which the last X motion event occurred.
327
328 If the last X motion event occurred in a scroll bar, we set this so
329 XTmouse_position can know whether to report a scroll bar motion or
330 an ordinary motion.
331
332 If the last X motion event didn't occur in a scroll bar, we set
333 this to Qnil, to tell XTmouse_position to return an ordinary motion
334 event. */
335
336 static Lisp_Object last_mouse_scroll_bar;
337
338 /* This is a hack. We would really prefer that XTmouse_position would
339 return the time associated with the position it returns, but there
340 doesn't seem to be any way to wrest the time-stamp from the server
341 along with the position query. So, we just keep track of the time
342 of the last movement we received, and return that in hopes that
343 it's somewhat accurate. */
344
345 static Time last_mouse_movement_time;
346
347 /* Incremented by XTread_socket whenever it really tries to read
348 events. */
349
350 #ifdef __STDC__
351 static int volatile input_signal_count;
352 #else
353 static int input_signal_count;
354 #endif
355
356 /* Used locally within XTread_socket. */
357
358 static int x_noop_count;
359
360 /* Initial values of argv and argc. */
361
362 extern char **initial_argv;
363 extern int initial_argc;
364
365 extern Lisp_Object Vcommand_line_args, Vsystem_name;
366
367 /* Tells if a window manager is present or not. */
368
369 extern Lisp_Object Vx_no_window_manager;
370
371 extern Lisp_Object Qface, Qmouse_face;
372
373 extern int errno;
374
375 /* A mask of extra modifier bits to put into every keyboard char. */
376
377 extern int extra_keyboard_modifiers;
378
379 static Lisp_Object Qvendor_specific_keysyms;
380
381 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
382 extern Lisp_Object x_icon_type P_ ((struct frame *));
383
384
385 /* Enumeration for overriding/changing the face to use for drawing
386 glyphs in x_draw_glyphs. */
387
388 enum draw_glyphs_face
389 {
390 DRAW_NORMAL_TEXT,
391 DRAW_INVERSE_VIDEO,
392 DRAW_CURSOR,
393 DRAW_MOUSE_FACE,
394 DRAW_IMAGE_RAISED,
395 DRAW_IMAGE_SUNKEN
396 };
397
398 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
399 static const XColor *x_color_cells P_ ((struct frame *, int *));
400 static void x_update_window_end P_ ((struct window *, int, int));
401 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402 void x_delete_display P_ ((struct x_display_info *));
403 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405 static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
408 int *, int *, int *, int *, int));
409 static void set_output_cursor P_ ((struct cursor_pos *));
410 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
411 int *, int *, int *, int));
412 static void note_mode_line_highlight P_ ((struct window *, int, int));
413 static void note_mouse_highlight P_ ((struct frame *, int, int));
414 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
415 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
416 static void show_mouse_face P_ ((struct x_display_info *,
417 enum draw_glyphs_face));
418 static int x_io_error_quitter P_ ((Display *));
419 int x_catch_errors P_ ((Display *));
420 void x_uncatch_errors P_ ((Display *, int));
421 void x_lower_frame P_ ((struct frame *));
422 void x_scroll_bar_clear P_ ((struct frame *));
423 int x_had_errors_p P_ ((Display *));
424 void x_wm_set_size_hint P_ ((struct frame *, long, int));
425 void x_raise_frame P_ ((struct frame *));
426 void x_set_window_size P_ ((struct frame *, int, int, int));
427 void x_wm_set_window_state P_ ((struct frame *, int));
428 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
429 void x_initialize P_ ((void));
430 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
431 static int x_compute_min_glyph_bounds P_ ((struct frame *));
432 static void x_draw_phys_cursor_glyph P_ ((struct window *,
433 struct glyph_row *,
434 enum draw_glyphs_face));
435 static void x_update_end P_ ((struct frame *));
436 static void XTframe_up_to_date P_ ((struct frame *));
437 static void XTreassert_line_highlight P_ ((int, int));
438 static void x_change_line_highlight P_ ((int, int, int, int));
439 static void XTset_terminal_modes P_ ((void));
440 static void XTreset_terminal_modes P_ ((void));
441 static void XTcursor_to P_ ((int, int, int, int));
442 static void x_write_glyphs P_ ((struct glyph *, int));
443 static void x_clear_end_of_line P_ ((int));
444 static void x_clear_frame P_ ((void));
445 static void x_clear_cursor P_ ((struct window *));
446 static void frame_highlight P_ ((struct frame *));
447 static void frame_unhighlight P_ ((struct frame *));
448 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
449 static void XTframe_rehighlight P_ ((struct frame *));
450 static void x_frame_rehighlight P_ ((struct x_display_info *));
451 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
452 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
453 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
454 XRectangle *));
455 static void expose_frame P_ ((struct frame *, int, int, int, int));
456 static void expose_window_tree P_ ((struct window *, XRectangle *));
457 static void expose_window P_ ((struct window *, XRectangle *));
458 static void expose_area P_ ((struct window *, struct glyph_row *,
459 XRectangle *, enum glyph_row_area));
460 static void expose_line P_ ((struct window *, struct glyph_row *,
461 XRectangle *));
462 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
463 static void x_update_window_cursor P_ ((struct window *, int));
464 static void x_erase_phys_cursor P_ ((struct window *));
465 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
466 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
467 enum bitmap_type));
468
469 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
470 GC, int));
471 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
472 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
473 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
474 static void x_flush P_ ((struct frame *f));
475 static void x_update_begin P_ ((struct frame *));
476 static void x_update_window_begin P_ ((struct window *));
477 static void x_draw_vertical_border P_ ((struct window *));
478 static void x_after_update_window_line P_ ((struct glyph_row *));
479 static INLINE void take_vertical_position_into_account P_ ((struct it *));
480 static void x_produce_stretch_glyph P_ ((struct it *));
481 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
482 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
483 enum scroll_bar_part *,
484 Lisp_Object *, Lisp_Object *,
485 unsigned long *));
486
487 /* Flush display of frame F, or of all frames if F is null. */
488
489 static void
490 x_flush (f)
491 struct frame *f;
492 {
493 BLOCK_INPUT;
494 if (f == NULL)
495 {
496 Lisp_Object rest, frame;
497 FOR_EACH_FRAME (rest, frame)
498 x_flush (XFRAME (frame));
499 }
500 else if (FRAME_X_P (f))
501 XFlush (FRAME_X_DISPLAY (f));
502 UNBLOCK_INPUT;
503 }
504
505
506 /* Remove calls to XFlush by defining XFlush to an empty replacement.
507 Calls to XFlush should be unnecessary because the X output buffer
508 is flushed automatically as needed by calls to XPending,
509 XNextEvent, or XWindowEvent according to the XFlush man page.
510 XTread_socket calls XPending. Removing XFlush improves
511 performance. */
512
513 #define XFlush(DISPLAY) (void) 0
514
515 \f
516 /***********************************************************************
517 Debugging
518 ***********************************************************************/
519
520 #if 0
521
522 /* This is a function useful for recording debugging information about
523 the sequence of occurrences in this file. */
524
525 struct record
526 {
527 char *locus;
528 int type;
529 };
530
531 struct record event_record[100];
532
533 int event_record_index;
534
535 record_event (locus, type)
536 char *locus;
537 int type;
538 {
539 if (event_record_index == sizeof (event_record) / sizeof (struct record))
540 event_record_index = 0;
541
542 event_record[event_record_index].locus = locus;
543 event_record[event_record_index].type = type;
544 event_record_index++;
545 }
546
547 #endif /* 0 */
548
549
550 \f
551 /* Return the struct x_display_info corresponding to DPY. */
552
553 struct x_display_info *
554 x_display_info_for_display (dpy)
555 Display *dpy;
556 {
557 struct x_display_info *dpyinfo;
558
559 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
560 if (dpyinfo->display == dpy)
561 return dpyinfo;
562
563 return 0;
564 }
565
566
567 \f
568 /***********************************************************************
569 Starting and ending an update
570 ***********************************************************************/
571
572 /* Start an update of frame F. This function is installed as a hook
573 for update_begin, i.e. it is called when update_begin is called.
574 This function is called prior to calls to x_update_window_begin for
575 each window being updated. Currently, there is nothing to do here
576 because all interesting stuff is done on a window basis. */
577
578 static void
579 x_update_begin (f)
580 struct frame *f;
581 {
582 /* Nothing to do. */
583 }
584
585
586 /* Start update of window W. Set the global variable updated_window
587 to the window being updated and set output_cursor to the cursor
588 position of W. */
589
590 static void
591 x_update_window_begin (w)
592 struct window *w;
593 {
594 struct frame *f = XFRAME (WINDOW_FRAME (w));
595 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
596
597 updated_window = w;
598 set_output_cursor (&w->cursor);
599
600 BLOCK_INPUT;
601
602 if (f == display_info->mouse_face_mouse_frame)
603 {
604 /* Don't do highlighting for mouse motion during the update. */
605 display_info->mouse_face_defer = 1;
606
607 /* If F needs to be redrawn, simply forget about any prior mouse
608 highlighting. */
609 if (FRAME_GARBAGED_P (f))
610 display_info->mouse_face_window = Qnil;
611
612 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
613 their mouse_face_p flag set, which means that they are always
614 unequal to rows in a desired matrix which never have that
615 flag set. So, rows containing mouse-face glyphs are never
616 scrolled, and we don't have to switch the mouse highlight off
617 here to prevent it from being scrolled. */
618
619 /* Can we tell that this update does not affect the window
620 where the mouse highlight is? If so, no need to turn off.
621 Likewise, don't do anything if the frame is garbaged;
622 in that case, the frame's current matrix that we would use
623 is all wrong, and we will redisplay that line anyway. */
624 if (!NILP (display_info->mouse_face_window)
625 && w == XWINDOW (display_info->mouse_face_window))
626 {
627 int i;
628
629 for (i = 0; i < w->desired_matrix->nrows; ++i)
630 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
631 break;
632
633 if (i < w->desired_matrix->nrows)
634 clear_mouse_face (display_info);
635 }
636 #endif /* 0 */
637 }
638
639 UNBLOCK_INPUT;
640 }
641
642
643 /* Draw a vertical window border to the right of window W if W doesn't
644 have vertical scroll bars. */
645
646 static void
647 x_draw_vertical_border (w)
648 struct window *w;
649 {
650 struct frame *f = XFRAME (WINDOW_FRAME (w));
651
652 /* Redraw borders between horizontally adjacent windows. Don't
653 do it for frames with vertical scroll bars because either the
654 right scroll bar of a window, or the left scroll bar of its
655 neighbor will suffice as a border. */
656 if (!WINDOW_RIGHTMOST_P (w)
657 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
658 {
659 int x0, x1, y0, y1;
660
661 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
662 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
663 y1 -= 1;
664
665 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
666 f->output_data.x->normal_gc, x1, y0, x1, y1);
667 }
668 }
669
670
671 /* End update of window W (which is equal to updated_window).
672
673 Draw vertical borders between horizontally adjacent windows, and
674 display W's cursor if CURSOR_ON_P is non-zero.
675
676 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
677 glyphs in mouse-face were overwritten. In that case we have to
678 make sure that the mouse-highlight is properly redrawn.
679
680 W may be a menu bar pseudo-window in case we don't have X toolkit
681 support. Such windows don't have a cursor, so don't display it
682 here. */
683
684 static void
685 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
686 struct window *w;
687 int cursor_on_p, mouse_face_overwritten_p;
688 {
689 if (!w->pseudo_window_p)
690 {
691 struct x_display_info *dpyinfo
692 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
693
694 BLOCK_INPUT;
695
696 /* If a row with mouse-face was overwritten, arrange for
697 XTframe_up_to_date to redisplay the mouse highlight. */
698 if (mouse_face_overwritten_p)
699 {
700 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
701 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
702 dpyinfo->mouse_face_window = Qnil;
703 }
704
705 if (cursor_on_p)
706 x_display_and_set_cursor (w, 1, output_cursor.hpos,
707 output_cursor.vpos,
708 output_cursor.x, output_cursor.y);
709
710 x_draw_vertical_border (w);
711 UNBLOCK_INPUT;
712 }
713
714 updated_window = NULL;
715 }
716
717
718 /* End update of frame F. This function is installed as a hook in
719 update_end. */
720
721 static void
722 x_update_end (f)
723 struct frame *f;
724 {
725 /* Mouse highlight may be displayed again. */
726 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
727
728 BLOCK_INPUT;
729 XFlush (FRAME_X_DISPLAY (f));
730 UNBLOCK_INPUT;
731 }
732
733
734 /* This function is called from various places in xdisp.c whenever a
735 complete update has been performed. The global variable
736 updated_window is not available here. */
737
738 static void
739 XTframe_up_to_date (f)
740 struct frame *f;
741 {
742 if (FRAME_X_P (f))
743 {
744 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
745
746 if (dpyinfo->mouse_face_deferred_gc
747 || f == dpyinfo->mouse_face_mouse_frame)
748 {
749 BLOCK_INPUT;
750 if (dpyinfo->mouse_face_mouse_frame)
751 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
752 dpyinfo->mouse_face_mouse_x,
753 dpyinfo->mouse_face_mouse_y);
754 dpyinfo->mouse_face_deferred_gc = 0;
755 UNBLOCK_INPUT;
756 }
757 }
758 }
759
760
761 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
762 arrow bitmaps, or clear the areas where they would be displayed
763 before DESIRED_ROW is made current. The window being updated is
764 found in updated_window. This function It is called from
765 update_window_line only if it is known that there are differences
766 between bitmaps to be drawn between current row and DESIRED_ROW. */
767
768 static void
769 x_after_update_window_line (desired_row)
770 struct glyph_row *desired_row;
771 {
772 struct window *w = updated_window;
773
774 xassert (w);
775
776 if (!desired_row->mode_line_p && !w->pseudo_window_p)
777 {
778 struct frame *f;
779 int width;
780
781 BLOCK_INPUT;
782 x_draw_row_bitmaps (w, desired_row);
783
784 /* When a window has disappeared, make sure that no rest of
785 full-width rows stays visible in the internal border. */
786 if (windows_or_buffers_changed
787 && (f = XFRAME (w->frame),
788 width = FRAME_INTERNAL_BORDER_WIDTH (f),
789 width != 0))
790 {
791 int height = desired_row->visible_height;
792 int x = (window_box_right (w, -1)
793 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
794 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
795
796 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
797 x, y, width, height, False);
798 }
799
800 UNBLOCK_INPUT;
801 }
802 }
803
804
805 /* Draw the bitmap WHICH in one of the areas to the left or right of
806 window W. ROW is the glyph row for which to display the bitmap; it
807 determines the vertical position at which the bitmap has to be
808 drawn. */
809
810 static void
811 x_draw_bitmap (w, row, which)
812 struct window *w;
813 struct glyph_row *row;
814 enum bitmap_type which;
815 {
816 struct frame *f = XFRAME (WINDOW_FRAME (w));
817 Display *display = FRAME_X_DISPLAY (f);
818 Window window = FRAME_X_WINDOW (f);
819 int x, y, wd, h, dy;
820 unsigned char *bits;
821 Pixmap pixmap;
822 GC gc = f->output_data.x->normal_gc;
823 struct face *face;
824 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
825
826 /* Must clip because of partially visible lines. */
827 x_clip_to_row (w, row, gc, 1);
828
829 switch (which)
830 {
831 case LEFT_TRUNCATION_BITMAP:
832 wd = left_width;
833 h = left_height;
834 bits = left_bits;
835 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
836 - wd
837 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
838 break;
839
840 case OVERLAY_ARROW_BITMAP:
841 wd = left_width;
842 h = left_height;
843 bits = ov_bits;
844 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
845 - wd
846 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
847 break;
848
849 case RIGHT_TRUNCATION_BITMAP:
850 wd = right_width;
851 h = right_height;
852 bits = right_bits;
853 x = window_box_right (w, -1);
854 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
855 break;
856
857 case CONTINUED_LINE_BITMAP:
858 wd = right_width;
859 h = right_height;
860 bits = continued_bits;
861 x = window_box_right (w, -1);
862 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
863 break;
864
865 case CONTINUATION_LINE_BITMAP:
866 wd = continuation_width;
867 h = continuation_height;
868 bits = continuation_bits;
869 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
870 - wd
871 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
872 break;
873
874 case ZV_LINE_BITMAP:
875 wd = zv_width;
876 h = zv_height;
877 bits = zv_bits;
878 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
879 - wd
880 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
881 break;
882
883 default:
884 abort ();
885 }
886
887 /* Convert to frame coordinates. Set dy to the offset in the row to
888 start drawing the bitmap. */
889 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
890 dy = (row->height - h) / 2;
891
892 /* Draw the bitmap. I believe these small pixmaps can be cached
893 by the server. */
894 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
895 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
896 face->foreground,
897 face->background, depth);
898 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
899 XFreePixmap (display, pixmap);
900 XSetClipMask (display, gc, None);
901 }
902
903
904 /* Draw flags bitmaps for glyph row ROW on window W. Call this
905 function with input blocked. */
906
907 static void
908 x_draw_row_bitmaps (w, row)
909 struct window *w;
910 struct glyph_row *row;
911 {
912 struct frame *f = XFRAME (w->frame);
913 enum bitmap_type bitmap;
914 struct face *face;
915 int header_line_height = -1;
916
917 xassert (interrupt_input_blocked);
918
919 /* If row is completely invisible, because of vscrolling, we
920 don't have to draw anything. */
921 if (row->visible_height <= 0)
922 return;
923
924 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
925 PREPARE_FACE_FOR_DISPLAY (f, face);
926
927 /* Decide which bitmap to draw at the left side. */
928 if (row->overlay_arrow_p)
929 bitmap = OVERLAY_ARROW_BITMAP;
930 else if (row->truncated_on_left_p)
931 bitmap = LEFT_TRUNCATION_BITMAP;
932 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
933 bitmap = CONTINUATION_LINE_BITMAP;
934 else if (row->indicate_empty_line_p)
935 bitmap = ZV_LINE_BITMAP;
936 else
937 bitmap = NO_BITMAP;
938
939 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
940 the flags area. */
941 if (bitmap == NO_BITMAP
942 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
943 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
944 {
945 /* If W has a vertical border to its left, don't draw over it. */
946 int border = ((XFASTINT (w->left) > 0
947 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
948 ? 1 : 0);
949 int left = window_box_left (w, -1);
950
951 if (header_line_height < 0)
952 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
953
954 /* In case the same realized face is used for bitmap areas and
955 for something displayed in the text (e.g. face `region' on
956 mono-displays, the fill style may have been changed to
957 FillSolid in x_draw_glyph_string_background. */
958 if (face->stipple)
959 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
960 else
961 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
962
963 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
964 face->gc,
965 (left
966 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
967 + border),
968 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
969 row->y)),
970 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
971 row->visible_height);
972 if (!face->stipple)
973 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
974 }
975
976 /* Draw the left bitmap. */
977 if (bitmap != NO_BITMAP)
978 x_draw_bitmap (w, row, bitmap);
979
980 /* Decide which bitmap to draw at the right side. */
981 if (row->truncated_on_right_p)
982 bitmap = RIGHT_TRUNCATION_BITMAP;
983 else if (row->continued_p)
984 bitmap = CONTINUED_LINE_BITMAP;
985 else
986 bitmap = NO_BITMAP;
987
988 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
989 the flags area. */
990 if (bitmap == NO_BITMAP
991 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
992 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
993 {
994 int right = window_box_right (w, -1);
995
996 if (header_line_height < 0)
997 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
998
999 /* In case the same realized face is used for bitmap areas and
1000 for something displayed in the text (e.g. face `region' on
1001 mono-displays, the fill style may have been changed to
1002 FillSolid in x_draw_glyph_string_background. */
1003 if (face->stipple)
1004 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1005 else
1006 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1007 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1008 face->gc,
1009 right,
1010 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1011 row->y)),
1012 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1013 row->visible_height);
1014 if (!face->stipple)
1015 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1016 }
1017
1018 /* Draw the right bitmap. */
1019 if (bitmap != NO_BITMAP)
1020 x_draw_bitmap (w, row, bitmap);
1021 }
1022
1023 \f
1024 /***********************************************************************
1025 Line Highlighting
1026 ***********************************************************************/
1027
1028 /* External interface to control of standout mode. Not used for X
1029 frames. Aborts when called. */
1030
1031 static void
1032 XTreassert_line_highlight (new, vpos)
1033 int new, vpos;
1034 {
1035 abort ();
1036 }
1037
1038
1039 /* Call this when about to modify line at position VPOS and change
1040 whether it is highlighted. Not used for X frames. Aborts when
1041 called. */
1042
1043 static void
1044 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1045 int new_highlight, vpos, y, first_unused_hpos;
1046 {
1047 abort ();
1048 }
1049
1050
1051 /* This is called when starting Emacs and when restarting after
1052 suspend. When starting Emacs, no X window is mapped. And nothing
1053 must be done to Emacs's own window if it is suspended (though that
1054 rarely happens). */
1055
1056 static void
1057 XTset_terminal_modes ()
1058 {
1059 }
1060
1061 /* This is called when exiting or suspending Emacs. Exiting will make
1062 the X-windows go away, and suspending requires no action. */
1063
1064 static void
1065 XTreset_terminal_modes ()
1066 {
1067 }
1068
1069
1070 \f
1071 /***********************************************************************
1072 Output Cursor
1073 ***********************************************************************/
1074
1075 /* Set the global variable output_cursor to CURSOR. All cursor
1076 positions are relative to updated_window. */
1077
1078 static void
1079 set_output_cursor (cursor)
1080 struct cursor_pos *cursor;
1081 {
1082 output_cursor.hpos = cursor->hpos;
1083 output_cursor.vpos = cursor->vpos;
1084 output_cursor.x = cursor->x;
1085 output_cursor.y = cursor->y;
1086 }
1087
1088
1089 /* Set a nominal cursor position.
1090
1091 HPOS and VPOS are column/row positions in a window glyph matrix. X
1092 and Y are window text area relative pixel positions.
1093
1094 If this is done during an update, updated_window will contain the
1095 window that is being updated and the position is the future output
1096 cursor position for that window. If updated_window is null, use
1097 selected_window and display the cursor at the given position. */
1098
1099 static void
1100 XTcursor_to (vpos, hpos, y, x)
1101 int vpos, hpos, y, x;
1102 {
1103 struct window *w;
1104
1105 /* If updated_window is not set, work on selected_window. */
1106 if (updated_window)
1107 w = updated_window;
1108 else
1109 w = XWINDOW (selected_window);
1110
1111 /* Set the output cursor. */
1112 output_cursor.hpos = hpos;
1113 output_cursor.vpos = vpos;
1114 output_cursor.x = x;
1115 output_cursor.y = y;
1116
1117 /* If not called as part of an update, really display the cursor.
1118 This will also set the cursor position of W. */
1119 if (updated_window == NULL)
1120 {
1121 BLOCK_INPUT;
1122 x_display_cursor (w, 1, hpos, vpos, x, y);
1123 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1124 UNBLOCK_INPUT;
1125 }
1126 }
1127
1128
1129 \f
1130 /***********************************************************************
1131 Display Iterator
1132 ***********************************************************************/
1133
1134 /* Function prototypes of this page. */
1135
1136 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1137 struct glyph *,
1138 XChar2b *,
1139 int *));
1140 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1141 int, XChar2b *, int));
1142 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1143 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1144 static void x_append_glyph P_ ((struct it *));
1145 static void x_append_composite_glyph P_ ((struct it *));
1146 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1147 int, int, double));
1148 static void x_produce_glyphs P_ ((struct it *));
1149 static void x_produce_image_glyph P_ ((struct it *it));
1150
1151
1152 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1153 is not contained in the font. */
1154
1155 static INLINE XCharStruct *
1156 x_per_char_metric (font, char2b)
1157 XFontStruct *font;
1158 XChar2b *char2b;
1159 {
1160 /* The result metric information. */
1161 XCharStruct *pcm = NULL;
1162
1163 xassert (font && char2b);
1164
1165 if (font->per_char != NULL)
1166 {
1167 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1168 {
1169 /* min_char_or_byte2 specifies the linear character index
1170 corresponding to the first element of the per_char array,
1171 max_char_or_byte2 is the index of the last character. A
1172 character with non-zero CHAR2B->byte1 is not in the font.
1173 A character with byte2 less than min_char_or_byte2 or
1174 greater max_char_or_byte2 is not in the font. */
1175 if (char2b->byte1 == 0
1176 && char2b->byte2 >= font->min_char_or_byte2
1177 && char2b->byte2 <= font->max_char_or_byte2)
1178 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1179 }
1180 else
1181 {
1182 /* If either min_byte1 or max_byte1 are nonzero, both
1183 min_char_or_byte2 and max_char_or_byte2 are less than
1184 256, and the 2-byte character index values corresponding
1185 to the per_char array element N (counting from 0) are:
1186
1187 byte1 = N/D + min_byte1
1188 byte2 = N\D + min_char_or_byte2
1189
1190 where:
1191
1192 D = max_char_or_byte2 - min_char_or_byte2 + 1
1193 / = integer division
1194 \ = integer modulus */
1195 if (char2b->byte1 >= font->min_byte1
1196 && char2b->byte1 <= font->max_byte1
1197 && char2b->byte2 >= font->min_char_or_byte2
1198 && char2b->byte2 <= font->max_char_or_byte2)
1199 {
1200 pcm = (font->per_char
1201 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1202 * (char2b->byte1 - font->min_byte1))
1203 + (char2b->byte2 - font->min_char_or_byte2));
1204 }
1205 }
1206 }
1207 else
1208 {
1209 /* If the per_char pointer is null, all glyphs between the first
1210 and last character indexes inclusive have the same
1211 information, as given by both min_bounds and max_bounds. */
1212 if (char2b->byte2 >= font->min_char_or_byte2
1213 && char2b->byte2 <= font->max_char_or_byte2)
1214 pcm = &font->max_bounds;
1215 }
1216
1217 return ((pcm == NULL
1218 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1219 ? NULL : pcm);
1220 }
1221
1222
1223 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1224 the two-byte form of C. Encoding is returned in *CHAR2B. */
1225
1226 static INLINE void
1227 x_encode_char (c, char2b, font_info)
1228 int c;
1229 XChar2b *char2b;
1230 struct font_info *font_info;
1231 {
1232 int charset = CHAR_CHARSET (c);
1233 XFontStruct *font = font_info->font;
1234
1235 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1236 This may be either a program in a special encoder language or a
1237 fixed encoding. */
1238 if (font_info->font_encoder)
1239 {
1240 /* It's a program. */
1241 struct ccl_program *ccl = font_info->font_encoder;
1242
1243 if (CHARSET_DIMENSION (charset) == 1)
1244 {
1245 ccl->reg[0] = charset;
1246 ccl->reg[1] = char2b->byte2;
1247 }
1248 else
1249 {
1250 ccl->reg[0] = charset;
1251 ccl->reg[1] = char2b->byte1;
1252 ccl->reg[2] = char2b->byte2;
1253 }
1254
1255 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1256
1257 /* We assume that MSBs are appropriately set/reset by CCL
1258 program. */
1259 if (font->max_byte1 == 0) /* 1-byte font */
1260 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1261 else
1262 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1263 }
1264 else if (font_info->encoding[charset])
1265 {
1266 /* Fixed encoding scheme. See fontset.h for the meaning of the
1267 encoding numbers. */
1268 int enc = font_info->encoding[charset];
1269
1270 if ((enc == 1 || enc == 2)
1271 && CHARSET_DIMENSION (charset) == 2)
1272 char2b->byte1 |= 0x80;
1273
1274 if (enc == 1 || enc == 3)
1275 char2b->byte2 |= 0x80;
1276 }
1277 }
1278
1279
1280 /* Get face and two-byte form of character C in face FACE_ID on frame
1281 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1282 means we want to display multibyte text. Value is a pointer to a
1283 realized face that is ready for display. */
1284
1285 static INLINE struct face *
1286 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1287 struct frame *f;
1288 int c, face_id;
1289 XChar2b *char2b;
1290 int multibyte_p;
1291 {
1292 struct face *face = FACE_FROM_ID (f, face_id);
1293
1294 if (!multibyte_p)
1295 {
1296 /* Unibyte case. We don't have to encode, but we have to make
1297 sure to use a face suitable for unibyte. */
1298 char2b->byte1 = 0;
1299 char2b->byte2 = c;
1300 face_id = FACE_FOR_CHAR (f, face, c);
1301 face = FACE_FROM_ID (f, face_id);
1302 }
1303 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1304 {
1305 /* Case of ASCII in a face known to fit ASCII. */
1306 char2b->byte1 = 0;
1307 char2b->byte2 = c;
1308 }
1309 else
1310 {
1311 int c1, c2, charset;
1312
1313 /* Split characters into bytes. If c2 is -1 afterwards, C is
1314 really a one-byte character so that byte1 is zero. */
1315 SPLIT_CHAR (c, charset, c1, c2);
1316 if (c2 > 0)
1317 char2b->byte1 = c1, char2b->byte2 = c2;
1318 else
1319 char2b->byte1 = 0, char2b->byte2 = c1;
1320
1321 /* Maybe encode the character in *CHAR2B. */
1322 if (face->font != NULL)
1323 {
1324 struct font_info *font_info
1325 = FONT_INFO_FROM_ID (f, face->font_info_id);
1326 if (font_info)
1327 x_encode_char (c, char2b, font_info);
1328 }
1329 }
1330
1331 /* Make sure X resources of the face are allocated. */
1332 xassert (face != NULL);
1333 PREPARE_FACE_FOR_DISPLAY (f, face);
1334
1335 return face;
1336 }
1337
1338
1339 /* Get face and two-byte form of character glyph GLYPH on frame F.
1340 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1341 a pointer to a realized face that is ready for display. */
1342
1343 static INLINE struct face *
1344 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1345 struct frame *f;
1346 struct glyph *glyph;
1347 XChar2b *char2b;
1348 int *two_byte_p;
1349 {
1350 struct face *face;
1351
1352 xassert (glyph->type == CHAR_GLYPH);
1353 face = FACE_FROM_ID (f, glyph->face_id);
1354
1355 if (two_byte_p)
1356 *two_byte_p = 0;
1357
1358 if (!glyph->multibyte_p)
1359 {
1360 /* Unibyte case. We don't have to encode, but we have to make
1361 sure to use a face suitable for unibyte. */
1362 char2b->byte1 = 0;
1363 char2b->byte2 = glyph->u.ch;
1364 }
1365 else if (glyph->u.ch < 128
1366 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1367 {
1368 /* Case of ASCII in a face known to fit ASCII. */
1369 char2b->byte1 = 0;
1370 char2b->byte2 = glyph->u.ch;
1371 }
1372 else
1373 {
1374 int c1, c2, charset;
1375
1376 /* Split characters into bytes. If c2 is -1 afterwards, C is
1377 really a one-byte character so that byte1 is zero. */
1378 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1379 if (c2 > 0)
1380 char2b->byte1 = c1, char2b->byte2 = c2;
1381 else
1382 char2b->byte1 = 0, char2b->byte2 = c1;
1383
1384 /* Maybe encode the character in *CHAR2B. */
1385 if (charset != CHARSET_ASCII)
1386 {
1387 struct font_info *font_info
1388 = FONT_INFO_FROM_ID (f, face->font_info_id);
1389 if (font_info)
1390 {
1391 x_encode_char (glyph->u.ch, char2b, font_info);
1392 if (two_byte_p)
1393 *two_byte_p
1394 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1395 }
1396 }
1397 }
1398
1399 /* Make sure X resources of the face are allocated. */
1400 xassert (face != NULL);
1401 PREPARE_FACE_FOR_DISPLAY (f, face);
1402 return face;
1403 }
1404
1405
1406 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1407 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1408
1409 static INLINE void
1410 x_append_glyph (it)
1411 struct it *it;
1412 {
1413 struct glyph *glyph;
1414 enum glyph_row_area area = it->area;
1415
1416 xassert (it->glyph_row);
1417 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1418
1419 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1420 if (glyph < it->glyph_row->glyphs[area + 1])
1421 {
1422 glyph->charpos = CHARPOS (it->position);
1423 glyph->object = it->object;
1424 glyph->pixel_width = it->pixel_width;
1425 glyph->voffset = it->voffset;
1426 glyph->type = CHAR_GLYPH;
1427 glyph->multibyte_p = it->multibyte_p;
1428 glyph->left_box_line_p = it->start_of_box_run_p;
1429 glyph->right_box_line_p = it->end_of_box_run_p;
1430 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1431 || it->phys_descent > it->descent);
1432 glyph->padding_p = 0;
1433 glyph->glyph_not_available_p = it->glyph_not_available_p;
1434 glyph->face_id = it->face_id;
1435 glyph->u.ch = it->char_to_display;
1436 ++it->glyph_row->used[area];
1437 }
1438 }
1439
1440 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1441 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1442
1443 static INLINE void
1444 x_append_composite_glyph (it)
1445 struct it *it;
1446 {
1447 struct glyph *glyph;
1448 enum glyph_row_area area = it->area;
1449
1450 xassert (it->glyph_row);
1451
1452 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1453 if (glyph < it->glyph_row->glyphs[area + 1])
1454 {
1455 glyph->charpos = CHARPOS (it->position);
1456 glyph->object = it->object;
1457 glyph->pixel_width = it->pixel_width;
1458 glyph->voffset = it->voffset;
1459 glyph->type = COMPOSITE_GLYPH;
1460 glyph->multibyte_p = it->multibyte_p;
1461 glyph->left_box_line_p = it->start_of_box_run_p;
1462 glyph->right_box_line_p = it->end_of_box_run_p;
1463 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1464 || it->phys_descent > it->descent);
1465 glyph->padding_p = 0;
1466 glyph->glyph_not_available_p = 0;
1467 glyph->face_id = it->face_id;
1468 glyph->u.cmp_id = it->cmp_id;
1469 ++it->glyph_row->used[area];
1470 }
1471 }
1472
1473
1474 /* Change IT->ascent and IT->height according to the setting of
1475 IT->voffset. */
1476
1477 static INLINE void
1478 take_vertical_position_into_account (it)
1479 struct it *it;
1480 {
1481 if (it->voffset)
1482 {
1483 if (it->voffset < 0)
1484 /* Increase the ascent so that we can display the text higher
1485 in the line. */
1486 it->ascent += abs (it->voffset);
1487 else
1488 /* Increase the descent so that we can display the text lower
1489 in the line. */
1490 it->descent += it->voffset;
1491 }
1492 }
1493
1494
1495 /* Produce glyphs/get display metrics for the image IT is loaded with.
1496 See the description of struct display_iterator in dispextern.h for
1497 an overview of struct display_iterator. */
1498
1499 static void
1500 x_produce_image_glyph (it)
1501 struct it *it;
1502 {
1503 struct image *img;
1504 struct face *face;
1505
1506 xassert (it->what == IT_IMAGE);
1507
1508 face = FACE_FROM_ID (it->f, it->face_id);
1509 img = IMAGE_FROM_ID (it->f, it->image_id);
1510 xassert (img);
1511
1512 /* Make sure X resources of the face and image are loaded. */
1513 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1514 prepare_image_for_display (it->f, img);
1515
1516 it->ascent = it->phys_ascent = image_ascent (img, face);
1517 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1518 it->pixel_width = img->width + 2 * img->hmargin;
1519
1520 it->nglyphs = 1;
1521
1522 if (face->box != FACE_NO_BOX)
1523 {
1524 if (face->box_line_width > 0)
1525 {
1526 it->ascent += face->box_line_width;
1527 it->descent += face->box_line_width;
1528 }
1529
1530 if (it->start_of_box_run_p)
1531 it->pixel_width += abs (face->box_line_width);
1532 if (it->end_of_box_run_p)
1533 it->pixel_width += abs (face->box_line_width);
1534 }
1535
1536 take_vertical_position_into_account (it);
1537
1538 if (it->glyph_row)
1539 {
1540 struct glyph *glyph;
1541 enum glyph_row_area area = it->area;
1542
1543 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1544 if (glyph < it->glyph_row->glyphs[area + 1])
1545 {
1546 glyph->charpos = CHARPOS (it->position);
1547 glyph->object = it->object;
1548 glyph->pixel_width = it->pixel_width;
1549 glyph->voffset = it->voffset;
1550 glyph->type = IMAGE_GLYPH;
1551 glyph->multibyte_p = it->multibyte_p;
1552 glyph->left_box_line_p = it->start_of_box_run_p;
1553 glyph->right_box_line_p = it->end_of_box_run_p;
1554 glyph->overlaps_vertically_p = 0;
1555 glyph->padding_p = 0;
1556 glyph->glyph_not_available_p = 0;
1557 glyph->face_id = it->face_id;
1558 glyph->u.img_id = img->id;
1559 ++it->glyph_row->used[area];
1560 }
1561 }
1562 }
1563
1564
1565 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1566 of the glyph, WIDTH and HEIGHT are the width and height of the
1567 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1568 ascent of the glyph (0 <= ASCENT <= 1). */
1569
1570 static void
1571 x_append_stretch_glyph (it, object, width, height, ascent)
1572 struct it *it;
1573 Lisp_Object object;
1574 int width, height;
1575 double ascent;
1576 {
1577 struct glyph *glyph;
1578 enum glyph_row_area area = it->area;
1579
1580 xassert (ascent >= 0 && ascent <= 1);
1581
1582 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1583 if (glyph < it->glyph_row->glyphs[area + 1])
1584 {
1585 glyph->charpos = CHARPOS (it->position);
1586 glyph->object = object;
1587 glyph->pixel_width = width;
1588 glyph->voffset = it->voffset;
1589 glyph->type = STRETCH_GLYPH;
1590 glyph->multibyte_p = it->multibyte_p;
1591 glyph->left_box_line_p = it->start_of_box_run_p;
1592 glyph->right_box_line_p = it->end_of_box_run_p;
1593 glyph->overlaps_vertically_p = 0;
1594 glyph->padding_p = 0;
1595 glyph->glyph_not_available_p = 0;
1596 glyph->face_id = it->face_id;
1597 glyph->u.stretch.ascent = height * ascent;
1598 glyph->u.stretch.height = height;
1599 ++it->glyph_row->used[area];
1600 }
1601 }
1602
1603
1604 /* Produce a stretch glyph for iterator IT. IT->object is the value
1605 of the glyph property displayed. The value must be a list
1606 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1607 being recognized:
1608
1609 1. `:width WIDTH' specifies that the space should be WIDTH *
1610 canonical char width wide. WIDTH may be an integer or floating
1611 point number.
1612
1613 2. `:relative-width FACTOR' specifies that the width of the stretch
1614 should be computed from the width of the first character having the
1615 `glyph' property, and should be FACTOR times that width.
1616
1617 3. `:align-to HPOS' specifies that the space should be wide enough
1618 to reach HPOS, a value in canonical character units.
1619
1620 Exactly one of the above pairs must be present.
1621
1622 4. `:height HEIGHT' specifies that the height of the stretch produced
1623 should be HEIGHT, measured in canonical character units.
1624
1625 5. `:relative-height FACTOR' specifies that the height of the the
1626 stretch should be FACTOR times the height of the characters having
1627 the glyph property.
1628
1629 Either none or exactly one of 4 or 5 must be present.
1630
1631 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1632 of the stretch should be used for the ascent of the stretch.
1633 ASCENT must be in the range 0 <= ASCENT <= 100. */
1634
1635 #define NUMVAL(X) \
1636 ((INTEGERP (X) || FLOATP (X)) \
1637 ? XFLOATINT (X) \
1638 : - 1)
1639
1640
1641 static void
1642 x_produce_stretch_glyph (it)
1643 struct it *it;
1644 {
1645 /* (space :width WIDTH :height HEIGHT. */
1646 #if GLYPH_DEBUG
1647 extern Lisp_Object Qspace;
1648 #endif
1649 extern Lisp_Object QCwidth, QCheight, QCascent;
1650 extern Lisp_Object QCrelative_width, QCrelative_height;
1651 extern Lisp_Object QCalign_to;
1652 Lisp_Object prop, plist;
1653 double width = 0, height = 0, ascent = 0;
1654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1655 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1656
1657 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1658
1659 /* List should start with `space'. */
1660 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1661 plist = XCDR (it->object);
1662
1663 /* Compute the width of the stretch. */
1664 if (prop = Fplist_get (plist, QCwidth),
1665 NUMVAL (prop) > 0)
1666 /* Absolute width `:width WIDTH' specified and valid. */
1667 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1668 else if (prop = Fplist_get (plist, QCrelative_width),
1669 NUMVAL (prop) > 0)
1670 {
1671 /* Relative width `:relative-width FACTOR' specified and valid.
1672 Compute the width of the characters having the `glyph'
1673 property. */
1674 struct it it2;
1675 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1676
1677 it2 = *it;
1678 if (it->multibyte_p)
1679 {
1680 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1681 - IT_BYTEPOS (*it));
1682 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1683 }
1684 else
1685 it2.c = *p, it2.len = 1;
1686
1687 it2.glyph_row = NULL;
1688 it2.what = IT_CHARACTER;
1689 x_produce_glyphs (&it2);
1690 width = NUMVAL (prop) * it2.pixel_width;
1691 }
1692 else if (prop = Fplist_get (plist, QCalign_to),
1693 NUMVAL (prop) > 0)
1694 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1695 else
1696 /* Nothing specified -> width defaults to canonical char width. */
1697 width = CANON_X_UNIT (it->f);
1698
1699 /* Compute height. */
1700 if (prop = Fplist_get (plist, QCheight),
1701 NUMVAL (prop) > 0)
1702 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1703 else if (prop = Fplist_get (plist, QCrelative_height),
1704 NUMVAL (prop) > 0)
1705 height = FONT_HEIGHT (font) * NUMVAL (prop);
1706 else
1707 height = FONT_HEIGHT (font);
1708
1709 /* Compute percentage of height used for ascent. If
1710 `:ascent ASCENT' is present and valid, use that. Otherwise,
1711 derive the ascent from the font in use. */
1712 if (prop = Fplist_get (plist, QCascent),
1713 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1714 ascent = NUMVAL (prop) / 100.0;
1715 else
1716 ascent = (double) font->ascent / FONT_HEIGHT (font);
1717
1718 if (width <= 0)
1719 width = 1;
1720 if (height <= 0)
1721 height = 1;
1722
1723 if (it->glyph_row)
1724 {
1725 Lisp_Object object = it->stack[it->sp - 1].string;
1726 if (!STRINGP (object))
1727 object = it->w->buffer;
1728 x_append_stretch_glyph (it, object, width, height, ascent);
1729 }
1730
1731 it->pixel_width = width;
1732 it->ascent = it->phys_ascent = height * ascent;
1733 it->descent = it->phys_descent = height - it->ascent;
1734 it->nglyphs = 1;
1735
1736 if (face->box != FACE_NO_BOX)
1737 {
1738 if (face->box_line_width > 0)
1739 {
1740 it->ascent += face->box_line_width;
1741 it->descent += face->box_line_width;
1742 }
1743
1744 if (it->start_of_box_run_p)
1745 it->pixel_width += abs (face->box_line_width);
1746 if (it->end_of_box_run_p)
1747 it->pixel_width += abs (face->box_line_width);
1748 }
1749
1750 take_vertical_position_into_account (it);
1751 }
1752
1753 /* Return proper value to be used as baseline offset of font that has
1754 ASCENT and DESCENT to draw characters by the font at the vertical
1755 center of the line of frame F.
1756
1757 Here, out task is to find the value of BOFF in the following figure;
1758
1759 -------------------------+-----------+-
1760 -+-+---------+-+ | |
1761 | | | | | |
1762 | | | | F_ASCENT F_HEIGHT
1763 | | | ASCENT | |
1764 HEIGHT | | | | |
1765 | | |-|-+------+-----------|------- baseline
1766 | | | | BOFF | |
1767 | |---------|-+-+ | |
1768 | | | DESCENT | |
1769 -+-+---------+-+ F_DESCENT |
1770 -------------------------+-----------+-
1771
1772 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1773 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1774 DESCENT = FONT->descent
1775 HEIGHT = FONT_HEIGHT (FONT)
1776 F_DESCENT = (F->output_data.x->font->descent
1777 - F->output_data.x->baseline_offset)
1778 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1779 */
1780
1781 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1782 ((FONT)->descent \
1783 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1784 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1785 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1786
1787 /* Produce glyphs/get display metrics for the display element IT is
1788 loaded with. See the description of struct display_iterator in
1789 dispextern.h for an overview of struct display_iterator. */
1790
1791 static void
1792 x_produce_glyphs (it)
1793 struct it *it;
1794 {
1795 it->glyph_not_available_p = 0;
1796
1797 if (it->what == IT_CHARACTER)
1798 {
1799 XChar2b char2b;
1800 XFontStruct *font;
1801 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1802 XCharStruct *pcm;
1803 int font_not_found_p;
1804 struct font_info *font_info;
1805 int boff; /* baseline offset */
1806 /* We may change it->multibyte_p upon unibyte<->multibyte
1807 conversion. So, save the current value now and restore it
1808 later.
1809
1810 Note: It seems that we don't have to record multibyte_p in
1811 struct glyph because the character code itself tells if or
1812 not the character is multibyte. Thus, in the future, we must
1813 consider eliminating the field `multibyte_p' in the struct
1814 glyph.
1815 */
1816 int saved_multibyte_p = it->multibyte_p;
1817
1818 /* Maybe translate single-byte characters to multibyte, or the
1819 other way. */
1820 it->char_to_display = it->c;
1821 if (!ASCII_BYTE_P (it->c))
1822 {
1823 if (unibyte_display_via_language_environment
1824 && SINGLE_BYTE_CHAR_P (it->c)
1825 && (it->c >= 0240
1826 || !NILP (Vnonascii_translation_table)))
1827 {
1828 it->char_to_display = unibyte_char_to_multibyte (it->c);
1829 it->multibyte_p = 1;
1830 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1831 face = FACE_FROM_ID (it->f, it->face_id);
1832 }
1833 else if (!SINGLE_BYTE_CHAR_P (it->c)
1834 && !it->multibyte_p)
1835 {
1836 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1837 it->multibyte_p = 0;
1838 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1839 face = FACE_FROM_ID (it->f, it->face_id);
1840 }
1841 }
1842
1843 /* Get font to use. Encode IT->char_to_display. */
1844 x_get_char_face_and_encoding (it->f, it->char_to_display,
1845 it->face_id, &char2b,
1846 it->multibyte_p);
1847 font = face->font;
1848
1849 /* When no suitable font found, use the default font. */
1850 font_not_found_p = font == NULL;
1851 if (font_not_found_p)
1852 {
1853 font = FRAME_FONT (it->f);
1854 boff = it->f->output_data.x->baseline_offset;
1855 font_info = NULL;
1856 }
1857 else
1858 {
1859 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1860 boff = font_info->baseline_offset;
1861 if (font_info->vertical_centering)
1862 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1863 }
1864
1865 if (it->char_to_display >= ' '
1866 && (!it->multibyte_p || it->char_to_display < 128))
1867 {
1868 /* Either unibyte or ASCII. */
1869 int stretched_p;
1870
1871 it->nglyphs = 1;
1872
1873 pcm = x_per_char_metric (font, &char2b);
1874 it->ascent = font->ascent + boff;
1875 it->descent = font->descent - boff;
1876
1877 if (pcm)
1878 {
1879 it->phys_ascent = pcm->ascent + boff;
1880 it->phys_descent = pcm->descent - boff;
1881 it->pixel_width = pcm->width;
1882 }
1883 else
1884 {
1885 it->glyph_not_available_p = 1;
1886 it->phys_ascent = font->ascent + boff;
1887 it->phys_descent = font->descent - boff;
1888 it->pixel_width = FONT_WIDTH (font);
1889 }
1890
1891 /* If this is a space inside a region of text with
1892 `space-width' property, change its width. */
1893 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1894 if (stretched_p)
1895 it->pixel_width *= XFLOATINT (it->space_width);
1896
1897 /* If face has a box, add the box thickness to the character
1898 height. If character has a box line to the left and/or
1899 right, add the box line width to the character's width. */
1900 if (face->box != FACE_NO_BOX)
1901 {
1902 int thick = face->box_line_width;
1903
1904 if (thick > 0)
1905 {
1906 it->ascent += thick;
1907 it->descent += thick;
1908 }
1909 else
1910 thick = -thick;
1911
1912 if (it->start_of_box_run_p)
1913 it->pixel_width += thick;
1914 if (it->end_of_box_run_p)
1915 it->pixel_width += thick;
1916 }
1917
1918 /* If face has an overline, add the height of the overline
1919 (1 pixel) and a 1 pixel margin to the character height. */
1920 if (face->overline_p)
1921 it->ascent += 2;
1922
1923 take_vertical_position_into_account (it);
1924
1925 /* If we have to actually produce glyphs, do it. */
1926 if (it->glyph_row)
1927 {
1928 if (stretched_p)
1929 {
1930 /* Translate a space with a `space-width' property
1931 into a stretch glyph. */
1932 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1933 x_append_stretch_glyph (it, it->object, it->pixel_width,
1934 it->ascent + it->descent, ascent);
1935 }
1936 else
1937 x_append_glyph (it);
1938
1939 /* If characters with lbearing or rbearing are displayed
1940 in this line, record that fact in a flag of the
1941 glyph row. This is used to optimize X output code. */
1942 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1943 it->glyph_row->contains_overlapping_glyphs_p = 1;
1944 }
1945 }
1946 else if (it->char_to_display == '\n')
1947 {
1948 /* A newline has no width but we need the height of the line. */
1949 it->pixel_width = 0;
1950 it->nglyphs = 0;
1951 it->ascent = it->phys_ascent = font->ascent + boff;
1952 it->descent = it->phys_descent = font->descent - boff;
1953
1954 if (face->box != FACE_NO_BOX
1955 && face->box_line_width > 0)
1956 {
1957 it->ascent += face->box_line_width;
1958 it->descent += face->box_line_width;
1959 }
1960 }
1961 else if (it->char_to_display == '\t')
1962 {
1963 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1964 int x = it->current_x + it->continuation_lines_width;
1965 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1966
1967 /* If the distance from the current position to the next tab
1968 stop is less than a canonical character width, use the
1969 tab stop after that. */
1970 if (next_tab_x - x < CANON_X_UNIT (it->f))
1971 next_tab_x += tab_width;
1972
1973 it->pixel_width = next_tab_x - x;
1974 it->nglyphs = 1;
1975 it->ascent = it->phys_ascent = font->ascent + boff;
1976 it->descent = it->phys_descent = font->descent - boff;
1977
1978 if (it->glyph_row)
1979 {
1980 double ascent = (double) it->ascent / (it->ascent + it->descent);
1981 x_append_stretch_glyph (it, it->object, it->pixel_width,
1982 it->ascent + it->descent, ascent);
1983 }
1984 }
1985 else
1986 {
1987 /* A multi-byte character. Assume that the display width of the
1988 character is the width of the character multiplied by the
1989 width of the font. */
1990
1991 /* If we found a font, this font should give us the right
1992 metrics. If we didn't find a font, use the frame's
1993 default font and calculate the width of the character
1994 from the charset width; this is what old redisplay code
1995 did. */
1996 pcm = x_per_char_metric (font, &char2b);
1997 if (font_not_found_p || !pcm)
1998 {
1999 int charset = CHAR_CHARSET (it->char_to_display);
2000
2001 it->glyph_not_available_p = 1;
2002 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2003 * CHARSET_WIDTH (charset));
2004 it->phys_ascent = font->ascent + boff;
2005 it->phys_descent = font->descent - boff;
2006 }
2007 else
2008 {
2009 it->pixel_width = pcm->width;
2010 it->phys_ascent = pcm->ascent + boff;
2011 it->phys_descent = pcm->descent - boff;
2012 if (it->glyph_row
2013 && (pcm->lbearing < 0
2014 || pcm->rbearing > pcm->width))
2015 it->glyph_row->contains_overlapping_glyphs_p = 1;
2016 }
2017 it->nglyphs = 1;
2018 it->ascent = font->ascent + boff;
2019 it->descent = font->descent - boff;
2020 if (face->box != FACE_NO_BOX)
2021 {
2022 int thick = face->box_line_width;
2023
2024 if (thick > 0)
2025 {
2026 it->ascent += thick;
2027 it->descent += thick;
2028 }
2029 else
2030 thick = - thick;
2031
2032 if (it->start_of_box_run_p)
2033 it->pixel_width += thick;
2034 if (it->end_of_box_run_p)
2035 it->pixel_width += thick;
2036 }
2037
2038 /* If face has an overline, add the height of the overline
2039 (1 pixel) and a 1 pixel margin to the character height. */
2040 if (face->overline_p)
2041 it->ascent += 2;
2042
2043 take_vertical_position_into_account (it);
2044
2045 if (it->glyph_row)
2046 x_append_glyph (it);
2047 }
2048 it->multibyte_p = saved_multibyte_p;
2049 }
2050 else if (it->what == IT_COMPOSITION)
2051 {
2052 /* Note: A composition is represented as one glyph in the
2053 glyph matrix. There are no padding glyphs. */
2054 XChar2b char2b;
2055 XFontStruct *font;
2056 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2057 XCharStruct *pcm;
2058 int font_not_found_p;
2059 struct font_info *font_info;
2060 int boff; /* baseline offset */
2061 struct composition *cmp = composition_table[it->cmp_id];
2062
2063 /* Maybe translate single-byte characters to multibyte. */
2064 it->char_to_display = it->c;
2065 if (unibyte_display_via_language_environment
2066 && SINGLE_BYTE_CHAR_P (it->c)
2067 && (it->c >= 0240
2068 || (it->c >= 0200
2069 && !NILP (Vnonascii_translation_table))))
2070 {
2071 it->char_to_display = unibyte_char_to_multibyte (it->c);
2072 }
2073
2074 /* Get face and font to use. Encode IT->char_to_display. */
2075 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2076 face = FACE_FROM_ID (it->f, it->face_id);
2077 x_get_char_face_and_encoding (it->f, it->char_to_display,
2078 it->face_id, &char2b, it->multibyte_p);
2079 font = face->font;
2080
2081 /* When no suitable font found, use the default font. */
2082 font_not_found_p = font == NULL;
2083 if (font_not_found_p)
2084 {
2085 font = FRAME_FONT (it->f);
2086 boff = it->f->output_data.x->baseline_offset;
2087 font_info = NULL;
2088 }
2089 else
2090 {
2091 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2092 boff = font_info->baseline_offset;
2093 if (font_info->vertical_centering)
2094 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2095 }
2096
2097 /* There are no padding glyphs, so there is only one glyph to
2098 produce for the composition. Important is that pixel_width,
2099 ascent and descent are the values of what is drawn by
2100 draw_glyphs (i.e. the values of the overall glyphs composed). */
2101 it->nglyphs = 1;
2102
2103 /* If we have not yet calculated pixel size data of glyphs of
2104 the composition for the current face font, calculate them
2105 now. Theoretically, we have to check all fonts for the
2106 glyphs, but that requires much time and memory space. So,
2107 here we check only the font of the first glyph. This leads
2108 to incorrect display very rarely, and C-l (recenter) can
2109 correct the display anyway. */
2110 if (cmp->font != (void *) font)
2111 {
2112 /* Ascent and descent of the font of the first character of
2113 this composition (adjusted by baseline offset). Ascent
2114 and descent of overall glyphs should not be less than
2115 them respectively. */
2116 int font_ascent = font->ascent + boff;
2117 int font_descent = font->descent - boff;
2118 /* Bounding box of the overall glyphs. */
2119 int leftmost, rightmost, lowest, highest;
2120 int i, width, ascent, descent;
2121
2122 cmp->font = (void *) font;
2123
2124 /* Initialize the bounding box. */
2125 if (font_info
2126 && (pcm = x_per_char_metric (font, &char2b)))
2127 {
2128 width = pcm->width;
2129 ascent = pcm->ascent;
2130 descent = pcm->descent;
2131 }
2132 else
2133 {
2134 width = FONT_WIDTH (font);
2135 ascent = font->ascent;
2136 descent = font->descent;
2137 }
2138
2139 rightmost = width;
2140 lowest = - descent + boff;
2141 highest = ascent + boff;
2142 leftmost = 0;
2143
2144 if (font_info
2145 && font_info->default_ascent
2146 && CHAR_TABLE_P (Vuse_default_ascent)
2147 && !NILP (Faref (Vuse_default_ascent,
2148 make_number (it->char_to_display))))
2149 highest = font_info->default_ascent + boff;
2150
2151 /* Draw the first glyph at the normal position. It may be
2152 shifted to right later if some other glyphs are drawn at
2153 the left. */
2154 cmp->offsets[0] = 0;
2155 cmp->offsets[1] = boff;
2156
2157 /* Set cmp->offsets for the remaining glyphs. */
2158 for (i = 1; i < cmp->glyph_len; i++)
2159 {
2160 int left, right, btm, top;
2161 int ch = COMPOSITION_GLYPH (cmp, i);
2162 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2163
2164 face = FACE_FROM_ID (it->f, face_id);
2165 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2166 it->multibyte_p);
2167 font = face->font;
2168 if (font == NULL)
2169 {
2170 font = FRAME_FONT (it->f);
2171 boff = it->f->output_data.x->baseline_offset;
2172 font_info = NULL;
2173 }
2174 else
2175 {
2176 font_info
2177 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2178 boff = font_info->baseline_offset;
2179 if (font_info->vertical_centering)
2180 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2181 }
2182
2183 if (font_info
2184 && (pcm = x_per_char_metric (font, &char2b)))
2185 {
2186 width = pcm->width;
2187 ascent = pcm->ascent;
2188 descent = pcm->descent;
2189 }
2190 else
2191 {
2192 width = FONT_WIDTH (font);
2193 ascent = 1;
2194 descent = 0;
2195 }
2196
2197 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2198 {
2199 /* Relative composition with or without
2200 alternate chars. */
2201 left = (leftmost + rightmost - width) / 2;
2202 btm = - descent + boff;
2203 if (font_info && font_info->relative_compose
2204 && (! CHAR_TABLE_P (Vignore_relative_composition)
2205 || NILP (Faref (Vignore_relative_composition,
2206 make_number (ch)))))
2207 {
2208
2209 if (- descent >= font_info->relative_compose)
2210 /* One extra pixel between two glyphs. */
2211 btm = highest + 1;
2212 else if (ascent <= 0)
2213 /* One extra pixel between two glyphs. */
2214 btm = lowest - 1 - ascent - descent;
2215 }
2216 }
2217 else
2218 {
2219 /* A composition rule is specified by an integer
2220 value that encodes global and new reference
2221 points (GREF and NREF). GREF and NREF are
2222 specified by numbers as below:
2223
2224 0---1---2 -- ascent
2225 | |
2226 | |
2227 | |
2228 9--10--11 -- center
2229 | |
2230 ---3---4---5--- baseline
2231 | |
2232 6---7---8 -- descent
2233 */
2234 int rule = COMPOSITION_RULE (cmp, i);
2235 int gref, nref, grefx, grefy, nrefx, nrefy;
2236
2237 COMPOSITION_DECODE_RULE (rule, gref, nref);
2238 grefx = gref % 3, nrefx = nref % 3;
2239 grefy = gref / 3, nrefy = nref / 3;
2240
2241 left = (leftmost
2242 + grefx * (rightmost - leftmost) / 2
2243 - nrefx * width / 2);
2244 btm = ((grefy == 0 ? highest
2245 : grefy == 1 ? 0
2246 : grefy == 2 ? lowest
2247 : (highest + lowest) / 2)
2248 - (nrefy == 0 ? ascent + descent
2249 : nrefy == 1 ? descent - boff
2250 : nrefy == 2 ? 0
2251 : (ascent + descent) / 2));
2252 }
2253
2254 cmp->offsets[i * 2] = left;
2255 cmp->offsets[i * 2 + 1] = btm + descent;
2256
2257 /* Update the bounding box of the overall glyphs. */
2258 right = left + width;
2259 top = btm + descent + ascent;
2260 if (left < leftmost)
2261 leftmost = left;
2262 if (right > rightmost)
2263 rightmost = right;
2264 if (top > highest)
2265 highest = top;
2266 if (btm < lowest)
2267 lowest = btm;
2268 }
2269
2270 /* If there are glyphs whose x-offsets are negative,
2271 shift all glyphs to the right and make all x-offsets
2272 non-negative. */
2273 if (leftmost < 0)
2274 {
2275 for (i = 0; i < cmp->glyph_len; i++)
2276 cmp->offsets[i * 2] -= leftmost;
2277 rightmost -= leftmost;
2278 }
2279
2280 cmp->pixel_width = rightmost;
2281 cmp->ascent = highest;
2282 cmp->descent = - lowest;
2283 if (cmp->ascent < font_ascent)
2284 cmp->ascent = font_ascent;
2285 if (cmp->descent < font_descent)
2286 cmp->descent = font_descent;
2287 }
2288
2289 it->pixel_width = cmp->pixel_width;
2290 it->ascent = it->phys_ascent = cmp->ascent;
2291 it->descent = it->phys_descent = cmp->descent;
2292
2293 if (face->box != FACE_NO_BOX)
2294 {
2295 int thick = face->box_line_width;
2296
2297 if (thick > 0)
2298 {
2299 it->ascent += thick;
2300 it->descent += thick;
2301 }
2302 else
2303 thick = - thick;
2304
2305 if (it->start_of_box_run_p)
2306 it->pixel_width += thick;
2307 if (it->end_of_box_run_p)
2308 it->pixel_width += thick;
2309 }
2310
2311 /* If face has an overline, add the height of the overline
2312 (1 pixel) and a 1 pixel margin to the character height. */
2313 if (face->overline_p)
2314 it->ascent += 2;
2315
2316 take_vertical_position_into_account (it);
2317
2318 if (it->glyph_row)
2319 x_append_composite_glyph (it);
2320 }
2321 else if (it->what == IT_IMAGE)
2322 x_produce_image_glyph (it);
2323 else if (it->what == IT_STRETCH)
2324 x_produce_stretch_glyph (it);
2325
2326 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2327 because this isn't true for images with `:ascent 100'. */
2328 xassert (it->ascent >= 0 && it->descent >= 0);
2329 if (it->area == TEXT_AREA)
2330 it->current_x += it->pixel_width;
2331
2332 it->descent += it->extra_line_spacing;
2333
2334 it->max_ascent = max (it->max_ascent, it->ascent);
2335 it->max_descent = max (it->max_descent, it->descent);
2336 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2337 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2338 }
2339
2340
2341 /* Estimate the pixel height of the mode or top line on frame F.
2342 FACE_ID specifies what line's height to estimate. */
2343
2344 int
2345 x_estimate_mode_line_height (f, face_id)
2346 struct frame *f;
2347 enum face_id face_id;
2348 {
2349 int height = FONT_HEIGHT (FRAME_FONT (f));
2350
2351 /* This function is called so early when Emacs starts that the face
2352 cache and mode line face are not yet initialized. */
2353 if (FRAME_FACE_CACHE (f))
2354 {
2355 struct face *face = FACE_FROM_ID (f, face_id);
2356 if (face)
2357 {
2358 if (face->font)
2359 height = FONT_HEIGHT (face->font);
2360 if (face->box_line_width > 0)
2361 height += 2 * face->box_line_width;
2362 }
2363 }
2364
2365 return height;
2366 }
2367
2368 \f
2369 /***********************************************************************
2370 Glyph display
2371 ***********************************************************************/
2372
2373 /* A sequence of glyphs to be drawn in the same face.
2374
2375 This data structure is not really completely X specific, so it
2376 could possibly, at least partially, be useful for other systems. It
2377 is currently not part of the external redisplay interface because
2378 it's not clear what other systems will need. */
2379
2380 struct glyph_string
2381 {
2382 /* X-origin of the string. */
2383 int x;
2384
2385 /* Y-origin and y-position of the base line of this string. */
2386 int y, ybase;
2387
2388 /* The width of the string, not including a face extension. */
2389 int width;
2390
2391 /* The width of the string, including a face extension. */
2392 int background_width;
2393
2394 /* The height of this string. This is the height of the line this
2395 string is drawn in, and can be different from the height of the
2396 font the string is drawn in. */
2397 int height;
2398
2399 /* Number of pixels this string overwrites in front of its x-origin.
2400 This number is zero if the string has an lbearing >= 0; it is
2401 -lbearing, if the string has an lbearing < 0. */
2402 int left_overhang;
2403
2404 /* Number of pixels this string overwrites past its right-most
2405 nominal x-position, i.e. x + width. Zero if the string's
2406 rbearing is <= its nominal width, rbearing - width otherwise. */
2407 int right_overhang;
2408
2409 /* The frame on which the glyph string is drawn. */
2410 struct frame *f;
2411
2412 /* The window on which the glyph string is drawn. */
2413 struct window *w;
2414
2415 /* X display and window for convenience. */
2416 Display *display;
2417 Window window;
2418
2419 /* The glyph row for which this string was built. It determines the
2420 y-origin and height of the string. */
2421 struct glyph_row *row;
2422
2423 /* The area within row. */
2424 enum glyph_row_area area;
2425
2426 /* Characters to be drawn, and number of characters. */
2427 XChar2b *char2b;
2428 int nchars;
2429
2430 /* A face-override for drawing cursors, mouse face and similar. */
2431 enum draw_glyphs_face hl;
2432
2433 /* Face in which this string is to be drawn. */
2434 struct face *face;
2435
2436 /* Font in which this string is to be drawn. */
2437 XFontStruct *font;
2438
2439 /* Font info for this string. */
2440 struct font_info *font_info;
2441
2442 /* Non-null means this string describes (part of) a composition.
2443 All characters from char2b are drawn composed. */
2444 struct composition *cmp;
2445
2446 /* Index of this glyph string's first character in the glyph
2447 definition of CMP. If this is zero, this glyph string describes
2448 the first character of a composition. */
2449 int gidx;
2450
2451 /* 1 means this glyph strings face has to be drawn to the right end
2452 of the window's drawing area. */
2453 unsigned extends_to_end_of_line_p : 1;
2454
2455 /* 1 means the background of this string has been drawn. */
2456 unsigned background_filled_p : 1;
2457
2458 /* 1 means glyph string must be drawn with 16-bit functions. */
2459 unsigned two_byte_p : 1;
2460
2461 /* 1 means that the original font determined for drawing this glyph
2462 string could not be loaded. The member `font' has been set to
2463 the frame's default font in this case. */
2464 unsigned font_not_found_p : 1;
2465
2466 /* 1 means that the face in which this glyph string is drawn has a
2467 stipple pattern. */
2468 unsigned stippled_p : 1;
2469
2470 /* 1 means only the foreground of this glyph string must be drawn,
2471 and we should use the physical height of the line this glyph
2472 string appears in as clip rect. */
2473 unsigned for_overlaps_p : 1;
2474
2475 /* The GC to use for drawing this glyph string. */
2476 GC gc;
2477
2478 /* A pointer to the first glyph in the string. This glyph
2479 corresponds to char2b[0]. Needed to draw rectangles if
2480 font_not_found_p is 1. */
2481 struct glyph *first_glyph;
2482
2483 /* Image, if any. */
2484 struct image *img;
2485
2486 struct glyph_string *next, *prev;
2487 };
2488
2489
2490 #if 1
2491
2492 static void
2493 x_dump_glyph_string (s)
2494 struct glyph_string *s;
2495 {
2496 fprintf (stderr, "glyph string\n");
2497 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2498 s->x, s->y, s->width, s->height);
2499 fprintf (stderr, " ybase = %d\n", s->ybase);
2500 fprintf (stderr, " hl = %d\n", s->hl);
2501 fprintf (stderr, " left overhang = %d, right = %d\n",
2502 s->left_overhang, s->right_overhang);
2503 fprintf (stderr, " nchars = %d\n", s->nchars);
2504 fprintf (stderr, " extends to end of line = %d\n",
2505 s->extends_to_end_of_line_p);
2506 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2507 fprintf (stderr, " bg width = %d\n", s->background_width);
2508 }
2509
2510 #endif /* GLYPH_DEBUG */
2511
2512
2513
2514 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2515 struct glyph_string **,
2516 struct glyph_string *,
2517 struct glyph_string *));
2518 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2519 struct glyph_string **,
2520 struct glyph_string *,
2521 struct glyph_string *));
2522 static void x_append_glyph_string P_ ((struct glyph_string **,
2523 struct glyph_string **,
2524 struct glyph_string *));
2525 static int x_left_overwritten P_ ((struct glyph_string *));
2526 static int x_left_overwriting P_ ((struct glyph_string *));
2527 static int x_right_overwritten P_ ((struct glyph_string *));
2528 static int x_right_overwriting P_ ((struct glyph_string *));
2529 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2530 int));
2531 static void x_init_glyph_string P_ ((struct glyph_string *,
2532 XChar2b *, struct window *,
2533 struct glyph_row *,
2534 enum glyph_row_area, int,
2535 enum draw_glyphs_face));
2536 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2537 enum glyph_row_area, int, int,
2538 enum draw_glyphs_face, int *, int *, int));
2539 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2540 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2541 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2542 int));
2543 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2544 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2545 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2546 static void x_draw_glyph_string P_ ((struct glyph_string *));
2547 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2548 static void x_set_cursor_gc P_ ((struct glyph_string *));
2549 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2550 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2551 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2552 int *, int *));
2553 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2554 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2555 unsigned long *, double, int));
2556 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2557 double, int, unsigned long));
2558 static void x_setup_relief_colors P_ ((struct glyph_string *));
2559 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2560 static void x_draw_image_relief P_ ((struct glyph_string *));
2561 static void x_draw_image_foreground P_ ((struct glyph_string *));
2562 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2563 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2564 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2565 int, int, int));
2566 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2567 int, int, int, int, XRectangle *));
2568 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2569 int, int, int, XRectangle *));
2570 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2571 enum glyph_row_area));
2572 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2573 struct glyph_row *,
2574 enum glyph_row_area, int, int));
2575
2576 #if GLYPH_DEBUG
2577 static void x_check_font P_ ((struct frame *, XFontStruct *));
2578 #endif
2579
2580
2581 /* Append the list of glyph strings with head H and tail T to the list
2582 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2583
2584 static INLINE void
2585 x_append_glyph_string_lists (head, tail, h, t)
2586 struct glyph_string **head, **tail;
2587 struct glyph_string *h, *t;
2588 {
2589 if (h)
2590 {
2591 if (*head)
2592 (*tail)->next = h;
2593 else
2594 *head = h;
2595 h->prev = *tail;
2596 *tail = t;
2597 }
2598 }
2599
2600
2601 /* Prepend the list of glyph strings with head H and tail T to the
2602 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2603 result. */
2604
2605 static INLINE void
2606 x_prepend_glyph_string_lists (head, tail, h, t)
2607 struct glyph_string **head, **tail;
2608 struct glyph_string *h, *t;
2609 {
2610 if (h)
2611 {
2612 if (*head)
2613 (*head)->prev = t;
2614 else
2615 *tail = t;
2616 t->next = *head;
2617 *head = h;
2618 }
2619 }
2620
2621
2622 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2623 Set *HEAD and *TAIL to the resulting list. */
2624
2625 static INLINE void
2626 x_append_glyph_string (head, tail, s)
2627 struct glyph_string **head, **tail;
2628 struct glyph_string *s;
2629 {
2630 s->next = s->prev = NULL;
2631 x_append_glyph_string_lists (head, tail, s, s);
2632 }
2633
2634
2635 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2636 face. */
2637
2638 static void
2639 x_set_cursor_gc (s)
2640 struct glyph_string *s;
2641 {
2642 if (s->font == FRAME_FONT (s->f)
2643 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2644 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2645 && !s->cmp)
2646 s->gc = s->f->output_data.x->cursor_gc;
2647 else
2648 {
2649 /* Cursor on non-default face: must merge. */
2650 XGCValues xgcv;
2651 unsigned long mask;
2652
2653 xgcv.background = s->f->output_data.x->cursor_pixel;
2654 xgcv.foreground = s->face->background;
2655
2656 /* If the glyph would be invisible, try a different foreground. */
2657 if (xgcv.foreground == xgcv.background)
2658 xgcv.foreground = s->face->foreground;
2659 if (xgcv.foreground == xgcv.background)
2660 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2661 if (xgcv.foreground == xgcv.background)
2662 xgcv.foreground = s->face->foreground;
2663
2664 /* Make sure the cursor is distinct from text in this face. */
2665 if (xgcv.background == s->face->background
2666 && xgcv.foreground == s->face->foreground)
2667 {
2668 xgcv.background = s->face->foreground;
2669 xgcv.foreground = s->face->background;
2670 }
2671
2672 IF_DEBUG (x_check_font (s->f, s->font));
2673 xgcv.font = s->font->fid;
2674 xgcv.graphics_exposures = False;
2675 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2676
2677 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2678 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2679 mask, &xgcv);
2680 else
2681 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2682 = XCreateGC (s->display, s->window, mask, &xgcv);
2683
2684 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2685 }
2686 }
2687
2688
2689 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2690
2691 static void
2692 x_set_mouse_face_gc (s)
2693 struct glyph_string *s;
2694 {
2695 int face_id;
2696 struct face *face;
2697
2698 /* What face has to be used last for the mouse face? */
2699 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2700 face = FACE_FROM_ID (s->f, face_id);
2701 if (face == NULL)
2702 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2703
2704 if (s->first_glyph->type == CHAR_GLYPH)
2705 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2706 else
2707 face_id = FACE_FOR_CHAR (s->f, face, 0);
2708 s->face = FACE_FROM_ID (s->f, face_id);
2709 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2710
2711 /* If font in this face is same as S->font, use it. */
2712 if (s->font == s->face->font)
2713 s->gc = s->face->gc;
2714 else
2715 {
2716 /* Otherwise construct scratch_cursor_gc with values from FACE
2717 but font FONT. */
2718 XGCValues xgcv;
2719 unsigned long mask;
2720
2721 xgcv.background = s->face->background;
2722 xgcv.foreground = s->face->foreground;
2723 IF_DEBUG (x_check_font (s->f, s->font));
2724 xgcv.font = s->font->fid;
2725 xgcv.graphics_exposures = False;
2726 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2727
2728 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2729 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2730 mask, &xgcv);
2731 else
2732 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2733 = XCreateGC (s->display, s->window, mask, &xgcv);
2734
2735 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2736 }
2737
2738 xassert (s->gc != 0);
2739 }
2740
2741
2742 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2743 Faces to use in the mode line have already been computed when the
2744 matrix was built, so there isn't much to do, here. */
2745
2746 static INLINE void
2747 x_set_mode_line_face_gc (s)
2748 struct glyph_string *s;
2749 {
2750 s->gc = s->face->gc;
2751 }
2752
2753
2754 /* Set S->gc of glyph string S for drawing that glyph string. Set
2755 S->stippled_p to a non-zero value if the face of S has a stipple
2756 pattern. */
2757
2758 static INLINE void
2759 x_set_glyph_string_gc (s)
2760 struct glyph_string *s;
2761 {
2762 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2763
2764 if (s->hl == DRAW_NORMAL_TEXT)
2765 {
2766 s->gc = s->face->gc;
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_INVERSE_VIDEO)
2770 {
2771 x_set_mode_line_face_gc (s);
2772 s->stippled_p = s->face->stipple != 0;
2773 }
2774 else if (s->hl == DRAW_CURSOR)
2775 {
2776 x_set_cursor_gc (s);
2777 s->stippled_p = 0;
2778 }
2779 else if (s->hl == DRAW_MOUSE_FACE)
2780 {
2781 x_set_mouse_face_gc (s);
2782 s->stippled_p = s->face->stipple != 0;
2783 }
2784 else if (s->hl == DRAW_IMAGE_RAISED
2785 || s->hl == DRAW_IMAGE_SUNKEN)
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790 else
2791 {
2792 s->gc = s->face->gc;
2793 s->stippled_p = s->face->stipple != 0;
2794 }
2795
2796 /* GC must have been set. */
2797 xassert (s->gc != 0);
2798 }
2799
2800
2801 /* Return in *R the clipping rectangle for glyph string S. */
2802
2803 static void
2804 x_get_glyph_string_clip_rect (s, r)
2805 struct glyph_string *s;
2806 XRectangle *r;
2807 {
2808 if (s->row->full_width_p)
2809 {
2810 /* Draw full-width. X coordinates are relative to S->w->left. */
2811 int canon_x = CANON_X_UNIT (s->f);
2812
2813 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2814 r->width = XFASTINT (s->w->width) * canon_x;
2815
2816 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2817 {
2818 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2819 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2820 r->x -= width;
2821 }
2822
2823 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2824
2825 /* Unless displaying a mode or menu bar line, which are always
2826 fully visible, clip to the visible part of the row. */
2827 if (s->w->pseudo_window_p)
2828 r->height = s->row->visible_height;
2829 else
2830 r->height = s->height;
2831 }
2832 else
2833 {
2834 /* This is a text line that may be partially visible. */
2835 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2836 r->width = window_box_width (s->w, s->area);
2837 r->height = s->row->visible_height;
2838 }
2839
2840 /* Don't use S->y for clipping because it doesn't take partially
2841 visible lines into account. For example, it can be negative for
2842 partially visible lines at the top of a window. */
2843 if (!s->row->full_width_p
2844 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2845 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2846 else
2847 r->y = max (0, s->row->y);
2848
2849 /* If drawing a tool-bar window, draw it over the internal border
2850 at the top of the window. */
2851 if (s->w == XWINDOW (s->f->tool_bar_window))
2852 r->y -= s->f->output_data.x->internal_border_width;
2853
2854 /* If S draws overlapping rows, it's sufficient to use the top and
2855 bottom of the window for clipping because this glyph string
2856 intentionally draws over other lines. */
2857 if (s->for_overlaps_p)
2858 {
2859 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2860 r->height = window_text_bottom_y (s->w) - r->y;
2861 }
2862
2863 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2864 }
2865
2866
2867 /* Set clipping for output of glyph string S. S may be part of a mode
2868 line or menu if we don't have X toolkit support. */
2869
2870 static INLINE void
2871 x_set_glyph_string_clipping (s)
2872 struct glyph_string *s;
2873 {
2874 XRectangle r;
2875 x_get_glyph_string_clip_rect (s, &r);
2876 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2877 }
2878
2879
2880 /* Compute left and right overhang of glyph string S. If S is a glyph
2881 string for a composition, assume overhangs don't exist. */
2882
2883 static INLINE void
2884 x_compute_glyph_string_overhangs (s)
2885 struct glyph_string *s;
2886 {
2887 if (s->cmp == NULL
2888 && s->first_glyph->type == CHAR_GLYPH)
2889 {
2890 XCharStruct cs;
2891 int direction, font_ascent, font_descent;
2892 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2893 &font_ascent, &font_descent, &cs);
2894 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2895 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2896 }
2897 }
2898
2899
2900 /* Compute overhangs and x-positions for glyph string S and its
2901 predecessors, or successors. X is the starting x-position for S.
2902 BACKWARD_P non-zero means process predecessors. */
2903
2904 static void
2905 x_compute_overhangs_and_x (s, x, backward_p)
2906 struct glyph_string *s;
2907 int x;
2908 int backward_p;
2909 {
2910 if (backward_p)
2911 {
2912 while (s)
2913 {
2914 x_compute_glyph_string_overhangs (s);
2915 x -= s->width;
2916 s->x = x;
2917 s = s->prev;
2918 }
2919 }
2920 else
2921 {
2922 while (s)
2923 {
2924 x_compute_glyph_string_overhangs (s);
2925 s->x = x;
2926 x += s->width;
2927 s = s->next;
2928 }
2929 }
2930 }
2931
2932
2933 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2934 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2935 assumed to be zero. */
2936
2937 static void
2938 x_get_glyph_overhangs (glyph, f, left, right)
2939 struct glyph *glyph;
2940 struct frame *f;
2941 int *left, *right;
2942 {
2943 *left = *right = 0;
2944
2945 if (glyph->type == CHAR_GLYPH)
2946 {
2947 XFontStruct *font;
2948 struct face *face;
2949 struct font_info *font_info;
2950 XChar2b char2b;
2951 XCharStruct *pcm;
2952
2953 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2954 font = face->font;
2955 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2956 if (font
2957 && (pcm = x_per_char_metric (font, &char2b)))
2958 {
2959 if (pcm->rbearing > pcm->width)
2960 *right = pcm->rbearing - pcm->width;
2961 if (pcm->lbearing < 0)
2962 *left = -pcm->lbearing;
2963 }
2964 }
2965 }
2966
2967
2968 /* Return the index of the first glyph preceding glyph string S that
2969 is overwritten by S because of S's left overhang. Value is -1
2970 if no glyphs are overwritten. */
2971
2972 static int
2973 x_left_overwritten (s)
2974 struct glyph_string *s;
2975 {
2976 int k;
2977
2978 if (s->left_overhang)
2979 {
2980 int x = 0, i;
2981 struct glyph *glyphs = s->row->glyphs[s->area];
2982 int first = s->first_glyph - glyphs;
2983
2984 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2985 x -= glyphs[i].pixel_width;
2986
2987 k = i + 1;
2988 }
2989 else
2990 k = -1;
2991
2992 return k;
2993 }
2994
2995
2996 /* Return the index of the first glyph preceding glyph string S that
2997 is overwriting S because of its right overhang. Value is -1 if no
2998 glyph in front of S overwrites S. */
2999
3000 static int
3001 x_left_overwriting (s)
3002 struct glyph_string *s;
3003 {
3004 int i, k, x;
3005 struct glyph *glyphs = s->row->glyphs[s->area];
3006 int first = s->first_glyph - glyphs;
3007
3008 k = -1;
3009 x = 0;
3010 for (i = first - 1; i >= 0; --i)
3011 {
3012 int left, right;
3013 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3014 if (x + right > 0)
3015 k = i;
3016 x -= glyphs[i].pixel_width;
3017 }
3018
3019 return k;
3020 }
3021
3022
3023 /* Return the index of the last glyph following glyph string S that is
3024 not overwritten by S because of S's right overhang. Value is -1 if
3025 no such glyph is found. */
3026
3027 static int
3028 x_right_overwritten (s)
3029 struct glyph_string *s;
3030 {
3031 int k = -1;
3032
3033 if (s->right_overhang)
3034 {
3035 int x = 0, i;
3036 struct glyph *glyphs = s->row->glyphs[s->area];
3037 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3038 int end = s->row->used[s->area];
3039
3040 for (i = first; i < end && s->right_overhang > x; ++i)
3041 x += glyphs[i].pixel_width;
3042
3043 k = i;
3044 }
3045
3046 return k;
3047 }
3048
3049
3050 /* Return the index of the last glyph following glyph string S that
3051 overwrites S because of its left overhang. Value is negative
3052 if no such glyph is found. */
3053
3054 static int
3055 x_right_overwriting (s)
3056 struct glyph_string *s;
3057 {
3058 int i, k, x;
3059 int end = s->row->used[s->area];
3060 struct glyph *glyphs = s->row->glyphs[s->area];
3061 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3062
3063 k = -1;
3064 x = 0;
3065 for (i = first; i < end; ++i)
3066 {
3067 int left, right;
3068 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3069 if (x - left < 0)
3070 k = i;
3071 x += glyphs[i].pixel_width;
3072 }
3073
3074 return k;
3075 }
3076
3077
3078 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3079
3080 static INLINE void
3081 x_clear_glyph_string_rect (s, x, y, w, h)
3082 struct glyph_string *s;
3083 int x, y, w, h;
3084 {
3085 XGCValues xgcv;
3086 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3087 XSetForeground (s->display, s->gc, xgcv.background);
3088 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3089 XSetForeground (s->display, s->gc, xgcv.foreground);
3090 }
3091
3092
3093 /* Draw the background of glyph_string S. If S->background_filled_p
3094 is non-zero don't draw it. FORCE_P non-zero means draw the
3095 background even if it wouldn't be drawn normally. This is used
3096 when a string preceding S draws into the background of S, or S
3097 contains the first component of a composition. */
3098
3099 static void
3100 x_draw_glyph_string_background (s, force_p)
3101 struct glyph_string *s;
3102 int force_p;
3103 {
3104 /* Nothing to do if background has already been drawn or if it
3105 shouldn't be drawn in the first place. */
3106 if (!s->background_filled_p)
3107 {
3108 int box_line_width = max (s->face->box_line_width, 0);
3109
3110 if (s->stippled_p)
3111 {
3112 /* Fill background with a stipple pattern. */
3113 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3114 XFillRectangle (s->display, s->window, s->gc, s->x,
3115 s->y + box_line_width,
3116 s->background_width,
3117 s->height - 2 * box_line_width);
3118 XSetFillStyle (s->display, s->gc, FillSolid);
3119 s->background_filled_p = 1;
3120 }
3121 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3122 || s->font_not_found_p
3123 || s->extends_to_end_of_line_p
3124 || force_p)
3125 {
3126 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3127 s->background_width,
3128 s->height - 2 * box_line_width);
3129 s->background_filled_p = 1;
3130 }
3131 }
3132 }
3133
3134
3135 /* Draw the foreground of glyph string S. */
3136
3137 static void
3138 x_draw_glyph_string_foreground (s)
3139 struct glyph_string *s;
3140 {
3141 int i, x;
3142
3143 /* If first glyph of S has a left box line, start drawing the text
3144 of S to the right of that box line. */
3145 if (s->face->box != FACE_NO_BOX
3146 && s->first_glyph->left_box_line_p)
3147 x = s->x + abs (s->face->box_line_width);
3148 else
3149 x = s->x;
3150
3151 /* Draw characters of S as rectangles if S's font could not be
3152 loaded. */
3153 if (s->font_not_found_p)
3154 {
3155 for (i = 0; i < s->nchars; ++i)
3156 {
3157 struct glyph *g = s->first_glyph + i;
3158 XDrawRectangle (s->display, s->window,
3159 s->gc, x, s->y, g->pixel_width - 1,
3160 s->height - 1);
3161 x += g->pixel_width;
3162 }
3163 }
3164 else
3165 {
3166 char *char1b = (char *) s->char2b;
3167 int boff = s->font_info->baseline_offset;
3168
3169 if (s->font_info->vertical_centering)
3170 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3171
3172 /* If we can use 8-bit functions, condense S->char2b. */
3173 if (!s->two_byte_p)
3174 for (i = 0; i < s->nchars; ++i)
3175 char1b[i] = s->char2b[i].byte2;
3176
3177 /* Draw text with XDrawString if background has already been
3178 filled. Otherwise, use XDrawImageString. (Note that
3179 XDrawImageString is usually faster than XDrawString.) Always
3180 use XDrawImageString when drawing the cursor so that there is
3181 no chance that characters under a box cursor are invisible. */
3182 if (s->for_overlaps_p
3183 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3184 {
3185 /* Draw characters with 16-bit or 8-bit functions. */
3186 if (s->two_byte_p)
3187 XDrawString16 (s->display, s->window, s->gc, x,
3188 s->ybase - boff, s->char2b, s->nchars);
3189 else
3190 XDrawString (s->display, s->window, s->gc, x,
3191 s->ybase - boff, char1b, s->nchars);
3192 }
3193 else
3194 {
3195 if (s->two_byte_p)
3196 XDrawImageString16 (s->display, s->window, s->gc, x,
3197 s->ybase - boff, s->char2b, s->nchars);
3198 else
3199 XDrawImageString (s->display, s->window, s->gc, x,
3200 s->ybase - boff, char1b, s->nchars);
3201 }
3202 }
3203 }
3204
3205 /* Draw the foreground of composite glyph string S. */
3206
3207 static void
3208 x_draw_composite_glyph_string_foreground (s)
3209 struct glyph_string *s;
3210 {
3211 int i, x;
3212
3213 /* If first glyph of S has a left box line, start drawing the text
3214 of S to the right of that box line. */
3215 if (s->face->box != FACE_NO_BOX
3216 && s->first_glyph->left_box_line_p)
3217 x = s->x + abs (s->face->box_line_width);
3218 else
3219 x = s->x;
3220
3221 /* S is a glyph string for a composition. S->gidx is the index of
3222 the first character drawn for glyphs of this composition.
3223 S->gidx == 0 means we are drawing the very first character of
3224 this composition. */
3225
3226 /* Draw a rectangle for the composition if the font for the very
3227 first character of the composition could not be loaded. */
3228 if (s->font_not_found_p)
3229 {
3230 if (s->gidx == 0)
3231 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3232 s->width - 1, s->height - 1);
3233 }
3234 else
3235 {
3236 for (i = 0; i < s->nchars; i++, ++s->gidx)
3237 XDrawString16 (s->display, s->window, s->gc,
3238 x + s->cmp->offsets[s->gidx * 2],
3239 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3240 s->char2b + i, 1);
3241 }
3242 }
3243
3244
3245 #ifdef USE_X_TOOLKIT
3246
3247 static struct frame *x_frame_of_widget P_ ((Widget));
3248
3249
3250 /* Return the frame on which widget WIDGET is used.. Abort if frame
3251 cannot be determined. */
3252
3253 static struct frame *
3254 x_frame_of_widget (widget)
3255 Widget widget;
3256 {
3257 struct x_display_info *dpyinfo;
3258 Lisp_Object tail;
3259 struct frame *f;
3260
3261 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3262
3263 /* Find the top-level shell of the widget. Note that this function
3264 can be called when the widget is not yet realized, so XtWindow
3265 (widget) == 0. That's the reason we can't simply use
3266 x_any_window_to_frame. */
3267 while (!XtIsTopLevelShell (widget))
3268 widget = XtParent (widget);
3269
3270 /* Look for a frame with that top-level widget. Allocate the color
3271 on that frame to get the right gamma correction value. */
3272 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3273 if (GC_FRAMEP (XCAR (tail))
3274 && (f = XFRAME (XCAR (tail)),
3275 (f->output_data.nothing != 1
3276 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3277 && f->output_data.x->widget == widget)
3278 return f;
3279
3280 abort ();
3281 }
3282
3283
3284 /* Allocate the color COLOR->pixel on the screen and display of
3285 widget WIDGET in colormap CMAP. If an exact match cannot be
3286 allocated, try the nearest color available. Value is non-zero
3287 if successful. This is called from lwlib. */
3288
3289 int
3290 x_alloc_nearest_color_for_widget (widget, cmap, color)
3291 Widget widget;
3292 Colormap cmap;
3293 XColor *color;
3294 {
3295 struct frame *f = x_frame_of_widget (widget);
3296 return x_alloc_nearest_color (f, cmap, color);
3297 }
3298
3299
3300 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3301 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3302 If this produces the same color as PIXEL, try a color where all RGB
3303 values have DELTA added. Return the allocated color in *PIXEL.
3304 DISPLAY is the X display, CMAP is the colormap to operate on.
3305 Value is non-zero if successful. */
3306
3307 int
3308 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3309 Widget widget;
3310 Display *display;
3311 Colormap cmap;
3312 unsigned long *pixel;
3313 double factor;
3314 int delta;
3315 {
3316 struct frame *f = x_frame_of_widget (widget);
3317 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3318 }
3319
3320
3321 #endif /* USE_X_TOOLKIT */
3322
3323
3324 /* Value is an array of XColor structures for the contents of the
3325 color map of frame F. Set *NCELLS to the size of the array.
3326 Note that this probably shouldn't be called for large color maps,
3327 say a 24-bit TrueColor map. */
3328
3329 static const XColor *
3330 x_color_cells (f, ncells)
3331 struct frame *f;
3332 int *ncells;
3333 {
3334 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3335
3336 if (dpyinfo->color_cells == NULL)
3337 {
3338 Display *display = FRAME_X_DISPLAY (f);
3339 Screen *screen = FRAME_X_SCREEN (f);
3340 int i;
3341
3342 dpyinfo->ncolor_cells
3343 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3344 dpyinfo->color_cells
3345 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3346 * sizeof *dpyinfo->color_cells);
3347
3348 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3349 dpyinfo->color_cells[i].pixel = i;
3350
3351 XQueryColors (display, FRAME_X_COLORMAP (f),
3352 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3353 }
3354
3355 *ncells = dpyinfo->ncolor_cells;
3356 return dpyinfo->color_cells;
3357 }
3358
3359
3360 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3361 colors in COLORS. Use cached information, if available. */
3362
3363 void
3364 x_query_colors (f, colors, ncolors)
3365 struct frame *f;
3366 XColor *colors;
3367 int ncolors;
3368 {
3369 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3370
3371 if (dpyinfo->color_cells)
3372 {
3373 int i;
3374 for (i = 0; i < ncolors; ++i)
3375 {
3376 unsigned long pixel = colors[i].pixel;
3377 xassert (pixel < dpyinfo->ncolor_cells);
3378 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3379 colors[i] = dpyinfo->color_cells[pixel];
3380 }
3381 }
3382 else
3383 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3384 }
3385
3386
3387 /* On frame F, translate pixel color to RGB values for the color in
3388 COLOR. Use cached information, if available. */
3389
3390 void
3391 x_query_color (f, color)
3392 struct frame *f;
3393 XColor *color;
3394 {
3395 x_query_colors (f, color, 1);
3396 }
3397
3398
3399 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3400 CMAP. If an exact match can't be allocated, try the nearest color
3401 available. Value is non-zero if successful. Set *COLOR to the
3402 color allocated. */
3403
3404 int
3405 x_alloc_nearest_color (f, cmap, color)
3406 struct frame *f;
3407 Colormap cmap;
3408 XColor *color;
3409 {
3410 Display *display = FRAME_X_DISPLAY (f);
3411 Screen *screen = FRAME_X_SCREEN (f);
3412 int rc;
3413
3414 gamma_correct (f, color);
3415 rc = XAllocColor (display, cmap, color);
3416 if (rc == 0)
3417 {
3418 /* If we got to this point, the colormap is full, so we're going
3419 to try to get the next closest color. The algorithm used is
3420 a least-squares matching, which is what X uses for closest
3421 color matching with StaticColor visuals. */
3422 int nearest, i;
3423 unsigned long nearest_delta = ~0;
3424 int ncells;
3425 const XColor *cells = x_color_cells (f, &ncells);
3426
3427 for (nearest = i = 0; i < ncells; ++i)
3428 {
3429 long dred = (color->red >> 8) - (cells[i].red >> 8);
3430 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3431 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3432 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3433
3434 if (delta < nearest_delta)
3435 {
3436 nearest = i;
3437 nearest_delta = delta;
3438 }
3439 }
3440
3441 color->red = cells[nearest].red;
3442 color->green = cells[nearest].green;
3443 color->blue = cells[nearest].blue;
3444 rc = XAllocColor (display, cmap, color);
3445 }
3446 else
3447 {
3448 /* If allocation succeeded, and the allocated pixel color is not
3449 equal to a cached pixel color recorded earlier, there was a
3450 change in the colormap, so clear the color cache. */
3451 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3452 XColor *cached_color;
3453
3454 if (dpyinfo->color_cells
3455 && (cached_color = &dpyinfo->color_cells[color->pixel],
3456 (cached_color->red != color->red
3457 || cached_color->blue != color->blue
3458 || cached_color->green != color->green)))
3459 {
3460 xfree (dpyinfo->color_cells);
3461 dpyinfo->color_cells = NULL;
3462 dpyinfo->ncolor_cells = 0;
3463 }
3464 }
3465
3466 #ifdef DEBUG_X_COLORS
3467 if (rc)
3468 register_color (color->pixel);
3469 #endif /* DEBUG_X_COLORS */
3470
3471 return rc;
3472 }
3473
3474
3475 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3476 It's necessary to do this instead of just using PIXEL directly to
3477 get color reference counts right. */
3478
3479 unsigned long
3480 x_copy_color (f, pixel)
3481 struct frame *f;
3482 unsigned long pixel;
3483 {
3484 XColor color;
3485
3486 color.pixel = pixel;
3487 BLOCK_INPUT;
3488 x_query_color (f, &color);
3489 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3490 UNBLOCK_INPUT;
3491 #ifdef DEBUG_X_COLORS
3492 register_color (pixel);
3493 #endif
3494 return color.pixel;
3495 }
3496
3497
3498 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3499 It's necessary to do this instead of just using PIXEL directly to
3500 get color reference counts right. */
3501
3502 unsigned long
3503 x_copy_dpy_color (dpy, cmap, pixel)
3504 Display *dpy;
3505 Colormap cmap;
3506 unsigned long pixel;
3507 {
3508 XColor color;
3509
3510 color.pixel = pixel;
3511 BLOCK_INPUT;
3512 XQueryColor (dpy, cmap, &color);
3513 XAllocColor (dpy, cmap, &color);
3514 UNBLOCK_INPUT;
3515 #ifdef DEBUG_X_COLORS
3516 register_color (pixel);
3517 #endif
3518 return color.pixel;
3519 }
3520
3521
3522 /* Brightness beyond which a color won't have its highlight brightness
3523 boosted.
3524
3525 Nominally, highlight colors for `3d' faces are calculated by
3526 brightening an object's color by a constant scale factor, but this
3527 doesn't yield good results for dark colors, so for colors who's
3528 brightness is less than this value (on a scale of 0-65535) have an
3529 use an additional additive factor.
3530
3531 The value here is set so that the default menu-bar/mode-line color
3532 (grey75) will not have its highlights changed at all. */
3533 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3534
3535
3536 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3537 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3538 If this produces the same color as PIXEL, try a color where all RGB
3539 values have DELTA added. Return the allocated color in *PIXEL.
3540 DISPLAY is the X display, CMAP is the colormap to operate on.
3541 Value is non-zero if successful. */
3542
3543 static int
3544 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3545 struct frame *f;
3546 Display *display;
3547 Colormap cmap;
3548 unsigned long *pixel;
3549 double factor;
3550 int delta;
3551 {
3552 XColor color, new;
3553 long bright;
3554 int success_p;
3555
3556 /* Get RGB color values. */
3557 color.pixel = *pixel;
3558 x_query_color (f, &color);
3559
3560 /* Change RGB values by specified FACTOR. Avoid overflow! */
3561 xassert (factor >= 0);
3562 new.red = min (0xffff, factor * color.red);
3563 new.green = min (0xffff, factor * color.green);
3564 new.blue = min (0xffff, factor * color.blue);
3565
3566 /* Calculate brightness of COLOR. */
3567 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3568
3569 /* We only boost colors that are darker than
3570 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3571 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3572 /* Make an additive adjustment to NEW, because it's dark enough so
3573 that scaling by FACTOR alone isn't enough. */
3574 {
3575 /* How far below the limit this color is (0 - 1, 1 being darker). */
3576 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3577 /* The additive adjustment. */
3578 int min_delta = delta * dimness * factor / 2;
3579
3580 if (factor < 1)
3581 {
3582 new.red = max (0, new.red - min_delta);
3583 new.green = max (0, new.green - min_delta);
3584 new.blue = max (0, new.blue - min_delta);
3585 }
3586 else
3587 {
3588 new.red = min (0xffff, min_delta + new.red);
3589 new.green = min (0xffff, min_delta + new.green);
3590 new.blue = min (0xffff, min_delta + new.blue);
3591 }
3592 }
3593
3594 /* Try to allocate the color. */
3595 success_p = x_alloc_nearest_color (f, cmap, &new);
3596 if (success_p)
3597 {
3598 if (new.pixel == *pixel)
3599 {
3600 /* If we end up with the same color as before, try adding
3601 delta to the RGB values. */
3602 x_free_colors (f, &new.pixel, 1);
3603
3604 new.red = min (0xffff, delta + color.red);
3605 new.green = min (0xffff, delta + color.green);
3606 new.blue = min (0xffff, delta + color.blue);
3607 success_p = x_alloc_nearest_color (f, cmap, &new);
3608 }
3609 else
3610 success_p = 1;
3611 *pixel = new.pixel;
3612 }
3613
3614 return success_p;
3615 }
3616
3617
3618 /* Set up the foreground color for drawing relief lines of glyph
3619 string S. RELIEF is a pointer to a struct relief containing the GC
3620 with which lines will be drawn. Use a color that is FACTOR or
3621 DELTA lighter or darker than the relief's background which is found
3622 in S->f->output_data.x->relief_background. If such a color cannot
3623 be allocated, use DEFAULT_PIXEL, instead. */
3624
3625 static void
3626 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3627 struct frame *f;
3628 struct relief *relief;
3629 double factor;
3630 int delta;
3631 unsigned long default_pixel;
3632 {
3633 XGCValues xgcv;
3634 struct x_output *di = f->output_data.x;
3635 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3636 unsigned long pixel;
3637 unsigned long background = di->relief_background;
3638 Colormap cmap = FRAME_X_COLORMAP (f);
3639 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3640 Display *dpy = FRAME_X_DISPLAY (f);
3641
3642 xgcv.graphics_exposures = False;
3643 xgcv.line_width = 1;
3644
3645 /* Free previously allocated color. The color cell will be reused
3646 when it has been freed as many times as it was allocated, so this
3647 doesn't affect faces using the same colors. */
3648 if (relief->gc
3649 && relief->allocated_p)
3650 {
3651 x_free_colors (f, &relief->pixel, 1);
3652 relief->allocated_p = 0;
3653 }
3654
3655 /* Allocate new color. */
3656 xgcv.foreground = default_pixel;
3657 pixel = background;
3658 if (dpyinfo->n_planes != 1
3659 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3660 {
3661 relief->allocated_p = 1;
3662 xgcv.foreground = relief->pixel = pixel;
3663 }
3664
3665 if (relief->gc == 0)
3666 {
3667 xgcv.stipple = dpyinfo->gray;
3668 mask |= GCStipple;
3669 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3670 }
3671 else
3672 XChangeGC (dpy, relief->gc, mask, &xgcv);
3673 }
3674
3675
3676 /* Set up colors for the relief lines around glyph string S. */
3677
3678 static void
3679 x_setup_relief_colors (s)
3680 struct glyph_string *s;
3681 {
3682 struct x_output *di = s->f->output_data.x;
3683 unsigned long color;
3684
3685 if (s->face->use_box_color_for_shadows_p)
3686 color = s->face->box_color;
3687 else
3688 {
3689 XGCValues xgcv;
3690
3691 /* Get the background color of the face. */
3692 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3693 color = xgcv.background;
3694 }
3695
3696 if (di->white_relief.gc == 0
3697 || color != di->relief_background)
3698 {
3699 di->relief_background = color;
3700 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3701 WHITE_PIX_DEFAULT (s->f));
3702 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3703 BLACK_PIX_DEFAULT (s->f));
3704 }
3705 }
3706
3707
3708 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3709 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3710 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3711 relief. LEFT_P non-zero means draw a relief on the left side of
3712 the rectangle. RIGHT_P non-zero means draw a relief on the right
3713 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3714 when drawing. */
3715
3716 static void
3717 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3718 raised_p, left_p, right_p, clip_rect)
3719 struct frame *f;
3720 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3721 XRectangle *clip_rect;
3722 {
3723 int i;
3724 GC gc;
3725
3726 if (raised_p)
3727 gc = f->output_data.x->white_relief.gc;
3728 else
3729 gc = f->output_data.x->black_relief.gc;
3730 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3731
3732 /* Top. */
3733 for (i = 0; i < width; ++i)
3734 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3735 left_x + i * left_p, top_y + i,
3736 right_x + 1 - i * right_p, top_y + i);
3737
3738 /* Left. */
3739 if (left_p)
3740 for (i = 0; i < width; ++i)
3741 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3742 left_x + i, top_y + i, left_x + i, bottom_y - i);
3743
3744 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3745 if (raised_p)
3746 gc = f->output_data.x->black_relief.gc;
3747 else
3748 gc = f->output_data.x->white_relief.gc;
3749 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3750
3751 /* Bottom. */
3752 for (i = 0; i < width; ++i)
3753 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3754 left_x + i * left_p, bottom_y - i,
3755 right_x + 1 - i * right_p, bottom_y - i);
3756
3757 /* Right. */
3758 if (right_p)
3759 for (i = 0; i < width; ++i)
3760 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3761 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3762
3763 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3764 }
3765
3766
3767 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3768 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3769 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3770 left side of the rectangle. RIGHT_P non-zero means draw a line
3771 on the right side of the rectangle. CLIP_RECT is the clipping
3772 rectangle to use when drawing. */
3773
3774 static void
3775 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3776 left_p, right_p, clip_rect)
3777 struct glyph_string *s;
3778 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3779 XRectangle *clip_rect;
3780 {
3781 XGCValues xgcv;
3782
3783 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3784 XSetForeground (s->display, s->gc, s->face->box_color);
3785 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3786
3787 /* Top. */
3788 XFillRectangle (s->display, s->window, s->gc,
3789 left_x, top_y, right_x - left_x + 1, width);
3790
3791 /* Left. */
3792 if (left_p)
3793 XFillRectangle (s->display, s->window, s->gc,
3794 left_x, top_y, width, bottom_y - top_y + 1);
3795
3796 /* Bottom. */
3797 XFillRectangle (s->display, s->window, s->gc,
3798 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3799
3800 /* Right. */
3801 if (right_p)
3802 XFillRectangle (s->display, s->window, s->gc,
3803 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3804
3805 XSetForeground (s->display, s->gc, xgcv.foreground);
3806 XSetClipMask (s->display, s->gc, None);
3807 }
3808
3809
3810 /* Draw a box around glyph string S. */
3811
3812 static void
3813 x_draw_glyph_string_box (s)
3814 struct glyph_string *s;
3815 {
3816 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3817 int left_p, right_p;
3818 struct glyph *last_glyph;
3819 XRectangle clip_rect;
3820
3821 last_x = window_box_right (s->w, s->area);
3822 if (s->row->full_width_p
3823 && !s->w->pseudo_window_p)
3824 {
3825 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3826 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3827 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3828 }
3829
3830 /* The glyph that may have a right box line. */
3831 last_glyph = (s->cmp || s->img
3832 ? s->first_glyph
3833 : s->first_glyph + s->nchars - 1);
3834
3835 width = abs (s->face->box_line_width);
3836 raised_p = s->face->box == FACE_RAISED_BOX;
3837 left_x = s->x;
3838 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3839 ? last_x - 1
3840 : min (last_x, s->x + s->background_width) - 1);
3841 top_y = s->y;
3842 bottom_y = top_y + s->height - 1;
3843
3844 left_p = (s->first_glyph->left_box_line_p
3845 || (s->hl == DRAW_MOUSE_FACE
3846 && (s->prev == NULL
3847 || s->prev->hl != s->hl)));
3848 right_p = (last_glyph->right_box_line_p
3849 || (s->hl == DRAW_MOUSE_FACE
3850 && (s->next == NULL
3851 || s->next->hl != s->hl)));
3852
3853 x_get_glyph_string_clip_rect (s, &clip_rect);
3854
3855 if (s->face->box == FACE_SIMPLE_BOX)
3856 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3857 left_p, right_p, &clip_rect);
3858 else
3859 {
3860 x_setup_relief_colors (s);
3861 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3862 width, raised_p, left_p, right_p, &clip_rect);
3863 }
3864 }
3865
3866
3867 /* Draw foreground of image glyph string S. */
3868
3869 static void
3870 x_draw_image_foreground (s)
3871 struct glyph_string *s;
3872 {
3873 int x;
3874 int y = s->ybase - image_ascent (s->img, s->face);
3875
3876 /* If first glyph of S has a left box line, start drawing it to the
3877 right of that line. */
3878 if (s->face->box != FACE_NO_BOX
3879 && s->first_glyph->left_box_line_p)
3880 x = s->x + abs (s->face->box_line_width);
3881 else
3882 x = s->x;
3883
3884 /* If there is a margin around the image, adjust x- and y-position
3885 by that margin. */
3886 x += s->img->hmargin;
3887 y += s->img->vmargin;
3888
3889 if (s->img->pixmap)
3890 {
3891 if (s->img->mask)
3892 {
3893 /* We can't set both a clip mask and use XSetClipRectangles
3894 because the latter also sets a clip mask. We also can't
3895 trust on the shape extension to be available
3896 (XShapeCombineRegion). So, compute the rectangle to draw
3897 manually. */
3898 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3899 | GCFunction);
3900 XGCValues xgcv;
3901 XRectangle clip_rect, image_rect, r;
3902
3903 xgcv.clip_mask = s->img->mask;
3904 xgcv.clip_x_origin = x;
3905 xgcv.clip_y_origin = y;
3906 xgcv.function = GXcopy;
3907 XChangeGC (s->display, s->gc, mask, &xgcv);
3908
3909 x_get_glyph_string_clip_rect (s, &clip_rect);
3910 image_rect.x = x;
3911 image_rect.y = y;
3912 image_rect.width = s->img->width;
3913 image_rect.height = s->img->height;
3914 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3915 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3916 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3917 }
3918 else
3919 {
3920 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3921 XGCValues xgcv;
3922 XRectangle clip_rect, image_rect, r;
3923
3924 x_get_glyph_string_clip_rect (s, &clip_rect);
3925 image_rect.x = x;
3926 image_rect.y = y;
3927 image_rect.width = s->img->width;
3928 image_rect.height = s->img->height;
3929 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3930 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3931 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3932
3933 /* When the image has a mask, we can expect that at
3934 least part of a mouse highlight or a block cursor will
3935 be visible. If the image doesn't have a mask, make
3936 a block cursor visible by drawing a rectangle around
3937 the image. I believe it's looking better if we do
3938 nothing here for mouse-face. */
3939 if (s->hl == DRAW_CURSOR)
3940 XDrawRectangle (s->display, s->window, s->gc, x, y,
3941 s->img->width - 1, s->img->height - 1);
3942 }
3943 }
3944 else
3945 /* Draw a rectangle if image could not be loaded. */
3946 XDrawRectangle (s->display, s->window, s->gc, x, y,
3947 s->img->width - 1, s->img->height - 1);
3948 }
3949
3950
3951 /* Draw a relief around the image glyph string S. */
3952
3953 static void
3954 x_draw_image_relief (s)
3955 struct glyph_string *s;
3956 {
3957 int x0, y0, x1, y1, thick, raised_p;
3958 XRectangle r;
3959 int x;
3960 int y = s->ybase - image_ascent (s->img, s->face);
3961
3962 /* If first glyph of S has a left box line, start drawing it to the
3963 right of that line. */
3964 if (s->face->box != FACE_NO_BOX
3965 && s->first_glyph->left_box_line_p)
3966 x = s->x + abs (s->face->box_line_width);
3967 else
3968 x = s->x;
3969
3970 /* If there is a margin around the image, adjust x- and y-position
3971 by that margin. */
3972 x += s->img->hmargin;
3973 y += s->img->vmargin;
3974
3975 if (s->hl == DRAW_IMAGE_SUNKEN
3976 || s->hl == DRAW_IMAGE_RAISED)
3977 {
3978 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3979 raised_p = s->hl == DRAW_IMAGE_RAISED;
3980 }
3981 else
3982 {
3983 thick = abs (s->img->relief);
3984 raised_p = s->img->relief > 0;
3985 }
3986
3987 x0 = x - thick;
3988 y0 = y - thick;
3989 x1 = x + s->img->width + thick - 1;
3990 y1 = y + s->img->height + thick - 1;
3991
3992 x_setup_relief_colors (s);
3993 x_get_glyph_string_clip_rect (s, &r);
3994 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3995 }
3996
3997
3998 /* Draw the foreground of image glyph string S to PIXMAP. */
3999
4000 static void
4001 x_draw_image_foreground_1 (s, pixmap)
4002 struct glyph_string *s;
4003 Pixmap pixmap;
4004 {
4005 int x;
4006 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4007
4008 /* If first glyph of S has a left box line, start drawing it to the
4009 right of that line. */
4010 if (s->face->box != FACE_NO_BOX
4011 && s->first_glyph->left_box_line_p)
4012 x = abs (s->face->box_line_width);
4013 else
4014 x = 0;
4015
4016 /* If there is a margin around the image, adjust x- and y-position
4017 by that margin. */
4018 x += s->img->hmargin;
4019 y += s->img->vmargin;
4020
4021 if (s->img->pixmap)
4022 {
4023 if (s->img->mask)
4024 {
4025 /* We can't set both a clip mask and use XSetClipRectangles
4026 because the latter also sets a clip mask. We also can't
4027 trust on the shape extension to be available
4028 (XShapeCombineRegion). So, compute the rectangle to draw
4029 manually. */
4030 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4031 | GCFunction);
4032 XGCValues xgcv;
4033
4034 xgcv.clip_mask = s->img->mask;
4035 xgcv.clip_x_origin = x;
4036 xgcv.clip_y_origin = y;
4037 xgcv.function = GXcopy;
4038 XChangeGC (s->display, s->gc, mask, &xgcv);
4039
4040 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4041 0, 0, s->img->width, s->img->height, x, y);
4042 XSetClipMask (s->display, s->gc, None);
4043 }
4044 else
4045 {
4046 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4047 0, 0, s->img->width, s->img->height, x, y);
4048
4049 /* When the image has a mask, we can expect that at
4050 least part of a mouse highlight or a block cursor will
4051 be visible. If the image doesn't have a mask, make
4052 a block cursor visible by drawing a rectangle around
4053 the image. I believe it's looking better if we do
4054 nothing here for mouse-face. */
4055 if (s->hl == DRAW_CURSOR)
4056 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4057 s->img->width - 1, s->img->height - 1);
4058 }
4059 }
4060 else
4061 /* Draw a rectangle if image could not be loaded. */
4062 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4063 s->img->width - 1, s->img->height - 1);
4064 }
4065
4066
4067 /* Draw part of the background of glyph string S. X, Y, W, and H
4068 give the rectangle to draw. */
4069
4070 static void
4071 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4072 struct glyph_string *s;
4073 int x, y, w, h;
4074 {
4075 if (s->stippled_p)
4076 {
4077 /* Fill background with a stipple pattern. */
4078 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4079 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4080 XSetFillStyle (s->display, s->gc, FillSolid);
4081 }
4082 else
4083 x_clear_glyph_string_rect (s, x, y, w, h);
4084 }
4085
4086
4087 /* Draw image glyph string S.
4088
4089 s->y
4090 s->x +-------------------------
4091 | s->face->box
4092 |
4093 | +-------------------------
4094 | | s->img->margin
4095 | |
4096 | | +-------------------
4097 | | | the image
4098
4099 */
4100
4101 static void
4102 x_draw_image_glyph_string (s)
4103 struct glyph_string *s;
4104 {
4105 int x, y;
4106 int box_line_hwidth = abs (s->face->box_line_width);
4107 int box_line_vwidth = max (s->face->box_line_width, 0);
4108 int height;
4109 Pixmap pixmap = None;
4110
4111 height = s->height - 2 * box_line_vwidth;
4112
4113 /* Fill background with face under the image. Do it only if row is
4114 taller than image or if image has a clip mask to reduce
4115 flickering. */
4116 s->stippled_p = s->face->stipple != 0;
4117 if (height > s->img->height
4118 || s->img->hmargin
4119 || s->img->vmargin
4120 || s->img->mask
4121 || s->img->pixmap == 0
4122 || s->width != s->background_width)
4123 {
4124 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4125 x = s->x + box_line_hwidth;
4126 else
4127 x = s->x;
4128
4129 y = s->y + box_line_vwidth;
4130
4131 if (s->img->mask)
4132 {
4133 /* Create a pixmap as large as the glyph string. Fill it
4134 with the background color. Copy the image to it, using
4135 its mask. Copy the temporary pixmap to the display. */
4136 Screen *screen = FRAME_X_SCREEN (s->f);
4137 int depth = DefaultDepthOfScreen (screen);
4138
4139 /* Create a pixmap as large as the glyph string. */
4140 pixmap = XCreatePixmap (s->display, s->window,
4141 s->background_width,
4142 s->height, depth);
4143
4144 /* Don't clip in the following because we're working on the
4145 pixmap. */
4146 XSetClipMask (s->display, s->gc, None);
4147
4148 /* Fill the pixmap with the background color/stipple. */
4149 if (s->stippled_p)
4150 {
4151 /* Fill background with a stipple pattern. */
4152 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4153 XFillRectangle (s->display, pixmap, s->gc,
4154 0, 0, s->background_width, s->height);
4155 XSetFillStyle (s->display, s->gc, FillSolid);
4156 }
4157 else
4158 {
4159 XGCValues xgcv;
4160 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4161 &xgcv);
4162 XSetForeground (s->display, s->gc, xgcv.background);
4163 XFillRectangle (s->display, pixmap, s->gc,
4164 0, 0, s->background_width, s->height);
4165 XSetForeground (s->display, s->gc, xgcv.foreground);
4166 }
4167 }
4168 else
4169 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4170
4171 s->background_filled_p = 1;
4172 }
4173
4174 /* Draw the foreground. */
4175 if (pixmap != None)
4176 {
4177 x_draw_image_foreground_1 (s, pixmap);
4178 x_set_glyph_string_clipping (s);
4179 XCopyArea (s->display, pixmap, s->window, s->gc,
4180 0, 0, s->background_width, s->height, s->x, s->y);
4181 XFreePixmap (s->display, pixmap);
4182 }
4183 else
4184 x_draw_image_foreground (s);
4185
4186 /* If we must draw a relief around the image, do it. */
4187 if (s->img->relief
4188 || s->hl == DRAW_IMAGE_RAISED
4189 || s->hl == DRAW_IMAGE_SUNKEN)
4190 x_draw_image_relief (s);
4191 }
4192
4193
4194 /* Draw stretch glyph string S. */
4195
4196 static void
4197 x_draw_stretch_glyph_string (s)
4198 struct glyph_string *s;
4199 {
4200 xassert (s->first_glyph->type == STRETCH_GLYPH);
4201 s->stippled_p = s->face->stipple != 0;
4202
4203 if (s->hl == DRAW_CURSOR
4204 && !x_stretch_cursor_p)
4205 {
4206 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4207 as wide as the stretch glyph. */
4208 int width = min (CANON_X_UNIT (s->f), s->background_width);
4209
4210 /* Draw cursor. */
4211 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4212
4213 /* Clear rest using the GC of the original non-cursor face. */
4214 if (width < s->background_width)
4215 {
4216 GC gc = s->face->gc;
4217 int x = s->x + width, y = s->y;
4218 int w = s->background_width - width, h = s->height;
4219 XRectangle r;
4220
4221 x_get_glyph_string_clip_rect (s, &r);
4222 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4223
4224 if (s->face->stipple)
4225 {
4226 /* Fill background with a stipple pattern. */
4227 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4228 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4229 XSetFillStyle (s->display, gc, FillSolid);
4230 }
4231 else
4232 {
4233 XGCValues xgcv;
4234 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4235 XSetForeground (s->display, gc, xgcv.background);
4236 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4237 XSetForeground (s->display, gc, xgcv.foreground);
4238 }
4239 }
4240 }
4241 else
4242 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4243 s->height);
4244
4245 s->background_filled_p = 1;
4246 }
4247
4248
4249 /* Draw glyph string S. */
4250
4251 static void
4252 x_draw_glyph_string (s)
4253 struct glyph_string *s;
4254 {
4255 /* If S draws into the background of its successor, draw the
4256 background of the successor first so that S can draw into it.
4257 This makes S->next use XDrawString instead of XDrawImageString. */
4258 if (s->next && s->right_overhang && !s->for_overlaps_p)
4259 {
4260 xassert (s->next->img == NULL);
4261 x_set_glyph_string_gc (s->next);
4262 x_set_glyph_string_clipping (s->next);
4263 x_draw_glyph_string_background (s->next, 1);
4264 }
4265
4266 /* Set up S->gc, set clipping and draw S. */
4267 x_set_glyph_string_gc (s);
4268 x_set_glyph_string_clipping (s);
4269
4270 switch (s->first_glyph->type)
4271 {
4272 case IMAGE_GLYPH:
4273 x_draw_image_glyph_string (s);
4274 break;
4275
4276 case STRETCH_GLYPH:
4277 x_draw_stretch_glyph_string (s);
4278 break;
4279
4280 case CHAR_GLYPH:
4281 if (s->for_overlaps_p)
4282 s->background_filled_p = 1;
4283 else
4284 x_draw_glyph_string_background (s, 0);
4285 x_draw_glyph_string_foreground (s);
4286 break;
4287
4288 case COMPOSITE_GLYPH:
4289 if (s->for_overlaps_p || s->gidx > 0)
4290 s->background_filled_p = 1;
4291 else
4292 x_draw_glyph_string_background (s, 1);
4293 x_draw_composite_glyph_string_foreground (s);
4294 break;
4295
4296 default:
4297 abort ();
4298 }
4299
4300 if (!s->for_overlaps_p)
4301 {
4302 /* Draw underline. */
4303 if (s->face->underline_p)
4304 {
4305 unsigned long tem, h;
4306 int y;
4307
4308 /* Get the underline thickness. Default is 1 pixel. */
4309 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4310 h = 1;
4311
4312 /* Get the underline position. This is the recommended
4313 vertical offset in pixels from the baseline to the top of
4314 the underline. This is a signed value according to the
4315 specs, and its default is
4316
4317 ROUND ((maximum descent) / 2), with
4318 ROUND(x) = floor (x + 0.5) */
4319
4320 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4321 y = s->ybase + (long) tem;
4322 else if (s->face->font)
4323 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4324 else
4325 y = s->height - h;
4326
4327 if (s->face->underline_defaulted_p)
4328 XFillRectangle (s->display, s->window, s->gc,
4329 s->x, y, s->width, h);
4330 else
4331 {
4332 XGCValues xgcv;
4333 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4334 XSetForeground (s->display, s->gc, s->face->underline_color);
4335 XFillRectangle (s->display, s->window, s->gc,
4336 s->x, y, s->width, h);
4337 XSetForeground (s->display, s->gc, xgcv.foreground);
4338 }
4339 }
4340
4341 /* Draw overline. */
4342 if (s->face->overline_p)
4343 {
4344 unsigned long dy = 0, h = 1;
4345
4346 if (s->face->overline_color_defaulted_p)
4347 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4348 s->width, h);
4349 else
4350 {
4351 XGCValues xgcv;
4352 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4353 XSetForeground (s->display, s->gc, s->face->overline_color);
4354 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4355 s->width, h);
4356 XSetForeground (s->display, s->gc, xgcv.foreground);
4357 }
4358 }
4359
4360 /* Draw strike-through. */
4361 if (s->face->strike_through_p)
4362 {
4363 unsigned long h = 1;
4364 unsigned long dy = (s->height - h) / 2;
4365
4366 if (s->face->strike_through_color_defaulted_p)
4367 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4368 s->width, h);
4369 else
4370 {
4371 XGCValues xgcv;
4372 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4373 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4374 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4375 s->width, h);
4376 XSetForeground (s->display, s->gc, xgcv.foreground);
4377 }
4378 }
4379
4380 /* Draw relief. */
4381 if (s->face->box != FACE_NO_BOX)
4382 x_draw_glyph_string_box (s);
4383 }
4384
4385 /* Reset clipping. */
4386 XSetClipMask (s->display, s->gc, None);
4387 }
4388
4389
4390 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4391 struct face **, int));
4392
4393
4394 /* Fill glyph string S with composition components specified by S->cmp.
4395
4396 FACES is an array of faces for all components of this composition.
4397 S->gidx is the index of the first component for S.
4398 OVERLAPS_P non-zero means S should draw the foreground only, and
4399 use its physical height for clipping.
4400
4401 Value is the index of a component not in S. */
4402
4403 static int
4404 x_fill_composite_glyph_string (s, faces, overlaps_p)
4405 struct glyph_string *s;
4406 struct face **faces;
4407 int overlaps_p;
4408 {
4409 int i;
4410
4411 xassert (s);
4412
4413 s->for_overlaps_p = overlaps_p;
4414
4415 s->face = faces[s->gidx];
4416 s->font = s->face->font;
4417 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4418
4419 /* For all glyphs of this composition, starting at the offset
4420 S->gidx, until we reach the end of the definition or encounter a
4421 glyph that requires the different face, add it to S. */
4422 ++s->nchars;
4423 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4424 ++s->nchars;
4425
4426 /* All glyph strings for the same composition has the same width,
4427 i.e. the width set for the first component of the composition. */
4428
4429 s->width = s->first_glyph->pixel_width;
4430
4431 /* If the specified font could not be loaded, use the frame's
4432 default font, but record the fact that we couldn't load it in
4433 the glyph string so that we can draw rectangles for the
4434 characters of the glyph string. */
4435 if (s->font == NULL)
4436 {
4437 s->font_not_found_p = 1;
4438 s->font = FRAME_FONT (s->f);
4439 }
4440
4441 /* Adjust base line for subscript/superscript text. */
4442 s->ybase += s->first_glyph->voffset;
4443
4444 xassert (s->face && s->face->gc);
4445
4446 /* This glyph string must always be drawn with 16-bit functions. */
4447 s->two_byte_p = 1;
4448
4449 return s->gidx + s->nchars;
4450 }
4451
4452
4453 /* Fill glyph string S from a sequence of character glyphs.
4454
4455 FACE_ID is the face id of the string. START is the index of the
4456 first glyph to consider, END is the index of the last + 1.
4457 OVERLAPS_P non-zero means S should draw the foreground only, and
4458 use its physical height for clipping.
4459
4460 Value is the index of the first glyph not in S. */
4461
4462 static int
4463 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4464 struct glyph_string *s;
4465 int face_id;
4466 int start, end, overlaps_p;
4467 {
4468 struct glyph *glyph, *last;
4469 int voffset;
4470 int glyph_not_available_p;
4471
4472 xassert (s->f == XFRAME (s->w->frame));
4473 xassert (s->nchars == 0);
4474 xassert (start >= 0 && end > start);
4475
4476 s->for_overlaps_p = overlaps_p,
4477 glyph = s->row->glyphs[s->area] + start;
4478 last = s->row->glyphs[s->area] + end;
4479 voffset = glyph->voffset;
4480
4481 glyph_not_available_p = glyph->glyph_not_available_p;
4482
4483 while (glyph < last
4484 && glyph->type == CHAR_GLYPH
4485 && glyph->voffset == voffset
4486 /* Same face id implies same font, nowadays. */
4487 && glyph->face_id == face_id
4488 && glyph->glyph_not_available_p == glyph_not_available_p)
4489 {
4490 int two_byte_p;
4491
4492 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4493 s->char2b + s->nchars,
4494 &two_byte_p);
4495 s->two_byte_p = two_byte_p;
4496 ++s->nchars;
4497 xassert (s->nchars <= end - start);
4498 s->width += glyph->pixel_width;
4499 ++glyph;
4500 }
4501
4502 s->font = s->face->font;
4503 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4504
4505 /* If the specified font could not be loaded, use the frame's font,
4506 but record the fact that we couldn't load it in
4507 S->font_not_found_p so that we can draw rectangles for the
4508 characters of the glyph string. */
4509 if (s->font == NULL || glyph_not_available_p)
4510 {
4511 s->font_not_found_p = 1;
4512 s->font = FRAME_FONT (s->f);
4513 }
4514
4515 /* Adjust base line for subscript/superscript text. */
4516 s->ybase += voffset;
4517
4518 xassert (s->face && s->face->gc);
4519 return glyph - s->row->glyphs[s->area];
4520 }
4521
4522
4523 /* Fill glyph string S from image glyph S->first_glyph. */
4524
4525 static void
4526 x_fill_image_glyph_string (s)
4527 struct glyph_string *s;
4528 {
4529 xassert (s->first_glyph->type == IMAGE_GLYPH);
4530 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4531 xassert (s->img);
4532 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4533 s->font = s->face->font;
4534 s->width = s->first_glyph->pixel_width;
4535
4536 /* Adjust base line for subscript/superscript text. */
4537 s->ybase += s->first_glyph->voffset;
4538 }
4539
4540
4541 /* Fill glyph string S from a sequence of stretch glyphs.
4542
4543 ROW is the glyph row in which the glyphs are found, AREA is the
4544 area within the row. START is the index of the first glyph to
4545 consider, END is the index of the last + 1.
4546
4547 Value is the index of the first glyph not in S. */
4548
4549 static int
4550 x_fill_stretch_glyph_string (s, row, area, start, end)
4551 struct glyph_string *s;
4552 struct glyph_row *row;
4553 enum glyph_row_area area;
4554 int start, end;
4555 {
4556 struct glyph *glyph, *last;
4557 int voffset, face_id;
4558
4559 xassert (s->first_glyph->type == STRETCH_GLYPH);
4560
4561 glyph = s->row->glyphs[s->area] + start;
4562 last = s->row->glyphs[s->area] + end;
4563 face_id = glyph->face_id;
4564 s->face = FACE_FROM_ID (s->f, face_id);
4565 s->font = s->face->font;
4566 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4567 s->width = glyph->pixel_width;
4568 voffset = glyph->voffset;
4569
4570 for (++glyph;
4571 (glyph < last
4572 && glyph->type == STRETCH_GLYPH
4573 && glyph->voffset == voffset
4574 && glyph->face_id == face_id);
4575 ++glyph)
4576 s->width += glyph->pixel_width;
4577
4578 /* Adjust base line for subscript/superscript text. */
4579 s->ybase += voffset;
4580
4581 xassert (s->face && s->face->gc);
4582 return glyph - s->row->glyphs[s->area];
4583 }
4584
4585
4586 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4587 of XChar2b structures for S; it can't be allocated in
4588 x_init_glyph_string because it must be allocated via `alloca'. W
4589 is the window on which S is drawn. ROW and AREA are the glyph row
4590 and area within the row from which S is constructed. START is the
4591 index of the first glyph structure covered by S. HL is a
4592 face-override for drawing S. */
4593
4594 static void
4595 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4596 struct glyph_string *s;
4597 XChar2b *char2b;
4598 struct window *w;
4599 struct glyph_row *row;
4600 enum glyph_row_area area;
4601 int start;
4602 enum draw_glyphs_face hl;
4603 {
4604 bzero (s, sizeof *s);
4605 s->w = w;
4606 s->f = XFRAME (w->frame);
4607 s->display = FRAME_X_DISPLAY (s->f);
4608 s->window = FRAME_X_WINDOW (s->f);
4609 s->char2b = char2b;
4610 s->hl = hl;
4611 s->row = row;
4612 s->area = area;
4613 s->first_glyph = row->glyphs[area] + start;
4614 s->height = row->height;
4615 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4616
4617 /* Display the internal border below the tool-bar window. */
4618 if (s->w == XWINDOW (s->f->tool_bar_window))
4619 s->y -= s->f->output_data.x->internal_border_width;
4620
4621 s->ybase = s->y + row->ascent;
4622 }
4623
4624
4625 /* Set background width of glyph string S. START is the index of the
4626 first glyph following S. LAST_X is the right-most x-position + 1
4627 in the drawing area. */
4628
4629 static INLINE void
4630 x_set_glyph_string_background_width (s, start, last_x)
4631 struct glyph_string *s;
4632 int start;
4633 int last_x;
4634 {
4635 /* If the face of this glyph string has to be drawn to the end of
4636 the drawing area, set S->extends_to_end_of_line_p. */
4637 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4638
4639 if (start == s->row->used[s->area]
4640 && s->hl == DRAW_NORMAL_TEXT
4641 && s->area == TEXT_AREA
4642 && (s->row->fill_line_p
4643 || s->face->background != default_face->background
4644 || s->face->stipple != default_face->stipple))
4645 s->extends_to_end_of_line_p = 1;
4646
4647 /* If S extends its face to the end of the line, set its
4648 background_width to the distance to the right edge of the drawing
4649 area. */
4650 if (s->extends_to_end_of_line_p)
4651 s->background_width = last_x - s->x + 1;
4652 else
4653 s->background_width = s->width;
4654 }
4655
4656
4657 /* Add a glyph string for a stretch glyph to the list of strings
4658 between HEAD and TAIL. START is the index of the stretch glyph in
4659 row area AREA of glyph row ROW. END is the index of the last glyph
4660 in that glyph row area. X is the current output position assigned
4661 to the new glyph string constructed. HL overrides that face of the
4662 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4663 is the right-most x-position of the drawing area. */
4664
4665 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4666 and below -- keep them on one line. */
4667 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4668 do \
4669 { \
4670 s = (struct glyph_string *) alloca (sizeof *s); \
4671 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4672 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4673 x_append_glyph_string (&HEAD, &TAIL, s); \
4674 s->x = (X); \
4675 } \
4676 while (0)
4677
4678
4679 /* Add a glyph string for an image glyph to the list of strings
4680 between HEAD and TAIL. START is the index of the image glyph in
4681 row area AREA of glyph row ROW. END is the index of the last glyph
4682 in that glyph row area. X is the current output position assigned
4683 to the new glyph string constructed. HL overrides that face of the
4684 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4685 is the right-most x-position of the drawing area. */
4686
4687 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4688 do \
4689 { \
4690 s = (struct glyph_string *) alloca (sizeof *s); \
4691 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4692 x_fill_image_glyph_string (s); \
4693 x_append_glyph_string (&HEAD, &TAIL, s); \
4694 ++START; \
4695 s->x = (X); \
4696 } \
4697 while (0)
4698
4699
4700 /* Add a glyph string for a sequence of character glyphs to the list
4701 of strings between HEAD and TAIL. START is the index of the first
4702 glyph in row area AREA of glyph row ROW that is part of the new
4703 glyph string. END is the index of the last glyph in that glyph row
4704 area. X is the current output position assigned to the new glyph
4705 string constructed. HL overrides that face of the glyph; e.g. it
4706 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4707 right-most x-position of the drawing area. */
4708
4709 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4710 do \
4711 { \
4712 int c, face_id; \
4713 XChar2b *char2b; \
4714 \
4715 c = (ROW)->glyphs[AREA][START].u.ch; \
4716 face_id = (ROW)->glyphs[AREA][START].face_id; \
4717 \
4718 s = (struct glyph_string *) alloca (sizeof *s); \
4719 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4720 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4721 x_append_glyph_string (&HEAD, &TAIL, s); \
4722 s->x = (X); \
4723 START = x_fill_glyph_string (s, face_id, START, END, \
4724 OVERLAPS_P); \
4725 } \
4726 while (0)
4727
4728
4729 /* Add a glyph string for a composite sequence to the list of strings
4730 between HEAD and TAIL. START is the index of the first glyph in
4731 row area AREA of glyph row ROW that is part of the new glyph
4732 string. END is the index of the last glyph in that glyph row area.
4733 X is the current output position assigned to the new glyph string
4734 constructed. HL overrides that face of the glyph; e.g. it is
4735 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4736 x-position of the drawing area. */
4737
4738 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4739 do { \
4740 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4741 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4742 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4743 struct composition *cmp = composition_table[cmp_id]; \
4744 int glyph_len = cmp->glyph_len; \
4745 XChar2b *char2b; \
4746 struct face **faces; \
4747 struct glyph_string *first_s = NULL; \
4748 int n; \
4749 \
4750 base_face = base_face->ascii_face; \
4751 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4752 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4753 /* At first, fill in `char2b' and `faces'. */ \
4754 for (n = 0; n < glyph_len; n++) \
4755 { \
4756 int c = COMPOSITION_GLYPH (cmp, n); \
4757 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4758 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4759 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4760 this_face_id, char2b + n, 1); \
4761 } \
4762 \
4763 /* Make glyph_strings for each glyph sequence that is drawable by \
4764 the same face, and append them to HEAD/TAIL. */ \
4765 for (n = 0; n < cmp->glyph_len;) \
4766 { \
4767 s = (struct glyph_string *) alloca (sizeof *s); \
4768 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4769 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4770 s->cmp = cmp; \
4771 s->gidx = n; \
4772 s->x = (X); \
4773 \
4774 if (n == 0) \
4775 first_s = s; \
4776 \
4777 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4778 } \
4779 \
4780 ++START; \
4781 s = first_s; \
4782 } while (0)
4783
4784
4785 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4786 of AREA of glyph row ROW on window W between indices START and END.
4787 HL overrides the face for drawing glyph strings, e.g. it is
4788 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4789 x-positions of the drawing area.
4790
4791 This is an ugly monster macro construct because we must use alloca
4792 to allocate glyph strings (because x_draw_glyphs can be called
4793 asynchronously). */
4794
4795 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4796 do \
4797 { \
4798 HEAD = TAIL = NULL; \
4799 while (START < END) \
4800 { \
4801 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4802 switch (first_glyph->type) \
4803 { \
4804 case CHAR_GLYPH: \
4805 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4806 TAIL, HL, X, LAST_X, \
4807 OVERLAPS_P); \
4808 break; \
4809 \
4810 case COMPOSITE_GLYPH: \
4811 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4812 HEAD, TAIL, HL, X, LAST_X,\
4813 OVERLAPS_P); \
4814 break; \
4815 \
4816 case STRETCH_GLYPH: \
4817 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4818 HEAD, TAIL, HL, X, LAST_X); \
4819 break; \
4820 \
4821 case IMAGE_GLYPH: \
4822 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4823 TAIL, HL, X, LAST_X); \
4824 break; \
4825 \
4826 default: \
4827 abort (); \
4828 } \
4829 \
4830 x_set_glyph_string_background_width (s, START, LAST_X); \
4831 (X) += s->width; \
4832 } \
4833 } \
4834 while (0)
4835
4836
4837 /* Draw glyphs between START and END in AREA of ROW on window W,
4838 starting at x-position X. X is relative to AREA in W. HL is a
4839 face-override with the following meaning:
4840
4841 DRAW_NORMAL_TEXT draw normally
4842 DRAW_CURSOR draw in cursor face
4843 DRAW_MOUSE_FACE draw in mouse face.
4844 DRAW_INVERSE_VIDEO draw in mode line face
4845 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4846 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4847
4848 If REAL_START is non-null, return in *REAL_START the real starting
4849 position for display. This can be different from START in case
4850 overlapping glyphs must be displayed. If REAL_END is non-null,
4851 return in *REAL_END the real end position for display. This can be
4852 different from END in case overlapping glyphs must be displayed.
4853
4854 If OVERLAPS_P is non-zero, draw only the foreground of characters
4855 and clip to the physical height of ROW.
4856
4857 Value is the x-position reached, relative to AREA of W. */
4858
4859 static int
4860 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4861 overlaps_p)
4862 struct window *w;
4863 int x;
4864 struct glyph_row *row;
4865 enum glyph_row_area area;
4866 int start, end;
4867 enum draw_glyphs_face hl;
4868 int *real_start, *real_end;
4869 int overlaps_p;
4870 {
4871 struct glyph_string *head, *tail;
4872 struct glyph_string *s;
4873 int last_x, area_width;
4874 int x_reached;
4875 int i, j;
4876
4877 /* Let's rather be paranoid than getting a SEGV. */
4878 end = min (end, row->used[area]);
4879 start = max (0, start);
4880 start = min (end, start);
4881 if (real_start)
4882 *real_start = start;
4883 if (real_end)
4884 *real_end = end;
4885
4886 /* Translate X to frame coordinates. Set last_x to the right
4887 end of the drawing area. */
4888 if (row->full_width_p)
4889 {
4890 /* X is relative to the left edge of W, without scroll bars
4891 or flag areas. */
4892 struct frame *f = XFRAME (w->frame);
4893 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4894 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4895
4896 x += window_left_x;
4897 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4898 last_x = window_left_x + area_width;
4899
4900 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4901 {
4902 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4903 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4904 last_x += width;
4905 else
4906 x -= width;
4907 }
4908
4909 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4910 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4911 }
4912 else
4913 {
4914 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4915 area_width = window_box_width (w, area);
4916 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4917 }
4918
4919 /* Build a doubly-linked list of glyph_string structures between
4920 head and tail from what we have to draw. Note that the macro
4921 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4922 the reason we use a separate variable `i'. */
4923 i = start;
4924 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4925 overlaps_p);
4926 if (tail)
4927 x_reached = tail->x + tail->background_width;
4928 else
4929 x_reached = x;
4930
4931 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4932 the row, redraw some glyphs in front or following the glyph
4933 strings built above. */
4934 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4935 {
4936 int dummy_x = 0;
4937 struct glyph_string *h, *t;
4938
4939 /* Compute overhangs for all glyph strings. */
4940 for (s = head; s; s = s->next)
4941 x_compute_glyph_string_overhangs (s);
4942
4943 /* Prepend glyph strings for glyphs in front of the first glyph
4944 string that are overwritten because of the first glyph
4945 string's left overhang. The background of all strings
4946 prepended must be drawn because the first glyph string
4947 draws over it. */
4948 i = x_left_overwritten (head);
4949 if (i >= 0)
4950 {
4951 j = i;
4952 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4953 DRAW_NORMAL_TEXT, dummy_x, last_x,
4954 overlaps_p);
4955 start = i;
4956 if (real_start)
4957 *real_start = start;
4958 x_compute_overhangs_and_x (t, head->x, 1);
4959 x_prepend_glyph_string_lists (&head, &tail, h, t);
4960 }
4961
4962 /* Prepend glyph strings for glyphs in front of the first glyph
4963 string that overwrite that glyph string because of their
4964 right overhang. For these strings, only the foreground must
4965 be drawn, because it draws over the glyph string at `head'.
4966 The background must not be drawn because this would overwrite
4967 right overhangs of preceding glyphs for which no glyph
4968 strings exist. */
4969 i = x_left_overwriting (head);
4970 if (i >= 0)
4971 {
4972 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4973 DRAW_NORMAL_TEXT, dummy_x, last_x,
4974 overlaps_p);
4975 for (s = h; s; s = s->next)
4976 s->background_filled_p = 1;
4977 if (real_start)
4978 *real_start = i;
4979 x_compute_overhangs_and_x (t, head->x, 1);
4980 x_prepend_glyph_string_lists (&head, &tail, h, t);
4981 }
4982
4983 /* Append glyphs strings for glyphs following the last glyph
4984 string tail that are overwritten by tail. The background of
4985 these strings has to be drawn because tail's foreground draws
4986 over it. */
4987 i = x_right_overwritten (tail);
4988 if (i >= 0)
4989 {
4990 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4991 DRAW_NORMAL_TEXT, x, last_x,
4992 overlaps_p);
4993 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4994 x_append_glyph_string_lists (&head, &tail, h, t);
4995 if (real_end)
4996 *real_end = i;
4997 }
4998
4999 /* Append glyph strings for glyphs following the last glyph
5000 string tail that overwrite tail. The foreground of such
5001 glyphs has to be drawn because it writes into the background
5002 of tail. The background must not be drawn because it could
5003 paint over the foreground of following glyphs. */
5004 i = x_right_overwriting (tail);
5005 if (i >= 0)
5006 {
5007 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5008 DRAW_NORMAL_TEXT, x, last_x,
5009 overlaps_p);
5010 for (s = h; s; s = s->next)
5011 s->background_filled_p = 1;
5012 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5013 x_append_glyph_string_lists (&head, &tail, h, t);
5014 if (real_end)
5015 *real_end = i;
5016 }
5017 }
5018
5019 /* Draw all strings. */
5020 for (s = head; s; s = s->next)
5021 x_draw_glyph_string (s);
5022
5023 /* Value is the x-position up to which drawn, relative to AREA of W.
5024 This doesn't include parts drawn because of overhangs. */
5025 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5026 if (!row->full_width_p)
5027 {
5028 if (area > LEFT_MARGIN_AREA)
5029 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5030 if (area > TEXT_AREA)
5031 x_reached -= window_box_width (w, TEXT_AREA);
5032 }
5033
5034 return x_reached;
5035 }
5036
5037
5038 /* Fix the display of area AREA of overlapping row ROW in window W. */
5039
5040 static void
5041 x_fix_overlapping_area (w, row, area)
5042 struct window *w;
5043 struct glyph_row *row;
5044 enum glyph_row_area area;
5045 {
5046 int i, x;
5047
5048 BLOCK_INPUT;
5049
5050 if (area == LEFT_MARGIN_AREA)
5051 x = 0;
5052 else if (area == TEXT_AREA)
5053 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5054 else
5055 x = (window_box_width (w, LEFT_MARGIN_AREA)
5056 + window_box_width (w, TEXT_AREA));
5057
5058 for (i = 0; i < row->used[area];)
5059 {
5060 if (row->glyphs[area][i].overlaps_vertically_p)
5061 {
5062 int start = i, start_x = x;
5063
5064 do
5065 {
5066 x += row->glyphs[area][i].pixel_width;
5067 ++i;
5068 }
5069 while (i < row->used[area]
5070 && row->glyphs[area][i].overlaps_vertically_p);
5071
5072 x_draw_glyphs (w, start_x, row, area, start, i,
5073 (row->inverse_p
5074 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5075 NULL, NULL, 1);
5076 }
5077 else
5078 {
5079 x += row->glyphs[area][i].pixel_width;
5080 ++i;
5081 }
5082 }
5083
5084 UNBLOCK_INPUT;
5085 }
5086
5087
5088 /* Output LEN glyphs starting at START at the nominal cursor position.
5089 Advance the nominal cursor over the text. The global variable
5090 updated_window contains the window being updated, updated_row is
5091 the glyph row being updated, and updated_area is the area of that
5092 row being updated. */
5093
5094 static void
5095 x_write_glyphs (start, len)
5096 struct glyph *start;
5097 int len;
5098 {
5099 int x, hpos, real_start, real_end;
5100
5101 xassert (updated_window && updated_row);
5102 BLOCK_INPUT;
5103
5104 /* Write glyphs. */
5105
5106 hpos = start - updated_row->glyphs[updated_area];
5107 x = x_draw_glyphs (updated_window, output_cursor.x,
5108 updated_row, updated_area,
5109 hpos, hpos + len,
5110 (updated_row->inverse_p
5111 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5112 &real_start, &real_end, 0);
5113
5114 /* If we drew over the cursor, note that it is not visible any more. */
5115 note_overwritten_text_cursor (updated_window, real_start,
5116 real_end - real_start);
5117
5118 UNBLOCK_INPUT;
5119
5120 /* Advance the output cursor. */
5121 output_cursor.hpos += len;
5122 output_cursor.x = x;
5123 }
5124
5125
5126 /* Insert LEN glyphs from START at the nominal cursor position. */
5127
5128 static void
5129 x_insert_glyphs (start, len)
5130 struct glyph *start;
5131 register int len;
5132 {
5133 struct frame *f;
5134 struct window *w;
5135 int line_height, shift_by_width, shifted_region_width;
5136 struct glyph_row *row;
5137 struct glyph *glyph;
5138 int frame_x, frame_y, hpos, real_start, real_end;
5139
5140 xassert (updated_window && updated_row);
5141 BLOCK_INPUT;
5142 w = updated_window;
5143 f = XFRAME (WINDOW_FRAME (w));
5144
5145 /* Get the height of the line we are in. */
5146 row = updated_row;
5147 line_height = row->height;
5148
5149 /* Get the width of the glyphs to insert. */
5150 shift_by_width = 0;
5151 for (glyph = start; glyph < start + len; ++glyph)
5152 shift_by_width += glyph->pixel_width;
5153
5154 /* Get the width of the region to shift right. */
5155 shifted_region_width = (window_box_width (w, updated_area)
5156 - output_cursor.x
5157 - shift_by_width);
5158
5159 /* Shift right. */
5160 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5161 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5162 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5163 f->output_data.x->normal_gc,
5164 frame_x, frame_y,
5165 shifted_region_width, line_height,
5166 frame_x + shift_by_width, frame_y);
5167
5168 /* Write the glyphs. */
5169 hpos = start - row->glyphs[updated_area];
5170 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5171 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5172 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5173
5174 /* Advance the output cursor. */
5175 output_cursor.hpos += len;
5176 output_cursor.x += shift_by_width;
5177 UNBLOCK_INPUT;
5178 }
5179
5180
5181 /* Delete N glyphs at the nominal cursor position. Not implemented
5182 for X frames. */
5183
5184 static void
5185 x_delete_glyphs (n)
5186 register int n;
5187 {
5188 abort ();
5189 }
5190
5191
5192 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5193 If they are <= 0, this is probably an error. */
5194
5195 void
5196 x_clear_area (dpy, window, x, y, width, height, exposures)
5197 Display *dpy;
5198 Window window;
5199 int x, y;
5200 int width, height;
5201 int exposures;
5202 {
5203 xassert (width > 0 && height > 0);
5204 XClearArea (dpy, window, x, y, width, height, exposures);
5205 }
5206
5207
5208 /* Erase the current text line from the nominal cursor position
5209 (inclusive) to pixel column TO_X (exclusive). The idea is that
5210 everything from TO_X onward is already erased.
5211
5212 TO_X is a pixel position relative to updated_area of
5213 updated_window. TO_X == -1 means clear to the end of this area. */
5214
5215 static void
5216 x_clear_end_of_line (to_x)
5217 int to_x;
5218 {
5219 struct frame *f;
5220 struct window *w = updated_window;
5221 int max_x, min_y, max_y;
5222 int from_x, from_y, to_y;
5223
5224 xassert (updated_window && updated_row);
5225 f = XFRAME (w->frame);
5226
5227 if (updated_row->full_width_p)
5228 {
5229 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5230 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5231 && !w->pseudo_window_p)
5232 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5233 }
5234 else
5235 max_x = window_box_width (w, updated_area);
5236 max_y = window_text_bottom_y (w);
5237
5238 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5239 of window. For TO_X > 0, truncate to end of drawing area. */
5240 if (to_x == 0)
5241 return;
5242 else if (to_x < 0)
5243 to_x = max_x;
5244 else
5245 to_x = min (to_x, max_x);
5246
5247 to_y = min (max_y, output_cursor.y + updated_row->height);
5248
5249 /* Notice if the cursor will be cleared by this operation. */
5250 if (!updated_row->full_width_p)
5251 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5252
5253 from_x = output_cursor.x;
5254
5255 /* Translate to frame coordinates. */
5256 if (updated_row->full_width_p)
5257 {
5258 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5259 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5260 }
5261 else
5262 {
5263 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5264 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5265 }
5266
5267 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5268 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5269 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5270
5271 /* Prevent inadvertently clearing to end of the X window. */
5272 if (to_x > from_x && to_y > from_y)
5273 {
5274 BLOCK_INPUT;
5275 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5276 from_x, from_y, to_x - from_x, to_y - from_y,
5277 False);
5278 UNBLOCK_INPUT;
5279 }
5280 }
5281
5282
5283 /* Clear entire frame. If updating_frame is non-null, clear that
5284 frame. Otherwise clear the selected frame. */
5285
5286 static void
5287 x_clear_frame ()
5288 {
5289 struct frame *f;
5290
5291 if (updating_frame)
5292 f = updating_frame;
5293 else
5294 f = SELECTED_FRAME ();
5295
5296 /* Clearing the frame will erase any cursor, so mark them all as no
5297 longer visible. */
5298 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5299 output_cursor.hpos = output_cursor.vpos = 0;
5300 output_cursor.x = -1;
5301
5302 /* We don't set the output cursor here because there will always
5303 follow an explicit cursor_to. */
5304 BLOCK_INPUT;
5305 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5306
5307 /* We have to clear the scroll bars, too. If we have changed
5308 colors or something like that, then they should be notified. */
5309 x_scroll_bar_clear (f);
5310
5311 XFlush (FRAME_X_DISPLAY (f));
5312 UNBLOCK_INPUT;
5313 }
5314
5315
5316 \f
5317 /* Invert the middle quarter of the frame for .15 sec. */
5318
5319 /* We use the select system call to do the waiting, so we have to make
5320 sure it's available. If it isn't, we just won't do visual bells. */
5321
5322 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5323
5324
5325 /* Subtract the `struct timeval' values X and Y, storing the result in
5326 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5327
5328 static int
5329 timeval_subtract (result, x, y)
5330 struct timeval *result, x, y;
5331 {
5332 /* Perform the carry for the later subtraction by updating y. This
5333 is safer because on some systems the tv_sec member is unsigned. */
5334 if (x.tv_usec < y.tv_usec)
5335 {
5336 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5337 y.tv_usec -= 1000000 * nsec;
5338 y.tv_sec += nsec;
5339 }
5340
5341 if (x.tv_usec - y.tv_usec > 1000000)
5342 {
5343 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5344 y.tv_usec += 1000000 * nsec;
5345 y.tv_sec -= nsec;
5346 }
5347
5348 /* Compute the time remaining to wait. tv_usec is certainly
5349 positive. */
5350 result->tv_sec = x.tv_sec - y.tv_sec;
5351 result->tv_usec = x.tv_usec - y.tv_usec;
5352
5353 /* Return indication of whether the result should be considered
5354 negative. */
5355 return x.tv_sec < y.tv_sec;
5356 }
5357
5358 void
5359 XTflash (f)
5360 struct frame *f;
5361 {
5362 BLOCK_INPUT;
5363
5364 {
5365 GC gc;
5366
5367 /* Create a GC that will use the GXxor function to flip foreground
5368 pixels into background pixels. */
5369 {
5370 XGCValues values;
5371
5372 values.function = GXxor;
5373 values.foreground = (f->output_data.x->foreground_pixel
5374 ^ f->output_data.x->background_pixel);
5375
5376 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5377 GCFunction | GCForeground, &values);
5378 }
5379
5380 {
5381 /* Get the height not including a menu bar widget. */
5382 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5383 /* Height of each line to flash. */
5384 int flash_height = FRAME_LINE_HEIGHT (f);
5385 /* These will be the left and right margins of the rectangles. */
5386 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5387 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5388
5389 int width;
5390
5391 /* Don't flash the area between a scroll bar and the frame
5392 edge it is next to. */
5393 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5394 {
5395 case vertical_scroll_bar_left:
5396 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5397 break;
5398
5399 case vertical_scroll_bar_right:
5400 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5401 break;
5402
5403 default:
5404 break;
5405 }
5406
5407 width = flash_right - flash_left;
5408
5409 /* If window is tall, flash top and bottom line. */
5410 if (height > 3 * FRAME_LINE_HEIGHT (f))
5411 {
5412 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5413 flash_left,
5414 (FRAME_INTERNAL_BORDER_WIDTH (f)
5415 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5416 width, flash_height);
5417 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5418 flash_left,
5419 (height - flash_height
5420 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5421 width, flash_height);
5422 }
5423 else
5424 /* If it is short, flash it all. */
5425 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5426 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5427 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5428
5429 x_flush (f);
5430
5431 {
5432 struct timeval wakeup;
5433
5434 EMACS_GET_TIME (wakeup);
5435
5436 /* Compute time to wait until, propagating carry from usecs. */
5437 wakeup.tv_usec += 150000;
5438 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5439 wakeup.tv_usec %= 1000000;
5440
5441 /* Keep waiting until past the time wakeup or any input gets
5442 available. */
5443 while (! detect_input_pending ())
5444 {
5445 struct timeval current;
5446 struct timeval timeout;
5447
5448 EMACS_GET_TIME (current);
5449
5450 /* Break if result would be negative. */
5451 if (timeval_subtract (&current, wakeup, current))
5452 break;
5453
5454 /* How long `select' should wait. */
5455 timeout.tv_sec = 0;
5456 timeout.tv_usec = 10000;
5457
5458 /* Try to wait that long--but we might wake up sooner. */
5459 select (0, NULL, NULL, NULL, &timeout);
5460 }
5461 }
5462
5463 /* If window is tall, flash top and bottom line. */
5464 if (height > 3 * FRAME_LINE_HEIGHT (f))
5465 {
5466 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5467 flash_left,
5468 (FRAME_INTERNAL_BORDER_WIDTH (f)
5469 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5470 width, flash_height);
5471 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5472 flash_left,
5473 (height - flash_height
5474 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5475 width, flash_height);
5476 }
5477 else
5478 /* If it is short, flash it all. */
5479 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5480 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5481 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5482
5483 XFreeGC (FRAME_X_DISPLAY (f), gc);
5484 x_flush (f);
5485 }
5486 }
5487
5488 UNBLOCK_INPUT;
5489 }
5490
5491 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5492
5493
5494 /* Make audible bell. */
5495
5496 void
5497 XTring_bell ()
5498 {
5499 struct frame *f = SELECTED_FRAME ();
5500
5501 if (FRAME_X_DISPLAY (f))
5502 {
5503 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5504 if (visible_bell)
5505 XTflash (f);
5506 else
5507 #endif
5508 {
5509 BLOCK_INPUT;
5510 XBell (FRAME_X_DISPLAY (f), 0);
5511 XFlush (FRAME_X_DISPLAY (f));
5512 UNBLOCK_INPUT;
5513 }
5514 }
5515 }
5516
5517 \f
5518 /* Specify how many text lines, from the top of the window,
5519 should be affected by insert-lines and delete-lines operations.
5520 This, and those operations, are used only within an update
5521 that is bounded by calls to x_update_begin and x_update_end. */
5522
5523 static void
5524 XTset_terminal_window (n)
5525 register int n;
5526 {
5527 /* This function intentionally left blank. */
5528 }
5529
5530
5531 \f
5532 /***********************************************************************
5533 Line Dance
5534 ***********************************************************************/
5535
5536 /* Perform an insert-lines or delete-lines operation, inserting N
5537 lines or deleting -N lines at vertical position VPOS. */
5538
5539 static void
5540 x_ins_del_lines (vpos, n)
5541 int vpos, n;
5542 {
5543 abort ();
5544 }
5545
5546
5547 /* Scroll part of the display as described by RUN. */
5548
5549 static void
5550 x_scroll_run (w, run)
5551 struct window *w;
5552 struct run *run;
5553 {
5554 struct frame *f = XFRAME (w->frame);
5555 int x, y, width, height, from_y, to_y, bottom_y;
5556
5557 /* Get frame-relative bounding box of the text display area of W,
5558 without mode lines. Include in this box the flags areas to the
5559 left and right of W. */
5560 window_box (w, -1, &x, &y, &width, &height);
5561 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5562 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5563
5564 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5565 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5566 bottom_y = y + height;
5567
5568 if (to_y < from_y)
5569 {
5570 /* Scrolling up. Make sure we don't copy part of the mode
5571 line at the bottom. */
5572 if (from_y + run->height > bottom_y)
5573 height = bottom_y - from_y;
5574 else
5575 height = run->height;
5576 }
5577 else
5578 {
5579 /* Scolling down. Make sure we don't copy over the mode line.
5580 at the bottom. */
5581 if (to_y + run->height > bottom_y)
5582 height = bottom_y - to_y;
5583 else
5584 height = run->height;
5585 }
5586
5587 BLOCK_INPUT;
5588
5589 /* Cursor off. Will be switched on again in x_update_window_end. */
5590 updated_window = w;
5591 x_clear_cursor (w);
5592
5593 XCopyArea (FRAME_X_DISPLAY (f),
5594 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5595 f->output_data.x->normal_gc,
5596 x, from_y,
5597 width, height,
5598 x, to_y);
5599
5600 UNBLOCK_INPUT;
5601 }
5602
5603
5604 \f
5605 /***********************************************************************
5606 Exposure Events
5607 ***********************************************************************/
5608
5609 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5610 corner of the exposed rectangle. W and H are width and height of
5611 the exposed area. All are pixel values. W or H zero means redraw
5612 the entire frame. */
5613
5614 static void
5615 expose_frame (f, x, y, w, h)
5616 struct frame *f;
5617 int x, y, w, h;
5618 {
5619 XRectangle r;
5620
5621 TRACE ((stderr, "expose_frame "));
5622
5623 /* No need to redraw if frame will be redrawn soon. */
5624 if (FRAME_GARBAGED_P (f))
5625 {
5626 TRACE ((stderr, " garbaged\n"));
5627 return;
5628 }
5629
5630 /* If basic faces haven't been realized yet, there is no point in
5631 trying to redraw anything. This can happen when we get an expose
5632 event while Emacs is starting, e.g. by moving another window. */
5633 if (FRAME_FACE_CACHE (f) == NULL
5634 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5635 {
5636 TRACE ((stderr, " no faces\n"));
5637 return;
5638 }
5639
5640 if (w == 0 || h == 0)
5641 {
5642 r.x = r.y = 0;
5643 r.width = CANON_X_UNIT (f) * f->width;
5644 r.height = CANON_Y_UNIT (f) * f->height;
5645 }
5646 else
5647 {
5648 r.x = x;
5649 r.y = y;
5650 r.width = w;
5651 r.height = h;
5652 }
5653
5654 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5655 expose_window_tree (XWINDOW (f->root_window), &r);
5656
5657 if (WINDOWP (f->tool_bar_window))
5658 {
5659 struct window *w = XWINDOW (f->tool_bar_window);
5660 XRectangle window_rect;
5661 XRectangle intersection_rect;
5662 int window_x, window_y, window_width, window_height;
5663
5664
5665 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5666 window_rect.x = window_x;
5667 window_rect.y = window_y;
5668 window_rect.width = window_width;
5669 window_rect.height = window_height;
5670
5671 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5672 expose_window (w, &intersection_rect);
5673 }
5674
5675 #ifndef USE_X_TOOLKIT
5676 if (WINDOWP (f->menu_bar_window))
5677 {
5678 struct window *w = XWINDOW (f->menu_bar_window);
5679 XRectangle window_rect;
5680 XRectangle intersection_rect;
5681 int window_x, window_y, window_width, window_height;
5682
5683
5684 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5685 window_rect.x = window_x;
5686 window_rect.y = window_y;
5687 window_rect.width = window_width;
5688 window_rect.height = window_height;
5689
5690 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5691 expose_window (w, &intersection_rect);
5692 }
5693 #endif /* not USE_X_TOOLKIT */
5694 }
5695
5696
5697 /* Redraw (parts) of all windows in the window tree rooted at W that
5698 intersect R. R contains frame pixel coordinates. */
5699
5700 static void
5701 expose_window_tree (w, r)
5702 struct window *w;
5703 XRectangle *r;
5704 {
5705 while (w)
5706 {
5707 if (!NILP (w->hchild))
5708 expose_window_tree (XWINDOW (w->hchild), r);
5709 else if (!NILP (w->vchild))
5710 expose_window_tree (XWINDOW (w->vchild), r);
5711 else
5712 {
5713 XRectangle window_rect;
5714 XRectangle intersection_rect;
5715 struct frame *f = XFRAME (w->frame);
5716 int window_x, window_y, window_width, window_height;
5717
5718 /* Frame-relative pixel rectangle of W. */
5719 window_box (w, -1, &window_x, &window_y, &window_width,
5720 &window_height);
5721 window_rect.x
5722 = (window_x
5723 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5724 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5725 window_rect.y = window_y;
5726 window_rect.width
5727 = (window_width
5728 + FRAME_X_FLAGS_AREA_WIDTH (f)
5729 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5730 window_rect.height
5731 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5732
5733 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5734 expose_window (w, &intersection_rect);
5735 }
5736
5737 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5738 }
5739 }
5740
5741
5742 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5743 which intersects rectangle R. R is in window-relative coordinates. */
5744
5745 static void
5746 expose_area (w, row, r, area)
5747 struct window *w;
5748 struct glyph_row *row;
5749 XRectangle *r;
5750 enum glyph_row_area area;
5751 {
5752 struct glyph *first = row->glyphs[area];
5753 struct glyph *end = row->glyphs[area] + row->used[area];
5754 struct glyph *last;
5755 int first_x, start_x, x;
5756
5757 if (area == TEXT_AREA && row->fill_line_p)
5758 /* If row extends face to end of line write the whole line. */
5759 x_draw_glyphs (w, 0, row, area,
5760 0, row->used[area],
5761 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5762 NULL, NULL, 0);
5763 else
5764 {
5765 /* Set START_X to the window-relative start position for drawing glyphs of
5766 AREA. The first glyph of the text area can be partially visible.
5767 The first glyphs of other areas cannot. */
5768 if (area == LEFT_MARGIN_AREA)
5769 start_x = 0;
5770 else if (area == TEXT_AREA)
5771 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5772 else
5773 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5774 + window_box_width (w, TEXT_AREA));
5775 x = start_x;
5776
5777 /* Find the first glyph that must be redrawn. */
5778 while (first < end
5779 && x + first->pixel_width < r->x)
5780 {
5781 x += first->pixel_width;
5782 ++first;
5783 }
5784
5785 /* Find the last one. */
5786 last = first;
5787 first_x = x;
5788 while (last < end
5789 && x < r->x + r->width)
5790 {
5791 x += last->pixel_width;
5792 ++last;
5793 }
5794
5795 /* Repaint. */
5796 if (last > first)
5797 x_draw_glyphs (w, first_x - start_x, row, area,
5798 first - row->glyphs[area],
5799 last - row->glyphs[area],
5800 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5801 NULL, NULL, 0);
5802 }
5803 }
5804
5805
5806 /* Redraw the parts of the glyph row ROW on window W intersecting
5807 rectangle R. R is in window-relative coordinates. */
5808
5809 static void
5810 expose_line (w, row, r)
5811 struct window *w;
5812 struct glyph_row *row;
5813 XRectangle *r;
5814 {
5815 xassert (row->enabled_p);
5816
5817 if (row->mode_line_p || w->pseudo_window_p)
5818 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5819 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5820 NULL, NULL, 0);
5821 else
5822 {
5823 if (row->used[LEFT_MARGIN_AREA])
5824 expose_area (w, row, r, LEFT_MARGIN_AREA);
5825 if (row->used[TEXT_AREA])
5826 expose_area (w, row, r, TEXT_AREA);
5827 if (row->used[RIGHT_MARGIN_AREA])
5828 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5829 x_draw_row_bitmaps (w, row);
5830 }
5831 }
5832
5833
5834 /* Return non-zero if W's cursor intersects rectangle R. */
5835
5836 static int
5837 x_phys_cursor_in_rect_p (w, r)
5838 struct window *w;
5839 XRectangle *r;
5840 {
5841 XRectangle cr, result;
5842 struct glyph *cursor_glyph;
5843
5844 cursor_glyph = get_phys_cursor_glyph (w);
5845 if (cursor_glyph)
5846 {
5847 cr.x = w->phys_cursor.x;
5848 cr.y = w->phys_cursor.y;
5849 cr.width = cursor_glyph->pixel_width;
5850 cr.height = w->phys_cursor_height;
5851 return x_intersect_rectangles (&cr, r, &result);
5852 }
5853 else
5854 return 0;
5855 }
5856
5857
5858 /* Redraw a rectangle of window W. R is a rectangle in window
5859 relative coordinates. Call this function with input blocked. */
5860
5861 static void
5862 expose_window (w, r)
5863 struct window *w;
5864 XRectangle *r;
5865 {
5866 struct glyph_row *row;
5867 int y;
5868 int yb = window_text_bottom_y (w);
5869 int cursor_cleared_p;
5870
5871 /* If window is not yet fully initialized, do nothing. This can
5872 happen when toolkit scroll bars are used and a window is split.
5873 Reconfiguring the scroll bar will generate an expose for a newly
5874 created window. */
5875 if (w->current_matrix == NULL || w == updated_window)
5876 return;
5877
5878 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5879 r->x, r->y, r->width, r->height));
5880
5881 /* Convert to window coordinates. */
5882 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5883 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5884
5885 /* Turn off the cursor. */
5886 if (!w->pseudo_window_p
5887 && x_phys_cursor_in_rect_p (w, r))
5888 {
5889 x_clear_cursor (w);
5890 cursor_cleared_p = 1;
5891 }
5892 else
5893 cursor_cleared_p = 0;
5894
5895 /* Find the first row intersecting the rectangle R. */
5896 row = w->current_matrix->rows;
5897 y = 0;
5898 while (row->enabled_p
5899 && y < yb
5900 && y + row->height < r->y)
5901 {
5902 y += row->height;
5903 ++row;
5904 }
5905
5906 /* Display the text in the rectangle, one text line at a time. */
5907 while (row->enabled_p
5908 && y < yb
5909 && y < r->y + r->height)
5910 {
5911 expose_line (w, row, r);
5912 y += row->height;
5913 ++row;
5914 }
5915
5916 /* Display the mode line if there is one. */
5917 if (WINDOW_WANTS_MODELINE_P (w)
5918 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5919 row->enabled_p)
5920 && row->y < r->y + r->height)
5921 expose_line (w, row, r);
5922
5923 if (!w->pseudo_window_p)
5924 {
5925 /* Draw border between windows. */
5926 x_draw_vertical_border (w);
5927
5928 /* Turn the cursor on again. */
5929 if (cursor_cleared_p)
5930 x_update_window_cursor (w, 1);
5931 }
5932 }
5933
5934
5935 /* Determine the intersection of two rectangles R1 and R2. Return
5936 the intersection in *RESULT. Value is non-zero if RESULT is not
5937 empty. */
5938
5939 static int
5940 x_intersect_rectangles (r1, r2, result)
5941 XRectangle *r1, *r2, *result;
5942 {
5943 XRectangle *left, *right;
5944 XRectangle *upper, *lower;
5945 int intersection_p = 0;
5946
5947 /* Rearrange so that R1 is the left-most rectangle. */
5948 if (r1->x < r2->x)
5949 left = r1, right = r2;
5950 else
5951 left = r2, right = r1;
5952
5953 /* X0 of the intersection is right.x0, if this is inside R1,
5954 otherwise there is no intersection. */
5955 if (right->x <= left->x + left->width)
5956 {
5957 result->x = right->x;
5958
5959 /* The right end of the intersection is the minimum of the
5960 the right ends of left and right. */
5961 result->width = (min (left->x + left->width, right->x + right->width)
5962 - result->x);
5963
5964 /* Same game for Y. */
5965 if (r1->y < r2->y)
5966 upper = r1, lower = r2;
5967 else
5968 upper = r2, lower = r1;
5969
5970 /* The upper end of the intersection is lower.y0, if this is inside
5971 of upper. Otherwise, there is no intersection. */
5972 if (lower->y <= upper->y + upper->height)
5973 {
5974 result->y = lower->y;
5975
5976 /* The lower end of the intersection is the minimum of the lower
5977 ends of upper and lower. */
5978 result->height = (min (lower->y + lower->height,
5979 upper->y + upper->height)
5980 - result->y);
5981 intersection_p = 1;
5982 }
5983 }
5984
5985 return intersection_p;
5986 }
5987
5988
5989
5990
5991 \f
5992 static void
5993 frame_highlight (f)
5994 struct frame *f;
5995 {
5996 /* We used to only do this if Vx_no_window_manager was non-nil, but
5997 the ICCCM (section 4.1.6) says that the window's border pixmap
5998 and border pixel are window attributes which are "private to the
5999 client", so we can always change it to whatever we want. */
6000 BLOCK_INPUT;
6001 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6002 f->output_data.x->border_pixel);
6003 UNBLOCK_INPUT;
6004 x_update_cursor (f, 1);
6005 }
6006
6007 static void
6008 frame_unhighlight (f)
6009 struct frame *f;
6010 {
6011 /* We used to only do this if Vx_no_window_manager was non-nil, but
6012 the ICCCM (section 4.1.6) says that the window's border pixmap
6013 and border pixel are window attributes which are "private to the
6014 client", so we can always change it to whatever we want. */
6015 BLOCK_INPUT;
6016 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6017 f->output_data.x->border_tile);
6018 UNBLOCK_INPUT;
6019 x_update_cursor (f, 1);
6020 }
6021
6022 /* The focus has changed. Update the frames as necessary to reflect
6023 the new situation. Note that we can't change the selected frame
6024 here, because the Lisp code we are interrupting might become confused.
6025 Each event gets marked with the frame in which it occurred, so the
6026 Lisp code can tell when the switch took place by examining the events. */
6027
6028 static void
6029 x_new_focus_frame (dpyinfo, frame)
6030 struct x_display_info *dpyinfo;
6031 struct frame *frame;
6032 {
6033 struct frame *old_focus = dpyinfo->x_focus_frame;
6034
6035 if (frame != dpyinfo->x_focus_frame)
6036 {
6037 /* Set this before calling other routines, so that they see
6038 the correct value of x_focus_frame. */
6039 dpyinfo->x_focus_frame = frame;
6040
6041 if (old_focus && old_focus->auto_lower)
6042 x_lower_frame (old_focus);
6043
6044 #if 0
6045 selected_frame = frame;
6046 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6047 selected_frame);
6048 Fselect_window (selected_frame->selected_window);
6049 choose_minibuf_frame ();
6050 #endif /* ! 0 */
6051
6052 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6053 pending_autoraise_frame = dpyinfo->x_focus_frame;
6054 else
6055 pending_autoraise_frame = 0;
6056 }
6057
6058 x_frame_rehighlight (dpyinfo);
6059 }
6060
6061 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6062
6063 void
6064 x_mouse_leave (dpyinfo)
6065 struct x_display_info *dpyinfo;
6066 {
6067 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6068 }
6069
6070 /* The focus has changed, or we have redirected a frame's focus to
6071 another frame (this happens when a frame uses a surrogate
6072 mini-buffer frame). Shift the highlight as appropriate.
6073
6074 The FRAME argument doesn't necessarily have anything to do with which
6075 frame is being highlighted or un-highlighted; we only use it to find
6076 the appropriate X display info. */
6077
6078 static void
6079 XTframe_rehighlight (frame)
6080 struct frame *frame;
6081 {
6082 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6083 }
6084
6085 static void
6086 x_frame_rehighlight (dpyinfo)
6087 struct x_display_info *dpyinfo;
6088 {
6089 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6090
6091 if (dpyinfo->x_focus_frame)
6092 {
6093 dpyinfo->x_highlight_frame
6094 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6095 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6096 : dpyinfo->x_focus_frame);
6097 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6098 {
6099 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6100 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6101 }
6102 }
6103 else
6104 dpyinfo->x_highlight_frame = 0;
6105
6106 if (dpyinfo->x_highlight_frame != old_highlight)
6107 {
6108 if (old_highlight)
6109 frame_unhighlight (old_highlight);
6110 if (dpyinfo->x_highlight_frame)
6111 frame_highlight (dpyinfo->x_highlight_frame);
6112 }
6113 }
6114
6115
6116 \f
6117 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6118
6119 /* Initialize mode_switch_bit and modifier_meaning. */
6120 static void
6121 x_find_modifier_meanings (dpyinfo)
6122 struct x_display_info *dpyinfo;
6123 {
6124 int min_code, max_code;
6125 KeySym *syms;
6126 int syms_per_code;
6127 XModifierKeymap *mods;
6128
6129 dpyinfo->meta_mod_mask = 0;
6130 dpyinfo->shift_lock_mask = 0;
6131 dpyinfo->alt_mod_mask = 0;
6132 dpyinfo->super_mod_mask = 0;
6133 dpyinfo->hyper_mod_mask = 0;
6134
6135 #ifdef HAVE_X11R4
6136 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6137 #else
6138 min_code = dpyinfo->display->min_keycode;
6139 max_code = dpyinfo->display->max_keycode;
6140 #endif
6141
6142 syms = XGetKeyboardMapping (dpyinfo->display,
6143 min_code, max_code - min_code + 1,
6144 &syms_per_code);
6145 mods = XGetModifierMapping (dpyinfo->display);
6146
6147 /* Scan the modifier table to see which modifier bits the Meta and
6148 Alt keysyms are on. */
6149 {
6150 int row, col; /* The row and column in the modifier table. */
6151
6152 for (row = 3; row < 8; row++)
6153 for (col = 0; col < mods->max_keypermod; col++)
6154 {
6155 KeyCode code
6156 = mods->modifiermap[(row * mods->max_keypermod) + col];
6157
6158 /* Zeroes are used for filler. Skip them. */
6159 if (code == 0)
6160 continue;
6161
6162 /* Are any of this keycode's keysyms a meta key? */
6163 {
6164 int code_col;
6165
6166 for (code_col = 0; code_col < syms_per_code; code_col++)
6167 {
6168 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6169
6170 switch (sym)
6171 {
6172 case XK_Meta_L:
6173 case XK_Meta_R:
6174 dpyinfo->meta_mod_mask |= (1 << row);
6175 break;
6176
6177 case XK_Alt_L:
6178 case XK_Alt_R:
6179 dpyinfo->alt_mod_mask |= (1 << row);
6180 break;
6181
6182 case XK_Hyper_L:
6183 case XK_Hyper_R:
6184 dpyinfo->hyper_mod_mask |= (1 << row);
6185 break;
6186
6187 case XK_Super_L:
6188 case XK_Super_R:
6189 dpyinfo->super_mod_mask |= (1 << row);
6190 break;
6191
6192 case XK_Shift_Lock:
6193 /* Ignore this if it's not on the lock modifier. */
6194 if ((1 << row) == LockMask)
6195 dpyinfo->shift_lock_mask = LockMask;
6196 break;
6197 }
6198 }
6199 }
6200 }
6201 }
6202
6203 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6204 if (! dpyinfo->meta_mod_mask)
6205 {
6206 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6207 dpyinfo->alt_mod_mask = 0;
6208 }
6209
6210 /* If some keys are both alt and meta,
6211 make them just meta, not alt. */
6212 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6213 {
6214 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6215 }
6216
6217 XFree ((char *) syms);
6218 XFreeModifiermap (mods);
6219 }
6220
6221 /* Convert between the modifier bits X uses and the modifier bits
6222 Emacs uses. */
6223
6224 static unsigned int
6225 x_x_to_emacs_modifiers (dpyinfo, state)
6226 struct x_display_info *dpyinfo;
6227 unsigned int state;
6228 {
6229 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6230 | ((state & ControlMask) ? ctrl_modifier : 0)
6231 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6232 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6233 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6234 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6235 }
6236
6237 static unsigned int
6238 x_emacs_to_x_modifiers (dpyinfo, state)
6239 struct x_display_info *dpyinfo;
6240 unsigned int state;
6241 {
6242 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6243 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6244 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6245 | ((state & shift_modifier) ? ShiftMask : 0)
6246 | ((state & ctrl_modifier) ? ControlMask : 0)
6247 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6248 }
6249
6250 /* Convert a keysym to its name. */
6251
6252 char *
6253 x_get_keysym_name (keysym)
6254 KeySym keysym;
6255 {
6256 char *value;
6257
6258 BLOCK_INPUT;
6259 value = XKeysymToString (keysym);
6260 UNBLOCK_INPUT;
6261
6262 return value;
6263 }
6264
6265
6266 \f
6267 /* Mouse clicks and mouse movement. Rah. */
6268
6269 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6270 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6271 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6272 not force the value into range. */
6273
6274 void
6275 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6276 FRAME_PTR f;
6277 register int pix_x, pix_y;
6278 register int *x, *y;
6279 XRectangle *bounds;
6280 int noclip;
6281 {
6282 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6283 even for negative values. */
6284 if (pix_x < 0)
6285 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6286 if (pix_y < 0)
6287 pix_y -= (f)->output_data.x->line_height - 1;
6288
6289 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6290 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6291
6292 if (bounds)
6293 {
6294 bounds->width = FONT_WIDTH (f->output_data.x->font);
6295 bounds->height = f->output_data.x->line_height;
6296 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6297 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6298 }
6299
6300 if (!noclip)
6301 {
6302 if (pix_x < 0)
6303 pix_x = 0;
6304 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6305 pix_x = FRAME_WINDOW_WIDTH (f);
6306
6307 if (pix_y < 0)
6308 pix_y = 0;
6309 else if (pix_y > f->height)
6310 pix_y = f->height;
6311 }
6312
6313 *x = pix_x;
6314 *y = pix_y;
6315 }
6316
6317
6318 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6319 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6320 can't tell the positions because W's display is not up to date,
6321 return 0. */
6322
6323 int
6324 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6325 struct window *w;
6326 int hpos, vpos;
6327 int *frame_x, *frame_y;
6328 {
6329 int success_p;
6330
6331 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6332 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6333
6334 if (display_completed)
6335 {
6336 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6337 struct glyph *glyph = row->glyphs[TEXT_AREA];
6338 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6339
6340 *frame_y = row->y;
6341 *frame_x = row->x;
6342 while (glyph < end)
6343 {
6344 *frame_x += glyph->pixel_width;
6345 ++glyph;
6346 }
6347
6348 success_p = 1;
6349 }
6350 else
6351 {
6352 *frame_y = *frame_x = 0;
6353 success_p = 0;
6354 }
6355
6356 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6357 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6358 return success_p;
6359 }
6360
6361
6362 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6363
6364 If the event is a button press, then note that we have grabbed
6365 the mouse. */
6366
6367 static Lisp_Object
6368 construct_mouse_click (result, event, f)
6369 struct input_event *result;
6370 XButtonEvent *event;
6371 struct frame *f;
6372 {
6373 /* Make the event type no_event; we'll change that when we decide
6374 otherwise. */
6375 result->kind = mouse_click;
6376 result->code = event->button - Button1;
6377 result->timestamp = event->time;
6378 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6379 event->state)
6380 | (event->type == ButtonRelease
6381 ? up_modifier
6382 : down_modifier));
6383
6384 XSETINT (result->x, event->x);
6385 XSETINT (result->y, event->y);
6386 XSETFRAME (result->frame_or_window, f);
6387 result->arg = Qnil;
6388 return Qnil;
6389 }
6390
6391 \f
6392 /* Function to report a mouse movement to the mainstream Emacs code.
6393 The input handler calls this.
6394
6395 We have received a mouse movement event, which is given in *event.
6396 If the mouse is over a different glyph than it was last time, tell
6397 the mainstream emacs code by setting mouse_moved. If not, ask for
6398 another motion event, so we can check again the next time it moves. */
6399
6400 static XMotionEvent last_mouse_motion_event;
6401 static Lisp_Object last_mouse_motion_frame;
6402
6403 static void
6404 note_mouse_movement (frame, event)
6405 FRAME_PTR frame;
6406 XMotionEvent *event;
6407 {
6408 last_mouse_movement_time = event->time;
6409 last_mouse_motion_event = *event;
6410 XSETFRAME (last_mouse_motion_frame, frame);
6411
6412 if (event->window != FRAME_X_WINDOW (frame))
6413 {
6414 frame->mouse_moved = 1;
6415 last_mouse_scroll_bar = Qnil;
6416 note_mouse_highlight (frame, -1, -1);
6417 }
6418
6419 /* Has the mouse moved off the glyph it was on at the last sighting? */
6420 else if (event->x < last_mouse_glyph.x
6421 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6422 || event->y < last_mouse_glyph.y
6423 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6424 {
6425 frame->mouse_moved = 1;
6426 last_mouse_scroll_bar = Qnil;
6427 note_mouse_highlight (frame, event->x, event->y);
6428 }
6429 }
6430
6431 /* This is used for debugging, to turn off note_mouse_highlight. */
6432
6433 int disable_mouse_highlight;
6434
6435
6436 \f
6437 /************************************************************************
6438 Mouse Face
6439 ************************************************************************/
6440
6441 /* Find the glyph under window-relative coordinates X/Y in window W.
6442 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6443 strings. Return in *HPOS and *VPOS the row and column number of
6444 the glyph found. Return in *AREA the glyph area containing X.
6445 Value is a pointer to the glyph found or null if X/Y is not on
6446 text, or we can't tell because W's current matrix is not up to
6447 date. */
6448
6449 static struct glyph *
6450 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6451 struct window *w;
6452 int x, y;
6453 int *hpos, *vpos, *area;
6454 int buffer_only_p;
6455 {
6456 struct glyph *glyph, *end;
6457 struct glyph_row *row = NULL;
6458 int x0, i, left_area_width;
6459
6460 /* Find row containing Y. Give up if some row is not enabled. */
6461 for (i = 0; i < w->current_matrix->nrows; ++i)
6462 {
6463 row = MATRIX_ROW (w->current_matrix, i);
6464 if (!row->enabled_p)
6465 return NULL;
6466 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6467 break;
6468 }
6469
6470 *vpos = i;
6471 *hpos = 0;
6472
6473 /* Give up if Y is not in the window. */
6474 if (i == w->current_matrix->nrows)
6475 return NULL;
6476
6477 /* Get the glyph area containing X. */
6478 if (w->pseudo_window_p)
6479 {
6480 *area = TEXT_AREA;
6481 x0 = 0;
6482 }
6483 else
6484 {
6485 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6486 if (x < left_area_width)
6487 {
6488 *area = LEFT_MARGIN_AREA;
6489 x0 = 0;
6490 }
6491 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6492 {
6493 *area = TEXT_AREA;
6494 x0 = row->x + left_area_width;
6495 }
6496 else
6497 {
6498 *area = RIGHT_MARGIN_AREA;
6499 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6500 }
6501 }
6502
6503 /* Find glyph containing X. */
6504 glyph = row->glyphs[*area];
6505 end = glyph + row->used[*area];
6506 while (glyph < end)
6507 {
6508 if (x < x0 + glyph->pixel_width)
6509 {
6510 if (w->pseudo_window_p)
6511 break;
6512 else if (!buffer_only_p || BUFFERP (glyph->object))
6513 break;
6514 }
6515
6516 x0 += glyph->pixel_width;
6517 ++glyph;
6518 }
6519
6520 if (glyph == end)
6521 return NULL;
6522
6523 *hpos = glyph - row->glyphs[*area];
6524 return glyph;
6525 }
6526
6527
6528 /* Convert frame-relative x/y to coordinates relative to window W.
6529 Takes pseudo-windows into account. */
6530
6531 static void
6532 frame_to_window_pixel_xy (w, x, y)
6533 struct window *w;
6534 int *x, *y;
6535 {
6536 if (w->pseudo_window_p)
6537 {
6538 /* A pseudo-window is always full-width, and starts at the
6539 left edge of the frame, plus a frame border. */
6540 struct frame *f = XFRAME (w->frame);
6541 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6542 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6543 }
6544 else
6545 {
6546 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6547 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6548 }
6549 }
6550
6551
6552 /* Take proper action when mouse has moved to the mode or header line of
6553 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6554 mode line. X is relative to the start of the text display area of
6555 W, so the width of bitmap areas and scroll bars must be subtracted
6556 to get a position relative to the start of the mode line. */
6557
6558 static void
6559 note_mode_line_highlight (w, x, mode_line_p)
6560 struct window *w;
6561 int x, mode_line_p;
6562 {
6563 struct frame *f = XFRAME (w->frame);
6564 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6565 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6566 struct glyph_row *row;
6567
6568 if (mode_line_p)
6569 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6570 else
6571 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6572
6573 if (row->enabled_p)
6574 {
6575 struct glyph *glyph, *end;
6576 Lisp_Object help, map;
6577 int x0;
6578
6579 /* Find the glyph under X. */
6580 glyph = row->glyphs[TEXT_AREA];
6581 end = glyph + row->used[TEXT_AREA];
6582 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6583 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6584
6585 while (glyph < end
6586 && x >= x0 + glyph->pixel_width)
6587 {
6588 x0 += glyph->pixel_width;
6589 ++glyph;
6590 }
6591
6592 if (glyph < end
6593 && STRINGP (glyph->object)
6594 && XSTRING (glyph->object)->intervals
6595 && glyph->charpos >= 0
6596 && glyph->charpos < XSTRING (glyph->object)->size)
6597 {
6598 /* If we're on a string with `help-echo' text property,
6599 arrange for the help to be displayed. This is done by
6600 setting the global variable help_echo to the help string. */
6601 help = Fget_text_property (make_number (glyph->charpos),
6602 Qhelp_echo, glyph->object);
6603 if (!NILP (help))
6604 {
6605 help_echo = help;
6606 XSETWINDOW (help_echo_window, w);
6607 help_echo_object = glyph->object;
6608 help_echo_pos = glyph->charpos;
6609 }
6610
6611 /* Change the mouse pointer according to what is under X/Y. */
6612 map = Fget_text_property (make_number (glyph->charpos),
6613 Qlocal_map, glyph->object);
6614 if (KEYMAPP (map))
6615 cursor = f->output_data.x->nontext_cursor;
6616 else
6617 {
6618 map = Fget_text_property (make_number (glyph->charpos),
6619 Qkeymap, glyph->object);
6620 if (KEYMAPP (map))
6621 cursor = f->output_data.x->nontext_cursor;
6622 }
6623 }
6624 }
6625
6626 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6627 }
6628
6629
6630 /* Take proper action when the mouse has moved to position X, Y on
6631 frame F as regards highlighting characters that have mouse-face
6632 properties. Also de-highlighting chars where the mouse was before.
6633 X and Y can be negative or out of range. */
6634
6635 static void
6636 note_mouse_highlight (f, x, y)
6637 struct frame *f;
6638 int x, y;
6639 {
6640 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6641 int portion;
6642 Lisp_Object window;
6643 struct window *w;
6644
6645 /* When a menu is active, don't highlight because this looks odd. */
6646 #ifdef USE_X_TOOLKIT
6647 if (popup_activated ())
6648 return;
6649 #endif
6650
6651 if (disable_mouse_highlight
6652 || !f->glyphs_initialized_p)
6653 return;
6654
6655 dpyinfo->mouse_face_mouse_x = x;
6656 dpyinfo->mouse_face_mouse_y = y;
6657 dpyinfo->mouse_face_mouse_frame = f;
6658
6659 if (dpyinfo->mouse_face_defer)
6660 return;
6661
6662 if (gc_in_progress)
6663 {
6664 dpyinfo->mouse_face_deferred_gc = 1;
6665 return;
6666 }
6667
6668 /* Which window is that in? */
6669 window = window_from_coordinates (f, x, y, &portion, 1);
6670
6671 /* If we were displaying active text in another window, clear that. */
6672 if (! EQ (window, dpyinfo->mouse_face_window))
6673 clear_mouse_face (dpyinfo);
6674
6675 /* Not on a window -> return. */
6676 if (!WINDOWP (window))
6677 return;
6678
6679 /* Convert to window-relative pixel coordinates. */
6680 w = XWINDOW (window);
6681 frame_to_window_pixel_xy (w, &x, &y);
6682
6683 /* Handle tool-bar window differently since it doesn't display a
6684 buffer. */
6685 if (EQ (window, f->tool_bar_window))
6686 {
6687 note_tool_bar_highlight (f, x, y);
6688 return;
6689 }
6690
6691 if (portion == 1 || portion == 3)
6692 {
6693 /* Mouse is on the mode or top line. */
6694 note_mode_line_highlight (w, x, portion == 1);
6695 return;
6696 }
6697 else if (portion == 2)
6698 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6699 f->output_data.x->horizontal_drag_cursor);
6700 else
6701 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6702 f->output_data.x->text_cursor);
6703
6704 /* Are we in a window whose display is up to date?
6705 And verify the buffer's text has not changed. */
6706 if (/* Within text portion of the window. */
6707 portion == 0
6708 && EQ (w->window_end_valid, w->buffer)
6709 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6710 && (XFASTINT (w->last_overlay_modified)
6711 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6712 {
6713 int hpos, vpos, pos, i, area;
6714 struct glyph *glyph;
6715 Lisp_Object object;
6716
6717 /* Find the glyph under X/Y. */
6718 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6719
6720 /* Clear mouse face if X/Y not over text. */
6721 if (glyph == NULL
6722 || area != TEXT_AREA
6723 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6724 {
6725 clear_mouse_face (dpyinfo);
6726 return;
6727 }
6728
6729 pos = glyph->charpos;
6730 object = glyph->object;
6731 if (!STRINGP (object) && !BUFFERP (object))
6732 return;
6733
6734 {
6735 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6736 Lisp_Object *overlay_vec = NULL;
6737 int len, noverlays;
6738 struct buffer *obuf;
6739 int obegv, ozv;
6740
6741 /* If we get an out-of-range value, return now; avoid an error. */
6742 if (BUFFERP (object) && pos > BUF_Z (XBUFFER (w->buffer)))
6743 return;
6744
6745 /* Make the window's buffer temporarily current for
6746 overlays_at and compute_char_face. */
6747 obuf = current_buffer;
6748 current_buffer = XBUFFER (w->buffer);
6749 obegv = BEGV;
6750 ozv = ZV;
6751 BEGV = BEG;
6752 ZV = Z;
6753
6754 /* Is this char mouse-active or does it have help-echo? */
6755 position = make_number (pos);
6756
6757 if (BUFFERP (object))
6758 {
6759 /* Put all the overlays we want in a vector in overlay_vec.
6760 Store the length in len. If there are more than 10, make
6761 enough space for all, and try again. */
6762 len = 10;
6763 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6764 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6765 if (noverlays > len)
6766 {
6767 len = noverlays;
6768 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6769 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6770 }
6771
6772 /* Sort overlays into increasing priority order. */
6773 noverlays = sort_overlays (overlay_vec, noverlays, w);
6774 }
6775 else
6776 noverlays = 0;
6777
6778 /* Check mouse-face highlighting. */
6779 if (! (EQ (window, dpyinfo->mouse_face_window)
6780 && vpos >= dpyinfo->mouse_face_beg_row
6781 && vpos <= dpyinfo->mouse_face_end_row
6782 && (vpos > dpyinfo->mouse_face_beg_row
6783 || hpos >= dpyinfo->mouse_face_beg_col)
6784 && (vpos < dpyinfo->mouse_face_end_row
6785 || hpos < dpyinfo->mouse_face_end_col
6786 || dpyinfo->mouse_face_past_end))
6787 /* If there exists an overlay with mouse-face overlapping
6788 the one we are currently highlighting, we have to
6789 check if we enter the overlapping overlay, and then
6790 highlight only that. */
6791 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6792 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6793
6794 {
6795 /* Clear the display of the old active region, if any. */
6796 clear_mouse_face (dpyinfo);
6797
6798 /* Find the highest priority overlay that has a mouse-face
6799 property. */
6800 overlay = Qnil;
6801 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6802 {
6803 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6804 if (!NILP (mouse_face))
6805 overlay = overlay_vec[i];
6806 }
6807 dpyinfo->mouse_face_overlay = overlay;
6808
6809 /* If no overlay applies, get a text property. */
6810 if (NILP (overlay))
6811 mouse_face = Fget_text_property (position, Qmouse_face, object);
6812
6813 /* Handle the overlay case. */
6814 if (!NILP (overlay))
6815 {
6816 /* Find the range of text around this char that
6817 should be active. */
6818 Lisp_Object before, after;
6819 int ignore;
6820
6821 before = Foverlay_start (overlay);
6822 after = Foverlay_end (overlay);
6823 /* Record this as the current active region. */
6824 fast_find_position (w, XFASTINT (before),
6825 &dpyinfo->mouse_face_beg_col,
6826 &dpyinfo->mouse_face_beg_row,
6827 &dpyinfo->mouse_face_beg_x,
6828 &dpyinfo->mouse_face_beg_y);
6829 dpyinfo->mouse_face_past_end
6830 = !fast_find_position (w, XFASTINT (after),
6831 &dpyinfo->mouse_face_end_col,
6832 &dpyinfo->mouse_face_end_row,
6833 &dpyinfo->mouse_face_end_x,
6834 &dpyinfo->mouse_face_end_y);
6835 dpyinfo->mouse_face_window = window;
6836 dpyinfo->mouse_face_face_id
6837 = face_at_buffer_position (w, pos, 0, 0,
6838 &ignore, pos + 1, 1);
6839
6840 /* Display it as active. */
6841 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6842 }
6843 /* Handle the text property case. */
6844 else if (!NILP (mouse_face) && BUFFERP (object))
6845 {
6846 /* Find the range of text around this char that
6847 should be active. */
6848 Lisp_Object before, after, beginning, end;
6849 int ignore;
6850
6851 beginning = Fmarker_position (w->start);
6852 end = make_number (BUF_Z (XBUFFER (object))
6853 - XFASTINT (w->window_end_pos));
6854 before
6855 = Fprevious_single_property_change (make_number (pos + 1),
6856 Qmouse_face,
6857 object, beginning);
6858 after
6859 = Fnext_single_property_change (position, Qmouse_face,
6860 object, end);
6861
6862 /* Record this as the current active region. */
6863 fast_find_position (w, XFASTINT (before),
6864 &dpyinfo->mouse_face_beg_col,
6865 &dpyinfo->mouse_face_beg_row,
6866 &dpyinfo->mouse_face_beg_x,
6867 &dpyinfo->mouse_face_beg_y);
6868 dpyinfo->mouse_face_past_end
6869 = !fast_find_position (w, XFASTINT (after),
6870 &dpyinfo->mouse_face_end_col,
6871 &dpyinfo->mouse_face_end_row,
6872 &dpyinfo->mouse_face_end_x,
6873 &dpyinfo->mouse_face_end_y);
6874 dpyinfo->mouse_face_window = window;
6875
6876 if (BUFFERP (object))
6877 dpyinfo->mouse_face_face_id
6878 = face_at_buffer_position (w, pos, 0, 0,
6879 &ignore, pos + 1, 1);
6880
6881 /* Display it as active. */
6882 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6883 }
6884 else if (!NILP (mouse_face) && STRINGP (object))
6885 {
6886 Lisp_Object b, e;
6887 int ignore;
6888
6889 b = Fprevious_single_property_change (make_number (pos + 1),
6890 Qmouse_face,
6891 object, Qnil);
6892 e = Fnext_single_property_change (position, Qmouse_face,
6893 object, Qnil);
6894 if (NILP (b))
6895 b = make_number (0);
6896 if (NILP (e))
6897 e = make_number (XSTRING (object)->size) - 1;
6898 fast_find_string_pos (w, XINT (b), object,
6899 &dpyinfo->mouse_face_beg_col,
6900 &dpyinfo->mouse_face_beg_row,
6901 &dpyinfo->mouse_face_beg_x,
6902 &dpyinfo->mouse_face_beg_y, 0);
6903 fast_find_string_pos (w, XINT (e), object,
6904 &dpyinfo->mouse_face_end_col,
6905 &dpyinfo->mouse_face_end_row,
6906 &dpyinfo->mouse_face_end_x,
6907 &dpyinfo->mouse_face_end_y, 1);
6908 dpyinfo->mouse_face_past_end = 0;
6909 dpyinfo->mouse_face_window = window;
6910 dpyinfo->mouse_face_face_id
6911 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
6912 glyph->face_id, 1);
6913 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6914 }
6915 }
6916
6917 /* Look for a `help-echo' property. */
6918 {
6919 Lisp_Object help, overlay;
6920
6921 /* Check overlays first. */
6922 help = overlay = Qnil;
6923 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6924 {
6925 overlay = overlay_vec[i];
6926 help = Foverlay_get (overlay, Qhelp_echo);
6927 }
6928
6929 if (!NILP (help))
6930 {
6931 help_echo = help;
6932 help_echo_window = window;
6933 help_echo_object = overlay;
6934 help_echo_pos = pos;
6935 }
6936 else
6937 {
6938 Lisp_Object object = glyph->object;
6939 int charpos = glyph->charpos;
6940
6941 /* Try text properties. */
6942 if (STRINGP (object)
6943 && charpos >= 0
6944 && charpos < XSTRING (object)->size)
6945 {
6946 help = Fget_text_property (make_number (charpos),
6947 Qhelp_echo, object);
6948 if (NILP (help))
6949 {
6950 /* If the string itself doesn't specify a help-echo,
6951 see if the buffer text ``under'' it does. */
6952 struct glyph_row *r
6953 = MATRIX_ROW (w->current_matrix, vpos);
6954 int start = MATRIX_ROW_START_CHARPOS (r);
6955 int pos = string_buffer_position (w, object, start);
6956 if (pos > 0)
6957 {
6958 help = Fget_text_property (make_number (pos),
6959 Qhelp_echo, w->buffer);
6960 if (!NILP (help))
6961 {
6962 charpos = pos;
6963 object = w->buffer;
6964 }
6965 }
6966 }
6967 }
6968 else if (BUFFERP (object)
6969 && charpos >= BEGV
6970 && charpos < ZV)
6971 help = Fget_text_property (make_number (charpos), Qhelp_echo,
6972 object);
6973
6974 if (!NILP (help))
6975 {
6976 help_echo = help;
6977 help_echo_window = window;
6978 help_echo_object = object;
6979 help_echo_pos = charpos;
6980 }
6981 }
6982 }
6983
6984 BEGV = obegv;
6985 ZV = ozv;
6986 current_buffer = obuf;
6987 }
6988 }
6989 }
6990
6991 static void
6992 redo_mouse_highlight ()
6993 {
6994 if (!NILP (last_mouse_motion_frame)
6995 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6996 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6997 last_mouse_motion_event.x,
6998 last_mouse_motion_event.y);
6999 }
7000
7001
7002 \f
7003 /***********************************************************************
7004 Tool-bars
7005 ***********************************************************************/
7006
7007 static int x_tool_bar_item P_ ((struct frame *, int, int,
7008 struct glyph **, int *, int *, int *));
7009
7010 /* Tool-bar item index of the item on which a mouse button was pressed
7011 or -1. */
7012
7013 static int last_tool_bar_item;
7014
7015
7016 /* Get information about the tool-bar item at position X/Y on frame F.
7017 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7018 the current matrix of the tool-bar window of F, or NULL if not
7019 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7020 item in F->tool_bar_items. Value is
7021
7022 -1 if X/Y is not on a tool-bar item
7023 0 if X/Y is on the same item that was highlighted before.
7024 1 otherwise. */
7025
7026 static int
7027 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7028 struct frame *f;
7029 int x, y;
7030 struct glyph **glyph;
7031 int *hpos, *vpos, *prop_idx;
7032 {
7033 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7034 struct window *w = XWINDOW (f->tool_bar_window);
7035 int area;
7036
7037 /* Find the glyph under X/Y. */
7038 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7039 if (*glyph == NULL)
7040 return -1;
7041
7042 /* Get the start of this tool-bar item's properties in
7043 f->tool_bar_items. */
7044 if (!tool_bar_item_info (f, *glyph, prop_idx))
7045 return -1;
7046
7047 /* Is mouse on the highlighted item? */
7048 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7049 && *vpos >= dpyinfo->mouse_face_beg_row
7050 && *vpos <= dpyinfo->mouse_face_end_row
7051 && (*vpos > dpyinfo->mouse_face_beg_row
7052 || *hpos >= dpyinfo->mouse_face_beg_col)
7053 && (*vpos < dpyinfo->mouse_face_end_row
7054 || *hpos < dpyinfo->mouse_face_end_col
7055 || dpyinfo->mouse_face_past_end))
7056 return 0;
7057
7058 return 1;
7059 }
7060
7061
7062 /* Handle mouse button event on the tool-bar of frame F, at
7063 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7064 or ButtonRelase. */
7065
7066 static void
7067 x_handle_tool_bar_click (f, button_event)
7068 struct frame *f;
7069 XButtonEvent *button_event;
7070 {
7071 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7072 struct window *w = XWINDOW (f->tool_bar_window);
7073 int hpos, vpos, prop_idx;
7074 struct glyph *glyph;
7075 Lisp_Object enabled_p;
7076 int x = button_event->x;
7077 int y = button_event->y;
7078
7079 /* If not on the highlighted tool-bar item, return. */
7080 frame_to_window_pixel_xy (w, &x, &y);
7081 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7082 return;
7083
7084 /* If item is disabled, do nothing. */
7085 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7086 if (NILP (enabled_p))
7087 return;
7088
7089 if (button_event->type == ButtonPress)
7090 {
7091 /* Show item in pressed state. */
7092 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7093 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7094 last_tool_bar_item = prop_idx;
7095 }
7096 else
7097 {
7098 Lisp_Object key, frame;
7099 struct input_event event;
7100
7101 /* Show item in released state. */
7102 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7103 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7104
7105 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7106
7107 XSETFRAME (frame, f);
7108 event.kind = TOOL_BAR_EVENT;
7109 event.frame_or_window = frame;
7110 event.arg = frame;
7111 kbd_buffer_store_event (&event);
7112
7113 event.kind = TOOL_BAR_EVENT;
7114 event.frame_or_window = frame;
7115 event.arg = key;
7116 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7117 button_event->state);
7118 kbd_buffer_store_event (&event);
7119 last_tool_bar_item = -1;
7120 }
7121 }
7122
7123
7124 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7125 tool-bar window-relative coordinates X/Y. Called from
7126 note_mouse_highlight. */
7127
7128 static void
7129 note_tool_bar_highlight (f, x, y)
7130 struct frame *f;
7131 int x, y;
7132 {
7133 Lisp_Object window = f->tool_bar_window;
7134 struct window *w = XWINDOW (window);
7135 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7136 int hpos, vpos;
7137 struct glyph *glyph;
7138 struct glyph_row *row;
7139 int i;
7140 Lisp_Object enabled_p;
7141 int prop_idx;
7142 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7143 int mouse_down_p, rc;
7144
7145 /* Function note_mouse_highlight is called with negative x(y
7146 values when mouse moves outside of the frame. */
7147 if (x <= 0 || y <= 0)
7148 {
7149 clear_mouse_face (dpyinfo);
7150 return;
7151 }
7152
7153 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7154 if (rc < 0)
7155 {
7156 /* Not on tool-bar item. */
7157 clear_mouse_face (dpyinfo);
7158 return;
7159 }
7160 else if (rc == 0)
7161 /* On same tool-bar item as before. */
7162 goto set_help_echo;
7163
7164 clear_mouse_face (dpyinfo);
7165
7166 /* Mouse is down, but on different tool-bar item? */
7167 mouse_down_p = (dpyinfo->grabbed
7168 && f == last_mouse_frame
7169 && FRAME_LIVE_P (f));
7170 if (mouse_down_p
7171 && last_tool_bar_item != prop_idx)
7172 return;
7173
7174 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7175 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7176
7177 /* If tool-bar item is not enabled, don't highlight it. */
7178 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7179 if (!NILP (enabled_p))
7180 {
7181 /* Compute the x-position of the glyph. In front and past the
7182 image is a space. We include this is the highlighted area. */
7183 row = MATRIX_ROW (w->current_matrix, vpos);
7184 for (i = x = 0; i < hpos; ++i)
7185 x += row->glyphs[TEXT_AREA][i].pixel_width;
7186
7187 /* Record this as the current active region. */
7188 dpyinfo->mouse_face_beg_col = hpos;
7189 dpyinfo->mouse_face_beg_row = vpos;
7190 dpyinfo->mouse_face_beg_x = x;
7191 dpyinfo->mouse_face_beg_y = row->y;
7192 dpyinfo->mouse_face_past_end = 0;
7193
7194 dpyinfo->mouse_face_end_col = hpos + 1;
7195 dpyinfo->mouse_face_end_row = vpos;
7196 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7197 dpyinfo->mouse_face_end_y = row->y;
7198 dpyinfo->mouse_face_window = window;
7199 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7200
7201 /* Display it as active. */
7202 show_mouse_face (dpyinfo, draw);
7203 dpyinfo->mouse_face_image_state = draw;
7204 }
7205
7206 set_help_echo:
7207
7208 /* Set help_echo to a help string.to display for this tool-bar item.
7209 XTread_socket does the rest. */
7210 help_echo_object = help_echo_window = Qnil;
7211 help_echo_pos = -1;
7212 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7213 if (NILP (help_echo))
7214 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7215 }
7216
7217
7218 \f
7219 /* Find the glyph matrix position of buffer position POS in window W.
7220 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7221 current glyphs must be up to date. If POS is above window start
7222 return (0, 0, 0, 0). If POS is after end of W, return end of
7223 last line in W. */
7224
7225 static int
7226 fast_find_position (w, pos, hpos, vpos, x, y)
7227 struct window *w;
7228 int pos;
7229 int *hpos, *vpos, *x, *y;
7230 {
7231 int i;
7232 int lastcol;
7233 int maybe_next_line_p = 0;
7234 int line_start_position;
7235 int yb = window_text_bottom_y (w);
7236 struct glyph_row *row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7237 struct glyph_row *best_row = row;
7238 int row_vpos = 0, best_row_vpos = 0;
7239 int current_x;
7240
7241 while (row->y < yb)
7242 {
7243 if (row->used[TEXT_AREA])
7244 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7245 else
7246 line_start_position = 0;
7247
7248 if (line_start_position > pos)
7249 break;
7250 /* If the position sought is the end of the buffer,
7251 don't include the blank lines at the bottom of the window. */
7252 else if (line_start_position == pos
7253 && pos == BUF_ZV (XBUFFER (w->buffer)))
7254 {
7255 maybe_next_line_p = 1;
7256 break;
7257 }
7258 else if (line_start_position > 0)
7259 {
7260 best_row = row;
7261 best_row_vpos = row_vpos;
7262 }
7263
7264 if (row->y + row->height >= yb)
7265 break;
7266
7267 ++row;
7268 ++row_vpos;
7269 }
7270
7271 /* Find the right column within BEST_ROW. */
7272 lastcol = 0;
7273 current_x = best_row->x;
7274 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7275 {
7276 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7277 int charpos;
7278
7279 charpos = glyph->charpos;
7280 if (charpos == pos)
7281 {
7282 *hpos = i;
7283 *vpos = best_row_vpos;
7284 *x = current_x;
7285 *y = best_row->y;
7286 return 1;
7287 }
7288 else if (charpos > pos)
7289 break;
7290 else if (charpos > 0)
7291 lastcol = i;
7292
7293 current_x += glyph->pixel_width;
7294 }
7295
7296 /* If we're looking for the end of the buffer,
7297 and we didn't find it in the line we scanned,
7298 use the start of the following line. */
7299 if (maybe_next_line_p)
7300 {
7301 ++best_row;
7302 ++best_row_vpos;
7303 lastcol = 0;
7304 current_x = best_row->x;
7305 }
7306
7307 *vpos = best_row_vpos;
7308 *hpos = lastcol + 1;
7309 *x = current_x;
7310 *y = best_row->y;
7311 return 0;
7312 }
7313
7314
7315 /* Find the position of the the glyph for position POS in OBJECT in
7316 window W's current matrix, and return in *X/*Y the pixel
7317 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7318
7319 RIGHT_P non-zero means return the position of the right edge of the
7320 glyph, RIGHT_P zero means return the left edge position.
7321
7322 If no glyph for POS exists in the matrix, return the position of
7323 the glyph with the next smaller position that is in the matrix, if
7324 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7325 exists in the matrix, return the position of the glyph with the
7326 next larger position in OBJECT.
7327
7328 Value is non-zero if a glyph was found. */
7329
7330 static int
7331 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7332 struct window *w;
7333 int pos;
7334 Lisp_Object object;
7335 int *hpos, *vpos, *x, *y;
7336 int right_p;
7337 {
7338 int yb = window_text_bottom_y (w);
7339 struct glyph_row *r;
7340 struct glyph *best_glyph = NULL;
7341 struct glyph_row *best_row = NULL;
7342 int best_x = 0;
7343
7344 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7345 r->enabled_p && r->y < yb;
7346 ++r)
7347 {
7348 struct glyph *g = r->glyphs[TEXT_AREA];
7349 struct glyph *e = g + r->used[TEXT_AREA];
7350 int gx;
7351
7352 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7353 if (EQ (g->object, object))
7354 {
7355 if (g->charpos == pos)
7356 {
7357 best_glyph = g;
7358 best_x = gx;
7359 best_row = r;
7360 goto found;
7361 }
7362 else if (best_glyph == NULL
7363 || ((abs (g->charpos - pos)
7364 < abs (best_glyph->charpos - pos))
7365 && (right_p
7366 ? g->charpos < pos
7367 : g->charpos > pos)))
7368 {
7369 best_glyph = g;
7370 best_x = gx;
7371 best_row = r;
7372 }
7373 }
7374 }
7375
7376 found:
7377
7378 if (best_glyph)
7379 {
7380 *x = best_x;
7381 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7382
7383 if (right_p)
7384 {
7385 *x += best_glyph->pixel_width;
7386 ++*hpos;
7387 }
7388
7389 *y = best_row->y;
7390 *vpos = best_row - w->current_matrix->rows;
7391 }
7392
7393 return best_glyph != NULL;
7394 }
7395
7396
7397 /* Display the active region described by mouse_face_*
7398 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7399
7400 static void
7401 show_mouse_face (dpyinfo, draw)
7402 struct x_display_info *dpyinfo;
7403 enum draw_glyphs_face draw;
7404 {
7405 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7406 struct frame *f = XFRAME (WINDOW_FRAME (w));
7407 int i;
7408 int cursor_off_p = 0;
7409 struct cursor_pos saved_cursor;
7410
7411 saved_cursor = output_cursor;
7412
7413 /* If window is in the process of being destroyed, don't bother
7414 to do anything. */
7415 if (w->current_matrix == NULL)
7416 goto set_x_cursor;
7417
7418 /* Recognize when we are called to operate on rows that don't exist
7419 anymore. This can happen when a window is split. */
7420 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7421 goto set_x_cursor;
7422
7423 set_output_cursor (&w->phys_cursor);
7424
7425 /* Note that mouse_face_beg_row etc. are window relative. */
7426 for (i = dpyinfo->mouse_face_beg_row;
7427 i <= dpyinfo->mouse_face_end_row;
7428 i++)
7429 {
7430 int start_hpos, end_hpos, start_x;
7431 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7432
7433 /* Don't do anything if row doesn't have valid contents. */
7434 if (!row->enabled_p)
7435 continue;
7436
7437 /* For all but the first row, the highlight starts at column 0. */
7438 if (i == dpyinfo->mouse_face_beg_row)
7439 {
7440 start_hpos = dpyinfo->mouse_face_beg_col;
7441 start_x = dpyinfo->mouse_face_beg_x;
7442 }
7443 else
7444 {
7445 start_hpos = 0;
7446 start_x = 0;
7447 }
7448
7449 if (i == dpyinfo->mouse_face_end_row)
7450 end_hpos = dpyinfo->mouse_face_end_col;
7451 else
7452 end_hpos = row->used[TEXT_AREA];
7453
7454 /* If the cursor's in the text we are about to rewrite, turn the
7455 cursor off. */
7456 if (!w->pseudo_window_p
7457 && i == output_cursor.vpos
7458 && output_cursor.hpos >= start_hpos - 1
7459 && output_cursor.hpos <= end_hpos)
7460 {
7461 x_update_window_cursor (w, 0);
7462 cursor_off_p = 1;
7463 }
7464
7465 if (end_hpos > start_hpos)
7466 {
7467 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7468 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7469 start_hpos, end_hpos, draw, NULL, NULL, 0);
7470 }
7471 }
7472
7473 /* If we turned the cursor off, turn it back on. */
7474 if (cursor_off_p)
7475 x_display_cursor (w, 1,
7476 output_cursor.hpos, output_cursor.vpos,
7477 output_cursor.x, output_cursor.y);
7478
7479 output_cursor = saved_cursor;
7480
7481 set_x_cursor:
7482
7483 /* Change the mouse cursor. */
7484 if (draw == DRAW_NORMAL_TEXT)
7485 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7486 f->output_data.x->text_cursor);
7487 else if (draw == DRAW_MOUSE_FACE)
7488 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7489 f->output_data.x->cross_cursor);
7490 else
7491 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7492 f->output_data.x->nontext_cursor);
7493 }
7494
7495 /* Clear out the mouse-highlighted active region.
7496 Redraw it un-highlighted first. */
7497
7498 void
7499 clear_mouse_face (dpyinfo)
7500 struct x_display_info *dpyinfo;
7501 {
7502 #if 0 /* This prevents redrawing tool bar items when changing from one
7503 to another while a tooltip is open, so don't do it. */
7504 if (!NILP (tip_frame))
7505 return;
7506 #endif
7507
7508 if (! NILP (dpyinfo->mouse_face_window))
7509 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7510
7511 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7512 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7513 dpyinfo->mouse_face_window = Qnil;
7514 }
7515
7516
7517 /* Clear any mouse-face on window W. This function is part of the
7518 redisplay interface, and is called from try_window_id and similar
7519 functions to ensure the mouse-highlight is off. */
7520
7521 static void
7522 x_clear_mouse_face (w)
7523 struct window *w;
7524 {
7525 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7526 Lisp_Object window;
7527
7528 BLOCK_INPUT;
7529 XSETWINDOW (window, w);
7530 if (EQ (window, dpyinfo->mouse_face_window))
7531 clear_mouse_face (dpyinfo);
7532 UNBLOCK_INPUT;
7533 }
7534
7535
7536 /* Just discard the mouse face information for frame F, if any.
7537 This is used when the size of F is changed. */
7538
7539 void
7540 cancel_mouse_face (f)
7541 FRAME_PTR f;
7542 {
7543 Lisp_Object window;
7544 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7545
7546 window = dpyinfo->mouse_face_window;
7547 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7548 {
7549 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7550 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7551 dpyinfo->mouse_face_window = Qnil;
7552 }
7553 }
7554
7555 \f
7556 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7557
7558
7559 /* Try to determine frame pixel position and size of the glyph under
7560 frame pixel coordinates X/Y on frame F . Return the position and
7561 size in *RECT. Value is non-zero if we could compute these
7562 values. */
7563
7564 static int
7565 glyph_rect (f, x, y, rect)
7566 struct frame *f;
7567 int x, y;
7568 XRectangle *rect;
7569 {
7570 Lisp_Object window;
7571 int part, found = 0;
7572
7573 window = window_from_coordinates (f, x, y, &part, 0);
7574 if (!NILP (window))
7575 {
7576 struct window *w = XWINDOW (window);
7577 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7578 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7579 int area;
7580
7581 frame_to_window_pixel_xy (w, &x, &y);
7582
7583 for (; !found && r < end && r->enabled_p; ++r)
7584 if (r->y >= y)
7585 {
7586 struct glyph *g = r->glyphs[TEXT_AREA];
7587 struct glyph *end = g + r->used[TEXT_AREA];
7588 int gx;
7589
7590 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7591 if (gx >= x)
7592 {
7593 rect->width = g->pixel_width;
7594 rect->height = r->height;
7595 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7596 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7597 found = 1;
7598 }
7599 }
7600 }
7601
7602 return found;
7603 }
7604
7605
7606 /* Return the current position of the mouse.
7607 *FP should be a frame which indicates which display to ask about.
7608
7609 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7610 and *PART to the frame, window, and scroll bar part that the mouse
7611 is over. Set *X and *Y to the portion and whole of the mouse's
7612 position on the scroll bar.
7613
7614 If the mouse movement started elsewhere, set *FP to the frame the
7615 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7616 the mouse is over.
7617
7618 Set *TIME to the server time-stamp for the time at which the mouse
7619 was at this position.
7620
7621 Don't store anything if we don't have a valid set of values to report.
7622
7623 This clears the mouse_moved flag, so we can wait for the next mouse
7624 movement. */
7625
7626 static void
7627 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7628 FRAME_PTR *fp;
7629 int insist;
7630 Lisp_Object *bar_window;
7631 enum scroll_bar_part *part;
7632 Lisp_Object *x, *y;
7633 unsigned long *time;
7634 {
7635 FRAME_PTR f1;
7636
7637 BLOCK_INPUT;
7638
7639 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7640 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7641 else
7642 {
7643 Window root;
7644 int root_x, root_y;
7645
7646 Window dummy_window;
7647 int dummy;
7648
7649 Lisp_Object frame, tail;
7650
7651 /* Clear the mouse-moved flag for every frame on this display. */
7652 FOR_EACH_FRAME (tail, frame)
7653 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7654 XFRAME (frame)->mouse_moved = 0;
7655
7656 last_mouse_scroll_bar = Qnil;
7657
7658 /* Figure out which root window we're on. */
7659 XQueryPointer (FRAME_X_DISPLAY (*fp),
7660 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7661
7662 /* The root window which contains the pointer. */
7663 &root,
7664
7665 /* Trash which we can't trust if the pointer is on
7666 a different screen. */
7667 &dummy_window,
7668
7669 /* The position on that root window. */
7670 &root_x, &root_y,
7671
7672 /* More trash we can't trust. */
7673 &dummy, &dummy,
7674
7675 /* Modifier keys and pointer buttons, about which
7676 we don't care. */
7677 (unsigned int *) &dummy);
7678
7679 /* Now we have a position on the root; find the innermost window
7680 containing the pointer. */
7681 {
7682 Window win, child;
7683 int win_x, win_y;
7684 int parent_x = 0, parent_y = 0;
7685 int count;
7686
7687 win = root;
7688
7689 /* XTranslateCoordinates can get errors if the window
7690 structure is changing at the same time this function
7691 is running. So at least we must not crash from them. */
7692
7693 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7694
7695 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7696 && FRAME_LIVE_P (last_mouse_frame))
7697 {
7698 /* If mouse was grabbed on a frame, give coords for that frame
7699 even if the mouse is now outside it. */
7700 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7701
7702 /* From-window, to-window. */
7703 root, FRAME_X_WINDOW (last_mouse_frame),
7704
7705 /* From-position, to-position. */
7706 root_x, root_y, &win_x, &win_y,
7707
7708 /* Child of win. */
7709 &child);
7710 f1 = last_mouse_frame;
7711 }
7712 else
7713 {
7714 while (1)
7715 {
7716 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7717
7718 /* From-window, to-window. */
7719 root, win,
7720
7721 /* From-position, to-position. */
7722 root_x, root_y, &win_x, &win_y,
7723
7724 /* Child of win. */
7725 &child);
7726
7727 if (child == None || child == win)
7728 break;
7729
7730 win = child;
7731 parent_x = win_x;
7732 parent_y = win_y;
7733 }
7734
7735 /* Now we know that:
7736 win is the innermost window containing the pointer
7737 (XTC says it has no child containing the pointer),
7738 win_x and win_y are the pointer's position in it
7739 (XTC did this the last time through), and
7740 parent_x and parent_y are the pointer's position in win's parent.
7741 (They are what win_x and win_y were when win was child.
7742 If win is the root window, it has no parent, and
7743 parent_{x,y} are invalid, but that's okay, because we'll
7744 never use them in that case.) */
7745
7746 /* Is win one of our frames? */
7747 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7748
7749 #ifdef USE_X_TOOLKIT
7750 /* If we end up with the menu bar window, say it's not
7751 on the frame. */
7752 if (f1 != NULL
7753 && f1->output_data.x->menubar_widget
7754 && win == XtWindow (f1->output_data.x->menubar_widget))
7755 f1 = NULL;
7756 #endif /* USE_X_TOOLKIT */
7757 }
7758
7759 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7760 f1 = 0;
7761
7762 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7763
7764 /* If not, is it one of our scroll bars? */
7765 if (! f1)
7766 {
7767 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7768
7769 if (bar)
7770 {
7771 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7772 win_x = parent_x;
7773 win_y = parent_y;
7774 }
7775 }
7776
7777 if (f1 == 0 && insist > 0)
7778 f1 = SELECTED_FRAME ();
7779
7780 if (f1)
7781 {
7782 /* Ok, we found a frame. Store all the values.
7783 last_mouse_glyph is a rectangle used to reduce the
7784 generation of mouse events. To not miss any motion
7785 events, we must divide the frame into rectangles of the
7786 size of the smallest character that could be displayed
7787 on it, i.e. into the same rectangles that matrices on
7788 the frame are divided into. */
7789
7790 int width, height, gx, gy;
7791 XRectangle rect;
7792
7793 if (glyph_rect (f1, win_x, win_y, &rect))
7794 last_mouse_glyph = rect;
7795 else
7796 {
7797 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7798 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7799 gx = win_x;
7800 gy = win_y;
7801
7802 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7803 round down even for negative values. */
7804 if (gx < 0)
7805 gx -= width - 1;
7806 if (gy < 0)
7807 gy -= height - 1;
7808 gx = (gx + width - 1) / width * width;
7809 gy = (gy + height - 1) / height * height;
7810
7811 last_mouse_glyph.width = width;
7812 last_mouse_glyph.height = height;
7813 last_mouse_glyph.x = gx;
7814 last_mouse_glyph.y = gy;
7815 }
7816
7817 *bar_window = Qnil;
7818 *part = 0;
7819 *fp = f1;
7820 XSETINT (*x, win_x);
7821 XSETINT (*y, win_y);
7822 *time = last_mouse_movement_time;
7823 }
7824 }
7825 }
7826
7827 UNBLOCK_INPUT;
7828 }
7829
7830
7831 #ifdef USE_X_TOOLKIT
7832
7833 /* Atimer callback function for TIMER. Called every 0.1s to process
7834 Xt timeouts, if needed. We must avoid calling XtAppPending as
7835 much as possible because that function does an implicit XFlush
7836 that slows us down. */
7837
7838 static void
7839 x_process_timeouts (timer)
7840 struct atimer *timer;
7841 {
7842 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7843 {
7844 BLOCK_INPUT;
7845 while (XtAppPending (Xt_app_con) & XtIMTimer)
7846 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7847 UNBLOCK_INPUT;
7848 }
7849 }
7850
7851 #endif /* USE_X_TOOLKIT */
7852
7853 \f
7854 /* Scroll bar support. */
7855
7856 /* Given an X window ID, find the struct scroll_bar which manages it.
7857 This can be called in GC, so we have to make sure to strip off mark
7858 bits. */
7859
7860 static struct scroll_bar *
7861 x_window_to_scroll_bar (window_id)
7862 Window window_id;
7863 {
7864 Lisp_Object tail;
7865
7866 for (tail = Vframe_list;
7867 XGCTYPE (tail) == Lisp_Cons;
7868 tail = XCDR (tail))
7869 {
7870 Lisp_Object frame, bar, condemned;
7871
7872 frame = XCAR (tail);
7873 /* All elements of Vframe_list should be frames. */
7874 if (! GC_FRAMEP (frame))
7875 abort ();
7876
7877 /* Scan this frame's scroll bar list for a scroll bar with the
7878 right window ID. */
7879 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7880 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7881 /* This trick allows us to search both the ordinary and
7882 condemned scroll bar lists with one loop. */
7883 ! GC_NILP (bar) || (bar = condemned,
7884 condemned = Qnil,
7885 ! GC_NILP (bar));
7886 bar = XSCROLL_BAR (bar)->next)
7887 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7888 return XSCROLL_BAR (bar);
7889 }
7890
7891 return 0;
7892 }
7893
7894
7895 \f
7896 /************************************************************************
7897 Toolkit scroll bars
7898 ************************************************************************/
7899
7900 #ifdef USE_TOOLKIT_SCROLL_BARS
7901
7902 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7903 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7904 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7905 struct scroll_bar *));
7906 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7907 int, int, int));
7908
7909
7910 /* Id of action hook installed for scroll bars. */
7911
7912 static XtActionHookId action_hook_id;
7913
7914 /* Lisp window being scrolled. Set when starting to interact with
7915 a toolkit scroll bar, reset to nil when ending the interaction. */
7916
7917 static Lisp_Object window_being_scrolled;
7918
7919 /* Last scroll bar part sent in xm_scroll_callback. */
7920
7921 static int last_scroll_bar_part;
7922
7923 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7924 that movements of 1/20 of the screen size are mapped to up/down. */
7925
7926 static Boolean xaw3d_arrow_scroll;
7927
7928 /* Whether the drag scrolling maintains the mouse at the top of the
7929 thumb. If not, resizing the thumb needs to be done more carefully
7930 to avoid jerkyness. */
7931
7932 static Boolean xaw3d_pick_top;
7933
7934
7935 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7936 bars are used.. The hook is responsible for detecting when
7937 the user ends an interaction with the scroll bar, and generates
7938 a `end-scroll' scroll_bar_click' event if so. */
7939
7940 static void
7941 xt_action_hook (widget, client_data, action_name, event, params,
7942 num_params)
7943 Widget widget;
7944 XtPointer client_data;
7945 String action_name;
7946 XEvent *event;
7947 String *params;
7948 Cardinal *num_params;
7949 {
7950 int scroll_bar_p;
7951 char *end_action;
7952
7953 #ifdef USE_MOTIF
7954 scroll_bar_p = XmIsScrollBar (widget);
7955 end_action = "Release";
7956 #else /* !USE_MOTIF i.e. use Xaw */
7957 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7958 end_action = "EndScroll";
7959 #endif /* USE_MOTIF */
7960
7961 if (scroll_bar_p
7962 && strcmp (action_name, end_action) == 0
7963 && WINDOWP (window_being_scrolled))
7964 {
7965 struct window *w;
7966
7967 x_send_scroll_bar_event (window_being_scrolled,
7968 scroll_bar_end_scroll, 0, 0);
7969 w = XWINDOW (window_being_scrolled);
7970 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7971 window_being_scrolled = Qnil;
7972 last_scroll_bar_part = -1;
7973
7974 /* Xt timeouts no longer needed. */
7975 toolkit_scroll_bar_interaction = 0;
7976 }
7977 }
7978
7979 /* A vector of windows used for communication between
7980 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7981
7982 static struct window **scroll_bar_windows;
7983 static int scroll_bar_windows_size;
7984
7985
7986 /* Send a client message with message type Xatom_Scrollbar for a
7987 scroll action to the frame of WINDOW. PART is a value identifying
7988 the part of the scroll bar that was clicked on. PORTION is the
7989 amount to scroll of a whole of WHOLE. */
7990
7991 static void
7992 x_send_scroll_bar_event (window, part, portion, whole)
7993 Lisp_Object window;
7994 int part, portion, whole;
7995 {
7996 XEvent event;
7997 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7998 struct window *w = XWINDOW (window);
7999 struct frame *f = XFRAME (w->frame);
8000 int i;
8001
8002 BLOCK_INPUT;
8003
8004 /* Construct a ClientMessage event to send to the frame. */
8005 ev->type = ClientMessage;
8006 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8007 ev->display = FRAME_X_DISPLAY (f);
8008 ev->window = FRAME_X_WINDOW (f);
8009 ev->format = 32;
8010
8011 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8012 not enough to store a pointer or Lisp_Object on a 64 bit system.
8013 So, store the window in scroll_bar_windows and pass the index
8014 into that array in the event. */
8015 for (i = 0; i < scroll_bar_windows_size; ++i)
8016 if (scroll_bar_windows[i] == NULL)
8017 break;
8018
8019 if (i == scroll_bar_windows_size)
8020 {
8021 int new_size = max (10, 2 * scroll_bar_windows_size);
8022 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8023 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8024
8025 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8026 nbytes);
8027 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8028 scroll_bar_windows_size = new_size;
8029 }
8030
8031 scroll_bar_windows[i] = w;
8032 ev->data.l[0] = (long) i;
8033 ev->data.l[1] = (long) part;
8034 ev->data.l[2] = (long) 0;
8035 ev->data.l[3] = (long) portion;
8036 ev->data.l[4] = (long) whole;
8037
8038 /* Make Xt timeouts work while the scroll bar is active. */
8039 toolkit_scroll_bar_interaction = 1;
8040
8041 /* Setting the event mask to zero means that the message will
8042 be sent to the client that created the window, and if that
8043 window no longer exists, no event will be sent. */
8044 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8045 UNBLOCK_INPUT;
8046 }
8047
8048
8049 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8050 in *IEVENT. */
8051
8052 static void
8053 x_scroll_bar_to_input_event (event, ievent)
8054 XEvent *event;
8055 struct input_event *ievent;
8056 {
8057 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8058 Lisp_Object window;
8059 struct frame *f;
8060 struct window *w;
8061
8062 w = scroll_bar_windows[ev->data.l[0]];
8063 scroll_bar_windows[ev->data.l[0]] = NULL;
8064
8065 XSETWINDOW (window, w);
8066 f = XFRAME (w->frame);
8067
8068 ievent->kind = scroll_bar_click;
8069 ievent->frame_or_window = window;
8070 ievent->arg = Qnil;
8071 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8072 ievent->part = ev->data.l[1];
8073 ievent->code = ev->data.l[2];
8074 ievent->x = make_number ((int) ev->data.l[3]);
8075 ievent->y = make_number ((int) ev->data.l[4]);
8076 ievent->modifiers = 0;
8077 }
8078
8079
8080 #ifdef USE_MOTIF
8081
8082 /* Minimum and maximum values used for Motif scroll bars. */
8083
8084 #define XM_SB_MIN 1
8085 #define XM_SB_MAX 10000000
8086 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8087
8088
8089 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8090 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8091 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8092
8093 static void
8094 xm_scroll_callback (widget, client_data, call_data)
8095 Widget widget;
8096 XtPointer client_data, call_data;
8097 {
8098 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8099 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8100 double percent;
8101 int part = -1, whole = 0, portion = 0;
8102
8103 switch (cs->reason)
8104 {
8105 case XmCR_DECREMENT:
8106 bar->dragging = Qnil;
8107 part = scroll_bar_up_arrow;
8108 break;
8109
8110 case XmCR_INCREMENT:
8111 bar->dragging = Qnil;
8112 part = scroll_bar_down_arrow;
8113 break;
8114
8115 case XmCR_PAGE_DECREMENT:
8116 bar->dragging = Qnil;
8117 part = scroll_bar_above_handle;
8118 break;
8119
8120 case XmCR_PAGE_INCREMENT:
8121 bar->dragging = Qnil;
8122 part = scroll_bar_below_handle;
8123 break;
8124
8125 case XmCR_TO_TOP:
8126 bar->dragging = Qnil;
8127 part = scroll_bar_to_top;
8128 break;
8129
8130 case XmCR_TO_BOTTOM:
8131 bar->dragging = Qnil;
8132 part = scroll_bar_to_bottom;
8133 break;
8134
8135 case XmCR_DRAG:
8136 {
8137 int slider_size;
8138 int dragging_down_p = (INTEGERP (bar->dragging)
8139 && XINT (bar->dragging) <= cs->value);
8140
8141 /* Get the slider size. */
8142 BLOCK_INPUT;
8143 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8144 UNBLOCK_INPUT;
8145
8146 /* At the max position of the scroll bar, do a line-wise
8147 movement. Without doing anything, we would be called with
8148 the same cs->value again and again. If we want to make
8149 sure that we can reach the end of the buffer, we have to do
8150 something.
8151
8152 Implementation note: setting bar->dragging always to
8153 cs->value gives a smoother movement at the max position.
8154 Setting it to nil when doing line-wise movement gives
8155 a better slider behavior. */
8156
8157 if (cs->value + slider_size == XM_SB_MAX
8158 || (dragging_down_p
8159 && last_scroll_bar_part == scroll_bar_down_arrow))
8160 {
8161 part = scroll_bar_down_arrow;
8162 bar->dragging = Qnil;
8163 }
8164 else
8165 {
8166 whole = XM_SB_RANGE;
8167 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8168 part = scroll_bar_handle;
8169 bar->dragging = make_number (cs->value);
8170 }
8171 }
8172 break;
8173
8174 case XmCR_VALUE_CHANGED:
8175 break;
8176 };
8177
8178 if (part >= 0)
8179 {
8180 window_being_scrolled = bar->window;
8181 last_scroll_bar_part = part;
8182 x_send_scroll_bar_event (bar->window, part, portion, whole);
8183 }
8184 }
8185
8186
8187 #else /* !USE_MOTIF, i.e. Xaw. */
8188
8189
8190 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8191 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8192 scroll bar struct. CALL_DATA is a pointer to a float saying where
8193 the thumb is. */
8194
8195 static void
8196 xaw_jump_callback (widget, client_data, call_data)
8197 Widget widget;
8198 XtPointer client_data, call_data;
8199 {
8200 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8201 float top = *(float *) call_data;
8202 float shown;
8203 int whole, portion, height;
8204 int part;
8205
8206 /* Get the size of the thumb, a value between 0 and 1. */
8207 BLOCK_INPUT;
8208 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8209 UNBLOCK_INPUT;
8210
8211 whole = 10000000;
8212 portion = shown < 1 ? top * whole : 0;
8213
8214 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8215 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8216 the bottom, so we force the scrolling whenever we see that we're
8217 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8218 we try to ensure that we always stay two pixels away from the
8219 bottom). */
8220 part = scroll_bar_down_arrow;
8221 else
8222 part = scroll_bar_handle;
8223
8224 window_being_scrolled = bar->window;
8225 bar->dragging = make_number (portion);
8226 last_scroll_bar_part = part;
8227 x_send_scroll_bar_event (bar->window, part, portion, whole);
8228 }
8229
8230
8231 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8232 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8233 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8234 the scroll bar. CALL_DATA is an integer specifying the action that
8235 has taken place. It's magnitude is in the range 0..height of the
8236 scroll bar. Negative values mean scroll towards buffer start.
8237 Values < height of scroll bar mean line-wise movement. */
8238
8239 static void
8240 xaw_scroll_callback (widget, client_data, call_data)
8241 Widget widget;
8242 XtPointer client_data, call_data;
8243 {
8244 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8245 int position = (int) call_data;
8246 Dimension height;
8247 int part;
8248
8249 /* Get the height of the scroll bar. */
8250 BLOCK_INPUT;
8251 XtVaGetValues (widget, XtNheight, &height, NULL);
8252 UNBLOCK_INPUT;
8253
8254 if (abs (position) >= height)
8255 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8256
8257 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8258 it maps line-movement to call_data = max(5, height/20). */
8259 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8260 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8261 else
8262 part = scroll_bar_move_ratio;
8263
8264 window_being_scrolled = bar->window;
8265 bar->dragging = Qnil;
8266 last_scroll_bar_part = part;
8267 x_send_scroll_bar_event (bar->window, part, position, height);
8268 }
8269
8270
8271 #endif /* not USE_MOTIF */
8272
8273
8274 /* Create the widget for scroll bar BAR on frame F. Record the widget
8275 and X window of the scroll bar in BAR. */
8276
8277 static void
8278 x_create_toolkit_scroll_bar (f, bar)
8279 struct frame *f;
8280 struct scroll_bar *bar;
8281 {
8282 Window xwindow;
8283 Widget widget;
8284 Arg av[20];
8285 int ac = 0;
8286 char *scroll_bar_name = "verticalScrollBar";
8287 unsigned long pixel;
8288
8289 BLOCK_INPUT;
8290
8291 #ifdef USE_MOTIF
8292 /* Set resources. Create the widget. */
8293 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8294 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8295 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8296 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8297 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8298 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8299 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8300
8301 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8302 if (pixel != -1)
8303 {
8304 XtSetArg (av[ac], XmNforeground, pixel);
8305 ++ac;
8306 }
8307
8308 pixel = f->output_data.x->scroll_bar_background_pixel;
8309 if (pixel != -1)
8310 {
8311 XtSetArg (av[ac], XmNbackground, pixel);
8312 ++ac;
8313 }
8314
8315 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8316 scroll_bar_name, av, ac);
8317
8318 /* Add one callback for everything that can happen. */
8319 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8320 (XtPointer) bar);
8321 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8322 (XtPointer) bar);
8323 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8324 (XtPointer) bar);
8325 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8326 (XtPointer) bar);
8327 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8328 (XtPointer) bar);
8329 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8330 (XtPointer) bar);
8331 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8332 (XtPointer) bar);
8333
8334 /* Realize the widget. Only after that is the X window created. */
8335 XtRealizeWidget (widget);
8336
8337 /* Set the cursor to an arrow. I didn't find a resource to do that.
8338 And I'm wondering why it hasn't an arrow cursor by default. */
8339 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8340 f->output_data.x->nontext_cursor);
8341
8342 #else /* !USE_MOTIF i.e. use Xaw */
8343
8344 /* Set resources. Create the widget. The background of the
8345 Xaw3d scroll bar widget is a little bit light for my taste.
8346 We don't alter it here to let users change it according
8347 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8348 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8349 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8350 /* For smoother scrolling with Xaw3d -sm */
8351 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8352 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8353
8354 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8355 if (pixel != -1)
8356 {
8357 XtSetArg (av[ac], XtNforeground, pixel);
8358 ++ac;
8359 }
8360
8361 pixel = f->output_data.x->scroll_bar_background_pixel;
8362 if (pixel != -1)
8363 {
8364 XtSetArg (av[ac], XtNbackground, pixel);
8365 ++ac;
8366 }
8367
8368 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8369 f->output_data.x->edit_widget, av, ac);
8370
8371 {
8372 char *initial = "";
8373 char *val = initial;
8374 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8375 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8376 if (val == initial)
8377 { /* ARROW_SCROLL */
8378 xaw3d_arrow_scroll = True;
8379 /* Isn't that just a personal preference ? -sm */
8380 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8381 }
8382 }
8383
8384 /* Define callbacks. */
8385 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8386 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8387 (XtPointer) bar);
8388
8389 /* Realize the widget. Only after that is the X window created. */
8390 XtRealizeWidget (widget);
8391
8392 #endif /* !USE_MOTIF */
8393
8394 /* Install an action hook that let's us detect when the user
8395 finishes interacting with a scroll bar. */
8396 if (action_hook_id == 0)
8397 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8398
8399 /* Remember X window and widget in the scroll bar vector. */
8400 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8401 xwindow = XtWindow (widget);
8402 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8403
8404 UNBLOCK_INPUT;
8405 }
8406
8407
8408 /* Set the thumb size and position of scroll bar BAR. We are currently
8409 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8410
8411 static void
8412 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8413 struct scroll_bar *bar;
8414 int portion, position, whole;
8415 {
8416 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8417 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8418 float top, shown;
8419
8420 if (whole == 0)
8421 top = 0, shown = 1;
8422 else
8423 {
8424 top = (float) position / whole;
8425 shown = (float) portion / whole;
8426 }
8427
8428 BLOCK_INPUT;
8429
8430 #ifdef USE_MOTIF
8431 {
8432 int size, value;
8433 XmScrollBarWidget sb;
8434
8435 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8436 is the scroll bar's maximum and MIN is the scroll bar's minimum
8437 value. */
8438 size = shown * XM_SB_RANGE;
8439 size = min (size, XM_SB_RANGE);
8440 size = max (size, 1);
8441
8442 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8443 value = top * XM_SB_RANGE;
8444 value = min (value, XM_SB_MAX - size);
8445 value = max (value, XM_SB_MIN);
8446
8447 if (NILP (bar->dragging))
8448 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8449 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8450 /* This has the negative side effect that the slider value is
8451 not what it would be if we scrolled here using line-wise or
8452 page-wise movement. */
8453 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8454 else
8455 {
8456 /* If currently dragging, only update the slider size.
8457 This reduces flicker effects. */
8458 int old_value, old_size, increment, page_increment;
8459
8460 XmScrollBarGetValues (widget, &old_value, &old_size,
8461 &increment, &page_increment);
8462 XmScrollBarSetValues (widget, old_value,
8463 min (size, XM_SB_RANGE - old_value),
8464 0, 0, False);
8465 }
8466 }
8467 #else /* !USE_MOTIF i.e. use Xaw */
8468 {
8469 float old_top, old_shown;
8470 Dimension height;
8471 XtVaGetValues (widget,
8472 XtNtopOfThumb, &old_top,
8473 XtNshown, &old_shown,
8474 XtNheight, &height,
8475 NULL);
8476
8477 /* Massage the top+shown values. */
8478 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8479 top = max (0, min (1, top));
8480 else
8481 top = old_top;
8482 /* Keep two pixels available for moving the thumb down. */
8483 shown = max (0, min (1 - top - (2.0 / height), shown));
8484
8485 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8486 check that your system's configuration file contains a define
8487 for `NARROWPROTO'. See s/freebsd.h for an example. */
8488 if (top != old_top || shown != old_shown)
8489 {
8490 if (NILP (bar->dragging))
8491 XawScrollbarSetThumb (widget, top, shown);
8492 else
8493 {
8494 #ifdef HAVE_XAW3D
8495 ScrollbarWidget sb = (ScrollbarWidget) widget;
8496 int scroll_mode = 0;
8497
8498 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8499 if (xaw3d_arrow_scroll)
8500 {
8501 /* Xaw3d stupidly ignores resize requests while dragging
8502 so we have to make it believe it's not in dragging mode. */
8503 scroll_mode = sb->scrollbar.scroll_mode;
8504 if (scroll_mode == 2)
8505 sb->scrollbar.scroll_mode = 0;
8506 }
8507 #endif
8508 /* Try to make the scrolling a tad smoother. */
8509 if (!xaw3d_pick_top)
8510 shown = min (shown, old_shown);
8511
8512 XawScrollbarSetThumb (widget, top, shown);
8513
8514 #ifdef HAVE_XAW3D
8515 if (xaw3d_arrow_scroll && scroll_mode == 2)
8516 sb->scrollbar.scroll_mode = scroll_mode;
8517 #endif
8518 }
8519 }
8520 }
8521 #endif /* !USE_MOTIF */
8522
8523 UNBLOCK_INPUT;
8524 }
8525
8526 #endif /* USE_TOOLKIT_SCROLL_BARS */
8527
8528
8529 \f
8530 /************************************************************************
8531 Scroll bars, general
8532 ************************************************************************/
8533
8534 /* Create a scroll bar and return the scroll bar vector for it. W is
8535 the Emacs window on which to create the scroll bar. TOP, LEFT,
8536 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8537 scroll bar. */
8538
8539 static struct scroll_bar *
8540 x_scroll_bar_create (w, top, left, width, height)
8541 struct window *w;
8542 int top, left, width, height;
8543 {
8544 struct frame *f = XFRAME (w->frame);
8545 struct scroll_bar *bar
8546 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8547
8548 BLOCK_INPUT;
8549
8550 #ifdef USE_TOOLKIT_SCROLL_BARS
8551 x_create_toolkit_scroll_bar (f, bar);
8552 #else /* not USE_TOOLKIT_SCROLL_BARS */
8553 {
8554 XSetWindowAttributes a;
8555 unsigned long mask;
8556 Window window;
8557
8558 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8559 if (a.background_pixel == -1)
8560 a.background_pixel = f->output_data.x->background_pixel;
8561
8562 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8563 | ButtonMotionMask | PointerMotionHintMask
8564 | ExposureMask);
8565 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8566
8567 mask = (CWBackPixel | CWEventMask | CWCursor);
8568
8569 /* Clear the area of W that will serve as a scroll bar. This is
8570 for the case that a window has been split horizontally. In
8571 this case, no clear_frame is generated to reduce flickering. */
8572 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8573 left, top, width,
8574 window_box_height (w), False);
8575
8576 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8577 /* Position and size of scroll bar. */
8578 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8579 top,
8580 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8581 height,
8582 /* Border width, depth, class, and visual. */
8583 0,
8584 CopyFromParent,
8585 CopyFromParent,
8586 CopyFromParent,
8587 /* Attributes. */
8588 mask, &a);
8589 SET_SCROLL_BAR_X_WINDOW (bar, window);
8590 }
8591 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8592
8593 XSETWINDOW (bar->window, w);
8594 XSETINT (bar->top, top);
8595 XSETINT (bar->left, left);
8596 XSETINT (bar->width, width);
8597 XSETINT (bar->height, height);
8598 XSETINT (bar->start, 0);
8599 XSETINT (bar->end, 0);
8600 bar->dragging = Qnil;
8601
8602 /* Add bar to its frame's list of scroll bars. */
8603 bar->next = FRAME_SCROLL_BARS (f);
8604 bar->prev = Qnil;
8605 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8606 if (!NILP (bar->next))
8607 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8608
8609 /* Map the window/widget. */
8610 #ifdef USE_TOOLKIT_SCROLL_BARS
8611 {
8612 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8613 XtConfigureWidget (scroll_bar,
8614 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8615 top,
8616 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8617 max (height, 1), 0);
8618 XtMapWidget (scroll_bar);
8619 }
8620 #else /* not USE_TOOLKIT_SCROLL_BARS */
8621 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8622 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8623
8624 UNBLOCK_INPUT;
8625 return bar;
8626 }
8627
8628
8629 /* Draw BAR's handle in the proper position.
8630
8631 If the handle is already drawn from START to END, don't bother
8632 redrawing it, unless REBUILD is non-zero; in that case, always
8633 redraw it. (REBUILD is handy for drawing the handle after expose
8634 events.)
8635
8636 Normally, we want to constrain the start and end of the handle to
8637 fit inside its rectangle, but if the user is dragging the scroll
8638 bar handle, we want to let them drag it down all the way, so that
8639 the bar's top is as far down as it goes; otherwise, there's no way
8640 to move to the very end of the buffer. */
8641
8642 #ifndef USE_TOOLKIT_SCROLL_BARS
8643
8644 static void
8645 x_scroll_bar_set_handle (bar, start, end, rebuild)
8646 struct scroll_bar *bar;
8647 int start, end;
8648 int rebuild;
8649 {
8650 int dragging = ! NILP (bar->dragging);
8651 Window w = SCROLL_BAR_X_WINDOW (bar);
8652 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8653 GC gc = f->output_data.x->normal_gc;
8654
8655 /* If the display is already accurate, do nothing. */
8656 if (! rebuild
8657 && start == XINT (bar->start)
8658 && end == XINT (bar->end))
8659 return;
8660
8661 BLOCK_INPUT;
8662
8663 {
8664 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8665 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8666 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8667
8668 /* Make sure the values are reasonable, and try to preserve
8669 the distance between start and end. */
8670 {
8671 int length = end - start;
8672
8673 if (start < 0)
8674 start = 0;
8675 else if (start > top_range)
8676 start = top_range;
8677 end = start + length;
8678
8679 if (end < start)
8680 end = start;
8681 else if (end > top_range && ! dragging)
8682 end = top_range;
8683 }
8684
8685 /* Store the adjusted setting in the scroll bar. */
8686 XSETINT (bar->start, start);
8687 XSETINT (bar->end, end);
8688
8689 /* Clip the end position, just for display. */
8690 if (end > top_range)
8691 end = top_range;
8692
8693 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8694 below top positions, to make sure the handle is always at least
8695 that many pixels tall. */
8696 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8697
8698 /* Draw the empty space above the handle. Note that we can't clear
8699 zero-height areas; that means "clear to end of window." */
8700 if (0 < start)
8701 x_clear_area (FRAME_X_DISPLAY (f), w,
8702 /* x, y, width, height, and exposures. */
8703 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8704 VERTICAL_SCROLL_BAR_TOP_BORDER,
8705 inside_width, start,
8706 False);
8707
8708 /* Change to proper foreground color if one is specified. */
8709 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8710 XSetForeground (FRAME_X_DISPLAY (f), gc,
8711 f->output_data.x->scroll_bar_foreground_pixel);
8712
8713 /* Draw the handle itself. */
8714 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8715
8716 /* x, y, width, height */
8717 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8718 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8719 inside_width, end - start);
8720
8721 /* Restore the foreground color of the GC if we changed it above. */
8722 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8723 XSetForeground (FRAME_X_DISPLAY (f), gc,
8724 f->output_data.x->foreground_pixel);
8725
8726 /* Draw the empty space below the handle. Note that we can't
8727 clear zero-height areas; that means "clear to end of window." */
8728 if (end < inside_height)
8729 x_clear_area (FRAME_X_DISPLAY (f), w,
8730 /* x, y, width, height, and exposures. */
8731 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8732 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8733 inside_width, inside_height - end,
8734 False);
8735
8736 }
8737
8738 UNBLOCK_INPUT;
8739 }
8740
8741 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8742
8743 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8744 nil. */
8745
8746 static void
8747 x_scroll_bar_remove (bar)
8748 struct scroll_bar *bar;
8749 {
8750 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8751 BLOCK_INPUT;
8752
8753 #ifdef USE_TOOLKIT_SCROLL_BARS
8754 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8755 #else
8756 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8757 #endif
8758
8759 /* Disassociate this scroll bar from its window. */
8760 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8761
8762 UNBLOCK_INPUT;
8763 }
8764
8765
8766 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8767 that we are displaying PORTION characters out of a total of WHOLE
8768 characters, starting at POSITION. If WINDOW has no scroll bar,
8769 create one. */
8770
8771 static void
8772 XTset_vertical_scroll_bar (w, portion, whole, position)
8773 struct window *w;
8774 int portion, whole, position;
8775 {
8776 struct frame *f = XFRAME (w->frame);
8777 struct scroll_bar *bar;
8778 int top, height, left, sb_left, width, sb_width;
8779 int window_x, window_y, window_width, window_height;
8780
8781 /* Get window dimensions. */
8782 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8783 top = window_y;
8784 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8785 height = window_height;
8786
8787 /* Compute the left edge of the scroll bar area. */
8788 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8789 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8790 else
8791 left = XFASTINT (w->left);
8792 left *= CANON_X_UNIT (f);
8793 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8794
8795 /* Compute the width of the scroll bar which might be less than
8796 the width of the area reserved for the scroll bar. */
8797 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8798 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8799 else
8800 sb_width = width;
8801
8802 /* Compute the left edge of the scroll bar. */
8803 #ifdef USE_TOOLKIT_SCROLL_BARS
8804 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8805 sb_left = left + width - sb_width - (width - sb_width) / 2;
8806 else
8807 sb_left = left + (width - sb_width) / 2;
8808 #else
8809 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8810 sb_left = left + width - sb_width;
8811 else
8812 sb_left = left;
8813 #endif
8814
8815 /* Does the scroll bar exist yet? */
8816 if (NILP (w->vertical_scroll_bar))
8817 {
8818 BLOCK_INPUT;
8819 if (width && height)
8820 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8821 left, top, width, height, False);
8822 UNBLOCK_INPUT;
8823 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8824 }
8825 else
8826 {
8827 /* It may just need to be moved and resized. */
8828 unsigned int mask = 0;
8829
8830 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8831
8832 BLOCK_INPUT;
8833
8834 if (sb_left != XINT (bar->left))
8835 mask |= CWX;
8836 if (top != XINT (bar->top))
8837 mask |= CWY;
8838 if (sb_width != XINT (bar->width))
8839 mask |= CWWidth;
8840 if (height != XINT (bar->height))
8841 mask |= CWHeight;
8842
8843 #ifdef USE_TOOLKIT_SCROLL_BARS
8844
8845 /* Since toolkit scroll bars are smaller than the space reserved
8846 for them on the frame, we have to clear "under" them. */
8847 if (width && height)
8848 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8849 left, top, width, height, False);
8850
8851 /* Move/size the scroll bar widget. */
8852 if (mask)
8853 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
8854 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8855 top,
8856 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8857 max (height, 1), 0);
8858
8859 #else /* not USE_TOOLKIT_SCROLL_BARS */
8860
8861 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8862 {
8863 /* Clear areas not covered by the scroll bar. This makes sure a
8864 previous mode line display is cleared after C-x 2 C-x 1, for
8865 example. Non-toolkit scroll bars are as wide as the area
8866 reserved for scroll bars - trim at both sides. */
8867 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8868 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8869 height, False);
8870 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8871 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8872 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8873 height, False);
8874 }
8875
8876 /* Move/size the scroll bar window. */
8877 if (mask)
8878 {
8879 XWindowChanges wc;
8880
8881 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8882 wc.y = top;
8883 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8884 wc.height = height;
8885 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8886 mask, &wc);
8887 }
8888
8889 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8890
8891 /* Remember new settings. */
8892 XSETINT (bar->left, sb_left);
8893 XSETINT (bar->top, top);
8894 XSETINT (bar->width, sb_width);
8895 XSETINT (bar->height, height);
8896
8897 UNBLOCK_INPUT;
8898 }
8899
8900 #ifdef USE_TOOLKIT_SCROLL_BARS
8901 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8902 #else /* not USE_TOOLKIT_SCROLL_BARS */
8903 /* Set the scroll bar's current state, unless we're currently being
8904 dragged. */
8905 if (NILP (bar->dragging))
8906 {
8907 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8908
8909 if (whole == 0)
8910 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8911 else
8912 {
8913 int start = ((double) position * top_range) / whole;
8914 int end = ((double) (position + portion) * top_range) / whole;
8915 x_scroll_bar_set_handle (bar, start, end, 0);
8916 }
8917 }
8918 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8919
8920 XSETVECTOR (w->vertical_scroll_bar, bar);
8921 }
8922
8923
8924 /* The following three hooks are used when we're doing a thorough
8925 redisplay of the frame. We don't explicitly know which scroll bars
8926 are going to be deleted, because keeping track of when windows go
8927 away is a real pain - "Can you say set-window-configuration, boys
8928 and girls?" Instead, we just assert at the beginning of redisplay
8929 that *all* scroll bars are to be removed, and then save a scroll bar
8930 from the fiery pit when we actually redisplay its window. */
8931
8932 /* Arrange for all scroll bars on FRAME to be removed at the next call
8933 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8934 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8935
8936 static void
8937 XTcondemn_scroll_bars (frame)
8938 FRAME_PTR frame;
8939 {
8940 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8941 while (! NILP (FRAME_SCROLL_BARS (frame)))
8942 {
8943 Lisp_Object bar;
8944 bar = FRAME_SCROLL_BARS (frame);
8945 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8946 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8947 XSCROLL_BAR (bar)->prev = Qnil;
8948 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8949 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8950 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8951 }
8952 }
8953
8954
8955 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8956 Note that WINDOW isn't necessarily condemned at all. */
8957
8958 static void
8959 XTredeem_scroll_bar (window)
8960 struct window *window;
8961 {
8962 struct scroll_bar *bar;
8963 struct frame *f;
8964
8965 /* We can't redeem this window's scroll bar if it doesn't have one. */
8966 if (NILP (window->vertical_scroll_bar))
8967 abort ();
8968
8969 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8970
8971 /* Unlink it from the condemned list. */
8972 f = XFRAME (WINDOW_FRAME (window));
8973 if (NILP (bar->prev))
8974 {
8975 /* If the prev pointer is nil, it must be the first in one of
8976 the lists. */
8977 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8978 /* It's not condemned. Everything's fine. */
8979 return;
8980 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8981 window->vertical_scroll_bar))
8982 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8983 else
8984 /* If its prev pointer is nil, it must be at the front of
8985 one or the other! */
8986 abort ();
8987 }
8988 else
8989 XSCROLL_BAR (bar->prev)->next = bar->next;
8990
8991 if (! NILP (bar->next))
8992 XSCROLL_BAR (bar->next)->prev = bar->prev;
8993
8994 bar->next = FRAME_SCROLL_BARS (f);
8995 bar->prev = Qnil;
8996 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8997 if (! NILP (bar->next))
8998 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8999 }
9000
9001 /* Remove all scroll bars on FRAME that haven't been saved since the
9002 last call to `*condemn_scroll_bars_hook'. */
9003
9004 static void
9005 XTjudge_scroll_bars (f)
9006 FRAME_PTR f;
9007 {
9008 Lisp_Object bar, next;
9009
9010 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9011
9012 /* Clear out the condemned list now so we won't try to process any
9013 more events on the hapless scroll bars. */
9014 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9015
9016 for (; ! NILP (bar); bar = next)
9017 {
9018 struct scroll_bar *b = XSCROLL_BAR (bar);
9019
9020 x_scroll_bar_remove (b);
9021
9022 next = b->next;
9023 b->next = b->prev = Qnil;
9024 }
9025
9026 /* Now there should be no references to the condemned scroll bars,
9027 and they should get garbage-collected. */
9028 }
9029
9030
9031 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9032 is a no-op when using toolkit scroll bars.
9033
9034 This may be called from a signal handler, so we have to ignore GC
9035 mark bits. */
9036
9037 static void
9038 x_scroll_bar_expose (bar, event)
9039 struct scroll_bar *bar;
9040 XEvent *event;
9041 {
9042 #ifndef USE_TOOLKIT_SCROLL_BARS
9043
9044 Window w = SCROLL_BAR_X_WINDOW (bar);
9045 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9046 GC gc = f->output_data.x->normal_gc;
9047 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9048
9049 BLOCK_INPUT;
9050
9051 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9052
9053 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9054 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9055
9056 /* x, y, width, height */
9057 0, 0,
9058 XINT (bar->width) - 1 - width_trim - width_trim,
9059 XINT (bar->height) - 1);
9060
9061 UNBLOCK_INPUT;
9062
9063 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9064 }
9065
9066 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9067 is set to something other than no_event, it is enqueued.
9068
9069 This may be called from a signal handler, so we have to ignore GC
9070 mark bits. */
9071
9072 #ifndef USE_TOOLKIT_SCROLL_BARS
9073
9074 static void
9075 x_scroll_bar_handle_click (bar, event, emacs_event)
9076 struct scroll_bar *bar;
9077 XEvent *event;
9078 struct input_event *emacs_event;
9079 {
9080 if (! GC_WINDOWP (bar->window))
9081 abort ();
9082
9083 emacs_event->kind = scroll_bar_click;
9084 emacs_event->code = event->xbutton.button - Button1;
9085 emacs_event->modifiers
9086 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9087 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9088 event->xbutton.state)
9089 | (event->type == ButtonRelease
9090 ? up_modifier
9091 : down_modifier));
9092 emacs_event->frame_or_window = bar->window;
9093 emacs_event->arg = Qnil;
9094 emacs_event->timestamp = event->xbutton.time;
9095 {
9096 #if 0
9097 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9098 int internal_height
9099 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9100 #endif
9101 int top_range
9102 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9103 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9104
9105 if (y < 0) y = 0;
9106 if (y > top_range) y = top_range;
9107
9108 if (y < XINT (bar->start))
9109 emacs_event->part = scroll_bar_above_handle;
9110 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9111 emacs_event->part = scroll_bar_handle;
9112 else
9113 emacs_event->part = scroll_bar_below_handle;
9114
9115 /* Just because the user has clicked on the handle doesn't mean
9116 they want to drag it. Lisp code needs to be able to decide
9117 whether or not we're dragging. */
9118 #if 0
9119 /* If the user has just clicked on the handle, record where they're
9120 holding it. */
9121 if (event->type == ButtonPress
9122 && emacs_event->part == scroll_bar_handle)
9123 XSETINT (bar->dragging, y - XINT (bar->start));
9124 #endif
9125
9126 /* If the user has released the handle, set it to its final position. */
9127 if (event->type == ButtonRelease
9128 && ! NILP (bar->dragging))
9129 {
9130 int new_start = y - XINT (bar->dragging);
9131 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9132
9133 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9134 bar->dragging = Qnil;
9135 }
9136
9137 /* Same deal here as the other #if 0. */
9138 #if 0
9139 /* Clicks on the handle are always reported as occurring at the top of
9140 the handle. */
9141 if (emacs_event->part == scroll_bar_handle)
9142 emacs_event->x = bar->start;
9143 else
9144 XSETINT (emacs_event->x, y);
9145 #else
9146 XSETINT (emacs_event->x, y);
9147 #endif
9148
9149 XSETINT (emacs_event->y, top_range);
9150 }
9151 }
9152
9153 /* Handle some mouse motion while someone is dragging the scroll bar.
9154
9155 This may be called from a signal handler, so we have to ignore GC
9156 mark bits. */
9157
9158 static void
9159 x_scroll_bar_note_movement (bar, event)
9160 struct scroll_bar *bar;
9161 XEvent *event;
9162 {
9163 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9164
9165 last_mouse_movement_time = event->xmotion.time;
9166
9167 f->mouse_moved = 1;
9168 XSETVECTOR (last_mouse_scroll_bar, bar);
9169
9170 /* If we're dragging the bar, display it. */
9171 if (! GC_NILP (bar->dragging))
9172 {
9173 /* Where should the handle be now? */
9174 int new_start = event->xmotion.y - XINT (bar->dragging);
9175
9176 if (new_start != XINT (bar->start))
9177 {
9178 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9179
9180 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9181 }
9182 }
9183 }
9184
9185 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9186
9187 /* Return information to the user about the current position of the mouse
9188 on the scroll bar. */
9189
9190 static void
9191 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9192 FRAME_PTR *fp;
9193 Lisp_Object *bar_window;
9194 enum scroll_bar_part *part;
9195 Lisp_Object *x, *y;
9196 unsigned long *time;
9197 {
9198 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9199 Window w = SCROLL_BAR_X_WINDOW (bar);
9200 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9201 int win_x, win_y;
9202 Window dummy_window;
9203 int dummy_coord;
9204 unsigned int dummy_mask;
9205
9206 BLOCK_INPUT;
9207
9208 /* Get the mouse's position relative to the scroll bar window, and
9209 report that. */
9210 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9211
9212 /* Root, child, root x and root y. */
9213 &dummy_window, &dummy_window,
9214 &dummy_coord, &dummy_coord,
9215
9216 /* Position relative to scroll bar. */
9217 &win_x, &win_y,
9218
9219 /* Mouse buttons and modifier keys. */
9220 &dummy_mask))
9221 ;
9222 else
9223 {
9224 #if 0
9225 int inside_height
9226 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9227 #endif
9228 int top_range
9229 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9230
9231 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9232
9233 if (! NILP (bar->dragging))
9234 win_y -= XINT (bar->dragging);
9235
9236 if (win_y < 0)
9237 win_y = 0;
9238 if (win_y > top_range)
9239 win_y = top_range;
9240
9241 *fp = f;
9242 *bar_window = bar->window;
9243
9244 if (! NILP (bar->dragging))
9245 *part = scroll_bar_handle;
9246 else if (win_y < XINT (bar->start))
9247 *part = scroll_bar_above_handle;
9248 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9249 *part = scroll_bar_handle;
9250 else
9251 *part = scroll_bar_below_handle;
9252
9253 XSETINT (*x, win_y);
9254 XSETINT (*y, top_range);
9255
9256 f->mouse_moved = 0;
9257 last_mouse_scroll_bar = Qnil;
9258 }
9259
9260 *time = last_mouse_movement_time;
9261
9262 UNBLOCK_INPUT;
9263 }
9264
9265
9266 /* The screen has been cleared so we may have changed foreground or
9267 background colors, and the scroll bars may need to be redrawn.
9268 Clear out the scroll bars, and ask for expose events, so we can
9269 redraw them. */
9270
9271 void
9272 x_scroll_bar_clear (f)
9273 FRAME_PTR f;
9274 {
9275 #ifndef USE_TOOLKIT_SCROLL_BARS
9276 Lisp_Object bar;
9277
9278 /* We can have scroll bars even if this is 0,
9279 if we just turned off scroll bar mode.
9280 But in that case we should not clear them. */
9281 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9282 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9283 bar = XSCROLL_BAR (bar)->next)
9284 XClearArea (FRAME_X_DISPLAY (f),
9285 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9286 0, 0, 0, 0, True);
9287 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9288 }
9289
9290 /* This processes Expose events from the menu-bar specific X event
9291 loop in xmenu.c. This allows to redisplay the frame if necessary
9292 when handling menu-bar or pop-up items. */
9293
9294 int
9295 process_expose_from_menu (event)
9296 XEvent event;
9297 {
9298 FRAME_PTR f;
9299 struct x_display_info *dpyinfo;
9300 int frame_exposed_p = 0;
9301
9302 BLOCK_INPUT;
9303
9304 dpyinfo = x_display_info_for_display (event.xexpose.display);
9305 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9306 if (f)
9307 {
9308 if (f->async_visible == 0)
9309 {
9310 f->async_visible = 1;
9311 f->async_iconified = 0;
9312 f->output_data.x->has_been_visible = 1;
9313 SET_FRAME_GARBAGED (f);
9314 }
9315 else
9316 {
9317 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9318 event.xexpose.x, event.xexpose.y,
9319 event.xexpose.width, event.xexpose.height);
9320 frame_exposed_p = 1;
9321 }
9322 }
9323 else
9324 {
9325 struct scroll_bar *bar
9326 = x_window_to_scroll_bar (event.xexpose.window);
9327
9328 if (bar)
9329 x_scroll_bar_expose (bar, &event);
9330 }
9331
9332 UNBLOCK_INPUT;
9333 return frame_exposed_p;
9334 }
9335 \f
9336 /* Define a queue to save up SelectionRequest events for later handling. */
9337
9338 struct selection_event_queue
9339 {
9340 XEvent event;
9341 struct selection_event_queue *next;
9342 };
9343
9344 static struct selection_event_queue *queue;
9345
9346 /* Nonzero means queue up certain events--don't process them yet. */
9347
9348 static int x_queue_selection_requests;
9349
9350 /* Queue up an X event *EVENT, to be processed later. */
9351
9352 static void
9353 x_queue_event (f, event)
9354 FRAME_PTR f;
9355 XEvent *event;
9356 {
9357 struct selection_event_queue *queue_tmp
9358 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9359
9360 if (queue_tmp != NULL)
9361 {
9362 queue_tmp->event = *event;
9363 queue_tmp->next = queue;
9364 queue = queue_tmp;
9365 }
9366 }
9367
9368 /* Take all the queued events and put them back
9369 so that they get processed afresh. */
9370
9371 static void
9372 x_unqueue_events (display)
9373 Display *display;
9374 {
9375 while (queue != NULL)
9376 {
9377 struct selection_event_queue *queue_tmp = queue;
9378 XPutBackEvent (display, &queue_tmp->event);
9379 queue = queue_tmp->next;
9380 xfree ((char *)queue_tmp);
9381 }
9382 }
9383
9384 /* Start queuing SelectionRequest events. */
9385
9386 void
9387 x_start_queuing_selection_requests (display)
9388 Display *display;
9389 {
9390 x_queue_selection_requests++;
9391 }
9392
9393 /* Stop queuing SelectionRequest events. */
9394
9395 void
9396 x_stop_queuing_selection_requests (display)
9397 Display *display;
9398 {
9399 x_queue_selection_requests--;
9400 x_unqueue_events (display);
9401 }
9402 \f
9403 /* The main X event-reading loop - XTread_socket. */
9404
9405 /* Time stamp of enter window event. This is only used by XTread_socket,
9406 but we have to put it out here, since static variables within functions
9407 sometimes don't work. */
9408
9409 static Time enter_timestamp;
9410
9411 /* This holds the state XLookupString needs to implement dead keys
9412 and other tricks known as "compose processing". _X Window System_
9413 says that a portable program can't use this, but Stephen Gildea assures
9414 me that letting the compiler initialize it to zeros will work okay.
9415
9416 This must be defined outside of XTread_socket, for the same reasons
9417 given for enter_time stamp, above. */
9418
9419 static XComposeStatus compose_status;
9420
9421 /* Record the last 100 characters stored
9422 to help debug the loss-of-chars-during-GC problem. */
9423
9424 static int temp_index;
9425 static short temp_buffer[100];
9426
9427 /* Set this to nonzero to fake an "X I/O error"
9428 on a particular display. */
9429
9430 struct x_display_info *XTread_socket_fake_io_error;
9431
9432 /* When we find no input here, we occasionally do a no-op command
9433 to verify that the X server is still running and we can still talk with it.
9434 We try all the open displays, one by one.
9435 This variable is used for cycling thru the displays. */
9436
9437 static struct x_display_info *next_noop_dpyinfo;
9438
9439 #define SET_SAVED_MENU_EVENT(size) \
9440 do \
9441 { \
9442 if (f->output_data.x->saved_menu_event == 0) \
9443 f->output_data.x->saved_menu_event \
9444 = (XEvent *) xmalloc (sizeof (XEvent)); \
9445 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9446 if (numchars >= 1) \
9447 { \
9448 bufp->kind = menu_bar_activate_event; \
9449 XSETFRAME (bufp->frame_or_window, f); \
9450 bufp->arg = Qnil; \
9451 bufp++; \
9452 count++; \
9453 numchars--; \
9454 } \
9455 } \
9456 while (0)
9457
9458 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9459 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9460
9461 /* Read events coming from the X server.
9462 This routine is called by the SIGIO handler.
9463 We return as soon as there are no more events to be read.
9464
9465 Events representing keys are stored in buffer BUFP,
9466 which can hold up to NUMCHARS characters.
9467 We return the number of characters stored into the buffer,
9468 thus pretending to be `read'.
9469
9470 EXPECTED is nonzero if the caller knows input is available. */
9471
9472 int
9473 XTread_socket (sd, bufp, numchars, expected)
9474 register int sd;
9475 /* register */ struct input_event *bufp;
9476 /* register */ int numchars;
9477 int expected;
9478 {
9479 int count = 0;
9480 int nbytes = 0;
9481 XEvent event;
9482 struct frame *f;
9483 int event_found = 0;
9484 struct x_display_info *dpyinfo;
9485 struct coding_system coding;
9486
9487 if (interrupt_input_blocked)
9488 {
9489 interrupt_input_pending = 1;
9490 return -1;
9491 }
9492
9493 interrupt_input_pending = 0;
9494 BLOCK_INPUT;
9495
9496 /* So people can tell when we have read the available input. */
9497 input_signal_count++;
9498
9499 if (numchars <= 0)
9500 abort (); /* Don't think this happens. */
9501
9502 ++handling_signal;
9503
9504 /* The input should be decoded if it is from XIM. Currently the
9505 locale of XIM is the same as that of the system. So, we can use
9506 Vlocale_coding_system which is initialized properly at Emacs
9507 startup time. */
9508 setup_coding_system (Vlocale_coding_system, &coding);
9509 coding.src_multibyte = 0;
9510 coding.dst_multibyte = 1;
9511 /* The input is converted to events, thus we can't handle
9512 composition. Anyway, there's no XIM that gives us composition
9513 information. */
9514 coding.composing = COMPOSITION_DISABLED;
9515
9516 /* Find the display we are supposed to read input for.
9517 It's the one communicating on descriptor SD. */
9518 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9519 {
9520 #if 0 /* This ought to be unnecessary; let's verify it. */
9521 #ifdef FIOSNBIO
9522 /* If available, Xlib uses FIOSNBIO to make the socket
9523 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9524 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9525 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9526 fcntl (dpyinfo->connection, F_SETFL, 0);
9527 #endif /* ! defined (FIOSNBIO) */
9528 #endif
9529
9530 #if 0 /* This code can't be made to work, with multiple displays,
9531 and appears not to be used on any system any more.
9532 Also keyboard.c doesn't turn O_NDELAY on and off
9533 for X connections. */
9534 #ifndef SIGIO
9535 #ifndef HAVE_SELECT
9536 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9537 {
9538 extern int read_alarm_should_throw;
9539 read_alarm_should_throw = 1;
9540 XPeekEvent (dpyinfo->display, &event);
9541 read_alarm_should_throw = 0;
9542 }
9543 #endif /* HAVE_SELECT */
9544 #endif /* SIGIO */
9545 #endif
9546
9547 /* For debugging, this gives a way to fake an I/O error. */
9548 if (dpyinfo == XTread_socket_fake_io_error)
9549 {
9550 XTread_socket_fake_io_error = 0;
9551 x_io_error_quitter (dpyinfo->display);
9552 }
9553
9554 while (XPending (dpyinfo->display))
9555 {
9556 XNextEvent (dpyinfo->display, &event);
9557
9558 #ifdef HAVE_X_I18N
9559 {
9560 /* Filter events for the current X input method.
9561 XFilterEvent returns non-zero if the input method has
9562 consumed the event. We pass the frame's X window to
9563 XFilterEvent because that's the one for which the IC
9564 was created. */
9565 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9566 event.xclient.window);
9567 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9568 break;
9569 }
9570 #endif
9571 event_found = 1;
9572
9573 switch (event.type)
9574 {
9575 case ClientMessage:
9576 {
9577 if (event.xclient.message_type
9578 == dpyinfo->Xatom_wm_protocols
9579 && event.xclient.format == 32)
9580 {
9581 if (event.xclient.data.l[0]
9582 == dpyinfo->Xatom_wm_take_focus)
9583 {
9584 /* Use x_any_window_to_frame because this
9585 could be the shell widget window
9586 if the frame has no title bar. */
9587 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9588 #ifdef HAVE_X_I18N
9589 /* Not quite sure this is needed -pd */
9590 if (f && FRAME_XIC (f))
9591 XSetICFocus (FRAME_XIC (f));
9592 #endif
9593 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9594 instructs the WM to set the input focus automatically for
9595 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9596 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9597 it has set the focus. So, XSetInputFocus below is not
9598 needed.
9599
9600 The call to XSetInputFocus below has also caused trouble. In
9601 cases where the XSetInputFocus done by the WM and the one
9602 below are temporally close (on a fast machine), the call
9603 below can generate additional FocusIn events which confuse
9604 Emacs. */
9605
9606 /* Since we set WM_TAKE_FOCUS, we must call
9607 XSetInputFocus explicitly. But not if f is null,
9608 since that might be an event for a deleted frame. */
9609 if (f)
9610 {
9611 Display *d = event.xclient.display;
9612 /* Catch and ignore errors, in case window has been
9613 iconified by a window manager such as GWM. */
9614 int count = x_catch_errors (d);
9615 XSetInputFocus (d, event.xclient.window,
9616 /* The ICCCM says this is
9617 the only valid choice. */
9618 RevertToParent,
9619 event.xclient.data.l[1]);
9620 /* This is needed to detect the error
9621 if there is an error. */
9622 XSync (d, False);
9623 x_uncatch_errors (d, count);
9624 }
9625 /* Not certain about handling scroll bars here */
9626 #endif /* 0 */
9627 }
9628 else if (event.xclient.data.l[0]
9629 == dpyinfo->Xatom_wm_save_yourself)
9630 {
9631 /* Save state modify the WM_COMMAND property to
9632 something which can reinstate us. This notifies
9633 the session manager, who's looking for such a
9634 PropertyNotify. Can restart processing when
9635 a keyboard or mouse event arrives. */
9636 if (numchars > 0)
9637 {
9638 f = x_top_window_to_frame (dpyinfo,
9639 event.xclient.window);
9640
9641 /* This is just so we only give real data once
9642 for a single Emacs process. */
9643 if (f == SELECTED_FRAME ())
9644 XSetCommand (FRAME_X_DISPLAY (f),
9645 event.xclient.window,
9646 initial_argv, initial_argc);
9647 else if (f)
9648 XSetCommand (FRAME_X_DISPLAY (f),
9649 event.xclient.window,
9650 0, 0);
9651 }
9652 }
9653 else if (event.xclient.data.l[0]
9654 == dpyinfo->Xatom_wm_delete_window)
9655 {
9656 struct frame *f
9657 = x_any_window_to_frame (dpyinfo,
9658 event.xclient.window);
9659
9660 if (f)
9661 {
9662 if (numchars == 0)
9663 abort ();
9664
9665 bufp->kind = delete_window_event;
9666 XSETFRAME (bufp->frame_or_window, f);
9667 bufp->arg = Qnil;
9668 bufp++;
9669
9670 count += 1;
9671 numchars -= 1;
9672 }
9673 }
9674 }
9675 else if (event.xclient.message_type
9676 == dpyinfo->Xatom_wm_configure_denied)
9677 {
9678 }
9679 else if (event.xclient.message_type
9680 == dpyinfo->Xatom_wm_window_moved)
9681 {
9682 int new_x, new_y;
9683 struct frame *f
9684 = x_window_to_frame (dpyinfo, event.xclient.window);
9685
9686 new_x = event.xclient.data.s[0];
9687 new_y = event.xclient.data.s[1];
9688
9689 if (f)
9690 {
9691 f->output_data.x->left_pos = new_x;
9692 f->output_data.x->top_pos = new_y;
9693 }
9694 }
9695 #ifdef HACK_EDITRES
9696 else if (event.xclient.message_type
9697 == dpyinfo->Xatom_editres)
9698 {
9699 struct frame *f
9700 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9701 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9702 &event, NULL);
9703 }
9704 #endif /* HACK_EDITRES */
9705 else if ((event.xclient.message_type
9706 == dpyinfo->Xatom_DONE)
9707 || (event.xclient.message_type
9708 == dpyinfo->Xatom_PAGE))
9709 {
9710 /* Ghostview job completed. Kill it. We could
9711 reply with "Next" if we received "Page", but we
9712 currently never do because we are interested in
9713 images, only, which should have 1 page. */
9714 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9715 struct frame *f
9716 = x_window_to_frame (dpyinfo, event.xclient.window);
9717 x_kill_gs_process (pixmap, f);
9718 expose_frame (f, 0, 0, 0, 0);
9719 }
9720 #ifdef USE_TOOLKIT_SCROLL_BARS
9721 /* Scroll bar callbacks send a ClientMessage from which
9722 we construct an input_event. */
9723 else if (event.xclient.message_type
9724 == dpyinfo->Xatom_Scrollbar)
9725 {
9726 x_scroll_bar_to_input_event (&event, bufp);
9727 ++bufp, ++count, --numchars;
9728 goto out;
9729 }
9730 #endif /* USE_TOOLKIT_SCROLL_BARS */
9731 else
9732 goto OTHER;
9733 }
9734 break;
9735
9736 case SelectionNotify:
9737 #ifdef USE_X_TOOLKIT
9738 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9739 goto OTHER;
9740 #endif /* not USE_X_TOOLKIT */
9741 x_handle_selection_notify (&event.xselection);
9742 break;
9743
9744 case SelectionClear: /* Someone has grabbed ownership. */
9745 #ifdef USE_X_TOOLKIT
9746 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9747 goto OTHER;
9748 #endif /* USE_X_TOOLKIT */
9749 {
9750 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9751
9752 if (numchars == 0)
9753 abort ();
9754
9755 bufp->kind = selection_clear_event;
9756 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9757 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9758 SELECTION_EVENT_TIME (bufp) = eventp->time;
9759 bufp->frame_or_window = Qnil;
9760 bufp->arg = Qnil;
9761 bufp++;
9762
9763 count += 1;
9764 numchars -= 1;
9765 }
9766 break;
9767
9768 case SelectionRequest: /* Someone wants our selection. */
9769 #ifdef USE_X_TOOLKIT
9770 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9771 goto OTHER;
9772 #endif /* USE_X_TOOLKIT */
9773 if (x_queue_selection_requests)
9774 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9775 &event);
9776 else
9777 {
9778 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9779
9780 if (numchars == 0)
9781 abort ();
9782
9783 bufp->kind = selection_request_event;
9784 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9785 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9786 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9787 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9788 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9789 SELECTION_EVENT_TIME (bufp) = eventp->time;
9790 bufp->frame_or_window = Qnil;
9791 bufp->arg = Qnil;
9792 bufp++;
9793
9794 count += 1;
9795 numchars -= 1;
9796 }
9797 break;
9798
9799 case PropertyNotify:
9800 #ifdef USE_X_TOOLKIT
9801 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9802 goto OTHER;
9803 #endif /* not USE_X_TOOLKIT */
9804 x_handle_property_notify (&event.xproperty);
9805 break;
9806
9807 case ReparentNotify:
9808 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9809 if (f)
9810 {
9811 int x, y;
9812 f->output_data.x->parent_desc = event.xreparent.parent;
9813 x_real_positions (f, &x, &y);
9814 f->output_data.x->left_pos = x;
9815 f->output_data.x->top_pos = y;
9816 }
9817 break;
9818
9819 case Expose:
9820 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9821 if (f)
9822 {
9823 if (f->async_visible == 0)
9824 {
9825 f->async_visible = 1;
9826 f->async_iconified = 0;
9827 f->output_data.x->has_been_visible = 1;
9828 SET_FRAME_GARBAGED (f);
9829 }
9830 else
9831 expose_frame (x_window_to_frame (dpyinfo,
9832 event.xexpose.window),
9833 event.xexpose.x, event.xexpose.y,
9834 event.xexpose.width, event.xexpose.height);
9835 }
9836 else
9837 {
9838 #ifdef USE_TOOLKIT_SCROLL_BARS
9839 /* Dispatch event to the widget. */
9840 goto OTHER;
9841 #else /* not USE_TOOLKIT_SCROLL_BARS */
9842 struct scroll_bar *bar
9843 = x_window_to_scroll_bar (event.xexpose.window);
9844
9845 if (bar)
9846 x_scroll_bar_expose (bar, &event);
9847 #ifdef USE_X_TOOLKIT
9848 else
9849 goto OTHER;
9850 #endif /* USE_X_TOOLKIT */
9851 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9852 }
9853 break;
9854
9855 case GraphicsExpose: /* This occurs when an XCopyArea's
9856 source area was obscured or not
9857 available.*/
9858 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9859 if (f)
9860 {
9861 expose_frame (f,
9862 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9863 event.xgraphicsexpose.width,
9864 event.xgraphicsexpose.height);
9865 }
9866 #ifdef USE_X_TOOLKIT
9867 else
9868 goto OTHER;
9869 #endif /* USE_X_TOOLKIT */
9870 break;
9871
9872 case NoExpose: /* This occurs when an XCopyArea's
9873 source area was completely
9874 available */
9875 break;
9876
9877 case UnmapNotify:
9878 /* Redo the mouse-highlight after the tooltip has gone. */
9879 if (event.xmap.window == tip_window)
9880 {
9881 tip_window = 0;
9882 redo_mouse_highlight ();
9883 }
9884
9885 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9886 if (f) /* F may no longer exist if
9887 the frame was deleted. */
9888 {
9889 /* While a frame is unmapped, display generation is
9890 disabled; you don't want to spend time updating a
9891 display that won't ever be seen. */
9892 f->async_visible = 0;
9893 /* We can't distinguish, from the event, whether the window
9894 has become iconified or invisible. So assume, if it
9895 was previously visible, than now it is iconified.
9896 But x_make_frame_invisible clears both
9897 the visible flag and the iconified flag;
9898 and that way, we know the window is not iconified now. */
9899 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9900 {
9901 f->async_iconified = 1;
9902
9903 bufp->kind = iconify_event;
9904 XSETFRAME (bufp->frame_or_window, f);
9905 bufp->arg = Qnil;
9906 bufp++;
9907 count++;
9908 numchars--;
9909 }
9910 }
9911 goto OTHER;
9912
9913 case MapNotify:
9914 if (event.xmap.window == tip_window)
9915 /* The tooltip has been drawn already. Avoid
9916 the SET_FRAME_GARBAGED below. */
9917 goto OTHER;
9918
9919 /* We use x_top_window_to_frame because map events can
9920 come for sub-windows and they don't mean that the
9921 frame is visible. */
9922 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9923 if (f)
9924 {
9925 f->async_visible = 1;
9926 f->async_iconified = 0;
9927 f->output_data.x->has_been_visible = 1;
9928
9929 /* wait_reading_process_input will notice this and update
9930 the frame's display structures. */
9931 SET_FRAME_GARBAGED (f);
9932
9933 if (f->iconified)
9934 {
9935 bufp->kind = deiconify_event;
9936 XSETFRAME (bufp->frame_or_window, f);
9937 bufp->arg = Qnil;
9938 bufp++;
9939 count++;
9940 numchars--;
9941 }
9942 else if (! NILP (Vframe_list)
9943 && ! NILP (XCDR (Vframe_list)))
9944 /* Force a redisplay sooner or later
9945 to update the frame titles
9946 in case this is the second frame. */
9947 record_asynch_buffer_change ();
9948 }
9949 goto OTHER;
9950
9951 case KeyPress:
9952 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9953
9954 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
9955 if (f == 0)
9956 {
9957 /* Scroll bars consume key events, but we want
9958 the keys to go to the scroll bar's frame. */
9959 Widget widget = XtWindowToWidget (dpyinfo->display,
9960 event.xkey.window);
9961 if (widget && XmIsScrollBar (widget))
9962 {
9963 widget = XtParent (widget);
9964 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9965 }
9966 }
9967 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
9968
9969 if (f != 0)
9970 {
9971 KeySym keysym, orig_keysym;
9972 /* al%imercury@uunet.uu.net says that making this 81
9973 instead of 80 fixed a bug whereby meta chars made
9974 his Emacs hang.
9975
9976 It seems that some version of XmbLookupString has
9977 a bug of not returning XBufferOverflow in
9978 status_return even if the input is too long to
9979 fit in 81 bytes. So, we must prepare sufficient
9980 bytes for copy_buffer. 513 bytes (256 chars for
9981 two-byte character set) seems to be a faily good
9982 approximation. -- 2000.8.10 handa@etl.go.jp */
9983 unsigned char copy_buffer[513];
9984 unsigned char *copy_bufptr = copy_buffer;
9985 int copy_bufsiz = sizeof (copy_buffer);
9986 int modifiers;
9987
9988 event.xkey.state
9989 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9990 extra_keyboard_modifiers);
9991 modifiers = event.xkey.state;
9992
9993 /* This will have to go some day... */
9994
9995 /* make_lispy_event turns chars into control chars.
9996 Don't do it here because XLookupString is too eager. */
9997 event.xkey.state &= ~ControlMask;
9998 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9999 | dpyinfo->super_mod_mask
10000 | dpyinfo->hyper_mod_mask
10001 | dpyinfo->alt_mod_mask);
10002
10003 /* In case Meta is ComposeCharacter,
10004 clear its status. According to Markus Ehrnsperger
10005 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10006 this enables ComposeCharacter to work whether or
10007 not it is combined with Meta. */
10008 if (modifiers & dpyinfo->meta_mod_mask)
10009 bzero (&compose_status, sizeof (compose_status));
10010
10011 #ifdef HAVE_X_I18N
10012 if (FRAME_XIC (f))
10013 {
10014 Status status_return;
10015
10016 nbytes = XmbLookupString (FRAME_XIC (f),
10017 &event.xkey, copy_bufptr,
10018 copy_bufsiz, &keysym,
10019 &status_return);
10020 if (status_return == XBufferOverflow)
10021 {
10022 copy_bufsiz = nbytes + 1;
10023 copy_bufptr = (char *) alloca (copy_bufsiz);
10024 nbytes = XmbLookupString (FRAME_XIC (f),
10025 &event.xkey, copy_bufptr,
10026 copy_bufsiz, &keysym,
10027 &status_return);
10028 }
10029
10030 if (status_return == XLookupNone)
10031 break;
10032 else if (status_return == XLookupChars)
10033 {
10034 keysym = NoSymbol;
10035 modifiers = 0;
10036 }
10037 else if (status_return != XLookupKeySym
10038 && status_return != XLookupBoth)
10039 abort ();
10040 }
10041 else
10042 nbytes = XLookupString (&event.xkey, copy_bufptr,
10043 copy_bufsiz, &keysym,
10044 &compose_status);
10045 #else
10046 nbytes = XLookupString (&event.xkey, copy_bufptr,
10047 copy_bufsiz, &keysym,
10048 &compose_status);
10049 #endif
10050
10051 orig_keysym = keysym;
10052
10053 if (numchars > 1)
10054 {
10055 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10056 || keysym == XK_Delete
10057 #ifdef XK_ISO_Left_Tab
10058 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10059 #endif
10060 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10061 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10062 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10063 #ifdef HPUX
10064 /* This recognizes the "extended function keys".
10065 It seems there's no cleaner way.
10066 Test IsModifierKey to avoid handling mode_switch
10067 incorrectly. */
10068 || ((unsigned) (keysym) >= XK_Select
10069 && (unsigned)(keysym) < XK_KP_Space)
10070 #endif
10071 #ifdef XK_dead_circumflex
10072 || orig_keysym == XK_dead_circumflex
10073 #endif
10074 #ifdef XK_dead_grave
10075 || orig_keysym == XK_dead_grave
10076 #endif
10077 #ifdef XK_dead_tilde
10078 || orig_keysym == XK_dead_tilde
10079 #endif
10080 #ifdef XK_dead_diaeresis
10081 || orig_keysym == XK_dead_diaeresis
10082 #endif
10083 #ifdef XK_dead_macron
10084 || orig_keysym == XK_dead_macron
10085 #endif
10086 #ifdef XK_dead_degree
10087 || orig_keysym == XK_dead_degree
10088 #endif
10089 #ifdef XK_dead_acute
10090 || orig_keysym == XK_dead_acute
10091 #endif
10092 #ifdef XK_dead_cedilla
10093 || orig_keysym == XK_dead_cedilla
10094 #endif
10095 #ifdef XK_dead_breve
10096 || orig_keysym == XK_dead_breve
10097 #endif
10098 #ifdef XK_dead_ogonek
10099 || orig_keysym == XK_dead_ogonek
10100 #endif
10101 #ifdef XK_dead_caron
10102 || orig_keysym == XK_dead_caron
10103 #endif
10104 #ifdef XK_dead_doubleacute
10105 || orig_keysym == XK_dead_doubleacute
10106 #endif
10107 #ifdef XK_dead_abovedot
10108 || orig_keysym == XK_dead_abovedot
10109 #endif
10110 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10111 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10112 /* Any "vendor-specific" key is ok. */
10113 || (orig_keysym & (1 << 28)))
10114 && ! (IsModifierKey (orig_keysym)
10115 #ifndef HAVE_X11R5
10116 #ifdef XK_Mode_switch
10117 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10118 #endif
10119 #ifdef XK_Num_Lock
10120 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10121 #endif
10122 #endif /* not HAVE_X11R5 */
10123 ))
10124 {
10125 if (temp_index == sizeof temp_buffer / sizeof (short))
10126 temp_index = 0;
10127 temp_buffer[temp_index++] = keysym;
10128 bufp->kind = non_ascii_keystroke;
10129 bufp->code = keysym;
10130 XSETFRAME (bufp->frame_or_window, f);
10131 bufp->arg = Qnil;
10132 bufp->modifiers
10133 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10134 modifiers);
10135 bufp->timestamp = event.xkey.time;
10136 bufp++;
10137 count++;
10138 numchars--;
10139 }
10140 else if (numchars > nbytes)
10141 {
10142 register int i;
10143 register int c;
10144 int nchars, len;
10145
10146 for (i = 0; i < nbytes; i++)
10147 {
10148 if (temp_index == (sizeof temp_buffer
10149 / sizeof (short)))
10150 temp_index = 0;
10151 temp_buffer[temp_index++] = copy_bufptr[i];
10152 }
10153
10154 if (/* If the event is not from XIM, */
10155 event.xkey.keycode != 0
10156 /* or the current locale doesn't request
10157 decoding of the intup data, ... */
10158 || coding.type == coding_type_raw_text
10159 || coding.type == coding_type_no_conversion)
10160 {
10161 /* ... we can use the input data as is. */
10162 nchars = nbytes;
10163 }
10164 else
10165 {
10166 /* We have to decode the input data. */
10167 int require;
10168 unsigned char *p;
10169
10170 require = decoding_buffer_size (&coding, nbytes);
10171 p = (unsigned char *) alloca (require);
10172 coding.mode |= CODING_MODE_LAST_BLOCK;
10173 decode_coding (&coding, copy_bufptr, p,
10174 nbytes, require);
10175 nbytes = coding.produced;
10176 nchars = coding.produced_char;
10177 copy_bufptr = p;
10178 }
10179
10180 /* Convert the input data to a sequence of
10181 character events. */
10182 for (i = 0; i < nbytes; i += len)
10183 {
10184 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10185 nbytes - i, len);
10186 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10187 ? ascii_keystroke
10188 : multibyte_char_keystroke);
10189 bufp->code = c;
10190 XSETFRAME (bufp->frame_or_window, f);
10191 bufp->arg = Qnil;
10192 bufp->modifiers
10193 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10194 modifiers);
10195 bufp->timestamp = event.xkey.time;
10196 bufp++;
10197 }
10198
10199 count += nchars;
10200 numchars -= nchars;
10201
10202 if (keysym == NoSymbol)
10203 break;
10204 }
10205 else
10206 abort ();
10207 }
10208 else
10209 abort ();
10210 }
10211 #ifdef HAVE_X_I18N
10212 /* Don't dispatch this event since XtDispatchEvent calls
10213 XFilterEvent, and two calls in a row may freeze the
10214 client. */
10215 break;
10216 #else
10217 goto OTHER;
10218 #endif
10219
10220 case KeyRelease:
10221 #ifdef HAVE_X_I18N
10222 /* Don't dispatch this event since XtDispatchEvent calls
10223 XFilterEvent, and two calls in a row may freeze the
10224 client. */
10225 break;
10226 #else
10227 goto OTHER;
10228 #endif
10229
10230 /* Here's a possible interpretation of the whole
10231 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10232 you get a FocusIn event, you have to get a FocusOut
10233 event before you relinquish the focus. If you
10234 haven't received a FocusIn event, then a mere
10235 LeaveNotify is enough to free you. */
10236
10237 case EnterNotify:
10238 {
10239 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10240
10241 if (event.xcrossing.focus)
10242 {
10243 /* Avoid nasty pop/raise loops. */
10244 if (f && (!(f->auto_raise)
10245 || !(f->auto_lower)
10246 || (event.xcrossing.time - enter_timestamp) > 500))
10247 {
10248 x_new_focus_frame (dpyinfo, f);
10249 enter_timestamp = event.xcrossing.time;
10250 }
10251 }
10252 else if (f == dpyinfo->x_focus_frame)
10253 x_new_focus_frame (dpyinfo, 0);
10254
10255 /* EnterNotify counts as mouse movement,
10256 so update things that depend on mouse position. */
10257 if (f && !f->output_data.x->hourglass_p)
10258 note_mouse_movement (f, &event.xmotion);
10259 goto OTHER;
10260 }
10261
10262 case FocusIn:
10263 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10264 if (event.xfocus.detail != NotifyPointer)
10265 dpyinfo->x_focus_event_frame = f;
10266 if (f)
10267 {
10268 x_new_focus_frame (dpyinfo, f);
10269
10270 /* Don't stop displaying the initial startup message
10271 for a switch-frame event we don't need. */
10272 if (GC_NILP (Vterminal_frame)
10273 && GC_CONSP (Vframe_list)
10274 && !GC_NILP (XCDR (Vframe_list)))
10275 {
10276 bufp->kind = FOCUS_IN_EVENT;
10277 XSETFRAME (bufp->frame_or_window, f);
10278 bufp->arg = Qnil;
10279 ++bufp, ++count, --numchars;
10280 }
10281 }
10282
10283 #ifdef HAVE_X_I18N
10284 if (f && FRAME_XIC (f))
10285 XSetICFocus (FRAME_XIC (f));
10286 #endif
10287
10288 goto OTHER;
10289
10290 case LeaveNotify:
10291 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10292 if (f)
10293 {
10294 if (f == dpyinfo->mouse_face_mouse_frame)
10295 {
10296 /* If we move outside the frame, then we're
10297 certainly no longer on any text in the frame. */
10298 clear_mouse_face (dpyinfo);
10299 dpyinfo->mouse_face_mouse_frame = 0;
10300 }
10301
10302 /* Generate a nil HELP_EVENT to cancel a help-echo.
10303 Do it only if there's something to cancel.
10304 Otherwise, the startup message is cleared when
10305 the mouse leaves the frame. */
10306 if (any_help_event_p)
10307 {
10308 Lisp_Object frame;
10309 int n;
10310
10311 XSETFRAME (frame, f);
10312 help_echo = Qnil;
10313 n = gen_help_event (bufp, numchars,
10314 Qnil, frame, Qnil, Qnil, 0);
10315 bufp += n, count += n, numchars -= n;
10316 }
10317
10318 if (event.xcrossing.focus)
10319 x_mouse_leave (dpyinfo);
10320 else
10321 {
10322 if (f == dpyinfo->x_focus_event_frame)
10323 dpyinfo->x_focus_event_frame = 0;
10324 if (f == dpyinfo->x_focus_frame)
10325 x_new_focus_frame (dpyinfo, 0);
10326 }
10327 }
10328 goto OTHER;
10329
10330 case FocusOut:
10331 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10332 if (event.xfocus.detail != NotifyPointer
10333 && f == dpyinfo->x_focus_event_frame)
10334 dpyinfo->x_focus_event_frame = 0;
10335 if (f && f == dpyinfo->x_focus_frame)
10336 x_new_focus_frame (dpyinfo, 0);
10337
10338 #ifdef HAVE_X_I18N
10339 if (f && FRAME_XIC (f))
10340 XUnsetICFocus (FRAME_XIC (f));
10341 #endif
10342
10343 goto OTHER;
10344
10345 case MotionNotify:
10346 {
10347 previous_help_echo = help_echo;
10348 help_echo = help_echo_object = help_echo_window = Qnil;
10349 help_echo_pos = -1;
10350
10351 if (dpyinfo->grabbed && last_mouse_frame
10352 && FRAME_LIVE_P (last_mouse_frame))
10353 f = last_mouse_frame;
10354 else
10355 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10356
10357 if (f)
10358 note_mouse_movement (f, &event.xmotion);
10359 else
10360 {
10361 #ifndef USE_TOOLKIT_SCROLL_BARS
10362 struct scroll_bar *bar
10363 = x_window_to_scroll_bar (event.xmotion.window);
10364
10365 if (bar)
10366 x_scroll_bar_note_movement (bar, &event);
10367 #endif /* USE_TOOLKIT_SCROLL_BARS */
10368
10369 /* If we move outside the frame, then we're
10370 certainly no longer on any text in the frame. */
10371 clear_mouse_face (dpyinfo);
10372 }
10373
10374 /* If the contents of the global variable help_echo
10375 has changed, generate a HELP_EVENT. */
10376 if (!NILP (help_echo)
10377 || !NILP (previous_help_echo))
10378 {
10379 Lisp_Object frame;
10380 int n;
10381
10382 if (f)
10383 XSETFRAME (frame, f);
10384 else
10385 frame = Qnil;
10386
10387 any_help_event_p = 1;
10388 n = gen_help_event (bufp, numchars, help_echo, frame,
10389 help_echo_window, help_echo_object,
10390 help_echo_pos);
10391 bufp += n, count += n, numchars -= n;
10392 }
10393
10394 goto OTHER;
10395 }
10396
10397 case ConfigureNotify:
10398 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10399 if (f)
10400 {
10401 #ifndef USE_X_TOOLKIT
10402 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10403 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10404
10405 /* In the toolkit version, change_frame_size
10406 is called by the code that handles resizing
10407 of the EmacsFrame widget. */
10408
10409 /* Even if the number of character rows and columns has
10410 not changed, the font size may have changed, so we need
10411 to check the pixel dimensions as well. */
10412 if (columns != f->width
10413 || rows != f->height
10414 || event.xconfigure.width != f->output_data.x->pixel_width
10415 || event.xconfigure.height != f->output_data.x->pixel_height)
10416 {
10417 change_frame_size (f, rows, columns, 0, 1, 0);
10418 SET_FRAME_GARBAGED (f);
10419 cancel_mouse_face (f);
10420 }
10421 #endif
10422
10423 f->output_data.x->pixel_width = event.xconfigure.width;
10424 f->output_data.x->pixel_height = event.xconfigure.height;
10425
10426 /* What we have now is the position of Emacs's own window.
10427 Convert that to the position of the window manager window. */
10428 x_real_positions (f, &f->output_data.x->left_pos,
10429 &f->output_data.x->top_pos);
10430
10431 #ifdef HAVE_X_I18N
10432 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10433 xic_set_statusarea (f);
10434 #endif
10435
10436 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10437 {
10438 /* Since the WM decorations come below top_pos now,
10439 we must put them below top_pos in the future. */
10440 f->output_data.x->win_gravity = NorthWestGravity;
10441 x_wm_set_size_hint (f, (long) 0, 0);
10442 }
10443 #ifdef USE_MOTIF
10444 /* Some window managers pass (0,0) as the location of
10445 the window, and the Motif event handler stores it
10446 in the emacs widget, which messes up Motif menus. */
10447 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10448 {
10449 event.xconfigure.x = f->output_data.x->widget->core.x;
10450 event.xconfigure.y = f->output_data.x->widget->core.y;
10451 }
10452 #endif /* USE_MOTIF */
10453 }
10454 goto OTHER;
10455
10456 case ButtonPress:
10457 case ButtonRelease:
10458 {
10459 /* If we decide we want to generate an event to be seen
10460 by the rest of Emacs, we put it here. */
10461 struct input_event emacs_event;
10462 int tool_bar_p = 0;
10463
10464 emacs_event.kind = no_event;
10465 bzero (&compose_status, sizeof (compose_status));
10466
10467 if (dpyinfo->grabbed
10468 && last_mouse_frame
10469 && FRAME_LIVE_P (last_mouse_frame))
10470 f = last_mouse_frame;
10471 else
10472 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10473
10474 if (f)
10475 {
10476 /* Is this in the tool-bar? */
10477 if (WINDOWP (f->tool_bar_window)
10478 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10479 {
10480 Lisp_Object window;
10481 int p, x, y;
10482
10483 x = event.xbutton.x;
10484 y = event.xbutton.y;
10485
10486 /* Set x and y. */
10487 window = window_from_coordinates (f, x, y, &p, 1);
10488 if (EQ (window, f->tool_bar_window))
10489 {
10490 x_handle_tool_bar_click (f, &event.xbutton);
10491 tool_bar_p = 1;
10492 }
10493 }
10494
10495 if (!tool_bar_p)
10496 if (!dpyinfo->x_focus_frame
10497 || f == dpyinfo->x_focus_frame)
10498 construct_mouse_click (&emacs_event, &event, f);
10499 }
10500 else
10501 {
10502 #ifndef USE_TOOLKIT_SCROLL_BARS
10503 struct scroll_bar *bar
10504 = x_window_to_scroll_bar (event.xbutton.window);
10505
10506 if (bar)
10507 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10508 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10509 }
10510
10511 if (event.type == ButtonPress)
10512 {
10513 dpyinfo->grabbed |= (1 << event.xbutton.button);
10514 last_mouse_frame = f;
10515 /* Ignore any mouse motion that happened
10516 before this event; any subsequent mouse-movement
10517 Emacs events should reflect only motion after
10518 the ButtonPress. */
10519 if (f != 0)
10520 f->mouse_moved = 0;
10521
10522 if (!tool_bar_p)
10523 last_tool_bar_item = -1;
10524 }
10525 else
10526 {
10527 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10528 }
10529
10530 if (numchars >= 1 && emacs_event.kind != no_event)
10531 {
10532 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10533 bufp++;
10534 count++;
10535 numchars--;
10536 }
10537
10538 #ifdef USE_X_TOOLKIT
10539 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10540 /* For a down-event in the menu bar,
10541 don't pass it to Xt right now.
10542 Instead, save it away
10543 and we will pass it to Xt from kbd_buffer_get_event.
10544 That way, we can run some Lisp code first. */
10545 if (f && event.type == ButtonPress
10546 /* Verify the event is really within the menu bar
10547 and not just sent to it due to grabbing. */
10548 && event.xbutton.x >= 0
10549 && event.xbutton.x < f->output_data.x->pixel_width
10550 && event.xbutton.y >= 0
10551 && event.xbutton.y < f->output_data.x->menubar_height
10552 && event.xbutton.same_screen)
10553 {
10554 SET_SAVED_BUTTON_EVENT;
10555 XSETFRAME (last_mouse_press_frame, f);
10556 }
10557 else if (event.type == ButtonPress)
10558 {
10559 last_mouse_press_frame = Qnil;
10560 goto OTHER;
10561 }
10562
10563 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10564 but I am trying to be cautious. */
10565 else if (event.type == ButtonRelease)
10566 {
10567 if (!NILP (last_mouse_press_frame))
10568 {
10569 f = XFRAME (last_mouse_press_frame);
10570 if (f->output_data.x)
10571 SET_SAVED_BUTTON_EVENT;
10572 }
10573 else
10574 goto OTHER;
10575 }
10576 #endif /* USE_MOTIF */
10577 else
10578 goto OTHER;
10579 #endif /* USE_X_TOOLKIT */
10580 }
10581 break;
10582
10583 case CirculateNotify:
10584 goto OTHER;
10585
10586 case CirculateRequest:
10587 goto OTHER;
10588
10589 case VisibilityNotify:
10590 goto OTHER;
10591
10592 case MappingNotify:
10593 /* Someone has changed the keyboard mapping - update the
10594 local cache. */
10595 switch (event.xmapping.request)
10596 {
10597 case MappingModifier:
10598 x_find_modifier_meanings (dpyinfo);
10599 /* This is meant to fall through. */
10600 case MappingKeyboard:
10601 XRefreshKeyboardMapping (&event.xmapping);
10602 }
10603 goto OTHER;
10604
10605 default:
10606 OTHER:
10607 #ifdef USE_X_TOOLKIT
10608 BLOCK_INPUT;
10609 XtDispatchEvent (&event);
10610 UNBLOCK_INPUT;
10611 #endif /* USE_X_TOOLKIT */
10612 break;
10613 }
10614 }
10615 }
10616
10617 out:;
10618
10619 /* On some systems, an X bug causes Emacs to get no more events
10620 when the window is destroyed. Detect that. (1994.) */
10621 if (! event_found)
10622 {
10623 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10624 One XNOOP in 100 loops will make Emacs terminate.
10625 B. Bretthauer, 1994 */
10626 x_noop_count++;
10627 if (x_noop_count >= 100)
10628 {
10629 x_noop_count=0;
10630
10631 if (next_noop_dpyinfo == 0)
10632 next_noop_dpyinfo = x_display_list;
10633
10634 XNoOp (next_noop_dpyinfo->display);
10635
10636 /* Each time we get here, cycle through the displays now open. */
10637 next_noop_dpyinfo = next_noop_dpyinfo->next;
10638 }
10639 }
10640
10641 /* If the focus was just given to an auto-raising frame,
10642 raise it now. */
10643 /* ??? This ought to be able to handle more than one such frame. */
10644 if (pending_autoraise_frame)
10645 {
10646 x_raise_frame (pending_autoraise_frame);
10647 pending_autoraise_frame = 0;
10648 }
10649
10650 UNBLOCK_INPUT;
10651 --handling_signal;
10652 return count;
10653 }
10654
10655
10656
10657 \f
10658 /***********************************************************************
10659 Text Cursor
10660 ***********************************************************************/
10661
10662 /* Note if the text cursor of window W has been overwritten by a
10663 drawing operation that outputs N glyphs starting at HPOS in the
10664 line given by output_cursor.vpos. N < 0 means all the rest of the
10665 line after HPOS has been written. */
10666
10667 static void
10668 note_overwritten_text_cursor (w, hpos, n)
10669 struct window *w;
10670 int hpos, n;
10671 {
10672 if (updated_area == TEXT_AREA
10673 && output_cursor.vpos == w->phys_cursor.vpos
10674 && hpos <= w->phys_cursor.hpos
10675 && (n < 0
10676 || hpos + n > w->phys_cursor.hpos))
10677 w->phys_cursor_on_p = 0;
10678 }
10679
10680
10681 /* Set clipping for output in glyph row ROW. W is the window in which
10682 we operate. GC is the graphics context to set clipping in.
10683 WHOLE_LINE_P non-zero means include the areas used for truncation
10684 mark display and alike in the clipping rectangle.
10685
10686 ROW may be a text row or, e.g., a mode line. Text rows must be
10687 clipped to the interior of the window dedicated to text display,
10688 mode lines must be clipped to the whole window. */
10689
10690 static void
10691 x_clip_to_row (w, row, gc, whole_line_p)
10692 struct window *w;
10693 struct glyph_row *row;
10694 GC gc;
10695 int whole_line_p;
10696 {
10697 struct frame *f = XFRAME (WINDOW_FRAME (w));
10698 XRectangle clip_rect;
10699 int window_x, window_y, window_width, window_height;
10700
10701 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10702
10703 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10704 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10705 clip_rect.y = max (clip_rect.y, window_y);
10706 clip_rect.width = window_width;
10707 clip_rect.height = row->visible_height;
10708
10709 /* If clipping to the whole line, including trunc marks, extend
10710 the rectangle to the left and increase its width. */
10711 if (whole_line_p)
10712 {
10713 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10714 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10715 }
10716
10717 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10718 }
10719
10720
10721 /* Draw a hollow box cursor on window W in glyph row ROW. */
10722
10723 static void
10724 x_draw_hollow_cursor (w, row)
10725 struct window *w;
10726 struct glyph_row *row;
10727 {
10728 struct frame *f = XFRAME (WINDOW_FRAME (w));
10729 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10730 Display *dpy = FRAME_X_DISPLAY (f);
10731 int x, y, wd, h;
10732 XGCValues xgcv;
10733 struct glyph *cursor_glyph;
10734 GC gc;
10735
10736 /* Compute frame-relative coordinates from window-relative
10737 coordinates. */
10738 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10739 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10740 + row->ascent - w->phys_cursor_ascent);
10741 h = row->height - 1;
10742
10743 /* Get the glyph the cursor is on. If we can't tell because
10744 the current matrix is invalid or such, give up. */
10745 cursor_glyph = get_phys_cursor_glyph (w);
10746 if (cursor_glyph == NULL)
10747 return;
10748
10749 /* Compute the width of the rectangle to draw. If on a stretch
10750 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10751 rectangle as wide as the glyph, but use a canonical character
10752 width instead. */
10753 wd = cursor_glyph->pixel_width - 1;
10754 if (cursor_glyph->type == STRETCH_GLYPH
10755 && !x_stretch_cursor_p)
10756 wd = min (CANON_X_UNIT (f), wd);
10757
10758 /* The foreground of cursor_gc is typically the same as the normal
10759 background color, which can cause the cursor box to be invisible. */
10760 xgcv.foreground = f->output_data.x->cursor_pixel;
10761 if (dpyinfo->scratch_cursor_gc)
10762 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10763 else
10764 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10765 GCForeground, &xgcv);
10766 gc = dpyinfo->scratch_cursor_gc;
10767
10768 /* Set clipping, draw the rectangle, and reset clipping again. */
10769 x_clip_to_row (w, row, gc, 0);
10770 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10771 XSetClipMask (dpy, gc, None);
10772 }
10773
10774
10775 /* Draw a bar cursor on window W in glyph row ROW.
10776
10777 Implementation note: One would like to draw a bar cursor with an
10778 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10779 Unfortunately, I didn't find a font yet that has this property set.
10780 --gerd. */
10781
10782 static void
10783 x_draw_bar_cursor (w, row, width)
10784 struct window *w;
10785 struct glyph_row *row;
10786 int width;
10787 {
10788 struct frame *f = XFRAME (w->frame);
10789 struct glyph *cursor_glyph;
10790 GC gc;
10791 int x;
10792 unsigned long mask;
10793 XGCValues xgcv;
10794 Display *dpy;
10795 Window window;
10796
10797 /* If cursor is out of bounds, don't draw garbage. This can happen
10798 in mini-buffer windows when switching between echo area glyphs
10799 and mini-buffer. */
10800 cursor_glyph = get_phys_cursor_glyph (w);
10801 if (cursor_glyph == NULL)
10802 return;
10803
10804 /* If on an image, draw like a normal cursor. That's usually better
10805 visible than drawing a bar, esp. if the image is large so that
10806 the bar might not be in the window. */
10807 if (cursor_glyph->type == IMAGE_GLYPH)
10808 {
10809 struct glyph_row *row;
10810 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10811 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10812 }
10813 else
10814 {
10815 xgcv.background = f->output_data.x->cursor_pixel;
10816 xgcv.foreground = f->output_data.x->cursor_pixel;
10817 xgcv.graphics_exposures = 0;
10818 mask = GCForeground | GCBackground | GCGraphicsExposures;
10819 dpy = FRAME_X_DISPLAY (f);
10820 window = FRAME_X_WINDOW (f);
10821 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10822
10823 if (gc)
10824 XChangeGC (dpy, gc, mask, &xgcv);
10825 else
10826 {
10827 gc = XCreateGC (dpy, window, mask, &xgcv);
10828 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10829 }
10830
10831 if (width < 0)
10832 width = f->output_data.x->cursor_width;
10833
10834 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10835 x_clip_to_row (w, row, gc, 0);
10836 XFillRectangle (dpy, window, gc,
10837 x,
10838 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10839 min (cursor_glyph->pixel_width, width),
10840 row->height);
10841 XSetClipMask (dpy, gc, None);
10842 }
10843 }
10844
10845
10846 /* Clear the cursor of window W to background color, and mark the
10847 cursor as not shown. This is used when the text where the cursor
10848 is is about to be rewritten. */
10849
10850 static void
10851 x_clear_cursor (w)
10852 struct window *w;
10853 {
10854 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10855 x_update_window_cursor (w, 0);
10856 }
10857
10858
10859 /* Draw the cursor glyph of window W in glyph row ROW. See the
10860 comment of x_draw_glyphs for the meaning of HL. */
10861
10862 static void
10863 x_draw_phys_cursor_glyph (w, row, hl)
10864 struct window *w;
10865 struct glyph_row *row;
10866 enum draw_glyphs_face hl;
10867 {
10868 /* If cursor hpos is out of bounds, don't draw garbage. This can
10869 happen in mini-buffer windows when switching between echo area
10870 glyphs and mini-buffer. */
10871 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10872 {
10873 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10874 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10875 hl, 0, 0, 0);
10876
10877 /* When we erase the cursor, and ROW is overlapped by other
10878 rows, make sure that these overlapping parts of other rows
10879 are redrawn. */
10880 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10881 {
10882 if (row > w->current_matrix->rows
10883 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10884 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10885
10886 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10887 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10888 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10889 }
10890 }
10891 }
10892
10893
10894 /* Erase the image of a cursor of window W from the screen. */
10895
10896 static void
10897 x_erase_phys_cursor (w)
10898 struct window *w;
10899 {
10900 struct frame *f = XFRAME (w->frame);
10901 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10902 int hpos = w->phys_cursor.hpos;
10903 int vpos = w->phys_cursor.vpos;
10904 int mouse_face_here_p = 0;
10905 struct glyph_matrix *active_glyphs = w->current_matrix;
10906 struct glyph_row *cursor_row;
10907 struct glyph *cursor_glyph;
10908 enum draw_glyphs_face hl;
10909
10910 /* No cursor displayed or row invalidated => nothing to do on the
10911 screen. */
10912 if (w->phys_cursor_type == NO_CURSOR)
10913 goto mark_cursor_off;
10914
10915 /* VPOS >= active_glyphs->nrows means that window has been resized.
10916 Don't bother to erase the cursor. */
10917 if (vpos >= active_glyphs->nrows)
10918 goto mark_cursor_off;
10919
10920 /* If row containing cursor is marked invalid, there is nothing we
10921 can do. */
10922 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10923 if (!cursor_row->enabled_p)
10924 goto mark_cursor_off;
10925
10926 /* This can happen when the new row is shorter than the old one.
10927 In this case, either x_draw_glyphs or clear_end_of_line
10928 should have cleared the cursor. Note that we wouldn't be
10929 able to erase the cursor in this case because we don't have a
10930 cursor glyph at hand. */
10931 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10932 goto mark_cursor_off;
10933
10934 /* If the cursor is in the mouse face area, redisplay that when
10935 we clear the cursor. */
10936 if (! NILP (dpyinfo->mouse_face_window)
10937 && w == XWINDOW (dpyinfo->mouse_face_window)
10938 && (vpos > dpyinfo->mouse_face_beg_row
10939 || (vpos == dpyinfo->mouse_face_beg_row
10940 && hpos >= dpyinfo->mouse_face_beg_col))
10941 && (vpos < dpyinfo->mouse_face_end_row
10942 || (vpos == dpyinfo->mouse_face_end_row
10943 && hpos < dpyinfo->mouse_face_end_col))
10944 /* Don't redraw the cursor's spot in mouse face if it is at the
10945 end of a line (on a newline). The cursor appears there, but
10946 mouse highlighting does not. */
10947 && cursor_row->used[TEXT_AREA] > hpos)
10948 mouse_face_here_p = 1;
10949
10950 /* Maybe clear the display under the cursor. */
10951 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10952 {
10953 int x;
10954 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10955
10956 cursor_glyph = get_phys_cursor_glyph (w);
10957 if (cursor_glyph == NULL)
10958 goto mark_cursor_off;
10959
10960 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10961
10962 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10963 x,
10964 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10965 cursor_row->y)),
10966 cursor_glyph->pixel_width,
10967 cursor_row->visible_height,
10968 False);
10969 }
10970
10971 /* Erase the cursor by redrawing the character underneath it. */
10972 if (mouse_face_here_p)
10973 hl = DRAW_MOUSE_FACE;
10974 else if (cursor_row->inverse_p)
10975 hl = DRAW_INVERSE_VIDEO;
10976 else
10977 hl = DRAW_NORMAL_TEXT;
10978 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10979
10980 mark_cursor_off:
10981 w->phys_cursor_on_p = 0;
10982 w->phys_cursor_type = NO_CURSOR;
10983 }
10984
10985
10986 /* Display or clear cursor of window W. If ON is zero, clear the
10987 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10988 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10989
10990 void
10991 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10992 struct window *w;
10993 int on, hpos, vpos, x, y;
10994 {
10995 struct frame *f = XFRAME (w->frame);
10996 int new_cursor_type;
10997 int new_cursor_width;
10998 struct glyph_matrix *current_glyphs;
10999 struct glyph_row *glyph_row;
11000 struct glyph *glyph;
11001
11002 /* This is pointless on invisible frames, and dangerous on garbaged
11003 windows and frames; in the latter case, the frame or window may
11004 be in the midst of changing its size, and x and y may be off the
11005 window. */
11006 if (! FRAME_VISIBLE_P (f)
11007 || FRAME_GARBAGED_P (f)
11008 || vpos >= w->current_matrix->nrows
11009 || hpos >= w->current_matrix->matrix_w)
11010 return;
11011
11012 /* If cursor is off and we want it off, return quickly. */
11013 if (!on && !w->phys_cursor_on_p)
11014 return;
11015
11016 current_glyphs = w->current_matrix;
11017 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11018 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11019
11020 /* If cursor row is not enabled, we don't really know where to
11021 display the cursor. */
11022 if (!glyph_row->enabled_p)
11023 {
11024 w->phys_cursor_on_p = 0;
11025 return;
11026 }
11027
11028 xassert (interrupt_input_blocked);
11029
11030 /* Set new_cursor_type to the cursor we want to be displayed. In a
11031 mini-buffer window, we want the cursor only to appear if we are
11032 reading input from this window. For the selected window, we want
11033 the cursor type given by the frame parameter. If explicitly
11034 marked off, draw no cursor. In all other cases, we want a hollow
11035 box cursor. */
11036 new_cursor_width = -1;
11037 if (cursor_in_echo_area
11038 && FRAME_HAS_MINIBUF_P (f)
11039 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11040 {
11041 if (w == XWINDOW (echo_area_window))
11042 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11043 else
11044 new_cursor_type = HOLLOW_BOX_CURSOR;
11045 }
11046 else
11047 {
11048 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11049 || w != XWINDOW (f->selected_window))
11050 {
11051 extern int cursor_in_non_selected_windows;
11052
11053 if (MINI_WINDOW_P (w)
11054 || !cursor_in_non_selected_windows
11055 || NILP (XBUFFER (w->buffer)->cursor_type))
11056 new_cursor_type = NO_CURSOR;
11057 else
11058 new_cursor_type = HOLLOW_BOX_CURSOR;
11059 }
11060 else if (w->cursor_off_p)
11061 new_cursor_type = NO_CURSOR;
11062 else
11063 {
11064 struct buffer *b = XBUFFER (w->buffer);
11065
11066 if (EQ (b->cursor_type, Qt))
11067 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11068 else
11069 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11070 &new_cursor_width);
11071 }
11072 }
11073
11074 /* If cursor is currently being shown and we don't want it to be or
11075 it is in the wrong place, or the cursor type is not what we want,
11076 erase it. */
11077 if (w->phys_cursor_on_p
11078 && (!on
11079 || w->phys_cursor.x != x
11080 || w->phys_cursor.y != y
11081 || new_cursor_type != w->phys_cursor_type))
11082 x_erase_phys_cursor (w);
11083
11084 /* If the cursor is now invisible and we want it to be visible,
11085 display it. */
11086 if (on && !w->phys_cursor_on_p)
11087 {
11088 w->phys_cursor_ascent = glyph_row->ascent;
11089 w->phys_cursor_height = glyph_row->height;
11090
11091 /* Set phys_cursor_.* before x_draw_.* is called because some
11092 of them may need the information. */
11093 w->phys_cursor.x = x;
11094 w->phys_cursor.y = glyph_row->y;
11095 w->phys_cursor.hpos = hpos;
11096 w->phys_cursor.vpos = vpos;
11097 w->phys_cursor_type = new_cursor_type;
11098 w->phys_cursor_on_p = 1;
11099
11100 switch (new_cursor_type)
11101 {
11102 case HOLLOW_BOX_CURSOR:
11103 x_draw_hollow_cursor (w, glyph_row);
11104 break;
11105
11106 case FILLED_BOX_CURSOR:
11107 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11108 break;
11109
11110 case BAR_CURSOR:
11111 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11112 break;
11113
11114 case NO_CURSOR:
11115 break;
11116
11117 default:
11118 abort ();
11119 }
11120
11121 #ifdef HAVE_X_I18N
11122 if (w == XWINDOW (f->selected_window))
11123 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11124 xic_set_preeditarea (w, x, y);
11125 #endif
11126 }
11127
11128 #ifndef XFlush
11129 if (updating_frame != f)
11130 XFlush (FRAME_X_DISPLAY (f));
11131 #endif
11132 }
11133
11134
11135 /* Display the cursor on window W, or clear it. X and Y are window
11136 relative pixel coordinates. HPOS and VPOS are glyph matrix
11137 positions. If W is not the selected window, display a hollow
11138 cursor. ON non-zero means display the cursor at X, Y which
11139 correspond to HPOS, VPOS, otherwise it is cleared. */
11140
11141 void
11142 x_display_cursor (w, on, hpos, vpos, x, y)
11143 struct window *w;
11144 int on, hpos, vpos, x, y;
11145 {
11146 BLOCK_INPUT;
11147 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11148 UNBLOCK_INPUT;
11149 }
11150
11151
11152 /* Display the cursor on window W, or clear it, according to ON_P.
11153 Don't change the cursor's position. */
11154
11155 void
11156 x_update_cursor (f, on_p)
11157 struct frame *f;
11158 {
11159 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11160 }
11161
11162
11163 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11164 in the window tree rooted at W. */
11165
11166 static void
11167 x_update_cursor_in_window_tree (w, on_p)
11168 struct window *w;
11169 int on_p;
11170 {
11171 while (w)
11172 {
11173 if (!NILP (w->hchild))
11174 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11175 else if (!NILP (w->vchild))
11176 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11177 else
11178 x_update_window_cursor (w, on_p);
11179
11180 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11181 }
11182 }
11183
11184
11185 /* Switch the display of W's cursor on or off, according to the value
11186 of ON. */
11187
11188 static void
11189 x_update_window_cursor (w, on)
11190 struct window *w;
11191 int on;
11192 {
11193 /* Don't update cursor in windows whose frame is in the process
11194 of being deleted. */
11195 if (w->current_matrix)
11196 {
11197 BLOCK_INPUT;
11198 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11199 w->phys_cursor.x, w->phys_cursor.y);
11200 UNBLOCK_INPUT;
11201 }
11202 }
11203
11204
11205
11206 \f
11207 /* Icons. */
11208
11209 /* Refresh bitmap kitchen sink icon for frame F
11210 when we get an expose event for it. */
11211
11212 void
11213 refreshicon (f)
11214 struct frame *f;
11215 {
11216 /* Normally, the window manager handles this function. */
11217 }
11218
11219 /* Make the x-window of frame F use the gnu icon bitmap. */
11220
11221 int
11222 x_bitmap_icon (f, file)
11223 struct frame *f;
11224 Lisp_Object file;
11225 {
11226 int bitmap_id;
11227
11228 if (FRAME_X_WINDOW (f) == 0)
11229 return 1;
11230
11231 /* Free up our existing icon bitmap if any. */
11232 if (f->output_data.x->icon_bitmap > 0)
11233 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11234 f->output_data.x->icon_bitmap = 0;
11235
11236 if (STRINGP (file))
11237 bitmap_id = x_create_bitmap_from_file (f, file);
11238 else
11239 {
11240 /* Create the GNU bitmap if necessary. */
11241 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11242 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11243 = x_create_bitmap_from_data (f, gnu_bits,
11244 gnu_width, gnu_height);
11245
11246 /* The first time we create the GNU bitmap,
11247 this increments the ref-count one extra time.
11248 As a result, the GNU bitmap is never freed.
11249 That way, we don't have to worry about allocating it again. */
11250 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11251
11252 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11253 }
11254
11255 x_wm_set_icon_pixmap (f, bitmap_id);
11256 f->output_data.x->icon_bitmap = bitmap_id;
11257
11258 return 0;
11259 }
11260
11261
11262 /* Make the x-window of frame F use a rectangle with text.
11263 Use ICON_NAME as the text. */
11264
11265 int
11266 x_text_icon (f, icon_name)
11267 struct frame *f;
11268 char *icon_name;
11269 {
11270 if (FRAME_X_WINDOW (f) == 0)
11271 return 1;
11272
11273 #ifdef HAVE_X11R4
11274 {
11275 XTextProperty text;
11276 text.value = (unsigned char *) icon_name;
11277 text.encoding = XA_STRING;
11278 text.format = 8;
11279 text.nitems = strlen (icon_name);
11280 #ifdef USE_X_TOOLKIT
11281 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11282 &text);
11283 #else /* not USE_X_TOOLKIT */
11284 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11285 #endif /* not USE_X_TOOLKIT */
11286 }
11287 #else /* not HAVE_X11R4 */
11288 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11289 #endif /* not HAVE_X11R4 */
11290
11291 if (f->output_data.x->icon_bitmap > 0)
11292 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11293 f->output_data.x->icon_bitmap = 0;
11294 x_wm_set_icon_pixmap (f, 0);
11295
11296 return 0;
11297 }
11298 \f
11299 #define X_ERROR_MESSAGE_SIZE 200
11300
11301 /* If non-nil, this should be a string.
11302 It means catch X errors and store the error message in this string. */
11303
11304 static Lisp_Object x_error_message_string;
11305
11306 /* An X error handler which stores the error message in
11307 x_error_message_string. This is called from x_error_handler if
11308 x_catch_errors is in effect. */
11309
11310 static void
11311 x_error_catcher (display, error)
11312 Display *display;
11313 XErrorEvent *error;
11314 {
11315 XGetErrorText (display, error->error_code,
11316 XSTRING (x_error_message_string)->data,
11317 X_ERROR_MESSAGE_SIZE);
11318 }
11319
11320 /* Begin trapping X errors for display DPY. Actually we trap X errors
11321 for all displays, but DPY should be the display you are actually
11322 operating on.
11323
11324 After calling this function, X protocol errors no longer cause
11325 Emacs to exit; instead, they are recorded in the string
11326 stored in x_error_message_string.
11327
11328 Calling x_check_errors signals an Emacs error if an X error has
11329 occurred since the last call to x_catch_errors or x_check_errors.
11330
11331 Calling x_uncatch_errors resumes the normal error handling. */
11332
11333 void x_check_errors ();
11334 static Lisp_Object x_catch_errors_unwind ();
11335
11336 int
11337 x_catch_errors (dpy)
11338 Display *dpy;
11339 {
11340 int count = specpdl_ptr - specpdl;
11341
11342 /* Make sure any errors from previous requests have been dealt with. */
11343 XSync (dpy, False);
11344
11345 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11346
11347 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11348 XSTRING (x_error_message_string)->data[0] = 0;
11349
11350 return count;
11351 }
11352
11353 /* Unbind the binding that we made to check for X errors. */
11354
11355 static Lisp_Object
11356 x_catch_errors_unwind (old_val)
11357 Lisp_Object old_val;
11358 {
11359 x_error_message_string = old_val;
11360 return Qnil;
11361 }
11362
11363 /* If any X protocol errors have arrived since the last call to
11364 x_catch_errors or x_check_errors, signal an Emacs error using
11365 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11366
11367 void
11368 x_check_errors (dpy, format)
11369 Display *dpy;
11370 char *format;
11371 {
11372 /* Make sure to catch any errors incurred so far. */
11373 XSync (dpy, False);
11374
11375 if (XSTRING (x_error_message_string)->data[0])
11376 error (format, XSTRING (x_error_message_string)->data);
11377 }
11378
11379 /* Nonzero if we had any X protocol errors
11380 since we did x_catch_errors on DPY. */
11381
11382 int
11383 x_had_errors_p (dpy)
11384 Display *dpy;
11385 {
11386 /* Make sure to catch any errors incurred so far. */
11387 XSync (dpy, False);
11388
11389 return XSTRING (x_error_message_string)->data[0] != 0;
11390 }
11391
11392 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11393
11394 void
11395 x_clear_errors (dpy)
11396 Display *dpy;
11397 {
11398 XSTRING (x_error_message_string)->data[0] = 0;
11399 }
11400
11401 /* Stop catching X protocol errors and let them make Emacs die.
11402 DPY should be the display that was passed to x_catch_errors.
11403 COUNT should be the value that was returned by
11404 the corresponding call to x_catch_errors. */
11405
11406 void
11407 x_uncatch_errors (dpy, count)
11408 Display *dpy;
11409 int count;
11410 {
11411 unbind_to (count, Qnil);
11412 }
11413
11414 #if 0
11415 static unsigned int x_wire_count;
11416 x_trace_wire ()
11417 {
11418 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11419 }
11420 #endif /* ! 0 */
11421
11422 \f
11423 /* Handle SIGPIPE, which can happen when the connection to a server
11424 simply goes away. SIGPIPE is handled by x_connection_signal.
11425 Don't need to do anything, because the write which caused the
11426 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11427 which will do the appropriate cleanup for us. */
11428
11429 static SIGTYPE
11430 x_connection_signal (signalnum) /* If we don't have an argument, */
11431 int signalnum; /* some compilers complain in signal calls. */
11432 {
11433 #ifdef USG
11434 /* USG systems forget handlers when they are used;
11435 must reestablish each time */
11436 signal (signalnum, x_connection_signal);
11437 #endif /* USG */
11438 }
11439
11440 \f
11441 /************************************************************************
11442 Handling X errors
11443 ************************************************************************/
11444
11445 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11446 the text of an error message that lead to the connection loss. */
11447
11448 static SIGTYPE
11449 x_connection_closed (dpy, error_message)
11450 Display *dpy;
11451 char *error_message;
11452 {
11453 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11454 Lisp_Object frame, tail;
11455 int count;
11456 char *msg;
11457
11458 msg = (char *) alloca (strlen (error_message) + 1);
11459 strcpy (msg, error_message);
11460 handling_signal = 0;
11461
11462 /* Prevent being called recursively because of an error condition
11463 below. Otherwise, we might end up with printing ``can't find per
11464 display information'' in the recursive call instead of printing
11465 the original message here. */
11466 count = x_catch_errors (dpy);
11467
11468 /* We have to close the display to inform Xt that it doesn't
11469 exist anymore. If we don't, Xt will continue to wait for
11470 events from the display. As a consequence, a sequence of
11471
11472 M-x make-frame-on-display RET :1 RET
11473 ...kill the new frame, so that we get an IO error...
11474 M-x make-frame-on-display RET :1 RET
11475
11476 will indefinitely wait in Xt for events for display `:1', opened
11477 in the first class to make-frame-on-display.
11478
11479 Closing the display is reported to lead to a bus error on
11480 OpenWindows in certain situations. I suspect that is a bug
11481 in OpenWindows. I don't know how to cicumvent it here. */
11482
11483 #ifdef USE_X_TOOLKIT
11484 /* If DPYINFO is null, this means we didn't open the display
11485 in the first place, so don't try to close it. */
11486 if (dpyinfo)
11487 XtCloseDisplay (dpy);
11488 #endif
11489
11490 /* Indicate that this display is dead. */
11491 if (dpyinfo)
11492 dpyinfo->display = 0;
11493
11494 /* First delete frames whose mini-buffers are on frames
11495 that are on the dead display. */
11496 FOR_EACH_FRAME (tail, frame)
11497 {
11498 Lisp_Object minibuf_frame;
11499 minibuf_frame
11500 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11501 if (FRAME_X_P (XFRAME (frame))
11502 && FRAME_X_P (XFRAME (minibuf_frame))
11503 && ! EQ (frame, minibuf_frame)
11504 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11505 Fdelete_frame (frame, Qt);
11506 }
11507
11508 /* Now delete all remaining frames on the dead display.
11509 We are now sure none of these is used as the mini-buffer
11510 for another frame that we need to delete. */
11511 FOR_EACH_FRAME (tail, frame)
11512 if (FRAME_X_P (XFRAME (frame))
11513 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11514 {
11515 /* Set this to t so that Fdelete_frame won't get confused
11516 trying to find a replacement. */
11517 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11518 Fdelete_frame (frame, Qt);
11519 }
11520
11521 if (dpyinfo)
11522 x_delete_display (dpyinfo);
11523
11524 x_uncatch_errors (dpy, count);
11525
11526 if (x_display_list == 0)
11527 {
11528 fprintf (stderr, "%s\n", msg);
11529 shut_down_emacs (0, 0, Qnil);
11530 exit (70);
11531 }
11532
11533 /* Ordinary stack unwind doesn't deal with these. */
11534 #ifdef SIGIO
11535 sigunblock (sigmask (SIGIO));
11536 #endif
11537 sigunblock (sigmask (SIGALRM));
11538 TOTALLY_UNBLOCK_INPUT;
11539
11540 clear_waiting_for_input ();
11541 error ("%s", msg);
11542 }
11543
11544
11545 /* This is the usual handler for X protocol errors.
11546 It kills all frames on the display that we got the error for.
11547 If that was the only one, it prints an error message and kills Emacs. */
11548
11549 static void
11550 x_error_quitter (display, error)
11551 Display *display;
11552 XErrorEvent *error;
11553 {
11554 char buf[256], buf1[356];
11555
11556 /* Note that there is no real way portable across R3/R4 to get the
11557 original error handler. */
11558
11559 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11560 sprintf (buf1, "X protocol error: %s on protocol request %d",
11561 buf, error->request_code);
11562 x_connection_closed (display, buf1);
11563 }
11564
11565
11566 /* This is the first-level handler for X protocol errors.
11567 It calls x_error_quitter or x_error_catcher. */
11568
11569 static int
11570 x_error_handler (display, error)
11571 Display *display;
11572 XErrorEvent *error;
11573 {
11574 if (! NILP (x_error_message_string))
11575 x_error_catcher (display, error);
11576 else
11577 x_error_quitter (display, error);
11578 return 0;
11579 }
11580
11581 /* This is the handler for X IO errors, always.
11582 It kills all frames on the display that we lost touch with.
11583 If that was the only one, it prints an error message and kills Emacs. */
11584
11585 static int
11586 x_io_error_quitter (display)
11587 Display *display;
11588 {
11589 char buf[256];
11590
11591 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11592 x_connection_closed (display, buf);
11593 return 0;
11594 }
11595 \f
11596 /* Changing the font of the frame. */
11597
11598 /* Give frame F the font named FONTNAME as its default font, and
11599 return the full name of that font. FONTNAME may be a wildcard
11600 pattern; in that case, we choose some font that fits the pattern.
11601 The return value shows which font we chose. */
11602
11603 Lisp_Object
11604 x_new_font (f, fontname)
11605 struct frame *f;
11606 register char *fontname;
11607 {
11608 struct font_info *fontp
11609 = FS_LOAD_FONT (f, 0, fontname, -1);
11610
11611 if (!fontp)
11612 return Qnil;
11613
11614 f->output_data.x->font = (XFontStruct *) (fontp->font);
11615 f->output_data.x->baseline_offset = fontp->baseline_offset;
11616 f->output_data.x->fontset = -1;
11617
11618 /* Compute the scroll bar width in character columns. */
11619 if (f->scroll_bar_pixel_width > 0)
11620 {
11621 int wid = FONT_WIDTH (f->output_data.x->font);
11622 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11623 }
11624 else
11625 {
11626 int wid = FONT_WIDTH (f->output_data.x->font);
11627 f->scroll_bar_cols = (14 + wid - 1) / wid;
11628 }
11629
11630 /* Now make the frame display the given font. */
11631 if (FRAME_X_WINDOW (f) != 0)
11632 {
11633 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11634 f->output_data.x->font->fid);
11635 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11636 f->output_data.x->font->fid);
11637 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11638 f->output_data.x->font->fid);
11639
11640 frame_update_line_height (f);
11641
11642 /* Don't change the size of a tip frame; there's no point in
11643 doing it because it's done in Fx_show_tip, and it leads to
11644 problems because the tip frame has no widget. */
11645 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11646 x_set_window_size (f, 0, f->width, f->height);
11647 }
11648 else
11649 /* If we are setting a new frame's font for the first time,
11650 there are no faces yet, so this font's height is the line height. */
11651 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11652
11653 return build_string (fontp->full_name);
11654 }
11655
11656 /* Give frame F the fontset named FONTSETNAME as its default font, and
11657 return the full name of that fontset. FONTSETNAME may be a wildcard
11658 pattern; in that case, we choose some fontset that fits the pattern.
11659 The return value shows which fontset we chose. */
11660
11661 Lisp_Object
11662 x_new_fontset (f, fontsetname)
11663 struct frame *f;
11664 char *fontsetname;
11665 {
11666 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11667 Lisp_Object result;
11668
11669 if (fontset < 0)
11670 return Qnil;
11671
11672 if (f->output_data.x->fontset == fontset)
11673 /* This fontset is already set in frame F. There's nothing more
11674 to do. */
11675 return fontset_name (fontset);
11676
11677 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11678
11679 if (!STRINGP (result))
11680 /* Can't load ASCII font. */
11681 return Qnil;
11682
11683 /* Since x_new_font doesn't update any fontset information, do it now. */
11684 f->output_data.x->fontset = fontset;
11685
11686 #ifdef HAVE_X_I18N
11687 if (FRAME_XIC (f)
11688 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11689 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11690 #endif
11691
11692 return build_string (fontsetname);
11693 }
11694
11695 \f
11696 /***********************************************************************
11697 X Input Methods
11698 ***********************************************************************/
11699
11700 #ifdef HAVE_X_I18N
11701
11702 #ifdef HAVE_X11R6
11703
11704 /* XIM destroy callback function, which is called whenever the
11705 connection to input method XIM dies. CLIENT_DATA contains a
11706 pointer to the x_display_info structure corresponding to XIM. */
11707
11708 static void
11709 xim_destroy_callback (xim, client_data, call_data)
11710 XIM xim;
11711 XPointer client_data;
11712 XPointer call_data;
11713 {
11714 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11715 Lisp_Object frame, tail;
11716
11717 BLOCK_INPUT;
11718
11719 /* No need to call XDestroyIC.. */
11720 FOR_EACH_FRAME (tail, frame)
11721 {
11722 struct frame *f = XFRAME (frame);
11723 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11724 {
11725 FRAME_XIC (f) = NULL;
11726 if (FRAME_XIC_FONTSET (f))
11727 {
11728 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11729 FRAME_XIC_FONTSET (f) = NULL;
11730 }
11731 }
11732 }
11733
11734 /* No need to call XCloseIM. */
11735 dpyinfo->xim = NULL;
11736 XFree (dpyinfo->xim_styles);
11737 UNBLOCK_INPUT;
11738 }
11739
11740 #endif /* HAVE_X11R6 */
11741
11742 /* Open the connection to the XIM server on display DPYINFO.
11743 RESOURCE_NAME is the resource name Emacs uses. */
11744
11745 static void
11746 xim_open_dpy (dpyinfo, resource_name)
11747 struct x_display_info *dpyinfo;
11748 char *resource_name;
11749 {
11750 #ifdef USE_XIM
11751 XIM xim;
11752
11753 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11754 dpyinfo->xim = xim;
11755
11756 if (xim)
11757 {
11758 #ifdef HAVE_X11R6
11759 XIMCallback destroy;
11760 #endif
11761
11762 /* Get supported styles and XIM values. */
11763 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11764
11765 #ifdef HAVE_X11R6
11766 destroy.callback = xim_destroy_callback;
11767 destroy.client_data = (XPointer)dpyinfo;
11768 /* This isn't prptotyped in OSF 5.0. */
11769 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11770 #endif
11771 }
11772
11773 #else /* not USE_XIM */
11774 dpyinfo->xim = NULL;
11775 #endif /* not USE_XIM */
11776 }
11777
11778
11779 #ifdef HAVE_X11R6_XIM
11780
11781 struct xim_inst_t
11782 {
11783 struct x_display_info *dpyinfo;
11784 char *resource_name;
11785 };
11786
11787 /* XIM instantiate callback function, which is called whenever an XIM
11788 server is available. DISPLAY is teh display of the XIM.
11789 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11790 when the callback was registered. */
11791
11792 static void
11793 xim_instantiate_callback (display, client_data, call_data)
11794 Display *display;
11795 XPointer client_data;
11796 XPointer call_data;
11797 {
11798 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11799 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11800
11801 /* We don't support multiple XIM connections. */
11802 if (dpyinfo->xim)
11803 return;
11804
11805 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11806
11807 /* Create XIC for the existing frames on the same display, as long
11808 as they have no XIC. */
11809 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11810 {
11811 Lisp_Object tail, frame;
11812
11813 BLOCK_INPUT;
11814 FOR_EACH_FRAME (tail, frame)
11815 {
11816 struct frame *f = XFRAME (frame);
11817
11818 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11819 if (FRAME_XIC (f) == NULL)
11820 {
11821 create_frame_xic (f);
11822 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11823 xic_set_statusarea (f);
11824 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11825 {
11826 struct window *w = XWINDOW (f->selected_window);
11827 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11828 }
11829 }
11830 }
11831
11832 UNBLOCK_INPUT;
11833 }
11834 }
11835
11836 #endif /* HAVE_X11R6_XIM */
11837
11838
11839 /* Open a connection to the XIM server on display DPYINFO.
11840 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11841 connection only at the first time. On X11R6, open the connection
11842 in the XIM instantiate callback function. */
11843
11844 static void
11845 xim_initialize (dpyinfo, resource_name)
11846 struct x_display_info *dpyinfo;
11847 char *resource_name;
11848 {
11849 #ifdef USE_XIM
11850 #ifdef HAVE_X11R6_XIM
11851 struct xim_inst_t *xim_inst;
11852 int len;
11853
11854 dpyinfo->xim = NULL;
11855 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11856 xim_inst->dpyinfo = dpyinfo;
11857 len = strlen (resource_name);
11858 xim_inst->resource_name = (char *) xmalloc (len + 1);
11859 bcopy (resource_name, xim_inst->resource_name, len + 1);
11860 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11861 resource_name, EMACS_CLASS,
11862 xim_instantiate_callback,
11863 /* Fixme: This is XPointer in
11864 XFree86 but (XPointer *) on
11865 Tru64, at least. */
11866 (XPointer) xim_inst);
11867 #else /* not HAVE_X11R6_XIM */
11868 dpyinfo->xim = NULL;
11869 xim_open_dpy (dpyinfo, resource_name);
11870 #endif /* not HAVE_X11R6_XIM */
11871
11872 #else /* not USE_XIM */
11873 dpyinfo->xim = NULL;
11874 #endif /* not USE_XIM */
11875 }
11876
11877
11878 /* Close the connection to the XIM server on display DPYINFO. */
11879
11880 static void
11881 xim_close_dpy (dpyinfo)
11882 struct x_display_info *dpyinfo;
11883 {
11884 #ifdef USE_XIM
11885 #ifdef HAVE_X11R6_XIM
11886 if (dpyinfo->display)
11887 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11888 NULL, EMACS_CLASS,
11889 xim_instantiate_callback, NULL);
11890 #endif /* not HAVE_X11R6_XIM */
11891 if (dpyinfo->display)
11892 XCloseIM (dpyinfo->xim);
11893 dpyinfo->xim = NULL;
11894 XFree (dpyinfo->xim_styles);
11895 #endif /* USE_XIM */
11896 }
11897
11898 #endif /* not HAVE_X11R6_XIM */
11899
11900
11901 \f
11902 /* Calculate the absolute position in frame F
11903 from its current recorded position values and gravity. */
11904
11905 void
11906 x_calc_absolute_position (f)
11907 struct frame *f;
11908 {
11909 Window child;
11910 int win_x = 0, win_y = 0;
11911 int flags = f->output_data.x->size_hint_flags;
11912 int this_window;
11913
11914 /* We have nothing to do if the current position
11915 is already for the top-left corner. */
11916 if (! ((flags & XNegative) || (flags & YNegative)))
11917 return;
11918
11919 #ifdef USE_X_TOOLKIT
11920 this_window = XtWindow (f->output_data.x->widget);
11921 #else
11922 this_window = FRAME_X_WINDOW (f);
11923 #endif
11924
11925 /* Find the position of the outside upper-left corner of
11926 the inner window, with respect to the outer window.
11927 But do this only if we will need the results. */
11928 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11929 {
11930 int count;
11931
11932 BLOCK_INPUT;
11933 count = x_catch_errors (FRAME_X_DISPLAY (f));
11934 while (1)
11935 {
11936 x_clear_errors (FRAME_X_DISPLAY (f));
11937 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11938
11939 /* From-window, to-window. */
11940 this_window,
11941 f->output_data.x->parent_desc,
11942
11943 /* From-position, to-position. */
11944 0, 0, &win_x, &win_y,
11945
11946 /* Child of win. */
11947 &child);
11948 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11949 {
11950 Window newroot, newparent = 0xdeadbeef;
11951 Window *newchildren;
11952 unsigned int nchildren;
11953
11954 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11955 &newparent, &newchildren, &nchildren))
11956 break;
11957
11958 XFree ((char *) newchildren);
11959
11960 f->output_data.x->parent_desc = newparent;
11961 }
11962 else
11963 break;
11964 }
11965
11966 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11967 UNBLOCK_INPUT;
11968 }
11969
11970 /* Treat negative positions as relative to the leftmost bottommost
11971 position that fits on the screen. */
11972 if (flags & XNegative)
11973 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11974 - 2 * f->output_data.x->border_width - win_x
11975 - PIXEL_WIDTH (f)
11976 + f->output_data.x->left_pos);
11977
11978 {
11979 int height = PIXEL_HEIGHT (f);
11980
11981 #if defined USE_X_TOOLKIT && defined USE_MOTIF
11982 /* Something is fishy here. When using Motif, starting Emacs with
11983 `-g -0-0', the frame appears too low by a few pixels.
11984
11985 This seems to be so because initially, while Emacs is starting,
11986 the column widget's height and the frame's pixel height are
11987 different. The column widget's height is the right one. In
11988 later invocations, when Emacs is up, the frame's pixel height
11989 is right, though.
11990
11991 It's not obvious where the initial small difference comes from.
11992 2000-12-01, gerd. */
11993
11994 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
11995 #endif
11996
11997 if (flags & YNegative)
11998 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11999 - 2 * f->output_data.x->border_width
12000 - win_y
12001 - height
12002 + f->output_data.x->top_pos);
12003 }
12004
12005 /* The left_pos and top_pos
12006 are now relative to the top and left screen edges,
12007 so the flags should correspond. */
12008 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12009 }
12010
12011 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12012 to really change the position, and 0 when calling from
12013 x_make_frame_visible (in that case, XOFF and YOFF are the current
12014 position values). It is -1 when calling from x_set_frame_parameters,
12015 which means, do adjust for borders but don't change the gravity. */
12016
12017 void
12018 x_set_offset (f, xoff, yoff, change_gravity)
12019 struct frame *f;
12020 register int xoff, yoff;
12021 int change_gravity;
12022 {
12023 int modified_top, modified_left;
12024
12025 if (change_gravity > 0)
12026 {
12027 f->output_data.x->top_pos = yoff;
12028 f->output_data.x->left_pos = xoff;
12029 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12030 if (xoff < 0)
12031 f->output_data.x->size_hint_flags |= XNegative;
12032 if (yoff < 0)
12033 f->output_data.x->size_hint_flags |= YNegative;
12034 f->output_data.x->win_gravity = NorthWestGravity;
12035 }
12036 x_calc_absolute_position (f);
12037
12038 BLOCK_INPUT;
12039 x_wm_set_size_hint (f, (long) 0, 0);
12040
12041 modified_left = f->output_data.x->left_pos;
12042 modified_top = f->output_data.x->top_pos;
12043 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12044 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12045 /* It is a mystery why we need to add the border_width here
12046 when the frame is already visible, but experiment says we do. */
12047 if (change_gravity != 0)
12048 {
12049 modified_left += f->output_data.x->border_width;
12050 modified_top += f->output_data.x->border_width;
12051 }
12052 #endif
12053
12054 #ifdef USE_X_TOOLKIT
12055 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12056 modified_left, modified_top);
12057 #else /* not USE_X_TOOLKIT */
12058 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12059 modified_left, modified_top);
12060 #endif /* not USE_X_TOOLKIT */
12061 UNBLOCK_INPUT;
12062 }
12063
12064
12065 /* Change the size of frame F's X window to COLS/ROWS in the case F
12066 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12067 top-left-corner window gravity for this size change and subsequent
12068 size changes. Otherwise we leave the window gravity unchanged. */
12069
12070 static void
12071 x_set_window_size_1 (f, change_gravity, cols, rows)
12072 struct frame *f;
12073 int change_gravity;
12074 int cols, rows;
12075 {
12076 int pixelwidth, pixelheight;
12077
12078 check_frame_size (f, &rows, &cols);
12079 f->output_data.x->vertical_scroll_bar_extra
12080 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12081 ? 0
12082 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12083 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12084 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12085 f->output_data.x->flags_areas_extra
12086 = FRAME_FLAGS_AREA_WIDTH (f);
12087 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12088 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12089
12090 f->output_data.x->win_gravity = NorthWestGravity;
12091 x_wm_set_size_hint (f, (long) 0, 0);
12092
12093 XSync (FRAME_X_DISPLAY (f), False);
12094 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12095 pixelwidth, pixelheight);
12096
12097 /* Now, strictly speaking, we can't be sure that this is accurate,
12098 but the window manager will get around to dealing with the size
12099 change request eventually, and we'll hear how it went when the
12100 ConfigureNotify event gets here.
12101
12102 We could just not bother storing any of this information here,
12103 and let the ConfigureNotify event set everything up, but that
12104 might be kind of confusing to the Lisp code, since size changes
12105 wouldn't be reported in the frame parameters until some random
12106 point in the future when the ConfigureNotify event arrives.
12107
12108 We pass 1 for DELAY since we can't run Lisp code inside of
12109 a BLOCK_INPUT. */
12110 change_frame_size (f, rows, cols, 0, 1, 0);
12111 PIXEL_WIDTH (f) = pixelwidth;
12112 PIXEL_HEIGHT (f) = pixelheight;
12113
12114 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12115 receive in the ConfigureNotify event; if we get what we asked
12116 for, then the event won't cause the screen to become garbaged, so
12117 we have to make sure to do it here. */
12118 SET_FRAME_GARBAGED (f);
12119
12120 XFlush (FRAME_X_DISPLAY (f));
12121 }
12122
12123
12124 /* Call this to change the size of frame F's x-window.
12125 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12126 for this size change and subsequent size changes.
12127 Otherwise we leave the window gravity unchanged. */
12128
12129 void
12130 x_set_window_size (f, change_gravity, cols, rows)
12131 struct frame *f;
12132 int change_gravity;
12133 int cols, rows;
12134 {
12135 BLOCK_INPUT;
12136
12137 #ifdef USE_X_TOOLKIT
12138
12139 if (f->output_data.x->widget != NULL)
12140 {
12141 /* The x and y position of the widget is clobbered by the
12142 call to XtSetValues within EmacsFrameSetCharSize.
12143 This is a real kludge, but I don't understand Xt so I can't
12144 figure out a correct fix. Can anyone else tell me? -- rms. */
12145 int xpos = f->output_data.x->widget->core.x;
12146 int ypos = f->output_data.x->widget->core.y;
12147 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12148 f->output_data.x->widget->core.x = xpos;
12149 f->output_data.x->widget->core.y = ypos;
12150 }
12151 else
12152 x_set_window_size_1 (f, change_gravity, cols, rows);
12153
12154 #else /* not USE_X_TOOLKIT */
12155
12156 x_set_window_size_1 (f, change_gravity, cols, rows);
12157
12158 #endif /* not USE_X_TOOLKIT */
12159
12160 /* If cursor was outside the new size, mark it as off. */
12161 mark_window_cursors_off (XWINDOW (f->root_window));
12162
12163 /* Clear out any recollection of where the mouse highlighting was,
12164 since it might be in a place that's outside the new frame size.
12165 Actually checking whether it is outside is a pain in the neck,
12166 so don't try--just let the highlighting be done afresh with new size. */
12167 cancel_mouse_face (f);
12168
12169 UNBLOCK_INPUT;
12170 }
12171 \f
12172 /* Mouse warping. */
12173
12174 void
12175 x_set_mouse_position (f, x, y)
12176 struct frame *f;
12177 int x, y;
12178 {
12179 int pix_x, pix_y;
12180
12181 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12182 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12183
12184 if (pix_x < 0) pix_x = 0;
12185 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12186
12187 if (pix_y < 0) pix_y = 0;
12188 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12189
12190 BLOCK_INPUT;
12191
12192 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12193 0, 0, 0, 0, pix_x, pix_y);
12194 UNBLOCK_INPUT;
12195 }
12196
12197 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12198
12199 void
12200 x_set_mouse_pixel_position (f, pix_x, pix_y)
12201 struct frame *f;
12202 int pix_x, pix_y;
12203 {
12204 BLOCK_INPUT;
12205
12206 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12207 0, 0, 0, 0, pix_x, pix_y);
12208 UNBLOCK_INPUT;
12209 }
12210 \f
12211 /* focus shifting, raising and lowering. */
12212
12213 void
12214 x_focus_on_frame (f)
12215 struct frame *f;
12216 {
12217 #if 0 /* This proves to be unpleasant. */
12218 x_raise_frame (f);
12219 #endif
12220 #if 0
12221 /* I don't think that the ICCCM allows programs to do things like this
12222 without the interaction of the window manager. Whatever you end up
12223 doing with this code, do it to x_unfocus_frame too. */
12224 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12225 RevertToPointerRoot, CurrentTime);
12226 #endif /* ! 0 */
12227 }
12228
12229 void
12230 x_unfocus_frame (f)
12231 struct frame *f;
12232 {
12233 #if 0
12234 /* Look at the remarks in x_focus_on_frame. */
12235 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12236 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12237 RevertToPointerRoot, CurrentTime);
12238 #endif /* ! 0 */
12239 }
12240
12241 /* Raise frame F. */
12242
12243 void
12244 x_raise_frame (f)
12245 struct frame *f;
12246 {
12247 if (f->async_visible)
12248 {
12249 BLOCK_INPUT;
12250 #ifdef USE_X_TOOLKIT
12251 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12252 #else /* not USE_X_TOOLKIT */
12253 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12254 #endif /* not USE_X_TOOLKIT */
12255 XFlush (FRAME_X_DISPLAY (f));
12256 UNBLOCK_INPUT;
12257 }
12258 }
12259
12260 /* Lower frame F. */
12261
12262 void
12263 x_lower_frame (f)
12264 struct frame *f;
12265 {
12266 if (f->async_visible)
12267 {
12268 BLOCK_INPUT;
12269 #ifdef USE_X_TOOLKIT
12270 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12271 #else /* not USE_X_TOOLKIT */
12272 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12273 #endif /* not USE_X_TOOLKIT */
12274 XFlush (FRAME_X_DISPLAY (f));
12275 UNBLOCK_INPUT;
12276 }
12277 }
12278
12279 static void
12280 XTframe_raise_lower (f, raise_flag)
12281 FRAME_PTR f;
12282 int raise_flag;
12283 {
12284 if (raise_flag)
12285 x_raise_frame (f);
12286 else
12287 x_lower_frame (f);
12288 }
12289 \f
12290 /* Change of visibility. */
12291
12292 /* This tries to wait until the frame is really visible.
12293 However, if the window manager asks the user where to position
12294 the frame, this will return before the user finishes doing that.
12295 The frame will not actually be visible at that time,
12296 but it will become visible later when the window manager
12297 finishes with it. */
12298
12299 void
12300 x_make_frame_visible (f)
12301 struct frame *f;
12302 {
12303 Lisp_Object type;
12304 int original_top, original_left;
12305 int retry_count = 2;
12306
12307 retry:
12308
12309 BLOCK_INPUT;
12310
12311 type = x_icon_type (f);
12312 if (!NILP (type))
12313 x_bitmap_icon (f, type);
12314
12315 if (! FRAME_VISIBLE_P (f))
12316 {
12317 /* We test FRAME_GARBAGED_P here to make sure we don't
12318 call x_set_offset a second time
12319 if we get to x_make_frame_visible a second time
12320 before the window gets really visible. */
12321 if (! FRAME_ICONIFIED_P (f)
12322 && ! f->output_data.x->asked_for_visible)
12323 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12324
12325 f->output_data.x->asked_for_visible = 1;
12326
12327 if (! EQ (Vx_no_window_manager, Qt))
12328 x_wm_set_window_state (f, NormalState);
12329 #ifdef USE_X_TOOLKIT
12330 /* This was XtPopup, but that did nothing for an iconified frame. */
12331 XtMapWidget (f->output_data.x->widget);
12332 #else /* not USE_X_TOOLKIT */
12333 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12334 #endif /* not USE_X_TOOLKIT */
12335 #if 0 /* This seems to bring back scroll bars in the wrong places
12336 if the window configuration has changed. They seem
12337 to come back ok without this. */
12338 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12339 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12340 #endif
12341 }
12342
12343 XFlush (FRAME_X_DISPLAY (f));
12344
12345 /* Synchronize to ensure Emacs knows the frame is visible
12346 before we do anything else. We do this loop with input not blocked
12347 so that incoming events are handled. */
12348 {
12349 Lisp_Object frame;
12350 int count;
12351 /* This must be before UNBLOCK_INPUT
12352 since events that arrive in response to the actions above
12353 will set it when they are handled. */
12354 int previously_visible = f->output_data.x->has_been_visible;
12355
12356 original_left = f->output_data.x->left_pos;
12357 original_top = f->output_data.x->top_pos;
12358
12359 /* This must come after we set COUNT. */
12360 UNBLOCK_INPUT;
12361
12362 /* We unblock here so that arriving X events are processed. */
12363
12364 /* Now move the window back to where it was "supposed to be".
12365 But don't do it if the gravity is negative.
12366 When the gravity is negative, this uses a position
12367 that is 3 pixels too low. Perhaps that's really the border width.
12368
12369 Don't do this if the window has never been visible before,
12370 because the window manager may choose the position
12371 and we don't want to override it. */
12372
12373 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12374 && f->output_data.x->win_gravity == NorthWestGravity
12375 && previously_visible)
12376 {
12377 Drawable rootw;
12378 int x, y;
12379 unsigned int width, height, border, depth;
12380
12381 BLOCK_INPUT;
12382
12383 /* On some window managers (such as FVWM) moving an existing
12384 window, even to the same place, causes the window manager
12385 to introduce an offset. This can cause the window to move
12386 to an unexpected location. Check the geometry (a little
12387 slow here) and then verify that the window is in the right
12388 place. If the window is not in the right place, move it
12389 there, and take the potential window manager hit. */
12390 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12391 &rootw, &x, &y, &width, &height, &border, &depth);
12392
12393 if (original_left != x || original_top != y)
12394 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12395 original_left, original_top);
12396
12397 UNBLOCK_INPUT;
12398 }
12399
12400 XSETFRAME (frame, f);
12401
12402 /* Wait until the frame is visible. Process X events until a
12403 MapNotify event has been seen, or until we think we won't get a
12404 MapNotify at all.. */
12405 for (count = input_signal_count + 10;
12406 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12407 {
12408 /* Force processing of queued events. */
12409 x_sync (f);
12410
12411 /* Machines that do polling rather than SIGIO have been
12412 observed to go into a busy-wait here. So we'll fake an
12413 alarm signal to let the handler know that there's something
12414 to be read. We used to raise a real alarm, but it seems
12415 that the handler isn't always enabled here. This is
12416 probably a bug. */
12417 if (input_polling_used ())
12418 {
12419 /* It could be confusing if a real alarm arrives while
12420 processing the fake one. Turn it off and let the
12421 handler reset it. */
12422 extern void poll_for_input_1 P_ ((void));
12423 int old_poll_suppress_count = poll_suppress_count;
12424 poll_suppress_count = 1;
12425 poll_for_input_1 ();
12426 poll_suppress_count = old_poll_suppress_count;
12427 }
12428
12429 /* See if a MapNotify event has been processed. */
12430 FRAME_SAMPLE_VISIBILITY (f);
12431 }
12432
12433 /* 2000-09-28: In
12434
12435 (let ((f (selected-frame)))
12436 (iconify-frame f)
12437 (raise-frame f))
12438
12439 the frame is not raised with various window managers on
12440 FreeBSD, Linux and Solaris. It turns out that, for some
12441 unknown reason, the call to XtMapWidget is completely ignored.
12442 Mapping the widget a second time works. */
12443
12444 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12445 goto retry;
12446 }
12447 }
12448
12449 /* Change from mapped state to withdrawn state. */
12450
12451 /* Make the frame visible (mapped and not iconified). */
12452
12453 void
12454 x_make_frame_invisible (f)
12455 struct frame *f;
12456 {
12457 Window window;
12458
12459 #ifdef USE_X_TOOLKIT
12460 /* Use the frame's outermost window, not the one we normally draw on. */
12461 window = XtWindow (f->output_data.x->widget);
12462 #else /* not USE_X_TOOLKIT */
12463 window = FRAME_X_WINDOW (f);
12464 #endif /* not USE_X_TOOLKIT */
12465
12466 /* Don't keep the highlight on an invisible frame. */
12467 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12468 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12469
12470 #if 0/* This might add unreliability; I don't trust it -- rms. */
12471 if (! f->async_visible && ! f->async_iconified)
12472 return;
12473 #endif
12474
12475 BLOCK_INPUT;
12476
12477 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12478 that the current position of the window is user-specified, rather than
12479 program-specified, so that when the window is mapped again, it will be
12480 placed at the same location, without forcing the user to position it
12481 by hand again (they have already done that once for this window.) */
12482 x_wm_set_size_hint (f, (long) 0, 1);
12483
12484 #ifdef HAVE_X11R4
12485
12486 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12487 DefaultScreen (FRAME_X_DISPLAY (f))))
12488 {
12489 UNBLOCK_INPUT_RESIGNAL;
12490 error ("Can't notify window manager of window withdrawal");
12491 }
12492 #else /* ! defined (HAVE_X11R4) */
12493
12494 /* Tell the window manager what we're going to do. */
12495 if (! EQ (Vx_no_window_manager, Qt))
12496 {
12497 XEvent unmap;
12498
12499 unmap.xunmap.type = UnmapNotify;
12500 unmap.xunmap.window = window;
12501 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12502 unmap.xunmap.from_configure = False;
12503 if (! XSendEvent (FRAME_X_DISPLAY (f),
12504 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12505 False,
12506 SubstructureRedirectMaskSubstructureNotifyMask,
12507 &unmap))
12508 {
12509 UNBLOCK_INPUT_RESIGNAL;
12510 error ("Can't notify window manager of withdrawal");
12511 }
12512 }
12513
12514 /* Unmap the window ourselves. Cheeky! */
12515 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12516 #endif /* ! defined (HAVE_X11R4) */
12517
12518 /* We can't distinguish this from iconification
12519 just by the event that we get from the server.
12520 So we can't win using the usual strategy of letting
12521 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12522 and synchronize with the server to make sure we agree. */
12523 f->visible = 0;
12524 FRAME_ICONIFIED_P (f) = 0;
12525 f->async_visible = 0;
12526 f->async_iconified = 0;
12527
12528 x_sync (f);
12529
12530 UNBLOCK_INPUT;
12531 }
12532
12533 /* Change window state from mapped to iconified. */
12534
12535 void
12536 x_iconify_frame (f)
12537 struct frame *f;
12538 {
12539 int result;
12540 Lisp_Object type;
12541
12542 /* Don't keep the highlight on an invisible frame. */
12543 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12544 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12545
12546 if (f->async_iconified)
12547 return;
12548
12549 BLOCK_INPUT;
12550
12551 FRAME_SAMPLE_VISIBILITY (f);
12552
12553 type = x_icon_type (f);
12554 if (!NILP (type))
12555 x_bitmap_icon (f, type);
12556
12557 #ifdef USE_X_TOOLKIT
12558
12559 if (! FRAME_VISIBLE_P (f))
12560 {
12561 if (! EQ (Vx_no_window_manager, Qt))
12562 x_wm_set_window_state (f, IconicState);
12563 /* This was XtPopup, but that did nothing for an iconified frame. */
12564 XtMapWidget (f->output_data.x->widget);
12565 /* The server won't give us any event to indicate
12566 that an invisible frame was changed to an icon,
12567 so we have to record it here. */
12568 f->iconified = 1;
12569 f->visible = 1;
12570 f->async_iconified = 1;
12571 f->async_visible = 0;
12572 UNBLOCK_INPUT;
12573 return;
12574 }
12575
12576 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12577 XtWindow (f->output_data.x->widget),
12578 DefaultScreen (FRAME_X_DISPLAY (f)));
12579 UNBLOCK_INPUT;
12580
12581 if (!result)
12582 error ("Can't notify window manager of iconification");
12583
12584 f->async_iconified = 1;
12585 f->async_visible = 0;
12586
12587
12588 BLOCK_INPUT;
12589 XFlush (FRAME_X_DISPLAY (f));
12590 UNBLOCK_INPUT;
12591 #else /* not USE_X_TOOLKIT */
12592
12593 /* Make sure the X server knows where the window should be positioned,
12594 in case the user deiconifies with the window manager. */
12595 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12596 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12597
12598 /* Since we don't know which revision of X we're running, we'll use both
12599 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12600
12601 /* X11R4: send a ClientMessage to the window manager using the
12602 WM_CHANGE_STATE type. */
12603 {
12604 XEvent message;
12605
12606 message.xclient.window = FRAME_X_WINDOW (f);
12607 message.xclient.type = ClientMessage;
12608 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12609 message.xclient.format = 32;
12610 message.xclient.data.l[0] = IconicState;
12611
12612 if (! XSendEvent (FRAME_X_DISPLAY (f),
12613 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12614 False,
12615 SubstructureRedirectMask | SubstructureNotifyMask,
12616 &message))
12617 {
12618 UNBLOCK_INPUT_RESIGNAL;
12619 error ("Can't notify window manager of iconification");
12620 }
12621 }
12622
12623 /* X11R3: set the initial_state field of the window manager hints to
12624 IconicState. */
12625 x_wm_set_window_state (f, IconicState);
12626
12627 if (!FRAME_VISIBLE_P (f))
12628 {
12629 /* If the frame was withdrawn, before, we must map it. */
12630 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12631 }
12632
12633 f->async_iconified = 1;
12634 f->async_visible = 0;
12635
12636 XFlush (FRAME_X_DISPLAY (f));
12637 UNBLOCK_INPUT;
12638 #endif /* not USE_X_TOOLKIT */
12639 }
12640
12641 \f
12642 /* Free X resources of frame F. */
12643
12644 void
12645 x_free_frame_resources (f)
12646 struct frame *f;
12647 {
12648 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12649
12650 BLOCK_INPUT;
12651
12652 /* If a display connection is dead, don't try sending more
12653 commands to the X server. */
12654 if (dpyinfo->display)
12655 {
12656 if (f->output_data.x->icon_desc)
12657 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12658
12659 #ifdef HAVE_X_I18N
12660 if (FRAME_XIC (f))
12661 free_frame_xic (f);
12662 #endif
12663
12664 if (FRAME_X_WINDOW (f))
12665 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12666
12667 #ifdef USE_X_TOOLKIT
12668 if (f->output_data.x->widget)
12669 XtDestroyWidget (f->output_data.x->widget);
12670 free_frame_menubar (f);
12671 #endif /* USE_X_TOOLKIT */
12672
12673 unload_color (f, f->output_data.x->foreground_pixel);
12674 unload_color (f, f->output_data.x->background_pixel);
12675 unload_color (f, f->output_data.x->cursor_pixel);
12676 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12677 unload_color (f, f->output_data.x->border_pixel);
12678 unload_color (f, f->output_data.x->mouse_pixel);
12679
12680 if (f->output_data.x->scroll_bar_background_pixel != -1)
12681 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12682 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12683 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12684 if (f->output_data.x->white_relief.allocated_p)
12685 unload_color (f, f->output_data.x->white_relief.pixel);
12686 if (f->output_data.x->black_relief.allocated_p)
12687 unload_color (f, f->output_data.x->black_relief.pixel);
12688
12689 if (FRAME_FACE_CACHE (f))
12690 free_frame_faces (f);
12691
12692 x_free_gcs (f);
12693 XFlush (FRAME_X_DISPLAY (f));
12694 }
12695
12696 if (f->output_data.x->saved_menu_event)
12697 xfree (f->output_data.x->saved_menu_event);
12698
12699 xfree (f->output_data.x);
12700 f->output_data.x = NULL;
12701
12702 if (f == dpyinfo->x_focus_frame)
12703 dpyinfo->x_focus_frame = 0;
12704 if (f == dpyinfo->x_focus_event_frame)
12705 dpyinfo->x_focus_event_frame = 0;
12706 if (f == dpyinfo->x_highlight_frame)
12707 dpyinfo->x_highlight_frame = 0;
12708
12709 if (f == dpyinfo->mouse_face_mouse_frame)
12710 {
12711 dpyinfo->mouse_face_beg_row
12712 = dpyinfo->mouse_face_beg_col = -1;
12713 dpyinfo->mouse_face_end_row
12714 = dpyinfo->mouse_face_end_col = -1;
12715 dpyinfo->mouse_face_window = Qnil;
12716 dpyinfo->mouse_face_deferred_gc = 0;
12717 dpyinfo->mouse_face_mouse_frame = 0;
12718 }
12719
12720 UNBLOCK_INPUT;
12721 }
12722
12723
12724 /* Destroy the X window of frame F. */
12725
12726 void
12727 x_destroy_window (f)
12728 struct frame *f;
12729 {
12730 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12731
12732 /* If a display connection is dead, don't try sending more
12733 commands to the X server. */
12734 if (dpyinfo->display != 0)
12735 x_free_frame_resources (f);
12736
12737 dpyinfo->reference_count--;
12738 }
12739
12740 \f
12741 /* Setting window manager hints. */
12742
12743 /* Set the normal size hints for the window manager, for frame F.
12744 FLAGS is the flags word to use--or 0 meaning preserve the flags
12745 that the window now has.
12746 If USER_POSITION is nonzero, we set the USPosition
12747 flag (this is useful when FLAGS is 0). */
12748
12749 void
12750 x_wm_set_size_hint (f, flags, user_position)
12751 struct frame *f;
12752 long flags;
12753 int user_position;
12754 {
12755 XSizeHints size_hints;
12756
12757 #ifdef USE_X_TOOLKIT
12758 Arg al[2];
12759 int ac = 0;
12760 Dimension widget_width, widget_height;
12761 Window window = XtWindow (f->output_data.x->widget);
12762 #else /* not USE_X_TOOLKIT */
12763 Window window = FRAME_X_WINDOW (f);
12764 #endif /* not USE_X_TOOLKIT */
12765
12766 /* Setting PMaxSize caused various problems. */
12767 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12768
12769 size_hints.x = f->output_data.x->left_pos;
12770 size_hints.y = f->output_data.x->top_pos;
12771
12772 #ifdef USE_X_TOOLKIT
12773 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12774 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12775 XtGetValues (f->output_data.x->widget, al, ac);
12776 size_hints.height = widget_height;
12777 size_hints.width = widget_width;
12778 #else /* not USE_X_TOOLKIT */
12779 size_hints.height = PIXEL_HEIGHT (f);
12780 size_hints.width = PIXEL_WIDTH (f);
12781 #endif /* not USE_X_TOOLKIT */
12782
12783 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12784 size_hints.height_inc = f->output_data.x->line_height;
12785 size_hints.max_width
12786 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12787 size_hints.max_height
12788 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12789
12790 /* Calculate the base and minimum sizes.
12791
12792 (When we use the X toolkit, we don't do it here.
12793 Instead we copy the values that the widgets are using, below.) */
12794 #ifndef USE_X_TOOLKIT
12795 {
12796 int base_width, base_height;
12797 int min_rows = 0, min_cols = 0;
12798
12799 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12800 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12801
12802 check_frame_size (f, &min_rows, &min_cols);
12803
12804 /* The window manager uses the base width hints to calculate the
12805 current number of rows and columns in the frame while
12806 resizing; min_width and min_height aren't useful for this
12807 purpose, since they might not give the dimensions for a
12808 zero-row, zero-column frame.
12809
12810 We use the base_width and base_height members if we have
12811 them; otherwise, we set the min_width and min_height members
12812 to the size for a zero x zero frame. */
12813
12814 #ifdef HAVE_X11R4
12815 size_hints.flags |= PBaseSize;
12816 size_hints.base_width = base_width;
12817 size_hints.base_height = base_height;
12818 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12819 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12820 #else
12821 size_hints.min_width = base_width;
12822 size_hints.min_height = base_height;
12823 #endif
12824 }
12825
12826 /* If we don't need the old flags, we don't need the old hint at all. */
12827 if (flags)
12828 {
12829 size_hints.flags |= flags;
12830 goto no_read;
12831 }
12832 #endif /* not USE_X_TOOLKIT */
12833
12834 {
12835 XSizeHints hints; /* Sometimes I hate X Windows... */
12836 long supplied_return;
12837 int value;
12838
12839 #ifdef HAVE_X11R4
12840 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12841 &supplied_return);
12842 #else
12843 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12844 #endif
12845
12846 #ifdef USE_X_TOOLKIT
12847 size_hints.base_height = hints.base_height;
12848 size_hints.base_width = hints.base_width;
12849 size_hints.min_height = hints.min_height;
12850 size_hints.min_width = hints.min_width;
12851 #endif
12852
12853 if (flags)
12854 size_hints.flags |= flags;
12855 else
12856 {
12857 if (value == 0)
12858 hints.flags = 0;
12859 if (hints.flags & PSize)
12860 size_hints.flags |= PSize;
12861 if (hints.flags & PPosition)
12862 size_hints.flags |= PPosition;
12863 if (hints.flags & USPosition)
12864 size_hints.flags |= USPosition;
12865 if (hints.flags & USSize)
12866 size_hints.flags |= USSize;
12867 }
12868 }
12869
12870 #ifndef USE_X_TOOLKIT
12871 no_read:
12872 #endif
12873
12874 #ifdef PWinGravity
12875 size_hints.win_gravity = f->output_data.x->win_gravity;
12876 size_hints.flags |= PWinGravity;
12877
12878 if (user_position)
12879 {
12880 size_hints.flags &= ~ PPosition;
12881 size_hints.flags |= USPosition;
12882 }
12883 #endif /* PWinGravity */
12884
12885 #ifdef HAVE_X11R4
12886 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12887 #else
12888 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12889 #endif
12890 }
12891
12892 /* Used for IconicState or NormalState */
12893
12894 void
12895 x_wm_set_window_state (f, state)
12896 struct frame *f;
12897 int state;
12898 {
12899 #ifdef USE_X_TOOLKIT
12900 Arg al[1];
12901
12902 XtSetArg (al[0], XtNinitialState, state);
12903 XtSetValues (f->output_data.x->widget, al, 1);
12904 #else /* not USE_X_TOOLKIT */
12905 Window window = FRAME_X_WINDOW (f);
12906
12907 f->output_data.x->wm_hints.flags |= StateHint;
12908 f->output_data.x->wm_hints.initial_state = state;
12909
12910 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12911 #endif /* not USE_X_TOOLKIT */
12912 }
12913
12914 void
12915 x_wm_set_icon_pixmap (f, pixmap_id)
12916 struct frame *f;
12917 int pixmap_id;
12918 {
12919 Pixmap icon_pixmap;
12920
12921 #ifndef USE_X_TOOLKIT
12922 Window window = FRAME_X_WINDOW (f);
12923 #endif
12924
12925 if (pixmap_id > 0)
12926 {
12927 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12928 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12929 }
12930 else
12931 {
12932 /* It seems there is no way to turn off use of an icon pixmap.
12933 The following line does it, only if no icon has yet been created,
12934 for some window managers. But with mwm it crashes.
12935 Some people say it should clear the IconPixmapHint bit in this case,
12936 but that doesn't work, and the X consortium said it isn't the
12937 right thing at all. Since there is no way to win,
12938 best to explicitly give up. */
12939 #if 0
12940 f->output_data.x->wm_hints.icon_pixmap = None;
12941 #else
12942 return;
12943 #endif
12944 }
12945
12946 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12947
12948 {
12949 Arg al[1];
12950 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12951 XtSetValues (f->output_data.x->widget, al, 1);
12952 }
12953
12954 #else /* not USE_X_TOOLKIT */
12955
12956 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12957 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12958
12959 #endif /* not USE_X_TOOLKIT */
12960 }
12961
12962 void
12963 x_wm_set_icon_position (f, icon_x, icon_y)
12964 struct frame *f;
12965 int icon_x, icon_y;
12966 {
12967 #ifdef USE_X_TOOLKIT
12968 Window window = XtWindow (f->output_data.x->widget);
12969 #else
12970 Window window = FRAME_X_WINDOW (f);
12971 #endif
12972
12973 f->output_data.x->wm_hints.flags |= IconPositionHint;
12974 f->output_data.x->wm_hints.icon_x = icon_x;
12975 f->output_data.x->wm_hints.icon_y = icon_y;
12976
12977 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12978 }
12979
12980 \f
12981 /***********************************************************************
12982 Fonts
12983 ***********************************************************************/
12984
12985 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12986
12987 struct font_info *
12988 x_get_font_info (f, font_idx)
12989 FRAME_PTR f;
12990 int font_idx;
12991 {
12992 return (FRAME_X_FONT_TABLE (f) + font_idx);
12993 }
12994
12995
12996 /* Return a list of names of available fonts matching PATTERN on frame F.
12997
12998 If SIZE is > 0, it is the size (maximum bounds width) of fonts
12999 to be listed.
13000
13001 SIZE < 0 means include scalable fonts.
13002
13003 Frame F null means we have not yet created any frame on X, and
13004 consult the first display in x_display_list. MAXNAMES sets a limit
13005 on how many fonts to match. */
13006
13007 Lisp_Object
13008 x_list_fonts (f, pattern, size, maxnames)
13009 struct frame *f;
13010 Lisp_Object pattern;
13011 int size;
13012 int maxnames;
13013 {
13014 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13015 Lisp_Object tem, second_best;
13016 struct x_display_info *dpyinfo
13017 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13018 Display *dpy = dpyinfo->display;
13019 int try_XLoadQueryFont = 0;
13020 int count;
13021 int allow_scalable_fonts_p = 0;
13022
13023 if (size < 0)
13024 {
13025 allow_scalable_fonts_p = 1;
13026 size = 0;
13027 }
13028
13029 patterns = Fassoc (pattern, Valternate_fontname_alist);
13030 if (NILP (patterns))
13031 patterns = Fcons (pattern, Qnil);
13032
13033 if (maxnames == 1 && !size)
13034 /* We can return any single font matching PATTERN. */
13035 try_XLoadQueryFont = 1;
13036
13037 for (; CONSP (patterns); patterns = XCDR (patterns))
13038 {
13039 int num_fonts;
13040 char **names = NULL;
13041
13042 pattern = XCAR (patterns);
13043 /* See if we cached the result for this particular query.
13044 The cache is an alist of the form:
13045 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13046 tem = XCDR (dpyinfo->name_list_element);
13047 key = Fcons (Fcons (pattern, make_number (maxnames)),
13048 allow_scalable_fonts_p ? Qt : Qnil);
13049 list = Fassoc (key, tem);
13050 if (!NILP (list))
13051 {
13052 list = Fcdr_safe (list);
13053 /* We have a cashed list. Don't have to get the list again. */
13054 goto label_cached;
13055 }
13056
13057 /* At first, put PATTERN in the cache. */
13058
13059 BLOCK_INPUT;
13060 count = x_catch_errors (dpy);
13061
13062 if (try_XLoadQueryFont)
13063 {
13064 XFontStruct *font;
13065 unsigned long value;
13066
13067 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13068 if (x_had_errors_p (dpy))
13069 {
13070 /* This error is perhaps due to insufficient memory on X
13071 server. Let's just ignore it. */
13072 font = NULL;
13073 x_clear_errors (dpy);
13074 }
13075
13076 if (font
13077 && XGetFontProperty (font, XA_FONT, &value))
13078 {
13079 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13080 int len = strlen (name);
13081 char *tmp;
13082
13083 /* If DXPC (a Differential X Protocol Compressor)
13084 Ver.3.7 is running, XGetAtomName will return null
13085 string. We must avoid such a name. */
13086 if (len == 0)
13087 try_XLoadQueryFont = 0;
13088 else
13089 {
13090 num_fonts = 1;
13091 names = (char **) alloca (sizeof (char *));
13092 /* Some systems only allow alloca assigned to a
13093 simple var. */
13094 tmp = (char *) alloca (len + 1); names[0] = tmp;
13095 bcopy (name, names[0], len + 1);
13096 XFree (name);
13097 }
13098 }
13099 else
13100 try_XLoadQueryFont = 0;
13101
13102 if (font)
13103 XFreeFont (dpy, font);
13104 }
13105
13106 if (!try_XLoadQueryFont)
13107 {
13108 /* We try at least 10 fonts because XListFonts will return
13109 auto-scaled fonts at the head. */
13110 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13111 &num_fonts);
13112 if (x_had_errors_p (dpy))
13113 {
13114 /* This error is perhaps due to insufficient memory on X
13115 server. Let's just ignore it. */
13116 names = NULL;
13117 x_clear_errors (dpy);
13118 }
13119 }
13120
13121 x_uncatch_errors (dpy, count);
13122 UNBLOCK_INPUT;
13123
13124 if (names)
13125 {
13126 int i;
13127
13128 /* Make a list of all the fonts we got back.
13129 Store that in the font cache for the display. */
13130 for (i = 0; i < num_fonts; i++)
13131 {
13132 int width = 0;
13133 char *p = names[i];
13134 int average_width = -1, dashes = 0;
13135
13136 /* Count the number of dashes in NAMES[I]. If there are
13137 14 dashes, and the field value following 12th dash
13138 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13139 is usually too ugly to be used for editing. Let's
13140 ignore it. */
13141 while (*p)
13142 if (*p++ == '-')
13143 {
13144 dashes++;
13145 if (dashes == 7) /* PIXEL_SIZE field */
13146 width = atoi (p);
13147 else if (dashes == 12) /* AVERAGE_WIDTH field */
13148 average_width = atoi (p);
13149 }
13150
13151 if (allow_scalable_fonts_p
13152 || dashes < 14 || average_width != 0)
13153 {
13154 tem = build_string (names[i]);
13155 if (NILP (Fassoc (tem, list)))
13156 {
13157 if (STRINGP (Vx_pixel_size_width_font_regexp)
13158 && ((fast_c_string_match_ignore_case
13159 (Vx_pixel_size_width_font_regexp, names[i]))
13160 >= 0))
13161 /* We can set the value of PIXEL_SIZE to the
13162 width of this font. */
13163 list = Fcons (Fcons (tem, make_number (width)), list);
13164 else
13165 /* For the moment, width is not known. */
13166 list = Fcons (Fcons (tem, Qnil), list);
13167 }
13168 }
13169 }
13170 if (!try_XLoadQueryFont)
13171 XFreeFontNames (names);
13172 }
13173
13174 /* Now store the result in the cache. */
13175 XCDR (dpyinfo->name_list_element)
13176 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13177
13178 label_cached:
13179 if (NILP (list)) continue; /* Try the remaining alternatives. */
13180
13181 newlist = second_best = Qnil;
13182 /* Make a list of the fonts that have the right width. */
13183 for (; CONSP (list); list = XCDR (list))
13184 {
13185 int found_size;
13186
13187 tem = XCAR (list);
13188
13189 if (!CONSP (tem) || NILP (XCAR (tem)))
13190 continue;
13191 if (!size)
13192 {
13193 newlist = Fcons (XCAR (tem), newlist);
13194 continue;
13195 }
13196
13197 if (!INTEGERP (XCDR (tem)))
13198 {
13199 /* Since we have not yet known the size of this font, we
13200 must try slow function call XLoadQueryFont. */
13201 XFontStruct *thisinfo;
13202
13203 BLOCK_INPUT;
13204 count = x_catch_errors (dpy);
13205 thisinfo = XLoadQueryFont (dpy,
13206 XSTRING (XCAR (tem))->data);
13207 if (x_had_errors_p (dpy))
13208 {
13209 /* This error is perhaps due to insufficient memory on X
13210 server. Let's just ignore it. */
13211 thisinfo = NULL;
13212 x_clear_errors (dpy);
13213 }
13214 x_uncatch_errors (dpy, count);
13215 UNBLOCK_INPUT;
13216
13217 if (thisinfo)
13218 {
13219 XCDR (tem)
13220 = (thisinfo->min_bounds.width == 0
13221 ? make_number (0)
13222 : make_number (thisinfo->max_bounds.width));
13223 XFreeFont (dpy, thisinfo);
13224 }
13225 else
13226 /* For unknown reason, the previous call of XListFont had
13227 returned a font which can't be opened. Record the size
13228 as 0 not to try to open it again. */
13229 XCDR (tem) = make_number (0);
13230 }
13231
13232 found_size = XINT (XCDR (tem));
13233 if (found_size == size)
13234 newlist = Fcons (XCAR (tem), newlist);
13235 else if (found_size > 0)
13236 {
13237 if (NILP (second_best))
13238 second_best = tem;
13239 else if (found_size < size)
13240 {
13241 if (XINT (XCDR (second_best)) > size
13242 || XINT (XCDR (second_best)) < found_size)
13243 second_best = tem;
13244 }
13245 else
13246 {
13247 if (XINT (XCDR (second_best)) > size
13248 && XINT (XCDR (second_best)) > found_size)
13249 second_best = tem;
13250 }
13251 }
13252 }
13253 if (!NILP (newlist))
13254 break;
13255 else if (!NILP (second_best))
13256 {
13257 newlist = Fcons (XCAR (second_best), Qnil);
13258 break;
13259 }
13260 }
13261
13262 return newlist;
13263 }
13264
13265
13266 #if GLYPH_DEBUG
13267
13268 /* Check that FONT is valid on frame F. It is if it can be found in F's
13269 font table. */
13270
13271 static void
13272 x_check_font (f, font)
13273 struct frame *f;
13274 XFontStruct *font;
13275 {
13276 int i;
13277 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13278
13279 xassert (font != NULL);
13280
13281 for (i = 0; i < dpyinfo->n_fonts; i++)
13282 if (dpyinfo->font_table[i].name
13283 && font == dpyinfo->font_table[i].font)
13284 break;
13285
13286 xassert (i < dpyinfo->n_fonts);
13287 }
13288
13289 #endif /* GLYPH_DEBUG != 0 */
13290
13291 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13292 Note: There are (broken) X fonts out there with invalid XFontStruct
13293 min_bounds contents. For example, handa@etl.go.jp reports that
13294 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13295 have font->min_bounds.width == 0. */
13296
13297 static INLINE void
13298 x_font_min_bounds (font, w, h)
13299 XFontStruct *font;
13300 int *w, *h;
13301 {
13302 *h = FONT_HEIGHT (font);
13303 *w = font->min_bounds.width;
13304
13305 /* Try to handle the case where FONT->min_bounds has invalid
13306 contents. Since the only font known to have invalid min_bounds
13307 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13308 if (*w <= 0)
13309 *w = font->max_bounds.width;
13310 }
13311
13312
13313 /* Compute the smallest character width and smallest font height over
13314 all fonts available on frame F. Set the members smallest_char_width
13315 and smallest_font_height in F's x_display_info structure to
13316 the values computed. Value is non-zero if smallest_font_height or
13317 smallest_char_width become smaller than they were before. */
13318
13319 static int
13320 x_compute_min_glyph_bounds (f)
13321 struct frame *f;
13322 {
13323 int i;
13324 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13325 XFontStruct *font;
13326 int old_width = dpyinfo->smallest_char_width;
13327 int old_height = dpyinfo->smallest_font_height;
13328
13329 dpyinfo->smallest_font_height = 100000;
13330 dpyinfo->smallest_char_width = 100000;
13331
13332 for (i = 0; i < dpyinfo->n_fonts; ++i)
13333 if (dpyinfo->font_table[i].name)
13334 {
13335 struct font_info *fontp = dpyinfo->font_table + i;
13336 int w, h;
13337
13338 font = (XFontStruct *) fontp->font;
13339 xassert (font != (XFontStruct *) ~0);
13340 x_font_min_bounds (font, &w, &h);
13341
13342 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13343 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13344 }
13345
13346 xassert (dpyinfo->smallest_char_width > 0
13347 && dpyinfo->smallest_font_height > 0);
13348
13349 return (dpyinfo->n_fonts == 1
13350 || dpyinfo->smallest_char_width < old_width
13351 || dpyinfo->smallest_font_height < old_height);
13352 }
13353
13354
13355 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13356 pointer to the structure font_info while allocating it dynamically.
13357 If SIZE is 0, load any size of font.
13358 If loading is failed, return NULL. */
13359
13360 struct font_info *
13361 x_load_font (f, fontname, size)
13362 struct frame *f;
13363 register char *fontname;
13364 int size;
13365 {
13366 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13367 Lisp_Object font_names;
13368 int count;
13369
13370 /* Get a list of all the fonts that match this name. Once we
13371 have a list of matching fonts, we compare them against the fonts
13372 we already have by comparing names. */
13373 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13374
13375 if (!NILP (font_names))
13376 {
13377 Lisp_Object tail;
13378 int i;
13379
13380 for (i = 0; i < dpyinfo->n_fonts; i++)
13381 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13382 if (dpyinfo->font_table[i].name
13383 && (!strcmp (dpyinfo->font_table[i].name,
13384 XSTRING (XCAR (tail))->data)
13385 || !strcmp (dpyinfo->font_table[i].full_name,
13386 XSTRING (XCAR (tail))->data)))
13387 return (dpyinfo->font_table + i);
13388 }
13389
13390 /* Load the font and add it to the table. */
13391 {
13392 char *full_name;
13393 XFontStruct *font;
13394 struct font_info *fontp;
13395 unsigned long value;
13396 int i;
13397
13398 /* If we have found fonts by x_list_font, load one of them. If
13399 not, we still try to load a font by the name given as FONTNAME
13400 because XListFonts (called in x_list_font) of some X server has
13401 a bug of not finding a font even if the font surely exists and
13402 is loadable by XLoadQueryFont. */
13403 if (size > 0 && !NILP (font_names))
13404 fontname = (char *) XSTRING (XCAR (font_names))->data;
13405
13406 BLOCK_INPUT;
13407 count = x_catch_errors (FRAME_X_DISPLAY (f));
13408 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13409 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13410 {
13411 /* This error is perhaps due to insufficient memory on X
13412 server. Let's just ignore it. */
13413 font = NULL;
13414 x_clear_errors (FRAME_X_DISPLAY (f));
13415 }
13416 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13417 UNBLOCK_INPUT;
13418 if (!font)
13419 return NULL;
13420
13421 /* Find a free slot in the font table. */
13422 for (i = 0; i < dpyinfo->n_fonts; ++i)
13423 if (dpyinfo->font_table[i].name == NULL)
13424 break;
13425
13426 /* If no free slot found, maybe enlarge the font table. */
13427 if (i == dpyinfo->n_fonts
13428 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13429 {
13430 int sz;
13431 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13432 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13433 dpyinfo->font_table
13434 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13435 }
13436
13437 fontp = dpyinfo->font_table + i;
13438 if (i == dpyinfo->n_fonts)
13439 ++dpyinfo->n_fonts;
13440
13441 /* Now fill in the slots of *FONTP. */
13442 BLOCK_INPUT;
13443 fontp->font = font;
13444 fontp->font_idx = i;
13445 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13446 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13447
13448 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13449 full_name = 0;
13450 if (XGetFontProperty (font, XA_FONT, &value))
13451 {
13452 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13453 char *p = name;
13454 int dashes = 0;
13455
13456 /* Count the number of dashes in the "full name".
13457 If it is too few, this isn't really the font's full name,
13458 so don't use it.
13459 In X11R4, the fonts did not come with their canonical names
13460 stored in them. */
13461 while (*p)
13462 {
13463 if (*p == '-')
13464 dashes++;
13465 p++;
13466 }
13467
13468 if (dashes >= 13)
13469 {
13470 full_name = (char *) xmalloc (p - name + 1);
13471 bcopy (name, full_name, p - name + 1);
13472 }
13473
13474 XFree (name);
13475 }
13476
13477 if (full_name != 0)
13478 fontp->full_name = full_name;
13479 else
13480 fontp->full_name = fontp->name;
13481
13482 fontp->size = font->max_bounds.width;
13483 fontp->height = FONT_HEIGHT (font);
13484
13485 if (NILP (font_names))
13486 {
13487 /* We come here because of a bug of XListFonts mentioned at
13488 the head of this block. Let's store this information in
13489 the cache for x_list_fonts. */
13490 Lisp_Object lispy_name = build_string (fontname);
13491 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13492 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13493 Qnil);
13494
13495 XCDR (dpyinfo->name_list_element)
13496 = Fcons (Fcons (key,
13497 Fcons (Fcons (lispy_full_name,
13498 make_number (fontp->size)),
13499 Qnil)),
13500 XCDR (dpyinfo->name_list_element));
13501 if (full_name)
13502 {
13503 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13504 Qnil);
13505 XCDR (dpyinfo->name_list_element)
13506 = Fcons (Fcons (key,
13507 Fcons (Fcons (lispy_full_name,
13508 make_number (fontp->size)),
13509 Qnil)),
13510 XCDR (dpyinfo->name_list_element));
13511 }
13512 }
13513
13514 /* The slot `encoding' specifies how to map a character
13515 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13516 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13517 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13518 2:0xA020..0xFF7F). For the moment, we don't know which charset
13519 uses this font. So, we set information in fontp->encoding[1]
13520 which is never used by any charset. If mapping can't be
13521 decided, set FONT_ENCODING_NOT_DECIDED. */
13522 fontp->encoding[1]
13523 = (font->max_byte1 == 0
13524 /* 1-byte font */
13525 ? (font->min_char_or_byte2 < 0x80
13526 ? (font->max_char_or_byte2 < 0x80
13527 ? 0 /* 0x20..0x7F */
13528 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13529 : 1) /* 0xA0..0xFF */
13530 /* 2-byte font */
13531 : (font->min_byte1 < 0x80
13532 ? (font->max_byte1 < 0x80
13533 ? (font->min_char_or_byte2 < 0x80
13534 ? (font->max_char_or_byte2 < 0x80
13535 ? 0 /* 0x2020..0x7F7F */
13536 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13537 : 3) /* 0x20A0..0x7FFF */
13538 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13539 : (font->min_char_or_byte2 < 0x80
13540 ? (font->max_char_or_byte2 < 0x80
13541 ? 2 /* 0xA020..0xFF7F */
13542 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13543 : 1))); /* 0xA0A0..0xFFFF */
13544
13545 fontp->baseline_offset
13546 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13547 ? (long) value : 0);
13548 fontp->relative_compose
13549 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13550 ? (long) value : 0);
13551 fontp->default_ascent
13552 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13553 ? (long) value : 0);
13554
13555 /* Set global flag fonts_changed_p to non-zero if the font loaded
13556 has a character with a smaller width than any other character
13557 before, or if the font loaded has a smalle>r height than any
13558 other font loaded before. If this happens, it will make a
13559 glyph matrix reallocation necessary. */
13560 fonts_changed_p = x_compute_min_glyph_bounds (f);
13561 UNBLOCK_INPUT;
13562 return fontp;
13563 }
13564 }
13565
13566
13567 /* Return a pointer to struct font_info of a font named FONTNAME for
13568 frame F. If no such font is loaded, return NULL. */
13569
13570 struct font_info *
13571 x_query_font (f, fontname)
13572 struct frame *f;
13573 register char *fontname;
13574 {
13575 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13576 int i;
13577
13578 for (i = 0; i < dpyinfo->n_fonts; i++)
13579 if (dpyinfo->font_table[i].name
13580 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13581 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13582 return (dpyinfo->font_table + i);
13583 return NULL;
13584 }
13585
13586
13587 /* Find a CCL program for a font specified by FONTP, and set the member
13588 `encoder' of the structure. */
13589
13590 void
13591 x_find_ccl_program (fontp)
13592 struct font_info *fontp;
13593 {
13594 Lisp_Object list, elt;
13595
13596 elt = Qnil;
13597 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13598 {
13599 elt = XCAR (list);
13600 if (CONSP (elt)
13601 && STRINGP (XCAR (elt))
13602 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13603 >= 0)
13604 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13605 >= 0)))
13606 break;
13607 }
13608
13609 if (! NILP (list))
13610 {
13611 struct ccl_program *ccl
13612 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13613
13614 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13615 xfree (ccl);
13616 else
13617 fontp->font_encoder = ccl;
13618 }
13619 }
13620
13621
13622 \f
13623 /***********************************************************************
13624 Initialization
13625 ***********************************************************************/
13626
13627 #ifdef USE_X_TOOLKIT
13628 static XrmOptionDescRec emacs_options[] = {
13629 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13630 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13631
13632 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13633 XrmoptionSepArg, NULL},
13634 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13635
13636 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13637 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13638 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13639 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13640 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13641 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13642 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13643 };
13644 #endif /* USE_X_TOOLKIT */
13645
13646 static int x_initialized;
13647
13648 #ifdef MULTI_KBOARD
13649 /* Test whether two display-name strings agree up to the dot that separates
13650 the screen number from the server number. */
13651 static int
13652 same_x_server (name1, name2)
13653 char *name1, *name2;
13654 {
13655 int seen_colon = 0;
13656 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13657 int system_name_length = strlen (system_name);
13658 int length_until_period = 0;
13659
13660 while (system_name[length_until_period] != 0
13661 && system_name[length_until_period] != '.')
13662 length_until_period++;
13663
13664 /* Treat `unix' like an empty host name. */
13665 if (! strncmp (name1, "unix:", 5))
13666 name1 += 4;
13667 if (! strncmp (name2, "unix:", 5))
13668 name2 += 4;
13669 /* Treat this host's name like an empty host name. */
13670 if (! strncmp (name1, system_name, system_name_length)
13671 && name1[system_name_length] == ':')
13672 name1 += system_name_length;
13673 if (! strncmp (name2, system_name, system_name_length)
13674 && name2[system_name_length] == ':')
13675 name2 += system_name_length;
13676 /* Treat this host's domainless name like an empty host name. */
13677 if (! strncmp (name1, system_name, length_until_period)
13678 && name1[length_until_period] == ':')
13679 name1 += length_until_period;
13680 if (! strncmp (name2, system_name, length_until_period)
13681 && name2[length_until_period] == ':')
13682 name2 += length_until_period;
13683
13684 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13685 {
13686 if (*name1 == ':')
13687 seen_colon++;
13688 if (seen_colon && *name1 == '.')
13689 return 1;
13690 }
13691 return (seen_colon
13692 && (*name1 == '.' || *name1 == '\0')
13693 && (*name2 == '.' || *name2 == '\0'));
13694 }
13695 #endif
13696
13697 struct x_display_info *
13698 x_term_init (display_name, xrm_option, resource_name)
13699 Lisp_Object display_name;
13700 char *xrm_option;
13701 char *resource_name;
13702 {
13703 int connection;
13704 Display *dpy;
13705 struct x_display_info *dpyinfo;
13706 XrmDatabase xrdb;
13707
13708 BLOCK_INPUT;
13709
13710 if (!x_initialized)
13711 {
13712 x_initialize ();
13713 x_initialized = 1;
13714 }
13715
13716 #ifdef USE_X_TOOLKIT
13717 /* weiner@footloose.sps.mot.com reports that this causes
13718 errors with X11R5:
13719 X protocol error: BadAtom (invalid Atom parameter)
13720 on protocol request 18skiloaf.
13721 So let's not use it until R6. */
13722 #ifdef HAVE_X11XTR6
13723 XtSetLanguageProc (NULL, NULL, NULL);
13724 #endif
13725
13726 {
13727 int argc = 0;
13728 char *argv[3];
13729
13730 argv[0] = "";
13731 argc = 1;
13732 if (xrm_option)
13733 {
13734 argv[argc++] = "-xrm";
13735 argv[argc++] = xrm_option;
13736 }
13737 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13738 resource_name, EMACS_CLASS,
13739 emacs_options, XtNumber (emacs_options),
13740 &argc, argv);
13741
13742 #ifdef HAVE_X11XTR6
13743 /* I think this is to compensate for XtSetLanguageProc. */
13744 fixup_locale ();
13745 #endif
13746 }
13747
13748 #else /* not USE_X_TOOLKIT */
13749 #ifdef HAVE_X11R5
13750 XSetLocaleModifiers ("");
13751 #endif
13752 dpy = XOpenDisplay (XSTRING (display_name)->data);
13753 #endif /* not USE_X_TOOLKIT */
13754
13755 /* Detect failure. */
13756 if (dpy == 0)
13757 {
13758 UNBLOCK_INPUT;
13759 return 0;
13760 }
13761
13762 /* We have definitely succeeded. Record the new connection. */
13763
13764 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13765 bzero (dpyinfo, sizeof *dpyinfo);
13766
13767 #ifdef MULTI_KBOARD
13768 {
13769 struct x_display_info *share;
13770 Lisp_Object tail;
13771
13772 for (share = x_display_list, tail = x_display_name_list; share;
13773 share = share->next, tail = XCDR (tail))
13774 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13775 XSTRING (display_name)->data))
13776 break;
13777 if (share)
13778 dpyinfo->kboard = share->kboard;
13779 else
13780 {
13781 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13782 init_kboard (dpyinfo->kboard);
13783 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13784 {
13785 char *vendor = ServerVendor (dpy);
13786 UNBLOCK_INPUT;
13787 dpyinfo->kboard->Vsystem_key_alist
13788 = call1 (Qvendor_specific_keysyms,
13789 build_string (vendor ? vendor : ""));
13790 BLOCK_INPUT;
13791 }
13792
13793 dpyinfo->kboard->next_kboard = all_kboards;
13794 all_kboards = dpyinfo->kboard;
13795 /* Don't let the initial kboard remain current longer than necessary.
13796 That would cause problems if a file loaded on startup tries to
13797 prompt in the mini-buffer. */
13798 if (current_kboard == initial_kboard)
13799 current_kboard = dpyinfo->kboard;
13800 }
13801 dpyinfo->kboard->reference_count++;
13802 }
13803 #endif
13804
13805 /* Put this display on the chain. */
13806 dpyinfo->next = x_display_list;
13807 x_display_list = dpyinfo;
13808
13809 /* Put it on x_display_name_list as well, to keep them parallel. */
13810 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13811 x_display_name_list);
13812 dpyinfo->name_list_element = XCAR (x_display_name_list);
13813
13814 dpyinfo->display = dpy;
13815
13816 #if 0
13817 XSetAfterFunction (x_current_display, x_trace_wire);
13818 #endif /* ! 0 */
13819
13820 dpyinfo->x_id_name
13821 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13822 + STRING_BYTES (XSTRING (Vsystem_name))
13823 + 2);
13824 sprintf (dpyinfo->x_id_name, "%s@%s",
13825 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13826
13827 /* Figure out which modifier bits mean what. */
13828 x_find_modifier_meanings (dpyinfo);
13829
13830 /* Get the scroll bar cursor. */
13831 dpyinfo->vertical_scroll_bar_cursor
13832 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13833
13834 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13835 resource_name, EMACS_CLASS);
13836 #ifdef HAVE_XRMSETDATABASE
13837 XrmSetDatabase (dpyinfo->display, xrdb);
13838 #else
13839 dpyinfo->display->db = xrdb;
13840 #endif
13841 /* Put the rdb where we can find it in a way that works on
13842 all versions. */
13843 dpyinfo->xrdb = xrdb;
13844
13845 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13846 DefaultScreen (dpyinfo->display));
13847 select_visual (dpyinfo);
13848 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13849 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13850 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13851 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13852 dpyinfo->grabbed = 0;
13853 dpyinfo->reference_count = 0;
13854 dpyinfo->icon_bitmap_id = -1;
13855 dpyinfo->font_table = NULL;
13856 dpyinfo->n_fonts = 0;
13857 dpyinfo->font_table_size = 0;
13858 dpyinfo->bitmaps = 0;
13859 dpyinfo->bitmaps_size = 0;
13860 dpyinfo->bitmaps_last = 0;
13861 dpyinfo->scratch_cursor_gc = 0;
13862 dpyinfo->mouse_face_mouse_frame = 0;
13863 dpyinfo->mouse_face_deferred_gc = 0;
13864 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13865 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13866 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13867 dpyinfo->mouse_face_window = Qnil;
13868 dpyinfo->mouse_face_overlay = Qnil;
13869 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13870 dpyinfo->mouse_face_defer = 0;
13871 dpyinfo->x_focus_frame = 0;
13872 dpyinfo->x_focus_event_frame = 0;
13873 dpyinfo->x_highlight_frame = 0;
13874 dpyinfo->image_cache = make_image_cache ();
13875
13876 /* See if a private colormap is requested. */
13877 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13878 {
13879 if (dpyinfo->visual->class == PseudoColor)
13880 {
13881 Lisp_Object value;
13882 value = display_x_get_resource (dpyinfo,
13883 build_string ("privateColormap"),
13884 build_string ("PrivateColormap"),
13885 Qnil, Qnil);
13886 if (STRINGP (value)
13887 && (!strcmp (XSTRING (value)->data, "true")
13888 || !strcmp (XSTRING (value)->data, "on")))
13889 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13890 }
13891 }
13892 else
13893 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13894 dpyinfo->visual, AllocNone);
13895
13896 {
13897 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13898 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13899 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13900 dpyinfo->resy = pixels * 25.4 / mm;
13901 pixels = DisplayWidth (dpyinfo->display, screen_number);
13902 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13903 dpyinfo->resx = pixels * 25.4 / mm;
13904 }
13905
13906 dpyinfo->Xatom_wm_protocols
13907 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13908 dpyinfo->Xatom_wm_take_focus
13909 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13910 dpyinfo->Xatom_wm_save_yourself
13911 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13912 dpyinfo->Xatom_wm_delete_window
13913 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13914 dpyinfo->Xatom_wm_change_state
13915 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13916 dpyinfo->Xatom_wm_configure_denied
13917 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13918 dpyinfo->Xatom_wm_window_moved
13919 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13920 dpyinfo->Xatom_editres
13921 = XInternAtom (dpyinfo->display, "Editres", False);
13922 dpyinfo->Xatom_CLIPBOARD
13923 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13924 dpyinfo->Xatom_TIMESTAMP
13925 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13926 dpyinfo->Xatom_TEXT
13927 = XInternAtom (dpyinfo->display, "TEXT", False);
13928 dpyinfo->Xatom_COMPOUND_TEXT
13929 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13930 dpyinfo->Xatom_DELETE
13931 = XInternAtom (dpyinfo->display, "DELETE", False);
13932 dpyinfo->Xatom_MULTIPLE
13933 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13934 dpyinfo->Xatom_INCR
13935 = XInternAtom (dpyinfo->display, "INCR", False);
13936 dpyinfo->Xatom_EMACS_TMP
13937 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13938 dpyinfo->Xatom_TARGETS
13939 = XInternAtom (dpyinfo->display, "TARGETS", False);
13940 dpyinfo->Xatom_NULL
13941 = XInternAtom (dpyinfo->display, "NULL", False);
13942 dpyinfo->Xatom_ATOM_PAIR
13943 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13944 /* For properties of font. */
13945 dpyinfo->Xatom_PIXEL_SIZE
13946 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13947 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13948 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13949 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13950 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13951 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13952 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13953
13954 /* Ghostscript support. */
13955 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13956 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13957
13958 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13959 False);
13960
13961 dpyinfo->cut_buffers_initialized = 0;
13962
13963 connection = ConnectionNumber (dpyinfo->display);
13964 dpyinfo->connection = connection;
13965
13966 {
13967 char null_bits[1];
13968
13969 null_bits[0] = 0x00;
13970
13971 dpyinfo->null_pixel
13972 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13973 null_bits, 1, 1, (long) 0, (long) 0,
13974 1);
13975 }
13976
13977 {
13978 extern int gray_bitmap_width, gray_bitmap_height;
13979 extern char *gray_bitmap_bits;
13980 dpyinfo->gray
13981 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13982 gray_bitmap_bits,
13983 gray_bitmap_width, gray_bitmap_height,
13984 (unsigned long) 1, (unsigned long) 0, 1);
13985 }
13986
13987 #ifdef HAVE_X_I18N
13988 xim_initialize (dpyinfo, resource_name);
13989 #endif
13990
13991 #ifdef subprocesses
13992 /* This is only needed for distinguishing keyboard and process input. */
13993 if (connection != 0)
13994 add_keyboard_wait_descriptor (connection);
13995 #endif
13996
13997 #ifndef F_SETOWN_BUG
13998 #ifdef F_SETOWN
13999 #ifdef F_SETOWN_SOCK_NEG
14000 /* stdin is a socket here */
14001 fcntl (connection, F_SETOWN, -getpid ());
14002 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14003 fcntl (connection, F_SETOWN, getpid ());
14004 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14005 #endif /* ! defined (F_SETOWN) */
14006 #endif /* F_SETOWN_BUG */
14007
14008 #ifdef SIGIO
14009 if (interrupt_input)
14010 init_sigio (connection);
14011 #endif /* ! defined (SIGIO) */
14012
14013 #ifdef USE_LUCID
14014 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14015 /* Make sure that we have a valid font for dialog boxes
14016 so that Xt does not crash. */
14017 {
14018 Display *dpy = dpyinfo->display;
14019 XrmValue d, fr, to;
14020 Font font;
14021 int count;
14022
14023 d.addr = (XPointer)&dpy;
14024 d.size = sizeof (Display *);
14025 fr.addr = XtDefaultFont;
14026 fr.size = sizeof (XtDefaultFont);
14027 to.size = sizeof (Font *);
14028 to.addr = (XPointer)&font;
14029 count = x_catch_errors (dpy);
14030 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14031 abort ();
14032 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14033 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14034 x_uncatch_errors (dpy, count);
14035 }
14036 #endif
14037 #endif
14038
14039 /* See if we should run in synchronous mode. This is useful
14040 for debugging X code. */
14041 {
14042 Lisp_Object value;
14043 value = display_x_get_resource (dpyinfo,
14044 build_string ("synchronous"),
14045 build_string ("Synchronous"),
14046 Qnil, Qnil);
14047 if (STRINGP (value)
14048 && (!strcmp (XSTRING (value)->data, "true")
14049 || !strcmp (XSTRING (value)->data, "on")))
14050 XSynchronize (dpyinfo->display, True);
14051 }
14052
14053 UNBLOCK_INPUT;
14054
14055 return dpyinfo;
14056 }
14057 \f
14058 /* Get rid of display DPYINFO, assuming all frames are already gone,
14059 and without sending any more commands to the X server. */
14060
14061 void
14062 x_delete_display (dpyinfo)
14063 struct x_display_info *dpyinfo;
14064 {
14065 delete_keyboard_wait_descriptor (dpyinfo->connection);
14066
14067 /* Discard this display from x_display_name_list and x_display_list.
14068 We can't use Fdelq because that can quit. */
14069 if (! NILP (x_display_name_list)
14070 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14071 x_display_name_list = XCDR (x_display_name_list);
14072 else
14073 {
14074 Lisp_Object tail;
14075
14076 tail = x_display_name_list;
14077 while (CONSP (tail) && CONSP (XCDR (tail)))
14078 {
14079 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14080 {
14081 XCDR (tail) = XCDR (XCDR (tail));
14082 break;
14083 }
14084 tail = XCDR (tail);
14085 }
14086 }
14087
14088 if (next_noop_dpyinfo == dpyinfo)
14089 next_noop_dpyinfo = dpyinfo->next;
14090
14091 if (x_display_list == dpyinfo)
14092 x_display_list = dpyinfo->next;
14093 else
14094 {
14095 struct x_display_info *tail;
14096
14097 for (tail = x_display_list; tail; tail = tail->next)
14098 if (tail->next == dpyinfo)
14099 tail->next = tail->next->next;
14100 }
14101
14102 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14103 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14104 XrmDestroyDatabase (dpyinfo->xrdb);
14105 #endif
14106 #endif
14107 #ifdef MULTI_KBOARD
14108 if (--dpyinfo->kboard->reference_count == 0)
14109 delete_kboard (dpyinfo->kboard);
14110 #endif
14111 #ifdef HAVE_X_I18N
14112 if (dpyinfo->xim)
14113 xim_close_dpy (dpyinfo);
14114 #endif
14115
14116 xfree (dpyinfo->font_table);
14117 xfree (dpyinfo->x_id_name);
14118 xfree (dpyinfo->color_cells);
14119 xfree (dpyinfo);
14120 }
14121
14122 \f
14123 /* Set up use of X before we make the first connection. */
14124
14125 static struct redisplay_interface x_redisplay_interface =
14126 {
14127 x_produce_glyphs,
14128 x_write_glyphs,
14129 x_insert_glyphs,
14130 x_clear_end_of_line,
14131 x_scroll_run,
14132 x_after_update_window_line,
14133 x_update_window_begin,
14134 x_update_window_end,
14135 XTcursor_to,
14136 x_flush,
14137 x_clear_mouse_face,
14138 x_get_glyph_overhangs,
14139 x_fix_overlapping_area
14140 };
14141
14142 void
14143 x_initialize ()
14144 {
14145 rif = &x_redisplay_interface;
14146
14147 clear_frame_hook = x_clear_frame;
14148 ins_del_lines_hook = x_ins_del_lines;
14149 change_line_highlight_hook = x_change_line_highlight;
14150 delete_glyphs_hook = x_delete_glyphs;
14151 ring_bell_hook = XTring_bell;
14152 reset_terminal_modes_hook = XTreset_terminal_modes;
14153 set_terminal_modes_hook = XTset_terminal_modes;
14154 update_begin_hook = x_update_begin;
14155 update_end_hook = x_update_end;
14156 set_terminal_window_hook = XTset_terminal_window;
14157 read_socket_hook = XTread_socket;
14158 frame_up_to_date_hook = XTframe_up_to_date;
14159 reassert_line_highlight_hook = XTreassert_line_highlight;
14160 mouse_position_hook = XTmouse_position;
14161 frame_rehighlight_hook = XTframe_rehighlight;
14162 frame_raise_lower_hook = XTframe_raise_lower;
14163 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14164 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14165 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14166 judge_scroll_bars_hook = XTjudge_scroll_bars;
14167 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14168
14169 scroll_region_ok = 1; /* we'll scroll partial frames */
14170 char_ins_del_ok = 1;
14171 line_ins_del_ok = 1; /* we'll just blt 'em */
14172 fast_clear_end_of_line = 1; /* X does this well */
14173 memory_below_frame = 0; /* we don't remember what scrolls
14174 off the bottom */
14175 baud_rate = 19200;
14176
14177 x_noop_count = 0;
14178 last_tool_bar_item = -1;
14179 any_help_event_p = 0;
14180
14181 /* Try to use interrupt input; if we can't, then start polling. */
14182 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14183
14184 #ifdef USE_X_TOOLKIT
14185 XtToolkitInitialize ();
14186 Xt_app_con = XtCreateApplicationContext ();
14187 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14188
14189 /* Install an asynchronous timer that processes Xt timeout events
14190 every 0.1s. This is necessary because some widget sets use
14191 timeouts internally, for example the LessTif menu bar, or the
14192 Xaw3d scroll bar. When Xt timouts aren't processed, these
14193 widgets don't behave normally. */
14194 {
14195 EMACS_TIME interval;
14196 EMACS_SET_SECS_USECS (interval, 0, 100000);
14197 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14198 }
14199 #endif
14200
14201 #ifdef USE_TOOLKIT_SCROLL_BARS
14202 xaw3d_arrow_scroll = False;
14203 xaw3d_pick_top = True;
14204 #endif
14205
14206 /* Note that there is no real way portable across R3/R4 to get the
14207 original error handler. */
14208 XSetErrorHandler (x_error_handler);
14209 XSetIOErrorHandler (x_io_error_quitter);
14210
14211 /* Disable Window Change signals; they are handled by X events. */
14212 #ifdef SIGWINCH
14213 signal (SIGWINCH, SIG_DFL);
14214 #endif /* ! defined (SIGWINCH) */
14215
14216 signal (SIGPIPE, x_connection_signal);
14217 }
14218
14219
14220 void
14221 syms_of_xterm ()
14222 {
14223 staticpro (&x_error_message_string);
14224 x_error_message_string = Qnil;
14225
14226 staticpro (&x_display_name_list);
14227 x_display_name_list = Qnil;
14228
14229 staticpro (&last_mouse_scroll_bar);
14230 last_mouse_scroll_bar = Qnil;
14231
14232 staticpro (&Qvendor_specific_keysyms);
14233 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14234
14235 staticpro (&last_mouse_press_frame);
14236 last_mouse_press_frame = Qnil;
14237
14238 help_echo = Qnil;
14239 staticpro (&help_echo);
14240 help_echo_object = Qnil;
14241 staticpro (&help_echo_object);
14242 help_echo_window = Qnil;
14243 staticpro (&help_echo_window);
14244 previous_help_echo = Qnil;
14245 staticpro (&previous_help_echo);
14246 help_echo_pos = -1;
14247
14248 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14249 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14250 For example, if a block cursor is over a tab, it will be drawn as\n\
14251 wide as that tab on the display.");
14252 x_stretch_cursor_p = 0;
14253
14254 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14255 "What X toolkit scroll bars Emacs uses.\n\
14256 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14257 Otherwise, value is a symbol describing the X toolkit.");
14258 #ifdef USE_TOOLKIT_SCROLL_BARS
14259 #ifdef USE_MOTIF
14260 Vx_toolkit_scroll_bars = intern ("motif");
14261 #elif defined HAVE_XAW3D
14262 Vx_toolkit_scroll_bars = intern ("xaw3d");
14263 #else
14264 Vx_toolkit_scroll_bars = intern ("xaw");
14265 #endif
14266 #else
14267 Vx_toolkit_scroll_bars = Qnil;
14268 #endif
14269
14270 staticpro (&last_mouse_motion_frame);
14271 last_mouse_motion_frame = Qnil;
14272 }
14273
14274 #endif /* not HAVE_X_WINDOWS */