*** empty log message ***
[bpt/emacs.git] / src / xterm.c
... / ...
CommitLineData
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
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, 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
105extern void free_frame_menubar P_ ((struct frame *));
106extern 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
111extern 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
166enum 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
182static 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
189static 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
196static 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
203static 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
210static 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
219static 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
225static unsigned char ov_bits[] = {
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
228#endif
229
230extern Lisp_Object Qhelp_echo;
231
232\f
233/* Non-zero means Emacs uses toolkit scroll bars. */
234
235int 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
240static Lisp_Object help_echo;
241static Lisp_Object help_echo_window;
242static Lisp_Object help_echo_object;
243static int help_echo_pos;
244
245/* Temporary variable for XTread_socket. */
246
247static Lisp_Object previous_help_echo;
248
249/* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252static 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
258int x_stretch_cursor_p;
259
260/* This is a chain of structures for all the X displays currently in
261 use. */
262
263struct 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
270Lisp_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
277extern struct frame *updating_frame;
278
279extern int waiting_for_input;
280
281/* This is a frame waiting to be auto-raised, within XTread_socket. */
282
283struct frame *pending_autoraise_frame;
284
285#ifdef USE_X_TOOLKIT
286/* The application context for Xt use. */
287XtAppContext Xt_app_con;
288static 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
295struct cursor_pos output_cursor;
296
297/* Non-zero means user is interacting with a toolkit scroll bar. */
298
299static 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
323FRAME_PTR last_mouse_frame;
324static XRectangle last_mouse_glyph;
325static 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
337static 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
346static Time last_mouse_movement_time;
347
348/* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
351#ifdef __STDC__
352static int volatile input_signal_count;
353#else
354static int input_signal_count;
355#endif
356
357/* Used locally within XTread_socket. */
358
359static int x_noop_count;
360
361/* Initial values of argv and argc. */
362
363extern char **initial_argv;
364extern int initial_argc;
365
366extern Lisp_Object Vcommand_line_args, Vsystem_name;
367
368/* Tells if a window manager is present or not. */
369
370extern Lisp_Object Vx_no_window_manager;
371
372extern Lisp_Object Qface, Qmouse_face;
373
374extern int errno;
375
376/* A mask of extra modifier bits to put into every keyboard char. */
377
378extern int extra_keyboard_modifiers;
379
380static Lisp_Object Qvendor_specific_keysyms;
381
382extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383extern 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
389enum 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
399static void x_update_window_end P_ ((struct window *, int, int));
400static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
401void x_delete_display P_ ((struct x_display_info *));
402static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
403 unsigned));
404static int fast_find_position P_ ((struct window *, int, int *, int *,
405 int *, int *));
406static void set_output_cursor P_ ((struct cursor_pos *));
407static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
408 int *, int *, int *));
409static void note_mode_line_highlight P_ ((struct window *, int, int));
410static void note_mouse_highlight P_ ((struct frame *, int, int));
411static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
412static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
413static void show_mouse_face P_ ((struct x_display_info *,
414 enum draw_glyphs_face));
415static int x_io_error_quitter P_ ((Display *));
416int x_catch_errors P_ ((Display *));
417void x_uncatch_errors P_ ((Display *, int));
418void x_lower_frame P_ ((struct frame *));
419void x_scroll_bar_clear P_ ((struct frame *));
420int x_had_errors_p P_ ((Display *));
421void x_wm_set_size_hint P_ ((struct frame *, long, int));
422void x_raise_frame P_ ((struct frame *));
423void x_set_window_size P_ ((struct frame *, int, int, int));
424void x_wm_set_window_state P_ ((struct frame *, int));
425void x_wm_set_icon_pixmap P_ ((struct frame *, int));
426void x_initialize P_ ((void));
427static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
428static int x_compute_min_glyph_bounds P_ ((struct frame *));
429static void x_draw_phys_cursor_glyph P_ ((struct window *,
430 struct glyph_row *,
431 enum draw_glyphs_face));
432static void x_update_end P_ ((struct frame *));
433static void XTframe_up_to_date P_ ((struct frame *));
434static void XTreassert_line_highlight P_ ((int, int));
435static void x_change_line_highlight P_ ((int, int, int, int));
436static void XTset_terminal_modes P_ ((void));
437static void XTreset_terminal_modes P_ ((void));
438static void XTcursor_to P_ ((int, int, int, int));
439static void x_write_glyphs P_ ((struct glyph *, int));
440static void x_clear_end_of_line P_ ((int));
441static void x_clear_frame P_ ((void));
442static void x_clear_cursor P_ ((struct window *));
443static void frame_highlight P_ ((struct frame *));
444static void frame_unhighlight P_ ((struct frame *));
445static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
446static void XTframe_rehighlight P_ ((struct frame *));
447static void x_frame_rehighlight P_ ((struct x_display_info *));
448static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
449static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
450static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
451 XRectangle *));
452static void expose_frame P_ ((struct frame *, int, int, int, int));
453static void expose_window_tree P_ ((struct window *, XRectangle *));
454static void expose_window P_ ((struct window *, XRectangle *));
455static void expose_area P_ ((struct window *, struct glyph_row *,
456 XRectangle *, enum glyph_row_area));
457static void expose_line P_ ((struct window *, struct glyph_row *,
458 XRectangle *));
459static void x_update_cursor_in_window_tree P_ ((struct window *, int));
460static void x_update_window_cursor P_ ((struct window *, int));
461static void x_erase_phys_cursor P_ ((struct window *));
462void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
463static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
464 enum bitmap_type));
465
466static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
467 GC, int));
468static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
469static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
470static void note_overwritten_text_cursor P_ ((struct window *, int, int));
471static void x_flush P_ ((struct frame *f));
472static void x_update_begin P_ ((struct frame *));
473static void x_update_window_begin P_ ((struct window *));
474static void x_draw_vertical_border P_ ((struct window *));
475static void x_after_update_window_line P_ ((struct glyph_row *));
476static INLINE void take_vertical_position_into_account P_ ((struct it *));
477static void x_produce_stretch_glyph P_ ((struct it *));
478
479
480/* Flush display of frame F, or of all frames if F is null. */
481
482static void
483x_flush (f)
484 struct frame *f;
485{
486 BLOCK_INPUT;
487 if (f == NULL)
488 {
489 Lisp_Object rest, frame;
490 FOR_EACH_FRAME (rest, frame)
491 x_flush (XFRAME (frame));
492 }
493 else if (FRAME_X_P (f))
494 XFlush (FRAME_X_DISPLAY (f));
495 UNBLOCK_INPUT;
496}
497
498
499/* Remove calls to XFlush by defining XFlush to an empty replacement.
500 Calls to XFlush should be unnecessary because the X output buffer
501 is flushed automatically as needed by calls to XPending,
502 XNextEvent, or XWindowEvent according to the XFlush man page.
503 XTread_socket calls XPending. Removing XFlush improves
504 performance. */
505
506#define XFlush(DISPLAY) (void) 0
507
508\f
509/***********************************************************************
510 Debugging
511 ***********************************************************************/
512
513#if 0
514
515/* This is a function useful for recording debugging information about
516 the sequence of occurrences in this file. */
517
518struct record
519{
520 char *locus;
521 int type;
522};
523
524struct record event_record[100];
525
526int event_record_index;
527
528record_event (locus, type)
529 char *locus;
530 int type;
531{
532 if (event_record_index == sizeof (event_record) / sizeof (struct record))
533 event_record_index = 0;
534
535 event_record[event_record_index].locus = locus;
536 event_record[event_record_index].type = type;
537 event_record_index++;
538}
539
540#endif /* 0 */
541
542
543\f
544/* Return the struct x_display_info corresponding to DPY. */
545
546struct x_display_info *
547x_display_info_for_display (dpy)
548 Display *dpy;
549{
550 struct x_display_info *dpyinfo;
551
552 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
553 if (dpyinfo->display == dpy)
554 return dpyinfo;
555
556 return 0;
557}
558
559
560\f
561/***********************************************************************
562 Starting and ending an update
563 ***********************************************************************/
564
565/* Start an update of frame F. This function is installed as a hook
566 for update_begin, i.e. it is called when update_begin is called.
567 This function is called prior to calls to x_update_window_begin for
568 each window being updated. Currently, there is nothing to do here
569 because all interesting stuff is done on a window basis. */
570
571static void
572x_update_begin (f)
573 struct frame *f;
574{
575 /* Nothing to do. */
576}
577
578
579/* Start update of window W. Set the global variable updated_window
580 to the window being updated and set output_cursor to the cursor
581 position of W. */
582
583static void
584x_update_window_begin (w)
585 struct window *w;
586{
587 struct frame *f = XFRAME (WINDOW_FRAME (w));
588 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
589
590 updated_window = w;
591 set_output_cursor (&w->cursor);
592
593 BLOCK_INPUT;
594
595 if (f == display_info->mouse_face_mouse_frame)
596 {
597 /* Don't do highlighting for mouse motion during the update. */
598 display_info->mouse_face_defer = 1;
599
600 /* If F needs to be redrawn, simply forget about any prior mouse
601 highlighting. */
602 if (FRAME_GARBAGED_P (f))
603 display_info->mouse_face_window = Qnil;
604
605#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
606 their mouse_face_p flag set, which means that they are always
607 unequal to rows in a desired matrix which never have that
608 flag set. So, rows containing mouse-face glyphs are never
609 scrolled, and we don't have to switch the mouse highlight off
610 here to prevent it from being scrolled. */
611
612 /* Can we tell that this update does not affect the window
613 where the mouse highlight is? If so, no need to turn off.
614 Likewise, don't do anything if the frame is garbaged;
615 in that case, the frame's current matrix that we would use
616 is all wrong, and we will redisplay that line anyway. */
617 if (!NILP (display_info->mouse_face_window)
618 && w == XWINDOW (display_info->mouse_face_window))
619 {
620 int i;
621
622 for (i = 0; i < w->desired_matrix->nrows; ++i)
623 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
624 break;
625
626 if (i < w->desired_matrix->nrows)
627 clear_mouse_face (display_info);
628 }
629#endif /* 0 */
630 }
631
632 UNBLOCK_INPUT;
633}
634
635
636/* Draw a vertical window border to the right of window W if W doesn't
637 have vertical scroll bars. */
638
639static void
640x_draw_vertical_border (w)
641 struct window *w;
642{
643 struct frame *f = XFRAME (WINDOW_FRAME (w));
644
645 /* Redraw borders between horizontally adjacent windows. Don't
646 do it for frames with vertical scroll bars because either the
647 right scroll bar of a window, or the left scroll bar of its
648 neighbor will suffice as a border. */
649 if (!WINDOW_RIGHTMOST_P (w)
650 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
651 {
652 int x0, x1, y0, y1;
653
654 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
655 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
656 y1 -= 1;
657
658 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
659 f->output_data.x->normal_gc, x1, y0, x1, y1);
660 }
661}
662
663
664/* End update of window W (which is equal to updated_window).
665
666 Draw vertical borders between horizontally adjacent windows, and
667 display W's cursor if CURSOR_ON_P is non-zero.
668
669 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
670 glyphs in mouse-face were overwritten. In that case we have to
671 make sure that the mouse-highlight is properly redrawn.
672
673 W may be a menu bar pseudo-window in case we don't have X toolkit
674 support. Such windows don't have a cursor, so don't display it
675 here. */
676
677static void
678x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
679 struct window *w;
680 int cursor_on_p, mouse_face_overwritten_p;
681{
682 if (!w->pseudo_window_p)
683 {
684 struct x_display_info *dpyinfo
685 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
686
687 BLOCK_INPUT;
688
689 /* If a row with mouse-face was overwritten, arrange for
690 XTframe_up_to_date to redisplay the mouse highlight. */
691 if (mouse_face_overwritten_p)
692 {
693 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
694 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
695 dpyinfo->mouse_face_window = Qnil;
696 }
697
698 if (cursor_on_p)
699 x_display_and_set_cursor (w, 1, output_cursor.hpos,
700 output_cursor.vpos,
701 output_cursor.x, output_cursor.y);
702
703 x_draw_vertical_border (w);
704 UNBLOCK_INPUT;
705 }
706
707 updated_window = NULL;
708}
709
710
711/* End update of frame F. This function is installed as a hook in
712 update_end. */
713
714static void
715x_update_end (f)
716 struct frame *f;
717{
718 /* Mouse highlight may be displayed again. */
719 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
720
721 BLOCK_INPUT;
722 XFlush (FRAME_X_DISPLAY (f));
723 UNBLOCK_INPUT;
724}
725
726
727/* This function is called from various places in xdisp.c whenever a
728 complete update has been performed. The global variable
729 updated_window is not available here. */
730
731static void
732XTframe_up_to_date (f)
733 struct frame *f;
734{
735 if (FRAME_X_P (f))
736 {
737 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
738
739 if (dpyinfo->mouse_face_deferred_gc
740 || f == dpyinfo->mouse_face_mouse_frame)
741 {
742 BLOCK_INPUT;
743 if (dpyinfo->mouse_face_mouse_frame)
744 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
745 dpyinfo->mouse_face_mouse_x,
746 dpyinfo->mouse_face_mouse_y);
747 dpyinfo->mouse_face_deferred_gc = 0;
748 UNBLOCK_INPUT;
749 }
750 }
751}
752
753
754/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
755 arrow bitmaps, or clear the areas where they would be displayed
756 before DESIRED_ROW is made current. The window being updated is
757 found in updated_window. This function It is called from
758 update_window_line only if it is known that there are differences
759 between bitmaps to be drawn between current row and DESIRED_ROW. */
760
761static void
762x_after_update_window_line (desired_row)
763 struct glyph_row *desired_row;
764{
765 struct window *w = updated_window;
766
767 xassert (w);
768
769 if (!desired_row->mode_line_p && !w->pseudo_window_p)
770 {
771 BLOCK_INPUT;
772 x_draw_row_bitmaps (w, desired_row);
773
774 /* When a window has disappeared, make sure that no rest of
775 full-width rows stays visible in the internal border. */
776 if (windows_or_buffers_changed)
777 {
778 struct frame *f = XFRAME (w->frame);
779 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
780 int height = desired_row->visible_height;
781 int x = (window_box_right (w, -1)
782 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
783 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
784
785 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
786 x, y, width, height, False);
787 }
788
789 UNBLOCK_INPUT;
790 }
791}
792
793
794/* Draw the bitmap WHICH in one of the areas to the left or right of
795 window W. ROW is the glyph row for which to display the bitmap; it
796 determines the vertical position at which the bitmap has to be
797 drawn. */
798
799static void
800x_draw_bitmap (w, row, which)
801 struct window *w;
802 struct glyph_row *row;
803 enum bitmap_type which;
804{
805 struct frame *f = XFRAME (WINDOW_FRAME (w));
806 Display *display = FRAME_X_DISPLAY (f);
807 Window window = FRAME_X_WINDOW (f);
808 int x, y, wd, h, dy;
809 unsigned char *bits;
810 Pixmap pixmap;
811 GC gc = f->output_data.x->normal_gc;
812 struct face *face;
813 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
814
815 /* Must clip because of partially visible lines. */
816 x_clip_to_row (w, row, gc, 1);
817
818 switch (which)
819 {
820 case LEFT_TRUNCATION_BITMAP:
821 wd = left_width;
822 h = left_height;
823 bits = left_bits;
824 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
825 - wd
826 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
827 break;
828
829 case OVERLAY_ARROW_BITMAP:
830 wd = left_width;
831 h = left_height;
832 bits = ov_bits;
833 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
834 - wd
835 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
836 break;
837
838 case RIGHT_TRUNCATION_BITMAP:
839 wd = right_width;
840 h = right_height;
841 bits = right_bits;
842 x = window_box_right (w, -1);
843 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
844 break;
845
846 case CONTINUED_LINE_BITMAP:
847 wd = right_width;
848 h = right_height;
849 bits = continued_bits;
850 x = window_box_right (w, -1);
851 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
852 break;
853
854 case CONTINUATION_LINE_BITMAP:
855 wd = continuation_width;
856 h = continuation_height;
857 bits = continuation_bits;
858 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
859 - wd
860 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
861 break;
862
863 case ZV_LINE_BITMAP:
864 wd = zv_width;
865 h = zv_height;
866 bits = zv_bits;
867 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
868 - wd
869 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
870 break;
871
872 default:
873 abort ();
874 }
875
876 /* Convert to frame coordinates. Set dy to the offset in the row to
877 start drawing the bitmap. */
878 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
879 dy = (row->height - h) / 2;
880
881 /* Draw the bitmap. I believe these small pixmaps can be cached
882 by the server. */
883 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
884 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
885 face->foreground,
886 face->background, depth);
887 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
888 XFreePixmap (display, pixmap);
889 XSetClipMask (display, gc, None);
890}
891
892
893/* Draw flags bitmaps for glyph row ROW on window W. Call this
894 function with input blocked. */
895
896static void
897x_draw_row_bitmaps (w, row)
898 struct window *w;
899 struct glyph_row *row;
900{
901 struct frame *f = XFRAME (w->frame);
902 enum bitmap_type bitmap;
903 struct face *face;
904 int header_line_height = -1;
905
906 xassert (interrupt_input_blocked);
907
908 /* If row is completely invisible, because of vscrolling, we
909 don't have to draw anything. */
910 if (row->visible_height <= 0)
911 return;
912
913 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
914 PREPARE_FACE_FOR_DISPLAY (f, face);
915
916 /* Decide which bitmap to draw at the left side. */
917 if (row->overlay_arrow_p)
918 bitmap = OVERLAY_ARROW_BITMAP;
919 else if (row->truncated_on_left_p)
920 bitmap = LEFT_TRUNCATION_BITMAP;
921 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
922 bitmap = CONTINUATION_LINE_BITMAP;
923 else if (row->indicate_empty_line_p)
924 bitmap = ZV_LINE_BITMAP;
925 else
926 bitmap = NO_BITMAP;
927
928 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
929 the flags area. */
930 if (bitmap == NO_BITMAP
931 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
932 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
933 {
934 /* If W has a vertical border to its left, don't draw over it. */
935 int border = ((XFASTINT (w->left) > 0
936 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
937 ? 1 : 0);
938 int left = window_box_left (w, -1);
939
940 if (header_line_height < 0)
941 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
942
943 /* In case the same realized face is used for bitmap areas and
944 for something displayed in the text (e.g. face `region' on
945 mono-displays, the fill style may have been changed to
946 FillSolid in x_draw_glyph_string_background. */
947 if (face->stipple)
948 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
949 else
950 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
951
952 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
953 face->gc,
954 (left
955 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
956 + border),
957 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
958 row->y)),
959 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
960 row->visible_height);
961 if (!face->stipple)
962 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
963 }
964
965 /* Draw the left bitmap. */
966 if (bitmap != NO_BITMAP)
967 x_draw_bitmap (w, row, bitmap);
968
969 /* Decide which bitmap to draw at the right side. */
970 if (row->truncated_on_right_p)
971 bitmap = RIGHT_TRUNCATION_BITMAP;
972 else if (row->continued_p)
973 bitmap = CONTINUED_LINE_BITMAP;
974 else
975 bitmap = NO_BITMAP;
976
977 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
978 the flags area. */
979 if (bitmap == NO_BITMAP
980 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
981 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
982 {
983 int right = window_box_right (w, -1);
984
985 if (header_line_height < 0)
986 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
987
988 /* In case the same realized face is used for bitmap areas and
989 for something displayed in the text (e.g. face `region' on
990 mono-displays, the fill style may have been changed to
991 FillSolid in x_draw_glyph_string_background. */
992 if (face->stipple)
993 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
994 else
995 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
996 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
997 face->gc,
998 right,
999 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1000 row->y)),
1001 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1002 row->visible_height);
1003 if (!face->stipple)
1004 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1005 }
1006
1007 /* Draw the right bitmap. */
1008 if (bitmap != NO_BITMAP)
1009 x_draw_bitmap (w, row, bitmap);
1010}
1011
1012\f
1013/***********************************************************************
1014 Line Highlighting
1015 ***********************************************************************/
1016
1017/* External interface to control of standout mode. Not used for X
1018 frames. Aborts when called. */
1019
1020static void
1021XTreassert_line_highlight (new, vpos)
1022 int new, vpos;
1023{
1024 abort ();
1025}
1026
1027
1028/* Call this when about to modify line at position VPOS and change
1029 whether it is highlighted. Not used for X frames. Aborts when
1030 called. */
1031
1032static void
1033x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1034 int new_highlight, vpos, y, first_unused_hpos;
1035{
1036 abort ();
1037}
1038
1039
1040/* This is called when starting Emacs and when restarting after
1041 suspend. When starting Emacs, no X window is mapped. And nothing
1042 must be done to Emacs's own window if it is suspended (though that
1043 rarely happens). */
1044
1045static void
1046XTset_terminal_modes ()
1047{
1048}
1049
1050/* This is called when exiting or suspending Emacs. Exiting will make
1051 the X-windows go away, and suspending requires no action. */
1052
1053static void
1054XTreset_terminal_modes ()
1055{
1056}
1057
1058
1059\f
1060/***********************************************************************
1061 Output Cursor
1062 ***********************************************************************/
1063
1064/* Set the global variable output_cursor to CURSOR. All cursor
1065 positions are relative to updated_window. */
1066
1067static void
1068set_output_cursor (cursor)
1069 struct cursor_pos *cursor;
1070{
1071 output_cursor.hpos = cursor->hpos;
1072 output_cursor.vpos = cursor->vpos;
1073 output_cursor.x = cursor->x;
1074 output_cursor.y = cursor->y;
1075}
1076
1077
1078/* Set a nominal cursor position.
1079
1080 HPOS and VPOS are column/row positions in a window glyph matrix. X
1081 and Y are window text area relative pixel positions.
1082
1083 If this is done during an update, updated_window will contain the
1084 window that is being updated and the position is the future output
1085 cursor position for that window. If updated_window is null, use
1086 selected_window and display the cursor at the given position. */
1087
1088static void
1089XTcursor_to (vpos, hpos, y, x)
1090 int vpos, hpos, y, x;
1091{
1092 struct window *w;
1093
1094 /* If updated_window is not set, work on selected_window. */
1095 if (updated_window)
1096 w = updated_window;
1097 else
1098 w = XWINDOW (selected_window);
1099
1100 /* Set the output cursor. */
1101 output_cursor.hpos = hpos;
1102 output_cursor.vpos = vpos;
1103 output_cursor.x = x;
1104 output_cursor.y = y;
1105
1106 /* If not called as part of an update, really display the cursor.
1107 This will also set the cursor position of W. */
1108 if (updated_window == NULL)
1109 {
1110 BLOCK_INPUT;
1111 x_display_cursor (w, 1, hpos, vpos, x, y);
1112 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1113 UNBLOCK_INPUT;
1114 }
1115}
1116
1117
1118\f
1119/***********************************************************************
1120 Display Iterator
1121 ***********************************************************************/
1122
1123/* Function prototypes of this page. */
1124
1125static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1126 struct glyph *,
1127 XChar2b *,
1128 int *));
1129static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1130 int, XChar2b *, int));
1131static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1132static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1133static void x_append_glyph P_ ((struct it *));
1134static void x_append_composite_glyph P_ ((struct it *));
1135static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1136 int, int, double));
1137static void x_produce_glyphs P_ ((struct it *));
1138static void x_produce_image_glyph P_ ((struct it *it));
1139
1140
1141/* Return a pointer to per-char metric information in FONT of a
1142 character pointed by B which is a pointer to an XChar2b. */
1143
1144#define PER_CHAR_METRIC(font, b) \
1145 ((font)->per_char \
1146 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1147 + (((font)->min_byte1 || (font)->max_byte1) \
1148 ? (((b)->byte1 - (font)->min_byte1) \
1149 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1150 : 0)) \
1151 : &((font)->max_bounds))
1152
1153
1154/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1155 is not contained in the font. */
1156
1157static INLINE XCharStruct *
1158x_per_char_metric (font, char2b)
1159 XFontStruct *font;
1160 XChar2b *char2b;
1161{
1162 /* The result metric information. */
1163 XCharStruct *pcm = NULL;
1164
1165 xassert (font && char2b);
1166
1167 if (font->per_char != NULL)
1168 {
1169 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1170 {
1171 /* min_char_or_byte2 specifies the linear character index
1172 corresponding to the first element of the per_char array,
1173 max_char_or_byte2 is the index of the last character. A
1174 character with non-zero CHAR2B->byte1 is not in the font.
1175 A character with byte2 less than min_char_or_byte2 or
1176 greater max_char_or_byte2 is not in the font. */
1177 if (char2b->byte1 == 0
1178 && char2b->byte2 >= font->min_char_or_byte2
1179 && char2b->byte2 <= font->max_char_or_byte2)
1180 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1181 }
1182 else
1183 {
1184 /* If either min_byte1 or max_byte1 are nonzero, both
1185 min_char_or_byte2 and max_char_or_byte2 are less than
1186 256, and the 2-byte character index values corresponding
1187 to the per_char array element N (counting from 0) are:
1188
1189 byte1 = N/D + min_byte1
1190 byte2 = N\D + min_char_or_byte2
1191
1192 where:
1193
1194 D = max_char_or_byte2 - min_char_or_byte2 + 1
1195 / = integer division
1196 \ = integer modulus */
1197 if (char2b->byte1 >= font->min_byte1
1198 && char2b->byte1 <= font->max_byte1
1199 && char2b->byte2 >= font->min_char_or_byte2
1200 && char2b->byte2 <= font->max_char_or_byte2)
1201 {
1202 pcm = (font->per_char
1203 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1204 * (char2b->byte1 - font->min_byte1))
1205 + (char2b->byte2 - font->min_char_or_byte2));
1206 }
1207 }
1208 }
1209 else
1210 {
1211 /* If the per_char pointer is null, all glyphs between the first
1212 and last character indexes inclusive have the same
1213 information, as given by both min_bounds and max_bounds. */
1214 if (char2b->byte2 >= font->min_char_or_byte2
1215 && char2b->byte2 <= font->max_char_or_byte2)
1216 pcm = &font->max_bounds;
1217 }
1218
1219 return ((pcm == NULL
1220 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1221 ? NULL : pcm);
1222}
1223
1224
1225/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1226 the two-byte form of C. Encoding is returned in *CHAR2B. */
1227
1228static INLINE void
1229x_encode_char (c, char2b, font_info)
1230 int c;
1231 XChar2b *char2b;
1232 struct font_info *font_info;
1233{
1234 int charset = CHAR_CHARSET (c);
1235 XFontStruct *font = font_info->font;
1236
1237 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1238 This may be either a program in a special encoder language or a
1239 fixed encoding. */
1240 if (font_info->font_encoder)
1241 {
1242 /* It's a program. */
1243 struct ccl_program *ccl = font_info->font_encoder;
1244
1245 if (CHARSET_DIMENSION (charset) == 1)
1246 {
1247 ccl->reg[0] = charset;
1248 ccl->reg[1] = char2b->byte2;
1249 }
1250 else
1251 {
1252 ccl->reg[0] = charset;
1253 ccl->reg[1] = char2b->byte1;
1254 ccl->reg[2] = char2b->byte2;
1255 }
1256
1257 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1258
1259 /* We assume that MSBs are appropriately set/reset by CCL
1260 program. */
1261 if (font->max_byte1 == 0) /* 1-byte font */
1262 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1263 else
1264 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1265 }
1266 else if (font_info->encoding[charset])
1267 {
1268 /* Fixed encoding scheme. See fontset.h for the meaning of the
1269 encoding numbers. */
1270 int enc = font_info->encoding[charset];
1271
1272 if ((enc == 1 || enc == 2)
1273 && CHARSET_DIMENSION (charset) == 2)
1274 char2b->byte1 |= 0x80;
1275
1276 if (enc == 1 || enc == 3)
1277 char2b->byte2 |= 0x80;
1278 }
1279}
1280
1281
1282/* Get face and two-byte form of character C in face FACE_ID on frame
1283 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1284 means we want to display multibyte text. Value is a pointer to a
1285 realized face that is ready for display. */
1286
1287static INLINE struct face *
1288x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1289 struct frame *f;
1290 int c, face_id;
1291 XChar2b *char2b;
1292 int multibyte_p;
1293{
1294 struct face *face = FACE_FROM_ID (f, face_id);
1295
1296 if (!multibyte_p)
1297 {
1298 /* Unibyte case. We don't have to encode, but we have to make
1299 sure to use a face suitable for unibyte. */
1300 char2b->byte1 = 0;
1301 char2b->byte2 = c;
1302 face_id = FACE_FOR_CHAR (f, face, c);
1303 face = FACE_FROM_ID (f, face_id);
1304 }
1305 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1306 {
1307 /* Case of ASCII in a face known to fit ASCII. */
1308 char2b->byte1 = 0;
1309 char2b->byte2 = c;
1310 }
1311 else
1312 {
1313 int c1, c2, charset;
1314
1315 /* Split characters into bytes. If c2 is -1 afterwards, C is
1316 really a one-byte character so that byte1 is zero. */
1317 SPLIT_CHAR (c, charset, c1, c2);
1318 if (c2 > 0)
1319 char2b->byte1 = c1, char2b->byte2 = c2;
1320 else
1321 char2b->byte1 = 0, char2b->byte2 = c1;
1322
1323 /* Maybe encode the character in *CHAR2B. */
1324 if (face->font != NULL)
1325 {
1326 struct font_info *font_info
1327 = FONT_INFO_FROM_ID (f, face->font_info_id);
1328 if (font_info)
1329 x_encode_char (c, char2b, font_info);
1330 }
1331 }
1332
1333 /* Make sure X resources of the face are allocated. */
1334 xassert (face != NULL);
1335 PREPARE_FACE_FOR_DISPLAY (f, face);
1336
1337 return face;
1338}
1339
1340
1341/* Get face and two-byte form of character glyph GLYPH on frame F.
1342 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1343 a pointer to a realized face that is ready for display. */
1344
1345static INLINE struct face *
1346x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1347 struct frame *f;
1348 struct glyph *glyph;
1349 XChar2b *char2b;
1350 int *two_byte_p;
1351{
1352 struct face *face;
1353
1354 xassert (glyph->type == CHAR_GLYPH);
1355 face = FACE_FROM_ID (f, glyph->face_id);
1356
1357 if (two_byte_p)
1358 *two_byte_p = 0;
1359
1360 if (!glyph->multibyte_p)
1361 {
1362 /* Unibyte case. We don't have to encode, but we have to make
1363 sure to use a face suitable for unibyte. */
1364 char2b->byte1 = 0;
1365 char2b->byte2 = glyph->u.ch;
1366 }
1367 else if (glyph->u.ch < 128
1368 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1369 {
1370 /* Case of ASCII in a face known to fit ASCII. */
1371 char2b->byte1 = 0;
1372 char2b->byte2 = glyph->u.ch;
1373 }
1374 else
1375 {
1376 int c1, c2, charset;
1377
1378 /* Split characters into bytes. If c2 is -1 afterwards, C is
1379 really a one-byte character so that byte1 is zero. */
1380 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1381 if (c2 > 0)
1382 char2b->byte1 = c1, char2b->byte2 = c2;
1383 else
1384 char2b->byte1 = 0, char2b->byte2 = c1;
1385
1386 /* Maybe encode the character in *CHAR2B. */
1387 if (charset != CHARSET_ASCII)
1388 {
1389 struct font_info *font_info
1390 = FONT_INFO_FROM_ID (f, face->font_info_id);
1391 if (font_info)
1392 {
1393 x_encode_char (glyph->u.ch, char2b, font_info);
1394 if (two_byte_p)
1395 *two_byte_p
1396 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1397 }
1398 }
1399 }
1400
1401 /* Make sure X resources of the face are allocated. */
1402 xassert (face != NULL);
1403 PREPARE_FACE_FOR_DISPLAY (f, face);
1404 return face;
1405}
1406
1407
1408/* Store one glyph for IT->char_to_display in IT->glyph_row.
1409 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1410
1411static INLINE void
1412x_append_glyph (it)
1413 struct it *it;
1414{
1415 struct glyph *glyph;
1416 enum glyph_row_area area = it->area;
1417
1418 xassert (it->glyph_row);
1419 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1420
1421 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1422 if (glyph < it->glyph_row->glyphs[area + 1])
1423 {
1424 glyph->charpos = CHARPOS (it->position);
1425 glyph->object = it->object;
1426 glyph->pixel_width = it->pixel_width;
1427 glyph->voffset = it->voffset;
1428 glyph->type = CHAR_GLYPH;
1429 glyph->multibyte_p = it->multibyte_p;
1430 glyph->left_box_line_p = it->start_of_box_run_p;
1431 glyph->right_box_line_p = it->end_of_box_run_p;
1432 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1433 || it->phys_descent > it->descent);
1434 glyph->padding_p = 0;
1435 glyph->glyph_not_available_p = it->glyph_not_available_p;
1436 glyph->face_id = it->face_id;
1437 glyph->u.ch = it->char_to_display;
1438 ++it->glyph_row->used[area];
1439 }
1440}
1441
1442/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1443 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1444
1445static INLINE void
1446x_append_composite_glyph (it)
1447 struct it *it;
1448{
1449 struct glyph *glyph;
1450 enum glyph_row_area area = it->area;
1451
1452 xassert (it->glyph_row);
1453
1454 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1455 if (glyph < it->glyph_row->glyphs[area + 1])
1456 {
1457 glyph->charpos = CHARPOS (it->position);
1458 glyph->object = it->object;
1459 glyph->pixel_width = it->pixel_width;
1460 glyph->voffset = it->voffset;
1461 glyph->type = COMPOSITE_GLYPH;
1462 glyph->multibyte_p = it->multibyte_p;
1463 glyph->left_box_line_p = it->start_of_box_run_p;
1464 glyph->right_box_line_p = it->end_of_box_run_p;
1465 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1466 || it->phys_descent > it->descent);
1467 glyph->padding_p = 0;
1468 glyph->glyph_not_available_p = 0;
1469 glyph->face_id = it->face_id;
1470 glyph->u.cmp_id = it->cmp_id;
1471 ++it->glyph_row->used[area];
1472 }
1473}
1474
1475
1476/* Change IT->ascent and IT->height according to the setting of
1477 IT->voffset. */
1478
1479static INLINE void
1480take_vertical_position_into_account (it)
1481 struct it *it;
1482{
1483 if (it->voffset)
1484 {
1485 if (it->voffset < 0)
1486 /* Increase the ascent so that we can display the text higher
1487 in the line. */
1488 it->ascent += abs (it->voffset);
1489 else
1490 /* Increase the descent so that we can display the text lower
1491 in the line. */
1492 it->descent += it->voffset;
1493 }
1494}
1495
1496
1497/* Produce glyphs/get display metrics for the image IT is loaded with.
1498 See the description of struct display_iterator in dispextern.h for
1499 an overview of struct display_iterator. */
1500
1501static void
1502x_produce_image_glyph (it)
1503 struct it *it;
1504{
1505 struct image *img;
1506 struct face *face;
1507
1508 xassert (it->what == IT_IMAGE);
1509
1510 face = FACE_FROM_ID (it->f, it->face_id);
1511 img = IMAGE_FROM_ID (it->f, it->image_id);
1512 xassert (img);
1513
1514 /* Make sure X resources of the face and image are loaded. */
1515 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1516 prepare_image_for_display (it->f, img);
1517
1518 it->ascent = it->phys_ascent = image_ascent (img, face);
1519 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1520 it->pixel_width = img->width + 2 * img->margin;
1521
1522 it->nglyphs = 1;
1523
1524 if (face->box != FACE_NO_BOX)
1525 {
1526 it->ascent += face->box_line_width;
1527 it->descent += face->box_line_width;
1528
1529 if (it->start_of_box_run_p)
1530 it->pixel_width += face->box_line_width;
1531 if (it->end_of_box_run_p)
1532 it->pixel_width += face->box_line_width;
1533 }
1534
1535 take_vertical_position_into_account (it);
1536
1537 if (it->glyph_row)
1538 {
1539 struct glyph *glyph;
1540 enum glyph_row_area area = it->area;
1541
1542 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1543 if (glyph < it->glyph_row->glyphs[area + 1])
1544 {
1545 glyph->charpos = CHARPOS (it->position);
1546 glyph->object = it->object;
1547 glyph->pixel_width = it->pixel_width;
1548 glyph->voffset = it->voffset;
1549 glyph->type = IMAGE_GLYPH;
1550 glyph->multibyte_p = it->multibyte_p;
1551 glyph->left_box_line_p = it->start_of_box_run_p;
1552 glyph->right_box_line_p = it->end_of_box_run_p;
1553 glyph->overlaps_vertically_p = 0;
1554 glyph->padding_p = 0;
1555 glyph->glyph_not_available_p = 0;
1556 glyph->face_id = it->face_id;
1557 glyph->u.img_id = img->id;
1558 ++it->glyph_row->used[area];
1559 }
1560 }
1561}
1562
1563
1564/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1565 of the glyph, WIDTH and HEIGHT are the width and height of the
1566 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1567 ascent of the glyph (0 <= ASCENT <= 1). */
1568
1569static void
1570x_append_stretch_glyph (it, object, width, height, ascent)
1571 struct it *it;
1572 Lisp_Object object;
1573 int width, height;
1574 double ascent;
1575{
1576 struct glyph *glyph;
1577 enum glyph_row_area area = it->area;
1578
1579 xassert (ascent >= 0 && ascent <= 1);
1580
1581 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1582 if (glyph < it->glyph_row->glyphs[area + 1])
1583 {
1584 glyph->charpos = CHARPOS (it->position);
1585 glyph->object = object;
1586 glyph->pixel_width = width;
1587 glyph->voffset = it->voffset;
1588 glyph->type = STRETCH_GLYPH;
1589 glyph->multibyte_p = it->multibyte_p;
1590 glyph->left_box_line_p = it->start_of_box_run_p;
1591 glyph->right_box_line_p = it->end_of_box_run_p;
1592 glyph->overlaps_vertically_p = 0;
1593 glyph->padding_p = 0;
1594 glyph->glyph_not_available_p = 0;
1595 glyph->face_id = it->face_id;
1596 glyph->u.stretch.ascent = height * ascent;
1597 glyph->u.stretch.height = height;
1598 ++it->glyph_row->used[area];
1599 }
1600}
1601
1602
1603/* Produce a stretch glyph for iterator IT. IT->object is the value
1604 of the glyph property displayed. The value must be a list
1605 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1606 being recognized:
1607
1608 1. `:width WIDTH' specifies that the space should be WIDTH *
1609 canonical char width wide. WIDTH may be an integer or floating
1610 point number.
1611
1612 2. `:relative-width FACTOR' specifies that the width of the stretch
1613 should be computed from the width of the first character having the
1614 `glyph' property, and should be FACTOR times that width.
1615
1616 3. `:align-to HPOS' specifies that the space should be wide enough
1617 to reach HPOS, a value in canonical character units.
1618
1619 Exactly one of the above pairs must be present.
1620
1621 4. `:height HEIGHT' specifies that the height of the stretch produced
1622 should be HEIGHT, measured in canonical character units.
1623
1624 5. `:relative-height FACTOR' specifies that the height of the the
1625 stretch should be FACTOR times the height of the characters having
1626 the glyph property.
1627
1628 Either none or exactly one of 4 or 5 must be present.
1629
1630 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1631 of the stretch should be used for the ascent of the stretch.
1632 ASCENT must be in the range 0 <= ASCENT <= 100. */
1633
1634#define NUMVAL(X) \
1635 ((INTEGERP (X) || FLOATP (X)) \
1636 ? XFLOATINT (X) \
1637 : - 1)
1638
1639
1640static void
1641x_produce_stretch_glyph (it)
1642 struct it *it;
1643{
1644 /* (space :width WIDTH :height HEIGHT. */
1645#if GLYPH_DEBUG
1646 extern Lisp_Object Qspace;
1647#endif
1648 extern Lisp_Object QCwidth, QCheight, QCascent;
1649 extern Lisp_Object QCrelative_width, QCrelative_height;
1650 extern Lisp_Object QCalign_to;
1651 Lisp_Object prop, plist;
1652 double width = 0, height = 0, ascent = 0;
1653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1655
1656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1657
1658 /* List should start with `space'. */
1659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1660 plist = XCDR (it->object);
1661
1662 /* Compute the width of the stretch. */
1663 if (prop = Fplist_get (plist, QCwidth),
1664 NUMVAL (prop) > 0)
1665 /* Absolute width `:width WIDTH' specified and valid. */
1666 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_width),
1668 NUMVAL (prop) > 0)
1669 {
1670 /* Relative width `:relative-width FACTOR' specified and valid.
1671 Compute the width of the characters having the `glyph'
1672 property. */
1673 struct it it2;
1674 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1675
1676 it2 = *it;
1677 if (it->multibyte_p)
1678 {
1679 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1680 - IT_BYTEPOS (*it));
1681 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1682 }
1683 else
1684 it2.c = *p, it2.len = 1;
1685
1686 it2.glyph_row = NULL;
1687 it2.what = IT_CHARACTER;
1688 x_produce_glyphs (&it2);
1689 width = NUMVAL (prop) * it2.pixel_width;
1690 }
1691 else if (prop = Fplist_get (plist, QCalign_to),
1692 NUMVAL (prop) > 0)
1693 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1694 else
1695 /* Nothing specified -> width defaults to canonical char width. */
1696 width = CANON_X_UNIT (it->f);
1697
1698 /* Compute height. */
1699 if (prop = Fplist_get (plist, QCheight),
1700 NUMVAL (prop) > 0)
1701 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1702 else if (prop = Fplist_get (plist, QCrelative_height),
1703 NUMVAL (prop) > 0)
1704 height = FONT_HEIGHT (font) * NUMVAL (prop);
1705 else
1706 height = FONT_HEIGHT (font);
1707
1708 /* Compute percentage of height used for ascent. If
1709 `:ascent ASCENT' is present and valid, use that. Otherwise,
1710 derive the ascent from the font in use. */
1711 if (prop = Fplist_get (plist, QCascent),
1712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1713 ascent = NUMVAL (prop) / 100.0;
1714 else
1715 ascent = (double) font->ascent / FONT_HEIGHT (font);
1716
1717 if (width <= 0)
1718 width = 1;
1719 if (height <= 0)
1720 height = 1;
1721
1722 if (it->glyph_row)
1723 {
1724 Lisp_Object object = it->stack[it->sp - 1].string;
1725 if (!STRINGP (object))
1726 object = it->w->buffer;
1727 x_append_stretch_glyph (it, object, width, height, ascent);
1728 }
1729
1730 it->pixel_width = width;
1731 it->ascent = it->phys_ascent = height * ascent;
1732 it->descent = it->phys_descent = height - it->ascent;
1733 it->nglyphs = 1;
1734
1735 if (face->box != FACE_NO_BOX)
1736 {
1737 it->ascent += face->box_line_width;
1738 it->descent += face->box_line_width;
1739
1740 if (it->start_of_box_run_p)
1741 it->pixel_width += face->box_line_width;
1742 if (it->end_of_box_run_p)
1743 it->pixel_width += face->box_line_width;
1744 }
1745
1746 take_vertical_position_into_account (it);
1747}
1748
1749/* Return proper value to be used as baseline offset of font that has
1750 ASCENT and DESCENT to draw characters by the font at the vertical
1751 center of the line of frame F.
1752
1753 Here, out task is to find the value of BOFF in the following figure;
1754
1755 -------------------------+-----------+-
1756 -+-+---------+-+ | |
1757 | | | | | |
1758 | | | | F_ASCENT F_HEIGHT
1759 | | | ASCENT | |
1760 HEIGHT | | | | |
1761 | | |-|-+------+-----------|------- baseline
1762 | | | | BOFF | |
1763 | |---------|-+-+ | |
1764 | | | DESCENT | |
1765 -+-+---------+-+ F_DESCENT |
1766 -------------------------+-----------+-
1767
1768 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1769 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1770 DESCENT = FONT->descent
1771 HEIGHT = FONT_HEIGHT (FONT)
1772 F_DESCENT = (F->output_data.x->font->descent
1773 - F->output_data.x->baseline_offset)
1774 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1775*/
1776
1777#define VCENTER_BASELINE_OFFSET(FONT, F) \
1778 ((FONT)->descent \
1779 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1780 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1781
1782/* Produce glyphs/get display metrics for the display element IT is
1783 loaded with. See the description of struct display_iterator in
1784 dispextern.h for an overview of struct display_iterator. */
1785
1786static void
1787x_produce_glyphs (it)
1788 struct it *it;
1789{
1790 it->glyph_not_available_p = 0;
1791
1792 if (it->what == IT_CHARACTER)
1793 {
1794 XChar2b char2b;
1795 XFontStruct *font;
1796 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1797 XCharStruct *pcm;
1798 int font_not_found_p;
1799 struct font_info *font_info;
1800 int boff; /* baseline offset */
1801
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->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1814 face = FACE_FROM_ID (it->f, it->face_id);
1815 }
1816 else if (!SINGLE_BYTE_CHAR_P (it->c)
1817 && !it->multibyte_p)
1818 {
1819 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1820 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1821 face = FACE_FROM_ID (it->f, it->face_id);
1822 }
1823 }
1824
1825 /* Get font to use. Encode IT->char_to_display. */
1826 x_get_char_face_and_encoding (it->f, it->char_to_display,
1827 it->face_id, &char2b,
1828 it->multibyte_p);
1829 font = face->font;
1830
1831 /* When no suitable font found, use the default font. */
1832 font_not_found_p = font == NULL;
1833 if (font_not_found_p)
1834 {
1835 font = FRAME_FONT (it->f);
1836 boff = it->f->output_data.x->baseline_offset;
1837 font_info = NULL;
1838 }
1839 else
1840 {
1841 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1842 boff = font_info->baseline_offset;
1843 if (font_info->vertical_centering)
1844 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1845 }
1846
1847 if (it->char_to_display >= ' '
1848 && (!it->multibyte_p || it->char_to_display < 128))
1849 {
1850 /* Either unibyte or ASCII. */
1851 int stretched_p;
1852
1853 it->nglyphs = 1;
1854
1855 pcm = x_per_char_metric (font, &char2b);
1856 it->ascent = font->ascent + boff;
1857 it->descent = font->descent - boff;
1858
1859 if (pcm)
1860 {
1861 it->phys_ascent = pcm->ascent + boff;
1862 it->phys_descent = pcm->descent - boff;
1863 it->pixel_width = pcm->width;
1864 }
1865 else
1866 {
1867 it->glyph_not_available_p = 1;
1868 it->phys_ascent = font->ascent + boff;
1869 it->phys_descent = font->descent - boff;
1870 it->pixel_width = FONT_WIDTH (font);
1871 }
1872
1873 /* If this is a space inside a region of text with
1874 `space-width' property, change its width. */
1875 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1876 if (stretched_p)
1877 it->pixel_width *= XFLOATINT (it->space_width);
1878
1879 /* If face has a box, add the box thickness to the character
1880 height. If character has a box line to the left and/or
1881 right, add the box line width to the character's width. */
1882 if (face->box != FACE_NO_BOX)
1883 {
1884 int thick = face->box_line_width;
1885
1886 it->ascent += thick;
1887 it->descent += thick;
1888
1889 if (it->start_of_box_run_p)
1890 it->pixel_width += thick;
1891 if (it->end_of_box_run_p)
1892 it->pixel_width += thick;
1893 }
1894
1895 /* If face has an overline, add the height of the overline
1896 (1 pixel) and a 1 pixel margin to the character height. */
1897 if (face->overline_p)
1898 it->ascent += 2;
1899
1900 take_vertical_position_into_account (it);
1901
1902 /* If we have to actually produce glyphs, do it. */
1903 if (it->glyph_row)
1904 {
1905 if (stretched_p)
1906 {
1907 /* Translate a space with a `space-width' property
1908 into a stretch glyph. */
1909 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1910 x_append_stretch_glyph (it, it->object, it->pixel_width,
1911 it->ascent + it->descent, ascent);
1912 }
1913 else
1914 x_append_glyph (it);
1915
1916 /* If characters with lbearing or rbearing are displayed
1917 in this line, record that fact in a flag of the
1918 glyph row. This is used to optimize X output code. */
1919 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1920 it->glyph_row->contains_overlapping_glyphs_p = 1;
1921 }
1922 }
1923 else if (it->char_to_display == '\n')
1924 {
1925 /* A newline has no width but we need the height of the line. */
1926 it->pixel_width = 0;
1927 it->nglyphs = 0;
1928 it->ascent = it->phys_ascent = font->ascent + boff;
1929 it->descent = it->phys_descent = font->descent - boff;
1930
1931 if (face->box != FACE_NO_BOX)
1932 {
1933 int thick = face->box_line_width;
1934 it->ascent += thick;
1935 it->descent += thick;
1936 }
1937 }
1938 else if (it->char_to_display == '\t')
1939 {
1940 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1941 int x = it->current_x + it->continuation_lines_width;
1942 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1943
1944 /* If the distance from the current position to the next tab
1945 stop is less than a canonical character width, use the
1946 tab stop after that. */
1947 if (next_tab_x - x < CANON_X_UNIT (it->f))
1948 next_tab_x += tab_width;
1949
1950 it->pixel_width = next_tab_x - x;
1951 it->nglyphs = 1;
1952 it->ascent = it->phys_ascent = font->ascent + boff;
1953 it->descent = it->phys_descent = font->descent - boff;
1954
1955 if (it->glyph_row)
1956 {
1957 double ascent = (double) it->ascent / (it->ascent + it->descent);
1958 x_append_stretch_glyph (it, it->object, it->pixel_width,
1959 it->ascent + it->descent, ascent);
1960 }
1961 }
1962 else
1963 {
1964 /* A multi-byte character. Assume that the display width of the
1965 character is the width of the character multiplied by the
1966 width of the font. */
1967
1968 /* If we found a font, this font should give us the right
1969 metrics. If we didn't find a font, use the frame's
1970 default font and calculate the width of the character
1971 from the charset width; this is what old redisplay code
1972 did. */
1973 pcm = x_per_char_metric (font, &char2b);
1974 if (font_not_found_p || !pcm)
1975 {
1976 int charset = CHAR_CHARSET (it->char_to_display);
1977
1978 it->glyph_not_available_p = 1;
1979 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1980 * CHARSET_WIDTH (charset));
1981 it->phys_ascent = font->ascent + boff;
1982 it->phys_descent = font->descent - boff;
1983 }
1984 else
1985 {
1986 it->pixel_width = pcm->width;
1987 it->phys_ascent = pcm->ascent + boff;
1988 it->phys_descent = pcm->descent - boff;
1989 if (it->glyph_row
1990 && (pcm->lbearing < 0
1991 || pcm->rbearing > pcm->width))
1992 it->glyph_row->contains_overlapping_glyphs_p = 1;
1993 }
1994 it->nglyphs = 1;
1995 it->ascent = font->ascent + boff;
1996 it->descent = font->descent - boff;
1997 if (face->box != FACE_NO_BOX)
1998 {
1999 int thick = face->box_line_width;
2000 it->ascent += thick;
2001 it->descent += thick;
2002
2003 if (it->start_of_box_run_p)
2004 it->pixel_width += thick;
2005 if (it->end_of_box_run_p)
2006 it->pixel_width += thick;
2007 }
2008
2009 /* If face has an overline, add the height of the overline
2010 (1 pixel) and a 1 pixel margin to the character height. */
2011 if (face->overline_p)
2012 it->ascent += 2;
2013
2014 take_vertical_position_into_account (it);
2015
2016 if (it->glyph_row)
2017 x_append_glyph (it);
2018 }
2019 }
2020 else if (it->what == IT_COMPOSITION)
2021 {
2022 /* Note: A composition is represented as one glyph in the
2023 glyph matrix. There are no padding glyphs. */
2024 XChar2b char2b;
2025 XFontStruct *font;
2026 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2027 XCharStruct *pcm;
2028 int font_not_found_p;
2029 struct font_info *font_info;
2030 int boff; /* baseline offset */
2031 struct composition *cmp = composition_table[it->cmp_id];
2032
2033 /* Maybe translate single-byte characters to multibyte. */
2034 it->char_to_display = it->c;
2035 if (unibyte_display_via_language_environment
2036 && SINGLE_BYTE_CHAR_P (it->c)
2037 && (it->c >= 0240
2038 || (it->c >= 0200
2039 && !NILP (Vnonascii_translation_table))))
2040 {
2041 it->char_to_display = unibyte_char_to_multibyte (it->c);
2042 }
2043
2044 /* Get face and font to use. Encode IT->char_to_display. */
2045 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2046 face = FACE_FROM_ID (it->f, it->face_id);
2047 x_get_char_face_and_encoding (it->f, it->char_to_display,
2048 it->face_id, &char2b, it->multibyte_p);
2049 font = face->font;
2050
2051 /* When no suitable font found, use the default font. */
2052 font_not_found_p = font == NULL;
2053 if (font_not_found_p)
2054 {
2055 font = FRAME_FONT (it->f);
2056 boff = it->f->output_data.x->baseline_offset;
2057 font_info = NULL;
2058 }
2059 else
2060 {
2061 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2062 boff = font_info->baseline_offset;
2063 if (font_info->vertical_centering)
2064 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2065 }
2066
2067 /* There are no padding glyphs, so there is only one glyph to
2068 produce for the composition. Important is that pixel_width,
2069 ascent and descent are the values of what is drawn by
2070 draw_glyphs (i.e. the values of the overall glyphs composed). */
2071 it->nglyphs = 1;
2072
2073 /* If we have not yet calculated pixel size data of glyphs of
2074 the composition for the current face font, calculate them
2075 now. Theoretically, we have to check all fonts for the
2076 glyphs, but that requires much time and memory space. So,
2077 here we check only the font of the first glyph. This leads
2078 to incorrect display very rarely, and C-l (recenter) can
2079 correct the display anyway. */
2080 if (cmp->font != (void *) font)
2081 {
2082 /* Ascent and descent of the font of the first character of
2083 this composition (adjusted by baseline offset). Ascent
2084 and descent of overall glyphs should not be less than
2085 them respectively. */
2086 int font_ascent = font->ascent + boff;
2087 int font_descent = font->descent - boff;
2088 /* Bounding box of the overall glyphs. */
2089 int leftmost, rightmost, lowest, highest;
2090 int i, width, ascent, descent;
2091
2092 cmp->font = (void *) font;
2093
2094 /* Initialize the bounding box. */
2095 pcm = x_per_char_metric (font, &char2b);
2096 if (pcm)
2097 {
2098 width = pcm->width;
2099 ascent = pcm->ascent;
2100 descent = pcm->descent;
2101 }
2102 else
2103 {
2104 width = FONT_WIDTH (font);
2105 ascent = font->ascent;
2106 descent = font->descent;
2107 }
2108
2109 rightmost = width;
2110 lowest = - descent + boff;
2111 highest = ascent + boff;
2112 leftmost = 0;
2113
2114 if (font_info
2115 && font_info->default_ascent
2116 && CHAR_TABLE_P (Vuse_default_ascent)
2117 && !NILP (Faref (Vuse_default_ascent,
2118 make_number (it->char_to_display))))
2119 highest = font_info->default_ascent + boff;
2120
2121 /* Draw the first glyph at the normal position. It may be
2122 shifted to right later if some other glyphs are drawn at
2123 the left. */
2124 cmp->offsets[0] = 0;
2125 cmp->offsets[1] = boff;
2126
2127 /* Set cmp->offsets for the remaining glyphs. */
2128 for (i = 1; i < cmp->glyph_len; i++)
2129 {
2130 int left, right, btm, top;
2131 int ch = COMPOSITION_GLYPH (cmp, i);
2132 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2133
2134 face = FACE_FROM_ID (it->f, face_id);
2135 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2136 it->multibyte_p);
2137 font = face->font;
2138 if (font == NULL)
2139 {
2140 font = FRAME_FONT (it->f);
2141 boff = it->f->output_data.x->baseline_offset;
2142 font_info = NULL;
2143 }
2144 else
2145 {
2146 font_info
2147 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2148 boff = font_info->baseline_offset;
2149 if (font_info->vertical_centering)
2150 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2151 }
2152
2153 pcm = x_per_char_metric (font, &char2b);
2154 if (pcm)
2155 {
2156 width = pcm->width;
2157 ascent = pcm->ascent;
2158 descent = pcm->descent;
2159 }
2160 else
2161 {
2162 width = FONT_WIDTH (font);
2163 ascent = font->ascent;
2164 descent = font->descent;
2165 }
2166
2167 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2168 {
2169 /* Relative composition with or without
2170 alternate chars. */
2171 left = (leftmost + rightmost - width) / 2;
2172 btm = - descent + boff;
2173 if (font_info && font_info->relative_compose
2174 && (! CHAR_TABLE_P (Vignore_relative_composition)
2175 || NILP (Faref (Vignore_relative_composition,
2176 make_number (ch)))))
2177 {
2178
2179 if (- descent >= font_info->relative_compose)
2180 /* One extra pixel between two glyphs. */
2181 btm = highest + 1;
2182 else if (ascent <= 0)
2183 /* One extra pixel between two glyphs. */
2184 btm = lowest - 1 - ascent - descent;
2185 }
2186 }
2187 else
2188 {
2189 /* A composition rule is specified by an integer
2190 value that encodes global and new reference
2191 points (GREF and NREF). GREF and NREF are
2192 specified by numbers as below:
2193
2194 0---1---2 -- ascent
2195 | |
2196 | |
2197 | |
2198 9--10--11 -- center
2199 | |
2200 ---3---4---5--- baseline
2201 | |
2202 6---7---8 -- descent
2203 */
2204 int rule = COMPOSITION_RULE (cmp, i);
2205 int gref, nref, grefx, grefy, nrefx, nrefy;
2206
2207 COMPOSITION_DECODE_RULE (rule, gref, nref);
2208 grefx = gref % 3, nrefx = nref % 3;
2209 grefy = gref / 3, nrefy = nref / 3;
2210
2211 left = (leftmost
2212 + grefx * (rightmost - leftmost) / 2
2213 - nrefx * width / 2);
2214 btm = ((grefy == 0 ? highest
2215 : grefy == 1 ? 0
2216 : grefy == 2 ? lowest
2217 : (highest + lowest) / 2)
2218 - (nrefy == 0 ? ascent + descent
2219 : nrefy == 1 ? descent - boff
2220 : nrefy == 2 ? 0
2221 : (ascent + descent) / 2));
2222 }
2223
2224 cmp->offsets[i * 2] = left;
2225 cmp->offsets[i * 2 + 1] = btm + descent;
2226
2227 /* Update the bounding box of the overall glyphs. */
2228 right = left + width;
2229 top = btm + descent + ascent;
2230 if (left < leftmost)
2231 leftmost = left;
2232 if (right > rightmost)
2233 rightmost = right;
2234 if (top > highest)
2235 highest = top;
2236 if (btm < lowest)
2237 lowest = btm;
2238 }
2239
2240 /* If there are glyphs whose x-offsets are negative,
2241 shift all glyphs to the right and make all x-offsets
2242 non-negative. */
2243 if (leftmost < 0)
2244 {
2245 for (i = 0; i < cmp->glyph_len; i++)
2246 cmp->offsets[i * 2] -= leftmost;
2247 rightmost -= leftmost;
2248 }
2249
2250 cmp->pixel_width = rightmost;
2251 cmp->ascent = highest;
2252 cmp->descent = - lowest;
2253 if (cmp->ascent < font_ascent)
2254 cmp->ascent = font_ascent;
2255 if (cmp->descent < font_descent)
2256 cmp->descent = font_descent;
2257 }
2258
2259 it->pixel_width = cmp->pixel_width;
2260 it->ascent = it->phys_ascent = cmp->ascent;
2261 it->descent = it->phys_descent = cmp->descent;
2262
2263 if (face->box != FACE_NO_BOX)
2264 {
2265 int thick = face->box_line_width;
2266 it->ascent += thick;
2267 it->descent += thick;
2268
2269 if (it->start_of_box_run_p)
2270 it->pixel_width += thick;
2271 if (it->end_of_box_run_p)
2272 it->pixel_width += thick;
2273 }
2274
2275 /* If face has an overline, add the height of the overline
2276 (1 pixel) and a 1 pixel margin to the character height. */
2277 if (face->overline_p)
2278 it->ascent += 2;
2279
2280 take_vertical_position_into_account (it);
2281
2282 if (it->glyph_row)
2283 x_append_composite_glyph (it);
2284 }
2285 else if (it->what == IT_IMAGE)
2286 x_produce_image_glyph (it);
2287 else if (it->what == IT_STRETCH)
2288 x_produce_stretch_glyph (it);
2289
2290 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2291 because this isn't true for images with `:ascent 100'. */
2292 xassert (it->ascent >= 0 && it->descent >= 0);
2293 if (it->area == TEXT_AREA)
2294 it->current_x += it->pixel_width;
2295
2296 it->descent += it->extra_line_spacing;
2297
2298 it->max_ascent = max (it->max_ascent, it->ascent);
2299 it->max_descent = max (it->max_descent, it->descent);
2300 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2301 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2302}
2303
2304
2305/* Estimate the pixel height of the mode or top line on frame F.
2306 FACE_ID specifies what line's height to estimate. */
2307
2308int
2309x_estimate_mode_line_height (f, face_id)
2310 struct frame *f;
2311 enum face_id face_id;
2312{
2313 int height = 1;
2314
2315 /* This function is called so early when Emacs starts that the face
2316 cache and mode line face are not yet initialized. */
2317 if (FRAME_FACE_CACHE (f))
2318 {
2319 struct face *face = FACE_FROM_ID (f, face_id);
2320 if (face)
2321 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2322 }
2323
2324 return height;
2325}
2326
2327\f
2328/***********************************************************************
2329 Glyph display
2330 ***********************************************************************/
2331
2332/* A sequence of glyphs to be drawn in the same face.
2333
2334 This data structure is not really completely X specific, so it
2335 could possibly, at least partially, be useful for other systems. It
2336 is currently not part of the external redisplay interface because
2337 it's not clear what other systems will need. */
2338
2339struct glyph_string
2340{
2341 /* X-origin of the string. */
2342 int x;
2343
2344 /* Y-origin and y-position of the base line of this string. */
2345 int y, ybase;
2346
2347 /* The width of the string, not including a face extension. */
2348 int width;
2349
2350 /* The width of the string, including a face extension. */
2351 int background_width;
2352
2353 /* The height of this string. This is the height of the line this
2354 string is drawn in, and can be different from the height of the
2355 font the string is drawn in. */
2356 int height;
2357
2358 /* Number of pixels this string overwrites in front of its x-origin.
2359 This number is zero if the string has an lbearing >= 0; it is
2360 -lbearing, if the string has an lbearing < 0. */
2361 int left_overhang;
2362
2363 /* Number of pixels this string overwrites past its right-most
2364 nominal x-position, i.e. x + width. Zero if the string's
2365 rbearing is <= its nominal width, rbearing - width otherwise. */
2366 int right_overhang;
2367
2368 /* The frame on which the glyph string is drawn. */
2369 struct frame *f;
2370
2371 /* The window on which the glyph string is drawn. */
2372 struct window *w;
2373
2374 /* X display and window for convenience. */
2375 Display *display;
2376 Window window;
2377
2378 /* The glyph row for which this string was built. It determines the
2379 y-origin and height of the string. */
2380 struct glyph_row *row;
2381
2382 /* The area within row. */
2383 enum glyph_row_area area;
2384
2385 /* Characters to be drawn, and number of characters. */
2386 XChar2b *char2b;
2387 int nchars;
2388
2389 /* A face-override for drawing cursors, mouse face and similar. */
2390 enum draw_glyphs_face hl;
2391
2392 /* Face in which this string is to be drawn. */
2393 struct face *face;
2394
2395 /* Font in which this string is to be drawn. */
2396 XFontStruct *font;
2397
2398 /* Font info for this string. */
2399 struct font_info *font_info;
2400
2401 /* Non-null means this string describes (part of) a composition.
2402 All characters from char2b are drawn composed. */
2403 struct composition *cmp;
2404
2405 /* Index of this glyph string's first character in the glyph
2406 definition of CMP. If this is zero, this glyph string describes
2407 the first character of a composition. */
2408 int gidx;
2409
2410 /* 1 means this glyph strings face has to be drawn to the right end
2411 of the window's drawing area. */
2412 unsigned extends_to_end_of_line_p : 1;
2413
2414 /* 1 means the background of this string has been drawn. */
2415 unsigned background_filled_p : 1;
2416
2417 /* 1 means glyph string must be drawn with 16-bit functions. */
2418 unsigned two_byte_p : 1;
2419
2420 /* 1 means that the original font determined for drawing this glyph
2421 string could not be loaded. The member `font' has been set to
2422 the frame's default font in this case. */
2423 unsigned font_not_found_p : 1;
2424
2425 /* 1 means that the face in which this glyph string is drawn has a
2426 stipple pattern. */
2427 unsigned stippled_p : 1;
2428
2429 /* 1 means only the foreground of this glyph string must be drawn,
2430 and we should use the physical height of the line this glyph
2431 string appears in as clip rect. */
2432 unsigned for_overlaps_p : 1;
2433
2434 /* The GC to use for drawing this glyph string. */
2435 GC gc;
2436
2437 /* A pointer to the first glyph in the string. This glyph
2438 corresponds to char2b[0]. Needed to draw rectangles if
2439 font_not_found_p is 1. */
2440 struct glyph *first_glyph;
2441
2442 /* Image, if any. */
2443 struct image *img;
2444
2445 struct glyph_string *next, *prev;
2446};
2447
2448
2449#if 0
2450
2451static void
2452x_dump_glyph_string (s)
2453 struct glyph_string *s;
2454{
2455 fprintf (stderr, "glyph string\n");
2456 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2457 s->x, s->y, s->width, s->height);
2458 fprintf (stderr, " ybase = %d\n", s->ybase);
2459 fprintf (stderr, " hl = %d\n", s->hl);
2460 fprintf (stderr, " left overhang = %d, right = %d\n",
2461 s->left_overhang, s->right_overhang);
2462 fprintf (stderr, " nchars = %d\n", s->nchars);
2463 fprintf (stderr, " extends to end of line = %d\n",
2464 s->extends_to_end_of_line_p);
2465 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2466 fprintf (stderr, " bg width = %d\n", s->background_width);
2467}
2468
2469#endif /* GLYPH_DEBUG */
2470
2471
2472
2473static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2474 struct glyph_string **,
2475 struct glyph_string *,
2476 struct glyph_string *));
2477static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2478 struct glyph_string **,
2479 struct glyph_string *,
2480 struct glyph_string *));
2481static void x_append_glyph_string P_ ((struct glyph_string **,
2482 struct glyph_string **,
2483 struct glyph_string *));
2484static int x_left_overwritten P_ ((struct glyph_string *));
2485static int x_left_overwriting P_ ((struct glyph_string *));
2486static int x_right_overwritten P_ ((struct glyph_string *));
2487static int x_right_overwriting P_ ((struct glyph_string *));
2488static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2489 int));
2490static void x_init_glyph_string P_ ((struct glyph_string *,
2491 XChar2b *, struct window *,
2492 struct glyph_row *,
2493 enum glyph_row_area, int,
2494 enum draw_glyphs_face));
2495static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2496 enum glyph_row_area, int, int,
2497 enum draw_glyphs_face, int *, int *, int));
2498static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2499static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2500static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2501 int));
2502static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2503static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2504static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2505static void x_draw_glyph_string P_ ((struct glyph_string *));
2506static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2507static void x_set_cursor_gc P_ ((struct glyph_string *));
2508static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2509static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2510static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2511 int *, int *));
2512static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2513static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2514 unsigned long *, double, int));
2515static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2516 double, int, unsigned long));
2517static void x_setup_relief_colors P_ ((struct glyph_string *));
2518static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2519static void x_draw_image_relief P_ ((struct glyph_string *));
2520static void x_draw_image_foreground P_ ((struct glyph_string *));
2521static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2522static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2523static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2524 int, int, int));
2525static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2526 int, int, int, int, XRectangle *));
2527static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2528 int, int, int, XRectangle *));
2529static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2530 enum glyph_row_area));
2531static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2532 struct glyph_row *,
2533 enum glyph_row_area, int, int));
2534
2535#if GLYPH_DEBUG
2536static void x_check_font P_ ((struct frame *, XFontStruct *));
2537#endif
2538
2539
2540/* Append the list of glyph strings with head H and tail T to the list
2541 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2542
2543static INLINE void
2544x_append_glyph_string_lists (head, tail, h, t)
2545 struct glyph_string **head, **tail;
2546 struct glyph_string *h, *t;
2547{
2548 if (h)
2549 {
2550 if (*head)
2551 (*tail)->next = h;
2552 else
2553 *head = h;
2554 h->prev = *tail;
2555 *tail = t;
2556 }
2557}
2558
2559
2560/* Prepend the list of glyph strings with head H and tail T to the
2561 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2562 result. */
2563
2564static INLINE void
2565x_prepend_glyph_string_lists (head, tail, h, t)
2566 struct glyph_string **head, **tail;
2567 struct glyph_string *h, *t;
2568{
2569 if (h)
2570 {
2571 if (*head)
2572 (*head)->prev = t;
2573 else
2574 *tail = t;
2575 t->next = *head;
2576 *head = h;
2577 }
2578}
2579
2580
2581/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2582 Set *HEAD and *TAIL to the resulting list. */
2583
2584static INLINE void
2585x_append_glyph_string (head, tail, s)
2586 struct glyph_string **head, **tail;
2587 struct glyph_string *s;
2588{
2589 s->next = s->prev = NULL;
2590 x_append_glyph_string_lists (head, tail, s, s);
2591}
2592
2593
2594/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2595 face. */
2596
2597static void
2598x_set_cursor_gc (s)
2599 struct glyph_string *s;
2600{
2601 if (s->font == FRAME_FONT (s->f)
2602 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2603 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2604 && !s->cmp)
2605 s->gc = s->f->output_data.x->cursor_gc;
2606 else
2607 {
2608 /* Cursor on non-default face: must merge. */
2609 XGCValues xgcv;
2610 unsigned long mask;
2611
2612 xgcv.background = s->f->output_data.x->cursor_pixel;
2613 xgcv.foreground = s->face->background;
2614
2615 /* If the glyph would be invisible, try a different foreground. */
2616 if (xgcv.foreground == xgcv.background)
2617 xgcv.foreground = s->face->foreground;
2618 if (xgcv.foreground == xgcv.background)
2619 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2620 if (xgcv.foreground == xgcv.background)
2621 xgcv.foreground = s->face->foreground;
2622
2623 /* Make sure the cursor is distinct from text in this face. */
2624 if (xgcv.background == s->face->background
2625 && xgcv.foreground == s->face->foreground)
2626 {
2627 xgcv.background = s->face->foreground;
2628 xgcv.foreground = s->face->background;
2629 }
2630
2631 IF_DEBUG (x_check_font (s->f, s->font));
2632 xgcv.font = s->font->fid;
2633 xgcv.graphics_exposures = False;
2634 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2635
2636 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2637 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2638 mask, &xgcv);
2639 else
2640 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2641 = XCreateGC (s->display, s->window, mask, &xgcv);
2642
2643 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2644 }
2645}
2646
2647
2648/* Set up S->gc of glyph string S for drawing text in mouse face. */
2649
2650static void
2651x_set_mouse_face_gc (s)
2652 struct glyph_string *s;
2653{
2654 int face_id;
2655 struct face *face;
2656
2657 /* What face has to be used for the mouse face? */
2658 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2659 face = FACE_FROM_ID (s->f, face_id);
2660 if (s->first_glyph->type == CHAR_GLYPH)
2661 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2662 else
2663 face_id = FACE_FOR_CHAR (s->f, face, 0);
2664 s->face = FACE_FROM_ID (s->f, face_id);
2665 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2666
2667 /* If font in this face is same as S->font, use it. */
2668 if (s->font == s->face->font)
2669 s->gc = s->face->gc;
2670 else
2671 {
2672 /* Otherwise construct scratch_cursor_gc with values from FACE
2673 but font FONT. */
2674 XGCValues xgcv;
2675 unsigned long mask;
2676
2677 xgcv.background = s->face->background;
2678 xgcv.foreground = s->face->foreground;
2679 IF_DEBUG (x_check_font (s->f, s->font));
2680 xgcv.font = s->font->fid;
2681 xgcv.graphics_exposures = False;
2682 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2683
2684 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2685 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2686 mask, &xgcv);
2687 else
2688 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2689 = XCreateGC (s->display, s->window, mask, &xgcv);
2690
2691 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2692 }
2693
2694 xassert (s->gc != 0);
2695}
2696
2697
2698/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2699 Faces to use in the mode line have already been computed when the
2700 matrix was built, so there isn't much to do, here. */
2701
2702static INLINE void
2703x_set_mode_line_face_gc (s)
2704 struct glyph_string *s;
2705{
2706 s->gc = s->face->gc;
2707}
2708
2709
2710/* Set S->gc of glyph string S for drawing that glyph string. Set
2711 S->stippled_p to a non-zero value if the face of S has a stipple
2712 pattern. */
2713
2714static INLINE void
2715x_set_glyph_string_gc (s)
2716 struct glyph_string *s;
2717{
2718 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2719
2720 if (s->hl == DRAW_NORMAL_TEXT)
2721 {
2722 s->gc = s->face->gc;
2723 s->stippled_p = s->face->stipple != 0;
2724 }
2725 else if (s->hl == DRAW_INVERSE_VIDEO)
2726 {
2727 x_set_mode_line_face_gc (s);
2728 s->stippled_p = s->face->stipple != 0;
2729 }
2730 else if (s->hl == DRAW_CURSOR)
2731 {
2732 x_set_cursor_gc (s);
2733 s->stippled_p = 0;
2734 }
2735 else if (s->hl == DRAW_MOUSE_FACE)
2736 {
2737 x_set_mouse_face_gc (s);
2738 s->stippled_p = s->face->stipple != 0;
2739 }
2740 else if (s->hl == DRAW_IMAGE_RAISED
2741 || s->hl == DRAW_IMAGE_SUNKEN)
2742 {
2743 s->gc = s->face->gc;
2744 s->stippled_p = s->face->stipple != 0;
2745 }
2746 else
2747 {
2748 s->gc = s->face->gc;
2749 s->stippled_p = s->face->stipple != 0;
2750 }
2751
2752 /* GC must have been set. */
2753 xassert (s->gc != 0);
2754}
2755
2756
2757/* Return in *R the clipping rectangle for glyph string S. */
2758
2759static void
2760x_get_glyph_string_clip_rect (s, r)
2761 struct glyph_string *s;
2762 XRectangle *r;
2763{
2764 if (s->row->full_width_p)
2765 {
2766 /* Draw full-width. X coordinates are relative to S->w->left. */
2767 int canon_x = CANON_X_UNIT (s->f);
2768
2769 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2770 r->width = XFASTINT (s->w->width) * canon_x;
2771
2772 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2773 {
2774 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2775 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2776 r->x -= width;
2777 }
2778
2779 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2780
2781 /* Unless displaying a mode or menu bar line, which are always
2782 fully visible, clip to the visible part of the row. */
2783 if (s->w->pseudo_window_p)
2784 r->height = s->row->visible_height;
2785 else
2786 r->height = s->height;
2787 }
2788 else
2789 {
2790 /* This is a text line that may be partially visible. */
2791 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2792 r->width = window_box_width (s->w, s->area);
2793 r->height = s->row->visible_height;
2794 }
2795
2796 /* Don't use S->y for clipping because it doesn't take partially
2797 visible lines into account. For example, it can be negative for
2798 partially visible lines at the top of a window. */
2799 if (!s->row->full_width_p
2800 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2801 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2802 else
2803 r->y = max (0, s->row->y);
2804
2805 /* If drawing a tool-bar window, draw it over the internal border
2806 at the top of the window. */
2807 if (s->w == XWINDOW (s->f->tool_bar_window))
2808 r->y -= s->f->output_data.x->internal_border_width;
2809
2810 /* If S draws overlapping rows, it's sufficient to use the top and
2811 bottom of the window for clipping because this glyph string
2812 intentionally draws over other lines. */
2813 if (s->for_overlaps_p)
2814 {
2815 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2816 r->height = window_text_bottom_y (s->w) - r->y;
2817 }
2818
2819 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2820}
2821
2822
2823/* Set clipping for output of glyph string S. S may be part of a mode
2824 line or menu if we don't have X toolkit support. */
2825
2826static INLINE void
2827x_set_glyph_string_clipping (s)
2828 struct glyph_string *s;
2829{
2830 XRectangle r;
2831 x_get_glyph_string_clip_rect (s, &r);
2832 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2833}
2834
2835
2836/* Compute left and right overhang of glyph string S. If S is a glyph
2837 string for a composition, assume overhangs don't exist. */
2838
2839static INLINE void
2840x_compute_glyph_string_overhangs (s)
2841 struct glyph_string *s;
2842{
2843 if (s->cmp == NULL
2844 && s->first_glyph->type == CHAR_GLYPH)
2845 {
2846 XCharStruct cs;
2847 int direction, font_ascent, font_descent;
2848 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2849 &font_ascent, &font_descent, &cs);
2850 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2851 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2852 }
2853}
2854
2855
2856/* Compute overhangs and x-positions for glyph string S and its
2857 predecessors, or successors. X is the starting x-position for S.
2858 BACKWARD_P non-zero means process predecessors. */
2859
2860static void
2861x_compute_overhangs_and_x (s, x, backward_p)
2862 struct glyph_string *s;
2863 int x;
2864 int backward_p;
2865{
2866 if (backward_p)
2867 {
2868 while (s)
2869 {
2870 x_compute_glyph_string_overhangs (s);
2871 x -= s->width;
2872 s->x = x;
2873 s = s->prev;
2874 }
2875 }
2876 else
2877 {
2878 while (s)
2879 {
2880 x_compute_glyph_string_overhangs (s);
2881 s->x = x;
2882 x += s->width;
2883 s = s->next;
2884 }
2885 }
2886}
2887
2888
2889/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2890 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2891 assumed to be zero. */
2892
2893static void
2894x_get_glyph_overhangs (glyph, f, left, right)
2895 struct glyph *glyph;
2896 struct frame *f;
2897 int *left, *right;
2898{
2899 *left = *right = 0;
2900
2901 if (glyph->type == CHAR_GLYPH)
2902 {
2903 XFontStruct *font;
2904 struct face *face;
2905 struct font_info *font_info;
2906 XChar2b char2b;
2907 XCharStruct *pcm;
2908
2909 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2910 font = face->font;
2911 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2912 if (font
2913 && (pcm = x_per_char_metric (font, &char2b)))
2914 {
2915 if (pcm->rbearing > pcm->width)
2916 *right = pcm->rbearing - pcm->width;
2917 if (pcm->lbearing < 0)
2918 *left = -pcm->lbearing;
2919 }
2920 }
2921}
2922
2923
2924/* Return the index of the first glyph preceding glyph string S that
2925 is overwritten by S because of S's left overhang. Value is -1
2926 if no glyphs are overwritten. */
2927
2928static int
2929x_left_overwritten (s)
2930 struct glyph_string *s;
2931{
2932 int k;
2933
2934 if (s->left_overhang)
2935 {
2936 int x = 0, i;
2937 struct glyph *glyphs = s->row->glyphs[s->area];
2938 int first = s->first_glyph - glyphs;
2939
2940 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2941 x -= glyphs[i].pixel_width;
2942
2943 k = i + 1;
2944 }
2945 else
2946 k = -1;
2947
2948 return k;
2949}
2950
2951
2952/* Return the index of the first glyph preceding glyph string S that
2953 is overwriting S because of its right overhang. Value is -1 if no
2954 glyph in front of S overwrites S. */
2955
2956static int
2957x_left_overwriting (s)
2958 struct glyph_string *s;
2959{
2960 int i, k, x;
2961 struct glyph *glyphs = s->row->glyphs[s->area];
2962 int first = s->first_glyph - glyphs;
2963
2964 k = -1;
2965 x = 0;
2966 for (i = first - 1; i >= 0; --i)
2967 {
2968 int left, right;
2969 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2970 if (x + right > 0)
2971 k = i;
2972 x -= glyphs[i].pixel_width;
2973 }
2974
2975 return k;
2976}
2977
2978
2979/* Return the index of the last glyph following glyph string S that is
2980 not overwritten by S because of S's right overhang. Value is -1 if
2981 no such glyph is found. */
2982
2983static int
2984x_right_overwritten (s)
2985 struct glyph_string *s;
2986{
2987 int k = -1;
2988
2989 if (s->right_overhang)
2990 {
2991 int x = 0, i;
2992 struct glyph *glyphs = s->row->glyphs[s->area];
2993 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2994 int end = s->row->used[s->area];
2995
2996 for (i = first; i < end && s->right_overhang > x; ++i)
2997 x += glyphs[i].pixel_width;
2998
2999 k = i;
3000 }
3001
3002 return k;
3003}
3004
3005
3006/* Return the index of the last glyph following glyph string S that
3007 overwrites S because of its left overhang. Value is negative
3008 if no such glyph is found. */
3009
3010static int
3011x_right_overwriting (s)
3012 struct glyph_string *s;
3013{
3014 int i, k, x;
3015 int end = s->row->used[s->area];
3016 struct glyph *glyphs = s->row->glyphs[s->area];
3017 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3018
3019 k = -1;
3020 x = 0;
3021 for (i = first; i < end; ++i)
3022 {
3023 int left, right;
3024 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3025 if (x - left < 0)
3026 k = i;
3027 x += glyphs[i].pixel_width;
3028 }
3029
3030 return k;
3031}
3032
3033
3034/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3035
3036static INLINE void
3037x_clear_glyph_string_rect (s, x, y, w, h)
3038 struct glyph_string *s;
3039 int x, y, w, h;
3040{
3041 XGCValues xgcv;
3042 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3043 XSetForeground (s->display, s->gc, xgcv.background);
3044 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3045 XSetForeground (s->display, s->gc, xgcv.foreground);
3046}
3047
3048
3049/* Draw the background of glyph_string S. If S->background_filled_p
3050 is non-zero don't draw it. FORCE_P non-zero means draw the
3051 background even if it wouldn't be drawn normally. This is used
3052 when a string preceding S draws into the background of S, or S
3053 contains the first component of a composition. */
3054
3055static void
3056x_draw_glyph_string_background (s, force_p)
3057 struct glyph_string *s;
3058 int force_p;
3059{
3060 /* Nothing to do if background has already been drawn or if it
3061 shouldn't be drawn in the first place. */
3062 if (!s->background_filled_p)
3063 {
3064 if (s->stippled_p)
3065 {
3066 /* Fill background with a stipple pattern. */
3067 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3068 XFillRectangle (s->display, s->window, s->gc, s->x,
3069 s->y + s->face->box_line_width,
3070 s->background_width,
3071 s->height - 2 * s->face->box_line_width);
3072 XSetFillStyle (s->display, s->gc, FillSolid);
3073 s->background_filled_p = 1;
3074 }
3075 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3076 || s->font_not_found_p
3077 || s->extends_to_end_of_line_p
3078 || force_p)
3079 {
3080 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3081 s->background_width,
3082 s->height - 2 * s->face->box_line_width);
3083 s->background_filled_p = 1;
3084 }
3085 }
3086}
3087
3088
3089/* Draw the foreground of glyph string S. */
3090
3091static void
3092x_draw_glyph_string_foreground (s)
3093 struct glyph_string *s;
3094{
3095 int i, x;
3096
3097 /* If first glyph of S has a left box line, start drawing the text
3098 of S to the right of that box line. */
3099 if (s->face->box != FACE_NO_BOX
3100 && s->first_glyph->left_box_line_p)
3101 x = s->x + s->face->box_line_width;
3102 else
3103 x = s->x;
3104
3105 /* Draw characters of S as rectangles if S's font could not be
3106 loaded. */
3107 if (s->font_not_found_p)
3108 {
3109 for (i = 0; i < s->nchars; ++i)
3110 {
3111 struct glyph *g = s->first_glyph + i;
3112 XDrawRectangle (s->display, s->window,
3113 s->gc, x, s->y, g->pixel_width - 1,
3114 s->height - 1);
3115 x += g->pixel_width;
3116 }
3117 }
3118 else
3119 {
3120 char *char1b = (char *) s->char2b;
3121 int boff = s->font_info->baseline_offset;
3122
3123 if (s->font_info->vertical_centering)
3124 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3125
3126 /* If we can use 8-bit functions, condense S->char2b. */
3127 if (!s->two_byte_p)
3128 for (i = 0; i < s->nchars; ++i)
3129 char1b[i] = s->char2b[i].byte2;
3130
3131 /* Draw text with XDrawString if background has already been
3132 filled. Otherwise, use XDrawImageString. (Note that
3133 XDrawImageString is usually faster than XDrawString.) Always
3134 use XDrawImageString when drawing the cursor so that there is
3135 no chance that characters under a box cursor are invisible. */
3136 if (s->for_overlaps_p
3137 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3138 {
3139 /* Draw characters with 16-bit or 8-bit functions. */
3140 if (s->two_byte_p)
3141 XDrawString16 (s->display, s->window, s->gc, x,
3142 s->ybase - boff, s->char2b, s->nchars);
3143 else
3144 XDrawString (s->display, s->window, s->gc, x,
3145 s->ybase - boff, char1b, s->nchars);
3146 }
3147 else
3148 {
3149 if (s->two_byte_p)
3150 XDrawImageString16 (s->display, s->window, s->gc, x,
3151 s->ybase - boff, s->char2b, s->nchars);
3152 else
3153 XDrawImageString (s->display, s->window, s->gc, x,
3154 s->ybase - boff, char1b, s->nchars);
3155 }
3156 }
3157}
3158
3159/* Draw the foreground of composite glyph string S. */
3160
3161static void
3162x_draw_composite_glyph_string_foreground (s)
3163 struct glyph_string *s;
3164{
3165 int i, x;
3166
3167 /* If first glyph of S has a left box line, start drawing the text
3168 of S to the right of that box line. */
3169 if (s->face->box != FACE_NO_BOX
3170 && s->first_glyph->left_box_line_p)
3171 x = s->x + s->face->box_line_width;
3172 else
3173 x = s->x;
3174
3175 /* S is a glyph string for a composition. S->gidx is the index of
3176 the first character drawn for glyphs of this composition.
3177 S->gidx == 0 means we are drawing the very first character of
3178 this composition. */
3179
3180 /* Draw a rectangle for the composition if the font for the very
3181 first character of the composition could not be loaded. */
3182 if (s->font_not_found_p)
3183 {
3184 if (s->gidx == 0)
3185 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3186 s->width - 1, s->height - 1);
3187 }
3188 else
3189 {
3190 for (i = 0; i < s->nchars; i++, ++s->gidx)
3191 XDrawString16 (s->display, s->window, s->gc,
3192 x + s->cmp->offsets[s->gidx * 2],
3193 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3194 s->char2b + i, 1);
3195 }
3196}
3197
3198
3199#ifdef USE_X_TOOLKIT
3200
3201static struct frame *x_frame_of_widget P_ ((Widget));
3202
3203
3204/* Return the frame on which widget WIDGET is used.. Abort if frame
3205 cannot be determined. */
3206
3207static struct frame *
3208x_frame_of_widget (widget)
3209 Widget widget;
3210{
3211 struct x_display_info *dpyinfo;
3212 Lisp_Object tail;
3213 struct frame *f;
3214
3215 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3216
3217 /* Find the top-level shell of the widget. Note that this function
3218 can be called when the widget is not yet realized, so XtWindow
3219 (widget) == 0. That's the reason we can't simply use
3220 x_any_window_to_frame. */
3221 while (!XtIsTopLevelShell (widget))
3222 widget = XtParent (widget);
3223
3224 /* Look for a frame with that top-level widget. Allocate the color
3225 on that frame to get the right gamma correction value. */
3226 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3227 if (GC_FRAMEP (XCAR (tail))
3228 && (f = XFRAME (XCAR (tail)),
3229 (f->output_data.nothing != 1
3230 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3231 && f->output_data.x->widget == widget)
3232 return f;
3233
3234 abort ();
3235}
3236
3237
3238/* Allocate the color COLOR->pixel on the screen and display of
3239 widget WIDGET in colormap CMAP. If an exact match cannot be
3240 allocated, try the nearest color available. Value is non-zero
3241 if successful. This is called from lwlib. */
3242
3243int
3244x_alloc_nearest_color_for_widget (widget, cmap, color)
3245 Widget widget;
3246 Colormap cmap;
3247 XColor *color;
3248{
3249 struct frame *f = x_frame_of_widget (widget);
3250 return x_alloc_nearest_color (f, cmap, color);
3251}
3252
3253
3254/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3255 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3256 If this produces the same color as PIXEL, try a color where all RGB
3257 values have DELTA added. Return the allocated color in *PIXEL.
3258 DISPLAY is the X display, CMAP is the colormap to operate on.
3259 Value is non-zero if successful. */
3260
3261int
3262x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3263 Widget widget;
3264 Display *display;
3265 Colormap cmap;
3266 unsigned long *pixel;
3267 double factor;
3268 int delta;
3269{
3270 struct frame *f = x_frame_of_widget (widget);
3271 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3272}
3273
3274
3275#endif /* USE_X_TOOLKIT */
3276
3277
3278/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3279 CMAP. If an exact match can't be allocated, try the nearest color
3280 available. Value is non-zero if successful. Set *COLOR to the
3281 color allocated. */
3282
3283int
3284x_alloc_nearest_color (f, cmap, color)
3285 struct frame *f;
3286 Colormap cmap;
3287 XColor *color;
3288{
3289 Display *display = FRAME_X_DISPLAY (f);
3290 Screen *screen = FRAME_X_SCREEN (f);
3291 int rc;
3292
3293 gamma_correct (f, color);
3294 rc = XAllocColor (display, cmap, color);
3295 if (rc == 0)
3296 {
3297 /* If we got to this point, the colormap is full, so we're going
3298 to try to get the next closest color. The algorithm used is
3299 a least-squares matching, which is what X uses for closest
3300 color matching with StaticColor visuals. */
3301 int nearest, i;
3302 unsigned long nearest_delta = ~0;
3303 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3304 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3305
3306 for (i = 0; i < ncells; ++i)
3307 cells[i].pixel = i;
3308 XQueryColors (display, cmap, cells, ncells);
3309
3310 for (nearest = i = 0; i < ncells; ++i)
3311 {
3312 long dred = (color->red >> 8) - (cells[i].red >> 8);
3313 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3314 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3315 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3316
3317 if (delta < nearest_delta)
3318 {
3319 nearest = i;
3320 nearest_delta = delta;
3321 }
3322 }
3323
3324 color->red = cells[nearest].red;
3325 color->green = cells[nearest].green;
3326 color->blue = cells[nearest].blue;
3327 rc = XAllocColor (display, cmap, color);
3328 }
3329
3330#ifdef DEBUG_X_COLORS
3331 if (rc)
3332 register_color (color->pixel);
3333#endif /* DEBUG_X_COLORS */
3334
3335 return rc;
3336}
3337
3338
3339/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3340 It's necessary to do this instead of just using PIXEL directly to
3341 get color reference counts right. */
3342
3343unsigned long
3344x_copy_color (f, pixel)
3345 struct frame *f;
3346 unsigned long pixel;
3347{
3348 XColor color;
3349
3350 color.pixel = pixel;
3351 BLOCK_INPUT;
3352 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3353 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3354 UNBLOCK_INPUT;
3355#ifdef DEBUG_X_COLORS
3356 register_color (pixel);
3357#endif
3358 return color.pixel;
3359}
3360
3361
3362/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3363 It's necessary to do this instead of just using PIXEL directly to
3364 get color reference counts right. */
3365
3366unsigned long
3367x_copy_dpy_color (dpy, cmap, pixel)
3368 Display *dpy;
3369 Colormap cmap;
3370 unsigned long pixel;
3371{
3372 XColor color;
3373
3374 color.pixel = pixel;
3375 BLOCK_INPUT;
3376 XQueryColor (dpy, cmap, &color);
3377 XAllocColor (dpy, cmap, &color);
3378 UNBLOCK_INPUT;
3379#ifdef DEBUG_X_COLORS
3380 register_color (pixel);
3381#endif
3382 return color.pixel;
3383}
3384
3385
3386/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3387 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3388 If this produces the same color as PIXEL, try a color where all RGB
3389 values have DELTA added. Return the allocated color in *PIXEL.
3390 DISPLAY is the X display, CMAP is the colormap to operate on.
3391 Value is non-zero if successful. */
3392
3393static int
3394x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3395 struct frame *f;
3396 Display *display;
3397 Colormap cmap;
3398 unsigned long *pixel;
3399 double factor;
3400 int delta;
3401{
3402 XColor color, new;
3403 int success_p;
3404
3405 /* Get RGB color values. */
3406 color.pixel = *pixel;
3407 XQueryColor (display, cmap, &color);
3408
3409 /* Change RGB values by specified FACTOR. Avoid overflow! */
3410 xassert (factor >= 0);
3411 new.red = min (0xffff, factor * color.red);
3412 new.green = min (0xffff, factor * color.green);
3413 new.blue = min (0xffff, factor * color.blue);
3414
3415 /* Try to allocate the color. */
3416 success_p = x_alloc_nearest_color (f, cmap, &new);
3417 if (success_p)
3418 {
3419 if (new.pixel == *pixel)
3420 {
3421 /* If we end up with the same color as before, try adding
3422 delta to the RGB values. */
3423 x_free_colors (f, &new.pixel, 1);
3424
3425 new.red = min (0xffff, delta + color.red);
3426 new.green = min (0xffff, delta + color.green);
3427 new.blue = min (0xffff, delta + color.blue);
3428 success_p = x_alloc_nearest_color (f, cmap, &new);
3429 }
3430 else
3431 success_p = 1;
3432 *pixel = new.pixel;
3433 }
3434
3435 return success_p;
3436}
3437
3438
3439/* Set up the foreground color for drawing relief lines of glyph
3440 string S. RELIEF is a pointer to a struct relief containing the GC
3441 with which lines will be drawn. Use a color that is FACTOR or
3442 DELTA lighter or darker than the relief's background which is found
3443 in S->f->output_data.x->relief_background. If such a color cannot
3444 be allocated, use DEFAULT_PIXEL, instead. */
3445
3446static void
3447x_setup_relief_color (f, relief, factor, delta, default_pixel)
3448 struct frame *f;
3449 struct relief *relief;
3450 double factor;
3451 int delta;
3452 unsigned long default_pixel;
3453{
3454 XGCValues xgcv;
3455 struct x_output *di = f->output_data.x;
3456 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3457 unsigned long pixel;
3458 unsigned long background = di->relief_background;
3459 Colormap cmap = FRAME_X_COLORMAP (f);
3460 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3461 Display *dpy = FRAME_X_DISPLAY (f);
3462
3463 xgcv.graphics_exposures = False;
3464 xgcv.line_width = 1;
3465
3466 /* Free previously allocated color. The color cell will be reused
3467 when it has been freed as many times as it was allocated, so this
3468 doesn't affect faces using the same colors. */
3469 if (relief->gc
3470 && relief->allocated_p)
3471 {
3472 x_free_colors (f, &relief->pixel, 1);
3473 relief->allocated_p = 0;
3474 }
3475
3476 /* Allocate new color. */
3477 xgcv.foreground = default_pixel;
3478 pixel = background;
3479 if (dpyinfo->n_planes != 1
3480 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3481 {
3482 relief->allocated_p = 1;
3483 xgcv.foreground = relief->pixel = pixel;
3484 }
3485
3486 if (relief->gc == 0)
3487 {
3488 xgcv.stipple = dpyinfo->gray;
3489 mask |= GCStipple;
3490 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3491 }
3492 else
3493 XChangeGC (dpy, relief->gc, mask, &xgcv);
3494}
3495
3496
3497/* Set up colors for the relief lines around glyph string S. */
3498
3499static void
3500x_setup_relief_colors (s)
3501 struct glyph_string *s;
3502{
3503 struct x_output *di = s->f->output_data.x;
3504 unsigned long color;
3505
3506 if (s->face->use_box_color_for_shadows_p)
3507 color = s->face->box_color;
3508 else
3509 {
3510 XGCValues xgcv;
3511
3512 /* Get the background color of the face. */
3513 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3514 color = xgcv.background;
3515 }
3516
3517 if (di->white_relief.gc == 0
3518 || color != di->relief_background)
3519 {
3520 di->relief_background = color;
3521 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3522 WHITE_PIX_DEFAULT (s->f));
3523 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3524 BLACK_PIX_DEFAULT (s->f));
3525 }
3526}
3527
3528
3529/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3530 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3531 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3532 relief. LEFT_P non-zero means draw a relief on the left side of
3533 the rectangle. RIGHT_P non-zero means draw a relief on the right
3534 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3535 when drawing. */
3536
3537static void
3538x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3539 raised_p, left_p, right_p, clip_rect)
3540 struct frame *f;
3541 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3542 XRectangle *clip_rect;
3543{
3544 int i;
3545 GC gc;
3546
3547 if (raised_p)
3548 gc = f->output_data.x->white_relief.gc;
3549 else
3550 gc = f->output_data.x->black_relief.gc;
3551 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3552
3553 /* Top. */
3554 for (i = 0; i < width; ++i)
3555 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3556 left_x + i * left_p, top_y + i,
3557 right_x + 1 - i * right_p, top_y + i);
3558
3559 /* Left. */
3560 if (left_p)
3561 for (i = 0; i < width; ++i)
3562 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3563 left_x + i, top_y + i, left_x + i, bottom_y - i);
3564
3565 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3566 if (raised_p)
3567 gc = f->output_data.x->black_relief.gc;
3568 else
3569 gc = f->output_data.x->white_relief.gc;
3570 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3571
3572 /* Bottom. */
3573 for (i = 0; i < width; ++i)
3574 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3575 left_x + i * left_p, bottom_y - i,
3576 right_x + 1 - i * right_p, bottom_y - i);
3577
3578 /* Right. */
3579 if (right_p)
3580 for (i = 0; i < width; ++i)
3581 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3582 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3583
3584 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3585}
3586
3587
3588/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3589 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3590 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3591 left side of the rectangle. RIGHT_P non-zero means draw a line
3592 on the right side of the rectangle. CLIP_RECT is the clipping
3593 rectangle to use when drawing. */
3594
3595static void
3596x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3597 left_p, right_p, clip_rect)
3598 struct glyph_string *s;
3599 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3600 XRectangle *clip_rect;
3601{
3602 XGCValues xgcv;
3603
3604 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3605 XSetForeground (s->display, s->gc, s->face->box_color);
3606 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3607
3608 /* Top. */
3609 XFillRectangle (s->display, s->window, s->gc,
3610 left_x, top_y, right_x - left_x, width);
3611
3612 /* Left. */
3613 if (left_p)
3614 XFillRectangle (s->display, s->window, s->gc,
3615 left_x, top_y, width, bottom_y - top_y);
3616
3617 /* Bottom. */
3618 XFillRectangle (s->display, s->window, s->gc,
3619 left_x, bottom_y - width, right_x - left_x, width);
3620
3621 /* Right. */
3622 if (right_p)
3623 XFillRectangle (s->display, s->window, s->gc,
3624 right_x - width, top_y, width, bottom_y - top_y);
3625
3626 XSetForeground (s->display, s->gc, xgcv.foreground);
3627 XSetClipMask (s->display, s->gc, None);
3628}
3629
3630
3631/* Draw a box around glyph string S. */
3632
3633static void
3634x_draw_glyph_string_box (s)
3635 struct glyph_string *s;
3636{
3637 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3638 int left_p, right_p;
3639 struct glyph *last_glyph;
3640 XRectangle clip_rect;
3641
3642 last_x = window_box_right (s->w, s->area);
3643 if (s->row->full_width_p
3644 && !s->w->pseudo_window_p)
3645 {
3646 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3647 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3648 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3649 }
3650
3651 /* The glyph that may have a right box line. */
3652 last_glyph = (s->cmp || s->img
3653 ? s->first_glyph
3654 : s->first_glyph + s->nchars - 1);
3655
3656 width = s->face->box_line_width;
3657 raised_p = s->face->box == FACE_RAISED_BOX;
3658 left_x = s->x;
3659 right_x = ((s->row->full_width_p
3660 ? last_x - 1
3661 : min (last_x, s->x + s->background_width) - 1));
3662 top_y = s->y;
3663 bottom_y = top_y + s->height - 1;
3664
3665 left_p = (s->first_glyph->left_box_line_p
3666 || (s->hl == DRAW_MOUSE_FACE
3667 && (s->prev == NULL
3668 || s->prev->hl != s->hl)));
3669 right_p = (last_glyph->right_box_line_p
3670 || (s->hl == DRAW_MOUSE_FACE
3671 && (s->next == NULL
3672 || s->next->hl != s->hl)));
3673
3674 x_get_glyph_string_clip_rect (s, &clip_rect);
3675
3676 if (s->face->box == FACE_SIMPLE_BOX)
3677 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3678 left_p, right_p, &clip_rect);
3679 else
3680 {
3681 x_setup_relief_colors (s);
3682 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3683 width, raised_p, left_p, right_p, &clip_rect);
3684 }
3685}
3686
3687
3688/* Draw foreground of image glyph string S. */
3689
3690static void
3691x_draw_image_foreground (s)
3692 struct glyph_string *s;
3693{
3694 int x;
3695 int y = s->ybase - image_ascent (s->img, s->face);
3696
3697 /* If first glyph of S has a left box line, start drawing it to the
3698 right of that line. */
3699 if (s->face->box != FACE_NO_BOX
3700 && s->first_glyph->left_box_line_p)
3701 x = s->x + s->face->box_line_width;
3702 else
3703 x = s->x;
3704
3705 /* If there is a margin around the image, adjust x- and y-position
3706 by that margin. */
3707 if (s->img->margin)
3708 {
3709 x += s->img->margin;
3710 y += s->img->margin;
3711 }
3712
3713 if (s->img->pixmap)
3714 {
3715 if (s->img->mask)
3716 {
3717 /* We can't set both a clip mask and use XSetClipRectangles
3718 because the latter also sets a clip mask. We also can't
3719 trust on the shape extension to be available
3720 (XShapeCombineRegion). So, compute the rectangle to draw
3721 manually. */
3722 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3723 | GCFunction);
3724 XGCValues xgcv;
3725 XRectangle clip_rect, image_rect, r;
3726
3727 xgcv.clip_mask = s->img->mask;
3728 xgcv.clip_x_origin = x;
3729 xgcv.clip_y_origin = y;
3730 xgcv.function = GXcopy;
3731 XChangeGC (s->display, s->gc, mask, &xgcv);
3732
3733 x_get_glyph_string_clip_rect (s, &clip_rect);
3734 image_rect.x = x;
3735 image_rect.y = y;
3736 image_rect.width = s->img->width;
3737 image_rect.height = s->img->height;
3738 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3739 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3740 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3741 }
3742 else
3743 {
3744 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3745 XGCValues xgcv;
3746 XRectangle clip_rect, image_rect, r;
3747
3748 x_get_glyph_string_clip_rect (s, &clip_rect);
3749 image_rect.x = x;
3750 image_rect.y = y;
3751 image_rect.width = s->img->width;
3752 image_rect.height = s->img->height;
3753 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3754 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3755 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3756
3757 /* When the image has a mask, we can expect that at
3758 least part of a mouse highlight or a block cursor will
3759 be visible. If the image doesn't have a mask, make
3760 a block cursor visible by drawing a rectangle around
3761 the image. I believe it's looking better if we do
3762 nothing here for mouse-face. */
3763 if (s->hl == DRAW_CURSOR)
3764 XDrawRectangle (s->display, s->window, s->gc, x, y,
3765 s->img->width - 1, s->img->height - 1);
3766 }
3767 }
3768 else
3769 /* Draw a rectangle if image could not be loaded. */
3770 XDrawRectangle (s->display, s->window, s->gc, x, y,
3771 s->img->width - 1, s->img->height - 1);
3772}
3773
3774
3775/* Draw a relief around the image glyph string S. */
3776
3777static void
3778x_draw_image_relief (s)
3779 struct glyph_string *s;
3780{
3781 int x0, y0, x1, y1, thick, raised_p;
3782 XRectangle r;
3783 int x;
3784 int y = s->ybase - image_ascent (s->img, s->face);
3785
3786 /* If first glyph of S has a left box line, start drawing it to the
3787 right of that line. */
3788 if (s->face->box != FACE_NO_BOX
3789 && s->first_glyph->left_box_line_p)
3790 x = s->x + s->face->box_line_width;
3791 else
3792 x = s->x;
3793
3794 /* If there is a margin around the image, adjust x- and y-position
3795 by that margin. */
3796 if (s->img->margin)
3797 {
3798 x += s->img->margin;
3799 y += s->img->margin;
3800 }
3801
3802 if (s->hl == DRAW_IMAGE_SUNKEN
3803 || s->hl == DRAW_IMAGE_RAISED)
3804 {
3805 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3806 raised_p = s->hl == DRAW_IMAGE_RAISED;
3807 }
3808 else
3809 {
3810 thick = abs (s->img->relief);
3811 raised_p = s->img->relief > 0;
3812 }
3813
3814 x0 = x - thick;
3815 y0 = y - thick;
3816 x1 = x + s->img->width + thick - 1;
3817 y1 = y + s->img->height + thick - 1;
3818
3819 x_setup_relief_colors (s);
3820 x_get_glyph_string_clip_rect (s, &r);
3821 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3822}
3823
3824
3825/* Draw the foreground of image glyph string S to PIXMAP. */
3826
3827static void
3828x_draw_image_foreground_1 (s, pixmap)
3829 struct glyph_string *s;
3830 Pixmap pixmap;
3831{
3832 int x;
3833 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3834
3835 /* If first glyph of S has a left box line, start drawing it to the
3836 right of that line. */
3837 if (s->face->box != FACE_NO_BOX
3838 && s->first_glyph->left_box_line_p)
3839 x = s->face->box_line_width;
3840 else
3841 x = 0;
3842
3843 /* If there is a margin around the image, adjust x- and y-position
3844 by that margin. */
3845 if (s->img->margin)
3846 {
3847 x += s->img->margin;
3848 y += s->img->margin;
3849 }
3850
3851 if (s->img->pixmap)
3852 {
3853 if (s->img->mask)
3854 {
3855 /* We can't set both a clip mask and use XSetClipRectangles
3856 because the latter also sets a clip mask. We also can't
3857 trust on the shape extension to be available
3858 (XShapeCombineRegion). So, compute the rectangle to draw
3859 manually. */
3860 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3861 | GCFunction);
3862 XGCValues xgcv;
3863
3864 xgcv.clip_mask = s->img->mask;
3865 xgcv.clip_x_origin = x;
3866 xgcv.clip_y_origin = y;
3867 xgcv.function = GXcopy;
3868 XChangeGC (s->display, s->gc, mask, &xgcv);
3869
3870 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3871 0, 0, s->img->width, s->img->height, x, y);
3872 XSetClipMask (s->display, s->gc, None);
3873 }
3874 else
3875 {
3876 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3877 0, 0, s->img->width, s->img->height, x, y);
3878
3879 /* When the image has a mask, we can expect that at
3880 least part of a mouse highlight or a block cursor will
3881 be visible. If the image doesn't have a mask, make
3882 a block cursor visible by drawing a rectangle around
3883 the image. I believe it's looking better if we do
3884 nothing here for mouse-face. */
3885 if (s->hl == DRAW_CURSOR)
3886 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3887 s->img->width - 1, s->img->height - 1);
3888 }
3889 }
3890 else
3891 /* Draw a rectangle if image could not be loaded. */
3892 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3893 s->img->width - 1, s->img->height - 1);
3894}
3895
3896
3897/* Draw part of the background of glyph string S. X, Y, W, and H
3898 give the rectangle to draw. */
3899
3900static void
3901x_draw_glyph_string_bg_rect (s, x, y, w, h)
3902 struct glyph_string *s;
3903 int x, y, w, h;
3904{
3905 if (s->stippled_p)
3906 {
3907 /* Fill background with a stipple pattern. */
3908 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3909 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3910 XSetFillStyle (s->display, s->gc, FillSolid);
3911 }
3912 else
3913 x_clear_glyph_string_rect (s, x, y, w, h);
3914}
3915
3916
3917/* Draw image glyph string S.
3918
3919 s->y
3920 s->x +-------------------------
3921 | s->face->box
3922 |
3923 | +-------------------------
3924 | | s->img->margin
3925 | |
3926 | | +-------------------
3927 | | | the image
3928
3929 */
3930
3931static void
3932x_draw_image_glyph_string (s)
3933 struct glyph_string *s;
3934{
3935 int x, y;
3936 int box_line_width = s->face->box_line_width;
3937 int margin = s->img->margin;
3938 int height;
3939 Pixmap pixmap = None;
3940
3941 height = s->height - 2 * box_line_width;
3942
3943 /* Fill background with face under the image. Do it only if row is
3944 taller than image or if image has a clip mask to reduce
3945 flickering. */
3946 s->stippled_p = s->face->stipple != 0;
3947 if (height > s->img->height
3948 || margin
3949 || s->img->mask
3950 || s->img->pixmap == 0
3951 || s->width != s->background_width)
3952 {
3953 if (box_line_width && s->first_glyph->left_box_line_p)
3954 x = s->x + box_line_width;
3955 else
3956 x = s->x;
3957
3958 y = s->y + box_line_width;
3959
3960 if (s->img->mask)
3961 {
3962 /* Create a pixmap as large as the glyph string. Fill it
3963 with the background color. Copy the image to it, using
3964 its mask. Copy the temporary pixmap to the display. */
3965 Screen *screen = FRAME_X_SCREEN (s->f);
3966 int depth = DefaultDepthOfScreen (screen);
3967
3968 /* Create a pixmap as large as the glyph string. */
3969 pixmap = XCreatePixmap (s->display, s->window,
3970 s->background_width,
3971 s->height, depth);
3972
3973 /* Don't clip in the following because we're working on the
3974 pixmap. */
3975 XSetClipMask (s->display, s->gc, None);
3976
3977 /* Fill the pixmap with the background color/stipple. */
3978 if (s->stippled_p)
3979 {
3980 /* Fill background with a stipple pattern. */
3981 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3982 XFillRectangle (s->display, pixmap, s->gc,
3983 0, 0, s->background_width, s->height);
3984 XSetFillStyle (s->display, s->gc, FillSolid);
3985 }
3986 else
3987 {
3988 XGCValues xgcv;
3989 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3990 &xgcv);
3991 XSetForeground (s->display, s->gc, xgcv.background);
3992 XFillRectangle (s->display, pixmap, s->gc,
3993 0, 0, s->background_width, s->height);
3994 XSetForeground (s->display, s->gc, xgcv.foreground);
3995 }
3996 }
3997 else
3998 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3999
4000 s->background_filled_p = 1;
4001 }
4002
4003 /* Draw the foreground. */
4004 if (pixmap != None)
4005 {
4006 x_draw_image_foreground_1 (s, pixmap);
4007 x_set_glyph_string_clipping (s);
4008 XCopyArea (s->display, pixmap, s->window, s->gc,
4009 0, 0, s->background_width, s->height, s->x, s->y);
4010 XFreePixmap (s->display, pixmap);
4011 }
4012 else
4013 x_draw_image_foreground (s);
4014
4015 /* If we must draw a relief around the image, do it. */
4016 if (s->img->relief
4017 || s->hl == DRAW_IMAGE_RAISED
4018 || s->hl == DRAW_IMAGE_SUNKEN)
4019 x_draw_image_relief (s);
4020}
4021
4022
4023/* Draw stretch glyph string S. */
4024
4025static void
4026x_draw_stretch_glyph_string (s)
4027 struct glyph_string *s;
4028{
4029 xassert (s->first_glyph->type == STRETCH_GLYPH);
4030 s->stippled_p = s->face->stipple != 0;
4031
4032 if (s->hl == DRAW_CURSOR
4033 && !x_stretch_cursor_p)
4034 {
4035 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4036 as wide as the stretch glyph. */
4037 int width = min (CANON_X_UNIT (s->f), s->background_width);
4038
4039 /* Draw cursor. */
4040 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4041
4042 /* Clear rest using the GC of the original non-cursor face. */
4043 if (width < s->background_width)
4044 {
4045 GC gc = s->face->gc;
4046 int x = s->x + width, y = s->y;
4047 int w = s->background_width - width, h = s->height;
4048 XRectangle r;
4049
4050 x_get_glyph_string_clip_rect (s, &r);
4051 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4052
4053 if (s->face->stipple)
4054 {
4055 /* Fill background with a stipple pattern. */
4056 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4057 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4058 XSetFillStyle (s->display, gc, FillSolid);
4059 }
4060 else
4061 {
4062 XGCValues xgcv;
4063 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4064 XSetForeground (s->display, gc, xgcv.background);
4065 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4066 XSetForeground (s->display, gc, xgcv.foreground);
4067 }
4068 }
4069 }
4070 else
4071 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4072 s->height);
4073
4074 s->background_filled_p = 1;
4075}
4076
4077
4078/* Draw glyph string S. */
4079
4080static void
4081x_draw_glyph_string (s)
4082 struct glyph_string *s;
4083{
4084 /* If S draws into the background of its successor, draw the
4085 background of the successor first so that S can draw into it.
4086 This makes S->next use XDrawString instead of XDrawImageString. */
4087 if (s->next && s->right_overhang && !s->for_overlaps_p)
4088 {
4089 xassert (s->next->img == NULL);
4090 x_set_glyph_string_gc (s->next);
4091 x_set_glyph_string_clipping (s->next);
4092 x_draw_glyph_string_background (s->next, 1);
4093 }
4094
4095 /* Set up S->gc, set clipping and draw S. */
4096 x_set_glyph_string_gc (s);
4097 x_set_glyph_string_clipping (s);
4098
4099 switch (s->first_glyph->type)
4100 {
4101 case IMAGE_GLYPH:
4102 x_draw_image_glyph_string (s);
4103 break;
4104
4105 case STRETCH_GLYPH:
4106 x_draw_stretch_glyph_string (s);
4107 break;
4108
4109 case CHAR_GLYPH:
4110 if (s->for_overlaps_p)
4111 s->background_filled_p = 1;
4112 else
4113 x_draw_glyph_string_background (s, 0);
4114 x_draw_glyph_string_foreground (s);
4115 break;
4116
4117 case COMPOSITE_GLYPH:
4118 if (s->for_overlaps_p || s->gidx > 0)
4119 s->background_filled_p = 1;
4120 else
4121 x_draw_glyph_string_background (s, 1);
4122 x_draw_composite_glyph_string_foreground (s);
4123 break;
4124
4125 default:
4126 abort ();
4127 }
4128
4129 if (!s->for_overlaps_p)
4130 {
4131 /* Draw underline. */
4132 if (s->face->underline_p)
4133 {
4134 unsigned long dy, h;
4135
4136 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4137 h = 1;
4138 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4139 dy = s->height - h;
4140
4141 if (s->face->underline_defaulted_p)
4142 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4143 s->width, h);
4144 else
4145 {
4146 XGCValues xgcv;
4147 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4148 XSetForeground (s->display, s->gc, s->face->underline_color);
4149 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4150 s->width, h);
4151 XSetForeground (s->display, s->gc, xgcv.foreground);
4152 }
4153 }
4154
4155 /* Draw overline. */
4156 if (s->face->overline_p)
4157 {
4158 unsigned long dy = 0, h = 1;
4159
4160 if (s->face->overline_color_defaulted_p)
4161 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4162 s->width, h);
4163 else
4164 {
4165 XGCValues xgcv;
4166 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4167 XSetForeground (s->display, s->gc, s->face->overline_color);
4168 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4169 s->width, h);
4170 XSetForeground (s->display, s->gc, xgcv.foreground);
4171 }
4172 }
4173
4174 /* Draw strike-through. */
4175 if (s->face->strike_through_p)
4176 {
4177 unsigned long h = 1;
4178 unsigned long dy = (s->height - h) / 2;
4179
4180 if (s->face->strike_through_color_defaulted_p)
4181 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4182 s->width, h);
4183 else
4184 {
4185 XGCValues xgcv;
4186 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4187 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4188 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4189 s->width, h);
4190 XSetForeground (s->display, s->gc, xgcv.foreground);
4191 }
4192 }
4193
4194 /* Draw relief. */
4195 if (s->face->box != FACE_NO_BOX)
4196 x_draw_glyph_string_box (s);
4197 }
4198
4199 /* Reset clipping. */
4200 XSetClipMask (s->display, s->gc, None);
4201}
4202
4203
4204static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4205 struct face **, int));
4206
4207
4208/* Fill glyph string S with composition components specified by S->cmp.
4209
4210 FACES is an array of faces for all components of this composition.
4211 S->gidx is the index of the first component for S.
4212 OVERLAPS_P non-zero means S should draw the foreground only, and
4213 use its physical height for clipping.
4214
4215 Value is the index of a component not in S. */
4216
4217static int
4218x_fill_composite_glyph_string (s, faces, overlaps_p)
4219 struct glyph_string *s;
4220 struct face **faces;
4221 int overlaps_p;
4222{
4223 int i;
4224
4225 xassert (s);
4226
4227 s->for_overlaps_p = overlaps_p;
4228
4229 s->face = faces[s->gidx];
4230 s->font = s->face->font;
4231 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4232
4233 /* For all glyphs of this composition, starting at the offset
4234 S->gidx, until we reach the end of the definition or encounter a
4235 glyph that requires the different face, add it to S. */
4236 ++s->nchars;
4237 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4238 ++s->nchars;
4239
4240 /* All glyph strings for the same composition has the same width,
4241 i.e. the width set for the first component of the composition. */
4242
4243 s->width = s->first_glyph->pixel_width;
4244
4245 /* If the specified font could not be loaded, use the frame's
4246 default font, but record the fact that we couldn't load it in
4247 the glyph string so that we can draw rectangles for the
4248 characters of the glyph string. */
4249 if (s->font == NULL)
4250 {
4251 s->font_not_found_p = 1;
4252 s->font = FRAME_FONT (s->f);
4253 }
4254
4255 /* Adjust base line for subscript/superscript text. */
4256 s->ybase += s->first_glyph->voffset;
4257
4258 xassert (s->face && s->face->gc);
4259
4260 /* This glyph string must always be drawn with 16-bit functions. */
4261 s->two_byte_p = 1;
4262
4263 return s->gidx + s->nchars;
4264}
4265
4266
4267/* Fill glyph string S from a sequence of character glyphs.
4268
4269 FACE_ID is the face id of the string. START is the index of the
4270 first glyph to consider, END is the index of the last + 1.
4271 OVERLAPS_P non-zero means S should draw the foreground only, and
4272 use its physical height for clipping.
4273
4274 Value is the index of the first glyph not in S. */
4275
4276static int
4277x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4278 struct glyph_string *s;
4279 int face_id;
4280 int start, end, overlaps_p;
4281{
4282 struct glyph *glyph, *last;
4283 int voffset;
4284 int glyph_not_available_p;
4285
4286 xassert (s->f == XFRAME (s->w->frame));
4287 xassert (s->nchars == 0);
4288 xassert (start >= 0 && end > start);
4289
4290 s->for_overlaps_p = overlaps_p,
4291 glyph = s->row->glyphs[s->area] + start;
4292 last = s->row->glyphs[s->area] + end;
4293 voffset = glyph->voffset;
4294
4295 glyph_not_available_p = glyph->glyph_not_available_p;
4296
4297 while (glyph < last
4298 && glyph->type == CHAR_GLYPH
4299 && glyph->voffset == voffset
4300 /* Same face id implies same font, nowadays. */
4301 && glyph->face_id == face_id
4302 && glyph->glyph_not_available_p == glyph_not_available_p)
4303 {
4304 int two_byte_p;
4305
4306 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4307 s->char2b + s->nchars,
4308 &two_byte_p);
4309 s->two_byte_p = two_byte_p;
4310 ++s->nchars;
4311 xassert (s->nchars <= end - start);
4312 s->width += glyph->pixel_width;
4313 ++glyph;
4314 }
4315
4316 s->font = s->face->font;
4317 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4318
4319 /* If the specified font could not be loaded, use the frame's font,
4320 but record the fact that we couldn't load it in
4321 S->font_not_found_p so that we can draw rectangles for the
4322 characters of the glyph string. */
4323 if (s->font == NULL || glyph_not_available_p)
4324 {
4325 s->font_not_found_p = 1;
4326 s->font = FRAME_FONT (s->f);
4327 }
4328
4329 /* Adjust base line for subscript/superscript text. */
4330 s->ybase += voffset;
4331
4332 xassert (s->face && s->face->gc);
4333 return glyph - s->row->glyphs[s->area];
4334}
4335
4336
4337/* Fill glyph string S from image glyph S->first_glyph. */
4338
4339static void
4340x_fill_image_glyph_string (s)
4341 struct glyph_string *s;
4342{
4343 xassert (s->first_glyph->type == IMAGE_GLYPH);
4344 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4345 xassert (s->img);
4346 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4347 s->font = s->face->font;
4348 s->width = s->first_glyph->pixel_width;
4349
4350 /* Adjust base line for subscript/superscript text. */
4351 s->ybase += s->first_glyph->voffset;
4352}
4353
4354
4355/* Fill glyph string S from a sequence of stretch glyphs.
4356
4357 ROW is the glyph row in which the glyphs are found, AREA is the
4358 area within the row. START is the index of the first glyph to
4359 consider, END is the index of the last + 1.
4360
4361 Value is the index of the first glyph not in S. */
4362
4363static int
4364x_fill_stretch_glyph_string (s, row, area, start, end)
4365 struct glyph_string *s;
4366 struct glyph_row *row;
4367 enum glyph_row_area area;
4368 int start, end;
4369{
4370 struct glyph *glyph, *last;
4371 int voffset, face_id;
4372
4373 xassert (s->first_glyph->type == STRETCH_GLYPH);
4374
4375 glyph = s->row->glyphs[s->area] + start;
4376 last = s->row->glyphs[s->area] + end;
4377 face_id = glyph->face_id;
4378 s->face = FACE_FROM_ID (s->f, face_id);
4379 s->font = s->face->font;
4380 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4381 s->width = glyph->pixel_width;
4382 voffset = glyph->voffset;
4383
4384 for (++glyph;
4385 (glyph < last
4386 && glyph->type == STRETCH_GLYPH
4387 && glyph->voffset == voffset
4388 && glyph->face_id == face_id);
4389 ++glyph)
4390 s->width += glyph->pixel_width;
4391
4392 /* Adjust base line for subscript/superscript text. */
4393 s->ybase += voffset;
4394
4395 xassert (s->face && s->face->gc);
4396 return glyph - s->row->glyphs[s->area];
4397}
4398
4399
4400/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4401 of XChar2b structures for S; it can't be allocated in
4402 x_init_glyph_string because it must be allocated via `alloca'. W
4403 is the window on which S is drawn. ROW and AREA are the glyph row
4404 and area within the row from which S is constructed. START is the
4405 index of the first glyph structure covered by S. HL is a
4406 face-override for drawing S. */
4407
4408static void
4409x_init_glyph_string (s, char2b, w, row, area, start, hl)
4410 struct glyph_string *s;
4411 XChar2b *char2b;
4412 struct window *w;
4413 struct glyph_row *row;
4414 enum glyph_row_area area;
4415 int start;
4416 enum draw_glyphs_face hl;
4417{
4418 bzero (s, sizeof *s);
4419 s->w = w;
4420 s->f = XFRAME (w->frame);
4421 s->display = FRAME_X_DISPLAY (s->f);
4422 s->window = FRAME_X_WINDOW (s->f);
4423 s->char2b = char2b;
4424 s->hl = hl;
4425 s->row = row;
4426 s->area = area;
4427 s->first_glyph = row->glyphs[area] + start;
4428 s->height = row->height;
4429 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4430
4431 /* Display the internal border below the tool-bar window. */
4432 if (s->w == XWINDOW (s->f->tool_bar_window))
4433 s->y -= s->f->output_data.x->internal_border_width;
4434
4435 s->ybase = s->y + row->ascent;
4436}
4437
4438
4439/* Set background width of glyph string S. START is the index of the
4440 first glyph following S. LAST_X is the right-most x-position + 1
4441 in the drawing area. */
4442
4443static INLINE void
4444x_set_glyph_string_background_width (s, start, last_x)
4445 struct glyph_string *s;
4446 int start;
4447 int last_x;
4448{
4449 /* If the face of this glyph string has to be drawn to the end of
4450 the drawing area, set S->extends_to_end_of_line_p. */
4451 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4452
4453 if (start == s->row->used[s->area]
4454 && s->hl == DRAW_NORMAL_TEXT
4455 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4456 || s->face->background != default_face->background
4457 || s->face->stipple != default_face->stipple))
4458 s->extends_to_end_of_line_p = 1;
4459
4460 /* If S extends its face to the end of the line, set its
4461 background_width to the distance to the right edge of the drawing
4462 area. */
4463 if (s->extends_to_end_of_line_p)
4464 s->background_width = last_x - s->x + 1;
4465 else
4466 s->background_width = s->width;
4467}
4468
4469
4470/* Add a glyph string for a stretch glyph to the list of strings
4471 between HEAD and TAIL. START is the index of the stretch glyph in
4472 row area AREA of glyph row ROW. END is the index of the last glyph
4473 in that glyph row area. X is the current output position assigned
4474 to the new glyph string constructed. HL overrides that face of the
4475 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4476 is the right-most x-position of the drawing area. */
4477
4478/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4479 and below -- keep them on one line. */
4480#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4481 do \
4482 { \
4483 s = (struct glyph_string *) alloca (sizeof *s); \
4484 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4485 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4486 x_append_glyph_string (&HEAD, &TAIL, s); \
4487 s->x = (X); \
4488 } \
4489 while (0)
4490
4491
4492/* Add a glyph string for an image glyph to the list of strings
4493 between HEAD and TAIL. START is the index of the image glyph in
4494 row area AREA of glyph row ROW. END is the index of the last glyph
4495 in that glyph row area. X is the current output position assigned
4496 to the new glyph string constructed. HL overrides that face of the
4497 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4498 is the right-most x-position of the drawing area. */
4499
4500#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4501 do \
4502 { \
4503 s = (struct glyph_string *) alloca (sizeof *s); \
4504 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4505 x_fill_image_glyph_string (s); \
4506 x_append_glyph_string (&HEAD, &TAIL, s); \
4507 ++START; \
4508 s->x = (X); \
4509 } \
4510 while (0)
4511
4512
4513/* Add a glyph string for a sequence of character glyphs to the list
4514 of strings between HEAD and TAIL. START is the index of the first
4515 glyph in row area AREA of glyph row ROW that is part of the new
4516 glyph string. END is the index of the last glyph in that glyph row
4517 area. X is the current output position assigned to the new glyph
4518 string constructed. HL overrides that face of the glyph; e.g. it
4519 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4520 right-most x-position of the drawing area. */
4521
4522#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4523 do \
4524 { \
4525 int c, face_id; \
4526 XChar2b *char2b; \
4527 \
4528 c = (ROW)->glyphs[AREA][START].u.ch; \
4529 face_id = (ROW)->glyphs[AREA][START].face_id; \
4530 \
4531 s = (struct glyph_string *) alloca (sizeof *s); \
4532 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4533 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4534 x_append_glyph_string (&HEAD, &TAIL, s); \
4535 s->x = (X); \
4536 START = x_fill_glyph_string (s, face_id, START, END, \
4537 OVERLAPS_P); \
4538 } \
4539 while (0)
4540
4541
4542/* Add a glyph string for a composite sequence to the list of strings
4543 between HEAD and TAIL. START is the index of the first glyph in
4544 row area AREA of glyph row ROW that is part of the new glyph
4545 string. END is the index of the last glyph in that glyph row area.
4546 X is the current output position assigned to the new glyph string
4547 constructed. HL overrides that face of the glyph; e.g. it is
4548 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4549 x-position of the drawing area. */
4550
4551#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4552 do { \
4553 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4554 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4555 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4556 struct composition *cmp = composition_table[cmp_id]; \
4557 int glyph_len = cmp->glyph_len; \
4558 XChar2b *char2b; \
4559 struct face **faces; \
4560 struct glyph_string *first_s = NULL; \
4561 int n; \
4562 \
4563 base_face = base_face->ascii_face; \
4564 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4565 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4566 /* At first, fill in `char2b' and `faces'. */ \
4567 for (n = 0; n < glyph_len; n++) \
4568 { \
4569 int c = COMPOSITION_GLYPH (cmp, n); \
4570 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4571 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4572 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4573 this_face_id, char2b + n, 1); \
4574 } \
4575 \
4576 /* Make glyph_strings for each glyph sequence that is drawable by \
4577 the same face, and append them to HEAD/TAIL. */ \
4578 for (n = 0; n < cmp->glyph_len;) \
4579 { \
4580 s = (struct glyph_string *) alloca (sizeof *s); \
4581 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4582 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4583 s->cmp = cmp; \
4584 s->gidx = n; \
4585 s->x = (X); \
4586 \
4587 if (n == 0) \
4588 first_s = s; \
4589 \
4590 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4591 } \
4592 \
4593 ++START; \
4594 s = first_s; \
4595 } while (0)
4596
4597
4598/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4599 of AREA of glyph row ROW on window W between indices START and END.
4600 HL overrides the face for drawing glyph strings, e.g. it is
4601 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4602 x-positions of the drawing area.
4603
4604 This is an ugly monster macro construct because we must use alloca
4605 to allocate glyph strings (because x_draw_glyphs can be called
4606 asynchronously). */
4607
4608#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4609 do \
4610 { \
4611 HEAD = TAIL = NULL; \
4612 while (START < END) \
4613 { \
4614 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4615 switch (first_glyph->type) \
4616 { \
4617 case CHAR_GLYPH: \
4618 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4619 TAIL, HL, X, LAST_X, \
4620 OVERLAPS_P); \
4621 break; \
4622 \
4623 case COMPOSITE_GLYPH: \
4624 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4625 HEAD, TAIL, HL, X, LAST_X,\
4626 OVERLAPS_P); \
4627 break; \
4628 \
4629 case STRETCH_GLYPH: \
4630 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4631 HEAD, TAIL, HL, X, LAST_X); \
4632 break; \
4633 \
4634 case IMAGE_GLYPH: \
4635 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4636 TAIL, HL, X, LAST_X); \
4637 break; \
4638 \
4639 default: \
4640 abort (); \
4641 } \
4642 \
4643 x_set_glyph_string_background_width (s, START, LAST_X); \
4644 (X) += s->width; \
4645 } \
4646 } \
4647 while (0)
4648
4649
4650/* Draw glyphs between START and END in AREA of ROW on window W,
4651 starting at x-position X. X is relative to AREA in W. HL is a
4652 face-override with the following meaning:
4653
4654 DRAW_NORMAL_TEXT draw normally
4655 DRAW_CURSOR draw in cursor face
4656 DRAW_MOUSE_FACE draw in mouse face.
4657 DRAW_INVERSE_VIDEO draw in mode line face
4658 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4659 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4660
4661 If REAL_START is non-null, return in *REAL_START the real starting
4662 position for display. This can be different from START in case
4663 overlapping glyphs must be displayed. If REAL_END is non-null,
4664 return in *REAL_END the real end position for display. This can be
4665 different from END in case overlapping glyphs must be displayed.
4666
4667 If OVERLAPS_P is non-zero, draw only the foreground of characters
4668 and clip to the physical height of ROW.
4669
4670 Value is the x-position reached, relative to AREA of W. */
4671
4672static int
4673x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4674 overlaps_p)
4675 struct window *w;
4676 int x;
4677 struct glyph_row *row;
4678 enum glyph_row_area area;
4679 int start, end;
4680 enum draw_glyphs_face hl;
4681 int *real_start, *real_end;
4682 int overlaps_p;
4683{
4684 struct glyph_string *head, *tail;
4685 struct glyph_string *s;
4686 int last_x, area_width;
4687 int x_reached;
4688 int i, j;
4689
4690 /* Let's rather be paranoid than getting a SEGV. */
4691 end = min (end, row->used[area]);
4692 start = max (0, start);
4693 start = min (end, start);
4694 if (real_start)
4695 *real_start = start;
4696 if (real_end)
4697 *real_end = end;
4698
4699 /* Translate X to frame coordinates. Set last_x to the right
4700 end of the drawing area. */
4701 if (row->full_width_p)
4702 {
4703 /* X is relative to the left edge of W, without scroll bars
4704 or flag areas. */
4705 struct frame *f = XFRAME (w->frame);
4706 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4707 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4708
4709 x += window_left_x;
4710 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4711 last_x = window_left_x + area_width;
4712
4713 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4714 {
4715 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4716 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4717 last_x += width;
4718 else
4719 x -= width;
4720 }
4721
4722 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4723 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4724 }
4725 else
4726 {
4727 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4728 area_width = window_box_width (w, area);
4729 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4730 }
4731
4732 /* Build a doubly-linked list of glyph_string structures between
4733 head and tail from what we have to draw. Note that the macro
4734 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4735 the reason we use a separate variable `i'. */
4736 i = start;
4737 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4738 overlaps_p);
4739 if (tail)
4740 x_reached = tail->x + tail->background_width;
4741 else
4742 x_reached = x;
4743
4744 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4745 the row, redraw some glyphs in front or following the glyph
4746 strings built above. */
4747 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4748 {
4749 int dummy_x = 0;
4750 struct glyph_string *h, *t;
4751
4752 /* Compute overhangs for all glyph strings. */
4753 for (s = head; s; s = s->next)
4754 x_compute_glyph_string_overhangs (s);
4755
4756 /* Prepend glyph strings for glyphs in front of the first glyph
4757 string that are overwritten because of the first glyph
4758 string's left overhang. The background of all strings
4759 prepended must be drawn because the first glyph string
4760 draws over it. */
4761 i = x_left_overwritten (head);
4762 if (i >= 0)
4763 {
4764 j = i;
4765 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4766 DRAW_NORMAL_TEXT, dummy_x, last_x,
4767 overlaps_p);
4768 start = i;
4769 if (real_start)
4770 *real_start = start;
4771 x_compute_overhangs_and_x (t, head->x, 1);
4772 x_prepend_glyph_string_lists (&head, &tail, h, t);
4773 }
4774
4775 /* Prepend glyph strings for glyphs in front of the first glyph
4776 string that overwrite that glyph string because of their
4777 right overhang. For these strings, only the foreground must
4778 be drawn, because it draws over the glyph string at `head'.
4779 The background must not be drawn because this would overwrite
4780 right overhangs of preceding glyphs for which no glyph
4781 strings exist. */
4782 i = x_left_overwriting (head);
4783 if (i >= 0)
4784 {
4785 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4786 DRAW_NORMAL_TEXT, dummy_x, last_x,
4787 overlaps_p);
4788 for (s = h; s; s = s->next)
4789 s->background_filled_p = 1;
4790 if (real_start)
4791 *real_start = i;
4792 x_compute_overhangs_and_x (t, head->x, 1);
4793 x_prepend_glyph_string_lists (&head, &tail, h, t);
4794 }
4795
4796 /* Append glyphs strings for glyphs following the last glyph
4797 string tail that are overwritten by tail. The background of
4798 these strings has to be drawn because tail's foreground draws
4799 over it. */
4800 i = x_right_overwritten (tail);
4801 if (i >= 0)
4802 {
4803 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4804 DRAW_NORMAL_TEXT, x, last_x,
4805 overlaps_p);
4806 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4807 x_append_glyph_string_lists (&head, &tail, h, t);
4808 if (real_end)
4809 *real_end = i;
4810 }
4811
4812 /* Append glyph strings for glyphs following the last glyph
4813 string tail that overwrite tail. The foreground of such
4814 glyphs has to be drawn because it writes into the background
4815 of tail. The background must not be drawn because it could
4816 paint over the foreground of following glyphs. */
4817 i = x_right_overwriting (tail);
4818 if (i >= 0)
4819 {
4820 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4821 DRAW_NORMAL_TEXT, x, last_x,
4822 overlaps_p);
4823 for (s = h; s; s = s->next)
4824 s->background_filled_p = 1;
4825 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4826 x_append_glyph_string_lists (&head, &tail, h, t);
4827 if (real_end)
4828 *real_end = i;
4829 }
4830 }
4831
4832 /* Draw all strings. */
4833 for (s = head; s; s = s->next)
4834 x_draw_glyph_string (s);
4835
4836 /* Value is the x-position up to which drawn, relative to AREA of W.
4837 This doesn't include parts drawn because of overhangs. */
4838 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4839 if (!row->full_width_p)
4840 {
4841 if (area > LEFT_MARGIN_AREA)
4842 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4843 if (area > TEXT_AREA)
4844 x_reached -= window_box_width (w, TEXT_AREA);
4845 }
4846
4847 return x_reached;
4848}
4849
4850
4851/* Fix the display of area AREA of overlapping row ROW in window W. */
4852
4853static void
4854x_fix_overlapping_area (w, row, area)
4855 struct window *w;
4856 struct glyph_row *row;
4857 enum glyph_row_area area;
4858{
4859 int i, x;
4860
4861 BLOCK_INPUT;
4862
4863 if (area == LEFT_MARGIN_AREA)
4864 x = 0;
4865 else if (area == TEXT_AREA)
4866 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4867 else
4868 x = (window_box_width (w, LEFT_MARGIN_AREA)
4869 + window_box_width (w, TEXT_AREA));
4870
4871 for (i = 0; i < row->used[area];)
4872 {
4873 if (row->glyphs[area][i].overlaps_vertically_p)
4874 {
4875 int start = i, start_x = x;
4876
4877 do
4878 {
4879 x += row->glyphs[area][i].pixel_width;
4880 ++i;
4881 }
4882 while (i < row->used[area]
4883 && row->glyphs[area][i].overlaps_vertically_p);
4884
4885 x_draw_glyphs (w, start_x, row, area, start, i,
4886 (row->inverse_p
4887 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4888 NULL, NULL, 1);
4889 }
4890 else
4891 {
4892 x += row->glyphs[area][i].pixel_width;
4893 ++i;
4894 }
4895 }
4896
4897 UNBLOCK_INPUT;
4898}
4899
4900
4901/* Output LEN glyphs starting at START at the nominal cursor position.
4902 Advance the nominal cursor over the text. The global variable
4903 updated_window contains the window being updated, updated_row is
4904 the glyph row being updated, and updated_area is the area of that
4905 row being updated. */
4906
4907static void
4908x_write_glyphs (start, len)
4909 struct glyph *start;
4910 int len;
4911{
4912 int x, hpos, real_start, real_end;
4913
4914 xassert (updated_window && updated_row);
4915 BLOCK_INPUT;
4916
4917 /* Write glyphs. */
4918
4919 hpos = start - updated_row->glyphs[updated_area];
4920 x = x_draw_glyphs (updated_window, output_cursor.x,
4921 updated_row, updated_area,
4922 hpos, hpos + len,
4923 (updated_row->inverse_p
4924 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4925 &real_start, &real_end, 0);
4926
4927 /* If we drew over the cursor, note that it is not visible any more. */
4928 note_overwritten_text_cursor (updated_window, real_start,
4929 real_end - real_start);
4930
4931 UNBLOCK_INPUT;
4932
4933 /* Advance the output cursor. */
4934 output_cursor.hpos += len;
4935 output_cursor.x = x;
4936}
4937
4938
4939/* Insert LEN glyphs from START at the nominal cursor position. */
4940
4941static void
4942x_insert_glyphs (start, len)
4943 struct glyph *start;
4944 register int len;
4945{
4946 struct frame *f;
4947 struct window *w;
4948 int line_height, shift_by_width, shifted_region_width;
4949 struct glyph_row *row;
4950 struct glyph *glyph;
4951 int frame_x, frame_y, hpos, real_start, real_end;
4952
4953 xassert (updated_window && updated_row);
4954 BLOCK_INPUT;
4955 w = updated_window;
4956 f = XFRAME (WINDOW_FRAME (w));
4957
4958 /* Get the height of the line we are in. */
4959 row = updated_row;
4960 line_height = row->height;
4961
4962 /* Get the width of the glyphs to insert. */
4963 shift_by_width = 0;
4964 for (glyph = start; glyph < start + len; ++glyph)
4965 shift_by_width += glyph->pixel_width;
4966
4967 /* Get the width of the region to shift right. */
4968 shifted_region_width = (window_box_width (w, updated_area)
4969 - output_cursor.x
4970 - shift_by_width);
4971
4972 /* Shift right. */
4973 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4974 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4975 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4976 f->output_data.x->normal_gc,
4977 frame_x, frame_y,
4978 shifted_region_width, line_height,
4979 frame_x + shift_by_width, frame_y);
4980
4981 /* Write the glyphs. */
4982 hpos = start - row->glyphs[updated_area];
4983 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4984 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4985 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4986
4987 /* Advance the output cursor. */
4988 output_cursor.hpos += len;
4989 output_cursor.x += shift_by_width;
4990 UNBLOCK_INPUT;
4991}
4992
4993
4994/* Delete N glyphs at the nominal cursor position. Not implemented
4995 for X frames. */
4996
4997static void
4998x_delete_glyphs (n)
4999 register int n;
5000{
5001 abort ();
5002}
5003
5004
5005/* Erase the current text line from the nominal cursor position
5006 (inclusive) to pixel column TO_X (exclusive). The idea is that
5007 everything from TO_X onward is already erased.
5008
5009 TO_X is a pixel position relative to updated_area of
5010 updated_window. TO_X == -1 means clear to the end of this area. */
5011
5012static void
5013x_clear_end_of_line (to_x)
5014 int to_x;
5015{
5016 struct frame *f;
5017 struct window *w = updated_window;
5018 int max_x, min_y, max_y;
5019 int from_x, from_y, to_y;
5020
5021 xassert (updated_window && updated_row);
5022 f = XFRAME (w->frame);
5023
5024 if (updated_row->full_width_p)
5025 {
5026 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5027 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5028 && !w->pseudo_window_p)
5029 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5030 }
5031 else
5032 max_x = window_box_width (w, updated_area);
5033 max_y = window_text_bottom_y (w);
5034
5035 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5036 of window. For TO_X > 0, truncate to end of drawing area. */
5037 if (to_x == 0)
5038 return;
5039 else if (to_x < 0)
5040 to_x = max_x;
5041 else
5042 to_x = min (to_x, max_x);
5043
5044 to_y = min (max_y, output_cursor.y + updated_row->height);
5045
5046 /* Notice if the cursor will be cleared by this operation. */
5047 if (!updated_row->full_width_p)
5048 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5049
5050 from_x = output_cursor.x;
5051
5052 /* Translate to frame coordinates. */
5053 if (updated_row->full_width_p)
5054 {
5055 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5056 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5057 }
5058 else
5059 {
5060 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5061 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5062 }
5063
5064 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5065 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5066 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5067
5068 /* Prevent inadvertently clearing to end of the X window. */
5069 if (to_x > from_x && to_y > from_y)
5070 {
5071 BLOCK_INPUT;
5072 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5073 from_x, from_y, to_x - from_x, to_y - from_y,
5074 False);
5075 UNBLOCK_INPUT;
5076 }
5077}
5078
5079
5080/* Clear entire frame. If updating_frame is non-null, clear that
5081 frame. Otherwise clear the selected frame. */
5082
5083static void
5084x_clear_frame ()
5085{
5086 struct frame *f;
5087
5088 if (updating_frame)
5089 f = updating_frame;
5090 else
5091 f = SELECTED_FRAME ();
5092
5093 /* Clearing the frame will erase any cursor, so mark them all as no
5094 longer visible. */
5095 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5096 output_cursor.hpos = output_cursor.vpos = 0;
5097 output_cursor.x = -1;
5098
5099 /* We don't set the output cursor here because there will always
5100 follow an explicit cursor_to. */
5101 BLOCK_INPUT;
5102 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5103
5104 /* We have to clear the scroll bars, too. If we have changed
5105 colors or something like that, then they should be notified. */
5106 x_scroll_bar_clear (f);
5107
5108 XFlush (FRAME_X_DISPLAY (f));
5109 UNBLOCK_INPUT;
5110}
5111
5112
5113\f
5114/* Invert the middle quarter of the frame for .15 sec. */
5115
5116/* We use the select system call to do the waiting, so we have to make
5117 sure it's available. If it isn't, we just won't do visual bells. */
5118
5119#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5120
5121
5122/* Subtract the `struct timeval' values X and Y, storing the result in
5123 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5124
5125static int
5126timeval_subtract (result, x, y)
5127 struct timeval *result, x, y;
5128{
5129 /* Perform the carry for the later subtraction by updating y. This
5130 is safer because on some systems the tv_sec member is unsigned. */
5131 if (x.tv_usec < y.tv_usec)
5132 {
5133 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5134 y.tv_usec -= 1000000 * nsec;
5135 y.tv_sec += nsec;
5136 }
5137
5138 if (x.tv_usec - y.tv_usec > 1000000)
5139 {
5140 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5141 y.tv_usec += 1000000 * nsec;
5142 y.tv_sec -= nsec;
5143 }
5144
5145 /* Compute the time remaining to wait. tv_usec is certainly
5146 positive. */
5147 result->tv_sec = x.tv_sec - y.tv_sec;
5148 result->tv_usec = x.tv_usec - y.tv_usec;
5149
5150 /* Return indication of whether the result should be considered
5151 negative. */
5152 return x.tv_sec < y.tv_sec;
5153}
5154
5155void
5156XTflash (f)
5157 struct frame *f;
5158{
5159 BLOCK_INPUT;
5160
5161 {
5162 GC gc;
5163
5164 /* Create a GC that will use the GXxor function to flip foreground
5165 pixels into background pixels. */
5166 {
5167 XGCValues values;
5168
5169 values.function = GXxor;
5170 values.foreground = (f->output_data.x->foreground_pixel
5171 ^ f->output_data.x->background_pixel);
5172
5173 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5174 GCFunction | GCForeground, &values);
5175 }
5176
5177 {
5178 /* Get the height not including a menu bar widget. */
5179 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5180 /* Height of each line to flash. */
5181 int flash_height = FRAME_LINE_HEIGHT (f);
5182 /* These will be the left and right margins of the rectangles. */
5183 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5184 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5185
5186 int width;
5187
5188 /* Don't flash the area between a scroll bar and the frame
5189 edge it is next to. */
5190 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5191 {
5192 case vertical_scroll_bar_left:
5193 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5194 break;
5195
5196 case vertical_scroll_bar_right:
5197 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5198 break;
5199
5200 default:
5201 break;
5202 }
5203
5204 width = flash_right - flash_left;
5205
5206 /* If window is tall, flash top and bottom line. */
5207 if (height > 3 * FRAME_LINE_HEIGHT (f))
5208 {
5209 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5210 flash_left,
5211 (FRAME_INTERNAL_BORDER_WIDTH (f)
5212 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5213 width, flash_height);
5214 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5215 flash_left,
5216 (height - flash_height
5217 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5218 width, flash_height);
5219 }
5220 else
5221 /* If it is short, flash it all. */
5222 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5223 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5224 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5225
5226 x_flush (f);
5227
5228 {
5229 struct timeval wakeup;
5230
5231 EMACS_GET_TIME (wakeup);
5232
5233 /* Compute time to wait until, propagating carry from usecs. */
5234 wakeup.tv_usec += 150000;
5235 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5236 wakeup.tv_usec %= 1000000;
5237
5238 /* Keep waiting until past the time wakeup. */
5239 while (1)
5240 {
5241 struct timeval timeout;
5242
5243 EMACS_GET_TIME (timeout);
5244
5245 /* In effect, timeout = wakeup - timeout.
5246 Break if result would be negative. */
5247 if (timeval_subtract (&timeout, wakeup, timeout))
5248 break;
5249
5250 /* Try to wait that long--but we might wake up sooner. */
5251 select (0, NULL, NULL, NULL, &timeout);
5252 }
5253 }
5254
5255 /* If window is tall, flash top and bottom line. */
5256 if (height > 3 * FRAME_LINE_HEIGHT (f))
5257 {
5258 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5259 flash_left,
5260 (FRAME_INTERNAL_BORDER_WIDTH (f)
5261 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5262 width, flash_height);
5263 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5264 flash_left,
5265 (height - flash_height
5266 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5267 width, flash_height);
5268 }
5269 else
5270 /* If it is short, flash it all. */
5271 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5272 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5273 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5274
5275 XFreeGC (FRAME_X_DISPLAY (f), gc);
5276 x_flush (f);
5277 }
5278 }
5279
5280 UNBLOCK_INPUT;
5281}
5282
5283#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5284
5285
5286/* Make audible bell. */
5287
5288void
5289XTring_bell ()
5290{
5291 struct frame *f = SELECTED_FRAME ();
5292
5293 if (FRAME_X_DISPLAY (f))
5294 {
5295#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5296 if (visible_bell)
5297 XTflash (f);
5298 else
5299#endif
5300 {
5301 BLOCK_INPUT;
5302 XBell (FRAME_X_DISPLAY (f), 0);
5303 XFlush (FRAME_X_DISPLAY (f));
5304 UNBLOCK_INPUT;
5305 }
5306 }
5307}
5308
5309\f
5310/* Specify how many text lines, from the top of the window,
5311 should be affected by insert-lines and delete-lines operations.
5312 This, and those operations, are used only within an update
5313 that is bounded by calls to x_update_begin and x_update_end. */
5314
5315static void
5316XTset_terminal_window (n)
5317 register int n;
5318{
5319 /* This function intentionally left blank. */
5320}
5321
5322
5323\f
5324/***********************************************************************
5325 Line Dance
5326 ***********************************************************************/
5327
5328/* Perform an insert-lines or delete-lines operation, inserting N
5329 lines or deleting -N lines at vertical position VPOS. */
5330
5331static void
5332x_ins_del_lines (vpos, n)
5333 int vpos, n;
5334{
5335 abort ();
5336}
5337
5338
5339/* Scroll part of the display as described by RUN. */
5340
5341static void
5342x_scroll_run (w, run)
5343 struct window *w;
5344 struct run *run;
5345{
5346 struct frame *f = XFRAME (w->frame);
5347 int x, y, width, height, from_y, to_y, bottom_y;
5348
5349 /* Get frame-relative bounding box of the text display area of W,
5350 without mode lines. Include in this box the flags areas to the
5351 left and right of W. */
5352 window_box (w, -1, &x, &y, &width, &height);
5353 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5354 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5355
5356 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5357 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5358 bottom_y = y + height;
5359
5360 if (to_y < from_y)
5361 {
5362 /* Scrolling up. Make sure we don't copy part of the mode
5363 line at the bottom. */
5364 if (from_y + run->height > bottom_y)
5365 height = bottom_y - from_y;
5366 else
5367 height = run->height;
5368 }
5369 else
5370 {
5371 /* Scolling down. Make sure we don't copy over the mode line.
5372 at the bottom. */
5373 if (to_y + run->height > bottom_y)
5374 height = bottom_y - to_y;
5375 else
5376 height = run->height;
5377 }
5378
5379 BLOCK_INPUT;
5380
5381 /* Cursor off. Will be switched on again in x_update_window_end. */
5382 updated_window = w;
5383 x_clear_cursor (w);
5384
5385 XCopyArea (FRAME_X_DISPLAY (f),
5386 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5387 f->output_data.x->normal_gc,
5388 x, from_y,
5389 width, height,
5390 x, to_y);
5391
5392 UNBLOCK_INPUT;
5393}
5394
5395
5396\f
5397/***********************************************************************
5398 Exposure Events
5399 ***********************************************************************/
5400
5401/* Redisplay an exposed area of frame F. X and Y are the upper-left
5402 corner of the exposed rectangle. W and H are width and height of
5403 the exposed area. All are pixel values. W or H zero means redraw
5404 the entire frame. */
5405
5406static void
5407expose_frame (f, x, y, w, h)
5408 struct frame *f;
5409 int x, y, w, h;
5410{
5411 XRectangle r;
5412
5413 TRACE ((stderr, "expose_frame "));
5414
5415 /* No need to redraw if frame will be redrawn soon. */
5416 if (FRAME_GARBAGED_P (f))
5417 {
5418 TRACE ((stderr, " garbaged\n"));
5419 return;
5420 }
5421
5422 /* If basic faces haven't been realized yet, there is no point in
5423 trying to redraw anything. This can happen when we get an expose
5424 event while Emacs is starting, e.g. by moving another window. */
5425 if (FRAME_FACE_CACHE (f) == NULL
5426 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5427 {
5428 TRACE ((stderr, " no faces\n"));
5429 return;
5430 }
5431
5432 if (w == 0 || h == 0)
5433 {
5434 r.x = r.y = 0;
5435 r.width = CANON_X_UNIT (f) * f->width;
5436 r.height = CANON_Y_UNIT (f) * f->height;
5437 }
5438 else
5439 {
5440 r.x = x;
5441 r.y = y;
5442 r.width = w;
5443 r.height = h;
5444 }
5445
5446 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5447 expose_window_tree (XWINDOW (f->root_window), &r);
5448
5449 if (WINDOWP (f->tool_bar_window))
5450 {
5451 struct window *w = XWINDOW (f->tool_bar_window);
5452 XRectangle window_rect;
5453 XRectangle intersection_rect;
5454 int window_x, window_y, window_width, window_height;
5455
5456
5457 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5458 window_rect.x = window_x;
5459 window_rect.y = window_y;
5460 window_rect.width = window_width;
5461 window_rect.height = window_height;
5462
5463 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5464 expose_window (w, &intersection_rect);
5465 }
5466
5467#ifndef USE_X_TOOLKIT
5468 if (WINDOWP (f->menu_bar_window))
5469 {
5470 struct window *w = XWINDOW (f->menu_bar_window);
5471 XRectangle window_rect;
5472 XRectangle intersection_rect;
5473 int window_x, window_y, window_width, window_height;
5474
5475
5476 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5477 window_rect.x = window_x;
5478 window_rect.y = window_y;
5479 window_rect.width = window_width;
5480 window_rect.height = window_height;
5481
5482 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5483 expose_window (w, &intersection_rect);
5484 }
5485#endif /* not USE_X_TOOLKIT */
5486}
5487
5488
5489/* Redraw (parts) of all windows in the window tree rooted at W that
5490 intersect R. R contains frame pixel coordinates. */
5491
5492static void
5493expose_window_tree (w, r)
5494 struct window *w;
5495 XRectangle *r;
5496{
5497 while (w)
5498 {
5499 if (!NILP (w->hchild))
5500 expose_window_tree (XWINDOW (w->hchild), r);
5501 else if (!NILP (w->vchild))
5502 expose_window_tree (XWINDOW (w->vchild), r);
5503 else
5504 {
5505 XRectangle window_rect;
5506 XRectangle intersection_rect;
5507 struct frame *f = XFRAME (w->frame);
5508 int window_x, window_y, window_width, window_height;
5509
5510 /* Frame-relative pixel rectangle of W. */
5511 window_box (w, -1, &window_x, &window_y, &window_width,
5512 &window_height);
5513 window_rect.x
5514 = (window_x
5515 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5516 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5517 window_rect.y = window_y;
5518 window_rect.width
5519 = (window_width
5520 + FRAME_X_FLAGS_AREA_WIDTH (f)
5521 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5522 window_rect.height
5523 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5524
5525 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5526 expose_window (w, &intersection_rect);
5527 }
5528
5529 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5530 }
5531}
5532
5533
5534/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5535 which intersects rectangle R. R is in window-relative coordinates. */
5536
5537static void
5538expose_area (w, row, r, area)
5539 struct window *w;
5540 struct glyph_row *row;
5541 XRectangle *r;
5542 enum glyph_row_area area;
5543{
5544 int x;
5545 struct glyph *first = row->glyphs[area];
5546 struct glyph *end = row->glyphs[area] + row->used[area];
5547 struct glyph *last;
5548 int first_x;
5549
5550 /* Set x to the window-relative start position for drawing glyphs of
5551 AREA. The first glyph of the text area can be partially visible.
5552 The first glyphs of other areas cannot. */
5553 if (area == LEFT_MARGIN_AREA)
5554 x = 0;
5555 else if (area == TEXT_AREA)
5556 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5557 else
5558 x = (window_box_width (w, LEFT_MARGIN_AREA)
5559 + window_box_width (w, TEXT_AREA));
5560
5561 if (area == TEXT_AREA && row->fill_line_p)
5562 /* If row extends face to end of line write the whole line. */
5563 x_draw_glyphs (w, x, row, area,
5564 0, row->used[area],
5565 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5566 NULL, NULL, 0);
5567 else
5568 {
5569 /* Find the first glyph that must be redrawn. */
5570 while (first < end
5571 && x + first->pixel_width < r->x)
5572 {
5573 x += first->pixel_width;
5574 ++first;
5575 }
5576
5577 /* Find the last one. */
5578 last = first;
5579 first_x = x;
5580 while (last < end
5581 && x < r->x + r->width)
5582 {
5583 x += last->pixel_width;
5584 ++last;
5585 }
5586
5587 /* Repaint. */
5588 if (last > first)
5589 x_draw_glyphs (w, first_x, row, area,
5590 first - row->glyphs[area],
5591 last - row->glyphs[area],
5592 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5593 NULL, NULL, 0);
5594 }
5595}
5596
5597
5598/* Redraw the parts of the glyph row ROW on window W intersecting
5599 rectangle R. R is in window-relative coordinates. */
5600
5601static void
5602expose_line (w, row, r)
5603 struct window *w;
5604 struct glyph_row *row;
5605 XRectangle *r;
5606{
5607 xassert (row->enabled_p);
5608
5609 if (row->mode_line_p || w->pseudo_window_p)
5610 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5611 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5612 NULL, NULL, 0);
5613 else
5614 {
5615 if (row->used[LEFT_MARGIN_AREA])
5616 expose_area (w, row, r, LEFT_MARGIN_AREA);
5617 if (row->used[TEXT_AREA])
5618 expose_area (w, row, r, TEXT_AREA);
5619 if (row->used[RIGHT_MARGIN_AREA])
5620 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5621 x_draw_row_bitmaps (w, row);
5622 }
5623}
5624
5625
5626/* Return non-zero if W's cursor intersects rectangle R. */
5627
5628static int
5629x_phys_cursor_in_rect_p (w, r)
5630 struct window *w;
5631 XRectangle *r;
5632{
5633 XRectangle cr, result;
5634 struct glyph *cursor_glyph;
5635
5636 cursor_glyph = get_phys_cursor_glyph (w);
5637 if (cursor_glyph)
5638 {
5639 cr.x = w->phys_cursor.x;
5640 cr.y = w->phys_cursor.y;
5641 cr.width = cursor_glyph->pixel_width;
5642 cr.height = w->phys_cursor_height;
5643 return x_intersect_rectangles (&cr, r, &result);
5644 }
5645 else
5646 return 0;
5647}
5648
5649
5650/* Redraw a rectangle of window W. R is a rectangle in window
5651 relative coordinates. Call this function with input blocked. */
5652
5653static void
5654expose_window (w, r)
5655 struct window *w;
5656 XRectangle *r;
5657{
5658 struct glyph_row *row;
5659 int y;
5660 int yb = window_text_bottom_y (w);
5661 int cursor_cleared_p;
5662
5663 /* If window is not yet fully initialized, do nothing. This can
5664 happen when toolkit scroll bars are used and a window is split.
5665 Reconfiguring the scroll bar will generate an expose for a newly
5666 created window. */
5667 if (w->current_matrix == NULL)
5668 return;
5669
5670 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5671 r->x, r->y, r->width, r->height));
5672
5673 /* Convert to window coordinates. */
5674 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5675 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5676
5677 /* Turn off the cursor. */
5678 if (!w->pseudo_window_p
5679 && x_phys_cursor_in_rect_p (w, r))
5680 {
5681 x_clear_cursor (w);
5682 cursor_cleared_p = 1;
5683 }
5684 else
5685 cursor_cleared_p = 0;
5686
5687 /* Find the first row intersecting the rectangle R. */
5688 row = w->current_matrix->rows;
5689 y = 0;
5690 while (row->enabled_p
5691 && y < yb
5692 && y + row->height < r->y)
5693 {
5694 y += row->height;
5695 ++row;
5696 }
5697
5698 /* Display the text in the rectangle, one text line at a time. */
5699 while (row->enabled_p
5700 && y < yb
5701 && y < r->y + r->height)
5702 {
5703 expose_line (w, row, r);
5704 y += row->height;
5705 ++row;
5706 }
5707
5708 /* Display the mode line if there is one. */
5709 if (WINDOW_WANTS_MODELINE_P (w)
5710 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5711 row->enabled_p)
5712 && row->y < r->y + r->height)
5713 expose_line (w, row, r);
5714
5715 if (!w->pseudo_window_p)
5716 {
5717 /* Draw border between windows. */
5718 x_draw_vertical_border (w);
5719
5720 /* Turn the cursor on again. */
5721 if (cursor_cleared_p)
5722 x_update_window_cursor (w, 1);
5723 }
5724}
5725
5726
5727/* Determine the intersection of two rectangles R1 and R2. Return
5728 the intersection in *RESULT. Value is non-zero if RESULT is not
5729 empty. */
5730
5731static int
5732x_intersect_rectangles (r1, r2, result)
5733 XRectangle *r1, *r2, *result;
5734{
5735 XRectangle *left, *right;
5736 XRectangle *upper, *lower;
5737 int intersection_p = 0;
5738
5739 /* Rearrange so that R1 is the left-most rectangle. */
5740 if (r1->x < r2->x)
5741 left = r1, right = r2;
5742 else
5743 left = r2, right = r1;
5744
5745 /* X0 of the intersection is right.x0, if this is inside R1,
5746 otherwise there is no intersection. */
5747 if (right->x <= left->x + left->width)
5748 {
5749 result->x = right->x;
5750
5751 /* The right end of the intersection is the minimum of the
5752 the right ends of left and right. */
5753 result->width = (min (left->x + left->width, right->x + right->width)
5754 - result->x);
5755
5756 /* Same game for Y. */
5757 if (r1->y < r2->y)
5758 upper = r1, lower = r2;
5759 else
5760 upper = r2, lower = r1;
5761
5762 /* The upper end of the intersection is lower.y0, if this is inside
5763 of upper. Otherwise, there is no intersection. */
5764 if (lower->y <= upper->y + upper->height)
5765 {
5766 result->y = lower->y;
5767
5768 /* The lower end of the intersection is the minimum of the lower
5769 ends of upper and lower. */
5770 result->height = (min (lower->y + lower->height,
5771 upper->y + upper->height)
5772 - result->y);
5773 intersection_p = 1;
5774 }
5775 }
5776
5777 return intersection_p;
5778}
5779
5780
5781
5782
5783\f
5784static void
5785frame_highlight (f)
5786 struct frame *f;
5787{
5788 /* We used to only do this if Vx_no_window_manager was non-nil, but
5789 the ICCCM (section 4.1.6) says that the window's border pixmap
5790 and border pixel are window attributes which are "private to the
5791 client", so we can always change it to whatever we want. */
5792 BLOCK_INPUT;
5793 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5794 f->output_data.x->border_pixel);
5795 UNBLOCK_INPUT;
5796 x_update_cursor (f, 1);
5797}
5798
5799static void
5800frame_unhighlight (f)
5801 struct frame *f;
5802{
5803 /* We used to only do this if Vx_no_window_manager was non-nil, but
5804 the ICCCM (section 4.1.6) says that the window's border pixmap
5805 and border pixel are window attributes which are "private to the
5806 client", so we can always change it to whatever we want. */
5807 BLOCK_INPUT;
5808 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5809 f->output_data.x->border_tile);
5810 UNBLOCK_INPUT;
5811 x_update_cursor (f, 1);
5812}
5813
5814/* The focus has changed. Update the frames as necessary to reflect
5815 the new situation. Note that we can't change the selected frame
5816 here, because the Lisp code we are interrupting might become confused.
5817 Each event gets marked with the frame in which it occurred, so the
5818 Lisp code can tell when the switch took place by examining the events. */
5819
5820static void
5821x_new_focus_frame (dpyinfo, frame)
5822 struct x_display_info *dpyinfo;
5823 struct frame *frame;
5824{
5825 struct frame *old_focus = dpyinfo->x_focus_frame;
5826
5827 if (frame != dpyinfo->x_focus_frame)
5828 {
5829 /* Set this before calling other routines, so that they see
5830 the correct value of x_focus_frame. */
5831 dpyinfo->x_focus_frame = frame;
5832
5833 if (old_focus && old_focus->auto_lower)
5834 x_lower_frame (old_focus);
5835
5836#if 0
5837 selected_frame = frame;
5838 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5839 selected_frame);
5840 Fselect_window (selected_frame->selected_window);
5841 choose_minibuf_frame ();
5842#endif /* ! 0 */
5843
5844 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5845 pending_autoraise_frame = dpyinfo->x_focus_frame;
5846 else
5847 pending_autoraise_frame = 0;
5848 }
5849
5850 x_frame_rehighlight (dpyinfo);
5851}
5852
5853/* Handle an event saying the mouse has moved out of an Emacs frame. */
5854
5855void
5856x_mouse_leave (dpyinfo)
5857 struct x_display_info *dpyinfo;
5858{
5859 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5860}
5861
5862/* The focus has changed, or we have redirected a frame's focus to
5863 another frame (this happens when a frame uses a surrogate
5864 mini-buffer frame). Shift the highlight as appropriate.
5865
5866 The FRAME argument doesn't necessarily have anything to do with which
5867 frame is being highlighted or un-highlighted; we only use it to find
5868 the appropriate X display info. */
5869
5870static void
5871XTframe_rehighlight (frame)
5872 struct frame *frame;
5873{
5874 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5875}
5876
5877static void
5878x_frame_rehighlight (dpyinfo)
5879 struct x_display_info *dpyinfo;
5880{
5881 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5882
5883 if (dpyinfo->x_focus_frame)
5884 {
5885 dpyinfo->x_highlight_frame
5886 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5887 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5888 : dpyinfo->x_focus_frame);
5889 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5890 {
5891 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5892 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5893 }
5894 }
5895 else
5896 dpyinfo->x_highlight_frame = 0;
5897
5898 if (dpyinfo->x_highlight_frame != old_highlight)
5899 {
5900 if (old_highlight)
5901 frame_unhighlight (old_highlight);
5902 if (dpyinfo->x_highlight_frame)
5903 frame_highlight (dpyinfo->x_highlight_frame);
5904 }
5905}
5906
5907
5908\f
5909/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5910
5911/* Initialize mode_switch_bit and modifier_meaning. */
5912static void
5913x_find_modifier_meanings (dpyinfo)
5914 struct x_display_info *dpyinfo;
5915{
5916 int min_code, max_code;
5917 KeySym *syms;
5918 int syms_per_code;
5919 XModifierKeymap *mods;
5920
5921 dpyinfo->meta_mod_mask = 0;
5922 dpyinfo->shift_lock_mask = 0;
5923 dpyinfo->alt_mod_mask = 0;
5924 dpyinfo->super_mod_mask = 0;
5925 dpyinfo->hyper_mod_mask = 0;
5926
5927#ifdef HAVE_X11R4
5928 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5929#else
5930 min_code = dpyinfo->display->min_keycode;
5931 max_code = dpyinfo->display->max_keycode;
5932#endif
5933
5934 syms = XGetKeyboardMapping (dpyinfo->display,
5935 min_code, max_code - min_code + 1,
5936 &syms_per_code);
5937 mods = XGetModifierMapping (dpyinfo->display);
5938
5939 /* Scan the modifier table to see which modifier bits the Meta and
5940 Alt keysyms are on. */
5941 {
5942 int row, col; /* The row and column in the modifier table. */
5943
5944 for (row = 3; row < 8; row++)
5945 for (col = 0; col < mods->max_keypermod; col++)
5946 {
5947 KeyCode code
5948 = mods->modifiermap[(row * mods->max_keypermod) + col];
5949
5950 /* Zeroes are used for filler. Skip them. */
5951 if (code == 0)
5952 continue;
5953
5954 /* Are any of this keycode's keysyms a meta key? */
5955 {
5956 int code_col;
5957
5958 for (code_col = 0; code_col < syms_per_code; code_col++)
5959 {
5960 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5961
5962 switch (sym)
5963 {
5964 case XK_Meta_L:
5965 case XK_Meta_R:
5966 dpyinfo->meta_mod_mask |= (1 << row);
5967 break;
5968
5969 case XK_Alt_L:
5970 case XK_Alt_R:
5971 dpyinfo->alt_mod_mask |= (1 << row);
5972 break;
5973
5974 case XK_Hyper_L:
5975 case XK_Hyper_R:
5976 dpyinfo->hyper_mod_mask |= (1 << row);
5977 break;
5978
5979 case XK_Super_L:
5980 case XK_Super_R:
5981 dpyinfo->super_mod_mask |= (1 << row);
5982 break;
5983
5984 case XK_Shift_Lock:
5985 /* Ignore this if it's not on the lock modifier. */
5986 if ((1 << row) == LockMask)
5987 dpyinfo->shift_lock_mask = LockMask;
5988 break;
5989 }
5990 }
5991 }
5992 }
5993 }
5994
5995 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5996 if (! dpyinfo->meta_mod_mask)
5997 {
5998 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5999 dpyinfo->alt_mod_mask = 0;
6000 }
6001
6002 /* If some keys are both alt and meta,
6003 make them just meta, not alt. */
6004 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6005 {
6006 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6007 }
6008
6009 XFree ((char *) syms);
6010 XFreeModifiermap (mods);
6011}
6012
6013/* Convert between the modifier bits X uses and the modifier bits
6014 Emacs uses. */
6015
6016static unsigned int
6017x_x_to_emacs_modifiers (dpyinfo, state)
6018 struct x_display_info *dpyinfo;
6019 unsigned int state;
6020{
6021 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6022 | ((state & ControlMask) ? ctrl_modifier : 0)
6023 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6024 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6025 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6026 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6027}
6028
6029static unsigned int
6030x_emacs_to_x_modifiers (dpyinfo, state)
6031 struct x_display_info *dpyinfo;
6032 unsigned int state;
6033{
6034 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6035 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6036 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6037 | ((state & shift_modifier) ? ShiftMask : 0)
6038 | ((state & ctrl_modifier) ? ControlMask : 0)
6039 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6040}
6041
6042/* Convert a keysym to its name. */
6043
6044char *
6045x_get_keysym_name (keysym)
6046 KeySym keysym;
6047{
6048 char *value;
6049
6050 BLOCK_INPUT;
6051 value = XKeysymToString (keysym);
6052 UNBLOCK_INPUT;
6053
6054 return value;
6055}
6056
6057
6058\f
6059/* Mouse clicks and mouse movement. Rah. */
6060
6061/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6062 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6063 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6064 not force the value into range. */
6065
6066void
6067pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6068 FRAME_PTR f;
6069 register int pix_x, pix_y;
6070 register int *x, *y;
6071 XRectangle *bounds;
6072 int noclip;
6073{
6074 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6075 even for negative values. */
6076 if (pix_x < 0)
6077 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6078 if (pix_y < 0)
6079 pix_y -= (f)->output_data.x->line_height - 1;
6080
6081 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6082 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6083
6084 if (bounds)
6085 {
6086 bounds->width = FONT_WIDTH (f->output_data.x->font);
6087 bounds->height = f->output_data.x->line_height;
6088 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6089 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6090 }
6091
6092 if (!noclip)
6093 {
6094 if (pix_x < 0)
6095 pix_x = 0;
6096 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6097 pix_x = FRAME_WINDOW_WIDTH (f);
6098
6099 if (pix_y < 0)
6100 pix_y = 0;
6101 else if (pix_y > f->height)
6102 pix_y = f->height;
6103 }
6104
6105 *x = pix_x;
6106 *y = pix_y;
6107}
6108
6109
6110/* Given HPOS/VPOS in the current matrix of W, return corresponding
6111 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6112 can't tell the positions because W's display is not up to date,
6113 return 0. */
6114
6115int
6116glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6117 struct window *w;
6118 int hpos, vpos;
6119 int *frame_x, *frame_y;
6120{
6121 int success_p;
6122
6123 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6124 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6125
6126 if (display_completed)
6127 {
6128 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6129 struct glyph *glyph = row->glyphs[TEXT_AREA];
6130 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6131
6132 *frame_y = row->y;
6133 *frame_x = row->x;
6134 while (glyph < end)
6135 {
6136 *frame_x += glyph->pixel_width;
6137 ++glyph;
6138 }
6139
6140 success_p = 1;
6141 }
6142 else
6143 {
6144 *frame_y = *frame_x = 0;
6145 success_p = 0;
6146 }
6147
6148 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6149 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6150 return success_p;
6151}
6152
6153
6154/* Prepare a mouse-event in *RESULT for placement in the input queue.
6155
6156 If the event is a button press, then note that we have grabbed
6157 the mouse. */
6158
6159static Lisp_Object
6160construct_mouse_click (result, event, f)
6161 struct input_event *result;
6162 XButtonEvent *event;
6163 struct frame *f;
6164{
6165 /* Make the event type no_event; we'll change that when we decide
6166 otherwise. */
6167 result->kind = mouse_click;
6168 result->code = event->button - Button1;
6169 result->timestamp = event->time;
6170 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6171 event->state)
6172 | (event->type == ButtonRelease
6173 ? up_modifier
6174 : down_modifier));
6175
6176 XSETINT (result->x, event->x);
6177 XSETINT (result->y, event->y);
6178 XSETFRAME (result->frame_or_window, f);
6179 result->arg = Qnil;
6180 return Qnil;
6181}
6182
6183\f
6184/* Function to report a mouse movement to the mainstream Emacs code.
6185 The input handler calls this.
6186
6187 We have received a mouse movement event, which is given in *event.
6188 If the mouse is over a different glyph than it was last time, tell
6189 the mainstream emacs code by setting mouse_moved. If not, ask for
6190 another motion event, so we can check again the next time it moves. */
6191
6192static XMotionEvent last_mouse_motion_event;
6193static Lisp_Object last_mouse_motion_frame;
6194
6195static void
6196note_mouse_movement (frame, event)
6197 FRAME_PTR frame;
6198 XMotionEvent *event;
6199{
6200 last_mouse_movement_time = event->time;
6201 last_mouse_motion_event = *event;
6202 XSETFRAME (last_mouse_motion_frame, frame);
6203
6204 if (event->window != FRAME_X_WINDOW (frame))
6205 {
6206 frame->mouse_moved = 1;
6207 last_mouse_scroll_bar = Qnil;
6208 note_mouse_highlight (frame, -1, -1);
6209 }
6210
6211 /* Has the mouse moved off the glyph it was on at the last sighting? */
6212 else if (event->x < last_mouse_glyph.x
6213 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6214 || event->y < last_mouse_glyph.y
6215 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6216 {
6217 frame->mouse_moved = 1;
6218 last_mouse_scroll_bar = Qnil;
6219 note_mouse_highlight (frame, event->x, event->y);
6220 }
6221}
6222
6223/* This is used for debugging, to turn off note_mouse_highlight. */
6224
6225 int disable_mouse_highlight;
6226
6227
6228\f
6229/************************************************************************
6230 Mouse Face
6231 ************************************************************************/
6232
6233/* Find the glyph under window-relative coordinates X/Y in window W.
6234 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6235 strings. Return in *HPOS and *VPOS the row and column number of
6236 the glyph found. Return in *AREA the glyph area containing X.
6237 Value is a pointer to the glyph found or null if X/Y is not on
6238 text, or we can't tell because W's current matrix is not up to
6239 date. */
6240
6241static struct glyph *
6242x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6243 struct window *w;
6244 int x, y;
6245 int *hpos, *vpos, *area;
6246{
6247 struct glyph *glyph, *end;
6248 struct glyph_row *row = NULL;
6249 int x0, i, left_area_width;
6250
6251 /* Find row containing Y. Give up if some row is not enabled. */
6252 for (i = 0; i < w->current_matrix->nrows; ++i)
6253 {
6254 row = MATRIX_ROW (w->current_matrix, i);
6255 if (!row->enabled_p)
6256 return NULL;
6257 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6258 break;
6259 }
6260
6261 *vpos = i;
6262 *hpos = 0;
6263
6264 /* Give up if Y is not in the window. */
6265 if (i == w->current_matrix->nrows)
6266 return NULL;
6267
6268 /* Get the glyph area containing X. */
6269 if (w->pseudo_window_p)
6270 {
6271 *area = TEXT_AREA;
6272 x0 = 0;
6273 }
6274 else
6275 {
6276 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6277 if (x < left_area_width)
6278 {
6279 *area = LEFT_MARGIN_AREA;
6280 x0 = 0;
6281 }
6282 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6283 {
6284 *area = TEXT_AREA;
6285 x0 = row->x + left_area_width;
6286 }
6287 else
6288 {
6289 *area = RIGHT_MARGIN_AREA;
6290 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6291 }
6292 }
6293
6294 /* Find glyph containing X. */
6295 glyph = row->glyphs[*area];
6296 end = glyph + row->used[*area];
6297 while (glyph < end)
6298 {
6299 if (x < x0 + glyph->pixel_width)
6300 {
6301 if (w->pseudo_window_p)
6302 break;
6303 else if (BUFFERP (glyph->object))
6304 break;
6305 }
6306
6307 x0 += glyph->pixel_width;
6308 ++glyph;
6309 }
6310
6311 if (glyph == end)
6312 return NULL;
6313
6314 *hpos = glyph - row->glyphs[*area];
6315 return glyph;
6316}
6317
6318
6319/* Convert frame-relative x/y to coordinates relative to window W.
6320 Takes pseudo-windows into account. */
6321
6322static void
6323frame_to_window_pixel_xy (w, x, y)
6324 struct window *w;
6325 int *x, *y;
6326{
6327 if (w->pseudo_window_p)
6328 {
6329 /* A pseudo-window is always full-width, and starts at the
6330 left edge of the frame, plus a frame border. */
6331 struct frame *f = XFRAME (w->frame);
6332 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6333 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6334 }
6335 else
6336 {
6337 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6338 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6339 }
6340}
6341
6342
6343/* Take proper action when mouse has moved to the mode or top line of
6344 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6345 mode line. X is relative to the start of the text display area of
6346 W, so the width of bitmap areas and scroll bars must be subtracted
6347 to get a position relative to the start of the mode line. */
6348
6349static void
6350note_mode_line_highlight (w, x, mode_line_p)
6351 struct window *w;
6352 int x, mode_line_p;
6353{
6354 struct frame *f = XFRAME (w->frame);
6355 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6356 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6357 struct glyph_row *row;
6358
6359 if (mode_line_p)
6360 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6361 else
6362 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6363
6364 if (row->enabled_p)
6365 {
6366 struct glyph *glyph, *end;
6367 Lisp_Object help, map;
6368 int x0;
6369
6370 /* Find the glyph under X. */
6371 glyph = row->glyphs[TEXT_AREA];
6372 end = glyph + row->used[TEXT_AREA];
6373 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6374 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6375 while (glyph < end
6376 && x >= x0 + glyph->pixel_width)
6377 {
6378 x0 += glyph->pixel_width;
6379 ++glyph;
6380 }
6381
6382 if (glyph < end
6383 && STRINGP (glyph->object)
6384 && XSTRING (glyph->object)->intervals
6385 && glyph->charpos >= 0
6386 && glyph->charpos < XSTRING (glyph->object)->size)
6387 {
6388 /* If we're on a string with `help-echo' text property,
6389 arrange for the help to be displayed. This is done by
6390 setting the global variable help_echo to the help string. */
6391 help = Fget_text_property (make_number (glyph->charpos),
6392 Qhelp_echo, glyph->object);
6393 if (!NILP (help))
6394 {
6395 help_echo = help;
6396 XSETWINDOW (help_echo_window, w);
6397 help_echo_object = glyph->object;
6398 help_echo_pos = glyph->charpos;
6399 }
6400
6401 /* Change the mouse pointer according to what is under X/Y. */
6402 map = Fget_text_property (make_number (glyph->charpos),
6403 Qlocal_map, glyph->object);
6404 if (!NILP (Fkeymapp (map)))
6405 cursor = f->output_data.x->nontext_cursor;
6406 else
6407 {
6408 map = Fget_text_property (make_number (glyph->charpos),
6409 Qkeymap, glyph->object);
6410 if (!NILP (Fkeymapp (map)))
6411 cursor = f->output_data.x->nontext_cursor;
6412 }
6413 }
6414 }
6415
6416 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6417}
6418
6419
6420/* Take proper action when the mouse has moved to position X, Y on
6421 frame F as regards highlighting characters that have mouse-face
6422 properties. Also de-highlighting chars where the mouse was before.
6423 X and Y can be negative or out of range. */
6424
6425static void
6426note_mouse_highlight (f, x, y)
6427 struct frame *f;
6428 int x, y;
6429{
6430 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6431 int portion;
6432 Lisp_Object window;
6433 struct window *w;
6434
6435 /* When a menu is active, don't highlight because this looks odd. */
6436#ifdef USE_X_TOOLKIT
6437 if (popup_activated ())
6438 return;
6439#endif
6440
6441 if (disable_mouse_highlight
6442 || !f->glyphs_initialized_p)
6443 return;
6444
6445 dpyinfo->mouse_face_mouse_x = x;
6446 dpyinfo->mouse_face_mouse_y = y;
6447 dpyinfo->mouse_face_mouse_frame = f;
6448
6449 if (dpyinfo->mouse_face_defer)
6450 return;
6451
6452 if (gc_in_progress)
6453 {
6454 dpyinfo->mouse_face_deferred_gc = 1;
6455 return;
6456 }
6457
6458 /* Which window is that in? */
6459 window = window_from_coordinates (f, x, y, &portion, 1);
6460
6461 /* If we were displaying active text in another window, clear that. */
6462 if (! EQ (window, dpyinfo->mouse_face_window))
6463 clear_mouse_face (dpyinfo);
6464
6465 /* Not on a window -> return. */
6466 if (!WINDOWP (window))
6467 return;
6468
6469 /* Convert to window-relative pixel coordinates. */
6470 w = XWINDOW (window);
6471 frame_to_window_pixel_xy (w, &x, &y);
6472
6473 /* Handle tool-bar window differently since it doesn't display a
6474 buffer. */
6475 if (EQ (window, f->tool_bar_window))
6476 {
6477 note_tool_bar_highlight (f, x, y);
6478 return;
6479 }
6480
6481 if (portion == 1 || portion == 3)
6482 {
6483 /* Mouse is on the mode or top line. */
6484 note_mode_line_highlight (w, x, portion == 1);
6485 return;
6486 }
6487 else
6488 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6489 f->output_data.x->text_cursor);
6490
6491 /* Are we in a window whose display is up to date?
6492 And verify the buffer's text has not changed. */
6493 if (/* Within text portion of the window. */
6494 portion == 0
6495 && EQ (w->window_end_valid, w->buffer)
6496 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6497 && (XFASTINT (w->last_overlay_modified)
6498 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6499 {
6500 int hpos, vpos, pos, i, area;
6501 struct glyph *glyph;
6502
6503 /* Find the glyph under X/Y. */
6504 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6505
6506 /* Clear mouse face if X/Y not over text. */
6507 if (glyph == NULL
6508 || area != TEXT_AREA
6509 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6510 {
6511 clear_mouse_face (dpyinfo);
6512 return;
6513 }
6514
6515 pos = glyph->charpos;
6516 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6517
6518 /* Check for mouse-face and help-echo. */
6519 {
6520 Lisp_Object mouse_face, overlay, position;
6521 Lisp_Object *overlay_vec;
6522 int len, noverlays;
6523 struct buffer *obuf;
6524 int obegv, ozv;
6525
6526 /* If we get an out-of-range value, return now; avoid an error. */
6527 if (pos > BUF_Z (XBUFFER (w->buffer)))
6528 return;
6529
6530 /* Make the window's buffer temporarily current for
6531 overlays_at and compute_char_face. */
6532 obuf = current_buffer;
6533 current_buffer = XBUFFER (w->buffer);
6534 obegv = BEGV;
6535 ozv = ZV;
6536 BEGV = BEG;
6537 ZV = Z;
6538
6539 /* Is this char mouse-active or does it have help-echo? */
6540 XSETINT (position, pos);
6541
6542 /* Put all the overlays we want in a vector in overlay_vec.
6543 Store the length in len. If there are more than 10, make
6544 enough space for all, and try again. */
6545 len = 10;
6546 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6547 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6548 if (noverlays > len)
6549 {
6550 len = noverlays;
6551 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6552 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6553 }
6554
6555 /* Sort overlays into increasing priority order. */
6556 noverlays = sort_overlays (overlay_vec, noverlays, w);
6557
6558 /* Check mouse-face highlighting. */
6559 if (! (EQ (window, dpyinfo->mouse_face_window)
6560 && vpos >= dpyinfo->mouse_face_beg_row
6561 && vpos <= dpyinfo->mouse_face_end_row
6562 && (vpos > dpyinfo->mouse_face_beg_row
6563 || hpos >= dpyinfo->mouse_face_beg_col)
6564 && (vpos < dpyinfo->mouse_face_end_row
6565 || hpos < dpyinfo->mouse_face_end_col
6566 || dpyinfo->mouse_face_past_end)))
6567 {
6568 /* Clear the display of the old active region, if any. */
6569 clear_mouse_face (dpyinfo);
6570
6571 /* Find the highest priority overlay that has a mouse-face prop. */
6572 overlay = Qnil;
6573 for (i = noverlays - 1; i >= 0; --i)
6574 {
6575 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6576 if (!NILP (mouse_face))
6577 {
6578 overlay = overlay_vec[i];
6579 break;
6580 }
6581 }
6582
6583 /* If no overlay applies, get a text property. */
6584 if (NILP (overlay))
6585 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6586
6587 /* Handle the overlay case. */
6588 if (! NILP (overlay))
6589 {
6590 /* Find the range of text around this char that
6591 should be active. */
6592 Lisp_Object before, after;
6593 int ignore;
6594
6595 before = Foverlay_start (overlay);
6596 after = Foverlay_end (overlay);
6597 /* Record this as the current active region. */
6598 fast_find_position (w, XFASTINT (before),
6599 &dpyinfo->mouse_face_beg_col,
6600 &dpyinfo->mouse_face_beg_row,
6601 &dpyinfo->mouse_face_beg_x,
6602 &dpyinfo->mouse_face_beg_y);
6603 dpyinfo->mouse_face_past_end
6604 = !fast_find_position (w, XFASTINT (after),
6605 &dpyinfo->mouse_face_end_col,
6606 &dpyinfo->mouse_face_end_row,
6607 &dpyinfo->mouse_face_end_x,
6608 &dpyinfo->mouse_face_end_y);
6609 dpyinfo->mouse_face_window = window;
6610 dpyinfo->mouse_face_face_id
6611 = face_at_buffer_position (w, pos, 0, 0,
6612 &ignore, pos + 1, 1);
6613
6614 /* Display it as active. */
6615 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6616 }
6617 /* Handle the text property case. */
6618 else if (! NILP (mouse_face))
6619 {
6620 /* Find the range of text around this char that
6621 should be active. */
6622 Lisp_Object before, after, beginning, end;
6623 int ignore;
6624
6625 beginning = Fmarker_position (w->start);
6626 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6627 - XFASTINT (w->window_end_pos)));
6628 before
6629 = Fprevious_single_property_change (make_number (pos + 1),
6630 Qmouse_face,
6631 w->buffer, beginning);
6632 after
6633 = Fnext_single_property_change (position, Qmouse_face,
6634 w->buffer, end);
6635 /* Record this as the current active region. */
6636 fast_find_position (w, XFASTINT (before),
6637 &dpyinfo->mouse_face_beg_col,
6638 &dpyinfo->mouse_face_beg_row,
6639 &dpyinfo->mouse_face_beg_x,
6640 &dpyinfo->mouse_face_beg_y);
6641 dpyinfo->mouse_face_past_end
6642 = !fast_find_position (w, XFASTINT (after),
6643 &dpyinfo->mouse_face_end_col,
6644 &dpyinfo->mouse_face_end_row,
6645 &dpyinfo->mouse_face_end_x,
6646 &dpyinfo->mouse_face_end_y);
6647 dpyinfo->mouse_face_window = window;
6648 dpyinfo->mouse_face_face_id
6649 = face_at_buffer_position (w, pos, 0, 0,
6650 &ignore, pos + 1, 1);
6651
6652 /* Display it as active. */
6653 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6654 }
6655 }
6656
6657 /* Look for a `help-echo' property. */
6658 {
6659 Lisp_Object help, overlay;
6660
6661 /* Check overlays first. */
6662 help = overlay = Qnil;
6663 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6664 {
6665 overlay = overlay_vec[i];
6666 help = Foverlay_get (overlay, Qhelp_echo);
6667 }
6668
6669 if (!NILP (help))
6670 {
6671 help_echo = help;
6672 help_echo_window = window;
6673 help_echo_object = overlay;
6674 help_echo_pos = pos;
6675 }
6676 else
6677 {
6678 /* Try text properties. */
6679 if ((STRINGP (glyph->object)
6680 && glyph->charpos >= 0
6681 && glyph->charpos < XSTRING (glyph->object)->size)
6682 || (BUFFERP (glyph->object)
6683 && glyph->charpos >= BEGV
6684 && glyph->charpos < ZV))
6685 help = Fget_text_property (make_number (glyph->charpos),
6686 Qhelp_echo, glyph->object);
6687
6688 if (!NILP (help))
6689 {
6690 help_echo = help;
6691 help_echo_window = window;
6692 help_echo_object = glyph->object;
6693 help_echo_pos = glyph->charpos;
6694 }
6695 }
6696 }
6697
6698 BEGV = obegv;
6699 ZV = ozv;
6700 current_buffer = obuf;
6701 }
6702 }
6703}
6704
6705static void
6706redo_mouse_highlight ()
6707{
6708 if (!NILP (last_mouse_motion_frame)
6709 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6710 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6711 last_mouse_motion_event.x,
6712 last_mouse_motion_event.y);
6713}
6714
6715
6716\f
6717/***********************************************************************
6718 Tool-bars
6719 ***********************************************************************/
6720
6721static int x_tool_bar_item P_ ((struct frame *, int, int,
6722 struct glyph **, int *, int *, int *));
6723
6724/* Tool-bar item index of the item on which a mouse button was pressed
6725 or -1. */
6726
6727static int last_tool_bar_item;
6728
6729
6730/* Get information about the tool-bar item at position X/Y on frame F.
6731 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6732 the current matrix of the tool-bar window of F, or NULL if not
6733 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6734 item in F->current_tool_bar_items. Value is
6735
6736 -1 if X/Y is not on a tool-bar item
6737 0 if X/Y is on the same item that was highlighted before.
6738 1 otherwise. */
6739
6740static int
6741x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6742 struct frame *f;
6743 int x, y;
6744 struct glyph **glyph;
6745 int *hpos, *vpos, *prop_idx;
6746{
6747 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6748 struct window *w = XWINDOW (f->tool_bar_window);
6749 int area;
6750
6751 /* Find the glyph under X/Y. */
6752 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6753 if (*glyph == NULL)
6754 return -1;
6755
6756 /* Get the start of this tool-bar item's properties in
6757 f->current_tool_bar_items. */
6758 if (!tool_bar_item_info (f, *glyph, prop_idx))
6759 return -1;
6760
6761 /* Is mouse on the highlighted item? */
6762 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6763 && *vpos >= dpyinfo->mouse_face_beg_row
6764 && *vpos <= dpyinfo->mouse_face_end_row
6765 && (*vpos > dpyinfo->mouse_face_beg_row
6766 || *hpos >= dpyinfo->mouse_face_beg_col)
6767 && (*vpos < dpyinfo->mouse_face_end_row
6768 || *hpos < dpyinfo->mouse_face_end_col
6769 || dpyinfo->mouse_face_past_end))
6770 return 0;
6771
6772 return 1;
6773}
6774
6775
6776/* Handle mouse button event on the tool-bar of frame F, at
6777 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6778 or ButtonRelase. */
6779
6780static void
6781x_handle_tool_bar_click (f, button_event)
6782 struct frame *f;
6783 XButtonEvent *button_event;
6784{
6785 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6786 struct window *w = XWINDOW (f->tool_bar_window);
6787 int hpos, vpos, prop_idx;
6788 struct glyph *glyph;
6789 Lisp_Object enabled_p;
6790 int x = button_event->x;
6791 int y = button_event->y;
6792
6793 /* If not on the highlighted tool-bar item, return. */
6794 frame_to_window_pixel_xy (w, &x, &y);
6795 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6796 return;
6797
6798 /* If item is disabled, do nothing. */
6799 enabled_p = (XVECTOR (f->current_tool_bar_items)
6800 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6801 if (NILP (enabled_p))
6802 return;
6803
6804 if (button_event->type == ButtonPress)
6805 {
6806 /* Show item in pressed state. */
6807 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6808 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6809 last_tool_bar_item = prop_idx;
6810 }
6811 else
6812 {
6813 Lisp_Object key, frame;
6814 struct input_event event;
6815
6816 /* Show item in released state. */
6817 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6818 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6819
6820 key = (XVECTOR (f->current_tool_bar_items)
6821 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6822
6823 XSETFRAME (frame, f);
6824 event.kind = TOOL_BAR_EVENT;
6825 event.frame_or_window = frame;
6826 event.arg = frame;
6827 kbd_buffer_store_event (&event);
6828
6829 event.kind = TOOL_BAR_EVENT;
6830 event.frame_or_window = frame;
6831 event.arg = key;
6832 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6833 button_event->state);
6834 kbd_buffer_store_event (&event);
6835 last_tool_bar_item = -1;
6836 }
6837}
6838
6839
6840/* Possibly highlight a tool-bar item on frame F when mouse moves to
6841 tool-bar window-relative coordinates X/Y. Called from
6842 note_mouse_highlight. */
6843
6844static void
6845note_tool_bar_highlight (f, x, y)
6846 struct frame *f;
6847 int x, y;
6848{
6849 Lisp_Object window = f->tool_bar_window;
6850 struct window *w = XWINDOW (window);
6851 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6852 int hpos, vpos;
6853 struct glyph *glyph;
6854 struct glyph_row *row;
6855 int i;
6856 Lisp_Object enabled_p;
6857 int prop_idx;
6858 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6859 int mouse_down_p, rc;
6860
6861 /* Function note_mouse_highlight is called with negative x(y
6862 values when mouse moves outside of the frame. */
6863 if (x <= 0 || y <= 0)
6864 {
6865 clear_mouse_face (dpyinfo);
6866 return;
6867 }
6868
6869 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6870 if (rc < 0)
6871 {
6872 /* Not on tool-bar item. */
6873 clear_mouse_face (dpyinfo);
6874 return;
6875 }
6876 else if (rc == 0)
6877 /* On same tool-bar item as before. */
6878 goto set_help_echo;
6879
6880 clear_mouse_face (dpyinfo);
6881
6882 /* Mouse is down, but on different tool-bar item? */
6883 mouse_down_p = (dpyinfo->grabbed
6884 && f == last_mouse_frame
6885 && FRAME_LIVE_P (f));
6886 if (mouse_down_p
6887 && last_tool_bar_item != prop_idx)
6888 return;
6889
6890 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6891 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6892
6893 /* If tool-bar item is not enabled, don't highlight it. */
6894 enabled_p = (XVECTOR (f->current_tool_bar_items)
6895 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6896 if (!NILP (enabled_p))
6897 {
6898 /* Compute the x-position of the glyph. In front and past the
6899 image is a space. We include this is the highlighted area. */
6900 row = MATRIX_ROW (w->current_matrix, vpos);
6901 for (i = x = 0; i < hpos; ++i)
6902 x += row->glyphs[TEXT_AREA][i].pixel_width;
6903
6904 /* Record this as the current active region. */
6905 dpyinfo->mouse_face_beg_col = hpos;
6906 dpyinfo->mouse_face_beg_row = vpos;
6907 dpyinfo->mouse_face_beg_x = x;
6908 dpyinfo->mouse_face_beg_y = row->y;
6909 dpyinfo->mouse_face_past_end = 0;
6910
6911 dpyinfo->mouse_face_end_col = hpos + 1;
6912 dpyinfo->mouse_face_end_row = vpos;
6913 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6914 dpyinfo->mouse_face_end_y = row->y;
6915 dpyinfo->mouse_face_window = window;
6916 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6917
6918 /* Display it as active. */
6919 show_mouse_face (dpyinfo, draw);
6920 dpyinfo->mouse_face_image_state = draw;
6921 }
6922
6923 set_help_echo:
6924
6925 /* Set help_echo to a help string.to display for this tool-bar item.
6926 XTread_socket does the rest. */
6927 help_echo_object = help_echo_window = Qnil;
6928 help_echo_pos = -1;
6929 help_echo = (XVECTOR (f->current_tool_bar_items)
6930 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6931 if (NILP (help_echo))
6932 help_echo = (XVECTOR (f->current_tool_bar_items)
6933 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6934}
6935
6936
6937\f
6938/* Find the glyph matrix position of buffer position POS in window W.
6939 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6940 current glyphs must be up to date. If POS is above window start
6941 return (0, 0, 0, 0). If POS is after end of W, return end of
6942 last line in W. */
6943
6944static int
6945fast_find_position (w, pos, hpos, vpos, x, y)
6946 struct window *w;
6947 int pos;
6948 int *hpos, *vpos, *x, *y;
6949{
6950 int i;
6951 int lastcol;
6952 int maybe_next_line_p = 0;
6953 int line_start_position;
6954 int yb = window_text_bottom_y (w);
6955 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6956 struct glyph_row *best_row = row;
6957 int row_vpos = 0, best_row_vpos = 0;
6958 int current_x;
6959
6960 while (row->y < yb)
6961 {
6962 if (row->used[TEXT_AREA])
6963 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6964 else
6965 line_start_position = 0;
6966
6967 if (line_start_position > pos)
6968 break;
6969 /* If the position sought is the end of the buffer,
6970 don't include the blank lines at the bottom of the window. */
6971 else if (line_start_position == pos
6972 && pos == BUF_ZV (XBUFFER (w->buffer)))
6973 {
6974 maybe_next_line_p = 1;
6975 break;
6976 }
6977 else if (line_start_position > 0)
6978 {
6979 best_row = row;
6980 best_row_vpos = row_vpos;
6981 }
6982
6983 if (row->y + row->height >= yb)
6984 break;
6985
6986 ++row;
6987 ++row_vpos;
6988 }
6989
6990 /* Find the right column within BEST_ROW. */
6991 lastcol = 0;
6992 current_x = best_row->x;
6993 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6994 {
6995 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6996 int charpos;
6997
6998 charpos = glyph->charpos;
6999 if (charpos == pos)
7000 {
7001 *hpos = i;
7002 *vpos = best_row_vpos;
7003 *x = current_x;
7004 *y = best_row->y;
7005 return 1;
7006 }
7007 else if (charpos > pos)
7008 break;
7009 else if (charpos > 0)
7010 lastcol = i;
7011
7012 current_x += glyph->pixel_width;
7013 }
7014
7015 /* If we're looking for the end of the buffer,
7016 and we didn't find it in the line we scanned,
7017 use the start of the following line. */
7018 if (maybe_next_line_p)
7019 {
7020 ++best_row;
7021 ++best_row_vpos;
7022 lastcol = 0;
7023 current_x = best_row->x;
7024 }
7025
7026 *vpos = best_row_vpos;
7027 *hpos = lastcol + 1;
7028 *x = current_x;
7029 *y = best_row->y;
7030 return 0;
7031}
7032
7033
7034/* Display the active region described by mouse_face_*
7035 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7036
7037static void
7038show_mouse_face (dpyinfo, draw)
7039 struct x_display_info *dpyinfo;
7040 enum draw_glyphs_face draw;
7041{
7042 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7043 struct frame *f = XFRAME (WINDOW_FRAME (w));
7044 int i;
7045 int cursor_off_p = 0;
7046 struct cursor_pos saved_cursor;
7047
7048 saved_cursor = output_cursor;
7049
7050 /* If window is in the process of being destroyed, don't bother
7051 to do anything. */
7052 if (w->current_matrix == NULL)
7053 goto set_x_cursor;
7054
7055 /* Recognize when we are called to operate on rows that don't exist
7056 anymore. This can happen when a window is split. */
7057 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7058 goto set_x_cursor;
7059
7060 set_output_cursor (&w->phys_cursor);
7061
7062 /* Note that mouse_face_beg_row etc. are window relative. */
7063 for (i = dpyinfo->mouse_face_beg_row;
7064 i <= dpyinfo->mouse_face_end_row;
7065 i++)
7066 {
7067 int start_hpos, end_hpos, start_x;
7068 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7069
7070 /* Don't do anything if row doesn't have valid contents. */
7071 if (!row->enabled_p)
7072 continue;
7073
7074 /* For all but the first row, the highlight starts at column 0. */
7075 if (i == dpyinfo->mouse_face_beg_row)
7076 {
7077 start_hpos = dpyinfo->mouse_face_beg_col;
7078 start_x = dpyinfo->mouse_face_beg_x;
7079 }
7080 else
7081 {
7082 start_hpos = 0;
7083 start_x = 0;
7084 }
7085
7086 if (i == dpyinfo->mouse_face_end_row)
7087 end_hpos = dpyinfo->mouse_face_end_col;
7088 else
7089 end_hpos = row->used[TEXT_AREA];
7090
7091 /* If the cursor's in the text we are about to rewrite, turn the
7092 cursor off. */
7093 if (!w->pseudo_window_p
7094 && i == output_cursor.vpos
7095 && output_cursor.hpos >= start_hpos - 1
7096 && output_cursor.hpos <= end_hpos)
7097 {
7098 x_update_window_cursor (w, 0);
7099 cursor_off_p = 1;
7100 }
7101
7102 if (end_hpos > start_hpos)
7103 {
7104 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7105 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7106 start_hpos, end_hpos, draw, NULL, NULL, 0);
7107 }
7108 }
7109
7110 /* If we turned the cursor off, turn it back on. */
7111 if (cursor_off_p)
7112 x_display_cursor (w, 1,
7113 output_cursor.hpos, output_cursor.vpos,
7114 output_cursor.x, output_cursor.y);
7115
7116 output_cursor = saved_cursor;
7117
7118 set_x_cursor:
7119
7120 /* Change the mouse cursor. */
7121 if (draw == DRAW_NORMAL_TEXT)
7122 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7123 f->output_data.x->text_cursor);
7124 else if (draw == DRAW_MOUSE_FACE)
7125 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7126 f->output_data.x->cross_cursor);
7127 else
7128 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7129 f->output_data.x->nontext_cursor);
7130}
7131
7132/* Clear out the mouse-highlighted active region.
7133 Redraw it un-highlighted first. */
7134
7135void
7136clear_mouse_face (dpyinfo)
7137 struct x_display_info *dpyinfo;
7138{
7139 if (tip_frame)
7140 return;
7141
7142 if (! NILP (dpyinfo->mouse_face_window))
7143 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7144
7145 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7146 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7147 dpyinfo->mouse_face_window = Qnil;
7148}
7149
7150
7151/* Clear any mouse-face on window W. This function is part of the
7152 redisplay interface, and is called from try_window_id and similar
7153 functions to ensure the mouse-highlight is off. */
7154
7155static void
7156x_clear_mouse_face (w)
7157 struct window *w;
7158{
7159 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7160 Lisp_Object window;
7161
7162 BLOCK_INPUT;
7163 XSETWINDOW (window, w);
7164 if (EQ (window, dpyinfo->mouse_face_window))
7165 clear_mouse_face (dpyinfo);
7166 UNBLOCK_INPUT;
7167}
7168
7169
7170/* Just discard the mouse face information for frame F, if any.
7171 This is used when the size of F is changed. */
7172
7173void
7174cancel_mouse_face (f)
7175 FRAME_PTR f;
7176{
7177 Lisp_Object window;
7178 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7179
7180 window = dpyinfo->mouse_face_window;
7181 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7182 {
7183 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7184 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7185 dpyinfo->mouse_face_window = Qnil;
7186 }
7187}
7188\f
7189static struct scroll_bar *x_window_to_scroll_bar ();
7190static void x_scroll_bar_report_motion ();
7191
7192/* Return the current position of the mouse.
7193 *fp should be a frame which indicates which display to ask about.
7194
7195 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7196 and *part to the frame, window, and scroll bar part that the mouse
7197 is over. Set *x and *y to the portion and whole of the mouse's
7198 position on the scroll bar.
7199
7200 If the mouse movement started elsewhere, set *fp to the frame the
7201 mouse is on, *bar_window to nil, and *x and *y to the character cell
7202 the mouse is over.
7203
7204 Set *time to the server time-stamp for the time at which the mouse
7205 was at this position.
7206
7207 Don't store anything if we don't have a valid set of values to report.
7208
7209 This clears the mouse_moved flag, so we can wait for the next mouse
7210 movement. */
7211
7212static void
7213XTmouse_position (fp, insist, bar_window, part, x, y, time)
7214 FRAME_PTR *fp;
7215 int insist;
7216 Lisp_Object *bar_window;
7217 enum scroll_bar_part *part;
7218 Lisp_Object *x, *y;
7219 unsigned long *time;
7220{
7221 FRAME_PTR f1;
7222
7223 BLOCK_INPUT;
7224
7225 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7226 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7227 else
7228 {
7229 Window root;
7230 int root_x, root_y;
7231
7232 Window dummy_window;
7233 int dummy;
7234
7235 Lisp_Object frame, tail;
7236
7237 /* Clear the mouse-moved flag for every frame on this display. */
7238 FOR_EACH_FRAME (tail, frame)
7239 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7240 XFRAME (frame)->mouse_moved = 0;
7241
7242 last_mouse_scroll_bar = Qnil;
7243
7244 /* Figure out which root window we're on. */
7245 XQueryPointer (FRAME_X_DISPLAY (*fp),
7246 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7247
7248 /* The root window which contains the pointer. */
7249 &root,
7250
7251 /* Trash which we can't trust if the pointer is on
7252 a different screen. */
7253 &dummy_window,
7254
7255 /* The position on that root window. */
7256 &root_x, &root_y,
7257
7258 /* More trash we can't trust. */
7259 &dummy, &dummy,
7260
7261 /* Modifier keys and pointer buttons, about which
7262 we don't care. */
7263 (unsigned int *) &dummy);
7264
7265 /* Now we have a position on the root; find the innermost window
7266 containing the pointer. */
7267 {
7268 Window win, child;
7269 int win_x, win_y;
7270 int parent_x = 0, parent_y = 0;
7271 int count;
7272
7273 win = root;
7274
7275 /* XTranslateCoordinates can get errors if the window
7276 structure is changing at the same time this function
7277 is running. So at least we must not crash from them. */
7278
7279 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7280
7281 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7282 && FRAME_LIVE_P (last_mouse_frame))
7283 {
7284 /* If mouse was grabbed on a frame, give coords for that frame
7285 even if the mouse is now outside it. */
7286 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7287
7288 /* From-window, to-window. */
7289 root, FRAME_X_WINDOW (last_mouse_frame),
7290
7291 /* From-position, to-position. */
7292 root_x, root_y, &win_x, &win_y,
7293
7294 /* Child of win. */
7295 &child);
7296 f1 = last_mouse_frame;
7297 }
7298 else
7299 {
7300 while (1)
7301 {
7302 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7303
7304 /* From-window, to-window. */
7305 root, win,
7306
7307 /* From-position, to-position. */
7308 root_x, root_y, &win_x, &win_y,
7309
7310 /* Child of win. */
7311 &child);
7312
7313 if (child == None || child == win)
7314 break;
7315
7316 win = child;
7317 parent_x = win_x;
7318 parent_y = win_y;
7319 }
7320
7321 /* Now we know that:
7322 win is the innermost window containing the pointer
7323 (XTC says it has no child containing the pointer),
7324 win_x and win_y are the pointer's position in it
7325 (XTC did this the last time through), and
7326 parent_x and parent_y are the pointer's position in win's parent.
7327 (They are what win_x and win_y were when win was child.
7328 If win is the root window, it has no parent, and
7329 parent_{x,y} are invalid, but that's okay, because we'll
7330 never use them in that case.) */
7331
7332 /* Is win one of our frames? */
7333 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7334 }
7335
7336 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7337 f1 = 0;
7338
7339 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7340
7341 /* If not, is it one of our scroll bars? */
7342 if (! f1)
7343 {
7344 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7345
7346 if (bar)
7347 {
7348 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7349 win_x = parent_x;
7350 win_y = parent_y;
7351 }
7352 }
7353
7354 if (f1 == 0 && insist > 0)
7355 f1 = SELECTED_FRAME ();
7356
7357 if (f1)
7358 {
7359 /* Ok, we found a frame. Store all the values.
7360 last_mouse_glyph is a rectangle used to reduce the
7361 generation of mouse events. To not miss any motion
7362 events, we must divide the frame into rectangles of the
7363 size of the smallest character that could be displayed
7364 on it, i.e. into the same rectangles that matrices on
7365 the frame are divided into. */
7366
7367#if OLD_REDISPLAY_CODE
7368 int ignore1, ignore2;
7369 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7370 &last_mouse_glyph,
7371 FRAME_X_DISPLAY_INFO (f1)->grabbed
7372 || insist);
7373#else
7374 {
7375 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7376 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7377 int x = win_x;
7378 int y = win_y;
7379
7380 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7381 round down even for negative values. */
7382 if (x < 0)
7383 x -= width - 1;
7384 if (y < 0)
7385 y -= height - 1;
7386
7387 last_mouse_glyph.width = width;
7388 last_mouse_glyph.height = height;
7389 last_mouse_glyph.x = (x + width - 1) / width * width;
7390 last_mouse_glyph.y = (y + height - 1) / height * height;
7391 }
7392#endif
7393
7394 *bar_window = Qnil;
7395 *part = 0;
7396 *fp = f1;
7397 XSETINT (*x, win_x);
7398 XSETINT (*y, win_y);
7399 *time = last_mouse_movement_time;
7400 }
7401 }
7402 }
7403
7404 UNBLOCK_INPUT;
7405}
7406
7407
7408#ifdef USE_X_TOOLKIT
7409
7410/* Atimer callback function for TIMER. Called every 0.1s to process
7411 Xt timeouts, if needed. We must avoid calling XtAppPending as
7412 much as possible because that function does an implicit XFlush
7413 that slows us down. */
7414
7415static void
7416x_process_timeouts (timer)
7417 struct atimer *timer;
7418{
7419 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7420 {
7421 BLOCK_INPUT;
7422 while (XtAppPending (Xt_app_con) & XtIMTimer)
7423 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7424 UNBLOCK_INPUT;
7425 }
7426}
7427
7428#endif /* USE_X_TOOLKIT */
7429
7430\f
7431/* Scroll bar support. */
7432
7433/* Given an X window ID, find the struct scroll_bar which manages it.
7434 This can be called in GC, so we have to make sure to strip off mark
7435 bits. */
7436
7437static struct scroll_bar *
7438x_window_to_scroll_bar (window_id)
7439 Window window_id;
7440{
7441 Lisp_Object tail;
7442
7443 for (tail = Vframe_list;
7444 XGCTYPE (tail) == Lisp_Cons;
7445 tail = XCDR (tail))
7446 {
7447 Lisp_Object frame, bar, condemned;
7448
7449 frame = XCAR (tail);
7450 /* All elements of Vframe_list should be frames. */
7451 if (! GC_FRAMEP (frame))
7452 abort ();
7453
7454 /* Scan this frame's scroll bar list for a scroll bar with the
7455 right window ID. */
7456 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7457 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7458 /* This trick allows us to search both the ordinary and
7459 condemned scroll bar lists with one loop. */
7460 ! GC_NILP (bar) || (bar = condemned,
7461 condemned = Qnil,
7462 ! GC_NILP (bar));
7463 bar = XSCROLL_BAR (bar)->next)
7464 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7465 return XSCROLL_BAR (bar);
7466 }
7467
7468 return 0;
7469}
7470
7471
7472\f
7473/************************************************************************
7474 Toolkit scroll bars
7475 ************************************************************************/
7476
7477#if USE_TOOLKIT_SCROLL_BARS
7478
7479static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7480static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7481static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7482 struct scroll_bar *));
7483static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7484 int, int, int));
7485
7486
7487/* Id of action hook installed for scroll bars. */
7488
7489static XtActionHookId action_hook_id;
7490
7491/* Lisp window being scrolled. Set when starting to interact with
7492 a toolkit scroll bar, reset to nil when ending the interaction. */
7493
7494static Lisp_Object window_being_scrolled;
7495
7496/* Last scroll bar part sent in xm_scroll_callback. */
7497
7498static int last_scroll_bar_part;
7499
7500/* Whether this is an Xaw with arrow-scrollbars. This should imply
7501 that movements of 1/20 of the screen size are mapped to up/down. */
7502
7503static Boolean xaw3d_arrow_scroll;
7504
7505/* Whether the drag scrolling maintains the mouse at the top of the
7506 thumb. If not, resizing the thumb needs to be done more carefully
7507 to avoid jerkyness. */
7508
7509static Boolean xaw3d_pick_top;
7510
7511
7512/* Action hook installed via XtAppAddActionHook when toolkit scroll
7513 bars are used.. The hook is responsible for detecting when
7514 the user ends an interaction with the scroll bar, and generates
7515 a `end-scroll' scroll_bar_click' event if so. */
7516
7517static void
7518xt_action_hook (widget, client_data, action_name, event, params,
7519 num_params)
7520 Widget widget;
7521 XtPointer client_data;
7522 String action_name;
7523 XEvent *event;
7524 String *params;
7525 Cardinal *num_params;
7526{
7527 int scroll_bar_p;
7528 char *end_action;
7529
7530#ifdef USE_MOTIF
7531 scroll_bar_p = XmIsScrollBar (widget);
7532 end_action = "Release";
7533#else /* !USE_MOTIF i.e. use Xaw */
7534 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7535 end_action = "EndScroll";
7536#endif /* USE_MOTIF */
7537
7538 if (scroll_bar_p
7539 && strcmp (action_name, end_action) == 0
7540 && WINDOWP (window_being_scrolled))
7541 {
7542 struct window *w;
7543
7544 x_send_scroll_bar_event (window_being_scrolled,
7545 scroll_bar_end_scroll, 0, 0);
7546 w = XWINDOW (window_being_scrolled);
7547 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7548 window_being_scrolled = Qnil;
7549 last_scroll_bar_part = -1;
7550
7551 /* Xt timeouts no longer needed. */
7552 toolkit_scroll_bar_interaction = 0;
7553 }
7554}
7555
7556
7557/* Send a client message with message type Xatom_Scrollbar for a
7558 scroll action to the frame of WINDOW. PART is a value identifying
7559 the part of the scroll bar that was clicked on. PORTION is the
7560 amount to scroll of a whole of WHOLE. */
7561
7562static void
7563x_send_scroll_bar_event (window, part, portion, whole)
7564 Lisp_Object window;
7565 int part, portion, whole;
7566{
7567 XEvent event;
7568 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7569 struct frame *f = XFRAME (XWINDOW (window)->frame);
7570
7571 /* Construct a ClientMessage event to send to the frame. */
7572 ev->type = ClientMessage;
7573 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7574 ev->display = FRAME_X_DISPLAY (f);
7575 ev->window = FRAME_X_WINDOW (f);
7576 ev->format = 32;
7577 ev->data.l[0] = (long) XFASTINT (window);
7578 ev->data.l[1] = (long) part;
7579 ev->data.l[2] = (long) 0;
7580 ev->data.l[3] = (long) portion;
7581 ev->data.l[4] = (long) whole;
7582
7583 /* Make Xt timeouts work while the scroll bar is active. */
7584 toolkit_scroll_bar_interaction = 1;
7585
7586 /* Setting the event mask to zero means that the message will
7587 be sent to the client that created the window, and if that
7588 window no longer exists, no event will be sent. */
7589 BLOCK_INPUT;
7590 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7591 UNBLOCK_INPUT;
7592}
7593
7594
7595/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7596 in *IEVENT. */
7597
7598static void
7599x_scroll_bar_to_input_event (event, ievent)
7600 XEvent *event;
7601 struct input_event *ievent;
7602{
7603 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7604 Lisp_Object window;
7605 struct frame *f;
7606
7607 XSETFASTINT (window, ev->data.l[0]);
7608 f = XFRAME (XWINDOW (window)->frame);
7609
7610 ievent->kind = scroll_bar_click;
7611 ievent->frame_or_window = window;
7612 ievent->arg = Qnil;
7613 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7614 ievent->part = ev->data.l[1];
7615 ievent->code = ev->data.l[2];
7616 ievent->x = make_number ((int) ev->data.l[3]);
7617 ievent->y = make_number ((int) ev->data.l[4]);
7618 ievent->modifiers = 0;
7619}
7620
7621
7622#ifdef USE_MOTIF
7623
7624/* Minimum and maximum values used for Motif scroll bars. */
7625
7626#define XM_SB_MIN 1
7627#define XM_SB_MAX 10000000
7628#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7629
7630
7631/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7632 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7633 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7634
7635static void
7636xm_scroll_callback (widget, client_data, call_data)
7637 Widget widget;
7638 XtPointer client_data, call_data;
7639{
7640 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7641 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7642 double percent;
7643 int part = -1, whole = 0, portion = 0;
7644
7645 switch (cs->reason)
7646 {
7647 case XmCR_DECREMENT:
7648 bar->dragging = Qnil;
7649 part = scroll_bar_up_arrow;
7650 break;
7651
7652 case XmCR_INCREMENT:
7653 bar->dragging = Qnil;
7654 part = scroll_bar_down_arrow;
7655 break;
7656
7657 case XmCR_PAGE_DECREMENT:
7658 bar->dragging = Qnil;
7659 part = scroll_bar_above_handle;
7660 break;
7661
7662 case XmCR_PAGE_INCREMENT:
7663 bar->dragging = Qnil;
7664 part = scroll_bar_below_handle;
7665 break;
7666
7667 case XmCR_TO_TOP:
7668 bar->dragging = Qnil;
7669 part = scroll_bar_to_top;
7670 break;
7671
7672 case XmCR_TO_BOTTOM:
7673 bar->dragging = Qnil;
7674 part = scroll_bar_to_bottom;
7675 break;
7676
7677 case XmCR_DRAG:
7678 {
7679 int slider_size;
7680 int dragging_down_p = (INTEGERP (bar->dragging)
7681 && XINT (bar->dragging) <= cs->value);
7682
7683 /* Get the slider size. */
7684 BLOCK_INPUT;
7685 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7686 UNBLOCK_INPUT;
7687
7688 /* At the max position of the scroll bar, do a line-wise
7689 movement. Without doing anything, the LessTif scroll bar
7690 calls us with the same cs->value again and again. If we
7691 want to make sure that we can reach the end of the buffer,
7692 we have to do something.
7693
7694 Implementation note: setting bar->dragging always to
7695 cs->value gives a smoother movement at the max position.
7696 Setting it to nil when doing line-wise movement gives
7697 a better slider behavior. */
7698
7699 if (cs->value + slider_size == XM_SB_MAX
7700 || (dragging_down_p
7701 && last_scroll_bar_part == scroll_bar_down_arrow))
7702 {
7703 part = scroll_bar_down_arrow;
7704 bar->dragging = Qnil;
7705 }
7706 else
7707 {
7708 whole = XM_SB_RANGE;
7709 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7710 part = scroll_bar_handle;
7711 bar->dragging = make_number (cs->value);
7712 }
7713 }
7714 break;
7715
7716 case XmCR_VALUE_CHANGED:
7717 break;
7718 };
7719
7720 if (part >= 0)
7721 {
7722 window_being_scrolled = bar->window;
7723 last_scroll_bar_part = part;
7724 x_send_scroll_bar_event (bar->window, part, portion, whole);
7725 }
7726}
7727
7728
7729#else /* !USE_MOTIF, i.e. Xaw. */
7730
7731
7732/* Xaw scroll bar callback. Invoked when the thumb is dragged.
7733 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7734 scroll bar struct. CALL_DATA is a pointer to a float saying where
7735 the thumb is. */
7736
7737static void
7738xaw_jump_callback (widget, client_data, call_data)
7739 Widget widget;
7740 XtPointer client_data, call_data;
7741{
7742 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7743 float top = *(float *) call_data;
7744 float shown;
7745 int whole, portion, height;
7746 int part;
7747
7748 /* Get the size of the thumb, a value between 0 and 1. */
7749 BLOCK_INPUT;
7750 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7751 UNBLOCK_INPUT;
7752
7753 whole = 10000000;
7754 portion = shown < 1 ? top * whole : 0;
7755
7756 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7757 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7758 the bottom, so we force the scrolling whenever we see that we're
7759 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7760 we try to ensure that we always stay two pixels away from the
7761 bottom). */
7762 part = scroll_bar_down_arrow;
7763 else
7764 part = scroll_bar_handle;
7765
7766 window_being_scrolled = bar->window;
7767 bar->dragging = make_number (portion);
7768 last_scroll_bar_part = part;
7769 x_send_scroll_bar_event (bar->window, part, portion, whole);
7770}
7771
7772
7773/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7774 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7775 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7776 the scroll bar. CALL_DATA is an integer specifying the action that
7777 has taken place. It's magnitude is in the range 0..height of the
7778 scroll bar. Negative values mean scroll towards buffer start.
7779 Values < height of scroll bar mean line-wise movement. */
7780
7781static void
7782xaw_scroll_callback (widget, client_data, call_data)
7783 Widget widget;
7784 XtPointer client_data, call_data;
7785{
7786 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7787 int position = (int) call_data;
7788 Dimension height;
7789 int part;
7790
7791 /* Get the height of the scroll bar. */
7792 BLOCK_INPUT;
7793 XtVaGetValues (widget, XtNheight, &height, NULL);
7794 UNBLOCK_INPUT;
7795
7796 if (abs (position) >= height)
7797 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7798
7799 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7800 it maps line-movement to call_data = max(5, height/20). */
7801 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7802 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7803 else
7804 part = scroll_bar_move_ratio;
7805
7806 window_being_scrolled = bar->window;
7807 bar->dragging = Qnil;
7808 last_scroll_bar_part = part;
7809 x_send_scroll_bar_event (bar->window, part, position, height);
7810}
7811
7812
7813#endif /* not USE_MOTIF */
7814
7815
7816/* Create the widget for scroll bar BAR on frame F. Record the widget
7817 and X window of the scroll bar in BAR. */
7818
7819static void
7820x_create_toolkit_scroll_bar (f, bar)
7821 struct frame *f;
7822 struct scroll_bar *bar;
7823{
7824 Window xwindow;
7825 Widget widget;
7826 Arg av[20];
7827 int ac = 0;
7828 char *scroll_bar_name = "verticalScrollBar";
7829 unsigned long pixel;
7830
7831 BLOCK_INPUT;
7832
7833#ifdef USE_MOTIF
7834 /* LessTif 0.85, problems:
7835
7836 1. When the mouse if over the scroll bar, the scroll bar will
7837 get keyboard events. I didn't find a way to turn this off.
7838
7839 2. Do we have to explicitly set the cursor to get an arrow
7840 cursor (see below)? */
7841
7842 /* Set resources. Create the widget. */
7843 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7844 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7845 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7846 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7847 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7848 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7849 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7850
7851 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7852 if (pixel != -1)
7853 {
7854 XtSetArg (av[ac], XmNforeground, pixel);
7855 ++ac;
7856 }
7857
7858 pixel = f->output_data.x->scroll_bar_background_pixel;
7859 if (pixel != -1)
7860 {
7861 XtSetArg (av[ac], XmNbackground, pixel);
7862 ++ac;
7863 }
7864
7865 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7866 scroll_bar_name, av, ac);
7867
7868 /* Add one callback for everything that can happen. */
7869 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7870 (XtPointer) bar);
7871 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7872 (XtPointer) bar);
7873 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7874 (XtPointer) bar);
7875 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7876 (XtPointer) bar);
7877 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7878 (XtPointer) bar);
7879 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7880 (XtPointer) bar);
7881 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7882 (XtPointer) bar);
7883
7884 /* Realize the widget. Only after that is the X window created. */
7885 XtRealizeWidget (widget);
7886
7887 /* Set the cursor to an arrow. I didn't find a resource to do that.
7888 And I'm wondering why it hasn't an arrow cursor by default. */
7889 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7890 f->output_data.x->nontext_cursor);
7891
7892#else /* !USE_MOTIF i.e. use Xaw */
7893
7894 /* Set resources. Create the widget. The background of the
7895 Xaw3d scroll bar widget is a little bit light for my taste.
7896 We don't alter it here to let users change it according
7897 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7898 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7899 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7900 /* For smoother scrolling with Xaw3d -sm */
7901 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7902 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7903
7904 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7905 if (pixel != -1)
7906 {
7907 XtSetArg (av[ac], XtNforeground, pixel);
7908 ++ac;
7909 }
7910
7911 pixel = f->output_data.x->scroll_bar_background_pixel;
7912 if (pixel != -1)
7913 {
7914 XtSetArg (av[ac], XtNbackground, pixel);
7915 ++ac;
7916 }
7917
7918 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7919 f->output_data.x->edit_widget, av, ac);
7920
7921 {
7922 char *initial = "";
7923 char *val = initial;
7924 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7925 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7926 if (val == initial)
7927 { /* ARROW_SCROLL */
7928 xaw3d_arrow_scroll = True;
7929 /* Isn't that just a personal preference ? -sm */
7930 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7931 }
7932 }
7933
7934 /* Define callbacks. */
7935 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7936 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7937 (XtPointer) bar);
7938
7939 /* Realize the widget. Only after that is the X window created. */
7940 XtRealizeWidget (widget);
7941
7942#endif /* !USE_MOTIF */
7943
7944 /* Install an action hook that let's us detect when the user
7945 finishes interacting with a scroll bar. */
7946 if (action_hook_id == 0)
7947 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7948
7949 /* Remember X window and widget in the scroll bar vector. */
7950 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7951 xwindow = XtWindow (widget);
7952 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7953
7954 UNBLOCK_INPUT;
7955}
7956
7957
7958/* Set the thumb size and position of scroll bar BAR. We are currently
7959 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7960
7961static void
7962x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7963 struct scroll_bar *bar;
7964 int portion, position, whole;
7965{
7966 float top, shown;
7967 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7968
7969 if (whole == 0)
7970 top = 0, shown = 1;
7971 else
7972 {
7973 top = (float) position / whole;
7974 shown = (float) portion / whole;
7975 }
7976
7977 BLOCK_INPUT;
7978
7979#ifdef USE_MOTIF
7980 {
7981 int size, value;
7982 Boolean arrow1_selected, arrow2_selected;
7983 unsigned char flags;
7984 XmScrollBarWidget sb;
7985
7986 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7987 is the scroll bar's maximum and MIN is the scroll bar's minimum
7988 value. */
7989 size = shown * XM_SB_RANGE;
7990 size = min (size, XM_SB_RANGE);
7991 size = max (size, 1);
7992
7993 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7994 value = top * XM_SB_RANGE;
7995 value = min (value, XM_SB_MAX - size);
7996 value = max (value, XM_SB_MIN);
7997
7998 /* LessTif: Calling XmScrollBarSetValues after an increment or
7999 decrement turns off auto-repeat LessTif-internally. This can
8000 be seen in ScrollBar.c which resets Arrow1Selected and
8001 Arrow2Selected. It also sets internal flags so that LessTif
8002 believes the mouse is in the slider. We either have to change
8003 our code, or work around that by accessing private data. */
8004
8005 sb = (XmScrollBarWidget) widget;
8006 arrow1_selected = sb->scrollBar.arrow1_selected;
8007 arrow2_selected = sb->scrollBar.arrow2_selected;
8008 flags = sb->scrollBar.flags;
8009
8010 if (NILP (bar->dragging))
8011 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8012 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8013 /* This has the negative side effect that the slider value is
8014 not what it would be if we scrolled here using line-wise or
8015 page-wise movement. */
8016 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8017 else
8018 {
8019 /* If currently dragging, only update the slider size.
8020 This reduces flicker effects. */
8021 int old_value, old_size, increment, page_increment;
8022
8023 XmScrollBarGetValues (widget, &old_value, &old_size,
8024 &increment, &page_increment);
8025 XmScrollBarSetValues (widget, old_value,
8026 min (size, XM_SB_RANGE - old_value),
8027 0, 0, False);
8028 }
8029
8030 sb->scrollBar.arrow1_selected = arrow1_selected;
8031 sb->scrollBar.arrow2_selected = arrow2_selected;
8032 sb->scrollBar.flags = flags;
8033 }
8034#else /* !USE_MOTIF i.e. use Xaw */
8035 {
8036 float old_top, old_shown;
8037 Dimension height;
8038 XtVaGetValues (widget,
8039 XtNtopOfThumb, &old_top,
8040 XtNshown, &old_shown,
8041 XtNheight, &height,
8042 NULL);
8043
8044 /* Massage the top+shown values. */
8045 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8046 top = max (0, min (1, top));
8047 else
8048 top = old_top;
8049 /* Keep two pixels available for moving the thumb down. */
8050 shown = max (0, min (1 - top - (2.0 / height), shown));
8051
8052 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8053 check that your system's configuration file contains a define
8054 for `NARROWPROTO'. See s/freebsd.h for an example. */
8055 if (top != old_top || shown != old_shown)
8056 {
8057 if (NILP (bar->dragging))
8058 XawScrollbarSetThumb (widget, top, shown);
8059 else
8060 {
8061#ifdef HAVE_XAW3D
8062 ScrollbarWidget sb = (ScrollbarWidget) widget;
8063 int scroll_mode = 0;
8064
8065 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8066 if (xaw3d_arrow_scroll)
8067 {
8068 /* Xaw3d stupidly ignores resize requests while dragging
8069 so we have to make it believe it's not in dragging mode. */
8070 scroll_mode = sb->scrollbar.scroll_mode;
8071 if (scroll_mode == 2)
8072 sb->scrollbar.scroll_mode = 0;
8073 }
8074#endif
8075 /* Try to make the scrolling a tad smoother. */
8076 if (!xaw3d_pick_top)
8077 shown = min (shown, old_shown);
8078
8079 XawScrollbarSetThumb (widget, top, shown);
8080
8081#ifdef HAVE_XAW3D
8082 if (xaw3d_arrow_scroll && scroll_mode == 2)
8083 sb->scrollbar.scroll_mode = scroll_mode;
8084#endif
8085 }
8086 }
8087 }
8088#endif /* !USE_MOTIF */
8089
8090 UNBLOCK_INPUT;
8091}
8092
8093#endif /* USE_TOOLKIT_SCROLL_BARS */
8094
8095
8096\f
8097/************************************************************************
8098 Scroll bars, general
8099 ************************************************************************/
8100
8101/* Create a scroll bar and return the scroll bar vector for it. W is
8102 the Emacs window on which to create the scroll bar. TOP, LEFT,
8103 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8104 scroll bar. */
8105
8106static struct scroll_bar *
8107x_scroll_bar_create (w, top, left, width, height)
8108 struct window *w;
8109 int top, left, width, height;
8110{
8111 struct frame *f = XFRAME (w->frame);
8112 struct scroll_bar *bar
8113 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8114
8115 BLOCK_INPUT;
8116
8117#if USE_TOOLKIT_SCROLL_BARS
8118 x_create_toolkit_scroll_bar (f, bar);
8119#else /* not USE_TOOLKIT_SCROLL_BARS */
8120 {
8121 XSetWindowAttributes a;
8122 unsigned long mask;
8123 Window window;
8124
8125 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8126 if (a.background_pixel == -1)
8127 a.background_pixel = f->output_data.x->background_pixel;
8128
8129 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8130 | ButtonMotionMask | PointerMotionHintMask
8131 | ExposureMask);
8132 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8133
8134 mask = (CWBackPixel | CWEventMask | CWCursor);
8135
8136 /* Clear the area of W that will serve as a scroll bar. This is
8137 for the case that a window has been split horizontally. In
8138 this case, no clear_frame is generated to reduce flickering. */
8139 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8140 left, top, width,
8141 window_box_height (w), False);
8142
8143 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8144 /* Position and size of scroll bar. */
8145 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8146 top,
8147 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8148 height,
8149 /* Border width, depth, class, and visual. */
8150 0,
8151 CopyFromParent,
8152 CopyFromParent,
8153 CopyFromParent,
8154 /* Attributes. */
8155 mask, &a);
8156 SET_SCROLL_BAR_X_WINDOW (bar, window);
8157 }
8158#endif /* not USE_TOOLKIT_SCROLL_BARS */
8159
8160 XSETWINDOW (bar->window, w);
8161 XSETINT (bar->top, top);
8162 XSETINT (bar->left, left);
8163 XSETINT (bar->width, width);
8164 XSETINT (bar->height, height);
8165 XSETINT (bar->start, 0);
8166 XSETINT (bar->end, 0);
8167 bar->dragging = Qnil;
8168
8169 /* Add bar to its frame's list of scroll bars. */
8170 bar->next = FRAME_SCROLL_BARS (f);
8171 bar->prev = Qnil;
8172 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8173 if (!NILP (bar->next))
8174 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8175
8176 /* Map the window/widget. */
8177#if USE_TOOLKIT_SCROLL_BARS
8178 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8179 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8180 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8181 top,
8182 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8183 height, 0);
8184#else /* not USE_TOOLKIT_SCROLL_BARS */
8185 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8186#endif /* not USE_TOOLKIT_SCROLL_BARS */
8187
8188 UNBLOCK_INPUT;
8189 return bar;
8190}
8191
8192
8193/* Draw BAR's handle in the proper position.
8194
8195 If the handle is already drawn from START to END, don't bother
8196 redrawing it, unless REBUILD is non-zero; in that case, always
8197 redraw it. (REBUILD is handy for drawing the handle after expose
8198 events.)
8199
8200 Normally, we want to constrain the start and end of the handle to
8201 fit inside its rectangle, but if the user is dragging the scroll
8202 bar handle, we want to let them drag it down all the way, so that
8203 the bar's top is as far down as it goes; otherwise, there's no way
8204 to move to the very end of the buffer. */
8205
8206#ifndef USE_TOOLKIT_SCROLL_BARS
8207
8208static void
8209x_scroll_bar_set_handle (bar, start, end, rebuild)
8210 struct scroll_bar *bar;
8211 int start, end;
8212 int rebuild;
8213{
8214 int dragging = ! NILP (bar->dragging);
8215 Window w = SCROLL_BAR_X_WINDOW (bar);
8216 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8217 GC gc = f->output_data.x->normal_gc;
8218
8219 /* If the display is already accurate, do nothing. */
8220 if (! rebuild
8221 && start == XINT (bar->start)
8222 && end == XINT (bar->end))
8223 return;
8224
8225 BLOCK_INPUT;
8226
8227 {
8228 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8229 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8230 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8231
8232 /* Make sure the values are reasonable, and try to preserve
8233 the distance between start and end. */
8234 {
8235 int length = end - start;
8236
8237 if (start < 0)
8238 start = 0;
8239 else if (start > top_range)
8240 start = top_range;
8241 end = start + length;
8242
8243 if (end < start)
8244 end = start;
8245 else if (end > top_range && ! dragging)
8246 end = top_range;
8247 }
8248
8249 /* Store the adjusted setting in the scroll bar. */
8250 XSETINT (bar->start, start);
8251 XSETINT (bar->end, end);
8252
8253 /* Clip the end position, just for display. */
8254 if (end > top_range)
8255 end = top_range;
8256
8257 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8258 below top positions, to make sure the handle is always at least
8259 that many pixels tall. */
8260 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8261
8262 /* Draw the empty space above the handle. Note that we can't clear
8263 zero-height areas; that means "clear to end of window." */
8264 if (0 < start)
8265 XClearArea (FRAME_X_DISPLAY (f), w,
8266
8267 /* x, y, width, height, and exposures. */
8268 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8269 VERTICAL_SCROLL_BAR_TOP_BORDER,
8270 inside_width, start,
8271 False);
8272
8273 /* Change to proper foreground color if one is specified. */
8274 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8275 XSetForeground (FRAME_X_DISPLAY (f), gc,
8276 f->output_data.x->scroll_bar_foreground_pixel);
8277
8278 /* Draw the handle itself. */
8279 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8280
8281 /* x, y, width, height */
8282 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8283 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8284 inside_width, end - start);
8285
8286 /* Restore the foreground color of the GC if we changed it above. */
8287 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8288 XSetForeground (FRAME_X_DISPLAY (f), gc,
8289 f->output_data.x->foreground_pixel);
8290
8291 /* Draw the empty space below the handle. Note that we can't
8292 clear zero-height areas; that means "clear to end of window." */
8293 if (end < inside_height)
8294 XClearArea (FRAME_X_DISPLAY (f), w,
8295
8296 /* x, y, width, height, and exposures. */
8297 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8298 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8299 inside_width, inside_height - end,
8300 False);
8301
8302 }
8303
8304 UNBLOCK_INPUT;
8305}
8306
8307#endif /* !USE_TOOLKIT_SCROLL_BARS */
8308
8309/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8310 nil. */
8311
8312static void
8313x_scroll_bar_remove (bar)
8314 struct scroll_bar *bar;
8315{
8316 BLOCK_INPUT;
8317
8318#if USE_TOOLKIT_SCROLL_BARS
8319 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8320#else /* not USE_TOOLKIT_SCROLL_BARS */
8321 {
8322 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8323 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8324 }
8325#endif /* not USE_TOOLKIT_SCROLL_BARS */
8326
8327 /* Disassociate this scroll bar from its window. */
8328 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8329
8330 UNBLOCK_INPUT;
8331}
8332
8333
8334/* Set the handle of the vertical scroll bar for WINDOW to indicate
8335 that we are displaying PORTION characters out of a total of WHOLE
8336 characters, starting at POSITION. If WINDOW has no scroll bar,
8337 create one. */
8338
8339static void
8340XTset_vertical_scroll_bar (w, portion, whole, position)
8341 struct window *w;
8342 int portion, whole, position;
8343{
8344 struct frame *f = XFRAME (w->frame);
8345 struct scroll_bar *bar;
8346 int top, height, left, sb_left, width, sb_width;
8347 int window_x, window_y, window_width, window_height;
8348
8349 /* Get window dimensions. */
8350 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8351 top = window_y;
8352 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8353 height = window_height;
8354
8355 /* Compute the left edge of the scroll bar area. */
8356 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8357 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8358 else
8359 left = XFASTINT (w->left);
8360 left *= CANON_X_UNIT (f);
8361 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8362
8363 /* Compute the width of the scroll bar which might be less than
8364 the width of the area reserved for the scroll bar. */
8365 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8366 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8367 else
8368 sb_width = width;
8369
8370 /* Compute the left edge of the scroll bar. */
8371#ifdef USE_TOOLKIT_SCROLL_BARS
8372 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8373 sb_left = left + width - sb_width - (width - sb_width) / 2;
8374 else
8375 sb_left = left + (width - sb_width) / 2;
8376#else
8377 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8378 sb_left = left + width - sb_width;
8379 else
8380 sb_left = left;
8381#endif
8382
8383 /* Does the scroll bar exist yet? */
8384 if (NILP (w->vertical_scroll_bar))
8385 {
8386 BLOCK_INPUT;
8387 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8388 left, top, width, height, False);
8389 UNBLOCK_INPUT;
8390 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8391 }
8392 else
8393 {
8394 /* It may just need to be moved and resized. */
8395 unsigned int mask = 0;
8396
8397 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8398
8399 BLOCK_INPUT;
8400
8401 if (sb_left != XINT (bar->left))
8402 mask |= CWX;
8403 if (top != XINT (bar->top))
8404 mask |= CWY;
8405 if (sb_width != XINT (bar->width))
8406 mask |= CWWidth;
8407 if (height != XINT (bar->height))
8408 mask |= CWHeight;
8409
8410#ifdef USE_TOOLKIT_SCROLL_BARS
8411
8412 /* Since toolkit scroll bars are smaller than the space reserved
8413 for them on the frame, we have to clear "under" them. */
8414 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8415 left, top, width, height, False);
8416
8417 /* Move/size the scroll bar widget. */
8418 if (mask)
8419 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8420 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8421 top,
8422 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8423 height, 0);
8424
8425#else /* not USE_TOOLKIT_SCROLL_BARS */
8426
8427 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8428 {
8429 /* Clear areas not covered by the scroll bar. This makes sure a
8430 previous mode line display is cleared after C-x 2 C-x 1, for
8431 example. Non-toolkit scroll bars are as wide as the area
8432 reserved for scroll bars - trim at both sides. */
8433 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8434 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8435 height, False);
8436 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8437 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8438 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8439 height, False);
8440 }
8441
8442 /* Move/size the scroll bar window. */
8443 if (mask)
8444 {
8445 XWindowChanges wc;
8446
8447 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8448 wc.y = top;
8449 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8450 wc.height = height;
8451 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8452 mask, &wc);
8453 }
8454
8455#endif /* not USE_TOOLKIT_SCROLL_BARS */
8456
8457 /* Remember new settings. */
8458 XSETINT (bar->left, sb_left);
8459 XSETINT (bar->top, top);
8460 XSETINT (bar->width, sb_width);
8461 XSETINT (bar->height, height);
8462
8463 UNBLOCK_INPUT;
8464 }
8465
8466#if USE_TOOLKIT_SCROLL_BARS
8467 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8468#else /* not USE_TOOLKIT_SCROLL_BARS */
8469 /* Set the scroll bar's current state, unless we're currently being
8470 dragged. */
8471 if (NILP (bar->dragging))
8472 {
8473 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8474
8475 if (whole == 0)
8476 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8477 else
8478 {
8479 int start = ((double) position * top_range) / whole;
8480 int end = ((double) (position + portion) * top_range) / whole;
8481 x_scroll_bar_set_handle (bar, start, end, 0);
8482 }
8483 }
8484#endif /* not USE_TOOLKIT_SCROLL_BARS */
8485
8486 XSETVECTOR (w->vertical_scroll_bar, bar);
8487}
8488
8489
8490/* The following three hooks are used when we're doing a thorough
8491 redisplay of the frame. We don't explicitly know which scroll bars
8492 are going to be deleted, because keeping track of when windows go
8493 away is a real pain - "Can you say set-window-configuration, boys
8494 and girls?" Instead, we just assert at the beginning of redisplay
8495 that *all* scroll bars are to be removed, and then save a scroll bar
8496 from the fiery pit when we actually redisplay its window. */
8497
8498/* Arrange for all scroll bars on FRAME to be removed at the next call
8499 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8500 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8501
8502static void
8503XTcondemn_scroll_bars (frame)
8504 FRAME_PTR frame;
8505{
8506 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8507 while (! NILP (FRAME_SCROLL_BARS (frame)))
8508 {
8509 Lisp_Object bar;
8510 bar = FRAME_SCROLL_BARS (frame);
8511 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8512 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8513 XSCROLL_BAR (bar)->prev = Qnil;
8514 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8515 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8516 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8517 }
8518}
8519
8520/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8521 Note that WINDOW isn't necessarily condemned at all. */
8522static void
8523XTredeem_scroll_bar (window)
8524 struct window *window;
8525{
8526 struct scroll_bar *bar;
8527
8528 /* We can't redeem this window's scroll bar if it doesn't have one. */
8529 if (NILP (window->vertical_scroll_bar))
8530 abort ();
8531
8532 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8533
8534 /* Unlink it from the condemned list. */
8535 {
8536 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8537
8538 if (NILP (bar->prev))
8539 {
8540 /* If the prev pointer is nil, it must be the first in one of
8541 the lists. */
8542 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8543 /* It's not condemned. Everything's fine. */
8544 return;
8545 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8546 window->vertical_scroll_bar))
8547 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8548 else
8549 /* If its prev pointer is nil, it must be at the front of
8550 one or the other! */
8551 abort ();
8552 }
8553 else
8554 XSCROLL_BAR (bar->prev)->next = bar->next;
8555
8556 if (! NILP (bar->next))
8557 XSCROLL_BAR (bar->next)->prev = bar->prev;
8558
8559 bar->next = FRAME_SCROLL_BARS (f);
8560 bar->prev = Qnil;
8561 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8562 if (! NILP (bar->next))
8563 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8564 }
8565}
8566
8567/* Remove all scroll bars on FRAME that haven't been saved since the
8568 last call to `*condemn_scroll_bars_hook'. */
8569
8570static void
8571XTjudge_scroll_bars (f)
8572 FRAME_PTR f;
8573{
8574 Lisp_Object bar, next;
8575
8576 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8577
8578 /* Clear out the condemned list now so we won't try to process any
8579 more events on the hapless scroll bars. */
8580 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8581
8582 for (; ! NILP (bar); bar = next)
8583 {
8584 struct scroll_bar *b = XSCROLL_BAR (bar);
8585
8586 x_scroll_bar_remove (b);
8587
8588 next = b->next;
8589 b->next = b->prev = Qnil;
8590 }
8591
8592 /* Now there should be no references to the condemned scroll bars,
8593 and they should get garbage-collected. */
8594}
8595
8596
8597/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8598 is a no-op when using toolkit scroll bars.
8599
8600 This may be called from a signal handler, so we have to ignore GC
8601 mark bits. */
8602
8603static void
8604x_scroll_bar_expose (bar, event)
8605 struct scroll_bar *bar;
8606 XEvent *event;
8607{
8608#ifndef USE_TOOLKIT_SCROLL_BARS
8609
8610 Window w = SCROLL_BAR_X_WINDOW (bar);
8611 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8612 GC gc = f->output_data.x->normal_gc;
8613 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8614
8615 BLOCK_INPUT;
8616
8617 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8618
8619 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8620 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8621
8622 /* x, y, width, height */
8623 0, 0,
8624 XINT (bar->width) - 1 - width_trim - width_trim,
8625 XINT (bar->height) - 1);
8626
8627 UNBLOCK_INPUT;
8628
8629#endif /* not USE_TOOLKIT_SCROLL_BARS */
8630}
8631
8632/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8633 is set to something other than no_event, it is enqueued.
8634
8635 This may be called from a signal handler, so we have to ignore GC
8636 mark bits. */
8637
8638#ifndef USE_TOOLKIT_SCROLL_BARS
8639
8640static void
8641x_scroll_bar_handle_click (bar, event, emacs_event)
8642 struct scroll_bar *bar;
8643 XEvent *event;
8644 struct input_event *emacs_event;
8645{
8646 if (! GC_WINDOWP (bar->window))
8647 abort ();
8648
8649 emacs_event->kind = scroll_bar_click;
8650 emacs_event->code = event->xbutton.button - Button1;
8651 emacs_event->modifiers
8652 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8653 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8654 event->xbutton.state)
8655 | (event->type == ButtonRelease
8656 ? up_modifier
8657 : down_modifier));
8658 emacs_event->frame_or_window = bar->window;
8659 emacs_event->arg = Qnil;
8660 emacs_event->timestamp = event->xbutton.time;
8661 {
8662#if 0
8663 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8664 int internal_height
8665 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8666#endif
8667 int top_range
8668 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8669 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8670
8671 if (y < 0) y = 0;
8672 if (y > top_range) y = top_range;
8673
8674 if (y < XINT (bar->start))
8675 emacs_event->part = scroll_bar_above_handle;
8676 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8677 emacs_event->part = scroll_bar_handle;
8678 else
8679 emacs_event->part = scroll_bar_below_handle;
8680
8681 /* Just because the user has clicked on the handle doesn't mean
8682 they want to drag it. Lisp code needs to be able to decide
8683 whether or not we're dragging. */
8684#if 0
8685 /* If the user has just clicked on the handle, record where they're
8686 holding it. */
8687 if (event->type == ButtonPress
8688 && emacs_event->part == scroll_bar_handle)
8689 XSETINT (bar->dragging, y - XINT (bar->start));
8690#endif
8691
8692 /* If the user has released the handle, set it to its final position. */
8693 if (event->type == ButtonRelease
8694 && ! NILP (bar->dragging))
8695 {
8696 int new_start = y - XINT (bar->dragging);
8697 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8698
8699 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8700 bar->dragging = Qnil;
8701 }
8702
8703 /* Same deal here as the other #if 0. */
8704#if 0
8705 /* Clicks on the handle are always reported as occurring at the top of
8706 the handle. */
8707 if (emacs_event->part == scroll_bar_handle)
8708 emacs_event->x = bar->start;
8709 else
8710 XSETINT (emacs_event->x, y);
8711#else
8712 XSETINT (emacs_event->x, y);
8713#endif
8714
8715 XSETINT (emacs_event->y, top_range);
8716 }
8717}
8718
8719/* Handle some mouse motion while someone is dragging the scroll bar.
8720
8721 This may be called from a signal handler, so we have to ignore GC
8722 mark bits. */
8723
8724static void
8725x_scroll_bar_note_movement (bar, event)
8726 struct scroll_bar *bar;
8727 XEvent *event;
8728{
8729 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8730
8731 last_mouse_movement_time = event->xmotion.time;
8732
8733 f->mouse_moved = 1;
8734 XSETVECTOR (last_mouse_scroll_bar, bar);
8735
8736 /* If we're dragging the bar, display it. */
8737 if (! GC_NILP (bar->dragging))
8738 {
8739 /* Where should the handle be now? */
8740 int new_start = event->xmotion.y - XINT (bar->dragging);
8741
8742 if (new_start != XINT (bar->start))
8743 {
8744 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8745
8746 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8747 }
8748 }
8749}
8750
8751#endif /* !USE_TOOLKIT_SCROLL_BARS */
8752
8753/* Return information to the user about the current position of the mouse
8754 on the scroll bar. */
8755
8756static void
8757x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8758 FRAME_PTR *fp;
8759 Lisp_Object *bar_window;
8760 enum scroll_bar_part *part;
8761 Lisp_Object *x, *y;
8762 unsigned long *time;
8763{
8764 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8765 Window w = SCROLL_BAR_X_WINDOW (bar);
8766 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8767 int win_x, win_y;
8768 Window dummy_window;
8769 int dummy_coord;
8770 unsigned int dummy_mask;
8771
8772 BLOCK_INPUT;
8773
8774 /* Get the mouse's position relative to the scroll bar window, and
8775 report that. */
8776 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8777
8778 /* Root, child, root x and root y. */
8779 &dummy_window, &dummy_window,
8780 &dummy_coord, &dummy_coord,
8781
8782 /* Position relative to scroll bar. */
8783 &win_x, &win_y,
8784
8785 /* Mouse buttons and modifier keys. */
8786 &dummy_mask))
8787 ;
8788 else
8789 {
8790#if 0
8791 int inside_height
8792 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8793#endif
8794 int top_range
8795 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8796
8797 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8798
8799 if (! NILP (bar->dragging))
8800 win_y -= XINT (bar->dragging);
8801
8802 if (win_y < 0)
8803 win_y = 0;
8804 if (win_y > top_range)
8805 win_y = top_range;
8806
8807 *fp = f;
8808 *bar_window = bar->window;
8809
8810 if (! NILP (bar->dragging))
8811 *part = scroll_bar_handle;
8812 else if (win_y < XINT (bar->start))
8813 *part = scroll_bar_above_handle;
8814 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8815 *part = scroll_bar_handle;
8816 else
8817 *part = scroll_bar_below_handle;
8818
8819 XSETINT (*x, win_y);
8820 XSETINT (*y, top_range);
8821
8822 f->mouse_moved = 0;
8823 last_mouse_scroll_bar = Qnil;
8824 }
8825
8826 *time = last_mouse_movement_time;
8827
8828 UNBLOCK_INPUT;
8829}
8830
8831
8832/* The screen has been cleared so we may have changed foreground or
8833 background colors, and the scroll bars may need to be redrawn.
8834 Clear out the scroll bars, and ask for expose events, so we can
8835 redraw them. */
8836
8837void
8838x_scroll_bar_clear (f)
8839 FRAME_PTR f;
8840{
8841#ifndef USE_TOOLKIT_SCROLL_BARS
8842 Lisp_Object bar;
8843
8844 /* We can have scroll bars even if this is 0,
8845 if we just turned off scroll bar mode.
8846 But in that case we should not clear them. */
8847 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8848 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8849 bar = XSCROLL_BAR (bar)->next)
8850 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8851 0, 0, 0, 0, True);
8852#endif /* not USE_TOOLKIT_SCROLL_BARS */
8853}
8854
8855/* This processes Expose events from the menu-bar specific X event
8856 loop in xmenu.c. This allows to redisplay the frame if necessary
8857 when handling menu-bar or pop-up items. */
8858
8859int
8860process_expose_from_menu (event)
8861 XEvent event;
8862{
8863 FRAME_PTR f;
8864 struct x_display_info *dpyinfo;
8865 int frame_exposed_p = 0;
8866
8867 BLOCK_INPUT;
8868
8869 dpyinfo = x_display_info_for_display (event.xexpose.display);
8870 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8871 if (f)
8872 {
8873 if (f->async_visible == 0)
8874 {
8875 f->async_visible = 1;
8876 f->async_iconified = 0;
8877 f->output_data.x->has_been_visible = 1;
8878 SET_FRAME_GARBAGED (f);
8879 }
8880 else
8881 {
8882 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8883 event.xexpose.x, event.xexpose.y,
8884 event.xexpose.width, event.xexpose.height);
8885 frame_exposed_p = 1;
8886 }
8887 }
8888 else
8889 {
8890 struct scroll_bar *bar
8891 = x_window_to_scroll_bar (event.xexpose.window);
8892
8893 if (bar)
8894 x_scroll_bar_expose (bar, &event);
8895 }
8896
8897 UNBLOCK_INPUT;
8898 return frame_exposed_p;
8899}
8900\f
8901/* Define a queue to save up SelectionRequest events for later handling. */
8902
8903struct selection_event_queue
8904 {
8905 XEvent event;
8906 struct selection_event_queue *next;
8907 };
8908
8909static struct selection_event_queue *queue;
8910
8911/* Nonzero means queue up certain events--don't process them yet. */
8912
8913static int x_queue_selection_requests;
8914
8915/* Queue up an X event *EVENT, to be processed later. */
8916
8917static void
8918x_queue_event (f, event)
8919 FRAME_PTR f;
8920 XEvent *event;
8921{
8922 struct selection_event_queue *queue_tmp
8923 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8924
8925 if (queue_tmp != NULL)
8926 {
8927 queue_tmp->event = *event;
8928 queue_tmp->next = queue;
8929 queue = queue_tmp;
8930 }
8931}
8932
8933/* Take all the queued events and put them back
8934 so that they get processed afresh. */
8935
8936static void
8937x_unqueue_events (display)
8938 Display *display;
8939{
8940 while (queue != NULL)
8941 {
8942 struct selection_event_queue *queue_tmp = queue;
8943 XPutBackEvent (display, &queue_tmp->event);
8944 queue = queue_tmp->next;
8945 xfree ((char *)queue_tmp);
8946 }
8947}
8948
8949/* Start queuing SelectionRequest events. */
8950
8951void
8952x_start_queuing_selection_requests (display)
8953 Display *display;
8954{
8955 x_queue_selection_requests++;
8956}
8957
8958/* Stop queuing SelectionRequest events. */
8959
8960void
8961x_stop_queuing_selection_requests (display)
8962 Display *display;
8963{
8964 x_queue_selection_requests--;
8965 x_unqueue_events (display);
8966}
8967\f
8968/* The main X event-reading loop - XTread_socket. */
8969
8970/* Time stamp of enter window event. This is only used by XTread_socket,
8971 but we have to put it out here, since static variables within functions
8972 sometimes don't work. */
8973
8974static Time enter_timestamp;
8975
8976/* This holds the state XLookupString needs to implement dead keys
8977 and other tricks known as "compose processing". _X Window System_
8978 says that a portable program can't use this, but Stephen Gildea assures
8979 me that letting the compiler initialize it to zeros will work okay.
8980
8981 This must be defined outside of XTread_socket, for the same reasons
8982 given for enter_time stamp, above. */
8983
8984static XComposeStatus compose_status;
8985
8986/* Record the last 100 characters stored
8987 to help debug the loss-of-chars-during-GC problem. */
8988
8989static int temp_index;
8990static short temp_buffer[100];
8991
8992/* Set this to nonzero to fake an "X I/O error"
8993 on a particular display. */
8994
8995struct x_display_info *XTread_socket_fake_io_error;
8996
8997/* When we find no input here, we occasionally do a no-op command
8998 to verify that the X server is still running and we can still talk with it.
8999 We try all the open displays, one by one.
9000 This variable is used for cycling thru the displays. */
9001
9002static struct x_display_info *next_noop_dpyinfo;
9003
9004#define SET_SAVED_MENU_EVENT(size) \
9005 do \
9006 { \
9007 if (f->output_data.x->saved_menu_event == 0) \
9008 f->output_data.x->saved_menu_event \
9009 = (XEvent *) xmalloc (sizeof (XEvent)); \
9010 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9011 if (numchars >= 1) \
9012 { \
9013 bufp->kind = menu_bar_activate_event; \
9014 XSETFRAME (bufp->frame_or_window, f); \
9015 bufp->arg = Qnil; \
9016 bufp++; \
9017 count++; \
9018 numchars--; \
9019 } \
9020 } \
9021 while (0)
9022
9023#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9024#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9025
9026/* Read events coming from the X server.
9027 This routine is called by the SIGIO handler.
9028 We return as soon as there are no more events to be read.
9029
9030 Events representing keys are stored in buffer BUFP,
9031 which can hold up to NUMCHARS characters.
9032 We return the number of characters stored into the buffer,
9033 thus pretending to be `read'.
9034
9035 EXPECTED is nonzero if the caller knows input is available. */
9036
9037int
9038XTread_socket (sd, bufp, numchars, expected)
9039 register int sd;
9040 /* register */ struct input_event *bufp;
9041 /* register */ int numchars;
9042 int expected;
9043{
9044 int count = 0;
9045 int nbytes = 0;
9046 XEvent event;
9047 struct frame *f;
9048 int event_found = 0;
9049 struct x_display_info *dpyinfo;
9050 struct coding_system coding;
9051
9052 if (interrupt_input_blocked)
9053 {
9054 interrupt_input_pending = 1;
9055 return -1;
9056 }
9057
9058 interrupt_input_pending = 0;
9059 BLOCK_INPUT;
9060
9061 /* So people can tell when we have read the available input. */
9062 input_signal_count++;
9063
9064 if (numchars <= 0)
9065 abort (); /* Don't think this happens. */
9066
9067 ++handling_signal;
9068
9069 /* The input should be decoded if it is from XIM. Currently the
9070 locale of XIM is the same as that of the system. So, we can use
9071 Vlocale_coding_system which is initialized properly at Emacs
9072 startup time. */
9073 setup_coding_system (Vlocale_coding_system, &coding);
9074 coding.src_multibyte = 0;
9075 coding.dst_multibyte = 1;
9076 /* The input is converted to events, thus we can't handle
9077 composition. Anyway, there's no XIM that gives us composition
9078 information. */
9079 coding.composing = COMPOSITION_DISABLED;
9080
9081 /* Find the display we are supposed to read input for.
9082 It's the one communicating on descriptor SD. */
9083 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9084 {
9085#if 0 /* This ought to be unnecessary; let's verify it. */
9086#ifdef FIOSNBIO
9087 /* If available, Xlib uses FIOSNBIO to make the socket
9088 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9089 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9090 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9091 fcntl (dpyinfo->connection, F_SETFL, 0);
9092#endif /* ! defined (FIOSNBIO) */
9093#endif
9094
9095#if 0 /* This code can't be made to work, with multiple displays,
9096 and appears not to be used on any system any more.
9097 Also keyboard.c doesn't turn O_NDELAY on and off
9098 for X connections. */
9099#ifndef SIGIO
9100#ifndef HAVE_SELECT
9101 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9102 {
9103 extern int read_alarm_should_throw;
9104 read_alarm_should_throw = 1;
9105 XPeekEvent (dpyinfo->display, &event);
9106 read_alarm_should_throw = 0;
9107 }
9108#endif /* HAVE_SELECT */
9109#endif /* SIGIO */
9110#endif
9111
9112 /* For debugging, this gives a way to fake an I/O error. */
9113 if (dpyinfo == XTread_socket_fake_io_error)
9114 {
9115 XTread_socket_fake_io_error = 0;
9116 x_io_error_quitter (dpyinfo->display);
9117 }
9118
9119 while (XPending (dpyinfo->display))
9120 {
9121 XNextEvent (dpyinfo->display, &event);
9122
9123#ifdef HAVE_X_I18N
9124 {
9125 /* Filter events for the current X input method.
9126 XFilterEvent returns non-zero if the input method has
9127 consumed the event. We pass the frame's X window to
9128 XFilterEvent because that's the one for which the IC
9129 was created. */
9130 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9131 event.xclient.window);
9132 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9133 break;
9134 }
9135#endif
9136 event_found = 1;
9137
9138 switch (event.type)
9139 {
9140 case ClientMessage:
9141 {
9142 if (event.xclient.message_type
9143 == dpyinfo->Xatom_wm_protocols
9144 && event.xclient.format == 32)
9145 {
9146 if (event.xclient.data.l[0]
9147 == dpyinfo->Xatom_wm_take_focus)
9148 {
9149 /* Use x_any_window_to_frame because this
9150 could be the shell widget window
9151 if the frame has no title bar. */
9152 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9153#ifdef HAVE_X_I18N
9154 /* Not quite sure this is needed -pd */
9155 if (f && FRAME_XIC (f))
9156 XSetICFocus (FRAME_XIC (f));
9157#endif
9158#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9159 instructs the WM to set the input focus automatically for
9160 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9161 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9162 it has set the focus. So, XSetInputFocus below is not
9163 needed.
9164
9165 The call to XSetInputFocus below has also caused trouble. In
9166 cases where the XSetInputFocus done by the WM and the one
9167 below are temporally close (on a fast machine), the call
9168 below can generate additional FocusIn events which confuse
9169 Emacs. */
9170
9171 /* Since we set WM_TAKE_FOCUS, we must call
9172 XSetInputFocus explicitly. But not if f is null,
9173 since that might be an event for a deleted frame. */
9174 if (f)
9175 {
9176 Display *d = event.xclient.display;
9177 /* Catch and ignore errors, in case window has been
9178 iconified by a window manager such as GWM. */
9179 int count = x_catch_errors (d);
9180 XSetInputFocus (d, event.xclient.window,
9181 /* The ICCCM says this is
9182 the only valid choice. */
9183 RevertToParent,
9184 event.xclient.data.l[1]);
9185 /* This is needed to detect the error
9186 if there is an error. */
9187 XSync (d, False);
9188 x_uncatch_errors (d, count);
9189 }
9190 /* Not certain about handling scroll bars here */
9191#endif /* 0 */
9192 }
9193 else if (event.xclient.data.l[0]
9194 == dpyinfo->Xatom_wm_save_yourself)
9195 {
9196 /* Save state modify the WM_COMMAND property to
9197 something which can reinstate us. This notifies
9198 the session manager, who's looking for such a
9199 PropertyNotify. Can restart processing when
9200 a keyboard or mouse event arrives. */
9201 if (numchars > 0)
9202 {
9203 f = x_top_window_to_frame (dpyinfo,
9204 event.xclient.window);
9205
9206 /* This is just so we only give real data once
9207 for a single Emacs process. */
9208 if (f == SELECTED_FRAME ())
9209 XSetCommand (FRAME_X_DISPLAY (f),
9210 event.xclient.window,
9211 initial_argv, initial_argc);
9212 else if (f)
9213 XSetCommand (FRAME_X_DISPLAY (f),
9214 event.xclient.window,
9215 0, 0);
9216 }
9217 }
9218 else if (event.xclient.data.l[0]
9219 == dpyinfo->Xatom_wm_delete_window)
9220 {
9221 struct frame *f
9222 = x_any_window_to_frame (dpyinfo,
9223 event.xclient.window);
9224
9225 if (f)
9226 {
9227 if (numchars == 0)
9228 abort ();
9229
9230 bufp->kind = delete_window_event;
9231 XSETFRAME (bufp->frame_or_window, f);
9232 bufp->arg = Qnil;
9233 bufp++;
9234
9235 count += 1;
9236 numchars -= 1;
9237 }
9238 }
9239 }
9240 else if (event.xclient.message_type
9241 == dpyinfo->Xatom_wm_configure_denied)
9242 {
9243 }
9244 else if (event.xclient.message_type
9245 == dpyinfo->Xatom_wm_window_moved)
9246 {
9247 int new_x, new_y;
9248 struct frame *f
9249 = x_window_to_frame (dpyinfo, event.xclient.window);
9250
9251 new_x = event.xclient.data.s[0];
9252 new_y = event.xclient.data.s[1];
9253
9254 if (f)
9255 {
9256 f->output_data.x->left_pos = new_x;
9257 f->output_data.x->top_pos = new_y;
9258 }
9259 }
9260#ifdef HACK_EDITRES
9261 else if (event.xclient.message_type
9262 == dpyinfo->Xatom_editres)
9263 {
9264 struct frame *f
9265 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9266 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9267 &event, NULL);
9268 }
9269#endif /* HACK_EDITRES */
9270 else if ((event.xclient.message_type
9271 == dpyinfo->Xatom_DONE)
9272 || (event.xclient.message_type
9273 == dpyinfo->Xatom_PAGE))
9274 {
9275 /* Ghostview job completed. Kill it. We could
9276 reply with "Next" if we received "Page", but we
9277 currently never do because we are interested in
9278 images, only, which should have 1 page. */
9279 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9280 struct frame *f
9281 = x_window_to_frame (dpyinfo, event.xclient.window);
9282 x_kill_gs_process (pixmap, f);
9283 expose_frame (f, 0, 0, 0, 0);
9284 }
9285#ifdef USE_TOOLKIT_SCROLL_BARS
9286 /* Scroll bar callbacks send a ClientMessage from which
9287 we construct an input_event. */
9288 else if (event.xclient.message_type
9289 == dpyinfo->Xatom_Scrollbar)
9290 {
9291 x_scroll_bar_to_input_event (&event, bufp);
9292 ++bufp, ++count, --numchars;
9293 goto out;
9294 }
9295#endif /* USE_TOOLKIT_SCROLL_BARS */
9296 else
9297 goto OTHER;
9298 }
9299 break;
9300
9301 case SelectionNotify:
9302#ifdef USE_X_TOOLKIT
9303 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9304 goto OTHER;
9305#endif /* not USE_X_TOOLKIT */
9306 x_handle_selection_notify (&event.xselection);
9307 break;
9308
9309 case SelectionClear: /* Someone has grabbed ownership. */
9310#ifdef USE_X_TOOLKIT
9311 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9312 goto OTHER;
9313#endif /* USE_X_TOOLKIT */
9314 {
9315 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9316
9317 if (numchars == 0)
9318 abort ();
9319
9320 bufp->kind = selection_clear_event;
9321 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9322 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9323 SELECTION_EVENT_TIME (bufp) = eventp->time;
9324 bufp->frame_or_window = Qnil;
9325 bufp->arg = Qnil;
9326 bufp++;
9327
9328 count += 1;
9329 numchars -= 1;
9330 }
9331 break;
9332
9333 case SelectionRequest: /* Someone wants our selection. */
9334#ifdef USE_X_TOOLKIT
9335 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9336 goto OTHER;
9337#endif /* USE_X_TOOLKIT */
9338 if (x_queue_selection_requests)
9339 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9340 &event);
9341 else
9342 {
9343 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9344
9345 if (numchars == 0)
9346 abort ();
9347
9348 bufp->kind = selection_request_event;
9349 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9350 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9351 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9352 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9353 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9354 SELECTION_EVENT_TIME (bufp) = eventp->time;
9355 bufp->frame_or_window = Qnil;
9356 bufp->arg = Qnil;
9357 bufp++;
9358
9359 count += 1;
9360 numchars -= 1;
9361 }
9362 break;
9363
9364 case PropertyNotify:
9365#ifdef USE_X_TOOLKIT
9366 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9367 goto OTHER;
9368#endif /* not USE_X_TOOLKIT */
9369 x_handle_property_notify (&event.xproperty);
9370 break;
9371
9372 case ReparentNotify:
9373 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9374 if (f)
9375 {
9376 int x, y;
9377 f->output_data.x->parent_desc = event.xreparent.parent;
9378 x_real_positions (f, &x, &y);
9379 f->output_data.x->left_pos = x;
9380 f->output_data.x->top_pos = y;
9381 }
9382 break;
9383
9384 case Expose:
9385 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9386 if (f)
9387 {
9388 if (f->async_visible == 0)
9389 {
9390 f->async_visible = 1;
9391 f->async_iconified = 0;
9392 f->output_data.x->has_been_visible = 1;
9393 SET_FRAME_GARBAGED (f);
9394 }
9395 else
9396 expose_frame (x_window_to_frame (dpyinfo,
9397 event.xexpose.window),
9398 event.xexpose.x, event.xexpose.y,
9399 event.xexpose.width, event.xexpose.height);
9400 }
9401 else
9402 {
9403#ifdef USE_TOOLKIT_SCROLL_BARS
9404 /* Dispatch event to the widget. */
9405 goto OTHER;
9406#else /* not USE_TOOLKIT_SCROLL_BARS */
9407 struct scroll_bar *bar
9408 = x_window_to_scroll_bar (event.xexpose.window);
9409
9410 if (bar)
9411 x_scroll_bar_expose (bar, &event);
9412#ifdef USE_X_TOOLKIT
9413 else
9414 goto OTHER;
9415#endif /* USE_X_TOOLKIT */
9416#endif /* not USE_TOOLKIT_SCROLL_BARS */
9417 }
9418 break;
9419
9420 case GraphicsExpose: /* This occurs when an XCopyArea's
9421 source area was obscured or not
9422 available.*/
9423 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9424 if (f)
9425 {
9426 expose_frame (f,
9427 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9428 event.xgraphicsexpose.width,
9429 event.xgraphicsexpose.height);
9430 }
9431#ifdef USE_X_TOOLKIT
9432 else
9433 goto OTHER;
9434#endif /* USE_X_TOOLKIT */
9435 break;
9436
9437 case NoExpose: /* This occurs when an XCopyArea's
9438 source area was completely
9439 available */
9440 break;
9441
9442 case UnmapNotify:
9443 /* Redo the mouse-highlight after the tooltip has gone. */
9444 if (event.xmap.window == tip_window)
9445 {
9446 tip_window = 0;
9447 redo_mouse_highlight ();
9448 }
9449
9450 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9451 if (f) /* F may no longer exist if
9452 the frame was deleted. */
9453 {
9454 /* While a frame is unmapped, display generation is
9455 disabled; you don't want to spend time updating a
9456 display that won't ever be seen. */
9457 f->async_visible = 0;
9458 /* We can't distinguish, from the event, whether the window
9459 has become iconified or invisible. So assume, if it
9460 was previously visible, than now it is iconified.
9461 But x_make_frame_invisible clears both
9462 the visible flag and the iconified flag;
9463 and that way, we know the window is not iconified now. */
9464 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9465 {
9466 f->async_iconified = 1;
9467
9468 bufp->kind = iconify_event;
9469 XSETFRAME (bufp->frame_or_window, f);
9470 bufp->arg = Qnil;
9471 bufp++;
9472 count++;
9473 numchars--;
9474 }
9475 }
9476 goto OTHER;
9477
9478 case MapNotify:
9479 if (event.xmap.window == tip_window)
9480 /* The tooltip has been drawn already. Avoid
9481 the SET_FRAME_GARBAGED below. */
9482 goto OTHER;
9483
9484 /* We use x_top_window_to_frame because map events can
9485 come for sub-windows and they don't mean that the
9486 frame is visible. */
9487 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9488 if (f)
9489 {
9490 f->async_visible = 1;
9491 f->async_iconified = 0;
9492 f->output_data.x->has_been_visible = 1;
9493
9494 /* wait_reading_process_input will notice this and update
9495 the frame's display structures. */
9496 SET_FRAME_GARBAGED (f);
9497
9498 if (f->iconified)
9499 {
9500 bufp->kind = deiconify_event;
9501 XSETFRAME (bufp->frame_or_window, f);
9502 bufp->arg = Qnil;
9503 bufp++;
9504 count++;
9505 numchars--;
9506 }
9507 else if (! NILP (Vframe_list)
9508 && ! NILP (XCDR (Vframe_list)))
9509 /* Force a redisplay sooner or later
9510 to update the frame titles
9511 in case this is the second frame. */
9512 record_asynch_buffer_change ();
9513 }
9514 goto OTHER;
9515
9516 case KeyPress:
9517 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9518
9519#ifdef USE_MOTIF
9520 /* I couldn't find a way to prevent LessTif scroll bars
9521 from consuming key events. */
9522 if (f == 0)
9523 {
9524 Widget widget = XtWindowToWidget (dpyinfo->display,
9525 event.xkey.window);
9526 if (widget && XmIsScrollBar (widget))
9527 {
9528 widget = XtParent (widget);
9529 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9530 }
9531 }
9532#endif /* USE_MOTIF */
9533
9534 if (f != 0)
9535 {
9536 KeySym keysym, orig_keysym;
9537 /* al%imercury@uunet.uu.net says that making this 81
9538 instead of 80 fixed a bug whereby meta chars made
9539 his Emacs hang.
9540
9541 It seems that some version of XmbLookupString has
9542 a bug of not returning XBufferOverflow in
9543 status_return even if the input is too long to
9544 fit in 81 bytes. So, we must prepare sufficient
9545 bytes for copy_buffer. 513 bytes (256 chars for
9546 two-byte character set) seems to be a faily good
9547 approximation. -- 2000.8.10 handa@etl.go.jp */
9548 unsigned char copy_buffer[513];
9549 unsigned char *copy_bufptr = copy_buffer;
9550 int copy_bufsiz = sizeof (copy_buffer);
9551 int modifiers;
9552
9553 event.xkey.state
9554 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9555 extra_keyboard_modifiers);
9556 modifiers = event.xkey.state;
9557
9558 /* This will have to go some day... */
9559
9560 /* make_lispy_event turns chars into control chars.
9561 Don't do it here because XLookupString is too eager. */
9562 event.xkey.state &= ~ControlMask;
9563 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9564 | dpyinfo->super_mod_mask
9565 | dpyinfo->hyper_mod_mask
9566 | dpyinfo->alt_mod_mask);
9567
9568 /* In case Meta is ComposeCharacter,
9569 clear its status. According to Markus Ehrnsperger
9570 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9571 this enables ComposeCharacter to work whether or
9572 not it is combined with Meta. */
9573 if (modifiers & dpyinfo->meta_mod_mask)
9574 bzero (&compose_status, sizeof (compose_status));
9575
9576#ifdef HAVE_X_I18N
9577 if (FRAME_XIC (f))
9578 {
9579 Status status_return;
9580
9581 nbytes = XmbLookupString (FRAME_XIC (f),
9582 &event.xkey, copy_bufptr,
9583 copy_bufsiz, &keysym,
9584 &status_return);
9585 if (status_return == XBufferOverflow)
9586 {
9587 copy_bufsiz = nbytes + 1;
9588 copy_bufptr = (char *) alloca (copy_bufsiz);
9589 nbytes = XmbLookupString (FRAME_XIC (f),
9590 &event.xkey, copy_bufptr,
9591 copy_bufsiz, &keysym,
9592 &status_return);
9593 }
9594
9595 if (status_return == XLookupNone)
9596 break;
9597 else if (status_return == XLookupChars)
9598 {
9599 keysym = NoSymbol;
9600 modifiers = 0;
9601 }
9602 else if (status_return != XLookupKeySym
9603 && status_return != XLookupBoth)
9604 abort ();
9605 }
9606 else
9607 nbytes = XLookupString (&event.xkey, copy_bufptr,
9608 copy_bufsiz, &keysym,
9609 &compose_status);
9610#else
9611 nbytes = XLookupString (&event.xkey, copy_bufptr,
9612 copy_bufsiz, &keysym,
9613 &compose_status);
9614#endif
9615
9616 orig_keysym = keysym;
9617
9618 if (numchars > 1)
9619 {
9620 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9621 || keysym == XK_Delete
9622#ifdef XK_ISO_Left_Tab
9623 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9624#endif
9625 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9626 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9627 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9628#ifdef HPUX
9629 /* This recognizes the "extended function keys".
9630 It seems there's no cleaner way.
9631 Test IsModifierKey to avoid handling mode_switch
9632 incorrectly. */
9633 || ((unsigned) (keysym) >= XK_Select
9634 && (unsigned)(keysym) < XK_KP_Space)
9635#endif
9636#ifdef XK_dead_circumflex
9637 || orig_keysym == XK_dead_circumflex
9638#endif
9639#ifdef XK_dead_grave
9640 || orig_keysym == XK_dead_grave
9641#endif
9642#ifdef XK_dead_tilde
9643 || orig_keysym == XK_dead_tilde
9644#endif
9645#ifdef XK_dead_diaeresis
9646 || orig_keysym == XK_dead_diaeresis
9647#endif
9648#ifdef XK_dead_macron
9649 || orig_keysym == XK_dead_macron
9650#endif
9651#ifdef XK_dead_degree
9652 || orig_keysym == XK_dead_degree
9653#endif
9654#ifdef XK_dead_acute
9655 || orig_keysym == XK_dead_acute
9656#endif
9657#ifdef XK_dead_cedilla
9658 || orig_keysym == XK_dead_cedilla
9659#endif
9660#ifdef XK_dead_breve
9661 || orig_keysym == XK_dead_breve
9662#endif
9663#ifdef XK_dead_ogonek
9664 || orig_keysym == XK_dead_ogonek
9665#endif
9666#ifdef XK_dead_caron
9667 || orig_keysym == XK_dead_caron
9668#endif
9669#ifdef XK_dead_doubleacute
9670 || orig_keysym == XK_dead_doubleacute
9671#endif
9672#ifdef XK_dead_abovedot
9673 || orig_keysym == XK_dead_abovedot
9674#endif
9675 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9676 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9677 /* Any "vendor-specific" key is ok. */
9678 || (orig_keysym & (1 << 28)))
9679 && ! (IsModifierKey (orig_keysym)
9680#ifndef HAVE_X11R5
9681#ifdef XK_Mode_switch
9682 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9683#endif
9684#ifdef XK_Num_Lock
9685 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9686#endif
9687#endif /* not HAVE_X11R5 */
9688 ))
9689 {
9690 if (temp_index == sizeof temp_buffer / sizeof (short))
9691 temp_index = 0;
9692 temp_buffer[temp_index++] = keysym;
9693 bufp->kind = non_ascii_keystroke;
9694 bufp->code = keysym;
9695 XSETFRAME (bufp->frame_or_window, f);
9696 bufp->arg = Qnil;
9697 bufp->modifiers
9698 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9699 modifiers);
9700 bufp->timestamp = event.xkey.time;
9701 bufp++;
9702 count++;
9703 numchars--;
9704 }
9705 else if (numchars > nbytes)
9706 {
9707 register int i;
9708 register int c;
9709 unsigned char *p, *pend;
9710 int nchars, len;
9711
9712 for (i = 0; i < nbytes; i++)
9713 {
9714 if (temp_index == (sizeof temp_buffer
9715 / sizeof (short)))
9716 temp_index = 0;
9717 temp_buffer[temp_index++] = copy_bufptr[i];
9718 }
9719
9720 if (/* If the event is not from XIM, */
9721 event.xkey.keycode != 0
9722 /* or the current locale doesn't request
9723 decoding of the intup data, ... */
9724 || coding.type == coding_type_raw_text
9725 || coding.type == coding_type_no_conversion)
9726 {
9727 /* ... we can use the input data as is. */
9728 nchars = nbytes;
9729 }
9730 else
9731 {
9732 /* We have to decode the input data. */
9733 int require;
9734 unsigned char *p;
9735
9736 require = decoding_buffer_size (&coding, nbytes);
9737 p = (unsigned char *) alloca (require);
9738 coding.mode |= CODING_MODE_LAST_BLOCK;
9739 decode_coding (&coding, copy_bufptr, p,
9740 nbytes, require);
9741 nbytes = coding.produced;
9742 nchars = coding.produced_char;
9743 copy_bufptr = p;
9744 }
9745
9746 /* Convert the input data to a sequence of
9747 character events. */
9748 for (i = 0; i < nbytes; i += len)
9749 {
9750 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9751 nbytes - i, len);
9752 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9753 ? ascii_keystroke
9754 : multibyte_char_keystroke);
9755 bufp->code = c;
9756 XSETFRAME (bufp->frame_or_window, f);
9757 bufp->arg = Qnil;
9758 bufp->modifiers
9759 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9760 modifiers);
9761 bufp->timestamp = event.xkey.time;
9762 bufp++;
9763 }
9764
9765 count += nchars;
9766 numchars -= nchars;
9767
9768 if (keysym == NoSymbol)
9769 break;
9770 }
9771 else
9772 abort ();
9773 }
9774 else
9775 abort ();
9776 }
9777#ifdef HAVE_X_I18N
9778 /* Don't dispatch this event since XtDispatchEvent calls
9779 XFilterEvent, and two calls in a row may freeze the
9780 client. */
9781 break;
9782#else
9783 goto OTHER;
9784#endif
9785
9786 case KeyRelease:
9787#ifdef HAVE_X_I18N
9788 /* Don't dispatch this event since XtDispatchEvent calls
9789 XFilterEvent, and two calls in a row may freeze the
9790 client. */
9791 break;
9792#else
9793 goto OTHER;
9794#endif
9795
9796 /* Here's a possible interpretation of the whole
9797 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9798 you get a FocusIn event, you have to get a FocusOut
9799 event before you relinquish the focus. If you
9800 haven't received a FocusIn event, then a mere
9801 LeaveNotify is enough to free you. */
9802
9803 case EnterNotify:
9804 {
9805 int from_menu_bar_p = 0;
9806
9807 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9808
9809#ifdef LESSTIF_VERSION
9810 /* When clicking outside of a menu bar popup to close
9811 it, we get a FocusIn/ EnterNotify sequence of
9812 events. The flag event.xcrossing.focus is not set
9813 in the EnterNotify event of that sequence because
9814 the focus is in the menu bar,
9815 event.xcrossing.window is the frame's X window.
9816 Unconditionally setting the focus frame to null in
9817 this case is not the right thing, because no event
9818 follows that could set the focus frame to the right
9819 value.
9820
9821 This could be a LessTif bug, but I wasn't able to
9822 reproduce the behavior in a simple test program.
9823
9824 (gerd, LessTif 0.88.1). */
9825
9826 if (!event.xcrossing.focus
9827 && f
9828 && f->output_data.x->menubar_widget)
9829 {
9830 Window focus;
9831 int revert;
9832
9833 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9834 if (focus == XtWindow (f->output_data.x->menubar_widget))
9835 from_menu_bar_p = 1;
9836 }
9837#endif /* LESSTIF_VERSION */
9838
9839 if (event.xcrossing.focus || from_menu_bar_p)
9840 {
9841 /* Avoid nasty pop/raise loops. */
9842 if (f && (!(f->auto_raise)
9843 || !(f->auto_lower)
9844 || (event.xcrossing.time - enter_timestamp) > 500))
9845 {
9846 x_new_focus_frame (dpyinfo, f);
9847 enter_timestamp = event.xcrossing.time;
9848 }
9849 }
9850 else if (f == dpyinfo->x_focus_frame)
9851 x_new_focus_frame (dpyinfo, 0);
9852
9853 /* EnterNotify counts as mouse movement,
9854 so update things that depend on mouse position. */
9855 if (f && !f->output_data.x->busy_p)
9856 note_mouse_movement (f, &event.xmotion);
9857 goto OTHER;
9858 }
9859
9860 case FocusIn:
9861 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9862 if (event.xfocus.detail != NotifyPointer)
9863 dpyinfo->x_focus_event_frame = f;
9864 if (f)
9865 {
9866 x_new_focus_frame (dpyinfo, f);
9867
9868 /* Don't stop displaying the initial startup message
9869 for a switch-frame event we don't need. */
9870 if (GC_NILP (Vterminal_frame)
9871 && GC_CONSP (Vframe_list)
9872 && !GC_NILP (XCDR (Vframe_list)))
9873 {
9874 bufp->kind = FOCUS_IN_EVENT;
9875 XSETFRAME (bufp->frame_or_window, f);
9876 bufp->arg = Qnil;
9877 ++bufp, ++count, --numchars;
9878 }
9879 }
9880
9881#ifdef HAVE_X_I18N
9882 if (f && FRAME_XIC (f))
9883 XSetICFocus (FRAME_XIC (f));
9884#endif
9885
9886 goto OTHER;
9887
9888 case LeaveNotify:
9889 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9890 if (f)
9891 {
9892 Lisp_Object frame;
9893 int from_menu_bar_p = 0;
9894
9895 if (f == dpyinfo->mouse_face_mouse_frame)
9896 {
9897 /* If we move outside the frame, then we're
9898 certainly no longer on any text in the frame. */
9899 clear_mouse_face (dpyinfo);
9900 dpyinfo->mouse_face_mouse_frame = 0;
9901 }
9902
9903 /* Generate a nil HELP_EVENT to cancel a help-echo.
9904 Do it only if there's something to cancel.
9905 Otherwise, the startup message is cleared when
9906 the mouse leaves the frame. */
9907 if (any_help_event_p)
9908 {
9909 Lisp_Object frame;
9910 int n;
9911
9912 XSETFRAME (frame, f);
9913 n = gen_help_event (bufp, numchars,
9914 Qnil, frame, Qnil, Qnil, 0);
9915 bufp += n, count += n, numchars -= n;
9916 }
9917
9918#ifdef LESSTIF_VERSION
9919 /* Please see the comment at the start of the
9920 EnterNotify case. */
9921 if (!event.xcrossing.focus
9922 && f->output_data.x->menubar_widget)
9923 {
9924 Window focus;
9925 int revert;
9926 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9927 if (focus == XtWindow (f->output_data.x->menubar_widget))
9928 from_menu_bar_p = 1;
9929 }
9930#endif /* LESSTIF_VERSION */
9931
9932 if (event.xcrossing.focus || from_menu_bar_p)
9933 x_mouse_leave (dpyinfo);
9934 else
9935 {
9936 if (f == dpyinfo->x_focus_event_frame)
9937 dpyinfo->x_focus_event_frame = 0;
9938 if (f == dpyinfo->x_focus_frame)
9939 x_new_focus_frame (dpyinfo, 0);
9940 }
9941 }
9942 goto OTHER;
9943
9944 case FocusOut:
9945 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9946 if (event.xfocus.detail != NotifyPointer
9947 && f == dpyinfo->x_focus_event_frame)
9948 dpyinfo->x_focus_event_frame = 0;
9949 if (f && f == dpyinfo->x_focus_frame)
9950 x_new_focus_frame (dpyinfo, 0);
9951
9952#ifdef HAVE_X_I18N
9953 if (f && FRAME_XIC (f))
9954 XUnsetICFocus (FRAME_XIC (f));
9955#endif
9956
9957 goto OTHER;
9958
9959 case MotionNotify:
9960 {
9961 previous_help_echo = help_echo;
9962 help_echo = help_echo_object = help_echo_window = Qnil;
9963 help_echo_pos = -1;
9964
9965 if (dpyinfo->grabbed && last_mouse_frame
9966 && FRAME_LIVE_P (last_mouse_frame))
9967 f = last_mouse_frame;
9968 else
9969 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9970
9971 if (f)
9972 note_mouse_movement (f, &event.xmotion);
9973 else
9974 {
9975#ifndef USE_TOOLKIT_SCROLL_BARS
9976 struct scroll_bar *bar
9977 = x_window_to_scroll_bar (event.xmotion.window);
9978
9979 if (bar)
9980 x_scroll_bar_note_movement (bar, &event);
9981#endif /* USE_TOOLKIT_SCROLL_BARS */
9982
9983 /* If we move outside the frame, then we're
9984 certainly no longer on any text in the frame. */
9985 clear_mouse_face (dpyinfo);
9986 }
9987
9988 /* If the contents of the global variable help_echo
9989 has changed, generate a HELP_EVENT. */
9990 if (!NILP (help_echo)
9991 || !NILP (previous_help_echo))
9992 {
9993 Lisp_Object frame;
9994 int n;
9995
9996 if (f)
9997 XSETFRAME (frame, f);
9998 else
9999 frame = Qnil;
10000
10001 any_help_event_p = 1;
10002 n = gen_help_event (bufp, numchars, help_echo, frame,
10003 help_echo_window, help_echo_object,
10004 help_echo_pos);
10005 bufp += n, count += n, numchars -= n;
10006 }
10007
10008 goto OTHER;
10009 }
10010
10011 case ConfigureNotify:
10012 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10013 if (f)
10014 {
10015#ifndef USE_X_TOOLKIT
10016 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10017 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10018
10019 /* In the toolkit version, change_frame_size
10020 is called by the code that handles resizing
10021 of the EmacsFrame widget. */
10022
10023 /* Even if the number of character rows and columns has
10024 not changed, the font size may have changed, so we need
10025 to check the pixel dimensions as well. */
10026 if (columns != f->width
10027 || rows != f->height
10028 || event.xconfigure.width != f->output_data.x->pixel_width
10029 || event.xconfigure.height != f->output_data.x->pixel_height)
10030 {
10031 change_frame_size (f, rows, columns, 0, 1, 0);
10032 SET_FRAME_GARBAGED (f);
10033 cancel_mouse_face (f);
10034 }
10035#endif
10036
10037 f->output_data.x->pixel_width = event.xconfigure.width;
10038 f->output_data.x->pixel_height = event.xconfigure.height;
10039
10040 /* What we have now is the position of Emacs's own window.
10041 Convert that to the position of the window manager window. */
10042 x_real_positions (f, &f->output_data.x->left_pos,
10043 &f->output_data.x->top_pos);
10044
10045#ifdef HAVE_X_I18N
10046 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10047 xic_set_statusarea (f);
10048#endif
10049
10050 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10051 {
10052 /* Since the WM decorations come below top_pos now,
10053 we must put them below top_pos in the future. */
10054 f->output_data.x->win_gravity = NorthWestGravity;
10055 x_wm_set_size_hint (f, (long) 0, 0);
10056 }
10057#ifdef USE_MOTIF
10058 /* Some window managers pass (0,0) as the location of
10059 the window, and the Motif event handler stores it
10060 in the emacs widget, which messes up Motif menus. */
10061 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10062 {
10063 event.xconfigure.x = f->output_data.x->widget->core.x;
10064 event.xconfigure.y = f->output_data.x->widget->core.y;
10065 }
10066#endif /* USE_MOTIF */
10067 }
10068 goto OTHER;
10069
10070 case ButtonPress:
10071 case ButtonRelease:
10072 {
10073 /* If we decide we want to generate an event to be seen
10074 by the rest of Emacs, we put it here. */
10075 struct input_event emacs_event;
10076 int tool_bar_p = 0;
10077
10078 emacs_event.kind = no_event;
10079 bzero (&compose_status, sizeof (compose_status));
10080
10081 if (dpyinfo->grabbed
10082 && last_mouse_frame
10083 && FRAME_LIVE_P (last_mouse_frame))
10084 f = last_mouse_frame;
10085 else
10086 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10087
10088 if (f)
10089 {
10090 /* Is this in the tool-bar? */
10091 if (WINDOWP (f->tool_bar_window)
10092 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10093 {
10094 Lisp_Object window;
10095 int p, x, y;
10096
10097 x = event.xbutton.x;
10098 y = event.xbutton.y;
10099
10100 /* Set x and y. */
10101 window = window_from_coordinates (f, x, y, &p, 1);
10102 if (EQ (window, f->tool_bar_window))
10103 {
10104 x_handle_tool_bar_click (f, &event.xbutton);
10105 tool_bar_p = 1;
10106 }
10107 }
10108
10109 if (!tool_bar_p)
10110 if (!dpyinfo->x_focus_frame
10111 || f == dpyinfo->x_focus_frame)
10112 construct_mouse_click (&emacs_event, &event, f);
10113 }
10114 else
10115 {
10116#ifndef USE_TOOLKIT_SCROLL_BARS
10117 struct scroll_bar *bar
10118 = x_window_to_scroll_bar (event.xbutton.window);
10119
10120 if (bar)
10121 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10122#endif /* not USE_TOOLKIT_SCROLL_BARS */
10123 }
10124
10125 if (event.type == ButtonPress)
10126 {
10127 dpyinfo->grabbed |= (1 << event.xbutton.button);
10128 last_mouse_frame = f;
10129 /* Ignore any mouse motion that happened
10130 before this event; any subsequent mouse-movement
10131 Emacs events should reflect only motion after
10132 the ButtonPress. */
10133 if (f != 0)
10134 f->mouse_moved = 0;
10135
10136 if (!tool_bar_p)
10137 last_tool_bar_item = -1;
10138 }
10139 else
10140 {
10141 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10142 }
10143
10144 if (numchars >= 1 && emacs_event.kind != no_event)
10145 {
10146 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10147 bufp++;
10148 count++;
10149 numchars--;
10150 }
10151
10152#ifdef USE_X_TOOLKIT
10153 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10154 /* For a down-event in the menu bar,
10155 don't pass it to Xt right now.
10156 Instead, save it away
10157 and we will pass it to Xt from kbd_buffer_get_event.
10158 That way, we can run some Lisp code first. */
10159 if (f && event.type == ButtonPress
10160 /* Verify the event is really within the menu bar
10161 and not just sent to it due to grabbing. */
10162 && event.xbutton.x >= 0
10163 && event.xbutton.x < f->output_data.x->pixel_width
10164 && event.xbutton.y >= 0
10165 && event.xbutton.y < f->output_data.x->menubar_height
10166 && event.xbutton.same_screen)
10167 {
10168 SET_SAVED_BUTTON_EVENT;
10169 XSETFRAME (last_mouse_press_frame, f);
10170 }
10171 else if (event.type == ButtonPress)
10172 {
10173 last_mouse_press_frame = Qnil;
10174 goto OTHER;
10175 }
10176
10177#ifdef USE_MOTIF /* This should do not harm for Lucid,
10178 but I am trying to be cautious. */
10179 else if (event.type == ButtonRelease)
10180 {
10181 if (!NILP (last_mouse_press_frame))
10182 {
10183 f = XFRAME (last_mouse_press_frame);
10184 if (f->output_data.x)
10185 SET_SAVED_BUTTON_EVENT;
10186 }
10187 else
10188 goto OTHER;
10189 }
10190#endif /* USE_MOTIF */
10191 else
10192 goto OTHER;
10193#endif /* USE_X_TOOLKIT */
10194 }
10195 break;
10196
10197 case CirculateNotify:
10198 goto OTHER;
10199
10200 case CirculateRequest:
10201 goto OTHER;
10202
10203 case VisibilityNotify:
10204 goto OTHER;
10205
10206 case MappingNotify:
10207 /* Someone has changed the keyboard mapping - update the
10208 local cache. */
10209 switch (event.xmapping.request)
10210 {
10211 case MappingModifier:
10212 x_find_modifier_meanings (dpyinfo);
10213 /* This is meant to fall through. */
10214 case MappingKeyboard:
10215 XRefreshKeyboardMapping (&event.xmapping);
10216 }
10217 goto OTHER;
10218
10219 default:
10220 OTHER:
10221#ifdef USE_X_TOOLKIT
10222 BLOCK_INPUT;
10223 XtDispatchEvent (&event);
10224 UNBLOCK_INPUT;
10225#endif /* USE_X_TOOLKIT */
10226 break;
10227 }
10228 }
10229 }
10230
10231 out:;
10232
10233 /* On some systems, an X bug causes Emacs to get no more events
10234 when the window is destroyed. Detect that. (1994.) */
10235 if (! event_found)
10236 {
10237 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10238 One XNOOP in 100 loops will make Emacs terminate.
10239 B. Bretthauer, 1994 */
10240 x_noop_count++;
10241 if (x_noop_count >= 100)
10242 {
10243 x_noop_count=0;
10244
10245 if (next_noop_dpyinfo == 0)
10246 next_noop_dpyinfo = x_display_list;
10247
10248 XNoOp (next_noop_dpyinfo->display);
10249
10250 /* Each time we get here, cycle through the displays now open. */
10251 next_noop_dpyinfo = next_noop_dpyinfo->next;
10252 }
10253 }
10254
10255 /* If the focus was just given to an auto-raising frame,
10256 raise it now. */
10257 /* ??? This ought to be able to handle more than one such frame. */
10258 if (pending_autoraise_frame)
10259 {
10260 x_raise_frame (pending_autoraise_frame);
10261 pending_autoraise_frame = 0;
10262 }
10263
10264 UNBLOCK_INPUT;
10265 --handling_signal;
10266 return count;
10267}
10268
10269
10270
10271\f
10272/***********************************************************************
10273 Text Cursor
10274 ***********************************************************************/
10275
10276/* Note if the text cursor of window W has been overwritten by a
10277 drawing operation that outputs N glyphs starting at HPOS in the
10278 line given by output_cursor.vpos. N < 0 means all the rest of the
10279 line after HPOS has been written. */
10280
10281static void
10282note_overwritten_text_cursor (w, hpos, n)
10283 struct window *w;
10284 int hpos, n;
10285{
10286 if (updated_area == TEXT_AREA
10287 && output_cursor.vpos == w->phys_cursor.vpos
10288 && hpos <= w->phys_cursor.hpos
10289 && (n < 0
10290 || hpos + n > w->phys_cursor.hpos))
10291 w->phys_cursor_on_p = 0;
10292}
10293
10294
10295/* Set clipping for output in glyph row ROW. W is the window in which
10296 we operate. GC is the graphics context to set clipping in.
10297 WHOLE_LINE_P non-zero means include the areas used for truncation
10298 mark display and alike in the clipping rectangle.
10299
10300 ROW may be a text row or, e.g., a mode line. Text rows must be
10301 clipped to the interior of the window dedicated to text display,
10302 mode lines must be clipped to the whole window. */
10303
10304static void
10305x_clip_to_row (w, row, gc, whole_line_p)
10306 struct window *w;
10307 struct glyph_row *row;
10308 GC gc;
10309 int whole_line_p;
10310{
10311 struct frame *f = XFRAME (WINDOW_FRAME (w));
10312 XRectangle clip_rect;
10313 int window_x, window_y, window_width, window_height;
10314
10315 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10316
10317 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10318 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10319 clip_rect.y = max (clip_rect.y, window_y);
10320 clip_rect.width = window_width;
10321 clip_rect.height = row->visible_height;
10322
10323 /* If clipping to the whole line, including trunc marks, extend
10324 the rectangle to the left and increase its width. */
10325 if (whole_line_p)
10326 {
10327 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10328 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10329 }
10330
10331 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10332}
10333
10334
10335/* Draw a hollow box cursor on window W in glyph row ROW. */
10336
10337static void
10338x_draw_hollow_cursor (w, row)
10339 struct window *w;
10340 struct glyph_row *row;
10341{
10342 struct frame *f = XFRAME (WINDOW_FRAME (w));
10343 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10344 Display *dpy = FRAME_X_DISPLAY (f);
10345 int x, y, wd, h;
10346 XGCValues xgcv;
10347 struct glyph *cursor_glyph;
10348 GC gc;
10349
10350 /* Compute frame-relative coordinates from window-relative
10351 coordinates. */
10352 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10353 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10354 + row->ascent - w->phys_cursor_ascent);
10355 h = row->height - 1;
10356
10357 /* Get the glyph the cursor is on. If we can't tell because
10358 the current matrix is invalid or such, give up. */
10359 cursor_glyph = get_phys_cursor_glyph (w);
10360 if (cursor_glyph == NULL)
10361 return;
10362
10363 /* Compute the width of the rectangle to draw. If on a stretch
10364 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10365 rectangle as wide as the glyph, but use a canonical character
10366 width instead. */
10367 wd = cursor_glyph->pixel_width - 1;
10368 if (cursor_glyph->type == STRETCH_GLYPH
10369 && !x_stretch_cursor_p)
10370 wd = min (CANON_X_UNIT (f), wd);
10371
10372 /* The foreground of cursor_gc is typically the same as the normal
10373 background color, which can cause the cursor box to be invisible. */
10374 xgcv.foreground = f->output_data.x->cursor_pixel;
10375 if (dpyinfo->scratch_cursor_gc)
10376 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10377 else
10378 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10379 GCForeground, &xgcv);
10380 gc = dpyinfo->scratch_cursor_gc;
10381
10382 /* Set clipping, draw the rectangle, and reset clipping again. */
10383 x_clip_to_row (w, row, gc, 0);
10384 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10385 XSetClipMask (dpy, gc, None);
10386}
10387
10388
10389/* Draw a bar cursor on window W in glyph row ROW.
10390
10391 Implementation note: One would like to draw a bar cursor with an
10392 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10393 Unfortunately, I didn't find a font yet that has this property set.
10394 --gerd. */
10395
10396static void
10397x_draw_bar_cursor (w, row, width)
10398 struct window *w;
10399 struct glyph_row *row;
10400 int width;
10401{
10402 struct frame *f = XFRAME (w->frame);
10403 struct glyph *cursor_glyph;
10404 GC gc;
10405 int x;
10406 unsigned long mask;
10407 XGCValues xgcv;
10408 Display *dpy;
10409 Window window;
10410
10411 /* If cursor is out of bounds, don't draw garbage. This can happen
10412 in mini-buffer windows when switching between echo area glyphs
10413 and mini-buffer. */
10414 cursor_glyph = get_phys_cursor_glyph (w);
10415 if (cursor_glyph == NULL)
10416 return;
10417
10418 /* If on an image, draw like a normal cursor. That's usually better
10419 visible than drawing a bar, esp. if the image is large so that
10420 the bar might not be in the window. */
10421 if (cursor_glyph->type == IMAGE_GLYPH)
10422 {
10423 struct glyph_row *row;
10424 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10425 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10426 }
10427 else
10428 {
10429 xgcv.background = f->output_data.x->cursor_pixel;
10430 xgcv.foreground = f->output_data.x->cursor_pixel;
10431 xgcv.graphics_exposures = 0;
10432 mask = GCForeground | GCBackground | GCGraphicsExposures;
10433 dpy = FRAME_X_DISPLAY (f);
10434 window = FRAME_X_WINDOW (f);
10435 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10436
10437 if (gc)
10438 XChangeGC (dpy, gc, mask, &xgcv);
10439 else
10440 {
10441 gc = XCreateGC (dpy, window, mask, &xgcv);
10442 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10443 }
10444
10445 if (width < 0)
10446 width = f->output_data.x->cursor_width;
10447
10448 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10449 x_clip_to_row (w, row, gc, 0);
10450 XFillRectangle (dpy, window, gc,
10451 x,
10452 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10453 min (cursor_glyph->pixel_width, width),
10454 row->height);
10455 XSetClipMask (dpy, gc, None);
10456 }
10457}
10458
10459
10460/* Clear the cursor of window W to background color, and mark the
10461 cursor as not shown. This is used when the text where the cursor
10462 is is about to be rewritten. */
10463
10464static void
10465x_clear_cursor (w)
10466 struct window *w;
10467{
10468 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10469 x_update_window_cursor (w, 0);
10470}
10471
10472
10473/* Draw the cursor glyph of window W in glyph row ROW. See the
10474 comment of x_draw_glyphs for the meaning of HL. */
10475
10476static void
10477x_draw_phys_cursor_glyph (w, row, hl)
10478 struct window *w;
10479 struct glyph_row *row;
10480 enum draw_glyphs_face hl;
10481{
10482 /* If cursor hpos is out of bounds, don't draw garbage. This can
10483 happen in mini-buffer windows when switching between echo area
10484 glyphs and mini-buffer. */
10485 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10486 {
10487 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10488 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10489 hl, 0, 0, 0);
10490
10491 /* When we erase the cursor, and ROW is overlapped by other
10492 rows, make sure that these overlapping parts of other rows
10493 are redrawn. */
10494 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10495 {
10496 if (row > w->current_matrix->rows
10497 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10498 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10499
10500 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10501 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10502 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10503 }
10504 }
10505}
10506
10507
10508/* Erase the image of a cursor of window W from the screen. */
10509
10510static void
10511x_erase_phys_cursor (w)
10512 struct window *w;
10513{
10514 struct frame *f = XFRAME (w->frame);
10515 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10516 int hpos = w->phys_cursor.hpos;
10517 int vpos = w->phys_cursor.vpos;
10518 int mouse_face_here_p = 0;
10519 struct glyph_matrix *active_glyphs = w->current_matrix;
10520 struct glyph_row *cursor_row;
10521 struct glyph *cursor_glyph;
10522 enum draw_glyphs_face hl;
10523
10524 /* No cursor displayed or row invalidated => nothing to do on the
10525 screen. */
10526 if (w->phys_cursor_type == NO_CURSOR)
10527 goto mark_cursor_off;
10528
10529 /* VPOS >= active_glyphs->nrows means that window has been resized.
10530 Don't bother to erase the cursor. */
10531 if (vpos >= active_glyphs->nrows)
10532 goto mark_cursor_off;
10533
10534 /* If row containing cursor is marked invalid, there is nothing we
10535 can do. */
10536 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10537 if (!cursor_row->enabled_p)
10538 goto mark_cursor_off;
10539
10540 /* This can happen when the new row is shorter than the old one.
10541 In this case, either x_draw_glyphs or clear_end_of_line
10542 should have cleared the cursor. Note that we wouldn't be
10543 able to erase the cursor in this case because we don't have a
10544 cursor glyph at hand. */
10545 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10546 goto mark_cursor_off;
10547
10548 /* If the cursor is in the mouse face area, redisplay that when
10549 we clear the cursor. */
10550 if (! NILP (dpyinfo->mouse_face_window)
10551 && w == XWINDOW (dpyinfo->mouse_face_window)
10552 && (vpos > dpyinfo->mouse_face_beg_row
10553 || (vpos == dpyinfo->mouse_face_beg_row
10554 && hpos >= dpyinfo->mouse_face_beg_col))
10555 && (vpos < dpyinfo->mouse_face_end_row
10556 || (vpos == dpyinfo->mouse_face_end_row
10557 && hpos < dpyinfo->mouse_face_end_col))
10558 /* Don't redraw the cursor's spot in mouse face if it is at the
10559 end of a line (on a newline). The cursor appears there, but
10560 mouse highlighting does not. */
10561 && cursor_row->used[TEXT_AREA] > hpos)
10562 mouse_face_here_p = 1;
10563
10564 /* Maybe clear the display under the cursor. */
10565 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10566 {
10567 int x;
10568 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10569
10570 cursor_glyph = get_phys_cursor_glyph (w);
10571 if (cursor_glyph == NULL)
10572 goto mark_cursor_off;
10573
10574 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10575
10576 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10577 x,
10578 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10579 cursor_row->y)),
10580 cursor_glyph->pixel_width,
10581 cursor_row->visible_height,
10582 False);
10583 }
10584
10585 /* Erase the cursor by redrawing the character underneath it. */
10586 if (mouse_face_here_p)
10587 hl = DRAW_MOUSE_FACE;
10588 else if (cursor_row->inverse_p)
10589 hl = DRAW_INVERSE_VIDEO;
10590 else
10591 hl = DRAW_NORMAL_TEXT;
10592 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10593
10594 mark_cursor_off:
10595 w->phys_cursor_on_p = 0;
10596 w->phys_cursor_type = NO_CURSOR;
10597}
10598
10599
10600/* Display or clear cursor of window W. If ON is zero, clear the
10601 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10602 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10603
10604void
10605x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10606 struct window *w;
10607 int on, hpos, vpos, x, y;
10608{
10609 struct frame *f = XFRAME (w->frame);
10610 int new_cursor_type;
10611 int new_cursor_width;
10612 struct glyph_matrix *current_glyphs;
10613 struct glyph_row *glyph_row;
10614 struct glyph *glyph;
10615
10616 /* This is pointless on invisible frames, and dangerous on garbaged
10617 windows and frames; in the latter case, the frame or window may
10618 be in the midst of changing its size, and x and y may be off the
10619 window. */
10620 if (! FRAME_VISIBLE_P (f)
10621 || FRAME_GARBAGED_P (f)
10622 || vpos >= w->current_matrix->nrows
10623 || hpos >= w->current_matrix->matrix_w)
10624 return;
10625
10626 /* If cursor is off and we want it off, return quickly. */
10627 if (!on && !w->phys_cursor_on_p)
10628 return;
10629
10630 current_glyphs = w->current_matrix;
10631 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10632 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10633
10634 /* If cursor row is not enabled, we don't really know where to
10635 display the cursor. */
10636 if (!glyph_row->enabled_p)
10637 {
10638 w->phys_cursor_on_p = 0;
10639 return;
10640 }
10641
10642 xassert (interrupt_input_blocked);
10643
10644 /* Set new_cursor_type to the cursor we want to be displayed. In a
10645 mini-buffer window, we want the cursor only to appear if we are
10646 reading input from this window. For the selected window, we want
10647 the cursor type given by the frame parameter. If explicitly
10648 marked off, draw no cursor. In all other cases, we want a hollow
10649 box cursor. */
10650 new_cursor_width = -1;
10651 if (cursor_in_echo_area
10652 && FRAME_HAS_MINIBUF_P (f)
10653 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10654 {
10655 if (w == XWINDOW (echo_area_window))
10656 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10657 else
10658 new_cursor_type = HOLLOW_BOX_CURSOR;
10659 }
10660 else
10661 {
10662 if (w != XWINDOW (selected_window)
10663 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10664 {
10665 extern int cursor_in_non_selected_windows;
10666
10667 if (MINI_WINDOW_P (w)
10668 || !cursor_in_non_selected_windows
10669 || NILP (XBUFFER (w->buffer)->cursor_type))
10670 new_cursor_type = NO_CURSOR;
10671 else
10672 new_cursor_type = HOLLOW_BOX_CURSOR;
10673 }
10674 else if (w->cursor_off_p)
10675 new_cursor_type = NO_CURSOR;
10676 else
10677 {
10678 struct buffer *b = XBUFFER (w->buffer);
10679
10680 if (EQ (b->cursor_type, Qt))
10681 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10682 else
10683 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10684 &new_cursor_width);
10685 }
10686 }
10687
10688 /* If cursor is currently being shown and we don't want it to be or
10689 it is in the wrong place, or the cursor type is not what we want,
10690 erase it. */
10691 if (w->phys_cursor_on_p
10692 && (!on
10693 || w->phys_cursor.x != x
10694 || w->phys_cursor.y != y
10695 || new_cursor_type != w->phys_cursor_type))
10696 x_erase_phys_cursor (w);
10697
10698 /* If the cursor is now invisible and we want it to be visible,
10699 display it. */
10700 if (on && !w->phys_cursor_on_p)
10701 {
10702 w->phys_cursor_ascent = glyph_row->ascent;
10703 w->phys_cursor_height = glyph_row->height;
10704
10705 /* Set phys_cursor_.* before x_draw_.* is called because some
10706 of them may need the information. */
10707 w->phys_cursor.x = x;
10708 w->phys_cursor.y = glyph_row->y;
10709 w->phys_cursor.hpos = hpos;
10710 w->phys_cursor.vpos = vpos;
10711 w->phys_cursor_type = new_cursor_type;
10712 w->phys_cursor_on_p = 1;
10713
10714 switch (new_cursor_type)
10715 {
10716 case HOLLOW_BOX_CURSOR:
10717 x_draw_hollow_cursor (w, glyph_row);
10718 break;
10719
10720 case FILLED_BOX_CURSOR:
10721 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10722 break;
10723
10724 case BAR_CURSOR:
10725 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10726 break;
10727
10728 case NO_CURSOR:
10729 break;
10730
10731 default:
10732 abort ();
10733 }
10734
10735#ifdef HAVE_X_I18N
10736 if (w == XWINDOW (f->selected_window))
10737 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10738 xic_set_preeditarea (w, x, y);
10739#endif
10740 }
10741
10742#ifndef XFlush
10743 if (updating_frame != f)
10744 XFlush (FRAME_X_DISPLAY (f));
10745#endif
10746}
10747
10748
10749/* Display the cursor on window W, or clear it. X and Y are window
10750 relative pixel coordinates. HPOS and VPOS are glyph matrix
10751 positions. If W is not the selected window, display a hollow
10752 cursor. ON non-zero means display the cursor at X, Y which
10753 correspond to HPOS, VPOS, otherwise it is cleared. */
10754
10755void
10756x_display_cursor (w, on, hpos, vpos, x, y)
10757 struct window *w;
10758 int on, hpos, vpos, x, y;
10759{
10760 BLOCK_INPUT;
10761 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10762 UNBLOCK_INPUT;
10763}
10764
10765
10766/* Display the cursor on window W, or clear it, according to ON_P.
10767 Don't change the cursor's position. */
10768
10769void
10770x_update_cursor (f, on_p)
10771 struct frame *f;
10772{
10773 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10774}
10775
10776
10777/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10778 in the window tree rooted at W. */
10779
10780static void
10781x_update_cursor_in_window_tree (w, on_p)
10782 struct window *w;
10783 int on_p;
10784{
10785 while (w)
10786 {
10787 if (!NILP (w->hchild))
10788 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10789 else if (!NILP (w->vchild))
10790 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10791 else
10792 x_update_window_cursor (w, on_p);
10793
10794 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10795 }
10796}
10797
10798
10799/* Switch the display of W's cursor on or off, according to the value
10800 of ON. */
10801
10802static void
10803x_update_window_cursor (w, on)
10804 struct window *w;
10805 int on;
10806{
10807 /* Don't update cursor in windows whose frame is in the process
10808 of being deleted. */
10809 if (w->current_matrix)
10810 {
10811 BLOCK_INPUT;
10812 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10813 w->phys_cursor.x, w->phys_cursor.y);
10814 UNBLOCK_INPUT;
10815 }
10816}
10817
10818
10819
10820\f
10821/* Icons. */
10822
10823/* Refresh bitmap kitchen sink icon for frame F
10824 when we get an expose event for it. */
10825
10826void
10827refreshicon (f)
10828 struct frame *f;
10829{
10830 /* Normally, the window manager handles this function. */
10831}
10832
10833/* Make the x-window of frame F use the gnu icon bitmap. */
10834
10835int
10836x_bitmap_icon (f, file)
10837 struct frame *f;
10838 Lisp_Object file;
10839{
10840 int bitmap_id;
10841
10842 if (FRAME_X_WINDOW (f) == 0)
10843 return 1;
10844
10845 /* Free up our existing icon bitmap if any. */
10846 if (f->output_data.x->icon_bitmap > 0)
10847 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10848 f->output_data.x->icon_bitmap = 0;
10849
10850 if (STRINGP (file))
10851 bitmap_id = x_create_bitmap_from_file (f, file);
10852 else
10853 {
10854 /* Create the GNU bitmap if necessary. */
10855 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10856 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10857 = x_create_bitmap_from_data (f, gnu_bits,
10858 gnu_width, gnu_height);
10859
10860 /* The first time we create the GNU bitmap,
10861 this increments the ref-count one extra time.
10862 As a result, the GNU bitmap is never freed.
10863 That way, we don't have to worry about allocating it again. */
10864 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10865
10866 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10867 }
10868
10869 x_wm_set_icon_pixmap (f, bitmap_id);
10870 f->output_data.x->icon_bitmap = bitmap_id;
10871
10872 return 0;
10873}
10874
10875
10876/* Make the x-window of frame F use a rectangle with text.
10877 Use ICON_NAME as the text. */
10878
10879int
10880x_text_icon (f, icon_name)
10881 struct frame *f;
10882 char *icon_name;
10883{
10884 if (FRAME_X_WINDOW (f) == 0)
10885 return 1;
10886
10887#ifdef HAVE_X11R4
10888 {
10889 XTextProperty text;
10890 text.value = (unsigned char *) icon_name;
10891 text.encoding = XA_STRING;
10892 text.format = 8;
10893 text.nitems = strlen (icon_name);
10894#ifdef USE_X_TOOLKIT
10895 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10896 &text);
10897#else /* not USE_X_TOOLKIT */
10898 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10899#endif /* not USE_X_TOOLKIT */
10900 }
10901#else /* not HAVE_X11R4 */
10902 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10903#endif /* not HAVE_X11R4 */
10904
10905 if (f->output_data.x->icon_bitmap > 0)
10906 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10907 f->output_data.x->icon_bitmap = 0;
10908 x_wm_set_icon_pixmap (f, 0);
10909
10910 return 0;
10911}
10912\f
10913#define X_ERROR_MESSAGE_SIZE 200
10914
10915/* If non-nil, this should be a string.
10916 It means catch X errors and store the error message in this string. */
10917
10918static Lisp_Object x_error_message_string;
10919
10920/* An X error handler which stores the error message in
10921 x_error_message_string. This is called from x_error_handler if
10922 x_catch_errors is in effect. */
10923
10924static void
10925x_error_catcher (display, error)
10926 Display *display;
10927 XErrorEvent *error;
10928{
10929 XGetErrorText (display, error->error_code,
10930 XSTRING (x_error_message_string)->data,
10931 X_ERROR_MESSAGE_SIZE);
10932}
10933
10934/* Begin trapping X errors for display DPY. Actually we trap X errors
10935 for all displays, but DPY should be the display you are actually
10936 operating on.
10937
10938 After calling this function, X protocol errors no longer cause
10939 Emacs to exit; instead, they are recorded in the string
10940 stored in x_error_message_string.
10941
10942 Calling x_check_errors signals an Emacs error if an X error has
10943 occurred since the last call to x_catch_errors or x_check_errors.
10944
10945 Calling x_uncatch_errors resumes the normal error handling. */
10946
10947void x_check_errors ();
10948static Lisp_Object x_catch_errors_unwind ();
10949
10950int
10951x_catch_errors (dpy)
10952 Display *dpy;
10953{
10954 int count = specpdl_ptr - specpdl;
10955
10956 /* Make sure any errors from previous requests have been dealt with. */
10957 XSync (dpy, False);
10958
10959 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10960
10961 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10962 XSTRING (x_error_message_string)->data[0] = 0;
10963
10964 return count;
10965}
10966
10967/* Unbind the binding that we made to check for X errors. */
10968
10969static Lisp_Object
10970x_catch_errors_unwind (old_val)
10971 Lisp_Object old_val;
10972{
10973 x_error_message_string = old_val;
10974 return Qnil;
10975}
10976
10977/* If any X protocol errors have arrived since the last call to
10978 x_catch_errors or x_check_errors, signal an Emacs error using
10979 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10980
10981void
10982x_check_errors (dpy, format)
10983 Display *dpy;
10984 char *format;
10985{
10986 /* Make sure to catch any errors incurred so far. */
10987 XSync (dpy, False);
10988
10989 if (XSTRING (x_error_message_string)->data[0])
10990 error (format, XSTRING (x_error_message_string)->data);
10991}
10992
10993/* Nonzero if we had any X protocol errors
10994 since we did x_catch_errors on DPY. */
10995
10996int
10997x_had_errors_p (dpy)
10998 Display *dpy;
10999{
11000 /* Make sure to catch any errors incurred so far. */
11001 XSync (dpy, False);
11002
11003 return XSTRING (x_error_message_string)->data[0] != 0;
11004}
11005
11006/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11007
11008void
11009x_clear_errors (dpy)
11010 Display *dpy;
11011{
11012 XSTRING (x_error_message_string)->data[0] = 0;
11013}
11014
11015/* Stop catching X protocol errors and let them make Emacs die.
11016 DPY should be the display that was passed to x_catch_errors.
11017 COUNT should be the value that was returned by
11018 the corresponding call to x_catch_errors. */
11019
11020void
11021x_uncatch_errors (dpy, count)
11022 Display *dpy;
11023 int count;
11024{
11025 unbind_to (count, Qnil);
11026}
11027
11028#if 0
11029static unsigned int x_wire_count;
11030x_trace_wire ()
11031{
11032 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11033}
11034#endif /* ! 0 */
11035
11036\f
11037/* Handle SIGPIPE, which can happen when the connection to a server
11038 simply goes away. SIGPIPE is handled by x_connection_signal.
11039 Don't need to do anything, because the write which caused the
11040 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11041 which will do the appropriate cleanup for us. */
11042
11043static SIGTYPE
11044x_connection_signal (signalnum) /* If we don't have an argument, */
11045 int signalnum; /* some compilers complain in signal calls. */
11046{
11047#ifdef USG
11048 /* USG systems forget handlers when they are used;
11049 must reestablish each time */
11050 signal (signalnum, x_connection_signal);
11051#endif /* USG */
11052}
11053\f
11054/* Handling X errors. */
11055
11056/* Handle the loss of connection to display DISPLAY. */
11057
11058static SIGTYPE
11059x_connection_closed (display, error_message)
11060 Display *display;
11061 char *error_message;
11062{
11063 struct x_display_info *dpyinfo = x_display_info_for_display (display);
11064 Lisp_Object frame, tail;
11065
11066 /* Indicate that this display is dead. */
11067
11068#if 0 /* Closing the display caused a bus error on OpenWindows. */
11069#ifdef USE_X_TOOLKIT
11070 XtCloseDisplay (display);
11071#endif
11072#endif
11073
11074 if (dpyinfo)
11075 dpyinfo->display = 0;
11076
11077 /* First delete frames whose mini-buffers are on frames
11078 that are on the dead display. */
11079 FOR_EACH_FRAME (tail, frame)
11080 {
11081 Lisp_Object minibuf_frame;
11082 minibuf_frame
11083 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11084 if (FRAME_X_P (XFRAME (frame))
11085 && FRAME_X_P (XFRAME (minibuf_frame))
11086 && ! EQ (frame, minibuf_frame)
11087 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11088 Fdelete_frame (frame, Qt);
11089 }
11090
11091 /* Now delete all remaining frames on the dead display.
11092 We are now sure none of these is used as the mini-buffer
11093 for another frame that we need to delete. */
11094 FOR_EACH_FRAME (tail, frame)
11095 if (FRAME_X_P (XFRAME (frame))
11096 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11097 {
11098 /* Set this to t so that Fdelete_frame won't get confused
11099 trying to find a replacement. */
11100 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11101 Fdelete_frame (frame, Qt);
11102 }
11103
11104 if (dpyinfo)
11105 x_delete_display (dpyinfo);
11106
11107 if (x_display_list == 0)
11108 {
11109 fprintf (stderr, "%s\n", error_message);
11110 shut_down_emacs (0, 0, Qnil);
11111 exit (70);
11112 }
11113
11114 /* Ordinary stack unwind doesn't deal with these. */
11115#ifdef SIGIO
11116 sigunblock (sigmask (SIGIO));
11117#endif
11118 sigunblock (sigmask (SIGALRM));
11119 TOTALLY_UNBLOCK_INPUT;
11120
11121 clear_waiting_for_input ();
11122 error ("%s", error_message);
11123}
11124
11125/* This is the usual handler for X protocol errors.
11126 It kills all frames on the display that we got the error for.
11127 If that was the only one, it prints an error message and kills Emacs. */
11128
11129static void
11130x_error_quitter (display, error)
11131 Display *display;
11132 XErrorEvent *error;
11133{
11134 char buf[256], buf1[356];
11135
11136 /* Note that there is no real way portable across R3/R4 to get the
11137 original error handler. */
11138
11139 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11140 sprintf (buf1, "X protocol error: %s on protocol request %d",
11141 buf, error->request_code);
11142 x_connection_closed (display, buf1);
11143}
11144
11145/* This is the first-level handler for X protocol errors.
11146 It calls x_error_quitter or x_error_catcher. */
11147
11148static int
11149x_error_handler (display, error)
11150 Display *display;
11151 XErrorEvent *error;
11152{
11153 if (! NILP (x_error_message_string))
11154 x_error_catcher (display, error);
11155 else
11156 x_error_quitter (display, error);
11157 return 0;
11158}
11159
11160/* This is the handler for X IO errors, always.
11161 It kills all frames on the display that we lost touch with.
11162 If that was the only one, it prints an error message and kills Emacs. */
11163
11164static int
11165x_io_error_quitter (display)
11166 Display *display;
11167{
11168 char buf[256];
11169
11170 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11171 x_connection_closed (display, buf);
11172 return 0;
11173}
11174\f
11175/* Changing the font of the frame. */
11176
11177/* Give frame F the font named FONTNAME as its default font, and
11178 return the full name of that font. FONTNAME may be a wildcard
11179 pattern; in that case, we choose some font that fits the pattern.
11180 The return value shows which font we chose. */
11181
11182Lisp_Object
11183x_new_font (f, fontname)
11184 struct frame *f;
11185 register char *fontname;
11186{
11187 struct font_info *fontp
11188 = FS_LOAD_FONT (f, 0, fontname, -1);
11189
11190 if (!fontp)
11191 return Qnil;
11192
11193 f->output_data.x->font = (XFontStruct *) (fontp->font);
11194 f->output_data.x->baseline_offset = fontp->baseline_offset;
11195 f->output_data.x->fontset = -1;
11196
11197 /* Compute the scroll bar width in character columns. */
11198 if (f->scroll_bar_pixel_width > 0)
11199 {
11200 int wid = FONT_WIDTH (f->output_data.x->font);
11201 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11202 }
11203 else
11204 {
11205 int wid = FONT_WIDTH (f->output_data.x->font);
11206 f->scroll_bar_cols = (14 + wid - 1) / wid;
11207 }
11208
11209 /* Now make the frame display the given font. */
11210 if (FRAME_X_WINDOW (f) != 0)
11211 {
11212 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11213 f->output_data.x->font->fid);
11214 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11215 f->output_data.x->font->fid);
11216 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11217 f->output_data.x->font->fid);
11218
11219 frame_update_line_height (f);
11220 x_set_window_size (f, 0, f->width, f->height);
11221 }
11222 else
11223 /* If we are setting a new frame's font for the first time,
11224 there are no faces yet, so this font's height is the line height. */
11225 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11226
11227 return build_string (fontp->full_name);
11228}
11229
11230/* Give frame F the fontset named FONTSETNAME as its default font, and
11231 return the full name of that fontset. FONTSETNAME may be a wildcard
11232 pattern; in that case, we choose some fontset that fits the pattern.
11233 The return value shows which fontset we chose. */
11234
11235Lisp_Object
11236x_new_fontset (f, fontsetname)
11237 struct frame *f;
11238 char *fontsetname;
11239{
11240 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11241 Lisp_Object result;
11242
11243 if (fontset < 0)
11244 return Qnil;
11245
11246 if (f->output_data.x->fontset == fontset)
11247 /* This fontset is already set in frame F. There's nothing more
11248 to do. */
11249 return fontset_name (fontset);
11250
11251 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11252
11253 if (!STRINGP (result))
11254 /* Can't load ASCII font. */
11255 return Qnil;
11256
11257 /* Since x_new_font doesn't update any fontset information, do it now. */
11258 f->output_data.x->fontset = fontset;
11259
11260#ifdef HAVE_X_I18N
11261 if (FRAME_XIC (f)
11262 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11263 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11264#endif
11265
11266 return build_string (fontsetname);
11267}
11268
11269\f
11270/***********************************************************************
11271 X Input Methods
11272 ***********************************************************************/
11273
11274#ifdef HAVE_X_I18N
11275
11276#ifdef HAVE_X11R6
11277
11278/* XIM destroy callback function, which is called whenever the
11279 connection to input method XIM dies. CLIENT_DATA contains a
11280 pointer to the x_display_info structure corresponding to XIM. */
11281
11282static void
11283xim_destroy_callback (xim, client_data, call_data)
11284 XIM xim;
11285 XPointer client_data;
11286 XPointer call_data;
11287{
11288 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11289 Lisp_Object frame, tail;
11290
11291 BLOCK_INPUT;
11292
11293 /* No need to call XDestroyIC.. */
11294 FOR_EACH_FRAME (tail, frame)
11295 {
11296 struct frame *f = XFRAME (frame);
11297 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11298 {
11299 FRAME_XIC (f) = NULL;
11300 if (FRAME_XIC_FONTSET (f))
11301 {
11302 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11303 FRAME_XIC_FONTSET (f) = NULL;
11304 }
11305 }
11306 }
11307
11308 /* No need to call XCloseIM. */
11309 dpyinfo->xim = NULL;
11310 XFree (dpyinfo->xim_styles);
11311 UNBLOCK_INPUT;
11312}
11313
11314#endif /* HAVE_X11R6 */
11315
11316/* Open the connection to the XIM server on display DPYINFO.
11317 RESOURCE_NAME is the resource name Emacs uses. */
11318
11319static void
11320xim_open_dpy (dpyinfo, resource_name)
11321 struct x_display_info *dpyinfo;
11322 char *resource_name;
11323{
11324#ifdef USE_XIM
11325 XIM xim;
11326
11327 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11328 dpyinfo->xim = xim;
11329
11330 if (xim)
11331 {
11332#ifdef HAVE_X11R6
11333 XIMCallback destroy;
11334#endif
11335
11336 /* Get supported styles and XIM values. */
11337 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11338
11339#ifdef HAVE_X11R6
11340 destroy.callback = xim_destroy_callback;
11341 destroy.client_data = (XPointer)dpyinfo;
11342 /* This isn't prptotyped in OSF 5.0. */
11343 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11344#endif
11345 }
11346
11347#else /* not USE_XIM */
11348 dpyinfo->xim = NULL;
11349#endif /* not USE_XIM */
11350}
11351
11352
11353#ifdef HAVE_X11R6_XIM
11354
11355struct xim_inst_t
11356{
11357 struct x_display_info *dpyinfo;
11358 char *resource_name;
11359};
11360
11361/* XIM instantiate callback function, which is called whenever an XIM
11362 server is available. DISPLAY is teh display of the XIM.
11363 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11364 when the callback was registered. */
11365
11366static void
11367xim_instantiate_callback (display, client_data, call_data)
11368 Display *display;
11369 XPointer client_data;
11370 XPointer call_data;
11371{
11372 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11373 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11374
11375 /* We don't support multiple XIM connections. */
11376 if (dpyinfo->xim)
11377 return;
11378
11379 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11380
11381 /* Create XIC for the existing frames on the same display, as long
11382 as they have no XIC. */
11383 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11384 {
11385 Lisp_Object tail, frame;
11386
11387 BLOCK_INPUT;
11388 FOR_EACH_FRAME (tail, frame)
11389 {
11390 struct frame *f = XFRAME (frame);
11391
11392 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11393 if (FRAME_XIC (f) == NULL)
11394 {
11395 create_frame_xic (f);
11396 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11397 xic_set_statusarea (f);
11398 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11399 {
11400 struct window *w = XWINDOW (f->selected_window);
11401 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11402 }
11403 }
11404 }
11405
11406 UNBLOCK_INPUT;
11407 }
11408}
11409
11410#endif /* HAVE_X11R6_XIM */
11411
11412
11413/* Open a connection to the XIM server on display DPYINFO.
11414 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11415 connection only at the first time. On X11R6, open the connection
11416 in the XIM instantiate callback function. */
11417
11418static void
11419xim_initialize (dpyinfo, resource_name)
11420 struct x_display_info *dpyinfo;
11421 char *resource_name;
11422{
11423#ifdef USE_XIM
11424#ifdef HAVE_X11R6_XIM
11425 struct xim_inst_t *xim_inst;
11426 int len;
11427
11428 dpyinfo->xim = NULL;
11429 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11430 xim_inst->dpyinfo = dpyinfo;
11431 len = strlen (resource_name);
11432 xim_inst->resource_name = (char *) xmalloc (len + 1);
11433 bcopy (resource_name, xim_inst->resource_name, len + 1);
11434 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11435 resource_name, EMACS_CLASS,
11436 xim_instantiate_callback,
11437 /* Fixme: This is XPointer in
11438 XFree86 but (XPointer *) on
11439 Tru64, at least. */
11440 (XPointer) xim_inst);
11441#else /* not HAVE_X11R6_XIM */
11442 dpyinfo->xim = NULL;
11443 xim_open_dpy (dpyinfo, resource_name);
11444#endif /* not HAVE_X11R6_XIM */
11445
11446#else /* not USE_XIM */
11447 dpyinfo->xim = NULL;
11448#endif /* not USE_XIM */
11449}
11450
11451
11452/* Close the connection to the XIM server on display DPYINFO. */
11453
11454static void
11455xim_close_dpy (dpyinfo)
11456 struct x_display_info *dpyinfo;
11457{
11458#ifdef USE_XIM
11459#ifdef HAVE_X11R6_XIM
11460 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11461 NULL, EMACS_CLASS,
11462 xim_instantiate_callback, NULL);
11463#endif /* not HAVE_X11R6_XIM */
11464 XCloseIM (dpyinfo->xim);
11465 dpyinfo->xim = NULL;
11466 XFree (dpyinfo->xim_styles);
11467#endif /* USE_XIM */
11468}
11469
11470#endif /* not HAVE_X11R6_XIM */
11471
11472
11473\f
11474/* Calculate the absolute position in frame F
11475 from its current recorded position values and gravity. */
11476
11477void
11478x_calc_absolute_position (f)
11479 struct frame *f;
11480{
11481 Window child;
11482 int win_x = 0, win_y = 0;
11483 int flags = f->output_data.x->size_hint_flags;
11484 int this_window;
11485
11486 /* We have nothing to do if the current position
11487 is already for the top-left corner. */
11488 if (! ((flags & XNegative) || (flags & YNegative)))
11489 return;
11490
11491#ifdef USE_X_TOOLKIT
11492 this_window = XtWindow (f->output_data.x->widget);
11493#else
11494 this_window = FRAME_X_WINDOW (f);
11495#endif
11496
11497 /* Find the position of the outside upper-left corner of
11498 the inner window, with respect to the outer window.
11499 But do this only if we will need the results. */
11500 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11501 {
11502 int count;
11503
11504 BLOCK_INPUT;
11505 count = x_catch_errors (FRAME_X_DISPLAY (f));
11506 while (1)
11507 {
11508 x_clear_errors (FRAME_X_DISPLAY (f));
11509 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11510
11511 /* From-window, to-window. */
11512 this_window,
11513 f->output_data.x->parent_desc,
11514
11515 /* From-position, to-position. */
11516 0, 0, &win_x, &win_y,
11517
11518 /* Child of win. */
11519 &child);
11520 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11521 {
11522 Window newroot, newparent = 0xdeadbeef;
11523 Window *newchildren;
11524 unsigned int nchildren;
11525
11526 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11527 &newparent, &newchildren, &nchildren))
11528 break;
11529
11530 XFree ((char *) newchildren);
11531
11532 f->output_data.x->parent_desc = newparent;
11533 }
11534 else
11535 break;
11536 }
11537
11538 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11539 UNBLOCK_INPUT;
11540 }
11541
11542 /* Treat negative positions as relative to the leftmost bottommost
11543 position that fits on the screen. */
11544 if (flags & XNegative)
11545 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11546 - 2 * f->output_data.x->border_width - win_x
11547 - PIXEL_WIDTH (f)
11548 + f->output_data.x->left_pos);
11549
11550 if (flags & YNegative)
11551 {
11552 int menubar_height = 0;
11553
11554#ifdef USE_X_TOOLKIT
11555 if (f->output_data.x->menubar_widget)
11556 menubar_height
11557 = (f->output_data.x->menubar_widget->core.height
11558 + f->output_data.x->menubar_widget->core.border_width);
11559#endif
11560
11561 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11562 - 2 * f->output_data.x->border_width
11563 - win_y
11564 - PIXEL_HEIGHT (f)
11565 - menubar_height
11566 + f->output_data.x->top_pos);
11567 }
11568
11569 /* The left_pos and top_pos
11570 are now relative to the top and left screen edges,
11571 so the flags should correspond. */
11572 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11573}
11574
11575/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11576 to really change the position, and 0 when calling from
11577 x_make_frame_visible (in that case, XOFF and YOFF are the current
11578 position values). It is -1 when calling from x_set_frame_parameters,
11579 which means, do adjust for borders but don't change the gravity. */
11580
11581void
11582x_set_offset (f, xoff, yoff, change_gravity)
11583 struct frame *f;
11584 register int xoff, yoff;
11585 int change_gravity;
11586{
11587 int modified_top, modified_left;
11588
11589 if (change_gravity > 0)
11590 {
11591 f->output_data.x->top_pos = yoff;
11592 f->output_data.x->left_pos = xoff;
11593 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11594 if (xoff < 0)
11595 f->output_data.x->size_hint_flags |= XNegative;
11596 if (yoff < 0)
11597 f->output_data.x->size_hint_flags |= YNegative;
11598 f->output_data.x->win_gravity = NorthWestGravity;
11599 }
11600 x_calc_absolute_position (f);
11601
11602 BLOCK_INPUT;
11603 x_wm_set_size_hint (f, (long) 0, 0);
11604
11605 modified_left = f->output_data.x->left_pos;
11606 modified_top = f->output_data.x->top_pos;
11607#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11608 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11609 /* It is a mystery why we need to add the border_width here
11610 when the frame is already visible, but experiment says we do. */
11611 if (change_gravity != 0)
11612 {
11613 modified_left += f->output_data.x->border_width;
11614 modified_top += f->output_data.x->border_width;
11615 }
11616#endif
11617
11618#ifdef USE_X_TOOLKIT
11619 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11620 modified_left, modified_top);
11621#else /* not USE_X_TOOLKIT */
11622 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11623 modified_left, modified_top);
11624#endif /* not USE_X_TOOLKIT */
11625 UNBLOCK_INPUT;
11626}
11627
11628/* Call this to change the size of frame F's x-window.
11629 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11630 for this size change and subsequent size changes.
11631 Otherwise we leave the window gravity unchanged. */
11632
11633void
11634x_set_window_size (f, change_gravity, cols, rows)
11635 struct frame *f;
11636 int change_gravity;
11637 int cols, rows;
11638{
11639#ifndef USE_X_TOOLKIT
11640 int pixelwidth, pixelheight;
11641#endif
11642
11643 BLOCK_INPUT;
11644
11645#ifdef USE_X_TOOLKIT
11646 {
11647 /* The x and y position of the widget is clobbered by the
11648 call to XtSetValues within EmacsFrameSetCharSize.
11649 This is a real kludge, but I don't understand Xt so I can't
11650 figure out a correct fix. Can anyone else tell me? -- rms. */
11651 int xpos = f->output_data.x->widget->core.x;
11652 int ypos = f->output_data.x->widget->core.y;
11653 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11654 f->output_data.x->widget->core.x = xpos;
11655 f->output_data.x->widget->core.y = ypos;
11656 }
11657
11658#else /* not USE_X_TOOLKIT */
11659
11660 check_frame_size (f, &rows, &cols);
11661 f->output_data.x->vertical_scroll_bar_extra
11662 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11663 ? 0
11664 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11665 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11666 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11667 f->output_data.x->flags_areas_extra
11668 = FRAME_FLAGS_AREA_WIDTH (f);
11669 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11670 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11671
11672 f->output_data.x->win_gravity = NorthWestGravity;
11673 x_wm_set_size_hint (f, (long) 0, 0);
11674
11675 XSync (FRAME_X_DISPLAY (f), False);
11676 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11677 pixelwidth, pixelheight);
11678
11679 /* Now, strictly speaking, we can't be sure that this is accurate,
11680 but the window manager will get around to dealing with the size
11681 change request eventually, and we'll hear how it went when the
11682 ConfigureNotify event gets here.
11683
11684 We could just not bother storing any of this information here,
11685 and let the ConfigureNotify event set everything up, but that
11686 might be kind of confusing to the Lisp code, since size changes
11687 wouldn't be reported in the frame parameters until some random
11688 point in the future when the ConfigureNotify event arrives.
11689
11690 We pass 1 for DELAY since we can't run Lisp code inside of
11691 a BLOCK_INPUT. */
11692 change_frame_size (f, rows, cols, 0, 1, 0);
11693 PIXEL_WIDTH (f) = pixelwidth;
11694 PIXEL_HEIGHT (f) = pixelheight;
11695
11696 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11697 receive in the ConfigureNotify event; if we get what we asked
11698 for, then the event won't cause the screen to become garbaged, so
11699 we have to make sure to do it here. */
11700 SET_FRAME_GARBAGED (f);
11701
11702 XFlush (FRAME_X_DISPLAY (f));
11703
11704#endif /* not USE_X_TOOLKIT */
11705
11706 /* If cursor was outside the new size, mark it as off. */
11707 mark_window_cursors_off (XWINDOW (f->root_window));
11708
11709 /* Clear out any recollection of where the mouse highlighting was,
11710 since it might be in a place that's outside the new frame size.
11711 Actually checking whether it is outside is a pain in the neck,
11712 so don't try--just let the highlighting be done afresh with new size. */
11713 cancel_mouse_face (f);
11714
11715 UNBLOCK_INPUT;
11716}
11717\f
11718/* Mouse warping. */
11719
11720void
11721x_set_mouse_position (f, x, y)
11722 struct frame *f;
11723 int x, y;
11724{
11725 int pix_x, pix_y;
11726
11727 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11728 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11729
11730 if (pix_x < 0) pix_x = 0;
11731 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11732
11733 if (pix_y < 0) pix_y = 0;
11734 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11735
11736 BLOCK_INPUT;
11737
11738 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11739 0, 0, 0, 0, pix_x, pix_y);
11740 UNBLOCK_INPUT;
11741}
11742
11743/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11744
11745void
11746x_set_mouse_pixel_position (f, pix_x, pix_y)
11747 struct frame *f;
11748 int pix_x, pix_y;
11749{
11750 BLOCK_INPUT;
11751
11752 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11753 0, 0, 0, 0, pix_x, pix_y);
11754 UNBLOCK_INPUT;
11755}
11756\f
11757/* focus shifting, raising and lowering. */
11758
11759void
11760x_focus_on_frame (f)
11761 struct frame *f;
11762{
11763#if 0 /* This proves to be unpleasant. */
11764 x_raise_frame (f);
11765#endif
11766#if 0
11767 /* I don't think that the ICCCM allows programs to do things like this
11768 without the interaction of the window manager. Whatever you end up
11769 doing with this code, do it to x_unfocus_frame too. */
11770 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11771 RevertToPointerRoot, CurrentTime);
11772#endif /* ! 0 */
11773}
11774
11775void
11776x_unfocus_frame (f)
11777 struct frame *f;
11778{
11779#if 0
11780 /* Look at the remarks in x_focus_on_frame. */
11781 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11782 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11783 RevertToPointerRoot, CurrentTime);
11784#endif /* ! 0 */
11785}
11786
11787/* Raise frame F. */
11788
11789void
11790x_raise_frame (f)
11791 struct frame *f;
11792{
11793 if (f->async_visible)
11794 {
11795 BLOCK_INPUT;
11796#ifdef USE_X_TOOLKIT
11797 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11798#else /* not USE_X_TOOLKIT */
11799 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11800#endif /* not USE_X_TOOLKIT */
11801 XFlush (FRAME_X_DISPLAY (f));
11802 UNBLOCK_INPUT;
11803 }
11804}
11805
11806/* Lower frame F. */
11807
11808void
11809x_lower_frame (f)
11810 struct frame *f;
11811{
11812 if (f->async_visible)
11813 {
11814 BLOCK_INPUT;
11815#ifdef USE_X_TOOLKIT
11816 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11817#else /* not USE_X_TOOLKIT */
11818 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11819#endif /* not USE_X_TOOLKIT */
11820 XFlush (FRAME_X_DISPLAY (f));
11821 UNBLOCK_INPUT;
11822 }
11823}
11824
11825static void
11826XTframe_raise_lower (f, raise_flag)
11827 FRAME_PTR f;
11828 int raise_flag;
11829{
11830 if (raise_flag)
11831 x_raise_frame (f);
11832 else
11833 x_lower_frame (f);
11834}
11835\f
11836/* Change of visibility. */
11837
11838/* This tries to wait until the frame is really visible.
11839 However, if the window manager asks the user where to position
11840 the frame, this will return before the user finishes doing that.
11841 The frame will not actually be visible at that time,
11842 but it will become visible later when the window manager
11843 finishes with it. */
11844
11845void
11846x_make_frame_visible (f)
11847 struct frame *f;
11848{
11849 Lisp_Object type;
11850 int original_top, original_left;
11851 int retry_count = 2;
11852
11853 retry:
11854
11855 BLOCK_INPUT;
11856
11857 type = x_icon_type (f);
11858 if (!NILP (type))
11859 x_bitmap_icon (f, type);
11860
11861 if (! FRAME_VISIBLE_P (f))
11862 {
11863 /* We test FRAME_GARBAGED_P here to make sure we don't
11864 call x_set_offset a second time
11865 if we get to x_make_frame_visible a second time
11866 before the window gets really visible. */
11867 if (! FRAME_ICONIFIED_P (f)
11868 && ! f->output_data.x->asked_for_visible)
11869 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11870
11871 f->output_data.x->asked_for_visible = 1;
11872
11873 if (! EQ (Vx_no_window_manager, Qt))
11874 x_wm_set_window_state (f, NormalState);
11875#ifdef USE_X_TOOLKIT
11876 /* This was XtPopup, but that did nothing for an iconified frame. */
11877 XtMapWidget (f->output_data.x->widget);
11878#else /* not USE_X_TOOLKIT */
11879 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11880#endif /* not USE_X_TOOLKIT */
11881#if 0 /* This seems to bring back scroll bars in the wrong places
11882 if the window configuration has changed. They seem
11883 to come back ok without this. */
11884 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11885 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11886#endif
11887 }
11888
11889 XFlush (FRAME_X_DISPLAY (f));
11890
11891 /* Synchronize to ensure Emacs knows the frame is visible
11892 before we do anything else. We do this loop with input not blocked
11893 so that incoming events are handled. */
11894 {
11895 Lisp_Object frame;
11896 int count;
11897 /* This must be before UNBLOCK_INPUT
11898 since events that arrive in response to the actions above
11899 will set it when they are handled. */
11900 int previously_visible = f->output_data.x->has_been_visible;
11901
11902 original_left = f->output_data.x->left_pos;
11903 original_top = f->output_data.x->top_pos;
11904
11905 /* This must come after we set COUNT. */
11906 UNBLOCK_INPUT;
11907
11908 /* We unblock here so that arriving X events are processed. */
11909
11910 /* Now move the window back to where it was "supposed to be".
11911 But don't do it if the gravity is negative.
11912 When the gravity is negative, this uses a position
11913 that is 3 pixels too low. Perhaps that's really the border width.
11914
11915 Don't do this if the window has never been visible before,
11916 because the window manager may choose the position
11917 and we don't want to override it. */
11918
11919 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11920 && f->output_data.x->win_gravity == NorthWestGravity
11921 && previously_visible)
11922 {
11923 Drawable rootw;
11924 int x, y;
11925 unsigned int width, height, border, depth;
11926
11927 BLOCK_INPUT;
11928
11929 /* On some window managers (such as FVWM) moving an existing
11930 window, even to the same place, causes the window manager
11931 to introduce an offset. This can cause the window to move
11932 to an unexpected location. Check the geometry (a little
11933 slow here) and then verify that the window is in the right
11934 place. If the window is not in the right place, move it
11935 there, and take the potential window manager hit. */
11936 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11937 &rootw, &x, &y, &width, &height, &border, &depth);
11938
11939 if (original_left != x || original_top != y)
11940 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11941 original_left, original_top);
11942
11943 UNBLOCK_INPUT;
11944 }
11945
11946 XSETFRAME (frame, f);
11947
11948 /* Wait until the frame is visible. Process X events until a
11949 MapNotify event has been seen, or until we think we won't get a
11950 MapNotify at all.. */
11951 for (count = input_signal_count + 10;
11952 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11953 {
11954 /* Force processing of queued events. */
11955 x_sync (f);
11956
11957 /* Machines that do polling rather than SIGIO have been
11958 observed to go into a busy-wait here. So we'll fake an
11959 alarm signal to let the handler know that there's something
11960 to be read. We used to raise a real alarm, but it seems
11961 that the handler isn't always enabled here. This is
11962 probably a bug. */
11963 if (input_polling_used ())
11964 {
11965 /* It could be confusing if a real alarm arrives while
11966 processing the fake one. Turn it off and let the
11967 handler reset it. */
11968 extern void poll_for_input_1 P_ ((void));
11969 int old_poll_suppress_count = poll_suppress_count;
11970 poll_suppress_count = 1;
11971 poll_for_input_1 ();
11972 poll_suppress_count = old_poll_suppress_count;
11973 }
11974
11975 /* See if a MapNotify event has been processed. */
11976 FRAME_SAMPLE_VISIBILITY (f);
11977 }
11978
11979 /* 2000-09-28: In
11980
11981 (let ((f (selected-frame)))
11982 (iconify-frame f)
11983 (raise-frame f))
11984
11985 the frame is not raised with various window managers on
11986 FreeBSD, Linux and Solaris. It turns out that, for some
11987 unknown reason, the call to XtMapWidget is completely ignored.
11988 Mapping the widget a second time works. */
11989
11990 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
11991 goto retry;
11992 }
11993}
11994
11995/* Change from mapped state to withdrawn state. */
11996
11997/* Make the frame visible (mapped and not iconified). */
11998
11999void
12000x_make_frame_invisible (f)
12001 struct frame *f;
12002{
12003 Window window;
12004
12005#ifdef USE_X_TOOLKIT
12006 /* Use the frame's outermost window, not the one we normally draw on. */
12007 window = XtWindow (f->output_data.x->widget);
12008#else /* not USE_X_TOOLKIT */
12009 window = FRAME_X_WINDOW (f);
12010#endif /* not USE_X_TOOLKIT */
12011
12012 /* Don't keep the highlight on an invisible frame. */
12013 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12014 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12015
12016#if 0/* This might add unreliability; I don't trust it -- rms. */
12017 if (! f->async_visible && ! f->async_iconified)
12018 return;
12019#endif
12020
12021 BLOCK_INPUT;
12022
12023 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12024 that the current position of the window is user-specified, rather than
12025 program-specified, so that when the window is mapped again, it will be
12026 placed at the same location, without forcing the user to position it
12027 by hand again (they have already done that once for this window.) */
12028 x_wm_set_size_hint (f, (long) 0, 1);
12029
12030#ifdef HAVE_X11R4
12031
12032 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12033 DefaultScreen (FRAME_X_DISPLAY (f))))
12034 {
12035 UNBLOCK_INPUT_RESIGNAL;
12036 error ("Can't notify window manager of window withdrawal");
12037 }
12038#else /* ! defined (HAVE_X11R4) */
12039
12040 /* Tell the window manager what we're going to do. */
12041 if (! EQ (Vx_no_window_manager, Qt))
12042 {
12043 XEvent unmap;
12044
12045 unmap.xunmap.type = UnmapNotify;
12046 unmap.xunmap.window = window;
12047 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12048 unmap.xunmap.from_configure = False;
12049 if (! XSendEvent (FRAME_X_DISPLAY (f),
12050 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12051 False,
12052 SubstructureRedirectMaskSubstructureNotifyMask,
12053 &unmap))
12054 {
12055 UNBLOCK_INPUT_RESIGNAL;
12056 error ("Can't notify window manager of withdrawal");
12057 }
12058 }
12059
12060 /* Unmap the window ourselves. Cheeky! */
12061 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12062#endif /* ! defined (HAVE_X11R4) */
12063
12064 /* We can't distinguish this from iconification
12065 just by the event that we get from the server.
12066 So we can't win using the usual strategy of letting
12067 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12068 and synchronize with the server to make sure we agree. */
12069 f->visible = 0;
12070 FRAME_ICONIFIED_P (f) = 0;
12071 f->async_visible = 0;
12072 f->async_iconified = 0;
12073
12074 x_sync (f);
12075
12076 UNBLOCK_INPUT;
12077}
12078
12079/* Change window state from mapped to iconified. */
12080
12081void
12082x_iconify_frame (f)
12083 struct frame *f;
12084{
12085 int result;
12086 Lisp_Object type;
12087
12088 /* Don't keep the highlight on an invisible frame. */
12089 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12090 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12091
12092 if (f->async_iconified)
12093 return;
12094
12095 BLOCK_INPUT;
12096
12097 FRAME_SAMPLE_VISIBILITY (f);
12098
12099 type = x_icon_type (f);
12100 if (!NILP (type))
12101 x_bitmap_icon (f, type);
12102
12103#ifdef USE_X_TOOLKIT
12104
12105 if (! FRAME_VISIBLE_P (f))
12106 {
12107 if (! EQ (Vx_no_window_manager, Qt))
12108 x_wm_set_window_state (f, IconicState);
12109 /* This was XtPopup, but that did nothing for an iconified frame. */
12110 XtMapWidget (f->output_data.x->widget);
12111 /* The server won't give us any event to indicate
12112 that an invisible frame was changed to an icon,
12113 so we have to record it here. */
12114 f->iconified = 1;
12115 f->visible = 1;
12116 f->async_iconified = 1;
12117 f->async_visible = 0;
12118 UNBLOCK_INPUT;
12119 return;
12120 }
12121
12122 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12123 XtWindow (f->output_data.x->widget),
12124 DefaultScreen (FRAME_X_DISPLAY (f)));
12125 UNBLOCK_INPUT;
12126
12127 if (!result)
12128 error ("Can't notify window manager of iconification");
12129
12130 f->async_iconified = 1;
12131 f->async_visible = 0;
12132
12133
12134 BLOCK_INPUT;
12135 XFlush (FRAME_X_DISPLAY (f));
12136 UNBLOCK_INPUT;
12137#else /* not USE_X_TOOLKIT */
12138
12139 /* Make sure the X server knows where the window should be positioned,
12140 in case the user deiconifies with the window manager. */
12141 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12142 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12143
12144 /* Since we don't know which revision of X we're running, we'll use both
12145 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12146
12147 /* X11R4: send a ClientMessage to the window manager using the
12148 WM_CHANGE_STATE type. */
12149 {
12150 XEvent message;
12151
12152 message.xclient.window = FRAME_X_WINDOW (f);
12153 message.xclient.type = ClientMessage;
12154 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12155 message.xclient.format = 32;
12156 message.xclient.data.l[0] = IconicState;
12157
12158 if (! XSendEvent (FRAME_X_DISPLAY (f),
12159 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12160 False,
12161 SubstructureRedirectMask | SubstructureNotifyMask,
12162 &message))
12163 {
12164 UNBLOCK_INPUT_RESIGNAL;
12165 error ("Can't notify window manager of iconification");
12166 }
12167 }
12168
12169 /* X11R3: set the initial_state field of the window manager hints to
12170 IconicState. */
12171 x_wm_set_window_state (f, IconicState);
12172
12173 if (!FRAME_VISIBLE_P (f))
12174 {
12175 /* If the frame was withdrawn, before, we must map it. */
12176 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12177 }
12178
12179 f->async_iconified = 1;
12180 f->async_visible = 0;
12181
12182 XFlush (FRAME_X_DISPLAY (f));
12183 UNBLOCK_INPUT;
12184#endif /* not USE_X_TOOLKIT */
12185}
12186\f
12187/* Destroy the X window of frame F. */
12188
12189void
12190x_destroy_window (f)
12191 struct frame *f;
12192{
12193 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12194
12195 BLOCK_INPUT;
12196
12197 /* If a display connection is dead, don't try sending more
12198 commands to the X server. */
12199 if (dpyinfo->display != 0)
12200 {
12201 if (f->output_data.x->icon_desc != 0)
12202 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12203#ifdef HAVE_X_I18N
12204 if (FRAME_XIC (f))
12205 free_frame_xic (f);
12206#endif
12207 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
12208#ifdef USE_X_TOOLKIT
12209 if (f->output_data.x->widget)
12210 XtDestroyWidget (f->output_data.x->widget);
12211 free_frame_menubar (f);
12212#endif /* USE_X_TOOLKIT */
12213
12214 unload_color (f, f->output_data.x->foreground_pixel);
12215 unload_color (f, f->output_data.x->background_pixel);
12216 unload_color (f, f->output_data.x->cursor_pixel);
12217 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12218 unload_color (f, f->output_data.x->border_pixel);
12219 unload_color (f, f->output_data.x->mouse_pixel);
12220 if (f->output_data.x->scroll_bar_background_pixel != -1)
12221 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12222 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12223 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12224 if (f->output_data.x->white_relief.allocated_p)
12225 unload_color (f, f->output_data.x->white_relief.pixel);
12226 if (f->output_data.x->black_relief.allocated_p)
12227 unload_color (f, f->output_data.x->black_relief.pixel);
12228
12229 free_frame_faces (f);
12230 XFlush (FRAME_X_DISPLAY (f));
12231 }
12232
12233 if (f->output_data.x->saved_menu_event)
12234 xfree (f->output_data.x->saved_menu_event);
12235
12236 xfree (f->output_data.x);
12237 f->output_data.x = 0;
12238 if (f == dpyinfo->x_focus_frame)
12239 dpyinfo->x_focus_frame = 0;
12240 if (f == dpyinfo->x_focus_event_frame)
12241 dpyinfo->x_focus_event_frame = 0;
12242 if (f == dpyinfo->x_highlight_frame)
12243 dpyinfo->x_highlight_frame = 0;
12244
12245 dpyinfo->reference_count--;
12246
12247 if (f == dpyinfo->mouse_face_mouse_frame)
12248 {
12249 dpyinfo->mouse_face_beg_row
12250 = dpyinfo->mouse_face_beg_col = -1;
12251 dpyinfo->mouse_face_end_row
12252 = dpyinfo->mouse_face_end_col = -1;
12253 dpyinfo->mouse_face_window = Qnil;
12254 dpyinfo->mouse_face_deferred_gc = 0;
12255 dpyinfo->mouse_face_mouse_frame = 0;
12256 }
12257
12258 UNBLOCK_INPUT;
12259}
12260\f
12261/* Setting window manager hints. */
12262
12263/* Set the normal size hints for the window manager, for frame F.
12264 FLAGS is the flags word to use--or 0 meaning preserve the flags
12265 that the window now has.
12266 If USER_POSITION is nonzero, we set the USPosition
12267 flag (this is useful when FLAGS is 0). */
12268
12269void
12270x_wm_set_size_hint (f, flags, user_position)
12271 struct frame *f;
12272 long flags;
12273 int user_position;
12274{
12275 XSizeHints size_hints;
12276
12277#ifdef USE_X_TOOLKIT
12278 Arg al[2];
12279 int ac = 0;
12280 Dimension widget_width, widget_height;
12281 Window window = XtWindow (f->output_data.x->widget);
12282#else /* not USE_X_TOOLKIT */
12283 Window window = FRAME_X_WINDOW (f);
12284#endif /* not USE_X_TOOLKIT */
12285
12286 /* Setting PMaxSize caused various problems. */
12287 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12288
12289 size_hints.x = f->output_data.x->left_pos;
12290 size_hints.y = f->output_data.x->top_pos;
12291
12292#ifdef USE_X_TOOLKIT
12293 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12294 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12295 XtGetValues (f->output_data.x->widget, al, ac);
12296 size_hints.height = widget_height;
12297 size_hints.width = widget_width;
12298#else /* not USE_X_TOOLKIT */
12299 size_hints.height = PIXEL_HEIGHT (f);
12300 size_hints.width = PIXEL_WIDTH (f);
12301#endif /* not USE_X_TOOLKIT */
12302
12303 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12304 size_hints.height_inc = f->output_data.x->line_height;
12305 size_hints.max_width
12306 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12307 size_hints.max_height
12308 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12309
12310 /* Calculate the base and minimum sizes.
12311
12312 (When we use the X toolkit, we don't do it here.
12313 Instead we copy the values that the widgets are using, below.) */
12314#ifndef USE_X_TOOLKIT
12315 {
12316 int base_width, base_height;
12317 int min_rows = 0, min_cols = 0;
12318
12319 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12320 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12321
12322 check_frame_size (f, &min_rows, &min_cols);
12323
12324 /* The window manager uses the base width hints to calculate the
12325 current number of rows and columns in the frame while
12326 resizing; min_width and min_height aren't useful for this
12327 purpose, since they might not give the dimensions for a
12328 zero-row, zero-column frame.
12329
12330 We use the base_width and base_height members if we have
12331 them; otherwise, we set the min_width and min_height members
12332 to the size for a zero x zero frame. */
12333
12334#ifdef HAVE_X11R4
12335 size_hints.flags |= PBaseSize;
12336 size_hints.base_width = base_width;
12337 size_hints.base_height = base_height;
12338 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12339 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12340#else
12341 size_hints.min_width = base_width;
12342 size_hints.min_height = base_height;
12343#endif
12344 }
12345
12346 /* If we don't need the old flags, we don't need the old hint at all. */
12347 if (flags)
12348 {
12349 size_hints.flags |= flags;
12350 goto no_read;
12351 }
12352#endif /* not USE_X_TOOLKIT */
12353
12354 {
12355 XSizeHints hints; /* Sometimes I hate X Windows... */
12356 long supplied_return;
12357 int value;
12358
12359#ifdef HAVE_X11R4
12360 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12361 &supplied_return);
12362#else
12363 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12364#endif
12365
12366#ifdef USE_X_TOOLKIT
12367 size_hints.base_height = hints.base_height;
12368 size_hints.base_width = hints.base_width;
12369 size_hints.min_height = hints.min_height;
12370 size_hints.min_width = hints.min_width;
12371#endif
12372
12373 if (flags)
12374 size_hints.flags |= flags;
12375 else
12376 {
12377 if (value == 0)
12378 hints.flags = 0;
12379 if (hints.flags & PSize)
12380 size_hints.flags |= PSize;
12381 if (hints.flags & PPosition)
12382 size_hints.flags |= PPosition;
12383 if (hints.flags & USPosition)
12384 size_hints.flags |= USPosition;
12385 if (hints.flags & USSize)
12386 size_hints.flags |= USSize;
12387 }
12388 }
12389
12390#ifndef USE_X_TOOLKIT
12391 no_read:
12392#endif
12393
12394#ifdef PWinGravity
12395 size_hints.win_gravity = f->output_data.x->win_gravity;
12396 size_hints.flags |= PWinGravity;
12397
12398 if (user_position)
12399 {
12400 size_hints.flags &= ~ PPosition;
12401 size_hints.flags |= USPosition;
12402 }
12403#endif /* PWinGravity */
12404
12405#ifdef HAVE_X11R4
12406 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12407#else
12408 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12409#endif
12410}
12411
12412/* Used for IconicState or NormalState */
12413
12414void
12415x_wm_set_window_state (f, state)
12416 struct frame *f;
12417 int state;
12418{
12419#ifdef USE_X_TOOLKIT
12420 Arg al[1];
12421
12422 XtSetArg (al[0], XtNinitialState, state);
12423 XtSetValues (f->output_data.x->widget, al, 1);
12424#else /* not USE_X_TOOLKIT */
12425 Window window = FRAME_X_WINDOW (f);
12426
12427 f->output_data.x->wm_hints.flags |= StateHint;
12428 f->output_data.x->wm_hints.initial_state = state;
12429
12430 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12431#endif /* not USE_X_TOOLKIT */
12432}
12433
12434void
12435x_wm_set_icon_pixmap (f, pixmap_id)
12436 struct frame *f;
12437 int pixmap_id;
12438{
12439 Pixmap icon_pixmap;
12440
12441#ifndef USE_X_TOOLKIT
12442 Window window = FRAME_X_WINDOW (f);
12443#endif
12444
12445 if (pixmap_id > 0)
12446 {
12447 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12448 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12449 }
12450 else
12451 {
12452 /* It seems there is no way to turn off use of an icon pixmap.
12453 The following line does it, only if no icon has yet been created,
12454 for some window managers. But with mwm it crashes.
12455 Some people say it should clear the IconPixmapHint bit in this case,
12456 but that doesn't work, and the X consortium said it isn't the
12457 right thing at all. Since there is no way to win,
12458 best to explicitly give up. */
12459#if 0
12460 f->output_data.x->wm_hints.icon_pixmap = None;
12461#else
12462 return;
12463#endif
12464 }
12465
12466#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12467
12468 {
12469 Arg al[1];
12470 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12471 XtSetValues (f->output_data.x->widget, al, 1);
12472 }
12473
12474#else /* not USE_X_TOOLKIT */
12475
12476 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12477 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12478
12479#endif /* not USE_X_TOOLKIT */
12480}
12481
12482void
12483x_wm_set_icon_position (f, icon_x, icon_y)
12484 struct frame *f;
12485 int icon_x, icon_y;
12486{
12487#ifdef USE_X_TOOLKIT
12488 Window window = XtWindow (f->output_data.x->widget);
12489#else
12490 Window window = FRAME_X_WINDOW (f);
12491#endif
12492
12493 f->output_data.x->wm_hints.flags |= IconPositionHint;
12494 f->output_data.x->wm_hints.icon_x = icon_x;
12495 f->output_data.x->wm_hints.icon_y = icon_y;
12496
12497 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12498}
12499
12500\f
12501/***********************************************************************
12502 Fonts
12503 ***********************************************************************/
12504
12505/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12506
12507struct font_info *
12508x_get_font_info (f, font_idx)
12509 FRAME_PTR f;
12510 int font_idx;
12511{
12512 return (FRAME_X_FONT_TABLE (f) + font_idx);
12513}
12514
12515
12516/* Return a list of names of available fonts matching PATTERN on frame
12517 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12518 to be listed. Frame F NULL means we have not yet created any
12519 frame on X, and consult the first display in x_display_list.
12520 MAXNAMES sets a limit on how many fonts to match. */
12521
12522Lisp_Object
12523x_list_fonts (f, pattern, size, maxnames)
12524 FRAME_PTR f;
12525 Lisp_Object pattern;
12526 int size;
12527 int maxnames;
12528{
12529 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12530 Lisp_Object tem, second_best;
12531 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12532 int try_XLoadQueryFont = 0;
12533 int count;
12534
12535 patterns = Fassoc (pattern, Valternate_fontname_alist);
12536 if (NILP (patterns))
12537 patterns = Fcons (pattern, Qnil);
12538
12539 if (maxnames == 1 && !size)
12540 /* We can return any single font matching PATTERN. */
12541 try_XLoadQueryFont = 1;
12542
12543 for (; CONSP (patterns); patterns = XCDR (patterns))
12544 {
12545 int num_fonts;
12546 char **names = NULL;
12547
12548 pattern = XCAR (patterns);
12549 /* See if we cached the result for this particular query.
12550 The cache is an alist of the form:
12551 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12552 */
12553 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12554 key = Fcons (pattern, make_number (maxnames)),
12555 !NILP (list = Fassoc (key, tem))))
12556 {
12557 list = Fcdr_safe (list);
12558 /* We have a cashed list. Don't have to get the list again. */
12559 goto label_cached;
12560 }
12561
12562 /* At first, put PATTERN in the cache. */
12563
12564 BLOCK_INPUT;
12565 count = x_catch_errors (dpy);
12566
12567 if (try_XLoadQueryFont)
12568 {
12569 XFontStruct *font;
12570 unsigned long value;
12571
12572 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12573 if (x_had_errors_p (dpy))
12574 {
12575 /* This error is perhaps due to insufficient memory on X
12576 server. Let's just ignore it. */
12577 font = NULL;
12578 x_clear_errors (dpy);
12579 }
12580
12581 if (font
12582 && XGetFontProperty (font, XA_FONT, &value))
12583 {
12584 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12585 int len = strlen (name);
12586 char *tmp;
12587
12588 /* If DXPC (a Differential X Protocol Compressor)
12589 Ver.3.7 is running, XGetAtomName will return null
12590 string. We must avoid such a name. */
12591 if (len == 0)
12592 try_XLoadQueryFont = 0;
12593 else
12594 {
12595 num_fonts = 1;
12596 names = (char **) alloca (sizeof (char *));
12597 /* Some systems only allow alloca assigned to a
12598 simple var. */
12599 tmp = (char *) alloca (len + 1); names[0] = tmp;
12600 bcopy (name, names[0], len + 1);
12601 XFree (name);
12602 }
12603 }
12604 else
12605 try_XLoadQueryFont = 0;
12606
12607 if (font)
12608 XFreeFont (dpy, font);
12609 }
12610
12611 if (!try_XLoadQueryFont)
12612 {
12613 /* We try at least 10 fonts because XListFonts will return
12614 auto-scaled fonts at the head. */
12615 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12616 &num_fonts);
12617 if (x_had_errors_p (dpy))
12618 {
12619 /* This error is perhaps due to insufficient memory on X
12620 server. Let's just ignore it. */
12621 names = NULL;
12622 x_clear_errors (dpy);
12623 }
12624 }
12625
12626 x_uncatch_errors (dpy, count);
12627 UNBLOCK_INPUT;
12628
12629 if (names)
12630 {
12631 int i;
12632
12633 /* Make a list of all the fonts we got back.
12634 Store that in the font cache for the display. */
12635 for (i = 0; i < num_fonts; i++)
12636 {
12637 int width = 0;
12638 char *p = names[i];
12639 int average_width = -1, dashes = 0;
12640
12641 /* Count the number of dashes in NAMES[I]. If there are
12642 14 dashes, and the field value following 12th dash
12643 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12644 is usually too ugly to be used for editing. Let's
12645 ignore it. */
12646 while (*p)
12647 if (*p++ == '-')
12648 {
12649 dashes++;
12650 if (dashes == 7) /* PIXEL_SIZE field */
12651 width = atoi (p);
12652 else if (dashes == 12) /* AVERAGE_WIDTH field */
12653 average_width = atoi (p);
12654 }
12655 if (dashes < 14 || average_width != 0)
12656 {
12657 tem = build_string (names[i]);
12658 if (NILP (Fassoc (tem, list)))
12659 {
12660 if (STRINGP (Vx_pixel_size_width_font_regexp)
12661 && ((fast_c_string_match_ignore_case
12662 (Vx_pixel_size_width_font_regexp, names[i]))
12663 >= 0))
12664 /* We can set the value of PIXEL_SIZE to the
12665 width of this font. */
12666 list = Fcons (Fcons (tem, make_number (width)), list);
12667 else
12668 /* For the moment, width is not known. */
12669 list = Fcons (Fcons (tem, Qnil), list);
12670 }
12671 }
12672 }
12673 if (!try_XLoadQueryFont)
12674 XFreeFontNames (names);
12675 }
12676
12677 /* Now store the result in the cache. */
12678 if (f != NULL)
12679 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12680 = Fcons (Fcons (key, list),
12681 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12682
12683 label_cached:
12684 if (NILP (list)) continue; /* Try the remaining alternatives. */
12685
12686 newlist = second_best = Qnil;
12687 /* Make a list of the fonts that have the right width. */
12688 for (; CONSP (list); list = XCDR (list))
12689 {
12690 int found_size;
12691
12692 tem = XCAR (list);
12693
12694 if (!CONSP (tem) || NILP (XCAR (tem)))
12695 continue;
12696 if (!size)
12697 {
12698 newlist = Fcons (XCAR (tem), newlist);
12699 continue;
12700 }
12701
12702 if (!INTEGERP (XCDR (tem)))
12703 {
12704 /* Since we have not yet known the size of this font, we
12705 must try slow function call XLoadQueryFont. */
12706 XFontStruct *thisinfo;
12707
12708 BLOCK_INPUT;
12709 count = x_catch_errors (dpy);
12710 thisinfo = XLoadQueryFont (dpy,
12711 XSTRING (XCAR (tem))->data);
12712 if (x_had_errors_p (dpy))
12713 {
12714 /* This error is perhaps due to insufficient memory on X
12715 server. Let's just ignore it. */
12716 thisinfo = NULL;
12717 x_clear_errors (dpy);
12718 }
12719 x_uncatch_errors (dpy, count);
12720 UNBLOCK_INPUT;
12721
12722 if (thisinfo)
12723 {
12724 XCDR (tem)
12725 = (thisinfo->min_bounds.width == 0
12726 ? make_number (0)
12727 : make_number (thisinfo->max_bounds.width));
12728 XFreeFont (dpy, thisinfo);
12729 }
12730 else
12731 /* For unknown reason, the previous call of XListFont had
12732 returned a font which can't be opened. Record the size
12733 as 0 not to try to open it again. */
12734 XCDR (tem) = make_number (0);
12735 }
12736
12737 found_size = XINT (XCDR (tem));
12738 if (found_size == size)
12739 newlist = Fcons (XCAR (tem), newlist);
12740 else if (found_size > 0)
12741 {
12742 if (NILP (second_best))
12743 second_best = tem;
12744 else if (found_size < size)
12745 {
12746 if (XINT (XCDR (second_best)) > size
12747 || XINT (XCDR (second_best)) < found_size)
12748 second_best = tem;
12749 }
12750 else
12751 {
12752 if (XINT (XCDR (second_best)) > size
12753 && XINT (XCDR (second_best)) > found_size)
12754 second_best = tem;
12755 }
12756 }
12757 }
12758 if (!NILP (newlist))
12759 break;
12760 else if (!NILP (second_best))
12761 {
12762 newlist = Fcons (XCAR (second_best), Qnil);
12763 break;
12764 }
12765 }
12766
12767 return newlist;
12768}
12769
12770
12771#if GLYPH_DEBUG
12772
12773/* Check that FONT is valid on frame F. It is if it can be found in F's
12774 font table. */
12775
12776static void
12777x_check_font (f, font)
12778 struct frame *f;
12779 XFontStruct *font;
12780{
12781 int i;
12782 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12783
12784 xassert (font != NULL);
12785
12786 for (i = 0; i < dpyinfo->n_fonts; i++)
12787 if (dpyinfo->font_table[i].name
12788 && font == dpyinfo->font_table[i].font)
12789 break;
12790
12791 xassert (i < dpyinfo->n_fonts);
12792}
12793
12794#endif /* GLYPH_DEBUG != 0 */
12795
12796/* Set *W to the minimum width, *H to the minimum font height of FONT.
12797 Note: There are (broken) X fonts out there with invalid XFontStruct
12798 min_bounds contents. For example, handa@etl.go.jp reports that
12799 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12800 have font->min_bounds.width == 0. */
12801
12802static INLINE void
12803x_font_min_bounds (font, w, h)
12804 XFontStruct *font;
12805 int *w, *h;
12806{
12807 *h = FONT_HEIGHT (font);
12808 *w = font->min_bounds.width;
12809
12810 /* Try to handle the case where FONT->min_bounds has invalid
12811 contents. Since the only font known to have invalid min_bounds
12812 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12813 if (*w <= 0)
12814 *w = font->max_bounds.width;
12815}
12816
12817
12818/* Compute the smallest character width and smallest font height over
12819 all fonts available on frame F. Set the members smallest_char_width
12820 and smallest_font_height in F's x_display_info structure to
12821 the values computed. Value is non-zero if smallest_font_height or
12822 smallest_char_width become smaller than they were before. */
12823
12824static int
12825x_compute_min_glyph_bounds (f)
12826 struct frame *f;
12827{
12828 int i;
12829 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12830 XFontStruct *font;
12831 int old_width = dpyinfo->smallest_char_width;
12832 int old_height = dpyinfo->smallest_font_height;
12833
12834 dpyinfo->smallest_font_height = 100000;
12835 dpyinfo->smallest_char_width = 100000;
12836
12837 for (i = 0; i < dpyinfo->n_fonts; ++i)
12838 if (dpyinfo->font_table[i].name)
12839 {
12840 struct font_info *fontp = dpyinfo->font_table + i;
12841 int w, h;
12842
12843 font = (XFontStruct *) fontp->font;
12844 xassert (font != (XFontStruct *) ~0);
12845 x_font_min_bounds (font, &w, &h);
12846
12847 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12848 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12849 }
12850
12851 xassert (dpyinfo->smallest_char_width > 0
12852 && dpyinfo->smallest_font_height > 0);
12853
12854 return (dpyinfo->n_fonts == 1
12855 || dpyinfo->smallest_char_width < old_width
12856 || dpyinfo->smallest_font_height < old_height);
12857}
12858
12859
12860/* Load font named FONTNAME of the size SIZE for frame F, and return a
12861 pointer to the structure font_info while allocating it dynamically.
12862 If SIZE is 0, load any size of font.
12863 If loading is failed, return NULL. */
12864
12865struct font_info *
12866x_load_font (f, fontname, size)
12867 struct frame *f;
12868 register char *fontname;
12869 int size;
12870{
12871 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12872 Lisp_Object font_names;
12873 int count;
12874
12875 /* Get a list of all the fonts that match this name. Once we
12876 have a list of matching fonts, we compare them against the fonts
12877 we already have by comparing names. */
12878 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12879
12880 if (!NILP (font_names))
12881 {
12882 Lisp_Object tail;
12883 int i;
12884
12885 for (i = 0; i < dpyinfo->n_fonts; i++)
12886 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12887 if (dpyinfo->font_table[i].name
12888 && (!strcmp (dpyinfo->font_table[i].name,
12889 XSTRING (XCAR (tail))->data)
12890 || !strcmp (dpyinfo->font_table[i].full_name,
12891 XSTRING (XCAR (tail))->data)))
12892 return (dpyinfo->font_table + i);
12893 }
12894
12895 /* Load the font and add it to the table. */
12896 {
12897 char *full_name;
12898 XFontStruct *font;
12899 struct font_info *fontp;
12900 unsigned long value;
12901 int i;
12902
12903 /* If we have found fonts by x_list_font, load one of them. If
12904 not, we still try to load a font by the name given as FONTNAME
12905 because XListFonts (called in x_list_font) of some X server has
12906 a bug of not finding a font even if the font surely exists and
12907 is loadable by XLoadQueryFont. */
12908 if (size > 0 && !NILP (font_names))
12909 fontname = (char *) XSTRING (XCAR (font_names))->data;
12910
12911 BLOCK_INPUT;
12912 count = x_catch_errors (FRAME_X_DISPLAY (f));
12913 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12914 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12915 {
12916 /* This error is perhaps due to insufficient memory on X
12917 server. Let's just ignore it. */
12918 font = NULL;
12919 x_clear_errors (FRAME_X_DISPLAY (f));
12920 }
12921 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12922 UNBLOCK_INPUT;
12923 if (!font)
12924 return NULL;
12925
12926 /* Find a free slot in the font table. */
12927 for (i = 0; i < dpyinfo->n_fonts; ++i)
12928 if (dpyinfo->font_table[i].name == NULL)
12929 break;
12930
12931 /* If no free slot found, maybe enlarge the font table. */
12932 if (i == dpyinfo->n_fonts
12933 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12934 {
12935 int sz;
12936 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12937 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12938 dpyinfo->font_table
12939 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12940 }
12941
12942 fontp = dpyinfo->font_table + i;
12943 if (i == dpyinfo->n_fonts)
12944 ++dpyinfo->n_fonts;
12945
12946 /* Now fill in the slots of *FONTP. */
12947 BLOCK_INPUT;
12948 fontp->font = font;
12949 fontp->font_idx = i;
12950 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12951 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12952
12953 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12954 full_name = 0;
12955 if (XGetFontProperty (font, XA_FONT, &value))
12956 {
12957 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12958 char *p = name;
12959 int dashes = 0;
12960
12961 /* Count the number of dashes in the "full name".
12962 If it is too few, this isn't really the font's full name,
12963 so don't use it.
12964 In X11R4, the fonts did not come with their canonical names
12965 stored in them. */
12966 while (*p)
12967 {
12968 if (*p == '-')
12969 dashes++;
12970 p++;
12971 }
12972
12973 if (dashes >= 13)
12974 {
12975 full_name = (char *) xmalloc (p - name + 1);
12976 bcopy (name, full_name, p - name + 1);
12977 }
12978
12979 XFree (name);
12980 }
12981
12982 if (full_name != 0)
12983 fontp->full_name = full_name;
12984 else
12985 fontp->full_name = fontp->name;
12986
12987 fontp->size = font->max_bounds.width;
12988 fontp->height = FONT_HEIGHT (font);
12989 {
12990 /* For some font, ascent and descent in max_bounds field is
12991 larger than the above value. */
12992 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12993 if (max_height > fontp->height)
12994 fontp->height = max_height;
12995 }
12996
12997 if (NILP (font_names))
12998 {
12999 /* We come here because of a bug of XListFonts mentioned at
13000 the head of this block. Let's store this information in
13001 the cache for x_list_fonts. */
13002 Lisp_Object lispy_name = build_string (fontname);
13003 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13004
13005 XCDR (dpyinfo->name_list_element)
13006 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13007 Fcons (Fcons (lispy_full_name,
13008 make_number (fontp->size)),
13009 Qnil)),
13010 XCDR (dpyinfo->name_list_element));
13011 if (full_name)
13012 XCDR (dpyinfo->name_list_element)
13013 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13014 Fcons (Fcons (lispy_full_name,
13015 make_number (fontp->size)),
13016 Qnil)),
13017 XCDR (dpyinfo->name_list_element));
13018 }
13019
13020 /* The slot `encoding' specifies how to map a character
13021 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13022 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13023 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13024 2:0xA020..0xFF7F). For the moment, we don't know which charset
13025 uses this font. So, we set information in fontp->encoding[1]
13026 which is never used by any charset. If mapping can't be
13027 decided, set FONT_ENCODING_NOT_DECIDED. */
13028 fontp->encoding[1]
13029 = (font->max_byte1 == 0
13030 /* 1-byte font */
13031 ? (font->min_char_or_byte2 < 0x80
13032 ? (font->max_char_or_byte2 < 0x80
13033 ? 0 /* 0x20..0x7F */
13034 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13035 : 1) /* 0xA0..0xFF */
13036 /* 2-byte font */
13037 : (font->min_byte1 < 0x80
13038 ? (font->max_byte1 < 0x80
13039 ? (font->min_char_or_byte2 < 0x80
13040 ? (font->max_char_or_byte2 < 0x80
13041 ? 0 /* 0x2020..0x7F7F */
13042 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13043 : 3) /* 0x20A0..0x7FFF */
13044 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13045 : (font->min_char_or_byte2 < 0x80
13046 ? (font->max_char_or_byte2 < 0x80
13047 ? 2 /* 0xA020..0xFF7F */
13048 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13049 : 1))); /* 0xA0A0..0xFFFF */
13050
13051 fontp->baseline_offset
13052 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13053 ? (long) value : 0);
13054 fontp->relative_compose
13055 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13056 ? (long) value : 0);
13057 fontp->default_ascent
13058 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13059 ? (long) value : 0);
13060
13061 /* Set global flag fonts_changed_p to non-zero if the font loaded
13062 has a character with a smaller width than any other character
13063 before, or if the font loaded has a smalle>r height than any
13064 other font loaded before. If this happens, it will make a
13065 glyph matrix reallocation necessary. */
13066 fonts_changed_p = x_compute_min_glyph_bounds (f);
13067 UNBLOCK_INPUT;
13068 return fontp;
13069 }
13070}
13071
13072
13073/* Return a pointer to struct font_info of a font named FONTNAME for
13074 frame F. If no such font is loaded, return NULL. */
13075
13076struct font_info *
13077x_query_font (f, fontname)
13078 struct frame *f;
13079 register char *fontname;
13080{
13081 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13082 int i;
13083
13084 for (i = 0; i < dpyinfo->n_fonts; i++)
13085 if (dpyinfo->font_table[i].name
13086 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13087 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13088 return (dpyinfo->font_table + i);
13089 return NULL;
13090}
13091
13092
13093/* Find a CCL program for a font specified by FONTP, and set the member
13094 `encoder' of the structure. */
13095
13096void
13097x_find_ccl_program (fontp)
13098 struct font_info *fontp;
13099{
13100 Lisp_Object list, elt;
13101
13102 elt = Qnil;
13103 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13104 {
13105 elt = XCAR (list);
13106 if (CONSP (elt)
13107 && STRINGP (XCAR (elt))
13108 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13109 >= 0))
13110 break;
13111 }
13112
13113 if (! NILP (list))
13114 {
13115 struct ccl_program *ccl
13116 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13117
13118 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13119 xfree (ccl);
13120 else
13121 fontp->font_encoder = ccl;
13122 }
13123}
13124
13125
13126\f
13127/***********************************************************************
13128 Initialization
13129 ***********************************************************************/
13130
13131#ifdef USE_X_TOOLKIT
13132static XrmOptionDescRec emacs_options[] = {
13133 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13134 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13135
13136 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13137 XrmoptionSepArg, NULL},
13138 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13139
13140 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13141 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13142 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13143 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13144 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13145 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13146 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13147};
13148#endif /* USE_X_TOOLKIT */
13149
13150static int x_initialized;
13151
13152#ifdef MULTI_KBOARD
13153/* Test whether two display-name strings agree up to the dot that separates
13154 the screen number from the server number. */
13155static int
13156same_x_server (name1, name2)
13157 char *name1, *name2;
13158{
13159 int seen_colon = 0;
13160 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13161 int system_name_length = strlen (system_name);
13162 int length_until_period = 0;
13163
13164 while (system_name[length_until_period] != 0
13165 && system_name[length_until_period] != '.')
13166 length_until_period++;
13167
13168 /* Treat `unix' like an empty host name. */
13169 if (! strncmp (name1, "unix:", 5))
13170 name1 += 4;
13171 if (! strncmp (name2, "unix:", 5))
13172 name2 += 4;
13173 /* Treat this host's name like an empty host name. */
13174 if (! strncmp (name1, system_name, system_name_length)
13175 && name1[system_name_length] == ':')
13176 name1 += system_name_length;
13177 if (! strncmp (name2, system_name, system_name_length)
13178 && name2[system_name_length] == ':')
13179 name2 += system_name_length;
13180 /* Treat this host's domainless name like an empty host name. */
13181 if (! strncmp (name1, system_name, length_until_period)
13182 && name1[length_until_period] == ':')
13183 name1 += length_until_period;
13184 if (! strncmp (name2, system_name, length_until_period)
13185 && name2[length_until_period] == ':')
13186 name2 += length_until_period;
13187
13188 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13189 {
13190 if (*name1 == ':')
13191 seen_colon++;
13192 if (seen_colon && *name1 == '.')
13193 return 1;
13194 }
13195 return (seen_colon
13196 && (*name1 == '.' || *name1 == '\0')
13197 && (*name2 == '.' || *name2 == '\0'));
13198}
13199#endif
13200
13201struct x_display_info *
13202x_term_init (display_name, xrm_option, resource_name)
13203 Lisp_Object display_name;
13204 char *xrm_option;
13205 char *resource_name;
13206{
13207 int connection;
13208 Display *dpy;
13209 struct x_display_info *dpyinfo;
13210 XrmDatabase xrdb;
13211
13212 BLOCK_INPUT;
13213
13214 if (!x_initialized)
13215 {
13216 x_initialize ();
13217 x_initialized = 1;
13218 }
13219
13220#ifdef USE_X_TOOLKIT
13221 /* weiner@footloose.sps.mot.com reports that this causes
13222 errors with X11R5:
13223 X protocol error: BadAtom (invalid Atom parameter)
13224 on protocol request 18skiloaf.
13225 So let's not use it until R6. */
13226#ifdef HAVE_X11XTR6
13227 XtSetLanguageProc (NULL, NULL, NULL);
13228#endif
13229
13230 {
13231 int argc = 0;
13232 char *argv[3];
13233
13234 argv[0] = "";
13235 argc = 1;
13236 if (xrm_option)
13237 {
13238 argv[argc++] = "-xrm";
13239 argv[argc++] = xrm_option;
13240 }
13241 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13242 resource_name, EMACS_CLASS,
13243 emacs_options, XtNumber (emacs_options),
13244 &argc, argv);
13245
13246#ifdef HAVE_X11XTR6
13247 /* I think this is to compensate for XtSetLanguageProc. */
13248 fixup_locale ();
13249#endif
13250 }
13251
13252#else /* not USE_X_TOOLKIT */
13253#ifdef HAVE_X11R5
13254 XSetLocaleModifiers ("");
13255#endif
13256 dpy = XOpenDisplay (XSTRING (display_name)->data);
13257#endif /* not USE_X_TOOLKIT */
13258
13259 /* Detect failure. */
13260 if (dpy == 0)
13261 {
13262 UNBLOCK_INPUT;
13263 return 0;
13264 }
13265
13266 /* We have definitely succeeded. Record the new connection. */
13267
13268 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13269
13270#ifdef MULTI_KBOARD
13271 {
13272 struct x_display_info *share;
13273 Lisp_Object tail;
13274
13275 for (share = x_display_list, tail = x_display_name_list; share;
13276 share = share->next, tail = XCDR (tail))
13277 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13278 XSTRING (display_name)->data))
13279 break;
13280 if (share)
13281 dpyinfo->kboard = share->kboard;
13282 else
13283 {
13284 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13285 init_kboard (dpyinfo->kboard);
13286 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13287 {
13288 char *vendor = ServerVendor (dpy);
13289 UNBLOCK_INPUT;
13290 dpyinfo->kboard->Vsystem_key_alist
13291 = call1 (Qvendor_specific_keysyms,
13292 build_string (vendor ? vendor : ""));
13293 BLOCK_INPUT;
13294 }
13295
13296 dpyinfo->kboard->next_kboard = all_kboards;
13297 all_kboards = dpyinfo->kboard;
13298 /* Don't let the initial kboard remain current longer than necessary.
13299 That would cause problems if a file loaded on startup tries to
13300 prompt in the mini-buffer. */
13301 if (current_kboard == initial_kboard)
13302 current_kboard = dpyinfo->kboard;
13303 }
13304 dpyinfo->kboard->reference_count++;
13305 }
13306#endif
13307
13308 /* Put this display on the chain. */
13309 dpyinfo->next = x_display_list;
13310 x_display_list = dpyinfo;
13311
13312 /* Put it on x_display_name_list as well, to keep them parallel. */
13313 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13314 x_display_name_list);
13315 dpyinfo->name_list_element = XCAR (x_display_name_list);
13316
13317 dpyinfo->display = dpy;
13318
13319#if 0
13320 XSetAfterFunction (x_current_display, x_trace_wire);
13321#endif /* ! 0 */
13322
13323 dpyinfo->x_id_name
13324 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13325 + STRING_BYTES (XSTRING (Vsystem_name))
13326 + 2);
13327 sprintf (dpyinfo->x_id_name, "%s@%s",
13328 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13329
13330 /* Figure out which modifier bits mean what. */
13331 x_find_modifier_meanings (dpyinfo);
13332
13333 /* Get the scroll bar cursor. */
13334 dpyinfo->vertical_scroll_bar_cursor
13335 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13336
13337 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13338 resource_name, EMACS_CLASS);
13339#ifdef HAVE_XRMSETDATABASE
13340 XrmSetDatabase (dpyinfo->display, xrdb);
13341#else
13342 dpyinfo->display->db = xrdb;
13343#endif
13344 /* Put the rdb where we can find it in a way that works on
13345 all versions. */
13346 dpyinfo->xrdb = xrdb;
13347
13348 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13349 DefaultScreen (dpyinfo->display));
13350 select_visual (dpyinfo);
13351 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13352 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13353 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13354 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13355 dpyinfo->grabbed = 0;
13356 dpyinfo->reference_count = 0;
13357 dpyinfo->icon_bitmap_id = -1;
13358 dpyinfo->font_table = NULL;
13359 dpyinfo->n_fonts = 0;
13360 dpyinfo->font_table_size = 0;
13361 dpyinfo->bitmaps = 0;
13362 dpyinfo->bitmaps_size = 0;
13363 dpyinfo->bitmaps_last = 0;
13364 dpyinfo->scratch_cursor_gc = 0;
13365 dpyinfo->mouse_face_mouse_frame = 0;
13366 dpyinfo->mouse_face_deferred_gc = 0;
13367 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13368 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13369 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13370 dpyinfo->mouse_face_window = Qnil;
13371 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13372 dpyinfo->mouse_face_defer = 0;
13373 dpyinfo->x_focus_frame = 0;
13374 dpyinfo->x_focus_event_frame = 0;
13375 dpyinfo->x_highlight_frame = 0;
13376 dpyinfo->image_cache = make_image_cache ();
13377
13378 /* See if a private colormap is requested. */
13379 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13380 {
13381 if (dpyinfo->visual->class == PseudoColor)
13382 {
13383 Lisp_Object value;
13384 value = display_x_get_resource (dpyinfo,
13385 build_string ("privateColormap"),
13386 build_string ("PrivateColormap"),
13387 Qnil, Qnil);
13388 if (STRINGP (value)
13389 && (!strcmp (XSTRING (value)->data, "true")
13390 || !strcmp (XSTRING (value)->data, "on")))
13391 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13392 }
13393 }
13394 else
13395 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13396 dpyinfo->visual, AllocNone);
13397
13398 {
13399 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13400 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13401 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13402 dpyinfo->resy = pixels * 25.4 / mm;
13403 pixels = DisplayWidth (dpyinfo->display, screen_number);
13404 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13405 dpyinfo->resx = pixels * 25.4 / mm;
13406 }
13407
13408 dpyinfo->Xatom_wm_protocols
13409 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13410 dpyinfo->Xatom_wm_take_focus
13411 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13412 dpyinfo->Xatom_wm_save_yourself
13413 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13414 dpyinfo->Xatom_wm_delete_window
13415 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13416 dpyinfo->Xatom_wm_change_state
13417 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13418 dpyinfo->Xatom_wm_configure_denied
13419 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13420 dpyinfo->Xatom_wm_window_moved
13421 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13422 dpyinfo->Xatom_editres
13423 = XInternAtom (dpyinfo->display, "Editres", False);
13424 dpyinfo->Xatom_CLIPBOARD
13425 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13426 dpyinfo->Xatom_TIMESTAMP
13427 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13428 dpyinfo->Xatom_TEXT
13429 = XInternAtom (dpyinfo->display, "TEXT", False);
13430 dpyinfo->Xatom_COMPOUND_TEXT
13431 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13432 dpyinfo->Xatom_DELETE
13433 = XInternAtom (dpyinfo->display, "DELETE", False);
13434 dpyinfo->Xatom_MULTIPLE
13435 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13436 dpyinfo->Xatom_INCR
13437 = XInternAtom (dpyinfo->display, "INCR", False);
13438 dpyinfo->Xatom_EMACS_TMP
13439 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13440 dpyinfo->Xatom_TARGETS
13441 = XInternAtom (dpyinfo->display, "TARGETS", False);
13442 dpyinfo->Xatom_NULL
13443 = XInternAtom (dpyinfo->display, "NULL", False);
13444 dpyinfo->Xatom_ATOM_PAIR
13445 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13446 /* For properties of font. */
13447 dpyinfo->Xatom_PIXEL_SIZE
13448 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13449 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13450 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13451 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13452 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13453 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13454 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13455
13456 /* Ghostscript support. */
13457 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13458 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13459
13460 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13461 False);
13462
13463 dpyinfo->cut_buffers_initialized = 0;
13464
13465 connection = ConnectionNumber (dpyinfo->display);
13466 dpyinfo->connection = connection;
13467
13468 {
13469 char null_bits[1];
13470
13471 null_bits[0] = 0x00;
13472
13473 dpyinfo->null_pixel
13474 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13475 null_bits, 1, 1, (long) 0, (long) 0,
13476 1);
13477 }
13478
13479 {
13480 extern int gray_bitmap_width, gray_bitmap_height;
13481 extern unsigned char *gray_bitmap_bits;
13482 dpyinfo->gray
13483 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13484 gray_bitmap_bits,
13485 gray_bitmap_width, gray_bitmap_height,
13486 (unsigned long) 1, (unsigned long) 0, 1);
13487 }
13488
13489#ifdef HAVE_X_I18N
13490 xim_initialize (dpyinfo, resource_name);
13491#endif
13492
13493#ifdef subprocesses
13494 /* This is only needed for distinguishing keyboard and process input. */
13495 if (connection != 0)
13496 add_keyboard_wait_descriptor (connection);
13497#endif
13498
13499#ifndef F_SETOWN_BUG
13500#ifdef F_SETOWN
13501#ifdef F_SETOWN_SOCK_NEG
13502 /* stdin is a socket here */
13503 fcntl (connection, F_SETOWN, -getpid ());
13504#else /* ! defined (F_SETOWN_SOCK_NEG) */
13505 fcntl (connection, F_SETOWN, getpid ());
13506#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13507#endif /* ! defined (F_SETOWN) */
13508#endif /* F_SETOWN_BUG */
13509
13510#ifdef SIGIO
13511 if (interrupt_input)
13512 init_sigio (connection);
13513#endif /* ! defined (SIGIO) */
13514
13515#ifdef USE_LUCID
13516#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13517 /* Make sure that we have a valid font for dialog boxes
13518 so that Xt does not crash. */
13519 {
13520 Display *dpy = dpyinfo->display;
13521 XrmValue d, fr, to;
13522 Font font;
13523 int count;
13524
13525 d.addr = (XPointer)&dpy;
13526 d.size = sizeof (Display *);
13527 fr.addr = XtDefaultFont;
13528 fr.size = sizeof (XtDefaultFont);
13529 to.size = sizeof (Font *);
13530 to.addr = (XPointer)&font;
13531 count = x_catch_errors (dpy);
13532 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13533 abort ();
13534 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13535 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13536 x_uncatch_errors (dpy, count);
13537 }
13538#endif
13539#endif
13540
13541 /* See if we should run in synchronous mode. This is useful
13542 for debugging X code. */
13543 {
13544 Lisp_Object value;
13545 value = display_x_get_resource (dpyinfo,
13546 build_string ("synchronous"),
13547 build_string ("Synchronous"),
13548 Qnil, Qnil);
13549 if (STRINGP (value)
13550 && (!strcmp (XSTRING (value)->data, "true")
13551 || !strcmp (XSTRING (value)->data, "on")))
13552 XSynchronize (dpyinfo->display, True);
13553 }
13554
13555 UNBLOCK_INPUT;
13556
13557 return dpyinfo;
13558}
13559\f
13560/* Get rid of display DPYINFO, assuming all frames are already gone,
13561 and without sending any more commands to the X server. */
13562
13563void
13564x_delete_display (dpyinfo)
13565 struct x_display_info *dpyinfo;
13566{
13567 delete_keyboard_wait_descriptor (dpyinfo->connection);
13568
13569 /* Discard this display from x_display_name_list and x_display_list.
13570 We can't use Fdelq because that can quit. */
13571 if (! NILP (x_display_name_list)
13572 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13573 x_display_name_list = XCDR (x_display_name_list);
13574 else
13575 {
13576 Lisp_Object tail;
13577
13578 tail = x_display_name_list;
13579 while (CONSP (tail) && CONSP (XCDR (tail)))
13580 {
13581 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13582 {
13583 XCDR (tail) = XCDR (XCDR (tail));
13584 break;
13585 }
13586 tail = XCDR (tail);
13587 }
13588 }
13589
13590 if (next_noop_dpyinfo == dpyinfo)
13591 next_noop_dpyinfo = dpyinfo->next;
13592
13593 if (x_display_list == dpyinfo)
13594 x_display_list = dpyinfo->next;
13595 else
13596 {
13597 struct x_display_info *tail;
13598
13599 for (tail = x_display_list; tail; tail = tail->next)
13600 if (tail->next == dpyinfo)
13601 tail->next = tail->next->next;
13602 }
13603
13604#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13605#ifndef AIX /* On AIX, XCloseDisplay calls this. */
13606 XrmDestroyDatabase (dpyinfo->xrdb);
13607#endif
13608#endif
13609#ifdef MULTI_KBOARD
13610 if (--dpyinfo->kboard->reference_count == 0)
13611 delete_kboard (dpyinfo->kboard);
13612#endif
13613#ifdef HAVE_X_I18N
13614 if (dpyinfo->xim)
13615 xim_close_dpy (dpyinfo);
13616#endif
13617
13618 xfree (dpyinfo->font_table);
13619 xfree (dpyinfo->x_id_name);
13620 xfree (dpyinfo);
13621}
13622\f
13623/* Set up use of X before we make the first connection. */
13624
13625static struct redisplay_interface x_redisplay_interface =
13626{
13627 x_produce_glyphs,
13628 x_write_glyphs,
13629 x_insert_glyphs,
13630 x_clear_end_of_line,
13631 x_scroll_run,
13632 x_after_update_window_line,
13633 x_update_window_begin,
13634 x_update_window_end,
13635 XTcursor_to,
13636 x_flush,
13637 x_clear_mouse_face,
13638 x_get_glyph_overhangs,
13639 x_fix_overlapping_area
13640};
13641
13642void
13643x_initialize ()
13644{
13645 rif = &x_redisplay_interface;
13646
13647 clear_frame_hook = x_clear_frame;
13648 ins_del_lines_hook = x_ins_del_lines;
13649 change_line_highlight_hook = x_change_line_highlight;
13650 delete_glyphs_hook = x_delete_glyphs;
13651 ring_bell_hook = XTring_bell;
13652 reset_terminal_modes_hook = XTreset_terminal_modes;
13653 set_terminal_modes_hook = XTset_terminal_modes;
13654 update_begin_hook = x_update_begin;
13655 update_end_hook = x_update_end;
13656 set_terminal_window_hook = XTset_terminal_window;
13657 read_socket_hook = XTread_socket;
13658 frame_up_to_date_hook = XTframe_up_to_date;
13659 reassert_line_highlight_hook = XTreassert_line_highlight;
13660 mouse_position_hook = XTmouse_position;
13661 frame_rehighlight_hook = XTframe_rehighlight;
13662 frame_raise_lower_hook = XTframe_raise_lower;
13663 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13664 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13665 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13666 judge_scroll_bars_hook = XTjudge_scroll_bars;
13667 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13668
13669 scroll_region_ok = 1; /* we'll scroll partial frames */
13670 char_ins_del_ok = 0; /* just as fast to write the line */
13671 line_ins_del_ok = 1; /* we'll just blt 'em */
13672 fast_clear_end_of_line = 1; /* X does this well */
13673 memory_below_frame = 0; /* we don't remember what scrolls
13674 off the bottom */
13675 baud_rate = 19200;
13676
13677 x_noop_count = 0;
13678 last_tool_bar_item = -1;
13679 any_help_event_p = 0;
13680
13681 /* Try to use interrupt input; if we can't, then start polling. */
13682 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13683
13684#ifdef USE_X_TOOLKIT
13685 XtToolkitInitialize ();
13686 Xt_app_con = XtCreateApplicationContext ();
13687 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13688
13689 /* Install an asynchronous timer that processes Xt timeout events
13690 every 0.1s. This is necessary because some widget sets use
13691 timeouts internally, for example the LessTif menu bar, or the
13692 Xaw3d scroll bar. When Xt timouts aren't processed, these
13693 widgets don't behave normally. */
13694 {
13695 EMACS_TIME interval;
13696 EMACS_SET_SECS_USECS (interval, 0, 100000);
13697 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13698 }
13699#endif
13700
13701#if USE_TOOLKIT_SCROLL_BARS
13702 xaw3d_arrow_scroll = False;
13703 xaw3d_pick_top = True;
13704#endif
13705
13706 /* Note that there is no real way portable across R3/R4 to get the
13707 original error handler. */
13708 XSetErrorHandler (x_error_handler);
13709 XSetIOErrorHandler (x_io_error_quitter);
13710
13711 /* Disable Window Change signals; they are handled by X events. */
13712#ifdef SIGWINCH
13713 signal (SIGWINCH, SIG_DFL);
13714#endif /* ! defined (SIGWINCH) */
13715
13716 signal (SIGPIPE, x_connection_signal);
13717}
13718
13719
13720void
13721syms_of_xterm ()
13722{
13723 staticpro (&x_error_message_string);
13724 x_error_message_string = Qnil;
13725
13726 staticpro (&x_display_name_list);
13727 x_display_name_list = Qnil;
13728
13729 staticpro (&last_mouse_scroll_bar);
13730 last_mouse_scroll_bar = Qnil;
13731
13732 staticpro (&Qvendor_specific_keysyms);
13733 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13734
13735 staticpro (&last_mouse_press_frame);
13736 last_mouse_press_frame = Qnil;
13737
13738 help_echo = Qnil;
13739 staticpro (&help_echo);
13740 help_echo_object = Qnil;
13741 staticpro (&help_echo_object);
13742 help_echo_window = Qnil;
13743 staticpro (&help_echo_window);
13744 previous_help_echo = Qnil;
13745 staticpro (&previous_help_echo);
13746 help_echo_pos = -1;
13747
13748 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13749 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13750For example, if a block cursor is over a tab, it will be drawn as\n\
13751wide as that tab on the display.");
13752 x_stretch_cursor_p = 0;
13753
13754 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13755 "If not nil, Emacs uses toolkit scroll bars.");
13756#if USE_TOOLKIT_SCROLL_BARS
13757 x_toolkit_scroll_bars_p = 1;
13758#else
13759 x_toolkit_scroll_bars_p = 0;
13760#endif
13761
13762 staticpro (&last_mouse_motion_frame);
13763 last_mouse_motion_frame = Qnil;
13764}
13765
13766#endif /* not HAVE_X_WINDOWS */
13767