* xterm.c (x_display_and_set_cursor): Set pre-edit area.
[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 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 #ifndef USG
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif /* makedev */
53 #endif /* USG */
54
55 #ifdef BSD_SYSTEM
56 #include <sys/ioctl.h>
57 #endif /* ! defined (BSD_SYSTEM) */
58
59 #include "systty.h"
60 #include "systime.h"
61
62 #ifndef INCLUDED_FCNTL
63 #include <fcntl.h>
64 #endif
65 #include <ctype.h>
66 #include <errno.h>
67 #include <setjmp.h>
68 #include <sys/stat.h>
69 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70 /* #include <sys/param.h> */
71
72 #include "charset.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "fontset.h"
76 #include "dispextern.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #if 0
81 #include "sink.h"
82 #include "sinkmask.h"
83 #endif /* ! 0 */
84 #include "gnu.h"
85 #include "disptab.h"
86 #include "buffer.h"
87 #include "window.h"
88 #include "keyboard.h"
89 #include "intervals.h"
90 #include "process.h"
91 #include "atimer.h"
92
93 #ifdef USE_X_TOOLKIT
94 #include <X11/Shell.h>
95 #endif
96
97 #include <sys/types.h>
98 #ifdef HAVE_SYS_TIME_H
99 #include <sys/time.h>
100 #endif
101 #ifdef HAVE_UNISTD_H
102 #include <unistd.h>
103 #endif
104
105 #ifdef USE_X_TOOLKIT
106
107 extern void free_frame_menubar ();
108 extern FRAME_PTR x_menubar_window_to_frame ();
109
110 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
111 #define HACK_EDITRES
112 extern void _XEditResCheckMessages ();
113 #endif /* not NO_EDITRES */
114
115 /* Include toolkit specific headers for the scroll bar widget. */
116
117 #ifdef USE_TOOLKIT_SCROLL_BARS
118 #if defined USE_MOTIF
119 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
120 #include <Xm/ScrollBar.h>
121 #include <Xm/ScrollBarP.h>
122 #else /* !USE_MOTIF i.e. use Xaw */
123
124 #ifdef HAVE_XAW3D
125 #include <X11/Xaw3d/Simple.h>
126 #include <X11/Xaw3d/Scrollbar.h>
127 #define ARROW_SCROLLBAR
128 #include <X11/Xaw3d/ScrollbarP.h>
129 #else /* !HAVE_XAW3D */
130 #include <X11/Xaw/Simple.h>
131 #include <X11/Xaw/Scrollbar.h>
132 #endif /* !HAVE_XAW3D */
133 #ifndef XtNpickTop
134 #define XtNpickTop "pickTop"
135 #endif /* !XtNpickTop */
136 #endif /* !USE_MOTIF */
137 #endif /* USE_TOOLKIT_SCROLL_BARS */
138
139 #endif /* USE_X_TOOLKIT */
140
141 #ifndef USE_X_TOOLKIT
142 #define x_any_window_to_frame x_window_to_frame
143 #define x_top_window_to_frame x_window_to_frame
144 #endif
145
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
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 /* Non-zero means user is interacting with a toolkit scroll bar. */
303
304 static int toolkit_scroll_bar_interaction;
305
306 /* Mouse movement.
307
308 Formerly, we used PointerMotionHintMask (in standard_event_mask)
309 so that we would have to call XQueryPointer after each MotionNotify
310 event to ask for another such event. However, this made mouse tracking
311 slow, and there was a bug that made it eventually stop.
312
313 Simply asking for MotionNotify all the time seems to work better.
314
315 In order to avoid asking for motion events and then throwing most
316 of them away or busy-polling the server for mouse positions, we ask
317 the server for pointer motion hints. This means that we get only
318 one event per group of mouse movements. "Groups" are delimited by
319 other kinds of events (focus changes and button clicks, for
320 example), or by XQueryPointer calls; when one of these happens, we
321 get another MotionNotify event the next time the mouse moves. This
322 is at least as efficient as getting motion events when mouse
323 tracking is on, and I suspect only negligibly worse when tracking
324 is off. */
325
326 /* Where the mouse was last time we reported a mouse event. */
327
328 FRAME_PTR last_mouse_frame;
329 static XRectangle last_mouse_glyph;
330 static Lisp_Object last_mouse_press_frame;
331
332 /* The scroll bar in which the last X motion event occurred.
333
334 If the last X motion event occurred in a scroll bar, we set this so
335 XTmouse_position can know whether to report a scroll bar motion or
336 an ordinary motion.
337
338 If the last X motion event didn't occur in a scroll bar, we set
339 this to Qnil, to tell XTmouse_position to return an ordinary motion
340 event. */
341
342 static Lisp_Object last_mouse_scroll_bar;
343
344 /* This is a hack. We would really prefer that XTmouse_position would
345 return the time associated with the position it returns, but there
346 doesn't seem to be any way to wrest the time-stamp from the server
347 along with the position query. So, we just keep track of the time
348 of the last movement we received, and return that in hopes that
349 it's somewhat accurate. */
350
351 static Time last_mouse_movement_time;
352
353 /* Incremented by XTread_socket whenever it really tries to read
354 events. */
355
356 #ifdef __STDC__
357 static int volatile input_signal_count;
358 #else
359 static int input_signal_count;
360 #endif
361
362 /* Used locally within XTread_socket. */
363
364 static int x_noop_count;
365
366 /* Initial values of argv and argc. */
367
368 extern char **initial_argv;
369 extern int initial_argc;
370
371 extern Lisp_Object Vcommand_line_args, Vsystem_name;
372
373 /* Tells if a window manager is present or not. */
374
375 extern Lisp_Object Vx_no_window_manager;
376
377 extern Lisp_Object Qface, Qmouse_face;
378
379 extern int errno;
380
381 /* A mask of extra modifier bits to put into every keyboard char. */
382
383 extern int extra_keyboard_modifiers;
384
385 static Lisp_Object Qvendor_specific_keysyms;
386
387 extern XrmDatabase x_load_resources ();
388 extern Lisp_Object x_icon_type ();
389
390
391 /* Enumeration for overriding/changing the face to use for drawing
392 glyphs in x_draw_glyphs. */
393
394 enum draw_glyphs_face
395 {
396 DRAW_NORMAL_TEXT,
397 DRAW_INVERSE_VIDEO,
398 DRAW_CURSOR,
399 DRAW_MOUSE_FACE,
400 DRAW_IMAGE_RAISED,
401 DRAW_IMAGE_SUNKEN
402 };
403
404 static void x_update_window_end P_ ((struct window *, int));
405 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
406 void x_delete_display P_ ((struct x_display_info *));
407 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
408 unsigned));
409 static int fast_find_position P_ ((struct window *, int, int *, int *,
410 int *, int *));
411 static void set_output_cursor P_ ((struct cursor_pos *));
412 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
413 int *, int *, int *));
414 static void note_mode_line_highlight P_ ((struct window *, int, int));
415 static void x_check_font P_ ((struct frame *, XFontStruct *));
416 static void note_mouse_highlight P_ ((struct frame *, int, int));
417 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
418 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
419 static void show_mouse_face P_ ((struct x_display_info *,
420 enum draw_glyphs_face));
421 static int x_io_error_quitter P_ ((Display *));
422 int x_catch_errors P_ ((Display *));
423 void x_uncatch_errors P_ ((Display *, int));
424 void x_lower_frame P_ ((struct frame *));
425 void x_scroll_bar_clear P_ ((struct frame *));
426 int x_had_errors_p P_ ((Display *));
427 void x_wm_set_size_hint P_ ((struct frame *, long, int));
428 void x_raise_frame P_ ((struct frame *));
429 void x_set_window_size P_ ((struct frame *, int, int, int));
430 void x_wm_set_window_state P_ ((struct frame *, int));
431 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
432 void x_initialize P_ ((void));
433 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
434 static int x_compute_min_glyph_bounds P_ ((struct frame *));
435 static void x_draw_phys_cursor_glyph P_ ((struct window *,
436 struct glyph_row *,
437 enum draw_glyphs_face));
438 static void x_update_end P_ ((struct frame *));
439 static void XTframe_up_to_date P_ ((struct frame *));
440 static void XTreassert_line_highlight P_ ((int, int));
441 static void x_change_line_highlight P_ ((int, int, int, int));
442 static void XTset_terminal_modes P_ ((void));
443 static void XTreset_terminal_modes P_ ((void));
444 static void XTcursor_to P_ ((int, int, int, int));
445 static void x_write_glyphs P_ ((struct glyph *, int));
446 static void x_clear_end_of_line P_ ((int));
447 static void x_clear_frame P_ ((void));
448 static void x_clear_cursor P_ ((struct window *));
449 static void frame_highlight P_ ((struct frame *));
450 static void frame_unhighlight P_ ((struct frame *));
451 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
452 static void XTframe_rehighlight P_ ((struct frame *));
453 static void x_frame_rehighlight P_ ((struct x_display_info *));
454 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
455 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
456 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
457 XRectangle *));
458 static void expose_frame P_ ((struct frame *, int, int, int, int));
459 static void expose_window_tree P_ ((struct window *, XRectangle *));
460 static void expose_window P_ ((struct window *, XRectangle *));
461 static void expose_area P_ ((struct window *, struct glyph_row *,
462 XRectangle *, enum glyph_row_area));
463 static void expose_line P_ ((struct window *, struct glyph_row *,
464 XRectangle *));
465 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
466 static void x_update_window_cursor P_ ((struct window *, int));
467 static void x_erase_phys_cursor P_ ((struct window *));
468 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
469 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
470 enum bitmap_type));
471
472 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
473 GC, int));
474 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
475 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
476 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
477 static void x_flush P_ ((struct frame *f));
478
479
480 /* Flush display of frame F, or of all frames if F is null. */
481
482 static void
483 x_flush (f)
484 struct frame *f;
485 {
486 BLOCK_INPUT;
487 if (f == NULL)
488 {
489 Lisp_Object rest, frame;
490 FOR_EACH_FRAME (rest, frame)
491 x_flush (XFRAME (frame));
492 }
493 else if (FRAME_X_P (f))
494 XFlush (FRAME_X_DISPLAY (f));
495 UNBLOCK_INPUT;
496 }
497
498
499 /* Remove calls to XFlush by defining XFlush to an empty replacement.
500 Calls to XFlush should be unnecessary because the X output buffer
501 is flushed automatically as needed by calls to XPending,
502 XNextEvent, or XWindowEvent according to the XFlush man page.
503 XTread_socket calls XPending. Removing XFlush improves
504 performance. */
505
506 #define XFlush(DISPLAY) (void) 0
507
508 \f
509 /***********************************************************************
510 Debugging
511 ***********************************************************************/
512
513 #if 0
514
515 /* This is a function useful for recording debugging information about
516 the sequence of occurrences in this file. */
517
518 struct record
519 {
520 char *locus;
521 int type;
522 };
523
524 struct record event_record[100];
525
526 int event_record_index;
527
528 record_event (locus, type)
529 char *locus;
530 int type;
531 {
532 if (event_record_index == sizeof (event_record) / sizeof (struct record))
533 event_record_index = 0;
534
535 event_record[event_record_index].locus = locus;
536 event_record[event_record_index].type = type;
537 event_record_index++;
538 }
539
540 #endif /* 0 */
541
542
543 \f
544 /* Return the struct x_display_info corresponding to DPY. */
545
546 struct x_display_info *
547 x_display_info_for_display (dpy)
548 Display *dpy;
549 {
550 struct x_display_info *dpyinfo;
551
552 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
553 if (dpyinfo->display == dpy)
554 return dpyinfo;
555
556 return 0;
557 }
558
559
560 \f
561 /***********************************************************************
562 Starting and ending an update
563 ***********************************************************************/
564
565 /* Start an update of frame F. This function is installed as a hook
566 for update_begin, i.e. it is called when update_begin is called.
567 This function is called prior to calls to x_update_window_begin for
568 each window being updated. Currently, there is nothing to do here
569 because all interesting stuff is done on a window basis. */
570
571 static void
572 x_update_begin (f)
573 struct frame *f;
574 {
575 /* Nothing to do. */
576 }
577
578
579 /* Start update of window W. Set the global variable updated_window
580 to the window being updated and set output_cursor to the cursor
581 position of W. */
582
583 static void
584 x_update_window_begin (w)
585 struct window *w;
586 {
587 struct frame *f = XFRAME (WINDOW_FRAME (w));
588 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
589
590 updated_window = w;
591 set_output_cursor (&w->cursor);
592
593 BLOCK_INPUT;
594
595 if (f == display_info->mouse_face_mouse_frame)
596 {
597 /* Don't do highlighting for mouse motion during the update. */
598 display_info->mouse_face_defer = 1;
599
600 /* If F needs to be redrawn, simply forget about any prior mouse
601 highlighting. */
602 if (FRAME_GARBAGED_P (f))
603 display_info->mouse_face_window = Qnil;
604
605 /* Can we tell that this update does not affect the window
606 where the mouse highlight is? If so, no need to turn off.
607 Likewise, don't do anything if the frame is garbaged;
608 in that case, the frame's current matrix that we would use
609 is all wrong, and we will redisplay that line anyway. */
610 if (!NILP (display_info->mouse_face_window)
611 && w == XWINDOW (display_info->mouse_face_window))
612 {
613 int i;
614
615 for (i = 0; i < w->desired_matrix->nrows; ++i)
616 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
617 break;
618
619 if (i < w->desired_matrix->nrows)
620 clear_mouse_face (display_info);
621 }
622 }
623
624 UNBLOCK_INPUT;
625 }
626
627
628 /* Draw a vertical window border to the right of window W if W doesn't
629 have vertical scroll bars. */
630
631 static void
632 x_draw_vertical_border (w)
633 struct window *w;
634 {
635 struct frame *f = XFRAME (WINDOW_FRAME (w));
636
637 /* Redraw borders between horizontally adjacent windows. Don't
638 do it for frames with vertical scroll bars because either the
639 right scroll bar of a window, or the left scroll bar of its
640 neighbor will suffice as a border. */
641 if (!WINDOW_RIGHTMOST_P (w)
642 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
643 {
644 int x0, x1, y0, y1;
645
646 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
647 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
648 y1 -= 1;
649
650 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
651 f->output_data.x->normal_gc, x1, y0, x1, y1);
652 }
653 }
654
655
656 /* End update of window W (which is equal to updated_window). Draw
657 vertical borders between horizontally adjacent windows, and display
658 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
659 pseudo-window in case we don't have X toolkit support. Such
660 windows don't have a cursor, so don't display it here. */
661
662 static void
663 x_update_window_end (w, cursor_on_p)
664 struct window *w;
665 int cursor_on_p;
666 {
667 if (!w->pseudo_window_p)
668 {
669 BLOCK_INPUT;
670 if (cursor_on_p)
671 x_display_and_set_cursor (w, 1, output_cursor.hpos,
672 output_cursor.vpos,
673 output_cursor.x, output_cursor.y);
674 x_draw_vertical_border (w);
675 UNBLOCK_INPUT;
676 }
677
678 updated_window = NULL;
679 }
680
681
682 /* End update of frame F. This function is installed as a hook in
683 update_end. */
684
685 static void
686 x_update_end (f)
687 struct frame *f;
688 {
689 /* Mouse highlight may be displayed again. */
690 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
691
692 BLOCK_INPUT;
693 XFlush (FRAME_X_DISPLAY (f));
694 UNBLOCK_INPUT;
695 }
696
697
698 /* This function is called from various places in xdisp.c whenever a
699 complete update has been performed. The global variable
700 updated_window is not available here. */
701
702 static void
703 XTframe_up_to_date (f)
704 struct frame *f;
705 {
706 if (FRAME_X_P (f))
707 {
708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
709 if (dpyinfo->mouse_face_deferred_gc
710 || f == dpyinfo->mouse_face_mouse_frame)
711 {
712 BLOCK_INPUT;
713 if (dpyinfo->mouse_face_mouse_frame)
714 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
715 dpyinfo->mouse_face_mouse_x,
716 dpyinfo->mouse_face_mouse_y);
717 dpyinfo->mouse_face_deferred_gc = 0;
718 UNBLOCK_INPUT;
719 }
720 }
721 }
722
723
724 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
725 arrow bitmaps, or clear the areas where they would be displayed
726 before DESIRED_ROW is made current. The window being updated is
727 found in updated_window. This function It is called from
728 update_window_line only if it is known that there are differences
729 between bitmaps to be drawn between current row and DESIRED_ROW. */
730
731 static void
732 x_after_update_window_line (desired_row)
733 struct glyph_row *desired_row;
734 {
735 struct window *w = updated_window;
736
737 xassert (w);
738
739 if (!desired_row->mode_line_p && !w->pseudo_window_p)
740 {
741 BLOCK_INPUT;
742 x_draw_row_bitmaps (w, desired_row);
743
744 /* When a window has disappeared, make sure that no rest of
745 full-width rows stays visible in the internal border. */
746 if (windows_or_buffers_changed)
747 {
748 struct frame *f = XFRAME (w->frame);
749 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
750 int height = desired_row->visible_height;
751 int x = (window_box_right (w, -1)
752 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
753 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
754
755 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
756 x, y, width, height, False);
757 }
758
759 UNBLOCK_INPUT;
760 }
761 }
762
763
764 /* Draw the bitmap WHICH in one of the areas to the left or right of
765 window W. ROW is the glyph row for which to display the bitmap; it
766 determines the vertical position at which the bitmap has to be
767 drawn. */
768
769 static void
770 x_draw_bitmap (w, row, which)
771 struct window *w;
772 struct glyph_row *row;
773 enum bitmap_type which;
774 {
775 struct frame *f = XFRAME (WINDOW_FRAME (w));
776 Display *display = FRAME_X_DISPLAY (f);
777 Window window = FRAME_X_WINDOW (f);
778 int x, y, wd, h, dy;
779 unsigned char *bits;
780 Pixmap pixmap;
781 GC gc = f->output_data.x->normal_gc;
782 struct face *face;
783 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
784
785 /* Must clip because of partially visible lines. */
786 x_clip_to_row (w, row, gc, 1);
787
788 switch (which)
789 {
790 case LEFT_TRUNCATION_BITMAP:
791 wd = left_width;
792 h = left_height;
793 bits = left_bits;
794 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
795 - wd
796 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
797 break;
798
799 case OVERLAY_ARROW_BITMAP:
800 wd = left_width;
801 h = left_height;
802 bits = ov_bits;
803 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
804 - wd
805 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
806 break;
807
808 case RIGHT_TRUNCATION_BITMAP:
809 wd = right_width;
810 h = right_height;
811 bits = right_bits;
812 x = window_box_right (w, -1);
813 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
814 break;
815
816 case CONTINUED_LINE_BITMAP:
817 wd = right_width;
818 h = right_height;
819 bits = continued_bits;
820 x = window_box_right (w, -1);
821 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
822 break;
823
824 case CONTINUATION_LINE_BITMAP:
825 wd = continuation_width;
826 h = continuation_height;
827 bits = continuation_bits;
828 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
829 - wd
830 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
831 break;
832
833 case ZV_LINE_BITMAP:
834 wd = zv_width;
835 h = zv_height;
836 bits = zv_bits;
837 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
838 - wd
839 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
840 break;
841
842 default:
843 abort ();
844 }
845
846 /* Convert to frame coordinates. Set dy to the offset in the row to
847 start drawing the bitmap. */
848 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
849 dy = (row->height - h) / 2;
850
851 /* Draw the bitmap. I believe these small pixmaps can be cached
852 by the server. */
853 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
854 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
855 face->foreground,
856 face->background, depth);
857 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
858 XFreePixmap (display, pixmap);
859 XSetClipMask (display, gc, None);
860 }
861
862
863 /* Draw flags bitmaps for glyph row ROW on window W. Call this
864 function with input blocked. */
865
866 static void
867 x_draw_row_bitmaps (w, row)
868 struct window *w;
869 struct glyph_row *row;
870 {
871 struct frame *f = XFRAME (w->frame);
872 enum bitmap_type bitmap;
873 struct face *face;
874 int header_line_height = -1;
875
876 xassert (interrupt_input_blocked);
877
878 /* If row is completely invisible, because of vscrolling, we
879 don't have to draw anything. */
880 if (row->visible_height <= 0)
881 return;
882
883 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
884 PREPARE_FACE_FOR_DISPLAY (f, face);
885
886 /* Decide which bitmap to draw at the left side. */
887 if (row->overlay_arrow_p)
888 bitmap = OVERLAY_ARROW_BITMAP;
889 else if (row->truncated_on_left_p)
890 bitmap = LEFT_TRUNCATION_BITMAP;
891 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
892 bitmap = CONTINUATION_LINE_BITMAP;
893 else if (row->indicate_empty_line_p)
894 bitmap = ZV_LINE_BITMAP;
895 else
896 bitmap = NO_BITMAP;
897
898 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
899 the flags area. */
900 if (bitmap == NO_BITMAP
901 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
902 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
903 {
904 /* If W has a vertical border to its left, don't draw over it. */
905 int border = ((XFASTINT (w->left) > 0
906 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
907 ? 1 : 0);
908 int left = window_box_left (w, -1);
909
910 if (header_line_height < 0)
911 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
912
913 /* In case the same realized face is used for bitmap areas and
914 for something displayed in the text (e.g. face `region' on
915 mono-displays, the fill style may have been changed to
916 FillSolid in x_draw_glyph_string_background. */
917 if (face->stipple)
918 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
919 else
920 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
921
922 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
923 face->gc,
924 (left
925 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
926 + border),
927 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
928 row->y)),
929 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
930 row->visible_height);
931 if (!face->stipple)
932 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
933 }
934
935 /* Draw the left bitmap. */
936 if (bitmap != NO_BITMAP)
937 x_draw_bitmap (w, row, bitmap);
938
939 /* Decide which bitmap to draw at the right side. */
940 if (row->truncated_on_right_p)
941 bitmap = RIGHT_TRUNCATION_BITMAP;
942 else if (row->continued_p)
943 bitmap = CONTINUED_LINE_BITMAP;
944 else
945 bitmap = NO_BITMAP;
946
947 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
948 the flags area. */
949 if (bitmap == NO_BITMAP
950 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
951 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
952 {
953 int right = window_box_right (w, -1);
954
955 if (header_line_height < 0)
956 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
957
958 /* In case the same realized face is used for bitmap areas and
959 for something displayed in the text (e.g. face `region' on
960 mono-displays, the fill style may have been changed to
961 FillSolid in x_draw_glyph_string_background. */
962 if (face->stipple)
963 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
964 else
965 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
966 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
967 face->gc,
968 right,
969 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
970 row->y)),
971 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
972 row->visible_height);
973 if (!face->stipple)
974 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
975 }
976
977 /* Draw the right bitmap. */
978 if (bitmap != NO_BITMAP)
979 x_draw_bitmap (w, row, bitmap);
980 }
981
982 \f
983 /***********************************************************************
984 Line Highlighting
985 ***********************************************************************/
986
987 /* External interface to control of standout mode. Not used for X
988 frames. Aborts when called. */
989
990 static void
991 XTreassert_line_highlight (new, vpos)
992 int new, vpos;
993 {
994 abort ();
995 }
996
997
998 /* Call this when about to modify line at position VPOS and change
999 whether it is highlighted. Not used for X frames. Aborts when
1000 called. */
1001
1002 static void
1003 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1004 int new_highlight, vpos, y, first_unused_hpos;
1005 {
1006 abort ();
1007 }
1008
1009
1010 /* This is called when starting Emacs and when restarting after
1011 suspend. When starting Emacs, no X window is mapped. And nothing
1012 must be done to Emacs's own window if it is suspended (though that
1013 rarely happens). */
1014
1015 static void
1016 XTset_terminal_modes ()
1017 {
1018 }
1019
1020 /* This is called when exiting or suspending Emacs. Exiting will make
1021 the X-windows go away, and suspending requires no action. */
1022
1023 static void
1024 XTreset_terminal_modes ()
1025 {
1026 }
1027
1028
1029 \f
1030 /***********************************************************************
1031 Output Cursor
1032 ***********************************************************************/
1033
1034 /* Set the global variable output_cursor to CURSOR. All cursor
1035 positions are relative to updated_window. */
1036
1037 static void
1038 set_output_cursor (cursor)
1039 struct cursor_pos *cursor;
1040 {
1041 output_cursor.hpos = cursor->hpos;
1042 output_cursor.vpos = cursor->vpos;
1043 output_cursor.x = cursor->x;
1044 output_cursor.y = cursor->y;
1045 }
1046
1047
1048 /* Set a nominal cursor position.
1049
1050 HPOS and VPOS are column/row positions in a window glyph matrix. X
1051 and Y are window text area relative pixel positions.
1052
1053 If this is done during an update, updated_window will contain the
1054 window that is being updated and the position is the future output
1055 cursor position for that window. If updated_window is null, use
1056 selected_window and display the cursor at the given position. */
1057
1058 static void
1059 XTcursor_to (vpos, hpos, y, x)
1060 int vpos, hpos, y, x;
1061 {
1062 struct window *w;
1063
1064 /* If updated_window is not set, work on selected_window. */
1065 if (updated_window)
1066 w = updated_window;
1067 else
1068 w = XWINDOW (selected_window);
1069
1070 /* Set the output cursor. */
1071 output_cursor.hpos = hpos;
1072 output_cursor.vpos = vpos;
1073 output_cursor.x = x;
1074 output_cursor.y = y;
1075
1076 /* If not called as part of an update, really display the cursor.
1077 This will also set the cursor position of W. */
1078 if (updated_window == NULL)
1079 {
1080 BLOCK_INPUT;
1081 x_display_cursor (w, 1, hpos, vpos, x, y);
1082 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1083 UNBLOCK_INPUT;
1084 }
1085 }
1086
1087
1088 \f
1089 /***********************************************************************
1090 Display Iterator
1091 ***********************************************************************/
1092
1093 /* Function prototypes of this page. */
1094
1095 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1096 struct glyph *,
1097 XChar2b *));
1098 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1099 int, XChar2b *, int));
1100 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1101 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1102 static void x_append_glyph P_ ((struct it *));
1103 static void x_append_composite_glyph P_ ((struct it *));
1104 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1105 int, int, double));
1106 static void x_produce_glyphs P_ ((struct it *));
1107 static void x_produce_image_glyph P_ ((struct it *it));
1108
1109
1110 /* Return a pointer to per-char metric information in FONT of a
1111 character pointed by B which is a pointer to an XChar2b. */
1112
1113 #define PER_CHAR_METRIC(font, b) \
1114 ((font)->per_char \
1115 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1116 + (((font)->min_byte1 || (font)->max_byte1) \
1117 ? (((b)->byte1 - (font)->min_byte1) \
1118 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1119 : 0)) \
1120 : &((font)->max_bounds))
1121
1122
1123 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
1124 If CHAR2B is not contained in FONT, the font's default character
1125 metric is returned. */
1126
1127 static INLINE XCharStruct *
1128 x_per_char_metric (font, char2b)
1129 XFontStruct *font;
1130 XChar2b *char2b;
1131 {
1132 /* The result metric information. */
1133 XCharStruct *pcm = NULL;
1134
1135 xassert (font && char2b);
1136
1137 if (font->per_char != NULL)
1138 {
1139 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1140 {
1141 /* min_char_or_byte2 specifies the linear character index
1142 corresponding to the first element of the per_char array,
1143 max_char_or_byte2 is the index of the last character. A
1144 character with non-zero CHAR2B->byte1 is not in the font.
1145 A character with byte2 less than min_char_or_byte2 or
1146 greater max_char_or_byte2 is not in the font. */
1147 if (char2b->byte1 == 0
1148 && char2b->byte2 >= font->min_char_or_byte2
1149 && char2b->byte2 <= font->max_char_or_byte2)
1150 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1151 }
1152 else
1153 {
1154 /* If either min_byte1 or max_byte1 are nonzero, both
1155 min_char_or_byte2 and max_char_or_byte2 are less than
1156 256, and the 2-byte character index values corresponding
1157 to the per_char array element N (counting from 0) are:
1158
1159 byte1 = N/D + min_byte1
1160 byte2 = N\D + min_char_or_byte2
1161
1162 where:
1163
1164 D = max_char_or_byte2 - min_char_or_byte2 + 1
1165 / = integer division
1166 \ = integer modulus */
1167 if (char2b->byte1 >= font->min_byte1
1168 && char2b->byte1 <= font->max_byte1
1169 && char2b->byte2 >= font->min_char_or_byte2
1170 && char2b->byte2 <= font->max_char_or_byte2)
1171 {
1172 pcm = (font->per_char
1173 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1174 * (char2b->byte1 - font->min_byte1))
1175 + (char2b->byte2 - font->min_char_or_byte2));
1176 }
1177 }
1178 }
1179 else
1180 {
1181 /* If the per_char pointer is null, all glyphs between the first
1182 and last character indexes inclusive have the same
1183 information, as given by both min_bounds and max_bounds. */
1184 if (char2b->byte2 >= font->min_char_or_byte2
1185 && char2b->byte2 <= font->max_char_or_byte2)
1186 pcm = &font->max_bounds;
1187 }
1188
1189
1190 if (pcm == NULL || pcm->width == 0)
1191 {
1192 /* Character not contained in the font. FONT->default_char
1193 gives the character that will be printed. FONT->default_char
1194 is a 16-bit character code with byte1 in the most significant
1195 byte and byte2 in the least significant byte. */
1196 XChar2b default_char;
1197 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1198 default_char.byte2 = font->default_char & 0xff;
1199
1200 /* Avoid an endless recursion if FONT->default_char itself
1201 hasn't per char metrics. handa@etl.go.jp reports that some
1202 fonts have this problem. */
1203 if (default_char.byte1 != char2b->byte1
1204 || default_char.byte2 != char2b->byte2)
1205 pcm = x_per_char_metric (font, &default_char);
1206 else
1207 pcm = &font->max_bounds;
1208 }
1209
1210 return pcm;
1211 }
1212
1213
1214 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1215 the two-byte form of C. Encoding is returned in *CHAR2B. */
1216
1217 static INLINE void
1218 x_encode_char (c, char2b, font_info)
1219 int c;
1220 XChar2b *char2b;
1221 struct font_info *font_info;
1222 {
1223 int charset = CHAR_CHARSET (c);
1224 XFontStruct *font = font_info->font;
1225
1226 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1227 This may be either a program in a special encoder language or a
1228 fixed encoding. */
1229 if (font_info->font_encoder)
1230 {
1231 /* It's a program. */
1232 struct ccl_program *ccl = font_info->font_encoder;
1233
1234 if (CHARSET_DIMENSION (charset) == 1)
1235 {
1236 ccl->reg[0] = charset;
1237 ccl->reg[1] = char2b->byte2;
1238 }
1239 else
1240 {
1241 ccl->reg[0] = charset;
1242 ccl->reg[1] = char2b->byte1;
1243 ccl->reg[2] = char2b->byte2;
1244 }
1245
1246 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1247
1248 /* We assume that MSBs are appropriately set/reset by CCL
1249 program. */
1250 if (font->max_byte1 == 0) /* 1-byte font */
1251 char2b->byte2 = ccl->reg[1];
1252 else
1253 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1254 }
1255 else if (font_info->encoding[charset])
1256 {
1257 /* Fixed encoding scheme. See fontset.h for the meaning of the
1258 encoding numbers. */
1259 int enc = font_info->encoding[charset];
1260
1261 if ((enc == 1 || enc == 2)
1262 && CHARSET_DIMENSION (charset) == 2)
1263 char2b->byte1 |= 0x80;
1264
1265 if (enc == 1 || enc == 3)
1266 char2b->byte2 |= 0x80;
1267 }
1268 }
1269
1270
1271 /* Get face and two-byte form of character C in face FACE_ID on frame
1272 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1273 means we want to display multibyte text. Value is a pointer to a
1274 realized face that is ready for display. */
1275
1276 static INLINE struct face *
1277 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1278 struct frame *f;
1279 int c, face_id;
1280 XChar2b *char2b;
1281 int multibyte_p;
1282 {
1283 struct face *face = FACE_FROM_ID (f, face_id);
1284
1285 if (!multibyte_p)
1286 {
1287 /* Unibyte case. We don't have to encode, but we have to make
1288 sure to use a face suitable for unibyte. */
1289 char2b->byte1 = 0;
1290 char2b->byte2 = c;
1291
1292 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1293 {
1294 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1295 face = FACE_FROM_ID (f, face_id);
1296 }
1297 }
1298 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1299 {
1300 /* Case of ASCII in a face known to fit ASCII. */
1301 char2b->byte1 = 0;
1302 char2b->byte2 = c;
1303 }
1304 else
1305 {
1306 int c1, c2, charset;
1307
1308 /* Split characters into bytes. If c2 is -1 afterwards, C is
1309 really a one-byte character so that byte1 is zero. */
1310 SPLIT_CHAR (c, charset, c1, c2);
1311 if (c2 > 0)
1312 char2b->byte1 = c1, char2b->byte2 = c2;
1313 else
1314 char2b->byte1 = 0, char2b->byte2 = c1;
1315
1316 /* Get the face for displaying C. If `face' is not suitable for
1317 charset, get the one that fits. (This can happen for the
1318 translations of a composition where the glyph
1319 specifies a face for the first component, but the other
1320 components have a different charset.) */
1321 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1322 {
1323 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1324 face = FACE_FROM_ID (f, face_id);
1325 }
1326
1327 /* Maybe encode the character in *CHAR2B. */
1328 if (charset != CHARSET_ASCII)
1329 {
1330 struct font_info *font_info
1331 = FONT_INFO_FROM_ID (f, face->font_info_id);
1332 if (font_info)
1333 {
1334 x_encode_char (c, char2b, font_info);
1335 if (charset == charset_latin_iso8859_1)
1336 {
1337 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1338 >= 0x80);
1339 char2b->byte2 |= 0x80;
1340 }
1341 }
1342 }
1343 }
1344
1345 /* Make sure X resources of the face are allocated. */
1346 xassert (face != NULL);
1347 PREPARE_FACE_FOR_DISPLAY (f, face);
1348
1349 return face;
1350 }
1351
1352
1353 /* Get face and two-byte form of character glyph GLYPH on frame F.
1354 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1355 a pointer to a realized face that is ready for display. */
1356
1357 static INLINE struct face *
1358 x_get_glyph_face_and_encoding (f, glyph, char2b)
1359 struct frame *f;
1360 struct glyph *glyph;
1361 XChar2b *char2b;
1362 {
1363 struct face *face;
1364
1365 xassert (glyph->type == CHAR_GLYPH);
1366 face = FACE_FROM_ID (f, glyph->face_id);
1367
1368 if (!glyph->multibyte_p)
1369 {
1370 /* Unibyte case. We don't have to encode, but we have to make
1371 sure to use a face suitable for unibyte. */
1372 char2b->byte1 = 0;
1373 char2b->byte2 = glyph->u.ch;
1374 }
1375 else if (glyph->u.ch < 128
1376 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1377 {
1378 /* Case of ASCII in a face known to fit ASCII. */
1379 char2b->byte1 = 0;
1380 char2b->byte2 = glyph->u.ch;
1381 }
1382 else
1383 {
1384 int c1, c2, charset;
1385
1386 /* Split characters into bytes. If c2 is -1 afterwards, C is
1387 really a one-byte character so that byte1 is zero. */
1388 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1389 if (c2 > 0)
1390 char2b->byte1 = c1, char2b->byte2 = c2;
1391 else
1392 char2b->byte1 = 0, char2b->byte2 = c1;
1393
1394 /* Maybe encode the character in *CHAR2B. */
1395 if (charset != CHARSET_ASCII)
1396 {
1397 struct font_info *font_info
1398 = FONT_INFO_FROM_ID (f, face->font_info_id);
1399 if (font_info)
1400 {
1401 x_encode_char (glyph->u.ch, char2b, font_info);
1402 if (charset == charset_latin_iso8859_1)
1403 char2b->byte2 |= 0x80;
1404 }
1405 }
1406 }
1407
1408 /* Make sure X resources of the face are allocated. */
1409 xassert (face != NULL);
1410 PREPARE_FACE_FOR_DISPLAY (f, face);
1411 return face;
1412 }
1413
1414
1415 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418 static INLINE void
1419 x_append_glyph (it)
1420 struct it *it;
1421 {
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1427
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1430 {
1431 /* Play it safe. If sub-structures of the glyph are not all the
1432 same size, it otherwise be that some bits stay set. This
1433 would prevent a comparison with GLYPH_EQUAL_P. */
1434 glyph->u.val = 0;
1435
1436 glyph->type = CHAR_GLYPH;
1437 glyph->pixel_width = it->pixel_width;
1438 glyph->u.ch = it->char_to_display;
1439 glyph->face_id = it->face_id;
1440 glyph->charpos = CHARPOS (it->position);
1441 glyph->object = it->object;
1442 glyph->left_box_line_p = it->start_of_box_run_p;
1443 glyph->right_box_line_p = it->end_of_box_run_p;
1444 glyph->voffset = it->voffset;
1445 glyph->multibyte_p = it->multibyte_p;
1446 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1447 || it->phys_descent > it->descent);
1448 ++it->glyph_row->used[area];
1449 }
1450 }
1451
1452 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1453 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1454
1455 static INLINE void
1456 x_append_composite_glyph (it)
1457 struct it *it;
1458 {
1459 struct glyph *glyph;
1460 enum glyph_row_area area = it->area;
1461
1462 xassert (it->glyph_row);
1463
1464 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1465 if (glyph < it->glyph_row->glyphs[area + 1])
1466 {
1467 /* Play it safe. If sub-structures of the glyph are not all the
1468 same size, it otherwise be that some bits stay set. This
1469 would prevent a comparison with GLYPH_EQUAL_P. */
1470 glyph->u.val = 0;
1471
1472 glyph->type = COMPOSITE_GLYPH;
1473 glyph->pixel_width = it->pixel_width;
1474 glyph->u.cmp_id = it->cmp_id;
1475 glyph->face_id = it->face_id;
1476 glyph->charpos = CHARPOS (it->position);
1477 glyph->object = it->object;
1478 glyph->left_box_line_p = it->start_of_box_run_p;
1479 glyph->right_box_line_p = it->end_of_box_run_p;
1480 glyph->voffset = it->voffset;
1481 glyph->multibyte_p = it->multibyte_p;
1482 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1483 || it->phys_descent > it->descent);
1484 ++it->glyph_row->used[area];
1485 }
1486 }
1487
1488
1489 /* Change IT->ascent and IT->height according to the setting of
1490 IT->voffset. */
1491
1492 static INLINE void
1493 take_vertical_position_into_account (it)
1494 struct it *it;
1495 {
1496 if (it->voffset)
1497 {
1498 if (it->voffset < 0)
1499 /* Increase the ascent so that we can display the text higher
1500 in the line. */
1501 it->ascent += abs (it->voffset);
1502 else
1503 /* Increase the descent so that we can display the text lower
1504 in the line. */
1505 it->descent += it->voffset;
1506 }
1507 }
1508
1509
1510 /* Produce glyphs/get display metrics for the image IT is loaded with.
1511 See the description of struct display_iterator in dispextern.h for
1512 an overview of struct display_iterator. */
1513
1514 static void
1515 x_produce_image_glyph (it)
1516 struct it *it;
1517 {
1518 struct image *img;
1519 struct face *face;
1520
1521 xassert (it->what == IT_IMAGE);
1522
1523 face = FACE_FROM_ID (it->f, it->face_id);
1524 img = IMAGE_FROM_ID (it->f, it->image_id);
1525 xassert (img);
1526
1527 /* Make sure X resources of the face and image are loaded. */
1528 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1529 prepare_image_for_display (it->f, img);
1530
1531 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1532 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1533 it->pixel_width = img->width + 2 * img->margin;
1534
1535 it->nglyphs = 1;
1536
1537 if (face->box != FACE_NO_BOX)
1538 {
1539 it->ascent += face->box_line_width;
1540 it->descent += face->box_line_width;
1541
1542 if (it->start_of_box_run_p)
1543 it->pixel_width += face->box_line_width;
1544 if (it->end_of_box_run_p)
1545 it->pixel_width += face->box_line_width;
1546 }
1547
1548 take_vertical_position_into_account (it);
1549
1550 if (it->glyph_row)
1551 {
1552 struct glyph *glyph;
1553 enum glyph_row_area area = it->area;
1554
1555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1556 if (glyph < it->glyph_row->glyphs[area + 1])
1557 {
1558 glyph->type = IMAGE_GLYPH;
1559 glyph->u.img_id = img->id;
1560 glyph->face_id = it->face_id;
1561 glyph->pixel_width = it->pixel_width;
1562 glyph->charpos = CHARPOS (it->position);
1563 glyph->object = it->object;
1564 glyph->left_box_line_p = it->start_of_box_run_p;
1565 glyph->right_box_line_p = it->end_of_box_run_p;
1566 glyph->voffset = it->voffset;
1567 glyph->multibyte_p = it->multibyte_p;
1568 ++it->glyph_row->used[area];
1569 }
1570 }
1571 }
1572
1573
1574 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1575 of the glyph, WIDTH and HEIGHT are the width and height of the
1576 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1577 ascent of the glyph (0 <= ASCENT <= 1). */
1578
1579 static void
1580 x_append_stretch_glyph (it, object, width, height, ascent)
1581 struct it *it;
1582 Lisp_Object object;
1583 int width, height;
1584 double ascent;
1585 {
1586 struct glyph *glyph;
1587 enum glyph_row_area area = it->area;
1588
1589 xassert (ascent >= 0 && ascent <= 1);
1590
1591 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1592 if (glyph < it->glyph_row->glyphs[area + 1])
1593 {
1594 glyph->type = STRETCH_GLYPH;
1595 glyph->u.stretch.ascent = height * ascent;
1596 glyph->u.stretch.height = height;
1597 glyph->face_id = it->face_id;
1598 glyph->pixel_width = width;
1599 glyph->charpos = CHARPOS (it->position);
1600 glyph->object = object;
1601 glyph->left_box_line_p = it->start_of_box_run_p;
1602 glyph->right_box_line_p = it->end_of_box_run_p;
1603 glyph->voffset = it->voffset;
1604 glyph->multibyte_p = it->multibyte_p;
1605 ++it->glyph_row->used[area];
1606 }
1607 }
1608
1609
1610 /* Produce a stretch glyph for iterator IT. IT->object is the value
1611 of the glyph property displayed. The value must be a list
1612 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1613 being recognized:
1614
1615 1. `:width WIDTH' specifies that the space should be WIDTH *
1616 canonical char width wide. WIDTH may be an integer or floating
1617 point number.
1618
1619 2. `:relative-width FACTOR' specifies that the width of the stretch
1620 should be computed from the width of the first character having the
1621 `glyph' property, and should be FACTOR times that width.
1622
1623 3. `:align-to HPOS' specifies that the space should be wide enough
1624 to reach HPOS, a value in canonical character units.
1625
1626 Exactly one of the above pairs must be present.
1627
1628 4. `:height HEIGHT' specifies that the height of the stretch produced
1629 should be HEIGHT, measured in canonical character units.
1630
1631 5. `:relative-height FACTOR' specifies that the height of the the
1632 stretch should be FACTOR times the height of the characters having
1633 the glyph property.
1634
1635 Either none or exactly one of 4 or 5 must be present.
1636
1637 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1638 of the stretch should be used for the ascent of the stretch.
1639 ASCENT must be in the range 0 <= ASCENT <= 100. */
1640
1641 #define NUMVAL(X) \
1642 ((INTEGERP (X) || FLOATP (X)) \
1643 ? XFLOATINT (X) \
1644 : - 1)
1645
1646
1647 static void
1648 x_produce_stretch_glyph (it)
1649 struct it *it;
1650 {
1651 /* (space :width WIDTH :height HEIGHT. */
1652 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1653 extern Lisp_Object QCrelative_width, QCrelative_height;
1654 extern Lisp_Object QCalign_to;
1655 Lisp_Object prop, plist;
1656 double width = 0, height = 0, ascent = 0;
1657 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1658 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1659
1660 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1661
1662 /* List should start with `space'. */
1663 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1664 plist = XCDR (it->object);
1665
1666 /* Compute the width of the stretch. */
1667 if (prop = Fplist_get (plist, QCwidth),
1668 NUMVAL (prop) > 0)
1669 /* Absolute width `:width WIDTH' specified and valid. */
1670 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1671 else if (prop = Fplist_get (plist, QCrelative_width),
1672 NUMVAL (prop) > 0)
1673 {
1674 /* Relative width `:relative-width FACTOR' specified and valid.
1675 Compute the width of the characters having the `glyph'
1676 property. */
1677 struct it it2;
1678 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1679
1680 it2 = *it;
1681 if (it->multibyte_p)
1682 {
1683 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1684 - IT_BYTEPOS (*it));
1685 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1686 }
1687 else
1688 it2.c = *p, it2.len = 1;
1689
1690 it2.glyph_row = NULL;
1691 it2.what = IT_CHARACTER;
1692 x_produce_glyphs (&it2);
1693 width = NUMVAL (prop) * it2.pixel_width;
1694 }
1695 else if (prop = Fplist_get (plist, QCalign_to),
1696 NUMVAL (prop) > 0)
1697 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1698 else
1699 /* Nothing specified -> width defaults to canonical char width. */
1700 width = CANON_X_UNIT (it->f);
1701
1702 /* Compute height. */
1703 if (prop = Fplist_get (plist, QCheight),
1704 NUMVAL (prop) > 0)
1705 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1706 else if (prop = Fplist_get (plist, QCrelative_height),
1707 NUMVAL (prop) > 0)
1708 height = FONT_HEIGHT (font) * NUMVAL (prop);
1709 else
1710 height = FONT_HEIGHT (font);
1711
1712 /* Compute percentage of height used for ascent. If
1713 `:ascent ASCENT' is present and valid, use that. Otherwise,
1714 derive the ascent from the font in use. */
1715 if (prop = Fplist_get (plist, QCascent),
1716 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1717 ascent = NUMVAL (prop) / 100.0;
1718 else
1719 ascent = (double) font->ascent / FONT_HEIGHT (font);
1720
1721 if (width <= 0)
1722 width = 1;
1723 if (height <= 0)
1724 height = 1;
1725
1726 if (it->glyph_row)
1727 {
1728 Lisp_Object object = it->stack[it->sp - 1].string;
1729 if (!STRINGP (object))
1730 object = it->w->buffer;
1731 x_append_stretch_glyph (it, object, width, height, ascent);
1732 }
1733
1734 it->pixel_width = width;
1735 it->ascent = it->phys_ascent = height * ascent;
1736 it->descent = it->phys_descent = height - it->ascent;
1737 it->nglyphs = 1;
1738
1739 if (face->box != FACE_NO_BOX)
1740 {
1741 it->ascent += face->box_line_width;
1742 it->descent += face->box_line_width;
1743
1744 if (it->start_of_box_run_p)
1745 it->pixel_width += face->box_line_width;
1746 if (it->end_of_box_run_p)
1747 it->pixel_width += face->box_line_width;
1748 }
1749
1750 take_vertical_position_into_account (it);
1751 }
1752
1753 /* Return proper value to be used as baseline offset of font that has
1754 ASCENT and DESCENT to draw characters by the font at the vertical
1755 center of the line of frame F.
1756
1757 Here, out task is to find the value of BOFF in the following figure;
1758
1759 -------------------------+-----------+-
1760 -+-+---------+-+ | |
1761 | | | | | |
1762 | | | | F_ASCENT F_HEIGHT
1763 | | | ASCENT | |
1764 HEIGHT | | | | |
1765 | | |-|-+------+-----------|------- baseline
1766 | | | | BOFF | |
1767 | |---------|-+-+ | |
1768 | | | DESCENT | |
1769 -+-+---------+-+ F_DESCENT |
1770 -------------------------+-----------+-
1771
1772 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1773 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1774 DESCENT = FONT->descent
1775 HEIGHT = FONT_HEIGHT (FONT)
1776 F_DESCENT = (F->output_data.x->font->descent
1777 - F->output_data.x->baseline_offset)
1778 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1779 */
1780
1781 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1782 ((FONT)->descent \
1783 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1784 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1785
1786 /* Produce glyphs/get display metrics for the display element IT is
1787 loaded with. See the description of struct display_iterator in
1788 dispextern.h for an overview of struct display_iterator. */
1789
1790 static void
1791 x_produce_glyphs (it)
1792 struct it *it;
1793 {
1794 if (it->what == IT_CHARACTER)
1795 {
1796 XChar2b char2b;
1797 XFontStruct *font;
1798 struct face *face;
1799 XCharStruct *pcm;
1800 int font_not_found_p;
1801 struct font_info *font_info;
1802 int boff; /* baseline offset */
1803
1804 /* Maybe translate single-byte characters to multibyte. */
1805 it->char_to_display = it->c;
1806 if (unibyte_display_via_language_environment
1807 && SINGLE_BYTE_CHAR_P (it->c)
1808 && (it->c >= 0240
1809 || (it->c >= 0200
1810 && !NILP (Vnonascii_translation_table))))
1811 {
1812 it->char_to_display = unibyte_char_to_multibyte (it->c);
1813 it->charset = CHAR_CHARSET (it->char_to_display);
1814 }
1815
1816 /* Get face and font to use. Encode IT->char_to_display. */
1817 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1818 it->face_id, &char2b,
1819 it->multibyte_p);
1820 font = face->font;
1821
1822 /* When no suitable font found, use the default font. */
1823 font_not_found_p = font == NULL;
1824 if (font_not_found_p)
1825 {
1826 font = FRAME_FONT (it->f);
1827 boff = it->f->output_data.x->baseline_offset;
1828 font_info = NULL;
1829 }
1830 else
1831 {
1832 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1833 boff = font_info->baseline_offset;
1834 if (font_info->vertical_centering)
1835 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1836 }
1837
1838 if (it->char_to_display >= ' '
1839 && (!it->multibyte_p || it->char_to_display < 128))
1840 {
1841 /* Either unibyte or ASCII. */
1842 int stretched_p;
1843
1844 it->nglyphs = 1;
1845
1846 pcm = x_per_char_metric (font, &char2b);
1847 it->ascent = font->ascent + boff;
1848 it->descent = font->descent - boff;
1849 it->phys_ascent = pcm->ascent + boff;
1850 it->phys_descent = pcm->descent - boff;
1851 it->pixel_width = pcm->width;
1852
1853 /* If this is a space inside a region of text with
1854 `space-width' property, change its width. */
1855 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1856 if (stretched_p)
1857 it->pixel_width *= XFLOATINT (it->space_width);
1858
1859 /* If face has a box, add the box thickness to the character
1860 height. If character has a box line to the left and/or
1861 right, add the box line width to the character's width. */
1862 if (face->box != FACE_NO_BOX)
1863 {
1864 int thick = face->box_line_width;
1865
1866 it->ascent += thick;
1867 it->descent += thick;
1868
1869 if (it->start_of_box_run_p)
1870 it->pixel_width += thick;
1871 if (it->end_of_box_run_p)
1872 it->pixel_width += thick;
1873 }
1874
1875 /* If face has an overline, add the height of the overline
1876 (1 pixel) and a 1 pixel margin to the character height. */
1877 if (face->overline_p)
1878 it->ascent += 2;
1879
1880 take_vertical_position_into_account (it);
1881
1882 /* If we have to actually produce glyphs, do it. */
1883 if (it->glyph_row)
1884 {
1885 if (stretched_p)
1886 {
1887 /* Translate a space with a `space-width' property
1888 into a stretch glyph. */
1889 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1890 x_append_stretch_glyph (it, it->object, it->pixel_width,
1891 it->ascent + it->descent, ascent);
1892 }
1893 else
1894 x_append_glyph (it);
1895
1896 /* If characters with lbearing or rbearing are displayed
1897 in this line, record that fact in a flag of the
1898 glyph row. This is used to optimize X output code. */
1899 if (pcm->lbearing < 0
1900 || pcm->rbearing > pcm->width)
1901 it->glyph_row->contains_overlapping_glyphs_p = 1;
1902 }
1903 }
1904 else if (it->char_to_display == '\n')
1905 {
1906 /* A newline has no width but we need the height of the line. */
1907 it->pixel_width = 0;
1908 it->nglyphs = 0;
1909 it->ascent = it->phys_ascent = font->ascent + boff;
1910 it->descent = it->phys_descent = font->descent - boff;
1911
1912 if (face->box != FACE_NO_BOX)
1913 {
1914 int thick = face->box_line_width;
1915 it->ascent += thick;
1916 it->descent += thick;
1917 }
1918 }
1919 else if (it->char_to_display == '\t')
1920 {
1921 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1922 int x = (it->current_x
1923 - it->prompt_width
1924 + it->continuation_lines_width);
1925 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1926
1927 it->pixel_width = next_tab_x - x;
1928 it->nglyphs = 1;
1929 it->ascent = it->phys_ascent = font->ascent + boff;
1930 it->descent = it->phys_descent = font->descent - boff;
1931
1932 if (it->glyph_row)
1933 {
1934 double ascent = (double) it->ascent / (it->ascent + it->descent);
1935 x_append_stretch_glyph (it, it->object, it->pixel_width,
1936 it->ascent + it->descent, ascent);
1937 }
1938 }
1939 else
1940 {
1941 /* A multi-byte character. Assume that the display width of the
1942 character is the width of the character multiplied by the
1943 width of the font. */
1944
1945 /* If we found a font, this font should give us the right
1946 metrics. If we didn't find a font, use the frame's
1947 default font and calculate the width of the character
1948 from the charset width; this is what old redisplay code
1949 did. */
1950 pcm = x_per_char_metric (font, &char2b);
1951 it->pixel_width = pcm->width;
1952 if (font_not_found_p)
1953 it->pixel_width *= CHARSET_WIDTH (it->charset);
1954 it->nglyphs = 1;
1955 it->ascent = font->ascent + boff;
1956 it->descent = font->descent - boff;
1957 it->phys_ascent = pcm->ascent + boff;
1958 it->phys_descent = pcm->descent - boff;
1959 if (it->glyph_row
1960 && (pcm->lbearing < 0
1961 || pcm->rbearing > pcm->width))
1962 it->glyph_row->contains_overlapping_glyphs_p = 1;
1963
1964 if (face->box != FACE_NO_BOX)
1965 {
1966 int thick = face->box_line_width;
1967 it->ascent += thick;
1968 it->descent += thick;
1969
1970 if (it->start_of_box_run_p)
1971 it->pixel_width += thick;
1972 if (it->end_of_box_run_p)
1973 it->pixel_width += thick;
1974 }
1975
1976 /* If face has an overline, add the height of the overline
1977 (1 pixel) and a 1 pixel margin to the character height. */
1978 if (face->overline_p)
1979 it->ascent += 2;
1980
1981 take_vertical_position_into_account (it);
1982
1983 if (it->glyph_row)
1984 x_append_glyph (it);
1985 }
1986 }
1987 else if (it->what == IT_COMPOSITION)
1988 {
1989 /* Note: A composition is represented as one glyph in the
1990 glyph matrix. There are no padding glyphs. */
1991 XChar2b char2b;
1992 XFontStruct *font;
1993 struct face *face;
1994 XCharStruct *pcm;
1995 int font_not_found_p;
1996 struct font_info *font_info;
1997 int boff; /* baseline offset */
1998 struct composition *cmp = composition_table[it->cmp_id];
1999
2000 /* Maybe translate single-byte characters to multibyte. */
2001 it->char_to_display = it->c;
2002 if (unibyte_display_via_language_environment
2003 && SINGLE_BYTE_CHAR_P (it->c)
2004 && (it->c >= 0240
2005 || (it->c >= 0200
2006 && !NILP (Vnonascii_translation_table))))
2007 {
2008 it->char_to_display = unibyte_char_to_multibyte (it->c);
2009 it->charset = CHAR_CHARSET (it->char_to_display);
2010 }
2011
2012 /* Get face and font to use. Encode IT->char_to_display. */
2013 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
2014 it->face_id, &char2b,
2015 it->multibyte_p);
2016 font = face->font;
2017
2018 /* When no suitable font found, use the default font. */
2019 font_not_found_p = font == NULL;
2020 if (font_not_found_p)
2021 {
2022 font = FRAME_FONT (it->f);
2023 boff = it->f->output_data.x->baseline_offset;
2024 font_info = NULL;
2025 }
2026 else
2027 {
2028 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2029 boff = font_info->baseline_offset;
2030 if (font_info->vertical_centering)
2031 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2032 }
2033
2034 /* There are no padding glyphs, so there is only one glyph to
2035 produce for the composition. Important is that pixel_width,
2036 ascent and descent are the values of what is drawn by
2037 draw_glyphs (i.e. the values of the overall glyphs composed). */
2038 it->nglyphs = 1;
2039
2040 /* If we have not yet calculated pixel size data of glyphs of
2041 the composition for the current face font, calculate them
2042 now. Theoretically, we have to check all fonts for the
2043 glyphs, but that requires much time and memory space. So,
2044 here we check only the font of the first glyph. This leads
2045 to incorrect display very rarely, and C-l (recenter) can
2046 correct the display anyway. */
2047 if (cmp->font != (void *) font)
2048 {
2049 /* Ascent and descent of the font of the first character of
2050 this composition (adjusted by baseline offset). Ascent
2051 and descent of overall glyphs should not be less than
2052 them respectively. */
2053 int font_ascent = font->ascent + boff;
2054 int font_descent = font->descent - boff;
2055 /* Bounding box of the overall glyphs. */
2056 int leftmost, rightmost, lowest, highest;
2057 int i;
2058
2059 cmp->font = (void *) font;
2060
2061 /* Initialize the bounding box. */
2062 pcm = x_per_char_metric (font, &char2b);
2063 leftmost = 0;
2064 rightmost = pcm->width;
2065 lowest = - pcm->descent + boff;
2066 highest = pcm->ascent + boff;
2067 if (font_info
2068 && font_info->default_ascent
2069 && CHAR_TABLE_P (Vuse_default_ascent)
2070 && !NILP (Faref (Vuse_default_ascent,
2071 make_number (it->char_to_display))))
2072 highest = font_info->default_ascent + boff;
2073
2074 /* Draw the first glyph at the normal position. It may be
2075 shifted to right later if some other glyphs are drawn at
2076 the left. */
2077 cmp->offsets[0] = 0;
2078 cmp->offsets[1] = boff;
2079
2080 /* Set cmp->offsets for the remaining glyphs. */
2081 for (i = 1; i < cmp->glyph_len; i++)
2082 {
2083 int left, right, btm, top;
2084 int ch = COMPOSITION_GLYPH (cmp, i);
2085
2086 face = x_get_char_face_and_encoding (it->f, ch,
2087 it->face_id, &char2b,
2088 it->multibyte_p);
2089 font = face->font;
2090 if (font == NULL)
2091 {
2092 font = FRAME_FONT (it->f);
2093 boff = it->f->output_data.x->baseline_offset;
2094 font_info = NULL;
2095 }
2096 else
2097 {
2098 font_info
2099 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2100 boff = font_info->baseline_offset;
2101 if (font_info->vertical_centering)
2102 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2103 }
2104
2105 pcm = x_per_char_metric (font, &char2b);
2106
2107 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2108 {
2109 /* Relative composition with or without
2110 alternate chars. */
2111 left = (leftmost + rightmost - pcm->width) / 2;
2112 btm = - pcm->descent + boff;
2113 if (font_info && font_info->relative_compose
2114 && (! CHAR_TABLE_P (Vignore_relative_composition)
2115 || NILP (Faref (Vignore_relative_composition,
2116 make_number (ch)))))
2117 {
2118
2119 if (- pcm->descent
2120 >= font_info->relative_compose)
2121 /* One extra pixel between two glyphs. */
2122 btm = highest + 1;
2123 else if (pcm->ascent <= 0)
2124 /* One extra pixel between two glyphs. */
2125 btm = lowest - 1 - pcm->ascent - pcm->descent;
2126 }
2127 }
2128 else
2129 {
2130 /* A composition rule is specified by an integer
2131 value that encodes global and new reference
2132 points (GREF and NREF). GREF and NREF are
2133 specified by numbers as below:
2134
2135 0---1---2 -- ascent
2136 | |
2137 | |
2138 | |
2139 9--10--11 -- center
2140 | |
2141 ---3---4---5--- baseline
2142 | |
2143 6---7---8 -- descent
2144 */
2145 int rule = COMPOSITION_RULE (cmp, i);
2146 int gref, nref, grefx, grefy, nrefx, nrefy;
2147
2148 COMPOSITION_DECODE_RULE (rule, gref, nref);
2149 grefx = gref % 3, nrefx = nref % 3;
2150 grefy = gref / 3, nrefy = nref / 3;
2151
2152 left = (leftmost
2153 + grefx * (rightmost - leftmost) / 2
2154 - nrefx * pcm->width / 2);
2155 btm = ((grefy == 0 ? highest
2156 : grefy == 1 ? 0
2157 : grefy == 2 ? lowest
2158 : (highest + lowest) / 2)
2159 - (nrefy == 0 ? pcm->ascent + pcm->descent
2160 : nrefy == 1 ? pcm->descent - boff
2161 : nrefy == 2 ? 0
2162 : (pcm->ascent + pcm->descent) / 2));
2163 }
2164
2165 cmp->offsets[i * 2] = left;
2166 cmp->offsets[i * 2 + 1] = btm + pcm->descent;
2167
2168 /* Update the bounding box of the overall glyphs. */
2169 right = left + pcm->width;
2170 top = btm + pcm->descent + pcm->ascent;
2171 if (left < leftmost)
2172 leftmost = left;
2173 if (right > rightmost)
2174 rightmost = right;
2175 if (top > highest)
2176 highest = top;
2177 if (btm < lowest)
2178 lowest = btm;
2179 }
2180
2181 /* If there are glyphs whose x-offsets are negative,
2182 shift all glyphs to the right and make all x-offsets
2183 non-negative. */
2184 if (leftmost < 0)
2185 {
2186 for (i = 0; i < cmp->glyph_len; i++)
2187 cmp->offsets[i * 2] -= leftmost;
2188 rightmost -= leftmost;
2189 }
2190
2191 cmp->pixel_width = rightmost;
2192 cmp->ascent = highest;
2193 cmp->descent = - lowest;
2194 if (cmp->ascent < font_ascent)
2195 cmp->ascent = font_ascent;
2196 if (cmp->descent < font_descent)
2197 cmp->descent = font_descent;
2198 }
2199
2200 it->pixel_width = cmp->pixel_width;
2201 it->ascent = it->phys_ascent = cmp->ascent;
2202 it->descent = it->phys_descent = cmp->descent;
2203
2204 if (face->box != FACE_NO_BOX)
2205 {
2206 int thick = face->box_line_width;
2207 it->ascent += thick;
2208 it->descent += thick;
2209
2210 if (it->start_of_box_run_p)
2211 it->pixel_width += thick;
2212 if (it->end_of_box_run_p)
2213 it->pixel_width += thick;
2214 }
2215
2216 /* If face has an overline, add the height of the overline
2217 (1 pixel) and a 1 pixel margin to the character height. */
2218 if (face->overline_p)
2219 it->ascent += 2;
2220
2221 take_vertical_position_into_account (it);
2222
2223 if (it->glyph_row)
2224 x_append_composite_glyph (it);
2225 }
2226 else if (it->what == IT_IMAGE)
2227 x_produce_image_glyph (it);
2228 else if (it->what == IT_STRETCH)
2229 x_produce_stretch_glyph (it);
2230
2231 /* Accumulate dimensions. */
2232 xassert (it->ascent >= 0 && it->descent > 0);
2233 if (it->area == TEXT_AREA)
2234 it->current_x += it->pixel_width;
2235
2236 it->max_ascent = max (it->max_ascent, it->ascent);
2237 it->max_descent = max (it->max_descent, it->descent);
2238 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2239 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2240 }
2241
2242
2243 /* Estimate the pixel height of the mode or top line on frame F.
2244 FACE_ID specifies what line's height to estimate. */
2245
2246 int
2247 x_estimate_mode_line_height (f, face_id)
2248 struct frame *f;
2249 enum face_id face_id;
2250 {
2251 int height = 1;
2252
2253 /* This function is called so early when Emacs starts that the face
2254 cache and mode line face are not yet initialized. */
2255 if (FRAME_FACE_CACHE (f))
2256 {
2257 struct face *face = FACE_FROM_ID (f, face_id);
2258 if (face)
2259 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2260 }
2261
2262 return height;
2263 }
2264
2265 \f
2266 /***********************************************************************
2267 Glyph display
2268 ***********************************************************************/
2269
2270 /* A sequence of glyphs to be drawn in the same face.
2271
2272 This data structure is not really completely X specific, so it
2273 could possibly, at least partially, be useful for other systems. It
2274 is currently not part of the external redisplay interface because
2275 it's not clear what other systems will need. */
2276
2277 struct glyph_string
2278 {
2279 /* X-origin of the string. */
2280 int x;
2281
2282 /* Y-origin and y-position of the base line of this string. */
2283 int y, ybase;
2284
2285 /* The width of the string, not including a face extension. */
2286 int width;
2287
2288 /* The width of the string, including a face extension. */
2289 int background_width;
2290
2291 /* The height of this string. This is the height of the line this
2292 string is drawn in, and can be different from the height of the
2293 font the string is drawn in. */
2294 int height;
2295
2296 /* Number of pixels this string overwrites in front of its x-origin.
2297 This number is zero if the string has an lbearing >= 0; it is
2298 -lbearing, if the string has an lbearing < 0. */
2299 int left_overhang;
2300
2301 /* Number of pixels this string overwrites past its right-most
2302 nominal x-position, i.e. x + width. Zero if the string's
2303 rbearing is <= its nominal width, rbearing - width otherwise. */
2304 int right_overhang;
2305
2306 /* The frame on which the glyph string is drawn. */
2307 struct frame *f;
2308
2309 /* The window on which the glyph string is drawn. */
2310 struct window *w;
2311
2312 /* X display and window for convenience. */
2313 Display *display;
2314 Window window;
2315
2316 /* The glyph row for which this string was built. It determines the
2317 y-origin and height of the string. */
2318 struct glyph_row *row;
2319
2320 /* The area within row. */
2321 enum glyph_row_area area;
2322
2323 /* Characters to be drawn, and number of characters. */
2324 XChar2b *char2b;
2325 int nchars;
2326
2327 /* Character set of this glyph string. */
2328 int charset;
2329
2330 /* A face-override for drawing cursors, mouse face and similar. */
2331 enum draw_glyphs_face hl;
2332
2333 /* Face in which this string is to be drawn. */
2334 struct face *face;
2335
2336 /* Font in which this string is to be drawn. */
2337 XFontStruct *font;
2338
2339 /* Font info for this string. */
2340 struct font_info *font_info;
2341
2342 /* Non-null means this string describes (part of) a composition.
2343 All characters from char2b are drawn composed. */
2344 struct composition *cmp;
2345
2346 /* Index of this glyph string's first character in the glyph
2347 definition of CMP. If this is zero, this glyph string describes
2348 the first character of a composition. */
2349 int gidx;
2350
2351 /* 1 means this glyph strings face has to be drawn to the right end
2352 of the window's drawing area. */
2353 unsigned extends_to_end_of_line_p : 1;
2354
2355 /* 1 means the background of this string has been drawn. */
2356 unsigned background_filled_p : 1;
2357
2358 /* 1 means glyph string must be drawn with 16-bit functions. */
2359 unsigned two_byte_p : 1;
2360
2361 /* 1 means that the original font determined for drawing this glyph
2362 string could not be loaded. The member `font' has been set to
2363 the frame's default font in this case. */
2364 unsigned font_not_found_p : 1;
2365
2366 /* 1 means that the face in which this glyph string is drawn has a
2367 stipple pattern. */
2368 unsigned stippled_p : 1;
2369
2370 /* 1 means only the foreground of this glyph string must be drawn,
2371 and we should use the physical height of the line this glyph
2372 string appears in as clip rect. */
2373 unsigned for_overlaps_p : 1;
2374
2375 /* The GC to use for drawing this glyph string. */
2376 GC gc;
2377
2378 /* A pointer to the first glyph in the string. This glyph
2379 corresponds to char2b[0]. Needed to draw rectangles if
2380 font_not_found_p is 1. */
2381 struct glyph *first_glyph;
2382
2383 /* Image, if any. */
2384 struct image *img;
2385
2386 struct glyph_string *next, *prev;
2387 };
2388
2389
2390 #if 0
2391
2392 static void
2393 x_dump_glyph_string (s)
2394 struct glyph_string *s;
2395 {
2396 fprintf (stderr, "glyph string\n");
2397 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2398 s->x, s->y, s->width, s->height);
2399 fprintf (stderr, " ybase = %d\n", s->ybase);
2400 fprintf (stderr, " hl = %d\n", s->hl);
2401 fprintf (stderr, " left overhang = %d, right = %d\n",
2402 s->left_overhang, s->right_overhang);
2403 fprintf (stderr, " nchars = %d\n", s->nchars);
2404 fprintf (stderr, " extends to end of line = %d\n",
2405 s->extends_to_end_of_line_p);
2406 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2407 fprintf (stderr, " bg width = %d\n", s->background_width);
2408 }
2409
2410 #endif /* GLYPH_DEBUG */
2411
2412
2413
2414 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2415 struct glyph_string **,
2416 struct glyph_string *,
2417 struct glyph_string *));
2418 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2419 struct glyph_string **,
2420 struct glyph_string *,
2421 struct glyph_string *));
2422 static void x_append_glyph_string P_ ((struct glyph_string **,
2423 struct glyph_string **,
2424 struct glyph_string *));
2425 static int x_left_overwritten P_ ((struct glyph_string *));
2426 static int x_left_overwriting P_ ((struct glyph_string *));
2427 static int x_right_overwritten P_ ((struct glyph_string *));
2428 static int x_right_overwriting P_ ((struct glyph_string *));
2429 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2430 int));
2431 static void x_init_glyph_string P_ ((struct glyph_string *,
2432 XChar2b *, struct window *,
2433 struct glyph_row *,
2434 enum glyph_row_area, int,
2435 enum draw_glyphs_face));
2436 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2437 enum glyph_row_area, int, int,
2438 enum draw_glyphs_face, int *, int *, int));
2439 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2440 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2441 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2442 int));
2443 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2444 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2445 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2446 static void x_draw_glyph_string P_ ((struct glyph_string *));
2447 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2448 static void x_set_cursor_gc P_ ((struct glyph_string *));
2449 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2450 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2451 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2452 int *, int *));
2453 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2454 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2455 unsigned long *, double, int));
2456 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2457 double, int, unsigned long));
2458 static void x_setup_relief_colors P_ ((struct glyph_string *));
2459 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2460 static void x_draw_image_relief P_ ((struct glyph_string *));
2461 static void x_draw_image_foreground P_ ((struct glyph_string *));
2462 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2463 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2464 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2465 int, int, int));
2466 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2467 int, int, int, int, XRectangle *));
2468 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2469 int, int, int, XRectangle *));
2470 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2471 enum glyph_row_area));
2472
2473
2474 /* Append the list of glyph strings with head H and tail T to the list
2475 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2476
2477 static INLINE void
2478 x_append_glyph_string_lists (head, tail, h, t)
2479 struct glyph_string **head, **tail;
2480 struct glyph_string *h, *t;
2481 {
2482 if (h)
2483 {
2484 if (*head)
2485 (*tail)->next = h;
2486 else
2487 *head = h;
2488 h->prev = *tail;
2489 *tail = t;
2490 }
2491 }
2492
2493
2494 /* Prepend the list of glyph strings with head H and tail T to the
2495 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2496 result. */
2497
2498 static INLINE void
2499 x_prepend_glyph_string_lists (head, tail, h, t)
2500 struct glyph_string **head, **tail;
2501 struct glyph_string *h, *t;
2502 {
2503 if (h)
2504 {
2505 if (*head)
2506 (*head)->prev = t;
2507 else
2508 *tail = t;
2509 t->next = *head;
2510 *head = h;
2511 }
2512 }
2513
2514
2515 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2516 Set *HEAD and *TAIL to the resulting list. */
2517
2518 static INLINE void
2519 x_append_glyph_string (head, tail, s)
2520 struct glyph_string **head, **tail;
2521 struct glyph_string *s;
2522 {
2523 s->next = s->prev = NULL;
2524 x_append_glyph_string_lists (head, tail, s, s);
2525 }
2526
2527
2528 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2529 face. */
2530
2531 static void
2532 x_set_cursor_gc (s)
2533 struct glyph_string *s;
2534 {
2535 if (s->font == FRAME_FONT (s->f)
2536 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2537 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2538 && !s->cmp)
2539 s->gc = s->f->output_data.x->cursor_gc;
2540 else
2541 {
2542 /* Cursor on non-default face: must merge. */
2543 XGCValues xgcv;
2544 unsigned long mask;
2545
2546 xgcv.background = s->f->output_data.x->cursor_pixel;
2547 xgcv.foreground = s->face->background;
2548
2549 /* If the glyph would be invisible, try a different foreground. */
2550 if (xgcv.foreground == xgcv.background)
2551 xgcv.foreground = s->face->foreground;
2552 if (xgcv.foreground == xgcv.background)
2553 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2554 if (xgcv.foreground == xgcv.background)
2555 xgcv.foreground = s->face->foreground;
2556
2557 /* Make sure the cursor is distinct from text in this face. */
2558 if (xgcv.background == s->face->background
2559 && xgcv.foreground == s->face->foreground)
2560 {
2561 xgcv.background = s->face->foreground;
2562 xgcv.foreground = s->face->background;
2563 }
2564
2565 IF_DEBUG (x_check_font (s->f, s->font));
2566 xgcv.font = s->font->fid;
2567 xgcv.graphics_exposures = False;
2568 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2569
2570 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2571 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2572 mask, &xgcv);
2573 else
2574 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2575 = XCreateGC (s->display, s->window, mask, &xgcv);
2576
2577 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2578 }
2579 }
2580
2581
2582 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2583
2584 static void
2585 x_set_mouse_face_gc (s)
2586 struct glyph_string *s;
2587 {
2588 int face_id;
2589
2590 /* What face has to be used for the mouse face? */
2591 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2592 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2593 s->face = FACE_FROM_ID (s->f, face_id);
2594 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2595
2596 /* If font in this face is same as S->font, use it. */
2597 if (s->font == s->face->font)
2598 s->gc = s->face->gc;
2599 else
2600 {
2601 /* Otherwise construct scratch_cursor_gc with values from FACE
2602 but font FONT. */
2603 XGCValues xgcv;
2604 unsigned long mask;
2605
2606 xgcv.background = s->face->background;
2607 xgcv.foreground = s->face->foreground;
2608 IF_DEBUG (x_check_font (s->f, s->font));
2609 xgcv.font = s->font->fid;
2610 xgcv.graphics_exposures = False;
2611 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2612
2613 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2614 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2615 mask, &xgcv);
2616 else
2617 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2618 = XCreateGC (s->display, s->window, mask, &xgcv);
2619
2620 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2621 }
2622
2623 xassert (s->gc != 0);
2624 }
2625
2626
2627 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2628 Faces to use in the mode line have already been computed when the
2629 matrix was built, so there isn't much to do, here. */
2630
2631 static INLINE void
2632 x_set_mode_line_face_gc (s)
2633 struct glyph_string *s;
2634 {
2635 s->gc = s->face->gc;
2636 xassert (s->gc != 0);
2637 }
2638
2639
2640 /* Set S->gc of glyph string S for drawing that glyph string. Set
2641 S->stippled_p to a non-zero value if the face of S has a stipple
2642 pattern. */
2643
2644 static INLINE void
2645 x_set_glyph_string_gc (s)
2646 struct glyph_string *s;
2647 {
2648 if (s->hl == DRAW_NORMAL_TEXT)
2649 {
2650 s->gc = s->face->gc;
2651 s->stippled_p = s->face->stipple != 0;
2652 }
2653 else if (s->hl == DRAW_INVERSE_VIDEO)
2654 {
2655 x_set_mode_line_face_gc (s);
2656 s->stippled_p = s->face->stipple != 0;
2657 }
2658 else if (s->hl == DRAW_CURSOR)
2659 {
2660 x_set_cursor_gc (s);
2661 s->stippled_p = 0;
2662 }
2663 else if (s->hl == DRAW_MOUSE_FACE)
2664 {
2665 x_set_mouse_face_gc (s);
2666 s->stippled_p = s->face->stipple != 0;
2667 }
2668 else if (s->hl == DRAW_IMAGE_RAISED
2669 || s->hl == DRAW_IMAGE_SUNKEN)
2670 {
2671 s->gc = s->face->gc;
2672 s->stippled_p = s->face->stipple != 0;
2673 }
2674 else
2675 {
2676 s->gc = s->face->gc;
2677 s->stippled_p = s->face->stipple != 0;
2678 }
2679
2680 /* GC must have been set. */
2681 xassert (s->gc != 0);
2682 }
2683
2684
2685 /* Return in *R the clipping rectangle for glyph string S. */
2686
2687 static void
2688 x_get_glyph_string_clip_rect (s, r)
2689 struct glyph_string *s;
2690 XRectangle *r;
2691 {
2692 if (s->row->full_width_p)
2693 {
2694 /* Draw full-width. X coordinates are relative to S->w->left. */
2695 int canon_x = CANON_X_UNIT (s->f);
2696
2697 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2698 r->width = XFASTINT (s->w->width) * canon_x;
2699
2700 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2701 {
2702 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2703 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2704 r->x -= width;
2705 }
2706
2707 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2708
2709 /* Unless displaying a mode or menu bar line, which are always
2710 fully visible, clip to the visible part of the row. */
2711 if (s->w->pseudo_window_p)
2712 r->height = s->row->visible_height;
2713 else
2714 r->height = s->height;
2715 }
2716 else
2717 {
2718 /* This is a text line that may be partially visible. */
2719 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2720 r->width = window_box_width (s->w, s->area);
2721 r->height = s->row->visible_height;
2722 }
2723
2724 /* Don't use S->y for clipping because it doesn't take partially
2725 visible lines into account. For example, it can be negative for
2726 partially visible lines at the top of a window. */
2727 if (!s->row->full_width_p
2728 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2729 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2730 else
2731 r->y = max (0, s->row->y);
2732
2733 /* If drawing a tool-bar window, draw it over the internal border
2734 at the top of the window. */
2735 if (s->w == XWINDOW (s->f->tool_bar_window))
2736 r->y -= s->f->output_data.x->internal_border_width;
2737
2738 /* If S draws overlapping rows, it's sufficient to use the top and
2739 bottom of the window for clipping because this glyph string
2740 intentionally draws over other lines. */
2741 if (s->for_overlaps_p)
2742 {
2743 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2744 r->height = window_text_bottom_y (s->w) - r->y;
2745 }
2746
2747 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2748 }
2749
2750
2751 /* Set clipping for output of glyph string S. S may be part of a mode
2752 line or menu if we don't have X toolkit support. */
2753
2754 static INLINE void
2755 x_set_glyph_string_clipping (s)
2756 struct glyph_string *s;
2757 {
2758 XRectangle r;
2759 x_get_glyph_string_clip_rect (s, &r);
2760 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2761 }
2762
2763
2764 /* Compute left and right overhang of glyph string S. If S is a glyph
2765 string for a composition, assume overhangs don't exist. */
2766
2767 static INLINE void
2768 x_compute_glyph_string_overhangs (s)
2769 struct glyph_string *s;
2770 {
2771 if (s->cmp == NULL
2772 && s->first_glyph->type == CHAR_GLYPH)
2773 {
2774 XCharStruct cs;
2775 int direction, font_ascent, font_descent;
2776 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2777 &font_ascent, &font_descent, &cs);
2778 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2779 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2780 }
2781 }
2782
2783
2784 /* Compute overhangs and x-positions for glyph string S and its
2785 predecessors, or successors. X is the starting x-position for S.
2786 BACKWARD_P non-zero means process predecessors. */
2787
2788 static void
2789 x_compute_overhangs_and_x (s, x, backward_p)
2790 struct glyph_string *s;
2791 int x;
2792 int backward_p;
2793 {
2794 if (backward_p)
2795 {
2796 while (s)
2797 {
2798 x_compute_glyph_string_overhangs (s);
2799 x -= s->width;
2800 s->x = x;
2801 s = s->prev;
2802 }
2803 }
2804 else
2805 {
2806 while (s)
2807 {
2808 x_compute_glyph_string_overhangs (s);
2809 s->x = x;
2810 x += s->width;
2811 s = s->next;
2812 }
2813 }
2814 }
2815
2816
2817 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2818 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2819 assumed to be zero. */
2820
2821 static void
2822 x_get_glyph_overhangs (glyph, f, left, right)
2823 struct glyph *glyph;
2824 struct frame *f;
2825 int *left, *right;
2826 {
2827 int c;
2828
2829 *left = *right = 0;
2830
2831 if (glyph->type == CHAR_GLYPH)
2832 {
2833 XFontStruct *font;
2834 struct face *face;
2835 struct font_info *font_info;
2836 XChar2b char2b;
2837
2838 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2839 font = face->font;
2840 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2841 if (font)
2842 {
2843 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2844
2845 if (pcm->rbearing > pcm->width)
2846 *right = pcm->rbearing - pcm->width;
2847 if (pcm->lbearing < 0)
2848 *left = -pcm->lbearing;
2849 }
2850 }
2851 }
2852
2853
2854 /* Return the index of the first glyph preceding glyph string S that
2855 is overwritten by S because of S's left overhang. Value is -1
2856 if no glyphs are overwritten. */
2857
2858 static int
2859 x_left_overwritten (s)
2860 struct glyph_string *s;
2861 {
2862 int k;
2863
2864 if (s->left_overhang)
2865 {
2866 int x = 0, i;
2867 struct glyph *glyphs = s->row->glyphs[s->area];
2868 int first = s->first_glyph - glyphs;
2869
2870 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2871 x -= glyphs[i].pixel_width;
2872
2873 k = i + 1;
2874 }
2875 else
2876 k = -1;
2877
2878 return k;
2879 }
2880
2881
2882 /* Return the index of the first glyph preceding glyph string S that
2883 is overwriting S because of its right overhang. Value is -1 if no
2884 glyph in front of S overwrites S. */
2885
2886 static int
2887 x_left_overwriting (s)
2888 struct glyph_string *s;
2889 {
2890 int i, k, x;
2891 struct glyph *glyphs = s->row->glyphs[s->area];
2892 int first = s->first_glyph - glyphs;
2893
2894 k = -1;
2895 x = 0;
2896 for (i = first - 1; i >= 0; --i)
2897 {
2898 int left, right;
2899 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2900 if (x + right > 0)
2901 k = i;
2902 x -= glyphs[i].pixel_width;
2903 }
2904
2905 return k;
2906 }
2907
2908
2909 /* Return the index of the last glyph following glyph string S that is
2910 not overwritten by S because of S's right overhang. Value is -1 if
2911 no such glyph is found. */
2912
2913 static int
2914 x_right_overwritten (s)
2915 struct glyph_string *s;
2916 {
2917 int k = -1;
2918
2919 if (s->right_overhang)
2920 {
2921 int x = 0, i;
2922 struct glyph *glyphs = s->row->glyphs[s->area];
2923 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2924 int end = s->row->used[s->area];
2925
2926 for (i = first; i < end && s->right_overhang > x; ++i)
2927 x += glyphs[i].pixel_width;
2928
2929 k = i;
2930 }
2931
2932 return k;
2933 }
2934
2935
2936 /* Return the index of the last glyph following glyph string S that
2937 overwrites S because of its left overhang. Value is negative
2938 if no such glyph is found. */
2939
2940 static int
2941 x_right_overwriting (s)
2942 struct glyph_string *s;
2943 {
2944 int i, k, x;
2945 int end = s->row->used[s->area];
2946 struct glyph *glyphs = s->row->glyphs[s->area];
2947 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2948
2949 k = -1;
2950 x = 0;
2951 for (i = first; i < end; ++i)
2952 {
2953 int left, right;
2954 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2955 if (x - left < 0)
2956 k = i;
2957 x += glyphs[i].pixel_width;
2958 }
2959
2960 return k;
2961 }
2962
2963
2964 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2965
2966 static INLINE void
2967 x_clear_glyph_string_rect (s, x, y, w, h)
2968 struct glyph_string *s;
2969 int x, y, w, h;
2970 {
2971 XGCValues xgcv;
2972 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2973 XSetForeground (s->display, s->gc, xgcv.background);
2974 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2975 XSetForeground (s->display, s->gc, xgcv.foreground);
2976 }
2977
2978
2979 /* Draw the background of glyph_string S. If S->background_filled_p
2980 is non-zero don't draw it. FORCE_P non-zero means draw the
2981 background even if it wouldn't be drawn normally. This is used
2982 when a string preceding S draws into the background of S, or S
2983 contains the first component of a composition. */
2984
2985 static void
2986 x_draw_glyph_string_background (s, force_p)
2987 struct glyph_string *s;
2988 int force_p;
2989 {
2990 /* Nothing to do if background has already been drawn or if it
2991 shouldn't be drawn in the first place. */
2992 if (!s->background_filled_p)
2993 {
2994 if (s->stippled_p)
2995 {
2996 /* Fill background with a stipple pattern. */
2997 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2998 XFillRectangle (s->display, s->window, s->gc, s->x,
2999 s->y + s->face->box_line_width,
3000 s->background_width,
3001 s->height - 2 * s->face->box_line_width);
3002 XSetFillStyle (s->display, s->gc, FillSolid);
3003 s->background_filled_p = 1;
3004 }
3005 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3006 || s->font_not_found_p
3007 || s->extends_to_end_of_line_p
3008 || force_p)
3009 {
3010 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3011 s->background_width,
3012 s->height - 2 * s->face->box_line_width);
3013 s->background_filled_p = 1;
3014 }
3015 }
3016 }
3017
3018
3019 /* Draw the foreground of glyph string S. */
3020
3021 static void
3022 x_draw_glyph_string_foreground (s)
3023 struct glyph_string *s;
3024 {
3025 int i, x;
3026
3027 /* If first glyph of S has a left box line, start drawing the text
3028 of S to the right of that box line. */
3029 if (s->face->box != FACE_NO_BOX
3030 && s->first_glyph->left_box_line_p)
3031 x = s->x + s->face->box_line_width;
3032 else
3033 x = s->x;
3034
3035 /* Draw characters of S as rectangles if S's font could not be
3036 loaded. */
3037 if (s->font_not_found_p)
3038 {
3039 for (i = 0; i < s->nchars; ++i)
3040 {
3041 struct glyph *g = s->first_glyph + i;
3042 XDrawRectangle (s->display, s->window,
3043 s->gc, x, s->y, g->pixel_width - 1,
3044 s->height - 1);
3045 x += g->pixel_width;
3046 }
3047 }
3048 else
3049 {
3050 char *char1b = (char *) s->char2b;
3051 int boff = s->font_info->baseline_offset;
3052
3053 if (s->font_info->vertical_centering)
3054 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3055
3056 /* If we can use 8-bit functions, condense S->char2b. */
3057 if (!s->two_byte_p)
3058 for (i = 0; i < s->nchars; ++i)
3059 char1b[i] = s->char2b[i].byte2;
3060
3061 /* Draw text with XDrawString if background has already been
3062 filled. Otherwise, use XDrawImageString. (Note that
3063 XDrawImageString is usually faster than XDrawString.) Always
3064 use XDrawImageString when drawing the cursor so that there is
3065 no chance that characters under a box cursor are invisible. */
3066 if (s->for_overlaps_p
3067 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3068 {
3069 /* Draw characters with 16-bit or 8-bit functions. */
3070 if (s->two_byte_p)
3071 XDrawString16 (s->display, s->window, s->gc, x,
3072 s->ybase - boff, s->char2b, s->nchars);
3073 else
3074 XDrawString (s->display, s->window, s->gc, x,
3075 s->ybase - boff, char1b, s->nchars);
3076 }
3077 else
3078 {
3079 if (s->two_byte_p)
3080 XDrawImageString16 (s->display, s->window, s->gc, x,
3081 s->ybase - boff, s->char2b, s->nchars);
3082 else
3083 XDrawImageString (s->display, s->window, s->gc, x,
3084 s->ybase - boff, char1b, s->nchars);
3085 }
3086 }
3087 }
3088
3089 /* Draw the foreground of composite glyph string S. */
3090
3091 static void
3092 x_draw_composite_glyph_string_foreground (s)
3093 struct glyph_string *s;
3094 {
3095 int i, x;
3096
3097 /* If first glyph of S has a left box line, start drawing the text
3098 of S to the right of that box line. */
3099 if (s->face->box != FACE_NO_BOX
3100 && s->first_glyph->left_box_line_p)
3101 x = s->x + s->face->box_line_width;
3102 else
3103 x = s->x;
3104
3105 /* S is a glyph string for a composition. S->gidx is the index of
3106 the first character drawn for glyphs of this composition.
3107 S->gidx == 0 means we are drawing the very first character of
3108 this composition. */
3109
3110 /* Draw a rectangle for the composition if the font for the very
3111 first character of the composition could not be loaded. */
3112 if (s->font_not_found_p)
3113 {
3114 if (s->gidx == 0)
3115 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3116 s->width - 1, s->height - 1);
3117 }
3118 else
3119 {
3120 for (i = 0; i < s->nchars; i++, ++s->gidx)
3121 XDrawString16 (s->display, s->window, s->gc,
3122 x + s->cmp->offsets[s->gidx * 2],
3123 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3124 s->char2b + i, 1);
3125 }
3126 }
3127
3128
3129 #ifdef USE_X_TOOLKIT
3130
3131 /* Allocate the color COLOR->pixel on the screen and display of
3132 widget WIDGET in colormap CMAP. If an exact match cannot be
3133 allocated, try the nearest color available. Value is non-zero
3134 if successful. This is called from lwlib. */
3135
3136 int
3137 x_alloc_nearest_color_for_widget (widget, cmap, color)
3138 Widget widget;
3139 Colormap cmap;
3140 XColor *color;
3141 {
3142 struct frame *f;
3143 struct x_display_info *dpyinfo;
3144 Lisp_Object tail;
3145
3146 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3147
3148 /* Find the top-level shell of the widget. Note that this function
3149 can be called when the widget is not yet realized, so XtWindow
3150 (widget) == 0. That's the reason we can't simply use
3151 x_any_window_to_frame. */
3152 while (!XtIsTopLevelShell (widget))
3153 widget = XtParent (widget);
3154
3155 /* Look for a frame with that top-level widget. Allocate the color
3156 on that frame to get the right gamma correction value. */
3157 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3158 if (GC_FRAMEP (XCAR (tail))
3159 && (f = XFRAME (XCAR (tail)),
3160 (f->output_data.nothing != 1
3161 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3162 && f->output_data.x->widget == widget)
3163 return x_alloc_nearest_color (f, cmap, color);
3164
3165 abort ();
3166 }
3167
3168 #endif /* USE_X_TOOLKIT */
3169
3170
3171 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3172 CMAP. If an exact match can't be allocated, try the nearest color
3173 available. Value is non-zero if successful. Set *COLOR to the
3174 color allocated. */
3175
3176 int
3177 x_alloc_nearest_color (f, cmap, color)
3178 struct frame *f;
3179 Colormap cmap;
3180 XColor *color;
3181 {
3182 Display *display = FRAME_X_DISPLAY (f);
3183 Screen *screen = FRAME_X_SCREEN (f);
3184 int rc;
3185
3186 gamma_correct (f, color);
3187 rc = XAllocColor (display, cmap, color);
3188 if (rc == 0)
3189 {
3190 /* If we got to this point, the colormap is full, so we're going
3191 to try to get the next closest color. The algorithm used is
3192 a least-squares matching, which is what X uses for closest
3193 color matching with StaticColor visuals. */
3194 int nearest, i;
3195 unsigned long nearest_delta = ~0;
3196 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3197 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3198
3199 for (i = 0; i < ncells; ++i)
3200 cells[i].pixel = i;
3201 XQueryColors (display, cmap, cells, ncells);
3202
3203 for (nearest = i = 0; i < ncells; ++i)
3204 {
3205 long dred = (color->red >> 8) - (cells[i].red >> 8);
3206 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3207 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3208 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3209
3210 if (delta < nearest_delta)
3211 {
3212 nearest = i;
3213 nearest_delta = delta;
3214 }
3215 }
3216
3217 color->red = cells[nearest].red;
3218 color->green = cells[nearest].green;
3219 color->blue = cells[nearest].blue;
3220 rc = XAllocColor (display, cmap, color);
3221 }
3222
3223 return rc;
3224 }
3225
3226
3227 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3228 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3229 If this produces the same color as PIXEL, try a color where all RGB
3230 values have DELTA added. Return the allocated color in *PIXEL.
3231 DISPLAY is the X display, CMAP is the colormap to operate on.
3232 Value is non-zero if successful. */
3233
3234 static int
3235 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3236 struct frame *f;
3237 Display *display;
3238 Colormap cmap;
3239 unsigned long *pixel;
3240 double factor;
3241 int delta;
3242 {
3243 XColor color, new;
3244 int success_p;
3245
3246 /* Get RGB color values. */
3247 color.pixel = *pixel;
3248 XQueryColor (display, cmap, &color);
3249
3250 /* Change RGB values by specified FACTOR. Avoid overflow! */
3251 xassert (factor >= 0);
3252 new.red = min (0xffff, factor * color.red);
3253 new.green = min (0xffff, factor * color.green);
3254 new.blue = min (0xffff, factor * color.blue);
3255
3256 /* Try to allocate the color. */
3257 success_p = x_alloc_nearest_color (f, cmap, &new);
3258 if (success_p)
3259 {
3260 if (new.pixel == *pixel)
3261 {
3262 /* If we end up with the same color as before, try adding
3263 delta to the RGB values. */
3264 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3265
3266 /* If display has an immutable color map, freeing colors is
3267 not necessary and some servers don't allow it. So don't
3268 do it. */
3269 if (class != StaticColor
3270 && class != StaticGray
3271 && class != TrueColor)
3272 XFreeColors (display, cmap, &new.pixel, 1, 0);
3273
3274 new.red = min (0xffff, delta + color.red);
3275 new.green = min (0xffff, delta + color.green);
3276 new.blue = min (0xffff, delta + color.blue);
3277 success_p = x_alloc_nearest_color (f, cmap, &new);
3278 }
3279 else
3280 success_p = 1;
3281 *pixel = new.pixel;
3282 }
3283
3284 return success_p;
3285 }
3286
3287
3288 /* Set up the foreground color for drawing relief lines of glyph
3289 string S. RELIEF is a pointer to a struct relief containing the GC
3290 with which lines will be drawn. Use a color that is FACTOR or
3291 DELTA lighter or darker than the relief's background which is found
3292 in S->f->output_data.x->relief_background. If such a color cannot
3293 be allocated, use DEFAULT_PIXEL, instead. */
3294
3295 static void
3296 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3297 struct frame *f;
3298 struct relief *relief;
3299 double factor;
3300 int delta;
3301 unsigned long default_pixel;
3302 {
3303 XGCValues xgcv;
3304 struct x_output *di = f->output_data.x;
3305 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3306 unsigned long pixel;
3307 unsigned long background = di->relief_background;
3308 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
3309 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3310 Display *dpy = FRAME_X_DISPLAY (f);
3311
3312 xgcv.graphics_exposures = False;
3313 xgcv.line_width = 1;
3314
3315 /* Free previously allocated color. The color cell will be reused
3316 when it has been freed as many times as it was allocated, so this
3317 doesn't affect faces using the same colors. */
3318 if (relief->gc
3319 && relief->allocated_p)
3320 {
3321 /* If display has an immutable color map, freeing colors is not
3322 necessary and some servers don't allow it. So don't do it. */
3323 int class = dpyinfo->visual->class;
3324 if (class != StaticColor
3325 && class != StaticGray
3326 && class != TrueColor)
3327 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
3328 relief->allocated_p = 0;
3329 }
3330
3331 /* Allocate new color. */
3332 xgcv.foreground = default_pixel;
3333 pixel = background;
3334 if (dpyinfo->n_planes != 1
3335 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3336 {
3337 relief->allocated_p = 1;
3338 xgcv.foreground = relief->pixel = pixel;
3339 }
3340
3341 if (relief->gc == 0)
3342 {
3343 xgcv.stipple = dpyinfo->gray;
3344 mask |= GCStipple;
3345 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3346 }
3347 else
3348 XChangeGC (dpy, relief->gc, mask, &xgcv);
3349 }
3350
3351
3352 /* Set up colors for the relief lines around glyph string S. */
3353
3354 static void
3355 x_setup_relief_colors (s)
3356 struct glyph_string *s;
3357 {
3358 struct x_output *di = s->f->output_data.x;
3359 unsigned long color;
3360
3361 if (s->face->use_box_color_for_shadows_p)
3362 color = s->face->box_color;
3363 else
3364 {
3365 XGCValues xgcv;
3366
3367 /* Get the background color of the face. */
3368 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3369 color = xgcv.background;
3370 }
3371
3372 if (di->white_relief.gc == 0
3373 || color != di->relief_background)
3374 {
3375 di->relief_background = color;
3376 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3377 WHITE_PIX_DEFAULT (s->f));
3378 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3379 BLACK_PIX_DEFAULT (s->f));
3380 }
3381 }
3382
3383
3384 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3385 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3386 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3387 relief. LEFT_P non-zero means draw a relief on the left side of
3388 the rectangle. RIGHT_P non-zero means draw a relief on the right
3389 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3390 when drawing. */
3391
3392 static void
3393 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3394 raised_p, left_p, right_p, clip_rect)
3395 struct frame *f;
3396 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3397 XRectangle *clip_rect;
3398 {
3399 int i;
3400 GC gc;
3401
3402 if (raised_p)
3403 gc = f->output_data.x->white_relief.gc;
3404 else
3405 gc = f->output_data.x->black_relief.gc;
3406 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3407
3408 /* Top. */
3409 for (i = 0; i < width; ++i)
3410 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3411 left_x + i * left_p, top_y + i,
3412 right_x + 1 - i * right_p, top_y + i);
3413
3414 /* Left. */
3415 if (left_p)
3416 for (i = 0; i < width; ++i)
3417 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3418 left_x + i, top_y + i, left_x + i, bottom_y - i);
3419
3420 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3421 if (raised_p)
3422 gc = f->output_data.x->black_relief.gc;
3423 else
3424 gc = f->output_data.x->white_relief.gc;
3425 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3426
3427 /* Bottom. */
3428 for (i = 0; i < width; ++i)
3429 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3430 left_x + i * left_p, bottom_y - i,
3431 right_x + 1 - i * right_p, bottom_y - i);
3432
3433 /* Right. */
3434 if (right_p)
3435 for (i = 0; i < width; ++i)
3436 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3437 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3438
3439 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3440 }
3441
3442
3443 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3444 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3445 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3446 left side of the rectangle. RIGHT_P non-zero means draw a line
3447 on the right side of the rectangle. CLIP_RECT is the clipping
3448 rectangle to use when drawing. */
3449
3450 static void
3451 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3452 left_p, right_p, clip_rect)
3453 struct glyph_string *s;
3454 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3455 XRectangle *clip_rect;
3456 {
3457 XGCValues xgcv;
3458
3459 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3460 XSetForeground (s->display, s->gc, s->face->box_color);
3461 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3462
3463 /* Top. */
3464 XFillRectangle (s->display, s->window, s->gc,
3465 left_x, top_y, right_x - left_x, width);
3466
3467 /* Left. */
3468 if (left_p)
3469 XFillRectangle (s->display, s->window, s->gc,
3470 left_x, top_y, width, bottom_y - top_y);
3471
3472 /* Bottom. */
3473 XFillRectangle (s->display, s->window, s->gc,
3474 left_x, bottom_y - width, right_x - left_x, width);
3475
3476 /* Right. */
3477 if (right_p)
3478 XFillRectangle (s->display, s->window, s->gc,
3479 right_x - width, top_y, width, bottom_y - top_y);
3480
3481 XSetForeground (s->display, s->gc, xgcv.foreground);
3482 XSetClipMask (s->display, s->gc, None);
3483 }
3484
3485
3486 /* Draw a box around glyph string S. */
3487
3488 static void
3489 x_draw_glyph_string_box (s)
3490 struct glyph_string *s;
3491 {
3492 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3493 int left_p, right_p;
3494 struct glyph *last_glyph;
3495 XRectangle clip_rect;
3496
3497 last_x = window_box_right (s->w, s->area);
3498 if (s->row->full_width_p
3499 && !s->w->pseudo_window_p)
3500 {
3501 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3502 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3503 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3504 }
3505
3506 /* The glyph that may have a right box line. */
3507 last_glyph = (s->cmp || s->img
3508 ? s->first_glyph
3509 : s->first_glyph + s->nchars - 1);
3510
3511 width = s->face->box_line_width;
3512 raised_p = s->face->box == FACE_RAISED_BOX;
3513 left_x = s->x;
3514 right_x = ((s->row->full_width_p
3515 ? last_x - 1
3516 : min (last_x, s->x + s->background_width) - 1));
3517 top_y = s->y;
3518 bottom_y = top_y + s->height - 1;
3519
3520 left_p = (s->first_glyph->left_box_line_p
3521 || (s->hl == DRAW_MOUSE_FACE
3522 && (s->prev == NULL
3523 || s->prev->hl != s->hl)));
3524 right_p = (last_glyph->right_box_line_p
3525 || (s->hl == DRAW_MOUSE_FACE
3526 && (s->next == NULL
3527 || s->next->hl != s->hl)));
3528
3529 x_get_glyph_string_clip_rect (s, &clip_rect);
3530
3531 if (s->face->box == FACE_SIMPLE_BOX)
3532 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3533 left_p, right_p, &clip_rect);
3534 else
3535 {
3536 x_setup_relief_colors (s);
3537 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3538 width, raised_p, left_p, right_p, &clip_rect);
3539 }
3540 }
3541
3542
3543 /* Draw foreground of image glyph string S. */
3544
3545 static void
3546 x_draw_image_foreground (s)
3547 struct glyph_string *s;
3548 {
3549 int x;
3550 int y = s->ybase - IMAGE_ASCENT (s->img);
3551
3552 /* If first glyph of S has a left box line, start drawing it to the
3553 right of that line. */
3554 if (s->face->box != FACE_NO_BOX
3555 && s->first_glyph->left_box_line_p)
3556 x = s->x + s->face->box_line_width;
3557 else
3558 x = s->x;
3559
3560 /* If there is a margin around the image, adjust x- and y-position
3561 by that margin. */
3562 if (s->img->margin)
3563 {
3564 x += s->img->margin;
3565 y += s->img->margin;
3566 }
3567
3568 if (s->img->pixmap)
3569 {
3570 if (s->img->mask)
3571 {
3572 /* We can't set both a clip mask and use XSetClipRectangles
3573 because the latter also sets a clip mask. We also can't
3574 trust on the shape extension to be available
3575 (XShapeCombineRegion). So, compute the rectangle to draw
3576 manually. */
3577 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3578 | GCFunction);
3579 XGCValues xgcv;
3580 XRectangle clip_rect, image_rect, r;
3581
3582 xgcv.clip_mask = s->img->mask;
3583 xgcv.clip_x_origin = x;
3584 xgcv.clip_y_origin = y;
3585 xgcv.function = GXcopy;
3586 XChangeGC (s->display, s->gc, mask, &xgcv);
3587
3588 x_get_glyph_string_clip_rect (s, &clip_rect);
3589 image_rect.x = x;
3590 image_rect.y = y;
3591 image_rect.width = s->img->width;
3592 image_rect.height = s->img->height;
3593 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3594 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3595 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3596 }
3597 else
3598 {
3599 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3600 0, 0, s->img->width, s->img->height, x, y);
3601
3602 /* When the image has a mask, we can expect that at
3603 least part of a mouse highlight or a block cursor will
3604 be visible. If the image doesn't have a mask, make
3605 a block cursor visible by drawing a rectangle around
3606 the image. I believe it's looking better if we do
3607 nothing here for mouse-face. */
3608 if (s->hl == DRAW_CURSOR)
3609 XDrawRectangle (s->display, s->window, s->gc, x, y,
3610 s->img->width - 1, s->img->height - 1);
3611 }
3612 }
3613 else
3614 /* Draw a rectangle if image could not be loaded. */
3615 XDrawRectangle (s->display, s->window, s->gc, x, y,
3616 s->img->width - 1, s->img->height - 1);
3617 }
3618
3619
3620 /* Draw a relief around the image glyph string S. */
3621
3622 static void
3623 x_draw_image_relief (s)
3624 struct glyph_string *s;
3625 {
3626 int x0, y0, x1, y1, thick, raised_p;
3627 XRectangle r;
3628 int x;
3629 int y = s->ybase - IMAGE_ASCENT (s->img);
3630
3631 /* If first glyph of S has a left box line, start drawing it to the
3632 right of that line. */
3633 if (s->face->box != FACE_NO_BOX
3634 && s->first_glyph->left_box_line_p)
3635 x = s->x + s->face->box_line_width;
3636 else
3637 x = s->x;
3638
3639 /* If there is a margin around the image, adjust x- and y-position
3640 by that margin. */
3641 if (s->img->margin)
3642 {
3643 x += s->img->margin;
3644 y += s->img->margin;
3645 }
3646
3647 if (s->hl == DRAW_IMAGE_SUNKEN
3648 || s->hl == DRAW_IMAGE_RAISED)
3649 {
3650 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3651 raised_p = s->hl == DRAW_IMAGE_RAISED;
3652 }
3653 else
3654 {
3655 thick = abs (s->img->relief);
3656 raised_p = s->img->relief > 0;
3657 }
3658
3659 x0 = x - thick;
3660 y0 = y - thick;
3661 x1 = x + s->img->width + thick - 1;
3662 y1 = y + s->img->height + thick - 1;
3663
3664 x_setup_relief_colors (s);
3665 x_get_glyph_string_clip_rect (s, &r);
3666 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3667 }
3668
3669
3670 /* Draw the foreground of image glyph string S to PIXMAP. */
3671
3672 static void
3673 x_draw_image_foreground_1 (s, pixmap)
3674 struct glyph_string *s;
3675 Pixmap pixmap;
3676 {
3677 int x;
3678 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3679
3680 /* If first glyph of S has a left box line, start drawing it to the
3681 right of that line. */
3682 if (s->face->box != FACE_NO_BOX
3683 && s->first_glyph->left_box_line_p)
3684 x = s->face->box_line_width;
3685 else
3686 x = 0;
3687
3688 /* If there is a margin around the image, adjust x- and y-position
3689 by that margin. */
3690 if (s->img->margin)
3691 {
3692 x += s->img->margin;
3693 y += s->img->margin;
3694 }
3695
3696 if (s->img->pixmap)
3697 {
3698 if (s->img->mask)
3699 {
3700 /* We can't set both a clip mask and use XSetClipRectangles
3701 because the latter also sets a clip mask. We also can't
3702 trust on the shape extension to be available
3703 (XShapeCombineRegion). So, compute the rectangle to draw
3704 manually. */
3705 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3706 | GCFunction);
3707 XGCValues xgcv;
3708
3709 xgcv.clip_mask = s->img->mask;
3710 xgcv.clip_x_origin = x;
3711 xgcv.clip_y_origin = y;
3712 xgcv.function = GXcopy;
3713 XChangeGC (s->display, s->gc, mask, &xgcv);
3714
3715 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3716 0, 0, s->img->width, s->img->height, x, y);
3717 XSetClipMask (s->display, s->gc, None);
3718 }
3719 else
3720 {
3721 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3722 0, 0, s->img->width, s->img->height, x, y);
3723
3724 /* When the image has a mask, we can expect that at
3725 least part of a mouse highlight or a block cursor will
3726 be visible. If the image doesn't have a mask, make
3727 a block cursor visible by drawing a rectangle around
3728 the image. I believe it's looking better if we do
3729 nothing here for mouse-face. */
3730 if (s->hl == DRAW_CURSOR)
3731 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3732 s->img->width - 1, s->img->height - 1);
3733 }
3734 }
3735 else
3736 /* Draw a rectangle if image could not be loaded. */
3737 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3738 s->img->width - 1, s->img->height - 1);
3739 }
3740
3741
3742 /* Draw part of the background of glyph string S. X, Y, W, and H
3743 give the rectangle to draw. */
3744
3745 static void
3746 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3747 struct glyph_string *s;
3748 int x, y, w, h;
3749 {
3750 if (s->stippled_p)
3751 {
3752 /* Fill background with a stipple pattern. */
3753 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3754 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3755 XSetFillStyle (s->display, s->gc, FillSolid);
3756 }
3757 else
3758 x_clear_glyph_string_rect (s, x, y, w, h);
3759 }
3760
3761
3762 /* Draw image glyph string S.
3763
3764 s->y
3765 s->x +-------------------------
3766 | s->face->box
3767 |
3768 | +-------------------------
3769 | | s->img->margin
3770 | |
3771 | | +-------------------
3772 | | | the image
3773
3774 */
3775
3776 static void
3777 x_draw_image_glyph_string (s)
3778 struct glyph_string *s;
3779 {
3780 int x, y;
3781 int box_line_width = s->face->box_line_width;
3782 int margin = s->img->margin;
3783 int height;
3784 Pixmap pixmap = None;
3785
3786 height = s->height - 2 * box_line_width;
3787
3788 /* Fill background with face under the image. Do it only if row is
3789 taller than image or if image has a clip mask to reduce
3790 flickering. */
3791 s->stippled_p = s->face->stipple != 0;
3792 if (height > s->img->height
3793 || margin
3794 || s->img->mask
3795 || s->img->pixmap == 0
3796 || s->width != s->background_width)
3797 {
3798 if (box_line_width && s->first_glyph->left_box_line_p)
3799 x = s->x + box_line_width;
3800 else
3801 x = s->x;
3802
3803 y = s->y + box_line_width;
3804
3805 if (s->img->mask)
3806 {
3807 /* Create a pixmap as large as the glyph string Fill it with
3808 the background color. Copy the image to it, using its
3809 mask. Copy the temporary pixmap to the display. */
3810 Screen *screen = FRAME_X_SCREEN (s->f);
3811 int depth = DefaultDepthOfScreen (screen);
3812
3813 /* Create a pixmap as large as the glyph string. */
3814 pixmap = XCreatePixmap (s->display, s->window,
3815 s->background_width,
3816 s->height, depth);
3817
3818 /* Don't clip in the following because we're working on the
3819 pixmap. */
3820 XSetClipMask (s->display, s->gc, None);
3821
3822 /* Fill the pixmap with the background color/stipple. */
3823 if (s->stippled_p)
3824 {
3825 /* Fill background with a stipple pattern. */
3826 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3827 XFillRectangle (s->display, pixmap, s->gc,
3828 0, 0, s->background_width, s->height);
3829 XSetFillStyle (s->display, s->gc, FillSolid);
3830 }
3831 else
3832 {
3833 XGCValues xgcv;
3834 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3835 &xgcv);
3836 XSetForeground (s->display, s->gc, xgcv.background);
3837 XFillRectangle (s->display, pixmap, s->gc,
3838 0, 0, s->background_width, s->height);
3839 XSetForeground (s->display, s->gc, xgcv.foreground);
3840 }
3841 }
3842 else
3843 /* Implementation idea: Is it possible to construct a mask?
3844 We could look at the color at the margins of the image, and
3845 say that this color is probably the background color of the
3846 image. */
3847 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3848
3849 s->background_filled_p = 1;
3850 }
3851
3852 /* Draw the foreground. */
3853 if (pixmap != None)
3854 {
3855 x_draw_image_foreground_1 (s, pixmap);
3856 x_set_glyph_string_clipping (s);
3857 XCopyArea (s->display, pixmap, s->window, s->gc,
3858 0, 0, s->background_width, s->height, s->x, s->y);
3859 XFreePixmap (s->display, pixmap);
3860 }
3861 else
3862 x_draw_image_foreground (s);
3863
3864 /* If we must draw a relief around the image, do it. */
3865 if (s->img->relief
3866 || s->hl == DRAW_IMAGE_RAISED
3867 || s->hl == DRAW_IMAGE_SUNKEN)
3868 x_draw_image_relief (s);
3869 }
3870
3871
3872 /* Draw stretch glyph string S. */
3873
3874 static void
3875 x_draw_stretch_glyph_string (s)
3876 struct glyph_string *s;
3877 {
3878 xassert (s->first_glyph->type == STRETCH_GLYPH);
3879 s->stippled_p = s->face->stipple != 0;
3880
3881 if (s->hl == DRAW_CURSOR
3882 && !x_stretch_cursor_p)
3883 {
3884 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3885 as wide as the stretch glyph. */
3886 int width = min (CANON_X_UNIT (s->f), s->background_width);
3887
3888 /* Draw cursor. */
3889 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3890
3891 /* Clear rest using the GC of the original non-cursor face. */
3892 if (width < s->background_width)
3893 {
3894 GC gc = s->face->gc;
3895 int x = s->x + width, y = s->y;
3896 int w = s->background_width - width, h = s->height;
3897 XRectangle r;
3898
3899 x_get_glyph_string_clip_rect (s, &r);
3900 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
3901
3902 if (s->face->stipple)
3903 {
3904 /* Fill background with a stipple pattern. */
3905 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3906 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3907 XSetFillStyle (s->display, gc, FillSolid);
3908 }
3909 else
3910 {
3911 XGCValues xgcv;
3912 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3913 XSetForeground (s->display, gc, xgcv.background);
3914 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3915 XSetForeground (s->display, gc, xgcv.foreground);
3916 }
3917 }
3918 }
3919 else
3920 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3921 s->height);
3922
3923 s->background_filled_p = 1;
3924 }
3925
3926
3927 /* Draw glyph string S. */
3928
3929 static void
3930 x_draw_glyph_string (s)
3931 struct glyph_string *s;
3932 {
3933 /* If S draws into the background of its successor, draw the
3934 background of the successor first so that S can draw into it.
3935 This makes S->next use XDrawString instead of XDrawImageString. */
3936 if (s->next && s->right_overhang && !s->for_overlaps_p)
3937 {
3938 xassert (s->next->img == NULL);
3939 x_set_glyph_string_gc (s->next);
3940 x_set_glyph_string_clipping (s->next);
3941 x_draw_glyph_string_background (s->next, 1);
3942 }
3943
3944 /* Set up S->gc, set clipping and draw S. */
3945 x_set_glyph_string_gc (s);
3946 x_set_glyph_string_clipping (s);
3947
3948 switch (s->first_glyph->type)
3949 {
3950 case IMAGE_GLYPH:
3951 x_draw_image_glyph_string (s);
3952 break;
3953
3954 case STRETCH_GLYPH:
3955 x_draw_stretch_glyph_string (s);
3956 break;
3957
3958 case CHAR_GLYPH:
3959 if (s->for_overlaps_p)
3960 s->background_filled_p = 1;
3961 else
3962 x_draw_glyph_string_background (s, 0);
3963 x_draw_glyph_string_foreground (s);
3964 break;
3965
3966 case COMPOSITE_GLYPH:
3967 if (s->for_overlaps_p || s->gidx > 0)
3968 s->background_filled_p = 1;
3969 else
3970 x_draw_glyph_string_background (s, 1);
3971 x_draw_composite_glyph_string_foreground (s);
3972 break;
3973
3974 default:
3975 abort ();
3976 }
3977
3978 if (!s->for_overlaps_p)
3979 {
3980 /* Draw underline. */
3981 if (s->face->underline_p)
3982 {
3983 unsigned long dy, h;
3984
3985 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3986 h = 1;
3987 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3988 dy = s->height - h;
3989
3990 if (s->face->underline_defaulted_p)
3991 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3992 s->width, h);
3993 else
3994 {
3995 XGCValues xgcv;
3996 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3997 XSetForeground (s->display, s->gc, s->face->underline_color);
3998 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3999 s->width, h);
4000 XSetForeground (s->display, s->gc, xgcv.foreground);
4001 }
4002 }
4003
4004 /* Draw overline. */
4005 if (s->face->overline_p)
4006 {
4007 unsigned long dy = 0, h = 1;
4008
4009 if (s->face->overline_color_defaulted_p)
4010 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4011 s->width, h);
4012 else
4013 {
4014 XGCValues xgcv;
4015 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4016 XSetForeground (s->display, s->gc, s->face->overline_color);
4017 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4018 s->width, h);
4019 XSetForeground (s->display, s->gc, xgcv.foreground);
4020 }
4021 }
4022
4023 /* Draw strike-through. */
4024 if (s->face->strike_through_p)
4025 {
4026 unsigned long h = 1;
4027 unsigned long dy = (s->height - h) / 2;
4028
4029 if (s->face->strike_through_color_defaulted_p)
4030 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4031 s->width, h);
4032 else
4033 {
4034 XGCValues xgcv;
4035 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4036 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4037 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4038 s->width, h);
4039 XSetForeground (s->display, s->gc, xgcv.foreground);
4040 }
4041 }
4042
4043 /* Draw relief. */
4044 if (s->face->box != FACE_NO_BOX)
4045 x_draw_glyph_string_box (s);
4046 }
4047
4048 /* Reset clipping. */
4049 XSetClipMask (s->display, s->gc, None);
4050 }
4051
4052
4053 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4054 struct face **, int));
4055
4056
4057 /* Load glyph string S with a composition components specified by S->cmp.
4058 FACES is an array of faces for all components of this composition.
4059 S->gidx is the index of the first component for S.
4060 OVERLAPS_P non-zero means S should draw the foreground only, and
4061 use its lines physical height for clipping.
4062
4063 Value is the index of a component not in S. */
4064
4065 static int
4066 x_fill_composite_glyph_string (s, faces, overlaps_p)
4067 struct glyph_string *s;
4068 struct face **faces;
4069 int overlaps_p;
4070 {
4071 int i;
4072
4073 xassert (s);
4074
4075 s->for_overlaps_p = overlaps_p;
4076
4077 s->face = faces[s->gidx];
4078 s->font = s->face->font;
4079 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4080
4081 /* For all glyphs of this composition, starting at the offset
4082 S->gidx, until we reach the end of the definition or encounter a
4083 glyph that requires the different face, add it to S. */
4084 ++s->nchars;
4085 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4086 ++s->nchars;
4087
4088 /* All glyph strings for the same composition has the same width,
4089 i.e. the width set for the first component of the composition. */
4090
4091 s->width = s->first_glyph->pixel_width;
4092
4093 /* If the specified font could not be loaded, use the frame's
4094 default font, but record the fact that we couldn't load it in
4095 the glyph string so that we can draw rectangles for the
4096 characters of the glyph string. */
4097 if (s->font == NULL)
4098 {
4099 s->font_not_found_p = 1;
4100 s->font = FRAME_FONT (s->f);
4101 }
4102
4103 /* Adjust base line for subscript/superscript text. */
4104 s->ybase += s->first_glyph->voffset;
4105
4106 xassert (s->face && s->face->gc);
4107
4108 /* This glyph string must always be drawn with 16-bit functions. */
4109 s->two_byte_p = 1;
4110
4111 return s->gidx + s->nchars;
4112 }
4113
4114
4115 /* Load glyph string S with a sequence characters.
4116 FACE_ID is the face id of the string. START is the index of the
4117 first glyph to consider, END is the index of the last + 1.
4118 OVERLAPS_P non-zero means S should draw the foreground only, and
4119 use its lines physical height for clipping.
4120
4121 Value is the index of the first glyph not in S. */
4122
4123 static int
4124 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4125 struct glyph_string *s;
4126 int face_id;
4127 int start, end, overlaps_p;
4128 {
4129 struct glyph *glyph, *last;
4130 int voffset;
4131
4132 xassert (s->f == XFRAME (s->w->frame));
4133 xassert (s->nchars == 0);
4134 xassert (start >= 0 && end > start);
4135
4136 s->for_overlaps_p = overlaps_p,
4137 glyph = s->row->glyphs[s->area] + start;
4138 last = s->row->glyphs[s->area] + end;
4139 voffset = glyph->voffset;
4140
4141 while (glyph < last
4142 && glyph->type == CHAR_GLYPH
4143 && glyph->voffset == voffset
4144 /* Same face id implies same charset, nowadays. */
4145 && glyph->face_id == face_id)
4146 {
4147 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4148 s->char2b + s->nchars);
4149 if (s->char2b[s->nchars].byte2 != 0)
4150 s->two_byte_p = 1;
4151
4152 ++s->nchars;
4153 xassert (s->nchars <= end - start);
4154 s->width += glyph->pixel_width;
4155 ++glyph;
4156 }
4157
4158 s->font = s->face->font;
4159 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4160
4161 /* If the specified font could not be loaded, use the frame's font,
4162 but record the fact that we couldn't load it in
4163 S->font_not_found_p so that we can draw rectangles for the
4164 characters of the glyph string. */
4165 if (s->font == NULL)
4166 {
4167 s->font_not_found_p = 1;
4168 s->font = FRAME_FONT (s->f);
4169 }
4170
4171 /* Adjust base line for subscript/superscript text. */
4172 s->ybase += voffset;
4173
4174 xassert (s->face && s->face->gc);
4175 return glyph - s->row->glyphs[s->area];
4176 }
4177
4178
4179 /* Fill glyph string S from image glyph S->first_glyph. */
4180
4181 static void
4182 x_fill_image_glyph_string (s)
4183 struct glyph_string *s;
4184 {
4185 xassert (s->first_glyph->type == IMAGE_GLYPH);
4186 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4187 xassert (s->img);
4188 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4189 s->font = s->face->font;
4190 s->width = s->first_glyph->pixel_width;
4191
4192 /* Adjust base line for subscript/superscript text. */
4193 s->ybase += s->first_glyph->voffset;
4194 }
4195
4196
4197 /* Fill glyph string S from stretch glyph S->first_glyph. */
4198
4199 static void
4200 x_fill_stretch_glyph_string (s)
4201 struct glyph_string *s;
4202 {
4203 xassert (s->first_glyph->type == STRETCH_GLYPH);
4204 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4205 s->font = s->face->font;
4206 s->width = s->first_glyph->pixel_width;
4207
4208 /* Adjust base line for subscript/superscript text. */
4209 s->ybase += s->first_glyph->voffset;
4210 }
4211
4212
4213 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4214 of XChar2b structures for S; it can't be allocated in
4215 x_init_glyph_string because it must be allocated via `alloca'. W
4216 is the window on which S is drawn. ROW and AREA are the glyph row
4217 and area within the row from which S is constructed. START is the
4218 index of the first glyph structure covered by S. HL is a
4219 face-override for drawing S. */
4220
4221 static void
4222 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4223 struct glyph_string *s;
4224 XChar2b *char2b;
4225 struct window *w;
4226 struct glyph_row *row;
4227 enum glyph_row_area area;
4228 int start;
4229 enum draw_glyphs_face hl;
4230 {
4231 bzero (s, sizeof *s);
4232 s->w = w;
4233 s->f = XFRAME (w->frame);
4234 s->display = FRAME_X_DISPLAY (s->f);
4235 s->window = FRAME_X_WINDOW (s->f);
4236 s->char2b = char2b;
4237 s->hl = hl;
4238 s->row = row;
4239 s->area = area;
4240 s->first_glyph = row->glyphs[area] + start;
4241 s->height = row->height;
4242 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4243
4244 /* Display the internal border below the tool-bar window. */
4245 if (s->w == XWINDOW (s->f->tool_bar_window))
4246 s->y -= s->f->output_data.x->internal_border_width;
4247
4248 s->ybase = s->y + row->ascent;
4249 }
4250
4251
4252 /* Set background width of glyph string S. START is the index of the
4253 first glyph following S. LAST_X is the right-most x-position + 1
4254 in the drawing area. */
4255
4256 static INLINE void
4257 x_set_glyph_string_background_width (s, start, last_x)
4258 struct glyph_string *s;
4259 int start;
4260 int last_x;
4261 {
4262 /* If the face of this glyph string has to be drawn to the end of
4263 the drawing area, set S->extends_to_end_of_line_p. */
4264 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4265
4266 if (start == s->row->used[s->area]
4267 && s->hl == DRAW_NORMAL_TEXT
4268 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4269 || s->face->background != default_face->background
4270 || s->face->stipple != default_face->stipple))
4271 s->extends_to_end_of_line_p = 1;
4272
4273 /* If S extends its face to the end of the line, set its
4274 background_width to the distance to the right edge of the drawing
4275 area. */
4276 if (s->extends_to_end_of_line_p)
4277 s->background_width = last_x - s->x + 1;
4278 else
4279 s->background_width = s->width;
4280 }
4281
4282
4283 /* Add a glyph string for a stretch glyph to the list of strings
4284 between HEAD and TAIL. START is the index of the stretch glyph in
4285 row area AREA of glyph row ROW. END is the index of the last glyph
4286 in that glyph row area. X is the current output position assigned
4287 to the new glyph string constructed. HL overrides that face of the
4288 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4289 is the right-most x-position of the drawing area. */
4290
4291 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4292 and below -- keep them on one line. */
4293 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4294 do \
4295 { \
4296 s = (struct glyph_string *) alloca (sizeof *s); \
4297 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4298 x_fill_stretch_glyph_string (s); \
4299 x_append_glyph_string (&HEAD, &TAIL, s); \
4300 ++START; \
4301 s->x = (X); \
4302 } \
4303 while (0)
4304
4305
4306 /* Add a glyph string for an image glyph to the list of strings
4307 between HEAD and TAIL. START is the index of the image glyph in
4308 row area AREA of glyph row ROW. END is the index of the last glyph
4309 in that glyph row area. X is the current output position assigned
4310 to the new glyph string constructed. HL overrides that face of the
4311 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4312 is the right-most x-position of the drawing area. */
4313
4314 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4315 do \
4316 { \
4317 s = (struct glyph_string *) alloca (sizeof *s); \
4318 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4319 x_fill_image_glyph_string (s); \
4320 x_append_glyph_string (&HEAD, &TAIL, s); \
4321 ++START; \
4322 s->x = (X); \
4323 } \
4324 while (0)
4325
4326
4327 /* Add a glyph string for a sequence of character glyphs to the list
4328 of strings between HEAD and TAIL. START is the index of the first
4329 glyph in row area AREA of glyph row ROW that is part of the new
4330 glyph string. END is the index of the last glyph in that glyph row
4331 area. X is the current output position assigned to the new glyph
4332 string constructed. HL overrides that face of the glyph; e.g. it
4333 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4334 right-most x-position of the drawing area. */
4335
4336 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4337 do \
4338 { \
4339 int c, charset, face_id; \
4340 XChar2b *char2b; \
4341 \
4342 c = (ROW)->glyphs[AREA][START].u.ch; \
4343 charset = CHAR_CHARSET (c); \
4344 face_id = (ROW)->glyphs[AREA][START].face_id; \
4345 \
4346 s = (struct glyph_string *) alloca (sizeof *s); \
4347 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4348 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4349 x_append_glyph_string (&HEAD, &TAIL, s); \
4350 s->charset = charset; \
4351 s->x = (X); \
4352 START = x_fill_glyph_string (s, face_id, START, END, \
4353 OVERLAPS_P); \
4354 } \
4355 while (0)
4356
4357
4358 /* Add a glyph string for a composite sequence to the list of strings
4359 between HEAD and TAIL. START is the index of the first glyph in
4360 row area AREA of glyph row ROW that is part of the new glyph
4361 string. END is the index of the last glyph in that glyph row area.
4362 X is the current output position assigned to the new glyph string
4363 constructed. HL overrides that face of the glyph; e.g. it is
4364 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4365 x-position of the drawing area. */
4366
4367 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4368 do { \
4369 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4370 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4371 struct composition *cmp = composition_table[cmp_id]; \
4372 int glyph_len = cmp->glyph_len; \
4373 XChar2b *char2b; \
4374 struct face **faces; \
4375 struct glyph_string *first_s = NULL; \
4376 int n; \
4377 \
4378 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4379 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4380 /* At first, fill in `char2b' and `faces'. */ \
4381 for (n = 0; n < glyph_len; n++) \
4382 { \
4383 int c = COMPOSITION_GLYPH (cmp, n); \
4384 faces[n] = x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4385 face_id, char2b + n, 1); \
4386 } \
4387 \
4388 /* Make glyph_strings for each glyph sequence that is drawable by \
4389 the same face, and append them to HEAD/TAIL. */ \
4390 for (n = 0; n < cmp->glyph_len;) \
4391 { \
4392 s = (struct glyph_string *) alloca (sizeof *s); \
4393 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4394 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4395 s->cmp = cmp; \
4396 s->gidx = n; \
4397 s->charset = 0; \
4398 s->x = (X); \
4399 \
4400 if (n == 0) \
4401 first_s = s; \
4402 \
4403 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4404 } \
4405 \
4406 ++START; \
4407 s = first_s; \
4408 } while (0)
4409
4410
4411 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4412 of AREA of glyph row ROW on window W between indices START and END.
4413 HL overrides the face for drawing glyph strings, e.g. it is
4414 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4415 x-positions of the drawing area.
4416
4417 This is an ugly monster macro construct because we must use alloca
4418 to allocate glyph strings (because x_draw_glyphs can be called
4419 asynchronously). */
4420
4421 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4422 do \
4423 { \
4424 HEAD = TAIL = NULL; \
4425 while (START < END) \
4426 { \
4427 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4428 switch (first_glyph->type) \
4429 { \
4430 case CHAR_GLYPH: \
4431 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4432 TAIL, HL, X, LAST_X, \
4433 OVERLAPS_P); \
4434 break; \
4435 \
4436 case COMPOSITE_GLYPH: \
4437 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4438 HEAD, TAIL, HL, X, LAST_X,\
4439 OVERLAPS_P); \
4440 break; \
4441 \
4442 case STRETCH_GLYPH: \
4443 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4444 HEAD, TAIL, HL, X, LAST_X); \
4445 break; \
4446 \
4447 case IMAGE_GLYPH: \
4448 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4449 TAIL, HL, X, LAST_X); \
4450 break; \
4451 \
4452 default: \
4453 abort (); \
4454 } \
4455 \
4456 x_set_glyph_string_background_width (s, START, LAST_X); \
4457 (X) += s->width; \
4458 } \
4459 } \
4460 while (0)
4461
4462
4463 /* Draw glyphs between START and END in AREA of ROW on window W,
4464 starting at x-position X. X is relative to AREA in W. HL is a
4465 face-override with the following meaning:
4466
4467 DRAW_NORMAL_TEXT draw normally
4468 DRAW_CURSOR draw in cursor face
4469 DRAW_MOUSE_FACE draw in mouse face.
4470 DRAW_INVERSE_VIDEO draw in mode line face
4471 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4472 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4473
4474 If REAL_START is non-null, return in *REAL_START the real starting
4475 position for display. This can be different from START in case
4476 overlapping glyphs must be displayed. If REAL_END is non-null,
4477 return in *REAL_END the real end position for display. This can be
4478 different from END in case overlapping glyphs must be displayed.
4479
4480 If OVERLAPS_P is non-zero, draw only the foreground of characters
4481 and clip to the physical height of ROW.
4482
4483 Value is the x-position reached, relative to AREA of W. */
4484
4485 static int
4486 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4487 overlaps_p)
4488 struct window *w;
4489 int x;
4490 struct glyph_row *row;
4491 enum glyph_row_area area;
4492 int start, end;
4493 enum draw_glyphs_face hl;
4494 int *real_start, *real_end;
4495 int overlaps_p;
4496 {
4497 struct glyph_string *head, *tail;
4498 struct glyph_string *s;
4499 int last_x, area_width;
4500 int x_reached;
4501 int i, j;
4502
4503 /* Let's rather be paranoid than getting a SEGV. */
4504 start = max (0, start);
4505 end = min (end, row->used[area]);
4506 if (real_start)
4507 *real_start = start;
4508 if (real_end)
4509 *real_end = end;
4510
4511 /* Translate X to frame coordinates. Set last_x to the right
4512 end of the drawing area. */
4513 if (row->full_width_p)
4514 {
4515 /* X is relative to the left edge of W, without scroll bars
4516 or flag areas. */
4517 struct frame *f = XFRAME (w->frame);
4518 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4519 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4520
4521 x += window_left_x;
4522 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4523 last_x = window_left_x + area_width;
4524
4525 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4526 {
4527 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4528 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4529 last_x += width;
4530 else
4531 x -= width;
4532 }
4533
4534 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4535 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4536 }
4537 else
4538 {
4539 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4540 area_width = window_box_width (w, area);
4541 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4542 }
4543
4544 /* Build a doubly-linked list of glyph_string structures between
4545 head and tail from what we have to draw. Note that the macro
4546 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4547 the reason we use a separate variable `i'. */
4548 i = start;
4549 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4550 overlaps_p);
4551 if (tail)
4552 x_reached = tail->x + tail->background_width;
4553 else
4554 x_reached = x;
4555
4556 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4557 the row, redraw some glyphs in front or following the glyph
4558 strings built above. */
4559 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4560 {
4561 int dummy_x = 0;
4562 struct glyph_string *h, *t;
4563
4564 /* Compute overhangs for all glyph strings. */
4565 for (s = head; s; s = s->next)
4566 x_compute_glyph_string_overhangs (s);
4567
4568 /* Prepend glyph strings for glyphs in front of the first glyph
4569 string that are overwritten because of the first glyph
4570 string's left overhang. The background of all strings
4571 prepended must be drawn because the first glyph string
4572 draws over it. */
4573 i = x_left_overwritten (head);
4574 if (i >= 0)
4575 {
4576 j = i;
4577 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4578 DRAW_NORMAL_TEXT, dummy_x, last_x,
4579 overlaps_p);
4580 start = i;
4581 if (real_start)
4582 *real_start = start;
4583 x_compute_overhangs_and_x (t, head->x, 1);
4584 x_prepend_glyph_string_lists (&head, &tail, h, t);
4585 }
4586
4587 /* Prepend glyph strings for glyphs in front of the first glyph
4588 string that overwrite that glyph string because of their
4589 right overhang. For these strings, only the foreground must
4590 be drawn, because it draws over the glyph string at `head'.
4591 The background must not be drawn because this would overwrite
4592 right overhangs of preceding glyphs for which no glyph
4593 strings exist. */
4594 i = x_left_overwriting (head);
4595 if (i >= 0)
4596 {
4597 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4598 DRAW_NORMAL_TEXT, dummy_x, last_x,
4599 overlaps_p);
4600 for (s = h; s; s = s->next)
4601 s->background_filled_p = 1;
4602 if (real_start)
4603 *real_start = i;
4604 x_compute_overhangs_and_x (t, head->x, 1);
4605 x_prepend_glyph_string_lists (&head, &tail, h, t);
4606 }
4607
4608 /* Append glyphs strings for glyphs following the last glyph
4609 string tail that are overwritten by tail. The background of
4610 these strings has to be drawn because tail's foreground draws
4611 over it. */
4612 i = x_right_overwritten (tail);
4613 if (i >= 0)
4614 {
4615 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4616 DRAW_NORMAL_TEXT, x, last_x,
4617 overlaps_p);
4618 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4619 x_append_glyph_string_lists (&head, &tail, h, t);
4620 if (real_end)
4621 *real_end = i;
4622 }
4623
4624 /* Append glyph strings for glyphs following the last glyph
4625 string tail that overwrite tail. The foreground of such
4626 glyphs has to be drawn because it writes into the background
4627 of tail. The background must not be drawn because it could
4628 paint over the foreground of following glyphs. */
4629 i = x_right_overwriting (tail);
4630 if (i >= 0)
4631 {
4632 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4633 DRAW_NORMAL_TEXT, x, last_x,
4634 overlaps_p);
4635 for (s = h; s; s = s->next)
4636 s->background_filled_p = 1;
4637 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4638 x_append_glyph_string_lists (&head, &tail, h, t);
4639 if (real_end)
4640 *real_end = i;
4641 }
4642 }
4643
4644 /* Draw all strings. */
4645 for (s = head; s; s = s->next)
4646 x_draw_glyph_string (s);
4647
4648 /* Value is the x-position up to which drawn, relative to AREA of W.
4649 This doesn't include parts drawn because of overhangs. */
4650 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4651 if (!row->full_width_p)
4652 {
4653 if (area > LEFT_MARGIN_AREA)
4654 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4655 if (area > TEXT_AREA)
4656 x_reached -= window_box_width (w, TEXT_AREA);
4657 }
4658 return x_reached;
4659 }
4660
4661
4662 /* Fix the display of area AREA of overlapping row ROW in window W. */
4663
4664 static void
4665 x_fix_overlapping_area (w, row, area)
4666 struct window *w;
4667 struct glyph_row *row;
4668 enum glyph_row_area area;
4669 {
4670 int i, x;
4671
4672 BLOCK_INPUT;
4673
4674 if (area == LEFT_MARGIN_AREA)
4675 x = 0;
4676 else if (area == TEXT_AREA)
4677 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4678 else
4679 x = (window_box_width (w, LEFT_MARGIN_AREA)
4680 + window_box_width (w, TEXT_AREA));
4681
4682 for (i = 0; i < row->used[area];)
4683 {
4684 if (row->glyphs[area][i].overlaps_vertically_p)
4685 {
4686 int start = i, start_x = x;
4687
4688 do
4689 {
4690 x += row->glyphs[area][i].pixel_width;
4691 ++i;
4692 }
4693 while (i < row->used[area]
4694 && row->glyphs[area][i].overlaps_vertically_p);
4695
4696 x_draw_glyphs (w, start_x, row, area, start, i,
4697 (row->inverse_p
4698 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4699 NULL, NULL, 1);
4700 }
4701 else
4702 {
4703 x += row->glyphs[area][i].pixel_width;
4704 ++i;
4705 }
4706 }
4707
4708 UNBLOCK_INPUT;
4709 }
4710
4711
4712 /* Output LEN glyphs starting at START at the nominal cursor position.
4713 Advance the nominal cursor over the text. The global variable
4714 updated_window contains the window being updated, updated_row is
4715 the glyph row being updated, and updated_area is the area of that
4716 row being updated. */
4717
4718 static void
4719 x_write_glyphs (start, len)
4720 struct glyph *start;
4721 int len;
4722 {
4723 int x, hpos, real_start, real_end;
4724
4725 xassert (updated_window && updated_row);
4726 BLOCK_INPUT;
4727
4728 /* Write glyphs. */
4729
4730 hpos = start - updated_row->glyphs[updated_area];
4731 x = x_draw_glyphs (updated_window, output_cursor.x,
4732 updated_row, updated_area,
4733 hpos, hpos + len,
4734 (updated_row->inverse_p
4735 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4736 &real_start, &real_end, 0);
4737
4738 /* If we drew over the cursor, note that it is not visible any more. */
4739 note_overwritten_text_cursor (updated_window, real_start,
4740 real_end - real_start);
4741
4742 UNBLOCK_INPUT;
4743
4744 /* Advance the output cursor. */
4745 output_cursor.hpos += len;
4746 output_cursor.x = x;
4747 }
4748
4749
4750 /* Insert LEN glyphs from START at the nominal cursor position. */
4751
4752 static void
4753 x_insert_glyphs (start, len)
4754 struct glyph *start;
4755 register int len;
4756 {
4757 struct frame *f;
4758 struct window *w;
4759 int line_height, shift_by_width, shifted_region_width;
4760 struct glyph_row *row;
4761 struct glyph *glyph;
4762 int frame_x, frame_y, hpos, real_start, real_end;
4763
4764 xassert (updated_window && updated_row);
4765 BLOCK_INPUT;
4766 w = updated_window;
4767 f = XFRAME (WINDOW_FRAME (w));
4768
4769 /* Get the height of the line we are in. */
4770 row = updated_row;
4771 line_height = row->height;
4772
4773 /* Get the width of the glyphs to insert. */
4774 shift_by_width = 0;
4775 for (glyph = start; glyph < start + len; ++glyph)
4776 shift_by_width += glyph->pixel_width;
4777
4778 /* Get the width of the region to shift right. */
4779 shifted_region_width = (window_box_width (w, updated_area)
4780 - output_cursor.x
4781 - shift_by_width);
4782
4783 /* Shift right. */
4784 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4785 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4786 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4787 f->output_data.x->normal_gc,
4788 frame_x, frame_y,
4789 shifted_region_width, line_height,
4790 frame_x + shift_by_width, frame_y);
4791
4792 /* Write the glyphs. */
4793 hpos = start - row->glyphs[updated_area];
4794 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4795 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4796 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4797
4798 /* Advance the output cursor. */
4799 output_cursor.hpos += len;
4800 output_cursor.x += shift_by_width;
4801 UNBLOCK_INPUT;
4802 }
4803
4804
4805 /* Delete N glyphs at the nominal cursor position. Not implemented
4806 for X frames. */
4807
4808 static void
4809 x_delete_glyphs (n)
4810 register int n;
4811 {
4812 abort ();
4813 }
4814
4815
4816 /* Erase the current text line from the nominal cursor position
4817 (inclusive) to pixel column TO_X (exclusive). The idea is that
4818 everything from TO_X onward is already erased.
4819
4820 TO_X is a pixel position relative to updated_area of
4821 updated_window. TO_X == -1 means clear to the end of this area. */
4822
4823 static void
4824 x_clear_end_of_line (to_x)
4825 int to_x;
4826 {
4827 struct frame *f;
4828 struct window *w = updated_window;
4829 int max_x, min_y, max_y;
4830 int from_x, from_y, to_y;
4831
4832 xassert (updated_window && updated_row);
4833 f = XFRAME (w->frame);
4834
4835 if (updated_row->full_width_p)
4836 {
4837 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4838 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4839 && !w->pseudo_window_p)
4840 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4841 }
4842 else
4843 max_x = window_box_width (w, updated_area);
4844 max_y = window_text_bottom_y (w);
4845
4846 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4847 of window. For TO_X > 0, truncate to end of drawing area. */
4848 if (to_x == 0)
4849 return;
4850 else if (to_x < 0)
4851 to_x = max_x;
4852 else
4853 to_x = min (to_x, max_x);
4854
4855 to_y = min (max_y, output_cursor.y + updated_row->height);
4856
4857 /* Notice if the cursor will be cleared by this operation. */
4858 if (!updated_row->full_width_p)
4859 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
4860
4861 from_x = output_cursor.x;
4862
4863 /* Translate to frame coordinates. */
4864 if (updated_row->full_width_p)
4865 {
4866 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4867 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4868 }
4869 else
4870 {
4871 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4872 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4873 }
4874
4875 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
4876 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4877 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4878
4879 /* Prevent inadvertently clearing to end of the X window. */
4880 if (to_x > from_x && to_y > from_y)
4881 {
4882 BLOCK_INPUT;
4883 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4884 from_x, from_y, to_x - from_x, to_y - from_y,
4885 False);
4886 UNBLOCK_INPUT;
4887 }
4888 }
4889
4890
4891 /* Clear entire frame. If updating_frame is non-null, clear that
4892 frame. Otherwise clear the selected frame. */
4893
4894 static void
4895 x_clear_frame ()
4896 {
4897 struct frame *f;
4898
4899 if (updating_frame)
4900 f = updating_frame;
4901 else
4902 f = SELECTED_FRAME ();
4903
4904 /* Clearing the frame will erase any cursor, so mark them all as no
4905 longer visible. */
4906 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4907 output_cursor.hpos = output_cursor.vpos = 0;
4908 output_cursor.x = -1;
4909
4910 /* We don't set the output cursor here because there will always
4911 follow an explicit cursor_to. */
4912 BLOCK_INPUT;
4913 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4914
4915 /* We have to clear the scroll bars, too. If we have changed
4916 colors or something like that, then they should be notified. */
4917 x_scroll_bar_clear (f);
4918
4919 XFlush (FRAME_X_DISPLAY (f));
4920 UNBLOCK_INPUT;
4921 }
4922
4923
4924 \f
4925 /* Invert the middle quarter of the frame for .15 sec. */
4926
4927 /* We use the select system call to do the waiting, so we have to make
4928 sure it's available. If it isn't, we just won't do visual bells. */
4929
4930 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4931
4932
4933 /* Subtract the `struct timeval' values X and Y, storing the result in
4934 *RESULT. Return 1 if the difference is negative, otherwise 0. */
4935
4936 static int
4937 timeval_subtract (result, x, y)
4938 struct timeval *result, x, y;
4939 {
4940 /* Perform the carry for the later subtraction by updating y. This
4941 is safer because on some systems the tv_sec member is unsigned. */
4942 if (x.tv_usec < y.tv_usec)
4943 {
4944 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4945 y.tv_usec -= 1000000 * nsec;
4946 y.tv_sec += nsec;
4947 }
4948
4949 if (x.tv_usec - y.tv_usec > 1000000)
4950 {
4951 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4952 y.tv_usec += 1000000 * nsec;
4953 y.tv_sec -= nsec;
4954 }
4955
4956 /* Compute the time remaining to wait. tv_usec is certainly
4957 positive. */
4958 result->tv_sec = x.tv_sec - y.tv_sec;
4959 result->tv_usec = x.tv_usec - y.tv_usec;
4960
4961 /* Return indication of whether the result should be considered
4962 negative. */
4963 return x.tv_sec < y.tv_sec;
4964 }
4965
4966 void
4967 XTflash (f)
4968 struct frame *f;
4969 {
4970 BLOCK_INPUT;
4971
4972 {
4973 GC gc;
4974
4975 /* Create a GC that will use the GXxor function to flip foreground
4976 pixels into background pixels. */
4977 {
4978 XGCValues values;
4979
4980 values.function = GXxor;
4981 values.foreground = (f->output_data.x->foreground_pixel
4982 ^ f->output_data.x->background_pixel);
4983
4984 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4985 GCFunction | GCForeground, &values);
4986 }
4987
4988 {
4989 /* Get the height not including a menu bar widget. */
4990 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4991 /* Height of each line to flash. */
4992 int flash_height = FRAME_LINE_HEIGHT (f);
4993 /* These will be the left and right margins of the rectangles. */
4994 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4995 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4996
4997 int width;
4998
4999 /* Don't flash the area between a scroll bar and the frame
5000 edge it is next to. */
5001 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5002 {
5003 case vertical_scroll_bar_left:
5004 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5005 break;
5006
5007 case vertical_scroll_bar_right:
5008 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5009 break;
5010
5011 default:
5012 break;
5013 }
5014
5015 width = flash_right - flash_left;
5016
5017 /* If window is tall, flash top and bottom line. */
5018 if (height > 3 * FRAME_LINE_HEIGHT (f))
5019 {
5020 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5021 flash_left,
5022 (FRAME_INTERNAL_BORDER_WIDTH (f)
5023 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5024 width, flash_height);
5025 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5026 flash_left,
5027 (height - flash_height
5028 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5029 width, flash_height);
5030 }
5031 else
5032 /* If it is short, flash it all. */
5033 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5034 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5035 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5036
5037 x_flush (f);
5038
5039 {
5040 struct timeval wakeup;
5041
5042 EMACS_GET_TIME (wakeup);
5043
5044 /* Compute time to wait until, propagating carry from usecs. */
5045 wakeup.tv_usec += 150000;
5046 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5047 wakeup.tv_usec %= 1000000;
5048
5049 /* Keep waiting until past the time wakeup. */
5050 while (1)
5051 {
5052 struct timeval timeout;
5053
5054 EMACS_GET_TIME (timeout);
5055
5056 /* In effect, timeout = wakeup - timeout.
5057 Break if result would be negative. */
5058 if (timeval_subtract (&timeout, wakeup, timeout))
5059 break;
5060
5061 /* Try to wait that long--but we might wake up sooner. */
5062 select (0, NULL, NULL, NULL, &timeout);
5063 }
5064 }
5065
5066 /* If window is tall, flash top and bottom line. */
5067 if (height > 3 * FRAME_LINE_HEIGHT (f))
5068 {
5069 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5070 flash_left,
5071 (FRAME_INTERNAL_BORDER_WIDTH (f)
5072 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5073 width, flash_height);
5074 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5075 flash_left,
5076 (height - flash_height
5077 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5078 width, flash_height);
5079 }
5080 else
5081 /* If it is short, flash it all. */
5082 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5083 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5084 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5085
5086 XFreeGC (FRAME_X_DISPLAY (f), gc);
5087 x_flush (f);
5088 }
5089 }
5090
5091 UNBLOCK_INPUT;
5092 }
5093
5094 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5095
5096
5097 /* Make audible bell. */
5098
5099 void
5100 XTring_bell ()
5101 {
5102 struct frame *f = SELECTED_FRAME ();
5103
5104 if (FRAME_X_DISPLAY (f))
5105 {
5106 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5107 if (visible_bell)
5108 XTflash (f);
5109 else
5110 #endif
5111 {
5112 BLOCK_INPUT;
5113 XBell (FRAME_X_DISPLAY (f), 0);
5114 XFlush (FRAME_X_DISPLAY (f));
5115 UNBLOCK_INPUT;
5116 }
5117 }
5118 }
5119
5120 \f
5121 /* Specify how many text lines, from the top of the window,
5122 should be affected by insert-lines and delete-lines operations.
5123 This, and those operations, are used only within an update
5124 that is bounded by calls to x_update_begin and x_update_end. */
5125
5126 static void
5127 XTset_terminal_window (n)
5128 register int n;
5129 {
5130 /* This function intentionally left blank. */
5131 }
5132
5133
5134 \f
5135 /***********************************************************************
5136 Line Dance
5137 ***********************************************************************/
5138
5139 /* Perform an insert-lines or delete-lines operation, inserting N
5140 lines or deleting -N lines at vertical position VPOS. */
5141
5142 static void
5143 x_ins_del_lines (vpos, n)
5144 int vpos, n;
5145 {
5146 abort ();
5147 }
5148
5149
5150 /* Scroll part of the display as described by RUN. */
5151
5152 static void
5153 x_scroll_run (w, run)
5154 struct window *w;
5155 struct run *run;
5156 {
5157 struct frame *f = XFRAME (w->frame);
5158 int x, y, width, height, from_y, to_y, bottom_y;
5159
5160 /* Get frame-relative bounding box of the text display area of W,
5161 without mode lines. Include in this box the flags areas to the
5162 left and right of W. */
5163 window_box (w, -1, &x, &y, &width, &height);
5164 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5165 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5166
5167 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5168 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5169 bottom_y = y + height;
5170
5171 if (to_y < from_y)
5172 {
5173 /* Scrolling up. Make sure we don't copy part of the mode
5174 line at the bottom. */
5175 if (from_y + run->height > bottom_y)
5176 height = bottom_y - from_y;
5177 else
5178 height = run->height;
5179 }
5180 else
5181 {
5182 /* Scolling down. Make sure we don't copy over the mode line.
5183 at the bottom. */
5184 if (to_y + run->height > bottom_y)
5185 height = bottom_y - to_y;
5186 else
5187 height = run->height;
5188 }
5189
5190 BLOCK_INPUT;
5191
5192 /* Cursor off. Will be switched on again in x_update_window_end. */
5193 updated_window = w;
5194 x_clear_cursor (w);
5195
5196 XCopyArea (FRAME_X_DISPLAY (f),
5197 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5198 f->output_data.x->normal_gc,
5199 x, from_y,
5200 width, height,
5201 x, to_y);
5202
5203 UNBLOCK_INPUT;
5204 }
5205
5206
5207 \f
5208 /***********************************************************************
5209 Exposure Events
5210 ***********************************************************************/
5211
5212 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5213 corner of the exposed rectangle. W and H are width and height of
5214 the exposed area. All are pixel values. W or H zero means redraw
5215 the entire frame. */
5216
5217 static void
5218 expose_frame (f, x, y, w, h)
5219 struct frame *f;
5220 int x, y, w, h;
5221 {
5222 XRectangle r;
5223
5224 TRACE ((stderr, "expose_frame "));
5225
5226 /* No need to redraw if frame will be redrawn soon. */
5227 if (FRAME_GARBAGED_P (f))
5228 {
5229 TRACE ((stderr, " garbaged\n"));
5230 return;
5231 }
5232
5233 /* If basic faces haven't been realized yet, there is no point in
5234 trying to redraw anything. This can happen when we get an expose
5235 event while Emacs is starting, e.g. by moving another window. */
5236 if (FRAME_FACE_CACHE (f) == NULL
5237 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5238 {
5239 TRACE ((stderr, " no faces\n"));
5240 return;
5241 }
5242
5243 if (w == 0 || h == 0)
5244 {
5245 r.x = r.y = 0;
5246 r.width = CANON_X_UNIT (f) * f->width;
5247 r.height = CANON_Y_UNIT (f) * f->height;
5248 }
5249 else
5250 {
5251 r.x = x;
5252 r.y = y;
5253 r.width = w;
5254 r.height = h;
5255 }
5256
5257 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5258 expose_window_tree (XWINDOW (f->root_window), &r);
5259
5260 if (WINDOWP (f->tool_bar_window))
5261 {
5262 struct window *w = XWINDOW (f->tool_bar_window);
5263 XRectangle window_rect;
5264 XRectangle intersection_rect;
5265 int window_x, window_y, window_width, window_height;
5266
5267
5268 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5269 window_rect.x = window_x;
5270 window_rect.y = window_y;
5271 window_rect.width = window_width;
5272 window_rect.height = window_height;
5273
5274 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5275 expose_window (w, &intersection_rect);
5276 }
5277
5278 #ifndef USE_X_TOOLKIT
5279 if (WINDOWP (f->menu_bar_window))
5280 {
5281 struct window *w = XWINDOW (f->menu_bar_window);
5282 XRectangle window_rect;
5283 XRectangle intersection_rect;
5284 int window_x, window_y, window_width, window_height;
5285
5286
5287 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5288 window_rect.x = window_x;
5289 window_rect.y = window_y;
5290 window_rect.width = window_width;
5291 window_rect.height = window_height;
5292
5293 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5294 expose_window (w, &intersection_rect);
5295 }
5296 #endif /* not USE_X_TOOLKIT */
5297 }
5298
5299
5300 /* Redraw (parts) of all windows in the window tree rooted at W that
5301 intersect R. R contains frame pixel coordinates. */
5302
5303 static void
5304 expose_window_tree (w, r)
5305 struct window *w;
5306 XRectangle *r;
5307 {
5308 while (w)
5309 {
5310 if (!NILP (w->hchild))
5311 expose_window_tree (XWINDOW (w->hchild), r);
5312 else if (!NILP (w->vchild))
5313 expose_window_tree (XWINDOW (w->vchild), r);
5314 else
5315 {
5316 XRectangle window_rect;
5317 XRectangle intersection_rect;
5318 struct frame *f = XFRAME (w->frame);
5319 int window_x, window_y, window_width, window_height;
5320
5321 /* Frame-relative pixel rectangle of W. */
5322 window_box (w, -1, &window_x, &window_y, &window_width,
5323 &window_height);
5324 window_rect.x
5325 = (window_x
5326 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5327 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5328 window_rect.y = window_y;
5329 window_rect.width
5330 = (window_width
5331 + FRAME_X_FLAGS_AREA_WIDTH (f)
5332 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5333 window_rect.height
5334 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5335
5336 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5337 expose_window (w, &intersection_rect);
5338 }
5339
5340 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5341 }
5342 }
5343
5344
5345 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5346 which intersects rectangle R. R is in window-relative coordinates. */
5347
5348 static void
5349 expose_area (w, row, r, area)
5350 struct window *w;
5351 struct glyph_row *row;
5352 XRectangle *r;
5353 enum glyph_row_area area;
5354 {
5355 int x;
5356 struct glyph *first = row->glyphs[area];
5357 struct glyph *end = row->glyphs[area] + row->used[area];
5358 struct glyph *last;
5359 int first_x;
5360
5361 /* Set x to the window-relative start position for drawing glyphs of
5362 AREA. The first glyph of the text area can be partially visible.
5363 The first glyphs of other areas cannot. */
5364 if (area == LEFT_MARGIN_AREA)
5365 x = 0;
5366 else if (area == TEXT_AREA)
5367 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5368 else
5369 x = (window_box_width (w, LEFT_MARGIN_AREA)
5370 + window_box_width (w, TEXT_AREA));
5371
5372 if (area == TEXT_AREA && row->fill_line_p)
5373 /* If row extends face to end of line write the whole line. */
5374 x_draw_glyphs (w, x, row, area,
5375 0, row->used[area],
5376 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5377 NULL, NULL, 0);
5378 else
5379 {
5380 /* Find the first glyph that must be redrawn. */
5381 while (first < end
5382 && x + first->pixel_width < r->x)
5383 {
5384 x += first->pixel_width;
5385 ++first;
5386 }
5387
5388 /* Find the last one. */
5389 last = first;
5390 first_x = x;
5391 while (last < end
5392 && x < r->x + r->width)
5393 {
5394 x += last->pixel_width;
5395 ++last;
5396 }
5397
5398 /* Repaint. */
5399 if (last > first)
5400 x_draw_glyphs (w, first_x, row, area,
5401 first - row->glyphs[area],
5402 last - row->glyphs[area],
5403 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5404 NULL, NULL, 0);
5405 }
5406 }
5407
5408
5409 /* Redraw the parts of the glyph row ROW on window W intersecting
5410 rectangle R. R is in window-relative coordinates. */
5411
5412 static void
5413 expose_line (w, row, r)
5414 struct window *w;
5415 struct glyph_row *row;
5416 XRectangle *r;
5417 {
5418 xassert (row->enabled_p);
5419
5420 if (row->mode_line_p || w->pseudo_window_p)
5421 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5422 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5423 NULL, NULL, 0);
5424 else
5425 {
5426 if (row->used[LEFT_MARGIN_AREA])
5427 expose_area (w, row, r, LEFT_MARGIN_AREA);
5428 if (row->used[TEXT_AREA])
5429 expose_area (w, row, r, TEXT_AREA);
5430 if (row->used[RIGHT_MARGIN_AREA])
5431 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5432 x_draw_row_bitmaps (w, row);
5433 }
5434 }
5435
5436
5437 /* Return non-zero if W's cursor intersects rectangle R. */
5438
5439 static int
5440 x_phys_cursor_in_rect_p (w, r)
5441 struct window *w;
5442 XRectangle *r;
5443 {
5444 XRectangle cr, result;
5445 struct glyph *cursor_glyph;
5446
5447 cursor_glyph = get_phys_cursor_glyph (w);
5448 if (cursor_glyph)
5449 {
5450 cr.x = w->phys_cursor.x;
5451 cr.y = w->phys_cursor.y;
5452 cr.width = cursor_glyph->pixel_width;
5453 cr.height = w->phys_cursor_height;
5454 return x_intersect_rectangles (&cr, r, &result);
5455 }
5456 else
5457 return 0;
5458 }
5459
5460
5461 /* Redraw a rectangle of window W. R is a rectangle in window
5462 relative coordinates. Call this function with input blocked. */
5463
5464 static void
5465 expose_window (w, r)
5466 struct window *w;
5467 XRectangle *r;
5468 {
5469 struct glyph_row *row;
5470 int y;
5471 int yb = window_text_bottom_y (w);
5472 int cursor_cleared_p;
5473
5474 /* If window is not yet fully initialized, do nothing. This can
5475 happen when toolkit scroll bars are used and a window is split.
5476 Reconfiguring the scroll bar will generate an expose for a newly
5477 created window. */
5478 if (w->current_matrix == NULL)
5479 return;
5480
5481 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5482 r->x, r->y, r->width, r->height));
5483
5484 /* Convert to window coordinates. */
5485 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5486 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5487
5488 /* Turn off the cursor. */
5489 if (!w->pseudo_window_p
5490 && x_phys_cursor_in_rect_p (w, r))
5491 {
5492 x_clear_cursor (w);
5493 cursor_cleared_p = 1;
5494 }
5495 else
5496 cursor_cleared_p = 0;
5497
5498 /* Find the first row intersecting the rectangle R. */
5499 row = w->current_matrix->rows;
5500 y = 0;
5501 while (row->enabled_p
5502 && y < yb
5503 && y + row->height < r->y)
5504 {
5505 y += row->height;
5506 ++row;
5507 }
5508
5509 /* Display the text in the rectangle, one text line at a time. */
5510 while (row->enabled_p
5511 && y < yb
5512 && y < r->y + r->height)
5513 {
5514 expose_line (w, row, r);
5515 y += row->height;
5516 ++row;
5517 }
5518
5519 /* Display the mode line if there is one. */
5520 if (WINDOW_WANTS_MODELINE_P (w)
5521 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5522 row->enabled_p)
5523 && row->y < r->y + r->height)
5524 expose_line (w, row, r);
5525
5526 if (!w->pseudo_window_p)
5527 {
5528 /* Draw border between windows. */
5529 x_draw_vertical_border (w);
5530
5531 /* Turn the cursor on again. */
5532 if (cursor_cleared_p)
5533 x_update_window_cursor (w, 1);
5534 }
5535 }
5536
5537
5538 /* Determine the intersection of two rectangles R1 and R2. Return
5539 the intersection in *RESULT. Value is non-zero if RESULT is not
5540 empty. */
5541
5542 static int
5543 x_intersect_rectangles (r1, r2, result)
5544 XRectangle *r1, *r2, *result;
5545 {
5546 XRectangle *left, *right;
5547 XRectangle *upper, *lower;
5548 int intersection_p = 0;
5549
5550 /* Rearrange so that R1 is the left-most rectangle. */
5551 if (r1->x < r2->x)
5552 left = r1, right = r2;
5553 else
5554 left = r2, right = r1;
5555
5556 /* X0 of the intersection is right.x0, if this is inside R1,
5557 otherwise there is no intersection. */
5558 if (right->x <= left->x + left->width)
5559 {
5560 result->x = right->x;
5561
5562 /* The right end of the intersection is the minimum of the
5563 the right ends of left and right. */
5564 result->width = (min (left->x + left->width, right->x + right->width)
5565 - result->x);
5566
5567 /* Same game for Y. */
5568 if (r1->y < r2->y)
5569 upper = r1, lower = r2;
5570 else
5571 upper = r2, lower = r1;
5572
5573 /* The upper end of the intersection is lower.y0, if this is inside
5574 of upper. Otherwise, there is no intersection. */
5575 if (lower->y <= upper->y + upper->height)
5576 {
5577 result->y = lower->y;
5578
5579 /* The lower end of the intersection is the minimum of the lower
5580 ends of upper and lower. */
5581 result->height = (min (lower->y + lower->height,
5582 upper->y + upper->height)
5583 - result->y);
5584 intersection_p = 1;
5585 }
5586 }
5587
5588 return intersection_p;
5589 }
5590
5591
5592
5593
5594 \f
5595 static void
5596 frame_highlight (f)
5597 struct frame *f;
5598 {
5599 /* We used to only do this if Vx_no_window_manager was non-nil, but
5600 the ICCCM (section 4.1.6) says that the window's border pixmap
5601 and border pixel are window attributes which are "private to the
5602 client", so we can always change it to whatever we want. */
5603 BLOCK_INPUT;
5604 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5605 f->output_data.x->border_pixel);
5606 UNBLOCK_INPUT;
5607 x_update_cursor (f, 1);
5608 }
5609
5610 static void
5611 frame_unhighlight (f)
5612 struct frame *f;
5613 {
5614 /* We used to only do this if Vx_no_window_manager was non-nil, but
5615 the ICCCM (section 4.1.6) says that the window's border pixmap
5616 and border pixel are window attributes which are "private to the
5617 client", so we can always change it to whatever we want. */
5618 BLOCK_INPUT;
5619 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5620 f->output_data.x->border_tile);
5621 UNBLOCK_INPUT;
5622 x_update_cursor (f, 1);
5623 }
5624
5625 /* The focus has changed. Update the frames as necessary to reflect
5626 the new situation. Note that we can't change the selected frame
5627 here, because the Lisp code we are interrupting might become confused.
5628 Each event gets marked with the frame in which it occurred, so the
5629 Lisp code can tell when the switch took place by examining the events. */
5630
5631 static void
5632 x_new_focus_frame (dpyinfo, frame)
5633 struct x_display_info *dpyinfo;
5634 struct frame *frame;
5635 {
5636 struct frame *old_focus = dpyinfo->x_focus_frame;
5637
5638 if (frame != dpyinfo->x_focus_frame)
5639 {
5640 /* Set this before calling other routines, so that they see
5641 the correct value of x_focus_frame. */
5642 dpyinfo->x_focus_frame = frame;
5643
5644 if (old_focus && old_focus->auto_lower)
5645 x_lower_frame (old_focus);
5646
5647 #if 0
5648 selected_frame = frame;
5649 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5650 selected_frame);
5651 Fselect_window (selected_frame->selected_window);
5652 choose_minibuf_frame ();
5653 #endif /* ! 0 */
5654
5655 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5656 pending_autoraise_frame = dpyinfo->x_focus_frame;
5657 else
5658 pending_autoraise_frame = 0;
5659 }
5660
5661 x_frame_rehighlight (dpyinfo);
5662 }
5663
5664 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5665
5666 void
5667 x_mouse_leave (dpyinfo)
5668 struct x_display_info *dpyinfo;
5669 {
5670 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5671 }
5672
5673 /* The focus has changed, or we have redirected a frame's focus to
5674 another frame (this happens when a frame uses a surrogate
5675 mini-buffer frame). Shift the highlight as appropriate.
5676
5677 The FRAME argument doesn't necessarily have anything to do with which
5678 frame is being highlighted or un-highlighted; we only use it to find
5679 the appropriate X display info. */
5680
5681 static void
5682 XTframe_rehighlight (frame)
5683 struct frame *frame;
5684 {
5685 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5686 }
5687
5688 static void
5689 x_frame_rehighlight (dpyinfo)
5690 struct x_display_info *dpyinfo;
5691 {
5692 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5693
5694 if (dpyinfo->x_focus_frame)
5695 {
5696 dpyinfo->x_highlight_frame
5697 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5698 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5699 : dpyinfo->x_focus_frame);
5700 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5701 {
5702 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5703 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5704 }
5705 }
5706 else
5707 dpyinfo->x_highlight_frame = 0;
5708
5709 if (dpyinfo->x_highlight_frame != old_highlight)
5710 {
5711 if (old_highlight)
5712 frame_unhighlight (old_highlight);
5713 if (dpyinfo->x_highlight_frame)
5714 frame_highlight (dpyinfo->x_highlight_frame);
5715 }
5716 }
5717
5718
5719 \f
5720 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5721
5722 /* Initialize mode_switch_bit and modifier_meaning. */
5723 static void
5724 x_find_modifier_meanings (dpyinfo)
5725 struct x_display_info *dpyinfo;
5726 {
5727 int min_code, max_code;
5728 KeySym *syms;
5729 int syms_per_code;
5730 XModifierKeymap *mods;
5731
5732 dpyinfo->meta_mod_mask = 0;
5733 dpyinfo->shift_lock_mask = 0;
5734 dpyinfo->alt_mod_mask = 0;
5735 dpyinfo->super_mod_mask = 0;
5736 dpyinfo->hyper_mod_mask = 0;
5737
5738 #ifdef HAVE_X11R4
5739 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5740 #else
5741 min_code = dpyinfo->display->min_keycode;
5742 max_code = dpyinfo->display->max_keycode;
5743 #endif
5744
5745 syms = XGetKeyboardMapping (dpyinfo->display,
5746 min_code, max_code - min_code + 1,
5747 &syms_per_code);
5748 mods = XGetModifierMapping (dpyinfo->display);
5749
5750 /* Scan the modifier table to see which modifier bits the Meta and
5751 Alt keysyms are on. */
5752 {
5753 int row, col; /* The row and column in the modifier table. */
5754
5755 for (row = 3; row < 8; row++)
5756 for (col = 0; col < mods->max_keypermod; col++)
5757 {
5758 KeyCode code
5759 = mods->modifiermap[(row * mods->max_keypermod) + col];
5760
5761 /* Zeroes are used for filler. Skip them. */
5762 if (code == 0)
5763 continue;
5764
5765 /* Are any of this keycode's keysyms a meta key? */
5766 {
5767 int code_col;
5768
5769 for (code_col = 0; code_col < syms_per_code; code_col++)
5770 {
5771 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5772
5773 switch (sym)
5774 {
5775 case XK_Meta_L:
5776 case XK_Meta_R:
5777 dpyinfo->meta_mod_mask |= (1 << row);
5778 break;
5779
5780 case XK_Alt_L:
5781 case XK_Alt_R:
5782 dpyinfo->alt_mod_mask |= (1 << row);
5783 break;
5784
5785 case XK_Hyper_L:
5786 case XK_Hyper_R:
5787 dpyinfo->hyper_mod_mask |= (1 << row);
5788 break;
5789
5790 case XK_Super_L:
5791 case XK_Super_R:
5792 dpyinfo->super_mod_mask |= (1 << row);
5793 break;
5794
5795 case XK_Shift_Lock:
5796 /* Ignore this if it's not on the lock modifier. */
5797 if ((1 << row) == LockMask)
5798 dpyinfo->shift_lock_mask = LockMask;
5799 break;
5800 }
5801 }
5802 }
5803 }
5804 }
5805
5806 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5807 if (! dpyinfo->meta_mod_mask)
5808 {
5809 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5810 dpyinfo->alt_mod_mask = 0;
5811 }
5812
5813 /* If some keys are both alt and meta,
5814 make them just meta, not alt. */
5815 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5816 {
5817 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5818 }
5819
5820 XFree ((char *) syms);
5821 XFreeModifiermap (mods);
5822 }
5823
5824 /* Convert between the modifier bits X uses and the modifier bits
5825 Emacs uses. */
5826
5827 static unsigned int
5828 x_x_to_emacs_modifiers (dpyinfo, state)
5829 struct x_display_info *dpyinfo;
5830 unsigned int state;
5831 {
5832 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5833 | ((state & ControlMask) ? ctrl_modifier : 0)
5834 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5835 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5836 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5837 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
5838 }
5839
5840 static unsigned int
5841 x_emacs_to_x_modifiers (dpyinfo, state)
5842 struct x_display_info *dpyinfo;
5843 unsigned int state;
5844 {
5845 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5846 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5847 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5848 | ((state & shift_modifier) ? ShiftMask : 0)
5849 | ((state & ctrl_modifier) ? ControlMask : 0)
5850 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
5851 }
5852
5853 /* Convert a keysym to its name. */
5854
5855 char *
5856 x_get_keysym_name (keysym)
5857 KeySym keysym;
5858 {
5859 char *value;
5860
5861 BLOCK_INPUT;
5862 value = XKeysymToString (keysym);
5863 UNBLOCK_INPUT;
5864
5865 return value;
5866 }
5867
5868
5869 \f
5870 /* Mouse clicks and mouse movement. Rah. */
5871
5872 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5873 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5874 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5875 not force the value into range. */
5876
5877 void
5878 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5879 FRAME_PTR f;
5880 register int pix_x, pix_y;
5881 register int *x, *y;
5882 XRectangle *bounds;
5883 int noclip;
5884 {
5885 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5886 even for negative values. */
5887 if (pix_x < 0)
5888 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
5889 if (pix_y < 0)
5890 pix_y -= (f)->output_data.x->line_height - 1;
5891
5892 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5893 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5894
5895 if (bounds)
5896 {
5897 bounds->width = FONT_WIDTH (f->output_data.x->font);
5898 bounds->height = f->output_data.x->line_height;
5899 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5900 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5901 }
5902
5903 if (!noclip)
5904 {
5905 if (pix_x < 0)
5906 pix_x = 0;
5907 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5908 pix_x = FRAME_WINDOW_WIDTH (f);
5909
5910 if (pix_y < 0)
5911 pix_y = 0;
5912 else if (pix_y > f->height)
5913 pix_y = f->height;
5914 }
5915
5916 *x = pix_x;
5917 *y = pix_y;
5918 }
5919
5920
5921 /* Given HPOS/VPOS in the current matrix of W, return corresponding
5922 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5923 can't tell the positions because W's display is not up to date,
5924 return 0. */
5925
5926 int
5927 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5928 struct window *w;
5929 int hpos, vpos;
5930 int *frame_x, *frame_y;
5931 {
5932 int success_p;
5933
5934 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5935 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5936
5937 if (display_completed)
5938 {
5939 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5940 struct glyph *glyph = row->glyphs[TEXT_AREA];
5941 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5942
5943 *frame_y = row->y;
5944 *frame_x = row->x;
5945 while (glyph < end)
5946 {
5947 *frame_x += glyph->pixel_width;
5948 ++glyph;
5949 }
5950
5951 success_p = 1;
5952 }
5953 else
5954 {
5955 *frame_y = *frame_x = 0;
5956 success_p = 0;
5957 }
5958
5959 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5960 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5961 return success_p;
5962 }
5963
5964
5965 /* Prepare a mouse-event in *RESULT for placement in the input queue.
5966
5967 If the event is a button press, then note that we have grabbed
5968 the mouse. */
5969
5970 static Lisp_Object
5971 construct_mouse_click (result, event, f)
5972 struct input_event *result;
5973 XButtonEvent *event;
5974 struct frame *f;
5975 {
5976 /* Make the event type no_event; we'll change that when we decide
5977 otherwise. */
5978 result->kind = mouse_click;
5979 result->code = event->button - Button1;
5980 result->timestamp = event->time;
5981 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5982 event->state)
5983 | (event->type == ButtonRelease
5984 ? up_modifier
5985 : down_modifier));
5986
5987 XSETINT (result->x, event->x);
5988 XSETINT (result->y, event->y);
5989 XSETFRAME (result->frame_or_window, f);
5990 return Qnil;
5991 }
5992
5993 #if 0 /* This function isn't called. --gerd */
5994
5995 /* Prepare a menu-event in *RESULT for placement in the input queue. */
5996
5997 static Lisp_Object
5998 construct_menu_click (result, event, f)
5999 struct input_event *result;
6000 XButtonEvent *event;
6001 struct frame *f;
6002 {
6003 /* Make the event type no_event; we'll change that when we decide
6004 otherwise. */
6005 result->kind = mouse_click;
6006 result->code = event->button - Button1;
6007 result->timestamp = event->time;
6008 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6009 event->state)
6010 | (event->type == ButtonRelease
6011 ? up_modifier
6012 : down_modifier));
6013
6014 XSETINT (result->x, event->x);
6015 XSETINT (result->y, -1);
6016 XSETFRAME (result->frame_or_window, f);
6017 }
6018
6019 #endif /* 0 */
6020
6021 \f
6022 /* Function to report a mouse movement to the mainstream Emacs code.
6023 The input handler calls this.
6024
6025 We have received a mouse movement event, which is given in *event.
6026 If the mouse is over a different glyph than it was last time, tell
6027 the mainstream emacs code by setting mouse_moved. If not, ask for
6028 another motion event, so we can check again the next time it moves. */
6029
6030 static XMotionEvent last_mouse_motion_event;
6031 static Lisp_Object last_mouse_motion_frame;
6032
6033 static void
6034 note_mouse_movement (frame, event)
6035 FRAME_PTR frame;
6036 XMotionEvent *event;
6037 {
6038 last_mouse_movement_time = event->time;
6039 last_mouse_motion_event = *event;
6040 XSETFRAME (last_mouse_motion_frame, frame);
6041
6042 if (event->window != FRAME_X_WINDOW (frame))
6043 {
6044 frame->mouse_moved = 1;
6045 last_mouse_scroll_bar = Qnil;
6046 note_mouse_highlight (frame, -1, -1);
6047 }
6048
6049 /* Has the mouse moved off the glyph it was on at the last sighting? */
6050 else if (event->x < last_mouse_glyph.x
6051 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6052 || event->y < last_mouse_glyph.y
6053 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6054 {
6055 frame->mouse_moved = 1;
6056 last_mouse_scroll_bar = Qnil;
6057 note_mouse_highlight (frame, event->x, event->y);
6058 }
6059 }
6060
6061 /* This is used for debugging, to turn off note_mouse_highlight. */
6062
6063 int disable_mouse_highlight;
6064
6065
6066 \f
6067 /************************************************************************
6068 Mouse Face
6069 ************************************************************************/
6070
6071 /* Find the glyph under window-relative coordinates X/Y in window W.
6072 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6073 strings. Return in *HPOS and *VPOS the row and column number of
6074 the glyph found. Return in *AREA the glyph area containing X.
6075 Value is a pointer to the glyph found or null if X/Y is not on
6076 text, or we can't tell because W's current matrix is not up to
6077 date. */
6078
6079 static struct glyph *
6080 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6081 struct window *w;
6082 int x, y;
6083 int *hpos, *vpos, *area;
6084 {
6085 struct glyph *glyph, *end;
6086 struct glyph_row *row;
6087 int x0, i, left_area_width;
6088
6089 /* Find row containing Y. Give up if some row is not enabled. */
6090 for (i = 0; i < w->current_matrix->nrows; ++i)
6091 {
6092 row = MATRIX_ROW (w->current_matrix, i);
6093 if (!row->enabled_p)
6094 return NULL;
6095 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6096 break;
6097 }
6098
6099 *vpos = i;
6100 *hpos = 0;
6101
6102 /* Give up if Y is not in the window. */
6103 if (i == w->current_matrix->nrows)
6104 return NULL;
6105
6106 /* Get the glyph area containing X. */
6107 if (w->pseudo_window_p)
6108 {
6109 *area = TEXT_AREA;
6110 x0 = 0;
6111 }
6112 else
6113 {
6114 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6115 if (x < left_area_width)
6116 {
6117 *area = LEFT_MARGIN_AREA;
6118 x0 = 0;
6119 }
6120 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6121 {
6122 *area = TEXT_AREA;
6123 x0 = row->x + left_area_width;
6124 }
6125 else
6126 {
6127 *area = RIGHT_MARGIN_AREA;
6128 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6129 }
6130 }
6131
6132 /* Find glyph containing X. */
6133 glyph = row->glyphs[*area];
6134 end = glyph + row->used[*area];
6135 while (glyph < end)
6136 {
6137 if (x < x0 + glyph->pixel_width)
6138 {
6139 if (w->pseudo_window_p)
6140 break;
6141 else if (BUFFERP (glyph->object))
6142 break;
6143 }
6144
6145 x0 += glyph->pixel_width;
6146 ++glyph;
6147 }
6148
6149 if (glyph == end)
6150 return NULL;
6151
6152 *hpos = glyph - row->glyphs[*area];
6153 return glyph;
6154 }
6155
6156
6157 /* Convert frame-relative x/y to coordinates relative to window W.
6158 Takes pseudo-windows into account. */
6159
6160 static void
6161 frame_to_window_pixel_xy (w, x, y)
6162 struct window *w;
6163 int *x, *y;
6164 {
6165 if (w->pseudo_window_p)
6166 {
6167 /* A pseudo-window is always full-width, and starts at the
6168 left edge of the frame, plus a frame border. */
6169 struct frame *f = XFRAME (w->frame);
6170 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6171 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6172 }
6173 else
6174 {
6175 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6176 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6177 }
6178 }
6179
6180
6181 /* Take proper action when mouse has moved to the mode or top line of
6182 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6183 mode line. X is relative to the start of the text display area of
6184 W, so the width of bitmap areas and scroll bars must be subtracted
6185 to get a position relative to the start of the mode line. */
6186
6187 static void
6188 note_mode_line_highlight (w, x, mode_line_p)
6189 struct window *w;
6190 int x, mode_line_p;
6191 {
6192 struct frame *f = XFRAME (w->frame);
6193 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6194 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6195 struct glyph_row *row;
6196
6197 if (mode_line_p)
6198 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6199 else
6200 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6201
6202 if (row->enabled_p)
6203 {
6204 struct glyph *glyph, *end;
6205 Lisp_Object help, map;
6206 int x0;
6207
6208 /* Find the glyph under X. */
6209 glyph = row->glyphs[TEXT_AREA];
6210 end = glyph + row->used[TEXT_AREA];
6211 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6212 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6213 while (glyph < end
6214 && x >= x0 + glyph->pixel_width)
6215 {
6216 x0 += glyph->pixel_width;
6217 ++glyph;
6218 }
6219
6220 if (glyph < end
6221 && STRINGP (glyph->object)
6222 && XSTRING (glyph->object)->intervals
6223 && glyph->charpos >= 0
6224 && glyph->charpos < XSTRING (glyph->object)->size)
6225 {
6226 /* If we're on a string with `help-echo' text property,
6227 arrange for the help to be displayed. This is done by
6228 setting the global variable help_echo to the help string. */
6229 help = Fget_text_property (make_number (glyph->charpos),
6230 Qhelp_echo, glyph->object);
6231 if (STRINGP (help))
6232 help_echo = help;
6233
6234 /* Change the mouse pointer according to what is under X/Y. */
6235 map = Fget_text_property (make_number (glyph->charpos),
6236 Qlocal_map, glyph->object);
6237 if (!NILP (Fkeymapp (map)))
6238 cursor = f->output_data.x->nontext_cursor;
6239 }
6240 }
6241
6242 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6243 }
6244
6245
6246 /* Take proper action when the mouse has moved to position X, Y on
6247 frame F as regards highlighting characters that have mouse-face
6248 properties. Also de-highlighting chars where the mouse was before.
6249 X and Y can be negative or out of range. */
6250
6251 static void
6252 note_mouse_highlight (f, x, y)
6253 struct frame *f;
6254 int x, y;
6255 {
6256 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6257 int portion;
6258 Lisp_Object window;
6259 struct window *w;
6260
6261 /* When a menu is active, don't highlight because this looks odd. */
6262 #ifdef USE_X_TOOLKIT
6263 if (popup_activated ())
6264 return;
6265 #endif
6266
6267 if (disable_mouse_highlight)
6268 return;
6269
6270 dpyinfo->mouse_face_mouse_x = x;
6271 dpyinfo->mouse_face_mouse_y = y;
6272 dpyinfo->mouse_face_mouse_frame = f;
6273
6274 if (dpyinfo->mouse_face_defer)
6275 return;
6276
6277 if (gc_in_progress)
6278 {
6279 dpyinfo->mouse_face_deferred_gc = 1;
6280 return;
6281 }
6282
6283 /* Which window is that in? */
6284 window = window_from_coordinates (f, x, y, &portion, 1);
6285
6286 /* If we were displaying active text in another window, clear that. */
6287 if (! EQ (window, dpyinfo->mouse_face_window))
6288 clear_mouse_face (dpyinfo);
6289
6290 /* Not on a window -> return. */
6291 if (!WINDOWP (window))
6292 return;
6293
6294 /* Convert to window-relative pixel coordinates. */
6295 w = XWINDOW (window);
6296 frame_to_window_pixel_xy (w, &x, &y);
6297
6298 /* Handle tool-bar window differently since it doesn't display a
6299 buffer. */
6300 if (EQ (window, f->tool_bar_window))
6301 {
6302 note_tool_bar_highlight (f, x, y);
6303 return;
6304 }
6305
6306 if (portion == 1 || portion == 3)
6307 {
6308 /* Mouse is on the mode or top line. */
6309 note_mode_line_highlight (w, x, portion == 1);
6310 return;
6311 }
6312 else
6313 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6314 f->output_data.x->text_cursor);
6315
6316 /* Are we in a window whose display is up to date?
6317 And verify the buffer's text has not changed. */
6318 if (/* Within text portion of the window. */
6319 portion == 0
6320 && EQ (w->window_end_valid, w->buffer)
6321 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6322 && (XFASTINT (w->last_overlay_modified)
6323 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6324 {
6325 int hpos, vpos, pos, i, area;
6326 struct glyph *glyph;
6327
6328 /* Find the glyph under X/Y. */
6329 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6330
6331 /* Clear mouse face if X/Y not over text. */
6332 if (glyph == NULL
6333 || area != TEXT_AREA
6334 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6335 {
6336 clear_mouse_face (dpyinfo);
6337 return;
6338 }
6339
6340 pos = glyph->charpos;
6341 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6342
6343 /* Check for mouse-face and help-echo. */
6344 {
6345 Lisp_Object mouse_face, overlay, position;
6346 Lisp_Object *overlay_vec;
6347 int len, noverlays;
6348 struct buffer *obuf;
6349 int obegv, ozv;
6350
6351 /* If we get an out-of-range value, return now; avoid an error. */
6352 if (pos > BUF_Z (XBUFFER (w->buffer)))
6353 return;
6354
6355 /* Make the window's buffer temporarily current for
6356 overlays_at and compute_char_face. */
6357 obuf = current_buffer;
6358 current_buffer = XBUFFER (w->buffer);
6359 obegv = BEGV;
6360 ozv = ZV;
6361 BEGV = BEG;
6362 ZV = Z;
6363
6364 /* Is this char mouse-active or does it have help-echo? */
6365 XSETINT (position, pos);
6366
6367 /* Put all the overlays we want in a vector in overlay_vec.
6368 Store the length in len. If there are more than 10, make
6369 enough space for all, and try again. */
6370 len = 10;
6371 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6372 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6373 if (noverlays > len)
6374 {
6375 len = noverlays;
6376 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6377 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6378 }
6379
6380 noverlays = sort_overlays (overlay_vec, noverlays, w);
6381
6382 /* Check mouse-face highlighting. */
6383 if (! (EQ (window, dpyinfo->mouse_face_window)
6384 && vpos >= dpyinfo->mouse_face_beg_row
6385 && vpos <= dpyinfo->mouse_face_end_row
6386 && (vpos > dpyinfo->mouse_face_beg_row
6387 || hpos >= dpyinfo->mouse_face_beg_col)
6388 && (vpos < dpyinfo->mouse_face_end_row
6389 || hpos < dpyinfo->mouse_face_end_col
6390 || dpyinfo->mouse_face_past_end)))
6391 {
6392 /* Clear the display of the old active region, if any. */
6393 clear_mouse_face (dpyinfo);
6394
6395 /* Find the highest priority overlay that has a mouse-face prop. */
6396 overlay = Qnil;
6397 for (i = 0; i < noverlays; i++)
6398 {
6399 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6400 if (!NILP (mouse_face))
6401 {
6402 overlay = overlay_vec[i];
6403 break;
6404 }
6405 }
6406
6407 /* If no overlay applies, get a text property. */
6408 if (NILP (overlay))
6409 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6410
6411 /* Handle the overlay case. */
6412 if (! NILP (overlay))
6413 {
6414 /* Find the range of text around this char that
6415 should be active. */
6416 Lisp_Object before, after;
6417 int ignore;
6418
6419 before = Foverlay_start (overlay);
6420 after = Foverlay_end (overlay);
6421 /* Record this as the current active region. */
6422 fast_find_position (w, XFASTINT (before),
6423 &dpyinfo->mouse_face_beg_col,
6424 &dpyinfo->mouse_face_beg_row,
6425 &dpyinfo->mouse_face_beg_x,
6426 &dpyinfo->mouse_face_beg_y);
6427 dpyinfo->mouse_face_past_end
6428 = !fast_find_position (w, XFASTINT (after),
6429 &dpyinfo->mouse_face_end_col,
6430 &dpyinfo->mouse_face_end_row,
6431 &dpyinfo->mouse_face_end_x,
6432 &dpyinfo->mouse_face_end_y);
6433 dpyinfo->mouse_face_window = window;
6434 dpyinfo->mouse_face_face_id
6435 = face_at_buffer_position (w, pos, 0, 0,
6436 &ignore, pos + 1, 1);
6437
6438 /* Display it as active. */
6439 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6440 }
6441 /* Handle the text property case. */
6442 else if (! NILP (mouse_face))
6443 {
6444 /* Find the range of text around this char that
6445 should be active. */
6446 Lisp_Object before, after, beginning, end;
6447 int ignore;
6448
6449 beginning = Fmarker_position (w->start);
6450 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6451 - XFASTINT (w->window_end_pos)));
6452 before
6453 = Fprevious_single_property_change (make_number (pos + 1),
6454 Qmouse_face,
6455 w->buffer, beginning);
6456 after
6457 = Fnext_single_property_change (position, Qmouse_face,
6458 w->buffer, end);
6459 /* Record this as the current active region. */
6460 fast_find_position (w, XFASTINT (before),
6461 &dpyinfo->mouse_face_beg_col,
6462 &dpyinfo->mouse_face_beg_row,
6463 &dpyinfo->mouse_face_beg_x,
6464 &dpyinfo->mouse_face_beg_y);
6465 dpyinfo->mouse_face_past_end
6466 = !fast_find_position (w, XFASTINT (after),
6467 &dpyinfo->mouse_face_end_col,
6468 &dpyinfo->mouse_face_end_row,
6469 &dpyinfo->mouse_face_end_x,
6470 &dpyinfo->mouse_face_end_y);
6471 dpyinfo->mouse_face_window = window;
6472 dpyinfo->mouse_face_face_id
6473 = face_at_buffer_position (w, pos, 0, 0,
6474 &ignore, pos + 1, 1);
6475
6476 /* Display it as active. */
6477 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6478 }
6479 }
6480
6481 /* Look for a `help-echo' property. */
6482 {
6483 Lisp_Object help;
6484
6485 /* Check overlays first. */
6486 help = Qnil;
6487 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6488 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6489
6490 /* Try text properties. */
6491 if (!STRINGP (help)
6492 && ((STRINGP (glyph->object)
6493 && glyph->charpos >= 0
6494 && glyph->charpos < XSTRING (glyph->object)->size)
6495 || (BUFFERP (glyph->object)
6496 && glyph->charpos >= BEGV
6497 && glyph->charpos < ZV)))
6498 help = Fget_text_property (make_number (glyph->charpos),
6499 Qhelp_echo, glyph->object);
6500
6501 if (STRINGP (help))
6502 help_echo = help;
6503 }
6504
6505 BEGV = obegv;
6506 ZV = ozv;
6507 current_buffer = obuf;
6508 }
6509 }
6510 }
6511
6512 static void
6513 redo_mouse_highlight ()
6514 {
6515 if (!NILP (last_mouse_motion_frame)
6516 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6517 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6518 last_mouse_motion_event.x,
6519 last_mouse_motion_event.y);
6520 }
6521
6522
6523 \f
6524 /***********************************************************************
6525 Tool-bars
6526 ***********************************************************************/
6527
6528 static int x_tool_bar_item P_ ((struct frame *, int, int,
6529 struct glyph **, int *, int *, int *));
6530
6531 /* Tool-bar item index of the item on which a mouse button was pressed
6532 or -1. */
6533
6534 static int last_tool_bar_item;
6535
6536
6537 /* Get information about the tool-bar item at position X/Y on frame F.
6538 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6539 the current matrix of the tool-bar window of F, or NULL if not
6540 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6541 item in F->current_tool_bar_items. Value is
6542
6543 -1 if X/Y is not on a tool-bar item
6544 0 if X/Y is on the same item that was highlighted before.
6545 1 otherwise. */
6546
6547 static int
6548 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6549 struct frame *f;
6550 int x, y;
6551 struct glyph **glyph;
6552 int *hpos, *vpos, *prop_idx;
6553 {
6554 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6555 struct window *w = XWINDOW (f->tool_bar_window);
6556 int area;
6557
6558 /* Find the glyph under X/Y. */
6559 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6560 if (*glyph == NULL)
6561 return -1;
6562
6563 /* Get the start of this tool-bar item's properties in
6564 f->current_tool_bar_items. */
6565 if (!tool_bar_item_info (f, *glyph, prop_idx))
6566 return -1;
6567
6568 /* Is mouse on the highlighted item? */
6569 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6570 && *vpos >= dpyinfo->mouse_face_beg_row
6571 && *vpos <= dpyinfo->mouse_face_end_row
6572 && (*vpos > dpyinfo->mouse_face_beg_row
6573 || *hpos >= dpyinfo->mouse_face_beg_col)
6574 && (*vpos < dpyinfo->mouse_face_end_row
6575 || *hpos < dpyinfo->mouse_face_end_col
6576 || dpyinfo->mouse_face_past_end))
6577 return 0;
6578
6579 return 1;
6580 }
6581
6582
6583 /* Handle mouse button event on the tool-bar of frame F, at
6584 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6585 or ButtonRelase. */
6586
6587 static void
6588 x_handle_tool_bar_click (f, button_event)
6589 struct frame *f;
6590 XButtonEvent *button_event;
6591 {
6592 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6593 struct window *w = XWINDOW (f->tool_bar_window);
6594 int hpos, vpos, prop_idx;
6595 struct glyph *glyph;
6596 Lisp_Object enabled_p;
6597 int x = button_event->x;
6598 int y = button_event->y;
6599
6600 /* If not on the highlighted tool-bar item, return. */
6601 frame_to_window_pixel_xy (w, &x, &y);
6602 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6603 return;
6604
6605 /* If item is disabled, do nothing. */
6606 enabled_p = (XVECTOR (f->current_tool_bar_items)
6607 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6608 if (NILP (enabled_p))
6609 return;
6610
6611 if (button_event->type == ButtonPress)
6612 {
6613 /* Show item in pressed state. */
6614 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6615 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6616 last_tool_bar_item = prop_idx;
6617 }
6618 else
6619 {
6620 Lisp_Object key, frame;
6621 struct input_event event;
6622
6623 /* Show item in released state. */
6624 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6625 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6626
6627 key = (XVECTOR (f->current_tool_bar_items)
6628 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6629
6630 XSETFRAME (frame, f);
6631 event.kind = TOOL_BAR_EVENT;
6632 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
6633 kbd_buffer_store_event (&event);
6634
6635 event.kind = TOOL_BAR_EVENT;
6636 event.frame_or_window = Fcons (frame, key);
6637 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6638 button_event->state);
6639 kbd_buffer_store_event (&event);
6640 last_tool_bar_item = -1;
6641 }
6642 }
6643
6644
6645 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6646 tool-bar window-relative coordinates X/Y. Called from
6647 note_mouse_highlight. */
6648
6649 static void
6650 note_tool_bar_highlight (f, x, y)
6651 struct frame *f;
6652 int x, y;
6653 {
6654 Lisp_Object window = f->tool_bar_window;
6655 struct window *w = XWINDOW (window);
6656 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6657 int hpos, vpos;
6658 struct glyph *glyph;
6659 struct glyph_row *row;
6660 int i;
6661 Lisp_Object enabled_p;
6662 int prop_idx;
6663 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6664 int mouse_down_p, rc;
6665
6666 /* Function note_mouse_highlight is called with negative x(y
6667 values when mouse moves outside of the frame. */
6668 if (x <= 0 || y <= 0)
6669 {
6670 clear_mouse_face (dpyinfo);
6671 return;
6672 }
6673
6674 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6675 if (rc < 0)
6676 {
6677 /* Not on tool-bar item. */
6678 clear_mouse_face (dpyinfo);
6679 return;
6680 }
6681 else if (rc == 0)
6682 /* On same tool-bar item as before. */
6683 goto set_help_echo;
6684
6685 clear_mouse_face (dpyinfo);
6686
6687 /* Mouse is down, but on different tool-bar item? */
6688 mouse_down_p = (dpyinfo->grabbed
6689 && f == last_mouse_frame
6690 && FRAME_LIVE_P (f));
6691 if (mouse_down_p
6692 && last_tool_bar_item != prop_idx)
6693 return;
6694
6695 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6696 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6697
6698 /* If tool-bar item is not enabled, don't highlight it. */
6699 enabled_p = (XVECTOR (f->current_tool_bar_items)
6700 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6701 if (!NILP (enabled_p))
6702 {
6703 /* Compute the x-position of the glyph. In front and past the
6704 image is a space. We include this is the highlighted area. */
6705 row = MATRIX_ROW (w->current_matrix, vpos);
6706 for (i = x = 0; i < hpos; ++i)
6707 x += row->glyphs[TEXT_AREA][i].pixel_width;
6708
6709 /* Record this as the current active region. */
6710 dpyinfo->mouse_face_beg_col = hpos;
6711 dpyinfo->mouse_face_beg_row = vpos;
6712 dpyinfo->mouse_face_beg_x = x;
6713 dpyinfo->mouse_face_beg_y = row->y;
6714 dpyinfo->mouse_face_past_end = 0;
6715
6716 dpyinfo->mouse_face_end_col = hpos + 1;
6717 dpyinfo->mouse_face_end_row = vpos;
6718 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6719 dpyinfo->mouse_face_end_y = row->y;
6720 dpyinfo->mouse_face_window = window;
6721 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6722
6723 /* Display it as active. */
6724 show_mouse_face (dpyinfo, draw);
6725 dpyinfo->mouse_face_image_state = draw;
6726 }
6727
6728 set_help_echo:
6729
6730 /* Set help_echo to a help string.to display for this tool-bar item.
6731 XTread_socket does the rest. */
6732 help_echo = (XVECTOR (f->current_tool_bar_items)
6733 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6734 if (!STRINGP (help_echo))
6735 help_echo = (XVECTOR (f->current_tool_bar_items)
6736 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6737 }
6738
6739
6740 \f
6741 /* Find the glyph matrix position of buffer position POS in window W.
6742 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6743 current glyphs must be up to date. If POS is above window start
6744 return (0, 0, 0, 0). If POS is after end of W, return end of
6745 last line in W. */
6746
6747 static int
6748 fast_find_position (w, pos, hpos, vpos, x, y)
6749 struct window *w;
6750 int pos;
6751 int *hpos, *vpos, *x, *y;
6752 {
6753 int i;
6754 int lastcol;
6755 int maybe_next_line_p = 0;
6756 int line_start_position;
6757 int yb = window_text_bottom_y (w);
6758 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6759 struct glyph_row *best_row = row;
6760 int row_vpos = 0, best_row_vpos = 0;
6761 int current_x;
6762
6763 while (row->y < yb)
6764 {
6765 if (row->used[TEXT_AREA])
6766 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6767 else
6768 line_start_position = 0;
6769
6770 if (line_start_position > pos)
6771 break;
6772 /* If the position sought is the end of the buffer,
6773 don't include the blank lines at the bottom of the window. */
6774 else if (line_start_position == pos
6775 && pos == BUF_ZV (XBUFFER (w->buffer)))
6776 {
6777 maybe_next_line_p = 1;
6778 break;
6779 }
6780 else if (line_start_position > 0)
6781 {
6782 best_row = row;
6783 best_row_vpos = row_vpos;
6784 }
6785
6786 ++row;
6787 ++row_vpos;
6788 }
6789
6790 /* Find the right column within BEST_ROW. */
6791 lastcol = 0;
6792 current_x = best_row->x;
6793 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6794 {
6795 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6796 int charpos;
6797
6798 charpos = glyph->charpos;
6799 if (charpos == pos)
6800 {
6801 *hpos = i;
6802 *vpos = best_row_vpos;
6803 *x = current_x;
6804 *y = best_row->y;
6805 return 1;
6806 }
6807 else if (charpos > pos)
6808 break;
6809 else if (charpos > 0)
6810 lastcol = i;
6811
6812 current_x += glyph->pixel_width;
6813 }
6814
6815 /* If we're looking for the end of the buffer,
6816 and we didn't find it in the line we scanned,
6817 use the start of the following line. */
6818 if (maybe_next_line_p)
6819 {
6820 ++best_row;
6821 ++best_row_vpos;
6822 lastcol = 0;
6823 current_x = best_row->x;
6824 }
6825
6826 *vpos = best_row_vpos;
6827 *hpos = lastcol + 1;
6828 *x = current_x;
6829 *y = best_row->y;
6830 return 0;
6831 }
6832
6833
6834 /* Display the active region described by mouse_face_*
6835 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6836
6837 static void
6838 show_mouse_face (dpyinfo, draw)
6839 struct x_display_info *dpyinfo;
6840 enum draw_glyphs_face draw;
6841 {
6842 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6843 struct frame *f = XFRAME (WINDOW_FRAME (w));
6844 int i;
6845 int cursor_off_p = 0;
6846 struct cursor_pos saved_cursor;
6847
6848 saved_cursor = output_cursor;
6849
6850 /* If window is in the process of being destroyed, don't bother
6851 to do anything. */
6852 if (w->current_matrix == NULL)
6853 goto set_x_cursor;
6854
6855 /* Recognize when we are called to operate on rows that don't exist
6856 anymore. This can happen when a window is split. */
6857 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6858 goto set_x_cursor;
6859
6860 set_output_cursor (&w->phys_cursor);
6861
6862 /* Note that mouse_face_beg_row etc. are window relative. */
6863 for (i = dpyinfo->mouse_face_beg_row;
6864 i <= dpyinfo->mouse_face_end_row;
6865 i++)
6866 {
6867 int start_hpos, end_hpos, start_x;
6868 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6869
6870 /* Don't do anything if row doesn't have valid contents. */
6871 if (!row->enabled_p)
6872 continue;
6873
6874 /* For all but the first row, the highlight starts at column 0. */
6875 if (i == dpyinfo->mouse_face_beg_row)
6876 {
6877 start_hpos = dpyinfo->mouse_face_beg_col;
6878 start_x = dpyinfo->mouse_face_beg_x;
6879 }
6880 else
6881 {
6882 start_hpos = 0;
6883 start_x = 0;
6884 }
6885
6886 if (i == dpyinfo->mouse_face_end_row)
6887 end_hpos = dpyinfo->mouse_face_end_col;
6888 else
6889 end_hpos = row->used[TEXT_AREA];
6890
6891 /* If the cursor's in the text we are about to rewrite, turn the
6892 cursor off. */
6893 if (!w->pseudo_window_p
6894 && i == output_cursor.vpos
6895 && output_cursor.hpos >= start_hpos - 1
6896 && output_cursor.hpos <= end_hpos)
6897 {
6898 x_update_window_cursor (w, 0);
6899 cursor_off_p = 1;
6900 }
6901
6902 if (end_hpos > start_hpos)
6903 x_draw_glyphs (w, start_x, row, TEXT_AREA,
6904 start_hpos, end_hpos, draw, NULL, NULL, 0);
6905 }
6906
6907 /* If we turned the cursor off, turn it back on. */
6908 if (cursor_off_p)
6909 x_display_cursor (w, 1,
6910 output_cursor.hpos, output_cursor.vpos,
6911 output_cursor.x, output_cursor.y);
6912
6913 output_cursor = saved_cursor;
6914
6915 set_x_cursor:
6916
6917 /* Change the mouse cursor. */
6918 if (draw == DRAW_NORMAL_TEXT)
6919 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6920 f->output_data.x->text_cursor);
6921 else if (draw == DRAW_MOUSE_FACE)
6922 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6923 f->output_data.x->cross_cursor);
6924 else
6925 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6926 f->output_data.x->nontext_cursor);
6927 }
6928
6929 /* Clear out the mouse-highlighted active region.
6930 Redraw it un-highlighted first. */
6931
6932 void
6933 clear_mouse_face (dpyinfo)
6934 struct x_display_info *dpyinfo;
6935 {
6936 if (tip_frame)
6937 return;
6938
6939 if (! NILP (dpyinfo->mouse_face_window))
6940 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
6941
6942 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6943 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6944 dpyinfo->mouse_face_window = Qnil;
6945 }
6946
6947 /* Just discard the mouse face information for frame F, if any.
6948 This is used when the size of F is changed. */
6949
6950 void
6951 cancel_mouse_face (f)
6952 FRAME_PTR f;
6953 {
6954 Lisp_Object window;
6955 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6956
6957 window = dpyinfo->mouse_face_window;
6958 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6959 {
6960 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6961 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6962 dpyinfo->mouse_face_window = Qnil;
6963 }
6964 }
6965 \f
6966 static struct scroll_bar *x_window_to_scroll_bar ();
6967 static void x_scroll_bar_report_motion ();
6968
6969 /* Return the current position of the mouse.
6970 *fp should be a frame which indicates which display to ask about.
6971
6972 If the mouse movement started in a scroll bar, set *fp, *bar_window,
6973 and *part to the frame, window, and scroll bar part that the mouse
6974 is over. Set *x and *y to the portion and whole of the mouse's
6975 position on the scroll bar.
6976
6977 If the mouse movement started elsewhere, set *fp to the frame the
6978 mouse is on, *bar_window to nil, and *x and *y to the character cell
6979 the mouse is over.
6980
6981 Set *time to the server time-stamp for the time at which the mouse
6982 was at this position.
6983
6984 Don't store anything if we don't have a valid set of values to report.
6985
6986 This clears the mouse_moved flag, so we can wait for the next mouse
6987 movement. */
6988
6989 static void
6990 XTmouse_position (fp, insist, bar_window, part, x, y, time)
6991 FRAME_PTR *fp;
6992 int insist;
6993 Lisp_Object *bar_window;
6994 enum scroll_bar_part *part;
6995 Lisp_Object *x, *y;
6996 unsigned long *time;
6997 {
6998 FRAME_PTR f1;
6999
7000 BLOCK_INPUT;
7001
7002 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7003 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7004 else
7005 {
7006 Window root;
7007 int root_x, root_y;
7008
7009 Window dummy_window;
7010 int dummy;
7011
7012 Lisp_Object frame, tail;
7013
7014 /* Clear the mouse-moved flag for every frame on this display. */
7015 FOR_EACH_FRAME (tail, frame)
7016 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7017 XFRAME (frame)->mouse_moved = 0;
7018
7019 last_mouse_scroll_bar = Qnil;
7020
7021 /* Figure out which root window we're on. */
7022 XQueryPointer (FRAME_X_DISPLAY (*fp),
7023 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7024
7025 /* The root window which contains the pointer. */
7026 &root,
7027
7028 /* Trash which we can't trust if the pointer is on
7029 a different screen. */
7030 &dummy_window,
7031
7032 /* The position on that root window. */
7033 &root_x, &root_y,
7034
7035 /* More trash we can't trust. */
7036 &dummy, &dummy,
7037
7038 /* Modifier keys and pointer buttons, about which
7039 we don't care. */
7040 (unsigned int *) &dummy);
7041
7042 /* Now we have a position on the root; find the innermost window
7043 containing the pointer. */
7044 {
7045 Window win, child;
7046 int win_x, win_y;
7047 int parent_x = 0, parent_y = 0;
7048 int count;
7049
7050 win = root;
7051
7052 /* XTranslateCoordinates can get errors if the window
7053 structure is changing at the same time this function
7054 is running. So at least we must not crash from them. */
7055
7056 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7057
7058 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7059 && FRAME_LIVE_P (last_mouse_frame))
7060 {
7061 /* If mouse was grabbed on a frame, give coords for that frame
7062 even if the mouse is now outside it. */
7063 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7064
7065 /* From-window, to-window. */
7066 root, FRAME_X_WINDOW (last_mouse_frame),
7067
7068 /* From-position, to-position. */
7069 root_x, root_y, &win_x, &win_y,
7070
7071 /* Child of win. */
7072 &child);
7073 f1 = last_mouse_frame;
7074 }
7075 else
7076 {
7077 while (1)
7078 {
7079 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7080
7081 /* From-window, to-window. */
7082 root, win,
7083
7084 /* From-position, to-position. */
7085 root_x, root_y, &win_x, &win_y,
7086
7087 /* Child of win. */
7088 &child);
7089
7090 if (child == None || child == win)
7091 break;
7092
7093 win = child;
7094 parent_x = win_x;
7095 parent_y = win_y;
7096 }
7097
7098 /* Now we know that:
7099 win is the innermost window containing the pointer
7100 (XTC says it has no child containing the pointer),
7101 win_x and win_y are the pointer's position in it
7102 (XTC did this the last time through), and
7103 parent_x and parent_y are the pointer's position in win's parent.
7104 (They are what win_x and win_y were when win was child.
7105 If win is the root window, it has no parent, and
7106 parent_{x,y} are invalid, but that's okay, because we'll
7107 never use them in that case.) */
7108
7109 /* Is win one of our frames? */
7110 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7111 }
7112
7113 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7114 f1 = 0;
7115
7116 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7117
7118 /* If not, is it one of our scroll bars? */
7119 if (! f1)
7120 {
7121 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7122
7123 if (bar)
7124 {
7125 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7126 win_x = parent_x;
7127 win_y = parent_y;
7128 }
7129 }
7130
7131 if (f1 == 0 && insist > 0)
7132 f1 = SELECTED_FRAME ();
7133
7134 if (f1)
7135 {
7136 /* Ok, we found a frame. Store all the values.
7137 last_mouse_glyph is a rectangle used to reduce the
7138 generation of mouse events. To not miss any motion
7139 events, we must divide the frame into rectangles of the
7140 size of the smallest character that could be displayed
7141 on it, i.e. into the same rectangles that matrices on
7142 the frame are divided into. */
7143
7144 #if OLD_REDISPLAY_CODE
7145 int ignore1, ignore2;
7146 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7147 &last_mouse_glyph,
7148 FRAME_X_DISPLAY_INFO (f1)->grabbed
7149 || insist);
7150 #else
7151 {
7152 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7153 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7154 int x = win_x;
7155 int y = win_y;
7156
7157 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7158 round down even for negative values. */
7159 if (x < 0)
7160 x -= width - 1;
7161 if (y < 0)
7162 y -= height - 1;
7163
7164 last_mouse_glyph.width = width;
7165 last_mouse_glyph.height = height;
7166 last_mouse_glyph.x = (x + width - 1) / width * width;
7167 last_mouse_glyph.y = (y + height - 1) / height * height;
7168 }
7169 #endif
7170
7171 *bar_window = Qnil;
7172 *part = 0;
7173 *fp = f1;
7174 XSETINT (*x, win_x);
7175 XSETINT (*y, win_y);
7176 *time = last_mouse_movement_time;
7177 }
7178 }
7179 }
7180
7181 UNBLOCK_INPUT;
7182 }
7183
7184
7185 #ifdef USE_X_TOOLKIT
7186
7187 /* Atimer callback function for TIMER. Called every 0.1s to process
7188 Xt timeouts, if needed. We must avoid calling XtAppPending as
7189 much as possible because that function does an implicit XFlush
7190 that slows us down. */
7191
7192 static void
7193 x_process_timeouts (timer)
7194 struct atimer *timer;
7195 {
7196 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7197 {
7198 BLOCK_INPUT;
7199 while (XtAppPending (Xt_app_con) & XtIMTimer)
7200 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7201 UNBLOCK_INPUT;
7202 }
7203 }
7204
7205 #endif /* USE_X_TOOLKIT */
7206
7207 \f
7208 /* Scroll bar support. */
7209
7210 /* Given an X window ID, find the struct scroll_bar which manages it.
7211 This can be called in GC, so we have to make sure to strip off mark
7212 bits. */
7213
7214 static struct scroll_bar *
7215 x_window_to_scroll_bar (window_id)
7216 Window window_id;
7217 {
7218 Lisp_Object tail;
7219
7220 for (tail = Vframe_list;
7221 XGCTYPE (tail) == Lisp_Cons;
7222 tail = XCDR (tail))
7223 {
7224 Lisp_Object frame, bar, condemned;
7225
7226 frame = XCAR (tail);
7227 /* All elements of Vframe_list should be frames. */
7228 if (! GC_FRAMEP (frame))
7229 abort ();
7230
7231 /* Scan this frame's scroll bar list for a scroll bar with the
7232 right window ID. */
7233 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7234 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7235 /* This trick allows us to search both the ordinary and
7236 condemned scroll bar lists with one loop. */
7237 ! GC_NILP (bar) || (bar = condemned,
7238 condemned = Qnil,
7239 ! GC_NILP (bar));
7240 bar = XSCROLL_BAR (bar)->next)
7241 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7242 return XSCROLL_BAR (bar);
7243 }
7244
7245 return 0;
7246 }
7247
7248
7249 \f
7250 /************************************************************************
7251 Toolkit scroll bars
7252 ************************************************************************/
7253
7254 #if USE_TOOLKIT_SCROLL_BARS
7255
7256 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7257 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7258 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7259 struct scroll_bar *));
7260 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7261 int, int, int));
7262
7263
7264 /* Id of action hook installed for scroll bars. */
7265
7266 static XtActionHookId action_hook_id;
7267
7268 /* Lisp window being scrolled. Set when starting to interact with
7269 a toolkit scroll bar, reset to nil when ending the interaction. */
7270
7271 static Lisp_Object window_being_scrolled;
7272
7273 /* Last scroll bar part sent in xm_scroll_callback. */
7274
7275 static int last_scroll_bar_part;
7276
7277 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7278 that movements of 1/20 of the screen size are mapped to up/down. */
7279
7280 static Boolean xaw3d_arrow_scroll;
7281
7282 /* Whether the drag scrolling maintains the mouse at the top of the
7283 thumb. If not, resizing the thumb needs to be done more carefully
7284 to avoid jerkyness. */
7285
7286 static Boolean xaw3d_pick_top;
7287
7288
7289 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7290 bars are used.. The hook is responsible for detecting when
7291 the user ends an interaction with the scroll bar, and generates
7292 a `end-scroll' scroll_bar_click' event if so. */
7293
7294 static void
7295 xt_action_hook (widget, client_data, action_name, event, params,
7296 num_params)
7297 Widget widget;
7298 XtPointer client_data;
7299 String action_name;
7300 XEvent *event;
7301 String *params;
7302 Cardinal *num_params;
7303 {
7304 int scroll_bar_p;
7305 char *end_action;
7306
7307 #ifdef USE_MOTIF
7308 scroll_bar_p = XmIsScrollBar (widget);
7309 end_action = "Release";
7310 #else /* !USE_MOTIF i.e. use Xaw */
7311 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7312 end_action = "EndScroll";
7313 #endif /* USE_MOTIF */
7314
7315 if (scroll_bar_p
7316 && strcmp (action_name, end_action) == 0
7317 && WINDOWP (window_being_scrolled))
7318 {
7319 struct window *w;
7320
7321 x_send_scroll_bar_event (window_being_scrolled,
7322 scroll_bar_end_scroll, 0, 0);
7323 w = XWINDOW (window_being_scrolled);
7324 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7325 window_being_scrolled = Qnil;
7326 last_scroll_bar_part = -1;
7327
7328 /* Xt timeouts no longer needed. */
7329 toolkit_scroll_bar_interaction = 0;
7330 }
7331 }
7332
7333
7334 /* Send a client message with message type Xatom_Scrollbar for a
7335 scroll action to the frame of WINDOW. PART is a value identifying
7336 the part of the scroll bar that was clicked on. PORTION is the
7337 amount to scroll of a whole of WHOLE. */
7338
7339 static void
7340 x_send_scroll_bar_event (window, part, portion, whole)
7341 Lisp_Object window;
7342 int part, portion, whole;
7343 {
7344 XEvent event;
7345 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7346 struct frame *f = XFRAME (XWINDOW (window)->frame);
7347
7348 /* Construct a ClientMessage event to send to the frame. */
7349 ev->type = ClientMessage;
7350 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7351 ev->display = FRAME_X_DISPLAY (f);
7352 ev->window = FRAME_X_WINDOW (f);
7353 ev->format = 32;
7354 ev->data.l[0] = (long) window;
7355 ev->data.l[1] = (long) part;
7356 ev->data.l[2] = (long) 0;
7357 ev->data.l[3] = (long) portion;
7358 ev->data.l[4] = (long) whole;
7359
7360 /* Make Xt timeouts work while the scroll bar is active. */
7361 toolkit_scroll_bar_interaction = 1;
7362
7363 /* Setting the event mask to zero means that the message will
7364 be sent to the client that created the window, and if that
7365 window no longer exists, no event will be sent. */
7366 BLOCK_INPUT;
7367 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7368 UNBLOCK_INPUT;
7369 }
7370
7371
7372 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7373 in *IEVENT. */
7374
7375 static void
7376 x_scroll_bar_to_input_event (event, ievent)
7377 XEvent *event;
7378 struct input_event *ievent;
7379 {
7380 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7381 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7382 struct frame *f = XFRAME (XWINDOW (window)->frame);
7383
7384 ievent->kind = scroll_bar_click;
7385 ievent->frame_or_window = window;
7386 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7387 ievent->part = ev->data.l[1];
7388 ievent->code = ev->data.l[2];
7389 ievent->x = make_number ((int) ev->data.l[3]);
7390 ievent->y = make_number ((int) ev->data.l[4]);
7391 ievent->modifiers = 0;
7392 }
7393
7394
7395 #ifdef USE_MOTIF
7396
7397 /* Minimum and maximum values used for Motif scroll bars. */
7398
7399 #define XM_SB_MIN 1
7400 #define XM_SB_MAX 10000000
7401 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7402
7403
7404 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7405 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7406 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7407
7408 static void
7409 xm_scroll_callback (widget, client_data, call_data)
7410 Widget widget;
7411 XtPointer client_data, call_data;
7412 {
7413 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7414 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7415 double percent;
7416 int part = -1, whole = 0, portion = 0;
7417
7418 switch (cs->reason)
7419 {
7420 case XmCR_DECREMENT:
7421 bar->dragging = Qnil;
7422 part = scroll_bar_up_arrow;
7423 break;
7424
7425 case XmCR_INCREMENT:
7426 bar->dragging = Qnil;
7427 part = scroll_bar_down_arrow;
7428 break;
7429
7430 case XmCR_PAGE_DECREMENT:
7431 bar->dragging = Qnil;
7432 part = scroll_bar_above_handle;
7433 break;
7434
7435 case XmCR_PAGE_INCREMENT:
7436 bar->dragging = Qnil;
7437 part = scroll_bar_below_handle;
7438 break;
7439
7440 case XmCR_TO_TOP:
7441 bar->dragging = Qnil;
7442 part = scroll_bar_to_top;
7443 break;
7444
7445 case XmCR_TO_BOTTOM:
7446 bar->dragging = Qnil;
7447 part = scroll_bar_to_bottom;
7448 break;
7449
7450 case XmCR_DRAG:
7451 {
7452 int slider_size;
7453 int dragging_down_p = (INTEGERP (bar->dragging)
7454 && XINT (bar->dragging) <= cs->value);
7455
7456 /* Get the slider size. */
7457 BLOCK_INPUT;
7458 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7459 UNBLOCK_INPUT;
7460
7461 /* At the max position of the scroll bar, do a line-wise
7462 movement. Without doing anything, the LessTif scroll bar
7463 calls us with the same cs->value again and again. If we
7464 want to make sure that we can reach the end of the buffer,
7465 we have to do something.
7466
7467 Implementation note: setting bar->dragging always to
7468 cs->value gives a smoother movement at the max position.
7469 Setting it to nil when doing line-wise movement gives
7470 a better slider behavior. */
7471
7472 if (cs->value + slider_size == XM_SB_MAX
7473 || (dragging_down_p
7474 && last_scroll_bar_part == scroll_bar_down_arrow))
7475 {
7476 part = scroll_bar_down_arrow;
7477 bar->dragging = Qnil;
7478 }
7479 else
7480 {
7481 whole = XM_SB_RANGE;
7482 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7483 part = scroll_bar_handle;
7484 bar->dragging = make_number (cs->value);
7485 }
7486 }
7487 break;
7488
7489 case XmCR_VALUE_CHANGED:
7490 break;
7491 };
7492
7493 if (part >= 0)
7494 {
7495 window_being_scrolled = bar->window;
7496 last_scroll_bar_part = part;
7497 x_send_scroll_bar_event (bar->window, part, portion, whole);
7498 }
7499 }
7500
7501
7502 #else /* !USE_MOTIF, i.e. Xaw. */
7503
7504
7505 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7506 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7507 scroll bar struct. CALL_DATA is a pointer to a float saying where
7508 the thumb is. */
7509
7510 static void
7511 xaw_jump_callback (widget, client_data, call_data)
7512 Widget widget;
7513 XtPointer client_data, call_data;
7514 {
7515 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7516 float top = *(float *) call_data;
7517 float shown;
7518 int whole, portion, height;
7519 int part;
7520
7521 /* Get the size of the thumb, a value between 0 and 1. */
7522 BLOCK_INPUT;
7523 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7524 UNBLOCK_INPUT;
7525
7526 whole = 10000000;
7527 portion = shown < 1 ? top * whole : 0;
7528
7529 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7530 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7531 the bottom, so we force the scrolling whenever we see that we're
7532 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7533 we try to ensure that we always stay two pixels away from the
7534 bottom). */
7535 part = scroll_bar_down_arrow;
7536 else
7537 part = scroll_bar_handle;
7538
7539 window_being_scrolled = bar->window;
7540 bar->dragging = make_number (portion);
7541 last_scroll_bar_part = part;
7542 x_send_scroll_bar_event (bar->window, part, portion, whole);
7543 }
7544
7545
7546 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7547 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7548 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7549 the scroll bar. CALL_DATA is an integer specifying the action that
7550 has taken place. It's magnitude is in the range 0..height of the
7551 scroll bar. Negative values mean scroll towards buffer start.
7552 Values < height of scroll bar mean line-wise movement. */
7553
7554 static void
7555 xaw_scroll_callback (widget, client_data, call_data)
7556 Widget widget;
7557 XtPointer client_data, call_data;
7558 {
7559 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7560 int position = (int) call_data;
7561 Dimension height;
7562 int part;
7563
7564 /* Get the height of the scroll bar. */
7565 BLOCK_INPUT;
7566 XtVaGetValues (widget, XtNheight, &height, NULL);
7567 UNBLOCK_INPUT;
7568
7569 if (abs (position) >= height)
7570 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7571
7572 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7573 it maps line-movement to call_data = max(5, height/20). */
7574 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7575 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7576 else
7577 part = scroll_bar_move_ratio;
7578
7579 window_being_scrolled = bar->window;
7580 bar->dragging = Qnil;
7581 last_scroll_bar_part = part;
7582 x_send_scroll_bar_event (bar->window, part, position, height);
7583 }
7584
7585
7586 #endif /* not USE_MOTIF */
7587
7588
7589 /* Create the widget for scroll bar BAR on frame F. Record the widget
7590 and X window of the scroll bar in BAR. */
7591
7592 static void
7593 x_create_toolkit_scroll_bar (f, bar)
7594 struct frame *f;
7595 struct scroll_bar *bar;
7596 {
7597 Window xwindow;
7598 Widget widget;
7599 Arg av[20];
7600 int ac = 0;
7601 char *scroll_bar_name = "verticalScrollBar";
7602 unsigned long pixel;
7603
7604 BLOCK_INPUT;
7605
7606 #ifdef USE_MOTIF
7607 /* LessTif 0.85, problems:
7608
7609 1. When the mouse if over the scroll bar, the scroll bar will
7610 get keyboard events. I didn't find a way to turn this off.
7611
7612 2. Do we have to explicitly set the cursor to get an arrow
7613 cursor (see below)? */
7614
7615 /* Set resources. Create the widget. */
7616 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7617 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7618 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7619 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7620 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7621 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7622 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7623
7624 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7625 if (pixel != -1)
7626 {
7627 XtSetArg (av[ac], XmNforeground, pixel);
7628 ++ac;
7629 }
7630
7631 pixel = f->output_data.x->scroll_bar_background_pixel;
7632 if (pixel != -1)
7633 {
7634 XtSetArg (av[ac], XmNbackground, pixel);
7635 ++ac;
7636 }
7637
7638 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7639 scroll_bar_name, av, ac);
7640
7641 /* Add one callback for everything that can happen. */
7642 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7643 (XtPointer) bar);
7644 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7645 (XtPointer) bar);
7646 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7647 (XtPointer) bar);
7648 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7649 (XtPointer) bar);
7650 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7651 (XtPointer) bar);
7652 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7653 (XtPointer) bar);
7654 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7655 (XtPointer) bar);
7656
7657 /* Realize the widget. Only after that is the X window created. */
7658 XtRealizeWidget (widget);
7659
7660 /* Set the cursor to an arrow. I didn't find a resource to do that.
7661 And I'm wondering why it hasn't an arrow cursor by default. */
7662 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7663 f->output_data.x->nontext_cursor);
7664
7665 #else /* !USE_MOTIF i.e. use Xaw */
7666
7667 /* Set resources. Create the widget. The background of the
7668 Xaw3d scroll bar widget is a little bit light for my taste.
7669 We don't alter it here to let users change it according
7670 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7671 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7672 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7673 /* For smoother scrolling with Xaw3d -sm */
7674 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7675 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7676
7677 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7678 if (pixel != -1)
7679 {
7680 XtSetArg (av[ac], XtNforeground, pixel);
7681 ++ac;
7682 }
7683
7684 pixel = f->output_data.x->scroll_bar_background_pixel;
7685 if (pixel != -1)
7686 {
7687 XtSetArg (av[ac], XtNbackground, pixel);
7688 ++ac;
7689 }
7690
7691 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7692 f->output_data.x->edit_widget, av, ac);
7693
7694 {
7695 char *initial = "";
7696 char *val = initial;
7697 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7698 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7699 if (val == initial)
7700 { /* ARROW_SCROLL */
7701 xaw3d_arrow_scroll = True;
7702 /* Isn't that just a personal preference ? -sm */
7703 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7704 }
7705 }
7706
7707 /* Define callbacks. */
7708 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7709 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7710 (XtPointer) bar);
7711
7712 /* Realize the widget. Only after that is the X window created. */
7713 XtRealizeWidget (widget);
7714
7715 #endif /* !USE_MOTIF */
7716
7717 /* Install an action hook that let's us detect when the user
7718 finishes interacting with a scroll bar. */
7719 if (action_hook_id == 0)
7720 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7721
7722 /* Remember X window and widget in the scroll bar vector. */
7723 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7724 xwindow = XtWindow (widget);
7725 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7726
7727 UNBLOCK_INPUT;
7728 }
7729
7730
7731 /* Set the thumb size and position of scroll bar BAR. We are currently
7732 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7733
7734 static void
7735 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7736 struct scroll_bar *bar;
7737 int portion, position, whole;
7738 {
7739 float top, shown;
7740 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7741
7742 if (whole == 0)
7743 top = 0, shown = 1;
7744 else
7745 {
7746 top = (float) position / whole;
7747 shown = (float) portion / whole;
7748 }
7749
7750 BLOCK_INPUT;
7751
7752 #ifdef USE_MOTIF
7753 {
7754 int size, value;
7755 Boolean arrow1_selected, arrow2_selected;
7756 unsigned char flags;
7757 XmScrollBarWidget sb;
7758
7759 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7760 is the scroll bar's maximum and MIN is the scroll bar's minimum
7761 value. */
7762 size = shown * XM_SB_RANGE;
7763 size = min (size, XM_SB_RANGE);
7764 size = max (size, 1);
7765
7766 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7767 value = top * XM_SB_RANGE;
7768 value = min (value, XM_SB_MAX - size);
7769 value = max (value, XM_SB_MIN);
7770
7771 /* LessTif: Calling XmScrollBarSetValues after an increment or
7772 decrement turns off auto-repeat LessTif-internally. This can
7773 be seen in ScrollBar.c which resets Arrow1Selected and
7774 Arrow2Selected. It also sets internal flags so that LessTif
7775 believes the mouse is in the slider. We either have to change
7776 our code, or work around that by accessing private data. */
7777
7778 sb = (XmScrollBarWidget) widget;
7779 arrow1_selected = sb->scrollBar.arrow1_selected;
7780 arrow2_selected = sb->scrollBar.arrow2_selected;
7781 flags = sb->scrollBar.flags;
7782
7783 if (NILP (bar->dragging))
7784 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7785 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7786 /* This has the negative side effect that the slider value is
7787 not what it would be if we scrolled here using line-wise or
7788 page-wise movement. */
7789 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7790 else
7791 {
7792 /* If currently dragging, only update the slider size.
7793 This reduces flicker effects. */
7794 int old_value, old_size, increment, page_increment;
7795
7796 XmScrollBarGetValues (widget, &old_value, &old_size,
7797 &increment, &page_increment);
7798 XmScrollBarSetValues (widget, old_value,
7799 min (size, XM_SB_RANGE - old_value),
7800 0, 0, False);
7801 }
7802
7803 sb->scrollBar.arrow1_selected = arrow1_selected;
7804 sb->scrollBar.arrow2_selected = arrow2_selected;
7805 sb->scrollBar.flags = flags;
7806 }
7807 #else /* !USE_MOTIF i.e. use Xaw */
7808 {
7809 float old_top, old_shown;
7810 Dimension height;
7811 XtVaGetValues (widget,
7812 XtNtopOfThumb, &old_top,
7813 XtNshown, &old_shown,
7814 XtNheight, &height,
7815 NULL);
7816
7817 /* Massage the top+shown values. */
7818 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7819 top = max (0, min (1, top));
7820 else
7821 top = old_top;
7822 /* Keep two pixels available for moving the thumb down. */
7823 shown = max (0, min (1 - top - (2.0 / height), shown));
7824
7825 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7826 check that your system's configuration file contains a define
7827 for `NARROWPROTO'. See s/freebsd.h for an example. */
7828 if (top != old_top || shown != old_shown)
7829 {
7830 if (NILP (bar->dragging))
7831 XawScrollbarSetThumb (widget, top, shown);
7832 else
7833 {
7834 #ifdef HAVE_XAW3D
7835 ScrollbarWidget sb = (ScrollbarWidget) widget;
7836 int scroll_mode;
7837
7838 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7839 if (xaw3d_arrow_scroll)
7840 {
7841 /* Xaw3d stupidly ignores resize requests while dragging
7842 so we have to make it believe it's not in dragging mode. */
7843 scroll_mode = sb->scrollbar.scroll_mode;
7844 if (scroll_mode == 2)
7845 sb->scrollbar.scroll_mode = 0;
7846 }
7847 #endif
7848 /* Try to make the scrolling a tad smoother. */
7849 if (!xaw3d_pick_top)
7850 shown = min (shown, old_shown);
7851
7852 XawScrollbarSetThumb (widget, top, shown);
7853
7854 #ifdef HAVE_XAW3D
7855 if (xaw3d_arrow_scroll && scroll_mode == 2)
7856 sb->scrollbar.scroll_mode = scroll_mode;
7857 #endif
7858 }
7859 }
7860 }
7861 #endif /* !USE_MOTIF */
7862
7863 UNBLOCK_INPUT;
7864 }
7865
7866 #endif /* USE_TOOLKIT_SCROLL_BARS */
7867
7868
7869 \f
7870 /************************************************************************
7871 Scroll bars, general
7872 ************************************************************************/
7873
7874 /* Create a scroll bar and return the scroll bar vector for it. W is
7875 the Emacs window on which to create the scroll bar. TOP, LEFT,
7876 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7877 scroll bar. */
7878
7879 static struct scroll_bar *
7880 x_scroll_bar_create (w, top, left, width, height)
7881 struct window *w;
7882 int top, left, width, height;
7883 {
7884 struct frame *f = XFRAME (w->frame);
7885 struct scroll_bar *bar
7886 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7887
7888 BLOCK_INPUT;
7889
7890 #if USE_TOOLKIT_SCROLL_BARS
7891 x_create_toolkit_scroll_bar (f, bar);
7892 #else /* not USE_TOOLKIT_SCROLL_BARS */
7893 {
7894 XSetWindowAttributes a;
7895 unsigned long mask;
7896 Window window;
7897
7898 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7899 if (a.background_pixel == -1)
7900 a.background_pixel = f->output_data.x->background_pixel;
7901
7902 a.event_mask = (ButtonPressMask | ButtonReleaseMask
7903 | ButtonMotionMask | PointerMotionHintMask
7904 | ExposureMask);
7905 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
7906
7907 mask = (CWBackPixel | CWEventMask | CWCursor);
7908
7909 /* Clear the area of W that will serve as a scroll bar. This is
7910 for the case that a window has been split horizontally. In
7911 this case, no clear_frame is generated to reduce flickering. */
7912 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7913 left, top, width,
7914 window_box_height (w), False);
7915
7916 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7917 /* Position and size of scroll bar. */
7918 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7919 top,
7920 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7921 height,
7922 /* Border width, depth, class, and visual. */
7923 0,
7924 CopyFromParent,
7925 CopyFromParent,
7926 CopyFromParent,
7927 /* Attributes. */
7928 mask, &a);
7929 SET_SCROLL_BAR_X_WINDOW (bar, window);
7930 }
7931 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7932
7933 XSETWINDOW (bar->window, w);
7934 XSETINT (bar->top, top);
7935 XSETINT (bar->left, left);
7936 XSETINT (bar->width, width);
7937 XSETINT (bar->height, height);
7938 XSETINT (bar->start, 0);
7939 XSETINT (bar->end, 0);
7940 bar->dragging = Qnil;
7941
7942 /* Add bar to its frame's list of scroll bars. */
7943 bar->next = FRAME_SCROLL_BARS (f);
7944 bar->prev = Qnil;
7945 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7946 if (!NILP (bar->next))
7947 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7948
7949 /* Map the window/widget. */
7950 #if USE_TOOLKIT_SCROLL_BARS
7951 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7952 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7953 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7954 top,
7955 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7956 height, 0);
7957 #else /* not USE_TOOLKIT_SCROLL_BARS */
7958 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
7959 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7960
7961 UNBLOCK_INPUT;
7962 return bar;
7963 }
7964
7965
7966 /* Draw BAR's handle in the proper position.
7967
7968 If the handle is already drawn from START to END, don't bother
7969 redrawing it, unless REBUILD is non-zero; in that case, always
7970 redraw it. (REBUILD is handy for drawing the handle after expose
7971 events.)
7972
7973 Normally, we want to constrain the start and end of the handle to
7974 fit inside its rectangle, but if the user is dragging the scroll
7975 bar handle, we want to let them drag it down all the way, so that
7976 the bar's top is as far down as it goes; otherwise, there's no way
7977 to move to the very end of the buffer. */
7978
7979 #ifndef USE_TOOLKIT_SCROLL_BARS
7980
7981 static void
7982 x_scroll_bar_set_handle (bar, start, end, rebuild)
7983 struct scroll_bar *bar;
7984 int start, end;
7985 int rebuild;
7986 {
7987 int dragging = ! NILP (bar->dragging);
7988 Window w = SCROLL_BAR_X_WINDOW (bar);
7989 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7990 GC gc = f->output_data.x->normal_gc;
7991
7992 /* If the display is already accurate, do nothing. */
7993 if (! rebuild
7994 && start == XINT (bar->start)
7995 && end == XINT (bar->end))
7996 return;
7997
7998 BLOCK_INPUT;
7999
8000 {
8001 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8002 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8003 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8004
8005 /* Make sure the values are reasonable, and try to preserve
8006 the distance between start and end. */
8007 {
8008 int length = end - start;
8009
8010 if (start < 0)
8011 start = 0;
8012 else if (start > top_range)
8013 start = top_range;
8014 end = start + length;
8015
8016 if (end < start)
8017 end = start;
8018 else if (end > top_range && ! dragging)
8019 end = top_range;
8020 }
8021
8022 /* Store the adjusted setting in the scroll bar. */
8023 XSETINT (bar->start, start);
8024 XSETINT (bar->end, end);
8025
8026 /* Clip the end position, just for display. */
8027 if (end > top_range)
8028 end = top_range;
8029
8030 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8031 below top positions, to make sure the handle is always at least
8032 that many pixels tall. */
8033 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8034
8035 /* Draw the empty space above the handle. Note that we can't clear
8036 zero-height areas; that means "clear to end of window." */
8037 if (0 < start)
8038 XClearArea (FRAME_X_DISPLAY (f), w,
8039
8040 /* x, y, width, height, and exposures. */
8041 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8042 VERTICAL_SCROLL_BAR_TOP_BORDER,
8043 inside_width, start,
8044 False);
8045
8046 /* Change to proper foreground color if one is specified. */
8047 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8048 XSetForeground (FRAME_X_DISPLAY (f), gc,
8049 f->output_data.x->scroll_bar_foreground_pixel);
8050
8051 /* Draw the handle itself. */
8052 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8053
8054 /* x, y, width, height */
8055 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8056 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8057 inside_width, end - start);
8058
8059 /* Restore the foreground color of the GC if we changed it above. */
8060 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8061 XSetForeground (FRAME_X_DISPLAY (f), gc,
8062 f->output_data.x->foreground_pixel);
8063
8064 /* Draw the empty space below the handle. Note that we can't
8065 clear zero-height areas; that means "clear to end of window." */
8066 if (end < inside_height)
8067 XClearArea (FRAME_X_DISPLAY (f), w,
8068
8069 /* x, y, width, height, and exposures. */
8070 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8071 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8072 inside_width, inside_height - end,
8073 False);
8074
8075 }
8076
8077 UNBLOCK_INPUT;
8078 }
8079
8080 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8081
8082 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8083 nil. */
8084
8085 static void
8086 x_scroll_bar_remove (bar)
8087 struct scroll_bar *bar;
8088 {
8089 BLOCK_INPUT;
8090
8091 #if USE_TOOLKIT_SCROLL_BARS
8092 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8093 #else /* not USE_TOOLKIT_SCROLL_BARS */
8094 {
8095 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8096 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8097 }
8098 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8099
8100 /* Disassociate this scroll bar from its window. */
8101 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8102
8103 UNBLOCK_INPUT;
8104 }
8105
8106
8107 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8108 that we are displaying PORTION characters out of a total of WHOLE
8109 characters, starting at POSITION. If WINDOW has no scroll bar,
8110 create one. */
8111
8112 static void
8113 XTset_vertical_scroll_bar (w, portion, whole, position)
8114 struct window *w;
8115 int portion, whole, position;
8116 {
8117 struct frame *f = XFRAME (w->frame);
8118 struct scroll_bar *bar;
8119 int top, height, left, sb_left, width, sb_width;
8120 int window_x, window_y, window_width, window_height;
8121
8122 /* Get window dimensions. */
8123 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8124 top = window_y;
8125 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8126 height = window_height;
8127
8128 /* Compute the left edge of the scroll bar area. */
8129 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8130 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8131 else
8132 left = XFASTINT (w->left);
8133 left *= CANON_X_UNIT (f);
8134 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8135
8136 /* Compute the width of the scroll bar which might be less than
8137 the width of the area reserved for the scroll bar. */
8138 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8139 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8140 else
8141 sb_width = width;
8142
8143 /* Compute the left edge of the scroll bar. */
8144 #ifdef USE_TOOLKIT_SCROLL_BARS
8145 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8146 sb_left = left + width - sb_width - (width - sb_width) / 2;
8147 else
8148 sb_left = left + (width - sb_width) / 2;
8149 #else
8150 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8151 sb_left = left + width - sb_width;
8152 else
8153 sb_left = left;
8154 #endif
8155
8156 /* Does the scroll bar exist yet? */
8157 if (NILP (w->vertical_scroll_bar))
8158 {
8159 BLOCK_INPUT;
8160 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8161 left, top, width, height, False);
8162 UNBLOCK_INPUT;
8163 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8164 }
8165 else
8166 {
8167 /* It may just need to be moved and resized. */
8168 unsigned int mask = 0;
8169
8170 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8171
8172 BLOCK_INPUT;
8173
8174 if (sb_left != XINT (bar->left))
8175 mask |= CWX;
8176 if (top != XINT (bar->top))
8177 mask |= CWY;
8178 if (sb_width != XINT (bar->width))
8179 mask |= CWWidth;
8180 if (height != XINT (bar->height))
8181 mask |= CWHeight;
8182
8183 #ifdef USE_TOOLKIT_SCROLL_BARS
8184
8185 /* Since toolkit scroll bars are smaller than the space reserved
8186 for them on the frame, we have to clear "under" them. */
8187 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8188 left, top, width, height, False);
8189
8190 /* Move/size the scroll bar widget. */
8191 if (mask)
8192 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8193 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8194 top,
8195 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8196 height, 0);
8197
8198 #else /* not USE_TOOLKIT_SCROLL_BARS */
8199
8200 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8201 {
8202 /* Clear areas not covered by the scroll bar. This makes sure a
8203 previous mode line display is cleared after C-x 2 C-x 1, for
8204 example. Non-toolkit scroll bars are as wide as the area
8205 reserved for scroll bars - trim at both sides. */
8206 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8207 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8208 height, False);
8209 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8210 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8211 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8212 height, False);
8213 }
8214
8215 /* Move/size the scroll bar window. */
8216 if (mask)
8217 {
8218 XWindowChanges wc;
8219
8220 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8221 wc.y = top;
8222 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8223 wc.height = height;
8224 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8225 mask, &wc);
8226 }
8227
8228 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8229
8230 /* Remember new settings. */
8231 XSETINT (bar->left, sb_left);
8232 XSETINT (bar->top, top);
8233 XSETINT (bar->width, sb_width);
8234 XSETINT (bar->height, height);
8235
8236 UNBLOCK_INPUT;
8237 }
8238
8239 #if USE_TOOLKIT_SCROLL_BARS
8240 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8241 #else /* not USE_TOOLKIT_SCROLL_BARS */
8242 /* Set the scroll bar's current state, unless we're currently being
8243 dragged. */
8244 if (NILP (bar->dragging))
8245 {
8246 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8247
8248 if (whole == 0)
8249 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8250 else
8251 {
8252 int start = ((double) position * top_range) / whole;
8253 int end = ((double) (position + portion) * top_range) / whole;
8254 x_scroll_bar_set_handle (bar, start, end, 0);
8255 }
8256 }
8257 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8258
8259 XSETVECTOR (w->vertical_scroll_bar, bar);
8260 }
8261
8262
8263 /* The following three hooks are used when we're doing a thorough
8264 redisplay of the frame. We don't explicitly know which scroll bars
8265 are going to be deleted, because keeping track of when windows go
8266 away is a real pain - "Can you say set-window-configuration, boys
8267 and girls?" Instead, we just assert at the beginning of redisplay
8268 that *all* scroll bars are to be removed, and then save a scroll bar
8269 from the fiery pit when we actually redisplay its window. */
8270
8271 /* Arrange for all scroll bars on FRAME to be removed at the next call
8272 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8273 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8274
8275 static void
8276 XTcondemn_scroll_bars (frame)
8277 FRAME_PTR frame;
8278 {
8279 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8280 while (! NILP (FRAME_SCROLL_BARS (frame)))
8281 {
8282 Lisp_Object bar;
8283 bar = FRAME_SCROLL_BARS (frame);
8284 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8285 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8286 XSCROLL_BAR (bar)->prev = Qnil;
8287 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8288 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8289 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8290 }
8291 }
8292
8293 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8294 Note that WINDOW isn't necessarily condemned at all. */
8295 static void
8296 XTredeem_scroll_bar (window)
8297 struct window *window;
8298 {
8299 struct scroll_bar *bar;
8300
8301 /* We can't redeem this window's scroll bar if it doesn't have one. */
8302 if (NILP (window->vertical_scroll_bar))
8303 abort ();
8304
8305 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8306
8307 /* Unlink it from the condemned list. */
8308 {
8309 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8310
8311 if (NILP (bar->prev))
8312 {
8313 /* If the prev pointer is nil, it must be the first in one of
8314 the lists. */
8315 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8316 /* It's not condemned. Everything's fine. */
8317 return;
8318 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8319 window->vertical_scroll_bar))
8320 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8321 else
8322 /* If its prev pointer is nil, it must be at the front of
8323 one or the other! */
8324 abort ();
8325 }
8326 else
8327 XSCROLL_BAR (bar->prev)->next = bar->next;
8328
8329 if (! NILP (bar->next))
8330 XSCROLL_BAR (bar->next)->prev = bar->prev;
8331
8332 bar->next = FRAME_SCROLL_BARS (f);
8333 bar->prev = Qnil;
8334 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8335 if (! NILP (bar->next))
8336 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8337 }
8338 }
8339
8340 /* Remove all scroll bars on FRAME that haven't been saved since the
8341 last call to `*condemn_scroll_bars_hook'. */
8342
8343 static void
8344 XTjudge_scroll_bars (f)
8345 FRAME_PTR f;
8346 {
8347 Lisp_Object bar, next;
8348
8349 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8350
8351 /* Clear out the condemned list now so we won't try to process any
8352 more events on the hapless scroll bars. */
8353 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8354
8355 for (; ! NILP (bar); bar = next)
8356 {
8357 struct scroll_bar *b = XSCROLL_BAR (bar);
8358
8359 x_scroll_bar_remove (b);
8360
8361 next = b->next;
8362 b->next = b->prev = Qnil;
8363 }
8364
8365 /* Now there should be no references to the condemned scroll bars,
8366 and they should get garbage-collected. */
8367 }
8368
8369
8370 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8371 is a no-op when using toolkit scroll bars.
8372
8373 This may be called from a signal handler, so we have to ignore GC
8374 mark bits. */
8375
8376 static void
8377 x_scroll_bar_expose (bar, event)
8378 struct scroll_bar *bar;
8379 XEvent *event;
8380 {
8381 #ifndef USE_TOOLKIT_SCROLL_BARS
8382
8383 Window w = SCROLL_BAR_X_WINDOW (bar);
8384 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8385 GC gc = f->output_data.x->normal_gc;
8386 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8387
8388 BLOCK_INPUT;
8389
8390 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8391
8392 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8393 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8394
8395 /* x, y, width, height */
8396 0, 0,
8397 XINT (bar->width) - 1 - width_trim - width_trim,
8398 XINT (bar->height) - 1);
8399
8400 UNBLOCK_INPUT;
8401
8402 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8403 }
8404
8405 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8406 is set to something other than no_event, it is enqueued.
8407
8408 This may be called from a signal handler, so we have to ignore GC
8409 mark bits. */
8410
8411 #ifndef USE_TOOLKIT_SCROLL_BARS
8412
8413 static void
8414 x_scroll_bar_handle_click (bar, event, emacs_event)
8415 struct scroll_bar *bar;
8416 XEvent *event;
8417 struct input_event *emacs_event;
8418 {
8419 if (! GC_WINDOWP (bar->window))
8420 abort ();
8421
8422 emacs_event->kind = scroll_bar_click;
8423 emacs_event->code = event->xbutton.button - Button1;
8424 emacs_event->modifiers
8425 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8426 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8427 event->xbutton.state)
8428 | (event->type == ButtonRelease
8429 ? up_modifier
8430 : down_modifier));
8431 emacs_event->frame_or_window = bar->window;
8432 emacs_event->timestamp = event->xbutton.time;
8433 {
8434 #if 0
8435 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8436 int internal_height
8437 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8438 #endif
8439 int top_range
8440 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8441 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8442
8443 if (y < 0) y = 0;
8444 if (y > top_range) y = top_range;
8445
8446 if (y < XINT (bar->start))
8447 emacs_event->part = scroll_bar_above_handle;
8448 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8449 emacs_event->part = scroll_bar_handle;
8450 else
8451 emacs_event->part = scroll_bar_below_handle;
8452
8453 /* Just because the user has clicked on the handle doesn't mean
8454 they want to drag it. Lisp code needs to be able to decide
8455 whether or not we're dragging. */
8456 #if 0
8457 /* If the user has just clicked on the handle, record where they're
8458 holding it. */
8459 if (event->type == ButtonPress
8460 && emacs_event->part == scroll_bar_handle)
8461 XSETINT (bar->dragging, y - XINT (bar->start));
8462 #endif
8463
8464 /* If the user has released the handle, set it to its final position. */
8465 if (event->type == ButtonRelease
8466 && ! NILP (bar->dragging))
8467 {
8468 int new_start = y - XINT (bar->dragging);
8469 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8470
8471 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8472 bar->dragging = Qnil;
8473 }
8474
8475 /* Same deal here as the other #if 0. */
8476 #if 0
8477 /* Clicks on the handle are always reported as occurring at the top of
8478 the handle. */
8479 if (emacs_event->part == scroll_bar_handle)
8480 emacs_event->x = bar->start;
8481 else
8482 XSETINT (emacs_event->x, y);
8483 #else
8484 XSETINT (emacs_event->x, y);
8485 #endif
8486
8487 XSETINT (emacs_event->y, top_range);
8488 }
8489 }
8490
8491 /* Handle some mouse motion while someone is dragging the scroll bar.
8492
8493 This may be called from a signal handler, so we have to ignore GC
8494 mark bits. */
8495
8496 static void
8497 x_scroll_bar_note_movement (bar, event)
8498 struct scroll_bar *bar;
8499 XEvent *event;
8500 {
8501 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8502
8503 last_mouse_movement_time = event->xmotion.time;
8504
8505 f->mouse_moved = 1;
8506 XSETVECTOR (last_mouse_scroll_bar, bar);
8507
8508 /* If we're dragging the bar, display it. */
8509 if (! GC_NILP (bar->dragging))
8510 {
8511 /* Where should the handle be now? */
8512 int new_start = event->xmotion.y - XINT (bar->dragging);
8513
8514 if (new_start != XINT (bar->start))
8515 {
8516 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8517
8518 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8519 }
8520 }
8521 }
8522
8523 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8524
8525 /* Return information to the user about the current position of the mouse
8526 on the scroll bar. */
8527
8528 static void
8529 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8530 FRAME_PTR *fp;
8531 Lisp_Object *bar_window;
8532 enum scroll_bar_part *part;
8533 Lisp_Object *x, *y;
8534 unsigned long *time;
8535 {
8536 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8537 Window w = SCROLL_BAR_X_WINDOW (bar);
8538 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8539 int win_x, win_y;
8540 Window dummy_window;
8541 int dummy_coord;
8542 unsigned int dummy_mask;
8543
8544 BLOCK_INPUT;
8545
8546 /* Get the mouse's position relative to the scroll bar window, and
8547 report that. */
8548 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8549
8550 /* Root, child, root x and root y. */
8551 &dummy_window, &dummy_window,
8552 &dummy_coord, &dummy_coord,
8553
8554 /* Position relative to scroll bar. */
8555 &win_x, &win_y,
8556
8557 /* Mouse buttons and modifier keys. */
8558 &dummy_mask))
8559 ;
8560 else
8561 {
8562 #if 0
8563 int inside_height
8564 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8565 #endif
8566 int top_range
8567 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8568
8569 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8570
8571 if (! NILP (bar->dragging))
8572 win_y -= XINT (bar->dragging);
8573
8574 if (win_y < 0)
8575 win_y = 0;
8576 if (win_y > top_range)
8577 win_y = top_range;
8578
8579 *fp = f;
8580 *bar_window = bar->window;
8581
8582 if (! NILP (bar->dragging))
8583 *part = scroll_bar_handle;
8584 else if (win_y < XINT (bar->start))
8585 *part = scroll_bar_above_handle;
8586 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8587 *part = scroll_bar_handle;
8588 else
8589 *part = scroll_bar_below_handle;
8590
8591 XSETINT (*x, win_y);
8592 XSETINT (*y, top_range);
8593
8594 f->mouse_moved = 0;
8595 last_mouse_scroll_bar = Qnil;
8596 }
8597
8598 *time = last_mouse_movement_time;
8599
8600 UNBLOCK_INPUT;
8601 }
8602
8603
8604 /* The screen has been cleared so we may have changed foreground or
8605 background colors, and the scroll bars may need to be redrawn.
8606 Clear out the scroll bars, and ask for expose events, so we can
8607 redraw them. */
8608
8609 void
8610 x_scroll_bar_clear (f)
8611 FRAME_PTR f;
8612 {
8613 #ifndef USE_TOOLKIT_SCROLL_BARS
8614 Lisp_Object bar;
8615
8616 /* We can have scroll bars even if this is 0,
8617 if we just turned off scroll bar mode.
8618 But in that case we should not clear them. */
8619 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8620 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8621 bar = XSCROLL_BAR (bar)->next)
8622 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8623 0, 0, 0, 0, True);
8624 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8625 }
8626
8627 /* This processes Expose events from the menu-bar specific X event
8628 loop in xmenu.c. This allows to redisplay the frame if necessary
8629 when handling menu-bar or pop-up items. */
8630
8631 int
8632 process_expose_from_menu (event)
8633 XEvent event;
8634 {
8635 FRAME_PTR f;
8636 struct x_display_info *dpyinfo;
8637 int frame_exposed_p = 0;
8638
8639 BLOCK_INPUT;
8640
8641 dpyinfo = x_display_info_for_display (event.xexpose.display);
8642 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8643 if (f)
8644 {
8645 if (f->async_visible == 0)
8646 {
8647 f->async_visible = 1;
8648 f->async_iconified = 0;
8649 f->output_data.x->has_been_visible = 1;
8650 SET_FRAME_GARBAGED (f);
8651 }
8652 else
8653 {
8654 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8655 event.xexpose.x, event.xexpose.y,
8656 event.xexpose.width, event.xexpose.height);
8657 frame_exposed_p = 1;
8658 }
8659 }
8660 else
8661 {
8662 struct scroll_bar *bar
8663 = x_window_to_scroll_bar (event.xexpose.window);
8664
8665 if (bar)
8666 x_scroll_bar_expose (bar, &event);
8667 }
8668
8669 UNBLOCK_INPUT;
8670 return frame_exposed_p;
8671 }
8672 \f
8673 /* Define a queue to save up SelectionRequest events for later handling. */
8674
8675 struct selection_event_queue
8676 {
8677 XEvent event;
8678 struct selection_event_queue *next;
8679 };
8680
8681 static struct selection_event_queue *queue;
8682
8683 /* Nonzero means queue up certain events--don't process them yet. */
8684
8685 static int x_queue_selection_requests;
8686
8687 /* Queue up an X event *EVENT, to be processed later. */
8688
8689 static void
8690 x_queue_event (f, event)
8691 FRAME_PTR f;
8692 XEvent *event;
8693 {
8694 struct selection_event_queue *queue_tmp
8695 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8696
8697 if (queue_tmp != NULL)
8698 {
8699 queue_tmp->event = *event;
8700 queue_tmp->next = queue;
8701 queue = queue_tmp;
8702 }
8703 }
8704
8705 /* Take all the queued events and put them back
8706 so that they get processed afresh. */
8707
8708 static void
8709 x_unqueue_events (display)
8710 Display *display;
8711 {
8712 while (queue != NULL)
8713 {
8714 struct selection_event_queue *queue_tmp = queue;
8715 XPutBackEvent (display, &queue_tmp->event);
8716 queue = queue_tmp->next;
8717 xfree ((char *)queue_tmp);
8718 }
8719 }
8720
8721 /* Start queuing SelectionRequest events. */
8722
8723 void
8724 x_start_queuing_selection_requests (display)
8725 Display *display;
8726 {
8727 x_queue_selection_requests++;
8728 }
8729
8730 /* Stop queuing SelectionRequest events. */
8731
8732 void
8733 x_stop_queuing_selection_requests (display)
8734 Display *display;
8735 {
8736 x_queue_selection_requests--;
8737 x_unqueue_events (display);
8738 }
8739 \f
8740 /* The main X event-reading loop - XTread_socket. */
8741
8742 /* Time stamp of enter window event. This is only used by XTread_socket,
8743 but we have to put it out here, since static variables within functions
8744 sometimes don't work. */
8745
8746 static Time enter_timestamp;
8747
8748 /* This holds the state XLookupString needs to implement dead keys
8749 and other tricks known as "compose processing". _X Window System_
8750 says that a portable program can't use this, but Stephen Gildea assures
8751 me that letting the compiler initialize it to zeros will work okay.
8752
8753 This must be defined outside of XTread_socket, for the same reasons
8754 given for enter_time stamp, above. */
8755
8756 static XComposeStatus compose_status;
8757
8758 /* Record the last 100 characters stored
8759 to help debug the loss-of-chars-during-GC problem. */
8760
8761 static int temp_index;
8762 static short temp_buffer[100];
8763
8764 /* Set this to nonzero to fake an "X I/O error"
8765 on a particular display. */
8766
8767 struct x_display_info *XTread_socket_fake_io_error;
8768
8769 /* When we find no input here, we occasionally do a no-op command
8770 to verify that the X server is still running and we can still talk with it.
8771 We try all the open displays, one by one.
8772 This variable is used for cycling thru the displays. */
8773
8774 static struct x_display_info *next_noop_dpyinfo;
8775
8776 #define SET_SAVED_MENU_EVENT(size) \
8777 do \
8778 { \
8779 if (f->output_data.x->saved_menu_event == 0) \
8780 f->output_data.x->saved_menu_event \
8781 = (XEvent *) xmalloc (sizeof (XEvent)); \
8782 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8783 if (numchars >= 1) \
8784 { \
8785 bufp->kind = menu_bar_activate_event; \
8786 XSETFRAME (bufp->frame_or_window, f); \
8787 bufp++; \
8788 count++; \
8789 numchars--; \
8790 } \
8791 } \
8792 while (0)
8793
8794 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
8795 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8796
8797 /* Read events coming from the X server.
8798 This routine is called by the SIGIO handler.
8799 We return as soon as there are no more events to be read.
8800
8801 Events representing keys are stored in buffer BUFP,
8802 which can hold up to NUMCHARS characters.
8803 We return the number of characters stored into the buffer,
8804 thus pretending to be `read'.
8805
8806 EXPECTED is nonzero if the caller knows input is available. */
8807
8808 int
8809 XTread_socket (sd, bufp, numchars, expected)
8810 register int sd;
8811 /* register */ struct input_event *bufp;
8812 /* register */ int numchars;
8813 int expected;
8814 {
8815 int count = 0;
8816 int nbytes = 0;
8817 XEvent event;
8818 struct frame *f;
8819 int event_found = 0;
8820 struct x_display_info *dpyinfo;
8821
8822 if (interrupt_input_blocked)
8823 {
8824 interrupt_input_pending = 1;
8825 return -1;
8826 }
8827
8828 interrupt_input_pending = 0;
8829 BLOCK_INPUT;
8830
8831 /* So people can tell when we have read the available input. */
8832 input_signal_count++;
8833
8834 if (numchars <= 0)
8835 abort (); /* Don't think this happens. */
8836
8837 /* Find the display we are supposed to read input for.
8838 It's the one communicating on descriptor SD. */
8839 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8840 {
8841 #if 0 /* This ought to be unnecessary; let's verify it. */
8842 #ifdef FIOSNBIO
8843 /* If available, Xlib uses FIOSNBIO to make the socket
8844 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
8845 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
8846 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
8847 fcntl (dpyinfo->connection, F_SETFL, 0);
8848 #endif /* ! defined (FIOSNBIO) */
8849 #endif
8850
8851 #if 0 /* This code can't be made to work, with multiple displays,
8852 and appears not to be used on any system any more.
8853 Also keyboard.c doesn't turn O_NDELAY on and off
8854 for X connections. */
8855 #ifndef SIGIO
8856 #ifndef HAVE_SELECT
8857 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8858 {
8859 extern int read_alarm_should_throw;
8860 read_alarm_should_throw = 1;
8861 XPeekEvent (dpyinfo->display, &event);
8862 read_alarm_should_throw = 0;
8863 }
8864 #endif /* HAVE_SELECT */
8865 #endif /* SIGIO */
8866 #endif
8867
8868 /* For debugging, this gives a way to fake an I/O error. */
8869 if (dpyinfo == XTread_socket_fake_io_error)
8870 {
8871 XTread_socket_fake_io_error = 0;
8872 x_io_error_quitter (dpyinfo->display);
8873 }
8874
8875 while (XPending (dpyinfo->display))
8876 {
8877 XNextEvent (dpyinfo->display, &event);
8878
8879 if (display_busy_cursor_p)
8880 {
8881 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8882 display until the next X event is read and we come
8883 here again. Setting it to 1 inhibits busy-cursor
8884 display for direct commands. */
8885 if (event.type == MotionNotify
8886 || event.type == EnterNotify
8887 || (dpyinfo->grabbed
8888 && event.type != ButtonRelease))
8889 inhibit_busy_cursor = 2;
8890 else
8891 inhibit_busy_cursor = 1;
8892 }
8893
8894 #ifdef HAVE_X_I18N
8895 {
8896 /* Filter events for the current X input method.
8897 XFilterEvent returns non-zero if the input method has
8898 consumed the event. We pass the frame's X window to
8899 XFilterEvent because that's the one for which the IC
8900 was created. */
8901 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8902 event.xclient.window);
8903 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
8904 break;
8905 }
8906 #endif
8907 event_found = 1;
8908
8909 switch (event.type)
8910 {
8911 case ClientMessage:
8912 {
8913 if (event.xclient.message_type
8914 == dpyinfo->Xatom_wm_protocols
8915 && event.xclient.format == 32)
8916 {
8917 if (event.xclient.data.l[0]
8918 == dpyinfo->Xatom_wm_take_focus)
8919 {
8920 /* Use x_any_window_to_frame because this
8921 could be the shell widget window
8922 if the frame has no title bar. */
8923 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
8924 #ifdef HAVE_X_I18N
8925 /* Not quite sure this is needed -pd */
8926 if (f && FRAME_XIC (f))
8927 XSetICFocus (FRAME_XIC (f));
8928 #endif
8929 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8930 instructs the WM to set the input focus automatically for
8931 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8932 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8933 it has set the focus. So, XSetInputFocus below is not
8934 needed.
8935
8936 The call to XSetInputFocus below has also caused trouble. In
8937 cases where the XSetInputFocus done by the WM and the one
8938 below are temporally close (on a fast machine), the call
8939 below can generate additional FocusIn events which confuse
8940 Emacs. */
8941
8942 /* Since we set WM_TAKE_FOCUS, we must call
8943 XSetInputFocus explicitly. But not if f is null,
8944 since that might be an event for a deleted frame. */
8945 if (f)
8946 {
8947 Display *d = event.xclient.display;
8948 /* Catch and ignore errors, in case window has been
8949 iconified by a window manager such as GWM. */
8950 int count = x_catch_errors (d);
8951 XSetInputFocus (d, event.xclient.window,
8952 /* The ICCCM says this is
8953 the only valid choice. */
8954 RevertToParent,
8955 event.xclient.data.l[1]);
8956 /* This is needed to detect the error
8957 if there is an error. */
8958 XSync (d, False);
8959 x_uncatch_errors (d, count);
8960 }
8961 /* Not certain about handling scroll bars here */
8962 #endif /* 0 */
8963 }
8964 else if (event.xclient.data.l[0]
8965 == dpyinfo->Xatom_wm_save_yourself)
8966 {
8967 /* Save state modify the WM_COMMAND property to
8968 something which can reinstate us. This notifies
8969 the session manager, who's looking for such a
8970 PropertyNotify. Can restart processing when
8971 a keyboard or mouse event arrives. */
8972 if (numchars > 0)
8973 {
8974 f = x_top_window_to_frame (dpyinfo,
8975 event.xclient.window);
8976
8977 /* This is just so we only give real data once
8978 for a single Emacs process. */
8979 if (f == SELECTED_FRAME ())
8980 XSetCommand (FRAME_X_DISPLAY (f),
8981 event.xclient.window,
8982 initial_argv, initial_argc);
8983 else if (f)
8984 XSetCommand (FRAME_X_DISPLAY (f),
8985 event.xclient.window,
8986 0, 0);
8987 }
8988 }
8989 else if (event.xclient.data.l[0]
8990 == dpyinfo->Xatom_wm_delete_window)
8991 {
8992 struct frame *f
8993 = x_any_window_to_frame (dpyinfo,
8994 event.xclient.window);
8995
8996 if (f)
8997 {
8998 if (numchars == 0)
8999 abort ();
9000
9001 bufp->kind = delete_window_event;
9002 XSETFRAME (bufp->frame_or_window, f);
9003 bufp++;
9004
9005 count += 1;
9006 numchars -= 1;
9007 }
9008 }
9009 }
9010 else if (event.xclient.message_type
9011 == dpyinfo->Xatom_wm_configure_denied)
9012 {
9013 }
9014 else if (event.xclient.message_type
9015 == dpyinfo->Xatom_wm_window_moved)
9016 {
9017 int new_x, new_y;
9018 struct frame *f
9019 = x_window_to_frame (dpyinfo, event.xclient.window);
9020
9021 new_x = event.xclient.data.s[0];
9022 new_y = event.xclient.data.s[1];
9023
9024 if (f)
9025 {
9026 f->output_data.x->left_pos = new_x;
9027 f->output_data.x->top_pos = new_y;
9028 }
9029 }
9030 #ifdef HACK_EDITRES
9031 else if (event.xclient.message_type
9032 == dpyinfo->Xatom_editres)
9033 {
9034 struct frame *f
9035 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9036 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9037 &event, NULL);
9038 }
9039 #endif /* HACK_EDITRES */
9040 else if ((event.xclient.message_type
9041 == dpyinfo->Xatom_DONE)
9042 || (event.xclient.message_type
9043 == dpyinfo->Xatom_PAGE))
9044 {
9045 /* Ghostview job completed. Kill it. We could
9046 reply with "Next" if we received "Page", but we
9047 currently never do because we are interested in
9048 images, only, which should have 1 page. */
9049 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9050 struct frame *f
9051 = x_window_to_frame (dpyinfo, event.xclient.window);
9052 x_kill_gs_process (pixmap, f);
9053 expose_frame (f, 0, 0, 0, 0);
9054 }
9055 #ifdef USE_TOOLKIT_SCROLL_BARS
9056 /* Scroll bar callbacks send a ClientMessage from which
9057 we construct an input_event. */
9058 else if (event.xclient.message_type
9059 == dpyinfo->Xatom_Scrollbar)
9060 {
9061 if (display_busy_cursor_p)
9062 inhibit_busy_cursor = 2;
9063 x_scroll_bar_to_input_event (&event, bufp);
9064 ++bufp, ++count, --numchars;
9065 goto out;
9066 }
9067 #endif /* USE_TOOLKIT_SCROLL_BARS */
9068 else
9069 goto OTHER;
9070 }
9071 break;
9072
9073 case SelectionNotify:
9074 #ifdef USE_X_TOOLKIT
9075 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9076 goto OTHER;
9077 #endif /* not USE_X_TOOLKIT */
9078 x_handle_selection_notify (&event.xselection);
9079 break;
9080
9081 case SelectionClear: /* Someone has grabbed ownership. */
9082 #ifdef USE_X_TOOLKIT
9083 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9084 goto OTHER;
9085 #endif /* USE_X_TOOLKIT */
9086 {
9087 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9088
9089 if (numchars == 0)
9090 abort ();
9091
9092 bufp->kind = selection_clear_event;
9093 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9094 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9095 SELECTION_EVENT_TIME (bufp) = eventp->time;
9096 bufp->frame_or_window = Qnil;
9097 bufp++;
9098
9099 count += 1;
9100 numchars -= 1;
9101 }
9102 break;
9103
9104 case SelectionRequest: /* Someone wants our selection. */
9105 #ifdef USE_X_TOOLKIT
9106 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9107 goto OTHER;
9108 #endif /* USE_X_TOOLKIT */
9109 if (x_queue_selection_requests)
9110 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9111 &event);
9112 else
9113 {
9114 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9115
9116 if (numchars == 0)
9117 abort ();
9118
9119 bufp->kind = selection_request_event;
9120 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9121 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9122 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9123 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9124 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9125 SELECTION_EVENT_TIME (bufp) = eventp->time;
9126 bufp->frame_or_window = Qnil;
9127 bufp++;
9128
9129 count += 1;
9130 numchars -= 1;
9131 }
9132 break;
9133
9134 case PropertyNotify:
9135 #ifdef USE_X_TOOLKIT
9136 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9137 goto OTHER;
9138 #endif /* not USE_X_TOOLKIT */
9139 x_handle_property_notify (&event.xproperty);
9140 break;
9141
9142 case ReparentNotify:
9143 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9144 if (f)
9145 {
9146 int x, y;
9147 f->output_data.x->parent_desc = event.xreparent.parent;
9148 x_real_positions (f, &x, &y);
9149 f->output_data.x->left_pos = x;
9150 f->output_data.x->top_pos = y;
9151 }
9152 break;
9153
9154 case Expose:
9155 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9156 if (f)
9157 {
9158 if (f->async_visible == 0)
9159 {
9160 f->async_visible = 1;
9161 f->async_iconified = 0;
9162 f->output_data.x->has_been_visible = 1;
9163 SET_FRAME_GARBAGED (f);
9164 }
9165 else
9166 expose_frame (x_window_to_frame (dpyinfo,
9167 event.xexpose.window),
9168 event.xexpose.x, event.xexpose.y,
9169 event.xexpose.width, event.xexpose.height);
9170 }
9171 else
9172 {
9173 #ifdef USE_TOOLKIT_SCROLL_BARS
9174 /* Dispatch event to the widget. */
9175 goto OTHER;
9176 #else /* not USE_TOOLKIT_SCROLL_BARS */
9177 struct scroll_bar *bar
9178 = x_window_to_scroll_bar (event.xexpose.window);
9179
9180 if (bar)
9181 x_scroll_bar_expose (bar, &event);
9182 #ifdef USE_X_TOOLKIT
9183 else
9184 goto OTHER;
9185 #endif /* USE_X_TOOLKIT */
9186 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9187 }
9188 break;
9189
9190 case GraphicsExpose: /* This occurs when an XCopyArea's
9191 source area was obscured or not
9192 available.*/
9193 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9194 if (f)
9195 {
9196 expose_frame (f,
9197 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9198 event.xgraphicsexpose.width,
9199 event.xgraphicsexpose.height);
9200 }
9201 #ifdef USE_X_TOOLKIT
9202 else
9203 goto OTHER;
9204 #endif /* USE_X_TOOLKIT */
9205 break;
9206
9207 case NoExpose: /* This occurs when an XCopyArea's
9208 source area was completely
9209 available */
9210 break;
9211
9212 case UnmapNotify:
9213 /* Redo the mouse-highlight after the tooltip has gone. */
9214 if (event.xmap.window == tip_window)
9215 {
9216 tip_window = 0;
9217 redo_mouse_highlight ();
9218 }
9219
9220 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9221 if (f) /* F may no longer exist if
9222 the frame was deleted. */
9223 {
9224 /* While a frame is unmapped, display generation is
9225 disabled; you don't want to spend time updating a
9226 display that won't ever be seen. */
9227 f->async_visible = 0;
9228 /* We can't distinguish, from the event, whether the window
9229 has become iconified or invisible. So assume, if it
9230 was previously visible, than now it is iconified.
9231 But x_make_frame_invisible clears both
9232 the visible flag and the iconified flag;
9233 and that way, we know the window is not iconified now. */
9234 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9235 {
9236 f->async_iconified = 1;
9237
9238 bufp->kind = iconify_event;
9239 XSETFRAME (bufp->frame_or_window, f);
9240 bufp++;
9241 count++;
9242 numchars--;
9243 }
9244 }
9245 goto OTHER;
9246
9247 case MapNotify:
9248 if (event.xmap.window == tip_window)
9249 /* The tooltip has been drawn already. Avoid
9250 the SET_FRAME_GARBAGED below. */
9251 goto OTHER;
9252
9253 /* We use x_top_window_to_frame because map events can
9254 come for sub-windows and they don't mean that the
9255 frame is visible. */
9256 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9257 if (f)
9258 {
9259 f->async_visible = 1;
9260 f->async_iconified = 0;
9261 f->output_data.x->has_been_visible = 1;
9262
9263 /* wait_reading_process_input will notice this and update
9264 the frame's display structures. */
9265 SET_FRAME_GARBAGED (f);
9266
9267 if (f->iconified)
9268 {
9269 bufp->kind = deiconify_event;
9270 XSETFRAME (bufp->frame_or_window, f);
9271 bufp++;
9272 count++;
9273 numchars--;
9274 }
9275 else if (! NILP (Vframe_list)
9276 && ! NILP (XCDR (Vframe_list)))
9277 /* Force a redisplay sooner or later
9278 to update the frame titles
9279 in case this is the second frame. */
9280 record_asynch_buffer_change ();
9281 }
9282 goto OTHER;
9283
9284 case KeyPress:
9285 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9286
9287 #ifdef USE_MOTIF
9288 /* I couldn't find a way to prevent LessTif scroll bars
9289 from consuming key events. */
9290 if (f == 0)
9291 {
9292 Widget widget = XtWindowToWidget (dpyinfo->display,
9293 event.xkey.window);
9294 if (widget && XmIsScrollBar (widget))
9295 {
9296 widget = XtParent (widget);
9297 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9298 }
9299 }
9300 #endif /* USE_MOTIF */
9301
9302 if (f != 0)
9303 {
9304 KeySym keysym, orig_keysym;
9305 /* al%imercury@uunet.uu.net says that making this 81 instead of
9306 80 fixed a bug whereby meta chars made his Emacs hang. */
9307 unsigned char copy_buffer[81];
9308 int modifiers;
9309
9310 event.xkey.state
9311 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9312 extra_keyboard_modifiers);
9313 modifiers = event.xkey.state;
9314
9315 /* This will have to go some day... */
9316
9317 /* make_lispy_event turns chars into control chars.
9318 Don't do it here because XLookupString is too eager. */
9319 event.xkey.state &= ~ControlMask;
9320 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9321 | dpyinfo->super_mod_mask
9322 | dpyinfo->hyper_mod_mask
9323 | dpyinfo->alt_mod_mask);
9324
9325 /* In case Meta is ComposeCharacter,
9326 clear its status. According to Markus Ehrnsperger
9327 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9328 this enables ComposeCharacter to work whether or
9329 not it is combined with Meta. */
9330 if (modifiers & dpyinfo->meta_mod_mask)
9331 bzero (&compose_status, sizeof (compose_status));
9332
9333 #ifdef HAVE_X_I18N
9334 if (FRAME_XIC (f))
9335 {
9336 unsigned char *copy_bufptr = copy_buffer;
9337 int copy_bufsiz = sizeof (copy_buffer);
9338 Status status_return;
9339
9340 nbytes = XmbLookupString (FRAME_XIC (f),
9341 &event.xkey, copy_bufptr,
9342 copy_bufsiz, &keysym,
9343 &status_return);
9344 if (status_return == XBufferOverflow)
9345 {
9346 copy_bufsiz = nbytes + 1;
9347 copy_bufptr = (char *) alloca (copy_bufsiz);
9348 nbytes = XmbLookupString (FRAME_XIC (f),
9349 &event.xkey, copy_bufptr,
9350 copy_bufsiz, &keysym,
9351 &status_return);
9352 }
9353
9354 if (status_return == XLookupNone)
9355 break;
9356 else if (status_return == XLookupChars)
9357 keysym = NoSymbol;
9358 else if (status_return != XLookupKeySym
9359 && status_return != XLookupBoth)
9360 abort ();
9361 }
9362 else
9363 nbytes = XLookupString (&event.xkey, copy_buffer,
9364 80, &keysym, &compose_status);
9365 #else
9366 nbytes = XLookupString (&event.xkey, copy_buffer,
9367 80, &keysym, &compose_status);
9368 #endif
9369
9370 orig_keysym = keysym;
9371
9372 if (numchars > 1)
9373 {
9374 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9375 || keysym == XK_Delete
9376 #ifdef XK_ISO_Left_Tab
9377 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9378 #endif
9379 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9380 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9381 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9382 #ifdef HPUX
9383 /* This recognizes the "extended function keys".
9384 It seems there's no cleaner way.
9385 Test IsModifierKey to avoid handling mode_switch
9386 incorrectly. */
9387 || ((unsigned) (keysym) >= XK_Select
9388 && (unsigned)(keysym) < XK_KP_Space)
9389 #endif
9390 #ifdef XK_dead_circumflex
9391 || orig_keysym == XK_dead_circumflex
9392 #endif
9393 #ifdef XK_dead_grave
9394 || orig_keysym == XK_dead_grave
9395 #endif
9396 #ifdef XK_dead_tilde
9397 || orig_keysym == XK_dead_tilde
9398 #endif
9399 #ifdef XK_dead_diaeresis
9400 || orig_keysym == XK_dead_diaeresis
9401 #endif
9402 #ifdef XK_dead_macron
9403 || orig_keysym == XK_dead_macron
9404 #endif
9405 #ifdef XK_dead_degree
9406 || orig_keysym == XK_dead_degree
9407 #endif
9408 #ifdef XK_dead_acute
9409 || orig_keysym == XK_dead_acute
9410 #endif
9411 #ifdef XK_dead_cedilla
9412 || orig_keysym == XK_dead_cedilla
9413 #endif
9414 #ifdef XK_dead_breve
9415 || orig_keysym == XK_dead_breve
9416 #endif
9417 #ifdef XK_dead_ogonek
9418 || orig_keysym == XK_dead_ogonek
9419 #endif
9420 #ifdef XK_dead_caron
9421 || orig_keysym == XK_dead_caron
9422 #endif
9423 #ifdef XK_dead_doubleacute
9424 || orig_keysym == XK_dead_doubleacute
9425 #endif
9426 #ifdef XK_dead_abovedot
9427 || orig_keysym == XK_dead_abovedot
9428 #endif
9429 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9430 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9431 /* Any "vendor-specific" key is ok. */
9432 || (orig_keysym & (1 << 28)))
9433 && ! (IsModifierKey (orig_keysym)
9434 #ifndef HAVE_X11R5
9435 #ifdef XK_Mode_switch
9436 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9437 #endif
9438 #ifdef XK_Num_Lock
9439 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9440 #endif
9441 #endif /* not HAVE_X11R5 */
9442 ))
9443 {
9444 if (temp_index == sizeof temp_buffer / sizeof (short))
9445 temp_index = 0;
9446 temp_buffer[temp_index++] = keysym;
9447 bufp->kind = non_ascii_keystroke;
9448 bufp->code = keysym;
9449 XSETFRAME (bufp->frame_or_window, f);
9450 bufp->modifiers
9451 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9452 modifiers);
9453 bufp->timestamp = event.xkey.time;
9454 bufp++;
9455 count++;
9456 numchars--;
9457
9458 if (display_busy_cursor_p)
9459 if (keysym != XK_Return || minibuf_level == 0)
9460 inhibit_busy_cursor = 2;
9461 }
9462 else if (numchars > nbytes)
9463 {
9464 register int i;
9465
9466 for (i = 0; i < nbytes; i++)
9467 {
9468 if (temp_index == sizeof temp_buffer / sizeof (short))
9469 temp_index = 0;
9470 temp_buffer[temp_index++] = copy_buffer[i];
9471 bufp->kind = ascii_keystroke;
9472 bufp->code = copy_buffer[i];
9473 XSETFRAME (bufp->frame_or_window, f);
9474 bufp->modifiers
9475 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9476 modifiers);
9477 bufp->timestamp = event.xkey.time;
9478 bufp++;
9479 }
9480
9481 count += nbytes;
9482 numchars -= nbytes;
9483
9484 if (keysym == NoSymbol)
9485 break;
9486 }
9487 else
9488 abort ();
9489 }
9490 else
9491 abort ();
9492 }
9493 #ifdef HAVE_X_I18N
9494 /* Don't dispatch this event since XtDispatchEvent calls
9495 XFilterEvent, and two calls in a row may freeze the
9496 client. */
9497 break;
9498 #else
9499 goto OTHER;
9500 #endif
9501
9502 case KeyRelease:
9503 #ifdef HAVE_X_I18N
9504 /* Don't dispatch this event since XtDispatchEvent calls
9505 XFilterEvent, and two calls in a row may freeze the
9506 client. */
9507 break;
9508 #else
9509 goto OTHER;
9510 #endif
9511
9512 /* Here's a possible interpretation of the whole
9513 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9514 you get a FocusIn event, you have to get a FocusOut
9515 event before you relinquish the focus. If you
9516 haven't received a FocusIn event, then a mere
9517 LeaveNotify is enough to free you. */
9518
9519 case EnterNotify:
9520 {
9521 int from_menu_bar_p = 0;
9522
9523 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9524
9525 #ifdef LESSTIF_VERSION
9526 /* When clicking outside of a menu bar popup to close
9527 it, we get a FocusIn/ EnterNotify sequence of
9528 events. The flag event.xcrossing.focus is not set
9529 in the EnterNotify event of that sequence because
9530 the focus is in the menu bar,
9531 event.xcrossing.window is the frame's X window.
9532 Unconditionally setting the focus frame to null in
9533 this case is not the right thing, because no event
9534 follows that could set the focus frame to the right
9535 value.
9536
9537 This could be a LessTif bug, but I wasn't able to
9538 reproduce the behavior in a simple test program.
9539
9540 (gerd, LessTif 0.88.1). */
9541
9542 if (!event.xcrossing.focus
9543 && f
9544 && f->output_data.x->menubar_widget)
9545 {
9546 Window focus;
9547 int revert;
9548
9549 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9550 if (focus == XtWindow (f->output_data.x->menubar_widget))
9551 from_menu_bar_p = 1;
9552 }
9553 #endif /* LESSTIF_VERSION */
9554
9555 if (event.xcrossing.focus || from_menu_bar_p)
9556 {
9557 /* Avoid nasty pop/raise loops. */
9558 if (f && (!(f->auto_raise)
9559 || !(f->auto_lower)
9560 || (event.xcrossing.time - enter_timestamp) > 500))
9561 {
9562 x_new_focus_frame (dpyinfo, f);
9563 enter_timestamp = event.xcrossing.time;
9564 }
9565 }
9566 else if (f == dpyinfo->x_focus_frame)
9567 x_new_focus_frame (dpyinfo, 0);
9568
9569 /* EnterNotify counts as mouse movement,
9570 so update things that depend on mouse position. */
9571 if (f && !f->output_data.x->busy_p)
9572 note_mouse_movement (f, &event.xmotion);
9573 goto OTHER;
9574 }
9575
9576 case FocusIn:
9577 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9578 if (event.xfocus.detail != NotifyPointer)
9579 dpyinfo->x_focus_event_frame = f;
9580 if (f)
9581 x_new_focus_frame (dpyinfo, f);
9582
9583 #ifdef HAVE_X_I18N
9584 if (f && FRAME_XIC (f))
9585 XSetICFocus (FRAME_XIC (f));
9586 #endif
9587
9588 goto OTHER;
9589
9590 case LeaveNotify:
9591 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9592 if (f)
9593 {
9594 Lisp_Object frame;
9595 int from_menu_bar_p = 0;
9596
9597 if (f == dpyinfo->mouse_face_mouse_frame)
9598 {
9599 /* If we move outside the frame, then we're
9600 certainly no longer on any text in the frame. */
9601 clear_mouse_face (dpyinfo);
9602 dpyinfo->mouse_face_mouse_frame = 0;
9603 }
9604
9605 /* Generate a nil HELP_EVENT to cancel a help-echo.
9606 Do it only if there's something to cancel.
9607 Otherwise, the startup message is cleared when
9608 the mouse leaves the frame. */
9609 if (any_help_event_p)
9610 {
9611 XSETFRAME (frame, f);
9612 bufp->kind = HELP_EVENT;
9613 bufp->frame_or_window = Fcons (frame, Qnil);
9614 ++bufp, ++count, --numchars;
9615 }
9616
9617 #ifdef LESSTIF_VERSION
9618 /* Please see the comment at the start of the
9619 EnterNotify case. */
9620 if (!event.xcrossing.focus
9621 && f->output_data.x->menubar_widget)
9622 {
9623 Window focus;
9624 int revert;
9625 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9626 if (focus == XtWindow (f->output_data.x->menubar_widget))
9627 from_menu_bar_p = 1;
9628 }
9629 #endif /* LESSTIF_VERSION */
9630
9631 if (event.xcrossing.focus || from_menu_bar_p)
9632 x_mouse_leave (dpyinfo);
9633 else
9634 {
9635 if (f == dpyinfo->x_focus_event_frame)
9636 dpyinfo->x_focus_event_frame = 0;
9637 if (f == dpyinfo->x_focus_frame)
9638 x_new_focus_frame (dpyinfo, 0);
9639 }
9640 }
9641 goto OTHER;
9642
9643 case FocusOut:
9644 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9645 if (event.xfocus.detail != NotifyPointer
9646 && f == dpyinfo->x_focus_event_frame)
9647 dpyinfo->x_focus_event_frame = 0;
9648 if (f && f == dpyinfo->x_focus_frame)
9649 x_new_focus_frame (dpyinfo, 0);
9650
9651 #ifdef HAVE_X_I18N
9652 if (f && FRAME_XIC (f))
9653 XUnsetICFocus (FRAME_XIC (f));
9654 #endif
9655
9656 goto OTHER;
9657
9658 case MotionNotify:
9659 {
9660 previous_help_echo = help_echo;
9661 help_echo = Qnil;
9662
9663 if (dpyinfo->grabbed && last_mouse_frame
9664 && FRAME_LIVE_P (last_mouse_frame))
9665 f = last_mouse_frame;
9666 else
9667 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9668
9669 if (f)
9670 note_mouse_movement (f, &event.xmotion);
9671 else
9672 {
9673 #ifndef USE_TOOLKIT_SCROLL_BARS
9674 struct scroll_bar *bar
9675 = x_window_to_scroll_bar (event.xmotion.window);
9676
9677 if (bar)
9678 x_scroll_bar_note_movement (bar, &event);
9679 #endif /* USE_TOOLKIT_SCROLL_BARS */
9680
9681 /* If we move outside the frame, then we're
9682 certainly no longer on any text in the frame. */
9683 clear_mouse_face (dpyinfo);
9684 }
9685
9686 /* If the contents of the global variable help_echo
9687 has changed, generate a HELP_EVENT. */
9688 if (STRINGP (help_echo)
9689 || STRINGP (previous_help_echo))
9690 {
9691 Lisp_Object frame;
9692
9693 if (f)
9694 XSETFRAME (frame, f);
9695 else
9696 frame = Qnil;
9697
9698 any_help_event_p = 1;
9699 bufp->kind = HELP_EVENT;
9700 bufp->frame_or_window = Fcons (frame, help_echo);
9701 ++bufp, ++count, --numchars;
9702 }
9703
9704 goto OTHER;
9705 }
9706
9707 case ConfigureNotify:
9708 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9709 if (f)
9710 {
9711 #ifndef USE_X_TOOLKIT
9712 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9713 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9714
9715 /* In the toolkit version, change_frame_size
9716 is called by the code that handles resizing
9717 of the EmacsFrame widget. */
9718
9719 /* Even if the number of character rows and columns has
9720 not changed, the font size may have changed, so we need
9721 to check the pixel dimensions as well. */
9722 if (columns != f->width
9723 || rows != f->height
9724 || event.xconfigure.width != f->output_data.x->pixel_width
9725 || event.xconfigure.height != f->output_data.x->pixel_height)
9726 {
9727 change_frame_size (f, rows, columns, 0, 1, 0);
9728 SET_FRAME_GARBAGED (f);
9729 cancel_mouse_face (f);
9730 }
9731 #endif
9732
9733 f->output_data.x->pixel_width = event.xconfigure.width;
9734 f->output_data.x->pixel_height = event.xconfigure.height;
9735
9736 /* What we have now is the position of Emacs's own window.
9737 Convert that to the position of the window manager window. */
9738 x_real_positions (f, &f->output_data.x->left_pos,
9739 &f->output_data.x->top_pos);
9740
9741 #ifdef HAVE_X_I18N
9742 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9743 xic_set_statusarea (f);
9744 #endif
9745
9746 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9747 {
9748 /* Since the WM decorations come below top_pos now,
9749 we must put them below top_pos in the future. */
9750 f->output_data.x->win_gravity = NorthWestGravity;
9751 x_wm_set_size_hint (f, (long) 0, 0);
9752 }
9753 #ifdef USE_MOTIF
9754 /* Some window managers pass (0,0) as the location of
9755 the window, and the Motif event handler stores it
9756 in the emacs widget, which messes up Motif menus. */
9757 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9758 {
9759 event.xconfigure.x = f->output_data.x->widget->core.x;
9760 event.xconfigure.y = f->output_data.x->widget->core.y;
9761 }
9762 #endif /* USE_MOTIF */
9763 }
9764 goto OTHER;
9765
9766 case ButtonPress:
9767 case ButtonRelease:
9768 {
9769 /* If we decide we want to generate an event to be seen
9770 by the rest of Emacs, we put it here. */
9771 struct input_event emacs_event;
9772 int tool_bar_p = 0;
9773
9774 emacs_event.kind = no_event;
9775 bzero (&compose_status, sizeof (compose_status));
9776
9777 if (dpyinfo->grabbed
9778 && last_mouse_frame
9779 && FRAME_LIVE_P (last_mouse_frame))
9780 f = last_mouse_frame;
9781 else
9782 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9783
9784 if (f)
9785 {
9786 /* Is this in the tool-bar? */
9787 if (WINDOWP (f->tool_bar_window)
9788 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
9789 {
9790 Lisp_Object window;
9791 int p, x, y;
9792
9793 x = event.xbutton.x;
9794 y = event.xbutton.y;
9795
9796 /* Set x and y. */
9797 window = window_from_coordinates (f, x, y, &p, 1);
9798 if (EQ (window, f->tool_bar_window))
9799 {
9800 x_handle_tool_bar_click (f, &event.xbutton);
9801 tool_bar_p = 1;
9802 }
9803 }
9804
9805 if (!tool_bar_p)
9806 if (!dpyinfo->x_focus_frame
9807 || f == dpyinfo->x_focus_frame)
9808 construct_mouse_click (&emacs_event, &event, f);
9809 }
9810 else
9811 {
9812 #ifndef USE_TOOLKIT_SCROLL_BARS
9813 struct scroll_bar *bar
9814 = x_window_to_scroll_bar (event.xbutton.window);
9815
9816 if (bar)
9817 x_scroll_bar_handle_click (bar, &event, &emacs_event);
9818 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9819 }
9820
9821 if (event.type == ButtonPress)
9822 {
9823 dpyinfo->grabbed |= (1 << event.xbutton.button);
9824 last_mouse_frame = f;
9825 /* Ignore any mouse motion that happened
9826 before this event; any subsequent mouse-movement
9827 Emacs events should reflect only motion after
9828 the ButtonPress. */
9829 if (f != 0)
9830 f->mouse_moved = 0;
9831
9832 if (!tool_bar_p)
9833 last_tool_bar_item = -1;
9834 if (display_busy_cursor_p)
9835 inhibit_busy_cursor = 2;
9836 }
9837 else
9838 {
9839 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
9840 }
9841
9842 if (numchars >= 1 && emacs_event.kind != no_event)
9843 {
9844 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9845 bufp++;
9846 count++;
9847 numchars--;
9848 }
9849
9850 #ifdef USE_X_TOOLKIT
9851 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9852 /* For a down-event in the menu bar,
9853 don't pass it to Xt right now.
9854 Instead, save it away
9855 and we will pass it to Xt from kbd_buffer_get_event.
9856 That way, we can run some Lisp code first. */
9857 if (f && event.type == ButtonPress
9858 /* Verify the event is really within the menu bar
9859 and not just sent to it due to grabbing. */
9860 && event.xbutton.x >= 0
9861 && event.xbutton.x < f->output_data.x->pixel_width
9862 && event.xbutton.y >= 0
9863 && event.xbutton.y < f->output_data.x->menubar_height
9864 && event.xbutton.same_screen)
9865 {
9866 SET_SAVED_BUTTON_EVENT;
9867 XSETFRAME (last_mouse_press_frame, f);
9868 }
9869 else if (event.type == ButtonPress)
9870 {
9871 last_mouse_press_frame = Qnil;
9872 goto OTHER;
9873 }
9874
9875 #ifdef USE_MOTIF /* This should do not harm for Lucid,
9876 but I am trying to be cautious. */
9877 else if (event.type == ButtonRelease)
9878 {
9879 if (!NILP (last_mouse_press_frame))
9880 {
9881 f = XFRAME (last_mouse_press_frame);
9882 if (f->output_data.x)
9883 SET_SAVED_BUTTON_EVENT;
9884 }
9885 else
9886 goto OTHER;
9887 }
9888 #endif /* USE_MOTIF */
9889 else
9890 goto OTHER;
9891 #endif /* USE_X_TOOLKIT */
9892 }
9893 break;
9894
9895 case CirculateNotify:
9896 goto OTHER;
9897
9898 case CirculateRequest:
9899 goto OTHER;
9900
9901 case VisibilityNotify:
9902 goto OTHER;
9903
9904 case MappingNotify:
9905 /* Someone has changed the keyboard mapping - update the
9906 local cache. */
9907 switch (event.xmapping.request)
9908 {
9909 case MappingModifier:
9910 x_find_modifier_meanings (dpyinfo);
9911 /* This is meant to fall through. */
9912 case MappingKeyboard:
9913 XRefreshKeyboardMapping (&event.xmapping);
9914 }
9915 goto OTHER;
9916
9917 default:
9918 OTHER:
9919 #ifdef USE_X_TOOLKIT
9920 BLOCK_INPUT;
9921 XtDispatchEvent (&event);
9922 UNBLOCK_INPUT;
9923 #endif /* USE_X_TOOLKIT */
9924 break;
9925 }
9926 }
9927 }
9928
9929 out:;
9930
9931 /* On some systems, an X bug causes Emacs to get no more events
9932 when the window is destroyed. Detect that. (1994.) */
9933 if (! event_found)
9934 {
9935 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9936 One XNOOP in 100 loops will make Emacs terminate.
9937 B. Bretthauer, 1994 */
9938 x_noop_count++;
9939 if (x_noop_count >= 100)
9940 {
9941 x_noop_count=0;
9942
9943 if (next_noop_dpyinfo == 0)
9944 next_noop_dpyinfo = x_display_list;
9945
9946 XNoOp (next_noop_dpyinfo->display);
9947
9948 /* Each time we get here, cycle through the displays now open. */
9949 next_noop_dpyinfo = next_noop_dpyinfo->next;
9950 }
9951 }
9952
9953 /* If the focus was just given to an auto-raising frame,
9954 raise it now. */
9955 /* ??? This ought to be able to handle more than one such frame. */
9956 if (pending_autoraise_frame)
9957 {
9958 x_raise_frame (pending_autoraise_frame);
9959 pending_autoraise_frame = 0;
9960 }
9961
9962 UNBLOCK_INPUT;
9963 return count;
9964 }
9965
9966
9967
9968 \f
9969 /***********************************************************************
9970 Text Cursor
9971 ***********************************************************************/
9972
9973 /* Note if the text cursor of window W has been overwritten by a
9974 drawing operation that outputs N glyphs starting at HPOS in the
9975 line given by output_cursor.vpos. N < 0 means all the rest of the
9976 line after HPOS has been written. */
9977
9978 static void
9979 note_overwritten_text_cursor (w, hpos, n)
9980 struct window *w;
9981 int hpos, n;
9982 {
9983 if (updated_area == TEXT_AREA
9984 && output_cursor.vpos == w->phys_cursor.vpos
9985 && hpos <= w->phys_cursor.hpos
9986 && (n < 0
9987 || hpos + n > w->phys_cursor.hpos))
9988 w->phys_cursor_on_p = 0;
9989 }
9990
9991
9992 /* Set clipping for output in glyph row ROW. W is the window in which
9993 we operate. GC is the graphics context to set clipping in.
9994 WHOLE_LINE_P non-zero means include the areas used for truncation
9995 mark display and alike in the clipping rectangle.
9996
9997 ROW may be a text row or, e.g., a mode line. Text rows must be
9998 clipped to the interior of the window dedicated to text display,
9999 mode lines must be clipped to the whole window. */
10000
10001 static void
10002 x_clip_to_row (w, row, gc, whole_line_p)
10003 struct window *w;
10004 struct glyph_row *row;
10005 GC gc;
10006 int whole_line_p;
10007 {
10008 struct frame *f = XFRAME (WINDOW_FRAME (w));
10009 XRectangle clip_rect;
10010 int window_x, window_y, window_width, window_height;
10011
10012 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10013
10014 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10015 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10016 clip_rect.y = max (clip_rect.y, window_y);
10017 clip_rect.width = window_width;
10018 clip_rect.height = row->visible_height;
10019
10020 /* If clipping to the whole line, including trunc marks, extend
10021 the rectangle to the left and increase its width. */
10022 if (whole_line_p)
10023 {
10024 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10025 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10026 }
10027
10028 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10029 }
10030
10031
10032 /* Draw a hollow box cursor on window W in glyph row ROW. */
10033
10034 static void
10035 x_draw_hollow_cursor (w, row)
10036 struct window *w;
10037 struct glyph_row *row;
10038 {
10039 struct frame *f = XFRAME (WINDOW_FRAME (w));
10040 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10041 Display *dpy = FRAME_X_DISPLAY (f);
10042 int x, y, wd, h;
10043 XGCValues xgcv;
10044 struct glyph *cursor_glyph;
10045 GC gc;
10046
10047 /* Compute frame-relative coordinates from window-relative
10048 coordinates. */
10049 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10050 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10051 + row->ascent - w->phys_cursor_ascent);
10052 h = row->height - 1;
10053
10054 /* Get the glyph the cursor is on. If we can't tell because
10055 the current matrix is invalid or such, give up. */
10056 cursor_glyph = get_phys_cursor_glyph (w);
10057 if (cursor_glyph == NULL)
10058 return;
10059
10060 /* Compute the width of the rectangle to draw. If on a stretch
10061 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10062 rectangle as wide as the glyph, but use a canonical character
10063 width instead. */
10064 wd = cursor_glyph->pixel_width - 1;
10065 if (cursor_glyph->type == STRETCH_GLYPH
10066 && !x_stretch_cursor_p)
10067 wd = min (CANON_X_UNIT (f), wd);
10068
10069 /* The foreground of cursor_gc is typically the same as the normal
10070 background color, which can cause the cursor box to be invisible. */
10071 xgcv.foreground = f->output_data.x->cursor_pixel;
10072 if (dpyinfo->scratch_cursor_gc)
10073 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10074 else
10075 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10076 GCForeground, &xgcv);
10077 gc = dpyinfo->scratch_cursor_gc;
10078
10079 /* Set clipping, draw the rectangle, and reset clipping again. */
10080 x_clip_to_row (w, row, gc, 0);
10081 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10082 XSetClipMask (dpy, gc, None);
10083 }
10084
10085
10086 /* Draw a bar cursor on window W in glyph row ROW.
10087
10088 Implementation note: One would like to draw a bar cursor with an
10089 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10090 Unfortunately, I didn't find a font yet that has this property set.
10091 --gerd. */
10092
10093 static void
10094 x_draw_bar_cursor (w, row)
10095 struct window *w;
10096 struct glyph_row *row;
10097 {
10098 /* If cursor hpos is out of bounds, don't draw garbage. This can
10099 happen in mini-buffer windows when switching between echo area
10100 glyphs and mini-buffer. */
10101 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10102 {
10103 struct frame *f = XFRAME (w->frame);
10104 struct glyph *cursor_glyph;
10105 GC gc;
10106 int x;
10107 unsigned long mask;
10108 XGCValues xgcv;
10109 Display *dpy;
10110 Window window;
10111
10112 cursor_glyph = get_phys_cursor_glyph (w);
10113 if (cursor_glyph == NULL)
10114 return;
10115
10116 xgcv.background = f->output_data.x->cursor_pixel;
10117 xgcv.foreground = f->output_data.x->cursor_pixel;
10118 xgcv.graphics_exposures = 0;
10119 mask = GCForeground | GCBackground | GCGraphicsExposures;
10120 dpy = FRAME_X_DISPLAY (f);
10121 window = FRAME_X_WINDOW (f);
10122 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10123
10124 if (gc)
10125 XChangeGC (dpy, gc, mask, &xgcv);
10126 else
10127 {
10128 gc = XCreateGC (dpy, window, mask, &xgcv);
10129 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10130 }
10131
10132 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10133 x_clip_to_row (w, row, gc, 0);
10134 XFillRectangle (dpy, window, gc,
10135 x,
10136 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10137 min (cursor_glyph->pixel_width,
10138 f->output_data.x->cursor_width),
10139 row->height);
10140 XSetClipMask (dpy, gc, None);
10141 }
10142 }
10143
10144
10145 /* Clear the cursor of window W to background color, and mark the
10146 cursor as not shown. This is used when the text where the cursor
10147 is is about to be rewritten. */
10148
10149 static void
10150 x_clear_cursor (w)
10151 struct window *w;
10152 {
10153 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10154 x_update_window_cursor (w, 0);
10155 }
10156
10157
10158 /* Draw the cursor glyph of window W in glyph row ROW. See the
10159 comment of x_draw_glyphs for the meaning of HL. */
10160
10161 static void
10162 x_draw_phys_cursor_glyph (w, row, hl)
10163 struct window *w;
10164 struct glyph_row *row;
10165 enum draw_glyphs_face hl;
10166 {
10167 /* If cursor hpos is out of bounds, don't draw garbage. This can
10168 happen in mini-buffer windows when switching between echo area
10169 glyphs and mini-buffer. */
10170 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10171 {
10172 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10173 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10174 hl, 0, 0, 0);
10175
10176 /* When we erase the cursor, and ROW is overlapped by other
10177 rows, make sure that these overlapping parts of other rows
10178 are redrawn. */
10179 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10180 {
10181 if (row > w->current_matrix->rows
10182 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10183 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10184
10185 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10186 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10187 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10188 }
10189 }
10190 }
10191
10192
10193 /* Erase the image of a cursor of window W from the screen. */
10194
10195 static void
10196 x_erase_phys_cursor (w)
10197 struct window *w;
10198 {
10199 struct frame *f = XFRAME (w->frame);
10200 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10201 int hpos = w->phys_cursor.hpos;
10202 int vpos = w->phys_cursor.vpos;
10203 int mouse_face_here_p = 0;
10204 struct glyph_matrix *active_glyphs = w->current_matrix;
10205 struct glyph_row *cursor_row;
10206 struct glyph *cursor_glyph;
10207 enum draw_glyphs_face hl;
10208
10209 /* No cursor displayed or row invalidated => nothing to do on the
10210 screen. */
10211 if (w->phys_cursor_type == NO_CURSOR)
10212 goto mark_cursor_off;
10213
10214 /* VPOS >= active_glyphs->nrows means that window has been resized.
10215 Don't bother to erase the cursor. */
10216 if (vpos >= active_glyphs->nrows)
10217 goto mark_cursor_off;
10218
10219 /* If row containing cursor is marked invalid, there is nothing we
10220 can do. */
10221 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10222 if (!cursor_row->enabled_p)
10223 goto mark_cursor_off;
10224
10225 /* This can happen when the new row is shorter than the old one.
10226 In this case, either x_draw_glyphs or clear_end_of_line
10227 should have cleared the cursor. Note that we wouldn't be
10228 able to erase the cursor in this case because we don't have a
10229 cursor glyph at hand. */
10230 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10231 goto mark_cursor_off;
10232
10233 /* If the cursor is in the mouse face area, redisplay that when
10234 we clear the cursor. */
10235 if (w == XWINDOW (dpyinfo->mouse_face_window)
10236 && (vpos > dpyinfo->mouse_face_beg_row
10237 || (vpos == dpyinfo->mouse_face_beg_row
10238 && hpos >= dpyinfo->mouse_face_beg_col))
10239 && (vpos < dpyinfo->mouse_face_end_row
10240 || (vpos == dpyinfo->mouse_face_end_row
10241 && hpos < dpyinfo->mouse_face_end_col))
10242 /* Don't redraw the cursor's spot in mouse face if it is at the
10243 end of a line (on a newline). The cursor appears there, but
10244 mouse highlighting does not. */
10245 && cursor_row->used[TEXT_AREA] > hpos)
10246 mouse_face_here_p = 1;
10247
10248 /* Maybe clear the display under the cursor. */
10249 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10250 {
10251 int x;
10252 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10253
10254 cursor_glyph = get_phys_cursor_glyph (w);
10255 if (cursor_glyph == NULL)
10256 goto mark_cursor_off;
10257
10258 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10259
10260 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10261 x,
10262 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10263 cursor_row->y)),
10264 cursor_glyph->pixel_width,
10265 cursor_row->visible_height,
10266 False);
10267 }
10268
10269 /* Erase the cursor by redrawing the character underneath it. */
10270 if (mouse_face_here_p)
10271 hl = DRAW_MOUSE_FACE;
10272 else if (cursor_row->inverse_p)
10273 hl = DRAW_INVERSE_VIDEO;
10274 else
10275 hl = DRAW_NORMAL_TEXT;
10276 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10277
10278 mark_cursor_off:
10279 w->phys_cursor_on_p = 0;
10280 w->phys_cursor_type = NO_CURSOR;
10281 }
10282
10283
10284 /* Display or clear cursor of window W. If ON is zero, clear the
10285 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10286 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10287
10288 void
10289 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10290 struct window *w;
10291 int on, hpos, vpos, x, y;
10292 {
10293 struct frame *f = XFRAME (w->frame);
10294 int new_cursor_type;
10295 struct glyph_matrix *current_glyphs;
10296 struct glyph_row *glyph_row;
10297 struct glyph *glyph;
10298
10299 /* This is pointless on invisible frames, and dangerous on garbaged
10300 windows and frames; in the latter case, the frame or window may
10301 be in the midst of changing its size, and x and y may be off the
10302 window. */
10303 if (! FRAME_VISIBLE_P (f)
10304 || FRAME_GARBAGED_P (f)
10305 || vpos >= w->current_matrix->nrows
10306 || hpos >= w->current_matrix->matrix_w)
10307 return;
10308
10309 /* If cursor is off and we want it off, return quickly. */
10310 if (!on && !w->phys_cursor_on_p)
10311 return;
10312
10313 current_glyphs = w->current_matrix;
10314 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10315 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10316
10317 /* If cursor row is not enabled, we don't really know where to
10318 display the cursor. */
10319 if (!glyph_row->enabled_p)
10320 {
10321 w->phys_cursor_on_p = 0;
10322 return;
10323 }
10324
10325 xassert (interrupt_input_blocked);
10326
10327 /* Set new_cursor_type to the cursor we want to be displayed. In a
10328 mini-buffer window, we want the cursor only to appear if we are
10329 reading input from this window. For the selected window, we want
10330 the cursor type given by the frame parameter. If explicitly
10331 marked off, draw no cursor. In all other cases, we want a hollow
10332 box cursor. */
10333 if (cursor_in_echo_area
10334 && FRAME_HAS_MINIBUF_P (f)
10335 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10336 {
10337 if (w == XWINDOW (echo_area_window))
10338 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10339 else
10340 new_cursor_type = HOLLOW_BOX_CURSOR;
10341 }
10342 else
10343 {
10344 if (w != XWINDOW (selected_window)
10345 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10346 {
10347 if (MINI_WINDOW_P (w))
10348 new_cursor_type = NO_CURSOR;
10349 else
10350 new_cursor_type = HOLLOW_BOX_CURSOR;
10351 }
10352 else if (w->cursor_off_p)
10353 new_cursor_type = NO_CURSOR;
10354 else
10355 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10356 }
10357
10358 /* If cursor is currently being shown and we don't want it to be or
10359 it is in the wrong place, or the cursor type is not what we want,
10360 erase it. */
10361 if (w->phys_cursor_on_p
10362 && (!on
10363 || w->phys_cursor.x != x
10364 || w->phys_cursor.y != y
10365 || new_cursor_type != w->phys_cursor_type))
10366 x_erase_phys_cursor (w);
10367
10368 /* If the cursor is now invisible and we want it to be visible,
10369 display it. */
10370 if (on && !w->phys_cursor_on_p)
10371 {
10372 w->phys_cursor_ascent = glyph_row->ascent;
10373 w->phys_cursor_height = glyph_row->height;
10374
10375 /* Set phys_cursor_.* before x_draw_.* is called because some
10376 of them may need the information. */
10377 w->phys_cursor.x = x;
10378 w->phys_cursor.y = glyph_row->y;
10379 w->phys_cursor.hpos = hpos;
10380 w->phys_cursor.vpos = vpos;
10381 w->phys_cursor_type = new_cursor_type;
10382 w->phys_cursor_on_p = 1;
10383
10384 switch (new_cursor_type)
10385 {
10386 case HOLLOW_BOX_CURSOR:
10387 x_draw_hollow_cursor (w, glyph_row);
10388 break;
10389
10390 case FILLED_BOX_CURSOR:
10391 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10392 break;
10393
10394 case BAR_CURSOR:
10395 x_draw_bar_cursor (w, glyph_row);
10396 break;
10397
10398 case NO_CURSOR:
10399 break;
10400
10401 default:
10402 abort ();
10403 }
10404
10405 #ifdef HAVE_X_I18N
10406 if (w == XWINDOW (f->selected_window))
10407 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10408 xic_set_preeditarea (w, x, y);
10409 #endif
10410 }
10411
10412 #ifndef XFlush
10413 if (updating_frame != f)
10414 XFlush (FRAME_X_DISPLAY (f));
10415 #endif
10416 }
10417
10418
10419 /* Display the cursor on window W, or clear it. X and Y are window
10420 relative pixel coordinates. HPOS and VPOS are glyph matrix
10421 positions. If W is not the selected window, display a hollow
10422 cursor. ON non-zero means display the cursor at X, Y which
10423 correspond to HPOS, VPOS, otherwise it is cleared. */
10424
10425 void
10426 x_display_cursor (w, on, hpos, vpos, x, y)
10427 struct window *w;
10428 int on, hpos, vpos, x, y;
10429 {
10430 BLOCK_INPUT;
10431 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10432 UNBLOCK_INPUT;
10433 }
10434
10435
10436 /* Display the cursor on window W, or clear it, according to ON_P.
10437 Don't change the cursor's position. */
10438
10439 void
10440 x_update_cursor (f, on_p)
10441 struct frame *f;
10442 {
10443 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10444 }
10445
10446
10447 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10448 in the window tree rooted at W. */
10449
10450 static void
10451 x_update_cursor_in_window_tree (w, on_p)
10452 struct window *w;
10453 int on_p;
10454 {
10455 while (w)
10456 {
10457 if (!NILP (w->hchild))
10458 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10459 else if (!NILP (w->vchild))
10460 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10461 else
10462 x_update_window_cursor (w, on_p);
10463
10464 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10465 }
10466 }
10467
10468
10469 /* Switch the display of W's cursor on or off, according to the value
10470 of ON. */
10471
10472 static void
10473 x_update_window_cursor (w, on)
10474 struct window *w;
10475 int on;
10476 {
10477 BLOCK_INPUT;
10478 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10479 w->phys_cursor.x, w->phys_cursor.y);
10480 UNBLOCK_INPUT;
10481 }
10482
10483
10484
10485 \f
10486 /* Icons. */
10487
10488 /* Refresh bitmap kitchen sink icon for frame F
10489 when we get an expose event for it. */
10490
10491 void
10492 refreshicon (f)
10493 struct frame *f;
10494 {
10495 /* Normally, the window manager handles this function. */
10496 }
10497
10498 /* Make the x-window of frame F use the gnu icon bitmap. */
10499
10500 int
10501 x_bitmap_icon (f, file)
10502 struct frame *f;
10503 Lisp_Object file;
10504 {
10505 int bitmap_id;
10506
10507 if (FRAME_X_WINDOW (f) == 0)
10508 return 1;
10509
10510 /* Free up our existing icon bitmap if any. */
10511 if (f->output_data.x->icon_bitmap > 0)
10512 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10513 f->output_data.x->icon_bitmap = 0;
10514
10515 if (STRINGP (file))
10516 bitmap_id = x_create_bitmap_from_file (f, file);
10517 else
10518 {
10519 /* Create the GNU bitmap if necessary. */
10520 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10521 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10522 = x_create_bitmap_from_data (f, gnu_bits,
10523 gnu_width, gnu_height);
10524
10525 /* The first time we create the GNU bitmap,
10526 this increments the ref-count one extra time.
10527 As a result, the GNU bitmap is never freed.
10528 That way, we don't have to worry about allocating it again. */
10529 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10530
10531 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10532 }
10533
10534 x_wm_set_icon_pixmap (f, bitmap_id);
10535 f->output_data.x->icon_bitmap = bitmap_id;
10536
10537 return 0;
10538 }
10539
10540
10541 /* Make the x-window of frame F use a rectangle with text.
10542 Use ICON_NAME as the text. */
10543
10544 int
10545 x_text_icon (f, icon_name)
10546 struct frame *f;
10547 char *icon_name;
10548 {
10549 if (FRAME_X_WINDOW (f) == 0)
10550 return 1;
10551
10552 #ifdef HAVE_X11R4
10553 {
10554 XTextProperty text;
10555 text.value = (unsigned char *) icon_name;
10556 text.encoding = XA_STRING;
10557 text.format = 8;
10558 text.nitems = strlen (icon_name);
10559 #ifdef USE_X_TOOLKIT
10560 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10561 &text);
10562 #else /* not USE_X_TOOLKIT */
10563 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10564 #endif /* not USE_X_TOOLKIT */
10565 }
10566 #else /* not HAVE_X11R4 */
10567 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10568 #endif /* not HAVE_X11R4 */
10569
10570 if (f->output_data.x->icon_bitmap > 0)
10571 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10572 f->output_data.x->icon_bitmap = 0;
10573 x_wm_set_icon_pixmap (f, 0);
10574
10575 return 0;
10576 }
10577 \f
10578 #define X_ERROR_MESSAGE_SIZE 200
10579
10580 /* If non-nil, this should be a string.
10581 It means catch X errors and store the error message in this string. */
10582
10583 static Lisp_Object x_error_message_string;
10584
10585 /* An X error handler which stores the error message in
10586 x_error_message_string. This is called from x_error_handler if
10587 x_catch_errors is in effect. */
10588
10589 static void
10590 x_error_catcher (display, error)
10591 Display *display;
10592 XErrorEvent *error;
10593 {
10594 XGetErrorText (display, error->error_code,
10595 XSTRING (x_error_message_string)->data,
10596 X_ERROR_MESSAGE_SIZE);
10597 }
10598
10599 /* Begin trapping X errors for display DPY. Actually we trap X errors
10600 for all displays, but DPY should be the display you are actually
10601 operating on.
10602
10603 After calling this function, X protocol errors no longer cause
10604 Emacs to exit; instead, they are recorded in the string
10605 stored in x_error_message_string.
10606
10607 Calling x_check_errors signals an Emacs error if an X error has
10608 occurred since the last call to x_catch_errors or x_check_errors.
10609
10610 Calling x_uncatch_errors resumes the normal error handling. */
10611
10612 void x_check_errors ();
10613 static Lisp_Object x_catch_errors_unwind ();
10614
10615 int
10616 x_catch_errors (dpy)
10617 Display *dpy;
10618 {
10619 int count = specpdl_ptr - specpdl;
10620
10621 /* Make sure any errors from previous requests have been dealt with. */
10622 XSync (dpy, False);
10623
10624 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10625
10626 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10627 XSTRING (x_error_message_string)->data[0] = 0;
10628
10629 return count;
10630 }
10631
10632 /* Unbind the binding that we made to check for X errors. */
10633
10634 static Lisp_Object
10635 x_catch_errors_unwind (old_val)
10636 Lisp_Object old_val;
10637 {
10638 x_error_message_string = old_val;
10639 return Qnil;
10640 }
10641
10642 /* If any X protocol errors have arrived since the last call to
10643 x_catch_errors or x_check_errors, signal an Emacs error using
10644 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10645
10646 void
10647 x_check_errors (dpy, format)
10648 Display *dpy;
10649 char *format;
10650 {
10651 /* Make sure to catch any errors incurred so far. */
10652 XSync (dpy, False);
10653
10654 if (XSTRING (x_error_message_string)->data[0])
10655 error (format, XSTRING (x_error_message_string)->data);
10656 }
10657
10658 /* Nonzero if we had any X protocol errors
10659 since we did x_catch_errors on DPY. */
10660
10661 int
10662 x_had_errors_p (dpy)
10663 Display *dpy;
10664 {
10665 /* Make sure to catch any errors incurred so far. */
10666 XSync (dpy, False);
10667
10668 return XSTRING (x_error_message_string)->data[0] != 0;
10669 }
10670
10671 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10672
10673 void
10674 x_clear_errors (dpy)
10675 Display *dpy;
10676 {
10677 XSTRING (x_error_message_string)->data[0] = 0;
10678 }
10679
10680 /* Stop catching X protocol errors and let them make Emacs die.
10681 DPY should be the display that was passed to x_catch_errors.
10682 COUNT should be the value that was returned by
10683 the corresponding call to x_catch_errors. */
10684
10685 void
10686 x_uncatch_errors (dpy, count)
10687 Display *dpy;
10688 int count;
10689 {
10690 unbind_to (count, Qnil);
10691 }
10692
10693 #if 0
10694 static unsigned int x_wire_count;
10695 x_trace_wire ()
10696 {
10697 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10698 }
10699 #endif /* ! 0 */
10700
10701 \f
10702 /* Handle SIGPIPE, which can happen when the connection to a server
10703 simply goes away. SIGPIPE is handled by x_connection_signal.
10704 Don't need to do anything, because the write which caused the
10705 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
10706 which will do the appropriate cleanup for us. */
10707
10708 static SIGTYPE
10709 x_connection_signal (signalnum) /* If we don't have an argument, */
10710 int signalnum; /* some compilers complain in signal calls. */
10711 {
10712 #ifdef USG
10713 /* USG systems forget handlers when they are used;
10714 must reestablish each time */
10715 signal (signalnum, x_connection_signal);
10716 #endif /* USG */
10717 }
10718 \f
10719 /* Handling X errors. */
10720
10721 /* Handle the loss of connection to display DISPLAY. */
10722
10723 static SIGTYPE
10724 x_connection_closed (display, error_message)
10725 Display *display;
10726 char *error_message;
10727 {
10728 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10729 Lisp_Object frame, tail;
10730
10731 /* Indicate that this display is dead. */
10732
10733 #ifdef USE_X_TOOLKIT
10734 XtCloseDisplay (display);
10735 #endif
10736
10737 dpyinfo->display = 0;
10738
10739 /* First delete frames whose mini-buffers are on frames
10740 that are on the dead display. */
10741 FOR_EACH_FRAME (tail, frame)
10742 {
10743 Lisp_Object minibuf_frame;
10744 minibuf_frame
10745 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
10746 if (FRAME_X_P (XFRAME (frame))
10747 && FRAME_X_P (XFRAME (minibuf_frame))
10748 && ! EQ (frame, minibuf_frame)
10749 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
10750 Fdelete_frame (frame, Qt);
10751 }
10752
10753 /* Now delete all remaining frames on the dead display.
10754 We are now sure none of these is used as the mini-buffer
10755 for another frame that we need to delete. */
10756 FOR_EACH_FRAME (tail, frame)
10757 if (FRAME_X_P (XFRAME (frame))
10758 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
10759 {
10760 /* Set this to t so that Fdelete_frame won't get confused
10761 trying to find a replacement. */
10762 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10763 Fdelete_frame (frame, Qt);
10764 }
10765
10766 if (dpyinfo)
10767 x_delete_display (dpyinfo);
10768
10769 if (x_display_list == 0)
10770 {
10771 fprintf (stderr, "%s\n", error_message);
10772 shut_down_emacs (0, 0, Qnil);
10773 exit (70);
10774 }
10775
10776 /* Ordinary stack unwind doesn't deal with these. */
10777 #ifdef SIGIO
10778 sigunblock (sigmask (SIGIO));
10779 #endif
10780 sigunblock (sigmask (SIGALRM));
10781 TOTALLY_UNBLOCK_INPUT;
10782
10783 clear_waiting_for_input ();
10784 error ("%s", error_message);
10785 }
10786
10787 /* This is the usual handler for X protocol errors.
10788 It kills all frames on the display that we got the error for.
10789 If that was the only one, it prints an error message and kills Emacs. */
10790
10791 static void
10792 x_error_quitter (display, error)
10793 Display *display;
10794 XErrorEvent *error;
10795 {
10796 char buf[256], buf1[356];
10797
10798 /* Note that there is no real way portable across R3/R4 to get the
10799 original error handler. */
10800
10801 XGetErrorText (display, error->error_code, buf, sizeof (buf));
10802 sprintf (buf1, "X protocol error: %s on protocol request %d",
10803 buf, error->request_code);
10804 x_connection_closed (display, buf1);
10805 }
10806
10807 /* This is the first-level handler for X protocol errors.
10808 It calls x_error_quitter or x_error_catcher. */
10809
10810 static int
10811 x_error_handler (display, error)
10812 Display *display;
10813 XErrorEvent *error;
10814 {
10815 if (! NILP (x_error_message_string))
10816 x_error_catcher (display, error);
10817 else
10818 x_error_quitter (display, error);
10819 return 0;
10820 }
10821
10822 /* This is the handler for X IO errors, always.
10823 It kills all frames on the display that we lost touch with.
10824 If that was the only one, it prints an error message and kills Emacs. */
10825
10826 static int
10827 x_io_error_quitter (display)
10828 Display *display;
10829 {
10830 char buf[256];
10831
10832 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10833 x_connection_closed (display, buf);
10834 return 0;
10835 }
10836 \f
10837 /* Changing the font of the frame. */
10838
10839 /* Give frame F the font named FONTNAME as its default font, and
10840 return the full name of that font. FONTNAME may be a wildcard
10841 pattern; in that case, we choose some font that fits the pattern.
10842 The return value shows which font we chose. */
10843
10844 Lisp_Object
10845 x_new_font (f, fontname)
10846 struct frame *f;
10847 register char *fontname;
10848 {
10849 struct font_info *fontp
10850 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
10851
10852 if (!fontp)
10853 return Qnil;
10854
10855 f->output_data.x->font = (XFontStruct *) (fontp->font);
10856 f->output_data.x->baseline_offset = fontp->baseline_offset;
10857 f->output_data.x->fontset = -1;
10858
10859 /* Compute the scroll bar width in character columns. */
10860 if (f->scroll_bar_pixel_width > 0)
10861 {
10862 int wid = FONT_WIDTH (f->output_data.x->font);
10863 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10864 }
10865 else
10866 {
10867 int wid = FONT_WIDTH (f->output_data.x->font);
10868 f->scroll_bar_cols = (14 + wid - 1) / wid;
10869 }
10870
10871 /* Now make the frame display the given font. */
10872 if (FRAME_X_WINDOW (f) != 0)
10873 {
10874 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10875 f->output_data.x->font->fid);
10876 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10877 f->output_data.x->font->fid);
10878 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10879 f->output_data.x->font->fid);
10880
10881 frame_update_line_height (f);
10882 x_set_window_size (f, 0, f->width, f->height);
10883 }
10884 else
10885 /* If we are setting a new frame's font for the first time,
10886 there are no faces yet, so this font's height is the line height. */
10887 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
10888
10889 return build_string (fontp->full_name);
10890 }
10891
10892 /* Give frame F the fontset named FONTSETNAME as its default font, and
10893 return the full name of that fontset. FONTSETNAME may be a wildcard
10894 pattern; in that case, we choose some fontset that fits the pattern.
10895 The return value shows which fontset we chose. */
10896
10897 Lisp_Object
10898 x_new_fontset (f, fontsetname)
10899 struct frame *f;
10900 char *fontsetname;
10901 {
10902 int fontset = fs_query_fontset (f, fontsetname);
10903 struct fontset_info *fontsetp;
10904 Lisp_Object result;
10905
10906 if (fontset < 0)
10907 return Qnil;
10908
10909 if (f->output_data.x->fontset == fontset)
10910 /* This fontset is already set in frame F. There's nothing more
10911 to do. */
10912 return build_string (fontsetname);
10913
10914 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10915
10916 if (!fontsetp->fontname[CHARSET_ASCII])
10917 /* This fontset doesn't contain ASCII font. */
10918 return Qnil;
10919
10920 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10921
10922 if (!STRINGP (result))
10923 /* Can't load ASCII font. */
10924 return Qnil;
10925
10926 /* Since x_new_font doesn't update any fontset information, do it now. */
10927 f->output_data.x->fontset = fontset;
10928 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
10929 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
10930
10931 #ifdef HAVE_X_I18N
10932 if (FRAME_XIC (f)
10933 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
10934 xic_set_xfontset (f, fontsetp->fontname[CHARSET_ASCII]);
10935 #endif
10936
10937 return build_string (fontsetname);
10938 }
10939
10940 \f
10941 /***********************************************************************
10942 X Input Methods
10943 ***********************************************************************/
10944
10945 #ifdef HAVE_X_I18N
10946
10947 #ifdef HAVE_X11R6
10948
10949 /* XIM destroy callback function, which is called whenever the
10950 connection to input method XIM dies. CLIENT_DATA contains a
10951 pointer to the x_display_info structure corresponding to XIM. */
10952
10953 static void
10954 xim_destroy_callback (xim, client_data, call_data)
10955 XIM xim;
10956 XPointer client_data;
10957 XPointer call_data;
10958 {
10959 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10960 Lisp_Object frame, tail;
10961
10962 BLOCK_INPUT;
10963
10964 /* No need to call XDestroyIC.. */
10965 FOR_EACH_FRAME (tail, frame)
10966 {
10967 struct frame *f = XFRAME (frame);
10968 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
10969 {
10970 FRAME_XIC (f) = NULL;
10971 if (FRAME_XIC_FONTSET (f))
10972 {
10973 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
10974 FRAME_XIC_FONTSET (f) = NULL;
10975 }
10976 }
10977 }
10978
10979 /* No need to call XCloseIM. */
10980 dpyinfo->xim = NULL;
10981 XFree (dpyinfo->xim_styles);
10982 UNBLOCK_INPUT;
10983 }
10984
10985 #endif /* HAVE_X11R6 */
10986
10987 /* Open the connection to the XIM server on display DPYINFO.
10988 RESOURCE_NAME is the resource name Emacs uses. */
10989
10990 static void
10991 xim_open_dpy (dpyinfo, resource_name)
10992 struct x_display_info *dpyinfo;
10993 char *resource_name;
10994 {
10995 XIM xim;
10996
10997 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
10998 dpyinfo->xim = xim;
10999
11000 if (xim)
11001 {
11002 #ifdef HAVE_X11R6
11003 XIMCallback destroy;
11004 #endif
11005
11006 /* Get supported styles and XIM values. */
11007 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11008
11009 #ifdef HAVE_X11R6
11010 destroy.callback = xim_destroy_callback;
11011 destroy.client_data = (XPointer)dpyinfo;
11012 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11013 #endif
11014 }
11015 }
11016
11017
11018 #ifdef HAVE_X11R6
11019
11020 struct xim_inst_t
11021 {
11022 struct x_display_info *dpyinfo;
11023 char *resource_name;
11024 };
11025
11026 /* XIM instantiate callback function, which is called whenever an XIM
11027 server is available. DISPLAY is teh display of the XIM.
11028 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11029 when the callback was registered. */
11030
11031 static void
11032 xim_instantiate_callback (display, client_data, call_data)
11033 Display *display;
11034 XPointer client_data;
11035 XPointer call_data;
11036 {
11037 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11038 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11039
11040 /* We don't support multiple XIM connections. */
11041 if (dpyinfo->xim)
11042 return;
11043
11044 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11045
11046 /* Create XIC for the existing frames on the same display, as long
11047 as they have no XIC. */
11048 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11049 {
11050 Lisp_Object tail, frame;
11051
11052 BLOCK_INPUT;
11053 FOR_EACH_FRAME (tail, frame)
11054 {
11055 struct frame *f = XFRAME (frame);
11056
11057 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11058 if (FRAME_XIC (f) == NULL)
11059 {
11060 create_frame_xic (f);
11061 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11062 xic_set_statusarea (f);
11063 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11064 {
11065 struct window *w = XWINDOW (f->selected_window);
11066 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11067 }
11068 }
11069 }
11070
11071 UNBLOCK_INPUT;
11072 }
11073 }
11074
11075 #endif /* HAVE_X11R6 */
11076
11077
11078 /* Open a connection to the XIM server on display DPYINFO.
11079 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11080 connection only at the first time. On X11R6, open the connection
11081 in the XIM instantiate callback function. */
11082
11083 static void
11084 xim_initialize (dpyinfo, resource_name)
11085 struct x_display_info *dpyinfo;
11086 char *resource_name;
11087 {
11088 #ifdef HAVE_X11R6
11089 struct xim_inst_t *xim_inst;
11090 int len;
11091
11092 dpyinfo->xim = NULL;
11093 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11094 xim_inst->dpyinfo = dpyinfo;
11095 len = strlen (resource_name);
11096 xim_inst->resource_name = (char *) xmalloc (len + 1);
11097 bcopy (resource_name, xim_inst->resource_name, len + 1);
11098 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11099 resource_name, EMACS_CLASS,
11100 xim_instantiate_callback,
11101 (XPointer)xim_inst);
11102 #else /* not HAVE_X11R6 */
11103 dpyinfo->xim = NULL;
11104 xim_open_dpy (dpyinfo, resource_name);
11105 #endif /* not HAVE_X11R6 */
11106 }
11107
11108
11109 /* Close the connection to the XIM server on display DPYINFO. */
11110
11111 static void
11112 xim_close_dpy (dpyinfo)
11113 struct x_display_info *dpyinfo;
11114 {
11115 #ifdef HAVE_X11R6
11116 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11117 NULL, EMACS_CLASS,
11118 xim_instantiate_callback, NULL);
11119 #endif /* HAVE_X11R6 */
11120 XCloseIM (dpyinfo->xim);
11121 dpyinfo->xim = NULL;
11122 XFree (dpyinfo->xim_styles);
11123 }
11124
11125 #endif /* HAVE_X_I18N */
11126
11127
11128 \f
11129 /* Calculate the absolute position in frame F
11130 from its current recorded position values and gravity. */
11131
11132 void
11133 x_calc_absolute_position (f)
11134 struct frame *f;
11135 {
11136 Window child;
11137 int win_x = 0, win_y = 0;
11138 int flags = f->output_data.x->size_hint_flags;
11139 int this_window;
11140
11141 /* We have nothing to do if the current position
11142 is already for the top-left corner. */
11143 if (! ((flags & XNegative) || (flags & YNegative)))
11144 return;
11145
11146 #ifdef USE_X_TOOLKIT
11147 this_window = XtWindow (f->output_data.x->widget);
11148 #else
11149 this_window = FRAME_X_WINDOW (f);
11150 #endif
11151
11152 /* Find the position of the outside upper-left corner of
11153 the inner window, with respect to the outer window.
11154 But do this only if we will need the results. */
11155 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11156 {
11157 int count;
11158
11159 BLOCK_INPUT;
11160 count = x_catch_errors (FRAME_X_DISPLAY (f));
11161 while (1)
11162 {
11163 x_clear_errors (FRAME_X_DISPLAY (f));
11164 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11165
11166 /* From-window, to-window. */
11167 this_window,
11168 f->output_data.x->parent_desc,
11169
11170 /* From-position, to-position. */
11171 0, 0, &win_x, &win_y,
11172
11173 /* Child of win. */
11174 &child);
11175 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11176 {
11177 Window newroot, newparent = 0xdeadbeef;
11178 Window *newchildren;
11179 int nchildren;
11180
11181 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11182 &newparent, &newchildren, &nchildren))
11183 break;
11184
11185 XFree ((char *) newchildren);
11186
11187 f->output_data.x->parent_desc = newparent;
11188 }
11189 else
11190 break;
11191 }
11192
11193 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11194 UNBLOCK_INPUT;
11195 }
11196
11197 /* Treat negative positions as relative to the leftmost bottommost
11198 position that fits on the screen. */
11199 if (flags & XNegative)
11200 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11201 - 2 * f->output_data.x->border_width - win_x
11202 - PIXEL_WIDTH (f)
11203 + f->output_data.x->left_pos);
11204
11205 if (flags & YNegative)
11206 {
11207 int menubar_height = 0;
11208
11209 #ifdef USE_X_TOOLKIT
11210 if (f->output_data.x->menubar_widget)
11211 menubar_height
11212 = (f->output_data.x->menubar_widget->core.height
11213 + f->output_data.x->menubar_widget->core.border_width);
11214 #endif
11215
11216 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11217 - 2 * f->output_data.x->border_width
11218 - win_y
11219 - PIXEL_HEIGHT (f)
11220 - menubar_height
11221 + f->output_data.x->top_pos);
11222 }
11223
11224 /* The left_pos and top_pos
11225 are now relative to the top and left screen edges,
11226 so the flags should correspond. */
11227 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11228 }
11229
11230 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11231 to really change the position, and 0 when calling from
11232 x_make_frame_visible (in that case, XOFF and YOFF are the current
11233 position values). It is -1 when calling from x_set_frame_parameters,
11234 which means, do adjust for borders but don't change the gravity. */
11235
11236 void
11237 x_set_offset (f, xoff, yoff, change_gravity)
11238 struct frame *f;
11239 register int xoff, yoff;
11240 int change_gravity;
11241 {
11242 int modified_top, modified_left;
11243
11244 if (change_gravity > 0)
11245 {
11246 f->output_data.x->top_pos = yoff;
11247 f->output_data.x->left_pos = xoff;
11248 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11249 if (xoff < 0)
11250 f->output_data.x->size_hint_flags |= XNegative;
11251 if (yoff < 0)
11252 f->output_data.x->size_hint_flags |= YNegative;
11253 f->output_data.x->win_gravity = NorthWestGravity;
11254 }
11255 x_calc_absolute_position (f);
11256
11257 BLOCK_INPUT;
11258 x_wm_set_size_hint (f, (long) 0, 0);
11259
11260 modified_left = f->output_data.x->left_pos;
11261 modified_top = f->output_data.x->top_pos;
11262 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11263 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11264 /* It is a mystery why we need to add the border_width here
11265 when the frame is already visible, but experiment says we do. */
11266 if (change_gravity != 0)
11267 {
11268 modified_left += f->output_data.x->border_width;
11269 modified_top += f->output_data.x->border_width;
11270 }
11271 #endif
11272
11273 #ifdef USE_X_TOOLKIT
11274 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11275 modified_left, modified_top);
11276 #else /* not USE_X_TOOLKIT */
11277 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11278 modified_left, modified_top);
11279 #endif /* not USE_X_TOOLKIT */
11280 UNBLOCK_INPUT;
11281 }
11282
11283 /* Call this to change the size of frame F's x-window.
11284 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11285 for this size change and subsequent size changes.
11286 Otherwise we leave the window gravity unchanged. */
11287
11288 void
11289 x_set_window_size (f, change_gravity, cols, rows)
11290 struct frame *f;
11291 int change_gravity;
11292 int cols, rows;
11293 {
11294 #ifndef USE_X_TOOLKIT
11295 int pixelwidth, pixelheight;
11296 #endif
11297
11298 BLOCK_INPUT;
11299
11300 #ifdef USE_X_TOOLKIT
11301 {
11302 /* The x and y position of the widget is clobbered by the
11303 call to XtSetValues within EmacsFrameSetCharSize.
11304 This is a real kludge, but I don't understand Xt so I can't
11305 figure out a correct fix. Can anyone else tell me? -- rms. */
11306 int xpos = f->output_data.x->widget->core.x;
11307 int ypos = f->output_data.x->widget->core.y;
11308 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11309 f->output_data.x->widget->core.x = xpos;
11310 f->output_data.x->widget->core.y = ypos;
11311 }
11312
11313 #else /* not USE_X_TOOLKIT */
11314
11315 check_frame_size (f, &rows, &cols);
11316 f->output_data.x->vertical_scroll_bar_extra
11317 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11318 ? 0
11319 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11320 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11321 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11322 f->output_data.x->flags_areas_extra
11323 = FRAME_FLAGS_AREA_WIDTH (f);
11324 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11325 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11326
11327 f->output_data.x->win_gravity = NorthWestGravity;
11328 x_wm_set_size_hint (f, (long) 0, 0);
11329
11330 XSync (FRAME_X_DISPLAY (f), False);
11331 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11332 pixelwidth, pixelheight);
11333
11334 /* Now, strictly speaking, we can't be sure that this is accurate,
11335 but the window manager will get around to dealing with the size
11336 change request eventually, and we'll hear how it went when the
11337 ConfigureNotify event gets here.
11338
11339 We could just not bother storing any of this information here,
11340 and let the ConfigureNotify event set everything up, but that
11341 might be kind of confusing to the Lisp code, since size changes
11342 wouldn't be reported in the frame parameters until some random
11343 point in the future when the ConfigureNotify event arrives.
11344
11345 We pass 1 for DELAY since we can't run Lisp code inside of
11346 a BLOCK_INPUT. */
11347 change_frame_size (f, rows, cols, 0, 1, 0);
11348 PIXEL_WIDTH (f) = pixelwidth;
11349 PIXEL_HEIGHT (f) = pixelheight;
11350
11351 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11352 receive in the ConfigureNotify event; if we get what we asked
11353 for, then the event won't cause the screen to become garbaged, so
11354 we have to make sure to do it here. */
11355 SET_FRAME_GARBAGED (f);
11356
11357 XFlush (FRAME_X_DISPLAY (f));
11358
11359 #endif /* not USE_X_TOOLKIT */
11360
11361 /* If cursor was outside the new size, mark it as off. */
11362 mark_window_cursors_off (XWINDOW (f->root_window));
11363
11364 /* Clear out any recollection of where the mouse highlighting was,
11365 since it might be in a place that's outside the new frame size.
11366 Actually checking whether it is outside is a pain in the neck,
11367 so don't try--just let the highlighting be done afresh with new size. */
11368 cancel_mouse_face (f);
11369
11370 UNBLOCK_INPUT;
11371 }
11372 \f
11373 /* Mouse warping. */
11374
11375 void
11376 x_set_mouse_position (f, x, y)
11377 struct frame *f;
11378 int x, y;
11379 {
11380 int pix_x, pix_y;
11381
11382 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11383 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11384
11385 if (pix_x < 0) pix_x = 0;
11386 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11387
11388 if (pix_y < 0) pix_y = 0;
11389 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11390
11391 BLOCK_INPUT;
11392
11393 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11394 0, 0, 0, 0, pix_x, pix_y);
11395 UNBLOCK_INPUT;
11396 }
11397
11398 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11399
11400 void
11401 x_set_mouse_pixel_position (f, pix_x, pix_y)
11402 struct frame *f;
11403 int pix_x, pix_y;
11404 {
11405 BLOCK_INPUT;
11406
11407 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11408 0, 0, 0, 0, pix_x, pix_y);
11409 UNBLOCK_INPUT;
11410 }
11411 \f
11412 /* focus shifting, raising and lowering. */
11413
11414 void
11415 x_focus_on_frame (f)
11416 struct frame *f;
11417 {
11418 #if 0 /* This proves to be unpleasant. */
11419 x_raise_frame (f);
11420 #endif
11421 #if 0
11422 /* I don't think that the ICCCM allows programs to do things like this
11423 without the interaction of the window manager. Whatever you end up
11424 doing with this code, do it to x_unfocus_frame too. */
11425 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11426 RevertToPointerRoot, CurrentTime);
11427 #endif /* ! 0 */
11428 }
11429
11430 void
11431 x_unfocus_frame (f)
11432 struct frame *f;
11433 {
11434 #if 0
11435 /* Look at the remarks in x_focus_on_frame. */
11436 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11437 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11438 RevertToPointerRoot, CurrentTime);
11439 #endif /* ! 0 */
11440 }
11441
11442 /* Raise frame F. */
11443
11444 void
11445 x_raise_frame (f)
11446 struct frame *f;
11447 {
11448 if (f->async_visible)
11449 {
11450 BLOCK_INPUT;
11451 #ifdef USE_X_TOOLKIT
11452 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11453 #else /* not USE_X_TOOLKIT */
11454 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11455 #endif /* not USE_X_TOOLKIT */
11456 XFlush (FRAME_X_DISPLAY (f));
11457 UNBLOCK_INPUT;
11458 }
11459 }
11460
11461 /* Lower frame F. */
11462
11463 void
11464 x_lower_frame (f)
11465 struct frame *f;
11466 {
11467 if (f->async_visible)
11468 {
11469 BLOCK_INPUT;
11470 #ifdef USE_X_TOOLKIT
11471 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11472 #else /* not USE_X_TOOLKIT */
11473 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11474 #endif /* not USE_X_TOOLKIT */
11475 XFlush (FRAME_X_DISPLAY (f));
11476 UNBLOCK_INPUT;
11477 }
11478 }
11479
11480 static void
11481 XTframe_raise_lower (f, raise_flag)
11482 FRAME_PTR f;
11483 int raise_flag;
11484 {
11485 if (raise_flag)
11486 x_raise_frame (f);
11487 else
11488 x_lower_frame (f);
11489 }
11490 \f
11491 /* Change of visibility. */
11492
11493 /* This tries to wait until the frame is really visible.
11494 However, if the window manager asks the user where to position
11495 the frame, this will return before the user finishes doing that.
11496 The frame will not actually be visible at that time,
11497 but it will become visible later when the window manager
11498 finishes with it. */
11499
11500 void
11501 x_make_frame_visible (f)
11502 struct frame *f;
11503 {
11504 Lisp_Object type;
11505 int original_top, original_left;
11506
11507 BLOCK_INPUT;
11508
11509 type = x_icon_type (f);
11510 if (!NILP (type))
11511 x_bitmap_icon (f, type);
11512
11513 if (! FRAME_VISIBLE_P (f))
11514 {
11515 /* We test FRAME_GARBAGED_P here to make sure we don't
11516 call x_set_offset a second time
11517 if we get to x_make_frame_visible a second time
11518 before the window gets really visible. */
11519 if (! FRAME_ICONIFIED_P (f)
11520 && ! f->output_data.x->asked_for_visible)
11521 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11522
11523 f->output_data.x->asked_for_visible = 1;
11524
11525 if (! EQ (Vx_no_window_manager, Qt))
11526 x_wm_set_window_state (f, NormalState);
11527 #ifdef USE_X_TOOLKIT
11528 /* This was XtPopup, but that did nothing for an iconified frame. */
11529 XtMapWidget (f->output_data.x->widget);
11530 #else /* not USE_X_TOOLKIT */
11531 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11532 #endif /* not USE_X_TOOLKIT */
11533 #if 0 /* This seems to bring back scroll bars in the wrong places
11534 if the window configuration has changed. They seem
11535 to come back ok without this. */
11536 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11537 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11538 #endif
11539 }
11540
11541 XFlush (FRAME_X_DISPLAY (f));
11542
11543 /* Synchronize to ensure Emacs knows the frame is visible
11544 before we do anything else. We do this loop with input not blocked
11545 so that incoming events are handled. */
11546 {
11547 Lisp_Object frame;
11548 int count;
11549 /* This must be before UNBLOCK_INPUT
11550 since events that arrive in response to the actions above
11551 will set it when they are handled. */
11552 int previously_visible = f->output_data.x->has_been_visible;
11553
11554 original_left = f->output_data.x->left_pos;
11555 original_top = f->output_data.x->top_pos;
11556
11557 /* This must come after we set COUNT. */
11558 UNBLOCK_INPUT;
11559
11560 /* We unblock here so that arriving X events are processed. */
11561
11562 /* Now move the window back to where it was "supposed to be".
11563 But don't do it if the gravity is negative.
11564 When the gravity is negative, this uses a position
11565 that is 3 pixels too low. Perhaps that's really the border width.
11566
11567 Don't do this if the window has never been visible before,
11568 because the window manager may choose the position
11569 and we don't want to override it. */
11570
11571 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11572 && f->output_data.x->win_gravity == NorthWestGravity
11573 && previously_visible)
11574 {
11575 Drawable rootw;
11576 int x, y;
11577 unsigned int width, height, border, depth;
11578
11579 BLOCK_INPUT;
11580
11581 /* On some window managers (such as FVWM) moving an existing
11582 window, even to the same place, causes the window manager
11583 to introduce an offset. This can cause the window to move
11584 to an unexpected location. Check the geometry (a little
11585 slow here) and then verify that the window is in the right
11586 place. If the window is not in the right place, move it
11587 there, and take the potential window manager hit. */
11588 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11589 &rootw, &x, &y, &width, &height, &border, &depth);
11590
11591 if (original_left != x || original_top != y)
11592 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11593 original_left, original_top);
11594
11595 UNBLOCK_INPUT;
11596 }
11597
11598 XSETFRAME (frame, f);
11599
11600 /* Wait until the frame is visible. Process X events until a
11601 MapNotify event has been seen, or until we think we won't get a
11602 MapNotify at all.. */
11603 for (count = input_signal_count + 10;
11604 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11605 {
11606 /* Force processing of queued events. */
11607 x_sync (f);
11608
11609 /* Machines that do polling rather than SIGIO have been
11610 observed to go into a busy-wait here. So we'll fake an
11611 alarm signal to let the handler know that there's something
11612 to be read. We used to raise a real alarm, but it seems
11613 that the handler isn't always enabled here. This is
11614 probably a bug. */
11615 if (input_polling_used ())
11616 {
11617 /* It could be confusing if a real alarm arrives while
11618 processing the fake one. Turn it off and let the
11619 handler reset it. */
11620 int old_poll_suppress_count = poll_suppress_count;
11621 poll_suppress_count = 1;
11622 poll_for_input_1 ();
11623 poll_suppress_count = old_poll_suppress_count;
11624 }
11625
11626 /* See if a MapNotify event has been processed. */
11627 FRAME_SAMPLE_VISIBILITY (f);
11628 }
11629 }
11630 }
11631
11632 /* Change from mapped state to withdrawn state. */
11633
11634 /* Make the frame visible (mapped and not iconified). */
11635
11636 void
11637 x_make_frame_invisible (f)
11638 struct frame *f;
11639 {
11640 Window window;
11641
11642 #ifdef USE_X_TOOLKIT
11643 /* Use the frame's outermost window, not the one we normally draw on. */
11644 window = XtWindow (f->output_data.x->widget);
11645 #else /* not USE_X_TOOLKIT */
11646 window = FRAME_X_WINDOW (f);
11647 #endif /* not USE_X_TOOLKIT */
11648
11649 /* Don't keep the highlight on an invisible frame. */
11650 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11651 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11652
11653 #if 0/* This might add unreliability; I don't trust it -- rms. */
11654 if (! f->async_visible && ! f->async_iconified)
11655 return;
11656 #endif
11657
11658 BLOCK_INPUT;
11659
11660 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11661 that the current position of the window is user-specified, rather than
11662 program-specified, so that when the window is mapped again, it will be
11663 placed at the same location, without forcing the user to position it
11664 by hand again (they have already done that once for this window.) */
11665 x_wm_set_size_hint (f, (long) 0, 1);
11666
11667 #ifdef HAVE_X11R4
11668
11669 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11670 DefaultScreen (FRAME_X_DISPLAY (f))))
11671 {
11672 UNBLOCK_INPUT_RESIGNAL;
11673 error ("Can't notify window manager of window withdrawal");
11674 }
11675 #else /* ! defined (HAVE_X11R4) */
11676
11677 /* Tell the window manager what we're going to do. */
11678 if (! EQ (Vx_no_window_manager, Qt))
11679 {
11680 XEvent unmap;
11681
11682 unmap.xunmap.type = UnmapNotify;
11683 unmap.xunmap.window = window;
11684 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
11685 unmap.xunmap.from_configure = False;
11686 if (! XSendEvent (FRAME_X_DISPLAY (f),
11687 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11688 False,
11689 SubstructureRedirectMaskSubstructureNotifyMask,
11690 &unmap))
11691 {
11692 UNBLOCK_INPUT_RESIGNAL;
11693 error ("Can't notify window manager of withdrawal");
11694 }
11695 }
11696
11697 /* Unmap the window ourselves. Cheeky! */
11698 XUnmapWindow (FRAME_X_DISPLAY (f), window);
11699 #endif /* ! defined (HAVE_X11R4) */
11700
11701 /* We can't distinguish this from iconification
11702 just by the event that we get from the server.
11703 So we can't win using the usual strategy of letting
11704 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11705 and synchronize with the server to make sure we agree. */
11706 f->visible = 0;
11707 FRAME_ICONIFIED_P (f) = 0;
11708 f->async_visible = 0;
11709 f->async_iconified = 0;
11710
11711 x_sync (f);
11712
11713 UNBLOCK_INPUT;
11714 }
11715
11716 /* Change window state from mapped to iconified. */
11717
11718 void
11719 x_iconify_frame (f)
11720 struct frame *f;
11721 {
11722 int result;
11723 Lisp_Object type;
11724
11725 /* Don't keep the highlight on an invisible frame. */
11726 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11727 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11728
11729 if (f->async_iconified)
11730 return;
11731
11732 BLOCK_INPUT;
11733
11734 FRAME_SAMPLE_VISIBILITY (f);
11735
11736 type = x_icon_type (f);
11737 if (!NILP (type))
11738 x_bitmap_icon (f, type);
11739
11740 #ifdef USE_X_TOOLKIT
11741
11742 if (! FRAME_VISIBLE_P (f))
11743 {
11744 if (! EQ (Vx_no_window_manager, Qt))
11745 x_wm_set_window_state (f, IconicState);
11746 /* This was XtPopup, but that did nothing for an iconified frame. */
11747 XtMapWidget (f->output_data.x->widget);
11748 /* The server won't give us any event to indicate
11749 that an invisible frame was changed to an icon,
11750 so we have to record it here. */
11751 f->iconified = 1;
11752 f->visible = 1;
11753 f->async_iconified = 1;
11754 f->async_visible = 0;
11755 UNBLOCK_INPUT;
11756 return;
11757 }
11758
11759 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11760 XtWindow (f->output_data.x->widget),
11761 DefaultScreen (FRAME_X_DISPLAY (f)));
11762 UNBLOCK_INPUT;
11763
11764 if (!result)
11765 error ("Can't notify window manager of iconification");
11766
11767 f->async_iconified = 1;
11768 f->async_visible = 0;
11769
11770
11771 BLOCK_INPUT;
11772 XFlush (FRAME_X_DISPLAY (f));
11773 UNBLOCK_INPUT;
11774 #else /* not USE_X_TOOLKIT */
11775
11776 /* Make sure the X server knows where the window should be positioned,
11777 in case the user deiconifies with the window manager. */
11778 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
11779 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11780
11781 /* Since we don't know which revision of X we're running, we'll use both
11782 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11783
11784 /* X11R4: send a ClientMessage to the window manager using the
11785 WM_CHANGE_STATE type. */
11786 {
11787 XEvent message;
11788
11789 message.xclient.window = FRAME_X_WINDOW (f);
11790 message.xclient.type = ClientMessage;
11791 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
11792 message.xclient.format = 32;
11793 message.xclient.data.l[0] = IconicState;
11794
11795 if (! XSendEvent (FRAME_X_DISPLAY (f),
11796 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11797 False,
11798 SubstructureRedirectMask | SubstructureNotifyMask,
11799 &message))
11800 {
11801 UNBLOCK_INPUT_RESIGNAL;
11802 error ("Can't notify window manager of iconification");
11803 }
11804 }
11805
11806 /* X11R3: set the initial_state field of the window manager hints to
11807 IconicState. */
11808 x_wm_set_window_state (f, IconicState);
11809
11810 if (!FRAME_VISIBLE_P (f))
11811 {
11812 /* If the frame was withdrawn, before, we must map it. */
11813 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11814 }
11815
11816 f->async_iconified = 1;
11817 f->async_visible = 0;
11818
11819 XFlush (FRAME_X_DISPLAY (f));
11820 UNBLOCK_INPUT;
11821 #endif /* not USE_X_TOOLKIT */
11822 }
11823 \f
11824 /* Destroy the X window of frame F. */
11825
11826 void
11827 x_destroy_window (f)
11828 struct frame *f;
11829 {
11830 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11831
11832 BLOCK_INPUT;
11833
11834 /* If a display connection is dead, don't try sending more
11835 commands to the X server. */
11836 if (dpyinfo->display != 0)
11837 {
11838 if (f->output_data.x->icon_desc != 0)
11839 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11840 #ifdef HAVE_X_I18N
11841 if (FRAME_XIC (f))
11842 free_frame_xic (f);
11843 #endif
11844 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
11845 #ifdef USE_X_TOOLKIT
11846 if (f->output_data.x->widget)
11847 XtDestroyWidget (f->output_data.x->widget);
11848 free_frame_menubar (f);
11849 #endif /* USE_X_TOOLKIT */
11850
11851 free_frame_faces (f);
11852 XFlush (FRAME_X_DISPLAY (f));
11853 }
11854
11855 if (f->output_data.x->saved_menu_event)
11856 xfree (f->output_data.x->saved_menu_event);
11857
11858 xfree (f->output_data.x);
11859 f->output_data.x = 0;
11860 if (f == dpyinfo->x_focus_frame)
11861 dpyinfo->x_focus_frame = 0;
11862 if (f == dpyinfo->x_focus_event_frame)
11863 dpyinfo->x_focus_event_frame = 0;
11864 if (f == dpyinfo->x_highlight_frame)
11865 dpyinfo->x_highlight_frame = 0;
11866
11867 dpyinfo->reference_count--;
11868
11869 if (f == dpyinfo->mouse_face_mouse_frame)
11870 {
11871 dpyinfo->mouse_face_beg_row
11872 = dpyinfo->mouse_face_beg_col = -1;
11873 dpyinfo->mouse_face_end_row
11874 = dpyinfo->mouse_face_end_col = -1;
11875 dpyinfo->mouse_face_window = Qnil;
11876 dpyinfo->mouse_face_deferred_gc = 0;
11877 dpyinfo->mouse_face_mouse_frame = 0;
11878 }
11879
11880 UNBLOCK_INPUT;
11881 }
11882 \f
11883 /* Setting window manager hints. */
11884
11885 /* Set the normal size hints for the window manager, for frame F.
11886 FLAGS is the flags word to use--or 0 meaning preserve the flags
11887 that the window now has.
11888 If USER_POSITION is nonzero, we set the USPosition
11889 flag (this is useful when FLAGS is 0). */
11890
11891 void
11892 x_wm_set_size_hint (f, flags, user_position)
11893 struct frame *f;
11894 long flags;
11895 int user_position;
11896 {
11897 XSizeHints size_hints;
11898
11899 #ifdef USE_X_TOOLKIT
11900 Arg al[2];
11901 int ac = 0;
11902 Dimension widget_width, widget_height;
11903 Window window = XtWindow (f->output_data.x->widget);
11904 #else /* not USE_X_TOOLKIT */
11905 Window window = FRAME_X_WINDOW (f);
11906 #endif /* not USE_X_TOOLKIT */
11907
11908 /* Setting PMaxSize caused various problems. */
11909 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11910
11911 size_hints.x = f->output_data.x->left_pos;
11912 size_hints.y = f->output_data.x->top_pos;
11913
11914 #ifdef USE_X_TOOLKIT
11915 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11916 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
11917 XtGetValues (f->output_data.x->widget, al, ac);
11918 size_hints.height = widget_height;
11919 size_hints.width = widget_width;
11920 #else /* not USE_X_TOOLKIT */
11921 size_hints.height = PIXEL_HEIGHT (f);
11922 size_hints.width = PIXEL_WIDTH (f);
11923 #endif /* not USE_X_TOOLKIT */
11924
11925 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11926 size_hints.height_inc = f->output_data.x->line_height;
11927 size_hints.max_width
11928 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11929 size_hints.max_height
11930 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
11931
11932 /* Calculate the base and minimum sizes.
11933
11934 (When we use the X toolkit, we don't do it here.
11935 Instead we copy the values that the widgets are using, below.) */
11936 #ifndef USE_X_TOOLKIT
11937 {
11938 int base_width, base_height;
11939 int min_rows = 0, min_cols = 0;
11940
11941 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11942 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
11943
11944 check_frame_size (f, &min_rows, &min_cols);
11945
11946 /* The window manager uses the base width hints to calculate the
11947 current number of rows and columns in the frame while
11948 resizing; min_width and min_height aren't useful for this
11949 purpose, since they might not give the dimensions for a
11950 zero-row, zero-column frame.
11951
11952 We use the base_width and base_height members if we have
11953 them; otherwise, we set the min_width and min_height members
11954 to the size for a zero x zero frame. */
11955
11956 #ifdef HAVE_X11R4
11957 size_hints.flags |= PBaseSize;
11958 size_hints.base_width = base_width;
11959 size_hints.base_height = base_height;
11960 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11961 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
11962 #else
11963 size_hints.min_width = base_width;
11964 size_hints.min_height = base_height;
11965 #endif
11966 }
11967
11968 /* If we don't need the old flags, we don't need the old hint at all. */
11969 if (flags)
11970 {
11971 size_hints.flags |= flags;
11972 goto no_read;
11973 }
11974 #endif /* not USE_X_TOOLKIT */
11975
11976 {
11977 XSizeHints hints; /* Sometimes I hate X Windows... */
11978 long supplied_return;
11979 int value;
11980
11981 #ifdef HAVE_X11R4
11982 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11983 &supplied_return);
11984 #else
11985 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
11986 #endif
11987
11988 #ifdef USE_X_TOOLKIT
11989 size_hints.base_height = hints.base_height;
11990 size_hints.base_width = hints.base_width;
11991 size_hints.min_height = hints.min_height;
11992 size_hints.min_width = hints.min_width;
11993 #endif
11994
11995 if (flags)
11996 size_hints.flags |= flags;
11997 else
11998 {
11999 if (value == 0)
12000 hints.flags = 0;
12001 if (hints.flags & PSize)
12002 size_hints.flags |= PSize;
12003 if (hints.flags & PPosition)
12004 size_hints.flags |= PPosition;
12005 if (hints.flags & USPosition)
12006 size_hints.flags |= USPosition;
12007 if (hints.flags & USSize)
12008 size_hints.flags |= USSize;
12009 }
12010 }
12011
12012 #ifndef USE_X_TOOLKIT
12013 no_read:
12014 #endif
12015
12016 #ifdef PWinGravity
12017 size_hints.win_gravity = f->output_data.x->win_gravity;
12018 size_hints.flags |= PWinGravity;
12019
12020 if (user_position)
12021 {
12022 size_hints.flags &= ~ PPosition;
12023 size_hints.flags |= USPosition;
12024 }
12025 #endif /* PWinGravity */
12026
12027 #ifdef HAVE_X11R4
12028 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12029 #else
12030 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12031 #endif
12032 }
12033
12034 /* Used for IconicState or NormalState */
12035
12036 void
12037 x_wm_set_window_state (f, state)
12038 struct frame *f;
12039 int state;
12040 {
12041 #ifdef USE_X_TOOLKIT
12042 Arg al[1];
12043
12044 XtSetArg (al[0], XtNinitialState, state);
12045 XtSetValues (f->output_data.x->widget, al, 1);
12046 #else /* not USE_X_TOOLKIT */
12047 Window window = FRAME_X_WINDOW (f);
12048
12049 f->output_data.x->wm_hints.flags |= StateHint;
12050 f->output_data.x->wm_hints.initial_state = state;
12051
12052 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12053 #endif /* not USE_X_TOOLKIT */
12054 }
12055
12056 void
12057 x_wm_set_icon_pixmap (f, pixmap_id)
12058 struct frame *f;
12059 int pixmap_id;
12060 {
12061 Pixmap icon_pixmap;
12062
12063 #ifndef USE_X_TOOLKIT
12064 Window window = FRAME_X_WINDOW (f);
12065 #endif
12066
12067 if (pixmap_id > 0)
12068 {
12069 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12070 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12071 }
12072 else
12073 {
12074 /* It seems there is no way to turn off use of an icon pixmap.
12075 The following line does it, only if no icon has yet been created,
12076 for some window managers. But with mwm it crashes.
12077 Some people say it should clear the IconPixmapHint bit in this case,
12078 but that doesn't work, and the X consortium said it isn't the
12079 right thing at all. Since there is no way to win,
12080 best to explicitly give up. */
12081 #if 0
12082 f->output_data.x->wm_hints.icon_pixmap = None;
12083 #else
12084 return;
12085 #endif
12086 }
12087
12088 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12089
12090 {
12091 Arg al[1];
12092 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12093 XtSetValues (f->output_data.x->widget, al, 1);
12094 }
12095
12096 #else /* not USE_X_TOOLKIT */
12097
12098 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12099 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12100
12101 #endif /* not USE_X_TOOLKIT */
12102 }
12103
12104 void
12105 x_wm_set_icon_position (f, icon_x, icon_y)
12106 struct frame *f;
12107 int icon_x, icon_y;
12108 {
12109 #ifdef USE_X_TOOLKIT
12110 Window window = XtWindow (f->output_data.x->widget);
12111 #else
12112 Window window = FRAME_X_WINDOW (f);
12113 #endif
12114
12115 f->output_data.x->wm_hints.flags |= IconPositionHint;
12116 f->output_data.x->wm_hints.icon_x = icon_x;
12117 f->output_data.x->wm_hints.icon_y = icon_y;
12118
12119 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12120 }
12121
12122 \f
12123 /***********************************************************************
12124 Fonts
12125 ***********************************************************************/
12126
12127 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12128
12129 struct font_info *
12130 x_get_font_info (f, font_idx)
12131 FRAME_PTR f;
12132 int font_idx;
12133 {
12134 return (FRAME_X_FONT_TABLE (f) + font_idx);
12135 }
12136
12137
12138 /* Return a list of names of available fonts matching PATTERN on frame
12139 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12140 to be listed. Frame F NULL means we have not yet created any
12141 frame on X, and consult the first display in x_display_list.
12142 MAXNAMES sets a limit on how many fonts to match. */
12143
12144 Lisp_Object
12145 x_list_fonts (f, pattern, size, maxnames)
12146 FRAME_PTR f;
12147 Lisp_Object pattern;
12148 int size;
12149 int maxnames;
12150 {
12151 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12152 Lisp_Object tem, second_best;
12153 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12154 int try_XLoadQueryFont = 0;
12155 int count;
12156
12157 patterns = Fassoc (pattern, Valternate_fontname_alist);
12158 if (NILP (patterns))
12159 patterns = Fcons (pattern, Qnil);
12160
12161 if (maxnames == 1 && !size)
12162 /* We can return any single font matching PATTERN. */
12163 try_XLoadQueryFont = 1;
12164
12165 for (; CONSP (patterns); patterns = XCDR (patterns))
12166 {
12167 int num_fonts;
12168 char **names;
12169
12170 pattern = XCAR (patterns);
12171 /* See if we cached the result for this particular query.
12172 The cache is an alist of the form:
12173 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12174 */
12175 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12176 key = Fcons (pattern, make_number (maxnames)),
12177 !NILP (list = Fassoc (key, tem))))
12178 {
12179 list = Fcdr_safe (list);
12180 /* We have a cashed list. Don't have to get the list again. */
12181 goto label_cached;
12182 }
12183
12184 /* At first, put PATTERN in the cache. */
12185
12186 BLOCK_INPUT;
12187 count = x_catch_errors (dpy);
12188
12189 if (try_XLoadQueryFont)
12190 {
12191 XFontStruct *font;
12192 unsigned long value;
12193
12194 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12195 if (x_had_errors_p (dpy))
12196 {
12197 /* This error is perhaps due to insufficient memory on X
12198 server. Let's just ignore it. */
12199 font = NULL;
12200 x_clear_errors (dpy);
12201 }
12202
12203 if (font
12204 && XGetFontProperty (font, XA_FONT, &value))
12205 {
12206 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12207 int len = strlen (name);
12208 char *tmp;
12209
12210 /* If DXPC (a Differential X Protocol Compressor)
12211 Ver.3.7 is running, XGetAtomName will return null
12212 string. We must avoid such a name. */
12213 if (len == 0)
12214 try_XLoadQueryFont = 0;
12215 else
12216 {
12217 num_fonts = 1;
12218 names = (char **) alloca (sizeof (char *));
12219 /* Some systems only allow alloca assigned to a
12220 simple var. */
12221 tmp = (char *) alloca (len + 1); names[0] = tmp;
12222 bcopy (name, names[0], len + 1);
12223 XFree (name);
12224 }
12225 }
12226 else
12227 try_XLoadQueryFont = 0;
12228
12229 if (font)
12230 XFreeFont (dpy, font);
12231 }
12232
12233 if (!try_XLoadQueryFont)
12234 {
12235 /* We try at least 10 fonts because XListFonts will return
12236 auto-scaled fonts at the head. */
12237 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12238 &num_fonts);
12239 if (x_had_errors_p (dpy))
12240 {
12241 /* This error is perhaps due to insufficient memory on X
12242 server. Let's just ignore it. */
12243 names = NULL;
12244 x_clear_errors (dpy);
12245 }
12246 }
12247
12248 x_uncatch_errors (dpy, count);
12249 UNBLOCK_INPUT;
12250
12251 if (names)
12252 {
12253 int i;
12254
12255 /* Make a list of all the fonts we got back.
12256 Store that in the font cache for the display. */
12257 for (i = 0; i < num_fonts; i++)
12258 {
12259 int width = 0;
12260 char *p = names[i];
12261 int average_width = -1, dashes = 0;
12262
12263 /* Count the number of dashes in NAMES[I]. If there are
12264 14 dashes, and the field value following 12th dash
12265 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12266 is usually too ugly to be used for editing. Let's
12267 ignore it. */
12268 while (*p)
12269 if (*p++ == '-')
12270 {
12271 dashes++;
12272 if (dashes == 7) /* PIXEL_SIZE field */
12273 width = atoi (p);
12274 else if (dashes == 12) /* AVERAGE_WIDTH field */
12275 average_width = atoi (p);
12276 }
12277 if (dashes < 14 || average_width != 0)
12278 {
12279 tem = build_string (names[i]);
12280 if (NILP (Fassoc (tem, list)))
12281 {
12282 if (STRINGP (Vx_pixel_size_width_font_regexp)
12283 && ((fast_c_string_match_ignore_case
12284 (Vx_pixel_size_width_font_regexp, names[i]))
12285 >= 0))
12286 /* We can set the value of PIXEL_SIZE to the
12287 width of this font. */
12288 list = Fcons (Fcons (tem, make_number (width)), list);
12289 else
12290 /* For the moment, width is not known. */
12291 list = Fcons (Fcons (tem, Qnil), list);
12292 }
12293 }
12294 }
12295 if (!try_XLoadQueryFont)
12296 XFreeFontNames (names);
12297 }
12298
12299 /* Now store the result in the cache. */
12300 if (f != NULL)
12301 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12302 = Fcons (Fcons (key, list),
12303 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12304
12305 label_cached:
12306 if (NILP (list)) continue; /* Try the remaining alternatives. */
12307
12308 newlist = second_best = Qnil;
12309 /* Make a list of the fonts that have the right width. */
12310 for (; CONSP (list); list = XCDR (list))
12311 {
12312 int found_size;
12313
12314 tem = XCAR (list);
12315
12316 if (!CONSP (tem) || NILP (XCAR (tem)))
12317 continue;
12318 if (!size)
12319 {
12320 newlist = Fcons (XCAR (tem), newlist);
12321 continue;
12322 }
12323
12324 if (!INTEGERP (XCDR (tem)))
12325 {
12326 /* Since we have not yet known the size of this font, we
12327 must try slow function call XLoadQueryFont. */
12328 XFontStruct *thisinfo;
12329
12330 BLOCK_INPUT;
12331 count = x_catch_errors (dpy);
12332 thisinfo = XLoadQueryFont (dpy,
12333 XSTRING (XCAR (tem))->data);
12334 if (x_had_errors_p (dpy))
12335 {
12336 /* This error is perhaps due to insufficient memory on X
12337 server. Let's just ignore it. */
12338 thisinfo = NULL;
12339 x_clear_errors (dpy);
12340 }
12341 x_uncatch_errors (dpy, count);
12342 UNBLOCK_INPUT;
12343
12344 if (thisinfo)
12345 {
12346 XCDR (tem)
12347 = (thisinfo->min_bounds.width == 0
12348 ? make_number (0)
12349 : make_number (thisinfo->max_bounds.width));
12350 XFreeFont (dpy, thisinfo);
12351 }
12352 else
12353 /* For unknown reason, the previous call of XListFont had
12354 returned a font which can't be opened. Record the size
12355 as 0 not to try to open it again. */
12356 XCDR (tem) = make_number (0);
12357 }
12358
12359 found_size = XINT (XCDR (tem));
12360 if (found_size == size)
12361 newlist = Fcons (XCAR (tem), newlist);
12362 else if (found_size > 0)
12363 {
12364 if (NILP (second_best))
12365 second_best = tem;
12366 else if (found_size < size)
12367 {
12368 if (XINT (XCDR (second_best)) > size
12369 || XINT (XCDR (second_best)) < found_size)
12370 second_best = tem;
12371 }
12372 else
12373 {
12374 if (XINT (XCDR (second_best)) > size
12375 && XINT (XCDR (second_best)) > found_size)
12376 second_best = tem;
12377 }
12378 }
12379 }
12380 if (!NILP (newlist))
12381 break;
12382 else if (!NILP (second_best))
12383 {
12384 newlist = Fcons (XCAR (second_best), Qnil);
12385 break;
12386 }
12387 }
12388
12389 return newlist;
12390 }
12391
12392
12393 #if GLYPH_DEBUG
12394
12395 /* Check that FONT is valid on frame F. It is if it can be found in F's
12396 font table. */
12397
12398 static void
12399 x_check_font (f, font)
12400 struct frame *f;
12401 XFontStruct *font;
12402 {
12403 int i;
12404 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12405
12406 xassert (font != NULL);
12407
12408 for (i = 0; i < dpyinfo->n_fonts; i++)
12409 if (dpyinfo->font_table[i].name
12410 && font == dpyinfo->font_table[i].font)
12411 break;
12412
12413 xassert (i < dpyinfo->n_fonts);
12414 }
12415
12416 #endif /* GLYPH_DEBUG != 0 */
12417
12418 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12419 Note: There are (broken) X fonts out there with invalid XFontStruct
12420 min_bounds contents. For example, handa@etl.go.jp reports that
12421 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12422 have font->min_bounds.width == 0. */
12423
12424 static INLINE void
12425 x_font_min_bounds (font, w, h)
12426 XFontStruct *font;
12427 int *w, *h;
12428 {
12429 *h = FONT_HEIGHT (font);
12430 *w = font->min_bounds.width;
12431
12432 /* Try to handle the case where FONT->min_bounds has invalid
12433 contents. Since the only font known to have invalid min_bounds
12434 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12435 if (*w <= 0)
12436 *w = font->max_bounds.width;
12437 }
12438
12439
12440 /* Compute the smallest character width and smallest font height over
12441 all fonts available on frame F. Set the members smallest_char_width
12442 and smallest_font_height in F's x_display_info structure to
12443 the values computed. Value is non-zero if smallest_font_height or
12444 smallest_char_width become smaller than they were before. */
12445
12446 static int
12447 x_compute_min_glyph_bounds (f)
12448 struct frame *f;
12449 {
12450 int i;
12451 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12452 XFontStruct *font;
12453 int old_width = dpyinfo->smallest_char_width;
12454 int old_height = dpyinfo->smallest_font_height;
12455
12456 dpyinfo->smallest_font_height = 100000;
12457 dpyinfo->smallest_char_width = 100000;
12458
12459 for (i = 0; i < dpyinfo->n_fonts; ++i)
12460 if (dpyinfo->font_table[i].name)
12461 {
12462 struct font_info *fontp = dpyinfo->font_table + i;
12463 int w, h;
12464
12465 font = (XFontStruct *) fontp->font;
12466 xassert (font != (XFontStruct *) ~0);
12467 x_font_min_bounds (font, &w, &h);
12468
12469 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12470 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12471 }
12472
12473 xassert (dpyinfo->smallest_char_width > 0
12474 && dpyinfo->smallest_font_height > 0);
12475
12476 return (dpyinfo->n_fonts == 1
12477 || dpyinfo->smallest_char_width < old_width
12478 || dpyinfo->smallest_font_height < old_height);
12479 }
12480
12481
12482 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12483 pointer to the structure font_info while allocating it dynamically.
12484 If SIZE is 0, load any size of font.
12485 If loading is failed, return NULL. */
12486
12487 struct font_info *
12488 x_load_font (f, fontname, size)
12489 struct frame *f;
12490 register char *fontname;
12491 int size;
12492 {
12493 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12494 Lisp_Object font_names;
12495 int count;
12496
12497 /* Get a list of all the fonts that match this name. Once we
12498 have a list of matching fonts, we compare them against the fonts
12499 we already have by comparing names. */
12500 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12501
12502 if (!NILP (font_names))
12503 {
12504 Lisp_Object tail;
12505 int i;
12506
12507 for (i = 0; i < dpyinfo->n_fonts; i++)
12508 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12509 if (dpyinfo->font_table[i].name
12510 && (!strcmp (dpyinfo->font_table[i].name,
12511 XSTRING (XCAR (tail))->data)
12512 || !strcmp (dpyinfo->font_table[i].full_name,
12513 XSTRING (XCAR (tail))->data)))
12514 return (dpyinfo->font_table + i);
12515 }
12516
12517 /* Load the font and add it to the table. */
12518 {
12519 char *full_name;
12520 XFontStruct *font;
12521 struct font_info *fontp;
12522 unsigned long value;
12523 int i;
12524
12525 /* If we have found fonts by x_list_font, load one of them. If
12526 not, we still try to load a font by the name given as FONTNAME
12527 because XListFonts (called in x_list_font) of some X server has
12528 a bug of not finding a font even if the font surely exists and
12529 is loadable by XLoadQueryFont. */
12530 if (size > 0 && !NILP (font_names))
12531 fontname = (char *) XSTRING (XCAR (font_names))->data;
12532
12533 BLOCK_INPUT;
12534 count = x_catch_errors (FRAME_X_DISPLAY (f));
12535 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12536 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12537 {
12538 /* This error is perhaps due to insufficient memory on X
12539 server. Let's just ignore it. */
12540 font = NULL;
12541 x_clear_errors (FRAME_X_DISPLAY (f));
12542 }
12543 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12544 UNBLOCK_INPUT;
12545 if (!font)
12546 return NULL;
12547
12548 /* Find a free slot in the font table. */
12549 for (i = 0; i < dpyinfo->n_fonts; ++i)
12550 if (dpyinfo->font_table[i].name == NULL)
12551 break;
12552
12553 /* If no free slot found, maybe enlarge the font table. */
12554 if (i == dpyinfo->n_fonts
12555 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12556 {
12557 int sz;
12558 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12559 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12560 dpyinfo->font_table
12561 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12562 }
12563
12564 fontp = dpyinfo->font_table + i;
12565 if (i == dpyinfo->n_fonts)
12566 ++dpyinfo->n_fonts;
12567
12568 /* Now fill in the slots of *FONTP. */
12569 BLOCK_INPUT;
12570 fontp->font = font;
12571 fontp->font_idx = i;
12572 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12573 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12574
12575 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12576 full_name = 0;
12577 if (XGetFontProperty (font, XA_FONT, &value))
12578 {
12579 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12580 char *p = name;
12581 int dashes = 0;
12582
12583 /* Count the number of dashes in the "full name".
12584 If it is too few, this isn't really the font's full name,
12585 so don't use it.
12586 In X11R4, the fonts did not come with their canonical names
12587 stored in them. */
12588 while (*p)
12589 {
12590 if (*p == '-')
12591 dashes++;
12592 p++;
12593 }
12594
12595 if (dashes >= 13)
12596 {
12597 full_name = (char *) xmalloc (p - name + 1);
12598 bcopy (name, full_name, p - name + 1);
12599 }
12600
12601 XFree (name);
12602 }
12603
12604 if (full_name != 0)
12605 fontp->full_name = full_name;
12606 else
12607 fontp->full_name = fontp->name;
12608
12609 fontp->size = font->max_bounds.width;
12610 fontp->height = FONT_HEIGHT (font);
12611 {
12612 /* For some font, ascent and descent in max_bounds field is
12613 larger than the above value. */
12614 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12615 if (max_height > fontp->height)
12616 fontp->height = max_height;
12617 }
12618
12619 if (NILP (font_names))
12620 {
12621 /* We come here because of a bug of XListFonts mentioned at
12622 the head of this block. Let's store this information in
12623 the cache for x_list_fonts. */
12624 Lisp_Object lispy_name = build_string (fontname);
12625 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12626
12627 XCDR (dpyinfo->name_list_element)
12628 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12629 Fcons (Fcons (lispy_full_name,
12630 make_number (fontp->size)),
12631 Qnil)),
12632 XCDR (dpyinfo->name_list_element));
12633 if (full_name)
12634 XCDR (dpyinfo->name_list_element)
12635 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12636 Fcons (Fcons (lispy_full_name,
12637 make_number (fontp->size)),
12638 Qnil)),
12639 XCDR (dpyinfo->name_list_element));
12640 }
12641
12642 /* The slot `encoding' specifies how to map a character
12643 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12644 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
12645 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12646 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12647 2:0xA020..0xFF7F). For the moment, we don't know which charset
12648 uses this font. So, we set information in fontp->encoding[1]
12649 which is never used by any charset. If mapping can't be
12650 decided, set FONT_ENCODING_NOT_DECIDED. */
12651 fontp->encoding[1]
12652 = (font->max_byte1 == 0
12653 /* 1-byte font */
12654 ? (font->min_char_or_byte2 < 0x80
12655 ? (font->max_char_or_byte2 < 0x80
12656 ? 0 /* 0x20..0x7F */
12657 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12658 : 1) /* 0xA0..0xFF */
12659 /* 2-byte font */
12660 : (font->min_byte1 < 0x80
12661 ? (font->max_byte1 < 0x80
12662 ? (font->min_char_or_byte2 < 0x80
12663 ? (font->max_char_or_byte2 < 0x80
12664 ? 0 /* 0x2020..0x7F7F */
12665 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
12666 : 3) /* 0x20A0..0x7FFF */
12667 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
12668 : (font->min_char_or_byte2 < 0x80
12669 ? (font->max_char_or_byte2 < 0x80
12670 ? 2 /* 0xA020..0xFF7F */
12671 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
12672 : 1))); /* 0xA0A0..0xFFFF */
12673
12674 fontp->baseline_offset
12675 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12676 ? (long) value : 0);
12677 fontp->relative_compose
12678 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12679 ? (long) value : 0);
12680 fontp->default_ascent
12681 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12682 ? (long) value : 0);
12683
12684 /* Set global flag fonts_changed_p to non-zero if the font loaded
12685 has a character with a smaller width than any other character
12686 before, or if the font loaded has a smalle>r height than any
12687 other font loaded before. If this happens, it will make a
12688 glyph matrix reallocation necessary. */
12689 fonts_changed_p = x_compute_min_glyph_bounds (f);
12690 UNBLOCK_INPUT;
12691 return fontp;
12692 }
12693 }
12694
12695
12696 /* Return a pointer to struct font_info of a font named FONTNAME for
12697 frame F. If no such font is loaded, return NULL. */
12698
12699 struct font_info *
12700 x_query_font (f, fontname)
12701 struct frame *f;
12702 register char *fontname;
12703 {
12704 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12705 int i;
12706
12707 for (i = 0; i < dpyinfo->n_fonts; i++)
12708 if (dpyinfo->font_table[i].name
12709 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12710 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
12711 return (dpyinfo->font_table + i);
12712 return NULL;
12713 }
12714
12715
12716 /* Find a CCL program for a font specified by FONTP, and set the member
12717 `encoder' of the structure. */
12718
12719 void
12720 x_find_ccl_program (fontp)
12721 struct font_info *fontp;
12722 {
12723 Lisp_Object list, elt;
12724
12725 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
12726 {
12727 elt = XCAR (list);
12728 if (CONSP (elt)
12729 && STRINGP (XCAR (elt))
12730 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
12731 >= 0))
12732 break;
12733 }
12734 if (! NILP (list))
12735 {
12736 struct ccl_program *ccl
12737 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
12738
12739 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
12740 xfree (ccl);
12741 else
12742 fontp->font_encoder = ccl;
12743 }
12744 }
12745
12746
12747 \f
12748 /***********************************************************************
12749 Initialization
12750 ***********************************************************************/
12751
12752 #ifdef USE_X_TOOLKIT
12753 static XrmOptionDescRec emacs_options[] = {
12754 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12755 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12756
12757 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12758 XrmoptionSepArg, NULL},
12759 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12760
12761 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12762 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12763 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12764 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12765 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12766 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12767 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12768 };
12769 #endif /* USE_X_TOOLKIT */
12770
12771 static int x_initialized;
12772
12773 #ifdef MULTI_KBOARD
12774 /* Test whether two display-name strings agree up to the dot that separates
12775 the screen number from the server number. */
12776 static int
12777 same_x_server (name1, name2)
12778 char *name1, *name2;
12779 {
12780 int seen_colon = 0;
12781 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12782 int system_name_length = strlen (system_name);
12783 int length_until_period = 0;
12784
12785 while (system_name[length_until_period] != 0
12786 && system_name[length_until_period] != '.')
12787 length_until_period++;
12788
12789 /* Treat `unix' like an empty host name. */
12790 if (! strncmp (name1, "unix:", 5))
12791 name1 += 4;
12792 if (! strncmp (name2, "unix:", 5))
12793 name2 += 4;
12794 /* Treat this host's name like an empty host name. */
12795 if (! strncmp (name1, system_name, system_name_length)
12796 && name1[system_name_length] == ':')
12797 name1 += system_name_length;
12798 if (! strncmp (name2, system_name, system_name_length)
12799 && name2[system_name_length] == ':')
12800 name2 += system_name_length;
12801 /* Treat this host's domainless name like an empty host name. */
12802 if (! strncmp (name1, system_name, length_until_period)
12803 && name1[length_until_period] == ':')
12804 name1 += length_until_period;
12805 if (! strncmp (name2, system_name, length_until_period)
12806 && name2[length_until_period] == ':')
12807 name2 += length_until_period;
12808
12809 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12810 {
12811 if (*name1 == ':')
12812 seen_colon++;
12813 if (seen_colon && *name1 == '.')
12814 return 1;
12815 }
12816 return (seen_colon
12817 && (*name1 == '.' || *name1 == '\0')
12818 && (*name2 == '.' || *name2 == '\0'));
12819 }
12820 #endif
12821
12822 struct x_display_info *
12823 x_term_init (display_name, xrm_option, resource_name)
12824 Lisp_Object display_name;
12825 char *xrm_option;
12826 char *resource_name;
12827 {
12828 int connection;
12829 Display *dpy;
12830 struct x_display_info *dpyinfo;
12831 XrmDatabase xrdb;
12832
12833 BLOCK_INPUT;
12834
12835 if (!x_initialized)
12836 {
12837 x_initialize ();
12838 x_initialized = 1;
12839 }
12840
12841 #ifdef USE_X_TOOLKIT
12842 /* weiner@footloose.sps.mot.com reports that this causes
12843 errors with X11R5:
12844 X protocol error: BadAtom (invalid Atom parameter)
12845 on protocol request 18skiloaf.
12846 So let's not use it until R6. */
12847 #ifdef HAVE_X11XTR6
12848 XtSetLanguageProc (NULL, NULL, NULL);
12849 #endif
12850
12851 {
12852 int argc = 0;
12853 char *argv[3];
12854
12855 argv[0] = "";
12856 argc = 1;
12857 if (xrm_option)
12858 {
12859 argv[argc++] = "-xrm";
12860 argv[argc++] = xrm_option;
12861 }
12862 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12863 resource_name, EMACS_CLASS,
12864 emacs_options, XtNumber (emacs_options),
12865 &argc, argv);
12866
12867 #ifdef HAVE_X11XTR6
12868 /* I think this is to compensate for XtSetLanguageProc. */
12869 fixup_locale ();
12870 #endif
12871 }
12872
12873 #else /* not USE_X_TOOLKIT */
12874 #ifdef HAVE_X11R5
12875 XSetLocaleModifiers ("");
12876 #endif
12877 dpy = XOpenDisplay (XSTRING (display_name)->data);
12878 #endif /* not USE_X_TOOLKIT */
12879
12880 /* Detect failure. */
12881 if (dpy == 0)
12882 {
12883 UNBLOCK_INPUT;
12884 return 0;
12885 }
12886
12887 /* We have definitely succeeded. Record the new connection. */
12888
12889 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12890
12891 #ifdef MULTI_KBOARD
12892 {
12893 struct x_display_info *share;
12894 Lisp_Object tail;
12895
12896 for (share = x_display_list, tail = x_display_name_list; share;
12897 share = share->next, tail = XCDR (tail))
12898 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
12899 XSTRING (display_name)->data))
12900 break;
12901 if (share)
12902 dpyinfo->kboard = share->kboard;
12903 else
12904 {
12905 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12906 init_kboard (dpyinfo->kboard);
12907 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12908 {
12909 char *vendor = ServerVendor (dpy);
12910 dpyinfo->kboard->Vsystem_key_alist
12911 = call1 (Qvendor_specific_keysyms,
12912 build_string (vendor ? vendor : ""));
12913 }
12914
12915 dpyinfo->kboard->next_kboard = all_kboards;
12916 all_kboards = dpyinfo->kboard;
12917 /* Don't let the initial kboard remain current longer than necessary.
12918 That would cause problems if a file loaded on startup tries to
12919 prompt in the mini-buffer. */
12920 if (current_kboard == initial_kboard)
12921 current_kboard = dpyinfo->kboard;
12922 }
12923 dpyinfo->kboard->reference_count++;
12924 }
12925 #endif
12926
12927 /* Put this display on the chain. */
12928 dpyinfo->next = x_display_list;
12929 x_display_list = dpyinfo;
12930
12931 /* Put it on x_display_name_list as well, to keep them parallel. */
12932 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12933 x_display_name_list);
12934 dpyinfo->name_list_element = XCAR (x_display_name_list);
12935
12936 dpyinfo->display = dpy;
12937
12938 #if 0
12939 XSetAfterFunction (x_current_display, x_trace_wire);
12940 #endif /* ! 0 */
12941
12942 dpyinfo->x_id_name
12943 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12944 + STRING_BYTES (XSTRING (Vsystem_name))
12945 + 2);
12946 sprintf (dpyinfo->x_id_name, "%s@%s",
12947 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
12948
12949 /* Figure out which modifier bits mean what. */
12950 x_find_modifier_meanings (dpyinfo);
12951
12952 /* Get the scroll bar cursor. */
12953 dpyinfo->vertical_scroll_bar_cursor
12954 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12955
12956 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12957 resource_name, EMACS_CLASS);
12958 #ifdef HAVE_XRMSETDATABASE
12959 XrmSetDatabase (dpyinfo->display, xrdb);
12960 #else
12961 dpyinfo->display->db = xrdb;
12962 #endif
12963 /* Put the rdb where we can find it in a way that works on
12964 all versions. */
12965 dpyinfo->xrdb = xrdb;
12966
12967 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12968 DefaultScreen (dpyinfo->display));
12969 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12970 &dpyinfo->n_planes);
12971 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12972 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12973 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12974 dpyinfo->grabbed = 0;
12975 dpyinfo->reference_count = 0;
12976 dpyinfo->icon_bitmap_id = -1;
12977 dpyinfo->font_table = NULL;
12978 dpyinfo->n_fonts = 0;
12979 dpyinfo->font_table_size = 0;
12980 dpyinfo->bitmaps = 0;
12981 dpyinfo->bitmaps_size = 0;
12982 dpyinfo->bitmaps_last = 0;
12983 dpyinfo->scratch_cursor_gc = 0;
12984 dpyinfo->mouse_face_mouse_frame = 0;
12985 dpyinfo->mouse_face_deferred_gc = 0;
12986 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12987 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
12988 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
12989 dpyinfo->mouse_face_window = Qnil;
12990 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12991 dpyinfo->mouse_face_defer = 0;
12992 dpyinfo->x_focus_frame = 0;
12993 dpyinfo->x_focus_event_frame = 0;
12994 dpyinfo->x_highlight_frame = 0;
12995 dpyinfo->image_cache = make_image_cache ();
12996
12997 {
12998 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12999 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13000 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13001 dpyinfo->resy = pixels * 25.4 / mm;
13002 pixels = DisplayWidth (dpyinfo->display, screen_number);
13003 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13004 dpyinfo->resx = pixels * 25.4 / mm;
13005 }
13006
13007 dpyinfo->Xatom_wm_protocols
13008 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13009 dpyinfo->Xatom_wm_take_focus
13010 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13011 dpyinfo->Xatom_wm_save_yourself
13012 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13013 dpyinfo->Xatom_wm_delete_window
13014 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13015 dpyinfo->Xatom_wm_change_state
13016 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13017 dpyinfo->Xatom_wm_configure_denied
13018 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13019 dpyinfo->Xatom_wm_window_moved
13020 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13021 dpyinfo->Xatom_editres
13022 = XInternAtom (dpyinfo->display, "Editres", False);
13023 dpyinfo->Xatom_CLIPBOARD
13024 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13025 dpyinfo->Xatom_TIMESTAMP
13026 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13027 dpyinfo->Xatom_TEXT
13028 = XInternAtom (dpyinfo->display, "TEXT", False);
13029 dpyinfo->Xatom_COMPOUND_TEXT
13030 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13031 dpyinfo->Xatom_DELETE
13032 = XInternAtom (dpyinfo->display, "DELETE", False);
13033 dpyinfo->Xatom_MULTIPLE
13034 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13035 dpyinfo->Xatom_INCR
13036 = XInternAtom (dpyinfo->display, "INCR", False);
13037 dpyinfo->Xatom_EMACS_TMP
13038 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13039 dpyinfo->Xatom_TARGETS
13040 = XInternAtom (dpyinfo->display, "TARGETS", False);
13041 dpyinfo->Xatom_NULL
13042 = XInternAtom (dpyinfo->display, "NULL", False);
13043 dpyinfo->Xatom_ATOM_PAIR
13044 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13045 /* For properties of font. */
13046 dpyinfo->Xatom_PIXEL_SIZE
13047 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13048 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13049 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13050 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13051 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13052 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13053 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13054
13055 /* Ghostscript support. */
13056 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13057 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13058
13059 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13060 False);
13061
13062 dpyinfo->cut_buffers_initialized = 0;
13063
13064 connection = ConnectionNumber (dpyinfo->display);
13065 dpyinfo->connection = connection;
13066
13067 {
13068 char null_bits[1];
13069
13070 null_bits[0] = 0x00;
13071
13072 dpyinfo->null_pixel
13073 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13074 null_bits, 1, 1, (long) 0, (long) 0,
13075 1);
13076 }
13077
13078 {
13079 extern int gray_bitmap_width, gray_bitmap_height;
13080 extern unsigned char *gray_bitmap_bits;
13081 dpyinfo->gray
13082 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13083 gray_bitmap_bits,
13084 gray_bitmap_width, gray_bitmap_height,
13085 (unsigned long) 1, (unsigned long) 0, 1);
13086 }
13087
13088 #ifdef HAVE_X_I18N
13089 xim_initialize (dpyinfo, resource_name);
13090 #endif
13091
13092 #ifdef subprocesses
13093 /* This is only needed for distinguishing keyboard and process input. */
13094 if (connection != 0)
13095 add_keyboard_wait_descriptor (connection);
13096 #endif
13097
13098 #ifndef F_SETOWN_BUG
13099 #ifdef F_SETOWN
13100 #ifdef F_SETOWN_SOCK_NEG
13101 /* stdin is a socket here */
13102 fcntl (connection, F_SETOWN, -getpid ());
13103 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13104 fcntl (connection, F_SETOWN, getpid ());
13105 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13106 #endif /* ! defined (F_SETOWN) */
13107 #endif /* F_SETOWN_BUG */
13108
13109 #ifdef SIGIO
13110 if (interrupt_input)
13111 init_sigio (connection);
13112 #endif /* ! defined (SIGIO) */
13113
13114 #ifdef USE_LUCID
13115 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13116 /* Make sure that we have a valid font for dialog boxes
13117 so that Xt does not crash. */
13118 {
13119 Display *dpy = dpyinfo->display;
13120 XrmValue d, fr, to;
13121 Font font;
13122 int count;
13123
13124 d.addr = (XPointer)&dpy;
13125 d.size = sizeof (Display *);
13126 fr.addr = XtDefaultFont;
13127 fr.size = sizeof (XtDefaultFont);
13128 to.size = sizeof (Font *);
13129 to.addr = (XPointer)&font;
13130 count = x_catch_errors (dpy);
13131 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13132 abort ();
13133 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13134 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13135 x_uncatch_errors (dpy, count);
13136 }
13137 #endif
13138 #endif
13139
13140 UNBLOCK_INPUT;
13141
13142 return dpyinfo;
13143 }
13144 \f
13145 /* Get rid of display DPYINFO, assuming all frames are already gone,
13146 and without sending any more commands to the X server. */
13147
13148 void
13149 x_delete_display (dpyinfo)
13150 struct x_display_info *dpyinfo;
13151 {
13152 delete_keyboard_wait_descriptor (dpyinfo->connection);
13153
13154 /* Discard this display from x_display_name_list and x_display_list.
13155 We can't use Fdelq because that can quit. */
13156 if (! NILP (x_display_name_list)
13157 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13158 x_display_name_list = XCDR (x_display_name_list);
13159 else
13160 {
13161 Lisp_Object tail;
13162
13163 tail = x_display_name_list;
13164 while (CONSP (tail) && CONSP (XCDR (tail)))
13165 {
13166 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13167 {
13168 XCDR (tail) = XCDR (XCDR (tail));
13169 break;
13170 }
13171 tail = XCDR (tail);
13172 }
13173 }
13174
13175 if (next_noop_dpyinfo == dpyinfo)
13176 next_noop_dpyinfo = dpyinfo->next;
13177
13178 if (x_display_list == dpyinfo)
13179 x_display_list = dpyinfo->next;
13180 else
13181 {
13182 struct x_display_info *tail;
13183
13184 for (tail = x_display_list; tail; tail = tail->next)
13185 if (tail->next == dpyinfo)
13186 tail->next = tail->next->next;
13187 }
13188
13189 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13190 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13191 XrmDestroyDatabase (dpyinfo->xrdb);
13192 #endif
13193 #endif
13194 #ifdef MULTI_KBOARD
13195 if (--dpyinfo->kboard->reference_count == 0)
13196 delete_kboard (dpyinfo->kboard);
13197 #endif
13198 #ifdef HAVE_X_I18N
13199 if (dpyinfo->xim)
13200 xim_close_dpy (dpyinfo);
13201 #endif
13202
13203 xfree (dpyinfo->font_table);
13204 xfree (dpyinfo->x_id_name);
13205 xfree (dpyinfo);
13206 }
13207 \f
13208 /* Set up use of X before we make the first connection. */
13209
13210 static struct redisplay_interface x_redisplay_interface =
13211 {
13212 x_produce_glyphs,
13213 x_write_glyphs,
13214 x_insert_glyphs,
13215 x_clear_end_of_line,
13216 x_scroll_run,
13217 x_after_update_window_line,
13218 x_update_window_begin,
13219 x_update_window_end,
13220 XTcursor_to,
13221 x_flush,
13222 x_get_glyph_overhangs,
13223 x_fix_overlapping_area
13224 };
13225
13226 void
13227 x_initialize ()
13228 {
13229 rif = &x_redisplay_interface;
13230
13231 clear_frame_hook = x_clear_frame;
13232 ins_del_lines_hook = x_ins_del_lines;
13233 change_line_highlight_hook = x_change_line_highlight;
13234 delete_glyphs_hook = x_delete_glyphs;
13235 ring_bell_hook = XTring_bell;
13236 reset_terminal_modes_hook = XTreset_terminal_modes;
13237 set_terminal_modes_hook = XTset_terminal_modes;
13238 update_begin_hook = x_update_begin;
13239 update_end_hook = x_update_end;
13240 set_terminal_window_hook = XTset_terminal_window;
13241 read_socket_hook = XTread_socket;
13242 frame_up_to_date_hook = XTframe_up_to_date;
13243 reassert_line_highlight_hook = XTreassert_line_highlight;
13244 mouse_position_hook = XTmouse_position;
13245 frame_rehighlight_hook = XTframe_rehighlight;
13246 frame_raise_lower_hook = XTframe_raise_lower;
13247 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13248 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13249 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13250 judge_scroll_bars_hook = XTjudge_scroll_bars;
13251 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13252
13253 scroll_region_ok = 1; /* we'll scroll partial frames */
13254 char_ins_del_ok = 0; /* just as fast to write the line */
13255 line_ins_del_ok = 1; /* we'll just blt 'em */
13256 fast_clear_end_of_line = 1; /* X does this well */
13257 memory_below_frame = 0; /* we don't remember what scrolls
13258 off the bottom */
13259 baud_rate = 19200;
13260
13261 x_noop_count = 0;
13262 last_tool_bar_item = -1;
13263 any_help_event_p = 0;
13264
13265 /* Try to use interrupt input; if we can't, then start polling. */
13266 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13267
13268 #ifdef USE_X_TOOLKIT
13269 XtToolkitInitialize ();
13270 Xt_app_con = XtCreateApplicationContext ();
13271 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13272
13273 /* Install an asynchronous timer that processes Xt timeout events
13274 every 0.1s. This is necessary because some widget sets use
13275 timeouts internally, for example the LessTif menu bar, or the
13276 Xaw3d scroll bar. When Xt timouts aren't processed, these
13277 widgets don't behave normally. */
13278 {
13279 EMACS_TIME interval;
13280 EMACS_SET_SECS_USECS (interval, 0, 100000);
13281 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13282 }
13283 #endif
13284
13285 #if USE_TOOLKIT_SCROLL_BARS
13286 xaw3d_arrow_scroll = False;
13287 xaw3d_pick_top = True;
13288 #endif
13289
13290 /* Note that there is no real way portable across R3/R4 to get the
13291 original error handler. */
13292 XSetErrorHandler (x_error_handler);
13293 XSetIOErrorHandler (x_io_error_quitter);
13294
13295 /* Disable Window Change signals; they are handled by X events. */
13296 #ifdef SIGWINCH
13297 signal (SIGWINCH, SIG_DFL);
13298 #endif /* ! defined (SIGWINCH) */
13299
13300 signal (SIGPIPE, x_connection_signal);
13301 }
13302
13303
13304 void
13305 syms_of_xterm ()
13306 {
13307 staticpro (&x_error_message_string);
13308 x_error_message_string = Qnil;
13309
13310 staticpro (&x_display_name_list);
13311 x_display_name_list = Qnil;
13312
13313 staticpro (&last_mouse_scroll_bar);
13314 last_mouse_scroll_bar = Qnil;
13315
13316 staticpro (&Qvendor_specific_keysyms);
13317 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13318
13319 staticpro (&last_mouse_press_frame);
13320 last_mouse_press_frame = Qnil;
13321
13322 staticpro (&help_echo);
13323 help_echo = Qnil;
13324 staticpro (&previous_help_echo);
13325 previous_help_echo = Qnil;
13326
13327 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13328 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13329 For example, if a block cursor is over a tab, it will be drawn as\n\
13330 wide as that tab on the display.");
13331 x_stretch_cursor_p = 0;
13332
13333 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13334 "If not nil, Emacs uses toolkit scroll bars.");
13335 #if USE_TOOLKIT_SCROLL_BARS
13336 x_toolkit_scroll_bars_p = 1;
13337 #else
13338 x_toolkit_scroll_bars_p = 0;
13339 #endif
13340
13341 staticpro (&last_mouse_motion_frame);
13342 last_mouse_motion_frame = Qnil;
13343 }
13344
13345 #endif /* not HAVE_X_WINDOWS */
13346