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