(tar-copy): Supply MUSTBENEW arg to write-region.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
1c7e22fd 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
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
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
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
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
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
dc6f92b8
JB
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"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8 46
16bd92ea 47#ifndef USG
dc6f92b8
JB
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06
JB
52#endif /* makedev */
53#endif /* USG */
dc6f92b8 54
6df54671 55#ifdef BSD_SYSTEM
dc6f92b8 56#include <sys/ioctl.h>
6df54671 57#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 58
2d368234 59#include "systty.h"
3a2712f9 60#include "systime.h"
dc6f92b8 61
b8009dd1 62#ifndef INCLUDED_FCNTL
dc6f92b8 63#include <fcntl.h>
b8009dd1 64#endif
dc6f92b8
JB
65#include <ctype.h>
66#include <errno.h>
67#include <setjmp.h>
68#include <sys/stat.h>
a0a7635f
RS
69/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70/* #include <sys/param.h> */
dc6f92b8 71
dc43ef94
KH
72#include "charset.h"
73#include "ccl.h"
7a13e894 74#include "frame.h"
dc6f92b8 75#include "dispextern.h"
ee569018 76#include "fontset.h"
dc6f92b8
JB
77#include "termhooks.h"
78#include "termopts.h"
79#include "termchar.h"
80#if 0
81#include "sink.h"
82#include "sinkmask.h"
c118dd06 83#endif /* ! 0 */
dc6f92b8 84#include "gnu.h"
dc6f92b8 85#include "disptab.h"
dc6f92b8 86#include "buffer.h"
f451eb13 87#include "window.h"
3b2fa4e6 88#include "keyboard.h"
bde7c500 89#include "intervals.h"
dfcf069d 90#include "process.h"
bffcfca9 91#include "atimer.h"
dc6f92b8 92
d2bd6bc4
RS
93#ifdef USE_X_TOOLKIT
94#include <X11/Shell.h>
95#endif
96
06a2c219
GM
97#include <sys/types.h>
98#ifdef HAVE_SYS_TIME_H
99#include <sys/time.h>
100#endif
101#ifdef HAVE_UNISTD_H
102#include <unistd.h>
103#endif
104
3afe33e7 105#ifdef USE_X_TOOLKIT
06a2c219 106
9d7e2e3e 107extern void free_frame_menubar ();
2224b905 108extern FRAME_PTR x_menubar_window_to_frame ();
06a2c219 109
0fdff6bb
RS
110#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
111#define HACK_EDITRES
112extern void _XEditResCheckMessages ();
113#endif /* not NO_EDITRES */
06a2c219
GM
114
115/* Include toolkit specific headers for the scroll bar widget. */
116
117#ifdef USE_TOOLKIT_SCROLL_BARS
118#if defined USE_MOTIF
119#include <Xm/Xm.h> /* for LESSTIF_VERSION */
120#include <Xm/ScrollBar.h>
121#include <Xm/ScrollBarP.h>
ec18280f
SM
122#else /* !USE_MOTIF i.e. use Xaw */
123
124#ifdef HAVE_XAW3D
06a2c219 125#include <X11/Xaw3d/Simple.h>
06a2c219
GM
126#include <X11/Xaw3d/Scrollbar.h>
127#define ARROW_SCROLLBAR
128#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
129#else /* !HAVE_XAW3D */
130#include <X11/Xaw/Simple.h>
131#include <X11/Xaw/Scrollbar.h>
132#endif /* !HAVE_XAW3D */
133#ifndef XtNpickTop
134#define XtNpickTop "pickTop"
135#endif /* !XtNpickTop */
136#endif /* !USE_MOTIF */
06a2c219
GM
137#endif /* USE_TOOLKIT_SCROLL_BARS */
138
3afe33e7
RS
139#endif /* USE_X_TOOLKIT */
140
b849c413
RS
141#ifndef USE_X_TOOLKIT
142#define x_any_window_to_frame x_window_to_frame
5627c40e 143#define x_top_window_to_frame x_window_to_frame
b849c413
RS
144#endif
145
546e6d5b 146#ifdef USE_X_TOOLKIT
d067ea8b 147#include "widget.h"
546e6d5b
RS
148#ifndef XtNinitialState
149#define XtNinitialState "initialState"
150#endif
151#endif
152
80528801
KH
153#ifdef SOLARIS2
154/* memmove will be defined as a macro in Xfuncs.h unless
155 <string.h> is included beforehand. The declaration for memmove in
156 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
157#include <string.h>
158#endif
159
e4b68333 160#ifndef min
06a2c219 161#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
162#endif
163#ifndef max
06a2c219
GM
164#define max(a,b) ((a) > (b) ? (a) : (b))
165#endif
166
167#define abs(x) ((x) < 0 ? -(x) : (x))
168
169#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
170
171\f
172/* Bitmaps for truncated lines. */
173
174enum bitmap_type
175{
176 NO_BITMAP,
177 LEFT_TRUNCATION_BITMAP,
178 RIGHT_TRUNCATION_BITMAP,
179 OVERLAY_ARROW_BITMAP,
180 CONTINUED_LINE_BITMAP,
181 CONTINUATION_LINE_BITMAP,
182 ZV_LINE_BITMAP
183};
184
185/* Bitmap drawn to indicate lines not displaying text if
186 `indicate-empty-lines' is non-nil. */
187
188#define zv_width 8
189#define zv_height 8
190static unsigned char zv_bits[] = {
191 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
192
193/* An arrow like this: `<-'. */
194
195#define left_width 8
196#define left_height 8
197static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
199
110859fc
GM
200/* Right truncation arrow bitmap `->'. */
201
202#define right_width 8
203#define right_height 8
204static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
206
06a2c219
GM
207/* Marker for continued lines. */
208
209#define continued_width 8
210#define continued_height 8
211static unsigned char continued_bits[] = {
110859fc
GM
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
213
214/* Marker for continuation lines. */
06a2c219
GM
215
216#define continuation_width 8
217#define continuation_height 8
218static unsigned char continuation_bits[] = {
110859fc 219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 220
110859fc 221/* Overlay arrow bitmap. */
06a2c219 222
110859fc
GM
223#if 0
224/* A bomb. */
06a2c219
GM
225#define ov_width 8
226#define ov_height 8
227static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 229#else
110859fc 230/* A triangular arrow. */
06a2c219
GM
231#define ov_width 8
232#define ov_height 8
233static unsigned char ov_bits[] = {
110859fc
GM
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
235
e4b68333 236#endif
06a2c219
GM
237
238extern Lisp_Object Qhelp_echo;
239
69388238 240\f
06a2c219
GM
241/* Non-zero means Emacs uses toolkit scroll bars. */
242
243int x_toolkit_scroll_bars_p;
244
245/* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
247
248static Lisp_Object help_echo;
249
250/* Temporary variable for XTread_socket. */
251
252static Lisp_Object previous_help_echo;
253
254/* Non-zero means that a HELP_EVENT has been generated since Emacs
255 start. */
256
257static int any_help_event_p;
258
259/* Non-zero means draw block and hollow cursor as wide as the glyph
260 under it. For example, if a block cursor is over a tab, it will be
261 drawn as wide as that tab on the display. */
262
263int x_stretch_cursor_p;
264
265/* This is a chain of structures for all the X displays currently in
266 use. */
267
334208b7 268struct x_display_info *x_display_list;
dc6f92b8 269
06a2c219
GM
270/* This is a list of cons cells, each of the form (NAME
271 . FONT-LIST-CACHE), one for each element of x_display_list and in
272 the same order. NAME is the name of the frame. FONT-LIST-CACHE
273 records previous values returned by x-list-fonts. */
274
7a13e894 275Lisp_Object x_display_name_list;
f451eb13 276
987d2ad1 277/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
278 This is set by update_begin and looked at by all the XT functions.
279 It is zero while not inside an update. In that case, the XT
280 functions assume that `selected_frame' is the frame to apply to. */
281
d0386f2a 282extern struct frame *updating_frame;
dc6f92b8 283
dfcf069d 284extern int waiting_for_input;
0e81d8cd 285
06a2c219
GM
286/* This is a frame waiting to be auto-raised, within XTread_socket. */
287
0134a210
RS
288struct frame *pending_autoraise_frame;
289
7f9c7f94
RS
290#ifdef USE_X_TOOLKIT
291/* The application context for Xt use. */
292XtAppContext Xt_app_con;
06a2c219
GM
293static String Xt_default_resources[] = {0};
294#endif /* USE_X_TOOLKIT */
665881ad 295
06a2c219
GM
296/* Nominal cursor position -- where to draw output.
297 HPOS and VPOS are window relative glyph matrix coordinates.
298 X and Y are window relative pixel coordinates. */
dc6f92b8 299
06a2c219 300struct cursor_pos output_cursor;
dc6f92b8 301
bffcfca9
GM
302/* Non-zero means user is interacting with a toolkit scroll bar. */
303
304static int toolkit_scroll_bar_interaction;
dc6f92b8 305
69388238
RS
306/* Mouse movement.
307
06a2c219 308 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
309 so that we would have to call XQueryPointer after each MotionNotify
310 event to ask for another such event. However, this made mouse tracking
311 slow, and there was a bug that made it eventually stop.
312
313 Simply asking for MotionNotify all the time seems to work better.
314
69388238
RS
315 In order to avoid asking for motion events and then throwing most
316 of them away or busy-polling the server for mouse positions, we ask
317 the server for pointer motion hints. This means that we get only
318 one event per group of mouse movements. "Groups" are delimited by
319 other kinds of events (focus changes and button clicks, for
320 example), or by XQueryPointer calls; when one of these happens, we
321 get another MotionNotify event the next time the mouse moves. This
322 is at least as efficient as getting motion events when mouse
323 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 324 is off. */
69388238
RS
325
326/* Where the mouse was last time we reported a mouse event. */
69388238 327
06a2c219
GM
328FRAME_PTR last_mouse_frame;
329static XRectangle last_mouse_glyph;
2237cac9
RS
330static Lisp_Object last_mouse_press_frame;
331
69388238
RS
332/* The scroll bar in which the last X motion event occurred.
333
06a2c219
GM
334 If the last X motion event occurred in a scroll bar, we set this so
335 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
336 an ordinary motion.
337
06a2c219
GM
338 If the last X motion event didn't occur in a scroll bar, we set
339 this to Qnil, to tell XTmouse_position to return an ordinary motion
340 event. */
341
69388238
RS
342static Lisp_Object last_mouse_scroll_bar;
343
69388238
RS
344/* This is a hack. We would really prefer that XTmouse_position would
345 return the time associated with the position it returns, but there
06a2c219 346 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
347 along with the position query. So, we just keep track of the time
348 of the last movement we received, and return that in hopes that
349 it's somewhat accurate. */
06a2c219 350
69388238
RS
351static Time last_mouse_movement_time;
352
06a2c219
GM
353/* Incremented by XTread_socket whenever it really tries to read
354 events. */
355
c0a04927
RS
356#ifdef __STDC__
357static int volatile input_signal_count;
358#else
359static int input_signal_count;
360#endif
361
7a13e894 362/* Used locally within XTread_socket. */
06a2c219 363
7a13e894 364static int x_noop_count;
dc6f92b8 365
7a13e894 366/* Initial values of argv and argc. */
06a2c219 367
7a13e894
RS
368extern char **initial_argv;
369extern int initial_argc;
dc6f92b8 370
7a13e894 371extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 372
06a2c219 373/* Tells if a window manager is present or not. */
7a13e894
RS
374
375extern Lisp_Object Vx_no_window_manager;
dc6f92b8 376
c2df547c 377extern Lisp_Object Qface, Qmouse_face;
b8009dd1 378
dc6f92b8
JB
379extern int errno;
380
dfeccd2d 381/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 382
64bb1782
RS
383extern int extra_keyboard_modifiers;
384
59e755be
KH
385static Lisp_Object Qvendor_specific_keysyms;
386
334208b7 387extern XrmDatabase x_load_resources ();
c32cdd9a
KH
388extern Lisp_Object x_icon_type ();
389
7a13e894 390
06a2c219
GM
391/* Enumeration for overriding/changing the face to use for drawing
392 glyphs in x_draw_glyphs. */
393
394enum draw_glyphs_face
395{
396 DRAW_NORMAL_TEXT,
397 DRAW_INVERSE_VIDEO,
398 DRAW_CURSOR,
399 DRAW_MOUSE_FACE,
400 DRAW_IMAGE_RAISED,
401 DRAW_IMAGE_SUNKEN
402};
403
404static void x_update_window_end P_ ((struct window *, int));
405static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
406void x_delete_display P_ ((struct x_display_info *));
407static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
408 unsigned));
409static int fast_find_position P_ ((struct window *, int, int *, int *,
410 int *, int *));
411static void set_output_cursor P_ ((struct cursor_pos *));
412static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
413 int *, int *, int *));
414static void note_mode_line_highlight P_ ((struct window *, int, int));
415static void x_check_font P_ ((struct frame *, XFontStruct *));
416static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
417static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
418static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
419static void show_mouse_face P_ ((struct x_display_info *,
420 enum draw_glyphs_face));
421static int x_io_error_quitter P_ ((Display *));
422int x_catch_errors P_ ((Display *));
423void x_uncatch_errors P_ ((Display *, int));
424void x_lower_frame P_ ((struct frame *));
425void x_scroll_bar_clear P_ ((struct frame *));
426int x_had_errors_p P_ ((Display *));
427void x_wm_set_size_hint P_ ((struct frame *, long, int));
428void x_raise_frame P_ ((struct frame *));
429void x_set_window_size P_ ((struct frame *, int, int, int));
430void x_wm_set_window_state P_ ((struct frame *, int));
431void x_wm_set_icon_pixmap P_ ((struct frame *, int));
432void x_initialize P_ ((void));
433static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
434static int x_compute_min_glyph_bounds P_ ((struct frame *));
435static void x_draw_phys_cursor_glyph P_ ((struct window *,
436 struct glyph_row *,
437 enum draw_glyphs_face));
438static void x_update_end P_ ((struct frame *));
439static void XTframe_up_to_date P_ ((struct frame *));
440static void XTreassert_line_highlight P_ ((int, int));
441static void x_change_line_highlight P_ ((int, int, int, int));
442static void XTset_terminal_modes P_ ((void));
443static void XTreset_terminal_modes P_ ((void));
444static void XTcursor_to P_ ((int, int, int, int));
445static void x_write_glyphs P_ ((struct glyph *, int));
446static void x_clear_end_of_line P_ ((int));
447static void x_clear_frame P_ ((void));
448static void x_clear_cursor P_ ((struct window *));
449static void frame_highlight P_ ((struct frame *));
450static void frame_unhighlight P_ ((struct frame *));
451static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
452static void XTframe_rehighlight P_ ((struct frame *));
453static void x_frame_rehighlight P_ ((struct x_display_info *));
454static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 455static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
456static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
457 XRectangle *));
458static void expose_frame P_ ((struct frame *, int, int, int, int));
459static void expose_window_tree P_ ((struct window *, XRectangle *));
460static void expose_window P_ ((struct window *, XRectangle *));
461static void expose_area P_ ((struct window *, struct glyph_row *,
462 XRectangle *, enum glyph_row_area));
463static void expose_line P_ ((struct window *, struct glyph_row *,
464 XRectangle *));
465static void x_update_cursor_in_window_tree P_ ((struct window *, int));
466static void x_update_window_cursor P_ ((struct window *, int));
467static void x_erase_phys_cursor P_ ((struct window *));
468void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
469static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
470 enum bitmap_type));
471
472static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
473 GC, int));
474static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
475static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
476static void note_overwritten_text_cursor P_ ((struct window *, int, int));
477static void x_flush P_ ((struct frame *f));
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
dc6f92b8 498
06a2c219
GM
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
b8009dd1 507
334208b7 508\f
06a2c219
GM
509/***********************************************************************
510 Debugging
511 ***********************************************************************/
512
9382638d 513#if 0
06a2c219
GM
514
515/* This is a function useful for recording debugging information about
516 the sequence of occurrences in this file. */
9382638d
KH
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 */
06a2c219
GM
541
542
9382638d 543\f
334208b7
RS
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;
16bd92ea 555
334208b7
RS
556 return 0;
557}
f451eb13 558
06a2c219
GM
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. */
dc6f92b8 570
dfcf069d 571static void
06a2c219 572x_update_begin (f)
f676886a 573 struct frame *f;
58769bee 574{
06a2c219
GM
575 /* Nothing to do. */
576}
dc6f92b8 577
dc6f92b8 578
06a2c219
GM
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. */
dc6f92b8 582
06a2c219
GM
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);
b8009dd1 592
06a2c219 593 BLOCK_INPUT;
d1bc4182 594
06a2c219 595 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 596 {
514e4681 597 /* Don't do highlighting for mouse motion during the update. */
06a2c219 598 display_info->mouse_face_defer = 1;
37c2c98b 599
06a2c219
GM
600 /* If F needs to be redrawn, simply forget about any prior mouse
601 highlighting. */
9f67f20b 602 if (FRAME_GARBAGED_P (f))
06a2c219
GM
603 display_info->mouse_face_window = Qnil;
604
605 /* Can we tell that this update does not affect the window
606 where the mouse highlight is? If so, no need to turn off.
607 Likewise, don't do anything if the frame is garbaged;
608 in that case, the frame's current matrix that we would use
609 is all wrong, and we will redisplay that line anyway. */
610 if (!NILP (display_info->mouse_face_window)
611 && w == XWINDOW (display_info->mouse_face_window))
514e4681 612 {
06a2c219 613 int i;
514e4681 614
06a2c219
GM
615 for (i = 0; i < w->desired_matrix->nrows; ++i)
616 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
617 break;
618
06a2c219
GM
619 if (i < w->desired_matrix->nrows)
620 clear_mouse_face (display_info);
514e4681 621 }
b8009dd1 622 }
6ccf47d1 623
dc6f92b8
JB
624 UNBLOCK_INPUT;
625}
626
06a2c219
GM
627
628/* Draw a vertical window border to the right of window W if W doesn't
629 have vertical scroll bars. */
630
dfcf069d 631static void
06a2c219
GM
632x_draw_vertical_border (w)
633 struct window *w;
58769bee 634{
06a2c219
GM
635 struct frame *f = XFRAME (WINDOW_FRAME (w));
636
637 /* Redraw borders between horizontally adjacent windows. Don't
638 do it for frames with vertical scroll bars because either the
639 right scroll bar of a window, or the left scroll bar of its
640 neighbor will suffice as a border. */
641 if (!WINDOW_RIGHTMOST_P (w)
642 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
643 {
644 int x0, x1, y0, y1;
dc6f92b8 645
06a2c219 646 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 647 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
648 y1 -= 1;
649
650 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
651 f->output_data.x->normal_gc, x1, y0, x1, y1);
652 }
653}
654
655
656/* End update of window W (which is equal to updated_window). Draw
657 vertical borders between horizontally adjacent windows, and display
658 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
659 pseudo-window in case we don't have X toolkit support. Such
660 windows don't have a cursor, so don't display it here. */
661
662static void
663x_update_window_end (w, cursor_on_p)
664 struct window *w;
665 int cursor_on_p;
666{
667 if (!w->pseudo_window_p)
668 {
669 BLOCK_INPUT;
670 if (cursor_on_p)
671 x_display_and_set_cursor (w, 1, output_cursor.hpos,
672 output_cursor.vpos,
673 output_cursor.x, output_cursor.y);
674 x_draw_vertical_border (w);
675 UNBLOCK_INPUT;
676 }
677
678 updated_window = NULL;
679}
dc6f92b8 680
dc6f92b8 681
06a2c219
GM
682/* End update of frame F. This function is installed as a hook in
683 update_end. */
684
685static void
686x_update_end (f)
687 struct frame *f;
688{
689 /* Mouse highlight may be displayed again. */
aa8bff2e 690 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 691
06a2c219 692 BLOCK_INPUT;
334208b7 693 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
694 UNBLOCK_INPUT;
695}
b8009dd1 696
06a2c219
GM
697
698/* This function is called from various places in xdisp.c whenever a
699 complete update has been performed. The global variable
700 updated_window is not available here. */
b8009dd1 701
dfcf069d 702static void
b8009dd1 703XTframe_up_to_date (f)
06a2c219 704 struct frame *f;
b8009dd1 705{
06a2c219 706 if (FRAME_X_P (f))
514e4681 707 {
06a2c219
GM
708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
709 if (dpyinfo->mouse_face_deferred_gc
710 || f == dpyinfo->mouse_face_mouse_frame)
711 {
712 BLOCK_INPUT;
713 if (dpyinfo->mouse_face_mouse_frame)
714 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
715 dpyinfo->mouse_face_mouse_x,
716 dpyinfo->mouse_face_mouse_y);
717 dpyinfo->mouse_face_deferred_gc = 0;
718 UNBLOCK_INPUT;
719 }
514e4681 720 }
b8009dd1 721}
06a2c219
GM
722
723
724/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
725 arrow bitmaps, or clear the areas where they would be displayed
726 before DESIRED_ROW is made current. The window being updated is
727 found in updated_window. This function It is called from
728 update_window_line only if it is known that there are differences
729 between bitmaps to be drawn between current row and DESIRED_ROW. */
730
731static void
732x_after_update_window_line (desired_row)
733 struct glyph_row *desired_row;
734{
735 struct window *w = updated_window;
736
737 xassert (w);
738
739 if (!desired_row->mode_line_p && !w->pseudo_window_p)
740 {
741 BLOCK_INPUT;
742 x_draw_row_bitmaps (w, desired_row);
743
744 /* When a window has disappeared, make sure that no rest of
745 full-width rows stays visible in the internal border. */
746 if (windows_or_buffers_changed)
747 {
748 struct frame *f = XFRAME (w->frame);
749 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
750 int height = desired_row->visible_height;
110859fc
GM
751 int x = (window_box_right (w, -1)
752 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
753 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
754
755 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
756 x, y, width, height, False);
757 }
758
759 UNBLOCK_INPUT;
760 }
761}
762
763
764/* Draw the bitmap WHICH in one of the areas to the left or right of
765 window W. ROW is the glyph row for which to display the bitmap; it
766 determines the vertical position at which the bitmap has to be
767 drawn. */
768
769static void
770x_draw_bitmap (w, row, which)
771 struct window *w;
772 struct glyph_row *row;
773 enum bitmap_type which;
774{
775 struct frame *f = XFRAME (WINDOW_FRAME (w));
776 Display *display = FRAME_X_DISPLAY (f);
777 Window window = FRAME_X_WINDOW (f);
778 int x, y, wd, h, dy;
779 unsigned char *bits;
780 Pixmap pixmap;
781 GC gc = f->output_data.x->normal_gc;
782 struct face *face;
783 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
784
785 /* Must clip because of partially visible lines. */
786 x_clip_to_row (w, row, gc, 1);
787
788 switch (which)
789 {
790 case LEFT_TRUNCATION_BITMAP:
791 wd = left_width;
792 h = left_height;
793 bits = left_bits;
794 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
795 - wd
110859fc 796 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
797 break;
798
799 case OVERLAY_ARROW_BITMAP:
800 wd = left_width;
801 h = left_height;
802 bits = ov_bits;
803 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
804 - wd
110859fc 805 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
806 break;
807
808 case RIGHT_TRUNCATION_BITMAP:
809 wd = right_width;
810 h = right_height;
811 bits = right_bits;
812 x = window_box_right (w, -1);
110859fc 813 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
814 break;
815
816 case CONTINUED_LINE_BITMAP:
817 wd = right_width;
818 h = right_height;
819 bits = continued_bits;
820 x = window_box_right (w, -1);
110859fc 821 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
822 break;
823
824 case CONTINUATION_LINE_BITMAP:
825 wd = continuation_width;
826 h = continuation_height;
827 bits = continuation_bits;
828 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
829 - wd
110859fc 830 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
831 break;
832
833 case ZV_LINE_BITMAP:
834 wd = zv_width;
835 h = zv_height;
836 bits = zv_bits;
837 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
838 - wd
110859fc 839 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
840 break;
841
842 default:
843 abort ();
844 }
845
846 /* Convert to frame coordinates. Set dy to the offset in the row to
847 start drawing the bitmap. */
848 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
849 dy = (row->height - h) / 2;
850
851 /* Draw the bitmap. I believe these small pixmaps can be cached
852 by the server. */
853 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
854 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
855 face->foreground,
856 face->background, depth);
857 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
858 XFreePixmap (display, pixmap);
859 XSetClipMask (display, gc, None);
860}
861
862
863/* Draw flags bitmaps for glyph row ROW on window W. Call this
864 function with input blocked. */
865
866static void
867x_draw_row_bitmaps (w, row)
868 struct window *w;
869 struct glyph_row *row;
870{
871 struct frame *f = XFRAME (w->frame);
872 enum bitmap_type bitmap;
873 struct face *face;
045dee35 874 int header_line_height = -1;
06a2c219
GM
875
876 xassert (interrupt_input_blocked);
877
878 /* If row is completely invisible, because of vscrolling, we
879 don't have to draw anything. */
880 if (row->visible_height <= 0)
881 return;
882
883 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
884 PREPARE_FACE_FOR_DISPLAY (f, face);
885
886 /* Decide which bitmap to draw at the left side. */
887 if (row->overlay_arrow_p)
888 bitmap = OVERLAY_ARROW_BITMAP;
889 else if (row->truncated_on_left_p)
890 bitmap = LEFT_TRUNCATION_BITMAP;
891 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
892 bitmap = CONTINUATION_LINE_BITMAP;
893 else if (row->indicate_empty_line_p)
894 bitmap = ZV_LINE_BITMAP;
895 else
896 bitmap = NO_BITMAP;
897
898 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
899 the flags area. */
900 if (bitmap == NO_BITMAP
110859fc 901 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
902 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
903 {
904 /* If W has a vertical border to its left, don't draw over it. */
905 int border = ((XFASTINT (w->left) > 0
906 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
907 ? 1 : 0);
908 int left = window_box_left (w, -1);
909
045dee35
GM
910 if (header_line_height < 0)
911 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
912
913 /* In case the same realized face is used for bitmap areas and
914 for something displayed in the text (e.g. face `region' on
915 mono-displays, the fill style may have been changed to
916 FillSolid in x_draw_glyph_string_background. */
917 if (face->stipple)
918 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
919 else
920 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
921
06a2c219
GM
922 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
923 face->gc,
924 (left
110859fc 925 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 926 + border),
045dee35 927 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 928 row->y)),
110859fc 929 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 930 row->visible_height);
dcd08bfb
GM
931 if (!face->stipple)
932 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
933 }
934
935 /* Draw the left bitmap. */
936 if (bitmap != NO_BITMAP)
937 x_draw_bitmap (w, row, bitmap);
938
939 /* Decide which bitmap to draw at the right side. */
940 if (row->truncated_on_right_p)
941 bitmap = RIGHT_TRUNCATION_BITMAP;
942 else if (row->continued_p)
943 bitmap = CONTINUED_LINE_BITMAP;
944 else
945 bitmap = NO_BITMAP;
946
947 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
948 the flags area. */
949 if (bitmap == NO_BITMAP
110859fc 950 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
951 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
952 {
953 int right = window_box_right (w, -1);
954
045dee35
GM
955 if (header_line_height < 0)
956 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
957
958 /* In case the same realized face is used for bitmap areas and
959 for something displayed in the text (e.g. face `region' on
960 mono-displays, the fill style may have been changed to
961 FillSolid in x_draw_glyph_string_background. */
962 if (face->stipple)
963 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
964 else
965 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
966 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
967 face->gc,
968 right,
045dee35 969 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 970 row->y)),
110859fc 971 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 972 row->visible_height);
dcd08bfb
GM
973 if (!face->stipple)
974 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
975 }
976
977 /* Draw the right bitmap. */
978 if (bitmap != NO_BITMAP)
979 x_draw_bitmap (w, row, bitmap);
980}
981
dc6f92b8 982\f
06a2c219
GM
983/***********************************************************************
984 Line Highlighting
985 ***********************************************************************/
dc6f92b8 986
06a2c219
GM
987/* External interface to control of standout mode. Not used for X
988 frames. Aborts when called. */
989
990static void
dc6f92b8
JB
991XTreassert_line_highlight (new, vpos)
992 int new, vpos;
993{
06a2c219 994 abort ();
dc6f92b8
JB
995}
996
06a2c219
GM
997
998/* Call this when about to modify line at position VPOS and change
999 whether it is highlighted. Not used for X frames. Aborts when
1000 called. */
dc6f92b8 1001
dfcf069d 1002static void
06a2c219
GM
1003x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1004 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1005{
06a2c219 1006 abort ();
dc6f92b8
JB
1007}
1008
06a2c219
GM
1009
1010/* This is called when starting Emacs and when restarting after
1011 suspend. When starting Emacs, no X window is mapped. And nothing
1012 must be done to Emacs's own window if it is suspended (though that
1013 rarely happens). */
dc6f92b8 1014
dfcf069d 1015static void
dc6f92b8
JB
1016XTset_terminal_modes ()
1017{
1018}
1019
06a2c219
GM
1020/* This is called when exiting or suspending Emacs. Exiting will make
1021 the X-windows go away, and suspending requires no action. */
dc6f92b8 1022
dfcf069d 1023static void
dc6f92b8
JB
1024XTreset_terminal_modes ()
1025{
dc6f92b8 1026}
06a2c219
GM
1027
1028
dc6f92b8 1029\f
06a2c219
GM
1030/***********************************************************************
1031 Output Cursor
1032 ***********************************************************************/
1033
1034/* Set the global variable output_cursor to CURSOR. All cursor
1035 positions are relative to updated_window. */
dc6f92b8 1036
dfcf069d 1037static void
06a2c219
GM
1038set_output_cursor (cursor)
1039 struct cursor_pos *cursor;
dc6f92b8 1040{
06a2c219
GM
1041 output_cursor.hpos = cursor->hpos;
1042 output_cursor.vpos = cursor->vpos;
1043 output_cursor.x = cursor->x;
1044 output_cursor.y = cursor->y;
1045}
1046
1047
1048/* Set a nominal cursor position.
dc6f92b8 1049
06a2c219
GM
1050 HPOS and VPOS are column/row positions in a window glyph matrix. X
1051 and Y are window text area relative pixel positions.
1052
1053 If this is done during an update, updated_window will contain the
1054 window that is being updated and the position is the future output
1055 cursor position for that window. If updated_window is null, use
1056 selected_window and display the cursor at the given position. */
1057
1058static void
1059XTcursor_to (vpos, hpos, y, x)
1060 int vpos, hpos, y, x;
1061{
1062 struct window *w;
1063
1064 /* If updated_window is not set, work on selected_window. */
1065 if (updated_window)
1066 w = updated_window;
1067 else
1068 w = XWINDOW (selected_window);
dbcb258a 1069
06a2c219
GM
1070 /* Set the output cursor. */
1071 output_cursor.hpos = hpos;
1072 output_cursor.vpos = vpos;
1073 output_cursor.x = x;
1074 output_cursor.y = y;
dc6f92b8 1075
06a2c219
GM
1076 /* If not called as part of an update, really display the cursor.
1077 This will also set the cursor position of W. */
1078 if (updated_window == NULL)
dc6f92b8
JB
1079 {
1080 BLOCK_INPUT;
06a2c219 1081 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1082 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1083 UNBLOCK_INPUT;
1084 }
1085}
dc43ef94 1086
06a2c219
GM
1087
1088\f
1089/***********************************************************************
1090 Display Iterator
1091 ***********************************************************************/
1092
1093/* Function prototypes of this page. */
1094
1095static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1096 struct glyph *,
ee569018
KH
1097 XChar2b *,
1098 int *));
06a2c219
GM
1099static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1100 int, XChar2b *, int));
1101static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1102static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1103static void x_append_glyph P_ ((struct it *));
b4192550 1104static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1105static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1106 int, int, double));
1107static void x_produce_glyphs P_ ((struct it *));
06a2c219 1108static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1109
1110
1111/* Return a pointer to per-char metric information in FONT of a
1112 character pointed by B which is a pointer to an XChar2b. */
1113
1114#define PER_CHAR_METRIC(font, b) \
1115 ((font)->per_char \
1116 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1117 + (((font)->min_byte1 || (font)->max_byte1) \
1118 ? (((b)->byte1 - (font)->min_byte1) \
1119 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1120 : 0)) \
1121 : &((font)->max_bounds))
dc43ef94 1122
dc6f92b8 1123
e2ef8ee6
GM
1124/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1125 is not contained in the font. */
dc43ef94 1126
06a2c219 1127static INLINE XCharStruct *
ee569018 1128x_per_char_metric (font, char2b)
06a2c219
GM
1129 XFontStruct *font;
1130 XChar2b *char2b;
1131{
1132 /* The result metric information. */
1133 XCharStruct *pcm = NULL;
dc6f92b8 1134
06a2c219 1135 xassert (font && char2b);
dc6f92b8 1136
06a2c219 1137 if (font->per_char != NULL)
dc6f92b8 1138 {
06a2c219 1139 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1140 {
06a2c219
GM
1141 /* min_char_or_byte2 specifies the linear character index
1142 corresponding to the first element of the per_char array,
1143 max_char_or_byte2 is the index of the last character. A
1144 character with non-zero CHAR2B->byte1 is not in the font.
1145 A character with byte2 less than min_char_or_byte2 or
1146 greater max_char_or_byte2 is not in the font. */
1147 if (char2b->byte1 == 0
1148 && char2b->byte2 >= font->min_char_or_byte2
1149 && char2b->byte2 <= font->max_char_or_byte2)
1150 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1151 }
06a2c219 1152 else
dc6f92b8 1153 {
06a2c219
GM
1154 /* If either min_byte1 or max_byte1 are nonzero, both
1155 min_char_or_byte2 and max_char_or_byte2 are less than
1156 256, and the 2-byte character index values corresponding
1157 to the per_char array element N (counting from 0) are:
1158
1159 byte1 = N/D + min_byte1
1160 byte2 = N\D + min_char_or_byte2
1161
1162 where:
1163
1164 D = max_char_or_byte2 - min_char_or_byte2 + 1
1165 / = integer division
1166 \ = integer modulus */
1167 if (char2b->byte1 >= font->min_byte1
1168 && char2b->byte1 <= font->max_byte1
1169 && char2b->byte2 >= font->min_char_or_byte2
1170 && char2b->byte2 <= font->max_char_or_byte2)
1171 {
1172 pcm = (font->per_char
1173 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1174 * (char2b->byte1 - font->min_byte1))
1175 + (char2b->byte2 - font->min_char_or_byte2));
1176 }
dc6f92b8 1177 }
06a2c219
GM
1178 }
1179 else
1180 {
1181 /* If the per_char pointer is null, all glyphs between the first
1182 and last character indexes inclusive have the same
1183 information, as given by both min_bounds and max_bounds. */
1184 if (char2b->byte2 >= font->min_char_or_byte2
1185 && char2b->byte2 <= font->max_char_or_byte2)
1186 pcm = &font->max_bounds;
1187 }
dc6f92b8 1188
ee569018
KH
1189 return ((pcm == NULL
1190 || pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1191 ? NULL : pcm);
06a2c219 1192}
b73b6aaf 1193
57b03282 1194
06a2c219
GM
1195/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1196 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1197
06a2c219
GM
1198static INLINE void
1199x_encode_char (c, char2b, font_info)
1200 int c;
1201 XChar2b *char2b;
1202 struct font_info *font_info;
1203{
1204 int charset = CHAR_CHARSET (c);
1205 XFontStruct *font = font_info->font;
1206
1207 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1208 This may be either a program in a special encoder language or a
1209 fixed encoding. */
1210 if (font_info->font_encoder)
1211 {
1212 /* It's a program. */
1213 struct ccl_program *ccl = font_info->font_encoder;
1214
1215 if (CHARSET_DIMENSION (charset) == 1)
1216 {
1217 ccl->reg[0] = charset;
1218 ccl->reg[1] = char2b->byte2;
1219 }
1220 else
1221 {
1222 ccl->reg[0] = charset;
1223 ccl->reg[1] = char2b->byte1;
1224 ccl->reg[2] = char2b->byte2;
1225 }
1226
1227 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1228
1229 /* We assume that MSBs are appropriately set/reset by CCL
1230 program. */
1231 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1232 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1233 else
1234 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1235 }
1236 else if (font_info->encoding[charset])
1237 {
1238 /* Fixed encoding scheme. See fontset.h for the meaning of the
1239 encoding numbers. */
1240 int enc = font_info->encoding[charset];
1241
1242 if ((enc == 1 || enc == 2)
1243 && CHARSET_DIMENSION (charset) == 2)
1244 char2b->byte1 |= 0x80;
1245
1246 if (enc == 1 || enc == 3)
1247 char2b->byte2 |= 0x80;
1248 }
1249}
1250
1251
1252/* Get face and two-byte form of character C in face FACE_ID on frame
1253 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1254 means we want to display multibyte text. Value is a pointer to a
1255 realized face that is ready for display. */
1256
1257static INLINE struct face *
1258x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1259 struct frame *f;
1260 int c, face_id;
1261 XChar2b *char2b;
1262 int multibyte_p;
1263{
1264 struct face *face = FACE_FROM_ID (f, face_id);
1265
1266 if (!multibyte_p)
1267 {
1268 /* Unibyte case. We don't have to encode, but we have to make
1269 sure to use a face suitable for unibyte. */
1270 char2b->byte1 = 0;
1271 char2b->byte2 = c;
ee569018
KH
1272 face_id = FACE_FOR_CHAR (f, face, c);
1273 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1274 }
1275 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1276 {
1277 /* Case of ASCII in a face known to fit ASCII. */
1278 char2b->byte1 = 0;
1279 char2b->byte2 = c;
1280 }
1281 else
1282 {
1283 int c1, c2, charset;
1284
1285 /* Split characters into bytes. If c2 is -1 afterwards, C is
1286 really a one-byte character so that byte1 is zero. */
1287 SPLIT_CHAR (c, charset, c1, c2);
1288 if (c2 > 0)
1289 char2b->byte1 = c1, char2b->byte2 = c2;
1290 else
1291 char2b->byte1 = 0, char2b->byte2 = c1;
1292
06a2c219 1293 /* Maybe encode the character in *CHAR2B. */
ee569018 1294 if (face->font != NULL)
06a2c219
GM
1295 {
1296 struct font_info *font_info
1297 = FONT_INFO_FROM_ID (f, face->font_info_id);
1298 if (font_info)
ee569018 1299 x_encode_char (c, char2b, font_info);
06a2c219
GM
1300 }
1301 }
1302
1303 /* Make sure X resources of the face are allocated. */
1304 xassert (face != NULL);
1305 PREPARE_FACE_FOR_DISPLAY (f, face);
1306
1307 return face;
1308}
1309
1310
1311/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1312 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1313 a pointer to a realized face that is ready for display. */
1314
1315static INLINE struct face *
ee569018 1316x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1317 struct frame *f;
1318 struct glyph *glyph;
1319 XChar2b *char2b;
ee569018 1320 int *two_byte_p;
06a2c219
GM
1321{
1322 struct face *face;
1323
1324 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1325 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1326
ee569018
KH
1327 if (two_byte_p)
1328 *two_byte_p = 0;
1329
06a2c219
GM
1330 if (!glyph->multibyte_p)
1331 {
1332 /* Unibyte case. We don't have to encode, but we have to make
1333 sure to use a face suitable for unibyte. */
1334 char2b->byte1 = 0;
43d120d8 1335 char2b->byte2 = glyph->u.ch;
06a2c219 1336 }
43d120d8
KH
1337 else if (glyph->u.ch < 128
1338 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1339 {
1340 /* Case of ASCII in a face known to fit ASCII. */
1341 char2b->byte1 = 0;
43d120d8 1342 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1343 }
1344 else
1345 {
1346 int c1, c2, charset;
1347
1348 /* Split characters into bytes. If c2 is -1 afterwards, C is
1349 really a one-byte character so that byte1 is zero. */
43d120d8 1350 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1351 if (c2 > 0)
1352 char2b->byte1 = c1, char2b->byte2 = c2;
1353 else
1354 char2b->byte1 = 0, char2b->byte2 = c1;
1355
1356 /* Maybe encode the character in *CHAR2B. */
1357 if (charset != CHARSET_ASCII)
1358 {
1359 struct font_info *font_info
1360 = FONT_INFO_FROM_ID (f, face->font_info_id);
1361 if (font_info)
1362 {
43d120d8 1363 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1364 if (two_byte_p)
1365 *two_byte_p
1366 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1367 }
1368 }
1369 }
1370
1371 /* Make sure X resources of the face are allocated. */
1372 xassert (face != NULL);
1373 PREPARE_FACE_FOR_DISPLAY (f, face);
1374 return face;
1375}
1376
1377
1378/* Store one glyph for IT->char_to_display in IT->glyph_row.
1379 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1380
1381static INLINE void
1382x_append_glyph (it)
1383 struct it *it;
1384{
1385 struct glyph *glyph;
1386 enum glyph_row_area area = it->area;
1387
1388 xassert (it->glyph_row);
1389 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1390
1391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1392 if (glyph < it->glyph_row->glyphs[area + 1])
1393 {
1394 /* Play it safe. If sub-structures of the glyph are not all the
1395 same size, it otherwise be that some bits stay set. This
1396 would prevent a comparison with GLYPH_EQUAL_P. */
1397 glyph->u.val = 0;
1398
1399 glyph->type = CHAR_GLYPH;
1400 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1401 glyph->u.ch = it->char_to_display;
1402 glyph->face_id = it->face_id;
06a2c219
GM
1403 glyph->charpos = CHARPOS (it->position);
1404 glyph->object = it->object;
1405 glyph->left_box_line_p = it->start_of_box_run_p;
1406 glyph->right_box_line_p = it->end_of_box_run_p;
1407 glyph->voffset = it->voffset;
1408 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1409 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1410 || it->phys_descent > it->descent);
ee569018 1411 glyph->glyph_not_available_p = it->glyph_not_available_p;
06a2c219
GM
1412 ++it->glyph_row->used[area];
1413 }
1414}
1415
b4192550
KH
1416/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1417 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1418
1419static INLINE void
1420x_append_composite_glyph (it)
1421 struct it *it;
1422{
1423 struct glyph *glyph;
1424 enum glyph_row_area area = it->area;
1425
1426 xassert (it->glyph_row);
1427
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1430 {
1431 /* Play it safe. If sub-structures of the glyph are not all the
1432 same size, it otherwise be that some bits stay set. This
1433 would prevent a comparison with GLYPH_EQUAL_P. */
1434 glyph->u.val = 0;
1435
1436 glyph->type = COMPOSITE_GLYPH;
1437 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1438 glyph->u.cmp_id = it->cmp_id;
1439 glyph->face_id = it->face_id;
b4192550
KH
1440 glyph->charpos = CHARPOS (it->position);
1441 glyph->object = it->object;
1442 glyph->left_box_line_p = it->start_of_box_run_p;
1443 glyph->right_box_line_p = it->end_of_box_run_p;
1444 glyph->voffset = it->voffset;
1445 glyph->multibyte_p = it->multibyte_p;
1446 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1447 || it->phys_descent > it->descent);
1448 ++it->glyph_row->used[area];
1449 }
1450}
1451
06a2c219
GM
1452
1453/* Change IT->ascent and IT->height according to the setting of
1454 IT->voffset. */
1455
1456static INLINE void
1457take_vertical_position_into_account (it)
1458 struct it *it;
1459{
1460 if (it->voffset)
1461 {
1462 if (it->voffset < 0)
1463 /* Increase the ascent so that we can display the text higher
1464 in the line. */
1465 it->ascent += abs (it->voffset);
1466 else
1467 /* Increase the descent so that we can display the text lower
1468 in the line. */
1469 it->descent += it->voffset;
1470 }
1471}
1472
1473
1474/* Produce glyphs/get display metrics for the image IT is loaded with.
1475 See the description of struct display_iterator in dispextern.h for
1476 an overview of struct display_iterator. */
1477
1478static void
1479x_produce_image_glyph (it)
1480 struct it *it;
1481{
1482 struct image *img;
1483 struct face *face;
1484
1485 xassert (it->what == IT_IMAGE);
1486
1487 face = FACE_FROM_ID (it->f, it->face_id);
1488 img = IMAGE_FROM_ID (it->f, it->image_id);
1489 xassert (img);
1490
1491 /* Make sure X resources of the face and image are loaded. */
1492 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1493 prepare_image_for_display (it->f, img);
1494
95af8492 1495 it->ascent = it->phys_ascent = image_ascent (img, face);
66ac4b0e 1496 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1497 it->pixel_width = img->width + 2 * img->margin;
1498
1499 it->nglyphs = 1;
1500
1501 if (face->box != FACE_NO_BOX)
1502 {
1503 it->ascent += face->box_line_width;
1504 it->descent += face->box_line_width;
1505
1506 if (it->start_of_box_run_p)
1507 it->pixel_width += face->box_line_width;
1508 if (it->end_of_box_run_p)
1509 it->pixel_width += face->box_line_width;
1510 }
1511
1512 take_vertical_position_into_account (it);
1513
1514 if (it->glyph_row)
1515 {
1516 struct glyph *glyph;
1517 enum glyph_row_area area = it->area;
1518
1519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1520 if (glyph < it->glyph_row->glyphs[area + 1])
1521 {
1522 glyph->type = IMAGE_GLYPH;
43d120d8
KH
1523 glyph->u.img_id = img->id;
1524 glyph->face_id = it->face_id;
06a2c219
GM
1525 glyph->pixel_width = it->pixel_width;
1526 glyph->charpos = CHARPOS (it->position);
1527 glyph->object = it->object;
1528 glyph->left_box_line_p = it->start_of_box_run_p;
1529 glyph->right_box_line_p = it->end_of_box_run_p;
1530 glyph->voffset = it->voffset;
1531 glyph->multibyte_p = it->multibyte_p;
1532 ++it->glyph_row->used[area];
1533 }
1534 }
1535}
1536
1537
1538/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1539 of the glyph, WIDTH and HEIGHT are the width and height of the
1540 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1541 ascent of the glyph (0 <= ASCENT <= 1). */
1542
1543static void
1544x_append_stretch_glyph (it, object, width, height, ascent)
1545 struct it *it;
1546 Lisp_Object object;
1547 int width, height;
1548 double ascent;
1549{
1550 struct glyph *glyph;
1551 enum glyph_row_area area = it->area;
1552
1553 xassert (ascent >= 0 && ascent <= 1);
1554
1555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1556 if (glyph < it->glyph_row->glyphs[area + 1])
1557 {
1558 glyph->type = STRETCH_GLYPH;
1559 glyph->u.stretch.ascent = height * ascent;
1560 glyph->u.stretch.height = height;
43d120d8 1561 glyph->face_id = it->face_id;
06a2c219
GM
1562 glyph->pixel_width = width;
1563 glyph->charpos = CHARPOS (it->position);
1564 glyph->object = object;
1565 glyph->left_box_line_p = it->start_of_box_run_p;
1566 glyph->right_box_line_p = it->end_of_box_run_p;
1567 glyph->voffset = it->voffset;
1568 glyph->multibyte_p = it->multibyte_p;
1569 ++it->glyph_row->used[area];
1570 }
1571}
1572
1573
1574/* Produce a stretch glyph for iterator IT. IT->object is the value
1575 of the glyph property displayed. The value must be a list
1576 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1577 being recognized:
1578
1579 1. `:width WIDTH' specifies that the space should be WIDTH *
1580 canonical char width wide. WIDTH may be an integer or floating
1581 point number.
1582
1583 2. `:relative-width FACTOR' specifies that the width of the stretch
1584 should be computed from the width of the first character having the
1585 `glyph' property, and should be FACTOR times that width.
1586
1587 3. `:align-to HPOS' specifies that the space should be wide enough
1588 to reach HPOS, a value in canonical character units.
1589
1590 Exactly one of the above pairs must be present.
1591
1592 4. `:height HEIGHT' specifies that the height of the stretch produced
1593 should be HEIGHT, measured in canonical character units.
1594
1595 5. `:relative-height FACTOR' specifies that the height of the the
1596 stretch should be FACTOR times the height of the characters having
1597 the glyph property.
1598
1599 Either none or exactly one of 4 or 5 must be present.
1600
1601 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1602 of the stretch should be used for the ascent of the stretch.
1603 ASCENT must be in the range 0 <= ASCENT <= 100. */
1604
1605#define NUMVAL(X) \
1606 ((INTEGERP (X) || FLOATP (X)) \
1607 ? XFLOATINT (X) \
1608 : - 1)
1609
1610
1611static void
1612x_produce_stretch_glyph (it)
1613 struct it *it;
1614{
1615 /* (space :width WIDTH :height HEIGHT. */
1616 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1617 extern Lisp_Object QCrelative_width, QCrelative_height;
1618 extern Lisp_Object QCalign_to;
1619 Lisp_Object prop, plist;
1620 double width = 0, height = 0, ascent = 0;
1621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1622 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1623
1624 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1625
1626 /* List should start with `space'. */
1627 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1628 plist = XCDR (it->object);
1629
1630 /* Compute the width of the stretch. */
1631 if (prop = Fplist_get (plist, QCwidth),
1632 NUMVAL (prop) > 0)
1633 /* Absolute width `:width WIDTH' specified and valid. */
1634 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1635 else if (prop = Fplist_get (plist, QCrelative_width),
1636 NUMVAL (prop) > 0)
1637 {
1638 /* Relative width `:relative-width FACTOR' specified and valid.
1639 Compute the width of the characters having the `glyph'
1640 property. */
1641 struct it it2;
1642 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1643
1644 it2 = *it;
1645 if (it->multibyte_p)
1646 {
1647 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1648 - IT_BYTEPOS (*it));
1649 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1650 }
1651 else
1652 it2.c = *p, it2.len = 1;
1653
1654 it2.glyph_row = NULL;
1655 it2.what = IT_CHARACTER;
1656 x_produce_glyphs (&it2);
1657 width = NUMVAL (prop) * it2.pixel_width;
1658 }
1659 else if (prop = Fplist_get (plist, QCalign_to),
1660 NUMVAL (prop) > 0)
1661 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1662 else
1663 /* Nothing specified -> width defaults to canonical char width. */
1664 width = CANON_X_UNIT (it->f);
1665
1666 /* Compute height. */
1667 if (prop = Fplist_get (plist, QCheight),
1668 NUMVAL (prop) > 0)
1669 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1670 else if (prop = Fplist_get (plist, QCrelative_height),
1671 NUMVAL (prop) > 0)
1672 height = FONT_HEIGHT (font) * NUMVAL (prop);
1673 else
1674 height = FONT_HEIGHT (font);
1675
1676 /* Compute percentage of height used for ascent. If
1677 `:ascent ASCENT' is present and valid, use that. Otherwise,
1678 derive the ascent from the font in use. */
1679 if (prop = Fplist_get (plist, QCascent),
1680 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1681 ascent = NUMVAL (prop) / 100.0;
1682 else
1683 ascent = (double) font->ascent / FONT_HEIGHT (font);
1684
1685 if (width <= 0)
1686 width = 1;
1687 if (height <= 0)
1688 height = 1;
1689
1690 if (it->glyph_row)
1691 {
1692 Lisp_Object object = it->stack[it->sp - 1].string;
1693 if (!STRINGP (object))
1694 object = it->w->buffer;
1695 x_append_stretch_glyph (it, object, width, height, ascent);
1696 }
1697
1698 it->pixel_width = width;
66ac4b0e
GM
1699 it->ascent = it->phys_ascent = height * ascent;
1700 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1701 it->nglyphs = 1;
1702
1703 if (face->box != FACE_NO_BOX)
1704 {
1705 it->ascent += face->box_line_width;
1706 it->descent += face->box_line_width;
1707
1708 if (it->start_of_box_run_p)
1709 it->pixel_width += face->box_line_width;
1710 if (it->end_of_box_run_p)
1711 it->pixel_width += face->box_line_width;
1712 }
1713
1714 take_vertical_position_into_account (it);
1715}
1716
b4192550
KH
1717/* Return proper value to be used as baseline offset of font that has
1718 ASCENT and DESCENT to draw characters by the font at the vertical
1719 center of the line of frame F.
1720
1721 Here, out task is to find the value of BOFF in the following figure;
1722
1723 -------------------------+-----------+-
1724 -+-+---------+-+ | |
1725 | | | | | |
1726 | | | | F_ASCENT F_HEIGHT
1727 | | | ASCENT | |
1728 HEIGHT | | | | |
1729 | | |-|-+------+-----------|------- baseline
1730 | | | | BOFF | |
1731 | |---------|-+-+ | |
1732 | | | DESCENT | |
1733 -+-+---------+-+ F_DESCENT |
1734 -------------------------+-----------+-
1735
1736 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1737 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1738 DESCENT = FONT->descent
1739 HEIGHT = FONT_HEIGHT (FONT)
1740 F_DESCENT = (F->output_data.x->font->descent
1741 - F->output_data.x->baseline_offset)
1742 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1743*/
1744
1745#define VCENTER_BASELINE_OFFSET(FONT, F) \
1746 ((FONT)->descent \
1747 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1748 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1749
1750/* Produce glyphs/get display metrics for the display element IT is
1751 loaded with. See the description of struct display_iterator in
1752 dispextern.h for an overview of struct display_iterator. */
1753
1754static void
1755x_produce_glyphs (it)
1756 struct it *it;
1757{
ee569018
KH
1758 it->glyph_not_available_p = 0;
1759
06a2c219
GM
1760 if (it->what == IT_CHARACTER)
1761 {
1762 XChar2b char2b;
1763 XFontStruct *font;
ee569018 1764 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1765 XCharStruct *pcm;
06a2c219 1766 int font_not_found_p;
b4192550
KH
1767 struct font_info *font_info;
1768 int boff; /* baseline offset */
06a2c219 1769
ee569018
KH
1770 /* Maybe translate single-byte characters to multibyte, or the
1771 other way. */
06a2c219 1772 it->char_to_display = it->c;
ee569018 1773 if (!ASCII_BYTE_P (it->c))
06a2c219 1774 {
ee569018
KH
1775 if (unibyte_display_via_language_environment
1776 && SINGLE_BYTE_CHAR_P (it->c)
1777 && (it->c >= 0240
1778 || !NILP (Vnonascii_translation_table)))
1779 {
1780 it->char_to_display = unibyte_char_to_multibyte (it->c);
1781 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1782 face = FACE_FROM_ID (it->f, it->face_id);
1783 }
1784 else if (!SINGLE_BYTE_CHAR_P (it->c)
1785 && !it->multibyte_p)
1786 {
1787 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1788 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1789 face = FACE_FROM_ID (it->f, it->face_id);
1790 }
06a2c219
GM
1791 }
1792
ee569018
KH
1793 /* Get font to use. Encode IT->char_to_display. */
1794 x_get_char_face_and_encoding (it->f, it->char_to_display,
1795 it->face_id, &char2b,
1796 it->multibyte_p);
06a2c219
GM
1797 font = face->font;
1798
1799 /* When no suitable font found, use the default font. */
1800 font_not_found_p = font == NULL;
1801 if (font_not_found_p)
b4192550
KH
1802 {
1803 font = FRAME_FONT (it->f);
1804 boff = it->f->output_data.x->baseline_offset;
1805 font_info = NULL;
1806 }
1807 else
1808 {
1809 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1810 boff = font_info->baseline_offset;
1811 if (font_info->vertical_centering)
1812 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1813 }
06a2c219
GM
1814
1815 if (it->char_to_display >= ' '
1816 && (!it->multibyte_p || it->char_to_display < 128))
1817 {
1818 /* Either unibyte or ASCII. */
1819 int stretched_p;
1820
1821 it->nglyphs = 1;
06a2c219
GM
1822
1823 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1824 it->ascent = font->ascent + boff;
1825 it->descent = font->descent - boff;
474848ac
GM
1826
1827 if (pcm)
1828 {
1829 it->phys_ascent = pcm->ascent + boff;
1830 it->phys_descent = pcm->descent - boff;
1831 it->pixel_width = pcm->width;
1832 }
1833 else
1834 {
1835 it->glyph_not_available_p = 1;
1836 it->phys_ascent = font->ascent + boff;
1837 it->phys_descent = font->descent - boff;
1838 it->pixel_width = FONT_WIDTH (font);
1839 }
06a2c219
GM
1840
1841 /* If this is a space inside a region of text with
1842 `space-width' property, change its width. */
1843 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1844 if (stretched_p)
1845 it->pixel_width *= XFLOATINT (it->space_width);
1846
1847 /* If face has a box, add the box thickness to the character
1848 height. If character has a box line to the left and/or
1849 right, add the box line width to the character's width. */
1850 if (face->box != FACE_NO_BOX)
1851 {
1852 int thick = face->box_line_width;
1853
1854 it->ascent += thick;
1855 it->descent += thick;
1856
1857 if (it->start_of_box_run_p)
1858 it->pixel_width += thick;
1859 if (it->end_of_box_run_p)
1860 it->pixel_width += thick;
1861 }
1862
1863 /* If face has an overline, add the height of the overline
1864 (1 pixel) and a 1 pixel margin to the character height. */
1865 if (face->overline_p)
1866 it->ascent += 2;
1867
1868 take_vertical_position_into_account (it);
1869
1870 /* If we have to actually produce glyphs, do it. */
1871 if (it->glyph_row)
1872 {
1873 if (stretched_p)
1874 {
1875 /* Translate a space with a `space-width' property
1876 into a stretch glyph. */
1877 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1878 x_append_stretch_glyph (it, it->object, it->pixel_width,
1879 it->ascent + it->descent, ascent);
1880 }
1881 else
1882 x_append_glyph (it);
1883
1884 /* If characters with lbearing or rbearing are displayed
1885 in this line, record that fact in a flag of the
1886 glyph row. This is used to optimize X output code. */
1c7e22fd 1887 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1888 it->glyph_row->contains_overlapping_glyphs_p = 1;
1889 }
1890 }
1891 else if (it->char_to_display == '\n')
1892 {
1893 /* A newline has no width but we need the height of the line. */
1894 it->pixel_width = 0;
1895 it->nglyphs = 0;
b4192550
KH
1896 it->ascent = it->phys_ascent = font->ascent + boff;
1897 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1898
1899 if (face->box != FACE_NO_BOX)
1900 {
1901 int thick = face->box_line_width;
1902 it->ascent += thick;
1903 it->descent += thick;
1904 }
1905 }
1906 else if (it->char_to_display == '\t')
1907 {
1908 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1909 int x = it->current_x + it->continuation_lines_width;
06a2c219
GM
1910 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1911
1912 it->pixel_width = next_tab_x - x;
1913 it->nglyphs = 1;
b4192550
KH
1914 it->ascent = it->phys_ascent = font->ascent + boff;
1915 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1916
1917 if (it->glyph_row)
1918 {
1919 double ascent = (double) it->ascent / (it->ascent + it->descent);
1920 x_append_stretch_glyph (it, it->object, it->pixel_width,
1921 it->ascent + it->descent, ascent);
1922 }
1923 }
1924 else
1925 {
1926 /* A multi-byte character. Assume that the display width of the
1927 character is the width of the character multiplied by the
b4192550 1928 width of the font. */
06a2c219 1929
b4192550
KH
1930 /* If we found a font, this font should give us the right
1931 metrics. If we didn't find a font, use the frame's
1932 default font and calculate the width of the character
1933 from the charset width; this is what old redisplay code
1934 did. */
1935 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1936 if (font_not_found_p || !pcm)
1937 {
1938 int charset = CHAR_CHARSET (it->char_to_display);
1939
1940 it->glyph_not_available_p = 1;
1941 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1942 * CHARSET_WIDTH (charset));
1943 it->phys_ascent = font->ascent + boff;
1944 it->phys_descent = font->descent - boff;
1945 }
1946 else
1947 {
1948 it->pixel_width = pcm->width;
1949 it->phys_ascent = pcm->ascent + boff;
1950 it->phys_descent = pcm->descent - boff;
1951 if (it->glyph_row
1952 && (pcm->lbearing < 0
1953 || pcm->rbearing > pcm->width))
1954 it->glyph_row->contains_overlapping_glyphs_p = 1;
1955 }
b4192550
KH
1956 it->nglyphs = 1;
1957 it->ascent = font->ascent + boff;
1958 it->descent = font->descent - boff;
06a2c219
GM
1959 if (face->box != FACE_NO_BOX)
1960 {
1961 int thick = face->box_line_width;
1962 it->ascent += thick;
1963 it->descent += thick;
1964
1965 if (it->start_of_box_run_p)
1966 it->pixel_width += thick;
1967 if (it->end_of_box_run_p)
1968 it->pixel_width += thick;
1969 }
1970
1971 /* If face has an overline, add the height of the overline
1972 (1 pixel) and a 1 pixel margin to the character height. */
1973 if (face->overline_p)
1974 it->ascent += 2;
1975
1976 take_vertical_position_into_account (it);
1977
1978 if (it->glyph_row)
1979 x_append_glyph (it);
1980 }
1981 }
b4192550
KH
1982 else if (it->what == IT_COMPOSITION)
1983 {
1984 /* Note: A composition is represented as one glyph in the
1985 glyph matrix. There are no padding glyphs. */
1986 XChar2b char2b;
1987 XFontStruct *font;
ee569018 1988 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
1989 XCharStruct *pcm;
1990 int font_not_found_p;
1991 struct font_info *font_info;
1992 int boff; /* baseline offset */
1993 struct composition *cmp = composition_table[it->cmp_id];
1994
1995 /* Maybe translate single-byte characters to multibyte. */
1996 it->char_to_display = it->c;
1997 if (unibyte_display_via_language_environment
1998 && SINGLE_BYTE_CHAR_P (it->c)
1999 && (it->c >= 0240
2000 || (it->c >= 0200
2001 && !NILP (Vnonascii_translation_table))))
2002 {
2003 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2004 }
2005
2006 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2007 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2008 face = FACE_FROM_ID (it->f, it->face_id);
2009 x_get_char_face_and_encoding (it->f, it->char_to_display,
2010 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2011 font = face->font;
2012
2013 /* When no suitable font found, use the default font. */
2014 font_not_found_p = font == NULL;
2015 if (font_not_found_p)
2016 {
2017 font = FRAME_FONT (it->f);
2018 boff = it->f->output_data.x->baseline_offset;
2019 font_info = NULL;
2020 }
2021 else
2022 {
2023 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2024 boff = font_info->baseline_offset;
2025 if (font_info->vertical_centering)
2026 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2027 }
2028
2029 /* There are no padding glyphs, so there is only one glyph to
2030 produce for the composition. Important is that pixel_width,
2031 ascent and descent are the values of what is drawn by
2032 draw_glyphs (i.e. the values of the overall glyphs composed). */
2033 it->nglyphs = 1;
2034
2035 /* If we have not yet calculated pixel size data of glyphs of
2036 the composition for the current face font, calculate them
2037 now. Theoretically, we have to check all fonts for the
2038 glyphs, but that requires much time and memory space. So,
2039 here we check only the font of the first glyph. This leads
2040 to incorrect display very rarely, and C-l (recenter) can
2041 correct the display anyway. */
2042 if (cmp->font != (void *) font)
2043 {
2044 /* Ascent and descent of the font of the first character of
2045 this composition (adjusted by baseline offset). Ascent
2046 and descent of overall glyphs should not be less than
2047 them respectively. */
2048 int font_ascent = font->ascent + boff;
2049 int font_descent = font->descent - boff;
2050 /* Bounding box of the overall glyphs. */
2051 int leftmost, rightmost, lowest, highest;
329bed06 2052 int i, width, ascent, descent;
b4192550
KH
2053
2054 cmp->font = (void *) font;
2055
2056 /* Initialize the bounding box. */
2057 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2058 if (pcm)
2059 {
2060 width = pcm->width;
2061 ascent = pcm->ascent;
2062 descent = pcm->descent;
2063 }
2064 else
2065 {
2066 width = FONT_WIDTH (font);
2067 ascent = font->ascent;
2068 descent = font->descent;
2069 }
2070
2071 rightmost = width;
2072 lowest = - descent + boff;
2073 highest = ascent + boff;
b4192550 2074 leftmost = 0;
329bed06 2075
b4192550
KH
2076 if (font_info
2077 && font_info->default_ascent
2078 && CHAR_TABLE_P (Vuse_default_ascent)
2079 && !NILP (Faref (Vuse_default_ascent,
2080 make_number (it->char_to_display))))
2081 highest = font_info->default_ascent + boff;
2082
2083 /* Draw the first glyph at the normal position. It may be
2084 shifted to right later if some other glyphs are drawn at
2085 the left. */
2086 cmp->offsets[0] = 0;
2087 cmp->offsets[1] = boff;
2088
2089 /* Set cmp->offsets for the remaining glyphs. */
2090 for (i = 1; i < cmp->glyph_len; i++)
2091 {
2092 int left, right, btm, top;
2093 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2094 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2095
2096 face = FACE_FROM_ID (it->f, face_id);
2097 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2098 it->multibyte_p);
b4192550
KH
2099 font = face->font;
2100 if (font == NULL)
2101 {
2102 font = FRAME_FONT (it->f);
2103 boff = it->f->output_data.x->baseline_offset;
2104 font_info = NULL;
2105 }
2106 else
2107 {
2108 font_info
2109 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2110 boff = font_info->baseline_offset;
2111 if (font_info->vertical_centering)
2112 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2113 }
2114
2115 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2116 if (pcm)
2117 {
2118 width = pcm->width;
2119 ascent = pcm->ascent;
2120 descent = pcm->descent;
2121 }
2122 else
2123 {
2124 width = FONT_WIDTH (font);
2125 ascent = font->ascent;
2126 descent = font->descent;
2127 }
b4192550
KH
2128
2129 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2130 {
2131 /* Relative composition with or without
2132 alternate chars. */
329bed06
GM
2133 left = (leftmost + rightmost - width) / 2;
2134 btm = - descent + boff;
b4192550
KH
2135 if (font_info && font_info->relative_compose
2136 && (! CHAR_TABLE_P (Vignore_relative_composition)
2137 || NILP (Faref (Vignore_relative_composition,
2138 make_number (ch)))))
2139 {
2140
329bed06 2141 if (- descent >= font_info->relative_compose)
b4192550
KH
2142 /* One extra pixel between two glyphs. */
2143 btm = highest + 1;
329bed06 2144 else if (ascent <= 0)
b4192550 2145 /* One extra pixel between two glyphs. */
329bed06 2146 btm = lowest - 1 - ascent - descent;
b4192550
KH
2147 }
2148 }
2149 else
2150 {
2151 /* A composition rule is specified by an integer
2152 value that encodes global and new reference
2153 points (GREF and NREF). GREF and NREF are
2154 specified by numbers as below:
2155
2156 0---1---2 -- ascent
2157 | |
2158 | |
2159 | |
2160 9--10--11 -- center
2161 | |
2162 ---3---4---5--- baseline
2163 | |
2164 6---7---8 -- descent
2165 */
2166 int rule = COMPOSITION_RULE (cmp, i);
2167 int gref, nref, grefx, grefy, nrefx, nrefy;
2168
2169 COMPOSITION_DECODE_RULE (rule, gref, nref);
2170 grefx = gref % 3, nrefx = nref % 3;
2171 grefy = gref / 3, nrefy = nref / 3;
2172
2173 left = (leftmost
2174 + grefx * (rightmost - leftmost) / 2
329bed06 2175 - nrefx * width / 2);
b4192550
KH
2176 btm = ((grefy == 0 ? highest
2177 : grefy == 1 ? 0
2178 : grefy == 2 ? lowest
2179 : (highest + lowest) / 2)
329bed06
GM
2180 - (nrefy == 0 ? ascent + descent
2181 : nrefy == 1 ? descent - boff
b4192550 2182 : nrefy == 2 ? 0
329bed06 2183 : (ascent + descent) / 2));
b4192550
KH
2184 }
2185
2186 cmp->offsets[i * 2] = left;
329bed06 2187 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2188
2189 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2190 right = left + width;
2191 top = btm + descent + ascent;
b4192550
KH
2192 if (left < leftmost)
2193 leftmost = left;
2194 if (right > rightmost)
2195 rightmost = right;
2196 if (top > highest)
2197 highest = top;
2198 if (btm < lowest)
2199 lowest = btm;
2200 }
2201
2202 /* If there are glyphs whose x-offsets are negative,
2203 shift all glyphs to the right and make all x-offsets
2204 non-negative. */
2205 if (leftmost < 0)
2206 {
2207 for (i = 0; i < cmp->glyph_len; i++)
2208 cmp->offsets[i * 2] -= leftmost;
2209 rightmost -= leftmost;
2210 }
2211
2212 cmp->pixel_width = rightmost;
2213 cmp->ascent = highest;
2214 cmp->descent = - lowest;
2215 if (cmp->ascent < font_ascent)
2216 cmp->ascent = font_ascent;
2217 if (cmp->descent < font_descent)
2218 cmp->descent = font_descent;
2219 }
2220
2221 it->pixel_width = cmp->pixel_width;
2222 it->ascent = it->phys_ascent = cmp->ascent;
2223 it->descent = it->phys_descent = cmp->descent;
2224
2225 if (face->box != FACE_NO_BOX)
2226 {
2227 int thick = face->box_line_width;
2228 it->ascent += thick;
2229 it->descent += thick;
2230
2231 if (it->start_of_box_run_p)
2232 it->pixel_width += thick;
2233 if (it->end_of_box_run_p)
2234 it->pixel_width += thick;
2235 }
2236
2237 /* If face has an overline, add the height of the overline
2238 (1 pixel) and a 1 pixel margin to the character height. */
2239 if (face->overline_p)
2240 it->ascent += 2;
2241
2242 take_vertical_position_into_account (it);
2243
2244 if (it->glyph_row)
2245 x_append_composite_glyph (it);
2246 }
06a2c219
GM
2247 else if (it->what == IT_IMAGE)
2248 x_produce_image_glyph (it);
2249 else if (it->what == IT_STRETCH)
2250 x_produce_stretch_glyph (it);
2251
3017fdd1
GM
2252 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2253 because this isn't true for images with `:ascent 100'. */
2254 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2255 if (it->area == TEXT_AREA)
2256 it->current_x += it->pixel_width;
66ac4b0e 2257
d365f5bb
GM
2258 it->descent += it->extra_line_spacing;
2259
06a2c219
GM
2260 it->max_ascent = max (it->max_ascent, it->ascent);
2261 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2262 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2263 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2264}
2265
2266
2267/* Estimate the pixel height of the mode or top line on frame F.
2268 FACE_ID specifies what line's height to estimate. */
2269
2270int
2271x_estimate_mode_line_height (f, face_id)
2272 struct frame *f;
2273 enum face_id face_id;
2274{
2275 int height = 1;
2276
2277 /* This function is called so early when Emacs starts that the face
2278 cache and mode line face are not yet initialized. */
2279 if (FRAME_FACE_CACHE (f))
2280 {
2281 struct face *face = FACE_FROM_ID (f, face_id);
2282 if (face)
2283 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2284 }
2285
2286 return height;
2287}
2288
2289\f
2290/***********************************************************************
2291 Glyph display
2292 ***********************************************************************/
2293
2294/* A sequence of glyphs to be drawn in the same face.
2295
2296 This data structure is not really completely X specific, so it
2297 could possibly, at least partially, be useful for other systems. It
2298 is currently not part of the external redisplay interface because
2299 it's not clear what other systems will need. */
2300
2301struct glyph_string
2302{
2303 /* X-origin of the string. */
2304 int x;
2305
2306 /* Y-origin and y-position of the base line of this string. */
2307 int y, ybase;
2308
2309 /* The width of the string, not including a face extension. */
2310 int width;
2311
2312 /* The width of the string, including a face extension. */
2313 int background_width;
2314
2315 /* The height of this string. This is the height of the line this
2316 string is drawn in, and can be different from the height of the
2317 font the string is drawn in. */
2318 int height;
2319
2320 /* Number of pixels this string overwrites in front of its x-origin.
2321 This number is zero if the string has an lbearing >= 0; it is
2322 -lbearing, if the string has an lbearing < 0. */
2323 int left_overhang;
2324
2325 /* Number of pixels this string overwrites past its right-most
2326 nominal x-position, i.e. x + width. Zero if the string's
2327 rbearing is <= its nominal width, rbearing - width otherwise. */
2328 int right_overhang;
2329
2330 /* The frame on which the glyph string is drawn. */
2331 struct frame *f;
2332
2333 /* The window on which the glyph string is drawn. */
2334 struct window *w;
2335
2336 /* X display and window for convenience. */
2337 Display *display;
2338 Window window;
2339
2340 /* The glyph row for which this string was built. It determines the
2341 y-origin and height of the string. */
2342 struct glyph_row *row;
2343
2344 /* The area within row. */
2345 enum glyph_row_area area;
2346
2347 /* Characters to be drawn, and number of characters. */
2348 XChar2b *char2b;
2349 int nchars;
2350
06a2c219
GM
2351 /* A face-override for drawing cursors, mouse face and similar. */
2352 enum draw_glyphs_face hl;
2353
2354 /* Face in which this string is to be drawn. */
2355 struct face *face;
2356
2357 /* Font in which this string is to be drawn. */
2358 XFontStruct *font;
2359
2360 /* Font info for this string. */
2361 struct font_info *font_info;
2362
b4192550
KH
2363 /* Non-null means this string describes (part of) a composition.
2364 All characters from char2b are drawn composed. */
2365 struct composition *cmp;
06a2c219
GM
2366
2367 /* Index of this glyph string's first character in the glyph
b4192550
KH
2368 definition of CMP. If this is zero, this glyph string describes
2369 the first character of a composition. */
06a2c219
GM
2370 int gidx;
2371
2372 /* 1 means this glyph strings face has to be drawn to the right end
2373 of the window's drawing area. */
2374 unsigned extends_to_end_of_line_p : 1;
2375
2376 /* 1 means the background of this string has been drawn. */
2377 unsigned background_filled_p : 1;
2378
2379 /* 1 means glyph string must be drawn with 16-bit functions. */
2380 unsigned two_byte_p : 1;
2381
2382 /* 1 means that the original font determined for drawing this glyph
2383 string could not be loaded. The member `font' has been set to
2384 the frame's default font in this case. */
2385 unsigned font_not_found_p : 1;
2386
2387 /* 1 means that the face in which this glyph string is drawn has a
2388 stipple pattern. */
2389 unsigned stippled_p : 1;
2390
66ac4b0e
GM
2391 /* 1 means only the foreground of this glyph string must be drawn,
2392 and we should use the physical height of the line this glyph
2393 string appears in as clip rect. */
2394 unsigned for_overlaps_p : 1;
2395
06a2c219
GM
2396 /* The GC to use for drawing this glyph string. */
2397 GC gc;
2398
2399 /* A pointer to the first glyph in the string. This glyph
2400 corresponds to char2b[0]. Needed to draw rectangles if
2401 font_not_found_p is 1. */
2402 struct glyph *first_glyph;
2403
2404 /* Image, if any. */
2405 struct image *img;
2406
2407 struct glyph_string *next, *prev;
2408};
2409
2410
5c187dee 2411#if 0
06a2c219
GM
2412
2413static void
2414x_dump_glyph_string (s)
2415 struct glyph_string *s;
2416{
2417 fprintf (stderr, "glyph string\n");
2418 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2419 s->x, s->y, s->width, s->height);
2420 fprintf (stderr, " ybase = %d\n", s->ybase);
2421 fprintf (stderr, " hl = %d\n", s->hl);
2422 fprintf (stderr, " left overhang = %d, right = %d\n",
2423 s->left_overhang, s->right_overhang);
2424 fprintf (stderr, " nchars = %d\n", s->nchars);
2425 fprintf (stderr, " extends to end of line = %d\n",
2426 s->extends_to_end_of_line_p);
2427 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2428 fprintf (stderr, " bg width = %d\n", s->background_width);
2429}
2430
2431#endif /* GLYPH_DEBUG */
2432
2433
2434
2435static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2436 struct glyph_string **,
2437 struct glyph_string *,
2438 struct glyph_string *));
2439static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2440 struct glyph_string **,
2441 struct glyph_string *,
2442 struct glyph_string *));
2443static void x_append_glyph_string P_ ((struct glyph_string **,
2444 struct glyph_string **,
2445 struct glyph_string *));
2446static int x_left_overwritten P_ ((struct glyph_string *));
2447static int x_left_overwriting P_ ((struct glyph_string *));
2448static int x_right_overwritten P_ ((struct glyph_string *));
2449static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2450static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2451 int));
06a2c219
GM
2452static void x_init_glyph_string P_ ((struct glyph_string *,
2453 XChar2b *, struct window *,
2454 struct glyph_row *,
2455 enum glyph_row_area, int,
2456 enum draw_glyphs_face));
2457static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2458 enum glyph_row_area, int, int,
66ac4b0e 2459 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2460static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2461static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2462static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2463 int));
2464static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2465static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2466static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2467static void x_draw_glyph_string P_ ((struct glyph_string *));
2468static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2469static void x_set_cursor_gc P_ ((struct glyph_string *));
2470static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2471static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2472static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2473 int *, int *));
2474static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2475static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2476 unsigned long *, double, int));
06a2c219 2477static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2478 double, int, unsigned long));
06a2c219
GM
2479static void x_setup_relief_colors P_ ((struct glyph_string *));
2480static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2481static void x_draw_image_relief P_ ((struct glyph_string *));
2482static void x_draw_image_foreground P_ ((struct glyph_string *));
2483static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2484static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2485static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2486 int, int, int));
2487static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2488 int, int, int, int, XRectangle *));
2489static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2490 int, int, int, XRectangle *));
66ac4b0e
GM
2491static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2492 enum glyph_row_area));
06a2c219
GM
2493
2494
06a2c219
GM
2495/* Append the list of glyph strings with head H and tail T to the list
2496 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2497
2498static INLINE void
2499x_append_glyph_string_lists (head, tail, h, t)
2500 struct glyph_string **head, **tail;
2501 struct glyph_string *h, *t;
2502{
2503 if (h)
2504 {
2505 if (*head)
2506 (*tail)->next = h;
2507 else
2508 *head = h;
2509 h->prev = *tail;
2510 *tail = t;
2511 }
2512}
2513
2514
2515/* Prepend the list of glyph strings with head H and tail T to the
2516 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2517 result. */
2518
2519static INLINE void
2520x_prepend_glyph_string_lists (head, tail, h, t)
2521 struct glyph_string **head, **tail;
2522 struct glyph_string *h, *t;
2523{
2524 if (h)
2525 {
2526 if (*head)
2527 (*head)->prev = t;
2528 else
2529 *tail = t;
2530 t->next = *head;
2531 *head = h;
2532 }
2533}
2534
2535
2536/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2537 Set *HEAD and *TAIL to the resulting list. */
2538
2539static INLINE void
2540x_append_glyph_string (head, tail, s)
2541 struct glyph_string **head, **tail;
2542 struct glyph_string *s;
2543{
2544 s->next = s->prev = NULL;
2545 x_append_glyph_string_lists (head, tail, s, s);
2546}
2547
2548
2549/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2550 face. */
2551
2552static void
2553x_set_cursor_gc (s)
2554 struct glyph_string *s;
2555{
2556 if (s->font == FRAME_FONT (s->f)
2557 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2558 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2559 && !s->cmp)
06a2c219
GM
2560 s->gc = s->f->output_data.x->cursor_gc;
2561 else
2562 {
2563 /* Cursor on non-default face: must merge. */
2564 XGCValues xgcv;
2565 unsigned long mask;
2566
2567 xgcv.background = s->f->output_data.x->cursor_pixel;
2568 xgcv.foreground = s->face->background;
2569
2570 /* If the glyph would be invisible, try a different foreground. */
2571 if (xgcv.foreground == xgcv.background)
2572 xgcv.foreground = s->face->foreground;
2573 if (xgcv.foreground == xgcv.background)
2574 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2575 if (xgcv.foreground == xgcv.background)
2576 xgcv.foreground = s->face->foreground;
2577
2578 /* Make sure the cursor is distinct from text in this face. */
2579 if (xgcv.background == s->face->background
2580 && xgcv.foreground == s->face->foreground)
2581 {
2582 xgcv.background = s->face->foreground;
2583 xgcv.foreground = s->face->background;
2584 }
2585
2586 IF_DEBUG (x_check_font (s->f, s->font));
2587 xgcv.font = s->font->fid;
2588 xgcv.graphics_exposures = False;
2589 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2590
2591 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2592 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2593 mask, &xgcv);
2594 else
2595 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2596 = XCreateGC (s->display, s->window, mask, &xgcv);
2597
2598 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2599 }
2600}
2601
2602
2603/* Set up S->gc of glyph string S for drawing text in mouse face. */
2604
2605static void
2606x_set_mouse_face_gc (s)
2607 struct glyph_string *s;
2608{
2609 int face_id;
ee569018 2610 struct face *face;
06a2c219
GM
2611
2612 /* What face has to be used for the mouse face? */
2613 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2614 face = FACE_FROM_ID (s->f, face_id);
033e3e18
GM
2615 if (s->first_glyph->type == CHAR_GLYPH)
2616 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2617 else
2618 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2619 s->face = FACE_FROM_ID (s->f, face_id);
2620 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2621
2622 /* If font in this face is same as S->font, use it. */
2623 if (s->font == s->face->font)
2624 s->gc = s->face->gc;
2625 else
2626 {
2627 /* Otherwise construct scratch_cursor_gc with values from FACE
2628 but font FONT. */
2629 XGCValues xgcv;
2630 unsigned long mask;
2631
2632 xgcv.background = s->face->background;
2633 xgcv.foreground = s->face->foreground;
2634 IF_DEBUG (x_check_font (s->f, s->font));
2635 xgcv.font = s->font->fid;
2636 xgcv.graphics_exposures = False;
2637 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2638
2639 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2640 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2641 mask, &xgcv);
2642 else
2643 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2644 = XCreateGC (s->display, s->window, mask, &xgcv);
2645
2646 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2647 }
2648
2649 xassert (s->gc != 0);
2650}
2651
2652
2653/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2654 Faces to use in the mode line have already been computed when the
2655 matrix was built, so there isn't much to do, here. */
2656
2657static INLINE void
2658x_set_mode_line_face_gc (s)
2659 struct glyph_string *s;
2660{
2661 s->gc = s->face->gc;
2662 xassert (s->gc != 0);
2663}
2664
2665
2666/* Set S->gc of glyph string S for drawing that glyph string. Set
2667 S->stippled_p to a non-zero value if the face of S has a stipple
2668 pattern. */
2669
2670static INLINE void
2671x_set_glyph_string_gc (s)
2672 struct glyph_string *s;
2673{
2674 if (s->hl == DRAW_NORMAL_TEXT)
2675 {
2676 s->gc = s->face->gc;
2677 s->stippled_p = s->face->stipple != 0;
2678 }
2679 else if (s->hl == DRAW_INVERSE_VIDEO)
2680 {
2681 x_set_mode_line_face_gc (s);
2682 s->stippled_p = s->face->stipple != 0;
2683 }
2684 else if (s->hl == DRAW_CURSOR)
2685 {
2686 x_set_cursor_gc (s);
2687 s->stippled_p = 0;
2688 }
2689 else if (s->hl == DRAW_MOUSE_FACE)
2690 {
2691 x_set_mouse_face_gc (s);
2692 s->stippled_p = s->face->stipple != 0;
2693 }
2694 else if (s->hl == DRAW_IMAGE_RAISED
2695 || s->hl == DRAW_IMAGE_SUNKEN)
2696 {
2697 s->gc = s->face->gc;
2698 s->stippled_p = s->face->stipple != 0;
2699 }
2700 else
2701 {
2702 s->gc = s->face->gc;
2703 s->stippled_p = s->face->stipple != 0;
2704 }
2705
2706 /* GC must have been set. */
2707 xassert (s->gc != 0);
2708}
2709
2710
2711/* Return in *R the clipping rectangle for glyph string S. */
2712
2713static void
2714x_get_glyph_string_clip_rect (s, r)
2715 struct glyph_string *s;
2716 XRectangle *r;
2717{
2718 if (s->row->full_width_p)
2719 {
2720 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2721 int canon_x = CANON_X_UNIT (s->f);
2722
2723 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2724 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2725
2726 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2727 {
1da3fd71 2728 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2729 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2730 r->x -= width;
2731 }
2732
b9432a85 2733 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2734
06a2c219
GM
2735 /* Unless displaying a mode or menu bar line, which are always
2736 fully visible, clip to the visible part of the row. */
2737 if (s->w->pseudo_window_p)
2738 r->height = s->row->visible_height;
2739 else
2740 r->height = s->height;
2741 }
2742 else
2743 {
2744 /* This is a text line that may be partially visible. */
2745 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2746 r->width = window_box_width (s->w, s->area);
2747 r->height = s->row->visible_height;
2748 }
2749
2750 /* Don't use S->y for clipping because it doesn't take partially
2751 visible lines into account. For example, it can be negative for
2752 partially visible lines at the top of a window. */
2753 if (!s->row->full_width_p
2754 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2755 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2756 else
2757 r->y = max (0, s->row->y);
06a2c219 2758
9ea173e8 2759 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2760 at the top of the window. */
9ea173e8 2761 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2762 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2763
2764 /* If S draws overlapping rows, it's sufficient to use the top and
2765 bottom of the window for clipping because this glyph string
2766 intentionally draws over other lines. */
2767 if (s->for_overlaps_p)
2768 {
045dee35 2769 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2770 r->height = window_text_bottom_y (s->w) - r->y;
2771 }
2772
2773 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2774}
2775
2776
2777/* Set clipping for output of glyph string S. S may be part of a mode
2778 line or menu if we don't have X toolkit support. */
2779
2780static INLINE void
2781x_set_glyph_string_clipping (s)
2782 struct glyph_string *s;
2783{
2784 XRectangle r;
2785 x_get_glyph_string_clip_rect (s, &r);
2786 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2787}
2788
2789
2790/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2791 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2792
2793static INLINE void
2794x_compute_glyph_string_overhangs (s)
2795 struct glyph_string *s;
2796{
b4192550 2797 if (s->cmp == NULL
06a2c219
GM
2798 && s->first_glyph->type == CHAR_GLYPH)
2799 {
2800 XCharStruct cs;
2801 int direction, font_ascent, font_descent;
2802 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2803 &font_ascent, &font_descent, &cs);
2804 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2805 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2806 }
2807}
2808
2809
2810/* Compute overhangs and x-positions for glyph string S and its
2811 predecessors, or successors. X is the starting x-position for S.
2812 BACKWARD_P non-zero means process predecessors. */
2813
2814static void
2815x_compute_overhangs_and_x (s, x, backward_p)
2816 struct glyph_string *s;
2817 int x;
2818 int backward_p;
2819{
2820 if (backward_p)
2821 {
2822 while (s)
2823 {
2824 x_compute_glyph_string_overhangs (s);
2825 x -= s->width;
2826 s->x = x;
2827 s = s->prev;
2828 }
2829 }
2830 else
2831 {
2832 while (s)
2833 {
2834 x_compute_glyph_string_overhangs (s);
2835 s->x = x;
2836 x += s->width;
2837 s = s->next;
2838 }
2839 }
2840}
2841
2842
2843/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2844 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2845 assumed to be zero. */
06a2c219
GM
2846
2847static void
2848x_get_glyph_overhangs (glyph, f, left, right)
2849 struct glyph *glyph;
2850 struct frame *f;
2851 int *left, *right;
2852{
2853 int c;
2854
2855 *left = *right = 0;
2856
b4192550 2857 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2858 {
2859 XFontStruct *font;
2860 struct face *face;
2861 struct font_info *font_info;
2862 XChar2b char2b;
ee569018
KH
2863 XCharStruct *pcm;
2864
2865 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2866 font = face->font;
2867 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2868 if (font
2869 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2870 {
06a2c219
GM
2871 if (pcm->rbearing > pcm->width)
2872 *right = pcm->rbearing - pcm->width;
2873 if (pcm->lbearing < 0)
2874 *left = -pcm->lbearing;
2875 }
2876 }
2877}
2878
2879
2880/* Return the index of the first glyph preceding glyph string S that
2881 is overwritten by S because of S's left overhang. Value is -1
2882 if no glyphs are overwritten. */
2883
2884static int
2885x_left_overwritten (s)
2886 struct glyph_string *s;
2887{
2888 int k;
2889
2890 if (s->left_overhang)
2891 {
2892 int x = 0, i;
2893 struct glyph *glyphs = s->row->glyphs[s->area];
2894 int first = s->first_glyph - glyphs;
2895
2896 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2897 x -= glyphs[i].pixel_width;
2898
2899 k = i + 1;
2900 }
2901 else
2902 k = -1;
2903
2904 return k;
2905}
2906
2907
2908/* Return the index of the first glyph preceding glyph string S that
2909 is overwriting S because of its right overhang. Value is -1 if no
2910 glyph in front of S overwrites S. */
2911
2912static int
2913x_left_overwriting (s)
2914 struct glyph_string *s;
2915{
2916 int i, k, x;
2917 struct glyph *glyphs = s->row->glyphs[s->area];
2918 int first = s->first_glyph - glyphs;
2919
2920 k = -1;
2921 x = 0;
2922 for (i = first - 1; i >= 0; --i)
2923 {
2924 int left, right;
2925 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2926 if (x + right > 0)
2927 k = i;
2928 x -= glyphs[i].pixel_width;
2929 }
2930
2931 return k;
2932}
2933
2934
2935/* Return the index of the last glyph following glyph string S that is
2936 not overwritten by S because of S's right overhang. Value is -1 if
2937 no such glyph is found. */
2938
2939static int
2940x_right_overwritten (s)
2941 struct glyph_string *s;
2942{
2943 int k = -1;
2944
2945 if (s->right_overhang)
2946 {
2947 int x = 0, i;
2948 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2949 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2950 int end = s->row->used[s->area];
2951
2952 for (i = first; i < end && s->right_overhang > x; ++i)
2953 x += glyphs[i].pixel_width;
2954
2955 k = i;
2956 }
2957
2958 return k;
2959}
2960
2961
2962/* Return the index of the last glyph following glyph string S that
2963 overwrites S because of its left overhang. Value is negative
2964 if no such glyph is found. */
2965
2966static int
2967x_right_overwriting (s)
2968 struct glyph_string *s;
2969{
2970 int i, k, x;
2971 int end = s->row->used[s->area];
2972 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2973 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2974
2975 k = -1;
2976 x = 0;
2977 for (i = first; i < end; ++i)
2978 {
2979 int left, right;
2980 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2981 if (x - left < 0)
2982 k = i;
2983 x += glyphs[i].pixel_width;
2984 }
2985
2986 return k;
2987}
2988
2989
2990/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2991
2992static INLINE void
2993x_clear_glyph_string_rect (s, x, y, w, h)
2994 struct glyph_string *s;
2995 int x, y, w, h;
2996{
2997 XGCValues xgcv;
2998 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2999 XSetForeground (s->display, s->gc, xgcv.background);
3000 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3001 XSetForeground (s->display, s->gc, xgcv.foreground);
3002}
3003
3004
3005/* Draw the background of glyph_string S. If S->background_filled_p
3006 is non-zero don't draw it. FORCE_P non-zero means draw the
3007 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3008 when a string preceding S draws into the background of S, or S
3009 contains the first component of a composition. */
06a2c219
GM
3010
3011static void
3012x_draw_glyph_string_background (s, force_p)
3013 struct glyph_string *s;
3014 int force_p;
3015{
3016 /* Nothing to do if background has already been drawn or if it
3017 shouldn't be drawn in the first place. */
3018 if (!s->background_filled_p)
3019 {
b4192550 3020 if (s->stippled_p)
06a2c219
GM
3021 {
3022 /* Fill background with a stipple pattern. */
3023 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3024 XFillRectangle (s->display, s->window, s->gc, s->x,
3025 s->y + s->face->box_line_width,
3026 s->background_width,
3027 s->height - 2 * s->face->box_line_width);
3028 XSetFillStyle (s->display, s->gc, FillSolid);
3029 s->background_filled_p = 1;
3030 }
3031 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3032 || s->font_not_found_p
3033 || s->extends_to_end_of_line_p
06a2c219
GM
3034 || force_p)
3035 {
3036 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3037 s->background_width,
3038 s->height - 2 * s->face->box_line_width);
3039 s->background_filled_p = 1;
3040 }
3041 }
3042}
3043
3044
3045/* Draw the foreground of glyph string S. */
3046
3047static void
3048x_draw_glyph_string_foreground (s)
3049 struct glyph_string *s;
3050{
3051 int i, x;
3052
3053 /* If first glyph of S has a left box line, start drawing the text
3054 of S to the right of that box line. */
3055 if (s->face->box != FACE_NO_BOX
3056 && s->first_glyph->left_box_line_p)
3057 x = s->x + s->face->box_line_width;
3058 else
3059 x = s->x;
3060
b4192550
KH
3061 /* Draw characters of S as rectangles if S's font could not be
3062 loaded. */
3063 if (s->font_not_found_p)
06a2c219 3064 {
b4192550 3065 for (i = 0; i < s->nchars; ++i)
06a2c219 3066 {
b4192550
KH
3067 struct glyph *g = s->first_glyph + i;
3068 XDrawRectangle (s->display, s->window,
3069 s->gc, x, s->y, g->pixel_width - 1,
3070 s->height - 1);
3071 x += g->pixel_width;
06a2c219
GM
3072 }
3073 }
3074 else
3075 {
b4192550
KH
3076 char *char1b = (char *) s->char2b;
3077 int boff = s->font_info->baseline_offset;
06a2c219 3078
b4192550
KH
3079 if (s->font_info->vertical_centering)
3080 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3081
3082 /* If we can use 8-bit functions, condense S->char2b. */
3083 if (!s->two_byte_p)
3084 for (i = 0; i < s->nchars; ++i)
3085 char1b[i] = s->char2b[i].byte2;
3086
3087 /* Draw text with XDrawString if background has already been
3088 filled. Otherwise, use XDrawImageString. (Note that
3089 XDrawImageString is usually faster than XDrawString.) Always
3090 use XDrawImageString when drawing the cursor so that there is
3091 no chance that characters under a box cursor are invisible. */
3092 if (s->for_overlaps_p
3093 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3094 {
3095 /* Draw characters with 16-bit or 8-bit functions. */
3096 if (s->two_byte_p)
3097 XDrawString16 (s->display, s->window, s->gc, x,
3098 s->ybase - boff, s->char2b, s->nchars);
3099 else
3100 XDrawString (s->display, s->window, s->gc, x,
3101 s->ybase - boff, char1b, s->nchars);
3102 }
06a2c219
GM
3103 else
3104 {
b4192550
KH
3105 if (s->two_byte_p)
3106 XDrawImageString16 (s->display, s->window, s->gc, x,
3107 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3108 else
b4192550
KH
3109 XDrawImageString (s->display, s->window, s->gc, x,
3110 s->ybase - boff, char1b, s->nchars);
3111 }
3112 }
3113}
06a2c219 3114
b4192550 3115/* Draw the foreground of composite glyph string S. */
06a2c219 3116
b4192550
KH
3117static void
3118x_draw_composite_glyph_string_foreground (s)
3119 struct glyph_string *s;
3120{
3121 int i, x;
06a2c219 3122
b4192550
KH
3123 /* If first glyph of S has a left box line, start drawing the text
3124 of S to the right of that box line. */
3125 if (s->face->box != FACE_NO_BOX
3126 && s->first_glyph->left_box_line_p)
3127 x = s->x + s->face->box_line_width;
3128 else
3129 x = s->x;
06a2c219 3130
b4192550
KH
3131 /* S is a glyph string for a composition. S->gidx is the index of
3132 the first character drawn for glyphs of this composition.
3133 S->gidx == 0 means we are drawing the very first character of
3134 this composition. */
06a2c219 3135
b4192550
KH
3136 /* Draw a rectangle for the composition if the font for the very
3137 first character of the composition could not be loaded. */
3138 if (s->font_not_found_p)
3139 {
3140 if (s->gidx == 0)
3141 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3142 s->width - 1, s->height - 1);
3143 }
3144 else
3145 {
3146 for (i = 0; i < s->nchars; i++, ++s->gidx)
3147 XDrawString16 (s->display, s->window, s->gc,
3148 x + s->cmp->offsets[s->gidx * 2],
3149 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3150 s->char2b + i, 1);
06a2c219
GM
3151 }
3152}
3153
3154
80c32bcc
GM
3155#ifdef USE_X_TOOLKIT
3156
3157/* Allocate the color COLOR->pixel on the screen and display of
3158 widget WIDGET in colormap CMAP. If an exact match cannot be
3159 allocated, try the nearest color available. Value is non-zero
3160 if successful. This is called from lwlib. */
3161
3162int
3163x_alloc_nearest_color_for_widget (widget, cmap, color)
3164 Widget widget;
3165 Colormap cmap;
3166 XColor *color;
3167{
3168 struct frame *f;
3169 struct x_display_info *dpyinfo;
5c187dee 3170 Lisp_Object tail;
80c32bcc
GM
3171
3172 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3173
3174 /* Find the top-level shell of the widget. Note that this function
3175 can be called when the widget is not yet realized, so XtWindow
3176 (widget) == 0. That's the reason we can't simply use
3177 x_any_window_to_frame. */
3178 while (!XtIsTopLevelShell (widget))
3179 widget = XtParent (widget);
3180
3181 /* Look for a frame with that top-level widget. Allocate the color
3182 on that frame to get the right gamma correction value. */
3183 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3184 if (GC_FRAMEP (XCAR (tail))
3185 && (f = XFRAME (XCAR (tail)),
3186 (f->output_data.nothing != 1
3187 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3188 && f->output_data.x->widget == widget)
3189 return x_alloc_nearest_color (f, cmap, color);
3190
3191 abort ();
3192}
3193
3194#endif /* USE_X_TOOLKIT */
3195
3196
06a2c219
GM
3197/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3198 CMAP. If an exact match can't be allocated, try the nearest color
3199 available. Value is non-zero if successful. Set *COLOR to the
3200 color allocated. */
3201
3202int
80c32bcc
GM
3203x_alloc_nearest_color (f, cmap, color)
3204 struct frame *f;
06a2c219
GM
3205 Colormap cmap;
3206 XColor *color;
3207{
80c32bcc
GM
3208 Display *display = FRAME_X_DISPLAY (f);
3209 Screen *screen = FRAME_X_SCREEN (f);
3210 int rc;
3211
3212 gamma_correct (f, color);
3213 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3214 if (rc == 0)
3215 {
3216 /* If we got to this point, the colormap is full, so we're going
3217 to try to get the next closest color. The algorithm used is
3218 a least-squares matching, which is what X uses for closest
3219 color matching with StaticColor visuals. */
3220 int nearest, i;
3221 unsigned long nearest_delta = ~0;
3222 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3223 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3224
3225 for (i = 0; i < ncells; ++i)
3226 cells[i].pixel = i;
3227 XQueryColors (display, cmap, cells, ncells);
3228
3229 for (nearest = i = 0; i < ncells; ++i)
3230 {
3231 long dred = (color->red >> 8) - (cells[i].red >> 8);
3232 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3233 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3234 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3235
3236 if (delta < nearest_delta)
3237 {
3238 nearest = i;
3239 nearest_delta = delta;
3240 }
3241 }
3242
3243 color->red = cells[nearest].red;
3244 color->green = cells[nearest].green;
3245 color->blue = cells[nearest].blue;
3246 rc = XAllocColor (display, cmap, color);
3247 }
3248
d9c545da
GM
3249#ifdef DEBUG_X_COLORS
3250 if (rc)
3251 register_color (color->pixel);
3252#endif /* DEBUG_X_COLORS */
3253
06a2c219
GM
3254 return rc;
3255}
3256
3257
d9c545da
GM
3258/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3259 It's necessary to do this instead of just using PIXEL directly to
3260 get color reference counts right. */
3261
3262unsigned long
3263x_copy_color (f, pixel)
3264 struct frame *f;
3265 unsigned long pixel;
3266{
3267 XColor color;
3268
3269 color.pixel = pixel;
3270 BLOCK_INPUT;
3271 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3272 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3273 UNBLOCK_INPUT;
3274#ifdef DEBUG_X_COLORS
3275 register_color (pixel);
3276#endif
3277 return color.pixel;
3278}
3279
3280
06a2c219
GM
3281/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3282 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3283 If this produces the same color as PIXEL, try a color where all RGB
3284 values have DELTA added. Return the allocated color in *PIXEL.
3285 DISPLAY is the X display, CMAP is the colormap to operate on.
3286 Value is non-zero if successful. */
3287
3288static int
3289x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3290 struct frame *f;
3291 Display *display;
3292 Colormap cmap;
3293 unsigned long *pixel;
68c45bf0 3294 double factor;
06a2c219
GM
3295 int delta;
3296{
3297 XColor color, new;
3298 int success_p;
3299
3300 /* Get RGB color values. */
3301 color.pixel = *pixel;
3302 XQueryColor (display, cmap, &color);
3303
3304 /* Change RGB values by specified FACTOR. Avoid overflow! */
3305 xassert (factor >= 0);
3306 new.red = min (0xffff, factor * color.red);
3307 new.green = min (0xffff, factor * color.green);
3308 new.blue = min (0xffff, factor * color.blue);
3309
3310 /* Try to allocate the color. */
80c32bcc 3311 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3312 if (success_p)
3313 {
3314 if (new.pixel == *pixel)
3315 {
3316 /* If we end up with the same color as before, try adding
3317 delta to the RGB values. */
0d605c67 3318 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3319
3320 new.red = min (0xffff, delta + color.red);
3321 new.green = min (0xffff, delta + color.green);
3322 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3323 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3324 }
3325 else
3326 success_p = 1;
3327 *pixel = new.pixel;
3328 }
3329
3330 return success_p;
3331}
3332
3333
3334/* Set up the foreground color for drawing relief lines of glyph
3335 string S. RELIEF is a pointer to a struct relief containing the GC
3336 with which lines will be drawn. Use a color that is FACTOR or
3337 DELTA lighter or darker than the relief's background which is found
3338 in S->f->output_data.x->relief_background. If such a color cannot
3339 be allocated, use DEFAULT_PIXEL, instead. */
3340
3341static void
3342x_setup_relief_color (f, relief, factor, delta, default_pixel)
3343 struct frame *f;
3344 struct relief *relief;
68c45bf0 3345 double factor;
06a2c219
GM
3346 int delta;
3347 unsigned long default_pixel;
3348{
3349 XGCValues xgcv;
3350 struct x_output *di = f->output_data.x;
3351 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3352 unsigned long pixel;
3353 unsigned long background = di->relief_background;
43bd1b2b 3354 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3355 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3356 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3357
3358 xgcv.graphics_exposures = False;
3359 xgcv.line_width = 1;
3360
3361 /* Free previously allocated color. The color cell will be reused
3362 when it has been freed as many times as it was allocated, so this
3363 doesn't affect faces using the same colors. */
3364 if (relief->gc
3365 && relief->allocated_p)
3366 {
3367 /* If display has an immutable color map, freeing colors is not
3368 necessary and some servers don't allow it. So don't do it. */
0d605c67 3369 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3370 relief->allocated_p = 0;
3371 }
3372
3373 /* Allocate new color. */
3374 xgcv.foreground = default_pixel;
3375 pixel = background;
dcd08bfb
GM
3376 if (dpyinfo->n_planes != 1
3377 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3378 {
3379 relief->allocated_p = 1;
3380 xgcv.foreground = relief->pixel = pixel;
3381 }
3382
3383 if (relief->gc == 0)
3384 {
dcd08bfb 3385 xgcv.stipple = dpyinfo->gray;
06a2c219 3386 mask |= GCStipple;
dcd08bfb 3387 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3388 }
3389 else
dcd08bfb 3390 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3391}
3392
3393
3394/* Set up colors for the relief lines around glyph string S. */
3395
3396static void
3397x_setup_relief_colors (s)
3398 struct glyph_string *s;
3399{
3400 struct x_output *di = s->f->output_data.x;
3401 unsigned long color;
3402
3403 if (s->face->use_box_color_for_shadows_p)
3404 color = s->face->box_color;
3405 else
3406 {
3407 XGCValues xgcv;
3408
3409 /* Get the background color of the face. */
3410 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3411 color = xgcv.background;
3412 }
3413
3414 if (di->white_relief.gc == 0
3415 || color != di->relief_background)
3416 {
3417 di->relief_background = color;
3418 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3419 WHITE_PIX_DEFAULT (s->f));
3420 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3421 BLACK_PIX_DEFAULT (s->f));
3422 }
3423}
3424
3425
3426/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3427 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3428 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3429 relief. LEFT_P non-zero means draw a relief on the left side of
3430 the rectangle. RIGHT_P non-zero means draw a relief on the right
3431 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3432 when drawing. */
3433
3434static void
3435x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3436 raised_p, left_p, right_p, clip_rect)
3437 struct frame *f;
3438 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3439 XRectangle *clip_rect;
3440{
3441 int i;
3442 GC gc;
3443
3444 if (raised_p)
3445 gc = f->output_data.x->white_relief.gc;
3446 else
3447 gc = f->output_data.x->black_relief.gc;
3448 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3449
3450 /* Top. */
3451 for (i = 0; i < width; ++i)
3452 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3453 left_x + i * left_p, top_y + i,
3454 right_x + 1 - i * right_p, top_y + i);
3455
3456 /* Left. */
3457 if (left_p)
3458 for (i = 0; i < width; ++i)
3459 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3460 left_x + i, top_y + i, left_x + i, bottom_y - i);
3461
3462 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3463 if (raised_p)
3464 gc = f->output_data.x->black_relief.gc;
3465 else
3466 gc = f->output_data.x->white_relief.gc;
3467 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3468
3469 /* Bottom. */
3470 for (i = 0; i < width; ++i)
3471 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3472 left_x + i * left_p, bottom_y - i,
3473 right_x + 1 - i * right_p, bottom_y - i);
3474
3475 /* Right. */
3476 if (right_p)
3477 for (i = 0; i < width; ++i)
3478 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3479 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3480
3481 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3482}
3483
3484
3485/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3486 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3487 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3488 left side of the rectangle. RIGHT_P non-zero means draw a line
3489 on the right side of the rectangle. CLIP_RECT is the clipping
3490 rectangle to use when drawing. */
3491
3492static void
3493x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3494 left_p, right_p, clip_rect)
3495 struct glyph_string *s;
3496 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3497 XRectangle *clip_rect;
3498{
3499 XGCValues xgcv;
3500
3501 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3502 XSetForeground (s->display, s->gc, s->face->box_color);
3503 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3504
3505 /* Top. */
3506 XFillRectangle (s->display, s->window, s->gc,
3507 left_x, top_y, right_x - left_x, width);
3508
3509 /* Left. */
3510 if (left_p)
3511 XFillRectangle (s->display, s->window, s->gc,
3512 left_x, top_y, width, bottom_y - top_y);
3513
3514 /* Bottom. */
3515 XFillRectangle (s->display, s->window, s->gc,
3516 left_x, bottom_y - width, right_x - left_x, width);
3517
3518 /* Right. */
3519 if (right_p)
3520 XFillRectangle (s->display, s->window, s->gc,
3521 right_x - width, top_y, width, bottom_y - top_y);
3522
3523 XSetForeground (s->display, s->gc, xgcv.foreground);
3524 XSetClipMask (s->display, s->gc, None);
3525}
3526
3527
3528/* Draw a box around glyph string S. */
3529
3530static void
3531x_draw_glyph_string_box (s)
3532 struct glyph_string *s;
3533{
3534 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3535 int left_p, right_p;
3536 struct glyph *last_glyph;
3537 XRectangle clip_rect;
3538
3539 last_x = window_box_right (s->w, s->area);
3540 if (s->row->full_width_p
3541 && !s->w->pseudo_window_p)
3542 {
110859fc 3543 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3544 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3545 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3546 }
3547
3548 /* The glyph that may have a right box line. */
b4192550 3549 last_glyph = (s->cmp || s->img
06a2c219
GM
3550 ? s->first_glyph
3551 : s->first_glyph + s->nchars - 1);
3552
3553 width = s->face->box_line_width;
3554 raised_p = s->face->box == FACE_RAISED_BOX;
3555 left_x = s->x;
3556 right_x = ((s->row->full_width_p
1da3fd71 3557 ? last_x - 1
a7aeb2de 3558 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3559 top_y = s->y;
3560 bottom_y = top_y + s->height - 1;
3561
3562 left_p = (s->first_glyph->left_box_line_p
3563 || (s->hl == DRAW_MOUSE_FACE
3564 && (s->prev == NULL
3565 || s->prev->hl != s->hl)));
3566 right_p = (last_glyph->right_box_line_p
3567 || (s->hl == DRAW_MOUSE_FACE
3568 && (s->next == NULL
3569 || s->next->hl != s->hl)));
3570
3571 x_get_glyph_string_clip_rect (s, &clip_rect);
3572
3573 if (s->face->box == FACE_SIMPLE_BOX)
3574 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3575 left_p, right_p, &clip_rect);
3576 else
3577 {
3578 x_setup_relief_colors (s);
3579 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3580 width, raised_p, left_p, right_p, &clip_rect);
3581 }
3582}
3583
3584
3585/* Draw foreground of image glyph string S. */
3586
3587static void
3588x_draw_image_foreground (s)
3589 struct glyph_string *s;
3590{
3591 int x;
95af8492 3592 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3593
3594 /* If first glyph of S has a left box line, start drawing it to the
3595 right of that line. */
3596 if (s->face->box != FACE_NO_BOX
3597 && s->first_glyph->left_box_line_p)
3598 x = s->x + s->face->box_line_width;
3599 else
3600 x = s->x;
3601
3602 /* If there is a margin around the image, adjust x- and y-position
3603 by that margin. */
3604 if (s->img->margin)
3605 {
3606 x += s->img->margin;
3607 y += s->img->margin;
3608 }
3609
3610 if (s->img->pixmap)
3611 {
3612 if (s->img->mask)
3613 {
3614 /* We can't set both a clip mask and use XSetClipRectangles
3615 because the latter also sets a clip mask. We also can't
3616 trust on the shape extension to be available
3617 (XShapeCombineRegion). So, compute the rectangle to draw
3618 manually. */
3619 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3620 | GCFunction);
3621 XGCValues xgcv;
3622 XRectangle clip_rect, image_rect, r;
3623
3624 xgcv.clip_mask = s->img->mask;
3625 xgcv.clip_x_origin = x;
3626 xgcv.clip_y_origin = y;
3627 xgcv.function = GXcopy;
3628 XChangeGC (s->display, s->gc, mask, &xgcv);
3629
3630 x_get_glyph_string_clip_rect (s, &clip_rect);
3631 image_rect.x = x;
3632 image_rect.y = y;
3633 image_rect.width = s->img->width;
3634 image_rect.height = s->img->height;
3635 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3636 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3637 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3638 }
3639 else
3640 {
3641 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3642 0, 0, s->img->width, s->img->height, x, y);
3643
3644 /* When the image has a mask, we can expect that at
3645 least part of a mouse highlight or a block cursor will
3646 be visible. If the image doesn't have a mask, make
3647 a block cursor visible by drawing a rectangle around
3648 the image. I believe it's looking better if we do
3649 nothing here for mouse-face. */
3650 if (s->hl == DRAW_CURSOR)
3651 XDrawRectangle (s->display, s->window, s->gc, x, y,
3652 s->img->width - 1, s->img->height - 1);
3653 }
3654 }
3655 else
3656 /* Draw a rectangle if image could not be loaded. */
3657 XDrawRectangle (s->display, s->window, s->gc, x, y,
3658 s->img->width - 1, s->img->height - 1);
3659}
3660
3661
3662/* Draw a relief around the image glyph string S. */
3663
3664static void
3665x_draw_image_relief (s)
3666 struct glyph_string *s;
3667{
3668 int x0, y0, x1, y1, thick, raised_p;
3669 XRectangle r;
3670 int x;
95af8492 3671 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3672
3673 /* If first glyph of S has a left box line, start drawing it to the
3674 right of that line. */
3675 if (s->face->box != FACE_NO_BOX
3676 && s->first_glyph->left_box_line_p)
3677 x = s->x + s->face->box_line_width;
3678 else
3679 x = s->x;
3680
3681 /* If there is a margin around the image, adjust x- and y-position
3682 by that margin. */
3683 if (s->img->margin)
3684 {
3685 x += s->img->margin;
3686 y += s->img->margin;
3687 }
3688
3689 if (s->hl == DRAW_IMAGE_SUNKEN
3690 || s->hl == DRAW_IMAGE_RAISED)
3691 {
9ea173e8 3692 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3693 raised_p = s->hl == DRAW_IMAGE_RAISED;
3694 }
3695 else
3696 {
3697 thick = abs (s->img->relief);
3698 raised_p = s->img->relief > 0;
3699 }
3700
3701 x0 = x - thick;
3702 y0 = y - thick;
3703 x1 = x + s->img->width + thick - 1;
3704 y1 = y + s->img->height + thick - 1;
3705
3706 x_setup_relief_colors (s);
3707 x_get_glyph_string_clip_rect (s, &r);
3708 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3709}
3710
3711
3712/* Draw the foreground of image glyph string S to PIXMAP. */
3713
3714static void
3715x_draw_image_foreground_1 (s, pixmap)
3716 struct glyph_string *s;
3717 Pixmap pixmap;
3718{
3719 int x;
95af8492 3720 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3721
3722 /* If first glyph of S has a left box line, start drawing it to the
3723 right of that line. */
3724 if (s->face->box != FACE_NO_BOX
3725 && s->first_glyph->left_box_line_p)
3726 x = s->face->box_line_width;
3727 else
3728 x = 0;
3729
3730 /* If there is a margin around the image, adjust x- and y-position
3731 by that margin. */
3732 if (s->img->margin)
3733 {
3734 x += s->img->margin;
3735 y += s->img->margin;
3736 }
dc43ef94 3737
06a2c219
GM
3738 if (s->img->pixmap)
3739 {
3740 if (s->img->mask)
3741 {
3742 /* We can't set both a clip mask and use XSetClipRectangles
3743 because the latter also sets a clip mask. We also can't
3744 trust on the shape extension to be available
3745 (XShapeCombineRegion). So, compute the rectangle to draw
3746 manually. */
3747 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3748 | GCFunction);
3749 XGCValues xgcv;
3750
3751 xgcv.clip_mask = s->img->mask;
3752 xgcv.clip_x_origin = x;
3753 xgcv.clip_y_origin = y;
3754 xgcv.function = GXcopy;
3755 XChangeGC (s->display, s->gc, mask, &xgcv);
3756
3757 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3758 0, 0, s->img->width, s->img->height, x, y);
3759 XSetClipMask (s->display, s->gc, None);
3760 }
3761 else
3762 {
3763 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3764 0, 0, s->img->width, s->img->height, x, y);
3765
3766 /* When the image has a mask, we can expect that at
3767 least part of a mouse highlight or a block cursor will
3768 be visible. If the image doesn't have a mask, make
3769 a block cursor visible by drawing a rectangle around
3770 the image. I believe it's looking better if we do
3771 nothing here for mouse-face. */
3772 if (s->hl == DRAW_CURSOR)
3773 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3774 s->img->width - 1, s->img->height - 1);
3775 }
3776 }
3777 else
3778 /* Draw a rectangle if image could not be loaded. */
3779 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3780 s->img->width - 1, s->img->height - 1);
3781}
dc43ef94 3782
990ba854 3783
06a2c219
GM
3784/* Draw part of the background of glyph string S. X, Y, W, and H
3785 give the rectangle to draw. */
a9a5b0a5 3786
06a2c219
GM
3787static void
3788x_draw_glyph_string_bg_rect (s, x, y, w, h)
3789 struct glyph_string *s;
3790 int x, y, w, h;
3791{
3792 if (s->stippled_p)
3793 {
3794 /* Fill background with a stipple pattern. */
3795 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3796 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3797 XSetFillStyle (s->display, s->gc, FillSolid);
3798 }
3799 else
3800 x_clear_glyph_string_rect (s, x, y, w, h);
3801}
07e34cb0 3802
b5210ea7 3803
06a2c219 3804/* Draw image glyph string S.
dc43ef94 3805
06a2c219
GM
3806 s->y
3807 s->x +-------------------------
3808 | s->face->box
3809 |
3810 | +-------------------------
3811 | | s->img->margin
3812 | |
3813 | | +-------------------
3814 | | | the image
dc43ef94 3815
06a2c219 3816 */
dc43ef94 3817
06a2c219
GM
3818static void
3819x_draw_image_glyph_string (s)
3820 struct glyph_string *s;
3821{
3822 int x, y;
3823 int box_line_width = s->face->box_line_width;
3824 int margin = s->img->margin;
3825 int height;
3826 Pixmap pixmap = None;
3827
3828 height = s->height - 2 * box_line_width;
3829
3830 /* Fill background with face under the image. Do it only if row is
3831 taller than image or if image has a clip mask to reduce
3832 flickering. */
3833 s->stippled_p = s->face->stipple != 0;
3834 if (height > s->img->height
3835 || margin
3836 || s->img->mask
3837 || s->img->pixmap == 0
3838 || s->width != s->background_width)
3839 {
3840 if (box_line_width && s->first_glyph->left_box_line_p)
3841 x = s->x + box_line_width;
3842 else
3843 x = s->x;
3844
3845 y = s->y + box_line_width;
3846
3847 if (s->img->mask)
3848 {
3849 /* Create a pixmap as large as the glyph string Fill it with
3850 the background color. Copy the image to it, using its
3851 mask. Copy the temporary pixmap to the display. */
3852 Screen *screen = FRAME_X_SCREEN (s->f);
3853 int depth = DefaultDepthOfScreen (screen);
3854
3855 /* Create a pixmap as large as the glyph string. */
3856 pixmap = XCreatePixmap (s->display, s->window,
3857 s->background_width,
3858 s->height, depth);
3859
3860 /* Don't clip in the following because we're working on the
3861 pixmap. */
3862 XSetClipMask (s->display, s->gc, None);
3863
3864 /* Fill the pixmap with the background color/stipple. */
3865 if (s->stippled_p)
3866 {
3867 /* Fill background with a stipple pattern. */
3868 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3869 XFillRectangle (s->display, pixmap, s->gc,
3870 0, 0, s->background_width, s->height);
3871 XSetFillStyle (s->display, s->gc, FillSolid);
3872 }
3873 else
3874 {
3875 XGCValues xgcv;
3876 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3877 &xgcv);
3878 XSetForeground (s->display, s->gc, xgcv.background);
3879 XFillRectangle (s->display, pixmap, s->gc,
3880 0, 0, s->background_width, s->height);
3881 XSetForeground (s->display, s->gc, xgcv.foreground);
3882 }
3883 }
3884 else
3885 /* Implementation idea: Is it possible to construct a mask?
3886 We could look at the color at the margins of the image, and
3887 say that this color is probably the background color of the
3888 image. */
3889 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3890
3891 s->background_filled_p = 1;
3892 }
dc43ef94 3893
06a2c219
GM
3894 /* Draw the foreground. */
3895 if (pixmap != None)
3896 {
3897 x_draw_image_foreground_1 (s, pixmap);
3898 x_set_glyph_string_clipping (s);
3899 XCopyArea (s->display, pixmap, s->window, s->gc,
3900 0, 0, s->background_width, s->height, s->x, s->y);
3901 XFreePixmap (s->display, pixmap);
3902 }
3903 else
3904 x_draw_image_foreground (s);
b5210ea7 3905
06a2c219
GM
3906 /* If we must draw a relief around the image, do it. */
3907 if (s->img->relief
3908 || s->hl == DRAW_IMAGE_RAISED
3909 || s->hl == DRAW_IMAGE_SUNKEN)
3910 x_draw_image_relief (s);
3911}
8c1a6a84 3912
990ba854 3913
06a2c219 3914/* Draw stretch glyph string S. */
dc43ef94 3915
06a2c219
GM
3916static void
3917x_draw_stretch_glyph_string (s)
3918 struct glyph_string *s;
3919{
3920 xassert (s->first_glyph->type == STRETCH_GLYPH);
3921 s->stippled_p = s->face->stipple != 0;
990ba854 3922
06a2c219
GM
3923 if (s->hl == DRAW_CURSOR
3924 && !x_stretch_cursor_p)
3925 {
3926 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3927 as wide as the stretch glyph. */
3928 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3929
06a2c219
GM
3930 /* Draw cursor. */
3931 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3932
06a2c219
GM
3933 /* Clear rest using the GC of the original non-cursor face. */
3934 if (width < s->background_width)
3935 {
3936 GC gc = s->face->gc;
3937 int x = s->x + width, y = s->y;
3938 int w = s->background_width - width, h = s->height;
3939 XRectangle r;
dc43ef94 3940
06a2c219
GM
3941 x_get_glyph_string_clip_rect (s, &r);
3942 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3943
06a2c219
GM
3944 if (s->face->stipple)
3945 {
3946 /* Fill background with a stipple pattern. */
3947 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3948 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3949 XSetFillStyle (s->display, gc, FillSolid);
3950 }
3951 else
3952 {
3953 XGCValues xgcv;
3954 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3955 XSetForeground (s->display, gc, xgcv.background);
3956 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3957 XSetForeground (s->display, gc, xgcv.foreground);
3958 }
3959 }
3960 }
3961 else
3962 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3963 s->height);
3964
3965 s->background_filled_p = 1;
3966}
3967
3968
3969/* Draw glyph string S. */
3970
3971static void
3972x_draw_glyph_string (s)
3973 struct glyph_string *s;
3974{
3975 /* If S draws into the background of its successor, draw the
3976 background of the successor first so that S can draw into it.
3977 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3978 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3979 {
3980 xassert (s->next->img == NULL);
3981 x_set_glyph_string_gc (s->next);
3982 x_set_glyph_string_clipping (s->next);
3983 x_draw_glyph_string_background (s->next, 1);
3984 }
97210f4e 3985
06a2c219
GM
3986 /* Set up S->gc, set clipping and draw S. */
3987 x_set_glyph_string_gc (s);
3988 x_set_glyph_string_clipping (s);
3989
3990 switch (s->first_glyph->type)
3991 {
3992 case IMAGE_GLYPH:
3993 x_draw_image_glyph_string (s);
3994 break;
3995
3996 case STRETCH_GLYPH:
3997 x_draw_stretch_glyph_string (s);
3998 break;
3999
4000 case CHAR_GLYPH:
66ac4b0e
GM
4001 if (s->for_overlaps_p)
4002 s->background_filled_p = 1;
4003 else
4004 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4005 x_draw_glyph_string_foreground (s);
4006 break;
4007
b4192550
KH
4008 case COMPOSITE_GLYPH:
4009 if (s->for_overlaps_p || s->gidx > 0)
4010 s->background_filled_p = 1;
4011 else
4012 x_draw_glyph_string_background (s, 1);
4013 x_draw_composite_glyph_string_foreground (s);
4014 break;
4015
06a2c219
GM
4016 default:
4017 abort ();
4018 }
4019
66ac4b0e 4020 if (!s->for_overlaps_p)
06a2c219 4021 {
66ac4b0e
GM
4022 /* Draw underline. */
4023 if (s->face->underline_p)
4024 {
4025 unsigned long dy, h;
06a2c219 4026
66ac4b0e
GM
4027 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4028 h = 1;
4029 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4030 dy = s->height - h;
06a2c219 4031
66ac4b0e
GM
4032 if (s->face->underline_defaulted_p)
4033 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4034 s->width, h);
4035 else
4036 {
4037 XGCValues xgcv;
4038 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4039 XSetForeground (s->display, s->gc, s->face->underline_color);
4040 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4041 s->width, h);
4042 XSetForeground (s->display, s->gc, xgcv.foreground);
4043 }
dc6f92b8 4044 }
07e34cb0 4045
66ac4b0e
GM
4046 /* Draw overline. */
4047 if (s->face->overline_p)
06a2c219 4048 {
66ac4b0e
GM
4049 unsigned long dy = 0, h = 1;
4050
4051 if (s->face->overline_color_defaulted_p)
4052 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4053 s->width, h);
4054 else
4055 {
4056 XGCValues xgcv;
4057 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4058 XSetForeground (s->display, s->gc, s->face->overline_color);
4059 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4060 s->width, h);
4061 XSetForeground (s->display, s->gc, xgcv.foreground);
4062 }
06a2c219 4063 }
06a2c219 4064
66ac4b0e
GM
4065 /* Draw strike-through. */
4066 if (s->face->strike_through_p)
06a2c219 4067 {
66ac4b0e
GM
4068 unsigned long h = 1;
4069 unsigned long dy = (s->height - h) / 2;
4070
4071 if (s->face->strike_through_color_defaulted_p)
4072 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4073 s->width, h);
4074 else
4075 {
4076 XGCValues xgcv;
4077 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4078 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4079 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4080 s->width, h);
4081 XSetForeground (s->display, s->gc, xgcv.foreground);
4082 }
06a2c219 4083 }
06a2c219 4084
66ac4b0e
GM
4085 /* Draw relief. */
4086 if (s->face->box != FACE_NO_BOX)
4087 x_draw_glyph_string_box (s);
4088 }
06a2c219
GM
4089
4090 /* Reset clipping. */
4091 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4092}
07e34cb0 4093
06a2c219 4094
b4192550
KH
4095static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4096 struct face **, int));
06a2c219 4097
06a2c219 4098
b4192550
KH
4099/* Load glyph string S with a composition components specified by S->cmp.
4100 FACES is an array of faces for all components of this composition.
4101 S->gidx is the index of the first component for S.
4102 OVERLAPS_P non-zero means S should draw the foreground only, and
4103 use its lines physical height for clipping.
06a2c219 4104
b4192550 4105 Value is the index of a component not in S. */
07e34cb0 4106
b4192550
KH
4107static int
4108x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4109 struct glyph_string *s;
b4192550 4110 struct face **faces;
66ac4b0e 4111 int overlaps_p;
07e34cb0 4112{
b4192550 4113 int i;
06a2c219 4114
b4192550 4115 xassert (s);
06a2c219 4116
b4192550 4117 s->for_overlaps_p = overlaps_p;
06a2c219 4118
b4192550
KH
4119 s->face = faces[s->gidx];
4120 s->font = s->face->font;
4121 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4122
b4192550
KH
4123 /* For all glyphs of this composition, starting at the offset
4124 S->gidx, until we reach the end of the definition or encounter a
4125 glyph that requires the different face, add it to S. */
4126 ++s->nchars;
4127 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4128 ++s->nchars;
06a2c219 4129
b4192550
KH
4130 /* All glyph strings for the same composition has the same width,
4131 i.e. the width set for the first component of the composition. */
06a2c219 4132
06a2c219
GM
4133 s->width = s->first_glyph->pixel_width;
4134
4135 /* If the specified font could not be loaded, use the frame's
4136 default font, but record the fact that we couldn't load it in
4137 the glyph string so that we can draw rectangles for the
4138 characters of the glyph string. */
4139 if (s->font == NULL)
4140 {
4141 s->font_not_found_p = 1;
4142 s->font = FRAME_FONT (s->f);
4143 }
4144
4145 /* Adjust base line for subscript/superscript text. */
4146 s->ybase += s->first_glyph->voffset;
4147
4148 xassert (s->face && s->face->gc);
4149
4150 /* This glyph string must always be drawn with 16-bit functions. */
4151 s->two_byte_p = 1;
b4192550
KH
4152
4153 return s->gidx + s->nchars;
06a2c219
GM
4154}
4155
4156
b4192550 4157/* Load glyph string S with a sequence characters.
06a2c219 4158 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4159 first glyph to consider, END is the index of the last + 1.
4160 OVERLAPS_P non-zero means S should draw the foreground only, and
4161 use its lines physical height for clipping.
4162
4163 Value is the index of the first glyph not in S. */
06a2c219
GM
4164
4165static int
66ac4b0e 4166x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4167 struct glyph_string *s;
4168 int face_id;
66ac4b0e 4169 int start, end, overlaps_p;
06a2c219
GM
4170{
4171 struct glyph *glyph, *last;
4172 int voffset;
ee569018 4173 int glyph_not_available_p;
06a2c219 4174
06a2c219
GM
4175 xassert (s->f == XFRAME (s->w->frame));
4176 xassert (s->nchars == 0);
4177 xassert (start >= 0 && end > start);
4178
66ac4b0e 4179 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4180 glyph = s->row->glyphs[s->area] + start;
4181 last = s->row->glyphs[s->area] + end;
4182 voffset = glyph->voffset;
4183
ee569018
KH
4184 glyph_not_available_p = glyph->glyph_not_available_p;
4185
06a2c219
GM
4186 while (glyph < last
4187 && glyph->type == CHAR_GLYPH
4188 && glyph->voffset == voffset
ee569018
KH
4189 /* Same face id implies same font, nowadays. */
4190 && glyph->face_id == face_id
4191 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4192 {
ee569018
KH
4193 int two_byte_p;
4194
06a2c219 4195 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4196 s->char2b + s->nchars,
4197 &two_byte_p);
4198 s->two_byte_p = two_byte_p;
06a2c219
GM
4199 ++s->nchars;
4200 xassert (s->nchars <= end - start);
4201 s->width += glyph->pixel_width;
4202 ++glyph;
4203 }
4204
4205 s->font = s->face->font;
4206 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4207
4208 /* If the specified font could not be loaded, use the frame's font,
4209 but record the fact that we couldn't load it in
4210 S->font_not_found_p so that we can draw rectangles for the
4211 characters of the glyph string. */
ee569018 4212 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4213 {
4214 s->font_not_found_p = 1;
4215 s->font = FRAME_FONT (s->f);
4216 }
4217
4218 /* Adjust base line for subscript/superscript text. */
4219 s->ybase += voffset;
66ac4b0e 4220
06a2c219
GM
4221 xassert (s->face && s->face->gc);
4222 return glyph - s->row->glyphs[s->area];
07e34cb0 4223}
dc6f92b8 4224
06a2c219
GM
4225
4226/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4227
dfcf069d 4228static void
06a2c219
GM
4229x_fill_image_glyph_string (s)
4230 struct glyph_string *s;
4231{
4232 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4233 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4234 xassert (s->img);
43d120d8 4235 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4236 s->font = s->face->font;
4237 s->width = s->first_glyph->pixel_width;
4238
4239 /* Adjust base line for subscript/superscript text. */
4240 s->ybase += s->first_glyph->voffset;
4241}
4242
4243
4244/* Fill glyph string S from stretch glyph S->first_glyph. */
4245
4246static void
4247x_fill_stretch_glyph_string (s)
4248 struct glyph_string *s;
4249{
4250 xassert (s->first_glyph->type == STRETCH_GLYPH);
43d120d8 4251 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4252 s->font = s->face->font;
4253 s->width = s->first_glyph->pixel_width;
4254
4255 /* Adjust base line for subscript/superscript text. */
4256 s->ybase += s->first_glyph->voffset;
4257}
4258
4259
4260/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4261 of XChar2b structures for S; it can't be allocated in
4262 x_init_glyph_string because it must be allocated via `alloca'. W
4263 is the window on which S is drawn. ROW and AREA are the glyph row
4264 and area within the row from which S is constructed. START is the
4265 index of the first glyph structure covered by S. HL is a
4266 face-override for drawing S. */
4267
4268static void
4269x_init_glyph_string (s, char2b, w, row, area, start, hl)
4270 struct glyph_string *s;
4271 XChar2b *char2b;
4272 struct window *w;
4273 struct glyph_row *row;
4274 enum glyph_row_area area;
4275 int start;
4276 enum draw_glyphs_face hl;
4277{
4278 bzero (s, sizeof *s);
4279 s->w = w;
4280 s->f = XFRAME (w->frame);
4281 s->display = FRAME_X_DISPLAY (s->f);
4282 s->window = FRAME_X_WINDOW (s->f);
4283 s->char2b = char2b;
4284 s->hl = hl;
4285 s->row = row;
4286 s->area = area;
4287 s->first_glyph = row->glyphs[area] + start;
4288 s->height = row->height;
4289 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4290
9ea173e8
GM
4291 /* Display the internal border below the tool-bar window. */
4292 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4293 s->y -= s->f->output_data.x->internal_border_width;
4294
4295 s->ybase = s->y + row->ascent;
4296}
4297
4298
4299/* Set background width of glyph string S. START is the index of the
4300 first glyph following S. LAST_X is the right-most x-position + 1
4301 in the drawing area. */
4302
4303static INLINE void
4304x_set_glyph_string_background_width (s, start, last_x)
4305 struct glyph_string *s;
4306 int start;
4307 int last_x;
4308{
4309 /* If the face of this glyph string has to be drawn to the end of
4310 the drawing area, set S->extends_to_end_of_line_p. */
4311 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4312
4313 if (start == s->row->used[s->area]
4314 && s->hl == DRAW_NORMAL_TEXT
4315 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4316 || s->face->background != default_face->background
4317 || s->face->stipple != default_face->stipple))
4318 s->extends_to_end_of_line_p = 1;
4319
4320 /* If S extends its face to the end of the line, set its
4321 background_width to the distance to the right edge of the drawing
4322 area. */
4323 if (s->extends_to_end_of_line_p)
1da3fd71 4324 s->background_width = last_x - s->x + 1;
06a2c219
GM
4325 else
4326 s->background_width = s->width;
4327}
4328
4329
4330/* Add a glyph string for a stretch glyph to the list of strings
4331 between HEAD and TAIL. START is the index of the stretch glyph in
4332 row area AREA of glyph row ROW. END is the index of the last glyph
4333 in that glyph row area. X is the current output position assigned
4334 to the new glyph string constructed. HL overrides that face of the
4335 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4336 is the right-most x-position of the drawing area. */
4337
8abee2e1
DL
4338/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4339 and below -- keep them on one line. */
4340#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4341 do \
4342 { \
4343 s = (struct glyph_string *) alloca (sizeof *s); \
4344 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4345 x_fill_stretch_glyph_string (s); \
4346 x_append_glyph_string (&HEAD, &TAIL, s); \
4347 ++START; \
4348 s->x = (X); \
4349 } \
4350 while (0)
4351
4352
4353/* Add a glyph string for an image glyph to the list of strings
4354 between HEAD and TAIL. START is the index of the image glyph in
4355 row area AREA of glyph row ROW. END is the index of the last glyph
4356 in that glyph row area. X is the current output position assigned
4357 to the new glyph string constructed. HL overrides that face of the
4358 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4359 is the right-most x-position of the drawing area. */
4360
8abee2e1 4361#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4362 do \
4363 { \
4364 s = (struct glyph_string *) alloca (sizeof *s); \
4365 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4366 x_fill_image_glyph_string (s); \
4367 x_append_glyph_string (&HEAD, &TAIL, s); \
4368 ++START; \
4369 s->x = (X); \
4370 } \
4371 while (0)
4372
4373
4374/* Add a glyph string for a sequence of character glyphs to the list
4375 of strings between HEAD and TAIL. START is the index of the first
4376 glyph in row area AREA of glyph row ROW that is part of the new
4377 glyph string. END is the index of the last glyph in that glyph row
4378 area. X is the current output position assigned to the new glyph
4379 string constructed. HL overrides that face of the glyph; e.g. it
4380 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4381 right-most x-position of the drawing area. */
4382
8abee2e1 4383#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4384 do \
4385 { \
4386 int c, charset, face_id; \
4387 XChar2b *char2b; \
4388 \
43d120d8 4389 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4390 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4391 \
b4192550
KH
4392 s = (struct glyph_string *) alloca (sizeof *s); \
4393 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4394 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4395 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4396 s->x = (X); \
4397 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4398 OVERLAPS_P); \
06a2c219
GM
4399 } \
4400 while (0)
4401
4402
b4192550
KH
4403/* Add a glyph string for a composite sequence to the list of strings
4404 between HEAD and TAIL. START is the index of the first glyph in
4405 row area AREA of glyph row ROW that is part of the new glyph
4406 string. END is the index of the last glyph in that glyph row area.
4407 X is the current output position assigned to the new glyph string
4408 constructed. HL overrides that face of the glyph; e.g. it is
4409 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4410 x-position of the drawing area. */
4411
6c27ec25 4412#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4413 do { \
43d120d8
KH
4414 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4415 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4416 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4417 struct composition *cmp = composition_table[cmp_id]; \
4418 int glyph_len = cmp->glyph_len; \
4419 XChar2b *char2b; \
4420 struct face **faces; \
4421 struct glyph_string *first_s = NULL; \
4422 int n; \
4423 \
ee569018 4424 base_face = base_face->ascii_face; \
b4192550
KH
4425 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4426 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4427 /* At first, fill in `char2b' and `faces'. */ \
4428 for (n = 0; n < glyph_len; n++) \
4429 { \
43d120d8 4430 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4431 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4432 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4433 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4434 this_face_id, char2b + n, 1); \
b4192550
KH
4435 } \
4436 \
4437 /* Make glyph_strings for each glyph sequence that is drawable by \
4438 the same face, and append them to HEAD/TAIL. */ \
4439 for (n = 0; n < cmp->glyph_len;) \
4440 { \
4441 s = (struct glyph_string *) alloca (sizeof *s); \
4442 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4443 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4444 s->cmp = cmp; \
4445 s->gidx = n; \
b4192550
KH
4446 s->x = (X); \
4447 \
4448 if (n == 0) \
4449 first_s = s; \
4450 \
4451 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4452 } \
4453 \
4454 ++START; \
4455 s = first_s; \
4456 } while (0)
4457
4458
06a2c219
GM
4459/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4460 of AREA of glyph row ROW on window W between indices START and END.
4461 HL overrides the face for drawing glyph strings, e.g. it is
4462 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4463 x-positions of the drawing area.
4464
4465 This is an ugly monster macro construct because we must use alloca
4466 to allocate glyph strings (because x_draw_glyphs can be called
4467 asynchronously). */
4468
8abee2e1 4469#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4470 do \
4471 { \
4472 HEAD = TAIL = NULL; \
4473 while (START < END) \
4474 { \
4475 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4476 switch (first_glyph->type) \
4477 { \
4478 case CHAR_GLYPH: \
4479 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4480 TAIL, HL, X, LAST_X, \
4481 OVERLAPS_P); \
06a2c219
GM
4482 break; \
4483 \
b4192550
KH
4484 case COMPOSITE_GLYPH: \
4485 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4486 HEAD, TAIL, HL, X, LAST_X,\
4487 OVERLAPS_P); \
4488 break; \
4489 \
06a2c219
GM
4490 case STRETCH_GLYPH: \
4491 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4492 HEAD, TAIL, HL, X, LAST_X); \
4493 break; \
4494 \
4495 case IMAGE_GLYPH: \
4496 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4497 TAIL, HL, X, LAST_X); \
4498 break; \
4499 \
4500 default: \
4501 abort (); \
4502 } \
4503 \
4504 x_set_glyph_string_background_width (s, START, LAST_X); \
4505 (X) += s->width; \
4506 } \
4507 } \
4508 while (0)
4509
4510
4511/* Draw glyphs between START and END in AREA of ROW on window W,
4512 starting at x-position X. X is relative to AREA in W. HL is a
4513 face-override with the following meaning:
4514
4515 DRAW_NORMAL_TEXT draw normally
4516 DRAW_CURSOR draw in cursor face
4517 DRAW_MOUSE_FACE draw in mouse face.
4518 DRAW_INVERSE_VIDEO draw in mode line face
4519 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4520 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4521
4522 If REAL_START is non-null, return in *REAL_START the real starting
4523 position for display. This can be different from START in case
4524 overlapping glyphs must be displayed. If REAL_END is non-null,
4525 return in *REAL_END the real end position for display. This can be
4526 different from END in case overlapping glyphs must be displayed.
4527
66ac4b0e
GM
4528 If OVERLAPS_P is non-zero, draw only the foreground of characters
4529 and clip to the physical height of ROW.
4530
06a2c219
GM
4531 Value is the x-position reached, relative to AREA of W. */
4532
4533static int
66ac4b0e
GM
4534x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4535 overlaps_p)
06a2c219
GM
4536 struct window *w;
4537 int x;
4538 struct glyph_row *row;
4539 enum glyph_row_area area;
4540 int start, end;
4541 enum draw_glyphs_face hl;
4542 int *real_start, *real_end;
66ac4b0e 4543 int overlaps_p;
dc6f92b8 4544{
06a2c219
GM
4545 struct glyph_string *head, *tail;
4546 struct glyph_string *s;
4547 int last_x, area_width;
4548 int x_reached;
4549 int i, j;
4550
4551 /* Let's rather be paranoid than getting a SEGV. */
4552 start = max (0, start);
4553 end = min (end, row->used[area]);
4554 if (real_start)
4555 *real_start = start;
4556 if (real_end)
4557 *real_end = end;
4558
4559 /* Translate X to frame coordinates. Set last_x to the right
4560 end of the drawing area. */
4561 if (row->full_width_p)
4562 {
4563 /* X is relative to the left edge of W, without scroll bars
4564 or flag areas. */
4565 struct frame *f = XFRAME (w->frame);
110859fc 4566 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4567 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4568
06a2c219
GM
4569 x += window_left_x;
4570 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4571 last_x = window_left_x + area_width;
4572
4573 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4574 {
110859fc 4575 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4576 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4577 last_x += width;
4578 else
4579 x -= width;
4580 }
dc6f92b8 4581
b9432a85
GM
4582 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4583 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4584 }
4585 else
dc6f92b8 4586 {
06a2c219
GM
4587 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4588 area_width = window_box_width (w, area);
4589 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4590 }
4591
06a2c219
GM
4592 /* Build a doubly-linked list of glyph_string structures between
4593 head and tail from what we have to draw. Note that the macro
4594 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4595 the reason we use a separate variable `i'. */
4596 i = start;
66ac4b0e
GM
4597 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4598 overlaps_p);
06a2c219
GM
4599 if (tail)
4600 x_reached = tail->x + tail->background_width;
4601 else
4602 x_reached = x;
90e65f07 4603
06a2c219
GM
4604 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4605 the row, redraw some glyphs in front or following the glyph
4606 strings built above. */
66ac4b0e 4607 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4608 {
4609 int dummy_x = 0;
4610 struct glyph_string *h, *t;
4611
4612 /* Compute overhangs for all glyph strings. */
4613 for (s = head; s; s = s->next)
4614 x_compute_glyph_string_overhangs (s);
4615
4616 /* Prepend glyph strings for glyphs in front of the first glyph
4617 string that are overwritten because of the first glyph
4618 string's left overhang. The background of all strings
4619 prepended must be drawn because the first glyph string
4620 draws over it. */
4621 i = x_left_overwritten (head);
4622 if (i >= 0)
4623 {
4624 j = i;
4625 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4626 DRAW_NORMAL_TEXT, dummy_x, last_x,
4627 overlaps_p);
06a2c219
GM
4628 start = i;
4629 if (real_start)
4630 *real_start = start;
4631 x_compute_overhangs_and_x (t, head->x, 1);
4632 x_prepend_glyph_string_lists (&head, &tail, h, t);
4633 }
58769bee 4634
06a2c219
GM
4635 /* Prepend glyph strings for glyphs in front of the first glyph
4636 string that overwrite that glyph string because of their
4637 right overhang. For these strings, only the foreground must
4638 be drawn, because it draws over the glyph string at `head'.
4639 The background must not be drawn because this would overwrite
4640 right overhangs of preceding glyphs for which no glyph
4641 strings exist. */
4642 i = x_left_overwriting (head);
4643 if (i >= 0)
4644 {
4645 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4646 DRAW_NORMAL_TEXT, dummy_x, last_x,
4647 overlaps_p);
06a2c219
GM
4648 for (s = h; s; s = s->next)
4649 s->background_filled_p = 1;
4650 if (real_start)
4651 *real_start = i;
4652 x_compute_overhangs_and_x (t, head->x, 1);
4653 x_prepend_glyph_string_lists (&head, &tail, h, t);
4654 }
dbcb258a 4655
06a2c219
GM
4656 /* Append glyphs strings for glyphs following the last glyph
4657 string tail that are overwritten by tail. The background of
4658 these strings has to be drawn because tail's foreground draws
4659 over it. */
4660 i = x_right_overwritten (tail);
4661 if (i >= 0)
4662 {
4663 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4664 DRAW_NORMAL_TEXT, x, last_x,
4665 overlaps_p);
06a2c219
GM
4666 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4667 x_append_glyph_string_lists (&head, &tail, h, t);
4668 if (real_end)
4669 *real_end = i;
4670 }
dc6f92b8 4671
06a2c219
GM
4672 /* Append glyph strings for glyphs following the last glyph
4673 string tail that overwrite tail. The foreground of such
4674 glyphs has to be drawn because it writes into the background
4675 of tail. The background must not be drawn because it could
4676 paint over the foreground of following glyphs. */
4677 i = x_right_overwriting (tail);
4678 if (i >= 0)
4679 {
4680 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4681 DRAW_NORMAL_TEXT, x, last_x,
4682 overlaps_p);
06a2c219
GM
4683 for (s = h; s; s = s->next)
4684 s->background_filled_p = 1;
4685 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4686 x_append_glyph_string_lists (&head, &tail, h, t);
4687 if (real_end)
4688 *real_end = i;
4689 }
4690 }
58769bee 4691
06a2c219
GM
4692 /* Draw all strings. */
4693 for (s = head; s; s = s->next)
4694 x_draw_glyph_string (s);
dc6f92b8 4695
06a2c219
GM
4696 /* Value is the x-position up to which drawn, relative to AREA of W.
4697 This doesn't include parts drawn because of overhangs. */
4698 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4699 if (!row->full_width_p)
4700 {
4701 if (area > LEFT_MARGIN_AREA)
4702 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4703 if (area > TEXT_AREA)
4704 x_reached -= window_box_width (w, TEXT_AREA);
4705 }
4706 return x_reached;
4707}
dc6f92b8 4708
dc6f92b8 4709
66ac4b0e
GM
4710/* Fix the display of area AREA of overlapping row ROW in window W. */
4711
4712static void
4713x_fix_overlapping_area (w, row, area)
4714 struct window *w;
4715 struct glyph_row *row;
4716 enum glyph_row_area area;
4717{
4718 int i, x;
4719
4720 BLOCK_INPUT;
4721
4722 if (area == LEFT_MARGIN_AREA)
4723 x = 0;
4724 else if (area == TEXT_AREA)
4725 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4726 else
4727 x = (window_box_width (w, LEFT_MARGIN_AREA)
4728 + window_box_width (w, TEXT_AREA));
4729
4730 for (i = 0; i < row->used[area];)
4731 {
4732 if (row->glyphs[area][i].overlaps_vertically_p)
4733 {
4734 int start = i, start_x = x;
4735
4736 do
4737 {
4738 x += row->glyphs[area][i].pixel_width;
4739 ++i;
4740 }
4741 while (i < row->used[area]
4742 && row->glyphs[area][i].overlaps_vertically_p);
4743
4744 x_draw_glyphs (w, start_x, row, area, start, i,
4745 (row->inverse_p
4746 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4747 NULL, NULL, 1);
4748 }
4749 else
4750 {
4751 x += row->glyphs[area][i].pixel_width;
4752 ++i;
4753 }
4754 }
4755
4756 UNBLOCK_INPUT;
4757}
4758
4759
06a2c219
GM
4760/* Output LEN glyphs starting at START at the nominal cursor position.
4761 Advance the nominal cursor over the text. The global variable
4762 updated_window contains the window being updated, updated_row is
4763 the glyph row being updated, and updated_area is the area of that
4764 row being updated. */
dc6f92b8 4765
06a2c219
GM
4766static void
4767x_write_glyphs (start, len)
4768 struct glyph *start;
4769 int len;
4770{
4771 int x, hpos, real_start, real_end;
d9cdbb3d 4772
06a2c219 4773 xassert (updated_window && updated_row);
dc6f92b8 4774 BLOCK_INPUT;
06a2c219
GM
4775
4776 /* Write glyphs. */
dc6f92b8 4777
06a2c219
GM
4778 hpos = start - updated_row->glyphs[updated_area];
4779 x = x_draw_glyphs (updated_window, output_cursor.x,
4780 updated_row, updated_area,
4781 hpos, hpos + len,
4782 (updated_row->inverse_p
4783 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4784 &real_start, &real_end, 0);
b30ec466 4785
06a2c219
GM
4786 /* If we drew over the cursor, note that it is not visible any more. */
4787 note_overwritten_text_cursor (updated_window, real_start,
4788 real_end - real_start);
dc6f92b8
JB
4789
4790 UNBLOCK_INPUT;
06a2c219
GM
4791
4792 /* Advance the output cursor. */
4793 output_cursor.hpos += len;
4794 output_cursor.x = x;
dc6f92b8
JB
4795}
4796
0cdd0c9f 4797
06a2c219 4798/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4799
06a2c219
GM
4800static void
4801x_insert_glyphs (start, len)
4802 struct glyph *start;
4803 register int len;
4804{
4805 struct frame *f;
4806 struct window *w;
4807 int line_height, shift_by_width, shifted_region_width;
4808 struct glyph_row *row;
4809 struct glyph *glyph;
4810 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4811
06a2c219 4812 xassert (updated_window && updated_row);
0cdd0c9f 4813 BLOCK_INPUT;
06a2c219
GM
4814 w = updated_window;
4815 f = XFRAME (WINDOW_FRAME (w));
4816
4817 /* Get the height of the line we are in. */
4818 row = updated_row;
4819 line_height = row->height;
4820
4821 /* Get the width of the glyphs to insert. */
4822 shift_by_width = 0;
4823 for (glyph = start; glyph < start + len; ++glyph)
4824 shift_by_width += glyph->pixel_width;
4825
4826 /* Get the width of the region to shift right. */
4827 shifted_region_width = (window_box_width (w, updated_area)
4828 - output_cursor.x
4829 - shift_by_width);
4830
4831 /* Shift right. */
4832 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4833 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4834 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4835 f->output_data.x->normal_gc,
4836 frame_x, frame_y,
4837 shifted_region_width, line_height,
4838 frame_x + shift_by_width, frame_y);
4839
4840 /* Write the glyphs. */
4841 hpos = start - row->glyphs[updated_area];
4842 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4843 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4844 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4845
4846 /* Advance the output cursor. */
4847 output_cursor.hpos += len;
4848 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4849 UNBLOCK_INPUT;
4850}
0cdd0c9f 4851
0cdd0c9f 4852
06a2c219
GM
4853/* Delete N glyphs at the nominal cursor position. Not implemented
4854 for X frames. */
c83febd7
RS
4855
4856static void
06a2c219
GM
4857x_delete_glyphs (n)
4858 register int n;
c83febd7 4859{
06a2c219 4860 abort ();
c83febd7
RS
4861}
4862
0cdd0c9f 4863
06a2c219
GM
4864/* Erase the current text line from the nominal cursor position
4865 (inclusive) to pixel column TO_X (exclusive). The idea is that
4866 everything from TO_X onward is already erased.
4867
4868 TO_X is a pixel position relative to updated_area of
4869 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4870
06a2c219
GM
4871static void
4872x_clear_end_of_line (to_x)
4873 int to_x;
4874{
4875 struct frame *f;
4876 struct window *w = updated_window;
4877 int max_x, min_y, max_y;
4878 int from_x, from_y, to_y;
4879
4880 xassert (updated_window && updated_row);
4881 f = XFRAME (w->frame);
4882
4883 if (updated_row->full_width_p)
4884 {
4885 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4886 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4887 && !w->pseudo_window_p)
4888 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4889 }
06a2c219
GM
4890 else
4891 max_x = window_box_width (w, updated_area);
4892 max_y = window_text_bottom_y (w);
dc6f92b8 4893
06a2c219
GM
4894 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4895 of window. For TO_X > 0, truncate to end of drawing area. */
4896 if (to_x == 0)
4897 return;
4898 else if (to_x < 0)
4899 to_x = max_x;
4900 else
4901 to_x = min (to_x, max_x);
dbc4e1c1 4902
06a2c219
GM
4903 to_y = min (max_y, output_cursor.y + updated_row->height);
4904
4905 /* Notice if the cursor will be cleared by this operation. */
4906 if (!updated_row->full_width_p)
4907 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4908
06a2c219
GM
4909 from_x = output_cursor.x;
4910
4911 /* Translate to frame coordinates. */
4912 if (updated_row->full_width_p)
4913 {
4914 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4915 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4916 }
0cdd0c9f
RS
4917 else
4918 {
06a2c219
GM
4919 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4920 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4921 }
4922
045dee35 4923 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4924 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4925 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4926
4927 /* Prevent inadvertently clearing to end of the X window. */
4928 if (to_x > from_x && to_y > from_y)
4929 {
4930 BLOCK_INPUT;
4931 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4932 from_x, from_y, to_x - from_x, to_y - from_y,
4933 False);
4934 UNBLOCK_INPUT;
0cdd0c9f 4935 }
0cdd0c9f 4936}
dbc4e1c1 4937
0cdd0c9f 4938
06a2c219 4939/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4940 frame. Otherwise clear the selected frame. */
06a2c219
GM
4941
4942static void
4943x_clear_frame ()
0cdd0c9f 4944{
06a2c219 4945 struct frame *f;
0cdd0c9f 4946
06a2c219
GM
4947 if (updating_frame)
4948 f = updating_frame;
0cdd0c9f 4949 else
b86bd3dd 4950 f = SELECTED_FRAME ();
58769bee 4951
06a2c219
GM
4952 /* Clearing the frame will erase any cursor, so mark them all as no
4953 longer visible. */
4954 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4955 output_cursor.hpos = output_cursor.vpos = 0;
4956 output_cursor.x = -1;
4957
4958 /* We don't set the output cursor here because there will always
4959 follow an explicit cursor_to. */
4960 BLOCK_INPUT;
4961 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4962
4963 /* We have to clear the scroll bars, too. If we have changed
4964 colors or something like that, then they should be notified. */
4965 x_scroll_bar_clear (f);
0cdd0c9f 4966
06a2c219
GM
4967 XFlush (FRAME_X_DISPLAY (f));
4968 UNBLOCK_INPUT;
dc6f92b8 4969}
06a2c219
GM
4970
4971
dc6f92b8 4972\f
dbc4e1c1
JB
4973/* Invert the middle quarter of the frame for .15 sec. */
4974
06a2c219
GM
4975/* We use the select system call to do the waiting, so we have to make
4976 sure it's available. If it isn't, we just won't do visual bells. */
4977
dbc4e1c1
JB
4978#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4979
06a2c219
GM
4980
4981/* Subtract the `struct timeval' values X and Y, storing the result in
4982 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4983
4984static int
4985timeval_subtract (result, x, y)
4986 struct timeval *result, x, y;
4987{
06a2c219
GM
4988 /* Perform the carry for the later subtraction by updating y. This
4989 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4990 if (x.tv_usec < y.tv_usec)
4991 {
4992 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4993 y.tv_usec -= 1000000 * nsec;
4994 y.tv_sec += nsec;
4995 }
06a2c219 4996
dbc4e1c1
JB
4997 if (x.tv_usec - y.tv_usec > 1000000)
4998 {
4999 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5000 y.tv_usec += 1000000 * nsec;
5001 y.tv_sec -= nsec;
5002 }
5003
06a2c219
GM
5004 /* Compute the time remaining to wait. tv_usec is certainly
5005 positive. */
dbc4e1c1
JB
5006 result->tv_sec = x.tv_sec - y.tv_sec;
5007 result->tv_usec = x.tv_usec - y.tv_usec;
5008
06a2c219
GM
5009 /* Return indication of whether the result should be considered
5010 negative. */
dbc4e1c1
JB
5011 return x.tv_sec < y.tv_sec;
5012}
dc6f92b8 5013
dfcf069d 5014void
f676886a
JB
5015XTflash (f)
5016 struct frame *f;
dc6f92b8 5017{
dbc4e1c1 5018 BLOCK_INPUT;
dc6f92b8 5019
dbc4e1c1
JB
5020 {
5021 GC gc;
dc6f92b8 5022
06a2c219
GM
5023 /* Create a GC that will use the GXxor function to flip foreground
5024 pixels into background pixels. */
dbc4e1c1
JB
5025 {
5026 XGCValues values;
dc6f92b8 5027
dbc4e1c1 5028 values.function = GXxor;
7556890b
RS
5029 values.foreground = (f->output_data.x->foreground_pixel
5030 ^ f->output_data.x->background_pixel);
58769bee 5031
334208b7 5032 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5033 GCFunction | GCForeground, &values);
5034 }
dc6f92b8 5035
dbc4e1c1 5036 {
e84e14c3
RS
5037 /* Get the height not including a menu bar widget. */
5038 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5039 /* Height of each line to flash. */
5040 int flash_height = FRAME_LINE_HEIGHT (f);
5041 /* These will be the left and right margins of the rectangles. */
5042 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5043 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5044
5045 int width;
5046
5047 /* Don't flash the area between a scroll bar and the frame
5048 edge it is next to. */
5049 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5050 {
5051 case vertical_scroll_bar_left:
5052 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5053 break;
5054
5055 case vertical_scroll_bar_right:
5056 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5057 break;
06a2c219
GM
5058
5059 default:
5060 break;
e84e14c3
RS
5061 }
5062
5063 width = flash_right - flash_left;
5064
5065 /* If window is tall, flash top and bottom line. */
5066 if (height > 3 * FRAME_LINE_HEIGHT (f))
5067 {
5068 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5069 flash_left,
5070 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5071 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5072 width, flash_height);
5073 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5074 flash_left,
5075 (height - flash_height
5076 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5077 width, flash_height);
5078 }
5079 else
5080 /* If it is short, flash it all. */
5081 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5082 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5083 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5084
06a2c219 5085 x_flush (f);
dc6f92b8 5086
dbc4e1c1 5087 {
06a2c219 5088 struct timeval wakeup;
dc6f92b8 5089
66c30ea1 5090 EMACS_GET_TIME (wakeup);
dc6f92b8 5091
dbc4e1c1
JB
5092 /* Compute time to wait until, propagating carry from usecs. */
5093 wakeup.tv_usec += 150000;
5094 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5095 wakeup.tv_usec %= 1000000;
5096
5097 /* Keep waiting until past the time wakeup. */
5098 while (1)
5099 {
5100 struct timeval timeout;
5101
66c30ea1 5102 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5103
5104 /* In effect, timeout = wakeup - timeout.
5105 Break if result would be negative. */
5106 if (timeval_subtract (&timeout, wakeup, timeout))
5107 break;
5108
5109 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5110 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5111 }
5112 }
58769bee 5113
e84e14c3
RS
5114 /* If window is tall, flash top and bottom line. */
5115 if (height > 3 * FRAME_LINE_HEIGHT (f))
5116 {
5117 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5118 flash_left,
5119 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5120 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5121 width, flash_height);
5122 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5123 flash_left,
5124 (height - flash_height
5125 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5126 width, flash_height);
5127 }
5128 else
5129 /* If it is short, flash it all. */
5130 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5131 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5132 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5133
334208b7 5134 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5135 x_flush (f);
dc6f92b8 5136 }
dbc4e1c1
JB
5137 }
5138
5139 UNBLOCK_INPUT;
dc6f92b8
JB
5140}
5141
06a2c219 5142#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5143
5144
dc6f92b8
JB
5145/* Make audible bell. */
5146
dfcf069d 5147void
dc6f92b8
JB
5148XTring_bell ()
5149{
b86bd3dd
GM
5150 struct frame *f = SELECTED_FRAME ();
5151
5152 if (FRAME_X_DISPLAY (f))
5153 {
dbc4e1c1 5154#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5155 if (visible_bell)
5156 XTflash (f);
5157 else
dbc4e1c1 5158#endif
b86bd3dd
GM
5159 {
5160 BLOCK_INPUT;
5161 XBell (FRAME_X_DISPLAY (f), 0);
5162 XFlush (FRAME_X_DISPLAY (f));
5163 UNBLOCK_INPUT;
5164 }
dc6f92b8
JB
5165 }
5166}
06a2c219 5167
dc6f92b8 5168\f
06a2c219
GM
5169/* Specify how many text lines, from the top of the window,
5170 should be affected by insert-lines and delete-lines operations.
5171 This, and those operations, are used only within an update
5172 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5173
dfcf069d 5174static void
06a2c219
GM
5175XTset_terminal_window (n)
5176 register int n;
dc6f92b8 5177{
06a2c219 5178 /* This function intentionally left blank. */
dc6f92b8
JB
5179}
5180
06a2c219
GM
5181
5182\f
5183/***********************************************************************
5184 Line Dance
5185 ***********************************************************************/
5186
5187/* Perform an insert-lines or delete-lines operation, inserting N
5188 lines or deleting -N lines at vertical position VPOS. */
5189
dfcf069d 5190static void
06a2c219
GM
5191x_ins_del_lines (vpos, n)
5192 int vpos, n;
dc6f92b8
JB
5193{
5194 abort ();
5195}
06a2c219
GM
5196
5197
5198/* Scroll part of the display as described by RUN. */
dc6f92b8 5199
dfcf069d 5200static void
06a2c219
GM
5201x_scroll_run (w, run)
5202 struct window *w;
5203 struct run *run;
dc6f92b8 5204{
06a2c219
GM
5205 struct frame *f = XFRAME (w->frame);
5206 int x, y, width, height, from_y, to_y, bottom_y;
5207
5208 /* Get frame-relative bounding box of the text display area of W,
5209 without mode lines. Include in this box the flags areas to the
5210 left and right of W. */
5211 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5212 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5213 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5214
5215 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5216 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5217 bottom_y = y + height;
dc6f92b8 5218
06a2c219
GM
5219 if (to_y < from_y)
5220 {
5221 /* Scrolling up. Make sure we don't copy part of the mode
5222 line at the bottom. */
5223 if (from_y + run->height > bottom_y)
5224 height = bottom_y - from_y;
5225 else
5226 height = run->height;
5227 }
dc6f92b8 5228 else
06a2c219
GM
5229 {
5230 /* Scolling down. Make sure we don't copy over the mode line.
5231 at the bottom. */
5232 if (to_y + run->height > bottom_y)
5233 height = bottom_y - to_y;
5234 else
5235 height = run->height;
5236 }
7a13e894 5237
06a2c219
GM
5238 BLOCK_INPUT;
5239
5240 /* Cursor off. Will be switched on again in x_update_window_end. */
5241 updated_window = w;
5242 x_clear_cursor (w);
5243
5244 XCopyArea (FRAME_X_DISPLAY (f),
5245 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5246 f->output_data.x->normal_gc,
5247 x, from_y,
5248 width, height,
5249 x, to_y);
5250
5251 UNBLOCK_INPUT;
5252}
dc6f92b8 5253
dc6f92b8 5254
06a2c219
GM
5255\f
5256/***********************************************************************
5257 Exposure Events
5258 ***********************************************************************/
5259
5260/* Redisplay an exposed area of frame F. X and Y are the upper-left
5261 corner of the exposed rectangle. W and H are width and height of
5262 the exposed area. All are pixel values. W or H zero means redraw
5263 the entire frame. */
dc6f92b8 5264
06a2c219
GM
5265static void
5266expose_frame (f, x, y, w, h)
5267 struct frame *f;
5268 int x, y, w, h;
dc6f92b8 5269{
06a2c219 5270 XRectangle r;
dc6f92b8 5271
06a2c219 5272 TRACE ((stderr, "expose_frame "));
dc6f92b8 5273
06a2c219
GM
5274 /* No need to redraw if frame will be redrawn soon. */
5275 if (FRAME_GARBAGED_P (f))
dc6f92b8 5276 {
06a2c219
GM
5277 TRACE ((stderr, " garbaged\n"));
5278 return;
5279 }
5280
5281 /* If basic faces haven't been realized yet, there is no point in
5282 trying to redraw anything. This can happen when we get an expose
5283 event while Emacs is starting, e.g. by moving another window. */
5284 if (FRAME_FACE_CACHE (f) == NULL
5285 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5286 {
5287 TRACE ((stderr, " no faces\n"));
5288 return;
58769bee 5289 }
06a2c219
GM
5290
5291 if (w == 0 || h == 0)
58769bee 5292 {
06a2c219
GM
5293 r.x = r.y = 0;
5294 r.width = CANON_X_UNIT (f) * f->width;
5295 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5296 }
5297 else
5298 {
06a2c219
GM
5299 r.x = x;
5300 r.y = y;
5301 r.width = w;
5302 r.height = h;
5303 }
5304
5305 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5306 expose_window_tree (XWINDOW (f->root_window), &r);
5307
9ea173e8 5308 if (WINDOWP (f->tool_bar_window))
06a2c219 5309 {
9ea173e8 5310 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5311 XRectangle window_rect;
5312 XRectangle intersection_rect;
5313 int window_x, window_y, window_width, window_height;
5314
5315
5316 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5317 window_rect.x = window_x;
5318 window_rect.y = window_y;
5319 window_rect.width = window_width;
5320 window_rect.height = window_height;
5321
5322 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5323 expose_window (w, &intersection_rect);
5324 }
5325
5326#ifndef USE_X_TOOLKIT
5327 if (WINDOWP (f->menu_bar_window))
5328 {
5329 struct window *w = XWINDOW (f->menu_bar_window);
5330 XRectangle window_rect;
5331 XRectangle intersection_rect;
5332 int window_x, window_y, window_width, window_height;
5333
5334
5335 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5336 window_rect.x = window_x;
5337 window_rect.y = window_y;
5338 window_rect.width = window_width;
5339 window_rect.height = window_height;
5340
5341 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5342 expose_window (w, &intersection_rect);
dc6f92b8 5343 }
06a2c219 5344#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5345}
5346
06a2c219
GM
5347
5348/* Redraw (parts) of all windows in the window tree rooted at W that
5349 intersect R. R contains frame pixel coordinates. */
5350
58769bee 5351static void
06a2c219
GM
5352expose_window_tree (w, r)
5353 struct window *w;
5354 XRectangle *r;
dc6f92b8 5355{
06a2c219
GM
5356 while (w)
5357 {
5358 if (!NILP (w->hchild))
5359 expose_window_tree (XWINDOW (w->hchild), r);
5360 else if (!NILP (w->vchild))
5361 expose_window_tree (XWINDOW (w->vchild), r);
5362 else
5363 {
5364 XRectangle window_rect;
5365 XRectangle intersection_rect;
5366 struct frame *f = XFRAME (w->frame);
5367 int window_x, window_y, window_width, window_height;
5368
5369 /* Frame-relative pixel rectangle of W. */
5370 window_box (w, -1, &window_x, &window_y, &window_width,
5371 &window_height);
5372 window_rect.x
5373 = (window_x
110859fc 5374 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5375 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5376 window_rect.y = window_y;
5377 window_rect.width
5378 = (window_width
110859fc 5379 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5380 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5381 window_rect.height
5382 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5383
5384 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5385 expose_window (w, &intersection_rect);
5386 }
58769bee 5387
06a2c219
GM
5388 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5389 }
5390}
58769bee 5391
dc6f92b8 5392
06a2c219
GM
5393/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5394 which intersects rectangle R. R is in window-relative coordinates. */
5395
5396static void
5397expose_area (w, row, r, area)
5398 struct window *w;
5399 struct glyph_row *row;
5400 XRectangle *r;
5401 enum glyph_row_area area;
5402{
5403 int x;
5404 struct glyph *first = row->glyphs[area];
5405 struct glyph *end = row->glyphs[area] + row->used[area];
5406 struct glyph *last;
5407 int first_x;
5408
5409 /* Set x to the window-relative start position for drawing glyphs of
5410 AREA. The first glyph of the text area can be partially visible.
5411 The first glyphs of other areas cannot. */
5412 if (area == LEFT_MARGIN_AREA)
5413 x = 0;
5414 else if (area == TEXT_AREA)
5415 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5416 else
5417 x = (window_box_width (w, LEFT_MARGIN_AREA)
5418 + window_box_width (w, TEXT_AREA));
5419
6fb13182
GM
5420 if (area == TEXT_AREA && row->fill_line_p)
5421 /* If row extends face to end of line write the whole line. */
5422 x_draw_glyphs (w, x, row, area,
5423 0, row->used[area],
06a2c219 5424 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5425 NULL, NULL, 0);
6fb13182
GM
5426 else
5427 {
5428 /* Find the first glyph that must be redrawn. */
5429 while (first < end
5430 && x + first->pixel_width < r->x)
5431 {
5432 x += first->pixel_width;
5433 ++first;
5434 }
5435
5436 /* Find the last one. */
5437 last = first;
5438 first_x = x;
5439 while (last < end
5440 && x < r->x + r->width)
5441 {
5442 x += last->pixel_width;
5443 ++last;
5444 }
5445
5446 /* Repaint. */
5447 if (last > first)
5448 x_draw_glyphs (w, first_x, row, area,
5449 first - row->glyphs[area],
5450 last - row->glyphs[area],
5451 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5452 NULL, NULL, 0);
5453 }
06a2c219
GM
5454}
5455
58769bee 5456
06a2c219
GM
5457/* Redraw the parts of the glyph row ROW on window W intersecting
5458 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5459
06a2c219
GM
5460static void
5461expose_line (w, row, r)
5462 struct window *w;
5463 struct glyph_row *row;
5464 XRectangle *r;
5465{
5466 xassert (row->enabled_p);
5467
5468 if (row->mode_line_p || w->pseudo_window_p)
5469 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5470 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5471 NULL, NULL, 0);
06a2c219
GM
5472 else
5473 {
5474 if (row->used[LEFT_MARGIN_AREA])
5475 expose_area (w, row, r, LEFT_MARGIN_AREA);
5476 if (row->used[TEXT_AREA])
5477 expose_area (w, row, r, TEXT_AREA);
5478 if (row->used[RIGHT_MARGIN_AREA])
5479 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5480 x_draw_row_bitmaps (w, row);
5481 }
5482}
dc6f92b8 5483
58769bee 5484
06a2c219
GM
5485/* Return non-zero if W's cursor intersects rectangle R. */
5486
5487static int
5488x_phys_cursor_in_rect_p (w, r)
5489 struct window *w;
5490 XRectangle *r;
5491{
5492 XRectangle cr, result;
5493 struct glyph *cursor_glyph;
5494
5495 cursor_glyph = get_phys_cursor_glyph (w);
5496 if (cursor_glyph)
5497 {
5498 cr.x = w->phys_cursor.x;
5499 cr.y = w->phys_cursor.y;
5500 cr.width = cursor_glyph->pixel_width;
5501 cr.height = w->phys_cursor_height;
5502 return x_intersect_rectangles (&cr, r, &result);
5503 }
5504 else
5505 return 0;
dc6f92b8 5506}
dc6f92b8 5507
06a2c219
GM
5508
5509/* Redraw a rectangle of window W. R is a rectangle in window
5510 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5511
5512static void
06a2c219
GM
5513expose_window (w, r)
5514 struct window *w;
5515 XRectangle *r;
dc6f92b8 5516{
06a2c219
GM
5517 struct glyph_row *row;
5518 int y;
5519 int yb = window_text_bottom_y (w);
5520 int cursor_cleared_p;
dc6f92b8 5521
80c32bcc
GM
5522 /* If window is not yet fully initialized, do nothing. This can
5523 happen when toolkit scroll bars are used and a window is split.
5524 Reconfiguring the scroll bar will generate an expose for a newly
5525 created window. */
5526 if (w->current_matrix == NULL)
5527 return;
5528
06a2c219
GM
5529 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5530 r->x, r->y, r->width, r->height));
dc6f92b8 5531
06a2c219
GM
5532 /* Convert to window coordinates. */
5533 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5534 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5535
06a2c219
GM
5536 /* Turn off the cursor. */
5537 if (!w->pseudo_window_p
5538 && x_phys_cursor_in_rect_p (w, r))
5539 {
5540 x_clear_cursor (w);
5541 cursor_cleared_p = 1;
5542 }
5543 else
5544 cursor_cleared_p = 0;
5545
5546 /* Find the first row intersecting the rectangle R. */
5547 row = w->current_matrix->rows;
5548 y = 0;
5549 while (row->enabled_p
5550 && y < yb
5551 && y + row->height < r->y)
5552 {
5553 y += row->height;
5554 ++row;
5555 }
5556
dc6f92b8 5557 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5558 while (row->enabled_p
5559 && y < yb
5560 && y < r->y + r->height)
5561 {
5562 expose_line (w, row, r);
5563 y += row->height;
5564 ++row;
5565 }
5566
5567 /* Display the mode line if there is one. */
5568 if (WINDOW_WANTS_MODELINE_P (w)
5569 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5570 row->enabled_p)
5571 && row->y < r->y + r->height)
5572 expose_line (w, row, r);
dc6f92b8 5573
06a2c219 5574 if (!w->pseudo_window_p)
dc6f92b8 5575 {
06a2c219
GM
5576 /* Draw border between windows. */
5577 x_draw_vertical_border (w);
5578
5579 /* Turn the cursor on again. */
5580 if (cursor_cleared_p)
5581 x_update_window_cursor (w, 1);
5582 }
5583}
dc6f92b8 5584
dc6f92b8 5585
06a2c219
GM
5586/* Determine the intersection of two rectangles R1 and R2. Return
5587 the intersection in *RESULT. Value is non-zero if RESULT is not
5588 empty. */
5589
5590static int
5591x_intersect_rectangles (r1, r2, result)
5592 XRectangle *r1, *r2, *result;
5593{
5594 XRectangle *left, *right;
5595 XRectangle *upper, *lower;
5596 int intersection_p = 0;
5597
5598 /* Rearrange so that R1 is the left-most rectangle. */
5599 if (r1->x < r2->x)
5600 left = r1, right = r2;
5601 else
5602 left = r2, right = r1;
5603
5604 /* X0 of the intersection is right.x0, if this is inside R1,
5605 otherwise there is no intersection. */
5606 if (right->x <= left->x + left->width)
5607 {
5608 result->x = right->x;
5609
5610 /* The right end of the intersection is the minimum of the
5611 the right ends of left and right. */
5612 result->width = (min (left->x + left->width, right->x + right->width)
5613 - result->x);
5614
5615 /* Same game for Y. */
5616 if (r1->y < r2->y)
5617 upper = r1, lower = r2;
5618 else
5619 upper = r2, lower = r1;
5620
5621 /* The upper end of the intersection is lower.y0, if this is inside
5622 of upper. Otherwise, there is no intersection. */
5623 if (lower->y <= upper->y + upper->height)
dc43ef94 5624 {
06a2c219
GM
5625 result->y = lower->y;
5626
5627 /* The lower end of the intersection is the minimum of the lower
5628 ends of upper and lower. */
5629 result->height = (min (lower->y + lower->height,
5630 upper->y + upper->height)
5631 - result->y);
5632 intersection_p = 1;
dc43ef94 5633 }
dc6f92b8
JB
5634 }
5635
06a2c219 5636 return intersection_p;
dc6f92b8 5637}
06a2c219
GM
5638
5639
5640
5641
dc6f92b8 5642\f
dc6f92b8 5643static void
334208b7
RS
5644frame_highlight (f)
5645 struct frame *f;
dc6f92b8 5646{
b3e1e05c
JB
5647 /* We used to only do this if Vx_no_window_manager was non-nil, but
5648 the ICCCM (section 4.1.6) says that the window's border pixmap
5649 and border pixel are window attributes which are "private to the
5650 client", so we can always change it to whatever we want. */
5651 BLOCK_INPUT;
334208b7 5652 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5653 f->output_data.x->border_pixel);
b3e1e05c 5654 UNBLOCK_INPUT;
5d46f928 5655 x_update_cursor (f, 1);
dc6f92b8
JB
5656}
5657
5658static void
334208b7
RS
5659frame_unhighlight (f)
5660 struct frame *f;
dc6f92b8 5661{
b3e1e05c
JB
5662 /* We used to only do this if Vx_no_window_manager was non-nil, but
5663 the ICCCM (section 4.1.6) says that the window's border pixmap
5664 and border pixel are window attributes which are "private to the
5665 client", so we can always change it to whatever we want. */
5666 BLOCK_INPUT;
334208b7 5667 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5668 f->output_data.x->border_tile);
b3e1e05c 5669 UNBLOCK_INPUT;
5d46f928 5670 x_update_cursor (f, 1);
dc6f92b8 5671}
dc6f92b8 5672
f676886a
JB
5673/* The focus has changed. Update the frames as necessary to reflect
5674 the new situation. Note that we can't change the selected frame
c5acd733 5675 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5676 Each event gets marked with the frame in which it occurred, so the
c5acd733 5677 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5678
6d4238f3 5679static void
0f941935
KH
5680x_new_focus_frame (dpyinfo, frame)
5681 struct x_display_info *dpyinfo;
f676886a 5682 struct frame *frame;
dc6f92b8 5683{
0f941935 5684 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5685
0f941935 5686 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5687 {
58769bee 5688 /* Set this before calling other routines, so that they see
f676886a 5689 the correct value of x_focus_frame. */
0f941935 5690 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5691
5692 if (old_focus && old_focus->auto_lower)
f676886a 5693 x_lower_frame (old_focus);
dc6f92b8
JB
5694
5695#if 0
f676886a 5696 selected_frame = frame;
e0c1aef2
KH
5697 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5698 selected_frame);
f676886a
JB
5699 Fselect_window (selected_frame->selected_window);
5700 choose_minibuf_frame ();
c118dd06 5701#endif /* ! 0 */
dc6f92b8 5702
0f941935
KH
5703 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5704 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5705 else
5706 pending_autoraise_frame = 0;
6d4238f3 5707 }
dc6f92b8 5708
0f941935 5709 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5710}
5711
37c2c98b
RS
5712/* Handle an event saying the mouse has moved out of an Emacs frame. */
5713
5714void
0f941935
KH
5715x_mouse_leave (dpyinfo)
5716 struct x_display_info *dpyinfo;
37c2c98b 5717{
0f941935 5718 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5719}
6d4238f3 5720
f451eb13
JB
5721/* The focus has changed, or we have redirected a frame's focus to
5722 another frame (this happens when a frame uses a surrogate
06a2c219 5723 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5724
5725 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5726 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5727 the appropriate X display info. */
06a2c219 5728
6d4238f3 5729static void
0f941935
KH
5730XTframe_rehighlight (frame)
5731 struct frame *frame;
6d4238f3 5732{
0f941935
KH
5733 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5734}
6d4238f3 5735
0f941935
KH
5736static void
5737x_frame_rehighlight (dpyinfo)
5738 struct x_display_info *dpyinfo;
5739{
5740 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5741
5742 if (dpyinfo->x_focus_frame)
6d4238f3 5743 {
0f941935
KH
5744 dpyinfo->x_highlight_frame
5745 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5746 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5747 : dpyinfo->x_focus_frame);
5748 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5749 {
0f941935
KH
5750 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5751 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5752 }
dc6f92b8 5753 }
6d4238f3 5754 else
0f941935 5755 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5756
0f941935 5757 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5758 {
5759 if (old_highlight)
f676886a 5760 frame_unhighlight (old_highlight);
0f941935
KH
5761 if (dpyinfo->x_highlight_frame)
5762 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5763 }
dc6f92b8 5764}
06a2c219
GM
5765
5766
dc6f92b8 5767\f
06a2c219 5768/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5769
28430d3c
JB
5770/* Initialize mode_switch_bit and modifier_meaning. */
5771static void
334208b7
RS
5772x_find_modifier_meanings (dpyinfo)
5773 struct x_display_info *dpyinfo;
28430d3c 5774{
f689eb05 5775 int min_code, max_code;
28430d3c
JB
5776 KeySym *syms;
5777 int syms_per_code;
5778 XModifierKeymap *mods;
5779
334208b7
RS
5780 dpyinfo->meta_mod_mask = 0;
5781 dpyinfo->shift_lock_mask = 0;
5782 dpyinfo->alt_mod_mask = 0;
5783 dpyinfo->super_mod_mask = 0;
5784 dpyinfo->hyper_mod_mask = 0;
58769bee 5785
9658a521 5786#ifdef HAVE_X11R4
334208b7 5787 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5788#else
4a60f8c5
RS
5789 min_code = dpyinfo->display->min_keycode;
5790 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5791#endif
5792
334208b7 5793 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5794 min_code, max_code - min_code + 1,
5795 &syms_per_code);
334208b7 5796 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5797
58769bee 5798 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5799 Alt keysyms are on. */
28430d3c 5800 {
06a2c219 5801 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5802
5803 for (row = 3; row < 8; row++)
5804 for (col = 0; col < mods->max_keypermod; col++)
5805 {
0299d313
RS
5806 KeyCode code
5807 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5808
af92970c
KH
5809 /* Zeroes are used for filler. Skip them. */
5810 if (code == 0)
5811 continue;
5812
28430d3c
JB
5813 /* Are any of this keycode's keysyms a meta key? */
5814 {
5815 int code_col;
5816
5817 for (code_col = 0; code_col < syms_per_code; code_col++)
5818 {
f689eb05 5819 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5820
f689eb05 5821 switch (sym)
28430d3c 5822 {
f689eb05
JB
5823 case XK_Meta_L:
5824 case XK_Meta_R:
334208b7 5825 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5826 break;
f689eb05
JB
5827
5828 case XK_Alt_L:
5829 case XK_Alt_R:
334208b7 5830 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5831 break;
5832
5833 case XK_Hyper_L:
5834 case XK_Hyper_R:
334208b7 5835 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5836 break;
5837
5838 case XK_Super_L:
5839 case XK_Super_R:
334208b7 5840 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5841 break;
11edeb03
JB
5842
5843 case XK_Shift_Lock:
5844 /* Ignore this if it's not on the lock modifier. */
5845 if ((1 << row) == LockMask)
334208b7 5846 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5847 break;
28430d3c
JB
5848 }
5849 }
5850 }
5851 }
5852 }
5853
f689eb05 5854 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5855 if (! dpyinfo->meta_mod_mask)
a3c44b14 5856 {
334208b7
RS
5857 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5858 dpyinfo->alt_mod_mask = 0;
a3c44b14 5859 }
f689eb05 5860
148c4b70
RS
5861 /* If some keys are both alt and meta,
5862 make them just meta, not alt. */
334208b7 5863 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5864 {
334208b7 5865 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5866 }
58769bee 5867
28430d3c 5868 XFree ((char *) syms);
f689eb05 5869 XFreeModifiermap (mods);
28430d3c
JB
5870}
5871
dfeccd2d
JB
5872/* Convert between the modifier bits X uses and the modifier bits
5873 Emacs uses. */
06a2c219 5874
7c5283e4 5875static unsigned int
334208b7
RS
5876x_x_to_emacs_modifiers (dpyinfo, state)
5877 struct x_display_info *dpyinfo;
dc6f92b8
JB
5878 unsigned int state;
5879{
334208b7
RS
5880 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5881 | ((state & ControlMask) ? ctrl_modifier : 0)
5882 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5883 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5884 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5885 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5886}
5887
dfeccd2d 5888static unsigned int
334208b7
RS
5889x_emacs_to_x_modifiers (dpyinfo, state)
5890 struct x_display_info *dpyinfo;
dfeccd2d
JB
5891 unsigned int state;
5892{
334208b7
RS
5893 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5894 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5895 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5896 | ((state & shift_modifier) ? ShiftMask : 0)
5897 | ((state & ctrl_modifier) ? ControlMask : 0)
5898 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5899}
d047c4eb
KH
5900
5901/* Convert a keysym to its name. */
5902
5903char *
5904x_get_keysym_name (keysym)
5905 KeySym keysym;
5906{
5907 char *value;
5908
5909 BLOCK_INPUT;
5910 value = XKeysymToString (keysym);
5911 UNBLOCK_INPUT;
5912
5913 return value;
5914}
06a2c219
GM
5915
5916
e4571a43
JB
5917\f
5918/* Mouse clicks and mouse movement. Rah. */
e4571a43 5919
06a2c219
GM
5920/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5921 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5922 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5923 not force the value into range. */
69388238 5924
c8dba240 5925void
69388238 5926pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5927 FRAME_PTR f;
69388238 5928 register int pix_x, pix_y;
e4571a43
JB
5929 register int *x, *y;
5930 XRectangle *bounds;
69388238 5931 int noclip;
e4571a43 5932{
06a2c219 5933 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5934 even for negative values. */
5935 if (pix_x < 0)
7556890b 5936 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5937 if (pix_y < 0)
7556890b 5938 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5939
e4571a43
JB
5940 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5941 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5942
5943 if (bounds)
5944 {
7556890b
RS
5945 bounds->width = FONT_WIDTH (f->output_data.x->font);
5946 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5947 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5948 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5949 }
5950
69388238
RS
5951 if (!noclip)
5952 {
5953 if (pix_x < 0)
5954 pix_x = 0;
3cbd2e0b
RS
5955 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5956 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5957
5958 if (pix_y < 0)
5959 pix_y = 0;
5960 else if (pix_y > f->height)
5961 pix_y = f->height;
5962 }
e4571a43
JB
5963
5964 *x = pix_x;
5965 *y = pix_y;
5966}
5967
06a2c219
GM
5968
5969/* Given HPOS/VPOS in the current matrix of W, return corresponding
5970 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5971 can't tell the positions because W's display is not up to date,
5972 return 0. */
5973
5974int
5975glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5976 struct window *w;
5977 int hpos, vpos;
5978 int *frame_x, *frame_y;
2b5c9e71 5979{
06a2c219
GM
5980 int success_p;
5981
5982 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5983 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5984
5985 if (display_completed)
5986 {
5987 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5988 struct glyph *glyph = row->glyphs[TEXT_AREA];
5989 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5990
5991 *frame_y = row->y;
5992 *frame_x = row->x;
5993 while (glyph < end)
5994 {
5995 *frame_x += glyph->pixel_width;
5996 ++glyph;
5997 }
5998
5999 success_p = 1;
6000 }
6001 else
6002 {
6003 *frame_y = *frame_x = 0;
6004 success_p = 0;
6005 }
6006
6007 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6008 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6009 return success_p;
2b5c9e71
RS
6010}
6011
06a2c219 6012
dc6f92b8
JB
6013/* Prepare a mouse-event in *RESULT for placement in the input queue.
6014
6015 If the event is a button press, then note that we have grabbed
f451eb13 6016 the mouse. */
dc6f92b8
JB
6017
6018static Lisp_Object
f451eb13 6019construct_mouse_click (result, event, f)
dc6f92b8
JB
6020 struct input_event *result;
6021 XButtonEvent *event;
f676886a 6022 struct frame *f;
dc6f92b8 6023{
f451eb13 6024 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6025 otherwise. */
f451eb13 6026 result->kind = mouse_click;
69388238 6027 result->code = event->button - Button1;
1113d9db 6028 result->timestamp = event->time;
334208b7
RS
6029 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6030 event->state)
f689eb05 6031 | (event->type == ButtonRelease
58769bee 6032 ? up_modifier
f689eb05 6033 : down_modifier));
dc6f92b8 6034
06a2c219
GM
6035 XSETINT (result->x, event->x);
6036 XSETINT (result->y, event->y);
6037 XSETFRAME (result->frame_or_window, f);
6038 return Qnil;
dc6f92b8 6039}
b849c413 6040
06a2c219
GM
6041#if 0 /* This function isn't called. --gerd */
6042
b849c413
RS
6043/* Prepare a menu-event in *RESULT for placement in the input queue. */
6044
6045static Lisp_Object
6046construct_menu_click (result, event, f)
6047 struct input_event *result;
6048 XButtonEvent *event;
6049 struct frame *f;
6050{
6051 /* Make the event type no_event; we'll change that when we decide
6052 otherwise. */
6053 result->kind = mouse_click;
26459b28 6054 result->code = event->button - Button1;
b849c413 6055 result->timestamp = event->time;
334208b7
RS
6056 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6057 event->state)
b849c413 6058 | (event->type == ButtonRelease
58769bee 6059 ? up_modifier
b849c413
RS
6060 : down_modifier));
6061
e0c1aef2
KH
6062 XSETINT (result->x, event->x);
6063 XSETINT (result->y, -1);
6064 XSETFRAME (result->frame_or_window, f);
b849c413 6065}
06a2c219
GM
6066
6067#endif /* 0 */
6068
69388238 6069\f
90e65f07
JB
6070/* Function to report a mouse movement to the mainstream Emacs code.
6071 The input handler calls this.
6072
6073 We have received a mouse movement event, which is given in *event.
6074 If the mouse is over a different glyph than it was last time, tell
6075 the mainstream emacs code by setting mouse_moved. If not, ask for
6076 another motion event, so we can check again the next time it moves. */
b8009dd1 6077
06a2c219
GM
6078static XMotionEvent last_mouse_motion_event;
6079static Lisp_Object last_mouse_motion_frame;
6080
90e65f07 6081static void
12ba150f 6082note_mouse_movement (frame, event)
f676886a 6083 FRAME_PTR frame;
90e65f07 6084 XMotionEvent *event;
90e65f07 6085{
e5d77022 6086 last_mouse_movement_time = event->time;
06a2c219
GM
6087 last_mouse_motion_event = *event;
6088 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6089
27f338af
RS
6090 if (event->window != FRAME_X_WINDOW (frame))
6091 {
39d8bb4d 6092 frame->mouse_moved = 1;
27f338af 6093 last_mouse_scroll_bar = Qnil;
27f338af 6094 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6095 }
6096
90e65f07 6097 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6098 else if (event->x < last_mouse_glyph.x
6099 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6100 || event->y < last_mouse_glyph.y
6101 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6102 {
39d8bb4d 6103 frame->mouse_moved = 1;
ab648270 6104 last_mouse_scroll_bar = Qnil;
b8009dd1 6105 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6106 }
6107}
6108
bf1c0ba1 6109/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6110
06a2c219
GM
6111 int disable_mouse_highlight;
6112
6113
6114\f
6115/************************************************************************
6116 Mouse Face
6117 ************************************************************************/
6118
6119/* Find the glyph under window-relative coordinates X/Y in window W.
6120 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6121 strings. Return in *HPOS and *VPOS the row and column number of
6122 the glyph found. Return in *AREA the glyph area containing X.
6123 Value is a pointer to the glyph found or null if X/Y is not on
6124 text, or we can't tell because W's current matrix is not up to
6125 date. */
6126
6127static struct glyph *
6128x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6129 struct window *w;
6130 int x, y;
6131 int *hpos, *vpos, *area;
6132{
6133 struct glyph *glyph, *end;
6134 struct glyph_row *row;
6135 int x0, i, left_area_width;
6136
6137 /* Find row containing Y. Give up if some row is not enabled. */
6138 for (i = 0; i < w->current_matrix->nrows; ++i)
6139 {
6140 row = MATRIX_ROW (w->current_matrix, i);
6141 if (!row->enabled_p)
6142 return NULL;
6143 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6144 break;
6145 }
6146
6147 *vpos = i;
6148 *hpos = 0;
6149
6150 /* Give up if Y is not in the window. */
6151 if (i == w->current_matrix->nrows)
6152 return NULL;
6153
6154 /* Get the glyph area containing X. */
6155 if (w->pseudo_window_p)
6156 {
6157 *area = TEXT_AREA;
6158 x0 = 0;
6159 }
6160 else
6161 {
6162 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6163 if (x < left_area_width)
6164 {
6165 *area = LEFT_MARGIN_AREA;
6166 x0 = 0;
6167 }
6168 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6169 {
6170 *area = TEXT_AREA;
6171 x0 = row->x + left_area_width;
6172 }
6173 else
6174 {
6175 *area = RIGHT_MARGIN_AREA;
6176 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6177 }
6178 }
6179
6180 /* Find glyph containing X. */
6181 glyph = row->glyphs[*area];
6182 end = glyph + row->used[*area];
6183 while (glyph < end)
6184 {
6185 if (x < x0 + glyph->pixel_width)
6186 {
6187 if (w->pseudo_window_p)
6188 break;
6189 else if (BUFFERP (glyph->object))
6190 break;
6191 }
6192
6193 x0 += glyph->pixel_width;
6194 ++glyph;
6195 }
6196
6197 if (glyph == end)
6198 return NULL;
6199
6200 *hpos = glyph - row->glyphs[*area];
6201 return glyph;
6202}
6203
6204
6205/* Convert frame-relative x/y to coordinates relative to window W.
6206 Takes pseudo-windows into account. */
6207
6208static void
6209frame_to_window_pixel_xy (w, x, y)
6210 struct window *w;
6211 int *x, *y;
6212{
6213 if (w->pseudo_window_p)
6214 {
6215 /* A pseudo-window is always full-width, and starts at the
6216 left edge of the frame, plus a frame border. */
6217 struct frame *f = XFRAME (w->frame);
6218 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6219 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6220 }
6221 else
6222 {
6223 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6224 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6225 }
6226}
6227
6228
6229/* Take proper action when mouse has moved to the mode or top line of
6230 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6231 mode line. X is relative to the start of the text display area of
6232 W, so the width of bitmap areas and scroll bars must be subtracted
6233 to get a position relative to the start of the mode line. */
6234
6235static void
6236note_mode_line_highlight (w, x, mode_line_p)
6237 struct window *w;
6238 int x, mode_line_p;
6239{
6240 struct frame *f = XFRAME (w->frame);
6241 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6242 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6243 struct glyph_row *row;
6244
6245 if (mode_line_p)
6246 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6247 else
045dee35 6248 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6249
6250 if (row->enabled_p)
6251 {
6252 struct glyph *glyph, *end;
6253 Lisp_Object help, map;
6254 int x0;
6255
6256 /* Find the glyph under X. */
6257 glyph = row->glyphs[TEXT_AREA];
6258 end = glyph + row->used[TEXT_AREA];
6259 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6260 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6261 while (glyph < end
6262 && x >= x0 + glyph->pixel_width)
6263 {
6264 x0 += glyph->pixel_width;
6265 ++glyph;
6266 }
6267
6268 if (glyph < end
6269 && STRINGP (glyph->object)
6270 && XSTRING (glyph->object)->intervals
6271 && glyph->charpos >= 0
6272 && glyph->charpos < XSTRING (glyph->object)->size)
6273 {
6274 /* If we're on a string with `help-echo' text property,
6275 arrange for the help to be displayed. This is done by
6276 setting the global variable help_echo to the help string. */
6277 help = Fget_text_property (make_number (glyph->charpos),
6278 Qhelp_echo, glyph->object);
6279 if (STRINGP (help))
6280 help_echo = help;
6281
6282 /* Change the mouse pointer according to what is under X/Y. */
6283 map = Fget_text_property (make_number (glyph->charpos),
6284 Qlocal_map, glyph->object);
6285 if (!NILP (Fkeymapp (map)))
6286 cursor = f->output_data.x->nontext_cursor;
6287 }
6288 }
6289
6290 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6291}
6292
6293
6294/* Take proper action when the mouse has moved to position X, Y on
6295 frame F as regards highlighting characters that have mouse-face
6296 properties. Also de-highlighting chars where the mouse was before.
27f338af 6297 X and Y can be negative or out of range. */
b8009dd1
RS
6298
6299static void
6300note_mouse_highlight (f, x, y)
06a2c219 6301 struct frame *f;
c32cdd9a 6302 int x, y;
b8009dd1 6303{
06a2c219
GM
6304 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6305 int portion;
b8009dd1
RS
6306 Lisp_Object window;
6307 struct window *w;
6308
06a2c219
GM
6309 /* When a menu is active, don't highlight because this looks odd. */
6310#ifdef USE_X_TOOLKIT
6311 if (popup_activated ())
6312 return;
6313#endif
6314
04fff9c0
GM
6315 if (disable_mouse_highlight
6316 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6317 return;
6318
06a2c219
GM
6319 dpyinfo->mouse_face_mouse_x = x;
6320 dpyinfo->mouse_face_mouse_y = y;
6321 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6322
06a2c219 6323 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6324 return;
6325
514e4681
RS
6326 if (gc_in_progress)
6327 {
06a2c219 6328 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6329 return;
6330 }
6331
b8009dd1 6332 /* Which window is that in? */
06a2c219 6333 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6334
6335 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6336 if (! EQ (window, dpyinfo->mouse_face_window))
6337 clear_mouse_face (dpyinfo);
6338
6339 /* Not on a window -> return. */
6340 if (!WINDOWP (window))
6341 return;
6342
6343 /* Convert to window-relative pixel coordinates. */
6344 w = XWINDOW (window);
6345 frame_to_window_pixel_xy (w, &x, &y);
6346
9ea173e8 6347 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6348 buffer. */
9ea173e8 6349 if (EQ (window, f->tool_bar_window))
06a2c219 6350 {
9ea173e8 6351 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6352 return;
6353 }
6354
6355 if (portion == 1 || portion == 3)
6356 {
6357 /* Mouse is on the mode or top line. */
6358 note_mode_line_highlight (w, x, portion == 1);
6359 return;
6360 }
6361 else
6362 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6363 f->output_data.x->text_cursor);
b8009dd1 6364
0cdd0c9f
RS
6365 /* Are we in a window whose display is up to date?
6366 And verify the buffer's text has not changed. */
06a2c219
GM
6367 if (/* Within text portion of the window. */
6368 portion == 0
0cdd0c9f 6369 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6370 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6371 && (XFASTINT (w->last_overlay_modified)
6372 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6373 {
06a2c219
GM
6374 int hpos, vpos, pos, i, area;
6375 struct glyph *glyph;
b8009dd1 6376
06a2c219
GM
6377 /* Find the glyph under X/Y. */
6378 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6379
6380 /* Clear mouse face if X/Y not over text. */
6381 if (glyph == NULL
6382 || area != TEXT_AREA
6383 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6384 {
06a2c219
GM
6385 clear_mouse_face (dpyinfo);
6386 return;
6387 }
6388
6389 pos = glyph->charpos;
6390 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6391
6392 /* Check for mouse-face and help-echo. */
6393 {
6394 Lisp_Object mouse_face, overlay, position;
6395 Lisp_Object *overlay_vec;
6396 int len, noverlays;
6397 struct buffer *obuf;
6398 int obegv, ozv;
6399
6400 /* If we get an out-of-range value, return now; avoid an error. */
6401 if (pos > BUF_Z (XBUFFER (w->buffer)))
6402 return;
6403
6404 /* Make the window's buffer temporarily current for
6405 overlays_at and compute_char_face. */
6406 obuf = current_buffer;
6407 current_buffer = XBUFFER (w->buffer);
6408 obegv = BEGV;
6409 ozv = ZV;
6410 BEGV = BEG;
6411 ZV = Z;
6412
6413 /* Is this char mouse-active or does it have help-echo? */
6414 XSETINT (position, pos);
6415
6416 /* Put all the overlays we want in a vector in overlay_vec.
6417 Store the length in len. If there are more than 10, make
6418 enough space for all, and try again. */
6419 len = 10;
6420 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6421 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6422 if (noverlays > len)
6423 {
6424 len = noverlays;
6425 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6426 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6427 }
6428
6429 noverlays = sort_overlays (overlay_vec, noverlays, w);
6430
6431 /* Check mouse-face highlighting. */
6432 if (! (EQ (window, dpyinfo->mouse_face_window)
6433 && vpos >= dpyinfo->mouse_face_beg_row
6434 && vpos <= dpyinfo->mouse_face_end_row
6435 && (vpos > dpyinfo->mouse_face_beg_row
6436 || hpos >= dpyinfo->mouse_face_beg_col)
6437 && (vpos < dpyinfo->mouse_face_end_row
6438 || hpos < dpyinfo->mouse_face_end_col
6439 || dpyinfo->mouse_face_past_end)))
6440 {
6441 /* Clear the display of the old active region, if any. */
6442 clear_mouse_face (dpyinfo);
6443
6444 /* Find the highest priority overlay that has a mouse-face prop. */
6445 overlay = Qnil;
6446 for (i = 0; i < noverlays; i++)
6447 {
6448 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6449 if (!NILP (mouse_face))
6450 {
6451 overlay = overlay_vec[i];
6452 break;
6453 }
6454 }
6455
6456 /* If no overlay applies, get a text property. */
6457 if (NILP (overlay))
6458 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6459
6460 /* Handle the overlay case. */
6461 if (! NILP (overlay))
6462 {
6463 /* Find the range of text around this char that
6464 should be active. */
6465 Lisp_Object before, after;
6466 int ignore;
6467
6468 before = Foverlay_start (overlay);
6469 after = Foverlay_end (overlay);
6470 /* Record this as the current active region. */
6471 fast_find_position (w, XFASTINT (before),
6472 &dpyinfo->mouse_face_beg_col,
6473 &dpyinfo->mouse_face_beg_row,
6474 &dpyinfo->mouse_face_beg_x,
6475 &dpyinfo->mouse_face_beg_y);
6476 dpyinfo->mouse_face_past_end
6477 = !fast_find_position (w, XFASTINT (after),
6478 &dpyinfo->mouse_face_end_col,
6479 &dpyinfo->mouse_face_end_row,
6480 &dpyinfo->mouse_face_end_x,
6481 &dpyinfo->mouse_face_end_y);
6482 dpyinfo->mouse_face_window = window;
6483 dpyinfo->mouse_face_face_id
6484 = face_at_buffer_position (w, pos, 0, 0,
6485 &ignore, pos + 1, 1);
6486
6487 /* Display it as active. */
6488 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6489 }
6490 /* Handle the text property case. */
6491 else if (! NILP (mouse_face))
6492 {
6493 /* Find the range of text around this char that
6494 should be active. */
6495 Lisp_Object before, after, beginning, end;
6496 int ignore;
6497
6498 beginning = Fmarker_position (w->start);
6499 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6500 - XFASTINT (w->window_end_pos)));
6501 before
6502 = Fprevious_single_property_change (make_number (pos + 1),
6503 Qmouse_face,
6504 w->buffer, beginning);
6505 after
6506 = Fnext_single_property_change (position, Qmouse_face,
6507 w->buffer, end);
6508 /* Record this as the current active region. */
6509 fast_find_position (w, XFASTINT (before),
6510 &dpyinfo->mouse_face_beg_col,
6511 &dpyinfo->mouse_face_beg_row,
6512 &dpyinfo->mouse_face_beg_x,
6513 &dpyinfo->mouse_face_beg_y);
6514 dpyinfo->mouse_face_past_end
6515 = !fast_find_position (w, XFASTINT (after),
6516 &dpyinfo->mouse_face_end_col,
6517 &dpyinfo->mouse_face_end_row,
6518 &dpyinfo->mouse_face_end_x,
6519 &dpyinfo->mouse_face_end_y);
6520 dpyinfo->mouse_face_window = window;
6521 dpyinfo->mouse_face_face_id
6522 = face_at_buffer_position (w, pos, 0, 0,
6523 &ignore, pos + 1, 1);
6524
6525 /* Display it as active. */
6526 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6527 }
6528 }
6529
6530 /* Look for a `help-echo' property. */
6531 {
6532 Lisp_Object help;
6533
6534 /* Check overlays first. */
6535 help = Qnil;
6536 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6537 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6538
6539 /* Try text properties. */
6540 if (!STRINGP (help)
6541 && ((STRINGP (glyph->object)
6542 && glyph->charpos >= 0
6543 && glyph->charpos < XSTRING (glyph->object)->size)
6544 || (BUFFERP (glyph->object)
6545 && glyph->charpos >= BEGV
6546 && glyph->charpos < ZV)))
6547 help = Fget_text_property (make_number (glyph->charpos),
6548 Qhelp_echo, glyph->object);
6549
6550 if (STRINGP (help))
6551 help_echo = help;
6552 }
6553
6554 BEGV = obegv;
6555 ZV = ozv;
6556 current_buffer = obuf;
6557 }
6558 }
6559}
6560
6561static void
6562redo_mouse_highlight ()
6563{
6564 if (!NILP (last_mouse_motion_frame)
6565 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6566 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6567 last_mouse_motion_event.x,
6568 last_mouse_motion_event.y);
6569}
6570
6571
6572\f
6573/***********************************************************************
9ea173e8 6574 Tool-bars
06a2c219
GM
6575 ***********************************************************************/
6576
9ea173e8
GM
6577static int x_tool_bar_item P_ ((struct frame *, int, int,
6578 struct glyph **, int *, int *, int *));
06a2c219 6579
9ea173e8 6580/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6581 or -1. */
6582
9ea173e8 6583static int last_tool_bar_item;
06a2c219
GM
6584
6585
9ea173e8
GM
6586/* Get information about the tool-bar item at position X/Y on frame F.
6587 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6588 the current matrix of the tool-bar window of F, or NULL if not
6589 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6590 item in F->current_tool_bar_items. Value is
06a2c219 6591
9ea173e8 6592 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6593 0 if X/Y is on the same item that was highlighted before.
6594 1 otherwise. */
6595
6596static int
9ea173e8 6597x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6598 struct frame *f;
6599 int x, y;
6600 struct glyph **glyph;
6601 int *hpos, *vpos, *prop_idx;
6602{
6603 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6604 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6605 int area;
6606
6607 /* Find the glyph under X/Y. */
6608 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6609 if (*glyph == NULL)
6610 return -1;
6611
9ea173e8
GM
6612 /* Get the start of this tool-bar item's properties in
6613 f->current_tool_bar_items. */
6614 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6615 return -1;
6616
6617 /* Is mouse on the highlighted item? */
9ea173e8 6618 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6619 && *vpos >= dpyinfo->mouse_face_beg_row
6620 && *vpos <= dpyinfo->mouse_face_end_row
6621 && (*vpos > dpyinfo->mouse_face_beg_row
6622 || *hpos >= dpyinfo->mouse_face_beg_col)
6623 && (*vpos < dpyinfo->mouse_face_end_row
6624 || *hpos < dpyinfo->mouse_face_end_col
6625 || dpyinfo->mouse_face_past_end))
6626 return 0;
6627
6628 return 1;
6629}
6630
6631
9ea173e8 6632/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6633 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6634 or ButtonRelase. */
6635
6636static void
9ea173e8 6637x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6638 struct frame *f;
6639 XButtonEvent *button_event;
6640{
6641 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6642 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6643 int hpos, vpos, prop_idx;
6644 struct glyph *glyph;
6645 Lisp_Object enabled_p;
6646 int x = button_event->x;
6647 int y = button_event->y;
6648
9ea173e8 6649 /* If not on the highlighted tool-bar item, return. */
06a2c219 6650 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6651 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6652 return;
6653
6654 /* If item is disabled, do nothing. */
9ea173e8
GM
6655 enabled_p = (XVECTOR (f->current_tool_bar_items)
6656 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6657 if (NILP (enabled_p))
6658 return;
6659
6660 if (button_event->type == ButtonPress)
6661 {
6662 /* Show item in pressed state. */
6663 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6664 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6665 last_tool_bar_item = prop_idx;
06a2c219
GM
6666 }
6667 else
6668 {
6669 Lisp_Object key, frame;
6670 struct input_event event;
6671
6672 /* Show item in released state. */
6673 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6674 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6675
9ea173e8
GM
6676 key = (XVECTOR (f->current_tool_bar_items)
6677 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6678
6679 XSETFRAME (frame, f);
9ea173e8
GM
6680 event.kind = TOOL_BAR_EVENT;
6681 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6682 kbd_buffer_store_event (&event);
6683
9ea173e8 6684 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6685 event.frame_or_window = Fcons (frame, key);
6686 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6687 button_event->state);
6688 kbd_buffer_store_event (&event);
9ea173e8 6689 last_tool_bar_item = -1;
06a2c219
GM
6690 }
6691}
6692
6693
9ea173e8
GM
6694/* Possibly highlight a tool-bar item on frame F when mouse moves to
6695 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6696 note_mouse_highlight. */
6697
6698static void
9ea173e8 6699note_tool_bar_highlight (f, x, y)
06a2c219
GM
6700 struct frame *f;
6701 int x, y;
6702{
9ea173e8 6703 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6704 struct window *w = XWINDOW (window);
6705 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6706 int hpos, vpos;
6707 struct glyph *glyph;
6708 struct glyph_row *row;
5c187dee 6709 int i;
06a2c219
GM
6710 Lisp_Object enabled_p;
6711 int prop_idx;
6712 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6713 int mouse_down_p, rc;
06a2c219
GM
6714
6715 /* Function note_mouse_highlight is called with negative x(y
6716 values when mouse moves outside of the frame. */
6717 if (x <= 0 || y <= 0)
6718 {
6719 clear_mouse_face (dpyinfo);
6720 return;
6721 }
6722
9ea173e8 6723 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6724 if (rc < 0)
6725 {
9ea173e8 6726 /* Not on tool-bar item. */
06a2c219
GM
6727 clear_mouse_face (dpyinfo);
6728 return;
6729 }
6730 else if (rc == 0)
9ea173e8 6731 /* On same tool-bar item as before. */
06a2c219 6732 goto set_help_echo;
b8009dd1 6733
06a2c219
GM
6734 clear_mouse_face (dpyinfo);
6735
9ea173e8 6736 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6737 mouse_down_p = (dpyinfo->grabbed
6738 && f == last_mouse_frame
6739 && FRAME_LIVE_P (f));
6740 if (mouse_down_p
9ea173e8 6741 && last_tool_bar_item != prop_idx)
06a2c219
GM
6742 return;
6743
6744 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6745 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6746
9ea173e8
GM
6747 /* If tool-bar item is not enabled, don't highlight it. */
6748 enabled_p = (XVECTOR (f->current_tool_bar_items)
6749 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6750 if (!NILP (enabled_p))
6751 {
6752 /* Compute the x-position of the glyph. In front and past the
6753 image is a space. We include this is the highlighted area. */
6754 row = MATRIX_ROW (w->current_matrix, vpos);
6755 for (i = x = 0; i < hpos; ++i)
6756 x += row->glyphs[TEXT_AREA][i].pixel_width;
6757
6758 /* Record this as the current active region. */
6759 dpyinfo->mouse_face_beg_col = hpos;
6760 dpyinfo->mouse_face_beg_row = vpos;
6761 dpyinfo->mouse_face_beg_x = x;
6762 dpyinfo->mouse_face_beg_y = row->y;
6763 dpyinfo->mouse_face_past_end = 0;
6764
6765 dpyinfo->mouse_face_end_col = hpos + 1;
6766 dpyinfo->mouse_face_end_row = vpos;
6767 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6768 dpyinfo->mouse_face_end_y = row->y;
6769 dpyinfo->mouse_face_window = window;
9ea173e8 6770 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6771
6772 /* Display it as active. */
6773 show_mouse_face (dpyinfo, draw);
6774 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6775 }
06a2c219
GM
6776
6777 set_help_echo:
6778
9ea173e8 6779 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6780 XTread_socket does the rest. */
9ea173e8
GM
6781 help_echo = (XVECTOR (f->current_tool_bar_items)
6782 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6783 if (!STRINGP (help_echo))
9ea173e8
GM
6784 help_echo = (XVECTOR (f->current_tool_bar_items)
6785 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6786}
4d73d038 6787
06a2c219
GM
6788
6789\f
6790/* Find the glyph matrix position of buffer position POS in window W.
6791 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6792 current glyphs must be up to date. If POS is above window start
6793 return (0, 0, 0, 0). If POS is after end of W, return end of
6794 last line in W. */
b8009dd1
RS
6795
6796static int
06a2c219
GM
6797fast_find_position (w, pos, hpos, vpos, x, y)
6798 struct window *w;
b8009dd1 6799 int pos;
06a2c219 6800 int *hpos, *vpos, *x, *y;
b8009dd1 6801{
b8009dd1 6802 int i;
bf1c0ba1 6803 int lastcol;
06a2c219
GM
6804 int maybe_next_line_p = 0;
6805 int line_start_position;
6806 int yb = window_text_bottom_y (w);
6807 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6808 struct glyph_row *best_row = row;
6809 int row_vpos = 0, best_row_vpos = 0;
6810 int current_x;
6811
6812 while (row->y < yb)
b8009dd1 6813 {
06a2c219
GM
6814 if (row->used[TEXT_AREA])
6815 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6816 else
6817 line_start_position = 0;
6818
6819 if (line_start_position > pos)
b8009dd1 6820 break;
77b68646
RS
6821 /* If the position sought is the end of the buffer,
6822 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6823 else if (line_start_position == pos
6824 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6825 {
06a2c219 6826 maybe_next_line_p = 1;
77b68646
RS
6827 break;
6828 }
06a2c219
GM
6829 else if (line_start_position > 0)
6830 {
6831 best_row = row;
6832 best_row_vpos = row_vpos;
6833 }
4b0bb6f3
GM
6834
6835 if (row->y + row->height >= yb)
6836 break;
06a2c219
GM
6837
6838 ++row;
6839 ++row_vpos;
b8009dd1 6840 }
06a2c219
GM
6841
6842 /* Find the right column within BEST_ROW. */
6843 lastcol = 0;
6844 current_x = best_row->x;
6845 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6846 {
06a2c219
GM
6847 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6848 int charpos;
6849
6850 charpos = glyph->charpos;
6851 if (charpos == pos)
bf1c0ba1 6852 {
06a2c219
GM
6853 *hpos = i;
6854 *vpos = best_row_vpos;
6855 *x = current_x;
6856 *y = best_row->y;
bf1c0ba1
RS
6857 return 1;
6858 }
06a2c219 6859 else if (charpos > pos)
4d73d038 6860 break;
06a2c219
GM
6861 else if (charpos > 0)
6862 lastcol = i;
6863
6864 current_x += glyph->pixel_width;
bf1c0ba1 6865 }
b8009dd1 6866
77b68646
RS
6867 /* If we're looking for the end of the buffer,
6868 and we didn't find it in the line we scanned,
6869 use the start of the following line. */
06a2c219 6870 if (maybe_next_line_p)
77b68646 6871 {
06a2c219
GM
6872 ++best_row;
6873 ++best_row_vpos;
6874 lastcol = 0;
6875 current_x = best_row->x;
77b68646
RS
6876 }
6877
06a2c219
GM
6878 *vpos = best_row_vpos;
6879 *hpos = lastcol + 1;
6880 *x = current_x;
6881 *y = best_row->y;
b8009dd1
RS
6882 return 0;
6883}
6884
06a2c219 6885
b8009dd1
RS
6886/* Display the active region described by mouse_face_*
6887 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6888
6889static void
06a2c219 6890show_mouse_face (dpyinfo, draw)
7a13e894 6891 struct x_display_info *dpyinfo;
06a2c219 6892 enum draw_glyphs_face draw;
b8009dd1 6893{
7a13e894 6894 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6895 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6896 int i;
06a2c219
GM
6897 int cursor_off_p = 0;
6898 struct cursor_pos saved_cursor;
6899
6900 saved_cursor = output_cursor;
6901
6902 /* If window is in the process of being destroyed, don't bother
6903 to do anything. */
6904 if (w->current_matrix == NULL)
6905 goto set_x_cursor;
6906
6907 /* Recognize when we are called to operate on rows that don't exist
6908 anymore. This can happen when a window is split. */
6909 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6910 goto set_x_cursor;
6911
6912 set_output_cursor (&w->phys_cursor);
6913
6914 /* Note that mouse_face_beg_row etc. are window relative. */
6915 for (i = dpyinfo->mouse_face_beg_row;
6916 i <= dpyinfo->mouse_face_end_row;
6917 i++)
6918 {
6919 int start_hpos, end_hpos, start_x;
6920 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6921
6922 /* Don't do anything if row doesn't have valid contents. */
6923 if (!row->enabled_p)
6924 continue;
6925
6926 /* For all but the first row, the highlight starts at column 0. */
6927 if (i == dpyinfo->mouse_face_beg_row)
6928 {
6929 start_hpos = dpyinfo->mouse_face_beg_col;
6930 start_x = dpyinfo->mouse_face_beg_x;
6931 }
6932 else
6933 {
6934 start_hpos = 0;
6935 start_x = 0;
6936 }
6937
6938 if (i == dpyinfo->mouse_face_end_row)
6939 end_hpos = dpyinfo->mouse_face_end_col;
6940 else
6941 end_hpos = row->used[TEXT_AREA];
6942
6943 /* If the cursor's in the text we are about to rewrite, turn the
6944 cursor off. */
6945 if (!w->pseudo_window_p
6946 && i == output_cursor.vpos
6947 && output_cursor.hpos >= start_hpos - 1
6948 && output_cursor.hpos <= end_hpos)
514e4681 6949 {
06a2c219
GM
6950 x_update_window_cursor (w, 0);
6951 cursor_off_p = 1;
514e4681 6952 }
b8009dd1 6953
06a2c219 6954 if (end_hpos > start_hpos)
54a91a0f 6955 x_draw_glyphs (w, start_x, row, TEXT_AREA,
66ac4b0e 6956 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6957 }
6958
514e4681 6959 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6960 if (cursor_off_p)
6961 x_display_cursor (w, 1,
6962 output_cursor.hpos, output_cursor.vpos,
6963 output_cursor.x, output_cursor.y);
2729a2b5 6964
06a2c219 6965 output_cursor = saved_cursor;
fb3b7de5 6966
06a2c219
GM
6967 set_x_cursor:
6968
6969 /* Change the mouse cursor. */
6970 if (draw == DRAW_NORMAL_TEXT)
6971 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6972 f->output_data.x->text_cursor);
6973 else if (draw == DRAW_MOUSE_FACE)
334208b7 6974 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6975 f->output_data.x->cross_cursor);
27ead1d5 6976 else
334208b7 6977 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6978 f->output_data.x->nontext_cursor);
b8009dd1
RS
6979}
6980
6981/* Clear out the mouse-highlighted active region.
06a2c219 6982 Redraw it un-highlighted first. */
b8009dd1 6983
06a2c219 6984void
7a13e894
RS
6985clear_mouse_face (dpyinfo)
6986 struct x_display_info *dpyinfo;
b8009dd1 6987{
06a2c219
GM
6988 if (tip_frame)
6989 return;
6990
7a13e894 6991 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6992 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6993
7a13e894
RS
6994 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6995 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6996 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6997}
e687d06e
RS
6998
6999/* Just discard the mouse face information for frame F, if any.
7000 This is used when the size of F is changed. */
7001
dfcf069d 7002void
e687d06e
RS
7003cancel_mouse_face (f)
7004 FRAME_PTR f;
7005{
7006 Lisp_Object window;
7007 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7008
7009 window = dpyinfo->mouse_face_window;
7010 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7011 {
7012 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7013 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7014 dpyinfo->mouse_face_window = Qnil;
7015 }
7016}
b8009dd1 7017\f
ab648270
JB
7018static struct scroll_bar *x_window_to_scroll_bar ();
7019static void x_scroll_bar_report_motion ();
12ba150f 7020
90e65f07 7021/* Return the current position of the mouse.
2d7fc7e8 7022 *fp should be a frame which indicates which display to ask about.
90e65f07 7023
2d7fc7e8 7024 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7025 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7026 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7027 position on the scroll bar.
12ba150f 7028
2d7fc7e8 7029 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7030 mouse is on, *bar_window to nil, and *x and *y to the character cell
7031 the mouse is over.
7032
06a2c219 7033 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7034 was at this position.
7035
a135645a
RS
7036 Don't store anything if we don't have a valid set of values to report.
7037
90e65f07 7038 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7039 movement. */
90e65f07
JB
7040
7041static void
1cf412ec 7042XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7043 FRAME_PTR *fp;
1cf412ec 7044 int insist;
12ba150f 7045 Lisp_Object *bar_window;
ab648270 7046 enum scroll_bar_part *part;
90e65f07 7047 Lisp_Object *x, *y;
e5d77022 7048 unsigned long *time;
90e65f07 7049{
a135645a
RS
7050 FRAME_PTR f1;
7051
90e65f07
JB
7052 BLOCK_INPUT;
7053
8bcee03e 7054 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7055 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7056 else
7057 {
12ba150f
JB
7058 Window root;
7059 int root_x, root_y;
90e65f07 7060
12ba150f
JB
7061 Window dummy_window;
7062 int dummy;
7063
39d8bb4d
KH
7064 Lisp_Object frame, tail;
7065
7066 /* Clear the mouse-moved flag for every frame on this display. */
7067 FOR_EACH_FRAME (tail, frame)
7068 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7069 XFRAME (frame)->mouse_moved = 0;
7070
ab648270 7071 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7072
7073 /* Figure out which root window we're on. */
334208b7
RS
7074 XQueryPointer (FRAME_X_DISPLAY (*fp),
7075 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7076
7077 /* The root window which contains the pointer. */
7078 &root,
7079
7080 /* Trash which we can't trust if the pointer is on
7081 a different screen. */
7082 &dummy_window,
7083
7084 /* The position on that root window. */
58769bee 7085 &root_x, &root_y,
12ba150f
JB
7086
7087 /* More trash we can't trust. */
7088 &dummy, &dummy,
7089
7090 /* Modifier keys and pointer buttons, about which
7091 we don't care. */
7092 (unsigned int *) &dummy);
7093
7094 /* Now we have a position on the root; find the innermost window
7095 containing the pointer. */
7096 {
7097 Window win, child;
7098 int win_x, win_y;
06a2c219 7099 int parent_x = 0, parent_y = 0;
e99db5a1 7100 int count;
12ba150f
JB
7101
7102 win = root;
69388238 7103
2d7fc7e8
RS
7104 /* XTranslateCoordinates can get errors if the window
7105 structure is changing at the same time this function
7106 is running. So at least we must not crash from them. */
7107
e99db5a1 7108 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7109
334208b7 7110 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7111 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7112 {
69388238
RS
7113 /* If mouse was grabbed on a frame, give coords for that frame
7114 even if the mouse is now outside it. */
334208b7 7115 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7116
12ba150f 7117 /* From-window, to-window. */
69388238 7118 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7119
7120 /* From-position, to-position. */
7121 root_x, root_y, &win_x, &win_y,
7122
7123 /* Child of win. */
7124 &child);
69388238
RS
7125 f1 = last_mouse_frame;
7126 }
7127 else
7128 {
7129 while (1)
7130 {
334208b7 7131 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7132
69388238
RS
7133 /* From-window, to-window. */
7134 root, win,
12ba150f 7135
69388238
RS
7136 /* From-position, to-position. */
7137 root_x, root_y, &win_x, &win_y,
7138
7139 /* Child of win. */
7140 &child);
7141
9af3143a 7142 if (child == None || child == win)
69388238
RS
7143 break;
7144
7145 win = child;
7146 parent_x = win_x;
7147 parent_y = win_y;
7148 }
12ba150f 7149
69388238
RS
7150 /* Now we know that:
7151 win is the innermost window containing the pointer
7152 (XTC says it has no child containing the pointer),
7153 win_x and win_y are the pointer's position in it
7154 (XTC did this the last time through), and
7155 parent_x and parent_y are the pointer's position in win's parent.
7156 (They are what win_x and win_y were when win was child.
7157 If win is the root window, it has no parent, and
7158 parent_{x,y} are invalid, but that's okay, because we'll
7159 never use them in that case.) */
7160
7161 /* Is win one of our frames? */
19126e11 7162 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7163 }
58769bee 7164
2d7fc7e8
RS
7165 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7166 f1 = 0;
7167
e99db5a1 7168 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7169
ab648270 7170 /* If not, is it one of our scroll bars? */
a135645a 7171 if (! f1)
12ba150f 7172 {
ab648270 7173 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7174
7175 if (bar)
7176 {
a135645a 7177 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7178 win_x = parent_x;
7179 win_y = parent_y;
7180 }
7181 }
90e65f07 7182
8bcee03e 7183 if (f1 == 0 && insist > 0)
b86bd3dd 7184 f1 = SELECTED_FRAME ();
1cf412ec 7185
a135645a 7186 if (f1)
12ba150f 7187 {
06a2c219
GM
7188 /* Ok, we found a frame. Store all the values.
7189 last_mouse_glyph is a rectangle used to reduce the
7190 generation of mouse events. To not miss any motion
7191 events, we must divide the frame into rectangles of the
7192 size of the smallest character that could be displayed
7193 on it, i.e. into the same rectangles that matrices on
7194 the frame are divided into. */
7195
7196#if OLD_REDISPLAY_CODE
2b5c9e71 7197 int ignore1, ignore2;
2b5c9e71 7198 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7199 &last_mouse_glyph,
1cf412ec
RS
7200 FRAME_X_DISPLAY_INFO (f1)->grabbed
7201 || insist);
06a2c219
GM
7202#else
7203 {
7204 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7205 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7206 int x = win_x;
7207 int y = win_y;
7208
7209 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7210 round down even for negative values. */
7211 if (x < 0)
7212 x -= width - 1;
7213 if (y < 0)
7214 y -= height - 1;
7215
7216 last_mouse_glyph.width = width;
7217 last_mouse_glyph.height = height;
7218 last_mouse_glyph.x = (x + width - 1) / width * width;
7219 last_mouse_glyph.y = (y + height - 1) / height * height;
7220 }
7221#endif
12ba150f
JB
7222
7223 *bar_window = Qnil;
7224 *part = 0;
334208b7 7225 *fp = f1;
e0c1aef2
KH
7226 XSETINT (*x, win_x);
7227 XSETINT (*y, win_y);
12ba150f
JB
7228 *time = last_mouse_movement_time;
7229 }
7230 }
7231 }
90e65f07
JB
7232
7233 UNBLOCK_INPUT;
7234}
f451eb13 7235
06a2c219 7236
06a2c219 7237#ifdef USE_X_TOOLKIT
bffcfca9
GM
7238
7239/* Atimer callback function for TIMER. Called every 0.1s to process
7240 Xt timeouts, if needed. We must avoid calling XtAppPending as
7241 much as possible because that function does an implicit XFlush
7242 that slows us down. */
7243
7244static void
7245x_process_timeouts (timer)
7246 struct atimer *timer;
7247{
7248 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7249 {
7250 BLOCK_INPUT;
7251 while (XtAppPending (Xt_app_con) & XtIMTimer)
7252 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7253 UNBLOCK_INPUT;
7254 }
06a2c219
GM
7255}
7256
bffcfca9 7257#endif /* USE_X_TOOLKIT */
06a2c219
GM
7258
7259\f
7260/* Scroll bar support. */
7261
7262/* Given an X window ID, find the struct scroll_bar which manages it.
7263 This can be called in GC, so we have to make sure to strip off mark
7264 bits. */
bffcfca9 7265
06a2c219
GM
7266static struct scroll_bar *
7267x_window_to_scroll_bar (window_id)
7268 Window window_id;
7269{
7270 Lisp_Object tail;
7271
7272 for (tail = Vframe_list;
7273 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7274 tail = XCDR (tail))
06a2c219
GM
7275 {
7276 Lisp_Object frame, bar, condemned;
7277
8e713be6 7278 frame = XCAR (tail);
06a2c219
GM
7279 /* All elements of Vframe_list should be frames. */
7280 if (! GC_FRAMEP (frame))
7281 abort ();
7282
7283 /* Scan this frame's scroll bar list for a scroll bar with the
7284 right window ID. */
7285 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7286 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7287 /* This trick allows us to search both the ordinary and
7288 condemned scroll bar lists with one loop. */
7289 ! GC_NILP (bar) || (bar = condemned,
7290 condemned = Qnil,
7291 ! GC_NILP (bar));
7292 bar = XSCROLL_BAR (bar)->next)
7293 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7294 return XSCROLL_BAR (bar);
7295 }
7296
7297 return 0;
7298}
7299
7300
7301\f
7302/************************************************************************
7303 Toolkit scroll bars
7304 ************************************************************************/
7305
7306#if USE_TOOLKIT_SCROLL_BARS
7307
7308static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7309static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7310static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7311 struct scroll_bar *));
7312static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7313 int, int, int));
7314
7315
7316/* Id of action hook installed for scroll bars. */
7317
7318static XtActionHookId action_hook_id;
7319
7320/* Lisp window being scrolled. Set when starting to interact with
7321 a toolkit scroll bar, reset to nil when ending the interaction. */
7322
7323static Lisp_Object window_being_scrolled;
7324
7325/* Last scroll bar part sent in xm_scroll_callback. */
7326
7327static int last_scroll_bar_part;
7328
ec18280f
SM
7329/* Whether this is an Xaw with arrow-scrollbars. This should imply
7330 that movements of 1/20 of the screen size are mapped to up/down. */
7331
7332static Boolean xaw3d_arrow_scroll;
7333
7334/* Whether the drag scrolling maintains the mouse at the top of the
7335 thumb. If not, resizing the thumb needs to be done more carefully
7336 to avoid jerkyness. */
7337
7338static Boolean xaw3d_pick_top;
7339
06a2c219
GM
7340
7341/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7342 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7343 the user ends an interaction with the scroll bar, and generates
7344 a `end-scroll' scroll_bar_click' event if so. */
7345
7346static void
7347xt_action_hook (widget, client_data, action_name, event, params,
7348 num_params)
7349 Widget widget;
7350 XtPointer client_data;
7351 String action_name;
7352 XEvent *event;
7353 String *params;
7354 Cardinal *num_params;
7355{
7356 int scroll_bar_p;
7357 char *end_action;
7358
7359#ifdef USE_MOTIF
7360 scroll_bar_p = XmIsScrollBar (widget);
7361 end_action = "Release";
ec18280f 7362#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7363 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7364 end_action = "EndScroll";
ec18280f 7365#endif /* USE_MOTIF */
06a2c219 7366
06a2c219
GM
7367 if (scroll_bar_p
7368 && strcmp (action_name, end_action) == 0
7369 && WINDOWP (window_being_scrolled))
7370 {
7371 struct window *w;
7372
7373 x_send_scroll_bar_event (window_being_scrolled,
7374 scroll_bar_end_scroll, 0, 0);
7375 w = XWINDOW (window_being_scrolled);
7376 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7377 window_being_scrolled = Qnil;
7378 last_scroll_bar_part = -1;
bffcfca9
GM
7379
7380 /* Xt timeouts no longer needed. */
7381 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7382 }
7383}
7384
7385
7386/* Send a client message with message type Xatom_Scrollbar for a
7387 scroll action to the frame of WINDOW. PART is a value identifying
7388 the part of the scroll bar that was clicked on. PORTION is the
7389 amount to scroll of a whole of WHOLE. */
7390
7391static void
7392x_send_scroll_bar_event (window, part, portion, whole)
7393 Lisp_Object window;
7394 int part, portion, whole;
7395{
7396 XEvent event;
7397 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7398 struct frame *f = XFRAME (XWINDOW (window)->frame);
7399
7400 /* Construct a ClientMessage event to send to the frame. */
7401 ev->type = ClientMessage;
7402 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7403 ev->display = FRAME_X_DISPLAY (f);
7404 ev->window = FRAME_X_WINDOW (f);
7405 ev->format = 32;
7406 ev->data.l[0] = (long) window;
7407 ev->data.l[1] = (long) part;
7408 ev->data.l[2] = (long) 0;
7409 ev->data.l[3] = (long) portion;
7410 ev->data.l[4] = (long) whole;
7411
bffcfca9
GM
7412 /* Make Xt timeouts work while the scroll bar is active. */
7413 toolkit_scroll_bar_interaction = 1;
7414
06a2c219
GM
7415 /* Setting the event mask to zero means that the message will
7416 be sent to the client that created the window, and if that
7417 window no longer exists, no event will be sent. */
7418 BLOCK_INPUT;
7419 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7420 UNBLOCK_INPUT;
7421}
7422
7423
7424/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7425 in *IEVENT. */
7426
7427static void
7428x_scroll_bar_to_input_event (event, ievent)
7429 XEvent *event;
7430 struct input_event *ievent;
7431{
7432 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7433 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7434 struct frame *f = XFRAME (XWINDOW (window)->frame);
7435
7436 ievent->kind = scroll_bar_click;
7437 ievent->frame_or_window = window;
7438 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7439 ievent->part = ev->data.l[1];
7440 ievent->code = ev->data.l[2];
7441 ievent->x = make_number ((int) ev->data.l[3]);
7442 ievent->y = make_number ((int) ev->data.l[4]);
7443 ievent->modifiers = 0;
7444}
7445
7446
7447#ifdef USE_MOTIF
7448
7449/* Minimum and maximum values used for Motif scroll bars. */
7450
7451#define XM_SB_MIN 1
7452#define XM_SB_MAX 10000000
7453#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7454
7455
7456/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7457 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7458 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7459
7460static void
7461xm_scroll_callback (widget, client_data, call_data)
7462 Widget widget;
7463 XtPointer client_data, call_data;
7464{
7465 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7466 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7467 double percent;
7468 int part = -1, whole = 0, portion = 0;
7469
7470 switch (cs->reason)
7471 {
7472 case XmCR_DECREMENT:
7473 bar->dragging = Qnil;
7474 part = scroll_bar_up_arrow;
7475 break;
7476
7477 case XmCR_INCREMENT:
7478 bar->dragging = Qnil;
7479 part = scroll_bar_down_arrow;
7480 break;
7481
7482 case XmCR_PAGE_DECREMENT:
7483 bar->dragging = Qnil;
7484 part = scroll_bar_above_handle;
7485 break;
7486
7487 case XmCR_PAGE_INCREMENT:
7488 bar->dragging = Qnil;
7489 part = scroll_bar_below_handle;
7490 break;
7491
7492 case XmCR_TO_TOP:
7493 bar->dragging = Qnil;
7494 part = scroll_bar_to_top;
7495 break;
7496
7497 case XmCR_TO_BOTTOM:
7498 bar->dragging = Qnil;
7499 part = scroll_bar_to_bottom;
7500 break;
7501
7502 case XmCR_DRAG:
7503 {
7504 int slider_size;
7505 int dragging_down_p = (INTEGERP (bar->dragging)
7506 && XINT (bar->dragging) <= cs->value);
7507
7508 /* Get the slider size. */
7509 BLOCK_INPUT;
7510 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7511 UNBLOCK_INPUT;
7512
7513 /* At the max position of the scroll bar, do a line-wise
7514 movement. Without doing anything, the LessTif scroll bar
7515 calls us with the same cs->value again and again. If we
7516 want to make sure that we can reach the end of the buffer,
7517 we have to do something.
7518
7519 Implementation note: setting bar->dragging always to
7520 cs->value gives a smoother movement at the max position.
7521 Setting it to nil when doing line-wise movement gives
7522 a better slider behavior. */
7523
7524 if (cs->value + slider_size == XM_SB_MAX
7525 || (dragging_down_p
7526 && last_scroll_bar_part == scroll_bar_down_arrow))
7527 {
7528 part = scroll_bar_down_arrow;
7529 bar->dragging = Qnil;
7530 }
7531 else
7532 {
7533 whole = XM_SB_RANGE;
7534 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7535 part = scroll_bar_handle;
7536 bar->dragging = make_number (cs->value);
7537 }
7538 }
7539 break;
7540
7541 case XmCR_VALUE_CHANGED:
7542 break;
7543 };
7544
7545 if (part >= 0)
7546 {
7547 window_being_scrolled = bar->window;
7548 last_scroll_bar_part = part;
7549 x_send_scroll_bar_event (bar->window, part, portion, whole);
7550 }
7551}
7552
7553
ec18280f 7554#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7555
7556
ec18280f 7557/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7558 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7559 scroll bar struct. CALL_DATA is a pointer to a float saying where
7560 the thumb is. */
7561
7562static void
ec18280f 7563xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7564 Widget widget;
7565 XtPointer client_data, call_data;
7566{
7567 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7568 float top = *(float *) call_data;
7569 float shown;
ec18280f
SM
7570 int whole, portion, height;
7571 int part;
06a2c219
GM
7572
7573 /* Get the size of the thumb, a value between 0 and 1. */
7574 BLOCK_INPUT;
ec18280f 7575 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7576 UNBLOCK_INPUT;
7577
7578 whole = 10000000;
7579 portion = shown < 1 ? top * whole : 0;
06a2c219 7580
ec18280f
SM
7581 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7582 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7583 the bottom, so we force the scrolling whenever we see that we're
7584 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7585 we try to ensure that we always stay two pixels away from the
7586 bottom). */
06a2c219
GM
7587 part = scroll_bar_down_arrow;
7588 else
7589 part = scroll_bar_handle;
7590
7591 window_being_scrolled = bar->window;
7592 bar->dragging = make_number (portion);
7593 last_scroll_bar_part = part;
7594 x_send_scroll_bar_event (bar->window, part, portion, whole);
7595}
7596
7597
ec18280f
SM
7598/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7599 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7600 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7601 the scroll bar. CALL_DATA is an integer specifying the action that
7602 has taken place. It's magnitude is in the range 0..height of the
7603 scroll bar. Negative values mean scroll towards buffer start.
7604 Values < height of scroll bar mean line-wise movement. */
7605
7606static void
ec18280f 7607xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7608 Widget widget;
7609 XtPointer client_data, call_data;
7610{
7611 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7612 int position = (int) call_data;
7613 Dimension height;
7614 int part;
7615
7616 /* Get the height of the scroll bar. */
7617 BLOCK_INPUT;
7618 XtVaGetValues (widget, XtNheight, &height, NULL);
7619 UNBLOCK_INPUT;
7620
ec18280f
SM
7621 if (abs (position) >= height)
7622 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7623
7624 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7625 it maps line-movement to call_data = max(5, height/20). */
7626 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7627 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7628 else
ec18280f 7629 part = scroll_bar_move_ratio;
06a2c219
GM
7630
7631 window_being_scrolled = bar->window;
7632 bar->dragging = Qnil;
7633 last_scroll_bar_part = part;
ec18280f 7634 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7635}
7636
7637
7638#endif /* not USE_MOTIF */
7639
7640
7641/* Create the widget for scroll bar BAR on frame F. Record the widget
7642 and X window of the scroll bar in BAR. */
7643
7644static void
7645x_create_toolkit_scroll_bar (f, bar)
7646 struct frame *f;
7647 struct scroll_bar *bar;
7648{
7649 Window xwindow;
7650 Widget widget;
7651 Arg av[20];
7652 int ac = 0;
7653 char *scroll_bar_name = "verticalScrollBar";
7654 unsigned long pixel;
7655
7656 BLOCK_INPUT;
7657
7658#ifdef USE_MOTIF
7659 /* LessTif 0.85, problems:
7660
7661 1. When the mouse if over the scroll bar, the scroll bar will
7662 get keyboard events. I didn't find a way to turn this off.
7663
7664 2. Do we have to explicitly set the cursor to get an arrow
7665 cursor (see below)? */
7666
7667 /* Set resources. Create the widget. */
7668 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7669 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7670 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7671 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7672 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7673 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7674 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7675
7676 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7677 if (pixel != -1)
7678 {
7679 XtSetArg (av[ac], XmNforeground, pixel);
7680 ++ac;
7681 }
7682
7683 pixel = f->output_data.x->scroll_bar_background_pixel;
7684 if (pixel != -1)
7685 {
7686 XtSetArg (av[ac], XmNbackground, pixel);
7687 ++ac;
7688 }
7689
7690 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7691 scroll_bar_name, av, ac);
7692
7693 /* Add one callback for everything that can happen. */
7694 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7695 (XtPointer) bar);
7696 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7697 (XtPointer) bar);
7698 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7699 (XtPointer) bar);
7700 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7701 (XtPointer) bar);
7702 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7703 (XtPointer) bar);
7704 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7705 (XtPointer) bar);
7706 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7707 (XtPointer) bar);
7708
7709 /* Realize the widget. Only after that is the X window created. */
7710 XtRealizeWidget (widget);
7711
7712 /* Set the cursor to an arrow. I didn't find a resource to do that.
7713 And I'm wondering why it hasn't an arrow cursor by default. */
7714 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7715 f->output_data.x->nontext_cursor);
7716
ec18280f 7717#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7718
7719 /* Set resources. Create the widget. The background of the
7720 Xaw3d scroll bar widget is a little bit light for my taste.
7721 We don't alter it here to let users change it according
7722 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7723 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7724 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7725 /* For smoother scrolling with Xaw3d -sm */
7726 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7727 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7728
7729 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7730 if (pixel != -1)
7731 {
7732 XtSetArg (av[ac], XtNforeground, pixel);
7733 ++ac;
7734 }
7735
7736 pixel = f->output_data.x->scroll_bar_background_pixel;
7737 if (pixel != -1)
7738 {
7739 XtSetArg (av[ac], XtNbackground, pixel);
7740 ++ac;
7741 }
7742
7743 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7744 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7745
7746 {
7747 char *initial = "";
7748 char *val = initial;
7749 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7750 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7751 if (val == initial)
7752 { /* ARROW_SCROLL */
7753 xaw3d_arrow_scroll = True;
7754 /* Isn't that just a personal preference ? -sm */
7755 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7756 }
7757 }
06a2c219
GM
7758
7759 /* Define callbacks. */
ec18280f
SM
7760 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7761 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7762 (XtPointer) bar);
7763
7764 /* Realize the widget. Only after that is the X window created. */
7765 XtRealizeWidget (widget);
7766
ec18280f 7767#endif /* !USE_MOTIF */
06a2c219
GM
7768
7769 /* Install an action hook that let's us detect when the user
7770 finishes interacting with a scroll bar. */
7771 if (action_hook_id == 0)
7772 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7773
7774 /* Remember X window and widget in the scroll bar vector. */
7775 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7776 xwindow = XtWindow (widget);
7777 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7778
7779 UNBLOCK_INPUT;
7780}
7781
7782
7783/* Set the thumb size and position of scroll bar BAR. We are currently
7784 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7785
7786static void
7787x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7788 struct scroll_bar *bar;
7789 int portion, position, whole;
f451eb13 7790{
06a2c219 7791 float top, shown;
06a2c219 7792 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7793
06a2c219
GM
7794 if (whole == 0)
7795 top = 0, shown = 1;
7796 else
f451eb13 7797 {
06a2c219
GM
7798 top = (float) position / whole;
7799 shown = (float) portion / whole;
7800 }
f451eb13 7801
06a2c219 7802 BLOCK_INPUT;
f451eb13 7803
06a2c219
GM
7804#ifdef USE_MOTIF
7805 {
7806 int size, value;
7807 Boolean arrow1_selected, arrow2_selected;
7808 unsigned char flags;
7809 XmScrollBarWidget sb;
7810
ec18280f 7811 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7812 is the scroll bar's maximum and MIN is the scroll bar's minimum
7813 value. */
7814 size = shown * XM_SB_RANGE;
7815 size = min (size, XM_SB_RANGE);
7816 size = max (size, 1);
7817
7818 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7819 value = top * XM_SB_RANGE;
7820 value = min (value, XM_SB_MAX - size);
7821 value = max (value, XM_SB_MIN);
7822
7823 /* LessTif: Calling XmScrollBarSetValues after an increment or
7824 decrement turns off auto-repeat LessTif-internally. This can
7825 be seen in ScrollBar.c which resets Arrow1Selected and
7826 Arrow2Selected. It also sets internal flags so that LessTif
7827 believes the mouse is in the slider. We either have to change
7828 our code, or work around that by accessing private data. */
7829
7830 sb = (XmScrollBarWidget) widget;
7831 arrow1_selected = sb->scrollBar.arrow1_selected;
7832 arrow2_selected = sb->scrollBar.arrow2_selected;
7833 flags = sb->scrollBar.flags;
7834
7835 if (NILP (bar->dragging))
7836 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7837 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7838 /* This has the negative side effect that the slider value is
ec18280f 7839 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7840 page-wise movement. */
7841 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7842 else
7843 {
7844 /* If currently dragging, only update the slider size.
7845 This reduces flicker effects. */
7846 int old_value, old_size, increment, page_increment;
7847
7848 XmScrollBarGetValues (widget, &old_value, &old_size,
7849 &increment, &page_increment);
7850 XmScrollBarSetValues (widget, old_value,
7851 min (size, XM_SB_RANGE - old_value),
7852 0, 0, False);
7853 }
7854
7855 sb->scrollBar.arrow1_selected = arrow1_selected;
7856 sb->scrollBar.arrow2_selected = arrow2_selected;
7857 sb->scrollBar.flags = flags;
7858 }
ec18280f 7859#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7860 {
ec18280f
SM
7861 float old_top, old_shown;
7862 Dimension height;
7863 XtVaGetValues (widget,
7864 XtNtopOfThumb, &old_top,
7865 XtNshown, &old_shown,
7866 XtNheight, &height,
7867 NULL);
7868
7869 /* Massage the top+shown values. */
7870 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7871 top = max (0, min (1, top));
7872 else
7873 top = old_top;
7874 /* Keep two pixels available for moving the thumb down. */
7875 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7876
7877 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7878 check that your system's configuration file contains a define
7879 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7880 if (top != old_top || shown != old_shown)
eb393530 7881 {
ec18280f 7882 if (NILP (bar->dragging))
eb393530 7883 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7884 else
7885 {
ec18280f
SM
7886#ifdef HAVE_XAW3D
7887 ScrollbarWidget sb = (ScrollbarWidget) widget;
7888 int scroll_mode;
7889
7890 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7891 if (xaw3d_arrow_scroll)
7892 {
7893 /* Xaw3d stupidly ignores resize requests while dragging
7894 so we have to make it believe it's not in dragging mode. */
7895 scroll_mode = sb->scrollbar.scroll_mode;
7896 if (scroll_mode == 2)
7897 sb->scrollbar.scroll_mode = 0;
7898 }
7899#endif
7900 /* Try to make the scrolling a tad smoother. */
7901 if (!xaw3d_pick_top)
7902 shown = min (shown, old_shown);
7903
7904 XawScrollbarSetThumb (widget, top, shown);
7905
7906#ifdef HAVE_XAW3D
7907 if (xaw3d_arrow_scroll && scroll_mode == 2)
7908 sb->scrollbar.scroll_mode = scroll_mode;
7909#endif
06a2c219 7910 }
06a2c219
GM
7911 }
7912 }
ec18280f 7913#endif /* !USE_MOTIF */
06a2c219
GM
7914
7915 UNBLOCK_INPUT;
f451eb13
JB
7916}
7917
06a2c219
GM
7918#endif /* USE_TOOLKIT_SCROLL_BARS */
7919
7920
7921\f
7922/************************************************************************
7923 Scroll bars, general
7924 ************************************************************************/
7925
7926/* Create a scroll bar and return the scroll bar vector for it. W is
7927 the Emacs window on which to create the scroll bar. TOP, LEFT,
7928 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7929 scroll bar. */
7930
ab648270 7931static struct scroll_bar *
06a2c219
GM
7932x_scroll_bar_create (w, top, left, width, height)
7933 struct window *w;
f451eb13
JB
7934 int top, left, width, height;
7935{
06a2c219 7936 struct frame *f = XFRAME (w->frame);
334208b7
RS
7937 struct scroll_bar *bar
7938 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7939
7940 BLOCK_INPUT;
7941
06a2c219
GM
7942#if USE_TOOLKIT_SCROLL_BARS
7943 x_create_toolkit_scroll_bar (f, bar);
7944#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7945 {
7946 XSetWindowAttributes a;
7947 unsigned long mask;
5c187dee 7948 Window window;
06a2c219
GM
7949
7950 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7951 if (a.background_pixel == -1)
7952 a.background_pixel = f->output_data.x->background_pixel;
7953
12ba150f 7954 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7955 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7956 | ExposureMask);
7a13e894 7957 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7958
dbc4e1c1 7959 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7960
06a2c219
GM
7961 /* Clear the area of W that will serve as a scroll bar. This is
7962 for the case that a window has been split horizontally. In
7963 this case, no clear_frame is generated to reduce flickering. */
7964 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7965 left, top, width,
7966 window_box_height (w), False);
7967
7968 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7969 /* Position and size of scroll bar. */
7970 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7971 top,
7972 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7973 height,
7974 /* Border width, depth, class, and visual. */
7975 0,
7976 CopyFromParent,
7977 CopyFromParent,
7978 CopyFromParent,
7979 /* Attributes. */
7980 mask, &a);
7981 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7982 }
06a2c219 7983#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7984
06a2c219 7985 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7986 XSETINT (bar->top, top);
7987 XSETINT (bar->left, left);
7988 XSETINT (bar->width, width);
7989 XSETINT (bar->height, height);
7990 XSETINT (bar->start, 0);
7991 XSETINT (bar->end, 0);
12ba150f 7992 bar->dragging = Qnil;
f451eb13
JB
7993
7994 /* Add bar to its frame's list of scroll bars. */
334208b7 7995 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7996 bar->prev = Qnil;
334208b7 7997 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7998 if (!NILP (bar->next))
e0c1aef2 7999 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8000
06a2c219
GM
8001 /* Map the window/widget. */
8002#if USE_TOOLKIT_SCROLL_BARS
8003 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8004 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8005 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8006 top,
8007 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8008 height, 0);
8009#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8010 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8011#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8012
8013 UNBLOCK_INPUT;
12ba150f 8014 return bar;
f451eb13
JB
8015}
8016
06a2c219 8017
12ba150f 8018/* Draw BAR's handle in the proper position.
06a2c219 8019
12ba150f
JB
8020 If the handle is already drawn from START to END, don't bother
8021 redrawing it, unless REBUILD is non-zero; in that case, always
8022 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8023 events.)
12ba150f
JB
8024
8025 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8026 fit inside its rectangle, but if the user is dragging the scroll
8027 bar handle, we want to let them drag it down all the way, so that
8028 the bar's top is as far down as it goes; otherwise, there's no way
8029 to move to the very end of the buffer. */
8030
5c187dee
GM
8031#ifndef USE_TOOLKIT_SCROLL_BARS
8032
f451eb13 8033static void
ab648270
JB
8034x_scroll_bar_set_handle (bar, start, end, rebuild)
8035 struct scroll_bar *bar;
f451eb13 8036 int start, end;
12ba150f 8037 int rebuild;
f451eb13 8038{
12ba150f 8039 int dragging = ! NILP (bar->dragging);
ab648270 8040 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8041 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8042 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8043
8044 /* If the display is already accurate, do nothing. */
8045 if (! rebuild
8046 && start == XINT (bar->start)
8047 && end == XINT (bar->end))
8048 return;
8049
f451eb13
JB
8050 BLOCK_INPUT;
8051
8052 {
d9cdbb3d
RS
8053 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8054 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8055 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8056
8057 /* Make sure the values are reasonable, and try to preserve
8058 the distance between start and end. */
12ba150f
JB
8059 {
8060 int length = end - start;
8061
8062 if (start < 0)
8063 start = 0;
8064 else if (start > top_range)
8065 start = top_range;
8066 end = start + length;
8067
8068 if (end < start)
8069 end = start;
8070 else if (end > top_range && ! dragging)
8071 end = top_range;
8072 }
f451eb13 8073
ab648270 8074 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8075 XSETINT (bar->start, start);
8076 XSETINT (bar->end, end);
f451eb13 8077
12ba150f
JB
8078 /* Clip the end position, just for display. */
8079 if (end > top_range)
8080 end = top_range;
f451eb13 8081
ab648270 8082 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8083 below top positions, to make sure the handle is always at least
8084 that many pixels tall. */
ab648270 8085 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8086
12ba150f
JB
8087 /* Draw the empty space above the handle. Note that we can't clear
8088 zero-height areas; that means "clear to end of window." */
8089 if (0 < start)
334208b7 8090 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8091
12ba150f 8092 /* x, y, width, height, and exposures. */
ab648270
JB
8093 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8094 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8095 inside_width, start,
8096 False);
f451eb13 8097
06a2c219
GM
8098 /* Change to proper foreground color if one is specified. */
8099 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8100 XSetForeground (FRAME_X_DISPLAY (f), gc,
8101 f->output_data.x->scroll_bar_foreground_pixel);
8102
12ba150f 8103 /* Draw the handle itself. */
334208b7 8104 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8105
12ba150f 8106 /* x, y, width, height */
ab648270
JB
8107 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8108 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8109 inside_width, end - start);
f451eb13 8110
06a2c219
GM
8111 /* Restore the foreground color of the GC if we changed it above. */
8112 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8113 XSetForeground (FRAME_X_DISPLAY (f), gc,
8114 f->output_data.x->foreground_pixel);
f451eb13 8115
12ba150f
JB
8116 /* Draw the empty space below the handle. Note that we can't
8117 clear zero-height areas; that means "clear to end of window." */
8118 if (end < inside_height)
334208b7 8119 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8120
12ba150f 8121 /* x, y, width, height, and exposures. */
ab648270
JB
8122 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8123 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8124 inside_width, inside_height - end,
8125 False);
f451eb13 8126
f451eb13
JB
8127 }
8128
f451eb13
JB
8129 UNBLOCK_INPUT;
8130}
8131
5c187dee 8132#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8133
06a2c219
GM
8134/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8135 nil. */
58769bee 8136
12ba150f 8137static void
ab648270
JB
8138x_scroll_bar_remove (bar)
8139 struct scroll_bar *bar;
12ba150f 8140{
12ba150f
JB
8141 BLOCK_INPUT;
8142
06a2c219
GM
8143#if USE_TOOLKIT_SCROLL_BARS
8144 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8145#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8146 {
8147 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8148 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8149 }
06a2c219
GM
8150#endif /* not USE_TOOLKIT_SCROLL_BARS */
8151
ab648270
JB
8152 /* Disassociate this scroll bar from its window. */
8153 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8154
8155 UNBLOCK_INPUT;
8156}
8157
06a2c219 8158
12ba150f
JB
8159/* Set the handle of the vertical scroll bar for WINDOW to indicate
8160 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8161 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8162 create one. */
06a2c219 8163
12ba150f 8164static void
06a2c219
GM
8165XTset_vertical_scroll_bar (w, portion, whole, position)
8166 struct window *w;
f451eb13
JB
8167 int portion, whole, position;
8168{
06a2c219 8169 struct frame *f = XFRAME (w->frame);
ab648270 8170 struct scroll_bar *bar;
3c6ede7b 8171 int top, height, left, sb_left, width, sb_width;
06a2c219 8172 int window_x, window_y, window_width, window_height;
06a2c219 8173
3c6ede7b 8174 /* Get window dimensions. */
06a2c219 8175 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8176 top = window_y;
8177 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8178 height = window_height;
06a2c219 8179
3c6ede7b 8180 /* Compute the left edge of the scroll bar area. */
06a2c219 8181 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8182 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8183 else
8184 left = XFASTINT (w->left);
8185 left *= CANON_X_UNIT (f);
8186 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8187
8188 /* Compute the width of the scroll bar which might be less than
8189 the width of the area reserved for the scroll bar. */
8190 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8191 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8192 else
3c6ede7b 8193 sb_width = width;
12ba150f 8194
3c6ede7b
GM
8195 /* Compute the left edge of the scroll bar. */
8196#ifdef USE_TOOLKIT_SCROLL_BARS
8197 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8198 sb_left = left + width - sb_width - (width - sb_width) / 2;
8199 else
8200 sb_left = left + (width - sb_width) / 2;
8201#else
8202 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8203 sb_left = left + width - sb_width;
8204 else
8205 sb_left = left;
8206#endif
8207
ab648270 8208 /* Does the scroll bar exist yet? */
06a2c219 8209 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8210 {
80c32bcc 8211 BLOCK_INPUT;
3c6ede7b
GM
8212 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8213 left, top, width, height, False);
80c32bcc 8214 UNBLOCK_INPUT;
3c6ede7b
GM
8215 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8216 }
f451eb13 8217 else
12ba150f
JB
8218 {
8219 /* It may just need to be moved and resized. */
06a2c219
GM
8220 unsigned int mask = 0;
8221
8222 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8223
8224 BLOCK_INPUT;
8225
3c6ede7b 8226 if (sb_left != XINT (bar->left))
06a2c219 8227 mask |= CWX;
3c6ede7b 8228 if (top != XINT (bar->top))
06a2c219 8229 mask |= CWY;
3c6ede7b 8230 if (sb_width != XINT (bar->width))
06a2c219 8231 mask |= CWWidth;
3c6ede7b 8232 if (height != XINT (bar->height))
06a2c219
GM
8233 mask |= CWHeight;
8234
8235#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8236
8237 /* Since toolkit scroll bars are smaller than the space reserved
8238 for them on the frame, we have to clear "under" them. */
8239 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8240 left, top, width, height, False);
06a2c219
GM
8241
8242 /* Move/size the scroll bar widget. */
8243 if (mask)
8244 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8245 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8246 top,
8247 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8248 height, 0);
06a2c219
GM
8249
8250#else /* not USE_TOOLKIT_SCROLL_BARS */
8251
e1f6572f
RS
8252 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8253 {
8254 /* Clear areas not covered by the scroll bar. This makes sure a
8255 previous mode line display is cleared after C-x 2 C-x 1, for
8256 example. Non-toolkit scroll bars are as wide as the area
8257 reserved for scroll bars - trim at both sides. */
8258 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8259 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8260 height, False);
8261 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8262 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8263 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8264 height, False);
8265 }
06a2c219
GM
8266
8267 /* Move/size the scroll bar window. */
8268 if (mask)
8269 {
8270 XWindowChanges wc;
8271
3c6ede7b
GM
8272 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8273 wc.y = top;
8274 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8275 wc.height = height;
06a2c219
GM
8276 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8277 mask, &wc);
8278 }
8279
8280#endif /* not USE_TOOLKIT_SCROLL_BARS */
8281
8282 /* Remember new settings. */
3c6ede7b
GM
8283 XSETINT (bar->left, sb_left);
8284 XSETINT (bar->top, top);
8285 XSETINT (bar->width, sb_width);
8286 XSETINT (bar->height, height);
06a2c219
GM
8287
8288 UNBLOCK_INPUT;
12ba150f 8289 }
f451eb13 8290
06a2c219
GM
8291#if USE_TOOLKIT_SCROLL_BARS
8292 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8293#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8294 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8295 dragged. */
12ba150f 8296 if (NILP (bar->dragging))
f451eb13 8297 {
92857db0 8298 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8299
12ba150f 8300 if (whole == 0)
ab648270 8301 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8302 else
8303 {
43f868f5
JB
8304 int start = ((double) position * top_range) / whole;
8305 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8306 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8307 }
f451eb13 8308 }
06a2c219 8309#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8310
06a2c219 8311 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8312}
8313
12ba150f 8314
f451eb13 8315/* The following three hooks are used when we're doing a thorough
ab648270 8316 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8317 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8318 away is a real pain - "Can you say set-window-configuration, boys
8319 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8320 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8321 from the fiery pit when we actually redisplay its window. */
f451eb13 8322
ab648270
JB
8323/* Arrange for all scroll bars on FRAME to be removed at the next call
8324 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8325 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8326
58769bee 8327static void
ab648270 8328XTcondemn_scroll_bars (frame)
f451eb13
JB
8329 FRAME_PTR frame;
8330{
f9e24cb9
RS
8331 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8332 while (! NILP (FRAME_SCROLL_BARS (frame)))
8333 {
8334 Lisp_Object bar;
8335 bar = FRAME_SCROLL_BARS (frame);
8336 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8337 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8338 XSCROLL_BAR (bar)->prev = Qnil;
8339 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8340 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8341 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8342 }
f451eb13
JB
8343}
8344
06a2c219 8345/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8346 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8347static void
ab648270 8348XTredeem_scroll_bar (window)
12ba150f 8349 struct window *window;
f451eb13 8350{
ab648270 8351 struct scroll_bar *bar;
12ba150f 8352
ab648270
JB
8353 /* We can't redeem this window's scroll bar if it doesn't have one. */
8354 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8355 abort ();
8356
ab648270 8357 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8358
8359 /* Unlink it from the condemned list. */
8360 {
8361 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8362
8363 if (NILP (bar->prev))
8364 {
8365 /* If the prev pointer is nil, it must be the first in one of
8366 the lists. */
ab648270 8367 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8368 /* It's not condemned. Everything's fine. */
8369 return;
ab648270
JB
8370 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8371 window->vertical_scroll_bar))
8372 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8373 else
8374 /* If its prev pointer is nil, it must be at the front of
8375 one or the other! */
8376 abort ();
8377 }
8378 else
ab648270 8379 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8380
8381 if (! NILP (bar->next))
ab648270 8382 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8383
ab648270 8384 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8385 bar->prev = Qnil;
e0c1aef2 8386 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8387 if (! NILP (bar->next))
e0c1aef2 8388 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8389 }
f451eb13
JB
8390}
8391
ab648270
JB
8392/* Remove all scroll bars on FRAME that haven't been saved since the
8393 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8394
f451eb13 8395static void
ab648270 8396XTjudge_scroll_bars (f)
12ba150f 8397 FRAME_PTR f;
f451eb13 8398{
12ba150f 8399 Lisp_Object bar, next;
f451eb13 8400
ab648270 8401 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8402
8403 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8404 more events on the hapless scroll bars. */
8405 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8406
8407 for (; ! NILP (bar); bar = next)
f451eb13 8408 {
ab648270 8409 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8410
ab648270 8411 x_scroll_bar_remove (b);
12ba150f
JB
8412
8413 next = b->next;
8414 b->next = b->prev = Qnil;
f451eb13 8415 }
12ba150f 8416
ab648270 8417 /* Now there should be no references to the condemned scroll bars,
12ba150f 8418 and they should get garbage-collected. */
f451eb13
JB
8419}
8420
8421
06a2c219
GM
8422/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8423 is a no-op when using toolkit scroll bars.
ab648270
JB
8424
8425 This may be called from a signal handler, so we have to ignore GC
8426 mark bits. */
06a2c219 8427
f451eb13 8428static void
ab648270
JB
8429x_scroll_bar_expose (bar, event)
8430 struct scroll_bar *bar;
f451eb13
JB
8431 XEvent *event;
8432{
06a2c219
GM
8433#ifndef USE_TOOLKIT_SCROLL_BARS
8434
ab648270 8435 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8436 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8437 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8438 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8439
f451eb13
JB
8440 BLOCK_INPUT;
8441
ab648270 8442 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8443
06a2c219 8444 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8445 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8446
8447 /* x, y, width, height */
d9cdbb3d 8448 0, 0,
3cbd2e0b 8449 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8450 XINT (bar->height) - 1);
8451
f451eb13 8452 UNBLOCK_INPUT;
06a2c219
GM
8453
8454#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8455}
8456
ab648270
JB
8457/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8458 is set to something other than no_event, it is enqueued.
8459
8460 This may be called from a signal handler, so we have to ignore GC
8461 mark bits. */
06a2c219 8462
5c187dee
GM
8463#ifndef USE_TOOLKIT_SCROLL_BARS
8464
f451eb13 8465static void
ab648270
JB
8466x_scroll_bar_handle_click (bar, event, emacs_event)
8467 struct scroll_bar *bar;
f451eb13
JB
8468 XEvent *event;
8469 struct input_event *emacs_event;
8470{
0299d313 8471 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8472 abort ();
8473
ab648270 8474 emacs_event->kind = scroll_bar_click;
69388238 8475 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8476 emacs_event->modifiers
8477 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8478 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8479 event->xbutton.state)
8480 | (event->type == ButtonRelease
8481 ? up_modifier
8482 : down_modifier));
12ba150f 8483 emacs_event->frame_or_window = bar->window;
f451eb13 8484 emacs_event->timestamp = event->xbutton.time;
12ba150f 8485 {
06a2c219 8486#if 0
d9cdbb3d 8487 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8488 int internal_height
d9cdbb3d 8489 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8490#endif
0299d313 8491 int top_range
d9cdbb3d 8492 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8493 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8494
8495 if (y < 0) y = 0;
8496 if (y > top_range) y = top_range;
8497
8498 if (y < XINT (bar->start))
ab648270
JB
8499 emacs_event->part = scroll_bar_above_handle;
8500 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8501 emacs_event->part = scroll_bar_handle;
12ba150f 8502 else
ab648270 8503 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8504
8505 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8506 they want to drag it. Lisp code needs to be able to decide
8507 whether or not we're dragging. */
929787e1 8508#if 0
12ba150f
JB
8509 /* If the user has just clicked on the handle, record where they're
8510 holding it. */
8511 if (event->type == ButtonPress
ab648270 8512 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8513 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8514#endif
12ba150f
JB
8515
8516 /* If the user has released the handle, set it to its final position. */
8517 if (event->type == ButtonRelease
8518 && ! NILP (bar->dragging))
8519 {
8520 int new_start = y - XINT (bar->dragging);
8521 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8522
ab648270 8523 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8524 bar->dragging = Qnil;
8525 }
f451eb13 8526
5116f055
JB
8527 /* Same deal here as the other #if 0. */
8528#if 0
58769bee 8529 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8530 the handle. */
ab648270 8531 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8532 emacs_event->x = bar->start;
8533 else
e0c1aef2 8534 XSETINT (emacs_event->x, y);
5116f055 8535#else
e0c1aef2 8536 XSETINT (emacs_event->x, y);
5116f055 8537#endif
f451eb13 8538
e0c1aef2 8539 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8540 }
8541}
f451eb13 8542
ab648270
JB
8543/* Handle some mouse motion while someone is dragging the scroll bar.
8544
8545 This may be called from a signal handler, so we have to ignore GC
8546 mark bits. */
06a2c219 8547
f451eb13 8548static void
ab648270
JB
8549x_scroll_bar_note_movement (bar, event)
8550 struct scroll_bar *bar;
f451eb13
JB
8551 XEvent *event;
8552{
39d8bb4d
KH
8553 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8554
f451eb13
JB
8555 last_mouse_movement_time = event->xmotion.time;
8556
39d8bb4d 8557 f->mouse_moved = 1;
e0c1aef2 8558 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8559
8560 /* If we're dragging the bar, display it. */
ab648270 8561 if (! GC_NILP (bar->dragging))
f451eb13
JB
8562 {
8563 /* Where should the handle be now? */
12ba150f 8564 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8565
12ba150f 8566 if (new_start != XINT (bar->start))
f451eb13 8567 {
12ba150f 8568 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8569
ab648270 8570 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8571 }
8572 }
f451eb13
JB
8573}
8574
5c187dee
GM
8575#endif /* !USE_TOOLKIT_SCROLL_BARS */
8576
12ba150f 8577/* Return information to the user about the current position of the mouse
ab648270 8578 on the scroll bar. */
06a2c219 8579
12ba150f 8580static void
334208b7
RS
8581x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8582 FRAME_PTR *fp;
12ba150f 8583 Lisp_Object *bar_window;
ab648270 8584 enum scroll_bar_part *part;
12ba150f
JB
8585 Lisp_Object *x, *y;
8586 unsigned long *time;
8587{
ab648270 8588 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8589 Window w = SCROLL_BAR_X_WINDOW (bar);
8590 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8591 int win_x, win_y;
559cb2fb
JB
8592 Window dummy_window;
8593 int dummy_coord;
8594 unsigned int dummy_mask;
12ba150f 8595
cf7cb199
JB
8596 BLOCK_INPUT;
8597
ab648270 8598 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8599 report that. */
334208b7 8600 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8601
559cb2fb
JB
8602 /* Root, child, root x and root y. */
8603 &dummy_window, &dummy_window,
8604 &dummy_coord, &dummy_coord,
12ba150f 8605
559cb2fb
JB
8606 /* Position relative to scroll bar. */
8607 &win_x, &win_y,
12ba150f 8608
559cb2fb
JB
8609 /* Mouse buttons and modifier keys. */
8610 &dummy_mask))
7a13e894 8611 ;
559cb2fb
JB
8612 else
8613 {
06a2c219 8614#if 0
559cb2fb 8615 int inside_height
d9cdbb3d 8616 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8617#endif
559cb2fb 8618 int top_range
d9cdbb3d 8619 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8620
8621 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8622
8623 if (! NILP (bar->dragging))
8624 win_y -= XINT (bar->dragging);
8625
8626 if (win_y < 0)
8627 win_y = 0;
8628 if (win_y > top_range)
8629 win_y = top_range;
8630
334208b7 8631 *fp = f;
7a13e894 8632 *bar_window = bar->window;
559cb2fb
JB
8633
8634 if (! NILP (bar->dragging))
8635 *part = scroll_bar_handle;
8636 else if (win_y < XINT (bar->start))
8637 *part = scroll_bar_above_handle;
8638 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8639 *part = scroll_bar_handle;
8640 else
8641 *part = scroll_bar_below_handle;
12ba150f 8642
e0c1aef2
KH
8643 XSETINT (*x, win_y);
8644 XSETINT (*y, top_range);
12ba150f 8645
39d8bb4d 8646 f->mouse_moved = 0;
559cb2fb
JB
8647 last_mouse_scroll_bar = Qnil;
8648 }
12ba150f 8649
559cb2fb 8650 *time = last_mouse_movement_time;
cf7cb199 8651
cf7cb199 8652 UNBLOCK_INPUT;
12ba150f
JB
8653}
8654
f451eb13 8655
dbc4e1c1 8656/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8657 background colors, and the scroll bars may need to be redrawn.
8658 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8659 redraw them. */
8660
dfcf069d 8661void
ab648270 8662x_scroll_bar_clear (f)
dbc4e1c1
JB
8663 FRAME_PTR f;
8664{
06a2c219 8665#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8666 Lisp_Object bar;
8667
b80c363e
RS
8668 /* We can have scroll bars even if this is 0,
8669 if we just turned off scroll bar mode.
8670 But in that case we should not clear them. */
8671 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8672 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8673 bar = XSCROLL_BAR (bar)->next)
8674 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8675 0, 0, 0, 0, True);
06a2c219 8676#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8677}
8678
06a2c219 8679/* This processes Expose events from the menu-bar specific X event
19126e11 8680 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8681 when handling menu-bar or pop-up items. */
3afe33e7 8682
06a2c219 8683int
3afe33e7
RS
8684process_expose_from_menu (event)
8685 XEvent event;
8686{
8687 FRAME_PTR f;
19126e11 8688 struct x_display_info *dpyinfo;
06a2c219 8689 int frame_exposed_p = 0;
3afe33e7 8690
f94397b5
KH
8691 BLOCK_INPUT;
8692
19126e11
KH
8693 dpyinfo = x_display_info_for_display (event.xexpose.display);
8694 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8695 if (f)
8696 {
8697 if (f->async_visible == 0)
8698 {
8699 f->async_visible = 1;
8700 f->async_iconified = 0;
06c488fd 8701 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8702 SET_FRAME_GARBAGED (f);
8703 }
8704 else
8705 {
06a2c219
GM
8706 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8707 event.xexpose.x, event.xexpose.y,
8708 event.xexpose.width, event.xexpose.height);
8709 frame_exposed_p = 1;
3afe33e7
RS
8710 }
8711 }
8712 else
8713 {
8714 struct scroll_bar *bar
8715 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8716
3afe33e7
RS
8717 if (bar)
8718 x_scroll_bar_expose (bar, &event);
8719 }
f94397b5
KH
8720
8721 UNBLOCK_INPUT;
06a2c219 8722 return frame_exposed_p;
3afe33e7 8723}
09756a85
RS
8724\f
8725/* Define a queue to save up SelectionRequest events for later handling. */
8726
8727struct selection_event_queue
8728 {
8729 XEvent event;
8730 struct selection_event_queue *next;
8731 };
8732
8733static struct selection_event_queue *queue;
8734
8735/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8736
09756a85
RS
8737static int x_queue_selection_requests;
8738
8739/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8740
09756a85 8741static void
334208b7
RS
8742x_queue_event (f, event)
8743 FRAME_PTR f;
09756a85
RS
8744 XEvent *event;
8745{
8746 struct selection_event_queue *queue_tmp
06a2c219 8747 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8748
58769bee 8749 if (queue_tmp != NULL)
09756a85
RS
8750 {
8751 queue_tmp->event = *event;
8752 queue_tmp->next = queue;
8753 queue = queue_tmp;
8754 }
8755}
8756
8757/* Take all the queued events and put them back
8758 so that they get processed afresh. */
8759
8760static void
db3906fd
RS
8761x_unqueue_events (display)
8762 Display *display;
09756a85 8763{
58769bee 8764 while (queue != NULL)
09756a85
RS
8765 {
8766 struct selection_event_queue *queue_tmp = queue;
db3906fd 8767 XPutBackEvent (display, &queue_tmp->event);
09756a85 8768 queue = queue_tmp->next;
06a2c219 8769 xfree ((char *)queue_tmp);
09756a85
RS
8770 }
8771}
8772
8773/* Start queuing SelectionRequest events. */
8774
8775void
db3906fd
RS
8776x_start_queuing_selection_requests (display)
8777 Display *display;
09756a85
RS
8778{
8779 x_queue_selection_requests++;
8780}
8781
8782/* Stop queuing SelectionRequest events. */
8783
8784void
db3906fd
RS
8785x_stop_queuing_selection_requests (display)
8786 Display *display;
09756a85
RS
8787{
8788 x_queue_selection_requests--;
db3906fd 8789 x_unqueue_events (display);
09756a85 8790}
f451eb13
JB
8791\f
8792/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8793
06a2c219 8794/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8795 but we have to put it out here, since static variables within functions
8796 sometimes don't work. */
06a2c219 8797
dc6f92b8
JB
8798static Time enter_timestamp;
8799
11edeb03 8800/* This holds the state XLookupString needs to implement dead keys
58769bee 8801 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8802 says that a portable program can't use this, but Stephen Gildea assures
8803 me that letting the compiler initialize it to zeros will work okay.
8804
8805 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8806 given for enter_time stamp, above. */
8807
11edeb03
JB
8808static XComposeStatus compose_status;
8809
10e6549c
RS
8810/* Record the last 100 characters stored
8811 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8812
2224b905
RS
8813static int temp_index;
8814static short temp_buffer[100];
10e6549c 8815
7a13e894
RS
8816/* Set this to nonzero to fake an "X I/O error"
8817 on a particular display. */
06a2c219 8818
7a13e894
RS
8819struct x_display_info *XTread_socket_fake_io_error;
8820
2224b905
RS
8821/* When we find no input here, we occasionally do a no-op command
8822 to verify that the X server is still running and we can still talk with it.
8823 We try all the open displays, one by one.
8824 This variable is used for cycling thru the displays. */
06a2c219 8825
2224b905
RS
8826static struct x_display_info *next_noop_dpyinfo;
8827
06a2c219
GM
8828#define SET_SAVED_MENU_EVENT(size) \
8829 do \
8830 { \
8831 if (f->output_data.x->saved_menu_event == 0) \
8832 f->output_data.x->saved_menu_event \
8833 = (XEvent *) xmalloc (sizeof (XEvent)); \
8834 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8835 if (numchars >= 1) \
8836 { \
8837 bufp->kind = menu_bar_activate_event; \
8838 XSETFRAME (bufp->frame_or_window, f); \
8839 bufp++; \
8840 count++; \
8841 numchars--; \
8842 } \
8843 } \
8844 while (0)
8845
8805890a 8846#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8847#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8848
dc6f92b8
JB
8849/* Read events coming from the X server.
8850 This routine is called by the SIGIO handler.
8851 We return as soon as there are no more events to be read.
8852
8853 Events representing keys are stored in buffer BUFP,
8854 which can hold up to NUMCHARS characters.
8855 We return the number of characters stored into the buffer,
8856 thus pretending to be `read'.
8857
dc6f92b8
JB
8858 EXPECTED is nonzero if the caller knows input is available. */
8859
7c5283e4 8860int
f66868ba 8861XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8862 register int sd;
8805890a
KH
8863 /* register */ struct input_event *bufp;
8864 /* register */ int numchars;
dc6f92b8
JB
8865 int expected;
8866{
8867 int count = 0;
8868 int nbytes = 0;
dc6f92b8 8869 XEvent event;
f676886a 8870 struct frame *f;
66f55a9d 8871 int event_found = 0;
334208b7 8872 struct x_display_info *dpyinfo;
dc6f92b8 8873
9ac0d9e0 8874 if (interrupt_input_blocked)
dc6f92b8 8875 {
9ac0d9e0 8876 interrupt_input_pending = 1;
dc6f92b8
JB
8877 return -1;
8878 }
8879
9ac0d9e0 8880 interrupt_input_pending = 0;
dc6f92b8 8881 BLOCK_INPUT;
c0a04927
RS
8882
8883 /* So people can tell when we have read the available input. */
8884 input_signal_count++;
8885
dc6f92b8 8886 if (numchars <= 0)
06a2c219 8887 abort (); /* Don't think this happens. */
dc6f92b8 8888
7a13e894
RS
8889 /* Find the display we are supposed to read input for.
8890 It's the one communicating on descriptor SD. */
8891 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8892 {
8893#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8894#ifdef FIOSNBIO
7a13e894
RS
8895 /* If available, Xlib uses FIOSNBIO to make the socket
8896 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8897 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8898 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8899 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8900#endif /* ! defined (FIOSNBIO) */
7a13e894 8901#endif
dc6f92b8 8902
7a13e894
RS
8903#if 0 /* This code can't be made to work, with multiple displays,
8904 and appears not to be used on any system any more.
8905 Also keyboard.c doesn't turn O_NDELAY on and off
8906 for X connections. */
dc6f92b8
JB
8907#ifndef SIGIO
8908#ifndef HAVE_SELECT
7a13e894
RS
8909 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8910 {
8911 extern int read_alarm_should_throw;
8912 read_alarm_should_throw = 1;
8913 XPeekEvent (dpyinfo->display, &event);
8914 read_alarm_should_throw = 0;
8915 }
c118dd06
JB
8916#endif /* HAVE_SELECT */
8917#endif /* SIGIO */
7a13e894 8918#endif
dc6f92b8 8919
7a13e894
RS
8920 /* For debugging, this gives a way to fake an I/O error. */
8921 if (dpyinfo == XTread_socket_fake_io_error)
8922 {
8923 XTread_socket_fake_io_error = 0;
8924 x_io_error_quitter (dpyinfo->display);
8925 }
dc6f92b8 8926
06a2c219 8927 while (XPending (dpyinfo->display))
dc6f92b8 8928 {
7a13e894 8929 XNextEvent (dpyinfo->display, &event);
06a2c219 8930
531483fb 8931#ifdef HAVE_X_I18N
d1bc4182 8932 {
f2be1146
GM
8933 /* Filter events for the current X input method.
8934 XFilterEvent returns non-zero if the input method has
8935 consumed the event. We pass the frame's X window to
8936 XFilterEvent because that's the one for which the IC
8937 was created. */
f5d11644
GM
8938 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8939 event.xclient.window);
8940 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
8941 break;
8942 }
0cd6403b 8943#endif
7a13e894
RS
8944 event_found = 1;
8945
8946 switch (event.type)
8947 {
8948 case ClientMessage:
c047688c 8949 {
7a13e894
RS
8950 if (event.xclient.message_type
8951 == dpyinfo->Xatom_wm_protocols
8952 && event.xclient.format == 32)
c047688c 8953 {
7a13e894
RS
8954 if (event.xclient.data.l[0]
8955 == dpyinfo->Xatom_wm_take_focus)
c047688c 8956 {
8c1a6a84
RS
8957 /* Use x_any_window_to_frame because this
8958 could be the shell widget window
8959 if the frame has no title bar. */
8960 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8961#ifdef HAVE_X_I18N
8962 /* Not quite sure this is needed -pd */
8c1a6a84 8963 if (f && FRAME_XIC (f))
6c183ba5
RS
8964 XSetICFocus (FRAME_XIC (f));
8965#endif
f1da8f06
GM
8966#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8967 instructs the WM to set the input focus automatically for
8968 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8969 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8970 it has set the focus. So, XSetInputFocus below is not
8971 needed.
8972
8973 The call to XSetInputFocus below has also caused trouble. In
8974 cases where the XSetInputFocus done by the WM and the one
8975 below are temporally close (on a fast machine), the call
8976 below can generate additional FocusIn events which confuse
8977 Emacs. */
8978
bf7253f4
RS
8979 /* Since we set WM_TAKE_FOCUS, we must call
8980 XSetInputFocus explicitly. But not if f is null,
8981 since that might be an event for a deleted frame. */
7a13e894 8982 if (f)
bf7253f4
RS
8983 {
8984 Display *d = event.xclient.display;
8985 /* Catch and ignore errors, in case window has been
8986 iconified by a window manager such as GWM. */
8987 int count = x_catch_errors (d);
8988 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8989 /* The ICCCM says this is
8990 the only valid choice. */
8991 RevertToParent,
bf7253f4
RS
8992 event.xclient.data.l[1]);
8993 /* This is needed to detect the error
8994 if there is an error. */
8995 XSync (d, False);
8996 x_uncatch_errors (d, count);
8997 }
7a13e894 8998 /* Not certain about handling scroll bars here */
f1da8f06 8999#endif /* 0 */
c047688c 9000 }
7a13e894
RS
9001 else if (event.xclient.data.l[0]
9002 == dpyinfo->Xatom_wm_save_yourself)
9003 {
9004 /* Save state modify the WM_COMMAND property to
06a2c219 9005 something which can reinstate us. This notifies
7a13e894
RS
9006 the session manager, who's looking for such a
9007 PropertyNotify. Can restart processing when
06a2c219 9008 a keyboard or mouse event arrives. */
7a13e894
RS
9009 if (numchars > 0)
9010 {
19126e11
KH
9011 f = x_top_window_to_frame (dpyinfo,
9012 event.xclient.window);
7a13e894
RS
9013
9014 /* This is just so we only give real data once
9015 for a single Emacs process. */
b86bd3dd 9016 if (f == SELECTED_FRAME ())
7a13e894
RS
9017 XSetCommand (FRAME_X_DISPLAY (f),
9018 event.xclient.window,
9019 initial_argv, initial_argc);
f000f5c5 9020 else if (f)
7a13e894
RS
9021 XSetCommand (FRAME_X_DISPLAY (f),
9022 event.xclient.window,
9023 0, 0);
9024 }
9025 }
9026 else if (event.xclient.data.l[0]
9027 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9028 {
19126e11
KH
9029 struct frame *f
9030 = x_any_window_to_frame (dpyinfo,
9031 event.xclient.window);
1fb20991 9032
7a13e894
RS
9033 if (f)
9034 {
9035 if (numchars == 0)
9036 abort ();
1fb20991 9037
7a13e894
RS
9038 bufp->kind = delete_window_event;
9039 XSETFRAME (bufp->frame_or_window, f);
9040 bufp++;
9041
9042 count += 1;
9043 numchars -= 1;
9044 }
1fb20991 9045 }
c047688c 9046 }
7a13e894
RS
9047 else if (event.xclient.message_type
9048 == dpyinfo->Xatom_wm_configure_denied)
9049 {
9050 }
9051 else if (event.xclient.message_type
9052 == dpyinfo->Xatom_wm_window_moved)
9053 {
9054 int new_x, new_y;
19126e11
KH
9055 struct frame *f
9056 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9057
7a13e894
RS
9058 new_x = event.xclient.data.s[0];
9059 new_y = event.xclient.data.s[1];
1fb20991 9060
7a13e894
RS
9061 if (f)
9062 {
7556890b
RS
9063 f->output_data.x->left_pos = new_x;
9064 f->output_data.x->top_pos = new_y;
7a13e894 9065 }
1fb20991 9066 }
0fdff6bb 9067#ifdef HACK_EDITRES
7a13e894
RS
9068 else if (event.xclient.message_type
9069 == dpyinfo->Xatom_editres)
9070 {
19126e11
KH
9071 struct frame *f
9072 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9073 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9074 &event, NULL);
7a13e894 9075 }
0fdff6bb 9076#endif /* HACK_EDITRES */
06a2c219
GM
9077 else if ((event.xclient.message_type
9078 == dpyinfo->Xatom_DONE)
9079 || (event.xclient.message_type
9080 == dpyinfo->Xatom_PAGE))
9081 {
9082 /* Ghostview job completed. Kill it. We could
9083 reply with "Next" if we received "Page", but we
9084 currently never do because we are interested in
9085 images, only, which should have 1 page. */
06a2c219
GM
9086 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9087 struct frame *f
9088 = x_window_to_frame (dpyinfo, event.xclient.window);
9089 x_kill_gs_process (pixmap, f);
9090 expose_frame (f, 0, 0, 0, 0);
9091 }
9092#ifdef USE_TOOLKIT_SCROLL_BARS
9093 /* Scroll bar callbacks send a ClientMessage from which
9094 we construct an input_event. */
9095 else if (event.xclient.message_type
9096 == dpyinfo->Xatom_Scrollbar)
9097 {
9098 x_scroll_bar_to_input_event (&event, bufp);
9099 ++bufp, ++count, --numchars;
9100 goto out;
9101 }
9102#endif /* USE_TOOLKIT_SCROLL_BARS */
9103 else
9104 goto OTHER;
7a13e894
RS
9105 }
9106 break;
dc6f92b8 9107
7a13e894 9108 case SelectionNotify:
3afe33e7 9109#ifdef USE_X_TOOLKIT
19126e11 9110 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9111 goto OTHER;
3afe33e7 9112#endif /* not USE_X_TOOLKIT */
dfcf069d 9113 x_handle_selection_notify (&event.xselection);
7a13e894 9114 break;
d56a553a 9115
06a2c219 9116 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9117#ifdef USE_X_TOOLKIT
19126e11 9118 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9119 goto OTHER;
3afe33e7 9120#endif /* USE_X_TOOLKIT */
7a13e894
RS
9121 {
9122 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9123
7a13e894
RS
9124 if (numchars == 0)
9125 abort ();
d56a553a 9126
7a13e894
RS
9127 bufp->kind = selection_clear_event;
9128 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9129 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9130 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9131 bufp->frame_or_window = Qnil;
7a13e894 9132 bufp++;
d56a553a 9133
7a13e894
RS
9134 count += 1;
9135 numchars -= 1;
9136 }
9137 break;
dc6f92b8 9138
06a2c219 9139 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9140#ifdef USE_X_TOOLKIT
19126e11 9141 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9142 goto OTHER;
3afe33e7 9143#endif /* USE_X_TOOLKIT */
7a13e894 9144 if (x_queue_selection_requests)
19126e11 9145 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9146 &event);
9147 else
9148 {
9149 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9150
7a13e894
RS
9151 if (numchars == 0)
9152 abort ();
9153
9154 bufp->kind = selection_request_event;
9155 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9156 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9157 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9158 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9159 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9160 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9161 bufp->frame_or_window = Qnil;
7a13e894
RS
9162 bufp++;
9163
9164 count += 1;
9165 numchars -= 1;
9166 }
9167 break;
9168
9169 case PropertyNotify:
3afe33e7 9170#ifdef USE_X_TOOLKIT
19126e11 9171 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9172 goto OTHER;
3afe33e7 9173#endif /* not USE_X_TOOLKIT */
dfcf069d 9174 x_handle_property_notify (&event.xproperty);
7a13e894 9175 break;
dc6f92b8 9176
7a13e894 9177 case ReparentNotify:
19126e11 9178 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9179 if (f)
9180 {
9181 int x, y;
7556890b 9182 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9183 x_real_positions (f, &x, &y);
7556890b
RS
9184 f->output_data.x->left_pos = x;
9185 f->output_data.x->top_pos = y;
7a13e894
RS
9186 }
9187 break;
3bd330d4 9188
7a13e894 9189 case Expose:
19126e11 9190 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9191 if (f)
dc6f92b8 9192 {
7a13e894
RS
9193 if (f->async_visible == 0)
9194 {
9195 f->async_visible = 1;
9196 f->async_iconified = 0;
06c488fd 9197 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9198 SET_FRAME_GARBAGED (f);
9199 }
9200 else
06a2c219
GM
9201 expose_frame (x_window_to_frame (dpyinfo,
9202 event.xexpose.window),
9203 event.xexpose.x, event.xexpose.y,
9204 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9205 }
9206 else
7a13e894 9207 {
06a2c219
GM
9208#ifdef USE_TOOLKIT_SCROLL_BARS
9209 /* Dispatch event to the widget. */
9210 goto OTHER;
9211#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9212 struct scroll_bar *bar
9213 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9214
7a13e894
RS
9215 if (bar)
9216 x_scroll_bar_expose (bar, &event);
3afe33e7 9217#ifdef USE_X_TOOLKIT
7a13e894
RS
9218 else
9219 goto OTHER;
3afe33e7 9220#endif /* USE_X_TOOLKIT */
06a2c219 9221#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9222 }
9223 break;
dc6f92b8 9224
7a13e894
RS
9225 case GraphicsExpose: /* This occurs when an XCopyArea's
9226 source area was obscured or not
9227 available.*/
19126e11 9228 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9229 if (f)
9230 {
06a2c219
GM
9231 expose_frame (f,
9232 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9233 event.xgraphicsexpose.width,
9234 event.xgraphicsexpose.height);
7a13e894 9235 }
3afe33e7 9236#ifdef USE_X_TOOLKIT
7a13e894
RS
9237 else
9238 goto OTHER;
3afe33e7 9239#endif /* USE_X_TOOLKIT */
7a13e894 9240 break;
dc6f92b8 9241
7a13e894 9242 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9243 source area was completely
9244 available */
7a13e894 9245 break;
dc6f92b8 9246
7a13e894 9247 case UnmapNotify:
06a2c219
GM
9248 /* Redo the mouse-highlight after the tooltip has gone. */
9249 if (event.xmap.window == tip_window)
9250 {
9251 tip_window = 0;
9252 redo_mouse_highlight ();
9253 }
9254
91ea2a7a 9255 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9256 if (f) /* F may no longer exist if
9257 the frame was deleted. */
9258 {
9259 /* While a frame is unmapped, display generation is
9260 disabled; you don't want to spend time updating a
9261 display that won't ever be seen. */
9262 f->async_visible = 0;
9263 /* We can't distinguish, from the event, whether the window
9264 has become iconified or invisible. So assume, if it
9265 was previously visible, than now it is iconified.
1aa6072f
RS
9266 But x_make_frame_invisible clears both
9267 the visible flag and the iconified flag;
9268 and that way, we know the window is not iconified now. */
7a13e894 9269 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9270 {
9271 f->async_iconified = 1;
bddd097c 9272
1aa6072f
RS
9273 bufp->kind = iconify_event;
9274 XSETFRAME (bufp->frame_or_window, f);
9275 bufp++;
9276 count++;
9277 numchars--;
9278 }
7a13e894 9279 }
7a13e894 9280 goto OTHER;
dc6f92b8 9281
7a13e894 9282 case MapNotify:
06a2c219
GM
9283 if (event.xmap.window == tip_window)
9284 /* The tooltip has been drawn already. Avoid
9285 the SET_FRAME_GARBAGED below. */
9286 goto OTHER;
9287
9288 /* We use x_top_window_to_frame because map events can
9289 come for sub-windows and they don't mean that the
9290 frame is visible. */
19126e11 9291 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9292 if (f)
9293 {
9294 f->async_visible = 1;
9295 f->async_iconified = 0;
06c488fd 9296 f->output_data.x->has_been_visible = 1;
dc6f92b8 9297
7a13e894
RS
9298 /* wait_reading_process_input will notice this and update
9299 the frame's display structures. */
9300 SET_FRAME_GARBAGED (f);
bddd097c 9301
d806e720
RS
9302 if (f->iconified)
9303 {
9304 bufp->kind = deiconify_event;
9305 XSETFRAME (bufp->frame_or_window, f);
9306 bufp++;
9307 count++;
9308 numchars--;
9309 }
e73ec6fa 9310 else if (! NILP (Vframe_list)
8e713be6 9311 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9312 /* Force a redisplay sooner or later
9313 to update the frame titles
9314 in case this is the second frame. */
9315 record_asynch_buffer_change ();
7a13e894 9316 }
7a13e894 9317 goto OTHER;
dc6f92b8 9318
7a13e894 9319 case KeyPress:
19126e11 9320 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9321
06a2c219
GM
9322#ifdef USE_MOTIF
9323 /* I couldn't find a way to prevent LessTif scroll bars
9324 from consuming key events. */
9325 if (f == 0)
9326 {
9327 Widget widget = XtWindowToWidget (dpyinfo->display,
9328 event.xkey.window);
9329 if (widget && XmIsScrollBar (widget))
9330 {
9331 widget = XtParent (widget);
9332 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9333 }
9334 }
9335#endif /* USE_MOTIF */
9336
7a13e894
RS
9337 if (f != 0)
9338 {
9339 KeySym keysym, orig_keysym;
9340 /* al%imercury@uunet.uu.net says that making this 81 instead of
9341 80 fixed a bug whereby meta chars made his Emacs hang. */
9342 unsigned char copy_buffer[81];
9343 int modifiers;
64bb1782 9344
7a13e894
RS
9345 event.xkey.state
9346 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9347 extra_keyboard_modifiers);
9348 modifiers = event.xkey.state;
3a2712f9 9349
7a13e894 9350 /* This will have to go some day... */
752a043f 9351
7a13e894
RS
9352 /* make_lispy_event turns chars into control chars.
9353 Don't do it here because XLookupString is too eager. */
9354 event.xkey.state &= ~ControlMask;
5d46f928
RS
9355 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9356 | dpyinfo->super_mod_mask
9357 | dpyinfo->hyper_mod_mask
9358 | dpyinfo->alt_mod_mask);
9359
1cf4a0d1
RS
9360 /* In case Meta is ComposeCharacter,
9361 clear its status. According to Markus Ehrnsperger
9362 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9363 this enables ComposeCharacter to work whether or
9364 not it is combined with Meta. */
9365 if (modifiers & dpyinfo->meta_mod_mask)
9366 bzero (&compose_status, sizeof (compose_status));
9367
6c183ba5
RS
9368#ifdef HAVE_X_I18N
9369 if (FRAME_XIC (f))
9370 {
f5d11644
GM
9371 unsigned char *copy_bufptr = copy_buffer;
9372 int copy_bufsiz = sizeof (copy_buffer);
9373 Status status_return;
9374
6c183ba5 9375 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9376 &event.xkey, copy_bufptr,
9377 copy_bufsiz, &keysym,
6c183ba5 9378 &status_return);
f5d11644
GM
9379 if (status_return == XBufferOverflow)
9380 {
9381 copy_bufsiz = nbytes + 1;
9382 copy_bufptr = (char *) alloca (copy_bufsiz);
9383 nbytes = XmbLookupString (FRAME_XIC (f),
9384 &event.xkey, copy_bufptr,
9385 copy_bufsiz, &keysym,
9386 &status_return);
9387 }
9388
1decb680
PE
9389 if (status_return == XLookupNone)
9390 break;
9391 else if (status_return == XLookupChars)
9392 keysym = NoSymbol;
9393 else if (status_return != XLookupKeySym
9394 && status_return != XLookupBoth)
9395 abort ();
6c183ba5
RS
9396 }
9397 else
9398 nbytes = XLookupString (&event.xkey, copy_buffer,
9399 80, &keysym, &compose_status);
9400#else
0299d313
RS
9401 nbytes = XLookupString (&event.xkey, copy_buffer,
9402 80, &keysym, &compose_status);
6c183ba5 9403#endif
dc6f92b8 9404
7a13e894 9405 orig_keysym = keysym;
55123275 9406
7a13e894
RS
9407 if (numchars > 1)
9408 {
9409 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9410 || keysym == XK_Delete
1097aea0 9411#ifdef XK_ISO_Left_Tab
441affdb 9412 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9413#endif
852bff8f 9414 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9415 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9416 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9417#ifdef HPUX
7a13e894
RS
9418 /* This recognizes the "extended function keys".
9419 It seems there's no cleaner way.
9420 Test IsModifierKey to avoid handling mode_switch
9421 incorrectly. */
9422 || ((unsigned) (keysym) >= XK_Select
9423 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9424#endif
9425#ifdef XK_dead_circumflex
7a13e894 9426 || orig_keysym == XK_dead_circumflex
69388238
RS
9427#endif
9428#ifdef XK_dead_grave
7a13e894 9429 || orig_keysym == XK_dead_grave
69388238
RS
9430#endif
9431#ifdef XK_dead_tilde
7a13e894 9432 || orig_keysym == XK_dead_tilde
69388238
RS
9433#endif
9434#ifdef XK_dead_diaeresis
7a13e894 9435 || orig_keysym == XK_dead_diaeresis
69388238
RS
9436#endif
9437#ifdef XK_dead_macron
7a13e894 9438 || orig_keysym == XK_dead_macron
69388238
RS
9439#endif
9440#ifdef XK_dead_degree
7a13e894 9441 || orig_keysym == XK_dead_degree
69388238
RS
9442#endif
9443#ifdef XK_dead_acute
7a13e894 9444 || orig_keysym == XK_dead_acute
69388238
RS
9445#endif
9446#ifdef XK_dead_cedilla
7a13e894 9447 || orig_keysym == XK_dead_cedilla
69388238
RS
9448#endif
9449#ifdef XK_dead_breve
7a13e894 9450 || orig_keysym == XK_dead_breve
69388238
RS
9451#endif
9452#ifdef XK_dead_ogonek
7a13e894 9453 || orig_keysym == XK_dead_ogonek
69388238
RS
9454#endif
9455#ifdef XK_dead_caron
7a13e894 9456 || orig_keysym == XK_dead_caron
69388238
RS
9457#endif
9458#ifdef XK_dead_doubleacute
7a13e894 9459 || orig_keysym == XK_dead_doubleacute
69388238
RS
9460#endif
9461#ifdef XK_dead_abovedot
7a13e894 9462 || orig_keysym == XK_dead_abovedot
c34790e0 9463#endif
7a13e894
RS
9464 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9465 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9466 /* Any "vendor-specific" key is ok. */
9467 || (orig_keysym & (1 << 28)))
9468 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9469#ifndef HAVE_X11R5
9470#ifdef XK_Mode_switch
7a13e894 9471 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9472#endif
9473#ifdef XK_Num_Lock
7a13e894 9474 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9475#endif
9476#endif /* not HAVE_X11R5 */
7a13e894 9477 ))
dc6f92b8 9478 {
10e6549c
RS
9479 if (temp_index == sizeof temp_buffer / sizeof (short))
9480 temp_index = 0;
7a13e894
RS
9481 temp_buffer[temp_index++] = keysym;
9482 bufp->kind = non_ascii_keystroke;
9483 bufp->code = keysym;
e0c1aef2 9484 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9485 bufp->modifiers
9486 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9487 modifiers);
1113d9db 9488 bufp->timestamp = event.xkey.time;
dc6f92b8 9489 bufp++;
7a13e894
RS
9490 count++;
9491 numchars--;
dc6f92b8 9492 }
7a13e894
RS
9493 else if (numchars > nbytes)
9494 {
9495 register int i;
9496
9497 for (i = 0; i < nbytes; i++)
9498 {
9499 if (temp_index == sizeof temp_buffer / sizeof (short))
9500 temp_index = 0;
9501 temp_buffer[temp_index++] = copy_buffer[i];
9502 bufp->kind = ascii_keystroke;
9503 bufp->code = copy_buffer[i];
9504 XSETFRAME (bufp->frame_or_window, f);
9505 bufp->modifiers
9506 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9507 modifiers);
9508 bufp->timestamp = event.xkey.time;
9509 bufp++;
9510 }
9511
9512 count += nbytes;
9513 numchars -= nbytes;
1decb680
PE
9514
9515 if (keysym == NoSymbol)
9516 break;
7a13e894
RS
9517 }
9518 else
9519 abort ();
dc6f92b8 9520 }
10e6549c
RS
9521 else
9522 abort ();
dc6f92b8 9523 }
59ddecde
GM
9524#ifdef HAVE_X_I18N
9525 /* Don't dispatch this event since XtDispatchEvent calls
9526 XFilterEvent, and two calls in a row may freeze the
9527 client. */
9528 break;
9529#else
717ca130 9530 goto OTHER;
59ddecde 9531#endif
f451eb13 9532
f5d11644 9533 case KeyRelease:
59ddecde
GM
9534#ifdef HAVE_X_I18N
9535 /* Don't dispatch this event since XtDispatchEvent calls
9536 XFilterEvent, and two calls in a row may freeze the
9537 client. */
9538 break;
9539#else
f5d11644 9540 goto OTHER;
59ddecde 9541#endif
f5d11644 9542
7a13e894 9543 /* Here's a possible interpretation of the whole
06a2c219
GM
9544 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9545 you get a FocusIn event, you have to get a FocusOut
9546 event before you relinquish the focus. If you
9547 haven't received a FocusIn event, then a mere
9548 LeaveNotify is enough to free you. */
f451eb13 9549
7a13e894 9550 case EnterNotify:
06a2c219
GM
9551 {
9552 int from_menu_bar_p = 0;
9553
9554 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9555
9556#ifdef LESSTIF_VERSION
9557 /* When clicking outside of a menu bar popup to close
9558 it, we get a FocusIn/ EnterNotify sequence of
9559 events. The flag event.xcrossing.focus is not set
9560 in the EnterNotify event of that sequence because
9561 the focus is in the menu bar,
9562 event.xcrossing.window is the frame's X window.
9563 Unconditionally setting the focus frame to null in
9564 this case is not the right thing, because no event
9565 follows that could set the focus frame to the right
9566 value.
9567
9568 This could be a LessTif bug, but I wasn't able to
9569 reproduce the behavior in a simple test program.
9570
9571 (gerd, LessTif 0.88.1). */
9572
9573 if (!event.xcrossing.focus
9574 && f
9575 && f->output_data.x->menubar_widget)
9576 {
9577 Window focus;
9578 int revert;
9579
9580 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9581 if (focus == XtWindow (f->output_data.x->menubar_widget))
9582 from_menu_bar_p = 1;
9583 }
9584#endif /* LESSTIF_VERSION */
6d4238f3 9585
06a2c219
GM
9586 if (event.xcrossing.focus || from_menu_bar_p)
9587 {
9588 /* Avoid nasty pop/raise loops. */
9589 if (f && (!(f->auto_raise)
9590 || !(f->auto_lower)
9591 || (event.xcrossing.time - enter_timestamp) > 500))
9592 {
9593 x_new_focus_frame (dpyinfo, f);
9594 enter_timestamp = event.xcrossing.time;
9595 }
9596 }
9597 else if (f == dpyinfo->x_focus_frame)
9598 x_new_focus_frame (dpyinfo, 0);
9599
9600 /* EnterNotify counts as mouse movement,
9601 so update things that depend on mouse position. */
9602 if (f && !f->output_data.x->busy_p)
9603 note_mouse_movement (f, &event.xmotion);
9604 goto OTHER;
9605 }
dc6f92b8 9606
7a13e894 9607 case FocusIn:
19126e11 9608 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9609 if (event.xfocus.detail != NotifyPointer)
0f941935 9610 dpyinfo->x_focus_event_frame = f;
7a13e894 9611 if (f)
eb72635f
GM
9612 {
9613 x_new_focus_frame (dpyinfo, f);
9614
9615 /* Don't stop displaying the initial startup message
9616 for a switch-frame event we don't need. */
9617 if (GC_NILP (Vterminal_frame)
9618 && GC_CONSP (Vframe_list)
9619 && !GC_NILP (XCDR (Vframe_list)))
9620 {
9621 bufp->kind = FOCUS_IN_EVENT;
9622 XSETFRAME (bufp->frame_or_window, f);
9623 ++bufp, ++count, --numchars;
9624 }
9625 }
f9e24cb9 9626
6c183ba5
RS
9627#ifdef HAVE_X_I18N
9628 if (f && FRAME_XIC (f))
9629 XSetICFocus (FRAME_XIC (f));
9630#endif
9631
7a13e894 9632 goto OTHER;
10c5e63d 9633
7a13e894 9634 case LeaveNotify:
19126e11 9635 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9636 if (f)
10c5e63d 9637 {
06a2c219
GM
9638 Lisp_Object frame;
9639 int from_menu_bar_p = 0;
9640
7a13e894 9641 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9642 {
9643 /* If we move outside the frame, then we're
9644 certainly no longer on any text in the frame. */
9645 clear_mouse_face (dpyinfo);
9646 dpyinfo->mouse_face_mouse_frame = 0;
9647 }
9648
9649 /* Generate a nil HELP_EVENT to cancel a help-echo.
9650 Do it only if there's something to cancel.
9651 Otherwise, the startup message is cleared when
9652 the mouse leaves the frame. */
9653 if (any_help_event_p)
9654 {
9655 XSETFRAME (frame, f);
9656 bufp->kind = HELP_EVENT;
9657 bufp->frame_or_window = Fcons (frame, Qnil);
9658 ++bufp, ++count, --numchars;
9659 }
7a13e894 9660
06a2c219
GM
9661#ifdef LESSTIF_VERSION
9662 /* Please see the comment at the start of the
9663 EnterNotify case. */
9664 if (!event.xcrossing.focus
9665 && f->output_data.x->menubar_widget)
9666 {
9667 Window focus;
9668 int revert;
9669 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9670 if (focus == XtWindow (f->output_data.x->menubar_widget))
9671 from_menu_bar_p = 1;
9672 }
9673#endif /* LESSTIF_VERSION */
9674
9675 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9676 x_mouse_leave (dpyinfo);
10c5e63d 9677 else
7a13e894 9678 {
0f941935
KH
9679 if (f == dpyinfo->x_focus_event_frame)
9680 dpyinfo->x_focus_event_frame = 0;
9681 if (f == dpyinfo->x_focus_frame)
9682 x_new_focus_frame (dpyinfo, 0);
7a13e894 9683 }
10c5e63d 9684 }
7a13e894 9685 goto OTHER;
dc6f92b8 9686
7a13e894 9687 case FocusOut:
19126e11 9688 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9689 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9690 && f == dpyinfo->x_focus_event_frame)
9691 dpyinfo->x_focus_event_frame = 0;
9692 if (f && f == dpyinfo->x_focus_frame)
9693 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9694
6c183ba5
RS
9695#ifdef HAVE_X_I18N
9696 if (f && FRAME_XIC (f))
9697 XUnsetICFocus (FRAME_XIC (f));
9698#endif
9699
7a13e894 9700 goto OTHER;
dc6f92b8 9701
7a13e894 9702 case MotionNotify:
dc6f92b8 9703 {
06a2c219
GM
9704 previous_help_echo = help_echo;
9705 help_echo = Qnil;
9706
7a13e894
RS
9707 if (dpyinfo->grabbed && last_mouse_frame
9708 && FRAME_LIVE_P (last_mouse_frame))
9709 f = last_mouse_frame;
9710 else
19126e11 9711 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9712
7a13e894
RS
9713 if (f)
9714 note_mouse_movement (f, &event.xmotion);
9715 else
9716 {
e88b3c50 9717#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9718 struct scroll_bar *bar
9719 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9720
7a13e894
RS
9721 if (bar)
9722 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9723#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9724
06a2c219
GM
9725 /* If we move outside the frame, then we're
9726 certainly no longer on any text in the frame. */
7a13e894
RS
9727 clear_mouse_face (dpyinfo);
9728 }
06a2c219
GM
9729
9730 /* If the contents of the global variable help_echo
9731 has changed, generate a HELP_EVENT. */
9732 if (STRINGP (help_echo)
9733 || STRINGP (previous_help_echo))
9734 {
9735 Lisp_Object frame;
9736
9737 if (f)
9738 XSETFRAME (frame, f);
9739 else
9740 frame = Qnil;
9741
9742 any_help_event_p = 1;
9743 bufp->kind = HELP_EVENT;
9744 bufp->frame_or_window = Fcons (frame, help_echo);
9745 ++bufp, ++count, --numchars;
9746 }
9747
9748 goto OTHER;
dc6f92b8 9749 }
dc6f92b8 9750
7a13e894 9751 case ConfigureNotify:
9829ddba
RS
9752 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9753 if (f)
af395ec1 9754 {
5c187dee 9755#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9756 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9757 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9758
2d7fc7e8
RS
9759 /* In the toolkit version, change_frame_size
9760 is called by the code that handles resizing
9761 of the EmacsFrame widget. */
7a13e894 9762
7a13e894
RS
9763 /* Even if the number of character rows and columns has
9764 not changed, the font size may have changed, so we need
9765 to check the pixel dimensions as well. */
9766 if (columns != f->width
9767 || rows != f->height
7556890b
RS
9768 || event.xconfigure.width != f->output_data.x->pixel_width
9769 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9770 {
7d1e984f 9771 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9772 SET_FRAME_GARBAGED (f);
e687d06e 9773 cancel_mouse_face (f);
7a13e894 9774 }
2d7fc7e8 9775#endif
af395ec1 9776
7556890b
RS
9777 f->output_data.x->pixel_width = event.xconfigure.width;
9778 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9779
9780 /* What we have now is the position of Emacs's own window.
9781 Convert that to the position of the window manager window. */
dcb07ae9
RS
9782 x_real_positions (f, &f->output_data.x->left_pos,
9783 &f->output_data.x->top_pos);
9784
f5d11644
GM
9785#ifdef HAVE_X_I18N
9786 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9787 xic_set_statusarea (f);
9788#endif
9789
dcb07ae9
RS
9790 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9791 {
9792 /* Since the WM decorations come below top_pos now,
9793 we must put them below top_pos in the future. */
9794 f->output_data.x->win_gravity = NorthWestGravity;
9795 x_wm_set_size_hint (f, (long) 0, 0);
9796 }
8f08dc93
KH
9797#ifdef USE_MOTIF
9798 /* Some window managers pass (0,0) as the location of
9799 the window, and the Motif event handler stores it
9800 in the emacs widget, which messes up Motif menus. */
9801 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9802 {
9803 event.xconfigure.x = f->output_data.x->widget->core.x;
9804 event.xconfigure.y = f->output_data.x->widget->core.y;
9805 }
06a2c219 9806#endif /* USE_MOTIF */
7a13e894 9807 }
2d7fc7e8 9808 goto OTHER;
dc6f92b8 9809
7a13e894
RS
9810 case ButtonPress:
9811 case ButtonRelease:
9812 {
9813 /* If we decide we want to generate an event to be seen
9814 by the rest of Emacs, we put it here. */
9815 struct input_event emacs_event;
9ea173e8 9816 int tool_bar_p = 0;
06a2c219 9817
7a13e894 9818 emacs_event.kind = no_event;
7a13e894 9819 bzero (&compose_status, sizeof (compose_status));
9b07615b 9820
06a2c219
GM
9821 if (dpyinfo->grabbed
9822 && last_mouse_frame
9f67f20b
RS
9823 && FRAME_LIVE_P (last_mouse_frame))
9824 f = last_mouse_frame;
9825 else
2224b905 9826 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9827
06a2c219
GM
9828 if (f)
9829 {
9ea173e8
GM
9830 /* Is this in the tool-bar? */
9831 if (WINDOWP (f->tool_bar_window)
9832 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9833 {
9834 Lisp_Object window;
9835 int p, x, y;
9836
9837 x = event.xbutton.x;
9838 y = event.xbutton.y;
9839
9840 /* Set x and y. */
9841 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9842 if (EQ (window, f->tool_bar_window))
06a2c219 9843 {
9ea173e8
GM
9844 x_handle_tool_bar_click (f, &event.xbutton);
9845 tool_bar_p = 1;
06a2c219
GM
9846 }
9847 }
9848
9ea173e8 9849 if (!tool_bar_p)
06a2c219
GM
9850 if (!dpyinfo->x_focus_frame
9851 || f == dpyinfo->x_focus_frame)
9852 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9853 }
9854 else
9855 {
06a2c219 9856#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9857 struct scroll_bar *bar
9858 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9859
7a13e894
RS
9860 if (bar)
9861 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9862#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9863 }
9864
9865 if (event.type == ButtonPress)
9866 {
9867 dpyinfo->grabbed |= (1 << event.xbutton.button);
9868 last_mouse_frame = f;
edad46f6
KH
9869 /* Ignore any mouse motion that happened
9870 before this event; any subsequent mouse-movement
9871 Emacs events should reflect only motion after
9872 the ButtonPress. */
a00e91cd
KH
9873 if (f != 0)
9874 f->mouse_moved = 0;
06a2c219 9875
9ea173e8
GM
9876 if (!tool_bar_p)
9877 last_tool_bar_item = -1;
7a13e894 9878 }
3afe33e7
RS
9879 else
9880 {
7a13e894 9881 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9882 }
23faf38f 9883
7a13e894
RS
9884 if (numchars >= 1 && emacs_event.kind != no_event)
9885 {
9886 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9887 bufp++;
9888 count++;
9889 numchars--;
9890 }
3afe33e7
RS
9891
9892#ifdef USE_X_TOOLKIT
2224b905
RS
9893 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9894 /* For a down-event in the menu bar,
9895 don't pass it to Xt right now.
9896 Instead, save it away
9897 and we will pass it to Xt from kbd_buffer_get_event.
9898 That way, we can run some Lisp code first. */
91375f8f
RS
9899 if (f && event.type == ButtonPress
9900 /* Verify the event is really within the menu bar
9901 and not just sent to it due to grabbing. */
9902 && event.xbutton.x >= 0
9903 && event.xbutton.x < f->output_data.x->pixel_width
9904 && event.xbutton.y >= 0
9905 && event.xbutton.y < f->output_data.x->menubar_height
9906 && event.xbutton.same_screen)
2224b905 9907 {
8805890a 9908 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9909 XSETFRAME (last_mouse_press_frame, f);
9910 }
9911 else if (event.type == ButtonPress)
9912 {
9913 last_mouse_press_frame = Qnil;
30e671c3 9914 goto OTHER;
ce89ef46 9915 }
06a2c219 9916
2237cac9
RS
9917#ifdef USE_MOTIF /* This should do not harm for Lucid,
9918 but I am trying to be cautious. */
ce89ef46
RS
9919 else if (event.type == ButtonRelease)
9920 {
2237cac9 9921 if (!NILP (last_mouse_press_frame))
f10ded1c 9922 {
2237cac9
RS
9923 f = XFRAME (last_mouse_press_frame);
9924 if (f->output_data.x)
06a2c219 9925 SET_SAVED_BUTTON_EVENT;
f10ded1c 9926 }
06a2c219 9927 else
30e671c3 9928 goto OTHER;
2224b905 9929 }
2237cac9 9930#endif /* USE_MOTIF */
2224b905
RS
9931 else
9932 goto OTHER;
3afe33e7 9933#endif /* USE_X_TOOLKIT */
7a13e894
RS
9934 }
9935 break;
dc6f92b8 9936
7a13e894 9937 case CirculateNotify:
06a2c219
GM
9938 goto OTHER;
9939
7a13e894 9940 case CirculateRequest:
06a2c219
GM
9941 goto OTHER;
9942
9943 case VisibilityNotify:
9944 goto OTHER;
dc6f92b8 9945
7a13e894
RS
9946 case MappingNotify:
9947 /* Someone has changed the keyboard mapping - update the
9948 local cache. */
9949 switch (event.xmapping.request)
9950 {
9951 case MappingModifier:
9952 x_find_modifier_meanings (dpyinfo);
9953 /* This is meant to fall through. */
9954 case MappingKeyboard:
9955 XRefreshKeyboardMapping (&event.xmapping);
9956 }
7a13e894 9957 goto OTHER;
dc6f92b8 9958
7a13e894 9959 default:
7a13e894 9960 OTHER:
717ca130 9961#ifdef USE_X_TOOLKIT
7a13e894
RS
9962 BLOCK_INPUT;
9963 XtDispatchEvent (&event);
9964 UNBLOCK_INPUT;
3afe33e7 9965#endif /* USE_X_TOOLKIT */
7a13e894
RS
9966 break;
9967 }
dc6f92b8
JB
9968 }
9969 }
9970
06a2c219
GM
9971 out:;
9972
9a5196d0
RS
9973 /* On some systems, an X bug causes Emacs to get no more events
9974 when the window is destroyed. Detect that. (1994.) */
58769bee 9975 if (! event_found)
ef2a22d0 9976 {
ef2a22d0
RS
9977 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9978 One XNOOP in 100 loops will make Emacs terminate.
9979 B. Bretthauer, 1994 */
9980 x_noop_count++;
58769bee 9981 if (x_noop_count >= 100)
ef2a22d0
RS
9982 {
9983 x_noop_count=0;
2224b905
RS
9984
9985 if (next_noop_dpyinfo == 0)
9986 next_noop_dpyinfo = x_display_list;
9987
9988 XNoOp (next_noop_dpyinfo->display);
9989
9990 /* Each time we get here, cycle through the displays now open. */
9991 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9992 }
9993 }
502add23 9994
06a2c219 9995 /* If the focus was just given to an auto-raising frame,
0134a210 9996 raise it now. */
7a13e894 9997 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9998 if (pending_autoraise_frame)
9999 {
10000 x_raise_frame (pending_autoraise_frame);
10001 pending_autoraise_frame = 0;
10002 }
0134a210 10003
dc6f92b8
JB
10004 UNBLOCK_INPUT;
10005 return count;
10006}
06a2c219
GM
10007
10008
10009
dc6f92b8 10010\f
06a2c219
GM
10011/***********************************************************************
10012 Text Cursor
10013 ***********************************************************************/
10014
10015/* Note if the text cursor of window W has been overwritten by a
10016 drawing operation that outputs N glyphs starting at HPOS in the
10017 line given by output_cursor.vpos. N < 0 means all the rest of the
10018 line after HPOS has been written. */
10019
10020static void
10021note_overwritten_text_cursor (w, hpos, n)
10022 struct window *w;
10023 int hpos, n;
10024{
10025 if (updated_area == TEXT_AREA
10026 && output_cursor.vpos == w->phys_cursor.vpos
10027 && hpos <= w->phys_cursor.hpos
10028 && (n < 0
10029 || hpos + n > w->phys_cursor.hpos))
10030 w->phys_cursor_on_p = 0;
10031}
f451eb13
JB
10032
10033
06a2c219
GM
10034/* Set clipping for output in glyph row ROW. W is the window in which
10035 we operate. GC is the graphics context to set clipping in.
10036 WHOLE_LINE_P non-zero means include the areas used for truncation
10037 mark display and alike in the clipping rectangle.
10038
10039 ROW may be a text row or, e.g., a mode line. Text rows must be
10040 clipped to the interior of the window dedicated to text display,
10041 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10042
10043static void
06a2c219
GM
10044x_clip_to_row (w, row, gc, whole_line_p)
10045 struct window *w;
10046 struct glyph_row *row;
10047 GC gc;
10048 int whole_line_p;
dc6f92b8 10049{
06a2c219
GM
10050 struct frame *f = XFRAME (WINDOW_FRAME (w));
10051 XRectangle clip_rect;
10052 int window_x, window_y, window_width, window_height;
dc6f92b8 10053
06a2c219 10054 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10055
06a2c219
GM
10056 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10057 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10058 clip_rect.y = max (clip_rect.y, window_y);
10059 clip_rect.width = window_width;
10060 clip_rect.height = row->visible_height;
5c1aae96 10061
06a2c219
GM
10062 /* If clipping to the whole line, including trunc marks, extend
10063 the rectangle to the left and increase its width. */
10064 if (whole_line_p)
10065 {
110859fc
GM
10066 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10067 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10068 }
5c1aae96 10069
06a2c219 10070 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10071}
10072
06a2c219
GM
10073
10074/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10075
10076static void
06a2c219
GM
10077x_draw_hollow_cursor (w, row)
10078 struct window *w;
10079 struct glyph_row *row;
dc6f92b8 10080{
06a2c219
GM
10081 struct frame *f = XFRAME (WINDOW_FRAME (w));
10082 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10083 Display *dpy = FRAME_X_DISPLAY (f);
10084 int x, y, wd, h;
10085 XGCValues xgcv;
10086 struct glyph *cursor_glyph;
10087 GC gc;
10088
10089 /* Compute frame-relative coordinates from window-relative
10090 coordinates. */
10091 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10092 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10093 + row->ascent - w->phys_cursor_ascent);
10094 h = row->height - 1;
10095
10096 /* Get the glyph the cursor is on. If we can't tell because
10097 the current matrix is invalid or such, give up. */
10098 cursor_glyph = get_phys_cursor_glyph (w);
10099 if (cursor_glyph == NULL)
dc6f92b8
JB
10100 return;
10101
06a2c219
GM
10102 /* Compute the width of the rectangle to draw. If on a stretch
10103 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10104 rectangle as wide as the glyph, but use a canonical character
10105 width instead. */
10106 wd = cursor_glyph->pixel_width - 1;
10107 if (cursor_glyph->type == STRETCH_GLYPH
10108 && !x_stretch_cursor_p)
10109 wd = min (CANON_X_UNIT (f), wd);
10110
10111 /* The foreground of cursor_gc is typically the same as the normal
10112 background color, which can cause the cursor box to be invisible. */
10113 xgcv.foreground = f->output_data.x->cursor_pixel;
10114 if (dpyinfo->scratch_cursor_gc)
10115 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10116 else
10117 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10118 GCForeground, &xgcv);
10119 gc = dpyinfo->scratch_cursor_gc;
10120
10121 /* Set clipping, draw the rectangle, and reset clipping again. */
10122 x_clip_to_row (w, row, gc, 0);
10123 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10124 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10125}
10126
06a2c219
GM
10127
10128/* Draw a bar cursor on window W in glyph row ROW.
10129
10130 Implementation note: One would like to draw a bar cursor with an
10131 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10132 Unfortunately, I didn't find a font yet that has this property set.
10133 --gerd. */
dc6f92b8
JB
10134
10135static void
f02d8aa0 10136x_draw_bar_cursor (w, row, width)
06a2c219
GM
10137 struct window *w;
10138 struct glyph_row *row;
f02d8aa0 10139 int width;
dc6f92b8 10140{
06a2c219
GM
10141 /* If cursor hpos is out of bounds, don't draw garbage. This can
10142 happen in mini-buffer windows when switching between echo area
10143 glyphs and mini-buffer. */
10144 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10145 {
10146 struct frame *f = XFRAME (w->frame);
10147 struct glyph *cursor_glyph;
10148 GC gc;
10149 int x;
10150 unsigned long mask;
10151 XGCValues xgcv;
10152 Display *dpy;
10153 Window window;
10154
10155 cursor_glyph = get_phys_cursor_glyph (w);
10156 if (cursor_glyph == NULL)
10157 return;
10158
10159 xgcv.background = f->output_data.x->cursor_pixel;
10160 xgcv.foreground = f->output_data.x->cursor_pixel;
10161 xgcv.graphics_exposures = 0;
10162 mask = GCForeground | GCBackground | GCGraphicsExposures;
10163 dpy = FRAME_X_DISPLAY (f);
10164 window = FRAME_X_WINDOW (f);
10165 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10166
10167 if (gc)
10168 XChangeGC (dpy, gc, mask, &xgcv);
10169 else
10170 {
10171 gc = XCreateGC (dpy, window, mask, &xgcv);
10172 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10173 }
10174
f02d8aa0
GM
10175 if (width < 0)
10176 width = f->output_data.x->cursor_width;
10177
06a2c219
GM
10178 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10179 x_clip_to_row (w, row, gc, 0);
10180 XFillRectangle (dpy, window, gc,
10181 x,
10182 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10183 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10184 row->height);
10185 XSetClipMask (dpy, gc, None);
10186 }
dc6f92b8
JB
10187}
10188
06a2c219
GM
10189
10190/* Clear the cursor of window W to background color, and mark the
10191 cursor as not shown. This is used when the text where the cursor
10192 is is about to be rewritten. */
10193
dc6f92b8 10194static void
06a2c219
GM
10195x_clear_cursor (w)
10196 struct window *w;
dc6f92b8 10197{
06a2c219
GM
10198 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10199 x_update_window_cursor (w, 0);
10200}
90e65f07 10201
dbc4e1c1 10202
06a2c219
GM
10203/* Draw the cursor glyph of window W in glyph row ROW. See the
10204 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10205
06a2c219
GM
10206static void
10207x_draw_phys_cursor_glyph (w, row, hl)
10208 struct window *w;
10209 struct glyph_row *row;
10210 enum draw_glyphs_face hl;
10211{
10212 /* If cursor hpos is out of bounds, don't draw garbage. This can
10213 happen in mini-buffer windows when switching between echo area
10214 glyphs and mini-buffer. */
10215 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10216 {
10217 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10218 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10219 hl, 0, 0, 0);
10220
10221 /* When we erase the cursor, and ROW is overlapped by other
10222 rows, make sure that these overlapping parts of other rows
10223 are redrawn. */
10224 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10225 {
10226 if (row > w->current_matrix->rows
10227 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10228 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10229
10230 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10231 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10232 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10233 }
10234 }
06a2c219 10235}
dbc4e1c1 10236
eea6af04 10237
06a2c219 10238/* Erase the image of a cursor of window W from the screen. */
eea6af04 10239
06a2c219
GM
10240static void
10241x_erase_phys_cursor (w)
10242 struct window *w;
10243{
10244 struct frame *f = XFRAME (w->frame);
10245 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10246 int hpos = w->phys_cursor.hpos;
10247 int vpos = w->phys_cursor.vpos;
10248 int mouse_face_here_p = 0;
10249 struct glyph_matrix *active_glyphs = w->current_matrix;
10250 struct glyph_row *cursor_row;
10251 struct glyph *cursor_glyph;
10252 enum draw_glyphs_face hl;
10253
10254 /* No cursor displayed or row invalidated => nothing to do on the
10255 screen. */
10256 if (w->phys_cursor_type == NO_CURSOR)
10257 goto mark_cursor_off;
10258
10259 /* VPOS >= active_glyphs->nrows means that window has been resized.
10260 Don't bother to erase the cursor. */
10261 if (vpos >= active_glyphs->nrows)
10262 goto mark_cursor_off;
10263
10264 /* If row containing cursor is marked invalid, there is nothing we
10265 can do. */
10266 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10267 if (!cursor_row->enabled_p)
10268 goto mark_cursor_off;
10269
10270 /* This can happen when the new row is shorter than the old one.
10271 In this case, either x_draw_glyphs or clear_end_of_line
10272 should have cleared the cursor. Note that we wouldn't be
10273 able to erase the cursor in this case because we don't have a
10274 cursor glyph at hand. */
10275 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10276 goto mark_cursor_off;
10277
10278 /* If the cursor is in the mouse face area, redisplay that when
10279 we clear the cursor. */
8801a864
KR
10280 if (! NILP (dpyinfo->mouse_face_window)
10281 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10282 && (vpos > dpyinfo->mouse_face_beg_row
10283 || (vpos == dpyinfo->mouse_face_beg_row
10284 && hpos >= dpyinfo->mouse_face_beg_col))
10285 && (vpos < dpyinfo->mouse_face_end_row
10286 || (vpos == dpyinfo->mouse_face_end_row
10287 && hpos < dpyinfo->mouse_face_end_col))
10288 /* Don't redraw the cursor's spot in mouse face if it is at the
10289 end of a line (on a newline). The cursor appears there, but
10290 mouse highlighting does not. */
10291 && cursor_row->used[TEXT_AREA] > hpos)
10292 mouse_face_here_p = 1;
10293
10294 /* Maybe clear the display under the cursor. */
10295 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10296 {
10297 int x;
045dee35 10298 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10299
06a2c219
GM
10300 cursor_glyph = get_phys_cursor_glyph (w);
10301 if (cursor_glyph == NULL)
10302 goto mark_cursor_off;
dbc4e1c1 10303
06a2c219
GM
10304 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10305
10306 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10307 x,
045dee35 10308 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10309 cursor_row->y)),
10310 cursor_glyph->pixel_width,
10311 cursor_row->visible_height,
10312 False);
dbc4e1c1 10313 }
06a2c219
GM
10314
10315 /* Erase the cursor by redrawing the character underneath it. */
10316 if (mouse_face_here_p)
10317 hl = DRAW_MOUSE_FACE;
10318 else if (cursor_row->inverse_p)
10319 hl = DRAW_INVERSE_VIDEO;
10320 else
10321 hl = DRAW_NORMAL_TEXT;
10322 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10323
06a2c219
GM
10324 mark_cursor_off:
10325 w->phys_cursor_on_p = 0;
10326 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10327}
10328
10329
06a2c219
GM
10330/* Display or clear cursor of window W. If ON is zero, clear the
10331 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10332 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10333
06a2c219
GM
10334void
10335x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10336 struct window *w;
10337 int on, hpos, vpos, x, y;
dbc4e1c1 10338{
06a2c219
GM
10339 struct frame *f = XFRAME (w->frame);
10340 int new_cursor_type;
f02d8aa0 10341 int new_cursor_width;
06a2c219
GM
10342 struct glyph_matrix *current_glyphs;
10343 struct glyph_row *glyph_row;
10344 struct glyph *glyph;
dbc4e1c1 10345
49d838ea 10346 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10347 windows and frames; in the latter case, the frame or window may
10348 be in the midst of changing its size, and x and y may be off the
10349 window. */
10350 if (! FRAME_VISIBLE_P (f)
10351 || FRAME_GARBAGED_P (f)
10352 || vpos >= w->current_matrix->nrows
10353 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10354 return;
10355
10356 /* If cursor is off and we want it off, return quickly. */
06a2c219 10357 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10358 return;
10359
06a2c219
GM
10360 current_glyphs = w->current_matrix;
10361 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10362 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10363
10364 /* If cursor row is not enabled, we don't really know where to
10365 display the cursor. */
10366 if (!glyph_row->enabled_p)
10367 {
10368 w->phys_cursor_on_p = 0;
10369 return;
10370 }
10371
10372 xassert (interrupt_input_blocked);
10373
10374 /* Set new_cursor_type to the cursor we want to be displayed. In a
10375 mini-buffer window, we want the cursor only to appear if we are
10376 reading input from this window. For the selected window, we want
10377 the cursor type given by the frame parameter. If explicitly
10378 marked off, draw no cursor. In all other cases, we want a hollow
10379 box cursor. */
f02d8aa0 10380 new_cursor_width = -1;
9b4a7047
GM
10381 if (cursor_in_echo_area
10382 && FRAME_HAS_MINIBUF_P (f)
10383 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10384 {
9b4a7047
GM
10385 if (w == XWINDOW (echo_area_window))
10386 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10387 else
10388 new_cursor_type = HOLLOW_BOX_CURSOR;
10389 }
06a2c219 10390 else
9b4a7047
GM
10391 {
10392 if (w != XWINDOW (selected_window)
10393 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10394 {
e55a0b79
GM
10395 extern int cursor_in_non_selected_windows;
10396
10397 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
9b4a7047
GM
10398 new_cursor_type = NO_CURSOR;
10399 else
10400 new_cursor_type = HOLLOW_BOX_CURSOR;
10401 }
10402 else if (w->cursor_off_p)
10403 new_cursor_type = NO_CURSOR;
10404 else
f02d8aa0
GM
10405 {
10406 struct buffer *b = XBUFFER (w->buffer);
10407
10408 if (EQ (b->cursor_type, Qt))
10409 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10410 else
10411 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10412 &new_cursor_width);
10413 }
9b4a7047 10414 }
06a2c219
GM
10415
10416 /* If cursor is currently being shown and we don't want it to be or
10417 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10418 erase it. */
06a2c219 10419 if (w->phys_cursor_on_p
dc6f92b8 10420 && (!on
06a2c219
GM
10421 || w->phys_cursor.x != x
10422 || w->phys_cursor.y != y
10423 || new_cursor_type != w->phys_cursor_type))
10424 x_erase_phys_cursor (w);
10425
10426 /* If the cursor is now invisible and we want it to be visible,
10427 display it. */
10428 if (on && !w->phys_cursor_on_p)
10429 {
10430 w->phys_cursor_ascent = glyph_row->ascent;
10431 w->phys_cursor_height = glyph_row->height;
10432
10433 /* Set phys_cursor_.* before x_draw_.* is called because some
10434 of them may need the information. */
10435 w->phys_cursor.x = x;
10436 w->phys_cursor.y = glyph_row->y;
10437 w->phys_cursor.hpos = hpos;
10438 w->phys_cursor.vpos = vpos;
10439 w->phys_cursor_type = new_cursor_type;
10440 w->phys_cursor_on_p = 1;
10441
10442 switch (new_cursor_type)
dc6f92b8 10443 {
06a2c219
GM
10444 case HOLLOW_BOX_CURSOR:
10445 x_draw_hollow_cursor (w, glyph_row);
10446 break;
10447
10448 case FILLED_BOX_CURSOR:
10449 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10450 break;
10451
10452 case BAR_CURSOR:
f02d8aa0 10453 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10454 break;
10455
10456 case NO_CURSOR:
10457 break;
dc6f92b8 10458
06a2c219
GM
10459 default:
10460 abort ();
10461 }
59ddecde
GM
10462
10463#ifdef HAVE_X_I18N
10464 if (w == XWINDOW (f->selected_window))
10465 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10466 xic_set_preeditarea (w, x, y);
10467#endif
dc6f92b8
JB
10468 }
10469
06a2c219 10470#ifndef XFlush
f676886a 10471 if (updating_frame != f)
334208b7 10472 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10473#endif
dc6f92b8
JB
10474}
10475
06a2c219
GM
10476
10477/* Display the cursor on window W, or clear it. X and Y are window
10478 relative pixel coordinates. HPOS and VPOS are glyph matrix
10479 positions. If W is not the selected window, display a hollow
10480 cursor. ON non-zero means display the cursor at X, Y which
10481 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10482
dfcf069d 10483void
06a2c219
GM
10484x_display_cursor (w, on, hpos, vpos, x, y)
10485 struct window *w;
10486 int on, hpos, vpos, x, y;
dc6f92b8 10487{
f94397b5 10488 BLOCK_INPUT;
06a2c219 10489 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10490 UNBLOCK_INPUT;
10491}
10492
06a2c219
GM
10493
10494/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10495 Don't change the cursor's position. */
10496
dfcf069d 10497void
06a2c219 10498x_update_cursor (f, on_p)
5d46f928 10499 struct frame *f;
5d46f928 10500{
06a2c219
GM
10501 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10502}
10503
10504
10505/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10506 in the window tree rooted at W. */
10507
10508static void
10509x_update_cursor_in_window_tree (w, on_p)
10510 struct window *w;
10511 int on_p;
10512{
10513 while (w)
10514 {
10515 if (!NILP (w->hchild))
10516 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10517 else if (!NILP (w->vchild))
10518 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10519 else
10520 x_update_window_cursor (w, on_p);
10521
10522 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10523 }
10524}
5d46f928 10525
f94397b5 10526
06a2c219
GM
10527/* Switch the display of W's cursor on or off, according to the value
10528 of ON. */
10529
10530static void
10531x_update_window_cursor (w, on)
10532 struct window *w;
10533 int on;
10534{
16b5d424
GM
10535 /* Don't update cursor in windows whose frame is in the process
10536 of being deleted. */
10537 if (w->current_matrix)
10538 {
10539 BLOCK_INPUT;
10540 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10541 w->phys_cursor.x, w->phys_cursor.y);
10542 UNBLOCK_INPUT;
10543 }
dc6f92b8 10544}
06a2c219
GM
10545
10546
10547
dc6f92b8
JB
10548\f
10549/* Icons. */
10550
f676886a 10551/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10552 when we get an expose event for it. */
dc6f92b8 10553
dfcf069d 10554void
f676886a
JB
10555refreshicon (f)
10556 struct frame *f;
dc6f92b8 10557{
06a2c219 10558 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10559}
10560
dbc4e1c1 10561/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10562
10563int
990ba854 10564x_bitmap_icon (f, file)
f676886a 10565 struct frame *f;
990ba854 10566 Lisp_Object file;
dc6f92b8 10567{
06a2c219 10568 int bitmap_id;
dc6f92b8 10569
c118dd06 10570 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10571 return 1;
10572
990ba854 10573 /* Free up our existing icon bitmap if any. */
7556890b
RS
10574 if (f->output_data.x->icon_bitmap > 0)
10575 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10576 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10577
10578 if (STRINGP (file))
7f2ae036
RS
10579 bitmap_id = x_create_bitmap_from_file (f, file);
10580 else
10581 {
990ba854 10582 /* Create the GNU bitmap if necessary. */
5bf01b68 10583 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10584 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10585 = x_create_bitmap_from_data (f, gnu_bits,
10586 gnu_width, gnu_height);
990ba854
RS
10587
10588 /* The first time we create the GNU bitmap,
06a2c219 10589 this increments the ref-count one extra time.
990ba854
RS
10590 As a result, the GNU bitmap is never freed.
10591 That way, we don't have to worry about allocating it again. */
334208b7 10592 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10593
334208b7 10594 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10595 }
10596
10597 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10598 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10599
10600 return 0;
10601}
10602
10603
1be2d067
KH
10604/* Make the x-window of frame F use a rectangle with text.
10605 Use ICON_NAME as the text. */
dc6f92b8
JB
10606
10607int
f676886a
JB
10608x_text_icon (f, icon_name)
10609 struct frame *f;
dc6f92b8
JB
10610 char *icon_name;
10611{
c118dd06 10612 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10613 return 1;
10614
1be2d067
KH
10615#ifdef HAVE_X11R4
10616 {
10617 XTextProperty text;
10618 text.value = (unsigned char *) icon_name;
10619 text.encoding = XA_STRING;
10620 text.format = 8;
10621 text.nitems = strlen (icon_name);
10622#ifdef USE_X_TOOLKIT
7556890b 10623 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10624 &text);
10625#else /* not USE_X_TOOLKIT */
10626 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10627#endif /* not USE_X_TOOLKIT */
10628 }
10629#else /* not HAVE_X11R4 */
10630 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10631#endif /* not HAVE_X11R4 */
58769bee 10632
7556890b
RS
10633 if (f->output_data.x->icon_bitmap > 0)
10634 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10635 f->output_data.x->icon_bitmap = 0;
b1c884c3 10636 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10637
10638 return 0;
10639}
10640\f
e99db5a1
RS
10641#define X_ERROR_MESSAGE_SIZE 200
10642
10643/* If non-nil, this should be a string.
10644 It means catch X errors and store the error message in this string. */
10645
10646static Lisp_Object x_error_message_string;
10647
10648/* An X error handler which stores the error message in
10649 x_error_message_string. This is called from x_error_handler if
10650 x_catch_errors is in effect. */
10651
06a2c219 10652static void
e99db5a1
RS
10653x_error_catcher (display, error)
10654 Display *display;
10655 XErrorEvent *error;
10656{
10657 XGetErrorText (display, error->error_code,
10658 XSTRING (x_error_message_string)->data,
10659 X_ERROR_MESSAGE_SIZE);
10660}
10661
10662/* Begin trapping X errors for display DPY. Actually we trap X errors
10663 for all displays, but DPY should be the display you are actually
10664 operating on.
10665
10666 After calling this function, X protocol errors no longer cause
10667 Emacs to exit; instead, they are recorded in the string
10668 stored in x_error_message_string.
10669
10670 Calling x_check_errors signals an Emacs error if an X error has
10671 occurred since the last call to x_catch_errors or x_check_errors.
10672
10673 Calling x_uncatch_errors resumes the normal error handling. */
10674
10675void x_check_errors ();
10676static Lisp_Object x_catch_errors_unwind ();
10677
10678int
10679x_catch_errors (dpy)
10680 Display *dpy;
10681{
10682 int count = specpdl_ptr - specpdl;
10683
10684 /* Make sure any errors from previous requests have been dealt with. */
10685 XSync (dpy, False);
10686
10687 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10688
10689 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10690 XSTRING (x_error_message_string)->data[0] = 0;
10691
10692 return count;
10693}
10694
10695/* Unbind the binding that we made to check for X errors. */
10696
10697static Lisp_Object
10698x_catch_errors_unwind (old_val)
10699 Lisp_Object old_val;
10700{
10701 x_error_message_string = old_val;
10702 return Qnil;
10703}
10704
10705/* If any X protocol errors have arrived since the last call to
10706 x_catch_errors or x_check_errors, signal an Emacs error using
10707 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10708
10709void
10710x_check_errors (dpy, format)
10711 Display *dpy;
10712 char *format;
10713{
10714 /* Make sure to catch any errors incurred so far. */
10715 XSync (dpy, False);
10716
10717 if (XSTRING (x_error_message_string)->data[0])
10718 error (format, XSTRING (x_error_message_string)->data);
10719}
10720
9829ddba
RS
10721/* Nonzero if we had any X protocol errors
10722 since we did x_catch_errors on DPY. */
e99db5a1
RS
10723
10724int
10725x_had_errors_p (dpy)
10726 Display *dpy;
10727{
10728 /* Make sure to catch any errors incurred so far. */
10729 XSync (dpy, False);
10730
10731 return XSTRING (x_error_message_string)->data[0] != 0;
10732}
10733
9829ddba
RS
10734/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10735
06a2c219 10736void
9829ddba
RS
10737x_clear_errors (dpy)
10738 Display *dpy;
10739{
10740 XSTRING (x_error_message_string)->data[0] = 0;
10741}
10742
e99db5a1
RS
10743/* Stop catching X protocol errors and let them make Emacs die.
10744 DPY should be the display that was passed to x_catch_errors.
10745 COUNT should be the value that was returned by
10746 the corresponding call to x_catch_errors. */
10747
10748void
10749x_uncatch_errors (dpy, count)
10750 Display *dpy;
10751 int count;
10752{
10753 unbind_to (count, Qnil);
10754}
10755
10756#if 0
10757static unsigned int x_wire_count;
10758x_trace_wire ()
10759{
10760 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10761}
10762#endif /* ! 0 */
10763
10764\f
10765/* Handle SIGPIPE, which can happen when the connection to a server
10766 simply goes away. SIGPIPE is handled by x_connection_signal.
10767 Don't need to do anything, because the write which caused the
10768 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10769 which will do the appropriate cleanup for us. */
e99db5a1
RS
10770
10771static SIGTYPE
10772x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10773 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10774{
10775#ifdef USG
10776 /* USG systems forget handlers when they are used;
10777 must reestablish each time */
10778 signal (signalnum, x_connection_signal);
10779#endif /* USG */
10780}
10781\f
4746118a
JB
10782/* Handling X errors. */
10783
7a13e894 10784/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10785
4746118a 10786static SIGTYPE
7a13e894
RS
10787x_connection_closed (display, error_message)
10788 Display *display;
10789 char *error_message;
4746118a 10790{
7a13e894
RS
10791 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10792 Lisp_Object frame, tail;
10793
6186a4a0
RS
10794 /* Indicate that this display is dead. */
10795
2e465cdd 10796#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 10797#ifdef USE_X_TOOLKIT
adabc3a9 10798 XtCloseDisplay (display);
2e465cdd 10799#endif
f613a4c8 10800#endif
adabc3a9 10801
6186a4a0
RS
10802 dpyinfo->display = 0;
10803
06a2c219 10804 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10805 that are on the dead display. */
10806 FOR_EACH_FRAME (tail, frame)
10807 {
10808 Lisp_Object minibuf_frame;
10809 minibuf_frame
10810 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10811 if (FRAME_X_P (XFRAME (frame))
10812 && FRAME_X_P (XFRAME (minibuf_frame))
10813 && ! EQ (frame, minibuf_frame)
10814 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10815 Fdelete_frame (frame, Qt);
10816 }
10817
10818 /* Now delete all remaining frames on the dead display.
06a2c219 10819 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10820 for another frame that we need to delete. */
10821 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10822 if (FRAME_X_P (XFRAME (frame))
10823 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10824 {
10825 /* Set this to t so that Fdelete_frame won't get confused
10826 trying to find a replacement. */
10827 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10828 Fdelete_frame (frame, Qt);
10829 }
7a13e894 10830
482a1bd2
KH
10831 if (dpyinfo)
10832 x_delete_display (dpyinfo);
7a13e894
RS
10833
10834 if (x_display_list == 0)
10835 {
f8d07b62 10836 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10837 shut_down_emacs (0, 0, Qnil);
10838 exit (70);
10839 }
12ba150f 10840
7a13e894
RS
10841 /* Ordinary stack unwind doesn't deal with these. */
10842#ifdef SIGIO
10843 sigunblock (sigmask (SIGIO));
10844#endif
10845 sigunblock (sigmask (SIGALRM));
10846 TOTALLY_UNBLOCK_INPUT;
10847
aa4d9a9e 10848 clear_waiting_for_input ();
7a13e894 10849 error ("%s", error_message);
4746118a
JB
10850}
10851
7a13e894
RS
10852/* This is the usual handler for X protocol errors.
10853 It kills all frames on the display that we got the error for.
10854 If that was the only one, it prints an error message and kills Emacs. */
10855
06a2c219 10856static void
c118dd06
JB
10857x_error_quitter (display, error)
10858 Display *display;
10859 XErrorEvent *error;
10860{
7a13e894 10861 char buf[256], buf1[356];
dc6f92b8 10862
58769bee 10863 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10864 original error handler. */
dc6f92b8 10865
c118dd06 10866 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10867 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10868 buf, error->request_code);
7a13e894 10869 x_connection_closed (display, buf1);
dc6f92b8
JB
10870}
10871
e99db5a1
RS
10872/* This is the first-level handler for X protocol errors.
10873 It calls x_error_quitter or x_error_catcher. */
7a13e894 10874
8922af5f 10875static int
e99db5a1 10876x_error_handler (display, error)
8922af5f 10877 Display *display;
e99db5a1 10878 XErrorEvent *error;
8922af5f 10879{
e99db5a1
RS
10880 if (! NILP (x_error_message_string))
10881 x_error_catcher (display, error);
10882 else
10883 x_error_quitter (display, error);
06a2c219 10884 return 0;
f9e24cb9 10885}
c118dd06 10886
e99db5a1
RS
10887/* This is the handler for X IO errors, always.
10888 It kills all frames on the display that we lost touch with.
10889 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10890
c118dd06 10891static int
e99db5a1 10892x_io_error_quitter (display)
c118dd06 10893 Display *display;
c118dd06 10894{
e99db5a1 10895 char buf[256];
dc6f92b8 10896
e99db5a1
RS
10897 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10898 x_connection_closed (display, buf);
06a2c219 10899 return 0;
dc6f92b8 10900}
dc6f92b8 10901\f
f451eb13
JB
10902/* Changing the font of the frame. */
10903
76bcdf39
RS
10904/* Give frame F the font named FONTNAME as its default font, and
10905 return the full name of that font. FONTNAME may be a wildcard
10906 pattern; in that case, we choose some font that fits the pattern.
10907 The return value shows which font we chose. */
10908
b5cf7a0e 10909Lisp_Object
f676886a
JB
10910x_new_font (f, fontname)
10911 struct frame *f;
dc6f92b8
JB
10912 register char *fontname;
10913{
dc43ef94 10914 struct font_info *fontp
ee569018 10915 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 10916
dc43ef94
KH
10917 if (!fontp)
10918 return Qnil;
2224a5fc 10919
dc43ef94 10920 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10921 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10922 f->output_data.x->fontset = -1;
10923
b2cad826
KH
10924 /* Compute the scroll bar width in character columns. */
10925 if (f->scroll_bar_pixel_width > 0)
10926 {
7556890b 10927 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10928 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10929 }
10930 else
4e61bddf
RS
10931 {
10932 int wid = FONT_WIDTH (f->output_data.x->font);
10933 f->scroll_bar_cols = (14 + wid - 1) / wid;
10934 }
b2cad826 10935
f676886a 10936 /* Now make the frame display the given font. */
c118dd06 10937 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10938 {
7556890b
RS
10939 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10940 f->output_data.x->font->fid);
10941 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10942 f->output_data.x->font->fid);
10943 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10944 f->output_data.x->font->fid);
f676886a 10945
a27f9f86 10946 frame_update_line_height (f);
0134a210 10947 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10948 }
a27f9f86
RS
10949 else
10950 /* If we are setting a new frame's font for the first time,
10951 there are no faces yet, so this font's height is the line height. */
7556890b 10952 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10953
dc43ef94
KH
10954 return build_string (fontp->full_name);
10955}
10956
10957/* Give frame F the fontset named FONTSETNAME as its default font, and
10958 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10959 pattern; in that case, we choose some fontset that fits the pattern.
10960 The return value shows which fontset we chose. */
b5cf7a0e 10961
dc43ef94
KH
10962Lisp_Object
10963x_new_fontset (f, fontsetname)
10964 struct frame *f;
10965 char *fontsetname;
10966{
ee569018 10967 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 10968 Lisp_Object result;
ee569018 10969 char *fontname;
b5cf7a0e 10970
dc43ef94
KH
10971 if (fontset < 0)
10972 return Qnil;
b5cf7a0e 10973
2da424f1
KH
10974 if (f->output_data.x->fontset == fontset)
10975 /* This fontset is already set in frame F. There's nothing more
10976 to do. */
ee569018 10977 return fontset_name (fontset);
dc43ef94 10978
ee569018 10979 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
10980
10981 if (!STRINGP (result))
10982 /* Can't load ASCII font. */
10983 return Qnil;
10984
10985 /* Since x_new_font doesn't update any fontset information, do it now. */
10986 f->output_data.x->fontset = fontset;
dc43ef94 10987
f5d11644
GM
10988#ifdef HAVE_X_I18N
10989 if (FRAME_XIC (f)
10990 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 10991 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
10992#endif
10993
dc43ef94 10994 return build_string (fontsetname);
dc6f92b8 10995}
f5d11644
GM
10996
10997\f
10998/***********************************************************************
10999 X Input Methods
11000 ***********************************************************************/
11001
11002#ifdef HAVE_X_I18N
11003
11004#ifdef HAVE_X11R6
11005
11006/* XIM destroy callback function, which is called whenever the
11007 connection to input method XIM dies. CLIENT_DATA contains a
11008 pointer to the x_display_info structure corresponding to XIM. */
11009
11010static void
11011xim_destroy_callback (xim, client_data, call_data)
11012 XIM xim;
11013 XPointer client_data;
11014 XPointer call_data;
11015{
11016 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11017 Lisp_Object frame, tail;
11018
11019 BLOCK_INPUT;
11020
11021 /* No need to call XDestroyIC.. */
11022 FOR_EACH_FRAME (tail, frame)
11023 {
11024 struct frame *f = XFRAME (frame);
11025 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11026 {
11027 FRAME_XIC (f) = NULL;
11028 if (FRAME_XIC_FONTSET (f))
11029 {
11030 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11031 FRAME_XIC_FONTSET (f) = NULL;
11032 }
11033 }
11034 }
11035
11036 /* No need to call XCloseIM. */
11037 dpyinfo->xim = NULL;
11038 XFree (dpyinfo->xim_styles);
11039 UNBLOCK_INPUT;
11040}
11041
11042#endif /* HAVE_X11R6 */
11043
11044/* Open the connection to the XIM server on display DPYINFO.
11045 RESOURCE_NAME is the resource name Emacs uses. */
11046
11047static void
11048xim_open_dpy (dpyinfo, resource_name)
11049 struct x_display_info *dpyinfo;
11050 char *resource_name;
11051{
11052 XIM xim;
11053
11054 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11055 dpyinfo->xim = xim;
11056
11057 if (xim)
11058 {
f5d11644
GM
11059#ifdef HAVE_X11R6
11060 XIMCallback destroy;
11061#endif
11062
11063 /* Get supported styles and XIM values. */
11064 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11065
11066#ifdef HAVE_X11R6
11067 destroy.callback = xim_destroy_callback;
11068 destroy.client_data = (XPointer)dpyinfo;
11069 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11070#endif
11071 }
11072}
11073
11074
b9de836c 11075#ifdef HAVE_X11R6_XIM
f5d11644
GM
11076
11077struct xim_inst_t
11078{
11079 struct x_display_info *dpyinfo;
11080 char *resource_name;
11081};
11082
11083/* XIM instantiate callback function, which is called whenever an XIM
11084 server is available. DISPLAY is teh display of the XIM.
11085 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11086 when the callback was registered. */
11087
11088static void
11089xim_instantiate_callback (display, client_data, call_data)
11090 Display *display;
11091 XPointer client_data;
11092 XPointer call_data;
11093{
11094 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11095 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11096
11097 /* We don't support multiple XIM connections. */
11098 if (dpyinfo->xim)
11099 return;
11100
11101 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11102
11103 /* Create XIC for the existing frames on the same display, as long
11104 as they have no XIC. */
11105 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11106 {
11107 Lisp_Object tail, frame;
11108
11109 BLOCK_INPUT;
11110 FOR_EACH_FRAME (tail, frame)
11111 {
11112 struct frame *f = XFRAME (frame);
11113
11114 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11115 if (FRAME_XIC (f) == NULL)
11116 {
11117 create_frame_xic (f);
11118 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11119 xic_set_statusarea (f);
11120 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11121 {
11122 struct window *w = XWINDOW (f->selected_window);
11123 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11124 }
11125 }
11126 }
11127
11128 UNBLOCK_INPUT;
11129 }
11130}
11131
b9de836c 11132#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11133
11134
11135/* Open a connection to the XIM server on display DPYINFO.
11136 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11137 connection only at the first time. On X11R6, open the connection
11138 in the XIM instantiate callback function. */
11139
11140static void
11141xim_initialize (dpyinfo, resource_name)
11142 struct x_display_info *dpyinfo;
11143 char *resource_name;
11144{
b9de836c 11145#ifdef HAVE_X11R6_XIM
f5d11644
GM
11146 struct xim_inst_t *xim_inst;
11147 int len;
11148
11149 dpyinfo->xim = NULL;
11150 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11151 xim_inst->dpyinfo = dpyinfo;
11152 len = strlen (resource_name);
11153 xim_inst->resource_name = (char *) xmalloc (len + 1);
11154 bcopy (resource_name, xim_inst->resource_name, len + 1);
11155 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11156 resource_name, EMACS_CLASS,
11157 xim_instantiate_callback,
2ebb2f8b
DL
11158 /* Fixme: This is XPointer in
11159 XFree86 but (XPointer *) on
11160 Tru64, at least. */
11161 (XPointer) xim_inst);
b9de836c 11162#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11163 dpyinfo->xim = NULL;
11164 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11165#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11166}
11167
11168
11169/* Close the connection to the XIM server on display DPYINFO. */
11170
11171static void
11172xim_close_dpy (dpyinfo)
11173 struct x_display_info *dpyinfo;
11174{
b9de836c 11175#ifdef HAVE_X11R6_XIM
f5d11644
GM
11176 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11177 NULL, EMACS_CLASS,
11178 xim_instantiate_callback, NULL);
b9de836c 11179#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11180 XCloseIM (dpyinfo->xim);
11181 dpyinfo->xim = NULL;
11182 XFree (dpyinfo->xim_styles);
11183}
11184
b9de836c 11185#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11186
11187
dc6f92b8 11188\f
2e365682
RS
11189/* Calculate the absolute position in frame F
11190 from its current recorded position values and gravity. */
11191
dfcf069d 11192void
43bca5d5 11193x_calc_absolute_position (f)
f676886a 11194 struct frame *f;
dc6f92b8 11195{
06a2c219 11196 Window child;
6dba1858 11197 int win_x = 0, win_y = 0;
7556890b 11198 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11199 int this_window;
11200
9829ddba
RS
11201 /* We have nothing to do if the current position
11202 is already for the top-left corner. */
11203 if (! ((flags & XNegative) || (flags & YNegative)))
11204 return;
11205
c81412a0 11206#ifdef USE_X_TOOLKIT
7556890b 11207 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11208#else
11209 this_window = FRAME_X_WINDOW (f);
11210#endif
6dba1858
RS
11211
11212 /* Find the position of the outside upper-left corner of
9829ddba
RS
11213 the inner window, with respect to the outer window.
11214 But do this only if we will need the results. */
7556890b 11215 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11216 {
9829ddba
RS
11217 int count;
11218
6dba1858 11219 BLOCK_INPUT;
9829ddba
RS
11220 count = x_catch_errors (FRAME_X_DISPLAY (f));
11221 while (1)
11222 {
11223 x_clear_errors (FRAME_X_DISPLAY (f));
11224 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11225
11226 /* From-window, to-window. */
11227 this_window,
11228 f->output_data.x->parent_desc,
11229
11230 /* From-position, to-position. */
11231 0, 0, &win_x, &win_y,
11232
11233 /* Child of win. */
11234 &child);
11235 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11236 {
11237 Window newroot, newparent = 0xdeadbeef;
11238 Window *newchildren;
2ebb2f8b 11239 unsigned int nchildren;
9829ddba
RS
11240
11241 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11242 &newparent, &newchildren, &nchildren))
11243 break;
58769bee 11244
7c3c78a3 11245 XFree ((char *) newchildren);
6dba1858 11246
9829ddba
RS
11247 f->output_data.x->parent_desc = newparent;
11248 }
11249 else
11250 break;
11251 }
6dba1858 11252
9829ddba 11253 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11254 UNBLOCK_INPUT;
11255 }
11256
11257 /* Treat negative positions as relative to the leftmost bottommost
11258 position that fits on the screen. */
20f55f9a 11259 if (flags & XNegative)
7556890b 11260 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11261 - 2 * f->output_data.x->border_width - win_x
11262 - PIXEL_WIDTH (f)
11263 + f->output_data.x->left_pos);
dc6f92b8 11264
20f55f9a 11265 if (flags & YNegative)
06a2c219
GM
11266 {
11267 int menubar_height = 0;
11268
11269#ifdef USE_X_TOOLKIT
11270 if (f->output_data.x->menubar_widget)
11271 menubar_height
11272 = (f->output_data.x->menubar_widget->core.height
11273 + f->output_data.x->menubar_widget->core.border_width);
11274#endif
11275
11276 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11277 - 2 * f->output_data.x->border_width
11278 - win_y
11279 - PIXEL_HEIGHT (f)
11280 - menubar_height
11281 + f->output_data.x->top_pos);
11282 }
2e365682 11283
3a35ab44
RS
11284 /* The left_pos and top_pos
11285 are now relative to the top and left screen edges,
11286 so the flags should correspond. */
7556890b 11287 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11288}
11289
3a35ab44
RS
11290/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11291 to really change the position, and 0 when calling from
11292 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11293 position values). It is -1 when calling from x_set_frame_parameters,
11294 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11295
dfcf069d 11296void
dc05a16b 11297x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11298 struct frame *f;
dc6f92b8 11299 register int xoff, yoff;
dc05a16b 11300 int change_gravity;
dc6f92b8 11301{
4a4cbdd5
KH
11302 int modified_top, modified_left;
11303
aa3ff7c9 11304 if (change_gravity > 0)
3a35ab44 11305 {
7556890b
RS
11306 f->output_data.x->top_pos = yoff;
11307 f->output_data.x->left_pos = xoff;
11308 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11309 if (xoff < 0)
7556890b 11310 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11311 if (yoff < 0)
7556890b
RS
11312 f->output_data.x->size_hint_flags |= YNegative;
11313 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11314 }
43bca5d5 11315 x_calc_absolute_position (f);
dc6f92b8
JB
11316
11317 BLOCK_INPUT;
c32cdd9a 11318 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11319
7556890b
RS
11320 modified_left = f->output_data.x->left_pos;
11321 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11322#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11323 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11324 /* It is a mystery why we need to add the border_width here
11325 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11326 if (change_gravity != 0)
4a4cbdd5 11327 {
7556890b
RS
11328 modified_left += f->output_data.x->border_width;
11329 modified_top += f->output_data.x->border_width;
4a4cbdd5 11330 }
e73ec6fa 11331#endif
4a4cbdd5 11332
3afe33e7 11333#ifdef USE_X_TOOLKIT
7556890b 11334 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11335 modified_left, modified_top);
3afe33e7 11336#else /* not USE_X_TOOLKIT */
334208b7 11337 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11338 modified_left, modified_top);
3afe33e7 11339#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11340 UNBLOCK_INPUT;
11341}
11342
bc20ebbf
FP
11343/* Call this to change the size of frame F's x-window.
11344 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11345 for this size change and subsequent size changes.
11346 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11347
dfcf069d 11348void
bc20ebbf 11349x_set_window_size (f, change_gravity, cols, rows)
f676886a 11350 struct frame *f;
bc20ebbf 11351 int change_gravity;
b1c884c3 11352 int cols, rows;
dc6f92b8 11353{
06a2c219 11354#ifndef USE_X_TOOLKIT
dc6f92b8 11355 int pixelwidth, pixelheight;
06a2c219 11356#endif
dc6f92b8 11357
80fd1fe2 11358 BLOCK_INPUT;
aee9a898
RS
11359
11360#ifdef USE_X_TOOLKIT
3a20653d
RS
11361 {
11362 /* The x and y position of the widget is clobbered by the
11363 call to XtSetValues within EmacsFrameSetCharSize.
11364 This is a real kludge, but I don't understand Xt so I can't
11365 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11366 int xpos = f->output_data.x->widget->core.x;
11367 int ypos = f->output_data.x->widget->core.y;
11368 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11369 f->output_data.x->widget->core.x = xpos;
11370 f->output_data.x->widget->core.y = ypos;
3a20653d 11371 }
80fd1fe2
FP
11372
11373#else /* not USE_X_TOOLKIT */
11374
b1c884c3 11375 check_frame_size (f, &rows, &cols);
7556890b 11376 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11377 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11378 ? 0
11379 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11380 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11381 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11382 f->output_data.x->flags_areas_extra
110859fc 11383 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11384 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11385 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11386
7556890b 11387 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11388 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11389
334208b7
RS
11390 XSync (FRAME_X_DISPLAY (f), False);
11391 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11392 pixelwidth, pixelheight);
b1c884c3
JB
11393
11394 /* Now, strictly speaking, we can't be sure that this is accurate,
11395 but the window manager will get around to dealing with the size
11396 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11397 ConfigureNotify event gets here.
11398
11399 We could just not bother storing any of this information here,
11400 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11401 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11402 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11403 point in the future when the ConfigureNotify event arrives.
11404
11405 We pass 1 for DELAY since we can't run Lisp code inside of
11406 a BLOCK_INPUT. */
7d1e984f 11407 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11408 PIXEL_WIDTH (f) = pixelwidth;
11409 PIXEL_HEIGHT (f) = pixelheight;
11410
aee9a898
RS
11411 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11412 receive in the ConfigureNotify event; if we get what we asked
11413 for, then the event won't cause the screen to become garbaged, so
11414 we have to make sure to do it here. */
11415 SET_FRAME_GARBAGED (f);
11416
11417 XFlush (FRAME_X_DISPLAY (f));
11418
11419#endif /* not USE_X_TOOLKIT */
11420
4d73d038 11421 /* If cursor was outside the new size, mark it as off. */
06a2c219 11422 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11423
aee9a898
RS
11424 /* Clear out any recollection of where the mouse highlighting was,
11425 since it might be in a place that's outside the new frame size.
11426 Actually checking whether it is outside is a pain in the neck,
11427 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11428 cancel_mouse_face (f);
dbc4e1c1 11429
dc6f92b8
JB
11430 UNBLOCK_INPUT;
11431}
dc6f92b8 11432\f
d047c4eb 11433/* Mouse warping. */
dc6f92b8 11434
9b378208 11435void
f676886a
JB
11436x_set_mouse_position (f, x, y)
11437 struct frame *f;
dc6f92b8
JB
11438 int x, y;
11439{
11440 int pix_x, pix_y;
11441
7556890b
RS
11442 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11443 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11444
11445 if (pix_x < 0) pix_x = 0;
11446 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11447
11448 if (pix_y < 0) pix_y = 0;
11449 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11450
11451 BLOCK_INPUT;
dc6f92b8 11452
334208b7
RS
11453 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11454 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11455 UNBLOCK_INPUT;
11456}
11457
9b378208
RS
11458/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11459
11460void
11461x_set_mouse_pixel_position (f, pix_x, pix_y)
11462 struct frame *f;
11463 int pix_x, pix_y;
11464{
11465 BLOCK_INPUT;
11466
334208b7
RS
11467 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11468 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11469 UNBLOCK_INPUT;
11470}
d047c4eb
KH
11471\f
11472/* focus shifting, raising and lowering. */
9b378208 11473
dfcf069d 11474void
f676886a
JB
11475x_focus_on_frame (f)
11476 struct frame *f;
dc6f92b8 11477{
1fb20991 11478#if 0 /* This proves to be unpleasant. */
f676886a 11479 x_raise_frame (f);
1fb20991 11480#endif
6d4238f3
JB
11481#if 0
11482 /* I don't think that the ICCCM allows programs to do things like this
11483 without the interaction of the window manager. Whatever you end up
f676886a 11484 doing with this code, do it to x_unfocus_frame too. */
334208b7 11485 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11486 RevertToPointerRoot, CurrentTime);
c118dd06 11487#endif /* ! 0 */
dc6f92b8
JB
11488}
11489
dfcf069d 11490void
f676886a
JB
11491x_unfocus_frame (f)
11492 struct frame *f;
dc6f92b8 11493{
6d4238f3 11494#if 0
f676886a 11495 /* Look at the remarks in x_focus_on_frame. */
0f941935 11496 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11497 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11498 RevertToPointerRoot, CurrentTime);
c118dd06 11499#endif /* ! 0 */
dc6f92b8
JB
11500}
11501
f676886a 11502/* Raise frame F. */
dc6f92b8 11503
dfcf069d 11504void
f676886a
JB
11505x_raise_frame (f)
11506 struct frame *f;
dc6f92b8 11507{
3a88c238 11508 if (f->async_visible)
dc6f92b8
JB
11509 {
11510 BLOCK_INPUT;
3afe33e7 11511#ifdef USE_X_TOOLKIT
7556890b 11512 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11513#else /* not USE_X_TOOLKIT */
334208b7 11514 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11515#endif /* not USE_X_TOOLKIT */
334208b7 11516 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11517 UNBLOCK_INPUT;
11518 }
11519}
11520
f676886a 11521/* Lower frame F. */
dc6f92b8 11522
dfcf069d 11523void
f676886a
JB
11524x_lower_frame (f)
11525 struct frame *f;
dc6f92b8 11526{
3a88c238 11527 if (f->async_visible)
dc6f92b8
JB
11528 {
11529 BLOCK_INPUT;
3afe33e7 11530#ifdef USE_X_TOOLKIT
7556890b 11531 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11532#else /* not USE_X_TOOLKIT */
334208b7 11533 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11534#endif /* not USE_X_TOOLKIT */
334208b7 11535 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11536 UNBLOCK_INPUT;
11537 }
11538}
11539
dbc4e1c1 11540static void
6b0442dc 11541XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11542 FRAME_PTR f;
6b0442dc 11543 int raise_flag;
dbc4e1c1 11544{
6b0442dc 11545 if (raise_flag)
dbc4e1c1
JB
11546 x_raise_frame (f);
11547 else
11548 x_lower_frame (f);
11549}
d047c4eb
KH
11550\f
11551/* Change of visibility. */
dc6f92b8 11552
9382638d
KH
11553/* This tries to wait until the frame is really visible.
11554 However, if the window manager asks the user where to position
11555 the frame, this will return before the user finishes doing that.
11556 The frame will not actually be visible at that time,
11557 but it will become visible later when the window manager
11558 finishes with it. */
11559
dfcf069d 11560void
f676886a
JB
11561x_make_frame_visible (f)
11562 struct frame *f;
dc6f92b8 11563{
990ba854 11564 Lisp_Object type;
1aa6072f 11565 int original_top, original_left;
dc6f92b8 11566
dc6f92b8 11567 BLOCK_INPUT;
dc6f92b8 11568
990ba854
RS
11569 type = x_icon_type (f);
11570 if (!NILP (type))
11571 x_bitmap_icon (f, type);
bdcd49ba 11572
f676886a 11573 if (! FRAME_VISIBLE_P (f))
90e65f07 11574 {
1aa6072f
RS
11575 /* We test FRAME_GARBAGED_P here to make sure we don't
11576 call x_set_offset a second time
11577 if we get to x_make_frame_visible a second time
11578 before the window gets really visible. */
11579 if (! FRAME_ICONIFIED_P (f)
11580 && ! f->output_data.x->asked_for_visible)
11581 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11582
11583 f->output_data.x->asked_for_visible = 1;
11584
90e65f07 11585 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11586 x_wm_set_window_state (f, NormalState);
3afe33e7 11587#ifdef USE_X_TOOLKIT
d7a38a2e 11588 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11589 XtMapWidget (f->output_data.x->widget);
3afe33e7 11590#else /* not USE_X_TOOLKIT */
7f9c7f94 11591 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11592#endif /* not USE_X_TOOLKIT */
0134a210
RS
11593#if 0 /* This seems to bring back scroll bars in the wrong places
11594 if the window configuration has changed. They seem
11595 to come back ok without this. */
ab648270 11596 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11597 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11598#endif
90e65f07 11599 }
dc6f92b8 11600
334208b7 11601 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11602
0dacf791
RS
11603 /* Synchronize to ensure Emacs knows the frame is visible
11604 before we do anything else. We do this loop with input not blocked
11605 so that incoming events are handled. */
11606 {
11607 Lisp_Object frame;
12ce2351 11608 int count;
28c01ffe
RS
11609 /* This must be before UNBLOCK_INPUT
11610 since events that arrive in response to the actions above
11611 will set it when they are handled. */
11612 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11613
11614 original_left = f->output_data.x->left_pos;
11615 original_top = f->output_data.x->top_pos;
c0a04927
RS
11616
11617 /* This must come after we set COUNT. */
11618 UNBLOCK_INPUT;
11619
2745e6c4 11620 /* We unblock here so that arriving X events are processed. */
1aa6072f 11621
dcb07ae9
RS
11622 /* Now move the window back to where it was "supposed to be".
11623 But don't do it if the gravity is negative.
11624 When the gravity is negative, this uses a position
28c01ffe
RS
11625 that is 3 pixels too low. Perhaps that's really the border width.
11626
11627 Don't do this if the window has never been visible before,
11628 because the window manager may choose the position
11629 and we don't want to override it. */
1aa6072f 11630
4d3f5d9a 11631 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11632 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11633 && previously_visible)
1aa6072f 11634 {
2745e6c4
RS
11635 Drawable rootw;
11636 int x, y;
11637 unsigned int width, height, border, depth;
06a2c219 11638
1aa6072f 11639 BLOCK_INPUT;
9829ddba 11640
06a2c219
GM
11641 /* On some window managers (such as FVWM) moving an existing
11642 window, even to the same place, causes the window manager
11643 to introduce an offset. This can cause the window to move
11644 to an unexpected location. Check the geometry (a little
11645 slow here) and then verify that the window is in the right
11646 place. If the window is not in the right place, move it
11647 there, and take the potential window manager hit. */
2745e6c4
RS
11648 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11649 &rootw, &x, &y, &width, &height, &border, &depth);
11650
11651 if (original_left != x || original_top != y)
11652 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11653 original_left, original_top);
11654
1aa6072f
RS
11655 UNBLOCK_INPUT;
11656 }
9829ddba 11657
e0c1aef2 11658 XSETFRAME (frame, f);
c0a04927 11659
12ce2351
GM
11660 /* Wait until the frame is visible. Process X events until a
11661 MapNotify event has been seen, or until we think we won't get a
11662 MapNotify at all.. */
11663 for (count = input_signal_count + 10;
11664 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11665 {
12ce2351 11666 /* Force processing of queued events. */
334208b7 11667 x_sync (f);
12ce2351
GM
11668
11669 /* Machines that do polling rather than SIGIO have been
11670 observed to go into a busy-wait here. So we'll fake an
11671 alarm signal to let the handler know that there's something
11672 to be read. We used to raise a real alarm, but it seems
11673 that the handler isn't always enabled here. This is
11674 probably a bug. */
8b2f8d4e 11675 if (input_polling_used ())
3b2fa4e6 11676 {
12ce2351
GM
11677 /* It could be confusing if a real alarm arrives while
11678 processing the fake one. Turn it off and let the
11679 handler reset it. */
bffcfca9
GM
11680 int old_poll_suppress_count = poll_suppress_count;
11681 poll_suppress_count = 1;
11682 poll_for_input_1 ();
11683 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11684 }
12ce2351
GM
11685
11686 /* See if a MapNotify event has been processed. */
11687 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11688 }
0dacf791 11689 }
dc6f92b8
JB
11690}
11691
06a2c219 11692/* Change from mapped state to withdrawn state. */
dc6f92b8 11693
d047c4eb
KH
11694/* Make the frame visible (mapped and not iconified). */
11695
dfcf069d 11696void
f676886a
JB
11697x_make_frame_invisible (f)
11698 struct frame *f;
dc6f92b8 11699{
546e6d5b
RS
11700 Window window;
11701
11702#ifdef USE_X_TOOLKIT
11703 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11704 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11705#else /* not USE_X_TOOLKIT */
11706 window = FRAME_X_WINDOW (f);
11707#endif /* not USE_X_TOOLKIT */
dc6f92b8 11708
9319ae23 11709 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11710 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11711 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11712
5627c40e 11713#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11714 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11715 return;
5627c40e 11716#endif
dc6f92b8
JB
11717
11718 BLOCK_INPUT;
c118dd06 11719
af31d76f
RS
11720 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11721 that the current position of the window is user-specified, rather than
11722 program-specified, so that when the window is mapped again, it will be
11723 placed at the same location, without forcing the user to position it
11724 by hand again (they have already done that once for this window.) */
c32cdd9a 11725 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11726
c118dd06
JB
11727#ifdef HAVE_X11R4
11728
334208b7
RS
11729 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11730 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11731 {
11732 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11733 error ("Can't notify window manager of window withdrawal");
c118dd06 11734 }
c118dd06 11735#else /* ! defined (HAVE_X11R4) */
16bd92ea 11736
c118dd06 11737 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11738 if (! EQ (Vx_no_window_manager, Qt))
11739 {
16bd92ea 11740 XEvent unmap;
dc6f92b8 11741
16bd92ea 11742 unmap.xunmap.type = UnmapNotify;
546e6d5b 11743 unmap.xunmap.window = window;
334208b7 11744 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11745 unmap.xunmap.from_configure = False;
334208b7
RS
11746 if (! XSendEvent (FRAME_X_DISPLAY (f),
11747 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11748 False,
06a2c219 11749 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11750 &unmap))
11751 {
11752 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11753 error ("Can't notify window manager of withdrawal");
16bd92ea 11754 }
dc6f92b8
JB
11755 }
11756
16bd92ea 11757 /* Unmap the window ourselves. Cheeky! */
334208b7 11758 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11759#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11760
5627c40e
RS
11761 /* We can't distinguish this from iconification
11762 just by the event that we get from the server.
11763 So we can't win using the usual strategy of letting
11764 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11765 and synchronize with the server to make sure we agree. */
11766 f->visible = 0;
11767 FRAME_ICONIFIED_P (f) = 0;
11768 f->async_visible = 0;
11769 f->async_iconified = 0;
11770
334208b7 11771 x_sync (f);
5627c40e 11772
dc6f92b8
JB
11773 UNBLOCK_INPUT;
11774}
11775
06a2c219 11776/* Change window state from mapped to iconified. */
dc6f92b8 11777
dfcf069d 11778void
f676886a
JB
11779x_iconify_frame (f)
11780 struct frame *f;
dc6f92b8 11781{
3afe33e7 11782 int result;
990ba854 11783 Lisp_Object type;
dc6f92b8 11784
9319ae23 11785 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11786 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11787 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11788
3a88c238 11789 if (f->async_iconified)
dc6f92b8
JB
11790 return;
11791
3afe33e7 11792 BLOCK_INPUT;
546e6d5b 11793
9af3143a
RS
11794 FRAME_SAMPLE_VISIBILITY (f);
11795
990ba854
RS
11796 type = x_icon_type (f);
11797 if (!NILP (type))
11798 x_bitmap_icon (f, type);
bdcd49ba
RS
11799
11800#ifdef USE_X_TOOLKIT
11801
546e6d5b
RS
11802 if (! FRAME_VISIBLE_P (f))
11803 {
11804 if (! EQ (Vx_no_window_manager, Qt))
11805 x_wm_set_window_state (f, IconicState);
11806 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11807 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11808 /* The server won't give us any event to indicate
11809 that an invisible frame was changed to an icon,
11810 so we have to record it here. */
11811 f->iconified = 1;
1e6bc770 11812 f->visible = 1;
9cf30a30 11813 f->async_iconified = 1;
1e6bc770 11814 f->async_visible = 0;
546e6d5b
RS
11815 UNBLOCK_INPUT;
11816 return;
11817 }
11818
334208b7 11819 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11820 XtWindow (f->output_data.x->widget),
334208b7 11821 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11822 UNBLOCK_INPUT;
11823
11824 if (!result)
546e6d5b 11825 error ("Can't notify window manager of iconification");
3afe33e7
RS
11826
11827 f->async_iconified = 1;
1e6bc770
RS
11828 f->async_visible = 0;
11829
8c002a25
KH
11830
11831 BLOCK_INPUT;
334208b7 11832 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11833 UNBLOCK_INPUT;
3afe33e7
RS
11834#else /* not USE_X_TOOLKIT */
11835
fd13dbb2
RS
11836 /* Make sure the X server knows where the window should be positioned,
11837 in case the user deiconifies with the window manager. */
11838 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11839 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11840
16bd92ea
JB
11841 /* Since we don't know which revision of X we're running, we'll use both
11842 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11843
11844 /* X11R4: send a ClientMessage to the window manager using the
11845 WM_CHANGE_STATE type. */
11846 {
11847 XEvent message;
58769bee 11848
c118dd06 11849 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11850 message.xclient.type = ClientMessage;
334208b7 11851 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11852 message.xclient.format = 32;
11853 message.xclient.data.l[0] = IconicState;
11854
334208b7
RS
11855 if (! XSendEvent (FRAME_X_DISPLAY (f),
11856 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11857 False,
11858 SubstructureRedirectMask | SubstructureNotifyMask,
11859 &message))
dc6f92b8
JB
11860 {
11861 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11862 error ("Can't notify window manager of iconification");
dc6f92b8 11863 }
16bd92ea 11864 }
dc6f92b8 11865
58769bee 11866 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11867 IconicState. */
11868 x_wm_set_window_state (f, IconicState);
dc6f92b8 11869
a9c00105
RS
11870 if (!FRAME_VISIBLE_P (f))
11871 {
11872 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11873 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11874 }
11875
3a88c238 11876 f->async_iconified = 1;
1e6bc770 11877 f->async_visible = 0;
dc6f92b8 11878
334208b7 11879 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11880 UNBLOCK_INPUT;
8c002a25 11881#endif /* not USE_X_TOOLKIT */
dc6f92b8 11882}
d047c4eb 11883\f
c0ff3fab 11884/* Destroy the X window of frame F. */
dc6f92b8 11885
dfcf069d 11886void
c0ff3fab 11887x_destroy_window (f)
f676886a 11888 struct frame *f;
dc6f92b8 11889{
7f9c7f94
RS
11890 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11891
dc6f92b8 11892 BLOCK_INPUT;
c0ff3fab 11893
6186a4a0
RS
11894 /* If a display connection is dead, don't try sending more
11895 commands to the X server. */
11896 if (dpyinfo->display != 0)
11897 {
11898 if (f->output_data.x->icon_desc != 0)
11899 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 11900#ifdef HAVE_X_I18N
f5d11644
GM
11901 if (FRAME_XIC (f))
11902 free_frame_xic (f);
31f41daf 11903#endif
6186a4a0 11904 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11905#ifdef USE_X_TOOLKIT
06a2c219
GM
11906 if (f->output_data.x->widget)
11907 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11908 free_frame_menubar (f);
3afe33e7
RS
11909#endif /* USE_X_TOOLKIT */
11910
6186a4a0
RS
11911 free_frame_faces (f);
11912 XFlush (FRAME_X_DISPLAY (f));
11913 }
dc6f92b8 11914
df89d8a4 11915 if (f->output_data.x->saved_menu_event)
06a2c219 11916 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11917
7556890b
RS
11918 xfree (f->output_data.x);
11919 f->output_data.x = 0;
0f941935
KH
11920 if (f == dpyinfo->x_focus_frame)
11921 dpyinfo->x_focus_frame = 0;
11922 if (f == dpyinfo->x_focus_event_frame)
11923 dpyinfo->x_focus_event_frame = 0;
11924 if (f == dpyinfo->x_highlight_frame)
11925 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11926
7f9c7f94
RS
11927 dpyinfo->reference_count--;
11928
11929 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11930 {
7f9c7f94
RS
11931 dpyinfo->mouse_face_beg_row
11932 = dpyinfo->mouse_face_beg_col = -1;
11933 dpyinfo->mouse_face_end_row
11934 = dpyinfo->mouse_face_end_col = -1;
11935 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11936 dpyinfo->mouse_face_deferred_gc = 0;
11937 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11938 }
0134a210 11939
c0ff3fab 11940 UNBLOCK_INPUT;
dc6f92b8
JB
11941}
11942\f
f451eb13
JB
11943/* Setting window manager hints. */
11944
af31d76f
RS
11945/* Set the normal size hints for the window manager, for frame F.
11946 FLAGS is the flags word to use--or 0 meaning preserve the flags
11947 that the window now has.
11948 If USER_POSITION is nonzero, we set the USPosition
11949 flag (this is useful when FLAGS is 0). */
6dba1858 11950
dfcf069d 11951void
af31d76f 11952x_wm_set_size_hint (f, flags, user_position)
f676886a 11953 struct frame *f;
af31d76f
RS
11954 long flags;
11955 int user_position;
dc6f92b8
JB
11956{
11957 XSizeHints size_hints;
3afe33e7
RS
11958
11959#ifdef USE_X_TOOLKIT
7e4f2521
FP
11960 Arg al[2];
11961 int ac = 0;
11962 Dimension widget_width, widget_height;
7556890b 11963 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11964#else /* not USE_X_TOOLKIT */
c118dd06 11965 Window window = FRAME_X_WINDOW (f);
3afe33e7 11966#endif /* not USE_X_TOOLKIT */
dc6f92b8 11967
b72a58fd
RS
11968 /* Setting PMaxSize caused various problems. */
11969 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11970
7556890b
RS
11971 size_hints.x = f->output_data.x->left_pos;
11972 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11973
7e4f2521
FP
11974#ifdef USE_X_TOOLKIT
11975 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11976 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11977 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11978 size_hints.height = widget_height;
11979 size_hints.width = widget_width;
11980#else /* not USE_X_TOOLKIT */
f676886a
JB
11981 size_hints.height = PIXEL_HEIGHT (f);
11982 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11983#endif /* not USE_X_TOOLKIT */
7553a6b7 11984
7556890b
RS
11985 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11986 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11987 size_hints.max_width
11988 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11989 size_hints.max_height
11990 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11991
d067ea8b
KH
11992 /* Calculate the base and minimum sizes.
11993
11994 (When we use the X toolkit, we don't do it here.
11995 Instead we copy the values that the widgets are using, below.) */
11996#ifndef USE_X_TOOLKIT
b1c884c3 11997 {
b0342f17 11998 int base_width, base_height;
0134a210 11999 int min_rows = 0, min_cols = 0;
b0342f17 12000
f451eb13
JB
12001 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12002 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12003
0134a210 12004 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12005
0134a210
RS
12006 /* The window manager uses the base width hints to calculate the
12007 current number of rows and columns in the frame while
12008 resizing; min_width and min_height aren't useful for this
12009 purpose, since they might not give the dimensions for a
12010 zero-row, zero-column frame.
58769bee 12011
0134a210
RS
12012 We use the base_width and base_height members if we have
12013 them; otherwise, we set the min_width and min_height members
12014 to the size for a zero x zero frame. */
b0342f17
JB
12015
12016#ifdef HAVE_X11R4
0134a210
RS
12017 size_hints.flags |= PBaseSize;
12018 size_hints.base_width = base_width;
12019 size_hints.base_height = base_height;
12020 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12021 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12022#else
0134a210
RS
12023 size_hints.min_width = base_width;
12024 size_hints.min_height = base_height;
b0342f17 12025#endif
b1c884c3 12026 }
dc6f92b8 12027
d067ea8b 12028 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12029 if (flags)
dc6f92b8 12030 {
d067ea8b
KH
12031 size_hints.flags |= flags;
12032 goto no_read;
12033 }
12034#endif /* not USE_X_TOOLKIT */
12035
12036 {
12037 XSizeHints hints; /* Sometimes I hate X Windows... */
12038 long supplied_return;
12039 int value;
af31d76f
RS
12040
12041#ifdef HAVE_X11R4
d067ea8b
KH
12042 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12043 &supplied_return);
af31d76f 12044#else
d067ea8b 12045 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12046#endif
58769bee 12047
d067ea8b
KH
12048#ifdef USE_X_TOOLKIT
12049 size_hints.base_height = hints.base_height;
12050 size_hints.base_width = hints.base_width;
12051 size_hints.min_height = hints.min_height;
12052 size_hints.min_width = hints.min_width;
12053#endif
12054
12055 if (flags)
12056 size_hints.flags |= flags;
12057 else
12058 {
12059 if (value == 0)
12060 hints.flags = 0;
12061 if (hints.flags & PSize)
12062 size_hints.flags |= PSize;
12063 if (hints.flags & PPosition)
12064 size_hints.flags |= PPosition;
12065 if (hints.flags & USPosition)
12066 size_hints.flags |= USPosition;
12067 if (hints.flags & USSize)
12068 size_hints.flags |= USSize;
12069 }
12070 }
12071
06a2c219 12072#ifndef USE_X_TOOLKIT
d067ea8b 12073 no_read:
06a2c219 12074#endif
0134a210 12075
af31d76f 12076#ifdef PWinGravity
7556890b 12077 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12078 size_hints.flags |= PWinGravity;
dc05a16b 12079
af31d76f 12080 if (user_position)
6dba1858 12081 {
af31d76f
RS
12082 size_hints.flags &= ~ PPosition;
12083 size_hints.flags |= USPosition;
6dba1858 12084 }
2554751d 12085#endif /* PWinGravity */
6dba1858 12086
b0342f17 12087#ifdef HAVE_X11R4
334208b7 12088 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12089#else
334208b7 12090 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12091#endif
dc6f92b8
JB
12092}
12093
12094/* Used for IconicState or NormalState */
06a2c219 12095
dfcf069d 12096void
f676886a
JB
12097x_wm_set_window_state (f, state)
12098 struct frame *f;
dc6f92b8
JB
12099 int state;
12100{
3afe33e7 12101#ifdef USE_X_TOOLKIT
546e6d5b
RS
12102 Arg al[1];
12103
12104 XtSetArg (al[0], XtNinitialState, state);
7556890b 12105 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12106#else /* not USE_X_TOOLKIT */
c118dd06 12107 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12108
7556890b
RS
12109 f->output_data.x->wm_hints.flags |= StateHint;
12110 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12111
7556890b 12112 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12113#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12114}
12115
dfcf069d 12116void
7f2ae036 12117x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12118 struct frame *f;
7f2ae036 12119 int pixmap_id;
dc6f92b8 12120{
d2bd6bc4
RS
12121 Pixmap icon_pixmap;
12122
06a2c219 12123#ifndef USE_X_TOOLKIT
c118dd06 12124 Window window = FRAME_X_WINDOW (f);
75231bad 12125#endif
dc6f92b8 12126
7f2ae036 12127 if (pixmap_id > 0)
dbc4e1c1 12128 {
d2bd6bc4 12129 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12130 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12131 }
12132 else
68568555
RS
12133 {
12134 /* It seems there is no way to turn off use of an icon pixmap.
12135 The following line does it, only if no icon has yet been created,
12136 for some window managers. But with mwm it crashes.
12137 Some people say it should clear the IconPixmapHint bit in this case,
12138 but that doesn't work, and the X consortium said it isn't the
12139 right thing at all. Since there is no way to win,
12140 best to explicitly give up. */
12141#if 0
12142 f->output_data.x->wm_hints.icon_pixmap = None;
12143#else
12144 return;
12145#endif
12146 }
b1c884c3 12147
d2bd6bc4
RS
12148#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12149
12150 {
12151 Arg al[1];
12152 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12153 XtSetValues (f->output_data.x->widget, al, 1);
12154 }
12155
12156#else /* not USE_X_TOOLKIT */
12157
7556890b
RS
12158 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12159 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12160
12161#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12162}
12163
dfcf069d 12164void
f676886a
JB
12165x_wm_set_icon_position (f, icon_x, icon_y)
12166 struct frame *f;
dc6f92b8
JB
12167 int icon_x, icon_y;
12168{
75231bad 12169#ifdef USE_X_TOOLKIT
7556890b 12170 Window window = XtWindow (f->output_data.x->widget);
75231bad 12171#else
c118dd06 12172 Window window = FRAME_X_WINDOW (f);
75231bad 12173#endif
dc6f92b8 12174
7556890b
RS
12175 f->output_data.x->wm_hints.flags |= IconPositionHint;
12176 f->output_data.x->wm_hints.icon_x = icon_x;
12177 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12178
7556890b 12179 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12180}
12181
12182\f
06a2c219
GM
12183/***********************************************************************
12184 Fonts
12185 ***********************************************************************/
dc43ef94
KH
12186
12187/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12188
dc43ef94
KH
12189struct font_info *
12190x_get_font_info (f, font_idx)
12191 FRAME_PTR f;
12192 int font_idx;
12193{
12194 return (FRAME_X_FONT_TABLE (f) + font_idx);
12195}
12196
12197
12198/* Return a list of names of available fonts matching PATTERN on frame
12199 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12200 to be listed. Frame F NULL means we have not yet created any
12201 frame on X, and consult the first display in x_display_list.
12202 MAXNAMES sets a limit on how many fonts to match. */
12203
12204Lisp_Object
12205x_list_fonts (f, pattern, size, maxnames)
12206 FRAME_PTR f;
12207 Lisp_Object pattern;
12208 int size;
12209 int maxnames;
12210{
06a2c219
GM
12211 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12212 Lisp_Object tem, second_best;
dc43ef94 12213 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12214 int try_XLoadQueryFont = 0;
53ca4657 12215 int count;
dc43ef94 12216
6b0efe73 12217 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12218 if (NILP (patterns))
12219 patterns = Fcons (pattern, Qnil);
81ba44e5 12220
09c6077f
KH
12221 if (maxnames == 1 && !size)
12222 /* We can return any single font matching PATTERN. */
12223 try_XLoadQueryFont = 1;
9a32686f 12224
8e713be6 12225 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12226 {
dc43ef94
KH
12227 int num_fonts;
12228 char **names;
12229
8e713be6 12230 pattern = XCAR (patterns);
536f4067
RS
12231 /* See if we cached the result for this particular query.
12232 The cache is an alist of the form:
12233 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12234 */
8e713be6 12235 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12236 key = Fcons (pattern, make_number (maxnames)),
12237 !NILP (list = Fassoc (key, tem))))
12238 {
12239 list = Fcdr_safe (list);
12240 /* We have a cashed list. Don't have to get the list again. */
12241 goto label_cached;
12242 }
12243
12244 /* At first, put PATTERN in the cache. */
09c6077f 12245
dc43ef94 12246 BLOCK_INPUT;
17d85edc
KH
12247 count = x_catch_errors (dpy);
12248
09c6077f
KH
12249 if (try_XLoadQueryFont)
12250 {
12251 XFontStruct *font;
12252 unsigned long value;
12253
12254 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12255 if (x_had_errors_p (dpy))
12256 {
12257 /* This error is perhaps due to insufficient memory on X
12258 server. Let's just ignore it. */
12259 font = NULL;
12260 x_clear_errors (dpy);
12261 }
12262
09c6077f
KH
12263 if (font
12264 && XGetFontProperty (font, XA_FONT, &value))
12265 {
12266 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12267 int len = strlen (name);
01c752b5 12268 char *tmp;
09c6077f 12269
6f6512e8
KH
12270 /* If DXPC (a Differential X Protocol Compressor)
12271 Ver.3.7 is running, XGetAtomName will return null
12272 string. We must avoid such a name. */
12273 if (len == 0)
12274 try_XLoadQueryFont = 0;
12275 else
12276 {
12277 num_fonts = 1;
12278 names = (char **) alloca (sizeof (char *));
12279 /* Some systems only allow alloca assigned to a
12280 simple var. */
12281 tmp = (char *) alloca (len + 1); names[0] = tmp;
12282 bcopy (name, names[0], len + 1);
12283 XFree (name);
12284 }
09c6077f
KH
12285 }
12286 else
12287 try_XLoadQueryFont = 0;
a083fd23
RS
12288
12289 if (font)
12290 XFreeFont (dpy, font);
09c6077f
KH
12291 }
12292
12293 if (!try_XLoadQueryFont)
17d85edc
KH
12294 {
12295 /* We try at least 10 fonts because XListFonts will return
12296 auto-scaled fonts at the head. */
12297 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12298 &num_fonts);
12299 if (x_had_errors_p (dpy))
12300 {
12301 /* This error is perhaps due to insufficient memory on X
12302 server. Let's just ignore it. */
12303 names = NULL;
12304 x_clear_errors (dpy);
12305 }
12306 }
12307
12308 x_uncatch_errors (dpy, count);
dc43ef94
KH
12309 UNBLOCK_INPUT;
12310
12311 if (names)
12312 {
12313 int i;
dc43ef94
KH
12314
12315 /* Make a list of all the fonts we got back.
12316 Store that in the font cache for the display. */
12317 for (i = 0; i < num_fonts; i++)
12318 {
06a2c219 12319 int width = 0;
dc43ef94 12320 char *p = names[i];
06a2c219
GM
12321 int average_width = -1, dashes = 0;
12322
dc43ef94 12323 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12324 14 dashes, and the field value following 12th dash
12325 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12326 is usually too ugly to be used for editing. Let's
12327 ignore it. */
dc43ef94
KH
12328 while (*p)
12329 if (*p++ == '-')
12330 {
12331 dashes++;
12332 if (dashes == 7) /* PIXEL_SIZE field */
12333 width = atoi (p);
12334 else if (dashes == 12) /* AVERAGE_WIDTH field */
12335 average_width = atoi (p);
12336 }
12337 if (dashes < 14 || average_width != 0)
12338 {
12339 tem = build_string (names[i]);
12340 if (NILP (Fassoc (tem, list)))
12341 {
12342 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12343 && ((fast_c_string_match_ignore_case
12344 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12345 >= 0))
12346 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12347 width of this font. */
dc43ef94
KH
12348 list = Fcons (Fcons (tem, make_number (width)), list);
12349 else
12350 /* For the moment, width is not known. */
12351 list = Fcons (Fcons (tem, Qnil), list);
12352 }
12353 }
12354 }
09c6077f
KH
12355 if (!try_XLoadQueryFont)
12356 XFreeFontNames (names);
dc43ef94
KH
12357 }
12358
b5210ea7 12359 /* Now store the result in the cache. */
dc43ef94 12360 if (f != NULL)
8e713be6 12361 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12362 = Fcons (Fcons (key, list),
8e713be6 12363 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12364
b5210ea7
KH
12365 label_cached:
12366 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12367
b5210ea7
KH
12368 newlist = second_best = Qnil;
12369 /* Make a list of the fonts that have the right width. */
8e713be6 12370 for (; CONSP (list); list = XCDR (list))
b5210ea7 12371 {
536f4067
RS
12372 int found_size;
12373
8e713be6 12374 tem = XCAR (list);
dc43ef94 12375
8e713be6 12376 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12377 continue;
12378 if (!size)
12379 {
8e713be6 12380 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12381 continue;
12382 }
dc43ef94 12383
8e713be6 12384 if (!INTEGERP (XCDR (tem)))
dc43ef94 12385 {
b5210ea7
KH
12386 /* Since we have not yet known the size of this font, we
12387 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12388 XFontStruct *thisinfo;
12389
12390 BLOCK_INPUT;
17d85edc 12391 count = x_catch_errors (dpy);
dc43ef94 12392 thisinfo = XLoadQueryFont (dpy,
8e713be6 12393 XSTRING (XCAR (tem))->data);
17d85edc
KH
12394 if (x_had_errors_p (dpy))
12395 {
12396 /* This error is perhaps due to insufficient memory on X
12397 server. Let's just ignore it. */
12398 thisinfo = NULL;
12399 x_clear_errors (dpy);
12400 }
12401 x_uncatch_errors (dpy, count);
dc43ef94
KH
12402 UNBLOCK_INPUT;
12403
12404 if (thisinfo)
12405 {
8e713be6 12406 XCDR (tem)
536f4067
RS
12407 = (thisinfo->min_bounds.width == 0
12408 ? make_number (0)
12409 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12410 XFreeFont (dpy, thisinfo);
12411 }
12412 else
b5210ea7 12413 /* For unknown reason, the previous call of XListFont had
06a2c219 12414 returned a font which can't be opened. Record the size
b5210ea7 12415 as 0 not to try to open it again. */
8e713be6 12416 XCDR (tem) = make_number (0);
dc43ef94 12417 }
536f4067 12418
8e713be6 12419 found_size = XINT (XCDR (tem));
536f4067 12420 if (found_size == size)
8e713be6 12421 newlist = Fcons (XCAR (tem), newlist);
536f4067 12422 else if (found_size > 0)
b5210ea7 12423 {
536f4067 12424 if (NILP (second_best))
b5210ea7 12425 second_best = tem;
536f4067
RS
12426 else if (found_size < size)
12427 {
8e713be6
KR
12428 if (XINT (XCDR (second_best)) > size
12429 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12430 second_best = tem;
12431 }
12432 else
12433 {
8e713be6
KR
12434 if (XINT (XCDR (second_best)) > size
12435 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12436 second_best = tem;
12437 }
b5210ea7
KH
12438 }
12439 }
12440 if (!NILP (newlist))
12441 break;
12442 else if (!NILP (second_best))
12443 {
8e713be6 12444 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12445 break;
dc43ef94 12446 }
dc43ef94
KH
12447 }
12448
12449 return newlist;
12450}
12451
06a2c219
GM
12452
12453#if GLYPH_DEBUG
12454
12455/* Check that FONT is valid on frame F. It is if it can be found in F's
12456 font table. */
12457
12458static void
12459x_check_font (f, font)
12460 struct frame *f;
12461 XFontStruct *font;
12462{
12463 int i;
12464 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12465
12466 xassert (font != NULL);
12467
12468 for (i = 0; i < dpyinfo->n_fonts; i++)
12469 if (dpyinfo->font_table[i].name
12470 && font == dpyinfo->font_table[i].font)
12471 break;
12472
12473 xassert (i < dpyinfo->n_fonts);
12474}
12475
12476#endif /* GLYPH_DEBUG != 0 */
12477
12478/* Set *W to the minimum width, *H to the minimum font height of FONT.
12479 Note: There are (broken) X fonts out there with invalid XFontStruct
12480 min_bounds contents. For example, handa@etl.go.jp reports that
12481 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12482 have font->min_bounds.width == 0. */
12483
12484static INLINE void
12485x_font_min_bounds (font, w, h)
12486 XFontStruct *font;
12487 int *w, *h;
12488{
12489 *h = FONT_HEIGHT (font);
12490 *w = font->min_bounds.width;
12491
12492 /* Try to handle the case where FONT->min_bounds has invalid
12493 contents. Since the only font known to have invalid min_bounds
12494 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12495 if (*w <= 0)
12496 *w = font->max_bounds.width;
12497}
12498
12499
12500/* Compute the smallest character width and smallest font height over
12501 all fonts available on frame F. Set the members smallest_char_width
12502 and smallest_font_height in F's x_display_info structure to
12503 the values computed. Value is non-zero if smallest_font_height or
12504 smallest_char_width become smaller than they were before. */
12505
12506static int
12507x_compute_min_glyph_bounds (f)
12508 struct frame *f;
12509{
12510 int i;
12511 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12512 XFontStruct *font;
12513 int old_width = dpyinfo->smallest_char_width;
12514 int old_height = dpyinfo->smallest_font_height;
12515
12516 dpyinfo->smallest_font_height = 100000;
12517 dpyinfo->smallest_char_width = 100000;
12518
12519 for (i = 0; i < dpyinfo->n_fonts; ++i)
12520 if (dpyinfo->font_table[i].name)
12521 {
12522 struct font_info *fontp = dpyinfo->font_table + i;
12523 int w, h;
12524
12525 font = (XFontStruct *) fontp->font;
12526 xassert (font != (XFontStruct *) ~0);
12527 x_font_min_bounds (font, &w, &h);
12528
12529 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12530 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12531 }
12532
12533 xassert (dpyinfo->smallest_char_width > 0
12534 && dpyinfo->smallest_font_height > 0);
12535
12536 return (dpyinfo->n_fonts == 1
12537 || dpyinfo->smallest_char_width < old_width
12538 || dpyinfo->smallest_font_height < old_height);
12539}
12540
12541
dc43ef94
KH
12542/* Load font named FONTNAME of the size SIZE for frame F, and return a
12543 pointer to the structure font_info while allocating it dynamically.
12544 If SIZE is 0, load any size of font.
12545 If loading is failed, return NULL. */
12546
12547struct font_info *
12548x_load_font (f, fontname, size)
12549 struct frame *f;
12550 register char *fontname;
12551 int size;
12552{
12553 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12554 Lisp_Object font_names;
d645aaa4 12555 int count;
dc43ef94
KH
12556
12557 /* Get a list of all the fonts that match this name. Once we
12558 have a list of matching fonts, we compare them against the fonts
12559 we already have by comparing names. */
09c6077f 12560 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12561
12562 if (!NILP (font_names))
12563 {
12564 Lisp_Object tail;
12565 int i;
12566
12567 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12568 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12569 if (dpyinfo->font_table[i].name
12570 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12571 XSTRING (XCAR (tail))->data)
06a2c219 12572 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12573 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12574 return (dpyinfo->font_table + i);
12575 }
12576
12577 /* Load the font and add it to the table. */
12578 {
12579 char *full_name;
12580 XFontStruct *font;
12581 struct font_info *fontp;
12582 unsigned long value;
06a2c219 12583 int i;
dc43ef94 12584
2da424f1
KH
12585 /* If we have found fonts by x_list_font, load one of them. If
12586 not, we still try to load a font by the name given as FONTNAME
12587 because XListFonts (called in x_list_font) of some X server has
12588 a bug of not finding a font even if the font surely exists and
12589 is loadable by XLoadQueryFont. */
e1d6d5b9 12590 if (size > 0 && !NILP (font_names))
8e713be6 12591 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12592
12593 BLOCK_INPUT;
d645aaa4 12594 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12595 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12596 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12597 {
12598 /* This error is perhaps due to insufficient memory on X
12599 server. Let's just ignore it. */
12600 font = NULL;
12601 x_clear_errors (FRAME_X_DISPLAY (f));
12602 }
12603 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12604 UNBLOCK_INPUT;
b5210ea7 12605 if (!font)
dc43ef94
KH
12606 return NULL;
12607
06a2c219
GM
12608 /* Find a free slot in the font table. */
12609 for (i = 0; i < dpyinfo->n_fonts; ++i)
12610 if (dpyinfo->font_table[i].name == NULL)
12611 break;
12612
12613 /* If no free slot found, maybe enlarge the font table. */
12614 if (i == dpyinfo->n_fonts
12615 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12616 {
06a2c219
GM
12617 int sz;
12618 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12619 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12620 dpyinfo->font_table
06a2c219 12621 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12622 }
12623
06a2c219
GM
12624 fontp = dpyinfo->font_table + i;
12625 if (i == dpyinfo->n_fonts)
12626 ++dpyinfo->n_fonts;
dc43ef94
KH
12627
12628 /* Now fill in the slots of *FONTP. */
12629 BLOCK_INPUT;
12630 fontp->font = font;
06a2c219 12631 fontp->font_idx = i;
dc43ef94
KH
12632 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12633 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12634
12635 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12636 full_name = 0;
12637 if (XGetFontProperty (font, XA_FONT, &value))
12638 {
12639 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12640 char *p = name;
12641 int dashes = 0;
12642
12643 /* Count the number of dashes in the "full name".
12644 If it is too few, this isn't really the font's full name,
12645 so don't use it.
12646 In X11R4, the fonts did not come with their canonical names
12647 stored in them. */
12648 while (*p)
12649 {
12650 if (*p == '-')
12651 dashes++;
12652 p++;
12653 }
12654
12655 if (dashes >= 13)
12656 {
12657 full_name = (char *) xmalloc (p - name + 1);
12658 bcopy (name, full_name, p - name + 1);
12659 }
12660
12661 XFree (name);
12662 }
12663
12664 if (full_name != 0)
12665 fontp->full_name = full_name;
12666 else
12667 fontp->full_name = fontp->name;
12668
12669 fontp->size = font->max_bounds.width;
d5749adb
KH
12670 fontp->height = FONT_HEIGHT (font);
12671 {
12672 /* For some font, ascent and descent in max_bounds field is
12673 larger than the above value. */
12674 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12675 if (max_height > fontp->height)
74848a96 12676 fontp->height = max_height;
d5749adb 12677 }
dc43ef94 12678
2da424f1
KH
12679 if (NILP (font_names))
12680 {
12681 /* We come here because of a bug of XListFonts mentioned at
12682 the head of this block. Let's store this information in
12683 the cache for x_list_fonts. */
12684 Lisp_Object lispy_name = build_string (fontname);
12685 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12686
8e713be6 12687 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12688 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12689 Fcons (Fcons (lispy_full_name,
12690 make_number (fontp->size)),
12691 Qnil)),
8e713be6 12692 XCDR (dpyinfo->name_list_element));
2da424f1 12693 if (full_name)
8e713be6 12694 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12695 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12696 Fcons (Fcons (lispy_full_name,
12697 make_number (fontp->size)),
12698 Qnil)),
8e713be6 12699 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12700 }
12701
dc43ef94
KH
12702 /* The slot `encoding' specifies how to map a character
12703 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12704 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12705 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12706 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12707 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12708 which is never used by any charset. If mapping can't be
12709 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12710 fontp->encoding[1]
12711 = (font->max_byte1 == 0
12712 /* 1-byte font */
12713 ? (font->min_char_or_byte2 < 0x80
12714 ? (font->max_char_or_byte2 < 0x80
12715 ? 0 /* 0x20..0x7F */
8ff102bd 12716 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12717 : 1) /* 0xA0..0xFF */
12718 /* 2-byte font */
12719 : (font->min_byte1 < 0x80
12720 ? (font->max_byte1 < 0x80
12721 ? (font->min_char_or_byte2 < 0x80
12722 ? (font->max_char_or_byte2 < 0x80
12723 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12724 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12725 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12726 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12727 : (font->min_char_or_byte2 < 0x80
12728 ? (font->max_char_or_byte2 < 0x80
12729 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12730 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12731 : 1))); /* 0xA0A0..0xFFFF */
12732
12733 fontp->baseline_offset
12734 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12735 ? (long) value : 0);
12736 fontp->relative_compose
12737 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12738 ? (long) value : 0);
f78798df
KH
12739 fontp->default_ascent
12740 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12741 ? (long) value : 0);
dc43ef94 12742
06a2c219
GM
12743 /* Set global flag fonts_changed_p to non-zero if the font loaded
12744 has a character with a smaller width than any other character
12745 before, or if the font loaded has a smalle>r height than any
12746 other font loaded before. If this happens, it will make a
12747 glyph matrix reallocation necessary. */
12748 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12749 UNBLOCK_INPUT;
dc43ef94
KH
12750 return fontp;
12751 }
12752}
12753
06a2c219
GM
12754
12755/* Return a pointer to struct font_info of a font named FONTNAME for
12756 frame F. If no such font is loaded, return NULL. */
12757
dc43ef94
KH
12758struct font_info *
12759x_query_font (f, fontname)
12760 struct frame *f;
12761 register char *fontname;
12762{
12763 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12764 int i;
12765
12766 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12767 if (dpyinfo->font_table[i].name
12768 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12769 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12770 return (dpyinfo->font_table + i);
12771 return NULL;
12772}
12773
06a2c219
GM
12774
12775/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12776 `encoder' of the structure. */
12777
12778void
12779x_find_ccl_program (fontp)
12780 struct font_info *fontp;
12781{
a42f54e6 12782 Lisp_Object list, elt;
a6582676 12783
8e713be6 12784 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12785 {
8e713be6 12786 elt = XCAR (list);
a6582676 12787 if (CONSP (elt)
8e713be6
KR
12788 && STRINGP (XCAR (elt))
12789 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12790 >= 0))
a42f54e6
KH
12791 break;
12792 }
12793 if (! NILP (list))
12794 {
d27f8ca7
KH
12795 struct ccl_program *ccl
12796 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12797
8e713be6 12798 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12799 xfree (ccl);
12800 else
12801 fontp->font_encoder = ccl;
a6582676
KH
12802 }
12803}
12804
06a2c219 12805
dc43ef94 12806\f
06a2c219
GM
12807/***********************************************************************
12808 Initialization
12809 ***********************************************************************/
f451eb13 12810
3afe33e7
RS
12811#ifdef USE_X_TOOLKIT
12812static XrmOptionDescRec emacs_options[] = {
12813 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12814 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12815
12816 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12817 XrmoptionSepArg, NULL},
12818 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12819
12820 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12821 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12822 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12823 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12824 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12825 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12826 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12827};
12828#endif /* USE_X_TOOLKIT */
12829
7a13e894
RS
12830static int x_initialized;
12831
29b38361
KH
12832#ifdef MULTI_KBOARD
12833/* Test whether two display-name strings agree up to the dot that separates
12834 the screen number from the server number. */
12835static int
12836same_x_server (name1, name2)
12837 char *name1, *name2;
12838{
12839 int seen_colon = 0;
cf591cc1
RS
12840 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12841 int system_name_length = strlen (system_name);
12842 int length_until_period = 0;
12843
12844 while (system_name[length_until_period] != 0
12845 && system_name[length_until_period] != '.')
12846 length_until_period++;
12847
12848 /* Treat `unix' like an empty host name. */
12849 if (! strncmp (name1, "unix:", 5))
12850 name1 += 4;
12851 if (! strncmp (name2, "unix:", 5))
12852 name2 += 4;
12853 /* Treat this host's name like an empty host name. */
12854 if (! strncmp (name1, system_name, system_name_length)
12855 && name1[system_name_length] == ':')
12856 name1 += system_name_length;
12857 if (! strncmp (name2, system_name, system_name_length)
12858 && name2[system_name_length] == ':')
12859 name2 += system_name_length;
12860 /* Treat this host's domainless name like an empty host name. */
12861 if (! strncmp (name1, system_name, length_until_period)
12862 && name1[length_until_period] == ':')
12863 name1 += length_until_period;
12864 if (! strncmp (name2, system_name, length_until_period)
12865 && name2[length_until_period] == ':')
12866 name2 += length_until_period;
12867
29b38361
KH
12868 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12869 {
12870 if (*name1 == ':')
12871 seen_colon++;
12872 if (seen_colon && *name1 == '.')
12873 return 1;
12874 }
12875 return (seen_colon
12876 && (*name1 == '.' || *name1 == '\0')
12877 && (*name2 == '.' || *name2 == '\0'));
12878}
12879#endif
12880
334208b7 12881struct x_display_info *
1f8255f2 12882x_term_init (display_name, xrm_option, resource_name)
334208b7 12883 Lisp_Object display_name;
1f8255f2
RS
12884 char *xrm_option;
12885 char *resource_name;
dc6f92b8 12886{
334208b7 12887 int connection;
7a13e894 12888 Display *dpy;
334208b7
RS
12889 struct x_display_info *dpyinfo;
12890 XrmDatabase xrdb;
12891
60439948
KH
12892 BLOCK_INPUT;
12893
7a13e894
RS
12894 if (!x_initialized)
12895 {
12896 x_initialize ();
12897 x_initialized = 1;
12898 }
dc6f92b8 12899
3afe33e7 12900#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12901 /* weiner@footloose.sps.mot.com reports that this causes
12902 errors with X11R5:
12903 X protocol error: BadAtom (invalid Atom parameter)
12904 on protocol request 18skiloaf.
12905 So let's not use it until R6. */
12906#ifdef HAVE_X11XTR6
bdcd49ba
RS
12907 XtSetLanguageProc (NULL, NULL, NULL);
12908#endif
12909
7f9c7f94
RS
12910 {
12911 int argc = 0;
12912 char *argv[3];
12913
12914 argv[0] = "";
12915 argc = 1;
12916 if (xrm_option)
12917 {
12918 argv[argc++] = "-xrm";
12919 argv[argc++] = xrm_option;
12920 }
12921 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12922 resource_name, EMACS_CLASS,
12923 emacs_options, XtNumber (emacs_options),
12924 &argc, argv);
39d8bb4d
KH
12925
12926#ifdef HAVE_X11XTR6
10537cb1 12927 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12928 fixup_locale ();
39d8bb4d 12929#endif
7f9c7f94 12930 }
3afe33e7
RS
12931
12932#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12933#ifdef HAVE_X11R5
12934 XSetLocaleModifiers ("");
12935#endif
7a13e894 12936 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12937#endif /* not USE_X_TOOLKIT */
334208b7 12938
7a13e894
RS
12939 /* Detect failure. */
12940 if (dpy == 0)
60439948
KH
12941 {
12942 UNBLOCK_INPUT;
12943 return 0;
12944 }
7a13e894
RS
12945
12946 /* We have definitely succeeded. Record the new connection. */
12947
12948 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12949
29b38361
KH
12950#ifdef MULTI_KBOARD
12951 {
12952 struct x_display_info *share;
12953 Lisp_Object tail;
12954
12955 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12956 share = share->next, tail = XCDR (tail))
12957 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12958 XSTRING (display_name)->data))
12959 break;
12960 if (share)
12961 dpyinfo->kboard = share->kboard;
12962 else
12963 {
12964 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12965 init_kboard (dpyinfo->kboard);
59e755be
KH
12966 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12967 {
12968 char *vendor = ServerVendor (dpy);
9b6ed9f3 12969 UNBLOCK_INPUT;
59e755be
KH
12970 dpyinfo->kboard->Vsystem_key_alist
12971 = call1 (Qvendor_specific_keysyms,
12972 build_string (vendor ? vendor : ""));
9b6ed9f3 12973 BLOCK_INPUT;
59e755be
KH
12974 }
12975
29b38361
KH
12976 dpyinfo->kboard->next_kboard = all_kboards;
12977 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12978 /* Don't let the initial kboard remain current longer than necessary.
12979 That would cause problems if a file loaded on startup tries to
06a2c219 12980 prompt in the mini-buffer. */
0ad5446c
KH
12981 if (current_kboard == initial_kboard)
12982 current_kboard = dpyinfo->kboard;
29b38361
KH
12983 }
12984 dpyinfo->kboard->reference_count++;
12985 }
b9737ad3
KH
12986#endif
12987
7a13e894
RS
12988 /* Put this display on the chain. */
12989 dpyinfo->next = x_display_list;
12990 x_display_list = dpyinfo;
12991
12992 /* Put it on x_display_name_list as well, to keep them parallel. */
12993 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12994 x_display_name_list);
8e713be6 12995 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12996
12997 dpyinfo->display = dpy;
dc6f92b8 12998
dc6f92b8 12999#if 0
7a13e894 13000 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13001#endif /* ! 0 */
7a13e894
RS
13002
13003 dpyinfo->x_id_name
fc932ac6
RS
13004 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13005 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13006 + 2);
13007 sprintf (dpyinfo->x_id_name, "%s@%s",
13008 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13009
13010 /* Figure out which modifier bits mean what. */
334208b7 13011 x_find_modifier_meanings (dpyinfo);
f451eb13 13012
ab648270 13013 /* Get the scroll bar cursor. */
7a13e894 13014 dpyinfo->vertical_scroll_bar_cursor
334208b7 13015 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13016
334208b7
RS
13017 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13018 resource_name, EMACS_CLASS);
13019#ifdef HAVE_XRMSETDATABASE
13020 XrmSetDatabase (dpyinfo->display, xrdb);
13021#else
13022 dpyinfo->display->db = xrdb;
13023#endif
547d9db8 13024 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13025 all versions. */
13026 dpyinfo->xrdb = xrdb;
334208b7
RS
13027
13028 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13029 DefaultScreen (dpyinfo->display));
5ff67d81 13030 select_visual (dpyinfo);
43bd1b2b 13031 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13032 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13033 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13034 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13035 dpyinfo->grabbed = 0;
13036 dpyinfo->reference_count = 0;
13037 dpyinfo->icon_bitmap_id = -1;
06a2c219 13038 dpyinfo->font_table = NULL;
7a13e894
RS
13039 dpyinfo->n_fonts = 0;
13040 dpyinfo->font_table_size = 0;
13041 dpyinfo->bitmaps = 0;
13042 dpyinfo->bitmaps_size = 0;
13043 dpyinfo->bitmaps_last = 0;
13044 dpyinfo->scratch_cursor_gc = 0;
13045 dpyinfo->mouse_face_mouse_frame = 0;
13046 dpyinfo->mouse_face_deferred_gc = 0;
13047 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13048 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13049 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13050 dpyinfo->mouse_face_window = Qnil;
13051 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13052 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13053 dpyinfo->x_focus_frame = 0;
13054 dpyinfo->x_focus_event_frame = 0;
13055 dpyinfo->x_highlight_frame = 0;
06a2c219 13056 dpyinfo->image_cache = make_image_cache ();
334208b7 13057
43bd1b2b 13058 /* See if a private colormap is requested. */
5ff67d81
GM
13059 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13060 {
13061 if (dpyinfo->visual->class == PseudoColor)
13062 {
13063 Lisp_Object value;
13064 value = display_x_get_resource (dpyinfo,
13065 build_string ("privateColormap"),
13066 build_string ("PrivateColormap"),
13067 Qnil, Qnil);
13068 if (STRINGP (value)
13069 && (!strcmp (XSTRING (value)->data, "true")
13070 || !strcmp (XSTRING (value)->data, "on")))
13071 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13072 }
43bd1b2b 13073 }
5ff67d81
GM
13074 else
13075 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13076 dpyinfo->visual, AllocNone);
43bd1b2b 13077
06a2c219
GM
13078 {
13079 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13080 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13081 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13082 dpyinfo->resy = pixels * 25.4 / mm;
13083 pixels = DisplayWidth (dpyinfo->display, screen_number);
13084 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13085 dpyinfo->resx = pixels * 25.4 / mm;
13086 }
13087
334208b7
RS
13088 dpyinfo->Xatom_wm_protocols
13089 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13090 dpyinfo->Xatom_wm_take_focus
13091 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13092 dpyinfo->Xatom_wm_save_yourself
13093 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13094 dpyinfo->Xatom_wm_delete_window
13095 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13096 dpyinfo->Xatom_wm_change_state
13097 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13098 dpyinfo->Xatom_wm_configure_denied
13099 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13100 dpyinfo->Xatom_wm_window_moved
13101 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13102 dpyinfo->Xatom_editres
13103 = XInternAtom (dpyinfo->display, "Editres", False);
13104 dpyinfo->Xatom_CLIPBOARD
13105 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13106 dpyinfo->Xatom_TIMESTAMP
13107 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13108 dpyinfo->Xatom_TEXT
13109 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13110 dpyinfo->Xatom_COMPOUND_TEXT
13111 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13112 dpyinfo->Xatom_DELETE
13113 = XInternAtom (dpyinfo->display, "DELETE", False);
13114 dpyinfo->Xatom_MULTIPLE
13115 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13116 dpyinfo->Xatom_INCR
13117 = XInternAtom (dpyinfo->display, "INCR", False);
13118 dpyinfo->Xatom_EMACS_TMP
13119 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13120 dpyinfo->Xatom_TARGETS
13121 = XInternAtom (dpyinfo->display, "TARGETS", False);
13122 dpyinfo->Xatom_NULL
13123 = XInternAtom (dpyinfo->display, "NULL", False);
13124 dpyinfo->Xatom_ATOM_PAIR
13125 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13126 /* For properties of font. */
13127 dpyinfo->Xatom_PIXEL_SIZE
13128 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13129 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13130 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13131 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13132 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13133 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13134 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13135
06a2c219
GM
13136 /* Ghostscript support. */
13137 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13138 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13139
13140 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13141 False);
13142
547d9db8
KH
13143 dpyinfo->cut_buffers_initialized = 0;
13144
334208b7
RS
13145 connection = ConnectionNumber (dpyinfo->display);
13146 dpyinfo->connection = connection;
13147
dc43ef94 13148 {
5d7cc324
RS
13149 char null_bits[1];
13150
13151 null_bits[0] = 0x00;
dc43ef94
KH
13152
13153 dpyinfo->null_pixel
13154 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13155 null_bits, 1, 1, (long) 0, (long) 0,
13156 1);
13157 }
13158
06a2c219
GM
13159 {
13160 extern int gray_bitmap_width, gray_bitmap_height;
13161 extern unsigned char *gray_bitmap_bits;
13162 dpyinfo->gray
13163 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13164 gray_bitmap_bits,
13165 gray_bitmap_width, gray_bitmap_height,
13166 (unsigned long) 1, (unsigned long) 0, 1);
13167 }
13168
f5d11644
GM
13169#ifdef HAVE_X_I18N
13170 xim_initialize (dpyinfo, resource_name);
13171#endif
13172
87485d6f
MW
13173#ifdef subprocesses
13174 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13175 if (connection != 0)
7a13e894 13176 add_keyboard_wait_descriptor (connection);
87485d6f 13177#endif
6d4238f3 13178
041b69ac 13179#ifndef F_SETOWN_BUG
dc6f92b8 13180#ifdef F_SETOWN
dc6f92b8 13181#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13182 /* stdin is a socket here */
334208b7 13183 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13184#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13185 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13186#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13187#endif /* ! defined (F_SETOWN) */
041b69ac 13188#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13189
13190#ifdef SIGIO
eee20f6a
KH
13191 if (interrupt_input)
13192 init_sigio (connection);
c118dd06 13193#endif /* ! defined (SIGIO) */
dc6f92b8 13194
51b592fb 13195#ifdef USE_LUCID
f8c39f51 13196#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13197 /* Make sure that we have a valid font for dialog boxes
13198 so that Xt does not crash. */
13199 {
13200 Display *dpy = dpyinfo->display;
13201 XrmValue d, fr, to;
13202 Font font;
e99db5a1 13203 int count;
51b592fb
RS
13204
13205 d.addr = (XPointer)&dpy;
13206 d.size = sizeof (Display *);
13207 fr.addr = XtDefaultFont;
13208 fr.size = sizeof (XtDefaultFont);
13209 to.size = sizeof (Font *);
13210 to.addr = (XPointer)&font;
e99db5a1 13211 count = x_catch_errors (dpy);
51b592fb
RS
13212 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13213 abort ();
13214 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13215 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13216 x_uncatch_errors (dpy, count);
51b592fb
RS
13217 }
13218#endif
f8c39f51 13219#endif
51b592fb 13220
34e23e5a
GM
13221 /* See if we should run in synchronous mode. This is useful
13222 for debugging X code. */
13223 {
13224 Lisp_Object value;
13225 value = display_x_get_resource (dpyinfo,
13226 build_string ("synchronous"),
13227 build_string ("Synchronous"),
13228 Qnil, Qnil);
13229 if (STRINGP (value)
13230 && (!strcmp (XSTRING (value)->data, "true")
13231 || !strcmp (XSTRING (value)->data, "on")))
13232 XSynchronize (dpyinfo->display, True);
13233 }
13234
60439948
KH
13235 UNBLOCK_INPUT;
13236
7a13e894
RS
13237 return dpyinfo;
13238}
13239\f
13240/* Get rid of display DPYINFO, assuming all frames are already gone,
13241 and without sending any more commands to the X server. */
dc6f92b8 13242
7a13e894
RS
13243void
13244x_delete_display (dpyinfo)
13245 struct x_display_info *dpyinfo;
13246{
13247 delete_keyboard_wait_descriptor (dpyinfo->connection);
13248
13249 /* Discard this display from x_display_name_list and x_display_list.
13250 We can't use Fdelq because that can quit. */
13251 if (! NILP (x_display_name_list)
8e713be6
KR
13252 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13253 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13254 else
13255 {
13256 Lisp_Object tail;
13257
13258 tail = x_display_name_list;
8e713be6 13259 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13260 {
bffcfca9 13261 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13262 {
8e713be6 13263 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13264 break;
13265 }
8e713be6 13266 tail = XCDR (tail);
7a13e894
RS
13267 }
13268 }
13269
9bda743f
GM
13270 if (next_noop_dpyinfo == dpyinfo)
13271 next_noop_dpyinfo = dpyinfo->next;
13272
7a13e894
RS
13273 if (x_display_list == dpyinfo)
13274 x_display_list = dpyinfo->next;
7f9c7f94
RS
13275 else
13276 {
13277 struct x_display_info *tail;
7a13e894 13278
7f9c7f94
RS
13279 for (tail = x_display_list; tail; tail = tail->next)
13280 if (tail->next == dpyinfo)
13281 tail->next = tail->next->next;
13282 }
7a13e894 13283
0d777288
RS
13284#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13285#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13286 XrmDestroyDatabase (dpyinfo->xrdb);
13287#endif
0d777288 13288#endif
29b38361
KH
13289#ifdef MULTI_KBOARD
13290 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13291 delete_kboard (dpyinfo->kboard);
b9737ad3 13292#endif
f5d11644
GM
13293#ifdef HAVE_X_I18N
13294 if (dpyinfo->xim)
13295 xim_close_dpy (dpyinfo);
13296#endif
13297
b9737ad3
KH
13298 xfree (dpyinfo->font_table);
13299 xfree (dpyinfo->x_id_name);
13300 xfree (dpyinfo);
7a13e894
RS
13301}
13302\f
13303/* Set up use of X before we make the first connection. */
13304
06a2c219
GM
13305static struct redisplay_interface x_redisplay_interface =
13306{
13307 x_produce_glyphs,
13308 x_write_glyphs,
13309 x_insert_glyphs,
13310 x_clear_end_of_line,
13311 x_scroll_run,
13312 x_after_update_window_line,
13313 x_update_window_begin,
13314 x_update_window_end,
13315 XTcursor_to,
13316 x_flush,
66ac4b0e
GM
13317 x_get_glyph_overhangs,
13318 x_fix_overlapping_area
06a2c219
GM
13319};
13320
dfcf069d 13321void
7a13e894
RS
13322x_initialize ()
13323{
06a2c219
GM
13324 rif = &x_redisplay_interface;
13325
13326 clear_frame_hook = x_clear_frame;
13327 ins_del_lines_hook = x_ins_del_lines;
13328 change_line_highlight_hook = x_change_line_highlight;
13329 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13330 ring_bell_hook = XTring_bell;
13331 reset_terminal_modes_hook = XTreset_terminal_modes;
13332 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13333 update_begin_hook = x_update_begin;
13334 update_end_hook = x_update_end;
dc6f92b8
JB
13335 set_terminal_window_hook = XTset_terminal_window;
13336 read_socket_hook = XTread_socket;
b8009dd1 13337 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13338 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13339 mouse_position_hook = XTmouse_position;
f451eb13 13340 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13341 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13342 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13343 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13344 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13345 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13346 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13347
f676886a 13348 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13349 char_ins_del_ok = 0; /* just as fast to write the line */
13350 line_ins_del_ok = 1; /* we'll just blt 'em */
13351 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13352 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13353 off the bottom */
13354 baud_rate = 19200;
13355
7a13e894 13356 x_noop_count = 0;
9ea173e8 13357 last_tool_bar_item = -1;
06a2c219
GM
13358 any_help_event_p = 0;
13359
b30b24cb
RS
13360 /* Try to use interrupt input; if we can't, then start polling. */
13361 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13362
7f9c7f94
RS
13363#ifdef USE_X_TOOLKIT
13364 XtToolkitInitialize ();
13365 Xt_app_con = XtCreateApplicationContext ();
665881ad 13366 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13367
13368 /* Install an asynchronous timer that processes Xt timeout events
13369 every 0.1s. This is necessary because some widget sets use
13370 timeouts internally, for example the LessTif menu bar, or the
13371 Xaw3d scroll bar. When Xt timouts aren't processed, these
13372 widgets don't behave normally. */
13373 {
13374 EMACS_TIME interval;
13375 EMACS_SET_SECS_USECS (interval, 0, 100000);
13376 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13377 }
db74249b 13378#endif
bffcfca9 13379
db74249b 13380#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13381 xaw3d_arrow_scroll = False;
13382 xaw3d_pick_top = True;
7f9c7f94
RS
13383#endif
13384
58769bee 13385 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13386 original error handler. */
e99db5a1 13387 XSetErrorHandler (x_error_handler);
334208b7 13388 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13389
06a2c219 13390 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13391#ifdef SIGWINCH
13392 signal (SIGWINCH, SIG_DFL);
c118dd06 13393#endif /* ! defined (SIGWINCH) */
dc6f92b8 13394
92e2441b 13395 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13396}
55123275 13397
06a2c219 13398
55123275
JB
13399void
13400syms_of_xterm ()
13401{
e99db5a1
RS
13402 staticpro (&x_error_message_string);
13403 x_error_message_string = Qnil;
13404
7a13e894
RS
13405 staticpro (&x_display_name_list);
13406 x_display_name_list = Qnil;
334208b7 13407
ab648270 13408 staticpro (&last_mouse_scroll_bar);
e53cb100 13409 last_mouse_scroll_bar = Qnil;
59e755be
KH
13410
13411 staticpro (&Qvendor_specific_keysyms);
13412 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13413
13414 staticpro (&last_mouse_press_frame);
13415 last_mouse_press_frame = Qnil;
06a2c219
GM
13416
13417 staticpro (&help_echo);
13418 help_echo = Qnil;
13419 staticpro (&previous_help_echo);
13420 previous_help_echo = Qnil;
13421
13422 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13423 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13424For example, if a block cursor is over a tab, it will be drawn as\n\
13425wide as that tab on the display.");
13426 x_stretch_cursor_p = 0;
13427
13428 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13429 "If not nil, Emacs uses toolkit scroll bars.");
13430#if USE_TOOLKIT_SCROLL_BARS
13431 x_toolkit_scroll_bars_p = 1;
13432#else
13433 x_toolkit_scroll_bars_p = 0;
13434#endif
13435
06a2c219
GM
13436 staticpro (&last_mouse_motion_frame);
13437 last_mouse_motion_frame = Qnil;
55123275 13438}
6cf0ae86
RS
13439
13440#endif /* not HAVE_X_WINDOWS */
06a2c219 13441