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