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