(mark_object) [GC_CHECK_MARKED_OBJECTS]: Check that no
[bpt/emacs.git] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 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 note_mouse_highlight P_ ((struct frame *, int, int));
416 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
417 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
418 static void show_mouse_face P_ ((struct x_display_info *,
419 enum draw_glyphs_face));
420 static int x_io_error_quitter P_ ((Display *));
421 int x_catch_errors P_ ((Display *));
422 void x_uncatch_errors P_ ((Display *, int));
423 void x_lower_frame P_ ((struct frame *));
424 void x_scroll_bar_clear P_ ((struct frame *));
425 int x_had_errors_p P_ ((Display *));
426 void x_wm_set_size_hint P_ ((struct frame *, long, int));
427 void x_raise_frame P_ ((struct frame *));
428 void x_set_window_size P_ ((struct frame *, int, int, int));
429 void x_wm_set_window_state P_ ((struct frame *, int));
430 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
431 void x_initialize P_ ((void));
432 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
433 static int x_compute_min_glyph_bounds P_ ((struct frame *));
434 static void x_draw_phys_cursor_glyph P_ ((struct window *,
435 struct glyph_row *,
436 enum draw_glyphs_face));
437 static void x_update_end P_ ((struct frame *));
438 static void XTframe_up_to_date P_ ((struct frame *));
439 static void XTreassert_line_highlight P_ ((int, int));
440 static void x_change_line_highlight P_ ((int, int, int, int));
441 static void XTset_terminal_modes P_ ((void));
442 static void XTreset_terminal_modes P_ ((void));
443 static void XTcursor_to P_ ((int, int, int, int));
444 static void x_write_glyphs P_ ((struct glyph *, int));
445 static void x_clear_end_of_line P_ ((int));
446 static void x_clear_frame P_ ((void));
447 static void x_clear_cursor P_ ((struct window *));
448 static void frame_highlight P_ ((struct frame *));
449 static void frame_unhighlight P_ ((struct frame *));
450 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
451 static void XTframe_rehighlight P_ ((struct frame *));
452 static void x_frame_rehighlight P_ ((struct x_display_info *));
453 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
454 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
455 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
456 XRectangle *));
457 static void expose_frame P_ ((struct frame *, int, int, int, int));
458 static void expose_window_tree P_ ((struct window *, XRectangle *));
459 static void expose_window P_ ((struct window *, XRectangle *));
460 static void expose_area P_ ((struct window *, struct glyph_row *,
461 XRectangle *, enum glyph_row_area));
462 static void expose_line P_ ((struct window *, struct glyph_row *,
463 XRectangle *));
464 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
465 static void x_update_window_cursor P_ ((struct window *, int));
466 static void x_erase_phys_cursor P_ ((struct window *));
467 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
468 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
469 enum bitmap_type));
470
471 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
472 GC, int));
473 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
474 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
475 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
476 static void x_flush P_ ((struct frame *f));
477
478
479 /* Flush display of frame F, or of all frames if F is null. */
480
481 static void
482 x_flush (f)
483 struct frame *f;
484 {
485 BLOCK_INPUT;
486 if (f == NULL)
487 {
488 Lisp_Object rest, frame;
489 FOR_EACH_FRAME (rest, frame)
490 x_flush (XFRAME (frame));
491 }
492 else if (FRAME_X_P (f))
493 XFlush (FRAME_X_DISPLAY (f));
494 UNBLOCK_INPUT;
495 }
496
497
498 /* Remove calls to XFlush by defining XFlush to an empty replacement.
499 Calls to XFlush should be unnecessary because the X output buffer
500 is flushed automatically as needed by calls to XPending,
501 XNextEvent, or XWindowEvent according to the XFlush man page.
502 XTread_socket calls XPending. Removing XFlush improves
503 performance. */
504
505 #define XFlush(DISPLAY) (void) 0
506
507 \f
508 /***********************************************************************
509 Debugging
510 ***********************************************************************/
511
512 #if 0
513
514 /* This is a function useful for recording debugging information about
515 the sequence of occurrences in this file. */
516
517 struct record
518 {
519 char *locus;
520 int type;
521 };
522
523 struct record event_record[100];
524
525 int event_record_index;
526
527 record_event (locus, type)
528 char *locus;
529 int type;
530 {
531 if (event_record_index == sizeof (event_record) / sizeof (struct record))
532 event_record_index = 0;
533
534 event_record[event_record_index].locus = locus;
535 event_record[event_record_index].type = type;
536 event_record_index++;
537 }
538
539 #endif /* 0 */
540
541
542 \f
543 /* Return the struct x_display_info corresponding to DPY. */
544
545 struct x_display_info *
546 x_display_info_for_display (dpy)
547 Display *dpy;
548 {
549 struct x_display_info *dpyinfo;
550
551 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
552 if (dpyinfo->display == dpy)
553 return dpyinfo;
554
555 return 0;
556 }
557
558
559 \f
560 /***********************************************************************
561 Starting and ending an update
562 ***********************************************************************/
563
564 /* Start an update of frame F. This function is installed as a hook
565 for update_begin, i.e. it is called when update_begin is called.
566 This function is called prior to calls to x_update_window_begin for
567 each window being updated. Currently, there is nothing to do here
568 because all interesting stuff is done on a window basis. */
569
570 static void
571 x_update_begin (f)
572 struct frame *f;
573 {
574 /* Nothing to do. */
575 }
576
577
578 /* Start update of window W. Set the global variable updated_window
579 to the window being updated and set output_cursor to the cursor
580 position of W. */
581
582 static void
583 x_update_window_begin (w)
584 struct window *w;
585 {
586 struct frame *f = XFRAME (WINDOW_FRAME (w));
587 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
588
589 updated_window = w;
590 set_output_cursor (&w->cursor);
591
592 BLOCK_INPUT;
593
594 if (f == display_info->mouse_face_mouse_frame)
595 {
596 /* Don't do highlighting for mouse motion during the update. */
597 display_info->mouse_face_defer = 1;
598
599 /* If F needs to be redrawn, simply forget about any prior mouse
600 highlighting. */
601 if (FRAME_GARBAGED_P (f))
602 display_info->mouse_face_window = Qnil;
603
604 /* Can we tell that this update does not affect the window
605 where the mouse highlight is? If so, no need to turn off.
606 Likewise, don't do anything if the frame is garbaged;
607 in that case, the frame's current matrix that we would use
608 is all wrong, and we will redisplay that line anyway. */
609 if (!NILP (display_info->mouse_face_window)
610 && w == XWINDOW (display_info->mouse_face_window))
611 {
612 int i;
613
614 for (i = 0; i < w->desired_matrix->nrows; ++i)
615 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
616 break;
617
618 if (i < w->desired_matrix->nrows)
619 clear_mouse_face (display_info);
620 }
621 }
622
623 UNBLOCK_INPUT;
624 }
625
626
627 /* Draw a vertical window border to the right of window W if W doesn't
628 have vertical scroll bars. */
629
630 static void
631 x_draw_vertical_border (w)
632 struct window *w;
633 {
634 struct frame *f = XFRAME (WINDOW_FRAME (w));
635
636 /* Redraw borders between horizontally adjacent windows. Don't
637 do it for frames with vertical scroll bars because either the
638 right scroll bar of a window, or the left scroll bar of its
639 neighbor will suffice as a border. */
640 if (!WINDOW_RIGHTMOST_P (w)
641 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
642 {
643 int x0, x1, y0, y1;
644
645 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
646 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
647 y1 -= 1;
648
649 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
650 f->output_data.x->normal_gc, x1, y0, x1, y1);
651 }
652 }
653
654
655 /* End update of window W (which is equal to updated_window). Draw
656 vertical borders between horizontally adjacent windows, and display
657 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
658 pseudo-window in case we don't have X toolkit support. Such
659 windows don't have a cursor, so don't display it here. */
660
661 static void
662 x_update_window_end (w, cursor_on_p)
663 struct window *w;
664 int cursor_on_p;
665 {
666 if (!w->pseudo_window_p)
667 {
668 BLOCK_INPUT;
669 if (cursor_on_p)
670 x_display_and_set_cursor (w, 1, output_cursor.hpos,
671 output_cursor.vpos,
672 output_cursor.x, output_cursor.y);
673 x_draw_vertical_border (w);
674 UNBLOCK_INPUT;
675 }
676
677 updated_window = NULL;
678 }
679
680
681 /* End update of frame F. This function is installed as a hook in
682 update_end. */
683
684 static void
685 x_update_end (f)
686 struct frame *f;
687 {
688 /* Mouse highlight may be displayed again. */
689 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
690
691 BLOCK_INPUT;
692 XFlush (FRAME_X_DISPLAY (f));
693 UNBLOCK_INPUT;
694 }
695
696
697 /* This function is called from various places in xdisp.c whenever a
698 complete update has been performed. The global variable
699 updated_window is not available here. */
700
701 static void
702 XTframe_up_to_date (f)
703 struct frame *f;
704 {
705 if (FRAME_X_P (f))
706 {
707 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
708 if (dpyinfo->mouse_face_deferred_gc
709 || f == dpyinfo->mouse_face_mouse_frame)
710 {
711 BLOCK_INPUT;
712 if (dpyinfo->mouse_face_mouse_frame)
713 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
714 dpyinfo->mouse_face_mouse_x,
715 dpyinfo->mouse_face_mouse_y);
716 dpyinfo->mouse_face_deferred_gc = 0;
717 UNBLOCK_INPUT;
718 }
719 }
720 }
721
722
723 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
724 arrow bitmaps, or clear the areas where they would be displayed
725 before DESIRED_ROW is made current. The window being updated is
726 found in updated_window. This function It is called from
727 update_window_line only if it is known that there are differences
728 between bitmaps to be drawn between current row and DESIRED_ROW. */
729
730 static void
731 x_after_update_window_line (desired_row)
732 struct glyph_row *desired_row;
733 {
734 struct window *w = updated_window;
735
736 xassert (w);
737
738 if (!desired_row->mode_line_p && !w->pseudo_window_p)
739 {
740 BLOCK_INPUT;
741 x_draw_row_bitmaps (w, desired_row);
742
743 /* When a window has disappeared, make sure that no rest of
744 full-width rows stays visible in the internal border. */
745 if (windows_or_buffers_changed)
746 {
747 struct frame *f = XFRAME (w->frame);
748 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
749 int height = desired_row->visible_height;
750 int x = (window_box_right (w, -1)
751 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
752 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
753
754 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
755 x, y, width, height, False);
756 }
757
758 UNBLOCK_INPUT;
759 }
760 }
761
762
763 /* Draw the bitmap WHICH in one of the areas to the left or right of
764 window W. ROW is the glyph row for which to display the bitmap; it
765 determines the vertical position at which the bitmap has to be
766 drawn. */
767
768 static void
769 x_draw_bitmap (w, row, which)
770 struct window *w;
771 struct glyph_row *row;
772 enum bitmap_type which;
773 {
774 struct frame *f = XFRAME (WINDOW_FRAME (w));
775 Display *display = FRAME_X_DISPLAY (f);
776 Window window = FRAME_X_WINDOW (f);
777 int x, y, wd, h, dy;
778 unsigned char *bits;
779 Pixmap pixmap;
780 GC gc = f->output_data.x->normal_gc;
781 struct face *face;
782 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
783
784 /* Must clip because of partially visible lines. */
785 x_clip_to_row (w, row, gc, 1);
786
787 switch (which)
788 {
789 case LEFT_TRUNCATION_BITMAP:
790 wd = left_width;
791 h = left_height;
792 bits = left_bits;
793 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
794 - wd
795 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
796 break;
797
798 case OVERLAY_ARROW_BITMAP:
799 wd = left_width;
800 h = left_height;
801 bits = ov_bits;
802 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
803 - wd
804 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
805 break;
806
807 case RIGHT_TRUNCATION_BITMAP:
808 wd = right_width;
809 h = right_height;
810 bits = right_bits;
811 x = window_box_right (w, -1);
812 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
813 break;
814
815 case CONTINUED_LINE_BITMAP:
816 wd = right_width;
817 h = right_height;
818 bits = continued_bits;
819 x = window_box_right (w, -1);
820 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
821 break;
822
823 case CONTINUATION_LINE_BITMAP:
824 wd = continuation_width;
825 h = continuation_height;
826 bits = continuation_bits;
827 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
828 - wd
829 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
830 break;
831
832 case ZV_LINE_BITMAP:
833 wd = zv_width;
834 h = zv_height;
835 bits = zv_bits;
836 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
837 - wd
838 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
839 break;
840
841 default:
842 abort ();
843 }
844
845 /* Convert to frame coordinates. Set dy to the offset in the row to
846 start drawing the bitmap. */
847 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
848 dy = (row->height - h) / 2;
849
850 /* Draw the bitmap. I believe these small pixmaps can be cached
851 by the server. */
852 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
853 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
854 face->foreground,
855 face->background, depth);
856 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
857 XFreePixmap (display, pixmap);
858 XSetClipMask (display, gc, None);
859 }
860
861
862 /* Draw flags bitmaps for glyph row ROW on window W. Call this
863 function with input blocked. */
864
865 static void
866 x_draw_row_bitmaps (w, row)
867 struct window *w;
868 struct glyph_row *row;
869 {
870 struct frame *f = XFRAME (w->frame);
871 enum bitmap_type bitmap;
872 struct face *face;
873 int header_line_height = -1;
874
875 xassert (interrupt_input_blocked);
876
877 /* If row is completely invisible, because of vscrolling, we
878 don't have to draw anything. */
879 if (row->visible_height <= 0)
880 return;
881
882 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
883 PREPARE_FACE_FOR_DISPLAY (f, face);
884
885 /* Decide which bitmap to draw at the left side. */
886 if (row->overlay_arrow_p)
887 bitmap = OVERLAY_ARROW_BITMAP;
888 else if (row->truncated_on_left_p)
889 bitmap = LEFT_TRUNCATION_BITMAP;
890 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
891 bitmap = CONTINUATION_LINE_BITMAP;
892 else if (row->indicate_empty_line_p)
893 bitmap = ZV_LINE_BITMAP;
894 else
895 bitmap = NO_BITMAP;
896
897 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
898 the flags area. */
899 if (bitmap == NO_BITMAP
900 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
901 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
902 {
903 /* If W has a vertical border to its left, don't draw over it. */
904 int border = ((XFASTINT (w->left) > 0
905 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
906 ? 1 : 0);
907 int left = window_box_left (w, -1);
908
909 if (header_line_height < 0)
910 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
911
912 /* In case the same realized face is used for bitmap areas and
913 for something displayed in the text (e.g. face `region' on
914 mono-displays, the fill style may have been changed to
915 FillSolid in x_draw_glyph_string_background. */
916 if (face->stipple)
917 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
918 else
919 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
920
921 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
922 face->gc,
923 (left
924 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
925 + border),
926 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
927 row->y)),
928 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
929 row->visible_height);
930 if (!face->stipple)
931 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
932 }
933
934 /* Draw the left bitmap. */
935 if (bitmap != NO_BITMAP)
936 x_draw_bitmap (w, row, bitmap);
937
938 /* Decide which bitmap to draw at the right side. */
939 if (row->truncated_on_right_p)
940 bitmap = RIGHT_TRUNCATION_BITMAP;
941 else if (row->continued_p)
942 bitmap = CONTINUED_LINE_BITMAP;
943 else
944 bitmap = NO_BITMAP;
945
946 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
947 the flags area. */
948 if (bitmap == NO_BITMAP
949 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
950 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
951 {
952 int right = window_box_right (w, -1);
953
954 if (header_line_height < 0)
955 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
956
957 /* In case the same realized face is used for bitmap areas and
958 for something displayed in the text (e.g. face `region' on
959 mono-displays, the fill style may have been changed to
960 FillSolid in x_draw_glyph_string_background. */
961 if (face->stipple)
962 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
963 else
964 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
965 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
966 face->gc,
967 right,
968 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
969 row->y)),
970 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
971 row->visible_height);
972 if (!face->stipple)
973 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
974 }
975
976 /* Draw the right bitmap. */
977 if (bitmap != NO_BITMAP)
978 x_draw_bitmap (w, row, bitmap);
979 }
980
981 \f
982 /***********************************************************************
983 Line Highlighting
984 ***********************************************************************/
985
986 /* External interface to control of standout mode. Not used for X
987 frames. Aborts when called. */
988
989 static void
990 XTreassert_line_highlight (new, vpos)
991 int new, vpos;
992 {
993 abort ();
994 }
995
996
997 /* Call this when about to modify line at position VPOS and change
998 whether it is highlighted. Not used for X frames. Aborts when
999 called. */
1000
1001 static void
1002 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1003 int new_highlight, vpos, y, first_unused_hpos;
1004 {
1005 abort ();
1006 }
1007
1008
1009 /* This is called when starting Emacs and when restarting after
1010 suspend. When starting Emacs, no X window is mapped. And nothing
1011 must be done to Emacs's own window if it is suspended (though that
1012 rarely happens). */
1013
1014 static void
1015 XTset_terminal_modes ()
1016 {
1017 }
1018
1019 /* This is called when exiting or suspending Emacs. Exiting will make
1020 the X-windows go away, and suspending requires no action. */
1021
1022 static void
1023 XTreset_terminal_modes ()
1024 {
1025 }
1026
1027
1028 \f
1029 /***********************************************************************
1030 Output Cursor
1031 ***********************************************************************/
1032
1033 /* Set the global variable output_cursor to CURSOR. All cursor
1034 positions are relative to updated_window. */
1035
1036 static void
1037 set_output_cursor (cursor)
1038 struct cursor_pos *cursor;
1039 {
1040 output_cursor.hpos = cursor->hpos;
1041 output_cursor.vpos = cursor->vpos;
1042 output_cursor.x = cursor->x;
1043 output_cursor.y = cursor->y;
1044 }
1045
1046
1047 /* Set a nominal cursor position.
1048
1049 HPOS and VPOS are column/row positions in a window glyph matrix. X
1050 and Y are window text area relative pixel positions.
1051
1052 If this is done during an update, updated_window will contain the
1053 window that is being updated and the position is the future output
1054 cursor position for that window. If updated_window is null, use
1055 selected_window and display the cursor at the given position. */
1056
1057 static void
1058 XTcursor_to (vpos, hpos, y, x)
1059 int vpos, hpos, y, x;
1060 {
1061 struct window *w;
1062
1063 /* If updated_window is not set, work on selected_window. */
1064 if (updated_window)
1065 w = updated_window;
1066 else
1067 w = XWINDOW (selected_window);
1068
1069 /* Set the output cursor. */
1070 output_cursor.hpos = hpos;
1071 output_cursor.vpos = vpos;
1072 output_cursor.x = x;
1073 output_cursor.y = y;
1074
1075 /* If not called as part of an update, really display the cursor.
1076 This will also set the cursor position of W. */
1077 if (updated_window == NULL)
1078 {
1079 BLOCK_INPUT;
1080 x_display_cursor (w, 1, hpos, vpos, x, y);
1081 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1082 UNBLOCK_INPUT;
1083 }
1084 }
1085
1086
1087 \f
1088 /***********************************************************************
1089 Display Iterator
1090 ***********************************************************************/
1091
1092 /* Function prototypes of this page. */
1093
1094 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1095 struct glyph *,
1096 XChar2b *,
1097 int *));
1098 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1099 int, XChar2b *, int));
1100 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1101 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1102 static void x_append_glyph P_ ((struct it *));
1103 static void x_append_composite_glyph P_ ((struct it *));
1104 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1105 int, int, double));
1106 static void x_produce_glyphs P_ ((struct it *));
1107 static void x_produce_image_glyph P_ ((struct it *it));
1108
1109
1110 /* Return a pointer to per-char metric information in FONT of a
1111 character pointed by B which is a pointer to an XChar2b. */
1112
1113 #define PER_CHAR_METRIC(font, b) \
1114 ((font)->per_char \
1115 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1116 + (((font)->min_byte1 || (font)->max_byte1) \
1117 ? (((b)->byte1 - (font)->min_byte1) \
1118 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1119 : 0)) \
1120 : &((font)->max_bounds))
1121
1122
1123 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1124 is not contained in the font. */
1125
1126 static INLINE XCharStruct *
1127 x_per_char_metric (font, char2b)
1128 XFontStruct *font;
1129 XChar2b *char2b;
1130 {
1131 /* The result metric information. */
1132 XCharStruct *pcm = NULL;
1133
1134 xassert (font && char2b);
1135
1136 if (font->per_char != NULL)
1137 {
1138 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1139 {
1140 /* min_char_or_byte2 specifies the linear character index
1141 corresponding to the first element of the per_char array,
1142 max_char_or_byte2 is the index of the last character. A
1143 character with non-zero CHAR2B->byte1 is not in the font.
1144 A character with byte2 less than min_char_or_byte2 or
1145 greater max_char_or_byte2 is not in the font. */
1146 if (char2b->byte1 == 0
1147 && char2b->byte2 >= font->min_char_or_byte2
1148 && char2b->byte2 <= font->max_char_or_byte2)
1149 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1150 }
1151 else
1152 {
1153 /* If either min_byte1 or max_byte1 are nonzero, both
1154 min_char_or_byte2 and max_char_or_byte2 are less than
1155 256, and the 2-byte character index values corresponding
1156 to the per_char array element N (counting from 0) are:
1157
1158 byte1 = N/D + min_byte1
1159 byte2 = N\D + min_char_or_byte2
1160
1161 where:
1162
1163 D = max_char_or_byte2 - min_char_or_byte2 + 1
1164 / = integer division
1165 \ = integer modulus */
1166 if (char2b->byte1 >= font->min_byte1
1167 && char2b->byte1 <= font->max_byte1
1168 && char2b->byte2 >= font->min_char_or_byte2
1169 && char2b->byte2 <= font->max_char_or_byte2)
1170 {
1171 pcm = (font->per_char
1172 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1173 * (char2b->byte1 - font->min_byte1))
1174 + (char2b->byte2 - font->min_char_or_byte2));
1175 }
1176 }
1177 }
1178 else
1179 {
1180 /* If the per_char pointer is null, all glyphs between the first
1181 and last character indexes inclusive have the same
1182 information, as given by both min_bounds and max_bounds. */
1183 if (char2b->byte2 >= font->min_char_or_byte2
1184 && char2b->byte2 <= font->max_char_or_byte2)
1185 pcm = &font->max_bounds;
1186 }
1187
1188 return ((pcm == NULL
1189 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1190 ? NULL : pcm);
1191 }
1192
1193
1194 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1195 the two-byte form of C. Encoding is returned in *CHAR2B. */
1196
1197 static INLINE void
1198 x_encode_char (c, char2b, font_info)
1199 int c;
1200 XChar2b *char2b;
1201 struct font_info *font_info;
1202 {
1203 int charset = CHAR_CHARSET (c);
1204 XFontStruct *font = font_info->font;
1205
1206 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1207 This may be either a program in a special encoder language or a
1208 fixed encoding. */
1209 if (font_info->font_encoder)
1210 {
1211 /* It's a program. */
1212 struct ccl_program *ccl = font_info->font_encoder;
1213
1214 if (CHARSET_DIMENSION (charset) == 1)
1215 {
1216 ccl->reg[0] = charset;
1217 ccl->reg[1] = char2b->byte2;
1218 }
1219 else
1220 {
1221 ccl->reg[0] = charset;
1222 ccl->reg[1] = char2b->byte1;
1223 ccl->reg[2] = char2b->byte2;
1224 }
1225
1226 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1227
1228 /* We assume that MSBs are appropriately set/reset by CCL
1229 program. */
1230 if (font->max_byte1 == 0) /* 1-byte font */
1231 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1232 else
1233 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1234 }
1235 else if (font_info->encoding[charset])
1236 {
1237 /* Fixed encoding scheme. See fontset.h for the meaning of the
1238 encoding numbers. */
1239 int enc = font_info->encoding[charset];
1240
1241 if ((enc == 1 || enc == 2)
1242 && CHARSET_DIMENSION (charset) == 2)
1243 char2b->byte1 |= 0x80;
1244
1245 if (enc == 1 || enc == 3)
1246 char2b->byte2 |= 0x80;
1247 }
1248 }
1249
1250
1251 /* Get face and two-byte form of character C in face FACE_ID on frame
1252 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1253 means we want to display multibyte text. Value is a pointer to a
1254 realized face that is ready for display. */
1255
1256 static INLINE struct face *
1257 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1258 struct frame *f;
1259 int c, face_id;
1260 XChar2b *char2b;
1261 int multibyte_p;
1262 {
1263 struct face *face = FACE_FROM_ID (f, face_id);
1264
1265 if (!multibyte_p)
1266 {
1267 /* Unibyte case. We don't have to encode, but we have to make
1268 sure to use a face suitable for unibyte. */
1269 char2b->byte1 = 0;
1270 char2b->byte2 = c;
1271 face_id = FACE_FOR_CHAR (f, face, c);
1272 face = FACE_FROM_ID (f, face_id);
1273 }
1274 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1275 {
1276 /* Case of ASCII in a face known to fit ASCII. */
1277 char2b->byte1 = 0;
1278 char2b->byte2 = c;
1279 }
1280 else
1281 {
1282 int c1, c2, charset;
1283
1284 /* Split characters into bytes. If c2 is -1 afterwards, C is
1285 really a one-byte character so that byte1 is zero. */
1286 SPLIT_CHAR (c, charset, c1, c2);
1287 if (c2 > 0)
1288 char2b->byte1 = c1, char2b->byte2 = c2;
1289 else
1290 char2b->byte1 = 0, char2b->byte2 = c1;
1291
1292 /* Maybe encode the character in *CHAR2B. */
1293 if (face->font != NULL)
1294 {
1295 struct font_info *font_info
1296 = FONT_INFO_FROM_ID (f, face->font_info_id);
1297 if (font_info)
1298 x_encode_char (c, char2b, font_info);
1299 }
1300 }
1301
1302 /* Make sure X resources of the face are allocated. */
1303 xassert (face != NULL);
1304 PREPARE_FACE_FOR_DISPLAY (f, face);
1305
1306 return face;
1307 }
1308
1309
1310 /* Get face and two-byte form of character glyph GLYPH on frame F.
1311 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1312 a pointer to a realized face that is ready for display. */
1313
1314 static INLINE struct face *
1315 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1316 struct frame *f;
1317 struct glyph *glyph;
1318 XChar2b *char2b;
1319 int *two_byte_p;
1320 {
1321 struct face *face;
1322
1323 xassert (glyph->type == CHAR_GLYPH);
1324 face = FACE_FROM_ID (f, glyph->face_id);
1325
1326 if (two_byte_p)
1327 *two_byte_p = 0;
1328
1329 if (!glyph->multibyte_p)
1330 {
1331 /* Unibyte case. We don't have to encode, but we have to make
1332 sure to use a face suitable for unibyte. */
1333 char2b->byte1 = 0;
1334 char2b->byte2 = glyph->u.ch;
1335 }
1336 else if (glyph->u.ch < 128
1337 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1338 {
1339 /* Case of ASCII in a face known to fit ASCII. */
1340 char2b->byte1 = 0;
1341 char2b->byte2 = glyph->u.ch;
1342 }
1343 else
1344 {
1345 int c1, c2, charset;
1346
1347 /* Split characters into bytes. If c2 is -1 afterwards, C is
1348 really a one-byte character so that byte1 is zero. */
1349 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1350 if (c2 > 0)
1351 char2b->byte1 = c1, char2b->byte2 = c2;
1352 else
1353 char2b->byte1 = 0, char2b->byte2 = c1;
1354
1355 /* Maybe encode the character in *CHAR2B. */
1356 if (charset != CHARSET_ASCII)
1357 {
1358 struct font_info *font_info
1359 = FONT_INFO_FROM_ID (f, face->font_info_id);
1360 if (font_info)
1361 {
1362 x_encode_char (glyph->u.ch, char2b, font_info);
1363 if (two_byte_p)
1364 *two_byte_p
1365 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1366 }
1367 }
1368 }
1369
1370 /* Make sure X resources of the face are allocated. */
1371 xassert (face != NULL);
1372 PREPARE_FACE_FOR_DISPLAY (f, face);
1373 return face;
1374 }
1375
1376
1377 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1378 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1379
1380 static INLINE void
1381 x_append_glyph (it)
1382 struct it *it;
1383 {
1384 struct glyph *glyph;
1385 enum glyph_row_area area = it->area;
1386
1387 xassert (it->glyph_row);
1388 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1389
1390 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1391 if (glyph < it->glyph_row->glyphs[area + 1])
1392 {
1393 /* Play it safe. If sub-structures of the glyph are not all the
1394 same size, it otherwise be that some bits stay set. This
1395 would prevent a comparison with GLYPH_EQUAL_P. */
1396 glyph->u.val = 0;
1397
1398 glyph->type = CHAR_GLYPH;
1399 glyph->pixel_width = it->pixel_width;
1400 glyph->u.ch = it->char_to_display;
1401 glyph->face_id = it->face_id;
1402 glyph->charpos = CHARPOS (it->position);
1403 glyph->object = it->object;
1404 glyph->left_box_line_p = it->start_of_box_run_p;
1405 glyph->right_box_line_p = it->end_of_box_run_p;
1406 glyph->voffset = it->voffset;
1407 glyph->multibyte_p = it->multibyte_p;
1408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1409 || it->phys_descent > it->descent);
1410 glyph->glyph_not_available_p = it->glyph_not_available_p;
1411 ++it->glyph_row->used[area];
1412 }
1413 }
1414
1415 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418 static INLINE void
1419 x_append_composite_glyph (it)
1420 struct it *it;
1421 {
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426
1427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1428 if (glyph < it->glyph_row->glyphs[area + 1])
1429 {
1430 /* Play it safe. If sub-structures of the glyph are not all the
1431 same size, it otherwise be that some bits stay set. This
1432 would prevent a comparison with GLYPH_EQUAL_P. */
1433 glyph->u.val = 0;
1434
1435 glyph->type = COMPOSITE_GLYPH;
1436 glyph->pixel_width = it->pixel_width;
1437 glyph->u.cmp_id = it->cmp_id;
1438 glyph->face_id = it->face_id;
1439 glyph->charpos = CHARPOS (it->position);
1440 glyph->object = it->object;
1441 glyph->left_box_line_p = it->start_of_box_run_p;
1442 glyph->right_box_line_p = it->end_of_box_run_p;
1443 glyph->voffset = it->voffset;
1444 glyph->multibyte_p = it->multibyte_p;
1445 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1446 || it->phys_descent > it->descent);
1447 ++it->glyph_row->used[area];
1448 }
1449 }
1450
1451
1452 /* Change IT->ascent and IT->height according to the setting of
1453 IT->voffset. */
1454
1455 static INLINE void
1456 take_vertical_position_into_account (it)
1457 struct it *it;
1458 {
1459 if (it->voffset)
1460 {
1461 if (it->voffset < 0)
1462 /* Increase the ascent so that we can display the text higher
1463 in the line. */
1464 it->ascent += abs (it->voffset);
1465 else
1466 /* Increase the descent so that we can display the text lower
1467 in the line. */
1468 it->descent += it->voffset;
1469 }
1470 }
1471
1472
1473 /* Produce glyphs/get display metrics for the image IT is loaded with.
1474 See the description of struct display_iterator in dispextern.h for
1475 an overview of struct display_iterator. */
1476
1477 static void
1478 x_produce_image_glyph (it)
1479 struct it *it;
1480 {
1481 struct image *img;
1482 struct face *face;
1483
1484 xassert (it->what == IT_IMAGE);
1485
1486 face = FACE_FROM_ID (it->f, it->face_id);
1487 img = IMAGE_FROM_ID (it->f, it->image_id);
1488 xassert (img);
1489
1490 /* Make sure X resources of the face and image are loaded. */
1491 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1492 prepare_image_for_display (it->f, img);
1493
1494 it->ascent = it->phys_ascent = image_ascent (img, face);
1495 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1496 it->pixel_width = img->width + 2 * img->margin;
1497
1498 it->nglyphs = 1;
1499
1500 if (face->box != FACE_NO_BOX)
1501 {
1502 it->ascent += face->box_line_width;
1503 it->descent += face->box_line_width;
1504
1505 if (it->start_of_box_run_p)
1506 it->pixel_width += face->box_line_width;
1507 if (it->end_of_box_run_p)
1508 it->pixel_width += face->box_line_width;
1509 }
1510
1511 take_vertical_position_into_account (it);
1512
1513 if (it->glyph_row)
1514 {
1515 struct glyph *glyph;
1516 enum glyph_row_area area = it->area;
1517
1518 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1519 if (glyph < it->glyph_row->glyphs[area + 1])
1520 {
1521 glyph->type = IMAGE_GLYPH;
1522 glyph->u.img_id = img->id;
1523 glyph->face_id = it->face_id;
1524 glyph->pixel_width = it->pixel_width;
1525 glyph->charpos = CHARPOS (it->position);
1526 glyph->object = it->object;
1527 glyph->left_box_line_p = it->start_of_box_run_p;
1528 glyph->right_box_line_p = it->end_of_box_run_p;
1529 glyph->voffset = it->voffset;
1530 glyph->multibyte_p = it->multibyte_p;
1531 ++it->glyph_row->used[area];
1532 }
1533 }
1534 }
1535
1536
1537 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1538 of the glyph, WIDTH and HEIGHT are the width and height of the
1539 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1540 ascent of the glyph (0 <= ASCENT <= 1). */
1541
1542 static void
1543 x_append_stretch_glyph (it, object, width, height, ascent)
1544 struct it *it;
1545 Lisp_Object object;
1546 int width, height;
1547 double ascent;
1548 {
1549 struct glyph *glyph;
1550 enum glyph_row_area area = it->area;
1551
1552 xassert (ascent >= 0 && ascent <= 1);
1553
1554 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1555 if (glyph < it->glyph_row->glyphs[area + 1])
1556 {
1557 glyph->type = STRETCH_GLYPH;
1558 glyph->u.stretch.ascent = height * ascent;
1559 glyph->u.stretch.height = height;
1560 glyph->face_id = it->face_id;
1561 glyph->pixel_width = width;
1562 glyph->charpos = CHARPOS (it->position);
1563 glyph->object = object;
1564 glyph->left_box_line_p = it->start_of_box_run_p;
1565 glyph->right_box_line_p = it->end_of_box_run_p;
1566 glyph->voffset = it->voffset;
1567 glyph->multibyte_p = it->multibyte_p;
1568 ++it->glyph_row->used[area];
1569 }
1570 }
1571
1572
1573 /* Produce a stretch glyph for iterator IT. IT->object is the value
1574 of the glyph property displayed. The value must be a list
1575 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1576 being recognized:
1577
1578 1. `:width WIDTH' specifies that the space should be WIDTH *
1579 canonical char width wide. WIDTH may be an integer or floating
1580 point number.
1581
1582 2. `:relative-width FACTOR' specifies that the width of the stretch
1583 should be computed from the width of the first character having the
1584 `glyph' property, and should be FACTOR times that width.
1585
1586 3. `:align-to HPOS' specifies that the space should be wide enough
1587 to reach HPOS, a value in canonical character units.
1588
1589 Exactly one of the above pairs must be present.
1590
1591 4. `:height HEIGHT' specifies that the height of the stretch produced
1592 should be HEIGHT, measured in canonical character units.
1593
1594 5. `:relative-height FACTOR' specifies that the height of the the
1595 stretch should be FACTOR times the height of the characters having
1596 the glyph property.
1597
1598 Either none or exactly one of 4 or 5 must be present.
1599
1600 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1601 of the stretch should be used for the ascent of the stretch.
1602 ASCENT must be in the range 0 <= ASCENT <= 100. */
1603
1604 #define NUMVAL(X) \
1605 ((INTEGERP (X) || FLOATP (X)) \
1606 ? XFLOATINT (X) \
1607 : - 1)
1608
1609
1610 static void
1611 x_produce_stretch_glyph (it)
1612 struct it *it;
1613 {
1614 /* (space :width WIDTH :height HEIGHT. */
1615 #if GLYPH_DEBUG
1616 extern Lisp_Object Qspace;
1617 #endif
1618 extern Lisp_Object QCwidth, QCheight, QCascent;
1619 extern Lisp_Object QCrelative_width, QCrelative_height;
1620 extern Lisp_Object QCalign_to;
1621 Lisp_Object prop, plist;
1622 double width = 0, height = 0, ascent = 0;
1623 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1624 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1625
1626 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1627
1628 /* List should start with `space'. */
1629 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1630 plist = XCDR (it->object);
1631
1632 /* Compute the width of the stretch. */
1633 if (prop = Fplist_get (plist, QCwidth),
1634 NUMVAL (prop) > 0)
1635 /* Absolute width `:width WIDTH' specified and valid. */
1636 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1637 else if (prop = Fplist_get (plist, QCrelative_width),
1638 NUMVAL (prop) > 0)
1639 {
1640 /* Relative width `:relative-width FACTOR' specified and valid.
1641 Compute the width of the characters having the `glyph'
1642 property. */
1643 struct it it2;
1644 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1645
1646 it2 = *it;
1647 if (it->multibyte_p)
1648 {
1649 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1650 - IT_BYTEPOS (*it));
1651 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1652 }
1653 else
1654 it2.c = *p, it2.len = 1;
1655
1656 it2.glyph_row = NULL;
1657 it2.what = IT_CHARACTER;
1658 x_produce_glyphs (&it2);
1659 width = NUMVAL (prop) * it2.pixel_width;
1660 }
1661 else if (prop = Fplist_get (plist, QCalign_to),
1662 NUMVAL (prop) > 0)
1663 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1664 else
1665 /* Nothing specified -> width defaults to canonical char width. */
1666 width = CANON_X_UNIT (it->f);
1667
1668 /* Compute height. */
1669 if (prop = Fplist_get (plist, QCheight),
1670 NUMVAL (prop) > 0)
1671 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1672 else if (prop = Fplist_get (plist, QCrelative_height),
1673 NUMVAL (prop) > 0)
1674 height = FONT_HEIGHT (font) * NUMVAL (prop);
1675 else
1676 height = FONT_HEIGHT (font);
1677
1678 /* Compute percentage of height used for ascent. If
1679 `:ascent ASCENT' is present and valid, use that. Otherwise,
1680 derive the ascent from the font in use. */
1681 if (prop = Fplist_get (plist, QCascent),
1682 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1683 ascent = NUMVAL (prop) / 100.0;
1684 else
1685 ascent = (double) font->ascent / FONT_HEIGHT (font);
1686
1687 if (width <= 0)
1688 width = 1;
1689 if (height <= 0)
1690 height = 1;
1691
1692 if (it->glyph_row)
1693 {
1694 Lisp_Object object = it->stack[it->sp - 1].string;
1695 if (!STRINGP (object))
1696 object = it->w->buffer;
1697 x_append_stretch_glyph (it, object, width, height, ascent);
1698 }
1699
1700 it->pixel_width = width;
1701 it->ascent = it->phys_ascent = height * ascent;
1702 it->descent = it->phys_descent = height - it->ascent;
1703 it->nglyphs = 1;
1704
1705 if (face->box != FACE_NO_BOX)
1706 {
1707 it->ascent += face->box_line_width;
1708 it->descent += face->box_line_width;
1709
1710 if (it->start_of_box_run_p)
1711 it->pixel_width += face->box_line_width;
1712 if (it->end_of_box_run_p)
1713 it->pixel_width += face->box_line_width;
1714 }
1715
1716 take_vertical_position_into_account (it);
1717 }
1718
1719 /* Return proper value to be used as baseline offset of font that has
1720 ASCENT and DESCENT to draw characters by the font at the vertical
1721 center of the line of frame F.
1722
1723 Here, out task is to find the value of BOFF in the following figure;
1724
1725 -------------------------+-----------+-
1726 -+-+---------+-+ | |
1727 | | | | | |
1728 | | | | F_ASCENT F_HEIGHT
1729 | | | ASCENT | |
1730 HEIGHT | | | | |
1731 | | |-|-+------+-----------|------- baseline
1732 | | | | BOFF | |
1733 | |---------|-+-+ | |
1734 | | | DESCENT | |
1735 -+-+---------+-+ F_DESCENT |
1736 -------------------------+-----------+-
1737
1738 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1739 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1740 DESCENT = FONT->descent
1741 HEIGHT = FONT_HEIGHT (FONT)
1742 F_DESCENT = (F->output_data.x->font->descent
1743 - F->output_data.x->baseline_offset)
1744 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1745 */
1746
1747 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1748 ((FONT)->descent \
1749 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1750 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1751
1752 /* Produce glyphs/get display metrics for the display element IT is
1753 loaded with. See the description of struct display_iterator in
1754 dispextern.h for an overview of struct display_iterator. */
1755
1756 static void
1757 x_produce_glyphs (it)
1758 struct it *it;
1759 {
1760 it->glyph_not_available_p = 0;
1761
1762 if (it->what == IT_CHARACTER)
1763 {
1764 XChar2b char2b;
1765 XFontStruct *font;
1766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1767 XCharStruct *pcm;
1768 int font_not_found_p;
1769 struct font_info *font_info;
1770 int boff; /* baseline offset */
1771
1772 /* Maybe translate single-byte characters to multibyte, or the
1773 other way. */
1774 it->char_to_display = it->c;
1775 if (!ASCII_BYTE_P (it->c))
1776 {
1777 if (unibyte_display_via_language_environment
1778 && SINGLE_BYTE_CHAR_P (it->c)
1779 && (it->c >= 0240
1780 || !NILP (Vnonascii_translation_table)))
1781 {
1782 it->char_to_display = unibyte_char_to_multibyte (it->c);
1783 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1784 face = FACE_FROM_ID (it->f, it->face_id);
1785 }
1786 else if (!SINGLE_BYTE_CHAR_P (it->c)
1787 && !it->multibyte_p)
1788 {
1789 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1790 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1791 face = FACE_FROM_ID (it->f, it->face_id);
1792 }
1793 }
1794
1795 /* Get font to use. Encode IT->char_to_display. */
1796 x_get_char_face_and_encoding (it->f, it->char_to_display,
1797 it->face_id, &char2b,
1798 it->multibyte_p);
1799 font = face->font;
1800
1801 /* When no suitable font found, use the default font. */
1802 font_not_found_p = font == NULL;
1803 if (font_not_found_p)
1804 {
1805 font = FRAME_FONT (it->f);
1806 boff = it->f->output_data.x->baseline_offset;
1807 font_info = NULL;
1808 }
1809 else
1810 {
1811 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1812 boff = font_info->baseline_offset;
1813 if (font_info->vertical_centering)
1814 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1815 }
1816
1817 if (it->char_to_display >= ' '
1818 && (!it->multibyte_p || it->char_to_display < 128))
1819 {
1820 /* Either unibyte or ASCII. */
1821 int stretched_p;
1822
1823 it->nglyphs = 1;
1824
1825 pcm = x_per_char_metric (font, &char2b);
1826 it->ascent = font->ascent + boff;
1827 it->descent = font->descent - boff;
1828
1829 if (pcm)
1830 {
1831 it->phys_ascent = pcm->ascent + boff;
1832 it->phys_descent = pcm->descent - boff;
1833 it->pixel_width = pcm->width;
1834 }
1835 else
1836 {
1837 it->glyph_not_available_p = 1;
1838 it->phys_ascent = font->ascent + boff;
1839 it->phys_descent = font->descent - boff;
1840 it->pixel_width = FONT_WIDTH (font);
1841 }
1842
1843 /* If this is a space inside a region of text with
1844 `space-width' property, change its width. */
1845 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1846 if (stretched_p)
1847 it->pixel_width *= XFLOATINT (it->space_width);
1848
1849 /* If face has a box, add the box thickness to the character
1850 height. If character has a box line to the left and/or
1851 right, add the box line width to the character's width. */
1852 if (face->box != FACE_NO_BOX)
1853 {
1854 int thick = face->box_line_width;
1855
1856 it->ascent += thick;
1857 it->descent += thick;
1858
1859 if (it->start_of_box_run_p)
1860 it->pixel_width += thick;
1861 if (it->end_of_box_run_p)
1862 it->pixel_width += thick;
1863 }
1864
1865 /* If face has an overline, add the height of the overline
1866 (1 pixel) and a 1 pixel margin to the character height. */
1867 if (face->overline_p)
1868 it->ascent += 2;
1869
1870 take_vertical_position_into_account (it);
1871
1872 /* If we have to actually produce glyphs, do it. */
1873 if (it->glyph_row)
1874 {
1875 if (stretched_p)
1876 {
1877 /* Translate a space with a `space-width' property
1878 into a stretch glyph. */
1879 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1880 x_append_stretch_glyph (it, it->object, it->pixel_width,
1881 it->ascent + it->descent, ascent);
1882 }
1883 else
1884 x_append_glyph (it);
1885
1886 /* If characters with lbearing or rbearing are displayed
1887 in this line, record that fact in a flag of the
1888 glyph row. This is used to optimize X output code. */
1889 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1890 it->glyph_row->contains_overlapping_glyphs_p = 1;
1891 }
1892 }
1893 else if (it->char_to_display == '\n')
1894 {
1895 /* A newline has no width but we need the height of the line. */
1896 it->pixel_width = 0;
1897 it->nglyphs = 0;
1898 it->ascent = it->phys_ascent = font->ascent + boff;
1899 it->descent = it->phys_descent = font->descent - boff;
1900
1901 if (face->box != FACE_NO_BOX)
1902 {
1903 int thick = face->box_line_width;
1904 it->ascent += thick;
1905 it->descent += thick;
1906 }
1907 }
1908 else if (it->char_to_display == '\t')
1909 {
1910 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1911 int x = it->current_x + it->continuation_lines_width;
1912 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1913
1914 it->pixel_width = next_tab_x - x;
1915 it->nglyphs = 1;
1916 it->ascent = it->phys_ascent = font->ascent + boff;
1917 it->descent = it->phys_descent = font->descent - boff;
1918
1919 if (it->glyph_row)
1920 {
1921 double ascent = (double) it->ascent / (it->ascent + it->descent);
1922 x_append_stretch_glyph (it, it->object, it->pixel_width,
1923 it->ascent + it->descent, ascent);
1924 }
1925 }
1926 else
1927 {
1928 /* A multi-byte character. Assume that the display width of the
1929 character is the width of the character multiplied by the
1930 width of the font. */
1931
1932 /* If we found a font, this font should give us the right
1933 metrics. If we didn't find a font, use the frame's
1934 default font and calculate the width of the character
1935 from the charset width; this is what old redisplay code
1936 did. */
1937 pcm = x_per_char_metric (font, &char2b);
1938 if (font_not_found_p || !pcm)
1939 {
1940 int charset = CHAR_CHARSET (it->char_to_display);
1941
1942 it->glyph_not_available_p = 1;
1943 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1944 * CHARSET_WIDTH (charset));
1945 it->phys_ascent = font->ascent + boff;
1946 it->phys_descent = font->descent - boff;
1947 }
1948 else
1949 {
1950 it->pixel_width = pcm->width;
1951 it->phys_ascent = pcm->ascent + boff;
1952 it->phys_descent = pcm->descent - boff;
1953 if (it->glyph_row
1954 && (pcm->lbearing < 0
1955 || pcm->rbearing > pcm->width))
1956 it->glyph_row->contains_overlapping_glyphs_p = 1;
1957 }
1958 it->nglyphs = 1;
1959 it->ascent = font->ascent + boff;
1960 it->descent = font->descent - boff;
1961 if (face->box != FACE_NO_BOX)
1962 {
1963 int thick = face->box_line_width;
1964 it->ascent += thick;
1965 it->descent += thick;
1966
1967 if (it->start_of_box_run_p)
1968 it->pixel_width += thick;
1969 if (it->end_of_box_run_p)
1970 it->pixel_width += thick;
1971 }
1972
1973 /* If face has an overline, add the height of the overline
1974 (1 pixel) and a 1 pixel margin to the character height. */
1975 if (face->overline_p)
1976 it->ascent += 2;
1977
1978 take_vertical_position_into_account (it);
1979
1980 if (it->glyph_row)
1981 x_append_glyph (it);
1982 }
1983 }
1984 else if (it->what == IT_COMPOSITION)
1985 {
1986 /* Note: A composition is represented as one glyph in the
1987 glyph matrix. There are no padding glyphs. */
1988 XChar2b char2b;
1989 XFontStruct *font;
1990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1991 XCharStruct *pcm;
1992 int font_not_found_p;
1993 struct font_info *font_info;
1994 int boff; /* baseline offset */
1995 struct composition *cmp = composition_table[it->cmp_id];
1996
1997 /* Maybe translate single-byte characters to multibyte. */
1998 it->char_to_display = it->c;
1999 if (unibyte_display_via_language_environment
2000 && SINGLE_BYTE_CHAR_P (it->c)
2001 && (it->c >= 0240
2002 || (it->c >= 0200
2003 && !NILP (Vnonascii_translation_table))))
2004 {
2005 it->char_to_display = unibyte_char_to_multibyte (it->c);
2006 }
2007
2008 /* Get face and font to use. Encode IT->char_to_display. */
2009 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2010 face = FACE_FROM_ID (it->f, it->face_id);
2011 x_get_char_face_and_encoding (it->f, it->char_to_display,
2012 it->face_id, &char2b, it->multibyte_p);
2013 font = face->font;
2014
2015 /* When no suitable font found, use the default font. */
2016 font_not_found_p = font == NULL;
2017 if (font_not_found_p)
2018 {
2019 font = FRAME_FONT (it->f);
2020 boff = it->f->output_data.x->baseline_offset;
2021 font_info = NULL;
2022 }
2023 else
2024 {
2025 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2026 boff = font_info->baseline_offset;
2027 if (font_info->vertical_centering)
2028 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2029 }
2030
2031 /* There are no padding glyphs, so there is only one glyph to
2032 produce for the composition. Important is that pixel_width,
2033 ascent and descent are the values of what is drawn by
2034 draw_glyphs (i.e. the values of the overall glyphs composed). */
2035 it->nglyphs = 1;
2036
2037 /* If we have not yet calculated pixel size data of glyphs of
2038 the composition for the current face font, calculate them
2039 now. Theoretically, we have to check all fonts for the
2040 glyphs, but that requires much time and memory space. So,
2041 here we check only the font of the first glyph. This leads
2042 to incorrect display very rarely, and C-l (recenter) can
2043 correct the display anyway. */
2044 if (cmp->font != (void *) font)
2045 {
2046 /* Ascent and descent of the font of the first character of
2047 this composition (adjusted by baseline offset). Ascent
2048 and descent of overall glyphs should not be less than
2049 them respectively. */
2050 int font_ascent = font->ascent + boff;
2051 int font_descent = font->descent - boff;
2052 /* Bounding box of the overall glyphs. */
2053 int leftmost, rightmost, lowest, highest;
2054 int i, width, ascent, descent;
2055
2056 cmp->font = (void *) font;
2057
2058 /* Initialize the bounding box. */
2059 pcm = x_per_char_metric (font, &char2b);
2060 if (pcm)
2061 {
2062 width = pcm->width;
2063 ascent = pcm->ascent;
2064 descent = pcm->descent;
2065 }
2066 else
2067 {
2068 width = FONT_WIDTH (font);
2069 ascent = font->ascent;
2070 descent = font->descent;
2071 }
2072
2073 rightmost = width;
2074 lowest = - descent + boff;
2075 highest = ascent + boff;
2076 leftmost = 0;
2077
2078 if (font_info
2079 && font_info->default_ascent
2080 && CHAR_TABLE_P (Vuse_default_ascent)
2081 && !NILP (Faref (Vuse_default_ascent,
2082 make_number (it->char_to_display))))
2083 highest = font_info->default_ascent + boff;
2084
2085 /* Draw the first glyph at the normal position. It may be
2086 shifted to right later if some other glyphs are drawn at
2087 the left. */
2088 cmp->offsets[0] = 0;
2089 cmp->offsets[1] = boff;
2090
2091 /* Set cmp->offsets for the remaining glyphs. */
2092 for (i = 1; i < cmp->glyph_len; i++)
2093 {
2094 int left, right, btm, top;
2095 int ch = COMPOSITION_GLYPH (cmp, i);
2096 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2097
2098 face = FACE_FROM_ID (it->f, face_id);
2099 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2100 it->multibyte_p);
2101 font = face->font;
2102 if (font == NULL)
2103 {
2104 font = FRAME_FONT (it->f);
2105 boff = it->f->output_data.x->baseline_offset;
2106 font_info = NULL;
2107 }
2108 else
2109 {
2110 font_info
2111 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2112 boff = font_info->baseline_offset;
2113 if (font_info->vertical_centering)
2114 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2115 }
2116
2117 pcm = x_per_char_metric (font, &char2b);
2118 if (pcm)
2119 {
2120 width = pcm->width;
2121 ascent = pcm->ascent;
2122 descent = pcm->descent;
2123 }
2124 else
2125 {
2126 width = FONT_WIDTH (font);
2127 ascent = font->ascent;
2128 descent = font->descent;
2129 }
2130
2131 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2132 {
2133 /* Relative composition with or without
2134 alternate chars. */
2135 left = (leftmost + rightmost - width) / 2;
2136 btm = - descent + boff;
2137 if (font_info && font_info->relative_compose
2138 && (! CHAR_TABLE_P (Vignore_relative_composition)
2139 || NILP (Faref (Vignore_relative_composition,
2140 make_number (ch)))))
2141 {
2142
2143 if (- descent >= font_info->relative_compose)
2144 /* One extra pixel between two glyphs. */
2145 btm = highest + 1;
2146 else if (ascent <= 0)
2147 /* One extra pixel between two glyphs. */
2148 btm = lowest - 1 - ascent - descent;
2149 }
2150 }
2151 else
2152 {
2153 /* A composition rule is specified by an integer
2154 value that encodes global and new reference
2155 points (GREF and NREF). GREF and NREF are
2156 specified by numbers as below:
2157
2158 0---1---2 -- ascent
2159 | |
2160 | |
2161 | |
2162 9--10--11 -- center
2163 | |
2164 ---3---4---5--- baseline
2165 | |
2166 6---7---8 -- descent
2167 */
2168 int rule = COMPOSITION_RULE (cmp, i);
2169 int gref, nref, grefx, grefy, nrefx, nrefy;
2170
2171 COMPOSITION_DECODE_RULE (rule, gref, nref);
2172 grefx = gref % 3, nrefx = nref % 3;
2173 grefy = gref / 3, nrefy = nref / 3;
2174
2175 left = (leftmost
2176 + grefx * (rightmost - leftmost) / 2
2177 - nrefx * width / 2);
2178 btm = ((grefy == 0 ? highest
2179 : grefy == 1 ? 0
2180 : grefy == 2 ? lowest
2181 : (highest + lowest) / 2)
2182 - (nrefy == 0 ? ascent + descent
2183 : nrefy == 1 ? descent - boff
2184 : nrefy == 2 ? 0
2185 : (ascent + descent) / 2));
2186 }
2187
2188 cmp->offsets[i * 2] = left;
2189 cmp->offsets[i * 2 + 1] = btm + descent;
2190
2191 /* Update the bounding box of the overall glyphs. */
2192 right = left + width;
2193 top = btm + descent + ascent;
2194 if (left < leftmost)
2195 leftmost = left;
2196 if (right > rightmost)
2197 rightmost = right;
2198 if (top > highest)
2199 highest = top;
2200 if (btm < lowest)
2201 lowest = btm;
2202 }
2203
2204 /* If there are glyphs whose x-offsets are negative,
2205 shift all glyphs to the right and make all x-offsets
2206 non-negative. */
2207 if (leftmost < 0)
2208 {
2209 for (i = 0; i < cmp->glyph_len; i++)
2210 cmp->offsets[i * 2] -= leftmost;
2211 rightmost -= leftmost;
2212 }
2213
2214 cmp->pixel_width = rightmost;
2215 cmp->ascent = highest;
2216 cmp->descent = - lowest;
2217 if (cmp->ascent < font_ascent)
2218 cmp->ascent = font_ascent;
2219 if (cmp->descent < font_descent)
2220 cmp->descent = font_descent;
2221 }
2222
2223 it->pixel_width = cmp->pixel_width;
2224 it->ascent = it->phys_ascent = cmp->ascent;
2225 it->descent = it->phys_descent = cmp->descent;
2226
2227 if (face->box != FACE_NO_BOX)
2228 {
2229 int thick = face->box_line_width;
2230 it->ascent += thick;
2231 it->descent += thick;
2232
2233 if (it->start_of_box_run_p)
2234 it->pixel_width += thick;
2235 if (it->end_of_box_run_p)
2236 it->pixel_width += thick;
2237 }
2238
2239 /* If face has an overline, add the height of the overline
2240 (1 pixel) and a 1 pixel margin to the character height. */
2241 if (face->overline_p)
2242 it->ascent += 2;
2243
2244 take_vertical_position_into_account (it);
2245
2246 if (it->glyph_row)
2247 x_append_composite_glyph (it);
2248 }
2249 else if (it->what == IT_IMAGE)
2250 x_produce_image_glyph (it);
2251 else if (it->what == IT_STRETCH)
2252 x_produce_stretch_glyph (it);
2253
2254 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2255 because this isn't true for images with `:ascent 100'. */
2256 xassert (it->ascent >= 0 && it->descent >= 0);
2257 if (it->area == TEXT_AREA)
2258 it->current_x += it->pixel_width;
2259
2260 it->descent += it->extra_line_spacing;
2261
2262 it->max_ascent = max (it->max_ascent, it->ascent);
2263 it->max_descent = max (it->max_descent, it->descent);
2264 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2265 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2266 }
2267
2268
2269 /* Estimate the pixel height of the mode or top line on frame F.
2270 FACE_ID specifies what line's height to estimate. */
2271
2272 int
2273 x_estimate_mode_line_height (f, face_id)
2274 struct frame *f;
2275 enum face_id face_id;
2276 {
2277 int height = 1;
2278
2279 /* This function is called so early when Emacs starts that the face
2280 cache and mode line face are not yet initialized. */
2281 if (FRAME_FACE_CACHE (f))
2282 {
2283 struct face *face = FACE_FROM_ID (f, face_id);
2284 if (face)
2285 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2286 }
2287
2288 return height;
2289 }
2290
2291 \f
2292 /***********************************************************************
2293 Glyph display
2294 ***********************************************************************/
2295
2296 /* A sequence of glyphs to be drawn in the same face.
2297
2298 This data structure is not really completely X specific, so it
2299 could possibly, at least partially, be useful for other systems. It
2300 is currently not part of the external redisplay interface because
2301 it's not clear what other systems will need. */
2302
2303 struct glyph_string
2304 {
2305 /* X-origin of the string. */
2306 int x;
2307
2308 /* Y-origin and y-position of the base line of this string. */
2309 int y, ybase;
2310
2311 /* The width of the string, not including a face extension. */
2312 int width;
2313
2314 /* The width of the string, including a face extension. */
2315 int background_width;
2316
2317 /* The height of this string. This is the height of the line this
2318 string is drawn in, and can be different from the height of the
2319 font the string is drawn in. */
2320 int height;
2321
2322 /* Number of pixels this string overwrites in front of its x-origin.
2323 This number is zero if the string has an lbearing >= 0; it is
2324 -lbearing, if the string has an lbearing < 0. */
2325 int left_overhang;
2326
2327 /* Number of pixels this string overwrites past its right-most
2328 nominal x-position, i.e. x + width. Zero if the string's
2329 rbearing is <= its nominal width, rbearing - width otherwise. */
2330 int right_overhang;
2331
2332 /* The frame on which the glyph string is drawn. */
2333 struct frame *f;
2334
2335 /* The window on which the glyph string is drawn. */
2336 struct window *w;
2337
2338 /* X display and window for convenience. */
2339 Display *display;
2340 Window window;
2341
2342 /* The glyph row for which this string was built. It determines the
2343 y-origin and height of the string. */
2344 struct glyph_row *row;
2345
2346 /* The area within row. */
2347 enum glyph_row_area area;
2348
2349 /* Characters to be drawn, and number of characters. */
2350 XChar2b *char2b;
2351 int nchars;
2352
2353 /* A face-override for drawing cursors, mouse face and similar. */
2354 enum draw_glyphs_face hl;
2355
2356 /* Face in which this string is to be drawn. */
2357 struct face *face;
2358
2359 /* Font in which this string is to be drawn. */
2360 XFontStruct *font;
2361
2362 /* Font info for this string. */
2363 struct font_info *font_info;
2364
2365 /* Non-null means this string describes (part of) a composition.
2366 All characters from char2b are drawn composed. */
2367 struct composition *cmp;
2368
2369 /* Index of this glyph string's first character in the glyph
2370 definition of CMP. If this is zero, this glyph string describes
2371 the first character of a composition. */
2372 int gidx;
2373
2374 /* 1 means this glyph strings face has to be drawn to the right end
2375 of the window's drawing area. */
2376 unsigned extends_to_end_of_line_p : 1;
2377
2378 /* 1 means the background of this string has been drawn. */
2379 unsigned background_filled_p : 1;
2380
2381 /* 1 means glyph string must be drawn with 16-bit functions. */
2382 unsigned two_byte_p : 1;
2383
2384 /* 1 means that the original font determined for drawing this glyph
2385 string could not be loaded. The member `font' has been set to
2386 the frame's default font in this case. */
2387 unsigned font_not_found_p : 1;
2388
2389 /* 1 means that the face in which this glyph string is drawn has a
2390 stipple pattern. */
2391 unsigned stippled_p : 1;
2392
2393 /* 1 means only the foreground of this glyph string must be drawn,
2394 and we should use the physical height of the line this glyph
2395 string appears in as clip rect. */
2396 unsigned for_overlaps_p : 1;
2397
2398 /* The GC to use for drawing this glyph string. */
2399 GC gc;
2400
2401 /* A pointer to the first glyph in the string. This glyph
2402 corresponds to char2b[0]. Needed to draw rectangles if
2403 font_not_found_p is 1. */
2404 struct glyph *first_glyph;
2405
2406 /* Image, if any. */
2407 struct image *img;
2408
2409 struct glyph_string *next, *prev;
2410 };
2411
2412
2413 #if 0
2414
2415 static void
2416 x_dump_glyph_string (s)
2417 struct glyph_string *s;
2418 {
2419 fprintf (stderr, "glyph string\n");
2420 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2421 s->x, s->y, s->width, s->height);
2422 fprintf (stderr, " ybase = %d\n", s->ybase);
2423 fprintf (stderr, " hl = %d\n", s->hl);
2424 fprintf (stderr, " left overhang = %d, right = %d\n",
2425 s->left_overhang, s->right_overhang);
2426 fprintf (stderr, " nchars = %d\n", s->nchars);
2427 fprintf (stderr, " extends to end of line = %d\n",
2428 s->extends_to_end_of_line_p);
2429 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2430 fprintf (stderr, " bg width = %d\n", s->background_width);
2431 }
2432
2433 #endif /* GLYPH_DEBUG */
2434
2435
2436
2437 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2438 struct glyph_string **,
2439 struct glyph_string *,
2440 struct glyph_string *));
2441 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2442 struct glyph_string **,
2443 struct glyph_string *,
2444 struct glyph_string *));
2445 static void x_append_glyph_string P_ ((struct glyph_string **,
2446 struct glyph_string **,
2447 struct glyph_string *));
2448 static int x_left_overwritten P_ ((struct glyph_string *));
2449 static int x_left_overwriting P_ ((struct glyph_string *));
2450 static int x_right_overwritten P_ ((struct glyph_string *));
2451 static int x_right_overwriting P_ ((struct glyph_string *));
2452 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2453 int));
2454 static void x_init_glyph_string P_ ((struct glyph_string *,
2455 XChar2b *, struct window *,
2456 struct glyph_row *,
2457 enum glyph_row_area, int,
2458 enum draw_glyphs_face));
2459 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2460 enum glyph_row_area, int, int,
2461 enum draw_glyphs_face, int *, int *, int));
2462 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2463 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2464 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2465 int));
2466 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2467 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2468 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2469 static void x_draw_glyph_string P_ ((struct glyph_string *));
2470 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2471 static void x_set_cursor_gc P_ ((struct glyph_string *));
2472 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2473 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2474 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2475 int *, int *));
2476 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2477 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2478 unsigned long *, double, int));
2479 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2480 double, int, unsigned long));
2481 static void x_setup_relief_colors P_ ((struct glyph_string *));
2482 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2483 static void x_draw_image_relief P_ ((struct glyph_string *));
2484 static void x_draw_image_foreground P_ ((struct glyph_string *));
2485 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2486 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2487 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2488 int, int, int));
2489 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2490 int, int, int, int, XRectangle *));
2491 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2492 int, int, int, XRectangle *));
2493 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2494 enum glyph_row_area));
2495
2496 #if GLYPH_DEBUG
2497 static void x_check_font P_ ((struct frame *, XFontStruct *));
2498 #endif
2499
2500
2501 /* Append the list of glyph strings with head H and tail T to the list
2502 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2503
2504 static INLINE void
2505 x_append_glyph_string_lists (head, tail, h, t)
2506 struct glyph_string **head, **tail;
2507 struct glyph_string *h, *t;
2508 {
2509 if (h)
2510 {
2511 if (*head)
2512 (*tail)->next = h;
2513 else
2514 *head = h;
2515 h->prev = *tail;
2516 *tail = t;
2517 }
2518 }
2519
2520
2521 /* Prepend the list of glyph strings with head H and tail T to the
2522 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2523 result. */
2524
2525 static INLINE void
2526 x_prepend_glyph_string_lists (head, tail, h, t)
2527 struct glyph_string **head, **tail;
2528 struct glyph_string *h, *t;
2529 {
2530 if (h)
2531 {
2532 if (*head)
2533 (*head)->prev = t;
2534 else
2535 *tail = t;
2536 t->next = *head;
2537 *head = h;
2538 }
2539 }
2540
2541
2542 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2543 Set *HEAD and *TAIL to the resulting list. */
2544
2545 static INLINE void
2546 x_append_glyph_string (head, tail, s)
2547 struct glyph_string **head, **tail;
2548 struct glyph_string *s;
2549 {
2550 s->next = s->prev = NULL;
2551 x_append_glyph_string_lists (head, tail, s, s);
2552 }
2553
2554
2555 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2556 face. */
2557
2558 static void
2559 x_set_cursor_gc (s)
2560 struct glyph_string *s;
2561 {
2562 if (s->font == FRAME_FONT (s->f)
2563 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2564 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2565 && !s->cmp)
2566 s->gc = s->f->output_data.x->cursor_gc;
2567 else
2568 {
2569 /* Cursor on non-default face: must merge. */
2570 XGCValues xgcv;
2571 unsigned long mask;
2572
2573 xgcv.background = s->f->output_data.x->cursor_pixel;
2574 xgcv.foreground = s->face->background;
2575
2576 /* If the glyph would be invisible, try a different foreground. */
2577 if (xgcv.foreground == xgcv.background)
2578 xgcv.foreground = s->face->foreground;
2579 if (xgcv.foreground == xgcv.background)
2580 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2581 if (xgcv.foreground == xgcv.background)
2582 xgcv.foreground = s->face->foreground;
2583
2584 /* Make sure the cursor is distinct from text in this face. */
2585 if (xgcv.background == s->face->background
2586 && xgcv.foreground == s->face->foreground)
2587 {
2588 xgcv.background = s->face->foreground;
2589 xgcv.foreground = s->face->background;
2590 }
2591
2592 IF_DEBUG (x_check_font (s->f, s->font));
2593 xgcv.font = s->font->fid;
2594 xgcv.graphics_exposures = False;
2595 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2596
2597 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2598 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2599 mask, &xgcv);
2600 else
2601 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2602 = XCreateGC (s->display, s->window, mask, &xgcv);
2603
2604 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2605 }
2606 }
2607
2608
2609 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2610
2611 static void
2612 x_set_mouse_face_gc (s)
2613 struct glyph_string *s;
2614 {
2615 int face_id;
2616 struct face *face;
2617
2618 /* What face has to be used for the mouse face? */
2619 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2620 face = FACE_FROM_ID (s->f, face_id);
2621 if (s->first_glyph->type == CHAR_GLYPH)
2622 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2623 else
2624 face_id = FACE_FOR_CHAR (s->f, face, 0);
2625 s->face = FACE_FROM_ID (s->f, face_id);
2626 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2627
2628 /* If font in this face is same as S->font, use it. */
2629 if (s->font == s->face->font)
2630 s->gc = s->face->gc;
2631 else
2632 {
2633 /* Otherwise construct scratch_cursor_gc with values from FACE
2634 but font FONT. */
2635 XGCValues xgcv;
2636 unsigned long mask;
2637
2638 xgcv.background = s->face->background;
2639 xgcv.foreground = s->face->foreground;
2640 IF_DEBUG (x_check_font (s->f, s->font));
2641 xgcv.font = s->font->fid;
2642 xgcv.graphics_exposures = False;
2643 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2644
2645 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2646 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2647 mask, &xgcv);
2648 else
2649 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2650 = XCreateGC (s->display, s->window, mask, &xgcv);
2651
2652 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2653 }
2654
2655 xassert (s->gc != 0);
2656 }
2657
2658
2659 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2660 Faces to use in the mode line have already been computed when the
2661 matrix was built, so there isn't much to do, here. */
2662
2663 static INLINE void
2664 x_set_mode_line_face_gc (s)
2665 struct glyph_string *s;
2666 {
2667 s->gc = s->face->gc;
2668 xassert (s->gc != 0);
2669 }
2670
2671
2672 /* Set S->gc of glyph string S for drawing that glyph string. Set
2673 S->stippled_p to a non-zero value if the face of S has a stipple
2674 pattern. */
2675
2676 static INLINE void
2677 x_set_glyph_string_gc (s)
2678 struct glyph_string *s;
2679 {
2680 if (s->hl == DRAW_NORMAL_TEXT)
2681 {
2682 s->gc = s->face->gc;
2683 s->stippled_p = s->face->stipple != 0;
2684 }
2685 else if (s->hl == DRAW_INVERSE_VIDEO)
2686 {
2687 x_set_mode_line_face_gc (s);
2688 s->stippled_p = s->face->stipple != 0;
2689 }
2690 else if (s->hl == DRAW_CURSOR)
2691 {
2692 x_set_cursor_gc (s);
2693 s->stippled_p = 0;
2694 }
2695 else if (s->hl == DRAW_MOUSE_FACE)
2696 {
2697 x_set_mouse_face_gc (s);
2698 s->stippled_p = s->face->stipple != 0;
2699 }
2700 else if (s->hl == DRAW_IMAGE_RAISED
2701 || s->hl == DRAW_IMAGE_SUNKEN)
2702 {
2703 s->gc = s->face->gc;
2704 s->stippled_p = s->face->stipple != 0;
2705 }
2706 else
2707 {
2708 s->gc = s->face->gc;
2709 s->stippled_p = s->face->stipple != 0;
2710 }
2711
2712 /* GC must have been set. */
2713 xassert (s->gc != 0);
2714 }
2715
2716
2717 /* Return in *R the clipping rectangle for glyph string S. */
2718
2719 static void
2720 x_get_glyph_string_clip_rect (s, r)
2721 struct glyph_string *s;
2722 XRectangle *r;
2723 {
2724 if (s->row->full_width_p)
2725 {
2726 /* Draw full-width. X coordinates are relative to S->w->left. */
2727 int canon_x = CANON_X_UNIT (s->f);
2728
2729 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2730 r->width = XFASTINT (s->w->width) * canon_x;
2731
2732 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2733 {
2734 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2735 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2736 r->x -= width;
2737 }
2738
2739 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2740
2741 /* Unless displaying a mode or menu bar line, which are always
2742 fully visible, clip to the visible part of the row. */
2743 if (s->w->pseudo_window_p)
2744 r->height = s->row->visible_height;
2745 else
2746 r->height = s->height;
2747 }
2748 else
2749 {
2750 /* This is a text line that may be partially visible. */
2751 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2752 r->width = window_box_width (s->w, s->area);
2753 r->height = s->row->visible_height;
2754 }
2755
2756 /* Don't use S->y for clipping because it doesn't take partially
2757 visible lines into account. For example, it can be negative for
2758 partially visible lines at the top of a window. */
2759 if (!s->row->full_width_p
2760 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2761 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2762 else
2763 r->y = max (0, s->row->y);
2764
2765 /* If drawing a tool-bar window, draw it over the internal border
2766 at the top of the window. */
2767 if (s->w == XWINDOW (s->f->tool_bar_window))
2768 r->y -= s->f->output_data.x->internal_border_width;
2769
2770 /* If S draws overlapping rows, it's sufficient to use the top and
2771 bottom of the window for clipping because this glyph string
2772 intentionally draws over other lines. */
2773 if (s->for_overlaps_p)
2774 {
2775 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2776 r->height = window_text_bottom_y (s->w) - r->y;
2777 }
2778
2779 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2780 }
2781
2782
2783 /* Set clipping for output of glyph string S. S may be part of a mode
2784 line or menu if we don't have X toolkit support. */
2785
2786 static INLINE void
2787 x_set_glyph_string_clipping (s)
2788 struct glyph_string *s;
2789 {
2790 XRectangle r;
2791 x_get_glyph_string_clip_rect (s, &r);
2792 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2793 }
2794
2795
2796 /* Compute left and right overhang of glyph string S. If S is a glyph
2797 string for a composition, assume overhangs don't exist. */
2798
2799 static INLINE void
2800 x_compute_glyph_string_overhangs (s)
2801 struct glyph_string *s;
2802 {
2803 if (s->cmp == NULL
2804 && s->first_glyph->type == CHAR_GLYPH)
2805 {
2806 XCharStruct cs;
2807 int direction, font_ascent, font_descent;
2808 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2809 &font_ascent, &font_descent, &cs);
2810 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2811 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2812 }
2813 }
2814
2815
2816 /* Compute overhangs and x-positions for glyph string S and its
2817 predecessors, or successors. X is the starting x-position for S.
2818 BACKWARD_P non-zero means process predecessors. */
2819
2820 static void
2821 x_compute_overhangs_and_x (s, x, backward_p)
2822 struct glyph_string *s;
2823 int x;
2824 int backward_p;
2825 {
2826 if (backward_p)
2827 {
2828 while (s)
2829 {
2830 x_compute_glyph_string_overhangs (s);
2831 x -= s->width;
2832 s->x = x;
2833 s = s->prev;
2834 }
2835 }
2836 else
2837 {
2838 while (s)
2839 {
2840 x_compute_glyph_string_overhangs (s);
2841 s->x = x;
2842 x += s->width;
2843 s = s->next;
2844 }
2845 }
2846 }
2847
2848
2849 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2850 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2851 assumed to be zero. */
2852
2853 static void
2854 x_get_glyph_overhangs (glyph, f, left, right)
2855 struct glyph *glyph;
2856 struct frame *f;
2857 int *left, *right;
2858 {
2859 *left = *right = 0;
2860
2861 if (glyph->type == CHAR_GLYPH)
2862 {
2863 XFontStruct *font;
2864 struct face *face;
2865 struct font_info *font_info;
2866 XChar2b char2b;
2867 XCharStruct *pcm;
2868
2869 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2870 font = face->font;
2871 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2872 if (font
2873 && (pcm = x_per_char_metric (font, &char2b)))
2874 {
2875 if (pcm->rbearing > pcm->width)
2876 *right = pcm->rbearing - pcm->width;
2877 if (pcm->lbearing < 0)
2878 *left = -pcm->lbearing;
2879 }
2880 }
2881 }
2882
2883
2884 /* Return the index of the first glyph preceding glyph string S that
2885 is overwritten by S because of S's left overhang. Value is -1
2886 if no glyphs are overwritten. */
2887
2888 static int
2889 x_left_overwritten (s)
2890 struct glyph_string *s;
2891 {
2892 int k;
2893
2894 if (s->left_overhang)
2895 {
2896 int x = 0, i;
2897 struct glyph *glyphs = s->row->glyphs[s->area];
2898 int first = s->first_glyph - glyphs;
2899
2900 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2901 x -= glyphs[i].pixel_width;
2902
2903 k = i + 1;
2904 }
2905 else
2906 k = -1;
2907
2908 return k;
2909 }
2910
2911
2912 /* Return the index of the first glyph preceding glyph string S that
2913 is overwriting S because of its right overhang. Value is -1 if no
2914 glyph in front of S overwrites S. */
2915
2916 static int
2917 x_left_overwriting (s)
2918 struct glyph_string *s;
2919 {
2920 int i, k, x;
2921 struct glyph *glyphs = s->row->glyphs[s->area];
2922 int first = s->first_glyph - glyphs;
2923
2924 k = -1;
2925 x = 0;
2926 for (i = first - 1; i >= 0; --i)
2927 {
2928 int left, right;
2929 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2930 if (x + right > 0)
2931 k = i;
2932 x -= glyphs[i].pixel_width;
2933 }
2934
2935 return k;
2936 }
2937
2938
2939 /* Return the index of the last glyph following glyph string S that is
2940 not overwritten by S because of S's right overhang. Value is -1 if
2941 no such glyph is found. */
2942
2943 static int
2944 x_right_overwritten (s)
2945 struct glyph_string *s;
2946 {
2947 int k = -1;
2948
2949 if (s->right_overhang)
2950 {
2951 int x = 0, i;
2952 struct glyph *glyphs = s->row->glyphs[s->area];
2953 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2954 int end = s->row->used[s->area];
2955
2956 for (i = first; i < end && s->right_overhang > x; ++i)
2957 x += glyphs[i].pixel_width;
2958
2959 k = i;
2960 }
2961
2962 return k;
2963 }
2964
2965
2966 /* Return the index of the last glyph following glyph string S that
2967 overwrites S because of its left overhang. Value is negative
2968 if no such glyph is found. */
2969
2970 static int
2971 x_right_overwriting (s)
2972 struct glyph_string *s;
2973 {
2974 int i, k, x;
2975 int end = s->row->used[s->area];
2976 struct glyph *glyphs = s->row->glyphs[s->area];
2977 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2978
2979 k = -1;
2980 x = 0;
2981 for (i = first; i < end; ++i)
2982 {
2983 int left, right;
2984 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2985 if (x - left < 0)
2986 k = i;
2987 x += glyphs[i].pixel_width;
2988 }
2989
2990 return k;
2991 }
2992
2993
2994 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2995
2996 static INLINE void
2997 x_clear_glyph_string_rect (s, x, y, w, h)
2998 struct glyph_string *s;
2999 int x, y, w, h;
3000 {
3001 XGCValues xgcv;
3002 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3003 XSetForeground (s->display, s->gc, xgcv.background);
3004 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3005 XSetForeground (s->display, s->gc, xgcv.foreground);
3006 }
3007
3008
3009 /* Draw the background of glyph_string S. If S->background_filled_p
3010 is non-zero don't draw it. FORCE_P non-zero means draw the
3011 background even if it wouldn't be drawn normally. This is used
3012 when a string preceding S draws into the background of S, or S
3013 contains the first component of a composition. */
3014
3015 static void
3016 x_draw_glyph_string_background (s, force_p)
3017 struct glyph_string *s;
3018 int force_p;
3019 {
3020 /* Nothing to do if background has already been drawn or if it
3021 shouldn't be drawn in the first place. */
3022 if (!s->background_filled_p)
3023 {
3024 if (s->stippled_p)
3025 {
3026 /* Fill background with a stipple pattern. */
3027 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3028 XFillRectangle (s->display, s->window, s->gc, s->x,
3029 s->y + s->face->box_line_width,
3030 s->background_width,
3031 s->height - 2 * s->face->box_line_width);
3032 XSetFillStyle (s->display, s->gc, FillSolid);
3033 s->background_filled_p = 1;
3034 }
3035 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3036 || s->font_not_found_p
3037 || s->extends_to_end_of_line_p
3038 || force_p)
3039 {
3040 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3041 s->background_width,
3042 s->height - 2 * s->face->box_line_width);
3043 s->background_filled_p = 1;
3044 }
3045 }
3046 }
3047
3048
3049 /* Draw the foreground of glyph string S. */
3050
3051 static void
3052 x_draw_glyph_string_foreground (s)
3053 struct glyph_string *s;
3054 {
3055 int i, x;
3056
3057 /* If first glyph of S has a left box line, start drawing the text
3058 of S to the right of that box line. */
3059 if (s->face->box != FACE_NO_BOX
3060 && s->first_glyph->left_box_line_p)
3061 x = s->x + s->face->box_line_width;
3062 else
3063 x = s->x;
3064
3065 /* Draw characters of S as rectangles if S's font could not be
3066 loaded. */
3067 if (s->font_not_found_p)
3068 {
3069 for (i = 0; i < s->nchars; ++i)
3070 {
3071 struct glyph *g = s->first_glyph + i;
3072 XDrawRectangle (s->display, s->window,
3073 s->gc, x, s->y, g->pixel_width - 1,
3074 s->height - 1);
3075 x += g->pixel_width;
3076 }
3077 }
3078 else
3079 {
3080 char *char1b = (char *) s->char2b;
3081 int boff = s->font_info->baseline_offset;
3082
3083 if (s->font_info->vertical_centering)
3084 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3085
3086 /* If we can use 8-bit functions, condense S->char2b. */
3087 if (!s->two_byte_p)
3088 for (i = 0; i < s->nchars; ++i)
3089 char1b[i] = s->char2b[i].byte2;
3090
3091 /* Draw text with XDrawString if background has already been
3092 filled. Otherwise, use XDrawImageString. (Note that
3093 XDrawImageString is usually faster than XDrawString.) Always
3094 use XDrawImageString when drawing the cursor so that there is
3095 no chance that characters under a box cursor are invisible. */
3096 if (s->for_overlaps_p
3097 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3098 {
3099 /* Draw characters with 16-bit or 8-bit functions. */
3100 if (s->two_byte_p)
3101 XDrawString16 (s->display, s->window, s->gc, x,
3102 s->ybase - boff, s->char2b, s->nchars);
3103 else
3104 XDrawString (s->display, s->window, s->gc, x,
3105 s->ybase - boff, char1b, s->nchars);
3106 }
3107 else
3108 {
3109 if (s->two_byte_p)
3110 XDrawImageString16 (s->display, s->window, s->gc, x,
3111 s->ybase - boff, s->char2b, s->nchars);
3112 else
3113 XDrawImageString (s->display, s->window, s->gc, x,
3114 s->ybase - boff, char1b, s->nchars);
3115 }
3116 }
3117 }
3118
3119 /* Draw the foreground of composite glyph string S. */
3120
3121 static void
3122 x_draw_composite_glyph_string_foreground (s)
3123 struct glyph_string *s;
3124 {
3125 int i, x;
3126
3127 /* If first glyph of S has a left box line, start drawing the text
3128 of S to the right of that box line. */
3129 if (s->face->box != FACE_NO_BOX
3130 && s->first_glyph->left_box_line_p)
3131 x = s->x + s->face->box_line_width;
3132 else
3133 x = s->x;
3134
3135 /* S is a glyph string for a composition. S->gidx is the index of
3136 the first character drawn for glyphs of this composition.
3137 S->gidx == 0 means we are drawing the very first character of
3138 this composition. */
3139
3140 /* Draw a rectangle for the composition if the font for the very
3141 first character of the composition could not be loaded. */
3142 if (s->font_not_found_p)
3143 {
3144 if (s->gidx == 0)
3145 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3146 s->width - 1, s->height - 1);
3147 }
3148 else
3149 {
3150 for (i = 0; i < s->nchars; i++, ++s->gidx)
3151 XDrawString16 (s->display, s->window, s->gc,
3152 x + s->cmp->offsets[s->gidx * 2],
3153 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3154 s->char2b + i, 1);
3155 }
3156 }
3157
3158
3159 #ifdef USE_X_TOOLKIT
3160
3161 static struct frame *x_frame_of_widget P_ ((Widget));
3162
3163
3164 /* Return the frame on which widget WIDGET is used.. Abort if frame
3165 cannot be determined. */
3166
3167 struct frame *
3168 x_frame_of_widget (widget)
3169 Widget widget;
3170 {
3171 struct x_display_info *dpyinfo;
3172 Lisp_Object tail;
3173 struct frame *f;
3174
3175 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3176
3177 /* Find the top-level shell of the widget. Note that this function
3178 can be called when the widget is not yet realized, so XtWindow
3179 (widget) == 0. That's the reason we can't simply use
3180 x_any_window_to_frame. */
3181 while (!XtIsTopLevelShell (widget))
3182 widget = XtParent (widget);
3183
3184 /* Look for a frame with that top-level widget. Allocate the color
3185 on that frame to get the right gamma correction value. */
3186 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3187 if (GC_FRAMEP (XCAR (tail))
3188 && (f = XFRAME (XCAR (tail)),
3189 (f->output_data.nothing != 1
3190 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3191 && f->output_data.x->widget == widget)
3192 return f;
3193
3194 abort ();
3195 }
3196
3197
3198 /* Allocate the color COLOR->pixel on the screen and display of
3199 widget WIDGET in colormap CMAP. If an exact match cannot be
3200 allocated, try the nearest color available. Value is non-zero
3201 if successful. This is called from lwlib. */
3202
3203 int
3204 x_alloc_nearest_color_for_widget (widget, cmap, color)
3205 Widget widget;
3206 Colormap cmap;
3207 XColor *color;
3208 {
3209 struct frame *f = x_frame_of_widget (widget);
3210 return x_alloc_nearest_color (f, cmap, color);
3211 }
3212
3213
3214 #endif /* USE_X_TOOLKIT */
3215
3216
3217 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3218 CMAP. If an exact match can't be allocated, try the nearest color
3219 available. Value is non-zero if successful. Set *COLOR to the
3220 color allocated. */
3221
3222 int
3223 x_alloc_nearest_color (f, cmap, color)
3224 struct frame *f;
3225 Colormap cmap;
3226 XColor *color;
3227 {
3228 Display *display = FRAME_X_DISPLAY (f);
3229 Screen *screen = FRAME_X_SCREEN (f);
3230 int rc;
3231
3232 gamma_correct (f, color);
3233 rc = XAllocColor (display, cmap, color);
3234 if (rc == 0)
3235 {
3236 /* If we got to this point, the colormap is full, so we're going
3237 to try to get the next closest color. The algorithm used is
3238 a least-squares matching, which is what X uses for closest
3239 color matching with StaticColor visuals. */
3240 int nearest, i;
3241 unsigned long nearest_delta = ~0;
3242 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3243 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3244
3245 for (i = 0; i < ncells; ++i)
3246 cells[i].pixel = i;
3247 XQueryColors (display, cmap, cells, ncells);
3248
3249 for (nearest = i = 0; i < ncells; ++i)
3250 {
3251 long dred = (color->red >> 8) - (cells[i].red >> 8);
3252 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3253 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3254 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3255
3256 if (delta < nearest_delta)
3257 {
3258 nearest = i;
3259 nearest_delta = delta;
3260 }
3261 }
3262
3263 color->red = cells[nearest].red;
3264 color->green = cells[nearest].green;
3265 color->blue = cells[nearest].blue;
3266 rc = XAllocColor (display, cmap, color);
3267 }
3268
3269 #ifdef DEBUG_X_COLORS
3270 if (rc)
3271 register_color (color->pixel);
3272 #endif /* DEBUG_X_COLORS */
3273
3274 return rc;
3275 }
3276
3277
3278 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3279 It's necessary to do this instead of just using PIXEL directly to
3280 get color reference counts right. */
3281
3282 unsigned long
3283 x_copy_color (f, pixel)
3284 struct frame *f;
3285 unsigned long pixel;
3286 {
3287 XColor color;
3288
3289 color.pixel = pixel;
3290 BLOCK_INPUT;
3291 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3292 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3293 UNBLOCK_INPUT;
3294 #ifdef DEBUG_X_COLORS
3295 register_color (pixel);
3296 #endif
3297 return color.pixel;
3298 }
3299
3300
3301 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3302 It's necessary to do this instead of just using PIXEL directly to
3303 get color reference counts right. */
3304
3305 unsigned long
3306 x_copy_dpy_color (dpy, cmap, pixel)
3307 Display *dpy;
3308 Colormap cmap;
3309 unsigned long pixel;
3310 {
3311 XColor color;
3312
3313 color.pixel = pixel;
3314 BLOCK_INPUT;
3315 XQueryColor (dpy, cmap, &color);
3316 XAllocColor (dpy, cmap, &color);
3317 UNBLOCK_INPUT;
3318 #ifdef DEBUG_X_COLORS
3319 register_color (pixel);
3320 #endif
3321 return color.pixel;
3322 }
3323
3324
3325 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3326 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3327 If this produces the same color as PIXEL, try a color where all RGB
3328 values have DELTA added. Return the allocated color in *PIXEL.
3329 DISPLAY is the X display, CMAP is the colormap to operate on.
3330 Value is non-zero if successful. */
3331
3332 static int
3333 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3334 struct frame *f;
3335 Display *display;
3336 Colormap cmap;
3337 unsigned long *pixel;
3338 double factor;
3339 int delta;
3340 {
3341 XColor color, new;
3342 int success_p;
3343
3344 /* Get RGB color values. */
3345 color.pixel = *pixel;
3346 XQueryColor (display, cmap, &color);
3347
3348 /* Change RGB values by specified FACTOR. Avoid overflow! */
3349 xassert (factor >= 0);
3350 new.red = min (0xffff, factor * color.red);
3351 new.green = min (0xffff, factor * color.green);
3352 new.blue = min (0xffff, factor * color.blue);
3353
3354 /* Try to allocate the color. */
3355 success_p = x_alloc_nearest_color (f, cmap, &new);
3356 if (success_p)
3357 {
3358 if (new.pixel == *pixel)
3359 {
3360 /* If we end up with the same color as before, try adding
3361 delta to the RGB values. */
3362 x_free_colors (f, &new.pixel, 1);
3363
3364 new.red = min (0xffff, delta + color.red);
3365 new.green = min (0xffff, delta + color.green);
3366 new.blue = min (0xffff, delta + color.blue);
3367 success_p = x_alloc_nearest_color (f, cmap, &new);
3368 }
3369 else
3370 success_p = 1;
3371 *pixel = new.pixel;
3372 }
3373
3374 return success_p;
3375 }
3376
3377
3378 /* Set up the foreground color for drawing relief lines of glyph
3379 string S. RELIEF is a pointer to a struct relief containing the GC
3380 with which lines will be drawn. Use a color that is FACTOR or
3381 DELTA lighter or darker than the relief's background which is found
3382 in S->f->output_data.x->relief_background. If such a color cannot
3383 be allocated, use DEFAULT_PIXEL, instead. */
3384
3385 static void
3386 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3387 struct frame *f;
3388 struct relief *relief;
3389 double factor;
3390 int delta;
3391 unsigned long default_pixel;
3392 {
3393 XGCValues xgcv;
3394 struct x_output *di = f->output_data.x;
3395 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3396 unsigned long pixel;
3397 unsigned long background = di->relief_background;
3398 Colormap cmap = FRAME_X_COLORMAP (f);
3399 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3400 Display *dpy = FRAME_X_DISPLAY (f);
3401
3402 xgcv.graphics_exposures = False;
3403 xgcv.line_width = 1;
3404
3405 /* Free previously allocated color. The color cell will be reused
3406 when it has been freed as many times as it was allocated, so this
3407 doesn't affect faces using the same colors. */
3408 if (relief->gc
3409 && relief->allocated_p)
3410 {
3411 x_free_colors (f, &relief->pixel, 1);
3412 relief->allocated_p = 0;
3413 }
3414
3415 /* Allocate new color. */
3416 xgcv.foreground = default_pixel;
3417 pixel = background;
3418 if (dpyinfo->n_planes != 1
3419 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3420 {
3421 relief->allocated_p = 1;
3422 xgcv.foreground = relief->pixel = pixel;
3423 }
3424
3425 if (relief->gc == 0)
3426 {
3427 xgcv.stipple = dpyinfo->gray;
3428 mask |= GCStipple;
3429 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3430 }
3431 else
3432 XChangeGC (dpy, relief->gc, mask, &xgcv);
3433 }
3434
3435
3436 /* Set up colors for the relief lines around glyph string S. */
3437
3438 static void
3439 x_setup_relief_colors (s)
3440 struct glyph_string *s;
3441 {
3442 struct x_output *di = s->f->output_data.x;
3443 unsigned long color;
3444
3445 if (s->face->use_box_color_for_shadows_p)
3446 color = s->face->box_color;
3447 else
3448 {
3449 XGCValues xgcv;
3450
3451 /* Get the background color of the face. */
3452 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3453 color = xgcv.background;
3454 }
3455
3456 if (di->white_relief.gc == 0
3457 || color != di->relief_background)
3458 {
3459 di->relief_background = color;
3460 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3461 WHITE_PIX_DEFAULT (s->f));
3462 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3463 BLACK_PIX_DEFAULT (s->f));
3464 }
3465 }
3466
3467
3468 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3469 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3470 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3471 relief. LEFT_P non-zero means draw a relief on the left side of
3472 the rectangle. RIGHT_P non-zero means draw a relief on the right
3473 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3474 when drawing. */
3475
3476 static void
3477 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3478 raised_p, left_p, right_p, clip_rect)
3479 struct frame *f;
3480 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3481 XRectangle *clip_rect;
3482 {
3483 int i;
3484 GC gc;
3485
3486 if (raised_p)
3487 gc = f->output_data.x->white_relief.gc;
3488 else
3489 gc = f->output_data.x->black_relief.gc;
3490 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3491
3492 /* Top. */
3493 for (i = 0; i < width; ++i)
3494 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3495 left_x + i * left_p, top_y + i,
3496 right_x + 1 - i * right_p, top_y + i);
3497
3498 /* Left. */
3499 if (left_p)
3500 for (i = 0; i < width; ++i)
3501 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3502 left_x + i, top_y + i, left_x + i, bottom_y - i);
3503
3504 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3505 if (raised_p)
3506 gc = f->output_data.x->black_relief.gc;
3507 else
3508 gc = f->output_data.x->white_relief.gc;
3509 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3510
3511 /* Bottom. */
3512 for (i = 0; i < width; ++i)
3513 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3514 left_x + i * left_p, bottom_y - i,
3515 right_x + 1 - i * right_p, bottom_y - i);
3516
3517 /* Right. */
3518 if (right_p)
3519 for (i = 0; i < width; ++i)
3520 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3521 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3522
3523 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3524 }
3525
3526
3527 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3528 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3529 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3530 left side of the rectangle. RIGHT_P non-zero means draw a line
3531 on the right side of the rectangle. CLIP_RECT is the clipping
3532 rectangle to use when drawing. */
3533
3534 static void
3535 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3536 left_p, right_p, clip_rect)
3537 struct glyph_string *s;
3538 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3539 XRectangle *clip_rect;
3540 {
3541 XGCValues xgcv;
3542
3543 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3544 XSetForeground (s->display, s->gc, s->face->box_color);
3545 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3546
3547 /* Top. */
3548 XFillRectangle (s->display, s->window, s->gc,
3549 left_x, top_y, right_x - left_x, width);
3550
3551 /* Left. */
3552 if (left_p)
3553 XFillRectangle (s->display, s->window, s->gc,
3554 left_x, top_y, width, bottom_y - top_y);
3555
3556 /* Bottom. */
3557 XFillRectangle (s->display, s->window, s->gc,
3558 left_x, bottom_y - width, right_x - left_x, width);
3559
3560 /* Right. */
3561 if (right_p)
3562 XFillRectangle (s->display, s->window, s->gc,
3563 right_x - width, top_y, width, bottom_y - top_y);
3564
3565 XSetForeground (s->display, s->gc, xgcv.foreground);
3566 XSetClipMask (s->display, s->gc, None);
3567 }
3568
3569
3570 /* Draw a box around glyph string S. */
3571
3572 static void
3573 x_draw_glyph_string_box (s)
3574 struct glyph_string *s;
3575 {
3576 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3577 int left_p, right_p;
3578 struct glyph *last_glyph;
3579 XRectangle clip_rect;
3580
3581 last_x = window_box_right (s->w, s->area);
3582 if (s->row->full_width_p
3583 && !s->w->pseudo_window_p)
3584 {
3585 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3586 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3587 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3588 }
3589
3590 /* The glyph that may have a right box line. */
3591 last_glyph = (s->cmp || s->img
3592 ? s->first_glyph
3593 : s->first_glyph + s->nchars - 1);
3594
3595 width = s->face->box_line_width;
3596 raised_p = s->face->box == FACE_RAISED_BOX;
3597 left_x = s->x;
3598 right_x = ((s->row->full_width_p
3599 ? last_x - 1
3600 : min (last_x, s->x + s->background_width) - 1));
3601 top_y = s->y;
3602 bottom_y = top_y + s->height - 1;
3603
3604 left_p = (s->first_glyph->left_box_line_p
3605 || (s->hl == DRAW_MOUSE_FACE
3606 && (s->prev == NULL
3607 || s->prev->hl != s->hl)));
3608 right_p = (last_glyph->right_box_line_p
3609 || (s->hl == DRAW_MOUSE_FACE
3610 && (s->next == NULL
3611 || s->next->hl != s->hl)));
3612
3613 x_get_glyph_string_clip_rect (s, &clip_rect);
3614
3615 if (s->face->box == FACE_SIMPLE_BOX)
3616 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3617 left_p, right_p, &clip_rect);
3618 else
3619 {
3620 x_setup_relief_colors (s);
3621 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3622 width, raised_p, left_p, right_p, &clip_rect);
3623 }
3624 }
3625
3626
3627 /* Draw foreground of image glyph string S. */
3628
3629 static void
3630 x_draw_image_foreground (s)
3631 struct glyph_string *s;
3632 {
3633 int x;
3634 int y = s->ybase - image_ascent (s->img, s->face);
3635
3636 /* If first glyph of S has a left box line, start drawing it to the
3637 right of that line. */
3638 if (s->face->box != FACE_NO_BOX
3639 && s->first_glyph->left_box_line_p)
3640 x = s->x + s->face->box_line_width;
3641 else
3642 x = s->x;
3643
3644 /* If there is a margin around the image, adjust x- and y-position
3645 by that margin. */
3646 if (s->img->margin)
3647 {
3648 x += s->img->margin;
3649 y += s->img->margin;
3650 }
3651
3652 if (s->img->pixmap)
3653 {
3654 if (s->img->mask)
3655 {
3656 /* We can't set both a clip mask and use XSetClipRectangles
3657 because the latter also sets a clip mask. We also can't
3658 trust on the shape extension to be available
3659 (XShapeCombineRegion). So, compute the rectangle to draw
3660 manually. */
3661 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3662 | GCFunction);
3663 XGCValues xgcv;
3664 XRectangle clip_rect, image_rect, r;
3665
3666 xgcv.clip_mask = s->img->mask;
3667 xgcv.clip_x_origin = x;
3668 xgcv.clip_y_origin = y;
3669 xgcv.function = GXcopy;
3670 XChangeGC (s->display, s->gc, mask, &xgcv);
3671
3672 x_get_glyph_string_clip_rect (s, &clip_rect);
3673 image_rect.x = x;
3674 image_rect.y = y;
3675 image_rect.width = s->img->width;
3676 image_rect.height = s->img->height;
3677 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3678 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3679 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3680 }
3681 else
3682 {
3683 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3684 0, 0, s->img->width, s->img->height, x, y);
3685
3686 /* When the image has a mask, we can expect that at
3687 least part of a mouse highlight or a block cursor will
3688 be visible. If the image doesn't have a mask, make
3689 a block cursor visible by drawing a rectangle around
3690 the image. I believe it's looking better if we do
3691 nothing here for mouse-face. */
3692 if (s->hl == DRAW_CURSOR)
3693 XDrawRectangle (s->display, s->window, s->gc, x, y,
3694 s->img->width - 1, s->img->height - 1);
3695 }
3696 }
3697 else
3698 /* Draw a rectangle if image could not be loaded. */
3699 XDrawRectangle (s->display, s->window, s->gc, x, y,
3700 s->img->width - 1, s->img->height - 1);
3701 }
3702
3703
3704 /* Draw a relief around the image glyph string S. */
3705
3706 static void
3707 x_draw_image_relief (s)
3708 struct glyph_string *s;
3709 {
3710 int x0, y0, x1, y1, thick, raised_p;
3711 XRectangle r;
3712 int x;
3713 int y = s->ybase - image_ascent (s->img, s->face);
3714
3715 /* If first glyph of S has a left box line, start drawing it to the
3716 right of that line. */
3717 if (s->face->box != FACE_NO_BOX
3718 && s->first_glyph->left_box_line_p)
3719 x = s->x + s->face->box_line_width;
3720 else
3721 x = s->x;
3722
3723 /* If there is a margin around the image, adjust x- and y-position
3724 by that margin. */
3725 if (s->img->margin)
3726 {
3727 x += s->img->margin;
3728 y += s->img->margin;
3729 }
3730
3731 if (s->hl == DRAW_IMAGE_SUNKEN
3732 || s->hl == DRAW_IMAGE_RAISED)
3733 {
3734 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3735 raised_p = s->hl == DRAW_IMAGE_RAISED;
3736 }
3737 else
3738 {
3739 thick = abs (s->img->relief);
3740 raised_p = s->img->relief > 0;
3741 }
3742
3743 x0 = x - thick;
3744 y0 = y - thick;
3745 x1 = x + s->img->width + thick - 1;
3746 y1 = y + s->img->height + thick - 1;
3747
3748 x_setup_relief_colors (s);
3749 x_get_glyph_string_clip_rect (s, &r);
3750 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3751 }
3752
3753
3754 /* Draw the foreground of image glyph string S to PIXMAP. */
3755
3756 static void
3757 x_draw_image_foreground_1 (s, pixmap)
3758 struct glyph_string *s;
3759 Pixmap pixmap;
3760 {
3761 int x;
3762 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3763
3764 /* If first glyph of S has a left box line, start drawing it to the
3765 right of that line. */
3766 if (s->face->box != FACE_NO_BOX
3767 && s->first_glyph->left_box_line_p)
3768 x = s->face->box_line_width;
3769 else
3770 x = 0;
3771
3772 /* If there is a margin around the image, adjust x- and y-position
3773 by that margin. */
3774 if (s->img->margin)
3775 {
3776 x += s->img->margin;
3777 y += s->img->margin;
3778 }
3779
3780 if (s->img->pixmap)
3781 {
3782 if (s->img->mask)
3783 {
3784 /* We can't set both a clip mask and use XSetClipRectangles
3785 because the latter also sets a clip mask. We also can't
3786 trust on the shape extension to be available
3787 (XShapeCombineRegion). So, compute the rectangle to draw
3788 manually. */
3789 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3790 | GCFunction);
3791 XGCValues xgcv;
3792
3793 xgcv.clip_mask = s->img->mask;
3794 xgcv.clip_x_origin = x;
3795 xgcv.clip_y_origin = y;
3796 xgcv.function = GXcopy;
3797 XChangeGC (s->display, s->gc, mask, &xgcv);
3798
3799 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3800 0, 0, s->img->width, s->img->height, x, y);
3801 XSetClipMask (s->display, s->gc, None);
3802 }
3803 else
3804 {
3805 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3806 0, 0, s->img->width, s->img->height, x, y);
3807
3808 /* When the image has a mask, we can expect that at
3809 least part of a mouse highlight or a block cursor will
3810 be visible. If the image doesn't have a mask, make
3811 a block cursor visible by drawing a rectangle around
3812 the image. I believe it's looking better if we do
3813 nothing here for mouse-face. */
3814 if (s->hl == DRAW_CURSOR)
3815 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3816 s->img->width - 1, s->img->height - 1);
3817 }
3818 }
3819 else
3820 /* Draw a rectangle if image could not be loaded. */
3821 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3822 s->img->width - 1, s->img->height - 1);
3823 }
3824
3825
3826 /* Draw part of the background of glyph string S. X, Y, W, and H
3827 give the rectangle to draw. */
3828
3829 static void
3830 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3831 struct glyph_string *s;
3832 int x, y, w, h;
3833 {
3834 if (s->stippled_p)
3835 {
3836 /* Fill background with a stipple pattern. */
3837 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3838 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3839 XSetFillStyle (s->display, s->gc, FillSolid);
3840 }
3841 else
3842 x_clear_glyph_string_rect (s, x, y, w, h);
3843 }
3844
3845
3846 /* Draw image glyph string S.
3847
3848 s->y
3849 s->x +-------------------------
3850 | s->face->box
3851 |
3852 | +-------------------------
3853 | | s->img->margin
3854 | |
3855 | | +-------------------
3856 | | | the image
3857
3858 */
3859
3860 static void
3861 x_draw_image_glyph_string (s)
3862 struct glyph_string *s;
3863 {
3864 int x, y;
3865 int box_line_width = s->face->box_line_width;
3866 int margin = s->img->margin;
3867 int height;
3868 Pixmap pixmap = None;
3869
3870 height = s->height - 2 * box_line_width;
3871
3872 /* Fill background with face under the image. Do it only if row is
3873 taller than image or if image has a clip mask to reduce
3874 flickering. */
3875 s->stippled_p = s->face->stipple != 0;
3876 if (height > s->img->height
3877 || margin
3878 || s->img->mask
3879 || s->img->pixmap == 0
3880 || s->width != s->background_width)
3881 {
3882 if (box_line_width && s->first_glyph->left_box_line_p)
3883 x = s->x + box_line_width;
3884 else
3885 x = s->x;
3886
3887 y = s->y + box_line_width;
3888
3889 if (s->img->mask)
3890 {
3891 /* Create a pixmap as large as the glyph string Fill it with
3892 the background color. Copy the image to it, using its
3893 mask. Copy the temporary pixmap to the display. */
3894 Screen *screen = FRAME_X_SCREEN (s->f);
3895 int depth = DefaultDepthOfScreen (screen);
3896
3897 /* Create a pixmap as large as the glyph string. */
3898 pixmap = XCreatePixmap (s->display, s->window,
3899 s->background_width,
3900 s->height, depth);
3901
3902 /* Don't clip in the following because we're working on the
3903 pixmap. */
3904 XSetClipMask (s->display, s->gc, None);
3905
3906 /* Fill the pixmap with the background color/stipple. */
3907 if (s->stippled_p)
3908 {
3909 /* Fill background with a stipple pattern. */
3910 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3911 XFillRectangle (s->display, pixmap, s->gc,
3912 0, 0, s->background_width, s->height);
3913 XSetFillStyle (s->display, s->gc, FillSolid);
3914 }
3915 else
3916 {
3917 XGCValues xgcv;
3918 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3919 &xgcv);
3920 XSetForeground (s->display, s->gc, xgcv.background);
3921 XFillRectangle (s->display, pixmap, s->gc,
3922 0, 0, s->background_width, s->height);
3923 XSetForeground (s->display, s->gc, xgcv.foreground);
3924 }
3925 }
3926 else
3927 /* Implementation idea: Is it possible to construct a mask?
3928 We could look at the color at the margins of the image, and
3929 say that this color is probably the background color of the
3930 image. */
3931 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3932
3933 s->background_filled_p = 1;
3934 }
3935
3936 /* Draw the foreground. */
3937 if (pixmap != None)
3938 {
3939 x_draw_image_foreground_1 (s, pixmap);
3940 x_set_glyph_string_clipping (s);
3941 XCopyArea (s->display, pixmap, s->window, s->gc,
3942 0, 0, s->background_width, s->height, s->x, s->y);
3943 XFreePixmap (s->display, pixmap);
3944 }
3945 else
3946 x_draw_image_foreground (s);
3947
3948 /* If we must draw a relief around the image, do it. */
3949 if (s->img->relief
3950 || s->hl == DRAW_IMAGE_RAISED
3951 || s->hl == DRAW_IMAGE_SUNKEN)
3952 x_draw_image_relief (s);
3953 }
3954
3955
3956 /* Draw stretch glyph string S. */
3957
3958 static void
3959 x_draw_stretch_glyph_string (s)
3960 struct glyph_string *s;
3961 {
3962 xassert (s->first_glyph->type == STRETCH_GLYPH);
3963 s->stippled_p = s->face->stipple != 0;
3964
3965 if (s->hl == DRAW_CURSOR
3966 && !x_stretch_cursor_p)
3967 {
3968 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3969 as wide as the stretch glyph. */
3970 int width = min (CANON_X_UNIT (s->f), s->background_width);
3971
3972 /* Draw cursor. */
3973 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3974
3975 /* Clear rest using the GC of the original non-cursor face. */
3976 if (width < s->background_width)
3977 {
3978 GC gc = s->face->gc;
3979 int x = s->x + width, y = s->y;
3980 int w = s->background_width - width, h = s->height;
3981 XRectangle r;
3982
3983 x_get_glyph_string_clip_rect (s, &r);
3984 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
3985
3986 if (s->face->stipple)
3987 {
3988 /* Fill background with a stipple pattern. */
3989 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3990 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3991 XSetFillStyle (s->display, gc, FillSolid);
3992 }
3993 else
3994 {
3995 XGCValues xgcv;
3996 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3997 XSetForeground (s->display, gc, xgcv.background);
3998 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3999 XSetForeground (s->display, gc, xgcv.foreground);
4000 }
4001 }
4002 }
4003 else
4004 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4005 s->height);
4006
4007 s->background_filled_p = 1;
4008 }
4009
4010
4011 /* Draw glyph string S. */
4012
4013 static void
4014 x_draw_glyph_string (s)
4015 struct glyph_string *s;
4016 {
4017 /* If S draws into the background of its successor, draw the
4018 background of the successor first so that S can draw into it.
4019 This makes S->next use XDrawString instead of XDrawImageString. */
4020 if (s->next && s->right_overhang && !s->for_overlaps_p)
4021 {
4022 xassert (s->next->img == NULL);
4023 x_set_glyph_string_gc (s->next);
4024 x_set_glyph_string_clipping (s->next);
4025 x_draw_glyph_string_background (s->next, 1);
4026 }
4027
4028 /* Set up S->gc, set clipping and draw S. */
4029 x_set_glyph_string_gc (s);
4030 x_set_glyph_string_clipping (s);
4031
4032 switch (s->first_glyph->type)
4033 {
4034 case IMAGE_GLYPH:
4035 x_draw_image_glyph_string (s);
4036 break;
4037
4038 case STRETCH_GLYPH:
4039 x_draw_stretch_glyph_string (s);
4040 break;
4041
4042 case CHAR_GLYPH:
4043 if (s->for_overlaps_p)
4044 s->background_filled_p = 1;
4045 else
4046 x_draw_glyph_string_background (s, 0);
4047 x_draw_glyph_string_foreground (s);
4048 break;
4049
4050 case COMPOSITE_GLYPH:
4051 if (s->for_overlaps_p || s->gidx > 0)
4052 s->background_filled_p = 1;
4053 else
4054 x_draw_glyph_string_background (s, 1);
4055 x_draw_composite_glyph_string_foreground (s);
4056 break;
4057
4058 default:
4059 abort ();
4060 }
4061
4062 if (!s->for_overlaps_p)
4063 {
4064 /* Draw underline. */
4065 if (s->face->underline_p)
4066 {
4067 unsigned long dy, h;
4068
4069 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4070 h = 1;
4071 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4072 dy = s->height - h;
4073
4074 if (s->face->underline_defaulted_p)
4075 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4076 s->width, h);
4077 else
4078 {
4079 XGCValues xgcv;
4080 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4081 XSetForeground (s->display, s->gc, s->face->underline_color);
4082 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4083 s->width, h);
4084 XSetForeground (s->display, s->gc, xgcv.foreground);
4085 }
4086 }
4087
4088 /* Draw overline. */
4089 if (s->face->overline_p)
4090 {
4091 unsigned long dy = 0, h = 1;
4092
4093 if (s->face->overline_color_defaulted_p)
4094 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4095 s->width, h);
4096 else
4097 {
4098 XGCValues xgcv;
4099 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4100 XSetForeground (s->display, s->gc, s->face->overline_color);
4101 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4102 s->width, h);
4103 XSetForeground (s->display, s->gc, xgcv.foreground);
4104 }
4105 }
4106
4107 /* Draw strike-through. */
4108 if (s->face->strike_through_p)
4109 {
4110 unsigned long h = 1;
4111 unsigned long dy = (s->height - h) / 2;
4112
4113 if (s->face->strike_through_color_defaulted_p)
4114 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4115 s->width, h);
4116 else
4117 {
4118 XGCValues xgcv;
4119 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4120 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4121 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4122 s->width, h);
4123 XSetForeground (s->display, s->gc, xgcv.foreground);
4124 }
4125 }
4126
4127 /* Draw relief. */
4128 if (s->face->box != FACE_NO_BOX)
4129 x_draw_glyph_string_box (s);
4130 }
4131
4132 /* Reset clipping. */
4133 XSetClipMask (s->display, s->gc, None);
4134 }
4135
4136
4137 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4138 struct face **, int));
4139
4140
4141 /* Load glyph string S with a composition components specified by S->cmp.
4142 FACES is an array of faces for all components of this composition.
4143 S->gidx is the index of the first component for S.
4144 OVERLAPS_P non-zero means S should draw the foreground only, and
4145 use its lines physical height for clipping.
4146
4147 Value is the index of a component not in S. */
4148
4149 static int
4150 x_fill_composite_glyph_string (s, faces, overlaps_p)
4151 struct glyph_string *s;
4152 struct face **faces;
4153 int overlaps_p;
4154 {
4155 int i;
4156
4157 xassert (s);
4158
4159 s->for_overlaps_p = overlaps_p;
4160
4161 s->face = faces[s->gidx];
4162 s->font = s->face->font;
4163 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4164
4165 /* For all glyphs of this composition, starting at the offset
4166 S->gidx, until we reach the end of the definition or encounter a
4167 glyph that requires the different face, add it to S. */
4168 ++s->nchars;
4169 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4170 ++s->nchars;
4171
4172 /* All glyph strings for the same composition has the same width,
4173 i.e. the width set for the first component of the composition. */
4174
4175 s->width = s->first_glyph->pixel_width;
4176
4177 /* If the specified font could not be loaded, use the frame's
4178 default font, but record the fact that we couldn't load it in
4179 the glyph string so that we can draw rectangles for the
4180 characters of the glyph string. */
4181 if (s->font == NULL)
4182 {
4183 s->font_not_found_p = 1;
4184 s->font = FRAME_FONT (s->f);
4185 }
4186
4187 /* Adjust base line for subscript/superscript text. */
4188 s->ybase += s->first_glyph->voffset;
4189
4190 xassert (s->face && s->face->gc);
4191
4192 /* This glyph string must always be drawn with 16-bit functions. */
4193 s->two_byte_p = 1;
4194
4195 return s->gidx + s->nchars;
4196 }
4197
4198
4199 /* Load glyph string S with a sequence characters.
4200 FACE_ID is the face id of the string. START is the index of the
4201 first glyph to consider, END is the index of the last + 1.
4202 OVERLAPS_P non-zero means S should draw the foreground only, and
4203 use its lines physical height for clipping.
4204
4205 Value is the index of the first glyph not in S. */
4206
4207 static int
4208 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4209 struct glyph_string *s;
4210 int face_id;
4211 int start, end, overlaps_p;
4212 {
4213 struct glyph *glyph, *last;
4214 int voffset;
4215 int glyph_not_available_p;
4216
4217 xassert (s->f == XFRAME (s->w->frame));
4218 xassert (s->nchars == 0);
4219 xassert (start >= 0 && end > start);
4220
4221 s->for_overlaps_p = overlaps_p,
4222 glyph = s->row->glyphs[s->area] + start;
4223 last = s->row->glyphs[s->area] + end;
4224 voffset = glyph->voffset;
4225
4226 glyph_not_available_p = glyph->glyph_not_available_p;
4227
4228 while (glyph < last
4229 && glyph->type == CHAR_GLYPH
4230 && glyph->voffset == voffset
4231 /* Same face id implies same font, nowadays. */
4232 && glyph->face_id == face_id
4233 && glyph->glyph_not_available_p == glyph_not_available_p)
4234 {
4235 int two_byte_p;
4236
4237 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4238 s->char2b + s->nchars,
4239 &two_byte_p);
4240 s->two_byte_p = two_byte_p;
4241 ++s->nchars;
4242 xassert (s->nchars <= end - start);
4243 s->width += glyph->pixel_width;
4244 ++glyph;
4245 }
4246
4247 s->font = s->face->font;
4248 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4249
4250 /* If the specified font could not be loaded, use the frame's font,
4251 but record the fact that we couldn't load it in
4252 S->font_not_found_p so that we can draw rectangles for the
4253 characters of the glyph string. */
4254 if (s->font == NULL || glyph_not_available_p)
4255 {
4256 s->font_not_found_p = 1;
4257 s->font = FRAME_FONT (s->f);
4258 }
4259
4260 /* Adjust base line for subscript/superscript text. */
4261 s->ybase += voffset;
4262
4263 xassert (s->face && s->face->gc);
4264 return glyph - s->row->glyphs[s->area];
4265 }
4266
4267
4268 /* Fill glyph string S from image glyph S->first_glyph. */
4269
4270 static void
4271 x_fill_image_glyph_string (s)
4272 struct glyph_string *s;
4273 {
4274 xassert (s->first_glyph->type == IMAGE_GLYPH);
4275 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4276 xassert (s->img);
4277 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4278 s->font = s->face->font;
4279 s->width = s->first_glyph->pixel_width;
4280
4281 /* Adjust base line for subscript/superscript text. */
4282 s->ybase += s->first_glyph->voffset;
4283 }
4284
4285
4286 /* Fill glyph string S from stretch glyph S->first_glyph. */
4287
4288 static void
4289 x_fill_stretch_glyph_string (s)
4290 struct glyph_string *s;
4291 {
4292 xassert (s->first_glyph->type == STRETCH_GLYPH);
4293 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4294 s->font = s->face->font;
4295 s->width = s->first_glyph->pixel_width;
4296
4297 /* Adjust base line for subscript/superscript text. */
4298 s->ybase += s->first_glyph->voffset;
4299 }
4300
4301
4302 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4303 of XChar2b structures for S; it can't be allocated in
4304 x_init_glyph_string because it must be allocated via `alloca'. W
4305 is the window on which S is drawn. ROW and AREA are the glyph row
4306 and area within the row from which S is constructed. START is the
4307 index of the first glyph structure covered by S. HL is a
4308 face-override for drawing S. */
4309
4310 static void
4311 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4312 struct glyph_string *s;
4313 XChar2b *char2b;
4314 struct window *w;
4315 struct glyph_row *row;
4316 enum glyph_row_area area;
4317 int start;
4318 enum draw_glyphs_face hl;
4319 {
4320 bzero (s, sizeof *s);
4321 s->w = w;
4322 s->f = XFRAME (w->frame);
4323 s->display = FRAME_X_DISPLAY (s->f);
4324 s->window = FRAME_X_WINDOW (s->f);
4325 s->char2b = char2b;
4326 s->hl = hl;
4327 s->row = row;
4328 s->area = area;
4329 s->first_glyph = row->glyphs[area] + start;
4330 s->height = row->height;
4331 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4332
4333 /* Display the internal border below the tool-bar window. */
4334 if (s->w == XWINDOW (s->f->tool_bar_window))
4335 s->y -= s->f->output_data.x->internal_border_width;
4336
4337 s->ybase = s->y + row->ascent;
4338 }
4339
4340
4341 /* Set background width of glyph string S. START is the index of the
4342 first glyph following S. LAST_X is the right-most x-position + 1
4343 in the drawing area. */
4344
4345 static INLINE void
4346 x_set_glyph_string_background_width (s, start, last_x)
4347 struct glyph_string *s;
4348 int start;
4349 int last_x;
4350 {
4351 /* If the face of this glyph string has to be drawn to the end of
4352 the drawing area, set S->extends_to_end_of_line_p. */
4353 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4354
4355 if (start == s->row->used[s->area]
4356 && s->hl == DRAW_NORMAL_TEXT
4357 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4358 || s->face->background != default_face->background
4359 || s->face->stipple != default_face->stipple))
4360 s->extends_to_end_of_line_p = 1;
4361
4362 /* If S extends its face to the end of the line, set its
4363 background_width to the distance to the right edge of the drawing
4364 area. */
4365 if (s->extends_to_end_of_line_p)
4366 s->background_width = last_x - s->x + 1;
4367 else
4368 s->background_width = s->width;
4369 }
4370
4371
4372 /* Add a glyph string for a stretch glyph to the list of strings
4373 between HEAD and TAIL. START is the index of the stretch glyph in
4374 row area AREA of glyph row ROW. END is the index of the last glyph
4375 in that glyph row area. X is the current output position assigned
4376 to the new glyph string constructed. HL overrides that face of the
4377 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4378 is the right-most x-position of the drawing area. */
4379
4380 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4381 and below -- keep them on one line. */
4382 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4383 do \
4384 { \
4385 s = (struct glyph_string *) alloca (sizeof *s); \
4386 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4387 x_fill_stretch_glyph_string (s); \
4388 x_append_glyph_string (&HEAD, &TAIL, s); \
4389 ++START; \
4390 s->x = (X); \
4391 } \
4392 while (0)
4393
4394
4395 /* Add a glyph string for an image glyph to the list of strings
4396 between HEAD and TAIL. START is the index of the image glyph in
4397 row area AREA of glyph row ROW. END is the index of the last glyph
4398 in that glyph row area. X is the current output position assigned
4399 to the new glyph string constructed. HL overrides that face of the
4400 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4401 is the right-most x-position of the drawing area. */
4402
4403 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4404 do \
4405 { \
4406 s = (struct glyph_string *) alloca (sizeof *s); \
4407 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4408 x_fill_image_glyph_string (s); \
4409 x_append_glyph_string (&HEAD, &TAIL, s); \
4410 ++START; \
4411 s->x = (X); \
4412 } \
4413 while (0)
4414
4415
4416 /* Add a glyph string for a sequence of character glyphs to the list
4417 of strings between HEAD and TAIL. START is the index of the first
4418 glyph in row area AREA of glyph row ROW that is part of the new
4419 glyph string. END is the index of the last glyph in that glyph row
4420 area. X is the current output position assigned to the new glyph
4421 string constructed. HL overrides that face of the glyph; e.g. it
4422 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4423 right-most x-position of the drawing area. */
4424
4425 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4426 do \
4427 { \
4428 int c, face_id; \
4429 XChar2b *char2b; \
4430 \
4431 c = (ROW)->glyphs[AREA][START].u.ch; \
4432 face_id = (ROW)->glyphs[AREA][START].face_id; \
4433 \
4434 s = (struct glyph_string *) alloca (sizeof *s); \
4435 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4436 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4437 x_append_glyph_string (&HEAD, &TAIL, s); \
4438 s->x = (X); \
4439 START = x_fill_glyph_string (s, face_id, START, END, \
4440 OVERLAPS_P); \
4441 } \
4442 while (0)
4443
4444
4445 /* Add a glyph string for a composite sequence to the list of strings
4446 between HEAD and TAIL. START is the index of the first glyph in
4447 row area AREA of glyph row ROW that is part of the new glyph
4448 string. END is the index of the last glyph in that glyph row area.
4449 X is the current output position assigned to the new glyph string
4450 constructed. HL overrides that face of the glyph; e.g. it is
4451 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4452 x-position of the drawing area. */
4453
4454 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4455 do { \
4456 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4457 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4458 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4459 struct composition *cmp = composition_table[cmp_id]; \
4460 int glyph_len = cmp->glyph_len; \
4461 XChar2b *char2b; \
4462 struct face **faces; \
4463 struct glyph_string *first_s = NULL; \
4464 int n; \
4465 \
4466 base_face = base_face->ascii_face; \
4467 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4468 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4469 /* At first, fill in `char2b' and `faces'. */ \
4470 for (n = 0; n < glyph_len; n++) \
4471 { \
4472 int c = COMPOSITION_GLYPH (cmp, n); \
4473 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4474 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4475 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4476 this_face_id, char2b + n, 1); \
4477 } \
4478 \
4479 /* Make glyph_strings for each glyph sequence that is drawable by \
4480 the same face, and append them to HEAD/TAIL. */ \
4481 for (n = 0; n < cmp->glyph_len;) \
4482 { \
4483 s = (struct glyph_string *) alloca (sizeof *s); \
4484 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4485 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4486 s->cmp = cmp; \
4487 s->gidx = n; \
4488 s->x = (X); \
4489 \
4490 if (n == 0) \
4491 first_s = s; \
4492 \
4493 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4494 } \
4495 \
4496 ++START; \
4497 s = first_s; \
4498 } while (0)
4499
4500
4501 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4502 of AREA of glyph row ROW on window W between indices START and END.
4503 HL overrides the face for drawing glyph strings, e.g. it is
4504 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4505 x-positions of the drawing area.
4506
4507 This is an ugly monster macro construct because we must use alloca
4508 to allocate glyph strings (because x_draw_glyphs can be called
4509 asynchronously). */
4510
4511 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4512 do \
4513 { \
4514 HEAD = TAIL = NULL; \
4515 while (START < END) \
4516 { \
4517 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4518 switch (first_glyph->type) \
4519 { \
4520 case CHAR_GLYPH: \
4521 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4522 TAIL, HL, X, LAST_X, \
4523 OVERLAPS_P); \
4524 break; \
4525 \
4526 case COMPOSITE_GLYPH: \
4527 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4528 HEAD, TAIL, HL, X, LAST_X,\
4529 OVERLAPS_P); \
4530 break; \
4531 \
4532 case STRETCH_GLYPH: \
4533 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4534 HEAD, TAIL, HL, X, LAST_X); \
4535 break; \
4536 \
4537 case IMAGE_GLYPH: \
4538 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4539 TAIL, HL, X, LAST_X); \
4540 break; \
4541 \
4542 default: \
4543 abort (); \
4544 } \
4545 \
4546 x_set_glyph_string_background_width (s, START, LAST_X); \
4547 (X) += s->width; \
4548 } \
4549 } \
4550 while (0)
4551
4552
4553 /* Draw glyphs between START and END in AREA of ROW on window W,
4554 starting at x-position X. X is relative to AREA in W. HL is a
4555 face-override with the following meaning:
4556
4557 DRAW_NORMAL_TEXT draw normally
4558 DRAW_CURSOR draw in cursor face
4559 DRAW_MOUSE_FACE draw in mouse face.
4560 DRAW_INVERSE_VIDEO draw in mode line face
4561 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4562 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4563
4564 If REAL_START is non-null, return in *REAL_START the real starting
4565 position for display. This can be different from START in case
4566 overlapping glyphs must be displayed. If REAL_END is non-null,
4567 return in *REAL_END the real end position for display. This can be
4568 different from END in case overlapping glyphs must be displayed.
4569
4570 If OVERLAPS_P is non-zero, draw only the foreground of characters
4571 and clip to the physical height of ROW.
4572
4573 Value is the x-position reached, relative to AREA of W. */
4574
4575 static int
4576 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4577 overlaps_p)
4578 struct window *w;
4579 int x;
4580 struct glyph_row *row;
4581 enum glyph_row_area area;
4582 int start, end;
4583 enum draw_glyphs_face hl;
4584 int *real_start, *real_end;
4585 int overlaps_p;
4586 {
4587 struct glyph_string *head, *tail;
4588 struct glyph_string *s;
4589 int last_x, area_width;
4590 int x_reached;
4591 int i, j;
4592
4593 /* Let's rather be paranoid than getting a SEGV. */
4594 start = max (0, start);
4595 end = min (end, row->used[area]);
4596 if (real_start)
4597 *real_start = start;
4598 if (real_end)
4599 *real_end = end;
4600
4601 /* Translate X to frame coordinates. Set last_x to the right
4602 end of the drawing area. */
4603 if (row->full_width_p)
4604 {
4605 /* X is relative to the left edge of W, without scroll bars
4606 or flag areas. */
4607 struct frame *f = XFRAME (w->frame);
4608 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4609 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4610
4611 x += window_left_x;
4612 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4613 last_x = window_left_x + area_width;
4614
4615 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4616 {
4617 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4618 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4619 last_x += width;
4620 else
4621 x -= width;
4622 }
4623
4624 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4625 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4626 }
4627 else
4628 {
4629 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4630 area_width = window_box_width (w, area);
4631 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4632 }
4633
4634 /* Build a doubly-linked list of glyph_string structures between
4635 head and tail from what we have to draw. Note that the macro
4636 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4637 the reason we use a separate variable `i'. */
4638 i = start;
4639 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4640 overlaps_p);
4641 if (tail)
4642 x_reached = tail->x + tail->background_width;
4643 else
4644 x_reached = x;
4645
4646 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4647 the row, redraw some glyphs in front or following the glyph
4648 strings built above. */
4649 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4650 {
4651 int dummy_x = 0;
4652 struct glyph_string *h, *t;
4653
4654 /* Compute overhangs for all glyph strings. */
4655 for (s = head; s; s = s->next)
4656 x_compute_glyph_string_overhangs (s);
4657
4658 /* Prepend glyph strings for glyphs in front of the first glyph
4659 string that are overwritten because of the first glyph
4660 string's left overhang. The background of all strings
4661 prepended must be drawn because the first glyph string
4662 draws over it. */
4663 i = x_left_overwritten (head);
4664 if (i >= 0)
4665 {
4666 j = i;
4667 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4668 DRAW_NORMAL_TEXT, dummy_x, last_x,
4669 overlaps_p);
4670 start = i;
4671 if (real_start)
4672 *real_start = start;
4673 x_compute_overhangs_and_x (t, head->x, 1);
4674 x_prepend_glyph_string_lists (&head, &tail, h, t);
4675 }
4676
4677 /* Prepend glyph strings for glyphs in front of the first glyph
4678 string that overwrite that glyph string because of their
4679 right overhang. For these strings, only the foreground must
4680 be drawn, because it draws over the glyph string at `head'.
4681 The background must not be drawn because this would overwrite
4682 right overhangs of preceding glyphs for which no glyph
4683 strings exist. */
4684 i = x_left_overwriting (head);
4685 if (i >= 0)
4686 {
4687 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4688 DRAW_NORMAL_TEXT, dummy_x, last_x,
4689 overlaps_p);
4690 for (s = h; s; s = s->next)
4691 s->background_filled_p = 1;
4692 if (real_start)
4693 *real_start = i;
4694 x_compute_overhangs_and_x (t, head->x, 1);
4695 x_prepend_glyph_string_lists (&head, &tail, h, t);
4696 }
4697
4698 /* Append glyphs strings for glyphs following the last glyph
4699 string tail that are overwritten by tail. The background of
4700 these strings has to be drawn because tail's foreground draws
4701 over it. */
4702 i = x_right_overwritten (tail);
4703 if (i >= 0)
4704 {
4705 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4706 DRAW_NORMAL_TEXT, x, last_x,
4707 overlaps_p);
4708 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4709 x_append_glyph_string_lists (&head, &tail, h, t);
4710 if (real_end)
4711 *real_end = i;
4712 }
4713
4714 /* Append glyph strings for glyphs following the last glyph
4715 string tail that overwrite tail. The foreground of such
4716 glyphs has to be drawn because it writes into the background
4717 of tail. The background must not be drawn because it could
4718 paint over the foreground of following glyphs. */
4719 i = x_right_overwriting (tail);
4720 if (i >= 0)
4721 {
4722 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4723 DRAW_NORMAL_TEXT, x, last_x,
4724 overlaps_p);
4725 for (s = h; s; s = s->next)
4726 s->background_filled_p = 1;
4727 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4728 x_append_glyph_string_lists (&head, &tail, h, t);
4729 if (real_end)
4730 *real_end = i;
4731 }
4732 }
4733
4734 /* Draw all strings. */
4735 for (s = head; s; s = s->next)
4736 x_draw_glyph_string (s);
4737
4738 /* Value is the x-position up to which drawn, relative to AREA of W.
4739 This doesn't include parts drawn because of overhangs. */
4740 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4741 if (!row->full_width_p)
4742 {
4743 if (area > LEFT_MARGIN_AREA)
4744 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4745 if (area > TEXT_AREA)
4746 x_reached -= window_box_width (w, TEXT_AREA);
4747 }
4748 return x_reached;
4749 }
4750
4751
4752 /* Fix the display of area AREA of overlapping row ROW in window W. */
4753
4754 static void
4755 x_fix_overlapping_area (w, row, area)
4756 struct window *w;
4757 struct glyph_row *row;
4758 enum glyph_row_area area;
4759 {
4760 int i, x;
4761
4762 BLOCK_INPUT;
4763
4764 if (area == LEFT_MARGIN_AREA)
4765 x = 0;
4766 else if (area == TEXT_AREA)
4767 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4768 else
4769 x = (window_box_width (w, LEFT_MARGIN_AREA)
4770 + window_box_width (w, TEXT_AREA));
4771
4772 for (i = 0; i < row->used[area];)
4773 {
4774 if (row->glyphs[area][i].overlaps_vertically_p)
4775 {
4776 int start = i, start_x = x;
4777
4778 do
4779 {
4780 x += row->glyphs[area][i].pixel_width;
4781 ++i;
4782 }
4783 while (i < row->used[area]
4784 && row->glyphs[area][i].overlaps_vertically_p);
4785
4786 x_draw_glyphs (w, start_x, row, area, start, i,
4787 (row->inverse_p
4788 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4789 NULL, NULL, 1);
4790 }
4791 else
4792 {
4793 x += row->glyphs[area][i].pixel_width;
4794 ++i;
4795 }
4796 }
4797
4798 UNBLOCK_INPUT;
4799 }
4800
4801
4802 /* Output LEN glyphs starting at START at the nominal cursor position.
4803 Advance the nominal cursor over the text. The global variable
4804 updated_window contains the window being updated, updated_row is
4805 the glyph row being updated, and updated_area is the area of that
4806 row being updated. */
4807
4808 static void
4809 x_write_glyphs (start, len)
4810 struct glyph *start;
4811 int len;
4812 {
4813 int x, hpos, real_start, real_end;
4814
4815 xassert (updated_window && updated_row);
4816 BLOCK_INPUT;
4817
4818 /* Write glyphs. */
4819
4820 hpos = start - updated_row->glyphs[updated_area];
4821 x = x_draw_glyphs (updated_window, output_cursor.x,
4822 updated_row, updated_area,
4823 hpos, hpos + len,
4824 (updated_row->inverse_p
4825 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4826 &real_start, &real_end, 0);
4827
4828 /* If we drew over the cursor, note that it is not visible any more. */
4829 note_overwritten_text_cursor (updated_window, real_start,
4830 real_end - real_start);
4831
4832 UNBLOCK_INPUT;
4833
4834 /* Advance the output cursor. */
4835 output_cursor.hpos += len;
4836 output_cursor.x = x;
4837 }
4838
4839
4840 /* Insert LEN glyphs from START at the nominal cursor position. */
4841
4842 static void
4843 x_insert_glyphs (start, len)
4844 struct glyph *start;
4845 register int len;
4846 {
4847 struct frame *f;
4848 struct window *w;
4849 int line_height, shift_by_width, shifted_region_width;
4850 struct glyph_row *row;
4851 struct glyph *glyph;
4852 int frame_x, frame_y, hpos, real_start, real_end;
4853
4854 xassert (updated_window && updated_row);
4855 BLOCK_INPUT;
4856 w = updated_window;
4857 f = XFRAME (WINDOW_FRAME (w));
4858
4859 /* Get the height of the line we are in. */
4860 row = updated_row;
4861 line_height = row->height;
4862
4863 /* Get the width of the glyphs to insert. */
4864 shift_by_width = 0;
4865 for (glyph = start; glyph < start + len; ++glyph)
4866 shift_by_width += glyph->pixel_width;
4867
4868 /* Get the width of the region to shift right. */
4869 shifted_region_width = (window_box_width (w, updated_area)
4870 - output_cursor.x
4871 - shift_by_width);
4872
4873 /* Shift right. */
4874 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4875 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4876 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4877 f->output_data.x->normal_gc,
4878 frame_x, frame_y,
4879 shifted_region_width, line_height,
4880 frame_x + shift_by_width, frame_y);
4881
4882 /* Write the glyphs. */
4883 hpos = start - row->glyphs[updated_area];
4884 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4885 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4886 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4887
4888 /* Advance the output cursor. */
4889 output_cursor.hpos += len;
4890 output_cursor.x += shift_by_width;
4891 UNBLOCK_INPUT;
4892 }
4893
4894
4895 /* Delete N glyphs at the nominal cursor position. Not implemented
4896 for X frames. */
4897
4898 static void
4899 x_delete_glyphs (n)
4900 register int n;
4901 {
4902 abort ();
4903 }
4904
4905
4906 /* Erase the current text line from the nominal cursor position
4907 (inclusive) to pixel column TO_X (exclusive). The idea is that
4908 everything from TO_X onward is already erased.
4909
4910 TO_X is a pixel position relative to updated_area of
4911 updated_window. TO_X == -1 means clear to the end of this area. */
4912
4913 static void
4914 x_clear_end_of_line (to_x)
4915 int to_x;
4916 {
4917 struct frame *f;
4918 struct window *w = updated_window;
4919 int max_x, min_y, max_y;
4920 int from_x, from_y, to_y;
4921
4922 xassert (updated_window && updated_row);
4923 f = XFRAME (w->frame);
4924
4925 if (updated_row->full_width_p)
4926 {
4927 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4928 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4929 && !w->pseudo_window_p)
4930 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4931 }
4932 else
4933 max_x = window_box_width (w, updated_area);
4934 max_y = window_text_bottom_y (w);
4935
4936 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4937 of window. For TO_X > 0, truncate to end of drawing area. */
4938 if (to_x == 0)
4939 return;
4940 else if (to_x < 0)
4941 to_x = max_x;
4942 else
4943 to_x = min (to_x, max_x);
4944
4945 to_y = min (max_y, output_cursor.y + updated_row->height);
4946
4947 /* Notice if the cursor will be cleared by this operation. */
4948 if (!updated_row->full_width_p)
4949 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
4950
4951 from_x = output_cursor.x;
4952
4953 /* Translate to frame coordinates. */
4954 if (updated_row->full_width_p)
4955 {
4956 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4957 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4958 }
4959 else
4960 {
4961 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4962 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4963 }
4964
4965 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
4966 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4967 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4968
4969 /* Prevent inadvertently clearing to end of the X window. */
4970 if (to_x > from_x && to_y > from_y)
4971 {
4972 BLOCK_INPUT;
4973 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4974 from_x, from_y, to_x - from_x, to_y - from_y,
4975 False);
4976 UNBLOCK_INPUT;
4977 }
4978 }
4979
4980
4981 /* Clear entire frame. If updating_frame is non-null, clear that
4982 frame. Otherwise clear the selected frame. */
4983
4984 static void
4985 x_clear_frame ()
4986 {
4987 struct frame *f;
4988
4989 if (updating_frame)
4990 f = updating_frame;
4991 else
4992 f = SELECTED_FRAME ();
4993
4994 /* Clearing the frame will erase any cursor, so mark them all as no
4995 longer visible. */
4996 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4997 output_cursor.hpos = output_cursor.vpos = 0;
4998 output_cursor.x = -1;
4999
5000 /* We don't set the output cursor here because there will always
5001 follow an explicit cursor_to. */
5002 BLOCK_INPUT;
5003 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5004
5005 /* We have to clear the scroll bars, too. If we have changed
5006 colors or something like that, then they should be notified. */
5007 x_scroll_bar_clear (f);
5008
5009 XFlush (FRAME_X_DISPLAY (f));
5010 UNBLOCK_INPUT;
5011 }
5012
5013
5014 \f
5015 /* Invert the middle quarter of the frame for .15 sec. */
5016
5017 /* We use the select system call to do the waiting, so we have to make
5018 sure it's available. If it isn't, we just won't do visual bells. */
5019
5020 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5021
5022
5023 /* Subtract the `struct timeval' values X and Y, storing the result in
5024 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5025
5026 static int
5027 timeval_subtract (result, x, y)
5028 struct timeval *result, x, y;
5029 {
5030 /* Perform the carry for the later subtraction by updating y. This
5031 is safer because on some systems the tv_sec member is unsigned. */
5032 if (x.tv_usec < y.tv_usec)
5033 {
5034 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5035 y.tv_usec -= 1000000 * nsec;
5036 y.tv_sec += nsec;
5037 }
5038
5039 if (x.tv_usec - y.tv_usec > 1000000)
5040 {
5041 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5042 y.tv_usec += 1000000 * nsec;
5043 y.tv_sec -= nsec;
5044 }
5045
5046 /* Compute the time remaining to wait. tv_usec is certainly
5047 positive. */
5048 result->tv_sec = x.tv_sec - y.tv_sec;
5049 result->tv_usec = x.tv_usec - y.tv_usec;
5050
5051 /* Return indication of whether the result should be considered
5052 negative. */
5053 return x.tv_sec < y.tv_sec;
5054 }
5055
5056 void
5057 XTflash (f)
5058 struct frame *f;
5059 {
5060 BLOCK_INPUT;
5061
5062 {
5063 GC gc;
5064
5065 /* Create a GC that will use the GXxor function to flip foreground
5066 pixels into background pixels. */
5067 {
5068 XGCValues values;
5069
5070 values.function = GXxor;
5071 values.foreground = (f->output_data.x->foreground_pixel
5072 ^ f->output_data.x->background_pixel);
5073
5074 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5075 GCFunction | GCForeground, &values);
5076 }
5077
5078 {
5079 /* Get the height not including a menu bar widget. */
5080 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5081 /* Height of each line to flash. */
5082 int flash_height = FRAME_LINE_HEIGHT (f);
5083 /* These will be the left and right margins of the rectangles. */
5084 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5085 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5086
5087 int width;
5088
5089 /* Don't flash the area between a scroll bar and the frame
5090 edge it is next to. */
5091 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5092 {
5093 case vertical_scroll_bar_left:
5094 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5095 break;
5096
5097 case vertical_scroll_bar_right:
5098 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5099 break;
5100
5101 default:
5102 break;
5103 }
5104
5105 width = flash_right - flash_left;
5106
5107 /* If window is tall, flash top and bottom line. */
5108 if (height > 3 * FRAME_LINE_HEIGHT (f))
5109 {
5110 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5111 flash_left,
5112 (FRAME_INTERNAL_BORDER_WIDTH (f)
5113 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5114 width, flash_height);
5115 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5116 flash_left,
5117 (height - flash_height
5118 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5119 width, flash_height);
5120 }
5121 else
5122 /* If it is short, flash it all. */
5123 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5124 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5125 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5126
5127 x_flush (f);
5128
5129 {
5130 struct timeval wakeup;
5131
5132 EMACS_GET_TIME (wakeup);
5133
5134 /* Compute time to wait until, propagating carry from usecs. */
5135 wakeup.tv_usec += 150000;
5136 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5137 wakeup.tv_usec %= 1000000;
5138
5139 /* Keep waiting until past the time wakeup. */
5140 while (1)
5141 {
5142 struct timeval timeout;
5143
5144 EMACS_GET_TIME (timeout);
5145
5146 /* In effect, timeout = wakeup - timeout.
5147 Break if result would be negative. */
5148 if (timeval_subtract (&timeout, wakeup, timeout))
5149 break;
5150
5151 /* Try to wait that long--but we might wake up sooner. */
5152 select (0, NULL, NULL, NULL, &timeout);
5153 }
5154 }
5155
5156 /* If window is tall, flash top and bottom line. */
5157 if (height > 3 * FRAME_LINE_HEIGHT (f))
5158 {
5159 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5160 flash_left,
5161 (FRAME_INTERNAL_BORDER_WIDTH (f)
5162 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5163 width, flash_height);
5164 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5165 flash_left,
5166 (height - flash_height
5167 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5168 width, flash_height);
5169 }
5170 else
5171 /* If it is short, flash it all. */
5172 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5173 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5174 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5175
5176 XFreeGC (FRAME_X_DISPLAY (f), gc);
5177 x_flush (f);
5178 }
5179 }
5180
5181 UNBLOCK_INPUT;
5182 }
5183
5184 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5185
5186
5187 /* Make audible bell. */
5188
5189 void
5190 XTring_bell ()
5191 {
5192 struct frame *f = SELECTED_FRAME ();
5193
5194 if (FRAME_X_DISPLAY (f))
5195 {
5196 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5197 if (visible_bell)
5198 XTflash (f);
5199 else
5200 #endif
5201 {
5202 BLOCK_INPUT;
5203 XBell (FRAME_X_DISPLAY (f), 0);
5204 XFlush (FRAME_X_DISPLAY (f));
5205 UNBLOCK_INPUT;
5206 }
5207 }
5208 }
5209
5210 \f
5211 /* Specify how many text lines, from the top of the window,
5212 should be affected by insert-lines and delete-lines operations.
5213 This, and those operations, are used only within an update
5214 that is bounded by calls to x_update_begin and x_update_end. */
5215
5216 static void
5217 XTset_terminal_window (n)
5218 register int n;
5219 {
5220 /* This function intentionally left blank. */
5221 }
5222
5223
5224 \f
5225 /***********************************************************************
5226 Line Dance
5227 ***********************************************************************/
5228
5229 /* Perform an insert-lines or delete-lines operation, inserting N
5230 lines or deleting -N lines at vertical position VPOS. */
5231
5232 static void
5233 x_ins_del_lines (vpos, n)
5234 int vpos, n;
5235 {
5236 abort ();
5237 }
5238
5239
5240 /* Scroll part of the display as described by RUN. */
5241
5242 static void
5243 x_scroll_run (w, run)
5244 struct window *w;
5245 struct run *run;
5246 {
5247 struct frame *f = XFRAME (w->frame);
5248 int x, y, width, height, from_y, to_y, bottom_y;
5249
5250 /* Get frame-relative bounding box of the text display area of W,
5251 without mode lines. Include in this box the flags areas to the
5252 left and right of W. */
5253 window_box (w, -1, &x, &y, &width, &height);
5254 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5255 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5256
5257 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5258 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5259 bottom_y = y + height;
5260
5261 if (to_y < from_y)
5262 {
5263 /* Scrolling up. Make sure we don't copy part of the mode
5264 line at the bottom. */
5265 if (from_y + run->height > bottom_y)
5266 height = bottom_y - from_y;
5267 else
5268 height = run->height;
5269 }
5270 else
5271 {
5272 /* Scolling down. Make sure we don't copy over the mode line.
5273 at the bottom. */
5274 if (to_y + run->height > bottom_y)
5275 height = bottom_y - to_y;
5276 else
5277 height = run->height;
5278 }
5279
5280 BLOCK_INPUT;
5281
5282 /* Cursor off. Will be switched on again in x_update_window_end. */
5283 updated_window = w;
5284 x_clear_cursor (w);
5285
5286 XCopyArea (FRAME_X_DISPLAY (f),
5287 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5288 f->output_data.x->normal_gc,
5289 x, from_y,
5290 width, height,
5291 x, to_y);
5292
5293 UNBLOCK_INPUT;
5294 }
5295
5296
5297 \f
5298 /***********************************************************************
5299 Exposure Events
5300 ***********************************************************************/
5301
5302 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5303 corner of the exposed rectangle. W and H are width and height of
5304 the exposed area. All are pixel values. W or H zero means redraw
5305 the entire frame. */
5306
5307 static void
5308 expose_frame (f, x, y, w, h)
5309 struct frame *f;
5310 int x, y, w, h;
5311 {
5312 XRectangle r;
5313
5314 TRACE ((stderr, "expose_frame "));
5315
5316 /* No need to redraw if frame will be redrawn soon. */
5317 if (FRAME_GARBAGED_P (f))
5318 {
5319 TRACE ((stderr, " garbaged\n"));
5320 return;
5321 }
5322
5323 /* If basic faces haven't been realized yet, there is no point in
5324 trying to redraw anything. This can happen when we get an expose
5325 event while Emacs is starting, e.g. by moving another window. */
5326 if (FRAME_FACE_CACHE (f) == NULL
5327 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5328 {
5329 TRACE ((stderr, " no faces\n"));
5330 return;
5331 }
5332
5333 if (w == 0 || h == 0)
5334 {
5335 r.x = r.y = 0;
5336 r.width = CANON_X_UNIT (f) * f->width;
5337 r.height = CANON_Y_UNIT (f) * f->height;
5338 }
5339 else
5340 {
5341 r.x = x;
5342 r.y = y;
5343 r.width = w;
5344 r.height = h;
5345 }
5346
5347 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5348 expose_window_tree (XWINDOW (f->root_window), &r);
5349
5350 if (WINDOWP (f->tool_bar_window))
5351 {
5352 struct window *w = XWINDOW (f->tool_bar_window);
5353 XRectangle window_rect;
5354 XRectangle intersection_rect;
5355 int window_x, window_y, window_width, window_height;
5356
5357
5358 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5359 window_rect.x = window_x;
5360 window_rect.y = window_y;
5361 window_rect.width = window_width;
5362 window_rect.height = window_height;
5363
5364 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5365 expose_window (w, &intersection_rect);
5366 }
5367
5368 #ifndef USE_X_TOOLKIT
5369 if (WINDOWP (f->menu_bar_window))
5370 {
5371 struct window *w = XWINDOW (f->menu_bar_window);
5372 XRectangle window_rect;
5373 XRectangle intersection_rect;
5374 int window_x, window_y, window_width, window_height;
5375
5376
5377 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5378 window_rect.x = window_x;
5379 window_rect.y = window_y;
5380 window_rect.width = window_width;
5381 window_rect.height = window_height;
5382
5383 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5384 expose_window (w, &intersection_rect);
5385 }
5386 #endif /* not USE_X_TOOLKIT */
5387 }
5388
5389
5390 /* Redraw (parts) of all windows in the window tree rooted at W that
5391 intersect R. R contains frame pixel coordinates. */
5392
5393 static void
5394 expose_window_tree (w, r)
5395 struct window *w;
5396 XRectangle *r;
5397 {
5398 while (w)
5399 {
5400 if (!NILP (w->hchild))
5401 expose_window_tree (XWINDOW (w->hchild), r);
5402 else if (!NILP (w->vchild))
5403 expose_window_tree (XWINDOW (w->vchild), r);
5404 else
5405 {
5406 XRectangle window_rect;
5407 XRectangle intersection_rect;
5408 struct frame *f = XFRAME (w->frame);
5409 int window_x, window_y, window_width, window_height;
5410
5411 /* Frame-relative pixel rectangle of W. */
5412 window_box (w, -1, &window_x, &window_y, &window_width,
5413 &window_height);
5414 window_rect.x
5415 = (window_x
5416 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5417 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5418 window_rect.y = window_y;
5419 window_rect.width
5420 = (window_width
5421 + FRAME_X_FLAGS_AREA_WIDTH (f)
5422 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5423 window_rect.height
5424 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5425
5426 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5427 expose_window (w, &intersection_rect);
5428 }
5429
5430 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5431 }
5432 }
5433
5434
5435 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5436 which intersects rectangle R. R is in window-relative coordinates. */
5437
5438 static void
5439 expose_area (w, row, r, area)
5440 struct window *w;
5441 struct glyph_row *row;
5442 XRectangle *r;
5443 enum glyph_row_area area;
5444 {
5445 int x;
5446 struct glyph *first = row->glyphs[area];
5447 struct glyph *end = row->glyphs[area] + row->used[area];
5448 struct glyph *last;
5449 int first_x;
5450
5451 /* Set x to the window-relative start position for drawing glyphs of
5452 AREA. The first glyph of the text area can be partially visible.
5453 The first glyphs of other areas cannot. */
5454 if (area == LEFT_MARGIN_AREA)
5455 x = 0;
5456 else if (area == TEXT_AREA)
5457 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5458 else
5459 x = (window_box_width (w, LEFT_MARGIN_AREA)
5460 + window_box_width (w, TEXT_AREA));
5461
5462 if (area == TEXT_AREA && row->fill_line_p)
5463 /* If row extends face to end of line write the whole line. */
5464 x_draw_glyphs (w, x, row, area,
5465 0, row->used[area],
5466 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5467 NULL, NULL, 0);
5468 else
5469 {
5470 /* Find the first glyph that must be redrawn. */
5471 while (first < end
5472 && x + first->pixel_width < r->x)
5473 {
5474 x += first->pixel_width;
5475 ++first;
5476 }
5477
5478 /* Find the last one. */
5479 last = first;
5480 first_x = x;
5481 while (last < end
5482 && x < r->x + r->width)
5483 {
5484 x += last->pixel_width;
5485 ++last;
5486 }
5487
5488 /* Repaint. */
5489 if (last > first)
5490 x_draw_glyphs (w, first_x, row, area,
5491 first - row->glyphs[area],
5492 last - row->glyphs[area],
5493 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5494 NULL, NULL, 0);
5495 }
5496 }
5497
5498
5499 /* Redraw the parts of the glyph row ROW on window W intersecting
5500 rectangle R. R is in window-relative coordinates. */
5501
5502 static void
5503 expose_line (w, row, r)
5504 struct window *w;
5505 struct glyph_row *row;
5506 XRectangle *r;
5507 {
5508 xassert (row->enabled_p);
5509
5510 if (row->mode_line_p || w->pseudo_window_p)
5511 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5512 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5513 NULL, NULL, 0);
5514 else
5515 {
5516 if (row->used[LEFT_MARGIN_AREA])
5517 expose_area (w, row, r, LEFT_MARGIN_AREA);
5518 if (row->used[TEXT_AREA])
5519 expose_area (w, row, r, TEXT_AREA);
5520 if (row->used[RIGHT_MARGIN_AREA])
5521 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5522 x_draw_row_bitmaps (w, row);
5523 }
5524 }
5525
5526
5527 /* Return non-zero if W's cursor intersects rectangle R. */
5528
5529 static int
5530 x_phys_cursor_in_rect_p (w, r)
5531 struct window *w;
5532 XRectangle *r;
5533 {
5534 XRectangle cr, result;
5535 struct glyph *cursor_glyph;
5536
5537 cursor_glyph = get_phys_cursor_glyph (w);
5538 if (cursor_glyph)
5539 {
5540 cr.x = w->phys_cursor.x;
5541 cr.y = w->phys_cursor.y;
5542 cr.width = cursor_glyph->pixel_width;
5543 cr.height = w->phys_cursor_height;
5544 return x_intersect_rectangles (&cr, r, &result);
5545 }
5546 else
5547 return 0;
5548 }
5549
5550
5551 /* Redraw a rectangle of window W. R is a rectangle in window
5552 relative coordinates. Call this function with input blocked. */
5553
5554 static void
5555 expose_window (w, r)
5556 struct window *w;
5557 XRectangle *r;
5558 {
5559 struct glyph_row *row;
5560 int y;
5561 int yb = window_text_bottom_y (w);
5562 int cursor_cleared_p;
5563
5564 /* If window is not yet fully initialized, do nothing. This can
5565 happen when toolkit scroll bars are used and a window is split.
5566 Reconfiguring the scroll bar will generate an expose for a newly
5567 created window. */
5568 if (w->current_matrix == NULL)
5569 return;
5570
5571 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5572 r->x, r->y, r->width, r->height));
5573
5574 /* Convert to window coordinates. */
5575 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5576 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5577
5578 /* Turn off the cursor. */
5579 if (!w->pseudo_window_p
5580 && x_phys_cursor_in_rect_p (w, r))
5581 {
5582 x_clear_cursor (w);
5583 cursor_cleared_p = 1;
5584 }
5585 else
5586 cursor_cleared_p = 0;
5587
5588 /* Find the first row intersecting the rectangle R. */
5589 row = w->current_matrix->rows;
5590 y = 0;
5591 while (row->enabled_p
5592 && y < yb
5593 && y + row->height < r->y)
5594 {
5595 y += row->height;
5596 ++row;
5597 }
5598
5599 /* Display the text in the rectangle, one text line at a time. */
5600 while (row->enabled_p
5601 && y < yb
5602 && y < r->y + r->height)
5603 {
5604 expose_line (w, row, r);
5605 y += row->height;
5606 ++row;
5607 }
5608
5609 /* Display the mode line if there is one. */
5610 if (WINDOW_WANTS_MODELINE_P (w)
5611 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5612 row->enabled_p)
5613 && row->y < r->y + r->height)
5614 expose_line (w, row, r);
5615
5616 if (!w->pseudo_window_p)
5617 {
5618 /* Draw border between windows. */
5619 x_draw_vertical_border (w);
5620
5621 /* Turn the cursor on again. */
5622 if (cursor_cleared_p)
5623 x_update_window_cursor (w, 1);
5624 }
5625 }
5626
5627
5628 /* Determine the intersection of two rectangles R1 and R2. Return
5629 the intersection in *RESULT. Value is non-zero if RESULT is not
5630 empty. */
5631
5632 static int
5633 x_intersect_rectangles (r1, r2, result)
5634 XRectangle *r1, *r2, *result;
5635 {
5636 XRectangle *left, *right;
5637 XRectangle *upper, *lower;
5638 int intersection_p = 0;
5639
5640 /* Rearrange so that R1 is the left-most rectangle. */
5641 if (r1->x < r2->x)
5642 left = r1, right = r2;
5643 else
5644 left = r2, right = r1;
5645
5646 /* X0 of the intersection is right.x0, if this is inside R1,
5647 otherwise there is no intersection. */
5648 if (right->x <= left->x + left->width)
5649 {
5650 result->x = right->x;
5651
5652 /* The right end of the intersection is the minimum of the
5653 the right ends of left and right. */
5654 result->width = (min (left->x + left->width, right->x + right->width)
5655 - result->x);
5656
5657 /* Same game for Y. */
5658 if (r1->y < r2->y)
5659 upper = r1, lower = r2;
5660 else
5661 upper = r2, lower = r1;
5662
5663 /* The upper end of the intersection is lower.y0, if this is inside
5664 of upper. Otherwise, there is no intersection. */
5665 if (lower->y <= upper->y + upper->height)
5666 {
5667 result->y = lower->y;
5668
5669 /* The lower end of the intersection is the minimum of the lower
5670 ends of upper and lower. */
5671 result->height = (min (lower->y + lower->height,
5672 upper->y + upper->height)
5673 - result->y);
5674 intersection_p = 1;
5675 }
5676 }
5677
5678 return intersection_p;
5679 }
5680
5681
5682
5683
5684 \f
5685 static void
5686 frame_highlight (f)
5687 struct frame *f;
5688 {
5689 /* We used to only do this if Vx_no_window_manager was non-nil, but
5690 the ICCCM (section 4.1.6) says that the window's border pixmap
5691 and border pixel are window attributes which are "private to the
5692 client", so we can always change it to whatever we want. */
5693 BLOCK_INPUT;
5694 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5695 f->output_data.x->border_pixel);
5696 UNBLOCK_INPUT;
5697 x_update_cursor (f, 1);
5698 }
5699
5700 static void
5701 frame_unhighlight (f)
5702 struct frame *f;
5703 {
5704 /* We used to only do this if Vx_no_window_manager was non-nil, but
5705 the ICCCM (section 4.1.6) says that the window's border pixmap
5706 and border pixel are window attributes which are "private to the
5707 client", so we can always change it to whatever we want. */
5708 BLOCK_INPUT;
5709 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5710 f->output_data.x->border_tile);
5711 UNBLOCK_INPUT;
5712 x_update_cursor (f, 1);
5713 }
5714
5715 /* The focus has changed. Update the frames as necessary to reflect
5716 the new situation. Note that we can't change the selected frame
5717 here, because the Lisp code we are interrupting might become confused.
5718 Each event gets marked with the frame in which it occurred, so the
5719 Lisp code can tell when the switch took place by examining the events. */
5720
5721 static void
5722 x_new_focus_frame (dpyinfo, frame)
5723 struct x_display_info *dpyinfo;
5724 struct frame *frame;
5725 {
5726 struct frame *old_focus = dpyinfo->x_focus_frame;
5727
5728 if (frame != dpyinfo->x_focus_frame)
5729 {
5730 /* Set this before calling other routines, so that they see
5731 the correct value of x_focus_frame. */
5732 dpyinfo->x_focus_frame = frame;
5733
5734 if (old_focus && old_focus->auto_lower)
5735 x_lower_frame (old_focus);
5736
5737 #if 0
5738 selected_frame = frame;
5739 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5740 selected_frame);
5741 Fselect_window (selected_frame->selected_window);
5742 choose_minibuf_frame ();
5743 #endif /* ! 0 */
5744
5745 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5746 pending_autoraise_frame = dpyinfo->x_focus_frame;
5747 else
5748 pending_autoraise_frame = 0;
5749 }
5750
5751 x_frame_rehighlight (dpyinfo);
5752 }
5753
5754 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5755
5756 void
5757 x_mouse_leave (dpyinfo)
5758 struct x_display_info *dpyinfo;
5759 {
5760 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5761 }
5762
5763 /* The focus has changed, or we have redirected a frame's focus to
5764 another frame (this happens when a frame uses a surrogate
5765 mini-buffer frame). Shift the highlight as appropriate.
5766
5767 The FRAME argument doesn't necessarily have anything to do with which
5768 frame is being highlighted or un-highlighted; we only use it to find
5769 the appropriate X display info. */
5770
5771 static void
5772 XTframe_rehighlight (frame)
5773 struct frame *frame;
5774 {
5775 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5776 }
5777
5778 static void
5779 x_frame_rehighlight (dpyinfo)
5780 struct x_display_info *dpyinfo;
5781 {
5782 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5783
5784 if (dpyinfo->x_focus_frame)
5785 {
5786 dpyinfo->x_highlight_frame
5787 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5788 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5789 : dpyinfo->x_focus_frame);
5790 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5791 {
5792 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5793 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5794 }
5795 }
5796 else
5797 dpyinfo->x_highlight_frame = 0;
5798
5799 if (dpyinfo->x_highlight_frame != old_highlight)
5800 {
5801 if (old_highlight)
5802 frame_unhighlight (old_highlight);
5803 if (dpyinfo->x_highlight_frame)
5804 frame_highlight (dpyinfo->x_highlight_frame);
5805 }
5806 }
5807
5808
5809 \f
5810 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5811
5812 /* Initialize mode_switch_bit and modifier_meaning. */
5813 static void
5814 x_find_modifier_meanings (dpyinfo)
5815 struct x_display_info *dpyinfo;
5816 {
5817 int min_code, max_code;
5818 KeySym *syms;
5819 int syms_per_code;
5820 XModifierKeymap *mods;
5821
5822 dpyinfo->meta_mod_mask = 0;
5823 dpyinfo->shift_lock_mask = 0;
5824 dpyinfo->alt_mod_mask = 0;
5825 dpyinfo->super_mod_mask = 0;
5826 dpyinfo->hyper_mod_mask = 0;
5827
5828 #ifdef HAVE_X11R4
5829 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5830 #else
5831 min_code = dpyinfo->display->min_keycode;
5832 max_code = dpyinfo->display->max_keycode;
5833 #endif
5834
5835 syms = XGetKeyboardMapping (dpyinfo->display,
5836 min_code, max_code - min_code + 1,
5837 &syms_per_code);
5838 mods = XGetModifierMapping (dpyinfo->display);
5839
5840 /* Scan the modifier table to see which modifier bits the Meta and
5841 Alt keysyms are on. */
5842 {
5843 int row, col; /* The row and column in the modifier table. */
5844
5845 for (row = 3; row < 8; row++)
5846 for (col = 0; col < mods->max_keypermod; col++)
5847 {
5848 KeyCode code
5849 = mods->modifiermap[(row * mods->max_keypermod) + col];
5850
5851 /* Zeroes are used for filler. Skip them. */
5852 if (code == 0)
5853 continue;
5854
5855 /* Are any of this keycode's keysyms a meta key? */
5856 {
5857 int code_col;
5858
5859 for (code_col = 0; code_col < syms_per_code; code_col++)
5860 {
5861 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5862
5863 switch (sym)
5864 {
5865 case XK_Meta_L:
5866 case XK_Meta_R:
5867 dpyinfo->meta_mod_mask |= (1 << row);
5868 break;
5869
5870 case XK_Alt_L:
5871 case XK_Alt_R:
5872 dpyinfo->alt_mod_mask |= (1 << row);
5873 break;
5874
5875 case XK_Hyper_L:
5876 case XK_Hyper_R:
5877 dpyinfo->hyper_mod_mask |= (1 << row);
5878 break;
5879
5880 case XK_Super_L:
5881 case XK_Super_R:
5882 dpyinfo->super_mod_mask |= (1 << row);
5883 break;
5884
5885 case XK_Shift_Lock:
5886 /* Ignore this if it's not on the lock modifier. */
5887 if ((1 << row) == LockMask)
5888 dpyinfo->shift_lock_mask = LockMask;
5889 break;
5890 }
5891 }
5892 }
5893 }
5894 }
5895
5896 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5897 if (! dpyinfo->meta_mod_mask)
5898 {
5899 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5900 dpyinfo->alt_mod_mask = 0;
5901 }
5902
5903 /* If some keys are both alt and meta,
5904 make them just meta, not alt. */
5905 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5906 {
5907 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5908 }
5909
5910 XFree ((char *) syms);
5911 XFreeModifiermap (mods);
5912 }
5913
5914 /* Convert between the modifier bits X uses and the modifier bits
5915 Emacs uses. */
5916
5917 static unsigned int
5918 x_x_to_emacs_modifiers (dpyinfo, state)
5919 struct x_display_info *dpyinfo;
5920 unsigned int state;
5921 {
5922 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5923 | ((state & ControlMask) ? ctrl_modifier : 0)
5924 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5925 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5926 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5927 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
5928 }
5929
5930 static unsigned int
5931 x_emacs_to_x_modifiers (dpyinfo, state)
5932 struct x_display_info *dpyinfo;
5933 unsigned int state;
5934 {
5935 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5936 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5937 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5938 | ((state & shift_modifier) ? ShiftMask : 0)
5939 | ((state & ctrl_modifier) ? ControlMask : 0)
5940 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
5941 }
5942
5943 /* Convert a keysym to its name. */
5944
5945 char *
5946 x_get_keysym_name (keysym)
5947 KeySym keysym;
5948 {
5949 char *value;
5950
5951 BLOCK_INPUT;
5952 value = XKeysymToString (keysym);
5953 UNBLOCK_INPUT;
5954
5955 return value;
5956 }
5957
5958
5959 \f
5960 /* Mouse clicks and mouse movement. Rah. */
5961
5962 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5963 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5964 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5965 not force the value into range. */
5966
5967 void
5968 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5969 FRAME_PTR f;
5970 register int pix_x, pix_y;
5971 register int *x, *y;
5972 XRectangle *bounds;
5973 int noclip;
5974 {
5975 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5976 even for negative values. */
5977 if (pix_x < 0)
5978 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
5979 if (pix_y < 0)
5980 pix_y -= (f)->output_data.x->line_height - 1;
5981
5982 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5983 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5984
5985 if (bounds)
5986 {
5987 bounds->width = FONT_WIDTH (f->output_data.x->font);
5988 bounds->height = f->output_data.x->line_height;
5989 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5990 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5991 }
5992
5993 if (!noclip)
5994 {
5995 if (pix_x < 0)
5996 pix_x = 0;
5997 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5998 pix_x = FRAME_WINDOW_WIDTH (f);
5999
6000 if (pix_y < 0)
6001 pix_y = 0;
6002 else if (pix_y > f->height)
6003 pix_y = f->height;
6004 }
6005
6006 *x = pix_x;
6007 *y = pix_y;
6008 }
6009
6010
6011 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6012 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6013 can't tell the positions because W's display is not up to date,
6014 return 0. */
6015
6016 int
6017 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6018 struct window *w;
6019 int hpos, vpos;
6020 int *frame_x, *frame_y;
6021 {
6022 int success_p;
6023
6024 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6025 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6026
6027 if (display_completed)
6028 {
6029 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6030 struct glyph *glyph = row->glyphs[TEXT_AREA];
6031 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6032
6033 *frame_y = row->y;
6034 *frame_x = row->x;
6035 while (glyph < end)
6036 {
6037 *frame_x += glyph->pixel_width;
6038 ++glyph;
6039 }
6040
6041 success_p = 1;
6042 }
6043 else
6044 {
6045 *frame_y = *frame_x = 0;
6046 success_p = 0;
6047 }
6048
6049 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6050 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6051 return success_p;
6052 }
6053
6054
6055 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6056
6057 If the event is a button press, then note that we have grabbed
6058 the mouse. */
6059
6060 static Lisp_Object
6061 construct_mouse_click (result, event, f)
6062 struct input_event *result;
6063 XButtonEvent *event;
6064 struct frame *f;
6065 {
6066 /* Make the event type no_event; we'll change that when we decide
6067 otherwise. */
6068 result->kind = mouse_click;
6069 result->code = event->button - Button1;
6070 result->timestamp = event->time;
6071 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6072 event->state)
6073 | (event->type == ButtonRelease
6074 ? up_modifier
6075 : down_modifier));
6076
6077 XSETINT (result->x, event->x);
6078 XSETINT (result->y, event->y);
6079 XSETFRAME (result->frame_or_window, f);
6080 return Qnil;
6081 }
6082
6083 #if 0 /* This function isn't called. --gerd */
6084
6085 /* Prepare a menu-event in *RESULT for placement in the input queue. */
6086
6087 static Lisp_Object
6088 construct_menu_click (result, event, f)
6089 struct input_event *result;
6090 XButtonEvent *event;
6091 struct frame *f;
6092 {
6093 /* Make the event type no_event; we'll change that when we decide
6094 otherwise. */
6095 result->kind = mouse_click;
6096 result->code = event->button - Button1;
6097 result->timestamp = event->time;
6098 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6099 event->state)
6100 | (event->type == ButtonRelease
6101 ? up_modifier
6102 : down_modifier));
6103
6104 XSETINT (result->x, event->x);
6105 XSETINT (result->y, -1);
6106 XSETFRAME (result->frame_or_window, f);
6107 }
6108
6109 #endif /* 0 */
6110
6111 \f
6112 /* Function to report a mouse movement to the mainstream Emacs code.
6113 The input handler calls this.
6114
6115 We have received a mouse movement event, which is given in *event.
6116 If the mouse is over a different glyph than it was last time, tell
6117 the mainstream emacs code by setting mouse_moved. If not, ask for
6118 another motion event, so we can check again the next time it moves. */
6119
6120 static XMotionEvent last_mouse_motion_event;
6121 static Lisp_Object last_mouse_motion_frame;
6122
6123 static void
6124 note_mouse_movement (frame, event)
6125 FRAME_PTR frame;
6126 XMotionEvent *event;
6127 {
6128 last_mouse_movement_time = event->time;
6129 last_mouse_motion_event = *event;
6130 XSETFRAME (last_mouse_motion_frame, frame);
6131
6132 if (event->window != FRAME_X_WINDOW (frame))
6133 {
6134 frame->mouse_moved = 1;
6135 last_mouse_scroll_bar = Qnil;
6136 note_mouse_highlight (frame, -1, -1);
6137 }
6138
6139 /* Has the mouse moved off the glyph it was on at the last sighting? */
6140 else if (event->x < last_mouse_glyph.x
6141 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6142 || event->y < last_mouse_glyph.y
6143 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6144 {
6145 frame->mouse_moved = 1;
6146 last_mouse_scroll_bar = Qnil;
6147 note_mouse_highlight (frame, event->x, event->y);
6148 }
6149 }
6150
6151 /* This is used for debugging, to turn off note_mouse_highlight. */
6152
6153 int disable_mouse_highlight;
6154
6155
6156 \f
6157 /************************************************************************
6158 Mouse Face
6159 ************************************************************************/
6160
6161 /* Find the glyph under window-relative coordinates X/Y in window W.
6162 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6163 strings. Return in *HPOS and *VPOS the row and column number of
6164 the glyph found. Return in *AREA the glyph area containing X.
6165 Value is a pointer to the glyph found or null if X/Y is not on
6166 text, or we can't tell because W's current matrix is not up to
6167 date. */
6168
6169 static struct glyph *
6170 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6171 struct window *w;
6172 int x, y;
6173 int *hpos, *vpos, *area;
6174 {
6175 struct glyph *glyph, *end;
6176 struct glyph_row *row = NULL;
6177 int x0, i, left_area_width;
6178
6179 /* Find row containing Y. Give up if some row is not enabled. */
6180 for (i = 0; i < w->current_matrix->nrows; ++i)
6181 {
6182 row = MATRIX_ROW (w->current_matrix, i);
6183 if (!row->enabled_p)
6184 return NULL;
6185 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6186 break;
6187 }
6188
6189 *vpos = i;
6190 *hpos = 0;
6191
6192 /* Give up if Y is not in the window. */
6193 if (i == w->current_matrix->nrows)
6194 return NULL;
6195
6196 /* Get the glyph area containing X. */
6197 if (w->pseudo_window_p)
6198 {
6199 *area = TEXT_AREA;
6200 x0 = 0;
6201 }
6202 else
6203 {
6204 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6205 if (x < left_area_width)
6206 {
6207 *area = LEFT_MARGIN_AREA;
6208 x0 = 0;
6209 }
6210 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6211 {
6212 *area = TEXT_AREA;
6213 x0 = row->x + left_area_width;
6214 }
6215 else
6216 {
6217 *area = RIGHT_MARGIN_AREA;
6218 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6219 }
6220 }
6221
6222 /* Find glyph containing X. */
6223 glyph = row->glyphs[*area];
6224 end = glyph + row->used[*area];
6225 while (glyph < end)
6226 {
6227 if (x < x0 + glyph->pixel_width)
6228 {
6229 if (w->pseudo_window_p)
6230 break;
6231 else if (BUFFERP (glyph->object))
6232 break;
6233 }
6234
6235 x0 += glyph->pixel_width;
6236 ++glyph;
6237 }
6238
6239 if (glyph == end)
6240 return NULL;
6241
6242 *hpos = glyph - row->glyphs[*area];
6243 return glyph;
6244 }
6245
6246
6247 /* Convert frame-relative x/y to coordinates relative to window W.
6248 Takes pseudo-windows into account. */
6249
6250 static void
6251 frame_to_window_pixel_xy (w, x, y)
6252 struct window *w;
6253 int *x, *y;
6254 {
6255 if (w->pseudo_window_p)
6256 {
6257 /* A pseudo-window is always full-width, and starts at the
6258 left edge of the frame, plus a frame border. */
6259 struct frame *f = XFRAME (w->frame);
6260 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6261 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6262 }
6263 else
6264 {
6265 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6266 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6267 }
6268 }
6269
6270
6271 /* Take proper action when mouse has moved to the mode or top line of
6272 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6273 mode line. X is relative to the start of the text display area of
6274 W, so the width of bitmap areas and scroll bars must be subtracted
6275 to get a position relative to the start of the mode line. */
6276
6277 static void
6278 note_mode_line_highlight (w, x, mode_line_p)
6279 struct window *w;
6280 int x, mode_line_p;
6281 {
6282 struct frame *f = XFRAME (w->frame);
6283 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6284 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6285 struct glyph_row *row;
6286
6287 if (mode_line_p)
6288 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6289 else
6290 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6291
6292 if (row->enabled_p)
6293 {
6294 struct glyph *glyph, *end;
6295 Lisp_Object help, map;
6296 int x0;
6297
6298 /* Find the glyph under X. */
6299 glyph = row->glyphs[TEXT_AREA];
6300 end = glyph + row->used[TEXT_AREA];
6301 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6302 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6303 while (glyph < end
6304 && x >= x0 + glyph->pixel_width)
6305 {
6306 x0 += glyph->pixel_width;
6307 ++glyph;
6308 }
6309
6310 if (glyph < end
6311 && STRINGP (glyph->object)
6312 && XSTRING (glyph->object)->intervals
6313 && glyph->charpos >= 0
6314 && glyph->charpos < XSTRING (glyph->object)->size)
6315 {
6316 /* If we're on a string with `help-echo' text property,
6317 arrange for the help to be displayed. This is done by
6318 setting the global variable help_echo to the help string. */
6319 help = Fget_text_property (make_number (glyph->charpos),
6320 Qhelp_echo, glyph->object);
6321 if (STRINGP (help))
6322 help_echo = help;
6323
6324 /* Change the mouse pointer according to what is under X/Y. */
6325 map = Fget_text_property (make_number (glyph->charpos),
6326 Qlocal_map, glyph->object);
6327 if (!NILP (Fkeymapp (map)))
6328 cursor = f->output_data.x->nontext_cursor;
6329 }
6330 }
6331
6332 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6333 }
6334
6335
6336 /* Take proper action when the mouse has moved to position X, Y on
6337 frame F as regards highlighting characters that have mouse-face
6338 properties. Also de-highlighting chars where the mouse was before.
6339 X and Y can be negative or out of range. */
6340
6341 static void
6342 note_mouse_highlight (f, x, y)
6343 struct frame *f;
6344 int x, y;
6345 {
6346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6347 int portion;
6348 Lisp_Object window;
6349 struct window *w;
6350
6351 /* When a menu is active, don't highlight because this looks odd. */
6352 #ifdef USE_X_TOOLKIT
6353 if (popup_activated ())
6354 return;
6355 #endif
6356
6357 if (disable_mouse_highlight
6358 || !f->glyphs_initialized_p)
6359 return;
6360
6361 dpyinfo->mouse_face_mouse_x = x;
6362 dpyinfo->mouse_face_mouse_y = y;
6363 dpyinfo->mouse_face_mouse_frame = f;
6364
6365 if (dpyinfo->mouse_face_defer)
6366 return;
6367
6368 if (gc_in_progress)
6369 {
6370 dpyinfo->mouse_face_deferred_gc = 1;
6371 return;
6372 }
6373
6374 /* Which window is that in? */
6375 window = window_from_coordinates (f, x, y, &portion, 1);
6376
6377 /* If we were displaying active text in another window, clear that. */
6378 if (! EQ (window, dpyinfo->mouse_face_window))
6379 clear_mouse_face (dpyinfo);
6380
6381 /* Not on a window -> return. */
6382 if (!WINDOWP (window))
6383 return;
6384
6385 /* Convert to window-relative pixel coordinates. */
6386 w = XWINDOW (window);
6387 frame_to_window_pixel_xy (w, &x, &y);
6388
6389 /* Handle tool-bar window differently since it doesn't display a
6390 buffer. */
6391 if (EQ (window, f->tool_bar_window))
6392 {
6393 note_tool_bar_highlight (f, x, y);
6394 return;
6395 }
6396
6397 if (portion == 1 || portion == 3)
6398 {
6399 /* Mouse is on the mode or top line. */
6400 note_mode_line_highlight (w, x, portion == 1);
6401 return;
6402 }
6403 else
6404 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6405 f->output_data.x->text_cursor);
6406
6407 /* Are we in a window whose display is up to date?
6408 And verify the buffer's text has not changed. */
6409 if (/* Within text portion of the window. */
6410 portion == 0
6411 && EQ (w->window_end_valid, w->buffer)
6412 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6413 && (XFASTINT (w->last_overlay_modified)
6414 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6415 {
6416 int hpos, vpos, pos, i, area;
6417 struct glyph *glyph;
6418
6419 /* Find the glyph under X/Y. */
6420 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6421
6422 /* Clear mouse face if X/Y not over text. */
6423 if (glyph == NULL
6424 || area != TEXT_AREA
6425 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6426 {
6427 clear_mouse_face (dpyinfo);
6428 return;
6429 }
6430
6431 pos = glyph->charpos;
6432 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6433
6434 /* Check for mouse-face and help-echo. */
6435 {
6436 Lisp_Object mouse_face, overlay, position;
6437 Lisp_Object *overlay_vec;
6438 int len, noverlays;
6439 struct buffer *obuf;
6440 int obegv, ozv;
6441
6442 /* If we get an out-of-range value, return now; avoid an error. */
6443 if (pos > BUF_Z (XBUFFER (w->buffer)))
6444 return;
6445
6446 /* Make the window's buffer temporarily current for
6447 overlays_at and compute_char_face. */
6448 obuf = current_buffer;
6449 current_buffer = XBUFFER (w->buffer);
6450 obegv = BEGV;
6451 ozv = ZV;
6452 BEGV = BEG;
6453 ZV = Z;
6454
6455 /* Is this char mouse-active or does it have help-echo? */
6456 XSETINT (position, pos);
6457
6458 /* Put all the overlays we want in a vector in overlay_vec.
6459 Store the length in len. If there are more than 10, make
6460 enough space for all, and try again. */
6461 len = 10;
6462 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6463 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6464 if (noverlays > len)
6465 {
6466 len = noverlays;
6467 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6468 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6469 }
6470
6471 noverlays = sort_overlays (overlay_vec, noverlays, w);
6472
6473 /* Check mouse-face highlighting. */
6474 if (! (EQ (window, dpyinfo->mouse_face_window)
6475 && vpos >= dpyinfo->mouse_face_beg_row
6476 && vpos <= dpyinfo->mouse_face_end_row
6477 && (vpos > dpyinfo->mouse_face_beg_row
6478 || hpos >= dpyinfo->mouse_face_beg_col)
6479 && (vpos < dpyinfo->mouse_face_end_row
6480 || hpos < dpyinfo->mouse_face_end_col
6481 || dpyinfo->mouse_face_past_end)))
6482 {
6483 /* Clear the display of the old active region, if any. */
6484 clear_mouse_face (dpyinfo);
6485
6486 /* Find the highest priority overlay that has a mouse-face prop. */
6487 overlay = Qnil;
6488 for (i = 0; i < noverlays; i++)
6489 {
6490 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6491 if (!NILP (mouse_face))
6492 {
6493 overlay = overlay_vec[i];
6494 break;
6495 }
6496 }
6497
6498 /* If no overlay applies, get a text property. */
6499 if (NILP (overlay))
6500 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6501
6502 /* Handle the overlay case. */
6503 if (! NILP (overlay))
6504 {
6505 /* Find the range of text around this char that
6506 should be active. */
6507 Lisp_Object before, after;
6508 int ignore;
6509
6510 before = Foverlay_start (overlay);
6511 after = Foverlay_end (overlay);
6512 /* Record this as the current active region. */
6513 fast_find_position (w, XFASTINT (before),
6514 &dpyinfo->mouse_face_beg_col,
6515 &dpyinfo->mouse_face_beg_row,
6516 &dpyinfo->mouse_face_beg_x,
6517 &dpyinfo->mouse_face_beg_y);
6518 dpyinfo->mouse_face_past_end
6519 = !fast_find_position (w, XFASTINT (after),
6520 &dpyinfo->mouse_face_end_col,
6521 &dpyinfo->mouse_face_end_row,
6522 &dpyinfo->mouse_face_end_x,
6523 &dpyinfo->mouse_face_end_y);
6524 dpyinfo->mouse_face_window = window;
6525 dpyinfo->mouse_face_face_id
6526 = face_at_buffer_position (w, pos, 0, 0,
6527 &ignore, pos + 1, 1);
6528
6529 /* Display it as active. */
6530 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6531 }
6532 /* Handle the text property case. */
6533 else if (! NILP (mouse_face))
6534 {
6535 /* Find the range of text around this char that
6536 should be active. */
6537 Lisp_Object before, after, beginning, end;
6538 int ignore;
6539
6540 beginning = Fmarker_position (w->start);
6541 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6542 - XFASTINT (w->window_end_pos)));
6543 before
6544 = Fprevious_single_property_change (make_number (pos + 1),
6545 Qmouse_face,
6546 w->buffer, beginning);
6547 after
6548 = Fnext_single_property_change (position, Qmouse_face,
6549 w->buffer, end);
6550 /* Record this as the current active region. */
6551 fast_find_position (w, XFASTINT (before),
6552 &dpyinfo->mouse_face_beg_col,
6553 &dpyinfo->mouse_face_beg_row,
6554 &dpyinfo->mouse_face_beg_x,
6555 &dpyinfo->mouse_face_beg_y);
6556 dpyinfo->mouse_face_past_end
6557 = !fast_find_position (w, XFASTINT (after),
6558 &dpyinfo->mouse_face_end_col,
6559 &dpyinfo->mouse_face_end_row,
6560 &dpyinfo->mouse_face_end_x,
6561 &dpyinfo->mouse_face_end_y);
6562 dpyinfo->mouse_face_window = window;
6563 dpyinfo->mouse_face_face_id
6564 = face_at_buffer_position (w, pos, 0, 0,
6565 &ignore, pos + 1, 1);
6566
6567 /* Display it as active. */
6568 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6569 }
6570 }
6571
6572 /* Look for a `help-echo' property. */
6573 {
6574 Lisp_Object help;
6575
6576 /* Check overlays first. */
6577 help = Qnil;
6578 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6579 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6580
6581 /* Try text properties. */
6582 if (!STRINGP (help)
6583 && ((STRINGP (glyph->object)
6584 && glyph->charpos >= 0
6585 && glyph->charpos < XSTRING (glyph->object)->size)
6586 || (BUFFERP (glyph->object)
6587 && glyph->charpos >= BEGV
6588 && glyph->charpos < ZV)))
6589 help = Fget_text_property (make_number (glyph->charpos),
6590 Qhelp_echo, glyph->object);
6591
6592 if (STRINGP (help))
6593 help_echo = help;
6594 }
6595
6596 BEGV = obegv;
6597 ZV = ozv;
6598 current_buffer = obuf;
6599 }
6600 }
6601 }
6602
6603 static void
6604 redo_mouse_highlight ()
6605 {
6606 if (!NILP (last_mouse_motion_frame)
6607 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6608 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6609 last_mouse_motion_event.x,
6610 last_mouse_motion_event.y);
6611 }
6612
6613
6614 \f
6615 /***********************************************************************
6616 Tool-bars
6617 ***********************************************************************/
6618
6619 static int x_tool_bar_item P_ ((struct frame *, int, int,
6620 struct glyph **, int *, int *, int *));
6621
6622 /* Tool-bar item index of the item on which a mouse button was pressed
6623 or -1. */
6624
6625 static int last_tool_bar_item;
6626
6627
6628 /* Get information about the tool-bar item at position X/Y on frame F.
6629 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6630 the current matrix of the tool-bar window of F, or NULL if not
6631 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6632 item in F->current_tool_bar_items. Value is
6633
6634 -1 if X/Y is not on a tool-bar item
6635 0 if X/Y is on the same item that was highlighted before.
6636 1 otherwise. */
6637
6638 static int
6639 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6640 struct frame *f;
6641 int x, y;
6642 struct glyph **glyph;
6643 int *hpos, *vpos, *prop_idx;
6644 {
6645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6646 struct window *w = XWINDOW (f->tool_bar_window);
6647 int area;
6648
6649 /* Find the glyph under X/Y. */
6650 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6651 if (*glyph == NULL)
6652 return -1;
6653
6654 /* Get the start of this tool-bar item's properties in
6655 f->current_tool_bar_items. */
6656 if (!tool_bar_item_info (f, *glyph, prop_idx))
6657 return -1;
6658
6659 /* Is mouse on the highlighted item? */
6660 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6661 && *vpos >= dpyinfo->mouse_face_beg_row
6662 && *vpos <= dpyinfo->mouse_face_end_row
6663 && (*vpos > dpyinfo->mouse_face_beg_row
6664 || *hpos >= dpyinfo->mouse_face_beg_col)
6665 && (*vpos < dpyinfo->mouse_face_end_row
6666 || *hpos < dpyinfo->mouse_face_end_col
6667 || dpyinfo->mouse_face_past_end))
6668 return 0;
6669
6670 return 1;
6671 }
6672
6673
6674 /* Handle mouse button event on the tool-bar of frame F, at
6675 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6676 or ButtonRelase. */
6677
6678 static void
6679 x_handle_tool_bar_click (f, button_event)
6680 struct frame *f;
6681 XButtonEvent *button_event;
6682 {
6683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6684 struct window *w = XWINDOW (f->tool_bar_window);
6685 int hpos, vpos, prop_idx;
6686 struct glyph *glyph;
6687 Lisp_Object enabled_p;
6688 int x = button_event->x;
6689 int y = button_event->y;
6690
6691 /* If not on the highlighted tool-bar item, return. */
6692 frame_to_window_pixel_xy (w, &x, &y);
6693 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6694 return;
6695
6696 /* If item is disabled, do nothing. */
6697 enabled_p = (XVECTOR (f->current_tool_bar_items)
6698 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6699 if (NILP (enabled_p))
6700 return;
6701
6702 if (button_event->type == ButtonPress)
6703 {
6704 /* Show item in pressed state. */
6705 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6706 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6707 last_tool_bar_item = prop_idx;
6708 }
6709 else
6710 {
6711 Lisp_Object key, frame;
6712 struct input_event event;
6713
6714 /* Show item in released state. */
6715 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6716 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6717
6718 key = (XVECTOR (f->current_tool_bar_items)
6719 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6720
6721 XSETFRAME (frame, f);
6722 event.kind = TOOL_BAR_EVENT;
6723 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
6724 kbd_buffer_store_event (&event);
6725
6726 event.kind = TOOL_BAR_EVENT;
6727 event.frame_or_window = Fcons (frame, key);
6728 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6729 button_event->state);
6730 kbd_buffer_store_event (&event);
6731 last_tool_bar_item = -1;
6732 }
6733 }
6734
6735
6736 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6737 tool-bar window-relative coordinates X/Y. Called from
6738 note_mouse_highlight. */
6739
6740 static void
6741 note_tool_bar_highlight (f, x, y)
6742 struct frame *f;
6743 int x, y;
6744 {
6745 Lisp_Object window = f->tool_bar_window;
6746 struct window *w = XWINDOW (window);
6747 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6748 int hpos, vpos;
6749 struct glyph *glyph;
6750 struct glyph_row *row;
6751 int i;
6752 Lisp_Object enabled_p;
6753 int prop_idx;
6754 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6755 int mouse_down_p, rc;
6756
6757 /* Function note_mouse_highlight is called with negative x(y
6758 values when mouse moves outside of the frame. */
6759 if (x <= 0 || y <= 0)
6760 {
6761 clear_mouse_face (dpyinfo);
6762 return;
6763 }
6764
6765 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6766 if (rc < 0)
6767 {
6768 /* Not on tool-bar item. */
6769 clear_mouse_face (dpyinfo);
6770 return;
6771 }
6772 else if (rc == 0)
6773 /* On same tool-bar item as before. */
6774 goto set_help_echo;
6775
6776 clear_mouse_face (dpyinfo);
6777
6778 /* Mouse is down, but on different tool-bar item? */
6779 mouse_down_p = (dpyinfo->grabbed
6780 && f == last_mouse_frame
6781 && FRAME_LIVE_P (f));
6782 if (mouse_down_p
6783 && last_tool_bar_item != prop_idx)
6784 return;
6785
6786 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6787 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6788
6789 /* If tool-bar item is not enabled, don't highlight it. */
6790 enabled_p = (XVECTOR (f->current_tool_bar_items)
6791 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6792 if (!NILP (enabled_p))
6793 {
6794 /* Compute the x-position of the glyph. In front and past the
6795 image is a space. We include this is the highlighted area. */
6796 row = MATRIX_ROW (w->current_matrix, vpos);
6797 for (i = x = 0; i < hpos; ++i)
6798 x += row->glyphs[TEXT_AREA][i].pixel_width;
6799
6800 /* Record this as the current active region. */
6801 dpyinfo->mouse_face_beg_col = hpos;
6802 dpyinfo->mouse_face_beg_row = vpos;
6803 dpyinfo->mouse_face_beg_x = x;
6804 dpyinfo->mouse_face_beg_y = row->y;
6805 dpyinfo->mouse_face_past_end = 0;
6806
6807 dpyinfo->mouse_face_end_col = hpos + 1;
6808 dpyinfo->mouse_face_end_row = vpos;
6809 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6810 dpyinfo->mouse_face_end_y = row->y;
6811 dpyinfo->mouse_face_window = window;
6812 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6813
6814 /* Display it as active. */
6815 show_mouse_face (dpyinfo, draw);
6816 dpyinfo->mouse_face_image_state = draw;
6817 }
6818
6819 set_help_echo:
6820
6821 /* Set help_echo to a help string.to display for this tool-bar item.
6822 XTread_socket does the rest. */
6823 help_echo = (XVECTOR (f->current_tool_bar_items)
6824 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6825 if (!STRINGP (help_echo))
6826 help_echo = (XVECTOR (f->current_tool_bar_items)
6827 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6828 }
6829
6830
6831 \f
6832 /* Find the glyph matrix position of buffer position POS in window W.
6833 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6834 current glyphs must be up to date. If POS is above window start
6835 return (0, 0, 0, 0). If POS is after end of W, return end of
6836 last line in W. */
6837
6838 static int
6839 fast_find_position (w, pos, hpos, vpos, x, y)
6840 struct window *w;
6841 int pos;
6842 int *hpos, *vpos, *x, *y;
6843 {
6844 int i;
6845 int lastcol;
6846 int maybe_next_line_p = 0;
6847 int line_start_position;
6848 int yb = window_text_bottom_y (w);
6849 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6850 struct glyph_row *best_row = row;
6851 int row_vpos = 0, best_row_vpos = 0;
6852 int current_x;
6853
6854 while (row->y < yb)
6855 {
6856 if (row->used[TEXT_AREA])
6857 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6858 else
6859 line_start_position = 0;
6860
6861 if (line_start_position > pos)
6862 break;
6863 /* If the position sought is the end of the buffer,
6864 don't include the blank lines at the bottom of the window. */
6865 else if (line_start_position == pos
6866 && pos == BUF_ZV (XBUFFER (w->buffer)))
6867 {
6868 maybe_next_line_p = 1;
6869 break;
6870 }
6871 else if (line_start_position > 0)
6872 {
6873 best_row = row;
6874 best_row_vpos = row_vpos;
6875 }
6876
6877 if (row->y + row->height >= yb)
6878 break;
6879
6880 ++row;
6881 ++row_vpos;
6882 }
6883
6884 /* Find the right column within BEST_ROW. */
6885 lastcol = 0;
6886 current_x = best_row->x;
6887 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6888 {
6889 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6890 int charpos;
6891
6892 charpos = glyph->charpos;
6893 if (charpos == pos)
6894 {
6895 *hpos = i;
6896 *vpos = best_row_vpos;
6897 *x = current_x;
6898 *y = best_row->y;
6899 return 1;
6900 }
6901 else if (charpos > pos)
6902 break;
6903 else if (charpos > 0)
6904 lastcol = i;
6905
6906 current_x += glyph->pixel_width;
6907 }
6908
6909 /* If we're looking for the end of the buffer,
6910 and we didn't find it in the line we scanned,
6911 use the start of the following line. */
6912 if (maybe_next_line_p)
6913 {
6914 ++best_row;
6915 ++best_row_vpos;
6916 lastcol = 0;
6917 current_x = best_row->x;
6918 }
6919
6920 *vpos = best_row_vpos;
6921 *hpos = lastcol + 1;
6922 *x = current_x;
6923 *y = best_row->y;
6924 return 0;
6925 }
6926
6927
6928 /* Display the active region described by mouse_face_*
6929 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6930
6931 static void
6932 show_mouse_face (dpyinfo, draw)
6933 struct x_display_info *dpyinfo;
6934 enum draw_glyphs_face draw;
6935 {
6936 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6937 struct frame *f = XFRAME (WINDOW_FRAME (w));
6938 int i;
6939 int cursor_off_p = 0;
6940 struct cursor_pos saved_cursor;
6941
6942 saved_cursor = output_cursor;
6943
6944 /* If window is in the process of being destroyed, don't bother
6945 to do anything. */
6946 if (w->current_matrix == NULL)
6947 goto set_x_cursor;
6948
6949 /* Recognize when we are called to operate on rows that don't exist
6950 anymore. This can happen when a window is split. */
6951 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6952 goto set_x_cursor;
6953
6954 set_output_cursor (&w->phys_cursor);
6955
6956 /* Note that mouse_face_beg_row etc. are window relative. */
6957 for (i = dpyinfo->mouse_face_beg_row;
6958 i <= dpyinfo->mouse_face_end_row;
6959 i++)
6960 {
6961 int start_hpos, end_hpos, start_x;
6962 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6963
6964 /* Don't do anything if row doesn't have valid contents. */
6965 if (!row->enabled_p)
6966 continue;
6967
6968 /* For all but the first row, the highlight starts at column 0. */
6969 if (i == dpyinfo->mouse_face_beg_row)
6970 {
6971 start_hpos = dpyinfo->mouse_face_beg_col;
6972 start_x = dpyinfo->mouse_face_beg_x;
6973 }
6974 else
6975 {
6976 start_hpos = 0;
6977 start_x = 0;
6978 }
6979
6980 if (i == dpyinfo->mouse_face_end_row)
6981 end_hpos = dpyinfo->mouse_face_end_col;
6982 else
6983 end_hpos = row->used[TEXT_AREA];
6984
6985 /* If the cursor's in the text we are about to rewrite, turn the
6986 cursor off. */
6987 if (!w->pseudo_window_p
6988 && i == output_cursor.vpos
6989 && output_cursor.hpos >= start_hpos - 1
6990 && output_cursor.hpos <= end_hpos)
6991 {
6992 x_update_window_cursor (w, 0);
6993 cursor_off_p = 1;
6994 }
6995
6996 if (end_hpos > start_hpos)
6997 x_draw_glyphs (w, start_x, row, TEXT_AREA,
6998 start_hpos, end_hpos, draw, NULL, NULL, 0);
6999 }
7000
7001 /* If we turned the cursor off, turn it back on. */
7002 if (cursor_off_p)
7003 x_display_cursor (w, 1,
7004 output_cursor.hpos, output_cursor.vpos,
7005 output_cursor.x, output_cursor.y);
7006
7007 output_cursor = saved_cursor;
7008
7009 set_x_cursor:
7010
7011 /* Change the mouse cursor. */
7012 if (draw == DRAW_NORMAL_TEXT)
7013 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7014 f->output_data.x->text_cursor);
7015 else if (draw == DRAW_MOUSE_FACE)
7016 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7017 f->output_data.x->cross_cursor);
7018 else
7019 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7020 f->output_data.x->nontext_cursor);
7021 }
7022
7023 /* Clear out the mouse-highlighted active region.
7024 Redraw it un-highlighted first. */
7025
7026 void
7027 clear_mouse_face (dpyinfo)
7028 struct x_display_info *dpyinfo;
7029 {
7030 if (tip_frame)
7031 return;
7032
7033 if (! NILP (dpyinfo->mouse_face_window))
7034 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7035
7036 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7037 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7038 dpyinfo->mouse_face_window = Qnil;
7039 }
7040
7041 /* Just discard the mouse face information for frame F, if any.
7042 This is used when the size of F is changed. */
7043
7044 void
7045 cancel_mouse_face (f)
7046 FRAME_PTR f;
7047 {
7048 Lisp_Object window;
7049 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7050
7051 window = dpyinfo->mouse_face_window;
7052 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7053 {
7054 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7055 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7056 dpyinfo->mouse_face_window = Qnil;
7057 }
7058 }
7059 \f
7060 static struct scroll_bar *x_window_to_scroll_bar ();
7061 static void x_scroll_bar_report_motion ();
7062
7063 /* Return the current position of the mouse.
7064 *fp should be a frame which indicates which display to ask about.
7065
7066 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7067 and *part to the frame, window, and scroll bar part that the mouse
7068 is over. Set *x and *y to the portion and whole of the mouse's
7069 position on the scroll bar.
7070
7071 If the mouse movement started elsewhere, set *fp to the frame the
7072 mouse is on, *bar_window to nil, and *x and *y to the character cell
7073 the mouse is over.
7074
7075 Set *time to the server time-stamp for the time at which the mouse
7076 was at this position.
7077
7078 Don't store anything if we don't have a valid set of values to report.
7079
7080 This clears the mouse_moved flag, so we can wait for the next mouse
7081 movement. */
7082
7083 static void
7084 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7085 FRAME_PTR *fp;
7086 int insist;
7087 Lisp_Object *bar_window;
7088 enum scroll_bar_part *part;
7089 Lisp_Object *x, *y;
7090 unsigned long *time;
7091 {
7092 FRAME_PTR f1;
7093
7094 BLOCK_INPUT;
7095
7096 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7097 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7098 else
7099 {
7100 Window root;
7101 int root_x, root_y;
7102
7103 Window dummy_window;
7104 int dummy;
7105
7106 Lisp_Object frame, tail;
7107
7108 /* Clear the mouse-moved flag for every frame on this display. */
7109 FOR_EACH_FRAME (tail, frame)
7110 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7111 XFRAME (frame)->mouse_moved = 0;
7112
7113 last_mouse_scroll_bar = Qnil;
7114
7115 /* Figure out which root window we're on. */
7116 XQueryPointer (FRAME_X_DISPLAY (*fp),
7117 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7118
7119 /* The root window which contains the pointer. */
7120 &root,
7121
7122 /* Trash which we can't trust if the pointer is on
7123 a different screen. */
7124 &dummy_window,
7125
7126 /* The position on that root window. */
7127 &root_x, &root_y,
7128
7129 /* More trash we can't trust. */
7130 &dummy, &dummy,
7131
7132 /* Modifier keys and pointer buttons, about which
7133 we don't care. */
7134 (unsigned int *) &dummy);
7135
7136 /* Now we have a position on the root; find the innermost window
7137 containing the pointer. */
7138 {
7139 Window win, child;
7140 int win_x, win_y;
7141 int parent_x = 0, parent_y = 0;
7142 int count;
7143
7144 win = root;
7145
7146 /* XTranslateCoordinates can get errors if the window
7147 structure is changing at the same time this function
7148 is running. So at least we must not crash from them. */
7149
7150 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7151
7152 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7153 && FRAME_LIVE_P (last_mouse_frame))
7154 {
7155 /* If mouse was grabbed on a frame, give coords for that frame
7156 even if the mouse is now outside it. */
7157 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7158
7159 /* From-window, to-window. */
7160 root, FRAME_X_WINDOW (last_mouse_frame),
7161
7162 /* From-position, to-position. */
7163 root_x, root_y, &win_x, &win_y,
7164
7165 /* Child of win. */
7166 &child);
7167 f1 = last_mouse_frame;
7168 }
7169 else
7170 {
7171 while (1)
7172 {
7173 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7174
7175 /* From-window, to-window. */
7176 root, win,
7177
7178 /* From-position, to-position. */
7179 root_x, root_y, &win_x, &win_y,
7180
7181 /* Child of win. */
7182 &child);
7183
7184 if (child == None || child == win)
7185 break;
7186
7187 win = child;
7188 parent_x = win_x;
7189 parent_y = win_y;
7190 }
7191
7192 /* Now we know that:
7193 win is the innermost window containing the pointer
7194 (XTC says it has no child containing the pointer),
7195 win_x and win_y are the pointer's position in it
7196 (XTC did this the last time through), and
7197 parent_x and parent_y are the pointer's position in win's parent.
7198 (They are what win_x and win_y were when win was child.
7199 If win is the root window, it has no parent, and
7200 parent_{x,y} are invalid, but that's okay, because we'll
7201 never use them in that case.) */
7202
7203 /* Is win one of our frames? */
7204 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7205 }
7206
7207 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7208 f1 = 0;
7209
7210 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7211
7212 /* If not, is it one of our scroll bars? */
7213 if (! f1)
7214 {
7215 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7216
7217 if (bar)
7218 {
7219 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7220 win_x = parent_x;
7221 win_y = parent_y;
7222 }
7223 }
7224
7225 if (f1 == 0 && insist > 0)
7226 f1 = SELECTED_FRAME ();
7227
7228 if (f1)
7229 {
7230 /* Ok, we found a frame. Store all the values.
7231 last_mouse_glyph is a rectangle used to reduce the
7232 generation of mouse events. To not miss any motion
7233 events, we must divide the frame into rectangles of the
7234 size of the smallest character that could be displayed
7235 on it, i.e. into the same rectangles that matrices on
7236 the frame are divided into. */
7237
7238 #if OLD_REDISPLAY_CODE
7239 int ignore1, ignore2;
7240 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7241 &last_mouse_glyph,
7242 FRAME_X_DISPLAY_INFO (f1)->grabbed
7243 || insist);
7244 #else
7245 {
7246 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7247 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7248 int x = win_x;
7249 int y = win_y;
7250
7251 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7252 round down even for negative values. */
7253 if (x < 0)
7254 x -= width - 1;
7255 if (y < 0)
7256 y -= height - 1;
7257
7258 last_mouse_glyph.width = width;
7259 last_mouse_glyph.height = height;
7260 last_mouse_glyph.x = (x + width - 1) / width * width;
7261 last_mouse_glyph.y = (y + height - 1) / height * height;
7262 }
7263 #endif
7264
7265 *bar_window = Qnil;
7266 *part = 0;
7267 *fp = f1;
7268 XSETINT (*x, win_x);
7269 XSETINT (*y, win_y);
7270 *time = last_mouse_movement_time;
7271 }
7272 }
7273 }
7274
7275 UNBLOCK_INPUT;
7276 }
7277
7278
7279 #ifdef USE_X_TOOLKIT
7280
7281 /* Atimer callback function for TIMER. Called every 0.1s to process
7282 Xt timeouts, if needed. We must avoid calling XtAppPending as
7283 much as possible because that function does an implicit XFlush
7284 that slows us down. */
7285
7286 static void
7287 x_process_timeouts (timer)
7288 struct atimer *timer;
7289 {
7290 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7291 {
7292 BLOCK_INPUT;
7293 while (XtAppPending (Xt_app_con) & XtIMTimer)
7294 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7295 UNBLOCK_INPUT;
7296 }
7297 }
7298
7299 #endif /* USE_X_TOOLKIT */
7300
7301 \f
7302 /* Scroll bar support. */
7303
7304 /* Given an X window ID, find the struct scroll_bar which manages it.
7305 This can be called in GC, so we have to make sure to strip off mark
7306 bits. */
7307
7308 static struct scroll_bar *
7309 x_window_to_scroll_bar (window_id)
7310 Window window_id;
7311 {
7312 Lisp_Object tail;
7313
7314 for (tail = Vframe_list;
7315 XGCTYPE (tail) == Lisp_Cons;
7316 tail = XCDR (tail))
7317 {
7318 Lisp_Object frame, bar, condemned;
7319
7320 frame = XCAR (tail);
7321 /* All elements of Vframe_list should be frames. */
7322 if (! GC_FRAMEP (frame))
7323 abort ();
7324
7325 /* Scan this frame's scroll bar list for a scroll bar with the
7326 right window ID. */
7327 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7328 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7329 /* This trick allows us to search both the ordinary and
7330 condemned scroll bar lists with one loop. */
7331 ! GC_NILP (bar) || (bar = condemned,
7332 condemned = Qnil,
7333 ! GC_NILP (bar));
7334 bar = XSCROLL_BAR (bar)->next)
7335 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7336 return XSCROLL_BAR (bar);
7337 }
7338
7339 return 0;
7340 }
7341
7342
7343 \f
7344 /************************************************************************
7345 Toolkit scroll bars
7346 ************************************************************************/
7347
7348 #if USE_TOOLKIT_SCROLL_BARS
7349
7350 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7351 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7352 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7353 struct scroll_bar *));
7354 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7355 int, int, int));
7356
7357
7358 /* Id of action hook installed for scroll bars. */
7359
7360 static XtActionHookId action_hook_id;
7361
7362 /* Lisp window being scrolled. Set when starting to interact with
7363 a toolkit scroll bar, reset to nil when ending the interaction. */
7364
7365 static Lisp_Object window_being_scrolled;
7366
7367 /* Last scroll bar part sent in xm_scroll_callback. */
7368
7369 static int last_scroll_bar_part;
7370
7371 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7372 that movements of 1/20 of the screen size are mapped to up/down. */
7373
7374 static Boolean xaw3d_arrow_scroll;
7375
7376 /* Whether the drag scrolling maintains the mouse at the top of the
7377 thumb. If not, resizing the thumb needs to be done more carefully
7378 to avoid jerkyness. */
7379
7380 static Boolean xaw3d_pick_top;
7381
7382
7383 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7384 bars are used.. The hook is responsible for detecting when
7385 the user ends an interaction with the scroll bar, and generates
7386 a `end-scroll' scroll_bar_click' event if so. */
7387
7388 static void
7389 xt_action_hook (widget, client_data, action_name, event, params,
7390 num_params)
7391 Widget widget;
7392 XtPointer client_data;
7393 String action_name;
7394 XEvent *event;
7395 String *params;
7396 Cardinal *num_params;
7397 {
7398 int scroll_bar_p;
7399 char *end_action;
7400
7401 #ifdef USE_MOTIF
7402 scroll_bar_p = XmIsScrollBar (widget);
7403 end_action = "Release";
7404 #else /* !USE_MOTIF i.e. use Xaw */
7405 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7406 end_action = "EndScroll";
7407 #endif /* USE_MOTIF */
7408
7409 if (scroll_bar_p
7410 && strcmp (action_name, end_action) == 0
7411 && WINDOWP (window_being_scrolled))
7412 {
7413 struct window *w;
7414
7415 x_send_scroll_bar_event (window_being_scrolled,
7416 scroll_bar_end_scroll, 0, 0);
7417 w = XWINDOW (window_being_scrolled);
7418 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7419 window_being_scrolled = Qnil;
7420 last_scroll_bar_part = -1;
7421
7422 /* Xt timeouts no longer needed. */
7423 toolkit_scroll_bar_interaction = 0;
7424 }
7425 }
7426
7427
7428 /* Send a client message with message type Xatom_Scrollbar for a
7429 scroll action to the frame of WINDOW. PART is a value identifying
7430 the part of the scroll bar that was clicked on. PORTION is the
7431 amount to scroll of a whole of WHOLE. */
7432
7433 static void
7434 x_send_scroll_bar_event (window, part, portion, whole)
7435 Lisp_Object window;
7436 int part, portion, whole;
7437 {
7438 XEvent event;
7439 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7440 struct frame *f = XFRAME (XWINDOW (window)->frame);
7441
7442 /* Construct a ClientMessage event to send to the frame. */
7443 ev->type = ClientMessage;
7444 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7445 ev->display = FRAME_X_DISPLAY (f);
7446 ev->window = FRAME_X_WINDOW (f);
7447 ev->format = 32;
7448 ev->data.l[0] = (long) XFASTINT (window);
7449 ev->data.l[1] = (long) part;
7450 ev->data.l[2] = (long) 0;
7451 ev->data.l[3] = (long) portion;
7452 ev->data.l[4] = (long) whole;
7453
7454 /* Make Xt timeouts work while the scroll bar is active. */
7455 toolkit_scroll_bar_interaction = 1;
7456
7457 /* Setting the event mask to zero means that the message will
7458 be sent to the client that created the window, and if that
7459 window no longer exists, no event will be sent. */
7460 BLOCK_INPUT;
7461 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7462 UNBLOCK_INPUT;
7463 }
7464
7465
7466 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7467 in *IEVENT. */
7468
7469 static void
7470 x_scroll_bar_to_input_event (event, ievent)
7471 XEvent *event;
7472 struct input_event *ievent;
7473 {
7474 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7475 Lisp_Object window;
7476 struct frame *f;
7477
7478 XSETFASTINT (window, ev->data.l[0]);
7479 f = XFRAME (XWINDOW (window)->frame);
7480
7481 ievent->kind = scroll_bar_click;
7482 ievent->frame_or_window = window;
7483 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7484 ievent->part = ev->data.l[1];
7485 ievent->code = ev->data.l[2];
7486 ievent->x = make_number ((int) ev->data.l[3]);
7487 ievent->y = make_number ((int) ev->data.l[4]);
7488 ievent->modifiers = 0;
7489 }
7490
7491
7492 #ifdef USE_MOTIF
7493
7494 /* Minimum and maximum values used for Motif scroll bars. */
7495
7496 #define XM_SB_MIN 1
7497 #define XM_SB_MAX 10000000
7498 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7499
7500
7501 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7502 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7503 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7504
7505 static void
7506 xm_scroll_callback (widget, client_data, call_data)
7507 Widget widget;
7508 XtPointer client_data, call_data;
7509 {
7510 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7511 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7512 double percent;
7513 int part = -1, whole = 0, portion = 0;
7514
7515 switch (cs->reason)
7516 {
7517 case XmCR_DECREMENT:
7518 bar->dragging = Qnil;
7519 part = scroll_bar_up_arrow;
7520 break;
7521
7522 case XmCR_INCREMENT:
7523 bar->dragging = Qnil;
7524 part = scroll_bar_down_arrow;
7525 break;
7526
7527 case XmCR_PAGE_DECREMENT:
7528 bar->dragging = Qnil;
7529 part = scroll_bar_above_handle;
7530 break;
7531
7532 case XmCR_PAGE_INCREMENT:
7533 bar->dragging = Qnil;
7534 part = scroll_bar_below_handle;
7535 break;
7536
7537 case XmCR_TO_TOP:
7538 bar->dragging = Qnil;
7539 part = scroll_bar_to_top;
7540 break;
7541
7542 case XmCR_TO_BOTTOM:
7543 bar->dragging = Qnil;
7544 part = scroll_bar_to_bottom;
7545 break;
7546
7547 case XmCR_DRAG:
7548 {
7549 int slider_size;
7550 int dragging_down_p = (INTEGERP (bar->dragging)
7551 && XINT (bar->dragging) <= cs->value);
7552
7553 /* Get the slider size. */
7554 BLOCK_INPUT;
7555 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7556 UNBLOCK_INPUT;
7557
7558 /* At the max position of the scroll bar, do a line-wise
7559 movement. Without doing anything, the LessTif scroll bar
7560 calls us with the same cs->value again and again. If we
7561 want to make sure that we can reach the end of the buffer,
7562 we have to do something.
7563
7564 Implementation note: setting bar->dragging always to
7565 cs->value gives a smoother movement at the max position.
7566 Setting it to nil when doing line-wise movement gives
7567 a better slider behavior. */
7568
7569 if (cs->value + slider_size == XM_SB_MAX
7570 || (dragging_down_p
7571 && last_scroll_bar_part == scroll_bar_down_arrow))
7572 {
7573 part = scroll_bar_down_arrow;
7574 bar->dragging = Qnil;
7575 }
7576 else
7577 {
7578 whole = XM_SB_RANGE;
7579 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7580 part = scroll_bar_handle;
7581 bar->dragging = make_number (cs->value);
7582 }
7583 }
7584 break;
7585
7586 case XmCR_VALUE_CHANGED:
7587 break;
7588 };
7589
7590 if (part >= 0)
7591 {
7592 window_being_scrolled = bar->window;
7593 last_scroll_bar_part = part;
7594 x_send_scroll_bar_event (bar->window, part, portion, whole);
7595 }
7596 }
7597
7598
7599 #else /* !USE_MOTIF, i.e. Xaw. */
7600
7601
7602 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7603 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7604 scroll bar struct. CALL_DATA is a pointer to a float saying where
7605 the thumb is. */
7606
7607 static void
7608 xaw_jump_callback (widget, client_data, call_data)
7609 Widget widget;
7610 XtPointer client_data, call_data;
7611 {
7612 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7613 float top = *(float *) call_data;
7614 float shown;
7615 int whole, portion, height;
7616 int part;
7617
7618 /* Get the size of the thumb, a value between 0 and 1. */
7619 BLOCK_INPUT;
7620 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7621 UNBLOCK_INPUT;
7622
7623 whole = 10000000;
7624 portion = shown < 1 ? top * whole : 0;
7625
7626 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7627 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7628 the bottom, so we force the scrolling whenever we see that we're
7629 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7630 we try to ensure that we always stay two pixels away from the
7631 bottom). */
7632 part = scroll_bar_down_arrow;
7633 else
7634 part = scroll_bar_handle;
7635
7636 window_being_scrolled = bar->window;
7637 bar->dragging = make_number (portion);
7638 last_scroll_bar_part = part;
7639 x_send_scroll_bar_event (bar->window, part, portion, whole);
7640 }
7641
7642
7643 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7644 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7645 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7646 the scroll bar. CALL_DATA is an integer specifying the action that
7647 has taken place. It's magnitude is in the range 0..height of the
7648 scroll bar. Negative values mean scroll towards buffer start.
7649 Values < height of scroll bar mean line-wise movement. */
7650
7651 static void
7652 xaw_scroll_callback (widget, client_data, call_data)
7653 Widget widget;
7654 XtPointer client_data, call_data;
7655 {
7656 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7657 int position = (int) call_data;
7658 Dimension height;
7659 int part;
7660
7661 /* Get the height of the scroll bar. */
7662 BLOCK_INPUT;
7663 XtVaGetValues (widget, XtNheight, &height, NULL);
7664 UNBLOCK_INPUT;
7665
7666 if (abs (position) >= height)
7667 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7668
7669 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7670 it maps line-movement to call_data = max(5, height/20). */
7671 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7672 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7673 else
7674 part = scroll_bar_move_ratio;
7675
7676 window_being_scrolled = bar->window;
7677 bar->dragging = Qnil;
7678 last_scroll_bar_part = part;
7679 x_send_scroll_bar_event (bar->window, part, position, height);
7680 }
7681
7682
7683 #endif /* not USE_MOTIF */
7684
7685
7686 /* Create the widget for scroll bar BAR on frame F. Record the widget
7687 and X window of the scroll bar in BAR. */
7688
7689 static void
7690 x_create_toolkit_scroll_bar (f, bar)
7691 struct frame *f;
7692 struct scroll_bar *bar;
7693 {
7694 Window xwindow;
7695 Widget widget;
7696 Arg av[20];
7697 int ac = 0;
7698 char *scroll_bar_name = "verticalScrollBar";
7699 unsigned long pixel;
7700
7701 BLOCK_INPUT;
7702
7703 #ifdef USE_MOTIF
7704 /* LessTif 0.85, problems:
7705
7706 1. When the mouse if over the scroll bar, the scroll bar will
7707 get keyboard events. I didn't find a way to turn this off.
7708
7709 2. Do we have to explicitly set the cursor to get an arrow
7710 cursor (see below)? */
7711
7712 /* Set resources. Create the widget. */
7713 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7714 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7715 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7716 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7717 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7718 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7719 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7720
7721 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7722 if (pixel != -1)
7723 {
7724 XtSetArg (av[ac], XmNforeground, pixel);
7725 ++ac;
7726 }
7727
7728 pixel = f->output_data.x->scroll_bar_background_pixel;
7729 if (pixel != -1)
7730 {
7731 XtSetArg (av[ac], XmNbackground, pixel);
7732 ++ac;
7733 }
7734
7735 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7736 scroll_bar_name, av, ac);
7737
7738 /* Add one callback for everything that can happen. */
7739 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7740 (XtPointer) bar);
7741 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7742 (XtPointer) bar);
7743 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7744 (XtPointer) bar);
7745 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7746 (XtPointer) bar);
7747 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7748 (XtPointer) bar);
7749 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7750 (XtPointer) bar);
7751 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7752 (XtPointer) bar);
7753
7754 /* Realize the widget. Only after that is the X window created. */
7755 XtRealizeWidget (widget);
7756
7757 /* Set the cursor to an arrow. I didn't find a resource to do that.
7758 And I'm wondering why it hasn't an arrow cursor by default. */
7759 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7760 f->output_data.x->nontext_cursor);
7761
7762 #else /* !USE_MOTIF i.e. use Xaw */
7763
7764 /* Set resources. Create the widget. The background of the
7765 Xaw3d scroll bar widget is a little bit light for my taste.
7766 We don't alter it here to let users change it according
7767 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7768 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7769 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7770 /* For smoother scrolling with Xaw3d -sm */
7771 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7772 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7773
7774 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7775 if (pixel != -1)
7776 {
7777 XtSetArg (av[ac], XtNforeground, pixel);
7778 ++ac;
7779 }
7780
7781 pixel = f->output_data.x->scroll_bar_background_pixel;
7782 if (pixel != -1)
7783 {
7784 XtSetArg (av[ac], XtNbackground, pixel);
7785 ++ac;
7786 }
7787
7788 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7789 f->output_data.x->edit_widget, av, ac);
7790
7791 {
7792 char *initial = "";
7793 char *val = initial;
7794 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7795 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7796 if (val == initial)
7797 { /* ARROW_SCROLL */
7798 xaw3d_arrow_scroll = True;
7799 /* Isn't that just a personal preference ? -sm */
7800 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7801 }
7802 }
7803
7804 /* Define callbacks. */
7805 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7806 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7807 (XtPointer) bar);
7808
7809 /* Realize the widget. Only after that is the X window created. */
7810 XtRealizeWidget (widget);
7811
7812 #endif /* !USE_MOTIF */
7813
7814 /* Install an action hook that let's us detect when the user
7815 finishes interacting with a scroll bar. */
7816 if (action_hook_id == 0)
7817 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7818
7819 /* Remember X window and widget in the scroll bar vector. */
7820 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7821 xwindow = XtWindow (widget);
7822 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7823
7824 UNBLOCK_INPUT;
7825 }
7826
7827
7828 /* Set the thumb size and position of scroll bar BAR. We are currently
7829 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7830
7831 static void
7832 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7833 struct scroll_bar *bar;
7834 int portion, position, whole;
7835 {
7836 float top, shown;
7837 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7838
7839 if (whole == 0)
7840 top = 0, shown = 1;
7841 else
7842 {
7843 top = (float) position / whole;
7844 shown = (float) portion / whole;
7845 }
7846
7847 BLOCK_INPUT;
7848
7849 #ifdef USE_MOTIF
7850 {
7851 int size, value;
7852 Boolean arrow1_selected, arrow2_selected;
7853 unsigned char flags;
7854 XmScrollBarWidget sb;
7855
7856 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7857 is the scroll bar's maximum and MIN is the scroll bar's minimum
7858 value. */
7859 size = shown * XM_SB_RANGE;
7860 size = min (size, XM_SB_RANGE);
7861 size = max (size, 1);
7862
7863 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7864 value = top * XM_SB_RANGE;
7865 value = min (value, XM_SB_MAX - size);
7866 value = max (value, XM_SB_MIN);
7867
7868 /* LessTif: Calling XmScrollBarSetValues after an increment or
7869 decrement turns off auto-repeat LessTif-internally. This can
7870 be seen in ScrollBar.c which resets Arrow1Selected and
7871 Arrow2Selected. It also sets internal flags so that LessTif
7872 believes the mouse is in the slider. We either have to change
7873 our code, or work around that by accessing private data. */
7874
7875 sb = (XmScrollBarWidget) widget;
7876 arrow1_selected = sb->scrollBar.arrow1_selected;
7877 arrow2_selected = sb->scrollBar.arrow2_selected;
7878 flags = sb->scrollBar.flags;
7879
7880 if (NILP (bar->dragging))
7881 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7882 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7883 /* This has the negative side effect that the slider value is
7884 not what it would be if we scrolled here using line-wise or
7885 page-wise movement. */
7886 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7887 else
7888 {
7889 /* If currently dragging, only update the slider size.
7890 This reduces flicker effects. */
7891 int old_value, old_size, increment, page_increment;
7892
7893 XmScrollBarGetValues (widget, &old_value, &old_size,
7894 &increment, &page_increment);
7895 XmScrollBarSetValues (widget, old_value,
7896 min (size, XM_SB_RANGE - old_value),
7897 0, 0, False);
7898 }
7899
7900 sb->scrollBar.arrow1_selected = arrow1_selected;
7901 sb->scrollBar.arrow2_selected = arrow2_selected;
7902 sb->scrollBar.flags = flags;
7903 }
7904 #else /* !USE_MOTIF i.e. use Xaw */
7905 {
7906 float old_top, old_shown;
7907 Dimension height;
7908 XtVaGetValues (widget,
7909 XtNtopOfThumb, &old_top,
7910 XtNshown, &old_shown,
7911 XtNheight, &height,
7912 NULL);
7913
7914 /* Massage the top+shown values. */
7915 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7916 top = max (0, min (1, top));
7917 else
7918 top = old_top;
7919 /* Keep two pixels available for moving the thumb down. */
7920 shown = max (0, min (1 - top - (2.0 / height), shown));
7921
7922 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7923 check that your system's configuration file contains a define
7924 for `NARROWPROTO'. See s/freebsd.h for an example. */
7925 if (top != old_top || shown != old_shown)
7926 {
7927 if (NILP (bar->dragging))
7928 XawScrollbarSetThumb (widget, top, shown);
7929 else
7930 {
7931 #ifdef HAVE_XAW3D
7932 ScrollbarWidget sb = (ScrollbarWidget) widget;
7933 int scroll_mode = 0;
7934
7935 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7936 if (xaw3d_arrow_scroll)
7937 {
7938 /* Xaw3d stupidly ignores resize requests while dragging
7939 so we have to make it believe it's not in dragging mode. */
7940 scroll_mode = sb->scrollbar.scroll_mode;
7941 if (scroll_mode == 2)
7942 sb->scrollbar.scroll_mode = 0;
7943 }
7944 #endif
7945 /* Try to make the scrolling a tad smoother. */
7946 if (!xaw3d_pick_top)
7947 shown = min (shown, old_shown);
7948
7949 XawScrollbarSetThumb (widget, top, shown);
7950
7951 #ifdef HAVE_XAW3D
7952 if (xaw3d_arrow_scroll && scroll_mode == 2)
7953 sb->scrollbar.scroll_mode = scroll_mode;
7954 #endif
7955 }
7956 }
7957 }
7958 #endif /* !USE_MOTIF */
7959
7960 UNBLOCK_INPUT;
7961 }
7962
7963 #endif /* USE_TOOLKIT_SCROLL_BARS */
7964
7965
7966 \f
7967 /************************************************************************
7968 Scroll bars, general
7969 ************************************************************************/
7970
7971 /* Create a scroll bar and return the scroll bar vector for it. W is
7972 the Emacs window on which to create the scroll bar. TOP, LEFT,
7973 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7974 scroll bar. */
7975
7976 static struct scroll_bar *
7977 x_scroll_bar_create (w, top, left, width, height)
7978 struct window *w;
7979 int top, left, width, height;
7980 {
7981 struct frame *f = XFRAME (w->frame);
7982 struct scroll_bar *bar
7983 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7984
7985 BLOCK_INPUT;
7986
7987 #if USE_TOOLKIT_SCROLL_BARS
7988 x_create_toolkit_scroll_bar (f, bar);
7989 #else /* not USE_TOOLKIT_SCROLL_BARS */
7990 {
7991 XSetWindowAttributes a;
7992 unsigned long mask;
7993 Window window;
7994
7995 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7996 if (a.background_pixel == -1)
7997 a.background_pixel = f->output_data.x->background_pixel;
7998
7999 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8000 | ButtonMotionMask | PointerMotionHintMask
8001 | ExposureMask);
8002 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8003
8004 mask = (CWBackPixel | CWEventMask | CWCursor);
8005
8006 /* Clear the area of W that will serve as a scroll bar. This is
8007 for the case that a window has been split horizontally. In
8008 this case, no clear_frame is generated to reduce flickering. */
8009 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8010 left, top, width,
8011 window_box_height (w), False);
8012
8013 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8014 /* Position and size of scroll bar. */
8015 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8016 top,
8017 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8018 height,
8019 /* Border width, depth, class, and visual. */
8020 0,
8021 CopyFromParent,
8022 CopyFromParent,
8023 CopyFromParent,
8024 /* Attributes. */
8025 mask, &a);
8026 SET_SCROLL_BAR_X_WINDOW (bar, window);
8027 }
8028 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8029
8030 XSETWINDOW (bar->window, w);
8031 XSETINT (bar->top, top);
8032 XSETINT (bar->left, left);
8033 XSETINT (bar->width, width);
8034 XSETINT (bar->height, height);
8035 XSETINT (bar->start, 0);
8036 XSETINT (bar->end, 0);
8037 bar->dragging = Qnil;
8038
8039 /* Add bar to its frame's list of scroll bars. */
8040 bar->next = FRAME_SCROLL_BARS (f);
8041 bar->prev = Qnil;
8042 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8043 if (!NILP (bar->next))
8044 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8045
8046 /* Map the window/widget. */
8047 #if USE_TOOLKIT_SCROLL_BARS
8048 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8049 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8050 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8051 top,
8052 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8053 height, 0);
8054 #else /* not USE_TOOLKIT_SCROLL_BARS */
8055 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8056 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8057
8058 UNBLOCK_INPUT;
8059 return bar;
8060 }
8061
8062
8063 /* Draw BAR's handle in the proper position.
8064
8065 If the handle is already drawn from START to END, don't bother
8066 redrawing it, unless REBUILD is non-zero; in that case, always
8067 redraw it. (REBUILD is handy for drawing the handle after expose
8068 events.)
8069
8070 Normally, we want to constrain the start and end of the handle to
8071 fit inside its rectangle, but if the user is dragging the scroll
8072 bar handle, we want to let them drag it down all the way, so that
8073 the bar's top is as far down as it goes; otherwise, there's no way
8074 to move to the very end of the buffer. */
8075
8076 #ifndef USE_TOOLKIT_SCROLL_BARS
8077
8078 static void
8079 x_scroll_bar_set_handle (bar, start, end, rebuild)
8080 struct scroll_bar *bar;
8081 int start, end;
8082 int rebuild;
8083 {
8084 int dragging = ! NILP (bar->dragging);
8085 Window w = SCROLL_BAR_X_WINDOW (bar);
8086 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8087 GC gc = f->output_data.x->normal_gc;
8088
8089 /* If the display is already accurate, do nothing. */
8090 if (! rebuild
8091 && start == XINT (bar->start)
8092 && end == XINT (bar->end))
8093 return;
8094
8095 BLOCK_INPUT;
8096
8097 {
8098 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8099 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8100 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8101
8102 /* Make sure the values are reasonable, and try to preserve
8103 the distance between start and end. */
8104 {
8105 int length = end - start;
8106
8107 if (start < 0)
8108 start = 0;
8109 else if (start > top_range)
8110 start = top_range;
8111 end = start + length;
8112
8113 if (end < start)
8114 end = start;
8115 else if (end > top_range && ! dragging)
8116 end = top_range;
8117 }
8118
8119 /* Store the adjusted setting in the scroll bar. */
8120 XSETINT (bar->start, start);
8121 XSETINT (bar->end, end);
8122
8123 /* Clip the end position, just for display. */
8124 if (end > top_range)
8125 end = top_range;
8126
8127 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8128 below top positions, to make sure the handle is always at least
8129 that many pixels tall. */
8130 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8131
8132 /* Draw the empty space above the handle. Note that we can't clear
8133 zero-height areas; that means "clear to end of window." */
8134 if (0 < start)
8135 XClearArea (FRAME_X_DISPLAY (f), w,
8136
8137 /* x, y, width, height, and exposures. */
8138 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8139 VERTICAL_SCROLL_BAR_TOP_BORDER,
8140 inside_width, start,
8141 False);
8142
8143 /* Change to proper foreground color if one is specified. */
8144 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8145 XSetForeground (FRAME_X_DISPLAY (f), gc,
8146 f->output_data.x->scroll_bar_foreground_pixel);
8147
8148 /* Draw the handle itself. */
8149 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8150
8151 /* x, y, width, height */
8152 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8153 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8154 inside_width, end - start);
8155
8156 /* Restore the foreground color of the GC if we changed it above. */
8157 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8158 XSetForeground (FRAME_X_DISPLAY (f), gc,
8159 f->output_data.x->foreground_pixel);
8160
8161 /* Draw the empty space below the handle. Note that we can't
8162 clear zero-height areas; that means "clear to end of window." */
8163 if (end < inside_height)
8164 XClearArea (FRAME_X_DISPLAY (f), w,
8165
8166 /* x, y, width, height, and exposures. */
8167 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8168 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8169 inside_width, inside_height - end,
8170 False);
8171
8172 }
8173
8174 UNBLOCK_INPUT;
8175 }
8176
8177 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8178
8179 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8180 nil. */
8181
8182 static void
8183 x_scroll_bar_remove (bar)
8184 struct scroll_bar *bar;
8185 {
8186 BLOCK_INPUT;
8187
8188 #if USE_TOOLKIT_SCROLL_BARS
8189 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8190 #else /* not USE_TOOLKIT_SCROLL_BARS */
8191 {
8192 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8193 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8194 }
8195 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8196
8197 /* Disassociate this scroll bar from its window. */
8198 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8199
8200 UNBLOCK_INPUT;
8201 }
8202
8203
8204 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8205 that we are displaying PORTION characters out of a total of WHOLE
8206 characters, starting at POSITION. If WINDOW has no scroll bar,
8207 create one. */
8208
8209 static void
8210 XTset_vertical_scroll_bar (w, portion, whole, position)
8211 struct window *w;
8212 int portion, whole, position;
8213 {
8214 struct frame *f = XFRAME (w->frame);
8215 struct scroll_bar *bar;
8216 int top, height, left, sb_left, width, sb_width;
8217 int window_x, window_y, window_width, window_height;
8218
8219 /* Get window dimensions. */
8220 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8221 top = window_y;
8222 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8223 height = window_height;
8224
8225 /* Compute the left edge of the scroll bar area. */
8226 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8227 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8228 else
8229 left = XFASTINT (w->left);
8230 left *= CANON_X_UNIT (f);
8231 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8232
8233 /* Compute the width of the scroll bar which might be less than
8234 the width of the area reserved for the scroll bar. */
8235 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8236 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8237 else
8238 sb_width = width;
8239
8240 /* Compute the left edge of the scroll bar. */
8241 #ifdef USE_TOOLKIT_SCROLL_BARS
8242 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8243 sb_left = left + width - sb_width - (width - sb_width) / 2;
8244 else
8245 sb_left = left + (width - sb_width) / 2;
8246 #else
8247 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8248 sb_left = left + width - sb_width;
8249 else
8250 sb_left = left;
8251 #endif
8252
8253 /* Does the scroll bar exist yet? */
8254 if (NILP (w->vertical_scroll_bar))
8255 {
8256 BLOCK_INPUT;
8257 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8258 left, top, width, height, False);
8259 UNBLOCK_INPUT;
8260 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8261 }
8262 else
8263 {
8264 /* It may just need to be moved and resized. */
8265 unsigned int mask = 0;
8266
8267 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8268
8269 BLOCK_INPUT;
8270
8271 if (sb_left != XINT (bar->left))
8272 mask |= CWX;
8273 if (top != XINT (bar->top))
8274 mask |= CWY;
8275 if (sb_width != XINT (bar->width))
8276 mask |= CWWidth;
8277 if (height != XINT (bar->height))
8278 mask |= CWHeight;
8279
8280 #ifdef USE_TOOLKIT_SCROLL_BARS
8281
8282 /* Since toolkit scroll bars are smaller than the space reserved
8283 for them on the frame, we have to clear "under" them. */
8284 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8285 left, top, width, height, False);
8286
8287 /* Move/size the scroll bar widget. */
8288 if (mask)
8289 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8290 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8291 top,
8292 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8293 height, 0);
8294
8295 #else /* not USE_TOOLKIT_SCROLL_BARS */
8296
8297 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8298 {
8299 /* Clear areas not covered by the scroll bar. This makes sure a
8300 previous mode line display is cleared after C-x 2 C-x 1, for
8301 example. Non-toolkit scroll bars are as wide as the area
8302 reserved for scroll bars - trim at both sides. */
8303 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8304 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8305 height, False);
8306 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8307 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8308 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8309 height, False);
8310 }
8311
8312 /* Move/size the scroll bar window. */
8313 if (mask)
8314 {
8315 XWindowChanges wc;
8316
8317 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8318 wc.y = top;
8319 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8320 wc.height = height;
8321 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8322 mask, &wc);
8323 }
8324
8325 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8326
8327 /* Remember new settings. */
8328 XSETINT (bar->left, sb_left);
8329 XSETINT (bar->top, top);
8330 XSETINT (bar->width, sb_width);
8331 XSETINT (bar->height, height);
8332
8333 UNBLOCK_INPUT;
8334 }
8335
8336 #if USE_TOOLKIT_SCROLL_BARS
8337 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8338 #else /* not USE_TOOLKIT_SCROLL_BARS */
8339 /* Set the scroll bar's current state, unless we're currently being
8340 dragged. */
8341 if (NILP (bar->dragging))
8342 {
8343 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8344
8345 if (whole == 0)
8346 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8347 else
8348 {
8349 int start = ((double) position * top_range) / whole;
8350 int end = ((double) (position + portion) * top_range) / whole;
8351 x_scroll_bar_set_handle (bar, start, end, 0);
8352 }
8353 }
8354 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8355
8356 XSETVECTOR (w->vertical_scroll_bar, bar);
8357 }
8358
8359
8360 /* The following three hooks are used when we're doing a thorough
8361 redisplay of the frame. We don't explicitly know which scroll bars
8362 are going to be deleted, because keeping track of when windows go
8363 away is a real pain - "Can you say set-window-configuration, boys
8364 and girls?" Instead, we just assert at the beginning of redisplay
8365 that *all* scroll bars are to be removed, and then save a scroll bar
8366 from the fiery pit when we actually redisplay its window. */
8367
8368 /* Arrange for all scroll bars on FRAME to be removed at the next call
8369 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8370 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8371
8372 static void
8373 XTcondemn_scroll_bars (frame)
8374 FRAME_PTR frame;
8375 {
8376 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8377 while (! NILP (FRAME_SCROLL_BARS (frame)))
8378 {
8379 Lisp_Object bar;
8380 bar = FRAME_SCROLL_BARS (frame);
8381 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8382 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8383 XSCROLL_BAR (bar)->prev = Qnil;
8384 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8385 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8386 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8387 }
8388 }
8389
8390 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8391 Note that WINDOW isn't necessarily condemned at all. */
8392 static void
8393 XTredeem_scroll_bar (window)
8394 struct window *window;
8395 {
8396 struct scroll_bar *bar;
8397
8398 /* We can't redeem this window's scroll bar if it doesn't have one. */
8399 if (NILP (window->vertical_scroll_bar))
8400 abort ();
8401
8402 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8403
8404 /* Unlink it from the condemned list. */
8405 {
8406 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8407
8408 if (NILP (bar->prev))
8409 {
8410 /* If the prev pointer is nil, it must be the first in one of
8411 the lists. */
8412 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8413 /* It's not condemned. Everything's fine. */
8414 return;
8415 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8416 window->vertical_scroll_bar))
8417 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8418 else
8419 /* If its prev pointer is nil, it must be at the front of
8420 one or the other! */
8421 abort ();
8422 }
8423 else
8424 XSCROLL_BAR (bar->prev)->next = bar->next;
8425
8426 if (! NILP (bar->next))
8427 XSCROLL_BAR (bar->next)->prev = bar->prev;
8428
8429 bar->next = FRAME_SCROLL_BARS (f);
8430 bar->prev = Qnil;
8431 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8432 if (! NILP (bar->next))
8433 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8434 }
8435 }
8436
8437 /* Remove all scroll bars on FRAME that haven't been saved since the
8438 last call to `*condemn_scroll_bars_hook'. */
8439
8440 static void
8441 XTjudge_scroll_bars (f)
8442 FRAME_PTR f;
8443 {
8444 Lisp_Object bar, next;
8445
8446 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8447
8448 /* Clear out the condemned list now so we won't try to process any
8449 more events on the hapless scroll bars. */
8450 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8451
8452 for (; ! NILP (bar); bar = next)
8453 {
8454 struct scroll_bar *b = XSCROLL_BAR (bar);
8455
8456 x_scroll_bar_remove (b);
8457
8458 next = b->next;
8459 b->next = b->prev = Qnil;
8460 }
8461
8462 /* Now there should be no references to the condemned scroll bars,
8463 and they should get garbage-collected. */
8464 }
8465
8466
8467 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8468 is a no-op when using toolkit scroll bars.
8469
8470 This may be called from a signal handler, so we have to ignore GC
8471 mark bits. */
8472
8473 static void
8474 x_scroll_bar_expose (bar, event)
8475 struct scroll_bar *bar;
8476 XEvent *event;
8477 {
8478 #ifndef USE_TOOLKIT_SCROLL_BARS
8479
8480 Window w = SCROLL_BAR_X_WINDOW (bar);
8481 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8482 GC gc = f->output_data.x->normal_gc;
8483 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8484
8485 BLOCK_INPUT;
8486
8487 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8488
8489 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8490 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8491
8492 /* x, y, width, height */
8493 0, 0,
8494 XINT (bar->width) - 1 - width_trim - width_trim,
8495 XINT (bar->height) - 1);
8496
8497 UNBLOCK_INPUT;
8498
8499 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8500 }
8501
8502 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8503 is set to something other than no_event, it is enqueued.
8504
8505 This may be called from a signal handler, so we have to ignore GC
8506 mark bits. */
8507
8508 #ifndef USE_TOOLKIT_SCROLL_BARS
8509
8510 static void
8511 x_scroll_bar_handle_click (bar, event, emacs_event)
8512 struct scroll_bar *bar;
8513 XEvent *event;
8514 struct input_event *emacs_event;
8515 {
8516 if (! GC_WINDOWP (bar->window))
8517 abort ();
8518
8519 emacs_event->kind = scroll_bar_click;
8520 emacs_event->code = event->xbutton.button - Button1;
8521 emacs_event->modifiers
8522 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8523 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8524 event->xbutton.state)
8525 | (event->type == ButtonRelease
8526 ? up_modifier
8527 : down_modifier));
8528 emacs_event->frame_or_window = bar->window;
8529 emacs_event->timestamp = event->xbutton.time;
8530 {
8531 #if 0
8532 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8533 int internal_height
8534 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8535 #endif
8536 int top_range
8537 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8538 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8539
8540 if (y < 0) y = 0;
8541 if (y > top_range) y = top_range;
8542
8543 if (y < XINT (bar->start))
8544 emacs_event->part = scroll_bar_above_handle;
8545 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8546 emacs_event->part = scroll_bar_handle;
8547 else
8548 emacs_event->part = scroll_bar_below_handle;
8549
8550 /* Just because the user has clicked on the handle doesn't mean
8551 they want to drag it. Lisp code needs to be able to decide
8552 whether or not we're dragging. */
8553 #if 0
8554 /* If the user has just clicked on the handle, record where they're
8555 holding it. */
8556 if (event->type == ButtonPress
8557 && emacs_event->part == scroll_bar_handle)
8558 XSETINT (bar->dragging, y - XINT (bar->start));
8559 #endif
8560
8561 /* If the user has released the handle, set it to its final position. */
8562 if (event->type == ButtonRelease
8563 && ! NILP (bar->dragging))
8564 {
8565 int new_start = y - XINT (bar->dragging);
8566 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8567
8568 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8569 bar->dragging = Qnil;
8570 }
8571
8572 /* Same deal here as the other #if 0. */
8573 #if 0
8574 /* Clicks on the handle are always reported as occurring at the top of
8575 the handle. */
8576 if (emacs_event->part == scroll_bar_handle)
8577 emacs_event->x = bar->start;
8578 else
8579 XSETINT (emacs_event->x, y);
8580 #else
8581 XSETINT (emacs_event->x, y);
8582 #endif
8583
8584 XSETINT (emacs_event->y, top_range);
8585 }
8586 }
8587
8588 /* Handle some mouse motion while someone is dragging the scroll bar.
8589
8590 This may be called from a signal handler, so we have to ignore GC
8591 mark bits. */
8592
8593 static void
8594 x_scroll_bar_note_movement (bar, event)
8595 struct scroll_bar *bar;
8596 XEvent *event;
8597 {
8598 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8599
8600 last_mouse_movement_time = event->xmotion.time;
8601
8602 f->mouse_moved = 1;
8603 XSETVECTOR (last_mouse_scroll_bar, bar);
8604
8605 /* If we're dragging the bar, display it. */
8606 if (! GC_NILP (bar->dragging))
8607 {
8608 /* Where should the handle be now? */
8609 int new_start = event->xmotion.y - XINT (bar->dragging);
8610
8611 if (new_start != XINT (bar->start))
8612 {
8613 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8614
8615 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8616 }
8617 }
8618 }
8619
8620 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8621
8622 /* Return information to the user about the current position of the mouse
8623 on the scroll bar. */
8624
8625 static void
8626 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8627 FRAME_PTR *fp;
8628 Lisp_Object *bar_window;
8629 enum scroll_bar_part *part;
8630 Lisp_Object *x, *y;
8631 unsigned long *time;
8632 {
8633 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8634 Window w = SCROLL_BAR_X_WINDOW (bar);
8635 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8636 int win_x, win_y;
8637 Window dummy_window;
8638 int dummy_coord;
8639 unsigned int dummy_mask;
8640
8641 BLOCK_INPUT;
8642
8643 /* Get the mouse's position relative to the scroll bar window, and
8644 report that. */
8645 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8646
8647 /* Root, child, root x and root y. */
8648 &dummy_window, &dummy_window,
8649 &dummy_coord, &dummy_coord,
8650
8651 /* Position relative to scroll bar. */
8652 &win_x, &win_y,
8653
8654 /* Mouse buttons and modifier keys. */
8655 &dummy_mask))
8656 ;
8657 else
8658 {
8659 #if 0
8660 int inside_height
8661 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8662 #endif
8663 int top_range
8664 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8665
8666 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8667
8668 if (! NILP (bar->dragging))
8669 win_y -= XINT (bar->dragging);
8670
8671 if (win_y < 0)
8672 win_y = 0;
8673 if (win_y > top_range)
8674 win_y = top_range;
8675
8676 *fp = f;
8677 *bar_window = bar->window;
8678
8679 if (! NILP (bar->dragging))
8680 *part = scroll_bar_handle;
8681 else if (win_y < XINT (bar->start))
8682 *part = scroll_bar_above_handle;
8683 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8684 *part = scroll_bar_handle;
8685 else
8686 *part = scroll_bar_below_handle;
8687
8688 XSETINT (*x, win_y);
8689 XSETINT (*y, top_range);
8690
8691 f->mouse_moved = 0;
8692 last_mouse_scroll_bar = Qnil;
8693 }
8694
8695 *time = last_mouse_movement_time;
8696
8697 UNBLOCK_INPUT;
8698 }
8699
8700
8701 /* The screen has been cleared so we may have changed foreground or
8702 background colors, and the scroll bars may need to be redrawn.
8703 Clear out the scroll bars, and ask for expose events, so we can
8704 redraw them. */
8705
8706 void
8707 x_scroll_bar_clear (f)
8708 FRAME_PTR f;
8709 {
8710 #ifndef USE_TOOLKIT_SCROLL_BARS
8711 Lisp_Object bar;
8712
8713 /* We can have scroll bars even if this is 0,
8714 if we just turned off scroll bar mode.
8715 But in that case we should not clear them. */
8716 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8717 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8718 bar = XSCROLL_BAR (bar)->next)
8719 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8720 0, 0, 0, 0, True);
8721 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8722 }
8723
8724 /* This processes Expose events from the menu-bar specific X event
8725 loop in xmenu.c. This allows to redisplay the frame if necessary
8726 when handling menu-bar or pop-up items. */
8727
8728 int
8729 process_expose_from_menu (event)
8730 XEvent event;
8731 {
8732 FRAME_PTR f;
8733 struct x_display_info *dpyinfo;
8734 int frame_exposed_p = 0;
8735
8736 BLOCK_INPUT;
8737
8738 dpyinfo = x_display_info_for_display (event.xexpose.display);
8739 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8740 if (f)
8741 {
8742 if (f->async_visible == 0)
8743 {
8744 f->async_visible = 1;
8745 f->async_iconified = 0;
8746 f->output_data.x->has_been_visible = 1;
8747 SET_FRAME_GARBAGED (f);
8748 }
8749 else
8750 {
8751 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8752 event.xexpose.x, event.xexpose.y,
8753 event.xexpose.width, event.xexpose.height);
8754 frame_exposed_p = 1;
8755 }
8756 }
8757 else
8758 {
8759 struct scroll_bar *bar
8760 = x_window_to_scroll_bar (event.xexpose.window);
8761
8762 if (bar)
8763 x_scroll_bar_expose (bar, &event);
8764 }
8765
8766 UNBLOCK_INPUT;
8767 return frame_exposed_p;
8768 }
8769 \f
8770 /* Define a queue to save up SelectionRequest events for later handling. */
8771
8772 struct selection_event_queue
8773 {
8774 XEvent event;
8775 struct selection_event_queue *next;
8776 };
8777
8778 static struct selection_event_queue *queue;
8779
8780 /* Nonzero means queue up certain events--don't process them yet. */
8781
8782 static int x_queue_selection_requests;
8783
8784 /* Queue up an X event *EVENT, to be processed later. */
8785
8786 static void
8787 x_queue_event (f, event)
8788 FRAME_PTR f;
8789 XEvent *event;
8790 {
8791 struct selection_event_queue *queue_tmp
8792 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8793
8794 if (queue_tmp != NULL)
8795 {
8796 queue_tmp->event = *event;
8797 queue_tmp->next = queue;
8798 queue = queue_tmp;
8799 }
8800 }
8801
8802 /* Take all the queued events and put them back
8803 so that they get processed afresh. */
8804
8805 static void
8806 x_unqueue_events (display)
8807 Display *display;
8808 {
8809 while (queue != NULL)
8810 {
8811 struct selection_event_queue *queue_tmp = queue;
8812 XPutBackEvent (display, &queue_tmp->event);
8813 queue = queue_tmp->next;
8814 xfree ((char *)queue_tmp);
8815 }
8816 }
8817
8818 /* Start queuing SelectionRequest events. */
8819
8820 void
8821 x_start_queuing_selection_requests (display)
8822 Display *display;
8823 {
8824 x_queue_selection_requests++;
8825 }
8826
8827 /* Stop queuing SelectionRequest events. */
8828
8829 void
8830 x_stop_queuing_selection_requests (display)
8831 Display *display;
8832 {
8833 x_queue_selection_requests--;
8834 x_unqueue_events (display);
8835 }
8836 \f
8837 /* The main X event-reading loop - XTread_socket. */
8838
8839 /* Time stamp of enter window event. This is only used by XTread_socket,
8840 but we have to put it out here, since static variables within functions
8841 sometimes don't work. */
8842
8843 static Time enter_timestamp;
8844
8845 /* This holds the state XLookupString needs to implement dead keys
8846 and other tricks known as "compose processing". _X Window System_
8847 says that a portable program can't use this, but Stephen Gildea assures
8848 me that letting the compiler initialize it to zeros will work okay.
8849
8850 This must be defined outside of XTread_socket, for the same reasons
8851 given for enter_time stamp, above. */
8852
8853 static XComposeStatus compose_status;
8854
8855 /* Record the last 100 characters stored
8856 to help debug the loss-of-chars-during-GC problem. */
8857
8858 static int temp_index;
8859 static short temp_buffer[100];
8860
8861 /* Set this to nonzero to fake an "X I/O error"
8862 on a particular display. */
8863
8864 struct x_display_info *XTread_socket_fake_io_error;
8865
8866 /* When we find no input here, we occasionally do a no-op command
8867 to verify that the X server is still running and we can still talk with it.
8868 We try all the open displays, one by one.
8869 This variable is used for cycling thru the displays. */
8870
8871 static struct x_display_info *next_noop_dpyinfo;
8872
8873 #define SET_SAVED_MENU_EVENT(size) \
8874 do \
8875 { \
8876 if (f->output_data.x->saved_menu_event == 0) \
8877 f->output_data.x->saved_menu_event \
8878 = (XEvent *) xmalloc (sizeof (XEvent)); \
8879 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8880 if (numchars >= 1) \
8881 { \
8882 bufp->kind = menu_bar_activate_event; \
8883 XSETFRAME (bufp->frame_or_window, f); \
8884 bufp++; \
8885 count++; \
8886 numchars--; \
8887 } \
8888 } \
8889 while (0)
8890
8891 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
8892 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8893
8894 /* Read events coming from the X server.
8895 This routine is called by the SIGIO handler.
8896 We return as soon as there are no more events to be read.
8897
8898 Events representing keys are stored in buffer BUFP,
8899 which can hold up to NUMCHARS characters.
8900 We return the number of characters stored into the buffer,
8901 thus pretending to be `read'.
8902
8903 EXPECTED is nonzero if the caller knows input is available. */
8904
8905 int
8906 XTread_socket (sd, bufp, numchars, expected)
8907 register int sd;
8908 /* register */ struct input_event *bufp;
8909 /* register */ int numchars;
8910 int expected;
8911 {
8912 int count = 0;
8913 int nbytes = 0;
8914 XEvent event;
8915 struct frame *f;
8916 int event_found = 0;
8917 struct x_display_info *dpyinfo;
8918
8919 if (interrupt_input_blocked)
8920 {
8921 interrupt_input_pending = 1;
8922 return -1;
8923 }
8924
8925 interrupt_input_pending = 0;
8926 BLOCK_INPUT;
8927
8928 /* So people can tell when we have read the available input. */
8929 input_signal_count++;
8930
8931 if (numchars <= 0)
8932 abort (); /* Don't think this happens. */
8933
8934 /* Find the display we are supposed to read input for.
8935 It's the one communicating on descriptor SD. */
8936 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8937 {
8938 #if 0 /* This ought to be unnecessary; let's verify it. */
8939 #ifdef FIOSNBIO
8940 /* If available, Xlib uses FIOSNBIO to make the socket
8941 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
8942 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
8943 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
8944 fcntl (dpyinfo->connection, F_SETFL, 0);
8945 #endif /* ! defined (FIOSNBIO) */
8946 #endif
8947
8948 #if 0 /* This code can't be made to work, with multiple displays,
8949 and appears not to be used on any system any more.
8950 Also keyboard.c doesn't turn O_NDELAY on and off
8951 for X connections. */
8952 #ifndef SIGIO
8953 #ifndef HAVE_SELECT
8954 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8955 {
8956 extern int read_alarm_should_throw;
8957 read_alarm_should_throw = 1;
8958 XPeekEvent (dpyinfo->display, &event);
8959 read_alarm_should_throw = 0;
8960 }
8961 #endif /* HAVE_SELECT */
8962 #endif /* SIGIO */
8963 #endif
8964
8965 /* For debugging, this gives a way to fake an I/O error. */
8966 if (dpyinfo == XTread_socket_fake_io_error)
8967 {
8968 XTread_socket_fake_io_error = 0;
8969 x_io_error_quitter (dpyinfo->display);
8970 }
8971
8972 while (XPending (dpyinfo->display))
8973 {
8974 XNextEvent (dpyinfo->display, &event);
8975
8976 #ifdef HAVE_X_I18N
8977 {
8978 /* Filter events for the current X input method.
8979 XFilterEvent returns non-zero if the input method has
8980 consumed the event. We pass the frame's X window to
8981 XFilterEvent because that's the one for which the IC
8982 was created. */
8983 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8984 event.xclient.window);
8985 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
8986 break;
8987 }
8988 #endif
8989 event_found = 1;
8990
8991 switch (event.type)
8992 {
8993 case ClientMessage:
8994 {
8995 if (event.xclient.message_type
8996 == dpyinfo->Xatom_wm_protocols
8997 && event.xclient.format == 32)
8998 {
8999 if (event.xclient.data.l[0]
9000 == dpyinfo->Xatom_wm_take_focus)
9001 {
9002 /* Use x_any_window_to_frame because this
9003 could be the shell widget window
9004 if the frame has no title bar. */
9005 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9006 #ifdef HAVE_X_I18N
9007 /* Not quite sure this is needed -pd */
9008 if (f && FRAME_XIC (f))
9009 XSetICFocus (FRAME_XIC (f));
9010 #endif
9011 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9012 instructs the WM to set the input focus automatically for
9013 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9014 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9015 it has set the focus. So, XSetInputFocus below is not
9016 needed.
9017
9018 The call to XSetInputFocus below has also caused trouble. In
9019 cases where the XSetInputFocus done by the WM and the one
9020 below are temporally close (on a fast machine), the call
9021 below can generate additional FocusIn events which confuse
9022 Emacs. */
9023
9024 /* Since we set WM_TAKE_FOCUS, we must call
9025 XSetInputFocus explicitly. But not if f is null,
9026 since that might be an event for a deleted frame. */
9027 if (f)
9028 {
9029 Display *d = event.xclient.display;
9030 /* Catch and ignore errors, in case window has been
9031 iconified by a window manager such as GWM. */
9032 int count = x_catch_errors (d);
9033 XSetInputFocus (d, event.xclient.window,
9034 /* The ICCCM says this is
9035 the only valid choice. */
9036 RevertToParent,
9037 event.xclient.data.l[1]);
9038 /* This is needed to detect the error
9039 if there is an error. */
9040 XSync (d, False);
9041 x_uncatch_errors (d, count);
9042 }
9043 /* Not certain about handling scroll bars here */
9044 #endif /* 0 */
9045 }
9046 else if (event.xclient.data.l[0]
9047 == dpyinfo->Xatom_wm_save_yourself)
9048 {
9049 /* Save state modify the WM_COMMAND property to
9050 something which can reinstate us. This notifies
9051 the session manager, who's looking for such a
9052 PropertyNotify. Can restart processing when
9053 a keyboard or mouse event arrives. */
9054 if (numchars > 0)
9055 {
9056 f = x_top_window_to_frame (dpyinfo,
9057 event.xclient.window);
9058
9059 /* This is just so we only give real data once
9060 for a single Emacs process. */
9061 if (f == SELECTED_FRAME ())
9062 XSetCommand (FRAME_X_DISPLAY (f),
9063 event.xclient.window,
9064 initial_argv, initial_argc);
9065 else if (f)
9066 XSetCommand (FRAME_X_DISPLAY (f),
9067 event.xclient.window,
9068 0, 0);
9069 }
9070 }
9071 else if (event.xclient.data.l[0]
9072 == dpyinfo->Xatom_wm_delete_window)
9073 {
9074 struct frame *f
9075 = x_any_window_to_frame (dpyinfo,
9076 event.xclient.window);
9077
9078 if (f)
9079 {
9080 if (numchars == 0)
9081 abort ();
9082
9083 bufp->kind = delete_window_event;
9084 XSETFRAME (bufp->frame_or_window, f);
9085 bufp++;
9086
9087 count += 1;
9088 numchars -= 1;
9089 }
9090 }
9091 }
9092 else if (event.xclient.message_type
9093 == dpyinfo->Xatom_wm_configure_denied)
9094 {
9095 }
9096 else if (event.xclient.message_type
9097 == dpyinfo->Xatom_wm_window_moved)
9098 {
9099 int new_x, new_y;
9100 struct frame *f
9101 = x_window_to_frame (dpyinfo, event.xclient.window);
9102
9103 new_x = event.xclient.data.s[0];
9104 new_y = event.xclient.data.s[1];
9105
9106 if (f)
9107 {
9108 f->output_data.x->left_pos = new_x;
9109 f->output_data.x->top_pos = new_y;
9110 }
9111 }
9112 #ifdef HACK_EDITRES
9113 else if (event.xclient.message_type
9114 == dpyinfo->Xatom_editres)
9115 {
9116 struct frame *f
9117 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9118 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9119 &event, NULL);
9120 }
9121 #endif /* HACK_EDITRES */
9122 else if ((event.xclient.message_type
9123 == dpyinfo->Xatom_DONE)
9124 || (event.xclient.message_type
9125 == dpyinfo->Xatom_PAGE))
9126 {
9127 /* Ghostview job completed. Kill it. We could
9128 reply with "Next" if we received "Page", but we
9129 currently never do because we are interested in
9130 images, only, which should have 1 page. */
9131 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9132 struct frame *f
9133 = x_window_to_frame (dpyinfo, event.xclient.window);
9134 x_kill_gs_process (pixmap, f);
9135 expose_frame (f, 0, 0, 0, 0);
9136 }
9137 #ifdef USE_TOOLKIT_SCROLL_BARS
9138 /* Scroll bar callbacks send a ClientMessage from which
9139 we construct an input_event. */
9140 else if (event.xclient.message_type
9141 == dpyinfo->Xatom_Scrollbar)
9142 {
9143 x_scroll_bar_to_input_event (&event, bufp);
9144 ++bufp, ++count, --numchars;
9145 goto out;
9146 }
9147 #endif /* USE_TOOLKIT_SCROLL_BARS */
9148 else
9149 goto OTHER;
9150 }
9151 break;
9152
9153 case SelectionNotify:
9154 #ifdef USE_X_TOOLKIT
9155 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9156 goto OTHER;
9157 #endif /* not USE_X_TOOLKIT */
9158 x_handle_selection_notify (&event.xselection);
9159 break;
9160
9161 case SelectionClear: /* Someone has grabbed ownership. */
9162 #ifdef USE_X_TOOLKIT
9163 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9164 goto OTHER;
9165 #endif /* USE_X_TOOLKIT */
9166 {
9167 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9168
9169 if (numchars == 0)
9170 abort ();
9171
9172 bufp->kind = selection_clear_event;
9173 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9174 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9175 SELECTION_EVENT_TIME (bufp) = eventp->time;
9176 bufp->frame_or_window = Qnil;
9177 bufp++;
9178
9179 count += 1;
9180 numchars -= 1;
9181 }
9182 break;
9183
9184 case SelectionRequest: /* Someone wants our selection. */
9185 #ifdef USE_X_TOOLKIT
9186 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9187 goto OTHER;
9188 #endif /* USE_X_TOOLKIT */
9189 if (x_queue_selection_requests)
9190 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9191 &event);
9192 else
9193 {
9194 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9195
9196 if (numchars == 0)
9197 abort ();
9198
9199 bufp->kind = selection_request_event;
9200 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9201 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9202 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9203 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9204 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9205 SELECTION_EVENT_TIME (bufp) = eventp->time;
9206 bufp->frame_or_window = Qnil;
9207 bufp++;
9208
9209 count += 1;
9210 numchars -= 1;
9211 }
9212 break;
9213
9214 case PropertyNotify:
9215 #ifdef USE_X_TOOLKIT
9216 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9217 goto OTHER;
9218 #endif /* not USE_X_TOOLKIT */
9219 x_handle_property_notify (&event.xproperty);
9220 break;
9221
9222 case ReparentNotify:
9223 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9224 if (f)
9225 {
9226 int x, y;
9227 f->output_data.x->parent_desc = event.xreparent.parent;
9228 x_real_positions (f, &x, &y);
9229 f->output_data.x->left_pos = x;
9230 f->output_data.x->top_pos = y;
9231 }
9232 break;
9233
9234 case Expose:
9235 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9236 if (f)
9237 {
9238 if (f->async_visible == 0)
9239 {
9240 f->async_visible = 1;
9241 f->async_iconified = 0;
9242 f->output_data.x->has_been_visible = 1;
9243 SET_FRAME_GARBAGED (f);
9244 }
9245 else
9246 expose_frame (x_window_to_frame (dpyinfo,
9247 event.xexpose.window),
9248 event.xexpose.x, event.xexpose.y,
9249 event.xexpose.width, event.xexpose.height);
9250 }
9251 else
9252 {
9253 #ifdef USE_TOOLKIT_SCROLL_BARS
9254 /* Dispatch event to the widget. */
9255 goto OTHER;
9256 #else /* not USE_TOOLKIT_SCROLL_BARS */
9257 struct scroll_bar *bar
9258 = x_window_to_scroll_bar (event.xexpose.window);
9259
9260 if (bar)
9261 x_scroll_bar_expose (bar, &event);
9262 #ifdef USE_X_TOOLKIT
9263 else
9264 goto OTHER;
9265 #endif /* USE_X_TOOLKIT */
9266 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9267 }
9268 break;
9269
9270 case GraphicsExpose: /* This occurs when an XCopyArea's
9271 source area was obscured or not
9272 available.*/
9273 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9274 if (f)
9275 {
9276 expose_frame (f,
9277 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9278 event.xgraphicsexpose.width,
9279 event.xgraphicsexpose.height);
9280 }
9281 #ifdef USE_X_TOOLKIT
9282 else
9283 goto OTHER;
9284 #endif /* USE_X_TOOLKIT */
9285 break;
9286
9287 case NoExpose: /* This occurs when an XCopyArea's
9288 source area was completely
9289 available */
9290 break;
9291
9292 case UnmapNotify:
9293 /* Redo the mouse-highlight after the tooltip has gone. */
9294 if (event.xmap.window == tip_window)
9295 {
9296 tip_window = 0;
9297 redo_mouse_highlight ();
9298 }
9299
9300 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9301 if (f) /* F may no longer exist if
9302 the frame was deleted. */
9303 {
9304 /* While a frame is unmapped, display generation is
9305 disabled; you don't want to spend time updating a
9306 display that won't ever be seen. */
9307 f->async_visible = 0;
9308 /* We can't distinguish, from the event, whether the window
9309 has become iconified or invisible. So assume, if it
9310 was previously visible, than now it is iconified.
9311 But x_make_frame_invisible clears both
9312 the visible flag and the iconified flag;
9313 and that way, we know the window is not iconified now. */
9314 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9315 {
9316 f->async_iconified = 1;
9317
9318 bufp->kind = iconify_event;
9319 XSETFRAME (bufp->frame_or_window, f);
9320 bufp++;
9321 count++;
9322 numchars--;
9323 }
9324 }
9325 goto OTHER;
9326
9327 case MapNotify:
9328 if (event.xmap.window == tip_window)
9329 /* The tooltip has been drawn already. Avoid
9330 the SET_FRAME_GARBAGED below. */
9331 goto OTHER;
9332
9333 /* We use x_top_window_to_frame because map events can
9334 come for sub-windows and they don't mean that the
9335 frame is visible. */
9336 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9337 if (f)
9338 {
9339 f->async_visible = 1;
9340 f->async_iconified = 0;
9341 f->output_data.x->has_been_visible = 1;
9342
9343 /* wait_reading_process_input will notice this and update
9344 the frame's display structures. */
9345 SET_FRAME_GARBAGED (f);
9346
9347 if (f->iconified)
9348 {
9349 bufp->kind = deiconify_event;
9350 XSETFRAME (bufp->frame_or_window, f);
9351 bufp++;
9352 count++;
9353 numchars--;
9354 }
9355 else if (! NILP (Vframe_list)
9356 && ! NILP (XCDR (Vframe_list)))
9357 /* Force a redisplay sooner or later
9358 to update the frame titles
9359 in case this is the second frame. */
9360 record_asynch_buffer_change ();
9361 }
9362 goto OTHER;
9363
9364 case KeyPress:
9365 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9366
9367 #ifdef USE_MOTIF
9368 /* I couldn't find a way to prevent LessTif scroll bars
9369 from consuming key events. */
9370 if (f == 0)
9371 {
9372 Widget widget = XtWindowToWidget (dpyinfo->display,
9373 event.xkey.window);
9374 if (widget && XmIsScrollBar (widget))
9375 {
9376 widget = XtParent (widget);
9377 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9378 }
9379 }
9380 #endif /* USE_MOTIF */
9381
9382 if (f != 0)
9383 {
9384 KeySym keysym, orig_keysym;
9385 /* al%imercury@uunet.uu.net says that making this 81 instead of
9386 80 fixed a bug whereby meta chars made his Emacs hang. */
9387 unsigned char copy_buffer[81];
9388 int modifiers;
9389
9390 event.xkey.state
9391 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9392 extra_keyboard_modifiers);
9393 modifiers = event.xkey.state;
9394
9395 /* This will have to go some day... */
9396
9397 /* make_lispy_event turns chars into control chars.
9398 Don't do it here because XLookupString is too eager. */
9399 event.xkey.state &= ~ControlMask;
9400 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9401 | dpyinfo->super_mod_mask
9402 | dpyinfo->hyper_mod_mask
9403 | dpyinfo->alt_mod_mask);
9404
9405 /* In case Meta is ComposeCharacter,
9406 clear its status. According to Markus Ehrnsperger
9407 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9408 this enables ComposeCharacter to work whether or
9409 not it is combined with Meta. */
9410 if (modifiers & dpyinfo->meta_mod_mask)
9411 bzero (&compose_status, sizeof (compose_status));
9412
9413 #ifdef HAVE_X_I18N
9414 if (FRAME_XIC (f))
9415 {
9416 unsigned char *copy_bufptr = copy_buffer;
9417 int copy_bufsiz = sizeof (copy_buffer);
9418 Status status_return;
9419
9420 nbytes = XmbLookupString (FRAME_XIC (f),
9421 &event.xkey, copy_bufptr,
9422 copy_bufsiz, &keysym,
9423 &status_return);
9424 if (status_return == XBufferOverflow)
9425 {
9426 copy_bufsiz = nbytes + 1;
9427 copy_bufptr = (char *) alloca (copy_bufsiz);
9428 nbytes = XmbLookupString (FRAME_XIC (f),
9429 &event.xkey, copy_bufptr,
9430 copy_bufsiz, &keysym,
9431 &status_return);
9432 }
9433
9434 if (status_return == XLookupNone)
9435 break;
9436 else if (status_return == XLookupChars)
9437 keysym = NoSymbol;
9438 else if (status_return != XLookupKeySym
9439 && status_return != XLookupBoth)
9440 abort ();
9441 }
9442 else
9443 nbytes = XLookupString (&event.xkey, copy_buffer,
9444 80, &keysym, &compose_status);
9445 #else
9446 nbytes = XLookupString (&event.xkey, copy_buffer,
9447 80, &keysym, &compose_status);
9448 #endif
9449
9450 orig_keysym = keysym;
9451
9452 if (numchars > 1)
9453 {
9454 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9455 || keysym == XK_Delete
9456 #ifdef XK_ISO_Left_Tab
9457 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9458 #endif
9459 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9460 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9461 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9462 #ifdef HPUX
9463 /* This recognizes the "extended function keys".
9464 It seems there's no cleaner way.
9465 Test IsModifierKey to avoid handling mode_switch
9466 incorrectly. */
9467 || ((unsigned) (keysym) >= XK_Select
9468 && (unsigned)(keysym) < XK_KP_Space)
9469 #endif
9470 #ifdef XK_dead_circumflex
9471 || orig_keysym == XK_dead_circumflex
9472 #endif
9473 #ifdef XK_dead_grave
9474 || orig_keysym == XK_dead_grave
9475 #endif
9476 #ifdef XK_dead_tilde
9477 || orig_keysym == XK_dead_tilde
9478 #endif
9479 #ifdef XK_dead_diaeresis
9480 || orig_keysym == XK_dead_diaeresis
9481 #endif
9482 #ifdef XK_dead_macron
9483 || orig_keysym == XK_dead_macron
9484 #endif
9485 #ifdef XK_dead_degree
9486 || orig_keysym == XK_dead_degree
9487 #endif
9488 #ifdef XK_dead_acute
9489 || orig_keysym == XK_dead_acute
9490 #endif
9491 #ifdef XK_dead_cedilla
9492 || orig_keysym == XK_dead_cedilla
9493 #endif
9494 #ifdef XK_dead_breve
9495 || orig_keysym == XK_dead_breve
9496 #endif
9497 #ifdef XK_dead_ogonek
9498 || orig_keysym == XK_dead_ogonek
9499 #endif
9500 #ifdef XK_dead_caron
9501 || orig_keysym == XK_dead_caron
9502 #endif
9503 #ifdef XK_dead_doubleacute
9504 || orig_keysym == XK_dead_doubleacute
9505 #endif
9506 #ifdef XK_dead_abovedot
9507 || orig_keysym == XK_dead_abovedot
9508 #endif
9509 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9510 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9511 /* Any "vendor-specific" key is ok. */
9512 || (orig_keysym & (1 << 28)))
9513 && ! (IsModifierKey (orig_keysym)
9514 #ifndef HAVE_X11R5
9515 #ifdef XK_Mode_switch
9516 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9517 #endif
9518 #ifdef XK_Num_Lock
9519 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9520 #endif
9521 #endif /* not HAVE_X11R5 */
9522 ))
9523 {
9524 if (temp_index == sizeof temp_buffer / sizeof (short))
9525 temp_index = 0;
9526 temp_buffer[temp_index++] = keysym;
9527 bufp->kind = non_ascii_keystroke;
9528 bufp->code = keysym;
9529 XSETFRAME (bufp->frame_or_window, f);
9530 bufp->modifiers
9531 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9532 modifiers);
9533 bufp->timestamp = event.xkey.time;
9534 bufp++;
9535 count++;
9536 numchars--;
9537 }
9538 else if (numchars > nbytes)
9539 {
9540 register int i;
9541
9542 for (i = 0; i < nbytes; i++)
9543 {
9544 if (temp_index == sizeof temp_buffer / sizeof (short))
9545 temp_index = 0;
9546 temp_buffer[temp_index++] = copy_buffer[i];
9547 bufp->kind = ascii_keystroke;
9548 bufp->code = copy_buffer[i];
9549 XSETFRAME (bufp->frame_or_window, f);
9550 bufp->modifiers
9551 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9552 modifiers);
9553 bufp->timestamp = event.xkey.time;
9554 bufp++;
9555 }
9556
9557 count += nbytes;
9558 numchars -= nbytes;
9559
9560 if (keysym == NoSymbol)
9561 break;
9562 }
9563 else
9564 abort ();
9565 }
9566 else
9567 abort ();
9568 }
9569 #ifdef HAVE_X_I18N
9570 /* Don't dispatch this event since XtDispatchEvent calls
9571 XFilterEvent, and two calls in a row may freeze the
9572 client. */
9573 break;
9574 #else
9575 goto OTHER;
9576 #endif
9577
9578 case KeyRelease:
9579 #ifdef HAVE_X_I18N
9580 /* Don't dispatch this event since XtDispatchEvent calls
9581 XFilterEvent, and two calls in a row may freeze the
9582 client. */
9583 break;
9584 #else
9585 goto OTHER;
9586 #endif
9587
9588 /* Here's a possible interpretation of the whole
9589 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9590 you get a FocusIn event, you have to get a FocusOut
9591 event before you relinquish the focus. If you
9592 haven't received a FocusIn event, then a mere
9593 LeaveNotify is enough to free you. */
9594
9595 case EnterNotify:
9596 {
9597 int from_menu_bar_p = 0;
9598
9599 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9600
9601 #ifdef LESSTIF_VERSION
9602 /* When clicking outside of a menu bar popup to close
9603 it, we get a FocusIn/ EnterNotify sequence of
9604 events. The flag event.xcrossing.focus is not set
9605 in the EnterNotify event of that sequence because
9606 the focus is in the menu bar,
9607 event.xcrossing.window is the frame's X window.
9608 Unconditionally setting the focus frame to null in
9609 this case is not the right thing, because no event
9610 follows that could set the focus frame to the right
9611 value.
9612
9613 This could be a LessTif bug, but I wasn't able to
9614 reproduce the behavior in a simple test program.
9615
9616 (gerd, LessTif 0.88.1). */
9617
9618 if (!event.xcrossing.focus
9619 && f
9620 && f->output_data.x->menubar_widget)
9621 {
9622 Window focus;
9623 int revert;
9624
9625 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9626 if (focus == XtWindow (f->output_data.x->menubar_widget))
9627 from_menu_bar_p = 1;
9628 }
9629 #endif /* LESSTIF_VERSION */
9630
9631 if (event.xcrossing.focus || from_menu_bar_p)
9632 {
9633 /* Avoid nasty pop/raise loops. */
9634 if (f && (!(f->auto_raise)
9635 || !(f->auto_lower)
9636 || (event.xcrossing.time - enter_timestamp) > 500))
9637 {
9638 x_new_focus_frame (dpyinfo, f);
9639 enter_timestamp = event.xcrossing.time;
9640 }
9641 }
9642 else if (f == dpyinfo->x_focus_frame)
9643 x_new_focus_frame (dpyinfo, 0);
9644
9645 /* EnterNotify counts as mouse movement,
9646 so update things that depend on mouse position. */
9647 if (f && !f->output_data.x->busy_p)
9648 note_mouse_movement (f, &event.xmotion);
9649 goto OTHER;
9650 }
9651
9652 case FocusIn:
9653 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9654 if (event.xfocus.detail != NotifyPointer)
9655 dpyinfo->x_focus_event_frame = f;
9656 if (f)
9657 {
9658 x_new_focus_frame (dpyinfo, f);
9659
9660 /* Don't stop displaying the initial startup message
9661 for a switch-frame event we don't need. */
9662 if (GC_NILP (Vterminal_frame)
9663 && GC_CONSP (Vframe_list)
9664 && !GC_NILP (XCDR (Vframe_list)))
9665 {
9666 bufp->kind = FOCUS_IN_EVENT;
9667 XSETFRAME (bufp->frame_or_window, f);
9668 ++bufp, ++count, --numchars;
9669 }
9670 }
9671
9672 #ifdef HAVE_X_I18N
9673 if (f && FRAME_XIC (f))
9674 XSetICFocus (FRAME_XIC (f));
9675 #endif
9676
9677 goto OTHER;
9678
9679 case LeaveNotify:
9680 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9681 if (f)
9682 {
9683 Lisp_Object frame;
9684 int from_menu_bar_p = 0;
9685
9686 if (f == dpyinfo->mouse_face_mouse_frame)
9687 {
9688 /* If we move outside the frame, then we're
9689 certainly no longer on any text in the frame. */
9690 clear_mouse_face (dpyinfo);
9691 dpyinfo->mouse_face_mouse_frame = 0;
9692 }
9693
9694 /* Generate a nil HELP_EVENT to cancel a help-echo.
9695 Do it only if there's something to cancel.
9696 Otherwise, the startup message is cleared when
9697 the mouse leaves the frame. */
9698 if (any_help_event_p)
9699 {
9700 XSETFRAME (frame, f);
9701 bufp->kind = HELP_EVENT;
9702 bufp->frame_or_window = Fcons (frame, Qnil);
9703 ++bufp, ++count, --numchars;
9704 }
9705
9706 #ifdef LESSTIF_VERSION
9707 /* Please see the comment at the start of the
9708 EnterNotify case. */
9709 if (!event.xcrossing.focus
9710 && f->output_data.x->menubar_widget)
9711 {
9712 Window focus;
9713 int revert;
9714 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9715 if (focus == XtWindow (f->output_data.x->menubar_widget))
9716 from_menu_bar_p = 1;
9717 }
9718 #endif /* LESSTIF_VERSION */
9719
9720 if (event.xcrossing.focus || from_menu_bar_p)
9721 x_mouse_leave (dpyinfo);
9722 else
9723 {
9724 if (f == dpyinfo->x_focus_event_frame)
9725 dpyinfo->x_focus_event_frame = 0;
9726 if (f == dpyinfo->x_focus_frame)
9727 x_new_focus_frame (dpyinfo, 0);
9728 }
9729 }
9730 goto OTHER;
9731
9732 case FocusOut:
9733 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9734 if (event.xfocus.detail != NotifyPointer
9735 && f == dpyinfo->x_focus_event_frame)
9736 dpyinfo->x_focus_event_frame = 0;
9737 if (f && f == dpyinfo->x_focus_frame)
9738 x_new_focus_frame (dpyinfo, 0);
9739
9740 #ifdef HAVE_X_I18N
9741 if (f && FRAME_XIC (f))
9742 XUnsetICFocus (FRAME_XIC (f));
9743 #endif
9744
9745 goto OTHER;
9746
9747 case MotionNotify:
9748 {
9749 previous_help_echo = help_echo;
9750 help_echo = Qnil;
9751
9752 if (dpyinfo->grabbed && last_mouse_frame
9753 && FRAME_LIVE_P (last_mouse_frame))
9754 f = last_mouse_frame;
9755 else
9756 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9757
9758 if (f)
9759 note_mouse_movement (f, &event.xmotion);
9760 else
9761 {
9762 #ifndef USE_TOOLKIT_SCROLL_BARS
9763 struct scroll_bar *bar
9764 = x_window_to_scroll_bar (event.xmotion.window);
9765
9766 if (bar)
9767 x_scroll_bar_note_movement (bar, &event);
9768 #endif /* USE_TOOLKIT_SCROLL_BARS */
9769
9770 /* If we move outside the frame, then we're
9771 certainly no longer on any text in the frame. */
9772 clear_mouse_face (dpyinfo);
9773 }
9774
9775 /* If the contents of the global variable help_echo
9776 has changed, generate a HELP_EVENT. */
9777 if (STRINGP (help_echo)
9778 || STRINGP (previous_help_echo))
9779 {
9780 Lisp_Object frame;
9781
9782 if (f)
9783 XSETFRAME (frame, f);
9784 else
9785 frame = Qnil;
9786
9787 any_help_event_p = 1;
9788 bufp->kind = HELP_EVENT;
9789 bufp->frame_or_window = Fcons (frame, help_echo);
9790 ++bufp, ++count, --numchars;
9791 }
9792
9793 goto OTHER;
9794 }
9795
9796 case ConfigureNotify:
9797 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9798 if (f)
9799 {
9800 #ifndef USE_X_TOOLKIT
9801 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9802 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9803
9804 /* In the toolkit version, change_frame_size
9805 is called by the code that handles resizing
9806 of the EmacsFrame widget. */
9807
9808 /* Even if the number of character rows and columns has
9809 not changed, the font size may have changed, so we need
9810 to check the pixel dimensions as well. */
9811 if (columns != f->width
9812 || rows != f->height
9813 || event.xconfigure.width != f->output_data.x->pixel_width
9814 || event.xconfigure.height != f->output_data.x->pixel_height)
9815 {
9816 change_frame_size (f, rows, columns, 0, 1, 0);
9817 SET_FRAME_GARBAGED (f);
9818 cancel_mouse_face (f);
9819 }
9820 #endif
9821
9822 f->output_data.x->pixel_width = event.xconfigure.width;
9823 f->output_data.x->pixel_height = event.xconfigure.height;
9824
9825 /* What we have now is the position of Emacs's own window.
9826 Convert that to the position of the window manager window. */
9827 x_real_positions (f, &f->output_data.x->left_pos,
9828 &f->output_data.x->top_pos);
9829
9830 #ifdef HAVE_X_I18N
9831 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9832 xic_set_statusarea (f);
9833 #endif
9834
9835 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9836 {
9837 /* Since the WM decorations come below top_pos now,
9838 we must put them below top_pos in the future. */
9839 f->output_data.x->win_gravity = NorthWestGravity;
9840 x_wm_set_size_hint (f, (long) 0, 0);
9841 }
9842 #ifdef USE_MOTIF
9843 /* Some window managers pass (0,0) as the location of
9844 the window, and the Motif event handler stores it
9845 in the emacs widget, which messes up Motif menus. */
9846 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9847 {
9848 event.xconfigure.x = f->output_data.x->widget->core.x;
9849 event.xconfigure.y = f->output_data.x->widget->core.y;
9850 }
9851 #endif /* USE_MOTIF */
9852 }
9853 goto OTHER;
9854
9855 case ButtonPress:
9856 case ButtonRelease:
9857 {
9858 /* If we decide we want to generate an event to be seen
9859 by the rest of Emacs, we put it here. */
9860 struct input_event emacs_event;
9861 int tool_bar_p = 0;
9862
9863 emacs_event.kind = no_event;
9864 bzero (&compose_status, sizeof (compose_status));
9865
9866 if (dpyinfo->grabbed
9867 && last_mouse_frame
9868 && FRAME_LIVE_P (last_mouse_frame))
9869 f = last_mouse_frame;
9870 else
9871 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9872
9873 if (f)
9874 {
9875 /* Is this in the tool-bar? */
9876 if (WINDOWP (f->tool_bar_window)
9877 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
9878 {
9879 Lisp_Object window;
9880 int p, x, y;
9881
9882 x = event.xbutton.x;
9883 y = event.xbutton.y;
9884
9885 /* Set x and y. */
9886 window = window_from_coordinates (f, x, y, &p, 1);
9887 if (EQ (window, f->tool_bar_window))
9888 {
9889 x_handle_tool_bar_click (f, &event.xbutton);
9890 tool_bar_p = 1;
9891 }
9892 }
9893
9894 if (!tool_bar_p)
9895 if (!dpyinfo->x_focus_frame
9896 || f == dpyinfo->x_focus_frame)
9897 construct_mouse_click (&emacs_event, &event, f);
9898 }
9899 else
9900 {
9901 #ifndef USE_TOOLKIT_SCROLL_BARS
9902 struct scroll_bar *bar
9903 = x_window_to_scroll_bar (event.xbutton.window);
9904
9905 if (bar)
9906 x_scroll_bar_handle_click (bar, &event, &emacs_event);
9907 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9908 }
9909
9910 if (event.type == ButtonPress)
9911 {
9912 dpyinfo->grabbed |= (1 << event.xbutton.button);
9913 last_mouse_frame = f;
9914 /* Ignore any mouse motion that happened
9915 before this event; any subsequent mouse-movement
9916 Emacs events should reflect only motion after
9917 the ButtonPress. */
9918 if (f != 0)
9919 f->mouse_moved = 0;
9920
9921 if (!tool_bar_p)
9922 last_tool_bar_item = -1;
9923 }
9924 else
9925 {
9926 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
9927 }
9928
9929 if (numchars >= 1 && emacs_event.kind != no_event)
9930 {
9931 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9932 bufp++;
9933 count++;
9934 numchars--;
9935 }
9936
9937 #ifdef USE_X_TOOLKIT
9938 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9939 /* For a down-event in the menu bar,
9940 don't pass it to Xt right now.
9941 Instead, save it away
9942 and we will pass it to Xt from kbd_buffer_get_event.
9943 That way, we can run some Lisp code first. */
9944 if (f && event.type == ButtonPress
9945 /* Verify the event is really within the menu bar
9946 and not just sent to it due to grabbing. */
9947 && event.xbutton.x >= 0
9948 && event.xbutton.x < f->output_data.x->pixel_width
9949 && event.xbutton.y >= 0
9950 && event.xbutton.y < f->output_data.x->menubar_height
9951 && event.xbutton.same_screen)
9952 {
9953 SET_SAVED_BUTTON_EVENT;
9954 XSETFRAME (last_mouse_press_frame, f);
9955 }
9956 else if (event.type == ButtonPress)
9957 {
9958 last_mouse_press_frame = Qnil;
9959 goto OTHER;
9960 }
9961
9962 #ifdef USE_MOTIF /* This should do not harm for Lucid,
9963 but I am trying to be cautious. */
9964 else if (event.type == ButtonRelease)
9965 {
9966 if (!NILP (last_mouse_press_frame))
9967 {
9968 f = XFRAME (last_mouse_press_frame);
9969 if (f->output_data.x)
9970 SET_SAVED_BUTTON_EVENT;
9971 }
9972 else
9973 goto OTHER;
9974 }
9975 #endif /* USE_MOTIF */
9976 else
9977 goto OTHER;
9978 #endif /* USE_X_TOOLKIT */
9979 }
9980 break;
9981
9982 case CirculateNotify:
9983 goto OTHER;
9984
9985 case CirculateRequest:
9986 goto OTHER;
9987
9988 case VisibilityNotify:
9989 goto OTHER;
9990
9991 case MappingNotify:
9992 /* Someone has changed the keyboard mapping - update the
9993 local cache. */
9994 switch (event.xmapping.request)
9995 {
9996 case MappingModifier:
9997 x_find_modifier_meanings (dpyinfo);
9998 /* This is meant to fall through. */
9999 case MappingKeyboard:
10000 XRefreshKeyboardMapping (&event.xmapping);
10001 }
10002 goto OTHER;
10003
10004 default:
10005 OTHER:
10006 #ifdef USE_X_TOOLKIT
10007 BLOCK_INPUT;
10008 XtDispatchEvent (&event);
10009 UNBLOCK_INPUT;
10010 #endif /* USE_X_TOOLKIT */
10011 break;
10012 }
10013 }
10014 }
10015
10016 out:;
10017
10018 /* On some systems, an X bug causes Emacs to get no more events
10019 when the window is destroyed. Detect that. (1994.) */
10020 if (! event_found)
10021 {
10022 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10023 One XNOOP in 100 loops will make Emacs terminate.
10024 B. Bretthauer, 1994 */
10025 x_noop_count++;
10026 if (x_noop_count >= 100)
10027 {
10028 x_noop_count=0;
10029
10030 if (next_noop_dpyinfo == 0)
10031 next_noop_dpyinfo = x_display_list;
10032
10033 XNoOp (next_noop_dpyinfo->display);
10034
10035 /* Each time we get here, cycle through the displays now open. */
10036 next_noop_dpyinfo = next_noop_dpyinfo->next;
10037 }
10038 }
10039
10040 /* If the focus was just given to an auto-raising frame,
10041 raise it now. */
10042 /* ??? This ought to be able to handle more than one such frame. */
10043 if (pending_autoraise_frame)
10044 {
10045 x_raise_frame (pending_autoraise_frame);
10046 pending_autoraise_frame = 0;
10047 }
10048
10049 UNBLOCK_INPUT;
10050 return count;
10051 }
10052
10053
10054
10055 \f
10056 /***********************************************************************
10057 Text Cursor
10058 ***********************************************************************/
10059
10060 /* Note if the text cursor of window W has been overwritten by a
10061 drawing operation that outputs N glyphs starting at HPOS in the
10062 line given by output_cursor.vpos. N < 0 means all the rest of the
10063 line after HPOS has been written. */
10064
10065 static void
10066 note_overwritten_text_cursor (w, hpos, n)
10067 struct window *w;
10068 int hpos, n;
10069 {
10070 if (updated_area == TEXT_AREA
10071 && output_cursor.vpos == w->phys_cursor.vpos
10072 && hpos <= w->phys_cursor.hpos
10073 && (n < 0
10074 || hpos + n > w->phys_cursor.hpos))
10075 w->phys_cursor_on_p = 0;
10076 }
10077
10078
10079 /* Set clipping for output in glyph row ROW. W is the window in which
10080 we operate. GC is the graphics context to set clipping in.
10081 WHOLE_LINE_P non-zero means include the areas used for truncation
10082 mark display and alike in the clipping rectangle.
10083
10084 ROW may be a text row or, e.g., a mode line. Text rows must be
10085 clipped to the interior of the window dedicated to text display,
10086 mode lines must be clipped to the whole window. */
10087
10088 static void
10089 x_clip_to_row (w, row, gc, whole_line_p)
10090 struct window *w;
10091 struct glyph_row *row;
10092 GC gc;
10093 int whole_line_p;
10094 {
10095 struct frame *f = XFRAME (WINDOW_FRAME (w));
10096 XRectangle clip_rect;
10097 int window_x, window_y, window_width, window_height;
10098
10099 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10100
10101 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10102 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10103 clip_rect.y = max (clip_rect.y, window_y);
10104 clip_rect.width = window_width;
10105 clip_rect.height = row->visible_height;
10106
10107 /* If clipping to the whole line, including trunc marks, extend
10108 the rectangle to the left and increase its width. */
10109 if (whole_line_p)
10110 {
10111 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10112 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10113 }
10114
10115 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10116 }
10117
10118
10119 /* Draw a hollow box cursor on window W in glyph row ROW. */
10120
10121 static void
10122 x_draw_hollow_cursor (w, row)
10123 struct window *w;
10124 struct glyph_row *row;
10125 {
10126 struct frame *f = XFRAME (WINDOW_FRAME (w));
10127 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10128 Display *dpy = FRAME_X_DISPLAY (f);
10129 int x, y, wd, h;
10130 XGCValues xgcv;
10131 struct glyph *cursor_glyph;
10132 GC gc;
10133
10134 /* Compute frame-relative coordinates from window-relative
10135 coordinates. */
10136 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10137 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10138 + row->ascent - w->phys_cursor_ascent);
10139 h = row->height - 1;
10140
10141 /* Get the glyph the cursor is on. If we can't tell because
10142 the current matrix is invalid or such, give up. */
10143 cursor_glyph = get_phys_cursor_glyph (w);
10144 if (cursor_glyph == NULL)
10145 return;
10146
10147 /* Compute the width of the rectangle to draw. If on a stretch
10148 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10149 rectangle as wide as the glyph, but use a canonical character
10150 width instead. */
10151 wd = cursor_glyph->pixel_width - 1;
10152 if (cursor_glyph->type == STRETCH_GLYPH
10153 && !x_stretch_cursor_p)
10154 wd = min (CANON_X_UNIT (f), wd);
10155
10156 /* The foreground of cursor_gc is typically the same as the normal
10157 background color, which can cause the cursor box to be invisible. */
10158 xgcv.foreground = f->output_data.x->cursor_pixel;
10159 if (dpyinfo->scratch_cursor_gc)
10160 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10161 else
10162 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10163 GCForeground, &xgcv);
10164 gc = dpyinfo->scratch_cursor_gc;
10165
10166 /* Set clipping, draw the rectangle, and reset clipping again. */
10167 x_clip_to_row (w, row, gc, 0);
10168 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10169 XSetClipMask (dpy, gc, None);
10170 }
10171
10172
10173 /* Draw a bar cursor on window W in glyph row ROW.
10174
10175 Implementation note: One would like to draw a bar cursor with an
10176 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10177 Unfortunately, I didn't find a font yet that has this property set.
10178 --gerd. */
10179
10180 static void
10181 x_draw_bar_cursor (w, row, width)
10182 struct window *w;
10183 struct glyph_row *row;
10184 int width;
10185 {
10186 /* If cursor hpos is out of bounds, don't draw garbage. This can
10187 happen in mini-buffer windows when switching between echo area
10188 glyphs and mini-buffer. */
10189 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10190 {
10191 struct frame *f = XFRAME (w->frame);
10192 struct glyph *cursor_glyph;
10193 GC gc;
10194 int x;
10195 unsigned long mask;
10196 XGCValues xgcv;
10197 Display *dpy;
10198 Window window;
10199
10200 cursor_glyph = get_phys_cursor_glyph (w);
10201 if (cursor_glyph == NULL)
10202 return;
10203
10204 xgcv.background = f->output_data.x->cursor_pixel;
10205 xgcv.foreground = f->output_data.x->cursor_pixel;
10206 xgcv.graphics_exposures = 0;
10207 mask = GCForeground | GCBackground | GCGraphicsExposures;
10208 dpy = FRAME_X_DISPLAY (f);
10209 window = FRAME_X_WINDOW (f);
10210 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10211
10212 if (gc)
10213 XChangeGC (dpy, gc, mask, &xgcv);
10214 else
10215 {
10216 gc = XCreateGC (dpy, window, mask, &xgcv);
10217 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10218 }
10219
10220 if (width < 0)
10221 width = f->output_data.x->cursor_width;
10222
10223 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10224 x_clip_to_row (w, row, gc, 0);
10225 XFillRectangle (dpy, window, gc,
10226 x,
10227 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10228 min (cursor_glyph->pixel_width, width),
10229 row->height);
10230 XSetClipMask (dpy, gc, None);
10231 }
10232 }
10233
10234
10235 /* Clear the cursor of window W to background color, and mark the
10236 cursor as not shown. This is used when the text where the cursor
10237 is is about to be rewritten. */
10238
10239 static void
10240 x_clear_cursor (w)
10241 struct window *w;
10242 {
10243 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10244 x_update_window_cursor (w, 0);
10245 }
10246
10247
10248 /* Draw the cursor glyph of window W in glyph row ROW. See the
10249 comment of x_draw_glyphs for the meaning of HL. */
10250
10251 static void
10252 x_draw_phys_cursor_glyph (w, row, hl)
10253 struct window *w;
10254 struct glyph_row *row;
10255 enum draw_glyphs_face hl;
10256 {
10257 /* If cursor hpos is out of bounds, don't draw garbage. This can
10258 happen in mini-buffer windows when switching between echo area
10259 glyphs and mini-buffer. */
10260 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10261 {
10262 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10263 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10264 hl, 0, 0, 0);
10265
10266 /* When we erase the cursor, and ROW is overlapped by other
10267 rows, make sure that these overlapping parts of other rows
10268 are redrawn. */
10269 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10270 {
10271 if (row > w->current_matrix->rows
10272 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10273 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10274
10275 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10276 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10277 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10278 }
10279 }
10280 }
10281
10282
10283 /* Erase the image of a cursor of window W from the screen. */
10284
10285 static void
10286 x_erase_phys_cursor (w)
10287 struct window *w;
10288 {
10289 struct frame *f = XFRAME (w->frame);
10290 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10291 int hpos = w->phys_cursor.hpos;
10292 int vpos = w->phys_cursor.vpos;
10293 int mouse_face_here_p = 0;
10294 struct glyph_matrix *active_glyphs = w->current_matrix;
10295 struct glyph_row *cursor_row;
10296 struct glyph *cursor_glyph;
10297 enum draw_glyphs_face hl;
10298
10299 /* No cursor displayed or row invalidated => nothing to do on the
10300 screen. */
10301 if (w->phys_cursor_type == NO_CURSOR)
10302 goto mark_cursor_off;
10303
10304 /* VPOS >= active_glyphs->nrows means that window has been resized.
10305 Don't bother to erase the cursor. */
10306 if (vpos >= active_glyphs->nrows)
10307 goto mark_cursor_off;
10308
10309 /* If row containing cursor is marked invalid, there is nothing we
10310 can do. */
10311 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10312 if (!cursor_row->enabled_p)
10313 goto mark_cursor_off;
10314
10315 /* This can happen when the new row is shorter than the old one.
10316 In this case, either x_draw_glyphs or clear_end_of_line
10317 should have cleared the cursor. Note that we wouldn't be
10318 able to erase the cursor in this case because we don't have a
10319 cursor glyph at hand. */
10320 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10321 goto mark_cursor_off;
10322
10323 /* If the cursor is in the mouse face area, redisplay that when
10324 we clear the cursor. */
10325 if (! NILP (dpyinfo->mouse_face_window)
10326 && w == XWINDOW (dpyinfo->mouse_face_window)
10327 && (vpos > dpyinfo->mouse_face_beg_row
10328 || (vpos == dpyinfo->mouse_face_beg_row
10329 && hpos >= dpyinfo->mouse_face_beg_col))
10330 && (vpos < dpyinfo->mouse_face_end_row
10331 || (vpos == dpyinfo->mouse_face_end_row
10332 && hpos < dpyinfo->mouse_face_end_col))
10333 /* Don't redraw the cursor's spot in mouse face if it is at the
10334 end of a line (on a newline). The cursor appears there, but
10335 mouse highlighting does not. */
10336 && cursor_row->used[TEXT_AREA] > hpos)
10337 mouse_face_here_p = 1;
10338
10339 /* Maybe clear the display under the cursor. */
10340 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10341 {
10342 int x;
10343 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10344
10345 cursor_glyph = get_phys_cursor_glyph (w);
10346 if (cursor_glyph == NULL)
10347 goto mark_cursor_off;
10348
10349 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10350
10351 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10352 x,
10353 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10354 cursor_row->y)),
10355 cursor_glyph->pixel_width,
10356 cursor_row->visible_height,
10357 False);
10358 }
10359
10360 /* Erase the cursor by redrawing the character underneath it. */
10361 if (mouse_face_here_p)
10362 hl = DRAW_MOUSE_FACE;
10363 else if (cursor_row->inverse_p)
10364 hl = DRAW_INVERSE_VIDEO;
10365 else
10366 hl = DRAW_NORMAL_TEXT;
10367 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10368
10369 mark_cursor_off:
10370 w->phys_cursor_on_p = 0;
10371 w->phys_cursor_type = NO_CURSOR;
10372 }
10373
10374
10375 /* Display or clear cursor of window W. If ON is zero, clear the
10376 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10377 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10378
10379 void
10380 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10381 struct window *w;
10382 int on, hpos, vpos, x, y;
10383 {
10384 struct frame *f = XFRAME (w->frame);
10385 int new_cursor_type;
10386 int new_cursor_width;
10387 struct glyph_matrix *current_glyphs;
10388 struct glyph_row *glyph_row;
10389 struct glyph *glyph;
10390
10391 /* This is pointless on invisible frames, and dangerous on garbaged
10392 windows and frames; in the latter case, the frame or window may
10393 be in the midst of changing its size, and x and y may be off the
10394 window. */
10395 if (! FRAME_VISIBLE_P (f)
10396 || FRAME_GARBAGED_P (f)
10397 || vpos >= w->current_matrix->nrows
10398 || hpos >= w->current_matrix->matrix_w)
10399 return;
10400
10401 /* If cursor is off and we want it off, return quickly. */
10402 if (!on && !w->phys_cursor_on_p)
10403 return;
10404
10405 current_glyphs = w->current_matrix;
10406 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10407 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10408
10409 /* If cursor row is not enabled, we don't really know where to
10410 display the cursor. */
10411 if (!glyph_row->enabled_p)
10412 {
10413 w->phys_cursor_on_p = 0;
10414 return;
10415 }
10416
10417 xassert (interrupt_input_blocked);
10418
10419 /* Set new_cursor_type to the cursor we want to be displayed. In a
10420 mini-buffer window, we want the cursor only to appear if we are
10421 reading input from this window. For the selected window, we want
10422 the cursor type given by the frame parameter. If explicitly
10423 marked off, draw no cursor. In all other cases, we want a hollow
10424 box cursor. */
10425 new_cursor_width = -1;
10426 if (cursor_in_echo_area
10427 && FRAME_HAS_MINIBUF_P (f)
10428 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10429 {
10430 if (w == XWINDOW (echo_area_window))
10431 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10432 else
10433 new_cursor_type = HOLLOW_BOX_CURSOR;
10434 }
10435 else
10436 {
10437 if (w != XWINDOW (selected_window)
10438 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10439 {
10440 extern int cursor_in_non_selected_windows;
10441
10442 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
10443 new_cursor_type = NO_CURSOR;
10444 else
10445 new_cursor_type = HOLLOW_BOX_CURSOR;
10446 }
10447 else if (w->cursor_off_p)
10448 new_cursor_type = NO_CURSOR;
10449 else
10450 {
10451 struct buffer *b = XBUFFER (w->buffer);
10452
10453 if (EQ (b->cursor_type, Qt))
10454 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10455 else
10456 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10457 &new_cursor_width);
10458 }
10459 }
10460
10461 /* If cursor is currently being shown and we don't want it to be or
10462 it is in the wrong place, or the cursor type is not what we want,
10463 erase it. */
10464 if (w->phys_cursor_on_p
10465 && (!on
10466 || w->phys_cursor.x != x
10467 || w->phys_cursor.y != y
10468 || new_cursor_type != w->phys_cursor_type))
10469 x_erase_phys_cursor (w);
10470
10471 /* If the cursor is now invisible and we want it to be visible,
10472 display it. */
10473 if (on && !w->phys_cursor_on_p)
10474 {
10475 w->phys_cursor_ascent = glyph_row->ascent;
10476 w->phys_cursor_height = glyph_row->height;
10477
10478 /* Set phys_cursor_.* before x_draw_.* is called because some
10479 of them may need the information. */
10480 w->phys_cursor.x = x;
10481 w->phys_cursor.y = glyph_row->y;
10482 w->phys_cursor.hpos = hpos;
10483 w->phys_cursor.vpos = vpos;
10484 w->phys_cursor_type = new_cursor_type;
10485 w->phys_cursor_on_p = 1;
10486
10487 switch (new_cursor_type)
10488 {
10489 case HOLLOW_BOX_CURSOR:
10490 x_draw_hollow_cursor (w, glyph_row);
10491 break;
10492
10493 case FILLED_BOX_CURSOR:
10494 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10495 break;
10496
10497 case BAR_CURSOR:
10498 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10499 break;
10500
10501 case NO_CURSOR:
10502 break;
10503
10504 default:
10505 abort ();
10506 }
10507
10508 #ifdef HAVE_X_I18N
10509 if (w == XWINDOW (f->selected_window))
10510 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10511 xic_set_preeditarea (w, x, y);
10512 #endif
10513 }
10514
10515 #ifndef XFlush
10516 if (updating_frame != f)
10517 XFlush (FRAME_X_DISPLAY (f));
10518 #endif
10519 }
10520
10521
10522 /* Display the cursor on window W, or clear it. X and Y are window
10523 relative pixel coordinates. HPOS and VPOS are glyph matrix
10524 positions. If W is not the selected window, display a hollow
10525 cursor. ON non-zero means display the cursor at X, Y which
10526 correspond to HPOS, VPOS, otherwise it is cleared. */
10527
10528 void
10529 x_display_cursor (w, on, hpos, vpos, x, y)
10530 struct window *w;
10531 int on, hpos, vpos, x, y;
10532 {
10533 BLOCK_INPUT;
10534 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10535 UNBLOCK_INPUT;
10536 }
10537
10538
10539 /* Display the cursor on window W, or clear it, according to ON_P.
10540 Don't change the cursor's position. */
10541
10542 void
10543 x_update_cursor (f, on_p)
10544 struct frame *f;
10545 {
10546 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10547 }
10548
10549
10550 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10551 in the window tree rooted at W. */
10552
10553 static void
10554 x_update_cursor_in_window_tree (w, on_p)
10555 struct window *w;
10556 int on_p;
10557 {
10558 while (w)
10559 {
10560 if (!NILP (w->hchild))
10561 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10562 else if (!NILP (w->vchild))
10563 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10564 else
10565 x_update_window_cursor (w, on_p);
10566
10567 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10568 }
10569 }
10570
10571
10572 /* Switch the display of W's cursor on or off, according to the value
10573 of ON. */
10574
10575 static void
10576 x_update_window_cursor (w, on)
10577 struct window *w;
10578 int on;
10579 {
10580 /* Don't update cursor in windows whose frame is in the process
10581 of being deleted. */
10582 if (w->current_matrix)
10583 {
10584 BLOCK_INPUT;
10585 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10586 w->phys_cursor.x, w->phys_cursor.y);
10587 UNBLOCK_INPUT;
10588 }
10589 }
10590
10591
10592
10593 \f
10594 /* Icons. */
10595
10596 /* Refresh bitmap kitchen sink icon for frame F
10597 when we get an expose event for it. */
10598
10599 void
10600 refreshicon (f)
10601 struct frame *f;
10602 {
10603 /* Normally, the window manager handles this function. */
10604 }
10605
10606 /* Make the x-window of frame F use the gnu icon bitmap. */
10607
10608 int
10609 x_bitmap_icon (f, file)
10610 struct frame *f;
10611 Lisp_Object file;
10612 {
10613 int bitmap_id;
10614
10615 if (FRAME_X_WINDOW (f) == 0)
10616 return 1;
10617
10618 /* Free up our existing icon bitmap if any. */
10619 if (f->output_data.x->icon_bitmap > 0)
10620 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10621 f->output_data.x->icon_bitmap = 0;
10622
10623 if (STRINGP (file))
10624 bitmap_id = x_create_bitmap_from_file (f, file);
10625 else
10626 {
10627 /* Create the GNU bitmap if necessary. */
10628 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10629 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10630 = x_create_bitmap_from_data (f, gnu_bits,
10631 gnu_width, gnu_height);
10632
10633 /* The first time we create the GNU bitmap,
10634 this increments the ref-count one extra time.
10635 As a result, the GNU bitmap is never freed.
10636 That way, we don't have to worry about allocating it again. */
10637 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10638
10639 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10640 }
10641
10642 x_wm_set_icon_pixmap (f, bitmap_id);
10643 f->output_data.x->icon_bitmap = bitmap_id;
10644
10645 return 0;
10646 }
10647
10648
10649 /* Make the x-window of frame F use a rectangle with text.
10650 Use ICON_NAME as the text. */
10651
10652 int
10653 x_text_icon (f, icon_name)
10654 struct frame *f;
10655 char *icon_name;
10656 {
10657 if (FRAME_X_WINDOW (f) == 0)
10658 return 1;
10659
10660 #ifdef HAVE_X11R4
10661 {
10662 XTextProperty text;
10663 text.value = (unsigned char *) icon_name;
10664 text.encoding = XA_STRING;
10665 text.format = 8;
10666 text.nitems = strlen (icon_name);
10667 #ifdef USE_X_TOOLKIT
10668 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10669 &text);
10670 #else /* not USE_X_TOOLKIT */
10671 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10672 #endif /* not USE_X_TOOLKIT */
10673 }
10674 #else /* not HAVE_X11R4 */
10675 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10676 #endif /* not HAVE_X11R4 */
10677
10678 if (f->output_data.x->icon_bitmap > 0)
10679 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10680 f->output_data.x->icon_bitmap = 0;
10681 x_wm_set_icon_pixmap (f, 0);
10682
10683 return 0;
10684 }
10685 \f
10686 #define X_ERROR_MESSAGE_SIZE 200
10687
10688 /* If non-nil, this should be a string.
10689 It means catch X errors and store the error message in this string. */
10690
10691 static Lisp_Object x_error_message_string;
10692
10693 /* An X error handler which stores the error message in
10694 x_error_message_string. This is called from x_error_handler if
10695 x_catch_errors is in effect. */
10696
10697 static void
10698 x_error_catcher (display, error)
10699 Display *display;
10700 XErrorEvent *error;
10701 {
10702 XGetErrorText (display, error->error_code,
10703 XSTRING (x_error_message_string)->data,
10704 X_ERROR_MESSAGE_SIZE);
10705 }
10706
10707 /* Begin trapping X errors for display DPY. Actually we trap X errors
10708 for all displays, but DPY should be the display you are actually
10709 operating on.
10710
10711 After calling this function, X protocol errors no longer cause
10712 Emacs to exit; instead, they are recorded in the string
10713 stored in x_error_message_string.
10714
10715 Calling x_check_errors signals an Emacs error if an X error has
10716 occurred since the last call to x_catch_errors or x_check_errors.
10717
10718 Calling x_uncatch_errors resumes the normal error handling. */
10719
10720 void x_check_errors ();
10721 static Lisp_Object x_catch_errors_unwind ();
10722
10723 int
10724 x_catch_errors (dpy)
10725 Display *dpy;
10726 {
10727 int count = specpdl_ptr - specpdl;
10728
10729 /* Make sure any errors from previous requests have been dealt with. */
10730 XSync (dpy, False);
10731
10732 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10733
10734 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10735 XSTRING (x_error_message_string)->data[0] = 0;
10736
10737 return count;
10738 }
10739
10740 /* Unbind the binding that we made to check for X errors. */
10741
10742 static Lisp_Object
10743 x_catch_errors_unwind (old_val)
10744 Lisp_Object old_val;
10745 {
10746 x_error_message_string = old_val;
10747 return Qnil;
10748 }
10749
10750 /* If any X protocol errors have arrived since the last call to
10751 x_catch_errors or x_check_errors, signal an Emacs error using
10752 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10753
10754 void
10755 x_check_errors (dpy, format)
10756 Display *dpy;
10757 char *format;
10758 {
10759 /* Make sure to catch any errors incurred so far. */
10760 XSync (dpy, False);
10761
10762 if (XSTRING (x_error_message_string)->data[0])
10763 error (format, XSTRING (x_error_message_string)->data);
10764 }
10765
10766 /* Nonzero if we had any X protocol errors
10767 since we did x_catch_errors on DPY. */
10768
10769 int
10770 x_had_errors_p (dpy)
10771 Display *dpy;
10772 {
10773 /* Make sure to catch any errors incurred so far. */
10774 XSync (dpy, False);
10775
10776 return XSTRING (x_error_message_string)->data[0] != 0;
10777 }
10778
10779 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10780
10781 void
10782 x_clear_errors (dpy)
10783 Display *dpy;
10784 {
10785 XSTRING (x_error_message_string)->data[0] = 0;
10786 }
10787
10788 /* Stop catching X protocol errors and let them make Emacs die.
10789 DPY should be the display that was passed to x_catch_errors.
10790 COUNT should be the value that was returned by
10791 the corresponding call to x_catch_errors. */
10792
10793 void
10794 x_uncatch_errors (dpy, count)
10795 Display *dpy;
10796 int count;
10797 {
10798 unbind_to (count, Qnil);
10799 }
10800
10801 #if 0
10802 static unsigned int x_wire_count;
10803 x_trace_wire ()
10804 {
10805 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10806 }
10807 #endif /* ! 0 */
10808
10809 \f
10810 /* Handle SIGPIPE, which can happen when the connection to a server
10811 simply goes away. SIGPIPE is handled by x_connection_signal.
10812 Don't need to do anything, because the write which caused the
10813 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
10814 which will do the appropriate cleanup for us. */
10815
10816 static SIGTYPE
10817 x_connection_signal (signalnum) /* If we don't have an argument, */
10818 int signalnum; /* some compilers complain in signal calls. */
10819 {
10820 #ifdef USG
10821 /* USG systems forget handlers when they are used;
10822 must reestablish each time */
10823 signal (signalnum, x_connection_signal);
10824 #endif /* USG */
10825 }
10826 \f
10827 /* Handling X errors. */
10828
10829 /* Handle the loss of connection to display DISPLAY. */
10830
10831 static SIGTYPE
10832 x_connection_closed (display, error_message)
10833 Display *display;
10834 char *error_message;
10835 {
10836 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10837 Lisp_Object frame, tail;
10838
10839 /* Indicate that this display is dead. */
10840
10841 #if 0 /* Closing the display caused a bus error on OpenWindows. */
10842 #ifdef USE_X_TOOLKIT
10843 XtCloseDisplay (display);
10844 #endif
10845 #endif
10846
10847 if (dpyinfo)
10848 dpyinfo->display = 0;
10849
10850 /* First delete frames whose mini-buffers are on frames
10851 that are on the dead display. */
10852 FOR_EACH_FRAME (tail, frame)
10853 {
10854 Lisp_Object minibuf_frame;
10855 minibuf_frame
10856 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
10857 if (FRAME_X_P (XFRAME (frame))
10858 && FRAME_X_P (XFRAME (minibuf_frame))
10859 && ! EQ (frame, minibuf_frame)
10860 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
10861 Fdelete_frame (frame, Qt);
10862 }
10863
10864 /* Now delete all remaining frames on the dead display.
10865 We are now sure none of these is used as the mini-buffer
10866 for another frame that we need to delete. */
10867 FOR_EACH_FRAME (tail, frame)
10868 if (FRAME_X_P (XFRAME (frame))
10869 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
10870 {
10871 /* Set this to t so that Fdelete_frame won't get confused
10872 trying to find a replacement. */
10873 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10874 Fdelete_frame (frame, Qt);
10875 }
10876
10877 if (dpyinfo)
10878 x_delete_display (dpyinfo);
10879
10880 if (x_display_list == 0)
10881 {
10882 fprintf (stderr, "%s\n", error_message);
10883 shut_down_emacs (0, 0, Qnil);
10884 exit (70);
10885 }
10886
10887 /* Ordinary stack unwind doesn't deal with these. */
10888 #ifdef SIGIO
10889 sigunblock (sigmask (SIGIO));
10890 #endif
10891 sigunblock (sigmask (SIGALRM));
10892 TOTALLY_UNBLOCK_INPUT;
10893
10894 clear_waiting_for_input ();
10895 error ("%s", error_message);
10896 }
10897
10898 /* This is the usual handler for X protocol errors.
10899 It kills all frames on the display that we got the error for.
10900 If that was the only one, it prints an error message and kills Emacs. */
10901
10902 static void
10903 x_error_quitter (display, error)
10904 Display *display;
10905 XErrorEvent *error;
10906 {
10907 char buf[256], buf1[356];
10908
10909 /* Note that there is no real way portable across R3/R4 to get the
10910 original error handler. */
10911
10912 XGetErrorText (display, error->error_code, buf, sizeof (buf));
10913 sprintf (buf1, "X protocol error: %s on protocol request %d",
10914 buf, error->request_code);
10915 x_connection_closed (display, buf1);
10916 }
10917
10918 /* This is the first-level handler for X protocol errors.
10919 It calls x_error_quitter or x_error_catcher. */
10920
10921 static int
10922 x_error_handler (display, error)
10923 Display *display;
10924 XErrorEvent *error;
10925 {
10926 if (! NILP (x_error_message_string))
10927 x_error_catcher (display, error);
10928 else
10929 x_error_quitter (display, error);
10930 return 0;
10931 }
10932
10933 /* This is the handler for X IO errors, always.
10934 It kills all frames on the display that we lost touch with.
10935 If that was the only one, it prints an error message and kills Emacs. */
10936
10937 static int
10938 x_io_error_quitter (display)
10939 Display *display;
10940 {
10941 char buf[256];
10942
10943 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10944 x_connection_closed (display, buf);
10945 return 0;
10946 }
10947 \f
10948 /* Changing the font of the frame. */
10949
10950 /* Give frame F the font named FONTNAME as its default font, and
10951 return the full name of that font. FONTNAME may be a wildcard
10952 pattern; in that case, we choose some font that fits the pattern.
10953 The return value shows which font we chose. */
10954
10955 Lisp_Object
10956 x_new_font (f, fontname)
10957 struct frame *f;
10958 register char *fontname;
10959 {
10960 struct font_info *fontp
10961 = FS_LOAD_FONT (f, 0, fontname, -1);
10962
10963 if (!fontp)
10964 return Qnil;
10965
10966 f->output_data.x->font = (XFontStruct *) (fontp->font);
10967 f->output_data.x->baseline_offset = fontp->baseline_offset;
10968 f->output_data.x->fontset = -1;
10969
10970 /* Compute the scroll bar width in character columns. */
10971 if (f->scroll_bar_pixel_width > 0)
10972 {
10973 int wid = FONT_WIDTH (f->output_data.x->font);
10974 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10975 }
10976 else
10977 {
10978 int wid = FONT_WIDTH (f->output_data.x->font);
10979 f->scroll_bar_cols = (14 + wid - 1) / wid;
10980 }
10981
10982 /* Now make the frame display the given font. */
10983 if (FRAME_X_WINDOW (f) != 0)
10984 {
10985 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10986 f->output_data.x->font->fid);
10987 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10988 f->output_data.x->font->fid);
10989 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10990 f->output_data.x->font->fid);
10991
10992 frame_update_line_height (f);
10993 x_set_window_size (f, 0, f->width, f->height);
10994 }
10995 else
10996 /* If we are setting a new frame's font for the first time,
10997 there are no faces yet, so this font's height is the line height. */
10998 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
10999
11000 return build_string (fontp->full_name);
11001 }
11002
11003 /* Give frame F the fontset named FONTSETNAME as its default font, and
11004 return the full name of that fontset. FONTSETNAME may be a wildcard
11005 pattern; in that case, we choose some fontset that fits the pattern.
11006 The return value shows which fontset we chose. */
11007
11008 Lisp_Object
11009 x_new_fontset (f, fontsetname)
11010 struct frame *f;
11011 char *fontsetname;
11012 {
11013 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11014 Lisp_Object result;
11015
11016 if (fontset < 0)
11017 return Qnil;
11018
11019 if (f->output_data.x->fontset == fontset)
11020 /* This fontset is already set in frame F. There's nothing more
11021 to do. */
11022 return fontset_name (fontset);
11023
11024 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11025
11026 if (!STRINGP (result))
11027 /* Can't load ASCII font. */
11028 return Qnil;
11029
11030 /* Since x_new_font doesn't update any fontset information, do it now. */
11031 f->output_data.x->fontset = fontset;
11032
11033 #ifdef HAVE_X_I18N
11034 if (FRAME_XIC (f)
11035 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11036 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11037 #endif
11038
11039 return build_string (fontsetname);
11040 }
11041
11042 \f
11043 /***********************************************************************
11044 X Input Methods
11045 ***********************************************************************/
11046
11047 #ifdef HAVE_X_I18N
11048
11049 #ifdef HAVE_X11R6
11050
11051 /* XIM destroy callback function, which is called whenever the
11052 connection to input method XIM dies. CLIENT_DATA contains a
11053 pointer to the x_display_info structure corresponding to XIM. */
11054
11055 static void
11056 xim_destroy_callback (xim, client_data, call_data)
11057 XIM xim;
11058 XPointer client_data;
11059 XPointer call_data;
11060 {
11061 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11062 Lisp_Object frame, tail;
11063
11064 BLOCK_INPUT;
11065
11066 /* No need to call XDestroyIC.. */
11067 FOR_EACH_FRAME (tail, frame)
11068 {
11069 struct frame *f = XFRAME (frame);
11070 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11071 {
11072 FRAME_XIC (f) = NULL;
11073 if (FRAME_XIC_FONTSET (f))
11074 {
11075 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11076 FRAME_XIC_FONTSET (f) = NULL;
11077 }
11078 }
11079 }
11080
11081 /* No need to call XCloseIM. */
11082 dpyinfo->xim = NULL;
11083 XFree (dpyinfo->xim_styles);
11084 UNBLOCK_INPUT;
11085 }
11086
11087 #endif /* HAVE_X11R6 */
11088
11089 /* Open the connection to the XIM server on display DPYINFO.
11090 RESOURCE_NAME is the resource name Emacs uses. */
11091
11092 static void
11093 xim_open_dpy (dpyinfo, resource_name)
11094 struct x_display_info *dpyinfo;
11095 char *resource_name;
11096 {
11097 XIM xim;
11098
11099 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11100 dpyinfo->xim = xim;
11101
11102 if (xim)
11103 {
11104 #ifdef HAVE_X11R6
11105 XIMCallback destroy;
11106 #endif
11107
11108 /* Get supported styles and XIM values. */
11109 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11110
11111 #ifdef HAVE_X11R6
11112 destroy.callback = xim_destroy_callback;
11113 destroy.client_data = (XPointer)dpyinfo;
11114 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11115 #endif
11116 }
11117 }
11118
11119
11120 #ifdef HAVE_X11R6_XIM
11121
11122 struct xim_inst_t
11123 {
11124 struct x_display_info *dpyinfo;
11125 char *resource_name;
11126 };
11127
11128 /* XIM instantiate callback function, which is called whenever an XIM
11129 server is available. DISPLAY is teh display of the XIM.
11130 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11131 when the callback was registered. */
11132
11133 static void
11134 xim_instantiate_callback (display, client_data, call_data)
11135 Display *display;
11136 XPointer client_data;
11137 XPointer call_data;
11138 {
11139 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11140 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11141
11142 /* We don't support multiple XIM connections. */
11143 if (dpyinfo->xim)
11144 return;
11145
11146 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11147
11148 /* Create XIC for the existing frames on the same display, as long
11149 as they have no XIC. */
11150 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11151 {
11152 Lisp_Object tail, frame;
11153
11154 BLOCK_INPUT;
11155 FOR_EACH_FRAME (tail, frame)
11156 {
11157 struct frame *f = XFRAME (frame);
11158
11159 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11160 if (FRAME_XIC (f) == NULL)
11161 {
11162 create_frame_xic (f);
11163 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11164 xic_set_statusarea (f);
11165 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11166 {
11167 struct window *w = XWINDOW (f->selected_window);
11168 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11169 }
11170 }
11171 }
11172
11173 UNBLOCK_INPUT;
11174 }
11175 }
11176
11177 #endif /* HAVE_X11R6_XIM */
11178
11179
11180 /* Open a connection to the XIM server on display DPYINFO.
11181 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11182 connection only at the first time. On X11R6, open the connection
11183 in the XIM instantiate callback function. */
11184
11185 static void
11186 xim_initialize (dpyinfo, resource_name)
11187 struct x_display_info *dpyinfo;
11188 char *resource_name;
11189 {
11190 #ifdef HAVE_X11R6_XIM
11191 struct xim_inst_t *xim_inst;
11192 int len;
11193
11194 dpyinfo->xim = NULL;
11195 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11196 xim_inst->dpyinfo = dpyinfo;
11197 len = strlen (resource_name);
11198 xim_inst->resource_name = (char *) xmalloc (len + 1);
11199 bcopy (resource_name, xim_inst->resource_name, len + 1);
11200 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11201 resource_name, EMACS_CLASS,
11202 xim_instantiate_callback,
11203 /* Fixme: This is XPointer in
11204 XFree86 but (XPointer *) on
11205 Tru64, at least. */
11206 (XPointer) xim_inst);
11207 #else /* not HAVE_X11R6_XIM */
11208 dpyinfo->xim = NULL;
11209 xim_open_dpy (dpyinfo, resource_name);
11210 #endif /* not HAVE_X11R6_XIM */
11211 }
11212
11213
11214 /* Close the connection to the XIM server on display DPYINFO. */
11215
11216 static void
11217 xim_close_dpy (dpyinfo)
11218 struct x_display_info *dpyinfo;
11219 {
11220 #ifdef HAVE_X11R6_XIM
11221 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11222 NULL, EMACS_CLASS,
11223 xim_instantiate_callback, NULL);
11224 #endif /* not HAVE_X11R6_XIM */
11225 XCloseIM (dpyinfo->xim);
11226 dpyinfo->xim = NULL;
11227 XFree (dpyinfo->xim_styles);
11228 }
11229
11230 #endif /* not HAVE_X11R6_XIM */
11231
11232
11233 \f
11234 /* Calculate the absolute position in frame F
11235 from its current recorded position values and gravity. */
11236
11237 void
11238 x_calc_absolute_position (f)
11239 struct frame *f;
11240 {
11241 Window child;
11242 int win_x = 0, win_y = 0;
11243 int flags = f->output_data.x->size_hint_flags;
11244 int this_window;
11245
11246 /* We have nothing to do if the current position
11247 is already for the top-left corner. */
11248 if (! ((flags & XNegative) || (flags & YNegative)))
11249 return;
11250
11251 #ifdef USE_X_TOOLKIT
11252 this_window = XtWindow (f->output_data.x->widget);
11253 #else
11254 this_window = FRAME_X_WINDOW (f);
11255 #endif
11256
11257 /* Find the position of the outside upper-left corner of
11258 the inner window, with respect to the outer window.
11259 But do this only if we will need the results. */
11260 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11261 {
11262 int count;
11263
11264 BLOCK_INPUT;
11265 count = x_catch_errors (FRAME_X_DISPLAY (f));
11266 while (1)
11267 {
11268 x_clear_errors (FRAME_X_DISPLAY (f));
11269 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11270
11271 /* From-window, to-window. */
11272 this_window,
11273 f->output_data.x->parent_desc,
11274
11275 /* From-position, to-position. */
11276 0, 0, &win_x, &win_y,
11277
11278 /* Child of win. */
11279 &child);
11280 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11281 {
11282 Window newroot, newparent = 0xdeadbeef;
11283 Window *newchildren;
11284 unsigned int nchildren;
11285
11286 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11287 &newparent, &newchildren, &nchildren))
11288 break;
11289
11290 XFree ((char *) newchildren);
11291
11292 f->output_data.x->parent_desc = newparent;
11293 }
11294 else
11295 break;
11296 }
11297
11298 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11299 UNBLOCK_INPUT;
11300 }
11301
11302 /* Treat negative positions as relative to the leftmost bottommost
11303 position that fits on the screen. */
11304 if (flags & XNegative)
11305 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11306 - 2 * f->output_data.x->border_width - win_x
11307 - PIXEL_WIDTH (f)
11308 + f->output_data.x->left_pos);
11309
11310 if (flags & YNegative)
11311 {
11312 int menubar_height = 0;
11313
11314 #ifdef USE_X_TOOLKIT
11315 if (f->output_data.x->menubar_widget)
11316 menubar_height
11317 = (f->output_data.x->menubar_widget->core.height
11318 + f->output_data.x->menubar_widget->core.border_width);
11319 #endif
11320
11321 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11322 - 2 * f->output_data.x->border_width
11323 - win_y
11324 - PIXEL_HEIGHT (f)
11325 - menubar_height
11326 + f->output_data.x->top_pos);
11327 }
11328
11329 /* The left_pos and top_pos
11330 are now relative to the top and left screen edges,
11331 so the flags should correspond. */
11332 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11333 }
11334
11335 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11336 to really change the position, and 0 when calling from
11337 x_make_frame_visible (in that case, XOFF and YOFF are the current
11338 position values). It is -1 when calling from x_set_frame_parameters,
11339 which means, do adjust for borders but don't change the gravity. */
11340
11341 void
11342 x_set_offset (f, xoff, yoff, change_gravity)
11343 struct frame *f;
11344 register int xoff, yoff;
11345 int change_gravity;
11346 {
11347 int modified_top, modified_left;
11348
11349 if (change_gravity > 0)
11350 {
11351 f->output_data.x->top_pos = yoff;
11352 f->output_data.x->left_pos = xoff;
11353 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11354 if (xoff < 0)
11355 f->output_data.x->size_hint_flags |= XNegative;
11356 if (yoff < 0)
11357 f->output_data.x->size_hint_flags |= YNegative;
11358 f->output_data.x->win_gravity = NorthWestGravity;
11359 }
11360 x_calc_absolute_position (f);
11361
11362 BLOCK_INPUT;
11363 x_wm_set_size_hint (f, (long) 0, 0);
11364
11365 modified_left = f->output_data.x->left_pos;
11366 modified_top = f->output_data.x->top_pos;
11367 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11368 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11369 /* It is a mystery why we need to add the border_width here
11370 when the frame is already visible, but experiment says we do. */
11371 if (change_gravity != 0)
11372 {
11373 modified_left += f->output_data.x->border_width;
11374 modified_top += f->output_data.x->border_width;
11375 }
11376 #endif
11377
11378 #ifdef USE_X_TOOLKIT
11379 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11380 modified_left, modified_top);
11381 #else /* not USE_X_TOOLKIT */
11382 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11383 modified_left, modified_top);
11384 #endif /* not USE_X_TOOLKIT */
11385 UNBLOCK_INPUT;
11386 }
11387
11388 /* Call this to change the size of frame F's x-window.
11389 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11390 for this size change and subsequent size changes.
11391 Otherwise we leave the window gravity unchanged. */
11392
11393 void
11394 x_set_window_size (f, change_gravity, cols, rows)
11395 struct frame *f;
11396 int change_gravity;
11397 int cols, rows;
11398 {
11399 #ifndef USE_X_TOOLKIT
11400 int pixelwidth, pixelheight;
11401 #endif
11402
11403 BLOCK_INPUT;
11404
11405 #ifdef USE_X_TOOLKIT
11406 {
11407 /* The x and y position of the widget is clobbered by the
11408 call to XtSetValues within EmacsFrameSetCharSize.
11409 This is a real kludge, but I don't understand Xt so I can't
11410 figure out a correct fix. Can anyone else tell me? -- rms. */
11411 int xpos = f->output_data.x->widget->core.x;
11412 int ypos = f->output_data.x->widget->core.y;
11413 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11414 f->output_data.x->widget->core.x = xpos;
11415 f->output_data.x->widget->core.y = ypos;
11416 }
11417
11418 #else /* not USE_X_TOOLKIT */
11419
11420 check_frame_size (f, &rows, &cols);
11421 f->output_data.x->vertical_scroll_bar_extra
11422 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11423 ? 0
11424 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11425 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11426 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11427 f->output_data.x->flags_areas_extra
11428 = FRAME_FLAGS_AREA_WIDTH (f);
11429 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11430 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11431
11432 f->output_data.x->win_gravity = NorthWestGravity;
11433 x_wm_set_size_hint (f, (long) 0, 0);
11434
11435 XSync (FRAME_X_DISPLAY (f), False);
11436 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11437 pixelwidth, pixelheight);
11438
11439 /* Now, strictly speaking, we can't be sure that this is accurate,
11440 but the window manager will get around to dealing with the size
11441 change request eventually, and we'll hear how it went when the
11442 ConfigureNotify event gets here.
11443
11444 We could just not bother storing any of this information here,
11445 and let the ConfigureNotify event set everything up, but that
11446 might be kind of confusing to the Lisp code, since size changes
11447 wouldn't be reported in the frame parameters until some random
11448 point in the future when the ConfigureNotify event arrives.
11449
11450 We pass 1 for DELAY since we can't run Lisp code inside of
11451 a BLOCK_INPUT. */
11452 change_frame_size (f, rows, cols, 0, 1, 0);
11453 PIXEL_WIDTH (f) = pixelwidth;
11454 PIXEL_HEIGHT (f) = pixelheight;
11455
11456 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11457 receive in the ConfigureNotify event; if we get what we asked
11458 for, then the event won't cause the screen to become garbaged, so
11459 we have to make sure to do it here. */
11460 SET_FRAME_GARBAGED (f);
11461
11462 XFlush (FRAME_X_DISPLAY (f));
11463
11464 #endif /* not USE_X_TOOLKIT */
11465
11466 /* If cursor was outside the new size, mark it as off. */
11467 mark_window_cursors_off (XWINDOW (f->root_window));
11468
11469 /* Clear out any recollection of where the mouse highlighting was,
11470 since it might be in a place that's outside the new frame size.
11471 Actually checking whether it is outside is a pain in the neck,
11472 so don't try--just let the highlighting be done afresh with new size. */
11473 cancel_mouse_face (f);
11474
11475 UNBLOCK_INPUT;
11476 }
11477 \f
11478 /* Mouse warping. */
11479
11480 void
11481 x_set_mouse_position (f, x, y)
11482 struct frame *f;
11483 int x, y;
11484 {
11485 int pix_x, pix_y;
11486
11487 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11488 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11489
11490 if (pix_x < 0) pix_x = 0;
11491 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11492
11493 if (pix_y < 0) pix_y = 0;
11494 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11495
11496 BLOCK_INPUT;
11497
11498 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11499 0, 0, 0, 0, pix_x, pix_y);
11500 UNBLOCK_INPUT;
11501 }
11502
11503 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11504
11505 void
11506 x_set_mouse_pixel_position (f, pix_x, pix_y)
11507 struct frame *f;
11508 int pix_x, pix_y;
11509 {
11510 BLOCK_INPUT;
11511
11512 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11513 0, 0, 0, 0, pix_x, pix_y);
11514 UNBLOCK_INPUT;
11515 }
11516 \f
11517 /* focus shifting, raising and lowering. */
11518
11519 void
11520 x_focus_on_frame (f)
11521 struct frame *f;
11522 {
11523 #if 0 /* This proves to be unpleasant. */
11524 x_raise_frame (f);
11525 #endif
11526 #if 0
11527 /* I don't think that the ICCCM allows programs to do things like this
11528 without the interaction of the window manager. Whatever you end up
11529 doing with this code, do it to x_unfocus_frame too. */
11530 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11531 RevertToPointerRoot, CurrentTime);
11532 #endif /* ! 0 */
11533 }
11534
11535 void
11536 x_unfocus_frame (f)
11537 struct frame *f;
11538 {
11539 #if 0
11540 /* Look at the remarks in x_focus_on_frame. */
11541 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11542 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11543 RevertToPointerRoot, CurrentTime);
11544 #endif /* ! 0 */
11545 }
11546
11547 /* Raise frame F. */
11548
11549 void
11550 x_raise_frame (f)
11551 struct frame *f;
11552 {
11553 if (f->async_visible)
11554 {
11555 BLOCK_INPUT;
11556 #ifdef USE_X_TOOLKIT
11557 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11558 #else /* not USE_X_TOOLKIT */
11559 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11560 #endif /* not USE_X_TOOLKIT */
11561 XFlush (FRAME_X_DISPLAY (f));
11562 UNBLOCK_INPUT;
11563 }
11564 }
11565
11566 /* Lower frame F. */
11567
11568 void
11569 x_lower_frame (f)
11570 struct frame *f;
11571 {
11572 if (f->async_visible)
11573 {
11574 BLOCK_INPUT;
11575 #ifdef USE_X_TOOLKIT
11576 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11577 #else /* not USE_X_TOOLKIT */
11578 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11579 #endif /* not USE_X_TOOLKIT */
11580 XFlush (FRAME_X_DISPLAY (f));
11581 UNBLOCK_INPUT;
11582 }
11583 }
11584
11585 static void
11586 XTframe_raise_lower (f, raise_flag)
11587 FRAME_PTR f;
11588 int raise_flag;
11589 {
11590 if (raise_flag)
11591 x_raise_frame (f);
11592 else
11593 x_lower_frame (f);
11594 }
11595 \f
11596 /* Change of visibility. */
11597
11598 /* This tries to wait until the frame is really visible.
11599 However, if the window manager asks the user where to position
11600 the frame, this will return before the user finishes doing that.
11601 The frame will not actually be visible at that time,
11602 but it will become visible later when the window manager
11603 finishes with it. */
11604
11605 void
11606 x_make_frame_visible (f)
11607 struct frame *f;
11608 {
11609 Lisp_Object type;
11610 int original_top, original_left;
11611
11612 BLOCK_INPUT;
11613
11614 type = x_icon_type (f);
11615 if (!NILP (type))
11616 x_bitmap_icon (f, type);
11617
11618 if (! FRAME_VISIBLE_P (f))
11619 {
11620 /* We test FRAME_GARBAGED_P here to make sure we don't
11621 call x_set_offset a second time
11622 if we get to x_make_frame_visible a second time
11623 before the window gets really visible. */
11624 if (! FRAME_ICONIFIED_P (f)
11625 && ! f->output_data.x->asked_for_visible)
11626 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11627
11628 f->output_data.x->asked_for_visible = 1;
11629
11630 if (! EQ (Vx_no_window_manager, Qt))
11631 x_wm_set_window_state (f, NormalState);
11632 #ifdef USE_X_TOOLKIT
11633 /* This was XtPopup, but that did nothing for an iconified frame. */
11634 XtMapWidget (f->output_data.x->widget);
11635 #else /* not USE_X_TOOLKIT */
11636 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11637 #endif /* not USE_X_TOOLKIT */
11638 #if 0 /* This seems to bring back scroll bars in the wrong places
11639 if the window configuration has changed. They seem
11640 to come back ok without this. */
11641 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11642 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11643 #endif
11644 }
11645
11646 XFlush (FRAME_X_DISPLAY (f));
11647
11648 /* Synchronize to ensure Emacs knows the frame is visible
11649 before we do anything else. We do this loop with input not blocked
11650 so that incoming events are handled. */
11651 {
11652 Lisp_Object frame;
11653 int count;
11654 /* This must be before UNBLOCK_INPUT
11655 since events that arrive in response to the actions above
11656 will set it when they are handled. */
11657 int previously_visible = f->output_data.x->has_been_visible;
11658
11659 original_left = f->output_data.x->left_pos;
11660 original_top = f->output_data.x->top_pos;
11661
11662 /* This must come after we set COUNT. */
11663 UNBLOCK_INPUT;
11664
11665 /* We unblock here so that arriving X events are processed. */
11666
11667 /* Now move the window back to where it was "supposed to be".
11668 But don't do it if the gravity is negative.
11669 When the gravity is negative, this uses a position
11670 that is 3 pixels too low. Perhaps that's really the border width.
11671
11672 Don't do this if the window has never been visible before,
11673 because the window manager may choose the position
11674 and we don't want to override it. */
11675
11676 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11677 && f->output_data.x->win_gravity == NorthWestGravity
11678 && previously_visible)
11679 {
11680 Drawable rootw;
11681 int x, y;
11682 unsigned int width, height, border, depth;
11683
11684 BLOCK_INPUT;
11685
11686 /* On some window managers (such as FVWM) moving an existing
11687 window, even to the same place, causes the window manager
11688 to introduce an offset. This can cause the window to move
11689 to an unexpected location. Check the geometry (a little
11690 slow here) and then verify that the window is in the right
11691 place. If the window is not in the right place, move it
11692 there, and take the potential window manager hit. */
11693 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11694 &rootw, &x, &y, &width, &height, &border, &depth);
11695
11696 if (original_left != x || original_top != y)
11697 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11698 original_left, original_top);
11699
11700 UNBLOCK_INPUT;
11701 }
11702
11703 XSETFRAME (frame, f);
11704
11705 /* Wait until the frame is visible. Process X events until a
11706 MapNotify event has been seen, or until we think we won't get a
11707 MapNotify at all.. */
11708 for (count = input_signal_count + 10;
11709 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11710 {
11711 /* Force processing of queued events. */
11712 x_sync (f);
11713
11714 /* Machines that do polling rather than SIGIO have been
11715 observed to go into a busy-wait here. So we'll fake an
11716 alarm signal to let the handler know that there's something
11717 to be read. We used to raise a real alarm, but it seems
11718 that the handler isn't always enabled here. This is
11719 probably a bug. */
11720 if (input_polling_used ())
11721 {
11722 /* It could be confusing if a real alarm arrives while
11723 processing the fake one. Turn it off and let the
11724 handler reset it. */
11725 extern void poll_for_input_1 P_ ((void));
11726 int old_poll_suppress_count = poll_suppress_count;
11727 poll_suppress_count = 1;
11728 poll_for_input_1 ();
11729 poll_suppress_count = old_poll_suppress_count;
11730 }
11731
11732 /* See if a MapNotify event has been processed. */
11733 FRAME_SAMPLE_VISIBILITY (f);
11734 }
11735 }
11736 }
11737
11738 /* Change from mapped state to withdrawn state. */
11739
11740 /* Make the frame visible (mapped and not iconified). */
11741
11742 void
11743 x_make_frame_invisible (f)
11744 struct frame *f;
11745 {
11746 Window window;
11747
11748 #ifdef USE_X_TOOLKIT
11749 /* Use the frame's outermost window, not the one we normally draw on. */
11750 window = XtWindow (f->output_data.x->widget);
11751 #else /* not USE_X_TOOLKIT */
11752 window = FRAME_X_WINDOW (f);
11753 #endif /* not USE_X_TOOLKIT */
11754
11755 /* Don't keep the highlight on an invisible frame. */
11756 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11757 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11758
11759 #if 0/* This might add unreliability; I don't trust it -- rms. */
11760 if (! f->async_visible && ! f->async_iconified)
11761 return;
11762 #endif
11763
11764 BLOCK_INPUT;
11765
11766 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11767 that the current position of the window is user-specified, rather than
11768 program-specified, so that when the window is mapped again, it will be
11769 placed at the same location, without forcing the user to position it
11770 by hand again (they have already done that once for this window.) */
11771 x_wm_set_size_hint (f, (long) 0, 1);
11772
11773 #ifdef HAVE_X11R4
11774
11775 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11776 DefaultScreen (FRAME_X_DISPLAY (f))))
11777 {
11778 UNBLOCK_INPUT_RESIGNAL;
11779 error ("Can't notify window manager of window withdrawal");
11780 }
11781 #else /* ! defined (HAVE_X11R4) */
11782
11783 /* Tell the window manager what we're going to do. */
11784 if (! EQ (Vx_no_window_manager, Qt))
11785 {
11786 XEvent unmap;
11787
11788 unmap.xunmap.type = UnmapNotify;
11789 unmap.xunmap.window = window;
11790 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
11791 unmap.xunmap.from_configure = False;
11792 if (! XSendEvent (FRAME_X_DISPLAY (f),
11793 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11794 False,
11795 SubstructureRedirectMaskSubstructureNotifyMask,
11796 &unmap))
11797 {
11798 UNBLOCK_INPUT_RESIGNAL;
11799 error ("Can't notify window manager of withdrawal");
11800 }
11801 }
11802
11803 /* Unmap the window ourselves. Cheeky! */
11804 XUnmapWindow (FRAME_X_DISPLAY (f), window);
11805 #endif /* ! defined (HAVE_X11R4) */
11806
11807 /* We can't distinguish this from iconification
11808 just by the event that we get from the server.
11809 So we can't win using the usual strategy of letting
11810 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11811 and synchronize with the server to make sure we agree. */
11812 f->visible = 0;
11813 FRAME_ICONIFIED_P (f) = 0;
11814 f->async_visible = 0;
11815 f->async_iconified = 0;
11816
11817 x_sync (f);
11818
11819 UNBLOCK_INPUT;
11820 }
11821
11822 /* Change window state from mapped to iconified. */
11823
11824 void
11825 x_iconify_frame (f)
11826 struct frame *f;
11827 {
11828 int result;
11829 Lisp_Object type;
11830
11831 /* Don't keep the highlight on an invisible frame. */
11832 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11833 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11834
11835 if (f->async_iconified)
11836 return;
11837
11838 BLOCK_INPUT;
11839
11840 FRAME_SAMPLE_VISIBILITY (f);
11841
11842 type = x_icon_type (f);
11843 if (!NILP (type))
11844 x_bitmap_icon (f, type);
11845
11846 #ifdef USE_X_TOOLKIT
11847
11848 if (! FRAME_VISIBLE_P (f))
11849 {
11850 if (! EQ (Vx_no_window_manager, Qt))
11851 x_wm_set_window_state (f, IconicState);
11852 /* This was XtPopup, but that did nothing for an iconified frame. */
11853 XtMapWidget (f->output_data.x->widget);
11854 /* The server won't give us any event to indicate
11855 that an invisible frame was changed to an icon,
11856 so we have to record it here. */
11857 f->iconified = 1;
11858 f->visible = 1;
11859 f->async_iconified = 1;
11860 f->async_visible = 0;
11861 UNBLOCK_INPUT;
11862 return;
11863 }
11864
11865 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11866 XtWindow (f->output_data.x->widget),
11867 DefaultScreen (FRAME_X_DISPLAY (f)));
11868 UNBLOCK_INPUT;
11869
11870 if (!result)
11871 error ("Can't notify window manager of iconification");
11872
11873 f->async_iconified = 1;
11874 f->async_visible = 0;
11875
11876
11877 BLOCK_INPUT;
11878 XFlush (FRAME_X_DISPLAY (f));
11879 UNBLOCK_INPUT;
11880 #else /* not USE_X_TOOLKIT */
11881
11882 /* Make sure the X server knows where the window should be positioned,
11883 in case the user deiconifies with the window manager. */
11884 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
11885 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11886
11887 /* Since we don't know which revision of X we're running, we'll use both
11888 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11889
11890 /* X11R4: send a ClientMessage to the window manager using the
11891 WM_CHANGE_STATE type. */
11892 {
11893 XEvent message;
11894
11895 message.xclient.window = FRAME_X_WINDOW (f);
11896 message.xclient.type = ClientMessage;
11897 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
11898 message.xclient.format = 32;
11899 message.xclient.data.l[0] = IconicState;
11900
11901 if (! XSendEvent (FRAME_X_DISPLAY (f),
11902 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11903 False,
11904 SubstructureRedirectMask | SubstructureNotifyMask,
11905 &message))
11906 {
11907 UNBLOCK_INPUT_RESIGNAL;
11908 error ("Can't notify window manager of iconification");
11909 }
11910 }
11911
11912 /* X11R3: set the initial_state field of the window manager hints to
11913 IconicState. */
11914 x_wm_set_window_state (f, IconicState);
11915
11916 if (!FRAME_VISIBLE_P (f))
11917 {
11918 /* If the frame was withdrawn, before, we must map it. */
11919 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11920 }
11921
11922 f->async_iconified = 1;
11923 f->async_visible = 0;
11924
11925 XFlush (FRAME_X_DISPLAY (f));
11926 UNBLOCK_INPUT;
11927 #endif /* not USE_X_TOOLKIT */
11928 }
11929 \f
11930 /* Destroy the X window of frame F. */
11931
11932 void
11933 x_destroy_window (f)
11934 struct frame *f;
11935 {
11936 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11937
11938 BLOCK_INPUT;
11939
11940 /* If a display connection is dead, don't try sending more
11941 commands to the X server. */
11942 if (dpyinfo->display != 0)
11943 {
11944 if (f->output_data.x->icon_desc != 0)
11945 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11946 #ifdef HAVE_X_I18N
11947 if (FRAME_XIC (f))
11948 free_frame_xic (f);
11949 #endif
11950 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
11951 #ifdef USE_X_TOOLKIT
11952 if (f->output_data.x->widget)
11953 XtDestroyWidget (f->output_data.x->widget);
11954 free_frame_menubar (f);
11955 #endif /* USE_X_TOOLKIT */
11956
11957 unload_color (f, f->output_data.x->foreground_pixel);
11958 unload_color (f, f->output_data.x->background_pixel);
11959 unload_color (f, f->output_data.x->cursor_pixel);
11960 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11961 unload_color (f, f->output_data.x->border_pixel);
11962 unload_color (f, f->output_data.x->mouse_pixel);
11963 if (f->output_data.x->scroll_bar_background_pixel != -1)
11964 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11965 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11966 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11967 if (f->output_data.x->white_relief.allocated_p)
11968 unload_color (f, f->output_data.x->white_relief.pixel);
11969 if (f->output_data.x->black_relief.allocated_p)
11970 unload_color (f, f->output_data.x->black_relief.pixel);
11971
11972 free_frame_faces (f);
11973 XFlush (FRAME_X_DISPLAY (f));
11974 }
11975
11976 if (f->output_data.x->saved_menu_event)
11977 xfree (f->output_data.x->saved_menu_event);
11978
11979 xfree (f->output_data.x);
11980 f->output_data.x = 0;
11981 if (f == dpyinfo->x_focus_frame)
11982 dpyinfo->x_focus_frame = 0;
11983 if (f == dpyinfo->x_focus_event_frame)
11984 dpyinfo->x_focus_event_frame = 0;
11985 if (f == dpyinfo->x_highlight_frame)
11986 dpyinfo->x_highlight_frame = 0;
11987
11988 dpyinfo->reference_count--;
11989
11990 if (f == dpyinfo->mouse_face_mouse_frame)
11991 {
11992 dpyinfo->mouse_face_beg_row
11993 = dpyinfo->mouse_face_beg_col = -1;
11994 dpyinfo->mouse_face_end_row
11995 = dpyinfo->mouse_face_end_col = -1;
11996 dpyinfo->mouse_face_window = Qnil;
11997 dpyinfo->mouse_face_deferred_gc = 0;
11998 dpyinfo->mouse_face_mouse_frame = 0;
11999 }
12000
12001 UNBLOCK_INPUT;
12002 }
12003 \f
12004 /* Setting window manager hints. */
12005
12006 /* Set the normal size hints for the window manager, for frame F.
12007 FLAGS is the flags word to use--or 0 meaning preserve the flags
12008 that the window now has.
12009 If USER_POSITION is nonzero, we set the USPosition
12010 flag (this is useful when FLAGS is 0). */
12011
12012 void
12013 x_wm_set_size_hint (f, flags, user_position)
12014 struct frame *f;
12015 long flags;
12016 int user_position;
12017 {
12018 XSizeHints size_hints;
12019
12020 #ifdef USE_X_TOOLKIT
12021 Arg al[2];
12022 int ac = 0;
12023 Dimension widget_width, widget_height;
12024 Window window = XtWindow (f->output_data.x->widget);
12025 #else /* not USE_X_TOOLKIT */
12026 Window window = FRAME_X_WINDOW (f);
12027 #endif /* not USE_X_TOOLKIT */
12028
12029 /* Setting PMaxSize caused various problems. */
12030 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12031
12032 size_hints.x = f->output_data.x->left_pos;
12033 size_hints.y = f->output_data.x->top_pos;
12034
12035 #ifdef USE_X_TOOLKIT
12036 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12037 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12038 XtGetValues (f->output_data.x->widget, al, ac);
12039 size_hints.height = widget_height;
12040 size_hints.width = widget_width;
12041 #else /* not USE_X_TOOLKIT */
12042 size_hints.height = PIXEL_HEIGHT (f);
12043 size_hints.width = PIXEL_WIDTH (f);
12044 #endif /* not USE_X_TOOLKIT */
12045
12046 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12047 size_hints.height_inc = f->output_data.x->line_height;
12048 size_hints.max_width
12049 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12050 size_hints.max_height
12051 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12052
12053 /* Calculate the base and minimum sizes.
12054
12055 (When we use the X toolkit, we don't do it here.
12056 Instead we copy the values that the widgets are using, below.) */
12057 #ifndef USE_X_TOOLKIT
12058 {
12059 int base_width, base_height;
12060 int min_rows = 0, min_cols = 0;
12061
12062 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12063 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12064
12065 check_frame_size (f, &min_rows, &min_cols);
12066
12067 /* The window manager uses the base width hints to calculate the
12068 current number of rows and columns in the frame while
12069 resizing; min_width and min_height aren't useful for this
12070 purpose, since they might not give the dimensions for a
12071 zero-row, zero-column frame.
12072
12073 We use the base_width and base_height members if we have
12074 them; otherwise, we set the min_width and min_height members
12075 to the size for a zero x zero frame. */
12076
12077 #ifdef HAVE_X11R4
12078 size_hints.flags |= PBaseSize;
12079 size_hints.base_width = base_width;
12080 size_hints.base_height = base_height;
12081 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12082 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12083 #else
12084 size_hints.min_width = base_width;
12085 size_hints.min_height = base_height;
12086 #endif
12087 }
12088
12089 /* If we don't need the old flags, we don't need the old hint at all. */
12090 if (flags)
12091 {
12092 size_hints.flags |= flags;
12093 goto no_read;
12094 }
12095 #endif /* not USE_X_TOOLKIT */
12096
12097 {
12098 XSizeHints hints; /* Sometimes I hate X Windows... */
12099 long supplied_return;
12100 int value;
12101
12102 #ifdef HAVE_X11R4
12103 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12104 &supplied_return);
12105 #else
12106 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12107 #endif
12108
12109 #ifdef USE_X_TOOLKIT
12110 size_hints.base_height = hints.base_height;
12111 size_hints.base_width = hints.base_width;
12112 size_hints.min_height = hints.min_height;
12113 size_hints.min_width = hints.min_width;
12114 #endif
12115
12116 if (flags)
12117 size_hints.flags |= flags;
12118 else
12119 {
12120 if (value == 0)
12121 hints.flags = 0;
12122 if (hints.flags & PSize)
12123 size_hints.flags |= PSize;
12124 if (hints.flags & PPosition)
12125 size_hints.flags |= PPosition;
12126 if (hints.flags & USPosition)
12127 size_hints.flags |= USPosition;
12128 if (hints.flags & USSize)
12129 size_hints.flags |= USSize;
12130 }
12131 }
12132
12133 #ifndef USE_X_TOOLKIT
12134 no_read:
12135 #endif
12136
12137 #ifdef PWinGravity
12138 size_hints.win_gravity = f->output_data.x->win_gravity;
12139 size_hints.flags |= PWinGravity;
12140
12141 if (user_position)
12142 {
12143 size_hints.flags &= ~ PPosition;
12144 size_hints.flags |= USPosition;
12145 }
12146 #endif /* PWinGravity */
12147
12148 #ifdef HAVE_X11R4
12149 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12150 #else
12151 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12152 #endif
12153 }
12154
12155 /* Used for IconicState or NormalState */
12156
12157 void
12158 x_wm_set_window_state (f, state)
12159 struct frame *f;
12160 int state;
12161 {
12162 #ifdef USE_X_TOOLKIT
12163 Arg al[1];
12164
12165 XtSetArg (al[0], XtNinitialState, state);
12166 XtSetValues (f->output_data.x->widget, al, 1);
12167 #else /* not USE_X_TOOLKIT */
12168 Window window = FRAME_X_WINDOW (f);
12169
12170 f->output_data.x->wm_hints.flags |= StateHint;
12171 f->output_data.x->wm_hints.initial_state = state;
12172
12173 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12174 #endif /* not USE_X_TOOLKIT */
12175 }
12176
12177 void
12178 x_wm_set_icon_pixmap (f, pixmap_id)
12179 struct frame *f;
12180 int pixmap_id;
12181 {
12182 Pixmap icon_pixmap;
12183
12184 #ifndef USE_X_TOOLKIT
12185 Window window = FRAME_X_WINDOW (f);
12186 #endif
12187
12188 if (pixmap_id > 0)
12189 {
12190 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12191 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12192 }
12193 else
12194 {
12195 /* It seems there is no way to turn off use of an icon pixmap.
12196 The following line does it, only if no icon has yet been created,
12197 for some window managers. But with mwm it crashes.
12198 Some people say it should clear the IconPixmapHint bit in this case,
12199 but that doesn't work, and the X consortium said it isn't the
12200 right thing at all. Since there is no way to win,
12201 best to explicitly give up. */
12202 #if 0
12203 f->output_data.x->wm_hints.icon_pixmap = None;
12204 #else
12205 return;
12206 #endif
12207 }
12208
12209 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12210
12211 {
12212 Arg al[1];
12213 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12214 XtSetValues (f->output_data.x->widget, al, 1);
12215 }
12216
12217 #else /* not USE_X_TOOLKIT */
12218
12219 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12220 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12221
12222 #endif /* not USE_X_TOOLKIT */
12223 }
12224
12225 void
12226 x_wm_set_icon_position (f, icon_x, icon_y)
12227 struct frame *f;
12228 int icon_x, icon_y;
12229 {
12230 #ifdef USE_X_TOOLKIT
12231 Window window = XtWindow (f->output_data.x->widget);
12232 #else
12233 Window window = FRAME_X_WINDOW (f);
12234 #endif
12235
12236 f->output_data.x->wm_hints.flags |= IconPositionHint;
12237 f->output_data.x->wm_hints.icon_x = icon_x;
12238 f->output_data.x->wm_hints.icon_y = icon_y;
12239
12240 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12241 }
12242
12243 \f
12244 /***********************************************************************
12245 Fonts
12246 ***********************************************************************/
12247
12248 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12249
12250 struct font_info *
12251 x_get_font_info (f, font_idx)
12252 FRAME_PTR f;
12253 int font_idx;
12254 {
12255 return (FRAME_X_FONT_TABLE (f) + font_idx);
12256 }
12257
12258
12259 /* Return a list of names of available fonts matching PATTERN on frame
12260 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12261 to be listed. Frame F NULL means we have not yet created any
12262 frame on X, and consult the first display in x_display_list.
12263 MAXNAMES sets a limit on how many fonts to match. */
12264
12265 Lisp_Object
12266 x_list_fonts (f, pattern, size, maxnames)
12267 FRAME_PTR f;
12268 Lisp_Object pattern;
12269 int size;
12270 int maxnames;
12271 {
12272 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12273 Lisp_Object tem, second_best;
12274 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12275 int try_XLoadQueryFont = 0;
12276 int count;
12277
12278 patterns = Fassoc (pattern, Valternate_fontname_alist);
12279 if (NILP (patterns))
12280 patterns = Fcons (pattern, Qnil);
12281
12282 if (maxnames == 1 && !size)
12283 /* We can return any single font matching PATTERN. */
12284 try_XLoadQueryFont = 1;
12285
12286 for (; CONSP (patterns); patterns = XCDR (patterns))
12287 {
12288 int num_fonts;
12289 char **names = NULL;
12290
12291 pattern = XCAR (patterns);
12292 /* See if we cached the result for this particular query.
12293 The cache is an alist of the form:
12294 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12295 */
12296 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12297 key = Fcons (pattern, make_number (maxnames)),
12298 !NILP (list = Fassoc (key, tem))))
12299 {
12300 list = Fcdr_safe (list);
12301 /* We have a cashed list. Don't have to get the list again. */
12302 goto label_cached;
12303 }
12304
12305 /* At first, put PATTERN in the cache. */
12306
12307 BLOCK_INPUT;
12308 count = x_catch_errors (dpy);
12309
12310 if (try_XLoadQueryFont)
12311 {
12312 XFontStruct *font;
12313 unsigned long value;
12314
12315 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12316 if (x_had_errors_p (dpy))
12317 {
12318 /* This error is perhaps due to insufficient memory on X
12319 server. Let's just ignore it. */
12320 font = NULL;
12321 x_clear_errors (dpy);
12322 }
12323
12324 if (font
12325 && XGetFontProperty (font, XA_FONT, &value))
12326 {
12327 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12328 int len = strlen (name);
12329 char *tmp;
12330
12331 /* If DXPC (a Differential X Protocol Compressor)
12332 Ver.3.7 is running, XGetAtomName will return null
12333 string. We must avoid such a name. */
12334 if (len == 0)
12335 try_XLoadQueryFont = 0;
12336 else
12337 {
12338 num_fonts = 1;
12339 names = (char **) alloca (sizeof (char *));
12340 /* Some systems only allow alloca assigned to a
12341 simple var. */
12342 tmp = (char *) alloca (len + 1); names[0] = tmp;
12343 bcopy (name, names[0], len + 1);
12344 XFree (name);
12345 }
12346 }
12347 else
12348 try_XLoadQueryFont = 0;
12349
12350 if (font)
12351 XFreeFont (dpy, font);
12352 }
12353
12354 if (!try_XLoadQueryFont)
12355 {
12356 /* We try at least 10 fonts because XListFonts will return
12357 auto-scaled fonts at the head. */
12358 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12359 &num_fonts);
12360 if (x_had_errors_p (dpy))
12361 {
12362 /* This error is perhaps due to insufficient memory on X
12363 server. Let's just ignore it. */
12364 names = NULL;
12365 x_clear_errors (dpy);
12366 }
12367 }
12368
12369 x_uncatch_errors (dpy, count);
12370 UNBLOCK_INPUT;
12371
12372 if (names)
12373 {
12374 int i;
12375
12376 /* Make a list of all the fonts we got back.
12377 Store that in the font cache for the display. */
12378 for (i = 0; i < num_fonts; i++)
12379 {
12380 int width = 0;
12381 char *p = names[i];
12382 int average_width = -1, dashes = 0;
12383
12384 /* Count the number of dashes in NAMES[I]. If there are
12385 14 dashes, and the field value following 12th dash
12386 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12387 is usually too ugly to be used for editing. Let's
12388 ignore it. */
12389 while (*p)
12390 if (*p++ == '-')
12391 {
12392 dashes++;
12393 if (dashes == 7) /* PIXEL_SIZE field */
12394 width = atoi (p);
12395 else if (dashes == 12) /* AVERAGE_WIDTH field */
12396 average_width = atoi (p);
12397 }
12398 if (dashes < 14 || average_width != 0)
12399 {
12400 tem = build_string (names[i]);
12401 if (NILP (Fassoc (tem, list)))
12402 {
12403 if (STRINGP (Vx_pixel_size_width_font_regexp)
12404 && ((fast_c_string_match_ignore_case
12405 (Vx_pixel_size_width_font_regexp, names[i]))
12406 >= 0))
12407 /* We can set the value of PIXEL_SIZE to the
12408 width of this font. */
12409 list = Fcons (Fcons (tem, make_number (width)), list);
12410 else
12411 /* For the moment, width is not known. */
12412 list = Fcons (Fcons (tem, Qnil), list);
12413 }
12414 }
12415 }
12416 if (!try_XLoadQueryFont)
12417 XFreeFontNames (names);
12418 }
12419
12420 /* Now store the result in the cache. */
12421 if (f != NULL)
12422 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12423 = Fcons (Fcons (key, list),
12424 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12425
12426 label_cached:
12427 if (NILP (list)) continue; /* Try the remaining alternatives. */
12428
12429 newlist = second_best = Qnil;
12430 /* Make a list of the fonts that have the right width. */
12431 for (; CONSP (list); list = XCDR (list))
12432 {
12433 int found_size;
12434
12435 tem = XCAR (list);
12436
12437 if (!CONSP (tem) || NILP (XCAR (tem)))
12438 continue;
12439 if (!size)
12440 {
12441 newlist = Fcons (XCAR (tem), newlist);
12442 continue;
12443 }
12444
12445 if (!INTEGERP (XCDR (tem)))
12446 {
12447 /* Since we have not yet known the size of this font, we
12448 must try slow function call XLoadQueryFont. */
12449 XFontStruct *thisinfo;
12450
12451 BLOCK_INPUT;
12452 count = x_catch_errors (dpy);
12453 thisinfo = XLoadQueryFont (dpy,
12454 XSTRING (XCAR (tem))->data);
12455 if (x_had_errors_p (dpy))
12456 {
12457 /* This error is perhaps due to insufficient memory on X
12458 server. Let's just ignore it. */
12459 thisinfo = NULL;
12460 x_clear_errors (dpy);
12461 }
12462 x_uncatch_errors (dpy, count);
12463 UNBLOCK_INPUT;
12464
12465 if (thisinfo)
12466 {
12467 XCDR (tem)
12468 = (thisinfo->min_bounds.width == 0
12469 ? make_number (0)
12470 : make_number (thisinfo->max_bounds.width));
12471 XFreeFont (dpy, thisinfo);
12472 }
12473 else
12474 /* For unknown reason, the previous call of XListFont had
12475 returned a font which can't be opened. Record the size
12476 as 0 not to try to open it again. */
12477 XCDR (tem) = make_number (0);
12478 }
12479
12480 found_size = XINT (XCDR (tem));
12481 if (found_size == size)
12482 newlist = Fcons (XCAR (tem), newlist);
12483 else if (found_size > 0)
12484 {
12485 if (NILP (second_best))
12486 second_best = tem;
12487 else if (found_size < size)
12488 {
12489 if (XINT (XCDR (second_best)) > size
12490 || XINT (XCDR (second_best)) < found_size)
12491 second_best = tem;
12492 }
12493 else
12494 {
12495 if (XINT (XCDR (second_best)) > size
12496 && XINT (XCDR (second_best)) > found_size)
12497 second_best = tem;
12498 }
12499 }
12500 }
12501 if (!NILP (newlist))
12502 break;
12503 else if (!NILP (second_best))
12504 {
12505 newlist = Fcons (XCAR (second_best), Qnil);
12506 break;
12507 }
12508 }
12509
12510 return newlist;
12511 }
12512
12513
12514 #if GLYPH_DEBUG
12515
12516 /* Check that FONT is valid on frame F. It is if it can be found in F's
12517 font table. */
12518
12519 static void
12520 x_check_font (f, font)
12521 struct frame *f;
12522 XFontStruct *font;
12523 {
12524 int i;
12525 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12526
12527 xassert (font != NULL);
12528
12529 for (i = 0; i < dpyinfo->n_fonts; i++)
12530 if (dpyinfo->font_table[i].name
12531 && font == dpyinfo->font_table[i].font)
12532 break;
12533
12534 xassert (i < dpyinfo->n_fonts);
12535 }
12536
12537 #endif /* GLYPH_DEBUG != 0 */
12538
12539 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12540 Note: There are (broken) X fonts out there with invalid XFontStruct
12541 min_bounds contents. For example, handa@etl.go.jp reports that
12542 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12543 have font->min_bounds.width == 0. */
12544
12545 static INLINE void
12546 x_font_min_bounds (font, w, h)
12547 XFontStruct *font;
12548 int *w, *h;
12549 {
12550 *h = FONT_HEIGHT (font);
12551 *w = font->min_bounds.width;
12552
12553 /* Try to handle the case where FONT->min_bounds has invalid
12554 contents. Since the only font known to have invalid min_bounds
12555 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12556 if (*w <= 0)
12557 *w = font->max_bounds.width;
12558 }
12559
12560
12561 /* Compute the smallest character width and smallest font height over
12562 all fonts available on frame F. Set the members smallest_char_width
12563 and smallest_font_height in F's x_display_info structure to
12564 the values computed. Value is non-zero if smallest_font_height or
12565 smallest_char_width become smaller than they were before. */
12566
12567 static int
12568 x_compute_min_glyph_bounds (f)
12569 struct frame *f;
12570 {
12571 int i;
12572 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12573 XFontStruct *font;
12574 int old_width = dpyinfo->smallest_char_width;
12575 int old_height = dpyinfo->smallest_font_height;
12576
12577 dpyinfo->smallest_font_height = 100000;
12578 dpyinfo->smallest_char_width = 100000;
12579
12580 for (i = 0; i < dpyinfo->n_fonts; ++i)
12581 if (dpyinfo->font_table[i].name)
12582 {
12583 struct font_info *fontp = dpyinfo->font_table + i;
12584 int w, h;
12585
12586 font = (XFontStruct *) fontp->font;
12587 xassert (font != (XFontStruct *) ~0);
12588 x_font_min_bounds (font, &w, &h);
12589
12590 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12591 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12592 }
12593
12594 xassert (dpyinfo->smallest_char_width > 0
12595 && dpyinfo->smallest_font_height > 0);
12596
12597 return (dpyinfo->n_fonts == 1
12598 || dpyinfo->smallest_char_width < old_width
12599 || dpyinfo->smallest_font_height < old_height);
12600 }
12601
12602
12603 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12604 pointer to the structure font_info while allocating it dynamically.
12605 If SIZE is 0, load any size of font.
12606 If loading is failed, return NULL. */
12607
12608 struct font_info *
12609 x_load_font (f, fontname, size)
12610 struct frame *f;
12611 register char *fontname;
12612 int size;
12613 {
12614 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12615 Lisp_Object font_names;
12616 int count;
12617
12618 /* Get a list of all the fonts that match this name. Once we
12619 have a list of matching fonts, we compare them against the fonts
12620 we already have by comparing names. */
12621 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12622
12623 if (!NILP (font_names))
12624 {
12625 Lisp_Object tail;
12626 int i;
12627
12628 for (i = 0; i < dpyinfo->n_fonts; i++)
12629 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12630 if (dpyinfo->font_table[i].name
12631 && (!strcmp (dpyinfo->font_table[i].name,
12632 XSTRING (XCAR (tail))->data)
12633 || !strcmp (dpyinfo->font_table[i].full_name,
12634 XSTRING (XCAR (tail))->data)))
12635 return (dpyinfo->font_table + i);
12636 }
12637
12638 /* Load the font and add it to the table. */
12639 {
12640 char *full_name;
12641 XFontStruct *font;
12642 struct font_info *fontp;
12643 unsigned long value;
12644 int i;
12645
12646 /* If we have found fonts by x_list_font, load one of them. If
12647 not, we still try to load a font by the name given as FONTNAME
12648 because XListFonts (called in x_list_font) of some X server has
12649 a bug of not finding a font even if the font surely exists and
12650 is loadable by XLoadQueryFont. */
12651 if (size > 0 && !NILP (font_names))
12652 fontname = (char *) XSTRING (XCAR (font_names))->data;
12653
12654 BLOCK_INPUT;
12655 count = x_catch_errors (FRAME_X_DISPLAY (f));
12656 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12657 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12658 {
12659 /* This error is perhaps due to insufficient memory on X
12660 server. Let's just ignore it. */
12661 font = NULL;
12662 x_clear_errors (FRAME_X_DISPLAY (f));
12663 }
12664 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12665 UNBLOCK_INPUT;
12666 if (!font)
12667 return NULL;
12668
12669 /* Find a free slot in the font table. */
12670 for (i = 0; i < dpyinfo->n_fonts; ++i)
12671 if (dpyinfo->font_table[i].name == NULL)
12672 break;
12673
12674 /* If no free slot found, maybe enlarge the font table. */
12675 if (i == dpyinfo->n_fonts
12676 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12677 {
12678 int sz;
12679 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12680 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12681 dpyinfo->font_table
12682 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12683 }
12684
12685 fontp = dpyinfo->font_table + i;
12686 if (i == dpyinfo->n_fonts)
12687 ++dpyinfo->n_fonts;
12688
12689 /* Now fill in the slots of *FONTP. */
12690 BLOCK_INPUT;
12691 fontp->font = font;
12692 fontp->font_idx = i;
12693 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12694 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12695
12696 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12697 full_name = 0;
12698 if (XGetFontProperty (font, XA_FONT, &value))
12699 {
12700 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12701 char *p = name;
12702 int dashes = 0;
12703
12704 /* Count the number of dashes in the "full name".
12705 If it is too few, this isn't really the font's full name,
12706 so don't use it.
12707 In X11R4, the fonts did not come with their canonical names
12708 stored in them. */
12709 while (*p)
12710 {
12711 if (*p == '-')
12712 dashes++;
12713 p++;
12714 }
12715
12716 if (dashes >= 13)
12717 {
12718 full_name = (char *) xmalloc (p - name + 1);
12719 bcopy (name, full_name, p - name + 1);
12720 }
12721
12722 XFree (name);
12723 }
12724
12725 if (full_name != 0)
12726 fontp->full_name = full_name;
12727 else
12728 fontp->full_name = fontp->name;
12729
12730 fontp->size = font->max_bounds.width;
12731 fontp->height = FONT_HEIGHT (font);
12732 {
12733 /* For some font, ascent and descent in max_bounds field is
12734 larger than the above value. */
12735 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12736 if (max_height > fontp->height)
12737 fontp->height = max_height;
12738 }
12739
12740 if (NILP (font_names))
12741 {
12742 /* We come here because of a bug of XListFonts mentioned at
12743 the head of this block. Let's store this information in
12744 the cache for x_list_fonts. */
12745 Lisp_Object lispy_name = build_string (fontname);
12746 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12747
12748 XCDR (dpyinfo->name_list_element)
12749 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12750 Fcons (Fcons (lispy_full_name,
12751 make_number (fontp->size)),
12752 Qnil)),
12753 XCDR (dpyinfo->name_list_element));
12754 if (full_name)
12755 XCDR (dpyinfo->name_list_element)
12756 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12757 Fcons (Fcons (lispy_full_name,
12758 make_number (fontp->size)),
12759 Qnil)),
12760 XCDR (dpyinfo->name_list_element));
12761 }
12762
12763 /* The slot `encoding' specifies how to map a character
12764 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12765 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12766 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
12767 2:0xA020..0xFF7F). For the moment, we don't know which charset
12768 uses this font. So, we set information in fontp->encoding[1]
12769 which is never used by any charset. If mapping can't be
12770 decided, set FONT_ENCODING_NOT_DECIDED. */
12771 fontp->encoding[1]
12772 = (font->max_byte1 == 0
12773 /* 1-byte font */
12774 ? (font->min_char_or_byte2 < 0x80
12775 ? (font->max_char_or_byte2 < 0x80
12776 ? 0 /* 0x20..0x7F */
12777 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12778 : 1) /* 0xA0..0xFF */
12779 /* 2-byte font */
12780 : (font->min_byte1 < 0x80
12781 ? (font->max_byte1 < 0x80
12782 ? (font->min_char_or_byte2 < 0x80
12783 ? (font->max_char_or_byte2 < 0x80
12784 ? 0 /* 0x2020..0x7F7F */
12785 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
12786 : 3) /* 0x20A0..0x7FFF */
12787 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
12788 : (font->min_char_or_byte2 < 0x80
12789 ? (font->max_char_or_byte2 < 0x80
12790 ? 2 /* 0xA020..0xFF7F */
12791 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
12792 : 1))); /* 0xA0A0..0xFFFF */
12793
12794 fontp->baseline_offset
12795 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12796 ? (long) value : 0);
12797 fontp->relative_compose
12798 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12799 ? (long) value : 0);
12800 fontp->default_ascent
12801 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12802 ? (long) value : 0);
12803
12804 /* Set global flag fonts_changed_p to non-zero if the font loaded
12805 has a character with a smaller width than any other character
12806 before, or if the font loaded has a smalle>r height than any
12807 other font loaded before. If this happens, it will make a
12808 glyph matrix reallocation necessary. */
12809 fonts_changed_p = x_compute_min_glyph_bounds (f);
12810 UNBLOCK_INPUT;
12811 return fontp;
12812 }
12813 }
12814
12815
12816 /* Return a pointer to struct font_info of a font named FONTNAME for
12817 frame F. If no such font is loaded, return NULL. */
12818
12819 struct font_info *
12820 x_query_font (f, fontname)
12821 struct frame *f;
12822 register char *fontname;
12823 {
12824 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12825 int i;
12826
12827 for (i = 0; i < dpyinfo->n_fonts; i++)
12828 if (dpyinfo->font_table[i].name
12829 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12830 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
12831 return (dpyinfo->font_table + i);
12832 return NULL;
12833 }
12834
12835
12836 /* Find a CCL program for a font specified by FONTP, and set the member
12837 `encoder' of the structure. */
12838
12839 void
12840 x_find_ccl_program (fontp)
12841 struct font_info *fontp;
12842 {
12843 Lisp_Object list, elt;
12844
12845 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
12846 {
12847 elt = XCAR (list);
12848 if (CONSP (elt)
12849 && STRINGP (XCAR (elt))
12850 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
12851 >= 0))
12852 break;
12853 }
12854 if (! NILP (list))
12855 {
12856 struct ccl_program *ccl
12857 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
12858
12859 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
12860 xfree (ccl);
12861 else
12862 fontp->font_encoder = ccl;
12863 }
12864 }
12865
12866
12867 \f
12868 /***********************************************************************
12869 Initialization
12870 ***********************************************************************/
12871
12872 #ifdef USE_X_TOOLKIT
12873 static XrmOptionDescRec emacs_options[] = {
12874 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12875 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12876
12877 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12878 XrmoptionSepArg, NULL},
12879 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12880
12881 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12882 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12883 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12884 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12885 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12886 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12887 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12888 };
12889 #endif /* USE_X_TOOLKIT */
12890
12891 static int x_initialized;
12892
12893 #ifdef MULTI_KBOARD
12894 /* Test whether two display-name strings agree up to the dot that separates
12895 the screen number from the server number. */
12896 static int
12897 same_x_server (name1, name2)
12898 char *name1, *name2;
12899 {
12900 int seen_colon = 0;
12901 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12902 int system_name_length = strlen (system_name);
12903 int length_until_period = 0;
12904
12905 while (system_name[length_until_period] != 0
12906 && system_name[length_until_period] != '.')
12907 length_until_period++;
12908
12909 /* Treat `unix' like an empty host name. */
12910 if (! strncmp (name1, "unix:", 5))
12911 name1 += 4;
12912 if (! strncmp (name2, "unix:", 5))
12913 name2 += 4;
12914 /* Treat this host's name like an empty host name. */
12915 if (! strncmp (name1, system_name, system_name_length)
12916 && name1[system_name_length] == ':')
12917 name1 += system_name_length;
12918 if (! strncmp (name2, system_name, system_name_length)
12919 && name2[system_name_length] == ':')
12920 name2 += system_name_length;
12921 /* Treat this host's domainless name like an empty host name. */
12922 if (! strncmp (name1, system_name, length_until_period)
12923 && name1[length_until_period] == ':')
12924 name1 += length_until_period;
12925 if (! strncmp (name2, system_name, length_until_period)
12926 && name2[length_until_period] == ':')
12927 name2 += length_until_period;
12928
12929 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12930 {
12931 if (*name1 == ':')
12932 seen_colon++;
12933 if (seen_colon && *name1 == '.')
12934 return 1;
12935 }
12936 return (seen_colon
12937 && (*name1 == '.' || *name1 == '\0')
12938 && (*name2 == '.' || *name2 == '\0'));
12939 }
12940 #endif
12941
12942 struct x_display_info *
12943 x_term_init (display_name, xrm_option, resource_name)
12944 Lisp_Object display_name;
12945 char *xrm_option;
12946 char *resource_name;
12947 {
12948 int connection;
12949 Display *dpy;
12950 struct x_display_info *dpyinfo;
12951 XrmDatabase xrdb;
12952
12953 BLOCK_INPUT;
12954
12955 if (!x_initialized)
12956 {
12957 x_initialize ();
12958 x_initialized = 1;
12959 }
12960
12961 #ifdef USE_X_TOOLKIT
12962 /* weiner@footloose.sps.mot.com reports that this causes
12963 errors with X11R5:
12964 X protocol error: BadAtom (invalid Atom parameter)
12965 on protocol request 18skiloaf.
12966 So let's not use it until R6. */
12967 #ifdef HAVE_X11XTR6
12968 XtSetLanguageProc (NULL, NULL, NULL);
12969 #endif
12970
12971 {
12972 int argc = 0;
12973 char *argv[3];
12974
12975 argv[0] = "";
12976 argc = 1;
12977 if (xrm_option)
12978 {
12979 argv[argc++] = "-xrm";
12980 argv[argc++] = xrm_option;
12981 }
12982 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12983 resource_name, EMACS_CLASS,
12984 emacs_options, XtNumber (emacs_options),
12985 &argc, argv);
12986
12987 #ifdef HAVE_X11XTR6
12988 /* I think this is to compensate for XtSetLanguageProc. */
12989 fixup_locale ();
12990 #endif
12991 }
12992
12993 #else /* not USE_X_TOOLKIT */
12994 #ifdef HAVE_X11R5
12995 XSetLocaleModifiers ("");
12996 #endif
12997 dpy = XOpenDisplay (XSTRING (display_name)->data);
12998 #endif /* not USE_X_TOOLKIT */
12999
13000 /* Detect failure. */
13001 if (dpy == 0)
13002 {
13003 UNBLOCK_INPUT;
13004 return 0;
13005 }
13006
13007 /* We have definitely succeeded. Record the new connection. */
13008
13009 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13010
13011 #ifdef MULTI_KBOARD
13012 {
13013 struct x_display_info *share;
13014 Lisp_Object tail;
13015
13016 for (share = x_display_list, tail = x_display_name_list; share;
13017 share = share->next, tail = XCDR (tail))
13018 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13019 XSTRING (display_name)->data))
13020 break;
13021 if (share)
13022 dpyinfo->kboard = share->kboard;
13023 else
13024 {
13025 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13026 init_kboard (dpyinfo->kboard);
13027 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13028 {
13029 char *vendor = ServerVendor (dpy);
13030 UNBLOCK_INPUT;
13031 dpyinfo->kboard->Vsystem_key_alist
13032 = call1 (Qvendor_specific_keysyms,
13033 build_string (vendor ? vendor : ""));
13034 BLOCK_INPUT;
13035 }
13036
13037 dpyinfo->kboard->next_kboard = all_kboards;
13038 all_kboards = dpyinfo->kboard;
13039 /* Don't let the initial kboard remain current longer than necessary.
13040 That would cause problems if a file loaded on startup tries to
13041 prompt in the mini-buffer. */
13042 if (current_kboard == initial_kboard)
13043 current_kboard = dpyinfo->kboard;
13044 }
13045 dpyinfo->kboard->reference_count++;
13046 }
13047 #endif
13048
13049 /* Put this display on the chain. */
13050 dpyinfo->next = x_display_list;
13051 x_display_list = dpyinfo;
13052
13053 /* Put it on x_display_name_list as well, to keep them parallel. */
13054 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13055 x_display_name_list);
13056 dpyinfo->name_list_element = XCAR (x_display_name_list);
13057
13058 dpyinfo->display = dpy;
13059
13060 #if 0
13061 XSetAfterFunction (x_current_display, x_trace_wire);
13062 #endif /* ! 0 */
13063
13064 dpyinfo->x_id_name
13065 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13066 + STRING_BYTES (XSTRING (Vsystem_name))
13067 + 2);
13068 sprintf (dpyinfo->x_id_name, "%s@%s",
13069 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13070
13071 /* Figure out which modifier bits mean what. */
13072 x_find_modifier_meanings (dpyinfo);
13073
13074 /* Get the scroll bar cursor. */
13075 dpyinfo->vertical_scroll_bar_cursor
13076 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13077
13078 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13079 resource_name, EMACS_CLASS);
13080 #ifdef HAVE_XRMSETDATABASE
13081 XrmSetDatabase (dpyinfo->display, xrdb);
13082 #else
13083 dpyinfo->display->db = xrdb;
13084 #endif
13085 /* Put the rdb where we can find it in a way that works on
13086 all versions. */
13087 dpyinfo->xrdb = xrdb;
13088
13089 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13090 DefaultScreen (dpyinfo->display));
13091 select_visual (dpyinfo);
13092 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13093 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13094 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13095 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13096 dpyinfo->grabbed = 0;
13097 dpyinfo->reference_count = 0;
13098 dpyinfo->icon_bitmap_id = -1;
13099 dpyinfo->font_table = NULL;
13100 dpyinfo->n_fonts = 0;
13101 dpyinfo->font_table_size = 0;
13102 dpyinfo->bitmaps = 0;
13103 dpyinfo->bitmaps_size = 0;
13104 dpyinfo->bitmaps_last = 0;
13105 dpyinfo->scratch_cursor_gc = 0;
13106 dpyinfo->mouse_face_mouse_frame = 0;
13107 dpyinfo->mouse_face_deferred_gc = 0;
13108 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13109 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13110 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13111 dpyinfo->mouse_face_window = Qnil;
13112 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13113 dpyinfo->mouse_face_defer = 0;
13114 dpyinfo->x_focus_frame = 0;
13115 dpyinfo->x_focus_event_frame = 0;
13116 dpyinfo->x_highlight_frame = 0;
13117 dpyinfo->image_cache = make_image_cache ();
13118
13119 /* See if a private colormap is requested. */
13120 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13121 {
13122 if (dpyinfo->visual->class == PseudoColor)
13123 {
13124 Lisp_Object value;
13125 value = display_x_get_resource (dpyinfo,
13126 build_string ("privateColormap"),
13127 build_string ("PrivateColormap"),
13128 Qnil, Qnil);
13129 if (STRINGP (value)
13130 && (!strcmp (XSTRING (value)->data, "true")
13131 || !strcmp (XSTRING (value)->data, "on")))
13132 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13133 }
13134 }
13135 else
13136 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13137 dpyinfo->visual, AllocNone);
13138
13139 {
13140 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13141 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13142 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13143 dpyinfo->resy = pixels * 25.4 / mm;
13144 pixels = DisplayWidth (dpyinfo->display, screen_number);
13145 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13146 dpyinfo->resx = pixels * 25.4 / mm;
13147 }
13148
13149 dpyinfo->Xatom_wm_protocols
13150 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13151 dpyinfo->Xatom_wm_take_focus
13152 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13153 dpyinfo->Xatom_wm_save_yourself
13154 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13155 dpyinfo->Xatom_wm_delete_window
13156 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13157 dpyinfo->Xatom_wm_change_state
13158 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13159 dpyinfo->Xatom_wm_configure_denied
13160 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13161 dpyinfo->Xatom_wm_window_moved
13162 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13163 dpyinfo->Xatom_editres
13164 = XInternAtom (dpyinfo->display, "Editres", False);
13165 dpyinfo->Xatom_CLIPBOARD
13166 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13167 dpyinfo->Xatom_TIMESTAMP
13168 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13169 dpyinfo->Xatom_TEXT
13170 = XInternAtom (dpyinfo->display, "TEXT", False);
13171 dpyinfo->Xatom_COMPOUND_TEXT
13172 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13173 dpyinfo->Xatom_DELETE
13174 = XInternAtom (dpyinfo->display, "DELETE", False);
13175 dpyinfo->Xatom_MULTIPLE
13176 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13177 dpyinfo->Xatom_INCR
13178 = XInternAtom (dpyinfo->display, "INCR", False);
13179 dpyinfo->Xatom_EMACS_TMP
13180 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13181 dpyinfo->Xatom_TARGETS
13182 = XInternAtom (dpyinfo->display, "TARGETS", False);
13183 dpyinfo->Xatom_NULL
13184 = XInternAtom (dpyinfo->display, "NULL", False);
13185 dpyinfo->Xatom_ATOM_PAIR
13186 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13187 /* For properties of font. */
13188 dpyinfo->Xatom_PIXEL_SIZE
13189 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13190 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13191 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13192 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13193 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13194 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13195 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13196
13197 /* Ghostscript support. */
13198 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13199 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13200
13201 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13202 False);
13203
13204 dpyinfo->cut_buffers_initialized = 0;
13205
13206 connection = ConnectionNumber (dpyinfo->display);
13207 dpyinfo->connection = connection;
13208
13209 {
13210 char null_bits[1];
13211
13212 null_bits[0] = 0x00;
13213
13214 dpyinfo->null_pixel
13215 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13216 null_bits, 1, 1, (long) 0, (long) 0,
13217 1);
13218 }
13219
13220 {
13221 extern int gray_bitmap_width, gray_bitmap_height;
13222 extern unsigned char *gray_bitmap_bits;
13223 dpyinfo->gray
13224 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13225 gray_bitmap_bits,
13226 gray_bitmap_width, gray_bitmap_height,
13227 (unsigned long) 1, (unsigned long) 0, 1);
13228 }
13229
13230 #ifdef HAVE_X_I18N
13231 xim_initialize (dpyinfo, resource_name);
13232 #endif
13233
13234 #ifdef subprocesses
13235 /* This is only needed for distinguishing keyboard and process input. */
13236 if (connection != 0)
13237 add_keyboard_wait_descriptor (connection);
13238 #endif
13239
13240 #ifndef F_SETOWN_BUG
13241 #ifdef F_SETOWN
13242 #ifdef F_SETOWN_SOCK_NEG
13243 /* stdin is a socket here */
13244 fcntl (connection, F_SETOWN, -getpid ());
13245 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13246 fcntl (connection, F_SETOWN, getpid ());
13247 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13248 #endif /* ! defined (F_SETOWN) */
13249 #endif /* F_SETOWN_BUG */
13250
13251 #ifdef SIGIO
13252 if (interrupt_input)
13253 init_sigio (connection);
13254 #endif /* ! defined (SIGIO) */
13255
13256 #ifdef USE_LUCID
13257 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13258 /* Make sure that we have a valid font for dialog boxes
13259 so that Xt does not crash. */
13260 {
13261 Display *dpy = dpyinfo->display;
13262 XrmValue d, fr, to;
13263 Font font;
13264 int count;
13265
13266 d.addr = (XPointer)&dpy;
13267 d.size = sizeof (Display *);
13268 fr.addr = XtDefaultFont;
13269 fr.size = sizeof (XtDefaultFont);
13270 to.size = sizeof (Font *);
13271 to.addr = (XPointer)&font;
13272 count = x_catch_errors (dpy);
13273 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13274 abort ();
13275 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13276 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13277 x_uncatch_errors (dpy, count);
13278 }
13279 #endif
13280 #endif
13281
13282 /* See if we should run in synchronous mode. This is useful
13283 for debugging X code. */
13284 {
13285 Lisp_Object value;
13286 value = display_x_get_resource (dpyinfo,
13287 build_string ("synchronous"),
13288 build_string ("Synchronous"),
13289 Qnil, Qnil);
13290 if (STRINGP (value)
13291 && (!strcmp (XSTRING (value)->data, "true")
13292 || !strcmp (XSTRING (value)->data, "on")))
13293 XSynchronize (dpyinfo->display, True);
13294 }
13295
13296 UNBLOCK_INPUT;
13297
13298 return dpyinfo;
13299 }
13300 \f
13301 /* Get rid of display DPYINFO, assuming all frames are already gone,
13302 and without sending any more commands to the X server. */
13303
13304 void
13305 x_delete_display (dpyinfo)
13306 struct x_display_info *dpyinfo;
13307 {
13308 delete_keyboard_wait_descriptor (dpyinfo->connection);
13309
13310 /* Discard this display from x_display_name_list and x_display_list.
13311 We can't use Fdelq because that can quit. */
13312 if (! NILP (x_display_name_list)
13313 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13314 x_display_name_list = XCDR (x_display_name_list);
13315 else
13316 {
13317 Lisp_Object tail;
13318
13319 tail = x_display_name_list;
13320 while (CONSP (tail) && CONSP (XCDR (tail)))
13321 {
13322 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13323 {
13324 XCDR (tail) = XCDR (XCDR (tail));
13325 break;
13326 }
13327 tail = XCDR (tail);
13328 }
13329 }
13330
13331 if (next_noop_dpyinfo == dpyinfo)
13332 next_noop_dpyinfo = dpyinfo->next;
13333
13334 if (x_display_list == dpyinfo)
13335 x_display_list = dpyinfo->next;
13336 else
13337 {
13338 struct x_display_info *tail;
13339
13340 for (tail = x_display_list; tail; tail = tail->next)
13341 if (tail->next == dpyinfo)
13342 tail->next = tail->next->next;
13343 }
13344
13345 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13346 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13347 XrmDestroyDatabase (dpyinfo->xrdb);
13348 #endif
13349 #endif
13350 #ifdef MULTI_KBOARD
13351 if (--dpyinfo->kboard->reference_count == 0)
13352 delete_kboard (dpyinfo->kboard);
13353 #endif
13354 #ifdef HAVE_X_I18N
13355 if (dpyinfo->xim)
13356 xim_close_dpy (dpyinfo);
13357 #endif
13358
13359 xfree (dpyinfo->font_table);
13360 xfree (dpyinfo->x_id_name);
13361 xfree (dpyinfo);
13362 }
13363 \f
13364 /* Set up use of X before we make the first connection. */
13365
13366 static struct redisplay_interface x_redisplay_interface =
13367 {
13368 x_produce_glyphs,
13369 x_write_glyphs,
13370 x_insert_glyphs,
13371 x_clear_end_of_line,
13372 x_scroll_run,
13373 x_after_update_window_line,
13374 x_update_window_begin,
13375 x_update_window_end,
13376 XTcursor_to,
13377 x_flush,
13378 x_get_glyph_overhangs,
13379 x_fix_overlapping_area
13380 };
13381
13382 void
13383 x_initialize ()
13384 {
13385 rif = &x_redisplay_interface;
13386
13387 clear_frame_hook = x_clear_frame;
13388 ins_del_lines_hook = x_ins_del_lines;
13389 change_line_highlight_hook = x_change_line_highlight;
13390 delete_glyphs_hook = x_delete_glyphs;
13391 ring_bell_hook = XTring_bell;
13392 reset_terminal_modes_hook = XTreset_terminal_modes;
13393 set_terminal_modes_hook = XTset_terminal_modes;
13394 update_begin_hook = x_update_begin;
13395 update_end_hook = x_update_end;
13396 set_terminal_window_hook = XTset_terminal_window;
13397 read_socket_hook = XTread_socket;
13398 frame_up_to_date_hook = XTframe_up_to_date;
13399 reassert_line_highlight_hook = XTreassert_line_highlight;
13400 mouse_position_hook = XTmouse_position;
13401 frame_rehighlight_hook = XTframe_rehighlight;
13402 frame_raise_lower_hook = XTframe_raise_lower;
13403 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13404 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13405 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13406 judge_scroll_bars_hook = XTjudge_scroll_bars;
13407 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13408
13409 scroll_region_ok = 1; /* we'll scroll partial frames */
13410 char_ins_del_ok = 0; /* just as fast to write the line */
13411 line_ins_del_ok = 1; /* we'll just blt 'em */
13412 fast_clear_end_of_line = 1; /* X does this well */
13413 memory_below_frame = 0; /* we don't remember what scrolls
13414 off the bottom */
13415 baud_rate = 19200;
13416
13417 x_noop_count = 0;
13418 last_tool_bar_item = -1;
13419 any_help_event_p = 0;
13420
13421 /* Try to use interrupt input; if we can't, then start polling. */
13422 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13423
13424 #ifdef USE_X_TOOLKIT
13425 XtToolkitInitialize ();
13426 Xt_app_con = XtCreateApplicationContext ();
13427 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13428
13429 /* Install an asynchronous timer that processes Xt timeout events
13430 every 0.1s. This is necessary because some widget sets use
13431 timeouts internally, for example the LessTif menu bar, or the
13432 Xaw3d scroll bar. When Xt timouts aren't processed, these
13433 widgets don't behave normally. */
13434 {
13435 EMACS_TIME interval;
13436 EMACS_SET_SECS_USECS (interval, 0, 100000);
13437 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13438 }
13439 #endif
13440
13441 #if USE_TOOLKIT_SCROLL_BARS
13442 xaw3d_arrow_scroll = False;
13443 xaw3d_pick_top = True;
13444 #endif
13445
13446 /* Note that there is no real way portable across R3/R4 to get the
13447 original error handler. */
13448 XSetErrorHandler (x_error_handler);
13449 XSetIOErrorHandler (x_io_error_quitter);
13450
13451 /* Disable Window Change signals; they are handled by X events. */
13452 #ifdef SIGWINCH
13453 signal (SIGWINCH, SIG_DFL);
13454 #endif /* ! defined (SIGWINCH) */
13455
13456 signal (SIGPIPE, x_connection_signal);
13457 }
13458
13459
13460 void
13461 syms_of_xterm ()
13462 {
13463 staticpro (&x_error_message_string);
13464 x_error_message_string = Qnil;
13465
13466 staticpro (&x_display_name_list);
13467 x_display_name_list = Qnil;
13468
13469 staticpro (&last_mouse_scroll_bar);
13470 last_mouse_scroll_bar = Qnil;
13471
13472 staticpro (&Qvendor_specific_keysyms);
13473 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13474
13475 staticpro (&last_mouse_press_frame);
13476 last_mouse_press_frame = Qnil;
13477
13478 staticpro (&help_echo);
13479 help_echo = Qnil;
13480 staticpro (&previous_help_echo);
13481 previous_help_echo = Qnil;
13482
13483 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13484 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13485 For example, if a block cursor is over a tab, it will be drawn as\n\
13486 wide as that tab on the display.");
13487 x_stretch_cursor_p = 0;
13488
13489 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13490 "If not nil, Emacs uses toolkit scroll bars.");
13491 #if USE_TOOLKIT_SCROLL_BARS
13492 x_toolkit_scroll_bars_p = 1;
13493 #else
13494 x_toolkit_scroll_bars_p = 0;
13495 #endif
13496
13497 staticpro (&last_mouse_motion_frame);
13498 last_mouse_motion_frame = Qnil;
13499 }
13500
13501 #endif /* not HAVE_X_WINDOWS */
13502