(window_loop) <GET_BUFFER_WINDOW>: Prefer to return
[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, 2001
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 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
91
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
95
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
102
103 #ifdef USE_X_TOOLKIT
104
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
108
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
113
114 /* Include toolkit specific headers for the scroll bar widget. */
115
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #include <Xm/ScrollBarP.h>
121 #else /* !USE_MOTIF i.e. use Xaw */
122
123 #ifdef HAVE_XAW3D
124 #include <X11/Xaw3d/Simple.h>
125 #include <X11/Xaw3d/Scrollbar.h>
126 #define ARROW_SCROLLBAR
127 #include <X11/Xaw3d/ScrollbarP.h>
128 #else /* !HAVE_XAW3D */
129 #include <X11/Xaw/Simple.h>
130 #include <X11/Xaw/Scrollbar.h>
131 #endif /* !HAVE_XAW3D */
132 #ifndef XtNpickTop
133 #define XtNpickTop "pickTop"
134 #endif /* !XtNpickTop */
135 #endif /* !USE_MOTIF */
136 #endif /* USE_TOOLKIT_SCROLL_BARS */
137
138 #endif /* USE_X_TOOLKIT */
139
140 #ifndef USE_X_TOOLKIT
141 #define x_any_window_to_frame x_window_to_frame
142 #define x_top_window_to_frame x_window_to_frame
143 #endif
144
145 #ifdef USE_X_TOOLKIT
146 #include "widget.h"
147 #ifndef XtNinitialState
148 #define XtNinitialState "initialState"
149 #endif
150 #endif
151
152 #ifndef min
153 #define min(a,b) ((a) < (b) ? (a) : (b))
154 #endif
155 #ifndef max
156 #define max(a,b) ((a) > (b) ? (a) : (b))
157 #endif
158
159 #define abs(x) ((x) < 0 ? -(x) : (x))
160
161 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163 \f
164 /* Bitmaps for truncated lines. */
165
166 enum bitmap_type
167 {
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175 };
176
177 /* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180 #define zv_width 8
181 #define zv_height 8
182 static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184
185 /* An arrow like this: `<-'. */
186
187 #define left_width 8
188 #define left_height 8
189 static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191
192 /* Right truncation arrow bitmap `->'. */
193
194 #define right_width 8
195 #define right_height 8
196 static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198
199 /* Marker for continued lines. */
200
201 #define continued_width 8
202 #define continued_height 8
203 static unsigned char continued_bits[] = {
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205
206 /* Marker for continuation lines. */
207
208 #define continuation_width 8
209 #define continuation_height 8
210 static unsigned char continuation_bits[] = {
211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
212
213 /* Overlay arrow bitmap. */
214
215 #if 0
216 /* A bomb. */
217 #define ov_width 8
218 #define ov_height 8
219 static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
221 #else
222 /* A triangular arrow. */
223 #define ov_width 8
224 #define ov_height 8
225 static unsigned char ov_bits[] = {
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
228 #endif
229
230 extern Lisp_Object Qhelp_echo;
231
232 \f
233 /* Non-nil means Emacs uses toolkit scroll bars. */
234
235 Lisp_Object Vx_toolkit_scroll_bars;
236
237 /* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
239
240 static Lisp_Object help_echo;
241 static Lisp_Object help_echo_window;
242 static Lisp_Object help_echo_object;
243 static int help_echo_pos;
244
245 /* Temporary variable for XTread_socket. */
246
247 static Lisp_Object previous_help_echo;
248
249 /* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252 static int any_help_event_p;
253
254 /* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
257
258 int x_stretch_cursor_p;
259
260 /* This is a chain of structures for all the X displays currently in
261 use. */
262
263 struct x_display_info *x_display_list;
264
265 /* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
269
270 Lisp_Object x_display_name_list;
271
272 /* Frame being updated by update_frame. This is declared in term.c.
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
276
277 extern struct frame *updating_frame;
278
279 extern int waiting_for_input;
280
281 /* This is a frame waiting to be auto-raised, within XTread_socket. */
282
283 struct frame *pending_autoraise_frame;
284
285 #ifdef USE_X_TOOLKIT
286 /* The application context for Xt use. */
287 XtAppContext Xt_app_con;
288 static String Xt_default_resources[] = {0};
289 #endif /* USE_X_TOOLKIT */
290
291 /* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
294
295 struct cursor_pos output_cursor;
296
297 /* Non-zero means user is interacting with a toolkit scroll bar. */
298
299 static int toolkit_scroll_bar_interaction;
300
301 /* Mouse movement.
302
303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
307
308 Simply asking for MotionNotify all the time seems to work better.
309
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
319 is off. */
320
321 /* Where the mouse was last time we reported a mouse event. */
322
323 FRAME_PTR last_mouse_frame;
324 static XRectangle last_mouse_glyph;
325 static Lisp_Object last_mouse_press_frame;
326
327 /* The scroll bar in which the last X motion event occurred.
328
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
331 an ordinary motion.
332
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
336
337 static Lisp_Object last_mouse_scroll_bar;
338
339 /* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
341 doesn't seem to be any way to wrest the time-stamp from the server
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
345
346 static Time last_mouse_movement_time;
347
348 /* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
351 #ifdef __STDC__
352 static int volatile input_signal_count;
353 #else
354 static int input_signal_count;
355 #endif
356
357 /* Used locally within XTread_socket. */
358
359 static int x_noop_count;
360
361 /* Initial values of argv and argc. */
362
363 extern char **initial_argv;
364 extern int initial_argc;
365
366 extern Lisp_Object Vcommand_line_args, Vsystem_name;
367
368 /* Tells if a window manager is present or not. */
369
370 extern Lisp_Object Vx_no_window_manager;
371
372 extern Lisp_Object Qface, Qmouse_face;
373
374 extern int errno;
375
376 /* A mask of extra modifier bits to put into every keyboard char. */
377
378 extern int extra_keyboard_modifiers;
379
380 static Lisp_Object Qvendor_specific_keysyms;
381
382 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383 extern Lisp_Object x_icon_type P_ ((struct frame *));
384
385
386 /* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
388
389 enum draw_glyphs_face
390 {
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
397 };
398
399 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
400 static const XColor *x_color_cells P_ ((struct frame *, int *));
401 static void x_update_window_end P_ ((struct window *, int, int));
402 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403 void x_delete_display P_ ((struct x_display_info *));
404 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406 static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
408 static void set_output_cursor P_ ((struct cursor_pos *));
409 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
410 int *, int *, int *));
411 static void note_mode_line_highlight P_ ((struct window *, int, int));
412 static void note_mouse_highlight P_ ((struct frame *, int, int));
413 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
414 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
415 static void show_mouse_face P_ ((struct x_display_info *,
416 enum draw_glyphs_face));
417 static int x_io_error_quitter P_ ((Display *));
418 int x_catch_errors P_ ((Display *));
419 void x_uncatch_errors P_ ((Display *, int));
420 void x_lower_frame P_ ((struct frame *));
421 void x_scroll_bar_clear P_ ((struct frame *));
422 int x_had_errors_p P_ ((Display *));
423 void x_wm_set_size_hint P_ ((struct frame *, long, int));
424 void x_raise_frame P_ ((struct frame *));
425 void x_set_window_size P_ ((struct frame *, int, int, int));
426 void x_wm_set_window_state P_ ((struct frame *, int));
427 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
428 void x_initialize P_ ((void));
429 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
430 static int x_compute_min_glyph_bounds P_ ((struct frame *));
431 static void x_draw_phys_cursor_glyph P_ ((struct window *,
432 struct glyph_row *,
433 enum draw_glyphs_face));
434 static void x_update_end P_ ((struct frame *));
435 static void XTframe_up_to_date P_ ((struct frame *));
436 static void XTreassert_line_highlight P_ ((int, int));
437 static void x_change_line_highlight P_ ((int, int, int, int));
438 static void XTset_terminal_modes P_ ((void));
439 static void XTreset_terminal_modes P_ ((void));
440 static void XTcursor_to P_ ((int, int, int, int));
441 static void x_write_glyphs P_ ((struct glyph *, int));
442 static void x_clear_end_of_line P_ ((int));
443 static void x_clear_frame P_ ((void));
444 static void x_clear_cursor P_ ((struct window *));
445 static void frame_highlight P_ ((struct frame *));
446 static void frame_unhighlight P_ ((struct frame *));
447 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
448 static void XTframe_rehighlight P_ ((struct frame *));
449 static void x_frame_rehighlight P_ ((struct x_display_info *));
450 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
451 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
452 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
453 XRectangle *));
454 static void expose_frame P_ ((struct frame *, int, int, int, int));
455 static void expose_window_tree P_ ((struct window *, XRectangle *));
456 static void expose_window P_ ((struct window *, XRectangle *));
457 static void expose_area P_ ((struct window *, struct glyph_row *,
458 XRectangle *, enum glyph_row_area));
459 static void expose_line P_ ((struct window *, struct glyph_row *,
460 XRectangle *));
461 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
462 static void x_update_window_cursor P_ ((struct window *, int));
463 static void x_erase_phys_cursor P_ ((struct window *));
464 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
465 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
466 enum bitmap_type));
467
468 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
469 GC, int));
470 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
471 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
472 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
473 static void x_flush P_ ((struct frame *f));
474 static void x_update_begin P_ ((struct frame *));
475 static void x_update_window_begin P_ ((struct window *));
476 static void x_draw_vertical_border P_ ((struct window *));
477 static void x_after_update_window_line P_ ((struct glyph_row *));
478 static INLINE void take_vertical_position_into_account P_ ((struct it *));
479 static void x_produce_stretch_glyph P_ ((struct it *));
480
481
482 /* Flush display of frame F, or of all frames if F is null. */
483
484 static void
485 x_flush (f)
486 struct frame *f;
487 {
488 BLOCK_INPUT;
489 if (f == NULL)
490 {
491 Lisp_Object rest, frame;
492 FOR_EACH_FRAME (rest, frame)
493 x_flush (XFRAME (frame));
494 }
495 else if (FRAME_X_P (f))
496 XFlush (FRAME_X_DISPLAY (f));
497 UNBLOCK_INPUT;
498 }
499
500
501 /* Remove calls to XFlush by defining XFlush to an empty replacement.
502 Calls to XFlush should be unnecessary because the X output buffer
503 is flushed automatically as needed by calls to XPending,
504 XNextEvent, or XWindowEvent according to the XFlush man page.
505 XTread_socket calls XPending. Removing XFlush improves
506 performance. */
507
508 #define XFlush(DISPLAY) (void) 0
509
510 \f
511 /***********************************************************************
512 Debugging
513 ***********************************************************************/
514
515 #if 0
516
517 /* This is a function useful for recording debugging information about
518 the sequence of occurrences in this file. */
519
520 struct record
521 {
522 char *locus;
523 int type;
524 };
525
526 struct record event_record[100];
527
528 int event_record_index;
529
530 record_event (locus, type)
531 char *locus;
532 int type;
533 {
534 if (event_record_index == sizeof (event_record) / sizeof (struct record))
535 event_record_index = 0;
536
537 event_record[event_record_index].locus = locus;
538 event_record[event_record_index].type = type;
539 event_record_index++;
540 }
541
542 #endif /* 0 */
543
544
545 \f
546 /* Return the struct x_display_info corresponding to DPY. */
547
548 struct x_display_info *
549 x_display_info_for_display (dpy)
550 Display *dpy;
551 {
552 struct x_display_info *dpyinfo;
553
554 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
555 if (dpyinfo->display == dpy)
556 return dpyinfo;
557
558 return 0;
559 }
560
561
562 \f
563 /***********************************************************************
564 Starting and ending an update
565 ***********************************************************************/
566
567 /* Start an update of frame F. This function is installed as a hook
568 for update_begin, i.e. it is called when update_begin is called.
569 This function is called prior to calls to x_update_window_begin for
570 each window being updated. Currently, there is nothing to do here
571 because all interesting stuff is done on a window basis. */
572
573 static void
574 x_update_begin (f)
575 struct frame *f;
576 {
577 /* Nothing to do. */
578 }
579
580
581 /* Start update of window W. Set the global variable updated_window
582 to the window being updated and set output_cursor to the cursor
583 position of W. */
584
585 static void
586 x_update_window_begin (w)
587 struct window *w;
588 {
589 struct frame *f = XFRAME (WINDOW_FRAME (w));
590 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
591
592 updated_window = w;
593 set_output_cursor (&w->cursor);
594
595 BLOCK_INPUT;
596
597 if (f == display_info->mouse_face_mouse_frame)
598 {
599 /* Don't do highlighting for mouse motion during the update. */
600 display_info->mouse_face_defer = 1;
601
602 /* If F needs to be redrawn, simply forget about any prior mouse
603 highlighting. */
604 if (FRAME_GARBAGED_P (f))
605 display_info->mouse_face_window = Qnil;
606
607 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
608 their mouse_face_p flag set, which means that they are always
609 unequal to rows in a desired matrix which never have that
610 flag set. So, rows containing mouse-face glyphs are never
611 scrolled, and we don't have to switch the mouse highlight off
612 here to prevent it from being scrolled. */
613
614 /* Can we tell that this update does not affect the window
615 where the mouse highlight is? If so, no need to turn off.
616 Likewise, don't do anything if the frame is garbaged;
617 in that case, the frame's current matrix that we would use
618 is all wrong, and we will redisplay that line anyway. */
619 if (!NILP (display_info->mouse_face_window)
620 && w == XWINDOW (display_info->mouse_face_window))
621 {
622 int i;
623
624 for (i = 0; i < w->desired_matrix->nrows; ++i)
625 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
626 break;
627
628 if (i < w->desired_matrix->nrows)
629 clear_mouse_face (display_info);
630 }
631 #endif /* 0 */
632 }
633
634 UNBLOCK_INPUT;
635 }
636
637
638 /* Draw a vertical window border to the right of window W if W doesn't
639 have vertical scroll bars. */
640
641 static void
642 x_draw_vertical_border (w)
643 struct window *w;
644 {
645 struct frame *f = XFRAME (WINDOW_FRAME (w));
646
647 /* Redraw borders between horizontally adjacent windows. Don't
648 do it for frames with vertical scroll bars because either the
649 right scroll bar of a window, or the left scroll bar of its
650 neighbor will suffice as a border. */
651 if (!WINDOW_RIGHTMOST_P (w)
652 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
653 {
654 int x0, x1, y0, y1;
655
656 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
657 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
658 y1 -= 1;
659
660 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
661 f->output_data.x->normal_gc, x1, y0, x1, y1);
662 }
663 }
664
665
666 /* End update of window W (which is equal to updated_window).
667
668 Draw vertical borders between horizontally adjacent windows, and
669 display W's cursor if CURSOR_ON_P is non-zero.
670
671 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
672 glyphs in mouse-face were overwritten. In that case we have to
673 make sure that the mouse-highlight is properly redrawn.
674
675 W may be a menu bar pseudo-window in case we don't have X toolkit
676 support. Such windows don't have a cursor, so don't display it
677 here. */
678
679 static void
680 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
681 struct window *w;
682 int cursor_on_p, mouse_face_overwritten_p;
683 {
684 if (!w->pseudo_window_p)
685 {
686 struct x_display_info *dpyinfo
687 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
688
689 BLOCK_INPUT;
690
691 /* If a row with mouse-face was overwritten, arrange for
692 XTframe_up_to_date to redisplay the mouse highlight. */
693 if (mouse_face_overwritten_p)
694 {
695 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
696 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
697 dpyinfo->mouse_face_window = Qnil;
698 }
699
700 if (cursor_on_p)
701 x_display_and_set_cursor (w, 1, output_cursor.hpos,
702 output_cursor.vpos,
703 output_cursor.x, output_cursor.y);
704
705 x_draw_vertical_border (w);
706 UNBLOCK_INPUT;
707 }
708
709 updated_window = NULL;
710 }
711
712
713 /* End update of frame F. This function is installed as a hook in
714 update_end. */
715
716 static void
717 x_update_end (f)
718 struct frame *f;
719 {
720 /* Mouse highlight may be displayed again. */
721 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
722
723 BLOCK_INPUT;
724 XFlush (FRAME_X_DISPLAY (f));
725 UNBLOCK_INPUT;
726 }
727
728
729 /* This function is called from various places in xdisp.c whenever a
730 complete update has been performed. The global variable
731 updated_window is not available here. */
732
733 static void
734 XTframe_up_to_date (f)
735 struct frame *f;
736 {
737 if (FRAME_X_P (f))
738 {
739 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
740
741 if (dpyinfo->mouse_face_deferred_gc
742 || f == dpyinfo->mouse_face_mouse_frame)
743 {
744 BLOCK_INPUT;
745 if (dpyinfo->mouse_face_mouse_frame)
746 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
747 dpyinfo->mouse_face_mouse_x,
748 dpyinfo->mouse_face_mouse_y);
749 dpyinfo->mouse_face_deferred_gc = 0;
750 UNBLOCK_INPUT;
751 }
752 }
753 }
754
755
756 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
757 arrow bitmaps, or clear the areas where they would be displayed
758 before DESIRED_ROW is made current. The window being updated is
759 found in updated_window. This function It is called from
760 update_window_line only if it is known that there are differences
761 between bitmaps to be drawn between current row and DESIRED_ROW. */
762
763 static void
764 x_after_update_window_line (desired_row)
765 struct glyph_row *desired_row;
766 {
767 struct window *w = updated_window;
768
769 xassert (w);
770
771 if (!desired_row->mode_line_p && !w->pseudo_window_p)
772 {
773 struct frame *f;
774 int width;
775
776 BLOCK_INPUT;
777 x_draw_row_bitmaps (w, desired_row);
778
779 /* When a window has disappeared, make sure that no rest of
780 full-width rows stays visible in the internal border. */
781 if (windows_or_buffers_changed
782 && (f = XFRAME (w->frame),
783 width = FRAME_INTERNAL_BORDER_WIDTH (f),
784 width != 0))
785 {
786 int height = desired_row->visible_height;
787 int x = (window_box_right (w, -1)
788 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
789 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
790
791 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
792 x, y, width, height, False);
793 }
794
795 UNBLOCK_INPUT;
796 }
797 }
798
799
800 /* Draw the bitmap WHICH in one of the areas to the left or right of
801 window W. ROW is the glyph row for which to display the bitmap; it
802 determines the vertical position at which the bitmap has to be
803 drawn. */
804
805 static void
806 x_draw_bitmap (w, row, which)
807 struct window *w;
808 struct glyph_row *row;
809 enum bitmap_type which;
810 {
811 struct frame *f = XFRAME (WINDOW_FRAME (w));
812 Display *display = FRAME_X_DISPLAY (f);
813 Window window = FRAME_X_WINDOW (f);
814 int x, y, wd, h, dy;
815 unsigned char *bits;
816 Pixmap pixmap;
817 GC gc = f->output_data.x->normal_gc;
818 struct face *face;
819 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
820
821 /* Must clip because of partially visible lines. */
822 x_clip_to_row (w, row, gc, 1);
823
824 switch (which)
825 {
826 case LEFT_TRUNCATION_BITMAP:
827 wd = left_width;
828 h = left_height;
829 bits = left_bits;
830 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
831 - wd
832 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
833 break;
834
835 case OVERLAY_ARROW_BITMAP:
836 wd = left_width;
837 h = left_height;
838 bits = ov_bits;
839 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
840 - wd
841 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
842 break;
843
844 case RIGHT_TRUNCATION_BITMAP:
845 wd = right_width;
846 h = right_height;
847 bits = right_bits;
848 x = window_box_right (w, -1);
849 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
850 break;
851
852 case CONTINUED_LINE_BITMAP:
853 wd = right_width;
854 h = right_height;
855 bits = continued_bits;
856 x = window_box_right (w, -1);
857 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
858 break;
859
860 case CONTINUATION_LINE_BITMAP:
861 wd = continuation_width;
862 h = continuation_height;
863 bits = continuation_bits;
864 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
865 - wd
866 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
867 break;
868
869 case ZV_LINE_BITMAP:
870 wd = zv_width;
871 h = zv_height;
872 bits = zv_bits;
873 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
874 - wd
875 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
876 break;
877
878 default:
879 abort ();
880 }
881
882 /* Convert to frame coordinates. Set dy to the offset in the row to
883 start drawing the bitmap. */
884 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
885 dy = (row->height - h) / 2;
886
887 /* Draw the bitmap. I believe these small pixmaps can be cached
888 by the server. */
889 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
890 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
891 face->foreground,
892 face->background, depth);
893 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
894 XFreePixmap (display, pixmap);
895 XSetClipMask (display, gc, None);
896 }
897
898
899 /* Draw flags bitmaps for glyph row ROW on window W. Call this
900 function with input blocked. */
901
902 static void
903 x_draw_row_bitmaps (w, row)
904 struct window *w;
905 struct glyph_row *row;
906 {
907 struct frame *f = XFRAME (w->frame);
908 enum bitmap_type bitmap;
909 struct face *face;
910 int header_line_height = -1;
911
912 xassert (interrupt_input_blocked);
913
914 /* If row is completely invisible, because of vscrolling, we
915 don't have to draw anything. */
916 if (row->visible_height <= 0)
917 return;
918
919 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
920 PREPARE_FACE_FOR_DISPLAY (f, face);
921
922 /* Decide which bitmap to draw at the left side. */
923 if (row->overlay_arrow_p)
924 bitmap = OVERLAY_ARROW_BITMAP;
925 else if (row->truncated_on_left_p)
926 bitmap = LEFT_TRUNCATION_BITMAP;
927 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
928 bitmap = CONTINUATION_LINE_BITMAP;
929 else if (row->indicate_empty_line_p)
930 bitmap = ZV_LINE_BITMAP;
931 else
932 bitmap = NO_BITMAP;
933
934 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
935 the flags area. */
936 if (bitmap == NO_BITMAP
937 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
938 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
939 {
940 /* If W has a vertical border to its left, don't draw over it. */
941 int border = ((XFASTINT (w->left) > 0
942 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
943 ? 1 : 0);
944 int left = window_box_left (w, -1);
945
946 if (header_line_height < 0)
947 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
948
949 /* In case the same realized face is used for bitmap areas and
950 for something displayed in the text (e.g. face `region' on
951 mono-displays, the fill style may have been changed to
952 FillSolid in x_draw_glyph_string_background. */
953 if (face->stipple)
954 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
955 else
956 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
957
958 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
959 face->gc,
960 (left
961 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
962 + border),
963 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
964 row->y)),
965 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
966 row->visible_height);
967 if (!face->stipple)
968 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
969 }
970
971 /* Draw the left bitmap. */
972 if (bitmap != NO_BITMAP)
973 x_draw_bitmap (w, row, bitmap);
974
975 /* Decide which bitmap to draw at the right side. */
976 if (row->truncated_on_right_p)
977 bitmap = RIGHT_TRUNCATION_BITMAP;
978 else if (row->continued_p)
979 bitmap = CONTINUED_LINE_BITMAP;
980 else
981 bitmap = NO_BITMAP;
982
983 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
984 the flags area. */
985 if (bitmap == NO_BITMAP
986 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
987 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
988 {
989 int right = window_box_right (w, -1);
990
991 if (header_line_height < 0)
992 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
993
994 /* In case the same realized face is used for bitmap areas and
995 for something displayed in the text (e.g. face `region' on
996 mono-displays, the fill style may have been changed to
997 FillSolid in x_draw_glyph_string_background. */
998 if (face->stipple)
999 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1000 else
1001 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1003 face->gc,
1004 right,
1005 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1006 row->y)),
1007 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1008 row->visible_height);
1009 if (!face->stipple)
1010 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1011 }
1012
1013 /* Draw the right bitmap. */
1014 if (bitmap != NO_BITMAP)
1015 x_draw_bitmap (w, row, bitmap);
1016 }
1017
1018 \f
1019 /***********************************************************************
1020 Line Highlighting
1021 ***********************************************************************/
1022
1023 /* External interface to control of standout mode. Not used for X
1024 frames. Aborts when called. */
1025
1026 static void
1027 XTreassert_line_highlight (new, vpos)
1028 int new, vpos;
1029 {
1030 abort ();
1031 }
1032
1033
1034 /* Call this when about to modify line at position VPOS and change
1035 whether it is highlighted. Not used for X frames. Aborts when
1036 called. */
1037
1038 static void
1039 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1040 int new_highlight, vpos, y, first_unused_hpos;
1041 {
1042 abort ();
1043 }
1044
1045
1046 /* This is called when starting Emacs and when restarting after
1047 suspend. When starting Emacs, no X window is mapped. And nothing
1048 must be done to Emacs's own window if it is suspended (though that
1049 rarely happens). */
1050
1051 static void
1052 XTset_terminal_modes ()
1053 {
1054 }
1055
1056 /* This is called when exiting or suspending Emacs. Exiting will make
1057 the X-windows go away, and suspending requires no action. */
1058
1059 static void
1060 XTreset_terminal_modes ()
1061 {
1062 }
1063
1064
1065 \f
1066 /***********************************************************************
1067 Output Cursor
1068 ***********************************************************************/
1069
1070 /* Set the global variable output_cursor to CURSOR. All cursor
1071 positions are relative to updated_window. */
1072
1073 static void
1074 set_output_cursor (cursor)
1075 struct cursor_pos *cursor;
1076 {
1077 output_cursor.hpos = cursor->hpos;
1078 output_cursor.vpos = cursor->vpos;
1079 output_cursor.x = cursor->x;
1080 output_cursor.y = cursor->y;
1081 }
1082
1083
1084 /* Set a nominal cursor position.
1085
1086 HPOS and VPOS are column/row positions in a window glyph matrix. X
1087 and Y are window text area relative pixel positions.
1088
1089 If this is done during an update, updated_window will contain the
1090 window that is being updated and the position is the future output
1091 cursor position for that window. If updated_window is null, use
1092 selected_window and display the cursor at the given position. */
1093
1094 static void
1095 XTcursor_to (vpos, hpos, y, x)
1096 int vpos, hpos, y, x;
1097 {
1098 struct window *w;
1099
1100 /* If updated_window is not set, work on selected_window. */
1101 if (updated_window)
1102 w = updated_window;
1103 else
1104 w = XWINDOW (selected_window);
1105
1106 /* Set the output cursor. */
1107 output_cursor.hpos = hpos;
1108 output_cursor.vpos = vpos;
1109 output_cursor.x = x;
1110 output_cursor.y = y;
1111
1112 /* If not called as part of an update, really display the cursor.
1113 This will also set the cursor position of W. */
1114 if (updated_window == NULL)
1115 {
1116 BLOCK_INPUT;
1117 x_display_cursor (w, 1, hpos, vpos, x, y);
1118 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1119 UNBLOCK_INPUT;
1120 }
1121 }
1122
1123
1124 \f
1125 /***********************************************************************
1126 Display Iterator
1127 ***********************************************************************/
1128
1129 /* Function prototypes of this page. */
1130
1131 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1132 struct glyph *,
1133 XChar2b *,
1134 int *));
1135 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1136 int, XChar2b *, int));
1137 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1138 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1139 static void x_append_glyph P_ ((struct it *));
1140 static void x_append_composite_glyph P_ ((struct it *));
1141 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1142 int, int, double));
1143 static void x_produce_glyphs P_ ((struct it *));
1144 static void x_produce_image_glyph P_ ((struct it *it));
1145
1146
1147 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1148 is not contained in the font. */
1149
1150 static INLINE XCharStruct *
1151 x_per_char_metric (font, char2b)
1152 XFontStruct *font;
1153 XChar2b *char2b;
1154 {
1155 /* The result metric information. */
1156 XCharStruct *pcm = NULL;
1157
1158 xassert (font && char2b);
1159
1160 if (font->per_char != NULL)
1161 {
1162 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1163 {
1164 /* min_char_or_byte2 specifies the linear character index
1165 corresponding to the first element of the per_char array,
1166 max_char_or_byte2 is the index of the last character. A
1167 character with non-zero CHAR2B->byte1 is not in the font.
1168 A character with byte2 less than min_char_or_byte2 or
1169 greater max_char_or_byte2 is not in the font. */
1170 if (char2b->byte1 == 0
1171 && char2b->byte2 >= font->min_char_or_byte2
1172 && char2b->byte2 <= font->max_char_or_byte2)
1173 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1174 }
1175 else
1176 {
1177 /* If either min_byte1 or max_byte1 are nonzero, both
1178 min_char_or_byte2 and max_char_or_byte2 are less than
1179 256, and the 2-byte character index values corresponding
1180 to the per_char array element N (counting from 0) are:
1181
1182 byte1 = N/D + min_byte1
1183 byte2 = N\D + min_char_or_byte2
1184
1185 where:
1186
1187 D = max_char_or_byte2 - min_char_or_byte2 + 1
1188 / = integer division
1189 \ = integer modulus */
1190 if (char2b->byte1 >= font->min_byte1
1191 && char2b->byte1 <= font->max_byte1
1192 && char2b->byte2 >= font->min_char_or_byte2
1193 && char2b->byte2 <= font->max_char_or_byte2)
1194 {
1195 pcm = (font->per_char
1196 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1197 * (char2b->byte1 - font->min_byte1))
1198 + (char2b->byte2 - font->min_char_or_byte2));
1199 }
1200 }
1201 }
1202 else
1203 {
1204 /* If the per_char pointer is null, all glyphs between the first
1205 and last character indexes inclusive have the same
1206 information, as given by both min_bounds and max_bounds. */
1207 if (char2b->byte2 >= font->min_char_or_byte2
1208 && char2b->byte2 <= font->max_char_or_byte2)
1209 pcm = &font->max_bounds;
1210 }
1211
1212 return ((pcm == NULL
1213 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1214 ? NULL : pcm);
1215 }
1216
1217
1218 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1219 the two-byte form of C. Encoding is returned in *CHAR2B. */
1220
1221 static INLINE void
1222 x_encode_char (c, char2b, font_info)
1223 int c;
1224 XChar2b *char2b;
1225 struct font_info *font_info;
1226 {
1227 int charset = CHAR_CHARSET (c);
1228 XFontStruct *font = font_info->font;
1229
1230 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1231 This may be either a program in a special encoder language or a
1232 fixed encoding. */
1233 if (font_info->font_encoder)
1234 {
1235 /* It's a program. */
1236 struct ccl_program *ccl = font_info->font_encoder;
1237
1238 if (CHARSET_DIMENSION (charset) == 1)
1239 {
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte2;
1242 }
1243 else
1244 {
1245 ccl->reg[0] = charset;
1246 ccl->reg[1] = char2b->byte1;
1247 ccl->reg[2] = char2b->byte2;
1248 }
1249
1250 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1251
1252 /* We assume that MSBs are appropriately set/reset by CCL
1253 program. */
1254 if (font->max_byte1 == 0) /* 1-byte font */
1255 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1256 else
1257 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1258 }
1259 else if (font_info->encoding[charset])
1260 {
1261 /* Fixed encoding scheme. See fontset.h for the meaning of the
1262 encoding numbers. */
1263 int enc = font_info->encoding[charset];
1264
1265 if ((enc == 1 || enc == 2)
1266 && CHARSET_DIMENSION (charset) == 2)
1267 char2b->byte1 |= 0x80;
1268
1269 if (enc == 1 || enc == 3)
1270 char2b->byte2 |= 0x80;
1271 }
1272 }
1273
1274
1275 /* Get face and two-byte form of character C in face FACE_ID on frame
1276 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1277 means we want to display multibyte text. Value is a pointer to a
1278 realized face that is ready for display. */
1279
1280 static INLINE struct face *
1281 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1282 struct frame *f;
1283 int c, face_id;
1284 XChar2b *char2b;
1285 int multibyte_p;
1286 {
1287 struct face *face = FACE_FROM_ID (f, face_id);
1288
1289 if (!multibyte_p)
1290 {
1291 /* Unibyte case. We don't have to encode, but we have to make
1292 sure to use a face suitable for unibyte. */
1293 char2b->byte1 = 0;
1294 char2b->byte2 = c;
1295 face_id = FACE_FOR_CHAR (f, face, c);
1296 face = FACE_FROM_ID (f, face_id);
1297 }
1298 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1299 {
1300 /* Case of ASCII in a face known to fit ASCII. */
1301 char2b->byte1 = 0;
1302 char2b->byte2 = c;
1303 }
1304 else
1305 {
1306 int c1, c2, charset;
1307
1308 /* Split characters into bytes. If c2 is -1 afterwards, C is
1309 really a one-byte character so that byte1 is zero. */
1310 SPLIT_CHAR (c, charset, c1, c2);
1311 if (c2 > 0)
1312 char2b->byte1 = c1, char2b->byte2 = c2;
1313 else
1314 char2b->byte1 = 0, char2b->byte2 = c1;
1315
1316 /* Maybe encode the character in *CHAR2B. */
1317 if (face->font != NULL)
1318 {
1319 struct font_info *font_info
1320 = FONT_INFO_FROM_ID (f, face->font_info_id);
1321 if (font_info)
1322 x_encode_char (c, char2b, font_info);
1323 }
1324 }
1325
1326 /* Make sure X resources of the face are allocated. */
1327 xassert (face != NULL);
1328 PREPARE_FACE_FOR_DISPLAY (f, face);
1329
1330 return face;
1331 }
1332
1333
1334 /* Get face and two-byte form of character glyph GLYPH on frame F.
1335 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1336 a pointer to a realized face that is ready for display. */
1337
1338 static INLINE struct face *
1339 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1340 struct frame *f;
1341 struct glyph *glyph;
1342 XChar2b *char2b;
1343 int *two_byte_p;
1344 {
1345 struct face *face;
1346
1347 xassert (glyph->type == CHAR_GLYPH);
1348 face = FACE_FROM_ID (f, glyph->face_id);
1349
1350 if (two_byte_p)
1351 *two_byte_p = 0;
1352
1353 if (!glyph->multibyte_p)
1354 {
1355 /* Unibyte case. We don't have to encode, but we have to make
1356 sure to use a face suitable for unibyte. */
1357 char2b->byte1 = 0;
1358 char2b->byte2 = glyph->u.ch;
1359 }
1360 else if (glyph->u.ch < 128
1361 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1362 {
1363 /* Case of ASCII in a face known to fit ASCII. */
1364 char2b->byte1 = 0;
1365 char2b->byte2 = glyph->u.ch;
1366 }
1367 else
1368 {
1369 int c1, c2, charset;
1370
1371 /* Split characters into bytes. If c2 is -1 afterwards, C is
1372 really a one-byte character so that byte1 is zero. */
1373 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1374 if (c2 > 0)
1375 char2b->byte1 = c1, char2b->byte2 = c2;
1376 else
1377 char2b->byte1 = 0, char2b->byte2 = c1;
1378
1379 /* Maybe encode the character in *CHAR2B. */
1380 if (charset != CHARSET_ASCII)
1381 {
1382 struct font_info *font_info
1383 = FONT_INFO_FROM_ID (f, face->font_info_id);
1384 if (font_info)
1385 {
1386 x_encode_char (glyph->u.ch, char2b, font_info);
1387 if (two_byte_p)
1388 *two_byte_p
1389 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1390 }
1391 }
1392 }
1393
1394 /* Make sure X resources of the face are allocated. */
1395 xassert (face != NULL);
1396 PREPARE_FACE_FOR_DISPLAY (f, face);
1397 return face;
1398 }
1399
1400
1401 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1402 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1403
1404 static INLINE void
1405 x_append_glyph (it)
1406 struct it *it;
1407 {
1408 struct glyph *glyph;
1409 enum glyph_row_area area = it->area;
1410
1411 xassert (it->glyph_row);
1412 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1413
1414 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1415 if (glyph < it->glyph_row->glyphs[area + 1])
1416 {
1417 glyph->charpos = CHARPOS (it->position);
1418 glyph->object = it->object;
1419 glyph->pixel_width = it->pixel_width;
1420 glyph->voffset = it->voffset;
1421 glyph->type = CHAR_GLYPH;
1422 glyph->multibyte_p = it->multibyte_p;
1423 glyph->left_box_line_p = it->start_of_box_run_p;
1424 glyph->right_box_line_p = it->end_of_box_run_p;
1425 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1426 || it->phys_descent > it->descent);
1427 glyph->padding_p = 0;
1428 glyph->glyph_not_available_p = it->glyph_not_available_p;
1429 glyph->face_id = it->face_id;
1430 glyph->u.ch = it->char_to_display;
1431 ++it->glyph_row->used[area];
1432 }
1433 }
1434
1435 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1436 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1437
1438 static INLINE void
1439 x_append_composite_glyph (it)
1440 struct it *it;
1441 {
1442 struct glyph *glyph;
1443 enum glyph_row_area area = it->area;
1444
1445 xassert (it->glyph_row);
1446
1447 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1448 if (glyph < it->glyph_row->glyphs[area + 1])
1449 {
1450 glyph->charpos = CHARPOS (it->position);
1451 glyph->object = it->object;
1452 glyph->pixel_width = it->pixel_width;
1453 glyph->voffset = it->voffset;
1454 glyph->type = COMPOSITE_GLYPH;
1455 glyph->multibyte_p = it->multibyte_p;
1456 glyph->left_box_line_p = it->start_of_box_run_p;
1457 glyph->right_box_line_p = it->end_of_box_run_p;
1458 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1459 || it->phys_descent > it->descent);
1460 glyph->padding_p = 0;
1461 glyph->glyph_not_available_p = 0;
1462 glyph->face_id = it->face_id;
1463 glyph->u.cmp_id = it->cmp_id;
1464 ++it->glyph_row->used[area];
1465 }
1466 }
1467
1468
1469 /* Change IT->ascent and IT->height according to the setting of
1470 IT->voffset. */
1471
1472 static INLINE void
1473 take_vertical_position_into_account (it)
1474 struct it *it;
1475 {
1476 if (it->voffset)
1477 {
1478 if (it->voffset < 0)
1479 /* Increase the ascent so that we can display the text higher
1480 in the line. */
1481 it->ascent += abs (it->voffset);
1482 else
1483 /* Increase the descent so that we can display the text lower
1484 in the line. */
1485 it->descent += it->voffset;
1486 }
1487 }
1488
1489
1490 /* Produce glyphs/get display metrics for the image IT is loaded with.
1491 See the description of struct display_iterator in dispextern.h for
1492 an overview of struct display_iterator. */
1493
1494 static void
1495 x_produce_image_glyph (it)
1496 struct it *it;
1497 {
1498 struct image *img;
1499 struct face *face;
1500
1501 xassert (it->what == IT_IMAGE);
1502
1503 face = FACE_FROM_ID (it->f, it->face_id);
1504 img = IMAGE_FROM_ID (it->f, it->image_id);
1505 xassert (img);
1506
1507 /* Make sure X resources of the face and image are loaded. */
1508 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1509 prepare_image_for_display (it->f, img);
1510
1511 it->ascent = it->phys_ascent = image_ascent (img, face);
1512 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1513 it->pixel_width = img->width + 2 * img->hmargin;
1514
1515 it->nglyphs = 1;
1516
1517 if (face->box != FACE_NO_BOX)
1518 {
1519 if (face->box_line_width > 0)
1520 {
1521 it->ascent += face->box_line_width;
1522 it->descent += face->box_line_width;
1523 }
1524
1525 if (it->start_of_box_run_p)
1526 it->pixel_width += abs (face->box_line_width);
1527 if (it->end_of_box_run_p)
1528 it->pixel_width += abs (face->box_line_width);
1529 }
1530
1531 take_vertical_position_into_account (it);
1532
1533 if (it->glyph_row)
1534 {
1535 struct glyph *glyph;
1536 enum glyph_row_area area = it->area;
1537
1538 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1539 if (glyph < it->glyph_row->glyphs[area + 1])
1540 {
1541 glyph->charpos = CHARPOS (it->position);
1542 glyph->object = it->object;
1543 glyph->pixel_width = it->pixel_width;
1544 glyph->voffset = it->voffset;
1545 glyph->type = IMAGE_GLYPH;
1546 glyph->multibyte_p = it->multibyte_p;
1547 glyph->left_box_line_p = it->start_of_box_run_p;
1548 glyph->right_box_line_p = it->end_of_box_run_p;
1549 glyph->overlaps_vertically_p = 0;
1550 glyph->padding_p = 0;
1551 glyph->glyph_not_available_p = 0;
1552 glyph->face_id = it->face_id;
1553 glyph->u.img_id = img->id;
1554 ++it->glyph_row->used[area];
1555 }
1556 }
1557 }
1558
1559
1560 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1561 of the glyph, WIDTH and HEIGHT are the width and height of the
1562 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1563 ascent of the glyph (0 <= ASCENT <= 1). */
1564
1565 static void
1566 x_append_stretch_glyph (it, object, width, height, ascent)
1567 struct it *it;
1568 Lisp_Object object;
1569 int width, height;
1570 double ascent;
1571 {
1572 struct glyph *glyph;
1573 enum glyph_row_area area = it->area;
1574
1575 xassert (ascent >= 0 && ascent <= 1);
1576
1577 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1578 if (glyph < it->glyph_row->glyphs[area + 1])
1579 {
1580 glyph->charpos = CHARPOS (it->position);
1581 glyph->object = object;
1582 glyph->pixel_width = width;
1583 glyph->voffset = it->voffset;
1584 glyph->type = STRETCH_GLYPH;
1585 glyph->multibyte_p = it->multibyte_p;
1586 glyph->left_box_line_p = it->start_of_box_run_p;
1587 glyph->right_box_line_p = it->end_of_box_run_p;
1588 glyph->overlaps_vertically_p = 0;
1589 glyph->padding_p = 0;
1590 glyph->glyph_not_available_p = 0;
1591 glyph->face_id = it->face_id;
1592 glyph->u.stretch.ascent = height * ascent;
1593 glyph->u.stretch.height = height;
1594 ++it->glyph_row->used[area];
1595 }
1596 }
1597
1598
1599 /* Produce a stretch glyph for iterator IT. IT->object is the value
1600 of the glyph property displayed. The value must be a list
1601 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1602 being recognized:
1603
1604 1. `:width WIDTH' specifies that the space should be WIDTH *
1605 canonical char width wide. WIDTH may be an integer or floating
1606 point number.
1607
1608 2. `:relative-width FACTOR' specifies that the width of the stretch
1609 should be computed from the width of the first character having the
1610 `glyph' property, and should be FACTOR times that width.
1611
1612 3. `:align-to HPOS' specifies that the space should be wide enough
1613 to reach HPOS, a value in canonical character units.
1614
1615 Exactly one of the above pairs must be present.
1616
1617 4. `:height HEIGHT' specifies that the height of the stretch produced
1618 should be HEIGHT, measured in canonical character units.
1619
1620 5. `:relative-height FACTOR' specifies that the height of the the
1621 stretch should be FACTOR times the height of the characters having
1622 the glyph property.
1623
1624 Either none or exactly one of 4 or 5 must be present.
1625
1626 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1627 of the stretch should be used for the ascent of the stretch.
1628 ASCENT must be in the range 0 <= ASCENT <= 100. */
1629
1630 #define NUMVAL(X) \
1631 ((INTEGERP (X) || FLOATP (X)) \
1632 ? XFLOATINT (X) \
1633 : - 1)
1634
1635
1636 static void
1637 x_produce_stretch_glyph (it)
1638 struct it *it;
1639 {
1640 /* (space :width WIDTH :height HEIGHT. */
1641 #if GLYPH_DEBUG
1642 extern Lisp_Object Qspace;
1643 #endif
1644 extern Lisp_Object QCwidth, QCheight, QCascent;
1645 extern Lisp_Object QCrelative_width, QCrelative_height;
1646 extern Lisp_Object QCalign_to;
1647 Lisp_Object prop, plist;
1648 double width = 0, height = 0, ascent = 0;
1649 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1650 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1651
1652 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1653
1654 /* List should start with `space'. */
1655 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1656 plist = XCDR (it->object);
1657
1658 /* Compute the width of the stretch. */
1659 if (prop = Fplist_get (plist, QCwidth),
1660 NUMVAL (prop) > 0)
1661 /* Absolute width `:width WIDTH' specified and valid. */
1662 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1663 else if (prop = Fplist_get (plist, QCrelative_width),
1664 NUMVAL (prop) > 0)
1665 {
1666 /* Relative width `:relative-width FACTOR' specified and valid.
1667 Compute the width of the characters having the `glyph'
1668 property. */
1669 struct it it2;
1670 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1671
1672 it2 = *it;
1673 if (it->multibyte_p)
1674 {
1675 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1676 - IT_BYTEPOS (*it));
1677 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1678 }
1679 else
1680 it2.c = *p, it2.len = 1;
1681
1682 it2.glyph_row = NULL;
1683 it2.what = IT_CHARACTER;
1684 x_produce_glyphs (&it2);
1685 width = NUMVAL (prop) * it2.pixel_width;
1686 }
1687 else if (prop = Fplist_get (plist, QCalign_to),
1688 NUMVAL (prop) > 0)
1689 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1690 else
1691 /* Nothing specified -> width defaults to canonical char width. */
1692 width = CANON_X_UNIT (it->f);
1693
1694 /* Compute height. */
1695 if (prop = Fplist_get (plist, QCheight),
1696 NUMVAL (prop) > 0)
1697 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1698 else if (prop = Fplist_get (plist, QCrelative_height),
1699 NUMVAL (prop) > 0)
1700 height = FONT_HEIGHT (font) * NUMVAL (prop);
1701 else
1702 height = FONT_HEIGHT (font);
1703
1704 /* Compute percentage of height used for ascent. If
1705 `:ascent ASCENT' is present and valid, use that. Otherwise,
1706 derive the ascent from the font in use. */
1707 if (prop = Fplist_get (plist, QCascent),
1708 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1709 ascent = NUMVAL (prop) / 100.0;
1710 else
1711 ascent = (double) font->ascent / FONT_HEIGHT (font);
1712
1713 if (width <= 0)
1714 width = 1;
1715 if (height <= 0)
1716 height = 1;
1717
1718 if (it->glyph_row)
1719 {
1720 Lisp_Object object = it->stack[it->sp - 1].string;
1721 if (!STRINGP (object))
1722 object = it->w->buffer;
1723 x_append_stretch_glyph (it, object, width, height, ascent);
1724 }
1725
1726 it->pixel_width = width;
1727 it->ascent = it->phys_ascent = height * ascent;
1728 it->descent = it->phys_descent = height - it->ascent;
1729 it->nglyphs = 1;
1730
1731 if (face->box != FACE_NO_BOX)
1732 {
1733 if (face->box_line_width > 0)
1734 {
1735 it->ascent += face->box_line_width;
1736 it->descent += face->box_line_width;
1737 }
1738
1739 if (it->start_of_box_run_p)
1740 it->pixel_width += abs (face->box_line_width);
1741 if (it->end_of_box_run_p)
1742 it->pixel_width += abs (face->box_line_width);
1743 }
1744
1745 take_vertical_position_into_account (it);
1746 }
1747
1748 /* Return proper value to be used as baseline offset of font that has
1749 ASCENT and DESCENT to draw characters by the font at the vertical
1750 center of the line of frame F.
1751
1752 Here, out task is to find the value of BOFF in the following figure;
1753
1754 -------------------------+-----------+-
1755 -+-+---------+-+ | |
1756 | | | | | |
1757 | | | | F_ASCENT F_HEIGHT
1758 | | | ASCENT | |
1759 HEIGHT | | | | |
1760 | | |-|-+------+-----------|------- baseline
1761 | | | | BOFF | |
1762 | |---------|-+-+ | |
1763 | | | DESCENT | |
1764 -+-+---------+-+ F_DESCENT |
1765 -------------------------+-----------+-
1766
1767 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1768 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1769 DESCENT = FONT->descent
1770 HEIGHT = FONT_HEIGHT (FONT)
1771 F_DESCENT = (F->output_data.x->font->descent
1772 - F->output_data.x->baseline_offset)
1773 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1774 */
1775
1776 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1777 ((FONT)->descent \
1778 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1779 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1780 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1781
1782 /* Produce glyphs/get display metrics for the display element IT is
1783 loaded with. See the description of struct display_iterator in
1784 dispextern.h for an overview of struct display_iterator. */
1785
1786 static void
1787 x_produce_glyphs (it)
1788 struct it *it;
1789 {
1790 it->glyph_not_available_p = 0;
1791
1792 if (it->what == IT_CHARACTER)
1793 {
1794 XChar2b char2b;
1795 XFontStruct *font;
1796 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1797 XCharStruct *pcm;
1798 int font_not_found_p;
1799 struct font_info *font_info;
1800 int boff; /* baseline offset */
1801 /* We may change it->multibyte_p upon unibyte<->multibyte
1802 conversion. So, save the current value now and restore it
1803 later.
1804
1805 Note: It seems that we don't have to record multibyte_p in
1806 struct glyph because the character code itself tells if or
1807 not the character is multibyte. Thus, in the future, we must
1808 consider eliminating the field `multibyte_p' in the struct
1809 glyph.
1810 */
1811 int saved_multibyte_p = it->multibyte_p;
1812
1813 /* Maybe translate single-byte characters to multibyte, or the
1814 other way. */
1815 it->char_to_display = it->c;
1816 if (!ASCII_BYTE_P (it->c))
1817 {
1818 if (unibyte_display_via_language_environment
1819 && SINGLE_BYTE_CHAR_P (it->c)
1820 && (it->c >= 0240
1821 || !NILP (Vnonascii_translation_table)))
1822 {
1823 it->char_to_display = unibyte_char_to_multibyte (it->c);
1824 it->multibyte_p = 1;
1825 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1826 face = FACE_FROM_ID (it->f, it->face_id);
1827 }
1828 else if (!SINGLE_BYTE_CHAR_P (it->c)
1829 && !it->multibyte_p)
1830 {
1831 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1832 it->multibyte_p = 0;
1833 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1834 face = FACE_FROM_ID (it->f, it->face_id);
1835 }
1836 }
1837
1838 /* Get font to use. Encode IT->char_to_display. */
1839 x_get_char_face_and_encoding (it->f, it->char_to_display,
1840 it->face_id, &char2b,
1841 it->multibyte_p);
1842 font = face->font;
1843
1844 /* When no suitable font found, use the default font. */
1845 font_not_found_p = font == NULL;
1846 if (font_not_found_p)
1847 {
1848 font = FRAME_FONT (it->f);
1849 boff = it->f->output_data.x->baseline_offset;
1850 font_info = NULL;
1851 }
1852 else
1853 {
1854 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1855 boff = font_info->baseline_offset;
1856 if (font_info->vertical_centering)
1857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1858 }
1859
1860 if (it->char_to_display >= ' '
1861 && (!it->multibyte_p || it->char_to_display < 128))
1862 {
1863 /* Either unibyte or ASCII. */
1864 int stretched_p;
1865
1866 it->nglyphs = 1;
1867
1868 pcm = x_per_char_metric (font, &char2b);
1869 it->ascent = font->ascent + boff;
1870 it->descent = font->descent - boff;
1871
1872 if (pcm)
1873 {
1874 it->phys_ascent = pcm->ascent + boff;
1875 it->phys_descent = pcm->descent - boff;
1876 it->pixel_width = pcm->width;
1877 }
1878 else
1879 {
1880 it->glyph_not_available_p = 1;
1881 it->phys_ascent = font->ascent + boff;
1882 it->phys_descent = font->descent - boff;
1883 it->pixel_width = FONT_WIDTH (font);
1884 }
1885
1886 /* If this is a space inside a region of text with
1887 `space-width' property, change its width. */
1888 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1889 if (stretched_p)
1890 it->pixel_width *= XFLOATINT (it->space_width);
1891
1892 /* If face has a box, add the box thickness to the character
1893 height. If character has a box line to the left and/or
1894 right, add the box line width to the character's width. */
1895 if (face->box != FACE_NO_BOX)
1896 {
1897 int thick = face->box_line_width;
1898
1899 if (thick > 0)
1900 {
1901 it->ascent += thick;
1902 it->descent += thick;
1903 }
1904 else
1905 thick = -thick;
1906
1907 if (it->start_of_box_run_p)
1908 it->pixel_width += thick;
1909 if (it->end_of_box_run_p)
1910 it->pixel_width += thick;
1911 }
1912
1913 /* If face has an overline, add the height of the overline
1914 (1 pixel) and a 1 pixel margin to the character height. */
1915 if (face->overline_p)
1916 it->ascent += 2;
1917
1918 take_vertical_position_into_account (it);
1919
1920 /* If we have to actually produce glyphs, do it. */
1921 if (it->glyph_row)
1922 {
1923 if (stretched_p)
1924 {
1925 /* Translate a space with a `space-width' property
1926 into a stretch glyph. */
1927 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1928 x_append_stretch_glyph (it, it->object, it->pixel_width,
1929 it->ascent + it->descent, ascent);
1930 }
1931 else
1932 x_append_glyph (it);
1933
1934 /* If characters with lbearing or rbearing are displayed
1935 in this line, record that fact in a flag of the
1936 glyph row. This is used to optimize X output code. */
1937 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1938 it->glyph_row->contains_overlapping_glyphs_p = 1;
1939 }
1940 }
1941 else if (it->char_to_display == '\n')
1942 {
1943 /* A newline has no width but we need the height of the line. */
1944 it->pixel_width = 0;
1945 it->nglyphs = 0;
1946 it->ascent = it->phys_ascent = font->ascent + boff;
1947 it->descent = it->phys_descent = font->descent - boff;
1948
1949 if (face->box != FACE_NO_BOX
1950 && face->box_line_width > 0)
1951 {
1952 it->ascent += face->box_line_width;
1953 it->descent += face->box_line_width;
1954 }
1955 }
1956 else if (it->char_to_display == '\t')
1957 {
1958 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1959 int x = it->current_x + it->continuation_lines_width;
1960 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1961
1962 /* If the distance from the current position to the next tab
1963 stop is less than a canonical character width, use the
1964 tab stop after that. */
1965 if (next_tab_x - x < CANON_X_UNIT (it->f))
1966 next_tab_x += tab_width;
1967
1968 it->pixel_width = next_tab_x - x;
1969 it->nglyphs = 1;
1970 it->ascent = it->phys_ascent = font->ascent + boff;
1971 it->descent = it->phys_descent = font->descent - boff;
1972
1973 if (it->glyph_row)
1974 {
1975 double ascent = (double) it->ascent / (it->ascent + it->descent);
1976 x_append_stretch_glyph (it, it->object, it->pixel_width,
1977 it->ascent + it->descent, ascent);
1978 }
1979 }
1980 else
1981 {
1982 /* A multi-byte character. Assume that the display width of the
1983 character is the width of the character multiplied by the
1984 width of the font. */
1985
1986 /* If we found a font, this font should give us the right
1987 metrics. If we didn't find a font, use the frame's
1988 default font and calculate the width of the character
1989 from the charset width; this is what old redisplay code
1990 did. */
1991 pcm = x_per_char_metric (font, &char2b);
1992 if (font_not_found_p || !pcm)
1993 {
1994 int charset = CHAR_CHARSET (it->char_to_display);
1995
1996 it->glyph_not_available_p = 1;
1997 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1998 * CHARSET_WIDTH (charset));
1999 it->phys_ascent = font->ascent + boff;
2000 it->phys_descent = font->descent - boff;
2001 }
2002 else
2003 {
2004 it->pixel_width = pcm->width;
2005 it->phys_ascent = pcm->ascent + boff;
2006 it->phys_descent = pcm->descent - boff;
2007 if (it->glyph_row
2008 && (pcm->lbearing < 0
2009 || pcm->rbearing > pcm->width))
2010 it->glyph_row->contains_overlapping_glyphs_p = 1;
2011 }
2012 it->nglyphs = 1;
2013 it->ascent = font->ascent + boff;
2014 it->descent = font->descent - boff;
2015 if (face->box != FACE_NO_BOX)
2016 {
2017 int thick = face->box_line_width;
2018
2019 if (thick > 0)
2020 {
2021 it->ascent += thick;
2022 it->descent += thick;
2023 }
2024 else
2025 thick = - thick;
2026
2027 if (it->start_of_box_run_p)
2028 it->pixel_width += thick;
2029 if (it->end_of_box_run_p)
2030 it->pixel_width += thick;
2031 }
2032
2033 /* If face has an overline, add the height of the overline
2034 (1 pixel) and a 1 pixel margin to the character height. */
2035 if (face->overline_p)
2036 it->ascent += 2;
2037
2038 take_vertical_position_into_account (it);
2039
2040 if (it->glyph_row)
2041 x_append_glyph (it);
2042 }
2043 it->multibyte_p = saved_multibyte_p;
2044 }
2045 else if (it->what == IT_COMPOSITION)
2046 {
2047 /* Note: A composition is represented as one glyph in the
2048 glyph matrix. There are no padding glyphs. */
2049 XChar2b char2b;
2050 XFontStruct *font;
2051 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2052 XCharStruct *pcm;
2053 int font_not_found_p;
2054 struct font_info *font_info;
2055 int boff; /* baseline offset */
2056 struct composition *cmp = composition_table[it->cmp_id];
2057
2058 /* Maybe translate single-byte characters to multibyte. */
2059 it->char_to_display = it->c;
2060 if (unibyte_display_via_language_environment
2061 && SINGLE_BYTE_CHAR_P (it->c)
2062 && (it->c >= 0240
2063 || (it->c >= 0200
2064 && !NILP (Vnonascii_translation_table))))
2065 {
2066 it->char_to_display = unibyte_char_to_multibyte (it->c);
2067 }
2068
2069 /* Get face and font to use. Encode IT->char_to_display. */
2070 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2071 face = FACE_FROM_ID (it->f, it->face_id);
2072 x_get_char_face_and_encoding (it->f, it->char_to_display,
2073 it->face_id, &char2b, it->multibyte_p);
2074 font = face->font;
2075
2076 /* When no suitable font found, use the default font. */
2077 font_not_found_p = font == NULL;
2078 if (font_not_found_p)
2079 {
2080 font = FRAME_FONT (it->f);
2081 boff = it->f->output_data.x->baseline_offset;
2082 font_info = NULL;
2083 }
2084 else
2085 {
2086 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2087 boff = font_info->baseline_offset;
2088 if (font_info->vertical_centering)
2089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2090 }
2091
2092 /* There are no padding glyphs, so there is only one glyph to
2093 produce for the composition. Important is that pixel_width,
2094 ascent and descent are the values of what is drawn by
2095 draw_glyphs (i.e. the values of the overall glyphs composed). */
2096 it->nglyphs = 1;
2097
2098 /* If we have not yet calculated pixel size data of glyphs of
2099 the composition for the current face font, calculate them
2100 now. Theoretically, we have to check all fonts for the
2101 glyphs, but that requires much time and memory space. So,
2102 here we check only the font of the first glyph. This leads
2103 to incorrect display very rarely, and C-l (recenter) can
2104 correct the display anyway. */
2105 if (cmp->font != (void *) font)
2106 {
2107 /* Ascent and descent of the font of the first character of
2108 this composition (adjusted by baseline offset). Ascent
2109 and descent of overall glyphs should not be less than
2110 them respectively. */
2111 int font_ascent = font->ascent + boff;
2112 int font_descent = font->descent - boff;
2113 /* Bounding box of the overall glyphs. */
2114 int leftmost, rightmost, lowest, highest;
2115 int i, width, ascent, descent;
2116
2117 cmp->font = (void *) font;
2118
2119 /* Initialize the bounding box. */
2120 if (font_info
2121 && (pcm = x_per_char_metric (font, &char2b)))
2122 {
2123 width = pcm->width;
2124 ascent = pcm->ascent;
2125 descent = pcm->descent;
2126 }
2127 else
2128 {
2129 width = FONT_WIDTH (font);
2130 ascent = font->ascent;
2131 descent = font->descent;
2132 }
2133
2134 rightmost = width;
2135 lowest = - descent + boff;
2136 highest = ascent + boff;
2137 leftmost = 0;
2138
2139 if (font_info
2140 && font_info->default_ascent
2141 && CHAR_TABLE_P (Vuse_default_ascent)
2142 && !NILP (Faref (Vuse_default_ascent,
2143 make_number (it->char_to_display))))
2144 highest = font_info->default_ascent + boff;
2145
2146 /* Draw the first glyph at the normal position. It may be
2147 shifted to right later if some other glyphs are drawn at
2148 the left. */
2149 cmp->offsets[0] = 0;
2150 cmp->offsets[1] = boff;
2151
2152 /* Set cmp->offsets for the remaining glyphs. */
2153 for (i = 1; i < cmp->glyph_len; i++)
2154 {
2155 int left, right, btm, top;
2156 int ch = COMPOSITION_GLYPH (cmp, i);
2157 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2158
2159 face = FACE_FROM_ID (it->f, face_id);
2160 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2161 it->multibyte_p);
2162 font = face->font;
2163 if (font == NULL)
2164 {
2165 font = FRAME_FONT (it->f);
2166 boff = it->f->output_data.x->baseline_offset;
2167 font_info = NULL;
2168 }
2169 else
2170 {
2171 font_info
2172 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2173 boff = font_info->baseline_offset;
2174 if (font_info->vertical_centering)
2175 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2176 }
2177
2178 if (font_info
2179 && (pcm = x_per_char_metric (font, &char2b)))
2180 {
2181 width = pcm->width;
2182 ascent = pcm->ascent;
2183 descent = pcm->descent;
2184 }
2185 else
2186 {
2187 width = FONT_WIDTH (font);
2188 ascent = 1;
2189 descent = 0;
2190 }
2191
2192 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2193 {
2194 /* Relative composition with or without
2195 alternate chars. */
2196 left = (leftmost + rightmost - width) / 2;
2197 btm = - descent + boff;
2198 if (font_info && font_info->relative_compose
2199 && (! CHAR_TABLE_P (Vignore_relative_composition)
2200 || NILP (Faref (Vignore_relative_composition,
2201 make_number (ch)))))
2202 {
2203
2204 if (- descent >= font_info->relative_compose)
2205 /* One extra pixel between two glyphs. */
2206 btm = highest + 1;
2207 else if (ascent <= 0)
2208 /* One extra pixel between two glyphs. */
2209 btm = lowest - 1 - ascent - descent;
2210 }
2211 }
2212 else
2213 {
2214 /* A composition rule is specified by an integer
2215 value that encodes global and new reference
2216 points (GREF and NREF). GREF and NREF are
2217 specified by numbers as below:
2218
2219 0---1---2 -- ascent
2220 | |
2221 | |
2222 | |
2223 9--10--11 -- center
2224 | |
2225 ---3---4---5--- baseline
2226 | |
2227 6---7---8 -- descent
2228 */
2229 int rule = COMPOSITION_RULE (cmp, i);
2230 int gref, nref, grefx, grefy, nrefx, nrefy;
2231
2232 COMPOSITION_DECODE_RULE (rule, gref, nref);
2233 grefx = gref % 3, nrefx = nref % 3;
2234 grefy = gref / 3, nrefy = nref / 3;
2235
2236 left = (leftmost
2237 + grefx * (rightmost - leftmost) / 2
2238 - nrefx * width / 2);
2239 btm = ((grefy == 0 ? highest
2240 : grefy == 1 ? 0
2241 : grefy == 2 ? lowest
2242 : (highest + lowest) / 2)
2243 - (nrefy == 0 ? ascent + descent
2244 : nrefy == 1 ? descent - boff
2245 : nrefy == 2 ? 0
2246 : (ascent + descent) / 2));
2247 }
2248
2249 cmp->offsets[i * 2] = left;
2250 cmp->offsets[i * 2 + 1] = btm + descent;
2251
2252 /* Update the bounding box of the overall glyphs. */
2253 right = left + width;
2254 top = btm + descent + ascent;
2255 if (left < leftmost)
2256 leftmost = left;
2257 if (right > rightmost)
2258 rightmost = right;
2259 if (top > highest)
2260 highest = top;
2261 if (btm < lowest)
2262 lowest = btm;
2263 }
2264
2265 /* If there are glyphs whose x-offsets are negative,
2266 shift all glyphs to the right and make all x-offsets
2267 non-negative. */
2268 if (leftmost < 0)
2269 {
2270 for (i = 0; i < cmp->glyph_len; i++)
2271 cmp->offsets[i * 2] -= leftmost;
2272 rightmost -= leftmost;
2273 }
2274
2275 cmp->pixel_width = rightmost;
2276 cmp->ascent = highest;
2277 cmp->descent = - lowest;
2278 if (cmp->ascent < font_ascent)
2279 cmp->ascent = font_ascent;
2280 if (cmp->descent < font_descent)
2281 cmp->descent = font_descent;
2282 }
2283
2284 it->pixel_width = cmp->pixel_width;
2285 it->ascent = it->phys_ascent = cmp->ascent;
2286 it->descent = it->phys_descent = cmp->descent;
2287
2288 if (face->box != FACE_NO_BOX)
2289 {
2290 int thick = face->box_line_width;
2291
2292 if (thick > 0)
2293 {
2294 it->ascent += thick;
2295 it->descent += thick;
2296 }
2297 else
2298 thick = - thick;
2299
2300 if (it->start_of_box_run_p)
2301 it->pixel_width += thick;
2302 if (it->end_of_box_run_p)
2303 it->pixel_width += thick;
2304 }
2305
2306 /* If face has an overline, add the height of the overline
2307 (1 pixel) and a 1 pixel margin to the character height. */
2308 if (face->overline_p)
2309 it->ascent += 2;
2310
2311 take_vertical_position_into_account (it);
2312
2313 if (it->glyph_row)
2314 x_append_composite_glyph (it);
2315 }
2316 else if (it->what == IT_IMAGE)
2317 x_produce_image_glyph (it);
2318 else if (it->what == IT_STRETCH)
2319 x_produce_stretch_glyph (it);
2320
2321 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2322 because this isn't true for images with `:ascent 100'. */
2323 xassert (it->ascent >= 0 && it->descent >= 0);
2324 if (it->area == TEXT_AREA)
2325 it->current_x += it->pixel_width;
2326
2327 it->descent += it->extra_line_spacing;
2328
2329 it->max_ascent = max (it->max_ascent, it->ascent);
2330 it->max_descent = max (it->max_descent, it->descent);
2331 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2332 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2333 }
2334
2335
2336 /* Estimate the pixel height of the mode or top line on frame F.
2337 FACE_ID specifies what line's height to estimate. */
2338
2339 int
2340 x_estimate_mode_line_height (f, face_id)
2341 struct frame *f;
2342 enum face_id face_id;
2343 {
2344 int height = FONT_HEIGHT (FRAME_FONT (f));
2345
2346 /* This function is called so early when Emacs starts that the face
2347 cache and mode line face are not yet initialized. */
2348 if (FRAME_FACE_CACHE (f))
2349 {
2350 struct face *face = FACE_FROM_ID (f, face_id);
2351 if (face)
2352 {
2353 if (face->font)
2354 height = FONT_HEIGHT (face->font);
2355 if (face->box_line_width > 0)
2356 height += 2 * face->box_line_width;
2357 }
2358 }
2359
2360 return height;
2361 }
2362
2363 \f
2364 /***********************************************************************
2365 Glyph display
2366 ***********************************************************************/
2367
2368 /* A sequence of glyphs to be drawn in the same face.
2369
2370 This data structure is not really completely X specific, so it
2371 could possibly, at least partially, be useful for other systems. It
2372 is currently not part of the external redisplay interface because
2373 it's not clear what other systems will need. */
2374
2375 struct glyph_string
2376 {
2377 /* X-origin of the string. */
2378 int x;
2379
2380 /* Y-origin and y-position of the base line of this string. */
2381 int y, ybase;
2382
2383 /* The width of the string, not including a face extension. */
2384 int width;
2385
2386 /* The width of the string, including a face extension. */
2387 int background_width;
2388
2389 /* The height of this string. This is the height of the line this
2390 string is drawn in, and can be different from the height of the
2391 font the string is drawn in. */
2392 int height;
2393
2394 /* Number of pixels this string overwrites in front of its x-origin.
2395 This number is zero if the string has an lbearing >= 0; it is
2396 -lbearing, if the string has an lbearing < 0. */
2397 int left_overhang;
2398
2399 /* Number of pixels this string overwrites past its right-most
2400 nominal x-position, i.e. x + width. Zero if the string's
2401 rbearing is <= its nominal width, rbearing - width otherwise. */
2402 int right_overhang;
2403
2404 /* The frame on which the glyph string is drawn. */
2405 struct frame *f;
2406
2407 /* The window on which the glyph string is drawn. */
2408 struct window *w;
2409
2410 /* X display and window for convenience. */
2411 Display *display;
2412 Window window;
2413
2414 /* The glyph row for which this string was built. It determines the
2415 y-origin and height of the string. */
2416 struct glyph_row *row;
2417
2418 /* The area within row. */
2419 enum glyph_row_area area;
2420
2421 /* Characters to be drawn, and number of characters. */
2422 XChar2b *char2b;
2423 int nchars;
2424
2425 /* A face-override for drawing cursors, mouse face and similar. */
2426 enum draw_glyphs_face hl;
2427
2428 /* Face in which this string is to be drawn. */
2429 struct face *face;
2430
2431 /* Font in which this string is to be drawn. */
2432 XFontStruct *font;
2433
2434 /* Font info for this string. */
2435 struct font_info *font_info;
2436
2437 /* Non-null means this string describes (part of) a composition.
2438 All characters from char2b are drawn composed. */
2439 struct composition *cmp;
2440
2441 /* Index of this glyph string's first character in the glyph
2442 definition of CMP. If this is zero, this glyph string describes
2443 the first character of a composition. */
2444 int gidx;
2445
2446 /* 1 means this glyph strings face has to be drawn to the right end
2447 of the window's drawing area. */
2448 unsigned extends_to_end_of_line_p : 1;
2449
2450 /* 1 means the background of this string has been drawn. */
2451 unsigned background_filled_p : 1;
2452
2453 /* 1 means glyph string must be drawn with 16-bit functions. */
2454 unsigned two_byte_p : 1;
2455
2456 /* 1 means that the original font determined for drawing this glyph
2457 string could not be loaded. The member `font' has been set to
2458 the frame's default font in this case. */
2459 unsigned font_not_found_p : 1;
2460
2461 /* 1 means that the face in which this glyph string is drawn has a
2462 stipple pattern. */
2463 unsigned stippled_p : 1;
2464
2465 /* 1 means only the foreground of this glyph string must be drawn,
2466 and we should use the physical height of the line this glyph
2467 string appears in as clip rect. */
2468 unsigned for_overlaps_p : 1;
2469
2470 /* The GC to use for drawing this glyph string. */
2471 GC gc;
2472
2473 /* A pointer to the first glyph in the string. This glyph
2474 corresponds to char2b[0]. Needed to draw rectangles if
2475 font_not_found_p is 1. */
2476 struct glyph *first_glyph;
2477
2478 /* Image, if any. */
2479 struct image *img;
2480
2481 struct glyph_string *next, *prev;
2482 };
2483
2484
2485 #if 0
2486
2487 static void
2488 x_dump_glyph_string (s)
2489 struct glyph_string *s;
2490 {
2491 fprintf (stderr, "glyph string\n");
2492 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2493 s->x, s->y, s->width, s->height);
2494 fprintf (stderr, " ybase = %d\n", s->ybase);
2495 fprintf (stderr, " hl = %d\n", s->hl);
2496 fprintf (stderr, " left overhang = %d, right = %d\n",
2497 s->left_overhang, s->right_overhang);
2498 fprintf (stderr, " nchars = %d\n", s->nchars);
2499 fprintf (stderr, " extends to end of line = %d\n",
2500 s->extends_to_end_of_line_p);
2501 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2502 fprintf (stderr, " bg width = %d\n", s->background_width);
2503 }
2504
2505 #endif /* GLYPH_DEBUG */
2506
2507
2508
2509 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2510 struct glyph_string **,
2511 struct glyph_string *,
2512 struct glyph_string *));
2513 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2514 struct glyph_string **,
2515 struct glyph_string *,
2516 struct glyph_string *));
2517 static void x_append_glyph_string P_ ((struct glyph_string **,
2518 struct glyph_string **,
2519 struct glyph_string *));
2520 static int x_left_overwritten P_ ((struct glyph_string *));
2521 static int x_left_overwriting P_ ((struct glyph_string *));
2522 static int x_right_overwritten P_ ((struct glyph_string *));
2523 static int x_right_overwriting P_ ((struct glyph_string *));
2524 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2525 int));
2526 static void x_init_glyph_string P_ ((struct glyph_string *,
2527 XChar2b *, struct window *,
2528 struct glyph_row *,
2529 enum glyph_row_area, int,
2530 enum draw_glyphs_face));
2531 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2532 enum glyph_row_area, int, int,
2533 enum draw_glyphs_face, int *, int *, int));
2534 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2535 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2536 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2537 int));
2538 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2539 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2540 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2541 static void x_draw_glyph_string P_ ((struct glyph_string *));
2542 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2543 static void x_set_cursor_gc P_ ((struct glyph_string *));
2544 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2545 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2546 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2547 int *, int *));
2548 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2549 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2550 unsigned long *, double, int));
2551 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2552 double, int, unsigned long));
2553 static void x_setup_relief_colors P_ ((struct glyph_string *));
2554 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2555 static void x_draw_image_relief P_ ((struct glyph_string *));
2556 static void x_draw_image_foreground P_ ((struct glyph_string *));
2557 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2558 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2559 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2560 int, int, int));
2561 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2562 int, int, int, int, XRectangle *));
2563 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2564 int, int, int, XRectangle *));
2565 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2566 enum glyph_row_area));
2567 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2568 struct glyph_row *,
2569 enum glyph_row_area, int, int));
2570
2571 #if GLYPH_DEBUG
2572 static void x_check_font P_ ((struct frame *, XFontStruct *));
2573 #endif
2574
2575
2576 /* Append the list of glyph strings with head H and tail T to the list
2577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2578
2579 static INLINE void
2580 x_append_glyph_string_lists (head, tail, h, t)
2581 struct glyph_string **head, **tail;
2582 struct glyph_string *h, *t;
2583 {
2584 if (h)
2585 {
2586 if (*head)
2587 (*tail)->next = h;
2588 else
2589 *head = h;
2590 h->prev = *tail;
2591 *tail = t;
2592 }
2593 }
2594
2595
2596 /* Prepend the list of glyph strings with head H and tail T to the
2597 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2598 result. */
2599
2600 static INLINE void
2601 x_prepend_glyph_string_lists (head, tail, h, t)
2602 struct glyph_string **head, **tail;
2603 struct glyph_string *h, *t;
2604 {
2605 if (h)
2606 {
2607 if (*head)
2608 (*head)->prev = t;
2609 else
2610 *tail = t;
2611 t->next = *head;
2612 *head = h;
2613 }
2614 }
2615
2616
2617 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2618 Set *HEAD and *TAIL to the resulting list. */
2619
2620 static INLINE void
2621 x_append_glyph_string (head, tail, s)
2622 struct glyph_string **head, **tail;
2623 struct glyph_string *s;
2624 {
2625 s->next = s->prev = NULL;
2626 x_append_glyph_string_lists (head, tail, s, s);
2627 }
2628
2629
2630 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2631 face. */
2632
2633 static void
2634 x_set_cursor_gc (s)
2635 struct glyph_string *s;
2636 {
2637 if (s->font == FRAME_FONT (s->f)
2638 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2639 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2640 && !s->cmp)
2641 s->gc = s->f->output_data.x->cursor_gc;
2642 else
2643 {
2644 /* Cursor on non-default face: must merge. */
2645 XGCValues xgcv;
2646 unsigned long mask;
2647
2648 xgcv.background = s->f->output_data.x->cursor_pixel;
2649 xgcv.foreground = s->face->background;
2650
2651 /* If the glyph would be invisible, try a different foreground. */
2652 if (xgcv.foreground == xgcv.background)
2653 xgcv.foreground = s->face->foreground;
2654 if (xgcv.foreground == xgcv.background)
2655 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2656 if (xgcv.foreground == xgcv.background)
2657 xgcv.foreground = s->face->foreground;
2658
2659 /* Make sure the cursor is distinct from text in this face. */
2660 if (xgcv.background == s->face->background
2661 && xgcv.foreground == s->face->foreground)
2662 {
2663 xgcv.background = s->face->foreground;
2664 xgcv.foreground = s->face->background;
2665 }
2666
2667 IF_DEBUG (x_check_font (s->f, s->font));
2668 xgcv.font = s->font->fid;
2669 xgcv.graphics_exposures = False;
2670 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2671
2672 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2673 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2674 mask, &xgcv);
2675 else
2676 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2677 = XCreateGC (s->display, s->window, mask, &xgcv);
2678
2679 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2680 }
2681 }
2682
2683
2684 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2685
2686 static void
2687 x_set_mouse_face_gc (s)
2688 struct glyph_string *s;
2689 {
2690 int face_id;
2691 struct face *face;
2692
2693 /* What face has to be used last for the mouse face? */
2694 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2695 face = FACE_FROM_ID (s->f, face_id);
2696 if (face == NULL)
2697 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2698
2699 if (s->first_glyph->type == CHAR_GLYPH)
2700 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2701 else
2702 face_id = FACE_FOR_CHAR (s->f, face, 0);
2703 s->face = FACE_FROM_ID (s->f, face_id);
2704 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2705
2706 /* If font in this face is same as S->font, use it. */
2707 if (s->font == s->face->font)
2708 s->gc = s->face->gc;
2709 else
2710 {
2711 /* Otherwise construct scratch_cursor_gc with values from FACE
2712 but font FONT. */
2713 XGCValues xgcv;
2714 unsigned long mask;
2715
2716 xgcv.background = s->face->background;
2717 xgcv.foreground = s->face->foreground;
2718 IF_DEBUG (x_check_font (s->f, s->font));
2719 xgcv.font = s->font->fid;
2720 xgcv.graphics_exposures = False;
2721 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2722
2723 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2724 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2725 mask, &xgcv);
2726 else
2727 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2728 = XCreateGC (s->display, s->window, mask, &xgcv);
2729
2730 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2731 }
2732
2733 xassert (s->gc != 0);
2734 }
2735
2736
2737 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2738 Faces to use in the mode line have already been computed when the
2739 matrix was built, so there isn't much to do, here. */
2740
2741 static INLINE void
2742 x_set_mode_line_face_gc (s)
2743 struct glyph_string *s;
2744 {
2745 s->gc = s->face->gc;
2746 }
2747
2748
2749 /* Set S->gc of glyph string S for drawing that glyph string. Set
2750 S->stippled_p to a non-zero value if the face of S has a stipple
2751 pattern. */
2752
2753 static INLINE void
2754 x_set_glyph_string_gc (s)
2755 struct glyph_string *s;
2756 {
2757 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2758
2759 if (s->hl == DRAW_NORMAL_TEXT)
2760 {
2761 s->gc = s->face->gc;
2762 s->stippled_p = s->face->stipple != 0;
2763 }
2764 else if (s->hl == DRAW_INVERSE_VIDEO)
2765 {
2766 x_set_mode_line_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_CURSOR)
2770 {
2771 x_set_cursor_gc (s);
2772 s->stippled_p = 0;
2773 }
2774 else if (s->hl == DRAW_MOUSE_FACE)
2775 {
2776 x_set_mouse_face_gc (s);
2777 s->stippled_p = s->face->stipple != 0;
2778 }
2779 else if (s->hl == DRAW_IMAGE_RAISED
2780 || s->hl == DRAW_IMAGE_SUNKEN)
2781 {
2782 s->gc = s->face->gc;
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790
2791 /* GC must have been set. */
2792 xassert (s->gc != 0);
2793 }
2794
2795
2796 /* Return in *R the clipping rectangle for glyph string S. */
2797
2798 static void
2799 x_get_glyph_string_clip_rect (s, r)
2800 struct glyph_string *s;
2801 XRectangle *r;
2802 {
2803 if (s->row->full_width_p)
2804 {
2805 /* Draw full-width. X coordinates are relative to S->w->left. */
2806 int canon_x = CANON_X_UNIT (s->f);
2807
2808 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2809 r->width = XFASTINT (s->w->width) * canon_x;
2810
2811 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2812 {
2813 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2814 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2815 r->x -= width;
2816 }
2817
2818 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2819
2820 /* Unless displaying a mode or menu bar line, which are always
2821 fully visible, clip to the visible part of the row. */
2822 if (s->w->pseudo_window_p)
2823 r->height = s->row->visible_height;
2824 else
2825 r->height = s->height;
2826 }
2827 else
2828 {
2829 /* This is a text line that may be partially visible. */
2830 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2831 r->width = window_box_width (s->w, s->area);
2832 r->height = s->row->visible_height;
2833 }
2834
2835 /* Don't use S->y for clipping because it doesn't take partially
2836 visible lines into account. For example, it can be negative for
2837 partially visible lines at the top of a window. */
2838 if (!s->row->full_width_p
2839 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2841 else
2842 r->y = max (0, s->row->y);
2843
2844 /* If drawing a tool-bar window, draw it over the internal border
2845 at the top of the window. */
2846 if (s->w == XWINDOW (s->f->tool_bar_window))
2847 r->y -= s->f->output_data.x->internal_border_width;
2848
2849 /* If S draws overlapping rows, it's sufficient to use the top and
2850 bottom of the window for clipping because this glyph string
2851 intentionally draws over other lines. */
2852 if (s->for_overlaps_p)
2853 {
2854 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2855 r->height = window_text_bottom_y (s->w) - r->y;
2856 }
2857
2858 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2859 }
2860
2861
2862 /* Set clipping for output of glyph string S. S may be part of a mode
2863 line or menu if we don't have X toolkit support. */
2864
2865 static INLINE void
2866 x_set_glyph_string_clipping (s)
2867 struct glyph_string *s;
2868 {
2869 XRectangle r;
2870 x_get_glyph_string_clip_rect (s, &r);
2871 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2872 }
2873
2874
2875 /* Compute left and right overhang of glyph string S. If S is a glyph
2876 string for a composition, assume overhangs don't exist. */
2877
2878 static INLINE void
2879 x_compute_glyph_string_overhangs (s)
2880 struct glyph_string *s;
2881 {
2882 if (s->cmp == NULL
2883 && s->first_glyph->type == CHAR_GLYPH)
2884 {
2885 XCharStruct cs;
2886 int direction, font_ascent, font_descent;
2887 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2888 &font_ascent, &font_descent, &cs);
2889 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2890 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2891 }
2892 }
2893
2894
2895 /* Compute overhangs and x-positions for glyph string S and its
2896 predecessors, or successors. X is the starting x-position for S.
2897 BACKWARD_P non-zero means process predecessors. */
2898
2899 static void
2900 x_compute_overhangs_and_x (s, x, backward_p)
2901 struct glyph_string *s;
2902 int x;
2903 int backward_p;
2904 {
2905 if (backward_p)
2906 {
2907 while (s)
2908 {
2909 x_compute_glyph_string_overhangs (s);
2910 x -= s->width;
2911 s->x = x;
2912 s = s->prev;
2913 }
2914 }
2915 else
2916 {
2917 while (s)
2918 {
2919 x_compute_glyph_string_overhangs (s);
2920 s->x = x;
2921 x += s->width;
2922 s = s->next;
2923 }
2924 }
2925 }
2926
2927
2928 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2929 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2930 assumed to be zero. */
2931
2932 static void
2933 x_get_glyph_overhangs (glyph, f, left, right)
2934 struct glyph *glyph;
2935 struct frame *f;
2936 int *left, *right;
2937 {
2938 *left = *right = 0;
2939
2940 if (glyph->type == CHAR_GLYPH)
2941 {
2942 XFontStruct *font;
2943 struct face *face;
2944 struct font_info *font_info;
2945 XChar2b char2b;
2946 XCharStruct *pcm;
2947
2948 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2949 font = face->font;
2950 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2951 if (font
2952 && (pcm = x_per_char_metric (font, &char2b)))
2953 {
2954 if (pcm->rbearing > pcm->width)
2955 *right = pcm->rbearing - pcm->width;
2956 if (pcm->lbearing < 0)
2957 *left = -pcm->lbearing;
2958 }
2959 }
2960 }
2961
2962
2963 /* Return the index of the first glyph preceding glyph string S that
2964 is overwritten by S because of S's left overhang. Value is -1
2965 if no glyphs are overwritten. */
2966
2967 static int
2968 x_left_overwritten (s)
2969 struct glyph_string *s;
2970 {
2971 int k;
2972
2973 if (s->left_overhang)
2974 {
2975 int x = 0, i;
2976 struct glyph *glyphs = s->row->glyphs[s->area];
2977 int first = s->first_glyph - glyphs;
2978
2979 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2980 x -= glyphs[i].pixel_width;
2981
2982 k = i + 1;
2983 }
2984 else
2985 k = -1;
2986
2987 return k;
2988 }
2989
2990
2991 /* Return the index of the first glyph preceding glyph string S that
2992 is overwriting S because of its right overhang. Value is -1 if no
2993 glyph in front of S overwrites S. */
2994
2995 static int
2996 x_left_overwriting (s)
2997 struct glyph_string *s;
2998 {
2999 int i, k, x;
3000 struct glyph *glyphs = s->row->glyphs[s->area];
3001 int first = s->first_glyph - glyphs;
3002
3003 k = -1;
3004 x = 0;
3005 for (i = first - 1; i >= 0; --i)
3006 {
3007 int left, right;
3008 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3009 if (x + right > 0)
3010 k = i;
3011 x -= glyphs[i].pixel_width;
3012 }
3013
3014 return k;
3015 }
3016
3017
3018 /* Return the index of the last glyph following glyph string S that is
3019 not overwritten by S because of S's right overhang. Value is -1 if
3020 no such glyph is found. */
3021
3022 static int
3023 x_right_overwritten (s)
3024 struct glyph_string *s;
3025 {
3026 int k = -1;
3027
3028 if (s->right_overhang)
3029 {
3030 int x = 0, i;
3031 struct glyph *glyphs = s->row->glyphs[s->area];
3032 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3033 int end = s->row->used[s->area];
3034
3035 for (i = first; i < end && s->right_overhang > x; ++i)
3036 x += glyphs[i].pixel_width;
3037
3038 k = i;
3039 }
3040
3041 return k;
3042 }
3043
3044
3045 /* Return the index of the last glyph following glyph string S that
3046 overwrites S because of its left overhang. Value is negative
3047 if no such glyph is found. */
3048
3049 static int
3050 x_right_overwriting (s)
3051 struct glyph_string *s;
3052 {
3053 int i, k, x;
3054 int end = s->row->used[s->area];
3055 struct glyph *glyphs = s->row->glyphs[s->area];
3056 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3057
3058 k = -1;
3059 x = 0;
3060 for (i = first; i < end; ++i)
3061 {
3062 int left, right;
3063 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3064 if (x - left < 0)
3065 k = i;
3066 x += glyphs[i].pixel_width;
3067 }
3068
3069 return k;
3070 }
3071
3072
3073 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3074
3075 static INLINE void
3076 x_clear_glyph_string_rect (s, x, y, w, h)
3077 struct glyph_string *s;
3078 int x, y, w, h;
3079 {
3080 XGCValues xgcv;
3081 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3082 XSetForeground (s->display, s->gc, xgcv.background);
3083 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3084 XSetForeground (s->display, s->gc, xgcv.foreground);
3085 }
3086
3087
3088 /* Draw the background of glyph_string S. If S->background_filled_p
3089 is non-zero don't draw it. FORCE_P non-zero means draw the
3090 background even if it wouldn't be drawn normally. This is used
3091 when a string preceding S draws into the background of S, or S
3092 contains the first component of a composition. */
3093
3094 static void
3095 x_draw_glyph_string_background (s, force_p)
3096 struct glyph_string *s;
3097 int force_p;
3098 {
3099 /* Nothing to do if background has already been drawn or if it
3100 shouldn't be drawn in the first place. */
3101 if (!s->background_filled_p)
3102 {
3103 int box_line_width = max (s->face->box_line_width, 0);
3104
3105 if (s->stippled_p)
3106 {
3107 /* Fill background with a stipple pattern. */
3108 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3109 XFillRectangle (s->display, s->window, s->gc, s->x,
3110 s->y + box_line_width,
3111 s->background_width,
3112 s->height - 2 * box_line_width);
3113 XSetFillStyle (s->display, s->gc, FillSolid);
3114 s->background_filled_p = 1;
3115 }
3116 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3117 || s->font_not_found_p
3118 || s->extends_to_end_of_line_p
3119 || force_p)
3120 {
3121 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3122 s->background_width,
3123 s->height - 2 * box_line_width);
3124 s->background_filled_p = 1;
3125 }
3126 }
3127 }
3128
3129
3130 /* Draw the foreground of glyph string S. */
3131
3132 static void
3133 x_draw_glyph_string_foreground (s)
3134 struct glyph_string *s;
3135 {
3136 int i, x;
3137
3138 /* If first glyph of S has a left box line, start drawing the text
3139 of S to the right of that box line. */
3140 if (s->face->box != FACE_NO_BOX
3141 && s->first_glyph->left_box_line_p)
3142 x = s->x + abs (s->face->box_line_width);
3143 else
3144 x = s->x;
3145
3146 /* Draw characters of S as rectangles if S's font could not be
3147 loaded. */
3148 if (s->font_not_found_p)
3149 {
3150 for (i = 0; i < s->nchars; ++i)
3151 {
3152 struct glyph *g = s->first_glyph + i;
3153 XDrawRectangle (s->display, s->window,
3154 s->gc, x, s->y, g->pixel_width - 1,
3155 s->height - 1);
3156 x += g->pixel_width;
3157 }
3158 }
3159 else
3160 {
3161 char *char1b = (char *) s->char2b;
3162 int boff = s->font_info->baseline_offset;
3163
3164 if (s->font_info->vertical_centering)
3165 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3166
3167 /* If we can use 8-bit functions, condense S->char2b. */
3168 if (!s->two_byte_p)
3169 for (i = 0; i < s->nchars; ++i)
3170 char1b[i] = s->char2b[i].byte2;
3171
3172 /* Draw text with XDrawString if background has already been
3173 filled. Otherwise, use XDrawImageString. (Note that
3174 XDrawImageString is usually faster than XDrawString.) Always
3175 use XDrawImageString when drawing the cursor so that there is
3176 no chance that characters under a box cursor are invisible. */
3177 if (s->for_overlaps_p
3178 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3179 {
3180 /* Draw characters with 16-bit or 8-bit functions. */
3181 if (s->two_byte_p)
3182 XDrawString16 (s->display, s->window, s->gc, x,
3183 s->ybase - boff, s->char2b, s->nchars);
3184 else
3185 XDrawString (s->display, s->window, s->gc, x,
3186 s->ybase - boff, char1b, s->nchars);
3187 }
3188 else
3189 {
3190 if (s->two_byte_p)
3191 XDrawImageString16 (s->display, s->window, s->gc, x,
3192 s->ybase - boff, s->char2b, s->nchars);
3193 else
3194 XDrawImageString (s->display, s->window, s->gc, x,
3195 s->ybase - boff, char1b, s->nchars);
3196 }
3197 }
3198 }
3199
3200 /* Draw the foreground of composite glyph string S. */
3201
3202 static void
3203 x_draw_composite_glyph_string_foreground (s)
3204 struct glyph_string *s;
3205 {
3206 int i, x;
3207
3208 /* If first glyph of S has a left box line, start drawing the text
3209 of S to the right of that box line. */
3210 if (s->face->box != FACE_NO_BOX
3211 && s->first_glyph->left_box_line_p)
3212 x = s->x + abs (s->face->box_line_width);
3213 else
3214 x = s->x;
3215
3216 /* S is a glyph string for a composition. S->gidx is the index of
3217 the first character drawn for glyphs of this composition.
3218 S->gidx == 0 means we are drawing the very first character of
3219 this composition. */
3220
3221 /* Draw a rectangle for the composition if the font for the very
3222 first character of the composition could not be loaded. */
3223 if (s->font_not_found_p)
3224 {
3225 if (s->gidx == 0)
3226 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3227 s->width - 1, s->height - 1);
3228 }
3229 else
3230 {
3231 for (i = 0; i < s->nchars; i++, ++s->gidx)
3232 XDrawString16 (s->display, s->window, s->gc,
3233 x + s->cmp->offsets[s->gidx * 2],
3234 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3235 s->char2b + i, 1);
3236 }
3237 }
3238
3239
3240 #ifdef USE_X_TOOLKIT
3241
3242 static struct frame *x_frame_of_widget P_ ((Widget));
3243
3244
3245 /* Return the frame on which widget WIDGET is used.. Abort if frame
3246 cannot be determined. */
3247
3248 static struct frame *
3249 x_frame_of_widget (widget)
3250 Widget widget;
3251 {
3252 struct x_display_info *dpyinfo;
3253 Lisp_Object tail;
3254 struct frame *f;
3255
3256 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3257
3258 /* Find the top-level shell of the widget. Note that this function
3259 can be called when the widget is not yet realized, so XtWindow
3260 (widget) == 0. That's the reason we can't simply use
3261 x_any_window_to_frame. */
3262 while (!XtIsTopLevelShell (widget))
3263 widget = XtParent (widget);
3264
3265 /* Look for a frame with that top-level widget. Allocate the color
3266 on that frame to get the right gamma correction value. */
3267 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3268 if (GC_FRAMEP (XCAR (tail))
3269 && (f = XFRAME (XCAR (tail)),
3270 (f->output_data.nothing != 1
3271 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3272 && f->output_data.x->widget == widget)
3273 return f;
3274
3275 abort ();
3276 }
3277
3278
3279 /* Allocate the color COLOR->pixel on the screen and display of
3280 widget WIDGET in colormap CMAP. If an exact match cannot be
3281 allocated, try the nearest color available. Value is non-zero
3282 if successful. This is called from lwlib. */
3283
3284 int
3285 x_alloc_nearest_color_for_widget (widget, cmap, color)
3286 Widget widget;
3287 Colormap cmap;
3288 XColor *color;
3289 {
3290 struct frame *f = x_frame_of_widget (widget);
3291 return x_alloc_nearest_color (f, cmap, color);
3292 }
3293
3294
3295 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3296 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3297 If this produces the same color as PIXEL, try a color where all RGB
3298 values have DELTA added. Return the allocated color in *PIXEL.
3299 DISPLAY is the X display, CMAP is the colormap to operate on.
3300 Value is non-zero if successful. */
3301
3302 int
3303 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3304 Widget widget;
3305 Display *display;
3306 Colormap cmap;
3307 unsigned long *pixel;
3308 double factor;
3309 int delta;
3310 {
3311 struct frame *f = x_frame_of_widget (widget);
3312 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3313 }
3314
3315
3316 #endif /* USE_X_TOOLKIT */
3317
3318
3319 /* Value is an array of XColor structures for the contents of the
3320 color map of frame F. Set *NCELLS to the size of the array.
3321 Note that this probably shouldn't be called for large color maps,
3322 say a 24-bit TrueColor map. */
3323
3324 static const XColor *
3325 x_color_cells (f, ncells)
3326 struct frame *f;
3327 int *ncells;
3328 {
3329 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3330
3331 if (dpyinfo->color_cells == NULL)
3332 {
3333 Display *display = FRAME_X_DISPLAY (f);
3334 Screen *screen = FRAME_X_SCREEN (f);
3335 int i;
3336
3337 dpyinfo->ncolor_cells
3338 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3339 dpyinfo->color_cells
3340 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3341 * sizeof *dpyinfo->color_cells);
3342
3343 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3344 dpyinfo->color_cells[i].pixel = i;
3345
3346 XQueryColors (display, FRAME_X_COLORMAP (f),
3347 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3348 }
3349
3350 *ncells = dpyinfo->ncolor_cells;
3351 return dpyinfo->color_cells;
3352 }
3353
3354
3355 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3356 colors in COLORS. Use cached information, if available. */
3357
3358 void
3359 x_query_colors (f, colors, ncolors)
3360 struct frame *f;
3361 XColor *colors;
3362 int ncolors;
3363 {
3364 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3365
3366 if (dpyinfo->color_cells)
3367 {
3368 int i;
3369 for (i = 0; i < ncolors; ++i)
3370 {
3371 unsigned long pixel = colors[i].pixel;
3372 xassert (pixel < dpyinfo->ncolor_cells);
3373 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3374 colors[i] = dpyinfo->color_cells[pixel];
3375 }
3376 }
3377 else
3378 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3379 }
3380
3381
3382 /* On frame F, translate pixel color to RGB values for the color in
3383 COLOR. Use cached information, if available. */
3384
3385 void
3386 x_query_color (f, color)
3387 struct frame *f;
3388 XColor *color;
3389 {
3390 x_query_colors (f, color, 1);
3391 }
3392
3393
3394 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3395 CMAP. If an exact match can't be allocated, try the nearest color
3396 available. Value is non-zero if successful. Set *COLOR to the
3397 color allocated. */
3398
3399 int
3400 x_alloc_nearest_color (f, cmap, color)
3401 struct frame *f;
3402 Colormap cmap;
3403 XColor *color;
3404 {
3405 Display *display = FRAME_X_DISPLAY (f);
3406 Screen *screen = FRAME_X_SCREEN (f);
3407 int rc;
3408
3409 gamma_correct (f, color);
3410 rc = XAllocColor (display, cmap, color);
3411 if (rc == 0)
3412 {
3413 /* If we got to this point, the colormap is full, so we're going
3414 to try to get the next closest color. The algorithm used is
3415 a least-squares matching, which is what X uses for closest
3416 color matching with StaticColor visuals. */
3417 int nearest, i;
3418 unsigned long nearest_delta = ~0;
3419 int ncells;
3420 const XColor *cells = x_color_cells (f, &ncells);
3421
3422 for (nearest = i = 0; i < ncells; ++i)
3423 {
3424 long dred = (color->red >> 8) - (cells[i].red >> 8);
3425 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3426 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3427 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3428
3429 if (delta < nearest_delta)
3430 {
3431 nearest = i;
3432 nearest_delta = delta;
3433 }
3434 }
3435
3436 color->red = cells[nearest].red;
3437 color->green = cells[nearest].green;
3438 color->blue = cells[nearest].blue;
3439 rc = XAllocColor (display, cmap, color);
3440 }
3441 else
3442 {
3443 /* If allocation succeeded, and the allocated pixel color is not
3444 equal to a cached pixel color recorded earlier, there was a
3445 change in the colormap, so clear the color cache. */
3446 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3447 XColor *cached_color;
3448
3449 if (dpyinfo->color_cells
3450 && (cached_color = &dpyinfo->color_cells[color->pixel],
3451 (cached_color->red != color->red
3452 || cached_color->blue != color->blue
3453 || cached_color->green != color->green)))
3454 {
3455 xfree (dpyinfo->color_cells);
3456 dpyinfo->color_cells = NULL;
3457 dpyinfo->ncolor_cells = 0;
3458 }
3459 }
3460
3461 #ifdef DEBUG_X_COLORS
3462 if (rc)
3463 register_color (color->pixel);
3464 #endif /* DEBUG_X_COLORS */
3465
3466 return rc;
3467 }
3468
3469
3470 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3471 It's necessary to do this instead of just using PIXEL directly to
3472 get color reference counts right. */
3473
3474 unsigned long
3475 x_copy_color (f, pixel)
3476 struct frame *f;
3477 unsigned long pixel;
3478 {
3479 XColor color;
3480
3481 color.pixel = pixel;
3482 BLOCK_INPUT;
3483 x_query_color (f, &color);
3484 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3485 UNBLOCK_INPUT;
3486 #ifdef DEBUG_X_COLORS
3487 register_color (pixel);
3488 #endif
3489 return color.pixel;
3490 }
3491
3492
3493 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3494 It's necessary to do this instead of just using PIXEL directly to
3495 get color reference counts right. */
3496
3497 unsigned long
3498 x_copy_dpy_color (dpy, cmap, pixel)
3499 Display *dpy;
3500 Colormap cmap;
3501 unsigned long pixel;
3502 {
3503 XColor color;
3504
3505 color.pixel = pixel;
3506 BLOCK_INPUT;
3507 XQueryColor (dpy, cmap, &color);
3508 XAllocColor (dpy, cmap, &color);
3509 UNBLOCK_INPUT;
3510 #ifdef DEBUG_X_COLORS
3511 register_color (pixel);
3512 #endif
3513 return color.pixel;
3514 }
3515
3516
3517 /* Brightness beyond which a color won't have its highlight brightness
3518 boosted.
3519
3520 Nominally, highlight colors for `3d' faces are calculated by
3521 brightening an object's color by a constant scale factor, but this
3522 doesn't yield good results for dark colors, so for colors who's
3523 brightness is less than this value (on a scale of 0-65535) have an
3524 use an additional additive factor.
3525
3526 The value here is set so that the default menu-bar/mode-line color
3527 (grey75) will not have its highlights changed at all. */
3528 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3529
3530
3531 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3532 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3533 If this produces the same color as PIXEL, try a color where all RGB
3534 values have DELTA added. Return the allocated color in *PIXEL.
3535 DISPLAY is the X display, CMAP is the colormap to operate on.
3536 Value is non-zero if successful. */
3537
3538 static int
3539 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3540 struct frame *f;
3541 Display *display;
3542 Colormap cmap;
3543 unsigned long *pixel;
3544 double factor;
3545 int delta;
3546 {
3547 XColor color, new;
3548 long bright;
3549 int success_p;
3550
3551 /* Get RGB color values. */
3552 color.pixel = *pixel;
3553 x_query_color (f, &color);
3554
3555 /* Change RGB values by specified FACTOR. Avoid overflow! */
3556 xassert (factor >= 0);
3557 new.red = min (0xffff, factor * color.red);
3558 new.green = min (0xffff, factor * color.green);
3559 new.blue = min (0xffff, factor * color.blue);
3560
3561 /* Calculate brightness of COLOR. */
3562 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3563
3564 /* We only boost colors that are darker than
3565 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3566 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3567 /* Make an additive adjustment to NEW, because it's dark enough so
3568 that scaling by FACTOR alone isn't enough. */
3569 {
3570 /* How far below the limit this color is (0 - 1, 1 being darker). */
3571 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3572 /* The additive adjustment. */
3573 int min_delta = delta * dimness * factor / 2;
3574
3575 if (factor < 1)
3576 {
3577 new.red = max (0, new.red - min_delta);
3578 new.green = max (0, new.green - min_delta);
3579 new.blue = max (0, new.blue - min_delta);
3580 }
3581 else
3582 {
3583 new.red = min (0xffff, min_delta + new.red);
3584 new.green = min (0xffff, min_delta + new.green);
3585 new.blue = min (0xffff, min_delta + new.blue);
3586 }
3587 }
3588
3589 /* Try to allocate the color. */
3590 success_p = x_alloc_nearest_color (f, cmap, &new);
3591 if (success_p)
3592 {
3593 if (new.pixel == *pixel)
3594 {
3595 /* If we end up with the same color as before, try adding
3596 delta to the RGB values. */
3597 x_free_colors (f, &new.pixel, 1);
3598
3599 new.red = min (0xffff, delta + color.red);
3600 new.green = min (0xffff, delta + color.green);
3601 new.blue = min (0xffff, delta + color.blue);
3602 success_p = x_alloc_nearest_color (f, cmap, &new);
3603 }
3604 else
3605 success_p = 1;
3606 *pixel = new.pixel;
3607 }
3608
3609 return success_p;
3610 }
3611
3612
3613 /* Set up the foreground color for drawing relief lines of glyph
3614 string S. RELIEF is a pointer to a struct relief containing the GC
3615 with which lines will be drawn. Use a color that is FACTOR or
3616 DELTA lighter or darker than the relief's background which is found
3617 in S->f->output_data.x->relief_background. If such a color cannot
3618 be allocated, use DEFAULT_PIXEL, instead. */
3619
3620 static void
3621 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3622 struct frame *f;
3623 struct relief *relief;
3624 double factor;
3625 int delta;
3626 unsigned long default_pixel;
3627 {
3628 XGCValues xgcv;
3629 struct x_output *di = f->output_data.x;
3630 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3631 unsigned long pixel;
3632 unsigned long background = di->relief_background;
3633 Colormap cmap = FRAME_X_COLORMAP (f);
3634 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3635 Display *dpy = FRAME_X_DISPLAY (f);
3636
3637 xgcv.graphics_exposures = False;
3638 xgcv.line_width = 1;
3639
3640 /* Free previously allocated color. The color cell will be reused
3641 when it has been freed as many times as it was allocated, so this
3642 doesn't affect faces using the same colors. */
3643 if (relief->gc
3644 && relief->allocated_p)
3645 {
3646 x_free_colors (f, &relief->pixel, 1);
3647 relief->allocated_p = 0;
3648 }
3649
3650 /* Allocate new color. */
3651 xgcv.foreground = default_pixel;
3652 pixel = background;
3653 if (dpyinfo->n_planes != 1
3654 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3655 {
3656 relief->allocated_p = 1;
3657 xgcv.foreground = relief->pixel = pixel;
3658 }
3659
3660 if (relief->gc == 0)
3661 {
3662 xgcv.stipple = dpyinfo->gray;
3663 mask |= GCStipple;
3664 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3665 }
3666 else
3667 XChangeGC (dpy, relief->gc, mask, &xgcv);
3668 }
3669
3670
3671 /* Set up colors for the relief lines around glyph string S. */
3672
3673 static void
3674 x_setup_relief_colors (s)
3675 struct glyph_string *s;
3676 {
3677 struct x_output *di = s->f->output_data.x;
3678 unsigned long color;
3679
3680 if (s->face->use_box_color_for_shadows_p)
3681 color = s->face->box_color;
3682 else
3683 {
3684 XGCValues xgcv;
3685
3686 /* Get the background color of the face. */
3687 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3688 color = xgcv.background;
3689 }
3690
3691 if (di->white_relief.gc == 0
3692 || color != di->relief_background)
3693 {
3694 di->relief_background = color;
3695 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3696 WHITE_PIX_DEFAULT (s->f));
3697 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3698 BLACK_PIX_DEFAULT (s->f));
3699 }
3700 }
3701
3702
3703 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3704 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3705 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3706 relief. LEFT_P non-zero means draw a relief on the left side of
3707 the rectangle. RIGHT_P non-zero means draw a relief on the right
3708 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3709 when drawing. */
3710
3711 static void
3712 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3713 raised_p, left_p, right_p, clip_rect)
3714 struct frame *f;
3715 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3716 XRectangle *clip_rect;
3717 {
3718 int i;
3719 GC gc;
3720
3721 if (raised_p)
3722 gc = f->output_data.x->white_relief.gc;
3723 else
3724 gc = f->output_data.x->black_relief.gc;
3725 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3726
3727 /* Top. */
3728 for (i = 0; i < width; ++i)
3729 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3730 left_x + i * left_p, top_y + i,
3731 right_x + 1 - i * right_p, top_y + i);
3732
3733 /* Left. */
3734 if (left_p)
3735 for (i = 0; i < width; ++i)
3736 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3737 left_x + i, top_y + i, left_x + i, bottom_y - i);
3738
3739 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3740 if (raised_p)
3741 gc = f->output_data.x->black_relief.gc;
3742 else
3743 gc = f->output_data.x->white_relief.gc;
3744 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3745
3746 /* Bottom. */
3747 for (i = 0; i < width; ++i)
3748 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3749 left_x + i * left_p, bottom_y - i,
3750 right_x + 1 - i * right_p, bottom_y - i);
3751
3752 /* Right. */
3753 if (right_p)
3754 for (i = 0; i < width; ++i)
3755 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3756 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3757
3758 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3759 }
3760
3761
3762 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3763 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3764 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3765 left side of the rectangle. RIGHT_P non-zero means draw a line
3766 on the right side of the rectangle. CLIP_RECT is the clipping
3767 rectangle to use when drawing. */
3768
3769 static void
3770 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3771 left_p, right_p, clip_rect)
3772 struct glyph_string *s;
3773 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3774 XRectangle *clip_rect;
3775 {
3776 XGCValues xgcv;
3777
3778 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3779 XSetForeground (s->display, s->gc, s->face->box_color);
3780 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3781
3782 /* Top. */
3783 XFillRectangle (s->display, s->window, s->gc,
3784 left_x, top_y, right_x - left_x + 1, width);
3785
3786 /* Left. */
3787 if (left_p)
3788 XFillRectangle (s->display, s->window, s->gc,
3789 left_x, top_y, width, bottom_y - top_y + 1);
3790
3791 /* Bottom. */
3792 XFillRectangle (s->display, s->window, s->gc,
3793 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3794
3795 /* Right. */
3796 if (right_p)
3797 XFillRectangle (s->display, s->window, s->gc,
3798 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3799
3800 XSetForeground (s->display, s->gc, xgcv.foreground);
3801 XSetClipMask (s->display, s->gc, None);
3802 }
3803
3804
3805 /* Draw a box around glyph string S. */
3806
3807 static void
3808 x_draw_glyph_string_box (s)
3809 struct glyph_string *s;
3810 {
3811 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3812 int left_p, right_p;
3813 struct glyph *last_glyph;
3814 XRectangle clip_rect;
3815
3816 last_x = window_box_right (s->w, s->area);
3817 if (s->row->full_width_p
3818 && !s->w->pseudo_window_p)
3819 {
3820 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3821 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3822 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3823 }
3824
3825 /* The glyph that may have a right box line. */
3826 last_glyph = (s->cmp || s->img
3827 ? s->first_glyph
3828 : s->first_glyph + s->nchars - 1);
3829
3830 width = abs (s->face->box_line_width);
3831 raised_p = s->face->box == FACE_RAISED_BOX;
3832 left_x = s->x;
3833 right_x = ((s->row->full_width_p
3834 ? last_x - 1
3835 : min (last_x, s->x + s->background_width) - 1));
3836 top_y = s->y;
3837 bottom_y = top_y + s->height - 1;
3838
3839 left_p = (s->first_glyph->left_box_line_p
3840 || (s->hl == DRAW_MOUSE_FACE
3841 && (s->prev == NULL
3842 || s->prev->hl != s->hl)));
3843 right_p = (last_glyph->right_box_line_p
3844 || (s->hl == DRAW_MOUSE_FACE
3845 && (s->next == NULL
3846 || s->next->hl != s->hl)));
3847
3848 x_get_glyph_string_clip_rect (s, &clip_rect);
3849
3850 if (s->face->box == FACE_SIMPLE_BOX)
3851 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3852 left_p, right_p, &clip_rect);
3853 else
3854 {
3855 x_setup_relief_colors (s);
3856 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3857 width, raised_p, left_p, right_p, &clip_rect);
3858 }
3859 }
3860
3861
3862 /* Draw foreground of image glyph string S. */
3863
3864 static void
3865 x_draw_image_foreground (s)
3866 struct glyph_string *s;
3867 {
3868 int x;
3869 int y = s->ybase - image_ascent (s->img, s->face);
3870
3871 /* If first glyph of S has a left box line, start drawing it to the
3872 right of that line. */
3873 if (s->face->box != FACE_NO_BOX
3874 && s->first_glyph->left_box_line_p)
3875 x = s->x + abs (s->face->box_line_width);
3876 else
3877 x = s->x;
3878
3879 /* If there is a margin around the image, adjust x- and y-position
3880 by that margin. */
3881 x += s->img->hmargin;
3882 y += s->img->vmargin;
3883
3884 if (s->img->pixmap)
3885 {
3886 if (s->img->mask)
3887 {
3888 /* We can't set both a clip mask and use XSetClipRectangles
3889 because the latter also sets a clip mask. We also can't
3890 trust on the shape extension to be available
3891 (XShapeCombineRegion). So, compute the rectangle to draw
3892 manually. */
3893 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3894 | GCFunction);
3895 XGCValues xgcv;
3896 XRectangle clip_rect, image_rect, r;
3897
3898 xgcv.clip_mask = s->img->mask;
3899 xgcv.clip_x_origin = x;
3900 xgcv.clip_y_origin = y;
3901 xgcv.function = GXcopy;
3902 XChangeGC (s->display, s->gc, mask, &xgcv);
3903
3904 x_get_glyph_string_clip_rect (s, &clip_rect);
3905 image_rect.x = x;
3906 image_rect.y = y;
3907 image_rect.width = s->img->width;
3908 image_rect.height = s->img->height;
3909 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3910 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3911 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3912 }
3913 else
3914 {
3915 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3916 XGCValues xgcv;
3917 XRectangle clip_rect, image_rect, r;
3918
3919 x_get_glyph_string_clip_rect (s, &clip_rect);
3920 image_rect.x = x;
3921 image_rect.y = y;
3922 image_rect.width = s->img->width;
3923 image_rect.height = s->img->height;
3924 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3925 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3926 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3927
3928 /* When the image has a mask, we can expect that at
3929 least part of a mouse highlight or a block cursor will
3930 be visible. If the image doesn't have a mask, make
3931 a block cursor visible by drawing a rectangle around
3932 the image. I believe it's looking better if we do
3933 nothing here for mouse-face. */
3934 if (s->hl == DRAW_CURSOR)
3935 XDrawRectangle (s->display, s->window, s->gc, x, y,
3936 s->img->width - 1, s->img->height - 1);
3937 }
3938 }
3939 else
3940 /* Draw a rectangle if image could not be loaded. */
3941 XDrawRectangle (s->display, s->window, s->gc, x, y,
3942 s->img->width - 1, s->img->height - 1);
3943 }
3944
3945
3946 /* Draw a relief around the image glyph string S. */
3947
3948 static void
3949 x_draw_image_relief (s)
3950 struct glyph_string *s;
3951 {
3952 int x0, y0, x1, y1, thick, raised_p;
3953 XRectangle r;
3954 int x;
3955 int y = s->ybase - image_ascent (s->img, s->face);
3956
3957 /* If first glyph of S has a left box line, start drawing it to the
3958 right of that line. */
3959 if (s->face->box != FACE_NO_BOX
3960 && s->first_glyph->left_box_line_p)
3961 x = s->x + abs (s->face->box_line_width);
3962 else
3963 x = s->x;
3964
3965 /* If there is a margin around the image, adjust x- and y-position
3966 by that margin. */
3967 x += s->img->hmargin;
3968 y += s->img->vmargin;
3969
3970 if (s->hl == DRAW_IMAGE_SUNKEN
3971 || s->hl == DRAW_IMAGE_RAISED)
3972 {
3973 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3974 raised_p = s->hl == DRAW_IMAGE_RAISED;
3975 }
3976 else
3977 {
3978 thick = abs (s->img->relief);
3979 raised_p = s->img->relief > 0;
3980 }
3981
3982 x0 = x - thick;
3983 y0 = y - thick;
3984 x1 = x + s->img->width + thick - 1;
3985 y1 = y + s->img->height + thick - 1;
3986
3987 x_setup_relief_colors (s);
3988 x_get_glyph_string_clip_rect (s, &r);
3989 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3990 }
3991
3992
3993 /* Draw the foreground of image glyph string S to PIXMAP. */
3994
3995 static void
3996 x_draw_image_foreground_1 (s, pixmap)
3997 struct glyph_string *s;
3998 Pixmap pixmap;
3999 {
4000 int x;
4001 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4002
4003 /* If first glyph of S has a left box line, start drawing it to the
4004 right of that line. */
4005 if (s->face->box != FACE_NO_BOX
4006 && s->first_glyph->left_box_line_p)
4007 x = abs (s->face->box_line_width);
4008 else
4009 x = 0;
4010
4011 /* If there is a margin around the image, adjust x- and y-position
4012 by that margin. */
4013 x += s->img->hmargin;
4014 y += s->img->vmargin;
4015
4016 if (s->img->pixmap)
4017 {
4018 if (s->img->mask)
4019 {
4020 /* We can't set both a clip mask and use XSetClipRectangles
4021 because the latter also sets a clip mask. We also can't
4022 trust on the shape extension to be available
4023 (XShapeCombineRegion). So, compute the rectangle to draw
4024 manually. */
4025 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4026 | GCFunction);
4027 XGCValues xgcv;
4028
4029 xgcv.clip_mask = s->img->mask;
4030 xgcv.clip_x_origin = x;
4031 xgcv.clip_y_origin = y;
4032 xgcv.function = GXcopy;
4033 XChangeGC (s->display, s->gc, mask, &xgcv);
4034
4035 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4036 0, 0, s->img->width, s->img->height, x, y);
4037 XSetClipMask (s->display, s->gc, None);
4038 }
4039 else
4040 {
4041 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4042 0, 0, s->img->width, s->img->height, x, y);
4043
4044 /* When the image has a mask, we can expect that at
4045 least part of a mouse highlight or a block cursor will
4046 be visible. If the image doesn't have a mask, make
4047 a block cursor visible by drawing a rectangle around
4048 the image. I believe it's looking better if we do
4049 nothing here for mouse-face. */
4050 if (s->hl == DRAW_CURSOR)
4051 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4052 s->img->width - 1, s->img->height - 1);
4053 }
4054 }
4055 else
4056 /* Draw a rectangle if image could not be loaded. */
4057 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4058 s->img->width - 1, s->img->height - 1);
4059 }
4060
4061
4062 /* Draw part of the background of glyph string S. X, Y, W, and H
4063 give the rectangle to draw. */
4064
4065 static void
4066 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4067 struct glyph_string *s;
4068 int x, y, w, h;
4069 {
4070 if (s->stippled_p)
4071 {
4072 /* Fill background with a stipple pattern. */
4073 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4074 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4075 XSetFillStyle (s->display, s->gc, FillSolid);
4076 }
4077 else
4078 x_clear_glyph_string_rect (s, x, y, w, h);
4079 }
4080
4081
4082 /* Draw image glyph string S.
4083
4084 s->y
4085 s->x +-------------------------
4086 | s->face->box
4087 |
4088 | +-------------------------
4089 | | s->img->margin
4090 | |
4091 | | +-------------------
4092 | | | the image
4093
4094 */
4095
4096 static void
4097 x_draw_image_glyph_string (s)
4098 struct glyph_string *s;
4099 {
4100 int x, y;
4101 int box_line_hwidth = abs (s->face->box_line_width);
4102 int box_line_vwidth = max (s->face->box_line_width, 0);
4103 int height;
4104 Pixmap pixmap = None;
4105
4106 height = s->height - 2 * box_line_vwidth;
4107
4108 /* Fill background with face under the image. Do it only if row is
4109 taller than image or if image has a clip mask to reduce
4110 flickering. */
4111 s->stippled_p = s->face->stipple != 0;
4112 if (height > s->img->height
4113 || s->img->hmargin
4114 || s->img->vmargin
4115 || s->img->mask
4116 || s->img->pixmap == 0
4117 || s->width != s->background_width)
4118 {
4119 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4120 x = s->x + box_line_hwidth;
4121 else
4122 x = s->x;
4123
4124 y = s->y + box_line_vwidth;
4125
4126 if (s->img->mask)
4127 {
4128 /* Create a pixmap as large as the glyph string. Fill it
4129 with the background color. Copy the image to it, using
4130 its mask. Copy the temporary pixmap to the display. */
4131 Screen *screen = FRAME_X_SCREEN (s->f);
4132 int depth = DefaultDepthOfScreen (screen);
4133
4134 /* Create a pixmap as large as the glyph string. */
4135 pixmap = XCreatePixmap (s->display, s->window,
4136 s->background_width,
4137 s->height, depth);
4138
4139 /* Don't clip in the following because we're working on the
4140 pixmap. */
4141 XSetClipMask (s->display, s->gc, None);
4142
4143 /* Fill the pixmap with the background color/stipple. */
4144 if (s->stippled_p)
4145 {
4146 /* Fill background with a stipple pattern. */
4147 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4148 XFillRectangle (s->display, pixmap, s->gc,
4149 0, 0, s->background_width, s->height);
4150 XSetFillStyle (s->display, s->gc, FillSolid);
4151 }
4152 else
4153 {
4154 XGCValues xgcv;
4155 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4156 &xgcv);
4157 XSetForeground (s->display, s->gc, xgcv.background);
4158 XFillRectangle (s->display, pixmap, s->gc,
4159 0, 0, s->background_width, s->height);
4160 XSetForeground (s->display, s->gc, xgcv.foreground);
4161 }
4162 }
4163 else
4164 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4165
4166 s->background_filled_p = 1;
4167 }
4168
4169 /* Draw the foreground. */
4170 if (pixmap != None)
4171 {
4172 x_draw_image_foreground_1 (s, pixmap);
4173 x_set_glyph_string_clipping (s);
4174 XCopyArea (s->display, pixmap, s->window, s->gc,
4175 0, 0, s->background_width, s->height, s->x, s->y);
4176 XFreePixmap (s->display, pixmap);
4177 }
4178 else
4179 x_draw_image_foreground (s);
4180
4181 /* If we must draw a relief around the image, do it. */
4182 if (s->img->relief
4183 || s->hl == DRAW_IMAGE_RAISED
4184 || s->hl == DRAW_IMAGE_SUNKEN)
4185 x_draw_image_relief (s);
4186 }
4187
4188
4189 /* Draw stretch glyph string S. */
4190
4191 static void
4192 x_draw_stretch_glyph_string (s)
4193 struct glyph_string *s;
4194 {
4195 xassert (s->first_glyph->type == STRETCH_GLYPH);
4196 s->stippled_p = s->face->stipple != 0;
4197
4198 if (s->hl == DRAW_CURSOR
4199 && !x_stretch_cursor_p)
4200 {
4201 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4202 as wide as the stretch glyph. */
4203 int width = min (CANON_X_UNIT (s->f), s->background_width);
4204
4205 /* Draw cursor. */
4206 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4207
4208 /* Clear rest using the GC of the original non-cursor face. */
4209 if (width < s->background_width)
4210 {
4211 GC gc = s->face->gc;
4212 int x = s->x + width, y = s->y;
4213 int w = s->background_width - width, h = s->height;
4214 XRectangle r;
4215
4216 x_get_glyph_string_clip_rect (s, &r);
4217 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4218
4219 if (s->face->stipple)
4220 {
4221 /* Fill background with a stipple pattern. */
4222 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4223 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4224 XSetFillStyle (s->display, gc, FillSolid);
4225 }
4226 else
4227 {
4228 XGCValues xgcv;
4229 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4230 XSetForeground (s->display, gc, xgcv.background);
4231 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4232 XSetForeground (s->display, gc, xgcv.foreground);
4233 }
4234 }
4235 }
4236 else
4237 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4238 s->height);
4239
4240 s->background_filled_p = 1;
4241 }
4242
4243
4244 /* Draw glyph string S. */
4245
4246 static void
4247 x_draw_glyph_string (s)
4248 struct glyph_string *s;
4249 {
4250 /* If S draws into the background of its successor, draw the
4251 background of the successor first so that S can draw into it.
4252 This makes S->next use XDrawString instead of XDrawImageString. */
4253 if (s->next && s->right_overhang && !s->for_overlaps_p)
4254 {
4255 xassert (s->next->img == NULL);
4256 x_set_glyph_string_gc (s->next);
4257 x_set_glyph_string_clipping (s->next);
4258 x_draw_glyph_string_background (s->next, 1);
4259 }
4260
4261 /* Set up S->gc, set clipping and draw S. */
4262 x_set_glyph_string_gc (s);
4263 x_set_glyph_string_clipping (s);
4264
4265 switch (s->first_glyph->type)
4266 {
4267 case IMAGE_GLYPH:
4268 x_draw_image_glyph_string (s);
4269 break;
4270
4271 case STRETCH_GLYPH:
4272 x_draw_stretch_glyph_string (s);
4273 break;
4274
4275 case CHAR_GLYPH:
4276 if (s->for_overlaps_p)
4277 s->background_filled_p = 1;
4278 else
4279 x_draw_glyph_string_background (s, 0);
4280 x_draw_glyph_string_foreground (s);
4281 break;
4282
4283 case COMPOSITE_GLYPH:
4284 if (s->for_overlaps_p || s->gidx > 0)
4285 s->background_filled_p = 1;
4286 else
4287 x_draw_glyph_string_background (s, 1);
4288 x_draw_composite_glyph_string_foreground (s);
4289 break;
4290
4291 default:
4292 abort ();
4293 }
4294
4295 if (!s->for_overlaps_p)
4296 {
4297 /* Draw underline. */
4298 if (s->face->underline_p)
4299 {
4300 unsigned long tem, h;
4301 int y;
4302
4303 /* Get the underline thickness. Default is 1 pixel. */
4304 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4305 h = 1;
4306
4307 /* Get the underline position. This is the recommended
4308 vertical offset in pixels from the baseline to the top of
4309 the underline. This is a signed value according to the
4310 specs, and its default is
4311
4312 ROUND ((maximum descent) / 2), with
4313 ROUND(x) = floor (x + 0.5) */
4314
4315 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4316 y = s->ybase + (long) tem;
4317 else if (s->face->font)
4318 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4319 else
4320 y = s->height - h;
4321
4322 if (s->face->underline_defaulted_p)
4323 XFillRectangle (s->display, s->window, s->gc,
4324 s->x, y, s->width, h);
4325 else
4326 {
4327 XGCValues xgcv;
4328 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4329 XSetForeground (s->display, s->gc, s->face->underline_color);
4330 XFillRectangle (s->display, s->window, s->gc,
4331 s->x, y, s->width, h);
4332 XSetForeground (s->display, s->gc, xgcv.foreground);
4333 }
4334 }
4335
4336 /* Draw overline. */
4337 if (s->face->overline_p)
4338 {
4339 unsigned long dy = 0, h = 1;
4340
4341 if (s->face->overline_color_defaulted_p)
4342 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4343 s->width, h);
4344 else
4345 {
4346 XGCValues xgcv;
4347 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4348 XSetForeground (s->display, s->gc, s->face->overline_color);
4349 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4350 s->width, h);
4351 XSetForeground (s->display, s->gc, xgcv.foreground);
4352 }
4353 }
4354
4355 /* Draw strike-through. */
4356 if (s->face->strike_through_p)
4357 {
4358 unsigned long h = 1;
4359 unsigned long dy = (s->height - h) / 2;
4360
4361 if (s->face->strike_through_color_defaulted_p)
4362 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4363 s->width, h);
4364 else
4365 {
4366 XGCValues xgcv;
4367 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4368 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4369 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4370 s->width, h);
4371 XSetForeground (s->display, s->gc, xgcv.foreground);
4372 }
4373 }
4374
4375 /* Draw relief. */
4376 if (s->face->box != FACE_NO_BOX)
4377 x_draw_glyph_string_box (s);
4378 }
4379
4380 /* Reset clipping. */
4381 XSetClipMask (s->display, s->gc, None);
4382 }
4383
4384
4385 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4386 struct face **, int));
4387
4388
4389 /* Fill glyph string S with composition components specified by S->cmp.
4390
4391 FACES is an array of faces for all components of this composition.
4392 S->gidx is the index of the first component for S.
4393 OVERLAPS_P non-zero means S should draw the foreground only, and
4394 use its physical height for clipping.
4395
4396 Value is the index of a component not in S. */
4397
4398 static int
4399 x_fill_composite_glyph_string (s, faces, overlaps_p)
4400 struct glyph_string *s;
4401 struct face **faces;
4402 int overlaps_p;
4403 {
4404 int i;
4405
4406 xassert (s);
4407
4408 s->for_overlaps_p = overlaps_p;
4409
4410 s->face = faces[s->gidx];
4411 s->font = s->face->font;
4412 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4413
4414 /* For all glyphs of this composition, starting at the offset
4415 S->gidx, until we reach the end of the definition or encounter a
4416 glyph that requires the different face, add it to S. */
4417 ++s->nchars;
4418 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4419 ++s->nchars;
4420
4421 /* All glyph strings for the same composition has the same width,
4422 i.e. the width set for the first component of the composition. */
4423
4424 s->width = s->first_glyph->pixel_width;
4425
4426 /* If the specified font could not be loaded, use the frame's
4427 default font, but record the fact that we couldn't load it in
4428 the glyph string so that we can draw rectangles for the
4429 characters of the glyph string. */
4430 if (s->font == NULL)
4431 {
4432 s->font_not_found_p = 1;
4433 s->font = FRAME_FONT (s->f);
4434 }
4435
4436 /* Adjust base line for subscript/superscript text. */
4437 s->ybase += s->first_glyph->voffset;
4438
4439 xassert (s->face && s->face->gc);
4440
4441 /* This glyph string must always be drawn with 16-bit functions. */
4442 s->two_byte_p = 1;
4443
4444 return s->gidx + s->nchars;
4445 }
4446
4447
4448 /* Fill glyph string S from a sequence of character glyphs.
4449
4450 FACE_ID is the face id of the string. START is the index of the
4451 first glyph to consider, END is the index of the last + 1.
4452 OVERLAPS_P non-zero means S should draw the foreground only, and
4453 use its physical height for clipping.
4454
4455 Value is the index of the first glyph not in S. */
4456
4457 static int
4458 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4459 struct glyph_string *s;
4460 int face_id;
4461 int start, end, overlaps_p;
4462 {
4463 struct glyph *glyph, *last;
4464 int voffset;
4465 int glyph_not_available_p;
4466
4467 xassert (s->f == XFRAME (s->w->frame));
4468 xassert (s->nchars == 0);
4469 xassert (start >= 0 && end > start);
4470
4471 s->for_overlaps_p = overlaps_p,
4472 glyph = s->row->glyphs[s->area] + start;
4473 last = s->row->glyphs[s->area] + end;
4474 voffset = glyph->voffset;
4475
4476 glyph_not_available_p = glyph->glyph_not_available_p;
4477
4478 while (glyph < last
4479 && glyph->type == CHAR_GLYPH
4480 && glyph->voffset == voffset
4481 /* Same face id implies same font, nowadays. */
4482 && glyph->face_id == face_id
4483 && glyph->glyph_not_available_p == glyph_not_available_p)
4484 {
4485 int two_byte_p;
4486
4487 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4488 s->char2b + s->nchars,
4489 &two_byte_p);
4490 s->two_byte_p = two_byte_p;
4491 ++s->nchars;
4492 xassert (s->nchars <= end - start);
4493 s->width += glyph->pixel_width;
4494 ++glyph;
4495 }
4496
4497 s->font = s->face->font;
4498 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4499
4500 /* If the specified font could not be loaded, use the frame's font,
4501 but record the fact that we couldn't load it in
4502 S->font_not_found_p so that we can draw rectangles for the
4503 characters of the glyph string. */
4504 if (s->font == NULL || glyph_not_available_p)
4505 {
4506 s->font_not_found_p = 1;
4507 s->font = FRAME_FONT (s->f);
4508 }
4509
4510 /* Adjust base line for subscript/superscript text. */
4511 s->ybase += voffset;
4512
4513 xassert (s->face && s->face->gc);
4514 return glyph - s->row->glyphs[s->area];
4515 }
4516
4517
4518 /* Fill glyph string S from image glyph S->first_glyph. */
4519
4520 static void
4521 x_fill_image_glyph_string (s)
4522 struct glyph_string *s;
4523 {
4524 xassert (s->first_glyph->type == IMAGE_GLYPH);
4525 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4526 xassert (s->img);
4527 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4528 s->font = s->face->font;
4529 s->width = s->first_glyph->pixel_width;
4530
4531 /* Adjust base line for subscript/superscript text. */
4532 s->ybase += s->first_glyph->voffset;
4533 }
4534
4535
4536 /* Fill glyph string S from a sequence of stretch glyphs.
4537
4538 ROW is the glyph row in which the glyphs are found, AREA is the
4539 area within the row. START is the index of the first glyph to
4540 consider, END is the index of the last + 1.
4541
4542 Value is the index of the first glyph not in S. */
4543
4544 static int
4545 x_fill_stretch_glyph_string (s, row, area, start, end)
4546 struct glyph_string *s;
4547 struct glyph_row *row;
4548 enum glyph_row_area area;
4549 int start, end;
4550 {
4551 struct glyph *glyph, *last;
4552 int voffset, face_id;
4553
4554 xassert (s->first_glyph->type == STRETCH_GLYPH);
4555
4556 glyph = s->row->glyphs[s->area] + start;
4557 last = s->row->glyphs[s->area] + end;
4558 face_id = glyph->face_id;
4559 s->face = FACE_FROM_ID (s->f, face_id);
4560 s->font = s->face->font;
4561 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4562 s->width = glyph->pixel_width;
4563 voffset = glyph->voffset;
4564
4565 for (++glyph;
4566 (glyph < last
4567 && glyph->type == STRETCH_GLYPH
4568 && glyph->voffset == voffset
4569 && glyph->face_id == face_id);
4570 ++glyph)
4571 s->width += glyph->pixel_width;
4572
4573 /* Adjust base line for subscript/superscript text. */
4574 s->ybase += voffset;
4575
4576 xassert (s->face && s->face->gc);
4577 return glyph - s->row->glyphs[s->area];
4578 }
4579
4580
4581 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4582 of XChar2b structures for S; it can't be allocated in
4583 x_init_glyph_string because it must be allocated via `alloca'. W
4584 is the window on which S is drawn. ROW and AREA are the glyph row
4585 and area within the row from which S is constructed. START is the
4586 index of the first glyph structure covered by S. HL is a
4587 face-override for drawing S. */
4588
4589 static void
4590 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4591 struct glyph_string *s;
4592 XChar2b *char2b;
4593 struct window *w;
4594 struct glyph_row *row;
4595 enum glyph_row_area area;
4596 int start;
4597 enum draw_glyphs_face hl;
4598 {
4599 bzero (s, sizeof *s);
4600 s->w = w;
4601 s->f = XFRAME (w->frame);
4602 s->display = FRAME_X_DISPLAY (s->f);
4603 s->window = FRAME_X_WINDOW (s->f);
4604 s->char2b = char2b;
4605 s->hl = hl;
4606 s->row = row;
4607 s->area = area;
4608 s->first_glyph = row->glyphs[area] + start;
4609 s->height = row->height;
4610 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4611
4612 /* Display the internal border below the tool-bar window. */
4613 if (s->w == XWINDOW (s->f->tool_bar_window))
4614 s->y -= s->f->output_data.x->internal_border_width;
4615
4616 s->ybase = s->y + row->ascent;
4617 }
4618
4619
4620 /* Set background width of glyph string S. START is the index of the
4621 first glyph following S. LAST_X is the right-most x-position + 1
4622 in the drawing area. */
4623
4624 static INLINE void
4625 x_set_glyph_string_background_width (s, start, last_x)
4626 struct glyph_string *s;
4627 int start;
4628 int last_x;
4629 {
4630 /* If the face of this glyph string has to be drawn to the end of
4631 the drawing area, set S->extends_to_end_of_line_p. */
4632 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4633
4634 if (start == s->row->used[s->area]
4635 && s->hl == DRAW_NORMAL_TEXT
4636 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4637 || s->face->background != default_face->background
4638 || s->face->stipple != default_face->stipple))
4639 s->extends_to_end_of_line_p = 1;
4640
4641 /* If S extends its face to the end of the line, set its
4642 background_width to the distance to the right edge of the drawing
4643 area. */
4644 if (s->extends_to_end_of_line_p)
4645 s->background_width = last_x - s->x + 1;
4646 else
4647 s->background_width = s->width;
4648 }
4649
4650
4651 /* Add a glyph string for a stretch glyph to the list of strings
4652 between HEAD and TAIL. START is the index of the stretch glyph in
4653 row area AREA of glyph row ROW. END is the index of the last glyph
4654 in that glyph row area. X is the current output position assigned
4655 to the new glyph string constructed. HL overrides that face of the
4656 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4657 is the right-most x-position of the drawing area. */
4658
4659 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4660 and below -- keep them on one line. */
4661 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4662 do \
4663 { \
4664 s = (struct glyph_string *) alloca (sizeof *s); \
4665 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4666 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4667 x_append_glyph_string (&HEAD, &TAIL, s); \
4668 s->x = (X); \
4669 } \
4670 while (0)
4671
4672
4673 /* Add a glyph string for an image glyph to the list of strings
4674 between HEAD and TAIL. START is the index of the image glyph in
4675 row area AREA of glyph row ROW. END is the index of the last glyph
4676 in that glyph row area. X is the current output position assigned
4677 to the new glyph string constructed. HL overrides that face of the
4678 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4679 is the right-most x-position of the drawing area. */
4680
4681 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4682 do \
4683 { \
4684 s = (struct glyph_string *) alloca (sizeof *s); \
4685 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4686 x_fill_image_glyph_string (s); \
4687 x_append_glyph_string (&HEAD, &TAIL, s); \
4688 ++START; \
4689 s->x = (X); \
4690 } \
4691 while (0)
4692
4693
4694 /* Add a glyph string for a sequence of character glyphs to the list
4695 of strings between HEAD and TAIL. START is the index of the first
4696 glyph in row area AREA of glyph row ROW that is part of the new
4697 glyph string. END is the index of the last glyph in that glyph row
4698 area. X is the current output position assigned to the new glyph
4699 string constructed. HL overrides that face of the glyph; e.g. it
4700 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4701 right-most x-position of the drawing area. */
4702
4703 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4704 do \
4705 { \
4706 int c, face_id; \
4707 XChar2b *char2b; \
4708 \
4709 c = (ROW)->glyphs[AREA][START].u.ch; \
4710 face_id = (ROW)->glyphs[AREA][START].face_id; \
4711 \
4712 s = (struct glyph_string *) alloca (sizeof *s); \
4713 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4714 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4715 x_append_glyph_string (&HEAD, &TAIL, s); \
4716 s->x = (X); \
4717 START = x_fill_glyph_string (s, face_id, START, END, \
4718 OVERLAPS_P); \
4719 } \
4720 while (0)
4721
4722
4723 /* Add a glyph string for a composite sequence to the list of strings
4724 between HEAD and TAIL. START is the index of the first glyph in
4725 row area AREA of glyph row ROW that is part of the new glyph
4726 string. END is the index of the last glyph in that glyph row area.
4727 X is the current output position assigned to the new glyph string
4728 constructed. HL overrides that face of the glyph; e.g. it is
4729 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4730 x-position of the drawing area. */
4731
4732 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4733 do { \
4734 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4735 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4736 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4737 struct composition *cmp = composition_table[cmp_id]; \
4738 int glyph_len = cmp->glyph_len; \
4739 XChar2b *char2b; \
4740 struct face **faces; \
4741 struct glyph_string *first_s = NULL; \
4742 int n; \
4743 \
4744 base_face = base_face->ascii_face; \
4745 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4746 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4747 /* At first, fill in `char2b' and `faces'. */ \
4748 for (n = 0; n < glyph_len; n++) \
4749 { \
4750 int c = COMPOSITION_GLYPH (cmp, n); \
4751 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4752 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4753 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4754 this_face_id, char2b + n, 1); \
4755 } \
4756 \
4757 /* Make glyph_strings for each glyph sequence that is drawable by \
4758 the same face, and append them to HEAD/TAIL. */ \
4759 for (n = 0; n < cmp->glyph_len;) \
4760 { \
4761 s = (struct glyph_string *) alloca (sizeof *s); \
4762 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4763 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4764 s->cmp = cmp; \
4765 s->gidx = n; \
4766 s->x = (X); \
4767 \
4768 if (n == 0) \
4769 first_s = s; \
4770 \
4771 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4772 } \
4773 \
4774 ++START; \
4775 s = first_s; \
4776 } while (0)
4777
4778
4779 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4780 of AREA of glyph row ROW on window W between indices START and END.
4781 HL overrides the face for drawing glyph strings, e.g. it is
4782 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4783 x-positions of the drawing area.
4784
4785 This is an ugly monster macro construct because we must use alloca
4786 to allocate glyph strings (because x_draw_glyphs can be called
4787 asynchronously). */
4788
4789 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4790 do \
4791 { \
4792 HEAD = TAIL = NULL; \
4793 while (START < END) \
4794 { \
4795 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4796 switch (first_glyph->type) \
4797 { \
4798 case CHAR_GLYPH: \
4799 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4800 TAIL, HL, X, LAST_X, \
4801 OVERLAPS_P); \
4802 break; \
4803 \
4804 case COMPOSITE_GLYPH: \
4805 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4806 HEAD, TAIL, HL, X, LAST_X,\
4807 OVERLAPS_P); \
4808 break; \
4809 \
4810 case STRETCH_GLYPH: \
4811 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4812 HEAD, TAIL, HL, X, LAST_X); \
4813 break; \
4814 \
4815 case IMAGE_GLYPH: \
4816 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4817 TAIL, HL, X, LAST_X); \
4818 break; \
4819 \
4820 default: \
4821 abort (); \
4822 } \
4823 \
4824 x_set_glyph_string_background_width (s, START, LAST_X); \
4825 (X) += s->width; \
4826 } \
4827 } \
4828 while (0)
4829
4830
4831 /* Draw glyphs between START and END in AREA of ROW on window W,
4832 starting at x-position X. X is relative to AREA in W. HL is a
4833 face-override with the following meaning:
4834
4835 DRAW_NORMAL_TEXT draw normally
4836 DRAW_CURSOR draw in cursor face
4837 DRAW_MOUSE_FACE draw in mouse face.
4838 DRAW_INVERSE_VIDEO draw in mode line face
4839 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4840 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4841
4842 If REAL_START is non-null, return in *REAL_START the real starting
4843 position for display. This can be different from START in case
4844 overlapping glyphs must be displayed. If REAL_END is non-null,
4845 return in *REAL_END the real end position for display. This can be
4846 different from END in case overlapping glyphs must be displayed.
4847
4848 If OVERLAPS_P is non-zero, draw only the foreground of characters
4849 and clip to the physical height of ROW.
4850
4851 Value is the x-position reached, relative to AREA of W. */
4852
4853 static int
4854 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4855 overlaps_p)
4856 struct window *w;
4857 int x;
4858 struct glyph_row *row;
4859 enum glyph_row_area area;
4860 int start, end;
4861 enum draw_glyphs_face hl;
4862 int *real_start, *real_end;
4863 int overlaps_p;
4864 {
4865 struct glyph_string *head, *tail;
4866 struct glyph_string *s;
4867 int last_x, area_width;
4868 int x_reached;
4869 int i, j;
4870
4871 /* Let's rather be paranoid than getting a SEGV. */
4872 end = min (end, row->used[area]);
4873 start = max (0, start);
4874 start = min (end, start);
4875 if (real_start)
4876 *real_start = start;
4877 if (real_end)
4878 *real_end = end;
4879
4880 /* Translate X to frame coordinates. Set last_x to the right
4881 end of the drawing area. */
4882 if (row->full_width_p)
4883 {
4884 /* X is relative to the left edge of W, without scroll bars
4885 or flag areas. */
4886 struct frame *f = XFRAME (w->frame);
4887 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4888 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4889
4890 x += window_left_x;
4891 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4892 last_x = window_left_x + area_width;
4893
4894 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4895 {
4896 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4897 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4898 last_x += width;
4899 else
4900 x -= width;
4901 }
4902
4903 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4904 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4905 }
4906 else
4907 {
4908 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4909 area_width = window_box_width (w, area);
4910 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4911 }
4912
4913 /* Build a doubly-linked list of glyph_string structures between
4914 head and tail from what we have to draw. Note that the macro
4915 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4916 the reason we use a separate variable `i'. */
4917 i = start;
4918 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4919 overlaps_p);
4920 if (tail)
4921 x_reached = tail->x + tail->background_width;
4922 else
4923 x_reached = x;
4924
4925 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4926 the row, redraw some glyphs in front or following the glyph
4927 strings built above. */
4928 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4929 {
4930 int dummy_x = 0;
4931 struct glyph_string *h, *t;
4932
4933 /* Compute overhangs for all glyph strings. */
4934 for (s = head; s; s = s->next)
4935 x_compute_glyph_string_overhangs (s);
4936
4937 /* Prepend glyph strings for glyphs in front of the first glyph
4938 string that are overwritten because of the first glyph
4939 string's left overhang. The background of all strings
4940 prepended must be drawn because the first glyph string
4941 draws over it. */
4942 i = x_left_overwritten (head);
4943 if (i >= 0)
4944 {
4945 j = i;
4946 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4947 DRAW_NORMAL_TEXT, dummy_x, last_x,
4948 overlaps_p);
4949 start = i;
4950 if (real_start)
4951 *real_start = start;
4952 x_compute_overhangs_and_x (t, head->x, 1);
4953 x_prepend_glyph_string_lists (&head, &tail, h, t);
4954 }
4955
4956 /* Prepend glyph strings for glyphs in front of the first glyph
4957 string that overwrite that glyph string because of their
4958 right overhang. For these strings, only the foreground must
4959 be drawn, because it draws over the glyph string at `head'.
4960 The background must not be drawn because this would overwrite
4961 right overhangs of preceding glyphs for which no glyph
4962 strings exist. */
4963 i = x_left_overwriting (head);
4964 if (i >= 0)
4965 {
4966 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4967 DRAW_NORMAL_TEXT, dummy_x, last_x,
4968 overlaps_p);
4969 for (s = h; s; s = s->next)
4970 s->background_filled_p = 1;
4971 if (real_start)
4972 *real_start = i;
4973 x_compute_overhangs_and_x (t, head->x, 1);
4974 x_prepend_glyph_string_lists (&head, &tail, h, t);
4975 }
4976
4977 /* Append glyphs strings for glyphs following the last glyph
4978 string tail that are overwritten by tail. The background of
4979 these strings has to be drawn because tail's foreground draws
4980 over it. */
4981 i = x_right_overwritten (tail);
4982 if (i >= 0)
4983 {
4984 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4985 DRAW_NORMAL_TEXT, x, last_x,
4986 overlaps_p);
4987 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4988 x_append_glyph_string_lists (&head, &tail, h, t);
4989 if (real_end)
4990 *real_end = i;
4991 }
4992
4993 /* Append glyph strings for glyphs following the last glyph
4994 string tail that overwrite tail. The foreground of such
4995 glyphs has to be drawn because it writes into the background
4996 of tail. The background must not be drawn because it could
4997 paint over the foreground of following glyphs. */
4998 i = x_right_overwriting (tail);
4999 if (i >= 0)
5000 {
5001 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5002 DRAW_NORMAL_TEXT, x, last_x,
5003 overlaps_p);
5004 for (s = h; s; s = s->next)
5005 s->background_filled_p = 1;
5006 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5007 x_append_glyph_string_lists (&head, &tail, h, t);
5008 if (real_end)
5009 *real_end = i;
5010 }
5011 }
5012
5013 /* Draw all strings. */
5014 for (s = head; s; s = s->next)
5015 x_draw_glyph_string (s);
5016
5017 /* Value is the x-position up to which drawn, relative to AREA of W.
5018 This doesn't include parts drawn because of overhangs. */
5019 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5020 if (!row->full_width_p)
5021 {
5022 if (area > LEFT_MARGIN_AREA)
5023 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5024 if (area > TEXT_AREA)
5025 x_reached -= window_box_width (w, TEXT_AREA);
5026 }
5027
5028 return x_reached;
5029 }
5030
5031
5032 /* Fix the display of area AREA of overlapping row ROW in window W. */
5033
5034 static void
5035 x_fix_overlapping_area (w, row, area)
5036 struct window *w;
5037 struct glyph_row *row;
5038 enum glyph_row_area area;
5039 {
5040 int i, x;
5041
5042 BLOCK_INPUT;
5043
5044 if (area == LEFT_MARGIN_AREA)
5045 x = 0;
5046 else if (area == TEXT_AREA)
5047 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5048 else
5049 x = (window_box_width (w, LEFT_MARGIN_AREA)
5050 + window_box_width (w, TEXT_AREA));
5051
5052 for (i = 0; i < row->used[area];)
5053 {
5054 if (row->glyphs[area][i].overlaps_vertically_p)
5055 {
5056 int start = i, start_x = x;
5057
5058 do
5059 {
5060 x += row->glyphs[area][i].pixel_width;
5061 ++i;
5062 }
5063 while (i < row->used[area]
5064 && row->glyphs[area][i].overlaps_vertically_p);
5065
5066 x_draw_glyphs (w, start_x, row, area, start, i,
5067 (row->inverse_p
5068 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5069 NULL, NULL, 1);
5070 }
5071 else
5072 {
5073 x += row->glyphs[area][i].pixel_width;
5074 ++i;
5075 }
5076 }
5077
5078 UNBLOCK_INPUT;
5079 }
5080
5081
5082 /* Output LEN glyphs starting at START at the nominal cursor position.
5083 Advance the nominal cursor over the text. The global variable
5084 updated_window contains the window being updated, updated_row is
5085 the glyph row being updated, and updated_area is the area of that
5086 row being updated. */
5087
5088 static void
5089 x_write_glyphs (start, len)
5090 struct glyph *start;
5091 int len;
5092 {
5093 int x, hpos, real_start, real_end;
5094
5095 xassert (updated_window && updated_row);
5096 BLOCK_INPUT;
5097
5098 /* Write glyphs. */
5099
5100 hpos = start - updated_row->glyphs[updated_area];
5101 x = x_draw_glyphs (updated_window, output_cursor.x,
5102 updated_row, updated_area,
5103 hpos, hpos + len,
5104 (updated_row->inverse_p
5105 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5106 &real_start, &real_end, 0);
5107
5108 /* If we drew over the cursor, note that it is not visible any more. */
5109 note_overwritten_text_cursor (updated_window, real_start,
5110 real_end - real_start);
5111
5112 UNBLOCK_INPUT;
5113
5114 /* Advance the output cursor. */
5115 output_cursor.hpos += len;
5116 output_cursor.x = x;
5117 }
5118
5119
5120 /* Insert LEN glyphs from START at the nominal cursor position. */
5121
5122 static void
5123 x_insert_glyphs (start, len)
5124 struct glyph *start;
5125 register int len;
5126 {
5127 struct frame *f;
5128 struct window *w;
5129 int line_height, shift_by_width, shifted_region_width;
5130 struct glyph_row *row;
5131 struct glyph *glyph;
5132 int frame_x, frame_y, hpos, real_start, real_end;
5133
5134 xassert (updated_window && updated_row);
5135 BLOCK_INPUT;
5136 w = updated_window;
5137 f = XFRAME (WINDOW_FRAME (w));
5138
5139 /* Get the height of the line we are in. */
5140 row = updated_row;
5141 line_height = row->height;
5142
5143 /* Get the width of the glyphs to insert. */
5144 shift_by_width = 0;
5145 for (glyph = start; glyph < start + len; ++glyph)
5146 shift_by_width += glyph->pixel_width;
5147
5148 /* Get the width of the region to shift right. */
5149 shifted_region_width = (window_box_width (w, updated_area)
5150 - output_cursor.x
5151 - shift_by_width);
5152
5153 /* Shift right. */
5154 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5155 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5156 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5157 f->output_data.x->normal_gc,
5158 frame_x, frame_y,
5159 shifted_region_width, line_height,
5160 frame_x + shift_by_width, frame_y);
5161
5162 /* Write the glyphs. */
5163 hpos = start - row->glyphs[updated_area];
5164 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5165 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5166 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5167
5168 /* Advance the output cursor. */
5169 output_cursor.hpos += len;
5170 output_cursor.x += shift_by_width;
5171 UNBLOCK_INPUT;
5172 }
5173
5174
5175 /* Delete N glyphs at the nominal cursor position. Not implemented
5176 for X frames. */
5177
5178 static void
5179 x_delete_glyphs (n)
5180 register int n;
5181 {
5182 abort ();
5183 }
5184
5185
5186 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5187 If they are <= 0, this is probably an error. */
5188
5189 void
5190 x_clear_area (dpy, window, x, y, width, height, exposures)
5191 Display *dpy;
5192 Window window;
5193 int x, y;
5194 int width, height;
5195 int exposures;
5196 {
5197 xassert (width > 0 && height > 0);
5198 XClearArea (dpy, window, x, y, width, height, exposures);
5199 }
5200
5201
5202 /* Erase the current text line from the nominal cursor position
5203 (inclusive) to pixel column TO_X (exclusive). The idea is that
5204 everything from TO_X onward is already erased.
5205
5206 TO_X is a pixel position relative to updated_area of
5207 updated_window. TO_X == -1 means clear to the end of this area. */
5208
5209 static void
5210 x_clear_end_of_line (to_x)
5211 int to_x;
5212 {
5213 struct frame *f;
5214 struct window *w = updated_window;
5215 int max_x, min_y, max_y;
5216 int from_x, from_y, to_y;
5217
5218 xassert (updated_window && updated_row);
5219 f = XFRAME (w->frame);
5220
5221 if (updated_row->full_width_p)
5222 {
5223 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5224 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5225 && !w->pseudo_window_p)
5226 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5227 }
5228 else
5229 max_x = window_box_width (w, updated_area);
5230 max_y = window_text_bottom_y (w);
5231
5232 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5233 of window. For TO_X > 0, truncate to end of drawing area. */
5234 if (to_x == 0)
5235 return;
5236 else if (to_x < 0)
5237 to_x = max_x;
5238 else
5239 to_x = min (to_x, max_x);
5240
5241 to_y = min (max_y, output_cursor.y + updated_row->height);
5242
5243 /* Notice if the cursor will be cleared by this operation. */
5244 if (!updated_row->full_width_p)
5245 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5246
5247 from_x = output_cursor.x;
5248
5249 /* Translate to frame coordinates. */
5250 if (updated_row->full_width_p)
5251 {
5252 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5253 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5254 }
5255 else
5256 {
5257 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5258 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5259 }
5260
5261 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5262 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5263 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5264
5265 /* Prevent inadvertently clearing to end of the X window. */
5266 if (to_x > from_x && to_y > from_y)
5267 {
5268 BLOCK_INPUT;
5269 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5270 from_x, from_y, to_x - from_x, to_y - from_y,
5271 False);
5272 UNBLOCK_INPUT;
5273 }
5274 }
5275
5276
5277 /* Clear entire frame. If updating_frame is non-null, clear that
5278 frame. Otherwise clear the selected frame. */
5279
5280 static void
5281 x_clear_frame ()
5282 {
5283 struct frame *f;
5284
5285 if (updating_frame)
5286 f = updating_frame;
5287 else
5288 f = SELECTED_FRAME ();
5289
5290 /* Clearing the frame will erase any cursor, so mark them all as no
5291 longer visible. */
5292 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5293 output_cursor.hpos = output_cursor.vpos = 0;
5294 output_cursor.x = -1;
5295
5296 /* We don't set the output cursor here because there will always
5297 follow an explicit cursor_to. */
5298 BLOCK_INPUT;
5299 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5300
5301 /* We have to clear the scroll bars, too. If we have changed
5302 colors or something like that, then they should be notified. */
5303 x_scroll_bar_clear (f);
5304
5305 XFlush (FRAME_X_DISPLAY (f));
5306 UNBLOCK_INPUT;
5307 }
5308
5309
5310 \f
5311 /* Invert the middle quarter of the frame for .15 sec. */
5312
5313 /* We use the select system call to do the waiting, so we have to make
5314 sure it's available. If it isn't, we just won't do visual bells. */
5315
5316 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5317
5318
5319 /* Subtract the `struct timeval' values X and Y, storing the result in
5320 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5321
5322 static int
5323 timeval_subtract (result, x, y)
5324 struct timeval *result, x, y;
5325 {
5326 /* Perform the carry for the later subtraction by updating y. This
5327 is safer because on some systems the tv_sec member is unsigned. */
5328 if (x.tv_usec < y.tv_usec)
5329 {
5330 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5331 y.tv_usec -= 1000000 * nsec;
5332 y.tv_sec += nsec;
5333 }
5334
5335 if (x.tv_usec - y.tv_usec > 1000000)
5336 {
5337 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5338 y.tv_usec += 1000000 * nsec;
5339 y.tv_sec -= nsec;
5340 }
5341
5342 /* Compute the time remaining to wait. tv_usec is certainly
5343 positive. */
5344 result->tv_sec = x.tv_sec - y.tv_sec;
5345 result->tv_usec = x.tv_usec - y.tv_usec;
5346
5347 /* Return indication of whether the result should be considered
5348 negative. */
5349 return x.tv_sec < y.tv_sec;
5350 }
5351
5352 void
5353 XTflash (f)
5354 struct frame *f;
5355 {
5356 BLOCK_INPUT;
5357
5358 {
5359 GC gc;
5360
5361 /* Create a GC that will use the GXxor function to flip foreground
5362 pixels into background pixels. */
5363 {
5364 XGCValues values;
5365
5366 values.function = GXxor;
5367 values.foreground = (f->output_data.x->foreground_pixel
5368 ^ f->output_data.x->background_pixel);
5369
5370 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5371 GCFunction | GCForeground, &values);
5372 }
5373
5374 {
5375 /* Get the height not including a menu bar widget. */
5376 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5377 /* Height of each line to flash. */
5378 int flash_height = FRAME_LINE_HEIGHT (f);
5379 /* These will be the left and right margins of the rectangles. */
5380 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5381 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5382
5383 int width;
5384
5385 /* Don't flash the area between a scroll bar and the frame
5386 edge it is next to. */
5387 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5388 {
5389 case vertical_scroll_bar_left:
5390 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5391 break;
5392
5393 case vertical_scroll_bar_right:
5394 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5395 break;
5396
5397 default:
5398 break;
5399 }
5400
5401 width = flash_right - flash_left;
5402
5403 /* If window is tall, flash top and bottom line. */
5404 if (height > 3 * FRAME_LINE_HEIGHT (f))
5405 {
5406 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5407 flash_left,
5408 (FRAME_INTERNAL_BORDER_WIDTH (f)
5409 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5410 width, flash_height);
5411 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5412 flash_left,
5413 (height - flash_height
5414 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5415 width, flash_height);
5416 }
5417 else
5418 /* If it is short, flash it all. */
5419 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5420 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5421 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5422
5423 x_flush (f);
5424
5425 {
5426 struct timeval wakeup;
5427
5428 EMACS_GET_TIME (wakeup);
5429
5430 /* Compute time to wait until, propagating carry from usecs. */
5431 wakeup.tv_usec += 150000;
5432 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5433 wakeup.tv_usec %= 1000000;
5434
5435 /* Keep waiting until past the time wakeup. */
5436 while (1)
5437 {
5438 struct timeval timeout;
5439
5440 EMACS_GET_TIME (timeout);
5441
5442 /* In effect, timeout = wakeup - timeout.
5443 Break if result would be negative. */
5444 if (timeval_subtract (&timeout, wakeup, timeout))
5445 break;
5446
5447 /* Try to wait that long--but we might wake up sooner. */
5448 select (0, NULL, NULL, NULL, &timeout);
5449 }
5450 }
5451
5452 /* If window is tall, flash top and bottom line. */
5453 if (height > 3 * FRAME_LINE_HEIGHT (f))
5454 {
5455 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5456 flash_left,
5457 (FRAME_INTERNAL_BORDER_WIDTH (f)
5458 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5459 width, flash_height);
5460 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5461 flash_left,
5462 (height - flash_height
5463 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5464 width, flash_height);
5465 }
5466 else
5467 /* If it is short, flash it all. */
5468 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5469 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5470 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5471
5472 XFreeGC (FRAME_X_DISPLAY (f), gc);
5473 x_flush (f);
5474 }
5475 }
5476
5477 UNBLOCK_INPUT;
5478 }
5479
5480 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5481
5482
5483 /* Make audible bell. */
5484
5485 void
5486 XTring_bell ()
5487 {
5488 struct frame *f = SELECTED_FRAME ();
5489
5490 if (FRAME_X_DISPLAY (f))
5491 {
5492 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5493 if (visible_bell)
5494 XTflash (f);
5495 else
5496 #endif
5497 {
5498 BLOCK_INPUT;
5499 XBell (FRAME_X_DISPLAY (f), 0);
5500 XFlush (FRAME_X_DISPLAY (f));
5501 UNBLOCK_INPUT;
5502 }
5503 }
5504 }
5505
5506 \f
5507 /* Specify how many text lines, from the top of the window,
5508 should be affected by insert-lines and delete-lines operations.
5509 This, and those operations, are used only within an update
5510 that is bounded by calls to x_update_begin and x_update_end. */
5511
5512 static void
5513 XTset_terminal_window (n)
5514 register int n;
5515 {
5516 /* This function intentionally left blank. */
5517 }
5518
5519
5520 \f
5521 /***********************************************************************
5522 Line Dance
5523 ***********************************************************************/
5524
5525 /* Perform an insert-lines or delete-lines operation, inserting N
5526 lines or deleting -N lines at vertical position VPOS. */
5527
5528 static void
5529 x_ins_del_lines (vpos, n)
5530 int vpos, n;
5531 {
5532 abort ();
5533 }
5534
5535
5536 /* Scroll part of the display as described by RUN. */
5537
5538 static void
5539 x_scroll_run (w, run)
5540 struct window *w;
5541 struct run *run;
5542 {
5543 struct frame *f = XFRAME (w->frame);
5544 int x, y, width, height, from_y, to_y, bottom_y;
5545
5546 /* Get frame-relative bounding box of the text display area of W,
5547 without mode lines. Include in this box the flags areas to the
5548 left and right of W. */
5549 window_box (w, -1, &x, &y, &width, &height);
5550 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5551 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5552
5553 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5554 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5555 bottom_y = y + height;
5556
5557 if (to_y < from_y)
5558 {
5559 /* Scrolling up. Make sure we don't copy part of the mode
5560 line at the bottom. */
5561 if (from_y + run->height > bottom_y)
5562 height = bottom_y - from_y;
5563 else
5564 height = run->height;
5565 }
5566 else
5567 {
5568 /* Scolling down. Make sure we don't copy over the mode line.
5569 at the bottom. */
5570 if (to_y + run->height > bottom_y)
5571 height = bottom_y - to_y;
5572 else
5573 height = run->height;
5574 }
5575
5576 BLOCK_INPUT;
5577
5578 /* Cursor off. Will be switched on again in x_update_window_end. */
5579 updated_window = w;
5580 x_clear_cursor (w);
5581
5582 XCopyArea (FRAME_X_DISPLAY (f),
5583 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5584 f->output_data.x->normal_gc,
5585 x, from_y,
5586 width, height,
5587 x, to_y);
5588
5589 UNBLOCK_INPUT;
5590 }
5591
5592
5593 \f
5594 /***********************************************************************
5595 Exposure Events
5596 ***********************************************************************/
5597
5598 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5599 corner of the exposed rectangle. W and H are width and height of
5600 the exposed area. All are pixel values. W or H zero means redraw
5601 the entire frame. */
5602
5603 static void
5604 expose_frame (f, x, y, w, h)
5605 struct frame *f;
5606 int x, y, w, h;
5607 {
5608 XRectangle r;
5609
5610 TRACE ((stderr, "expose_frame "));
5611
5612 /* No need to redraw if frame will be redrawn soon. */
5613 if (FRAME_GARBAGED_P (f))
5614 {
5615 TRACE ((stderr, " garbaged\n"));
5616 return;
5617 }
5618
5619 /* If basic faces haven't been realized yet, there is no point in
5620 trying to redraw anything. This can happen when we get an expose
5621 event while Emacs is starting, e.g. by moving another window. */
5622 if (FRAME_FACE_CACHE (f) == NULL
5623 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5624 {
5625 TRACE ((stderr, " no faces\n"));
5626 return;
5627 }
5628
5629 if (w == 0 || h == 0)
5630 {
5631 r.x = r.y = 0;
5632 r.width = CANON_X_UNIT (f) * f->width;
5633 r.height = CANON_Y_UNIT (f) * f->height;
5634 }
5635 else
5636 {
5637 r.x = x;
5638 r.y = y;
5639 r.width = w;
5640 r.height = h;
5641 }
5642
5643 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5644 expose_window_tree (XWINDOW (f->root_window), &r);
5645
5646 if (WINDOWP (f->tool_bar_window))
5647 {
5648 struct window *w = XWINDOW (f->tool_bar_window);
5649 XRectangle window_rect;
5650 XRectangle intersection_rect;
5651 int window_x, window_y, window_width, window_height;
5652
5653
5654 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5655 window_rect.x = window_x;
5656 window_rect.y = window_y;
5657 window_rect.width = window_width;
5658 window_rect.height = window_height;
5659
5660 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5661 expose_window (w, &intersection_rect);
5662 }
5663
5664 #ifndef USE_X_TOOLKIT
5665 if (WINDOWP (f->menu_bar_window))
5666 {
5667 struct window *w = XWINDOW (f->menu_bar_window);
5668 XRectangle window_rect;
5669 XRectangle intersection_rect;
5670 int window_x, window_y, window_width, window_height;
5671
5672
5673 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5674 window_rect.x = window_x;
5675 window_rect.y = window_y;
5676 window_rect.width = window_width;
5677 window_rect.height = window_height;
5678
5679 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5680 expose_window (w, &intersection_rect);
5681 }
5682 #endif /* not USE_X_TOOLKIT */
5683 }
5684
5685
5686 /* Redraw (parts) of all windows in the window tree rooted at W that
5687 intersect R. R contains frame pixel coordinates. */
5688
5689 static void
5690 expose_window_tree (w, r)
5691 struct window *w;
5692 XRectangle *r;
5693 {
5694 while (w)
5695 {
5696 if (!NILP (w->hchild))
5697 expose_window_tree (XWINDOW (w->hchild), r);
5698 else if (!NILP (w->vchild))
5699 expose_window_tree (XWINDOW (w->vchild), r);
5700 else
5701 {
5702 XRectangle window_rect;
5703 XRectangle intersection_rect;
5704 struct frame *f = XFRAME (w->frame);
5705 int window_x, window_y, window_width, window_height;
5706
5707 /* Frame-relative pixel rectangle of W. */
5708 window_box (w, -1, &window_x, &window_y, &window_width,
5709 &window_height);
5710 window_rect.x
5711 = (window_x
5712 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5713 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5714 window_rect.y = window_y;
5715 window_rect.width
5716 = (window_width
5717 + FRAME_X_FLAGS_AREA_WIDTH (f)
5718 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5719 window_rect.height
5720 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5721
5722 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5723 expose_window (w, &intersection_rect);
5724 }
5725
5726 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5727 }
5728 }
5729
5730
5731 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5732 which intersects rectangle R. R is in window-relative coordinates. */
5733
5734 static void
5735 expose_area (w, row, r, area)
5736 struct window *w;
5737 struct glyph_row *row;
5738 XRectangle *r;
5739 enum glyph_row_area area;
5740 {
5741 struct glyph *first = row->glyphs[area];
5742 struct glyph *end = row->glyphs[area] + row->used[area];
5743 struct glyph *last;
5744 int first_x, start_x, x;
5745
5746 if (area == TEXT_AREA && row->fill_line_p)
5747 /* If row extends face to end of line write the whole line. */
5748 x_draw_glyphs (w, 0, row, area,
5749 0, row->used[area],
5750 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5751 NULL, NULL, 0);
5752 else
5753 {
5754 /* Set START_X to the window-relative start position for drawing glyphs of
5755 AREA. The first glyph of the text area can be partially visible.
5756 The first glyphs of other areas cannot. */
5757 if (area == LEFT_MARGIN_AREA)
5758 start_x = 0;
5759 else if (area == TEXT_AREA)
5760 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5761 else
5762 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5763 + window_box_width (w, TEXT_AREA));
5764 x = start_x;
5765
5766 /* Find the first glyph that must be redrawn. */
5767 while (first < end
5768 && x + first->pixel_width < r->x)
5769 {
5770 x += first->pixel_width;
5771 ++first;
5772 }
5773
5774 /* Find the last one. */
5775 last = first;
5776 first_x = x;
5777 while (last < end
5778 && x < r->x + r->width)
5779 {
5780 x += last->pixel_width;
5781 ++last;
5782 }
5783
5784 /* Repaint. */
5785 if (last > first)
5786 x_draw_glyphs (w, first_x - start_x, row, area,
5787 first - row->glyphs[area],
5788 last - row->glyphs[area],
5789 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5790 NULL, NULL, 0);
5791 }
5792 }
5793
5794
5795 /* Redraw the parts of the glyph row ROW on window W intersecting
5796 rectangle R. R is in window-relative coordinates. */
5797
5798 static void
5799 expose_line (w, row, r)
5800 struct window *w;
5801 struct glyph_row *row;
5802 XRectangle *r;
5803 {
5804 xassert (row->enabled_p);
5805
5806 if (row->mode_line_p || w->pseudo_window_p)
5807 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5808 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5809 NULL, NULL, 0);
5810 else
5811 {
5812 if (row->used[LEFT_MARGIN_AREA])
5813 expose_area (w, row, r, LEFT_MARGIN_AREA);
5814 if (row->used[TEXT_AREA])
5815 expose_area (w, row, r, TEXT_AREA);
5816 if (row->used[RIGHT_MARGIN_AREA])
5817 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5818 x_draw_row_bitmaps (w, row);
5819 }
5820 }
5821
5822
5823 /* Return non-zero if W's cursor intersects rectangle R. */
5824
5825 static int
5826 x_phys_cursor_in_rect_p (w, r)
5827 struct window *w;
5828 XRectangle *r;
5829 {
5830 XRectangle cr, result;
5831 struct glyph *cursor_glyph;
5832
5833 cursor_glyph = get_phys_cursor_glyph (w);
5834 if (cursor_glyph)
5835 {
5836 cr.x = w->phys_cursor.x;
5837 cr.y = w->phys_cursor.y;
5838 cr.width = cursor_glyph->pixel_width;
5839 cr.height = w->phys_cursor_height;
5840 return x_intersect_rectangles (&cr, r, &result);
5841 }
5842 else
5843 return 0;
5844 }
5845
5846
5847 /* Redraw a rectangle of window W. R is a rectangle in window
5848 relative coordinates. Call this function with input blocked. */
5849
5850 static void
5851 expose_window (w, r)
5852 struct window *w;
5853 XRectangle *r;
5854 {
5855 struct glyph_row *row;
5856 int y;
5857 int yb = window_text_bottom_y (w);
5858 int cursor_cleared_p;
5859
5860 /* If window is not yet fully initialized, do nothing. This can
5861 happen when toolkit scroll bars are used and a window is split.
5862 Reconfiguring the scroll bar will generate an expose for a newly
5863 created window. */
5864 if (w->current_matrix == NULL || w == updated_window)
5865 return;
5866
5867 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5868 r->x, r->y, r->width, r->height));
5869
5870 /* Convert to window coordinates. */
5871 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5872 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5873
5874 /* Turn off the cursor. */
5875 if (!w->pseudo_window_p
5876 && x_phys_cursor_in_rect_p (w, r))
5877 {
5878 x_clear_cursor (w);
5879 cursor_cleared_p = 1;
5880 }
5881 else
5882 cursor_cleared_p = 0;
5883
5884 /* Find the first row intersecting the rectangle R. */
5885 row = w->current_matrix->rows;
5886 y = 0;
5887 while (row->enabled_p
5888 && y < yb
5889 && y + row->height < r->y)
5890 {
5891 y += row->height;
5892 ++row;
5893 }
5894
5895 /* Display the text in the rectangle, one text line at a time. */
5896 while (row->enabled_p
5897 && y < yb
5898 && y < r->y + r->height)
5899 {
5900 expose_line (w, row, r);
5901 y += row->height;
5902 ++row;
5903 }
5904
5905 /* Display the mode line if there is one. */
5906 if (WINDOW_WANTS_MODELINE_P (w)
5907 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5908 row->enabled_p)
5909 && row->y < r->y + r->height)
5910 expose_line (w, row, r);
5911
5912 if (!w->pseudo_window_p)
5913 {
5914 /* Draw border between windows. */
5915 x_draw_vertical_border (w);
5916
5917 /* Turn the cursor on again. */
5918 if (cursor_cleared_p)
5919 x_update_window_cursor (w, 1);
5920 }
5921 }
5922
5923
5924 /* Determine the intersection of two rectangles R1 and R2. Return
5925 the intersection in *RESULT. Value is non-zero if RESULT is not
5926 empty. */
5927
5928 static int
5929 x_intersect_rectangles (r1, r2, result)
5930 XRectangle *r1, *r2, *result;
5931 {
5932 XRectangle *left, *right;
5933 XRectangle *upper, *lower;
5934 int intersection_p = 0;
5935
5936 /* Rearrange so that R1 is the left-most rectangle. */
5937 if (r1->x < r2->x)
5938 left = r1, right = r2;
5939 else
5940 left = r2, right = r1;
5941
5942 /* X0 of the intersection is right.x0, if this is inside R1,
5943 otherwise there is no intersection. */
5944 if (right->x <= left->x + left->width)
5945 {
5946 result->x = right->x;
5947
5948 /* The right end of the intersection is the minimum of the
5949 the right ends of left and right. */
5950 result->width = (min (left->x + left->width, right->x + right->width)
5951 - result->x);
5952
5953 /* Same game for Y. */
5954 if (r1->y < r2->y)
5955 upper = r1, lower = r2;
5956 else
5957 upper = r2, lower = r1;
5958
5959 /* The upper end of the intersection is lower.y0, if this is inside
5960 of upper. Otherwise, there is no intersection. */
5961 if (lower->y <= upper->y + upper->height)
5962 {
5963 result->y = lower->y;
5964
5965 /* The lower end of the intersection is the minimum of the lower
5966 ends of upper and lower. */
5967 result->height = (min (lower->y + lower->height,
5968 upper->y + upper->height)
5969 - result->y);
5970 intersection_p = 1;
5971 }
5972 }
5973
5974 return intersection_p;
5975 }
5976
5977
5978
5979
5980 \f
5981 static void
5982 frame_highlight (f)
5983 struct frame *f;
5984 {
5985 /* We used to only do this if Vx_no_window_manager was non-nil, but
5986 the ICCCM (section 4.1.6) says that the window's border pixmap
5987 and border pixel are window attributes which are "private to the
5988 client", so we can always change it to whatever we want. */
5989 BLOCK_INPUT;
5990 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5991 f->output_data.x->border_pixel);
5992 UNBLOCK_INPUT;
5993 x_update_cursor (f, 1);
5994 }
5995
5996 static void
5997 frame_unhighlight (f)
5998 struct frame *f;
5999 {
6000 /* We used to only do this if Vx_no_window_manager was non-nil, but
6001 the ICCCM (section 4.1.6) says that the window's border pixmap
6002 and border pixel are window attributes which are "private to the
6003 client", so we can always change it to whatever we want. */
6004 BLOCK_INPUT;
6005 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6006 f->output_data.x->border_tile);
6007 UNBLOCK_INPUT;
6008 x_update_cursor (f, 1);
6009 }
6010
6011 /* The focus has changed. Update the frames as necessary to reflect
6012 the new situation. Note that we can't change the selected frame
6013 here, because the Lisp code we are interrupting might become confused.
6014 Each event gets marked with the frame in which it occurred, so the
6015 Lisp code can tell when the switch took place by examining the events. */
6016
6017 static void
6018 x_new_focus_frame (dpyinfo, frame)
6019 struct x_display_info *dpyinfo;
6020 struct frame *frame;
6021 {
6022 struct frame *old_focus = dpyinfo->x_focus_frame;
6023
6024 if (frame != dpyinfo->x_focus_frame)
6025 {
6026 /* Set this before calling other routines, so that they see
6027 the correct value of x_focus_frame. */
6028 dpyinfo->x_focus_frame = frame;
6029
6030 if (old_focus && old_focus->auto_lower)
6031 x_lower_frame (old_focus);
6032
6033 #if 0
6034 selected_frame = frame;
6035 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6036 selected_frame);
6037 Fselect_window (selected_frame->selected_window);
6038 choose_minibuf_frame ();
6039 #endif /* ! 0 */
6040
6041 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6042 pending_autoraise_frame = dpyinfo->x_focus_frame;
6043 else
6044 pending_autoraise_frame = 0;
6045 }
6046
6047 x_frame_rehighlight (dpyinfo);
6048 }
6049
6050 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6051
6052 void
6053 x_mouse_leave (dpyinfo)
6054 struct x_display_info *dpyinfo;
6055 {
6056 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6057 }
6058
6059 /* The focus has changed, or we have redirected a frame's focus to
6060 another frame (this happens when a frame uses a surrogate
6061 mini-buffer frame). Shift the highlight as appropriate.
6062
6063 The FRAME argument doesn't necessarily have anything to do with which
6064 frame is being highlighted or un-highlighted; we only use it to find
6065 the appropriate X display info. */
6066
6067 static void
6068 XTframe_rehighlight (frame)
6069 struct frame *frame;
6070 {
6071 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6072 }
6073
6074 static void
6075 x_frame_rehighlight (dpyinfo)
6076 struct x_display_info *dpyinfo;
6077 {
6078 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6079
6080 if (dpyinfo->x_focus_frame)
6081 {
6082 dpyinfo->x_highlight_frame
6083 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6084 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6085 : dpyinfo->x_focus_frame);
6086 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6087 {
6088 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6089 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6090 }
6091 }
6092 else
6093 dpyinfo->x_highlight_frame = 0;
6094
6095 if (dpyinfo->x_highlight_frame != old_highlight)
6096 {
6097 if (old_highlight)
6098 frame_unhighlight (old_highlight);
6099 if (dpyinfo->x_highlight_frame)
6100 frame_highlight (dpyinfo->x_highlight_frame);
6101 }
6102 }
6103
6104
6105 \f
6106 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6107
6108 /* Initialize mode_switch_bit and modifier_meaning. */
6109 static void
6110 x_find_modifier_meanings (dpyinfo)
6111 struct x_display_info *dpyinfo;
6112 {
6113 int min_code, max_code;
6114 KeySym *syms;
6115 int syms_per_code;
6116 XModifierKeymap *mods;
6117
6118 dpyinfo->meta_mod_mask = 0;
6119 dpyinfo->shift_lock_mask = 0;
6120 dpyinfo->alt_mod_mask = 0;
6121 dpyinfo->super_mod_mask = 0;
6122 dpyinfo->hyper_mod_mask = 0;
6123
6124 #ifdef HAVE_X11R4
6125 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6126 #else
6127 min_code = dpyinfo->display->min_keycode;
6128 max_code = dpyinfo->display->max_keycode;
6129 #endif
6130
6131 syms = XGetKeyboardMapping (dpyinfo->display,
6132 min_code, max_code - min_code + 1,
6133 &syms_per_code);
6134 mods = XGetModifierMapping (dpyinfo->display);
6135
6136 /* Scan the modifier table to see which modifier bits the Meta and
6137 Alt keysyms are on. */
6138 {
6139 int row, col; /* The row and column in the modifier table. */
6140
6141 for (row = 3; row < 8; row++)
6142 for (col = 0; col < mods->max_keypermod; col++)
6143 {
6144 KeyCode code
6145 = mods->modifiermap[(row * mods->max_keypermod) + col];
6146
6147 /* Zeroes are used for filler. Skip them. */
6148 if (code == 0)
6149 continue;
6150
6151 /* Are any of this keycode's keysyms a meta key? */
6152 {
6153 int code_col;
6154
6155 for (code_col = 0; code_col < syms_per_code; code_col++)
6156 {
6157 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6158
6159 switch (sym)
6160 {
6161 case XK_Meta_L:
6162 case XK_Meta_R:
6163 dpyinfo->meta_mod_mask |= (1 << row);
6164 break;
6165
6166 case XK_Alt_L:
6167 case XK_Alt_R:
6168 dpyinfo->alt_mod_mask |= (1 << row);
6169 break;
6170
6171 case XK_Hyper_L:
6172 case XK_Hyper_R:
6173 dpyinfo->hyper_mod_mask |= (1 << row);
6174 break;
6175
6176 case XK_Super_L:
6177 case XK_Super_R:
6178 dpyinfo->super_mod_mask |= (1 << row);
6179 break;
6180
6181 case XK_Shift_Lock:
6182 /* Ignore this if it's not on the lock modifier. */
6183 if ((1 << row) == LockMask)
6184 dpyinfo->shift_lock_mask = LockMask;
6185 break;
6186 }
6187 }
6188 }
6189 }
6190 }
6191
6192 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6193 if (! dpyinfo->meta_mod_mask)
6194 {
6195 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6196 dpyinfo->alt_mod_mask = 0;
6197 }
6198
6199 /* If some keys are both alt and meta,
6200 make them just meta, not alt. */
6201 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6202 {
6203 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6204 }
6205
6206 XFree ((char *) syms);
6207 XFreeModifiermap (mods);
6208 }
6209
6210 /* Convert between the modifier bits X uses and the modifier bits
6211 Emacs uses. */
6212
6213 static unsigned int
6214 x_x_to_emacs_modifiers (dpyinfo, state)
6215 struct x_display_info *dpyinfo;
6216 unsigned int state;
6217 {
6218 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6219 | ((state & ControlMask) ? ctrl_modifier : 0)
6220 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6221 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6222 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6223 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6224 }
6225
6226 static unsigned int
6227 x_emacs_to_x_modifiers (dpyinfo, state)
6228 struct x_display_info *dpyinfo;
6229 unsigned int state;
6230 {
6231 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6232 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6233 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6234 | ((state & shift_modifier) ? ShiftMask : 0)
6235 | ((state & ctrl_modifier) ? ControlMask : 0)
6236 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6237 }
6238
6239 /* Convert a keysym to its name. */
6240
6241 char *
6242 x_get_keysym_name (keysym)
6243 KeySym keysym;
6244 {
6245 char *value;
6246
6247 BLOCK_INPUT;
6248 value = XKeysymToString (keysym);
6249 UNBLOCK_INPUT;
6250
6251 return value;
6252 }
6253
6254
6255 \f
6256 /* Mouse clicks and mouse movement. Rah. */
6257
6258 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6259 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6260 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6261 not force the value into range. */
6262
6263 void
6264 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6265 FRAME_PTR f;
6266 register int pix_x, pix_y;
6267 register int *x, *y;
6268 XRectangle *bounds;
6269 int noclip;
6270 {
6271 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6272 even for negative values. */
6273 if (pix_x < 0)
6274 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6275 if (pix_y < 0)
6276 pix_y -= (f)->output_data.x->line_height - 1;
6277
6278 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6279 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6280
6281 if (bounds)
6282 {
6283 bounds->width = FONT_WIDTH (f->output_data.x->font);
6284 bounds->height = f->output_data.x->line_height;
6285 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6286 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6287 }
6288
6289 if (!noclip)
6290 {
6291 if (pix_x < 0)
6292 pix_x = 0;
6293 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6294 pix_x = FRAME_WINDOW_WIDTH (f);
6295
6296 if (pix_y < 0)
6297 pix_y = 0;
6298 else if (pix_y > f->height)
6299 pix_y = f->height;
6300 }
6301
6302 *x = pix_x;
6303 *y = pix_y;
6304 }
6305
6306
6307 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6308 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6309 can't tell the positions because W's display is not up to date,
6310 return 0. */
6311
6312 int
6313 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6314 struct window *w;
6315 int hpos, vpos;
6316 int *frame_x, *frame_y;
6317 {
6318 int success_p;
6319
6320 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6321 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6322
6323 if (display_completed)
6324 {
6325 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6326 struct glyph *glyph = row->glyphs[TEXT_AREA];
6327 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6328
6329 *frame_y = row->y;
6330 *frame_x = row->x;
6331 while (glyph < end)
6332 {
6333 *frame_x += glyph->pixel_width;
6334 ++glyph;
6335 }
6336
6337 success_p = 1;
6338 }
6339 else
6340 {
6341 *frame_y = *frame_x = 0;
6342 success_p = 0;
6343 }
6344
6345 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6346 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6347 return success_p;
6348 }
6349
6350
6351 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6352
6353 If the event is a button press, then note that we have grabbed
6354 the mouse. */
6355
6356 static Lisp_Object
6357 construct_mouse_click (result, event, f)
6358 struct input_event *result;
6359 XButtonEvent *event;
6360 struct frame *f;
6361 {
6362 /* Make the event type no_event; we'll change that when we decide
6363 otherwise. */
6364 result->kind = mouse_click;
6365 result->code = event->button - Button1;
6366 result->timestamp = event->time;
6367 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6368 event->state)
6369 | (event->type == ButtonRelease
6370 ? up_modifier
6371 : down_modifier));
6372
6373 XSETINT (result->x, event->x);
6374 XSETINT (result->y, event->y);
6375 XSETFRAME (result->frame_or_window, f);
6376 result->arg = Qnil;
6377 return Qnil;
6378 }
6379
6380 \f
6381 /* Function to report a mouse movement to the mainstream Emacs code.
6382 The input handler calls this.
6383
6384 We have received a mouse movement event, which is given in *event.
6385 If the mouse is over a different glyph than it was last time, tell
6386 the mainstream emacs code by setting mouse_moved. If not, ask for
6387 another motion event, so we can check again the next time it moves. */
6388
6389 static XMotionEvent last_mouse_motion_event;
6390 static Lisp_Object last_mouse_motion_frame;
6391
6392 static void
6393 note_mouse_movement (frame, event)
6394 FRAME_PTR frame;
6395 XMotionEvent *event;
6396 {
6397 last_mouse_movement_time = event->time;
6398 last_mouse_motion_event = *event;
6399 XSETFRAME (last_mouse_motion_frame, frame);
6400
6401 if (event->window != FRAME_X_WINDOW (frame))
6402 {
6403 frame->mouse_moved = 1;
6404 last_mouse_scroll_bar = Qnil;
6405 note_mouse_highlight (frame, -1, -1);
6406 }
6407
6408 /* Has the mouse moved off the glyph it was on at the last sighting? */
6409 else if (event->x < last_mouse_glyph.x
6410 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6411 || event->y < last_mouse_glyph.y
6412 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6413 {
6414 frame->mouse_moved = 1;
6415 last_mouse_scroll_bar = Qnil;
6416 note_mouse_highlight (frame, event->x, event->y);
6417 }
6418 }
6419
6420 /* This is used for debugging, to turn off note_mouse_highlight. */
6421
6422 int disable_mouse_highlight;
6423
6424
6425 \f
6426 /************************************************************************
6427 Mouse Face
6428 ************************************************************************/
6429
6430 /* Find the glyph under window-relative coordinates X/Y in window W.
6431 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6432 strings. Return in *HPOS and *VPOS the row and column number of
6433 the glyph found. Return in *AREA the glyph area containing X.
6434 Value is a pointer to the glyph found or null if X/Y is not on
6435 text, or we can't tell because W's current matrix is not up to
6436 date. */
6437
6438 static struct glyph *
6439 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6440 struct window *w;
6441 int x, y;
6442 int *hpos, *vpos, *area;
6443 {
6444 struct glyph *glyph, *end;
6445 struct glyph_row *row = NULL;
6446 int x0, i, left_area_width;
6447
6448 /* Find row containing Y. Give up if some row is not enabled. */
6449 for (i = 0; i < w->current_matrix->nrows; ++i)
6450 {
6451 row = MATRIX_ROW (w->current_matrix, i);
6452 if (!row->enabled_p)
6453 return NULL;
6454 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6455 break;
6456 }
6457
6458 *vpos = i;
6459 *hpos = 0;
6460
6461 /* Give up if Y is not in the window. */
6462 if (i == w->current_matrix->nrows)
6463 return NULL;
6464
6465 /* Get the glyph area containing X. */
6466 if (w->pseudo_window_p)
6467 {
6468 *area = TEXT_AREA;
6469 x0 = 0;
6470 }
6471 else
6472 {
6473 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6474 if (x < left_area_width)
6475 {
6476 *area = LEFT_MARGIN_AREA;
6477 x0 = 0;
6478 }
6479 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6480 {
6481 *area = TEXT_AREA;
6482 x0 = row->x + left_area_width;
6483 }
6484 else
6485 {
6486 *area = RIGHT_MARGIN_AREA;
6487 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6488 }
6489 }
6490
6491 /* Find glyph containing X. */
6492 glyph = row->glyphs[*area];
6493 end = glyph + row->used[*area];
6494 while (glyph < end)
6495 {
6496 if (x < x0 + glyph->pixel_width)
6497 {
6498 if (w->pseudo_window_p)
6499 break;
6500 else if (BUFFERP (glyph->object))
6501 break;
6502 }
6503
6504 x0 += glyph->pixel_width;
6505 ++glyph;
6506 }
6507
6508 if (glyph == end)
6509 return NULL;
6510
6511 *hpos = glyph - row->glyphs[*area];
6512 return glyph;
6513 }
6514
6515
6516 /* Convert frame-relative x/y to coordinates relative to window W.
6517 Takes pseudo-windows into account. */
6518
6519 static void
6520 frame_to_window_pixel_xy (w, x, y)
6521 struct window *w;
6522 int *x, *y;
6523 {
6524 if (w->pseudo_window_p)
6525 {
6526 /* A pseudo-window is always full-width, and starts at the
6527 left edge of the frame, plus a frame border. */
6528 struct frame *f = XFRAME (w->frame);
6529 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6530 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6531 }
6532 else
6533 {
6534 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6535 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6536 }
6537 }
6538
6539
6540 /* Take proper action when mouse has moved to the mode or header line of
6541 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6542 mode line. X is relative to the start of the text display area of
6543 W, so the width of bitmap areas and scroll bars must be subtracted
6544 to get a position relative to the start of the mode line. */
6545
6546 static void
6547 note_mode_line_highlight (w, x, mode_line_p)
6548 struct window *w;
6549 int x, mode_line_p;
6550 {
6551 struct frame *f = XFRAME (w->frame);
6552 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6553 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6554 struct glyph_row *row;
6555
6556 if (mode_line_p)
6557 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6558 else
6559 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6560
6561 if (row->enabled_p)
6562 {
6563 struct glyph *glyph, *end;
6564 Lisp_Object help, map;
6565 int x0;
6566
6567 /* Find the glyph under X. */
6568 glyph = row->glyphs[TEXT_AREA];
6569 end = glyph + row->used[TEXT_AREA];
6570 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6571 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6572
6573 while (glyph < end
6574 && x >= x0 + glyph->pixel_width)
6575 {
6576 x0 += glyph->pixel_width;
6577 ++glyph;
6578 }
6579
6580 if (glyph < end
6581 && STRINGP (glyph->object)
6582 && XSTRING (glyph->object)->intervals
6583 && glyph->charpos >= 0
6584 && glyph->charpos < XSTRING (glyph->object)->size)
6585 {
6586 /* If we're on a string with `help-echo' text property,
6587 arrange for the help to be displayed. This is done by
6588 setting the global variable help_echo to the help string. */
6589 help = Fget_text_property (make_number (glyph->charpos),
6590 Qhelp_echo, glyph->object);
6591 if (!NILP (help))
6592 {
6593 help_echo = help;
6594 XSETWINDOW (help_echo_window, w);
6595 help_echo_object = glyph->object;
6596 help_echo_pos = glyph->charpos;
6597 }
6598
6599 /* Change the mouse pointer according to what is under X/Y. */
6600 map = Fget_text_property (make_number (glyph->charpos),
6601 Qlocal_map, glyph->object);
6602 if (KEYMAPP (map))
6603 cursor = f->output_data.x->nontext_cursor;
6604 else
6605 {
6606 map = Fget_text_property (make_number (glyph->charpos),
6607 Qkeymap, glyph->object);
6608 if (KEYMAPP (map))
6609 cursor = f->output_data.x->nontext_cursor;
6610 }
6611 }
6612 }
6613
6614 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6615 }
6616
6617
6618 /* Take proper action when the mouse has moved to position X, Y on
6619 frame F as regards highlighting characters that have mouse-face
6620 properties. Also de-highlighting chars where the mouse was before.
6621 X and Y can be negative or out of range. */
6622
6623 static void
6624 note_mouse_highlight (f, x, y)
6625 struct frame *f;
6626 int x, y;
6627 {
6628 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6629 int portion;
6630 Lisp_Object window;
6631 struct window *w;
6632
6633 /* When a menu is active, don't highlight because this looks odd. */
6634 #ifdef USE_X_TOOLKIT
6635 if (popup_activated ())
6636 return;
6637 #endif
6638
6639 if (disable_mouse_highlight
6640 || !f->glyphs_initialized_p)
6641 return;
6642
6643 dpyinfo->mouse_face_mouse_x = x;
6644 dpyinfo->mouse_face_mouse_y = y;
6645 dpyinfo->mouse_face_mouse_frame = f;
6646
6647 if (dpyinfo->mouse_face_defer)
6648 return;
6649
6650 if (gc_in_progress)
6651 {
6652 dpyinfo->mouse_face_deferred_gc = 1;
6653 return;
6654 }
6655
6656 /* Which window is that in? */
6657 window = window_from_coordinates (f, x, y, &portion, 1);
6658
6659 /* If we were displaying active text in another window, clear that. */
6660 if (! EQ (window, dpyinfo->mouse_face_window))
6661 clear_mouse_face (dpyinfo);
6662
6663 /* Not on a window -> return. */
6664 if (!WINDOWP (window))
6665 return;
6666
6667 /* Convert to window-relative pixel coordinates. */
6668 w = XWINDOW (window);
6669 frame_to_window_pixel_xy (w, &x, &y);
6670
6671 /* Handle tool-bar window differently since it doesn't display a
6672 buffer. */
6673 if (EQ (window, f->tool_bar_window))
6674 {
6675 note_tool_bar_highlight (f, x, y);
6676 return;
6677 }
6678
6679 if (portion == 1 || portion == 3)
6680 {
6681 /* Mouse is on the mode or top line. */
6682 note_mode_line_highlight (w, x, portion == 1);
6683 return;
6684 }
6685 else if (portion == 2)
6686 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6687 f->output_data.x->horizontal_drag_cursor);
6688 else
6689 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6690 f->output_data.x->text_cursor);
6691
6692 /* Are we in a window whose display is up to date?
6693 And verify the buffer's text has not changed. */
6694 if (/* Within text portion of the window. */
6695 portion == 0
6696 && EQ (w->window_end_valid, w->buffer)
6697 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6698 && (XFASTINT (w->last_overlay_modified)
6699 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6700 {
6701 int hpos, vpos, pos, i, area;
6702 struct glyph *glyph;
6703
6704 /* Find the glyph under X/Y. */
6705 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6706
6707 /* Clear mouse face if X/Y not over text. */
6708 if (glyph == NULL
6709 || area != TEXT_AREA
6710 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6711 {
6712 clear_mouse_face (dpyinfo);
6713 return;
6714 }
6715
6716 pos = glyph->charpos;
6717 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6718
6719 /* Check for mouse-face and help-echo. */
6720 {
6721 Lisp_Object mouse_face, overlay, position;
6722 Lisp_Object *overlay_vec;
6723 int len, noverlays;
6724 struct buffer *obuf;
6725 int obegv, ozv;
6726
6727 /* If we get an out-of-range value, return now; avoid an error. */
6728 if (pos > BUF_Z (XBUFFER (w->buffer)))
6729 return;
6730
6731 /* Make the window's buffer temporarily current for
6732 overlays_at and compute_char_face. */
6733 obuf = current_buffer;
6734 current_buffer = XBUFFER (w->buffer);
6735 obegv = BEGV;
6736 ozv = ZV;
6737 BEGV = BEG;
6738 ZV = Z;
6739
6740 /* Is this char mouse-active or does it have help-echo? */
6741 XSETINT (position, pos);
6742
6743 /* Put all the overlays we want in a vector in overlay_vec.
6744 Store the length in len. If there are more than 10, make
6745 enough space for all, and try again. */
6746 len = 10;
6747 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6748 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6749 if (noverlays > len)
6750 {
6751 len = noverlays;
6752 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6753 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6754 }
6755
6756 /* Sort overlays into increasing priority order. */
6757 noverlays = sort_overlays (overlay_vec, noverlays, w);
6758
6759 /* Check mouse-face highlighting. */
6760 if (! (EQ (window, dpyinfo->mouse_face_window)
6761 && vpos >= dpyinfo->mouse_face_beg_row
6762 && vpos <= dpyinfo->mouse_face_end_row
6763 && (vpos > dpyinfo->mouse_face_beg_row
6764 || hpos >= dpyinfo->mouse_face_beg_col)
6765 && (vpos < dpyinfo->mouse_face_end_row
6766 || hpos < dpyinfo->mouse_face_end_col
6767 || dpyinfo->mouse_face_past_end)))
6768 {
6769 /* Clear the display of the old active region, if any. */
6770 clear_mouse_face (dpyinfo);
6771
6772 /* Find the highest priority overlay that has a mouse-face prop. */
6773 overlay = Qnil;
6774 for (i = noverlays - 1; i >= 0; --i)
6775 {
6776 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6777 if (!NILP (mouse_face))
6778 {
6779 overlay = overlay_vec[i];
6780 break;
6781 }
6782 }
6783
6784 /* If no overlay applies, get a text property. */
6785 if (NILP (overlay))
6786 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6787
6788 /* Handle the overlay case. */
6789 if (! NILP (overlay))
6790 {
6791 /* Find the range of text around this char that
6792 should be active. */
6793 Lisp_Object before, after;
6794 int ignore;
6795
6796 before = Foverlay_start (overlay);
6797 after = Foverlay_end (overlay);
6798 /* Record this as the current active region. */
6799 fast_find_position (w, XFASTINT (before),
6800 &dpyinfo->mouse_face_beg_col,
6801 &dpyinfo->mouse_face_beg_row,
6802 &dpyinfo->mouse_face_beg_x,
6803 &dpyinfo->mouse_face_beg_y);
6804 dpyinfo->mouse_face_past_end
6805 = !fast_find_position (w, XFASTINT (after),
6806 &dpyinfo->mouse_face_end_col,
6807 &dpyinfo->mouse_face_end_row,
6808 &dpyinfo->mouse_face_end_x,
6809 &dpyinfo->mouse_face_end_y);
6810 dpyinfo->mouse_face_window = window;
6811 dpyinfo->mouse_face_face_id
6812 = face_at_buffer_position (w, pos, 0, 0,
6813 &ignore, pos + 1, 1);
6814
6815 /* Display it as active. */
6816 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6817 }
6818 /* Handle the text property case. */
6819 else if (! NILP (mouse_face))
6820 {
6821 /* Find the range of text around this char that
6822 should be active. */
6823 Lisp_Object before, after, beginning, end;
6824 int ignore;
6825
6826 beginning = Fmarker_position (w->start);
6827 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6828 - XFASTINT (w->window_end_pos)));
6829 before
6830 = Fprevious_single_property_change (make_number (pos + 1),
6831 Qmouse_face,
6832 w->buffer, beginning);
6833 after
6834 = Fnext_single_property_change (position, Qmouse_face,
6835 w->buffer, end);
6836 /* Record this as the current active region. */
6837 fast_find_position (w, XFASTINT (before),
6838 &dpyinfo->mouse_face_beg_col,
6839 &dpyinfo->mouse_face_beg_row,
6840 &dpyinfo->mouse_face_beg_x,
6841 &dpyinfo->mouse_face_beg_y);
6842 dpyinfo->mouse_face_past_end
6843 = !fast_find_position (w, XFASTINT (after),
6844 &dpyinfo->mouse_face_end_col,
6845 &dpyinfo->mouse_face_end_row,
6846 &dpyinfo->mouse_face_end_x,
6847 &dpyinfo->mouse_face_end_y);
6848 dpyinfo->mouse_face_window = window;
6849 dpyinfo->mouse_face_face_id
6850 = face_at_buffer_position (w, pos, 0, 0,
6851 &ignore, pos + 1, 1);
6852
6853 /* Display it as active. */
6854 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6855 }
6856 }
6857
6858 /* Look for a `help-echo' property. */
6859 {
6860 Lisp_Object help, overlay;
6861
6862 /* Check overlays first. */
6863 help = overlay = Qnil;
6864 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6865 {
6866 overlay = overlay_vec[i];
6867 help = Foverlay_get (overlay, Qhelp_echo);
6868 }
6869
6870 if (!NILP (help))
6871 {
6872 help_echo = help;
6873 help_echo_window = window;
6874 help_echo_object = overlay;
6875 help_echo_pos = pos;
6876 }
6877 else
6878 {
6879 /* Try text properties. */
6880 if ((STRINGP (glyph->object)
6881 && glyph->charpos >= 0
6882 && glyph->charpos < XSTRING (glyph->object)->size)
6883 || (BUFFERP (glyph->object)
6884 && glyph->charpos >= BEGV
6885 && glyph->charpos < ZV))
6886 help = Fget_text_property (make_number (glyph->charpos),
6887 Qhelp_echo, glyph->object);
6888
6889 if (!NILP (help))
6890 {
6891 help_echo = help;
6892 help_echo_window = window;
6893 help_echo_object = glyph->object;
6894 help_echo_pos = glyph->charpos;
6895 }
6896 }
6897 }
6898
6899 BEGV = obegv;
6900 ZV = ozv;
6901 current_buffer = obuf;
6902 }
6903 }
6904 }
6905
6906 static void
6907 redo_mouse_highlight ()
6908 {
6909 if (!NILP (last_mouse_motion_frame)
6910 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6911 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6912 last_mouse_motion_event.x,
6913 last_mouse_motion_event.y);
6914 }
6915
6916
6917 \f
6918 /***********************************************************************
6919 Tool-bars
6920 ***********************************************************************/
6921
6922 static int x_tool_bar_item P_ ((struct frame *, int, int,
6923 struct glyph **, int *, int *, int *));
6924
6925 /* Tool-bar item index of the item on which a mouse button was pressed
6926 or -1. */
6927
6928 static int last_tool_bar_item;
6929
6930
6931 /* Get information about the tool-bar item at position X/Y on frame F.
6932 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6933 the current matrix of the tool-bar window of F, or NULL if not
6934 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6935 item in F->tool_bar_items. Value is
6936
6937 -1 if X/Y is not on a tool-bar item
6938 0 if X/Y is on the same item that was highlighted before.
6939 1 otherwise. */
6940
6941 static int
6942 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6943 struct frame *f;
6944 int x, y;
6945 struct glyph **glyph;
6946 int *hpos, *vpos, *prop_idx;
6947 {
6948 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6949 struct window *w = XWINDOW (f->tool_bar_window);
6950 int area;
6951
6952 /* Find the glyph under X/Y. */
6953 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6954 if (*glyph == NULL)
6955 return -1;
6956
6957 /* Get the start of this tool-bar item's properties in
6958 f->tool_bar_items. */
6959 if (!tool_bar_item_info (f, *glyph, prop_idx))
6960 return -1;
6961
6962 /* Is mouse on the highlighted item? */
6963 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6964 && *vpos >= dpyinfo->mouse_face_beg_row
6965 && *vpos <= dpyinfo->mouse_face_end_row
6966 && (*vpos > dpyinfo->mouse_face_beg_row
6967 || *hpos >= dpyinfo->mouse_face_beg_col)
6968 && (*vpos < dpyinfo->mouse_face_end_row
6969 || *hpos < dpyinfo->mouse_face_end_col
6970 || dpyinfo->mouse_face_past_end))
6971 return 0;
6972
6973 return 1;
6974 }
6975
6976
6977 /* Handle mouse button event on the tool-bar of frame F, at
6978 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6979 or ButtonRelase. */
6980
6981 static void
6982 x_handle_tool_bar_click (f, button_event)
6983 struct frame *f;
6984 XButtonEvent *button_event;
6985 {
6986 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6987 struct window *w = XWINDOW (f->tool_bar_window);
6988 int hpos, vpos, prop_idx;
6989 struct glyph *glyph;
6990 Lisp_Object enabled_p;
6991 int x = button_event->x;
6992 int y = button_event->y;
6993
6994 /* If not on the highlighted tool-bar item, return. */
6995 frame_to_window_pixel_xy (w, &x, &y);
6996 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6997 return;
6998
6999 /* If item is disabled, do nothing. */
7000 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7001 if (NILP (enabled_p))
7002 return;
7003
7004 if (button_event->type == ButtonPress)
7005 {
7006 /* Show item in pressed state. */
7007 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7008 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7009 last_tool_bar_item = prop_idx;
7010 }
7011 else
7012 {
7013 Lisp_Object key, frame;
7014 struct input_event event;
7015
7016 /* Show item in released state. */
7017 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7018 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7019
7020 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7021
7022 XSETFRAME (frame, f);
7023 event.kind = TOOL_BAR_EVENT;
7024 event.frame_or_window = frame;
7025 event.arg = frame;
7026 kbd_buffer_store_event (&event);
7027
7028 event.kind = TOOL_BAR_EVENT;
7029 event.frame_or_window = frame;
7030 event.arg = key;
7031 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7032 button_event->state);
7033 kbd_buffer_store_event (&event);
7034 last_tool_bar_item = -1;
7035 }
7036 }
7037
7038
7039 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7040 tool-bar window-relative coordinates X/Y. Called from
7041 note_mouse_highlight. */
7042
7043 static void
7044 note_tool_bar_highlight (f, x, y)
7045 struct frame *f;
7046 int x, y;
7047 {
7048 Lisp_Object window = f->tool_bar_window;
7049 struct window *w = XWINDOW (window);
7050 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7051 int hpos, vpos;
7052 struct glyph *glyph;
7053 struct glyph_row *row;
7054 int i;
7055 Lisp_Object enabled_p;
7056 int prop_idx;
7057 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7058 int mouse_down_p, rc;
7059
7060 /* Function note_mouse_highlight is called with negative x(y
7061 values when mouse moves outside of the frame. */
7062 if (x <= 0 || y <= 0)
7063 {
7064 clear_mouse_face (dpyinfo);
7065 return;
7066 }
7067
7068 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7069 if (rc < 0)
7070 {
7071 /* Not on tool-bar item. */
7072 clear_mouse_face (dpyinfo);
7073 return;
7074 }
7075 else if (rc == 0)
7076 /* On same tool-bar item as before. */
7077 goto set_help_echo;
7078
7079 clear_mouse_face (dpyinfo);
7080
7081 /* Mouse is down, but on different tool-bar item? */
7082 mouse_down_p = (dpyinfo->grabbed
7083 && f == last_mouse_frame
7084 && FRAME_LIVE_P (f));
7085 if (mouse_down_p
7086 && last_tool_bar_item != prop_idx)
7087 return;
7088
7089 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7090 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7091
7092 /* If tool-bar item is not enabled, don't highlight it. */
7093 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7094 if (!NILP (enabled_p))
7095 {
7096 /* Compute the x-position of the glyph. In front and past the
7097 image is a space. We include this is the highlighted area. */
7098 row = MATRIX_ROW (w->current_matrix, vpos);
7099 for (i = x = 0; i < hpos; ++i)
7100 x += row->glyphs[TEXT_AREA][i].pixel_width;
7101
7102 /* Record this as the current active region. */
7103 dpyinfo->mouse_face_beg_col = hpos;
7104 dpyinfo->mouse_face_beg_row = vpos;
7105 dpyinfo->mouse_face_beg_x = x;
7106 dpyinfo->mouse_face_beg_y = row->y;
7107 dpyinfo->mouse_face_past_end = 0;
7108
7109 dpyinfo->mouse_face_end_col = hpos + 1;
7110 dpyinfo->mouse_face_end_row = vpos;
7111 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7112 dpyinfo->mouse_face_end_y = row->y;
7113 dpyinfo->mouse_face_window = window;
7114 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7115
7116 /* Display it as active. */
7117 show_mouse_face (dpyinfo, draw);
7118 dpyinfo->mouse_face_image_state = draw;
7119 }
7120
7121 set_help_echo:
7122
7123 /* Set help_echo to a help string.to display for this tool-bar item.
7124 XTread_socket does the rest. */
7125 help_echo_object = help_echo_window = Qnil;
7126 help_echo_pos = -1;
7127 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7128 if (NILP (help_echo))
7129 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7130 }
7131
7132
7133 \f
7134 /* Find the glyph matrix position of buffer position POS in window W.
7135 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7136 current glyphs must be up to date. If POS is above window start
7137 return (0, 0, 0, 0). If POS is after end of W, return end of
7138 last line in W. */
7139
7140 static int
7141 fast_find_position (w, pos, hpos, vpos, x, y)
7142 struct window *w;
7143 int pos;
7144 int *hpos, *vpos, *x, *y;
7145 {
7146 int i;
7147 int lastcol;
7148 int maybe_next_line_p = 0;
7149 int line_start_position;
7150 int yb = window_text_bottom_y (w);
7151 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7152 struct glyph_row *best_row = row;
7153 int row_vpos = 0, best_row_vpos = 0;
7154 int current_x;
7155
7156 while (row->y < yb)
7157 {
7158 if (row->used[TEXT_AREA])
7159 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7160 else
7161 line_start_position = 0;
7162
7163 if (line_start_position > pos)
7164 break;
7165 /* If the position sought is the end of the buffer,
7166 don't include the blank lines at the bottom of the window. */
7167 else if (line_start_position == pos
7168 && pos == BUF_ZV (XBUFFER (w->buffer)))
7169 {
7170 maybe_next_line_p = 1;
7171 break;
7172 }
7173 else if (line_start_position > 0)
7174 {
7175 best_row = row;
7176 best_row_vpos = row_vpos;
7177 }
7178
7179 if (row->y + row->height >= yb)
7180 break;
7181
7182 ++row;
7183 ++row_vpos;
7184 }
7185
7186 /* Find the right column within BEST_ROW. */
7187 lastcol = 0;
7188 current_x = best_row->x;
7189 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7190 {
7191 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7192 int charpos;
7193
7194 charpos = glyph->charpos;
7195 if (charpos == pos)
7196 {
7197 *hpos = i;
7198 *vpos = best_row_vpos;
7199 *x = current_x;
7200 *y = best_row->y;
7201 return 1;
7202 }
7203 else if (charpos > pos)
7204 break;
7205 else if (charpos > 0)
7206 lastcol = i;
7207
7208 current_x += glyph->pixel_width;
7209 }
7210
7211 /* If we're looking for the end of the buffer,
7212 and we didn't find it in the line we scanned,
7213 use the start of the following line. */
7214 if (maybe_next_line_p)
7215 {
7216 ++best_row;
7217 ++best_row_vpos;
7218 lastcol = 0;
7219 current_x = best_row->x;
7220 }
7221
7222 *vpos = best_row_vpos;
7223 *hpos = lastcol + 1;
7224 *x = current_x;
7225 *y = best_row->y;
7226 return 0;
7227 }
7228
7229
7230 /* Display the active region described by mouse_face_*
7231 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7232
7233 static void
7234 show_mouse_face (dpyinfo, draw)
7235 struct x_display_info *dpyinfo;
7236 enum draw_glyphs_face draw;
7237 {
7238 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7239 struct frame *f = XFRAME (WINDOW_FRAME (w));
7240 int i;
7241 int cursor_off_p = 0;
7242 struct cursor_pos saved_cursor;
7243
7244 saved_cursor = output_cursor;
7245
7246 /* If window is in the process of being destroyed, don't bother
7247 to do anything. */
7248 if (w->current_matrix == NULL)
7249 goto set_x_cursor;
7250
7251 /* Recognize when we are called to operate on rows that don't exist
7252 anymore. This can happen when a window is split. */
7253 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7254 goto set_x_cursor;
7255
7256 set_output_cursor (&w->phys_cursor);
7257
7258 /* Note that mouse_face_beg_row etc. are window relative. */
7259 for (i = dpyinfo->mouse_face_beg_row;
7260 i <= dpyinfo->mouse_face_end_row;
7261 i++)
7262 {
7263 int start_hpos, end_hpos, start_x;
7264 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7265
7266 /* Don't do anything if row doesn't have valid contents. */
7267 if (!row->enabled_p)
7268 continue;
7269
7270 /* For all but the first row, the highlight starts at column 0. */
7271 if (i == dpyinfo->mouse_face_beg_row)
7272 {
7273 start_hpos = dpyinfo->mouse_face_beg_col;
7274 start_x = dpyinfo->mouse_face_beg_x;
7275 }
7276 else
7277 {
7278 start_hpos = 0;
7279 start_x = 0;
7280 }
7281
7282 if (i == dpyinfo->mouse_face_end_row)
7283 end_hpos = dpyinfo->mouse_face_end_col;
7284 else
7285 end_hpos = row->used[TEXT_AREA];
7286
7287 /* If the cursor's in the text we are about to rewrite, turn the
7288 cursor off. */
7289 if (!w->pseudo_window_p
7290 && i == output_cursor.vpos
7291 && output_cursor.hpos >= start_hpos - 1
7292 && output_cursor.hpos <= end_hpos)
7293 {
7294 x_update_window_cursor (w, 0);
7295 cursor_off_p = 1;
7296 }
7297
7298 if (end_hpos > start_hpos)
7299 {
7300 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7301 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7302 start_hpos, end_hpos, draw, NULL, NULL, 0);
7303 }
7304 }
7305
7306 /* If we turned the cursor off, turn it back on. */
7307 if (cursor_off_p)
7308 x_display_cursor (w, 1,
7309 output_cursor.hpos, output_cursor.vpos,
7310 output_cursor.x, output_cursor.y);
7311
7312 output_cursor = saved_cursor;
7313
7314 set_x_cursor:
7315
7316 /* Change the mouse cursor. */
7317 if (draw == DRAW_NORMAL_TEXT)
7318 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7319 f->output_data.x->text_cursor);
7320 else if (draw == DRAW_MOUSE_FACE)
7321 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7322 f->output_data.x->cross_cursor);
7323 else
7324 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7325 f->output_data.x->nontext_cursor);
7326 }
7327
7328 /* Clear out the mouse-highlighted active region.
7329 Redraw it un-highlighted first. */
7330
7331 void
7332 clear_mouse_face (dpyinfo)
7333 struct x_display_info *dpyinfo;
7334 {
7335 #if 0 /* This prevents redrawing tool bar items when changing from one
7336 to another while a tooltip is open, so don't do it. */
7337 if (!NILP (tip_frame))
7338 return;
7339 #endif
7340
7341 if (! NILP (dpyinfo->mouse_face_window))
7342 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7343
7344 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7345 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7346 dpyinfo->mouse_face_window = Qnil;
7347 }
7348
7349
7350 /* Clear any mouse-face on window W. This function is part of the
7351 redisplay interface, and is called from try_window_id and similar
7352 functions to ensure the mouse-highlight is off. */
7353
7354 static void
7355 x_clear_mouse_face (w)
7356 struct window *w;
7357 {
7358 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7359 Lisp_Object window;
7360
7361 BLOCK_INPUT;
7362 XSETWINDOW (window, w);
7363 if (EQ (window, dpyinfo->mouse_face_window))
7364 clear_mouse_face (dpyinfo);
7365 UNBLOCK_INPUT;
7366 }
7367
7368
7369 /* Just discard the mouse face information for frame F, if any.
7370 This is used when the size of F is changed. */
7371
7372 void
7373 cancel_mouse_face (f)
7374 FRAME_PTR f;
7375 {
7376 Lisp_Object window;
7377 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7378
7379 window = dpyinfo->mouse_face_window;
7380 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7381 {
7382 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7383 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7384 dpyinfo->mouse_face_window = Qnil;
7385 }
7386 }
7387 \f
7388 static struct scroll_bar *x_window_to_scroll_bar ();
7389 static void x_scroll_bar_report_motion ();
7390
7391 /* Return the current position of the mouse.
7392 *fp should be a frame which indicates which display to ask about.
7393
7394 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7395 and *part to the frame, window, and scroll bar part that the mouse
7396 is over. Set *x and *y to the portion and whole of the mouse's
7397 position on the scroll bar.
7398
7399 If the mouse movement started elsewhere, set *fp to the frame the
7400 mouse is on, *bar_window to nil, and *x and *y to the character cell
7401 the mouse is over.
7402
7403 Set *time to the server time-stamp for the time at which the mouse
7404 was at this position.
7405
7406 Don't store anything if we don't have a valid set of values to report.
7407
7408 This clears the mouse_moved flag, so we can wait for the next mouse
7409 movement. */
7410
7411 static void
7412 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7413 FRAME_PTR *fp;
7414 int insist;
7415 Lisp_Object *bar_window;
7416 enum scroll_bar_part *part;
7417 Lisp_Object *x, *y;
7418 unsigned long *time;
7419 {
7420 FRAME_PTR f1;
7421
7422 BLOCK_INPUT;
7423
7424 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7425 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7426 else
7427 {
7428 Window root;
7429 int root_x, root_y;
7430
7431 Window dummy_window;
7432 int dummy;
7433
7434 Lisp_Object frame, tail;
7435
7436 /* Clear the mouse-moved flag for every frame on this display. */
7437 FOR_EACH_FRAME (tail, frame)
7438 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7439 XFRAME (frame)->mouse_moved = 0;
7440
7441 last_mouse_scroll_bar = Qnil;
7442
7443 /* Figure out which root window we're on. */
7444 XQueryPointer (FRAME_X_DISPLAY (*fp),
7445 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7446
7447 /* The root window which contains the pointer. */
7448 &root,
7449
7450 /* Trash which we can't trust if the pointer is on
7451 a different screen. */
7452 &dummy_window,
7453
7454 /* The position on that root window. */
7455 &root_x, &root_y,
7456
7457 /* More trash we can't trust. */
7458 &dummy, &dummy,
7459
7460 /* Modifier keys and pointer buttons, about which
7461 we don't care. */
7462 (unsigned int *) &dummy);
7463
7464 /* Now we have a position on the root; find the innermost window
7465 containing the pointer. */
7466 {
7467 Window win, child;
7468 int win_x, win_y;
7469 int parent_x = 0, parent_y = 0;
7470 int count;
7471
7472 win = root;
7473
7474 /* XTranslateCoordinates can get errors if the window
7475 structure is changing at the same time this function
7476 is running. So at least we must not crash from them. */
7477
7478 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7479
7480 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7481 && FRAME_LIVE_P (last_mouse_frame))
7482 {
7483 /* If mouse was grabbed on a frame, give coords for that frame
7484 even if the mouse is now outside it. */
7485 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7486
7487 /* From-window, to-window. */
7488 root, FRAME_X_WINDOW (last_mouse_frame),
7489
7490 /* From-position, to-position. */
7491 root_x, root_y, &win_x, &win_y,
7492
7493 /* Child of win. */
7494 &child);
7495 f1 = last_mouse_frame;
7496 }
7497 else
7498 {
7499 while (1)
7500 {
7501 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7502
7503 /* From-window, to-window. */
7504 root, win,
7505
7506 /* From-position, to-position. */
7507 root_x, root_y, &win_x, &win_y,
7508
7509 /* Child of win. */
7510 &child);
7511
7512 if (child == None || child == win)
7513 break;
7514
7515 win = child;
7516 parent_x = win_x;
7517 parent_y = win_y;
7518 }
7519
7520 /* Now we know that:
7521 win is the innermost window containing the pointer
7522 (XTC says it has no child containing the pointer),
7523 win_x and win_y are the pointer's position in it
7524 (XTC did this the last time through), and
7525 parent_x and parent_y are the pointer's position in win's parent.
7526 (They are what win_x and win_y were when win was child.
7527 If win is the root window, it has no parent, and
7528 parent_{x,y} are invalid, but that's okay, because we'll
7529 never use them in that case.) */
7530
7531 /* Is win one of our frames? */
7532 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7533
7534 #ifdef USE_X_TOOLKIT
7535 /* If we end up with the menu bar window, say it's not
7536 on the frame. */
7537 if (f1 != NULL
7538 && f1->output_data.x->menubar_widget
7539 && win == XtWindow (f1->output_data.x->menubar_widget))
7540 f1 = NULL;
7541 #endif /* USE_X_TOOLKIT */
7542 }
7543
7544 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7545 f1 = 0;
7546
7547 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7548
7549 /* If not, is it one of our scroll bars? */
7550 if (! f1)
7551 {
7552 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7553
7554 if (bar)
7555 {
7556 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7557 win_x = parent_x;
7558 win_y = parent_y;
7559 }
7560 }
7561
7562 if (f1 == 0 && insist > 0)
7563 f1 = SELECTED_FRAME ();
7564
7565 if (f1)
7566 {
7567 /* Ok, we found a frame. Store all the values.
7568 last_mouse_glyph is a rectangle used to reduce the
7569 generation of mouse events. To not miss any motion
7570 events, we must divide the frame into rectangles of the
7571 size of the smallest character that could be displayed
7572 on it, i.e. into the same rectangles that matrices on
7573 the frame are divided into. */
7574
7575 #if OLD_REDISPLAY_CODE
7576 int ignore1, ignore2;
7577 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7578 &last_mouse_glyph,
7579 FRAME_X_DISPLAY_INFO (f1)->grabbed
7580 || insist);
7581 #else
7582 {
7583 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7584 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7585 int x = win_x;
7586 int y = win_y;
7587
7588 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7589 round down even for negative values. */
7590 if (x < 0)
7591 x -= width - 1;
7592 if (y < 0)
7593 y -= height - 1;
7594
7595 last_mouse_glyph.width = width;
7596 last_mouse_glyph.height = height;
7597 last_mouse_glyph.x = (x + width - 1) / width * width;
7598 last_mouse_glyph.y = (y + height - 1) / height * height;
7599 }
7600 #endif
7601
7602 *bar_window = Qnil;
7603 *part = 0;
7604 *fp = f1;
7605 XSETINT (*x, win_x);
7606 XSETINT (*y, win_y);
7607 *time = last_mouse_movement_time;
7608 }
7609 }
7610 }
7611
7612 UNBLOCK_INPUT;
7613 }
7614
7615
7616 #ifdef USE_X_TOOLKIT
7617
7618 /* Atimer callback function for TIMER. Called every 0.1s to process
7619 Xt timeouts, if needed. We must avoid calling XtAppPending as
7620 much as possible because that function does an implicit XFlush
7621 that slows us down. */
7622
7623 static void
7624 x_process_timeouts (timer)
7625 struct atimer *timer;
7626 {
7627 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7628 {
7629 BLOCK_INPUT;
7630 while (XtAppPending (Xt_app_con) & XtIMTimer)
7631 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7632 UNBLOCK_INPUT;
7633 }
7634 }
7635
7636 #endif /* USE_X_TOOLKIT */
7637
7638 \f
7639 /* Scroll bar support. */
7640
7641 /* Given an X window ID, find the struct scroll_bar which manages it.
7642 This can be called in GC, so we have to make sure to strip off mark
7643 bits. */
7644
7645 static struct scroll_bar *
7646 x_window_to_scroll_bar (window_id)
7647 Window window_id;
7648 {
7649 Lisp_Object tail;
7650
7651 for (tail = Vframe_list;
7652 XGCTYPE (tail) == Lisp_Cons;
7653 tail = XCDR (tail))
7654 {
7655 Lisp_Object frame, bar, condemned;
7656
7657 frame = XCAR (tail);
7658 /* All elements of Vframe_list should be frames. */
7659 if (! GC_FRAMEP (frame))
7660 abort ();
7661
7662 /* Scan this frame's scroll bar list for a scroll bar with the
7663 right window ID. */
7664 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7665 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7666 /* This trick allows us to search both the ordinary and
7667 condemned scroll bar lists with one loop. */
7668 ! GC_NILP (bar) || (bar = condemned,
7669 condemned = Qnil,
7670 ! GC_NILP (bar));
7671 bar = XSCROLL_BAR (bar)->next)
7672 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7673 return XSCROLL_BAR (bar);
7674 }
7675
7676 return 0;
7677 }
7678
7679
7680 \f
7681 /************************************************************************
7682 Toolkit scroll bars
7683 ************************************************************************/
7684
7685 #ifdef USE_TOOLKIT_SCROLL_BARS
7686
7687 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7688 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7689 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7690 struct scroll_bar *));
7691 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7692 int, int, int));
7693
7694
7695 /* Id of action hook installed for scroll bars. */
7696
7697 static XtActionHookId action_hook_id;
7698
7699 /* Lisp window being scrolled. Set when starting to interact with
7700 a toolkit scroll bar, reset to nil when ending the interaction. */
7701
7702 static Lisp_Object window_being_scrolled;
7703
7704 /* Last scroll bar part sent in xm_scroll_callback. */
7705
7706 static int last_scroll_bar_part;
7707
7708 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7709 that movements of 1/20 of the screen size are mapped to up/down. */
7710
7711 static Boolean xaw3d_arrow_scroll;
7712
7713 /* Whether the drag scrolling maintains the mouse at the top of the
7714 thumb. If not, resizing the thumb needs to be done more carefully
7715 to avoid jerkyness. */
7716
7717 static Boolean xaw3d_pick_top;
7718
7719
7720 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7721 bars are used.. The hook is responsible for detecting when
7722 the user ends an interaction with the scroll bar, and generates
7723 a `end-scroll' scroll_bar_click' event if so. */
7724
7725 static void
7726 xt_action_hook (widget, client_data, action_name, event, params,
7727 num_params)
7728 Widget widget;
7729 XtPointer client_data;
7730 String action_name;
7731 XEvent *event;
7732 String *params;
7733 Cardinal *num_params;
7734 {
7735 int scroll_bar_p;
7736 char *end_action;
7737
7738 #ifdef USE_MOTIF
7739 scroll_bar_p = XmIsScrollBar (widget);
7740 end_action = "Release";
7741 #else /* !USE_MOTIF i.e. use Xaw */
7742 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7743 end_action = "EndScroll";
7744 #endif /* USE_MOTIF */
7745
7746 if (scroll_bar_p
7747 && strcmp (action_name, end_action) == 0
7748 && WINDOWP (window_being_scrolled))
7749 {
7750 struct window *w;
7751
7752 x_send_scroll_bar_event (window_being_scrolled,
7753 scroll_bar_end_scroll, 0, 0);
7754 w = XWINDOW (window_being_scrolled);
7755 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7756 window_being_scrolled = Qnil;
7757 last_scroll_bar_part = -1;
7758
7759 /* Xt timeouts no longer needed. */
7760 toolkit_scroll_bar_interaction = 0;
7761 }
7762 }
7763
7764 /* A vector of windows used for communication between
7765 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7766
7767 static struct window **scroll_bar_windows;
7768 static int scroll_bar_windows_size;
7769
7770
7771 /* Send a client message with message type Xatom_Scrollbar for a
7772 scroll action to the frame of WINDOW. PART is a value identifying
7773 the part of the scroll bar that was clicked on. PORTION is the
7774 amount to scroll of a whole of WHOLE. */
7775
7776 static void
7777 x_send_scroll_bar_event (window, part, portion, whole)
7778 Lisp_Object window;
7779 int part, portion, whole;
7780 {
7781 XEvent event;
7782 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7783 struct window *w = XWINDOW (window);
7784 struct frame *f = XFRAME (w->frame);
7785 int i;
7786
7787 BLOCK_INPUT;
7788
7789 /* Construct a ClientMessage event to send to the frame. */
7790 ev->type = ClientMessage;
7791 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7792 ev->display = FRAME_X_DISPLAY (f);
7793 ev->window = FRAME_X_WINDOW (f);
7794 ev->format = 32;
7795
7796 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7797 not enough to store a pointer or Lisp_Object on a 64 bit system.
7798 So, store the window in scroll_bar_windows and pass the index
7799 into that array in the event. */
7800 for (i = 0; i < scroll_bar_windows_size; ++i)
7801 if (scroll_bar_windows[i] == NULL)
7802 break;
7803
7804 if (i == scroll_bar_windows_size)
7805 {
7806 int new_size = max (10, 2 * scroll_bar_windows_size);
7807 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7808 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7809
7810 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7811 nbytes);
7812 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7813 scroll_bar_windows_size = new_size;
7814 }
7815
7816 scroll_bar_windows[i] = w;
7817 ev->data.l[0] = (long) i;
7818 ev->data.l[1] = (long) part;
7819 ev->data.l[2] = (long) 0;
7820 ev->data.l[3] = (long) portion;
7821 ev->data.l[4] = (long) whole;
7822
7823 /* Make Xt timeouts work while the scroll bar is active. */
7824 toolkit_scroll_bar_interaction = 1;
7825
7826 /* Setting the event mask to zero means that the message will
7827 be sent to the client that created the window, and if that
7828 window no longer exists, no event will be sent. */
7829 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7830 UNBLOCK_INPUT;
7831 }
7832
7833
7834 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7835 in *IEVENT. */
7836
7837 static void
7838 x_scroll_bar_to_input_event (event, ievent)
7839 XEvent *event;
7840 struct input_event *ievent;
7841 {
7842 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7843 Lisp_Object window;
7844 struct frame *f;
7845 struct window *w;
7846
7847 w = scroll_bar_windows[ev->data.l[0]];
7848 scroll_bar_windows[ev->data.l[0]] = NULL;
7849
7850 XSETWINDOW (window, w);
7851 f = XFRAME (w->frame);
7852
7853 ievent->kind = scroll_bar_click;
7854 ievent->frame_or_window = window;
7855 ievent->arg = Qnil;
7856 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7857 ievent->part = ev->data.l[1];
7858 ievent->code = ev->data.l[2];
7859 ievent->x = make_number ((int) ev->data.l[3]);
7860 ievent->y = make_number ((int) ev->data.l[4]);
7861 ievent->modifiers = 0;
7862 }
7863
7864
7865 #ifdef USE_MOTIF
7866
7867 /* Minimum and maximum values used for Motif scroll bars. */
7868
7869 #define XM_SB_MIN 1
7870 #define XM_SB_MAX 10000000
7871 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7872
7873
7874 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7875 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7876 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7877
7878 static void
7879 xm_scroll_callback (widget, client_data, call_data)
7880 Widget widget;
7881 XtPointer client_data, call_data;
7882 {
7883 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7884 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7885 double percent;
7886 int part = -1, whole = 0, portion = 0;
7887
7888 switch (cs->reason)
7889 {
7890 case XmCR_DECREMENT:
7891 bar->dragging = Qnil;
7892 part = scroll_bar_up_arrow;
7893 break;
7894
7895 case XmCR_INCREMENT:
7896 bar->dragging = Qnil;
7897 part = scroll_bar_down_arrow;
7898 break;
7899
7900 case XmCR_PAGE_DECREMENT:
7901 bar->dragging = Qnil;
7902 part = scroll_bar_above_handle;
7903 break;
7904
7905 case XmCR_PAGE_INCREMENT:
7906 bar->dragging = Qnil;
7907 part = scroll_bar_below_handle;
7908 break;
7909
7910 case XmCR_TO_TOP:
7911 bar->dragging = Qnil;
7912 part = scroll_bar_to_top;
7913 break;
7914
7915 case XmCR_TO_BOTTOM:
7916 bar->dragging = Qnil;
7917 part = scroll_bar_to_bottom;
7918 break;
7919
7920 case XmCR_DRAG:
7921 {
7922 int slider_size;
7923 int dragging_down_p = (INTEGERP (bar->dragging)
7924 && XINT (bar->dragging) <= cs->value);
7925
7926 /* Get the slider size. */
7927 BLOCK_INPUT;
7928 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7929 UNBLOCK_INPUT;
7930
7931 /* At the max position of the scroll bar, do a line-wise
7932 movement. Without doing anything, the LessTif scroll bar
7933 calls us with the same cs->value again and again. If we
7934 want to make sure that we can reach the end of the buffer,
7935 we have to do something.
7936
7937 Implementation note: setting bar->dragging always to
7938 cs->value gives a smoother movement at the max position.
7939 Setting it to nil when doing line-wise movement gives
7940 a better slider behavior. */
7941
7942 if (cs->value + slider_size == XM_SB_MAX
7943 || (dragging_down_p
7944 && last_scroll_bar_part == scroll_bar_down_arrow))
7945 {
7946 part = scroll_bar_down_arrow;
7947 bar->dragging = Qnil;
7948 }
7949 else
7950 {
7951 whole = XM_SB_RANGE;
7952 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7953 part = scroll_bar_handle;
7954 bar->dragging = make_number (cs->value);
7955 }
7956 }
7957 break;
7958
7959 case XmCR_VALUE_CHANGED:
7960 break;
7961 };
7962
7963 if (part >= 0)
7964 {
7965 window_being_scrolled = bar->window;
7966 last_scroll_bar_part = part;
7967 x_send_scroll_bar_event (bar->window, part, portion, whole);
7968 }
7969 }
7970
7971
7972 #else /* !USE_MOTIF, i.e. Xaw. */
7973
7974
7975 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7976 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7977 scroll bar struct. CALL_DATA is a pointer to a float saying where
7978 the thumb is. */
7979
7980 static void
7981 xaw_jump_callback (widget, client_data, call_data)
7982 Widget widget;
7983 XtPointer client_data, call_data;
7984 {
7985 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7986 float top = *(float *) call_data;
7987 float shown;
7988 int whole, portion, height;
7989 int part;
7990
7991 /* Get the size of the thumb, a value between 0 and 1. */
7992 BLOCK_INPUT;
7993 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7994 UNBLOCK_INPUT;
7995
7996 whole = 10000000;
7997 portion = shown < 1 ? top * whole : 0;
7998
7999 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8000 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8001 the bottom, so we force the scrolling whenever we see that we're
8002 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8003 we try to ensure that we always stay two pixels away from the
8004 bottom). */
8005 part = scroll_bar_down_arrow;
8006 else
8007 part = scroll_bar_handle;
8008
8009 window_being_scrolled = bar->window;
8010 bar->dragging = make_number (portion);
8011 last_scroll_bar_part = part;
8012 x_send_scroll_bar_event (bar->window, part, portion, whole);
8013 }
8014
8015
8016 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8017 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8018 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8019 the scroll bar. CALL_DATA is an integer specifying the action that
8020 has taken place. It's magnitude is in the range 0..height of the
8021 scroll bar. Negative values mean scroll towards buffer start.
8022 Values < height of scroll bar mean line-wise movement. */
8023
8024 static void
8025 xaw_scroll_callback (widget, client_data, call_data)
8026 Widget widget;
8027 XtPointer client_data, call_data;
8028 {
8029 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8030 int position = (int) call_data;
8031 Dimension height;
8032 int part;
8033
8034 /* Get the height of the scroll bar. */
8035 BLOCK_INPUT;
8036 XtVaGetValues (widget, XtNheight, &height, NULL);
8037 UNBLOCK_INPUT;
8038
8039 if (abs (position) >= height)
8040 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8041
8042 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8043 it maps line-movement to call_data = max(5, height/20). */
8044 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8045 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8046 else
8047 part = scroll_bar_move_ratio;
8048
8049 window_being_scrolled = bar->window;
8050 bar->dragging = Qnil;
8051 last_scroll_bar_part = part;
8052 x_send_scroll_bar_event (bar->window, part, position, height);
8053 }
8054
8055
8056 #endif /* not USE_MOTIF */
8057
8058
8059 /* Create the widget for scroll bar BAR on frame F. Record the widget
8060 and X window of the scroll bar in BAR. */
8061
8062 static void
8063 x_create_toolkit_scroll_bar (f, bar)
8064 struct frame *f;
8065 struct scroll_bar *bar;
8066 {
8067 Window xwindow;
8068 Widget widget;
8069 Arg av[20];
8070 int ac = 0;
8071 char *scroll_bar_name = "verticalScrollBar";
8072 unsigned long pixel;
8073
8074 BLOCK_INPUT;
8075
8076 #ifdef USE_MOTIF
8077 /* LessTif 0.85, problems:
8078
8079 1. When the mouse if over the scroll bar, the scroll bar will
8080 get keyboard events. I didn't find a way to turn this off.
8081
8082 2. Do we have to explicitly set the cursor to get an arrow
8083 cursor (see below)? */
8084
8085 /* Set resources. Create the widget. */
8086 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8087 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8088 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8089 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8090 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8091 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8092 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8093
8094 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8095 if (pixel != -1)
8096 {
8097 XtSetArg (av[ac], XmNforeground, pixel);
8098 ++ac;
8099 }
8100
8101 pixel = f->output_data.x->scroll_bar_background_pixel;
8102 if (pixel != -1)
8103 {
8104 XtSetArg (av[ac], XmNbackground, pixel);
8105 ++ac;
8106 }
8107
8108 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8109 scroll_bar_name, av, ac);
8110
8111 /* Add one callback for everything that can happen. */
8112 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8113 (XtPointer) bar);
8114 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8115 (XtPointer) bar);
8116 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8117 (XtPointer) bar);
8118 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8119 (XtPointer) bar);
8120 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8121 (XtPointer) bar);
8122 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8123 (XtPointer) bar);
8124 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8125 (XtPointer) bar);
8126
8127 /* Realize the widget. Only after that is the X window created. */
8128 XtRealizeWidget (widget);
8129
8130 /* Set the cursor to an arrow. I didn't find a resource to do that.
8131 And I'm wondering why it hasn't an arrow cursor by default. */
8132 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8133 f->output_data.x->nontext_cursor);
8134
8135 #else /* !USE_MOTIF i.e. use Xaw */
8136
8137 /* Set resources. Create the widget. The background of the
8138 Xaw3d scroll bar widget is a little bit light for my taste.
8139 We don't alter it here to let users change it according
8140 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8141 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8142 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8143 /* For smoother scrolling with Xaw3d -sm */
8144 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8145 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8146
8147 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8148 if (pixel != -1)
8149 {
8150 XtSetArg (av[ac], XtNforeground, pixel);
8151 ++ac;
8152 }
8153
8154 pixel = f->output_data.x->scroll_bar_background_pixel;
8155 if (pixel != -1)
8156 {
8157 XtSetArg (av[ac], XtNbackground, pixel);
8158 ++ac;
8159 }
8160
8161 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8162 f->output_data.x->edit_widget, av, ac);
8163
8164 {
8165 char *initial = "";
8166 char *val = initial;
8167 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8168 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8169 if (val == initial)
8170 { /* ARROW_SCROLL */
8171 xaw3d_arrow_scroll = True;
8172 /* Isn't that just a personal preference ? -sm */
8173 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8174 }
8175 }
8176
8177 /* Define callbacks. */
8178 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8179 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8180 (XtPointer) bar);
8181
8182 /* Realize the widget. Only after that is the X window created. */
8183 XtRealizeWidget (widget);
8184
8185 #endif /* !USE_MOTIF */
8186
8187 /* Install an action hook that let's us detect when the user
8188 finishes interacting with a scroll bar. */
8189 if (action_hook_id == 0)
8190 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8191
8192 /* Remember X window and widget in the scroll bar vector. */
8193 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8194 xwindow = XtWindow (widget);
8195 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8196
8197 UNBLOCK_INPUT;
8198 }
8199
8200
8201 /* Set the thumb size and position of scroll bar BAR. We are currently
8202 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8203
8204 static void
8205 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8206 struct scroll_bar *bar;
8207 int portion, position, whole;
8208 {
8209 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8210 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8211 float top, shown;
8212
8213 if (whole == 0)
8214 top = 0, shown = 1;
8215 else
8216 {
8217 top = (float) position / whole;
8218 shown = (float) portion / whole;
8219 }
8220
8221 BLOCK_INPUT;
8222
8223 #ifdef USE_MOTIF
8224 {
8225 int size, value;
8226 Boolean arrow1_selected, arrow2_selected;
8227 unsigned char flags;
8228 XmScrollBarWidget sb;
8229
8230 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8231 is the scroll bar's maximum and MIN is the scroll bar's minimum
8232 value. */
8233 size = shown * XM_SB_RANGE;
8234 size = min (size, XM_SB_RANGE);
8235 size = max (size, 1);
8236
8237 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8238 value = top * XM_SB_RANGE;
8239 value = min (value, XM_SB_MAX - size);
8240 value = max (value, XM_SB_MIN);
8241
8242 /* LessTif: Calling XmScrollBarSetValues after an increment or
8243 decrement turns off auto-repeat LessTif-internally. This can
8244 be seen in ScrollBar.c which resets Arrow1Selected and
8245 Arrow2Selected. It also sets internal flags so that LessTif
8246 believes the mouse is in the slider. We either have to change
8247 our code, or work around that by accessing private data. */
8248
8249 sb = (XmScrollBarWidget) widget;
8250 arrow1_selected = sb->scrollBar.arrow1_selected;
8251 arrow2_selected = sb->scrollBar.arrow2_selected;
8252 flags = sb->scrollBar.flags;
8253
8254 if (NILP (bar->dragging))
8255 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8256 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8257 /* This has the negative side effect that the slider value is
8258 not what it would be if we scrolled here using line-wise or
8259 page-wise movement. */
8260 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8261 else
8262 {
8263 /* If currently dragging, only update the slider size.
8264 This reduces flicker effects. */
8265 int old_value, old_size, increment, page_increment;
8266
8267 XmScrollBarGetValues (widget, &old_value, &old_size,
8268 &increment, &page_increment);
8269 XmScrollBarSetValues (widget, old_value,
8270 min (size, XM_SB_RANGE - old_value),
8271 0, 0, False);
8272 }
8273
8274 sb->scrollBar.arrow1_selected = arrow1_selected;
8275 sb->scrollBar.arrow2_selected = arrow2_selected;
8276 sb->scrollBar.flags = flags;
8277 }
8278 #else /* !USE_MOTIF i.e. use Xaw */
8279 {
8280 float old_top, old_shown;
8281 Dimension height;
8282 XtVaGetValues (widget,
8283 XtNtopOfThumb, &old_top,
8284 XtNshown, &old_shown,
8285 XtNheight, &height,
8286 NULL);
8287
8288 /* Massage the top+shown values. */
8289 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8290 top = max (0, min (1, top));
8291 else
8292 top = old_top;
8293 /* Keep two pixels available for moving the thumb down. */
8294 shown = max (0, min (1 - top - (2.0 / height), shown));
8295
8296 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8297 check that your system's configuration file contains a define
8298 for `NARROWPROTO'. See s/freebsd.h for an example. */
8299 if (top != old_top || shown != old_shown)
8300 {
8301 if (NILP (bar->dragging))
8302 XawScrollbarSetThumb (widget, top, shown);
8303 else
8304 {
8305 #ifdef HAVE_XAW3D
8306 ScrollbarWidget sb = (ScrollbarWidget) widget;
8307 int scroll_mode = 0;
8308
8309 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8310 if (xaw3d_arrow_scroll)
8311 {
8312 /* Xaw3d stupidly ignores resize requests while dragging
8313 so we have to make it believe it's not in dragging mode. */
8314 scroll_mode = sb->scrollbar.scroll_mode;
8315 if (scroll_mode == 2)
8316 sb->scrollbar.scroll_mode = 0;
8317 }
8318 #endif
8319 /* Try to make the scrolling a tad smoother. */
8320 if (!xaw3d_pick_top)
8321 shown = min (shown, old_shown);
8322
8323 XawScrollbarSetThumb (widget, top, shown);
8324
8325 #ifdef HAVE_XAW3D
8326 if (xaw3d_arrow_scroll && scroll_mode == 2)
8327 sb->scrollbar.scroll_mode = scroll_mode;
8328 #endif
8329 }
8330 }
8331 }
8332 #endif /* !USE_MOTIF */
8333
8334 UNBLOCK_INPUT;
8335 }
8336
8337 #endif /* USE_TOOLKIT_SCROLL_BARS */
8338
8339
8340 \f
8341 /************************************************************************
8342 Scroll bars, general
8343 ************************************************************************/
8344
8345 /* Create a scroll bar and return the scroll bar vector for it. W is
8346 the Emacs window on which to create the scroll bar. TOP, LEFT,
8347 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8348 scroll bar. */
8349
8350 static struct scroll_bar *
8351 x_scroll_bar_create (w, top, left, width, height)
8352 struct window *w;
8353 int top, left, width, height;
8354 {
8355 struct frame *f = XFRAME (w->frame);
8356 struct scroll_bar *bar
8357 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8358
8359 BLOCK_INPUT;
8360
8361 #ifdef USE_TOOLKIT_SCROLL_BARS
8362 x_create_toolkit_scroll_bar (f, bar);
8363 #else /* not USE_TOOLKIT_SCROLL_BARS */
8364 {
8365 XSetWindowAttributes a;
8366 unsigned long mask;
8367 Window window;
8368
8369 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8370 if (a.background_pixel == -1)
8371 a.background_pixel = f->output_data.x->background_pixel;
8372
8373 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8374 | ButtonMotionMask | PointerMotionHintMask
8375 | ExposureMask);
8376 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8377
8378 mask = (CWBackPixel | CWEventMask | CWCursor);
8379
8380 /* Clear the area of W that will serve as a scroll bar. This is
8381 for the case that a window has been split horizontally. In
8382 this case, no clear_frame is generated to reduce flickering. */
8383 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8384 left, top, width,
8385 window_box_height (w), False);
8386
8387 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8388 /* Position and size of scroll bar. */
8389 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8390 top,
8391 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8392 height,
8393 /* Border width, depth, class, and visual. */
8394 0,
8395 CopyFromParent,
8396 CopyFromParent,
8397 CopyFromParent,
8398 /* Attributes. */
8399 mask, &a);
8400 SET_SCROLL_BAR_X_WINDOW (bar, window);
8401 }
8402 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8403
8404 XSETWINDOW (bar->window, w);
8405 XSETINT (bar->top, top);
8406 XSETINT (bar->left, left);
8407 XSETINT (bar->width, width);
8408 XSETINT (bar->height, height);
8409 XSETINT (bar->start, 0);
8410 XSETINT (bar->end, 0);
8411 bar->dragging = Qnil;
8412
8413 /* Add bar to its frame's list of scroll bars. */
8414 bar->next = FRAME_SCROLL_BARS (f);
8415 bar->prev = Qnil;
8416 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8417 if (!NILP (bar->next))
8418 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8419
8420 /* Map the window/widget. */
8421 #ifdef USE_TOOLKIT_SCROLL_BARS
8422 {
8423 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8424 XtConfigureWidget (scroll_bar,
8425 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8426 top,
8427 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8428 height, 0);
8429 XtMapWidget (scroll_bar);
8430 }
8431 #else /* not USE_TOOLKIT_SCROLL_BARS */
8432 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8433 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8434
8435 UNBLOCK_INPUT;
8436 return bar;
8437 }
8438
8439
8440 /* Draw BAR's handle in the proper position.
8441
8442 If the handle is already drawn from START to END, don't bother
8443 redrawing it, unless REBUILD is non-zero; in that case, always
8444 redraw it. (REBUILD is handy for drawing the handle after expose
8445 events.)
8446
8447 Normally, we want to constrain the start and end of the handle to
8448 fit inside its rectangle, but if the user is dragging the scroll
8449 bar handle, we want to let them drag it down all the way, so that
8450 the bar's top is as far down as it goes; otherwise, there's no way
8451 to move to the very end of the buffer. */
8452
8453 #ifndef USE_TOOLKIT_SCROLL_BARS
8454
8455 static void
8456 x_scroll_bar_set_handle (bar, start, end, rebuild)
8457 struct scroll_bar *bar;
8458 int start, end;
8459 int rebuild;
8460 {
8461 int dragging = ! NILP (bar->dragging);
8462 Window w = SCROLL_BAR_X_WINDOW (bar);
8463 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8464 GC gc = f->output_data.x->normal_gc;
8465
8466 /* If the display is already accurate, do nothing. */
8467 if (! rebuild
8468 && start == XINT (bar->start)
8469 && end == XINT (bar->end))
8470 return;
8471
8472 BLOCK_INPUT;
8473
8474 {
8475 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8476 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8477 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8478
8479 /* Make sure the values are reasonable, and try to preserve
8480 the distance between start and end. */
8481 {
8482 int length = end - start;
8483
8484 if (start < 0)
8485 start = 0;
8486 else if (start > top_range)
8487 start = top_range;
8488 end = start + length;
8489
8490 if (end < start)
8491 end = start;
8492 else if (end > top_range && ! dragging)
8493 end = top_range;
8494 }
8495
8496 /* Store the adjusted setting in the scroll bar. */
8497 XSETINT (bar->start, start);
8498 XSETINT (bar->end, end);
8499
8500 /* Clip the end position, just for display. */
8501 if (end > top_range)
8502 end = top_range;
8503
8504 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8505 below top positions, to make sure the handle is always at least
8506 that many pixels tall. */
8507 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8508
8509 /* Draw the empty space above the handle. Note that we can't clear
8510 zero-height areas; that means "clear to end of window." */
8511 if (0 < start)
8512 x_clear_area (FRAME_X_DISPLAY (f), w,
8513 /* x, y, width, height, and exposures. */
8514 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8515 VERTICAL_SCROLL_BAR_TOP_BORDER,
8516 inside_width, start,
8517 False);
8518
8519 /* Change to proper foreground color if one is specified. */
8520 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8521 XSetForeground (FRAME_X_DISPLAY (f), gc,
8522 f->output_data.x->scroll_bar_foreground_pixel);
8523
8524 /* Draw the handle itself. */
8525 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8526
8527 /* x, y, width, height */
8528 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8529 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8530 inside_width, end - start);
8531
8532 /* Restore the foreground color of the GC if we changed it above. */
8533 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8534 XSetForeground (FRAME_X_DISPLAY (f), gc,
8535 f->output_data.x->foreground_pixel);
8536
8537 /* Draw the empty space below the handle. Note that we can't
8538 clear zero-height areas; that means "clear to end of window." */
8539 if (end < inside_height)
8540 x_clear_area (FRAME_X_DISPLAY (f), w,
8541 /* x, y, width, height, and exposures. */
8542 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8543 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8544 inside_width, inside_height - end,
8545 False);
8546
8547 }
8548
8549 UNBLOCK_INPUT;
8550 }
8551
8552 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8553
8554 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8555 nil. */
8556
8557 static void
8558 x_scroll_bar_remove (bar)
8559 struct scroll_bar *bar;
8560 {
8561 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8562 BLOCK_INPUT;
8563
8564 #ifdef USE_TOOLKIT_SCROLL_BARS
8565 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8566 #else
8567 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8568 #endif
8569
8570 /* Disassociate this scroll bar from its window. */
8571 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8572
8573 UNBLOCK_INPUT;
8574 }
8575
8576
8577 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8578 that we are displaying PORTION characters out of a total of WHOLE
8579 characters, starting at POSITION. If WINDOW has no scroll bar,
8580 create one. */
8581
8582 static void
8583 XTset_vertical_scroll_bar (w, portion, whole, position)
8584 struct window *w;
8585 int portion, whole, position;
8586 {
8587 struct frame *f = XFRAME (w->frame);
8588 struct scroll_bar *bar;
8589 int top, height, left, sb_left, width, sb_width;
8590 int window_x, window_y, window_width, window_height;
8591
8592 /* Get window dimensions. */
8593 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8594 top = window_y;
8595 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8596 height = window_height;
8597
8598 /* Compute the left edge of the scroll bar area. */
8599 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8600 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8601 else
8602 left = XFASTINT (w->left);
8603 left *= CANON_X_UNIT (f);
8604 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8605
8606 /* Compute the width of the scroll bar which might be less than
8607 the width of the area reserved for the scroll bar. */
8608 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8609 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8610 else
8611 sb_width = width;
8612
8613 /* Compute the left edge of the scroll bar. */
8614 #ifdef USE_TOOLKIT_SCROLL_BARS
8615 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8616 sb_left = left + width - sb_width - (width - sb_width) / 2;
8617 else
8618 sb_left = left + (width - sb_width) / 2;
8619 #else
8620 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8621 sb_left = left + width - sb_width;
8622 else
8623 sb_left = left;
8624 #endif
8625
8626 /* Does the scroll bar exist yet? */
8627 if (NILP (w->vertical_scroll_bar))
8628 {
8629 BLOCK_INPUT;
8630 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8631 left, top, width, height, False);
8632 UNBLOCK_INPUT;
8633 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8634 }
8635 else
8636 {
8637 /* It may just need to be moved and resized. */
8638 unsigned int mask = 0;
8639
8640 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8641
8642 BLOCK_INPUT;
8643
8644 if (sb_left != XINT (bar->left))
8645 mask |= CWX;
8646 if (top != XINT (bar->top))
8647 mask |= CWY;
8648 if (sb_width != XINT (bar->width))
8649 mask |= CWWidth;
8650 if (height != XINT (bar->height))
8651 mask |= CWHeight;
8652
8653 #ifdef USE_TOOLKIT_SCROLL_BARS
8654
8655 /* Since toolkit scroll bars are smaller than the space reserved
8656 for them on the frame, we have to clear "under" them. */
8657 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8658 left, top, width, height, False);
8659
8660 /* Move/size the scroll bar widget. */
8661 if (mask)
8662 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
8663 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8664 top,
8665 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8666 height, 0);
8667
8668 #else /* not USE_TOOLKIT_SCROLL_BARS */
8669
8670 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8671 {
8672 /* Clear areas not covered by the scroll bar. This makes sure a
8673 previous mode line display is cleared after C-x 2 C-x 1, for
8674 example. Non-toolkit scroll bars are as wide as the area
8675 reserved for scroll bars - trim at both sides. */
8676 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8677 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8678 height, False);
8679 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8680 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8681 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8682 height, False);
8683 }
8684
8685 /* Move/size the scroll bar window. */
8686 if (mask)
8687 {
8688 XWindowChanges wc;
8689
8690 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8691 wc.y = top;
8692 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8693 wc.height = height;
8694 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8695 mask, &wc);
8696 }
8697
8698 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8699
8700 /* Remember new settings. */
8701 XSETINT (bar->left, sb_left);
8702 XSETINT (bar->top, top);
8703 XSETINT (bar->width, sb_width);
8704 XSETINT (bar->height, height);
8705
8706 UNBLOCK_INPUT;
8707 }
8708
8709 #ifdef USE_TOOLKIT_SCROLL_BARS
8710 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8711 #else /* not USE_TOOLKIT_SCROLL_BARS */
8712 /* Set the scroll bar's current state, unless we're currently being
8713 dragged. */
8714 if (NILP (bar->dragging))
8715 {
8716 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8717
8718 if (whole == 0)
8719 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8720 else
8721 {
8722 int start = ((double) position * top_range) / whole;
8723 int end = ((double) (position + portion) * top_range) / whole;
8724 x_scroll_bar_set_handle (bar, start, end, 0);
8725 }
8726 }
8727 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8728
8729 XSETVECTOR (w->vertical_scroll_bar, bar);
8730 }
8731
8732
8733 /* The following three hooks are used when we're doing a thorough
8734 redisplay of the frame. We don't explicitly know which scroll bars
8735 are going to be deleted, because keeping track of when windows go
8736 away is a real pain - "Can you say set-window-configuration, boys
8737 and girls?" Instead, we just assert at the beginning of redisplay
8738 that *all* scroll bars are to be removed, and then save a scroll bar
8739 from the fiery pit when we actually redisplay its window. */
8740
8741 /* Arrange for all scroll bars on FRAME to be removed at the next call
8742 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8743 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8744
8745 static void
8746 XTcondemn_scroll_bars (frame)
8747 FRAME_PTR frame;
8748 {
8749 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8750 while (! NILP (FRAME_SCROLL_BARS (frame)))
8751 {
8752 Lisp_Object bar;
8753 bar = FRAME_SCROLL_BARS (frame);
8754 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8755 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8756 XSCROLL_BAR (bar)->prev = Qnil;
8757 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8758 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8759 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8760 }
8761 }
8762
8763
8764 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8765 Note that WINDOW isn't necessarily condemned at all. */
8766
8767 static void
8768 XTredeem_scroll_bar (window)
8769 struct window *window;
8770 {
8771 struct scroll_bar *bar;
8772 struct frame *f;
8773
8774 /* We can't redeem this window's scroll bar if it doesn't have one. */
8775 if (NILP (window->vertical_scroll_bar))
8776 abort ();
8777
8778 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8779
8780 /* Unlink it from the condemned list. */
8781 f = XFRAME (WINDOW_FRAME (window));
8782 if (NILP (bar->prev))
8783 {
8784 /* If the prev pointer is nil, it must be the first in one of
8785 the lists. */
8786 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8787 /* It's not condemned. Everything's fine. */
8788 return;
8789 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8790 window->vertical_scroll_bar))
8791 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8792 else
8793 /* If its prev pointer is nil, it must be at the front of
8794 one or the other! */
8795 abort ();
8796 }
8797 else
8798 XSCROLL_BAR (bar->prev)->next = bar->next;
8799
8800 if (! NILP (bar->next))
8801 XSCROLL_BAR (bar->next)->prev = bar->prev;
8802
8803 bar->next = FRAME_SCROLL_BARS (f);
8804 bar->prev = Qnil;
8805 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8806 if (! NILP (bar->next))
8807 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8808 }
8809
8810 /* Remove all scroll bars on FRAME that haven't been saved since the
8811 last call to `*condemn_scroll_bars_hook'. */
8812
8813 static void
8814 XTjudge_scroll_bars (f)
8815 FRAME_PTR f;
8816 {
8817 Lisp_Object bar, next;
8818
8819 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8820
8821 /* Clear out the condemned list now so we won't try to process any
8822 more events on the hapless scroll bars. */
8823 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8824
8825 for (; ! NILP (bar); bar = next)
8826 {
8827 struct scroll_bar *b = XSCROLL_BAR (bar);
8828
8829 x_scroll_bar_remove (b);
8830
8831 next = b->next;
8832 b->next = b->prev = Qnil;
8833 }
8834
8835 /* Now there should be no references to the condemned scroll bars,
8836 and they should get garbage-collected. */
8837 }
8838
8839
8840 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8841 is a no-op when using toolkit scroll bars.
8842
8843 This may be called from a signal handler, so we have to ignore GC
8844 mark bits. */
8845
8846 static void
8847 x_scroll_bar_expose (bar, event)
8848 struct scroll_bar *bar;
8849 XEvent *event;
8850 {
8851 #ifndef USE_TOOLKIT_SCROLL_BARS
8852
8853 Window w = SCROLL_BAR_X_WINDOW (bar);
8854 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8855 GC gc = f->output_data.x->normal_gc;
8856 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8857
8858 BLOCK_INPUT;
8859
8860 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8861
8862 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8863 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8864
8865 /* x, y, width, height */
8866 0, 0,
8867 XINT (bar->width) - 1 - width_trim - width_trim,
8868 XINT (bar->height) - 1);
8869
8870 UNBLOCK_INPUT;
8871
8872 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8873 }
8874
8875 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8876 is set to something other than no_event, it is enqueued.
8877
8878 This may be called from a signal handler, so we have to ignore GC
8879 mark bits. */
8880
8881 #ifndef USE_TOOLKIT_SCROLL_BARS
8882
8883 static void
8884 x_scroll_bar_handle_click (bar, event, emacs_event)
8885 struct scroll_bar *bar;
8886 XEvent *event;
8887 struct input_event *emacs_event;
8888 {
8889 if (! GC_WINDOWP (bar->window))
8890 abort ();
8891
8892 emacs_event->kind = scroll_bar_click;
8893 emacs_event->code = event->xbutton.button - Button1;
8894 emacs_event->modifiers
8895 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8896 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8897 event->xbutton.state)
8898 | (event->type == ButtonRelease
8899 ? up_modifier
8900 : down_modifier));
8901 emacs_event->frame_or_window = bar->window;
8902 emacs_event->arg = Qnil;
8903 emacs_event->timestamp = event->xbutton.time;
8904 {
8905 #if 0
8906 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8907 int internal_height
8908 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8909 #endif
8910 int top_range
8911 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8912 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8913
8914 if (y < 0) y = 0;
8915 if (y > top_range) y = top_range;
8916
8917 if (y < XINT (bar->start))
8918 emacs_event->part = scroll_bar_above_handle;
8919 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8920 emacs_event->part = scroll_bar_handle;
8921 else
8922 emacs_event->part = scroll_bar_below_handle;
8923
8924 /* Just because the user has clicked on the handle doesn't mean
8925 they want to drag it. Lisp code needs to be able to decide
8926 whether or not we're dragging. */
8927 #if 0
8928 /* If the user has just clicked on the handle, record where they're
8929 holding it. */
8930 if (event->type == ButtonPress
8931 && emacs_event->part == scroll_bar_handle)
8932 XSETINT (bar->dragging, y - XINT (bar->start));
8933 #endif
8934
8935 /* If the user has released the handle, set it to its final position. */
8936 if (event->type == ButtonRelease
8937 && ! NILP (bar->dragging))
8938 {
8939 int new_start = y - XINT (bar->dragging);
8940 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8941
8942 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8943 bar->dragging = Qnil;
8944 }
8945
8946 /* Same deal here as the other #if 0. */
8947 #if 0
8948 /* Clicks on the handle are always reported as occurring at the top of
8949 the handle. */
8950 if (emacs_event->part == scroll_bar_handle)
8951 emacs_event->x = bar->start;
8952 else
8953 XSETINT (emacs_event->x, y);
8954 #else
8955 XSETINT (emacs_event->x, y);
8956 #endif
8957
8958 XSETINT (emacs_event->y, top_range);
8959 }
8960 }
8961
8962 /* Handle some mouse motion while someone is dragging the scroll bar.
8963
8964 This may be called from a signal handler, so we have to ignore GC
8965 mark bits. */
8966
8967 static void
8968 x_scroll_bar_note_movement (bar, event)
8969 struct scroll_bar *bar;
8970 XEvent *event;
8971 {
8972 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8973
8974 last_mouse_movement_time = event->xmotion.time;
8975
8976 f->mouse_moved = 1;
8977 XSETVECTOR (last_mouse_scroll_bar, bar);
8978
8979 /* If we're dragging the bar, display it. */
8980 if (! GC_NILP (bar->dragging))
8981 {
8982 /* Where should the handle be now? */
8983 int new_start = event->xmotion.y - XINT (bar->dragging);
8984
8985 if (new_start != XINT (bar->start))
8986 {
8987 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8988
8989 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8990 }
8991 }
8992 }
8993
8994 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8995
8996 /* Return information to the user about the current position of the mouse
8997 on the scroll bar. */
8998
8999 static void
9000 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9001 FRAME_PTR *fp;
9002 Lisp_Object *bar_window;
9003 enum scroll_bar_part *part;
9004 Lisp_Object *x, *y;
9005 unsigned long *time;
9006 {
9007 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9008 Window w = SCROLL_BAR_X_WINDOW (bar);
9009 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9010 int win_x, win_y;
9011 Window dummy_window;
9012 int dummy_coord;
9013 unsigned int dummy_mask;
9014
9015 BLOCK_INPUT;
9016
9017 /* Get the mouse's position relative to the scroll bar window, and
9018 report that. */
9019 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9020
9021 /* Root, child, root x and root y. */
9022 &dummy_window, &dummy_window,
9023 &dummy_coord, &dummy_coord,
9024
9025 /* Position relative to scroll bar. */
9026 &win_x, &win_y,
9027
9028 /* Mouse buttons and modifier keys. */
9029 &dummy_mask))
9030 ;
9031 else
9032 {
9033 #if 0
9034 int inside_height
9035 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9036 #endif
9037 int top_range
9038 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9039
9040 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9041
9042 if (! NILP (bar->dragging))
9043 win_y -= XINT (bar->dragging);
9044
9045 if (win_y < 0)
9046 win_y = 0;
9047 if (win_y > top_range)
9048 win_y = top_range;
9049
9050 *fp = f;
9051 *bar_window = bar->window;
9052
9053 if (! NILP (bar->dragging))
9054 *part = scroll_bar_handle;
9055 else if (win_y < XINT (bar->start))
9056 *part = scroll_bar_above_handle;
9057 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9058 *part = scroll_bar_handle;
9059 else
9060 *part = scroll_bar_below_handle;
9061
9062 XSETINT (*x, win_y);
9063 XSETINT (*y, top_range);
9064
9065 f->mouse_moved = 0;
9066 last_mouse_scroll_bar = Qnil;
9067 }
9068
9069 *time = last_mouse_movement_time;
9070
9071 UNBLOCK_INPUT;
9072 }
9073
9074
9075 /* The screen has been cleared so we may have changed foreground or
9076 background colors, and the scroll bars may need to be redrawn.
9077 Clear out the scroll bars, and ask for expose events, so we can
9078 redraw them. */
9079
9080 void
9081 x_scroll_bar_clear (f)
9082 FRAME_PTR f;
9083 {
9084 #ifndef USE_TOOLKIT_SCROLL_BARS
9085 Lisp_Object bar;
9086
9087 /* We can have scroll bars even if this is 0,
9088 if we just turned off scroll bar mode.
9089 But in that case we should not clear them. */
9090 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9091 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9092 bar = XSCROLL_BAR (bar)->next)
9093 XClearArea (FRAME_X_DISPLAY (f),
9094 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9095 0, 0, 0, 0, True);
9096 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9097 }
9098
9099 /* This processes Expose events from the menu-bar specific X event
9100 loop in xmenu.c. This allows to redisplay the frame if necessary
9101 when handling menu-bar or pop-up items. */
9102
9103 int
9104 process_expose_from_menu (event)
9105 XEvent event;
9106 {
9107 FRAME_PTR f;
9108 struct x_display_info *dpyinfo;
9109 int frame_exposed_p = 0;
9110
9111 BLOCK_INPUT;
9112
9113 dpyinfo = x_display_info_for_display (event.xexpose.display);
9114 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9115 if (f)
9116 {
9117 if (f->async_visible == 0)
9118 {
9119 f->async_visible = 1;
9120 f->async_iconified = 0;
9121 f->output_data.x->has_been_visible = 1;
9122 SET_FRAME_GARBAGED (f);
9123 }
9124 else
9125 {
9126 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9127 event.xexpose.x, event.xexpose.y,
9128 event.xexpose.width, event.xexpose.height);
9129 frame_exposed_p = 1;
9130 }
9131 }
9132 else
9133 {
9134 struct scroll_bar *bar
9135 = x_window_to_scroll_bar (event.xexpose.window);
9136
9137 if (bar)
9138 x_scroll_bar_expose (bar, &event);
9139 }
9140
9141 UNBLOCK_INPUT;
9142 return frame_exposed_p;
9143 }
9144 \f
9145 /* Define a queue to save up SelectionRequest events for later handling. */
9146
9147 struct selection_event_queue
9148 {
9149 XEvent event;
9150 struct selection_event_queue *next;
9151 };
9152
9153 static struct selection_event_queue *queue;
9154
9155 /* Nonzero means queue up certain events--don't process them yet. */
9156
9157 static int x_queue_selection_requests;
9158
9159 /* Queue up an X event *EVENT, to be processed later. */
9160
9161 static void
9162 x_queue_event (f, event)
9163 FRAME_PTR f;
9164 XEvent *event;
9165 {
9166 struct selection_event_queue *queue_tmp
9167 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9168
9169 if (queue_tmp != NULL)
9170 {
9171 queue_tmp->event = *event;
9172 queue_tmp->next = queue;
9173 queue = queue_tmp;
9174 }
9175 }
9176
9177 /* Take all the queued events and put them back
9178 so that they get processed afresh. */
9179
9180 static void
9181 x_unqueue_events (display)
9182 Display *display;
9183 {
9184 while (queue != NULL)
9185 {
9186 struct selection_event_queue *queue_tmp = queue;
9187 XPutBackEvent (display, &queue_tmp->event);
9188 queue = queue_tmp->next;
9189 xfree ((char *)queue_tmp);
9190 }
9191 }
9192
9193 /* Start queuing SelectionRequest events. */
9194
9195 void
9196 x_start_queuing_selection_requests (display)
9197 Display *display;
9198 {
9199 x_queue_selection_requests++;
9200 }
9201
9202 /* Stop queuing SelectionRequest events. */
9203
9204 void
9205 x_stop_queuing_selection_requests (display)
9206 Display *display;
9207 {
9208 x_queue_selection_requests--;
9209 x_unqueue_events (display);
9210 }
9211 \f
9212 /* The main X event-reading loop - XTread_socket. */
9213
9214 /* Time stamp of enter window event. This is only used by XTread_socket,
9215 but we have to put it out here, since static variables within functions
9216 sometimes don't work. */
9217
9218 static Time enter_timestamp;
9219
9220 /* This holds the state XLookupString needs to implement dead keys
9221 and other tricks known as "compose processing". _X Window System_
9222 says that a portable program can't use this, but Stephen Gildea assures
9223 me that letting the compiler initialize it to zeros will work okay.
9224
9225 This must be defined outside of XTread_socket, for the same reasons
9226 given for enter_time stamp, above. */
9227
9228 static XComposeStatus compose_status;
9229
9230 /* Record the last 100 characters stored
9231 to help debug the loss-of-chars-during-GC problem. */
9232
9233 static int temp_index;
9234 static short temp_buffer[100];
9235
9236 /* Set this to nonzero to fake an "X I/O error"
9237 on a particular display. */
9238
9239 struct x_display_info *XTread_socket_fake_io_error;
9240
9241 /* When we find no input here, we occasionally do a no-op command
9242 to verify that the X server is still running and we can still talk with it.
9243 We try all the open displays, one by one.
9244 This variable is used for cycling thru the displays. */
9245
9246 static struct x_display_info *next_noop_dpyinfo;
9247
9248 #define SET_SAVED_MENU_EVENT(size) \
9249 do \
9250 { \
9251 if (f->output_data.x->saved_menu_event == 0) \
9252 f->output_data.x->saved_menu_event \
9253 = (XEvent *) xmalloc (sizeof (XEvent)); \
9254 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9255 if (numchars >= 1) \
9256 { \
9257 bufp->kind = menu_bar_activate_event; \
9258 XSETFRAME (bufp->frame_or_window, f); \
9259 bufp->arg = Qnil; \
9260 bufp++; \
9261 count++; \
9262 numchars--; \
9263 } \
9264 } \
9265 while (0)
9266
9267 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9268 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9269
9270 /* Read events coming from the X server.
9271 This routine is called by the SIGIO handler.
9272 We return as soon as there are no more events to be read.
9273
9274 Events representing keys are stored in buffer BUFP,
9275 which can hold up to NUMCHARS characters.
9276 We return the number of characters stored into the buffer,
9277 thus pretending to be `read'.
9278
9279 EXPECTED is nonzero if the caller knows input is available. */
9280
9281 int
9282 XTread_socket (sd, bufp, numchars, expected)
9283 register int sd;
9284 /* register */ struct input_event *bufp;
9285 /* register */ int numchars;
9286 int expected;
9287 {
9288 int count = 0;
9289 int nbytes = 0;
9290 XEvent event;
9291 struct frame *f;
9292 int event_found = 0;
9293 struct x_display_info *dpyinfo;
9294 struct coding_system coding;
9295
9296 if (interrupt_input_blocked)
9297 {
9298 interrupt_input_pending = 1;
9299 return -1;
9300 }
9301
9302 interrupt_input_pending = 0;
9303 BLOCK_INPUT;
9304
9305 /* So people can tell when we have read the available input. */
9306 input_signal_count++;
9307
9308 if (numchars <= 0)
9309 abort (); /* Don't think this happens. */
9310
9311 ++handling_signal;
9312
9313 /* The input should be decoded if it is from XIM. Currently the
9314 locale of XIM is the same as that of the system. So, we can use
9315 Vlocale_coding_system which is initialized properly at Emacs
9316 startup time. */
9317 setup_coding_system (Vlocale_coding_system, &coding);
9318 coding.src_multibyte = 0;
9319 coding.dst_multibyte = 1;
9320 /* The input is converted to events, thus we can't handle
9321 composition. Anyway, there's no XIM that gives us composition
9322 information. */
9323 coding.composing = COMPOSITION_DISABLED;
9324
9325 /* Find the display we are supposed to read input for.
9326 It's the one communicating on descriptor SD. */
9327 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9328 {
9329 #if 0 /* This ought to be unnecessary; let's verify it. */
9330 #ifdef FIOSNBIO
9331 /* If available, Xlib uses FIOSNBIO to make the socket
9332 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9333 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9334 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9335 fcntl (dpyinfo->connection, F_SETFL, 0);
9336 #endif /* ! defined (FIOSNBIO) */
9337 #endif
9338
9339 #if 0 /* This code can't be made to work, with multiple displays,
9340 and appears not to be used on any system any more.
9341 Also keyboard.c doesn't turn O_NDELAY on and off
9342 for X connections. */
9343 #ifndef SIGIO
9344 #ifndef HAVE_SELECT
9345 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9346 {
9347 extern int read_alarm_should_throw;
9348 read_alarm_should_throw = 1;
9349 XPeekEvent (dpyinfo->display, &event);
9350 read_alarm_should_throw = 0;
9351 }
9352 #endif /* HAVE_SELECT */
9353 #endif /* SIGIO */
9354 #endif
9355
9356 /* For debugging, this gives a way to fake an I/O error. */
9357 if (dpyinfo == XTread_socket_fake_io_error)
9358 {
9359 XTread_socket_fake_io_error = 0;
9360 x_io_error_quitter (dpyinfo->display);
9361 }
9362
9363 while (XPending (dpyinfo->display))
9364 {
9365 XNextEvent (dpyinfo->display, &event);
9366
9367 #ifdef HAVE_X_I18N
9368 {
9369 /* Filter events for the current X input method.
9370 XFilterEvent returns non-zero if the input method has
9371 consumed the event. We pass the frame's X window to
9372 XFilterEvent because that's the one for which the IC
9373 was created. */
9374 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9375 event.xclient.window);
9376 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9377 break;
9378 }
9379 #endif
9380 event_found = 1;
9381
9382 switch (event.type)
9383 {
9384 case ClientMessage:
9385 {
9386 if (event.xclient.message_type
9387 == dpyinfo->Xatom_wm_protocols
9388 && event.xclient.format == 32)
9389 {
9390 if (event.xclient.data.l[0]
9391 == dpyinfo->Xatom_wm_take_focus)
9392 {
9393 /* Use x_any_window_to_frame because this
9394 could be the shell widget window
9395 if the frame has no title bar. */
9396 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9397 #ifdef HAVE_X_I18N
9398 /* Not quite sure this is needed -pd */
9399 if (f && FRAME_XIC (f))
9400 XSetICFocus (FRAME_XIC (f));
9401 #endif
9402 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9403 instructs the WM to set the input focus automatically for
9404 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9405 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9406 it has set the focus. So, XSetInputFocus below is not
9407 needed.
9408
9409 The call to XSetInputFocus below has also caused trouble. In
9410 cases where the XSetInputFocus done by the WM and the one
9411 below are temporally close (on a fast machine), the call
9412 below can generate additional FocusIn events which confuse
9413 Emacs. */
9414
9415 /* Since we set WM_TAKE_FOCUS, we must call
9416 XSetInputFocus explicitly. But not if f is null,
9417 since that might be an event for a deleted frame. */
9418 if (f)
9419 {
9420 Display *d = event.xclient.display;
9421 /* Catch and ignore errors, in case window has been
9422 iconified by a window manager such as GWM. */
9423 int count = x_catch_errors (d);
9424 XSetInputFocus (d, event.xclient.window,
9425 /* The ICCCM says this is
9426 the only valid choice. */
9427 RevertToParent,
9428 event.xclient.data.l[1]);
9429 /* This is needed to detect the error
9430 if there is an error. */
9431 XSync (d, False);
9432 x_uncatch_errors (d, count);
9433 }
9434 /* Not certain about handling scroll bars here */
9435 #endif /* 0 */
9436 }
9437 else if (event.xclient.data.l[0]
9438 == dpyinfo->Xatom_wm_save_yourself)
9439 {
9440 /* Save state modify the WM_COMMAND property to
9441 something which can reinstate us. This notifies
9442 the session manager, who's looking for such a
9443 PropertyNotify. Can restart processing when
9444 a keyboard or mouse event arrives. */
9445 if (numchars > 0)
9446 {
9447 f = x_top_window_to_frame (dpyinfo,
9448 event.xclient.window);
9449
9450 /* This is just so we only give real data once
9451 for a single Emacs process. */
9452 if (f == SELECTED_FRAME ())
9453 XSetCommand (FRAME_X_DISPLAY (f),
9454 event.xclient.window,
9455 initial_argv, initial_argc);
9456 else if (f)
9457 XSetCommand (FRAME_X_DISPLAY (f),
9458 event.xclient.window,
9459 0, 0);
9460 }
9461 }
9462 else if (event.xclient.data.l[0]
9463 == dpyinfo->Xatom_wm_delete_window)
9464 {
9465 struct frame *f
9466 = x_any_window_to_frame (dpyinfo,
9467 event.xclient.window);
9468
9469 if (f)
9470 {
9471 if (numchars == 0)
9472 abort ();
9473
9474 bufp->kind = delete_window_event;
9475 XSETFRAME (bufp->frame_or_window, f);
9476 bufp->arg = Qnil;
9477 bufp++;
9478
9479 count += 1;
9480 numchars -= 1;
9481 }
9482 }
9483 }
9484 else if (event.xclient.message_type
9485 == dpyinfo->Xatom_wm_configure_denied)
9486 {
9487 }
9488 else if (event.xclient.message_type
9489 == dpyinfo->Xatom_wm_window_moved)
9490 {
9491 int new_x, new_y;
9492 struct frame *f
9493 = x_window_to_frame (dpyinfo, event.xclient.window);
9494
9495 new_x = event.xclient.data.s[0];
9496 new_y = event.xclient.data.s[1];
9497
9498 if (f)
9499 {
9500 f->output_data.x->left_pos = new_x;
9501 f->output_data.x->top_pos = new_y;
9502 }
9503 }
9504 #ifdef HACK_EDITRES
9505 else if (event.xclient.message_type
9506 == dpyinfo->Xatom_editres)
9507 {
9508 struct frame *f
9509 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9510 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9511 &event, NULL);
9512 }
9513 #endif /* HACK_EDITRES */
9514 else if ((event.xclient.message_type
9515 == dpyinfo->Xatom_DONE)
9516 || (event.xclient.message_type
9517 == dpyinfo->Xatom_PAGE))
9518 {
9519 /* Ghostview job completed. Kill it. We could
9520 reply with "Next" if we received "Page", but we
9521 currently never do because we are interested in
9522 images, only, which should have 1 page. */
9523 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9524 struct frame *f
9525 = x_window_to_frame (dpyinfo, event.xclient.window);
9526 x_kill_gs_process (pixmap, f);
9527 expose_frame (f, 0, 0, 0, 0);
9528 }
9529 #ifdef USE_TOOLKIT_SCROLL_BARS
9530 /* Scroll bar callbacks send a ClientMessage from which
9531 we construct an input_event. */
9532 else if (event.xclient.message_type
9533 == dpyinfo->Xatom_Scrollbar)
9534 {
9535 x_scroll_bar_to_input_event (&event, bufp);
9536 ++bufp, ++count, --numchars;
9537 goto out;
9538 }
9539 #endif /* USE_TOOLKIT_SCROLL_BARS */
9540 else
9541 goto OTHER;
9542 }
9543 break;
9544
9545 case SelectionNotify:
9546 #ifdef USE_X_TOOLKIT
9547 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9548 goto OTHER;
9549 #endif /* not USE_X_TOOLKIT */
9550 x_handle_selection_notify (&event.xselection);
9551 break;
9552
9553 case SelectionClear: /* Someone has grabbed ownership. */
9554 #ifdef USE_X_TOOLKIT
9555 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9556 goto OTHER;
9557 #endif /* USE_X_TOOLKIT */
9558 {
9559 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9560
9561 if (numchars == 0)
9562 abort ();
9563
9564 bufp->kind = selection_clear_event;
9565 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9566 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9567 SELECTION_EVENT_TIME (bufp) = eventp->time;
9568 bufp->frame_or_window = Qnil;
9569 bufp->arg = Qnil;
9570 bufp++;
9571
9572 count += 1;
9573 numchars -= 1;
9574 }
9575 break;
9576
9577 case SelectionRequest: /* Someone wants our selection. */
9578 #ifdef USE_X_TOOLKIT
9579 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9580 goto OTHER;
9581 #endif /* USE_X_TOOLKIT */
9582 if (x_queue_selection_requests)
9583 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9584 &event);
9585 else
9586 {
9587 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9588
9589 if (numchars == 0)
9590 abort ();
9591
9592 bufp->kind = selection_request_event;
9593 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9594 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9595 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9596 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9597 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9598 SELECTION_EVENT_TIME (bufp) = eventp->time;
9599 bufp->frame_or_window = Qnil;
9600 bufp->arg = Qnil;
9601 bufp++;
9602
9603 count += 1;
9604 numchars -= 1;
9605 }
9606 break;
9607
9608 case PropertyNotify:
9609 #ifdef USE_X_TOOLKIT
9610 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9611 goto OTHER;
9612 #endif /* not USE_X_TOOLKIT */
9613 x_handle_property_notify (&event.xproperty);
9614 break;
9615
9616 case ReparentNotify:
9617 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9618 if (f)
9619 {
9620 int x, y;
9621 f->output_data.x->parent_desc = event.xreparent.parent;
9622 x_real_positions (f, &x, &y);
9623 f->output_data.x->left_pos = x;
9624 f->output_data.x->top_pos = y;
9625 }
9626 break;
9627
9628 case Expose:
9629 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9630 if (f)
9631 {
9632 if (f->async_visible == 0)
9633 {
9634 f->async_visible = 1;
9635 f->async_iconified = 0;
9636 f->output_data.x->has_been_visible = 1;
9637 SET_FRAME_GARBAGED (f);
9638 }
9639 else
9640 expose_frame (x_window_to_frame (dpyinfo,
9641 event.xexpose.window),
9642 event.xexpose.x, event.xexpose.y,
9643 event.xexpose.width, event.xexpose.height);
9644 }
9645 else
9646 {
9647 #ifdef USE_TOOLKIT_SCROLL_BARS
9648 /* Dispatch event to the widget. */
9649 goto OTHER;
9650 #else /* not USE_TOOLKIT_SCROLL_BARS */
9651 struct scroll_bar *bar
9652 = x_window_to_scroll_bar (event.xexpose.window);
9653
9654 if (bar)
9655 x_scroll_bar_expose (bar, &event);
9656 #ifdef USE_X_TOOLKIT
9657 else
9658 goto OTHER;
9659 #endif /* USE_X_TOOLKIT */
9660 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9661 }
9662 break;
9663
9664 case GraphicsExpose: /* This occurs when an XCopyArea's
9665 source area was obscured or not
9666 available.*/
9667 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9668 if (f)
9669 {
9670 expose_frame (f,
9671 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9672 event.xgraphicsexpose.width,
9673 event.xgraphicsexpose.height);
9674 }
9675 #ifdef USE_X_TOOLKIT
9676 else
9677 goto OTHER;
9678 #endif /* USE_X_TOOLKIT */
9679 break;
9680
9681 case NoExpose: /* This occurs when an XCopyArea's
9682 source area was completely
9683 available */
9684 break;
9685
9686 case UnmapNotify:
9687 /* Redo the mouse-highlight after the tooltip has gone. */
9688 if (event.xmap.window == tip_window)
9689 {
9690 tip_window = 0;
9691 redo_mouse_highlight ();
9692 }
9693
9694 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9695 if (f) /* F may no longer exist if
9696 the frame was deleted. */
9697 {
9698 /* While a frame is unmapped, display generation is
9699 disabled; you don't want to spend time updating a
9700 display that won't ever be seen. */
9701 f->async_visible = 0;
9702 /* We can't distinguish, from the event, whether the window
9703 has become iconified or invisible. So assume, if it
9704 was previously visible, than now it is iconified.
9705 But x_make_frame_invisible clears both
9706 the visible flag and the iconified flag;
9707 and that way, we know the window is not iconified now. */
9708 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9709 {
9710 f->async_iconified = 1;
9711
9712 bufp->kind = iconify_event;
9713 XSETFRAME (bufp->frame_or_window, f);
9714 bufp->arg = Qnil;
9715 bufp++;
9716 count++;
9717 numchars--;
9718 }
9719 }
9720 goto OTHER;
9721
9722 case MapNotify:
9723 if (event.xmap.window == tip_window)
9724 /* The tooltip has been drawn already. Avoid
9725 the SET_FRAME_GARBAGED below. */
9726 goto OTHER;
9727
9728 /* We use x_top_window_to_frame because map events can
9729 come for sub-windows and they don't mean that the
9730 frame is visible. */
9731 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9732 if (f)
9733 {
9734 f->async_visible = 1;
9735 f->async_iconified = 0;
9736 f->output_data.x->has_been_visible = 1;
9737
9738 /* wait_reading_process_input will notice this and update
9739 the frame's display structures. */
9740 SET_FRAME_GARBAGED (f);
9741
9742 if (f->iconified)
9743 {
9744 bufp->kind = deiconify_event;
9745 XSETFRAME (bufp->frame_or_window, f);
9746 bufp->arg = Qnil;
9747 bufp++;
9748 count++;
9749 numchars--;
9750 }
9751 else if (! NILP (Vframe_list)
9752 && ! NILP (XCDR (Vframe_list)))
9753 /* Force a redisplay sooner or later
9754 to update the frame titles
9755 in case this is the second frame. */
9756 record_asynch_buffer_change ();
9757 }
9758 goto OTHER;
9759
9760 case KeyPress:
9761 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9762
9763 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
9764 /* I couldn't find a way to prevent LessTif scroll bars
9765 from consuming key events. */
9766 if (f == 0)
9767 {
9768 Widget widget = XtWindowToWidget (dpyinfo->display,
9769 event.xkey.window);
9770 if (widget && XmIsScrollBar (widget))
9771 {
9772 widget = XtParent (widget);
9773 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9774 }
9775 }
9776 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
9777
9778 if (f != 0)
9779 {
9780 KeySym keysym, orig_keysym;
9781 /* al%imercury@uunet.uu.net says that making this 81
9782 instead of 80 fixed a bug whereby meta chars made
9783 his Emacs hang.
9784
9785 It seems that some version of XmbLookupString has
9786 a bug of not returning XBufferOverflow in
9787 status_return even if the input is too long to
9788 fit in 81 bytes. So, we must prepare sufficient
9789 bytes for copy_buffer. 513 bytes (256 chars for
9790 two-byte character set) seems to be a faily good
9791 approximation. -- 2000.8.10 handa@etl.go.jp */
9792 unsigned char copy_buffer[513];
9793 unsigned char *copy_bufptr = copy_buffer;
9794 int copy_bufsiz = sizeof (copy_buffer);
9795 int modifiers;
9796
9797 event.xkey.state
9798 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9799 extra_keyboard_modifiers);
9800 modifiers = event.xkey.state;
9801
9802 /* This will have to go some day... */
9803
9804 /* make_lispy_event turns chars into control chars.
9805 Don't do it here because XLookupString is too eager. */
9806 event.xkey.state &= ~ControlMask;
9807 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9808 | dpyinfo->super_mod_mask
9809 | dpyinfo->hyper_mod_mask
9810 | dpyinfo->alt_mod_mask);
9811
9812 /* In case Meta is ComposeCharacter,
9813 clear its status. According to Markus Ehrnsperger
9814 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9815 this enables ComposeCharacter to work whether or
9816 not it is combined with Meta. */
9817 if (modifiers & dpyinfo->meta_mod_mask)
9818 bzero (&compose_status, sizeof (compose_status));
9819
9820 #ifdef HAVE_X_I18N
9821 if (FRAME_XIC (f))
9822 {
9823 Status status_return;
9824
9825 nbytes = XmbLookupString (FRAME_XIC (f),
9826 &event.xkey, copy_bufptr,
9827 copy_bufsiz, &keysym,
9828 &status_return);
9829 if (status_return == XBufferOverflow)
9830 {
9831 copy_bufsiz = nbytes + 1;
9832 copy_bufptr = (char *) alloca (copy_bufsiz);
9833 nbytes = XmbLookupString (FRAME_XIC (f),
9834 &event.xkey, copy_bufptr,
9835 copy_bufsiz, &keysym,
9836 &status_return);
9837 }
9838
9839 if (status_return == XLookupNone)
9840 break;
9841 else if (status_return == XLookupChars)
9842 {
9843 keysym = NoSymbol;
9844 modifiers = 0;
9845 }
9846 else if (status_return != XLookupKeySym
9847 && status_return != XLookupBoth)
9848 abort ();
9849 }
9850 else
9851 nbytes = XLookupString (&event.xkey, copy_bufptr,
9852 copy_bufsiz, &keysym,
9853 &compose_status);
9854 #else
9855 nbytes = XLookupString (&event.xkey, copy_bufptr,
9856 copy_bufsiz, &keysym,
9857 &compose_status);
9858 #endif
9859
9860 orig_keysym = keysym;
9861
9862 if (numchars > 1)
9863 {
9864 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9865 || keysym == XK_Delete
9866 #ifdef XK_ISO_Left_Tab
9867 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9868 #endif
9869 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9870 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9871 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9872 #ifdef HPUX
9873 /* This recognizes the "extended function keys".
9874 It seems there's no cleaner way.
9875 Test IsModifierKey to avoid handling mode_switch
9876 incorrectly. */
9877 || ((unsigned) (keysym) >= XK_Select
9878 && (unsigned)(keysym) < XK_KP_Space)
9879 #endif
9880 #ifdef XK_dead_circumflex
9881 || orig_keysym == XK_dead_circumflex
9882 #endif
9883 #ifdef XK_dead_grave
9884 || orig_keysym == XK_dead_grave
9885 #endif
9886 #ifdef XK_dead_tilde
9887 || orig_keysym == XK_dead_tilde
9888 #endif
9889 #ifdef XK_dead_diaeresis
9890 || orig_keysym == XK_dead_diaeresis
9891 #endif
9892 #ifdef XK_dead_macron
9893 || orig_keysym == XK_dead_macron
9894 #endif
9895 #ifdef XK_dead_degree
9896 || orig_keysym == XK_dead_degree
9897 #endif
9898 #ifdef XK_dead_acute
9899 || orig_keysym == XK_dead_acute
9900 #endif
9901 #ifdef XK_dead_cedilla
9902 || orig_keysym == XK_dead_cedilla
9903 #endif
9904 #ifdef XK_dead_breve
9905 || orig_keysym == XK_dead_breve
9906 #endif
9907 #ifdef XK_dead_ogonek
9908 || orig_keysym == XK_dead_ogonek
9909 #endif
9910 #ifdef XK_dead_caron
9911 || orig_keysym == XK_dead_caron
9912 #endif
9913 #ifdef XK_dead_doubleacute
9914 || orig_keysym == XK_dead_doubleacute
9915 #endif
9916 #ifdef XK_dead_abovedot
9917 || orig_keysym == XK_dead_abovedot
9918 #endif
9919 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9920 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9921 /* Any "vendor-specific" key is ok. */
9922 || (orig_keysym & (1 << 28)))
9923 && ! (IsModifierKey (orig_keysym)
9924 #ifndef HAVE_X11R5
9925 #ifdef XK_Mode_switch
9926 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9927 #endif
9928 #ifdef XK_Num_Lock
9929 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9930 #endif
9931 #endif /* not HAVE_X11R5 */
9932 ))
9933 {
9934 if (temp_index == sizeof temp_buffer / sizeof (short))
9935 temp_index = 0;
9936 temp_buffer[temp_index++] = keysym;
9937 bufp->kind = non_ascii_keystroke;
9938 bufp->code = keysym;
9939 XSETFRAME (bufp->frame_or_window, f);
9940 bufp->arg = Qnil;
9941 bufp->modifiers
9942 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9943 modifiers);
9944 bufp->timestamp = event.xkey.time;
9945 bufp++;
9946 count++;
9947 numchars--;
9948 }
9949 else if (numchars > nbytes)
9950 {
9951 register int i;
9952 register int c;
9953 int nchars, len;
9954
9955 for (i = 0; i < nbytes; i++)
9956 {
9957 if (temp_index == (sizeof temp_buffer
9958 / sizeof (short)))
9959 temp_index = 0;
9960 temp_buffer[temp_index++] = copy_bufptr[i];
9961 }
9962
9963 if (/* If the event is not from XIM, */
9964 event.xkey.keycode != 0
9965 /* or the current locale doesn't request
9966 decoding of the intup data, ... */
9967 || coding.type == coding_type_raw_text
9968 || coding.type == coding_type_no_conversion)
9969 {
9970 /* ... we can use the input data as is. */
9971 nchars = nbytes;
9972 }
9973 else
9974 {
9975 /* We have to decode the input data. */
9976 int require;
9977 unsigned char *p;
9978
9979 require = decoding_buffer_size (&coding, nbytes);
9980 p = (unsigned char *) alloca (require);
9981 coding.mode |= CODING_MODE_LAST_BLOCK;
9982 decode_coding (&coding, copy_bufptr, p,
9983 nbytes, require);
9984 nbytes = coding.produced;
9985 nchars = coding.produced_char;
9986 copy_bufptr = p;
9987 }
9988
9989 /* Convert the input data to a sequence of
9990 character events. */
9991 for (i = 0; i < nbytes; i += len)
9992 {
9993 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9994 nbytes - i, len);
9995 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9996 ? ascii_keystroke
9997 : multibyte_char_keystroke);
9998 bufp->code = c;
9999 XSETFRAME (bufp->frame_or_window, f);
10000 bufp->arg = Qnil;
10001 bufp->modifiers
10002 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10003 modifiers);
10004 bufp->timestamp = event.xkey.time;
10005 bufp++;
10006 }
10007
10008 count += nchars;
10009 numchars -= nchars;
10010
10011 if (keysym == NoSymbol)
10012 break;
10013 }
10014 else
10015 abort ();
10016 }
10017 else
10018 abort ();
10019 }
10020 #ifdef HAVE_X_I18N
10021 /* Don't dispatch this event since XtDispatchEvent calls
10022 XFilterEvent, and two calls in a row may freeze the
10023 client. */
10024 break;
10025 #else
10026 goto OTHER;
10027 #endif
10028
10029 case KeyRelease:
10030 #ifdef HAVE_X_I18N
10031 /* Don't dispatch this event since XtDispatchEvent calls
10032 XFilterEvent, and two calls in a row may freeze the
10033 client. */
10034 break;
10035 #else
10036 goto OTHER;
10037 #endif
10038
10039 /* Here's a possible interpretation of the whole
10040 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10041 you get a FocusIn event, you have to get a FocusOut
10042 event before you relinquish the focus. If you
10043 haven't received a FocusIn event, then a mere
10044 LeaveNotify is enough to free you. */
10045
10046 case EnterNotify:
10047 {
10048 int from_menu_bar_p = 0;
10049
10050 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10051
10052 #ifdef LESSTIF_VERSION
10053 /* When clicking outside of a menu bar popup to close
10054 it, we get a FocusIn/ EnterNotify sequence of
10055 events. The flag event.xcrossing.focus is not set
10056 in the EnterNotify event of that sequence because
10057 the focus is in the menu bar,
10058 event.xcrossing.window is the frame's X window.
10059 Unconditionally setting the focus frame to null in
10060 this case is not the right thing, because no event
10061 follows that could set the focus frame to the right
10062 value.
10063
10064 This could be a LessTif bug, but I wasn't able to
10065 reproduce the behavior in a simple test program.
10066 On the other hand, Motif seems to not have this
10067 problem.
10068
10069 (gerd, LessTif 0.92). */
10070
10071 if (!event.xcrossing.focus
10072 && f
10073 && f->output_data.x->menubar_widget)
10074 {
10075 Window focus;
10076 int revert;
10077
10078 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10079 if (focus == XtWindow (f->output_data.x->menubar_widget))
10080 from_menu_bar_p = 1;
10081 }
10082 #endif /* LESSTIF_VERSION */
10083
10084 if (event.xcrossing.focus || from_menu_bar_p)
10085 {
10086 /* Avoid nasty pop/raise loops. */
10087 if (f && (!(f->auto_raise)
10088 || !(f->auto_lower)
10089 || (event.xcrossing.time - enter_timestamp) > 500))
10090 {
10091 x_new_focus_frame (dpyinfo, f);
10092 enter_timestamp = event.xcrossing.time;
10093 }
10094 }
10095 else if (f == dpyinfo->x_focus_frame)
10096 x_new_focus_frame (dpyinfo, 0);
10097
10098 /* EnterNotify counts as mouse movement,
10099 so update things that depend on mouse position. */
10100 if (f && !f->output_data.x->busy_p)
10101 note_mouse_movement (f, &event.xmotion);
10102 goto OTHER;
10103 }
10104
10105 case FocusIn:
10106 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10107 if (event.xfocus.detail != NotifyPointer)
10108 dpyinfo->x_focus_event_frame = f;
10109 if (f)
10110 {
10111 x_new_focus_frame (dpyinfo, f);
10112
10113 /* Don't stop displaying the initial startup message
10114 for a switch-frame event we don't need. */
10115 if (GC_NILP (Vterminal_frame)
10116 && GC_CONSP (Vframe_list)
10117 && !GC_NILP (XCDR (Vframe_list)))
10118 {
10119 bufp->kind = FOCUS_IN_EVENT;
10120 XSETFRAME (bufp->frame_or_window, f);
10121 bufp->arg = Qnil;
10122 ++bufp, ++count, --numchars;
10123 }
10124 }
10125
10126 #ifdef HAVE_X_I18N
10127 if (f && FRAME_XIC (f))
10128 XSetICFocus (FRAME_XIC (f));
10129 #endif
10130
10131 goto OTHER;
10132
10133 case LeaveNotify:
10134 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10135 if (f)
10136 {
10137 int from_menu_bar_p = 0;
10138
10139 if (f == dpyinfo->mouse_face_mouse_frame)
10140 {
10141 /* If we move outside the frame, then we're
10142 certainly no longer on any text in the frame. */
10143 clear_mouse_face (dpyinfo);
10144 dpyinfo->mouse_face_mouse_frame = 0;
10145 }
10146
10147 /* Generate a nil HELP_EVENT to cancel a help-echo.
10148 Do it only if there's something to cancel.
10149 Otherwise, the startup message is cleared when
10150 the mouse leaves the frame. */
10151 if (any_help_event_p)
10152 {
10153 Lisp_Object frame;
10154 int n;
10155
10156 XSETFRAME (frame, f);
10157 help_echo = Qnil;
10158 n = gen_help_event (bufp, numchars,
10159 Qnil, frame, Qnil, Qnil, 0);
10160 bufp += n, count += n, numchars -= n;
10161 }
10162
10163 #ifdef LESSTIF_VERSION
10164 /* Please see the comment at the start of the
10165 EnterNotify case. */
10166 if (!event.xcrossing.focus
10167 && f->output_data.x->menubar_widget)
10168 {
10169 Window focus;
10170 int revert;
10171 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10172 if (focus == XtWindow (f->output_data.x->menubar_widget))
10173 from_menu_bar_p = 1;
10174 }
10175 #endif /* LESSTIF_VERSION */
10176
10177 if (event.xcrossing.focus || from_menu_bar_p)
10178 x_mouse_leave (dpyinfo);
10179 else
10180 {
10181 if (f == dpyinfo->x_focus_event_frame)
10182 dpyinfo->x_focus_event_frame = 0;
10183 if (f == dpyinfo->x_focus_frame)
10184 x_new_focus_frame (dpyinfo, 0);
10185 }
10186 }
10187 goto OTHER;
10188
10189 case FocusOut:
10190 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10191 if (event.xfocus.detail != NotifyPointer
10192 && f == dpyinfo->x_focus_event_frame)
10193 dpyinfo->x_focus_event_frame = 0;
10194 if (f && f == dpyinfo->x_focus_frame)
10195 x_new_focus_frame (dpyinfo, 0);
10196
10197 #ifdef HAVE_X_I18N
10198 if (f && FRAME_XIC (f))
10199 XUnsetICFocus (FRAME_XIC (f));
10200 #endif
10201
10202 goto OTHER;
10203
10204 case MotionNotify:
10205 {
10206 previous_help_echo = help_echo;
10207 help_echo = help_echo_object = help_echo_window = Qnil;
10208 help_echo_pos = -1;
10209
10210 if (dpyinfo->grabbed && last_mouse_frame
10211 && FRAME_LIVE_P (last_mouse_frame))
10212 f = last_mouse_frame;
10213 else
10214 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10215
10216 if (f)
10217 note_mouse_movement (f, &event.xmotion);
10218 else
10219 {
10220 #ifndef USE_TOOLKIT_SCROLL_BARS
10221 struct scroll_bar *bar
10222 = x_window_to_scroll_bar (event.xmotion.window);
10223
10224 if (bar)
10225 x_scroll_bar_note_movement (bar, &event);
10226 #endif /* USE_TOOLKIT_SCROLL_BARS */
10227
10228 /* If we move outside the frame, then we're
10229 certainly no longer on any text in the frame. */
10230 clear_mouse_face (dpyinfo);
10231 }
10232
10233 /* If the contents of the global variable help_echo
10234 has changed, generate a HELP_EVENT. */
10235 if (!NILP (help_echo)
10236 || !NILP (previous_help_echo))
10237 {
10238 Lisp_Object frame;
10239 int n;
10240
10241 if (f)
10242 XSETFRAME (frame, f);
10243 else
10244 frame = Qnil;
10245
10246 any_help_event_p = 1;
10247 n = gen_help_event (bufp, numchars, help_echo, frame,
10248 help_echo_window, help_echo_object,
10249 help_echo_pos);
10250 bufp += n, count += n, numchars -= n;
10251 }
10252
10253 goto OTHER;
10254 }
10255
10256 case ConfigureNotify:
10257 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10258 if (f)
10259 {
10260 #ifndef USE_X_TOOLKIT
10261 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10262 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10263
10264 /* In the toolkit version, change_frame_size
10265 is called by the code that handles resizing
10266 of the EmacsFrame widget. */
10267
10268 /* Even if the number of character rows and columns has
10269 not changed, the font size may have changed, so we need
10270 to check the pixel dimensions as well. */
10271 if (columns != f->width
10272 || rows != f->height
10273 || event.xconfigure.width != f->output_data.x->pixel_width
10274 || event.xconfigure.height != f->output_data.x->pixel_height)
10275 {
10276 change_frame_size (f, rows, columns, 0, 1, 0);
10277 SET_FRAME_GARBAGED (f);
10278 cancel_mouse_face (f);
10279 }
10280 #endif
10281
10282 f->output_data.x->pixel_width = event.xconfigure.width;
10283 f->output_data.x->pixel_height = event.xconfigure.height;
10284
10285 /* What we have now is the position of Emacs's own window.
10286 Convert that to the position of the window manager window. */
10287 x_real_positions (f, &f->output_data.x->left_pos,
10288 &f->output_data.x->top_pos);
10289
10290 #ifdef HAVE_X_I18N
10291 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10292 xic_set_statusarea (f);
10293 #endif
10294
10295 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10296 {
10297 /* Since the WM decorations come below top_pos now,
10298 we must put them below top_pos in the future. */
10299 f->output_data.x->win_gravity = NorthWestGravity;
10300 x_wm_set_size_hint (f, (long) 0, 0);
10301 }
10302 #ifdef USE_MOTIF
10303 /* Some window managers pass (0,0) as the location of
10304 the window, and the Motif event handler stores it
10305 in the emacs widget, which messes up Motif menus. */
10306 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10307 {
10308 event.xconfigure.x = f->output_data.x->widget->core.x;
10309 event.xconfigure.y = f->output_data.x->widget->core.y;
10310 }
10311 #endif /* USE_MOTIF */
10312 }
10313 goto OTHER;
10314
10315 case ButtonPress:
10316 case ButtonRelease:
10317 {
10318 /* If we decide we want to generate an event to be seen
10319 by the rest of Emacs, we put it here. */
10320 struct input_event emacs_event;
10321 int tool_bar_p = 0;
10322
10323 emacs_event.kind = no_event;
10324 bzero (&compose_status, sizeof (compose_status));
10325
10326 if (dpyinfo->grabbed
10327 && last_mouse_frame
10328 && FRAME_LIVE_P (last_mouse_frame))
10329 f = last_mouse_frame;
10330 else
10331 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10332
10333 if (f)
10334 {
10335 /* Is this in the tool-bar? */
10336 if (WINDOWP (f->tool_bar_window)
10337 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10338 {
10339 Lisp_Object window;
10340 int p, x, y;
10341
10342 x = event.xbutton.x;
10343 y = event.xbutton.y;
10344
10345 /* Set x and y. */
10346 window = window_from_coordinates (f, x, y, &p, 1);
10347 if (EQ (window, f->tool_bar_window))
10348 {
10349 x_handle_tool_bar_click (f, &event.xbutton);
10350 tool_bar_p = 1;
10351 }
10352 }
10353
10354 if (!tool_bar_p)
10355 if (!dpyinfo->x_focus_frame
10356 || f == dpyinfo->x_focus_frame)
10357 construct_mouse_click (&emacs_event, &event, f);
10358 }
10359 else
10360 {
10361 #ifndef USE_TOOLKIT_SCROLL_BARS
10362 struct scroll_bar *bar
10363 = x_window_to_scroll_bar (event.xbutton.window);
10364
10365 if (bar)
10366 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10367 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10368 }
10369
10370 if (event.type == ButtonPress)
10371 {
10372 dpyinfo->grabbed |= (1 << event.xbutton.button);
10373 last_mouse_frame = f;
10374 /* Ignore any mouse motion that happened
10375 before this event; any subsequent mouse-movement
10376 Emacs events should reflect only motion after
10377 the ButtonPress. */
10378 if (f != 0)
10379 f->mouse_moved = 0;
10380
10381 if (!tool_bar_p)
10382 last_tool_bar_item = -1;
10383 }
10384 else
10385 {
10386 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10387 }
10388
10389 if (numchars >= 1 && emacs_event.kind != no_event)
10390 {
10391 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10392 bufp++;
10393 count++;
10394 numchars--;
10395 }
10396
10397 #ifdef USE_X_TOOLKIT
10398 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10399 /* For a down-event in the menu bar,
10400 don't pass it to Xt right now.
10401 Instead, save it away
10402 and we will pass it to Xt from kbd_buffer_get_event.
10403 That way, we can run some Lisp code first. */
10404 if (f && event.type == ButtonPress
10405 /* Verify the event is really within the menu bar
10406 and not just sent to it due to grabbing. */
10407 && event.xbutton.x >= 0
10408 && event.xbutton.x < f->output_data.x->pixel_width
10409 && event.xbutton.y >= 0
10410 && event.xbutton.y < f->output_data.x->menubar_height
10411 && event.xbutton.same_screen)
10412 {
10413 SET_SAVED_BUTTON_EVENT;
10414 XSETFRAME (last_mouse_press_frame, f);
10415 }
10416 else if (event.type == ButtonPress)
10417 {
10418 last_mouse_press_frame = Qnil;
10419 goto OTHER;
10420 }
10421
10422 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10423 but I am trying to be cautious. */
10424 else if (event.type == ButtonRelease)
10425 {
10426 if (!NILP (last_mouse_press_frame))
10427 {
10428 f = XFRAME (last_mouse_press_frame);
10429 if (f->output_data.x)
10430 SET_SAVED_BUTTON_EVENT;
10431 }
10432 else
10433 goto OTHER;
10434 }
10435 #endif /* USE_MOTIF */
10436 else
10437 goto OTHER;
10438 #endif /* USE_X_TOOLKIT */
10439 }
10440 break;
10441
10442 case CirculateNotify:
10443 goto OTHER;
10444
10445 case CirculateRequest:
10446 goto OTHER;
10447
10448 case VisibilityNotify:
10449 goto OTHER;
10450
10451 case MappingNotify:
10452 /* Someone has changed the keyboard mapping - update the
10453 local cache. */
10454 switch (event.xmapping.request)
10455 {
10456 case MappingModifier:
10457 x_find_modifier_meanings (dpyinfo);
10458 /* This is meant to fall through. */
10459 case MappingKeyboard:
10460 XRefreshKeyboardMapping (&event.xmapping);
10461 }
10462 goto OTHER;
10463
10464 default:
10465 OTHER:
10466 #ifdef USE_X_TOOLKIT
10467 BLOCK_INPUT;
10468 XtDispatchEvent (&event);
10469 UNBLOCK_INPUT;
10470 #endif /* USE_X_TOOLKIT */
10471 break;
10472 }
10473 }
10474 }
10475
10476 out:;
10477
10478 /* On some systems, an X bug causes Emacs to get no more events
10479 when the window is destroyed. Detect that. (1994.) */
10480 if (! event_found)
10481 {
10482 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10483 One XNOOP in 100 loops will make Emacs terminate.
10484 B. Bretthauer, 1994 */
10485 x_noop_count++;
10486 if (x_noop_count >= 100)
10487 {
10488 x_noop_count=0;
10489
10490 if (next_noop_dpyinfo == 0)
10491 next_noop_dpyinfo = x_display_list;
10492
10493 XNoOp (next_noop_dpyinfo->display);
10494
10495 /* Each time we get here, cycle through the displays now open. */
10496 next_noop_dpyinfo = next_noop_dpyinfo->next;
10497 }
10498 }
10499
10500 /* If the focus was just given to an auto-raising frame,
10501 raise it now. */
10502 /* ??? This ought to be able to handle more than one such frame. */
10503 if (pending_autoraise_frame)
10504 {
10505 x_raise_frame (pending_autoraise_frame);
10506 pending_autoraise_frame = 0;
10507 }
10508
10509 UNBLOCK_INPUT;
10510 --handling_signal;
10511 return count;
10512 }
10513
10514
10515
10516 \f
10517 /***********************************************************************
10518 Text Cursor
10519 ***********************************************************************/
10520
10521 /* Note if the text cursor of window W has been overwritten by a
10522 drawing operation that outputs N glyphs starting at HPOS in the
10523 line given by output_cursor.vpos. N < 0 means all the rest of the
10524 line after HPOS has been written. */
10525
10526 static void
10527 note_overwritten_text_cursor (w, hpos, n)
10528 struct window *w;
10529 int hpos, n;
10530 {
10531 if (updated_area == TEXT_AREA
10532 && output_cursor.vpos == w->phys_cursor.vpos
10533 && hpos <= w->phys_cursor.hpos
10534 && (n < 0
10535 || hpos + n > w->phys_cursor.hpos))
10536 w->phys_cursor_on_p = 0;
10537 }
10538
10539
10540 /* Set clipping for output in glyph row ROW. W is the window in which
10541 we operate. GC is the graphics context to set clipping in.
10542 WHOLE_LINE_P non-zero means include the areas used for truncation
10543 mark display and alike in the clipping rectangle.
10544
10545 ROW may be a text row or, e.g., a mode line. Text rows must be
10546 clipped to the interior of the window dedicated to text display,
10547 mode lines must be clipped to the whole window. */
10548
10549 static void
10550 x_clip_to_row (w, row, gc, whole_line_p)
10551 struct window *w;
10552 struct glyph_row *row;
10553 GC gc;
10554 int whole_line_p;
10555 {
10556 struct frame *f = XFRAME (WINDOW_FRAME (w));
10557 XRectangle clip_rect;
10558 int window_x, window_y, window_width, window_height;
10559
10560 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10561
10562 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10563 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10564 clip_rect.y = max (clip_rect.y, window_y);
10565 clip_rect.width = window_width;
10566 clip_rect.height = row->visible_height;
10567
10568 /* If clipping to the whole line, including trunc marks, extend
10569 the rectangle to the left and increase its width. */
10570 if (whole_line_p)
10571 {
10572 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10573 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10574 }
10575
10576 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10577 }
10578
10579
10580 /* Draw a hollow box cursor on window W in glyph row ROW. */
10581
10582 static void
10583 x_draw_hollow_cursor (w, row)
10584 struct window *w;
10585 struct glyph_row *row;
10586 {
10587 struct frame *f = XFRAME (WINDOW_FRAME (w));
10588 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10589 Display *dpy = FRAME_X_DISPLAY (f);
10590 int x, y, wd, h;
10591 XGCValues xgcv;
10592 struct glyph *cursor_glyph;
10593 GC gc;
10594
10595 /* Compute frame-relative coordinates from window-relative
10596 coordinates. */
10597 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10598 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10599 + row->ascent - w->phys_cursor_ascent);
10600 h = row->height - 1;
10601
10602 /* Get the glyph the cursor is on. If we can't tell because
10603 the current matrix is invalid or such, give up. */
10604 cursor_glyph = get_phys_cursor_glyph (w);
10605 if (cursor_glyph == NULL)
10606 return;
10607
10608 /* Compute the width of the rectangle to draw. If on a stretch
10609 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10610 rectangle as wide as the glyph, but use a canonical character
10611 width instead. */
10612 wd = cursor_glyph->pixel_width - 1;
10613 if (cursor_glyph->type == STRETCH_GLYPH
10614 && !x_stretch_cursor_p)
10615 wd = min (CANON_X_UNIT (f), wd);
10616
10617 /* The foreground of cursor_gc is typically the same as the normal
10618 background color, which can cause the cursor box to be invisible. */
10619 xgcv.foreground = f->output_data.x->cursor_pixel;
10620 if (dpyinfo->scratch_cursor_gc)
10621 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10622 else
10623 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10624 GCForeground, &xgcv);
10625 gc = dpyinfo->scratch_cursor_gc;
10626
10627 /* Set clipping, draw the rectangle, and reset clipping again. */
10628 x_clip_to_row (w, row, gc, 0);
10629 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10630 XSetClipMask (dpy, gc, None);
10631 }
10632
10633
10634 /* Draw a bar cursor on window W in glyph row ROW.
10635
10636 Implementation note: One would like to draw a bar cursor with an
10637 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10638 Unfortunately, I didn't find a font yet that has this property set.
10639 --gerd. */
10640
10641 static void
10642 x_draw_bar_cursor (w, row, width)
10643 struct window *w;
10644 struct glyph_row *row;
10645 int width;
10646 {
10647 struct frame *f = XFRAME (w->frame);
10648 struct glyph *cursor_glyph;
10649 GC gc;
10650 int x;
10651 unsigned long mask;
10652 XGCValues xgcv;
10653 Display *dpy;
10654 Window window;
10655
10656 /* If cursor is out of bounds, don't draw garbage. This can happen
10657 in mini-buffer windows when switching between echo area glyphs
10658 and mini-buffer. */
10659 cursor_glyph = get_phys_cursor_glyph (w);
10660 if (cursor_glyph == NULL)
10661 return;
10662
10663 /* If on an image, draw like a normal cursor. That's usually better
10664 visible than drawing a bar, esp. if the image is large so that
10665 the bar might not be in the window. */
10666 if (cursor_glyph->type == IMAGE_GLYPH)
10667 {
10668 struct glyph_row *row;
10669 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10670 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10671 }
10672 else
10673 {
10674 xgcv.background = f->output_data.x->cursor_pixel;
10675 xgcv.foreground = f->output_data.x->cursor_pixel;
10676 xgcv.graphics_exposures = 0;
10677 mask = GCForeground | GCBackground | GCGraphicsExposures;
10678 dpy = FRAME_X_DISPLAY (f);
10679 window = FRAME_X_WINDOW (f);
10680 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10681
10682 if (gc)
10683 XChangeGC (dpy, gc, mask, &xgcv);
10684 else
10685 {
10686 gc = XCreateGC (dpy, window, mask, &xgcv);
10687 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10688 }
10689
10690 if (width < 0)
10691 width = f->output_data.x->cursor_width;
10692
10693 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10694 x_clip_to_row (w, row, gc, 0);
10695 XFillRectangle (dpy, window, gc,
10696 x,
10697 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10698 min (cursor_glyph->pixel_width, width),
10699 row->height);
10700 XSetClipMask (dpy, gc, None);
10701 }
10702 }
10703
10704
10705 /* Clear the cursor of window W to background color, and mark the
10706 cursor as not shown. This is used when the text where the cursor
10707 is is about to be rewritten. */
10708
10709 static void
10710 x_clear_cursor (w)
10711 struct window *w;
10712 {
10713 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10714 x_update_window_cursor (w, 0);
10715 }
10716
10717
10718 /* Draw the cursor glyph of window W in glyph row ROW. See the
10719 comment of x_draw_glyphs for the meaning of HL. */
10720
10721 static void
10722 x_draw_phys_cursor_glyph (w, row, hl)
10723 struct window *w;
10724 struct glyph_row *row;
10725 enum draw_glyphs_face hl;
10726 {
10727 /* If cursor hpos is out of bounds, don't draw garbage. This can
10728 happen in mini-buffer windows when switching between echo area
10729 glyphs and mini-buffer. */
10730 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10731 {
10732 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10733 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10734 hl, 0, 0, 0);
10735
10736 /* When we erase the cursor, and ROW is overlapped by other
10737 rows, make sure that these overlapping parts of other rows
10738 are redrawn. */
10739 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10740 {
10741 if (row > w->current_matrix->rows
10742 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10743 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10744
10745 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10746 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10747 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10748 }
10749 }
10750 }
10751
10752
10753 /* Erase the image of a cursor of window W from the screen. */
10754
10755 static void
10756 x_erase_phys_cursor (w)
10757 struct window *w;
10758 {
10759 struct frame *f = XFRAME (w->frame);
10760 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10761 int hpos = w->phys_cursor.hpos;
10762 int vpos = w->phys_cursor.vpos;
10763 int mouse_face_here_p = 0;
10764 struct glyph_matrix *active_glyphs = w->current_matrix;
10765 struct glyph_row *cursor_row;
10766 struct glyph *cursor_glyph;
10767 enum draw_glyphs_face hl;
10768
10769 /* No cursor displayed or row invalidated => nothing to do on the
10770 screen. */
10771 if (w->phys_cursor_type == NO_CURSOR)
10772 goto mark_cursor_off;
10773
10774 /* VPOS >= active_glyphs->nrows means that window has been resized.
10775 Don't bother to erase the cursor. */
10776 if (vpos >= active_glyphs->nrows)
10777 goto mark_cursor_off;
10778
10779 /* If row containing cursor is marked invalid, there is nothing we
10780 can do. */
10781 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10782 if (!cursor_row->enabled_p)
10783 goto mark_cursor_off;
10784
10785 /* This can happen when the new row is shorter than the old one.
10786 In this case, either x_draw_glyphs or clear_end_of_line
10787 should have cleared the cursor. Note that we wouldn't be
10788 able to erase the cursor in this case because we don't have a
10789 cursor glyph at hand. */
10790 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10791 goto mark_cursor_off;
10792
10793 /* If the cursor is in the mouse face area, redisplay that when
10794 we clear the cursor. */
10795 if (! NILP (dpyinfo->mouse_face_window)
10796 && w == XWINDOW (dpyinfo->mouse_face_window)
10797 && (vpos > dpyinfo->mouse_face_beg_row
10798 || (vpos == dpyinfo->mouse_face_beg_row
10799 && hpos >= dpyinfo->mouse_face_beg_col))
10800 && (vpos < dpyinfo->mouse_face_end_row
10801 || (vpos == dpyinfo->mouse_face_end_row
10802 && hpos < dpyinfo->mouse_face_end_col))
10803 /* Don't redraw the cursor's spot in mouse face if it is at the
10804 end of a line (on a newline). The cursor appears there, but
10805 mouse highlighting does not. */
10806 && cursor_row->used[TEXT_AREA] > hpos)
10807 mouse_face_here_p = 1;
10808
10809 /* Maybe clear the display under the cursor. */
10810 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10811 {
10812 int x;
10813 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10814
10815 cursor_glyph = get_phys_cursor_glyph (w);
10816 if (cursor_glyph == NULL)
10817 goto mark_cursor_off;
10818
10819 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10820
10821 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10822 x,
10823 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10824 cursor_row->y)),
10825 cursor_glyph->pixel_width,
10826 cursor_row->visible_height,
10827 False);
10828 }
10829
10830 /* Erase the cursor by redrawing the character underneath it. */
10831 if (mouse_face_here_p)
10832 hl = DRAW_MOUSE_FACE;
10833 else if (cursor_row->inverse_p)
10834 hl = DRAW_INVERSE_VIDEO;
10835 else
10836 hl = DRAW_NORMAL_TEXT;
10837 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10838
10839 mark_cursor_off:
10840 w->phys_cursor_on_p = 0;
10841 w->phys_cursor_type = NO_CURSOR;
10842 }
10843
10844
10845 /* Display or clear cursor of window W. If ON is zero, clear the
10846 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10847 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10848
10849 void
10850 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10851 struct window *w;
10852 int on, hpos, vpos, x, y;
10853 {
10854 struct frame *f = XFRAME (w->frame);
10855 int new_cursor_type;
10856 int new_cursor_width;
10857 struct glyph_matrix *current_glyphs;
10858 struct glyph_row *glyph_row;
10859 struct glyph *glyph;
10860
10861 /* This is pointless on invisible frames, and dangerous on garbaged
10862 windows and frames; in the latter case, the frame or window may
10863 be in the midst of changing its size, and x and y may be off the
10864 window. */
10865 if (! FRAME_VISIBLE_P (f)
10866 || FRAME_GARBAGED_P (f)
10867 || vpos >= w->current_matrix->nrows
10868 || hpos >= w->current_matrix->matrix_w)
10869 return;
10870
10871 /* If cursor is off and we want it off, return quickly. */
10872 if (!on && !w->phys_cursor_on_p)
10873 return;
10874
10875 current_glyphs = w->current_matrix;
10876 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10877 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10878
10879 /* If cursor row is not enabled, we don't really know where to
10880 display the cursor. */
10881 if (!glyph_row->enabled_p)
10882 {
10883 w->phys_cursor_on_p = 0;
10884 return;
10885 }
10886
10887 xassert (interrupt_input_blocked);
10888
10889 /* Set new_cursor_type to the cursor we want to be displayed. In a
10890 mini-buffer window, we want the cursor only to appear if we are
10891 reading input from this window. For the selected window, we want
10892 the cursor type given by the frame parameter. If explicitly
10893 marked off, draw no cursor. In all other cases, we want a hollow
10894 box cursor. */
10895 new_cursor_width = -1;
10896 if (cursor_in_echo_area
10897 && FRAME_HAS_MINIBUF_P (f)
10898 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10899 {
10900 if (w == XWINDOW (echo_area_window))
10901 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10902 else
10903 new_cursor_type = HOLLOW_BOX_CURSOR;
10904 }
10905 else
10906 {
10907 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
10908 || w != XWINDOW (f->selected_window))
10909 {
10910 extern int cursor_in_non_selected_windows;
10911
10912 if (MINI_WINDOW_P (w)
10913 || !cursor_in_non_selected_windows
10914 || NILP (XBUFFER (w->buffer)->cursor_type))
10915 new_cursor_type = NO_CURSOR;
10916 else
10917 new_cursor_type = HOLLOW_BOX_CURSOR;
10918 }
10919 else if (w->cursor_off_p)
10920 new_cursor_type = NO_CURSOR;
10921 else
10922 {
10923 struct buffer *b = XBUFFER (w->buffer);
10924
10925 if (EQ (b->cursor_type, Qt))
10926 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10927 else
10928 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10929 &new_cursor_width);
10930 }
10931 }
10932
10933 /* If cursor is currently being shown and we don't want it to be or
10934 it is in the wrong place, or the cursor type is not what we want,
10935 erase it. */
10936 if (w->phys_cursor_on_p
10937 && (!on
10938 || w->phys_cursor.x != x
10939 || w->phys_cursor.y != y
10940 || new_cursor_type != w->phys_cursor_type))
10941 x_erase_phys_cursor (w);
10942
10943 /* If the cursor is now invisible and we want it to be visible,
10944 display it. */
10945 if (on && !w->phys_cursor_on_p)
10946 {
10947 w->phys_cursor_ascent = glyph_row->ascent;
10948 w->phys_cursor_height = glyph_row->height;
10949
10950 /* Set phys_cursor_.* before x_draw_.* is called because some
10951 of them may need the information. */
10952 w->phys_cursor.x = x;
10953 w->phys_cursor.y = glyph_row->y;
10954 w->phys_cursor.hpos = hpos;
10955 w->phys_cursor.vpos = vpos;
10956 w->phys_cursor_type = new_cursor_type;
10957 w->phys_cursor_on_p = 1;
10958
10959 switch (new_cursor_type)
10960 {
10961 case HOLLOW_BOX_CURSOR:
10962 x_draw_hollow_cursor (w, glyph_row);
10963 break;
10964
10965 case FILLED_BOX_CURSOR:
10966 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10967 break;
10968
10969 case BAR_CURSOR:
10970 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10971 break;
10972
10973 case NO_CURSOR:
10974 break;
10975
10976 default:
10977 abort ();
10978 }
10979
10980 #ifdef HAVE_X_I18N
10981 if (w == XWINDOW (f->selected_window))
10982 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10983 xic_set_preeditarea (w, x, y);
10984 #endif
10985 }
10986
10987 #ifndef XFlush
10988 if (updating_frame != f)
10989 XFlush (FRAME_X_DISPLAY (f));
10990 #endif
10991 }
10992
10993
10994 /* Display the cursor on window W, or clear it. X and Y are window
10995 relative pixel coordinates. HPOS and VPOS are glyph matrix
10996 positions. If W is not the selected window, display a hollow
10997 cursor. ON non-zero means display the cursor at X, Y which
10998 correspond to HPOS, VPOS, otherwise it is cleared. */
10999
11000 void
11001 x_display_cursor (w, on, hpos, vpos, x, y)
11002 struct window *w;
11003 int on, hpos, vpos, x, y;
11004 {
11005 BLOCK_INPUT;
11006 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11007 UNBLOCK_INPUT;
11008 }
11009
11010
11011 /* Display the cursor on window W, or clear it, according to ON_P.
11012 Don't change the cursor's position. */
11013
11014 void
11015 x_update_cursor (f, on_p)
11016 struct frame *f;
11017 {
11018 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11019 }
11020
11021
11022 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11023 in the window tree rooted at W. */
11024
11025 static void
11026 x_update_cursor_in_window_tree (w, on_p)
11027 struct window *w;
11028 int on_p;
11029 {
11030 while (w)
11031 {
11032 if (!NILP (w->hchild))
11033 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11034 else if (!NILP (w->vchild))
11035 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11036 else
11037 x_update_window_cursor (w, on_p);
11038
11039 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11040 }
11041 }
11042
11043
11044 /* Switch the display of W's cursor on or off, according to the value
11045 of ON. */
11046
11047 static void
11048 x_update_window_cursor (w, on)
11049 struct window *w;
11050 int on;
11051 {
11052 /* Don't update cursor in windows whose frame is in the process
11053 of being deleted. */
11054 if (w->current_matrix)
11055 {
11056 BLOCK_INPUT;
11057 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11058 w->phys_cursor.x, w->phys_cursor.y);
11059 UNBLOCK_INPUT;
11060 }
11061 }
11062
11063
11064
11065 \f
11066 /* Icons. */
11067
11068 /* Refresh bitmap kitchen sink icon for frame F
11069 when we get an expose event for it. */
11070
11071 void
11072 refreshicon (f)
11073 struct frame *f;
11074 {
11075 /* Normally, the window manager handles this function. */
11076 }
11077
11078 /* Make the x-window of frame F use the gnu icon bitmap. */
11079
11080 int
11081 x_bitmap_icon (f, file)
11082 struct frame *f;
11083 Lisp_Object file;
11084 {
11085 int bitmap_id;
11086
11087 if (FRAME_X_WINDOW (f) == 0)
11088 return 1;
11089
11090 /* Free up our existing icon bitmap if any. */
11091 if (f->output_data.x->icon_bitmap > 0)
11092 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11093 f->output_data.x->icon_bitmap = 0;
11094
11095 if (STRINGP (file))
11096 bitmap_id = x_create_bitmap_from_file (f, file);
11097 else
11098 {
11099 /* Create the GNU bitmap if necessary. */
11100 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11101 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11102 = x_create_bitmap_from_data (f, gnu_bits,
11103 gnu_width, gnu_height);
11104
11105 /* The first time we create the GNU bitmap,
11106 this increments the ref-count one extra time.
11107 As a result, the GNU bitmap is never freed.
11108 That way, we don't have to worry about allocating it again. */
11109 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11110
11111 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11112 }
11113
11114 x_wm_set_icon_pixmap (f, bitmap_id);
11115 f->output_data.x->icon_bitmap = bitmap_id;
11116
11117 return 0;
11118 }
11119
11120
11121 /* Make the x-window of frame F use a rectangle with text.
11122 Use ICON_NAME as the text. */
11123
11124 int
11125 x_text_icon (f, icon_name)
11126 struct frame *f;
11127 char *icon_name;
11128 {
11129 if (FRAME_X_WINDOW (f) == 0)
11130 return 1;
11131
11132 #ifdef HAVE_X11R4
11133 {
11134 XTextProperty text;
11135 text.value = (unsigned char *) icon_name;
11136 text.encoding = XA_STRING;
11137 text.format = 8;
11138 text.nitems = strlen (icon_name);
11139 #ifdef USE_X_TOOLKIT
11140 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11141 &text);
11142 #else /* not USE_X_TOOLKIT */
11143 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11144 #endif /* not USE_X_TOOLKIT */
11145 }
11146 #else /* not HAVE_X11R4 */
11147 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11148 #endif /* not HAVE_X11R4 */
11149
11150 if (f->output_data.x->icon_bitmap > 0)
11151 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11152 f->output_data.x->icon_bitmap = 0;
11153 x_wm_set_icon_pixmap (f, 0);
11154
11155 return 0;
11156 }
11157 \f
11158 #define X_ERROR_MESSAGE_SIZE 200
11159
11160 /* If non-nil, this should be a string.
11161 It means catch X errors and store the error message in this string. */
11162
11163 static Lisp_Object x_error_message_string;
11164
11165 /* An X error handler which stores the error message in
11166 x_error_message_string. This is called from x_error_handler if
11167 x_catch_errors is in effect. */
11168
11169 static void
11170 x_error_catcher (display, error)
11171 Display *display;
11172 XErrorEvent *error;
11173 {
11174 XGetErrorText (display, error->error_code,
11175 XSTRING (x_error_message_string)->data,
11176 X_ERROR_MESSAGE_SIZE);
11177 }
11178
11179 /* Begin trapping X errors for display DPY. Actually we trap X errors
11180 for all displays, but DPY should be the display you are actually
11181 operating on.
11182
11183 After calling this function, X protocol errors no longer cause
11184 Emacs to exit; instead, they are recorded in the string
11185 stored in x_error_message_string.
11186
11187 Calling x_check_errors signals an Emacs error if an X error has
11188 occurred since the last call to x_catch_errors or x_check_errors.
11189
11190 Calling x_uncatch_errors resumes the normal error handling. */
11191
11192 void x_check_errors ();
11193 static Lisp_Object x_catch_errors_unwind ();
11194
11195 int
11196 x_catch_errors (dpy)
11197 Display *dpy;
11198 {
11199 int count = specpdl_ptr - specpdl;
11200
11201 /* Make sure any errors from previous requests have been dealt with. */
11202 XSync (dpy, False);
11203
11204 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11205
11206 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11207 XSTRING (x_error_message_string)->data[0] = 0;
11208
11209 return count;
11210 }
11211
11212 /* Unbind the binding that we made to check for X errors. */
11213
11214 static Lisp_Object
11215 x_catch_errors_unwind (old_val)
11216 Lisp_Object old_val;
11217 {
11218 x_error_message_string = old_val;
11219 return Qnil;
11220 }
11221
11222 /* If any X protocol errors have arrived since the last call to
11223 x_catch_errors or x_check_errors, signal an Emacs error using
11224 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11225
11226 void
11227 x_check_errors (dpy, format)
11228 Display *dpy;
11229 char *format;
11230 {
11231 /* Make sure to catch any errors incurred so far. */
11232 XSync (dpy, False);
11233
11234 if (XSTRING (x_error_message_string)->data[0])
11235 error (format, XSTRING (x_error_message_string)->data);
11236 }
11237
11238 /* Nonzero if we had any X protocol errors
11239 since we did x_catch_errors on DPY. */
11240
11241 int
11242 x_had_errors_p (dpy)
11243 Display *dpy;
11244 {
11245 /* Make sure to catch any errors incurred so far. */
11246 XSync (dpy, False);
11247
11248 return XSTRING (x_error_message_string)->data[0] != 0;
11249 }
11250
11251 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11252
11253 void
11254 x_clear_errors (dpy)
11255 Display *dpy;
11256 {
11257 XSTRING (x_error_message_string)->data[0] = 0;
11258 }
11259
11260 /* Stop catching X protocol errors and let them make Emacs die.
11261 DPY should be the display that was passed to x_catch_errors.
11262 COUNT should be the value that was returned by
11263 the corresponding call to x_catch_errors. */
11264
11265 void
11266 x_uncatch_errors (dpy, count)
11267 Display *dpy;
11268 int count;
11269 {
11270 unbind_to (count, Qnil);
11271 }
11272
11273 #if 0
11274 static unsigned int x_wire_count;
11275 x_trace_wire ()
11276 {
11277 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11278 }
11279 #endif /* ! 0 */
11280
11281 \f
11282 /* Handle SIGPIPE, which can happen when the connection to a server
11283 simply goes away. SIGPIPE is handled by x_connection_signal.
11284 Don't need to do anything, because the write which caused the
11285 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11286 which will do the appropriate cleanup for us. */
11287
11288 static SIGTYPE
11289 x_connection_signal (signalnum) /* If we don't have an argument, */
11290 int signalnum; /* some compilers complain in signal calls. */
11291 {
11292 #ifdef USG
11293 /* USG systems forget handlers when they are used;
11294 must reestablish each time */
11295 signal (signalnum, x_connection_signal);
11296 #endif /* USG */
11297 }
11298
11299 \f
11300 /************************************************************************
11301 Handling X errors
11302 ************************************************************************/
11303
11304 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11305 the text of an error message that lead to the connection loss. */
11306
11307 static SIGTYPE
11308 x_connection_closed (dpy, error_message)
11309 Display *dpy;
11310 char *error_message;
11311 {
11312 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11313 Lisp_Object frame, tail;
11314 int count;
11315 char *msg;
11316
11317 msg = (char *) alloca (strlen (error_message) + 1);
11318 strcpy (msg, error_message);
11319 handling_signal = 0;
11320
11321 /* Prevent being called recursively because of an error condition
11322 below. Otherwise, we might end up with printing ``can't find per
11323 display information'' in the recursive call instead of printing
11324 the original message here. */
11325 count = x_catch_errors (dpy);
11326
11327 /* We have to close the display to inform Xt that it doesn't
11328 exist anymore. If we don't, Xt will continue to wait for
11329 events from the display. As a consequence, a sequence of
11330
11331 M-x make-frame-on-display RET :1 RET
11332 ...kill the new frame, so that we get an IO error...
11333 M-x make-frame-on-display RET :1 RET
11334
11335 will indefinitely wait in Xt for events for display `:1', opened
11336 in the first class to make-frame-on-display.
11337
11338 Closing the display is reported to lead to a bus error on
11339 OpenWindows in certain situations. I suspect that is a bug
11340 in OpenWindows. I don't know how to cicumvent it here. */
11341
11342 #ifdef USE_X_TOOLKIT
11343 /* If DPYINFO is null, this means we didn't open the display
11344 in the first place, so don't try to close it. */
11345 if (dpyinfo)
11346 XtCloseDisplay (dpy);
11347 #endif
11348
11349 /* Indicate that this display is dead. */
11350 if (dpyinfo)
11351 dpyinfo->display = 0;
11352
11353 /* First delete frames whose mini-buffers are on frames
11354 that are on the dead display. */
11355 FOR_EACH_FRAME (tail, frame)
11356 {
11357 Lisp_Object minibuf_frame;
11358 minibuf_frame
11359 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11360 if (FRAME_X_P (XFRAME (frame))
11361 && FRAME_X_P (XFRAME (minibuf_frame))
11362 && ! EQ (frame, minibuf_frame)
11363 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11364 Fdelete_frame (frame, Qt);
11365 }
11366
11367 /* Now delete all remaining frames on the dead display.
11368 We are now sure none of these is used as the mini-buffer
11369 for another frame that we need to delete. */
11370 FOR_EACH_FRAME (tail, frame)
11371 if (FRAME_X_P (XFRAME (frame))
11372 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11373 {
11374 /* Set this to t so that Fdelete_frame won't get confused
11375 trying to find a replacement. */
11376 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11377 Fdelete_frame (frame, Qt);
11378 }
11379
11380 if (dpyinfo)
11381 x_delete_display (dpyinfo);
11382
11383 x_uncatch_errors (dpy, count);
11384
11385 if (x_display_list == 0)
11386 {
11387 fprintf (stderr, "%s\n", msg);
11388 shut_down_emacs (0, 0, Qnil);
11389 exit (70);
11390 }
11391
11392 /* Ordinary stack unwind doesn't deal with these. */
11393 #ifdef SIGIO
11394 sigunblock (sigmask (SIGIO));
11395 #endif
11396 sigunblock (sigmask (SIGALRM));
11397 TOTALLY_UNBLOCK_INPUT;
11398
11399 clear_waiting_for_input ();
11400 error ("%s", msg);
11401 }
11402
11403
11404 /* This is the usual handler for X protocol errors.
11405 It kills all frames on the display that we got the error for.
11406 If that was the only one, it prints an error message and kills Emacs. */
11407
11408 static void
11409 x_error_quitter (display, error)
11410 Display *display;
11411 XErrorEvent *error;
11412 {
11413 char buf[256], buf1[356];
11414
11415 /* Note that there is no real way portable across R3/R4 to get the
11416 original error handler. */
11417
11418 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11419 sprintf (buf1, "X protocol error: %s on protocol request %d",
11420 buf, error->request_code);
11421 x_connection_closed (display, buf1);
11422 }
11423
11424
11425 /* This is the first-level handler for X protocol errors.
11426 It calls x_error_quitter or x_error_catcher. */
11427
11428 static int
11429 x_error_handler (display, error)
11430 Display *display;
11431 XErrorEvent *error;
11432 {
11433 if (! NILP (x_error_message_string))
11434 x_error_catcher (display, error);
11435 else
11436 x_error_quitter (display, error);
11437 return 0;
11438 }
11439
11440 /* This is the handler for X IO errors, always.
11441 It kills all frames on the display that we lost touch with.
11442 If that was the only one, it prints an error message and kills Emacs. */
11443
11444 static int
11445 x_io_error_quitter (display)
11446 Display *display;
11447 {
11448 char buf[256];
11449
11450 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11451 x_connection_closed (display, buf);
11452 return 0;
11453 }
11454 \f
11455 /* Changing the font of the frame. */
11456
11457 /* Give frame F the font named FONTNAME as its default font, and
11458 return the full name of that font. FONTNAME may be a wildcard
11459 pattern; in that case, we choose some font that fits the pattern.
11460 The return value shows which font we chose. */
11461
11462 Lisp_Object
11463 x_new_font (f, fontname)
11464 struct frame *f;
11465 register char *fontname;
11466 {
11467 struct font_info *fontp
11468 = FS_LOAD_FONT (f, 0, fontname, -1);
11469
11470 if (!fontp)
11471 return Qnil;
11472
11473 f->output_data.x->font = (XFontStruct *) (fontp->font);
11474 f->output_data.x->baseline_offset = fontp->baseline_offset;
11475 f->output_data.x->fontset = -1;
11476
11477 /* Compute the scroll bar width in character columns. */
11478 if (f->scroll_bar_pixel_width > 0)
11479 {
11480 int wid = FONT_WIDTH (f->output_data.x->font);
11481 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11482 }
11483 else
11484 {
11485 int wid = FONT_WIDTH (f->output_data.x->font);
11486 f->scroll_bar_cols = (14 + wid - 1) / wid;
11487 }
11488
11489 /* Now make the frame display the given font. */
11490 if (FRAME_X_WINDOW (f) != 0)
11491 {
11492 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11493 f->output_data.x->font->fid);
11494 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11495 f->output_data.x->font->fid);
11496 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11497 f->output_data.x->font->fid);
11498
11499 frame_update_line_height (f);
11500
11501 /* Don't change the size of a tip frame; there's no point in
11502 doing it because it's done in Fx_show_tip, and it leads to
11503 problems because the tip frame has no widget. */
11504 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11505 x_set_window_size (f, 0, f->width, f->height);
11506 }
11507 else
11508 /* If we are setting a new frame's font for the first time,
11509 there are no faces yet, so this font's height is the line height. */
11510 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11511
11512 return build_string (fontp->full_name);
11513 }
11514
11515 /* Give frame F the fontset named FONTSETNAME as its default font, and
11516 return the full name of that fontset. FONTSETNAME may be a wildcard
11517 pattern; in that case, we choose some fontset that fits the pattern.
11518 The return value shows which fontset we chose. */
11519
11520 Lisp_Object
11521 x_new_fontset (f, fontsetname)
11522 struct frame *f;
11523 char *fontsetname;
11524 {
11525 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11526 Lisp_Object result;
11527
11528 if (fontset < 0)
11529 return Qnil;
11530
11531 if (f->output_data.x->fontset == fontset)
11532 /* This fontset is already set in frame F. There's nothing more
11533 to do. */
11534 return fontset_name (fontset);
11535
11536 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11537
11538 if (!STRINGP (result))
11539 /* Can't load ASCII font. */
11540 return Qnil;
11541
11542 /* Since x_new_font doesn't update any fontset information, do it now. */
11543 f->output_data.x->fontset = fontset;
11544
11545 #ifdef HAVE_X_I18N
11546 if (FRAME_XIC (f)
11547 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11548 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11549 #endif
11550
11551 return build_string (fontsetname);
11552 }
11553
11554 \f
11555 /***********************************************************************
11556 X Input Methods
11557 ***********************************************************************/
11558
11559 #ifdef HAVE_X_I18N
11560
11561 #ifdef HAVE_X11R6
11562
11563 /* XIM destroy callback function, which is called whenever the
11564 connection to input method XIM dies. CLIENT_DATA contains a
11565 pointer to the x_display_info structure corresponding to XIM. */
11566
11567 static void
11568 xim_destroy_callback (xim, client_data, call_data)
11569 XIM xim;
11570 XPointer client_data;
11571 XPointer call_data;
11572 {
11573 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11574 Lisp_Object frame, tail;
11575
11576 BLOCK_INPUT;
11577
11578 /* No need to call XDestroyIC.. */
11579 FOR_EACH_FRAME (tail, frame)
11580 {
11581 struct frame *f = XFRAME (frame);
11582 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11583 {
11584 FRAME_XIC (f) = NULL;
11585 if (FRAME_XIC_FONTSET (f))
11586 {
11587 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11588 FRAME_XIC_FONTSET (f) = NULL;
11589 }
11590 }
11591 }
11592
11593 /* No need to call XCloseIM. */
11594 dpyinfo->xim = NULL;
11595 XFree (dpyinfo->xim_styles);
11596 UNBLOCK_INPUT;
11597 }
11598
11599 #endif /* HAVE_X11R6 */
11600
11601 /* Open the connection to the XIM server on display DPYINFO.
11602 RESOURCE_NAME is the resource name Emacs uses. */
11603
11604 static void
11605 xim_open_dpy (dpyinfo, resource_name)
11606 struct x_display_info *dpyinfo;
11607 char *resource_name;
11608 {
11609 #ifdef USE_XIM
11610 XIM xim;
11611
11612 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11613 dpyinfo->xim = xim;
11614
11615 if (xim)
11616 {
11617 #ifdef HAVE_X11R6
11618 XIMCallback destroy;
11619 #endif
11620
11621 /* Get supported styles and XIM values. */
11622 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11623
11624 #ifdef HAVE_X11R6
11625 destroy.callback = xim_destroy_callback;
11626 destroy.client_data = (XPointer)dpyinfo;
11627 /* This isn't prptotyped in OSF 5.0. */
11628 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11629 #endif
11630 }
11631
11632 #else /* not USE_XIM */
11633 dpyinfo->xim = NULL;
11634 #endif /* not USE_XIM */
11635 }
11636
11637
11638 #ifdef HAVE_X11R6_XIM
11639
11640 struct xim_inst_t
11641 {
11642 struct x_display_info *dpyinfo;
11643 char *resource_name;
11644 };
11645
11646 /* XIM instantiate callback function, which is called whenever an XIM
11647 server is available. DISPLAY is teh display of the XIM.
11648 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11649 when the callback was registered. */
11650
11651 static void
11652 xim_instantiate_callback (display, client_data, call_data)
11653 Display *display;
11654 XPointer client_data;
11655 XPointer call_data;
11656 {
11657 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11658 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11659
11660 /* We don't support multiple XIM connections. */
11661 if (dpyinfo->xim)
11662 return;
11663
11664 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11665
11666 /* Create XIC for the existing frames on the same display, as long
11667 as they have no XIC. */
11668 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11669 {
11670 Lisp_Object tail, frame;
11671
11672 BLOCK_INPUT;
11673 FOR_EACH_FRAME (tail, frame)
11674 {
11675 struct frame *f = XFRAME (frame);
11676
11677 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11678 if (FRAME_XIC (f) == NULL)
11679 {
11680 create_frame_xic (f);
11681 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11682 xic_set_statusarea (f);
11683 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11684 {
11685 struct window *w = XWINDOW (f->selected_window);
11686 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11687 }
11688 }
11689 }
11690
11691 UNBLOCK_INPUT;
11692 }
11693 }
11694
11695 #endif /* HAVE_X11R6_XIM */
11696
11697
11698 /* Open a connection to the XIM server on display DPYINFO.
11699 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11700 connection only at the first time. On X11R6, open the connection
11701 in the XIM instantiate callback function. */
11702
11703 static void
11704 xim_initialize (dpyinfo, resource_name)
11705 struct x_display_info *dpyinfo;
11706 char *resource_name;
11707 {
11708 #ifdef USE_XIM
11709 #ifdef HAVE_X11R6_XIM
11710 struct xim_inst_t *xim_inst;
11711 int len;
11712
11713 dpyinfo->xim = NULL;
11714 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11715 xim_inst->dpyinfo = dpyinfo;
11716 len = strlen (resource_name);
11717 xim_inst->resource_name = (char *) xmalloc (len + 1);
11718 bcopy (resource_name, xim_inst->resource_name, len + 1);
11719 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11720 resource_name, EMACS_CLASS,
11721 xim_instantiate_callback,
11722 /* Fixme: This is XPointer in
11723 XFree86 but (XPointer *) on
11724 Tru64, at least. */
11725 (XPointer) xim_inst);
11726 #else /* not HAVE_X11R6_XIM */
11727 dpyinfo->xim = NULL;
11728 xim_open_dpy (dpyinfo, resource_name);
11729 #endif /* not HAVE_X11R6_XIM */
11730
11731 #else /* not USE_XIM */
11732 dpyinfo->xim = NULL;
11733 #endif /* not USE_XIM */
11734 }
11735
11736
11737 /* Close the connection to the XIM server on display DPYINFO. */
11738
11739 static void
11740 xim_close_dpy (dpyinfo)
11741 struct x_display_info *dpyinfo;
11742 {
11743 #ifdef USE_XIM
11744 #ifdef HAVE_X11R6_XIM
11745 if (dpyinfo->display)
11746 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11747 NULL, EMACS_CLASS,
11748 xim_instantiate_callback, NULL);
11749 #endif /* not HAVE_X11R6_XIM */
11750 if (dpyinfo->display)
11751 XCloseIM (dpyinfo->xim);
11752 dpyinfo->xim = NULL;
11753 XFree (dpyinfo->xim_styles);
11754 #endif /* USE_XIM */
11755 }
11756
11757 #endif /* not HAVE_X11R6_XIM */
11758
11759
11760 \f
11761 /* Calculate the absolute position in frame F
11762 from its current recorded position values and gravity. */
11763
11764 void
11765 x_calc_absolute_position (f)
11766 struct frame *f;
11767 {
11768 Window child;
11769 int win_x = 0, win_y = 0;
11770 int flags = f->output_data.x->size_hint_flags;
11771 int this_window;
11772
11773 /* We have nothing to do if the current position
11774 is already for the top-left corner. */
11775 if (! ((flags & XNegative) || (flags & YNegative)))
11776 return;
11777
11778 #ifdef USE_X_TOOLKIT
11779 this_window = XtWindow (f->output_data.x->widget);
11780 #else
11781 this_window = FRAME_X_WINDOW (f);
11782 #endif
11783
11784 /* Find the position of the outside upper-left corner of
11785 the inner window, with respect to the outer window.
11786 But do this only if we will need the results. */
11787 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11788 {
11789 int count;
11790
11791 BLOCK_INPUT;
11792 count = x_catch_errors (FRAME_X_DISPLAY (f));
11793 while (1)
11794 {
11795 x_clear_errors (FRAME_X_DISPLAY (f));
11796 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11797
11798 /* From-window, to-window. */
11799 this_window,
11800 f->output_data.x->parent_desc,
11801
11802 /* From-position, to-position. */
11803 0, 0, &win_x, &win_y,
11804
11805 /* Child of win. */
11806 &child);
11807 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11808 {
11809 Window newroot, newparent = 0xdeadbeef;
11810 Window *newchildren;
11811 unsigned int nchildren;
11812
11813 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11814 &newparent, &newchildren, &nchildren))
11815 break;
11816
11817 XFree ((char *) newchildren);
11818
11819 f->output_data.x->parent_desc = newparent;
11820 }
11821 else
11822 break;
11823 }
11824
11825 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11826 UNBLOCK_INPUT;
11827 }
11828
11829 /* Treat negative positions as relative to the leftmost bottommost
11830 position that fits on the screen. */
11831 if (flags & XNegative)
11832 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11833 - 2 * f->output_data.x->border_width - win_x
11834 - PIXEL_WIDTH (f)
11835 + f->output_data.x->left_pos);
11836
11837 {
11838 int height = PIXEL_HEIGHT (f);
11839
11840 #if defined USE_X_TOOLKIT && defined USE_MOTIF
11841 /* Something is fishy here. When using Motif, starting Emacs with
11842 `-g -0-0', the frame appears too low by a few pixels.
11843
11844 This seems to be so because initially, while Emacs is starting,
11845 the column widget's height and the frame's pixel height are
11846 different. The column widget's height is the right one. In
11847 later invocations, when Emacs is up, the frame's pixel height
11848 is right, though.
11849
11850 It's not obvious where the initial small difference comes from.
11851 2000-12-01, gerd. */
11852
11853 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
11854 #endif
11855
11856 if (flags & YNegative)
11857 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11858 - 2 * f->output_data.x->border_width
11859 - win_y
11860 - height
11861 + f->output_data.x->top_pos);
11862 }
11863
11864 /* The left_pos and top_pos
11865 are now relative to the top and left screen edges,
11866 so the flags should correspond. */
11867 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11868 }
11869
11870 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11871 to really change the position, and 0 when calling from
11872 x_make_frame_visible (in that case, XOFF and YOFF are the current
11873 position values). It is -1 when calling from x_set_frame_parameters,
11874 which means, do adjust for borders but don't change the gravity. */
11875
11876 void
11877 x_set_offset (f, xoff, yoff, change_gravity)
11878 struct frame *f;
11879 register int xoff, yoff;
11880 int change_gravity;
11881 {
11882 int modified_top, modified_left;
11883
11884 if (change_gravity > 0)
11885 {
11886 f->output_data.x->top_pos = yoff;
11887 f->output_data.x->left_pos = xoff;
11888 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11889 if (xoff < 0)
11890 f->output_data.x->size_hint_flags |= XNegative;
11891 if (yoff < 0)
11892 f->output_data.x->size_hint_flags |= YNegative;
11893 f->output_data.x->win_gravity = NorthWestGravity;
11894 }
11895 x_calc_absolute_position (f);
11896
11897 BLOCK_INPUT;
11898 x_wm_set_size_hint (f, (long) 0, 0);
11899
11900 modified_left = f->output_data.x->left_pos;
11901 modified_top = f->output_data.x->top_pos;
11902 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11903 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11904 /* It is a mystery why we need to add the border_width here
11905 when the frame is already visible, but experiment says we do. */
11906 if (change_gravity != 0)
11907 {
11908 modified_left += f->output_data.x->border_width;
11909 modified_top += f->output_data.x->border_width;
11910 }
11911 #endif
11912
11913 #ifdef USE_X_TOOLKIT
11914 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11915 modified_left, modified_top);
11916 #else /* not USE_X_TOOLKIT */
11917 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11918 modified_left, modified_top);
11919 #endif /* not USE_X_TOOLKIT */
11920 UNBLOCK_INPUT;
11921 }
11922
11923
11924 /* Change the size of frame F's X window to COLS/ROWS in the case F
11925 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
11926 top-left-corner window gravity for this size change and subsequent
11927 size changes. Otherwise we leave the window gravity unchanged. */
11928
11929 static void
11930 x_set_window_size_1 (f, change_gravity, cols, rows)
11931 struct frame *f;
11932 int change_gravity;
11933 int cols, rows;
11934 {
11935 int pixelwidth, pixelheight;
11936
11937 check_frame_size (f, &rows, &cols);
11938 f->output_data.x->vertical_scroll_bar_extra
11939 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11940 ? 0
11941 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11942 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11943 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11944 f->output_data.x->flags_areas_extra
11945 = FRAME_FLAGS_AREA_WIDTH (f);
11946 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11947 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11948
11949 f->output_data.x->win_gravity = NorthWestGravity;
11950 x_wm_set_size_hint (f, (long) 0, 0);
11951
11952 XSync (FRAME_X_DISPLAY (f), False);
11953 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11954 pixelwidth, pixelheight);
11955
11956 /* Now, strictly speaking, we can't be sure that this is accurate,
11957 but the window manager will get around to dealing with the size
11958 change request eventually, and we'll hear how it went when the
11959 ConfigureNotify event gets here.
11960
11961 We could just not bother storing any of this information here,
11962 and let the ConfigureNotify event set everything up, but that
11963 might be kind of confusing to the Lisp code, since size changes
11964 wouldn't be reported in the frame parameters until some random
11965 point in the future when the ConfigureNotify event arrives.
11966
11967 We pass 1 for DELAY since we can't run Lisp code inside of
11968 a BLOCK_INPUT. */
11969 change_frame_size (f, rows, cols, 0, 1, 0);
11970 PIXEL_WIDTH (f) = pixelwidth;
11971 PIXEL_HEIGHT (f) = pixelheight;
11972
11973 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11974 receive in the ConfigureNotify event; if we get what we asked
11975 for, then the event won't cause the screen to become garbaged, so
11976 we have to make sure to do it here. */
11977 SET_FRAME_GARBAGED (f);
11978
11979 XFlush (FRAME_X_DISPLAY (f));
11980 }
11981
11982
11983 /* Call this to change the size of frame F's x-window.
11984 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11985 for this size change and subsequent size changes.
11986 Otherwise we leave the window gravity unchanged. */
11987
11988 void
11989 x_set_window_size (f, change_gravity, cols, rows)
11990 struct frame *f;
11991 int change_gravity;
11992 int cols, rows;
11993 {
11994 BLOCK_INPUT;
11995
11996 #ifdef USE_X_TOOLKIT
11997
11998 if (f->output_data.x->widget != None)
11999 {
12000 /* The x and y position of the widget is clobbered by the
12001 call to XtSetValues within EmacsFrameSetCharSize.
12002 This is a real kludge, but I don't understand Xt so I can't
12003 figure out a correct fix. Can anyone else tell me? -- rms. */
12004 int xpos = f->output_data.x->widget->core.x;
12005 int ypos = f->output_data.x->widget->core.y;
12006 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12007 f->output_data.x->widget->core.x = xpos;
12008 f->output_data.x->widget->core.y = ypos;
12009 }
12010 else
12011 x_set_window_size_1 (f, change_gravity, cols, rows);
12012
12013 #else /* not USE_X_TOOLKIT */
12014
12015 x_set_window_size_1 (f, change_gravity, cols, rows);
12016
12017 #endif /* not USE_X_TOOLKIT */
12018
12019 /* If cursor was outside the new size, mark it as off. */
12020 mark_window_cursors_off (XWINDOW (f->root_window));
12021
12022 /* Clear out any recollection of where the mouse highlighting was,
12023 since it might be in a place that's outside the new frame size.
12024 Actually checking whether it is outside is a pain in the neck,
12025 so don't try--just let the highlighting be done afresh with new size. */
12026 cancel_mouse_face (f);
12027
12028 UNBLOCK_INPUT;
12029 }
12030 \f
12031 /* Mouse warping. */
12032
12033 void
12034 x_set_mouse_position (f, x, y)
12035 struct frame *f;
12036 int x, y;
12037 {
12038 int pix_x, pix_y;
12039
12040 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12041 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12042
12043 if (pix_x < 0) pix_x = 0;
12044 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12045
12046 if (pix_y < 0) pix_y = 0;
12047 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12048
12049 BLOCK_INPUT;
12050
12051 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12052 0, 0, 0, 0, pix_x, pix_y);
12053 UNBLOCK_INPUT;
12054 }
12055
12056 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12057
12058 void
12059 x_set_mouse_pixel_position (f, pix_x, pix_y)
12060 struct frame *f;
12061 int pix_x, pix_y;
12062 {
12063 BLOCK_INPUT;
12064
12065 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12066 0, 0, 0, 0, pix_x, pix_y);
12067 UNBLOCK_INPUT;
12068 }
12069 \f
12070 /* focus shifting, raising and lowering. */
12071
12072 void
12073 x_focus_on_frame (f)
12074 struct frame *f;
12075 {
12076 #if 0 /* This proves to be unpleasant. */
12077 x_raise_frame (f);
12078 #endif
12079 #if 0
12080 /* I don't think that the ICCCM allows programs to do things like this
12081 without the interaction of the window manager. Whatever you end up
12082 doing with this code, do it to x_unfocus_frame too. */
12083 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12084 RevertToPointerRoot, CurrentTime);
12085 #endif /* ! 0 */
12086 }
12087
12088 void
12089 x_unfocus_frame (f)
12090 struct frame *f;
12091 {
12092 #if 0
12093 /* Look at the remarks in x_focus_on_frame. */
12094 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12095 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12096 RevertToPointerRoot, CurrentTime);
12097 #endif /* ! 0 */
12098 }
12099
12100 /* Raise frame F. */
12101
12102 void
12103 x_raise_frame (f)
12104 struct frame *f;
12105 {
12106 if (f->async_visible)
12107 {
12108 BLOCK_INPUT;
12109 #ifdef USE_X_TOOLKIT
12110 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12111 #else /* not USE_X_TOOLKIT */
12112 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12113 #endif /* not USE_X_TOOLKIT */
12114 XFlush (FRAME_X_DISPLAY (f));
12115 UNBLOCK_INPUT;
12116 }
12117 }
12118
12119 /* Lower frame F. */
12120
12121 void
12122 x_lower_frame (f)
12123 struct frame *f;
12124 {
12125 if (f->async_visible)
12126 {
12127 BLOCK_INPUT;
12128 #ifdef USE_X_TOOLKIT
12129 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12130 #else /* not USE_X_TOOLKIT */
12131 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12132 #endif /* not USE_X_TOOLKIT */
12133 XFlush (FRAME_X_DISPLAY (f));
12134 UNBLOCK_INPUT;
12135 }
12136 }
12137
12138 static void
12139 XTframe_raise_lower (f, raise_flag)
12140 FRAME_PTR f;
12141 int raise_flag;
12142 {
12143 if (raise_flag)
12144 x_raise_frame (f);
12145 else
12146 x_lower_frame (f);
12147 }
12148 \f
12149 /* Change of visibility. */
12150
12151 /* This tries to wait until the frame is really visible.
12152 However, if the window manager asks the user where to position
12153 the frame, this will return before the user finishes doing that.
12154 The frame will not actually be visible at that time,
12155 but it will become visible later when the window manager
12156 finishes with it. */
12157
12158 void
12159 x_make_frame_visible (f)
12160 struct frame *f;
12161 {
12162 Lisp_Object type;
12163 int original_top, original_left;
12164 int retry_count = 2;
12165
12166 retry:
12167
12168 BLOCK_INPUT;
12169
12170 type = x_icon_type (f);
12171 if (!NILP (type))
12172 x_bitmap_icon (f, type);
12173
12174 if (! FRAME_VISIBLE_P (f))
12175 {
12176 /* We test FRAME_GARBAGED_P here to make sure we don't
12177 call x_set_offset a second time
12178 if we get to x_make_frame_visible a second time
12179 before the window gets really visible. */
12180 if (! FRAME_ICONIFIED_P (f)
12181 && ! f->output_data.x->asked_for_visible)
12182 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12183
12184 f->output_data.x->asked_for_visible = 1;
12185
12186 if (! EQ (Vx_no_window_manager, Qt))
12187 x_wm_set_window_state (f, NormalState);
12188 #ifdef USE_X_TOOLKIT
12189 /* This was XtPopup, but that did nothing for an iconified frame. */
12190 XtMapWidget (f->output_data.x->widget);
12191 #else /* not USE_X_TOOLKIT */
12192 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12193 #endif /* not USE_X_TOOLKIT */
12194 #if 0 /* This seems to bring back scroll bars in the wrong places
12195 if the window configuration has changed. They seem
12196 to come back ok without this. */
12197 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12198 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12199 #endif
12200 }
12201
12202 XFlush (FRAME_X_DISPLAY (f));
12203
12204 /* Synchronize to ensure Emacs knows the frame is visible
12205 before we do anything else. We do this loop with input not blocked
12206 so that incoming events are handled. */
12207 {
12208 Lisp_Object frame;
12209 int count;
12210 /* This must be before UNBLOCK_INPUT
12211 since events that arrive in response to the actions above
12212 will set it when they are handled. */
12213 int previously_visible = f->output_data.x->has_been_visible;
12214
12215 original_left = f->output_data.x->left_pos;
12216 original_top = f->output_data.x->top_pos;
12217
12218 /* This must come after we set COUNT. */
12219 UNBLOCK_INPUT;
12220
12221 /* We unblock here so that arriving X events are processed. */
12222
12223 /* Now move the window back to where it was "supposed to be".
12224 But don't do it if the gravity is negative.
12225 When the gravity is negative, this uses a position
12226 that is 3 pixels too low. Perhaps that's really the border width.
12227
12228 Don't do this if the window has never been visible before,
12229 because the window manager may choose the position
12230 and we don't want to override it. */
12231
12232 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12233 && f->output_data.x->win_gravity == NorthWestGravity
12234 && previously_visible)
12235 {
12236 Drawable rootw;
12237 int x, y;
12238 unsigned int width, height, border, depth;
12239
12240 BLOCK_INPUT;
12241
12242 /* On some window managers (such as FVWM) moving an existing
12243 window, even to the same place, causes the window manager
12244 to introduce an offset. This can cause the window to move
12245 to an unexpected location. Check the geometry (a little
12246 slow here) and then verify that the window is in the right
12247 place. If the window is not in the right place, move it
12248 there, and take the potential window manager hit. */
12249 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12250 &rootw, &x, &y, &width, &height, &border, &depth);
12251
12252 if (original_left != x || original_top != y)
12253 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12254 original_left, original_top);
12255
12256 UNBLOCK_INPUT;
12257 }
12258
12259 XSETFRAME (frame, f);
12260
12261 /* Wait until the frame is visible. Process X events until a
12262 MapNotify event has been seen, or until we think we won't get a
12263 MapNotify at all.. */
12264 for (count = input_signal_count + 10;
12265 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12266 {
12267 /* Force processing of queued events. */
12268 x_sync (f);
12269
12270 /* Machines that do polling rather than SIGIO have been
12271 observed to go into a busy-wait here. So we'll fake an
12272 alarm signal to let the handler know that there's something
12273 to be read. We used to raise a real alarm, but it seems
12274 that the handler isn't always enabled here. This is
12275 probably a bug. */
12276 if (input_polling_used ())
12277 {
12278 /* It could be confusing if a real alarm arrives while
12279 processing the fake one. Turn it off and let the
12280 handler reset it. */
12281 extern void poll_for_input_1 P_ ((void));
12282 int old_poll_suppress_count = poll_suppress_count;
12283 poll_suppress_count = 1;
12284 poll_for_input_1 ();
12285 poll_suppress_count = old_poll_suppress_count;
12286 }
12287
12288 /* See if a MapNotify event has been processed. */
12289 FRAME_SAMPLE_VISIBILITY (f);
12290 }
12291
12292 /* 2000-09-28: In
12293
12294 (let ((f (selected-frame)))
12295 (iconify-frame f)
12296 (raise-frame f))
12297
12298 the frame is not raised with various window managers on
12299 FreeBSD, Linux and Solaris. It turns out that, for some
12300 unknown reason, the call to XtMapWidget is completely ignored.
12301 Mapping the widget a second time works. */
12302
12303 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12304 goto retry;
12305 }
12306 }
12307
12308 /* Change from mapped state to withdrawn state. */
12309
12310 /* Make the frame visible (mapped and not iconified). */
12311
12312 void
12313 x_make_frame_invisible (f)
12314 struct frame *f;
12315 {
12316 Window window;
12317
12318 #ifdef USE_X_TOOLKIT
12319 /* Use the frame's outermost window, not the one we normally draw on. */
12320 window = XtWindow (f->output_data.x->widget);
12321 #else /* not USE_X_TOOLKIT */
12322 window = FRAME_X_WINDOW (f);
12323 #endif /* not USE_X_TOOLKIT */
12324
12325 /* Don't keep the highlight on an invisible frame. */
12326 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12327 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12328
12329 #if 0/* This might add unreliability; I don't trust it -- rms. */
12330 if (! f->async_visible && ! f->async_iconified)
12331 return;
12332 #endif
12333
12334 BLOCK_INPUT;
12335
12336 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12337 that the current position of the window is user-specified, rather than
12338 program-specified, so that when the window is mapped again, it will be
12339 placed at the same location, without forcing the user to position it
12340 by hand again (they have already done that once for this window.) */
12341 x_wm_set_size_hint (f, (long) 0, 1);
12342
12343 #ifdef HAVE_X11R4
12344
12345 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12346 DefaultScreen (FRAME_X_DISPLAY (f))))
12347 {
12348 UNBLOCK_INPUT_RESIGNAL;
12349 error ("Can't notify window manager of window withdrawal");
12350 }
12351 #else /* ! defined (HAVE_X11R4) */
12352
12353 /* Tell the window manager what we're going to do. */
12354 if (! EQ (Vx_no_window_manager, Qt))
12355 {
12356 XEvent unmap;
12357
12358 unmap.xunmap.type = UnmapNotify;
12359 unmap.xunmap.window = window;
12360 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12361 unmap.xunmap.from_configure = False;
12362 if (! XSendEvent (FRAME_X_DISPLAY (f),
12363 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12364 False,
12365 SubstructureRedirectMaskSubstructureNotifyMask,
12366 &unmap))
12367 {
12368 UNBLOCK_INPUT_RESIGNAL;
12369 error ("Can't notify window manager of withdrawal");
12370 }
12371 }
12372
12373 /* Unmap the window ourselves. Cheeky! */
12374 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12375 #endif /* ! defined (HAVE_X11R4) */
12376
12377 /* We can't distinguish this from iconification
12378 just by the event that we get from the server.
12379 So we can't win using the usual strategy of letting
12380 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12381 and synchronize with the server to make sure we agree. */
12382 f->visible = 0;
12383 FRAME_ICONIFIED_P (f) = 0;
12384 f->async_visible = 0;
12385 f->async_iconified = 0;
12386
12387 x_sync (f);
12388
12389 UNBLOCK_INPUT;
12390 }
12391
12392 /* Change window state from mapped to iconified. */
12393
12394 void
12395 x_iconify_frame (f)
12396 struct frame *f;
12397 {
12398 int result;
12399 Lisp_Object type;
12400
12401 /* Don't keep the highlight on an invisible frame. */
12402 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12403 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12404
12405 if (f->async_iconified)
12406 return;
12407
12408 BLOCK_INPUT;
12409
12410 FRAME_SAMPLE_VISIBILITY (f);
12411
12412 type = x_icon_type (f);
12413 if (!NILP (type))
12414 x_bitmap_icon (f, type);
12415
12416 #ifdef USE_X_TOOLKIT
12417
12418 if (! FRAME_VISIBLE_P (f))
12419 {
12420 if (! EQ (Vx_no_window_manager, Qt))
12421 x_wm_set_window_state (f, IconicState);
12422 /* This was XtPopup, but that did nothing for an iconified frame. */
12423 XtMapWidget (f->output_data.x->widget);
12424 /* The server won't give us any event to indicate
12425 that an invisible frame was changed to an icon,
12426 so we have to record it here. */
12427 f->iconified = 1;
12428 f->visible = 1;
12429 f->async_iconified = 1;
12430 f->async_visible = 0;
12431 UNBLOCK_INPUT;
12432 return;
12433 }
12434
12435 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12436 XtWindow (f->output_data.x->widget),
12437 DefaultScreen (FRAME_X_DISPLAY (f)));
12438 UNBLOCK_INPUT;
12439
12440 if (!result)
12441 error ("Can't notify window manager of iconification");
12442
12443 f->async_iconified = 1;
12444 f->async_visible = 0;
12445
12446
12447 BLOCK_INPUT;
12448 XFlush (FRAME_X_DISPLAY (f));
12449 UNBLOCK_INPUT;
12450 #else /* not USE_X_TOOLKIT */
12451
12452 /* Make sure the X server knows where the window should be positioned,
12453 in case the user deiconifies with the window manager. */
12454 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12455 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12456
12457 /* Since we don't know which revision of X we're running, we'll use both
12458 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12459
12460 /* X11R4: send a ClientMessage to the window manager using the
12461 WM_CHANGE_STATE type. */
12462 {
12463 XEvent message;
12464
12465 message.xclient.window = FRAME_X_WINDOW (f);
12466 message.xclient.type = ClientMessage;
12467 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12468 message.xclient.format = 32;
12469 message.xclient.data.l[0] = IconicState;
12470
12471 if (! XSendEvent (FRAME_X_DISPLAY (f),
12472 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12473 False,
12474 SubstructureRedirectMask | SubstructureNotifyMask,
12475 &message))
12476 {
12477 UNBLOCK_INPUT_RESIGNAL;
12478 error ("Can't notify window manager of iconification");
12479 }
12480 }
12481
12482 /* X11R3: set the initial_state field of the window manager hints to
12483 IconicState. */
12484 x_wm_set_window_state (f, IconicState);
12485
12486 if (!FRAME_VISIBLE_P (f))
12487 {
12488 /* If the frame was withdrawn, before, we must map it. */
12489 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12490 }
12491
12492 f->async_iconified = 1;
12493 f->async_visible = 0;
12494
12495 XFlush (FRAME_X_DISPLAY (f));
12496 UNBLOCK_INPUT;
12497 #endif /* not USE_X_TOOLKIT */
12498 }
12499
12500 \f
12501 /* Free X resources of frame F. */
12502
12503 void
12504 x_free_frame_resources (f)
12505 struct frame *f;
12506 {
12507 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12508
12509 BLOCK_INPUT;
12510
12511 /* If a display connection is dead, don't try sending more
12512 commands to the X server. */
12513 if (dpyinfo->display)
12514 {
12515 if (f->output_data.x->icon_desc)
12516 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12517
12518 #ifdef HAVE_X_I18N
12519 if (FRAME_XIC (f))
12520 free_frame_xic (f);
12521 #endif
12522
12523 if (FRAME_X_WINDOW (f))
12524 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12525
12526 #ifdef USE_X_TOOLKIT
12527 if (f->output_data.x->widget)
12528 XtDestroyWidget (f->output_data.x->widget);
12529 free_frame_menubar (f);
12530 #endif /* USE_X_TOOLKIT */
12531
12532 unload_color (f, f->output_data.x->foreground_pixel);
12533 unload_color (f, f->output_data.x->background_pixel);
12534 unload_color (f, f->output_data.x->cursor_pixel);
12535 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12536 unload_color (f, f->output_data.x->border_pixel);
12537 unload_color (f, f->output_data.x->mouse_pixel);
12538
12539 if (f->output_data.x->scroll_bar_background_pixel != -1)
12540 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12541 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12542 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12543 if (f->output_data.x->white_relief.allocated_p)
12544 unload_color (f, f->output_data.x->white_relief.pixel);
12545 if (f->output_data.x->black_relief.allocated_p)
12546 unload_color (f, f->output_data.x->black_relief.pixel);
12547
12548 if (FRAME_FACE_CACHE (f))
12549 free_frame_faces (f);
12550
12551 x_free_gcs (f);
12552 XFlush (FRAME_X_DISPLAY (f));
12553 }
12554
12555 if (f->output_data.x->saved_menu_event)
12556 xfree (f->output_data.x->saved_menu_event);
12557
12558 xfree (f->output_data.x);
12559 f->output_data.x = NULL;
12560
12561 if (f == dpyinfo->x_focus_frame)
12562 dpyinfo->x_focus_frame = 0;
12563 if (f == dpyinfo->x_focus_event_frame)
12564 dpyinfo->x_focus_event_frame = 0;
12565 if (f == dpyinfo->x_highlight_frame)
12566 dpyinfo->x_highlight_frame = 0;
12567
12568 if (f == dpyinfo->mouse_face_mouse_frame)
12569 {
12570 dpyinfo->mouse_face_beg_row
12571 = dpyinfo->mouse_face_beg_col = -1;
12572 dpyinfo->mouse_face_end_row
12573 = dpyinfo->mouse_face_end_col = -1;
12574 dpyinfo->mouse_face_window = Qnil;
12575 dpyinfo->mouse_face_deferred_gc = 0;
12576 dpyinfo->mouse_face_mouse_frame = 0;
12577 }
12578
12579 UNBLOCK_INPUT;
12580 }
12581
12582
12583 /* Destroy the X window of frame F. */
12584
12585 void
12586 x_destroy_window (f)
12587 struct frame *f;
12588 {
12589 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12590
12591 /* If a display connection is dead, don't try sending more
12592 commands to the X server. */
12593 if (dpyinfo->display != 0)
12594 x_free_frame_resources (f);
12595
12596 dpyinfo->reference_count--;
12597 }
12598
12599 \f
12600 /* Setting window manager hints. */
12601
12602 /* Set the normal size hints for the window manager, for frame F.
12603 FLAGS is the flags word to use--or 0 meaning preserve the flags
12604 that the window now has.
12605 If USER_POSITION is nonzero, we set the USPosition
12606 flag (this is useful when FLAGS is 0). */
12607
12608 void
12609 x_wm_set_size_hint (f, flags, user_position)
12610 struct frame *f;
12611 long flags;
12612 int user_position;
12613 {
12614 XSizeHints size_hints;
12615
12616 #ifdef USE_X_TOOLKIT
12617 Arg al[2];
12618 int ac = 0;
12619 Dimension widget_width, widget_height;
12620 Window window = XtWindow (f->output_data.x->widget);
12621 #else /* not USE_X_TOOLKIT */
12622 Window window = FRAME_X_WINDOW (f);
12623 #endif /* not USE_X_TOOLKIT */
12624
12625 /* Setting PMaxSize caused various problems. */
12626 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12627
12628 size_hints.x = f->output_data.x->left_pos;
12629 size_hints.y = f->output_data.x->top_pos;
12630
12631 #ifdef USE_X_TOOLKIT
12632 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12633 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12634 XtGetValues (f->output_data.x->widget, al, ac);
12635 size_hints.height = widget_height;
12636 size_hints.width = widget_width;
12637 #else /* not USE_X_TOOLKIT */
12638 size_hints.height = PIXEL_HEIGHT (f);
12639 size_hints.width = PIXEL_WIDTH (f);
12640 #endif /* not USE_X_TOOLKIT */
12641
12642 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12643 size_hints.height_inc = f->output_data.x->line_height;
12644 size_hints.max_width
12645 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12646 size_hints.max_height
12647 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12648
12649 /* Calculate the base and minimum sizes.
12650
12651 (When we use the X toolkit, we don't do it here.
12652 Instead we copy the values that the widgets are using, below.) */
12653 #ifndef USE_X_TOOLKIT
12654 {
12655 int base_width, base_height;
12656 int min_rows = 0, min_cols = 0;
12657
12658 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12659 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12660
12661 check_frame_size (f, &min_rows, &min_cols);
12662
12663 /* The window manager uses the base width hints to calculate the
12664 current number of rows and columns in the frame while
12665 resizing; min_width and min_height aren't useful for this
12666 purpose, since they might not give the dimensions for a
12667 zero-row, zero-column frame.
12668
12669 We use the base_width and base_height members if we have
12670 them; otherwise, we set the min_width and min_height members
12671 to the size for a zero x zero frame. */
12672
12673 #ifdef HAVE_X11R4
12674 size_hints.flags |= PBaseSize;
12675 size_hints.base_width = base_width;
12676 size_hints.base_height = base_height;
12677 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12678 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12679 #else
12680 size_hints.min_width = base_width;
12681 size_hints.min_height = base_height;
12682 #endif
12683 }
12684
12685 /* If we don't need the old flags, we don't need the old hint at all. */
12686 if (flags)
12687 {
12688 size_hints.flags |= flags;
12689 goto no_read;
12690 }
12691 #endif /* not USE_X_TOOLKIT */
12692
12693 {
12694 XSizeHints hints; /* Sometimes I hate X Windows... */
12695 long supplied_return;
12696 int value;
12697
12698 #ifdef HAVE_X11R4
12699 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12700 &supplied_return);
12701 #else
12702 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12703 #endif
12704
12705 #ifdef USE_X_TOOLKIT
12706 size_hints.base_height = hints.base_height;
12707 size_hints.base_width = hints.base_width;
12708 size_hints.min_height = hints.min_height;
12709 size_hints.min_width = hints.min_width;
12710 #endif
12711
12712 if (flags)
12713 size_hints.flags |= flags;
12714 else
12715 {
12716 if (value == 0)
12717 hints.flags = 0;
12718 if (hints.flags & PSize)
12719 size_hints.flags |= PSize;
12720 if (hints.flags & PPosition)
12721 size_hints.flags |= PPosition;
12722 if (hints.flags & USPosition)
12723 size_hints.flags |= USPosition;
12724 if (hints.flags & USSize)
12725 size_hints.flags |= USSize;
12726 }
12727 }
12728
12729 #ifndef USE_X_TOOLKIT
12730 no_read:
12731 #endif
12732
12733 #ifdef PWinGravity
12734 size_hints.win_gravity = f->output_data.x->win_gravity;
12735 size_hints.flags |= PWinGravity;
12736
12737 if (user_position)
12738 {
12739 size_hints.flags &= ~ PPosition;
12740 size_hints.flags |= USPosition;
12741 }
12742 #endif /* PWinGravity */
12743
12744 #ifdef HAVE_X11R4
12745 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12746 #else
12747 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12748 #endif
12749 }
12750
12751 /* Used for IconicState or NormalState */
12752
12753 void
12754 x_wm_set_window_state (f, state)
12755 struct frame *f;
12756 int state;
12757 {
12758 #ifdef USE_X_TOOLKIT
12759 Arg al[1];
12760
12761 XtSetArg (al[0], XtNinitialState, state);
12762 XtSetValues (f->output_data.x->widget, al, 1);
12763 #else /* not USE_X_TOOLKIT */
12764 Window window = FRAME_X_WINDOW (f);
12765
12766 f->output_data.x->wm_hints.flags |= StateHint;
12767 f->output_data.x->wm_hints.initial_state = state;
12768
12769 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12770 #endif /* not USE_X_TOOLKIT */
12771 }
12772
12773 void
12774 x_wm_set_icon_pixmap (f, pixmap_id)
12775 struct frame *f;
12776 int pixmap_id;
12777 {
12778 Pixmap icon_pixmap;
12779
12780 #ifndef USE_X_TOOLKIT
12781 Window window = FRAME_X_WINDOW (f);
12782 #endif
12783
12784 if (pixmap_id > 0)
12785 {
12786 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12787 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12788 }
12789 else
12790 {
12791 /* It seems there is no way to turn off use of an icon pixmap.
12792 The following line does it, only if no icon has yet been created,
12793 for some window managers. But with mwm it crashes.
12794 Some people say it should clear the IconPixmapHint bit in this case,
12795 but that doesn't work, and the X consortium said it isn't the
12796 right thing at all. Since there is no way to win,
12797 best to explicitly give up. */
12798 #if 0
12799 f->output_data.x->wm_hints.icon_pixmap = None;
12800 #else
12801 return;
12802 #endif
12803 }
12804
12805 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12806
12807 {
12808 Arg al[1];
12809 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12810 XtSetValues (f->output_data.x->widget, al, 1);
12811 }
12812
12813 #else /* not USE_X_TOOLKIT */
12814
12815 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12816 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12817
12818 #endif /* not USE_X_TOOLKIT */
12819 }
12820
12821 void
12822 x_wm_set_icon_position (f, icon_x, icon_y)
12823 struct frame *f;
12824 int icon_x, icon_y;
12825 {
12826 #ifdef USE_X_TOOLKIT
12827 Window window = XtWindow (f->output_data.x->widget);
12828 #else
12829 Window window = FRAME_X_WINDOW (f);
12830 #endif
12831
12832 f->output_data.x->wm_hints.flags |= IconPositionHint;
12833 f->output_data.x->wm_hints.icon_x = icon_x;
12834 f->output_data.x->wm_hints.icon_y = icon_y;
12835
12836 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12837 }
12838
12839 \f
12840 /***********************************************************************
12841 Fonts
12842 ***********************************************************************/
12843
12844 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12845
12846 struct font_info *
12847 x_get_font_info (f, font_idx)
12848 FRAME_PTR f;
12849 int font_idx;
12850 {
12851 return (FRAME_X_FONT_TABLE (f) + font_idx);
12852 }
12853
12854
12855 /* Return a list of names of available fonts matching PATTERN on frame F.
12856
12857 If SIZE is > 0, it is the size (maximum bounds width) of fonts
12858 to be listed.
12859
12860 SIZE < 0 means include scalable fonts.
12861
12862 Frame F null means we have not yet created any frame on X, and
12863 consult the first display in x_display_list. MAXNAMES sets a limit
12864 on how many fonts to match. */
12865
12866 Lisp_Object
12867 x_list_fonts (f, pattern, size, maxnames)
12868 struct frame *f;
12869 Lisp_Object pattern;
12870 int size;
12871 int maxnames;
12872 {
12873 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12874 Lisp_Object tem, second_best;
12875 struct x_display_info *dpyinfo
12876 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
12877 Display *dpy = dpyinfo->display;
12878 int try_XLoadQueryFont = 0;
12879 int count;
12880 int allow_scalable_fonts_p = 0;
12881
12882 if (size < 0)
12883 {
12884 allow_scalable_fonts_p = 1;
12885 size = 0;
12886 }
12887
12888 patterns = Fassoc (pattern, Valternate_fontname_alist);
12889 if (NILP (patterns))
12890 patterns = Fcons (pattern, Qnil);
12891
12892 if (maxnames == 1 && !size)
12893 /* We can return any single font matching PATTERN. */
12894 try_XLoadQueryFont = 1;
12895
12896 for (; CONSP (patterns); patterns = XCDR (patterns))
12897 {
12898 int num_fonts;
12899 char **names = NULL;
12900
12901 pattern = XCAR (patterns);
12902 /* See if we cached the result for this particular query.
12903 The cache is an alist of the form:
12904 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
12905 tem = XCDR (dpyinfo->name_list_element);
12906 key = Fcons (Fcons (pattern, make_number (maxnames)),
12907 allow_scalable_fonts_p ? Qt : Qnil);
12908 list = Fassoc (key, tem);
12909 if (!NILP (list))
12910 {
12911 list = Fcdr_safe (list);
12912 /* We have a cashed list. Don't have to get the list again. */
12913 goto label_cached;
12914 }
12915
12916 /* At first, put PATTERN in the cache. */
12917
12918 BLOCK_INPUT;
12919 count = x_catch_errors (dpy);
12920
12921 if (try_XLoadQueryFont)
12922 {
12923 XFontStruct *font;
12924 unsigned long value;
12925
12926 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12927 if (x_had_errors_p (dpy))
12928 {
12929 /* This error is perhaps due to insufficient memory on X
12930 server. Let's just ignore it. */
12931 font = NULL;
12932 x_clear_errors (dpy);
12933 }
12934
12935 if (font
12936 && XGetFontProperty (font, XA_FONT, &value))
12937 {
12938 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12939 int len = strlen (name);
12940 char *tmp;
12941
12942 /* If DXPC (a Differential X Protocol Compressor)
12943 Ver.3.7 is running, XGetAtomName will return null
12944 string. We must avoid such a name. */
12945 if (len == 0)
12946 try_XLoadQueryFont = 0;
12947 else
12948 {
12949 num_fonts = 1;
12950 names = (char **) alloca (sizeof (char *));
12951 /* Some systems only allow alloca assigned to a
12952 simple var. */
12953 tmp = (char *) alloca (len + 1); names[0] = tmp;
12954 bcopy (name, names[0], len + 1);
12955 XFree (name);
12956 }
12957 }
12958 else
12959 try_XLoadQueryFont = 0;
12960
12961 if (font)
12962 XFreeFont (dpy, font);
12963 }
12964
12965 if (!try_XLoadQueryFont)
12966 {
12967 /* We try at least 10 fonts because XListFonts will return
12968 auto-scaled fonts at the head. */
12969 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12970 &num_fonts);
12971 if (x_had_errors_p (dpy))
12972 {
12973 /* This error is perhaps due to insufficient memory on X
12974 server. Let's just ignore it. */
12975 names = NULL;
12976 x_clear_errors (dpy);
12977 }
12978 }
12979
12980 x_uncatch_errors (dpy, count);
12981 UNBLOCK_INPUT;
12982
12983 if (names)
12984 {
12985 int i;
12986
12987 /* Make a list of all the fonts we got back.
12988 Store that in the font cache for the display. */
12989 for (i = 0; i < num_fonts; i++)
12990 {
12991 int width = 0;
12992 char *p = names[i];
12993 int average_width = -1, dashes = 0;
12994
12995 /* Count the number of dashes in NAMES[I]. If there are
12996 14 dashes, and the field value following 12th dash
12997 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12998 is usually too ugly to be used for editing. Let's
12999 ignore it. */
13000 while (*p)
13001 if (*p++ == '-')
13002 {
13003 dashes++;
13004 if (dashes == 7) /* PIXEL_SIZE field */
13005 width = atoi (p);
13006 else if (dashes == 12) /* AVERAGE_WIDTH field */
13007 average_width = atoi (p);
13008 }
13009
13010 if (allow_scalable_fonts_p
13011 || dashes < 14 || average_width != 0)
13012 {
13013 tem = build_string (names[i]);
13014 if (NILP (Fassoc (tem, list)))
13015 {
13016 if (STRINGP (Vx_pixel_size_width_font_regexp)
13017 && ((fast_c_string_match_ignore_case
13018 (Vx_pixel_size_width_font_regexp, names[i]))
13019 >= 0))
13020 /* We can set the value of PIXEL_SIZE to the
13021 width of this font. */
13022 list = Fcons (Fcons (tem, make_number (width)), list);
13023 else
13024 /* For the moment, width is not known. */
13025 list = Fcons (Fcons (tem, Qnil), list);
13026 }
13027 }
13028 }
13029 if (!try_XLoadQueryFont)
13030 XFreeFontNames (names);
13031 }
13032
13033 /* Now store the result in the cache. */
13034 XCDR (dpyinfo->name_list_element)
13035 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13036
13037 label_cached:
13038 if (NILP (list)) continue; /* Try the remaining alternatives. */
13039
13040 newlist = second_best = Qnil;
13041 /* Make a list of the fonts that have the right width. */
13042 for (; CONSP (list); list = XCDR (list))
13043 {
13044 int found_size;
13045
13046 tem = XCAR (list);
13047
13048 if (!CONSP (tem) || NILP (XCAR (tem)))
13049 continue;
13050 if (!size)
13051 {
13052 newlist = Fcons (XCAR (tem), newlist);
13053 continue;
13054 }
13055
13056 if (!INTEGERP (XCDR (tem)))
13057 {
13058 /* Since we have not yet known the size of this font, we
13059 must try slow function call XLoadQueryFont. */
13060 XFontStruct *thisinfo;
13061
13062 BLOCK_INPUT;
13063 count = x_catch_errors (dpy);
13064 thisinfo = XLoadQueryFont (dpy,
13065 XSTRING (XCAR (tem))->data);
13066 if (x_had_errors_p (dpy))
13067 {
13068 /* This error is perhaps due to insufficient memory on X
13069 server. Let's just ignore it. */
13070 thisinfo = NULL;
13071 x_clear_errors (dpy);
13072 }
13073 x_uncatch_errors (dpy, count);
13074 UNBLOCK_INPUT;
13075
13076 if (thisinfo)
13077 {
13078 XCDR (tem)
13079 = (thisinfo->min_bounds.width == 0
13080 ? make_number (0)
13081 : make_number (thisinfo->max_bounds.width));
13082 XFreeFont (dpy, thisinfo);
13083 }
13084 else
13085 /* For unknown reason, the previous call of XListFont had
13086 returned a font which can't be opened. Record the size
13087 as 0 not to try to open it again. */
13088 XCDR (tem) = make_number (0);
13089 }
13090
13091 found_size = XINT (XCDR (tem));
13092 if (found_size == size)
13093 newlist = Fcons (XCAR (tem), newlist);
13094 else if (found_size > 0)
13095 {
13096 if (NILP (second_best))
13097 second_best = tem;
13098 else if (found_size < size)
13099 {
13100 if (XINT (XCDR (second_best)) > size
13101 || XINT (XCDR (second_best)) < found_size)
13102 second_best = tem;
13103 }
13104 else
13105 {
13106 if (XINT (XCDR (second_best)) > size
13107 && XINT (XCDR (second_best)) > found_size)
13108 second_best = tem;
13109 }
13110 }
13111 }
13112 if (!NILP (newlist))
13113 break;
13114 else if (!NILP (second_best))
13115 {
13116 newlist = Fcons (XCAR (second_best), Qnil);
13117 break;
13118 }
13119 }
13120
13121 return newlist;
13122 }
13123
13124
13125 #if GLYPH_DEBUG
13126
13127 /* Check that FONT is valid on frame F. It is if it can be found in F's
13128 font table. */
13129
13130 static void
13131 x_check_font (f, font)
13132 struct frame *f;
13133 XFontStruct *font;
13134 {
13135 int i;
13136 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13137
13138 xassert (font != NULL);
13139
13140 for (i = 0; i < dpyinfo->n_fonts; i++)
13141 if (dpyinfo->font_table[i].name
13142 && font == dpyinfo->font_table[i].font)
13143 break;
13144
13145 xassert (i < dpyinfo->n_fonts);
13146 }
13147
13148 #endif /* GLYPH_DEBUG != 0 */
13149
13150 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13151 Note: There are (broken) X fonts out there with invalid XFontStruct
13152 min_bounds contents. For example, handa@etl.go.jp reports that
13153 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13154 have font->min_bounds.width == 0. */
13155
13156 static INLINE void
13157 x_font_min_bounds (font, w, h)
13158 XFontStruct *font;
13159 int *w, *h;
13160 {
13161 *h = FONT_HEIGHT (font);
13162 *w = font->min_bounds.width;
13163
13164 /* Try to handle the case where FONT->min_bounds has invalid
13165 contents. Since the only font known to have invalid min_bounds
13166 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13167 if (*w <= 0)
13168 *w = font->max_bounds.width;
13169 }
13170
13171
13172 /* Compute the smallest character width and smallest font height over
13173 all fonts available on frame F. Set the members smallest_char_width
13174 and smallest_font_height in F's x_display_info structure to
13175 the values computed. Value is non-zero if smallest_font_height or
13176 smallest_char_width become smaller than they were before. */
13177
13178 static int
13179 x_compute_min_glyph_bounds (f)
13180 struct frame *f;
13181 {
13182 int i;
13183 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13184 XFontStruct *font;
13185 int old_width = dpyinfo->smallest_char_width;
13186 int old_height = dpyinfo->smallest_font_height;
13187
13188 dpyinfo->smallest_font_height = 100000;
13189 dpyinfo->smallest_char_width = 100000;
13190
13191 for (i = 0; i < dpyinfo->n_fonts; ++i)
13192 if (dpyinfo->font_table[i].name)
13193 {
13194 struct font_info *fontp = dpyinfo->font_table + i;
13195 int w, h;
13196
13197 font = (XFontStruct *) fontp->font;
13198 xassert (font != (XFontStruct *) ~0);
13199 x_font_min_bounds (font, &w, &h);
13200
13201 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13202 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13203 }
13204
13205 xassert (dpyinfo->smallest_char_width > 0
13206 && dpyinfo->smallest_font_height > 0);
13207
13208 return (dpyinfo->n_fonts == 1
13209 || dpyinfo->smallest_char_width < old_width
13210 || dpyinfo->smallest_font_height < old_height);
13211 }
13212
13213
13214 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13215 pointer to the structure font_info while allocating it dynamically.
13216 If SIZE is 0, load any size of font.
13217 If loading is failed, return NULL. */
13218
13219 struct font_info *
13220 x_load_font (f, fontname, size)
13221 struct frame *f;
13222 register char *fontname;
13223 int size;
13224 {
13225 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13226 Lisp_Object font_names;
13227 int count;
13228
13229 /* Get a list of all the fonts that match this name. Once we
13230 have a list of matching fonts, we compare them against the fonts
13231 we already have by comparing names. */
13232 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13233
13234 if (!NILP (font_names))
13235 {
13236 Lisp_Object tail;
13237 int i;
13238
13239 for (i = 0; i < dpyinfo->n_fonts; i++)
13240 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13241 if (dpyinfo->font_table[i].name
13242 && (!strcmp (dpyinfo->font_table[i].name,
13243 XSTRING (XCAR (tail))->data)
13244 || !strcmp (dpyinfo->font_table[i].full_name,
13245 XSTRING (XCAR (tail))->data)))
13246 return (dpyinfo->font_table + i);
13247 }
13248
13249 /* Load the font and add it to the table. */
13250 {
13251 char *full_name;
13252 XFontStruct *font;
13253 struct font_info *fontp;
13254 unsigned long value;
13255 int i;
13256
13257 /* If we have found fonts by x_list_font, load one of them. If
13258 not, we still try to load a font by the name given as FONTNAME
13259 because XListFonts (called in x_list_font) of some X server has
13260 a bug of not finding a font even if the font surely exists and
13261 is loadable by XLoadQueryFont. */
13262 if (size > 0 && !NILP (font_names))
13263 fontname = (char *) XSTRING (XCAR (font_names))->data;
13264
13265 BLOCK_INPUT;
13266 count = x_catch_errors (FRAME_X_DISPLAY (f));
13267 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13268 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13269 {
13270 /* This error is perhaps due to insufficient memory on X
13271 server. Let's just ignore it. */
13272 font = NULL;
13273 x_clear_errors (FRAME_X_DISPLAY (f));
13274 }
13275 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13276 UNBLOCK_INPUT;
13277 if (!font)
13278 return NULL;
13279
13280 /* Find a free slot in the font table. */
13281 for (i = 0; i < dpyinfo->n_fonts; ++i)
13282 if (dpyinfo->font_table[i].name == NULL)
13283 break;
13284
13285 /* If no free slot found, maybe enlarge the font table. */
13286 if (i == dpyinfo->n_fonts
13287 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13288 {
13289 int sz;
13290 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13291 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13292 dpyinfo->font_table
13293 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13294 }
13295
13296 fontp = dpyinfo->font_table + i;
13297 if (i == dpyinfo->n_fonts)
13298 ++dpyinfo->n_fonts;
13299
13300 /* Now fill in the slots of *FONTP. */
13301 BLOCK_INPUT;
13302 fontp->font = font;
13303 fontp->font_idx = i;
13304 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13305 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13306
13307 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13308 full_name = 0;
13309 if (XGetFontProperty (font, XA_FONT, &value))
13310 {
13311 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13312 char *p = name;
13313 int dashes = 0;
13314
13315 /* Count the number of dashes in the "full name".
13316 If it is too few, this isn't really the font's full name,
13317 so don't use it.
13318 In X11R4, the fonts did not come with their canonical names
13319 stored in them. */
13320 while (*p)
13321 {
13322 if (*p == '-')
13323 dashes++;
13324 p++;
13325 }
13326
13327 if (dashes >= 13)
13328 {
13329 full_name = (char *) xmalloc (p - name + 1);
13330 bcopy (name, full_name, p - name + 1);
13331 }
13332
13333 XFree (name);
13334 }
13335
13336 if (full_name != 0)
13337 fontp->full_name = full_name;
13338 else
13339 fontp->full_name = fontp->name;
13340
13341 fontp->size = font->max_bounds.width;
13342 fontp->height = FONT_HEIGHT (font);
13343
13344 if (NILP (font_names))
13345 {
13346 /* We come here because of a bug of XListFonts mentioned at
13347 the head of this block. Let's store this information in
13348 the cache for x_list_fonts. */
13349 Lisp_Object lispy_name = build_string (fontname);
13350 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13351 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13352 Qnil);
13353
13354 XCDR (dpyinfo->name_list_element)
13355 = Fcons (Fcons (key,
13356 Fcons (Fcons (lispy_full_name,
13357 make_number (fontp->size)),
13358 Qnil)),
13359 XCDR (dpyinfo->name_list_element));
13360 if (full_name)
13361 {
13362 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13363 Qnil);
13364 XCDR (dpyinfo->name_list_element)
13365 = Fcons (Fcons (key,
13366 Fcons (Fcons (lispy_full_name,
13367 make_number (fontp->size)),
13368 Qnil)),
13369 XCDR (dpyinfo->name_list_element));
13370 }
13371 }
13372
13373 /* The slot `encoding' specifies how to map a character
13374 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13375 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13376 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13377 2:0xA020..0xFF7F). For the moment, we don't know which charset
13378 uses this font. So, we set information in fontp->encoding[1]
13379 which is never used by any charset. If mapping can't be
13380 decided, set FONT_ENCODING_NOT_DECIDED. */
13381 fontp->encoding[1]
13382 = (font->max_byte1 == 0
13383 /* 1-byte font */
13384 ? (font->min_char_or_byte2 < 0x80
13385 ? (font->max_char_or_byte2 < 0x80
13386 ? 0 /* 0x20..0x7F */
13387 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13388 : 1) /* 0xA0..0xFF */
13389 /* 2-byte font */
13390 : (font->min_byte1 < 0x80
13391 ? (font->max_byte1 < 0x80
13392 ? (font->min_char_or_byte2 < 0x80
13393 ? (font->max_char_or_byte2 < 0x80
13394 ? 0 /* 0x2020..0x7F7F */
13395 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13396 : 3) /* 0x20A0..0x7FFF */
13397 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13398 : (font->min_char_or_byte2 < 0x80
13399 ? (font->max_char_or_byte2 < 0x80
13400 ? 2 /* 0xA020..0xFF7F */
13401 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13402 : 1))); /* 0xA0A0..0xFFFF */
13403
13404 fontp->baseline_offset
13405 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13406 ? (long) value : 0);
13407 fontp->relative_compose
13408 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13409 ? (long) value : 0);
13410 fontp->default_ascent
13411 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13412 ? (long) value : 0);
13413
13414 /* Set global flag fonts_changed_p to non-zero if the font loaded
13415 has a character with a smaller width than any other character
13416 before, or if the font loaded has a smalle>r height than any
13417 other font loaded before. If this happens, it will make a
13418 glyph matrix reallocation necessary. */
13419 fonts_changed_p = x_compute_min_glyph_bounds (f);
13420 UNBLOCK_INPUT;
13421 return fontp;
13422 }
13423 }
13424
13425
13426 /* Return a pointer to struct font_info of a font named FONTNAME for
13427 frame F. If no such font is loaded, return NULL. */
13428
13429 struct font_info *
13430 x_query_font (f, fontname)
13431 struct frame *f;
13432 register char *fontname;
13433 {
13434 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13435 int i;
13436
13437 for (i = 0; i < dpyinfo->n_fonts; i++)
13438 if (dpyinfo->font_table[i].name
13439 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13440 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13441 return (dpyinfo->font_table + i);
13442 return NULL;
13443 }
13444
13445
13446 /* Find a CCL program for a font specified by FONTP, and set the member
13447 `encoder' of the structure. */
13448
13449 void
13450 x_find_ccl_program (fontp)
13451 struct font_info *fontp;
13452 {
13453 Lisp_Object list, elt;
13454
13455 elt = Qnil;
13456 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13457 {
13458 elt = XCAR (list);
13459 if (CONSP (elt)
13460 && STRINGP (XCAR (elt))
13461 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13462 >= 0)
13463 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13464 >= 0)))
13465 break;
13466 }
13467
13468 if (! NILP (list))
13469 {
13470 struct ccl_program *ccl
13471 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13472
13473 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13474 xfree (ccl);
13475 else
13476 fontp->font_encoder = ccl;
13477 }
13478 }
13479
13480
13481 \f
13482 /***********************************************************************
13483 Initialization
13484 ***********************************************************************/
13485
13486 #ifdef USE_X_TOOLKIT
13487 static XrmOptionDescRec emacs_options[] = {
13488 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13489 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13490
13491 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13492 XrmoptionSepArg, NULL},
13493 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13494
13495 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13496 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13497 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13498 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13499 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13500 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13501 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13502 };
13503 #endif /* USE_X_TOOLKIT */
13504
13505 static int x_initialized;
13506
13507 #ifdef MULTI_KBOARD
13508 /* Test whether two display-name strings agree up to the dot that separates
13509 the screen number from the server number. */
13510 static int
13511 same_x_server (name1, name2)
13512 char *name1, *name2;
13513 {
13514 int seen_colon = 0;
13515 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13516 int system_name_length = strlen (system_name);
13517 int length_until_period = 0;
13518
13519 while (system_name[length_until_period] != 0
13520 && system_name[length_until_period] != '.')
13521 length_until_period++;
13522
13523 /* Treat `unix' like an empty host name. */
13524 if (! strncmp (name1, "unix:", 5))
13525 name1 += 4;
13526 if (! strncmp (name2, "unix:", 5))
13527 name2 += 4;
13528 /* Treat this host's name like an empty host name. */
13529 if (! strncmp (name1, system_name, system_name_length)
13530 && name1[system_name_length] == ':')
13531 name1 += system_name_length;
13532 if (! strncmp (name2, system_name, system_name_length)
13533 && name2[system_name_length] == ':')
13534 name2 += system_name_length;
13535 /* Treat this host's domainless name like an empty host name. */
13536 if (! strncmp (name1, system_name, length_until_period)
13537 && name1[length_until_period] == ':')
13538 name1 += length_until_period;
13539 if (! strncmp (name2, system_name, length_until_period)
13540 && name2[length_until_period] == ':')
13541 name2 += length_until_period;
13542
13543 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13544 {
13545 if (*name1 == ':')
13546 seen_colon++;
13547 if (seen_colon && *name1 == '.')
13548 return 1;
13549 }
13550 return (seen_colon
13551 && (*name1 == '.' || *name1 == '\0')
13552 && (*name2 == '.' || *name2 == '\0'));
13553 }
13554 #endif
13555
13556 struct x_display_info *
13557 x_term_init (display_name, xrm_option, resource_name)
13558 Lisp_Object display_name;
13559 char *xrm_option;
13560 char *resource_name;
13561 {
13562 int connection;
13563 Display *dpy;
13564 struct x_display_info *dpyinfo;
13565 XrmDatabase xrdb;
13566
13567 BLOCK_INPUT;
13568
13569 if (!x_initialized)
13570 {
13571 x_initialize ();
13572 x_initialized = 1;
13573 }
13574
13575 #ifdef USE_X_TOOLKIT
13576 /* weiner@footloose.sps.mot.com reports that this causes
13577 errors with X11R5:
13578 X protocol error: BadAtom (invalid Atom parameter)
13579 on protocol request 18skiloaf.
13580 So let's not use it until R6. */
13581 #ifdef HAVE_X11XTR6
13582 XtSetLanguageProc (NULL, NULL, NULL);
13583 #endif
13584
13585 {
13586 int argc = 0;
13587 char *argv[3];
13588
13589 argv[0] = "";
13590 argc = 1;
13591 if (xrm_option)
13592 {
13593 argv[argc++] = "-xrm";
13594 argv[argc++] = xrm_option;
13595 }
13596 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13597 resource_name, EMACS_CLASS,
13598 emacs_options, XtNumber (emacs_options),
13599 &argc, argv);
13600
13601 #ifdef HAVE_X11XTR6
13602 /* I think this is to compensate for XtSetLanguageProc. */
13603 fixup_locale ();
13604 #endif
13605 }
13606
13607 #else /* not USE_X_TOOLKIT */
13608 #ifdef HAVE_X11R5
13609 XSetLocaleModifiers ("");
13610 #endif
13611 dpy = XOpenDisplay (XSTRING (display_name)->data);
13612 #endif /* not USE_X_TOOLKIT */
13613
13614 /* Detect failure. */
13615 if (dpy == 0)
13616 {
13617 UNBLOCK_INPUT;
13618 return 0;
13619 }
13620
13621 /* We have definitely succeeded. Record the new connection. */
13622
13623 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13624 bzero (dpyinfo, sizeof *dpyinfo);
13625
13626 #ifdef MULTI_KBOARD
13627 {
13628 struct x_display_info *share;
13629 Lisp_Object tail;
13630
13631 for (share = x_display_list, tail = x_display_name_list; share;
13632 share = share->next, tail = XCDR (tail))
13633 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13634 XSTRING (display_name)->data))
13635 break;
13636 if (share)
13637 dpyinfo->kboard = share->kboard;
13638 else
13639 {
13640 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13641 init_kboard (dpyinfo->kboard);
13642 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13643 {
13644 char *vendor = ServerVendor (dpy);
13645 UNBLOCK_INPUT;
13646 dpyinfo->kboard->Vsystem_key_alist
13647 = call1 (Qvendor_specific_keysyms,
13648 build_string (vendor ? vendor : ""));
13649 BLOCK_INPUT;
13650 }
13651
13652 dpyinfo->kboard->next_kboard = all_kboards;
13653 all_kboards = dpyinfo->kboard;
13654 /* Don't let the initial kboard remain current longer than necessary.
13655 That would cause problems if a file loaded on startup tries to
13656 prompt in the mini-buffer. */
13657 if (current_kboard == initial_kboard)
13658 current_kboard = dpyinfo->kboard;
13659 }
13660 dpyinfo->kboard->reference_count++;
13661 }
13662 #endif
13663
13664 /* Put this display on the chain. */
13665 dpyinfo->next = x_display_list;
13666 x_display_list = dpyinfo;
13667
13668 /* Put it on x_display_name_list as well, to keep them parallel. */
13669 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13670 x_display_name_list);
13671 dpyinfo->name_list_element = XCAR (x_display_name_list);
13672
13673 dpyinfo->display = dpy;
13674
13675 #if 0
13676 XSetAfterFunction (x_current_display, x_trace_wire);
13677 #endif /* ! 0 */
13678
13679 dpyinfo->x_id_name
13680 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13681 + STRING_BYTES (XSTRING (Vsystem_name))
13682 + 2);
13683 sprintf (dpyinfo->x_id_name, "%s@%s",
13684 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13685
13686 /* Figure out which modifier bits mean what. */
13687 x_find_modifier_meanings (dpyinfo);
13688
13689 /* Get the scroll bar cursor. */
13690 dpyinfo->vertical_scroll_bar_cursor
13691 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13692
13693 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13694 resource_name, EMACS_CLASS);
13695 #ifdef HAVE_XRMSETDATABASE
13696 XrmSetDatabase (dpyinfo->display, xrdb);
13697 #else
13698 dpyinfo->display->db = xrdb;
13699 #endif
13700 /* Put the rdb where we can find it in a way that works on
13701 all versions. */
13702 dpyinfo->xrdb = xrdb;
13703
13704 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13705 DefaultScreen (dpyinfo->display));
13706 select_visual (dpyinfo);
13707 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13708 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13709 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13710 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13711 dpyinfo->grabbed = 0;
13712 dpyinfo->reference_count = 0;
13713 dpyinfo->icon_bitmap_id = -1;
13714 dpyinfo->font_table = NULL;
13715 dpyinfo->n_fonts = 0;
13716 dpyinfo->font_table_size = 0;
13717 dpyinfo->bitmaps = 0;
13718 dpyinfo->bitmaps_size = 0;
13719 dpyinfo->bitmaps_last = 0;
13720 dpyinfo->scratch_cursor_gc = 0;
13721 dpyinfo->mouse_face_mouse_frame = 0;
13722 dpyinfo->mouse_face_deferred_gc = 0;
13723 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13724 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13725 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13726 dpyinfo->mouse_face_window = Qnil;
13727 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13728 dpyinfo->mouse_face_defer = 0;
13729 dpyinfo->x_focus_frame = 0;
13730 dpyinfo->x_focus_event_frame = 0;
13731 dpyinfo->x_highlight_frame = 0;
13732 dpyinfo->image_cache = make_image_cache ();
13733
13734 /* See if a private colormap is requested. */
13735 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13736 {
13737 if (dpyinfo->visual->class == PseudoColor)
13738 {
13739 Lisp_Object value;
13740 value = display_x_get_resource (dpyinfo,
13741 build_string ("privateColormap"),
13742 build_string ("PrivateColormap"),
13743 Qnil, Qnil);
13744 if (STRINGP (value)
13745 && (!strcmp (XSTRING (value)->data, "true")
13746 || !strcmp (XSTRING (value)->data, "on")))
13747 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13748 }
13749 }
13750 else
13751 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13752 dpyinfo->visual, AllocNone);
13753
13754 {
13755 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13756 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13757 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13758 dpyinfo->resy = pixels * 25.4 / mm;
13759 pixels = DisplayWidth (dpyinfo->display, screen_number);
13760 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13761 dpyinfo->resx = pixels * 25.4 / mm;
13762 }
13763
13764 dpyinfo->Xatom_wm_protocols
13765 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13766 dpyinfo->Xatom_wm_take_focus
13767 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13768 dpyinfo->Xatom_wm_save_yourself
13769 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13770 dpyinfo->Xatom_wm_delete_window
13771 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13772 dpyinfo->Xatom_wm_change_state
13773 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13774 dpyinfo->Xatom_wm_configure_denied
13775 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13776 dpyinfo->Xatom_wm_window_moved
13777 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13778 dpyinfo->Xatom_editres
13779 = XInternAtom (dpyinfo->display, "Editres", False);
13780 dpyinfo->Xatom_CLIPBOARD
13781 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13782 dpyinfo->Xatom_TIMESTAMP
13783 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13784 dpyinfo->Xatom_TEXT
13785 = XInternAtom (dpyinfo->display, "TEXT", False);
13786 dpyinfo->Xatom_COMPOUND_TEXT
13787 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13788 dpyinfo->Xatom_DELETE
13789 = XInternAtom (dpyinfo->display, "DELETE", False);
13790 dpyinfo->Xatom_MULTIPLE
13791 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13792 dpyinfo->Xatom_INCR
13793 = XInternAtom (dpyinfo->display, "INCR", False);
13794 dpyinfo->Xatom_EMACS_TMP
13795 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13796 dpyinfo->Xatom_TARGETS
13797 = XInternAtom (dpyinfo->display, "TARGETS", False);
13798 dpyinfo->Xatom_NULL
13799 = XInternAtom (dpyinfo->display, "NULL", False);
13800 dpyinfo->Xatom_ATOM_PAIR
13801 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13802 /* For properties of font. */
13803 dpyinfo->Xatom_PIXEL_SIZE
13804 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13805 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13806 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13807 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13808 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13809 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13810 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13811
13812 /* Ghostscript support. */
13813 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13814 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13815
13816 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13817 False);
13818
13819 dpyinfo->cut_buffers_initialized = 0;
13820
13821 connection = ConnectionNumber (dpyinfo->display);
13822 dpyinfo->connection = connection;
13823
13824 {
13825 char null_bits[1];
13826
13827 null_bits[0] = 0x00;
13828
13829 dpyinfo->null_pixel
13830 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13831 null_bits, 1, 1, (long) 0, (long) 0,
13832 1);
13833 }
13834
13835 {
13836 extern int gray_bitmap_width, gray_bitmap_height;
13837 extern char *gray_bitmap_bits;
13838 dpyinfo->gray
13839 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13840 gray_bitmap_bits,
13841 gray_bitmap_width, gray_bitmap_height,
13842 (unsigned long) 1, (unsigned long) 0, 1);
13843 }
13844
13845 #ifdef HAVE_X_I18N
13846 xim_initialize (dpyinfo, resource_name);
13847 #endif
13848
13849 #ifdef subprocesses
13850 /* This is only needed for distinguishing keyboard and process input. */
13851 if (connection != 0)
13852 add_keyboard_wait_descriptor (connection);
13853 #endif
13854
13855 #ifndef F_SETOWN_BUG
13856 #ifdef F_SETOWN
13857 #ifdef F_SETOWN_SOCK_NEG
13858 /* stdin is a socket here */
13859 fcntl (connection, F_SETOWN, -getpid ());
13860 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13861 fcntl (connection, F_SETOWN, getpid ());
13862 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13863 #endif /* ! defined (F_SETOWN) */
13864 #endif /* F_SETOWN_BUG */
13865
13866 #ifdef SIGIO
13867 if (interrupt_input)
13868 init_sigio (connection);
13869 #endif /* ! defined (SIGIO) */
13870
13871 #ifdef USE_LUCID
13872 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13873 /* Make sure that we have a valid font for dialog boxes
13874 so that Xt does not crash. */
13875 {
13876 Display *dpy = dpyinfo->display;
13877 XrmValue d, fr, to;
13878 Font font;
13879 int count;
13880
13881 d.addr = (XPointer)&dpy;
13882 d.size = sizeof (Display *);
13883 fr.addr = XtDefaultFont;
13884 fr.size = sizeof (XtDefaultFont);
13885 to.size = sizeof (Font *);
13886 to.addr = (XPointer)&font;
13887 count = x_catch_errors (dpy);
13888 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13889 abort ();
13890 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13891 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13892 x_uncatch_errors (dpy, count);
13893 }
13894 #endif
13895 #endif
13896
13897 /* See if we should run in synchronous mode. This is useful
13898 for debugging X code. */
13899 {
13900 Lisp_Object value;
13901 value = display_x_get_resource (dpyinfo,
13902 build_string ("synchronous"),
13903 build_string ("Synchronous"),
13904 Qnil, Qnil);
13905 if (STRINGP (value)
13906 && (!strcmp (XSTRING (value)->data, "true")
13907 || !strcmp (XSTRING (value)->data, "on")))
13908 XSynchronize (dpyinfo->display, True);
13909 }
13910
13911 UNBLOCK_INPUT;
13912
13913 return dpyinfo;
13914 }
13915 \f
13916 /* Get rid of display DPYINFO, assuming all frames are already gone,
13917 and without sending any more commands to the X server. */
13918
13919 void
13920 x_delete_display (dpyinfo)
13921 struct x_display_info *dpyinfo;
13922 {
13923 delete_keyboard_wait_descriptor (dpyinfo->connection);
13924
13925 /* Discard this display from x_display_name_list and x_display_list.
13926 We can't use Fdelq because that can quit. */
13927 if (! NILP (x_display_name_list)
13928 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13929 x_display_name_list = XCDR (x_display_name_list);
13930 else
13931 {
13932 Lisp_Object tail;
13933
13934 tail = x_display_name_list;
13935 while (CONSP (tail) && CONSP (XCDR (tail)))
13936 {
13937 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13938 {
13939 XCDR (tail) = XCDR (XCDR (tail));
13940 break;
13941 }
13942 tail = XCDR (tail);
13943 }
13944 }
13945
13946 if (next_noop_dpyinfo == dpyinfo)
13947 next_noop_dpyinfo = dpyinfo->next;
13948
13949 if (x_display_list == dpyinfo)
13950 x_display_list = dpyinfo->next;
13951 else
13952 {
13953 struct x_display_info *tail;
13954
13955 for (tail = x_display_list; tail; tail = tail->next)
13956 if (tail->next == dpyinfo)
13957 tail->next = tail->next->next;
13958 }
13959
13960 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13961 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13962 XrmDestroyDatabase (dpyinfo->xrdb);
13963 #endif
13964 #endif
13965 #ifdef MULTI_KBOARD
13966 if (--dpyinfo->kboard->reference_count == 0)
13967 delete_kboard (dpyinfo->kboard);
13968 #endif
13969 #ifdef HAVE_X_I18N
13970 if (dpyinfo->xim)
13971 xim_close_dpy (dpyinfo);
13972 #endif
13973
13974 xfree (dpyinfo->font_table);
13975 xfree (dpyinfo->x_id_name);
13976 xfree (dpyinfo->color_cells);
13977 xfree (dpyinfo);
13978 }
13979
13980 \f
13981 /* Set up use of X before we make the first connection. */
13982
13983 static struct redisplay_interface x_redisplay_interface =
13984 {
13985 x_produce_glyphs,
13986 x_write_glyphs,
13987 x_insert_glyphs,
13988 x_clear_end_of_line,
13989 x_scroll_run,
13990 x_after_update_window_line,
13991 x_update_window_begin,
13992 x_update_window_end,
13993 XTcursor_to,
13994 x_flush,
13995 x_clear_mouse_face,
13996 x_get_glyph_overhangs,
13997 x_fix_overlapping_area
13998 };
13999
14000 void
14001 x_initialize ()
14002 {
14003 rif = &x_redisplay_interface;
14004
14005 clear_frame_hook = x_clear_frame;
14006 ins_del_lines_hook = x_ins_del_lines;
14007 change_line_highlight_hook = x_change_line_highlight;
14008 delete_glyphs_hook = x_delete_glyphs;
14009 ring_bell_hook = XTring_bell;
14010 reset_terminal_modes_hook = XTreset_terminal_modes;
14011 set_terminal_modes_hook = XTset_terminal_modes;
14012 update_begin_hook = x_update_begin;
14013 update_end_hook = x_update_end;
14014 set_terminal_window_hook = XTset_terminal_window;
14015 read_socket_hook = XTread_socket;
14016 frame_up_to_date_hook = XTframe_up_to_date;
14017 reassert_line_highlight_hook = XTreassert_line_highlight;
14018 mouse_position_hook = XTmouse_position;
14019 frame_rehighlight_hook = XTframe_rehighlight;
14020 frame_raise_lower_hook = XTframe_raise_lower;
14021 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14022 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14023 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14024 judge_scroll_bars_hook = XTjudge_scroll_bars;
14025 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14026
14027 scroll_region_ok = 1; /* we'll scroll partial frames */
14028 char_ins_del_ok = 1;
14029 line_ins_del_ok = 1; /* we'll just blt 'em */
14030 fast_clear_end_of_line = 1; /* X does this well */
14031 memory_below_frame = 0; /* we don't remember what scrolls
14032 off the bottom */
14033 baud_rate = 19200;
14034
14035 x_noop_count = 0;
14036 last_tool_bar_item = -1;
14037 any_help_event_p = 0;
14038
14039 /* Try to use interrupt input; if we can't, then start polling. */
14040 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14041
14042 #ifdef USE_X_TOOLKIT
14043 XtToolkitInitialize ();
14044 Xt_app_con = XtCreateApplicationContext ();
14045 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14046
14047 /* Install an asynchronous timer that processes Xt timeout events
14048 every 0.1s. This is necessary because some widget sets use
14049 timeouts internally, for example the LessTif menu bar, or the
14050 Xaw3d scroll bar. When Xt timouts aren't processed, these
14051 widgets don't behave normally. */
14052 {
14053 EMACS_TIME interval;
14054 EMACS_SET_SECS_USECS (interval, 0, 100000);
14055 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14056 }
14057 #endif
14058
14059 #ifdef USE_TOOLKIT_SCROLL_BARS
14060 xaw3d_arrow_scroll = False;
14061 xaw3d_pick_top = True;
14062 #endif
14063
14064 /* Note that there is no real way portable across R3/R4 to get the
14065 original error handler. */
14066 XSetErrorHandler (x_error_handler);
14067 XSetIOErrorHandler (x_io_error_quitter);
14068
14069 /* Disable Window Change signals; they are handled by X events. */
14070 #ifdef SIGWINCH
14071 signal (SIGWINCH, SIG_DFL);
14072 #endif /* ! defined (SIGWINCH) */
14073
14074 signal (SIGPIPE, x_connection_signal);
14075 }
14076
14077
14078 void
14079 syms_of_xterm ()
14080 {
14081 staticpro (&x_error_message_string);
14082 x_error_message_string = Qnil;
14083
14084 staticpro (&x_display_name_list);
14085 x_display_name_list = Qnil;
14086
14087 staticpro (&last_mouse_scroll_bar);
14088 last_mouse_scroll_bar = Qnil;
14089
14090 staticpro (&Qvendor_specific_keysyms);
14091 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14092
14093 staticpro (&last_mouse_press_frame);
14094 last_mouse_press_frame = Qnil;
14095
14096 help_echo = Qnil;
14097 staticpro (&help_echo);
14098 help_echo_object = Qnil;
14099 staticpro (&help_echo_object);
14100 help_echo_window = Qnil;
14101 staticpro (&help_echo_window);
14102 previous_help_echo = Qnil;
14103 staticpro (&previous_help_echo);
14104 help_echo_pos = -1;
14105
14106 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14107 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14108 For example, if a block cursor is over a tab, it will be drawn as\n\
14109 wide as that tab on the display.");
14110 x_stretch_cursor_p = 0;
14111
14112 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14113 "What X toolkit scroll bars Emacs uses.\n\
14114 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14115 Otherwise, value is a symbol describing the X toolkit.");
14116 #ifdef USE_TOOLKIT_SCROLL_BARS
14117 #ifdef USE_MOTIF
14118 Vx_toolkit_scroll_bars = intern ("motif");
14119 #elif defined HAVE_XAW3D
14120 Vx_toolkit_scroll_bars = intern ("xaw3d");
14121 #else
14122 Vx_toolkit_scroll_bars = intern ("xaw");
14123 #endif
14124 #else
14125 Vx_toolkit_scroll_bars = Qnil;
14126 #endif
14127
14128 staticpro (&last_mouse_motion_frame);
14129 last_mouse_motion_frame = Qnil;
14130 }
14131
14132 #endif /* not HAVE_X_WINDOWS */