99a935b260933ee96c34d21da0fd67ee9c464c85
[bpt/emacs.git] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999
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 /* On 4.3 these lose if they come after xterm.h. */
26 /* On HP-UX 8.0 signal.h loses if it comes after config.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
29
30 #include <config.h>
31
32 #include <stdio.h>
33 #ifdef STDC_HEADERS
34 #include <stdlib.h>
35 #endif
36
37 #ifdef HAVE_X_WINDOWS
38
39 #include "lisp.h"
40 #include "blockinput.h"
41
42 /* Need syssignal.h for various externs and definitions that may be required
43 by some configurations for calls to signal later in this source file. */
44 #include "syssignal.h"
45
46 /* This may include sys/types.h, and that somehow loses
47 if this is not done before the other system files. */
48 #include "xterm.h"
49 #include <X11/cursorfont.h>
50
51 #ifndef USG
52 /* Load sys/types.h if not already loaded.
53 In some systems loading it twice is suicidal. */
54 #ifndef makedev
55 #include <sys/types.h>
56 #endif /* makedev */
57 #endif /* USG */
58
59 #ifdef BSD_SYSTEM
60 #include <sys/ioctl.h>
61 #endif /* ! defined (BSD_SYSTEM) */
62
63 #include "systty.h"
64 #include "systime.h"
65
66 #ifndef INCLUDED_FCNTL
67 #include <fcntl.h>
68 #endif
69 #include <ctype.h>
70 #include <errno.h>
71 #include <setjmp.h>
72 #include <sys/stat.h>
73 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
74 /* #include <sys/param.h> */
75
76 #include "charset.h"
77 #include "ccl.h"
78 #include "frame.h"
79 #include "fontset.h"
80 #include "dispextern.h"
81 #include "termhooks.h"
82 #include "termopts.h"
83 #include "termchar.h"
84 #if 0
85 #include "sink.h"
86 #include "sinkmask.h"
87 #endif /* ! 0 */
88 #include "gnu.h"
89 #include "disptab.h"
90 #include "buffer.h"
91 #include "window.h"
92 #include "keyboard.h"
93 #include "intervals.h"
94 #include "process.h"
95
96 #ifdef USE_X_TOOLKIT
97 #include <X11/Shell.h>
98 #endif
99
100 #include <sys/types.h>
101 #ifdef HAVE_SYS_TIME_H
102 #include <sys/time.h>
103 #endif
104 #ifdef HAVE_UNISTD_H
105 #include <unistd.h>
106 #endif
107
108 #ifdef USE_X_TOOLKIT
109
110 extern void free_frame_menubar ();
111 extern FRAME_PTR x_menubar_window_to_frame ();
112
113 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
114 #define HACK_EDITRES
115 extern void _XEditResCheckMessages ();
116 #endif /* not NO_EDITRES */
117
118 /* Include toolkit specific headers for the scroll bar widget. */
119
120 #ifdef USE_TOOLKIT_SCROLL_BARS
121 #if defined USE_MOTIF
122 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
123 #include <Xm/ScrollBar.h>
124 #include <Xm/ScrollBarP.h>
125 #elif defined HAVE_XAW3D
126 #include <X11/Xaw3d/Simple.h>
127 #include <X11/Xaw3d/ThreeD.h>
128 #include <X11/Xaw3d/Scrollbar.h>
129 #define ARROW_SCROLLBAR
130 #include <X11/Xaw3d/ScrollbarP.h>
131 #endif /* HAVE_XAW3D */
132 #endif /* USE_TOOLKIT_SCROLL_BARS */
133
134 #endif /* USE_X_TOOLKIT */
135
136 #ifndef USE_X_TOOLKIT
137 #define x_any_window_to_frame x_window_to_frame
138 #define x_top_window_to_frame x_window_to_frame
139 #endif
140
141 #ifdef USE_X_TOOLKIT
142 #include "widget.h"
143 #ifndef XtNinitialState
144 #define XtNinitialState "initialState"
145 #endif
146 #endif
147
148 #ifdef HAVE_SETLOCALE
149 /* So we can do setlocale. */
150 #include <locale.h>
151 #endif
152
153 #ifdef SOLARIS2
154 /* memmove will be defined as a macro in Xfuncs.h unless
155 <string.h> is included beforehand. The declaration for memmove in
156 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
157 #include <string.h>
158 #endif
159
160 #ifndef min
161 #define min(a,b) ((a) < (b) ? (a) : (b))
162 #endif
163 #ifndef max
164 #define max(a,b) ((a) > (b) ? (a) : (b))
165 #endif
166
167 #define abs(x) ((x) < 0 ? -(x) : (x))
168
169 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
170
171 \f
172 /* Bitmaps for truncated lines. */
173
174 enum bitmap_type
175 {
176 NO_BITMAP,
177 LEFT_TRUNCATION_BITMAP,
178 RIGHT_TRUNCATION_BITMAP,
179 OVERLAY_ARROW_BITMAP,
180 CONTINUED_LINE_BITMAP,
181 CONTINUATION_LINE_BITMAP,
182 ZV_LINE_BITMAP
183 };
184
185 /* Bitmap drawn to indicate lines not displaying text if
186 `indicate-empty-lines' is non-nil. */
187
188 #define zv_width 8
189 #define zv_height 8
190 static unsigned char zv_bits[] = {
191 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
192
193 /* An arrow like this: `<-'. */
194
195 #define left_width 8
196 #define left_height 8
197 static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
199
200 /* Right truncation arrow bitmap `->'. */
201
202 #define right_width 8
203 #define right_height 8
204 static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
206
207 /* Marker for continued lines. */
208
209 #define continued_width 8
210 #define continued_height 8
211 static unsigned char continued_bits[] = {
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
213
214 /* Marker for continuation lines. */
215
216 #define continuation_width 8
217 #define continuation_height 8
218 static unsigned char continuation_bits[] = {
219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
220
221 /* Overlay arrow bitmap. */
222
223 #if 0
224 /* A bomb. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
229 #else
230 /* A triangular arrow. */
231 #define ov_width 8
232 #define ov_height 8
233 static unsigned char ov_bits[] = {
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
235
236 #endif
237
238 extern Lisp_Object Qhelp_echo;
239
240 \f
241 /* Non-zero means Emacs uses toolkit scroll bars. */
242
243 int x_toolkit_scroll_bars_p;
244
245 /* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
247
248 static Lisp_Object help_echo;
249
250 /* Temporary variable for XTread_socket. */
251
252 static Lisp_Object previous_help_echo;
253
254 /* Non-zero means that a HELP_EVENT has been generated since Emacs
255 start. */
256
257 static int any_help_event_p;
258
259 /* Non-zero means draw block and hollow cursor as wide as the glyph
260 under it. For example, if a block cursor is over a tab, it will be
261 drawn as wide as that tab on the display. */
262
263 int x_stretch_cursor_p;
264
265 /* This is a chain of structures for all the X displays currently in
266 use. */
267
268 struct x_display_info *x_display_list;
269
270 /* This is a list of cons cells, each of the form (NAME
271 . FONT-LIST-CACHE), one for each element of x_display_list and in
272 the same order. NAME is the name of the frame. FONT-LIST-CACHE
273 records previous values returned by x-list-fonts. */
274
275 Lisp_Object x_display_name_list;
276
277 /* Frame being updated by update_frame. This is declared in term.c.
278 This is set by update_begin and looked at by all the XT functions.
279 It is zero while not inside an update. In that case, the XT
280 functions assume that `selected_frame' is the frame to apply to. */
281
282 extern struct frame *updating_frame;
283
284 extern int waiting_for_input;
285
286 /* This is a frame waiting to be auto-raised, within XTread_socket. */
287
288 struct frame *pending_autoraise_frame;
289
290 #ifdef USE_X_TOOLKIT
291 /* The application context for Xt use. */
292 XtAppContext Xt_app_con;
293 static String Xt_default_resources[] = {0};
294 #endif /* USE_X_TOOLKIT */
295
296 /* Nominal cursor position -- where to draw output.
297 HPOS and VPOS are window relative glyph matrix coordinates.
298 X and Y are window relative pixel coordinates. */
299
300 struct cursor_pos output_cursor;
301
302
303 /* Mouse movement.
304
305 Formerly, we used PointerMotionHintMask (in standard_event_mask)
306 so that we would have to call XQueryPointer after each MotionNotify
307 event to ask for another such event. However, this made mouse tracking
308 slow, and there was a bug that made it eventually stop.
309
310 Simply asking for MotionNotify all the time seems to work better.
311
312 In order to avoid asking for motion events and then throwing most
313 of them away or busy-polling the server for mouse positions, we ask
314 the server for pointer motion hints. This means that we get only
315 one event per group of mouse movements. "Groups" are delimited by
316 other kinds of events (focus changes and button clicks, for
317 example), or by XQueryPointer calls; when one of these happens, we
318 get another MotionNotify event the next time the mouse moves. This
319 is at least as efficient as getting motion events when mouse
320 tracking is on, and I suspect only negligibly worse when tracking
321 is off. */
322
323 /* Where the mouse was last time we reported a mouse event. */
324
325 FRAME_PTR last_mouse_frame;
326 static XRectangle last_mouse_glyph;
327 static Lisp_Object last_mouse_press_frame;
328
329 /* The scroll bar in which the last X motion event occurred.
330
331 If the last X motion event occurred in a scroll bar, we set this so
332 XTmouse_position can know whether to report a scroll bar motion or
333 an ordinary motion.
334
335 If the last X motion event didn't occur in a scroll bar, we set
336 this to Qnil, to tell XTmouse_position to return an ordinary motion
337 event. */
338
339 static Lisp_Object last_mouse_scroll_bar;
340
341 /* This is a hack. We would really prefer that XTmouse_position would
342 return the time associated with the position it returns, but there
343 doesn't seem to be any way to wrest the time-stamp from the server
344 along with the position query. So, we just keep track of the time
345 of the last movement we received, and return that in hopes that
346 it's somewhat accurate. */
347
348 static Time last_mouse_movement_time;
349
350 /* Incremented by XTread_socket whenever it really tries to read
351 events. */
352
353 #ifdef __STDC__
354 static int volatile input_signal_count;
355 #else
356 static int input_signal_count;
357 #endif
358
359 /* Used locally within XTread_socket. */
360
361 static int x_noop_count;
362
363 /* Initial values of argv and argc. */
364
365 extern char **initial_argv;
366 extern int initial_argc;
367
368 extern Lisp_Object Vcommand_line_args, Vsystem_name;
369
370 /* Tells if a window manager is present or not. */
371
372 extern Lisp_Object Vx_no_window_manager;
373
374 extern Lisp_Object Qface, Qmouse_face;
375
376 extern int errno;
377
378 /* A mask of extra modifier bits to put into every keyboard char. */
379
380 extern int extra_keyboard_modifiers;
381
382 static Lisp_Object Qvendor_specific_keysyms;
383
384 extern XrmDatabase x_load_resources ();
385 extern Lisp_Object x_icon_type ();
386
387
388 /* Enumeration for overriding/changing the face to use for drawing
389 glyphs in x_draw_glyphs. */
390
391 enum draw_glyphs_face
392 {
393 DRAW_NORMAL_TEXT,
394 DRAW_INVERSE_VIDEO,
395 DRAW_CURSOR,
396 DRAW_MOUSE_FACE,
397 DRAW_IMAGE_RAISED,
398 DRAW_IMAGE_SUNKEN
399 };
400
401 static void x_update_window_end P_ ((struct window *, int));
402 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403 void x_delete_display P_ ((struct x_display_info *));
404 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406 static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
408 static void set_output_cursor P_ ((struct cursor_pos *));
409 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
410 int *, int *, int *));
411 static void note_mode_line_highlight P_ ((struct window *, int, int));
412 static void x_check_font P_ ((struct frame *, XFontStruct *));
413 static void note_mouse_highlight P_ ((struct frame *, int, int));
414 static void note_toolbar_highlight P_ ((struct frame *f, int, int));
415 static void x_handle_toolbar_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 *));
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
476
477 /* Flush display of frame F, or of all frames if F is null. */
478
479 static void
480 x_flush (f)
481 struct frame *f;
482 {
483 BLOCK_INPUT;
484 if (f == NULL)
485 {
486 Lisp_Object rest, frame;
487 FOR_EACH_FRAME (rest, frame)
488 x_flush (XFRAME (frame));
489 }
490 else if (FRAME_X_P (f))
491 XFlush (FRAME_X_DISPLAY (f));
492 UNBLOCK_INPUT;
493 }
494
495
496 /* Remove calls to XFlush by defining XFlush to an empty replacement.
497 Calls to XFlush should be unnecessary because the X output buffer
498 is flushed automatically as needed by calls to XPending,
499 XNextEvent, or XWindowEvent according to the XFlush man page.
500 XTread_socket calls XPending. Removing XFlush improves
501 performance. */
502
503 #define XFlush(DISPLAY) (void) 0
504
505 \f
506 /***********************************************************************
507 Debugging
508 ***********************************************************************/
509
510 #if 0
511
512 /* This is a function useful for recording debugging information about
513 the sequence of occurrences in this file. */
514
515 struct record
516 {
517 char *locus;
518 int type;
519 };
520
521 struct record event_record[100];
522
523 int event_record_index;
524
525 record_event (locus, type)
526 char *locus;
527 int type;
528 {
529 if (event_record_index == sizeof (event_record) / sizeof (struct record))
530 event_record_index = 0;
531
532 event_record[event_record_index].locus = locus;
533 event_record[event_record_index].type = type;
534 event_record_index++;
535 }
536
537 #endif /* 0 */
538
539
540 \f
541 /* Return the struct x_display_info corresponding to DPY. */
542
543 struct x_display_info *
544 x_display_info_for_display (dpy)
545 Display *dpy;
546 {
547 struct x_display_info *dpyinfo;
548
549 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
550 if (dpyinfo->display == dpy)
551 return dpyinfo;
552
553 return 0;
554 }
555
556
557 \f
558 /***********************************************************************
559 Starting and ending an update
560 ***********************************************************************/
561
562 /* Start an update of frame F. This function is installed as a hook
563 for update_begin, i.e. it is called when update_begin is called.
564 This function is called prior to calls to x_update_window_begin for
565 each window being updated. Currently, there is nothing to do here
566 because all interesting stuff is done on a window basis. */
567
568 static void
569 x_update_begin (f)
570 struct frame *f;
571 {
572 /* Nothing to do. */
573 }
574
575
576 /* Start update of window W. Set the global variable updated_window
577 to the window being updated and set output_cursor to the cursor
578 position of W. */
579
580 static void
581 x_update_window_begin (w)
582 struct window *w;
583 {
584 struct frame *f = XFRAME (WINDOW_FRAME (w));
585 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
586
587 updated_window = w;
588 set_output_cursor (&w->cursor);
589
590 BLOCK_INPUT;
591
592 if (f == display_info->mouse_face_mouse_frame)
593 {
594 /* Don't do highlighting for mouse motion during the update. */
595 display_info->mouse_face_defer = 1;
596
597 /* If F needs to be redrawn, simply forget about any prior mouse
598 highlighting. */
599 if (FRAME_GARBAGED_P (f))
600 display_info->mouse_face_window = Qnil;
601
602 /* Can we tell that this update does not affect the window
603 where the mouse highlight is? If so, no need to turn off.
604 Likewise, don't do anything if the frame is garbaged;
605 in that case, the frame's current matrix that we would use
606 is all wrong, and we will redisplay that line anyway. */
607 if (!NILP (display_info->mouse_face_window)
608 && w == XWINDOW (display_info->mouse_face_window))
609 {
610 int i;
611
612 for (i = 0; i < w->desired_matrix->nrows; ++i)
613 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
614 break;
615
616 if (i < w->desired_matrix->nrows)
617 clear_mouse_face (display_info);
618 }
619 }
620
621 UNBLOCK_INPUT;
622 }
623
624
625 /* Draw a vertical window border to the right of window W if W doesn't
626 have vertical scroll bars. */
627
628 static void
629 x_draw_vertical_border (w)
630 struct window *w;
631 {
632 struct frame *f = XFRAME (WINDOW_FRAME (w));
633
634 /* Redraw borders between horizontally adjacent windows. Don't
635 do it for frames with vertical scroll bars because either the
636 right scroll bar of a window, or the left scroll bar of its
637 neighbor will suffice as a border. */
638 if (!WINDOW_RIGHTMOST_P (w)
639 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
640 {
641 int x0, x1, y0, y1;
642
643 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
644 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
645 y1 -= 1;
646
647 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
648 f->output_data.x->normal_gc, x1, y0, x1, y1);
649 }
650 }
651
652
653 /* End update of window W (which is equal to updated_window). Draw
654 vertical borders between horizontally adjacent windows, and display
655 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
656 pseudo-window in case we don't have X toolkit support. Such
657 windows don't have a cursor, so don't display it here. */
658
659 static void
660 x_update_window_end (w, cursor_on_p)
661 struct window *w;
662 int cursor_on_p;
663 {
664 if (!w->pseudo_window_p)
665 {
666 BLOCK_INPUT;
667 if (cursor_on_p)
668 x_display_and_set_cursor (w, 1, output_cursor.hpos,
669 output_cursor.vpos,
670 output_cursor.x, output_cursor.y);
671 x_draw_vertical_border (w);
672 UNBLOCK_INPUT;
673 }
674
675 updated_window = NULL;
676 }
677
678
679 /* End update of frame F. This function is installed as a hook in
680 update_end. */
681
682 static void
683 x_update_end (f)
684 struct frame *f;
685 {
686 /* Mouse highlight may be displayed again. */
687 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
688
689 BLOCK_INPUT;
690 XFlush (FRAME_X_DISPLAY (f));
691 UNBLOCK_INPUT;
692 }
693
694
695 /* This function is called from various places in xdisp.c whenever a
696 complete update has been performed. The global variable
697 updated_window is not available here. */
698
699 static void
700 XTframe_up_to_date (f)
701 struct frame *f;
702 {
703 if (FRAME_X_P (f))
704 {
705 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
706 if (dpyinfo->mouse_face_deferred_gc
707 || f == dpyinfo->mouse_face_mouse_frame)
708 {
709 BLOCK_INPUT;
710 if (dpyinfo->mouse_face_mouse_frame)
711 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
712 dpyinfo->mouse_face_mouse_x,
713 dpyinfo->mouse_face_mouse_y);
714 dpyinfo->mouse_face_deferred_gc = 0;
715 UNBLOCK_INPUT;
716 }
717 }
718 }
719
720
721 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
722 arrow bitmaps, or clear the areas where they would be displayed
723 before DESIRED_ROW is made current. The window being updated is
724 found in updated_window. This function It is called from
725 update_window_line only if it is known that there are differences
726 between bitmaps to be drawn between current row and DESIRED_ROW. */
727
728 static void
729 x_after_update_window_line (desired_row)
730 struct glyph_row *desired_row;
731 {
732 struct window *w = updated_window;
733
734 xassert (w);
735
736 if (!desired_row->mode_line_p && !w->pseudo_window_p)
737 {
738 BLOCK_INPUT;
739 x_draw_row_bitmaps (w, desired_row);
740
741 /* When a window has disappeared, make sure that no rest of
742 full-width rows stays visible in the internal border. */
743 if (windows_or_buffers_changed)
744 {
745 struct frame *f = XFRAME (w->frame);
746 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
747 int height = desired_row->visible_height;
748 int x = (window_box_right (w, -1)
749 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
750 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
751
752 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
753 x, y, width, height, False);
754 }
755
756 UNBLOCK_INPUT;
757 }
758 }
759
760
761 /* Draw the bitmap WHICH in one of the areas to the left or right of
762 window W. ROW is the glyph row for which to display the bitmap; it
763 determines the vertical position at which the bitmap has to be
764 drawn. */
765
766 static void
767 x_draw_bitmap (w, row, which)
768 struct window *w;
769 struct glyph_row *row;
770 enum bitmap_type which;
771 {
772 struct frame *f = XFRAME (WINDOW_FRAME (w));
773 Display *display = FRAME_X_DISPLAY (f);
774 Window window = FRAME_X_WINDOW (f);
775 int x, y, wd, h, dy;
776 unsigned char *bits;
777 Pixmap pixmap;
778 GC gc = f->output_data.x->normal_gc;
779 struct face *face;
780 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
781
782 /* Must clip because of partially visible lines. */
783 x_clip_to_row (w, row, gc, 1);
784
785 switch (which)
786 {
787 case LEFT_TRUNCATION_BITMAP:
788 wd = left_width;
789 h = left_height;
790 bits = left_bits;
791 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
792 - wd
793 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
794 break;
795
796 case OVERLAY_ARROW_BITMAP:
797 wd = left_width;
798 h = left_height;
799 bits = ov_bits;
800 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
801 - wd
802 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
803 break;
804
805 case RIGHT_TRUNCATION_BITMAP:
806 wd = right_width;
807 h = right_height;
808 bits = right_bits;
809 x = window_box_right (w, -1);
810 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
811 break;
812
813 case CONTINUED_LINE_BITMAP:
814 wd = right_width;
815 h = right_height;
816 bits = continued_bits;
817 x = window_box_right (w, -1);
818 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
819 break;
820
821 case CONTINUATION_LINE_BITMAP:
822 wd = continuation_width;
823 h = continuation_height;
824 bits = continuation_bits;
825 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
826 - wd
827 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
828 break;
829
830 case ZV_LINE_BITMAP:
831 wd = zv_width;
832 h = zv_height;
833 bits = zv_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
837 break;
838
839 default:
840 abort ();
841 }
842
843 /* Convert to frame coordinates. Set dy to the offset in the row to
844 start drawing the bitmap. */
845 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
846 dy = (row->height - h) / 2;
847
848 /* Draw the bitmap. I believe these small pixmaps can be cached
849 by the server. */
850 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
851 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
852 face->foreground,
853 face->background, depth);
854 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
855 XFreePixmap (display, pixmap);
856 XSetClipMask (display, gc, None);
857 }
858
859
860 /* Draw flags bitmaps for glyph row ROW on window W. Call this
861 function with input blocked. */
862
863 static void
864 x_draw_row_bitmaps (w, row)
865 struct window *w;
866 struct glyph_row *row;
867 {
868 struct frame *f = XFRAME (w->frame);
869 enum bitmap_type bitmap;
870 struct face *face;
871 int top_line_height = -1;
872
873 xassert (interrupt_input_blocked);
874
875 /* If row is completely invisible, because of vscrolling, we
876 don't have to draw anything. */
877 if (row->visible_height <= 0)
878 return;
879
880 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
881 PREPARE_FACE_FOR_DISPLAY (f, face);
882
883 /* Decide which bitmap to draw at the left side. */
884 if (row->overlay_arrow_p)
885 bitmap = OVERLAY_ARROW_BITMAP;
886 else if (row->truncated_on_left_p)
887 bitmap = LEFT_TRUNCATION_BITMAP;
888 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
889 bitmap = CONTINUATION_LINE_BITMAP;
890 else if (row->indicate_empty_line_p)
891 bitmap = ZV_LINE_BITMAP;
892 else
893 bitmap = NO_BITMAP;
894
895 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
896 the flags area. */
897 if (bitmap == NO_BITMAP
898 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
899 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
900 {
901 /* If W has a vertical border to its left, don't draw over it. */
902 int border = ((XFASTINT (w->left) > 0
903 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
904 ? 1 : 0);
905 int left = window_box_left (w, -1);
906
907 if (top_line_height < 0)
908 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
909
910 /* In case the same realized face is used for bitmap areas and
911 for something displayed in the text (e.g. face `region' on
912 mono-displays, the fill style may have been changed to
913 FillSolid in x_draw_glyph_string_background. */
914 if (face->stipple)
915 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
916 else
917 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
918
919 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
920 face->gc,
921 (left
922 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
923 + border),
924 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
925 row->y)),
926 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
927 row->visible_height);
928 if (!face->stipple)
929 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
930 }
931
932 /* Draw the left bitmap. */
933 if (bitmap != NO_BITMAP)
934 x_draw_bitmap (w, row, bitmap);
935
936 /* Decide which bitmap to draw at the right side. */
937 if (row->truncated_on_right_p)
938 bitmap = RIGHT_TRUNCATION_BITMAP;
939 else if (row->continued_p)
940 bitmap = CONTINUED_LINE_BITMAP;
941 else
942 bitmap = NO_BITMAP;
943
944 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
945 the flags area. */
946 if (bitmap == NO_BITMAP
947 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
948 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
949 {
950 int right = window_box_right (w, -1);
951
952 if (top_line_height < 0)
953 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
954
955 /* In case the same realized face is used for bitmap areas and
956 for something displayed in the text (e.g. face `region' on
957 mono-displays, the fill style may have been changed to
958 FillSolid in x_draw_glyph_string_background. */
959 if (face->stipple)
960 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
961 else
962 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
963 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
964 face->gc,
965 right,
966 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
967 row->y)),
968 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
969 row->visible_height);
970 if (!face->stipple)
971 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
972 }
973
974 /* Draw the right bitmap. */
975 if (bitmap != NO_BITMAP)
976 x_draw_bitmap (w, row, bitmap);
977 }
978
979 \f
980 /***********************************************************************
981 Line Highlighting
982 ***********************************************************************/
983
984 /* External interface to control of standout mode. Not used for X
985 frames. Aborts when called. */
986
987 static void
988 XTreassert_line_highlight (new, vpos)
989 int new, vpos;
990 {
991 abort ();
992 }
993
994
995 /* Call this when about to modify line at position VPOS and change
996 whether it is highlighted. Not used for X frames. Aborts when
997 called. */
998
999 static void
1000 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1001 int new_highlight, vpos, y, first_unused_hpos;
1002 {
1003 abort ();
1004 }
1005
1006
1007 /* This is called when starting Emacs and when restarting after
1008 suspend. When starting Emacs, no X window is mapped. And nothing
1009 must be done to Emacs's own window if it is suspended (though that
1010 rarely happens). */
1011
1012 static void
1013 XTset_terminal_modes ()
1014 {
1015 }
1016
1017 /* This is called when exiting or suspending Emacs. Exiting will make
1018 the X-windows go away, and suspending requires no action. */
1019
1020 static void
1021 XTreset_terminal_modes ()
1022 {
1023 }
1024
1025
1026 \f
1027 /***********************************************************************
1028 Output Cursor
1029 ***********************************************************************/
1030
1031 /* Set the global variable output_cursor to CURSOR. All cursor
1032 positions are relative to updated_window. */
1033
1034 static void
1035 set_output_cursor (cursor)
1036 struct cursor_pos *cursor;
1037 {
1038 output_cursor.hpos = cursor->hpos;
1039 output_cursor.vpos = cursor->vpos;
1040 output_cursor.x = cursor->x;
1041 output_cursor.y = cursor->y;
1042 }
1043
1044
1045 /* Set a nominal cursor position.
1046
1047 HPOS and VPOS are column/row positions in a window glyph matrix. X
1048 and Y are window text area relative pixel positions.
1049
1050 If this is done during an update, updated_window will contain the
1051 window that is being updated and the position is the future output
1052 cursor position for that window. If updated_window is null, use
1053 selected_window and display the cursor at the given position. */
1054
1055 static void
1056 XTcursor_to (vpos, hpos, y, x)
1057 int vpos, hpos, y, x;
1058 {
1059 struct window *w;
1060
1061 /* If updated_window is not set, work on selected_window. */
1062 if (updated_window)
1063 w = updated_window;
1064 else
1065 w = XWINDOW (selected_window);
1066
1067 /* Set the output cursor. */
1068 output_cursor.hpos = hpos;
1069 output_cursor.vpos = vpos;
1070 output_cursor.x = x;
1071 output_cursor.y = y;
1072
1073 /* If not called as part of an update, really display the cursor.
1074 This will also set the cursor position of W. */
1075 if (updated_window == NULL)
1076 {
1077 BLOCK_INPUT;
1078 x_display_cursor (w, 1, hpos, vpos, x, y);
1079 XFlush (FRAME_X_DISPLAY (selected_frame));
1080 UNBLOCK_INPUT;
1081 }
1082 }
1083
1084
1085 \f
1086 /***********************************************************************
1087 Display Iterator
1088 ***********************************************************************/
1089
1090 /* Function prototypes of this page. */
1091
1092 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1093 struct glyph *,
1094 XChar2b *));
1095 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1096 int, XChar2b *, int));
1097 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1098 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1099 static void x_append_glyph P_ ((struct it *));
1100 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1101 int, int, double));
1102 static void x_produce_glyphs P_ ((struct it *));
1103 static void x_produce_special_glyphs P_ ((struct it *, enum display_element_type));
1104 static void x_produce_image_glyph P_ ((struct it *it));
1105
1106
1107 /* Return a pointer to per-char metric information in FONT of a
1108 character pointed by B which is a pointer to an XChar2b. */
1109
1110 #define PER_CHAR_METRIC(font, b) \
1111 ((font)->per_char \
1112 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1113 + (((font)->min_byte1 || (font)->max_byte1) \
1114 ? (((b)->byte1 - (font)->min_byte1) \
1115 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1116 : 0)) \
1117 : &((font)->max_bounds))
1118
1119
1120 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
1121 If CHAR2B is not contained in FONT, the font's default character
1122 metric is returned. */
1123
1124 static INLINE XCharStruct *
1125 x_per_char_metric (font, char2b)
1126 XFontStruct *font;
1127 XChar2b *char2b;
1128 {
1129 /* The result metric information. */
1130 XCharStruct *pcm = NULL;
1131
1132 xassert (font && char2b);
1133
1134 if (font->per_char != NULL)
1135 {
1136 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1137 {
1138 /* min_char_or_byte2 specifies the linear character index
1139 corresponding to the first element of the per_char array,
1140 max_char_or_byte2 is the index of the last character. A
1141 character with non-zero CHAR2B->byte1 is not in the font.
1142 A character with byte2 less than min_char_or_byte2 or
1143 greater max_char_or_byte2 is not in the font. */
1144 if (char2b->byte1 == 0
1145 && char2b->byte2 >= font->min_char_or_byte2
1146 && char2b->byte2 <= font->max_char_or_byte2)
1147 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1148 }
1149 else
1150 {
1151 /* If either min_byte1 or max_byte1 are nonzero, both
1152 min_char_or_byte2 and max_char_or_byte2 are less than
1153 256, and the 2-byte character index values corresponding
1154 to the per_char array element N (counting from 0) are:
1155
1156 byte1 = N/D + min_byte1
1157 byte2 = N\D + min_char_or_byte2
1158
1159 where:
1160
1161 D = max_char_or_byte2 - min_char_or_byte2 + 1
1162 / = integer division
1163 \ = integer modulus */
1164 if (char2b->byte1 >= font->min_byte1
1165 && char2b->byte1 <= font->max_byte1
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 {
1169 pcm = (font->per_char
1170 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1171 * (char2b->byte1 - font->min_byte1))
1172 + (char2b->byte2 - font->min_char_or_byte2));
1173 }
1174 }
1175 }
1176 else
1177 {
1178 /* If the per_char pointer is null, all glyphs between the first
1179 and last character indexes inclusive have the same
1180 information, as given by both min_bounds and max_bounds. */
1181 if (char2b->byte2 >= font->min_char_or_byte2
1182 && char2b->byte2 <= font->max_char_or_byte2)
1183 pcm = &font->max_bounds;
1184 }
1185
1186
1187 if (pcm == NULL || pcm->width == 0)
1188 {
1189 /* Character not contained in the font. FONT->default_char
1190 gives the character that will be printed. FONT->default_char
1191 is a 16-bit character code with byte1 in the most significant
1192 byte and byte2 in the least significant byte. */
1193 XChar2b default_char;
1194 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1195 default_char.byte2 = font->default_char & 0xff;
1196
1197 /* Avoid an endless recursion if FONT->default_char itself
1198 hasn't per char metrics. handa@etl.go.jp reports that some
1199 fonts have this problem. */
1200 if (default_char.byte1 != char2b->byte1
1201 || default_char.byte2 != char2b->byte2)
1202 pcm = x_per_char_metric (font, &default_char);
1203 else
1204 pcm = &font->max_bounds;
1205 }
1206
1207 return pcm;
1208 }
1209
1210
1211 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1212 the two-byte form of C. Encoding is returned in *CHAR2B. */
1213
1214 static INLINE void
1215 x_encode_char (c, char2b, font_info)
1216 int c;
1217 XChar2b *char2b;
1218 struct font_info *font_info;
1219 {
1220 int charset = CHAR_CHARSET (c);
1221 XFontStruct *font = font_info->font;
1222
1223 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1224 This may be either a program in a special encoder language or a
1225 fixed encoding. */
1226 if (font_info->font_encoder)
1227 {
1228 /* It's a program. */
1229 struct ccl_program *ccl = font_info->font_encoder;
1230
1231 if (CHARSET_DIMENSION (charset) == 1)
1232 {
1233 ccl->reg[0] = charset;
1234 ccl->reg[1] = char2b->byte2;
1235 }
1236 else
1237 {
1238 ccl->reg[0] = charset;
1239 ccl->reg[1] = char2b->byte1;
1240 ccl->reg[2] = char2b->byte2;
1241 }
1242
1243 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1244
1245 /* We assume that MSBs are appropriately set/reset by CCL
1246 program. */
1247 if (font->max_byte1 == 0) /* 1-byte font */
1248 char2b->byte2 = ccl->reg[1];
1249 else
1250 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1251 }
1252 else if (font_info->encoding[charset])
1253 {
1254 /* Fixed encoding scheme. See fontset.h for the meaning of the
1255 encoding numbers. */
1256 int enc = font_info->encoding[charset];
1257
1258 if ((enc == 1 || enc == 2)
1259 && CHARSET_DIMENSION (charset) == 2)
1260 char2b->byte1 |= 0x80;
1261
1262 if (enc == 1 || enc == 3)
1263 char2b->byte2 |= 0x80;
1264 }
1265 }
1266
1267
1268 /* Get face and two-byte form of character C in face FACE_ID on frame
1269 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1270 means we want to display multibyte text. Value is a pointer to a
1271 realized face that is ready for display. */
1272
1273 static INLINE struct face *
1274 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1275 struct frame *f;
1276 int c, face_id;
1277 XChar2b *char2b;
1278 int multibyte_p;
1279 {
1280 struct face *face = FACE_FROM_ID (f, face_id);
1281
1282 if (!multibyte_p)
1283 {
1284 /* Unibyte case. We don't have to encode, but we have to make
1285 sure to use a face suitable for unibyte. */
1286 char2b->byte1 = 0;
1287 char2b->byte2 = c;
1288
1289 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1290 {
1291 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1292 face = FACE_FROM_ID (f, face_id);
1293 }
1294 }
1295 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1296 {
1297 /* Case of ASCII in a face known to fit ASCII. */
1298 char2b->byte1 = 0;
1299 char2b->byte2 = c;
1300 }
1301 else
1302 {
1303 int c1, c2, charset;
1304
1305 /* Split characters into bytes. If c2 is -1 afterwards, C is
1306 really a one-byte character so that byte1 is zero. */
1307 SPLIT_CHAR (c, charset, c1, c2);
1308 if (c2 > 0)
1309 char2b->byte1 = c1, char2b->byte2 = c2;
1310 else
1311 char2b->byte1 = 0, char2b->byte2 = c1;
1312
1313 /* Get the face for displaying C. If `face' is not suitable for
1314 charset, get the one that fits. (This can happen for the
1315 translations of composite characters where the glyph
1316 specifies a face for ASCII, but translations have a different
1317 charset.) */
1318 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1319 {
1320 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1321 face = FACE_FROM_ID (f, face_id);
1322 }
1323
1324 /* Maybe encode the character in *CHAR2B. */
1325 if (charset != CHARSET_ASCII)
1326 {
1327 struct font_info *font_info
1328 = FONT_INFO_FROM_ID (f, face->font_info_id);
1329 if (font_info)
1330 {
1331 x_encode_char (c, char2b, font_info);
1332 if (charset == charset_latin_iso8859_1)
1333 {
1334 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1335 >= 0x80);
1336 char2b->byte2 |= 0x80;
1337 }
1338 }
1339 }
1340 }
1341
1342 /* Make sure X resources of the face are allocated. */
1343 xassert (face != NULL);
1344 PREPARE_FACE_FOR_DISPLAY (f, face);
1345
1346 return face;
1347 }
1348
1349
1350 /* Get face and two-byte form of character glyph GLYPH on frame F.
1351 The encoding of GLYPH->u.ch.code is returned in *CHAR2B. Value is
1352 a pointer to a realized face that is ready for display. */
1353
1354 static INLINE struct face *
1355 x_get_glyph_face_and_encoding (f, glyph, char2b)
1356 struct frame *f;
1357 struct glyph *glyph;
1358 XChar2b *char2b;
1359 {
1360 struct face *face;
1361
1362 xassert (glyph->type == CHAR_GLYPH);
1363 face = FACE_FROM_ID (f, glyph->u.ch.face_id);
1364
1365 if (!glyph->multibyte_p)
1366 {
1367 /* Unibyte case. We don't have to encode, but we have to make
1368 sure to use a face suitable for unibyte. */
1369 char2b->byte1 = 0;
1370 char2b->byte2 = glyph->u.ch.code;
1371 }
1372 else if (glyph->u.ch.code < 128
1373 && glyph->u.ch.face_id < BASIC_FACE_ID_SENTINEL)
1374 {
1375 /* Case of ASCII in a face known to fit ASCII. */
1376 char2b->byte1 = 0;
1377 char2b->byte2 = glyph->u.ch.code;
1378 }
1379 else
1380 {
1381 int c1, c2, charset;
1382
1383 /* Split characters into bytes. If c2 is -1 afterwards, C is
1384 really a one-byte character so that byte1 is zero. */
1385 SPLIT_CHAR (glyph->u.ch.code, charset, c1, c2);
1386 if (c2 > 0)
1387 char2b->byte1 = c1, char2b->byte2 = c2;
1388 else
1389 char2b->byte1 = 0, char2b->byte2 = c1;
1390
1391 /* Maybe encode the character in *CHAR2B. */
1392 if (charset != CHARSET_ASCII)
1393 {
1394 struct font_info *font_info
1395 = FONT_INFO_FROM_ID (f, face->font_info_id);
1396 if (font_info)
1397 {
1398 x_encode_char (glyph->u.ch.code, char2b, font_info);
1399 if (charset == charset_latin_iso8859_1)
1400 char2b->byte2 |= 0x80;
1401 }
1402 }
1403 }
1404
1405 /* Make sure X resources of the face are allocated. */
1406 xassert (face != NULL);
1407 PREPARE_FACE_FOR_DISPLAY (f, face);
1408 return face;
1409 }
1410
1411
1412 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1413 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1414
1415 static INLINE void
1416 x_append_glyph (it)
1417 struct it *it;
1418 {
1419 struct glyph *glyph;
1420 enum glyph_row_area area = it->area;
1421
1422 xassert (it->glyph_row);
1423 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1424
1425 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1426 if (glyph < it->glyph_row->glyphs[area + 1])
1427 {
1428 /* Play it safe. If sub-structures of the glyph are not all the
1429 same size, it otherwise be that some bits stay set. This
1430 would prevent a comparison with GLYPH_EQUAL_P. */
1431 glyph->u.val = 0;
1432
1433 glyph->type = CHAR_GLYPH;
1434 glyph->pixel_width = it->pixel_width;
1435 glyph->u.ch.code = it->char_to_display;
1436 glyph->u.ch.face_id = it->face_id;
1437 glyph->charpos = CHARPOS (it->position);
1438 glyph->object = it->object;
1439 glyph->left_box_line_p = it->start_of_box_run_p;
1440 glyph->right_box_line_p = it->end_of_box_run_p;
1441 glyph->voffset = it->voffset;
1442 glyph->multibyte_p = it->multibyte_p;
1443 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1444 || it->phys_descent > it->descent);
1445 ++it->glyph_row->used[area];
1446 }
1447 }
1448
1449
1450 /* Change IT->ascent and IT->height according to the setting of
1451 IT->voffset. */
1452
1453 static INLINE void
1454 take_vertical_position_into_account (it)
1455 struct it *it;
1456 {
1457 if (it->voffset)
1458 {
1459 if (it->voffset < 0)
1460 /* Increase the ascent so that we can display the text higher
1461 in the line. */
1462 it->ascent += abs (it->voffset);
1463 else
1464 /* Increase the descent so that we can display the text lower
1465 in the line. */
1466 it->descent += it->voffset;
1467 }
1468 }
1469
1470
1471 /* Produce glyphs/get display metrics for the image IT is loaded with.
1472 See the description of struct display_iterator in dispextern.h for
1473 an overview of struct display_iterator. */
1474
1475 static void
1476 x_produce_image_glyph (it)
1477 struct it *it;
1478 {
1479 struct image *img;
1480 struct face *face;
1481
1482 xassert (it->what == IT_IMAGE);
1483
1484 face = FACE_FROM_ID (it->f, it->face_id);
1485 img = IMAGE_FROM_ID (it->f, it->image_id);
1486 xassert (img);
1487
1488 /* Make sure X resources of the face and image are loaded. */
1489 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1490 prepare_image_for_display (it->f, img);
1491
1492 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1493 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1494 it->pixel_width = img->width + 2 * img->margin;
1495
1496 it->nglyphs = 1;
1497
1498 if (face->box != FACE_NO_BOX)
1499 {
1500 it->ascent += face->box_line_width;
1501 it->descent += face->box_line_width;
1502
1503 if (it->start_of_box_run_p)
1504 it->pixel_width += face->box_line_width;
1505 if (it->end_of_box_run_p)
1506 it->pixel_width += face->box_line_width;
1507 }
1508
1509 take_vertical_position_into_account (it);
1510
1511 if (it->glyph_row)
1512 {
1513 struct glyph *glyph;
1514 enum glyph_row_area area = it->area;
1515
1516 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1517 if (glyph < it->glyph_row->glyphs[area + 1])
1518 {
1519 glyph->type = IMAGE_GLYPH;
1520 glyph->u.img.id = img->id;
1521 glyph->u.img.face_id = it->face_id;
1522 glyph->pixel_width = it->pixel_width;
1523 glyph->charpos = CHARPOS (it->position);
1524 glyph->object = it->object;
1525 glyph->left_box_line_p = it->start_of_box_run_p;
1526 glyph->right_box_line_p = it->end_of_box_run_p;
1527 glyph->voffset = it->voffset;
1528 glyph->multibyte_p = it->multibyte_p;
1529 ++it->glyph_row->used[area];
1530 }
1531 }
1532 }
1533
1534
1535 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1536 of the glyph, WIDTH and HEIGHT are the width and height of the
1537 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1538 ascent of the glyph (0 <= ASCENT <= 1). */
1539
1540 static void
1541 x_append_stretch_glyph (it, object, width, height, ascent)
1542 struct it *it;
1543 Lisp_Object object;
1544 int width, height;
1545 double ascent;
1546 {
1547 struct glyph *glyph;
1548 enum glyph_row_area area = it->area;
1549
1550 xassert (ascent >= 0 && ascent <= 1);
1551
1552 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1553 if (glyph < it->glyph_row->glyphs[area + 1])
1554 {
1555 glyph->type = STRETCH_GLYPH;
1556 glyph->u.stretch.ascent = height * ascent;
1557 glyph->u.stretch.height = height;
1558 glyph->u.stretch.face_id = it->face_id;
1559 glyph->pixel_width = width;
1560 glyph->charpos = CHARPOS (it->position);
1561 glyph->object = object;
1562 glyph->left_box_line_p = it->start_of_box_run_p;
1563 glyph->right_box_line_p = it->end_of_box_run_p;
1564 glyph->voffset = it->voffset;
1565 glyph->multibyte_p = it->multibyte_p;
1566 ++it->glyph_row->used[area];
1567 }
1568 }
1569
1570
1571 /* Produce a stretch glyph for iterator IT. IT->object is the value
1572 of the glyph property displayed. The value must be a list
1573 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1574 being recognized:
1575
1576 1. `:width WIDTH' specifies that the space should be WIDTH *
1577 canonical char width wide. WIDTH may be an integer or floating
1578 point number.
1579
1580 2. `:relative-width FACTOR' specifies that the width of the stretch
1581 should be computed from the width of the first character having the
1582 `glyph' property, and should be FACTOR times that width.
1583
1584 3. `:align-to HPOS' specifies that the space should be wide enough
1585 to reach HPOS, a value in canonical character units.
1586
1587 Exactly one of the above pairs must be present.
1588
1589 4. `:height HEIGHT' specifies that the height of the stretch produced
1590 should be HEIGHT, measured in canonical character units.
1591
1592 5. `:relative-height FACTOR' specifies that the height of the the
1593 stretch should be FACTOR times the height of the characters having
1594 the glyph property.
1595
1596 Either none or exactly one of 4 or 5 must be present.
1597
1598 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1599 of the stretch should be used for the ascent of the stretch.
1600 ASCENT must be in the range 0 <= ASCENT <= 100. */
1601
1602 #define NUMVAL(X) \
1603 ((INTEGERP (X) || FLOATP (X)) \
1604 ? XFLOATINT (X) \
1605 : - 1)
1606
1607
1608 static void
1609 x_produce_stretch_glyph (it)
1610 struct it *it;
1611 {
1612 /* (space :width WIDTH :height HEIGHT. */
1613 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1614 extern Lisp_Object QCrelative_width, QCrelative_height;
1615 extern Lisp_Object QCalign_to;
1616 Lisp_Object prop, plist;
1617 double width = 0, height = 0, ascent = 0;
1618 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1619 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1620
1621 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1622
1623 /* List should start with `space'. */
1624 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1625 plist = XCDR (it->object);
1626
1627 /* Compute the width of the stretch. */
1628 if (prop = Fplist_get (plist, QCwidth),
1629 NUMVAL (prop) > 0)
1630 /* Absolute width `:width WIDTH' specified and valid. */
1631 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1632 else if (prop = Fplist_get (plist, QCrelative_width),
1633 NUMVAL (prop) > 0)
1634 {
1635 /* Relative width `:relative-width FACTOR' specified and valid.
1636 Compute the width of the characters having the `glyph'
1637 property. */
1638 struct it it2;
1639 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1640
1641 it2 = *it;
1642 if (it->multibyte_p)
1643 {
1644 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1645 - IT_BYTEPOS (*it));
1646 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1647 }
1648 else
1649 it2.c = *p, it2.len = 1;
1650
1651 it2.glyph_row = NULL;
1652 it2.what = IT_CHARACTER;
1653 x_produce_glyphs (&it2);
1654 width = NUMVAL (prop) * it2.pixel_width;
1655 }
1656 else if (prop = Fplist_get (plist, QCalign_to),
1657 NUMVAL (prop) > 0)
1658 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1659 else
1660 /* Nothing specified -> width defaults to canonical char width. */
1661 width = CANON_X_UNIT (it->f);
1662
1663 /* Compute height. */
1664 if (prop = Fplist_get (plist, QCheight),
1665 NUMVAL (prop) > 0)
1666 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_height),
1668 NUMVAL (prop) > 0)
1669 height = FONT_HEIGHT (font) * NUMVAL (prop);
1670 else
1671 height = FONT_HEIGHT (font);
1672
1673 /* Compute percentage of height used for ascent. If
1674 `:ascent ASCENT' is present and valid, use that. Otherwise,
1675 derive the ascent from the font in use. */
1676 if (prop = Fplist_get (plist, QCascent),
1677 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1678 ascent = NUMVAL (prop) / 100.0;
1679 else
1680 ascent = (double) font->ascent / FONT_HEIGHT (font);
1681
1682 if (width <= 0)
1683 width = 1;
1684 if (height <= 0)
1685 height = 1;
1686
1687 if (it->glyph_row)
1688 {
1689 Lisp_Object object = it->stack[it->sp - 1].string;
1690 if (!STRINGP (object))
1691 object = it->w->buffer;
1692 x_append_stretch_glyph (it, object, width, height, ascent);
1693 }
1694
1695 it->pixel_width = width;
1696 it->ascent = it->phys_ascent = height * ascent;
1697 it->descent = it->phys_descent = height - it->ascent;
1698 it->nglyphs = 1;
1699
1700 if (face->box != FACE_NO_BOX)
1701 {
1702 it->ascent += face->box_line_width;
1703 it->descent += face->box_line_width;
1704
1705 if (it->start_of_box_run_p)
1706 it->pixel_width += face->box_line_width;
1707 if (it->end_of_box_run_p)
1708 it->pixel_width += face->box_line_width;
1709 }
1710
1711 take_vertical_position_into_account (it);
1712 }
1713
1714
1715 /* Produce glyphs/get display metrics for the display element IT is
1716 loaded with. See the description of struct display_iterator in
1717 dispextern.h for an overview of struct display_iterator. */
1718
1719 static void
1720 x_produce_glyphs (it)
1721 struct it *it;
1722 {
1723 if (it->what == IT_CHARACTER)
1724 {
1725 XChar2b char2b;
1726 XFontStruct *font;
1727 struct face *face;
1728 XCharStruct *pcm;
1729 struct it ci;
1730 int font_not_found_p;
1731 int c;
1732
1733 /* Maybe translate single-byte characters to multibyte. */
1734 it->char_to_display = it->c;
1735 if (unibyte_display_via_language_environment
1736 && SINGLE_BYTE_CHAR_P (it->c)
1737 && (it->c >= 0240
1738 || (it->c >= 0200
1739 && !NILP (Vnonascii_translation_table))))
1740 {
1741 it->char_to_display = unibyte_char_to_multibyte (it->c);
1742 it->charset = CHAR_CHARSET (it->char_to_display);
1743 }
1744
1745 /* Get face and font to use. Encode IT->char_to_display. */
1746 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1747 it->face_id, &char2b,
1748 it->multibyte_p);
1749 font = face->font;
1750
1751 /* When no suitable font found, use the default font. */
1752 font_not_found_p = font == NULL;
1753 if (font_not_found_p)
1754 font = FRAME_FONT (it->f);
1755
1756 if (it->char_to_display >= ' '
1757 && (!it->multibyte_p || it->char_to_display < 128))
1758 {
1759 /* Either unibyte or ASCII. */
1760 int stretched_p;
1761
1762 it->nglyphs = 1;
1763
1764 pcm = x_per_char_metric (font, &char2b);
1765 it->ascent = font->ascent;
1766 it->descent = font->descent;
1767 it->phys_ascent = pcm->ascent;
1768 it->phys_descent = pcm->descent;
1769 it->pixel_width = pcm->width;
1770
1771 /* If this is a space inside a region of text with
1772 `space-width' property, change its width. */
1773 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1774 if (stretched_p)
1775 it->pixel_width *= XFLOATINT (it->space_width);
1776
1777 /* If face has a box, add the box thickness to the character
1778 height. If character has a box line to the left and/or
1779 right, add the box line width to the character's width. */
1780 if (face->box != FACE_NO_BOX)
1781 {
1782 int thick = face->box_line_width;
1783
1784 it->ascent += thick;
1785 it->descent += thick;
1786
1787 if (it->start_of_box_run_p)
1788 it->pixel_width += thick;
1789 if (it->end_of_box_run_p)
1790 it->pixel_width += thick;
1791 }
1792
1793 /* If face has an overline, add the height of the overline
1794 (1 pixel) and a 1 pixel margin to the character height. */
1795 if (face->overline_p)
1796 it->ascent += 2;
1797
1798 take_vertical_position_into_account (it);
1799
1800 /* If we have to actually produce glyphs, do it. */
1801 if (it->glyph_row)
1802 {
1803 if (stretched_p)
1804 {
1805 /* Translate a space with a `space-width' property
1806 into a stretch glyph. */
1807 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1808 x_append_stretch_glyph (it, it->object, it->pixel_width,
1809 it->ascent + it->descent, ascent);
1810 }
1811 else
1812 x_append_glyph (it);
1813
1814 /* If characters with lbearing or rbearing are displayed
1815 in this line, record that fact in a flag of the
1816 glyph row. This is used to optimize X output code. */
1817 if (pcm->lbearing < 0
1818 || pcm->rbearing > pcm->width)
1819 it->glyph_row->contains_overlapping_glyphs_p = 1;
1820 }
1821 }
1822 else if (it->char_to_display == '\n')
1823 {
1824 /* A newline has no width but we need the height of the line. */
1825 it->pixel_width = 0;
1826 it->nglyphs = 0;
1827 it->ascent = it->phys_ascent = font->ascent;
1828 it->descent = it->phys_descent = font->descent;
1829
1830 if (face->box != FACE_NO_BOX)
1831 {
1832 int thick = face->box_line_width;
1833 it->ascent += thick;
1834 it->descent += thick;
1835 }
1836 }
1837 else if (it->char_to_display == '\t')
1838 {
1839 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1840 int x = (it->current_x
1841 - it->prompt_width
1842 + it->continuation_lines_width);
1843 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1844
1845 it->pixel_width = next_tab_x - x;
1846 it->nglyphs = 1;
1847 it->ascent = it->phys_ascent = font->ascent;
1848 it->descent = it->phys_descent = font->descent;
1849
1850 if (it->glyph_row)
1851 {
1852 double ascent = (double) it->ascent / (it->ascent + it->descent);
1853 x_append_stretch_glyph (it, it->object, it->pixel_width,
1854 it->ascent + it->descent, ascent);
1855 }
1856 }
1857 else
1858 {
1859 /* A multi-byte character. Assume that the display width of the
1860 character is the width of the character multiplied by the
1861 width of the font. There has to be better support for
1862 variable sizes in cmpchar_info to do anything better than
1863 that.
1864
1865 Note: composite characters are represented as one glyph in
1866 the glyph matrix. There are no padding glyphs. */
1867 if (it->charset == CHARSET_COMPOSITION)
1868 {
1869 struct cmpchar_info *cmpcharp;
1870 int idx;
1871
1872 idx = COMPOSITE_CHAR_ID (it->char_to_display);
1873 cmpcharp = cmpchar_table[idx];
1874 it->pixel_width = font->max_bounds.width * cmpcharp->width;
1875
1876 /* There are no padding glyphs, so there is only one glyph
1877 to produce for the composite char. Important is that
1878 pixel_width, ascent and descent are the values of what is
1879 drawn by draw_glyphs. */
1880 it->nglyphs = 1;
1881
1882 /* These settings may not be correct. We must have more
1883 information in cmpcharp to do the correct setting. */
1884 it->ascent = font->ascent;
1885 it->descent = font->descent;
1886 it->phys_ascent = font->max_bounds.ascent;
1887 it->phys_descent = font->max_bounds.descent;
1888 }
1889 else
1890 {
1891 /* If we found a font, this font should give us the right
1892 metrics. If we didn't find a font, use the frame's
1893 default font and calculate the width of the character
1894 from the charset width; this is what old redisplay code
1895 did. */
1896 pcm = x_per_char_metric (font, &char2b);
1897 it->pixel_width = pcm->width;
1898 if (font_not_found_p)
1899 it->pixel_width *= CHARSET_WIDTH (it->charset);
1900 it->nglyphs = 1;
1901 it->ascent = font->ascent;
1902 it->descent = font->descent;
1903 it->phys_ascent = pcm->ascent;
1904 it->phys_descent = pcm->descent;
1905 if (it->glyph_row
1906 && (pcm->lbearing < 0
1907 || pcm->rbearing > pcm->width))
1908 it->glyph_row->contains_overlapping_glyphs_p = 1;
1909 }
1910
1911 if (face->box != FACE_NO_BOX)
1912 {
1913 int thick = face->box_line_width;
1914 it->ascent += thick;
1915 it->descent += thick;
1916
1917 if (it->start_of_box_run_p)
1918 it->pixel_width += thick;
1919 if (it->end_of_box_run_p)
1920 it->pixel_width += thick;
1921 }
1922
1923 /* If face has an overline, add the height of the overline
1924 (1 pixel) and a 1 pixel margin to the character height. */
1925 if (face->overline_p)
1926 it->ascent += 2;
1927
1928 take_vertical_position_into_account (it);
1929
1930 if (it->glyph_row)
1931 x_append_glyph (it);
1932 }
1933 }
1934 else if (it->what == IT_IMAGE)
1935 x_produce_image_glyph (it);
1936 else if (it->what == IT_STRETCH)
1937 x_produce_stretch_glyph (it);
1938
1939 /* Accumulate dimensions. */
1940 xassert (it->ascent >= 0 && it->descent > 0);
1941 if (it->area == TEXT_AREA)
1942 it->current_x += it->pixel_width;
1943
1944 it->max_ascent = max (it->max_ascent, it->ascent);
1945 it->max_descent = max (it->max_descent, it->descent);
1946 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
1947 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
1948 }
1949
1950
1951 /* Estimate the pixel height of the mode or top line on frame F.
1952 FACE_ID specifies what line's height to estimate. */
1953
1954 int
1955 x_estimate_mode_line_height (f, face_id)
1956 struct frame *f;
1957 enum face_id face_id;
1958 {
1959 int height = 1;
1960
1961 /* This function is called so early when Emacs starts that the face
1962 cache and mode line face are not yet initialized. */
1963 if (FRAME_FACE_CACHE (f))
1964 {
1965 struct face *face = FACE_FROM_ID (f, face_id);
1966 if (face)
1967 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
1968 }
1969
1970 return height;
1971 }
1972
1973 \f
1974 /***********************************************************************
1975 Glyph display
1976 ***********************************************************************/
1977
1978 /* A sequence of glyphs to be drawn in the same face.
1979
1980 This data structure is not really completely X specific, so it
1981 could possibly, at least partially, be useful for other systems. It
1982 is currently not part of the external redisplay interface because
1983 it's not clear what other systems will need. */
1984
1985 struct glyph_string
1986 {
1987 /* X-origin of the string. */
1988 int x;
1989
1990 /* Y-origin and y-position of the base line of this string. */
1991 int y, ybase;
1992
1993 /* The width of the string, not including a face extension. */
1994 int width;
1995
1996 /* The width of the string, including a face extension. */
1997 int background_width;
1998
1999 /* The height of this string. This is the height of the line this
2000 string is drawn in, and can be different from the height of the
2001 font the string is drawn in. */
2002 int height;
2003
2004 /* Number of pixels this string overwrites in front of its x-origin.
2005 This number is zero if the string has an lbearing >= 0; it is
2006 -lbearing, if the string has an lbearing < 0. */
2007 int left_overhang;
2008
2009 /* Number of pixels this string overwrites past its right-most
2010 nominal x-position, i.e. x + width. Zero if the string's
2011 rbearing is <= its nominal width, rbearing - width otherwise. */
2012 int right_overhang;
2013
2014 /* The frame on which the glyph string is drawn. */
2015 struct frame *f;
2016
2017 /* The window on which the glyph string is drawn. */
2018 struct window *w;
2019
2020 /* X display and window for convenience. */
2021 Display *display;
2022 Window window;
2023
2024 /* The glyph row for which this string was built. It determines the
2025 y-origin and height of the string. */
2026 struct glyph_row *row;
2027
2028 /* The area within row. */
2029 enum glyph_row_area area;
2030
2031 /* Characters to be drawn, and number of characters. */
2032 XChar2b *char2b;
2033 int nchars;
2034
2035 /* Character set of this glyph string. */
2036 int charset;
2037
2038 /* A face-override for drawing cursors, mouse face and similar. */
2039 enum draw_glyphs_face hl;
2040
2041 /* Face in which this string is to be drawn. */
2042 struct face *face;
2043
2044 /* Font in which this string is to be drawn. */
2045 XFontStruct *font;
2046
2047 /* Font info for this string. */
2048 struct font_info *font_info;
2049
2050 /* Non-null means this string describes (part of) a composite
2051 character. All characters from char2b are drawn at the same
2052 x-origin in that case. */
2053 struct cmpchar_info *cmpcharp;
2054
2055 /* Index of this glyph string's first character in the glyph
2056 definition of cmpcharp. If this is zero, this glyph string
2057 describes the first character of a composite character. */
2058 int gidx;
2059
2060 /* 1 means this glyph strings face has to be drawn to the right end
2061 of the window's drawing area. */
2062 unsigned extends_to_end_of_line_p : 1;
2063
2064 /* 1 means the background of this string has been drawn. */
2065 unsigned background_filled_p : 1;
2066
2067 /* 1 means glyph string must be drawn with 16-bit functions. */
2068 unsigned two_byte_p : 1;
2069
2070 /* 1 means that the original font determined for drawing this glyph
2071 string could not be loaded. The member `font' has been set to
2072 the frame's default font in this case. */
2073 unsigned font_not_found_p : 1;
2074
2075 /* 1 means that the face in which this glyph string is drawn has a
2076 stipple pattern. */
2077 unsigned stippled_p : 1;
2078
2079 /* 1 means only the foreground of this glyph string must be drawn,
2080 and we should use the physical height of the line this glyph
2081 string appears in as clip rect. */
2082 unsigned for_overlaps_p : 1;
2083
2084 /* The GC to use for drawing this glyph string. */
2085 GC gc;
2086
2087 /* A pointer to the first glyph in the string. This glyph
2088 corresponds to char2b[0]. Needed to draw rectangles if
2089 font_not_found_p is 1. */
2090 struct glyph *first_glyph;
2091
2092 /* Image, if any. */
2093 struct image *img;
2094
2095 struct glyph_string *next, *prev;
2096 };
2097
2098
2099 #if GLYPH_DEBUG
2100
2101 static void
2102 x_dump_glyph_string (s)
2103 struct glyph_string *s;
2104 {
2105 fprintf (stderr, "glyph string\n");
2106 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2107 s->x, s->y, s->width, s->height);
2108 fprintf (stderr, " ybase = %d\n", s->ybase);
2109 fprintf (stderr, " hl = %d\n", s->hl);
2110 fprintf (stderr, " left overhang = %d, right = %d\n",
2111 s->left_overhang, s->right_overhang);
2112 fprintf (stderr, " nchars = %d\n", s->nchars);
2113 fprintf (stderr, " extends to end of line = %d\n",
2114 s->extends_to_end_of_line_p);
2115 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2116 fprintf (stderr, " bg width = %d\n", s->background_width);
2117 }
2118
2119 #endif /* GLYPH_DEBUG */
2120
2121
2122
2123 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2124 struct glyph_string **,
2125 struct glyph_string *,
2126 struct glyph_string *));
2127 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2128 struct glyph_string **,
2129 struct glyph_string *,
2130 struct glyph_string *));
2131 static void x_append_glyph_string P_ ((struct glyph_string **,
2132 struct glyph_string **,
2133 struct glyph_string *));
2134 static int x_left_overwritten P_ ((struct glyph_string *));
2135 static int x_left_overwriting P_ ((struct glyph_string *));
2136 static int x_right_overwritten P_ ((struct glyph_string *));
2137 static int x_right_overwriting P_ ((struct glyph_string *));
2138 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2139 int));
2140 static void x_init_glyph_string P_ ((struct glyph_string *,
2141 XChar2b *, struct window *,
2142 struct glyph_row *,
2143 enum glyph_row_area, int,
2144 enum draw_glyphs_face));
2145 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2146 enum glyph_row_area, int, int,
2147 enum draw_glyphs_face, int *, int *, int));
2148 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2149 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2150 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2151 int));
2152 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2153 static void x_draw_glyph_string_underline P_ ((struct glyph_string *));
2154 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2155 static void x_draw_glyph_string P_ ((struct glyph_string *));
2156 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2157 static void x_set_cursor_gc P_ ((struct glyph_string *));
2158 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2159 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2160 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2161 int *, int *));
2162 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2163 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2164 unsigned long *, float, int));
2165 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2166 float, int, unsigned long));
2167 static void x_setup_relief_colors P_ ((struct glyph_string *));
2168 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2169 static void x_draw_image_relief P_ ((struct glyph_string *));
2170 static void x_draw_image_foreground P_ ((struct glyph_string *));
2171 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2172 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2173 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2174 int, int, int));
2175 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2176 int, int, int, int, XRectangle *));
2177 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2178 int, int, int, XRectangle *));
2179 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2180 enum glyph_row_area));
2181
2182
2183 /* Append the list of glyph strings with head H and tail T to the list
2184 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2185
2186 static INLINE void
2187 x_append_glyph_string_lists (head, tail, h, t)
2188 struct glyph_string **head, **tail;
2189 struct glyph_string *h, *t;
2190 {
2191 if (h)
2192 {
2193 if (*head)
2194 (*tail)->next = h;
2195 else
2196 *head = h;
2197 h->prev = *tail;
2198 *tail = t;
2199 }
2200 }
2201
2202
2203 /* Prepend the list of glyph strings with head H and tail T to the
2204 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2205 result. */
2206
2207 static INLINE void
2208 x_prepend_glyph_string_lists (head, tail, h, t)
2209 struct glyph_string **head, **tail;
2210 struct glyph_string *h, *t;
2211 {
2212 if (h)
2213 {
2214 if (*head)
2215 (*head)->prev = t;
2216 else
2217 *tail = t;
2218 t->next = *head;
2219 *head = h;
2220 }
2221 }
2222
2223
2224 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2225 Set *HEAD and *TAIL to the resulting list. */
2226
2227 static INLINE void
2228 x_append_glyph_string (head, tail, s)
2229 struct glyph_string **head, **tail;
2230 struct glyph_string *s;
2231 {
2232 s->next = s->prev = NULL;
2233 x_append_glyph_string_lists (head, tail, s, s);
2234 }
2235
2236
2237 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2238 face. */
2239
2240 static void
2241 x_set_cursor_gc (s)
2242 struct glyph_string *s;
2243 {
2244 if (s->font == FRAME_FONT (s->f)
2245 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2246 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2247 && !s->cmpcharp)
2248 s->gc = s->f->output_data.x->cursor_gc;
2249 else
2250 {
2251 /* Cursor on non-default face: must merge. */
2252 XGCValues xgcv;
2253 unsigned long mask;
2254
2255 xgcv.background = s->f->output_data.x->cursor_pixel;
2256 xgcv.foreground = s->face->background;
2257
2258 /* If the glyph would be invisible, try a different foreground. */
2259 if (xgcv.foreground == xgcv.background)
2260 xgcv.foreground = s->face->foreground;
2261 if (xgcv.foreground == xgcv.background)
2262 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2263 if (xgcv.foreground == xgcv.background)
2264 xgcv.foreground = s->face->foreground;
2265
2266 /* Make sure the cursor is distinct from text in this face. */
2267 if (xgcv.background == s->face->background
2268 && xgcv.foreground == s->face->foreground)
2269 {
2270 xgcv.background = s->face->foreground;
2271 xgcv.foreground = s->face->background;
2272 }
2273
2274 IF_DEBUG (x_check_font (s->f, s->font));
2275 xgcv.font = s->font->fid;
2276 xgcv.graphics_exposures = False;
2277 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2278
2279 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2280 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2281 mask, &xgcv);
2282 else
2283 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2284 = XCreateGC (s->display, s->window, mask, &xgcv);
2285
2286 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2287 }
2288 }
2289
2290
2291 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2292
2293 static void
2294 x_set_mouse_face_gc (s)
2295 struct glyph_string *s;
2296 {
2297 int face_id;
2298
2299 /* What face has to be used for the mouse face? */
2300 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2301 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2302 s->face = FACE_FROM_ID (s->f, face_id);
2303 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2304
2305 /* If font in this face is same as S->font, use it. */
2306 if (s->font == s->face->font)
2307 s->gc = s->face->gc;
2308 else
2309 {
2310 /* Otherwise construct scratch_cursor_gc with values from FACE
2311 but font FONT. */
2312 XGCValues xgcv;
2313 unsigned long mask;
2314
2315 xgcv.background = s->face->background;
2316 xgcv.foreground = s->face->foreground;
2317 IF_DEBUG (x_check_font (s->f, s->font));
2318 xgcv.font = s->font->fid;
2319 xgcv.graphics_exposures = False;
2320 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2321
2322 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2323 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2324 mask, &xgcv);
2325 else
2326 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2327 = XCreateGC (s->display, s->window, mask, &xgcv);
2328
2329 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2330 }
2331
2332 xassert (s->gc != 0);
2333 }
2334
2335
2336 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2337 Faces to use in the mode line have already been computed when the
2338 matrix was built, so there isn't much to do, here. */
2339
2340 static INLINE void
2341 x_set_mode_line_face_gc (s)
2342 struct glyph_string *s;
2343 {
2344 s->gc = s->face->gc;
2345 xassert (s->gc != 0);
2346 }
2347
2348
2349 /* Set S->gc of glyph string S for drawing that glyph string. Set
2350 S->stippled_p to a non-zero value if the face of S has a stipple
2351 pattern. */
2352
2353 static INLINE void
2354 x_set_glyph_string_gc (s)
2355 struct glyph_string *s;
2356 {
2357 if (s->hl == DRAW_NORMAL_TEXT)
2358 {
2359 s->gc = s->face->gc;
2360 s->stippled_p = s->face->stipple != 0;
2361 }
2362 else if (s->hl == DRAW_INVERSE_VIDEO)
2363 {
2364 x_set_mode_line_face_gc (s);
2365 s->stippled_p = s->face->stipple != 0;
2366 }
2367 else if (s->hl == DRAW_CURSOR)
2368 {
2369 x_set_cursor_gc (s);
2370 s->stippled_p = 0;
2371 }
2372 else if (s->hl == DRAW_MOUSE_FACE)
2373 {
2374 x_set_mouse_face_gc (s);
2375 s->stippled_p = s->face->stipple != 0;
2376 }
2377 else if (s->hl == DRAW_IMAGE_RAISED
2378 || s->hl == DRAW_IMAGE_SUNKEN)
2379 {
2380 s->gc = s->face->gc;
2381 s->stippled_p = s->face->stipple != 0;
2382 }
2383 else
2384 {
2385 s->gc = s->face->gc;
2386 s->stippled_p = s->face->stipple != 0;
2387 }
2388
2389 /* GC must have been set. */
2390 xassert (s->gc != 0);
2391 }
2392
2393
2394 /* Return in *R the clipping rectangle for glyph string S. */
2395
2396 static void
2397 x_get_glyph_string_clip_rect (s, r)
2398 struct glyph_string *s;
2399 XRectangle *r;
2400 {
2401 if (s->row->full_width_p)
2402 {
2403 /* Draw full-width. X coordinates are relative to S->w->left. */
2404 int canon_x = CANON_X_UNIT (s->f);
2405
2406 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2407 r->width = XFASTINT (s->w->width) * canon_x;
2408
2409 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2410 {
2411 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2412 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2413 r->x -= width;
2414 }
2415
2416 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2417
2418 /* Unless displaying a mode or menu bar line, which are always
2419 fully visible, clip to the visible part of the row. */
2420 if (s->w->pseudo_window_p)
2421 r->height = s->row->visible_height;
2422 else
2423 r->height = s->height;
2424 }
2425 else
2426 {
2427 /* This is a text line that may be partially visible. */
2428 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2429 r->width = window_box_width (s->w, s->area);
2430 r->height = s->row->visible_height;
2431 }
2432
2433 /* Don't use S->y for clipping because it doesn't take partially
2434 visible lines into account. For example, it can be negative for
2435 partially visible lines at the top of a window. */
2436 if (!s->row->full_width_p
2437 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2438 r->y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (s->w);
2439 else
2440 r->y = max (0, s->row->y);
2441
2442 /* If drawing a toolbar window, draw it over the internal border
2443 at the top of the window. */
2444 if (s->w == XWINDOW (s->f->toolbar_window))
2445 r->y -= s->f->output_data.x->internal_border_width;
2446
2447 /* If S draws overlapping rows, it's sufficient to use the top and
2448 bottom of the window for clipping because this glyph string
2449 intentionally draws over other lines. */
2450 if (s->for_overlaps_p)
2451 {
2452 r->y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (s->w);
2453 r->height = window_text_bottom_y (s->w) - r->y;
2454 }
2455
2456 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2457 }
2458
2459
2460 /* Set clipping for output of glyph string S. S may be part of a mode
2461 line or menu if we don't have X toolkit support. */
2462
2463 static INLINE void
2464 x_set_glyph_string_clipping (s)
2465 struct glyph_string *s;
2466 {
2467 XRectangle r;
2468 x_get_glyph_string_clip_rect (s, &r);
2469 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2470 }
2471
2472
2473 /* Compute left and right overhang of glyph string S. If S is a glyph
2474 string for a composite character, assume overhangs don't exist. */
2475
2476 static INLINE void
2477 x_compute_glyph_string_overhangs (s)
2478 struct glyph_string *s;
2479 {
2480 if (s->cmpcharp == NULL
2481 && s->first_glyph->type == CHAR_GLYPH)
2482 {
2483 XCharStruct cs;
2484 int direction, font_ascent, font_descent;
2485 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2486 &font_ascent, &font_descent, &cs);
2487 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2488 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2489 }
2490 }
2491
2492
2493 /* Compute overhangs and x-positions for glyph string S and its
2494 predecessors, or successors. X is the starting x-position for S.
2495 BACKWARD_P non-zero means process predecessors. */
2496
2497 static void
2498 x_compute_overhangs_and_x (s, x, backward_p)
2499 struct glyph_string *s;
2500 int x;
2501 int backward_p;
2502 {
2503 if (backward_p)
2504 {
2505 while (s)
2506 {
2507 x_compute_glyph_string_overhangs (s);
2508 x -= s->width;
2509 s->x = x;
2510 s = s->prev;
2511 }
2512 }
2513 else
2514 {
2515 while (s)
2516 {
2517 x_compute_glyph_string_overhangs (s);
2518 s->x = x;
2519 x += s->width;
2520 s = s->next;
2521 }
2522 }
2523 }
2524
2525
2526 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2527 frame F. Overhangs of glyphs other than type CHAR_GLYPH or of
2528 character glyphs for composite characters are assumed to be zero. */
2529
2530 static void
2531 x_get_glyph_overhangs (glyph, f, left, right)
2532 struct glyph *glyph;
2533 struct frame *f;
2534 int *left, *right;
2535 {
2536 int c;
2537
2538 *left = *right = 0;
2539
2540 if (glyph->type == CHAR_GLYPH
2541 && (c = glyph->u.ch.code,
2542 CHAR_CHARSET (c) != CHARSET_COMPOSITION))
2543 {
2544 XFontStruct *font;
2545 struct face *face;
2546 struct font_info *font_info;
2547 XChar2b char2b;
2548
2549 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2550 font = face->font;
2551 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2552 if (font)
2553 {
2554 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2555
2556 if (pcm->rbearing > pcm->width)
2557 *right = pcm->rbearing - pcm->width;
2558 if (pcm->lbearing < 0)
2559 *left = -pcm->lbearing;
2560 }
2561 }
2562 }
2563
2564
2565 /* Return the index of the first glyph preceding glyph string S that
2566 is overwritten by S because of S's left overhang. Value is -1
2567 if no glyphs are overwritten. */
2568
2569 static int
2570 x_left_overwritten (s)
2571 struct glyph_string *s;
2572 {
2573 int k;
2574
2575 if (s->left_overhang)
2576 {
2577 int x = 0, i;
2578 struct glyph *glyphs = s->row->glyphs[s->area];
2579 int first = s->first_glyph - glyphs;
2580
2581 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2582 x -= glyphs[i].pixel_width;
2583
2584 k = i + 1;
2585 }
2586 else
2587 k = -1;
2588
2589 return k;
2590 }
2591
2592
2593 /* Return the index of the first glyph preceding glyph string S that
2594 is overwriting S because of its right overhang. Value is -1 if no
2595 glyph in front of S overwrites S. */
2596
2597 static int
2598 x_left_overwriting (s)
2599 struct glyph_string *s;
2600 {
2601 int i, k, x;
2602 struct glyph *glyphs = s->row->glyphs[s->area];
2603 int first = s->first_glyph - glyphs;
2604
2605 k = -1;
2606 x = 0;
2607 for (i = first - 1; i >= 0; --i)
2608 {
2609 int left, right;
2610 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2611 if (x + right > 0)
2612 k = i;
2613 x -= glyphs[i].pixel_width;
2614 }
2615
2616 return k;
2617 }
2618
2619
2620 /* Return the index of the last glyph following glyph string S that is
2621 not overwritten by S because of S's right overhang. Value is -1 if
2622 no such glyph is found. */
2623
2624 static int
2625 x_right_overwritten (s)
2626 struct glyph_string *s;
2627 {
2628 int k = -1;
2629
2630 if (s->right_overhang)
2631 {
2632 int x = 0, i;
2633 struct glyph *glyphs = s->row->glyphs[s->area];
2634 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2635 int end = s->row->used[s->area];
2636
2637 for (i = first; i < end && s->right_overhang > x; ++i)
2638 x += glyphs[i].pixel_width;
2639
2640 k = i;
2641 }
2642
2643 return k;
2644 }
2645
2646
2647 /* Return the index of the last glyph following glyph string S that
2648 overwrites S because of its left overhang. Value is negative
2649 if no such glyph is found. */
2650
2651 static int
2652 x_right_overwriting (s)
2653 struct glyph_string *s;
2654 {
2655 int i, k, x;
2656 int end = s->row->used[s->area];
2657 struct glyph *glyphs = s->row->glyphs[s->area];
2658 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2659
2660 k = -1;
2661 x = 0;
2662 for (i = first; i < end; ++i)
2663 {
2664 int left, right;
2665 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2666 if (x - left < 0)
2667 k = i;
2668 x += glyphs[i].pixel_width;
2669 }
2670
2671 return k;
2672 }
2673
2674
2675 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2676
2677 static INLINE void
2678 x_clear_glyph_string_rect (s, x, y, w, h)
2679 struct glyph_string *s;
2680 int x, y, w, h;
2681 {
2682 XGCValues xgcv;
2683 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2684 XSetForeground (s->display, s->gc, xgcv.background);
2685 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2686 XSetForeground (s->display, s->gc, xgcv.foreground);
2687 }
2688
2689
2690 /* Draw the background of glyph_string S. If S->background_filled_p
2691 is non-zero don't draw it. FORCE_P non-zero means draw the
2692 background even if it wouldn't be drawn normally. This is used
2693 when a string preceding S draws into the background of S. */
2694
2695 static void
2696 x_draw_glyph_string_background (s, force_p)
2697 struct glyph_string *s;
2698 int force_p;
2699 {
2700 /* Nothing to do if background has already been drawn or if it
2701 shouldn't be drawn in the first place. */
2702 if (!s->background_filled_p)
2703 {
2704 if (s->cmpcharp
2705 && s->gidx > 0
2706 && !s->font_not_found_p
2707 && !s->extends_to_end_of_line_p)
2708 {
2709 /* Don't draw background for glyphs of a composite
2710 characters, except for the first one. */
2711 s->background_filled_p = 1;
2712 }
2713 else if (s->stippled_p)
2714 {
2715 /* Fill background with a stipple pattern. */
2716 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2717 XFillRectangle (s->display, s->window, s->gc, s->x,
2718 s->y + s->face->box_line_width,
2719 s->background_width,
2720 s->height - 2 * s->face->box_line_width);
2721 XSetFillStyle (s->display, s->gc, FillSolid);
2722 s->background_filled_p = 1;
2723 }
2724 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
2725 || s->font_not_found_p
2726 || s->extends_to_end_of_line_p
2727 || s->cmpcharp
2728 || force_p)
2729 {
2730 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
2731 s->background_width,
2732 s->height - 2 * s->face->box_line_width);
2733 s->background_filled_p = 1;
2734 }
2735 }
2736 }
2737
2738
2739 /* Draw the foreground of glyph string S. */
2740
2741 static void
2742 x_draw_glyph_string_foreground (s)
2743 struct glyph_string *s;
2744 {
2745 int i, x;
2746
2747 /* If first glyph of S has a left box line, start drawing the text
2748 of S to the right of that box line. */
2749 if (s->face->box != FACE_NO_BOX
2750 && s->first_glyph->left_box_line_p)
2751 x = s->x + s->face->box_line_width;
2752 else
2753 x = s->x;
2754
2755 if (s->cmpcharp == NULL)
2756 {
2757 /* Not a composite character. Draw characters of S as
2758 rectangles if S's font could not be loaded. */
2759 if (s->font_not_found_p)
2760 {
2761 for (i = 0; i < s->nchars; ++i)
2762 {
2763 struct glyph *g = s->first_glyph + i;
2764 XDrawRectangle (s->display, s->window,
2765 s->gc, x, s->y, g->pixel_width - 1,
2766 s->height - 1);
2767 x += g->pixel_width;
2768 }
2769 }
2770 else
2771 {
2772 char *char1b = (char *) s->char2b;
2773
2774 /* If we can use 8-bit functions, condense S->char2b. */
2775 if (!s->two_byte_p)
2776 for (i = 0; i < s->nchars; ++i)
2777 char1b[i] = s->char2b[i].byte2;
2778
2779 /* Draw text with XDrawString if background has already been
2780 filled. Otherwise, use XDrawImageString. (Note that
2781 XDrawImageString is usually faster than XDrawString.)
2782 Always use XDrawImageString when drawing the cursor so
2783 that there is no chance that characters under a box
2784 cursor are invisible. */
2785 if (s->for_overlaps_p
2786 || (s->background_filled_p && s->hl != DRAW_CURSOR))
2787 {
2788 /* Draw characters with 16-bit or 8-bit functions. */
2789 if (s->two_byte_p)
2790 XDrawString16 (s->display, s->window, s->gc, x, s->ybase,
2791 s->char2b, s->nchars);
2792 else
2793 XDrawString (s->display, s->window, s->gc, x, s->ybase,
2794 char1b, s->nchars);
2795 }
2796 else
2797 {
2798 if (s->two_byte_p)
2799 XDrawImageString16 (s->display, s->window, s->gc,
2800 x, s->ybase, s->char2b, s->nchars);
2801 else
2802 XDrawImageString (s->display, s->window, s->gc,
2803 x, s->ybase, char1b, s->nchars);
2804 }
2805 }
2806 }
2807 else
2808 {
2809 /* S is a glyph string for a composite character. S->gidx is the
2810 index of the first character drawn in the vector
2811 S->cmpcharp->glyph. S->gidx == 0 means we are drawing the
2812 very first component character of a composite char. */
2813
2814 /* Draw a single rectangle for the composite character if S's
2815 font could not be loaded. */
2816 if (s->font_not_found_p && s->gidx == 0)
2817 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
2818 s->width - 1, s->height - 1);
2819 else
2820 {
2821 XCharStruct *pcm;
2822 int relative_compose, default_ascent, i;
2823 int highest = 0, lowest = 0;
2824
2825 /* The value of font_info my be null if we couldn't find it
2826 in x_get_char_face_and_encoding. */
2827 if (s->cmpcharp->cmp_rule == NULL && s->font_info)
2828 {
2829 relative_compose = s->font_info->relative_compose;
2830 default_ascent = s->font_info->default_ascent;
2831 }
2832 else
2833 relative_compose = default_ascent = 0;
2834
2835 if ((s->cmpcharp->cmp_rule || relative_compose)
2836 && s->gidx == 0)
2837 {
2838 /* This is the first character. Initialize variables.
2839 Highest is the highest position of glyphs ever
2840 written, lowest the lowest position. */
2841 int x_offset = 0;
2842 int first_ch = s->first_glyph->u.ch.code;
2843
2844 if (default_ascent
2845 && CHAR_TABLE_P (Vuse_default_ascent)
2846 && !NILP (Faref (Vuse_default_ascent, first_ch)))
2847 {
2848 highest = default_ascent;
2849 lowest = 0;
2850 }
2851 else
2852 {
2853 pcm = PER_CHAR_METRIC (s->font, s->char2b);
2854 highest = pcm->ascent + 1;
2855 lowest = - pcm->descent;
2856 }
2857
2858 if (s->cmpcharp->cmp_rule)
2859 x_offset = (s->cmpcharp->col_offset[0]
2860 * FONT_WIDTH (s->f->output_data.x->font));
2861
2862 /* Draw the first character at the normal position. */
2863 XDrawString16 (s->display, s->window, s->gc,
2864 x + x_offset,
2865 s->ybase, s->char2b, 1);
2866 i = 1;
2867 ++s->gidx;
2868 }
2869 else
2870 i = 0;
2871
2872 for (; i < s->nchars; i++, ++s->gidx)
2873 {
2874 int x_offset = 0, y_offset = 0;
2875
2876 if (relative_compose)
2877 {
2878 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2879 if (NILP (Vignore_relative_composition)
2880 || NILP (Faref (Vignore_relative_composition,
2881 make_number (s->cmpcharp->glyph[s->gidx]))))
2882 {
2883 if (- pcm->descent >= relative_compose)
2884 {
2885 /* Draw above the current glyphs. */
2886 y_offset = highest + pcm->descent;
2887 highest += pcm->ascent + pcm->descent;
2888 }
2889 else if (pcm->ascent <= 0)
2890 {
2891 /* Draw beneath the current glyphs. */
2892 y_offset = lowest - pcm->ascent;
2893 lowest -= pcm->ascent + pcm->descent;
2894 }
2895 }
2896 else
2897 {
2898 /* Draw the glyph at normal position. If
2899 it sticks out of HIGHEST or LOWEST,
2900 update them appropriately. */
2901 if (pcm->ascent > highest)
2902 highest = pcm->ascent;
2903 else if (- pcm->descent < lowest)
2904 lowest = - pcm->descent;
2905 }
2906 }
2907 else if (s->cmpcharp->cmp_rule)
2908 {
2909 int gref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) / 9;
2910 int nref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) % 9;
2911 int bottom, top;
2912
2913 /* Re-encode GREF and NREF so that they specify
2914 only Y-axis information:
2915 0:top, 1:base, 2:bottom, 3:center */
2916 gref = gref / 3 + (gref == 4) * 2;
2917 nref = nref / 3 + (nref == 4) * 2;
2918
2919 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2920 bottom = ((gref == 0 ? highest : gref == 1 ? 0
2921 : gref == 2 ? lowest
2922 : (highest + lowest) / 2)
2923 - (nref == 0 ? pcm->ascent + pcm->descent
2924 : nref == 1 ? pcm->descent : nref == 2 ? 0
2925 : (pcm->ascent + pcm->descent) / 2));
2926 top = bottom + (pcm->ascent + pcm->descent);
2927 if (top > highest)
2928 highest = top;
2929 if (bottom < lowest)
2930 lowest = bottom;
2931 y_offset = bottom + pcm->descent;
2932 x_offset = (s->cmpcharp->col_offset[s->gidx]
2933 * FONT_WIDTH (FRAME_FONT (s->f)));
2934 }
2935
2936 XDrawString16 (s->display, s->window, s->gc,
2937 x + x_offset, s->ybase - y_offset,
2938 s->char2b + i, 1);
2939 }
2940 }
2941 }
2942 }
2943
2944
2945 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
2946 CMAP. If an exact match can't be allocated, try the nearest color
2947 available. Value is non-zero if successful. Set *COLOR to the
2948 color allocated. */
2949
2950 int
2951 x_alloc_nearest_color (display, screen, cmap, color)
2952 Display *display;
2953 Screen *screen;
2954 Colormap cmap;
2955 XColor *color;
2956 {
2957 int rc = XAllocColor (display, cmap, color);
2958 if (rc == 0)
2959 {
2960 /* If we got to this point, the colormap is full, so we're going
2961 to try to get the next closest color. The algorithm used is
2962 a least-squares matching, which is what X uses for closest
2963 color matching with StaticColor visuals. */
2964 int nearest, i;
2965 unsigned long nearest_delta = ~0;
2966 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
2967 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
2968
2969 for (i = 0; i < ncells; ++i)
2970 cells[i].pixel = i;
2971 XQueryColors (display, cmap, cells, ncells);
2972
2973 for (nearest = i = 0; i < ncells; ++i)
2974 {
2975 long dred = (color->red >> 8) - (cells[i].red >> 8);
2976 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
2977 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2978 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
2979
2980 if (delta < nearest_delta)
2981 {
2982 nearest = i;
2983 nearest_delta = delta;
2984 }
2985 }
2986
2987 color->red = cells[nearest].red;
2988 color->green = cells[nearest].green;
2989 color->blue = cells[nearest].blue;
2990 rc = XAllocColor (display, cmap, color);
2991 }
2992
2993 return rc;
2994 }
2995
2996
2997 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2998 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2999 If this produces the same color as PIXEL, try a color where all RGB
3000 values have DELTA added. Return the allocated color in *PIXEL.
3001 DISPLAY is the X display, CMAP is the colormap to operate on.
3002 Value is non-zero if successful. */
3003
3004 static int
3005 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3006 struct frame *f;
3007 Display *display;
3008 Colormap cmap;
3009 unsigned long *pixel;
3010 float factor;
3011 int delta;
3012 {
3013 XColor color, new;
3014 int success_p;
3015
3016 /* Get RGB color values. */
3017 color.pixel = *pixel;
3018 XQueryColor (display, cmap, &color);
3019
3020 /* Change RGB values by specified FACTOR. Avoid overflow! */
3021 xassert (factor >= 0);
3022 new.red = min (0xffff, factor * color.red);
3023 new.green = min (0xffff, factor * color.green);
3024 new.blue = min (0xffff, factor * color.blue);
3025
3026 /* Try to allocate the color. */
3027 success_p = x_alloc_nearest_color (display, FRAME_X_SCREEN (f), cmap, &new);
3028 if (success_p)
3029 {
3030 if (new.pixel == *pixel)
3031 {
3032 /* If we end up with the same color as before, try adding
3033 delta to the RGB values. */
3034 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3035
3036 /* If display has an immutable color map, freeing colors is
3037 not necessary and some servers don't allow it. So don't
3038 do it. */
3039 if (class != StaticColor
3040 && class != StaticGray
3041 && class != TrueColor)
3042 XFreeColors (display, cmap, &new.pixel, 1, 0);
3043
3044 new.red = min (0xffff, delta + color.red);
3045 new.green = min (0xffff, delta + color.green);
3046 new.blue = min (0xffff, delta + color.blue);
3047 success_p = x_alloc_nearest_color (display, FRAME_X_SCREEN (f),
3048 cmap, &new);
3049 }
3050 else
3051 success_p = 1;
3052 *pixel = new.pixel;
3053 }
3054
3055 return success_p;
3056 }
3057
3058
3059 /* Set up the foreground color for drawing relief lines of glyph
3060 string S. RELIEF is a pointer to a struct relief containing the GC
3061 with which lines will be drawn. Use a color that is FACTOR or
3062 DELTA lighter or darker than the relief's background which is found
3063 in S->f->output_data.x->relief_background. If such a color cannot
3064 be allocated, use DEFAULT_PIXEL, instead. */
3065
3066 static void
3067 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3068 struct frame *f;
3069 struct relief *relief;
3070 float factor;
3071 int delta;
3072 unsigned long default_pixel;
3073 {
3074 XGCValues xgcv;
3075 struct x_output *di = f->output_data.x;
3076 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3077 unsigned long pixel;
3078 unsigned long background = di->relief_background;
3079 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
3080 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3081 Display *dpy = FRAME_X_DISPLAY (f);
3082
3083 xgcv.graphics_exposures = False;
3084 xgcv.line_width = 1;
3085
3086 /* Free previously allocated color. The color cell will be reused
3087 when it has been freed as many times as it was allocated, so this
3088 doesn't affect faces using the same colors. */
3089 if (relief->gc
3090 && relief->allocated_p)
3091 {
3092 /* If display has an immutable color map, freeing colors is not
3093 necessary and some servers don't allow it. So don't do it. */
3094 int class = dpyinfo->visual->class;
3095 if (class != StaticColor
3096 && class != StaticGray
3097 && class != TrueColor)
3098 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
3099 relief->allocated_p = 0;
3100 }
3101
3102 /* Allocate new color. */
3103 xgcv.foreground = default_pixel;
3104 pixel = background;
3105 if (dpyinfo->n_planes != 1
3106 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3107 {
3108 relief->allocated_p = 1;
3109 xgcv.foreground = relief->pixel = pixel;
3110 }
3111
3112 if (relief->gc == 0)
3113 {
3114 xgcv.stipple = dpyinfo->gray;
3115 mask |= GCStipple;
3116 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3117 }
3118 else
3119 XChangeGC (dpy, relief->gc, mask, &xgcv);
3120 }
3121
3122
3123 /* Set up colors for the relief lines around glyph string S. */
3124
3125 static void
3126 x_setup_relief_colors (s)
3127 struct glyph_string *s;
3128 {
3129 struct x_output *di = s->f->output_data.x;
3130 unsigned long color;
3131
3132 if (s->face->use_box_color_for_shadows_p)
3133 color = s->face->box_color;
3134 else
3135 {
3136 XGCValues xgcv;
3137
3138 /* Get the background color of the face. */
3139 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3140 color = xgcv.background;
3141 }
3142
3143 if (di->white_relief.gc == 0
3144 || color != di->relief_background)
3145 {
3146 di->relief_background = color;
3147 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3148 WHITE_PIX_DEFAULT (s->f));
3149 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3150 BLACK_PIX_DEFAULT (s->f));
3151 }
3152 }
3153
3154
3155 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3156 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3157 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3158 relief. LEFT_P non-zero means draw a relief on the left side of
3159 the rectangle. RIGHT_P non-zero means draw a relief on the right
3160 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3161 when drawing. */
3162
3163 static void
3164 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3165 raised_p, left_p, right_p, clip_rect)
3166 struct frame *f;
3167 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3168 XRectangle *clip_rect;
3169 {
3170 int i;
3171 GC gc;
3172
3173 if (raised_p)
3174 gc = f->output_data.x->white_relief.gc;
3175 else
3176 gc = f->output_data.x->black_relief.gc;
3177 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3178
3179 /* Top. */
3180 for (i = 0; i < width; ++i)
3181 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3182 left_x + i * left_p, top_y + i,
3183 right_x + 1 - i * right_p, top_y + i);
3184
3185 /* Left. */
3186 if (left_p)
3187 for (i = 0; i < width; ++i)
3188 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3189 left_x + i, top_y + i, left_x + i, bottom_y - i);
3190
3191 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3192 if (raised_p)
3193 gc = f->output_data.x->black_relief.gc;
3194 else
3195 gc = f->output_data.x->white_relief.gc;
3196 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3197
3198 /* Bottom. */
3199 for (i = 0; i < width; ++i)
3200 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3201 left_x + i * left_p, bottom_y - i,
3202 right_x + 1 - i * right_p, bottom_y - i);
3203
3204 /* Right. */
3205 if (right_p)
3206 for (i = 0; i < width; ++i)
3207 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3208 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3209
3210 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3211 }
3212
3213
3214 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3215 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3216 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3217 left side of the rectangle. RIGHT_P non-zero means draw a line
3218 on the right side of the rectangle. CLIP_RECT is the clipping
3219 rectangle to use when drawing. */
3220
3221 static void
3222 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3223 left_p, right_p, clip_rect)
3224 struct glyph_string *s;
3225 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3226 XRectangle *clip_rect;
3227 {
3228 XGCValues xgcv;
3229
3230 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3231 XSetForeground (s->display, s->gc, s->face->box_color);
3232 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3233
3234 /* Top. */
3235 XFillRectangle (s->display, s->window, s->gc,
3236 left_x, top_y, right_x - left_x, width);
3237
3238 /* Left. */
3239 if (left_p)
3240 XFillRectangle (s->display, s->window, s->gc,
3241 left_x, top_y, width, bottom_y - top_y);
3242
3243 /* Bottom. */
3244 XFillRectangle (s->display, s->window, s->gc,
3245 left_x, bottom_y - width, right_x - left_x, width);
3246
3247 /* Right. */
3248 if (right_p)
3249 XFillRectangle (s->display, s->window, s->gc,
3250 right_x - width, top_y, width, bottom_y - top_y);
3251
3252 XSetForeground (s->display, s->gc, xgcv.foreground);
3253 XSetClipMask (s->display, s->gc, None);
3254 }
3255
3256
3257 /* Draw a box around glyph string S. */
3258
3259 static void
3260 x_draw_glyph_string_box (s)
3261 struct glyph_string *s;
3262 {
3263 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3264 int left_p, right_p;
3265 struct glyph *last_glyph;
3266 XRectangle clip_rect;
3267
3268 last_x = window_box_right (s->w, s->area);
3269 if (s->row->full_width_p
3270 && !s->w->pseudo_window_p)
3271 {
3272 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3273 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3274 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3275 }
3276
3277 /* The glyph that may have a right box line. */
3278 last_glyph = (s->cmpcharp || s->img
3279 ? s->first_glyph
3280 : s->first_glyph + s->nchars - 1);
3281
3282 width = s->face->box_line_width;
3283 raised_p = s->face->box == FACE_RAISED_BOX;
3284 left_x = s->x;
3285 right_x = ((s->row->full_width_p
3286 ? last_x - 1
3287 : min (last_x, s->x + s->background_width) - 1));
3288 top_y = s->y;
3289 bottom_y = top_y + s->height - 1;
3290
3291 left_p = (s->first_glyph->left_box_line_p
3292 || (s->hl == DRAW_MOUSE_FACE
3293 && (s->prev == NULL
3294 || s->prev->hl != s->hl)));
3295 right_p = (last_glyph->right_box_line_p
3296 || (s->hl == DRAW_MOUSE_FACE
3297 && (s->next == NULL
3298 || s->next->hl != s->hl)));
3299
3300 x_get_glyph_string_clip_rect (s, &clip_rect);
3301
3302 if (s->face->box == FACE_SIMPLE_BOX)
3303 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3304 left_p, right_p, &clip_rect);
3305 else
3306 {
3307 x_setup_relief_colors (s);
3308 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3309 width, raised_p, left_p, right_p, &clip_rect);
3310 }
3311 }
3312
3313
3314 /* Draw foreground of image glyph string S. */
3315
3316 static void
3317 x_draw_image_foreground (s)
3318 struct glyph_string *s;
3319 {
3320 int x;
3321 int y = s->ybase - IMAGE_ASCENT (s->img);
3322
3323 /* If first glyph of S has a left box line, start drawing it to the
3324 right of that line. */
3325 if (s->face->box != FACE_NO_BOX
3326 && s->first_glyph->left_box_line_p)
3327 x = s->x + s->face->box_line_width;
3328 else
3329 x = s->x;
3330
3331 /* If there is a margin around the image, adjust x- and y-position
3332 by that margin. */
3333 if (s->img->margin)
3334 {
3335 x += s->img->margin;
3336 y += s->img->margin;
3337 }
3338
3339 if (s->img->pixmap)
3340 {
3341 if (s->img->mask)
3342 {
3343 /* We can't set both a clip mask and use XSetClipRectangles
3344 because the latter also sets a clip mask. We also can't
3345 trust on the shape extension to be available
3346 (XShapeCombineRegion). So, compute the rectangle to draw
3347 manually. */
3348 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3349 | GCFunction);
3350 XGCValues xgcv;
3351 XRectangle clip_rect, image_rect, r;
3352
3353 xgcv.clip_mask = s->img->mask;
3354 xgcv.clip_x_origin = x;
3355 xgcv.clip_y_origin = y;
3356 xgcv.function = GXcopy;
3357 XChangeGC (s->display, s->gc, mask, &xgcv);
3358
3359 x_get_glyph_string_clip_rect (s, &clip_rect);
3360 image_rect.x = x;
3361 image_rect.y = y;
3362 image_rect.width = s->img->width;
3363 image_rect.height = s->img->height;
3364 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3365 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3366 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3367 }
3368 else
3369 {
3370 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3371 0, 0, s->img->width, s->img->height, x, y);
3372
3373 /* When the image has a mask, we can expect that at
3374 least part of a mouse highlight or a block cursor will
3375 be visible. If the image doesn't have a mask, make
3376 a block cursor visible by drawing a rectangle around
3377 the image. I believe it's looking better if we do
3378 nothing here for mouse-face. */
3379 if (s->hl == DRAW_CURSOR)
3380 XDrawRectangle (s->display, s->window, s->gc, x, y,
3381 s->img->width - 1, s->img->height - 1);
3382 }
3383 }
3384 else
3385 /* Draw a rectangle if image could not be loaded. */
3386 XDrawRectangle (s->display, s->window, s->gc, x, y,
3387 s->img->width - 1, s->img->height - 1);
3388 }
3389
3390
3391 /* Draw a relief around the image glyph string S. */
3392
3393 static void
3394 x_draw_image_relief (s)
3395 struct glyph_string *s;
3396 {
3397 int x0, y0, x1, y1, thick, raised_p;
3398 XRectangle r;
3399 int x;
3400 int y = s->ybase - IMAGE_ASCENT (s->img);
3401
3402 /* If first glyph of S has a left box line, start drawing it to the
3403 right of that line. */
3404 if (s->face->box != FACE_NO_BOX
3405 && s->first_glyph->left_box_line_p)
3406 x = s->x + s->face->box_line_width;
3407 else
3408 x = s->x;
3409
3410 /* If there is a margin around the image, adjust x- and y-position
3411 by that margin. */
3412 if (s->img->margin)
3413 {
3414 x += s->img->margin;
3415 y += s->img->margin;
3416 }
3417
3418 if (s->hl == DRAW_IMAGE_SUNKEN
3419 || s->hl == DRAW_IMAGE_RAISED)
3420 {
3421 thick = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
3422 raised_p = s->hl == DRAW_IMAGE_RAISED;
3423 }
3424 else
3425 {
3426 thick = abs (s->img->relief);
3427 raised_p = s->img->relief > 0;
3428 }
3429
3430 x0 = x - thick;
3431 y0 = y - thick;
3432 x1 = x + s->img->width + thick - 1;
3433 y1 = y + s->img->height + thick - 1;
3434
3435 x_setup_relief_colors (s);
3436 x_get_glyph_string_clip_rect (s, &r);
3437 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3438 }
3439
3440
3441 /* Draw the foreground of image glyph string S to PIXMAP. */
3442
3443 static void
3444 x_draw_image_foreground_1 (s, pixmap)
3445 struct glyph_string *s;
3446 Pixmap pixmap;
3447 {
3448 int x;
3449 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3450
3451 /* If first glyph of S has a left box line, start drawing it to the
3452 right of that line. */
3453 if (s->face->box != FACE_NO_BOX
3454 && s->first_glyph->left_box_line_p)
3455 x = s->face->box_line_width;
3456 else
3457 x = 0;
3458
3459 /* If there is a margin around the image, adjust x- and y-position
3460 by that margin. */
3461 if (s->img->margin)
3462 {
3463 x += s->img->margin;
3464 y += s->img->margin;
3465 }
3466
3467 if (s->img->pixmap)
3468 {
3469 if (s->img->mask)
3470 {
3471 /* We can't set both a clip mask and use XSetClipRectangles
3472 because the latter also sets a clip mask. We also can't
3473 trust on the shape extension to be available
3474 (XShapeCombineRegion). So, compute the rectangle to draw
3475 manually. */
3476 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3477 | GCFunction);
3478 XGCValues xgcv;
3479
3480 xgcv.clip_mask = s->img->mask;
3481 xgcv.clip_x_origin = x;
3482 xgcv.clip_y_origin = y;
3483 xgcv.function = GXcopy;
3484 XChangeGC (s->display, s->gc, mask, &xgcv);
3485
3486 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3487 0, 0, s->img->width, s->img->height, x, y);
3488 XSetClipMask (s->display, s->gc, None);
3489 }
3490 else
3491 {
3492 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3493 0, 0, s->img->width, s->img->height, x, y);
3494
3495 /* When the image has a mask, we can expect that at
3496 least part of a mouse highlight or a block cursor will
3497 be visible. If the image doesn't have a mask, make
3498 a block cursor visible by drawing a rectangle around
3499 the image. I believe it's looking better if we do
3500 nothing here for mouse-face. */
3501 if (s->hl == DRAW_CURSOR)
3502 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3503 s->img->width - 1, s->img->height - 1);
3504 }
3505 }
3506 else
3507 /* Draw a rectangle if image could not be loaded. */
3508 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3509 s->img->width - 1, s->img->height - 1);
3510 }
3511
3512
3513 /* Draw part of the background of glyph string S. X, Y, W, and H
3514 give the rectangle to draw. */
3515
3516 static void
3517 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3518 struct glyph_string *s;
3519 int x, y, w, h;
3520 {
3521 if (s->stippled_p)
3522 {
3523 /* Fill background with a stipple pattern. */
3524 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3525 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3526 XSetFillStyle (s->display, s->gc, FillSolid);
3527 }
3528 else
3529 x_clear_glyph_string_rect (s, x, y, w, h);
3530 }
3531
3532
3533 /* Draw image glyph string S.
3534
3535 s->y
3536 s->x +-------------------------
3537 | s->face->box
3538 |
3539 | +-------------------------
3540 | | s->img->margin
3541 | |
3542 | | +-------------------
3543 | | | the image
3544
3545 */
3546
3547 static void
3548 x_draw_image_glyph_string (s)
3549 struct glyph_string *s;
3550 {
3551 int x, y;
3552 int box_line_width = s->face->box_line_width;
3553 int margin = s->img->margin;
3554 int height;
3555 Pixmap pixmap = None;
3556
3557 height = s->height - 2 * box_line_width;
3558
3559 /* Fill background with face under the image. Do it only if row is
3560 taller than image or if image has a clip mask to reduce
3561 flickering. */
3562 s->stippled_p = s->face->stipple != 0;
3563 if (height > s->img->height
3564 || margin
3565 || s->img->mask
3566 || s->img->pixmap == 0
3567 || s->width != s->background_width)
3568 {
3569 if (box_line_width && s->first_glyph->left_box_line_p)
3570 x = s->x + box_line_width;
3571 else
3572 x = s->x;
3573
3574 y = s->y + box_line_width;
3575
3576 if (s->img->mask)
3577 {
3578 /* Create a pixmap as large as the glyph string Fill it with
3579 the background color. Copy the image to it, using its
3580 mask. Copy the temporary pixmap to the display. */
3581 Screen *screen = FRAME_X_SCREEN (s->f);
3582 int depth = DefaultDepthOfScreen (screen);
3583
3584 /* Create a pixmap as large as the glyph string. */
3585 pixmap = XCreatePixmap (s->display, s->window,
3586 s->background_width,
3587 s->height, depth);
3588
3589 /* Don't clip in the following because we're working on the
3590 pixmap. */
3591 XSetClipMask (s->display, s->gc, None);
3592
3593 /* Fill the pixmap with the background color/stipple. */
3594 if (s->stippled_p)
3595 {
3596 /* Fill background with a stipple pattern. */
3597 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3598 XFillRectangle (s->display, pixmap, s->gc,
3599 0, 0, s->background_width, s->height);
3600 XSetFillStyle (s->display, s->gc, FillSolid);
3601 }
3602 else
3603 {
3604 XGCValues xgcv;
3605 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3606 &xgcv);
3607 XSetForeground (s->display, s->gc, xgcv.background);
3608 XFillRectangle (s->display, pixmap, s->gc,
3609 0, 0, s->background_width, s->height);
3610 XSetForeground (s->display, s->gc, xgcv.foreground);
3611 }
3612 }
3613 else
3614 /* Implementation idea: Is it possible to construct a mask?
3615 We could look at the color at the margins of the image, and
3616 say that this color is probably the background color of the
3617 image. */
3618 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3619
3620 s->background_filled_p = 1;
3621 }
3622
3623 /* Draw the foreground. */
3624 if (pixmap != None)
3625 {
3626 x_draw_image_foreground_1 (s, pixmap);
3627 x_set_glyph_string_clipping (s);
3628 XCopyArea (s->display, pixmap, s->window, s->gc,
3629 0, 0, s->background_width, s->height, s->x, s->y);
3630 XFreePixmap (s->display, pixmap);
3631 }
3632 else
3633 x_draw_image_foreground (s);
3634
3635 /* If we must draw a relief around the image, do it. */
3636 if (s->img->relief
3637 || s->hl == DRAW_IMAGE_RAISED
3638 || s->hl == DRAW_IMAGE_SUNKEN)
3639 x_draw_image_relief (s);
3640 }
3641
3642
3643 /* Draw stretch glyph string S. */
3644
3645 static void
3646 x_draw_stretch_glyph_string (s)
3647 struct glyph_string *s;
3648 {
3649 xassert (s->first_glyph->type == STRETCH_GLYPH);
3650 s->stippled_p = s->face->stipple != 0;
3651
3652 if (s->hl == DRAW_CURSOR
3653 && !x_stretch_cursor_p)
3654 {
3655 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3656 as wide as the stretch glyph. */
3657 int width = min (CANON_X_UNIT (s->f), s->background_width);
3658
3659 /* Draw cursor. */
3660 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3661
3662 /* Clear rest using the GC of the original non-cursor face. */
3663 if (width < s->background_width)
3664 {
3665 GC gc = s->face->gc;
3666 int x = s->x + width, y = s->y;
3667 int w = s->background_width - width, h = s->height;
3668 XRectangle r;
3669
3670 x_get_glyph_string_clip_rect (s, &r);
3671 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
3672
3673 if (s->face->stipple)
3674 {
3675 /* Fill background with a stipple pattern. */
3676 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3677 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3678 XSetFillStyle (s->display, gc, FillSolid);
3679 }
3680 else
3681 {
3682 XGCValues xgcv;
3683 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3684 XSetForeground (s->display, gc, xgcv.background);
3685 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3686 XSetForeground (s->display, gc, xgcv.foreground);
3687 }
3688 }
3689 }
3690 else
3691 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3692 s->height);
3693
3694 s->background_filled_p = 1;
3695 }
3696
3697
3698 /* Draw glyph string S. */
3699
3700 static void
3701 x_draw_glyph_string (s)
3702 struct glyph_string *s;
3703 {
3704 /* If S draws into the background of its successor, draw the
3705 background of the successor first so that S can draw into it.
3706 This makes S->next use XDrawString instead of XDrawImageString. */
3707 if (s->next && s->right_overhang && !s->for_overlaps_p)
3708 {
3709 xassert (s->next->img == NULL);
3710 x_set_glyph_string_gc (s->next);
3711 x_set_glyph_string_clipping (s->next);
3712 x_draw_glyph_string_background (s->next, 1);
3713 }
3714
3715 /* Set up S->gc, set clipping and draw S. */
3716 x_set_glyph_string_gc (s);
3717 x_set_glyph_string_clipping (s);
3718
3719 switch (s->first_glyph->type)
3720 {
3721 case IMAGE_GLYPH:
3722 x_draw_image_glyph_string (s);
3723 break;
3724
3725 case STRETCH_GLYPH:
3726 x_draw_stretch_glyph_string (s);
3727 break;
3728
3729 case CHAR_GLYPH:
3730 if (s->for_overlaps_p)
3731 s->background_filled_p = 1;
3732 else
3733 x_draw_glyph_string_background (s, 0);
3734 x_draw_glyph_string_foreground (s);
3735 break;
3736
3737 default:
3738 abort ();
3739 }
3740
3741 if (!s->for_overlaps_p)
3742 {
3743 /* Draw underline. */
3744 if (s->face->underline_p)
3745 {
3746 unsigned long dy, h;
3747
3748 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3749 h = 1;
3750 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3751 dy = s->height - h;
3752
3753 if (s->face->underline_defaulted_p)
3754 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3755 s->width, h);
3756 else
3757 {
3758 XGCValues xgcv;
3759 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3760 XSetForeground (s->display, s->gc, s->face->underline_color);
3761 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3762 s->width, h);
3763 XSetForeground (s->display, s->gc, xgcv.foreground);
3764 }
3765 }
3766
3767 /* Draw overline. */
3768 if (s->face->overline_p)
3769 {
3770 unsigned long dy = 0, h = 1;
3771
3772 if (s->face->overline_color_defaulted_p)
3773 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3774 s->width, h);
3775 else
3776 {
3777 XGCValues xgcv;
3778 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3779 XSetForeground (s->display, s->gc, s->face->overline_color);
3780 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3781 s->width, h);
3782 XSetForeground (s->display, s->gc, xgcv.foreground);
3783 }
3784 }
3785
3786 /* Draw strike-through. */
3787 if (s->face->strike_through_p)
3788 {
3789 unsigned long h = 1;
3790 unsigned long dy = (s->height - h) / 2;
3791
3792 if (s->face->strike_through_color_defaulted_p)
3793 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3794 s->width, h);
3795 else
3796 {
3797 XGCValues xgcv;
3798 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3799 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3800 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3801 s->width, h);
3802 XSetForeground (s->display, s->gc, xgcv.foreground);
3803 }
3804 }
3805
3806 /* Draw relief. */
3807 if (s->face->box != FACE_NO_BOX)
3808 x_draw_glyph_string_box (s);
3809 }
3810
3811 /* Reset clipping. */
3812 XSetClipMask (s->display, s->gc, None);
3813 }
3814
3815
3816 /* A work-list entry used during the construction of glyph_string
3817 structures for a composite character. */
3818
3819 struct work
3820 {
3821 /* Pointer to composite char info defining has the composite
3822 character is drawn. */
3823 struct cmpchar_info *cmpcharp;
3824
3825 /* Start index in compcharp->glyph[]. */
3826 int gidx;
3827
3828 /* Next in stack. */
3829 struct work *next;
3830 };
3831
3832
3833 static void x_fill_composite_glyph_string P_ ((struct glyph_string *,
3834 int, struct work **,
3835 struct work **, int));
3836
3837
3838 /* Load glyph string S with information from the top of *STACK for a
3839 composite character. FACE_ID is the id of the face in which S is
3840 drawn. *NEW is a pointer to a struct work not on the stack, that
3841 can be used if this function needs to push a new structure on the
3842 stack. If it uses it, *NEW is set to null. OVERLAPS_P non-zero
3843 means S should draw the foreground only, and use its lines physical
3844 height for clipping. */
3845
3846 static void
3847 x_fill_composite_glyph_string (s, face_id, stack, new, overlaps_p)
3848 struct glyph_string *s;
3849 int face_id;
3850 struct work **stack, **new;
3851 int overlaps_p;
3852 {
3853 int i, c;
3854 struct work *work;
3855
3856 xassert (s && *new && *stack);
3857
3858 s->for_overlaps_p = 1;
3859
3860 /* Pop the work stack. */
3861 work = *stack;
3862 *stack = work->next;
3863
3864 /* For all glyphs of cmpcharp->glyph, starting at the offset
3865 work->offset, until we reach the end of the definition or
3866 encounter another composite char, get the font and face to use,
3867 and add it to S. */
3868 for (i = work->gidx; i < work->cmpcharp->glyph_len; ++i)
3869 {
3870 c = FAST_GLYPH_CHAR (work->cmpcharp->glyph[i]);
3871 if (CHAR_CHARSET (c) == CHARSET_COMPOSITION)
3872 break;
3873 s->face = x_get_char_face_and_encoding (s->f, c, face_id,
3874 s->char2b + s->nchars, 1);
3875 s->font = s->face->font;
3876 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3877 ++s->nchars;
3878 }
3879
3880 /* If we find another composite char in the glyph definition of
3881 work->cmpcharp, put back the rest of the glyphs on the work
3882 stack, and make a new entry for the composite char. */
3883 if (i < work->cmpcharp->glyph_len)
3884 {
3885 /* Push back an unprocessed rest of this glyph spec. */
3886 if (i < work->cmpcharp->glyph_len - 1)
3887 {
3888 work->gidx = i + 1;
3889 work->next = *stack;
3890 *stack = work;
3891 work = *new;
3892 *new = NULL;
3893 }
3894
3895 /* Make an entry for the composite char on the work stack. */
3896 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)];
3897 work->gidx = 0;
3898 work->next = *stack;
3899 *stack = work;
3900 }
3901
3902 /* The width of this glyph string equals the width of the first
3903 glyph. All characters are drawn at the same x-position. */
3904 s->width = s->first_glyph->pixel_width;
3905
3906 /* If the specified font could not be loaded, use the frame's
3907 default font, but record the fact that we couldn't load it in
3908 the glyph string so that we can draw rectangles for the
3909 characters of the glyph string. */
3910 if (s->font == NULL)
3911 {
3912 s->font_not_found_p = 1;
3913 s->font = FRAME_FONT (s->f);
3914 }
3915
3916 /* Adjust base line for subscript/superscript text. */
3917 s->ybase += s->first_glyph->voffset;
3918
3919 xassert (s->face && s->face->gc);
3920
3921 /* This glyph string must always be drawn with 16-bit functions. */
3922 s->two_byte_p = 1;
3923 }
3924
3925
3926 /* Load glyph string S with a sequence of non-composite characters.
3927 FACE_ID is the face id of the string. START is the index of the
3928 first glyph to consider, END is the index of the last + 1.
3929 OVERLAPS_P non-zero means S should draw the foreground only, and
3930 use its lines physical height for clipping.
3931
3932 Value is the index of the first glyph not in S. */
3933
3934 static int
3935 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
3936 struct glyph_string *s;
3937 int face_id;
3938 int start, end, overlaps_p;
3939 {
3940 struct glyph *glyph, *last;
3941 int voffset;
3942
3943 xassert (s->charset != CHARSET_COMPOSITION);
3944 xassert (s->f == XFRAME (s->w->frame));
3945 xassert (s->nchars == 0);
3946 xassert (start >= 0 && end > start);
3947
3948 s->for_overlaps_p = overlaps_p,
3949 glyph = s->row->glyphs[s->area] + start;
3950 last = s->row->glyphs[s->area] + end;
3951 voffset = glyph->voffset;
3952
3953 while (glyph < last
3954 && glyph->type == CHAR_GLYPH
3955 && glyph->voffset == voffset
3956 /* Same face id implies same charset, nowadays. */
3957 && glyph->u.ch.face_id == face_id)
3958 {
3959 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
3960 s->char2b + s->nchars);
3961 if (s->char2b[s->nchars].byte2 != 0)
3962 s->two_byte_p = 1;
3963
3964 ++s->nchars;
3965 xassert (s->nchars <= end - start);
3966 s->width += glyph->pixel_width;
3967 ++glyph;
3968 }
3969
3970 s->font = s->face->font;
3971 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3972
3973 /* If the specified font could not be loaded, use the frame's font,
3974 but record the fact that we couldn't load it in
3975 S->font_not_found_p so that we can draw rectangles for the
3976 characters of the glyph string. */
3977 if (s->font == NULL)
3978 {
3979 s->font_not_found_p = 1;
3980 s->font = FRAME_FONT (s->f);
3981 }
3982
3983 /* Adjust base line for subscript/superscript text. */
3984 s->ybase += voffset;
3985
3986 xassert (s->face && s->face->gc);
3987 return glyph - s->row->glyphs[s->area];
3988 }
3989
3990
3991 /* Fill glyph string S from image glyph S->first_glyph. */
3992
3993 static void
3994 x_fill_image_glyph_string (s)
3995 struct glyph_string *s;
3996 {
3997 xassert (s->first_glyph->type == IMAGE_GLYPH);
3998 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img.id);
3999 xassert (s->img);
4000 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.img.face_id);
4001 s->font = s->face->font;
4002 s->width = s->first_glyph->pixel_width;
4003
4004 /* Adjust base line for subscript/superscript text. */
4005 s->ybase += s->first_glyph->voffset;
4006 }
4007
4008
4009 /* Fill glyph string S from stretch glyph S->first_glyph. */
4010
4011 static void
4012 x_fill_stretch_glyph_string (s)
4013 struct glyph_string *s;
4014 {
4015 xassert (s->first_glyph->type == STRETCH_GLYPH);
4016 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.stretch.face_id);
4017 s->font = s->face->font;
4018 s->width = s->first_glyph->pixel_width;
4019
4020 /* Adjust base line for subscript/superscript text. */
4021 s->ybase += s->first_glyph->voffset;
4022 }
4023
4024
4025 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4026 of XChar2b structures for S; it can't be allocated in
4027 x_init_glyph_string because it must be allocated via `alloca'. W
4028 is the window on which S is drawn. ROW and AREA are the glyph row
4029 and area within the row from which S is constructed. START is the
4030 index of the first glyph structure covered by S. HL is a
4031 face-override for drawing S. */
4032
4033 static void
4034 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4035 struct glyph_string *s;
4036 XChar2b *char2b;
4037 struct window *w;
4038 struct glyph_row *row;
4039 enum glyph_row_area area;
4040 int start;
4041 enum draw_glyphs_face hl;
4042 {
4043 bzero (s, sizeof *s);
4044 s->w = w;
4045 s->f = XFRAME (w->frame);
4046 s->display = FRAME_X_DISPLAY (s->f);
4047 s->window = FRAME_X_WINDOW (s->f);
4048 s->char2b = char2b;
4049 s->hl = hl;
4050 s->row = row;
4051 s->area = area;
4052 s->first_glyph = row->glyphs[area] + start;
4053 s->height = row->height;
4054 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4055
4056 /* Display the internal border below the toolbar window. */
4057 if (s->w == XWINDOW (s->f->toolbar_window))
4058 s->y -= s->f->output_data.x->internal_border_width;
4059
4060 s->ybase = s->y + row->ascent;
4061 }
4062
4063
4064 /* Set background width of glyph string S. START is the index of the
4065 first glyph following S. LAST_X is the right-most x-position + 1
4066 in the drawing area. */
4067
4068 static INLINE void
4069 x_set_glyph_string_background_width (s, start, last_x)
4070 struct glyph_string *s;
4071 int start;
4072 int last_x;
4073 {
4074 /* If the face of this glyph string has to be drawn to the end of
4075 the drawing area, set S->extends_to_end_of_line_p. */
4076 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4077
4078 if (start == s->row->used[s->area]
4079 && s->hl == DRAW_NORMAL_TEXT
4080 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4081 || s->face->background != default_face->background
4082 || s->face->stipple != default_face->stipple))
4083 s->extends_to_end_of_line_p = 1;
4084
4085 /* If S extends its face to the end of the line, set its
4086 background_width to the distance to the right edge of the drawing
4087 area. */
4088 if (s->extends_to_end_of_line_p)
4089 s->background_width = last_x - s->x + 1;
4090 else
4091 s->background_width = s->width;
4092 }
4093
4094
4095 /* Add a glyph string for a stretch glyph to the list of strings
4096 between HEAD and TAIL. START is the index of the stretch glyph in
4097 row area AREA of glyph row ROW. END is the index of the last glyph
4098 in that glyph row area. X is the current output position assigned
4099 to the new glyph string constructed. HL overrides that face of the
4100 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4101 is the right-most x-position of the drawing area. */
4102
4103 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, \
4104 HL, X, LAST_X) \
4105 do \
4106 { \
4107 s = (struct glyph_string *) alloca (sizeof *s); \
4108 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4109 x_fill_stretch_glyph_string (s); \
4110 x_append_glyph_string (&HEAD, &TAIL, s); \
4111 ++START; \
4112 s->x = (X); \
4113 } \
4114 while (0)
4115
4116
4117 /* Add a glyph string for an image glyph to the list of strings
4118 between HEAD and TAIL. START is the index of the image glyph in
4119 row area AREA of glyph row ROW. END is the index of the last glyph
4120 in that glyph row area. X is the current output position assigned
4121 to the new glyph string constructed. HL overrides that face of the
4122 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4123 is the right-most x-position of the drawing area. */
4124
4125 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, \
4126 HL, X, LAST_X) \
4127 do \
4128 { \
4129 s = (struct glyph_string *) alloca (sizeof *s); \
4130 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4131 x_fill_image_glyph_string (s); \
4132 x_append_glyph_string (&HEAD, &TAIL, s); \
4133 ++START; \
4134 s->x = (X); \
4135 } \
4136 while (0)
4137
4138
4139 /* Add a glyph string for a sequence of character glyphs to the list
4140 of strings between HEAD and TAIL. START is the index of the first
4141 glyph in row area AREA of glyph row ROW that is part of the new
4142 glyph string. END is the index of the last glyph in that glyph row
4143 area. X is the current output position assigned to the new glyph
4144 string constructed. HL overrides that face of the glyph; e.g. it
4145 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4146 right-most x-position of the drawing area. */
4147
4148 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, \
4149 X, LAST_X, OVERLAPS_P) \
4150 do \
4151 { \
4152 int c, charset, face_id; \
4153 XChar2b *char2b; \
4154 \
4155 c = (ROW)->glyphs[AREA][START].u.ch.code; \
4156 charset = CHAR_CHARSET (c); \
4157 face_id = (ROW)->glyphs[AREA][START].u.ch.face_id; \
4158 \
4159 if (charset == CHARSET_COMPOSITION) \
4160 { \
4161 struct work *stack, *work, *new = NULL; \
4162 int n = 0; \
4163 struct glyph_string *first_s = NULL; \
4164 \
4165 /* Push an initial entry for character c on the stack. */ \
4166 stack = NULL; \
4167 work = (struct work *) alloca (sizeof *work); \
4168 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)]; \
4169 work->gidx = 0; \
4170 work->next = stack; \
4171 stack = work; \
4172 \
4173 /* While the stack is not empty, append glyph_strings \
4174 to head/tail for glyphs to draw. */ \
4175 while (stack) \
4176 { \
4177 s = (struct glyph_string *) alloca (sizeof *s); \
4178 char2b = (XChar2b *) alloca (stack->cmpcharp->glyph_len \
4179 * sizeof (XChar2b)); \
4180 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4181 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4182 s->cmpcharp = stack->cmpcharp; \
4183 s->gidx = stack->gidx; \
4184 s->charset = charset; \
4185 s->x = (X); \
4186 \
4187 if (n == 0) \
4188 { \
4189 /* Don't draw the background except for the \
4190 first glyph string. */ \
4191 s->background_filled_p = n > 0; \
4192 first_s = s; \
4193 } \
4194 ++n; \
4195 \
4196 if (new == NULL) \
4197 new = (struct work *) alloca (sizeof *new); \
4198 x_fill_composite_glyph_string (s, face_id, &stack, \
4199 &new, OVERLAPS_P); \
4200 } \
4201 \
4202 ++START; \
4203 s = first_s; \
4204 } \
4205 else \
4206 { \
4207 s = (struct glyph_string *) alloca (sizeof *s); \
4208 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4209 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4210 x_append_glyph_string (&HEAD, &TAIL, s); \
4211 s->charset = charset; \
4212 s->x = (X); \
4213 START = x_fill_glyph_string (s, face_id, START, END, \
4214 OVERLAPS_P); \
4215 } \
4216 } \
4217 while (0)
4218
4219
4220 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4221 of AREA of glyph row ROW on window W between indices START and END.
4222 HL overrides the face for drawing glyph strings, e.g. it is
4223 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4224 x-positions of the drawing area.
4225
4226 This is an ugly monster macro construct because we must use alloca
4227 to allocate glyph strings (because x_draw_glyphs can be called
4228 asynchronously). */
4229
4230 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, \
4231 X, LAST_X, OVERLAPS_P) \
4232 do \
4233 { \
4234 HEAD = TAIL = NULL; \
4235 while (START < END) \
4236 { \
4237 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4238 switch (first_glyph->type) \
4239 { \
4240 case CHAR_GLYPH: \
4241 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4242 TAIL, HL, X, LAST_X, \
4243 OVERLAPS_P); \
4244 break; \
4245 \
4246 case STRETCH_GLYPH: \
4247 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4248 HEAD, TAIL, HL, X, LAST_X); \
4249 break; \
4250 \
4251 case IMAGE_GLYPH: \
4252 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4253 TAIL, HL, X, LAST_X); \
4254 break; \
4255 \
4256 default: \
4257 abort (); \
4258 } \
4259 \
4260 x_set_glyph_string_background_width (s, START, LAST_X); \
4261 (X) += s->width; \
4262 } \
4263 } \
4264 while (0)
4265
4266
4267 /* Draw glyphs between START and END in AREA of ROW on window W,
4268 starting at x-position X. X is relative to AREA in W. HL is a
4269 face-override with the following meaning:
4270
4271 DRAW_NORMAL_TEXT draw normally
4272 DRAW_CURSOR draw in cursor face
4273 DRAW_MOUSE_FACE draw in mouse face.
4274 DRAW_INVERSE_VIDEO draw in mode line face
4275 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4276 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4277
4278 If REAL_START is non-null, return in *REAL_START the real starting
4279 position for display. This can be different from START in case
4280 overlapping glyphs must be displayed. If REAL_END is non-null,
4281 return in *REAL_END the real end position for display. This can be
4282 different from END in case overlapping glyphs must be displayed.
4283
4284 If OVERLAPS_P is non-zero, draw only the foreground of characters
4285 and clip to the physical height of ROW.
4286
4287 Value is the x-position reached, relative to AREA of W. */
4288
4289 static int
4290 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4291 overlaps_p)
4292 struct window *w;
4293 int x;
4294 struct glyph_row *row;
4295 enum glyph_row_area area;
4296 int start, end;
4297 enum draw_glyphs_face hl;
4298 int *real_start, *real_end;
4299 int overlaps_p;
4300 {
4301 struct glyph_string *head, *tail;
4302 struct glyph_string *s;
4303 int last_x, area_width;
4304 int x_reached;
4305 int i, j;
4306
4307 /* Let's rather be paranoid than getting a SEGV. */
4308 start = max (0, start);
4309 end = min (end, row->used[area]);
4310 if (real_start)
4311 *real_start = start;
4312 if (real_end)
4313 *real_end = end;
4314
4315 /* Translate X to frame coordinates. Set last_x to the right
4316 end of the drawing area. */
4317 if (row->full_width_p)
4318 {
4319 /* X is relative to the left edge of W, without scroll bars
4320 or flag areas. */
4321 struct frame *f = XFRAME (w->frame);
4322 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4323 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4324
4325 x += window_left_x;
4326 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4327 last_x = window_left_x + area_width;
4328
4329 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4330 {
4331 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4332 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4333 last_x += width;
4334 else
4335 x -= width;
4336 }
4337
4338 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4339 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4340 }
4341 else
4342 {
4343 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4344 area_width = window_box_width (w, area);
4345 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4346 }
4347
4348 /* Build a doubly-linked list of glyph_string structures between
4349 head and tail from what we have to draw. Note that the macro
4350 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4351 the reason we use a separate variable `i'. */
4352 i = start;
4353 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4354 overlaps_p);
4355 if (tail)
4356 x_reached = tail->x + tail->background_width;
4357 else
4358 x_reached = x;
4359
4360 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4361 the row, redraw some glyphs in front or following the glyph
4362 strings built above. */
4363 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4364 {
4365 int dummy_x = 0;
4366 struct glyph_string *h, *t;
4367
4368 /* Compute overhangs for all glyph strings. */
4369 for (s = head; s; s = s->next)
4370 x_compute_glyph_string_overhangs (s);
4371
4372 /* Prepend glyph strings for glyphs in front of the first glyph
4373 string that are overwritten because of the first glyph
4374 string's left overhang. The background of all strings
4375 prepended must be drawn because the first glyph string
4376 draws over it. */
4377 i = x_left_overwritten (head);
4378 if (i >= 0)
4379 {
4380 j = i;
4381 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4382 DRAW_NORMAL_TEXT, dummy_x, last_x,
4383 overlaps_p);
4384 start = i;
4385 if (real_start)
4386 *real_start = start;
4387 x_compute_overhangs_and_x (t, head->x, 1);
4388 x_prepend_glyph_string_lists (&head, &tail, h, t);
4389 }
4390
4391 /* Prepend glyph strings for glyphs in front of the first glyph
4392 string that overwrite that glyph string because of their
4393 right overhang. For these strings, only the foreground must
4394 be drawn, because it draws over the glyph string at `head'.
4395 The background must not be drawn because this would overwrite
4396 right overhangs of preceding glyphs for which no glyph
4397 strings exist. */
4398 i = x_left_overwriting (head);
4399 if (i >= 0)
4400 {
4401 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4402 DRAW_NORMAL_TEXT, dummy_x, last_x,
4403 overlaps_p);
4404 for (s = h; s; s = s->next)
4405 s->background_filled_p = 1;
4406 if (real_start)
4407 *real_start = i;
4408 x_compute_overhangs_and_x (t, head->x, 1);
4409 x_prepend_glyph_string_lists (&head, &tail, h, t);
4410 }
4411
4412 /* Append glyphs strings for glyphs following the last glyph
4413 string tail that are overwritten by tail. The background of
4414 these strings has to be drawn because tail's foreground draws
4415 over it. */
4416 i = x_right_overwritten (tail);
4417 if (i >= 0)
4418 {
4419 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4420 DRAW_NORMAL_TEXT, x, last_x,
4421 overlaps_p);
4422 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4423 x_append_glyph_string_lists (&head, &tail, h, t);
4424 if (real_end)
4425 *real_end = i;
4426 }
4427
4428 /* Append glyph strings for glyphs following the last glyph
4429 string tail that overwrite tail. The foreground of such
4430 glyphs has to be drawn because it writes into the background
4431 of tail. The background must not be drawn because it could
4432 paint over the foreground of following glyphs. */
4433 i = x_right_overwriting (tail);
4434 if (i >= 0)
4435 {
4436 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4437 DRAW_NORMAL_TEXT, x, last_x,
4438 overlaps_p);
4439 for (s = h; s; s = s->next)
4440 s->background_filled_p = 1;
4441 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4442 x_append_glyph_string_lists (&head, &tail, h, t);
4443 if (real_end)
4444 *real_end = i;
4445 }
4446 }
4447
4448 /* Draw all strings. */
4449 for (s = head; s; s = s->next)
4450 x_draw_glyph_string (s);
4451
4452 /* Value is the x-position up to which drawn, relative to AREA of W.
4453 This doesn't include parts drawn because of overhangs. */
4454 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4455 if (!row->full_width_p)
4456 {
4457 if (area > LEFT_MARGIN_AREA)
4458 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4459 if (area > TEXT_AREA)
4460 x_reached -= window_box_width (w, TEXT_AREA);
4461 }
4462 return x_reached;
4463 }
4464
4465
4466 /* Fix the display of area AREA of overlapping row ROW in window W. */
4467
4468 static void
4469 x_fix_overlapping_area (w, row, area)
4470 struct window *w;
4471 struct glyph_row *row;
4472 enum glyph_row_area area;
4473 {
4474 int i, x;
4475
4476 BLOCK_INPUT;
4477
4478 if (area == LEFT_MARGIN_AREA)
4479 x = 0;
4480 else if (area == TEXT_AREA)
4481 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4482 else
4483 x = (window_box_width (w, LEFT_MARGIN_AREA)
4484 + window_box_width (w, TEXT_AREA));
4485
4486 for (i = 0; i < row->used[area];)
4487 {
4488 if (row->glyphs[area][i].overlaps_vertically_p)
4489 {
4490 int start = i, start_x = x;
4491
4492 do
4493 {
4494 x += row->glyphs[area][i].pixel_width;
4495 ++i;
4496 }
4497 while (i < row->used[area]
4498 && row->glyphs[area][i].overlaps_vertically_p);
4499
4500 x_draw_glyphs (w, start_x, row, area, start, i,
4501 (row->inverse_p
4502 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4503 NULL, NULL, 1);
4504 }
4505 else
4506 {
4507 x += row->glyphs[area][i].pixel_width;
4508 ++i;
4509 }
4510 }
4511
4512 UNBLOCK_INPUT;
4513 }
4514
4515
4516 /* Output LEN glyphs starting at START at the nominal cursor position.
4517 Advance the nominal cursor over the text. The global variable
4518 updated_window contains the window being updated, updated_row is
4519 the glyph row being updated, and updated_area is the area of that
4520 row being updated. */
4521
4522 static void
4523 x_write_glyphs (start, len)
4524 struct glyph *start;
4525 int len;
4526 {
4527 int x, hpos, real_start, real_end;
4528
4529 xassert (updated_window && updated_row);
4530 BLOCK_INPUT;
4531
4532 /* Write glyphs. */
4533
4534 hpos = start - updated_row->glyphs[updated_area];
4535 x = x_draw_glyphs (updated_window, output_cursor.x,
4536 updated_row, updated_area,
4537 hpos, hpos + len,
4538 (updated_row->inverse_p
4539 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4540 &real_start, &real_end, 0);
4541
4542 /* If we drew over the cursor, note that it is not visible any more. */
4543 note_overwritten_text_cursor (updated_window, real_start,
4544 real_end - real_start);
4545
4546 UNBLOCK_INPUT;
4547
4548 /* Advance the output cursor. */
4549 output_cursor.hpos += len;
4550 output_cursor.x = x;
4551 }
4552
4553
4554 /* Insert LEN glyphs from START at the nominal cursor position. */
4555
4556 static void
4557 x_insert_glyphs (start, len)
4558 struct glyph *start;
4559 register int len;
4560 {
4561 struct frame *f;
4562 struct window *w;
4563 int line_height, shift_by_width, shifted_region_width;
4564 struct glyph_row *row;
4565 struct glyph *glyph;
4566 int frame_x, frame_y, hpos, real_start, real_end;
4567
4568 xassert (updated_window && updated_row);
4569 BLOCK_INPUT;
4570 w = updated_window;
4571 f = XFRAME (WINDOW_FRAME (w));
4572
4573 /* Get the height of the line we are in. */
4574 row = updated_row;
4575 line_height = row->height;
4576
4577 /* Get the width of the glyphs to insert. */
4578 shift_by_width = 0;
4579 for (glyph = start; glyph < start + len; ++glyph)
4580 shift_by_width += glyph->pixel_width;
4581
4582 /* Get the width of the region to shift right. */
4583 shifted_region_width = (window_box_width (w, updated_area)
4584 - output_cursor.x
4585 - shift_by_width);
4586
4587 /* Shift right. */
4588 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4589 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4590 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4591 f->output_data.x->normal_gc,
4592 frame_x, frame_y,
4593 shifted_region_width, line_height,
4594 frame_x + shift_by_width, frame_y);
4595
4596 /* Write the glyphs. */
4597 hpos = start - row->glyphs[updated_area];
4598 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4599 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4600 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4601
4602 /* Advance the output cursor. */
4603 output_cursor.hpos += len;
4604 output_cursor.x += shift_by_width;
4605 UNBLOCK_INPUT;
4606 }
4607
4608
4609 /* Delete N glyphs at the nominal cursor position. Not implemented
4610 for X frames. */
4611
4612 static void
4613 x_delete_glyphs (n)
4614 register int n;
4615 {
4616 abort ();
4617 }
4618
4619
4620 /* Erase the current text line from the nominal cursor position
4621 (inclusive) to pixel column TO_X (exclusive). The idea is that
4622 everything from TO_X onward is already erased.
4623
4624 TO_X is a pixel position relative to updated_area of
4625 updated_window. TO_X == -1 means clear to the end of this area. */
4626
4627 static void
4628 x_clear_end_of_line (to_x)
4629 int to_x;
4630 {
4631 struct frame *f;
4632 struct window *w = updated_window;
4633 int max_x, min_y, max_y;
4634 int from_x, from_y, to_y;
4635
4636 xassert (updated_window && updated_row);
4637 f = XFRAME (w->frame);
4638
4639 if (updated_row->full_width_p)
4640 {
4641 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4642 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4643 && !w->pseudo_window_p)
4644 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4645 }
4646 else
4647 max_x = window_box_width (w, updated_area);
4648 max_y = window_text_bottom_y (w);
4649
4650 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4651 of window. For TO_X > 0, truncate to end of drawing area. */
4652 if (to_x == 0)
4653 return;
4654 else if (to_x < 0)
4655 to_x = max_x;
4656 else
4657 to_x = min (to_x, max_x);
4658
4659 to_y = min (max_y, output_cursor.y + updated_row->height);
4660
4661 /* Notice if the cursor will be cleared by this operation. */
4662 if (!updated_row->full_width_p)
4663 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
4664
4665 from_x = output_cursor.x;
4666
4667 /* Translate to frame coordinates. */
4668 if (updated_row->full_width_p)
4669 {
4670 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4671 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4672 }
4673 else
4674 {
4675 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4676 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4677 }
4678
4679 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
4680 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4681 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4682
4683 /* Prevent inadvertently clearing to end of the X window. */
4684 if (to_x > from_x && to_y > from_y)
4685 {
4686 BLOCK_INPUT;
4687 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4688 from_x, from_y, to_x - from_x, to_y - from_y,
4689 False);
4690 UNBLOCK_INPUT;
4691 }
4692 }
4693
4694
4695 /* Clear entire frame. If updating_frame is non-null, clear that
4696 frame. Otherwise clear selected_frame. */
4697
4698 static void
4699 x_clear_frame ()
4700 {
4701 struct frame *f;
4702
4703 if (updating_frame)
4704 f = updating_frame;
4705 else
4706 f = selected_frame;
4707
4708 /* Clearing the frame will erase any cursor, so mark them all as no
4709 longer visible. */
4710 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4711 output_cursor.hpos = output_cursor.vpos = 0;
4712 output_cursor.x = -1;
4713
4714 /* We don't set the output cursor here because there will always
4715 follow an explicit cursor_to. */
4716 BLOCK_INPUT;
4717 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4718
4719 /* We have to clear the scroll bars, too. If we have changed
4720 colors or something like that, then they should be notified. */
4721 x_scroll_bar_clear (f);
4722
4723 XFlush (FRAME_X_DISPLAY (f));
4724 UNBLOCK_INPUT;
4725 }
4726
4727
4728 \f
4729 /* Invert the middle quarter of the frame for .15 sec. */
4730
4731 /* We use the select system call to do the waiting, so we have to make
4732 sure it's available. If it isn't, we just won't do visual bells. */
4733
4734 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4735
4736
4737 /* Subtract the `struct timeval' values X and Y, storing the result in
4738 *RESULT. Return 1 if the difference is negative, otherwise 0. */
4739
4740 static int
4741 timeval_subtract (result, x, y)
4742 struct timeval *result, x, y;
4743 {
4744 /* Perform the carry for the later subtraction by updating y. This
4745 is safer because on some systems the tv_sec member is unsigned. */
4746 if (x.tv_usec < y.tv_usec)
4747 {
4748 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4749 y.tv_usec -= 1000000 * nsec;
4750 y.tv_sec += nsec;
4751 }
4752
4753 if (x.tv_usec - y.tv_usec > 1000000)
4754 {
4755 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4756 y.tv_usec += 1000000 * nsec;
4757 y.tv_sec -= nsec;
4758 }
4759
4760 /* Compute the time remaining to wait. tv_usec is certainly
4761 positive. */
4762 result->tv_sec = x.tv_sec - y.tv_sec;
4763 result->tv_usec = x.tv_usec - y.tv_usec;
4764
4765 /* Return indication of whether the result should be considered
4766 negative. */
4767 return x.tv_sec < y.tv_sec;
4768 }
4769
4770 void
4771 XTflash (f)
4772 struct frame *f;
4773 {
4774 BLOCK_INPUT;
4775
4776 {
4777 GC gc;
4778
4779 /* Create a GC that will use the GXxor function to flip foreground
4780 pixels into background pixels. */
4781 {
4782 XGCValues values;
4783
4784 values.function = GXxor;
4785 values.foreground = (f->output_data.x->foreground_pixel
4786 ^ f->output_data.x->background_pixel);
4787
4788 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4789 GCFunction | GCForeground, &values);
4790 }
4791
4792 {
4793 /* Get the height not including a menu bar widget. */
4794 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4795 /* Height of each line to flash. */
4796 int flash_height = FRAME_LINE_HEIGHT (f);
4797 /* These will be the left and right margins of the rectangles. */
4798 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4799 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4800
4801 int width;
4802
4803 /* Don't flash the area between a scroll bar and the frame
4804 edge it is next to. */
4805 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4806 {
4807 case vertical_scroll_bar_left:
4808 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4809 break;
4810
4811 case vertical_scroll_bar_right:
4812 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4813 break;
4814
4815 default:
4816 break;
4817 }
4818
4819 width = flash_right - flash_left;
4820
4821 /* If window is tall, flash top and bottom line. */
4822 if (height > 3 * FRAME_LINE_HEIGHT (f))
4823 {
4824 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4825 flash_left,
4826 (FRAME_INTERNAL_BORDER_WIDTH (f)
4827 + FRAME_TOOLBAR_LINES (f) * CANON_Y_UNIT (f)),
4828 width, flash_height);
4829 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4830 flash_left,
4831 (height - flash_height
4832 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4833 width, flash_height);
4834 }
4835 else
4836 /* If it is short, flash it all. */
4837 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4838 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4839 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4840
4841 x_flush (f);
4842
4843 {
4844 struct timeval wakeup;
4845
4846 EMACS_GET_TIME (wakeup);
4847
4848 /* Compute time to wait until, propagating carry from usecs. */
4849 wakeup.tv_usec += 150000;
4850 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
4851 wakeup.tv_usec %= 1000000;
4852
4853 /* Keep waiting until past the time wakeup. */
4854 while (1)
4855 {
4856 struct timeval timeout;
4857
4858 EMACS_GET_TIME (timeout);
4859
4860 /* In effect, timeout = wakeup - timeout.
4861 Break if result would be negative. */
4862 if (timeval_subtract (&timeout, wakeup, timeout))
4863 break;
4864
4865 /* Try to wait that long--but we might wake up sooner. */
4866 select (0, NULL, NULL, NULL, &timeout);
4867 }
4868 }
4869
4870 /* If window is tall, flash top and bottom line. */
4871 if (height > 3 * FRAME_LINE_HEIGHT (f))
4872 {
4873 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4874 flash_left,
4875 (FRAME_INTERNAL_BORDER_WIDTH (f)
4876 + FRAME_TOOLBAR_LINES (f) * CANON_Y_UNIT (f)),
4877 width, flash_height);
4878 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4879 flash_left,
4880 (height - flash_height
4881 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4882 width, flash_height);
4883 }
4884 else
4885 /* If it is short, flash it all. */
4886 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4887 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4888 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4889
4890 XFreeGC (FRAME_X_DISPLAY (f), gc);
4891 x_flush (f);
4892 }
4893 }
4894
4895 UNBLOCK_INPUT;
4896 }
4897
4898 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
4899
4900
4901 /* Make audible bell. */
4902
4903 #define XRINGBELL XBell (FRAME_X_DISPLAY (selected_frame), 0)
4904
4905 void
4906 XTring_bell ()
4907 {
4908 if (FRAME_X_DISPLAY (selected_frame) == 0)
4909 return;
4910
4911 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4912 if (visible_bell)
4913 XTflash (selected_frame);
4914 else
4915 #endif
4916 {
4917 BLOCK_INPUT;
4918 XRINGBELL;
4919 XFlush (FRAME_X_DISPLAY (selected_frame));
4920 UNBLOCK_INPUT;
4921 }
4922 }
4923
4924 \f
4925 /* Specify how many text lines, from the top of the window,
4926 should be affected by insert-lines and delete-lines operations.
4927 This, and those operations, are used only within an update
4928 that is bounded by calls to x_update_begin and x_update_end. */
4929
4930 static void
4931 XTset_terminal_window (n)
4932 register int n;
4933 {
4934 /* This function intentionally left blank. */
4935 }
4936
4937
4938 \f
4939 /***********************************************************************
4940 Line Dance
4941 ***********************************************************************/
4942
4943 /* Perform an insert-lines or delete-lines operation, inserting N
4944 lines or deleting -N lines at vertical position VPOS. */
4945
4946 static void
4947 x_ins_del_lines (vpos, n)
4948 int vpos, n;
4949 {
4950 abort ();
4951 }
4952
4953
4954 /* Scroll part of the display as described by RUN. */
4955
4956 static void
4957 x_scroll_run (w, run)
4958 struct window *w;
4959 struct run *run;
4960 {
4961 struct frame *f = XFRAME (w->frame);
4962 int x, y, width, height, from_y, to_y, bottom_y;
4963
4964 /* Get frame-relative bounding box of the text display area of W,
4965 without mode lines. Include in this box the flags areas to the
4966 left and right of W. */
4967 window_box (w, -1, &x, &y, &width, &height);
4968 width += FRAME_X_FLAGS_AREA_WIDTH (f);
4969 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
4970
4971 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4972 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4973 bottom_y = y + height;
4974
4975 if (to_y < from_y)
4976 {
4977 /* Scrolling up. Make sure we don't copy part of the mode
4978 line at the bottom. */
4979 if (from_y + run->height > bottom_y)
4980 height = bottom_y - from_y;
4981 else
4982 height = run->height;
4983 }
4984 else
4985 {
4986 /* Scolling down. Make sure we don't copy over the mode line.
4987 at the bottom. */
4988 if (to_y + run->height > bottom_y)
4989 height = bottom_y - to_y;
4990 else
4991 height = run->height;
4992 }
4993
4994 BLOCK_INPUT;
4995
4996 /* Cursor off. Will be switched on again in x_update_window_end. */
4997 updated_window = w;
4998 x_clear_cursor (w);
4999
5000 XCopyArea (FRAME_X_DISPLAY (f),
5001 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5002 f->output_data.x->normal_gc,
5003 x, from_y,
5004 width, height,
5005 x, to_y);
5006
5007 UNBLOCK_INPUT;
5008 }
5009
5010
5011 \f
5012 /***********************************************************************
5013 Exposure Events
5014 ***********************************************************************/
5015
5016 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5017 corner of the exposed rectangle. W and H are width and height of
5018 the exposed area. All are pixel values. W or H zero means redraw
5019 the entire frame. */
5020
5021 static void
5022 expose_frame (f, x, y, w, h)
5023 struct frame *f;
5024 int x, y, w, h;
5025 {
5026 XRectangle r;
5027
5028 TRACE ((stderr, "expose_frame "));
5029
5030 /* No need to redraw if frame will be redrawn soon. */
5031 if (FRAME_GARBAGED_P (f))
5032 {
5033 TRACE ((stderr, " garbaged\n"));
5034 return;
5035 }
5036
5037 /* If basic faces haven't been realized yet, there is no point in
5038 trying to redraw anything. This can happen when we get an expose
5039 event while Emacs is starting, e.g. by moving another window. */
5040 if (FRAME_FACE_CACHE (f) == NULL
5041 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5042 {
5043 TRACE ((stderr, " no faces\n"));
5044 return;
5045 }
5046
5047 if (w == 0 || h == 0)
5048 {
5049 r.x = r.y = 0;
5050 r.width = CANON_X_UNIT (f) * f->width;
5051 r.height = CANON_Y_UNIT (f) * f->height;
5052 }
5053 else
5054 {
5055 r.x = x;
5056 r.y = y;
5057 r.width = w;
5058 r.height = h;
5059 }
5060
5061 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5062 expose_window_tree (XWINDOW (f->root_window), &r);
5063
5064 if (WINDOWP (f->toolbar_window))
5065 {
5066 struct window *w = XWINDOW (f->toolbar_window);
5067 XRectangle window_rect;
5068 XRectangle intersection_rect;
5069 int window_x, window_y, window_width, window_height;
5070
5071
5072 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5073 window_rect.x = window_x;
5074 window_rect.y = window_y;
5075 window_rect.width = window_width;
5076 window_rect.height = window_height;
5077
5078 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5079 expose_window (w, &intersection_rect);
5080 }
5081
5082 #ifndef USE_X_TOOLKIT
5083 if (WINDOWP (f->menu_bar_window))
5084 {
5085 struct window *w = XWINDOW (f->menu_bar_window);
5086 XRectangle window_rect;
5087 XRectangle intersection_rect;
5088 int window_x, window_y, window_width, window_height;
5089
5090
5091 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5092 window_rect.x = window_x;
5093 window_rect.y = window_y;
5094 window_rect.width = window_width;
5095 window_rect.height = window_height;
5096
5097 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5098 expose_window (w, &intersection_rect);
5099 }
5100 #endif /* not USE_X_TOOLKIT */
5101 }
5102
5103
5104 /* Redraw (parts) of all windows in the window tree rooted at W that
5105 intersect R. R contains frame pixel coordinates. */
5106
5107 static void
5108 expose_window_tree (w, r)
5109 struct window *w;
5110 XRectangle *r;
5111 {
5112 while (w)
5113 {
5114 if (!NILP (w->hchild))
5115 expose_window_tree (XWINDOW (w->hchild), r);
5116 else if (!NILP (w->vchild))
5117 expose_window_tree (XWINDOW (w->vchild), r);
5118 else
5119 {
5120 XRectangle window_rect;
5121 XRectangle intersection_rect;
5122 struct frame *f = XFRAME (w->frame);
5123 int window_x, window_y, window_width, window_height;
5124
5125 /* Frame-relative pixel rectangle of W. */
5126 window_box (w, -1, &window_x, &window_y, &window_width,
5127 &window_height);
5128 window_rect.x
5129 = (window_x
5130 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5131 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5132 window_rect.y = window_y;
5133 window_rect.width
5134 = (window_width
5135 + FRAME_X_FLAGS_AREA_WIDTH (f)
5136 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5137 window_rect.height
5138 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5139
5140 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5141 expose_window (w, &intersection_rect);
5142 }
5143
5144 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5145 }
5146 }
5147
5148
5149 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5150 which intersects rectangle R. R is in window-relative coordinates. */
5151
5152 static void
5153 expose_area (w, row, r, area)
5154 struct window *w;
5155 struct glyph_row *row;
5156 XRectangle *r;
5157 enum glyph_row_area area;
5158 {
5159 int x;
5160 struct glyph *first = row->glyphs[area];
5161 struct glyph *end = row->glyphs[area] + row->used[area];
5162 struct glyph *last;
5163 int first_x;
5164
5165 /* Set x to the window-relative start position for drawing glyphs of
5166 AREA. The first glyph of the text area can be partially visible.
5167 The first glyphs of other areas cannot. */
5168 if (area == LEFT_MARGIN_AREA)
5169 x = 0;
5170 else if (area == TEXT_AREA)
5171 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5172 else
5173 x = (window_box_width (w, LEFT_MARGIN_AREA)
5174 + window_box_width (w, TEXT_AREA));
5175
5176 if (area == TEXT_AREA && row->fill_line_p)
5177 /* If row extends face to end of line write the whole line. */
5178 x_draw_glyphs (w, x, row, area,
5179 0, row->used[area],
5180 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5181 NULL, NULL, 0);
5182 else
5183 {
5184 /* Find the first glyph that must be redrawn. */
5185 while (first < end
5186 && x + first->pixel_width < r->x)
5187 {
5188 x += first->pixel_width;
5189 ++first;
5190 }
5191
5192 /* Find the last one. */
5193 last = first;
5194 first_x = x;
5195 while (last < end
5196 && x < r->x + r->width)
5197 {
5198 x += last->pixel_width;
5199 ++last;
5200 }
5201
5202 /* Repaint. */
5203 if (last > first)
5204 x_draw_glyphs (w, first_x, row, area,
5205 first - row->glyphs[area],
5206 last - row->glyphs[area],
5207 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5208 NULL, NULL, 0);
5209 }
5210 }
5211
5212
5213 /* Redraw the parts of the glyph row ROW on window W intersecting
5214 rectangle R. R is in window-relative coordinates. */
5215
5216 static void
5217 expose_line (w, row, r)
5218 struct window *w;
5219 struct glyph_row *row;
5220 XRectangle *r;
5221 {
5222 xassert (row->enabled_p);
5223
5224 if (row->mode_line_p || w->pseudo_window_p)
5225 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5226 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5227 NULL, NULL, 0);
5228 else
5229 {
5230 if (row->used[LEFT_MARGIN_AREA])
5231 expose_area (w, row, r, LEFT_MARGIN_AREA);
5232 if (row->used[TEXT_AREA])
5233 expose_area (w, row, r, TEXT_AREA);
5234 if (row->used[RIGHT_MARGIN_AREA])
5235 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5236 x_draw_row_bitmaps (w, row);
5237 }
5238 }
5239
5240
5241 /* Return non-zero if W's cursor intersects rectangle R. */
5242
5243 static int
5244 x_phys_cursor_in_rect_p (w, r)
5245 struct window *w;
5246 XRectangle *r;
5247 {
5248 XRectangle cr, result;
5249 struct glyph *cursor_glyph;
5250
5251 cursor_glyph = get_phys_cursor_glyph (w);
5252 if (cursor_glyph)
5253 {
5254 cr.x = w->phys_cursor.x;
5255 cr.y = w->phys_cursor.y;
5256 cr.width = cursor_glyph->pixel_width;
5257 cr.height = w->phys_cursor_height;
5258 return x_intersect_rectangles (&cr, r, &result);
5259 }
5260 else
5261 return 0;
5262 }
5263
5264
5265 /* Redraw a rectangle of window W. R is a rectangle in window
5266 relative coordinates. Call this function with input blocked. */
5267
5268 static void
5269 expose_window (w, r)
5270 struct window *w;
5271 XRectangle *r;
5272 {
5273 struct glyph_row *row;
5274 int y;
5275 int yb = window_text_bottom_y (w);
5276 int cursor_cleared_p;
5277
5278 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5279 r->x, r->y, r->width, r->height));
5280
5281 /* Convert to window coordinates. */
5282 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5283 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5284
5285 /* Turn off the cursor. */
5286 if (!w->pseudo_window_p
5287 && x_phys_cursor_in_rect_p (w, r))
5288 {
5289 x_clear_cursor (w);
5290 cursor_cleared_p = 1;
5291 }
5292 else
5293 cursor_cleared_p = 0;
5294
5295 /* Find the first row intersecting the rectangle R. */
5296 row = w->current_matrix->rows;
5297 y = 0;
5298 while (row->enabled_p
5299 && y < yb
5300 && y + row->height < r->y)
5301 {
5302 y += row->height;
5303 ++row;
5304 }
5305
5306 /* Display the text in the rectangle, one text line at a time. */
5307 while (row->enabled_p
5308 && y < yb
5309 && y < r->y + r->height)
5310 {
5311 expose_line (w, row, r);
5312 y += row->height;
5313 ++row;
5314 }
5315
5316 /* Display the mode line if there is one. */
5317 if (WINDOW_WANTS_MODELINE_P (w)
5318 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5319 row->enabled_p)
5320 && row->y < r->y + r->height)
5321 expose_line (w, row, r);
5322
5323 if (!w->pseudo_window_p)
5324 {
5325 /* Draw border between windows. */
5326 x_draw_vertical_border (w);
5327
5328 /* Turn the cursor on again. */
5329 if (cursor_cleared_p)
5330 x_update_window_cursor (w, 1);
5331 }
5332 }
5333
5334
5335 /* Determine the intersection of two rectangles R1 and R2. Return
5336 the intersection in *RESULT. Value is non-zero if RESULT is not
5337 empty. */
5338
5339 static int
5340 x_intersect_rectangles (r1, r2, result)
5341 XRectangle *r1, *r2, *result;
5342 {
5343 XRectangle *left, *right;
5344 XRectangle *upper, *lower;
5345 int intersection_p = 0;
5346
5347 /* Rearrange so that R1 is the left-most rectangle. */
5348 if (r1->x < r2->x)
5349 left = r1, right = r2;
5350 else
5351 left = r2, right = r1;
5352
5353 /* X0 of the intersection is right.x0, if this is inside R1,
5354 otherwise there is no intersection. */
5355 if (right->x <= left->x + left->width)
5356 {
5357 result->x = right->x;
5358
5359 /* The right end of the intersection is the minimum of the
5360 the right ends of left and right. */
5361 result->width = (min (left->x + left->width, right->x + right->width)
5362 - result->x);
5363
5364 /* Same game for Y. */
5365 if (r1->y < r2->y)
5366 upper = r1, lower = r2;
5367 else
5368 upper = r2, lower = r1;
5369
5370 /* The upper end of the intersection is lower.y0, if this is inside
5371 of upper. Otherwise, there is no intersection. */
5372 if (lower->y <= upper->y + upper->height)
5373 {
5374 result->y = lower->y;
5375
5376 /* The lower end of the intersection is the minimum of the lower
5377 ends of upper and lower. */
5378 result->height = (min (lower->y + lower->height,
5379 upper->y + upper->height)
5380 - result->y);
5381 intersection_p = 1;
5382 }
5383 }
5384
5385 return intersection_p;
5386 }
5387
5388
5389
5390
5391 \f
5392 static void
5393 frame_highlight (f)
5394 struct frame *f;
5395 {
5396 /* We used to only do this if Vx_no_window_manager was non-nil, but
5397 the ICCCM (section 4.1.6) says that the window's border pixmap
5398 and border pixel are window attributes which are "private to the
5399 client", so we can always change it to whatever we want. */
5400 BLOCK_INPUT;
5401 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5402 f->output_data.x->border_pixel);
5403 UNBLOCK_INPUT;
5404 x_update_cursor (f, 1);
5405 }
5406
5407 static void
5408 frame_unhighlight (f)
5409 struct frame *f;
5410 {
5411 /* We used to only do this if Vx_no_window_manager was non-nil, but
5412 the ICCCM (section 4.1.6) says that the window's border pixmap
5413 and border pixel are window attributes which are "private to the
5414 client", so we can always change it to whatever we want. */
5415 BLOCK_INPUT;
5416 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5417 f->output_data.x->border_tile);
5418 UNBLOCK_INPUT;
5419 x_update_cursor (f, 1);
5420 }
5421
5422 /* The focus has changed. Update the frames as necessary to reflect
5423 the new situation. Note that we can't change the selected frame
5424 here, because the Lisp code we are interrupting might become confused.
5425 Each event gets marked with the frame in which it occurred, so the
5426 Lisp code can tell when the switch took place by examining the events. */
5427
5428 static void
5429 x_new_focus_frame (dpyinfo, frame)
5430 struct x_display_info *dpyinfo;
5431 struct frame *frame;
5432 {
5433 struct frame *old_focus = dpyinfo->x_focus_frame;
5434
5435 if (frame != dpyinfo->x_focus_frame)
5436 {
5437 /* Set this before calling other routines, so that they see
5438 the correct value of x_focus_frame. */
5439 dpyinfo->x_focus_frame = frame;
5440
5441 if (old_focus && old_focus->auto_lower)
5442 x_lower_frame (old_focus);
5443
5444 #if 0
5445 selected_frame = frame;
5446 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5447 selected_frame);
5448 Fselect_window (selected_frame->selected_window);
5449 choose_minibuf_frame ();
5450 #endif /* ! 0 */
5451
5452 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5453 pending_autoraise_frame = dpyinfo->x_focus_frame;
5454 else
5455 pending_autoraise_frame = 0;
5456 }
5457
5458 x_frame_rehighlight (dpyinfo);
5459 }
5460
5461 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5462
5463 void
5464 x_mouse_leave (dpyinfo)
5465 struct x_display_info *dpyinfo;
5466 {
5467 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5468 }
5469
5470 /* The focus has changed, or we have redirected a frame's focus to
5471 another frame (this happens when a frame uses a surrogate
5472 mini-buffer frame). Shift the highlight as appropriate.
5473
5474 The FRAME argument doesn't necessarily have anything to do with which
5475 frame is being highlighted or un-highlighted; we only use it to find
5476 the appropriate X display info. */
5477
5478 static void
5479 XTframe_rehighlight (frame)
5480 struct frame *frame;
5481 {
5482 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5483 }
5484
5485 static void
5486 x_frame_rehighlight (dpyinfo)
5487 struct x_display_info *dpyinfo;
5488 {
5489 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5490
5491 if (dpyinfo->x_focus_frame)
5492 {
5493 dpyinfo->x_highlight_frame
5494 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5495 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5496 : dpyinfo->x_focus_frame);
5497 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5498 {
5499 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5500 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5501 }
5502 }
5503 else
5504 dpyinfo->x_highlight_frame = 0;
5505
5506 if (dpyinfo->x_highlight_frame != old_highlight)
5507 {
5508 if (old_highlight)
5509 frame_unhighlight (old_highlight);
5510 if (dpyinfo->x_highlight_frame)
5511 frame_highlight (dpyinfo->x_highlight_frame);
5512 }
5513 }
5514
5515
5516 \f
5517 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5518
5519 /* Initialize mode_switch_bit and modifier_meaning. */
5520 static void
5521 x_find_modifier_meanings (dpyinfo)
5522 struct x_display_info *dpyinfo;
5523 {
5524 int min_code, max_code;
5525 KeySym *syms;
5526 int syms_per_code;
5527 XModifierKeymap *mods;
5528
5529 dpyinfo->meta_mod_mask = 0;
5530 dpyinfo->shift_lock_mask = 0;
5531 dpyinfo->alt_mod_mask = 0;
5532 dpyinfo->super_mod_mask = 0;
5533 dpyinfo->hyper_mod_mask = 0;
5534
5535 #ifdef HAVE_X11R4
5536 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5537 #else
5538 min_code = dpyinfo->display->min_keycode;
5539 max_code = dpyinfo->display->max_keycode;
5540 #endif
5541
5542 syms = XGetKeyboardMapping (dpyinfo->display,
5543 min_code, max_code - min_code + 1,
5544 &syms_per_code);
5545 mods = XGetModifierMapping (dpyinfo->display);
5546
5547 /* Scan the modifier table to see which modifier bits the Meta and
5548 Alt keysyms are on. */
5549 {
5550 int row, col; /* The row and column in the modifier table. */
5551
5552 for (row = 3; row < 8; row++)
5553 for (col = 0; col < mods->max_keypermod; col++)
5554 {
5555 KeyCode code
5556 = mods->modifiermap[(row * mods->max_keypermod) + col];
5557
5558 /* Zeroes are used for filler. Skip them. */
5559 if (code == 0)
5560 continue;
5561
5562 /* Are any of this keycode's keysyms a meta key? */
5563 {
5564 int code_col;
5565
5566 for (code_col = 0; code_col < syms_per_code; code_col++)
5567 {
5568 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5569
5570 switch (sym)
5571 {
5572 case XK_Meta_L:
5573 case XK_Meta_R:
5574 dpyinfo->meta_mod_mask |= (1 << row);
5575 break;
5576
5577 case XK_Alt_L:
5578 case XK_Alt_R:
5579 dpyinfo->alt_mod_mask |= (1 << row);
5580 break;
5581
5582 case XK_Hyper_L:
5583 case XK_Hyper_R:
5584 dpyinfo->hyper_mod_mask |= (1 << row);
5585 break;
5586
5587 case XK_Super_L:
5588 case XK_Super_R:
5589 dpyinfo->super_mod_mask |= (1 << row);
5590 break;
5591
5592 case XK_Shift_Lock:
5593 /* Ignore this if it's not on the lock modifier. */
5594 if ((1 << row) == LockMask)
5595 dpyinfo->shift_lock_mask = LockMask;
5596 break;
5597 }
5598 }
5599 }
5600 }
5601 }
5602
5603 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5604 if (! dpyinfo->meta_mod_mask)
5605 {
5606 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5607 dpyinfo->alt_mod_mask = 0;
5608 }
5609
5610 /* If some keys are both alt and meta,
5611 make them just meta, not alt. */
5612 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5613 {
5614 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5615 }
5616
5617 XFree ((char *) syms);
5618 XFreeModifiermap (mods);
5619 }
5620
5621 /* Convert between the modifier bits X uses and the modifier bits
5622 Emacs uses. */
5623
5624 static unsigned int
5625 x_x_to_emacs_modifiers (dpyinfo, state)
5626 struct x_display_info *dpyinfo;
5627 unsigned int state;
5628 {
5629 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5630 | ((state & ControlMask) ? ctrl_modifier : 0)
5631 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5632 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5633 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5634 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
5635 }
5636
5637 static unsigned int
5638 x_emacs_to_x_modifiers (dpyinfo, state)
5639 struct x_display_info *dpyinfo;
5640 unsigned int state;
5641 {
5642 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5643 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5644 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5645 | ((state & shift_modifier) ? ShiftMask : 0)
5646 | ((state & ctrl_modifier) ? ControlMask : 0)
5647 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
5648 }
5649
5650 /* Convert a keysym to its name. */
5651
5652 char *
5653 x_get_keysym_name (keysym)
5654 KeySym keysym;
5655 {
5656 char *value;
5657
5658 BLOCK_INPUT;
5659 value = XKeysymToString (keysym);
5660 UNBLOCK_INPUT;
5661
5662 return value;
5663 }
5664
5665
5666 \f
5667 /* Mouse clicks and mouse movement. Rah. */
5668
5669 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5670 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5671 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5672 not force the value into range. */
5673
5674 void
5675 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5676 FRAME_PTR f;
5677 register int pix_x, pix_y;
5678 register int *x, *y;
5679 XRectangle *bounds;
5680 int noclip;
5681 {
5682 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5683 even for negative values. */
5684 if (pix_x < 0)
5685 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
5686 if (pix_y < 0)
5687 pix_y -= (f)->output_data.x->line_height - 1;
5688
5689 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5690 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5691
5692 if (bounds)
5693 {
5694 bounds->width = FONT_WIDTH (f->output_data.x->font);
5695 bounds->height = f->output_data.x->line_height;
5696 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5697 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5698 }
5699
5700 if (!noclip)
5701 {
5702 if (pix_x < 0)
5703 pix_x = 0;
5704 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5705 pix_x = FRAME_WINDOW_WIDTH (f);
5706
5707 if (pix_y < 0)
5708 pix_y = 0;
5709 else if (pix_y > f->height)
5710 pix_y = f->height;
5711 }
5712
5713 *x = pix_x;
5714 *y = pix_y;
5715 }
5716
5717
5718 /* Given HPOS/VPOS in the current matrix of W, return corresponding
5719 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5720 can't tell the positions because W's display is not up to date,
5721 return 0. */
5722
5723 int
5724 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5725 struct window *w;
5726 int hpos, vpos;
5727 int *frame_x, *frame_y;
5728 {
5729 int success_p;
5730
5731 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5732 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5733
5734 if (display_completed)
5735 {
5736 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5737 struct glyph *glyph = row->glyphs[TEXT_AREA];
5738 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5739
5740 *frame_y = row->y;
5741 *frame_x = row->x;
5742 while (glyph < end)
5743 {
5744 *frame_x += glyph->pixel_width;
5745 ++glyph;
5746 }
5747
5748 success_p = 1;
5749 }
5750 else
5751 {
5752 *frame_y = *frame_x = 0;
5753 success_p = 0;
5754 }
5755
5756 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5757 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5758 return success_p;
5759 }
5760
5761
5762 /* Prepare a mouse-event in *RESULT for placement in the input queue.
5763
5764 If the event is a button press, then note that we have grabbed
5765 the mouse. */
5766
5767 static Lisp_Object
5768 construct_mouse_click (result, event, f)
5769 struct input_event *result;
5770 XButtonEvent *event;
5771 struct frame *f;
5772 {
5773 /* Make the event type no_event; we'll change that when we decide
5774 otherwise. */
5775 result->kind = mouse_click;
5776 result->code = event->button - Button1;
5777 result->timestamp = event->time;
5778 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5779 event->state)
5780 | (event->type == ButtonRelease
5781 ? up_modifier
5782 : down_modifier));
5783
5784 XSETINT (result->x, event->x);
5785 XSETINT (result->y, event->y);
5786 XSETFRAME (result->frame_or_window, f);
5787 return Qnil;
5788 }
5789
5790 #if 0 /* This function isn't called. --gerd */
5791
5792 /* Prepare a menu-event in *RESULT for placement in the input queue. */
5793
5794 static Lisp_Object
5795 construct_menu_click (result, event, f)
5796 struct input_event *result;
5797 XButtonEvent *event;
5798 struct frame *f;
5799 {
5800 /* Make the event type no_event; we'll change that when we decide
5801 otherwise. */
5802 result->kind = mouse_click;
5803 result->code = event->button - Button1;
5804 result->timestamp = event->time;
5805 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5806 event->state)
5807 | (event->type == ButtonRelease
5808 ? up_modifier
5809 : down_modifier));
5810
5811 XSETINT (result->x, event->x);
5812 XSETINT (result->y, -1);
5813 XSETFRAME (result->frame_or_window, f);
5814 }
5815
5816 #endif /* 0 */
5817
5818 \f
5819 /* Function to report a mouse movement to the mainstream Emacs code.
5820 The input handler calls this.
5821
5822 We have received a mouse movement event, which is given in *event.
5823 If the mouse is over a different glyph than it was last time, tell
5824 the mainstream emacs code by setting mouse_moved. If not, ask for
5825 another motion event, so we can check again the next time it moves. */
5826
5827 static XMotionEvent last_mouse_motion_event;
5828 static Lisp_Object last_mouse_motion_frame;
5829
5830 static void
5831 note_mouse_movement (frame, event)
5832 FRAME_PTR frame;
5833 XMotionEvent *event;
5834 {
5835 last_mouse_movement_time = event->time;
5836 last_mouse_motion_event = *event;
5837 XSETFRAME (last_mouse_motion_frame, frame);
5838
5839 if (event->window != FRAME_X_WINDOW (frame))
5840 {
5841 frame->mouse_moved = 1;
5842 last_mouse_scroll_bar = Qnil;
5843 note_mouse_highlight (frame, -1, -1);
5844 }
5845
5846 /* Has the mouse moved off the glyph it was on at the last sighting? */
5847 else if (event->x < last_mouse_glyph.x
5848 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
5849 || event->y < last_mouse_glyph.y
5850 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
5851 {
5852 frame->mouse_moved = 1;
5853 last_mouse_scroll_bar = Qnil;
5854 note_mouse_highlight (frame, event->x, event->y);
5855 }
5856 }
5857
5858 /* This is used for debugging, to turn off note_mouse_highlight. */
5859
5860 int disable_mouse_highlight;
5861
5862
5863 \f
5864 /************************************************************************
5865 Mouse Face
5866 ************************************************************************/
5867
5868 /* Find the glyph under window-relative coordinates X/Y in window W.
5869 Consider only glyphs from buffer text, i.e. no glyphs from overlay
5870 strings. Return in *HPOS and *VPOS the row and column number of
5871 the glyph found. Return in *AREA the glyph area containing X.
5872 Value is a pointer to the glyph found or null if X/Y is not on
5873 text, or we can't tell because W's current matrix is not up to
5874 date. */
5875
5876 static struct glyph *
5877 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
5878 struct window *w;
5879 int x, y;
5880 int *hpos, *vpos, *area;
5881 {
5882 struct glyph *glyph, *end;
5883 struct glyph_row *row;
5884 int x0, i, left_area_width;
5885
5886 /* Find row containing Y. Give up if some row is not enabled. */
5887 for (i = 0; i < w->current_matrix->nrows; ++i)
5888 {
5889 row = MATRIX_ROW (w->current_matrix, i);
5890 if (!row->enabled_p)
5891 return NULL;
5892 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
5893 break;
5894 }
5895
5896 *vpos = i;
5897 *hpos = 0;
5898
5899 /* Give up if Y is not in the window. */
5900 if (i == w->current_matrix->nrows)
5901 return NULL;
5902
5903 /* Get the glyph area containing X. */
5904 if (w->pseudo_window_p)
5905 {
5906 *area = TEXT_AREA;
5907 x0 = 0;
5908 }
5909 else
5910 {
5911 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5912 if (x < left_area_width)
5913 {
5914 *area = LEFT_MARGIN_AREA;
5915 x0 = 0;
5916 }
5917 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
5918 {
5919 *area = TEXT_AREA;
5920 x0 = row->x + left_area_width;
5921 }
5922 else
5923 {
5924 *area = RIGHT_MARGIN_AREA;
5925 x0 = left_area_width + window_box_width (w, TEXT_AREA);
5926 }
5927 }
5928
5929 /* Find glyph containing X. */
5930 glyph = row->glyphs[*area];
5931 end = glyph + row->used[*area];
5932 while (glyph < end)
5933 {
5934 if (x < x0 + glyph->pixel_width)
5935 {
5936 if (w->pseudo_window_p)
5937 break;
5938 else if (BUFFERP (glyph->object))
5939 break;
5940 }
5941
5942 x0 += glyph->pixel_width;
5943 ++glyph;
5944 }
5945
5946 if (glyph == end)
5947 return NULL;
5948
5949 *hpos = glyph - row->glyphs[*area];
5950 return glyph;
5951 }
5952
5953
5954 /* Convert frame-relative x/y to coordinates relative to window W.
5955 Takes pseudo-windows into account. */
5956
5957 static void
5958 frame_to_window_pixel_xy (w, x, y)
5959 struct window *w;
5960 int *x, *y;
5961 {
5962 if (w->pseudo_window_p)
5963 {
5964 /* A pseudo-window is always full-width, and starts at the
5965 left edge of the frame, plus a frame border. */
5966 struct frame *f = XFRAME (w->frame);
5967 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
5968 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5969 }
5970 else
5971 {
5972 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
5973 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5974 }
5975 }
5976
5977
5978 /* Take proper action when mouse has moved to the mode or top line of
5979 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
5980 mode line. X is relative to the start of the text display area of
5981 W, so the width of bitmap areas and scroll bars must be subtracted
5982 to get a position relative to the start of the mode line. */
5983
5984 static void
5985 note_mode_line_highlight (w, x, mode_line_p)
5986 struct window *w;
5987 int x, mode_line_p;
5988 {
5989 struct frame *f = XFRAME (w->frame);
5990 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5991 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
5992 struct glyph_row *row;
5993
5994 if (mode_line_p)
5995 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5996 else
5997 row = MATRIX_TOP_LINE_ROW (w->current_matrix);
5998
5999 if (row->enabled_p)
6000 {
6001 struct glyph *glyph, *end;
6002 Lisp_Object help, map;
6003 int x0;
6004
6005 /* Find the glyph under X. */
6006 glyph = row->glyphs[TEXT_AREA];
6007 end = glyph + row->used[TEXT_AREA];
6008 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6009 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6010 while (glyph < end
6011 && x >= x0 + glyph->pixel_width)
6012 {
6013 x0 += glyph->pixel_width;
6014 ++glyph;
6015 }
6016
6017 if (glyph < end
6018 && STRINGP (glyph->object)
6019 && XSTRING (glyph->object)->intervals
6020 && glyph->charpos >= 0
6021 && glyph->charpos < XSTRING (glyph->object)->size)
6022 {
6023 /* If we're on a string with `help-echo' text property,
6024 arrange for the help to be displayed. This is done by
6025 setting the global variable help_echo to the help string. */
6026 help = Fget_text_property (make_number (glyph->charpos),
6027 Qhelp_echo, glyph->object);
6028 if (STRINGP (help))
6029 help_echo = help;
6030
6031 /* Change the mouse pointer according to what is under X/Y. */
6032 map = Fget_text_property (make_number (glyph->charpos),
6033 Qlocal_map, glyph->object);
6034 if (!NILP (Fkeymapp (map)))
6035 cursor = f->output_data.x->nontext_cursor;
6036 }
6037 }
6038
6039 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6040 }
6041
6042
6043 /* Take proper action when the mouse has moved to position X, Y on
6044 frame F as regards highlighting characters that have mouse-face
6045 properties. Also de-highlighting chars where the mouse was before.
6046 X and Y can be negative or out of range. */
6047
6048 static void
6049 note_mouse_highlight (f, x, y)
6050 struct frame *f;
6051 int x, y;
6052 {
6053 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6054 int portion;
6055 Lisp_Object window;
6056 struct window *w;
6057
6058 /* When a menu is active, don't highlight because this looks odd. */
6059 #ifdef USE_X_TOOLKIT
6060 if (popup_activated ())
6061 return;
6062 #endif
6063
6064 if (disable_mouse_highlight)
6065 return;
6066
6067 dpyinfo->mouse_face_mouse_x = x;
6068 dpyinfo->mouse_face_mouse_y = y;
6069 dpyinfo->mouse_face_mouse_frame = f;
6070
6071 if (dpyinfo->mouse_face_defer)
6072 return;
6073
6074 if (gc_in_progress)
6075 {
6076 dpyinfo->mouse_face_deferred_gc = 1;
6077 return;
6078 }
6079
6080 /* Which window is that in? */
6081 window = window_from_coordinates (f, x, y, &portion, 1);
6082
6083 /* If we were displaying active text in another window, clear that. */
6084 if (! EQ (window, dpyinfo->mouse_face_window))
6085 clear_mouse_face (dpyinfo);
6086
6087 /* Not on a window -> return. */
6088 if (!WINDOWP (window))
6089 return;
6090
6091 /* Convert to window-relative pixel coordinates. */
6092 w = XWINDOW (window);
6093 frame_to_window_pixel_xy (w, &x, &y);
6094
6095 /* Handle toolbar window differently since it doesn't display a
6096 buffer. */
6097 if (EQ (window, f->toolbar_window))
6098 {
6099 note_toolbar_highlight (f, x, y);
6100 return;
6101 }
6102
6103 if (portion == 1 || portion == 3)
6104 {
6105 /* Mouse is on the mode or top line. */
6106 note_mode_line_highlight (w, x, portion == 1);
6107 return;
6108 }
6109 else
6110 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6111 f->output_data.x->text_cursor);
6112
6113 /* Are we in a window whose display is up to date?
6114 And verify the buffer's text has not changed. */
6115 if (/* Within text portion of the window. */
6116 portion == 0
6117 && EQ (w->window_end_valid, w->buffer)
6118 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6119 && (XFASTINT (w->last_overlay_modified)
6120 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6121 {
6122 int hpos, vpos, pos, i, area;
6123 struct glyph *glyph;
6124
6125 /* Find the glyph under X/Y. */
6126 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6127
6128 /* Clear mouse face if X/Y not over text. */
6129 if (glyph == NULL
6130 || area != TEXT_AREA
6131 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6132 {
6133 clear_mouse_face (dpyinfo);
6134 return;
6135 }
6136
6137 pos = glyph->charpos;
6138 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6139
6140 /* Check for mouse-face and help-echo. */
6141 {
6142 Lisp_Object mouse_face, overlay, position;
6143 Lisp_Object *overlay_vec;
6144 int len, noverlays;
6145 struct buffer *obuf;
6146 int obegv, ozv;
6147
6148 /* If we get an out-of-range value, return now; avoid an error. */
6149 if (pos > BUF_Z (XBUFFER (w->buffer)))
6150 return;
6151
6152 /* Make the window's buffer temporarily current for
6153 overlays_at and compute_char_face. */
6154 obuf = current_buffer;
6155 current_buffer = XBUFFER (w->buffer);
6156 obegv = BEGV;
6157 ozv = ZV;
6158 BEGV = BEG;
6159 ZV = Z;
6160
6161 /* Is this char mouse-active or does it have help-echo? */
6162 XSETINT (position, pos);
6163
6164 /* Put all the overlays we want in a vector in overlay_vec.
6165 Store the length in len. If there are more than 10, make
6166 enough space for all, and try again. */
6167 len = 10;
6168 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6169 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6170 if (noverlays > len)
6171 {
6172 len = noverlays;
6173 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6174 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6175 }
6176
6177 noverlays = sort_overlays (overlay_vec, noverlays, w);
6178
6179 /* Check mouse-face highlighting. */
6180 if (! (EQ (window, dpyinfo->mouse_face_window)
6181 && vpos >= dpyinfo->mouse_face_beg_row
6182 && vpos <= dpyinfo->mouse_face_end_row
6183 && (vpos > dpyinfo->mouse_face_beg_row
6184 || hpos >= dpyinfo->mouse_face_beg_col)
6185 && (vpos < dpyinfo->mouse_face_end_row
6186 || hpos < dpyinfo->mouse_face_end_col
6187 || dpyinfo->mouse_face_past_end)))
6188 {
6189 /* Clear the display of the old active region, if any. */
6190 clear_mouse_face (dpyinfo);
6191
6192 /* Find the highest priority overlay that has a mouse-face prop. */
6193 overlay = Qnil;
6194 for (i = 0; i < noverlays; i++)
6195 {
6196 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6197 if (!NILP (mouse_face))
6198 {
6199 overlay = overlay_vec[i];
6200 break;
6201 }
6202 }
6203
6204 /* If no overlay applies, get a text property. */
6205 if (NILP (overlay))
6206 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6207
6208 /* Handle the overlay case. */
6209 if (! NILP (overlay))
6210 {
6211 /* Find the range of text around this char that
6212 should be active. */
6213 Lisp_Object before, after;
6214 int ignore;
6215
6216 before = Foverlay_start (overlay);
6217 after = Foverlay_end (overlay);
6218 /* Record this as the current active region. */
6219 fast_find_position (w, XFASTINT (before),
6220 &dpyinfo->mouse_face_beg_col,
6221 &dpyinfo->mouse_face_beg_row,
6222 &dpyinfo->mouse_face_beg_x,
6223 &dpyinfo->mouse_face_beg_y);
6224 dpyinfo->mouse_face_past_end
6225 = !fast_find_position (w, XFASTINT (after),
6226 &dpyinfo->mouse_face_end_col,
6227 &dpyinfo->mouse_face_end_row,
6228 &dpyinfo->mouse_face_end_x,
6229 &dpyinfo->mouse_face_end_y);
6230 dpyinfo->mouse_face_window = window;
6231 dpyinfo->mouse_face_face_id
6232 = face_at_buffer_position (w, pos, 0, 0,
6233 &ignore, pos + 1, 1);
6234
6235 /* Display it as active. */
6236 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6237 }
6238 /* Handle the text property case. */
6239 else if (! NILP (mouse_face))
6240 {
6241 /* Find the range of text around this char that
6242 should be active. */
6243 Lisp_Object before, after, beginning, end;
6244 int ignore;
6245
6246 beginning = Fmarker_position (w->start);
6247 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6248 - XFASTINT (w->window_end_pos)));
6249 before
6250 = Fprevious_single_property_change (make_number (pos + 1),
6251 Qmouse_face,
6252 w->buffer, beginning);
6253 after
6254 = Fnext_single_property_change (position, Qmouse_face,
6255 w->buffer, end);
6256 /* Record this as the current active region. */
6257 fast_find_position (w, XFASTINT (before),
6258 &dpyinfo->mouse_face_beg_col,
6259 &dpyinfo->mouse_face_beg_row,
6260 &dpyinfo->mouse_face_beg_x,
6261 &dpyinfo->mouse_face_beg_y);
6262 dpyinfo->mouse_face_past_end
6263 = !fast_find_position (w, XFASTINT (after),
6264 &dpyinfo->mouse_face_end_col,
6265 &dpyinfo->mouse_face_end_row,
6266 &dpyinfo->mouse_face_end_x,
6267 &dpyinfo->mouse_face_end_y);
6268 dpyinfo->mouse_face_window = window;
6269 dpyinfo->mouse_face_face_id
6270 = face_at_buffer_position (w, pos, 0, 0,
6271 &ignore, pos + 1, 1);
6272
6273 /* Display it as active. */
6274 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6275 }
6276 }
6277
6278 /* Look for a `help-echo' property. */
6279 {
6280 Lisp_Object help;
6281
6282 /* Check overlays first. */
6283 help = Qnil;
6284 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6285 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6286
6287 /* Try text properties. */
6288 if (!STRINGP (help)
6289 && ((STRINGP (glyph->object)
6290 && glyph->charpos >= 0
6291 && glyph->charpos < XSTRING (glyph->object)->size)
6292 || (BUFFERP (glyph->object)
6293 && glyph->charpos >= BEGV
6294 && glyph->charpos < ZV)))
6295 help = Fget_text_property (make_number (glyph->charpos),
6296 Qhelp_echo, glyph->object);
6297
6298 if (STRINGP (help))
6299 help_echo = help;
6300 }
6301
6302 BEGV = obegv;
6303 ZV = ozv;
6304 current_buffer = obuf;
6305 }
6306 }
6307 }
6308
6309 static void
6310 redo_mouse_highlight ()
6311 {
6312 if (!NILP (last_mouse_motion_frame)
6313 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6314 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6315 last_mouse_motion_event.x,
6316 last_mouse_motion_event.y);
6317 }
6318
6319
6320 \f
6321 /***********************************************************************
6322 Toolbars
6323 ***********************************************************************/
6324
6325 static int x_toolbar_item P_ ((struct frame *, int, int,
6326 struct glyph **, int *, int *, int *));
6327
6328 /* Toolbar item index of the item on which a mouse button was pressed
6329 or -1. */
6330
6331 static int last_toolbar_item;
6332
6333
6334 /* Get information about the toolbar item at position X/Y on frame F.
6335 Return in *GLYPH a pointer to the glyph of the toolbar item in
6336 the current matrix of the toolbar window of F, or NULL if not
6337 on a toolbar item. Return in *PROP_IDX the index of the toolbar
6338 item in F->current_toolbar_items. Value is
6339
6340 -1 if X/Y is not on a toolbar item
6341 0 if X/Y is on the same item that was highlighted before.
6342 1 otherwise. */
6343
6344 static int
6345 x_toolbar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6346 struct frame *f;
6347 int x, y;
6348 struct glyph **glyph;
6349 int *hpos, *vpos, *prop_idx;
6350 {
6351 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6352 struct window *w = XWINDOW (f->toolbar_window);
6353 int area;
6354
6355 /* Find the glyph under X/Y. */
6356 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6357 if (*glyph == NULL)
6358 return -1;
6359
6360 /* Get the start of this toolbar item's properties in
6361 f->current_toolbar_items. */
6362 if (!toolbar_item_info (f, *glyph, prop_idx))
6363 return -1;
6364
6365 /* Is mouse on the highlighted item? */
6366 if (EQ (f->toolbar_window, dpyinfo->mouse_face_window)
6367 && *vpos >= dpyinfo->mouse_face_beg_row
6368 && *vpos <= dpyinfo->mouse_face_end_row
6369 && (*vpos > dpyinfo->mouse_face_beg_row
6370 || *hpos >= dpyinfo->mouse_face_beg_col)
6371 && (*vpos < dpyinfo->mouse_face_end_row
6372 || *hpos < dpyinfo->mouse_face_end_col
6373 || dpyinfo->mouse_face_past_end))
6374 return 0;
6375
6376 return 1;
6377 }
6378
6379
6380 /* Handle mouse button event on the toolbar of frame F, at
6381 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6382 or ButtonRelase. */
6383
6384 static void
6385 x_handle_toolbar_click (f, button_event)
6386 struct frame *f;
6387 XButtonEvent *button_event;
6388 {
6389 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6390 struct window *w = XWINDOW (f->toolbar_window);
6391 int hpos, vpos, prop_idx;
6392 struct glyph *glyph;
6393 Lisp_Object enabled_p;
6394 int x = button_event->x;
6395 int y = button_event->y;
6396
6397 /* If not on the highlighted toolbar item, return. */
6398 frame_to_window_pixel_xy (w, &x, &y);
6399 if (x_toolbar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6400 return;
6401
6402 /* If item is disabled, do nothing. */
6403 enabled_p = (XVECTOR (f->current_toolbar_items)
6404 ->contents[prop_idx + TOOLBAR_ITEM_ENABLED_P]);
6405 if (NILP (enabled_p))
6406 return;
6407
6408 if (button_event->type == ButtonPress)
6409 {
6410 /* Show item in pressed state. */
6411 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6412 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6413 last_toolbar_item = prop_idx;
6414 }
6415 else
6416 {
6417 Lisp_Object key, frame;
6418 struct input_event event;
6419
6420 /* Show item in released state. */
6421 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6422 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6423
6424 key = (XVECTOR (f->current_toolbar_items)
6425 ->contents[prop_idx + TOOLBAR_ITEM_KEY]);
6426
6427 XSETFRAME (frame, f);
6428 event.kind = TOOLBAR_EVENT;
6429 event.frame_or_window = Fcons (frame, Fcons (Qtoolbar, Qnil));
6430 kbd_buffer_store_event (&event);
6431
6432 event.kind = TOOLBAR_EVENT;
6433 event.frame_or_window = Fcons (frame, key);
6434 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6435 button_event->state);
6436 kbd_buffer_store_event (&event);
6437 last_toolbar_item = -1;
6438 }
6439 }
6440
6441
6442 /* Possibly highlight a toolbar item on frame F when mouse moves to
6443 toolbar window-relative coordinates X/Y. Called from
6444 note_mouse_highlight. */
6445
6446 static void
6447 note_toolbar_highlight (f, x, y)
6448 struct frame *f;
6449 int x, y;
6450 {
6451 Lisp_Object window = f->toolbar_window;
6452 struct window *w = XWINDOW (window);
6453 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6454 int hpos, vpos;
6455 struct glyph *glyph;
6456 struct glyph_row *row;
6457 int i, j, area;
6458 Lisp_Object enabled_p;
6459 int prop_idx;
6460 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6461 int on_highlight_p, mouse_down_p, rc;
6462
6463 /* Function note_mouse_highlight is called with negative x(y
6464 values when mouse moves outside of the frame. */
6465 if (x <= 0 || y <= 0)
6466 {
6467 clear_mouse_face (dpyinfo);
6468 return;
6469 }
6470
6471 rc = x_toolbar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6472 if (rc < 0)
6473 {
6474 /* Not on toolbar item. */
6475 clear_mouse_face (dpyinfo);
6476 return;
6477 }
6478 else if (rc == 0)
6479 /* On same toolbar item as before. */
6480 goto set_help_echo;
6481
6482 clear_mouse_face (dpyinfo);
6483
6484 /* Mouse is down, but on different toolbar item? */
6485 mouse_down_p = (dpyinfo->grabbed
6486 && f == last_mouse_frame
6487 && FRAME_LIVE_P (f));
6488 if (mouse_down_p
6489 && last_toolbar_item != prop_idx)
6490 return;
6491
6492 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6493 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6494
6495 /* If toolbar item is not enabled, don't highlight it. */
6496 enabled_p = (XVECTOR (f->current_toolbar_items)
6497 ->contents[prop_idx + TOOLBAR_ITEM_ENABLED_P]);
6498 if (!NILP (enabled_p))
6499 {
6500 /* Compute the x-position of the glyph. In front and past the
6501 image is a space. We include this is the highlighted area. */
6502 row = MATRIX_ROW (w->current_matrix, vpos);
6503 for (i = x = 0; i < hpos; ++i)
6504 x += row->glyphs[TEXT_AREA][i].pixel_width;
6505
6506 /* Record this as the current active region. */
6507 dpyinfo->mouse_face_beg_col = hpos;
6508 dpyinfo->mouse_face_beg_row = vpos;
6509 dpyinfo->mouse_face_beg_x = x;
6510 dpyinfo->mouse_face_beg_y = row->y;
6511 dpyinfo->mouse_face_past_end = 0;
6512
6513 dpyinfo->mouse_face_end_col = hpos + 1;
6514 dpyinfo->mouse_face_end_row = vpos;
6515 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6516 dpyinfo->mouse_face_end_y = row->y;
6517 dpyinfo->mouse_face_window = window;
6518 dpyinfo->mouse_face_face_id = TOOLBAR_FACE_ID;
6519
6520 /* Display it as active. */
6521 show_mouse_face (dpyinfo, draw);
6522 dpyinfo->mouse_face_image_state = draw;
6523 }
6524
6525 set_help_echo:
6526
6527 /* Set help_echo to a help string.to display for this toolbar item.
6528 XTread_socket does the rest. */
6529 help_echo = (XVECTOR (f->current_toolbar_items)
6530 ->contents[prop_idx + TOOLBAR_ITEM_HELP]);
6531 if (!STRINGP (help_echo))
6532 help_echo = (XVECTOR (f->current_toolbar_items)
6533 ->contents[prop_idx + TOOLBAR_ITEM_CAPTION]);
6534 }
6535
6536
6537 \f
6538 /* Find the glyph matrix position of buffer position POS in window W.
6539 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6540 current glyphs must be up to date. If POS is above window start
6541 return (0, 0, 0, 0). If POS is after end of W, return end of
6542 last line in W. */
6543
6544 static int
6545 fast_find_position (w, pos, hpos, vpos, x, y)
6546 struct window *w;
6547 int pos;
6548 int *hpos, *vpos, *x, *y;
6549 {
6550 int i;
6551 int lastcol;
6552 int maybe_next_line_p = 0;
6553 int line_start_position;
6554 int yb = window_text_bottom_y (w);
6555 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6556 struct glyph_row *best_row = row;
6557 int row_vpos = 0, best_row_vpos = 0;
6558 int current_x;
6559
6560 while (row->y < yb)
6561 {
6562 if (row->used[TEXT_AREA])
6563 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6564 else
6565 line_start_position = 0;
6566
6567 if (line_start_position > pos)
6568 break;
6569 /* If the position sought is the end of the buffer,
6570 don't include the blank lines at the bottom of the window. */
6571 else if (line_start_position == pos
6572 && pos == BUF_ZV (XBUFFER (w->buffer)))
6573 {
6574 maybe_next_line_p = 1;
6575 break;
6576 }
6577 else if (line_start_position > 0)
6578 {
6579 best_row = row;
6580 best_row_vpos = row_vpos;
6581 }
6582
6583 ++row;
6584 ++row_vpos;
6585 }
6586
6587 /* Find the right column within BEST_ROW. */
6588 lastcol = 0;
6589 current_x = best_row->x;
6590 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6591 {
6592 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6593 int charpos;
6594
6595 charpos = glyph->charpos;
6596 if (charpos == pos)
6597 {
6598 *hpos = i;
6599 *vpos = best_row_vpos;
6600 *x = current_x;
6601 *y = best_row->y;
6602 return 1;
6603 }
6604 else if (charpos > pos)
6605 break;
6606 else if (charpos > 0)
6607 lastcol = i;
6608
6609 current_x += glyph->pixel_width;
6610 }
6611
6612 /* If we're looking for the end of the buffer,
6613 and we didn't find it in the line we scanned,
6614 use the start of the following line. */
6615 if (maybe_next_line_p)
6616 {
6617 ++best_row;
6618 ++best_row_vpos;
6619 lastcol = 0;
6620 current_x = best_row->x;
6621 }
6622
6623 *vpos = best_row_vpos;
6624 *hpos = lastcol + 1;
6625 *x = current_x;
6626 *y = best_row->y;
6627 return 0;
6628 }
6629
6630
6631 /* Display the active region described by mouse_face_*
6632 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6633
6634 static void
6635 show_mouse_face (dpyinfo, draw)
6636 struct x_display_info *dpyinfo;
6637 enum draw_glyphs_face draw;
6638 {
6639 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6640 struct frame *f = XFRAME (WINDOW_FRAME (w));
6641 int i;
6642 int cursor_off_p = 0;
6643 struct cursor_pos saved_cursor;
6644
6645 saved_cursor = output_cursor;
6646
6647 /* If window is in the process of being destroyed, don't bother
6648 to do anything. */
6649 if (w->current_matrix == NULL)
6650 goto set_x_cursor;
6651
6652 /* Recognize when we are called to operate on rows that don't exist
6653 anymore. This can happen when a window is split. */
6654 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6655 goto set_x_cursor;
6656
6657 set_output_cursor (&w->phys_cursor);
6658
6659 /* Note that mouse_face_beg_row etc. are window relative. */
6660 for (i = dpyinfo->mouse_face_beg_row;
6661 i <= dpyinfo->mouse_face_end_row;
6662 i++)
6663 {
6664 int start_hpos, end_hpos, start_x;
6665 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6666
6667 /* Don't do anything if row doesn't have valid contents. */
6668 if (!row->enabled_p)
6669 continue;
6670
6671 /* For all but the first row, the highlight starts at column 0. */
6672 if (i == dpyinfo->mouse_face_beg_row)
6673 {
6674 start_hpos = dpyinfo->mouse_face_beg_col;
6675 start_x = dpyinfo->mouse_face_beg_x;
6676 }
6677 else
6678 {
6679 start_hpos = 0;
6680 start_x = 0;
6681 }
6682
6683 if (i == dpyinfo->mouse_face_end_row)
6684 end_hpos = dpyinfo->mouse_face_end_col;
6685 else
6686 end_hpos = row->used[TEXT_AREA];
6687
6688 /* If the cursor's in the text we are about to rewrite, turn the
6689 cursor off. */
6690 if (!w->pseudo_window_p
6691 && i == output_cursor.vpos
6692 && output_cursor.hpos >= start_hpos - 1
6693 && output_cursor.hpos <= end_hpos)
6694 {
6695 x_update_window_cursor (w, 0);
6696 cursor_off_p = 1;
6697 }
6698
6699 if (end_hpos > start_hpos)
6700 x_draw_glyphs (w, start_x, row, updated_area,
6701 start_hpos, end_hpos, draw, NULL, NULL, 0);
6702 }
6703
6704 /* If we turned the cursor off, turn it back on. */
6705 if (cursor_off_p)
6706 x_display_cursor (w, 1,
6707 output_cursor.hpos, output_cursor.vpos,
6708 output_cursor.x, output_cursor.y);
6709
6710 output_cursor = saved_cursor;
6711
6712 set_x_cursor:
6713
6714 /* Change the mouse cursor. */
6715 if (draw == DRAW_NORMAL_TEXT)
6716 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6717 f->output_data.x->text_cursor);
6718 else if (draw == DRAW_MOUSE_FACE)
6719 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6720 f->output_data.x->cross_cursor);
6721 else
6722 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6723 f->output_data.x->nontext_cursor);
6724 }
6725
6726 /* Clear out the mouse-highlighted active region.
6727 Redraw it un-highlighted first. */
6728
6729 void
6730 clear_mouse_face (dpyinfo)
6731 struct x_display_info *dpyinfo;
6732 {
6733 if (tip_frame)
6734 return;
6735
6736 if (! NILP (dpyinfo->mouse_face_window))
6737 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
6738
6739 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6740 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6741 dpyinfo->mouse_face_window = Qnil;
6742 }
6743
6744 /* Just discard the mouse face information for frame F, if any.
6745 This is used when the size of F is changed. */
6746
6747 void
6748 cancel_mouse_face (f)
6749 FRAME_PTR f;
6750 {
6751 Lisp_Object window;
6752 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6753
6754 window = dpyinfo->mouse_face_window;
6755 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6756 {
6757 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6758 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6759 dpyinfo->mouse_face_window = Qnil;
6760 }
6761 }
6762 \f
6763 static struct scroll_bar *x_window_to_scroll_bar ();
6764 static void x_scroll_bar_report_motion ();
6765
6766 /* Return the current position of the mouse.
6767 *fp should be a frame which indicates which display to ask about.
6768
6769 If the mouse movement started in a scroll bar, set *fp, *bar_window,
6770 and *part to the frame, window, and scroll bar part that the mouse
6771 is over. Set *x and *y to the portion and whole of the mouse's
6772 position on the scroll bar.
6773
6774 If the mouse movement started elsewhere, set *fp to the frame the
6775 mouse is on, *bar_window to nil, and *x and *y to the character cell
6776 the mouse is over.
6777
6778 Set *time to the server time-stamp for the time at which the mouse
6779 was at this position.
6780
6781 Don't store anything if we don't have a valid set of values to report.
6782
6783 This clears the mouse_moved flag, so we can wait for the next mouse
6784 movement. */
6785
6786 static void
6787 XTmouse_position (fp, insist, bar_window, part, x, y, time)
6788 FRAME_PTR *fp;
6789 int insist;
6790 Lisp_Object *bar_window;
6791 enum scroll_bar_part *part;
6792 Lisp_Object *x, *y;
6793 unsigned long *time;
6794 {
6795 FRAME_PTR f1;
6796
6797 BLOCK_INPUT;
6798
6799 if (! NILP (last_mouse_scroll_bar) && insist == 0)
6800 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
6801 else
6802 {
6803 Window root;
6804 int root_x, root_y;
6805
6806 Window dummy_window;
6807 int dummy;
6808
6809 Lisp_Object frame, tail;
6810
6811 /* Clear the mouse-moved flag for every frame on this display. */
6812 FOR_EACH_FRAME (tail, frame)
6813 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
6814 XFRAME (frame)->mouse_moved = 0;
6815
6816 last_mouse_scroll_bar = Qnil;
6817
6818 /* Figure out which root window we're on. */
6819 XQueryPointer (FRAME_X_DISPLAY (*fp),
6820 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
6821
6822 /* The root window which contains the pointer. */
6823 &root,
6824
6825 /* Trash which we can't trust if the pointer is on
6826 a different screen. */
6827 &dummy_window,
6828
6829 /* The position on that root window. */
6830 &root_x, &root_y,
6831
6832 /* More trash we can't trust. */
6833 &dummy, &dummy,
6834
6835 /* Modifier keys and pointer buttons, about which
6836 we don't care. */
6837 (unsigned int *) &dummy);
6838
6839 /* Now we have a position on the root; find the innermost window
6840 containing the pointer. */
6841 {
6842 Window win, child;
6843 int win_x, win_y;
6844 int parent_x = 0, parent_y = 0;
6845 int count;
6846
6847 win = root;
6848
6849 /* XTranslateCoordinates can get errors if the window
6850 structure is changing at the same time this function
6851 is running. So at least we must not crash from them. */
6852
6853 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
6854
6855 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
6856 && FRAME_LIVE_P (last_mouse_frame))
6857 {
6858 /* If mouse was grabbed on a frame, give coords for that frame
6859 even if the mouse is now outside it. */
6860 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
6861
6862 /* From-window, to-window. */
6863 root, FRAME_X_WINDOW (last_mouse_frame),
6864
6865 /* From-position, to-position. */
6866 root_x, root_y, &win_x, &win_y,
6867
6868 /* Child of win. */
6869 &child);
6870 f1 = last_mouse_frame;
6871 }
6872 else
6873 {
6874 while (1)
6875 {
6876 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
6877
6878 /* From-window, to-window. */
6879 root, win,
6880
6881 /* From-position, to-position. */
6882 root_x, root_y, &win_x, &win_y,
6883
6884 /* Child of win. */
6885 &child);
6886
6887 if (child == None || child == win)
6888 break;
6889
6890 win = child;
6891 parent_x = win_x;
6892 parent_y = win_y;
6893 }
6894
6895 /* Now we know that:
6896 win is the innermost window containing the pointer
6897 (XTC says it has no child containing the pointer),
6898 win_x and win_y are the pointer's position in it
6899 (XTC did this the last time through), and
6900 parent_x and parent_y are the pointer's position in win's parent.
6901 (They are what win_x and win_y were when win was child.
6902 If win is the root window, it has no parent, and
6903 parent_{x,y} are invalid, but that's okay, because we'll
6904 never use them in that case.) */
6905
6906 /* Is win one of our frames? */
6907 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
6908 }
6909
6910 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
6911 f1 = 0;
6912
6913 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
6914
6915 /* If not, is it one of our scroll bars? */
6916 if (! f1)
6917 {
6918 struct scroll_bar *bar = x_window_to_scroll_bar (win);
6919
6920 if (bar)
6921 {
6922 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6923 win_x = parent_x;
6924 win_y = parent_y;
6925 }
6926 }
6927
6928 if (f1 == 0 && insist > 0)
6929 f1 = selected_frame;
6930
6931 if (f1)
6932 {
6933 /* Ok, we found a frame. Store all the values.
6934 last_mouse_glyph is a rectangle used to reduce the
6935 generation of mouse events. To not miss any motion
6936 events, we must divide the frame into rectangles of the
6937 size of the smallest character that could be displayed
6938 on it, i.e. into the same rectangles that matrices on
6939 the frame are divided into. */
6940
6941 #if OLD_REDISPLAY_CODE
6942 int ignore1, ignore2;
6943 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
6944 &last_mouse_glyph,
6945 FRAME_X_DISPLAY_INFO (f1)->grabbed
6946 || insist);
6947 #else
6948 {
6949 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
6950 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
6951 int x = win_x;
6952 int y = win_y;
6953
6954 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
6955 round down even for negative values. */
6956 if (x < 0)
6957 x -= width - 1;
6958 if (y < 0)
6959 y -= height - 1;
6960
6961 last_mouse_glyph.width = width;
6962 last_mouse_glyph.height = height;
6963 last_mouse_glyph.x = (x + width - 1) / width * width;
6964 last_mouse_glyph.y = (y + height - 1) / height * height;
6965 }
6966 #endif
6967
6968 *bar_window = Qnil;
6969 *part = 0;
6970 *fp = f1;
6971 XSETINT (*x, win_x);
6972 XSETINT (*y, win_y);
6973 *time = last_mouse_movement_time;
6974 }
6975 }
6976 }
6977
6978 UNBLOCK_INPUT;
6979 }
6980
6981
6982 DEFUN ("xt-process-timeouts", Fxt_process_timeouts, Sxt_process_timeouts,
6983 0, 0, 0,
6984 "Arrange for Xt timeout callbacks to be called.")
6985 ()
6986 {
6987 #ifdef USE_X_TOOLKIT
6988 BLOCK_INPUT;
6989 while (XtAppPending (Xt_app_con) & XtIMTimer)
6990 XtAppProcessEvent (Xt_app_con, XtIMTimer);
6991 UNBLOCK_INPUT;
6992 #endif /* USE_X_TOOLKIT */
6993
6994 return Qnil;
6995 }
6996
6997
6998 \f
6999 /* Scroll bar support. */
7000
7001 /* Given an X window ID, find the struct scroll_bar which manages it.
7002 This can be called in GC, so we have to make sure to strip off mark
7003 bits. */
7004 static struct scroll_bar *
7005 x_window_to_scroll_bar (window_id)
7006 Window window_id;
7007 {
7008 Lisp_Object tail;
7009
7010 for (tail = Vframe_list;
7011 XGCTYPE (tail) == Lisp_Cons;
7012 tail = XCONS (tail)->cdr)
7013 {
7014 Lisp_Object frame, bar, condemned;
7015
7016 frame = XCONS (tail)->car;
7017 /* All elements of Vframe_list should be frames. */
7018 if (! GC_FRAMEP (frame))
7019 abort ();
7020
7021 /* Scan this frame's scroll bar list for a scroll bar with the
7022 right window ID. */
7023 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7024 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7025 /* This trick allows us to search both the ordinary and
7026 condemned scroll bar lists with one loop. */
7027 ! GC_NILP (bar) || (bar = condemned,
7028 condemned = Qnil,
7029 ! GC_NILP (bar));
7030 bar = XSCROLL_BAR (bar)->next)
7031 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7032 return XSCROLL_BAR (bar);
7033 }
7034
7035 return 0;
7036 }
7037
7038
7039 \f
7040 /************************************************************************
7041 Toolkit scroll bars
7042 ************************************************************************/
7043
7044 #if USE_TOOLKIT_SCROLL_BARS
7045
7046 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7047 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7048 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7049 struct scroll_bar *));
7050 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7051 int, int, int));
7052
7053
7054 /* Id of action hook installed for scroll bars. */
7055
7056 static XtActionHookId action_hook_id;
7057
7058 /* Lisp window being scrolled. Set when starting to interact with
7059 a toolkit scroll bar, reset to nil when ending the interaction. */
7060
7061 static Lisp_Object window_being_scrolled;
7062
7063 /* Last scroll bar part sent in xm_scroll_callback. */
7064
7065 static int last_scroll_bar_part;
7066
7067
7068 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7069 bars are used.. The hoos is responsible for detecting when
7070 the user ends an interaction with the scroll bar, and generates
7071 a `end-scroll' scroll_bar_click' event if so. */
7072
7073 static void
7074 xt_action_hook (widget, client_data, action_name, event, params,
7075 num_params)
7076 Widget widget;
7077 XtPointer client_data;
7078 String action_name;
7079 XEvent *event;
7080 String *params;
7081 Cardinal *num_params;
7082 {
7083 int scroll_bar_p;
7084 char *end_action;
7085
7086 #ifdef USE_MOTIF
7087 scroll_bar_p = XmIsScrollBar (widget);
7088 end_action = "Release";
7089 #elif defined HAVE_XAW3D
7090 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7091 end_action = "EndScroll";
7092 #else
7093 #error unknown scroll bar toolkit
7094 #endif /* HAVE_XAW3D */
7095
7096 /* Although LessTif uses XtTimeouts like Xaw3d, the timer hack to
7097 let Xt timeouts be processed doesn't work. */
7098 if (scroll_bar_p
7099 && strcmp (action_name, end_action) == 0
7100 && WINDOWP (window_being_scrolled))
7101 {
7102 struct window *w;
7103
7104 x_send_scroll_bar_event (window_being_scrolled,
7105 scroll_bar_end_scroll, 0, 0);
7106 w = XWINDOW (window_being_scrolled);
7107 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7108 window_being_scrolled = Qnil;
7109 last_scroll_bar_part = -1;
7110 }
7111 }
7112
7113
7114 /* Send a client message with message type Xatom_Scrollbar for a
7115 scroll action to the frame of WINDOW. PART is a value identifying
7116 the part of the scroll bar that was clicked on. PORTION is the
7117 amount to scroll of a whole of WHOLE. */
7118
7119 static void
7120 x_send_scroll_bar_event (window, part, portion, whole)
7121 Lisp_Object window;
7122 int part, portion, whole;
7123 {
7124 XEvent event;
7125 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7126 struct frame *f = XFRAME (XWINDOW (window)->frame);
7127
7128 /* Construct a ClientMessage event to send to the frame. */
7129 ev->type = ClientMessage;
7130 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7131 ev->display = FRAME_X_DISPLAY (f);
7132 ev->window = FRAME_X_WINDOW (f);
7133 ev->format = 32;
7134 ev->data.l[0] = (long) window;
7135 ev->data.l[1] = (long) part;
7136 ev->data.l[2] = (long) 0;
7137 ev->data.l[3] = (long) portion;
7138 ev->data.l[4] = (long) whole;
7139
7140 /* Setting the event mask to zero means that the message will
7141 be sent to the client that created the window, and if that
7142 window no longer exists, no event will be sent. */
7143 BLOCK_INPUT;
7144 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7145 UNBLOCK_INPUT;
7146 }
7147
7148
7149 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7150 in *IEVENT. */
7151
7152 static void
7153 x_scroll_bar_to_input_event (event, ievent)
7154 XEvent *event;
7155 struct input_event *ievent;
7156 {
7157 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7158 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7159 struct frame *f = XFRAME (XWINDOW (window)->frame);
7160
7161 ievent->kind = scroll_bar_click;
7162 ievent->frame_or_window = window;
7163 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7164 ievent->part = ev->data.l[1];
7165 ievent->code = ev->data.l[2];
7166 ievent->x = make_number ((int) ev->data.l[3]);
7167 ievent->y = make_number ((int) ev->data.l[4]);
7168 ievent->modifiers = 0;
7169 }
7170
7171
7172 #ifdef USE_MOTIF
7173
7174 /* Minimum and maximum values used for Motif scroll bars. */
7175
7176 #define XM_SB_MIN 1
7177 #define XM_SB_MAX 10000000
7178 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7179
7180
7181 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7182 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7183 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7184
7185 static void
7186 xm_scroll_callback (widget, client_data, call_data)
7187 Widget widget;
7188 XtPointer client_data, call_data;
7189 {
7190 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7191 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7192 double percent;
7193 int part = -1, whole = 0, portion = 0;
7194
7195 switch (cs->reason)
7196 {
7197 case XmCR_DECREMENT:
7198 bar->dragging = Qnil;
7199 part = scroll_bar_up_arrow;
7200 break;
7201
7202 case XmCR_INCREMENT:
7203 bar->dragging = Qnil;
7204 part = scroll_bar_down_arrow;
7205 break;
7206
7207 case XmCR_PAGE_DECREMENT:
7208 bar->dragging = Qnil;
7209 part = scroll_bar_above_handle;
7210 break;
7211
7212 case XmCR_PAGE_INCREMENT:
7213 bar->dragging = Qnil;
7214 part = scroll_bar_below_handle;
7215 break;
7216
7217 case XmCR_TO_TOP:
7218 bar->dragging = Qnil;
7219 part = scroll_bar_to_top;
7220 break;
7221
7222 case XmCR_TO_BOTTOM:
7223 bar->dragging = Qnil;
7224 part = scroll_bar_to_bottom;
7225 break;
7226
7227 case XmCR_DRAG:
7228 {
7229 int slider_size;
7230 int dragging_down_p = (INTEGERP (bar->dragging)
7231 && XINT (bar->dragging) <= cs->value);
7232
7233 /* Get the slider size. */
7234 BLOCK_INPUT;
7235 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7236 UNBLOCK_INPUT;
7237
7238 /* At the max position of the scroll bar, do a line-wise
7239 movement. Without doing anything, the LessTif scroll bar
7240 calls us with the same cs->value again and again. If we
7241 want to make sure that we can reach the end of the buffer,
7242 we have to do something.
7243
7244 Implementation note: setting bar->dragging always to
7245 cs->value gives a smoother movement at the max position.
7246 Setting it to nil when doing line-wise movement gives
7247 a better slider behavior. */
7248
7249 if (cs->value + slider_size == XM_SB_MAX
7250 || (dragging_down_p
7251 && last_scroll_bar_part == scroll_bar_down_arrow))
7252 {
7253 part = scroll_bar_down_arrow;
7254 bar->dragging = Qnil;
7255 }
7256 else
7257 {
7258 whole = XM_SB_RANGE;
7259 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7260 part = scroll_bar_handle;
7261 bar->dragging = make_number (cs->value);
7262 }
7263 }
7264 break;
7265
7266 case XmCR_VALUE_CHANGED:
7267 break;
7268 };
7269
7270 if (part >= 0)
7271 {
7272 window_being_scrolled = bar->window;
7273 last_scroll_bar_part = part;
7274 x_send_scroll_bar_event (bar->window, part, portion, whole);
7275 }
7276 }
7277
7278
7279 #else /* not USE_MOTIF, i.e. XAW3D. */
7280
7281
7282 /* Xaw3d scroll bar callback. Invoked when the thumb is dragged.
7283 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7284 scroll bar struct. CALL_DATA is a pointer to a float saying where
7285 the thumb is. */
7286
7287 static void
7288 xaw3d_jump_callback (widget, client_data, call_data)
7289 Widget widget;
7290 XtPointer client_data, call_data;
7291 {
7292 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7293 float top = *(float *) call_data;
7294 float shown;
7295 int whole, portion;
7296 int dragging_down_p, part;
7297 double epsilon = 0.01;
7298
7299 /* Get the size of the thumb, a value between 0 and 1. */
7300 BLOCK_INPUT;
7301 XtVaGetValues (widget, XtNshown, &shown, NULL);
7302 UNBLOCK_INPUT;
7303
7304 whole = 10000000;
7305 portion = shown < 1 ? top * whole : 0;
7306 dragging_down_p = (INTEGERP (bar->dragging)
7307 && XINT (bar->dragging) < portion);
7308
7309 if (shown < 1
7310 && (abs (top + shown - 1) < epsilon
7311 || (dragging_down_p
7312 && last_scroll_bar_part == scroll_bar_down_arrow)))
7313 part = scroll_bar_down_arrow;
7314 else
7315 part = scroll_bar_handle;
7316
7317 window_being_scrolled = bar->window;
7318 bar->dragging = make_number (portion);
7319 last_scroll_bar_part = part;
7320 x_send_scroll_bar_event (bar->window, part, portion, whole);
7321 }
7322
7323
7324 /* Xaw3d scroll bar callback. Invoked for incremental scrolling.,
7325 i.e. line or page up or down. WIDGET is the Xaw3d scroll bar
7326 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7327 the scroll bar. CALL_DATA is an integer specifying the action that
7328 has taken place. It's magnitude is in the range 0..height of the
7329 scroll bar. Negative values mean scroll towards buffer start.
7330 Values < height of scroll bar mean line-wise movement. */
7331
7332 static void
7333 xaw3d_scroll_callback (widget, client_data, call_data)
7334 Widget widget;
7335 XtPointer client_data, call_data;
7336 {
7337 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7338 int position = (int) call_data;
7339 Dimension height;
7340 int part;
7341
7342 /* Get the height of the scroll bar. */
7343 BLOCK_INPUT;
7344 XtVaGetValues (widget, XtNheight, &height, NULL);
7345 UNBLOCK_INPUT;
7346
7347 if (position < 0)
7348 {
7349 if (abs (position) < height)
7350 part = scroll_bar_up_arrow;
7351 else
7352 part = scroll_bar_above_handle;
7353 }
7354 else
7355 {
7356 if (abs (position) < height)
7357 part = scroll_bar_down_arrow;
7358 else
7359 part = scroll_bar_below_handle;
7360 }
7361
7362 window_being_scrolled = bar->window;
7363 bar->dragging = Qnil;
7364 last_scroll_bar_part = part;
7365 x_send_scroll_bar_event (bar->window, part, 0, 0);
7366 }
7367
7368
7369 #endif /* not USE_MOTIF */
7370
7371
7372 /* Create the widget for scroll bar BAR on frame F. Record the widget
7373 and X window of the scroll bar in BAR. */
7374
7375 static void
7376 x_create_toolkit_scroll_bar (f, bar)
7377 struct frame *f;
7378 struct scroll_bar *bar;
7379 {
7380 Window xwindow;
7381 Widget widget;
7382 Arg av[20];
7383 int ac = 0;
7384 char *scroll_bar_name = "verticalScrollBar";
7385 unsigned long pixel;
7386
7387 BLOCK_INPUT;
7388
7389 #ifdef USE_MOTIF
7390 /* LessTif 0.85, problems:
7391
7392 1. When the mouse if over the scroll bar, the scroll bar will
7393 get keyboard events. I didn't find a way to turn this off.
7394
7395 2. Do we have to explicitly set the cursor to get an arrow
7396 cursor (see below)? */
7397
7398 /* Set resources. Create the widget. */
7399 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7400 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7401 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7402 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7403 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7404 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7405 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7406
7407 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7408 if (pixel != -1)
7409 {
7410 XtSetArg (av[ac], XmNforeground, pixel);
7411 ++ac;
7412 }
7413
7414 pixel = f->output_data.x->scroll_bar_background_pixel;
7415 if (pixel != -1)
7416 {
7417 XtSetArg (av[ac], XmNbackground, pixel);
7418 ++ac;
7419 }
7420
7421 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7422 scroll_bar_name, av, ac);
7423
7424 /* Add one callback for everything that can happen. */
7425 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7426 (XtPointer) bar);
7427 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7428 (XtPointer) bar);
7429 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7430 (XtPointer) bar);
7431 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7432 (XtPointer) bar);
7433 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7434 (XtPointer) bar);
7435 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7436 (XtPointer) bar);
7437 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7438 (XtPointer) bar);
7439
7440 /* Realize the widget. Only after that is the X window created. */
7441 XtRealizeWidget (widget);
7442
7443 /* Set the cursor to an arrow. I didn't find a resource to do that.
7444 And I'm wondering why it hasn't an arrow cursor by default. */
7445 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7446 f->output_data.x->nontext_cursor);
7447
7448 #elif defined HAVE_XAW3D
7449
7450 /* Set resources. Create the widget. The background of the
7451 Xaw3d scroll bar widget is a little bit light for my taste.
7452 We don't alter it here to let users change it according
7453 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7454 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7455 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7456 XtSetArg (av[ac], XtNcursorName, "left_ptr"); ++ac;
7457 XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac;
7458
7459 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7460 if (pixel != -1)
7461 {
7462 XtSetArg (av[ac], XtNforeground, pixel);
7463 ++ac;
7464 }
7465
7466 pixel = f->output_data.x->scroll_bar_background_pixel;
7467 if (pixel != -1)
7468 {
7469 XtSetArg (av[ac], XtNbackground, pixel);
7470 ++ac;
7471 }
7472
7473 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7474 f->output_data.x->edit_widget, av, ac);
7475
7476 /* Define callbacks. */
7477 XtAddCallback (widget, XtNjumpProc, xaw3d_jump_callback, (XtPointer) bar);
7478 XtAddCallback (widget, XtNscrollProc, xaw3d_scroll_callback,
7479 (XtPointer) bar);
7480
7481 /* Realize the widget. Only after that is the X window created. */
7482 XtRealizeWidget (widget);
7483
7484 #endif /* HAVE_XAW3D */
7485
7486 /* Install an action hook that let's us detect when the user
7487 finishes interacting with a scroll bar. */
7488 if (action_hook_id == 0)
7489 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7490
7491 /* Remember X window and widget in the scroll bar vector. */
7492 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7493 xwindow = XtWindow (widget);
7494 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7495
7496 UNBLOCK_INPUT;
7497 }
7498
7499
7500 /* Set the thumb size and position of scroll bar BAR. We are currently
7501 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7502
7503 static void
7504 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7505 struct scroll_bar *bar;
7506 int portion, position, whole;
7507 {
7508 float top, shown;
7509 Arg av[2];
7510 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7511
7512 if (whole == 0)
7513 top = 0, shown = 1;
7514 else
7515 {
7516 top = (float) position / whole;
7517 shown = (float) portion / whole;
7518 }
7519
7520 BLOCK_INPUT;
7521
7522 #ifdef USE_MOTIF
7523 {
7524 int size, value;
7525 Boolean arrow1_selected, arrow2_selected;
7526 unsigned char flags;
7527 XmScrollBarWidget sb;
7528
7529 /* Slider size. Must be in the range [1 .. MAX - MIN] where NAX
7530 is the scroll bar's maximum and MIN is the scroll bar's minimum
7531 value. */
7532 size = shown * XM_SB_RANGE;
7533 size = min (size, XM_SB_RANGE);
7534 size = max (size, 1);
7535
7536 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7537 value = top * XM_SB_RANGE;
7538 value = min (value, XM_SB_MAX - size);
7539 value = max (value, XM_SB_MIN);
7540
7541 /* LessTif: Calling XmScrollBarSetValues after an increment or
7542 decrement turns off auto-repeat LessTif-internally. This can
7543 be seen in ScrollBar.c which resets Arrow1Selected and
7544 Arrow2Selected. It also sets internal flags so that LessTif
7545 believes the mouse is in the slider. We either have to change
7546 our code, or work around that by accessing private data. */
7547
7548 sb = (XmScrollBarWidget) widget;
7549 arrow1_selected = sb->scrollBar.arrow1_selected;
7550 arrow2_selected = sb->scrollBar.arrow2_selected;
7551 flags = sb->scrollBar.flags;
7552
7553 if (NILP (bar->dragging))
7554 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7555 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7556 /* This has the negative side effect that the slider value is
7557 not would it would be if we scrolled here using line-wise or
7558 page-wise movement. */
7559 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7560 else
7561 {
7562 /* If currently dragging, only update the slider size.
7563 This reduces flicker effects. */
7564 int old_value, old_size, increment, page_increment;
7565
7566 XmScrollBarGetValues (widget, &old_value, &old_size,
7567 &increment, &page_increment);
7568 XmScrollBarSetValues (widget, old_value,
7569 min (size, XM_SB_RANGE - old_value),
7570 0, 0, False);
7571 }
7572
7573 sb->scrollBar.arrow1_selected = arrow1_selected;
7574 sb->scrollBar.arrow2_selected = arrow2_selected;
7575 sb->scrollBar.flags = flags;
7576 }
7577 #elif defined HAVE_XAW3D
7578 {
7579 /* Restrict to [0 1]. */
7580 top = max (0, min (1, top));
7581 shown = max (0, min (1, shown));
7582
7583 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7584 check that your system's configuration file contains a define
7585 for `NARROWPROTO'. See s/freebsd.h for an example. */
7586 if (NILP (bar->dragging))
7587 {
7588 float old_top, old_shown;
7589 XtVaGetValues (widget, XtNtopOfThumb, &old_top, XtNshown, &old_shown,
7590 NULL);
7591 if (top != old_top || shown != old_shown)
7592 XawScrollbarSetThumb (widget, top, shown);
7593 }
7594 else
7595 {
7596 ScrollbarWidget sb = (ScrollbarWidget) widget;
7597 int scroll_mode = sb->scrollbar.scroll_mode;
7598
7599 sb->scrollbar.scroll_mode = 0;
7600
7601 if (last_scroll_bar_part == scroll_bar_down_arrow)
7602 XawScrollbarSetThumb (widget, top, 1 - top);
7603 else
7604 {
7605 float old_top;
7606 XtVaGetValues (widget, XtNtopOfThumb, &old_top, NULL);
7607 XawScrollbarSetThumb (widget, old_top, min (shown, 1 - old_top));
7608 }
7609
7610 sb->scrollbar.scroll_mode = scroll_mode;
7611 }
7612 }
7613 #endif /* HAVE_XAW3D */
7614
7615 UNBLOCK_INPUT;
7616 }
7617
7618 #endif /* USE_TOOLKIT_SCROLL_BARS */
7619
7620
7621 \f
7622 /************************************************************************
7623 Scroll bars, general
7624 ************************************************************************/
7625
7626 /* Create a scroll bar and return the scroll bar vector for it. W is
7627 the Emacs window on which to create the scroll bar. TOP, LEFT,
7628 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7629 scroll bar. */
7630
7631 static struct scroll_bar *
7632 x_scroll_bar_create (w, top, left, width, height)
7633 struct window *w;
7634 int top, left, width, height;
7635 {
7636 struct frame *f = XFRAME (w->frame);
7637 #ifdef USE_X_TOOLKIT
7638 Arg av[10];
7639 #endif
7640 int ac = 0;
7641 Window window;
7642 struct scroll_bar *bar
7643 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7644
7645 BLOCK_INPUT;
7646
7647 #if USE_TOOLKIT_SCROLL_BARS
7648 x_create_toolkit_scroll_bar (f, bar);
7649 #else /* not USE_TOOLKIT_SCROLL_BARS */
7650 {
7651 XSetWindowAttributes a;
7652 unsigned long mask;
7653
7654 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7655 if (a.background_pixel == -1)
7656 a.background_pixel = f->output_data.x->background_pixel;
7657
7658 a.event_mask = (ButtonPressMask | ButtonReleaseMask
7659 | ButtonMotionMask | PointerMotionHintMask
7660 | ExposureMask);
7661 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
7662
7663 mask = (CWBackPixel | CWEventMask | CWCursor);
7664
7665 /* Clear the area of W that will serve as a scroll bar. This is
7666 for the case that a window has been split horizontally. In
7667 this case, no clear_frame is generated to reduce flickering. */
7668 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7669 left, top, width,
7670 window_box_height (w), False);
7671
7672 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7673 /* Position and size of scroll bar. */
7674 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7675 top,
7676 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7677 height,
7678 /* Border width, depth, class, and visual. */
7679 0,
7680 CopyFromParent,
7681 CopyFromParent,
7682 CopyFromParent,
7683 /* Attributes. */
7684 mask, &a);
7685 SET_SCROLL_BAR_X_WINDOW (bar, window);
7686 }
7687 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7688
7689 XSETWINDOW (bar->window, w);
7690 XSETINT (bar->top, top);
7691 XSETINT (bar->left, left);
7692 XSETINT (bar->width, width);
7693 XSETINT (bar->height, height);
7694 XSETINT (bar->start, 0);
7695 XSETINT (bar->end, 0);
7696 bar->dragging = Qnil;
7697
7698 /* Add bar to its frame's list of scroll bars. */
7699 bar->next = FRAME_SCROLL_BARS (f);
7700 bar->prev = Qnil;
7701 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7702 if (!NILP (bar->next))
7703 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7704
7705 /* Map the window/widget. */
7706 #if USE_TOOLKIT_SCROLL_BARS
7707 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7708 left, top,
7709 FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f),
7710 window_box_height (w), False);
7711 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7712 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7713 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7714 top,
7715 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7716 height, 0);
7717 #else /* not USE_TOOLKIT_SCROLL_BARS */
7718 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
7719 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7720
7721 UNBLOCK_INPUT;
7722 return bar;
7723 }
7724
7725
7726 /* Draw BAR's handle in the proper position.
7727
7728 If the handle is already drawn from START to END, don't bother
7729 redrawing it, unless REBUILD is non-zero; in that case, always
7730 redraw it. (REBUILD is handy for drawing the handle after expose
7731 events.)
7732
7733 Normally, we want to constrain the start and end of the handle to
7734 fit inside its rectangle, but if the user is dragging the scroll
7735 bar handle, we want to let them drag it down all the way, so that
7736 the bar's top is as far down as it goes; otherwise, there's no way
7737 to move to the very end of the buffer. */
7738
7739 static void
7740 x_scroll_bar_set_handle (bar, start, end, rebuild)
7741 struct scroll_bar *bar;
7742 int start, end;
7743 int rebuild;
7744 {
7745 #ifndef USE_TOOLKIT_SCROLL_BARS
7746 int dragging = ! NILP (bar->dragging);
7747 Window w = SCROLL_BAR_X_WINDOW (bar);
7748 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7749 GC gc = f->output_data.x->normal_gc;
7750
7751 /* If the display is already accurate, do nothing. */
7752 if (! rebuild
7753 && start == XINT (bar->start)
7754 && end == XINT (bar->end))
7755 return;
7756
7757 BLOCK_INPUT;
7758
7759 {
7760 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7761 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7762 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7763
7764 /* Make sure the values are reasonable, and try to preserve
7765 the distance between start and end. */
7766 {
7767 int length = end - start;
7768
7769 if (start < 0)
7770 start = 0;
7771 else if (start > top_range)
7772 start = top_range;
7773 end = start + length;
7774
7775 if (end < start)
7776 end = start;
7777 else if (end > top_range && ! dragging)
7778 end = top_range;
7779 }
7780
7781 /* Store the adjusted setting in the scroll bar. */
7782 XSETINT (bar->start, start);
7783 XSETINT (bar->end, end);
7784
7785 /* Clip the end position, just for display. */
7786 if (end > top_range)
7787 end = top_range;
7788
7789 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
7790 below top positions, to make sure the handle is always at least
7791 that many pixels tall. */
7792 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
7793
7794 /* Draw the empty space above the handle. Note that we can't clear
7795 zero-height areas; that means "clear to end of window." */
7796 if (0 < start)
7797 XClearArea (FRAME_X_DISPLAY (f), w,
7798
7799 /* x, y, width, height, and exposures. */
7800 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7801 VERTICAL_SCROLL_BAR_TOP_BORDER,
7802 inside_width, start,
7803 False);
7804
7805 /* Change to proper foreground color if one is specified. */
7806 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7807 XSetForeground (FRAME_X_DISPLAY (f), gc,
7808 f->output_data.x->scroll_bar_foreground_pixel);
7809
7810 /* Draw the handle itself. */
7811 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
7812
7813 /* x, y, width, height */
7814 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7815 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
7816 inside_width, end - start);
7817
7818 /* Restore the foreground color of the GC if we changed it above. */
7819 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7820 XSetForeground (FRAME_X_DISPLAY (f), gc,
7821 f->output_data.x->foreground_pixel);
7822
7823 /* Draw the empty space below the handle. Note that we can't
7824 clear zero-height areas; that means "clear to end of window." */
7825 if (end < inside_height)
7826 XClearArea (FRAME_X_DISPLAY (f), w,
7827
7828 /* x, y, width, height, and exposures. */
7829 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7830 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
7831 inside_width, inside_height - end,
7832 False);
7833
7834 }
7835
7836 UNBLOCK_INPUT;
7837 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7838 }
7839
7840
7841 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7842 nil. */
7843
7844 static void
7845 x_scroll_bar_remove (bar)
7846 struct scroll_bar *bar;
7847 {
7848 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7849
7850 BLOCK_INPUT;
7851
7852 #if USE_TOOLKIT_SCROLL_BARS
7853 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
7854 #else /* not USE_TOOLKIT_SCROLL_BARS */
7855 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
7856 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7857
7858 /* Disassociate this scroll bar from its window. */
7859 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
7860
7861 UNBLOCK_INPUT;
7862 }
7863
7864
7865 /* Set the handle of the vertical scroll bar for WINDOW to indicate
7866 that we are displaying PORTION characters out of a total of WHOLE
7867 characters, starting at POSITION. If WINDOW has no scroll bar,
7868 create one. */
7869
7870 static void
7871 XTset_vertical_scroll_bar (w, portion, whole, position)
7872 struct window *w;
7873 int portion, whole, position;
7874 {
7875 struct frame *f = XFRAME (w->frame);
7876 struct scroll_bar *bar;
7877 int pixel_top, pixel_left, pixel_width, pixel_height;
7878 int window_x, window_y, window_width, window_height;
7879 int scroll_bar_area_width;
7880
7881 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7882
7883 /* Where should this scroll bar be, pixel-wise? */
7884 pixel_top = window_y;
7885 pixel_height = window_height;
7886
7887 /* The width of the scroll bar itself. */
7888 pixel_width = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
7889 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7890 : (FRAME_SCROLL_BAR_COLS (f)
7891 * FONT_WIDTH (FRAME_FONT (f))));
7892
7893 /* The width on the screen reserved for the scroll bar plus maybe
7894 some empty room at both sides of the scroll bar. */
7895 scroll_bar_area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7896
7897 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7898 pixel_left = (window_x
7899 + window_width
7900 + FRAME_LEFT_FLAGS_AREA_WIDTH (f)
7901 + scroll_bar_area_width
7902 - pixel_width + 1);
7903 else
7904 pixel_left = (window_x
7905 - FRAME_LEFT_FLAGS_AREA_WIDTH (f)
7906 - scroll_bar_area_width);
7907
7908 /* Does the scroll bar exist yet? */
7909 if (NILP (w->vertical_scroll_bar))
7910 bar = x_scroll_bar_create (w, pixel_top, pixel_left, pixel_width,
7911 pixel_height);
7912 else
7913 {
7914 /* It may just need to be moved and resized. */
7915 unsigned int mask = 0;
7916
7917 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7918
7919 BLOCK_INPUT;
7920
7921 if (pixel_left != XINT (bar->left))
7922 mask |= CWX;
7923 if (pixel_top != XINT (bar->top))
7924 mask |= CWY;
7925 if (pixel_width != XINT (bar->width))
7926 mask |= CWWidth;
7927 if (pixel_height != XINT (bar->height))
7928 mask |= CWHeight;
7929
7930 #ifdef USE_TOOLKIT_SCROLL_BARS
7931
7932 /* Since toolkit scroll bars are smaller than the space reserved
7933 for them on the frame, we have to clear "under" them. */
7934 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7935 pixel_left, pixel_top,
7936 FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f),
7937 pixel_height, False);
7938
7939 /* Move/size the scroll bar widget. */
7940 if (mask)
7941 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7942 pixel_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7943 pixel_top,
7944 pixel_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7945 pixel_height, 0);
7946
7947 #else /* not USE_TOOLKIT_SCROLL_BARS */
7948
7949 /* Clear areas not covered by the scroll bar. This makes sure a
7950 previous mode line display is cleared after C-x 2 C-x 1, for
7951 example. Non-toolkit scroll bars are as wide as the area
7952 reserved for scroll bars - trim at both sides. */
7953 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7954 pixel_left, pixel_top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7955 pixel_height, False);
7956 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7957 (pixel_left
7958 + pixel_width
7959 - VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7960 pixel_top,
7961 VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7962 pixel_height, False);
7963
7964 /* Move/size the scroll bar window. */
7965 if (mask)
7966 {
7967 XWindowChanges wc;
7968
7969 wc.x = pixel_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
7970 wc.y = pixel_top;
7971 wc.width = pixel_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
7972 wc.height = pixel_height;
7973 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
7974 mask, &wc);
7975 }
7976
7977 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7978
7979 /* Remember new settings. */
7980 XSETINT (bar->left, pixel_left);
7981 XSETINT (bar->top, pixel_top);
7982 XSETINT (bar->width, pixel_width);
7983 XSETINT (bar->height, pixel_height);
7984
7985 UNBLOCK_INPUT;
7986 }
7987
7988 #if USE_TOOLKIT_SCROLL_BARS
7989 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
7990 #else /* not USE_TOOLKIT_SCROLL_BARS */
7991 /* Set the scroll bar's current state, unless we're currently being
7992 dragged. */
7993 if (NILP (bar->dragging))
7994 {
7995 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, pixel_height);
7996
7997 if (whole == 0)
7998 x_scroll_bar_set_handle (bar, 0, top_range, 0);
7999 else
8000 {
8001 int start = ((double) position * top_range) / whole;
8002 int end = ((double) (position + portion) * top_range) / whole;
8003 x_scroll_bar_set_handle (bar, start, end, 0);
8004 }
8005 }
8006 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8007
8008 XSETVECTOR (w->vertical_scroll_bar, bar);
8009 }
8010
8011
8012 /* The following three hooks are used when we're doing a thorough
8013 redisplay of the frame. We don't explicitly know which scroll bars
8014 are going to be deleted, because keeping track of when windows go
8015 away is a real pain - "Can you say set-window-configuration, boys
8016 and girls?" Instead, we just assert at the beginning of redisplay
8017 that *all* scroll bars are to be removed, and then save a scroll bar
8018 from the fiery pit when we actually redisplay its window. */
8019
8020 /* Arrange for all scroll bars on FRAME to be removed at the next call
8021 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8022 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8023
8024 static void
8025 XTcondemn_scroll_bars (frame)
8026 FRAME_PTR frame;
8027 {
8028 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8029 while (! NILP (FRAME_SCROLL_BARS (frame)))
8030 {
8031 Lisp_Object bar;
8032 bar = FRAME_SCROLL_BARS (frame);
8033 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8034 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8035 XSCROLL_BAR (bar)->prev = Qnil;
8036 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8037 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8038 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8039 }
8040 }
8041
8042 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8043 Note that WINDOW isn't necessarily condemned at all. */
8044 static void
8045 XTredeem_scroll_bar (window)
8046 struct window *window;
8047 {
8048 struct scroll_bar *bar;
8049
8050 /* We can't redeem this window's scroll bar if it doesn't have one. */
8051 if (NILP (window->vertical_scroll_bar))
8052 abort ();
8053
8054 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8055
8056 /* Unlink it from the condemned list. */
8057 {
8058 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8059
8060 if (NILP (bar->prev))
8061 {
8062 /* If the prev pointer is nil, it must be the first in one of
8063 the lists. */
8064 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8065 /* It's not condemned. Everything's fine. */
8066 return;
8067 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8068 window->vertical_scroll_bar))
8069 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8070 else
8071 /* If its prev pointer is nil, it must be at the front of
8072 one or the other! */
8073 abort ();
8074 }
8075 else
8076 XSCROLL_BAR (bar->prev)->next = bar->next;
8077
8078 if (! NILP (bar->next))
8079 XSCROLL_BAR (bar->next)->prev = bar->prev;
8080
8081 bar->next = FRAME_SCROLL_BARS (f);
8082 bar->prev = Qnil;
8083 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8084 if (! NILP (bar->next))
8085 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8086 }
8087 }
8088
8089 /* Remove all scroll bars on FRAME that haven't been saved since the
8090 last call to `*condemn_scroll_bars_hook'. */
8091
8092 static void
8093 XTjudge_scroll_bars (f)
8094 FRAME_PTR f;
8095 {
8096 Lisp_Object bar, next;
8097
8098 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8099
8100 /* Clear out the condemned list now so we won't try to process any
8101 more events on the hapless scroll bars. */
8102 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8103
8104 for (; ! NILP (bar); bar = next)
8105 {
8106 struct scroll_bar *b = XSCROLL_BAR (bar);
8107
8108 x_scroll_bar_remove (b);
8109
8110 next = b->next;
8111 b->next = b->prev = Qnil;
8112 }
8113
8114 /* Now there should be no references to the condemned scroll bars,
8115 and they should get garbage-collected. */
8116 }
8117
8118
8119 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8120 is a no-op when using toolkit scroll bars.
8121
8122 This may be called from a signal handler, so we have to ignore GC
8123 mark bits. */
8124
8125 static void
8126 x_scroll_bar_expose (bar, event)
8127 struct scroll_bar *bar;
8128 XEvent *event;
8129 {
8130 #ifndef USE_TOOLKIT_SCROLL_BARS
8131
8132 Window w = SCROLL_BAR_X_WINDOW (bar);
8133 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8134 GC gc = f->output_data.x->normal_gc;
8135 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8136
8137 BLOCK_INPUT;
8138
8139 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8140
8141 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8142 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8143
8144 /* x, y, width, height */
8145 0, 0,
8146 XINT (bar->width) - 1 - width_trim - width_trim,
8147 XINT (bar->height) - 1);
8148
8149 UNBLOCK_INPUT;
8150
8151 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8152 }
8153
8154 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8155 is set to something other than no_event, it is enqueued.
8156
8157 This may be called from a signal handler, so we have to ignore GC
8158 mark bits. */
8159
8160 static void
8161 x_scroll_bar_handle_click (bar, event, emacs_event)
8162 struct scroll_bar *bar;
8163 XEvent *event;
8164 struct input_event *emacs_event;
8165 {
8166 if (! GC_WINDOWP (bar->window))
8167 abort ();
8168
8169 emacs_event->kind = scroll_bar_click;
8170 emacs_event->code = event->xbutton.button - Button1;
8171 emacs_event->modifiers
8172 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8173 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8174 event->xbutton.state)
8175 | (event->type == ButtonRelease
8176 ? up_modifier
8177 : down_modifier));
8178 emacs_event->frame_or_window = bar->window;
8179 emacs_event->timestamp = event->xbutton.time;
8180 {
8181 #if 0
8182 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8183 int internal_height
8184 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8185 #endif
8186 int top_range
8187 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8188 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8189
8190 if (y < 0) y = 0;
8191 if (y > top_range) y = top_range;
8192
8193 if (y < XINT (bar->start))
8194 emacs_event->part = scroll_bar_above_handle;
8195 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8196 emacs_event->part = scroll_bar_handle;
8197 else
8198 emacs_event->part = scroll_bar_below_handle;
8199
8200 /* Just because the user has clicked on the handle doesn't mean
8201 they want to drag it. Lisp code needs to be able to decide
8202 whether or not we're dragging. */
8203 #if 0
8204 /* If the user has just clicked on the handle, record where they're
8205 holding it. */
8206 if (event->type == ButtonPress
8207 && emacs_event->part == scroll_bar_handle)
8208 XSETINT (bar->dragging, y - XINT (bar->start));
8209 #endif
8210
8211 /* If the user has released the handle, set it to its final position. */
8212 if (event->type == ButtonRelease
8213 && ! NILP (bar->dragging))
8214 {
8215 int new_start = y - XINT (bar->dragging);
8216 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8217
8218 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8219 bar->dragging = Qnil;
8220 }
8221
8222 /* Same deal here as the other #if 0. */
8223 #if 0
8224 /* Clicks on the handle are always reported as occurring at the top of
8225 the handle. */
8226 if (emacs_event->part == scroll_bar_handle)
8227 emacs_event->x = bar->start;
8228 else
8229 XSETINT (emacs_event->x, y);
8230 #else
8231 XSETINT (emacs_event->x, y);
8232 #endif
8233
8234 XSETINT (emacs_event->y, top_range);
8235 }
8236 }
8237
8238 /* Handle some mouse motion while someone is dragging the scroll bar.
8239
8240 This may be called from a signal handler, so we have to ignore GC
8241 mark bits. */
8242
8243 static void
8244 x_scroll_bar_note_movement (bar, event)
8245 struct scroll_bar *bar;
8246 XEvent *event;
8247 {
8248 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8249
8250 last_mouse_movement_time = event->xmotion.time;
8251
8252 f->mouse_moved = 1;
8253 XSETVECTOR (last_mouse_scroll_bar, bar);
8254
8255 /* If we're dragging the bar, display it. */
8256 if (! GC_NILP (bar->dragging))
8257 {
8258 /* Where should the handle be now? */
8259 int new_start = event->xmotion.y - XINT (bar->dragging);
8260
8261 if (new_start != XINT (bar->start))
8262 {
8263 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8264
8265 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8266 }
8267 }
8268 }
8269
8270 /* Return information to the user about the current position of the mouse
8271 on the scroll bar. */
8272
8273 static void
8274 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8275 FRAME_PTR *fp;
8276 Lisp_Object *bar_window;
8277 enum scroll_bar_part *part;
8278 Lisp_Object *x, *y;
8279 unsigned long *time;
8280 {
8281 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8282 Window w = SCROLL_BAR_X_WINDOW (bar);
8283 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8284 int win_x, win_y;
8285 Window dummy_window;
8286 int dummy_coord;
8287 unsigned int dummy_mask;
8288
8289 BLOCK_INPUT;
8290
8291 /* Get the mouse's position relative to the scroll bar window, and
8292 report that. */
8293 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8294
8295 /* Root, child, root x and root y. */
8296 &dummy_window, &dummy_window,
8297 &dummy_coord, &dummy_coord,
8298
8299 /* Position relative to scroll bar. */
8300 &win_x, &win_y,
8301
8302 /* Mouse buttons and modifier keys. */
8303 &dummy_mask))
8304 ;
8305 else
8306 {
8307 #if 0
8308 int inside_height
8309 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8310 #endif
8311 int top_range
8312 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8313
8314 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8315
8316 if (! NILP (bar->dragging))
8317 win_y -= XINT (bar->dragging);
8318
8319 if (win_y < 0)
8320 win_y = 0;
8321 if (win_y > top_range)
8322 win_y = top_range;
8323
8324 *fp = f;
8325 *bar_window = bar->window;
8326
8327 if (! NILP (bar->dragging))
8328 *part = scroll_bar_handle;
8329 else if (win_y < XINT (bar->start))
8330 *part = scroll_bar_above_handle;
8331 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8332 *part = scroll_bar_handle;
8333 else
8334 *part = scroll_bar_below_handle;
8335
8336 XSETINT (*x, win_y);
8337 XSETINT (*y, top_range);
8338
8339 f->mouse_moved = 0;
8340 last_mouse_scroll_bar = Qnil;
8341 }
8342
8343 *time = last_mouse_movement_time;
8344
8345 UNBLOCK_INPUT;
8346 }
8347
8348
8349 /* The screen has been cleared so we may have changed foreground or
8350 background colors, and the scroll bars may need to be redrawn.
8351 Clear out the scroll bars, and ask for expose events, so we can
8352 redraw them. */
8353
8354 void
8355 x_scroll_bar_clear (f)
8356 FRAME_PTR f;
8357 {
8358 #ifndef USE_TOOLKIT_SCROLL_BARS
8359 Lisp_Object bar;
8360
8361 /* We can have scroll bars even if this is 0,
8362 if we just turned off scroll bar mode.
8363 But in that case we should not clear them. */
8364 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8365 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8366 bar = XSCROLL_BAR (bar)->next)
8367 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8368 0, 0, 0, 0, True);
8369 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8370 }
8371
8372 /* This processes Expose events from the menu-bar specific X event
8373 loop in xmenu.c. This allows to redisplay the frame if necessary
8374 when handling menu-bar or pop-up items. */
8375
8376 int
8377 process_expose_from_menu (event)
8378 XEvent event;
8379 {
8380 FRAME_PTR f;
8381 struct x_display_info *dpyinfo;
8382 int frame_exposed_p = 0;
8383
8384 BLOCK_INPUT;
8385
8386 dpyinfo = x_display_info_for_display (event.xexpose.display);
8387 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8388 if (f)
8389 {
8390 if (f->async_visible == 0)
8391 {
8392 f->async_visible = 1;
8393 f->async_iconified = 0;
8394 f->output_data.x->has_been_visible = 1;
8395 SET_FRAME_GARBAGED (f);
8396 }
8397 else
8398 {
8399 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8400 event.xexpose.x, event.xexpose.y,
8401 event.xexpose.width, event.xexpose.height);
8402 frame_exposed_p = 1;
8403 }
8404 }
8405 else
8406 {
8407 struct scroll_bar *bar
8408 = x_window_to_scroll_bar (event.xexpose.window);
8409
8410 if (bar)
8411 x_scroll_bar_expose (bar, &event);
8412 }
8413
8414 UNBLOCK_INPUT;
8415 return frame_exposed_p;
8416 }
8417 \f
8418 /* Define a queue to save up SelectionRequest events for later handling. */
8419
8420 struct selection_event_queue
8421 {
8422 XEvent event;
8423 struct selection_event_queue *next;
8424 };
8425
8426 static struct selection_event_queue *queue;
8427
8428 /* Nonzero means queue up certain events--don't process them yet. */
8429
8430 static int x_queue_selection_requests;
8431
8432 /* Queue up an X event *EVENT, to be processed later. */
8433
8434 static void
8435 x_queue_event (f, event)
8436 FRAME_PTR f;
8437 XEvent *event;
8438 {
8439 struct selection_event_queue *queue_tmp
8440 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8441
8442 if (queue_tmp != NULL)
8443 {
8444 queue_tmp->event = *event;
8445 queue_tmp->next = queue;
8446 queue = queue_tmp;
8447 }
8448 }
8449
8450 /* Take all the queued events and put them back
8451 so that they get processed afresh. */
8452
8453 static void
8454 x_unqueue_events (display)
8455 Display *display;
8456 {
8457 while (queue != NULL)
8458 {
8459 struct selection_event_queue *queue_tmp = queue;
8460 XPutBackEvent (display, &queue_tmp->event);
8461 queue = queue_tmp->next;
8462 xfree ((char *)queue_tmp);
8463 }
8464 }
8465
8466 /* Start queuing SelectionRequest events. */
8467
8468 void
8469 x_start_queuing_selection_requests (display)
8470 Display *display;
8471 {
8472 x_queue_selection_requests++;
8473 }
8474
8475 /* Stop queuing SelectionRequest events. */
8476
8477 void
8478 x_stop_queuing_selection_requests (display)
8479 Display *display;
8480 {
8481 x_queue_selection_requests--;
8482 x_unqueue_events (display);
8483 }
8484 \f
8485 /* The main X event-reading loop - XTread_socket. */
8486
8487 /* Time stamp of enter window event. This is only used by XTread_socket,
8488 but we have to put it out here, since static variables within functions
8489 sometimes don't work. */
8490
8491 static Time enter_timestamp;
8492
8493 /* This holds the state XLookupString needs to implement dead keys
8494 and other tricks known as "compose processing". _X Window System_
8495 says that a portable program can't use this, but Stephen Gildea assures
8496 me that letting the compiler initialize it to zeros will work okay.
8497
8498 This must be defined outside of XTread_socket, for the same reasons
8499 given for enter_time stamp, above. */
8500
8501 static XComposeStatus compose_status;
8502
8503 /* Record the last 100 characters stored
8504 to help debug the loss-of-chars-during-GC problem. */
8505
8506 static int temp_index;
8507 static short temp_buffer[100];
8508
8509 /* Set this to nonzero to fake an "X I/O error"
8510 on a particular display. */
8511
8512 struct x_display_info *XTread_socket_fake_io_error;
8513
8514 /* When we find no input here, we occasionally do a no-op command
8515 to verify that the X server is still running and we can still talk with it.
8516 We try all the open displays, one by one.
8517 This variable is used for cycling thru the displays. */
8518
8519 static struct x_display_info *next_noop_dpyinfo;
8520
8521 #define SET_SAVED_MENU_EVENT(size) \
8522 do \
8523 { \
8524 if (f->output_data.x->saved_menu_event == 0) \
8525 f->output_data.x->saved_menu_event \
8526 = (XEvent *) xmalloc (sizeof (XEvent)); \
8527 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8528 if (numchars >= 1) \
8529 { \
8530 bufp->kind = menu_bar_activate_event; \
8531 XSETFRAME (bufp->frame_or_window, f); \
8532 bufp++; \
8533 count++; \
8534 numchars--; \
8535 } \
8536 } \
8537 while (0)
8538
8539 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
8540 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8541
8542 /* Read events coming from the X server.
8543 This routine is called by the SIGIO handler.
8544 We return as soon as there are no more events to be read.
8545
8546 Events representing keys are stored in buffer BUFP,
8547 which can hold up to NUMCHARS characters.
8548 We return the number of characters stored into the buffer,
8549 thus pretending to be `read'.
8550
8551 EXPECTED is nonzero if the caller knows input is available. */
8552
8553 int
8554 XTread_socket (sd, bufp, numchars, expected)
8555 register int sd;
8556 /* register */ struct input_event *bufp;
8557 /* register */ int numchars;
8558 int expected;
8559 {
8560 int count = 0;
8561 int nbytes = 0;
8562 XEvent event;
8563 struct frame *f;
8564 int event_found = 0;
8565 struct x_display_info *dpyinfo;
8566 #ifdef HAVE_X_I18N
8567 Status status_return;
8568 #endif
8569
8570 if (interrupt_input_blocked)
8571 {
8572 interrupt_input_pending = 1;
8573 return -1;
8574 }
8575
8576 interrupt_input_pending = 0;
8577 BLOCK_INPUT;
8578
8579 /* So people can tell when we have read the available input. */
8580 input_signal_count++;
8581
8582 if (numchars <= 0)
8583 abort (); /* Don't think this happens. */
8584
8585 /* Find the display we are supposed to read input for.
8586 It's the one communicating on descriptor SD. */
8587 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8588 {
8589 #if 0 /* This ought to be unnecessary; let's verify it. */
8590 #ifdef FIOSNBIO
8591 /* If available, Xlib uses FIOSNBIO to make the socket
8592 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
8593 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
8594 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
8595 fcntl (dpyinfo->connection, F_SETFL, 0);
8596 #endif /* ! defined (FIOSNBIO) */
8597 #endif
8598
8599 #if 0 /* This code can't be made to work, with multiple displays,
8600 and appears not to be used on any system any more.
8601 Also keyboard.c doesn't turn O_NDELAY on and off
8602 for X connections. */
8603 #ifndef SIGIO
8604 #ifndef HAVE_SELECT
8605 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8606 {
8607 extern int read_alarm_should_throw;
8608 read_alarm_should_throw = 1;
8609 XPeekEvent (dpyinfo->display, &event);
8610 read_alarm_should_throw = 0;
8611 }
8612 #endif /* HAVE_SELECT */
8613 #endif /* SIGIO */
8614 #endif
8615
8616 /* For debugging, this gives a way to fake an I/O error. */
8617 if (dpyinfo == XTread_socket_fake_io_error)
8618 {
8619 XTread_socket_fake_io_error = 0;
8620 x_io_error_quitter (dpyinfo->display);
8621 }
8622
8623 while (XPending (dpyinfo->display))
8624 {
8625 XNextEvent (dpyinfo->display, &event);
8626
8627 if (display_busy_cursor_p)
8628 {
8629 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8630 display until the next X event is read and we come
8631 here again. Setting it to 1 inhibits busy-cursor
8632 display for direct commands. */
8633 if (event.type == MotionNotify
8634 || event.type == EnterNotify
8635 || (dpyinfo->grabbed
8636 && event.type != ButtonRelease))
8637 inhibit_busy_cursor = 2;
8638 else
8639 inhibit_busy_cursor = 1;
8640 }
8641
8642 #ifdef HAVE_X_I18N
8643 {
8644 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8645 event.xclient.window);
8646 /* The necessity of the following line took me
8647 a full work-day to decipher from the docs!! */
8648 if (f1 != 0 && FRAME_XIC (f1) && XFilterEvent (&event, None))
8649 break;
8650 }
8651 #endif
8652 event_found = 1;
8653
8654 switch (event.type)
8655 {
8656 case ClientMessage:
8657 {
8658 if (event.xclient.message_type
8659 == dpyinfo->Xatom_wm_protocols
8660 && event.xclient.format == 32)
8661 {
8662 if (event.xclient.data.l[0]
8663 == dpyinfo->Xatom_wm_take_focus)
8664 {
8665 /* Use x_any_window_to_frame because this
8666 could be the shell widget window
8667 if the frame has no title bar. */
8668 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
8669 #ifdef HAVE_X_I18N
8670 /* Not quite sure this is needed -pd */
8671 if (f && FRAME_XIC (f))
8672 XSetICFocus (FRAME_XIC (f));
8673 #endif
8674 /* Since we set WM_TAKE_FOCUS, we must call
8675 XSetInputFocus explicitly. But not if f is null,
8676 since that might be an event for a deleted frame. */
8677 if (f)
8678 {
8679 Display *d = event.xclient.display;
8680 /* Catch and ignore errors, in case window has been
8681 iconified by a window manager such as GWM. */
8682 int count = x_catch_errors (d);
8683 XSetInputFocus (d, event.xclient.window,
8684 RevertToPointerRoot,
8685 event.xclient.data.l[1]);
8686 /* This is needed to detect the error
8687 if there is an error. */
8688 XSync (d, False);
8689 x_uncatch_errors (d, count);
8690 }
8691 /* Not certain about handling scroll bars here */
8692 }
8693 else if (event.xclient.data.l[0]
8694 == dpyinfo->Xatom_wm_save_yourself)
8695 {
8696 /* Save state modify the WM_COMMAND property to
8697 something which can reinstate us. This notifies
8698 the session manager, who's looking for such a
8699 PropertyNotify. Can restart processing when
8700 a keyboard or mouse event arrives. */
8701 if (numchars > 0)
8702 {
8703 f = x_top_window_to_frame (dpyinfo,
8704 event.xclient.window);
8705
8706 /* This is just so we only give real data once
8707 for a single Emacs process. */
8708 if (f == selected_frame)
8709 XSetCommand (FRAME_X_DISPLAY (f),
8710 event.xclient.window,
8711 initial_argv, initial_argc);
8712 else if (f)
8713 XSetCommand (FRAME_X_DISPLAY (f),
8714 event.xclient.window,
8715 0, 0);
8716 }
8717 }
8718 else if (event.xclient.data.l[0]
8719 == dpyinfo->Xatom_wm_delete_window)
8720 {
8721 struct frame *f
8722 = x_any_window_to_frame (dpyinfo,
8723 event.xclient.window);
8724
8725 if (f)
8726 {
8727 if (numchars == 0)
8728 abort ();
8729
8730 bufp->kind = delete_window_event;
8731 XSETFRAME (bufp->frame_or_window, f);
8732 bufp++;
8733
8734 count += 1;
8735 numchars -= 1;
8736 }
8737 }
8738 }
8739 else if (event.xclient.message_type
8740 == dpyinfo->Xatom_wm_configure_denied)
8741 {
8742 }
8743 else if (event.xclient.message_type
8744 == dpyinfo->Xatom_wm_window_moved)
8745 {
8746 int new_x, new_y;
8747 struct frame *f
8748 = x_window_to_frame (dpyinfo, event.xclient.window);
8749
8750 new_x = event.xclient.data.s[0];
8751 new_y = event.xclient.data.s[1];
8752
8753 if (f)
8754 {
8755 f->output_data.x->left_pos = new_x;
8756 f->output_data.x->top_pos = new_y;
8757 }
8758 }
8759 #ifdef HACK_EDITRES
8760 else if (event.xclient.message_type
8761 == dpyinfo->Xatom_editres)
8762 {
8763 struct frame *f
8764 = x_any_window_to_frame (dpyinfo, event.xclient.window);
8765 _XEditResCheckMessages (f->output_data.x->widget, NULL,
8766 &event, NULL);
8767 }
8768 #endif /* HACK_EDITRES */
8769 else if ((event.xclient.message_type
8770 == dpyinfo->Xatom_DONE)
8771 || (event.xclient.message_type
8772 == dpyinfo->Xatom_PAGE))
8773 {
8774 /* Ghostview job completed. Kill it. We could
8775 reply with "Next" if we received "Page", but we
8776 currently never do because we are interested in
8777 images, only, which should have 1 page. */
8778 Window gs_window = (Window) event.xclient.data.l[0];
8779 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
8780 struct frame *f
8781 = x_window_to_frame (dpyinfo, event.xclient.window);
8782 x_kill_gs_process (pixmap, f);
8783 expose_frame (f, 0, 0, 0, 0);
8784 }
8785 #ifdef USE_TOOLKIT_SCROLL_BARS
8786 /* Scroll bar callbacks send a ClientMessage from which
8787 we construct an input_event. */
8788 else if (event.xclient.message_type
8789 == dpyinfo->Xatom_Scrollbar)
8790 {
8791 x_scroll_bar_to_input_event (&event, bufp);
8792 ++bufp, ++count, --numchars;
8793 goto out;
8794 }
8795 #endif /* USE_TOOLKIT_SCROLL_BARS */
8796 else
8797 goto OTHER;
8798 }
8799 break;
8800
8801 case SelectionNotify:
8802 #ifdef USE_X_TOOLKIT
8803 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
8804 goto OTHER;
8805 #endif /* not USE_X_TOOLKIT */
8806 x_handle_selection_notify (&event.xselection);
8807 break;
8808
8809 case SelectionClear: /* Someone has grabbed ownership. */
8810 #ifdef USE_X_TOOLKIT
8811 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
8812 goto OTHER;
8813 #endif /* USE_X_TOOLKIT */
8814 {
8815 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
8816
8817 if (numchars == 0)
8818 abort ();
8819
8820 bufp->kind = selection_clear_event;
8821 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8822 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8823 SELECTION_EVENT_TIME (bufp) = eventp->time;
8824 bufp->frame_or_window = Qnil;
8825 bufp++;
8826
8827 count += 1;
8828 numchars -= 1;
8829 }
8830 break;
8831
8832 case SelectionRequest: /* Someone wants our selection. */
8833 #ifdef USE_X_TOOLKIT
8834 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
8835 goto OTHER;
8836 #endif /* USE_X_TOOLKIT */
8837 if (x_queue_selection_requests)
8838 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
8839 &event);
8840 else
8841 {
8842 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
8843
8844 if (numchars == 0)
8845 abort ();
8846
8847 bufp->kind = selection_request_event;
8848 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8849 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
8850 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8851 SELECTION_EVENT_TARGET (bufp) = eventp->target;
8852 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
8853 SELECTION_EVENT_TIME (bufp) = eventp->time;
8854 bufp->frame_or_window = Qnil;
8855 bufp++;
8856
8857 count += 1;
8858 numchars -= 1;
8859 }
8860 break;
8861
8862 case PropertyNotify:
8863 #ifdef USE_X_TOOLKIT
8864 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
8865 goto OTHER;
8866 #endif /* not USE_X_TOOLKIT */
8867 x_handle_property_notify (&event.xproperty);
8868 break;
8869
8870 case ReparentNotify:
8871 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
8872 if (f)
8873 {
8874 int x, y;
8875 f->output_data.x->parent_desc = event.xreparent.parent;
8876 x_real_positions (f, &x, &y);
8877 f->output_data.x->left_pos = x;
8878 f->output_data.x->top_pos = y;
8879 }
8880 break;
8881
8882 case Expose:
8883 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8884 if (f)
8885 {
8886 if (f->async_visible == 0)
8887 {
8888 f->async_visible = 1;
8889 f->async_iconified = 0;
8890 f->output_data.x->has_been_visible = 1;
8891 SET_FRAME_GARBAGED (f);
8892 }
8893 else
8894 expose_frame (x_window_to_frame (dpyinfo,
8895 event.xexpose.window),
8896 event.xexpose.x, event.xexpose.y,
8897 event.xexpose.width, event.xexpose.height);
8898 }
8899 else
8900 {
8901 #ifdef USE_TOOLKIT_SCROLL_BARS
8902 /* Dispatch event to the widget. */
8903 goto OTHER;
8904 #else /* not USE_TOOLKIT_SCROLL_BARS */
8905 struct scroll_bar *bar
8906 = x_window_to_scroll_bar (event.xexpose.window);
8907
8908 if (bar)
8909 x_scroll_bar_expose (bar, &event);
8910 #ifdef USE_X_TOOLKIT
8911 else
8912 goto OTHER;
8913 #endif /* USE_X_TOOLKIT */
8914 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8915 }
8916 break;
8917
8918 case GraphicsExpose: /* This occurs when an XCopyArea's
8919 source area was obscured or not
8920 available.*/
8921 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
8922 if (f)
8923 {
8924 expose_frame (f,
8925 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
8926 event.xgraphicsexpose.width,
8927 event.xgraphicsexpose.height);
8928 }
8929 #ifdef USE_X_TOOLKIT
8930 else
8931 goto OTHER;
8932 #endif /* USE_X_TOOLKIT */
8933 break;
8934
8935 case NoExpose: /* This occurs when an XCopyArea's
8936 source area was completely
8937 available */
8938 break;
8939
8940 case UnmapNotify:
8941 /* Redo the mouse-highlight after the tooltip has gone. */
8942 if (event.xmap.window == tip_window)
8943 {
8944 tip_window = 0;
8945 redo_mouse_highlight ();
8946 }
8947
8948 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
8949 if (f) /* F may no longer exist if
8950 the frame was deleted. */
8951 {
8952 /* While a frame is unmapped, display generation is
8953 disabled; you don't want to spend time updating a
8954 display that won't ever be seen. */
8955 f->async_visible = 0;
8956 /* We can't distinguish, from the event, whether the window
8957 has become iconified or invisible. So assume, if it
8958 was previously visible, than now it is iconified.
8959 But x_make_frame_invisible clears both
8960 the visible flag and the iconified flag;
8961 and that way, we know the window is not iconified now. */
8962 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
8963 {
8964 f->async_iconified = 1;
8965
8966 bufp->kind = iconify_event;
8967 XSETFRAME (bufp->frame_or_window, f);
8968 bufp++;
8969 count++;
8970 numchars--;
8971 }
8972 }
8973 goto OTHER;
8974
8975 case MapNotify:
8976 if (event.xmap.window == tip_window)
8977 /* The tooltip has been drawn already. Avoid
8978 the SET_FRAME_GARBAGED below. */
8979 goto OTHER;
8980
8981 /* We use x_top_window_to_frame because map events can
8982 come for sub-windows and they don't mean that the
8983 frame is visible. */
8984 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
8985 if (f)
8986 {
8987 f->async_visible = 1;
8988 f->async_iconified = 0;
8989 f->output_data.x->has_been_visible = 1;
8990
8991 /* wait_reading_process_input will notice this and update
8992 the frame's display structures. */
8993 SET_FRAME_GARBAGED (f);
8994
8995 if (f->iconified)
8996 {
8997 bufp->kind = deiconify_event;
8998 XSETFRAME (bufp->frame_or_window, f);
8999 bufp++;
9000 count++;
9001 numchars--;
9002 }
9003 else if (! NILP (Vframe_list)
9004 && ! NILP (XCONS (Vframe_list)->cdr))
9005 /* Force a redisplay sooner or later
9006 to update the frame titles
9007 in case this is the second frame. */
9008 record_asynch_buffer_change ();
9009 }
9010 goto OTHER;
9011
9012 case KeyPress:
9013 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9014
9015 #ifdef USE_MOTIF
9016 /* I couldn't find a way to prevent LessTif scroll bars
9017 from consuming key events. */
9018 if (f == 0)
9019 {
9020 Widget widget = XtWindowToWidget (dpyinfo->display,
9021 event.xkey.window);
9022 if (widget && XmIsScrollBar (widget))
9023 {
9024 widget = XtParent (widget);
9025 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9026 }
9027 }
9028 #endif /* USE_MOTIF */
9029
9030 if (f != 0)
9031 {
9032 KeySym keysym, orig_keysym;
9033 /* al%imercury@uunet.uu.net says that making this 81 instead of
9034 80 fixed a bug whereby meta chars made his Emacs hang. */
9035 unsigned char copy_buffer[81];
9036 int modifiers;
9037
9038 event.xkey.state
9039 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9040 extra_keyboard_modifiers);
9041 modifiers = event.xkey.state;
9042
9043 /* This will have to go some day... */
9044
9045 /* make_lispy_event turns chars into control chars.
9046 Don't do it here because XLookupString is too eager. */
9047 event.xkey.state &= ~ControlMask;
9048 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9049 | dpyinfo->super_mod_mask
9050 | dpyinfo->hyper_mod_mask
9051 | dpyinfo->alt_mod_mask);
9052
9053 /* In case Meta is ComposeCharacter,
9054 clear its status. According to Markus Ehrnsperger
9055 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9056 this enables ComposeCharacter to work whether or
9057 not it is combined with Meta. */
9058 if (modifiers & dpyinfo->meta_mod_mask)
9059 bzero (&compose_status, sizeof (compose_status));
9060
9061 #ifdef HAVE_X_I18N
9062 if (FRAME_XIC (f))
9063 {
9064 /* The necessity of the following line took me
9065 a full work-day to decipher from the docs!! */
9066 if (XFilterEvent (&event, None))
9067 break;
9068 nbytes = XmbLookupString (FRAME_XIC (f),
9069 &event.xkey, copy_buffer,
9070 80, &keysym,
9071 &status_return);
9072 if (status_return == XLookupNone)
9073 break;
9074 else if (status_return == XLookupChars)
9075 keysym = NoSymbol;
9076 else if (status_return != XLookupKeySym
9077 && status_return != XLookupBoth)
9078 abort ();
9079 }
9080 else
9081 nbytes = XLookupString (&event.xkey, copy_buffer,
9082 80, &keysym, &compose_status);
9083 #else
9084 nbytes = XLookupString (&event.xkey, copy_buffer,
9085 80, &keysym, &compose_status);
9086 #endif
9087
9088 orig_keysym = keysym;
9089
9090 if (numchars > 1)
9091 {
9092 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9093 || keysym == XK_Delete
9094 #ifdef XK_ISO_Left_Tab
9095 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9096 #endif
9097 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9098 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9099 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9100 #ifdef HPUX
9101 /* This recognizes the "extended function keys".
9102 It seems there's no cleaner way.
9103 Test IsModifierKey to avoid handling mode_switch
9104 incorrectly. */
9105 || ((unsigned) (keysym) >= XK_Select
9106 && (unsigned)(keysym) < XK_KP_Space)
9107 #endif
9108 #ifdef XK_dead_circumflex
9109 || orig_keysym == XK_dead_circumflex
9110 #endif
9111 #ifdef XK_dead_grave
9112 || orig_keysym == XK_dead_grave
9113 #endif
9114 #ifdef XK_dead_tilde
9115 || orig_keysym == XK_dead_tilde
9116 #endif
9117 #ifdef XK_dead_diaeresis
9118 || orig_keysym == XK_dead_diaeresis
9119 #endif
9120 #ifdef XK_dead_macron
9121 || orig_keysym == XK_dead_macron
9122 #endif
9123 #ifdef XK_dead_degree
9124 || orig_keysym == XK_dead_degree
9125 #endif
9126 #ifdef XK_dead_acute
9127 || orig_keysym == XK_dead_acute
9128 #endif
9129 #ifdef XK_dead_cedilla
9130 || orig_keysym == XK_dead_cedilla
9131 #endif
9132 #ifdef XK_dead_breve
9133 || orig_keysym == XK_dead_breve
9134 #endif
9135 #ifdef XK_dead_ogonek
9136 || orig_keysym == XK_dead_ogonek
9137 #endif
9138 #ifdef XK_dead_caron
9139 || orig_keysym == XK_dead_caron
9140 #endif
9141 #ifdef XK_dead_doubleacute
9142 || orig_keysym == XK_dead_doubleacute
9143 #endif
9144 #ifdef XK_dead_abovedot
9145 || orig_keysym == XK_dead_abovedot
9146 #endif
9147 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9148 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9149 /* Any "vendor-specific" key is ok. */
9150 || (orig_keysym & (1 << 28)))
9151 && ! (IsModifierKey (orig_keysym)
9152 #ifndef HAVE_X11R5
9153 #ifdef XK_Mode_switch
9154 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9155 #endif
9156 #ifdef XK_Num_Lock
9157 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9158 #endif
9159 #endif /* not HAVE_X11R5 */
9160 ))
9161 {
9162 if (temp_index == sizeof temp_buffer / sizeof (short))
9163 temp_index = 0;
9164 temp_buffer[temp_index++] = keysym;
9165 bufp->kind = non_ascii_keystroke;
9166 bufp->code = keysym;
9167 XSETFRAME (bufp->frame_or_window, f);
9168 bufp->modifiers
9169 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9170 modifiers);
9171 bufp->timestamp = event.xkey.time;
9172 bufp++;
9173 count++;
9174 numchars--;
9175
9176 if (display_busy_cursor_p)
9177 if (keysym != XK_Return || minibuf_level == 0)
9178 inhibit_busy_cursor = 2;
9179 }
9180 else if (numchars > nbytes)
9181 {
9182 register int i;
9183
9184 for (i = 0; i < nbytes; i++)
9185 {
9186 if (temp_index == sizeof temp_buffer / sizeof (short))
9187 temp_index = 0;
9188 temp_buffer[temp_index++] = copy_buffer[i];
9189 bufp->kind = ascii_keystroke;
9190 bufp->code = copy_buffer[i];
9191 XSETFRAME (bufp->frame_or_window, f);
9192 bufp->modifiers
9193 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9194 modifiers);
9195 bufp->timestamp = event.xkey.time;
9196 bufp++;
9197 }
9198
9199 count += nbytes;
9200 numchars -= nbytes;
9201
9202 if (keysym == NoSymbol)
9203 break;
9204 }
9205 else
9206 abort ();
9207 }
9208 else
9209 abort ();
9210 }
9211 goto OTHER;
9212
9213 /* Here's a possible interpretation of the whole
9214 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9215 you get a FocusIn event, you have to get a FocusOut
9216 event before you relinquish the focus. If you
9217 haven't received a FocusIn event, then a mere
9218 LeaveNotify is enough to free you. */
9219
9220 case EnterNotify:
9221 {
9222 int from_menu_bar_p = 0;
9223
9224 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9225
9226 #ifdef LESSTIF_VERSION
9227 /* When clicking outside of a menu bar popup to close
9228 it, we get a FocusIn/ EnterNotify sequence of
9229 events. The flag event.xcrossing.focus is not set
9230 in the EnterNotify event of that sequence because
9231 the focus is in the menu bar,
9232 event.xcrossing.window is the frame's X window.
9233 Unconditionally setting the focus frame to null in
9234 this case is not the right thing, because no event
9235 follows that could set the focus frame to the right
9236 value.
9237
9238 This could be a LessTif bug, but I wasn't able to
9239 reproduce the behavior in a simple test program.
9240
9241 (gerd, LessTif 0.88.1). */
9242
9243 if (!event.xcrossing.focus
9244 && f
9245 && f->output_data.x->menubar_widget)
9246 {
9247 Window focus;
9248 int revert;
9249
9250 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9251 if (focus == XtWindow (f->output_data.x->menubar_widget))
9252 from_menu_bar_p = 1;
9253 }
9254 #endif /* LESSTIF_VERSION */
9255
9256 if (event.xcrossing.focus || from_menu_bar_p)
9257 {
9258 /* Avoid nasty pop/raise loops. */
9259 if (f && (!(f->auto_raise)
9260 || !(f->auto_lower)
9261 || (event.xcrossing.time - enter_timestamp) > 500))
9262 {
9263 x_new_focus_frame (dpyinfo, f);
9264 enter_timestamp = event.xcrossing.time;
9265 }
9266 }
9267 else if (f == dpyinfo->x_focus_frame)
9268 x_new_focus_frame (dpyinfo, 0);
9269
9270 /* EnterNotify counts as mouse movement,
9271 so update things that depend on mouse position. */
9272 if (f && !f->output_data.x->busy_p)
9273 note_mouse_movement (f, &event.xmotion);
9274 goto OTHER;
9275 }
9276
9277 case FocusIn:
9278 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9279 if (event.xfocus.detail != NotifyPointer)
9280 dpyinfo->x_focus_event_frame = f;
9281 if (f)
9282 x_new_focus_frame (dpyinfo, f);
9283
9284 #ifdef HAVE_X_I18N
9285 if (f && FRAME_XIC (f))
9286 XSetICFocus (FRAME_XIC (f));
9287 #endif
9288
9289 goto OTHER;
9290
9291 case LeaveNotify:
9292 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9293 if (f)
9294 {
9295 Lisp_Object frame;
9296 int from_menu_bar_p = 0;
9297
9298 if (f == dpyinfo->mouse_face_mouse_frame)
9299 {
9300 /* If we move outside the frame, then we're
9301 certainly no longer on any text in the frame. */
9302 clear_mouse_face (dpyinfo);
9303 dpyinfo->mouse_face_mouse_frame = 0;
9304 }
9305
9306 /* Generate a nil HELP_EVENT to cancel a help-echo.
9307 Do it only if there's something to cancel.
9308 Otherwise, the startup message is cleared when
9309 the mouse leaves the frame. */
9310 if (any_help_event_p)
9311 {
9312 XSETFRAME (frame, f);
9313 bufp->kind = HELP_EVENT;
9314 bufp->frame_or_window = Fcons (frame, Qnil);
9315 ++bufp, ++count, --numchars;
9316 }
9317
9318 #ifdef LESSTIF_VERSION
9319 /* Please see the comment at the start of the
9320 EnterNotify case. */
9321 if (!event.xcrossing.focus
9322 && f->output_data.x->menubar_widget)
9323 {
9324 Window focus;
9325 int revert;
9326 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9327 if (focus == XtWindow (f->output_data.x->menubar_widget))
9328 from_menu_bar_p = 1;
9329 }
9330 #endif /* LESSTIF_VERSION */
9331
9332 if (event.xcrossing.focus || from_menu_bar_p)
9333 x_mouse_leave (dpyinfo);
9334 else
9335 {
9336 if (f == dpyinfo->x_focus_event_frame)
9337 dpyinfo->x_focus_event_frame = 0;
9338 if (f == dpyinfo->x_focus_frame)
9339 x_new_focus_frame (dpyinfo, 0);
9340 }
9341 }
9342 goto OTHER;
9343
9344 case FocusOut:
9345 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9346 if (event.xfocus.detail != NotifyPointer
9347 && f == dpyinfo->x_focus_event_frame)
9348 dpyinfo->x_focus_event_frame = 0;
9349 if (f && f == dpyinfo->x_focus_frame)
9350 x_new_focus_frame (dpyinfo, 0);
9351
9352 #ifdef HAVE_X_I18N
9353 if (f && FRAME_XIC (f))
9354 XUnsetICFocus (FRAME_XIC (f));
9355 #endif
9356
9357 goto OTHER;
9358
9359 case MotionNotify:
9360 {
9361 previous_help_echo = help_echo;
9362 help_echo = Qnil;
9363
9364 if (dpyinfo->grabbed && last_mouse_frame
9365 && FRAME_LIVE_P (last_mouse_frame))
9366 f = last_mouse_frame;
9367 else
9368 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9369
9370 if (f)
9371 note_mouse_movement (f, &event.xmotion);
9372 else
9373 {
9374 #ifndef USE_X_TOOLKIT
9375 struct scroll_bar *bar
9376 = x_window_to_scroll_bar (event.xmotion.window);
9377
9378 if (bar)
9379 x_scroll_bar_note_movement (bar, &event);
9380 #endif /* USE_X_TOOLKIT */
9381
9382 /* If we move outside the frame, then we're
9383 certainly no longer on any text in the frame. */
9384 clear_mouse_face (dpyinfo);
9385 }
9386
9387 /* If the contents of the global variable help_echo
9388 has changed, generate a HELP_EVENT. */
9389 if (STRINGP (help_echo)
9390 || STRINGP (previous_help_echo))
9391 {
9392 Lisp_Object frame;
9393
9394 if (f)
9395 XSETFRAME (frame, f);
9396 else
9397 frame = Qnil;
9398
9399 any_help_event_p = 1;
9400 bufp->kind = HELP_EVENT;
9401 bufp->frame_or_window = Fcons (frame, help_echo);
9402 ++bufp, ++count, --numchars;
9403 }
9404
9405 goto OTHER;
9406 }
9407
9408 case ConfigureNotify:
9409 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9410 if (f)
9411 {
9412 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9413 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9414
9415 #ifndef USE_X_TOOLKIT
9416 /* In the toolkit version, change_frame_size
9417 is called by the code that handles resizing
9418 of the EmacsFrame widget. */
9419
9420 /* Even if the number of character rows and columns has
9421 not changed, the font size may have changed, so we need
9422 to check the pixel dimensions as well. */
9423 if (columns != f->width
9424 || rows != f->height
9425 || event.xconfigure.width != f->output_data.x->pixel_width
9426 || event.xconfigure.height != f->output_data.x->pixel_height)
9427 {
9428 change_frame_size (f, rows, columns, 0, 1, 0);
9429 SET_FRAME_GARBAGED (f);
9430 cancel_mouse_face (f);
9431 }
9432 #endif
9433
9434 f->output_data.x->pixel_width = event.xconfigure.width;
9435 f->output_data.x->pixel_height = event.xconfigure.height;
9436
9437 /* What we have now is the position of Emacs's own window.
9438 Convert that to the position of the window manager window. */
9439 x_real_positions (f, &f->output_data.x->left_pos,
9440 &f->output_data.x->top_pos);
9441
9442 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9443 {
9444 /* Since the WM decorations come below top_pos now,
9445 we must put them below top_pos in the future. */
9446 f->output_data.x->win_gravity = NorthWestGravity;
9447 x_wm_set_size_hint (f, (long) 0, 0);
9448 }
9449 #ifdef USE_MOTIF
9450 /* Some window managers pass (0,0) as the location of
9451 the window, and the Motif event handler stores it
9452 in the emacs widget, which messes up Motif menus. */
9453 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9454 {
9455 event.xconfigure.x = f->output_data.x->widget->core.x;
9456 event.xconfigure.y = f->output_data.x->widget->core.y;
9457 }
9458 #endif /* USE_MOTIF */
9459 }
9460 goto OTHER;
9461
9462 case ButtonPress:
9463 case ButtonRelease:
9464 {
9465 /* If we decide we want to generate an event to be seen
9466 by the rest of Emacs, we put it here. */
9467 struct input_event emacs_event;
9468 int toolbar_p = 0;
9469
9470 emacs_event.kind = no_event;
9471 bzero (&compose_status, sizeof (compose_status));
9472
9473 if (dpyinfo->grabbed
9474 && last_mouse_frame
9475 && FRAME_LIVE_P (last_mouse_frame))
9476 f = last_mouse_frame;
9477 else
9478 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9479
9480 if (f)
9481 {
9482 /* Is this in the toolbar? */
9483 if (WINDOWP (f->toolbar_window)
9484 && XFASTINT (XWINDOW (f->toolbar_window)->height))
9485 {
9486 Lisp_Object window;
9487 int p, x, y;
9488
9489 x = event.xbutton.x;
9490 y = event.xbutton.y;
9491
9492 /* Set x and y. */
9493 window = window_from_coordinates (f, x, y, &p, 1);
9494 if (EQ (window, f->toolbar_window))
9495 {
9496 x_handle_toolbar_click (f, &event.xbutton);
9497 toolbar_p = 1;
9498 }
9499 }
9500
9501 if (!toolbar_p)
9502 if (!dpyinfo->x_focus_frame
9503 || f == dpyinfo->x_focus_frame)
9504 construct_mouse_click (&emacs_event, &event, f);
9505 }
9506 else
9507 {
9508 #ifndef USE_TOOLKIT_SCROLL_BARS
9509 struct scroll_bar *bar
9510 = x_window_to_scroll_bar (event.xbutton.window);
9511
9512 if (bar)
9513 x_scroll_bar_handle_click (bar, &event, &emacs_event);
9514 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9515 }
9516
9517 if (event.type == ButtonPress)
9518 {
9519 dpyinfo->grabbed |= (1 << event.xbutton.button);
9520 last_mouse_frame = f;
9521 /* Ignore any mouse motion that happened
9522 before this event; any subsequent mouse-movement
9523 Emacs events should reflect only motion after
9524 the ButtonPress. */
9525 if (f != 0)
9526 f->mouse_moved = 0;
9527
9528 if (!toolbar_p)
9529 last_toolbar_item = -1;
9530 if (display_busy_cursor_p)
9531 inhibit_busy_cursor = 2;
9532 }
9533 else
9534 {
9535 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
9536 }
9537
9538 if (numchars >= 1 && emacs_event.kind != no_event)
9539 {
9540 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9541 bufp++;
9542 count++;
9543 numchars--;
9544 }
9545
9546 #ifdef USE_X_TOOLKIT
9547 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9548 /* For a down-event in the menu bar,
9549 don't pass it to Xt right now.
9550 Instead, save it away
9551 and we will pass it to Xt from kbd_buffer_get_event.
9552 That way, we can run some Lisp code first. */
9553 if (f && event.type == ButtonPress
9554 /* Verify the event is really within the menu bar
9555 and not just sent to it due to grabbing. */
9556 && event.xbutton.x >= 0
9557 && event.xbutton.x < f->output_data.x->pixel_width
9558 && event.xbutton.y >= 0
9559 && event.xbutton.y < f->output_data.x->menubar_height
9560 && event.xbutton.same_screen)
9561 {
9562 SET_SAVED_BUTTON_EVENT;
9563 XSETFRAME (last_mouse_press_frame, f);
9564 }
9565 else if (event.type == ButtonPress)
9566 {
9567 last_mouse_press_frame = Qnil;
9568 goto OTHER;
9569 }
9570
9571 #ifdef USE_MOTIF /* This should do not harm for Lucid,
9572 but I am trying to be cautious. */
9573 else if (event.type == ButtonRelease)
9574 {
9575 if (!NILP (last_mouse_press_frame))
9576 {
9577 f = XFRAME (last_mouse_press_frame);
9578 if (f->output_data.x)
9579 SET_SAVED_BUTTON_EVENT;
9580 }
9581 else
9582 goto OTHER;
9583 }
9584 #endif /* USE_MOTIF */
9585 else
9586 goto OTHER;
9587 #endif /* USE_X_TOOLKIT */
9588 }
9589 break;
9590
9591 case CirculateNotify:
9592 goto OTHER;
9593
9594 case CirculateRequest:
9595 goto OTHER;
9596
9597 case VisibilityNotify:
9598 goto OTHER;
9599
9600 case MappingNotify:
9601 /* Someone has changed the keyboard mapping - update the
9602 local cache. */
9603 switch (event.xmapping.request)
9604 {
9605 case MappingModifier:
9606 x_find_modifier_meanings (dpyinfo);
9607 /* This is meant to fall through. */
9608 case MappingKeyboard:
9609 XRefreshKeyboardMapping (&event.xmapping);
9610 }
9611 goto OTHER;
9612
9613 default:
9614 OTHER:
9615 #ifdef USE_X_TOOLKIT
9616 BLOCK_INPUT;
9617 XtDispatchEvent (&event);
9618 UNBLOCK_INPUT;
9619 #endif /* USE_X_TOOLKIT */
9620 break;
9621 }
9622 }
9623 }
9624
9625 out:;
9626
9627 /* On some systems, an X bug causes Emacs to get no more events
9628 when the window is destroyed. Detect that. (1994.) */
9629 if (! event_found)
9630 {
9631 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9632 One XNOOP in 100 loops will make Emacs terminate.
9633 B. Bretthauer, 1994 */
9634 x_noop_count++;
9635 if (x_noop_count >= 100)
9636 {
9637 x_noop_count=0;
9638
9639 if (next_noop_dpyinfo == 0)
9640 next_noop_dpyinfo = x_display_list;
9641
9642 XNoOp (next_noop_dpyinfo->display);
9643
9644 /* Each time we get here, cycle through the displays now open. */
9645 next_noop_dpyinfo = next_noop_dpyinfo->next;
9646 }
9647 }
9648
9649 /* If the focus was just given to an auto-raising frame,
9650 raise it now. */
9651 /* ??? This ought to be able to handle more than one such frame. */
9652 if (pending_autoraise_frame)
9653 {
9654 x_raise_frame (pending_autoraise_frame);
9655 pending_autoraise_frame = 0;
9656 }
9657
9658 UNBLOCK_INPUT;
9659 return count;
9660 }
9661
9662
9663
9664 \f
9665 /***********************************************************************
9666 Text Cursor
9667 ***********************************************************************/
9668
9669 /* Note if the text cursor of window W has been overwritten by a
9670 drawing operation that outputs N glyphs starting at HPOS in the
9671 line given by output_cursor.vpos. N < 0 means all the rest of the
9672 line after HPOS has been written. */
9673
9674 static void
9675 note_overwritten_text_cursor (w, hpos, n)
9676 struct window *w;
9677 int hpos, n;
9678 {
9679 if (updated_area == TEXT_AREA
9680 && output_cursor.vpos == w->phys_cursor.vpos
9681 && hpos <= w->phys_cursor.hpos
9682 && (n < 0
9683 || hpos + n > w->phys_cursor.hpos))
9684 w->phys_cursor_on_p = 0;
9685 }
9686
9687
9688 /* Set clipping for output in glyph row ROW. W is the window in which
9689 we operate. GC is the graphics context to set clipping in.
9690 WHOLE_LINE_P non-zero means include the areas used for truncation
9691 mark display and alike in the clipping rectangle.
9692
9693 ROW may be a text row or, e.g., a mode line. Text rows must be
9694 clipped to the interior of the window dedicated to text display,
9695 mode lines must be clipped to the whole window. */
9696
9697 static void
9698 x_clip_to_row (w, row, gc, whole_line_p)
9699 struct window *w;
9700 struct glyph_row *row;
9701 GC gc;
9702 int whole_line_p;
9703 {
9704 struct frame *f = XFRAME (WINDOW_FRAME (w));
9705 XRectangle clip_rect;
9706 int window_x, window_y, window_width, window_height;
9707
9708 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9709
9710 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9711 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9712 clip_rect.y = max (clip_rect.y, window_y);
9713 clip_rect.width = window_width;
9714 clip_rect.height = row->visible_height;
9715
9716 /* If clipping to the whole line, including trunc marks, extend
9717 the rectangle to the left and increase its width. */
9718 if (whole_line_p)
9719 {
9720 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
9721 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
9722 }
9723
9724 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
9725 }
9726
9727
9728 /* Draw a hollow box cursor on window W in glyph row ROW. */
9729
9730 static void
9731 x_draw_hollow_cursor (w, row)
9732 struct window *w;
9733 struct glyph_row *row;
9734 {
9735 struct frame *f = XFRAME (WINDOW_FRAME (w));
9736 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9737 Display *dpy = FRAME_X_DISPLAY (f);
9738 int x, y, wd, h;
9739 XGCValues xgcv;
9740 struct glyph *cursor_glyph;
9741 GC gc;
9742
9743 /* Compute frame-relative coordinates from window-relative
9744 coordinates. */
9745 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9746 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9747 + row->ascent - w->phys_cursor_ascent);
9748 h = row->height - 1;
9749
9750 /* Get the glyph the cursor is on. If we can't tell because
9751 the current matrix is invalid or such, give up. */
9752 cursor_glyph = get_phys_cursor_glyph (w);
9753 if (cursor_glyph == NULL)
9754 return;
9755
9756 /* Compute the width of the rectangle to draw. If on a stretch
9757 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9758 rectangle as wide as the glyph, but use a canonical character
9759 width instead. */
9760 wd = cursor_glyph->pixel_width - 1;
9761 if (cursor_glyph->type == STRETCH_GLYPH
9762 && !x_stretch_cursor_p)
9763 wd = min (CANON_X_UNIT (f), wd);
9764
9765 /* The foreground of cursor_gc is typically the same as the normal
9766 background color, which can cause the cursor box to be invisible. */
9767 xgcv.foreground = f->output_data.x->cursor_pixel;
9768 if (dpyinfo->scratch_cursor_gc)
9769 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9770 else
9771 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
9772 GCForeground, &xgcv);
9773 gc = dpyinfo->scratch_cursor_gc;
9774
9775 /* Set clipping, draw the rectangle, and reset clipping again. */
9776 x_clip_to_row (w, row, gc, 0);
9777 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
9778 XSetClipMask (dpy, gc, None);
9779 }
9780
9781
9782 /* Draw a bar cursor on window W in glyph row ROW.
9783
9784 Implementation note: One would like to draw a bar cursor with an
9785 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9786 Unfortunately, I didn't find a font yet that has this property set.
9787 --gerd. */
9788
9789 static void
9790 x_draw_bar_cursor (w, row)
9791 struct window *w;
9792 struct glyph_row *row;
9793 {
9794 /* If cursor hpos is out of bounds, don't draw garbage. This can
9795 happen in mini-buffer windows when switching between echo area
9796 glyphs and mini-buffer. */
9797 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9798 {
9799 struct frame *f = XFRAME (w->frame);
9800 struct glyph *cursor_glyph;
9801 GC gc;
9802 int x;
9803 unsigned long mask;
9804 XGCValues xgcv;
9805 Display *dpy;
9806 Window window;
9807
9808 cursor_glyph = get_phys_cursor_glyph (w);
9809 if (cursor_glyph == NULL)
9810 return;
9811
9812 xgcv.background = f->output_data.x->cursor_pixel;
9813 xgcv.foreground = f->output_data.x->cursor_pixel;
9814 xgcv.graphics_exposures = 0;
9815 mask = GCForeground | GCBackground | GCGraphicsExposures;
9816 dpy = FRAME_X_DISPLAY (f);
9817 window = FRAME_X_WINDOW (f);
9818 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
9819
9820 if (gc)
9821 XChangeGC (dpy, gc, mask, &xgcv);
9822 else
9823 {
9824 gc = XCreateGC (dpy, window, mask, &xgcv);
9825 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9826 }
9827
9828 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9829 x_clip_to_row (w, row, gc, 0);
9830 XFillRectangle (dpy, window, gc,
9831 x,
9832 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9833 min (cursor_glyph->pixel_width,
9834 f->output_data.x->cursor_width),
9835 row->height);
9836 XSetClipMask (dpy, gc, None);
9837 }
9838 }
9839
9840
9841 /* Clear the cursor of window W to background color, and mark the
9842 cursor as not shown. This is used when the text where the cursor
9843 is is about to be rewritten. */
9844
9845 static void
9846 x_clear_cursor (w)
9847 struct window *w;
9848 {
9849 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9850 x_update_window_cursor (w, 0);
9851 }
9852
9853
9854 /* Draw the cursor glyph of window W in glyph row ROW. See the
9855 comment of x_draw_glyphs for the meaning of HL. */
9856
9857 static void
9858 x_draw_phys_cursor_glyph (w, row, hl)
9859 struct window *w;
9860 struct glyph_row *row;
9861 enum draw_glyphs_face hl;
9862 {
9863 /* If cursor hpos is out of bounds, don't draw garbage. This can
9864 happen in mini-buffer windows when switching between echo area
9865 glyphs and mini-buffer. */
9866 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9867 {
9868 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9869 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
9870 hl, 0, 0, 0);
9871
9872 /* When we erase the cursor, and ROW is overlapped by other
9873 rows, make sure that these overlapping parts of other rows
9874 are redrawn. */
9875 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
9876 {
9877 if (row > w->current_matrix->rows
9878 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9879 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9880
9881 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9882 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9883 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9884 }
9885 }
9886 }
9887
9888
9889 /* Erase the image of a cursor of window W from the screen. */
9890
9891 static void
9892 x_erase_phys_cursor (w)
9893 struct window *w;
9894 {
9895 struct frame *f = XFRAME (w->frame);
9896 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9897 int hpos = w->phys_cursor.hpos;
9898 int vpos = w->phys_cursor.vpos;
9899 int mouse_face_here_p = 0;
9900 struct glyph_matrix *active_glyphs = w->current_matrix;
9901 struct glyph_row *cursor_row;
9902 struct glyph *cursor_glyph;
9903 enum draw_glyphs_face hl;
9904
9905 /* No cursor displayed or row invalidated => nothing to do on the
9906 screen. */
9907 if (w->phys_cursor_type == NO_CURSOR)
9908 goto mark_cursor_off;
9909
9910 /* VPOS >= active_glyphs->nrows means that window has been resized.
9911 Don't bother to erase the cursor. */
9912 if (vpos >= active_glyphs->nrows)
9913 goto mark_cursor_off;
9914
9915 /* If row containing cursor is marked invalid, there is nothing we
9916 can do. */
9917 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9918 if (!cursor_row->enabled_p)
9919 goto mark_cursor_off;
9920
9921 /* This can happen when the new row is shorter than the old one.
9922 In this case, either x_draw_glyphs or clear_end_of_line
9923 should have cleared the cursor. Note that we wouldn't be
9924 able to erase the cursor in this case because we don't have a
9925 cursor glyph at hand. */
9926 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9927 goto mark_cursor_off;
9928
9929 /* If the cursor is in the mouse face area, redisplay that when
9930 we clear the cursor. */
9931 if (w == XWINDOW (dpyinfo->mouse_face_window)
9932 && (vpos > dpyinfo->mouse_face_beg_row
9933 || (vpos == dpyinfo->mouse_face_beg_row
9934 && hpos >= dpyinfo->mouse_face_beg_col))
9935 && (vpos < dpyinfo->mouse_face_end_row
9936 || (vpos == dpyinfo->mouse_face_end_row
9937 && hpos < dpyinfo->mouse_face_end_col))
9938 /* Don't redraw the cursor's spot in mouse face if it is at the
9939 end of a line (on a newline). The cursor appears there, but
9940 mouse highlighting does not. */
9941 && cursor_row->used[TEXT_AREA] > hpos)
9942 mouse_face_here_p = 1;
9943
9944 /* Maybe clear the display under the cursor. */
9945 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9946 {
9947 int x;
9948 int top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9949
9950 cursor_glyph = get_phys_cursor_glyph (w);
9951 if (cursor_glyph == NULL)
9952 goto mark_cursor_off;
9953
9954 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
9955
9956 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9957 x,
9958 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
9959 cursor_row->y)),
9960 cursor_glyph->pixel_width,
9961 cursor_row->visible_height,
9962 False);
9963 }
9964
9965 /* Erase the cursor by redrawing the character underneath it. */
9966 if (mouse_face_here_p)
9967 hl = DRAW_MOUSE_FACE;
9968 else if (cursor_row->inverse_p)
9969 hl = DRAW_INVERSE_VIDEO;
9970 else
9971 hl = DRAW_NORMAL_TEXT;
9972 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9973
9974 mark_cursor_off:
9975 w->phys_cursor_on_p = 0;
9976 w->phys_cursor_type = NO_CURSOR;
9977 }
9978
9979
9980 /* Display or clear cursor of window W. If ON is zero, clear the
9981 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9982 where to put the cursor is specified by HPOS, VPOS, X and Y. */
9983
9984 void
9985 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9986 struct window *w;
9987 int on, hpos, vpos, x, y;
9988 {
9989 struct frame *f = XFRAME (w->frame);
9990 int new_cursor_type;
9991 struct glyph_matrix *current_glyphs;
9992 struct glyph_row *glyph_row;
9993 struct glyph *glyph;
9994
9995 /* This is pointless on invisible frames, and dangerous on garbaged
9996 windows and frames; in the latter case, the frame or window may
9997 be in the midst of changing its size, and x and y may be off the
9998 window. */
9999 if (! FRAME_VISIBLE_P (f)
10000 || FRAME_GARBAGED_P (f)
10001 || vpos >= w->current_matrix->nrows
10002 || hpos >= w->current_matrix->matrix_w)
10003 return;
10004
10005 /* If cursor is off and we want it off, return quickly. */
10006 if (!on && !w->phys_cursor_on_p)
10007 return;
10008
10009 current_glyphs = w->current_matrix;
10010 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10011 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10012
10013 /* If cursor row is not enabled, we don't really know where to
10014 display the cursor. */
10015 if (!glyph_row->enabled_p)
10016 {
10017 w->phys_cursor_on_p = 0;
10018 return;
10019 }
10020
10021 xassert (interrupt_input_blocked);
10022
10023 /* Set new_cursor_type to the cursor we want to be displayed. In a
10024 mini-buffer window, we want the cursor only to appear if we are
10025 reading input from this window. For the selected window, we want
10026 the cursor type given by the frame parameter. If explicitly
10027 marked off, draw no cursor. In all other cases, we want a hollow
10028 box cursor. */
10029 if (cursor_in_echo_area
10030 && FRAME_HAS_MINIBUF_P (f)
10031 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10032 {
10033 if (w == XWINDOW (echo_area_window))
10034 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10035 else
10036 new_cursor_type = HOLLOW_BOX_CURSOR;
10037 }
10038 else
10039 {
10040 if (w != XWINDOW (selected_window)
10041 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10042 {
10043 if (MINI_WINDOW_P (w))
10044 new_cursor_type = NO_CURSOR;
10045 else
10046 new_cursor_type = HOLLOW_BOX_CURSOR;
10047 }
10048 else if (w->cursor_off_p)
10049 new_cursor_type = NO_CURSOR;
10050 else
10051 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10052 }
10053
10054 /* If cursor is currently being shown and we don't want it to be or
10055 it is in the wrong place, or the cursor type is not what we want,
10056 erase it. */
10057 if (w->phys_cursor_on_p
10058 && (!on
10059 || w->phys_cursor.x != x
10060 || w->phys_cursor.y != y
10061 || new_cursor_type != w->phys_cursor_type))
10062 x_erase_phys_cursor (w);
10063
10064 /* If the cursor is now invisible and we want it to be visible,
10065 display it. */
10066 if (on && !w->phys_cursor_on_p)
10067 {
10068 w->phys_cursor_ascent = glyph_row->ascent;
10069 w->phys_cursor_height = glyph_row->height;
10070
10071 /* Set phys_cursor_.* before x_draw_.* is called because some
10072 of them may need the information. */
10073 w->phys_cursor.x = x;
10074 w->phys_cursor.y = glyph_row->y;
10075 w->phys_cursor.hpos = hpos;
10076 w->phys_cursor.vpos = vpos;
10077 w->phys_cursor_type = new_cursor_type;
10078 w->phys_cursor_on_p = 1;
10079
10080 switch (new_cursor_type)
10081 {
10082 case HOLLOW_BOX_CURSOR:
10083 x_draw_hollow_cursor (w, glyph_row);
10084 break;
10085
10086 case FILLED_BOX_CURSOR:
10087 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10088 break;
10089
10090 case BAR_CURSOR:
10091 x_draw_bar_cursor (w, glyph_row);
10092 break;
10093
10094 case NO_CURSOR:
10095 break;
10096
10097 default:
10098 abort ();
10099 }
10100 }
10101
10102 #ifndef XFlush
10103 if (updating_frame != f)
10104 XFlush (FRAME_X_DISPLAY (f));
10105 #endif
10106 }
10107
10108
10109 /* Display the cursor on window W, or clear it. X and Y are window
10110 relative pixel coordinates. HPOS and VPOS are glyph matrix
10111 positions. If W is not the selected window, display a hollow
10112 cursor. ON non-zero means display the cursor at X, Y which
10113 correspond to HPOS, VPOS, otherwise it is cleared. */
10114
10115 void
10116 x_display_cursor (w, on, hpos, vpos, x, y)
10117 struct window *w;
10118 int on, hpos, vpos, x, y;
10119 {
10120 BLOCK_INPUT;
10121 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10122 UNBLOCK_INPUT;
10123 }
10124
10125
10126 /* Display the cursor on window W, or clear it, according to ON_P.
10127 Don't change the cursor's position. */
10128
10129 void
10130 x_update_cursor (f, on_p)
10131 struct frame *f;
10132 {
10133 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10134 }
10135
10136
10137 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10138 in the window tree rooted at W. */
10139
10140 static void
10141 x_update_cursor_in_window_tree (w, on_p)
10142 struct window *w;
10143 int on_p;
10144 {
10145 while (w)
10146 {
10147 if (!NILP (w->hchild))
10148 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10149 else if (!NILP (w->vchild))
10150 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10151 else
10152 x_update_window_cursor (w, on_p);
10153
10154 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10155 }
10156 }
10157
10158
10159 /* Switch the display of W's cursor on or off, according to the value
10160 of ON. */
10161
10162 static void
10163 x_update_window_cursor (w, on)
10164 struct window *w;
10165 int on;
10166 {
10167 BLOCK_INPUT;
10168 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10169 w->phys_cursor.x, w->phys_cursor.y);
10170 UNBLOCK_INPUT;
10171 }
10172
10173
10174
10175 \f
10176 /* Icons. */
10177
10178 /* Refresh bitmap kitchen sink icon for frame F
10179 when we get an expose event for it. */
10180
10181 void
10182 refreshicon (f)
10183 struct frame *f;
10184 {
10185 /* Normally, the window manager handles this function. */
10186 }
10187
10188 /* Make the x-window of frame F use the gnu icon bitmap. */
10189
10190 int
10191 x_bitmap_icon (f, file)
10192 struct frame *f;
10193 Lisp_Object file;
10194 {
10195 int bitmap_id;
10196
10197 if (FRAME_X_WINDOW (f) == 0)
10198 return 1;
10199
10200 /* Free up our existing icon bitmap if any. */
10201 if (f->output_data.x->icon_bitmap > 0)
10202 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10203 f->output_data.x->icon_bitmap = 0;
10204
10205 if (STRINGP (file))
10206 bitmap_id = x_create_bitmap_from_file (f, file);
10207 else
10208 {
10209 /* Create the GNU bitmap if necessary. */
10210 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10211 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10212 = x_create_bitmap_from_data (f, gnu_bits,
10213 gnu_width, gnu_height);
10214
10215 /* The first time we create the GNU bitmap,
10216 this increments the ref-count one extra time.
10217 As a result, the GNU bitmap is never freed.
10218 That way, we don't have to worry about allocating it again. */
10219 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10220
10221 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10222 }
10223
10224 x_wm_set_icon_pixmap (f, bitmap_id);
10225 f->output_data.x->icon_bitmap = bitmap_id;
10226
10227 return 0;
10228 }
10229
10230
10231 /* Make the x-window of frame F use a rectangle with text.
10232 Use ICON_NAME as the text. */
10233
10234 int
10235 x_text_icon (f, icon_name)
10236 struct frame *f;
10237 char *icon_name;
10238 {
10239 if (FRAME_X_WINDOW (f) == 0)
10240 return 1;
10241
10242 #ifdef HAVE_X11R4
10243 {
10244 XTextProperty text;
10245 text.value = (unsigned char *) icon_name;
10246 text.encoding = XA_STRING;
10247 text.format = 8;
10248 text.nitems = strlen (icon_name);
10249 #ifdef USE_X_TOOLKIT
10250 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10251 &text);
10252 #else /* not USE_X_TOOLKIT */
10253 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10254 #endif /* not USE_X_TOOLKIT */
10255 }
10256 #else /* not HAVE_X11R4 */
10257 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10258 #endif /* not HAVE_X11R4 */
10259
10260 if (f->output_data.x->icon_bitmap > 0)
10261 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10262 f->output_data.x->icon_bitmap = 0;
10263 x_wm_set_icon_pixmap (f, 0);
10264
10265 return 0;
10266 }
10267 \f
10268 #define X_ERROR_MESSAGE_SIZE 200
10269
10270 /* If non-nil, this should be a string.
10271 It means catch X errors and store the error message in this string. */
10272
10273 static Lisp_Object x_error_message_string;
10274
10275 /* An X error handler which stores the error message in
10276 x_error_message_string. This is called from x_error_handler if
10277 x_catch_errors is in effect. */
10278
10279 static void
10280 x_error_catcher (display, error)
10281 Display *display;
10282 XErrorEvent *error;
10283 {
10284 XGetErrorText (display, error->error_code,
10285 XSTRING (x_error_message_string)->data,
10286 X_ERROR_MESSAGE_SIZE);
10287 }
10288
10289 /* Begin trapping X errors for display DPY. Actually we trap X errors
10290 for all displays, but DPY should be the display you are actually
10291 operating on.
10292
10293 After calling this function, X protocol errors no longer cause
10294 Emacs to exit; instead, they are recorded in the string
10295 stored in x_error_message_string.
10296
10297 Calling x_check_errors signals an Emacs error if an X error has
10298 occurred since the last call to x_catch_errors or x_check_errors.
10299
10300 Calling x_uncatch_errors resumes the normal error handling. */
10301
10302 void x_check_errors ();
10303 static Lisp_Object x_catch_errors_unwind ();
10304
10305 int
10306 x_catch_errors (dpy)
10307 Display *dpy;
10308 {
10309 int count = specpdl_ptr - specpdl;
10310
10311 /* Make sure any errors from previous requests have been dealt with. */
10312 XSync (dpy, False);
10313
10314 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10315
10316 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10317 XSTRING (x_error_message_string)->data[0] = 0;
10318
10319 return count;
10320 }
10321
10322 /* Unbind the binding that we made to check for X errors. */
10323
10324 static Lisp_Object
10325 x_catch_errors_unwind (old_val)
10326 Lisp_Object old_val;
10327 {
10328 x_error_message_string = old_val;
10329 return Qnil;
10330 }
10331
10332 /* If any X protocol errors have arrived since the last call to
10333 x_catch_errors or x_check_errors, signal an Emacs error using
10334 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10335
10336 void
10337 x_check_errors (dpy, format)
10338 Display *dpy;
10339 char *format;
10340 {
10341 /* Make sure to catch any errors incurred so far. */
10342 XSync (dpy, False);
10343
10344 if (XSTRING (x_error_message_string)->data[0])
10345 error (format, XSTRING (x_error_message_string)->data);
10346 }
10347
10348 /* Nonzero if we had any X protocol errors
10349 since we did x_catch_errors on DPY. */
10350
10351 int
10352 x_had_errors_p (dpy)
10353 Display *dpy;
10354 {
10355 /* Make sure to catch any errors incurred so far. */
10356 XSync (dpy, False);
10357
10358 return XSTRING (x_error_message_string)->data[0] != 0;
10359 }
10360
10361 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10362
10363 void
10364 x_clear_errors (dpy)
10365 Display *dpy;
10366 {
10367 XSTRING (x_error_message_string)->data[0] = 0;
10368 }
10369
10370 /* Stop catching X protocol errors and let them make Emacs die.
10371 DPY should be the display that was passed to x_catch_errors.
10372 COUNT should be the value that was returned by
10373 the corresponding call to x_catch_errors. */
10374
10375 void
10376 x_uncatch_errors (dpy, count)
10377 Display *dpy;
10378 int count;
10379 {
10380 unbind_to (count, Qnil);
10381 }
10382
10383 #if 0
10384 static unsigned int x_wire_count;
10385 x_trace_wire ()
10386 {
10387 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10388 }
10389 #endif /* ! 0 */
10390
10391 \f
10392 /* Handle SIGPIPE, which can happen when the connection to a server
10393 simply goes away. SIGPIPE is handled by x_connection_signal.
10394 Don't need to do anything, because the write which caused the
10395 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
10396 which will do the appropriate cleanup for us. */
10397
10398 static SIGTYPE
10399 x_connection_signal (signalnum) /* If we don't have an argument, */
10400 int signalnum; /* some compilers complain in signal calls. */
10401 {
10402 #ifdef USG
10403 /* USG systems forget handlers when they are used;
10404 must reestablish each time */
10405 signal (signalnum, x_connection_signal);
10406 #endif /* USG */
10407 }
10408 \f
10409 /* Handling X errors. */
10410
10411 /* Handle the loss of connection to display DISPLAY. */
10412
10413 static SIGTYPE
10414 x_connection_closed (display, error_message)
10415 Display *display;
10416 char *error_message;
10417 {
10418 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10419 Lisp_Object frame, tail;
10420
10421 /* Indicate that this display is dead. */
10422
10423 #ifdef USE_X_TOOLKIT
10424 XtCloseDisplay (display);
10425 #endif
10426
10427 dpyinfo->display = 0;
10428
10429 /* First delete frames whose mini-buffers are on frames
10430 that are on the dead display. */
10431 FOR_EACH_FRAME (tail, frame)
10432 {
10433 Lisp_Object minibuf_frame;
10434 minibuf_frame
10435 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
10436 if (FRAME_X_P (XFRAME (frame))
10437 && FRAME_X_P (XFRAME (minibuf_frame))
10438 && ! EQ (frame, minibuf_frame)
10439 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
10440 Fdelete_frame (frame, Qt);
10441 }
10442
10443 /* Now delete all remaining frames on the dead display.
10444 We are now sure none of these is used as the mini-buffer
10445 for another frame that we need to delete. */
10446 FOR_EACH_FRAME (tail, frame)
10447 if (FRAME_X_P (XFRAME (frame))
10448 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
10449 {
10450 /* Set this to t so that Fdelete_frame won't get confused
10451 trying to find a replacement. */
10452 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10453 Fdelete_frame (frame, Qt);
10454 }
10455
10456 if (dpyinfo)
10457 x_delete_display (dpyinfo);
10458
10459 if (x_display_list == 0)
10460 {
10461 fprintf (stderr, "%s\n", error_message);
10462 shut_down_emacs (0, 0, Qnil);
10463 exit (70);
10464 }
10465
10466 /* Ordinary stack unwind doesn't deal with these. */
10467 #ifdef SIGIO
10468 sigunblock (sigmask (SIGIO));
10469 #endif
10470 sigunblock (sigmask (SIGALRM));
10471 TOTALLY_UNBLOCK_INPUT;
10472
10473 clear_waiting_for_input ();
10474 error ("%s", error_message);
10475 }
10476
10477 /* This is the usual handler for X protocol errors.
10478 It kills all frames on the display that we got the error for.
10479 If that was the only one, it prints an error message and kills Emacs. */
10480
10481 static void
10482 x_error_quitter (display, error)
10483 Display *display;
10484 XErrorEvent *error;
10485 {
10486 char buf[256], buf1[356];
10487
10488 /* Note that there is no real way portable across R3/R4 to get the
10489 original error handler. */
10490
10491 XGetErrorText (display, error->error_code, buf, sizeof (buf));
10492 sprintf (buf1, "X protocol error: %s on protocol request %d",
10493 buf, error->request_code);
10494 x_connection_closed (display, buf1);
10495 }
10496
10497 /* This is the first-level handler for X protocol errors.
10498 It calls x_error_quitter or x_error_catcher. */
10499
10500 static int
10501 x_error_handler (display, error)
10502 Display *display;
10503 XErrorEvent *error;
10504 {
10505 if (! NILP (x_error_message_string))
10506 x_error_catcher (display, error);
10507 else
10508 x_error_quitter (display, error);
10509 return 0;
10510 }
10511
10512 /* This is the handler for X IO errors, always.
10513 It kills all frames on the display that we lost touch with.
10514 If that was the only one, it prints an error message and kills Emacs. */
10515
10516 static int
10517 x_io_error_quitter (display)
10518 Display *display;
10519 {
10520 char buf[256];
10521
10522 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10523 x_connection_closed (display, buf);
10524 return 0;
10525 }
10526 \f
10527 /* Changing the font of the frame. */
10528
10529 /* Give frame F the font named FONTNAME as its default font, and
10530 return the full name of that font. FONTNAME may be a wildcard
10531 pattern; in that case, we choose some font that fits the pattern.
10532 The return value shows which font we chose. */
10533
10534 Lisp_Object
10535 x_new_font (f, fontname)
10536 struct frame *f;
10537 register char *fontname;
10538 {
10539 struct font_info *fontp
10540 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
10541
10542 if (!fontp)
10543 return Qnil;
10544
10545 f->output_data.x->font = (XFontStruct *) (fontp->font);
10546 f->output_data.x->font_baseline
10547 = (f->output_data.x->font->ascent + fontp->baseline_offset);
10548 f->output_data.x->fontset = -1;
10549
10550 /* Compute the scroll bar width in character columns. */
10551 if (f->scroll_bar_pixel_width > 0)
10552 {
10553 int wid = FONT_WIDTH (f->output_data.x->font);
10554 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10555 }
10556 else
10557 {
10558 int wid = FONT_WIDTH (f->output_data.x->font);
10559 f->scroll_bar_cols = (14 + wid - 1) / wid;
10560 }
10561
10562 /* Now make the frame display the given font. */
10563 if (FRAME_X_WINDOW (f) != 0)
10564 {
10565 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10566 f->output_data.x->font->fid);
10567 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10568 f->output_data.x->font->fid);
10569 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10570 f->output_data.x->font->fid);
10571
10572 frame_update_line_height (f);
10573 x_set_window_size (f, 0, f->width, f->height);
10574 }
10575 else
10576 /* If we are setting a new frame's font for the first time,
10577 there are no faces yet, so this font's height is the line height. */
10578 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
10579
10580 return build_string (fontp->full_name);
10581 }
10582
10583 /* Give frame F the fontset named FONTSETNAME as its default font, and
10584 return the full name of that fontset. FONTSETNAME may be a wildcard
10585 pattern; in that case, we choose some fontset that fits the pattern.
10586 The return value shows which fontset we chose. */
10587
10588 Lisp_Object
10589 x_new_fontset (f, fontsetname)
10590 struct frame *f;
10591 char *fontsetname;
10592 {
10593 int fontset = fs_query_fontset (f, fontsetname);
10594 struct fontset_info *fontsetp;
10595 Lisp_Object result;
10596
10597 if (fontset < 0)
10598 return Qnil;
10599
10600 if (f->output_data.x->fontset == fontset)
10601 /* This fontset is already set in frame F. There's nothing more
10602 to do. */
10603 return build_string (fontsetname);
10604
10605 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10606
10607 if (!fontsetp->fontname[CHARSET_ASCII])
10608 /* This fontset doesn't contain ASCII font. */
10609 return Qnil;
10610
10611 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10612
10613 if (!STRINGP (result))
10614 /* Can't load ASCII font. */
10615 return Qnil;
10616
10617 /* Since x_new_font doesn't update any fontset information, do it now. */
10618 f->output_data.x->fontset = fontset;
10619 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
10620 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
10621
10622 return build_string (fontsetname);
10623 }
10624 \f
10625 /* Calculate the absolute position in frame F
10626 from its current recorded position values and gravity. */
10627
10628 void
10629 x_calc_absolute_position (f)
10630 struct frame *f;
10631 {
10632 Window child;
10633 int win_x = 0, win_y = 0;
10634 int flags = f->output_data.x->size_hint_flags;
10635 int this_window;
10636
10637 /* We have nothing to do if the current position
10638 is already for the top-left corner. */
10639 if (! ((flags & XNegative) || (flags & YNegative)))
10640 return;
10641
10642 #ifdef USE_X_TOOLKIT
10643 this_window = XtWindow (f->output_data.x->widget);
10644 #else
10645 this_window = FRAME_X_WINDOW (f);
10646 #endif
10647
10648 /* Find the position of the outside upper-left corner of
10649 the inner window, with respect to the outer window.
10650 But do this only if we will need the results. */
10651 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10652 {
10653 int count;
10654
10655 BLOCK_INPUT;
10656 count = x_catch_errors (FRAME_X_DISPLAY (f));
10657 while (1)
10658 {
10659 x_clear_errors (FRAME_X_DISPLAY (f));
10660 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10661
10662 /* From-window, to-window. */
10663 this_window,
10664 f->output_data.x->parent_desc,
10665
10666 /* From-position, to-position. */
10667 0, 0, &win_x, &win_y,
10668
10669 /* Child of win. */
10670 &child);
10671 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10672 {
10673 Window newroot, newparent = 0xdeadbeef;
10674 Window *newchildren;
10675 int nchildren;
10676
10677 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10678 &newparent, &newchildren, &nchildren))
10679 break;
10680
10681 XFree ((char *) newchildren);
10682
10683 f->output_data.x->parent_desc = newparent;
10684 }
10685 else
10686 break;
10687 }
10688
10689 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
10690 UNBLOCK_INPUT;
10691 }
10692
10693 /* Treat negative positions as relative to the leftmost bottommost
10694 position that fits on the screen. */
10695 if (flags & XNegative)
10696 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
10697 - 2 * f->output_data.x->border_width - win_x
10698 - PIXEL_WIDTH (f)
10699 + f->output_data.x->left_pos);
10700
10701 if (flags & YNegative)
10702 {
10703 int menubar_height = 0;
10704
10705 #ifdef USE_X_TOOLKIT
10706 if (f->output_data.x->menubar_widget)
10707 menubar_height
10708 = (f->output_data.x->menubar_widget->core.height
10709 + f->output_data.x->menubar_widget->core.border_width);
10710 #endif
10711
10712 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10713 - 2 * f->output_data.x->border_width
10714 - win_y
10715 - PIXEL_HEIGHT (f)
10716 - menubar_height
10717 + f->output_data.x->top_pos);
10718 }
10719
10720 /* The left_pos and top_pos
10721 are now relative to the top and left screen edges,
10722 so the flags should correspond. */
10723 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
10724 }
10725
10726 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10727 to really change the position, and 0 when calling from
10728 x_make_frame_visible (in that case, XOFF and YOFF are the current
10729 position values). It is -1 when calling from x_set_frame_parameters,
10730 which means, do adjust for borders but don't change the gravity. */
10731
10732 void
10733 x_set_offset (f, xoff, yoff, change_gravity)
10734 struct frame *f;
10735 register int xoff, yoff;
10736 int change_gravity;
10737 {
10738 int modified_top, modified_left;
10739
10740 if (change_gravity > 0)
10741 {
10742 f->output_data.x->top_pos = yoff;
10743 f->output_data.x->left_pos = xoff;
10744 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
10745 if (xoff < 0)
10746 f->output_data.x->size_hint_flags |= XNegative;
10747 if (yoff < 0)
10748 f->output_data.x->size_hint_flags |= YNegative;
10749 f->output_data.x->win_gravity = NorthWestGravity;
10750 }
10751 x_calc_absolute_position (f);
10752
10753 BLOCK_INPUT;
10754 x_wm_set_size_hint (f, (long) 0, 0);
10755
10756 modified_left = f->output_data.x->left_pos;
10757 modified_top = f->output_data.x->top_pos;
10758 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
10759 this seems to be unnecessary and incorrect. rms, 4/17/97. */
10760 /* It is a mystery why we need to add the border_width here
10761 when the frame is already visible, but experiment says we do. */
10762 if (change_gravity != 0)
10763 {
10764 modified_left += f->output_data.x->border_width;
10765 modified_top += f->output_data.x->border_width;
10766 }
10767 #endif
10768
10769 #ifdef USE_X_TOOLKIT
10770 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10771 modified_left, modified_top);
10772 #else /* not USE_X_TOOLKIT */
10773 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10774 modified_left, modified_top);
10775 #endif /* not USE_X_TOOLKIT */
10776 UNBLOCK_INPUT;
10777 }
10778
10779 /* Call this to change the size of frame F's x-window.
10780 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
10781 for this size change and subsequent size changes.
10782 Otherwise we leave the window gravity unchanged. */
10783
10784 void
10785 x_set_window_size (f, change_gravity, cols, rows)
10786 struct frame *f;
10787 int change_gravity;
10788 int cols, rows;
10789 {
10790 #ifndef USE_X_TOOLKIT
10791 int pixelwidth, pixelheight;
10792 #endif
10793
10794 BLOCK_INPUT;
10795
10796 #ifdef USE_X_TOOLKIT
10797 {
10798 /* The x and y position of the widget is clobbered by the
10799 call to XtSetValues within EmacsFrameSetCharSize.
10800 This is a real kludge, but I don't understand Xt so I can't
10801 figure out a correct fix. Can anyone else tell me? -- rms. */
10802 int xpos = f->output_data.x->widget->core.x;
10803 int ypos = f->output_data.x->widget->core.y;
10804 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
10805 f->output_data.x->widget->core.x = xpos;
10806 f->output_data.x->widget->core.y = ypos;
10807 }
10808
10809 #else /* not USE_X_TOOLKIT */
10810
10811 check_frame_size (f, &rows, &cols);
10812 f->output_data.x->vertical_scroll_bar_extra
10813 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10814 ? 0
10815 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
10816 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
10817 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
10818 f->output_data.x->flags_areas_extra
10819 = FRAME_FLAGS_AREA_WIDTH (f);
10820 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10821 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
10822
10823 f->output_data.x->win_gravity = NorthWestGravity;
10824 x_wm_set_size_hint (f, (long) 0, 0);
10825
10826 XSync (FRAME_X_DISPLAY (f), False);
10827 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10828 pixelwidth, pixelheight);
10829
10830 /* Now, strictly speaking, we can't be sure that this is accurate,
10831 but the window manager will get around to dealing with the size
10832 change request eventually, and we'll hear how it went when the
10833 ConfigureNotify event gets here.
10834
10835 We could just not bother storing any of this information here,
10836 and let the ConfigureNotify event set everything up, but that
10837 might be kind of confusing to the Lisp code, since size changes
10838 wouldn't be reported in the frame parameters until some random
10839 point in the future when the ConfigureNotify event arrives.
10840
10841 We pass 1 for DELAY since we can't run Lisp code inside of
10842 a BLOCK_INPUT. */
10843 change_frame_size (f, rows, cols, 0, 1, 0);
10844 PIXEL_WIDTH (f) = pixelwidth;
10845 PIXEL_HEIGHT (f) = pixelheight;
10846
10847 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10848 receive in the ConfigureNotify event; if we get what we asked
10849 for, then the event won't cause the screen to become garbaged, so
10850 we have to make sure to do it here. */
10851 SET_FRAME_GARBAGED (f);
10852
10853 XFlush (FRAME_X_DISPLAY (f));
10854
10855 #endif /* not USE_X_TOOLKIT */
10856
10857 /* If cursor was outside the new size, mark it as off. */
10858 mark_window_cursors_off (XWINDOW (f->root_window));
10859
10860 /* Clear out any recollection of where the mouse highlighting was,
10861 since it might be in a place that's outside the new frame size.
10862 Actually checking whether it is outside is a pain in the neck,
10863 so don't try--just let the highlighting be done afresh with new size. */
10864 cancel_mouse_face (f);
10865
10866 UNBLOCK_INPUT;
10867 }
10868 \f
10869 /* Mouse warping. */
10870
10871 void
10872 x_set_mouse_position (f, x, y)
10873 struct frame *f;
10874 int x, y;
10875 {
10876 int pix_x, pix_y;
10877
10878 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
10879 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
10880
10881 if (pix_x < 0) pix_x = 0;
10882 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
10883
10884 if (pix_y < 0) pix_y = 0;
10885 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
10886
10887 BLOCK_INPUT;
10888
10889 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10890 0, 0, 0, 0, pix_x, pix_y);
10891 UNBLOCK_INPUT;
10892 }
10893
10894 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10895
10896 void
10897 x_set_mouse_pixel_position (f, pix_x, pix_y)
10898 struct frame *f;
10899 int pix_x, pix_y;
10900 {
10901 BLOCK_INPUT;
10902
10903 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10904 0, 0, 0, 0, pix_x, pix_y);
10905 UNBLOCK_INPUT;
10906 }
10907 \f
10908 /* focus shifting, raising and lowering. */
10909
10910 void
10911 x_focus_on_frame (f)
10912 struct frame *f;
10913 {
10914 #if 0 /* This proves to be unpleasant. */
10915 x_raise_frame (f);
10916 #endif
10917 #if 0
10918 /* I don't think that the ICCCM allows programs to do things like this
10919 without the interaction of the window manager. Whatever you end up
10920 doing with this code, do it to x_unfocus_frame too. */
10921 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10922 RevertToPointerRoot, CurrentTime);
10923 #endif /* ! 0 */
10924 }
10925
10926 void
10927 x_unfocus_frame (f)
10928 struct frame *f;
10929 {
10930 #if 0
10931 /* Look at the remarks in x_focus_on_frame. */
10932 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
10933 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
10934 RevertToPointerRoot, CurrentTime);
10935 #endif /* ! 0 */
10936 }
10937
10938 /* Raise frame F. */
10939
10940 void
10941 x_raise_frame (f)
10942 struct frame *f;
10943 {
10944 if (f->async_visible)
10945 {
10946 BLOCK_INPUT;
10947 #ifdef USE_X_TOOLKIT
10948 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
10949 #else /* not USE_X_TOOLKIT */
10950 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10951 #endif /* not USE_X_TOOLKIT */
10952 XFlush (FRAME_X_DISPLAY (f));
10953 UNBLOCK_INPUT;
10954 }
10955 }
10956
10957 /* Lower frame F. */
10958
10959 void
10960 x_lower_frame (f)
10961 struct frame *f;
10962 {
10963 if (f->async_visible)
10964 {
10965 BLOCK_INPUT;
10966 #ifdef USE_X_TOOLKIT
10967 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
10968 #else /* not USE_X_TOOLKIT */
10969 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10970 #endif /* not USE_X_TOOLKIT */
10971 XFlush (FRAME_X_DISPLAY (f));
10972 UNBLOCK_INPUT;
10973 }
10974 }
10975
10976 static void
10977 XTframe_raise_lower (f, raise_flag)
10978 FRAME_PTR f;
10979 int raise_flag;
10980 {
10981 if (raise_flag)
10982 x_raise_frame (f);
10983 else
10984 x_lower_frame (f);
10985 }
10986 \f
10987 /* Change of visibility. */
10988
10989 /* This tries to wait until the frame is really visible.
10990 However, if the window manager asks the user where to position
10991 the frame, this will return before the user finishes doing that.
10992 The frame will not actually be visible at that time,
10993 but it will become visible later when the window manager
10994 finishes with it. */
10995
10996 void
10997 x_make_frame_visible (f)
10998 struct frame *f;
10999 {
11000 Lisp_Object type;
11001 int original_top, original_left;
11002
11003 BLOCK_INPUT;
11004
11005 type = x_icon_type (f);
11006 if (!NILP (type))
11007 x_bitmap_icon (f, type);
11008
11009 if (! FRAME_VISIBLE_P (f))
11010 {
11011 /* We test FRAME_GARBAGED_P here to make sure we don't
11012 call x_set_offset a second time
11013 if we get to x_make_frame_visible a second time
11014 before the window gets really visible. */
11015 if (! FRAME_ICONIFIED_P (f)
11016 && ! f->output_data.x->asked_for_visible)
11017 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11018
11019 f->output_data.x->asked_for_visible = 1;
11020
11021 if (! EQ (Vx_no_window_manager, Qt))
11022 x_wm_set_window_state (f, NormalState);
11023 #ifdef USE_X_TOOLKIT
11024 /* This was XtPopup, but that did nothing for an iconified frame. */
11025 XtMapWidget (f->output_data.x->widget);
11026 #else /* not USE_X_TOOLKIT */
11027 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11028 #endif /* not USE_X_TOOLKIT */
11029 #if 0 /* This seems to bring back scroll bars in the wrong places
11030 if the window configuration has changed. They seem
11031 to come back ok without this. */
11032 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11033 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11034 #endif
11035 }
11036
11037 XFlush (FRAME_X_DISPLAY (f));
11038
11039 /* Synchronize to ensure Emacs knows the frame is visible
11040 before we do anything else. We do this loop with input not blocked
11041 so that incoming events are handled. */
11042 {
11043 Lisp_Object frame;
11044 int count = input_signal_count;
11045 /* This must be before UNBLOCK_INPUT
11046 since events that arrive in response to the actions above
11047 will set it when they are handled. */
11048 int previously_visible = f->output_data.x->has_been_visible;
11049
11050 original_left = f->output_data.x->left_pos;
11051 original_top = f->output_data.x->top_pos;
11052
11053 /* This must come after we set COUNT. */
11054 UNBLOCK_INPUT;
11055
11056 /* We unblock here so that arriving X events are processed. */
11057
11058 /* Now move the window back to where it was "supposed to be".
11059 But don't do it if the gravity is negative.
11060 When the gravity is negative, this uses a position
11061 that is 3 pixels too low. Perhaps that's really the border width.
11062
11063 Don't do this if the window has never been visible before,
11064 because the window manager may choose the position
11065 and we don't want to override it. */
11066
11067 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11068 && f->output_data.x->win_gravity == NorthWestGravity
11069 && previously_visible)
11070 {
11071 Drawable rootw;
11072 int x, y;
11073 unsigned int width, height, border, depth;
11074
11075 BLOCK_INPUT;
11076
11077 /* On some window managers (such as FVWM) moving an existing
11078 window, even to the same place, causes the window manager
11079 to introduce an offset. This can cause the window to move
11080 to an unexpected location. Check the geometry (a little
11081 slow here) and then verify that the window is in the right
11082 place. If the window is not in the right place, move it
11083 there, and take the potential window manager hit. */
11084 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11085 &rootw, &x, &y, &width, &height, &border, &depth);
11086
11087 if (original_left != x || original_top != y)
11088 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11089 original_left, original_top);
11090
11091 UNBLOCK_INPUT;
11092 }
11093
11094 XSETFRAME (frame, f);
11095
11096 while (1)
11097 {
11098 x_sync (f);
11099 /* Once we have handled input events,
11100 we should have received the MapNotify if one is coming.
11101 So if we have not got it yet, stop looping.
11102 Some window managers make their own decisions
11103 about visibility. */
11104 if (input_signal_count != count)
11105 break;
11106 /* Machines that do polling rather than SIGIO have been observed
11107 to go into a busy-wait here. So we'll fake an alarm signal
11108 to let the handler know that there's something to be read.
11109 We used to raise a real alarm, but it seems that the handler
11110 isn't always enabled here. This is probably a bug. */
11111 if (input_polling_used ())
11112 {
11113 /* It could be confusing if a real alarm arrives while processing
11114 the fake one. Turn it off and let the handler reset it. */
11115 alarm (0);
11116 input_poll_signal (0);
11117 }
11118 /* Once we have handled input events,
11119 we should have received the MapNotify if one is coming.
11120 So if we have not got it yet, stop looping.
11121 Some window managers make their own decisions
11122 about visibility. */
11123 if (input_signal_count != count)
11124 break;
11125 }
11126 FRAME_SAMPLE_VISIBILITY (f);
11127 }
11128 }
11129
11130 /* Change from mapped state to withdrawn state. */
11131
11132 /* Make the frame visible (mapped and not iconified). */
11133
11134 void
11135 x_make_frame_invisible (f)
11136 struct frame *f;
11137 {
11138 Window window;
11139
11140 #ifdef USE_X_TOOLKIT
11141 /* Use the frame's outermost window, not the one we normally draw on. */
11142 window = XtWindow (f->output_data.x->widget);
11143 #else /* not USE_X_TOOLKIT */
11144 window = FRAME_X_WINDOW (f);
11145 #endif /* not USE_X_TOOLKIT */
11146
11147 /* Don't keep the highlight on an invisible frame. */
11148 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11149 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11150
11151 #if 0/* This might add unreliability; I don't trust it -- rms. */
11152 if (! f->async_visible && ! f->async_iconified)
11153 return;
11154 #endif
11155
11156 BLOCK_INPUT;
11157
11158 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11159 that the current position of the window is user-specified, rather than
11160 program-specified, so that when the window is mapped again, it will be
11161 placed at the same location, without forcing the user to position it
11162 by hand again (they have already done that once for this window.) */
11163 x_wm_set_size_hint (f, (long) 0, 1);
11164
11165 #ifdef HAVE_X11R4
11166
11167 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11168 DefaultScreen (FRAME_X_DISPLAY (f))))
11169 {
11170 UNBLOCK_INPUT_RESIGNAL;
11171 error ("Can't notify window manager of window withdrawal");
11172 }
11173 #else /* ! defined (HAVE_X11R4) */
11174
11175 /* Tell the window manager what we're going to do. */
11176 if (! EQ (Vx_no_window_manager, Qt))
11177 {
11178 XEvent unmap;
11179
11180 unmap.xunmap.type = UnmapNotify;
11181 unmap.xunmap.window = window;
11182 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
11183 unmap.xunmap.from_configure = False;
11184 if (! XSendEvent (FRAME_X_DISPLAY (f),
11185 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11186 False,
11187 SubstructureRedirectMaskSubstructureNotifyMask,
11188 &unmap))
11189 {
11190 UNBLOCK_INPUT_RESIGNAL;
11191 error ("Can't notify window manager of withdrawal");
11192 }
11193 }
11194
11195 /* Unmap the window ourselves. Cheeky! */
11196 XUnmapWindow (FRAME_X_DISPLAY (f), window);
11197 #endif /* ! defined (HAVE_X11R4) */
11198
11199 /* We can't distinguish this from iconification
11200 just by the event that we get from the server.
11201 So we can't win using the usual strategy of letting
11202 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11203 and synchronize with the server to make sure we agree. */
11204 f->visible = 0;
11205 FRAME_ICONIFIED_P (f) = 0;
11206 f->async_visible = 0;
11207 f->async_iconified = 0;
11208
11209 x_sync (f);
11210
11211 UNBLOCK_INPUT;
11212 }
11213
11214 /* Change window state from mapped to iconified. */
11215
11216 void
11217 x_iconify_frame (f)
11218 struct frame *f;
11219 {
11220 int result;
11221 Lisp_Object type;
11222
11223 /* Don't keep the highlight on an invisible frame. */
11224 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11225 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11226
11227 if (f->async_iconified)
11228 return;
11229
11230 BLOCK_INPUT;
11231
11232 FRAME_SAMPLE_VISIBILITY (f);
11233
11234 type = x_icon_type (f);
11235 if (!NILP (type))
11236 x_bitmap_icon (f, type);
11237
11238 #ifdef USE_X_TOOLKIT
11239
11240 if (! FRAME_VISIBLE_P (f))
11241 {
11242 if (! EQ (Vx_no_window_manager, Qt))
11243 x_wm_set_window_state (f, IconicState);
11244 /* This was XtPopup, but that did nothing for an iconified frame. */
11245 XtMapWidget (f->output_data.x->widget);
11246 /* The server won't give us any event to indicate
11247 that an invisible frame was changed to an icon,
11248 so we have to record it here. */
11249 f->iconified = 1;
11250 f->visible = 1;
11251 f->async_iconified = 1;
11252 f->async_visible = 0;
11253 UNBLOCK_INPUT;
11254 return;
11255 }
11256
11257 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11258 XtWindow (f->output_data.x->widget),
11259 DefaultScreen (FRAME_X_DISPLAY (f)));
11260 UNBLOCK_INPUT;
11261
11262 if (!result)
11263 error ("Can't notify window manager of iconification");
11264
11265 f->async_iconified = 1;
11266 f->async_visible = 0;
11267
11268
11269 BLOCK_INPUT;
11270 XFlush (FRAME_X_DISPLAY (f));
11271 UNBLOCK_INPUT;
11272 #else /* not USE_X_TOOLKIT */
11273
11274 /* Make sure the X server knows where the window should be positioned,
11275 in case the user deiconifies with the window manager. */
11276 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
11277 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11278
11279 /* Since we don't know which revision of X we're running, we'll use both
11280 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11281
11282 /* X11R4: send a ClientMessage to the window manager using the
11283 WM_CHANGE_STATE type. */
11284 {
11285 XEvent message;
11286
11287 message.xclient.window = FRAME_X_WINDOW (f);
11288 message.xclient.type = ClientMessage;
11289 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
11290 message.xclient.format = 32;
11291 message.xclient.data.l[0] = IconicState;
11292
11293 if (! XSendEvent (FRAME_X_DISPLAY (f),
11294 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11295 False,
11296 SubstructureRedirectMask | SubstructureNotifyMask,
11297 &message))
11298 {
11299 UNBLOCK_INPUT_RESIGNAL;
11300 error ("Can't notify window manager of iconification");
11301 }
11302 }
11303
11304 /* X11R3: set the initial_state field of the window manager hints to
11305 IconicState. */
11306 x_wm_set_window_state (f, IconicState);
11307
11308 if (!FRAME_VISIBLE_P (f))
11309 {
11310 /* If the frame was withdrawn, before, we must map it. */
11311 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11312 }
11313
11314 f->async_iconified = 1;
11315 f->async_visible = 0;
11316
11317 XFlush (FRAME_X_DISPLAY (f));
11318 UNBLOCK_INPUT;
11319 #endif /* not USE_X_TOOLKIT */
11320 }
11321 \f
11322 /* Destroy the X window of frame F. */
11323
11324 void
11325 x_destroy_window (f)
11326 struct frame *f;
11327 {
11328 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11329
11330 BLOCK_INPUT;
11331
11332 /* If a display connection is dead, don't try sending more
11333 commands to the X server. */
11334 if (dpyinfo->display != 0)
11335 {
11336 if (f->output_data.x->icon_desc != 0)
11337 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11338 #ifdef HAVE_X_I18N
11339 if (FRAME_XIM (f))
11340 {
11341 XDestroyIC (FRAME_XIC (f));
11342 #if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11343 /* This line causes crashes on Solaris with Openwin,
11344 due to an apparent bug in XCloseIM.
11345 X11R6 seems not to have the bug. */
11346 XCloseIM (FRAME_XIM (f));
11347 #endif
11348 }
11349 #endif
11350 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
11351 #ifdef USE_X_TOOLKIT
11352 if (f->output_data.x->widget)
11353 XtDestroyWidget (f->output_data.x->widget);
11354 free_frame_menubar (f);
11355 #endif /* USE_X_TOOLKIT */
11356
11357 free_frame_faces (f);
11358 XFlush (FRAME_X_DISPLAY (f));
11359 }
11360
11361 if (f->output_data.x->saved_menu_event)
11362 xfree (f->output_data.x->saved_menu_event);
11363
11364 xfree (f->output_data.x);
11365 f->output_data.x = 0;
11366 if (f == dpyinfo->x_focus_frame)
11367 dpyinfo->x_focus_frame = 0;
11368 if (f == dpyinfo->x_focus_event_frame)
11369 dpyinfo->x_focus_event_frame = 0;
11370 if (f == dpyinfo->x_highlight_frame)
11371 dpyinfo->x_highlight_frame = 0;
11372
11373 dpyinfo->reference_count--;
11374
11375 if (f == dpyinfo->mouse_face_mouse_frame)
11376 {
11377 dpyinfo->mouse_face_beg_row
11378 = dpyinfo->mouse_face_beg_col = -1;
11379 dpyinfo->mouse_face_end_row
11380 = dpyinfo->mouse_face_end_col = -1;
11381 dpyinfo->mouse_face_window = Qnil;
11382 dpyinfo->mouse_face_deferred_gc = 0;
11383 dpyinfo->mouse_face_mouse_frame = 0;
11384 }
11385
11386 UNBLOCK_INPUT;
11387 }
11388 \f
11389 /* Setting window manager hints. */
11390
11391 /* Set the normal size hints for the window manager, for frame F.
11392 FLAGS is the flags word to use--or 0 meaning preserve the flags
11393 that the window now has.
11394 If USER_POSITION is nonzero, we set the USPosition
11395 flag (this is useful when FLAGS is 0). */
11396
11397 void
11398 x_wm_set_size_hint (f, flags, user_position)
11399 struct frame *f;
11400 long flags;
11401 int user_position;
11402 {
11403 XSizeHints size_hints;
11404
11405 #ifdef USE_X_TOOLKIT
11406 Arg al[2];
11407 int ac = 0;
11408 Dimension widget_width, widget_height;
11409 Window window = XtWindow (f->output_data.x->widget);
11410 #else /* not USE_X_TOOLKIT */
11411 Window window = FRAME_X_WINDOW (f);
11412 #endif /* not USE_X_TOOLKIT */
11413
11414 /* Setting PMaxSize caused various problems. */
11415 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11416
11417 size_hints.x = f->output_data.x->left_pos;
11418 size_hints.y = f->output_data.x->top_pos;
11419
11420 #ifdef USE_X_TOOLKIT
11421 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11422 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
11423 XtGetValues (f->output_data.x->widget, al, ac);
11424 size_hints.height = widget_height;
11425 size_hints.width = widget_width;
11426 #else /* not USE_X_TOOLKIT */
11427 size_hints.height = PIXEL_HEIGHT (f);
11428 size_hints.width = PIXEL_WIDTH (f);
11429 #endif /* not USE_X_TOOLKIT */
11430
11431 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11432 size_hints.height_inc = f->output_data.x->line_height;
11433 size_hints.max_width
11434 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11435 size_hints.max_height
11436 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
11437
11438 /* Calculate the base and minimum sizes.
11439
11440 (When we use the X toolkit, we don't do it here.
11441 Instead we copy the values that the widgets are using, below.) */
11442 #ifndef USE_X_TOOLKIT
11443 {
11444 int base_width, base_height;
11445 int min_rows = 0, min_cols = 0;
11446
11447 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11448 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
11449
11450 check_frame_size (f, &min_rows, &min_cols);
11451
11452 /* The window manager uses the base width hints to calculate the
11453 current number of rows and columns in the frame while
11454 resizing; min_width and min_height aren't useful for this
11455 purpose, since they might not give the dimensions for a
11456 zero-row, zero-column frame.
11457
11458 We use the base_width and base_height members if we have
11459 them; otherwise, we set the min_width and min_height members
11460 to the size for a zero x zero frame. */
11461
11462 #ifdef HAVE_X11R4
11463 size_hints.flags |= PBaseSize;
11464 size_hints.base_width = base_width;
11465 size_hints.base_height = base_height;
11466 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11467 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
11468 #else
11469 size_hints.min_width = base_width;
11470 size_hints.min_height = base_height;
11471 #endif
11472 }
11473
11474 /* If we don't need the old flags, we don't need the old hint at all. */
11475 if (flags)
11476 {
11477 size_hints.flags |= flags;
11478 goto no_read;
11479 }
11480 #endif /* not USE_X_TOOLKIT */
11481
11482 {
11483 XSizeHints hints; /* Sometimes I hate X Windows... */
11484 long supplied_return;
11485 int value;
11486
11487 #ifdef HAVE_X11R4
11488 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11489 &supplied_return);
11490 #else
11491 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
11492 #endif
11493
11494 #ifdef USE_X_TOOLKIT
11495 size_hints.base_height = hints.base_height;
11496 size_hints.base_width = hints.base_width;
11497 size_hints.min_height = hints.min_height;
11498 size_hints.min_width = hints.min_width;
11499 #endif
11500
11501 if (flags)
11502 size_hints.flags |= flags;
11503 else
11504 {
11505 if (value == 0)
11506 hints.flags = 0;
11507 if (hints.flags & PSize)
11508 size_hints.flags |= PSize;
11509 if (hints.flags & PPosition)
11510 size_hints.flags |= PPosition;
11511 if (hints.flags & USPosition)
11512 size_hints.flags |= USPosition;
11513 if (hints.flags & USSize)
11514 size_hints.flags |= USSize;
11515 }
11516 }
11517
11518 #ifndef USE_X_TOOLKIT
11519 no_read:
11520 #endif
11521
11522 #ifdef PWinGravity
11523 size_hints.win_gravity = f->output_data.x->win_gravity;
11524 size_hints.flags |= PWinGravity;
11525
11526 if (user_position)
11527 {
11528 size_hints.flags &= ~ PPosition;
11529 size_hints.flags |= USPosition;
11530 }
11531 #endif /* PWinGravity */
11532
11533 #ifdef HAVE_X11R4
11534 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11535 #else
11536 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11537 #endif
11538 }
11539
11540 /* Used for IconicState or NormalState */
11541
11542 void
11543 x_wm_set_window_state (f, state)
11544 struct frame *f;
11545 int state;
11546 {
11547 #ifdef USE_X_TOOLKIT
11548 Arg al[1];
11549
11550 XtSetArg (al[0], XtNinitialState, state);
11551 XtSetValues (f->output_data.x->widget, al, 1);
11552 #else /* not USE_X_TOOLKIT */
11553 Window window = FRAME_X_WINDOW (f);
11554
11555 f->output_data.x->wm_hints.flags |= StateHint;
11556 f->output_data.x->wm_hints.initial_state = state;
11557
11558 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11559 #endif /* not USE_X_TOOLKIT */
11560 }
11561
11562 void
11563 x_wm_set_icon_pixmap (f, pixmap_id)
11564 struct frame *f;
11565 int pixmap_id;
11566 {
11567 Pixmap icon_pixmap;
11568
11569 #ifndef USE_X_TOOLKIT
11570 Window window = FRAME_X_WINDOW (f);
11571 #endif
11572
11573 if (pixmap_id > 0)
11574 {
11575 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11576 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11577 }
11578 else
11579 {
11580 /* It seems there is no way to turn off use of an icon pixmap.
11581 The following line does it, only if no icon has yet been created,
11582 for some window managers. But with mwm it crashes.
11583 Some people say it should clear the IconPixmapHint bit in this case,
11584 but that doesn't work, and the X consortium said it isn't the
11585 right thing at all. Since there is no way to win,
11586 best to explicitly give up. */
11587 #if 0
11588 f->output_data.x->wm_hints.icon_pixmap = None;
11589 #else
11590 return;
11591 #endif
11592 }
11593
11594 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11595
11596 {
11597 Arg al[1];
11598 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11599 XtSetValues (f->output_data.x->widget, al, 1);
11600 }
11601
11602 #else /* not USE_X_TOOLKIT */
11603
11604 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11605 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11606
11607 #endif /* not USE_X_TOOLKIT */
11608 }
11609
11610 void
11611 x_wm_set_icon_position (f, icon_x, icon_y)
11612 struct frame *f;
11613 int icon_x, icon_y;
11614 {
11615 #ifdef USE_X_TOOLKIT
11616 Window window = XtWindow (f->output_data.x->widget);
11617 #else
11618 Window window = FRAME_X_WINDOW (f);
11619 #endif
11620
11621 f->output_data.x->wm_hints.flags |= IconPositionHint;
11622 f->output_data.x->wm_hints.icon_x = icon_x;
11623 f->output_data.x->wm_hints.icon_y = icon_y;
11624
11625 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11626 }
11627
11628 \f
11629 /***********************************************************************
11630 Fonts
11631 ***********************************************************************/
11632
11633 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
11634
11635 struct font_info *
11636 x_get_font_info (f, font_idx)
11637 FRAME_PTR f;
11638 int font_idx;
11639 {
11640 return (FRAME_X_FONT_TABLE (f) + font_idx);
11641 }
11642
11643
11644 /* Return a list of names of available fonts matching PATTERN on frame
11645 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11646 to be listed. Frame F NULL means we have not yet created any
11647 frame on X, and consult the first display in x_display_list.
11648 MAXNAMES sets a limit on how many fonts to match. */
11649
11650 Lisp_Object
11651 x_list_fonts (f, pattern, size, maxnames)
11652 FRAME_PTR f;
11653 Lisp_Object pattern;
11654 int size;
11655 int maxnames;
11656 {
11657 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11658 Lisp_Object tem, second_best;
11659 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
11660 int try_XLoadQueryFont = 0;
11661 int count;
11662
11663 patterns = Fassoc (pattern, Valternate_fontname_alist);
11664 if (NILP (patterns))
11665 patterns = Fcons (pattern, Qnil);
11666
11667 if (maxnames == 1 && !size)
11668 /* We can return any single font matching PATTERN. */
11669 try_XLoadQueryFont = 1;
11670
11671 for (; CONSP (patterns); patterns = XCONS (patterns)->cdr)
11672 {
11673 int num_fonts;
11674 char **names;
11675
11676 pattern = XCONS (patterns)->car;
11677 /* See if we cached the result for this particular query.
11678 The cache is an alist of the form:
11679 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11680 */
11681 if (f && (tem = XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr,
11682 key = Fcons (pattern, make_number (maxnames)),
11683 !NILP (list = Fassoc (key, tem))))
11684 {
11685 list = Fcdr_safe (list);
11686 /* We have a cashed list. Don't have to get the list again. */
11687 goto label_cached;
11688 }
11689
11690 /* At first, put PATTERN in the cache. */
11691
11692 BLOCK_INPUT;
11693 count = x_catch_errors (dpy);
11694
11695 if (try_XLoadQueryFont)
11696 {
11697 XFontStruct *font;
11698 unsigned long value;
11699
11700 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
11701 if (x_had_errors_p (dpy))
11702 {
11703 /* This error is perhaps due to insufficient memory on X
11704 server. Let's just ignore it. */
11705 font = NULL;
11706 x_clear_errors (dpy);
11707 }
11708
11709 if (font
11710 && XGetFontProperty (font, XA_FONT, &value))
11711 {
11712 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11713 int len = strlen (name);
11714 char *tmp;
11715
11716 /* If DXPC (a Differential X Protocol Compressor)
11717 Ver.3.7 is running, XGetAtomName will return null
11718 string. We must avoid such a name. */
11719 if (len == 0)
11720 try_XLoadQueryFont = 0;
11721 else
11722 {
11723 num_fonts = 1;
11724 names = (char **) alloca (sizeof (char *));
11725 /* Some systems only allow alloca assigned to a
11726 simple var. */
11727 tmp = (char *) alloca (len + 1); names[0] = tmp;
11728 bcopy (name, names[0], len + 1);
11729 XFree (name);
11730 }
11731 }
11732 else
11733 try_XLoadQueryFont = 0;
11734
11735 if (font)
11736 XFreeFont (dpy, font);
11737 }
11738
11739 if (!try_XLoadQueryFont)
11740 {
11741 /* We try at least 10 fonts because XListFonts will return
11742 auto-scaled fonts at the head. */
11743 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
11744 &num_fonts);
11745 if (x_had_errors_p (dpy))
11746 {
11747 /* This error is perhaps due to insufficient memory on X
11748 server. Let's just ignore it. */
11749 names = NULL;
11750 x_clear_errors (dpy);
11751 }
11752 }
11753
11754 x_uncatch_errors (dpy, count);
11755 UNBLOCK_INPUT;
11756
11757 if (names)
11758 {
11759 int i;
11760
11761 /* Make a list of all the fonts we got back.
11762 Store that in the font cache for the display. */
11763 for (i = 0; i < num_fonts; i++)
11764 {
11765 int width = 0;
11766 char *p = names[i];
11767 int average_width = -1, dashes = 0;
11768
11769 /* Count the number of dashes in NAMES[I]. If there are
11770 14 dashes, and the field value following 12th dash
11771 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
11772 is usually too ugly to be used for editing. Let's
11773 ignore it. */
11774 while (*p)
11775 if (*p++ == '-')
11776 {
11777 dashes++;
11778 if (dashes == 7) /* PIXEL_SIZE field */
11779 width = atoi (p);
11780 else if (dashes == 12) /* AVERAGE_WIDTH field */
11781 average_width = atoi (p);
11782 }
11783 if (dashes < 14 || average_width != 0)
11784 {
11785 tem = build_string (names[i]);
11786 if (NILP (Fassoc (tem, list)))
11787 {
11788 if (STRINGP (Vx_pixel_size_width_font_regexp)
11789 && ((fast_c_string_match_ignore_case
11790 (Vx_pixel_size_width_font_regexp, names[i]))
11791 >= 0))
11792 /* We can set the value of PIXEL_SIZE to the
11793 width of this font. */
11794 list = Fcons (Fcons (tem, make_number (width)), list);
11795 else
11796 /* For the moment, width is not known. */
11797 list = Fcons (Fcons (tem, Qnil), list);
11798 }
11799 }
11800 }
11801 if (!try_XLoadQueryFont)
11802 XFreeFontNames (names);
11803 }
11804
11805 /* Now store the result in the cache. */
11806 if (f != NULL)
11807 XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr
11808 = Fcons (Fcons (key, list),
11809 XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr);
11810
11811 label_cached:
11812 if (NILP (list)) continue; /* Try the remaining alternatives. */
11813
11814 newlist = second_best = Qnil;
11815 /* Make a list of the fonts that have the right width. */
11816 for (; CONSP (list); list = XCONS (list)->cdr)
11817 {
11818 int found_size;
11819
11820 tem = XCONS (list)->car;
11821
11822 if (!CONSP (tem) || NILP (XCONS (tem)->car))
11823 continue;
11824 if (!size)
11825 {
11826 newlist = Fcons (XCONS (tem)->car, newlist);
11827 continue;
11828 }
11829
11830 if (!INTEGERP (XCONS (tem)->cdr))
11831 {
11832 /* Since we have not yet known the size of this font, we
11833 must try slow function call XLoadQueryFont. */
11834 XFontStruct *thisinfo;
11835
11836 BLOCK_INPUT;
11837 count = x_catch_errors (dpy);
11838 thisinfo = XLoadQueryFont (dpy,
11839 XSTRING (XCONS (tem)->car)->data);
11840 if (x_had_errors_p (dpy))
11841 {
11842 /* This error is perhaps due to insufficient memory on X
11843 server. Let's just ignore it. */
11844 thisinfo = NULL;
11845 x_clear_errors (dpy);
11846 }
11847 x_uncatch_errors (dpy, count);
11848 UNBLOCK_INPUT;
11849
11850 if (thisinfo)
11851 {
11852 XCONS (tem)->cdr
11853 = (thisinfo->min_bounds.width == 0
11854 ? make_number (0)
11855 : make_number (thisinfo->max_bounds.width));
11856 XFreeFont (dpy, thisinfo);
11857 }
11858 else
11859 /* For unknown reason, the previous call of XListFont had
11860 returned a font which can't be opened. Record the size
11861 as 0 not to try to open it again. */
11862 XCONS (tem)->cdr = make_number (0);
11863 }
11864
11865 found_size = XINT (XCONS (tem)->cdr);
11866 if (found_size == size)
11867 newlist = Fcons (XCONS (tem)->car, newlist);
11868 else if (found_size > 0)
11869 {
11870 if (NILP (second_best))
11871 second_best = tem;
11872 else if (found_size < size)
11873 {
11874 if (XINT (XCONS (second_best)->cdr) > size
11875 || XINT (XCONS (second_best)->cdr) < found_size)
11876 second_best = tem;
11877 }
11878 else
11879 {
11880 if (XINT (XCONS (second_best)->cdr) > size
11881 && XINT (XCONS (second_best)->cdr) > found_size)
11882 second_best = tem;
11883 }
11884 }
11885 }
11886 if (!NILP (newlist))
11887 break;
11888 else if (!NILP (second_best))
11889 {
11890 newlist = Fcons (XCONS (second_best)->car, Qnil);
11891 break;
11892 }
11893 }
11894
11895 return newlist;
11896 }
11897
11898
11899 #if GLYPH_DEBUG
11900
11901 /* Check that FONT is valid on frame F. It is if it can be found in F's
11902 font table. */
11903
11904 static void
11905 x_check_font (f, font)
11906 struct frame *f;
11907 XFontStruct *font;
11908 {
11909 int i;
11910 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11911
11912 xassert (font != NULL);
11913
11914 for (i = 0; i < dpyinfo->n_fonts; i++)
11915 if (dpyinfo->font_table[i].name
11916 && font == dpyinfo->font_table[i].font)
11917 break;
11918
11919 xassert (i < dpyinfo->n_fonts);
11920 }
11921
11922 #endif /* GLYPH_DEBUG != 0 */
11923
11924 /* Set *W to the minimum width, *H to the minimum font height of FONT.
11925 Note: There are (broken) X fonts out there with invalid XFontStruct
11926 min_bounds contents. For example, handa@etl.go.jp reports that
11927 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
11928 have font->min_bounds.width == 0. */
11929
11930 static INLINE void
11931 x_font_min_bounds (font, w, h)
11932 XFontStruct *font;
11933 int *w, *h;
11934 {
11935 *h = FONT_HEIGHT (font);
11936 *w = font->min_bounds.width;
11937
11938 /* Try to handle the case where FONT->min_bounds has invalid
11939 contents. Since the only font known to have invalid min_bounds
11940 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
11941 if (*w <= 0)
11942 *w = font->max_bounds.width;
11943 }
11944
11945
11946 /* Compute the smallest character width and smallest font height over
11947 all fonts available on frame F. Set the members smallest_char_width
11948 and smallest_font_height in F's x_display_info structure to
11949 the values computed. Value is non-zero if smallest_font_height or
11950 smallest_char_width become smaller than they were before. */
11951
11952 static int
11953 x_compute_min_glyph_bounds (f)
11954 struct frame *f;
11955 {
11956 int i;
11957 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11958 XFontStruct *font;
11959 int old_width = dpyinfo->smallest_char_width;
11960 int old_height = dpyinfo->smallest_font_height;
11961
11962 dpyinfo->smallest_font_height = 100000;
11963 dpyinfo->smallest_char_width = 100000;
11964
11965 for (i = 0; i < dpyinfo->n_fonts; ++i)
11966 if (dpyinfo->font_table[i].name)
11967 {
11968 struct font_info *fontp = dpyinfo->font_table + i;
11969 int w, h;
11970
11971 font = (XFontStruct *) fontp->font;
11972 xassert (font != (XFontStruct *) ~0);
11973 x_font_min_bounds (font, &w, &h);
11974
11975 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
11976 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
11977 }
11978
11979 xassert (dpyinfo->smallest_char_width > 0
11980 && dpyinfo->smallest_font_height > 0);
11981
11982 return (dpyinfo->n_fonts == 1
11983 || dpyinfo->smallest_char_width < old_width
11984 || dpyinfo->smallest_font_height < old_height);
11985 }
11986
11987
11988 /* Load font named FONTNAME of the size SIZE for frame F, and return a
11989 pointer to the structure font_info while allocating it dynamically.
11990 If SIZE is 0, load any size of font.
11991 If loading is failed, return NULL. */
11992
11993 struct font_info *
11994 x_load_font (f, fontname, size)
11995 struct frame *f;
11996 register char *fontname;
11997 int size;
11998 {
11999 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12000 Lisp_Object font_names;
12001 int count;
12002
12003 /* Get a list of all the fonts that match this name. Once we
12004 have a list of matching fonts, we compare them against the fonts
12005 we already have by comparing names. */
12006 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12007
12008 if (!NILP (font_names))
12009 {
12010 Lisp_Object tail;
12011 int i;
12012
12013 for (i = 0; i < dpyinfo->n_fonts; i++)
12014 for (tail = font_names; CONSP (tail); tail = XCONS (tail)->cdr)
12015 if (dpyinfo->font_table[i].name
12016 && (!strcmp (dpyinfo->font_table[i].name,
12017 XSTRING (XCONS (tail)->car)->data)
12018 || !strcmp (dpyinfo->font_table[i].full_name,
12019 XSTRING (XCONS (tail)->car)->data)))
12020 return (dpyinfo->font_table + i);
12021 }
12022
12023 /* Load the font and add it to the table. */
12024 {
12025 char *full_name;
12026 XFontStruct *font;
12027 struct font_info *fontp;
12028 unsigned long value;
12029 int i;
12030
12031 /* If we have found fonts by x_list_font, load one of them. If
12032 not, we still try to load a font by the name given as FONTNAME
12033 because XListFonts (called in x_list_font) of some X server has
12034 a bug of not finding a font even if the font surely exists and
12035 is loadable by XLoadQueryFont. */
12036 if (size > 0 && !NILP (font_names))
12037 fontname = (char *) XSTRING (XCONS (font_names)->car)->data;
12038
12039 BLOCK_INPUT;
12040 count = x_catch_errors (FRAME_X_DISPLAY (f));
12041 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12042 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12043 {
12044 /* This error is perhaps due to insufficient memory on X
12045 server. Let's just ignore it. */
12046 font = NULL;
12047 x_clear_errors (FRAME_X_DISPLAY (f));
12048 }
12049 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12050 UNBLOCK_INPUT;
12051 if (!font)
12052 return NULL;
12053
12054 /* Find a free slot in the font table. */
12055 for (i = 0; i < dpyinfo->n_fonts; ++i)
12056 if (dpyinfo->font_table[i].name == NULL)
12057 break;
12058
12059 /* If no free slot found, maybe enlarge the font table. */
12060 if (i == dpyinfo->n_fonts
12061 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12062 {
12063 int sz;
12064 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12065 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12066 dpyinfo->font_table
12067 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12068 }
12069
12070 fontp = dpyinfo->font_table + i;
12071 if (i == dpyinfo->n_fonts)
12072 ++dpyinfo->n_fonts;
12073
12074 /* Now fill in the slots of *FONTP. */
12075 BLOCK_INPUT;
12076 fontp->font = font;
12077 fontp->font_idx = i;
12078 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12079 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12080
12081 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12082 full_name = 0;
12083 if (XGetFontProperty (font, XA_FONT, &value))
12084 {
12085 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12086 char *p = name;
12087 int dashes = 0;
12088
12089 /* Count the number of dashes in the "full name".
12090 If it is too few, this isn't really the font's full name,
12091 so don't use it.
12092 In X11R4, the fonts did not come with their canonical names
12093 stored in them. */
12094 while (*p)
12095 {
12096 if (*p == '-')
12097 dashes++;
12098 p++;
12099 }
12100
12101 if (dashes >= 13)
12102 {
12103 full_name = (char *) xmalloc (p - name + 1);
12104 bcopy (name, full_name, p - name + 1);
12105 }
12106
12107 XFree (name);
12108 }
12109
12110 if (full_name != 0)
12111 fontp->full_name = full_name;
12112 else
12113 fontp->full_name = fontp->name;
12114
12115 fontp->size = font->max_bounds.width;
12116 fontp->height = FONT_HEIGHT (font);
12117 {
12118 /* For some font, ascent and descent in max_bounds field is
12119 larger than the above value. */
12120 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12121 if (max_height > fontp->height)
12122 fontp->height = max_height;
12123 }
12124
12125 if (NILP (font_names))
12126 {
12127 /* We come here because of a bug of XListFonts mentioned at
12128 the head of this block. Let's store this information in
12129 the cache for x_list_fonts. */
12130 Lisp_Object lispy_name = build_string (fontname);
12131 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12132
12133 XCONS (dpyinfo->name_list_element)->cdr
12134 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12135 Fcons (Fcons (lispy_full_name,
12136 make_number (fontp->size)),
12137 Qnil)),
12138 XCONS (dpyinfo->name_list_element)->cdr);
12139 if (full_name)
12140 XCONS (dpyinfo->name_list_element)->cdr
12141 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12142 Fcons (Fcons (lispy_full_name,
12143 make_number (fontp->size)),
12144 Qnil)),
12145 XCONS (dpyinfo->name_list_element)->cdr);
12146 }
12147
12148 /* The slot `encoding' specifies how to map a character
12149 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12150 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
12151 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12152 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12153 2:0xA020..0xFF7F). For the moment, we don't know which charset
12154 uses this font. So, we set information in fontp->encoding[1]
12155 which is never used by any charset. If mapping can't be
12156 decided, set FONT_ENCODING_NOT_DECIDED. */
12157 fontp->encoding[1]
12158 = (font->max_byte1 == 0
12159 /* 1-byte font */
12160 ? (font->min_char_or_byte2 < 0x80
12161 ? (font->max_char_or_byte2 < 0x80
12162 ? 0 /* 0x20..0x7F */
12163 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12164 : 1) /* 0xA0..0xFF */
12165 /* 2-byte font */
12166 : (font->min_byte1 < 0x80
12167 ? (font->max_byte1 < 0x80
12168 ? (font->min_char_or_byte2 < 0x80
12169 ? (font->max_char_or_byte2 < 0x80
12170 ? 0 /* 0x2020..0x7F7F */
12171 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
12172 : 3) /* 0x20A0..0x7FFF */
12173 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
12174 : (font->min_char_or_byte2 < 0x80
12175 ? (font->max_char_or_byte2 < 0x80
12176 ? 2 /* 0xA020..0xFF7F */
12177 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
12178 : 1))); /* 0xA0A0..0xFFFF */
12179
12180 fontp->baseline_offset
12181 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12182 ? (long) value : 0);
12183 fontp->relative_compose
12184 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12185 ? (long) value : 0);
12186 fontp->default_ascent
12187 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12188 ? (long) value : 0);
12189
12190 /* Set global flag fonts_changed_p to non-zero if the font loaded
12191 has a character with a smaller width than any other character
12192 before, or if the font loaded has a smalle>r height than any
12193 other font loaded before. If this happens, it will make a
12194 glyph matrix reallocation necessary. */
12195 fonts_changed_p = x_compute_min_glyph_bounds (f);
12196 UNBLOCK_INPUT;
12197 return fontp;
12198 }
12199 }
12200
12201
12202 /* Return a pointer to struct font_info of a font named FONTNAME for
12203 frame F. If no such font is loaded, return NULL. */
12204
12205 struct font_info *
12206 x_query_font (f, fontname)
12207 struct frame *f;
12208 register char *fontname;
12209 {
12210 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12211 int i;
12212
12213 for (i = 0; i < dpyinfo->n_fonts; i++)
12214 if (dpyinfo->font_table[i].name
12215 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12216 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
12217 return (dpyinfo->font_table + i);
12218 return NULL;
12219 }
12220
12221
12222 /* Find a CCL program for a font specified by FONTP, and set the member
12223 `encoder' of the structure. */
12224
12225 void
12226 x_find_ccl_program (fontp)
12227 struct font_info *fontp;
12228 {
12229 Lisp_Object list, elt;
12230
12231 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCONS (list)->cdr)
12232 {
12233 elt = XCONS (list)->car;
12234 if (CONSP (elt)
12235 && STRINGP (XCONS (elt)->car)
12236 && (fast_c_string_match_ignore_case (XCONS (elt)->car, fontp->name)
12237 >= 0))
12238 break;
12239 }
12240 if (! NILP (list))
12241 {
12242 struct ccl_program *ccl
12243 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
12244
12245 if (setup_ccl_program (ccl, XCONS (elt)->cdr) < 0)
12246 xfree (ccl);
12247 else
12248 fontp->font_encoder = ccl;
12249 }
12250 }
12251
12252
12253 \f
12254 /***********************************************************************
12255 Initialization
12256 ***********************************************************************/
12257
12258 #ifdef USE_X_TOOLKIT
12259 static XrmOptionDescRec emacs_options[] = {
12260 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12261 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12262
12263 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12264 XrmoptionSepArg, NULL},
12265 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12266
12267 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12268 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12269 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12270 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12271 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12272 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12273 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12274 };
12275 #endif /* USE_X_TOOLKIT */
12276
12277 static int x_initialized;
12278
12279 #ifdef MULTI_KBOARD
12280 /* Test whether two display-name strings agree up to the dot that separates
12281 the screen number from the server number. */
12282 static int
12283 same_x_server (name1, name2)
12284 char *name1, *name2;
12285 {
12286 int seen_colon = 0;
12287 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12288 int system_name_length = strlen (system_name);
12289 int length_until_period = 0;
12290
12291 while (system_name[length_until_period] != 0
12292 && system_name[length_until_period] != '.')
12293 length_until_period++;
12294
12295 /* Treat `unix' like an empty host name. */
12296 if (! strncmp (name1, "unix:", 5))
12297 name1 += 4;
12298 if (! strncmp (name2, "unix:", 5))
12299 name2 += 4;
12300 /* Treat this host's name like an empty host name. */
12301 if (! strncmp (name1, system_name, system_name_length)
12302 && name1[system_name_length] == ':')
12303 name1 += system_name_length;
12304 if (! strncmp (name2, system_name, system_name_length)
12305 && name2[system_name_length] == ':')
12306 name2 += system_name_length;
12307 /* Treat this host's domainless name like an empty host name. */
12308 if (! strncmp (name1, system_name, length_until_period)
12309 && name1[length_until_period] == ':')
12310 name1 += length_until_period;
12311 if (! strncmp (name2, system_name, length_until_period)
12312 && name2[length_until_period] == ':')
12313 name2 += length_until_period;
12314
12315 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12316 {
12317 if (*name1 == ':')
12318 seen_colon++;
12319 if (seen_colon && *name1 == '.')
12320 return 1;
12321 }
12322 return (seen_colon
12323 && (*name1 == '.' || *name1 == '\0')
12324 && (*name2 == '.' || *name2 == '\0'));
12325 }
12326 #endif
12327
12328 #if defined (HAVE_X_I18N) || (defined (USE_X_TOOLKIT) && defined (HAVE_X11XTR6))
12329 /* Recover from setlocale (LC_ALL, ""). */
12330 static void
12331 fixup_locale ()
12332 {
12333 /* Currently we require strerror to use the "C" locale,
12334 since we don't yet support decoding its string result. */
12335 #ifdef LC_MESSAGES
12336 setlocale (LC_MESSAGES, "C");
12337 #endif
12338
12339 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
12340 so that numbers are read and printed properly for Emacs Lisp. */
12341 setlocale (LC_NUMERIC, "C");
12342
12343 /* Currently we require strftime to use the "C" locale,
12344 since we don't yet support encoding its format argument,
12345 or decoding its string result. */
12346 setlocale (LC_TIME, "C");
12347 }
12348 #endif
12349
12350 struct x_display_info *
12351 x_term_init (display_name, xrm_option, resource_name)
12352 Lisp_Object display_name;
12353 char *xrm_option;
12354 char *resource_name;
12355 {
12356 int connection;
12357 Display *dpy;
12358 struct x_display_info *dpyinfo;
12359 XrmDatabase xrdb;
12360
12361 BLOCK_INPUT;
12362
12363 if (!x_initialized)
12364 {
12365 x_initialize ();
12366 x_initialized = 1;
12367 }
12368
12369 #ifdef HAVE_X_I18N
12370 setlocale (LC_ALL, "");
12371 fixup_locale ();
12372 #endif
12373
12374 #ifdef USE_X_TOOLKIT
12375 /* weiner@footloose.sps.mot.com reports that this causes
12376 errors with X11R5:
12377 X protocol error: BadAtom (invalid Atom parameter)
12378 on protocol request 18skiloaf.
12379 So let's not use it until R6. */
12380 #ifdef HAVE_X11XTR6
12381 XtSetLanguageProc (NULL, NULL, NULL);
12382 #endif
12383
12384 {
12385 int argc = 0;
12386 char *argv[3];
12387
12388 argv[0] = "";
12389 argc = 1;
12390 if (xrm_option)
12391 {
12392 argv[argc++] = "-xrm";
12393 argv[argc++] = xrm_option;
12394 }
12395 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12396 resource_name, EMACS_CLASS,
12397 emacs_options, XtNumber (emacs_options),
12398 &argc, argv);
12399
12400 #ifdef HAVE_X11XTR6
12401 /* I think this is to compensate for XtSetLanguageProc. */
12402 fixup_locale ();
12403 #endif
12404 }
12405
12406 #else /* not USE_X_TOOLKIT */
12407 #ifdef HAVE_X11R5
12408 XSetLocaleModifiers ("");
12409 #endif
12410 dpy = XOpenDisplay (XSTRING (display_name)->data);
12411 #endif /* not USE_X_TOOLKIT */
12412
12413 /* Detect failure. */
12414 if (dpy == 0)
12415 {
12416 UNBLOCK_INPUT;
12417 return 0;
12418 }
12419
12420 /* We have definitely succeeded. Record the new connection. */
12421
12422 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12423
12424 #ifdef MULTI_KBOARD
12425 {
12426 struct x_display_info *share;
12427 Lisp_Object tail;
12428
12429 for (share = x_display_list, tail = x_display_name_list; share;
12430 share = share->next, tail = XCONS (tail)->cdr)
12431 if (same_x_server (XSTRING (XCONS (XCONS (tail)->car)->car)->data,
12432 XSTRING (display_name)->data))
12433 break;
12434 if (share)
12435 dpyinfo->kboard = share->kboard;
12436 else
12437 {
12438 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12439 init_kboard (dpyinfo->kboard);
12440 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12441 {
12442 char *vendor = ServerVendor (dpy);
12443 dpyinfo->kboard->Vsystem_key_alist
12444 = call1 (Qvendor_specific_keysyms,
12445 build_string (vendor ? vendor : ""));
12446 }
12447
12448 dpyinfo->kboard->next_kboard = all_kboards;
12449 all_kboards = dpyinfo->kboard;
12450 /* Don't let the initial kboard remain current longer than necessary.
12451 That would cause problems if a file loaded on startup tries to
12452 prompt in the mini-buffer. */
12453 if (current_kboard == initial_kboard)
12454 current_kboard = dpyinfo->kboard;
12455 }
12456 dpyinfo->kboard->reference_count++;
12457 }
12458 #endif
12459
12460 /* Put this display on the chain. */
12461 dpyinfo->next = x_display_list;
12462 x_display_list = dpyinfo;
12463
12464 /* Put it on x_display_name_list as well, to keep them parallel. */
12465 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12466 x_display_name_list);
12467 dpyinfo->name_list_element = XCONS (x_display_name_list)->car;
12468
12469 dpyinfo->display = dpy;
12470
12471 #if 0
12472 XSetAfterFunction (x_current_display, x_trace_wire);
12473 #endif /* ! 0 */
12474
12475 dpyinfo->x_id_name
12476 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12477 + STRING_BYTES (XSTRING (Vsystem_name))
12478 + 2);
12479 sprintf (dpyinfo->x_id_name, "%s@%s",
12480 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
12481
12482 /* Figure out which modifier bits mean what. */
12483 x_find_modifier_meanings (dpyinfo);
12484
12485 /* Get the scroll bar cursor. */
12486 dpyinfo->vertical_scroll_bar_cursor
12487 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12488
12489 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12490 resource_name, EMACS_CLASS);
12491 #ifdef HAVE_XRMSETDATABASE
12492 XrmSetDatabase (dpyinfo->display, xrdb);
12493 #else
12494 dpyinfo->display->db = xrdb;
12495 #endif
12496 /* Put the rdb where we can find it in a way that works on
12497 all versions. */
12498 dpyinfo->xrdb = xrdb;
12499
12500 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12501 DefaultScreen (dpyinfo->display));
12502 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12503 &dpyinfo->n_planes);
12504 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12505 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12506 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12507 dpyinfo->grabbed = 0;
12508 dpyinfo->reference_count = 0;
12509 dpyinfo->icon_bitmap_id = -1;
12510 dpyinfo->font_table = NULL;
12511 dpyinfo->n_fonts = 0;
12512 dpyinfo->font_table_size = 0;
12513 dpyinfo->bitmaps = 0;
12514 dpyinfo->bitmaps_size = 0;
12515 dpyinfo->bitmaps_last = 0;
12516 dpyinfo->scratch_cursor_gc = 0;
12517 dpyinfo->mouse_face_mouse_frame = 0;
12518 dpyinfo->mouse_face_deferred_gc = 0;
12519 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12520 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
12521 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
12522 dpyinfo->mouse_face_window = Qnil;
12523 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12524 dpyinfo->mouse_face_defer = 0;
12525 dpyinfo->x_focus_frame = 0;
12526 dpyinfo->x_focus_event_frame = 0;
12527 dpyinfo->x_highlight_frame = 0;
12528 dpyinfo->image_cache = make_image_cache ();
12529
12530 {
12531 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12532 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12533 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12534 dpyinfo->resy = pixels * 25.4 / mm;
12535 pixels = DisplayWidth (dpyinfo->display, screen_number);
12536 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12537 dpyinfo->resx = pixels * 25.4 / mm;
12538 }
12539
12540 dpyinfo->Xatom_wm_protocols
12541 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12542 dpyinfo->Xatom_wm_take_focus
12543 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12544 dpyinfo->Xatom_wm_save_yourself
12545 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12546 dpyinfo->Xatom_wm_delete_window
12547 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12548 dpyinfo->Xatom_wm_change_state
12549 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12550 dpyinfo->Xatom_wm_configure_denied
12551 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12552 dpyinfo->Xatom_wm_window_moved
12553 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12554 dpyinfo->Xatom_editres
12555 = XInternAtom (dpyinfo->display, "Editres", False);
12556 dpyinfo->Xatom_CLIPBOARD
12557 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12558 dpyinfo->Xatom_TIMESTAMP
12559 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12560 dpyinfo->Xatom_TEXT
12561 = XInternAtom (dpyinfo->display, "TEXT", False);
12562 dpyinfo->Xatom_COMPOUND_TEXT
12563 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
12564 dpyinfo->Xatom_DELETE
12565 = XInternAtom (dpyinfo->display, "DELETE", False);
12566 dpyinfo->Xatom_MULTIPLE
12567 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12568 dpyinfo->Xatom_INCR
12569 = XInternAtom (dpyinfo->display, "INCR", False);
12570 dpyinfo->Xatom_EMACS_TMP
12571 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12572 dpyinfo->Xatom_TARGETS
12573 = XInternAtom (dpyinfo->display, "TARGETS", False);
12574 dpyinfo->Xatom_NULL
12575 = XInternAtom (dpyinfo->display, "NULL", False);
12576 dpyinfo->Xatom_ATOM_PAIR
12577 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
12578 /* For properties of font. */
12579 dpyinfo->Xatom_PIXEL_SIZE
12580 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12581 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12582 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12583 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12584 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
12585 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12586 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
12587
12588 /* Ghostscript support. */
12589 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12590 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12591
12592 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12593 False);
12594
12595 dpyinfo->cut_buffers_initialized = 0;
12596
12597 connection = ConnectionNumber (dpyinfo->display);
12598 dpyinfo->connection = connection;
12599
12600 {
12601 char null_bits[1];
12602
12603 null_bits[0] = 0x00;
12604
12605 dpyinfo->null_pixel
12606 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12607 null_bits, 1, 1, (long) 0, (long) 0,
12608 1);
12609 }
12610
12611 {
12612 extern int gray_bitmap_width, gray_bitmap_height;
12613 extern unsigned char *gray_bitmap_bits;
12614 dpyinfo->gray
12615 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12616 gray_bitmap_bits,
12617 gray_bitmap_width, gray_bitmap_height,
12618 (unsigned long) 1, (unsigned long) 0, 1);
12619 }
12620
12621 #ifdef subprocesses
12622 /* This is only needed for distinguishing keyboard and process input. */
12623 if (connection != 0)
12624 add_keyboard_wait_descriptor (connection);
12625 #endif
12626
12627 #ifndef F_SETOWN_BUG
12628 #ifdef F_SETOWN
12629 #ifdef F_SETOWN_SOCK_NEG
12630 /* stdin is a socket here */
12631 fcntl (connection, F_SETOWN, -getpid ());
12632 #else /* ! defined (F_SETOWN_SOCK_NEG) */
12633 fcntl (connection, F_SETOWN, getpid ());
12634 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
12635 #endif /* ! defined (F_SETOWN) */
12636 #endif /* F_SETOWN_BUG */
12637
12638 #ifdef SIGIO
12639 if (interrupt_input)
12640 init_sigio (connection);
12641 #endif /* ! defined (SIGIO) */
12642
12643 #ifdef USE_LUCID
12644 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
12645 /* Make sure that we have a valid font for dialog boxes
12646 so that Xt does not crash. */
12647 {
12648 Display *dpy = dpyinfo->display;
12649 XrmValue d, fr, to;
12650 Font font;
12651 int count;
12652
12653 d.addr = (XPointer)&dpy;
12654 d.size = sizeof (Display *);
12655 fr.addr = XtDefaultFont;
12656 fr.size = sizeof (XtDefaultFont);
12657 to.size = sizeof (Font *);
12658 to.addr = (XPointer)&font;
12659 count = x_catch_errors (dpy);
12660 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12661 abort ();
12662 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12663 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12664 x_uncatch_errors (dpy, count);
12665 }
12666 #endif
12667 #endif
12668
12669 UNBLOCK_INPUT;
12670
12671 return dpyinfo;
12672 }
12673 \f
12674 /* Get rid of display DPYINFO, assuming all frames are already gone,
12675 and without sending any more commands to the X server. */
12676
12677 void
12678 x_delete_display (dpyinfo)
12679 struct x_display_info *dpyinfo;
12680 {
12681 delete_keyboard_wait_descriptor (dpyinfo->connection);
12682
12683 /* Discard this display from x_display_name_list and x_display_list.
12684 We can't use Fdelq because that can quit. */
12685 if (! NILP (x_display_name_list)
12686 && EQ (XCONS (x_display_name_list)->car, dpyinfo->name_list_element))
12687 x_display_name_list = XCONS (x_display_name_list)->cdr;
12688 else
12689 {
12690 Lisp_Object tail;
12691
12692 tail = x_display_name_list;
12693 while (CONSP (tail) && CONSP (XCONS (tail)->cdr))
12694 {
12695 if (EQ (XCONS (XCONS (tail)->cdr)->car,
12696 dpyinfo->name_list_element))
12697 {
12698 XCONS (tail)->cdr = XCONS (XCONS (tail)->cdr)->cdr;
12699 break;
12700 }
12701 tail = XCONS (tail)->cdr;
12702 }
12703 }
12704
12705 if (x_display_list == dpyinfo)
12706 x_display_list = dpyinfo->next;
12707 else
12708 {
12709 struct x_display_info *tail;
12710
12711 for (tail = x_display_list; tail; tail = tail->next)
12712 if (tail->next == dpyinfo)
12713 tail->next = tail->next->next;
12714 }
12715
12716 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12717 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
12718 XrmDestroyDatabase (dpyinfo->xrdb);
12719 #endif
12720 #endif
12721 #ifdef MULTI_KBOARD
12722 if (--dpyinfo->kboard->reference_count == 0)
12723 delete_kboard (dpyinfo->kboard);
12724 #endif
12725 xfree (dpyinfo->font_table);
12726 xfree (dpyinfo->x_id_name);
12727 xfree (dpyinfo);
12728 }
12729 \f
12730 /* Set up use of X before we make the first connection. */
12731
12732 static struct redisplay_interface x_redisplay_interface =
12733 {
12734 x_produce_glyphs,
12735 x_write_glyphs,
12736 x_insert_glyphs,
12737 x_clear_end_of_line,
12738 x_scroll_run,
12739 x_after_update_window_line,
12740 x_update_window_begin,
12741 x_update_window_end,
12742 XTcursor_to,
12743 x_flush,
12744 x_get_glyph_overhangs,
12745 x_fix_overlapping_area
12746 };
12747
12748 void
12749 x_initialize ()
12750 {
12751 rif = &x_redisplay_interface;
12752
12753 clear_frame_hook = x_clear_frame;
12754 ins_del_lines_hook = x_ins_del_lines;
12755 change_line_highlight_hook = x_change_line_highlight;
12756 delete_glyphs_hook = x_delete_glyphs;
12757 ring_bell_hook = XTring_bell;
12758 reset_terminal_modes_hook = XTreset_terminal_modes;
12759 set_terminal_modes_hook = XTset_terminal_modes;
12760 update_begin_hook = x_update_begin;
12761 update_end_hook = x_update_end;
12762 set_terminal_window_hook = XTset_terminal_window;
12763 read_socket_hook = XTread_socket;
12764 frame_up_to_date_hook = XTframe_up_to_date;
12765 reassert_line_highlight_hook = XTreassert_line_highlight;
12766 mouse_position_hook = XTmouse_position;
12767 frame_rehighlight_hook = XTframe_rehighlight;
12768 frame_raise_lower_hook = XTframe_raise_lower;
12769 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12770 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12771 redeem_scroll_bar_hook = XTredeem_scroll_bar;
12772 judge_scroll_bars_hook = XTjudge_scroll_bars;
12773 estimate_mode_line_height_hook = x_estimate_mode_line_height;
12774
12775 scroll_region_ok = 1; /* we'll scroll partial frames */
12776 char_ins_del_ok = 0; /* just as fast to write the line */
12777 line_ins_del_ok = 1; /* we'll just blt 'em */
12778 fast_clear_end_of_line = 1; /* X does this well */
12779 memory_below_frame = 0; /* we don't remember what scrolls
12780 off the bottom */
12781 baud_rate = 19200;
12782
12783 x_noop_count = 0;
12784 last_toolbar_item = -1;
12785 any_help_event_p = 0;
12786
12787 /* Try to use interrupt input; if we can't, then start polling. */
12788 Fset_input_mode (Qt, Qnil, Qt, Qnil);
12789
12790 #ifdef USE_X_TOOLKIT
12791 XtToolkitInitialize ();
12792 Xt_app_con = XtCreateApplicationContext ();
12793 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12794 #endif
12795
12796 /* Note that there is no real way portable across R3/R4 to get the
12797 original error handler. */
12798 XSetErrorHandler (x_error_handler);
12799 XSetIOErrorHandler (x_io_error_quitter);
12800
12801 /* Disable Window Change signals; they are handled by X events. */
12802 #ifdef SIGWINCH
12803 signal (SIGWINCH, SIG_DFL);
12804 #endif /* ! defined (SIGWINCH) */
12805
12806 signal (SIGPIPE, x_connection_signal);
12807 }
12808
12809
12810 void
12811 syms_of_xterm ()
12812 {
12813 staticpro (&x_error_message_string);
12814 x_error_message_string = Qnil;
12815
12816 staticpro (&x_display_name_list);
12817 x_display_name_list = Qnil;
12818
12819 staticpro (&last_mouse_scroll_bar);
12820 last_mouse_scroll_bar = Qnil;
12821
12822 staticpro (&Qvendor_specific_keysyms);
12823 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
12824
12825 staticpro (&last_mouse_press_frame);
12826 last_mouse_press_frame = Qnil;
12827
12828 staticpro (&help_echo);
12829 help_echo = Qnil;
12830 staticpro (&previous_help_echo);
12831 previous_help_echo = Qnil;
12832
12833 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
12834 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
12835 For example, if a block cursor is over a tab, it will be drawn as\n\
12836 wide as that tab on the display.");
12837 x_stretch_cursor_p = 0;
12838
12839 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
12840 "If not nil, Emacs uses toolkit scroll bars.");
12841 #if USE_TOOLKIT_SCROLL_BARS
12842 x_toolkit_scroll_bars_p = 1;
12843 #else
12844 x_toolkit_scroll_bars_p = 0;
12845 #endif
12846
12847 defsubr (&Sxt_process_timeouts);
12848 staticpro (&last_mouse_motion_frame);
12849 last_mouse_motion_frame = Qnil;
12850 }
12851
12852 #endif /* not HAVE_X_WINDOWS */
12853