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