]> code.delx.au - gnu-emacs/blob - src/xterm.c
47b3f532e9223ed2f05d80266ac9311733cc440e
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 /* 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-zero means Emacs uses toolkit scroll bars. */
234
235 int x_toolkit_scroll_bars_p;
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 const XColor *x_color_cells P_ ((struct frame *, int *));
400 static void x_update_window_end P_ ((struct window *, int, int));
401 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402 void x_delete_display P_ ((struct x_display_info *));
403 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405 static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407 static void set_output_cursor P_ ((struct cursor_pos *));
408 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *));
410 static void note_mode_line_highlight P_ ((struct window *, int, int));
411 static void note_mouse_highlight P_ ((struct frame *, int, int));
412 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
413 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
414 static void show_mouse_face P_ ((struct x_display_info *,
415 enum draw_glyphs_face));
416 static int x_io_error_quitter P_ ((Display *));
417 int x_catch_errors P_ ((Display *));
418 void x_uncatch_errors P_ ((Display *, int));
419 void x_lower_frame P_ ((struct frame *));
420 void x_scroll_bar_clear P_ ((struct frame *));
421 int x_had_errors_p P_ ((Display *));
422 void x_wm_set_size_hint P_ ((struct frame *, long, int));
423 void x_raise_frame P_ ((struct frame *));
424 void x_set_window_size P_ ((struct frame *, int, int, int));
425 void x_wm_set_window_state P_ ((struct frame *, int));
426 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
427 void x_initialize P_ ((void));
428 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
429 static int x_compute_min_glyph_bounds P_ ((struct frame *));
430 static void x_draw_phys_cursor_glyph P_ ((struct window *,
431 struct glyph_row *,
432 enum draw_glyphs_face));
433 static void x_update_end P_ ((struct frame *));
434 static void XTframe_up_to_date P_ ((struct frame *));
435 static void XTreassert_line_highlight P_ ((int, int));
436 static void x_change_line_highlight P_ ((int, int, int, int));
437 static void XTset_terminal_modes P_ ((void));
438 static void XTreset_terminal_modes P_ ((void));
439 static void XTcursor_to P_ ((int, int, int, int));
440 static void x_write_glyphs P_ ((struct glyph *, int));
441 static void x_clear_end_of_line P_ ((int));
442 static void x_clear_frame P_ ((void));
443 static void x_clear_cursor P_ ((struct window *));
444 static void frame_highlight P_ ((struct frame *));
445 static void frame_unhighlight P_ ((struct frame *));
446 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
447 static void XTframe_rehighlight P_ ((struct frame *));
448 static void x_frame_rehighlight P_ ((struct x_display_info *));
449 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
450 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
451 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
452 XRectangle *));
453 static void expose_frame P_ ((struct frame *, int, int, int, int));
454 static void expose_window_tree P_ ((struct window *, XRectangle *));
455 static void expose_window P_ ((struct window *, XRectangle *));
456 static void expose_area P_ ((struct window *, struct glyph_row *,
457 XRectangle *, enum glyph_row_area));
458 static void expose_line P_ ((struct window *, struct glyph_row *,
459 XRectangle *));
460 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
461 static void x_update_window_cursor P_ ((struct window *, int));
462 static void x_erase_phys_cursor P_ ((struct window *));
463 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
464 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
465 enum bitmap_type));
466
467 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
468 GC, int));
469 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
470 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
471 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
472 static void x_flush P_ ((struct frame *f));
473 static void x_update_begin P_ ((struct frame *));
474 static void x_update_window_begin P_ ((struct window *));
475 static void x_draw_vertical_border P_ ((struct window *));
476 static void x_after_update_window_line P_ ((struct glyph_row *));
477 static INLINE void take_vertical_position_into_account P_ ((struct it *));
478 static void x_produce_stretch_glyph P_ ((struct it *));
479
480
481 /* Flush display of frame F, or of all frames if F is null. */
482
483 static void
484 x_flush (f)
485 struct frame *f;
486 {
487 BLOCK_INPUT;
488 if (f == NULL)
489 {
490 Lisp_Object rest, frame;
491 FOR_EACH_FRAME (rest, frame)
492 x_flush (XFRAME (frame));
493 }
494 else if (FRAME_X_P (f))
495 XFlush (FRAME_X_DISPLAY (f));
496 UNBLOCK_INPUT;
497 }
498
499
500 /* Remove calls to XFlush by defining XFlush to an empty replacement.
501 Calls to XFlush should be unnecessary because the X output buffer
502 is flushed automatically as needed by calls to XPending,
503 XNextEvent, or XWindowEvent according to the XFlush man page.
504 XTread_socket calls XPending. Removing XFlush improves
505 performance. */
506
507 #define XFlush(DISPLAY) (void) 0
508
509 \f
510 /***********************************************************************
511 Debugging
512 ***********************************************************************/
513
514 #if 0
515
516 /* This is a function useful for recording debugging information about
517 the sequence of occurrences in this file. */
518
519 struct record
520 {
521 char *locus;
522 int type;
523 };
524
525 struct record event_record[100];
526
527 int event_record_index;
528
529 record_event (locus, type)
530 char *locus;
531 int type;
532 {
533 if (event_record_index == sizeof (event_record) / sizeof (struct record))
534 event_record_index = 0;
535
536 event_record[event_record_index].locus = locus;
537 event_record[event_record_index].type = type;
538 event_record_index++;
539 }
540
541 #endif /* 0 */
542
543
544 \f
545 /* Return the struct x_display_info corresponding to DPY. */
546
547 struct x_display_info *
548 x_display_info_for_display (dpy)
549 Display *dpy;
550 {
551 struct x_display_info *dpyinfo;
552
553 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
554 if (dpyinfo->display == dpy)
555 return dpyinfo;
556
557 return 0;
558 }
559
560
561 \f
562 /***********************************************************************
563 Starting and ending an update
564 ***********************************************************************/
565
566 /* Start an update of frame F. This function is installed as a hook
567 for update_begin, i.e. it is called when update_begin is called.
568 This function is called prior to calls to x_update_window_begin for
569 each window being updated. Currently, there is nothing to do here
570 because all interesting stuff is done on a window basis. */
571
572 static void
573 x_update_begin (f)
574 struct frame *f;
575 {
576 /* Nothing to do. */
577 }
578
579
580 /* Start update of window W. Set the global variable updated_window
581 to the window being updated and set output_cursor to the cursor
582 position of W. */
583
584 static void
585 x_update_window_begin (w)
586 struct window *w;
587 {
588 struct frame *f = XFRAME (WINDOW_FRAME (w));
589 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
590
591 updated_window = w;
592 set_output_cursor (&w->cursor);
593
594 BLOCK_INPUT;
595
596 if (f == display_info->mouse_face_mouse_frame)
597 {
598 /* Don't do highlighting for mouse motion during the update. */
599 display_info->mouse_face_defer = 1;
600
601 /* If F needs to be redrawn, simply forget about any prior mouse
602 highlighting. */
603 if (FRAME_GARBAGED_P (f))
604 display_info->mouse_face_window = Qnil;
605
606 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
607 their mouse_face_p flag set, which means that they are always
608 unequal to rows in a desired matrix which never have that
609 flag set. So, rows containing mouse-face glyphs are never
610 scrolled, and we don't have to switch the mouse highlight off
611 here to prevent it from being scrolled. */
612
613 /* Can we tell that this update does not affect the window
614 where the mouse highlight is? If so, no need to turn off.
615 Likewise, don't do anything if the frame is garbaged;
616 in that case, the frame's current matrix that we would use
617 is all wrong, and we will redisplay that line anyway. */
618 if (!NILP (display_info->mouse_face_window)
619 && w == XWINDOW (display_info->mouse_face_window))
620 {
621 int i;
622
623 for (i = 0; i < w->desired_matrix->nrows; ++i)
624 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
625 break;
626
627 if (i < w->desired_matrix->nrows)
628 clear_mouse_face (display_info);
629 }
630 #endif /* 0 */
631 }
632
633 UNBLOCK_INPUT;
634 }
635
636
637 /* Draw a vertical window border to the right of window W if W doesn't
638 have vertical scroll bars. */
639
640 static void
641 x_draw_vertical_border (w)
642 struct window *w;
643 {
644 struct frame *f = XFRAME (WINDOW_FRAME (w));
645
646 /* Redraw borders between horizontally adjacent windows. Don't
647 do it for frames with vertical scroll bars because either the
648 right scroll bar of a window, or the left scroll bar of its
649 neighbor will suffice as a border. */
650 if (!WINDOW_RIGHTMOST_P (w)
651 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
652 {
653 int x0, x1, y0, y1;
654
655 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
656 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
657 y1 -= 1;
658
659 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
660 f->output_data.x->normal_gc, x1, y0, x1, y1);
661 }
662 }
663
664
665 /* End update of window W (which is equal to updated_window).
666
667 Draw vertical borders between horizontally adjacent windows, and
668 display W's cursor if CURSOR_ON_P is non-zero.
669
670 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
671 glyphs in mouse-face were overwritten. In that case we have to
672 make sure that the mouse-highlight is properly redrawn.
673
674 W may be a menu bar pseudo-window in case we don't have X toolkit
675 support. Such windows don't have a cursor, so don't display it
676 here. */
677
678 static void
679 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
680 struct window *w;
681 int cursor_on_p, mouse_face_overwritten_p;
682 {
683 if (!w->pseudo_window_p)
684 {
685 struct x_display_info *dpyinfo
686 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
687
688 BLOCK_INPUT;
689
690 /* If a row with mouse-face was overwritten, arrange for
691 XTframe_up_to_date to redisplay the mouse highlight. */
692 if (mouse_face_overwritten_p)
693 {
694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
696 dpyinfo->mouse_face_window = Qnil;
697 }
698
699 if (cursor_on_p)
700 x_display_and_set_cursor (w, 1, output_cursor.hpos,
701 output_cursor.vpos,
702 output_cursor.x, output_cursor.y);
703
704 x_draw_vertical_border (w);
705 UNBLOCK_INPUT;
706 }
707
708 updated_window = NULL;
709 }
710
711
712 /* End update of frame F. This function is installed as a hook in
713 update_end. */
714
715 static void
716 x_update_end (f)
717 struct frame *f;
718 {
719 /* Mouse highlight may be displayed again. */
720 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
721
722 BLOCK_INPUT;
723 XFlush (FRAME_X_DISPLAY (f));
724 UNBLOCK_INPUT;
725 }
726
727
728 /* This function is called from various places in xdisp.c whenever a
729 complete update has been performed. The global variable
730 updated_window is not available here. */
731
732 static void
733 XTframe_up_to_date (f)
734 struct frame *f;
735 {
736 if (FRAME_X_P (f))
737 {
738 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
739
740 if (dpyinfo->mouse_face_deferred_gc
741 || f == dpyinfo->mouse_face_mouse_frame)
742 {
743 BLOCK_INPUT;
744 if (dpyinfo->mouse_face_mouse_frame)
745 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
746 dpyinfo->mouse_face_mouse_x,
747 dpyinfo->mouse_face_mouse_y);
748 dpyinfo->mouse_face_deferred_gc = 0;
749 UNBLOCK_INPUT;
750 }
751 }
752 }
753
754
755 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
756 arrow bitmaps, or clear the areas where they would be displayed
757 before DESIRED_ROW is made current. The window being updated is
758 found in updated_window. This function It is called from
759 update_window_line only if it is known that there are differences
760 between bitmaps to be drawn between current row and DESIRED_ROW. */
761
762 static void
763 x_after_update_window_line (desired_row)
764 struct glyph_row *desired_row;
765 {
766 struct window *w = updated_window;
767
768 xassert (w);
769
770 if (!desired_row->mode_line_p && !w->pseudo_window_p)
771 {
772 BLOCK_INPUT;
773 x_draw_row_bitmaps (w, desired_row);
774
775 /* When a window has disappeared, make sure that no rest of
776 full-width rows stays visible in the internal border. */
777 if (windows_or_buffers_changed)
778 {
779 struct frame *f = XFRAME (w->frame);
780 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
781 int height = desired_row->visible_height;
782 int x = (window_box_right (w, -1)
783 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
784 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
785
786 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
787 x, y, width, height, False);
788 }
789
790 UNBLOCK_INPUT;
791 }
792 }
793
794
795 /* Draw the bitmap WHICH in one of the areas to the left or right of
796 window W. ROW is the glyph row for which to display the bitmap; it
797 determines the vertical position at which the bitmap has to be
798 drawn. */
799
800 static void
801 x_draw_bitmap (w, row, which)
802 struct window *w;
803 struct glyph_row *row;
804 enum bitmap_type which;
805 {
806 struct frame *f = XFRAME (WINDOW_FRAME (w));
807 Display *display = FRAME_X_DISPLAY (f);
808 Window window = FRAME_X_WINDOW (f);
809 int x, y, wd, h, dy;
810 unsigned char *bits;
811 Pixmap pixmap;
812 GC gc = f->output_data.x->normal_gc;
813 struct face *face;
814 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
815
816 /* Must clip because of partially visible lines. */
817 x_clip_to_row (w, row, gc, 1);
818
819 switch (which)
820 {
821 case LEFT_TRUNCATION_BITMAP:
822 wd = left_width;
823 h = left_height;
824 bits = left_bits;
825 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
826 - wd
827 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
828 break;
829
830 case OVERLAY_ARROW_BITMAP:
831 wd = left_width;
832 h = left_height;
833 bits = ov_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
837 break;
838
839 case RIGHT_TRUNCATION_BITMAP:
840 wd = right_width;
841 h = right_height;
842 bits = right_bits;
843 x = window_box_right (w, -1);
844 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
845 break;
846
847 case CONTINUED_LINE_BITMAP:
848 wd = right_width;
849 h = right_height;
850 bits = continued_bits;
851 x = window_box_right (w, -1);
852 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
853 break;
854
855 case CONTINUATION_LINE_BITMAP:
856 wd = continuation_width;
857 h = continuation_height;
858 bits = continuation_bits;
859 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
860 - wd
861 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
862 break;
863
864 case ZV_LINE_BITMAP:
865 wd = zv_width;
866 h = zv_height;
867 bits = zv_bits;
868 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
869 - wd
870 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
871 break;
872
873 default:
874 abort ();
875 }
876
877 /* Convert to frame coordinates. Set dy to the offset in the row to
878 start drawing the bitmap. */
879 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
880 dy = (row->height - h) / 2;
881
882 /* Draw the bitmap. I believe these small pixmaps can be cached
883 by the server. */
884 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
885 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
886 face->foreground,
887 face->background, depth);
888 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
889 XFreePixmap (display, pixmap);
890 XSetClipMask (display, gc, None);
891 }
892
893
894 /* Draw flags bitmaps for glyph row ROW on window W. Call this
895 function with input blocked. */
896
897 static void
898 x_draw_row_bitmaps (w, row)
899 struct window *w;
900 struct glyph_row *row;
901 {
902 struct frame *f = XFRAME (w->frame);
903 enum bitmap_type bitmap;
904 struct face *face;
905 int header_line_height = -1;
906
907 xassert (interrupt_input_blocked);
908
909 /* If row is completely invisible, because of vscrolling, we
910 don't have to draw anything. */
911 if (row->visible_height <= 0)
912 return;
913
914 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
915 PREPARE_FACE_FOR_DISPLAY (f, face);
916
917 /* Decide which bitmap to draw at the left side. */
918 if (row->overlay_arrow_p)
919 bitmap = OVERLAY_ARROW_BITMAP;
920 else if (row->truncated_on_left_p)
921 bitmap = LEFT_TRUNCATION_BITMAP;
922 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
923 bitmap = CONTINUATION_LINE_BITMAP;
924 else if (row->indicate_empty_line_p)
925 bitmap = ZV_LINE_BITMAP;
926 else
927 bitmap = NO_BITMAP;
928
929 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
930 the flags area. */
931 if (bitmap == NO_BITMAP
932 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
933 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
934 {
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 int left = window_box_left (w, -1);
940
941 if (header_line_height < 0)
942 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
943
944 /* In case the same realized face is used for bitmap areas and
945 for something displayed in the text (e.g. face `region' on
946 mono-displays, the fill style may have been changed to
947 FillSolid in x_draw_glyph_string_background. */
948 if (face->stipple)
949 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
950 else
951 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
952
953 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
954 face->gc,
955 (left
956 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
957 + border),
958 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
959 row->y)),
960 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
961 row->visible_height);
962 if (!face->stipple)
963 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
964 }
965
966 /* Draw the left bitmap. */
967 if (bitmap != NO_BITMAP)
968 x_draw_bitmap (w, row, bitmap);
969
970 /* Decide which bitmap to draw at the right side. */
971 if (row->truncated_on_right_p)
972 bitmap = RIGHT_TRUNCATION_BITMAP;
973 else if (row->continued_p)
974 bitmap = CONTINUED_LINE_BITMAP;
975 else
976 bitmap = NO_BITMAP;
977
978 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
979 the flags area. */
980 if (bitmap == NO_BITMAP
981 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
982 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
983 {
984 int right = window_box_right (w, -1);
985
986 if (header_line_height < 0)
987 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
988
989 /* In case the same realized face is used for bitmap areas and
990 for something displayed in the text (e.g. face `region' on
991 mono-displays, the fill style may have been changed to
992 FillSolid in x_draw_glyph_string_background. */
993 if (face->stipple)
994 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
995 else
996 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
998 face->gc,
999 right,
1000 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1001 row->y)),
1002 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1003 row->visible_height);
1004 if (!face->stipple)
1005 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1006 }
1007
1008 /* Draw the right bitmap. */
1009 if (bitmap != NO_BITMAP)
1010 x_draw_bitmap (w, row, bitmap);
1011 }
1012
1013 \f
1014 /***********************************************************************
1015 Line Highlighting
1016 ***********************************************************************/
1017
1018 /* External interface to control of standout mode. Not used for X
1019 frames. Aborts when called. */
1020
1021 static void
1022 XTreassert_line_highlight (new, vpos)
1023 int new, vpos;
1024 {
1025 abort ();
1026 }
1027
1028
1029 /* Call this when about to modify line at position VPOS and change
1030 whether it is highlighted. Not used for X frames. Aborts when
1031 called. */
1032
1033 static void
1034 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1035 int new_highlight, vpos, y, first_unused_hpos;
1036 {
1037 abort ();
1038 }
1039
1040
1041 /* This is called when starting Emacs and when restarting after
1042 suspend. When starting Emacs, no X window is mapped. And nothing
1043 must be done to Emacs's own window if it is suspended (though that
1044 rarely happens). */
1045
1046 static void
1047 XTset_terminal_modes ()
1048 {
1049 }
1050
1051 /* This is called when exiting or suspending Emacs. Exiting will make
1052 the X-windows go away, and suspending requires no action. */
1053
1054 static void
1055 XTreset_terminal_modes ()
1056 {
1057 }
1058
1059
1060 \f
1061 /***********************************************************************
1062 Output Cursor
1063 ***********************************************************************/
1064
1065 /* Set the global variable output_cursor to CURSOR. All cursor
1066 positions are relative to updated_window. */
1067
1068 static void
1069 set_output_cursor (cursor)
1070 struct cursor_pos *cursor;
1071 {
1072 output_cursor.hpos = cursor->hpos;
1073 output_cursor.vpos = cursor->vpos;
1074 output_cursor.x = cursor->x;
1075 output_cursor.y = cursor->y;
1076 }
1077
1078
1079 /* Set a nominal cursor position.
1080
1081 HPOS and VPOS are column/row positions in a window glyph matrix. X
1082 and Y are window text area relative pixel positions.
1083
1084 If this is done during an update, updated_window will contain the
1085 window that is being updated and the position is the future output
1086 cursor position for that window. If updated_window is null, use
1087 selected_window and display the cursor at the given position. */
1088
1089 static void
1090 XTcursor_to (vpos, hpos, y, x)
1091 int vpos, hpos, y, x;
1092 {
1093 struct window *w;
1094
1095 /* If updated_window is not set, work on selected_window. */
1096 if (updated_window)
1097 w = updated_window;
1098 else
1099 w = XWINDOW (selected_window);
1100
1101 /* Set the output cursor. */
1102 output_cursor.hpos = hpos;
1103 output_cursor.vpos = vpos;
1104 output_cursor.x = x;
1105 output_cursor.y = y;
1106
1107 /* If not called as part of an update, really display the cursor.
1108 This will also set the cursor position of W. */
1109 if (updated_window == NULL)
1110 {
1111 BLOCK_INPUT;
1112 x_display_cursor (w, 1, hpos, vpos, x, y);
1113 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1114 UNBLOCK_INPUT;
1115 }
1116 }
1117
1118
1119 \f
1120 /***********************************************************************
1121 Display Iterator
1122 ***********************************************************************/
1123
1124 /* Function prototypes of this page. */
1125
1126 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1127 struct glyph *,
1128 XChar2b *,
1129 int *));
1130 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1131 int, XChar2b *, int));
1132 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1133 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1134 static void x_append_glyph P_ ((struct it *));
1135 static void x_append_composite_glyph P_ ((struct it *));
1136 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1137 int, int, double));
1138 static void x_produce_glyphs P_ ((struct it *));
1139 static void x_produce_image_glyph P_ ((struct it *it));
1140
1141
1142 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1143 is not contained in the font. */
1144
1145 static INLINE XCharStruct *
1146 x_per_char_metric (font, char2b)
1147 XFontStruct *font;
1148 XChar2b *char2b;
1149 {
1150 /* The result metric information. */
1151 XCharStruct *pcm = NULL;
1152
1153 xassert (font && char2b);
1154
1155 if (font->per_char != NULL)
1156 {
1157 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1158 {
1159 /* min_char_or_byte2 specifies the linear character index
1160 corresponding to the first element of the per_char array,
1161 max_char_or_byte2 is the index of the last character. A
1162 character with non-zero CHAR2B->byte1 is not in the font.
1163 A character with byte2 less than min_char_or_byte2 or
1164 greater max_char_or_byte2 is not in the font. */
1165 if (char2b->byte1 == 0
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1169 }
1170 else
1171 {
1172 /* If either min_byte1 or max_byte1 are nonzero, both
1173 min_char_or_byte2 and max_char_or_byte2 are less than
1174 256, and the 2-byte character index values corresponding
1175 to the per_char array element N (counting from 0) are:
1176
1177 byte1 = N/D + min_byte1
1178 byte2 = N\D + min_char_or_byte2
1179
1180 where:
1181
1182 D = max_char_or_byte2 - min_char_or_byte2 + 1
1183 / = integer division
1184 \ = integer modulus */
1185 if (char2b->byte1 >= font->min_byte1
1186 && char2b->byte1 <= font->max_byte1
1187 && char2b->byte2 >= font->min_char_or_byte2
1188 && char2b->byte2 <= font->max_char_or_byte2)
1189 {
1190 pcm = (font->per_char
1191 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1192 * (char2b->byte1 - font->min_byte1))
1193 + (char2b->byte2 - font->min_char_or_byte2));
1194 }
1195 }
1196 }
1197 else
1198 {
1199 /* If the per_char pointer is null, all glyphs between the first
1200 and last character indexes inclusive have the same
1201 information, as given by both min_bounds and max_bounds. */
1202 if (char2b->byte2 >= font->min_char_or_byte2
1203 && char2b->byte2 <= font->max_char_or_byte2)
1204 pcm = &font->max_bounds;
1205 }
1206
1207 return ((pcm == NULL
1208 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1209 ? NULL : pcm);
1210 }
1211
1212
1213 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1214 the two-byte form of C. Encoding is returned in *CHAR2B. */
1215
1216 static INLINE void
1217 x_encode_char (c, char2b, font_info)
1218 int c;
1219 XChar2b *char2b;
1220 struct font_info *font_info;
1221 {
1222 int charset = CHAR_CHARSET (c);
1223 XFontStruct *font = font_info->font;
1224
1225 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1226 This may be either a program in a special encoder language or a
1227 fixed encoding. */
1228 if (font_info->font_encoder)
1229 {
1230 /* It's a program. */
1231 struct ccl_program *ccl = font_info->font_encoder;
1232
1233 if (CHARSET_DIMENSION (charset) == 1)
1234 {
1235 ccl->reg[0] = charset;
1236 ccl->reg[1] = char2b->byte2;
1237 }
1238 else
1239 {
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte1;
1242 ccl->reg[2] = char2b->byte2;
1243 }
1244
1245 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1246
1247 /* We assume that MSBs are appropriately set/reset by CCL
1248 program. */
1249 if (font->max_byte1 == 0) /* 1-byte font */
1250 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1251 else
1252 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1253 }
1254 else if (font_info->encoding[charset])
1255 {
1256 /* Fixed encoding scheme. See fontset.h for the meaning of the
1257 encoding numbers. */
1258 int enc = font_info->encoding[charset];
1259
1260 if ((enc == 1 || enc == 2)
1261 && CHARSET_DIMENSION (charset) == 2)
1262 char2b->byte1 |= 0x80;
1263
1264 if (enc == 1 || enc == 3)
1265 char2b->byte2 |= 0x80;
1266 }
1267 }
1268
1269
1270 /* Get face and two-byte form of character C in face FACE_ID on frame
1271 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1272 means we want to display multibyte text. Value is a pointer to a
1273 realized face that is ready for display. */
1274
1275 static INLINE struct face *
1276 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1277 struct frame *f;
1278 int c, face_id;
1279 XChar2b *char2b;
1280 int multibyte_p;
1281 {
1282 struct face *face = FACE_FROM_ID (f, face_id);
1283
1284 if (!multibyte_p)
1285 {
1286 /* Unibyte case. We don't have to encode, but we have to make
1287 sure to use a face suitable for unibyte. */
1288 char2b->byte1 = 0;
1289 char2b->byte2 = c;
1290 face_id = FACE_FOR_CHAR (f, face, c);
1291 face = FACE_FROM_ID (f, face_id);
1292 }
1293 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1294 {
1295 /* Case of ASCII in a face known to fit ASCII. */
1296 char2b->byte1 = 0;
1297 char2b->byte2 = c;
1298 }
1299 else
1300 {
1301 int c1, c2, charset;
1302
1303 /* Split characters into bytes. If c2 is -1 afterwards, C is
1304 really a one-byte character so that byte1 is zero. */
1305 SPLIT_CHAR (c, charset, c1, c2);
1306 if (c2 > 0)
1307 char2b->byte1 = c1, char2b->byte2 = c2;
1308 else
1309 char2b->byte1 = 0, char2b->byte2 = c1;
1310
1311 /* Maybe encode the character in *CHAR2B. */
1312 if (face->font != NULL)
1313 {
1314 struct font_info *font_info
1315 = FONT_INFO_FROM_ID (f, face->font_info_id);
1316 if (font_info)
1317 x_encode_char (c, char2b, font_info);
1318 }
1319 }
1320
1321 /* Make sure X resources of the face are allocated. */
1322 xassert (face != NULL);
1323 PREPARE_FACE_FOR_DISPLAY (f, face);
1324
1325 return face;
1326 }
1327
1328
1329 /* Get face and two-byte form of character glyph GLYPH on frame F.
1330 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1331 a pointer to a realized face that is ready for display. */
1332
1333 static INLINE struct face *
1334 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1335 struct frame *f;
1336 struct glyph *glyph;
1337 XChar2b *char2b;
1338 int *two_byte_p;
1339 {
1340 struct face *face;
1341
1342 xassert (glyph->type == CHAR_GLYPH);
1343 face = FACE_FROM_ID (f, glyph->face_id);
1344
1345 if (two_byte_p)
1346 *two_byte_p = 0;
1347
1348 if (!glyph->multibyte_p)
1349 {
1350 /* Unibyte case. We don't have to encode, but we have to make
1351 sure to use a face suitable for unibyte. */
1352 char2b->byte1 = 0;
1353 char2b->byte2 = glyph->u.ch;
1354 }
1355 else if (glyph->u.ch < 128
1356 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1357 {
1358 /* Case of ASCII in a face known to fit ASCII. */
1359 char2b->byte1 = 0;
1360 char2b->byte2 = glyph->u.ch;
1361 }
1362 else
1363 {
1364 int c1, c2, charset;
1365
1366 /* Split characters into bytes. If c2 is -1 afterwards, C is
1367 really a one-byte character so that byte1 is zero. */
1368 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1369 if (c2 > 0)
1370 char2b->byte1 = c1, char2b->byte2 = c2;
1371 else
1372 char2b->byte1 = 0, char2b->byte2 = c1;
1373
1374 /* Maybe encode the character in *CHAR2B. */
1375 if (charset != CHARSET_ASCII)
1376 {
1377 struct font_info *font_info
1378 = FONT_INFO_FROM_ID (f, face->font_info_id);
1379 if (font_info)
1380 {
1381 x_encode_char (glyph->u.ch, char2b, font_info);
1382 if (two_byte_p)
1383 *two_byte_p
1384 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1385 }
1386 }
1387 }
1388
1389 /* Make sure X resources of the face are allocated. */
1390 xassert (face != NULL);
1391 PREPARE_FACE_FOR_DISPLAY (f, face);
1392 return face;
1393 }
1394
1395
1396 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1397 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1398
1399 static INLINE void
1400 x_append_glyph (it)
1401 struct it *it;
1402 {
1403 struct glyph *glyph;
1404 enum glyph_row_area area = it->area;
1405
1406 xassert (it->glyph_row);
1407 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1408
1409 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1410 if (glyph < it->glyph_row->glyphs[area + 1])
1411 {
1412 glyph->charpos = CHARPOS (it->position);
1413 glyph->object = it->object;
1414 glyph->pixel_width = it->pixel_width;
1415 glyph->voffset = it->voffset;
1416 glyph->type = CHAR_GLYPH;
1417 glyph->multibyte_p = it->multibyte_p;
1418 glyph->left_box_line_p = it->start_of_box_run_p;
1419 glyph->right_box_line_p = it->end_of_box_run_p;
1420 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1421 || it->phys_descent > it->descent);
1422 glyph->padding_p = 0;
1423 glyph->glyph_not_available_p = it->glyph_not_available_p;
1424 glyph->face_id = it->face_id;
1425 glyph->u.ch = it->char_to_display;
1426 ++it->glyph_row->used[area];
1427 }
1428 }
1429
1430 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1431 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1432
1433 static INLINE void
1434 x_append_composite_glyph (it)
1435 struct it *it;
1436 {
1437 struct glyph *glyph;
1438 enum glyph_row_area area = it->area;
1439
1440 xassert (it->glyph_row);
1441
1442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1443 if (glyph < it->glyph_row->glyphs[area + 1])
1444 {
1445 glyph->charpos = CHARPOS (it->position);
1446 glyph->object = it->object;
1447 glyph->pixel_width = it->pixel_width;
1448 glyph->voffset = it->voffset;
1449 glyph->type = COMPOSITE_GLYPH;
1450 glyph->multibyte_p = it->multibyte_p;
1451 glyph->left_box_line_p = it->start_of_box_run_p;
1452 glyph->right_box_line_p = it->end_of_box_run_p;
1453 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1454 || it->phys_descent > it->descent);
1455 glyph->padding_p = 0;
1456 glyph->glyph_not_available_p = 0;
1457 glyph->face_id = it->face_id;
1458 glyph->u.cmp_id = it->cmp_id;
1459 ++it->glyph_row->used[area];
1460 }
1461 }
1462
1463
1464 /* Change IT->ascent and IT->height according to the setting of
1465 IT->voffset. */
1466
1467 static INLINE void
1468 take_vertical_position_into_account (it)
1469 struct it *it;
1470 {
1471 if (it->voffset)
1472 {
1473 if (it->voffset < 0)
1474 /* Increase the ascent so that we can display the text higher
1475 in the line. */
1476 it->ascent += abs (it->voffset);
1477 else
1478 /* Increase the descent so that we can display the text lower
1479 in the line. */
1480 it->descent += it->voffset;
1481 }
1482 }
1483
1484
1485 /* Produce glyphs/get display metrics for the image IT is loaded with.
1486 See the description of struct display_iterator in dispextern.h for
1487 an overview of struct display_iterator. */
1488
1489 static void
1490 x_produce_image_glyph (it)
1491 struct it *it;
1492 {
1493 struct image *img;
1494 struct face *face;
1495
1496 xassert (it->what == IT_IMAGE);
1497
1498 face = FACE_FROM_ID (it->f, it->face_id);
1499 img = IMAGE_FROM_ID (it->f, it->image_id);
1500 xassert (img);
1501
1502 /* Make sure X resources of the face and image are loaded. */
1503 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1504 prepare_image_for_display (it->f, img);
1505
1506 it->ascent = it->phys_ascent = image_ascent (img, face);
1507 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1508 it->pixel_width = img->width + 2 * img->margin;
1509
1510 it->nglyphs = 1;
1511
1512 if (face->box != FACE_NO_BOX)
1513 {
1514 it->ascent += face->box_line_width;
1515 it->descent += face->box_line_width;
1516
1517 if (it->start_of_box_run_p)
1518 it->pixel_width += face->box_line_width;
1519 if (it->end_of_box_run_p)
1520 it->pixel_width += face->box_line_width;
1521 }
1522
1523 take_vertical_position_into_account (it);
1524
1525 if (it->glyph_row)
1526 {
1527 struct glyph *glyph;
1528 enum glyph_row_area area = it->area;
1529
1530 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1531 if (glyph < it->glyph_row->glyphs[area + 1])
1532 {
1533 glyph->charpos = CHARPOS (it->position);
1534 glyph->object = it->object;
1535 glyph->pixel_width = it->pixel_width;
1536 glyph->voffset = it->voffset;
1537 glyph->type = IMAGE_GLYPH;
1538 glyph->multibyte_p = it->multibyte_p;
1539 glyph->left_box_line_p = it->start_of_box_run_p;
1540 glyph->right_box_line_p = it->end_of_box_run_p;
1541 glyph->overlaps_vertically_p = 0;
1542 glyph->padding_p = 0;
1543 glyph->glyph_not_available_p = 0;
1544 glyph->face_id = it->face_id;
1545 glyph->u.img_id = img->id;
1546 ++it->glyph_row->used[area];
1547 }
1548 }
1549 }
1550
1551
1552 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1553 of the glyph, WIDTH and HEIGHT are the width and height of the
1554 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1555 ascent of the glyph (0 <= ASCENT <= 1). */
1556
1557 static void
1558 x_append_stretch_glyph (it, object, width, height, ascent)
1559 struct it *it;
1560 Lisp_Object object;
1561 int width, height;
1562 double ascent;
1563 {
1564 struct glyph *glyph;
1565 enum glyph_row_area area = it->area;
1566
1567 xassert (ascent >= 0 && ascent <= 1);
1568
1569 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1570 if (glyph < it->glyph_row->glyphs[area + 1])
1571 {
1572 glyph->charpos = CHARPOS (it->position);
1573 glyph->object = object;
1574 glyph->pixel_width = width;
1575 glyph->voffset = it->voffset;
1576 glyph->type = STRETCH_GLYPH;
1577 glyph->multibyte_p = it->multibyte_p;
1578 glyph->left_box_line_p = it->start_of_box_run_p;
1579 glyph->right_box_line_p = it->end_of_box_run_p;
1580 glyph->overlaps_vertically_p = 0;
1581 glyph->padding_p = 0;
1582 glyph->glyph_not_available_p = 0;
1583 glyph->face_id = it->face_id;
1584 glyph->u.stretch.ascent = height * ascent;
1585 glyph->u.stretch.height = height;
1586 ++it->glyph_row->used[area];
1587 }
1588 }
1589
1590
1591 /* Produce a stretch glyph for iterator IT. IT->object is the value
1592 of the glyph property displayed. The value must be a list
1593 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1594 being recognized:
1595
1596 1. `:width WIDTH' specifies that the space should be WIDTH *
1597 canonical char width wide. WIDTH may be an integer or floating
1598 point number.
1599
1600 2. `:relative-width FACTOR' specifies that the width of the stretch
1601 should be computed from the width of the first character having the
1602 `glyph' property, and should be FACTOR times that width.
1603
1604 3. `:align-to HPOS' specifies that the space should be wide enough
1605 to reach HPOS, a value in canonical character units.
1606
1607 Exactly one of the above pairs must be present.
1608
1609 4. `:height HEIGHT' specifies that the height of the stretch produced
1610 should be HEIGHT, measured in canonical character units.
1611
1612 5. `:relative-height FACTOR' specifies that the height of the the
1613 stretch should be FACTOR times the height of the characters having
1614 the glyph property.
1615
1616 Either none or exactly one of 4 or 5 must be present.
1617
1618 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1619 of the stretch should be used for the ascent of the stretch.
1620 ASCENT must be in the range 0 <= ASCENT <= 100. */
1621
1622 #define NUMVAL(X) \
1623 ((INTEGERP (X) || FLOATP (X)) \
1624 ? XFLOATINT (X) \
1625 : - 1)
1626
1627
1628 static void
1629 x_produce_stretch_glyph (it)
1630 struct it *it;
1631 {
1632 /* (space :width WIDTH :height HEIGHT. */
1633 #if GLYPH_DEBUG
1634 extern Lisp_Object Qspace;
1635 #endif
1636 extern Lisp_Object QCwidth, QCheight, QCascent;
1637 extern Lisp_Object QCrelative_width, QCrelative_height;
1638 extern Lisp_Object QCalign_to;
1639 Lisp_Object prop, plist;
1640 double width = 0, height = 0, ascent = 0;
1641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1642 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1643
1644 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1645
1646 /* List should start with `space'. */
1647 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1648 plist = XCDR (it->object);
1649
1650 /* Compute the width of the stretch. */
1651 if (prop = Fplist_get (plist, QCwidth),
1652 NUMVAL (prop) > 0)
1653 /* Absolute width `:width WIDTH' specified and valid. */
1654 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1655 else if (prop = Fplist_get (plist, QCrelative_width),
1656 NUMVAL (prop) > 0)
1657 {
1658 /* Relative width `:relative-width FACTOR' specified and valid.
1659 Compute the width of the characters having the `glyph'
1660 property. */
1661 struct it it2;
1662 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1663
1664 it2 = *it;
1665 if (it->multibyte_p)
1666 {
1667 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1668 - IT_BYTEPOS (*it));
1669 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1670 }
1671 else
1672 it2.c = *p, it2.len = 1;
1673
1674 it2.glyph_row = NULL;
1675 it2.what = IT_CHARACTER;
1676 x_produce_glyphs (&it2);
1677 width = NUMVAL (prop) * it2.pixel_width;
1678 }
1679 else if (prop = Fplist_get (plist, QCalign_to),
1680 NUMVAL (prop) > 0)
1681 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1682 else
1683 /* Nothing specified -> width defaults to canonical char width. */
1684 width = CANON_X_UNIT (it->f);
1685
1686 /* Compute height. */
1687 if (prop = Fplist_get (plist, QCheight),
1688 NUMVAL (prop) > 0)
1689 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1690 else if (prop = Fplist_get (plist, QCrelative_height),
1691 NUMVAL (prop) > 0)
1692 height = FONT_HEIGHT (font) * NUMVAL (prop);
1693 else
1694 height = FONT_HEIGHT (font);
1695
1696 /* Compute percentage of height used for ascent. If
1697 `:ascent ASCENT' is present and valid, use that. Otherwise,
1698 derive the ascent from the font in use. */
1699 if (prop = Fplist_get (plist, QCascent),
1700 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1701 ascent = NUMVAL (prop) / 100.0;
1702 else
1703 ascent = (double) font->ascent / FONT_HEIGHT (font);
1704
1705 if (width <= 0)
1706 width = 1;
1707 if (height <= 0)
1708 height = 1;
1709
1710 if (it->glyph_row)
1711 {
1712 Lisp_Object object = it->stack[it->sp - 1].string;
1713 if (!STRINGP (object))
1714 object = it->w->buffer;
1715 x_append_stretch_glyph (it, object, width, height, ascent);
1716 }
1717
1718 it->pixel_width = width;
1719 it->ascent = it->phys_ascent = height * ascent;
1720 it->descent = it->phys_descent = height - it->ascent;
1721 it->nglyphs = 1;
1722
1723 if (face->box != FACE_NO_BOX)
1724 {
1725 it->ascent += face->box_line_width;
1726 it->descent += face->box_line_width;
1727
1728 if (it->start_of_box_run_p)
1729 it->pixel_width += face->box_line_width;
1730 if (it->end_of_box_run_p)
1731 it->pixel_width += face->box_line_width;
1732 }
1733
1734 take_vertical_position_into_account (it);
1735 }
1736
1737 /* Return proper value to be used as baseline offset of font that has
1738 ASCENT and DESCENT to draw characters by the font at the vertical
1739 center of the line of frame F.
1740
1741 Here, out task is to find the value of BOFF in the following figure;
1742
1743 -------------------------+-----------+-
1744 -+-+---------+-+ | |
1745 | | | | | |
1746 | | | | F_ASCENT F_HEIGHT
1747 | | | ASCENT | |
1748 HEIGHT | | | | |
1749 | | |-|-+------+-----------|------- baseline
1750 | | | | BOFF | |
1751 | |---------|-+-+ | |
1752 | | | DESCENT | |
1753 -+-+---------+-+ F_DESCENT |
1754 -------------------------+-----------+-
1755
1756 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1757 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1758 DESCENT = FONT->descent
1759 HEIGHT = FONT_HEIGHT (FONT)
1760 F_DESCENT = (F->output_data.x->font->descent
1761 - F->output_data.x->baseline_offset)
1762 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1763 */
1764
1765 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1766 ((FONT)->descent \
1767 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1768 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1769 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1770
1771 /* Produce glyphs/get display metrics for the display element IT is
1772 loaded with. See the description of struct display_iterator in
1773 dispextern.h for an overview of struct display_iterator. */
1774
1775 static void
1776 x_produce_glyphs (it)
1777 struct it *it;
1778 {
1779 it->glyph_not_available_p = 0;
1780
1781 if (it->what == IT_CHARACTER)
1782 {
1783 XChar2b char2b;
1784 XFontStruct *font;
1785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1786 XCharStruct *pcm;
1787 int font_not_found_p;
1788 struct font_info *font_info;
1789 int boff; /* baseline offset */
1790 /* We may change it->multibyte_p upon unibyte<->multibyte
1791 conversion. So, save the current value now and restore it
1792 later.
1793
1794 Note: It seems that we don't have to record multibyte_p in
1795 struct glyph because the character code itself tells if or
1796 not the character is multibyte. Thus, in the future, we must
1797 consider eliminating the field `multibyte_p' in the struct
1798 glyph.
1799 */
1800 int saved_multibyte_p = it->multibyte_p;
1801
1802 /* Maybe translate single-byte characters to multibyte, or the
1803 other way. */
1804 it->char_to_display = it->c;
1805 if (!ASCII_BYTE_P (it->c))
1806 {
1807 if (unibyte_display_via_language_environment
1808 && SINGLE_BYTE_CHAR_P (it->c)
1809 && (it->c >= 0240
1810 || !NILP (Vnonascii_translation_table)))
1811 {
1812 it->char_to_display = unibyte_char_to_multibyte (it->c);
1813 it->multibyte_p = 1;
1814 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1815 face = FACE_FROM_ID (it->f, it->face_id);
1816 }
1817 else if (!SINGLE_BYTE_CHAR_P (it->c)
1818 && !it->multibyte_p)
1819 {
1820 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1821 it->multibyte_p = 0;
1822 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1823 face = FACE_FROM_ID (it->f, it->face_id);
1824 }
1825 }
1826
1827 /* Get font to use. Encode IT->char_to_display. */
1828 x_get_char_face_and_encoding (it->f, it->char_to_display,
1829 it->face_id, &char2b,
1830 it->multibyte_p);
1831 font = face->font;
1832
1833 /* When no suitable font found, use the default font. */
1834 font_not_found_p = font == NULL;
1835 if (font_not_found_p)
1836 {
1837 font = FRAME_FONT (it->f);
1838 boff = it->f->output_data.x->baseline_offset;
1839 font_info = NULL;
1840 }
1841 else
1842 {
1843 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1844 boff = font_info->baseline_offset;
1845 if (font_info->vertical_centering)
1846 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1847 }
1848
1849 if (it->char_to_display >= ' '
1850 && (!it->multibyte_p || it->char_to_display < 128))
1851 {
1852 /* Either unibyte or ASCII. */
1853 int stretched_p;
1854
1855 it->nglyphs = 1;
1856
1857 pcm = x_per_char_metric (font, &char2b);
1858 it->ascent = font->ascent + boff;
1859 it->descent = font->descent - boff;
1860
1861 if (pcm)
1862 {
1863 it->phys_ascent = pcm->ascent + boff;
1864 it->phys_descent = pcm->descent - boff;
1865 it->pixel_width = pcm->width;
1866 }
1867 else
1868 {
1869 it->glyph_not_available_p = 1;
1870 it->phys_ascent = font->ascent + boff;
1871 it->phys_descent = font->descent - boff;
1872 it->pixel_width = FONT_WIDTH (font);
1873 }
1874
1875 /* If this is a space inside a region of text with
1876 `space-width' property, change its width. */
1877 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1878 if (stretched_p)
1879 it->pixel_width *= XFLOATINT (it->space_width);
1880
1881 /* If face has a box, add the box thickness to the character
1882 height. If character has a box line to the left and/or
1883 right, add the box line width to the character's width. */
1884 if (face->box != FACE_NO_BOX)
1885 {
1886 int thick = face->box_line_width;
1887
1888 it->ascent += thick;
1889 it->descent += thick;
1890
1891 if (it->start_of_box_run_p)
1892 it->pixel_width += thick;
1893 if (it->end_of_box_run_p)
1894 it->pixel_width += thick;
1895 }
1896
1897 /* If face has an overline, add the height of the overline
1898 (1 pixel) and a 1 pixel margin to the character height. */
1899 if (face->overline_p)
1900 it->ascent += 2;
1901
1902 take_vertical_position_into_account (it);
1903
1904 /* If we have to actually produce glyphs, do it. */
1905 if (it->glyph_row)
1906 {
1907 if (stretched_p)
1908 {
1909 /* Translate a space with a `space-width' property
1910 into a stretch glyph. */
1911 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1912 x_append_stretch_glyph (it, it->object, it->pixel_width,
1913 it->ascent + it->descent, ascent);
1914 }
1915 else
1916 x_append_glyph (it);
1917
1918 /* If characters with lbearing or rbearing are displayed
1919 in this line, record that fact in a flag of the
1920 glyph row. This is used to optimize X output code. */
1921 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1922 it->glyph_row->contains_overlapping_glyphs_p = 1;
1923 }
1924 }
1925 else if (it->char_to_display == '\n')
1926 {
1927 /* A newline has no width but we need the height of the line. */
1928 it->pixel_width = 0;
1929 it->nglyphs = 0;
1930 it->ascent = it->phys_ascent = font->ascent + boff;
1931 it->descent = it->phys_descent = font->descent - boff;
1932
1933 if (face->box != FACE_NO_BOX)
1934 {
1935 int thick = face->box_line_width;
1936 it->ascent += thick;
1937 it->descent += thick;
1938 }
1939 }
1940 else if (it->char_to_display == '\t')
1941 {
1942 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1943 int x = it->current_x + it->continuation_lines_width;
1944 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1945
1946 /* If the distance from the current position to the next tab
1947 stop is less than a canonical character width, use the
1948 tab stop after that. */
1949 if (next_tab_x - x < CANON_X_UNIT (it->f))
1950 next_tab_x += tab_width;
1951
1952 it->pixel_width = next_tab_x - x;
1953 it->nglyphs = 1;
1954 it->ascent = it->phys_ascent = font->ascent + boff;
1955 it->descent = it->phys_descent = font->descent - boff;
1956
1957 if (it->glyph_row)
1958 {
1959 double ascent = (double) it->ascent / (it->ascent + it->descent);
1960 x_append_stretch_glyph (it, it->object, it->pixel_width,
1961 it->ascent + it->descent, ascent);
1962 }
1963 }
1964 else
1965 {
1966 /* A multi-byte character. Assume that the display width of the
1967 character is the width of the character multiplied by the
1968 width of the font. */
1969
1970 /* If we found a font, this font should give us the right
1971 metrics. If we didn't find a font, use the frame's
1972 default font and calculate the width of the character
1973 from the charset width; this is what old redisplay code
1974 did. */
1975 pcm = x_per_char_metric (font, &char2b);
1976 if (font_not_found_p || !pcm)
1977 {
1978 int charset = CHAR_CHARSET (it->char_to_display);
1979
1980 it->glyph_not_available_p = 1;
1981 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1982 * CHARSET_WIDTH (charset));
1983 it->phys_ascent = font->ascent + boff;
1984 it->phys_descent = font->descent - boff;
1985 }
1986 else
1987 {
1988 it->pixel_width = pcm->width;
1989 it->phys_ascent = pcm->ascent + boff;
1990 it->phys_descent = pcm->descent - boff;
1991 if (it->glyph_row
1992 && (pcm->lbearing < 0
1993 || pcm->rbearing > pcm->width))
1994 it->glyph_row->contains_overlapping_glyphs_p = 1;
1995 }
1996 it->nglyphs = 1;
1997 it->ascent = font->ascent + boff;
1998 it->descent = font->descent - boff;
1999 if (face->box != FACE_NO_BOX)
2000 {
2001 int thick = face->box_line_width;
2002 it->ascent += thick;
2003 it->descent += thick;
2004
2005 if (it->start_of_box_run_p)
2006 it->pixel_width += thick;
2007 if (it->end_of_box_run_p)
2008 it->pixel_width += thick;
2009 }
2010
2011 /* If face has an overline, add the height of the overline
2012 (1 pixel) and a 1 pixel margin to the character height. */
2013 if (face->overline_p)
2014 it->ascent += 2;
2015
2016 take_vertical_position_into_account (it);
2017
2018 if (it->glyph_row)
2019 x_append_glyph (it);
2020 }
2021 it->multibyte_p = saved_multibyte_p;
2022 }
2023 else if (it->what == IT_COMPOSITION)
2024 {
2025 /* Note: A composition is represented as one glyph in the
2026 glyph matrix. There are no padding glyphs. */
2027 XChar2b char2b;
2028 XFontStruct *font;
2029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2030 XCharStruct *pcm;
2031 int font_not_found_p;
2032 struct font_info *font_info;
2033 int boff; /* baseline offset */
2034 struct composition *cmp = composition_table[it->cmp_id];
2035
2036 /* Maybe translate single-byte characters to multibyte. */
2037 it->char_to_display = it->c;
2038 if (unibyte_display_via_language_environment
2039 && SINGLE_BYTE_CHAR_P (it->c)
2040 && (it->c >= 0240
2041 || (it->c >= 0200
2042 && !NILP (Vnonascii_translation_table))))
2043 {
2044 it->char_to_display = unibyte_char_to_multibyte (it->c);
2045 }
2046
2047 /* Get face and font to use. Encode IT->char_to_display. */
2048 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2049 face = FACE_FROM_ID (it->f, it->face_id);
2050 x_get_char_face_and_encoding (it->f, it->char_to_display,
2051 it->face_id, &char2b, it->multibyte_p);
2052 font = face->font;
2053
2054 /* When no suitable font found, use the default font. */
2055 font_not_found_p = font == NULL;
2056 if (font_not_found_p)
2057 {
2058 font = FRAME_FONT (it->f);
2059 boff = it->f->output_data.x->baseline_offset;
2060 font_info = NULL;
2061 }
2062 else
2063 {
2064 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2065 boff = font_info->baseline_offset;
2066 if (font_info->vertical_centering)
2067 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2068 }
2069
2070 /* There are no padding glyphs, so there is only one glyph to
2071 produce for the composition. Important is that pixel_width,
2072 ascent and descent are the values of what is drawn by
2073 draw_glyphs (i.e. the values of the overall glyphs composed). */
2074 it->nglyphs = 1;
2075
2076 /* If we have not yet calculated pixel size data of glyphs of
2077 the composition for the current face font, calculate them
2078 now. Theoretically, we have to check all fonts for the
2079 glyphs, but that requires much time and memory space. So,
2080 here we check only the font of the first glyph. This leads
2081 to incorrect display very rarely, and C-l (recenter) can
2082 correct the display anyway. */
2083 if (cmp->font != (void *) font)
2084 {
2085 /* Ascent and descent of the font of the first character of
2086 this composition (adjusted by baseline offset). Ascent
2087 and descent of overall glyphs should not be less than
2088 them respectively. */
2089 int font_ascent = font->ascent + boff;
2090 int font_descent = font->descent - boff;
2091 /* Bounding box of the overall glyphs. */
2092 int leftmost, rightmost, lowest, highest;
2093 int i, width, ascent, descent;
2094
2095 cmp->font = (void *) font;
2096
2097 /* Initialize the bounding box. */
2098 if (font_info
2099 && (pcm = x_per_char_metric (font, &char2b)))
2100 {
2101 width = pcm->width;
2102 ascent = pcm->ascent;
2103 descent = pcm->descent;
2104 }
2105 else
2106 {
2107 width = FONT_WIDTH (font);
2108 ascent = font->ascent;
2109 descent = font->descent;
2110 }
2111
2112 rightmost = width;
2113 lowest = - descent + boff;
2114 highest = ascent + boff;
2115 leftmost = 0;
2116
2117 if (font_info
2118 && font_info->default_ascent
2119 && CHAR_TABLE_P (Vuse_default_ascent)
2120 && !NILP (Faref (Vuse_default_ascent,
2121 make_number (it->char_to_display))))
2122 highest = font_info->default_ascent + boff;
2123
2124 /* Draw the first glyph at the normal position. It may be
2125 shifted to right later if some other glyphs are drawn at
2126 the left. */
2127 cmp->offsets[0] = 0;
2128 cmp->offsets[1] = boff;
2129
2130 /* Set cmp->offsets for the remaining glyphs. */
2131 for (i = 1; i < cmp->glyph_len; i++)
2132 {
2133 int left, right, btm, top;
2134 int ch = COMPOSITION_GLYPH (cmp, i);
2135 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2136
2137 face = FACE_FROM_ID (it->f, face_id);
2138 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2139 it->multibyte_p);
2140 font = face->font;
2141 if (font == NULL)
2142 {
2143 font = FRAME_FONT (it->f);
2144 boff = it->f->output_data.x->baseline_offset;
2145 font_info = NULL;
2146 }
2147 else
2148 {
2149 font_info
2150 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2151 boff = font_info->baseline_offset;
2152 if (font_info->vertical_centering)
2153 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2154 }
2155
2156 if (font_info
2157 && (pcm = x_per_char_metric (font, &char2b)))
2158 {
2159 width = pcm->width;
2160 ascent = pcm->ascent;
2161 descent = pcm->descent;
2162 }
2163 else
2164 {
2165 width = FONT_WIDTH (font);
2166 ascent = 1;
2167 descent = 0;
2168 }
2169
2170 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2171 {
2172 /* Relative composition with or without
2173 alternate chars. */
2174 left = (leftmost + rightmost - width) / 2;
2175 btm = - descent + boff;
2176 if (font_info && font_info->relative_compose
2177 && (! CHAR_TABLE_P (Vignore_relative_composition)
2178 || NILP (Faref (Vignore_relative_composition,
2179 make_number (ch)))))
2180 {
2181
2182 if (- descent >= font_info->relative_compose)
2183 /* One extra pixel between two glyphs. */
2184 btm = highest + 1;
2185 else if (ascent <= 0)
2186 /* One extra pixel between two glyphs. */
2187 btm = lowest - 1 - ascent - descent;
2188 }
2189 }
2190 else
2191 {
2192 /* A composition rule is specified by an integer
2193 value that encodes global and new reference
2194 points (GREF and NREF). GREF and NREF are
2195 specified by numbers as below:
2196
2197 0---1---2 -- ascent
2198 | |
2199 | |
2200 | |
2201 9--10--11 -- center
2202 | |
2203 ---3---4---5--- baseline
2204 | |
2205 6---7---8 -- descent
2206 */
2207 int rule = COMPOSITION_RULE (cmp, i);
2208 int gref, nref, grefx, grefy, nrefx, nrefy;
2209
2210 COMPOSITION_DECODE_RULE (rule, gref, nref);
2211 grefx = gref % 3, nrefx = nref % 3;
2212 grefy = gref / 3, nrefy = nref / 3;
2213
2214 left = (leftmost
2215 + grefx * (rightmost - leftmost) / 2
2216 - nrefx * width / 2);
2217 btm = ((grefy == 0 ? highest
2218 : grefy == 1 ? 0
2219 : grefy == 2 ? lowest
2220 : (highest + lowest) / 2)
2221 - (nrefy == 0 ? ascent + descent
2222 : nrefy == 1 ? descent - boff
2223 : nrefy == 2 ? 0
2224 : (ascent + descent) / 2));
2225 }
2226
2227 cmp->offsets[i * 2] = left;
2228 cmp->offsets[i * 2 + 1] = btm + descent;
2229
2230 /* Update the bounding box of the overall glyphs. */
2231 right = left + width;
2232 top = btm + descent + ascent;
2233 if (left < leftmost)
2234 leftmost = left;
2235 if (right > rightmost)
2236 rightmost = right;
2237 if (top > highest)
2238 highest = top;
2239 if (btm < lowest)
2240 lowest = btm;
2241 }
2242
2243 /* If there are glyphs whose x-offsets are negative,
2244 shift all glyphs to the right and make all x-offsets
2245 non-negative. */
2246 if (leftmost < 0)
2247 {
2248 for (i = 0; i < cmp->glyph_len; i++)
2249 cmp->offsets[i * 2] -= leftmost;
2250 rightmost -= leftmost;
2251 }
2252
2253 cmp->pixel_width = rightmost;
2254 cmp->ascent = highest;
2255 cmp->descent = - lowest;
2256 if (cmp->ascent < font_ascent)
2257 cmp->ascent = font_ascent;
2258 if (cmp->descent < font_descent)
2259 cmp->descent = font_descent;
2260 }
2261
2262 it->pixel_width = cmp->pixel_width;
2263 it->ascent = it->phys_ascent = cmp->ascent;
2264 it->descent = it->phys_descent = cmp->descent;
2265
2266 if (face->box != FACE_NO_BOX)
2267 {
2268 int thick = face->box_line_width;
2269 it->ascent += thick;
2270 it->descent += thick;
2271
2272 if (it->start_of_box_run_p)
2273 it->pixel_width += thick;
2274 if (it->end_of_box_run_p)
2275 it->pixel_width += thick;
2276 }
2277
2278 /* If face has an overline, add the height of the overline
2279 (1 pixel) and a 1 pixel margin to the character height. */
2280 if (face->overline_p)
2281 it->ascent += 2;
2282
2283 take_vertical_position_into_account (it);
2284
2285 if (it->glyph_row)
2286 x_append_composite_glyph (it);
2287 }
2288 else if (it->what == IT_IMAGE)
2289 x_produce_image_glyph (it);
2290 else if (it->what == IT_STRETCH)
2291 x_produce_stretch_glyph (it);
2292
2293 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2294 because this isn't true for images with `:ascent 100'. */
2295 xassert (it->ascent >= 0 && it->descent >= 0);
2296 if (it->area == TEXT_AREA)
2297 it->current_x += it->pixel_width;
2298
2299 it->descent += it->extra_line_spacing;
2300
2301 it->max_ascent = max (it->max_ascent, it->ascent);
2302 it->max_descent = max (it->max_descent, it->descent);
2303 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2304 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2305 }
2306
2307
2308 /* Estimate the pixel height of the mode or top line on frame F.
2309 FACE_ID specifies what line's height to estimate. */
2310
2311 int
2312 x_estimate_mode_line_height (f, face_id)
2313 struct frame *f;
2314 enum face_id face_id;
2315 {
2316 int height = FONT_HEIGHT (FRAME_FONT (f));
2317
2318 /* This function is called so early when Emacs starts that the face
2319 cache and mode line face are not yet initialized. */
2320 if (FRAME_FACE_CACHE (f))
2321 {
2322 struct face *face = FACE_FROM_ID (f, face_id);
2323 if (face)
2324 {
2325 if (face->font)
2326 height = FONT_HEIGHT (face->font);
2327 height += 2 * face->box_line_width;
2328 }
2329 }
2330
2331 return height;
2332 }
2333
2334 \f
2335 /***********************************************************************
2336 Glyph display
2337 ***********************************************************************/
2338
2339 /* A sequence of glyphs to be drawn in the same face.
2340
2341 This data structure is not really completely X specific, so it
2342 could possibly, at least partially, be useful for other systems. It
2343 is currently not part of the external redisplay interface because
2344 it's not clear what other systems will need. */
2345
2346 struct glyph_string
2347 {
2348 /* X-origin of the string. */
2349 int x;
2350
2351 /* Y-origin and y-position of the base line of this string. */
2352 int y, ybase;
2353
2354 /* The width of the string, not including a face extension. */
2355 int width;
2356
2357 /* The width of the string, including a face extension. */
2358 int background_width;
2359
2360 /* The height of this string. This is the height of the line this
2361 string is drawn in, and can be different from the height of the
2362 font the string is drawn in. */
2363 int height;
2364
2365 /* Number of pixels this string overwrites in front of its x-origin.
2366 This number is zero if the string has an lbearing >= 0; it is
2367 -lbearing, if the string has an lbearing < 0. */
2368 int left_overhang;
2369
2370 /* Number of pixels this string overwrites past its right-most
2371 nominal x-position, i.e. x + width. Zero if the string's
2372 rbearing is <= its nominal width, rbearing - width otherwise. */
2373 int right_overhang;
2374
2375 /* The frame on which the glyph string is drawn. */
2376 struct frame *f;
2377
2378 /* The window on which the glyph string is drawn. */
2379 struct window *w;
2380
2381 /* X display and window for convenience. */
2382 Display *display;
2383 Window window;
2384
2385 /* The glyph row for which this string was built. It determines the
2386 y-origin and height of the string. */
2387 struct glyph_row *row;
2388
2389 /* The area within row. */
2390 enum glyph_row_area area;
2391
2392 /* Characters to be drawn, and number of characters. */
2393 XChar2b *char2b;
2394 int nchars;
2395
2396 /* A face-override for drawing cursors, mouse face and similar. */
2397 enum draw_glyphs_face hl;
2398
2399 /* Face in which this string is to be drawn. */
2400 struct face *face;
2401
2402 /* Font in which this string is to be drawn. */
2403 XFontStruct *font;
2404
2405 /* Font info for this string. */
2406 struct font_info *font_info;
2407
2408 /* Non-null means this string describes (part of) a composition.
2409 All characters from char2b are drawn composed. */
2410 struct composition *cmp;
2411
2412 /* Index of this glyph string's first character in the glyph
2413 definition of CMP. If this is zero, this glyph string describes
2414 the first character of a composition. */
2415 int gidx;
2416
2417 /* 1 means this glyph strings face has to be drawn to the right end
2418 of the window's drawing area. */
2419 unsigned extends_to_end_of_line_p : 1;
2420
2421 /* 1 means the background of this string has been drawn. */
2422 unsigned background_filled_p : 1;
2423
2424 /* 1 means glyph string must be drawn with 16-bit functions. */
2425 unsigned two_byte_p : 1;
2426
2427 /* 1 means that the original font determined for drawing this glyph
2428 string could not be loaded. The member `font' has been set to
2429 the frame's default font in this case. */
2430 unsigned font_not_found_p : 1;
2431
2432 /* 1 means that the face in which this glyph string is drawn has a
2433 stipple pattern. */
2434 unsigned stippled_p : 1;
2435
2436 /* 1 means only the foreground of this glyph string must be drawn,
2437 and we should use the physical height of the line this glyph
2438 string appears in as clip rect. */
2439 unsigned for_overlaps_p : 1;
2440
2441 /* The GC to use for drawing this glyph string. */
2442 GC gc;
2443
2444 /* A pointer to the first glyph in the string. This glyph
2445 corresponds to char2b[0]. Needed to draw rectangles if
2446 font_not_found_p is 1. */
2447 struct glyph *first_glyph;
2448
2449 /* Image, if any. */
2450 struct image *img;
2451
2452 struct glyph_string *next, *prev;
2453 };
2454
2455
2456 #if 0
2457
2458 static void
2459 x_dump_glyph_string (s)
2460 struct glyph_string *s;
2461 {
2462 fprintf (stderr, "glyph string\n");
2463 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2464 s->x, s->y, s->width, s->height);
2465 fprintf (stderr, " ybase = %d\n", s->ybase);
2466 fprintf (stderr, " hl = %d\n", s->hl);
2467 fprintf (stderr, " left overhang = %d, right = %d\n",
2468 s->left_overhang, s->right_overhang);
2469 fprintf (stderr, " nchars = %d\n", s->nchars);
2470 fprintf (stderr, " extends to end of line = %d\n",
2471 s->extends_to_end_of_line_p);
2472 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2473 fprintf (stderr, " bg width = %d\n", s->background_width);
2474 }
2475
2476 #endif /* GLYPH_DEBUG */
2477
2478
2479
2480 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2481 struct glyph_string **,
2482 struct glyph_string *,
2483 struct glyph_string *));
2484 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2485 struct glyph_string **,
2486 struct glyph_string *,
2487 struct glyph_string *));
2488 static void x_append_glyph_string P_ ((struct glyph_string **,
2489 struct glyph_string **,
2490 struct glyph_string *));
2491 static int x_left_overwritten P_ ((struct glyph_string *));
2492 static int x_left_overwriting P_ ((struct glyph_string *));
2493 static int x_right_overwritten P_ ((struct glyph_string *));
2494 static int x_right_overwriting P_ ((struct glyph_string *));
2495 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2496 int));
2497 static void x_init_glyph_string P_ ((struct glyph_string *,
2498 XChar2b *, struct window *,
2499 struct glyph_row *,
2500 enum glyph_row_area, int,
2501 enum draw_glyphs_face));
2502 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2503 enum glyph_row_area, int, int,
2504 enum draw_glyphs_face, int *, int *, int));
2505 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2506 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2507 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2508 int));
2509 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2510 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2511 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2512 static void x_draw_glyph_string P_ ((struct glyph_string *));
2513 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2514 static void x_set_cursor_gc P_ ((struct glyph_string *));
2515 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2516 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2517 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2518 int *, int *));
2519 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2520 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2521 unsigned long *, double, int));
2522 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2523 double, int, unsigned long));
2524 static void x_setup_relief_colors P_ ((struct glyph_string *));
2525 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2526 static void x_draw_image_relief P_ ((struct glyph_string *));
2527 static void x_draw_image_foreground P_ ((struct glyph_string *));
2528 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2529 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2530 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2531 int, int, int));
2532 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2533 int, int, int, int, XRectangle *));
2534 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2535 int, int, int, XRectangle *));
2536 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2537 enum glyph_row_area));
2538 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2539 struct glyph_row *,
2540 enum glyph_row_area, int, int));
2541
2542 #if GLYPH_DEBUG
2543 static void x_check_font P_ ((struct frame *, XFontStruct *));
2544 #endif
2545
2546
2547 /* Append the list of glyph strings with head H and tail T to the list
2548 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2549
2550 static INLINE void
2551 x_append_glyph_string_lists (head, tail, h, t)
2552 struct glyph_string **head, **tail;
2553 struct glyph_string *h, *t;
2554 {
2555 if (h)
2556 {
2557 if (*head)
2558 (*tail)->next = h;
2559 else
2560 *head = h;
2561 h->prev = *tail;
2562 *tail = t;
2563 }
2564 }
2565
2566
2567 /* Prepend the list of glyph strings with head H and tail T to the
2568 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2569 result. */
2570
2571 static INLINE void
2572 x_prepend_glyph_string_lists (head, tail, h, t)
2573 struct glyph_string **head, **tail;
2574 struct glyph_string *h, *t;
2575 {
2576 if (h)
2577 {
2578 if (*head)
2579 (*head)->prev = t;
2580 else
2581 *tail = t;
2582 t->next = *head;
2583 *head = h;
2584 }
2585 }
2586
2587
2588 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2589 Set *HEAD and *TAIL to the resulting list. */
2590
2591 static INLINE void
2592 x_append_glyph_string (head, tail, s)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *s;
2595 {
2596 s->next = s->prev = NULL;
2597 x_append_glyph_string_lists (head, tail, s, s);
2598 }
2599
2600
2601 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2602 face. */
2603
2604 static void
2605 x_set_cursor_gc (s)
2606 struct glyph_string *s;
2607 {
2608 if (s->font == FRAME_FONT (s->f)
2609 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2610 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2611 && !s->cmp)
2612 s->gc = s->f->output_data.x->cursor_gc;
2613 else
2614 {
2615 /* Cursor on non-default face: must merge. */
2616 XGCValues xgcv;
2617 unsigned long mask;
2618
2619 xgcv.background = s->f->output_data.x->cursor_pixel;
2620 xgcv.foreground = s->face->background;
2621
2622 /* If the glyph would be invisible, try a different foreground. */
2623 if (xgcv.foreground == xgcv.background)
2624 xgcv.foreground = s->face->foreground;
2625 if (xgcv.foreground == xgcv.background)
2626 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2627 if (xgcv.foreground == xgcv.background)
2628 xgcv.foreground = s->face->foreground;
2629
2630 /* Make sure the cursor is distinct from text in this face. */
2631 if (xgcv.background == s->face->background
2632 && xgcv.foreground == s->face->foreground)
2633 {
2634 xgcv.background = s->face->foreground;
2635 xgcv.foreground = s->face->background;
2636 }
2637
2638 IF_DEBUG (x_check_font (s->f, s->font));
2639 xgcv.font = s->font->fid;
2640 xgcv.graphics_exposures = False;
2641 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2642
2643 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2644 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2645 mask, &xgcv);
2646 else
2647 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2648 = XCreateGC (s->display, s->window, mask, &xgcv);
2649
2650 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2651 }
2652 }
2653
2654
2655 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2656
2657 static void
2658 x_set_mouse_face_gc (s)
2659 struct glyph_string *s;
2660 {
2661 int face_id;
2662 struct face *face;
2663
2664 /* What face has to be used for the mouse face? */
2665 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2666 face = FACE_FROM_ID (s->f, face_id);
2667 if (s->first_glyph->type == CHAR_GLYPH)
2668 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2669 else
2670 face_id = FACE_FOR_CHAR (s->f, face, 0);
2671 s->face = FACE_FROM_ID (s->f, face_id);
2672 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2673
2674 /* If font in this face is same as S->font, use it. */
2675 if (s->font == s->face->font)
2676 s->gc = s->face->gc;
2677 else
2678 {
2679 /* Otherwise construct scratch_cursor_gc with values from FACE
2680 but font FONT. */
2681 XGCValues xgcv;
2682 unsigned long mask;
2683
2684 xgcv.background = s->face->background;
2685 xgcv.foreground = s->face->foreground;
2686 IF_DEBUG (x_check_font (s->f, s->font));
2687 xgcv.font = s->font->fid;
2688 xgcv.graphics_exposures = False;
2689 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2690
2691 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2692 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2693 mask, &xgcv);
2694 else
2695 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2696 = XCreateGC (s->display, s->window, mask, &xgcv);
2697
2698 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2699 }
2700
2701 xassert (s->gc != 0);
2702 }
2703
2704
2705 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2706 Faces to use in the mode line have already been computed when the
2707 matrix was built, so there isn't much to do, here. */
2708
2709 static INLINE void
2710 x_set_mode_line_face_gc (s)
2711 struct glyph_string *s;
2712 {
2713 s->gc = s->face->gc;
2714 }
2715
2716
2717 /* Set S->gc of glyph string S for drawing that glyph string. Set
2718 S->stippled_p to a non-zero value if the face of S has a stipple
2719 pattern. */
2720
2721 static INLINE void
2722 x_set_glyph_string_gc (s)
2723 struct glyph_string *s;
2724 {
2725 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2726
2727 if (s->hl == DRAW_NORMAL_TEXT)
2728 {
2729 s->gc = s->face->gc;
2730 s->stippled_p = s->face->stipple != 0;
2731 }
2732 else if (s->hl == DRAW_INVERSE_VIDEO)
2733 {
2734 x_set_mode_line_face_gc (s);
2735 s->stippled_p = s->face->stipple != 0;
2736 }
2737 else if (s->hl == DRAW_CURSOR)
2738 {
2739 x_set_cursor_gc (s);
2740 s->stippled_p = 0;
2741 }
2742 else if (s->hl == DRAW_MOUSE_FACE)
2743 {
2744 x_set_mouse_face_gc (s);
2745 s->stippled_p = s->face->stipple != 0;
2746 }
2747 else if (s->hl == DRAW_IMAGE_RAISED
2748 || s->hl == DRAW_IMAGE_SUNKEN)
2749 {
2750 s->gc = s->face->gc;
2751 s->stippled_p = s->face->stipple != 0;
2752 }
2753 else
2754 {
2755 s->gc = s->face->gc;
2756 s->stippled_p = s->face->stipple != 0;
2757 }
2758
2759 /* GC must have been set. */
2760 xassert (s->gc != 0);
2761 }
2762
2763
2764 /* Return in *R the clipping rectangle for glyph string S. */
2765
2766 static void
2767 x_get_glyph_string_clip_rect (s, r)
2768 struct glyph_string *s;
2769 XRectangle *r;
2770 {
2771 if (s->row->full_width_p)
2772 {
2773 /* Draw full-width. X coordinates are relative to S->w->left. */
2774 int canon_x = CANON_X_UNIT (s->f);
2775
2776 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2777 r->width = XFASTINT (s->w->width) * canon_x;
2778
2779 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2780 {
2781 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2782 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2783 r->x -= width;
2784 }
2785
2786 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2787
2788 /* Unless displaying a mode or menu bar line, which are always
2789 fully visible, clip to the visible part of the row. */
2790 if (s->w->pseudo_window_p)
2791 r->height = s->row->visible_height;
2792 else
2793 r->height = s->height;
2794 }
2795 else
2796 {
2797 /* This is a text line that may be partially visible. */
2798 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2799 r->width = window_box_width (s->w, s->area);
2800 r->height = s->row->visible_height;
2801 }
2802
2803 /* Don't use S->y for clipping because it doesn't take partially
2804 visible lines into account. For example, it can be negative for
2805 partially visible lines at the top of a window. */
2806 if (!s->row->full_width_p
2807 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2808 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2809 else
2810 r->y = max (0, s->row->y);
2811
2812 /* If drawing a tool-bar window, draw it over the internal border
2813 at the top of the window. */
2814 if (s->w == XWINDOW (s->f->tool_bar_window))
2815 r->y -= s->f->output_data.x->internal_border_width;
2816
2817 /* If S draws overlapping rows, it's sufficient to use the top and
2818 bottom of the window for clipping because this glyph string
2819 intentionally draws over other lines. */
2820 if (s->for_overlaps_p)
2821 {
2822 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2823 r->height = window_text_bottom_y (s->w) - r->y;
2824 }
2825
2826 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2827 }
2828
2829
2830 /* Set clipping for output of glyph string S. S may be part of a mode
2831 line or menu if we don't have X toolkit support. */
2832
2833 static INLINE void
2834 x_set_glyph_string_clipping (s)
2835 struct glyph_string *s;
2836 {
2837 XRectangle r;
2838 x_get_glyph_string_clip_rect (s, &r);
2839 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2840 }
2841
2842
2843 /* Compute left and right overhang of glyph string S. If S is a glyph
2844 string for a composition, assume overhangs don't exist. */
2845
2846 static INLINE void
2847 x_compute_glyph_string_overhangs (s)
2848 struct glyph_string *s;
2849 {
2850 if (s->cmp == NULL
2851 && s->first_glyph->type == CHAR_GLYPH)
2852 {
2853 XCharStruct cs;
2854 int direction, font_ascent, font_descent;
2855 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2856 &font_ascent, &font_descent, &cs);
2857 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2858 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2859 }
2860 }
2861
2862
2863 /* Compute overhangs and x-positions for glyph string S and its
2864 predecessors, or successors. X is the starting x-position for S.
2865 BACKWARD_P non-zero means process predecessors. */
2866
2867 static void
2868 x_compute_overhangs_and_x (s, x, backward_p)
2869 struct glyph_string *s;
2870 int x;
2871 int backward_p;
2872 {
2873 if (backward_p)
2874 {
2875 while (s)
2876 {
2877 x_compute_glyph_string_overhangs (s);
2878 x -= s->width;
2879 s->x = x;
2880 s = s->prev;
2881 }
2882 }
2883 else
2884 {
2885 while (s)
2886 {
2887 x_compute_glyph_string_overhangs (s);
2888 s->x = x;
2889 x += s->width;
2890 s = s->next;
2891 }
2892 }
2893 }
2894
2895
2896 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2897 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2898 assumed to be zero. */
2899
2900 static void
2901 x_get_glyph_overhangs (glyph, f, left, right)
2902 struct glyph *glyph;
2903 struct frame *f;
2904 int *left, *right;
2905 {
2906 *left = *right = 0;
2907
2908 if (glyph->type == CHAR_GLYPH)
2909 {
2910 XFontStruct *font;
2911 struct face *face;
2912 struct font_info *font_info;
2913 XChar2b char2b;
2914 XCharStruct *pcm;
2915
2916 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2917 font = face->font;
2918 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2919 if (font
2920 && (pcm = x_per_char_metric (font, &char2b)))
2921 {
2922 if (pcm->rbearing > pcm->width)
2923 *right = pcm->rbearing - pcm->width;
2924 if (pcm->lbearing < 0)
2925 *left = -pcm->lbearing;
2926 }
2927 }
2928 }
2929
2930
2931 /* Return the index of the first glyph preceding glyph string S that
2932 is overwritten by S because of S's left overhang. Value is -1
2933 if no glyphs are overwritten. */
2934
2935 static int
2936 x_left_overwritten (s)
2937 struct glyph_string *s;
2938 {
2939 int k;
2940
2941 if (s->left_overhang)
2942 {
2943 int x = 0, i;
2944 struct glyph *glyphs = s->row->glyphs[s->area];
2945 int first = s->first_glyph - glyphs;
2946
2947 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2948 x -= glyphs[i].pixel_width;
2949
2950 k = i + 1;
2951 }
2952 else
2953 k = -1;
2954
2955 return k;
2956 }
2957
2958
2959 /* Return the index of the first glyph preceding glyph string S that
2960 is overwriting S because of its right overhang. Value is -1 if no
2961 glyph in front of S overwrites S. */
2962
2963 static int
2964 x_left_overwriting (s)
2965 struct glyph_string *s;
2966 {
2967 int i, k, x;
2968 struct glyph *glyphs = s->row->glyphs[s->area];
2969 int first = s->first_glyph - glyphs;
2970
2971 k = -1;
2972 x = 0;
2973 for (i = first - 1; i >= 0; --i)
2974 {
2975 int left, right;
2976 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2977 if (x + right > 0)
2978 k = i;
2979 x -= glyphs[i].pixel_width;
2980 }
2981
2982 return k;
2983 }
2984
2985
2986 /* Return the index of the last glyph following glyph string S that is
2987 not overwritten by S because of S's right overhang. Value is -1 if
2988 no such glyph is found. */
2989
2990 static int
2991 x_right_overwritten (s)
2992 struct glyph_string *s;
2993 {
2994 int k = -1;
2995
2996 if (s->right_overhang)
2997 {
2998 int x = 0, i;
2999 struct glyph *glyphs = s->row->glyphs[s->area];
3000 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3001 int end = s->row->used[s->area];
3002
3003 for (i = first; i < end && s->right_overhang > x; ++i)
3004 x += glyphs[i].pixel_width;
3005
3006 k = i;
3007 }
3008
3009 return k;
3010 }
3011
3012
3013 /* Return the index of the last glyph following glyph string S that
3014 overwrites S because of its left overhang. Value is negative
3015 if no such glyph is found. */
3016
3017 static int
3018 x_right_overwriting (s)
3019 struct glyph_string *s;
3020 {
3021 int i, k, x;
3022 int end = s->row->used[s->area];
3023 struct glyph *glyphs = s->row->glyphs[s->area];
3024 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3025
3026 k = -1;
3027 x = 0;
3028 for (i = first; i < end; ++i)
3029 {
3030 int left, right;
3031 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3032 if (x - left < 0)
3033 k = i;
3034 x += glyphs[i].pixel_width;
3035 }
3036
3037 return k;
3038 }
3039
3040
3041 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3042
3043 static INLINE void
3044 x_clear_glyph_string_rect (s, x, y, w, h)
3045 struct glyph_string *s;
3046 int x, y, w, h;
3047 {
3048 XGCValues xgcv;
3049 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3050 XSetForeground (s->display, s->gc, xgcv.background);
3051 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3052 XSetForeground (s->display, s->gc, xgcv.foreground);
3053 }
3054
3055
3056 /* Draw the background of glyph_string S. If S->background_filled_p
3057 is non-zero don't draw it. FORCE_P non-zero means draw the
3058 background even if it wouldn't be drawn normally. This is used
3059 when a string preceding S draws into the background of S, or S
3060 contains the first component of a composition. */
3061
3062 static void
3063 x_draw_glyph_string_background (s, force_p)
3064 struct glyph_string *s;
3065 int force_p;
3066 {
3067 /* Nothing to do if background has already been drawn or if it
3068 shouldn't be drawn in the first place. */
3069 if (!s->background_filled_p)
3070 {
3071 if (s->stippled_p)
3072 {
3073 /* Fill background with a stipple pattern. */
3074 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3075 XFillRectangle (s->display, s->window, s->gc, s->x,
3076 s->y + s->face->box_line_width,
3077 s->background_width,
3078 s->height - 2 * s->face->box_line_width);
3079 XSetFillStyle (s->display, s->gc, FillSolid);
3080 s->background_filled_p = 1;
3081 }
3082 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3083 || s->font_not_found_p
3084 || s->extends_to_end_of_line_p
3085 || force_p)
3086 {
3087 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3088 s->background_width,
3089 s->height - 2 * s->face->box_line_width);
3090 s->background_filled_p = 1;
3091 }
3092 }
3093 }
3094
3095
3096 /* Draw the foreground of glyph string S. */
3097
3098 static void
3099 x_draw_glyph_string_foreground (s)
3100 struct glyph_string *s;
3101 {
3102 int i, x;
3103
3104 /* If first glyph of S has a left box line, start drawing the text
3105 of S to the right of that box line. */
3106 if (s->face->box != FACE_NO_BOX
3107 && s->first_glyph->left_box_line_p)
3108 x = s->x + s->face->box_line_width;
3109 else
3110 x = s->x;
3111
3112 /* Draw characters of S as rectangles if S's font could not be
3113 loaded. */
3114 if (s->font_not_found_p)
3115 {
3116 for (i = 0; i < s->nchars; ++i)
3117 {
3118 struct glyph *g = s->first_glyph + i;
3119 XDrawRectangle (s->display, s->window,
3120 s->gc, x, s->y, g->pixel_width - 1,
3121 s->height - 1);
3122 x += g->pixel_width;
3123 }
3124 }
3125 else
3126 {
3127 char *char1b = (char *) s->char2b;
3128 int boff = s->font_info->baseline_offset;
3129
3130 if (s->font_info->vertical_centering)
3131 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3132
3133 /* If we can use 8-bit functions, condense S->char2b. */
3134 if (!s->two_byte_p)
3135 for (i = 0; i < s->nchars; ++i)
3136 char1b[i] = s->char2b[i].byte2;
3137
3138 /* Draw text with XDrawString if background has already been
3139 filled. Otherwise, use XDrawImageString. (Note that
3140 XDrawImageString is usually faster than XDrawString.) Always
3141 use XDrawImageString when drawing the cursor so that there is
3142 no chance that characters under a box cursor are invisible. */
3143 if (s->for_overlaps_p
3144 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3145 {
3146 /* Draw characters with 16-bit or 8-bit functions. */
3147 if (s->two_byte_p)
3148 XDrawString16 (s->display, s->window, s->gc, x,
3149 s->ybase - boff, s->char2b, s->nchars);
3150 else
3151 XDrawString (s->display, s->window, s->gc, x,
3152 s->ybase - boff, char1b, s->nchars);
3153 }
3154 else
3155 {
3156 if (s->two_byte_p)
3157 XDrawImageString16 (s->display, s->window, s->gc, x,
3158 s->ybase - boff, s->char2b, s->nchars);
3159 else
3160 XDrawImageString (s->display, s->window, s->gc, x,
3161 s->ybase - boff, char1b, s->nchars);
3162 }
3163 }
3164 }
3165
3166 /* Draw the foreground of composite glyph string S. */
3167
3168 static void
3169 x_draw_composite_glyph_string_foreground (s)
3170 struct glyph_string *s;
3171 {
3172 int i, x;
3173
3174 /* If first glyph of S has a left box line, start drawing the text
3175 of S to the right of that box line. */
3176 if (s->face->box != FACE_NO_BOX
3177 && s->first_glyph->left_box_line_p)
3178 x = s->x + s->face->box_line_width;
3179 else
3180 x = s->x;
3181
3182 /* S is a glyph string for a composition. S->gidx is the index of
3183 the first character drawn for glyphs of this composition.
3184 S->gidx == 0 means we are drawing the very first character of
3185 this composition. */
3186
3187 /* Draw a rectangle for the composition if the font for the very
3188 first character of the composition could not be loaded. */
3189 if (s->font_not_found_p)
3190 {
3191 if (s->gidx == 0)
3192 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3193 s->width - 1, s->height - 1);
3194 }
3195 else
3196 {
3197 for (i = 0; i < s->nchars; i++, ++s->gidx)
3198 XDrawString16 (s->display, s->window, s->gc,
3199 x + s->cmp->offsets[s->gidx * 2],
3200 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3201 s->char2b + i, 1);
3202 }
3203 }
3204
3205
3206 #ifdef USE_X_TOOLKIT
3207
3208 static struct frame *x_frame_of_widget P_ ((Widget));
3209
3210
3211 /* Return the frame on which widget WIDGET is used.. Abort if frame
3212 cannot be determined. */
3213
3214 static struct frame *
3215 x_frame_of_widget (widget)
3216 Widget widget;
3217 {
3218 struct x_display_info *dpyinfo;
3219 Lisp_Object tail;
3220 struct frame *f;
3221
3222 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3223
3224 /* Find the top-level shell of the widget. Note that this function
3225 can be called when the widget is not yet realized, so XtWindow
3226 (widget) == 0. That's the reason we can't simply use
3227 x_any_window_to_frame. */
3228 while (!XtIsTopLevelShell (widget))
3229 widget = XtParent (widget);
3230
3231 /* Look for a frame with that top-level widget. Allocate the color
3232 on that frame to get the right gamma correction value. */
3233 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3234 if (GC_FRAMEP (XCAR (tail))
3235 && (f = XFRAME (XCAR (tail)),
3236 (f->output_data.nothing != 1
3237 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3238 && f->output_data.x->widget == widget)
3239 return f;
3240
3241 abort ();
3242 }
3243
3244
3245 /* Allocate the color COLOR->pixel on the screen and display of
3246 widget WIDGET in colormap CMAP. If an exact match cannot be
3247 allocated, try the nearest color available. Value is non-zero
3248 if successful. This is called from lwlib. */
3249
3250 int
3251 x_alloc_nearest_color_for_widget (widget, cmap, color)
3252 Widget widget;
3253 Colormap cmap;
3254 XColor *color;
3255 {
3256 struct frame *f = x_frame_of_widget (widget);
3257 return x_alloc_nearest_color (f, cmap, color);
3258 }
3259
3260
3261 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3262 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3263 If this produces the same color as PIXEL, try a color where all RGB
3264 values have DELTA added. Return the allocated color in *PIXEL.
3265 DISPLAY is the X display, CMAP is the colormap to operate on.
3266 Value is non-zero if successful. */
3267
3268 int
3269 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3270 Widget widget;
3271 Display *display;
3272 Colormap cmap;
3273 unsigned long *pixel;
3274 double factor;
3275 int delta;
3276 {
3277 struct frame *f = x_frame_of_widget (widget);
3278 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3279 }
3280
3281
3282 #endif /* USE_X_TOOLKIT */
3283
3284
3285 /* Value is an array of XColor structures for the contents of the
3286 color map of frame F. Set *NCELLS to the size of the array.
3287 Note that this probably shouldn't be called for large color maps,
3288 say a 24-bit TrueColor map. */
3289
3290 static const XColor *
3291 x_color_cells (f, ncells)
3292 struct frame *f;
3293 int *ncells;
3294 {
3295 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3296
3297 if (dpyinfo->color_cells == NULL)
3298 {
3299 Display *display = FRAME_X_DISPLAY (f);
3300 Screen *screen = FRAME_X_SCREEN (f);
3301 int i;
3302
3303 dpyinfo->ncolor_cells
3304 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3305 dpyinfo->color_cells
3306 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3307 * sizeof *dpyinfo->color_cells);
3308
3309 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3310 dpyinfo->color_cells[i].pixel = i;
3311
3312 XQueryColors (display, FRAME_X_COLORMAP (f),
3313 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3314 }
3315
3316 *ncells = dpyinfo->ncolor_cells;
3317 return dpyinfo->color_cells;
3318 }
3319
3320
3321 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3322 colors in COLORS. Use cached information, if available. */
3323
3324 void
3325 x_query_colors (f, colors, ncolors)
3326 struct frame *f;
3327 XColor *colors;
3328 int ncolors;
3329 {
3330 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3331
3332 if (dpyinfo->color_cells)
3333 {
3334 int i;
3335 for (i = 0; i < ncolors; ++i)
3336 {
3337 unsigned long pixel = colors[i].pixel;
3338 xassert (pixel < dpyinfo->ncolor_cells);
3339 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3340 colors[i] = dpyinfo->color_cells[pixel];
3341 }
3342 }
3343 else
3344 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3345 }
3346
3347
3348 /* On frame F, translate pixel color to RGB values for the color in
3349 COLOR. Use cached information, if available. */
3350
3351 void
3352 x_query_color (f, color)
3353 struct frame *f;
3354 XColor *color;
3355 {
3356 x_query_colors (f, color, 1);
3357 }
3358
3359
3360 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3361 CMAP. If an exact match can't be allocated, try the nearest color
3362 available. Value is non-zero if successful. Set *COLOR to the
3363 color allocated. */
3364
3365 int
3366 x_alloc_nearest_color (f, cmap, color)
3367 struct frame *f;
3368 Colormap cmap;
3369 XColor *color;
3370 {
3371 Display *display = FRAME_X_DISPLAY (f);
3372 Screen *screen = FRAME_X_SCREEN (f);
3373 int rc;
3374
3375 gamma_correct (f, color);
3376 rc = XAllocColor (display, cmap, color);
3377 if (rc == 0)
3378 {
3379 /* If we got to this point, the colormap is full, so we're going
3380 to try to get the next closest color. The algorithm used is
3381 a least-squares matching, which is what X uses for closest
3382 color matching with StaticColor visuals. */
3383 int nearest, i;
3384 unsigned long nearest_delta = ~0;
3385 int ncells;
3386 const XColor *cells = x_color_cells (f, &ncells);
3387
3388 for (nearest = i = 0; i < ncells; ++i)
3389 {
3390 long dred = (color->red >> 8) - (cells[i].red >> 8);
3391 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3392 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3393 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3394
3395 if (delta < nearest_delta)
3396 {
3397 nearest = i;
3398 nearest_delta = delta;
3399 }
3400 }
3401
3402 color->red = cells[nearest].red;
3403 color->green = cells[nearest].green;
3404 color->blue = cells[nearest].blue;
3405 rc = XAllocColor (display, cmap, color);
3406 }
3407 else
3408 {
3409 /* If allocation succeeded, and the allocated pixel color is not
3410 equal to a cached pixel color recorded earlier, there was a
3411 change in the colormap, so clear the color cache. */
3412 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3413 XColor *cached_color;
3414
3415 if (dpyinfo->color_cells
3416 && (cached_color = &dpyinfo->color_cells[color->pixel],
3417 (cached_color->red != color->red
3418 || cached_color->blue != color->blue
3419 || cached_color->green != color->green)))
3420 {
3421 xfree (dpyinfo->color_cells);
3422 dpyinfo->color_cells = NULL;
3423 dpyinfo->ncolor_cells = 0;
3424 }
3425 }
3426
3427 #ifdef DEBUG_X_COLORS
3428 if (rc)
3429 register_color (color->pixel);
3430 #endif /* DEBUG_X_COLORS */
3431
3432 return rc;
3433 }
3434
3435
3436 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3437 It's necessary to do this instead of just using PIXEL directly to
3438 get color reference counts right. */
3439
3440 unsigned long
3441 x_copy_color (f, pixel)
3442 struct frame *f;
3443 unsigned long pixel;
3444 {
3445 XColor color;
3446
3447 color.pixel = pixel;
3448 BLOCK_INPUT;
3449 x_query_color (f, &color);
3450 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3451 UNBLOCK_INPUT;
3452 #ifdef DEBUG_X_COLORS
3453 register_color (pixel);
3454 #endif
3455 return color.pixel;
3456 }
3457
3458
3459 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3460 It's necessary to do this instead of just using PIXEL directly to
3461 get color reference counts right. */
3462
3463 unsigned long
3464 x_copy_dpy_color (dpy, cmap, pixel)
3465 Display *dpy;
3466 Colormap cmap;
3467 unsigned long pixel;
3468 {
3469 XColor color;
3470
3471 color.pixel = pixel;
3472 BLOCK_INPUT;
3473 XQueryColor (dpy, cmap, &color);
3474 XAllocColor (dpy, cmap, &color);
3475 UNBLOCK_INPUT;
3476 #ifdef DEBUG_X_COLORS
3477 register_color (pixel);
3478 #endif
3479 return color.pixel;
3480 }
3481
3482
3483 /* Brightness beyond which a color won't have its highlight brightness
3484 boosted.
3485
3486 Nominally, highlight colors for `3d' faces are calculated by
3487 brightening an object's color by a constant scale factor, but this
3488 doesn't yield good results for dark colors, so for colors who's
3489 brightness is less than this value (on a scale of 0-65535) have an
3490 use an additional additive factor.
3491
3492 The value here is set so that the default menu-bar/mode-line color
3493 (grey75) will not have its highlights changed at all. */
3494 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3495
3496
3497 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3498 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3499 If this produces the same color as PIXEL, try a color where all RGB
3500 values have DELTA added. Return the allocated color in *PIXEL.
3501 DISPLAY is the X display, CMAP is the colormap to operate on.
3502 Value is non-zero if successful. */
3503
3504 static int
3505 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3506 struct frame *f;
3507 Display *display;
3508 Colormap cmap;
3509 unsigned long *pixel;
3510 double factor;
3511 int delta;
3512 {
3513 XColor color, new;
3514 long bright;
3515 int success_p;
3516
3517 /* Get RGB color values. */
3518 color.pixel = *pixel;
3519 x_query_color (f, &color);
3520
3521 /* Change RGB values by specified FACTOR. Avoid overflow! */
3522 xassert (factor >= 0);
3523 new.red = min (0xffff, factor * color.red);
3524 new.green = min (0xffff, factor * color.green);
3525 new.blue = min (0xffff, factor * color.blue);
3526
3527 /* Calculate brightness of COLOR. */
3528 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3529
3530 /* We only boost colors that are darker than
3531 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3532 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3533 /* Make an additive adjustment to NEW, because it's dark enough so
3534 that scaling by FACTOR alone isn't enough. */
3535 {
3536 /* How far below the limit this color is (0 - 1, 1 being darker). */
3537 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3538 /* The additive adjustment. */
3539 int min_delta = delta * dimness * factor / 2;
3540
3541 if (factor < 1)
3542 {
3543 new.red = max (0, new.red - min_delta);
3544 new.green = max (0, new.green - min_delta);
3545 new.blue = max (0, new.blue - min_delta);
3546 }
3547 else
3548 {
3549 new.red = min (0xffff, min_delta + new.red);
3550 new.green = min (0xffff, min_delta + new.green);
3551 new.blue = min (0xffff, min_delta + new.blue);
3552 }
3553 }
3554
3555 /* Try to allocate the color. */
3556 success_p = x_alloc_nearest_color (f, cmap, &new);
3557 if (success_p)
3558 {
3559 if (new.pixel == *pixel)
3560 {
3561 /* If we end up with the same color as before, try adding
3562 delta to the RGB values. */
3563 x_free_colors (f, &new.pixel, 1);
3564
3565 new.red = min (0xffff, delta + color.red);
3566 new.green = min (0xffff, delta + color.green);
3567 new.blue = min (0xffff, delta + color.blue);
3568 success_p = x_alloc_nearest_color (f, cmap, &new);
3569 }
3570 else
3571 success_p = 1;
3572 *pixel = new.pixel;
3573 }
3574
3575 return success_p;
3576 }
3577
3578
3579 /* Set up the foreground color for drawing relief lines of glyph
3580 string S. RELIEF is a pointer to a struct relief containing the GC
3581 with which lines will be drawn. Use a color that is FACTOR or
3582 DELTA lighter or darker than the relief's background which is found
3583 in S->f->output_data.x->relief_background. If such a color cannot
3584 be allocated, use DEFAULT_PIXEL, instead. */
3585
3586 static void
3587 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3588 struct frame *f;
3589 struct relief *relief;
3590 double factor;
3591 int delta;
3592 unsigned long default_pixel;
3593 {
3594 XGCValues xgcv;
3595 struct x_output *di = f->output_data.x;
3596 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3597 unsigned long pixel;
3598 unsigned long background = di->relief_background;
3599 Colormap cmap = FRAME_X_COLORMAP (f);
3600 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3601 Display *dpy = FRAME_X_DISPLAY (f);
3602
3603 xgcv.graphics_exposures = False;
3604 xgcv.line_width = 1;
3605
3606 /* Free previously allocated color. The color cell will be reused
3607 when it has been freed as many times as it was allocated, so this
3608 doesn't affect faces using the same colors. */
3609 if (relief->gc
3610 && relief->allocated_p)
3611 {
3612 x_free_colors (f, &relief->pixel, 1);
3613 relief->allocated_p = 0;
3614 }
3615
3616 /* Allocate new color. */
3617 xgcv.foreground = default_pixel;
3618 pixel = background;
3619 if (dpyinfo->n_planes != 1
3620 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3621 {
3622 relief->allocated_p = 1;
3623 xgcv.foreground = relief->pixel = pixel;
3624 }
3625
3626 if (relief->gc == 0)
3627 {
3628 xgcv.stipple = dpyinfo->gray;
3629 mask |= GCStipple;
3630 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3631 }
3632 else
3633 XChangeGC (dpy, relief->gc, mask, &xgcv);
3634 }
3635
3636
3637 /* Set up colors for the relief lines around glyph string S. */
3638
3639 static void
3640 x_setup_relief_colors (s)
3641 struct glyph_string *s;
3642 {
3643 struct x_output *di = s->f->output_data.x;
3644 unsigned long color;
3645
3646 if (s->face->use_box_color_for_shadows_p)
3647 color = s->face->box_color;
3648 else
3649 {
3650 XGCValues xgcv;
3651
3652 /* Get the background color of the face. */
3653 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3654 color = xgcv.background;
3655 }
3656
3657 if (di->white_relief.gc == 0
3658 || color != di->relief_background)
3659 {
3660 di->relief_background = color;
3661 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3662 WHITE_PIX_DEFAULT (s->f));
3663 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3664 BLACK_PIX_DEFAULT (s->f));
3665 }
3666 }
3667
3668
3669 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3670 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3671 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3672 relief. LEFT_P non-zero means draw a relief on the left side of
3673 the rectangle. RIGHT_P non-zero means draw a relief on the right
3674 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3675 when drawing. */
3676
3677 static void
3678 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3679 raised_p, left_p, right_p, clip_rect)
3680 struct frame *f;
3681 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3682 XRectangle *clip_rect;
3683 {
3684 int i;
3685 GC gc;
3686
3687 if (raised_p)
3688 gc = f->output_data.x->white_relief.gc;
3689 else
3690 gc = f->output_data.x->black_relief.gc;
3691 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3692
3693 /* Top. */
3694 for (i = 0; i < width; ++i)
3695 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3696 left_x + i * left_p, top_y + i,
3697 right_x + 1 - i * right_p, top_y + i);
3698
3699 /* Left. */
3700 if (left_p)
3701 for (i = 0; i < width; ++i)
3702 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3703 left_x + i, top_y + i, left_x + i, bottom_y - i);
3704
3705 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3706 if (raised_p)
3707 gc = f->output_data.x->black_relief.gc;
3708 else
3709 gc = f->output_data.x->white_relief.gc;
3710 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3711
3712 /* Bottom. */
3713 for (i = 0; i < width; ++i)
3714 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3715 left_x + i * left_p, bottom_y - i,
3716 right_x + 1 - i * right_p, bottom_y - i);
3717
3718 /* Right. */
3719 if (right_p)
3720 for (i = 0; i < width; ++i)
3721 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3722 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3723
3724 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3725 }
3726
3727
3728 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3729 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3730 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3731 left side of the rectangle. RIGHT_P non-zero means draw a line
3732 on the right side of the rectangle. CLIP_RECT is the clipping
3733 rectangle to use when drawing. */
3734
3735 static void
3736 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3737 left_p, right_p, clip_rect)
3738 struct glyph_string *s;
3739 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3740 XRectangle *clip_rect;
3741 {
3742 XGCValues xgcv;
3743
3744 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3745 XSetForeground (s->display, s->gc, s->face->box_color);
3746 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3747
3748 /* Top. */
3749 XFillRectangle (s->display, s->window, s->gc,
3750 left_x, top_y, right_x - left_x + 1, width);
3751
3752 /* Left. */
3753 if (left_p)
3754 XFillRectangle (s->display, s->window, s->gc,
3755 left_x, top_y, width, bottom_y - top_y + 1);
3756
3757 /* Bottom. */
3758 XFillRectangle (s->display, s->window, s->gc,
3759 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3760
3761 /* Right. */
3762 if (right_p)
3763 XFillRectangle (s->display, s->window, s->gc,
3764 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3765
3766 XSetForeground (s->display, s->gc, xgcv.foreground);
3767 XSetClipMask (s->display, s->gc, None);
3768 }
3769
3770
3771 /* Draw a box around glyph string S. */
3772
3773 static void
3774 x_draw_glyph_string_box (s)
3775 struct glyph_string *s;
3776 {
3777 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3778 int left_p, right_p;
3779 struct glyph *last_glyph;
3780 XRectangle clip_rect;
3781
3782 last_x = window_box_right (s->w, s->area);
3783 if (s->row->full_width_p
3784 && !s->w->pseudo_window_p)
3785 {
3786 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3787 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3788 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3789 }
3790
3791 /* The glyph that may have a right box line. */
3792 last_glyph = (s->cmp || s->img
3793 ? s->first_glyph
3794 : s->first_glyph + s->nchars - 1);
3795
3796 width = s->face->box_line_width;
3797 raised_p = s->face->box == FACE_RAISED_BOX;
3798 left_x = s->x;
3799 right_x = ((s->row->full_width_p
3800 ? last_x - 1
3801 : min (last_x, s->x + s->background_width) - 1));
3802 top_y = s->y;
3803 bottom_y = top_y + s->height - 1;
3804
3805 left_p = (s->first_glyph->left_box_line_p
3806 || (s->hl == DRAW_MOUSE_FACE
3807 && (s->prev == NULL
3808 || s->prev->hl != s->hl)));
3809 right_p = (last_glyph->right_box_line_p
3810 || (s->hl == DRAW_MOUSE_FACE
3811 && (s->next == NULL
3812 || s->next->hl != s->hl)));
3813
3814 x_get_glyph_string_clip_rect (s, &clip_rect);
3815
3816 if (s->face->box == FACE_SIMPLE_BOX)
3817 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3818 left_p, right_p, &clip_rect);
3819 else
3820 {
3821 x_setup_relief_colors (s);
3822 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3823 width, raised_p, left_p, right_p, &clip_rect);
3824 }
3825 }
3826
3827
3828 /* Draw foreground of image glyph string S. */
3829
3830 static void
3831 x_draw_image_foreground (s)
3832 struct glyph_string *s;
3833 {
3834 int x;
3835 int y = s->ybase - image_ascent (s->img, s->face);
3836
3837 /* If first glyph of S has a left box line, start drawing it to the
3838 right of that line. */
3839 if (s->face->box != FACE_NO_BOX
3840 && s->first_glyph->left_box_line_p)
3841 x = s->x + s->face->box_line_width;
3842 else
3843 x = s->x;
3844
3845 /* If there is a margin around the image, adjust x- and y-position
3846 by that margin. */
3847 if (s->img->margin)
3848 {
3849 x += s->img->margin;
3850 y += s->img->margin;
3851 }
3852
3853 if (s->img->pixmap)
3854 {
3855 if (s->img->mask)
3856 {
3857 /* We can't set both a clip mask and use XSetClipRectangles
3858 because the latter also sets a clip mask. We also can't
3859 trust on the shape extension to be available
3860 (XShapeCombineRegion). So, compute the rectangle to draw
3861 manually. */
3862 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3863 | GCFunction);
3864 XGCValues xgcv;
3865 XRectangle clip_rect, image_rect, r;
3866
3867 xgcv.clip_mask = s->img->mask;
3868 xgcv.clip_x_origin = x;
3869 xgcv.clip_y_origin = y;
3870 xgcv.function = GXcopy;
3871 XChangeGC (s->display, s->gc, mask, &xgcv);
3872
3873 x_get_glyph_string_clip_rect (s, &clip_rect);
3874 image_rect.x = x;
3875 image_rect.y = y;
3876 image_rect.width = s->img->width;
3877 image_rect.height = s->img->height;
3878 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3879 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3880 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3881 }
3882 else
3883 {
3884 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3885 XGCValues xgcv;
3886 XRectangle clip_rect, image_rect, r;
3887
3888 x_get_glyph_string_clip_rect (s, &clip_rect);
3889 image_rect.x = x;
3890 image_rect.y = y;
3891 image_rect.width = s->img->width;
3892 image_rect.height = s->img->height;
3893 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3894 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3895 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3896
3897 /* When the image has a mask, we can expect that at
3898 least part of a mouse highlight or a block cursor will
3899 be visible. If the image doesn't have a mask, make
3900 a block cursor visible by drawing a rectangle around
3901 the image. I believe it's looking better if we do
3902 nothing here for mouse-face. */
3903 if (s->hl == DRAW_CURSOR)
3904 XDrawRectangle (s->display, s->window, s->gc, x, y,
3905 s->img->width - 1, s->img->height - 1);
3906 }
3907 }
3908 else
3909 /* Draw a rectangle if image could not be loaded. */
3910 XDrawRectangle (s->display, s->window, s->gc, x, y,
3911 s->img->width - 1, s->img->height - 1);
3912 }
3913
3914
3915 /* Draw a relief around the image glyph string S. */
3916
3917 static void
3918 x_draw_image_relief (s)
3919 struct glyph_string *s;
3920 {
3921 int x0, y0, x1, y1, thick, raised_p;
3922 XRectangle r;
3923 int x;
3924 int y = s->ybase - image_ascent (s->img, s->face);
3925
3926 /* If first glyph of S has a left box line, start drawing it to the
3927 right of that line. */
3928 if (s->face->box != FACE_NO_BOX
3929 && s->first_glyph->left_box_line_p)
3930 x = s->x + s->face->box_line_width;
3931 else
3932 x = s->x;
3933
3934 /* If there is a margin around the image, adjust x- and y-position
3935 by that margin. */
3936 if (s->img->margin)
3937 {
3938 x += s->img->margin;
3939 y += s->img->margin;
3940 }
3941
3942 if (s->hl == DRAW_IMAGE_SUNKEN
3943 || s->hl == DRAW_IMAGE_RAISED)
3944 {
3945 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3946 raised_p = s->hl == DRAW_IMAGE_RAISED;
3947 }
3948 else
3949 {
3950 thick = abs (s->img->relief);
3951 raised_p = s->img->relief > 0;
3952 }
3953
3954 x0 = x - thick;
3955 y0 = y - thick;
3956 x1 = x + s->img->width + thick - 1;
3957 y1 = y + s->img->height + thick - 1;
3958
3959 x_setup_relief_colors (s);
3960 x_get_glyph_string_clip_rect (s, &r);
3961 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3962 }
3963
3964
3965 /* Draw the foreground of image glyph string S to PIXMAP. */
3966
3967 static void
3968 x_draw_image_foreground_1 (s, pixmap)
3969 struct glyph_string *s;
3970 Pixmap pixmap;
3971 {
3972 int x;
3973 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3974
3975 /* If first glyph of S has a left box line, start drawing it to the
3976 right of that line. */
3977 if (s->face->box != FACE_NO_BOX
3978 && s->first_glyph->left_box_line_p)
3979 x = s->face->box_line_width;
3980 else
3981 x = 0;
3982
3983 /* If there is a margin around the image, adjust x- and y-position
3984 by that margin. */
3985 if (s->img->margin)
3986 {
3987 x += s->img->margin;
3988 y += s->img->margin;
3989 }
3990
3991 if (s->img->pixmap)
3992 {
3993 if (s->img->mask)
3994 {
3995 /* We can't set both a clip mask and use XSetClipRectangles
3996 because the latter also sets a clip mask. We also can't
3997 trust on the shape extension to be available
3998 (XShapeCombineRegion). So, compute the rectangle to draw
3999 manually. */
4000 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4001 | GCFunction);
4002 XGCValues xgcv;
4003
4004 xgcv.clip_mask = s->img->mask;
4005 xgcv.clip_x_origin = x;
4006 xgcv.clip_y_origin = y;
4007 xgcv.function = GXcopy;
4008 XChangeGC (s->display, s->gc, mask, &xgcv);
4009
4010 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4011 0, 0, s->img->width, s->img->height, x, y);
4012 XSetClipMask (s->display, s->gc, None);
4013 }
4014 else
4015 {
4016 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4017 0, 0, s->img->width, s->img->height, x, y);
4018
4019 /* When the image has a mask, we can expect that at
4020 least part of a mouse highlight or a block cursor will
4021 be visible. If the image doesn't have a mask, make
4022 a block cursor visible by drawing a rectangle around
4023 the image. I believe it's looking better if we do
4024 nothing here for mouse-face. */
4025 if (s->hl == DRAW_CURSOR)
4026 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4027 s->img->width - 1, s->img->height - 1);
4028 }
4029 }
4030 else
4031 /* Draw a rectangle if image could not be loaded. */
4032 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4033 s->img->width - 1, s->img->height - 1);
4034 }
4035
4036
4037 /* Draw part of the background of glyph string S. X, Y, W, and H
4038 give the rectangle to draw. */
4039
4040 static void
4041 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4042 struct glyph_string *s;
4043 int x, y, w, h;
4044 {
4045 if (s->stippled_p)
4046 {
4047 /* Fill background with a stipple pattern. */
4048 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4049 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4050 XSetFillStyle (s->display, s->gc, FillSolid);
4051 }
4052 else
4053 x_clear_glyph_string_rect (s, x, y, w, h);
4054 }
4055
4056
4057 /* Draw image glyph string S.
4058
4059 s->y
4060 s->x +-------------------------
4061 | s->face->box
4062 |
4063 | +-------------------------
4064 | | s->img->margin
4065 | |
4066 | | +-------------------
4067 | | | the image
4068
4069 */
4070
4071 static void
4072 x_draw_image_glyph_string (s)
4073 struct glyph_string *s;
4074 {
4075 int x, y;
4076 int box_line_width = s->face->box_line_width;
4077 int margin = s->img->margin;
4078 int height;
4079 Pixmap pixmap = None;
4080
4081 height = s->height - 2 * box_line_width;
4082
4083 /* Fill background with face under the image. Do it only if row is
4084 taller than image or if image has a clip mask to reduce
4085 flickering. */
4086 s->stippled_p = s->face->stipple != 0;
4087 if (height > s->img->height
4088 || margin
4089 || s->img->mask
4090 || s->img->pixmap == 0
4091 || s->width != s->background_width)
4092 {
4093 if (box_line_width && s->first_glyph->left_box_line_p)
4094 x = s->x + box_line_width;
4095 else
4096 x = s->x;
4097
4098 y = s->y + box_line_width;
4099
4100 if (s->img->mask)
4101 {
4102 /* Create a pixmap as large as the glyph string. Fill it
4103 with the background color. Copy the image to it, using
4104 its mask. Copy the temporary pixmap to the display. */
4105 Screen *screen = FRAME_X_SCREEN (s->f);
4106 int depth = DefaultDepthOfScreen (screen);
4107
4108 /* Create a pixmap as large as the glyph string. */
4109 pixmap = XCreatePixmap (s->display, s->window,
4110 s->background_width,
4111 s->height, depth);
4112
4113 /* Don't clip in the following because we're working on the
4114 pixmap. */
4115 XSetClipMask (s->display, s->gc, None);
4116
4117 /* Fill the pixmap with the background color/stipple. */
4118 if (s->stippled_p)
4119 {
4120 /* Fill background with a stipple pattern. */
4121 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4122 XFillRectangle (s->display, pixmap, s->gc,
4123 0, 0, s->background_width, s->height);
4124 XSetFillStyle (s->display, s->gc, FillSolid);
4125 }
4126 else
4127 {
4128 XGCValues xgcv;
4129 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4130 &xgcv);
4131 XSetForeground (s->display, s->gc, xgcv.background);
4132 XFillRectangle (s->display, pixmap, s->gc,
4133 0, 0, s->background_width, s->height);
4134 XSetForeground (s->display, s->gc, xgcv.foreground);
4135 }
4136 }
4137 else
4138 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4139
4140 s->background_filled_p = 1;
4141 }
4142
4143 /* Draw the foreground. */
4144 if (pixmap != None)
4145 {
4146 x_draw_image_foreground_1 (s, pixmap);
4147 x_set_glyph_string_clipping (s);
4148 XCopyArea (s->display, pixmap, s->window, s->gc,
4149 0, 0, s->background_width, s->height, s->x, s->y);
4150 XFreePixmap (s->display, pixmap);
4151 }
4152 else
4153 x_draw_image_foreground (s);
4154
4155 /* If we must draw a relief around the image, do it. */
4156 if (s->img->relief
4157 || s->hl == DRAW_IMAGE_RAISED
4158 || s->hl == DRAW_IMAGE_SUNKEN)
4159 x_draw_image_relief (s);
4160 }
4161
4162
4163 /* Draw stretch glyph string S. */
4164
4165 static void
4166 x_draw_stretch_glyph_string (s)
4167 struct glyph_string *s;
4168 {
4169 xassert (s->first_glyph->type == STRETCH_GLYPH);
4170 s->stippled_p = s->face->stipple != 0;
4171
4172 if (s->hl == DRAW_CURSOR
4173 && !x_stretch_cursor_p)
4174 {
4175 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4176 as wide as the stretch glyph. */
4177 int width = min (CANON_X_UNIT (s->f), s->background_width);
4178
4179 /* Draw cursor. */
4180 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4181
4182 /* Clear rest using the GC of the original non-cursor face. */
4183 if (width < s->background_width)
4184 {
4185 GC gc = s->face->gc;
4186 int x = s->x + width, y = s->y;
4187 int w = s->background_width - width, h = s->height;
4188 XRectangle r;
4189
4190 x_get_glyph_string_clip_rect (s, &r);
4191 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4192
4193 if (s->face->stipple)
4194 {
4195 /* Fill background with a stipple pattern. */
4196 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4197 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4198 XSetFillStyle (s->display, gc, FillSolid);
4199 }
4200 else
4201 {
4202 XGCValues xgcv;
4203 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4204 XSetForeground (s->display, gc, xgcv.background);
4205 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4206 XSetForeground (s->display, gc, xgcv.foreground);
4207 }
4208 }
4209 }
4210 else
4211 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4212 s->height);
4213
4214 s->background_filled_p = 1;
4215 }
4216
4217
4218 /* Draw glyph string S. */
4219
4220 static void
4221 x_draw_glyph_string (s)
4222 struct glyph_string *s;
4223 {
4224 /* If S draws into the background of its successor, draw the
4225 background of the successor first so that S can draw into it.
4226 This makes S->next use XDrawString instead of XDrawImageString. */
4227 if (s->next && s->right_overhang && !s->for_overlaps_p)
4228 {
4229 xassert (s->next->img == NULL);
4230 x_set_glyph_string_gc (s->next);
4231 x_set_glyph_string_clipping (s->next);
4232 x_draw_glyph_string_background (s->next, 1);
4233 }
4234
4235 /* Set up S->gc, set clipping and draw S. */
4236 x_set_glyph_string_gc (s);
4237 x_set_glyph_string_clipping (s);
4238
4239 switch (s->first_glyph->type)
4240 {
4241 case IMAGE_GLYPH:
4242 x_draw_image_glyph_string (s);
4243 break;
4244
4245 case STRETCH_GLYPH:
4246 x_draw_stretch_glyph_string (s);
4247 break;
4248
4249 case CHAR_GLYPH:
4250 if (s->for_overlaps_p)
4251 s->background_filled_p = 1;
4252 else
4253 x_draw_glyph_string_background (s, 0);
4254 x_draw_glyph_string_foreground (s);
4255 break;
4256
4257 case COMPOSITE_GLYPH:
4258 if (s->for_overlaps_p || s->gidx > 0)
4259 s->background_filled_p = 1;
4260 else
4261 x_draw_glyph_string_background (s, 1);
4262 x_draw_composite_glyph_string_foreground (s);
4263 break;
4264
4265 default:
4266 abort ();
4267 }
4268
4269 if (!s->for_overlaps_p)
4270 {
4271 /* Draw underline. */
4272 if (s->face->underline_p)
4273 {
4274 unsigned long tem, h;
4275 int y;
4276
4277 /* Get the underline thickness. Default is 1 pixel. */
4278 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4279 h = 1;
4280
4281 /* Get the underline position. This is the recommended
4282 vertical offset in pixels from the baseline to the top of
4283 the underline. This is a signed value according to the
4284 specs, and its default is
4285
4286 ROUND ((maximum descent) / 2), with
4287 ROUND(x) = floor (x + 0.5) */
4288
4289 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4290 y = s->ybase + (long) tem;
4291 else if (s->face->font)
4292 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4293 else
4294 y = s->height - h;
4295
4296 if (s->face->underline_defaulted_p)
4297 XFillRectangle (s->display, s->window, s->gc,
4298 s->x, y, s->width, h);
4299 else
4300 {
4301 XGCValues xgcv;
4302 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4303 XSetForeground (s->display, s->gc, s->face->underline_color);
4304 XFillRectangle (s->display, s->window, s->gc,
4305 s->x, y, s->width, h);
4306 XSetForeground (s->display, s->gc, xgcv.foreground);
4307 }
4308 }
4309
4310 /* Draw overline. */
4311 if (s->face->overline_p)
4312 {
4313 unsigned long dy = 0, h = 1;
4314
4315 if (s->face->overline_color_defaulted_p)
4316 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4317 s->width, h);
4318 else
4319 {
4320 XGCValues xgcv;
4321 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4322 XSetForeground (s->display, s->gc, s->face->overline_color);
4323 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4324 s->width, h);
4325 XSetForeground (s->display, s->gc, xgcv.foreground);
4326 }
4327 }
4328
4329 /* Draw strike-through. */
4330 if (s->face->strike_through_p)
4331 {
4332 unsigned long h = 1;
4333 unsigned long dy = (s->height - h) / 2;
4334
4335 if (s->face->strike_through_color_defaulted_p)
4336 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4337 s->width, h);
4338 else
4339 {
4340 XGCValues xgcv;
4341 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4342 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4343 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4344 s->width, h);
4345 XSetForeground (s->display, s->gc, xgcv.foreground);
4346 }
4347 }
4348
4349 /* Draw relief. */
4350 if (s->face->box != FACE_NO_BOX)
4351 x_draw_glyph_string_box (s);
4352 }
4353
4354 /* Reset clipping. */
4355 XSetClipMask (s->display, s->gc, None);
4356 }
4357
4358
4359 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4360 struct face **, int));
4361
4362
4363 /* Fill glyph string S with composition components specified by S->cmp.
4364
4365 FACES is an array of faces for all components of this composition.
4366 S->gidx is the index of the first component for S.
4367 OVERLAPS_P non-zero means S should draw the foreground only, and
4368 use its physical height for clipping.
4369
4370 Value is the index of a component not in S. */
4371
4372 static int
4373 x_fill_composite_glyph_string (s, faces, overlaps_p)
4374 struct glyph_string *s;
4375 struct face **faces;
4376 int overlaps_p;
4377 {
4378 int i;
4379
4380 xassert (s);
4381
4382 s->for_overlaps_p = overlaps_p;
4383
4384 s->face = faces[s->gidx];
4385 s->font = s->face->font;
4386 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4387
4388 /* For all glyphs of this composition, starting at the offset
4389 S->gidx, until we reach the end of the definition or encounter a
4390 glyph that requires the different face, add it to S. */
4391 ++s->nchars;
4392 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4393 ++s->nchars;
4394
4395 /* All glyph strings for the same composition has the same width,
4396 i.e. the width set for the first component of the composition. */
4397
4398 s->width = s->first_glyph->pixel_width;
4399
4400 /* If the specified font could not be loaded, use the frame's
4401 default font, but record the fact that we couldn't load it in
4402 the glyph string so that we can draw rectangles for the
4403 characters of the glyph string. */
4404 if (s->font == NULL)
4405 {
4406 s->font_not_found_p = 1;
4407 s->font = FRAME_FONT (s->f);
4408 }
4409
4410 /* Adjust base line for subscript/superscript text. */
4411 s->ybase += s->first_glyph->voffset;
4412
4413 xassert (s->face && s->face->gc);
4414
4415 /* This glyph string must always be drawn with 16-bit functions. */
4416 s->two_byte_p = 1;
4417
4418 return s->gidx + s->nchars;
4419 }
4420
4421
4422 /* Fill glyph string S from a sequence of character glyphs.
4423
4424 FACE_ID is the face id of the string. START is the index of the
4425 first glyph to consider, END is the index of the last + 1.
4426 OVERLAPS_P non-zero means S should draw the foreground only, and
4427 use its physical height for clipping.
4428
4429 Value is the index of the first glyph not in S. */
4430
4431 static int
4432 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4433 struct glyph_string *s;
4434 int face_id;
4435 int start, end, overlaps_p;
4436 {
4437 struct glyph *glyph, *last;
4438 int voffset;
4439 int glyph_not_available_p;
4440
4441 xassert (s->f == XFRAME (s->w->frame));
4442 xassert (s->nchars == 0);
4443 xassert (start >= 0 && end > start);
4444
4445 s->for_overlaps_p = overlaps_p,
4446 glyph = s->row->glyphs[s->area] + start;
4447 last = s->row->glyphs[s->area] + end;
4448 voffset = glyph->voffset;
4449
4450 glyph_not_available_p = glyph->glyph_not_available_p;
4451
4452 while (glyph < last
4453 && glyph->type == CHAR_GLYPH
4454 && glyph->voffset == voffset
4455 /* Same face id implies same font, nowadays. */
4456 && glyph->face_id == face_id
4457 && glyph->glyph_not_available_p == glyph_not_available_p)
4458 {
4459 int two_byte_p;
4460
4461 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4462 s->char2b + s->nchars,
4463 &two_byte_p);
4464 s->two_byte_p = two_byte_p;
4465 ++s->nchars;
4466 xassert (s->nchars <= end - start);
4467 s->width += glyph->pixel_width;
4468 ++glyph;
4469 }
4470
4471 s->font = s->face->font;
4472 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4473
4474 /* If the specified font could not be loaded, use the frame's font,
4475 but record the fact that we couldn't load it in
4476 S->font_not_found_p so that we can draw rectangles for the
4477 characters of the glyph string. */
4478 if (s->font == NULL || glyph_not_available_p)
4479 {
4480 s->font_not_found_p = 1;
4481 s->font = FRAME_FONT (s->f);
4482 }
4483
4484 /* Adjust base line for subscript/superscript text. */
4485 s->ybase += voffset;
4486
4487 xassert (s->face && s->face->gc);
4488 return glyph - s->row->glyphs[s->area];
4489 }
4490
4491
4492 /* Fill glyph string S from image glyph S->first_glyph. */
4493
4494 static void
4495 x_fill_image_glyph_string (s)
4496 struct glyph_string *s;
4497 {
4498 xassert (s->first_glyph->type == IMAGE_GLYPH);
4499 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4500 xassert (s->img);
4501 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4502 s->font = s->face->font;
4503 s->width = s->first_glyph->pixel_width;
4504
4505 /* Adjust base line for subscript/superscript text. */
4506 s->ybase += s->first_glyph->voffset;
4507 }
4508
4509
4510 /* Fill glyph string S from a sequence of stretch glyphs.
4511
4512 ROW is the glyph row in which the glyphs are found, AREA is the
4513 area within the row. START is the index of the first glyph to
4514 consider, END is the index of the last + 1.
4515
4516 Value is the index of the first glyph not in S. */
4517
4518 static int
4519 x_fill_stretch_glyph_string (s, row, area, start, end)
4520 struct glyph_string *s;
4521 struct glyph_row *row;
4522 enum glyph_row_area area;
4523 int start, end;
4524 {
4525 struct glyph *glyph, *last;
4526 int voffset, face_id;
4527
4528 xassert (s->first_glyph->type == STRETCH_GLYPH);
4529
4530 glyph = s->row->glyphs[s->area] + start;
4531 last = s->row->glyphs[s->area] + end;
4532 face_id = glyph->face_id;
4533 s->face = FACE_FROM_ID (s->f, face_id);
4534 s->font = s->face->font;
4535 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4536 s->width = glyph->pixel_width;
4537 voffset = glyph->voffset;
4538
4539 for (++glyph;
4540 (glyph < last
4541 && glyph->type == STRETCH_GLYPH
4542 && glyph->voffset == voffset
4543 && glyph->face_id == face_id);
4544 ++glyph)
4545 s->width += glyph->pixel_width;
4546
4547 /* Adjust base line for subscript/superscript text. */
4548 s->ybase += voffset;
4549
4550 xassert (s->face && s->face->gc);
4551 return glyph - s->row->glyphs[s->area];
4552 }
4553
4554
4555 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4556 of XChar2b structures for S; it can't be allocated in
4557 x_init_glyph_string because it must be allocated via `alloca'. W
4558 is the window on which S is drawn. ROW and AREA are the glyph row
4559 and area within the row from which S is constructed. START is the
4560 index of the first glyph structure covered by S. HL is a
4561 face-override for drawing S. */
4562
4563 static void
4564 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4565 struct glyph_string *s;
4566 XChar2b *char2b;
4567 struct window *w;
4568 struct glyph_row *row;
4569 enum glyph_row_area area;
4570 int start;
4571 enum draw_glyphs_face hl;
4572 {
4573 bzero (s, sizeof *s);
4574 s->w = w;
4575 s->f = XFRAME (w->frame);
4576 s->display = FRAME_X_DISPLAY (s->f);
4577 s->window = FRAME_X_WINDOW (s->f);
4578 s->char2b = char2b;
4579 s->hl = hl;
4580 s->row = row;
4581 s->area = area;
4582 s->first_glyph = row->glyphs[area] + start;
4583 s->height = row->height;
4584 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4585
4586 /* Display the internal border below the tool-bar window. */
4587 if (s->w == XWINDOW (s->f->tool_bar_window))
4588 s->y -= s->f->output_data.x->internal_border_width;
4589
4590 s->ybase = s->y + row->ascent;
4591 }
4592
4593
4594 /* Set background width of glyph string S. START is the index of the
4595 first glyph following S. LAST_X is the right-most x-position + 1
4596 in the drawing area. */
4597
4598 static INLINE void
4599 x_set_glyph_string_background_width (s, start, last_x)
4600 struct glyph_string *s;
4601 int start;
4602 int last_x;
4603 {
4604 /* If the face of this glyph string has to be drawn to the end of
4605 the drawing area, set S->extends_to_end_of_line_p. */
4606 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4607
4608 if (start == s->row->used[s->area]
4609 && s->hl == DRAW_NORMAL_TEXT
4610 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4611 || s->face->background != default_face->background
4612 || s->face->stipple != default_face->stipple))
4613 s->extends_to_end_of_line_p = 1;
4614
4615 /* If S extends its face to the end of the line, set its
4616 background_width to the distance to the right edge of the drawing
4617 area. */
4618 if (s->extends_to_end_of_line_p)
4619 s->background_width = last_x - s->x + 1;
4620 else
4621 s->background_width = s->width;
4622 }
4623
4624
4625 /* Add a glyph string for a stretch glyph to the list of strings
4626 between HEAD and TAIL. START is the index of the stretch glyph in
4627 row area AREA of glyph row ROW. END is the index of the last glyph
4628 in that glyph row area. X is the current output position assigned
4629 to the new glyph string constructed. HL overrides that face of the
4630 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4631 is the right-most x-position of the drawing area. */
4632
4633 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4634 and below -- keep them on one line. */
4635 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4636 do \
4637 { \
4638 s = (struct glyph_string *) alloca (sizeof *s); \
4639 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4640 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4641 x_append_glyph_string (&HEAD, &TAIL, s); \
4642 s->x = (X); \
4643 } \
4644 while (0)
4645
4646
4647 /* Add a glyph string for an image glyph to the list of strings
4648 between HEAD and TAIL. START is the index of the image glyph in
4649 row area AREA of glyph row ROW. END is the index of the last glyph
4650 in that glyph row area. X is the current output position assigned
4651 to the new glyph string constructed. HL overrides that face of the
4652 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4653 is the right-most x-position of the drawing area. */
4654
4655 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4656 do \
4657 { \
4658 s = (struct glyph_string *) alloca (sizeof *s); \
4659 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4660 x_fill_image_glyph_string (s); \
4661 x_append_glyph_string (&HEAD, &TAIL, s); \
4662 ++START; \
4663 s->x = (X); \
4664 } \
4665 while (0)
4666
4667
4668 /* Add a glyph string for a sequence of character glyphs to the list
4669 of strings between HEAD and TAIL. START is the index of the first
4670 glyph in row area AREA of glyph row ROW that is part of the new
4671 glyph string. END is the index of the last glyph in that glyph row
4672 area. X is the current output position assigned to the new glyph
4673 string constructed. HL overrides that face of the glyph; e.g. it
4674 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4675 right-most x-position of the drawing area. */
4676
4677 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4678 do \
4679 { \
4680 int c, face_id; \
4681 XChar2b *char2b; \
4682 \
4683 c = (ROW)->glyphs[AREA][START].u.ch; \
4684 face_id = (ROW)->glyphs[AREA][START].face_id; \
4685 \
4686 s = (struct glyph_string *) alloca (sizeof *s); \
4687 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4688 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4689 x_append_glyph_string (&HEAD, &TAIL, s); \
4690 s->x = (X); \
4691 START = x_fill_glyph_string (s, face_id, START, END, \
4692 OVERLAPS_P); \
4693 } \
4694 while (0)
4695
4696
4697 /* Add a glyph string for a composite sequence to the list of strings
4698 between HEAD and TAIL. START is the index of the first glyph in
4699 row area AREA of glyph row ROW that is part of the new glyph
4700 string. END is the index of the last glyph in that glyph row area.
4701 X is the current output position assigned to the new glyph string
4702 constructed. HL overrides that face of the glyph; e.g. it is
4703 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4704 x-position of the drawing area. */
4705
4706 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4707 do { \
4708 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4709 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4710 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4711 struct composition *cmp = composition_table[cmp_id]; \
4712 int glyph_len = cmp->glyph_len; \
4713 XChar2b *char2b; \
4714 struct face **faces; \
4715 struct glyph_string *first_s = NULL; \
4716 int n; \
4717 \
4718 base_face = base_face->ascii_face; \
4719 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4720 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4721 /* At first, fill in `char2b' and `faces'. */ \
4722 for (n = 0; n < glyph_len; n++) \
4723 { \
4724 int c = COMPOSITION_GLYPH (cmp, n); \
4725 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4726 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4727 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4728 this_face_id, char2b + n, 1); \
4729 } \
4730 \
4731 /* Make glyph_strings for each glyph sequence that is drawable by \
4732 the same face, and append them to HEAD/TAIL. */ \
4733 for (n = 0; n < cmp->glyph_len;) \
4734 { \
4735 s = (struct glyph_string *) alloca (sizeof *s); \
4736 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4737 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4738 s->cmp = cmp; \
4739 s->gidx = n; \
4740 s->x = (X); \
4741 \
4742 if (n == 0) \
4743 first_s = s; \
4744 \
4745 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4746 } \
4747 \
4748 ++START; \
4749 s = first_s; \
4750 } while (0)
4751
4752
4753 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4754 of AREA of glyph row ROW on window W between indices START and END.
4755 HL overrides the face for drawing glyph strings, e.g. it is
4756 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4757 x-positions of the drawing area.
4758
4759 This is an ugly monster macro construct because we must use alloca
4760 to allocate glyph strings (because x_draw_glyphs can be called
4761 asynchronously). */
4762
4763 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4764 do \
4765 { \
4766 HEAD = TAIL = NULL; \
4767 while (START < END) \
4768 { \
4769 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4770 switch (first_glyph->type) \
4771 { \
4772 case CHAR_GLYPH: \
4773 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4774 TAIL, HL, X, LAST_X, \
4775 OVERLAPS_P); \
4776 break; \
4777 \
4778 case COMPOSITE_GLYPH: \
4779 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4780 HEAD, TAIL, HL, X, LAST_X,\
4781 OVERLAPS_P); \
4782 break; \
4783 \
4784 case STRETCH_GLYPH: \
4785 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4786 HEAD, TAIL, HL, X, LAST_X); \
4787 break; \
4788 \
4789 case IMAGE_GLYPH: \
4790 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4791 TAIL, HL, X, LAST_X); \
4792 break; \
4793 \
4794 default: \
4795 abort (); \
4796 } \
4797 \
4798 x_set_glyph_string_background_width (s, START, LAST_X); \
4799 (X) += s->width; \
4800 } \
4801 } \
4802 while (0)
4803
4804
4805 /* Draw glyphs between START and END in AREA of ROW on window W,
4806 starting at x-position X. X is relative to AREA in W. HL is a
4807 face-override with the following meaning:
4808
4809 DRAW_NORMAL_TEXT draw normally
4810 DRAW_CURSOR draw in cursor face
4811 DRAW_MOUSE_FACE draw in mouse face.
4812 DRAW_INVERSE_VIDEO draw in mode line face
4813 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4814 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4815
4816 If REAL_START is non-null, return in *REAL_START the real starting
4817 position for display. This can be different from START in case
4818 overlapping glyphs must be displayed. If REAL_END is non-null,
4819 return in *REAL_END the real end position for display. This can be
4820 different from END in case overlapping glyphs must be displayed.
4821
4822 If OVERLAPS_P is non-zero, draw only the foreground of characters
4823 and clip to the physical height of ROW.
4824
4825 Value is the x-position reached, relative to AREA of W. */
4826
4827 static int
4828 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4829 overlaps_p)
4830 struct window *w;
4831 int x;
4832 struct glyph_row *row;
4833 enum glyph_row_area area;
4834 int start, end;
4835 enum draw_glyphs_face hl;
4836 int *real_start, *real_end;
4837 int overlaps_p;
4838 {
4839 struct glyph_string *head, *tail;
4840 struct glyph_string *s;
4841 int last_x, area_width;
4842 int x_reached;
4843 int i, j;
4844
4845 /* Let's rather be paranoid than getting a SEGV. */
4846 end = min (end, row->used[area]);
4847 start = max (0, start);
4848 start = min (end, start);
4849 if (real_start)
4850 *real_start = start;
4851 if (real_end)
4852 *real_end = end;
4853
4854 /* Translate X to frame coordinates. Set last_x to the right
4855 end of the drawing area. */
4856 if (row->full_width_p)
4857 {
4858 /* X is relative to the left edge of W, without scroll bars
4859 or flag areas. */
4860 struct frame *f = XFRAME (w->frame);
4861 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4862 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4863
4864 x += window_left_x;
4865 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4866 last_x = window_left_x + area_width;
4867
4868 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4869 {
4870 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4871 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4872 last_x += width;
4873 else
4874 x -= width;
4875 }
4876
4877 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4878 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4879 }
4880 else
4881 {
4882 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4883 area_width = window_box_width (w, area);
4884 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4885 }
4886
4887 /* Build a doubly-linked list of glyph_string structures between
4888 head and tail from what we have to draw. Note that the macro
4889 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4890 the reason we use a separate variable `i'. */
4891 i = start;
4892 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4893 overlaps_p);
4894 if (tail)
4895 x_reached = tail->x + tail->background_width;
4896 else
4897 x_reached = x;
4898
4899 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4900 the row, redraw some glyphs in front or following the glyph
4901 strings built above. */
4902 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4903 {
4904 int dummy_x = 0;
4905 struct glyph_string *h, *t;
4906
4907 /* Compute overhangs for all glyph strings. */
4908 for (s = head; s; s = s->next)
4909 x_compute_glyph_string_overhangs (s);
4910
4911 /* Prepend glyph strings for glyphs in front of the first glyph
4912 string that are overwritten because of the first glyph
4913 string's left overhang. The background of all strings
4914 prepended must be drawn because the first glyph string
4915 draws over it. */
4916 i = x_left_overwritten (head);
4917 if (i >= 0)
4918 {
4919 j = i;
4920 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4921 DRAW_NORMAL_TEXT, dummy_x, last_x,
4922 overlaps_p);
4923 start = i;
4924 if (real_start)
4925 *real_start = start;
4926 x_compute_overhangs_and_x (t, head->x, 1);
4927 x_prepend_glyph_string_lists (&head, &tail, h, t);
4928 }
4929
4930 /* Prepend glyph strings for glyphs in front of the first glyph
4931 string that overwrite that glyph string because of their
4932 right overhang. For these strings, only the foreground must
4933 be drawn, because it draws over the glyph string at `head'.
4934 The background must not be drawn because this would overwrite
4935 right overhangs of preceding glyphs for which no glyph
4936 strings exist. */
4937 i = x_left_overwriting (head);
4938 if (i >= 0)
4939 {
4940 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4941 DRAW_NORMAL_TEXT, dummy_x, last_x,
4942 overlaps_p);
4943 for (s = h; s; s = s->next)
4944 s->background_filled_p = 1;
4945 if (real_start)
4946 *real_start = i;
4947 x_compute_overhangs_and_x (t, head->x, 1);
4948 x_prepend_glyph_string_lists (&head, &tail, h, t);
4949 }
4950
4951 /* Append glyphs strings for glyphs following the last glyph
4952 string tail that are overwritten by tail. The background of
4953 these strings has to be drawn because tail's foreground draws
4954 over it. */
4955 i = x_right_overwritten (tail);
4956 if (i >= 0)
4957 {
4958 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4959 DRAW_NORMAL_TEXT, x, last_x,
4960 overlaps_p);
4961 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4962 x_append_glyph_string_lists (&head, &tail, h, t);
4963 if (real_end)
4964 *real_end = i;
4965 }
4966
4967 /* Append glyph strings for glyphs following the last glyph
4968 string tail that overwrite tail. The foreground of such
4969 glyphs has to be drawn because it writes into the background
4970 of tail. The background must not be drawn because it could
4971 paint over the foreground of following glyphs. */
4972 i = x_right_overwriting (tail);
4973 if (i >= 0)
4974 {
4975 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4976 DRAW_NORMAL_TEXT, x, last_x,
4977 overlaps_p);
4978 for (s = h; s; s = s->next)
4979 s->background_filled_p = 1;
4980 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4981 x_append_glyph_string_lists (&head, &tail, h, t);
4982 if (real_end)
4983 *real_end = i;
4984 }
4985 }
4986
4987 /* Draw all strings. */
4988 for (s = head; s; s = s->next)
4989 x_draw_glyph_string (s);
4990
4991 /* Value is the x-position up to which drawn, relative to AREA of W.
4992 This doesn't include parts drawn because of overhangs. */
4993 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4994 if (!row->full_width_p)
4995 {
4996 if (area > LEFT_MARGIN_AREA)
4997 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4998 if (area > TEXT_AREA)
4999 x_reached -= window_box_width (w, TEXT_AREA);
5000 }
5001
5002 return x_reached;
5003 }
5004
5005
5006 /* Fix the display of area AREA of overlapping row ROW in window W. */
5007
5008 static void
5009 x_fix_overlapping_area (w, row, area)
5010 struct window *w;
5011 struct glyph_row *row;
5012 enum glyph_row_area area;
5013 {
5014 int i, x;
5015
5016 BLOCK_INPUT;
5017
5018 if (area == LEFT_MARGIN_AREA)
5019 x = 0;
5020 else if (area == TEXT_AREA)
5021 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5022 else
5023 x = (window_box_width (w, LEFT_MARGIN_AREA)
5024 + window_box_width (w, TEXT_AREA));
5025
5026 for (i = 0; i < row->used[area];)
5027 {
5028 if (row->glyphs[area][i].overlaps_vertically_p)
5029 {
5030 int start = i, start_x = x;
5031
5032 do
5033 {
5034 x += row->glyphs[area][i].pixel_width;
5035 ++i;
5036 }
5037 while (i < row->used[area]
5038 && row->glyphs[area][i].overlaps_vertically_p);
5039
5040 x_draw_glyphs (w, start_x, row, area, start, i,
5041 (row->inverse_p
5042 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5043 NULL, NULL, 1);
5044 }
5045 else
5046 {
5047 x += row->glyphs[area][i].pixel_width;
5048 ++i;
5049 }
5050 }
5051
5052 UNBLOCK_INPUT;
5053 }
5054
5055
5056 /* Output LEN glyphs starting at START at the nominal cursor position.
5057 Advance the nominal cursor over the text. The global variable
5058 updated_window contains the window being updated, updated_row is
5059 the glyph row being updated, and updated_area is the area of that
5060 row being updated. */
5061
5062 static void
5063 x_write_glyphs (start, len)
5064 struct glyph *start;
5065 int len;
5066 {
5067 int x, hpos, real_start, real_end;
5068
5069 xassert (updated_window && updated_row);
5070 BLOCK_INPUT;
5071
5072 /* Write glyphs. */
5073
5074 hpos = start - updated_row->glyphs[updated_area];
5075 x = x_draw_glyphs (updated_window, output_cursor.x,
5076 updated_row, updated_area,
5077 hpos, hpos + len,
5078 (updated_row->inverse_p
5079 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5080 &real_start, &real_end, 0);
5081
5082 /* If we drew over the cursor, note that it is not visible any more. */
5083 note_overwritten_text_cursor (updated_window, real_start,
5084 real_end - real_start);
5085
5086 UNBLOCK_INPUT;
5087
5088 /* Advance the output cursor. */
5089 output_cursor.hpos += len;
5090 output_cursor.x = x;
5091 }
5092
5093
5094 /* Insert LEN glyphs from START at the nominal cursor position. */
5095
5096 static void
5097 x_insert_glyphs (start, len)
5098 struct glyph *start;
5099 register int len;
5100 {
5101 struct frame *f;
5102 struct window *w;
5103 int line_height, shift_by_width, shifted_region_width;
5104 struct glyph_row *row;
5105 struct glyph *glyph;
5106 int frame_x, frame_y, hpos, real_start, real_end;
5107
5108 xassert (updated_window && updated_row);
5109 BLOCK_INPUT;
5110 w = updated_window;
5111 f = XFRAME (WINDOW_FRAME (w));
5112
5113 /* Get the height of the line we are in. */
5114 row = updated_row;
5115 line_height = row->height;
5116
5117 /* Get the width of the glyphs to insert. */
5118 shift_by_width = 0;
5119 for (glyph = start; glyph < start + len; ++glyph)
5120 shift_by_width += glyph->pixel_width;
5121
5122 /* Get the width of the region to shift right. */
5123 shifted_region_width = (window_box_width (w, updated_area)
5124 - output_cursor.x
5125 - shift_by_width);
5126
5127 /* Shift right. */
5128 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5129 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5130 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5131 f->output_data.x->normal_gc,
5132 frame_x, frame_y,
5133 shifted_region_width, line_height,
5134 frame_x + shift_by_width, frame_y);
5135
5136 /* Write the glyphs. */
5137 hpos = start - row->glyphs[updated_area];
5138 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5139 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5140 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5141
5142 /* Advance the output cursor. */
5143 output_cursor.hpos += len;
5144 output_cursor.x += shift_by_width;
5145 UNBLOCK_INPUT;
5146 }
5147
5148
5149 /* Delete N glyphs at the nominal cursor position. Not implemented
5150 for X frames. */
5151
5152 static void
5153 x_delete_glyphs (n)
5154 register int n;
5155 {
5156 abort ();
5157 }
5158
5159
5160 /* Erase the current text line from the nominal cursor position
5161 (inclusive) to pixel column TO_X (exclusive). The idea is that
5162 everything from TO_X onward is already erased.
5163
5164 TO_X is a pixel position relative to updated_area of
5165 updated_window. TO_X == -1 means clear to the end of this area. */
5166
5167 static void
5168 x_clear_end_of_line (to_x)
5169 int to_x;
5170 {
5171 struct frame *f;
5172 struct window *w = updated_window;
5173 int max_x, min_y, max_y;
5174 int from_x, from_y, to_y;
5175
5176 xassert (updated_window && updated_row);
5177 f = XFRAME (w->frame);
5178
5179 if (updated_row->full_width_p)
5180 {
5181 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5182 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5183 && !w->pseudo_window_p)
5184 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5185 }
5186 else
5187 max_x = window_box_width (w, updated_area);
5188 max_y = window_text_bottom_y (w);
5189
5190 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5191 of window. For TO_X > 0, truncate to end of drawing area. */
5192 if (to_x == 0)
5193 return;
5194 else if (to_x < 0)
5195 to_x = max_x;
5196 else
5197 to_x = min (to_x, max_x);
5198
5199 to_y = min (max_y, output_cursor.y + updated_row->height);
5200
5201 /* Notice if the cursor will be cleared by this operation. */
5202 if (!updated_row->full_width_p)
5203 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5204
5205 from_x = output_cursor.x;
5206
5207 /* Translate to frame coordinates. */
5208 if (updated_row->full_width_p)
5209 {
5210 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5211 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5212 }
5213 else
5214 {
5215 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5216 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5217 }
5218
5219 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5220 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5221 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5222
5223 /* Prevent inadvertently clearing to end of the X window. */
5224 if (to_x > from_x && to_y > from_y)
5225 {
5226 BLOCK_INPUT;
5227 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5228 from_x, from_y, to_x - from_x, to_y - from_y,
5229 False);
5230 UNBLOCK_INPUT;
5231 }
5232 }
5233
5234
5235 /* Clear entire frame. If updating_frame is non-null, clear that
5236 frame. Otherwise clear the selected frame. */
5237
5238 static void
5239 x_clear_frame ()
5240 {
5241 struct frame *f;
5242
5243 if (updating_frame)
5244 f = updating_frame;
5245 else
5246 f = SELECTED_FRAME ();
5247
5248 /* Clearing the frame will erase any cursor, so mark them all as no
5249 longer visible. */
5250 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5251 output_cursor.hpos = output_cursor.vpos = 0;
5252 output_cursor.x = -1;
5253
5254 /* We don't set the output cursor here because there will always
5255 follow an explicit cursor_to. */
5256 BLOCK_INPUT;
5257 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5258
5259 /* We have to clear the scroll bars, too. If we have changed
5260 colors or something like that, then they should be notified. */
5261 x_scroll_bar_clear (f);
5262
5263 XFlush (FRAME_X_DISPLAY (f));
5264 UNBLOCK_INPUT;
5265 }
5266
5267
5268 \f
5269 /* Invert the middle quarter of the frame for .15 sec. */
5270
5271 /* We use the select system call to do the waiting, so we have to make
5272 sure it's available. If it isn't, we just won't do visual bells. */
5273
5274 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5275
5276
5277 /* Subtract the `struct timeval' values X and Y, storing the result in
5278 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5279
5280 static int
5281 timeval_subtract (result, x, y)
5282 struct timeval *result, x, y;
5283 {
5284 /* Perform the carry for the later subtraction by updating y. This
5285 is safer because on some systems the tv_sec member is unsigned. */
5286 if (x.tv_usec < y.tv_usec)
5287 {
5288 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5289 y.tv_usec -= 1000000 * nsec;
5290 y.tv_sec += nsec;
5291 }
5292
5293 if (x.tv_usec - y.tv_usec > 1000000)
5294 {
5295 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5296 y.tv_usec += 1000000 * nsec;
5297 y.tv_sec -= nsec;
5298 }
5299
5300 /* Compute the time remaining to wait. tv_usec is certainly
5301 positive. */
5302 result->tv_sec = x.tv_sec - y.tv_sec;
5303 result->tv_usec = x.tv_usec - y.tv_usec;
5304
5305 /* Return indication of whether the result should be considered
5306 negative. */
5307 return x.tv_sec < y.tv_sec;
5308 }
5309
5310 void
5311 XTflash (f)
5312 struct frame *f;
5313 {
5314 BLOCK_INPUT;
5315
5316 {
5317 GC gc;
5318
5319 /* Create a GC that will use the GXxor function to flip foreground
5320 pixels into background pixels. */
5321 {
5322 XGCValues values;
5323
5324 values.function = GXxor;
5325 values.foreground = (f->output_data.x->foreground_pixel
5326 ^ f->output_data.x->background_pixel);
5327
5328 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5329 GCFunction | GCForeground, &values);
5330 }
5331
5332 {
5333 /* Get the height not including a menu bar widget. */
5334 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5335 /* Height of each line to flash. */
5336 int flash_height = FRAME_LINE_HEIGHT (f);
5337 /* These will be the left and right margins of the rectangles. */
5338 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5339 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5340
5341 int width;
5342
5343 /* Don't flash the area between a scroll bar and the frame
5344 edge it is next to. */
5345 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5346 {
5347 case vertical_scroll_bar_left:
5348 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5349 break;
5350
5351 case vertical_scroll_bar_right:
5352 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5353 break;
5354
5355 default:
5356 break;
5357 }
5358
5359 width = flash_right - flash_left;
5360
5361 /* If window is tall, flash top and bottom line. */
5362 if (height > 3 * FRAME_LINE_HEIGHT (f))
5363 {
5364 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5365 flash_left,
5366 (FRAME_INTERNAL_BORDER_WIDTH (f)
5367 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5368 width, flash_height);
5369 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5370 flash_left,
5371 (height - flash_height
5372 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5373 width, flash_height);
5374 }
5375 else
5376 /* If it is short, flash it all. */
5377 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5378 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5379 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5380
5381 x_flush (f);
5382
5383 {
5384 struct timeval wakeup;
5385
5386 EMACS_GET_TIME (wakeup);
5387
5388 /* Compute time to wait until, propagating carry from usecs. */
5389 wakeup.tv_usec += 150000;
5390 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5391 wakeup.tv_usec %= 1000000;
5392
5393 /* Keep waiting until past the time wakeup. */
5394 while (1)
5395 {
5396 struct timeval timeout;
5397
5398 EMACS_GET_TIME (timeout);
5399
5400 /* In effect, timeout = wakeup - timeout.
5401 Break if result would be negative. */
5402 if (timeval_subtract (&timeout, wakeup, timeout))
5403 break;
5404
5405 /* Try to wait that long--but we might wake up sooner. */
5406 select (0, NULL, NULL, NULL, &timeout);
5407 }
5408 }
5409
5410 /* If window is tall, flash top and bottom line. */
5411 if (height > 3 * FRAME_LINE_HEIGHT (f))
5412 {
5413 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5414 flash_left,
5415 (FRAME_INTERNAL_BORDER_WIDTH (f)
5416 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5417 width, flash_height);
5418 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5419 flash_left,
5420 (height - flash_height
5421 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5422 width, flash_height);
5423 }
5424 else
5425 /* If it is short, flash it all. */
5426 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5427 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5428 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5429
5430 XFreeGC (FRAME_X_DISPLAY (f), gc);
5431 x_flush (f);
5432 }
5433 }
5434
5435 UNBLOCK_INPUT;
5436 }
5437
5438 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5439
5440
5441 /* Make audible bell. */
5442
5443 void
5444 XTring_bell ()
5445 {
5446 struct frame *f = SELECTED_FRAME ();
5447
5448 if (FRAME_X_DISPLAY (f))
5449 {
5450 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5451 if (visible_bell)
5452 XTflash (f);
5453 else
5454 #endif
5455 {
5456 BLOCK_INPUT;
5457 XBell (FRAME_X_DISPLAY (f), 0);
5458 XFlush (FRAME_X_DISPLAY (f));
5459 UNBLOCK_INPUT;
5460 }
5461 }
5462 }
5463
5464 \f
5465 /* Specify how many text lines, from the top of the window,
5466 should be affected by insert-lines and delete-lines operations.
5467 This, and those operations, are used only within an update
5468 that is bounded by calls to x_update_begin and x_update_end. */
5469
5470 static void
5471 XTset_terminal_window (n)
5472 register int n;
5473 {
5474 /* This function intentionally left blank. */
5475 }
5476
5477
5478 \f
5479 /***********************************************************************
5480 Line Dance
5481 ***********************************************************************/
5482
5483 /* Perform an insert-lines or delete-lines operation, inserting N
5484 lines or deleting -N lines at vertical position VPOS. */
5485
5486 static void
5487 x_ins_del_lines (vpos, n)
5488 int vpos, n;
5489 {
5490 abort ();
5491 }
5492
5493
5494 /* Scroll part of the display as described by RUN. */
5495
5496 static void
5497 x_scroll_run (w, run)
5498 struct window *w;
5499 struct run *run;
5500 {
5501 struct frame *f = XFRAME (w->frame);
5502 int x, y, width, height, from_y, to_y, bottom_y;
5503
5504 /* Get frame-relative bounding box of the text display area of W,
5505 without mode lines. Include in this box the flags areas to the
5506 left and right of W. */
5507 window_box (w, -1, &x, &y, &width, &height);
5508 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5509 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5510
5511 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5512 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5513 bottom_y = y + height;
5514
5515 if (to_y < from_y)
5516 {
5517 /* Scrolling up. Make sure we don't copy part of the mode
5518 line at the bottom. */
5519 if (from_y + run->height > bottom_y)
5520 height = bottom_y - from_y;
5521 else
5522 height = run->height;
5523 }
5524 else
5525 {
5526 /* Scolling down. Make sure we don't copy over the mode line.
5527 at the bottom. */
5528 if (to_y + run->height > bottom_y)
5529 height = bottom_y - to_y;
5530 else
5531 height = run->height;
5532 }
5533
5534 BLOCK_INPUT;
5535
5536 /* Cursor off. Will be switched on again in x_update_window_end. */
5537 updated_window = w;
5538 x_clear_cursor (w);
5539
5540 XCopyArea (FRAME_X_DISPLAY (f),
5541 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5542 f->output_data.x->normal_gc,
5543 x, from_y,
5544 width, height,
5545 x, to_y);
5546
5547 UNBLOCK_INPUT;
5548 }
5549
5550
5551 \f
5552 /***********************************************************************
5553 Exposure Events
5554 ***********************************************************************/
5555
5556 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5557 corner of the exposed rectangle. W and H are width and height of
5558 the exposed area. All are pixel values. W or H zero means redraw
5559 the entire frame. */
5560
5561 static void
5562 expose_frame (f, x, y, w, h)
5563 struct frame *f;
5564 int x, y, w, h;
5565 {
5566 XRectangle r;
5567
5568 TRACE ((stderr, "expose_frame "));
5569
5570 /* No need to redraw if frame will be redrawn soon. */
5571 if (FRAME_GARBAGED_P (f))
5572 {
5573 TRACE ((stderr, " garbaged\n"));
5574 return;
5575 }
5576
5577 /* If basic faces haven't been realized yet, there is no point in
5578 trying to redraw anything. This can happen when we get an expose
5579 event while Emacs is starting, e.g. by moving another window. */
5580 if (FRAME_FACE_CACHE (f) == NULL
5581 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5582 {
5583 TRACE ((stderr, " no faces\n"));
5584 return;
5585 }
5586
5587 if (w == 0 || h == 0)
5588 {
5589 r.x = r.y = 0;
5590 r.width = CANON_X_UNIT (f) * f->width;
5591 r.height = CANON_Y_UNIT (f) * f->height;
5592 }
5593 else
5594 {
5595 r.x = x;
5596 r.y = y;
5597 r.width = w;
5598 r.height = h;
5599 }
5600
5601 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5602 expose_window_tree (XWINDOW (f->root_window), &r);
5603
5604 if (WINDOWP (f->tool_bar_window))
5605 {
5606 struct window *w = XWINDOW (f->tool_bar_window);
5607 XRectangle window_rect;
5608 XRectangle intersection_rect;
5609 int window_x, window_y, window_width, window_height;
5610
5611
5612 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5613 window_rect.x = window_x;
5614 window_rect.y = window_y;
5615 window_rect.width = window_width;
5616 window_rect.height = window_height;
5617
5618 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5619 expose_window (w, &intersection_rect);
5620 }
5621
5622 #ifndef USE_X_TOOLKIT
5623 if (WINDOWP (f->menu_bar_window))
5624 {
5625 struct window *w = XWINDOW (f->menu_bar_window);
5626 XRectangle window_rect;
5627 XRectangle intersection_rect;
5628 int window_x, window_y, window_width, window_height;
5629
5630
5631 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5632 window_rect.x = window_x;
5633 window_rect.y = window_y;
5634 window_rect.width = window_width;
5635 window_rect.height = window_height;
5636
5637 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5638 expose_window (w, &intersection_rect);
5639 }
5640 #endif /* not USE_X_TOOLKIT */
5641 }
5642
5643
5644 /* Redraw (parts) of all windows in the window tree rooted at W that
5645 intersect R. R contains frame pixel coordinates. */
5646
5647 static void
5648 expose_window_tree (w, r)
5649 struct window *w;
5650 XRectangle *r;
5651 {
5652 while (w)
5653 {
5654 if (!NILP (w->hchild))
5655 expose_window_tree (XWINDOW (w->hchild), r);
5656 else if (!NILP (w->vchild))
5657 expose_window_tree (XWINDOW (w->vchild), r);
5658 else
5659 {
5660 XRectangle window_rect;
5661 XRectangle intersection_rect;
5662 struct frame *f = XFRAME (w->frame);
5663 int window_x, window_y, window_width, window_height;
5664
5665 /* Frame-relative pixel rectangle of W. */
5666 window_box (w, -1, &window_x, &window_y, &window_width,
5667 &window_height);
5668 window_rect.x
5669 = (window_x
5670 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5671 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5672 window_rect.y = window_y;
5673 window_rect.width
5674 = (window_width
5675 + FRAME_X_FLAGS_AREA_WIDTH (f)
5676 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5677 window_rect.height
5678 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5679
5680 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5681 expose_window (w, &intersection_rect);
5682 }
5683
5684 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5685 }
5686 }
5687
5688
5689 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5690 which intersects rectangle R. R is in window-relative coordinates. */
5691
5692 static void
5693 expose_area (w, row, r, area)
5694 struct window *w;
5695 struct glyph_row *row;
5696 XRectangle *r;
5697 enum glyph_row_area area;
5698 {
5699 struct glyph *first = row->glyphs[area];
5700 struct glyph *end = row->glyphs[area] + row->used[area];
5701 struct glyph *last;
5702 int first_x, start_x, x;
5703
5704 if (area == TEXT_AREA && row->fill_line_p)
5705 /* If row extends face to end of line write the whole line. */
5706 x_draw_glyphs (w, 0, row, area,
5707 0, row->used[area],
5708 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5709 NULL, NULL, 0);
5710 else
5711 {
5712 /* Set START_X to the window-relative start position for drawing glyphs of
5713 AREA. The first glyph of the text area can be partially visible.
5714 The first glyphs of other areas cannot. */
5715 if (area == LEFT_MARGIN_AREA)
5716 start_x = 0;
5717 else if (area == TEXT_AREA)
5718 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5719 else
5720 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5721 + window_box_width (w, TEXT_AREA));
5722 x = start_x;
5723
5724 /* Find the first glyph that must be redrawn. */
5725 while (first < end
5726 && x + first->pixel_width < r->x)
5727 {
5728 x += first->pixel_width;
5729 ++first;
5730 }
5731
5732 /* Find the last one. */
5733 last = first;
5734 first_x = x;
5735 while (last < end
5736 && x < r->x + r->width)
5737 {
5738 x += last->pixel_width;
5739 ++last;
5740 }
5741
5742 /* Repaint. */
5743 if (last > first)
5744 x_draw_glyphs (w, first_x - start_x, row, area,
5745 first - row->glyphs[area],
5746 last - row->glyphs[area],
5747 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5748 NULL, NULL, 0);
5749 }
5750 }
5751
5752
5753 /* Redraw the parts of the glyph row ROW on window W intersecting
5754 rectangle R. R is in window-relative coordinates. */
5755
5756 static void
5757 expose_line (w, row, r)
5758 struct window *w;
5759 struct glyph_row *row;
5760 XRectangle *r;
5761 {
5762 xassert (row->enabled_p);
5763
5764 if (row->mode_line_p || w->pseudo_window_p)
5765 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5766 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5767 NULL, NULL, 0);
5768 else
5769 {
5770 if (row->used[LEFT_MARGIN_AREA])
5771 expose_area (w, row, r, LEFT_MARGIN_AREA);
5772 if (row->used[TEXT_AREA])
5773 expose_area (w, row, r, TEXT_AREA);
5774 if (row->used[RIGHT_MARGIN_AREA])
5775 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5776 x_draw_row_bitmaps (w, row);
5777 }
5778 }
5779
5780
5781 /* Return non-zero if W's cursor intersects rectangle R. */
5782
5783 static int
5784 x_phys_cursor_in_rect_p (w, r)
5785 struct window *w;
5786 XRectangle *r;
5787 {
5788 XRectangle cr, result;
5789 struct glyph *cursor_glyph;
5790
5791 cursor_glyph = get_phys_cursor_glyph (w);
5792 if (cursor_glyph)
5793 {
5794 cr.x = w->phys_cursor.x;
5795 cr.y = w->phys_cursor.y;
5796 cr.width = cursor_glyph->pixel_width;
5797 cr.height = w->phys_cursor_height;
5798 return x_intersect_rectangles (&cr, r, &result);
5799 }
5800 else
5801 return 0;
5802 }
5803
5804
5805 /* Redraw a rectangle of window W. R is a rectangle in window
5806 relative coordinates. Call this function with input blocked. */
5807
5808 static void
5809 expose_window (w, r)
5810 struct window *w;
5811 XRectangle *r;
5812 {
5813 struct glyph_row *row;
5814 int y;
5815 int yb = window_text_bottom_y (w);
5816 int cursor_cleared_p;
5817
5818 /* If window is not yet fully initialized, do nothing. This can
5819 happen when toolkit scroll bars are used and a window is split.
5820 Reconfiguring the scroll bar will generate an expose for a newly
5821 created window. */
5822 if (w->current_matrix == NULL || w == updated_window)
5823 return;
5824
5825 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5826 r->x, r->y, r->width, r->height));
5827
5828 /* Convert to window coordinates. */
5829 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5830 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5831
5832 /* Turn off the cursor. */
5833 if (!w->pseudo_window_p
5834 && x_phys_cursor_in_rect_p (w, r))
5835 {
5836 x_clear_cursor (w);
5837 cursor_cleared_p = 1;
5838 }
5839 else
5840 cursor_cleared_p = 0;
5841
5842 /* Find the first row intersecting the rectangle R. */
5843 row = w->current_matrix->rows;
5844 y = 0;
5845 while (row->enabled_p
5846 && y < yb
5847 && y + row->height < r->y)
5848 {
5849 y += row->height;
5850 ++row;
5851 }
5852
5853 /* Display the text in the rectangle, one text line at a time. */
5854 while (row->enabled_p
5855 && y < yb
5856 && y < r->y + r->height)
5857 {
5858 expose_line (w, row, r);
5859 y += row->height;
5860 ++row;
5861 }
5862
5863 /* Display the mode line if there is one. */
5864 if (WINDOW_WANTS_MODELINE_P (w)
5865 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5866 row->enabled_p)
5867 && row->y < r->y + r->height)
5868 expose_line (w, row, r);
5869
5870 if (!w->pseudo_window_p)
5871 {
5872 /* Draw border between windows. */
5873 x_draw_vertical_border (w);
5874
5875 /* Turn the cursor on again. */
5876 if (cursor_cleared_p)
5877 x_update_window_cursor (w, 1);
5878 }
5879 }
5880
5881
5882 /* Determine the intersection of two rectangles R1 and R2. Return
5883 the intersection in *RESULT. Value is non-zero if RESULT is not
5884 empty. */
5885
5886 static int
5887 x_intersect_rectangles (r1, r2, result)
5888 XRectangle *r1, *r2, *result;
5889 {
5890 XRectangle *left, *right;
5891 XRectangle *upper, *lower;
5892 int intersection_p = 0;
5893
5894 /* Rearrange so that R1 is the left-most rectangle. */
5895 if (r1->x < r2->x)
5896 left = r1, right = r2;
5897 else
5898 left = r2, right = r1;
5899
5900 /* X0 of the intersection is right.x0, if this is inside R1,
5901 otherwise there is no intersection. */
5902 if (right->x <= left->x + left->width)
5903 {
5904 result->x = right->x;
5905
5906 /* The right end of the intersection is the minimum of the
5907 the right ends of left and right. */
5908 result->width = (min (left->x + left->width, right->x + right->width)
5909 - result->x);
5910
5911 /* Same game for Y. */
5912 if (r1->y < r2->y)
5913 upper = r1, lower = r2;
5914 else
5915 upper = r2, lower = r1;
5916
5917 /* The upper end of the intersection is lower.y0, if this is inside
5918 of upper. Otherwise, there is no intersection. */
5919 if (lower->y <= upper->y + upper->height)
5920 {
5921 result->y = lower->y;
5922
5923 /* The lower end of the intersection is the minimum of the lower
5924 ends of upper and lower. */
5925 result->height = (min (lower->y + lower->height,
5926 upper->y + upper->height)
5927 - result->y);
5928 intersection_p = 1;
5929 }
5930 }
5931
5932 return intersection_p;
5933 }
5934
5935
5936
5937
5938 \f
5939 static void
5940 frame_highlight (f)
5941 struct frame *f;
5942 {
5943 /* We used to only do this if Vx_no_window_manager was non-nil, but
5944 the ICCCM (section 4.1.6) says that the window's border pixmap
5945 and border pixel are window attributes which are "private to the
5946 client", so we can always change it to whatever we want. */
5947 BLOCK_INPUT;
5948 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5949 f->output_data.x->border_pixel);
5950 UNBLOCK_INPUT;
5951 x_update_cursor (f, 1);
5952 }
5953
5954 static void
5955 frame_unhighlight (f)
5956 struct frame *f;
5957 {
5958 /* We used to only do this if Vx_no_window_manager was non-nil, but
5959 the ICCCM (section 4.1.6) says that the window's border pixmap
5960 and border pixel are window attributes which are "private to the
5961 client", so we can always change it to whatever we want. */
5962 BLOCK_INPUT;
5963 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5964 f->output_data.x->border_tile);
5965 UNBLOCK_INPUT;
5966 x_update_cursor (f, 1);
5967 }
5968
5969 /* The focus has changed. Update the frames as necessary to reflect
5970 the new situation. Note that we can't change the selected frame
5971 here, because the Lisp code we are interrupting might become confused.
5972 Each event gets marked with the frame in which it occurred, so the
5973 Lisp code can tell when the switch took place by examining the events. */
5974
5975 static void
5976 x_new_focus_frame (dpyinfo, frame)
5977 struct x_display_info *dpyinfo;
5978 struct frame *frame;
5979 {
5980 struct frame *old_focus = dpyinfo->x_focus_frame;
5981
5982 if (frame != dpyinfo->x_focus_frame)
5983 {
5984 /* Set this before calling other routines, so that they see
5985 the correct value of x_focus_frame. */
5986 dpyinfo->x_focus_frame = frame;
5987
5988 if (old_focus && old_focus->auto_lower)
5989 x_lower_frame (old_focus);
5990
5991 #if 0
5992 selected_frame = frame;
5993 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5994 selected_frame);
5995 Fselect_window (selected_frame->selected_window);
5996 choose_minibuf_frame ();
5997 #endif /* ! 0 */
5998
5999 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6000 pending_autoraise_frame = dpyinfo->x_focus_frame;
6001 else
6002 pending_autoraise_frame = 0;
6003 }
6004
6005 x_frame_rehighlight (dpyinfo);
6006 }
6007
6008 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6009
6010 void
6011 x_mouse_leave (dpyinfo)
6012 struct x_display_info *dpyinfo;
6013 {
6014 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6015 }
6016
6017 /* The focus has changed, or we have redirected a frame's focus to
6018 another frame (this happens when a frame uses a surrogate
6019 mini-buffer frame). Shift the highlight as appropriate.
6020
6021 The FRAME argument doesn't necessarily have anything to do with which
6022 frame is being highlighted or un-highlighted; we only use it to find
6023 the appropriate X display info. */
6024
6025 static void
6026 XTframe_rehighlight (frame)
6027 struct frame *frame;
6028 {
6029 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6030 }
6031
6032 static void
6033 x_frame_rehighlight (dpyinfo)
6034 struct x_display_info *dpyinfo;
6035 {
6036 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6037
6038 if (dpyinfo->x_focus_frame)
6039 {
6040 dpyinfo->x_highlight_frame
6041 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6042 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6043 : dpyinfo->x_focus_frame);
6044 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6045 {
6046 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6047 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6048 }
6049 }
6050 else
6051 dpyinfo->x_highlight_frame = 0;
6052
6053 if (dpyinfo->x_highlight_frame != old_highlight)
6054 {
6055 if (old_highlight)
6056 frame_unhighlight (old_highlight);
6057 if (dpyinfo->x_highlight_frame)
6058 frame_highlight (dpyinfo->x_highlight_frame);
6059 }
6060 }
6061
6062
6063 \f
6064 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6065
6066 /* Initialize mode_switch_bit and modifier_meaning. */
6067 static void
6068 x_find_modifier_meanings (dpyinfo)
6069 struct x_display_info *dpyinfo;
6070 {
6071 int min_code, max_code;
6072 KeySym *syms;
6073 int syms_per_code;
6074 XModifierKeymap *mods;
6075
6076 dpyinfo->meta_mod_mask = 0;
6077 dpyinfo->shift_lock_mask = 0;
6078 dpyinfo->alt_mod_mask = 0;
6079 dpyinfo->super_mod_mask = 0;
6080 dpyinfo->hyper_mod_mask = 0;
6081
6082 #ifdef HAVE_X11R4
6083 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6084 #else
6085 min_code = dpyinfo->display->min_keycode;
6086 max_code = dpyinfo->display->max_keycode;
6087 #endif
6088
6089 syms = XGetKeyboardMapping (dpyinfo->display,
6090 min_code, max_code - min_code + 1,
6091 &syms_per_code);
6092 mods = XGetModifierMapping (dpyinfo->display);
6093
6094 /* Scan the modifier table to see which modifier bits the Meta and
6095 Alt keysyms are on. */
6096 {
6097 int row, col; /* The row and column in the modifier table. */
6098
6099 for (row = 3; row < 8; row++)
6100 for (col = 0; col < mods->max_keypermod; col++)
6101 {
6102 KeyCode code
6103 = mods->modifiermap[(row * mods->max_keypermod) + col];
6104
6105 /* Zeroes are used for filler. Skip them. */
6106 if (code == 0)
6107 continue;
6108
6109 /* Are any of this keycode's keysyms a meta key? */
6110 {
6111 int code_col;
6112
6113 for (code_col = 0; code_col < syms_per_code; code_col++)
6114 {
6115 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6116
6117 switch (sym)
6118 {
6119 case XK_Meta_L:
6120 case XK_Meta_R:
6121 dpyinfo->meta_mod_mask |= (1 << row);
6122 break;
6123
6124 case XK_Alt_L:
6125 case XK_Alt_R:
6126 dpyinfo->alt_mod_mask |= (1 << row);
6127 break;
6128
6129 case XK_Hyper_L:
6130 case XK_Hyper_R:
6131 dpyinfo->hyper_mod_mask |= (1 << row);
6132 break;
6133
6134 case XK_Super_L:
6135 case XK_Super_R:
6136 dpyinfo->super_mod_mask |= (1 << row);
6137 break;
6138
6139 case XK_Shift_Lock:
6140 /* Ignore this if it's not on the lock modifier. */
6141 if ((1 << row) == LockMask)
6142 dpyinfo->shift_lock_mask = LockMask;
6143 break;
6144 }
6145 }
6146 }
6147 }
6148 }
6149
6150 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6151 if (! dpyinfo->meta_mod_mask)
6152 {
6153 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6154 dpyinfo->alt_mod_mask = 0;
6155 }
6156
6157 /* If some keys are both alt and meta,
6158 make them just meta, not alt. */
6159 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6160 {
6161 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6162 }
6163
6164 XFree ((char *) syms);
6165 XFreeModifiermap (mods);
6166 }
6167
6168 /* Convert between the modifier bits X uses and the modifier bits
6169 Emacs uses. */
6170
6171 static unsigned int
6172 x_x_to_emacs_modifiers (dpyinfo, state)
6173 struct x_display_info *dpyinfo;
6174 unsigned int state;
6175 {
6176 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6177 | ((state & ControlMask) ? ctrl_modifier : 0)
6178 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6179 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6180 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6181 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6182 }
6183
6184 static unsigned int
6185 x_emacs_to_x_modifiers (dpyinfo, state)
6186 struct x_display_info *dpyinfo;
6187 unsigned int state;
6188 {
6189 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6190 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6191 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6192 | ((state & shift_modifier) ? ShiftMask : 0)
6193 | ((state & ctrl_modifier) ? ControlMask : 0)
6194 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6195 }
6196
6197 /* Convert a keysym to its name. */
6198
6199 char *
6200 x_get_keysym_name (keysym)
6201 KeySym keysym;
6202 {
6203 char *value;
6204
6205 BLOCK_INPUT;
6206 value = XKeysymToString (keysym);
6207 UNBLOCK_INPUT;
6208
6209 return value;
6210 }
6211
6212
6213 \f
6214 /* Mouse clicks and mouse movement. Rah. */
6215
6216 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6217 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6218 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6219 not force the value into range. */
6220
6221 void
6222 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6223 FRAME_PTR f;
6224 register int pix_x, pix_y;
6225 register int *x, *y;
6226 XRectangle *bounds;
6227 int noclip;
6228 {
6229 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6230 even for negative values. */
6231 if (pix_x < 0)
6232 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6233 if (pix_y < 0)
6234 pix_y -= (f)->output_data.x->line_height - 1;
6235
6236 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6237 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6238
6239 if (bounds)
6240 {
6241 bounds->width = FONT_WIDTH (f->output_data.x->font);
6242 bounds->height = f->output_data.x->line_height;
6243 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6244 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6245 }
6246
6247 if (!noclip)
6248 {
6249 if (pix_x < 0)
6250 pix_x = 0;
6251 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6252 pix_x = FRAME_WINDOW_WIDTH (f);
6253
6254 if (pix_y < 0)
6255 pix_y = 0;
6256 else if (pix_y > f->height)
6257 pix_y = f->height;
6258 }
6259
6260 *x = pix_x;
6261 *y = pix_y;
6262 }
6263
6264
6265 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6266 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6267 can't tell the positions because W's display is not up to date,
6268 return 0. */
6269
6270 int
6271 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6272 struct window *w;
6273 int hpos, vpos;
6274 int *frame_x, *frame_y;
6275 {
6276 int success_p;
6277
6278 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6279 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6280
6281 if (display_completed)
6282 {
6283 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6284 struct glyph *glyph = row->glyphs[TEXT_AREA];
6285 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6286
6287 *frame_y = row->y;
6288 *frame_x = row->x;
6289 while (glyph < end)
6290 {
6291 *frame_x += glyph->pixel_width;
6292 ++glyph;
6293 }
6294
6295 success_p = 1;
6296 }
6297 else
6298 {
6299 *frame_y = *frame_x = 0;
6300 success_p = 0;
6301 }
6302
6303 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6304 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6305 return success_p;
6306 }
6307
6308
6309 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6310
6311 If the event is a button press, then note that we have grabbed
6312 the mouse. */
6313
6314 static Lisp_Object
6315 construct_mouse_click (result, event, f)
6316 struct input_event *result;
6317 XButtonEvent *event;
6318 struct frame *f;
6319 {
6320 /* Make the event type no_event; we'll change that when we decide
6321 otherwise. */
6322 result->kind = mouse_click;
6323 result->code = event->button - Button1;
6324 result->timestamp = event->time;
6325 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6326 event->state)
6327 | (event->type == ButtonRelease
6328 ? up_modifier
6329 : down_modifier));
6330
6331 XSETINT (result->x, event->x);
6332 XSETINT (result->y, event->y);
6333 XSETFRAME (result->frame_or_window, f);
6334 result->arg = Qnil;
6335 return Qnil;
6336 }
6337
6338 \f
6339 /* Function to report a mouse movement to the mainstream Emacs code.
6340 The input handler calls this.
6341
6342 We have received a mouse movement event, which is given in *event.
6343 If the mouse is over a different glyph than it was last time, tell
6344 the mainstream emacs code by setting mouse_moved. If not, ask for
6345 another motion event, so we can check again the next time it moves. */
6346
6347 static XMotionEvent last_mouse_motion_event;
6348 static Lisp_Object last_mouse_motion_frame;
6349
6350 static void
6351 note_mouse_movement (frame, event)
6352 FRAME_PTR frame;
6353 XMotionEvent *event;
6354 {
6355 last_mouse_movement_time = event->time;
6356 last_mouse_motion_event = *event;
6357 XSETFRAME (last_mouse_motion_frame, frame);
6358
6359 if (event->window != FRAME_X_WINDOW (frame))
6360 {
6361 frame->mouse_moved = 1;
6362 last_mouse_scroll_bar = Qnil;
6363 note_mouse_highlight (frame, -1, -1);
6364 }
6365
6366 /* Has the mouse moved off the glyph it was on at the last sighting? */
6367 else if (event->x < last_mouse_glyph.x
6368 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6369 || event->y < last_mouse_glyph.y
6370 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6371 {
6372 frame->mouse_moved = 1;
6373 last_mouse_scroll_bar = Qnil;
6374 note_mouse_highlight (frame, event->x, event->y);
6375 }
6376 }
6377
6378 /* This is used for debugging, to turn off note_mouse_highlight. */
6379
6380 int disable_mouse_highlight;
6381
6382
6383 \f
6384 /************************************************************************
6385 Mouse Face
6386 ************************************************************************/
6387
6388 /* Find the glyph under window-relative coordinates X/Y in window W.
6389 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6390 strings. Return in *HPOS and *VPOS the row and column number of
6391 the glyph found. Return in *AREA the glyph area containing X.
6392 Value is a pointer to the glyph found or null if X/Y is not on
6393 text, or we can't tell because W's current matrix is not up to
6394 date. */
6395
6396 static struct glyph *
6397 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6398 struct window *w;
6399 int x, y;
6400 int *hpos, *vpos, *area;
6401 {
6402 struct glyph *glyph, *end;
6403 struct glyph_row *row = NULL;
6404 int x0, i, left_area_width;
6405
6406 /* Find row containing Y. Give up if some row is not enabled. */
6407 for (i = 0; i < w->current_matrix->nrows; ++i)
6408 {
6409 row = MATRIX_ROW (w->current_matrix, i);
6410 if (!row->enabled_p)
6411 return NULL;
6412 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6413 break;
6414 }
6415
6416 *vpos = i;
6417 *hpos = 0;
6418
6419 /* Give up if Y is not in the window. */
6420 if (i == w->current_matrix->nrows)
6421 return NULL;
6422
6423 /* Get the glyph area containing X. */
6424 if (w->pseudo_window_p)
6425 {
6426 *area = TEXT_AREA;
6427 x0 = 0;
6428 }
6429 else
6430 {
6431 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6432 if (x < left_area_width)
6433 {
6434 *area = LEFT_MARGIN_AREA;
6435 x0 = 0;
6436 }
6437 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6438 {
6439 *area = TEXT_AREA;
6440 x0 = row->x + left_area_width;
6441 }
6442 else
6443 {
6444 *area = RIGHT_MARGIN_AREA;
6445 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6446 }
6447 }
6448
6449 /* Find glyph containing X. */
6450 glyph = row->glyphs[*area];
6451 end = glyph + row->used[*area];
6452 while (glyph < end)
6453 {
6454 if (x < x0 + glyph->pixel_width)
6455 {
6456 if (w->pseudo_window_p)
6457 break;
6458 else if (BUFFERP (glyph->object))
6459 break;
6460 }
6461
6462 x0 += glyph->pixel_width;
6463 ++glyph;
6464 }
6465
6466 if (glyph == end)
6467 return NULL;
6468
6469 *hpos = glyph - row->glyphs[*area];
6470 return glyph;
6471 }
6472
6473
6474 /* Convert frame-relative x/y to coordinates relative to window W.
6475 Takes pseudo-windows into account. */
6476
6477 static void
6478 frame_to_window_pixel_xy (w, x, y)
6479 struct window *w;
6480 int *x, *y;
6481 {
6482 if (w->pseudo_window_p)
6483 {
6484 /* A pseudo-window is always full-width, and starts at the
6485 left edge of the frame, plus a frame border. */
6486 struct frame *f = XFRAME (w->frame);
6487 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6488 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6489 }
6490 else
6491 {
6492 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6493 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6494 }
6495 }
6496
6497
6498 /* Take proper action when mouse has moved to the mode or header line of
6499 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6500 mode line. X is relative to the start of the text display area of
6501 W, so the width of bitmap areas and scroll bars must be subtracted
6502 to get a position relative to the start of the mode line. */
6503
6504 static void
6505 note_mode_line_highlight (w, x, mode_line_p)
6506 struct window *w;
6507 int x, mode_line_p;
6508 {
6509 struct frame *f = XFRAME (w->frame);
6510 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6511 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6512 struct glyph_row *row;
6513
6514 if (mode_line_p)
6515 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6516 else
6517 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6518
6519 if (row->enabled_p)
6520 {
6521 struct glyph *glyph, *end;
6522 Lisp_Object help, map;
6523 int x0;
6524
6525 /* Find the glyph under X. */
6526 glyph = row->glyphs[TEXT_AREA];
6527 end = glyph + row->used[TEXT_AREA];
6528 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6529 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6530
6531 while (glyph < end
6532 && x >= x0 + glyph->pixel_width)
6533 {
6534 x0 += glyph->pixel_width;
6535 ++glyph;
6536 }
6537
6538 if (glyph < end
6539 && STRINGP (glyph->object)
6540 && XSTRING (glyph->object)->intervals
6541 && glyph->charpos >= 0
6542 && glyph->charpos < XSTRING (glyph->object)->size)
6543 {
6544 /* If we're on a string with `help-echo' text property,
6545 arrange for the help to be displayed. This is done by
6546 setting the global variable help_echo to the help string. */
6547 help = Fget_text_property (make_number (glyph->charpos),
6548 Qhelp_echo, glyph->object);
6549 if (!NILP (help))
6550 {
6551 help_echo = help;
6552 XSETWINDOW (help_echo_window, w);
6553 help_echo_object = glyph->object;
6554 help_echo_pos = glyph->charpos;
6555 }
6556
6557 /* Change the mouse pointer according to what is under X/Y. */
6558 map = Fget_text_property (make_number (glyph->charpos),
6559 Qlocal_map, glyph->object);
6560 if (KEYMAPP (map))
6561 cursor = f->output_data.x->nontext_cursor;
6562 else
6563 {
6564 map = Fget_text_property (make_number (glyph->charpos),
6565 Qkeymap, glyph->object);
6566 if (KEYMAPP (map))
6567 cursor = f->output_data.x->nontext_cursor;
6568 }
6569 }
6570 }
6571
6572 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6573 }
6574
6575
6576 /* Take proper action when the mouse has moved to position X, Y on
6577 frame F as regards highlighting characters that have mouse-face
6578 properties. Also de-highlighting chars where the mouse was before.
6579 X and Y can be negative or out of range. */
6580
6581 static void
6582 note_mouse_highlight (f, x, y)
6583 struct frame *f;
6584 int x, y;
6585 {
6586 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6587 int portion;
6588 Lisp_Object window;
6589 struct window *w;
6590
6591 /* When a menu is active, don't highlight because this looks odd. */
6592 #ifdef USE_X_TOOLKIT
6593 if (popup_activated ())
6594 return;
6595 #endif
6596
6597 if (disable_mouse_highlight
6598 || !f->glyphs_initialized_p)
6599 return;
6600
6601 dpyinfo->mouse_face_mouse_x = x;
6602 dpyinfo->mouse_face_mouse_y = y;
6603 dpyinfo->mouse_face_mouse_frame = f;
6604
6605 if (dpyinfo->mouse_face_defer)
6606 return;
6607
6608 if (gc_in_progress)
6609 {
6610 dpyinfo->mouse_face_deferred_gc = 1;
6611 return;
6612 }
6613
6614 /* Which window is that in? */
6615 window = window_from_coordinates (f, x, y, &portion, 1);
6616
6617 /* If we were displaying active text in another window, clear that. */
6618 if (! EQ (window, dpyinfo->mouse_face_window))
6619 clear_mouse_face (dpyinfo);
6620
6621 /* Not on a window -> return. */
6622 if (!WINDOWP (window))
6623 return;
6624
6625 /* Convert to window-relative pixel coordinates. */
6626 w = XWINDOW (window);
6627 frame_to_window_pixel_xy (w, &x, &y);
6628
6629 /* Handle tool-bar window differently since it doesn't display a
6630 buffer. */
6631 if (EQ (window, f->tool_bar_window))
6632 {
6633 note_tool_bar_highlight (f, x, y);
6634 return;
6635 }
6636
6637 if (portion == 1 || portion == 3)
6638 {
6639 /* Mouse is on the mode or top line. */
6640 note_mode_line_highlight (w, x, portion == 1);
6641 return;
6642 }
6643 else if (portion == 2)
6644 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6645 f->output_data.x->horizontal_drag_cursor);
6646 else
6647 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6648 f->output_data.x->text_cursor);
6649
6650 /* Are we in a window whose display is up to date?
6651 And verify the buffer's text has not changed. */
6652 if (/* Within text portion of the window. */
6653 portion == 0
6654 && EQ (w->window_end_valid, w->buffer)
6655 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6656 && (XFASTINT (w->last_overlay_modified)
6657 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6658 {
6659 int hpos, vpos, pos, i, area;
6660 struct glyph *glyph;
6661
6662 /* Find the glyph under X/Y. */
6663 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6664
6665 /* Clear mouse face if X/Y not over text. */
6666 if (glyph == NULL
6667 || area != TEXT_AREA
6668 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6669 {
6670 clear_mouse_face (dpyinfo);
6671 return;
6672 }
6673
6674 pos = glyph->charpos;
6675 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6676
6677 /* Check for mouse-face and help-echo. */
6678 {
6679 Lisp_Object mouse_face, overlay, position;
6680 Lisp_Object *overlay_vec;
6681 int len, noverlays;
6682 struct buffer *obuf;
6683 int obegv, ozv;
6684
6685 /* If we get an out-of-range value, return now; avoid an error. */
6686 if (pos > BUF_Z (XBUFFER (w->buffer)))
6687 return;
6688
6689 /* Make the window's buffer temporarily current for
6690 overlays_at and compute_char_face. */
6691 obuf = current_buffer;
6692 current_buffer = XBUFFER (w->buffer);
6693 obegv = BEGV;
6694 ozv = ZV;
6695 BEGV = BEG;
6696 ZV = Z;
6697
6698 /* Is this char mouse-active or does it have help-echo? */
6699 XSETINT (position, pos);
6700
6701 /* Put all the overlays we want in a vector in overlay_vec.
6702 Store the length in len. If there are more than 10, make
6703 enough space for all, and try again. */
6704 len = 10;
6705 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6706 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6707 if (noverlays > len)
6708 {
6709 len = noverlays;
6710 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6711 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6712 }
6713
6714 /* Sort overlays into increasing priority order. */
6715 noverlays = sort_overlays (overlay_vec, noverlays, w);
6716
6717 /* Check mouse-face highlighting. */
6718 if (! (EQ (window, dpyinfo->mouse_face_window)
6719 && vpos >= dpyinfo->mouse_face_beg_row
6720 && vpos <= dpyinfo->mouse_face_end_row
6721 && (vpos > dpyinfo->mouse_face_beg_row
6722 || hpos >= dpyinfo->mouse_face_beg_col)
6723 && (vpos < dpyinfo->mouse_face_end_row
6724 || hpos < dpyinfo->mouse_face_end_col
6725 || dpyinfo->mouse_face_past_end)))
6726 {
6727 /* Clear the display of the old active region, if any. */
6728 clear_mouse_face (dpyinfo);
6729
6730 /* Find the highest priority overlay that has a mouse-face prop. */
6731 overlay = Qnil;
6732 for (i = noverlays - 1; i >= 0; --i)
6733 {
6734 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6735 if (!NILP (mouse_face))
6736 {
6737 overlay = overlay_vec[i];
6738 break;
6739 }
6740 }
6741
6742 /* If no overlay applies, get a text property. */
6743 if (NILP (overlay))
6744 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6745
6746 /* Handle the overlay case. */
6747 if (! NILP (overlay))
6748 {
6749 /* Find the range of text around this char that
6750 should be active. */
6751 Lisp_Object before, after;
6752 int ignore;
6753
6754 before = Foverlay_start (overlay);
6755 after = Foverlay_end (overlay);
6756 /* Record this as the current active region. */
6757 fast_find_position (w, XFASTINT (before),
6758 &dpyinfo->mouse_face_beg_col,
6759 &dpyinfo->mouse_face_beg_row,
6760 &dpyinfo->mouse_face_beg_x,
6761 &dpyinfo->mouse_face_beg_y);
6762 dpyinfo->mouse_face_past_end
6763 = !fast_find_position (w, XFASTINT (after),
6764 &dpyinfo->mouse_face_end_col,
6765 &dpyinfo->mouse_face_end_row,
6766 &dpyinfo->mouse_face_end_x,
6767 &dpyinfo->mouse_face_end_y);
6768 dpyinfo->mouse_face_window = window;
6769 dpyinfo->mouse_face_face_id
6770 = face_at_buffer_position (w, pos, 0, 0,
6771 &ignore, pos + 1, 1);
6772
6773 /* Display it as active. */
6774 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6775 }
6776 /* Handle the text property case. */
6777 else if (! NILP (mouse_face))
6778 {
6779 /* Find the range of text around this char that
6780 should be active. */
6781 Lisp_Object before, after, beginning, end;
6782 int ignore;
6783
6784 beginning = Fmarker_position (w->start);
6785 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6786 - XFASTINT (w->window_end_pos)));
6787 before
6788 = Fprevious_single_property_change (make_number (pos + 1),
6789 Qmouse_face,
6790 w->buffer, beginning);
6791 after
6792 = Fnext_single_property_change (position, Qmouse_face,
6793 w->buffer, end);
6794 /* Record this as the current active region. */
6795 fast_find_position (w, XFASTINT (before),
6796 &dpyinfo->mouse_face_beg_col,
6797 &dpyinfo->mouse_face_beg_row,
6798 &dpyinfo->mouse_face_beg_x,
6799 &dpyinfo->mouse_face_beg_y);
6800 dpyinfo->mouse_face_past_end
6801 = !fast_find_position (w, XFASTINT (after),
6802 &dpyinfo->mouse_face_end_col,
6803 &dpyinfo->mouse_face_end_row,
6804 &dpyinfo->mouse_face_end_x,
6805 &dpyinfo->mouse_face_end_y);
6806 dpyinfo->mouse_face_window = window;
6807 dpyinfo->mouse_face_face_id
6808 = face_at_buffer_position (w, pos, 0, 0,
6809 &ignore, pos + 1, 1);
6810
6811 /* Display it as active. */
6812 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6813 }
6814 }
6815
6816 /* Look for a `help-echo' property. */
6817 {
6818 Lisp_Object help, overlay;
6819
6820 /* Check overlays first. */
6821 help = overlay = Qnil;
6822 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6823 {
6824 overlay = overlay_vec[i];
6825 help = Foverlay_get (overlay, Qhelp_echo);
6826 }
6827
6828 if (!NILP (help))
6829 {
6830 help_echo = help;
6831 help_echo_window = window;
6832 help_echo_object = overlay;
6833 help_echo_pos = pos;
6834 }
6835 else
6836 {
6837 /* Try text properties. */
6838 if ((STRINGP (glyph->object)
6839 && glyph->charpos >= 0
6840 && glyph->charpos < XSTRING (glyph->object)->size)
6841 || (BUFFERP (glyph->object)
6842 && glyph->charpos >= BEGV
6843 && glyph->charpos < ZV))
6844 help = Fget_text_property (make_number (glyph->charpos),
6845 Qhelp_echo, glyph->object);
6846
6847 if (!NILP (help))
6848 {
6849 help_echo = help;
6850 help_echo_window = window;
6851 help_echo_object = glyph->object;
6852 help_echo_pos = glyph->charpos;
6853 }
6854 }
6855 }
6856
6857 BEGV = obegv;
6858 ZV = ozv;
6859 current_buffer = obuf;
6860 }
6861 }
6862 }
6863
6864 static void
6865 redo_mouse_highlight ()
6866 {
6867 if (!NILP (last_mouse_motion_frame)
6868 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6869 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6870 last_mouse_motion_event.x,
6871 last_mouse_motion_event.y);
6872 }
6873
6874
6875 \f
6876 /***********************************************************************
6877 Tool-bars
6878 ***********************************************************************/
6879
6880 static int x_tool_bar_item P_ ((struct frame *, int, int,
6881 struct glyph **, int *, int *, int *));
6882
6883 /* Tool-bar item index of the item on which a mouse button was pressed
6884 or -1. */
6885
6886 static int last_tool_bar_item;
6887
6888
6889 /* Get information about the tool-bar item at position X/Y on frame F.
6890 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6891 the current matrix of the tool-bar window of F, or NULL if not
6892 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6893 item in F->tool_bar_items. Value is
6894
6895 -1 if X/Y is not on a tool-bar item
6896 0 if X/Y is on the same item that was highlighted before.
6897 1 otherwise. */
6898
6899 static int
6900 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6901 struct frame *f;
6902 int x, y;
6903 struct glyph **glyph;
6904 int *hpos, *vpos, *prop_idx;
6905 {
6906 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6907 struct window *w = XWINDOW (f->tool_bar_window);
6908 int area;
6909
6910 /* Find the glyph under X/Y. */
6911 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6912 if (*glyph == NULL)
6913 return -1;
6914
6915 /* Get the start of this tool-bar item's properties in
6916 f->tool_bar_items. */
6917 if (!tool_bar_item_info (f, *glyph, prop_idx))
6918 return -1;
6919
6920 /* Is mouse on the highlighted item? */
6921 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6922 && *vpos >= dpyinfo->mouse_face_beg_row
6923 && *vpos <= dpyinfo->mouse_face_end_row
6924 && (*vpos > dpyinfo->mouse_face_beg_row
6925 || *hpos >= dpyinfo->mouse_face_beg_col)
6926 && (*vpos < dpyinfo->mouse_face_end_row
6927 || *hpos < dpyinfo->mouse_face_end_col
6928 || dpyinfo->mouse_face_past_end))
6929 return 0;
6930
6931 return 1;
6932 }
6933
6934
6935 /* Handle mouse button event on the tool-bar of frame F, at
6936 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6937 or ButtonRelase. */
6938
6939 static void
6940 x_handle_tool_bar_click (f, button_event)
6941 struct frame *f;
6942 XButtonEvent *button_event;
6943 {
6944 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6945 struct window *w = XWINDOW (f->tool_bar_window);
6946 int hpos, vpos, prop_idx;
6947 struct glyph *glyph;
6948 Lisp_Object enabled_p;
6949 int x = button_event->x;
6950 int y = button_event->y;
6951
6952 /* If not on the highlighted tool-bar item, return. */
6953 frame_to_window_pixel_xy (w, &x, &y);
6954 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6955 return;
6956
6957 /* If item is disabled, do nothing. */
6958 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6959 if (NILP (enabled_p))
6960 return;
6961
6962 if (button_event->type == ButtonPress)
6963 {
6964 /* Show item in pressed state. */
6965 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6966 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6967 last_tool_bar_item = prop_idx;
6968 }
6969 else
6970 {
6971 Lisp_Object key, frame;
6972 struct input_event event;
6973
6974 /* Show item in released state. */
6975 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6976 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6977
6978 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
6979
6980 XSETFRAME (frame, f);
6981 event.kind = TOOL_BAR_EVENT;
6982 event.frame_or_window = frame;
6983 event.arg = frame;
6984 kbd_buffer_store_event (&event);
6985
6986 event.kind = TOOL_BAR_EVENT;
6987 event.frame_or_window = frame;
6988 event.arg = key;
6989 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6990 button_event->state);
6991 kbd_buffer_store_event (&event);
6992 last_tool_bar_item = -1;
6993 }
6994 }
6995
6996
6997 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6998 tool-bar window-relative coordinates X/Y. Called from
6999 note_mouse_highlight. */
7000
7001 static void
7002 note_tool_bar_highlight (f, x, y)
7003 struct frame *f;
7004 int x, y;
7005 {
7006 Lisp_Object window = f->tool_bar_window;
7007 struct window *w = XWINDOW (window);
7008 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7009 int hpos, vpos;
7010 struct glyph *glyph;
7011 struct glyph_row *row;
7012 int i;
7013 Lisp_Object enabled_p;
7014 int prop_idx;
7015 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7016 int mouse_down_p, rc;
7017
7018 /* Function note_mouse_highlight is called with negative x(y
7019 values when mouse moves outside of the frame. */
7020 if (x <= 0 || y <= 0)
7021 {
7022 clear_mouse_face (dpyinfo);
7023 return;
7024 }
7025
7026 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7027 if (rc < 0)
7028 {
7029 /* Not on tool-bar item. */
7030 clear_mouse_face (dpyinfo);
7031 return;
7032 }
7033 else if (rc == 0)
7034 /* On same tool-bar item as before. */
7035 goto set_help_echo;
7036
7037 clear_mouse_face (dpyinfo);
7038
7039 /* Mouse is down, but on different tool-bar item? */
7040 mouse_down_p = (dpyinfo->grabbed
7041 && f == last_mouse_frame
7042 && FRAME_LIVE_P (f));
7043 if (mouse_down_p
7044 && last_tool_bar_item != prop_idx)
7045 return;
7046
7047 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7048 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7049
7050 /* If tool-bar item is not enabled, don't highlight it. */
7051 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7052 if (!NILP (enabled_p))
7053 {
7054 /* Compute the x-position of the glyph. In front and past the
7055 image is a space. We include this is the highlighted area. */
7056 row = MATRIX_ROW (w->current_matrix, vpos);
7057 for (i = x = 0; i < hpos; ++i)
7058 x += row->glyphs[TEXT_AREA][i].pixel_width;
7059
7060 /* Record this as the current active region. */
7061 dpyinfo->mouse_face_beg_col = hpos;
7062 dpyinfo->mouse_face_beg_row = vpos;
7063 dpyinfo->mouse_face_beg_x = x;
7064 dpyinfo->mouse_face_beg_y = row->y;
7065 dpyinfo->mouse_face_past_end = 0;
7066
7067 dpyinfo->mouse_face_end_col = hpos + 1;
7068 dpyinfo->mouse_face_end_row = vpos;
7069 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7070 dpyinfo->mouse_face_end_y = row->y;
7071 dpyinfo->mouse_face_window = window;
7072 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7073
7074 /* Display it as active. */
7075 show_mouse_face (dpyinfo, draw);
7076 dpyinfo->mouse_face_image_state = draw;
7077 }
7078
7079 set_help_echo:
7080
7081 /* Set help_echo to a help string.to display for this tool-bar item.
7082 XTread_socket does the rest. */
7083 help_echo_object = help_echo_window = Qnil;
7084 help_echo_pos = -1;
7085 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7086 if (NILP (help_echo))
7087 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7088 }
7089
7090
7091 \f
7092 /* Find the glyph matrix position of buffer position POS in window W.
7093 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7094 current glyphs must be up to date. If POS is above window start
7095 return (0, 0, 0, 0). If POS is after end of W, return end of
7096 last line in W. */
7097
7098 static int
7099 fast_find_position (w, pos, hpos, vpos, x, y)
7100 struct window *w;
7101 int pos;
7102 int *hpos, *vpos, *x, *y;
7103 {
7104 int i;
7105 int lastcol;
7106 int maybe_next_line_p = 0;
7107 int line_start_position;
7108 int yb = window_text_bottom_y (w);
7109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7110 struct glyph_row *best_row = row;
7111 int row_vpos = 0, best_row_vpos = 0;
7112 int current_x;
7113
7114 while (row->y < yb)
7115 {
7116 if (row->used[TEXT_AREA])
7117 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7118 else
7119 line_start_position = 0;
7120
7121 if (line_start_position > pos)
7122 break;
7123 /* If the position sought is the end of the buffer,
7124 don't include the blank lines at the bottom of the window. */
7125 else if (line_start_position == pos
7126 && pos == BUF_ZV (XBUFFER (w->buffer)))
7127 {
7128 maybe_next_line_p = 1;
7129 break;
7130 }
7131 else if (line_start_position > 0)
7132 {
7133 best_row = row;
7134 best_row_vpos = row_vpos;
7135 }
7136
7137 if (row->y + row->height >= yb)
7138 break;
7139
7140 ++row;
7141 ++row_vpos;
7142 }
7143
7144 /* Find the right column within BEST_ROW. */
7145 lastcol = 0;
7146 current_x = best_row->x;
7147 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7148 {
7149 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7150 int charpos;
7151
7152 charpos = glyph->charpos;
7153 if (charpos == pos)
7154 {
7155 *hpos = i;
7156 *vpos = best_row_vpos;
7157 *x = current_x;
7158 *y = best_row->y;
7159 return 1;
7160 }
7161 else if (charpos > pos)
7162 break;
7163 else if (charpos > 0)
7164 lastcol = i;
7165
7166 current_x += glyph->pixel_width;
7167 }
7168
7169 /* If we're looking for the end of the buffer,
7170 and we didn't find it in the line we scanned,
7171 use the start of the following line. */
7172 if (maybe_next_line_p)
7173 {
7174 ++best_row;
7175 ++best_row_vpos;
7176 lastcol = 0;
7177 current_x = best_row->x;
7178 }
7179
7180 *vpos = best_row_vpos;
7181 *hpos = lastcol + 1;
7182 *x = current_x;
7183 *y = best_row->y;
7184 return 0;
7185 }
7186
7187
7188 /* Display the active region described by mouse_face_*
7189 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7190
7191 static void
7192 show_mouse_face (dpyinfo, draw)
7193 struct x_display_info *dpyinfo;
7194 enum draw_glyphs_face draw;
7195 {
7196 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7197 struct frame *f = XFRAME (WINDOW_FRAME (w));
7198 int i;
7199 int cursor_off_p = 0;
7200 struct cursor_pos saved_cursor;
7201
7202 saved_cursor = output_cursor;
7203
7204 /* If window is in the process of being destroyed, don't bother
7205 to do anything. */
7206 if (w->current_matrix == NULL)
7207 goto set_x_cursor;
7208
7209 /* Recognize when we are called to operate on rows that don't exist
7210 anymore. This can happen when a window is split. */
7211 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7212 goto set_x_cursor;
7213
7214 set_output_cursor (&w->phys_cursor);
7215
7216 /* Note that mouse_face_beg_row etc. are window relative. */
7217 for (i = dpyinfo->mouse_face_beg_row;
7218 i <= dpyinfo->mouse_face_end_row;
7219 i++)
7220 {
7221 int start_hpos, end_hpos, start_x;
7222 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7223
7224 /* Don't do anything if row doesn't have valid contents. */
7225 if (!row->enabled_p)
7226 continue;
7227
7228 /* For all but the first row, the highlight starts at column 0. */
7229 if (i == dpyinfo->mouse_face_beg_row)
7230 {
7231 start_hpos = dpyinfo->mouse_face_beg_col;
7232 start_x = dpyinfo->mouse_face_beg_x;
7233 }
7234 else
7235 {
7236 start_hpos = 0;
7237 start_x = 0;
7238 }
7239
7240 if (i == dpyinfo->mouse_face_end_row)
7241 end_hpos = dpyinfo->mouse_face_end_col;
7242 else
7243 end_hpos = row->used[TEXT_AREA];
7244
7245 /* If the cursor's in the text we are about to rewrite, turn the
7246 cursor off. */
7247 if (!w->pseudo_window_p
7248 && i == output_cursor.vpos
7249 && output_cursor.hpos >= start_hpos - 1
7250 && output_cursor.hpos <= end_hpos)
7251 {
7252 x_update_window_cursor (w, 0);
7253 cursor_off_p = 1;
7254 }
7255
7256 if (end_hpos > start_hpos)
7257 {
7258 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7259 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7260 start_hpos, end_hpos, draw, NULL, NULL, 0);
7261 }
7262 }
7263
7264 /* If we turned the cursor off, turn it back on. */
7265 if (cursor_off_p)
7266 x_display_cursor (w, 1,
7267 output_cursor.hpos, output_cursor.vpos,
7268 output_cursor.x, output_cursor.y);
7269
7270 output_cursor = saved_cursor;
7271
7272 set_x_cursor:
7273
7274 /* Change the mouse cursor. */
7275 if (draw == DRAW_NORMAL_TEXT)
7276 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7277 f->output_data.x->text_cursor);
7278 else if (draw == DRAW_MOUSE_FACE)
7279 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7280 f->output_data.x->cross_cursor);
7281 else
7282 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7283 f->output_data.x->nontext_cursor);
7284 }
7285
7286 /* Clear out the mouse-highlighted active region.
7287 Redraw it un-highlighted first. */
7288
7289 void
7290 clear_mouse_face (dpyinfo)
7291 struct x_display_info *dpyinfo;
7292 {
7293 if (!NILP (tip_frame))
7294 return;
7295
7296 if (! NILP (dpyinfo->mouse_face_window))
7297 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7298
7299 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7300 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7301 dpyinfo->mouse_face_window = Qnil;
7302 }
7303
7304
7305 /* Clear any mouse-face on window W. This function is part of the
7306 redisplay interface, and is called from try_window_id and similar
7307 functions to ensure the mouse-highlight is off. */
7308
7309 static void
7310 x_clear_mouse_face (w)
7311 struct window *w;
7312 {
7313 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7314 Lisp_Object window;
7315
7316 BLOCK_INPUT;
7317 XSETWINDOW (window, w);
7318 if (EQ (window, dpyinfo->mouse_face_window))
7319 clear_mouse_face (dpyinfo);
7320 UNBLOCK_INPUT;
7321 }
7322
7323
7324 /* Just discard the mouse face information for frame F, if any.
7325 This is used when the size of F is changed. */
7326
7327 void
7328 cancel_mouse_face (f)
7329 FRAME_PTR f;
7330 {
7331 Lisp_Object window;
7332 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7333
7334 window = dpyinfo->mouse_face_window;
7335 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7336 {
7337 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7338 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7339 dpyinfo->mouse_face_window = Qnil;
7340 }
7341 }
7342 \f
7343 static struct scroll_bar *x_window_to_scroll_bar ();
7344 static void x_scroll_bar_report_motion ();
7345
7346 /* Return the current position of the mouse.
7347 *fp should be a frame which indicates which display to ask about.
7348
7349 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7350 and *part to the frame, window, and scroll bar part that the mouse
7351 is over. Set *x and *y to the portion and whole of the mouse's
7352 position on the scroll bar.
7353
7354 If the mouse movement started elsewhere, set *fp to the frame the
7355 mouse is on, *bar_window to nil, and *x and *y to the character cell
7356 the mouse is over.
7357
7358 Set *time to the server time-stamp for the time at which the mouse
7359 was at this position.
7360
7361 Don't store anything if we don't have a valid set of values to report.
7362
7363 This clears the mouse_moved flag, so we can wait for the next mouse
7364 movement. */
7365
7366 static void
7367 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7368 FRAME_PTR *fp;
7369 int insist;
7370 Lisp_Object *bar_window;
7371 enum scroll_bar_part *part;
7372 Lisp_Object *x, *y;
7373 unsigned long *time;
7374 {
7375 FRAME_PTR f1;
7376
7377 BLOCK_INPUT;
7378
7379 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7380 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7381 else
7382 {
7383 Window root;
7384 int root_x, root_y;
7385
7386 Window dummy_window;
7387 int dummy;
7388
7389 Lisp_Object frame, tail;
7390
7391 /* Clear the mouse-moved flag for every frame on this display. */
7392 FOR_EACH_FRAME (tail, frame)
7393 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7394 XFRAME (frame)->mouse_moved = 0;
7395
7396 last_mouse_scroll_bar = Qnil;
7397
7398 /* Figure out which root window we're on. */
7399 XQueryPointer (FRAME_X_DISPLAY (*fp),
7400 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7401
7402 /* The root window which contains the pointer. */
7403 &root,
7404
7405 /* Trash which we can't trust if the pointer is on
7406 a different screen. */
7407 &dummy_window,
7408
7409 /* The position on that root window. */
7410 &root_x, &root_y,
7411
7412 /* More trash we can't trust. */
7413 &dummy, &dummy,
7414
7415 /* Modifier keys and pointer buttons, about which
7416 we don't care. */
7417 (unsigned int *) &dummy);
7418
7419 /* Now we have a position on the root; find the innermost window
7420 containing the pointer. */
7421 {
7422 Window win, child;
7423 int win_x, win_y;
7424 int parent_x = 0, parent_y = 0;
7425 int count;
7426
7427 win = root;
7428
7429 /* XTranslateCoordinates can get errors if the window
7430 structure is changing at the same time this function
7431 is running. So at least we must not crash from them. */
7432
7433 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7434
7435 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7436 && FRAME_LIVE_P (last_mouse_frame))
7437 {
7438 /* If mouse was grabbed on a frame, give coords for that frame
7439 even if the mouse is now outside it. */
7440 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7441
7442 /* From-window, to-window. */
7443 root, FRAME_X_WINDOW (last_mouse_frame),
7444
7445 /* From-position, to-position. */
7446 root_x, root_y, &win_x, &win_y,
7447
7448 /* Child of win. */
7449 &child);
7450 f1 = last_mouse_frame;
7451 }
7452 else
7453 {
7454 while (1)
7455 {
7456 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7457
7458 /* From-window, to-window. */
7459 root, win,
7460
7461 /* From-position, to-position. */
7462 root_x, root_y, &win_x, &win_y,
7463
7464 /* Child of win. */
7465 &child);
7466
7467 if (child == None || child == win)
7468 break;
7469
7470 win = child;
7471 parent_x = win_x;
7472 parent_y = win_y;
7473 }
7474
7475 /* Now we know that:
7476 win is the innermost window containing the pointer
7477 (XTC says it has no child containing the pointer),
7478 win_x and win_y are the pointer's position in it
7479 (XTC did this the last time through), and
7480 parent_x and parent_y are the pointer's position in win's parent.
7481 (They are what win_x and win_y were when win was child.
7482 If win is the root window, it has no parent, and
7483 parent_{x,y} are invalid, but that's okay, because we'll
7484 never use them in that case.) */
7485
7486 /* Is win one of our frames? */
7487 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7488
7489 #ifdef USE_X_TOOLKIT
7490 /* If we end up with the menu bar window, say it's not
7491 on the frame. */
7492 if (f1 != NULL
7493 && f1->output_data.x->menubar_widget
7494 && win == XtWindow (f1->output_data.x->menubar_widget))
7495 f1 = NULL;
7496 #endif /* USE_X_TOOLKIT */
7497 }
7498
7499 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7500 f1 = 0;
7501
7502 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7503
7504 /* If not, is it one of our scroll bars? */
7505 if (! f1)
7506 {
7507 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7508
7509 if (bar)
7510 {
7511 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7512 win_x = parent_x;
7513 win_y = parent_y;
7514 }
7515 }
7516
7517 if (f1 == 0 && insist > 0)
7518 f1 = SELECTED_FRAME ();
7519
7520 if (f1)
7521 {
7522 /* Ok, we found a frame. Store all the values.
7523 last_mouse_glyph is a rectangle used to reduce the
7524 generation of mouse events. To not miss any motion
7525 events, we must divide the frame into rectangles of the
7526 size of the smallest character that could be displayed
7527 on it, i.e. into the same rectangles that matrices on
7528 the frame are divided into. */
7529
7530 #if OLD_REDISPLAY_CODE
7531 int ignore1, ignore2;
7532 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7533 &last_mouse_glyph,
7534 FRAME_X_DISPLAY_INFO (f1)->grabbed
7535 || insist);
7536 #else
7537 {
7538 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7539 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7540 int x = win_x;
7541 int y = win_y;
7542
7543 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7544 round down even for negative values. */
7545 if (x < 0)
7546 x -= width - 1;
7547 if (y < 0)
7548 y -= height - 1;
7549
7550 last_mouse_glyph.width = width;
7551 last_mouse_glyph.height = height;
7552 last_mouse_glyph.x = (x + width - 1) / width * width;
7553 last_mouse_glyph.y = (y + height - 1) / height * height;
7554 }
7555 #endif
7556
7557 *bar_window = Qnil;
7558 *part = 0;
7559 *fp = f1;
7560 XSETINT (*x, win_x);
7561 XSETINT (*y, win_y);
7562 *time = last_mouse_movement_time;
7563 }
7564 }
7565 }
7566
7567 UNBLOCK_INPUT;
7568 }
7569
7570
7571 #ifdef USE_X_TOOLKIT
7572
7573 /* Atimer callback function for TIMER. Called every 0.1s to process
7574 Xt timeouts, if needed. We must avoid calling XtAppPending as
7575 much as possible because that function does an implicit XFlush
7576 that slows us down. */
7577
7578 static void
7579 x_process_timeouts (timer)
7580 struct atimer *timer;
7581 {
7582 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7583 {
7584 BLOCK_INPUT;
7585 while (XtAppPending (Xt_app_con) & XtIMTimer)
7586 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7587 UNBLOCK_INPUT;
7588 }
7589 }
7590
7591 #endif /* USE_X_TOOLKIT */
7592
7593 \f
7594 /* Scroll bar support. */
7595
7596 /* Given an X window ID, find the struct scroll_bar which manages it.
7597 This can be called in GC, so we have to make sure to strip off mark
7598 bits. */
7599
7600 static struct scroll_bar *
7601 x_window_to_scroll_bar (window_id)
7602 Window window_id;
7603 {
7604 Lisp_Object tail;
7605
7606 for (tail = Vframe_list;
7607 XGCTYPE (tail) == Lisp_Cons;
7608 tail = XCDR (tail))
7609 {
7610 Lisp_Object frame, bar, condemned;
7611
7612 frame = XCAR (tail);
7613 /* All elements of Vframe_list should be frames. */
7614 if (! GC_FRAMEP (frame))
7615 abort ();
7616
7617 /* Scan this frame's scroll bar list for a scroll bar with the
7618 right window ID. */
7619 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7620 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7621 /* This trick allows us to search both the ordinary and
7622 condemned scroll bar lists with one loop. */
7623 ! GC_NILP (bar) || (bar = condemned,
7624 condemned = Qnil,
7625 ! GC_NILP (bar));
7626 bar = XSCROLL_BAR (bar)->next)
7627 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7628 return XSCROLL_BAR (bar);
7629 }
7630
7631 return 0;
7632 }
7633
7634
7635 \f
7636 /************************************************************************
7637 Toolkit scroll bars
7638 ************************************************************************/
7639
7640 #ifdef USE_TOOLKIT_SCROLL_BARS
7641
7642 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7643 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7644 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7645 struct scroll_bar *));
7646 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7647 int, int, int));
7648
7649
7650 /* Id of action hook installed for scroll bars. */
7651
7652 static XtActionHookId action_hook_id;
7653
7654 /* Lisp window being scrolled. Set when starting to interact with
7655 a toolkit scroll bar, reset to nil when ending the interaction. */
7656
7657 static Lisp_Object window_being_scrolled;
7658
7659 /* Last scroll bar part sent in xm_scroll_callback. */
7660
7661 static int last_scroll_bar_part;
7662
7663 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7664 that movements of 1/20 of the screen size are mapped to up/down. */
7665
7666 static Boolean xaw3d_arrow_scroll;
7667
7668 /* Whether the drag scrolling maintains the mouse at the top of the
7669 thumb. If not, resizing the thumb needs to be done more carefully
7670 to avoid jerkyness. */
7671
7672 static Boolean xaw3d_pick_top;
7673
7674
7675 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7676 bars are used.. The hook is responsible for detecting when
7677 the user ends an interaction with the scroll bar, and generates
7678 a `end-scroll' scroll_bar_click' event if so. */
7679
7680 static void
7681 xt_action_hook (widget, client_data, action_name, event, params,
7682 num_params)
7683 Widget widget;
7684 XtPointer client_data;
7685 String action_name;
7686 XEvent *event;
7687 String *params;
7688 Cardinal *num_params;
7689 {
7690 int scroll_bar_p;
7691 char *end_action;
7692
7693 #ifdef USE_MOTIF
7694 scroll_bar_p = XmIsScrollBar (widget);
7695 end_action = "Release";
7696 #else /* !USE_MOTIF i.e. use Xaw */
7697 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7698 end_action = "EndScroll";
7699 #endif /* USE_MOTIF */
7700
7701 if (scroll_bar_p
7702 && strcmp (action_name, end_action) == 0
7703 && WINDOWP (window_being_scrolled))
7704 {
7705 struct window *w;
7706
7707 x_send_scroll_bar_event (window_being_scrolled,
7708 scroll_bar_end_scroll, 0, 0);
7709 w = XWINDOW (window_being_scrolled);
7710 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7711 window_being_scrolled = Qnil;
7712 last_scroll_bar_part = -1;
7713
7714 /* Xt timeouts no longer needed. */
7715 toolkit_scroll_bar_interaction = 0;
7716 }
7717 }
7718
7719 /* A vector of windows used for communication between
7720 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7721
7722 static struct window **scroll_bar_windows;
7723 static int scroll_bar_windows_size;
7724
7725
7726 /* Send a client message with message type Xatom_Scrollbar for a
7727 scroll action to the frame of WINDOW. PART is a value identifying
7728 the part of the scroll bar that was clicked on. PORTION is the
7729 amount to scroll of a whole of WHOLE. */
7730
7731 static void
7732 x_send_scroll_bar_event (window, part, portion, whole)
7733 Lisp_Object window;
7734 int part, portion, whole;
7735 {
7736 XEvent event;
7737 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7738 struct window *w = XWINDOW (window);
7739 struct frame *f = XFRAME (w->frame);
7740 int i;
7741
7742 BLOCK_INPUT;
7743
7744 /* Construct a ClientMessage event to send to the frame. */
7745 ev->type = ClientMessage;
7746 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7747 ev->display = FRAME_X_DISPLAY (f);
7748 ev->window = FRAME_X_WINDOW (f);
7749 ev->format = 32;
7750
7751 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7752 not enough to store a pointer or Lisp_Object on a 64 bit system.
7753 So, store the window in scroll_bar_windows and pass the index
7754 into that array in the event. */
7755 for (i = 0; i < scroll_bar_windows_size; ++i)
7756 if (scroll_bar_windows[i] == NULL)
7757 break;
7758
7759 if (i == scroll_bar_windows_size)
7760 {
7761 int new_size = max (10, 2 * scroll_bar_windows_size);
7762 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7763 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7764
7765 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7766 nbytes);
7767 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7768 scroll_bar_windows_size = new_size;
7769 }
7770
7771 scroll_bar_windows[i] = w;
7772 ev->data.l[0] = (long) i;
7773 ev->data.l[1] = (long) part;
7774 ev->data.l[2] = (long) 0;
7775 ev->data.l[3] = (long) portion;
7776 ev->data.l[4] = (long) whole;
7777
7778 /* Make Xt timeouts work while the scroll bar is active. */
7779 toolkit_scroll_bar_interaction = 1;
7780
7781 /* Setting the event mask to zero means that the message will
7782 be sent to the client that created the window, and if that
7783 window no longer exists, no event will be sent. */
7784 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7785 UNBLOCK_INPUT;
7786 }
7787
7788
7789 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7790 in *IEVENT. */
7791
7792 static void
7793 x_scroll_bar_to_input_event (event, ievent)
7794 XEvent *event;
7795 struct input_event *ievent;
7796 {
7797 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7798 Lisp_Object window;
7799 struct frame *f;
7800 struct window *w;
7801
7802 w = scroll_bar_windows[ev->data.l[0]];
7803 scroll_bar_windows[ev->data.l[0]] = NULL;
7804
7805 XSETWINDOW (window, w);
7806 f = XFRAME (w->frame);
7807
7808 ievent->kind = scroll_bar_click;
7809 ievent->frame_or_window = window;
7810 ievent->arg = Qnil;
7811 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7812 ievent->part = ev->data.l[1];
7813 ievent->code = ev->data.l[2];
7814 ievent->x = make_number ((int) ev->data.l[3]);
7815 ievent->y = make_number ((int) ev->data.l[4]);
7816 ievent->modifiers = 0;
7817 }
7818
7819
7820 #ifdef USE_MOTIF
7821
7822 /* Minimum and maximum values used for Motif scroll bars. */
7823
7824 #define XM_SB_MIN 1
7825 #define XM_SB_MAX 10000000
7826 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7827
7828
7829 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7830 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7831 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7832
7833 static void
7834 xm_scroll_callback (widget, client_data, call_data)
7835 Widget widget;
7836 XtPointer client_data, call_data;
7837 {
7838 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7839 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7840 double percent;
7841 int part = -1, whole = 0, portion = 0;
7842
7843 switch (cs->reason)
7844 {
7845 case XmCR_DECREMENT:
7846 bar->dragging = Qnil;
7847 part = scroll_bar_up_arrow;
7848 break;
7849
7850 case XmCR_INCREMENT:
7851 bar->dragging = Qnil;
7852 part = scroll_bar_down_arrow;
7853 break;
7854
7855 case XmCR_PAGE_DECREMENT:
7856 bar->dragging = Qnil;
7857 part = scroll_bar_above_handle;
7858 break;
7859
7860 case XmCR_PAGE_INCREMENT:
7861 bar->dragging = Qnil;
7862 part = scroll_bar_below_handle;
7863 break;
7864
7865 case XmCR_TO_TOP:
7866 bar->dragging = Qnil;
7867 part = scroll_bar_to_top;
7868 break;
7869
7870 case XmCR_TO_BOTTOM:
7871 bar->dragging = Qnil;
7872 part = scroll_bar_to_bottom;
7873 break;
7874
7875 case XmCR_DRAG:
7876 {
7877 int slider_size;
7878 int dragging_down_p = (INTEGERP (bar->dragging)
7879 && XINT (bar->dragging) <= cs->value);
7880
7881 /* Get the slider size. */
7882 BLOCK_INPUT;
7883 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7884 UNBLOCK_INPUT;
7885
7886 /* At the max position of the scroll bar, do a line-wise
7887 movement. Without doing anything, the LessTif scroll bar
7888 calls us with the same cs->value again and again. If we
7889 want to make sure that we can reach the end of the buffer,
7890 we have to do something.
7891
7892 Implementation note: setting bar->dragging always to
7893 cs->value gives a smoother movement at the max position.
7894 Setting it to nil when doing line-wise movement gives
7895 a better slider behavior. */
7896
7897 if (cs->value + slider_size == XM_SB_MAX
7898 || (dragging_down_p
7899 && last_scroll_bar_part == scroll_bar_down_arrow))
7900 {
7901 part = scroll_bar_down_arrow;
7902 bar->dragging = Qnil;
7903 }
7904 else
7905 {
7906 whole = XM_SB_RANGE;
7907 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7908 part = scroll_bar_handle;
7909 bar->dragging = make_number (cs->value);
7910 }
7911 }
7912 break;
7913
7914 case XmCR_VALUE_CHANGED:
7915 break;
7916 };
7917
7918 if (part >= 0)
7919 {
7920 window_being_scrolled = bar->window;
7921 last_scroll_bar_part = part;
7922 x_send_scroll_bar_event (bar->window, part, portion, whole);
7923 }
7924 }
7925
7926
7927 #else /* !USE_MOTIF, i.e. Xaw. */
7928
7929
7930 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7931 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7932 scroll bar struct. CALL_DATA is a pointer to a float saying where
7933 the thumb is. */
7934
7935 static void
7936 xaw_jump_callback (widget, client_data, call_data)
7937 Widget widget;
7938 XtPointer client_data, call_data;
7939 {
7940 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7941 float top = *(float *) call_data;
7942 float shown;
7943 int whole, portion, height;
7944 int part;
7945
7946 /* Get the size of the thumb, a value between 0 and 1. */
7947 BLOCK_INPUT;
7948 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7949 UNBLOCK_INPUT;
7950
7951 whole = 10000000;
7952 portion = shown < 1 ? top * whole : 0;
7953
7954 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7955 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7956 the bottom, so we force the scrolling whenever we see that we're
7957 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7958 we try to ensure that we always stay two pixels away from the
7959 bottom). */
7960 part = scroll_bar_down_arrow;
7961 else
7962 part = scroll_bar_handle;
7963
7964 window_being_scrolled = bar->window;
7965 bar->dragging = make_number (portion);
7966 last_scroll_bar_part = part;
7967 x_send_scroll_bar_event (bar->window, part, portion, whole);
7968 }
7969
7970
7971 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7972 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7973 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7974 the scroll bar. CALL_DATA is an integer specifying the action that
7975 has taken place. It's magnitude is in the range 0..height of the
7976 scroll bar. Negative values mean scroll towards buffer start.
7977 Values < height of scroll bar mean line-wise movement. */
7978
7979 static void
7980 xaw_scroll_callback (widget, client_data, call_data)
7981 Widget widget;
7982 XtPointer client_data, call_data;
7983 {
7984 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7985 int position = (int) call_data;
7986 Dimension height;
7987 int part;
7988
7989 /* Get the height of the scroll bar. */
7990 BLOCK_INPUT;
7991 XtVaGetValues (widget, XtNheight, &height, NULL);
7992 UNBLOCK_INPUT;
7993
7994 if (abs (position) >= height)
7995 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7996
7997 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7998 it maps line-movement to call_data = max(5, height/20). */
7999 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8000 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8001 else
8002 part = scroll_bar_move_ratio;
8003
8004 window_being_scrolled = bar->window;
8005 bar->dragging = Qnil;
8006 last_scroll_bar_part = part;
8007 x_send_scroll_bar_event (bar->window, part, position, height);
8008 }
8009
8010
8011 #endif /* not USE_MOTIF */
8012
8013
8014 /* Create the widget for scroll bar BAR on frame F. Record the widget
8015 and X window of the scroll bar in BAR. */
8016
8017 static void
8018 x_create_toolkit_scroll_bar (f, bar)
8019 struct frame *f;
8020 struct scroll_bar *bar;
8021 {
8022 Window xwindow;
8023 Widget widget;
8024 Arg av[20];
8025 int ac = 0;
8026 char *scroll_bar_name = "verticalScrollBar";
8027 unsigned long pixel;
8028
8029 BLOCK_INPUT;
8030
8031 #ifdef USE_MOTIF
8032 /* LessTif 0.85, problems:
8033
8034 1. When the mouse if over the scroll bar, the scroll bar will
8035 get keyboard events. I didn't find a way to turn this off.
8036
8037 2. Do we have to explicitly set the cursor to get an arrow
8038 cursor (see below)? */
8039
8040 /* Set resources. Create the widget. */
8041 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8042 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8043 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8044 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8045 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8046 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8047 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8048
8049 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8050 if (pixel != -1)
8051 {
8052 XtSetArg (av[ac], XmNforeground, pixel);
8053 ++ac;
8054 }
8055
8056 pixel = f->output_data.x->scroll_bar_background_pixel;
8057 if (pixel != -1)
8058 {
8059 XtSetArg (av[ac], XmNbackground, pixel);
8060 ++ac;
8061 }
8062
8063 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8064 scroll_bar_name, av, ac);
8065
8066 /* Add one callback for everything that can happen. */
8067 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8068 (XtPointer) bar);
8069 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8070 (XtPointer) bar);
8071 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8072 (XtPointer) bar);
8073 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8074 (XtPointer) bar);
8075 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8076 (XtPointer) bar);
8077 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8078 (XtPointer) bar);
8079 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8080 (XtPointer) bar);
8081
8082 /* Realize the widget. Only after that is the X window created. */
8083 XtRealizeWidget (widget);
8084
8085 /* Set the cursor to an arrow. I didn't find a resource to do that.
8086 And I'm wondering why it hasn't an arrow cursor by default. */
8087 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8088 f->output_data.x->nontext_cursor);
8089
8090 #else /* !USE_MOTIF i.e. use Xaw */
8091
8092 /* Set resources. Create the widget. The background of the
8093 Xaw3d scroll bar widget is a little bit light for my taste.
8094 We don't alter it here to let users change it according
8095 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8096 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8097 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8098 /* For smoother scrolling with Xaw3d -sm */
8099 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8100 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8101
8102 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8103 if (pixel != -1)
8104 {
8105 XtSetArg (av[ac], XtNforeground, pixel);
8106 ++ac;
8107 }
8108
8109 pixel = f->output_data.x->scroll_bar_background_pixel;
8110 if (pixel != -1)
8111 {
8112 XtSetArg (av[ac], XtNbackground, pixel);
8113 ++ac;
8114 }
8115
8116 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8117 f->output_data.x->edit_widget, av, ac);
8118
8119 {
8120 char *initial = "";
8121 char *val = initial;
8122 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8123 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8124 if (val == initial)
8125 { /* ARROW_SCROLL */
8126 xaw3d_arrow_scroll = True;
8127 /* Isn't that just a personal preference ? -sm */
8128 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8129 }
8130 }
8131
8132 /* Define callbacks. */
8133 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8134 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8135 (XtPointer) bar);
8136
8137 /* Realize the widget. Only after that is the X window created. */
8138 XtRealizeWidget (widget);
8139
8140 #endif /* !USE_MOTIF */
8141
8142 /* Install an action hook that let's us detect when the user
8143 finishes interacting with a scroll bar. */
8144 if (action_hook_id == 0)
8145 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8146
8147 /* Remember X window and widget in the scroll bar vector. */
8148 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8149 xwindow = XtWindow (widget);
8150 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8151
8152 UNBLOCK_INPUT;
8153 }
8154
8155
8156 /* Set the thumb size and position of scroll bar BAR. We are currently
8157 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8158
8159 static void
8160 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8161 struct scroll_bar *bar;
8162 int portion, position, whole;
8163 {
8164 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8165 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8166 float top, shown;
8167
8168 if (whole == 0)
8169 top = 0, shown = 1;
8170 else
8171 {
8172 top = (float) position / whole;
8173 shown = (float) portion / whole;
8174 }
8175
8176 BLOCK_INPUT;
8177
8178 #ifdef USE_MOTIF
8179 {
8180 int size, value;
8181 Boolean arrow1_selected, arrow2_selected;
8182 unsigned char flags;
8183 XmScrollBarWidget sb;
8184
8185 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8186 is the scroll bar's maximum and MIN is the scroll bar's minimum
8187 value. */
8188 size = shown * XM_SB_RANGE;
8189 size = min (size, XM_SB_RANGE);
8190 size = max (size, 1);
8191
8192 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8193 value = top * XM_SB_RANGE;
8194 value = min (value, XM_SB_MAX - size);
8195 value = max (value, XM_SB_MIN);
8196
8197 /* LessTif: Calling XmScrollBarSetValues after an increment or
8198 decrement turns off auto-repeat LessTif-internally. This can
8199 be seen in ScrollBar.c which resets Arrow1Selected and
8200 Arrow2Selected. It also sets internal flags so that LessTif
8201 believes the mouse is in the slider. We either have to change
8202 our code, or work around that by accessing private data. */
8203
8204 sb = (XmScrollBarWidget) widget;
8205 arrow1_selected = sb->scrollBar.arrow1_selected;
8206 arrow2_selected = sb->scrollBar.arrow2_selected;
8207 flags = sb->scrollBar.flags;
8208
8209 if (NILP (bar->dragging))
8210 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8211 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8212 /* This has the negative side effect that the slider value is
8213 not what it would be if we scrolled here using line-wise or
8214 page-wise movement. */
8215 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8216 else
8217 {
8218 /* If currently dragging, only update the slider size.
8219 This reduces flicker effects. */
8220 int old_value, old_size, increment, page_increment;
8221
8222 XmScrollBarGetValues (widget, &old_value, &old_size,
8223 &increment, &page_increment);
8224 XmScrollBarSetValues (widget, old_value,
8225 min (size, XM_SB_RANGE - old_value),
8226 0, 0, False);
8227 }
8228
8229 sb->scrollBar.arrow1_selected = arrow1_selected;
8230 sb->scrollBar.arrow2_selected = arrow2_selected;
8231 sb->scrollBar.flags = flags;
8232 }
8233 #else /* !USE_MOTIF i.e. use Xaw */
8234 {
8235 float old_top, old_shown;
8236 Dimension height;
8237 XtVaGetValues (widget,
8238 XtNtopOfThumb, &old_top,
8239 XtNshown, &old_shown,
8240 XtNheight, &height,
8241 NULL);
8242
8243 /* Massage the top+shown values. */
8244 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8245 top = max (0, min (1, top));
8246 else
8247 top = old_top;
8248 /* Keep two pixels available for moving the thumb down. */
8249 shown = max (0, min (1 - top - (2.0 / height), shown));
8250
8251 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8252 check that your system's configuration file contains a define
8253 for `NARROWPROTO'. See s/freebsd.h for an example. */
8254 if (top != old_top || shown != old_shown)
8255 {
8256 if (NILP (bar->dragging))
8257 XawScrollbarSetThumb (widget, top, shown);
8258 else
8259 {
8260 #ifdef HAVE_XAW3D
8261 ScrollbarWidget sb = (ScrollbarWidget) widget;
8262 int scroll_mode = 0;
8263
8264 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8265 if (xaw3d_arrow_scroll)
8266 {
8267 /* Xaw3d stupidly ignores resize requests while dragging
8268 so we have to make it believe it's not in dragging mode. */
8269 scroll_mode = sb->scrollbar.scroll_mode;
8270 if (scroll_mode == 2)
8271 sb->scrollbar.scroll_mode = 0;
8272 }
8273 #endif
8274 /* Try to make the scrolling a tad smoother. */
8275 if (!xaw3d_pick_top)
8276 shown = min (shown, old_shown);
8277
8278 XawScrollbarSetThumb (widget, top, shown);
8279
8280 #ifdef HAVE_XAW3D
8281 if (xaw3d_arrow_scroll && scroll_mode == 2)
8282 sb->scrollbar.scroll_mode = scroll_mode;
8283 #endif
8284 }
8285 }
8286 }
8287 #endif /* !USE_MOTIF */
8288
8289 UNBLOCK_INPUT;
8290 }
8291
8292 #endif /* USE_TOOLKIT_SCROLL_BARS */
8293
8294
8295 \f
8296 /************************************************************************
8297 Scroll bars, general
8298 ************************************************************************/
8299
8300 /* Create a scroll bar and return the scroll bar vector for it. W is
8301 the Emacs window on which to create the scroll bar. TOP, LEFT,
8302 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8303 scroll bar. */
8304
8305 static struct scroll_bar *
8306 x_scroll_bar_create (w, top, left, width, height)
8307 struct window *w;
8308 int top, left, width, height;
8309 {
8310 struct frame *f = XFRAME (w->frame);
8311 struct scroll_bar *bar
8312 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8313
8314 BLOCK_INPUT;
8315
8316 #ifdef USE_TOOLKIT_SCROLL_BARS
8317 x_create_toolkit_scroll_bar (f, bar);
8318 #else /* not USE_TOOLKIT_SCROLL_BARS */
8319 {
8320 XSetWindowAttributes a;
8321 unsigned long mask;
8322 Window window;
8323
8324 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8325 if (a.background_pixel == -1)
8326 a.background_pixel = f->output_data.x->background_pixel;
8327
8328 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8329 | ButtonMotionMask | PointerMotionHintMask
8330 | ExposureMask);
8331 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8332
8333 mask = (CWBackPixel | CWEventMask | CWCursor);
8334
8335 /* Clear the area of W that will serve as a scroll bar. This is
8336 for the case that a window has been split horizontally. In
8337 this case, no clear_frame is generated to reduce flickering. */
8338 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8339 left, top, width,
8340 window_box_height (w), False);
8341
8342 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8343 /* Position and size of scroll bar. */
8344 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8345 top,
8346 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8347 height,
8348 /* Border width, depth, class, and visual. */
8349 0,
8350 CopyFromParent,
8351 CopyFromParent,
8352 CopyFromParent,
8353 /* Attributes. */
8354 mask, &a);
8355 SET_SCROLL_BAR_X_WINDOW (bar, window);
8356 }
8357 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8358
8359 XSETWINDOW (bar->window, w);
8360 XSETINT (bar->top, top);
8361 XSETINT (bar->left, left);
8362 XSETINT (bar->width, width);
8363 XSETINT (bar->height, height);
8364 XSETINT (bar->start, 0);
8365 XSETINT (bar->end, 0);
8366 bar->dragging = Qnil;
8367
8368 /* Add bar to its frame's list of scroll bars. */
8369 bar->next = FRAME_SCROLL_BARS (f);
8370 bar->prev = Qnil;
8371 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8372 if (!NILP (bar->next))
8373 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8374
8375 /* Map the window/widget. */
8376 #ifdef USE_TOOLKIT_SCROLL_BARS
8377 {
8378 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8379 XtConfigureWidget (scroll_bar,
8380 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8381 top,
8382 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8383 height, 0);
8384 XtMapWidget (scroll_bar);
8385 }
8386 #else /* not USE_TOOLKIT_SCROLL_BARS */
8387 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8388 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8389
8390 UNBLOCK_INPUT;
8391 return bar;
8392 }
8393
8394
8395 /* Draw BAR's handle in the proper position.
8396
8397 If the handle is already drawn from START to END, don't bother
8398 redrawing it, unless REBUILD is non-zero; in that case, always
8399 redraw it. (REBUILD is handy for drawing the handle after expose
8400 events.)
8401
8402 Normally, we want to constrain the start and end of the handle to
8403 fit inside its rectangle, but if the user is dragging the scroll
8404 bar handle, we want to let them drag it down all the way, so that
8405 the bar's top is as far down as it goes; otherwise, there's no way
8406 to move to the very end of the buffer. */
8407
8408 #ifndef USE_TOOLKIT_SCROLL_BARS
8409
8410 static void
8411 x_scroll_bar_set_handle (bar, start, end, rebuild)
8412 struct scroll_bar *bar;
8413 int start, end;
8414 int rebuild;
8415 {
8416 int dragging = ! NILP (bar->dragging);
8417 Window w = SCROLL_BAR_X_WINDOW (bar);
8418 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8419 GC gc = f->output_data.x->normal_gc;
8420
8421 /* If the display is already accurate, do nothing. */
8422 if (! rebuild
8423 && start == XINT (bar->start)
8424 && end == XINT (bar->end))
8425 return;
8426
8427 BLOCK_INPUT;
8428
8429 {
8430 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8431 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8432 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8433
8434 /* Make sure the values are reasonable, and try to preserve
8435 the distance between start and end. */
8436 {
8437 int length = end - start;
8438
8439 if (start < 0)
8440 start = 0;
8441 else if (start > top_range)
8442 start = top_range;
8443 end = start + length;
8444
8445 if (end < start)
8446 end = start;
8447 else if (end > top_range && ! dragging)
8448 end = top_range;
8449 }
8450
8451 /* Store the adjusted setting in the scroll bar. */
8452 XSETINT (bar->start, start);
8453 XSETINT (bar->end, end);
8454
8455 /* Clip the end position, just for display. */
8456 if (end > top_range)
8457 end = top_range;
8458
8459 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8460 below top positions, to make sure the handle is always at least
8461 that many pixels tall. */
8462 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8463
8464 /* Draw the empty space above the handle. Note that we can't clear
8465 zero-height areas; that means "clear to end of window." */
8466 if (0 < start)
8467 XClearArea (FRAME_X_DISPLAY (f), w,
8468
8469 /* x, y, width, height, and exposures. */
8470 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8471 VERTICAL_SCROLL_BAR_TOP_BORDER,
8472 inside_width, start,
8473 False);
8474
8475 /* Change to proper foreground color if one is specified. */
8476 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8477 XSetForeground (FRAME_X_DISPLAY (f), gc,
8478 f->output_data.x->scroll_bar_foreground_pixel);
8479
8480 /* Draw the handle itself. */
8481 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8482
8483 /* x, y, width, height */
8484 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8485 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8486 inside_width, end - start);
8487
8488 /* Restore the foreground color of the GC if we changed it above. */
8489 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8490 XSetForeground (FRAME_X_DISPLAY (f), gc,
8491 f->output_data.x->foreground_pixel);
8492
8493 /* Draw the empty space below the handle. Note that we can't
8494 clear zero-height areas; that means "clear to end of window." */
8495 if (end < inside_height)
8496 XClearArea (FRAME_X_DISPLAY (f), w,
8497
8498 /* x, y, width, height, and exposures. */
8499 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8500 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8501 inside_width, inside_height - end,
8502 False);
8503
8504 }
8505
8506 UNBLOCK_INPUT;
8507 }
8508
8509 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8510
8511 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8512 nil. */
8513
8514 static void
8515 x_scroll_bar_remove (bar)
8516 struct scroll_bar *bar;
8517 {
8518 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8519 BLOCK_INPUT;
8520
8521 #ifdef USE_TOOLKIT_SCROLL_BARS
8522 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8523 #else
8524 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8525 #endif
8526
8527 /* Disassociate this scroll bar from its window. */
8528 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8529
8530 UNBLOCK_INPUT;
8531 }
8532
8533
8534 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8535 that we are displaying PORTION characters out of a total of WHOLE
8536 characters, starting at POSITION. If WINDOW has no scroll bar,
8537 create one. */
8538
8539 static void
8540 XTset_vertical_scroll_bar (w, portion, whole, position)
8541 struct window *w;
8542 int portion, whole, position;
8543 {
8544 struct frame *f = XFRAME (w->frame);
8545 struct scroll_bar *bar;
8546 int top, height, left, sb_left, width, sb_width;
8547 int window_x, window_y, window_width, window_height;
8548
8549 /* Get window dimensions. */
8550 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8551 top = window_y;
8552 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8553 height = window_height;
8554
8555 /* Compute the left edge of the scroll bar area. */
8556 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8557 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8558 else
8559 left = XFASTINT (w->left);
8560 left *= CANON_X_UNIT (f);
8561 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8562
8563 /* Compute the width of the scroll bar which might be less than
8564 the width of the area reserved for the scroll bar. */
8565 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8566 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8567 else
8568 sb_width = width;
8569
8570 /* Compute the left edge of the scroll bar. */
8571 #ifdef USE_TOOLKIT_SCROLL_BARS
8572 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8573 sb_left = left + width - sb_width - (width - sb_width) / 2;
8574 else
8575 sb_left = left + (width - sb_width) / 2;
8576 #else
8577 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8578 sb_left = left + width - sb_width;
8579 else
8580 sb_left = left;
8581 #endif
8582
8583 /* Does the scroll bar exist yet? */
8584 if (NILP (w->vertical_scroll_bar))
8585 {
8586 BLOCK_INPUT;
8587 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8588 left, top, width, height, False);
8589 UNBLOCK_INPUT;
8590 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8591 }
8592 else
8593 {
8594 /* It may just need to be moved and resized. */
8595 unsigned int mask = 0;
8596
8597 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8598
8599 BLOCK_INPUT;
8600
8601 if (sb_left != XINT (bar->left))
8602 mask |= CWX;
8603 if (top != XINT (bar->top))
8604 mask |= CWY;
8605 if (sb_width != XINT (bar->width))
8606 mask |= CWWidth;
8607 if (height != XINT (bar->height))
8608 mask |= CWHeight;
8609
8610 #ifdef USE_TOOLKIT_SCROLL_BARS
8611
8612 /* Since toolkit scroll bars are smaller than the space reserved
8613 for them on the frame, we have to clear "under" them. */
8614 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8615 left, top, width, height, False);
8616
8617 /* Move/size the scroll bar widget. */
8618 if (mask)
8619 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
8620 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8621 top,
8622 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8623 height, 0);
8624
8625 #else /* not USE_TOOLKIT_SCROLL_BARS */
8626
8627 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8628 {
8629 /* Clear areas not covered by the scroll bar. This makes sure a
8630 previous mode line display is cleared after C-x 2 C-x 1, for
8631 example. Non-toolkit scroll bars are as wide as the area
8632 reserved for scroll bars - trim at both sides. */
8633 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8634 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8635 height, False);
8636 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8637 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8638 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8639 height, False);
8640 }
8641
8642 /* Move/size the scroll bar window. */
8643 if (mask)
8644 {
8645 XWindowChanges wc;
8646
8647 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8648 wc.y = top;
8649 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8650 wc.height = height;
8651 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8652 mask, &wc);
8653 }
8654
8655 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8656
8657 /* Remember new settings. */
8658 XSETINT (bar->left, sb_left);
8659 XSETINT (bar->top, top);
8660 XSETINT (bar->width, sb_width);
8661 XSETINT (bar->height, height);
8662
8663 UNBLOCK_INPUT;
8664 }
8665
8666 #ifdef USE_TOOLKIT_SCROLL_BARS
8667 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8668 #else /* not USE_TOOLKIT_SCROLL_BARS */
8669 /* Set the scroll bar's current state, unless we're currently being
8670 dragged. */
8671 if (NILP (bar->dragging))
8672 {
8673 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8674
8675 if (whole == 0)
8676 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8677 else
8678 {
8679 int start = ((double) position * top_range) / whole;
8680 int end = ((double) (position + portion) * top_range) / whole;
8681 x_scroll_bar_set_handle (bar, start, end, 0);
8682 }
8683 }
8684 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8685
8686 XSETVECTOR (w->vertical_scroll_bar, bar);
8687 }
8688
8689
8690 /* The following three hooks are used when we're doing a thorough
8691 redisplay of the frame. We don't explicitly know which scroll bars
8692 are going to be deleted, because keeping track of when windows go
8693 away is a real pain - "Can you say set-window-configuration, boys
8694 and girls?" Instead, we just assert at the beginning of redisplay
8695 that *all* scroll bars are to be removed, and then save a scroll bar
8696 from the fiery pit when we actually redisplay its window. */
8697
8698 /* Arrange for all scroll bars on FRAME to be removed at the next call
8699 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8700 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8701
8702 static void
8703 XTcondemn_scroll_bars (frame)
8704 FRAME_PTR frame;
8705 {
8706 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8707 while (! NILP (FRAME_SCROLL_BARS (frame)))
8708 {
8709 Lisp_Object bar;
8710 bar = FRAME_SCROLL_BARS (frame);
8711 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8712 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8713 XSCROLL_BAR (bar)->prev = Qnil;
8714 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8715 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8716 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8717 }
8718 }
8719
8720
8721 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8722 Note that WINDOW isn't necessarily condemned at all. */
8723
8724 static void
8725 XTredeem_scroll_bar (window)
8726 struct window *window;
8727 {
8728 struct scroll_bar *bar;
8729 struct frame *f;
8730
8731 /* We can't redeem this window's scroll bar if it doesn't have one. */
8732 if (NILP (window->vertical_scroll_bar))
8733 abort ();
8734
8735 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8736
8737 /* Unlink it from the condemned list. */
8738 f = XFRAME (WINDOW_FRAME (window));
8739 if (NILP (bar->prev))
8740 {
8741 /* If the prev pointer is nil, it must be the first in one of
8742 the lists. */
8743 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8744 /* It's not condemned. Everything's fine. */
8745 return;
8746 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8747 window->vertical_scroll_bar))
8748 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8749 else
8750 /* If its prev pointer is nil, it must be at the front of
8751 one or the other! */
8752 abort ();
8753 }
8754 else
8755 XSCROLL_BAR (bar->prev)->next = bar->next;
8756
8757 if (! NILP (bar->next))
8758 XSCROLL_BAR (bar->next)->prev = bar->prev;
8759
8760 bar->next = FRAME_SCROLL_BARS (f);
8761 bar->prev = Qnil;
8762 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8763 if (! NILP (bar->next))
8764 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8765 }
8766
8767 /* Remove all scroll bars on FRAME that haven't been saved since the
8768 last call to `*condemn_scroll_bars_hook'. */
8769
8770 static void
8771 XTjudge_scroll_bars (f)
8772 FRAME_PTR f;
8773 {
8774 Lisp_Object bar, next;
8775
8776 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8777
8778 /* Clear out the condemned list now so we won't try to process any
8779 more events on the hapless scroll bars. */
8780 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8781
8782 for (; ! NILP (bar); bar = next)
8783 {
8784 struct scroll_bar *b = XSCROLL_BAR (bar);
8785
8786 x_scroll_bar_remove (b);
8787
8788 next = b->next;
8789 b->next = b->prev = Qnil;
8790 }
8791
8792 /* Now there should be no references to the condemned scroll bars,
8793 and they should get garbage-collected. */
8794 }
8795
8796
8797 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8798 is a no-op when using toolkit scroll bars.
8799
8800 This may be called from a signal handler, so we have to ignore GC
8801 mark bits. */
8802
8803 static void
8804 x_scroll_bar_expose (bar, event)
8805 struct scroll_bar *bar;
8806 XEvent *event;
8807 {
8808 #ifndef USE_TOOLKIT_SCROLL_BARS
8809
8810 Window w = SCROLL_BAR_X_WINDOW (bar);
8811 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8812 GC gc = f->output_data.x->normal_gc;
8813 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8814
8815 BLOCK_INPUT;
8816
8817 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8818
8819 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8820 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8821
8822 /* x, y, width, height */
8823 0, 0,
8824 XINT (bar->width) - 1 - width_trim - width_trim,
8825 XINT (bar->height) - 1);
8826
8827 UNBLOCK_INPUT;
8828
8829 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8830 }
8831
8832 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8833 is set to something other than no_event, it is enqueued.
8834
8835 This may be called from a signal handler, so we have to ignore GC
8836 mark bits. */
8837
8838 #ifndef USE_TOOLKIT_SCROLL_BARS
8839
8840 static void
8841 x_scroll_bar_handle_click (bar, event, emacs_event)
8842 struct scroll_bar *bar;
8843 XEvent *event;
8844 struct input_event *emacs_event;
8845 {
8846 if (! GC_WINDOWP (bar->window))
8847 abort ();
8848
8849 emacs_event->kind = scroll_bar_click;
8850 emacs_event->code = event->xbutton.button - Button1;
8851 emacs_event->modifiers
8852 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8853 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8854 event->xbutton.state)
8855 | (event->type == ButtonRelease
8856 ? up_modifier
8857 : down_modifier));
8858 emacs_event->frame_or_window = bar->window;
8859 emacs_event->arg = Qnil;
8860 emacs_event->timestamp = event->xbutton.time;
8861 {
8862 #if 0
8863 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8864 int internal_height
8865 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8866 #endif
8867 int top_range
8868 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8869 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8870
8871 if (y < 0) y = 0;
8872 if (y > top_range) y = top_range;
8873
8874 if (y < XINT (bar->start))
8875 emacs_event->part = scroll_bar_above_handle;
8876 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8877 emacs_event->part = scroll_bar_handle;
8878 else
8879 emacs_event->part = scroll_bar_below_handle;
8880
8881 /* Just because the user has clicked on the handle doesn't mean
8882 they want to drag it. Lisp code needs to be able to decide
8883 whether or not we're dragging. */
8884 #if 0
8885 /* If the user has just clicked on the handle, record where they're
8886 holding it. */
8887 if (event->type == ButtonPress
8888 && emacs_event->part == scroll_bar_handle)
8889 XSETINT (bar->dragging, y - XINT (bar->start));
8890 #endif
8891
8892 /* If the user has released the handle, set it to its final position. */
8893 if (event->type == ButtonRelease
8894 && ! NILP (bar->dragging))
8895 {
8896 int new_start = y - XINT (bar->dragging);
8897 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8898
8899 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8900 bar->dragging = Qnil;
8901 }
8902
8903 /* Same deal here as the other #if 0. */
8904 #if 0
8905 /* Clicks on the handle are always reported as occurring at the top of
8906 the handle. */
8907 if (emacs_event->part == scroll_bar_handle)
8908 emacs_event->x = bar->start;
8909 else
8910 XSETINT (emacs_event->x, y);
8911 #else
8912 XSETINT (emacs_event->x, y);
8913 #endif
8914
8915 XSETINT (emacs_event->y, top_range);
8916 }
8917 }
8918
8919 /* Handle some mouse motion while someone is dragging the scroll bar.
8920
8921 This may be called from a signal handler, so we have to ignore GC
8922 mark bits. */
8923
8924 static void
8925 x_scroll_bar_note_movement (bar, event)
8926 struct scroll_bar *bar;
8927 XEvent *event;
8928 {
8929 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8930
8931 last_mouse_movement_time = event->xmotion.time;
8932
8933 f->mouse_moved = 1;
8934 XSETVECTOR (last_mouse_scroll_bar, bar);
8935
8936 /* If we're dragging the bar, display it. */
8937 if (! GC_NILP (bar->dragging))
8938 {
8939 /* Where should the handle be now? */
8940 int new_start = event->xmotion.y - XINT (bar->dragging);
8941
8942 if (new_start != XINT (bar->start))
8943 {
8944 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8945
8946 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8947 }
8948 }
8949 }
8950
8951 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8952
8953 /* Return information to the user about the current position of the mouse
8954 on the scroll bar. */
8955
8956 static void
8957 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8958 FRAME_PTR *fp;
8959 Lisp_Object *bar_window;
8960 enum scroll_bar_part *part;
8961 Lisp_Object *x, *y;
8962 unsigned long *time;
8963 {
8964 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8965 Window w = SCROLL_BAR_X_WINDOW (bar);
8966 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8967 int win_x, win_y;
8968 Window dummy_window;
8969 int dummy_coord;
8970 unsigned int dummy_mask;
8971
8972 BLOCK_INPUT;
8973
8974 /* Get the mouse's position relative to the scroll bar window, and
8975 report that. */
8976 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8977
8978 /* Root, child, root x and root y. */
8979 &dummy_window, &dummy_window,
8980 &dummy_coord, &dummy_coord,
8981
8982 /* Position relative to scroll bar. */
8983 &win_x, &win_y,
8984
8985 /* Mouse buttons and modifier keys. */
8986 &dummy_mask))
8987 ;
8988 else
8989 {
8990 #if 0
8991 int inside_height
8992 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8993 #endif
8994 int top_range
8995 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8996
8997 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8998
8999 if (! NILP (bar->dragging))
9000 win_y -= XINT (bar->dragging);
9001
9002 if (win_y < 0)
9003 win_y = 0;
9004 if (win_y > top_range)
9005 win_y = top_range;
9006
9007 *fp = f;
9008 *bar_window = bar->window;
9009
9010 if (! NILP (bar->dragging))
9011 *part = scroll_bar_handle;
9012 else if (win_y < XINT (bar->start))
9013 *part = scroll_bar_above_handle;
9014 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9015 *part = scroll_bar_handle;
9016 else
9017 *part = scroll_bar_below_handle;
9018
9019 XSETINT (*x, win_y);
9020 XSETINT (*y, top_range);
9021
9022 f->mouse_moved = 0;
9023 last_mouse_scroll_bar = Qnil;
9024 }
9025
9026 *time = last_mouse_movement_time;
9027
9028 UNBLOCK_INPUT;
9029 }
9030
9031
9032 /* The screen has been cleared so we may have changed foreground or
9033 background colors, and the scroll bars may need to be redrawn.
9034 Clear out the scroll bars, and ask for expose events, so we can
9035 redraw them. */
9036
9037 void
9038 x_scroll_bar_clear (f)
9039 FRAME_PTR f;
9040 {
9041 #ifndef USE_TOOLKIT_SCROLL_BARS
9042 Lisp_Object bar;
9043
9044 /* We can have scroll bars even if this is 0,
9045 if we just turned off scroll bar mode.
9046 But in that case we should not clear them. */
9047 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9048 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9049 bar = XSCROLL_BAR (bar)->next)
9050 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9051 0, 0, 0, 0, True);
9052 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9053 }
9054
9055 /* This processes Expose events from the menu-bar specific X event
9056 loop in xmenu.c. This allows to redisplay the frame if necessary
9057 when handling menu-bar or pop-up items. */
9058
9059 int
9060 process_expose_from_menu (event)
9061 XEvent event;
9062 {
9063 FRAME_PTR f;
9064 struct x_display_info *dpyinfo;
9065 int frame_exposed_p = 0;
9066
9067 BLOCK_INPUT;
9068
9069 dpyinfo = x_display_info_for_display (event.xexpose.display);
9070 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9071 if (f)
9072 {
9073 if (f->async_visible == 0)
9074 {
9075 f->async_visible = 1;
9076 f->async_iconified = 0;
9077 f->output_data.x->has_been_visible = 1;
9078 SET_FRAME_GARBAGED (f);
9079 }
9080 else
9081 {
9082 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9083 event.xexpose.x, event.xexpose.y,
9084 event.xexpose.width, event.xexpose.height);
9085 frame_exposed_p = 1;
9086 }
9087 }
9088 else
9089 {
9090 struct scroll_bar *bar
9091 = x_window_to_scroll_bar (event.xexpose.window);
9092
9093 if (bar)
9094 x_scroll_bar_expose (bar, &event);
9095 }
9096
9097 UNBLOCK_INPUT;
9098 return frame_exposed_p;
9099 }
9100 \f
9101 /* Define a queue to save up SelectionRequest events for later handling. */
9102
9103 struct selection_event_queue
9104 {
9105 XEvent event;
9106 struct selection_event_queue *next;
9107 };
9108
9109 static struct selection_event_queue *queue;
9110
9111 /* Nonzero means queue up certain events--don't process them yet. */
9112
9113 static int x_queue_selection_requests;
9114
9115 /* Queue up an X event *EVENT, to be processed later. */
9116
9117 static void
9118 x_queue_event (f, event)
9119 FRAME_PTR f;
9120 XEvent *event;
9121 {
9122 struct selection_event_queue *queue_tmp
9123 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9124
9125 if (queue_tmp != NULL)
9126 {
9127 queue_tmp->event = *event;
9128 queue_tmp->next = queue;
9129 queue = queue_tmp;
9130 }
9131 }
9132
9133 /* Take all the queued events and put them back
9134 so that they get processed afresh. */
9135
9136 static void
9137 x_unqueue_events (display)
9138 Display *display;
9139 {
9140 while (queue != NULL)
9141 {
9142 struct selection_event_queue *queue_tmp = queue;
9143 XPutBackEvent (display, &queue_tmp->event);
9144 queue = queue_tmp->next;
9145 xfree ((char *)queue_tmp);
9146 }
9147 }
9148
9149 /* Start queuing SelectionRequest events. */
9150
9151 void
9152 x_start_queuing_selection_requests (display)
9153 Display *display;
9154 {
9155 x_queue_selection_requests++;
9156 }
9157
9158 /* Stop queuing SelectionRequest events. */
9159
9160 void
9161 x_stop_queuing_selection_requests (display)
9162 Display *display;
9163 {
9164 x_queue_selection_requests--;
9165 x_unqueue_events (display);
9166 }
9167 \f
9168 /* The main X event-reading loop - XTread_socket. */
9169
9170 /* Time stamp of enter window event. This is only used by XTread_socket,
9171 but we have to put it out here, since static variables within functions
9172 sometimes don't work. */
9173
9174 static Time enter_timestamp;
9175
9176 /* This holds the state XLookupString needs to implement dead keys
9177 and other tricks known as "compose processing". _X Window System_
9178 says that a portable program can't use this, but Stephen Gildea assures
9179 me that letting the compiler initialize it to zeros will work okay.
9180
9181 This must be defined outside of XTread_socket, for the same reasons
9182 given for enter_time stamp, above. */
9183
9184 static XComposeStatus compose_status;
9185
9186 /* Record the last 100 characters stored
9187 to help debug the loss-of-chars-during-GC problem. */
9188
9189 static int temp_index;
9190 static short temp_buffer[100];
9191
9192 /* Set this to nonzero to fake an "X I/O error"
9193 on a particular display. */
9194
9195 struct x_display_info *XTread_socket_fake_io_error;
9196
9197 /* When we find no input here, we occasionally do a no-op command
9198 to verify that the X server is still running and we can still talk with it.
9199 We try all the open displays, one by one.
9200 This variable is used for cycling thru the displays. */
9201
9202 static struct x_display_info *next_noop_dpyinfo;
9203
9204 #define SET_SAVED_MENU_EVENT(size) \
9205 do \
9206 { \
9207 if (f->output_data.x->saved_menu_event == 0) \
9208 f->output_data.x->saved_menu_event \
9209 = (XEvent *) xmalloc (sizeof (XEvent)); \
9210 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9211 if (numchars >= 1) \
9212 { \
9213 bufp->kind = menu_bar_activate_event; \
9214 XSETFRAME (bufp->frame_or_window, f); \
9215 bufp->arg = Qnil; \
9216 bufp++; \
9217 count++; \
9218 numchars--; \
9219 } \
9220 } \
9221 while (0)
9222
9223 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9224 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9225
9226 /* Read events coming from the X server.
9227 This routine is called by the SIGIO handler.
9228 We return as soon as there are no more events to be read.
9229
9230 Events representing keys are stored in buffer BUFP,
9231 which can hold up to NUMCHARS characters.
9232 We return the number of characters stored into the buffer,
9233 thus pretending to be `read'.
9234
9235 EXPECTED is nonzero if the caller knows input is available. */
9236
9237 int
9238 XTread_socket (sd, bufp, numchars, expected)
9239 register int sd;
9240 /* register */ struct input_event *bufp;
9241 /* register */ int numchars;
9242 int expected;
9243 {
9244 int count = 0;
9245 int nbytes = 0;
9246 XEvent event;
9247 struct frame *f;
9248 int event_found = 0;
9249 struct x_display_info *dpyinfo;
9250 struct coding_system coding;
9251
9252 if (interrupt_input_blocked)
9253 {
9254 interrupt_input_pending = 1;
9255 return -1;
9256 }
9257
9258 interrupt_input_pending = 0;
9259 BLOCK_INPUT;
9260
9261 /* So people can tell when we have read the available input. */
9262 input_signal_count++;
9263
9264 if (numchars <= 0)
9265 abort (); /* Don't think this happens. */
9266
9267 ++handling_signal;
9268
9269 /* The input should be decoded if it is from XIM. Currently the
9270 locale of XIM is the same as that of the system. So, we can use
9271 Vlocale_coding_system which is initialized properly at Emacs
9272 startup time. */
9273 setup_coding_system (Vlocale_coding_system, &coding);
9274 coding.src_multibyte = 0;
9275 coding.dst_multibyte = 1;
9276 /* The input is converted to events, thus we can't handle
9277 composition. Anyway, there's no XIM that gives us composition
9278 information. */
9279 coding.composing = COMPOSITION_DISABLED;
9280
9281 /* Find the display we are supposed to read input for.
9282 It's the one communicating on descriptor SD. */
9283 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9284 {
9285 #if 0 /* This ought to be unnecessary; let's verify it. */
9286 #ifdef FIOSNBIO
9287 /* If available, Xlib uses FIOSNBIO to make the socket
9288 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9289 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9290 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9291 fcntl (dpyinfo->connection, F_SETFL, 0);
9292 #endif /* ! defined (FIOSNBIO) */
9293 #endif
9294
9295 #if 0 /* This code can't be made to work, with multiple displays,
9296 and appears not to be used on any system any more.
9297 Also keyboard.c doesn't turn O_NDELAY on and off
9298 for X connections. */
9299 #ifndef SIGIO
9300 #ifndef HAVE_SELECT
9301 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9302 {
9303 extern int read_alarm_should_throw;
9304 read_alarm_should_throw = 1;
9305 XPeekEvent (dpyinfo->display, &event);
9306 read_alarm_should_throw = 0;
9307 }
9308 #endif /* HAVE_SELECT */
9309 #endif /* SIGIO */
9310 #endif
9311
9312 /* For debugging, this gives a way to fake an I/O error. */
9313 if (dpyinfo == XTread_socket_fake_io_error)
9314 {
9315 XTread_socket_fake_io_error = 0;
9316 x_io_error_quitter (dpyinfo->display);
9317 }
9318
9319 while (XPending (dpyinfo->display))
9320 {
9321 XNextEvent (dpyinfo->display, &event);
9322
9323 #ifdef HAVE_X_I18N
9324 {
9325 /* Filter events for the current X input method.
9326 XFilterEvent returns non-zero if the input method has
9327 consumed the event. We pass the frame's X window to
9328 XFilterEvent because that's the one for which the IC
9329 was created. */
9330 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9331 event.xclient.window);
9332 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9333 break;
9334 }
9335 #endif
9336 event_found = 1;
9337
9338 switch (event.type)
9339 {
9340 case ClientMessage:
9341 {
9342 if (event.xclient.message_type
9343 == dpyinfo->Xatom_wm_protocols
9344 && event.xclient.format == 32)
9345 {
9346 if (event.xclient.data.l[0]
9347 == dpyinfo->Xatom_wm_take_focus)
9348 {
9349 /* Use x_any_window_to_frame because this
9350 could be the shell widget window
9351 if the frame has no title bar. */
9352 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9353 #ifdef HAVE_X_I18N
9354 /* Not quite sure this is needed -pd */
9355 if (f && FRAME_XIC (f))
9356 XSetICFocus (FRAME_XIC (f));
9357 #endif
9358 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9359 instructs the WM to set the input focus automatically for
9360 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9361 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9362 it has set the focus. So, XSetInputFocus below is not
9363 needed.
9364
9365 The call to XSetInputFocus below has also caused trouble. In
9366 cases where the XSetInputFocus done by the WM and the one
9367 below are temporally close (on a fast machine), the call
9368 below can generate additional FocusIn events which confuse
9369 Emacs. */
9370
9371 /* Since we set WM_TAKE_FOCUS, we must call
9372 XSetInputFocus explicitly. But not if f is null,
9373 since that might be an event for a deleted frame. */
9374 if (f)
9375 {
9376 Display *d = event.xclient.display;
9377 /* Catch and ignore errors, in case window has been
9378 iconified by a window manager such as GWM. */
9379 int count = x_catch_errors (d);
9380 XSetInputFocus (d, event.xclient.window,
9381 /* The ICCCM says this is
9382 the only valid choice. */
9383 RevertToParent,
9384 event.xclient.data.l[1]);
9385 /* This is needed to detect the error
9386 if there is an error. */
9387 XSync (d, False);
9388 x_uncatch_errors (d, count);
9389 }
9390 /* Not certain about handling scroll bars here */
9391 #endif /* 0 */
9392 }
9393 else if (event.xclient.data.l[0]
9394 == dpyinfo->Xatom_wm_save_yourself)
9395 {
9396 /* Save state modify the WM_COMMAND property to
9397 something which can reinstate us. This notifies
9398 the session manager, who's looking for such a
9399 PropertyNotify. Can restart processing when
9400 a keyboard or mouse event arrives. */
9401 if (numchars > 0)
9402 {
9403 f = x_top_window_to_frame (dpyinfo,
9404 event.xclient.window);
9405
9406 /* This is just so we only give real data once
9407 for a single Emacs process. */
9408 if (f == SELECTED_FRAME ())
9409 XSetCommand (FRAME_X_DISPLAY (f),
9410 event.xclient.window,
9411 initial_argv, initial_argc);
9412 else if (f)
9413 XSetCommand (FRAME_X_DISPLAY (f),
9414 event.xclient.window,
9415 0, 0);
9416 }
9417 }
9418 else if (event.xclient.data.l[0]
9419 == dpyinfo->Xatom_wm_delete_window)
9420 {
9421 struct frame *f
9422 = x_any_window_to_frame (dpyinfo,
9423 event.xclient.window);
9424
9425 if (f)
9426 {
9427 if (numchars == 0)
9428 abort ();
9429
9430 bufp->kind = delete_window_event;
9431 XSETFRAME (bufp->frame_or_window, f);
9432 bufp->arg = Qnil;
9433 bufp++;
9434
9435 count += 1;
9436 numchars -= 1;
9437 }
9438 }
9439 }
9440 else if (event.xclient.message_type
9441 == dpyinfo->Xatom_wm_configure_denied)
9442 {
9443 }
9444 else if (event.xclient.message_type
9445 == dpyinfo->Xatom_wm_window_moved)
9446 {
9447 int new_x, new_y;
9448 struct frame *f
9449 = x_window_to_frame (dpyinfo, event.xclient.window);
9450
9451 new_x = event.xclient.data.s[0];
9452 new_y = event.xclient.data.s[1];
9453
9454 if (f)
9455 {
9456 f->output_data.x->left_pos = new_x;
9457 f->output_data.x->top_pos = new_y;
9458 }
9459 }
9460 #ifdef HACK_EDITRES
9461 else if (event.xclient.message_type
9462 == dpyinfo->Xatom_editres)
9463 {
9464 struct frame *f
9465 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9466 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9467 &event, NULL);
9468 }
9469 #endif /* HACK_EDITRES */
9470 else if ((event.xclient.message_type
9471 == dpyinfo->Xatom_DONE)
9472 || (event.xclient.message_type
9473 == dpyinfo->Xatom_PAGE))
9474 {
9475 /* Ghostview job completed. Kill it. We could
9476 reply with "Next" if we received "Page", but we
9477 currently never do because we are interested in
9478 images, only, which should have 1 page. */
9479 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9480 struct frame *f
9481 = x_window_to_frame (dpyinfo, event.xclient.window);
9482 x_kill_gs_process (pixmap, f);
9483 expose_frame (f, 0, 0, 0, 0);
9484 }
9485 #ifdef USE_TOOLKIT_SCROLL_BARS
9486 /* Scroll bar callbacks send a ClientMessage from which
9487 we construct an input_event. */
9488 else if (event.xclient.message_type
9489 == dpyinfo->Xatom_Scrollbar)
9490 {
9491 x_scroll_bar_to_input_event (&event, bufp);
9492 ++bufp, ++count, --numchars;
9493 goto out;
9494 }
9495 #endif /* USE_TOOLKIT_SCROLL_BARS */
9496 else
9497 goto OTHER;
9498 }
9499 break;
9500
9501 case SelectionNotify:
9502 #ifdef USE_X_TOOLKIT
9503 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9504 goto OTHER;
9505 #endif /* not USE_X_TOOLKIT */
9506 x_handle_selection_notify (&event.xselection);
9507 break;
9508
9509 case SelectionClear: /* Someone has grabbed ownership. */
9510 #ifdef USE_X_TOOLKIT
9511 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9512 goto OTHER;
9513 #endif /* USE_X_TOOLKIT */
9514 {
9515 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9516
9517 if (numchars == 0)
9518 abort ();
9519
9520 bufp->kind = selection_clear_event;
9521 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9522 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9523 SELECTION_EVENT_TIME (bufp) = eventp->time;
9524 bufp->frame_or_window = Qnil;
9525 bufp->arg = Qnil;
9526 bufp++;
9527
9528 count += 1;
9529 numchars -= 1;
9530 }
9531 break;
9532
9533 case SelectionRequest: /* Someone wants our selection. */
9534 #ifdef USE_X_TOOLKIT
9535 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9536 goto OTHER;
9537 #endif /* USE_X_TOOLKIT */
9538 if (x_queue_selection_requests)
9539 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9540 &event);
9541 else
9542 {
9543 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9544
9545 if (numchars == 0)
9546 abort ();
9547
9548 bufp->kind = selection_request_event;
9549 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9550 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9551 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9552 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9553 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9554 SELECTION_EVENT_TIME (bufp) = eventp->time;
9555 bufp->frame_or_window = Qnil;
9556 bufp->arg = Qnil;
9557 bufp++;
9558
9559 count += 1;
9560 numchars -= 1;
9561 }
9562 break;
9563
9564 case PropertyNotify:
9565 #ifdef USE_X_TOOLKIT
9566 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9567 goto OTHER;
9568 #endif /* not USE_X_TOOLKIT */
9569 x_handle_property_notify (&event.xproperty);
9570 break;
9571
9572 case ReparentNotify:
9573 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9574 if (f)
9575 {
9576 int x, y;
9577 f->output_data.x->parent_desc = event.xreparent.parent;
9578 x_real_positions (f, &x, &y);
9579 f->output_data.x->left_pos = x;
9580 f->output_data.x->top_pos = y;
9581 }
9582 break;
9583
9584 case Expose:
9585 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9586 if (f)
9587 {
9588 if (f->async_visible == 0)
9589 {
9590 f->async_visible = 1;
9591 f->async_iconified = 0;
9592 f->output_data.x->has_been_visible = 1;
9593 SET_FRAME_GARBAGED (f);
9594 }
9595 else
9596 expose_frame (x_window_to_frame (dpyinfo,
9597 event.xexpose.window),
9598 event.xexpose.x, event.xexpose.y,
9599 event.xexpose.width, event.xexpose.height);
9600 }
9601 else
9602 {
9603 #ifdef USE_TOOLKIT_SCROLL_BARS
9604 /* Dispatch event to the widget. */
9605 goto OTHER;
9606 #else /* not USE_TOOLKIT_SCROLL_BARS */
9607 struct scroll_bar *bar
9608 = x_window_to_scroll_bar (event.xexpose.window);
9609
9610 if (bar)
9611 x_scroll_bar_expose (bar, &event);
9612 #ifdef USE_X_TOOLKIT
9613 else
9614 goto OTHER;
9615 #endif /* USE_X_TOOLKIT */
9616 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9617 }
9618 break;
9619
9620 case GraphicsExpose: /* This occurs when an XCopyArea's
9621 source area was obscured or not
9622 available.*/
9623 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9624 if (f)
9625 {
9626 expose_frame (f,
9627 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9628 event.xgraphicsexpose.width,
9629 event.xgraphicsexpose.height);
9630 }
9631 #ifdef USE_X_TOOLKIT
9632 else
9633 goto OTHER;
9634 #endif /* USE_X_TOOLKIT */
9635 break;
9636
9637 case NoExpose: /* This occurs when an XCopyArea's
9638 source area was completely
9639 available */
9640 break;
9641
9642 case UnmapNotify:
9643 /* Redo the mouse-highlight after the tooltip has gone. */
9644 if (event.xmap.window == tip_window)
9645 {
9646 tip_window = 0;
9647 redo_mouse_highlight ();
9648 }
9649
9650 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9651 if (f) /* F may no longer exist if
9652 the frame was deleted. */
9653 {
9654 /* While a frame is unmapped, display generation is
9655 disabled; you don't want to spend time updating a
9656 display that won't ever be seen. */
9657 f->async_visible = 0;
9658 /* We can't distinguish, from the event, whether the window
9659 has become iconified or invisible. So assume, if it
9660 was previously visible, than now it is iconified.
9661 But x_make_frame_invisible clears both
9662 the visible flag and the iconified flag;
9663 and that way, we know the window is not iconified now. */
9664 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9665 {
9666 f->async_iconified = 1;
9667
9668 bufp->kind = iconify_event;
9669 XSETFRAME (bufp->frame_or_window, f);
9670 bufp->arg = Qnil;
9671 bufp++;
9672 count++;
9673 numchars--;
9674 }
9675 }
9676 goto OTHER;
9677
9678 case MapNotify:
9679 if (event.xmap.window == tip_window)
9680 /* The tooltip has been drawn already. Avoid
9681 the SET_FRAME_GARBAGED below. */
9682 goto OTHER;
9683
9684 /* We use x_top_window_to_frame because map events can
9685 come for sub-windows and they don't mean that the
9686 frame is visible. */
9687 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9688 if (f)
9689 {
9690 f->async_visible = 1;
9691 f->async_iconified = 0;
9692 f->output_data.x->has_been_visible = 1;
9693
9694 /* wait_reading_process_input will notice this and update
9695 the frame's display structures. */
9696 SET_FRAME_GARBAGED (f);
9697
9698 if (f->iconified)
9699 {
9700 bufp->kind = deiconify_event;
9701 XSETFRAME (bufp->frame_or_window, f);
9702 bufp->arg = Qnil;
9703 bufp++;
9704 count++;
9705 numchars--;
9706 }
9707 else if (! NILP (Vframe_list)
9708 && ! NILP (XCDR (Vframe_list)))
9709 /* Force a redisplay sooner or later
9710 to update the frame titles
9711 in case this is the second frame. */
9712 record_asynch_buffer_change ();
9713 }
9714 goto OTHER;
9715
9716 case KeyPress:
9717 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9718
9719 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
9720 /* I couldn't find a way to prevent LessTif scroll bars
9721 from consuming key events. */
9722 if (f == 0)
9723 {
9724 Widget widget = XtWindowToWidget (dpyinfo->display,
9725 event.xkey.window);
9726 if (widget && XmIsScrollBar (widget))
9727 {
9728 widget = XtParent (widget);
9729 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9730 }
9731 }
9732 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
9733
9734 if (f != 0)
9735 {
9736 KeySym keysym, orig_keysym;
9737 /* al%imercury@uunet.uu.net says that making this 81
9738 instead of 80 fixed a bug whereby meta chars made
9739 his Emacs hang.
9740
9741 It seems that some version of XmbLookupString has
9742 a bug of not returning XBufferOverflow in
9743 status_return even if the input is too long to
9744 fit in 81 bytes. So, we must prepare sufficient
9745 bytes for copy_buffer. 513 bytes (256 chars for
9746 two-byte character set) seems to be a faily good
9747 approximation. -- 2000.8.10 handa@etl.go.jp */
9748 unsigned char copy_buffer[513];
9749 unsigned char *copy_bufptr = copy_buffer;
9750 int copy_bufsiz = sizeof (copy_buffer);
9751 int modifiers;
9752
9753 event.xkey.state
9754 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9755 extra_keyboard_modifiers);
9756 modifiers = event.xkey.state;
9757
9758 /* This will have to go some day... */
9759
9760 /* make_lispy_event turns chars into control chars.
9761 Don't do it here because XLookupString is too eager. */
9762 event.xkey.state &= ~ControlMask;
9763 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9764 | dpyinfo->super_mod_mask
9765 | dpyinfo->hyper_mod_mask
9766 | dpyinfo->alt_mod_mask);
9767
9768 /* In case Meta is ComposeCharacter,
9769 clear its status. According to Markus Ehrnsperger
9770 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9771 this enables ComposeCharacter to work whether or
9772 not it is combined with Meta. */
9773 if (modifiers & dpyinfo->meta_mod_mask)
9774 bzero (&compose_status, sizeof (compose_status));
9775
9776 #ifdef HAVE_X_I18N
9777 if (FRAME_XIC (f))
9778 {
9779 Status status_return;
9780
9781 nbytes = XmbLookupString (FRAME_XIC (f),
9782 &event.xkey, copy_bufptr,
9783 copy_bufsiz, &keysym,
9784 &status_return);
9785 if (status_return == XBufferOverflow)
9786 {
9787 copy_bufsiz = nbytes + 1;
9788 copy_bufptr = (char *) alloca (copy_bufsiz);
9789 nbytes = XmbLookupString (FRAME_XIC (f),
9790 &event.xkey, copy_bufptr,
9791 copy_bufsiz, &keysym,
9792 &status_return);
9793 }
9794
9795 if (status_return == XLookupNone)
9796 break;
9797 else if (status_return == XLookupChars)
9798 {
9799 keysym = NoSymbol;
9800 modifiers = 0;
9801 }
9802 else if (status_return != XLookupKeySym
9803 && status_return != XLookupBoth)
9804 abort ();
9805 }
9806 else
9807 nbytes = XLookupString (&event.xkey, copy_bufptr,
9808 copy_bufsiz, &keysym,
9809 &compose_status);
9810 #else
9811 nbytes = XLookupString (&event.xkey, copy_bufptr,
9812 copy_bufsiz, &keysym,
9813 &compose_status);
9814 #endif
9815
9816 orig_keysym = keysym;
9817
9818 if (numchars > 1)
9819 {
9820 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9821 || keysym == XK_Delete
9822 #ifdef XK_ISO_Left_Tab
9823 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9824 #endif
9825 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9826 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9827 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9828 #ifdef HPUX
9829 /* This recognizes the "extended function keys".
9830 It seems there's no cleaner way.
9831 Test IsModifierKey to avoid handling mode_switch
9832 incorrectly. */
9833 || ((unsigned) (keysym) >= XK_Select
9834 && (unsigned)(keysym) < XK_KP_Space)
9835 #endif
9836 #ifdef XK_dead_circumflex
9837 || orig_keysym == XK_dead_circumflex
9838 #endif
9839 #ifdef XK_dead_grave
9840 || orig_keysym == XK_dead_grave
9841 #endif
9842 #ifdef XK_dead_tilde
9843 || orig_keysym == XK_dead_tilde
9844 #endif
9845 #ifdef XK_dead_diaeresis
9846 || orig_keysym == XK_dead_diaeresis
9847 #endif
9848 #ifdef XK_dead_macron
9849 || orig_keysym == XK_dead_macron
9850 #endif
9851 #ifdef XK_dead_degree
9852 || orig_keysym == XK_dead_degree
9853 #endif
9854 #ifdef XK_dead_acute
9855 || orig_keysym == XK_dead_acute
9856 #endif
9857 #ifdef XK_dead_cedilla
9858 || orig_keysym == XK_dead_cedilla
9859 #endif
9860 #ifdef XK_dead_breve
9861 || orig_keysym == XK_dead_breve
9862 #endif
9863 #ifdef XK_dead_ogonek
9864 || orig_keysym == XK_dead_ogonek
9865 #endif
9866 #ifdef XK_dead_caron
9867 || orig_keysym == XK_dead_caron
9868 #endif
9869 #ifdef XK_dead_doubleacute
9870 || orig_keysym == XK_dead_doubleacute
9871 #endif
9872 #ifdef XK_dead_abovedot
9873 || orig_keysym == XK_dead_abovedot
9874 #endif
9875 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9876 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9877 /* Any "vendor-specific" key is ok. */
9878 || (orig_keysym & (1 << 28)))
9879 && ! (IsModifierKey (orig_keysym)
9880 #ifndef HAVE_X11R5
9881 #ifdef XK_Mode_switch
9882 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9883 #endif
9884 #ifdef XK_Num_Lock
9885 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9886 #endif
9887 #endif /* not HAVE_X11R5 */
9888 ))
9889 {
9890 if (temp_index == sizeof temp_buffer / sizeof (short))
9891 temp_index = 0;
9892 temp_buffer[temp_index++] = keysym;
9893 bufp->kind = non_ascii_keystroke;
9894 bufp->code = keysym;
9895 XSETFRAME (bufp->frame_or_window, f);
9896 bufp->arg = Qnil;
9897 bufp->modifiers
9898 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9899 modifiers);
9900 bufp->timestamp = event.xkey.time;
9901 bufp++;
9902 count++;
9903 numchars--;
9904 }
9905 else if (numchars > nbytes)
9906 {
9907 register int i;
9908 register int c;
9909 unsigned char *p, *pend;
9910 int nchars, len;
9911
9912 for (i = 0; i < nbytes; i++)
9913 {
9914 if (temp_index == (sizeof temp_buffer
9915 / sizeof (short)))
9916 temp_index = 0;
9917 temp_buffer[temp_index++] = copy_bufptr[i];
9918 }
9919
9920 if (/* If the event is not from XIM, */
9921 event.xkey.keycode != 0
9922 /* or the current locale doesn't request
9923 decoding of the intup data, ... */
9924 || coding.type == coding_type_raw_text
9925 || coding.type == coding_type_no_conversion)
9926 {
9927 /* ... we can use the input data as is. */
9928 nchars = nbytes;
9929 }
9930 else
9931 {
9932 /* We have to decode the input data. */
9933 int require;
9934 unsigned char *p;
9935
9936 require = decoding_buffer_size (&coding, nbytes);
9937 p = (unsigned char *) alloca (require);
9938 coding.mode |= CODING_MODE_LAST_BLOCK;
9939 decode_coding (&coding, copy_bufptr, p,
9940 nbytes, require);
9941 nbytes = coding.produced;
9942 nchars = coding.produced_char;
9943 copy_bufptr = p;
9944 }
9945
9946 /* Convert the input data to a sequence of
9947 character events. */
9948 for (i = 0; i < nbytes; i += len)
9949 {
9950 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9951 nbytes - i, len);
9952 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9953 ? ascii_keystroke
9954 : multibyte_char_keystroke);
9955 bufp->code = c;
9956 XSETFRAME (bufp->frame_or_window, f);
9957 bufp->arg = Qnil;
9958 bufp->modifiers
9959 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9960 modifiers);
9961 bufp->timestamp = event.xkey.time;
9962 bufp++;
9963 }
9964
9965 count += nchars;
9966 numchars -= nchars;
9967
9968 if (keysym == NoSymbol)
9969 break;
9970 }
9971 else
9972 abort ();
9973 }
9974 else
9975 abort ();
9976 }
9977 #ifdef HAVE_X_I18N
9978 /* Don't dispatch this event since XtDispatchEvent calls
9979 XFilterEvent, and two calls in a row may freeze the
9980 client. */
9981 break;
9982 #else
9983 goto OTHER;
9984 #endif
9985
9986 case KeyRelease:
9987 #ifdef HAVE_X_I18N
9988 /* Don't dispatch this event since XtDispatchEvent calls
9989 XFilterEvent, and two calls in a row may freeze the
9990 client. */
9991 break;
9992 #else
9993 goto OTHER;
9994 #endif
9995
9996 /* Here's a possible interpretation of the whole
9997 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9998 you get a FocusIn event, you have to get a FocusOut
9999 event before you relinquish the focus. If you
10000 haven't received a FocusIn event, then a mere
10001 LeaveNotify is enough to free you. */
10002
10003 case EnterNotify:
10004 {
10005 int from_menu_bar_p = 0;
10006
10007 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10008
10009 #ifdef LESSTIF_VERSION
10010 /* When clicking outside of a menu bar popup to close
10011 it, we get a FocusIn/ EnterNotify sequence of
10012 events. The flag event.xcrossing.focus is not set
10013 in the EnterNotify event of that sequence because
10014 the focus is in the menu bar,
10015 event.xcrossing.window is the frame's X window.
10016 Unconditionally setting the focus frame to null in
10017 this case is not the right thing, because no event
10018 follows that could set the focus frame to the right
10019 value.
10020
10021 This could be a LessTif bug, but I wasn't able to
10022 reproduce the behavior in a simple test program.
10023
10024 (gerd, LessTif 0.88.1). */
10025
10026 if (!event.xcrossing.focus
10027 && f
10028 && f->output_data.x->menubar_widget)
10029 {
10030 Window focus;
10031 int revert;
10032
10033 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10034 if (focus == XtWindow (f->output_data.x->menubar_widget))
10035 from_menu_bar_p = 1;
10036 }
10037 #endif /* LESSTIF_VERSION */
10038
10039 if (event.xcrossing.focus || from_menu_bar_p)
10040 {
10041 /* Avoid nasty pop/raise loops. */
10042 if (f && (!(f->auto_raise)
10043 || !(f->auto_lower)
10044 || (event.xcrossing.time - enter_timestamp) > 500))
10045 {
10046 x_new_focus_frame (dpyinfo, f);
10047 enter_timestamp = event.xcrossing.time;
10048 }
10049 }
10050 else if (f == dpyinfo->x_focus_frame)
10051 x_new_focus_frame (dpyinfo, 0);
10052
10053 /* EnterNotify counts as mouse movement,
10054 so update things that depend on mouse position. */
10055 if (f && !f->output_data.x->busy_p)
10056 note_mouse_movement (f, &event.xmotion);
10057 goto OTHER;
10058 }
10059
10060 case FocusIn:
10061 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10062 if (event.xfocus.detail != NotifyPointer)
10063 dpyinfo->x_focus_event_frame = f;
10064 if (f)
10065 {
10066 x_new_focus_frame (dpyinfo, f);
10067
10068 /* Don't stop displaying the initial startup message
10069 for a switch-frame event we don't need. */
10070 if (GC_NILP (Vterminal_frame)
10071 && GC_CONSP (Vframe_list)
10072 && !GC_NILP (XCDR (Vframe_list)))
10073 {
10074 bufp->kind = FOCUS_IN_EVENT;
10075 XSETFRAME (bufp->frame_or_window, f);
10076 bufp->arg = Qnil;
10077 ++bufp, ++count, --numchars;
10078 }
10079 }
10080
10081 #ifdef HAVE_X_I18N
10082 if (f && FRAME_XIC (f))
10083 XSetICFocus (FRAME_XIC (f));
10084 #endif
10085
10086 goto OTHER;
10087
10088 case LeaveNotify:
10089 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10090 if (f)
10091 {
10092 Lisp_Object frame;
10093 int from_menu_bar_p = 0;
10094
10095 if (f == dpyinfo->mouse_face_mouse_frame)
10096 {
10097 /* If we move outside the frame, then we're
10098 certainly no longer on any text in the frame. */
10099 clear_mouse_face (dpyinfo);
10100 dpyinfo->mouse_face_mouse_frame = 0;
10101 }
10102
10103 /* Generate a nil HELP_EVENT to cancel a help-echo.
10104 Do it only if there's something to cancel.
10105 Otherwise, the startup message is cleared when
10106 the mouse leaves the frame. */
10107 if (any_help_event_p)
10108 {
10109 Lisp_Object frame;
10110 int n;
10111
10112 XSETFRAME (frame, f);
10113 n = gen_help_event (bufp, numchars,
10114 Qnil, frame, Qnil, Qnil, 0);
10115 bufp += n, count += n, numchars -= n;
10116 }
10117
10118 #ifdef LESSTIF_VERSION
10119 /* Please see the comment at the start of the
10120 EnterNotify case. */
10121 if (!event.xcrossing.focus
10122 && f->output_data.x->menubar_widget)
10123 {
10124 Window focus;
10125 int revert;
10126 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10127 if (focus == XtWindow (f->output_data.x->menubar_widget))
10128 from_menu_bar_p = 1;
10129 }
10130 #endif /* LESSTIF_VERSION */
10131
10132 if (event.xcrossing.focus || from_menu_bar_p)
10133 x_mouse_leave (dpyinfo);
10134 else
10135 {
10136 if (f == dpyinfo->x_focus_event_frame)
10137 dpyinfo->x_focus_event_frame = 0;
10138 if (f == dpyinfo->x_focus_frame)
10139 x_new_focus_frame (dpyinfo, 0);
10140 }
10141 }
10142 goto OTHER;
10143
10144 case FocusOut:
10145 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10146 if (event.xfocus.detail != NotifyPointer
10147 && f == dpyinfo->x_focus_event_frame)
10148 dpyinfo->x_focus_event_frame = 0;
10149 if (f && f == dpyinfo->x_focus_frame)
10150 x_new_focus_frame (dpyinfo, 0);
10151
10152 #ifdef HAVE_X_I18N
10153 if (f && FRAME_XIC (f))
10154 XUnsetICFocus (FRAME_XIC (f));
10155 #endif
10156
10157 goto OTHER;
10158
10159 case MotionNotify:
10160 {
10161 previous_help_echo = help_echo;
10162 help_echo = help_echo_object = help_echo_window = Qnil;
10163 help_echo_pos = -1;
10164
10165 if (dpyinfo->grabbed && last_mouse_frame
10166 && FRAME_LIVE_P (last_mouse_frame))
10167 f = last_mouse_frame;
10168 else
10169 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10170
10171 if (f)
10172 note_mouse_movement (f, &event.xmotion);
10173 else
10174 {
10175 #ifndef USE_TOOLKIT_SCROLL_BARS
10176 struct scroll_bar *bar
10177 = x_window_to_scroll_bar (event.xmotion.window);
10178
10179 if (bar)
10180 x_scroll_bar_note_movement (bar, &event);
10181 #endif /* USE_TOOLKIT_SCROLL_BARS */
10182
10183 /* If we move outside the frame, then we're
10184 certainly no longer on any text in the frame. */
10185 clear_mouse_face (dpyinfo);
10186 }
10187
10188 /* If the contents of the global variable help_echo
10189 has changed, generate a HELP_EVENT. */
10190 if (!NILP (help_echo)
10191 || !NILP (previous_help_echo))
10192 {
10193 Lisp_Object frame;
10194 int n;
10195
10196 if (f)
10197 XSETFRAME (frame, f);
10198 else
10199 frame = Qnil;
10200
10201 any_help_event_p = 1;
10202 n = gen_help_event (bufp, numchars, help_echo, frame,
10203 help_echo_window, help_echo_object,
10204 help_echo_pos);
10205 bufp += n, count += n, numchars -= n;
10206 }
10207
10208 goto OTHER;
10209 }
10210
10211 case ConfigureNotify:
10212 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10213 if (f)
10214 {
10215 #ifndef USE_X_TOOLKIT
10216 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10217 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10218
10219 /* In the toolkit version, change_frame_size
10220 is called by the code that handles resizing
10221 of the EmacsFrame widget. */
10222
10223 /* Even if the number of character rows and columns has
10224 not changed, the font size may have changed, so we need
10225 to check the pixel dimensions as well. */
10226 if (columns != f->width
10227 || rows != f->height
10228 || event.xconfigure.width != f->output_data.x->pixel_width
10229 || event.xconfigure.height != f->output_data.x->pixel_height)
10230 {
10231 change_frame_size (f, rows, columns, 0, 1, 0);
10232 SET_FRAME_GARBAGED (f);
10233 cancel_mouse_face (f);
10234 }
10235 #endif
10236
10237 f->output_data.x->pixel_width = event.xconfigure.width;
10238 f->output_data.x->pixel_height = event.xconfigure.height;
10239
10240 /* What we have now is the position of Emacs's own window.
10241 Convert that to the position of the window manager window. */
10242 x_real_positions (f, &f->output_data.x->left_pos,
10243 &f->output_data.x->top_pos);
10244
10245 #ifdef HAVE_X_I18N
10246 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10247 xic_set_statusarea (f);
10248 #endif
10249
10250 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10251 {
10252 /* Since the WM decorations come below top_pos now,
10253 we must put them below top_pos in the future. */
10254 f->output_data.x->win_gravity = NorthWestGravity;
10255 x_wm_set_size_hint (f, (long) 0, 0);
10256 }
10257 #ifdef USE_MOTIF
10258 /* Some window managers pass (0,0) as the location of
10259 the window, and the Motif event handler stores it
10260 in the emacs widget, which messes up Motif menus. */
10261 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10262 {
10263 event.xconfigure.x = f->output_data.x->widget->core.x;
10264 event.xconfigure.y = f->output_data.x->widget->core.y;
10265 }
10266 #endif /* USE_MOTIF */
10267 }
10268 goto OTHER;
10269
10270 case ButtonPress:
10271 case ButtonRelease:
10272 {
10273 /* If we decide we want to generate an event to be seen
10274 by the rest of Emacs, we put it here. */
10275 struct input_event emacs_event;
10276 int tool_bar_p = 0;
10277
10278 emacs_event.kind = no_event;
10279 bzero (&compose_status, sizeof (compose_status));
10280
10281 if (dpyinfo->grabbed
10282 && last_mouse_frame
10283 && FRAME_LIVE_P (last_mouse_frame))
10284 f = last_mouse_frame;
10285 else
10286 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10287
10288 if (f)
10289 {
10290 /* Is this in the tool-bar? */
10291 if (WINDOWP (f->tool_bar_window)
10292 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10293 {
10294 Lisp_Object window;
10295 int p, x, y;
10296
10297 x = event.xbutton.x;
10298 y = event.xbutton.y;
10299
10300 /* Set x and y. */
10301 window = window_from_coordinates (f, x, y, &p, 1);
10302 if (EQ (window, f->tool_bar_window))
10303 {
10304 x_handle_tool_bar_click (f, &event.xbutton);
10305 tool_bar_p = 1;
10306 }
10307 }
10308
10309 if (!tool_bar_p)
10310 if (!dpyinfo->x_focus_frame
10311 || f == dpyinfo->x_focus_frame)
10312 construct_mouse_click (&emacs_event, &event, f);
10313 }
10314 else
10315 {
10316 #ifndef USE_TOOLKIT_SCROLL_BARS
10317 struct scroll_bar *bar
10318 = x_window_to_scroll_bar (event.xbutton.window);
10319
10320 if (bar)
10321 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10322 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10323 }
10324
10325 if (event.type == ButtonPress)
10326 {
10327 dpyinfo->grabbed |= (1 << event.xbutton.button);
10328 last_mouse_frame = f;
10329 /* Ignore any mouse motion that happened
10330 before this event; any subsequent mouse-movement
10331 Emacs events should reflect only motion after
10332 the ButtonPress. */
10333 if (f != 0)
10334 f->mouse_moved = 0;
10335
10336 if (!tool_bar_p)
10337 last_tool_bar_item = -1;
10338 }
10339 else
10340 {
10341 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10342 }
10343
10344 if (numchars >= 1 && emacs_event.kind != no_event)
10345 {
10346 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10347 bufp++;
10348 count++;
10349 numchars--;
10350 }
10351
10352 #ifdef USE_X_TOOLKIT
10353 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10354 /* For a down-event in the menu bar,
10355 don't pass it to Xt right now.
10356 Instead, save it away
10357 and we will pass it to Xt from kbd_buffer_get_event.
10358 That way, we can run some Lisp code first. */
10359 if (f && event.type == ButtonPress
10360 /* Verify the event is really within the menu bar
10361 and not just sent to it due to grabbing. */
10362 && event.xbutton.x >= 0
10363 && event.xbutton.x < f->output_data.x->pixel_width
10364 && event.xbutton.y >= 0
10365 && event.xbutton.y < f->output_data.x->menubar_height
10366 && event.xbutton.same_screen)
10367 {
10368 SET_SAVED_BUTTON_EVENT;
10369 XSETFRAME (last_mouse_press_frame, f);
10370 }
10371 else if (event.type == ButtonPress)
10372 {
10373 last_mouse_press_frame = Qnil;
10374 goto OTHER;
10375 }
10376
10377 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10378 but I am trying to be cautious. */
10379 else if (event.type == ButtonRelease)
10380 {
10381 if (!NILP (last_mouse_press_frame))
10382 {
10383 f = XFRAME (last_mouse_press_frame);
10384 if (f->output_data.x)
10385 SET_SAVED_BUTTON_EVENT;
10386 }
10387 else
10388 goto OTHER;
10389 }
10390 #endif /* USE_MOTIF */
10391 else
10392 goto OTHER;
10393 #endif /* USE_X_TOOLKIT */
10394 }
10395 break;
10396
10397 case CirculateNotify:
10398 goto OTHER;
10399
10400 case CirculateRequest:
10401 goto OTHER;
10402
10403 case VisibilityNotify:
10404 goto OTHER;
10405
10406 case MappingNotify:
10407 /* Someone has changed the keyboard mapping - update the
10408 local cache. */
10409 switch (event.xmapping.request)
10410 {
10411 case MappingModifier:
10412 x_find_modifier_meanings (dpyinfo);
10413 /* This is meant to fall through. */
10414 case MappingKeyboard:
10415 XRefreshKeyboardMapping (&event.xmapping);
10416 }
10417 goto OTHER;
10418
10419 default:
10420 OTHER:
10421 #ifdef USE_X_TOOLKIT
10422 BLOCK_INPUT;
10423 XtDispatchEvent (&event);
10424 UNBLOCK_INPUT;
10425 #endif /* USE_X_TOOLKIT */
10426 break;
10427 }
10428 }
10429 }
10430
10431 out:;
10432
10433 /* On some systems, an X bug causes Emacs to get no more events
10434 when the window is destroyed. Detect that. (1994.) */
10435 if (! event_found)
10436 {
10437 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10438 One XNOOP in 100 loops will make Emacs terminate.
10439 B. Bretthauer, 1994 */
10440 x_noop_count++;
10441 if (x_noop_count >= 100)
10442 {
10443 x_noop_count=0;
10444
10445 if (next_noop_dpyinfo == 0)
10446 next_noop_dpyinfo = x_display_list;
10447
10448 XNoOp (next_noop_dpyinfo->display);
10449
10450 /* Each time we get here, cycle through the displays now open. */
10451 next_noop_dpyinfo = next_noop_dpyinfo->next;
10452 }
10453 }
10454
10455 /* If the focus was just given to an auto-raising frame,
10456 raise it now. */
10457 /* ??? This ought to be able to handle more than one such frame. */
10458 if (pending_autoraise_frame)
10459 {
10460 x_raise_frame (pending_autoraise_frame);
10461 pending_autoraise_frame = 0;
10462 }
10463
10464 UNBLOCK_INPUT;
10465 --handling_signal;
10466 return count;
10467 }
10468
10469
10470
10471 \f
10472 /***********************************************************************
10473 Text Cursor
10474 ***********************************************************************/
10475
10476 /* Note if the text cursor of window W has been overwritten by a
10477 drawing operation that outputs N glyphs starting at HPOS in the
10478 line given by output_cursor.vpos. N < 0 means all the rest of the
10479 line after HPOS has been written. */
10480
10481 static void
10482 note_overwritten_text_cursor (w, hpos, n)
10483 struct window *w;
10484 int hpos, n;
10485 {
10486 if (updated_area == TEXT_AREA
10487 && output_cursor.vpos == w->phys_cursor.vpos
10488 && hpos <= w->phys_cursor.hpos
10489 && (n < 0
10490 || hpos + n > w->phys_cursor.hpos))
10491 w->phys_cursor_on_p = 0;
10492 }
10493
10494
10495 /* Set clipping for output in glyph row ROW. W is the window in which
10496 we operate. GC is the graphics context to set clipping in.
10497 WHOLE_LINE_P non-zero means include the areas used for truncation
10498 mark display and alike in the clipping rectangle.
10499
10500 ROW may be a text row or, e.g., a mode line. Text rows must be
10501 clipped to the interior of the window dedicated to text display,
10502 mode lines must be clipped to the whole window. */
10503
10504 static void
10505 x_clip_to_row (w, row, gc, whole_line_p)
10506 struct window *w;
10507 struct glyph_row *row;
10508 GC gc;
10509 int whole_line_p;
10510 {
10511 struct frame *f = XFRAME (WINDOW_FRAME (w));
10512 XRectangle clip_rect;
10513 int window_x, window_y, window_width, window_height;
10514
10515 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10516
10517 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10518 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10519 clip_rect.y = max (clip_rect.y, window_y);
10520 clip_rect.width = window_width;
10521 clip_rect.height = row->visible_height;
10522
10523 /* If clipping to the whole line, including trunc marks, extend
10524 the rectangle to the left and increase its width. */
10525 if (whole_line_p)
10526 {
10527 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10528 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10529 }
10530
10531 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10532 }
10533
10534
10535 /* Draw a hollow box cursor on window W in glyph row ROW. */
10536
10537 static void
10538 x_draw_hollow_cursor (w, row)
10539 struct window *w;
10540 struct glyph_row *row;
10541 {
10542 struct frame *f = XFRAME (WINDOW_FRAME (w));
10543 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10544 Display *dpy = FRAME_X_DISPLAY (f);
10545 int x, y, wd, h;
10546 XGCValues xgcv;
10547 struct glyph *cursor_glyph;
10548 GC gc;
10549
10550 /* Compute frame-relative coordinates from window-relative
10551 coordinates. */
10552 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10553 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10554 + row->ascent - w->phys_cursor_ascent);
10555 h = row->height - 1;
10556
10557 /* Get the glyph the cursor is on. If we can't tell because
10558 the current matrix is invalid or such, give up. */
10559 cursor_glyph = get_phys_cursor_glyph (w);
10560 if (cursor_glyph == NULL)
10561 return;
10562
10563 /* Compute the width of the rectangle to draw. If on a stretch
10564 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10565 rectangle as wide as the glyph, but use a canonical character
10566 width instead. */
10567 wd = cursor_glyph->pixel_width - 1;
10568 if (cursor_glyph->type == STRETCH_GLYPH
10569 && !x_stretch_cursor_p)
10570 wd = min (CANON_X_UNIT (f), wd);
10571
10572 /* The foreground of cursor_gc is typically the same as the normal
10573 background color, which can cause the cursor box to be invisible. */
10574 xgcv.foreground = f->output_data.x->cursor_pixel;
10575 if (dpyinfo->scratch_cursor_gc)
10576 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10577 else
10578 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10579 GCForeground, &xgcv);
10580 gc = dpyinfo->scratch_cursor_gc;
10581
10582 /* Set clipping, draw the rectangle, and reset clipping again. */
10583 x_clip_to_row (w, row, gc, 0);
10584 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10585 XSetClipMask (dpy, gc, None);
10586 }
10587
10588
10589 /* Draw a bar cursor on window W in glyph row ROW.
10590
10591 Implementation note: One would like to draw a bar cursor with an
10592 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10593 Unfortunately, I didn't find a font yet that has this property set.
10594 --gerd. */
10595
10596 static void
10597 x_draw_bar_cursor (w, row, width)
10598 struct window *w;
10599 struct glyph_row *row;
10600 int width;
10601 {
10602 struct frame *f = XFRAME (w->frame);
10603 struct glyph *cursor_glyph;
10604 GC gc;
10605 int x;
10606 unsigned long mask;
10607 XGCValues xgcv;
10608 Display *dpy;
10609 Window window;
10610
10611 /* If cursor is out of bounds, don't draw garbage. This can happen
10612 in mini-buffer windows when switching between echo area glyphs
10613 and mini-buffer. */
10614 cursor_glyph = get_phys_cursor_glyph (w);
10615 if (cursor_glyph == NULL)
10616 return;
10617
10618 /* If on an image, draw like a normal cursor. That's usually better
10619 visible than drawing a bar, esp. if the image is large so that
10620 the bar might not be in the window. */
10621 if (cursor_glyph->type == IMAGE_GLYPH)
10622 {
10623 struct glyph_row *row;
10624 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10625 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10626 }
10627 else
10628 {
10629 xgcv.background = f->output_data.x->cursor_pixel;
10630 xgcv.foreground = f->output_data.x->cursor_pixel;
10631 xgcv.graphics_exposures = 0;
10632 mask = GCForeground | GCBackground | GCGraphicsExposures;
10633 dpy = FRAME_X_DISPLAY (f);
10634 window = FRAME_X_WINDOW (f);
10635 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10636
10637 if (gc)
10638 XChangeGC (dpy, gc, mask, &xgcv);
10639 else
10640 {
10641 gc = XCreateGC (dpy, window, mask, &xgcv);
10642 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10643 }
10644
10645 if (width < 0)
10646 width = f->output_data.x->cursor_width;
10647
10648 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10649 x_clip_to_row (w, row, gc, 0);
10650 XFillRectangle (dpy, window, gc,
10651 x,
10652 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10653 min (cursor_glyph->pixel_width, width),
10654 row->height);
10655 XSetClipMask (dpy, gc, None);
10656 }
10657 }
10658
10659
10660 /* Clear the cursor of window W to background color, and mark the
10661 cursor as not shown. This is used when the text where the cursor
10662 is is about to be rewritten. */
10663
10664 static void
10665 x_clear_cursor (w)
10666 struct window *w;
10667 {
10668 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10669 x_update_window_cursor (w, 0);
10670 }
10671
10672
10673 /* Draw the cursor glyph of window W in glyph row ROW. See the
10674 comment of x_draw_glyphs for the meaning of HL. */
10675
10676 static void
10677 x_draw_phys_cursor_glyph (w, row, hl)
10678 struct window *w;
10679 struct glyph_row *row;
10680 enum draw_glyphs_face hl;
10681 {
10682 /* If cursor hpos is out of bounds, don't draw garbage. This can
10683 happen in mini-buffer windows when switching between echo area
10684 glyphs and mini-buffer. */
10685 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10686 {
10687 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10688 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10689 hl, 0, 0, 0);
10690
10691 /* When we erase the cursor, and ROW is overlapped by other
10692 rows, make sure that these overlapping parts of other rows
10693 are redrawn. */
10694 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10695 {
10696 if (row > w->current_matrix->rows
10697 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10698 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10699
10700 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10701 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10702 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10703 }
10704 }
10705 }
10706
10707
10708 /* Erase the image of a cursor of window W from the screen. */
10709
10710 static void
10711 x_erase_phys_cursor (w)
10712 struct window *w;
10713 {
10714 struct frame *f = XFRAME (w->frame);
10715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10716 int hpos = w->phys_cursor.hpos;
10717 int vpos = w->phys_cursor.vpos;
10718 int mouse_face_here_p = 0;
10719 struct glyph_matrix *active_glyphs = w->current_matrix;
10720 struct glyph_row *cursor_row;
10721 struct glyph *cursor_glyph;
10722 enum draw_glyphs_face hl;
10723
10724 /* No cursor displayed or row invalidated => nothing to do on the
10725 screen. */
10726 if (w->phys_cursor_type == NO_CURSOR)
10727 goto mark_cursor_off;
10728
10729 /* VPOS >= active_glyphs->nrows means that window has been resized.
10730 Don't bother to erase the cursor. */
10731 if (vpos >= active_glyphs->nrows)
10732 goto mark_cursor_off;
10733
10734 /* If row containing cursor is marked invalid, there is nothing we
10735 can do. */
10736 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10737 if (!cursor_row->enabled_p)
10738 goto mark_cursor_off;
10739
10740 /* This can happen when the new row is shorter than the old one.
10741 In this case, either x_draw_glyphs or clear_end_of_line
10742 should have cleared the cursor. Note that we wouldn't be
10743 able to erase the cursor in this case because we don't have a
10744 cursor glyph at hand. */
10745 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10746 goto mark_cursor_off;
10747
10748 /* If the cursor is in the mouse face area, redisplay that when
10749 we clear the cursor. */
10750 if (! NILP (dpyinfo->mouse_face_window)
10751 && w == XWINDOW (dpyinfo->mouse_face_window)
10752 && (vpos > dpyinfo->mouse_face_beg_row
10753 || (vpos == dpyinfo->mouse_face_beg_row
10754 && hpos >= dpyinfo->mouse_face_beg_col))
10755 && (vpos < dpyinfo->mouse_face_end_row
10756 || (vpos == dpyinfo->mouse_face_end_row
10757 && hpos < dpyinfo->mouse_face_end_col))
10758 /* Don't redraw the cursor's spot in mouse face if it is at the
10759 end of a line (on a newline). The cursor appears there, but
10760 mouse highlighting does not. */
10761 && cursor_row->used[TEXT_AREA] > hpos)
10762 mouse_face_here_p = 1;
10763
10764 /* Maybe clear the display under the cursor. */
10765 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10766 {
10767 int x;
10768 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10769
10770 cursor_glyph = get_phys_cursor_glyph (w);
10771 if (cursor_glyph == NULL)
10772 goto mark_cursor_off;
10773
10774 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10775
10776 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10777 x,
10778 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10779 cursor_row->y)),
10780 cursor_glyph->pixel_width,
10781 cursor_row->visible_height,
10782 False);
10783 }
10784
10785 /* Erase the cursor by redrawing the character underneath it. */
10786 if (mouse_face_here_p)
10787 hl = DRAW_MOUSE_FACE;
10788 else if (cursor_row->inverse_p)
10789 hl = DRAW_INVERSE_VIDEO;
10790 else
10791 hl = DRAW_NORMAL_TEXT;
10792 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10793
10794 mark_cursor_off:
10795 w->phys_cursor_on_p = 0;
10796 w->phys_cursor_type = NO_CURSOR;
10797 }
10798
10799
10800 /* Display or clear cursor of window W. If ON is zero, clear the
10801 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10802 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10803
10804 void
10805 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10806 struct window *w;
10807 int on, hpos, vpos, x, y;
10808 {
10809 struct frame *f = XFRAME (w->frame);
10810 int new_cursor_type;
10811 int new_cursor_width;
10812 struct glyph_matrix *current_glyphs;
10813 struct glyph_row *glyph_row;
10814 struct glyph *glyph;
10815
10816 /* This is pointless on invisible frames, and dangerous on garbaged
10817 windows and frames; in the latter case, the frame or window may
10818 be in the midst of changing its size, and x and y may be off the
10819 window. */
10820 if (! FRAME_VISIBLE_P (f)
10821 || FRAME_GARBAGED_P (f)
10822 || vpos >= w->current_matrix->nrows
10823 || hpos >= w->current_matrix->matrix_w)
10824 return;
10825
10826 /* If cursor is off and we want it off, return quickly. */
10827 if (!on && !w->phys_cursor_on_p)
10828 return;
10829
10830 current_glyphs = w->current_matrix;
10831 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10832 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10833
10834 /* If cursor row is not enabled, we don't really know where to
10835 display the cursor. */
10836 if (!glyph_row->enabled_p)
10837 {
10838 w->phys_cursor_on_p = 0;
10839 return;
10840 }
10841
10842 xassert (interrupt_input_blocked);
10843
10844 /* Set new_cursor_type to the cursor we want to be displayed. In a
10845 mini-buffer window, we want the cursor only to appear if we are
10846 reading input from this window. For the selected window, we want
10847 the cursor type given by the frame parameter. If explicitly
10848 marked off, draw no cursor. In all other cases, we want a hollow
10849 box cursor. */
10850 new_cursor_width = -1;
10851 if (cursor_in_echo_area
10852 && FRAME_HAS_MINIBUF_P (f)
10853 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10854 {
10855 if (w == XWINDOW (echo_area_window))
10856 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10857 else
10858 new_cursor_type = HOLLOW_BOX_CURSOR;
10859 }
10860 else
10861 {
10862 if (w != XWINDOW (selected_window)
10863 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10864 {
10865 extern int cursor_in_non_selected_windows;
10866
10867 if (MINI_WINDOW_P (w)
10868 || !cursor_in_non_selected_windows
10869 || NILP (XBUFFER (w->buffer)->cursor_type))
10870 new_cursor_type = NO_CURSOR;
10871 else
10872 new_cursor_type = HOLLOW_BOX_CURSOR;
10873 }
10874 else if (w->cursor_off_p)
10875 new_cursor_type = NO_CURSOR;
10876 else
10877 {
10878 struct buffer *b = XBUFFER (w->buffer);
10879
10880 if (EQ (b->cursor_type, Qt))
10881 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10882 else
10883 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10884 &new_cursor_width);
10885 }
10886 }
10887
10888 /* If cursor is currently being shown and we don't want it to be or
10889 it is in the wrong place, or the cursor type is not what we want,
10890 erase it. */
10891 if (w->phys_cursor_on_p
10892 && (!on
10893 || w->phys_cursor.x != x
10894 || w->phys_cursor.y != y
10895 || new_cursor_type != w->phys_cursor_type))
10896 x_erase_phys_cursor (w);
10897
10898 /* If the cursor is now invisible and we want it to be visible,
10899 display it. */
10900 if (on && !w->phys_cursor_on_p)
10901 {
10902 w->phys_cursor_ascent = glyph_row->ascent;
10903 w->phys_cursor_height = glyph_row->height;
10904
10905 /* Set phys_cursor_.* before x_draw_.* is called because some
10906 of them may need the information. */
10907 w->phys_cursor.x = x;
10908 w->phys_cursor.y = glyph_row->y;
10909 w->phys_cursor.hpos = hpos;
10910 w->phys_cursor.vpos = vpos;
10911 w->phys_cursor_type = new_cursor_type;
10912 w->phys_cursor_on_p = 1;
10913
10914 switch (new_cursor_type)
10915 {
10916 case HOLLOW_BOX_CURSOR:
10917 x_draw_hollow_cursor (w, glyph_row);
10918 break;
10919
10920 case FILLED_BOX_CURSOR:
10921 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10922 break;
10923
10924 case BAR_CURSOR:
10925 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10926 break;
10927
10928 case NO_CURSOR:
10929 break;
10930
10931 default:
10932 abort ();
10933 }
10934
10935 #ifdef HAVE_X_I18N
10936 if (w == XWINDOW (f->selected_window))
10937 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10938 xic_set_preeditarea (w, x, y);
10939 #endif
10940 }
10941
10942 #ifndef XFlush
10943 if (updating_frame != f)
10944 XFlush (FRAME_X_DISPLAY (f));
10945 #endif
10946 }
10947
10948
10949 /* Display the cursor on window W, or clear it. X and Y are window
10950 relative pixel coordinates. HPOS and VPOS are glyph matrix
10951 positions. If W is not the selected window, display a hollow
10952 cursor. ON non-zero means display the cursor at X, Y which
10953 correspond to HPOS, VPOS, otherwise it is cleared. */
10954
10955 void
10956 x_display_cursor (w, on, hpos, vpos, x, y)
10957 struct window *w;
10958 int on, hpos, vpos, x, y;
10959 {
10960 BLOCK_INPUT;
10961 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10962 UNBLOCK_INPUT;
10963 }
10964
10965
10966 /* Display the cursor on window W, or clear it, according to ON_P.
10967 Don't change the cursor's position. */
10968
10969 void
10970 x_update_cursor (f, on_p)
10971 struct frame *f;
10972 {
10973 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10974 }
10975
10976
10977 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10978 in the window tree rooted at W. */
10979
10980 static void
10981 x_update_cursor_in_window_tree (w, on_p)
10982 struct window *w;
10983 int on_p;
10984 {
10985 while (w)
10986 {
10987 if (!NILP (w->hchild))
10988 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10989 else if (!NILP (w->vchild))
10990 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10991 else
10992 x_update_window_cursor (w, on_p);
10993
10994 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10995 }
10996 }
10997
10998
10999 /* Switch the display of W's cursor on or off, according to the value
11000 of ON. */
11001
11002 static void
11003 x_update_window_cursor (w, on)
11004 struct window *w;
11005 int on;
11006 {
11007 /* Don't update cursor in windows whose frame is in the process
11008 of being deleted. */
11009 if (w->current_matrix)
11010 {
11011 BLOCK_INPUT;
11012 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11013 w->phys_cursor.x, w->phys_cursor.y);
11014 UNBLOCK_INPUT;
11015 }
11016 }
11017
11018
11019
11020 \f
11021 /* Icons. */
11022
11023 /* Refresh bitmap kitchen sink icon for frame F
11024 when we get an expose event for it. */
11025
11026 void
11027 refreshicon (f)
11028 struct frame *f;
11029 {
11030 /* Normally, the window manager handles this function. */
11031 }
11032
11033 /* Make the x-window of frame F use the gnu icon bitmap. */
11034
11035 int
11036 x_bitmap_icon (f, file)
11037 struct frame *f;
11038 Lisp_Object file;
11039 {
11040 int bitmap_id;
11041
11042 if (FRAME_X_WINDOW (f) == 0)
11043 return 1;
11044
11045 /* Free up our existing icon bitmap if any. */
11046 if (f->output_data.x->icon_bitmap > 0)
11047 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11048 f->output_data.x->icon_bitmap = 0;
11049
11050 if (STRINGP (file))
11051 bitmap_id = x_create_bitmap_from_file (f, file);
11052 else
11053 {
11054 /* Create the GNU bitmap if necessary. */
11055 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11056 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11057 = x_create_bitmap_from_data (f, gnu_bits,
11058 gnu_width, gnu_height);
11059
11060 /* The first time we create the GNU bitmap,
11061 this increments the ref-count one extra time.
11062 As a result, the GNU bitmap is never freed.
11063 That way, we don't have to worry about allocating it again. */
11064 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11065
11066 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11067 }
11068
11069 x_wm_set_icon_pixmap (f, bitmap_id);
11070 f->output_data.x->icon_bitmap = bitmap_id;
11071
11072 return 0;
11073 }
11074
11075
11076 /* Make the x-window of frame F use a rectangle with text.
11077 Use ICON_NAME as the text. */
11078
11079 int
11080 x_text_icon (f, icon_name)
11081 struct frame *f;
11082 char *icon_name;
11083 {
11084 if (FRAME_X_WINDOW (f) == 0)
11085 return 1;
11086
11087 #ifdef HAVE_X11R4
11088 {
11089 XTextProperty text;
11090 text.value = (unsigned char *) icon_name;
11091 text.encoding = XA_STRING;
11092 text.format = 8;
11093 text.nitems = strlen (icon_name);
11094 #ifdef USE_X_TOOLKIT
11095 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11096 &text);
11097 #else /* not USE_X_TOOLKIT */
11098 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11099 #endif /* not USE_X_TOOLKIT */
11100 }
11101 #else /* not HAVE_X11R4 */
11102 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11103 #endif /* not HAVE_X11R4 */
11104
11105 if (f->output_data.x->icon_bitmap > 0)
11106 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11107 f->output_data.x->icon_bitmap = 0;
11108 x_wm_set_icon_pixmap (f, 0);
11109
11110 return 0;
11111 }
11112 \f
11113 #define X_ERROR_MESSAGE_SIZE 200
11114
11115 /* If non-nil, this should be a string.
11116 It means catch X errors and store the error message in this string. */
11117
11118 static Lisp_Object x_error_message_string;
11119
11120 /* An X error handler which stores the error message in
11121 x_error_message_string. This is called from x_error_handler if
11122 x_catch_errors is in effect. */
11123
11124 static void
11125 x_error_catcher (display, error)
11126 Display *display;
11127 XErrorEvent *error;
11128 {
11129 XGetErrorText (display, error->error_code,
11130 XSTRING (x_error_message_string)->data,
11131 X_ERROR_MESSAGE_SIZE);
11132 }
11133
11134 /* Begin trapping X errors for display DPY. Actually we trap X errors
11135 for all displays, but DPY should be the display you are actually
11136 operating on.
11137
11138 After calling this function, X protocol errors no longer cause
11139 Emacs to exit; instead, they are recorded in the string
11140 stored in x_error_message_string.
11141
11142 Calling x_check_errors signals an Emacs error if an X error has
11143 occurred since the last call to x_catch_errors or x_check_errors.
11144
11145 Calling x_uncatch_errors resumes the normal error handling. */
11146
11147 void x_check_errors ();
11148 static Lisp_Object x_catch_errors_unwind ();
11149
11150 int
11151 x_catch_errors (dpy)
11152 Display *dpy;
11153 {
11154 int count = specpdl_ptr - specpdl;
11155
11156 /* Make sure any errors from previous requests have been dealt with. */
11157 XSync (dpy, False);
11158
11159 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11160
11161 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11162 XSTRING (x_error_message_string)->data[0] = 0;
11163
11164 return count;
11165 }
11166
11167 /* Unbind the binding that we made to check for X errors. */
11168
11169 static Lisp_Object
11170 x_catch_errors_unwind (old_val)
11171 Lisp_Object old_val;
11172 {
11173 x_error_message_string = old_val;
11174 return Qnil;
11175 }
11176
11177 /* If any X protocol errors have arrived since the last call to
11178 x_catch_errors or x_check_errors, signal an Emacs error using
11179 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11180
11181 void
11182 x_check_errors (dpy, format)
11183 Display *dpy;
11184 char *format;
11185 {
11186 /* Make sure to catch any errors incurred so far. */
11187 XSync (dpy, False);
11188
11189 if (XSTRING (x_error_message_string)->data[0])
11190 error (format, XSTRING (x_error_message_string)->data);
11191 }
11192
11193 /* Nonzero if we had any X protocol errors
11194 since we did x_catch_errors on DPY. */
11195
11196 int
11197 x_had_errors_p (dpy)
11198 Display *dpy;
11199 {
11200 /* Make sure to catch any errors incurred so far. */
11201 XSync (dpy, False);
11202
11203 return XSTRING (x_error_message_string)->data[0] != 0;
11204 }
11205
11206 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11207
11208 void
11209 x_clear_errors (dpy)
11210 Display *dpy;
11211 {
11212 XSTRING (x_error_message_string)->data[0] = 0;
11213 }
11214
11215 /* Stop catching X protocol errors and let them make Emacs die.
11216 DPY should be the display that was passed to x_catch_errors.
11217 COUNT should be the value that was returned by
11218 the corresponding call to x_catch_errors. */
11219
11220 void
11221 x_uncatch_errors (dpy, count)
11222 Display *dpy;
11223 int count;
11224 {
11225 unbind_to (count, Qnil);
11226 }
11227
11228 #if 0
11229 static unsigned int x_wire_count;
11230 x_trace_wire ()
11231 {
11232 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11233 }
11234 #endif /* ! 0 */
11235
11236 \f
11237 /* Handle SIGPIPE, which can happen when the connection to a server
11238 simply goes away. SIGPIPE is handled by x_connection_signal.
11239 Don't need to do anything, because the write which caused the
11240 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11241 which will do the appropriate cleanup for us. */
11242
11243 static SIGTYPE
11244 x_connection_signal (signalnum) /* If we don't have an argument, */
11245 int signalnum; /* some compilers complain in signal calls. */
11246 {
11247 #ifdef USG
11248 /* USG systems forget handlers when they are used;
11249 must reestablish each time */
11250 signal (signalnum, x_connection_signal);
11251 #endif /* USG */
11252 }
11253 \f
11254 /* Handling X errors. */
11255
11256 /* Handle the loss of connection to display DISPLAY. */
11257
11258 static SIGTYPE
11259 x_connection_closed (dpy, error_message)
11260 Display *dpy;
11261 char *error_message;
11262 {
11263 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11264 Lisp_Object frame, tail;
11265
11266 /* We have to close the display to inform Xt that it doesn't
11267 exist anymore. If we don't, Xt will continue to wait for
11268 events from the display. As a consequence, a sequence of
11269
11270 M-x make-frame-on-display RET :1 RET
11271 ...kill the new frame, so that we get an IO error...
11272 M-x make-frame-on-display RET :1 RET
11273
11274 will indefinitely wait in Xt for events for display `:1', opened
11275 in the first class to make-frame-on-display.
11276
11277 Closing the display is reported to lead to a bus error on
11278 OpenWindows in certain situations. I suspect that is a bug
11279 in OpenWindows. I don't know how to cicumvent it here. */
11280
11281 #ifdef USE_X_TOOLKIT
11282 {
11283 /* Prevent being called recursively because of an error condition
11284 in XtCloseDisplay. Otherwise, we might end up with printing
11285 ``can't find per display information'' in the recursive call
11286 instead of printing the original message here. */
11287 int count = x_catch_errors (dpy);
11288 XtCloseDisplay (dpy);
11289 x_uncatch_errors (dpy, count);
11290 }
11291 #endif
11292
11293 /* Indicate that this display is dead. */
11294 if (dpyinfo)
11295 dpyinfo->display = 0;
11296
11297 /* First delete frames whose mini-buffers are on frames
11298 that are on the dead display. */
11299 FOR_EACH_FRAME (tail, frame)
11300 {
11301 Lisp_Object minibuf_frame;
11302 minibuf_frame
11303 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11304 if (FRAME_X_P (XFRAME (frame))
11305 && FRAME_X_P (XFRAME (minibuf_frame))
11306 && ! EQ (frame, minibuf_frame)
11307 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11308 Fdelete_frame (frame, Qt);
11309 }
11310
11311 /* Now delete all remaining frames on the dead display.
11312 We are now sure none of these is used as the mini-buffer
11313 for another frame that we need to delete. */
11314 FOR_EACH_FRAME (tail, frame)
11315 if (FRAME_X_P (XFRAME (frame))
11316 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11317 {
11318 /* Set this to t so that Fdelete_frame won't get confused
11319 trying to find a replacement. */
11320 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11321 Fdelete_frame (frame, Qt);
11322 }
11323
11324 if (dpyinfo)
11325 x_delete_display (dpyinfo);
11326
11327 if (x_display_list == 0)
11328 {
11329 fprintf (stderr, "%s\n", error_message);
11330 shut_down_emacs (0, 0, Qnil);
11331 exit (70);
11332 }
11333
11334 /* Ordinary stack unwind doesn't deal with these. */
11335 #ifdef SIGIO
11336 sigunblock (sigmask (SIGIO));
11337 #endif
11338 sigunblock (sigmask (SIGALRM));
11339 TOTALLY_UNBLOCK_INPUT;
11340
11341 clear_waiting_for_input ();
11342 handling_signal = 0;
11343 error ("%s", error_message);
11344 }
11345
11346 /* This is the usual handler for X protocol errors.
11347 It kills all frames on the display that we got the error for.
11348 If that was the only one, it prints an error message and kills Emacs. */
11349
11350 static void
11351 x_error_quitter (display, error)
11352 Display *display;
11353 XErrorEvent *error;
11354 {
11355 char buf[256], buf1[356];
11356
11357 /* Note that there is no real way portable across R3/R4 to get the
11358 original error handler. */
11359
11360 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11361 sprintf (buf1, "X protocol error: %s on protocol request %d",
11362 buf, error->request_code);
11363 x_connection_closed (display, buf1);
11364 }
11365
11366 /* This is the first-level handler for X protocol errors.
11367 It calls x_error_quitter or x_error_catcher. */
11368
11369 static int
11370 x_error_handler (display, error)
11371 Display *display;
11372 XErrorEvent *error;
11373 {
11374 if (! NILP (x_error_message_string))
11375 x_error_catcher (display, error);
11376 else
11377 x_error_quitter (display, error);
11378 return 0;
11379 }
11380
11381 /* This is the handler for X IO errors, always.
11382 It kills all frames on the display that we lost touch with.
11383 If that was the only one, it prints an error message and kills Emacs. */
11384
11385 static int
11386 x_io_error_quitter (display)
11387 Display *display;
11388 {
11389 char buf[256];
11390
11391 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11392 x_connection_closed (display, buf);
11393 return 0;
11394 }
11395 \f
11396 /* Changing the font of the frame. */
11397
11398 /* Give frame F the font named FONTNAME as its default font, and
11399 return the full name of that font. FONTNAME may be a wildcard
11400 pattern; in that case, we choose some font that fits the pattern.
11401 The return value shows which font we chose. */
11402
11403 Lisp_Object
11404 x_new_font (f, fontname)
11405 struct frame *f;
11406 register char *fontname;
11407 {
11408 struct font_info *fontp
11409 = FS_LOAD_FONT (f, 0, fontname, -1);
11410
11411 if (!fontp)
11412 return Qnil;
11413
11414 f->output_data.x->font = (XFontStruct *) (fontp->font);
11415 f->output_data.x->baseline_offset = fontp->baseline_offset;
11416 f->output_data.x->fontset = -1;
11417
11418 /* Compute the scroll bar width in character columns. */
11419 if (f->scroll_bar_pixel_width > 0)
11420 {
11421 int wid = FONT_WIDTH (f->output_data.x->font);
11422 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11423 }
11424 else
11425 {
11426 int wid = FONT_WIDTH (f->output_data.x->font);
11427 f->scroll_bar_cols = (14 + wid - 1) / wid;
11428 }
11429
11430 /* Now make the frame display the given font. */
11431 if (FRAME_X_WINDOW (f) != 0)
11432 {
11433 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11434 f->output_data.x->font->fid);
11435 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11436 f->output_data.x->font->fid);
11437 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11438 f->output_data.x->font->fid);
11439
11440 frame_update_line_height (f);
11441 x_set_window_size (f, 0, f->width, f->height);
11442 }
11443 else
11444 /* If we are setting a new frame's font for the first time,
11445 there are no faces yet, so this font's height is the line height. */
11446 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11447
11448 return build_string (fontp->full_name);
11449 }
11450
11451 /* Give frame F the fontset named FONTSETNAME as its default font, and
11452 return the full name of that fontset. FONTSETNAME may be a wildcard
11453 pattern; in that case, we choose some fontset that fits the pattern.
11454 The return value shows which fontset we chose. */
11455
11456 Lisp_Object
11457 x_new_fontset (f, fontsetname)
11458 struct frame *f;
11459 char *fontsetname;
11460 {
11461 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11462 Lisp_Object result;
11463
11464 if (fontset < 0)
11465 return Qnil;
11466
11467 if (f->output_data.x->fontset == fontset)
11468 /* This fontset is already set in frame F. There's nothing more
11469 to do. */
11470 return fontset_name (fontset);
11471
11472 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11473
11474 if (!STRINGP (result))
11475 /* Can't load ASCII font. */
11476 return Qnil;
11477
11478 /* Since x_new_font doesn't update any fontset information, do it now. */
11479 f->output_data.x->fontset = fontset;
11480
11481 #ifdef HAVE_X_I18N
11482 if (FRAME_XIC (f)
11483 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11484 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11485 #endif
11486
11487 return build_string (fontsetname);
11488 }
11489
11490 \f
11491 /***********************************************************************
11492 X Input Methods
11493 ***********************************************************************/
11494
11495 #ifdef HAVE_X_I18N
11496
11497 #ifdef HAVE_X11R6
11498
11499 /* XIM destroy callback function, which is called whenever the
11500 connection to input method XIM dies. CLIENT_DATA contains a
11501 pointer to the x_display_info structure corresponding to XIM. */
11502
11503 static void
11504 xim_destroy_callback (xim, client_data, call_data)
11505 XIM xim;
11506 XPointer client_data;
11507 XPointer call_data;
11508 {
11509 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11510 Lisp_Object frame, tail;
11511
11512 BLOCK_INPUT;
11513
11514 /* No need to call XDestroyIC.. */
11515 FOR_EACH_FRAME (tail, frame)
11516 {
11517 struct frame *f = XFRAME (frame);
11518 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11519 {
11520 FRAME_XIC (f) = NULL;
11521 if (FRAME_XIC_FONTSET (f))
11522 {
11523 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11524 FRAME_XIC_FONTSET (f) = NULL;
11525 }
11526 }
11527 }
11528
11529 /* No need to call XCloseIM. */
11530 dpyinfo->xim = NULL;
11531 XFree (dpyinfo->xim_styles);
11532 UNBLOCK_INPUT;
11533 }
11534
11535 #endif /* HAVE_X11R6 */
11536
11537 /* Open the connection to the XIM server on display DPYINFO.
11538 RESOURCE_NAME is the resource name Emacs uses. */
11539
11540 static void
11541 xim_open_dpy (dpyinfo, resource_name)
11542 struct x_display_info *dpyinfo;
11543 char *resource_name;
11544 {
11545 #ifdef USE_XIM
11546 XIM xim;
11547
11548 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11549 dpyinfo->xim = xim;
11550
11551 if (xim)
11552 {
11553 #ifdef HAVE_X11R6
11554 XIMCallback destroy;
11555 #endif
11556
11557 /* Get supported styles and XIM values. */
11558 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11559
11560 #ifdef HAVE_X11R6
11561 destroy.callback = xim_destroy_callback;
11562 destroy.client_data = (XPointer)dpyinfo;
11563 /* This isn't prptotyped in OSF 5.0. */
11564 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11565 #endif
11566 }
11567
11568 #else /* not USE_XIM */
11569 dpyinfo->xim = NULL;
11570 #endif /* not USE_XIM */
11571 }
11572
11573
11574 #ifdef HAVE_X11R6_XIM
11575
11576 struct xim_inst_t
11577 {
11578 struct x_display_info *dpyinfo;
11579 char *resource_name;
11580 };
11581
11582 /* XIM instantiate callback function, which is called whenever an XIM
11583 server is available. DISPLAY is teh display of the XIM.
11584 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11585 when the callback was registered. */
11586
11587 static void
11588 xim_instantiate_callback (display, client_data, call_data)
11589 Display *display;
11590 XPointer client_data;
11591 XPointer call_data;
11592 {
11593 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11594 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11595
11596 /* We don't support multiple XIM connections. */
11597 if (dpyinfo->xim)
11598 return;
11599
11600 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11601
11602 /* Create XIC for the existing frames on the same display, as long
11603 as they have no XIC. */
11604 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11605 {
11606 Lisp_Object tail, frame;
11607
11608 BLOCK_INPUT;
11609 FOR_EACH_FRAME (tail, frame)
11610 {
11611 struct frame *f = XFRAME (frame);
11612
11613 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11614 if (FRAME_XIC (f) == NULL)
11615 {
11616 create_frame_xic (f);
11617 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11618 xic_set_statusarea (f);
11619 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11620 {
11621 struct window *w = XWINDOW (f->selected_window);
11622 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11623 }
11624 }
11625 }
11626
11627 UNBLOCK_INPUT;
11628 }
11629 }
11630
11631 #endif /* HAVE_X11R6_XIM */
11632
11633
11634 /* Open a connection to the XIM server on display DPYINFO.
11635 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11636 connection only at the first time. On X11R6, open the connection
11637 in the XIM instantiate callback function. */
11638
11639 static void
11640 xim_initialize (dpyinfo, resource_name)
11641 struct x_display_info *dpyinfo;
11642 char *resource_name;
11643 {
11644 #ifdef USE_XIM
11645 #ifdef HAVE_X11R6_XIM
11646 struct xim_inst_t *xim_inst;
11647 int len;
11648
11649 dpyinfo->xim = NULL;
11650 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11651 xim_inst->dpyinfo = dpyinfo;
11652 len = strlen (resource_name);
11653 xim_inst->resource_name = (char *) xmalloc (len + 1);
11654 bcopy (resource_name, xim_inst->resource_name, len + 1);
11655 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11656 resource_name, EMACS_CLASS,
11657 xim_instantiate_callback,
11658 /* Fixme: This is XPointer in
11659 XFree86 but (XPointer *) on
11660 Tru64, at least. */
11661 (XPointer) xim_inst);
11662 #else /* not HAVE_X11R6_XIM */
11663 dpyinfo->xim = NULL;
11664 xim_open_dpy (dpyinfo, resource_name);
11665 #endif /* not HAVE_X11R6_XIM */
11666
11667 #else /* not USE_XIM */
11668 dpyinfo->xim = NULL;
11669 #endif /* not USE_XIM */
11670 }
11671
11672
11673 /* Close the connection to the XIM server on display DPYINFO. */
11674
11675 static void
11676 xim_close_dpy (dpyinfo)
11677 struct x_display_info *dpyinfo;
11678 {
11679 #ifdef USE_XIM
11680 #ifdef HAVE_X11R6_XIM
11681 if (dpyinfo->display)
11682 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11683 NULL, EMACS_CLASS,
11684 xim_instantiate_callback, NULL);
11685 #endif /* not HAVE_X11R6_XIM */
11686 if (dpyinfo->display)
11687 XCloseIM (dpyinfo->xim);
11688 dpyinfo->xim = NULL;
11689 XFree (dpyinfo->xim_styles);
11690 #endif /* USE_XIM */
11691 }
11692
11693 #endif /* not HAVE_X11R6_XIM */
11694
11695
11696 \f
11697 /* Calculate the absolute position in frame F
11698 from its current recorded position values and gravity. */
11699
11700 void
11701 x_calc_absolute_position (f)
11702 struct frame *f;
11703 {
11704 Window child;
11705 int win_x = 0, win_y = 0;
11706 int flags = f->output_data.x->size_hint_flags;
11707 int this_window;
11708
11709 /* We have nothing to do if the current position
11710 is already for the top-left corner. */
11711 if (! ((flags & XNegative) || (flags & YNegative)))
11712 return;
11713
11714 #ifdef USE_X_TOOLKIT
11715 this_window = XtWindow (f->output_data.x->widget);
11716 #else
11717 this_window = FRAME_X_WINDOW (f);
11718 #endif
11719
11720 /* Find the position of the outside upper-left corner of
11721 the inner window, with respect to the outer window.
11722 But do this only if we will need the results. */
11723 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11724 {
11725 int count;
11726
11727 BLOCK_INPUT;
11728 count = x_catch_errors (FRAME_X_DISPLAY (f));
11729 while (1)
11730 {
11731 x_clear_errors (FRAME_X_DISPLAY (f));
11732 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11733
11734 /* From-window, to-window. */
11735 this_window,
11736 f->output_data.x->parent_desc,
11737
11738 /* From-position, to-position. */
11739 0, 0, &win_x, &win_y,
11740
11741 /* Child of win. */
11742 &child);
11743 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11744 {
11745 Window newroot, newparent = 0xdeadbeef;
11746 Window *newchildren;
11747 unsigned int nchildren;
11748
11749 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11750 &newparent, &newchildren, &nchildren))
11751 break;
11752
11753 XFree ((char *) newchildren);
11754
11755 f->output_data.x->parent_desc = newparent;
11756 }
11757 else
11758 break;
11759 }
11760
11761 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11762 UNBLOCK_INPUT;
11763 }
11764
11765 /* Treat negative positions as relative to the leftmost bottommost
11766 position that fits on the screen. */
11767 if (flags & XNegative)
11768 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11769 - 2 * f->output_data.x->border_width - win_x
11770 - PIXEL_WIDTH (f)
11771 + f->output_data.x->left_pos);
11772
11773 {
11774 int height = PIXEL_HEIGHT (f);
11775
11776 #if defined USE_X_TOOLKIT && defined USE_MOTIF
11777 /* Something is fishy here. When using Motif, starting Emacs with
11778 `-g -0-0', the frame appears too low by a few pixels.
11779
11780 This seems to be so because initially, while Emacs is starting,
11781 the column widget's height and the frame's pixel height are
11782 different. The column widget's height is the right one. In
11783 later invocations, when Emacs is up, the frame's pixel height
11784 is right, though.
11785
11786 It's not obvious where the initial small difference comes from.
11787 2000-12-01, gerd. */
11788
11789 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
11790 #endif
11791
11792 if (flags & YNegative)
11793 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11794 - 2 * f->output_data.x->border_width
11795 - win_y
11796 - height
11797 + f->output_data.x->top_pos);
11798 }
11799
11800 /* The left_pos and top_pos
11801 are now relative to the top and left screen edges,
11802 so the flags should correspond. */
11803 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11804 }
11805
11806 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11807 to really change the position, and 0 when calling from
11808 x_make_frame_visible (in that case, XOFF and YOFF are the current
11809 position values). It is -1 when calling from x_set_frame_parameters,
11810 which means, do adjust for borders but don't change the gravity. */
11811
11812 void
11813 x_set_offset (f, xoff, yoff, change_gravity)
11814 struct frame *f;
11815 register int xoff, yoff;
11816 int change_gravity;
11817 {
11818 int modified_top, modified_left;
11819
11820 if (change_gravity > 0)
11821 {
11822 f->output_data.x->top_pos = yoff;
11823 f->output_data.x->left_pos = xoff;
11824 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11825 if (xoff < 0)
11826 f->output_data.x->size_hint_flags |= XNegative;
11827 if (yoff < 0)
11828 f->output_data.x->size_hint_flags |= YNegative;
11829 f->output_data.x->win_gravity = NorthWestGravity;
11830 }
11831 x_calc_absolute_position (f);
11832
11833 BLOCK_INPUT;
11834 x_wm_set_size_hint (f, (long) 0, 0);
11835
11836 modified_left = f->output_data.x->left_pos;
11837 modified_top = f->output_data.x->top_pos;
11838 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11839 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11840 /* It is a mystery why we need to add the border_width here
11841 when the frame is already visible, but experiment says we do. */
11842 if (change_gravity != 0)
11843 {
11844 modified_left += f->output_data.x->border_width;
11845 modified_top += f->output_data.x->border_width;
11846 }
11847 #endif
11848
11849 #ifdef USE_X_TOOLKIT
11850 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11851 modified_left, modified_top);
11852 #else /* not USE_X_TOOLKIT */
11853 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11854 modified_left, modified_top);
11855 #endif /* not USE_X_TOOLKIT */
11856 UNBLOCK_INPUT;
11857 }
11858
11859 /* Call this to change the size of frame F's x-window.
11860 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11861 for this size change and subsequent size changes.
11862 Otherwise we leave the window gravity unchanged. */
11863
11864 void
11865 x_set_window_size (f, change_gravity, cols, rows)
11866 struct frame *f;
11867 int change_gravity;
11868 int cols, rows;
11869 {
11870 #ifndef USE_X_TOOLKIT
11871 int pixelwidth, pixelheight;
11872 #endif
11873
11874 BLOCK_INPUT;
11875
11876 #ifdef USE_X_TOOLKIT
11877 {
11878 /* The x and y position of the widget is clobbered by the
11879 call to XtSetValues within EmacsFrameSetCharSize.
11880 This is a real kludge, but I don't understand Xt so I can't
11881 figure out a correct fix. Can anyone else tell me? -- rms. */
11882 int xpos = f->output_data.x->widget->core.x;
11883 int ypos = f->output_data.x->widget->core.y;
11884 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11885 f->output_data.x->widget->core.x = xpos;
11886 f->output_data.x->widget->core.y = ypos;
11887 }
11888
11889 #else /* not USE_X_TOOLKIT */
11890
11891 check_frame_size (f, &rows, &cols);
11892 f->output_data.x->vertical_scroll_bar_extra
11893 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11894 ? 0
11895 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11896 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11897 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11898 f->output_data.x->flags_areas_extra
11899 = FRAME_FLAGS_AREA_WIDTH (f);
11900 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11901 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11902
11903 f->output_data.x->win_gravity = NorthWestGravity;
11904 x_wm_set_size_hint (f, (long) 0, 0);
11905
11906 XSync (FRAME_X_DISPLAY (f), False);
11907 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11908 pixelwidth, pixelheight);
11909
11910 /* Now, strictly speaking, we can't be sure that this is accurate,
11911 but the window manager will get around to dealing with the size
11912 change request eventually, and we'll hear how it went when the
11913 ConfigureNotify event gets here.
11914
11915 We could just not bother storing any of this information here,
11916 and let the ConfigureNotify event set everything up, but that
11917 might be kind of confusing to the Lisp code, since size changes
11918 wouldn't be reported in the frame parameters until some random
11919 point in the future when the ConfigureNotify event arrives.
11920
11921 We pass 1 for DELAY since we can't run Lisp code inside of
11922 a BLOCK_INPUT. */
11923 change_frame_size (f, rows, cols, 0, 1, 0);
11924 PIXEL_WIDTH (f) = pixelwidth;
11925 PIXEL_HEIGHT (f) = pixelheight;
11926
11927 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11928 receive in the ConfigureNotify event; if we get what we asked
11929 for, then the event won't cause the screen to become garbaged, so
11930 we have to make sure to do it here. */
11931 SET_FRAME_GARBAGED (f);
11932
11933 XFlush (FRAME_X_DISPLAY (f));
11934
11935 #endif /* not USE_X_TOOLKIT */
11936
11937 /* If cursor was outside the new size, mark it as off. */
11938 mark_window_cursors_off (XWINDOW (f->root_window));
11939
11940 /* Clear out any recollection of where the mouse highlighting was,
11941 since it might be in a place that's outside the new frame size.
11942 Actually checking whether it is outside is a pain in the neck,
11943 so don't try--just let the highlighting be done afresh with new size. */
11944 cancel_mouse_face (f);
11945
11946 UNBLOCK_INPUT;
11947 }
11948 \f
11949 /* Mouse warping. */
11950
11951 void
11952 x_set_mouse_position (f, x, y)
11953 struct frame *f;
11954 int x, y;
11955 {
11956 int pix_x, pix_y;
11957
11958 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11959 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11960
11961 if (pix_x < 0) pix_x = 0;
11962 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11963
11964 if (pix_y < 0) pix_y = 0;
11965 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11966
11967 BLOCK_INPUT;
11968
11969 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11970 0, 0, 0, 0, pix_x, pix_y);
11971 UNBLOCK_INPUT;
11972 }
11973
11974 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11975
11976 void
11977 x_set_mouse_pixel_position (f, pix_x, pix_y)
11978 struct frame *f;
11979 int pix_x, pix_y;
11980 {
11981 BLOCK_INPUT;
11982
11983 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11984 0, 0, 0, 0, pix_x, pix_y);
11985 UNBLOCK_INPUT;
11986 }
11987 \f
11988 /* focus shifting, raising and lowering. */
11989
11990 void
11991 x_focus_on_frame (f)
11992 struct frame *f;
11993 {
11994 #if 0 /* This proves to be unpleasant. */
11995 x_raise_frame (f);
11996 #endif
11997 #if 0
11998 /* I don't think that the ICCCM allows programs to do things like this
11999 without the interaction of the window manager. Whatever you end up
12000 doing with this code, do it to x_unfocus_frame too. */
12001 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12002 RevertToPointerRoot, CurrentTime);
12003 #endif /* ! 0 */
12004 }
12005
12006 void
12007 x_unfocus_frame (f)
12008 struct frame *f;
12009 {
12010 #if 0
12011 /* Look at the remarks in x_focus_on_frame. */
12012 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12013 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12014 RevertToPointerRoot, CurrentTime);
12015 #endif /* ! 0 */
12016 }
12017
12018 /* Raise frame F. */
12019
12020 void
12021 x_raise_frame (f)
12022 struct frame *f;
12023 {
12024 if (f->async_visible)
12025 {
12026 BLOCK_INPUT;
12027 #ifdef USE_X_TOOLKIT
12028 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12029 #else /* not USE_X_TOOLKIT */
12030 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12031 #endif /* not USE_X_TOOLKIT */
12032 XFlush (FRAME_X_DISPLAY (f));
12033 UNBLOCK_INPUT;
12034 }
12035 }
12036
12037 /* Lower frame F. */
12038
12039 void
12040 x_lower_frame (f)
12041 struct frame *f;
12042 {
12043 if (f->async_visible)
12044 {
12045 BLOCK_INPUT;
12046 #ifdef USE_X_TOOLKIT
12047 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12048 #else /* not USE_X_TOOLKIT */
12049 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12050 #endif /* not USE_X_TOOLKIT */
12051 XFlush (FRAME_X_DISPLAY (f));
12052 UNBLOCK_INPUT;
12053 }
12054 }
12055
12056 static void
12057 XTframe_raise_lower (f, raise_flag)
12058 FRAME_PTR f;
12059 int raise_flag;
12060 {
12061 if (raise_flag)
12062 x_raise_frame (f);
12063 else
12064 x_lower_frame (f);
12065 }
12066 \f
12067 /* Change of visibility. */
12068
12069 /* This tries to wait until the frame is really visible.
12070 However, if the window manager asks the user where to position
12071 the frame, this will return before the user finishes doing that.
12072 The frame will not actually be visible at that time,
12073 but it will become visible later when the window manager
12074 finishes with it. */
12075
12076 void
12077 x_make_frame_visible (f)
12078 struct frame *f;
12079 {
12080 Lisp_Object type;
12081 int original_top, original_left;
12082 int retry_count = 2;
12083
12084 retry:
12085
12086 BLOCK_INPUT;
12087
12088 type = x_icon_type (f);
12089 if (!NILP (type))
12090 x_bitmap_icon (f, type);
12091
12092 if (! FRAME_VISIBLE_P (f))
12093 {
12094 /* We test FRAME_GARBAGED_P here to make sure we don't
12095 call x_set_offset a second time
12096 if we get to x_make_frame_visible a second time
12097 before the window gets really visible. */
12098 if (! FRAME_ICONIFIED_P (f)
12099 && ! f->output_data.x->asked_for_visible)
12100 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12101
12102 f->output_data.x->asked_for_visible = 1;
12103
12104 if (! EQ (Vx_no_window_manager, Qt))
12105 x_wm_set_window_state (f, NormalState);
12106 #ifdef USE_X_TOOLKIT
12107 /* This was XtPopup, but that did nothing for an iconified frame. */
12108 XtMapWidget (f->output_data.x->widget);
12109 #else /* not USE_X_TOOLKIT */
12110 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12111 #endif /* not USE_X_TOOLKIT */
12112 #if 0 /* This seems to bring back scroll bars in the wrong places
12113 if the window configuration has changed. They seem
12114 to come back ok without this. */
12115 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12116 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12117 #endif
12118 }
12119
12120 XFlush (FRAME_X_DISPLAY (f));
12121
12122 /* Synchronize to ensure Emacs knows the frame is visible
12123 before we do anything else. We do this loop with input not blocked
12124 so that incoming events are handled. */
12125 {
12126 Lisp_Object frame;
12127 int count;
12128 /* This must be before UNBLOCK_INPUT
12129 since events that arrive in response to the actions above
12130 will set it when they are handled. */
12131 int previously_visible = f->output_data.x->has_been_visible;
12132
12133 original_left = f->output_data.x->left_pos;
12134 original_top = f->output_data.x->top_pos;
12135
12136 /* This must come after we set COUNT. */
12137 UNBLOCK_INPUT;
12138
12139 /* We unblock here so that arriving X events are processed. */
12140
12141 /* Now move the window back to where it was "supposed to be".
12142 But don't do it if the gravity is negative.
12143 When the gravity is negative, this uses a position
12144 that is 3 pixels too low. Perhaps that's really the border width.
12145
12146 Don't do this if the window has never been visible before,
12147 because the window manager may choose the position
12148 and we don't want to override it. */
12149
12150 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12151 && f->output_data.x->win_gravity == NorthWestGravity
12152 && previously_visible)
12153 {
12154 Drawable rootw;
12155 int x, y;
12156 unsigned int width, height, border, depth;
12157
12158 BLOCK_INPUT;
12159
12160 /* On some window managers (such as FVWM) moving an existing
12161 window, even to the same place, causes the window manager
12162 to introduce an offset. This can cause the window to move
12163 to an unexpected location. Check the geometry (a little
12164 slow here) and then verify that the window is in the right
12165 place. If the window is not in the right place, move it
12166 there, and take the potential window manager hit. */
12167 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12168 &rootw, &x, &y, &width, &height, &border, &depth);
12169
12170 if (original_left != x || original_top != y)
12171 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12172 original_left, original_top);
12173
12174 UNBLOCK_INPUT;
12175 }
12176
12177 XSETFRAME (frame, f);
12178
12179 /* Wait until the frame is visible. Process X events until a
12180 MapNotify event has been seen, or until we think we won't get a
12181 MapNotify at all.. */
12182 for (count = input_signal_count + 10;
12183 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12184 {
12185 /* Force processing of queued events. */
12186 x_sync (f);
12187
12188 /* Machines that do polling rather than SIGIO have been
12189 observed to go into a busy-wait here. So we'll fake an
12190 alarm signal to let the handler know that there's something
12191 to be read. We used to raise a real alarm, but it seems
12192 that the handler isn't always enabled here. This is
12193 probably a bug. */
12194 if (input_polling_used ())
12195 {
12196 /* It could be confusing if a real alarm arrives while
12197 processing the fake one. Turn it off and let the
12198 handler reset it. */
12199 extern void poll_for_input_1 P_ ((void));
12200 int old_poll_suppress_count = poll_suppress_count;
12201 poll_suppress_count = 1;
12202 poll_for_input_1 ();
12203 poll_suppress_count = old_poll_suppress_count;
12204 }
12205
12206 /* See if a MapNotify event has been processed. */
12207 FRAME_SAMPLE_VISIBILITY (f);
12208 }
12209
12210 /* 2000-09-28: In
12211
12212 (let ((f (selected-frame)))
12213 (iconify-frame f)
12214 (raise-frame f))
12215
12216 the frame is not raised with various window managers on
12217 FreeBSD, Linux and Solaris. It turns out that, for some
12218 unknown reason, the call to XtMapWidget is completely ignored.
12219 Mapping the widget a second time works. */
12220
12221 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12222 goto retry;
12223 }
12224 }
12225
12226 /* Change from mapped state to withdrawn state. */
12227
12228 /* Make the frame visible (mapped and not iconified). */
12229
12230 void
12231 x_make_frame_invisible (f)
12232 struct frame *f;
12233 {
12234 Window window;
12235
12236 #ifdef USE_X_TOOLKIT
12237 /* Use the frame's outermost window, not the one we normally draw on. */
12238 window = XtWindow (f->output_data.x->widget);
12239 #else /* not USE_X_TOOLKIT */
12240 window = FRAME_X_WINDOW (f);
12241 #endif /* not USE_X_TOOLKIT */
12242
12243 /* Don't keep the highlight on an invisible frame. */
12244 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12245 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12246
12247 #if 0/* This might add unreliability; I don't trust it -- rms. */
12248 if (! f->async_visible && ! f->async_iconified)
12249 return;
12250 #endif
12251
12252 BLOCK_INPUT;
12253
12254 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12255 that the current position of the window is user-specified, rather than
12256 program-specified, so that when the window is mapped again, it will be
12257 placed at the same location, without forcing the user to position it
12258 by hand again (they have already done that once for this window.) */
12259 x_wm_set_size_hint (f, (long) 0, 1);
12260
12261 #ifdef HAVE_X11R4
12262
12263 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12264 DefaultScreen (FRAME_X_DISPLAY (f))))
12265 {
12266 UNBLOCK_INPUT_RESIGNAL;
12267 error ("Can't notify window manager of window withdrawal");
12268 }
12269 #else /* ! defined (HAVE_X11R4) */
12270
12271 /* Tell the window manager what we're going to do. */
12272 if (! EQ (Vx_no_window_manager, Qt))
12273 {
12274 XEvent unmap;
12275
12276 unmap.xunmap.type = UnmapNotify;
12277 unmap.xunmap.window = window;
12278 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12279 unmap.xunmap.from_configure = False;
12280 if (! XSendEvent (FRAME_X_DISPLAY (f),
12281 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12282 False,
12283 SubstructureRedirectMaskSubstructureNotifyMask,
12284 &unmap))
12285 {
12286 UNBLOCK_INPUT_RESIGNAL;
12287 error ("Can't notify window manager of withdrawal");
12288 }
12289 }
12290
12291 /* Unmap the window ourselves. Cheeky! */
12292 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12293 #endif /* ! defined (HAVE_X11R4) */
12294
12295 /* We can't distinguish this from iconification
12296 just by the event that we get from the server.
12297 So we can't win using the usual strategy of letting
12298 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12299 and synchronize with the server to make sure we agree. */
12300 f->visible = 0;
12301 FRAME_ICONIFIED_P (f) = 0;
12302 f->async_visible = 0;
12303 f->async_iconified = 0;
12304
12305 x_sync (f);
12306
12307 UNBLOCK_INPUT;
12308 }
12309
12310 /* Change window state from mapped to iconified. */
12311
12312 void
12313 x_iconify_frame (f)
12314 struct frame *f;
12315 {
12316 int result;
12317 Lisp_Object type;
12318
12319 /* Don't keep the highlight on an invisible frame. */
12320 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12321 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12322
12323 if (f->async_iconified)
12324 return;
12325
12326 BLOCK_INPUT;
12327
12328 FRAME_SAMPLE_VISIBILITY (f);
12329
12330 type = x_icon_type (f);
12331 if (!NILP (type))
12332 x_bitmap_icon (f, type);
12333
12334 #ifdef USE_X_TOOLKIT
12335
12336 if (! FRAME_VISIBLE_P (f))
12337 {
12338 if (! EQ (Vx_no_window_manager, Qt))
12339 x_wm_set_window_state (f, IconicState);
12340 /* This was XtPopup, but that did nothing for an iconified frame. */
12341 XtMapWidget (f->output_data.x->widget);
12342 /* The server won't give us any event to indicate
12343 that an invisible frame was changed to an icon,
12344 so we have to record it here. */
12345 f->iconified = 1;
12346 f->visible = 1;
12347 f->async_iconified = 1;
12348 f->async_visible = 0;
12349 UNBLOCK_INPUT;
12350 return;
12351 }
12352
12353 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12354 XtWindow (f->output_data.x->widget),
12355 DefaultScreen (FRAME_X_DISPLAY (f)));
12356 UNBLOCK_INPUT;
12357
12358 if (!result)
12359 error ("Can't notify window manager of iconification");
12360
12361 f->async_iconified = 1;
12362 f->async_visible = 0;
12363
12364
12365 BLOCK_INPUT;
12366 XFlush (FRAME_X_DISPLAY (f));
12367 UNBLOCK_INPUT;
12368 #else /* not USE_X_TOOLKIT */
12369
12370 /* Make sure the X server knows where the window should be positioned,
12371 in case the user deiconifies with the window manager. */
12372 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12373 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12374
12375 /* Since we don't know which revision of X we're running, we'll use both
12376 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12377
12378 /* X11R4: send a ClientMessage to the window manager using the
12379 WM_CHANGE_STATE type. */
12380 {
12381 XEvent message;
12382
12383 message.xclient.window = FRAME_X_WINDOW (f);
12384 message.xclient.type = ClientMessage;
12385 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12386 message.xclient.format = 32;
12387 message.xclient.data.l[0] = IconicState;
12388
12389 if (! XSendEvent (FRAME_X_DISPLAY (f),
12390 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12391 False,
12392 SubstructureRedirectMask | SubstructureNotifyMask,
12393 &message))
12394 {
12395 UNBLOCK_INPUT_RESIGNAL;
12396 error ("Can't notify window manager of iconification");
12397 }
12398 }
12399
12400 /* X11R3: set the initial_state field of the window manager hints to
12401 IconicState. */
12402 x_wm_set_window_state (f, IconicState);
12403
12404 if (!FRAME_VISIBLE_P (f))
12405 {
12406 /* If the frame was withdrawn, before, we must map it. */
12407 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12408 }
12409
12410 f->async_iconified = 1;
12411 f->async_visible = 0;
12412
12413 XFlush (FRAME_X_DISPLAY (f));
12414 UNBLOCK_INPUT;
12415 #endif /* not USE_X_TOOLKIT */
12416 }
12417
12418 \f
12419 /* Free X resources of frame F. */
12420
12421 void
12422 x_free_frame_resources (f)
12423 struct frame *f;
12424 {
12425 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12426
12427 BLOCK_INPUT;
12428
12429 /* If a display connection is dead, don't try sending more
12430 commands to the X server. */
12431 if (dpyinfo->display)
12432 {
12433 if (f->output_data.x->icon_desc)
12434 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12435
12436 #ifdef HAVE_X_I18N
12437 if (FRAME_XIC (f))
12438 free_frame_xic (f);
12439 #endif
12440
12441 if (FRAME_X_WINDOW (f))
12442 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12443
12444 #ifdef USE_X_TOOLKIT
12445 if (f->output_data.x->widget)
12446 XtDestroyWidget (f->output_data.x->widget);
12447 free_frame_menubar (f);
12448 #endif /* USE_X_TOOLKIT */
12449
12450 unload_color (f, f->output_data.x->foreground_pixel);
12451 unload_color (f, f->output_data.x->background_pixel);
12452 unload_color (f, f->output_data.x->cursor_pixel);
12453 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12454 unload_color (f, f->output_data.x->border_pixel);
12455 unload_color (f, f->output_data.x->mouse_pixel);
12456
12457 if (f->output_data.x->scroll_bar_background_pixel != -1)
12458 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12459 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12460 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12461 if (f->output_data.x->white_relief.allocated_p)
12462 unload_color (f, f->output_data.x->white_relief.pixel);
12463 if (f->output_data.x->black_relief.allocated_p)
12464 unload_color (f, f->output_data.x->black_relief.pixel);
12465
12466 if (FRAME_FACE_CACHE (f))
12467 free_frame_faces (f);
12468
12469 x_free_gcs (f);
12470 XFlush (FRAME_X_DISPLAY (f));
12471 }
12472
12473 if (f->output_data.x->saved_menu_event)
12474 xfree (f->output_data.x->saved_menu_event);
12475
12476 xfree (f->output_data.x);
12477 f->output_data.x = NULL;
12478
12479 if (f == dpyinfo->x_focus_frame)
12480 dpyinfo->x_focus_frame = 0;
12481 if (f == dpyinfo->x_focus_event_frame)
12482 dpyinfo->x_focus_event_frame = 0;
12483 if (f == dpyinfo->x_highlight_frame)
12484 dpyinfo->x_highlight_frame = 0;
12485
12486 if (f == dpyinfo->mouse_face_mouse_frame)
12487 {
12488 dpyinfo->mouse_face_beg_row
12489 = dpyinfo->mouse_face_beg_col = -1;
12490 dpyinfo->mouse_face_end_row
12491 = dpyinfo->mouse_face_end_col = -1;
12492 dpyinfo->mouse_face_window = Qnil;
12493 dpyinfo->mouse_face_deferred_gc = 0;
12494 dpyinfo->mouse_face_mouse_frame = 0;
12495 }
12496
12497 UNBLOCK_INPUT;
12498 }
12499
12500
12501 /* Destroy the X window of frame F. */
12502
12503 void
12504 x_destroy_window (f)
12505 struct frame *f;
12506 {
12507 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12508
12509 /* If a display connection is dead, don't try sending more
12510 commands to the X server. */
12511 if (dpyinfo->display != 0)
12512 x_free_frame_resources (f);
12513
12514 dpyinfo->reference_count--;
12515 }
12516
12517 \f
12518 /* Setting window manager hints. */
12519
12520 /* Set the normal size hints for the window manager, for frame F.
12521 FLAGS is the flags word to use--or 0 meaning preserve the flags
12522 that the window now has.
12523 If USER_POSITION is nonzero, we set the USPosition
12524 flag (this is useful when FLAGS is 0). */
12525
12526 void
12527 x_wm_set_size_hint (f, flags, user_position)
12528 struct frame *f;
12529 long flags;
12530 int user_position;
12531 {
12532 XSizeHints size_hints;
12533
12534 #ifdef USE_X_TOOLKIT
12535 Arg al[2];
12536 int ac = 0;
12537 Dimension widget_width, widget_height;
12538 Window window = XtWindow (f->output_data.x->widget);
12539 #else /* not USE_X_TOOLKIT */
12540 Window window = FRAME_X_WINDOW (f);
12541 #endif /* not USE_X_TOOLKIT */
12542
12543 /* Setting PMaxSize caused various problems. */
12544 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12545
12546 size_hints.x = f->output_data.x->left_pos;
12547 size_hints.y = f->output_data.x->top_pos;
12548
12549 #ifdef USE_X_TOOLKIT
12550 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12551 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12552 XtGetValues (f->output_data.x->widget, al, ac);
12553 size_hints.height = widget_height;
12554 size_hints.width = widget_width;
12555 #else /* not USE_X_TOOLKIT */
12556 size_hints.height = PIXEL_HEIGHT (f);
12557 size_hints.width = PIXEL_WIDTH (f);
12558 #endif /* not USE_X_TOOLKIT */
12559
12560 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12561 size_hints.height_inc = f->output_data.x->line_height;
12562 size_hints.max_width
12563 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12564 size_hints.max_height
12565 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12566
12567 /* Calculate the base and minimum sizes.
12568
12569 (When we use the X toolkit, we don't do it here.
12570 Instead we copy the values that the widgets are using, below.) */
12571 #ifndef USE_X_TOOLKIT
12572 {
12573 int base_width, base_height;
12574 int min_rows = 0, min_cols = 0;
12575
12576 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12577 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12578
12579 check_frame_size (f, &min_rows, &min_cols);
12580
12581 /* The window manager uses the base width hints to calculate the
12582 current number of rows and columns in the frame while
12583 resizing; min_width and min_height aren't useful for this
12584 purpose, since they might not give the dimensions for a
12585 zero-row, zero-column frame.
12586
12587 We use the base_width and base_height members if we have
12588 them; otherwise, we set the min_width and min_height members
12589 to the size for a zero x zero frame. */
12590
12591 #ifdef HAVE_X11R4
12592 size_hints.flags |= PBaseSize;
12593 size_hints.base_width = base_width;
12594 size_hints.base_height = base_height;
12595 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12596 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12597 #else
12598 size_hints.min_width = base_width;
12599 size_hints.min_height = base_height;
12600 #endif
12601 }
12602
12603 /* If we don't need the old flags, we don't need the old hint at all. */
12604 if (flags)
12605 {
12606 size_hints.flags |= flags;
12607 goto no_read;
12608 }
12609 #endif /* not USE_X_TOOLKIT */
12610
12611 {
12612 XSizeHints hints; /* Sometimes I hate X Windows... */
12613 long supplied_return;
12614 int value;
12615
12616 #ifdef HAVE_X11R4
12617 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12618 &supplied_return);
12619 #else
12620 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12621 #endif
12622
12623 #ifdef USE_X_TOOLKIT
12624 size_hints.base_height = hints.base_height;
12625 size_hints.base_width = hints.base_width;
12626 size_hints.min_height = hints.min_height;
12627 size_hints.min_width = hints.min_width;
12628 #endif
12629
12630 if (flags)
12631 size_hints.flags |= flags;
12632 else
12633 {
12634 if (value == 0)
12635 hints.flags = 0;
12636 if (hints.flags & PSize)
12637 size_hints.flags |= PSize;
12638 if (hints.flags & PPosition)
12639 size_hints.flags |= PPosition;
12640 if (hints.flags & USPosition)
12641 size_hints.flags |= USPosition;
12642 if (hints.flags & USSize)
12643 size_hints.flags |= USSize;
12644 }
12645 }
12646
12647 #ifndef USE_X_TOOLKIT
12648 no_read:
12649 #endif
12650
12651 #ifdef PWinGravity
12652 size_hints.win_gravity = f->output_data.x->win_gravity;
12653 size_hints.flags |= PWinGravity;
12654
12655 if (user_position)
12656 {
12657 size_hints.flags &= ~ PPosition;
12658 size_hints.flags |= USPosition;
12659 }
12660 #endif /* PWinGravity */
12661
12662 #ifdef HAVE_X11R4
12663 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12664 #else
12665 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12666 #endif
12667 }
12668
12669 /* Used for IconicState or NormalState */
12670
12671 void
12672 x_wm_set_window_state (f, state)
12673 struct frame *f;
12674 int state;
12675 {
12676 #ifdef USE_X_TOOLKIT
12677 Arg al[1];
12678
12679 XtSetArg (al[0], XtNinitialState, state);
12680 XtSetValues (f->output_data.x->widget, al, 1);
12681 #else /* not USE_X_TOOLKIT */
12682 Window window = FRAME_X_WINDOW (f);
12683
12684 f->output_data.x->wm_hints.flags |= StateHint;
12685 f->output_data.x->wm_hints.initial_state = state;
12686
12687 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12688 #endif /* not USE_X_TOOLKIT */
12689 }
12690
12691 void
12692 x_wm_set_icon_pixmap (f, pixmap_id)
12693 struct frame *f;
12694 int pixmap_id;
12695 {
12696 Pixmap icon_pixmap;
12697
12698 #ifndef USE_X_TOOLKIT
12699 Window window = FRAME_X_WINDOW (f);
12700 #endif
12701
12702 if (pixmap_id > 0)
12703 {
12704 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12705 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12706 }
12707 else
12708 {
12709 /* It seems there is no way to turn off use of an icon pixmap.
12710 The following line does it, only if no icon has yet been created,
12711 for some window managers. But with mwm it crashes.
12712 Some people say it should clear the IconPixmapHint bit in this case,
12713 but that doesn't work, and the X consortium said it isn't the
12714 right thing at all. Since there is no way to win,
12715 best to explicitly give up. */
12716 #if 0
12717 f->output_data.x->wm_hints.icon_pixmap = None;
12718 #else
12719 return;
12720 #endif
12721 }
12722
12723 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12724
12725 {
12726 Arg al[1];
12727 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12728 XtSetValues (f->output_data.x->widget, al, 1);
12729 }
12730
12731 #else /* not USE_X_TOOLKIT */
12732
12733 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12734 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12735
12736 #endif /* not USE_X_TOOLKIT */
12737 }
12738
12739 void
12740 x_wm_set_icon_position (f, icon_x, icon_y)
12741 struct frame *f;
12742 int icon_x, icon_y;
12743 {
12744 #ifdef USE_X_TOOLKIT
12745 Window window = XtWindow (f->output_data.x->widget);
12746 #else
12747 Window window = FRAME_X_WINDOW (f);
12748 #endif
12749
12750 f->output_data.x->wm_hints.flags |= IconPositionHint;
12751 f->output_data.x->wm_hints.icon_x = icon_x;
12752 f->output_data.x->wm_hints.icon_y = icon_y;
12753
12754 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12755 }
12756
12757 \f
12758 /***********************************************************************
12759 Fonts
12760 ***********************************************************************/
12761
12762 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12763
12764 struct font_info *
12765 x_get_font_info (f, font_idx)
12766 FRAME_PTR f;
12767 int font_idx;
12768 {
12769 return (FRAME_X_FONT_TABLE (f) + font_idx);
12770 }
12771
12772
12773 /* Return a list of names of available fonts matching PATTERN on frame
12774 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12775 to be listed. Frame F NULL means we have not yet created any
12776 frame on X, and consult the first display in x_display_list.
12777 MAXNAMES sets a limit on how many fonts to match. */
12778
12779 Lisp_Object
12780 x_list_fonts (f, pattern, size, maxnames)
12781 FRAME_PTR f;
12782 Lisp_Object pattern;
12783 int size;
12784 int maxnames;
12785 {
12786 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12787 Lisp_Object tem, second_best;
12788 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12789 int try_XLoadQueryFont = 0;
12790 int count;
12791
12792 patterns = Fassoc (pattern, Valternate_fontname_alist);
12793 if (NILP (patterns))
12794 patterns = Fcons (pattern, Qnil);
12795
12796 if (maxnames == 1 && !size)
12797 /* We can return any single font matching PATTERN. */
12798 try_XLoadQueryFont = 1;
12799
12800 for (; CONSP (patterns); patterns = XCDR (patterns))
12801 {
12802 int num_fonts;
12803 char **names = NULL;
12804
12805 pattern = XCAR (patterns);
12806 /* See if we cached the result for this particular query.
12807 The cache is an alist of the form:
12808 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12809 */
12810 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12811 key = Fcons (pattern, make_number (maxnames)),
12812 !NILP (list = Fassoc (key, tem))))
12813 {
12814 list = Fcdr_safe (list);
12815 /* We have a cashed list. Don't have to get the list again. */
12816 goto label_cached;
12817 }
12818
12819 /* At first, put PATTERN in the cache. */
12820
12821 BLOCK_INPUT;
12822 count = x_catch_errors (dpy);
12823
12824 if (try_XLoadQueryFont)
12825 {
12826 XFontStruct *font;
12827 unsigned long value;
12828
12829 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12830 if (x_had_errors_p (dpy))
12831 {
12832 /* This error is perhaps due to insufficient memory on X
12833 server. Let's just ignore it. */
12834 font = NULL;
12835 x_clear_errors (dpy);
12836 }
12837
12838 if (font
12839 && XGetFontProperty (font, XA_FONT, &value))
12840 {
12841 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12842 int len = strlen (name);
12843 char *tmp;
12844
12845 /* If DXPC (a Differential X Protocol Compressor)
12846 Ver.3.7 is running, XGetAtomName will return null
12847 string. We must avoid such a name. */
12848 if (len == 0)
12849 try_XLoadQueryFont = 0;
12850 else
12851 {
12852 num_fonts = 1;
12853 names = (char **) alloca (sizeof (char *));
12854 /* Some systems only allow alloca assigned to a
12855 simple var. */
12856 tmp = (char *) alloca (len + 1); names[0] = tmp;
12857 bcopy (name, names[0], len + 1);
12858 XFree (name);
12859 }
12860 }
12861 else
12862 try_XLoadQueryFont = 0;
12863
12864 if (font)
12865 XFreeFont (dpy, font);
12866 }
12867
12868 if (!try_XLoadQueryFont)
12869 {
12870 /* We try at least 10 fonts because XListFonts will return
12871 auto-scaled fonts at the head. */
12872 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12873 &num_fonts);
12874 if (x_had_errors_p (dpy))
12875 {
12876 /* This error is perhaps due to insufficient memory on X
12877 server. Let's just ignore it. */
12878 names = NULL;
12879 x_clear_errors (dpy);
12880 }
12881 }
12882
12883 x_uncatch_errors (dpy, count);
12884 UNBLOCK_INPUT;
12885
12886 if (names)
12887 {
12888 int i;
12889
12890 /* Make a list of all the fonts we got back.
12891 Store that in the font cache for the display. */
12892 for (i = 0; i < num_fonts; i++)
12893 {
12894 int width = 0;
12895 char *p = names[i];
12896 int average_width = -1, dashes = 0;
12897
12898 /* Count the number of dashes in NAMES[I]. If there are
12899 14 dashes, and the field value following 12th dash
12900 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12901 is usually too ugly to be used for editing. Let's
12902 ignore it. */
12903 while (*p)
12904 if (*p++ == '-')
12905 {
12906 dashes++;
12907 if (dashes == 7) /* PIXEL_SIZE field */
12908 width = atoi (p);
12909 else if (dashes == 12) /* AVERAGE_WIDTH field */
12910 average_width = atoi (p);
12911 }
12912 if (dashes < 14 || average_width != 0)
12913 {
12914 tem = build_string (names[i]);
12915 if (NILP (Fassoc (tem, list)))
12916 {
12917 if (STRINGP (Vx_pixel_size_width_font_regexp)
12918 && ((fast_c_string_match_ignore_case
12919 (Vx_pixel_size_width_font_regexp, names[i]))
12920 >= 0))
12921 /* We can set the value of PIXEL_SIZE to the
12922 width of this font. */
12923 list = Fcons (Fcons (tem, make_number (width)), list);
12924 else
12925 /* For the moment, width is not known. */
12926 list = Fcons (Fcons (tem, Qnil), list);
12927 }
12928 }
12929 }
12930 if (!try_XLoadQueryFont)
12931 XFreeFontNames (names);
12932 }
12933
12934 /* Now store the result in the cache. */
12935 if (f != NULL)
12936 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12937 = Fcons (Fcons (key, list),
12938 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12939
12940 label_cached:
12941 if (NILP (list)) continue; /* Try the remaining alternatives. */
12942
12943 newlist = second_best = Qnil;
12944 /* Make a list of the fonts that have the right width. */
12945 for (; CONSP (list); list = XCDR (list))
12946 {
12947 int found_size;
12948
12949 tem = XCAR (list);
12950
12951 if (!CONSP (tem) || NILP (XCAR (tem)))
12952 continue;
12953 if (!size)
12954 {
12955 newlist = Fcons (XCAR (tem), newlist);
12956 continue;
12957 }
12958
12959 if (!INTEGERP (XCDR (tem)))
12960 {
12961 /* Since we have not yet known the size of this font, we
12962 must try slow function call XLoadQueryFont. */
12963 XFontStruct *thisinfo;
12964
12965 BLOCK_INPUT;
12966 count = x_catch_errors (dpy);
12967 thisinfo = XLoadQueryFont (dpy,
12968 XSTRING (XCAR (tem))->data);
12969 if (x_had_errors_p (dpy))
12970 {
12971 /* This error is perhaps due to insufficient memory on X
12972 server. Let's just ignore it. */
12973 thisinfo = NULL;
12974 x_clear_errors (dpy);
12975 }
12976 x_uncatch_errors (dpy, count);
12977 UNBLOCK_INPUT;
12978
12979 if (thisinfo)
12980 {
12981 XCDR (tem)
12982 = (thisinfo->min_bounds.width == 0
12983 ? make_number (0)
12984 : make_number (thisinfo->max_bounds.width));
12985 XFreeFont (dpy, thisinfo);
12986 }
12987 else
12988 /* For unknown reason, the previous call of XListFont had
12989 returned a font which can't be opened. Record the size
12990 as 0 not to try to open it again. */
12991 XCDR (tem) = make_number (0);
12992 }
12993
12994 found_size = XINT (XCDR (tem));
12995 if (found_size == size)
12996 newlist = Fcons (XCAR (tem), newlist);
12997 else if (found_size > 0)
12998 {
12999 if (NILP (second_best))
13000 second_best = tem;
13001 else if (found_size < size)
13002 {
13003 if (XINT (XCDR (second_best)) > size
13004 || XINT (XCDR (second_best)) < found_size)
13005 second_best = tem;
13006 }
13007 else
13008 {
13009 if (XINT (XCDR (second_best)) > size
13010 && XINT (XCDR (second_best)) > found_size)
13011 second_best = tem;
13012 }
13013 }
13014 }
13015 if (!NILP (newlist))
13016 break;
13017 else if (!NILP (second_best))
13018 {
13019 newlist = Fcons (XCAR (second_best), Qnil);
13020 break;
13021 }
13022 }
13023
13024 return newlist;
13025 }
13026
13027
13028 #if GLYPH_DEBUG
13029
13030 /* Check that FONT is valid on frame F. It is if it can be found in F's
13031 font table. */
13032
13033 static void
13034 x_check_font (f, font)
13035 struct frame *f;
13036 XFontStruct *font;
13037 {
13038 int i;
13039 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13040
13041 xassert (font != NULL);
13042
13043 for (i = 0; i < dpyinfo->n_fonts; i++)
13044 if (dpyinfo->font_table[i].name
13045 && font == dpyinfo->font_table[i].font)
13046 break;
13047
13048 xassert (i < dpyinfo->n_fonts);
13049 }
13050
13051 #endif /* GLYPH_DEBUG != 0 */
13052
13053 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13054 Note: There are (broken) X fonts out there with invalid XFontStruct
13055 min_bounds contents. For example, handa@etl.go.jp reports that
13056 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13057 have font->min_bounds.width == 0. */
13058
13059 static INLINE void
13060 x_font_min_bounds (font, w, h)
13061 XFontStruct *font;
13062 int *w, *h;
13063 {
13064 *h = FONT_HEIGHT (font);
13065 *w = font->min_bounds.width;
13066
13067 /* Try to handle the case where FONT->min_bounds has invalid
13068 contents. Since the only font known to have invalid min_bounds
13069 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13070 if (*w <= 0)
13071 *w = font->max_bounds.width;
13072 }
13073
13074
13075 /* Compute the smallest character width and smallest font height over
13076 all fonts available on frame F. Set the members smallest_char_width
13077 and smallest_font_height in F's x_display_info structure to
13078 the values computed. Value is non-zero if smallest_font_height or
13079 smallest_char_width become smaller than they were before. */
13080
13081 static int
13082 x_compute_min_glyph_bounds (f)
13083 struct frame *f;
13084 {
13085 int i;
13086 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13087 XFontStruct *font;
13088 int old_width = dpyinfo->smallest_char_width;
13089 int old_height = dpyinfo->smallest_font_height;
13090
13091 dpyinfo->smallest_font_height = 100000;
13092 dpyinfo->smallest_char_width = 100000;
13093
13094 for (i = 0; i < dpyinfo->n_fonts; ++i)
13095 if (dpyinfo->font_table[i].name)
13096 {
13097 struct font_info *fontp = dpyinfo->font_table + i;
13098 int w, h;
13099
13100 font = (XFontStruct *) fontp->font;
13101 xassert (font != (XFontStruct *) ~0);
13102 x_font_min_bounds (font, &w, &h);
13103
13104 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13105 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13106 }
13107
13108 xassert (dpyinfo->smallest_char_width > 0
13109 && dpyinfo->smallest_font_height > 0);
13110
13111 return (dpyinfo->n_fonts == 1
13112 || dpyinfo->smallest_char_width < old_width
13113 || dpyinfo->smallest_font_height < old_height);
13114 }
13115
13116
13117 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13118 pointer to the structure font_info while allocating it dynamically.
13119 If SIZE is 0, load any size of font.
13120 If loading is failed, return NULL. */
13121
13122 struct font_info *
13123 x_load_font (f, fontname, size)
13124 struct frame *f;
13125 register char *fontname;
13126 int size;
13127 {
13128 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13129 Lisp_Object font_names;
13130 int count;
13131
13132 /* Get a list of all the fonts that match this name. Once we
13133 have a list of matching fonts, we compare them against the fonts
13134 we already have by comparing names. */
13135 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13136
13137 if (!NILP (font_names))
13138 {
13139 Lisp_Object tail;
13140 int i;
13141
13142 for (i = 0; i < dpyinfo->n_fonts; i++)
13143 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13144 if (dpyinfo->font_table[i].name
13145 && (!strcmp (dpyinfo->font_table[i].name,
13146 XSTRING (XCAR (tail))->data)
13147 || !strcmp (dpyinfo->font_table[i].full_name,
13148 XSTRING (XCAR (tail))->data)))
13149 return (dpyinfo->font_table + i);
13150 }
13151
13152 /* Load the font and add it to the table. */
13153 {
13154 char *full_name;
13155 XFontStruct *font;
13156 struct font_info *fontp;
13157 unsigned long value;
13158 int i;
13159
13160 /* If we have found fonts by x_list_font, load one of them. If
13161 not, we still try to load a font by the name given as FONTNAME
13162 because XListFonts (called in x_list_font) of some X server has
13163 a bug of not finding a font even if the font surely exists and
13164 is loadable by XLoadQueryFont. */
13165 if (size > 0 && !NILP (font_names))
13166 fontname = (char *) XSTRING (XCAR (font_names))->data;
13167
13168 BLOCK_INPUT;
13169 count = x_catch_errors (FRAME_X_DISPLAY (f));
13170 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13171 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13172 {
13173 /* This error is perhaps due to insufficient memory on X
13174 server. Let's just ignore it. */
13175 font = NULL;
13176 x_clear_errors (FRAME_X_DISPLAY (f));
13177 }
13178 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13179 UNBLOCK_INPUT;
13180 if (!font)
13181 return NULL;
13182
13183 /* Find a free slot in the font table. */
13184 for (i = 0; i < dpyinfo->n_fonts; ++i)
13185 if (dpyinfo->font_table[i].name == NULL)
13186 break;
13187
13188 /* If no free slot found, maybe enlarge the font table. */
13189 if (i == dpyinfo->n_fonts
13190 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13191 {
13192 int sz;
13193 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13194 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13195 dpyinfo->font_table
13196 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13197 }
13198
13199 fontp = dpyinfo->font_table + i;
13200 if (i == dpyinfo->n_fonts)
13201 ++dpyinfo->n_fonts;
13202
13203 /* Now fill in the slots of *FONTP. */
13204 BLOCK_INPUT;
13205 fontp->font = font;
13206 fontp->font_idx = i;
13207 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13208 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13209
13210 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13211 full_name = 0;
13212 if (XGetFontProperty (font, XA_FONT, &value))
13213 {
13214 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13215 char *p = name;
13216 int dashes = 0;
13217
13218 /* Count the number of dashes in the "full name".
13219 If it is too few, this isn't really the font's full name,
13220 so don't use it.
13221 In X11R4, the fonts did not come with their canonical names
13222 stored in them. */
13223 while (*p)
13224 {
13225 if (*p == '-')
13226 dashes++;
13227 p++;
13228 }
13229
13230 if (dashes >= 13)
13231 {
13232 full_name = (char *) xmalloc (p - name + 1);
13233 bcopy (name, full_name, p - name + 1);
13234 }
13235
13236 XFree (name);
13237 }
13238
13239 if (full_name != 0)
13240 fontp->full_name = full_name;
13241 else
13242 fontp->full_name = fontp->name;
13243
13244 fontp->size = font->max_bounds.width;
13245 fontp->height = FONT_HEIGHT (font);
13246
13247 if (NILP (font_names))
13248 {
13249 /* We come here because of a bug of XListFonts mentioned at
13250 the head of this block. Let's store this information in
13251 the cache for x_list_fonts. */
13252 Lisp_Object lispy_name = build_string (fontname);
13253 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13254
13255 XCDR (dpyinfo->name_list_element)
13256 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13257 Fcons (Fcons (lispy_full_name,
13258 make_number (fontp->size)),
13259 Qnil)),
13260 XCDR (dpyinfo->name_list_element));
13261 if (full_name)
13262 XCDR (dpyinfo->name_list_element)
13263 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13264 Fcons (Fcons (lispy_full_name,
13265 make_number (fontp->size)),
13266 Qnil)),
13267 XCDR (dpyinfo->name_list_element));
13268 }
13269
13270 /* The slot `encoding' specifies how to map a character
13271 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13272 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13273 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13274 2:0xA020..0xFF7F). For the moment, we don't know which charset
13275 uses this font. So, we set information in fontp->encoding[1]
13276 which is never used by any charset. If mapping can't be
13277 decided, set FONT_ENCODING_NOT_DECIDED. */
13278 fontp->encoding[1]
13279 = (font->max_byte1 == 0
13280 /* 1-byte font */
13281 ? (font->min_char_or_byte2 < 0x80
13282 ? (font->max_char_or_byte2 < 0x80
13283 ? 0 /* 0x20..0x7F */
13284 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13285 : 1) /* 0xA0..0xFF */
13286 /* 2-byte font */
13287 : (font->min_byte1 < 0x80
13288 ? (font->max_byte1 < 0x80
13289 ? (font->min_char_or_byte2 < 0x80
13290 ? (font->max_char_or_byte2 < 0x80
13291 ? 0 /* 0x2020..0x7F7F */
13292 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13293 : 3) /* 0x20A0..0x7FFF */
13294 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13295 : (font->min_char_or_byte2 < 0x80
13296 ? (font->max_char_or_byte2 < 0x80
13297 ? 2 /* 0xA020..0xFF7F */
13298 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13299 : 1))); /* 0xA0A0..0xFFFF */
13300
13301 fontp->baseline_offset
13302 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13303 ? (long) value : 0);
13304 fontp->relative_compose
13305 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13306 ? (long) value : 0);
13307 fontp->default_ascent
13308 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13309 ? (long) value : 0);
13310
13311 /* Set global flag fonts_changed_p to non-zero if the font loaded
13312 has a character with a smaller width than any other character
13313 before, or if the font loaded has a smalle>r height than any
13314 other font loaded before. If this happens, it will make a
13315 glyph matrix reallocation necessary. */
13316 fonts_changed_p = x_compute_min_glyph_bounds (f);
13317 UNBLOCK_INPUT;
13318 return fontp;
13319 }
13320 }
13321
13322
13323 /* Return a pointer to struct font_info of a font named FONTNAME for
13324 frame F. If no such font is loaded, return NULL. */
13325
13326 struct font_info *
13327 x_query_font (f, fontname)
13328 struct frame *f;
13329 register char *fontname;
13330 {
13331 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13332 int i;
13333
13334 for (i = 0; i < dpyinfo->n_fonts; i++)
13335 if (dpyinfo->font_table[i].name
13336 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13337 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13338 return (dpyinfo->font_table + i);
13339 return NULL;
13340 }
13341
13342
13343 /* Find a CCL program for a font specified by FONTP, and set the member
13344 `encoder' of the structure. */
13345
13346 void
13347 x_find_ccl_program (fontp)
13348 struct font_info *fontp;
13349 {
13350 Lisp_Object list, elt;
13351
13352 elt = Qnil;
13353 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13354 {
13355 elt = XCAR (list);
13356 if (CONSP (elt)
13357 && STRINGP (XCAR (elt))
13358 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13359 >= 0)
13360 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13361 >= 0)))
13362 break;
13363 }
13364
13365 if (! NILP (list))
13366 {
13367 struct ccl_program *ccl
13368 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13369
13370 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13371 xfree (ccl);
13372 else
13373 fontp->font_encoder = ccl;
13374 }
13375 }
13376
13377
13378 \f
13379 /***********************************************************************
13380 Initialization
13381 ***********************************************************************/
13382
13383 #ifdef USE_X_TOOLKIT
13384 static XrmOptionDescRec emacs_options[] = {
13385 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13386 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13387
13388 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13389 XrmoptionSepArg, NULL},
13390 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13391
13392 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13393 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13394 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13395 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13396 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13397 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13398 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13399 };
13400 #endif /* USE_X_TOOLKIT */
13401
13402 static int x_initialized;
13403
13404 #ifdef MULTI_KBOARD
13405 /* Test whether two display-name strings agree up to the dot that separates
13406 the screen number from the server number. */
13407 static int
13408 same_x_server (name1, name2)
13409 char *name1, *name2;
13410 {
13411 int seen_colon = 0;
13412 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13413 int system_name_length = strlen (system_name);
13414 int length_until_period = 0;
13415
13416 while (system_name[length_until_period] != 0
13417 && system_name[length_until_period] != '.')
13418 length_until_period++;
13419
13420 /* Treat `unix' like an empty host name. */
13421 if (! strncmp (name1, "unix:", 5))
13422 name1 += 4;
13423 if (! strncmp (name2, "unix:", 5))
13424 name2 += 4;
13425 /* Treat this host's name like an empty host name. */
13426 if (! strncmp (name1, system_name, system_name_length)
13427 && name1[system_name_length] == ':')
13428 name1 += system_name_length;
13429 if (! strncmp (name2, system_name, system_name_length)
13430 && name2[system_name_length] == ':')
13431 name2 += system_name_length;
13432 /* Treat this host's domainless name like an empty host name. */
13433 if (! strncmp (name1, system_name, length_until_period)
13434 && name1[length_until_period] == ':')
13435 name1 += length_until_period;
13436 if (! strncmp (name2, system_name, length_until_period)
13437 && name2[length_until_period] == ':')
13438 name2 += length_until_period;
13439
13440 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13441 {
13442 if (*name1 == ':')
13443 seen_colon++;
13444 if (seen_colon && *name1 == '.')
13445 return 1;
13446 }
13447 return (seen_colon
13448 && (*name1 == '.' || *name1 == '\0')
13449 && (*name2 == '.' || *name2 == '\0'));
13450 }
13451 #endif
13452
13453 struct x_display_info *
13454 x_term_init (display_name, xrm_option, resource_name)
13455 Lisp_Object display_name;
13456 char *xrm_option;
13457 char *resource_name;
13458 {
13459 int connection;
13460 Display *dpy;
13461 struct x_display_info *dpyinfo;
13462 XrmDatabase xrdb;
13463
13464 BLOCK_INPUT;
13465
13466 if (!x_initialized)
13467 {
13468 x_initialize ();
13469 x_initialized = 1;
13470 }
13471
13472 #ifdef USE_X_TOOLKIT
13473 /* weiner@footloose.sps.mot.com reports that this causes
13474 errors with X11R5:
13475 X protocol error: BadAtom (invalid Atom parameter)
13476 on protocol request 18skiloaf.
13477 So let's not use it until R6. */
13478 #ifdef HAVE_X11XTR6
13479 XtSetLanguageProc (NULL, NULL, NULL);
13480 #endif
13481
13482 {
13483 int argc = 0;
13484 char *argv[3];
13485
13486 argv[0] = "";
13487 argc = 1;
13488 if (xrm_option)
13489 {
13490 argv[argc++] = "-xrm";
13491 argv[argc++] = xrm_option;
13492 }
13493 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13494 resource_name, EMACS_CLASS,
13495 emacs_options, XtNumber (emacs_options),
13496 &argc, argv);
13497
13498 #ifdef HAVE_X11XTR6
13499 /* I think this is to compensate for XtSetLanguageProc. */
13500 fixup_locale ();
13501 #endif
13502 }
13503
13504 #else /* not USE_X_TOOLKIT */
13505 #ifdef HAVE_X11R5
13506 XSetLocaleModifiers ("");
13507 #endif
13508 dpy = XOpenDisplay (XSTRING (display_name)->data);
13509 #endif /* not USE_X_TOOLKIT */
13510
13511 /* Detect failure. */
13512 if (dpy == 0)
13513 {
13514 UNBLOCK_INPUT;
13515 return 0;
13516 }
13517
13518 /* We have definitely succeeded. Record the new connection. */
13519
13520 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13521 bzero (dpyinfo, sizeof *dpyinfo);
13522
13523 #ifdef MULTI_KBOARD
13524 {
13525 struct x_display_info *share;
13526 Lisp_Object tail;
13527
13528 for (share = x_display_list, tail = x_display_name_list; share;
13529 share = share->next, tail = XCDR (tail))
13530 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13531 XSTRING (display_name)->data))
13532 break;
13533 if (share)
13534 dpyinfo->kboard = share->kboard;
13535 else
13536 {
13537 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13538 init_kboard (dpyinfo->kboard);
13539 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13540 {
13541 char *vendor = ServerVendor (dpy);
13542 UNBLOCK_INPUT;
13543 dpyinfo->kboard->Vsystem_key_alist
13544 = call1 (Qvendor_specific_keysyms,
13545 build_string (vendor ? vendor : ""));
13546 BLOCK_INPUT;
13547 }
13548
13549 dpyinfo->kboard->next_kboard = all_kboards;
13550 all_kboards = dpyinfo->kboard;
13551 /* Don't let the initial kboard remain current longer than necessary.
13552 That would cause problems if a file loaded on startup tries to
13553 prompt in the mini-buffer. */
13554 if (current_kboard == initial_kboard)
13555 current_kboard = dpyinfo->kboard;
13556 }
13557 dpyinfo->kboard->reference_count++;
13558 }
13559 #endif
13560
13561 /* Put this display on the chain. */
13562 dpyinfo->next = x_display_list;
13563 x_display_list = dpyinfo;
13564
13565 /* Put it on x_display_name_list as well, to keep them parallel. */
13566 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13567 x_display_name_list);
13568 dpyinfo->name_list_element = XCAR (x_display_name_list);
13569
13570 dpyinfo->display = dpy;
13571
13572 #if 0
13573 XSetAfterFunction (x_current_display, x_trace_wire);
13574 #endif /* ! 0 */
13575
13576 dpyinfo->x_id_name
13577 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13578 + STRING_BYTES (XSTRING (Vsystem_name))
13579 + 2);
13580 sprintf (dpyinfo->x_id_name, "%s@%s",
13581 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13582
13583 /* Figure out which modifier bits mean what. */
13584 x_find_modifier_meanings (dpyinfo);
13585
13586 /* Get the scroll bar cursor. */
13587 dpyinfo->vertical_scroll_bar_cursor
13588 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13589
13590 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13591 resource_name, EMACS_CLASS);
13592 #ifdef HAVE_XRMSETDATABASE
13593 XrmSetDatabase (dpyinfo->display, xrdb);
13594 #else
13595 dpyinfo->display->db = xrdb;
13596 #endif
13597 /* Put the rdb where we can find it in a way that works on
13598 all versions. */
13599 dpyinfo->xrdb = xrdb;
13600
13601 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13602 DefaultScreen (dpyinfo->display));
13603 select_visual (dpyinfo);
13604 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13605 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13606 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13607 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13608 dpyinfo->grabbed = 0;
13609 dpyinfo->reference_count = 0;
13610 dpyinfo->icon_bitmap_id = -1;
13611 dpyinfo->font_table = NULL;
13612 dpyinfo->n_fonts = 0;
13613 dpyinfo->font_table_size = 0;
13614 dpyinfo->bitmaps = 0;
13615 dpyinfo->bitmaps_size = 0;
13616 dpyinfo->bitmaps_last = 0;
13617 dpyinfo->scratch_cursor_gc = 0;
13618 dpyinfo->mouse_face_mouse_frame = 0;
13619 dpyinfo->mouse_face_deferred_gc = 0;
13620 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13621 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13622 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13623 dpyinfo->mouse_face_window = Qnil;
13624 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13625 dpyinfo->mouse_face_defer = 0;
13626 dpyinfo->x_focus_frame = 0;
13627 dpyinfo->x_focus_event_frame = 0;
13628 dpyinfo->x_highlight_frame = 0;
13629 dpyinfo->image_cache = make_image_cache ();
13630
13631 /* See if a private colormap is requested. */
13632 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13633 {
13634 if (dpyinfo->visual->class == PseudoColor)
13635 {
13636 Lisp_Object value;
13637 value = display_x_get_resource (dpyinfo,
13638 build_string ("privateColormap"),
13639 build_string ("PrivateColormap"),
13640 Qnil, Qnil);
13641 if (STRINGP (value)
13642 && (!strcmp (XSTRING (value)->data, "true")
13643 || !strcmp (XSTRING (value)->data, "on")))
13644 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13645 }
13646 }
13647 else
13648 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13649 dpyinfo->visual, AllocNone);
13650
13651 {
13652 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13653 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13654 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13655 dpyinfo->resy = pixels * 25.4 / mm;
13656 pixels = DisplayWidth (dpyinfo->display, screen_number);
13657 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13658 dpyinfo->resx = pixels * 25.4 / mm;
13659 }
13660
13661 dpyinfo->Xatom_wm_protocols
13662 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13663 dpyinfo->Xatom_wm_take_focus
13664 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13665 dpyinfo->Xatom_wm_save_yourself
13666 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13667 dpyinfo->Xatom_wm_delete_window
13668 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13669 dpyinfo->Xatom_wm_change_state
13670 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13671 dpyinfo->Xatom_wm_configure_denied
13672 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13673 dpyinfo->Xatom_wm_window_moved
13674 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13675 dpyinfo->Xatom_editres
13676 = XInternAtom (dpyinfo->display, "Editres", False);
13677 dpyinfo->Xatom_CLIPBOARD
13678 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13679 dpyinfo->Xatom_TIMESTAMP
13680 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13681 dpyinfo->Xatom_TEXT
13682 = XInternAtom (dpyinfo->display, "TEXT", False);
13683 dpyinfo->Xatom_COMPOUND_TEXT
13684 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13685 dpyinfo->Xatom_DELETE
13686 = XInternAtom (dpyinfo->display, "DELETE", False);
13687 dpyinfo->Xatom_MULTIPLE
13688 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13689 dpyinfo->Xatom_INCR
13690 = XInternAtom (dpyinfo->display, "INCR", False);
13691 dpyinfo->Xatom_EMACS_TMP
13692 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13693 dpyinfo->Xatom_TARGETS
13694 = XInternAtom (dpyinfo->display, "TARGETS", False);
13695 dpyinfo->Xatom_NULL
13696 = XInternAtom (dpyinfo->display, "NULL", False);
13697 dpyinfo->Xatom_ATOM_PAIR
13698 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13699 /* For properties of font. */
13700 dpyinfo->Xatom_PIXEL_SIZE
13701 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13702 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13703 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13704 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13705 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13706 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13707 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13708
13709 /* Ghostscript support. */
13710 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13711 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13712
13713 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13714 False);
13715
13716 dpyinfo->cut_buffers_initialized = 0;
13717
13718 connection = ConnectionNumber (dpyinfo->display);
13719 dpyinfo->connection = connection;
13720
13721 {
13722 char null_bits[1];
13723
13724 null_bits[0] = 0x00;
13725
13726 dpyinfo->null_pixel
13727 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13728 null_bits, 1, 1, (long) 0, (long) 0,
13729 1);
13730 }
13731
13732 {
13733 extern int gray_bitmap_width, gray_bitmap_height;
13734 extern char *gray_bitmap_bits;
13735 dpyinfo->gray
13736 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13737 gray_bitmap_bits,
13738 gray_bitmap_width, gray_bitmap_height,
13739 (unsigned long) 1, (unsigned long) 0, 1);
13740 }
13741
13742 #ifdef HAVE_X_I18N
13743 xim_initialize (dpyinfo, resource_name);
13744 #endif
13745
13746 #ifdef subprocesses
13747 /* This is only needed for distinguishing keyboard and process input. */
13748 if (connection != 0)
13749 add_keyboard_wait_descriptor (connection);
13750 #endif
13751
13752 #ifndef F_SETOWN_BUG
13753 #ifdef F_SETOWN
13754 #ifdef F_SETOWN_SOCK_NEG
13755 /* stdin is a socket here */
13756 fcntl (connection, F_SETOWN, -getpid ());
13757 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13758 fcntl (connection, F_SETOWN, getpid ());
13759 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13760 #endif /* ! defined (F_SETOWN) */
13761 #endif /* F_SETOWN_BUG */
13762
13763 #ifdef SIGIO
13764 if (interrupt_input)
13765 init_sigio (connection);
13766 #endif /* ! defined (SIGIO) */
13767
13768 #ifdef USE_LUCID
13769 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13770 /* Make sure that we have a valid font for dialog boxes
13771 so that Xt does not crash. */
13772 {
13773 Display *dpy = dpyinfo->display;
13774 XrmValue d, fr, to;
13775 Font font;
13776 int count;
13777
13778 d.addr = (XPointer)&dpy;
13779 d.size = sizeof (Display *);
13780 fr.addr = XtDefaultFont;
13781 fr.size = sizeof (XtDefaultFont);
13782 to.size = sizeof (Font *);
13783 to.addr = (XPointer)&font;
13784 count = x_catch_errors (dpy);
13785 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13786 abort ();
13787 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13788 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13789 x_uncatch_errors (dpy, count);
13790 }
13791 #endif
13792 #endif
13793
13794 /* See if we should run in synchronous mode. This is useful
13795 for debugging X code. */
13796 {
13797 Lisp_Object value;
13798 value = display_x_get_resource (dpyinfo,
13799 build_string ("synchronous"),
13800 build_string ("Synchronous"),
13801 Qnil, Qnil);
13802 if (STRINGP (value)
13803 && (!strcmp (XSTRING (value)->data, "true")
13804 || !strcmp (XSTRING (value)->data, "on")))
13805 XSynchronize (dpyinfo->display, True);
13806 }
13807
13808 UNBLOCK_INPUT;
13809
13810 return dpyinfo;
13811 }
13812 \f
13813 /* Get rid of display DPYINFO, assuming all frames are already gone,
13814 and without sending any more commands to the X server. */
13815
13816 void
13817 x_delete_display (dpyinfo)
13818 struct x_display_info *dpyinfo;
13819 {
13820 delete_keyboard_wait_descriptor (dpyinfo->connection);
13821
13822 /* Discard this display from x_display_name_list and x_display_list.
13823 We can't use Fdelq because that can quit. */
13824 if (! NILP (x_display_name_list)
13825 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13826 x_display_name_list = XCDR (x_display_name_list);
13827 else
13828 {
13829 Lisp_Object tail;
13830
13831 tail = x_display_name_list;
13832 while (CONSP (tail) && CONSP (XCDR (tail)))
13833 {
13834 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13835 {
13836 XCDR (tail) = XCDR (XCDR (tail));
13837 break;
13838 }
13839 tail = XCDR (tail);
13840 }
13841 }
13842
13843 if (next_noop_dpyinfo == dpyinfo)
13844 next_noop_dpyinfo = dpyinfo->next;
13845
13846 if (x_display_list == dpyinfo)
13847 x_display_list = dpyinfo->next;
13848 else
13849 {
13850 struct x_display_info *tail;
13851
13852 for (tail = x_display_list; tail; tail = tail->next)
13853 if (tail->next == dpyinfo)
13854 tail->next = tail->next->next;
13855 }
13856
13857 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13858 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13859 XrmDestroyDatabase (dpyinfo->xrdb);
13860 #endif
13861 #endif
13862 #ifdef MULTI_KBOARD
13863 if (--dpyinfo->kboard->reference_count == 0)
13864 delete_kboard (dpyinfo->kboard);
13865 #endif
13866 #ifdef HAVE_X_I18N
13867 if (dpyinfo->xim)
13868 xim_close_dpy (dpyinfo);
13869 #endif
13870
13871 xfree (dpyinfo->font_table);
13872 xfree (dpyinfo->x_id_name);
13873 xfree (dpyinfo->color_cells);
13874 xfree (dpyinfo);
13875 }
13876
13877 \f
13878 /* Set up use of X before we make the first connection. */
13879
13880 static struct redisplay_interface x_redisplay_interface =
13881 {
13882 x_produce_glyphs,
13883 x_write_glyphs,
13884 x_insert_glyphs,
13885 x_clear_end_of_line,
13886 x_scroll_run,
13887 x_after_update_window_line,
13888 x_update_window_begin,
13889 x_update_window_end,
13890 XTcursor_to,
13891 x_flush,
13892 x_clear_mouse_face,
13893 x_get_glyph_overhangs,
13894 x_fix_overlapping_area
13895 };
13896
13897 void
13898 x_initialize ()
13899 {
13900 rif = &x_redisplay_interface;
13901
13902 clear_frame_hook = x_clear_frame;
13903 ins_del_lines_hook = x_ins_del_lines;
13904 change_line_highlight_hook = x_change_line_highlight;
13905 delete_glyphs_hook = x_delete_glyphs;
13906 ring_bell_hook = XTring_bell;
13907 reset_terminal_modes_hook = XTreset_terminal_modes;
13908 set_terminal_modes_hook = XTset_terminal_modes;
13909 update_begin_hook = x_update_begin;
13910 update_end_hook = x_update_end;
13911 set_terminal_window_hook = XTset_terminal_window;
13912 read_socket_hook = XTread_socket;
13913 frame_up_to_date_hook = XTframe_up_to_date;
13914 reassert_line_highlight_hook = XTreassert_line_highlight;
13915 mouse_position_hook = XTmouse_position;
13916 frame_rehighlight_hook = XTframe_rehighlight;
13917 frame_raise_lower_hook = XTframe_raise_lower;
13918 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13919 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13920 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13921 judge_scroll_bars_hook = XTjudge_scroll_bars;
13922 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13923
13924 scroll_region_ok = 1; /* we'll scroll partial frames */
13925 char_ins_del_ok = 0; /* just as fast to write the line */
13926 line_ins_del_ok = 1; /* we'll just blt 'em */
13927 fast_clear_end_of_line = 1; /* X does this well */
13928 memory_below_frame = 0; /* we don't remember what scrolls
13929 off the bottom */
13930 baud_rate = 19200;
13931
13932 x_noop_count = 0;
13933 last_tool_bar_item = -1;
13934 any_help_event_p = 0;
13935
13936 /* Try to use interrupt input; if we can't, then start polling. */
13937 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13938
13939 #ifdef USE_X_TOOLKIT
13940 XtToolkitInitialize ();
13941 Xt_app_con = XtCreateApplicationContext ();
13942 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13943
13944 /* Install an asynchronous timer that processes Xt timeout events
13945 every 0.1s. This is necessary because some widget sets use
13946 timeouts internally, for example the LessTif menu bar, or the
13947 Xaw3d scroll bar. When Xt timouts aren't processed, these
13948 widgets don't behave normally. */
13949 {
13950 EMACS_TIME interval;
13951 EMACS_SET_SECS_USECS (interval, 0, 100000);
13952 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13953 }
13954 #endif
13955
13956 #ifdef USE_TOOLKIT_SCROLL_BARS
13957 xaw3d_arrow_scroll = False;
13958 xaw3d_pick_top = True;
13959 #endif
13960
13961 /* Note that there is no real way portable across R3/R4 to get the
13962 original error handler. */
13963 XSetErrorHandler (x_error_handler);
13964 XSetIOErrorHandler (x_io_error_quitter);
13965
13966 /* Disable Window Change signals; they are handled by X events. */
13967 #ifdef SIGWINCH
13968 signal (SIGWINCH, SIG_DFL);
13969 #endif /* ! defined (SIGWINCH) */
13970
13971 signal (SIGPIPE, x_connection_signal);
13972 }
13973
13974
13975 void
13976 syms_of_xterm ()
13977 {
13978 staticpro (&x_error_message_string);
13979 x_error_message_string = Qnil;
13980
13981 staticpro (&x_display_name_list);
13982 x_display_name_list = Qnil;
13983
13984 staticpro (&last_mouse_scroll_bar);
13985 last_mouse_scroll_bar = Qnil;
13986
13987 staticpro (&Qvendor_specific_keysyms);
13988 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13989
13990 staticpro (&last_mouse_press_frame);
13991 last_mouse_press_frame = Qnil;
13992
13993 help_echo = Qnil;
13994 staticpro (&help_echo);
13995 help_echo_object = Qnil;
13996 staticpro (&help_echo_object);
13997 help_echo_window = Qnil;
13998 staticpro (&help_echo_window);
13999 previous_help_echo = Qnil;
14000 staticpro (&previous_help_echo);
14001 help_echo_pos = -1;
14002
14003 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14004 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14005 For example, if a block cursor is over a tab, it will be drawn as\n\
14006 wide as that tab on the display.");
14007 x_stretch_cursor_p = 0;
14008
14009 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
14010 "If not nil, Emacs uses toolkit scroll bars.");
14011 #ifdef USE_TOOLKIT_SCROLL_BARS
14012 x_toolkit_scroll_bars_p = 1;
14013 #else
14014 x_toolkit_scroll_bars_p = 0;
14015 #endif
14016
14017 staticpro (&last_mouse_motion_frame);
14018 last_mouse_motion_frame = Qnil;
14019 }
14020
14021 #endif /* not HAVE_X_WINDOWS */