*** empty log message ***
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
1c7e22fd 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8 46
16bd92ea 47#ifndef USG
dc6f92b8
JB
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06
JB
52#endif /* makedev */
53#endif /* USG */
dc6f92b8 54
6df54671 55#ifdef BSD_SYSTEM
dc6f92b8 56#include <sys/ioctl.h>
6df54671 57#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 58
2d368234 59#include "systty.h"
3a2712f9 60#include "systime.h"
dc6f92b8 61
b8009dd1 62#ifndef INCLUDED_FCNTL
dc6f92b8 63#include <fcntl.h>
b8009dd1 64#endif
dc6f92b8
JB
65#include <ctype.h>
66#include <errno.h>
67#include <setjmp.h>
68#include <sys/stat.h>
a0a7635f
RS
69/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70/* #include <sys/param.h> */
dc6f92b8 71
dc43ef94
KH
72#include "charset.h"
73#include "ccl.h"
7a13e894 74#include "frame.h"
dc6f92b8 75#include "dispextern.h"
ee569018 76#include "fontset.h"
dc6f92b8
JB
77#include "termhooks.h"
78#include "termopts.h"
79#include "termchar.h"
80#if 0
81#include "sink.h"
82#include "sinkmask.h"
c118dd06 83#endif /* ! 0 */
dc6f92b8 84#include "gnu.h"
dc6f92b8 85#include "disptab.h"
dc6f92b8 86#include "buffer.h"
f451eb13 87#include "window.h"
3b2fa4e6 88#include "keyboard.h"
bde7c500 89#include "intervals.h"
dfcf069d 90#include "process.h"
bffcfca9 91#include "atimer.h"
dc6f92b8 92
d2bd6bc4
RS
93#ifdef USE_X_TOOLKIT
94#include <X11/Shell.h>
95#endif
96
06a2c219
GM
97#include <sys/types.h>
98#ifdef HAVE_SYS_TIME_H
99#include <sys/time.h>
100#endif
101#ifdef HAVE_UNISTD_H
102#include <unistd.h>
103#endif
104
3afe33e7 105#ifdef USE_X_TOOLKIT
06a2c219 106
9d7e2e3e 107extern void free_frame_menubar ();
2224b905 108extern FRAME_PTR x_menubar_window_to_frame ();
06a2c219 109
0fdff6bb
RS
110#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
111#define HACK_EDITRES
112extern void _XEditResCheckMessages ();
113#endif /* not NO_EDITRES */
06a2c219
GM
114
115/* Include toolkit specific headers for the scroll bar widget. */
116
117#ifdef USE_TOOLKIT_SCROLL_BARS
118#if defined USE_MOTIF
119#include <Xm/Xm.h> /* for LESSTIF_VERSION */
120#include <Xm/ScrollBar.h>
121#include <Xm/ScrollBarP.h>
ec18280f
SM
122#else /* !USE_MOTIF i.e. use Xaw */
123
124#ifdef HAVE_XAW3D
06a2c219 125#include <X11/Xaw3d/Simple.h>
06a2c219
GM
126#include <X11/Xaw3d/Scrollbar.h>
127#define ARROW_SCROLLBAR
128#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
129#else /* !HAVE_XAW3D */
130#include <X11/Xaw/Simple.h>
131#include <X11/Xaw/Scrollbar.h>
132#endif /* !HAVE_XAW3D */
133#ifndef XtNpickTop
134#define XtNpickTop "pickTop"
135#endif /* !XtNpickTop */
136#endif /* !USE_MOTIF */
06a2c219
GM
137#endif /* USE_TOOLKIT_SCROLL_BARS */
138
3afe33e7
RS
139#endif /* USE_X_TOOLKIT */
140
b849c413
RS
141#ifndef USE_X_TOOLKIT
142#define x_any_window_to_frame x_window_to_frame
5627c40e 143#define x_top_window_to_frame x_window_to_frame
b849c413
RS
144#endif
145
546e6d5b 146#ifdef USE_X_TOOLKIT
d067ea8b 147#include "widget.h"
546e6d5b
RS
148#ifndef XtNinitialState
149#define XtNinitialState "initialState"
150#endif
151#endif
152
80528801
KH
153#ifdef SOLARIS2
154/* memmove will be defined as a macro in Xfuncs.h unless
155 <string.h> is included beforehand. The declaration for memmove in
156 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
157#include <string.h>
158#endif
159
e4b68333 160#ifndef min
06a2c219 161#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
162#endif
163#ifndef max
06a2c219
GM
164#define max(a,b) ((a) > (b) ? (a) : (b))
165#endif
166
167#define abs(x) ((x) < 0 ? -(x) : (x))
168
169#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
170
171\f
172/* Bitmaps for truncated lines. */
173
174enum bitmap_type
175{
176 NO_BITMAP,
177 LEFT_TRUNCATION_BITMAP,
178 RIGHT_TRUNCATION_BITMAP,
179 OVERLAY_ARROW_BITMAP,
180 CONTINUED_LINE_BITMAP,
181 CONTINUATION_LINE_BITMAP,
182 ZV_LINE_BITMAP
183};
184
185/* Bitmap drawn to indicate lines not displaying text if
186 `indicate-empty-lines' is non-nil. */
187
188#define zv_width 8
189#define zv_height 8
190static unsigned char zv_bits[] = {
191 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
192
193/* An arrow like this: `<-'. */
194
195#define left_width 8
196#define left_height 8
197static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
199
110859fc
GM
200/* Right truncation arrow bitmap `->'. */
201
202#define right_width 8
203#define right_height 8
204static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
206
06a2c219
GM
207/* Marker for continued lines. */
208
209#define continued_width 8
210#define continued_height 8
211static unsigned char continued_bits[] = {
110859fc
GM
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
213
214/* Marker for continuation lines. */
06a2c219
GM
215
216#define continuation_width 8
217#define continuation_height 8
218static unsigned char continuation_bits[] = {
110859fc 219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 220
110859fc 221/* Overlay arrow bitmap. */
06a2c219 222
110859fc
GM
223#if 0
224/* A bomb. */
06a2c219
GM
225#define ov_width 8
226#define ov_height 8
227static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 229#else
110859fc 230/* A triangular arrow. */
06a2c219
GM
231#define ov_width 8
232#define ov_height 8
233static unsigned char ov_bits[] = {
110859fc
GM
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
235
e4b68333 236#endif
06a2c219
GM
237
238extern Lisp_Object Qhelp_echo;
239
69388238 240\f
06a2c219
GM
241/* Non-zero means Emacs uses toolkit scroll bars. */
242
243int x_toolkit_scroll_bars_p;
244
245/* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
247
248static Lisp_Object help_echo;
249
250/* Temporary variable for XTread_socket. */
251
252static Lisp_Object previous_help_echo;
253
254/* Non-zero means that a HELP_EVENT has been generated since Emacs
255 start. */
256
257static int any_help_event_p;
258
259/* Non-zero means draw block and hollow cursor as wide as the glyph
260 under it. For example, if a block cursor is over a tab, it will be
261 drawn as wide as that tab on the display. */
262
263int x_stretch_cursor_p;
264
265/* This is a chain of structures for all the X displays currently in
266 use. */
267
334208b7 268struct x_display_info *x_display_list;
dc6f92b8 269
06a2c219
GM
270/* This is a list of cons cells, each of the form (NAME
271 . FONT-LIST-CACHE), one for each element of x_display_list and in
272 the same order. NAME is the name of the frame. FONT-LIST-CACHE
273 records previous values returned by x-list-fonts. */
274
7a13e894 275Lisp_Object x_display_name_list;
f451eb13 276
987d2ad1 277/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
278 This is set by update_begin and looked at by all the XT functions.
279 It is zero while not inside an update. In that case, the XT
280 functions assume that `selected_frame' is the frame to apply to. */
281
d0386f2a 282extern struct frame *updating_frame;
dc6f92b8 283
dfcf069d 284extern int waiting_for_input;
0e81d8cd 285
06a2c219
GM
286/* This is a frame waiting to be auto-raised, within XTread_socket. */
287
0134a210
RS
288struct frame *pending_autoraise_frame;
289
7f9c7f94
RS
290#ifdef USE_X_TOOLKIT
291/* The application context for Xt use. */
292XtAppContext Xt_app_con;
06a2c219
GM
293static String Xt_default_resources[] = {0};
294#endif /* USE_X_TOOLKIT */
665881ad 295
06a2c219
GM
296/* Nominal cursor position -- where to draw output.
297 HPOS and VPOS are window relative glyph matrix coordinates.
298 X and Y are window relative pixel coordinates. */
dc6f92b8 299
06a2c219 300struct cursor_pos output_cursor;
dc6f92b8 301
bffcfca9
GM
302/* Non-zero means user is interacting with a toolkit scroll bar. */
303
304static int toolkit_scroll_bar_interaction;
dc6f92b8 305
69388238
RS
306/* Mouse movement.
307
06a2c219 308 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
309 so that we would have to call XQueryPointer after each MotionNotify
310 event to ask for another such event. However, this made mouse tracking
311 slow, and there was a bug that made it eventually stop.
312
313 Simply asking for MotionNotify all the time seems to work better.
314
69388238
RS
315 In order to avoid asking for motion events and then throwing most
316 of them away or busy-polling the server for mouse positions, we ask
317 the server for pointer motion hints. This means that we get only
318 one event per group of mouse movements. "Groups" are delimited by
319 other kinds of events (focus changes and button clicks, for
320 example), or by XQueryPointer calls; when one of these happens, we
321 get another MotionNotify event the next time the mouse moves. This
322 is at least as efficient as getting motion events when mouse
323 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 324 is off. */
69388238
RS
325
326/* Where the mouse was last time we reported a mouse event. */
69388238 327
06a2c219
GM
328FRAME_PTR last_mouse_frame;
329static XRectangle last_mouse_glyph;
2237cac9
RS
330static Lisp_Object last_mouse_press_frame;
331
69388238
RS
332/* The scroll bar in which the last X motion event occurred.
333
06a2c219
GM
334 If the last X motion event occurred in a scroll bar, we set this so
335 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
336 an ordinary motion.
337
06a2c219
GM
338 If the last X motion event didn't occur in a scroll bar, we set
339 this to Qnil, to tell XTmouse_position to return an ordinary motion
340 event. */
341
69388238
RS
342static Lisp_Object last_mouse_scroll_bar;
343
69388238
RS
344/* This is a hack. We would really prefer that XTmouse_position would
345 return the time associated with the position it returns, but there
06a2c219 346 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
347 along with the position query. So, we just keep track of the time
348 of the last movement we received, and return that in hopes that
349 it's somewhat accurate. */
06a2c219 350
69388238
RS
351static Time last_mouse_movement_time;
352
06a2c219
GM
353/* Incremented by XTread_socket whenever it really tries to read
354 events. */
355
c0a04927
RS
356#ifdef __STDC__
357static int volatile input_signal_count;
358#else
359static int input_signal_count;
360#endif
361
7a13e894 362/* Used locally within XTread_socket. */
06a2c219 363
7a13e894 364static int x_noop_count;
dc6f92b8 365
7a13e894 366/* Initial values of argv and argc. */
06a2c219 367
7a13e894
RS
368extern char **initial_argv;
369extern int initial_argc;
dc6f92b8 370
7a13e894 371extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 372
06a2c219 373/* Tells if a window manager is present or not. */
7a13e894
RS
374
375extern Lisp_Object Vx_no_window_manager;
dc6f92b8 376
c2df547c 377extern Lisp_Object Qface, Qmouse_face;
b8009dd1 378
dc6f92b8
JB
379extern int errno;
380
dfeccd2d 381/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 382
64bb1782
RS
383extern int extra_keyboard_modifiers;
384
59e755be
KH
385static Lisp_Object Qvendor_specific_keysyms;
386
334208b7 387extern XrmDatabase x_load_resources ();
c32cdd9a
KH
388extern Lisp_Object x_icon_type ();
389
7a13e894 390
06a2c219
GM
391/* Enumeration for overriding/changing the face to use for drawing
392 glyphs in x_draw_glyphs. */
393
394enum draw_glyphs_face
395{
396 DRAW_NORMAL_TEXT,
397 DRAW_INVERSE_VIDEO,
398 DRAW_CURSOR,
399 DRAW_MOUSE_FACE,
400 DRAW_IMAGE_RAISED,
401 DRAW_IMAGE_SUNKEN
402};
403
404static void x_update_window_end P_ ((struct window *, int));
405static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
406void x_delete_display P_ ((struct x_display_info *));
407static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
408 unsigned));
409static int fast_find_position P_ ((struct window *, int, int *, int *,
410 int *, int *));
411static void set_output_cursor P_ ((struct cursor_pos *));
412static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
413 int *, int *, int *));
414static void note_mode_line_highlight P_ ((struct window *, int, int));
415static void x_check_font P_ ((struct frame *, XFontStruct *));
416static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
417static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
418static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
419static void show_mouse_face P_ ((struct x_display_info *,
420 enum draw_glyphs_face));
421static int x_io_error_quitter P_ ((Display *));
422int x_catch_errors P_ ((Display *));
423void x_uncatch_errors P_ ((Display *, int));
424void x_lower_frame P_ ((struct frame *));
425void x_scroll_bar_clear P_ ((struct frame *));
426int x_had_errors_p P_ ((Display *));
427void x_wm_set_size_hint P_ ((struct frame *, long, int));
428void x_raise_frame P_ ((struct frame *));
429void x_set_window_size P_ ((struct frame *, int, int, int));
430void x_wm_set_window_state P_ ((struct frame *, int));
431void x_wm_set_icon_pixmap P_ ((struct frame *, int));
432void x_initialize P_ ((void));
433static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
434static int x_compute_min_glyph_bounds P_ ((struct frame *));
435static void x_draw_phys_cursor_glyph P_ ((struct window *,
436 struct glyph_row *,
437 enum draw_glyphs_face));
438static void x_update_end P_ ((struct frame *));
439static void XTframe_up_to_date P_ ((struct frame *));
440static void XTreassert_line_highlight P_ ((int, int));
441static void x_change_line_highlight P_ ((int, int, int, int));
442static void XTset_terminal_modes P_ ((void));
443static void XTreset_terminal_modes P_ ((void));
444static void XTcursor_to P_ ((int, int, int, int));
445static void x_write_glyphs P_ ((struct glyph *, int));
446static void x_clear_end_of_line P_ ((int));
447static void x_clear_frame P_ ((void));
448static void x_clear_cursor P_ ((struct window *));
449static void frame_highlight P_ ((struct frame *));
450static void frame_unhighlight P_ ((struct frame *));
451static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
452static void XTframe_rehighlight P_ ((struct frame *));
453static void x_frame_rehighlight P_ ((struct x_display_info *));
454static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 455static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
456static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
457 XRectangle *));
458static void expose_frame P_ ((struct frame *, int, int, int, int));
459static void expose_window_tree P_ ((struct window *, XRectangle *));
460static void expose_window P_ ((struct window *, XRectangle *));
461static void expose_area P_ ((struct window *, struct glyph_row *,
462 XRectangle *, enum glyph_row_area));
463static void expose_line P_ ((struct window *, struct glyph_row *,
464 XRectangle *));
465static void x_update_cursor_in_window_tree P_ ((struct window *, int));
466static void x_update_window_cursor P_ ((struct window *, int));
467static void x_erase_phys_cursor P_ ((struct window *));
468void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
469static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
470 enum bitmap_type));
471
472static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
473 GC, int));
474static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
475static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
476static void note_overwritten_text_cursor P_ ((struct window *, int, int));
477static void x_flush P_ ((struct frame *f));
478
479
480/* Flush display of frame F, or of all frames if F is null. */
481
482static void
483x_flush (f)
484 struct frame *f;
485{
486 BLOCK_INPUT;
487 if (f == NULL)
488 {
489 Lisp_Object rest, frame;
490 FOR_EACH_FRAME (rest, frame)
491 x_flush (XFRAME (frame));
492 }
493 else if (FRAME_X_P (f))
494 XFlush (FRAME_X_DISPLAY (f));
495 UNBLOCK_INPUT;
496}
497
dc6f92b8 498
06a2c219
GM
499/* Remove calls to XFlush by defining XFlush to an empty replacement.
500 Calls to XFlush should be unnecessary because the X output buffer
501 is flushed automatically as needed by calls to XPending,
502 XNextEvent, or XWindowEvent according to the XFlush man page.
503 XTread_socket calls XPending. Removing XFlush improves
504 performance. */
505
506#define XFlush(DISPLAY) (void) 0
b8009dd1 507
334208b7 508\f
06a2c219
GM
509/***********************************************************************
510 Debugging
511 ***********************************************************************/
512
9382638d 513#if 0
06a2c219
GM
514
515/* This is a function useful for recording debugging information about
516 the sequence of occurrences in this file. */
9382638d
KH
517
518struct record
519{
520 char *locus;
521 int type;
522};
523
524struct record event_record[100];
525
526int event_record_index;
527
528record_event (locus, type)
529 char *locus;
530 int type;
531{
532 if (event_record_index == sizeof (event_record) / sizeof (struct record))
533 event_record_index = 0;
534
535 event_record[event_record_index].locus = locus;
536 event_record[event_record_index].type = type;
537 event_record_index++;
538}
539
540#endif /* 0 */
06a2c219
GM
541
542
9382638d 543\f
334208b7
RS
544/* Return the struct x_display_info corresponding to DPY. */
545
546struct x_display_info *
547x_display_info_for_display (dpy)
548 Display *dpy;
549{
550 struct x_display_info *dpyinfo;
551
552 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
553 if (dpyinfo->display == dpy)
554 return dpyinfo;
16bd92ea 555
334208b7
RS
556 return 0;
557}
f451eb13 558
06a2c219
GM
559
560\f
561/***********************************************************************
562 Starting and ending an update
563 ***********************************************************************/
564
565/* Start an update of frame F. This function is installed as a hook
566 for update_begin, i.e. it is called when update_begin is called.
567 This function is called prior to calls to x_update_window_begin for
568 each window being updated. Currently, there is nothing to do here
569 because all interesting stuff is done on a window basis. */
dc6f92b8 570
dfcf069d 571static void
06a2c219 572x_update_begin (f)
f676886a 573 struct frame *f;
58769bee 574{
06a2c219
GM
575 /* Nothing to do. */
576}
dc6f92b8 577
dc6f92b8 578
06a2c219
GM
579/* Start update of window W. Set the global variable updated_window
580 to the window being updated and set output_cursor to the cursor
581 position of W. */
dc6f92b8 582
06a2c219
GM
583static void
584x_update_window_begin (w)
585 struct window *w;
586{
587 struct frame *f = XFRAME (WINDOW_FRAME (w));
588 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
589
590 updated_window = w;
591 set_output_cursor (&w->cursor);
b8009dd1 592
06a2c219 593 BLOCK_INPUT;
d1bc4182 594
06a2c219 595 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 596 {
514e4681 597 /* Don't do highlighting for mouse motion during the update. */
06a2c219 598 display_info->mouse_face_defer = 1;
37c2c98b 599
06a2c219
GM
600 /* If F needs to be redrawn, simply forget about any prior mouse
601 highlighting. */
9f67f20b 602 if (FRAME_GARBAGED_P (f))
06a2c219
GM
603 display_info->mouse_face_window = Qnil;
604
605 /* Can we tell that this update does not affect the window
606 where the mouse highlight is? If so, no need to turn off.
607 Likewise, don't do anything if the frame is garbaged;
608 in that case, the frame's current matrix that we would use
609 is all wrong, and we will redisplay that line anyway. */
610 if (!NILP (display_info->mouse_face_window)
611 && w == XWINDOW (display_info->mouse_face_window))
514e4681 612 {
06a2c219 613 int i;
514e4681 614
06a2c219
GM
615 for (i = 0; i < w->desired_matrix->nrows; ++i)
616 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
617 break;
618
06a2c219
GM
619 if (i < w->desired_matrix->nrows)
620 clear_mouse_face (display_info);
514e4681 621 }
b8009dd1 622 }
6ccf47d1 623
dc6f92b8
JB
624 UNBLOCK_INPUT;
625}
626
06a2c219
GM
627
628/* Draw a vertical window border to the right of window W if W doesn't
629 have vertical scroll bars. */
630
dfcf069d 631static void
06a2c219
GM
632x_draw_vertical_border (w)
633 struct window *w;
58769bee 634{
06a2c219
GM
635 struct frame *f = XFRAME (WINDOW_FRAME (w));
636
637 /* Redraw borders between horizontally adjacent windows. Don't
638 do it for frames with vertical scroll bars because either the
639 right scroll bar of a window, or the left scroll bar of its
640 neighbor will suffice as a border. */
641 if (!WINDOW_RIGHTMOST_P (w)
642 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
643 {
644 int x0, x1, y0, y1;
dc6f92b8 645
06a2c219 646 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 647 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
648 y1 -= 1;
649
650 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
651 f->output_data.x->normal_gc, x1, y0, x1, y1);
652 }
653}
654
655
656/* End update of window W (which is equal to updated_window). Draw
657 vertical borders between horizontally adjacent windows, and display
658 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
659 pseudo-window in case we don't have X toolkit support. Such
660 windows don't have a cursor, so don't display it here. */
661
662static void
663x_update_window_end (w, cursor_on_p)
664 struct window *w;
665 int cursor_on_p;
666{
667 if (!w->pseudo_window_p)
668 {
669 BLOCK_INPUT;
670 if (cursor_on_p)
671 x_display_and_set_cursor (w, 1, output_cursor.hpos,
672 output_cursor.vpos,
673 output_cursor.x, output_cursor.y);
674 x_draw_vertical_border (w);
675 UNBLOCK_INPUT;
676 }
677
678 updated_window = NULL;
679}
dc6f92b8 680
dc6f92b8 681
06a2c219
GM
682/* End update of frame F. This function is installed as a hook in
683 update_end. */
684
685static void
686x_update_end (f)
687 struct frame *f;
688{
689 /* Mouse highlight may be displayed again. */
aa8bff2e 690 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 691
06a2c219 692 BLOCK_INPUT;
334208b7 693 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
694 UNBLOCK_INPUT;
695}
b8009dd1 696
06a2c219
GM
697
698/* This function is called from various places in xdisp.c whenever a
699 complete update has been performed. The global variable
700 updated_window is not available here. */
b8009dd1 701
dfcf069d 702static void
b8009dd1 703XTframe_up_to_date (f)
06a2c219 704 struct frame *f;
b8009dd1 705{
06a2c219 706 if (FRAME_X_P (f))
514e4681 707 {
06a2c219
GM
708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
709 if (dpyinfo->mouse_face_deferred_gc
710 || f == dpyinfo->mouse_face_mouse_frame)
711 {
712 BLOCK_INPUT;
713 if (dpyinfo->mouse_face_mouse_frame)
714 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
715 dpyinfo->mouse_face_mouse_x,
716 dpyinfo->mouse_face_mouse_y);
717 dpyinfo->mouse_face_deferred_gc = 0;
718 UNBLOCK_INPUT;
719 }
514e4681 720 }
b8009dd1 721}
06a2c219
GM
722
723
724/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
725 arrow bitmaps, or clear the areas where they would be displayed
726 before DESIRED_ROW is made current. The window being updated is
727 found in updated_window. This function It is called from
728 update_window_line only if it is known that there are differences
729 between bitmaps to be drawn between current row and DESIRED_ROW. */
730
731static void
732x_after_update_window_line (desired_row)
733 struct glyph_row *desired_row;
734{
735 struct window *w = updated_window;
736
737 xassert (w);
738
739 if (!desired_row->mode_line_p && !w->pseudo_window_p)
740 {
741 BLOCK_INPUT;
742 x_draw_row_bitmaps (w, desired_row);
743
744 /* When a window has disappeared, make sure that no rest of
745 full-width rows stays visible in the internal border. */
746 if (windows_or_buffers_changed)
747 {
748 struct frame *f = XFRAME (w->frame);
749 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
750 int height = desired_row->visible_height;
110859fc
GM
751 int x = (window_box_right (w, -1)
752 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
753 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
754
755 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
756 x, y, width, height, False);
757 }
758
759 UNBLOCK_INPUT;
760 }
761}
762
763
764/* Draw the bitmap WHICH in one of the areas to the left or right of
765 window W. ROW is the glyph row for which to display the bitmap; it
766 determines the vertical position at which the bitmap has to be
767 drawn. */
768
769static void
770x_draw_bitmap (w, row, which)
771 struct window *w;
772 struct glyph_row *row;
773 enum bitmap_type which;
774{
775 struct frame *f = XFRAME (WINDOW_FRAME (w));
776 Display *display = FRAME_X_DISPLAY (f);
777 Window window = FRAME_X_WINDOW (f);
778 int x, y, wd, h, dy;
779 unsigned char *bits;
780 Pixmap pixmap;
781 GC gc = f->output_data.x->normal_gc;
782 struct face *face;
783 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
784
785 /* Must clip because of partially visible lines. */
786 x_clip_to_row (w, row, gc, 1);
787
788 switch (which)
789 {
790 case LEFT_TRUNCATION_BITMAP:
791 wd = left_width;
792 h = left_height;
793 bits = left_bits;
794 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
795 - wd
110859fc 796 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
797 break;
798
799 case OVERLAY_ARROW_BITMAP:
800 wd = left_width;
801 h = left_height;
802 bits = ov_bits;
803 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
804 - wd
110859fc 805 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
806 break;
807
808 case RIGHT_TRUNCATION_BITMAP:
809 wd = right_width;
810 h = right_height;
811 bits = right_bits;
812 x = window_box_right (w, -1);
110859fc 813 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
814 break;
815
816 case CONTINUED_LINE_BITMAP:
817 wd = right_width;
818 h = right_height;
819 bits = continued_bits;
820 x = window_box_right (w, -1);
110859fc 821 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
822 break;
823
824 case CONTINUATION_LINE_BITMAP:
825 wd = continuation_width;
826 h = continuation_height;
827 bits = continuation_bits;
828 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
829 - wd
110859fc 830 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
831 break;
832
833 case ZV_LINE_BITMAP:
834 wd = zv_width;
835 h = zv_height;
836 bits = zv_bits;
837 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
838 - wd
110859fc 839 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
840 break;
841
842 default:
843 abort ();
844 }
845
846 /* Convert to frame coordinates. Set dy to the offset in the row to
847 start drawing the bitmap. */
848 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
849 dy = (row->height - h) / 2;
850
851 /* Draw the bitmap. I believe these small pixmaps can be cached
852 by the server. */
853 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
854 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
855 face->foreground,
856 face->background, depth);
857 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
858 XFreePixmap (display, pixmap);
859 XSetClipMask (display, gc, None);
860}
861
862
863/* Draw flags bitmaps for glyph row ROW on window W. Call this
864 function with input blocked. */
865
866static void
867x_draw_row_bitmaps (w, row)
868 struct window *w;
869 struct glyph_row *row;
870{
871 struct frame *f = XFRAME (w->frame);
872 enum bitmap_type bitmap;
873 struct face *face;
045dee35 874 int header_line_height = -1;
06a2c219
GM
875
876 xassert (interrupt_input_blocked);
877
878 /* If row is completely invisible, because of vscrolling, we
879 don't have to draw anything. */
880 if (row->visible_height <= 0)
881 return;
882
883 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
884 PREPARE_FACE_FOR_DISPLAY (f, face);
885
886 /* Decide which bitmap to draw at the left side. */
887 if (row->overlay_arrow_p)
888 bitmap = OVERLAY_ARROW_BITMAP;
889 else if (row->truncated_on_left_p)
890 bitmap = LEFT_TRUNCATION_BITMAP;
891 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
892 bitmap = CONTINUATION_LINE_BITMAP;
893 else if (row->indicate_empty_line_p)
894 bitmap = ZV_LINE_BITMAP;
895 else
896 bitmap = NO_BITMAP;
897
898 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
899 the flags area. */
900 if (bitmap == NO_BITMAP
110859fc 901 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
902 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
903 {
904 /* If W has a vertical border to its left, don't draw over it. */
905 int border = ((XFASTINT (w->left) > 0
906 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
907 ? 1 : 0);
908 int left = window_box_left (w, -1);
909
045dee35
GM
910 if (header_line_height < 0)
911 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
912
913 /* In case the same realized face is used for bitmap areas and
914 for something displayed in the text (e.g. face `region' on
915 mono-displays, the fill style may have been changed to
916 FillSolid in x_draw_glyph_string_background. */
917 if (face->stipple)
918 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
919 else
920 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
921
06a2c219
GM
922 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
923 face->gc,
924 (left
110859fc 925 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 926 + border),
045dee35 927 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 928 row->y)),
110859fc 929 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 930 row->visible_height);
dcd08bfb
GM
931 if (!face->stipple)
932 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
933 }
934
935 /* Draw the left bitmap. */
936 if (bitmap != NO_BITMAP)
937 x_draw_bitmap (w, row, bitmap);
938
939 /* Decide which bitmap to draw at the right side. */
940 if (row->truncated_on_right_p)
941 bitmap = RIGHT_TRUNCATION_BITMAP;
942 else if (row->continued_p)
943 bitmap = CONTINUED_LINE_BITMAP;
944 else
945 bitmap = NO_BITMAP;
946
947 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
948 the flags area. */
949 if (bitmap == NO_BITMAP
110859fc 950 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
951 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
952 {
953 int right = window_box_right (w, -1);
954
045dee35
GM
955 if (header_line_height < 0)
956 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
957
958 /* In case the same realized face is used for bitmap areas and
959 for something displayed in the text (e.g. face `region' on
960 mono-displays, the fill style may have been changed to
961 FillSolid in x_draw_glyph_string_background. */
962 if (face->stipple)
963 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
964 else
965 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
966 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
967 face->gc,
968 right,
045dee35 969 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 970 row->y)),
110859fc 971 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 972 row->visible_height);
dcd08bfb
GM
973 if (!face->stipple)
974 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
975 }
976
977 /* Draw the right bitmap. */
978 if (bitmap != NO_BITMAP)
979 x_draw_bitmap (w, row, bitmap);
980}
981
dc6f92b8 982\f
06a2c219
GM
983/***********************************************************************
984 Line Highlighting
985 ***********************************************************************/
dc6f92b8 986
06a2c219
GM
987/* External interface to control of standout mode. Not used for X
988 frames. Aborts when called. */
989
990static void
dc6f92b8
JB
991XTreassert_line_highlight (new, vpos)
992 int new, vpos;
993{
06a2c219 994 abort ();
dc6f92b8
JB
995}
996
06a2c219
GM
997
998/* Call this when about to modify line at position VPOS and change
999 whether it is highlighted. Not used for X frames. Aborts when
1000 called. */
dc6f92b8 1001
dfcf069d 1002static void
06a2c219
GM
1003x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1004 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1005{
06a2c219 1006 abort ();
dc6f92b8
JB
1007}
1008
06a2c219
GM
1009
1010/* This is called when starting Emacs and when restarting after
1011 suspend. When starting Emacs, no X window is mapped. And nothing
1012 must be done to Emacs's own window if it is suspended (though that
1013 rarely happens). */
dc6f92b8 1014
dfcf069d 1015static void
dc6f92b8
JB
1016XTset_terminal_modes ()
1017{
1018}
1019
06a2c219
GM
1020/* This is called when exiting or suspending Emacs. Exiting will make
1021 the X-windows go away, and suspending requires no action. */
dc6f92b8 1022
dfcf069d 1023static void
dc6f92b8
JB
1024XTreset_terminal_modes ()
1025{
dc6f92b8 1026}
06a2c219
GM
1027
1028
dc6f92b8 1029\f
06a2c219
GM
1030/***********************************************************************
1031 Output Cursor
1032 ***********************************************************************/
1033
1034/* Set the global variable output_cursor to CURSOR. All cursor
1035 positions are relative to updated_window. */
dc6f92b8 1036
dfcf069d 1037static void
06a2c219
GM
1038set_output_cursor (cursor)
1039 struct cursor_pos *cursor;
dc6f92b8 1040{
06a2c219
GM
1041 output_cursor.hpos = cursor->hpos;
1042 output_cursor.vpos = cursor->vpos;
1043 output_cursor.x = cursor->x;
1044 output_cursor.y = cursor->y;
1045}
1046
1047
1048/* Set a nominal cursor position.
dc6f92b8 1049
06a2c219
GM
1050 HPOS and VPOS are column/row positions in a window glyph matrix. X
1051 and Y are window text area relative pixel positions.
1052
1053 If this is done during an update, updated_window will contain the
1054 window that is being updated and the position is the future output
1055 cursor position for that window. If updated_window is null, use
1056 selected_window and display the cursor at the given position. */
1057
1058static void
1059XTcursor_to (vpos, hpos, y, x)
1060 int vpos, hpos, y, x;
1061{
1062 struct window *w;
1063
1064 /* If updated_window is not set, work on selected_window. */
1065 if (updated_window)
1066 w = updated_window;
1067 else
1068 w = XWINDOW (selected_window);
dbcb258a 1069
06a2c219
GM
1070 /* Set the output cursor. */
1071 output_cursor.hpos = hpos;
1072 output_cursor.vpos = vpos;
1073 output_cursor.x = x;
1074 output_cursor.y = y;
dc6f92b8 1075
06a2c219
GM
1076 /* If not called as part of an update, really display the cursor.
1077 This will also set the cursor position of W. */
1078 if (updated_window == NULL)
dc6f92b8
JB
1079 {
1080 BLOCK_INPUT;
06a2c219 1081 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1082 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1083 UNBLOCK_INPUT;
1084 }
1085}
dc43ef94 1086
06a2c219
GM
1087
1088\f
1089/***********************************************************************
1090 Display Iterator
1091 ***********************************************************************/
1092
1093/* Function prototypes of this page. */
1094
1095static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1096 struct glyph *,
ee569018
KH
1097 XChar2b *,
1098 int *));
06a2c219
GM
1099static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1100 int, XChar2b *, int));
1101static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1102static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1103static void x_append_glyph P_ ((struct it *));
b4192550 1104static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1105static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1106 int, int, double));
1107static void x_produce_glyphs P_ ((struct it *));
06a2c219 1108static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1109
1110
1111/* Return a pointer to per-char metric information in FONT of a
1112 character pointed by B which is a pointer to an XChar2b. */
1113
1114#define PER_CHAR_METRIC(font, b) \
1115 ((font)->per_char \
1116 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1117 + (((font)->min_byte1 || (font)->max_byte1) \
1118 ? (((b)->byte1 - (font)->min_byte1) \
1119 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1120 : 0)) \
1121 : &((font)->max_bounds))
dc43ef94 1122
dc6f92b8 1123
e2ef8ee6
GM
1124/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1125 is not contained in the font. */
dc43ef94 1126
06a2c219 1127static INLINE XCharStruct *
ee569018 1128x_per_char_metric (font, char2b)
06a2c219
GM
1129 XFontStruct *font;
1130 XChar2b *char2b;
1131{
1132 /* The result metric information. */
1133 XCharStruct *pcm = NULL;
dc6f92b8 1134
06a2c219 1135 xassert (font && char2b);
dc6f92b8 1136
06a2c219 1137 if (font->per_char != NULL)
dc6f92b8 1138 {
06a2c219 1139 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1140 {
06a2c219
GM
1141 /* min_char_or_byte2 specifies the linear character index
1142 corresponding to the first element of the per_char array,
1143 max_char_or_byte2 is the index of the last character. A
1144 character with non-zero CHAR2B->byte1 is not in the font.
1145 A character with byte2 less than min_char_or_byte2 or
1146 greater max_char_or_byte2 is not in the font. */
1147 if (char2b->byte1 == 0
1148 && char2b->byte2 >= font->min_char_or_byte2
1149 && char2b->byte2 <= font->max_char_or_byte2)
1150 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1151 }
06a2c219 1152 else
dc6f92b8 1153 {
06a2c219
GM
1154 /* If either min_byte1 or max_byte1 are nonzero, both
1155 min_char_or_byte2 and max_char_or_byte2 are less than
1156 256, and the 2-byte character index values corresponding
1157 to the per_char array element N (counting from 0) are:
1158
1159 byte1 = N/D + min_byte1
1160 byte2 = N\D + min_char_or_byte2
1161
1162 where:
1163
1164 D = max_char_or_byte2 - min_char_or_byte2 + 1
1165 / = integer division
1166 \ = integer modulus */
1167 if (char2b->byte1 >= font->min_byte1
1168 && char2b->byte1 <= font->max_byte1
1169 && char2b->byte2 >= font->min_char_or_byte2
1170 && char2b->byte2 <= font->max_char_or_byte2)
1171 {
1172 pcm = (font->per_char
1173 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1174 * (char2b->byte1 - font->min_byte1))
1175 + (char2b->byte2 - font->min_char_or_byte2));
1176 }
dc6f92b8 1177 }
06a2c219
GM
1178 }
1179 else
1180 {
1181 /* If the per_char pointer is null, all glyphs between the first
1182 and last character indexes inclusive have the same
1183 information, as given by both min_bounds and max_bounds. */
1184 if (char2b->byte2 >= font->min_char_or_byte2
1185 && char2b->byte2 <= font->max_char_or_byte2)
1186 pcm = &font->max_bounds;
1187 }
dc6f92b8 1188
ee569018
KH
1189 return ((pcm == NULL
1190 || pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1191 ? NULL : pcm);
06a2c219 1192}
b73b6aaf 1193
57b03282 1194
06a2c219
GM
1195/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1196 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1197
06a2c219
GM
1198static INLINE void
1199x_encode_char (c, char2b, font_info)
1200 int c;
1201 XChar2b *char2b;
1202 struct font_info *font_info;
1203{
1204 int charset = CHAR_CHARSET (c);
1205 XFontStruct *font = font_info->font;
1206
1207 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1208 This may be either a program in a special encoder language or a
1209 fixed encoding. */
1210 if (font_info->font_encoder)
1211 {
1212 /* It's a program. */
1213 struct ccl_program *ccl = font_info->font_encoder;
1214
1215 if (CHARSET_DIMENSION (charset) == 1)
1216 {
1217 ccl->reg[0] = charset;
1218 ccl->reg[1] = char2b->byte2;
1219 }
1220 else
1221 {
1222 ccl->reg[0] = charset;
1223 ccl->reg[1] = char2b->byte1;
1224 ccl->reg[2] = char2b->byte2;
1225 }
1226
1227 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1228
1229 /* We assume that MSBs are appropriately set/reset by CCL
1230 program. */
1231 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1232 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1233 else
1234 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1235 }
1236 else if (font_info->encoding[charset])
1237 {
1238 /* Fixed encoding scheme. See fontset.h for the meaning of the
1239 encoding numbers. */
1240 int enc = font_info->encoding[charset];
1241
1242 if ((enc == 1 || enc == 2)
1243 && CHARSET_DIMENSION (charset) == 2)
1244 char2b->byte1 |= 0x80;
1245
1246 if (enc == 1 || enc == 3)
1247 char2b->byte2 |= 0x80;
1248 }
1249}
1250
1251
1252/* Get face and two-byte form of character C in face FACE_ID on frame
1253 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1254 means we want to display multibyte text. Value is a pointer to a
1255 realized face that is ready for display. */
1256
1257static INLINE struct face *
1258x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1259 struct frame *f;
1260 int c, face_id;
1261 XChar2b *char2b;
1262 int multibyte_p;
1263{
1264 struct face *face = FACE_FROM_ID (f, face_id);
1265
1266 if (!multibyte_p)
1267 {
1268 /* Unibyte case. We don't have to encode, but we have to make
1269 sure to use a face suitable for unibyte. */
1270 char2b->byte1 = 0;
1271 char2b->byte2 = c;
ee569018
KH
1272 face_id = FACE_FOR_CHAR (f, face, c);
1273 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1274 }
1275 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1276 {
1277 /* Case of ASCII in a face known to fit ASCII. */
1278 char2b->byte1 = 0;
1279 char2b->byte2 = c;
1280 }
1281 else
1282 {
1283 int c1, c2, charset;
1284
1285 /* Split characters into bytes. If c2 is -1 afterwards, C is
1286 really a one-byte character so that byte1 is zero. */
1287 SPLIT_CHAR (c, charset, c1, c2);
1288 if (c2 > 0)
1289 char2b->byte1 = c1, char2b->byte2 = c2;
1290 else
1291 char2b->byte1 = 0, char2b->byte2 = c1;
1292
06a2c219 1293 /* Maybe encode the character in *CHAR2B. */
ee569018 1294 if (face->font != NULL)
06a2c219
GM
1295 {
1296 struct font_info *font_info
1297 = FONT_INFO_FROM_ID (f, face->font_info_id);
1298 if (font_info)
ee569018 1299 x_encode_char (c, char2b, font_info);
06a2c219
GM
1300 }
1301 }
1302
1303 /* Make sure X resources of the face are allocated. */
1304 xassert (face != NULL);
1305 PREPARE_FACE_FOR_DISPLAY (f, face);
1306
1307 return face;
1308}
1309
1310
1311/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1312 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1313 a pointer to a realized face that is ready for display. */
1314
1315static INLINE struct face *
ee569018 1316x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1317 struct frame *f;
1318 struct glyph *glyph;
1319 XChar2b *char2b;
ee569018 1320 int *two_byte_p;
06a2c219
GM
1321{
1322 struct face *face;
1323
1324 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1325 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1326
ee569018
KH
1327 if (two_byte_p)
1328 *two_byte_p = 0;
1329
06a2c219
GM
1330 if (!glyph->multibyte_p)
1331 {
1332 /* Unibyte case. We don't have to encode, but we have to make
1333 sure to use a face suitable for unibyte. */
1334 char2b->byte1 = 0;
43d120d8 1335 char2b->byte2 = glyph->u.ch;
06a2c219 1336 }
43d120d8
KH
1337 else if (glyph->u.ch < 128
1338 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1339 {
1340 /* Case of ASCII in a face known to fit ASCII. */
1341 char2b->byte1 = 0;
43d120d8 1342 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1343 }
1344 else
1345 {
1346 int c1, c2, charset;
1347
1348 /* Split characters into bytes. If c2 is -1 afterwards, C is
1349 really a one-byte character so that byte1 is zero. */
43d120d8 1350 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1351 if (c2 > 0)
1352 char2b->byte1 = c1, char2b->byte2 = c2;
1353 else
1354 char2b->byte1 = 0, char2b->byte2 = c1;
1355
1356 /* Maybe encode the character in *CHAR2B. */
1357 if (charset != CHARSET_ASCII)
1358 {
1359 struct font_info *font_info
1360 = FONT_INFO_FROM_ID (f, face->font_info_id);
1361 if (font_info)
1362 {
43d120d8 1363 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1364 if (two_byte_p)
1365 *two_byte_p
1366 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1367 }
1368 }
1369 }
1370
1371 /* Make sure X resources of the face are allocated. */
1372 xassert (face != NULL);
1373 PREPARE_FACE_FOR_DISPLAY (f, face);
1374 return face;
1375}
1376
1377
1378/* Store one glyph for IT->char_to_display in IT->glyph_row.
1379 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1380
1381static INLINE void
1382x_append_glyph (it)
1383 struct it *it;
1384{
1385 struct glyph *glyph;
1386 enum glyph_row_area area = it->area;
1387
1388 xassert (it->glyph_row);
1389 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1390
1391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1392 if (glyph < it->glyph_row->glyphs[area + 1])
1393 {
1394 /* Play it safe. If sub-structures of the glyph are not all the
1395 same size, it otherwise be that some bits stay set. This
1396 would prevent a comparison with GLYPH_EQUAL_P. */
1397 glyph->u.val = 0;
1398
1399 glyph->type = CHAR_GLYPH;
1400 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1401 glyph->u.ch = it->char_to_display;
1402 glyph->face_id = it->face_id;
06a2c219
GM
1403 glyph->charpos = CHARPOS (it->position);
1404 glyph->object = it->object;
1405 glyph->left_box_line_p = it->start_of_box_run_p;
1406 glyph->right_box_line_p = it->end_of_box_run_p;
1407 glyph->voffset = it->voffset;
1408 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1409 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1410 || it->phys_descent > it->descent);
ee569018 1411 glyph->glyph_not_available_p = it->glyph_not_available_p;
06a2c219
GM
1412 ++it->glyph_row->used[area];
1413 }
1414}
1415
b4192550
KH
1416/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1417 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1418
1419static INLINE void
1420x_append_composite_glyph (it)
1421 struct it *it;
1422{
1423 struct glyph *glyph;
1424 enum glyph_row_area area = it->area;
1425
1426 xassert (it->glyph_row);
1427
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1430 {
1431 /* Play it safe. If sub-structures of the glyph are not all the
1432 same size, it otherwise be that some bits stay set. This
1433 would prevent a comparison with GLYPH_EQUAL_P. */
1434 glyph->u.val = 0;
1435
1436 glyph->type = COMPOSITE_GLYPH;
1437 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1438 glyph->u.cmp_id = it->cmp_id;
1439 glyph->face_id = it->face_id;
b4192550
KH
1440 glyph->charpos = CHARPOS (it->position);
1441 glyph->object = it->object;
1442 glyph->left_box_line_p = it->start_of_box_run_p;
1443 glyph->right_box_line_p = it->end_of_box_run_p;
1444 glyph->voffset = it->voffset;
1445 glyph->multibyte_p = it->multibyte_p;
1446 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1447 || it->phys_descent > it->descent);
1448 ++it->glyph_row->used[area];
1449 }
1450}
1451
06a2c219
GM
1452
1453/* Change IT->ascent and IT->height according to the setting of
1454 IT->voffset. */
1455
1456static INLINE void
1457take_vertical_position_into_account (it)
1458 struct it *it;
1459{
1460 if (it->voffset)
1461 {
1462 if (it->voffset < 0)
1463 /* Increase the ascent so that we can display the text higher
1464 in the line. */
1465 it->ascent += abs (it->voffset);
1466 else
1467 /* Increase the descent so that we can display the text lower
1468 in the line. */
1469 it->descent += it->voffset;
1470 }
1471}
1472
1473
1474/* Produce glyphs/get display metrics for the image IT is loaded with.
1475 See the description of struct display_iterator in dispextern.h for
1476 an overview of struct display_iterator. */
1477
1478static void
1479x_produce_image_glyph (it)
1480 struct it *it;
1481{
1482 struct image *img;
1483 struct face *face;
1484
1485 xassert (it->what == IT_IMAGE);
1486
1487 face = FACE_FROM_ID (it->f, it->face_id);
1488 img = IMAGE_FROM_ID (it->f, it->image_id);
1489 xassert (img);
1490
1491 /* Make sure X resources of the face and image are loaded. */
1492 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1493 prepare_image_for_display (it->f, img);
1494
95af8492 1495 it->ascent = it->phys_ascent = image_ascent (img, face);
66ac4b0e 1496 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1497 it->pixel_width = img->width + 2 * img->margin;
1498
1499 it->nglyphs = 1;
1500
1501 if (face->box != FACE_NO_BOX)
1502 {
1503 it->ascent += face->box_line_width;
1504 it->descent += face->box_line_width;
1505
1506 if (it->start_of_box_run_p)
1507 it->pixel_width += face->box_line_width;
1508 if (it->end_of_box_run_p)
1509 it->pixel_width += face->box_line_width;
1510 }
1511
1512 take_vertical_position_into_account (it);
1513
1514 if (it->glyph_row)
1515 {
1516 struct glyph *glyph;
1517 enum glyph_row_area area = it->area;
1518
1519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1520 if (glyph < it->glyph_row->glyphs[area + 1])
1521 {
1522 glyph->type = IMAGE_GLYPH;
43d120d8
KH
1523 glyph->u.img_id = img->id;
1524 glyph->face_id = it->face_id;
06a2c219
GM
1525 glyph->pixel_width = it->pixel_width;
1526 glyph->charpos = CHARPOS (it->position);
1527 glyph->object = it->object;
1528 glyph->left_box_line_p = it->start_of_box_run_p;
1529 glyph->right_box_line_p = it->end_of_box_run_p;
1530 glyph->voffset = it->voffset;
1531 glyph->multibyte_p = it->multibyte_p;
1532 ++it->glyph_row->used[area];
1533 }
1534 }
1535}
1536
1537
1538/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1539 of the glyph, WIDTH and HEIGHT are the width and height of the
1540 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1541 ascent of the glyph (0 <= ASCENT <= 1). */
1542
1543static void
1544x_append_stretch_glyph (it, object, width, height, ascent)
1545 struct it *it;
1546 Lisp_Object object;
1547 int width, height;
1548 double ascent;
1549{
1550 struct glyph *glyph;
1551 enum glyph_row_area area = it->area;
1552
1553 xassert (ascent >= 0 && ascent <= 1);
1554
1555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1556 if (glyph < it->glyph_row->glyphs[area + 1])
1557 {
1558 glyph->type = STRETCH_GLYPH;
1559 glyph->u.stretch.ascent = height * ascent;
1560 glyph->u.stretch.height = height;
43d120d8 1561 glyph->face_id = it->face_id;
06a2c219
GM
1562 glyph->pixel_width = width;
1563 glyph->charpos = CHARPOS (it->position);
1564 glyph->object = object;
1565 glyph->left_box_line_p = it->start_of_box_run_p;
1566 glyph->right_box_line_p = it->end_of_box_run_p;
1567 glyph->voffset = it->voffset;
1568 glyph->multibyte_p = it->multibyte_p;
1569 ++it->glyph_row->used[area];
1570 }
1571}
1572
1573
1574/* Produce a stretch glyph for iterator IT. IT->object is the value
1575 of the glyph property displayed. The value must be a list
1576 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1577 being recognized:
1578
1579 1. `:width WIDTH' specifies that the space should be WIDTH *
1580 canonical char width wide. WIDTH may be an integer or floating
1581 point number.
1582
1583 2. `:relative-width FACTOR' specifies that the width of the stretch
1584 should be computed from the width of the first character having the
1585 `glyph' property, and should be FACTOR times that width.
1586
1587 3. `:align-to HPOS' specifies that the space should be wide enough
1588 to reach HPOS, a value in canonical character units.
1589
1590 Exactly one of the above pairs must be present.
1591
1592 4. `:height HEIGHT' specifies that the height of the stretch produced
1593 should be HEIGHT, measured in canonical character units.
1594
1595 5. `:relative-height FACTOR' specifies that the height of the the
1596 stretch should be FACTOR times the height of the characters having
1597 the glyph property.
1598
1599 Either none or exactly one of 4 or 5 must be present.
1600
1601 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1602 of the stretch should be used for the ascent of the stretch.
1603 ASCENT must be in the range 0 <= ASCENT <= 100. */
1604
1605#define NUMVAL(X) \
1606 ((INTEGERP (X) || FLOATP (X)) \
1607 ? XFLOATINT (X) \
1608 : - 1)
1609
1610
1611static void
1612x_produce_stretch_glyph (it)
1613 struct it *it;
1614{
1615 /* (space :width WIDTH :height HEIGHT. */
1616 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1617 extern Lisp_Object QCrelative_width, QCrelative_height;
1618 extern Lisp_Object QCalign_to;
1619 Lisp_Object prop, plist;
1620 double width = 0, height = 0, ascent = 0;
1621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1622 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1623
1624 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1625
1626 /* List should start with `space'. */
1627 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1628 plist = XCDR (it->object);
1629
1630 /* Compute the width of the stretch. */
1631 if (prop = Fplist_get (plist, QCwidth),
1632 NUMVAL (prop) > 0)
1633 /* Absolute width `:width WIDTH' specified and valid. */
1634 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1635 else if (prop = Fplist_get (plist, QCrelative_width),
1636 NUMVAL (prop) > 0)
1637 {
1638 /* Relative width `:relative-width FACTOR' specified and valid.
1639 Compute the width of the characters having the `glyph'
1640 property. */
1641 struct it it2;
1642 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1643
1644 it2 = *it;
1645 if (it->multibyte_p)
1646 {
1647 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1648 - IT_BYTEPOS (*it));
1649 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1650 }
1651 else
1652 it2.c = *p, it2.len = 1;
1653
1654 it2.glyph_row = NULL;
1655 it2.what = IT_CHARACTER;
1656 x_produce_glyphs (&it2);
1657 width = NUMVAL (prop) * it2.pixel_width;
1658 }
1659 else if (prop = Fplist_get (plist, QCalign_to),
1660 NUMVAL (prop) > 0)
1661 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1662 else
1663 /* Nothing specified -> width defaults to canonical char width. */
1664 width = CANON_X_UNIT (it->f);
1665
1666 /* Compute height. */
1667 if (prop = Fplist_get (plist, QCheight),
1668 NUMVAL (prop) > 0)
1669 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1670 else if (prop = Fplist_get (plist, QCrelative_height),
1671 NUMVAL (prop) > 0)
1672 height = FONT_HEIGHT (font) * NUMVAL (prop);
1673 else
1674 height = FONT_HEIGHT (font);
1675
1676 /* Compute percentage of height used for ascent. If
1677 `:ascent ASCENT' is present and valid, use that. Otherwise,
1678 derive the ascent from the font in use. */
1679 if (prop = Fplist_get (plist, QCascent),
1680 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1681 ascent = NUMVAL (prop) / 100.0;
1682 else
1683 ascent = (double) font->ascent / FONT_HEIGHT (font);
1684
1685 if (width <= 0)
1686 width = 1;
1687 if (height <= 0)
1688 height = 1;
1689
1690 if (it->glyph_row)
1691 {
1692 Lisp_Object object = it->stack[it->sp - 1].string;
1693 if (!STRINGP (object))
1694 object = it->w->buffer;
1695 x_append_stretch_glyph (it, object, width, height, ascent);
1696 }
1697
1698 it->pixel_width = width;
66ac4b0e
GM
1699 it->ascent = it->phys_ascent = height * ascent;
1700 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1701 it->nglyphs = 1;
1702
1703 if (face->box != FACE_NO_BOX)
1704 {
1705 it->ascent += face->box_line_width;
1706 it->descent += face->box_line_width;
1707
1708 if (it->start_of_box_run_p)
1709 it->pixel_width += face->box_line_width;
1710 if (it->end_of_box_run_p)
1711 it->pixel_width += face->box_line_width;
1712 }
1713
1714 take_vertical_position_into_account (it);
1715}
1716
b4192550
KH
1717/* Return proper value to be used as baseline offset of font that has
1718 ASCENT and DESCENT to draw characters by the font at the vertical
1719 center of the line of frame F.
1720
1721 Here, out task is to find the value of BOFF in the following figure;
1722
1723 -------------------------+-----------+-
1724 -+-+---------+-+ | |
1725 | | | | | |
1726 | | | | F_ASCENT F_HEIGHT
1727 | | | ASCENT | |
1728 HEIGHT | | | | |
1729 | | |-|-+------+-----------|------- baseline
1730 | | | | BOFF | |
1731 | |---------|-+-+ | |
1732 | | | DESCENT | |
1733 -+-+---------+-+ F_DESCENT |
1734 -------------------------+-----------+-
1735
1736 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1737 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1738 DESCENT = FONT->descent
1739 HEIGHT = FONT_HEIGHT (FONT)
1740 F_DESCENT = (F->output_data.x->font->descent
1741 - F->output_data.x->baseline_offset)
1742 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1743*/
1744
1745#define VCENTER_BASELINE_OFFSET(FONT, F) \
1746 ((FONT)->descent \
1747 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1748 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1749
1750/* Produce glyphs/get display metrics for the display element IT is
1751 loaded with. See the description of struct display_iterator in
1752 dispextern.h for an overview of struct display_iterator. */
1753
1754static void
1755x_produce_glyphs (it)
1756 struct it *it;
1757{
ee569018
KH
1758 it->glyph_not_available_p = 0;
1759
06a2c219
GM
1760 if (it->what == IT_CHARACTER)
1761 {
1762 XChar2b char2b;
1763 XFontStruct *font;
ee569018 1764 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1765 XCharStruct *pcm;
06a2c219 1766 int font_not_found_p;
b4192550
KH
1767 struct font_info *font_info;
1768 int boff; /* baseline offset */
06a2c219 1769
ee569018
KH
1770 /* Maybe translate single-byte characters to multibyte, or the
1771 other way. */
06a2c219 1772 it->char_to_display = it->c;
ee569018 1773 if (!ASCII_BYTE_P (it->c))
06a2c219 1774 {
ee569018
KH
1775 if (unibyte_display_via_language_environment
1776 && SINGLE_BYTE_CHAR_P (it->c)
1777 && (it->c >= 0240
1778 || !NILP (Vnonascii_translation_table)))
1779 {
1780 it->char_to_display = unibyte_char_to_multibyte (it->c);
1781 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1782 face = FACE_FROM_ID (it->f, it->face_id);
1783 }
1784 else if (!SINGLE_BYTE_CHAR_P (it->c)
1785 && !it->multibyte_p)
1786 {
1787 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1788 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1789 face = FACE_FROM_ID (it->f, it->face_id);
1790 }
06a2c219
GM
1791 }
1792
ee569018
KH
1793 /* Get font to use. Encode IT->char_to_display. */
1794 x_get_char_face_and_encoding (it->f, it->char_to_display,
1795 it->face_id, &char2b,
1796 it->multibyte_p);
06a2c219
GM
1797 font = face->font;
1798
1799 /* When no suitable font found, use the default font. */
1800 font_not_found_p = font == NULL;
1801 if (font_not_found_p)
b4192550
KH
1802 {
1803 font = FRAME_FONT (it->f);
1804 boff = it->f->output_data.x->baseline_offset;
1805 font_info = NULL;
1806 }
1807 else
1808 {
1809 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1810 boff = font_info->baseline_offset;
1811 if (font_info->vertical_centering)
1812 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1813 }
06a2c219
GM
1814
1815 if (it->char_to_display >= ' '
1816 && (!it->multibyte_p || it->char_to_display < 128))
1817 {
1818 /* Either unibyte or ASCII. */
1819 int stretched_p;
1820
1821 it->nglyphs = 1;
06a2c219
GM
1822
1823 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1824 it->ascent = font->ascent + boff;
1825 it->descent = font->descent - boff;
474848ac
GM
1826
1827 if (pcm)
1828 {
1829 it->phys_ascent = pcm->ascent + boff;
1830 it->phys_descent = pcm->descent - boff;
1831 it->pixel_width = pcm->width;
1832 }
1833 else
1834 {
1835 it->glyph_not_available_p = 1;
1836 it->phys_ascent = font->ascent + boff;
1837 it->phys_descent = font->descent - boff;
1838 it->pixel_width = FONT_WIDTH (font);
1839 }
06a2c219
GM
1840
1841 /* If this is a space inside a region of text with
1842 `space-width' property, change its width. */
1843 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1844 if (stretched_p)
1845 it->pixel_width *= XFLOATINT (it->space_width);
1846
1847 /* If face has a box, add the box thickness to the character
1848 height. If character has a box line to the left and/or
1849 right, add the box line width to the character's width. */
1850 if (face->box != FACE_NO_BOX)
1851 {
1852 int thick = face->box_line_width;
1853
1854 it->ascent += thick;
1855 it->descent += thick;
1856
1857 if (it->start_of_box_run_p)
1858 it->pixel_width += thick;
1859 if (it->end_of_box_run_p)
1860 it->pixel_width += thick;
1861 }
1862
1863 /* If face has an overline, add the height of the overline
1864 (1 pixel) and a 1 pixel margin to the character height. */
1865 if (face->overline_p)
1866 it->ascent += 2;
1867
1868 take_vertical_position_into_account (it);
1869
1870 /* If we have to actually produce glyphs, do it. */
1871 if (it->glyph_row)
1872 {
1873 if (stretched_p)
1874 {
1875 /* Translate a space with a `space-width' property
1876 into a stretch glyph. */
1877 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1878 x_append_stretch_glyph (it, it->object, it->pixel_width,
1879 it->ascent + it->descent, ascent);
1880 }
1881 else
1882 x_append_glyph (it);
1883
1884 /* If characters with lbearing or rbearing are displayed
1885 in this line, record that fact in a flag of the
1886 glyph row. This is used to optimize X output code. */
1c7e22fd 1887 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1888 it->glyph_row->contains_overlapping_glyphs_p = 1;
1889 }
1890 }
1891 else if (it->char_to_display == '\n')
1892 {
1893 /* A newline has no width but we need the height of the line. */
1894 it->pixel_width = 0;
1895 it->nglyphs = 0;
b4192550
KH
1896 it->ascent = it->phys_ascent = font->ascent + boff;
1897 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1898
1899 if (face->box != FACE_NO_BOX)
1900 {
1901 int thick = face->box_line_width;
1902 it->ascent += thick;
1903 it->descent += thick;
1904 }
1905 }
1906 else if (it->char_to_display == '\t')
1907 {
1908 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1909 int x = it->current_x + it->continuation_lines_width;
06a2c219
GM
1910 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1911
1912 it->pixel_width = next_tab_x - x;
1913 it->nglyphs = 1;
b4192550
KH
1914 it->ascent = it->phys_ascent = font->ascent + boff;
1915 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1916
1917 if (it->glyph_row)
1918 {
1919 double ascent = (double) it->ascent / (it->ascent + it->descent);
1920 x_append_stretch_glyph (it, it->object, it->pixel_width,
1921 it->ascent + it->descent, ascent);
1922 }
1923 }
1924 else
1925 {
1926 /* A multi-byte character. Assume that the display width of the
1927 character is the width of the character multiplied by the
b4192550 1928 width of the font. */
06a2c219 1929
b4192550
KH
1930 /* If we found a font, this font should give us the right
1931 metrics. If we didn't find a font, use the frame's
1932 default font and calculate the width of the character
1933 from the charset width; this is what old redisplay code
1934 did. */
1935 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1936 if (font_not_found_p || !pcm)
1937 {
1938 int charset = CHAR_CHARSET (it->char_to_display);
1939
1940 it->glyph_not_available_p = 1;
1941 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1942 * CHARSET_WIDTH (charset));
1943 it->phys_ascent = font->ascent + boff;
1944 it->phys_descent = font->descent - boff;
1945 }
1946 else
1947 {
1948 it->pixel_width = pcm->width;
1949 it->phys_ascent = pcm->ascent + boff;
1950 it->phys_descent = pcm->descent - boff;
1951 if (it->glyph_row
1952 && (pcm->lbearing < 0
1953 || pcm->rbearing > pcm->width))
1954 it->glyph_row->contains_overlapping_glyphs_p = 1;
1955 }
b4192550
KH
1956 it->nglyphs = 1;
1957 it->ascent = font->ascent + boff;
1958 it->descent = font->descent - boff;
06a2c219
GM
1959 if (face->box != FACE_NO_BOX)
1960 {
1961 int thick = face->box_line_width;
1962 it->ascent += thick;
1963 it->descent += thick;
1964
1965 if (it->start_of_box_run_p)
1966 it->pixel_width += thick;
1967 if (it->end_of_box_run_p)
1968 it->pixel_width += thick;
1969 }
1970
1971 /* If face has an overline, add the height of the overline
1972 (1 pixel) and a 1 pixel margin to the character height. */
1973 if (face->overline_p)
1974 it->ascent += 2;
1975
1976 take_vertical_position_into_account (it);
1977
1978 if (it->glyph_row)
1979 x_append_glyph (it);
1980 }
1981 }
b4192550
KH
1982 else if (it->what == IT_COMPOSITION)
1983 {
1984 /* Note: A composition is represented as one glyph in the
1985 glyph matrix. There are no padding glyphs. */
1986 XChar2b char2b;
1987 XFontStruct *font;
ee569018 1988 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
1989 XCharStruct *pcm;
1990 int font_not_found_p;
1991 struct font_info *font_info;
1992 int boff; /* baseline offset */
1993 struct composition *cmp = composition_table[it->cmp_id];
1994
1995 /* Maybe translate single-byte characters to multibyte. */
1996 it->char_to_display = it->c;
1997 if (unibyte_display_via_language_environment
1998 && SINGLE_BYTE_CHAR_P (it->c)
1999 && (it->c >= 0240
2000 || (it->c >= 0200
2001 && !NILP (Vnonascii_translation_table))))
2002 {
2003 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2004 }
2005
2006 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2007 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2008 face = FACE_FROM_ID (it->f, it->face_id);
2009 x_get_char_face_and_encoding (it->f, it->char_to_display,
2010 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2011 font = face->font;
2012
2013 /* When no suitable font found, use the default font. */
2014 font_not_found_p = font == NULL;
2015 if (font_not_found_p)
2016 {
2017 font = FRAME_FONT (it->f);
2018 boff = it->f->output_data.x->baseline_offset;
2019 font_info = NULL;
2020 }
2021 else
2022 {
2023 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2024 boff = font_info->baseline_offset;
2025 if (font_info->vertical_centering)
2026 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2027 }
2028
2029 /* There are no padding glyphs, so there is only one glyph to
2030 produce for the composition. Important is that pixel_width,
2031 ascent and descent are the values of what is drawn by
2032 draw_glyphs (i.e. the values of the overall glyphs composed). */
2033 it->nglyphs = 1;
2034
2035 /* If we have not yet calculated pixel size data of glyphs of
2036 the composition for the current face font, calculate them
2037 now. Theoretically, we have to check all fonts for the
2038 glyphs, but that requires much time and memory space. So,
2039 here we check only the font of the first glyph. This leads
2040 to incorrect display very rarely, and C-l (recenter) can
2041 correct the display anyway. */
2042 if (cmp->font != (void *) font)
2043 {
2044 /* Ascent and descent of the font of the first character of
2045 this composition (adjusted by baseline offset). Ascent
2046 and descent of overall glyphs should not be less than
2047 them respectively. */
2048 int font_ascent = font->ascent + boff;
2049 int font_descent = font->descent - boff;
2050 /* Bounding box of the overall glyphs. */
2051 int leftmost, rightmost, lowest, highest;
2052 int i;
2053
2054 cmp->font = (void *) font;
2055
2056 /* Initialize the bounding box. */
2057 pcm = x_per_char_metric (font, &char2b);
2058 leftmost = 0;
2059 rightmost = pcm->width;
2060 lowest = - pcm->descent + boff;
2061 highest = pcm->ascent + boff;
2062 if (font_info
2063 && font_info->default_ascent
2064 && CHAR_TABLE_P (Vuse_default_ascent)
2065 && !NILP (Faref (Vuse_default_ascent,
2066 make_number (it->char_to_display))))
2067 highest = font_info->default_ascent + boff;
2068
2069 /* Draw the first glyph at the normal position. It may be
2070 shifted to right later if some other glyphs are drawn at
2071 the left. */
2072 cmp->offsets[0] = 0;
2073 cmp->offsets[1] = boff;
2074
2075 /* Set cmp->offsets for the remaining glyphs. */
2076 for (i = 1; i < cmp->glyph_len; i++)
2077 {
2078 int left, right, btm, top;
2079 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2080 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2081
2082 face = FACE_FROM_ID (it->f, face_id);
2083 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2084 it->multibyte_p);
b4192550
KH
2085 font = face->font;
2086 if (font == NULL)
2087 {
2088 font = FRAME_FONT (it->f);
2089 boff = it->f->output_data.x->baseline_offset;
2090 font_info = NULL;
2091 }
2092 else
2093 {
2094 font_info
2095 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2096 boff = font_info->baseline_offset;
2097 if (font_info->vertical_centering)
2098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2099 }
2100
2101 pcm = x_per_char_metric (font, &char2b);
2102
2103 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2104 {
2105 /* Relative composition with or without
2106 alternate chars. */
2107 left = (leftmost + rightmost - pcm->width) / 2;
2108 btm = - pcm->descent + boff;
2109 if (font_info && font_info->relative_compose
2110 && (! CHAR_TABLE_P (Vignore_relative_composition)
2111 || NILP (Faref (Vignore_relative_composition,
2112 make_number (ch)))))
2113 {
2114
2115 if (- pcm->descent
2116 >= font_info->relative_compose)
2117 /* One extra pixel between two glyphs. */
2118 btm = highest + 1;
2119 else if (pcm->ascent <= 0)
2120 /* One extra pixel between two glyphs. */
2121 btm = lowest - 1 - pcm->ascent - pcm->descent;
2122 }
2123 }
2124 else
2125 {
2126 /* A composition rule is specified by an integer
2127 value that encodes global and new reference
2128 points (GREF and NREF). GREF and NREF are
2129 specified by numbers as below:
2130
2131 0---1---2 -- ascent
2132 | |
2133 | |
2134 | |
2135 9--10--11 -- center
2136 | |
2137 ---3---4---5--- baseline
2138 | |
2139 6---7---8 -- descent
2140 */
2141 int rule = COMPOSITION_RULE (cmp, i);
2142 int gref, nref, grefx, grefy, nrefx, nrefy;
2143
2144 COMPOSITION_DECODE_RULE (rule, gref, nref);
2145 grefx = gref % 3, nrefx = nref % 3;
2146 grefy = gref / 3, nrefy = nref / 3;
2147
2148 left = (leftmost
2149 + grefx * (rightmost - leftmost) / 2
2150 - nrefx * pcm->width / 2);
2151 btm = ((grefy == 0 ? highest
2152 : grefy == 1 ? 0
2153 : grefy == 2 ? lowest
2154 : (highest + lowest) / 2)
2155 - (nrefy == 0 ? pcm->ascent + pcm->descent
2156 : nrefy == 1 ? pcm->descent - boff
2157 : nrefy == 2 ? 0
2158 : (pcm->ascent + pcm->descent) / 2));
2159 }
2160
2161 cmp->offsets[i * 2] = left;
2162 cmp->offsets[i * 2 + 1] = btm + pcm->descent;
2163
2164 /* Update the bounding box of the overall glyphs. */
2165 right = left + pcm->width;
2166 top = btm + pcm->descent + pcm->ascent;
2167 if (left < leftmost)
2168 leftmost = left;
2169 if (right > rightmost)
2170 rightmost = right;
2171 if (top > highest)
2172 highest = top;
2173 if (btm < lowest)
2174 lowest = btm;
2175 }
2176
2177 /* If there are glyphs whose x-offsets are negative,
2178 shift all glyphs to the right and make all x-offsets
2179 non-negative. */
2180 if (leftmost < 0)
2181 {
2182 for (i = 0; i < cmp->glyph_len; i++)
2183 cmp->offsets[i * 2] -= leftmost;
2184 rightmost -= leftmost;
2185 }
2186
2187 cmp->pixel_width = rightmost;
2188 cmp->ascent = highest;
2189 cmp->descent = - lowest;
2190 if (cmp->ascent < font_ascent)
2191 cmp->ascent = font_ascent;
2192 if (cmp->descent < font_descent)
2193 cmp->descent = font_descent;
2194 }
2195
2196 it->pixel_width = cmp->pixel_width;
2197 it->ascent = it->phys_ascent = cmp->ascent;
2198 it->descent = it->phys_descent = cmp->descent;
2199
2200 if (face->box != FACE_NO_BOX)
2201 {
2202 int thick = face->box_line_width;
2203 it->ascent += thick;
2204 it->descent += thick;
2205
2206 if (it->start_of_box_run_p)
2207 it->pixel_width += thick;
2208 if (it->end_of_box_run_p)
2209 it->pixel_width += thick;
2210 }
2211
2212 /* If face has an overline, add the height of the overline
2213 (1 pixel) and a 1 pixel margin to the character height. */
2214 if (face->overline_p)
2215 it->ascent += 2;
2216
2217 take_vertical_position_into_account (it);
2218
2219 if (it->glyph_row)
2220 x_append_composite_glyph (it);
2221 }
06a2c219
GM
2222 else if (it->what == IT_IMAGE)
2223 x_produce_image_glyph (it);
2224 else if (it->what == IT_STRETCH)
2225 x_produce_stretch_glyph (it);
2226
2227 /* Accumulate dimensions. */
2228 xassert (it->ascent >= 0 && it->descent > 0);
2229 if (it->area == TEXT_AREA)
2230 it->current_x += it->pixel_width;
66ac4b0e 2231
d365f5bb
GM
2232 it->descent += it->extra_line_spacing;
2233
06a2c219
GM
2234 it->max_ascent = max (it->max_ascent, it->ascent);
2235 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2236 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2237 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2238}
2239
2240
2241/* Estimate the pixel height of the mode or top line on frame F.
2242 FACE_ID specifies what line's height to estimate. */
2243
2244int
2245x_estimate_mode_line_height (f, face_id)
2246 struct frame *f;
2247 enum face_id face_id;
2248{
2249 int height = 1;
2250
2251 /* This function is called so early when Emacs starts that the face
2252 cache and mode line face are not yet initialized. */
2253 if (FRAME_FACE_CACHE (f))
2254 {
2255 struct face *face = FACE_FROM_ID (f, face_id);
2256 if (face)
2257 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2258 }
2259
2260 return height;
2261}
2262
2263\f
2264/***********************************************************************
2265 Glyph display
2266 ***********************************************************************/
2267
2268/* A sequence of glyphs to be drawn in the same face.
2269
2270 This data structure is not really completely X specific, so it
2271 could possibly, at least partially, be useful for other systems. It
2272 is currently not part of the external redisplay interface because
2273 it's not clear what other systems will need. */
2274
2275struct glyph_string
2276{
2277 /* X-origin of the string. */
2278 int x;
2279
2280 /* Y-origin and y-position of the base line of this string. */
2281 int y, ybase;
2282
2283 /* The width of the string, not including a face extension. */
2284 int width;
2285
2286 /* The width of the string, including a face extension. */
2287 int background_width;
2288
2289 /* The height of this string. This is the height of the line this
2290 string is drawn in, and can be different from the height of the
2291 font the string is drawn in. */
2292 int height;
2293
2294 /* Number of pixels this string overwrites in front of its x-origin.
2295 This number is zero if the string has an lbearing >= 0; it is
2296 -lbearing, if the string has an lbearing < 0. */
2297 int left_overhang;
2298
2299 /* Number of pixels this string overwrites past its right-most
2300 nominal x-position, i.e. x + width. Zero if the string's
2301 rbearing is <= its nominal width, rbearing - width otherwise. */
2302 int right_overhang;
2303
2304 /* The frame on which the glyph string is drawn. */
2305 struct frame *f;
2306
2307 /* The window on which the glyph string is drawn. */
2308 struct window *w;
2309
2310 /* X display and window for convenience. */
2311 Display *display;
2312 Window window;
2313
2314 /* The glyph row for which this string was built. It determines the
2315 y-origin and height of the string. */
2316 struct glyph_row *row;
2317
2318 /* The area within row. */
2319 enum glyph_row_area area;
2320
2321 /* Characters to be drawn, and number of characters. */
2322 XChar2b *char2b;
2323 int nchars;
2324
06a2c219
GM
2325 /* A face-override for drawing cursors, mouse face and similar. */
2326 enum draw_glyphs_face hl;
2327
2328 /* Face in which this string is to be drawn. */
2329 struct face *face;
2330
2331 /* Font in which this string is to be drawn. */
2332 XFontStruct *font;
2333
2334 /* Font info for this string. */
2335 struct font_info *font_info;
2336
b4192550
KH
2337 /* Non-null means this string describes (part of) a composition.
2338 All characters from char2b are drawn composed. */
2339 struct composition *cmp;
06a2c219
GM
2340
2341 /* Index of this glyph string's first character in the glyph
b4192550
KH
2342 definition of CMP. If this is zero, this glyph string describes
2343 the first character of a composition. */
06a2c219
GM
2344 int gidx;
2345
2346 /* 1 means this glyph strings face has to be drawn to the right end
2347 of the window's drawing area. */
2348 unsigned extends_to_end_of_line_p : 1;
2349
2350 /* 1 means the background of this string has been drawn. */
2351 unsigned background_filled_p : 1;
2352
2353 /* 1 means glyph string must be drawn with 16-bit functions. */
2354 unsigned two_byte_p : 1;
2355
2356 /* 1 means that the original font determined for drawing this glyph
2357 string could not be loaded. The member `font' has been set to
2358 the frame's default font in this case. */
2359 unsigned font_not_found_p : 1;
2360
2361 /* 1 means that the face in which this glyph string is drawn has a
2362 stipple pattern. */
2363 unsigned stippled_p : 1;
2364
66ac4b0e
GM
2365 /* 1 means only the foreground of this glyph string must be drawn,
2366 and we should use the physical height of the line this glyph
2367 string appears in as clip rect. */
2368 unsigned for_overlaps_p : 1;
2369
06a2c219
GM
2370 /* The GC to use for drawing this glyph string. */
2371 GC gc;
2372
2373 /* A pointer to the first glyph in the string. This glyph
2374 corresponds to char2b[0]. Needed to draw rectangles if
2375 font_not_found_p is 1. */
2376 struct glyph *first_glyph;
2377
2378 /* Image, if any. */
2379 struct image *img;
2380
2381 struct glyph_string *next, *prev;
2382};
2383
2384
5c187dee 2385#if 0
06a2c219
GM
2386
2387static void
2388x_dump_glyph_string (s)
2389 struct glyph_string *s;
2390{
2391 fprintf (stderr, "glyph string\n");
2392 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2393 s->x, s->y, s->width, s->height);
2394 fprintf (stderr, " ybase = %d\n", s->ybase);
2395 fprintf (stderr, " hl = %d\n", s->hl);
2396 fprintf (stderr, " left overhang = %d, right = %d\n",
2397 s->left_overhang, s->right_overhang);
2398 fprintf (stderr, " nchars = %d\n", s->nchars);
2399 fprintf (stderr, " extends to end of line = %d\n",
2400 s->extends_to_end_of_line_p);
2401 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2402 fprintf (stderr, " bg width = %d\n", s->background_width);
2403}
2404
2405#endif /* GLYPH_DEBUG */
2406
2407
2408
2409static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2410 struct glyph_string **,
2411 struct glyph_string *,
2412 struct glyph_string *));
2413static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2414 struct glyph_string **,
2415 struct glyph_string *,
2416 struct glyph_string *));
2417static void x_append_glyph_string P_ ((struct glyph_string **,
2418 struct glyph_string **,
2419 struct glyph_string *));
2420static int x_left_overwritten P_ ((struct glyph_string *));
2421static int x_left_overwriting P_ ((struct glyph_string *));
2422static int x_right_overwritten P_ ((struct glyph_string *));
2423static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2424static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2425 int));
06a2c219
GM
2426static void x_init_glyph_string P_ ((struct glyph_string *,
2427 XChar2b *, struct window *,
2428 struct glyph_row *,
2429 enum glyph_row_area, int,
2430 enum draw_glyphs_face));
2431static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2432 enum glyph_row_area, int, int,
66ac4b0e 2433 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2434static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2435static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2436static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2437 int));
2438static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2439static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2440static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2441static void x_draw_glyph_string P_ ((struct glyph_string *));
2442static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2443static void x_set_cursor_gc P_ ((struct glyph_string *));
2444static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2445static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2446static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2447 int *, int *));
2448static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2449static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2450 unsigned long *, double, int));
06a2c219 2451static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2452 double, int, unsigned long));
06a2c219
GM
2453static void x_setup_relief_colors P_ ((struct glyph_string *));
2454static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2455static void x_draw_image_relief P_ ((struct glyph_string *));
2456static void x_draw_image_foreground P_ ((struct glyph_string *));
2457static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2458static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2459static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2460 int, int, int));
2461static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2462 int, int, int, int, XRectangle *));
2463static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2464 int, int, int, XRectangle *));
66ac4b0e
GM
2465static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2466 enum glyph_row_area));
06a2c219
GM
2467
2468
06a2c219
GM
2469/* Append the list of glyph strings with head H and tail T to the list
2470 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2471
2472static INLINE void
2473x_append_glyph_string_lists (head, tail, h, t)
2474 struct glyph_string **head, **tail;
2475 struct glyph_string *h, *t;
2476{
2477 if (h)
2478 {
2479 if (*head)
2480 (*tail)->next = h;
2481 else
2482 *head = h;
2483 h->prev = *tail;
2484 *tail = t;
2485 }
2486}
2487
2488
2489/* Prepend the list of glyph strings with head H and tail T to the
2490 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2491 result. */
2492
2493static INLINE void
2494x_prepend_glyph_string_lists (head, tail, h, t)
2495 struct glyph_string **head, **tail;
2496 struct glyph_string *h, *t;
2497{
2498 if (h)
2499 {
2500 if (*head)
2501 (*head)->prev = t;
2502 else
2503 *tail = t;
2504 t->next = *head;
2505 *head = h;
2506 }
2507}
2508
2509
2510/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2511 Set *HEAD and *TAIL to the resulting list. */
2512
2513static INLINE void
2514x_append_glyph_string (head, tail, s)
2515 struct glyph_string **head, **tail;
2516 struct glyph_string *s;
2517{
2518 s->next = s->prev = NULL;
2519 x_append_glyph_string_lists (head, tail, s, s);
2520}
2521
2522
2523/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2524 face. */
2525
2526static void
2527x_set_cursor_gc (s)
2528 struct glyph_string *s;
2529{
2530 if (s->font == FRAME_FONT (s->f)
2531 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2532 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2533 && !s->cmp)
06a2c219
GM
2534 s->gc = s->f->output_data.x->cursor_gc;
2535 else
2536 {
2537 /* Cursor on non-default face: must merge. */
2538 XGCValues xgcv;
2539 unsigned long mask;
2540
2541 xgcv.background = s->f->output_data.x->cursor_pixel;
2542 xgcv.foreground = s->face->background;
2543
2544 /* If the glyph would be invisible, try a different foreground. */
2545 if (xgcv.foreground == xgcv.background)
2546 xgcv.foreground = s->face->foreground;
2547 if (xgcv.foreground == xgcv.background)
2548 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2549 if (xgcv.foreground == xgcv.background)
2550 xgcv.foreground = s->face->foreground;
2551
2552 /* Make sure the cursor is distinct from text in this face. */
2553 if (xgcv.background == s->face->background
2554 && xgcv.foreground == s->face->foreground)
2555 {
2556 xgcv.background = s->face->foreground;
2557 xgcv.foreground = s->face->background;
2558 }
2559
2560 IF_DEBUG (x_check_font (s->f, s->font));
2561 xgcv.font = s->font->fid;
2562 xgcv.graphics_exposures = False;
2563 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2564
2565 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2566 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2567 mask, &xgcv);
2568 else
2569 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2570 = XCreateGC (s->display, s->window, mask, &xgcv);
2571
2572 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2573 }
2574}
2575
2576
2577/* Set up S->gc of glyph string S for drawing text in mouse face. */
2578
2579static void
2580x_set_mouse_face_gc (s)
2581 struct glyph_string *s;
2582{
2583 int face_id;
ee569018 2584 struct face *face;
06a2c219
GM
2585
2586 /* What face has to be used for the mouse face? */
2587 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018
KH
2588 face = FACE_FROM_ID (s->f, face_id);
2589 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
06a2c219
GM
2590 s->face = FACE_FROM_ID (s->f, face_id);
2591 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2592
2593 /* If font in this face is same as S->font, use it. */
2594 if (s->font == s->face->font)
2595 s->gc = s->face->gc;
2596 else
2597 {
2598 /* Otherwise construct scratch_cursor_gc with values from FACE
2599 but font FONT. */
2600 XGCValues xgcv;
2601 unsigned long mask;
2602
2603 xgcv.background = s->face->background;
2604 xgcv.foreground = s->face->foreground;
2605 IF_DEBUG (x_check_font (s->f, s->font));
2606 xgcv.font = s->font->fid;
2607 xgcv.graphics_exposures = False;
2608 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2609
2610 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2611 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2612 mask, &xgcv);
2613 else
2614 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2615 = XCreateGC (s->display, s->window, mask, &xgcv);
2616
2617 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2618 }
2619
2620 xassert (s->gc != 0);
2621}
2622
2623
2624/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2625 Faces to use in the mode line have already been computed when the
2626 matrix was built, so there isn't much to do, here. */
2627
2628static INLINE void
2629x_set_mode_line_face_gc (s)
2630 struct glyph_string *s;
2631{
2632 s->gc = s->face->gc;
2633 xassert (s->gc != 0);
2634}
2635
2636
2637/* Set S->gc of glyph string S for drawing that glyph string. Set
2638 S->stippled_p to a non-zero value if the face of S has a stipple
2639 pattern. */
2640
2641static INLINE void
2642x_set_glyph_string_gc (s)
2643 struct glyph_string *s;
2644{
2645 if (s->hl == DRAW_NORMAL_TEXT)
2646 {
2647 s->gc = s->face->gc;
2648 s->stippled_p = s->face->stipple != 0;
2649 }
2650 else if (s->hl == DRAW_INVERSE_VIDEO)
2651 {
2652 x_set_mode_line_face_gc (s);
2653 s->stippled_p = s->face->stipple != 0;
2654 }
2655 else if (s->hl == DRAW_CURSOR)
2656 {
2657 x_set_cursor_gc (s);
2658 s->stippled_p = 0;
2659 }
2660 else if (s->hl == DRAW_MOUSE_FACE)
2661 {
2662 x_set_mouse_face_gc (s);
2663 s->stippled_p = s->face->stipple != 0;
2664 }
2665 else if (s->hl == DRAW_IMAGE_RAISED
2666 || s->hl == DRAW_IMAGE_SUNKEN)
2667 {
2668 s->gc = s->face->gc;
2669 s->stippled_p = s->face->stipple != 0;
2670 }
2671 else
2672 {
2673 s->gc = s->face->gc;
2674 s->stippled_p = s->face->stipple != 0;
2675 }
2676
2677 /* GC must have been set. */
2678 xassert (s->gc != 0);
2679}
2680
2681
2682/* Return in *R the clipping rectangle for glyph string S. */
2683
2684static void
2685x_get_glyph_string_clip_rect (s, r)
2686 struct glyph_string *s;
2687 XRectangle *r;
2688{
2689 if (s->row->full_width_p)
2690 {
2691 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2692 int canon_x = CANON_X_UNIT (s->f);
2693
2694 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2695 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2696
2697 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2698 {
1da3fd71 2699 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2700 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2701 r->x -= width;
2702 }
2703
b9432a85 2704 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2705
06a2c219
GM
2706 /* Unless displaying a mode or menu bar line, which are always
2707 fully visible, clip to the visible part of the row. */
2708 if (s->w->pseudo_window_p)
2709 r->height = s->row->visible_height;
2710 else
2711 r->height = s->height;
2712 }
2713 else
2714 {
2715 /* This is a text line that may be partially visible. */
2716 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2717 r->width = window_box_width (s->w, s->area);
2718 r->height = s->row->visible_height;
2719 }
2720
2721 /* Don't use S->y for clipping because it doesn't take partially
2722 visible lines into account. For example, it can be negative for
2723 partially visible lines at the top of a window. */
2724 if (!s->row->full_width_p
2725 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2726 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2727 else
2728 r->y = max (0, s->row->y);
06a2c219 2729
9ea173e8 2730 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2731 at the top of the window. */
9ea173e8 2732 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2733 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2734
2735 /* If S draws overlapping rows, it's sufficient to use the top and
2736 bottom of the window for clipping because this glyph string
2737 intentionally draws over other lines. */
2738 if (s->for_overlaps_p)
2739 {
045dee35 2740 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2741 r->height = window_text_bottom_y (s->w) - r->y;
2742 }
2743
2744 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2745}
2746
2747
2748/* Set clipping for output of glyph string S. S may be part of a mode
2749 line or menu if we don't have X toolkit support. */
2750
2751static INLINE void
2752x_set_glyph_string_clipping (s)
2753 struct glyph_string *s;
2754{
2755 XRectangle r;
2756 x_get_glyph_string_clip_rect (s, &r);
2757 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2758}
2759
2760
2761/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2762 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2763
2764static INLINE void
2765x_compute_glyph_string_overhangs (s)
2766 struct glyph_string *s;
2767{
b4192550 2768 if (s->cmp == NULL
06a2c219
GM
2769 && s->first_glyph->type == CHAR_GLYPH)
2770 {
2771 XCharStruct cs;
2772 int direction, font_ascent, font_descent;
2773 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2774 &font_ascent, &font_descent, &cs);
2775 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2776 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2777 }
2778}
2779
2780
2781/* Compute overhangs and x-positions for glyph string S and its
2782 predecessors, or successors. X is the starting x-position for S.
2783 BACKWARD_P non-zero means process predecessors. */
2784
2785static void
2786x_compute_overhangs_and_x (s, x, backward_p)
2787 struct glyph_string *s;
2788 int x;
2789 int backward_p;
2790{
2791 if (backward_p)
2792 {
2793 while (s)
2794 {
2795 x_compute_glyph_string_overhangs (s);
2796 x -= s->width;
2797 s->x = x;
2798 s = s->prev;
2799 }
2800 }
2801 else
2802 {
2803 while (s)
2804 {
2805 x_compute_glyph_string_overhangs (s);
2806 s->x = x;
2807 x += s->width;
2808 s = s->next;
2809 }
2810 }
2811}
2812
2813
2814/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2815 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2816 assumed to be zero. */
06a2c219
GM
2817
2818static void
2819x_get_glyph_overhangs (glyph, f, left, right)
2820 struct glyph *glyph;
2821 struct frame *f;
2822 int *left, *right;
2823{
2824 int c;
2825
2826 *left = *right = 0;
2827
b4192550 2828 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2829 {
2830 XFontStruct *font;
2831 struct face *face;
2832 struct font_info *font_info;
2833 XChar2b char2b;
ee569018
KH
2834 XCharStruct *pcm;
2835
2836 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2837 font = face->font;
2838 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2839 if (font
2840 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2841 {
06a2c219
GM
2842 if (pcm->rbearing > pcm->width)
2843 *right = pcm->rbearing - pcm->width;
2844 if (pcm->lbearing < 0)
2845 *left = -pcm->lbearing;
2846 }
2847 }
2848}
2849
2850
2851/* Return the index of the first glyph preceding glyph string S that
2852 is overwritten by S because of S's left overhang. Value is -1
2853 if no glyphs are overwritten. */
2854
2855static int
2856x_left_overwritten (s)
2857 struct glyph_string *s;
2858{
2859 int k;
2860
2861 if (s->left_overhang)
2862 {
2863 int x = 0, i;
2864 struct glyph *glyphs = s->row->glyphs[s->area];
2865 int first = s->first_glyph - glyphs;
2866
2867 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2868 x -= glyphs[i].pixel_width;
2869
2870 k = i + 1;
2871 }
2872 else
2873 k = -1;
2874
2875 return k;
2876}
2877
2878
2879/* Return the index of the first glyph preceding glyph string S that
2880 is overwriting S because of its right overhang. Value is -1 if no
2881 glyph in front of S overwrites S. */
2882
2883static int
2884x_left_overwriting (s)
2885 struct glyph_string *s;
2886{
2887 int i, k, x;
2888 struct glyph *glyphs = s->row->glyphs[s->area];
2889 int first = s->first_glyph - glyphs;
2890
2891 k = -1;
2892 x = 0;
2893 for (i = first - 1; i >= 0; --i)
2894 {
2895 int left, right;
2896 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2897 if (x + right > 0)
2898 k = i;
2899 x -= glyphs[i].pixel_width;
2900 }
2901
2902 return k;
2903}
2904
2905
2906/* Return the index of the last glyph following glyph string S that is
2907 not overwritten by S because of S's right overhang. Value is -1 if
2908 no such glyph is found. */
2909
2910static int
2911x_right_overwritten (s)
2912 struct glyph_string *s;
2913{
2914 int k = -1;
2915
2916 if (s->right_overhang)
2917 {
2918 int x = 0, i;
2919 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2920 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2921 int end = s->row->used[s->area];
2922
2923 for (i = first; i < end && s->right_overhang > x; ++i)
2924 x += glyphs[i].pixel_width;
2925
2926 k = i;
2927 }
2928
2929 return k;
2930}
2931
2932
2933/* Return the index of the last glyph following glyph string S that
2934 overwrites S because of its left overhang. Value is negative
2935 if no such glyph is found. */
2936
2937static int
2938x_right_overwriting (s)
2939 struct glyph_string *s;
2940{
2941 int i, k, x;
2942 int end = s->row->used[s->area];
2943 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2944 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2945
2946 k = -1;
2947 x = 0;
2948 for (i = first; i < end; ++i)
2949 {
2950 int left, right;
2951 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2952 if (x - left < 0)
2953 k = i;
2954 x += glyphs[i].pixel_width;
2955 }
2956
2957 return k;
2958}
2959
2960
2961/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2962
2963static INLINE void
2964x_clear_glyph_string_rect (s, x, y, w, h)
2965 struct glyph_string *s;
2966 int x, y, w, h;
2967{
2968 XGCValues xgcv;
2969 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2970 XSetForeground (s->display, s->gc, xgcv.background);
2971 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2972 XSetForeground (s->display, s->gc, xgcv.foreground);
2973}
2974
2975
2976/* Draw the background of glyph_string S. If S->background_filled_p
2977 is non-zero don't draw it. FORCE_P non-zero means draw the
2978 background even if it wouldn't be drawn normally. This is used
b4192550
KH
2979 when a string preceding S draws into the background of S, or S
2980 contains the first component of a composition. */
06a2c219
GM
2981
2982static void
2983x_draw_glyph_string_background (s, force_p)
2984 struct glyph_string *s;
2985 int force_p;
2986{
2987 /* Nothing to do if background has already been drawn or if it
2988 shouldn't be drawn in the first place. */
2989 if (!s->background_filled_p)
2990 {
b4192550 2991 if (s->stippled_p)
06a2c219
GM
2992 {
2993 /* Fill background with a stipple pattern. */
2994 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2995 XFillRectangle (s->display, s->window, s->gc, s->x,
2996 s->y + s->face->box_line_width,
2997 s->background_width,
2998 s->height - 2 * s->face->box_line_width);
2999 XSetFillStyle (s->display, s->gc, FillSolid);
3000 s->background_filled_p = 1;
3001 }
3002 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3003 || s->font_not_found_p
3004 || s->extends_to_end_of_line_p
06a2c219
GM
3005 || force_p)
3006 {
3007 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3008 s->background_width,
3009 s->height - 2 * s->face->box_line_width);
3010 s->background_filled_p = 1;
3011 }
3012 }
3013}
3014
3015
3016/* Draw the foreground of glyph string S. */
3017
3018static void
3019x_draw_glyph_string_foreground (s)
3020 struct glyph_string *s;
3021{
3022 int i, x;
3023
3024 /* If first glyph of S has a left box line, start drawing the text
3025 of S to the right of that box line. */
3026 if (s->face->box != FACE_NO_BOX
3027 && s->first_glyph->left_box_line_p)
3028 x = s->x + s->face->box_line_width;
3029 else
3030 x = s->x;
3031
b4192550
KH
3032 /* Draw characters of S as rectangles if S's font could not be
3033 loaded. */
3034 if (s->font_not_found_p)
06a2c219 3035 {
b4192550 3036 for (i = 0; i < s->nchars; ++i)
06a2c219 3037 {
b4192550
KH
3038 struct glyph *g = s->first_glyph + i;
3039 XDrawRectangle (s->display, s->window,
3040 s->gc, x, s->y, g->pixel_width - 1,
3041 s->height - 1);
3042 x += g->pixel_width;
06a2c219
GM
3043 }
3044 }
3045 else
3046 {
b4192550
KH
3047 char *char1b = (char *) s->char2b;
3048 int boff = s->font_info->baseline_offset;
06a2c219 3049
b4192550
KH
3050 if (s->font_info->vertical_centering)
3051 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3052
3053 /* If we can use 8-bit functions, condense S->char2b. */
3054 if (!s->two_byte_p)
3055 for (i = 0; i < s->nchars; ++i)
3056 char1b[i] = s->char2b[i].byte2;
3057
3058 /* Draw text with XDrawString if background has already been
3059 filled. Otherwise, use XDrawImageString. (Note that
3060 XDrawImageString is usually faster than XDrawString.) Always
3061 use XDrawImageString when drawing the cursor so that there is
3062 no chance that characters under a box cursor are invisible. */
3063 if (s->for_overlaps_p
3064 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3065 {
3066 /* Draw characters with 16-bit or 8-bit functions. */
3067 if (s->two_byte_p)
3068 XDrawString16 (s->display, s->window, s->gc, x,
3069 s->ybase - boff, s->char2b, s->nchars);
3070 else
3071 XDrawString (s->display, s->window, s->gc, x,
3072 s->ybase - boff, char1b, s->nchars);
3073 }
06a2c219
GM
3074 else
3075 {
b4192550
KH
3076 if (s->two_byte_p)
3077 XDrawImageString16 (s->display, s->window, s->gc, x,
3078 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3079 else
b4192550
KH
3080 XDrawImageString (s->display, s->window, s->gc, x,
3081 s->ybase - boff, char1b, s->nchars);
3082 }
3083 }
3084}
06a2c219 3085
b4192550 3086/* Draw the foreground of composite glyph string S. */
06a2c219 3087
b4192550
KH
3088static void
3089x_draw_composite_glyph_string_foreground (s)
3090 struct glyph_string *s;
3091{
3092 int i, x;
06a2c219 3093
b4192550
KH
3094 /* If first glyph of S has a left box line, start drawing the text
3095 of S to the right of that box line. */
3096 if (s->face->box != FACE_NO_BOX
3097 && s->first_glyph->left_box_line_p)
3098 x = s->x + s->face->box_line_width;
3099 else
3100 x = s->x;
06a2c219 3101
b4192550
KH
3102 /* S is a glyph string for a composition. S->gidx is the index of
3103 the first character drawn for glyphs of this composition.
3104 S->gidx == 0 means we are drawing the very first character of
3105 this composition. */
06a2c219 3106
b4192550
KH
3107 /* Draw a rectangle for the composition if the font for the very
3108 first character of the composition could not be loaded. */
3109 if (s->font_not_found_p)
3110 {
3111 if (s->gidx == 0)
3112 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3113 s->width - 1, s->height - 1);
3114 }
3115 else
3116 {
3117 for (i = 0; i < s->nchars; i++, ++s->gidx)
3118 XDrawString16 (s->display, s->window, s->gc,
3119 x + s->cmp->offsets[s->gidx * 2],
3120 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3121 s->char2b + i, 1);
06a2c219
GM
3122 }
3123}
3124
3125
80c32bcc
GM
3126#ifdef USE_X_TOOLKIT
3127
3128/* Allocate the color COLOR->pixel on the screen and display of
3129 widget WIDGET in colormap CMAP. If an exact match cannot be
3130 allocated, try the nearest color available. Value is non-zero
3131 if successful. This is called from lwlib. */
3132
3133int
3134x_alloc_nearest_color_for_widget (widget, cmap, color)
3135 Widget widget;
3136 Colormap cmap;
3137 XColor *color;
3138{
3139 struct frame *f;
3140 struct x_display_info *dpyinfo;
5c187dee 3141 Lisp_Object tail;
80c32bcc
GM
3142
3143 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3144
3145 /* Find the top-level shell of the widget. Note that this function
3146 can be called when the widget is not yet realized, so XtWindow
3147 (widget) == 0. That's the reason we can't simply use
3148 x_any_window_to_frame. */
3149 while (!XtIsTopLevelShell (widget))
3150 widget = XtParent (widget);
3151
3152 /* Look for a frame with that top-level widget. Allocate the color
3153 on that frame to get the right gamma correction value. */
3154 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3155 if (GC_FRAMEP (XCAR (tail))
3156 && (f = XFRAME (XCAR (tail)),
3157 (f->output_data.nothing != 1
3158 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3159 && f->output_data.x->widget == widget)
3160 return x_alloc_nearest_color (f, cmap, color);
3161
3162 abort ();
3163}
3164
3165#endif /* USE_X_TOOLKIT */
3166
3167
06a2c219
GM
3168/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3169 CMAP. If an exact match can't be allocated, try the nearest color
3170 available. Value is non-zero if successful. Set *COLOR to the
3171 color allocated. */
3172
3173int
80c32bcc
GM
3174x_alloc_nearest_color (f, cmap, color)
3175 struct frame *f;
06a2c219
GM
3176 Colormap cmap;
3177 XColor *color;
3178{
80c32bcc
GM
3179 Display *display = FRAME_X_DISPLAY (f);
3180 Screen *screen = FRAME_X_SCREEN (f);
3181 int rc;
3182
3183 gamma_correct (f, color);
3184 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3185 if (rc == 0)
3186 {
3187 /* If we got to this point, the colormap is full, so we're going
3188 to try to get the next closest color. The algorithm used is
3189 a least-squares matching, which is what X uses for closest
3190 color matching with StaticColor visuals. */
3191 int nearest, i;
3192 unsigned long nearest_delta = ~0;
3193 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3194 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3195
3196 for (i = 0; i < ncells; ++i)
3197 cells[i].pixel = i;
3198 XQueryColors (display, cmap, cells, ncells);
3199
3200 for (nearest = i = 0; i < ncells; ++i)
3201 {
3202 long dred = (color->red >> 8) - (cells[i].red >> 8);
3203 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3204 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3205 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3206
3207 if (delta < nearest_delta)
3208 {
3209 nearest = i;
3210 nearest_delta = delta;
3211 }
3212 }
3213
3214 color->red = cells[nearest].red;
3215 color->green = cells[nearest].green;
3216 color->blue = cells[nearest].blue;
3217 rc = XAllocColor (display, cmap, color);
3218 }
3219
d9c545da
GM
3220#ifdef DEBUG_X_COLORS
3221 if (rc)
3222 register_color (color->pixel);
3223#endif /* DEBUG_X_COLORS */
3224
06a2c219
GM
3225 return rc;
3226}
3227
3228
d9c545da
GM
3229/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3230 It's necessary to do this instead of just using PIXEL directly to
3231 get color reference counts right. */
3232
3233unsigned long
3234x_copy_color (f, pixel)
3235 struct frame *f;
3236 unsigned long pixel;
3237{
3238 XColor color;
3239
3240 color.pixel = pixel;
3241 BLOCK_INPUT;
3242 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3243 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3244 UNBLOCK_INPUT;
3245#ifdef DEBUG_X_COLORS
3246 register_color (pixel);
3247#endif
3248 return color.pixel;
3249}
3250
3251
06a2c219
GM
3252/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3253 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3254 If this produces the same color as PIXEL, try a color where all RGB
3255 values have DELTA added. Return the allocated color in *PIXEL.
3256 DISPLAY is the X display, CMAP is the colormap to operate on.
3257 Value is non-zero if successful. */
3258
3259static int
3260x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3261 struct frame *f;
3262 Display *display;
3263 Colormap cmap;
3264 unsigned long *pixel;
68c45bf0 3265 double factor;
06a2c219
GM
3266 int delta;
3267{
3268 XColor color, new;
3269 int success_p;
3270
3271 /* Get RGB color values. */
3272 color.pixel = *pixel;
3273 XQueryColor (display, cmap, &color);
3274
3275 /* Change RGB values by specified FACTOR. Avoid overflow! */
3276 xassert (factor >= 0);
3277 new.red = min (0xffff, factor * color.red);
3278 new.green = min (0xffff, factor * color.green);
3279 new.blue = min (0xffff, factor * color.blue);
3280
3281 /* Try to allocate the color. */
80c32bcc 3282 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3283 if (success_p)
3284 {
3285 if (new.pixel == *pixel)
3286 {
3287 /* If we end up with the same color as before, try adding
3288 delta to the RGB values. */
0d605c67 3289 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3290
3291 new.red = min (0xffff, delta + color.red);
3292 new.green = min (0xffff, delta + color.green);
3293 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3294 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3295 }
3296 else
3297 success_p = 1;
3298 *pixel = new.pixel;
3299 }
3300
3301 return success_p;
3302}
3303
3304
3305/* Set up the foreground color for drawing relief lines of glyph
3306 string S. RELIEF is a pointer to a struct relief containing the GC
3307 with which lines will be drawn. Use a color that is FACTOR or
3308 DELTA lighter or darker than the relief's background which is found
3309 in S->f->output_data.x->relief_background. If such a color cannot
3310 be allocated, use DEFAULT_PIXEL, instead. */
3311
3312static void
3313x_setup_relief_color (f, relief, factor, delta, default_pixel)
3314 struct frame *f;
3315 struct relief *relief;
68c45bf0 3316 double factor;
06a2c219
GM
3317 int delta;
3318 unsigned long default_pixel;
3319{
3320 XGCValues xgcv;
3321 struct x_output *di = f->output_data.x;
3322 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3323 unsigned long pixel;
3324 unsigned long background = di->relief_background;
43bd1b2b 3325 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3326 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3327 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3328
3329 xgcv.graphics_exposures = False;
3330 xgcv.line_width = 1;
3331
3332 /* Free previously allocated color. The color cell will be reused
3333 when it has been freed as many times as it was allocated, so this
3334 doesn't affect faces using the same colors. */
3335 if (relief->gc
3336 && relief->allocated_p)
3337 {
3338 /* If display has an immutable color map, freeing colors is not
3339 necessary and some servers don't allow it. So don't do it. */
0d605c67 3340 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3341 relief->allocated_p = 0;
3342 }
3343
3344 /* Allocate new color. */
3345 xgcv.foreground = default_pixel;
3346 pixel = background;
dcd08bfb
GM
3347 if (dpyinfo->n_planes != 1
3348 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3349 {
3350 relief->allocated_p = 1;
3351 xgcv.foreground = relief->pixel = pixel;
3352 }
3353
3354 if (relief->gc == 0)
3355 {
dcd08bfb 3356 xgcv.stipple = dpyinfo->gray;
06a2c219 3357 mask |= GCStipple;
dcd08bfb 3358 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3359 }
3360 else
dcd08bfb 3361 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3362}
3363
3364
3365/* Set up colors for the relief lines around glyph string S. */
3366
3367static void
3368x_setup_relief_colors (s)
3369 struct glyph_string *s;
3370{
3371 struct x_output *di = s->f->output_data.x;
3372 unsigned long color;
3373
3374 if (s->face->use_box_color_for_shadows_p)
3375 color = s->face->box_color;
3376 else
3377 {
3378 XGCValues xgcv;
3379
3380 /* Get the background color of the face. */
3381 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3382 color = xgcv.background;
3383 }
3384
3385 if (di->white_relief.gc == 0
3386 || color != di->relief_background)
3387 {
3388 di->relief_background = color;
3389 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3390 WHITE_PIX_DEFAULT (s->f));
3391 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3392 BLACK_PIX_DEFAULT (s->f));
3393 }
3394}
3395
3396
3397/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3398 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3399 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3400 relief. LEFT_P non-zero means draw a relief on the left side of
3401 the rectangle. RIGHT_P non-zero means draw a relief on the right
3402 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3403 when drawing. */
3404
3405static void
3406x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3407 raised_p, left_p, right_p, clip_rect)
3408 struct frame *f;
3409 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3410 XRectangle *clip_rect;
3411{
3412 int i;
3413 GC gc;
3414
3415 if (raised_p)
3416 gc = f->output_data.x->white_relief.gc;
3417 else
3418 gc = f->output_data.x->black_relief.gc;
3419 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3420
3421 /* Top. */
3422 for (i = 0; i < width; ++i)
3423 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3424 left_x + i * left_p, top_y + i,
3425 right_x + 1 - i * right_p, top_y + i);
3426
3427 /* Left. */
3428 if (left_p)
3429 for (i = 0; i < width; ++i)
3430 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3431 left_x + i, top_y + i, left_x + i, bottom_y - i);
3432
3433 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3434 if (raised_p)
3435 gc = f->output_data.x->black_relief.gc;
3436 else
3437 gc = f->output_data.x->white_relief.gc;
3438 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3439
3440 /* Bottom. */
3441 for (i = 0; i < width; ++i)
3442 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3443 left_x + i * left_p, bottom_y - i,
3444 right_x + 1 - i * right_p, bottom_y - i);
3445
3446 /* Right. */
3447 if (right_p)
3448 for (i = 0; i < width; ++i)
3449 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3450 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3451
3452 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3453}
3454
3455
3456/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3457 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3458 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3459 left side of the rectangle. RIGHT_P non-zero means draw a line
3460 on the right side of the rectangle. CLIP_RECT is the clipping
3461 rectangle to use when drawing. */
3462
3463static void
3464x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3465 left_p, right_p, clip_rect)
3466 struct glyph_string *s;
3467 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3468 XRectangle *clip_rect;
3469{
3470 XGCValues xgcv;
3471
3472 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3473 XSetForeground (s->display, s->gc, s->face->box_color);
3474 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3475
3476 /* Top. */
3477 XFillRectangle (s->display, s->window, s->gc,
3478 left_x, top_y, right_x - left_x, width);
3479
3480 /* Left. */
3481 if (left_p)
3482 XFillRectangle (s->display, s->window, s->gc,
3483 left_x, top_y, width, bottom_y - top_y);
3484
3485 /* Bottom. */
3486 XFillRectangle (s->display, s->window, s->gc,
3487 left_x, bottom_y - width, right_x - left_x, width);
3488
3489 /* Right. */
3490 if (right_p)
3491 XFillRectangle (s->display, s->window, s->gc,
3492 right_x - width, top_y, width, bottom_y - top_y);
3493
3494 XSetForeground (s->display, s->gc, xgcv.foreground);
3495 XSetClipMask (s->display, s->gc, None);
3496}
3497
3498
3499/* Draw a box around glyph string S. */
3500
3501static void
3502x_draw_glyph_string_box (s)
3503 struct glyph_string *s;
3504{
3505 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3506 int left_p, right_p;
3507 struct glyph *last_glyph;
3508 XRectangle clip_rect;
3509
3510 last_x = window_box_right (s->w, s->area);
3511 if (s->row->full_width_p
3512 && !s->w->pseudo_window_p)
3513 {
110859fc 3514 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3515 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3516 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3517 }
3518
3519 /* The glyph that may have a right box line. */
b4192550 3520 last_glyph = (s->cmp || s->img
06a2c219
GM
3521 ? s->first_glyph
3522 : s->first_glyph + s->nchars - 1);
3523
3524 width = s->face->box_line_width;
3525 raised_p = s->face->box == FACE_RAISED_BOX;
3526 left_x = s->x;
3527 right_x = ((s->row->full_width_p
1da3fd71 3528 ? last_x - 1
a7aeb2de 3529 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3530 top_y = s->y;
3531 bottom_y = top_y + s->height - 1;
3532
3533 left_p = (s->first_glyph->left_box_line_p
3534 || (s->hl == DRAW_MOUSE_FACE
3535 && (s->prev == NULL
3536 || s->prev->hl != s->hl)));
3537 right_p = (last_glyph->right_box_line_p
3538 || (s->hl == DRAW_MOUSE_FACE
3539 && (s->next == NULL
3540 || s->next->hl != s->hl)));
3541
3542 x_get_glyph_string_clip_rect (s, &clip_rect);
3543
3544 if (s->face->box == FACE_SIMPLE_BOX)
3545 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3546 left_p, right_p, &clip_rect);
3547 else
3548 {
3549 x_setup_relief_colors (s);
3550 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3551 width, raised_p, left_p, right_p, &clip_rect);
3552 }
3553}
3554
3555
3556/* Draw foreground of image glyph string S. */
3557
3558static void
3559x_draw_image_foreground (s)
3560 struct glyph_string *s;
3561{
3562 int x;
95af8492 3563 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3564
3565 /* If first glyph of S has a left box line, start drawing it to the
3566 right of that line. */
3567 if (s->face->box != FACE_NO_BOX
3568 && s->first_glyph->left_box_line_p)
3569 x = s->x + s->face->box_line_width;
3570 else
3571 x = s->x;
3572
3573 /* If there is a margin around the image, adjust x- and y-position
3574 by that margin. */
3575 if (s->img->margin)
3576 {
3577 x += s->img->margin;
3578 y += s->img->margin;
3579 }
3580
3581 if (s->img->pixmap)
3582 {
3583 if (s->img->mask)
3584 {
3585 /* We can't set both a clip mask and use XSetClipRectangles
3586 because the latter also sets a clip mask. We also can't
3587 trust on the shape extension to be available
3588 (XShapeCombineRegion). So, compute the rectangle to draw
3589 manually. */
3590 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3591 | GCFunction);
3592 XGCValues xgcv;
3593 XRectangle clip_rect, image_rect, r;
3594
3595 xgcv.clip_mask = s->img->mask;
3596 xgcv.clip_x_origin = x;
3597 xgcv.clip_y_origin = y;
3598 xgcv.function = GXcopy;
3599 XChangeGC (s->display, s->gc, mask, &xgcv);
3600
3601 x_get_glyph_string_clip_rect (s, &clip_rect);
3602 image_rect.x = x;
3603 image_rect.y = y;
3604 image_rect.width = s->img->width;
3605 image_rect.height = s->img->height;
3606 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3607 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3608 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3609 }
3610 else
3611 {
3612 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3613 0, 0, s->img->width, s->img->height, x, y);
3614
3615 /* When the image has a mask, we can expect that at
3616 least part of a mouse highlight or a block cursor will
3617 be visible. If the image doesn't have a mask, make
3618 a block cursor visible by drawing a rectangle around
3619 the image. I believe it's looking better if we do
3620 nothing here for mouse-face. */
3621 if (s->hl == DRAW_CURSOR)
3622 XDrawRectangle (s->display, s->window, s->gc, x, y,
3623 s->img->width - 1, s->img->height - 1);
3624 }
3625 }
3626 else
3627 /* Draw a rectangle if image could not be loaded. */
3628 XDrawRectangle (s->display, s->window, s->gc, x, y,
3629 s->img->width - 1, s->img->height - 1);
3630}
3631
3632
3633/* Draw a relief around the image glyph string S. */
3634
3635static void
3636x_draw_image_relief (s)
3637 struct glyph_string *s;
3638{
3639 int x0, y0, x1, y1, thick, raised_p;
3640 XRectangle r;
3641 int x;
95af8492 3642 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3643
3644 /* If first glyph of S has a left box line, start drawing it to the
3645 right of that line. */
3646 if (s->face->box != FACE_NO_BOX
3647 && s->first_glyph->left_box_line_p)
3648 x = s->x + s->face->box_line_width;
3649 else
3650 x = s->x;
3651
3652 /* If there is a margin around the image, adjust x- and y-position
3653 by that margin. */
3654 if (s->img->margin)
3655 {
3656 x += s->img->margin;
3657 y += s->img->margin;
3658 }
3659
3660 if (s->hl == DRAW_IMAGE_SUNKEN
3661 || s->hl == DRAW_IMAGE_RAISED)
3662 {
9ea173e8 3663 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3664 raised_p = s->hl == DRAW_IMAGE_RAISED;
3665 }
3666 else
3667 {
3668 thick = abs (s->img->relief);
3669 raised_p = s->img->relief > 0;
3670 }
3671
3672 x0 = x - thick;
3673 y0 = y - thick;
3674 x1 = x + s->img->width + thick - 1;
3675 y1 = y + s->img->height + thick - 1;
3676
3677 x_setup_relief_colors (s);
3678 x_get_glyph_string_clip_rect (s, &r);
3679 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3680}
3681
3682
3683/* Draw the foreground of image glyph string S to PIXMAP. */
3684
3685static void
3686x_draw_image_foreground_1 (s, pixmap)
3687 struct glyph_string *s;
3688 Pixmap pixmap;
3689{
3690 int x;
95af8492 3691 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3692
3693 /* If first glyph of S has a left box line, start drawing it to the
3694 right of that line. */
3695 if (s->face->box != FACE_NO_BOX
3696 && s->first_glyph->left_box_line_p)
3697 x = s->face->box_line_width;
3698 else
3699 x = 0;
3700
3701 /* If there is a margin around the image, adjust x- and y-position
3702 by that margin. */
3703 if (s->img->margin)
3704 {
3705 x += s->img->margin;
3706 y += s->img->margin;
3707 }
dc43ef94 3708
06a2c219
GM
3709 if (s->img->pixmap)
3710 {
3711 if (s->img->mask)
3712 {
3713 /* We can't set both a clip mask and use XSetClipRectangles
3714 because the latter also sets a clip mask. We also can't
3715 trust on the shape extension to be available
3716 (XShapeCombineRegion). So, compute the rectangle to draw
3717 manually. */
3718 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3719 | GCFunction);
3720 XGCValues xgcv;
3721
3722 xgcv.clip_mask = s->img->mask;
3723 xgcv.clip_x_origin = x;
3724 xgcv.clip_y_origin = y;
3725 xgcv.function = GXcopy;
3726 XChangeGC (s->display, s->gc, mask, &xgcv);
3727
3728 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3729 0, 0, s->img->width, s->img->height, x, y);
3730 XSetClipMask (s->display, s->gc, None);
3731 }
3732 else
3733 {
3734 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3735 0, 0, s->img->width, s->img->height, x, y);
3736
3737 /* When the image has a mask, we can expect that at
3738 least part of a mouse highlight or a block cursor will
3739 be visible. If the image doesn't have a mask, make
3740 a block cursor visible by drawing a rectangle around
3741 the image. I believe it's looking better if we do
3742 nothing here for mouse-face. */
3743 if (s->hl == DRAW_CURSOR)
3744 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3745 s->img->width - 1, s->img->height - 1);
3746 }
3747 }
3748 else
3749 /* Draw a rectangle if image could not be loaded. */
3750 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3751 s->img->width - 1, s->img->height - 1);
3752}
dc43ef94 3753
990ba854 3754
06a2c219
GM
3755/* Draw part of the background of glyph string S. X, Y, W, and H
3756 give the rectangle to draw. */
a9a5b0a5 3757
06a2c219
GM
3758static void
3759x_draw_glyph_string_bg_rect (s, x, y, w, h)
3760 struct glyph_string *s;
3761 int x, y, w, h;
3762{
3763 if (s->stippled_p)
3764 {
3765 /* Fill background with a stipple pattern. */
3766 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3767 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3768 XSetFillStyle (s->display, s->gc, FillSolid);
3769 }
3770 else
3771 x_clear_glyph_string_rect (s, x, y, w, h);
3772}
07e34cb0 3773
b5210ea7 3774
06a2c219 3775/* Draw image glyph string S.
dc43ef94 3776
06a2c219
GM
3777 s->y
3778 s->x +-------------------------
3779 | s->face->box
3780 |
3781 | +-------------------------
3782 | | s->img->margin
3783 | |
3784 | | +-------------------
3785 | | | the image
dc43ef94 3786
06a2c219 3787 */
dc43ef94 3788
06a2c219
GM
3789static void
3790x_draw_image_glyph_string (s)
3791 struct glyph_string *s;
3792{
3793 int x, y;
3794 int box_line_width = s->face->box_line_width;
3795 int margin = s->img->margin;
3796 int height;
3797 Pixmap pixmap = None;
3798
3799 height = s->height - 2 * box_line_width;
3800
3801 /* Fill background with face under the image. Do it only if row is
3802 taller than image or if image has a clip mask to reduce
3803 flickering. */
3804 s->stippled_p = s->face->stipple != 0;
3805 if (height > s->img->height
3806 || margin
3807 || s->img->mask
3808 || s->img->pixmap == 0
3809 || s->width != s->background_width)
3810 {
3811 if (box_line_width && s->first_glyph->left_box_line_p)
3812 x = s->x + box_line_width;
3813 else
3814 x = s->x;
3815
3816 y = s->y + box_line_width;
3817
3818 if (s->img->mask)
3819 {
3820 /* Create a pixmap as large as the glyph string Fill it with
3821 the background color. Copy the image to it, using its
3822 mask. Copy the temporary pixmap to the display. */
3823 Screen *screen = FRAME_X_SCREEN (s->f);
3824 int depth = DefaultDepthOfScreen (screen);
3825
3826 /* Create a pixmap as large as the glyph string. */
3827 pixmap = XCreatePixmap (s->display, s->window,
3828 s->background_width,
3829 s->height, depth);
3830
3831 /* Don't clip in the following because we're working on the
3832 pixmap. */
3833 XSetClipMask (s->display, s->gc, None);
3834
3835 /* Fill the pixmap with the background color/stipple. */
3836 if (s->stippled_p)
3837 {
3838 /* Fill background with a stipple pattern. */
3839 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3840 XFillRectangle (s->display, pixmap, s->gc,
3841 0, 0, s->background_width, s->height);
3842 XSetFillStyle (s->display, s->gc, FillSolid);
3843 }
3844 else
3845 {
3846 XGCValues xgcv;
3847 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3848 &xgcv);
3849 XSetForeground (s->display, s->gc, xgcv.background);
3850 XFillRectangle (s->display, pixmap, s->gc,
3851 0, 0, s->background_width, s->height);
3852 XSetForeground (s->display, s->gc, xgcv.foreground);
3853 }
3854 }
3855 else
3856 /* Implementation idea: Is it possible to construct a mask?
3857 We could look at the color at the margins of the image, and
3858 say that this color is probably the background color of the
3859 image. */
3860 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3861
3862 s->background_filled_p = 1;
3863 }
dc43ef94 3864
06a2c219
GM
3865 /* Draw the foreground. */
3866 if (pixmap != None)
3867 {
3868 x_draw_image_foreground_1 (s, pixmap);
3869 x_set_glyph_string_clipping (s);
3870 XCopyArea (s->display, pixmap, s->window, s->gc,
3871 0, 0, s->background_width, s->height, s->x, s->y);
3872 XFreePixmap (s->display, pixmap);
3873 }
3874 else
3875 x_draw_image_foreground (s);
b5210ea7 3876
06a2c219
GM
3877 /* If we must draw a relief around the image, do it. */
3878 if (s->img->relief
3879 || s->hl == DRAW_IMAGE_RAISED
3880 || s->hl == DRAW_IMAGE_SUNKEN)
3881 x_draw_image_relief (s);
3882}
8c1a6a84 3883
990ba854 3884
06a2c219 3885/* Draw stretch glyph string S. */
dc43ef94 3886
06a2c219
GM
3887static void
3888x_draw_stretch_glyph_string (s)
3889 struct glyph_string *s;
3890{
3891 xassert (s->first_glyph->type == STRETCH_GLYPH);
3892 s->stippled_p = s->face->stipple != 0;
990ba854 3893
06a2c219
GM
3894 if (s->hl == DRAW_CURSOR
3895 && !x_stretch_cursor_p)
3896 {
3897 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3898 as wide as the stretch glyph. */
3899 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3900
06a2c219
GM
3901 /* Draw cursor. */
3902 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3903
06a2c219
GM
3904 /* Clear rest using the GC of the original non-cursor face. */
3905 if (width < s->background_width)
3906 {
3907 GC gc = s->face->gc;
3908 int x = s->x + width, y = s->y;
3909 int w = s->background_width - width, h = s->height;
3910 XRectangle r;
dc43ef94 3911
06a2c219
GM
3912 x_get_glyph_string_clip_rect (s, &r);
3913 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3914
06a2c219
GM
3915 if (s->face->stipple)
3916 {
3917 /* Fill background with a stipple pattern. */
3918 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3919 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3920 XSetFillStyle (s->display, gc, FillSolid);
3921 }
3922 else
3923 {
3924 XGCValues xgcv;
3925 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3926 XSetForeground (s->display, gc, xgcv.background);
3927 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3928 XSetForeground (s->display, gc, xgcv.foreground);
3929 }
3930 }
3931 }
3932 else
3933 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3934 s->height);
3935
3936 s->background_filled_p = 1;
3937}
3938
3939
3940/* Draw glyph string S. */
3941
3942static void
3943x_draw_glyph_string (s)
3944 struct glyph_string *s;
3945{
3946 /* If S draws into the background of its successor, draw the
3947 background of the successor first so that S can draw into it.
3948 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3949 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3950 {
3951 xassert (s->next->img == NULL);
3952 x_set_glyph_string_gc (s->next);
3953 x_set_glyph_string_clipping (s->next);
3954 x_draw_glyph_string_background (s->next, 1);
3955 }
97210f4e 3956
06a2c219
GM
3957 /* Set up S->gc, set clipping and draw S. */
3958 x_set_glyph_string_gc (s);
3959 x_set_glyph_string_clipping (s);
3960
3961 switch (s->first_glyph->type)
3962 {
3963 case IMAGE_GLYPH:
3964 x_draw_image_glyph_string (s);
3965 break;
3966
3967 case STRETCH_GLYPH:
3968 x_draw_stretch_glyph_string (s);
3969 break;
3970
3971 case CHAR_GLYPH:
66ac4b0e
GM
3972 if (s->for_overlaps_p)
3973 s->background_filled_p = 1;
3974 else
3975 x_draw_glyph_string_background (s, 0);
06a2c219
GM
3976 x_draw_glyph_string_foreground (s);
3977 break;
3978
b4192550
KH
3979 case COMPOSITE_GLYPH:
3980 if (s->for_overlaps_p || s->gidx > 0)
3981 s->background_filled_p = 1;
3982 else
3983 x_draw_glyph_string_background (s, 1);
3984 x_draw_composite_glyph_string_foreground (s);
3985 break;
3986
06a2c219
GM
3987 default:
3988 abort ();
3989 }
3990
66ac4b0e 3991 if (!s->for_overlaps_p)
06a2c219 3992 {
66ac4b0e
GM
3993 /* Draw underline. */
3994 if (s->face->underline_p)
3995 {
3996 unsigned long dy, h;
06a2c219 3997
66ac4b0e
GM
3998 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3999 h = 1;
4000 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4001 dy = s->height - h;
06a2c219 4002
66ac4b0e
GM
4003 if (s->face->underline_defaulted_p)
4004 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4005 s->width, h);
4006 else
4007 {
4008 XGCValues xgcv;
4009 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4010 XSetForeground (s->display, s->gc, s->face->underline_color);
4011 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4012 s->width, h);
4013 XSetForeground (s->display, s->gc, xgcv.foreground);
4014 }
dc6f92b8 4015 }
07e34cb0 4016
66ac4b0e
GM
4017 /* Draw overline. */
4018 if (s->face->overline_p)
06a2c219 4019 {
66ac4b0e
GM
4020 unsigned long dy = 0, h = 1;
4021
4022 if (s->face->overline_color_defaulted_p)
4023 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4024 s->width, h);
4025 else
4026 {
4027 XGCValues xgcv;
4028 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4029 XSetForeground (s->display, s->gc, s->face->overline_color);
4030 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4031 s->width, h);
4032 XSetForeground (s->display, s->gc, xgcv.foreground);
4033 }
06a2c219 4034 }
06a2c219 4035
66ac4b0e
GM
4036 /* Draw strike-through. */
4037 if (s->face->strike_through_p)
06a2c219 4038 {
66ac4b0e
GM
4039 unsigned long h = 1;
4040 unsigned long dy = (s->height - h) / 2;
4041
4042 if (s->face->strike_through_color_defaulted_p)
4043 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4044 s->width, h);
4045 else
4046 {
4047 XGCValues xgcv;
4048 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4049 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4050 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4051 s->width, h);
4052 XSetForeground (s->display, s->gc, xgcv.foreground);
4053 }
06a2c219 4054 }
06a2c219 4055
66ac4b0e
GM
4056 /* Draw relief. */
4057 if (s->face->box != FACE_NO_BOX)
4058 x_draw_glyph_string_box (s);
4059 }
06a2c219
GM
4060
4061 /* Reset clipping. */
4062 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4063}
07e34cb0 4064
06a2c219 4065
b4192550
KH
4066static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4067 struct face **, int));
06a2c219 4068
06a2c219 4069
b4192550
KH
4070/* Load glyph string S with a composition components specified by S->cmp.
4071 FACES is an array of faces for all components of this composition.
4072 S->gidx is the index of the first component for S.
4073 OVERLAPS_P non-zero means S should draw the foreground only, and
4074 use its lines physical height for clipping.
06a2c219 4075
b4192550 4076 Value is the index of a component not in S. */
07e34cb0 4077
b4192550
KH
4078static int
4079x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4080 struct glyph_string *s;
b4192550 4081 struct face **faces;
66ac4b0e 4082 int overlaps_p;
07e34cb0 4083{
b4192550 4084 int i;
06a2c219 4085
b4192550 4086 xassert (s);
06a2c219 4087
b4192550 4088 s->for_overlaps_p = overlaps_p;
06a2c219 4089
b4192550
KH
4090 s->face = faces[s->gidx];
4091 s->font = s->face->font;
4092 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4093
b4192550
KH
4094 /* For all glyphs of this composition, starting at the offset
4095 S->gidx, until we reach the end of the definition or encounter a
4096 glyph that requires the different face, add it to S. */
4097 ++s->nchars;
4098 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4099 ++s->nchars;
06a2c219 4100
b4192550
KH
4101 /* All glyph strings for the same composition has the same width,
4102 i.e. the width set for the first component of the composition. */
06a2c219 4103
06a2c219
GM
4104 s->width = s->first_glyph->pixel_width;
4105
4106 /* If the specified font could not be loaded, use the frame's
4107 default font, but record the fact that we couldn't load it in
4108 the glyph string so that we can draw rectangles for the
4109 characters of the glyph string. */
4110 if (s->font == NULL)
4111 {
4112 s->font_not_found_p = 1;
4113 s->font = FRAME_FONT (s->f);
4114 }
4115
4116 /* Adjust base line for subscript/superscript text. */
4117 s->ybase += s->first_glyph->voffset;
4118
4119 xassert (s->face && s->face->gc);
4120
4121 /* This glyph string must always be drawn with 16-bit functions. */
4122 s->two_byte_p = 1;
b4192550
KH
4123
4124 return s->gidx + s->nchars;
06a2c219
GM
4125}
4126
4127
b4192550 4128/* Load glyph string S with a sequence characters.
06a2c219 4129 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4130 first glyph to consider, END is the index of the last + 1.
4131 OVERLAPS_P non-zero means S should draw the foreground only, and
4132 use its lines physical height for clipping.
4133
4134 Value is the index of the first glyph not in S. */
06a2c219
GM
4135
4136static int
66ac4b0e 4137x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4138 struct glyph_string *s;
4139 int face_id;
66ac4b0e 4140 int start, end, overlaps_p;
06a2c219
GM
4141{
4142 struct glyph *glyph, *last;
4143 int voffset;
ee569018 4144 int glyph_not_available_p;
06a2c219 4145
06a2c219
GM
4146 xassert (s->f == XFRAME (s->w->frame));
4147 xassert (s->nchars == 0);
4148 xassert (start >= 0 && end > start);
4149
66ac4b0e 4150 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4151 glyph = s->row->glyphs[s->area] + start;
4152 last = s->row->glyphs[s->area] + end;
4153 voffset = glyph->voffset;
4154
ee569018
KH
4155 glyph_not_available_p = glyph->glyph_not_available_p;
4156
06a2c219
GM
4157 while (glyph < last
4158 && glyph->type == CHAR_GLYPH
4159 && glyph->voffset == voffset
ee569018
KH
4160 /* Same face id implies same font, nowadays. */
4161 && glyph->face_id == face_id
4162 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4163 {
ee569018
KH
4164 int two_byte_p;
4165
06a2c219 4166 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4167 s->char2b + s->nchars,
4168 &two_byte_p);
4169 s->two_byte_p = two_byte_p;
06a2c219
GM
4170 ++s->nchars;
4171 xassert (s->nchars <= end - start);
4172 s->width += glyph->pixel_width;
4173 ++glyph;
4174 }
4175
4176 s->font = s->face->font;
4177 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4178
4179 /* If the specified font could not be loaded, use the frame's font,
4180 but record the fact that we couldn't load it in
4181 S->font_not_found_p so that we can draw rectangles for the
4182 characters of the glyph string. */
ee569018 4183 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4184 {
4185 s->font_not_found_p = 1;
4186 s->font = FRAME_FONT (s->f);
4187 }
4188
4189 /* Adjust base line for subscript/superscript text. */
4190 s->ybase += voffset;
66ac4b0e 4191
06a2c219
GM
4192 xassert (s->face && s->face->gc);
4193 return glyph - s->row->glyphs[s->area];
07e34cb0 4194}
dc6f92b8 4195
06a2c219
GM
4196
4197/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4198
dfcf069d 4199static void
06a2c219
GM
4200x_fill_image_glyph_string (s)
4201 struct glyph_string *s;
4202{
4203 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4204 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4205 xassert (s->img);
43d120d8 4206 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4207 s->font = s->face->font;
4208 s->width = s->first_glyph->pixel_width;
4209
4210 /* Adjust base line for subscript/superscript text. */
4211 s->ybase += s->first_glyph->voffset;
4212}
4213
4214
4215/* Fill glyph string S from stretch glyph S->first_glyph. */
4216
4217static void
4218x_fill_stretch_glyph_string (s)
4219 struct glyph_string *s;
4220{
4221 xassert (s->first_glyph->type == STRETCH_GLYPH);
43d120d8 4222 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4223 s->font = s->face->font;
4224 s->width = s->first_glyph->pixel_width;
4225
4226 /* Adjust base line for subscript/superscript text. */
4227 s->ybase += s->first_glyph->voffset;
4228}
4229
4230
4231/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4232 of XChar2b structures for S; it can't be allocated in
4233 x_init_glyph_string because it must be allocated via `alloca'. W
4234 is the window on which S is drawn. ROW and AREA are the glyph row
4235 and area within the row from which S is constructed. START is the
4236 index of the first glyph structure covered by S. HL is a
4237 face-override for drawing S. */
4238
4239static void
4240x_init_glyph_string (s, char2b, w, row, area, start, hl)
4241 struct glyph_string *s;
4242 XChar2b *char2b;
4243 struct window *w;
4244 struct glyph_row *row;
4245 enum glyph_row_area area;
4246 int start;
4247 enum draw_glyphs_face hl;
4248{
4249 bzero (s, sizeof *s);
4250 s->w = w;
4251 s->f = XFRAME (w->frame);
4252 s->display = FRAME_X_DISPLAY (s->f);
4253 s->window = FRAME_X_WINDOW (s->f);
4254 s->char2b = char2b;
4255 s->hl = hl;
4256 s->row = row;
4257 s->area = area;
4258 s->first_glyph = row->glyphs[area] + start;
4259 s->height = row->height;
4260 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4261
9ea173e8
GM
4262 /* Display the internal border below the tool-bar window. */
4263 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4264 s->y -= s->f->output_data.x->internal_border_width;
4265
4266 s->ybase = s->y + row->ascent;
4267}
4268
4269
4270/* Set background width of glyph string S. START is the index of the
4271 first glyph following S. LAST_X is the right-most x-position + 1
4272 in the drawing area. */
4273
4274static INLINE void
4275x_set_glyph_string_background_width (s, start, last_x)
4276 struct glyph_string *s;
4277 int start;
4278 int last_x;
4279{
4280 /* If the face of this glyph string has to be drawn to the end of
4281 the drawing area, set S->extends_to_end_of_line_p. */
4282 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4283
4284 if (start == s->row->used[s->area]
4285 && s->hl == DRAW_NORMAL_TEXT
4286 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4287 || s->face->background != default_face->background
4288 || s->face->stipple != default_face->stipple))
4289 s->extends_to_end_of_line_p = 1;
4290
4291 /* If S extends its face to the end of the line, set its
4292 background_width to the distance to the right edge of the drawing
4293 area. */
4294 if (s->extends_to_end_of_line_p)
1da3fd71 4295 s->background_width = last_x - s->x + 1;
06a2c219
GM
4296 else
4297 s->background_width = s->width;
4298}
4299
4300
4301/* Add a glyph string for a stretch glyph to the list of strings
4302 between HEAD and TAIL. START is the index of the stretch glyph in
4303 row area AREA of glyph row ROW. END is the index of the last glyph
4304 in that glyph row area. X is the current output position assigned
4305 to the new glyph string constructed. HL overrides that face of the
4306 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4307 is the right-most x-position of the drawing area. */
4308
8abee2e1
DL
4309/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4310 and below -- keep them on one line. */
4311#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4312 do \
4313 { \
4314 s = (struct glyph_string *) alloca (sizeof *s); \
4315 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4316 x_fill_stretch_glyph_string (s); \
4317 x_append_glyph_string (&HEAD, &TAIL, s); \
4318 ++START; \
4319 s->x = (X); \
4320 } \
4321 while (0)
4322
4323
4324/* Add a glyph string for an image glyph to the list of strings
4325 between HEAD and TAIL. START is the index of the image glyph in
4326 row area AREA of glyph row ROW. END is the index of the last glyph
4327 in that glyph row area. X is the current output position assigned
4328 to the new glyph string constructed. HL overrides that face of the
4329 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4330 is the right-most x-position of the drawing area. */
4331
8abee2e1 4332#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4333 do \
4334 { \
4335 s = (struct glyph_string *) alloca (sizeof *s); \
4336 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4337 x_fill_image_glyph_string (s); \
4338 x_append_glyph_string (&HEAD, &TAIL, s); \
4339 ++START; \
4340 s->x = (X); \
4341 } \
4342 while (0)
4343
4344
4345/* Add a glyph string for a sequence of character glyphs to the list
4346 of strings between HEAD and TAIL. START is the index of the first
4347 glyph in row area AREA of glyph row ROW that is part of the new
4348 glyph string. END is the index of the last glyph in that glyph row
4349 area. X is the current output position assigned to the new glyph
4350 string constructed. HL overrides that face of the glyph; e.g. it
4351 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4352 right-most x-position of the drawing area. */
4353
8abee2e1 4354#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4355 do \
4356 { \
4357 int c, charset, face_id; \
4358 XChar2b *char2b; \
4359 \
43d120d8 4360 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4361 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4362 \
b4192550
KH
4363 s = (struct glyph_string *) alloca (sizeof *s); \
4364 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4365 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4366 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4367 s->x = (X); \
4368 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4369 OVERLAPS_P); \
06a2c219
GM
4370 } \
4371 while (0)
4372
4373
b4192550
KH
4374/* Add a glyph string for a composite sequence to the list of strings
4375 between HEAD and TAIL. START is the index of the first glyph in
4376 row area AREA of glyph row ROW that is part of the new glyph
4377 string. END is the index of the last glyph in that glyph row area.
4378 X is the current output position assigned to the new glyph string
4379 constructed. HL overrides that face of the glyph; e.g. it is
4380 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4381 x-position of the drawing area. */
4382
6c27ec25 4383#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4384 do { \
43d120d8
KH
4385 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4386 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4387 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4388 struct composition *cmp = composition_table[cmp_id]; \
4389 int glyph_len = cmp->glyph_len; \
4390 XChar2b *char2b; \
4391 struct face **faces; \
4392 struct glyph_string *first_s = NULL; \
4393 int n; \
4394 \
ee569018 4395 base_face = base_face->ascii_face; \
b4192550
KH
4396 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4397 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4398 /* At first, fill in `char2b' and `faces'. */ \
4399 for (n = 0; n < glyph_len; n++) \
4400 { \
43d120d8 4401 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4402 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4403 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4404 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4405 this_face_id, char2b + n, 1); \
b4192550
KH
4406 } \
4407 \
4408 /* Make glyph_strings for each glyph sequence that is drawable by \
4409 the same face, and append them to HEAD/TAIL. */ \
4410 for (n = 0; n < cmp->glyph_len;) \
4411 { \
4412 s = (struct glyph_string *) alloca (sizeof *s); \
4413 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4414 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4415 s->cmp = cmp; \
4416 s->gidx = n; \
b4192550
KH
4417 s->x = (X); \
4418 \
4419 if (n == 0) \
4420 first_s = s; \
4421 \
4422 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4423 } \
4424 \
4425 ++START; \
4426 s = first_s; \
4427 } while (0)
4428
4429
06a2c219
GM
4430/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4431 of AREA of glyph row ROW on window W between indices START and END.
4432 HL overrides the face for drawing glyph strings, e.g. it is
4433 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4434 x-positions of the drawing area.
4435
4436 This is an ugly monster macro construct because we must use alloca
4437 to allocate glyph strings (because x_draw_glyphs can be called
4438 asynchronously). */
4439
8abee2e1 4440#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4441 do \
4442 { \
4443 HEAD = TAIL = NULL; \
4444 while (START < END) \
4445 { \
4446 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4447 switch (first_glyph->type) \
4448 { \
4449 case CHAR_GLYPH: \
4450 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4451 TAIL, HL, X, LAST_X, \
4452 OVERLAPS_P); \
06a2c219
GM
4453 break; \
4454 \
b4192550
KH
4455 case COMPOSITE_GLYPH: \
4456 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4457 HEAD, TAIL, HL, X, LAST_X,\
4458 OVERLAPS_P); \
4459 break; \
4460 \
06a2c219
GM
4461 case STRETCH_GLYPH: \
4462 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4463 HEAD, TAIL, HL, X, LAST_X); \
4464 break; \
4465 \
4466 case IMAGE_GLYPH: \
4467 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4468 TAIL, HL, X, LAST_X); \
4469 break; \
4470 \
4471 default: \
4472 abort (); \
4473 } \
4474 \
4475 x_set_glyph_string_background_width (s, START, LAST_X); \
4476 (X) += s->width; \
4477 } \
4478 } \
4479 while (0)
4480
4481
4482/* Draw glyphs between START and END in AREA of ROW on window W,
4483 starting at x-position X. X is relative to AREA in W. HL is a
4484 face-override with the following meaning:
4485
4486 DRAW_NORMAL_TEXT draw normally
4487 DRAW_CURSOR draw in cursor face
4488 DRAW_MOUSE_FACE draw in mouse face.
4489 DRAW_INVERSE_VIDEO draw in mode line face
4490 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4491 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4492
4493 If REAL_START is non-null, return in *REAL_START the real starting
4494 position for display. This can be different from START in case
4495 overlapping glyphs must be displayed. If REAL_END is non-null,
4496 return in *REAL_END the real end position for display. This can be
4497 different from END in case overlapping glyphs must be displayed.
4498
66ac4b0e
GM
4499 If OVERLAPS_P is non-zero, draw only the foreground of characters
4500 and clip to the physical height of ROW.
4501
06a2c219
GM
4502 Value is the x-position reached, relative to AREA of W. */
4503
4504static int
66ac4b0e
GM
4505x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4506 overlaps_p)
06a2c219
GM
4507 struct window *w;
4508 int x;
4509 struct glyph_row *row;
4510 enum glyph_row_area area;
4511 int start, end;
4512 enum draw_glyphs_face hl;
4513 int *real_start, *real_end;
66ac4b0e 4514 int overlaps_p;
dc6f92b8 4515{
06a2c219
GM
4516 struct glyph_string *head, *tail;
4517 struct glyph_string *s;
4518 int last_x, area_width;
4519 int x_reached;
4520 int i, j;
4521
4522 /* Let's rather be paranoid than getting a SEGV. */
4523 start = max (0, start);
4524 end = min (end, row->used[area]);
4525 if (real_start)
4526 *real_start = start;
4527 if (real_end)
4528 *real_end = end;
4529
4530 /* Translate X to frame coordinates. Set last_x to the right
4531 end of the drawing area. */
4532 if (row->full_width_p)
4533 {
4534 /* X is relative to the left edge of W, without scroll bars
4535 or flag areas. */
4536 struct frame *f = XFRAME (w->frame);
110859fc 4537 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4538 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4539
06a2c219
GM
4540 x += window_left_x;
4541 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4542 last_x = window_left_x + area_width;
4543
4544 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4545 {
110859fc 4546 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4547 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4548 last_x += width;
4549 else
4550 x -= width;
4551 }
dc6f92b8 4552
b9432a85
GM
4553 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4554 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4555 }
4556 else
dc6f92b8 4557 {
06a2c219
GM
4558 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4559 area_width = window_box_width (w, area);
4560 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4561 }
4562
06a2c219
GM
4563 /* Build a doubly-linked list of glyph_string structures between
4564 head and tail from what we have to draw. Note that the macro
4565 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4566 the reason we use a separate variable `i'. */
4567 i = start;
66ac4b0e
GM
4568 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4569 overlaps_p);
06a2c219
GM
4570 if (tail)
4571 x_reached = tail->x + tail->background_width;
4572 else
4573 x_reached = x;
90e65f07 4574
06a2c219
GM
4575 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4576 the row, redraw some glyphs in front or following the glyph
4577 strings built above. */
66ac4b0e 4578 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4579 {
4580 int dummy_x = 0;
4581 struct glyph_string *h, *t;
4582
4583 /* Compute overhangs for all glyph strings. */
4584 for (s = head; s; s = s->next)
4585 x_compute_glyph_string_overhangs (s);
4586
4587 /* Prepend glyph strings for glyphs in front of the first glyph
4588 string that are overwritten because of the first glyph
4589 string's left overhang. The background of all strings
4590 prepended must be drawn because the first glyph string
4591 draws over it. */
4592 i = x_left_overwritten (head);
4593 if (i >= 0)
4594 {
4595 j = i;
4596 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4597 DRAW_NORMAL_TEXT, dummy_x, last_x,
4598 overlaps_p);
06a2c219
GM
4599 start = i;
4600 if (real_start)
4601 *real_start = start;
4602 x_compute_overhangs_and_x (t, head->x, 1);
4603 x_prepend_glyph_string_lists (&head, &tail, h, t);
4604 }
58769bee 4605
06a2c219
GM
4606 /* Prepend glyph strings for glyphs in front of the first glyph
4607 string that overwrite that glyph string because of their
4608 right overhang. For these strings, only the foreground must
4609 be drawn, because it draws over the glyph string at `head'.
4610 The background must not be drawn because this would overwrite
4611 right overhangs of preceding glyphs for which no glyph
4612 strings exist. */
4613 i = x_left_overwriting (head);
4614 if (i >= 0)
4615 {
4616 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4617 DRAW_NORMAL_TEXT, dummy_x, last_x,
4618 overlaps_p);
06a2c219
GM
4619 for (s = h; s; s = s->next)
4620 s->background_filled_p = 1;
4621 if (real_start)
4622 *real_start = i;
4623 x_compute_overhangs_and_x (t, head->x, 1);
4624 x_prepend_glyph_string_lists (&head, &tail, h, t);
4625 }
dbcb258a 4626
06a2c219
GM
4627 /* Append glyphs strings for glyphs following the last glyph
4628 string tail that are overwritten by tail. The background of
4629 these strings has to be drawn because tail's foreground draws
4630 over it. */
4631 i = x_right_overwritten (tail);
4632 if (i >= 0)
4633 {
4634 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4635 DRAW_NORMAL_TEXT, x, last_x,
4636 overlaps_p);
06a2c219
GM
4637 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4638 x_append_glyph_string_lists (&head, &tail, h, t);
4639 if (real_end)
4640 *real_end = i;
4641 }
dc6f92b8 4642
06a2c219
GM
4643 /* Append glyph strings for glyphs following the last glyph
4644 string tail that overwrite tail. The foreground of such
4645 glyphs has to be drawn because it writes into the background
4646 of tail. The background must not be drawn because it could
4647 paint over the foreground of following glyphs. */
4648 i = x_right_overwriting (tail);
4649 if (i >= 0)
4650 {
4651 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4652 DRAW_NORMAL_TEXT, x, last_x,
4653 overlaps_p);
06a2c219
GM
4654 for (s = h; s; s = s->next)
4655 s->background_filled_p = 1;
4656 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4657 x_append_glyph_string_lists (&head, &tail, h, t);
4658 if (real_end)
4659 *real_end = i;
4660 }
4661 }
58769bee 4662
06a2c219
GM
4663 /* Draw all strings. */
4664 for (s = head; s; s = s->next)
4665 x_draw_glyph_string (s);
dc6f92b8 4666
06a2c219
GM
4667 /* Value is the x-position up to which drawn, relative to AREA of W.
4668 This doesn't include parts drawn because of overhangs. */
4669 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4670 if (!row->full_width_p)
4671 {
4672 if (area > LEFT_MARGIN_AREA)
4673 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4674 if (area > TEXT_AREA)
4675 x_reached -= window_box_width (w, TEXT_AREA);
4676 }
4677 return x_reached;
4678}
dc6f92b8 4679
dc6f92b8 4680
66ac4b0e
GM
4681/* Fix the display of area AREA of overlapping row ROW in window W. */
4682
4683static void
4684x_fix_overlapping_area (w, row, area)
4685 struct window *w;
4686 struct glyph_row *row;
4687 enum glyph_row_area area;
4688{
4689 int i, x;
4690
4691 BLOCK_INPUT;
4692
4693 if (area == LEFT_MARGIN_AREA)
4694 x = 0;
4695 else if (area == TEXT_AREA)
4696 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4697 else
4698 x = (window_box_width (w, LEFT_MARGIN_AREA)
4699 + window_box_width (w, TEXT_AREA));
4700
4701 for (i = 0; i < row->used[area];)
4702 {
4703 if (row->glyphs[area][i].overlaps_vertically_p)
4704 {
4705 int start = i, start_x = x;
4706
4707 do
4708 {
4709 x += row->glyphs[area][i].pixel_width;
4710 ++i;
4711 }
4712 while (i < row->used[area]
4713 && row->glyphs[area][i].overlaps_vertically_p);
4714
4715 x_draw_glyphs (w, start_x, row, area, start, i,
4716 (row->inverse_p
4717 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4718 NULL, NULL, 1);
4719 }
4720 else
4721 {
4722 x += row->glyphs[area][i].pixel_width;
4723 ++i;
4724 }
4725 }
4726
4727 UNBLOCK_INPUT;
4728}
4729
4730
06a2c219
GM
4731/* Output LEN glyphs starting at START at the nominal cursor position.
4732 Advance the nominal cursor over the text. The global variable
4733 updated_window contains the window being updated, updated_row is
4734 the glyph row being updated, and updated_area is the area of that
4735 row being updated. */
dc6f92b8 4736
06a2c219
GM
4737static void
4738x_write_glyphs (start, len)
4739 struct glyph *start;
4740 int len;
4741{
4742 int x, hpos, real_start, real_end;
d9cdbb3d 4743
06a2c219 4744 xassert (updated_window && updated_row);
dc6f92b8 4745 BLOCK_INPUT;
06a2c219
GM
4746
4747 /* Write glyphs. */
dc6f92b8 4748
06a2c219
GM
4749 hpos = start - updated_row->glyphs[updated_area];
4750 x = x_draw_glyphs (updated_window, output_cursor.x,
4751 updated_row, updated_area,
4752 hpos, hpos + len,
4753 (updated_row->inverse_p
4754 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4755 &real_start, &real_end, 0);
b30ec466 4756
06a2c219
GM
4757 /* If we drew over the cursor, note that it is not visible any more. */
4758 note_overwritten_text_cursor (updated_window, real_start,
4759 real_end - real_start);
dc6f92b8
JB
4760
4761 UNBLOCK_INPUT;
06a2c219
GM
4762
4763 /* Advance the output cursor. */
4764 output_cursor.hpos += len;
4765 output_cursor.x = x;
dc6f92b8
JB
4766}
4767
0cdd0c9f 4768
06a2c219 4769/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4770
06a2c219
GM
4771static void
4772x_insert_glyphs (start, len)
4773 struct glyph *start;
4774 register int len;
4775{
4776 struct frame *f;
4777 struct window *w;
4778 int line_height, shift_by_width, shifted_region_width;
4779 struct glyph_row *row;
4780 struct glyph *glyph;
4781 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4782
06a2c219 4783 xassert (updated_window && updated_row);
0cdd0c9f 4784 BLOCK_INPUT;
06a2c219
GM
4785 w = updated_window;
4786 f = XFRAME (WINDOW_FRAME (w));
4787
4788 /* Get the height of the line we are in. */
4789 row = updated_row;
4790 line_height = row->height;
4791
4792 /* Get the width of the glyphs to insert. */
4793 shift_by_width = 0;
4794 for (glyph = start; glyph < start + len; ++glyph)
4795 shift_by_width += glyph->pixel_width;
4796
4797 /* Get the width of the region to shift right. */
4798 shifted_region_width = (window_box_width (w, updated_area)
4799 - output_cursor.x
4800 - shift_by_width);
4801
4802 /* Shift right. */
4803 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4804 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4805 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4806 f->output_data.x->normal_gc,
4807 frame_x, frame_y,
4808 shifted_region_width, line_height,
4809 frame_x + shift_by_width, frame_y);
4810
4811 /* Write the glyphs. */
4812 hpos = start - row->glyphs[updated_area];
4813 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4814 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4815 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4816
4817 /* Advance the output cursor. */
4818 output_cursor.hpos += len;
4819 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4820 UNBLOCK_INPUT;
4821}
0cdd0c9f 4822
0cdd0c9f 4823
06a2c219
GM
4824/* Delete N glyphs at the nominal cursor position. Not implemented
4825 for X frames. */
c83febd7
RS
4826
4827static void
06a2c219
GM
4828x_delete_glyphs (n)
4829 register int n;
c83febd7 4830{
06a2c219 4831 abort ();
c83febd7
RS
4832}
4833
0cdd0c9f 4834
06a2c219
GM
4835/* Erase the current text line from the nominal cursor position
4836 (inclusive) to pixel column TO_X (exclusive). The idea is that
4837 everything from TO_X onward is already erased.
4838
4839 TO_X is a pixel position relative to updated_area of
4840 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4841
06a2c219
GM
4842static void
4843x_clear_end_of_line (to_x)
4844 int to_x;
4845{
4846 struct frame *f;
4847 struct window *w = updated_window;
4848 int max_x, min_y, max_y;
4849 int from_x, from_y, to_y;
4850
4851 xassert (updated_window && updated_row);
4852 f = XFRAME (w->frame);
4853
4854 if (updated_row->full_width_p)
4855 {
4856 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4857 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4858 && !w->pseudo_window_p)
4859 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4860 }
06a2c219
GM
4861 else
4862 max_x = window_box_width (w, updated_area);
4863 max_y = window_text_bottom_y (w);
dc6f92b8 4864
06a2c219
GM
4865 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4866 of window. For TO_X > 0, truncate to end of drawing area. */
4867 if (to_x == 0)
4868 return;
4869 else if (to_x < 0)
4870 to_x = max_x;
4871 else
4872 to_x = min (to_x, max_x);
dbc4e1c1 4873
06a2c219
GM
4874 to_y = min (max_y, output_cursor.y + updated_row->height);
4875
4876 /* Notice if the cursor will be cleared by this operation. */
4877 if (!updated_row->full_width_p)
4878 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4879
06a2c219
GM
4880 from_x = output_cursor.x;
4881
4882 /* Translate to frame coordinates. */
4883 if (updated_row->full_width_p)
4884 {
4885 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4886 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4887 }
0cdd0c9f
RS
4888 else
4889 {
06a2c219
GM
4890 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4891 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4892 }
4893
045dee35 4894 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4895 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4896 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4897
4898 /* Prevent inadvertently clearing to end of the X window. */
4899 if (to_x > from_x && to_y > from_y)
4900 {
4901 BLOCK_INPUT;
4902 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4903 from_x, from_y, to_x - from_x, to_y - from_y,
4904 False);
4905 UNBLOCK_INPUT;
0cdd0c9f 4906 }
0cdd0c9f 4907}
dbc4e1c1 4908
0cdd0c9f 4909
06a2c219 4910/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4911 frame. Otherwise clear the selected frame. */
06a2c219
GM
4912
4913static void
4914x_clear_frame ()
0cdd0c9f 4915{
06a2c219 4916 struct frame *f;
0cdd0c9f 4917
06a2c219
GM
4918 if (updating_frame)
4919 f = updating_frame;
0cdd0c9f 4920 else
b86bd3dd 4921 f = SELECTED_FRAME ();
58769bee 4922
06a2c219
GM
4923 /* Clearing the frame will erase any cursor, so mark them all as no
4924 longer visible. */
4925 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4926 output_cursor.hpos = output_cursor.vpos = 0;
4927 output_cursor.x = -1;
4928
4929 /* We don't set the output cursor here because there will always
4930 follow an explicit cursor_to. */
4931 BLOCK_INPUT;
4932 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4933
4934 /* We have to clear the scroll bars, too. If we have changed
4935 colors or something like that, then they should be notified. */
4936 x_scroll_bar_clear (f);
0cdd0c9f 4937
06a2c219
GM
4938 XFlush (FRAME_X_DISPLAY (f));
4939 UNBLOCK_INPUT;
dc6f92b8 4940}
06a2c219
GM
4941
4942
dc6f92b8 4943\f
dbc4e1c1
JB
4944/* Invert the middle quarter of the frame for .15 sec. */
4945
06a2c219
GM
4946/* We use the select system call to do the waiting, so we have to make
4947 sure it's available. If it isn't, we just won't do visual bells. */
4948
dbc4e1c1
JB
4949#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4950
06a2c219
GM
4951
4952/* Subtract the `struct timeval' values X and Y, storing the result in
4953 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4954
4955static int
4956timeval_subtract (result, x, y)
4957 struct timeval *result, x, y;
4958{
06a2c219
GM
4959 /* Perform the carry for the later subtraction by updating y. This
4960 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4961 if (x.tv_usec < y.tv_usec)
4962 {
4963 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4964 y.tv_usec -= 1000000 * nsec;
4965 y.tv_sec += nsec;
4966 }
06a2c219 4967
dbc4e1c1
JB
4968 if (x.tv_usec - y.tv_usec > 1000000)
4969 {
4970 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4971 y.tv_usec += 1000000 * nsec;
4972 y.tv_sec -= nsec;
4973 }
4974
06a2c219
GM
4975 /* Compute the time remaining to wait. tv_usec is certainly
4976 positive. */
dbc4e1c1
JB
4977 result->tv_sec = x.tv_sec - y.tv_sec;
4978 result->tv_usec = x.tv_usec - y.tv_usec;
4979
06a2c219
GM
4980 /* Return indication of whether the result should be considered
4981 negative. */
dbc4e1c1
JB
4982 return x.tv_sec < y.tv_sec;
4983}
dc6f92b8 4984
dfcf069d 4985void
f676886a
JB
4986XTflash (f)
4987 struct frame *f;
dc6f92b8 4988{
dbc4e1c1 4989 BLOCK_INPUT;
dc6f92b8 4990
dbc4e1c1
JB
4991 {
4992 GC gc;
dc6f92b8 4993
06a2c219
GM
4994 /* Create a GC that will use the GXxor function to flip foreground
4995 pixels into background pixels. */
dbc4e1c1
JB
4996 {
4997 XGCValues values;
dc6f92b8 4998
dbc4e1c1 4999 values.function = GXxor;
7556890b
RS
5000 values.foreground = (f->output_data.x->foreground_pixel
5001 ^ f->output_data.x->background_pixel);
58769bee 5002
334208b7 5003 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5004 GCFunction | GCForeground, &values);
5005 }
dc6f92b8 5006
dbc4e1c1 5007 {
e84e14c3
RS
5008 /* Get the height not including a menu bar widget. */
5009 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5010 /* Height of each line to flash. */
5011 int flash_height = FRAME_LINE_HEIGHT (f);
5012 /* These will be the left and right margins of the rectangles. */
5013 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5014 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5015
5016 int width;
5017
5018 /* Don't flash the area between a scroll bar and the frame
5019 edge it is next to. */
5020 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5021 {
5022 case vertical_scroll_bar_left:
5023 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5024 break;
5025
5026 case vertical_scroll_bar_right:
5027 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5028 break;
06a2c219
GM
5029
5030 default:
5031 break;
e84e14c3
RS
5032 }
5033
5034 width = flash_right - flash_left;
5035
5036 /* If window is tall, flash top and bottom line. */
5037 if (height > 3 * FRAME_LINE_HEIGHT (f))
5038 {
5039 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5040 flash_left,
5041 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5042 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5043 width, flash_height);
5044 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5045 flash_left,
5046 (height - flash_height
5047 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5048 width, flash_height);
5049 }
5050 else
5051 /* If it is short, flash it all. */
5052 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5053 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5054 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5055
06a2c219 5056 x_flush (f);
dc6f92b8 5057
dbc4e1c1 5058 {
06a2c219 5059 struct timeval wakeup;
dc6f92b8 5060
66c30ea1 5061 EMACS_GET_TIME (wakeup);
dc6f92b8 5062
dbc4e1c1
JB
5063 /* Compute time to wait until, propagating carry from usecs. */
5064 wakeup.tv_usec += 150000;
5065 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5066 wakeup.tv_usec %= 1000000;
5067
5068 /* Keep waiting until past the time wakeup. */
5069 while (1)
5070 {
5071 struct timeval timeout;
5072
66c30ea1 5073 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5074
5075 /* In effect, timeout = wakeup - timeout.
5076 Break if result would be negative. */
5077 if (timeval_subtract (&timeout, wakeup, timeout))
5078 break;
5079
5080 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5081 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5082 }
5083 }
58769bee 5084
e84e14c3
RS
5085 /* If window is tall, flash top and bottom line. */
5086 if (height > 3 * FRAME_LINE_HEIGHT (f))
5087 {
5088 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5089 flash_left,
5090 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5091 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5092 width, flash_height);
5093 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5094 flash_left,
5095 (height - flash_height
5096 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5097 width, flash_height);
5098 }
5099 else
5100 /* If it is short, flash it all. */
5101 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5102 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5103 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5104
334208b7 5105 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5106 x_flush (f);
dc6f92b8 5107 }
dbc4e1c1
JB
5108 }
5109
5110 UNBLOCK_INPUT;
dc6f92b8
JB
5111}
5112
06a2c219 5113#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5114
5115
dc6f92b8
JB
5116/* Make audible bell. */
5117
dfcf069d 5118void
dc6f92b8
JB
5119XTring_bell ()
5120{
b86bd3dd
GM
5121 struct frame *f = SELECTED_FRAME ();
5122
5123 if (FRAME_X_DISPLAY (f))
5124 {
dbc4e1c1 5125#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5126 if (visible_bell)
5127 XTflash (f);
5128 else
dbc4e1c1 5129#endif
b86bd3dd
GM
5130 {
5131 BLOCK_INPUT;
5132 XBell (FRAME_X_DISPLAY (f), 0);
5133 XFlush (FRAME_X_DISPLAY (f));
5134 UNBLOCK_INPUT;
5135 }
dc6f92b8
JB
5136 }
5137}
06a2c219 5138
dc6f92b8 5139\f
06a2c219
GM
5140/* Specify how many text lines, from the top of the window,
5141 should be affected by insert-lines and delete-lines operations.
5142 This, and those operations, are used only within an update
5143 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5144
dfcf069d 5145static void
06a2c219
GM
5146XTset_terminal_window (n)
5147 register int n;
dc6f92b8 5148{
06a2c219 5149 /* This function intentionally left blank. */
dc6f92b8
JB
5150}
5151
06a2c219
GM
5152
5153\f
5154/***********************************************************************
5155 Line Dance
5156 ***********************************************************************/
5157
5158/* Perform an insert-lines or delete-lines operation, inserting N
5159 lines or deleting -N lines at vertical position VPOS. */
5160
dfcf069d 5161static void
06a2c219
GM
5162x_ins_del_lines (vpos, n)
5163 int vpos, n;
dc6f92b8
JB
5164{
5165 abort ();
5166}
06a2c219
GM
5167
5168
5169/* Scroll part of the display as described by RUN. */
dc6f92b8 5170
dfcf069d 5171static void
06a2c219
GM
5172x_scroll_run (w, run)
5173 struct window *w;
5174 struct run *run;
dc6f92b8 5175{
06a2c219
GM
5176 struct frame *f = XFRAME (w->frame);
5177 int x, y, width, height, from_y, to_y, bottom_y;
5178
5179 /* Get frame-relative bounding box of the text display area of W,
5180 without mode lines. Include in this box the flags areas to the
5181 left and right of W. */
5182 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5183 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5184 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5185
5186 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5187 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5188 bottom_y = y + height;
dc6f92b8 5189
06a2c219
GM
5190 if (to_y < from_y)
5191 {
5192 /* Scrolling up. Make sure we don't copy part of the mode
5193 line at the bottom. */
5194 if (from_y + run->height > bottom_y)
5195 height = bottom_y - from_y;
5196 else
5197 height = run->height;
5198 }
dc6f92b8 5199 else
06a2c219
GM
5200 {
5201 /* Scolling down. Make sure we don't copy over the mode line.
5202 at the bottom. */
5203 if (to_y + run->height > bottom_y)
5204 height = bottom_y - to_y;
5205 else
5206 height = run->height;
5207 }
7a13e894 5208
06a2c219
GM
5209 BLOCK_INPUT;
5210
5211 /* Cursor off. Will be switched on again in x_update_window_end. */
5212 updated_window = w;
5213 x_clear_cursor (w);
5214
5215 XCopyArea (FRAME_X_DISPLAY (f),
5216 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5217 f->output_data.x->normal_gc,
5218 x, from_y,
5219 width, height,
5220 x, to_y);
5221
5222 UNBLOCK_INPUT;
5223}
dc6f92b8 5224
dc6f92b8 5225
06a2c219
GM
5226\f
5227/***********************************************************************
5228 Exposure Events
5229 ***********************************************************************/
5230
5231/* Redisplay an exposed area of frame F. X and Y are the upper-left
5232 corner of the exposed rectangle. W and H are width and height of
5233 the exposed area. All are pixel values. W or H zero means redraw
5234 the entire frame. */
dc6f92b8 5235
06a2c219
GM
5236static void
5237expose_frame (f, x, y, w, h)
5238 struct frame *f;
5239 int x, y, w, h;
dc6f92b8 5240{
06a2c219 5241 XRectangle r;
dc6f92b8 5242
06a2c219 5243 TRACE ((stderr, "expose_frame "));
dc6f92b8 5244
06a2c219
GM
5245 /* No need to redraw if frame will be redrawn soon. */
5246 if (FRAME_GARBAGED_P (f))
dc6f92b8 5247 {
06a2c219
GM
5248 TRACE ((stderr, " garbaged\n"));
5249 return;
5250 }
5251
5252 /* If basic faces haven't been realized yet, there is no point in
5253 trying to redraw anything. This can happen when we get an expose
5254 event while Emacs is starting, e.g. by moving another window. */
5255 if (FRAME_FACE_CACHE (f) == NULL
5256 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5257 {
5258 TRACE ((stderr, " no faces\n"));
5259 return;
58769bee 5260 }
06a2c219
GM
5261
5262 if (w == 0 || h == 0)
58769bee 5263 {
06a2c219
GM
5264 r.x = r.y = 0;
5265 r.width = CANON_X_UNIT (f) * f->width;
5266 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5267 }
5268 else
5269 {
06a2c219
GM
5270 r.x = x;
5271 r.y = y;
5272 r.width = w;
5273 r.height = h;
5274 }
5275
5276 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5277 expose_window_tree (XWINDOW (f->root_window), &r);
5278
9ea173e8 5279 if (WINDOWP (f->tool_bar_window))
06a2c219 5280 {
9ea173e8 5281 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5282 XRectangle window_rect;
5283 XRectangle intersection_rect;
5284 int window_x, window_y, window_width, window_height;
5285
5286
5287 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5288 window_rect.x = window_x;
5289 window_rect.y = window_y;
5290 window_rect.width = window_width;
5291 window_rect.height = window_height;
5292
5293 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5294 expose_window (w, &intersection_rect);
5295 }
5296
5297#ifndef USE_X_TOOLKIT
5298 if (WINDOWP (f->menu_bar_window))
5299 {
5300 struct window *w = XWINDOW (f->menu_bar_window);
5301 XRectangle window_rect;
5302 XRectangle intersection_rect;
5303 int window_x, window_y, window_width, window_height;
5304
5305
5306 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5307 window_rect.x = window_x;
5308 window_rect.y = window_y;
5309 window_rect.width = window_width;
5310 window_rect.height = window_height;
5311
5312 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5313 expose_window (w, &intersection_rect);
dc6f92b8 5314 }
06a2c219 5315#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5316}
5317
06a2c219
GM
5318
5319/* Redraw (parts) of all windows in the window tree rooted at W that
5320 intersect R. R contains frame pixel coordinates. */
5321
58769bee 5322static void
06a2c219
GM
5323expose_window_tree (w, r)
5324 struct window *w;
5325 XRectangle *r;
dc6f92b8 5326{
06a2c219
GM
5327 while (w)
5328 {
5329 if (!NILP (w->hchild))
5330 expose_window_tree (XWINDOW (w->hchild), r);
5331 else if (!NILP (w->vchild))
5332 expose_window_tree (XWINDOW (w->vchild), r);
5333 else
5334 {
5335 XRectangle window_rect;
5336 XRectangle intersection_rect;
5337 struct frame *f = XFRAME (w->frame);
5338 int window_x, window_y, window_width, window_height;
5339
5340 /* Frame-relative pixel rectangle of W. */
5341 window_box (w, -1, &window_x, &window_y, &window_width,
5342 &window_height);
5343 window_rect.x
5344 = (window_x
110859fc 5345 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5346 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5347 window_rect.y = window_y;
5348 window_rect.width
5349 = (window_width
110859fc 5350 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5351 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5352 window_rect.height
5353 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5354
5355 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5356 expose_window (w, &intersection_rect);
5357 }
58769bee 5358
06a2c219
GM
5359 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5360 }
5361}
58769bee 5362
dc6f92b8 5363
06a2c219
GM
5364/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5365 which intersects rectangle R. R is in window-relative coordinates. */
5366
5367static void
5368expose_area (w, row, r, area)
5369 struct window *w;
5370 struct glyph_row *row;
5371 XRectangle *r;
5372 enum glyph_row_area area;
5373{
5374 int x;
5375 struct glyph *first = row->glyphs[area];
5376 struct glyph *end = row->glyphs[area] + row->used[area];
5377 struct glyph *last;
5378 int first_x;
5379
5380 /* Set x to the window-relative start position for drawing glyphs of
5381 AREA. The first glyph of the text area can be partially visible.
5382 The first glyphs of other areas cannot. */
5383 if (area == LEFT_MARGIN_AREA)
5384 x = 0;
5385 else if (area == TEXT_AREA)
5386 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5387 else
5388 x = (window_box_width (w, LEFT_MARGIN_AREA)
5389 + window_box_width (w, TEXT_AREA));
5390
6fb13182
GM
5391 if (area == TEXT_AREA && row->fill_line_p)
5392 /* If row extends face to end of line write the whole line. */
5393 x_draw_glyphs (w, x, row, area,
5394 0, row->used[area],
06a2c219 5395 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5396 NULL, NULL, 0);
6fb13182
GM
5397 else
5398 {
5399 /* Find the first glyph that must be redrawn. */
5400 while (first < end
5401 && x + first->pixel_width < r->x)
5402 {
5403 x += first->pixel_width;
5404 ++first;
5405 }
5406
5407 /* Find the last one. */
5408 last = first;
5409 first_x = x;
5410 while (last < end
5411 && x < r->x + r->width)
5412 {
5413 x += last->pixel_width;
5414 ++last;
5415 }
5416
5417 /* Repaint. */
5418 if (last > first)
5419 x_draw_glyphs (w, first_x, row, area,
5420 first - row->glyphs[area],
5421 last - row->glyphs[area],
5422 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5423 NULL, NULL, 0);
5424 }
06a2c219
GM
5425}
5426
58769bee 5427
06a2c219
GM
5428/* Redraw the parts of the glyph row ROW on window W intersecting
5429 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5430
06a2c219
GM
5431static void
5432expose_line (w, row, r)
5433 struct window *w;
5434 struct glyph_row *row;
5435 XRectangle *r;
5436{
5437 xassert (row->enabled_p);
5438
5439 if (row->mode_line_p || w->pseudo_window_p)
5440 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5441 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5442 NULL, NULL, 0);
06a2c219
GM
5443 else
5444 {
5445 if (row->used[LEFT_MARGIN_AREA])
5446 expose_area (w, row, r, LEFT_MARGIN_AREA);
5447 if (row->used[TEXT_AREA])
5448 expose_area (w, row, r, TEXT_AREA);
5449 if (row->used[RIGHT_MARGIN_AREA])
5450 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5451 x_draw_row_bitmaps (w, row);
5452 }
5453}
dc6f92b8 5454
58769bee 5455
06a2c219
GM
5456/* Return non-zero if W's cursor intersects rectangle R. */
5457
5458static int
5459x_phys_cursor_in_rect_p (w, r)
5460 struct window *w;
5461 XRectangle *r;
5462{
5463 XRectangle cr, result;
5464 struct glyph *cursor_glyph;
5465
5466 cursor_glyph = get_phys_cursor_glyph (w);
5467 if (cursor_glyph)
5468 {
5469 cr.x = w->phys_cursor.x;
5470 cr.y = w->phys_cursor.y;
5471 cr.width = cursor_glyph->pixel_width;
5472 cr.height = w->phys_cursor_height;
5473 return x_intersect_rectangles (&cr, r, &result);
5474 }
5475 else
5476 return 0;
dc6f92b8 5477}
dc6f92b8 5478
06a2c219
GM
5479
5480/* Redraw a rectangle of window W. R is a rectangle in window
5481 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5482
5483static void
06a2c219
GM
5484expose_window (w, r)
5485 struct window *w;
5486 XRectangle *r;
dc6f92b8 5487{
06a2c219
GM
5488 struct glyph_row *row;
5489 int y;
5490 int yb = window_text_bottom_y (w);
5491 int cursor_cleared_p;
dc6f92b8 5492
80c32bcc
GM
5493 /* If window is not yet fully initialized, do nothing. This can
5494 happen when toolkit scroll bars are used and a window is split.
5495 Reconfiguring the scroll bar will generate an expose for a newly
5496 created window. */
5497 if (w->current_matrix == NULL)
5498 return;
5499
06a2c219
GM
5500 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5501 r->x, r->y, r->width, r->height));
dc6f92b8 5502
06a2c219
GM
5503 /* Convert to window coordinates. */
5504 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5505 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5506
06a2c219
GM
5507 /* Turn off the cursor. */
5508 if (!w->pseudo_window_p
5509 && x_phys_cursor_in_rect_p (w, r))
5510 {
5511 x_clear_cursor (w);
5512 cursor_cleared_p = 1;
5513 }
5514 else
5515 cursor_cleared_p = 0;
5516
5517 /* Find the first row intersecting the rectangle R. */
5518 row = w->current_matrix->rows;
5519 y = 0;
5520 while (row->enabled_p
5521 && y < yb
5522 && y + row->height < r->y)
5523 {
5524 y += row->height;
5525 ++row;
5526 }
5527
dc6f92b8 5528 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5529 while (row->enabled_p
5530 && y < yb
5531 && y < r->y + r->height)
5532 {
5533 expose_line (w, row, r);
5534 y += row->height;
5535 ++row;
5536 }
5537
5538 /* Display the mode line if there is one. */
5539 if (WINDOW_WANTS_MODELINE_P (w)
5540 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5541 row->enabled_p)
5542 && row->y < r->y + r->height)
5543 expose_line (w, row, r);
dc6f92b8 5544
06a2c219 5545 if (!w->pseudo_window_p)
dc6f92b8 5546 {
06a2c219
GM
5547 /* Draw border between windows. */
5548 x_draw_vertical_border (w);
5549
5550 /* Turn the cursor on again. */
5551 if (cursor_cleared_p)
5552 x_update_window_cursor (w, 1);
5553 }
5554}
dc6f92b8 5555
dc6f92b8 5556
06a2c219
GM
5557/* Determine the intersection of two rectangles R1 and R2. Return
5558 the intersection in *RESULT. Value is non-zero if RESULT is not
5559 empty. */
5560
5561static int
5562x_intersect_rectangles (r1, r2, result)
5563 XRectangle *r1, *r2, *result;
5564{
5565 XRectangle *left, *right;
5566 XRectangle *upper, *lower;
5567 int intersection_p = 0;
5568
5569 /* Rearrange so that R1 is the left-most rectangle. */
5570 if (r1->x < r2->x)
5571 left = r1, right = r2;
5572 else
5573 left = r2, right = r1;
5574
5575 /* X0 of the intersection is right.x0, if this is inside R1,
5576 otherwise there is no intersection. */
5577 if (right->x <= left->x + left->width)
5578 {
5579 result->x = right->x;
5580
5581 /* The right end of the intersection is the minimum of the
5582 the right ends of left and right. */
5583 result->width = (min (left->x + left->width, right->x + right->width)
5584 - result->x);
5585
5586 /* Same game for Y. */
5587 if (r1->y < r2->y)
5588 upper = r1, lower = r2;
5589 else
5590 upper = r2, lower = r1;
5591
5592 /* The upper end of the intersection is lower.y0, if this is inside
5593 of upper. Otherwise, there is no intersection. */
5594 if (lower->y <= upper->y + upper->height)
dc43ef94 5595 {
06a2c219
GM
5596 result->y = lower->y;
5597
5598 /* The lower end of the intersection is the minimum of the lower
5599 ends of upper and lower. */
5600 result->height = (min (lower->y + lower->height,
5601 upper->y + upper->height)
5602 - result->y);
5603 intersection_p = 1;
dc43ef94 5604 }
dc6f92b8
JB
5605 }
5606
06a2c219 5607 return intersection_p;
dc6f92b8 5608}
06a2c219
GM
5609
5610
5611
5612
dc6f92b8 5613\f
dc6f92b8 5614static void
334208b7
RS
5615frame_highlight (f)
5616 struct frame *f;
dc6f92b8 5617{
b3e1e05c
JB
5618 /* We used to only do this if Vx_no_window_manager was non-nil, but
5619 the ICCCM (section 4.1.6) says that the window's border pixmap
5620 and border pixel are window attributes which are "private to the
5621 client", so we can always change it to whatever we want. */
5622 BLOCK_INPUT;
334208b7 5623 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5624 f->output_data.x->border_pixel);
b3e1e05c 5625 UNBLOCK_INPUT;
5d46f928 5626 x_update_cursor (f, 1);
dc6f92b8
JB
5627}
5628
5629static void
334208b7
RS
5630frame_unhighlight (f)
5631 struct frame *f;
dc6f92b8 5632{
b3e1e05c
JB
5633 /* We used to only do this if Vx_no_window_manager was non-nil, but
5634 the ICCCM (section 4.1.6) says that the window's border pixmap
5635 and border pixel are window attributes which are "private to the
5636 client", so we can always change it to whatever we want. */
5637 BLOCK_INPUT;
334208b7 5638 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5639 f->output_data.x->border_tile);
b3e1e05c 5640 UNBLOCK_INPUT;
5d46f928 5641 x_update_cursor (f, 1);
dc6f92b8 5642}
dc6f92b8 5643
f676886a
JB
5644/* The focus has changed. Update the frames as necessary to reflect
5645 the new situation. Note that we can't change the selected frame
c5acd733 5646 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5647 Each event gets marked with the frame in which it occurred, so the
c5acd733 5648 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5649
6d4238f3 5650static void
0f941935
KH
5651x_new_focus_frame (dpyinfo, frame)
5652 struct x_display_info *dpyinfo;
f676886a 5653 struct frame *frame;
dc6f92b8 5654{
0f941935 5655 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5656
0f941935 5657 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5658 {
58769bee 5659 /* Set this before calling other routines, so that they see
f676886a 5660 the correct value of x_focus_frame. */
0f941935 5661 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5662
5663 if (old_focus && old_focus->auto_lower)
f676886a 5664 x_lower_frame (old_focus);
dc6f92b8
JB
5665
5666#if 0
f676886a 5667 selected_frame = frame;
e0c1aef2
KH
5668 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5669 selected_frame);
f676886a
JB
5670 Fselect_window (selected_frame->selected_window);
5671 choose_minibuf_frame ();
c118dd06 5672#endif /* ! 0 */
dc6f92b8 5673
0f941935
KH
5674 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5675 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5676 else
5677 pending_autoraise_frame = 0;
6d4238f3 5678 }
dc6f92b8 5679
0f941935 5680 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5681}
5682
37c2c98b
RS
5683/* Handle an event saying the mouse has moved out of an Emacs frame. */
5684
5685void
0f941935
KH
5686x_mouse_leave (dpyinfo)
5687 struct x_display_info *dpyinfo;
37c2c98b 5688{
0f941935 5689 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5690}
6d4238f3 5691
f451eb13
JB
5692/* The focus has changed, or we have redirected a frame's focus to
5693 another frame (this happens when a frame uses a surrogate
06a2c219 5694 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5695
5696 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5697 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5698 the appropriate X display info. */
06a2c219 5699
6d4238f3 5700static void
0f941935
KH
5701XTframe_rehighlight (frame)
5702 struct frame *frame;
6d4238f3 5703{
0f941935
KH
5704 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5705}
6d4238f3 5706
0f941935
KH
5707static void
5708x_frame_rehighlight (dpyinfo)
5709 struct x_display_info *dpyinfo;
5710{
5711 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5712
5713 if (dpyinfo->x_focus_frame)
6d4238f3 5714 {
0f941935
KH
5715 dpyinfo->x_highlight_frame
5716 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5717 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5718 : dpyinfo->x_focus_frame);
5719 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5720 {
0f941935
KH
5721 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5722 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5723 }
dc6f92b8 5724 }
6d4238f3 5725 else
0f941935 5726 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5727
0f941935 5728 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5729 {
5730 if (old_highlight)
f676886a 5731 frame_unhighlight (old_highlight);
0f941935
KH
5732 if (dpyinfo->x_highlight_frame)
5733 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5734 }
dc6f92b8 5735}
06a2c219
GM
5736
5737
dc6f92b8 5738\f
06a2c219 5739/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5740
28430d3c
JB
5741/* Initialize mode_switch_bit and modifier_meaning. */
5742static void
334208b7
RS
5743x_find_modifier_meanings (dpyinfo)
5744 struct x_display_info *dpyinfo;
28430d3c 5745{
f689eb05 5746 int min_code, max_code;
28430d3c
JB
5747 KeySym *syms;
5748 int syms_per_code;
5749 XModifierKeymap *mods;
5750
334208b7
RS
5751 dpyinfo->meta_mod_mask = 0;
5752 dpyinfo->shift_lock_mask = 0;
5753 dpyinfo->alt_mod_mask = 0;
5754 dpyinfo->super_mod_mask = 0;
5755 dpyinfo->hyper_mod_mask = 0;
58769bee 5756
9658a521 5757#ifdef HAVE_X11R4
334208b7 5758 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5759#else
4a60f8c5
RS
5760 min_code = dpyinfo->display->min_keycode;
5761 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5762#endif
5763
334208b7 5764 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5765 min_code, max_code - min_code + 1,
5766 &syms_per_code);
334208b7 5767 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5768
58769bee 5769 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5770 Alt keysyms are on. */
28430d3c 5771 {
06a2c219 5772 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5773
5774 for (row = 3; row < 8; row++)
5775 for (col = 0; col < mods->max_keypermod; col++)
5776 {
0299d313
RS
5777 KeyCode code
5778 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5779
af92970c
KH
5780 /* Zeroes are used for filler. Skip them. */
5781 if (code == 0)
5782 continue;
5783
28430d3c
JB
5784 /* Are any of this keycode's keysyms a meta key? */
5785 {
5786 int code_col;
5787
5788 for (code_col = 0; code_col < syms_per_code; code_col++)
5789 {
f689eb05 5790 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5791
f689eb05 5792 switch (sym)
28430d3c 5793 {
f689eb05
JB
5794 case XK_Meta_L:
5795 case XK_Meta_R:
334208b7 5796 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5797 break;
f689eb05
JB
5798
5799 case XK_Alt_L:
5800 case XK_Alt_R:
334208b7 5801 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5802 break;
5803
5804 case XK_Hyper_L:
5805 case XK_Hyper_R:
334208b7 5806 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5807 break;
5808
5809 case XK_Super_L:
5810 case XK_Super_R:
334208b7 5811 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5812 break;
11edeb03
JB
5813
5814 case XK_Shift_Lock:
5815 /* Ignore this if it's not on the lock modifier. */
5816 if ((1 << row) == LockMask)
334208b7 5817 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5818 break;
28430d3c
JB
5819 }
5820 }
5821 }
5822 }
5823 }
5824
f689eb05 5825 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5826 if (! dpyinfo->meta_mod_mask)
a3c44b14 5827 {
334208b7
RS
5828 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5829 dpyinfo->alt_mod_mask = 0;
a3c44b14 5830 }
f689eb05 5831
148c4b70
RS
5832 /* If some keys are both alt and meta,
5833 make them just meta, not alt. */
334208b7 5834 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5835 {
334208b7 5836 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5837 }
58769bee 5838
28430d3c 5839 XFree ((char *) syms);
f689eb05 5840 XFreeModifiermap (mods);
28430d3c
JB
5841}
5842
dfeccd2d
JB
5843/* Convert between the modifier bits X uses and the modifier bits
5844 Emacs uses. */
06a2c219 5845
7c5283e4 5846static unsigned int
334208b7
RS
5847x_x_to_emacs_modifiers (dpyinfo, state)
5848 struct x_display_info *dpyinfo;
dc6f92b8
JB
5849 unsigned int state;
5850{
334208b7
RS
5851 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5852 | ((state & ControlMask) ? ctrl_modifier : 0)
5853 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5854 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5855 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5856 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5857}
5858
dfeccd2d 5859static unsigned int
334208b7
RS
5860x_emacs_to_x_modifiers (dpyinfo, state)
5861 struct x_display_info *dpyinfo;
dfeccd2d
JB
5862 unsigned int state;
5863{
334208b7
RS
5864 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5865 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5866 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5867 | ((state & shift_modifier) ? ShiftMask : 0)
5868 | ((state & ctrl_modifier) ? ControlMask : 0)
5869 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5870}
d047c4eb
KH
5871
5872/* Convert a keysym to its name. */
5873
5874char *
5875x_get_keysym_name (keysym)
5876 KeySym keysym;
5877{
5878 char *value;
5879
5880 BLOCK_INPUT;
5881 value = XKeysymToString (keysym);
5882 UNBLOCK_INPUT;
5883
5884 return value;
5885}
06a2c219
GM
5886
5887
e4571a43
JB
5888\f
5889/* Mouse clicks and mouse movement. Rah. */
e4571a43 5890
06a2c219
GM
5891/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5892 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5893 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5894 not force the value into range. */
69388238 5895
c8dba240 5896void
69388238 5897pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5898 FRAME_PTR f;
69388238 5899 register int pix_x, pix_y;
e4571a43
JB
5900 register int *x, *y;
5901 XRectangle *bounds;
69388238 5902 int noclip;
e4571a43 5903{
06a2c219 5904 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5905 even for negative values. */
5906 if (pix_x < 0)
7556890b 5907 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5908 if (pix_y < 0)
7556890b 5909 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5910
e4571a43
JB
5911 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5912 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5913
5914 if (bounds)
5915 {
7556890b
RS
5916 bounds->width = FONT_WIDTH (f->output_data.x->font);
5917 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5918 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5919 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5920 }
5921
69388238
RS
5922 if (!noclip)
5923 {
5924 if (pix_x < 0)
5925 pix_x = 0;
3cbd2e0b
RS
5926 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5927 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5928
5929 if (pix_y < 0)
5930 pix_y = 0;
5931 else if (pix_y > f->height)
5932 pix_y = f->height;
5933 }
e4571a43
JB
5934
5935 *x = pix_x;
5936 *y = pix_y;
5937}
5938
06a2c219
GM
5939
5940/* Given HPOS/VPOS in the current matrix of W, return corresponding
5941 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5942 can't tell the positions because W's display is not up to date,
5943 return 0. */
5944
5945int
5946glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5947 struct window *w;
5948 int hpos, vpos;
5949 int *frame_x, *frame_y;
2b5c9e71 5950{
06a2c219
GM
5951 int success_p;
5952
5953 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5954 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5955
5956 if (display_completed)
5957 {
5958 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5959 struct glyph *glyph = row->glyphs[TEXT_AREA];
5960 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5961
5962 *frame_y = row->y;
5963 *frame_x = row->x;
5964 while (glyph < end)
5965 {
5966 *frame_x += glyph->pixel_width;
5967 ++glyph;
5968 }
5969
5970 success_p = 1;
5971 }
5972 else
5973 {
5974 *frame_y = *frame_x = 0;
5975 success_p = 0;
5976 }
5977
5978 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5979 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5980 return success_p;
2b5c9e71
RS
5981}
5982
06a2c219 5983
dc6f92b8
JB
5984/* Prepare a mouse-event in *RESULT for placement in the input queue.
5985
5986 If the event is a button press, then note that we have grabbed
f451eb13 5987 the mouse. */
dc6f92b8
JB
5988
5989static Lisp_Object
f451eb13 5990construct_mouse_click (result, event, f)
dc6f92b8
JB
5991 struct input_event *result;
5992 XButtonEvent *event;
f676886a 5993 struct frame *f;
dc6f92b8 5994{
f451eb13 5995 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5996 otherwise. */
f451eb13 5997 result->kind = mouse_click;
69388238 5998 result->code = event->button - Button1;
1113d9db 5999 result->timestamp = event->time;
334208b7
RS
6000 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6001 event->state)
f689eb05 6002 | (event->type == ButtonRelease
58769bee 6003 ? up_modifier
f689eb05 6004 : down_modifier));
dc6f92b8 6005
06a2c219
GM
6006 XSETINT (result->x, event->x);
6007 XSETINT (result->y, event->y);
6008 XSETFRAME (result->frame_or_window, f);
6009 return Qnil;
dc6f92b8 6010}
b849c413 6011
06a2c219
GM
6012#if 0 /* This function isn't called. --gerd */
6013
b849c413
RS
6014/* Prepare a menu-event in *RESULT for placement in the input queue. */
6015
6016static Lisp_Object
6017construct_menu_click (result, event, f)
6018 struct input_event *result;
6019 XButtonEvent *event;
6020 struct frame *f;
6021{
6022 /* Make the event type no_event; we'll change that when we decide
6023 otherwise. */
6024 result->kind = mouse_click;
26459b28 6025 result->code = event->button - Button1;
b849c413 6026 result->timestamp = event->time;
334208b7
RS
6027 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6028 event->state)
b849c413 6029 | (event->type == ButtonRelease
58769bee 6030 ? up_modifier
b849c413
RS
6031 : down_modifier));
6032
e0c1aef2
KH
6033 XSETINT (result->x, event->x);
6034 XSETINT (result->y, -1);
6035 XSETFRAME (result->frame_or_window, f);
b849c413 6036}
06a2c219
GM
6037
6038#endif /* 0 */
6039
69388238 6040\f
90e65f07
JB
6041/* Function to report a mouse movement to the mainstream Emacs code.
6042 The input handler calls this.
6043
6044 We have received a mouse movement event, which is given in *event.
6045 If the mouse is over a different glyph than it was last time, tell
6046 the mainstream emacs code by setting mouse_moved. If not, ask for
6047 another motion event, so we can check again the next time it moves. */
b8009dd1 6048
06a2c219
GM
6049static XMotionEvent last_mouse_motion_event;
6050static Lisp_Object last_mouse_motion_frame;
6051
90e65f07 6052static void
12ba150f 6053note_mouse_movement (frame, event)
f676886a 6054 FRAME_PTR frame;
90e65f07 6055 XMotionEvent *event;
90e65f07 6056{
e5d77022 6057 last_mouse_movement_time = event->time;
06a2c219
GM
6058 last_mouse_motion_event = *event;
6059 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6060
27f338af
RS
6061 if (event->window != FRAME_X_WINDOW (frame))
6062 {
39d8bb4d 6063 frame->mouse_moved = 1;
27f338af 6064 last_mouse_scroll_bar = Qnil;
27f338af 6065 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6066 }
6067
90e65f07 6068 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6069 else if (event->x < last_mouse_glyph.x
6070 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6071 || event->y < last_mouse_glyph.y
6072 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6073 {
39d8bb4d 6074 frame->mouse_moved = 1;
ab648270 6075 last_mouse_scroll_bar = Qnil;
b8009dd1 6076 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6077 }
6078}
6079
bf1c0ba1 6080/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6081
06a2c219
GM
6082 int disable_mouse_highlight;
6083
6084
6085\f
6086/************************************************************************
6087 Mouse Face
6088 ************************************************************************/
6089
6090/* Find the glyph under window-relative coordinates X/Y in window W.
6091 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6092 strings. Return in *HPOS and *VPOS the row and column number of
6093 the glyph found. Return in *AREA the glyph area containing X.
6094 Value is a pointer to the glyph found or null if X/Y is not on
6095 text, or we can't tell because W's current matrix is not up to
6096 date. */
6097
6098static struct glyph *
6099x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6100 struct window *w;
6101 int x, y;
6102 int *hpos, *vpos, *area;
6103{
6104 struct glyph *glyph, *end;
6105 struct glyph_row *row;
6106 int x0, i, left_area_width;
6107
6108 /* Find row containing Y. Give up if some row is not enabled. */
6109 for (i = 0; i < w->current_matrix->nrows; ++i)
6110 {
6111 row = MATRIX_ROW (w->current_matrix, i);
6112 if (!row->enabled_p)
6113 return NULL;
6114 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6115 break;
6116 }
6117
6118 *vpos = i;
6119 *hpos = 0;
6120
6121 /* Give up if Y is not in the window. */
6122 if (i == w->current_matrix->nrows)
6123 return NULL;
6124
6125 /* Get the glyph area containing X. */
6126 if (w->pseudo_window_p)
6127 {
6128 *area = TEXT_AREA;
6129 x0 = 0;
6130 }
6131 else
6132 {
6133 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6134 if (x < left_area_width)
6135 {
6136 *area = LEFT_MARGIN_AREA;
6137 x0 = 0;
6138 }
6139 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6140 {
6141 *area = TEXT_AREA;
6142 x0 = row->x + left_area_width;
6143 }
6144 else
6145 {
6146 *area = RIGHT_MARGIN_AREA;
6147 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6148 }
6149 }
6150
6151 /* Find glyph containing X. */
6152 glyph = row->glyphs[*area];
6153 end = glyph + row->used[*area];
6154 while (glyph < end)
6155 {
6156 if (x < x0 + glyph->pixel_width)
6157 {
6158 if (w->pseudo_window_p)
6159 break;
6160 else if (BUFFERP (glyph->object))
6161 break;
6162 }
6163
6164 x0 += glyph->pixel_width;
6165 ++glyph;
6166 }
6167
6168 if (glyph == end)
6169 return NULL;
6170
6171 *hpos = glyph - row->glyphs[*area];
6172 return glyph;
6173}
6174
6175
6176/* Convert frame-relative x/y to coordinates relative to window W.
6177 Takes pseudo-windows into account. */
6178
6179static void
6180frame_to_window_pixel_xy (w, x, y)
6181 struct window *w;
6182 int *x, *y;
6183{
6184 if (w->pseudo_window_p)
6185 {
6186 /* A pseudo-window is always full-width, and starts at the
6187 left edge of the frame, plus a frame border. */
6188 struct frame *f = XFRAME (w->frame);
6189 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6190 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6191 }
6192 else
6193 {
6194 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6195 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6196 }
6197}
6198
6199
6200/* Take proper action when mouse has moved to the mode or top line of
6201 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6202 mode line. X is relative to the start of the text display area of
6203 W, so the width of bitmap areas and scroll bars must be subtracted
6204 to get a position relative to the start of the mode line. */
6205
6206static void
6207note_mode_line_highlight (w, x, mode_line_p)
6208 struct window *w;
6209 int x, mode_line_p;
6210{
6211 struct frame *f = XFRAME (w->frame);
6212 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6213 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6214 struct glyph_row *row;
6215
6216 if (mode_line_p)
6217 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6218 else
045dee35 6219 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6220
6221 if (row->enabled_p)
6222 {
6223 struct glyph *glyph, *end;
6224 Lisp_Object help, map;
6225 int x0;
6226
6227 /* Find the glyph under X. */
6228 glyph = row->glyphs[TEXT_AREA];
6229 end = glyph + row->used[TEXT_AREA];
6230 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6231 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6232 while (glyph < end
6233 && x >= x0 + glyph->pixel_width)
6234 {
6235 x0 += glyph->pixel_width;
6236 ++glyph;
6237 }
6238
6239 if (glyph < end
6240 && STRINGP (glyph->object)
6241 && XSTRING (glyph->object)->intervals
6242 && glyph->charpos >= 0
6243 && glyph->charpos < XSTRING (glyph->object)->size)
6244 {
6245 /* If we're on a string with `help-echo' text property,
6246 arrange for the help to be displayed. This is done by
6247 setting the global variable help_echo to the help string. */
6248 help = Fget_text_property (make_number (glyph->charpos),
6249 Qhelp_echo, glyph->object);
6250 if (STRINGP (help))
6251 help_echo = help;
6252
6253 /* Change the mouse pointer according to what is under X/Y. */
6254 map = Fget_text_property (make_number (glyph->charpos),
6255 Qlocal_map, glyph->object);
6256 if (!NILP (Fkeymapp (map)))
6257 cursor = f->output_data.x->nontext_cursor;
6258 }
6259 }
6260
6261 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6262}
6263
6264
6265/* Take proper action when the mouse has moved to position X, Y on
6266 frame F as regards highlighting characters that have mouse-face
6267 properties. Also de-highlighting chars where the mouse was before.
27f338af 6268 X and Y can be negative or out of range. */
b8009dd1
RS
6269
6270static void
6271note_mouse_highlight (f, x, y)
06a2c219 6272 struct frame *f;
c32cdd9a 6273 int x, y;
b8009dd1 6274{
06a2c219
GM
6275 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6276 int portion;
b8009dd1
RS
6277 Lisp_Object window;
6278 struct window *w;
6279
06a2c219
GM
6280 /* When a menu is active, don't highlight because this looks odd. */
6281#ifdef USE_X_TOOLKIT
6282 if (popup_activated ())
6283 return;
6284#endif
6285
04fff9c0
GM
6286 if (disable_mouse_highlight
6287 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6288 return;
6289
06a2c219
GM
6290 dpyinfo->mouse_face_mouse_x = x;
6291 dpyinfo->mouse_face_mouse_y = y;
6292 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6293
06a2c219 6294 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6295 return;
6296
514e4681
RS
6297 if (gc_in_progress)
6298 {
06a2c219 6299 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6300 return;
6301 }
6302
b8009dd1 6303 /* Which window is that in? */
06a2c219 6304 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6305
6306 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6307 if (! EQ (window, dpyinfo->mouse_face_window))
6308 clear_mouse_face (dpyinfo);
6309
6310 /* Not on a window -> return. */
6311 if (!WINDOWP (window))
6312 return;
6313
6314 /* Convert to window-relative pixel coordinates. */
6315 w = XWINDOW (window);
6316 frame_to_window_pixel_xy (w, &x, &y);
6317
9ea173e8 6318 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6319 buffer. */
9ea173e8 6320 if (EQ (window, f->tool_bar_window))
06a2c219 6321 {
9ea173e8 6322 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6323 return;
6324 }
6325
6326 if (portion == 1 || portion == 3)
6327 {
6328 /* Mouse is on the mode or top line. */
6329 note_mode_line_highlight (w, x, portion == 1);
6330 return;
6331 }
6332 else
6333 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6334 f->output_data.x->text_cursor);
b8009dd1 6335
0cdd0c9f
RS
6336 /* Are we in a window whose display is up to date?
6337 And verify the buffer's text has not changed. */
06a2c219
GM
6338 if (/* Within text portion of the window. */
6339 portion == 0
0cdd0c9f 6340 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6341 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6342 && (XFASTINT (w->last_overlay_modified)
6343 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6344 {
06a2c219
GM
6345 int hpos, vpos, pos, i, area;
6346 struct glyph *glyph;
b8009dd1 6347
06a2c219
GM
6348 /* Find the glyph under X/Y. */
6349 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6350
6351 /* Clear mouse face if X/Y not over text. */
6352 if (glyph == NULL
6353 || area != TEXT_AREA
6354 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6355 {
06a2c219
GM
6356 clear_mouse_face (dpyinfo);
6357 return;
6358 }
6359
6360 pos = glyph->charpos;
6361 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6362
6363 /* Check for mouse-face and help-echo. */
6364 {
6365 Lisp_Object mouse_face, overlay, position;
6366 Lisp_Object *overlay_vec;
6367 int len, noverlays;
6368 struct buffer *obuf;
6369 int obegv, ozv;
6370
6371 /* If we get an out-of-range value, return now; avoid an error. */
6372 if (pos > BUF_Z (XBUFFER (w->buffer)))
6373 return;
6374
6375 /* Make the window's buffer temporarily current for
6376 overlays_at and compute_char_face. */
6377 obuf = current_buffer;
6378 current_buffer = XBUFFER (w->buffer);
6379 obegv = BEGV;
6380 ozv = ZV;
6381 BEGV = BEG;
6382 ZV = Z;
6383
6384 /* Is this char mouse-active or does it have help-echo? */
6385 XSETINT (position, pos);
6386
6387 /* Put all the overlays we want in a vector in overlay_vec.
6388 Store the length in len. If there are more than 10, make
6389 enough space for all, and try again. */
6390 len = 10;
6391 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6392 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6393 if (noverlays > len)
6394 {
6395 len = noverlays;
6396 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6397 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6398 }
6399
6400 noverlays = sort_overlays (overlay_vec, noverlays, w);
6401
6402 /* Check mouse-face highlighting. */
6403 if (! (EQ (window, dpyinfo->mouse_face_window)
6404 && vpos >= dpyinfo->mouse_face_beg_row
6405 && vpos <= dpyinfo->mouse_face_end_row
6406 && (vpos > dpyinfo->mouse_face_beg_row
6407 || hpos >= dpyinfo->mouse_face_beg_col)
6408 && (vpos < dpyinfo->mouse_face_end_row
6409 || hpos < dpyinfo->mouse_face_end_col
6410 || dpyinfo->mouse_face_past_end)))
6411 {
6412 /* Clear the display of the old active region, if any. */
6413 clear_mouse_face (dpyinfo);
6414
6415 /* Find the highest priority overlay that has a mouse-face prop. */
6416 overlay = Qnil;
6417 for (i = 0; i < noverlays; i++)
6418 {
6419 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6420 if (!NILP (mouse_face))
6421 {
6422 overlay = overlay_vec[i];
6423 break;
6424 }
6425 }
6426
6427 /* If no overlay applies, get a text property. */
6428 if (NILP (overlay))
6429 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6430
6431 /* Handle the overlay case. */
6432 if (! NILP (overlay))
6433 {
6434 /* Find the range of text around this char that
6435 should be active. */
6436 Lisp_Object before, after;
6437 int ignore;
6438
6439 before = Foverlay_start (overlay);
6440 after = Foverlay_end (overlay);
6441 /* Record this as the current active region. */
6442 fast_find_position (w, XFASTINT (before),
6443 &dpyinfo->mouse_face_beg_col,
6444 &dpyinfo->mouse_face_beg_row,
6445 &dpyinfo->mouse_face_beg_x,
6446 &dpyinfo->mouse_face_beg_y);
6447 dpyinfo->mouse_face_past_end
6448 = !fast_find_position (w, XFASTINT (after),
6449 &dpyinfo->mouse_face_end_col,
6450 &dpyinfo->mouse_face_end_row,
6451 &dpyinfo->mouse_face_end_x,
6452 &dpyinfo->mouse_face_end_y);
6453 dpyinfo->mouse_face_window = window;
6454 dpyinfo->mouse_face_face_id
6455 = face_at_buffer_position (w, pos, 0, 0,
6456 &ignore, pos + 1, 1);
6457
6458 /* Display it as active. */
6459 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6460 }
6461 /* Handle the text property case. */
6462 else if (! NILP (mouse_face))
6463 {
6464 /* Find the range of text around this char that
6465 should be active. */
6466 Lisp_Object before, after, beginning, end;
6467 int ignore;
6468
6469 beginning = Fmarker_position (w->start);
6470 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6471 - XFASTINT (w->window_end_pos)));
6472 before
6473 = Fprevious_single_property_change (make_number (pos + 1),
6474 Qmouse_face,
6475 w->buffer, beginning);
6476 after
6477 = Fnext_single_property_change (position, Qmouse_face,
6478 w->buffer, end);
6479 /* Record this as the current active region. */
6480 fast_find_position (w, XFASTINT (before),
6481 &dpyinfo->mouse_face_beg_col,
6482 &dpyinfo->mouse_face_beg_row,
6483 &dpyinfo->mouse_face_beg_x,
6484 &dpyinfo->mouse_face_beg_y);
6485 dpyinfo->mouse_face_past_end
6486 = !fast_find_position (w, XFASTINT (after),
6487 &dpyinfo->mouse_face_end_col,
6488 &dpyinfo->mouse_face_end_row,
6489 &dpyinfo->mouse_face_end_x,
6490 &dpyinfo->mouse_face_end_y);
6491 dpyinfo->mouse_face_window = window;
6492 dpyinfo->mouse_face_face_id
6493 = face_at_buffer_position (w, pos, 0, 0,
6494 &ignore, pos + 1, 1);
6495
6496 /* Display it as active. */
6497 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6498 }
6499 }
6500
6501 /* Look for a `help-echo' property. */
6502 {
6503 Lisp_Object help;
6504
6505 /* Check overlays first. */
6506 help = Qnil;
6507 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6508 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6509
6510 /* Try text properties. */
6511 if (!STRINGP (help)
6512 && ((STRINGP (glyph->object)
6513 && glyph->charpos >= 0
6514 && glyph->charpos < XSTRING (glyph->object)->size)
6515 || (BUFFERP (glyph->object)
6516 && glyph->charpos >= BEGV
6517 && glyph->charpos < ZV)))
6518 help = Fget_text_property (make_number (glyph->charpos),
6519 Qhelp_echo, glyph->object);
6520
6521 if (STRINGP (help))
6522 help_echo = help;
6523 }
6524
6525 BEGV = obegv;
6526 ZV = ozv;
6527 current_buffer = obuf;
6528 }
6529 }
6530}
6531
6532static void
6533redo_mouse_highlight ()
6534{
6535 if (!NILP (last_mouse_motion_frame)
6536 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6537 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6538 last_mouse_motion_event.x,
6539 last_mouse_motion_event.y);
6540}
6541
6542
6543\f
6544/***********************************************************************
9ea173e8 6545 Tool-bars
06a2c219
GM
6546 ***********************************************************************/
6547
9ea173e8
GM
6548static int x_tool_bar_item P_ ((struct frame *, int, int,
6549 struct glyph **, int *, int *, int *));
06a2c219 6550
9ea173e8 6551/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6552 or -1. */
6553
9ea173e8 6554static int last_tool_bar_item;
06a2c219
GM
6555
6556
9ea173e8
GM
6557/* Get information about the tool-bar item at position X/Y on frame F.
6558 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6559 the current matrix of the tool-bar window of F, or NULL if not
6560 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6561 item in F->current_tool_bar_items. Value is
06a2c219 6562
9ea173e8 6563 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6564 0 if X/Y is on the same item that was highlighted before.
6565 1 otherwise. */
6566
6567static int
9ea173e8 6568x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6569 struct frame *f;
6570 int x, y;
6571 struct glyph **glyph;
6572 int *hpos, *vpos, *prop_idx;
6573{
6574 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6575 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6576 int area;
6577
6578 /* Find the glyph under X/Y. */
6579 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6580 if (*glyph == NULL)
6581 return -1;
6582
9ea173e8
GM
6583 /* Get the start of this tool-bar item's properties in
6584 f->current_tool_bar_items. */
6585 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6586 return -1;
6587
6588 /* Is mouse on the highlighted item? */
9ea173e8 6589 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6590 && *vpos >= dpyinfo->mouse_face_beg_row
6591 && *vpos <= dpyinfo->mouse_face_end_row
6592 && (*vpos > dpyinfo->mouse_face_beg_row
6593 || *hpos >= dpyinfo->mouse_face_beg_col)
6594 && (*vpos < dpyinfo->mouse_face_end_row
6595 || *hpos < dpyinfo->mouse_face_end_col
6596 || dpyinfo->mouse_face_past_end))
6597 return 0;
6598
6599 return 1;
6600}
6601
6602
9ea173e8 6603/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6604 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6605 or ButtonRelase. */
6606
6607static void
9ea173e8 6608x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6609 struct frame *f;
6610 XButtonEvent *button_event;
6611{
6612 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6613 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6614 int hpos, vpos, prop_idx;
6615 struct glyph *glyph;
6616 Lisp_Object enabled_p;
6617 int x = button_event->x;
6618 int y = button_event->y;
6619
9ea173e8 6620 /* If not on the highlighted tool-bar item, return. */
06a2c219 6621 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6622 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6623 return;
6624
6625 /* If item is disabled, do nothing. */
9ea173e8
GM
6626 enabled_p = (XVECTOR (f->current_tool_bar_items)
6627 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6628 if (NILP (enabled_p))
6629 return;
6630
6631 if (button_event->type == ButtonPress)
6632 {
6633 /* Show item in pressed state. */
6634 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6635 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6636 last_tool_bar_item = prop_idx;
06a2c219
GM
6637 }
6638 else
6639 {
6640 Lisp_Object key, frame;
6641 struct input_event event;
6642
6643 /* Show item in released state. */
6644 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6645 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6646
9ea173e8
GM
6647 key = (XVECTOR (f->current_tool_bar_items)
6648 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6649
6650 XSETFRAME (frame, f);
9ea173e8
GM
6651 event.kind = TOOL_BAR_EVENT;
6652 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6653 kbd_buffer_store_event (&event);
6654
9ea173e8 6655 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6656 event.frame_or_window = Fcons (frame, key);
6657 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6658 button_event->state);
6659 kbd_buffer_store_event (&event);
9ea173e8 6660 last_tool_bar_item = -1;
06a2c219
GM
6661 }
6662}
6663
6664
9ea173e8
GM
6665/* Possibly highlight a tool-bar item on frame F when mouse moves to
6666 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6667 note_mouse_highlight. */
6668
6669static void
9ea173e8 6670note_tool_bar_highlight (f, x, y)
06a2c219
GM
6671 struct frame *f;
6672 int x, y;
6673{
9ea173e8 6674 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6675 struct window *w = XWINDOW (window);
6676 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6677 int hpos, vpos;
6678 struct glyph *glyph;
6679 struct glyph_row *row;
5c187dee 6680 int i;
06a2c219
GM
6681 Lisp_Object enabled_p;
6682 int prop_idx;
6683 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6684 int mouse_down_p, rc;
06a2c219
GM
6685
6686 /* Function note_mouse_highlight is called with negative x(y
6687 values when mouse moves outside of the frame. */
6688 if (x <= 0 || y <= 0)
6689 {
6690 clear_mouse_face (dpyinfo);
6691 return;
6692 }
6693
9ea173e8 6694 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6695 if (rc < 0)
6696 {
9ea173e8 6697 /* Not on tool-bar item. */
06a2c219
GM
6698 clear_mouse_face (dpyinfo);
6699 return;
6700 }
6701 else if (rc == 0)
9ea173e8 6702 /* On same tool-bar item as before. */
06a2c219 6703 goto set_help_echo;
b8009dd1 6704
06a2c219
GM
6705 clear_mouse_face (dpyinfo);
6706
9ea173e8 6707 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6708 mouse_down_p = (dpyinfo->grabbed
6709 && f == last_mouse_frame
6710 && FRAME_LIVE_P (f));
6711 if (mouse_down_p
9ea173e8 6712 && last_tool_bar_item != prop_idx)
06a2c219
GM
6713 return;
6714
6715 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6716 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6717
9ea173e8
GM
6718 /* If tool-bar item is not enabled, don't highlight it. */
6719 enabled_p = (XVECTOR (f->current_tool_bar_items)
6720 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6721 if (!NILP (enabled_p))
6722 {
6723 /* Compute the x-position of the glyph. In front and past the
6724 image is a space. We include this is the highlighted area. */
6725 row = MATRIX_ROW (w->current_matrix, vpos);
6726 for (i = x = 0; i < hpos; ++i)
6727 x += row->glyphs[TEXT_AREA][i].pixel_width;
6728
6729 /* Record this as the current active region. */
6730 dpyinfo->mouse_face_beg_col = hpos;
6731 dpyinfo->mouse_face_beg_row = vpos;
6732 dpyinfo->mouse_face_beg_x = x;
6733 dpyinfo->mouse_face_beg_y = row->y;
6734 dpyinfo->mouse_face_past_end = 0;
6735
6736 dpyinfo->mouse_face_end_col = hpos + 1;
6737 dpyinfo->mouse_face_end_row = vpos;
6738 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6739 dpyinfo->mouse_face_end_y = row->y;
6740 dpyinfo->mouse_face_window = window;
9ea173e8 6741 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6742
6743 /* Display it as active. */
6744 show_mouse_face (dpyinfo, draw);
6745 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6746 }
06a2c219
GM
6747
6748 set_help_echo:
6749
9ea173e8 6750 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6751 XTread_socket does the rest. */
9ea173e8
GM
6752 help_echo = (XVECTOR (f->current_tool_bar_items)
6753 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6754 if (!STRINGP (help_echo))
9ea173e8
GM
6755 help_echo = (XVECTOR (f->current_tool_bar_items)
6756 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6757}
4d73d038 6758
06a2c219
GM
6759
6760\f
6761/* Find the glyph matrix position of buffer position POS in window W.
6762 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6763 current glyphs must be up to date. If POS is above window start
6764 return (0, 0, 0, 0). If POS is after end of W, return end of
6765 last line in W. */
b8009dd1
RS
6766
6767static int
06a2c219
GM
6768fast_find_position (w, pos, hpos, vpos, x, y)
6769 struct window *w;
b8009dd1 6770 int pos;
06a2c219 6771 int *hpos, *vpos, *x, *y;
b8009dd1 6772{
b8009dd1 6773 int i;
bf1c0ba1 6774 int lastcol;
06a2c219
GM
6775 int maybe_next_line_p = 0;
6776 int line_start_position;
6777 int yb = window_text_bottom_y (w);
6778 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6779 struct glyph_row *best_row = row;
6780 int row_vpos = 0, best_row_vpos = 0;
6781 int current_x;
6782
6783 while (row->y < yb)
b8009dd1 6784 {
06a2c219
GM
6785 if (row->used[TEXT_AREA])
6786 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6787 else
6788 line_start_position = 0;
6789
6790 if (line_start_position > pos)
b8009dd1 6791 break;
77b68646
RS
6792 /* If the position sought is the end of the buffer,
6793 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6794 else if (line_start_position == pos
6795 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6796 {
06a2c219 6797 maybe_next_line_p = 1;
77b68646
RS
6798 break;
6799 }
06a2c219
GM
6800 else if (line_start_position > 0)
6801 {
6802 best_row = row;
6803 best_row_vpos = row_vpos;
6804 }
4b0bb6f3
GM
6805
6806 if (row->y + row->height >= yb)
6807 break;
06a2c219
GM
6808
6809 ++row;
6810 ++row_vpos;
b8009dd1 6811 }
06a2c219
GM
6812
6813 /* Find the right column within BEST_ROW. */
6814 lastcol = 0;
6815 current_x = best_row->x;
6816 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6817 {
06a2c219
GM
6818 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6819 int charpos;
6820
6821 charpos = glyph->charpos;
6822 if (charpos == pos)
bf1c0ba1 6823 {
06a2c219
GM
6824 *hpos = i;
6825 *vpos = best_row_vpos;
6826 *x = current_x;
6827 *y = best_row->y;
bf1c0ba1
RS
6828 return 1;
6829 }
06a2c219 6830 else if (charpos > pos)
4d73d038 6831 break;
06a2c219
GM
6832 else if (charpos > 0)
6833 lastcol = i;
6834
6835 current_x += glyph->pixel_width;
bf1c0ba1 6836 }
b8009dd1 6837
77b68646
RS
6838 /* If we're looking for the end of the buffer,
6839 and we didn't find it in the line we scanned,
6840 use the start of the following line. */
06a2c219 6841 if (maybe_next_line_p)
77b68646 6842 {
06a2c219
GM
6843 ++best_row;
6844 ++best_row_vpos;
6845 lastcol = 0;
6846 current_x = best_row->x;
77b68646
RS
6847 }
6848
06a2c219
GM
6849 *vpos = best_row_vpos;
6850 *hpos = lastcol + 1;
6851 *x = current_x;
6852 *y = best_row->y;
b8009dd1
RS
6853 return 0;
6854}
6855
06a2c219 6856
b8009dd1
RS
6857/* Display the active region described by mouse_face_*
6858 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6859
6860static void
06a2c219 6861show_mouse_face (dpyinfo, draw)
7a13e894 6862 struct x_display_info *dpyinfo;
06a2c219 6863 enum draw_glyphs_face draw;
b8009dd1 6864{
7a13e894 6865 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6866 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6867 int i;
06a2c219
GM
6868 int cursor_off_p = 0;
6869 struct cursor_pos saved_cursor;
6870
6871 saved_cursor = output_cursor;
6872
6873 /* If window is in the process of being destroyed, don't bother
6874 to do anything. */
6875 if (w->current_matrix == NULL)
6876 goto set_x_cursor;
6877
6878 /* Recognize when we are called to operate on rows that don't exist
6879 anymore. This can happen when a window is split. */
6880 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6881 goto set_x_cursor;
6882
6883 set_output_cursor (&w->phys_cursor);
6884
6885 /* Note that mouse_face_beg_row etc. are window relative. */
6886 for (i = dpyinfo->mouse_face_beg_row;
6887 i <= dpyinfo->mouse_face_end_row;
6888 i++)
6889 {
6890 int start_hpos, end_hpos, start_x;
6891 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6892
6893 /* Don't do anything if row doesn't have valid contents. */
6894 if (!row->enabled_p)
6895 continue;
6896
6897 /* For all but the first row, the highlight starts at column 0. */
6898 if (i == dpyinfo->mouse_face_beg_row)
6899 {
6900 start_hpos = dpyinfo->mouse_face_beg_col;
6901 start_x = dpyinfo->mouse_face_beg_x;
6902 }
6903 else
6904 {
6905 start_hpos = 0;
6906 start_x = 0;
6907 }
6908
6909 if (i == dpyinfo->mouse_face_end_row)
6910 end_hpos = dpyinfo->mouse_face_end_col;
6911 else
6912 end_hpos = row->used[TEXT_AREA];
6913
6914 /* If the cursor's in the text we are about to rewrite, turn the
6915 cursor off. */
6916 if (!w->pseudo_window_p
6917 && i == output_cursor.vpos
6918 && output_cursor.hpos >= start_hpos - 1
6919 && output_cursor.hpos <= end_hpos)
514e4681 6920 {
06a2c219
GM
6921 x_update_window_cursor (w, 0);
6922 cursor_off_p = 1;
514e4681 6923 }
b8009dd1 6924
06a2c219 6925 if (end_hpos > start_hpos)
54a91a0f 6926 x_draw_glyphs (w, start_x, row, TEXT_AREA,
66ac4b0e 6927 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6928 }
6929
514e4681 6930 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6931 if (cursor_off_p)
6932 x_display_cursor (w, 1,
6933 output_cursor.hpos, output_cursor.vpos,
6934 output_cursor.x, output_cursor.y);
2729a2b5 6935
06a2c219 6936 output_cursor = saved_cursor;
fb3b7de5 6937
06a2c219
GM
6938 set_x_cursor:
6939
6940 /* Change the mouse cursor. */
6941 if (draw == DRAW_NORMAL_TEXT)
6942 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6943 f->output_data.x->text_cursor);
6944 else if (draw == DRAW_MOUSE_FACE)
334208b7 6945 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6946 f->output_data.x->cross_cursor);
27ead1d5 6947 else
334208b7 6948 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6949 f->output_data.x->nontext_cursor);
b8009dd1
RS
6950}
6951
6952/* Clear out the mouse-highlighted active region.
06a2c219 6953 Redraw it un-highlighted first. */
b8009dd1 6954
06a2c219 6955void
7a13e894
RS
6956clear_mouse_face (dpyinfo)
6957 struct x_display_info *dpyinfo;
b8009dd1 6958{
06a2c219
GM
6959 if (tip_frame)
6960 return;
6961
7a13e894 6962 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6963 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6964
7a13e894
RS
6965 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6966 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6967 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6968}
e687d06e
RS
6969
6970/* Just discard the mouse face information for frame F, if any.
6971 This is used when the size of F is changed. */
6972
dfcf069d 6973void
e687d06e
RS
6974cancel_mouse_face (f)
6975 FRAME_PTR f;
6976{
6977 Lisp_Object window;
6978 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6979
6980 window = dpyinfo->mouse_face_window;
6981 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6982 {
6983 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6984 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6985 dpyinfo->mouse_face_window = Qnil;
6986 }
6987}
b8009dd1 6988\f
ab648270
JB
6989static struct scroll_bar *x_window_to_scroll_bar ();
6990static void x_scroll_bar_report_motion ();
12ba150f 6991
90e65f07 6992/* Return the current position of the mouse.
2d7fc7e8 6993 *fp should be a frame which indicates which display to ask about.
90e65f07 6994
2d7fc7e8 6995 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6996 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6997 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6998 position on the scroll bar.
12ba150f 6999
2d7fc7e8 7000 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7001 mouse is on, *bar_window to nil, and *x and *y to the character cell
7002 the mouse is over.
7003
06a2c219 7004 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7005 was at this position.
7006
a135645a
RS
7007 Don't store anything if we don't have a valid set of values to report.
7008
90e65f07 7009 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7010 movement. */
90e65f07
JB
7011
7012static void
1cf412ec 7013XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7014 FRAME_PTR *fp;
1cf412ec 7015 int insist;
12ba150f 7016 Lisp_Object *bar_window;
ab648270 7017 enum scroll_bar_part *part;
90e65f07 7018 Lisp_Object *x, *y;
e5d77022 7019 unsigned long *time;
90e65f07 7020{
a135645a
RS
7021 FRAME_PTR f1;
7022
90e65f07
JB
7023 BLOCK_INPUT;
7024
8bcee03e 7025 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7026 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7027 else
7028 {
12ba150f
JB
7029 Window root;
7030 int root_x, root_y;
90e65f07 7031
12ba150f
JB
7032 Window dummy_window;
7033 int dummy;
7034
39d8bb4d
KH
7035 Lisp_Object frame, tail;
7036
7037 /* Clear the mouse-moved flag for every frame on this display. */
7038 FOR_EACH_FRAME (tail, frame)
7039 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7040 XFRAME (frame)->mouse_moved = 0;
7041
ab648270 7042 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7043
7044 /* Figure out which root window we're on. */
334208b7
RS
7045 XQueryPointer (FRAME_X_DISPLAY (*fp),
7046 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7047
7048 /* The root window which contains the pointer. */
7049 &root,
7050
7051 /* Trash which we can't trust if the pointer is on
7052 a different screen. */
7053 &dummy_window,
7054
7055 /* The position on that root window. */
58769bee 7056 &root_x, &root_y,
12ba150f
JB
7057
7058 /* More trash we can't trust. */
7059 &dummy, &dummy,
7060
7061 /* Modifier keys and pointer buttons, about which
7062 we don't care. */
7063 (unsigned int *) &dummy);
7064
7065 /* Now we have a position on the root; find the innermost window
7066 containing the pointer. */
7067 {
7068 Window win, child;
7069 int win_x, win_y;
06a2c219 7070 int parent_x = 0, parent_y = 0;
e99db5a1 7071 int count;
12ba150f
JB
7072
7073 win = root;
69388238 7074
2d7fc7e8
RS
7075 /* XTranslateCoordinates can get errors if the window
7076 structure is changing at the same time this function
7077 is running. So at least we must not crash from them. */
7078
e99db5a1 7079 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7080
334208b7 7081 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7082 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7083 {
69388238
RS
7084 /* If mouse was grabbed on a frame, give coords for that frame
7085 even if the mouse is now outside it. */
334208b7 7086 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7087
12ba150f 7088 /* From-window, to-window. */
69388238 7089 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7090
7091 /* From-position, to-position. */
7092 root_x, root_y, &win_x, &win_y,
7093
7094 /* Child of win. */
7095 &child);
69388238
RS
7096 f1 = last_mouse_frame;
7097 }
7098 else
7099 {
7100 while (1)
7101 {
334208b7 7102 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7103
69388238
RS
7104 /* From-window, to-window. */
7105 root, win,
12ba150f 7106
69388238
RS
7107 /* From-position, to-position. */
7108 root_x, root_y, &win_x, &win_y,
7109
7110 /* Child of win. */
7111 &child);
7112
9af3143a 7113 if (child == None || child == win)
69388238
RS
7114 break;
7115
7116 win = child;
7117 parent_x = win_x;
7118 parent_y = win_y;
7119 }
12ba150f 7120
69388238
RS
7121 /* Now we know that:
7122 win is the innermost window containing the pointer
7123 (XTC says it has no child containing the pointer),
7124 win_x and win_y are the pointer's position in it
7125 (XTC did this the last time through), and
7126 parent_x and parent_y are the pointer's position in win's parent.
7127 (They are what win_x and win_y were when win was child.
7128 If win is the root window, it has no parent, and
7129 parent_{x,y} are invalid, but that's okay, because we'll
7130 never use them in that case.) */
7131
7132 /* Is win one of our frames? */
19126e11 7133 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7134 }
58769bee 7135
2d7fc7e8
RS
7136 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7137 f1 = 0;
7138
e99db5a1 7139 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7140
ab648270 7141 /* If not, is it one of our scroll bars? */
a135645a 7142 if (! f1)
12ba150f 7143 {
ab648270 7144 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7145
7146 if (bar)
7147 {
a135645a 7148 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7149 win_x = parent_x;
7150 win_y = parent_y;
7151 }
7152 }
90e65f07 7153
8bcee03e 7154 if (f1 == 0 && insist > 0)
b86bd3dd 7155 f1 = SELECTED_FRAME ();
1cf412ec 7156
a135645a 7157 if (f1)
12ba150f 7158 {
06a2c219
GM
7159 /* Ok, we found a frame. Store all the values.
7160 last_mouse_glyph is a rectangle used to reduce the
7161 generation of mouse events. To not miss any motion
7162 events, we must divide the frame into rectangles of the
7163 size of the smallest character that could be displayed
7164 on it, i.e. into the same rectangles that matrices on
7165 the frame are divided into. */
7166
7167#if OLD_REDISPLAY_CODE
2b5c9e71 7168 int ignore1, ignore2;
2b5c9e71 7169 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7170 &last_mouse_glyph,
1cf412ec
RS
7171 FRAME_X_DISPLAY_INFO (f1)->grabbed
7172 || insist);
06a2c219
GM
7173#else
7174 {
7175 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7176 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7177 int x = win_x;
7178 int y = win_y;
7179
7180 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7181 round down even for negative values. */
7182 if (x < 0)
7183 x -= width - 1;
7184 if (y < 0)
7185 y -= height - 1;
7186
7187 last_mouse_glyph.width = width;
7188 last_mouse_glyph.height = height;
7189 last_mouse_glyph.x = (x + width - 1) / width * width;
7190 last_mouse_glyph.y = (y + height - 1) / height * height;
7191 }
7192#endif
12ba150f
JB
7193
7194 *bar_window = Qnil;
7195 *part = 0;
334208b7 7196 *fp = f1;
e0c1aef2
KH
7197 XSETINT (*x, win_x);
7198 XSETINT (*y, win_y);
12ba150f
JB
7199 *time = last_mouse_movement_time;
7200 }
7201 }
7202 }
90e65f07
JB
7203
7204 UNBLOCK_INPUT;
7205}
f451eb13 7206
06a2c219 7207
06a2c219 7208#ifdef USE_X_TOOLKIT
bffcfca9
GM
7209
7210/* Atimer callback function for TIMER. Called every 0.1s to process
7211 Xt timeouts, if needed. We must avoid calling XtAppPending as
7212 much as possible because that function does an implicit XFlush
7213 that slows us down. */
7214
7215static void
7216x_process_timeouts (timer)
7217 struct atimer *timer;
7218{
7219 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7220 {
7221 BLOCK_INPUT;
7222 while (XtAppPending (Xt_app_con) & XtIMTimer)
7223 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7224 UNBLOCK_INPUT;
7225 }
06a2c219
GM
7226}
7227
bffcfca9 7228#endif /* USE_X_TOOLKIT */
06a2c219
GM
7229
7230\f
7231/* Scroll bar support. */
7232
7233/* Given an X window ID, find the struct scroll_bar which manages it.
7234 This can be called in GC, so we have to make sure to strip off mark
7235 bits. */
bffcfca9 7236
06a2c219
GM
7237static struct scroll_bar *
7238x_window_to_scroll_bar (window_id)
7239 Window window_id;
7240{
7241 Lisp_Object tail;
7242
7243 for (tail = Vframe_list;
7244 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7245 tail = XCDR (tail))
06a2c219
GM
7246 {
7247 Lisp_Object frame, bar, condemned;
7248
8e713be6 7249 frame = XCAR (tail);
06a2c219
GM
7250 /* All elements of Vframe_list should be frames. */
7251 if (! GC_FRAMEP (frame))
7252 abort ();
7253
7254 /* Scan this frame's scroll bar list for a scroll bar with the
7255 right window ID. */
7256 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7257 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7258 /* This trick allows us to search both the ordinary and
7259 condemned scroll bar lists with one loop. */
7260 ! GC_NILP (bar) || (bar = condemned,
7261 condemned = Qnil,
7262 ! GC_NILP (bar));
7263 bar = XSCROLL_BAR (bar)->next)
7264 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7265 return XSCROLL_BAR (bar);
7266 }
7267
7268 return 0;
7269}
7270
7271
7272\f
7273/************************************************************************
7274 Toolkit scroll bars
7275 ************************************************************************/
7276
7277#if USE_TOOLKIT_SCROLL_BARS
7278
7279static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7280static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7281static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7282 struct scroll_bar *));
7283static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7284 int, int, int));
7285
7286
7287/* Id of action hook installed for scroll bars. */
7288
7289static XtActionHookId action_hook_id;
7290
7291/* Lisp window being scrolled. Set when starting to interact with
7292 a toolkit scroll bar, reset to nil when ending the interaction. */
7293
7294static Lisp_Object window_being_scrolled;
7295
7296/* Last scroll bar part sent in xm_scroll_callback. */
7297
7298static int last_scroll_bar_part;
7299
ec18280f
SM
7300/* Whether this is an Xaw with arrow-scrollbars. This should imply
7301 that movements of 1/20 of the screen size are mapped to up/down. */
7302
7303static Boolean xaw3d_arrow_scroll;
7304
7305/* Whether the drag scrolling maintains the mouse at the top of the
7306 thumb. If not, resizing the thumb needs to be done more carefully
7307 to avoid jerkyness. */
7308
7309static Boolean xaw3d_pick_top;
7310
06a2c219
GM
7311
7312/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7313 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7314 the user ends an interaction with the scroll bar, and generates
7315 a `end-scroll' scroll_bar_click' event if so. */
7316
7317static void
7318xt_action_hook (widget, client_data, action_name, event, params,
7319 num_params)
7320 Widget widget;
7321 XtPointer client_data;
7322 String action_name;
7323 XEvent *event;
7324 String *params;
7325 Cardinal *num_params;
7326{
7327 int scroll_bar_p;
7328 char *end_action;
7329
7330#ifdef USE_MOTIF
7331 scroll_bar_p = XmIsScrollBar (widget);
7332 end_action = "Release";
ec18280f 7333#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7334 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7335 end_action = "EndScroll";
ec18280f 7336#endif /* USE_MOTIF */
06a2c219 7337
06a2c219
GM
7338 if (scroll_bar_p
7339 && strcmp (action_name, end_action) == 0
7340 && WINDOWP (window_being_scrolled))
7341 {
7342 struct window *w;
7343
7344 x_send_scroll_bar_event (window_being_scrolled,
7345 scroll_bar_end_scroll, 0, 0);
7346 w = XWINDOW (window_being_scrolled);
7347 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7348 window_being_scrolled = Qnil;
7349 last_scroll_bar_part = -1;
bffcfca9
GM
7350
7351 /* Xt timeouts no longer needed. */
7352 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7353 }
7354}
7355
7356
7357/* Send a client message with message type Xatom_Scrollbar for a
7358 scroll action to the frame of WINDOW. PART is a value identifying
7359 the part of the scroll bar that was clicked on. PORTION is the
7360 amount to scroll of a whole of WHOLE. */
7361
7362static void
7363x_send_scroll_bar_event (window, part, portion, whole)
7364 Lisp_Object window;
7365 int part, portion, whole;
7366{
7367 XEvent event;
7368 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7369 struct frame *f = XFRAME (XWINDOW (window)->frame);
7370
7371 /* Construct a ClientMessage event to send to the frame. */
7372 ev->type = ClientMessage;
7373 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7374 ev->display = FRAME_X_DISPLAY (f);
7375 ev->window = FRAME_X_WINDOW (f);
7376 ev->format = 32;
7377 ev->data.l[0] = (long) window;
7378 ev->data.l[1] = (long) part;
7379 ev->data.l[2] = (long) 0;
7380 ev->data.l[3] = (long) portion;
7381 ev->data.l[4] = (long) whole;
7382
bffcfca9
GM
7383 /* Make Xt timeouts work while the scroll bar is active. */
7384 toolkit_scroll_bar_interaction = 1;
7385
06a2c219
GM
7386 /* Setting the event mask to zero means that the message will
7387 be sent to the client that created the window, and if that
7388 window no longer exists, no event will be sent. */
7389 BLOCK_INPUT;
7390 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7391 UNBLOCK_INPUT;
7392}
7393
7394
7395/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7396 in *IEVENT. */
7397
7398static void
7399x_scroll_bar_to_input_event (event, ievent)
7400 XEvent *event;
7401 struct input_event *ievent;
7402{
7403 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7404 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7405 struct frame *f = XFRAME (XWINDOW (window)->frame);
7406
7407 ievent->kind = scroll_bar_click;
7408 ievent->frame_or_window = window;
7409 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7410 ievent->part = ev->data.l[1];
7411 ievent->code = ev->data.l[2];
7412 ievent->x = make_number ((int) ev->data.l[3]);
7413 ievent->y = make_number ((int) ev->data.l[4]);
7414 ievent->modifiers = 0;
7415}
7416
7417
7418#ifdef USE_MOTIF
7419
7420/* Minimum and maximum values used for Motif scroll bars. */
7421
7422#define XM_SB_MIN 1
7423#define XM_SB_MAX 10000000
7424#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7425
7426
7427/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7428 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7429 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7430
7431static void
7432xm_scroll_callback (widget, client_data, call_data)
7433 Widget widget;
7434 XtPointer client_data, call_data;
7435{
7436 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7437 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7438 double percent;
7439 int part = -1, whole = 0, portion = 0;
7440
7441 switch (cs->reason)
7442 {
7443 case XmCR_DECREMENT:
7444 bar->dragging = Qnil;
7445 part = scroll_bar_up_arrow;
7446 break;
7447
7448 case XmCR_INCREMENT:
7449 bar->dragging = Qnil;
7450 part = scroll_bar_down_arrow;
7451 break;
7452
7453 case XmCR_PAGE_DECREMENT:
7454 bar->dragging = Qnil;
7455 part = scroll_bar_above_handle;
7456 break;
7457
7458 case XmCR_PAGE_INCREMENT:
7459 bar->dragging = Qnil;
7460 part = scroll_bar_below_handle;
7461 break;
7462
7463 case XmCR_TO_TOP:
7464 bar->dragging = Qnil;
7465 part = scroll_bar_to_top;
7466 break;
7467
7468 case XmCR_TO_BOTTOM:
7469 bar->dragging = Qnil;
7470 part = scroll_bar_to_bottom;
7471 break;
7472
7473 case XmCR_DRAG:
7474 {
7475 int slider_size;
7476 int dragging_down_p = (INTEGERP (bar->dragging)
7477 && XINT (bar->dragging) <= cs->value);
7478
7479 /* Get the slider size. */
7480 BLOCK_INPUT;
7481 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7482 UNBLOCK_INPUT;
7483
7484 /* At the max position of the scroll bar, do a line-wise
7485 movement. Without doing anything, the LessTif scroll bar
7486 calls us with the same cs->value again and again. If we
7487 want to make sure that we can reach the end of the buffer,
7488 we have to do something.
7489
7490 Implementation note: setting bar->dragging always to
7491 cs->value gives a smoother movement at the max position.
7492 Setting it to nil when doing line-wise movement gives
7493 a better slider behavior. */
7494
7495 if (cs->value + slider_size == XM_SB_MAX
7496 || (dragging_down_p
7497 && last_scroll_bar_part == scroll_bar_down_arrow))
7498 {
7499 part = scroll_bar_down_arrow;
7500 bar->dragging = Qnil;
7501 }
7502 else
7503 {
7504 whole = XM_SB_RANGE;
7505 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7506 part = scroll_bar_handle;
7507 bar->dragging = make_number (cs->value);
7508 }
7509 }
7510 break;
7511
7512 case XmCR_VALUE_CHANGED:
7513 break;
7514 };
7515
7516 if (part >= 0)
7517 {
7518 window_being_scrolled = bar->window;
7519 last_scroll_bar_part = part;
7520 x_send_scroll_bar_event (bar->window, part, portion, whole);
7521 }
7522}
7523
7524
ec18280f 7525#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7526
7527
ec18280f 7528/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7529 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7530 scroll bar struct. CALL_DATA is a pointer to a float saying where
7531 the thumb is. */
7532
7533static void
ec18280f 7534xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7535 Widget widget;
7536 XtPointer client_data, call_data;
7537{
7538 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7539 float top = *(float *) call_data;
7540 float shown;
ec18280f
SM
7541 int whole, portion, height;
7542 int part;
06a2c219
GM
7543
7544 /* Get the size of the thumb, a value between 0 and 1. */
7545 BLOCK_INPUT;
ec18280f 7546 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7547 UNBLOCK_INPUT;
7548
7549 whole = 10000000;
7550 portion = shown < 1 ? top * whole : 0;
06a2c219 7551
ec18280f
SM
7552 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7553 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7554 the bottom, so we force the scrolling whenever we see that we're
7555 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7556 we try to ensure that we always stay two pixels away from the
7557 bottom). */
06a2c219
GM
7558 part = scroll_bar_down_arrow;
7559 else
7560 part = scroll_bar_handle;
7561
7562 window_being_scrolled = bar->window;
7563 bar->dragging = make_number (portion);
7564 last_scroll_bar_part = part;
7565 x_send_scroll_bar_event (bar->window, part, portion, whole);
7566}
7567
7568
ec18280f
SM
7569/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7570 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7571 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7572 the scroll bar. CALL_DATA is an integer specifying the action that
7573 has taken place. It's magnitude is in the range 0..height of the
7574 scroll bar. Negative values mean scroll towards buffer start.
7575 Values < height of scroll bar mean line-wise movement. */
7576
7577static void
ec18280f 7578xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7579 Widget widget;
7580 XtPointer client_data, call_data;
7581{
7582 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7583 int position = (int) call_data;
7584 Dimension height;
7585 int part;
7586
7587 /* Get the height of the scroll bar. */
7588 BLOCK_INPUT;
7589 XtVaGetValues (widget, XtNheight, &height, NULL);
7590 UNBLOCK_INPUT;
7591
ec18280f
SM
7592 if (abs (position) >= height)
7593 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7594
7595 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7596 it maps line-movement to call_data = max(5, height/20). */
7597 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7598 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7599 else
ec18280f 7600 part = scroll_bar_move_ratio;
06a2c219
GM
7601
7602 window_being_scrolled = bar->window;
7603 bar->dragging = Qnil;
7604 last_scroll_bar_part = part;
ec18280f 7605 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7606}
7607
7608
7609#endif /* not USE_MOTIF */
7610
7611
7612/* Create the widget for scroll bar BAR on frame F. Record the widget
7613 and X window of the scroll bar in BAR. */
7614
7615static void
7616x_create_toolkit_scroll_bar (f, bar)
7617 struct frame *f;
7618 struct scroll_bar *bar;
7619{
7620 Window xwindow;
7621 Widget widget;
7622 Arg av[20];
7623 int ac = 0;
7624 char *scroll_bar_name = "verticalScrollBar";
7625 unsigned long pixel;
7626
7627 BLOCK_INPUT;
7628
7629#ifdef USE_MOTIF
7630 /* LessTif 0.85, problems:
7631
7632 1. When the mouse if over the scroll bar, the scroll bar will
7633 get keyboard events. I didn't find a way to turn this off.
7634
7635 2. Do we have to explicitly set the cursor to get an arrow
7636 cursor (see below)? */
7637
7638 /* Set resources. Create the widget. */
7639 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7640 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7641 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7642 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7643 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7644 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7645 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7646
7647 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7648 if (pixel != -1)
7649 {
7650 XtSetArg (av[ac], XmNforeground, pixel);
7651 ++ac;
7652 }
7653
7654 pixel = f->output_data.x->scroll_bar_background_pixel;
7655 if (pixel != -1)
7656 {
7657 XtSetArg (av[ac], XmNbackground, pixel);
7658 ++ac;
7659 }
7660
7661 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7662 scroll_bar_name, av, ac);
7663
7664 /* Add one callback for everything that can happen. */
7665 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7666 (XtPointer) bar);
7667 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7668 (XtPointer) bar);
7669 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7670 (XtPointer) bar);
7671 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7672 (XtPointer) bar);
7673 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7674 (XtPointer) bar);
7675 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7676 (XtPointer) bar);
7677 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7678 (XtPointer) bar);
7679
7680 /* Realize the widget. Only after that is the X window created. */
7681 XtRealizeWidget (widget);
7682
7683 /* Set the cursor to an arrow. I didn't find a resource to do that.
7684 And I'm wondering why it hasn't an arrow cursor by default. */
7685 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7686 f->output_data.x->nontext_cursor);
7687
ec18280f 7688#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7689
7690 /* Set resources. Create the widget. The background of the
7691 Xaw3d scroll bar widget is a little bit light for my taste.
7692 We don't alter it here to let users change it according
7693 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7694 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7695 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7696 /* For smoother scrolling with Xaw3d -sm */
7697 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7698 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7699
7700 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7701 if (pixel != -1)
7702 {
7703 XtSetArg (av[ac], XtNforeground, pixel);
7704 ++ac;
7705 }
7706
7707 pixel = f->output_data.x->scroll_bar_background_pixel;
7708 if (pixel != -1)
7709 {
7710 XtSetArg (av[ac], XtNbackground, pixel);
7711 ++ac;
7712 }
7713
7714 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7715 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7716
7717 {
7718 char *initial = "";
7719 char *val = initial;
7720 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7721 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7722 if (val == initial)
7723 { /* ARROW_SCROLL */
7724 xaw3d_arrow_scroll = True;
7725 /* Isn't that just a personal preference ? -sm */
7726 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7727 }
7728 }
06a2c219
GM
7729
7730 /* Define callbacks. */
ec18280f
SM
7731 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7732 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7733 (XtPointer) bar);
7734
7735 /* Realize the widget. Only after that is the X window created. */
7736 XtRealizeWidget (widget);
7737
ec18280f 7738#endif /* !USE_MOTIF */
06a2c219
GM
7739
7740 /* Install an action hook that let's us detect when the user
7741 finishes interacting with a scroll bar. */
7742 if (action_hook_id == 0)
7743 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7744
7745 /* Remember X window and widget in the scroll bar vector. */
7746 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7747 xwindow = XtWindow (widget);
7748 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7749
7750 UNBLOCK_INPUT;
7751}
7752
7753
7754/* Set the thumb size and position of scroll bar BAR. We are currently
7755 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7756
7757static void
7758x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7759 struct scroll_bar *bar;
7760 int portion, position, whole;
f451eb13 7761{
06a2c219 7762 float top, shown;
06a2c219 7763 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7764
06a2c219
GM
7765 if (whole == 0)
7766 top = 0, shown = 1;
7767 else
f451eb13 7768 {
06a2c219
GM
7769 top = (float) position / whole;
7770 shown = (float) portion / whole;
7771 }
f451eb13 7772
06a2c219 7773 BLOCK_INPUT;
f451eb13 7774
06a2c219
GM
7775#ifdef USE_MOTIF
7776 {
7777 int size, value;
7778 Boolean arrow1_selected, arrow2_selected;
7779 unsigned char flags;
7780 XmScrollBarWidget sb;
7781
ec18280f 7782 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7783 is the scroll bar's maximum and MIN is the scroll bar's minimum
7784 value. */
7785 size = shown * XM_SB_RANGE;
7786 size = min (size, XM_SB_RANGE);
7787 size = max (size, 1);
7788
7789 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7790 value = top * XM_SB_RANGE;
7791 value = min (value, XM_SB_MAX - size);
7792 value = max (value, XM_SB_MIN);
7793
7794 /* LessTif: Calling XmScrollBarSetValues after an increment or
7795 decrement turns off auto-repeat LessTif-internally. This can
7796 be seen in ScrollBar.c which resets Arrow1Selected and
7797 Arrow2Selected. It also sets internal flags so that LessTif
7798 believes the mouse is in the slider. We either have to change
7799 our code, or work around that by accessing private data. */
7800
7801 sb = (XmScrollBarWidget) widget;
7802 arrow1_selected = sb->scrollBar.arrow1_selected;
7803 arrow2_selected = sb->scrollBar.arrow2_selected;
7804 flags = sb->scrollBar.flags;
7805
7806 if (NILP (bar->dragging))
7807 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7808 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7809 /* This has the negative side effect that the slider value is
ec18280f 7810 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7811 page-wise movement. */
7812 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7813 else
7814 {
7815 /* If currently dragging, only update the slider size.
7816 This reduces flicker effects. */
7817 int old_value, old_size, increment, page_increment;
7818
7819 XmScrollBarGetValues (widget, &old_value, &old_size,
7820 &increment, &page_increment);
7821 XmScrollBarSetValues (widget, old_value,
7822 min (size, XM_SB_RANGE - old_value),
7823 0, 0, False);
7824 }
7825
7826 sb->scrollBar.arrow1_selected = arrow1_selected;
7827 sb->scrollBar.arrow2_selected = arrow2_selected;
7828 sb->scrollBar.flags = flags;
7829 }
ec18280f 7830#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7831 {
ec18280f
SM
7832 float old_top, old_shown;
7833 Dimension height;
7834 XtVaGetValues (widget,
7835 XtNtopOfThumb, &old_top,
7836 XtNshown, &old_shown,
7837 XtNheight, &height,
7838 NULL);
7839
7840 /* Massage the top+shown values. */
7841 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7842 top = max (0, min (1, top));
7843 else
7844 top = old_top;
7845 /* Keep two pixels available for moving the thumb down. */
7846 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7847
7848 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7849 check that your system's configuration file contains a define
7850 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7851 if (top != old_top || shown != old_shown)
eb393530 7852 {
ec18280f 7853 if (NILP (bar->dragging))
eb393530 7854 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7855 else
7856 {
ec18280f
SM
7857#ifdef HAVE_XAW3D
7858 ScrollbarWidget sb = (ScrollbarWidget) widget;
7859 int scroll_mode;
7860
7861 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7862 if (xaw3d_arrow_scroll)
7863 {
7864 /* Xaw3d stupidly ignores resize requests while dragging
7865 so we have to make it believe it's not in dragging mode. */
7866 scroll_mode = sb->scrollbar.scroll_mode;
7867 if (scroll_mode == 2)
7868 sb->scrollbar.scroll_mode = 0;
7869 }
7870#endif
7871 /* Try to make the scrolling a tad smoother. */
7872 if (!xaw3d_pick_top)
7873 shown = min (shown, old_shown);
7874
7875 XawScrollbarSetThumb (widget, top, shown);
7876
7877#ifdef HAVE_XAW3D
7878 if (xaw3d_arrow_scroll && scroll_mode == 2)
7879 sb->scrollbar.scroll_mode = scroll_mode;
7880#endif
06a2c219 7881 }
06a2c219
GM
7882 }
7883 }
ec18280f 7884#endif /* !USE_MOTIF */
06a2c219
GM
7885
7886 UNBLOCK_INPUT;
f451eb13
JB
7887}
7888
06a2c219
GM
7889#endif /* USE_TOOLKIT_SCROLL_BARS */
7890
7891
7892\f
7893/************************************************************************
7894 Scroll bars, general
7895 ************************************************************************/
7896
7897/* Create a scroll bar and return the scroll bar vector for it. W is
7898 the Emacs window on which to create the scroll bar. TOP, LEFT,
7899 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7900 scroll bar. */
7901
ab648270 7902static struct scroll_bar *
06a2c219
GM
7903x_scroll_bar_create (w, top, left, width, height)
7904 struct window *w;
f451eb13
JB
7905 int top, left, width, height;
7906{
06a2c219 7907 struct frame *f = XFRAME (w->frame);
334208b7
RS
7908 struct scroll_bar *bar
7909 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7910
7911 BLOCK_INPUT;
7912
06a2c219
GM
7913#if USE_TOOLKIT_SCROLL_BARS
7914 x_create_toolkit_scroll_bar (f, bar);
7915#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7916 {
7917 XSetWindowAttributes a;
7918 unsigned long mask;
5c187dee 7919 Window window;
06a2c219
GM
7920
7921 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7922 if (a.background_pixel == -1)
7923 a.background_pixel = f->output_data.x->background_pixel;
7924
12ba150f 7925 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7926 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7927 | ExposureMask);
7a13e894 7928 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7929
dbc4e1c1 7930 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7931
06a2c219
GM
7932 /* Clear the area of W that will serve as a scroll bar. This is
7933 for the case that a window has been split horizontally. In
7934 this case, no clear_frame is generated to reduce flickering. */
7935 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7936 left, top, width,
7937 window_box_height (w), False);
7938
7939 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7940 /* Position and size of scroll bar. */
7941 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7942 top,
7943 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7944 height,
7945 /* Border width, depth, class, and visual. */
7946 0,
7947 CopyFromParent,
7948 CopyFromParent,
7949 CopyFromParent,
7950 /* Attributes. */
7951 mask, &a);
7952 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7953 }
06a2c219 7954#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7955
06a2c219 7956 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7957 XSETINT (bar->top, top);
7958 XSETINT (bar->left, left);
7959 XSETINT (bar->width, width);
7960 XSETINT (bar->height, height);
7961 XSETINT (bar->start, 0);
7962 XSETINT (bar->end, 0);
12ba150f 7963 bar->dragging = Qnil;
f451eb13
JB
7964
7965 /* Add bar to its frame's list of scroll bars. */
334208b7 7966 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7967 bar->prev = Qnil;
334208b7 7968 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7969 if (!NILP (bar->next))
e0c1aef2 7970 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7971
06a2c219
GM
7972 /* Map the window/widget. */
7973#if USE_TOOLKIT_SCROLL_BARS
7974 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7975 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7976 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7977 top,
7978 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7979 height, 0);
7980#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7981 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7982#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7983
7984 UNBLOCK_INPUT;
12ba150f 7985 return bar;
f451eb13
JB
7986}
7987
06a2c219 7988
12ba150f 7989/* Draw BAR's handle in the proper position.
06a2c219 7990
12ba150f
JB
7991 If the handle is already drawn from START to END, don't bother
7992 redrawing it, unless REBUILD is non-zero; in that case, always
7993 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7994 events.)
12ba150f
JB
7995
7996 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7997 fit inside its rectangle, but if the user is dragging the scroll
7998 bar handle, we want to let them drag it down all the way, so that
7999 the bar's top is as far down as it goes; otherwise, there's no way
8000 to move to the very end of the buffer. */
8001
5c187dee
GM
8002#ifndef USE_TOOLKIT_SCROLL_BARS
8003
f451eb13 8004static void
ab648270
JB
8005x_scroll_bar_set_handle (bar, start, end, rebuild)
8006 struct scroll_bar *bar;
f451eb13 8007 int start, end;
12ba150f 8008 int rebuild;
f451eb13 8009{
12ba150f 8010 int dragging = ! NILP (bar->dragging);
ab648270 8011 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8012 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8013 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8014
8015 /* If the display is already accurate, do nothing. */
8016 if (! rebuild
8017 && start == XINT (bar->start)
8018 && end == XINT (bar->end))
8019 return;
8020
f451eb13
JB
8021 BLOCK_INPUT;
8022
8023 {
d9cdbb3d
RS
8024 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8025 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8026 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8027
8028 /* Make sure the values are reasonable, and try to preserve
8029 the distance between start and end. */
12ba150f
JB
8030 {
8031 int length = end - start;
8032
8033 if (start < 0)
8034 start = 0;
8035 else if (start > top_range)
8036 start = top_range;
8037 end = start + length;
8038
8039 if (end < start)
8040 end = start;
8041 else if (end > top_range && ! dragging)
8042 end = top_range;
8043 }
f451eb13 8044
ab648270 8045 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8046 XSETINT (bar->start, start);
8047 XSETINT (bar->end, end);
f451eb13 8048
12ba150f
JB
8049 /* Clip the end position, just for display. */
8050 if (end > top_range)
8051 end = top_range;
f451eb13 8052
ab648270 8053 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8054 below top positions, to make sure the handle is always at least
8055 that many pixels tall. */
ab648270 8056 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8057
12ba150f
JB
8058 /* Draw the empty space above the handle. Note that we can't clear
8059 zero-height areas; that means "clear to end of window." */
8060 if (0 < start)
334208b7 8061 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8062
12ba150f 8063 /* x, y, width, height, and exposures. */
ab648270
JB
8064 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8065 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8066 inside_width, start,
8067 False);
f451eb13 8068
06a2c219
GM
8069 /* Change to proper foreground color if one is specified. */
8070 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8071 XSetForeground (FRAME_X_DISPLAY (f), gc,
8072 f->output_data.x->scroll_bar_foreground_pixel);
8073
12ba150f 8074 /* Draw the handle itself. */
334208b7 8075 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8076
12ba150f 8077 /* x, y, width, height */
ab648270
JB
8078 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8079 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8080 inside_width, end - start);
f451eb13 8081
06a2c219
GM
8082 /* Restore the foreground color of the GC if we changed it above. */
8083 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8084 XSetForeground (FRAME_X_DISPLAY (f), gc,
8085 f->output_data.x->foreground_pixel);
f451eb13 8086
12ba150f
JB
8087 /* Draw the empty space below the handle. Note that we can't
8088 clear zero-height areas; that means "clear to end of window." */
8089 if (end < inside_height)
334208b7 8090 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8091
12ba150f 8092 /* x, y, width, height, and exposures. */
ab648270
JB
8093 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8094 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8095 inside_width, inside_height - end,
8096 False);
f451eb13 8097
f451eb13
JB
8098 }
8099
f451eb13
JB
8100 UNBLOCK_INPUT;
8101}
8102
5c187dee 8103#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8104
06a2c219
GM
8105/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8106 nil. */
58769bee 8107
12ba150f 8108static void
ab648270
JB
8109x_scroll_bar_remove (bar)
8110 struct scroll_bar *bar;
12ba150f 8111{
12ba150f
JB
8112 BLOCK_INPUT;
8113
06a2c219
GM
8114#if USE_TOOLKIT_SCROLL_BARS
8115 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8116#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8117 {
8118 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8119 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8120 }
06a2c219
GM
8121#endif /* not USE_TOOLKIT_SCROLL_BARS */
8122
ab648270
JB
8123 /* Disassociate this scroll bar from its window. */
8124 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8125
8126 UNBLOCK_INPUT;
8127}
8128
06a2c219 8129
12ba150f
JB
8130/* Set the handle of the vertical scroll bar for WINDOW to indicate
8131 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8132 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8133 create one. */
06a2c219 8134
12ba150f 8135static void
06a2c219
GM
8136XTset_vertical_scroll_bar (w, portion, whole, position)
8137 struct window *w;
f451eb13
JB
8138 int portion, whole, position;
8139{
06a2c219 8140 struct frame *f = XFRAME (w->frame);
ab648270 8141 struct scroll_bar *bar;
3c6ede7b 8142 int top, height, left, sb_left, width, sb_width;
06a2c219 8143 int window_x, window_y, window_width, window_height;
06a2c219 8144
3c6ede7b 8145 /* Get window dimensions. */
06a2c219 8146 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8147 top = window_y;
8148 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8149 height = window_height;
06a2c219 8150
3c6ede7b 8151 /* Compute the left edge of the scroll bar area. */
06a2c219 8152 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8153 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8154 else
8155 left = XFASTINT (w->left);
8156 left *= CANON_X_UNIT (f);
8157 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8158
8159 /* Compute the width of the scroll bar which might be less than
8160 the width of the area reserved for the scroll bar. */
8161 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8162 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8163 else
3c6ede7b 8164 sb_width = width;
12ba150f 8165
3c6ede7b
GM
8166 /* Compute the left edge of the scroll bar. */
8167#ifdef USE_TOOLKIT_SCROLL_BARS
8168 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8169 sb_left = left + width - sb_width - (width - sb_width) / 2;
8170 else
8171 sb_left = left + (width - sb_width) / 2;
8172#else
8173 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8174 sb_left = left + width - sb_width;
8175 else
8176 sb_left = left;
8177#endif
8178
ab648270 8179 /* Does the scroll bar exist yet? */
06a2c219 8180 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8181 {
80c32bcc 8182 BLOCK_INPUT;
3c6ede7b
GM
8183 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8184 left, top, width, height, False);
80c32bcc 8185 UNBLOCK_INPUT;
3c6ede7b
GM
8186 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8187 }
f451eb13 8188 else
12ba150f
JB
8189 {
8190 /* It may just need to be moved and resized. */
06a2c219
GM
8191 unsigned int mask = 0;
8192
8193 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8194
8195 BLOCK_INPUT;
8196
3c6ede7b 8197 if (sb_left != XINT (bar->left))
06a2c219 8198 mask |= CWX;
3c6ede7b 8199 if (top != XINT (bar->top))
06a2c219 8200 mask |= CWY;
3c6ede7b 8201 if (sb_width != XINT (bar->width))
06a2c219 8202 mask |= CWWidth;
3c6ede7b 8203 if (height != XINT (bar->height))
06a2c219
GM
8204 mask |= CWHeight;
8205
8206#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8207
8208 /* Since toolkit scroll bars are smaller than the space reserved
8209 for them on the frame, we have to clear "under" them. */
8210 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8211 left, top, width, height, False);
06a2c219
GM
8212
8213 /* Move/size the scroll bar widget. */
8214 if (mask)
8215 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8216 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8217 top,
8218 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8219 height, 0);
06a2c219
GM
8220
8221#else /* not USE_TOOLKIT_SCROLL_BARS */
8222
e1f6572f
RS
8223 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8224 {
8225 /* Clear areas not covered by the scroll bar. This makes sure a
8226 previous mode line display is cleared after C-x 2 C-x 1, for
8227 example. Non-toolkit scroll bars are as wide as the area
8228 reserved for scroll bars - trim at both sides. */
8229 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8230 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8231 height, False);
8232 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8233 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8234 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8235 height, False);
8236 }
06a2c219
GM
8237
8238 /* Move/size the scroll bar window. */
8239 if (mask)
8240 {
8241 XWindowChanges wc;
8242
3c6ede7b
GM
8243 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8244 wc.y = top;
8245 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8246 wc.height = height;
06a2c219
GM
8247 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8248 mask, &wc);
8249 }
8250
8251#endif /* not USE_TOOLKIT_SCROLL_BARS */
8252
8253 /* Remember new settings. */
3c6ede7b
GM
8254 XSETINT (bar->left, sb_left);
8255 XSETINT (bar->top, top);
8256 XSETINT (bar->width, sb_width);
8257 XSETINT (bar->height, height);
06a2c219
GM
8258
8259 UNBLOCK_INPUT;
12ba150f 8260 }
f451eb13 8261
06a2c219
GM
8262#if USE_TOOLKIT_SCROLL_BARS
8263 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8264#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8265 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8266 dragged. */
12ba150f 8267 if (NILP (bar->dragging))
f451eb13 8268 {
92857db0 8269 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8270
12ba150f 8271 if (whole == 0)
ab648270 8272 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8273 else
8274 {
43f868f5
JB
8275 int start = ((double) position * top_range) / whole;
8276 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8277 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8278 }
f451eb13 8279 }
06a2c219 8280#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8281
06a2c219 8282 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8283}
8284
12ba150f 8285
f451eb13 8286/* The following three hooks are used when we're doing a thorough
ab648270 8287 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8288 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8289 away is a real pain - "Can you say set-window-configuration, boys
8290 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8291 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8292 from the fiery pit when we actually redisplay its window. */
f451eb13 8293
ab648270
JB
8294/* Arrange for all scroll bars on FRAME to be removed at the next call
8295 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8296 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8297
58769bee 8298static void
ab648270 8299XTcondemn_scroll_bars (frame)
f451eb13
JB
8300 FRAME_PTR frame;
8301{
f9e24cb9
RS
8302 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8303 while (! NILP (FRAME_SCROLL_BARS (frame)))
8304 {
8305 Lisp_Object bar;
8306 bar = FRAME_SCROLL_BARS (frame);
8307 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8308 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8309 XSCROLL_BAR (bar)->prev = Qnil;
8310 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8311 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8312 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8313 }
f451eb13
JB
8314}
8315
06a2c219 8316/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8317 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8318static void
ab648270 8319XTredeem_scroll_bar (window)
12ba150f 8320 struct window *window;
f451eb13 8321{
ab648270 8322 struct scroll_bar *bar;
12ba150f 8323
ab648270
JB
8324 /* We can't redeem this window's scroll bar if it doesn't have one. */
8325 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8326 abort ();
8327
ab648270 8328 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8329
8330 /* Unlink it from the condemned list. */
8331 {
8332 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8333
8334 if (NILP (bar->prev))
8335 {
8336 /* If the prev pointer is nil, it must be the first in one of
8337 the lists. */
ab648270 8338 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8339 /* It's not condemned. Everything's fine. */
8340 return;
ab648270
JB
8341 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8342 window->vertical_scroll_bar))
8343 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8344 else
8345 /* If its prev pointer is nil, it must be at the front of
8346 one or the other! */
8347 abort ();
8348 }
8349 else
ab648270 8350 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8351
8352 if (! NILP (bar->next))
ab648270 8353 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8354
ab648270 8355 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8356 bar->prev = Qnil;
e0c1aef2 8357 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8358 if (! NILP (bar->next))
e0c1aef2 8359 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8360 }
f451eb13
JB
8361}
8362
ab648270
JB
8363/* Remove all scroll bars on FRAME that haven't been saved since the
8364 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8365
f451eb13 8366static void
ab648270 8367XTjudge_scroll_bars (f)
12ba150f 8368 FRAME_PTR f;
f451eb13 8369{
12ba150f 8370 Lisp_Object bar, next;
f451eb13 8371
ab648270 8372 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8373
8374 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8375 more events on the hapless scroll bars. */
8376 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8377
8378 for (; ! NILP (bar); bar = next)
f451eb13 8379 {
ab648270 8380 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8381
ab648270 8382 x_scroll_bar_remove (b);
12ba150f
JB
8383
8384 next = b->next;
8385 b->next = b->prev = Qnil;
f451eb13 8386 }
12ba150f 8387
ab648270 8388 /* Now there should be no references to the condemned scroll bars,
12ba150f 8389 and they should get garbage-collected. */
f451eb13
JB
8390}
8391
8392
06a2c219
GM
8393/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8394 is a no-op when using toolkit scroll bars.
ab648270
JB
8395
8396 This may be called from a signal handler, so we have to ignore GC
8397 mark bits. */
06a2c219 8398
f451eb13 8399static void
ab648270
JB
8400x_scroll_bar_expose (bar, event)
8401 struct scroll_bar *bar;
f451eb13
JB
8402 XEvent *event;
8403{
06a2c219
GM
8404#ifndef USE_TOOLKIT_SCROLL_BARS
8405
ab648270 8406 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8407 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8408 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8409 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8410
f451eb13
JB
8411 BLOCK_INPUT;
8412
ab648270 8413 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8414
06a2c219 8415 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8416 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8417
8418 /* x, y, width, height */
d9cdbb3d 8419 0, 0,
3cbd2e0b 8420 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8421 XINT (bar->height) - 1);
8422
f451eb13 8423 UNBLOCK_INPUT;
06a2c219
GM
8424
8425#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8426}
8427
ab648270
JB
8428/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8429 is set to something other than no_event, it is enqueued.
8430
8431 This may be called from a signal handler, so we have to ignore GC
8432 mark bits. */
06a2c219 8433
5c187dee
GM
8434#ifndef USE_TOOLKIT_SCROLL_BARS
8435
f451eb13 8436static void
ab648270
JB
8437x_scroll_bar_handle_click (bar, event, emacs_event)
8438 struct scroll_bar *bar;
f451eb13
JB
8439 XEvent *event;
8440 struct input_event *emacs_event;
8441{
0299d313 8442 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8443 abort ();
8444
ab648270 8445 emacs_event->kind = scroll_bar_click;
69388238 8446 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8447 emacs_event->modifiers
8448 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8449 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8450 event->xbutton.state)
8451 | (event->type == ButtonRelease
8452 ? up_modifier
8453 : down_modifier));
12ba150f 8454 emacs_event->frame_or_window = bar->window;
f451eb13 8455 emacs_event->timestamp = event->xbutton.time;
12ba150f 8456 {
06a2c219 8457#if 0
d9cdbb3d 8458 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8459 int internal_height
d9cdbb3d 8460 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8461#endif
0299d313 8462 int top_range
d9cdbb3d 8463 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8464 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8465
8466 if (y < 0) y = 0;
8467 if (y > top_range) y = top_range;
8468
8469 if (y < XINT (bar->start))
ab648270
JB
8470 emacs_event->part = scroll_bar_above_handle;
8471 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8472 emacs_event->part = scroll_bar_handle;
12ba150f 8473 else
ab648270 8474 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8475
8476 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8477 they want to drag it. Lisp code needs to be able to decide
8478 whether or not we're dragging. */
929787e1 8479#if 0
12ba150f
JB
8480 /* If the user has just clicked on the handle, record where they're
8481 holding it. */
8482 if (event->type == ButtonPress
ab648270 8483 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8484 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8485#endif
12ba150f
JB
8486
8487 /* If the user has released the handle, set it to its final position. */
8488 if (event->type == ButtonRelease
8489 && ! NILP (bar->dragging))
8490 {
8491 int new_start = y - XINT (bar->dragging);
8492 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8493
ab648270 8494 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8495 bar->dragging = Qnil;
8496 }
f451eb13 8497
5116f055
JB
8498 /* Same deal here as the other #if 0. */
8499#if 0
58769bee 8500 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8501 the handle. */
ab648270 8502 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8503 emacs_event->x = bar->start;
8504 else
e0c1aef2 8505 XSETINT (emacs_event->x, y);
5116f055 8506#else
e0c1aef2 8507 XSETINT (emacs_event->x, y);
5116f055 8508#endif
f451eb13 8509
e0c1aef2 8510 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8511 }
8512}
f451eb13 8513
ab648270
JB
8514/* Handle some mouse motion while someone is dragging the scroll bar.
8515
8516 This may be called from a signal handler, so we have to ignore GC
8517 mark bits. */
06a2c219 8518
f451eb13 8519static void
ab648270
JB
8520x_scroll_bar_note_movement (bar, event)
8521 struct scroll_bar *bar;
f451eb13
JB
8522 XEvent *event;
8523{
39d8bb4d
KH
8524 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8525
f451eb13
JB
8526 last_mouse_movement_time = event->xmotion.time;
8527
39d8bb4d 8528 f->mouse_moved = 1;
e0c1aef2 8529 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8530
8531 /* If we're dragging the bar, display it. */
ab648270 8532 if (! GC_NILP (bar->dragging))
f451eb13
JB
8533 {
8534 /* Where should the handle be now? */
12ba150f 8535 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8536
12ba150f 8537 if (new_start != XINT (bar->start))
f451eb13 8538 {
12ba150f 8539 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8540
ab648270 8541 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8542 }
8543 }
f451eb13
JB
8544}
8545
5c187dee
GM
8546#endif /* !USE_TOOLKIT_SCROLL_BARS */
8547
12ba150f 8548/* Return information to the user about the current position of the mouse
ab648270 8549 on the scroll bar. */
06a2c219 8550
12ba150f 8551static void
334208b7
RS
8552x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8553 FRAME_PTR *fp;
12ba150f 8554 Lisp_Object *bar_window;
ab648270 8555 enum scroll_bar_part *part;
12ba150f
JB
8556 Lisp_Object *x, *y;
8557 unsigned long *time;
8558{
ab648270 8559 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8560 Window w = SCROLL_BAR_X_WINDOW (bar);
8561 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8562 int win_x, win_y;
559cb2fb
JB
8563 Window dummy_window;
8564 int dummy_coord;
8565 unsigned int dummy_mask;
12ba150f 8566
cf7cb199
JB
8567 BLOCK_INPUT;
8568
ab648270 8569 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8570 report that. */
334208b7 8571 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8572
559cb2fb
JB
8573 /* Root, child, root x and root y. */
8574 &dummy_window, &dummy_window,
8575 &dummy_coord, &dummy_coord,
12ba150f 8576
559cb2fb
JB
8577 /* Position relative to scroll bar. */
8578 &win_x, &win_y,
12ba150f 8579
559cb2fb
JB
8580 /* Mouse buttons and modifier keys. */
8581 &dummy_mask))
7a13e894 8582 ;
559cb2fb
JB
8583 else
8584 {
06a2c219 8585#if 0
559cb2fb 8586 int inside_height
d9cdbb3d 8587 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8588#endif
559cb2fb 8589 int top_range
d9cdbb3d 8590 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8591
8592 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8593
8594 if (! NILP (bar->dragging))
8595 win_y -= XINT (bar->dragging);
8596
8597 if (win_y < 0)
8598 win_y = 0;
8599 if (win_y > top_range)
8600 win_y = top_range;
8601
334208b7 8602 *fp = f;
7a13e894 8603 *bar_window = bar->window;
559cb2fb
JB
8604
8605 if (! NILP (bar->dragging))
8606 *part = scroll_bar_handle;
8607 else if (win_y < XINT (bar->start))
8608 *part = scroll_bar_above_handle;
8609 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8610 *part = scroll_bar_handle;
8611 else
8612 *part = scroll_bar_below_handle;
12ba150f 8613
e0c1aef2
KH
8614 XSETINT (*x, win_y);
8615 XSETINT (*y, top_range);
12ba150f 8616
39d8bb4d 8617 f->mouse_moved = 0;
559cb2fb
JB
8618 last_mouse_scroll_bar = Qnil;
8619 }
12ba150f 8620
559cb2fb 8621 *time = last_mouse_movement_time;
cf7cb199 8622
cf7cb199 8623 UNBLOCK_INPUT;
12ba150f
JB
8624}
8625
f451eb13 8626
dbc4e1c1 8627/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8628 background colors, and the scroll bars may need to be redrawn.
8629 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8630 redraw them. */
8631
dfcf069d 8632void
ab648270 8633x_scroll_bar_clear (f)
dbc4e1c1
JB
8634 FRAME_PTR f;
8635{
06a2c219 8636#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8637 Lisp_Object bar;
8638
b80c363e
RS
8639 /* We can have scroll bars even if this is 0,
8640 if we just turned off scroll bar mode.
8641 But in that case we should not clear them. */
8642 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8643 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8644 bar = XSCROLL_BAR (bar)->next)
8645 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8646 0, 0, 0, 0, True);
06a2c219 8647#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8648}
8649
06a2c219 8650/* This processes Expose events from the menu-bar specific X event
19126e11 8651 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8652 when handling menu-bar or pop-up items. */
3afe33e7 8653
06a2c219 8654int
3afe33e7
RS
8655process_expose_from_menu (event)
8656 XEvent event;
8657{
8658 FRAME_PTR f;
19126e11 8659 struct x_display_info *dpyinfo;
06a2c219 8660 int frame_exposed_p = 0;
3afe33e7 8661
f94397b5
KH
8662 BLOCK_INPUT;
8663
19126e11
KH
8664 dpyinfo = x_display_info_for_display (event.xexpose.display);
8665 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8666 if (f)
8667 {
8668 if (f->async_visible == 0)
8669 {
8670 f->async_visible = 1;
8671 f->async_iconified = 0;
06c488fd 8672 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8673 SET_FRAME_GARBAGED (f);
8674 }
8675 else
8676 {
06a2c219
GM
8677 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8678 event.xexpose.x, event.xexpose.y,
8679 event.xexpose.width, event.xexpose.height);
8680 frame_exposed_p = 1;
3afe33e7
RS
8681 }
8682 }
8683 else
8684 {
8685 struct scroll_bar *bar
8686 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8687
3afe33e7
RS
8688 if (bar)
8689 x_scroll_bar_expose (bar, &event);
8690 }
f94397b5
KH
8691
8692 UNBLOCK_INPUT;
06a2c219 8693 return frame_exposed_p;
3afe33e7 8694}
09756a85
RS
8695\f
8696/* Define a queue to save up SelectionRequest events for later handling. */
8697
8698struct selection_event_queue
8699 {
8700 XEvent event;
8701 struct selection_event_queue *next;
8702 };
8703
8704static struct selection_event_queue *queue;
8705
8706/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8707
09756a85
RS
8708static int x_queue_selection_requests;
8709
8710/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8711
09756a85 8712static void
334208b7
RS
8713x_queue_event (f, event)
8714 FRAME_PTR f;
09756a85
RS
8715 XEvent *event;
8716{
8717 struct selection_event_queue *queue_tmp
06a2c219 8718 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8719
58769bee 8720 if (queue_tmp != NULL)
09756a85
RS
8721 {
8722 queue_tmp->event = *event;
8723 queue_tmp->next = queue;
8724 queue = queue_tmp;
8725 }
8726}
8727
8728/* Take all the queued events and put them back
8729 so that they get processed afresh. */
8730
8731static void
db3906fd
RS
8732x_unqueue_events (display)
8733 Display *display;
09756a85 8734{
58769bee 8735 while (queue != NULL)
09756a85
RS
8736 {
8737 struct selection_event_queue *queue_tmp = queue;
db3906fd 8738 XPutBackEvent (display, &queue_tmp->event);
09756a85 8739 queue = queue_tmp->next;
06a2c219 8740 xfree ((char *)queue_tmp);
09756a85
RS
8741 }
8742}
8743
8744/* Start queuing SelectionRequest events. */
8745
8746void
db3906fd
RS
8747x_start_queuing_selection_requests (display)
8748 Display *display;
09756a85
RS
8749{
8750 x_queue_selection_requests++;
8751}
8752
8753/* Stop queuing SelectionRequest events. */
8754
8755void
db3906fd
RS
8756x_stop_queuing_selection_requests (display)
8757 Display *display;
09756a85
RS
8758{
8759 x_queue_selection_requests--;
db3906fd 8760 x_unqueue_events (display);
09756a85 8761}
f451eb13
JB
8762\f
8763/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8764
06a2c219 8765/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8766 but we have to put it out here, since static variables within functions
8767 sometimes don't work. */
06a2c219 8768
dc6f92b8
JB
8769static Time enter_timestamp;
8770
11edeb03 8771/* This holds the state XLookupString needs to implement dead keys
58769bee 8772 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8773 says that a portable program can't use this, but Stephen Gildea assures
8774 me that letting the compiler initialize it to zeros will work okay.
8775
8776 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8777 given for enter_time stamp, above. */
8778
11edeb03
JB
8779static XComposeStatus compose_status;
8780
10e6549c
RS
8781/* Record the last 100 characters stored
8782 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8783
2224b905
RS
8784static int temp_index;
8785static short temp_buffer[100];
10e6549c 8786
7a13e894
RS
8787/* Set this to nonzero to fake an "X I/O error"
8788 on a particular display. */
06a2c219 8789
7a13e894
RS
8790struct x_display_info *XTread_socket_fake_io_error;
8791
2224b905
RS
8792/* When we find no input here, we occasionally do a no-op command
8793 to verify that the X server is still running and we can still talk with it.
8794 We try all the open displays, one by one.
8795 This variable is used for cycling thru the displays. */
06a2c219 8796
2224b905
RS
8797static struct x_display_info *next_noop_dpyinfo;
8798
06a2c219
GM
8799#define SET_SAVED_MENU_EVENT(size) \
8800 do \
8801 { \
8802 if (f->output_data.x->saved_menu_event == 0) \
8803 f->output_data.x->saved_menu_event \
8804 = (XEvent *) xmalloc (sizeof (XEvent)); \
8805 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8806 if (numchars >= 1) \
8807 { \
8808 bufp->kind = menu_bar_activate_event; \
8809 XSETFRAME (bufp->frame_or_window, f); \
8810 bufp++; \
8811 count++; \
8812 numchars--; \
8813 } \
8814 } \
8815 while (0)
8816
8805890a 8817#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8818#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8819
dc6f92b8
JB
8820/* Read events coming from the X server.
8821 This routine is called by the SIGIO handler.
8822 We return as soon as there are no more events to be read.
8823
8824 Events representing keys are stored in buffer BUFP,
8825 which can hold up to NUMCHARS characters.
8826 We return the number of characters stored into the buffer,
8827 thus pretending to be `read'.
8828
dc6f92b8
JB
8829 EXPECTED is nonzero if the caller knows input is available. */
8830
7c5283e4 8831int
f66868ba 8832XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8833 register int sd;
8805890a
KH
8834 /* register */ struct input_event *bufp;
8835 /* register */ int numchars;
dc6f92b8
JB
8836 int expected;
8837{
8838 int count = 0;
8839 int nbytes = 0;
dc6f92b8 8840 XEvent event;
f676886a 8841 struct frame *f;
66f55a9d 8842 int event_found = 0;
334208b7 8843 struct x_display_info *dpyinfo;
dc6f92b8 8844
9ac0d9e0 8845 if (interrupt_input_blocked)
dc6f92b8 8846 {
9ac0d9e0 8847 interrupt_input_pending = 1;
dc6f92b8
JB
8848 return -1;
8849 }
8850
9ac0d9e0 8851 interrupt_input_pending = 0;
dc6f92b8 8852 BLOCK_INPUT;
c0a04927
RS
8853
8854 /* So people can tell when we have read the available input. */
8855 input_signal_count++;
8856
dc6f92b8 8857 if (numchars <= 0)
06a2c219 8858 abort (); /* Don't think this happens. */
dc6f92b8 8859
7a13e894
RS
8860 /* Find the display we are supposed to read input for.
8861 It's the one communicating on descriptor SD. */
8862 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8863 {
8864#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8865#ifdef FIOSNBIO
7a13e894
RS
8866 /* If available, Xlib uses FIOSNBIO to make the socket
8867 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8868 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8869 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8870 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8871#endif /* ! defined (FIOSNBIO) */
7a13e894 8872#endif
dc6f92b8 8873
7a13e894
RS
8874#if 0 /* This code can't be made to work, with multiple displays,
8875 and appears not to be used on any system any more.
8876 Also keyboard.c doesn't turn O_NDELAY on and off
8877 for X connections. */
dc6f92b8
JB
8878#ifndef SIGIO
8879#ifndef HAVE_SELECT
7a13e894
RS
8880 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8881 {
8882 extern int read_alarm_should_throw;
8883 read_alarm_should_throw = 1;
8884 XPeekEvent (dpyinfo->display, &event);
8885 read_alarm_should_throw = 0;
8886 }
c118dd06
JB
8887#endif /* HAVE_SELECT */
8888#endif /* SIGIO */
7a13e894 8889#endif
dc6f92b8 8890
7a13e894
RS
8891 /* For debugging, this gives a way to fake an I/O error. */
8892 if (dpyinfo == XTread_socket_fake_io_error)
8893 {
8894 XTread_socket_fake_io_error = 0;
8895 x_io_error_quitter (dpyinfo->display);
8896 }
dc6f92b8 8897
06a2c219 8898 while (XPending (dpyinfo->display))
dc6f92b8 8899 {
7a13e894 8900 XNextEvent (dpyinfo->display, &event);
06a2c219 8901
531483fb 8902#ifdef HAVE_X_I18N
d1bc4182 8903 {
f2be1146
GM
8904 /* Filter events for the current X input method.
8905 XFilterEvent returns non-zero if the input method has
8906 consumed the event. We pass the frame's X window to
8907 XFilterEvent because that's the one for which the IC
8908 was created. */
f5d11644
GM
8909 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8910 event.xclient.window);
8911 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
8912 break;
8913 }
0cd6403b 8914#endif
7a13e894
RS
8915 event_found = 1;
8916
8917 switch (event.type)
8918 {
8919 case ClientMessage:
c047688c 8920 {
7a13e894
RS
8921 if (event.xclient.message_type
8922 == dpyinfo->Xatom_wm_protocols
8923 && event.xclient.format == 32)
c047688c 8924 {
7a13e894
RS
8925 if (event.xclient.data.l[0]
8926 == dpyinfo->Xatom_wm_take_focus)
c047688c 8927 {
8c1a6a84
RS
8928 /* Use x_any_window_to_frame because this
8929 could be the shell widget window
8930 if the frame has no title bar. */
8931 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8932#ifdef HAVE_X_I18N
8933 /* Not quite sure this is needed -pd */
8c1a6a84 8934 if (f && FRAME_XIC (f))
6c183ba5
RS
8935 XSetICFocus (FRAME_XIC (f));
8936#endif
f1da8f06
GM
8937#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8938 instructs the WM to set the input focus automatically for
8939 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8940 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8941 it has set the focus. So, XSetInputFocus below is not
8942 needed.
8943
8944 The call to XSetInputFocus below has also caused trouble. In
8945 cases where the XSetInputFocus done by the WM and the one
8946 below are temporally close (on a fast machine), the call
8947 below can generate additional FocusIn events which confuse
8948 Emacs. */
8949
bf7253f4
RS
8950 /* Since we set WM_TAKE_FOCUS, we must call
8951 XSetInputFocus explicitly. But not if f is null,
8952 since that might be an event for a deleted frame. */
7a13e894 8953 if (f)
bf7253f4
RS
8954 {
8955 Display *d = event.xclient.display;
8956 /* Catch and ignore errors, in case window has been
8957 iconified by a window manager such as GWM. */
8958 int count = x_catch_errors (d);
8959 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8960 /* The ICCCM says this is
8961 the only valid choice. */
8962 RevertToParent,
bf7253f4
RS
8963 event.xclient.data.l[1]);
8964 /* This is needed to detect the error
8965 if there is an error. */
8966 XSync (d, False);
8967 x_uncatch_errors (d, count);
8968 }
7a13e894 8969 /* Not certain about handling scroll bars here */
f1da8f06 8970#endif /* 0 */
c047688c 8971 }
7a13e894
RS
8972 else if (event.xclient.data.l[0]
8973 == dpyinfo->Xatom_wm_save_yourself)
8974 {
8975 /* Save state modify the WM_COMMAND property to
06a2c219 8976 something which can reinstate us. This notifies
7a13e894
RS
8977 the session manager, who's looking for such a
8978 PropertyNotify. Can restart processing when
06a2c219 8979 a keyboard or mouse event arrives. */
7a13e894
RS
8980 if (numchars > 0)
8981 {
19126e11
KH
8982 f = x_top_window_to_frame (dpyinfo,
8983 event.xclient.window);
7a13e894
RS
8984
8985 /* This is just so we only give real data once
8986 for a single Emacs process. */
b86bd3dd 8987 if (f == SELECTED_FRAME ())
7a13e894
RS
8988 XSetCommand (FRAME_X_DISPLAY (f),
8989 event.xclient.window,
8990 initial_argv, initial_argc);
f000f5c5 8991 else if (f)
7a13e894
RS
8992 XSetCommand (FRAME_X_DISPLAY (f),
8993 event.xclient.window,
8994 0, 0);
8995 }
8996 }
8997 else if (event.xclient.data.l[0]
8998 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8999 {
19126e11
KH
9000 struct frame *f
9001 = x_any_window_to_frame (dpyinfo,
9002 event.xclient.window);
1fb20991 9003
7a13e894
RS
9004 if (f)
9005 {
9006 if (numchars == 0)
9007 abort ();
1fb20991 9008
7a13e894
RS
9009 bufp->kind = delete_window_event;
9010 XSETFRAME (bufp->frame_or_window, f);
9011 bufp++;
9012
9013 count += 1;
9014 numchars -= 1;
9015 }
1fb20991 9016 }
c047688c 9017 }
7a13e894
RS
9018 else if (event.xclient.message_type
9019 == dpyinfo->Xatom_wm_configure_denied)
9020 {
9021 }
9022 else if (event.xclient.message_type
9023 == dpyinfo->Xatom_wm_window_moved)
9024 {
9025 int new_x, new_y;
19126e11
KH
9026 struct frame *f
9027 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9028
7a13e894
RS
9029 new_x = event.xclient.data.s[0];
9030 new_y = event.xclient.data.s[1];
1fb20991 9031
7a13e894
RS
9032 if (f)
9033 {
7556890b
RS
9034 f->output_data.x->left_pos = new_x;
9035 f->output_data.x->top_pos = new_y;
7a13e894 9036 }
1fb20991 9037 }
0fdff6bb 9038#ifdef HACK_EDITRES
7a13e894
RS
9039 else if (event.xclient.message_type
9040 == dpyinfo->Xatom_editres)
9041 {
19126e11
KH
9042 struct frame *f
9043 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9044 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9045 &event, NULL);
7a13e894 9046 }
0fdff6bb 9047#endif /* HACK_EDITRES */
06a2c219
GM
9048 else if ((event.xclient.message_type
9049 == dpyinfo->Xatom_DONE)
9050 || (event.xclient.message_type
9051 == dpyinfo->Xatom_PAGE))
9052 {
9053 /* Ghostview job completed. Kill it. We could
9054 reply with "Next" if we received "Page", but we
9055 currently never do because we are interested in
9056 images, only, which should have 1 page. */
06a2c219
GM
9057 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9058 struct frame *f
9059 = x_window_to_frame (dpyinfo, event.xclient.window);
9060 x_kill_gs_process (pixmap, f);
9061 expose_frame (f, 0, 0, 0, 0);
9062 }
9063#ifdef USE_TOOLKIT_SCROLL_BARS
9064 /* Scroll bar callbacks send a ClientMessage from which
9065 we construct an input_event. */
9066 else if (event.xclient.message_type
9067 == dpyinfo->Xatom_Scrollbar)
9068 {
9069 x_scroll_bar_to_input_event (&event, bufp);
9070 ++bufp, ++count, --numchars;
9071 goto out;
9072 }
9073#endif /* USE_TOOLKIT_SCROLL_BARS */
9074 else
9075 goto OTHER;
7a13e894
RS
9076 }
9077 break;
dc6f92b8 9078
7a13e894 9079 case SelectionNotify:
3afe33e7 9080#ifdef USE_X_TOOLKIT
19126e11 9081 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9082 goto OTHER;
3afe33e7 9083#endif /* not USE_X_TOOLKIT */
dfcf069d 9084 x_handle_selection_notify (&event.xselection);
7a13e894 9085 break;
d56a553a 9086
06a2c219 9087 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9088#ifdef USE_X_TOOLKIT
19126e11 9089 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9090 goto OTHER;
3afe33e7 9091#endif /* USE_X_TOOLKIT */
7a13e894
RS
9092 {
9093 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9094
7a13e894
RS
9095 if (numchars == 0)
9096 abort ();
d56a553a 9097
7a13e894
RS
9098 bufp->kind = selection_clear_event;
9099 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9100 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9101 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9102 bufp->frame_or_window = Qnil;
7a13e894 9103 bufp++;
d56a553a 9104
7a13e894
RS
9105 count += 1;
9106 numchars -= 1;
9107 }
9108 break;
dc6f92b8 9109
06a2c219 9110 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9111#ifdef USE_X_TOOLKIT
19126e11 9112 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9113 goto OTHER;
3afe33e7 9114#endif /* USE_X_TOOLKIT */
7a13e894 9115 if (x_queue_selection_requests)
19126e11 9116 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9117 &event);
9118 else
9119 {
9120 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9121
7a13e894
RS
9122 if (numchars == 0)
9123 abort ();
9124
9125 bufp->kind = selection_request_event;
9126 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9127 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9128 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9129 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9130 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9131 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9132 bufp->frame_or_window = Qnil;
7a13e894
RS
9133 bufp++;
9134
9135 count += 1;
9136 numchars -= 1;
9137 }
9138 break;
9139
9140 case PropertyNotify:
3afe33e7 9141#ifdef USE_X_TOOLKIT
19126e11 9142 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9143 goto OTHER;
3afe33e7 9144#endif /* not USE_X_TOOLKIT */
dfcf069d 9145 x_handle_property_notify (&event.xproperty);
7a13e894 9146 break;
dc6f92b8 9147
7a13e894 9148 case ReparentNotify:
19126e11 9149 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9150 if (f)
9151 {
9152 int x, y;
7556890b 9153 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9154 x_real_positions (f, &x, &y);
7556890b
RS
9155 f->output_data.x->left_pos = x;
9156 f->output_data.x->top_pos = y;
7a13e894
RS
9157 }
9158 break;
3bd330d4 9159
7a13e894 9160 case Expose:
19126e11 9161 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9162 if (f)
dc6f92b8 9163 {
7a13e894
RS
9164 if (f->async_visible == 0)
9165 {
9166 f->async_visible = 1;
9167 f->async_iconified = 0;
06c488fd 9168 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9169 SET_FRAME_GARBAGED (f);
9170 }
9171 else
06a2c219
GM
9172 expose_frame (x_window_to_frame (dpyinfo,
9173 event.xexpose.window),
9174 event.xexpose.x, event.xexpose.y,
9175 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9176 }
9177 else
7a13e894 9178 {
06a2c219
GM
9179#ifdef USE_TOOLKIT_SCROLL_BARS
9180 /* Dispatch event to the widget. */
9181 goto OTHER;
9182#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9183 struct scroll_bar *bar
9184 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9185
7a13e894
RS
9186 if (bar)
9187 x_scroll_bar_expose (bar, &event);
3afe33e7 9188#ifdef USE_X_TOOLKIT
7a13e894
RS
9189 else
9190 goto OTHER;
3afe33e7 9191#endif /* USE_X_TOOLKIT */
06a2c219 9192#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9193 }
9194 break;
dc6f92b8 9195
7a13e894
RS
9196 case GraphicsExpose: /* This occurs when an XCopyArea's
9197 source area was obscured or not
9198 available.*/
19126e11 9199 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9200 if (f)
9201 {
06a2c219
GM
9202 expose_frame (f,
9203 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9204 event.xgraphicsexpose.width,
9205 event.xgraphicsexpose.height);
7a13e894 9206 }
3afe33e7 9207#ifdef USE_X_TOOLKIT
7a13e894
RS
9208 else
9209 goto OTHER;
3afe33e7 9210#endif /* USE_X_TOOLKIT */
7a13e894 9211 break;
dc6f92b8 9212
7a13e894 9213 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9214 source area was completely
9215 available */
7a13e894 9216 break;
dc6f92b8 9217
7a13e894 9218 case UnmapNotify:
06a2c219
GM
9219 /* Redo the mouse-highlight after the tooltip has gone. */
9220 if (event.xmap.window == tip_window)
9221 {
9222 tip_window = 0;
9223 redo_mouse_highlight ();
9224 }
9225
91ea2a7a 9226 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9227 if (f) /* F may no longer exist if
9228 the frame was deleted. */
9229 {
9230 /* While a frame is unmapped, display generation is
9231 disabled; you don't want to spend time updating a
9232 display that won't ever be seen. */
9233 f->async_visible = 0;
9234 /* We can't distinguish, from the event, whether the window
9235 has become iconified or invisible. So assume, if it
9236 was previously visible, than now it is iconified.
1aa6072f
RS
9237 But x_make_frame_invisible clears both
9238 the visible flag and the iconified flag;
9239 and that way, we know the window is not iconified now. */
7a13e894 9240 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9241 {
9242 f->async_iconified = 1;
bddd097c 9243
1aa6072f
RS
9244 bufp->kind = iconify_event;
9245 XSETFRAME (bufp->frame_or_window, f);
9246 bufp++;
9247 count++;
9248 numchars--;
9249 }
7a13e894 9250 }
7a13e894 9251 goto OTHER;
dc6f92b8 9252
7a13e894 9253 case MapNotify:
06a2c219
GM
9254 if (event.xmap.window == tip_window)
9255 /* The tooltip has been drawn already. Avoid
9256 the SET_FRAME_GARBAGED below. */
9257 goto OTHER;
9258
9259 /* We use x_top_window_to_frame because map events can
9260 come for sub-windows and they don't mean that the
9261 frame is visible. */
19126e11 9262 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9263 if (f)
9264 {
9265 f->async_visible = 1;
9266 f->async_iconified = 0;
06c488fd 9267 f->output_data.x->has_been_visible = 1;
dc6f92b8 9268
7a13e894
RS
9269 /* wait_reading_process_input will notice this and update
9270 the frame's display structures. */
9271 SET_FRAME_GARBAGED (f);
bddd097c 9272
d806e720
RS
9273 if (f->iconified)
9274 {
9275 bufp->kind = deiconify_event;
9276 XSETFRAME (bufp->frame_or_window, f);
9277 bufp++;
9278 count++;
9279 numchars--;
9280 }
e73ec6fa 9281 else if (! NILP (Vframe_list)
8e713be6 9282 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9283 /* Force a redisplay sooner or later
9284 to update the frame titles
9285 in case this is the second frame. */
9286 record_asynch_buffer_change ();
7a13e894 9287 }
7a13e894 9288 goto OTHER;
dc6f92b8 9289
7a13e894 9290 case KeyPress:
19126e11 9291 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9292
06a2c219
GM
9293#ifdef USE_MOTIF
9294 /* I couldn't find a way to prevent LessTif scroll bars
9295 from consuming key events. */
9296 if (f == 0)
9297 {
9298 Widget widget = XtWindowToWidget (dpyinfo->display,
9299 event.xkey.window);
9300 if (widget && XmIsScrollBar (widget))
9301 {
9302 widget = XtParent (widget);
9303 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9304 }
9305 }
9306#endif /* USE_MOTIF */
9307
7a13e894
RS
9308 if (f != 0)
9309 {
9310 KeySym keysym, orig_keysym;
9311 /* al%imercury@uunet.uu.net says that making this 81 instead of
9312 80 fixed a bug whereby meta chars made his Emacs hang. */
9313 unsigned char copy_buffer[81];
9314 int modifiers;
64bb1782 9315
7a13e894
RS
9316 event.xkey.state
9317 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9318 extra_keyboard_modifiers);
9319 modifiers = event.xkey.state;
3a2712f9 9320
7a13e894 9321 /* This will have to go some day... */
752a043f 9322
7a13e894
RS
9323 /* make_lispy_event turns chars into control chars.
9324 Don't do it here because XLookupString is too eager. */
9325 event.xkey.state &= ~ControlMask;
5d46f928
RS
9326 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9327 | dpyinfo->super_mod_mask
9328 | dpyinfo->hyper_mod_mask
9329 | dpyinfo->alt_mod_mask);
9330
1cf4a0d1
RS
9331 /* In case Meta is ComposeCharacter,
9332 clear its status. According to Markus Ehrnsperger
9333 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9334 this enables ComposeCharacter to work whether or
9335 not it is combined with Meta. */
9336 if (modifiers & dpyinfo->meta_mod_mask)
9337 bzero (&compose_status, sizeof (compose_status));
9338
6c183ba5
RS
9339#ifdef HAVE_X_I18N
9340 if (FRAME_XIC (f))
9341 {
f5d11644
GM
9342 unsigned char *copy_bufptr = copy_buffer;
9343 int copy_bufsiz = sizeof (copy_buffer);
9344 Status status_return;
9345
6c183ba5 9346 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9347 &event.xkey, copy_bufptr,
9348 copy_bufsiz, &keysym,
6c183ba5 9349 &status_return);
f5d11644
GM
9350 if (status_return == XBufferOverflow)
9351 {
9352 copy_bufsiz = nbytes + 1;
9353 copy_bufptr = (char *) alloca (copy_bufsiz);
9354 nbytes = XmbLookupString (FRAME_XIC (f),
9355 &event.xkey, copy_bufptr,
9356 copy_bufsiz, &keysym,
9357 &status_return);
9358 }
9359
1decb680
PE
9360 if (status_return == XLookupNone)
9361 break;
9362 else if (status_return == XLookupChars)
9363 keysym = NoSymbol;
9364 else if (status_return != XLookupKeySym
9365 && status_return != XLookupBoth)
9366 abort ();
6c183ba5
RS
9367 }
9368 else
9369 nbytes = XLookupString (&event.xkey, copy_buffer,
9370 80, &keysym, &compose_status);
9371#else
0299d313
RS
9372 nbytes = XLookupString (&event.xkey, copy_buffer,
9373 80, &keysym, &compose_status);
6c183ba5 9374#endif
dc6f92b8 9375
7a13e894 9376 orig_keysym = keysym;
55123275 9377
7a13e894
RS
9378 if (numchars > 1)
9379 {
9380 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9381 || keysym == XK_Delete
1097aea0 9382#ifdef XK_ISO_Left_Tab
441affdb 9383 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9384#endif
852bff8f 9385 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9386 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9387 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9388#ifdef HPUX
7a13e894
RS
9389 /* This recognizes the "extended function keys".
9390 It seems there's no cleaner way.
9391 Test IsModifierKey to avoid handling mode_switch
9392 incorrectly. */
9393 || ((unsigned) (keysym) >= XK_Select
9394 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9395#endif
9396#ifdef XK_dead_circumflex
7a13e894 9397 || orig_keysym == XK_dead_circumflex
69388238
RS
9398#endif
9399#ifdef XK_dead_grave
7a13e894 9400 || orig_keysym == XK_dead_grave
69388238
RS
9401#endif
9402#ifdef XK_dead_tilde
7a13e894 9403 || orig_keysym == XK_dead_tilde
69388238
RS
9404#endif
9405#ifdef XK_dead_diaeresis
7a13e894 9406 || orig_keysym == XK_dead_diaeresis
69388238
RS
9407#endif
9408#ifdef XK_dead_macron
7a13e894 9409 || orig_keysym == XK_dead_macron
69388238
RS
9410#endif
9411#ifdef XK_dead_degree
7a13e894 9412 || orig_keysym == XK_dead_degree
69388238
RS
9413#endif
9414#ifdef XK_dead_acute
7a13e894 9415 || orig_keysym == XK_dead_acute
69388238
RS
9416#endif
9417#ifdef XK_dead_cedilla
7a13e894 9418 || orig_keysym == XK_dead_cedilla
69388238
RS
9419#endif
9420#ifdef XK_dead_breve
7a13e894 9421 || orig_keysym == XK_dead_breve
69388238
RS
9422#endif
9423#ifdef XK_dead_ogonek
7a13e894 9424 || orig_keysym == XK_dead_ogonek
69388238
RS
9425#endif
9426#ifdef XK_dead_caron
7a13e894 9427 || orig_keysym == XK_dead_caron
69388238
RS
9428#endif
9429#ifdef XK_dead_doubleacute
7a13e894 9430 || orig_keysym == XK_dead_doubleacute
69388238
RS
9431#endif
9432#ifdef XK_dead_abovedot
7a13e894 9433 || orig_keysym == XK_dead_abovedot
c34790e0 9434#endif
7a13e894
RS
9435 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9436 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9437 /* Any "vendor-specific" key is ok. */
9438 || (orig_keysym & (1 << 28)))
9439 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9440#ifndef HAVE_X11R5
9441#ifdef XK_Mode_switch
7a13e894 9442 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9443#endif
9444#ifdef XK_Num_Lock
7a13e894 9445 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9446#endif
9447#endif /* not HAVE_X11R5 */
7a13e894 9448 ))
dc6f92b8 9449 {
10e6549c
RS
9450 if (temp_index == sizeof temp_buffer / sizeof (short))
9451 temp_index = 0;
7a13e894
RS
9452 temp_buffer[temp_index++] = keysym;
9453 bufp->kind = non_ascii_keystroke;
9454 bufp->code = keysym;
e0c1aef2 9455 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9456 bufp->modifiers
9457 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9458 modifiers);
1113d9db 9459 bufp->timestamp = event.xkey.time;
dc6f92b8 9460 bufp++;
7a13e894
RS
9461 count++;
9462 numchars--;
dc6f92b8 9463 }
7a13e894
RS
9464 else if (numchars > nbytes)
9465 {
9466 register int i;
9467
9468 for (i = 0; i < nbytes; i++)
9469 {
9470 if (temp_index == sizeof temp_buffer / sizeof (short))
9471 temp_index = 0;
9472 temp_buffer[temp_index++] = copy_buffer[i];
9473 bufp->kind = ascii_keystroke;
9474 bufp->code = copy_buffer[i];
9475 XSETFRAME (bufp->frame_or_window, f);
9476 bufp->modifiers
9477 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9478 modifiers);
9479 bufp->timestamp = event.xkey.time;
9480 bufp++;
9481 }
9482
9483 count += nbytes;
9484 numchars -= nbytes;
1decb680
PE
9485
9486 if (keysym == NoSymbol)
9487 break;
7a13e894
RS
9488 }
9489 else
9490 abort ();
dc6f92b8 9491 }
10e6549c
RS
9492 else
9493 abort ();
dc6f92b8 9494 }
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
717ca130 9501 goto OTHER;
59ddecde 9502#endif
f451eb13 9503
f5d11644 9504 case KeyRelease:
59ddecde
GM
9505#ifdef HAVE_X_I18N
9506 /* Don't dispatch this event since XtDispatchEvent calls
9507 XFilterEvent, and two calls in a row may freeze the
9508 client. */
9509 break;
9510#else
f5d11644 9511 goto OTHER;
59ddecde 9512#endif
f5d11644 9513
7a13e894 9514 /* Here's a possible interpretation of the whole
06a2c219
GM
9515 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9516 you get a FocusIn event, you have to get a FocusOut
9517 event before you relinquish the focus. If you
9518 haven't received a FocusIn event, then a mere
9519 LeaveNotify is enough to free you. */
f451eb13 9520
7a13e894 9521 case EnterNotify:
06a2c219
GM
9522 {
9523 int from_menu_bar_p = 0;
9524
9525 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9526
9527#ifdef LESSTIF_VERSION
9528 /* When clicking outside of a menu bar popup to close
9529 it, we get a FocusIn/ EnterNotify sequence of
9530 events. The flag event.xcrossing.focus is not set
9531 in the EnterNotify event of that sequence because
9532 the focus is in the menu bar,
9533 event.xcrossing.window is the frame's X window.
9534 Unconditionally setting the focus frame to null in
9535 this case is not the right thing, because no event
9536 follows that could set the focus frame to the right
9537 value.
9538
9539 This could be a LessTif bug, but I wasn't able to
9540 reproduce the behavior in a simple test program.
9541
9542 (gerd, LessTif 0.88.1). */
9543
9544 if (!event.xcrossing.focus
9545 && f
9546 && f->output_data.x->menubar_widget)
9547 {
9548 Window focus;
9549 int revert;
9550
9551 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9552 if (focus == XtWindow (f->output_data.x->menubar_widget))
9553 from_menu_bar_p = 1;
9554 }
9555#endif /* LESSTIF_VERSION */
6d4238f3 9556
06a2c219
GM
9557 if (event.xcrossing.focus || from_menu_bar_p)
9558 {
9559 /* Avoid nasty pop/raise loops. */
9560 if (f && (!(f->auto_raise)
9561 || !(f->auto_lower)
9562 || (event.xcrossing.time - enter_timestamp) > 500))
9563 {
9564 x_new_focus_frame (dpyinfo, f);
9565 enter_timestamp = event.xcrossing.time;
9566 }
9567 }
9568 else if (f == dpyinfo->x_focus_frame)
9569 x_new_focus_frame (dpyinfo, 0);
9570
9571 /* EnterNotify counts as mouse movement,
9572 so update things that depend on mouse position. */
9573 if (f && !f->output_data.x->busy_p)
9574 note_mouse_movement (f, &event.xmotion);
9575 goto OTHER;
9576 }
dc6f92b8 9577
7a13e894 9578 case FocusIn:
19126e11 9579 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9580 if (event.xfocus.detail != NotifyPointer)
0f941935 9581 dpyinfo->x_focus_event_frame = f;
7a13e894 9582 if (f)
0f941935 9583 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9584
6c183ba5
RS
9585#ifdef HAVE_X_I18N
9586 if (f && FRAME_XIC (f))
9587 XSetICFocus (FRAME_XIC (f));
9588#endif
9589
7a13e894 9590 goto OTHER;
10c5e63d 9591
7a13e894 9592 case LeaveNotify:
19126e11 9593 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9594 if (f)
10c5e63d 9595 {
06a2c219
GM
9596 Lisp_Object frame;
9597 int from_menu_bar_p = 0;
9598
7a13e894 9599 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9600 {
9601 /* If we move outside the frame, then we're
9602 certainly no longer on any text in the frame. */
9603 clear_mouse_face (dpyinfo);
9604 dpyinfo->mouse_face_mouse_frame = 0;
9605 }
9606
9607 /* Generate a nil HELP_EVENT to cancel a help-echo.
9608 Do it only if there's something to cancel.
9609 Otherwise, the startup message is cleared when
9610 the mouse leaves the frame. */
9611 if (any_help_event_p)
9612 {
9613 XSETFRAME (frame, f);
9614 bufp->kind = HELP_EVENT;
9615 bufp->frame_or_window = Fcons (frame, Qnil);
9616 ++bufp, ++count, --numchars;
9617 }
7a13e894 9618
06a2c219
GM
9619#ifdef LESSTIF_VERSION
9620 /* Please see the comment at the start of the
9621 EnterNotify case. */
9622 if (!event.xcrossing.focus
9623 && f->output_data.x->menubar_widget)
9624 {
9625 Window focus;
9626 int revert;
9627 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9628 if (focus == XtWindow (f->output_data.x->menubar_widget))
9629 from_menu_bar_p = 1;
9630 }
9631#endif /* LESSTIF_VERSION */
9632
9633 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9634 x_mouse_leave (dpyinfo);
10c5e63d 9635 else
7a13e894 9636 {
0f941935
KH
9637 if (f == dpyinfo->x_focus_event_frame)
9638 dpyinfo->x_focus_event_frame = 0;
9639 if (f == dpyinfo->x_focus_frame)
9640 x_new_focus_frame (dpyinfo, 0);
7a13e894 9641 }
10c5e63d 9642 }
7a13e894 9643 goto OTHER;
dc6f92b8 9644
7a13e894 9645 case FocusOut:
19126e11 9646 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9647 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9648 && f == dpyinfo->x_focus_event_frame)
9649 dpyinfo->x_focus_event_frame = 0;
9650 if (f && f == dpyinfo->x_focus_frame)
9651 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9652
6c183ba5
RS
9653#ifdef HAVE_X_I18N
9654 if (f && FRAME_XIC (f))
9655 XUnsetICFocus (FRAME_XIC (f));
9656#endif
9657
7a13e894 9658 goto OTHER;
dc6f92b8 9659
7a13e894 9660 case MotionNotify:
dc6f92b8 9661 {
06a2c219
GM
9662 previous_help_echo = help_echo;
9663 help_echo = Qnil;
9664
7a13e894
RS
9665 if (dpyinfo->grabbed && last_mouse_frame
9666 && FRAME_LIVE_P (last_mouse_frame))
9667 f = last_mouse_frame;
9668 else
19126e11 9669 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9670
7a13e894
RS
9671 if (f)
9672 note_mouse_movement (f, &event.xmotion);
9673 else
9674 {
e88b3c50 9675#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9676 struct scroll_bar *bar
9677 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9678
7a13e894
RS
9679 if (bar)
9680 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9681#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9682
06a2c219
GM
9683 /* If we move outside the frame, then we're
9684 certainly no longer on any text in the frame. */
7a13e894
RS
9685 clear_mouse_face (dpyinfo);
9686 }
06a2c219
GM
9687
9688 /* If the contents of the global variable help_echo
9689 has changed, generate a HELP_EVENT. */
9690 if (STRINGP (help_echo)
9691 || STRINGP (previous_help_echo))
9692 {
9693 Lisp_Object frame;
9694
9695 if (f)
9696 XSETFRAME (frame, f);
9697 else
9698 frame = Qnil;
9699
9700 any_help_event_p = 1;
9701 bufp->kind = HELP_EVENT;
9702 bufp->frame_or_window = Fcons (frame, help_echo);
9703 ++bufp, ++count, --numchars;
9704 }
9705
9706 goto OTHER;
dc6f92b8 9707 }
dc6f92b8 9708
7a13e894 9709 case ConfigureNotify:
9829ddba
RS
9710 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9711 if (f)
af395ec1 9712 {
5c187dee 9713#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9714 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9715 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9716
2d7fc7e8
RS
9717 /* In the toolkit version, change_frame_size
9718 is called by the code that handles resizing
9719 of the EmacsFrame widget. */
7a13e894 9720
7a13e894
RS
9721 /* Even if the number of character rows and columns has
9722 not changed, the font size may have changed, so we need
9723 to check the pixel dimensions as well. */
9724 if (columns != f->width
9725 || rows != f->height
7556890b
RS
9726 || event.xconfigure.width != f->output_data.x->pixel_width
9727 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9728 {
7d1e984f 9729 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9730 SET_FRAME_GARBAGED (f);
e687d06e 9731 cancel_mouse_face (f);
7a13e894 9732 }
2d7fc7e8 9733#endif
af395ec1 9734
7556890b
RS
9735 f->output_data.x->pixel_width = event.xconfigure.width;
9736 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9737
9738 /* What we have now is the position of Emacs's own window.
9739 Convert that to the position of the window manager window. */
dcb07ae9
RS
9740 x_real_positions (f, &f->output_data.x->left_pos,
9741 &f->output_data.x->top_pos);
9742
f5d11644
GM
9743#ifdef HAVE_X_I18N
9744 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9745 xic_set_statusarea (f);
9746#endif
9747
dcb07ae9
RS
9748 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9749 {
9750 /* Since the WM decorations come below top_pos now,
9751 we must put them below top_pos in the future. */
9752 f->output_data.x->win_gravity = NorthWestGravity;
9753 x_wm_set_size_hint (f, (long) 0, 0);
9754 }
8f08dc93
KH
9755#ifdef USE_MOTIF
9756 /* Some window managers pass (0,0) as the location of
9757 the window, and the Motif event handler stores it
9758 in the emacs widget, which messes up Motif menus. */
9759 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9760 {
9761 event.xconfigure.x = f->output_data.x->widget->core.x;
9762 event.xconfigure.y = f->output_data.x->widget->core.y;
9763 }
06a2c219 9764#endif /* USE_MOTIF */
7a13e894 9765 }
2d7fc7e8 9766 goto OTHER;
dc6f92b8 9767
7a13e894
RS
9768 case ButtonPress:
9769 case ButtonRelease:
9770 {
9771 /* If we decide we want to generate an event to be seen
9772 by the rest of Emacs, we put it here. */
9773 struct input_event emacs_event;
9ea173e8 9774 int tool_bar_p = 0;
06a2c219 9775
7a13e894 9776 emacs_event.kind = no_event;
7a13e894 9777 bzero (&compose_status, sizeof (compose_status));
9b07615b 9778
06a2c219
GM
9779 if (dpyinfo->grabbed
9780 && last_mouse_frame
9f67f20b
RS
9781 && FRAME_LIVE_P (last_mouse_frame))
9782 f = last_mouse_frame;
9783 else
2224b905 9784 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9785
06a2c219
GM
9786 if (f)
9787 {
9ea173e8
GM
9788 /* Is this in the tool-bar? */
9789 if (WINDOWP (f->tool_bar_window)
9790 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9791 {
9792 Lisp_Object window;
9793 int p, x, y;
9794
9795 x = event.xbutton.x;
9796 y = event.xbutton.y;
9797
9798 /* Set x and y. */
9799 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9800 if (EQ (window, f->tool_bar_window))
06a2c219 9801 {
9ea173e8
GM
9802 x_handle_tool_bar_click (f, &event.xbutton);
9803 tool_bar_p = 1;
06a2c219
GM
9804 }
9805 }
9806
9ea173e8 9807 if (!tool_bar_p)
06a2c219
GM
9808 if (!dpyinfo->x_focus_frame
9809 || f == dpyinfo->x_focus_frame)
9810 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9811 }
9812 else
9813 {
06a2c219 9814#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9815 struct scroll_bar *bar
9816 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9817
7a13e894
RS
9818 if (bar)
9819 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9820#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9821 }
9822
9823 if (event.type == ButtonPress)
9824 {
9825 dpyinfo->grabbed |= (1 << event.xbutton.button);
9826 last_mouse_frame = f;
edad46f6
KH
9827 /* Ignore any mouse motion that happened
9828 before this event; any subsequent mouse-movement
9829 Emacs events should reflect only motion after
9830 the ButtonPress. */
a00e91cd
KH
9831 if (f != 0)
9832 f->mouse_moved = 0;
06a2c219 9833
9ea173e8
GM
9834 if (!tool_bar_p)
9835 last_tool_bar_item = -1;
7a13e894 9836 }
3afe33e7
RS
9837 else
9838 {
7a13e894 9839 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9840 }
23faf38f 9841
7a13e894
RS
9842 if (numchars >= 1 && emacs_event.kind != no_event)
9843 {
9844 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9845 bufp++;
9846 count++;
9847 numchars--;
9848 }
3afe33e7
RS
9849
9850#ifdef USE_X_TOOLKIT
2224b905
RS
9851 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9852 /* For a down-event in the menu bar,
9853 don't pass it to Xt right now.
9854 Instead, save it away
9855 and we will pass it to Xt from kbd_buffer_get_event.
9856 That way, we can run some Lisp code first. */
91375f8f
RS
9857 if (f && event.type == ButtonPress
9858 /* Verify the event is really within the menu bar
9859 and not just sent to it due to grabbing. */
9860 && event.xbutton.x >= 0
9861 && event.xbutton.x < f->output_data.x->pixel_width
9862 && event.xbutton.y >= 0
9863 && event.xbutton.y < f->output_data.x->menubar_height
9864 && event.xbutton.same_screen)
2224b905 9865 {
8805890a 9866 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9867 XSETFRAME (last_mouse_press_frame, f);
9868 }
9869 else if (event.type == ButtonPress)
9870 {
9871 last_mouse_press_frame = Qnil;
30e671c3 9872 goto OTHER;
ce89ef46 9873 }
06a2c219 9874
2237cac9
RS
9875#ifdef USE_MOTIF /* This should do not harm for Lucid,
9876 but I am trying to be cautious. */
ce89ef46
RS
9877 else if (event.type == ButtonRelease)
9878 {
2237cac9 9879 if (!NILP (last_mouse_press_frame))
f10ded1c 9880 {
2237cac9
RS
9881 f = XFRAME (last_mouse_press_frame);
9882 if (f->output_data.x)
06a2c219 9883 SET_SAVED_BUTTON_EVENT;
f10ded1c 9884 }
06a2c219 9885 else
30e671c3 9886 goto OTHER;
2224b905 9887 }
2237cac9 9888#endif /* USE_MOTIF */
2224b905
RS
9889 else
9890 goto OTHER;
3afe33e7 9891#endif /* USE_X_TOOLKIT */
7a13e894
RS
9892 }
9893 break;
dc6f92b8 9894
7a13e894 9895 case CirculateNotify:
06a2c219
GM
9896 goto OTHER;
9897
7a13e894 9898 case CirculateRequest:
06a2c219
GM
9899 goto OTHER;
9900
9901 case VisibilityNotify:
9902 goto OTHER;
dc6f92b8 9903
7a13e894
RS
9904 case MappingNotify:
9905 /* Someone has changed the keyboard mapping - update the
9906 local cache. */
9907 switch (event.xmapping.request)
9908 {
9909 case MappingModifier:
9910 x_find_modifier_meanings (dpyinfo);
9911 /* This is meant to fall through. */
9912 case MappingKeyboard:
9913 XRefreshKeyboardMapping (&event.xmapping);
9914 }
7a13e894 9915 goto OTHER;
dc6f92b8 9916
7a13e894 9917 default:
7a13e894 9918 OTHER:
717ca130 9919#ifdef USE_X_TOOLKIT
7a13e894
RS
9920 BLOCK_INPUT;
9921 XtDispatchEvent (&event);
9922 UNBLOCK_INPUT;
3afe33e7 9923#endif /* USE_X_TOOLKIT */
7a13e894
RS
9924 break;
9925 }
dc6f92b8
JB
9926 }
9927 }
9928
06a2c219
GM
9929 out:;
9930
9a5196d0
RS
9931 /* On some systems, an X bug causes Emacs to get no more events
9932 when the window is destroyed. Detect that. (1994.) */
58769bee 9933 if (! event_found)
ef2a22d0 9934 {
ef2a22d0
RS
9935 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9936 One XNOOP in 100 loops will make Emacs terminate.
9937 B. Bretthauer, 1994 */
9938 x_noop_count++;
58769bee 9939 if (x_noop_count >= 100)
ef2a22d0
RS
9940 {
9941 x_noop_count=0;
2224b905
RS
9942
9943 if (next_noop_dpyinfo == 0)
9944 next_noop_dpyinfo = x_display_list;
9945
9946 XNoOp (next_noop_dpyinfo->display);
9947
9948 /* Each time we get here, cycle through the displays now open. */
9949 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9950 }
9951 }
502add23 9952
06a2c219 9953 /* If the focus was just given to an auto-raising frame,
0134a210 9954 raise it now. */
7a13e894 9955 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9956 if (pending_autoraise_frame)
9957 {
9958 x_raise_frame (pending_autoraise_frame);
9959 pending_autoraise_frame = 0;
9960 }
0134a210 9961
dc6f92b8
JB
9962 UNBLOCK_INPUT;
9963 return count;
9964}
06a2c219
GM
9965
9966
9967
dc6f92b8 9968\f
06a2c219
GM
9969/***********************************************************************
9970 Text Cursor
9971 ***********************************************************************/
9972
9973/* Note if the text cursor of window W has been overwritten by a
9974 drawing operation that outputs N glyphs starting at HPOS in the
9975 line given by output_cursor.vpos. N < 0 means all the rest of the
9976 line after HPOS has been written. */
9977
9978static void
9979note_overwritten_text_cursor (w, hpos, n)
9980 struct window *w;
9981 int hpos, n;
9982{
9983 if (updated_area == TEXT_AREA
9984 && output_cursor.vpos == w->phys_cursor.vpos
9985 && hpos <= w->phys_cursor.hpos
9986 && (n < 0
9987 || hpos + n > w->phys_cursor.hpos))
9988 w->phys_cursor_on_p = 0;
9989}
f451eb13
JB
9990
9991
06a2c219
GM
9992/* Set clipping for output in glyph row ROW. W is the window in which
9993 we operate. GC is the graphics context to set clipping in.
9994 WHOLE_LINE_P non-zero means include the areas used for truncation
9995 mark display and alike in the clipping rectangle.
9996
9997 ROW may be a text row or, e.g., a mode line. Text rows must be
9998 clipped to the interior of the window dedicated to text display,
9999 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10000
10001static void
06a2c219
GM
10002x_clip_to_row (w, row, gc, whole_line_p)
10003 struct window *w;
10004 struct glyph_row *row;
10005 GC gc;
10006 int whole_line_p;
dc6f92b8 10007{
06a2c219
GM
10008 struct frame *f = XFRAME (WINDOW_FRAME (w));
10009 XRectangle clip_rect;
10010 int window_x, window_y, window_width, window_height;
dc6f92b8 10011
06a2c219 10012 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10013
06a2c219
GM
10014 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10015 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10016 clip_rect.y = max (clip_rect.y, window_y);
10017 clip_rect.width = window_width;
10018 clip_rect.height = row->visible_height;
5c1aae96 10019
06a2c219
GM
10020 /* If clipping to the whole line, including trunc marks, extend
10021 the rectangle to the left and increase its width. */
10022 if (whole_line_p)
10023 {
110859fc
GM
10024 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10025 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10026 }
5c1aae96 10027
06a2c219 10028 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10029}
10030
06a2c219
GM
10031
10032/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10033
10034static void
06a2c219
GM
10035x_draw_hollow_cursor (w, row)
10036 struct window *w;
10037 struct glyph_row *row;
dc6f92b8 10038{
06a2c219
GM
10039 struct frame *f = XFRAME (WINDOW_FRAME (w));
10040 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10041 Display *dpy = FRAME_X_DISPLAY (f);
10042 int x, y, wd, h;
10043 XGCValues xgcv;
10044 struct glyph *cursor_glyph;
10045 GC gc;
10046
10047 /* Compute frame-relative coordinates from window-relative
10048 coordinates. */
10049 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10050 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10051 + row->ascent - w->phys_cursor_ascent);
10052 h = row->height - 1;
10053
10054 /* Get the glyph the cursor is on. If we can't tell because
10055 the current matrix is invalid or such, give up. */
10056 cursor_glyph = get_phys_cursor_glyph (w);
10057 if (cursor_glyph == NULL)
dc6f92b8
JB
10058 return;
10059
06a2c219
GM
10060 /* Compute the width of the rectangle to draw. If on a stretch
10061 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10062 rectangle as wide as the glyph, but use a canonical character
10063 width instead. */
10064 wd = cursor_glyph->pixel_width - 1;
10065 if (cursor_glyph->type == STRETCH_GLYPH
10066 && !x_stretch_cursor_p)
10067 wd = min (CANON_X_UNIT (f), wd);
10068
10069 /* The foreground of cursor_gc is typically the same as the normal
10070 background color, which can cause the cursor box to be invisible. */
10071 xgcv.foreground = f->output_data.x->cursor_pixel;
10072 if (dpyinfo->scratch_cursor_gc)
10073 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10074 else
10075 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10076 GCForeground, &xgcv);
10077 gc = dpyinfo->scratch_cursor_gc;
10078
10079 /* Set clipping, draw the rectangle, and reset clipping again. */
10080 x_clip_to_row (w, row, gc, 0);
10081 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10082 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10083}
10084
06a2c219
GM
10085
10086/* Draw a bar cursor on window W in glyph row ROW.
10087
10088 Implementation note: One would like to draw a bar cursor with an
10089 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10090 Unfortunately, I didn't find a font yet that has this property set.
10091 --gerd. */
dc6f92b8
JB
10092
10093static void
f02d8aa0 10094x_draw_bar_cursor (w, row, width)
06a2c219
GM
10095 struct window *w;
10096 struct glyph_row *row;
f02d8aa0 10097 int width;
dc6f92b8 10098{
06a2c219
GM
10099 /* If cursor hpos is out of bounds, don't draw garbage. This can
10100 happen in mini-buffer windows when switching between echo area
10101 glyphs and mini-buffer. */
10102 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10103 {
10104 struct frame *f = XFRAME (w->frame);
10105 struct glyph *cursor_glyph;
10106 GC gc;
10107 int x;
10108 unsigned long mask;
10109 XGCValues xgcv;
10110 Display *dpy;
10111 Window window;
10112
10113 cursor_glyph = get_phys_cursor_glyph (w);
10114 if (cursor_glyph == NULL)
10115 return;
10116
10117 xgcv.background = f->output_data.x->cursor_pixel;
10118 xgcv.foreground = f->output_data.x->cursor_pixel;
10119 xgcv.graphics_exposures = 0;
10120 mask = GCForeground | GCBackground | GCGraphicsExposures;
10121 dpy = FRAME_X_DISPLAY (f);
10122 window = FRAME_X_WINDOW (f);
10123 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10124
10125 if (gc)
10126 XChangeGC (dpy, gc, mask, &xgcv);
10127 else
10128 {
10129 gc = XCreateGC (dpy, window, mask, &xgcv);
10130 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10131 }
10132
f02d8aa0
GM
10133 if (width < 0)
10134 width = f->output_data.x->cursor_width;
10135
06a2c219
GM
10136 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10137 x_clip_to_row (w, row, gc, 0);
10138 XFillRectangle (dpy, window, gc,
10139 x,
10140 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10141 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10142 row->height);
10143 XSetClipMask (dpy, gc, None);
10144 }
dc6f92b8
JB
10145}
10146
06a2c219
GM
10147
10148/* Clear the cursor of window W to background color, and mark the
10149 cursor as not shown. This is used when the text where the cursor
10150 is is about to be rewritten. */
10151
dc6f92b8 10152static void
06a2c219
GM
10153x_clear_cursor (w)
10154 struct window *w;
dc6f92b8 10155{
06a2c219
GM
10156 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10157 x_update_window_cursor (w, 0);
10158}
90e65f07 10159
dbc4e1c1 10160
06a2c219
GM
10161/* Draw the cursor glyph of window W in glyph row ROW. See the
10162 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10163
06a2c219
GM
10164static void
10165x_draw_phys_cursor_glyph (w, row, hl)
10166 struct window *w;
10167 struct glyph_row *row;
10168 enum draw_glyphs_face hl;
10169{
10170 /* If cursor hpos is out of bounds, don't draw garbage. This can
10171 happen in mini-buffer windows when switching between echo area
10172 glyphs and mini-buffer. */
10173 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10174 {
10175 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10176 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10177 hl, 0, 0, 0);
10178
10179 /* When we erase the cursor, and ROW is overlapped by other
10180 rows, make sure that these overlapping parts of other rows
10181 are redrawn. */
10182 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10183 {
10184 if (row > w->current_matrix->rows
10185 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10186 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10187
10188 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10189 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10190 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10191 }
10192 }
06a2c219 10193}
dbc4e1c1 10194
eea6af04 10195
06a2c219 10196/* Erase the image of a cursor of window W from the screen. */
eea6af04 10197
06a2c219
GM
10198static void
10199x_erase_phys_cursor (w)
10200 struct window *w;
10201{
10202 struct frame *f = XFRAME (w->frame);
10203 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10204 int hpos = w->phys_cursor.hpos;
10205 int vpos = w->phys_cursor.vpos;
10206 int mouse_face_here_p = 0;
10207 struct glyph_matrix *active_glyphs = w->current_matrix;
10208 struct glyph_row *cursor_row;
10209 struct glyph *cursor_glyph;
10210 enum draw_glyphs_face hl;
10211
10212 /* No cursor displayed or row invalidated => nothing to do on the
10213 screen. */
10214 if (w->phys_cursor_type == NO_CURSOR)
10215 goto mark_cursor_off;
10216
10217 /* VPOS >= active_glyphs->nrows means that window has been resized.
10218 Don't bother to erase the cursor. */
10219 if (vpos >= active_glyphs->nrows)
10220 goto mark_cursor_off;
10221
10222 /* If row containing cursor is marked invalid, there is nothing we
10223 can do. */
10224 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10225 if (!cursor_row->enabled_p)
10226 goto mark_cursor_off;
10227
10228 /* This can happen when the new row is shorter than the old one.
10229 In this case, either x_draw_glyphs or clear_end_of_line
10230 should have cleared the cursor. Note that we wouldn't be
10231 able to erase the cursor in this case because we don't have a
10232 cursor glyph at hand. */
10233 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10234 goto mark_cursor_off;
10235
10236 /* If the cursor is in the mouse face area, redisplay that when
10237 we clear the cursor. */
8801a864
KR
10238 if (! NILP (dpyinfo->mouse_face_window)
10239 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10240 && (vpos > dpyinfo->mouse_face_beg_row
10241 || (vpos == dpyinfo->mouse_face_beg_row
10242 && hpos >= dpyinfo->mouse_face_beg_col))
10243 && (vpos < dpyinfo->mouse_face_end_row
10244 || (vpos == dpyinfo->mouse_face_end_row
10245 && hpos < dpyinfo->mouse_face_end_col))
10246 /* Don't redraw the cursor's spot in mouse face if it is at the
10247 end of a line (on a newline). The cursor appears there, but
10248 mouse highlighting does not. */
10249 && cursor_row->used[TEXT_AREA] > hpos)
10250 mouse_face_here_p = 1;
10251
10252 /* Maybe clear the display under the cursor. */
10253 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10254 {
10255 int x;
045dee35 10256 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10257
06a2c219
GM
10258 cursor_glyph = get_phys_cursor_glyph (w);
10259 if (cursor_glyph == NULL)
10260 goto mark_cursor_off;
dbc4e1c1 10261
06a2c219
GM
10262 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10263
10264 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10265 x,
045dee35 10266 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10267 cursor_row->y)),
10268 cursor_glyph->pixel_width,
10269 cursor_row->visible_height,
10270 False);
dbc4e1c1 10271 }
06a2c219
GM
10272
10273 /* Erase the cursor by redrawing the character underneath it. */
10274 if (mouse_face_here_p)
10275 hl = DRAW_MOUSE_FACE;
10276 else if (cursor_row->inverse_p)
10277 hl = DRAW_INVERSE_VIDEO;
10278 else
10279 hl = DRAW_NORMAL_TEXT;
10280 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10281
06a2c219
GM
10282 mark_cursor_off:
10283 w->phys_cursor_on_p = 0;
10284 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10285}
10286
10287
06a2c219
GM
10288/* Display or clear cursor of window W. If ON is zero, clear the
10289 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10290 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10291
06a2c219
GM
10292void
10293x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10294 struct window *w;
10295 int on, hpos, vpos, x, y;
dbc4e1c1 10296{
06a2c219
GM
10297 struct frame *f = XFRAME (w->frame);
10298 int new_cursor_type;
f02d8aa0 10299 int new_cursor_width;
06a2c219
GM
10300 struct glyph_matrix *current_glyphs;
10301 struct glyph_row *glyph_row;
10302 struct glyph *glyph;
dbc4e1c1 10303
49d838ea 10304 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10305 windows and frames; in the latter case, the frame or window may
10306 be in the midst of changing its size, and x and y may be off the
10307 window. */
10308 if (! FRAME_VISIBLE_P (f)
10309 || FRAME_GARBAGED_P (f)
10310 || vpos >= w->current_matrix->nrows
10311 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10312 return;
10313
10314 /* If cursor is off and we want it off, return quickly. */
06a2c219 10315 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10316 return;
10317
06a2c219
GM
10318 current_glyphs = w->current_matrix;
10319 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10320 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10321
10322 /* If cursor row is not enabled, we don't really know where to
10323 display the cursor. */
10324 if (!glyph_row->enabled_p)
10325 {
10326 w->phys_cursor_on_p = 0;
10327 return;
10328 }
10329
10330 xassert (interrupt_input_blocked);
10331
10332 /* Set new_cursor_type to the cursor we want to be displayed. In a
10333 mini-buffer window, we want the cursor only to appear if we are
10334 reading input from this window. For the selected window, we want
10335 the cursor type given by the frame parameter. If explicitly
10336 marked off, draw no cursor. In all other cases, we want a hollow
10337 box cursor. */
f02d8aa0 10338 new_cursor_width = -1;
9b4a7047
GM
10339 if (cursor_in_echo_area
10340 && FRAME_HAS_MINIBUF_P (f)
10341 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10342 {
9b4a7047
GM
10343 if (w == XWINDOW (echo_area_window))
10344 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10345 else
10346 new_cursor_type = HOLLOW_BOX_CURSOR;
10347 }
06a2c219 10348 else
9b4a7047
GM
10349 {
10350 if (w != XWINDOW (selected_window)
10351 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10352 {
e55a0b79
GM
10353 extern int cursor_in_non_selected_windows;
10354
10355 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
9b4a7047
GM
10356 new_cursor_type = NO_CURSOR;
10357 else
10358 new_cursor_type = HOLLOW_BOX_CURSOR;
10359 }
10360 else if (w->cursor_off_p)
10361 new_cursor_type = NO_CURSOR;
10362 else
f02d8aa0
GM
10363 {
10364 struct buffer *b = XBUFFER (w->buffer);
10365
10366 if (EQ (b->cursor_type, Qt))
10367 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10368 else
10369 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10370 &new_cursor_width);
10371 }
9b4a7047 10372 }
06a2c219
GM
10373
10374 /* If cursor is currently being shown and we don't want it to be or
10375 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10376 erase it. */
06a2c219 10377 if (w->phys_cursor_on_p
dc6f92b8 10378 && (!on
06a2c219
GM
10379 || w->phys_cursor.x != x
10380 || w->phys_cursor.y != y
10381 || new_cursor_type != w->phys_cursor_type))
10382 x_erase_phys_cursor (w);
10383
10384 /* If the cursor is now invisible and we want it to be visible,
10385 display it. */
10386 if (on && !w->phys_cursor_on_p)
10387 {
10388 w->phys_cursor_ascent = glyph_row->ascent;
10389 w->phys_cursor_height = glyph_row->height;
10390
10391 /* Set phys_cursor_.* before x_draw_.* is called because some
10392 of them may need the information. */
10393 w->phys_cursor.x = x;
10394 w->phys_cursor.y = glyph_row->y;
10395 w->phys_cursor.hpos = hpos;
10396 w->phys_cursor.vpos = vpos;
10397 w->phys_cursor_type = new_cursor_type;
10398 w->phys_cursor_on_p = 1;
10399
10400 switch (new_cursor_type)
dc6f92b8 10401 {
06a2c219
GM
10402 case HOLLOW_BOX_CURSOR:
10403 x_draw_hollow_cursor (w, glyph_row);
10404 break;
10405
10406 case FILLED_BOX_CURSOR:
10407 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10408 break;
10409
10410 case BAR_CURSOR:
f02d8aa0 10411 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10412 break;
10413
10414 case NO_CURSOR:
10415 break;
dc6f92b8 10416
06a2c219
GM
10417 default:
10418 abort ();
10419 }
59ddecde
GM
10420
10421#ifdef HAVE_X_I18N
10422 if (w == XWINDOW (f->selected_window))
10423 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10424 xic_set_preeditarea (w, x, y);
10425#endif
dc6f92b8
JB
10426 }
10427
06a2c219 10428#ifndef XFlush
f676886a 10429 if (updating_frame != f)
334208b7 10430 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10431#endif
dc6f92b8
JB
10432}
10433
06a2c219
GM
10434
10435/* Display the cursor on window W, or clear it. X and Y are window
10436 relative pixel coordinates. HPOS and VPOS are glyph matrix
10437 positions. If W is not the selected window, display a hollow
10438 cursor. ON non-zero means display the cursor at X, Y which
10439 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10440
dfcf069d 10441void
06a2c219
GM
10442x_display_cursor (w, on, hpos, vpos, x, y)
10443 struct window *w;
10444 int on, hpos, vpos, x, y;
dc6f92b8 10445{
f94397b5 10446 BLOCK_INPUT;
06a2c219 10447 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10448 UNBLOCK_INPUT;
10449}
10450
06a2c219
GM
10451
10452/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10453 Don't change the cursor's position. */
10454
dfcf069d 10455void
06a2c219 10456x_update_cursor (f, on_p)
5d46f928 10457 struct frame *f;
5d46f928 10458{
06a2c219
GM
10459 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10460}
10461
10462
10463/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10464 in the window tree rooted at W. */
10465
10466static void
10467x_update_cursor_in_window_tree (w, on_p)
10468 struct window *w;
10469 int on_p;
10470{
10471 while (w)
10472 {
10473 if (!NILP (w->hchild))
10474 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10475 else if (!NILP (w->vchild))
10476 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10477 else
10478 x_update_window_cursor (w, on_p);
10479
10480 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10481 }
10482}
5d46f928 10483
f94397b5 10484
06a2c219
GM
10485/* Switch the display of W's cursor on or off, according to the value
10486 of ON. */
10487
10488static void
10489x_update_window_cursor (w, on)
10490 struct window *w;
10491 int on;
10492{
16b5d424
GM
10493 /* Don't update cursor in windows whose frame is in the process
10494 of being deleted. */
10495 if (w->current_matrix)
10496 {
10497 BLOCK_INPUT;
10498 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10499 w->phys_cursor.x, w->phys_cursor.y);
10500 UNBLOCK_INPUT;
10501 }
dc6f92b8 10502}
06a2c219
GM
10503
10504
10505
dc6f92b8
JB
10506\f
10507/* Icons. */
10508
f676886a 10509/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10510 when we get an expose event for it. */
dc6f92b8 10511
dfcf069d 10512void
f676886a
JB
10513refreshicon (f)
10514 struct frame *f;
dc6f92b8 10515{
06a2c219 10516 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10517}
10518
dbc4e1c1 10519/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10520
10521int
990ba854 10522x_bitmap_icon (f, file)
f676886a 10523 struct frame *f;
990ba854 10524 Lisp_Object file;
dc6f92b8 10525{
06a2c219 10526 int bitmap_id;
dc6f92b8 10527
c118dd06 10528 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10529 return 1;
10530
990ba854 10531 /* Free up our existing icon bitmap if any. */
7556890b
RS
10532 if (f->output_data.x->icon_bitmap > 0)
10533 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10534 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10535
10536 if (STRINGP (file))
7f2ae036
RS
10537 bitmap_id = x_create_bitmap_from_file (f, file);
10538 else
10539 {
990ba854 10540 /* Create the GNU bitmap if necessary. */
5bf01b68 10541 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10542 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10543 = x_create_bitmap_from_data (f, gnu_bits,
10544 gnu_width, gnu_height);
990ba854
RS
10545
10546 /* The first time we create the GNU bitmap,
06a2c219 10547 this increments the ref-count one extra time.
990ba854
RS
10548 As a result, the GNU bitmap is never freed.
10549 That way, we don't have to worry about allocating it again. */
334208b7 10550 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10551
334208b7 10552 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10553 }
10554
10555 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10556 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10557
10558 return 0;
10559}
10560
10561
1be2d067
KH
10562/* Make the x-window of frame F use a rectangle with text.
10563 Use ICON_NAME as the text. */
dc6f92b8
JB
10564
10565int
f676886a
JB
10566x_text_icon (f, icon_name)
10567 struct frame *f;
dc6f92b8
JB
10568 char *icon_name;
10569{
c118dd06 10570 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10571 return 1;
10572
1be2d067
KH
10573#ifdef HAVE_X11R4
10574 {
10575 XTextProperty text;
10576 text.value = (unsigned char *) icon_name;
10577 text.encoding = XA_STRING;
10578 text.format = 8;
10579 text.nitems = strlen (icon_name);
10580#ifdef USE_X_TOOLKIT
7556890b 10581 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10582 &text);
10583#else /* not USE_X_TOOLKIT */
10584 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10585#endif /* not USE_X_TOOLKIT */
10586 }
10587#else /* not HAVE_X11R4 */
10588 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10589#endif /* not HAVE_X11R4 */
58769bee 10590
7556890b
RS
10591 if (f->output_data.x->icon_bitmap > 0)
10592 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10593 f->output_data.x->icon_bitmap = 0;
b1c884c3 10594 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10595
10596 return 0;
10597}
10598\f
e99db5a1
RS
10599#define X_ERROR_MESSAGE_SIZE 200
10600
10601/* If non-nil, this should be a string.
10602 It means catch X errors and store the error message in this string. */
10603
10604static Lisp_Object x_error_message_string;
10605
10606/* An X error handler which stores the error message in
10607 x_error_message_string. This is called from x_error_handler if
10608 x_catch_errors is in effect. */
10609
06a2c219 10610static void
e99db5a1
RS
10611x_error_catcher (display, error)
10612 Display *display;
10613 XErrorEvent *error;
10614{
10615 XGetErrorText (display, error->error_code,
10616 XSTRING (x_error_message_string)->data,
10617 X_ERROR_MESSAGE_SIZE);
10618}
10619
10620/* Begin trapping X errors for display DPY. Actually we trap X errors
10621 for all displays, but DPY should be the display you are actually
10622 operating on.
10623
10624 After calling this function, X protocol errors no longer cause
10625 Emacs to exit; instead, they are recorded in the string
10626 stored in x_error_message_string.
10627
10628 Calling x_check_errors signals an Emacs error if an X error has
10629 occurred since the last call to x_catch_errors or x_check_errors.
10630
10631 Calling x_uncatch_errors resumes the normal error handling. */
10632
10633void x_check_errors ();
10634static Lisp_Object x_catch_errors_unwind ();
10635
10636int
10637x_catch_errors (dpy)
10638 Display *dpy;
10639{
10640 int count = specpdl_ptr - specpdl;
10641
10642 /* Make sure any errors from previous requests have been dealt with. */
10643 XSync (dpy, False);
10644
10645 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10646
10647 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10648 XSTRING (x_error_message_string)->data[0] = 0;
10649
10650 return count;
10651}
10652
10653/* Unbind the binding that we made to check for X errors. */
10654
10655static Lisp_Object
10656x_catch_errors_unwind (old_val)
10657 Lisp_Object old_val;
10658{
10659 x_error_message_string = old_val;
10660 return Qnil;
10661}
10662
10663/* If any X protocol errors have arrived since the last call to
10664 x_catch_errors or x_check_errors, signal an Emacs error using
10665 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10666
10667void
10668x_check_errors (dpy, format)
10669 Display *dpy;
10670 char *format;
10671{
10672 /* Make sure to catch any errors incurred so far. */
10673 XSync (dpy, False);
10674
10675 if (XSTRING (x_error_message_string)->data[0])
10676 error (format, XSTRING (x_error_message_string)->data);
10677}
10678
9829ddba
RS
10679/* Nonzero if we had any X protocol errors
10680 since we did x_catch_errors on DPY. */
e99db5a1
RS
10681
10682int
10683x_had_errors_p (dpy)
10684 Display *dpy;
10685{
10686 /* Make sure to catch any errors incurred so far. */
10687 XSync (dpy, False);
10688
10689 return XSTRING (x_error_message_string)->data[0] != 0;
10690}
10691
9829ddba
RS
10692/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10693
06a2c219 10694void
9829ddba
RS
10695x_clear_errors (dpy)
10696 Display *dpy;
10697{
10698 XSTRING (x_error_message_string)->data[0] = 0;
10699}
10700
e99db5a1
RS
10701/* Stop catching X protocol errors and let them make Emacs die.
10702 DPY should be the display that was passed to x_catch_errors.
10703 COUNT should be the value that was returned by
10704 the corresponding call to x_catch_errors. */
10705
10706void
10707x_uncatch_errors (dpy, count)
10708 Display *dpy;
10709 int count;
10710{
10711 unbind_to (count, Qnil);
10712}
10713
10714#if 0
10715static unsigned int x_wire_count;
10716x_trace_wire ()
10717{
10718 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10719}
10720#endif /* ! 0 */
10721
10722\f
10723/* Handle SIGPIPE, which can happen when the connection to a server
10724 simply goes away. SIGPIPE is handled by x_connection_signal.
10725 Don't need to do anything, because the write which caused the
10726 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10727 which will do the appropriate cleanup for us. */
e99db5a1
RS
10728
10729static SIGTYPE
10730x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10731 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10732{
10733#ifdef USG
10734 /* USG systems forget handlers when they are used;
10735 must reestablish each time */
10736 signal (signalnum, x_connection_signal);
10737#endif /* USG */
10738}
10739\f
4746118a
JB
10740/* Handling X errors. */
10741
7a13e894 10742/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10743
4746118a 10744static SIGTYPE
7a13e894
RS
10745x_connection_closed (display, error_message)
10746 Display *display;
10747 char *error_message;
4746118a 10748{
7a13e894
RS
10749 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10750 Lisp_Object frame, tail;
10751
6186a4a0
RS
10752 /* Indicate that this display is dead. */
10753
2e465cdd 10754#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 10755#ifdef USE_X_TOOLKIT
adabc3a9 10756 XtCloseDisplay (display);
2e465cdd 10757#endif
f613a4c8 10758#endif
adabc3a9 10759
6186a4a0
RS
10760 dpyinfo->display = 0;
10761
06a2c219 10762 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10763 that are on the dead display. */
10764 FOR_EACH_FRAME (tail, frame)
10765 {
10766 Lisp_Object minibuf_frame;
10767 minibuf_frame
10768 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10769 if (FRAME_X_P (XFRAME (frame))
10770 && FRAME_X_P (XFRAME (minibuf_frame))
10771 && ! EQ (frame, minibuf_frame)
10772 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10773 Fdelete_frame (frame, Qt);
10774 }
10775
10776 /* Now delete all remaining frames on the dead display.
06a2c219 10777 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10778 for another frame that we need to delete. */
10779 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10780 if (FRAME_X_P (XFRAME (frame))
10781 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10782 {
10783 /* Set this to t so that Fdelete_frame won't get confused
10784 trying to find a replacement. */
10785 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10786 Fdelete_frame (frame, Qt);
10787 }
7a13e894 10788
482a1bd2
KH
10789 if (dpyinfo)
10790 x_delete_display (dpyinfo);
7a13e894
RS
10791
10792 if (x_display_list == 0)
10793 {
f8d07b62 10794 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10795 shut_down_emacs (0, 0, Qnil);
10796 exit (70);
10797 }
12ba150f 10798
7a13e894
RS
10799 /* Ordinary stack unwind doesn't deal with these. */
10800#ifdef SIGIO
10801 sigunblock (sigmask (SIGIO));
10802#endif
10803 sigunblock (sigmask (SIGALRM));
10804 TOTALLY_UNBLOCK_INPUT;
10805
aa4d9a9e 10806 clear_waiting_for_input ();
7a13e894 10807 error ("%s", error_message);
4746118a
JB
10808}
10809
7a13e894
RS
10810/* This is the usual handler for X protocol errors.
10811 It kills all frames on the display that we got the error for.
10812 If that was the only one, it prints an error message and kills Emacs. */
10813
06a2c219 10814static void
c118dd06
JB
10815x_error_quitter (display, error)
10816 Display *display;
10817 XErrorEvent *error;
10818{
7a13e894 10819 char buf[256], buf1[356];
dc6f92b8 10820
58769bee 10821 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10822 original error handler. */
dc6f92b8 10823
c118dd06 10824 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10825 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10826 buf, error->request_code);
7a13e894 10827 x_connection_closed (display, buf1);
dc6f92b8
JB
10828}
10829
e99db5a1
RS
10830/* This is the first-level handler for X protocol errors.
10831 It calls x_error_quitter or x_error_catcher. */
7a13e894 10832
8922af5f 10833static int
e99db5a1 10834x_error_handler (display, error)
8922af5f 10835 Display *display;
e99db5a1 10836 XErrorEvent *error;
8922af5f 10837{
e99db5a1
RS
10838 if (! NILP (x_error_message_string))
10839 x_error_catcher (display, error);
10840 else
10841 x_error_quitter (display, error);
06a2c219 10842 return 0;
f9e24cb9 10843}
c118dd06 10844
e99db5a1
RS
10845/* This is the handler for X IO errors, always.
10846 It kills all frames on the display that we lost touch with.
10847 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10848
c118dd06 10849static int
e99db5a1 10850x_io_error_quitter (display)
c118dd06 10851 Display *display;
c118dd06 10852{
e99db5a1 10853 char buf[256];
dc6f92b8 10854
e99db5a1
RS
10855 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10856 x_connection_closed (display, buf);
06a2c219 10857 return 0;
dc6f92b8 10858}
dc6f92b8 10859\f
f451eb13
JB
10860/* Changing the font of the frame. */
10861
76bcdf39
RS
10862/* Give frame F the font named FONTNAME as its default font, and
10863 return the full name of that font. FONTNAME may be a wildcard
10864 pattern; in that case, we choose some font that fits the pattern.
10865 The return value shows which font we chose. */
10866
b5cf7a0e 10867Lisp_Object
f676886a
JB
10868x_new_font (f, fontname)
10869 struct frame *f;
dc6f92b8
JB
10870 register char *fontname;
10871{
dc43ef94 10872 struct font_info *fontp
ee569018 10873 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 10874
dc43ef94
KH
10875 if (!fontp)
10876 return Qnil;
2224a5fc 10877
dc43ef94 10878 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10879 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10880 f->output_data.x->fontset = -1;
10881
b2cad826
KH
10882 /* Compute the scroll bar width in character columns. */
10883 if (f->scroll_bar_pixel_width > 0)
10884 {
7556890b 10885 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10886 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10887 }
10888 else
4e61bddf
RS
10889 {
10890 int wid = FONT_WIDTH (f->output_data.x->font);
10891 f->scroll_bar_cols = (14 + wid - 1) / wid;
10892 }
b2cad826 10893
f676886a 10894 /* Now make the frame display the given font. */
c118dd06 10895 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10896 {
7556890b
RS
10897 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10898 f->output_data.x->font->fid);
10899 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10900 f->output_data.x->font->fid);
10901 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10902 f->output_data.x->font->fid);
f676886a 10903
a27f9f86 10904 frame_update_line_height (f);
0134a210 10905 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10906 }
a27f9f86
RS
10907 else
10908 /* If we are setting a new frame's font for the first time,
10909 there are no faces yet, so this font's height is the line height. */
7556890b 10910 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10911
dc43ef94
KH
10912 return build_string (fontp->full_name);
10913}
10914
10915/* Give frame F the fontset named FONTSETNAME as its default font, and
10916 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10917 pattern; in that case, we choose some fontset that fits the pattern.
10918 The return value shows which fontset we chose. */
b5cf7a0e 10919
dc43ef94
KH
10920Lisp_Object
10921x_new_fontset (f, fontsetname)
10922 struct frame *f;
10923 char *fontsetname;
10924{
ee569018 10925 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 10926 Lisp_Object result;
ee569018 10927 char *fontname;
b5cf7a0e 10928
dc43ef94
KH
10929 if (fontset < 0)
10930 return Qnil;
b5cf7a0e 10931
2da424f1
KH
10932 if (f->output_data.x->fontset == fontset)
10933 /* This fontset is already set in frame F. There's nothing more
10934 to do. */
ee569018 10935 return fontset_name (fontset);
dc43ef94 10936
ee569018 10937 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
10938
10939 if (!STRINGP (result))
10940 /* Can't load ASCII font. */
10941 return Qnil;
10942
10943 /* Since x_new_font doesn't update any fontset information, do it now. */
10944 f->output_data.x->fontset = fontset;
dc43ef94 10945
f5d11644
GM
10946#ifdef HAVE_X_I18N
10947 if (FRAME_XIC (f)
10948 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 10949 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
10950#endif
10951
dc43ef94 10952 return build_string (fontsetname);
dc6f92b8 10953}
f5d11644
GM
10954
10955\f
10956/***********************************************************************
10957 X Input Methods
10958 ***********************************************************************/
10959
10960#ifdef HAVE_X_I18N
10961
10962#ifdef HAVE_X11R6
10963
10964/* XIM destroy callback function, which is called whenever the
10965 connection to input method XIM dies. CLIENT_DATA contains a
10966 pointer to the x_display_info structure corresponding to XIM. */
10967
10968static void
10969xim_destroy_callback (xim, client_data, call_data)
10970 XIM xim;
10971 XPointer client_data;
10972 XPointer call_data;
10973{
10974 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10975 Lisp_Object frame, tail;
10976
10977 BLOCK_INPUT;
10978
10979 /* No need to call XDestroyIC.. */
10980 FOR_EACH_FRAME (tail, frame)
10981 {
10982 struct frame *f = XFRAME (frame);
10983 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
10984 {
10985 FRAME_XIC (f) = NULL;
10986 if (FRAME_XIC_FONTSET (f))
10987 {
10988 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
10989 FRAME_XIC_FONTSET (f) = NULL;
10990 }
10991 }
10992 }
10993
10994 /* No need to call XCloseIM. */
10995 dpyinfo->xim = NULL;
10996 XFree (dpyinfo->xim_styles);
10997 UNBLOCK_INPUT;
10998}
10999
11000#endif /* HAVE_X11R6 */
11001
11002/* Open the connection to the XIM server on display DPYINFO.
11003 RESOURCE_NAME is the resource name Emacs uses. */
11004
11005static void
11006xim_open_dpy (dpyinfo, resource_name)
11007 struct x_display_info *dpyinfo;
11008 char *resource_name;
11009{
11010 XIM xim;
11011
11012 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11013 dpyinfo->xim = xim;
11014
11015 if (xim)
11016 {
f5d11644
GM
11017#ifdef HAVE_X11R6
11018 XIMCallback destroy;
11019#endif
11020
11021 /* Get supported styles and XIM values. */
11022 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11023
11024#ifdef HAVE_X11R6
11025 destroy.callback = xim_destroy_callback;
11026 destroy.client_data = (XPointer)dpyinfo;
11027 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11028#endif
11029 }
11030}
11031
11032
b9de836c 11033#ifdef HAVE_X11R6_XIM
f5d11644
GM
11034
11035struct xim_inst_t
11036{
11037 struct x_display_info *dpyinfo;
11038 char *resource_name;
11039};
11040
11041/* XIM instantiate callback function, which is called whenever an XIM
11042 server is available. DISPLAY is teh display of the XIM.
11043 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11044 when the callback was registered. */
11045
11046static void
11047xim_instantiate_callback (display, client_data, call_data)
11048 Display *display;
11049 XPointer client_data;
11050 XPointer call_data;
11051{
11052 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11053 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11054
11055 /* We don't support multiple XIM connections. */
11056 if (dpyinfo->xim)
11057 return;
11058
11059 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11060
11061 /* Create XIC for the existing frames on the same display, as long
11062 as they have no XIC. */
11063 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11064 {
11065 Lisp_Object tail, frame;
11066
11067 BLOCK_INPUT;
11068 FOR_EACH_FRAME (tail, frame)
11069 {
11070 struct frame *f = XFRAME (frame);
11071
11072 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11073 if (FRAME_XIC (f) == NULL)
11074 {
11075 create_frame_xic (f);
11076 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11077 xic_set_statusarea (f);
11078 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11079 {
11080 struct window *w = XWINDOW (f->selected_window);
11081 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11082 }
11083 }
11084 }
11085
11086 UNBLOCK_INPUT;
11087 }
11088}
11089
b9de836c 11090#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11091
11092
11093/* Open a connection to the XIM server on display DPYINFO.
11094 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11095 connection only at the first time. On X11R6, open the connection
11096 in the XIM instantiate callback function. */
11097
11098static void
11099xim_initialize (dpyinfo, resource_name)
11100 struct x_display_info *dpyinfo;
11101 char *resource_name;
11102{
b9de836c 11103#ifdef HAVE_X11R6_XIM
f5d11644
GM
11104 struct xim_inst_t *xim_inst;
11105 int len;
11106
11107 dpyinfo->xim = NULL;
11108 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11109 xim_inst->dpyinfo = dpyinfo;
11110 len = strlen (resource_name);
11111 xim_inst->resource_name = (char *) xmalloc (len + 1);
11112 bcopy (resource_name, xim_inst->resource_name, len + 1);
11113 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11114 resource_name, EMACS_CLASS,
11115 xim_instantiate_callback,
11116 (XPointer)xim_inst);
b9de836c 11117#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11118 dpyinfo->xim = NULL;
11119 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11120#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11121}
11122
11123
11124/* Close the connection to the XIM server on display DPYINFO. */
11125
11126static void
11127xim_close_dpy (dpyinfo)
11128 struct x_display_info *dpyinfo;
11129{
b9de836c 11130#ifdef HAVE_X11R6_XIM
f5d11644
GM
11131 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11132 NULL, EMACS_CLASS,
11133 xim_instantiate_callback, NULL);
b9de836c 11134#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11135 XCloseIM (dpyinfo->xim);
11136 dpyinfo->xim = NULL;
11137 XFree (dpyinfo->xim_styles);
11138}
11139
b9de836c 11140#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11141
11142
dc6f92b8 11143\f
2e365682
RS
11144/* Calculate the absolute position in frame F
11145 from its current recorded position values and gravity. */
11146
dfcf069d 11147void
43bca5d5 11148x_calc_absolute_position (f)
f676886a 11149 struct frame *f;
dc6f92b8 11150{
06a2c219 11151 Window child;
6dba1858 11152 int win_x = 0, win_y = 0;
7556890b 11153 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11154 int this_window;
11155
9829ddba
RS
11156 /* We have nothing to do if the current position
11157 is already for the top-left corner. */
11158 if (! ((flags & XNegative) || (flags & YNegative)))
11159 return;
11160
c81412a0 11161#ifdef USE_X_TOOLKIT
7556890b 11162 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11163#else
11164 this_window = FRAME_X_WINDOW (f);
11165#endif
6dba1858
RS
11166
11167 /* Find the position of the outside upper-left corner of
9829ddba
RS
11168 the inner window, with respect to the outer window.
11169 But do this only if we will need the results. */
7556890b 11170 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11171 {
9829ddba
RS
11172 int count;
11173
6dba1858 11174 BLOCK_INPUT;
9829ddba
RS
11175 count = x_catch_errors (FRAME_X_DISPLAY (f));
11176 while (1)
11177 {
11178 x_clear_errors (FRAME_X_DISPLAY (f));
11179 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11180
11181 /* From-window, to-window. */
11182 this_window,
11183 f->output_data.x->parent_desc,
11184
11185 /* From-position, to-position. */
11186 0, 0, &win_x, &win_y,
11187
11188 /* Child of win. */
11189 &child);
11190 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11191 {
11192 Window newroot, newparent = 0xdeadbeef;
11193 Window *newchildren;
11194 int nchildren;
11195
11196 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11197 &newparent, &newchildren, &nchildren))
11198 break;
58769bee 11199
7c3c78a3 11200 XFree ((char *) newchildren);
6dba1858 11201
9829ddba
RS
11202 f->output_data.x->parent_desc = newparent;
11203 }
11204 else
11205 break;
11206 }
6dba1858 11207
9829ddba 11208 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11209 UNBLOCK_INPUT;
11210 }
11211
11212 /* Treat negative positions as relative to the leftmost bottommost
11213 position that fits on the screen. */
20f55f9a 11214 if (flags & XNegative)
7556890b 11215 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11216 - 2 * f->output_data.x->border_width - win_x
11217 - PIXEL_WIDTH (f)
11218 + f->output_data.x->left_pos);
dc6f92b8 11219
20f55f9a 11220 if (flags & YNegative)
06a2c219
GM
11221 {
11222 int menubar_height = 0;
11223
11224#ifdef USE_X_TOOLKIT
11225 if (f->output_data.x->menubar_widget)
11226 menubar_height
11227 = (f->output_data.x->menubar_widget->core.height
11228 + f->output_data.x->menubar_widget->core.border_width);
11229#endif
11230
11231 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11232 - 2 * f->output_data.x->border_width
11233 - win_y
11234 - PIXEL_HEIGHT (f)
11235 - menubar_height
11236 + f->output_data.x->top_pos);
11237 }
2e365682 11238
3a35ab44
RS
11239 /* The left_pos and top_pos
11240 are now relative to the top and left screen edges,
11241 so the flags should correspond. */
7556890b 11242 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11243}
11244
3a35ab44
RS
11245/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11246 to really change the position, and 0 when calling from
11247 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11248 position values). It is -1 when calling from x_set_frame_parameters,
11249 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11250
dfcf069d 11251void
dc05a16b 11252x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11253 struct frame *f;
dc6f92b8 11254 register int xoff, yoff;
dc05a16b 11255 int change_gravity;
dc6f92b8 11256{
4a4cbdd5
KH
11257 int modified_top, modified_left;
11258
aa3ff7c9 11259 if (change_gravity > 0)
3a35ab44 11260 {
7556890b
RS
11261 f->output_data.x->top_pos = yoff;
11262 f->output_data.x->left_pos = xoff;
11263 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11264 if (xoff < 0)
7556890b 11265 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11266 if (yoff < 0)
7556890b
RS
11267 f->output_data.x->size_hint_flags |= YNegative;
11268 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11269 }
43bca5d5 11270 x_calc_absolute_position (f);
dc6f92b8
JB
11271
11272 BLOCK_INPUT;
c32cdd9a 11273 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11274
7556890b
RS
11275 modified_left = f->output_data.x->left_pos;
11276 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11277#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11278 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11279 /* It is a mystery why we need to add the border_width here
11280 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11281 if (change_gravity != 0)
4a4cbdd5 11282 {
7556890b
RS
11283 modified_left += f->output_data.x->border_width;
11284 modified_top += f->output_data.x->border_width;
4a4cbdd5 11285 }
e73ec6fa 11286#endif
4a4cbdd5 11287
3afe33e7 11288#ifdef USE_X_TOOLKIT
7556890b 11289 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11290 modified_left, modified_top);
3afe33e7 11291#else /* not USE_X_TOOLKIT */
334208b7 11292 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11293 modified_left, modified_top);
3afe33e7 11294#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11295 UNBLOCK_INPUT;
11296}
11297
bc20ebbf
FP
11298/* Call this to change the size of frame F's x-window.
11299 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11300 for this size change and subsequent size changes.
11301 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11302
dfcf069d 11303void
bc20ebbf 11304x_set_window_size (f, change_gravity, cols, rows)
f676886a 11305 struct frame *f;
bc20ebbf 11306 int change_gravity;
b1c884c3 11307 int cols, rows;
dc6f92b8 11308{
06a2c219 11309#ifndef USE_X_TOOLKIT
dc6f92b8 11310 int pixelwidth, pixelheight;
06a2c219 11311#endif
dc6f92b8 11312
80fd1fe2 11313 BLOCK_INPUT;
aee9a898
RS
11314
11315#ifdef USE_X_TOOLKIT
3a20653d
RS
11316 {
11317 /* The x and y position of the widget is clobbered by the
11318 call to XtSetValues within EmacsFrameSetCharSize.
11319 This is a real kludge, but I don't understand Xt so I can't
11320 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11321 int xpos = f->output_data.x->widget->core.x;
11322 int ypos = f->output_data.x->widget->core.y;
11323 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11324 f->output_data.x->widget->core.x = xpos;
11325 f->output_data.x->widget->core.y = ypos;
3a20653d 11326 }
80fd1fe2
FP
11327
11328#else /* not USE_X_TOOLKIT */
11329
b1c884c3 11330 check_frame_size (f, &rows, &cols);
7556890b 11331 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11332 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11333 ? 0
11334 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11335 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11336 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11337 f->output_data.x->flags_areas_extra
110859fc 11338 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11339 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11340 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11341
7556890b 11342 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11343 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11344
334208b7
RS
11345 XSync (FRAME_X_DISPLAY (f), False);
11346 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11347 pixelwidth, pixelheight);
b1c884c3
JB
11348
11349 /* Now, strictly speaking, we can't be sure that this is accurate,
11350 but the window manager will get around to dealing with the size
11351 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11352 ConfigureNotify event gets here.
11353
11354 We could just not bother storing any of this information here,
11355 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11356 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11357 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11358 point in the future when the ConfigureNotify event arrives.
11359
11360 We pass 1 for DELAY since we can't run Lisp code inside of
11361 a BLOCK_INPUT. */
7d1e984f 11362 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11363 PIXEL_WIDTH (f) = pixelwidth;
11364 PIXEL_HEIGHT (f) = pixelheight;
11365
aee9a898
RS
11366 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11367 receive in the ConfigureNotify event; if we get what we asked
11368 for, then the event won't cause the screen to become garbaged, so
11369 we have to make sure to do it here. */
11370 SET_FRAME_GARBAGED (f);
11371
11372 XFlush (FRAME_X_DISPLAY (f));
11373
11374#endif /* not USE_X_TOOLKIT */
11375
4d73d038 11376 /* If cursor was outside the new size, mark it as off. */
06a2c219 11377 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11378
aee9a898
RS
11379 /* Clear out any recollection of where the mouse highlighting was,
11380 since it might be in a place that's outside the new frame size.
11381 Actually checking whether it is outside is a pain in the neck,
11382 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11383 cancel_mouse_face (f);
dbc4e1c1 11384
dc6f92b8
JB
11385 UNBLOCK_INPUT;
11386}
dc6f92b8 11387\f
d047c4eb 11388/* Mouse warping. */
dc6f92b8 11389
9b378208 11390void
f676886a
JB
11391x_set_mouse_position (f, x, y)
11392 struct frame *f;
dc6f92b8
JB
11393 int x, y;
11394{
11395 int pix_x, pix_y;
11396
7556890b
RS
11397 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11398 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11399
11400 if (pix_x < 0) pix_x = 0;
11401 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11402
11403 if (pix_y < 0) pix_y = 0;
11404 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11405
11406 BLOCK_INPUT;
dc6f92b8 11407
334208b7
RS
11408 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11409 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11410 UNBLOCK_INPUT;
11411}
11412
9b378208
RS
11413/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11414
11415void
11416x_set_mouse_pixel_position (f, pix_x, pix_y)
11417 struct frame *f;
11418 int pix_x, pix_y;
11419{
11420 BLOCK_INPUT;
11421
334208b7
RS
11422 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11423 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11424 UNBLOCK_INPUT;
11425}
d047c4eb
KH
11426\f
11427/* focus shifting, raising and lowering. */
9b378208 11428
dfcf069d 11429void
f676886a
JB
11430x_focus_on_frame (f)
11431 struct frame *f;
dc6f92b8 11432{
1fb20991 11433#if 0 /* This proves to be unpleasant. */
f676886a 11434 x_raise_frame (f);
1fb20991 11435#endif
6d4238f3
JB
11436#if 0
11437 /* I don't think that the ICCCM allows programs to do things like this
11438 without the interaction of the window manager. Whatever you end up
f676886a 11439 doing with this code, do it to x_unfocus_frame too. */
334208b7 11440 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11441 RevertToPointerRoot, CurrentTime);
c118dd06 11442#endif /* ! 0 */
dc6f92b8
JB
11443}
11444
dfcf069d 11445void
f676886a
JB
11446x_unfocus_frame (f)
11447 struct frame *f;
dc6f92b8 11448{
6d4238f3 11449#if 0
f676886a 11450 /* Look at the remarks in x_focus_on_frame. */
0f941935 11451 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11452 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11453 RevertToPointerRoot, CurrentTime);
c118dd06 11454#endif /* ! 0 */
dc6f92b8
JB
11455}
11456
f676886a 11457/* Raise frame F. */
dc6f92b8 11458
dfcf069d 11459void
f676886a
JB
11460x_raise_frame (f)
11461 struct frame *f;
dc6f92b8 11462{
3a88c238 11463 if (f->async_visible)
dc6f92b8
JB
11464 {
11465 BLOCK_INPUT;
3afe33e7 11466#ifdef USE_X_TOOLKIT
7556890b 11467 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11468#else /* not USE_X_TOOLKIT */
334208b7 11469 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11470#endif /* not USE_X_TOOLKIT */
334208b7 11471 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11472 UNBLOCK_INPUT;
11473 }
11474}
11475
f676886a 11476/* Lower frame F. */
dc6f92b8 11477
dfcf069d 11478void
f676886a
JB
11479x_lower_frame (f)
11480 struct frame *f;
dc6f92b8 11481{
3a88c238 11482 if (f->async_visible)
dc6f92b8
JB
11483 {
11484 BLOCK_INPUT;
3afe33e7 11485#ifdef USE_X_TOOLKIT
7556890b 11486 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11487#else /* not USE_X_TOOLKIT */
334208b7 11488 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11489#endif /* not USE_X_TOOLKIT */
334208b7 11490 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11491 UNBLOCK_INPUT;
11492 }
11493}
11494
dbc4e1c1 11495static void
6b0442dc 11496XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11497 FRAME_PTR f;
6b0442dc 11498 int raise_flag;
dbc4e1c1 11499{
6b0442dc 11500 if (raise_flag)
dbc4e1c1
JB
11501 x_raise_frame (f);
11502 else
11503 x_lower_frame (f);
11504}
d047c4eb
KH
11505\f
11506/* Change of visibility. */
dc6f92b8 11507
9382638d
KH
11508/* This tries to wait until the frame is really visible.
11509 However, if the window manager asks the user where to position
11510 the frame, this will return before the user finishes doing that.
11511 The frame will not actually be visible at that time,
11512 but it will become visible later when the window manager
11513 finishes with it. */
11514
dfcf069d 11515void
f676886a
JB
11516x_make_frame_visible (f)
11517 struct frame *f;
dc6f92b8 11518{
990ba854 11519 Lisp_Object type;
1aa6072f 11520 int original_top, original_left;
dc6f92b8 11521
dc6f92b8 11522 BLOCK_INPUT;
dc6f92b8 11523
990ba854
RS
11524 type = x_icon_type (f);
11525 if (!NILP (type))
11526 x_bitmap_icon (f, type);
bdcd49ba 11527
f676886a 11528 if (! FRAME_VISIBLE_P (f))
90e65f07 11529 {
1aa6072f
RS
11530 /* We test FRAME_GARBAGED_P here to make sure we don't
11531 call x_set_offset a second time
11532 if we get to x_make_frame_visible a second time
11533 before the window gets really visible. */
11534 if (! FRAME_ICONIFIED_P (f)
11535 && ! f->output_data.x->asked_for_visible)
11536 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11537
11538 f->output_data.x->asked_for_visible = 1;
11539
90e65f07 11540 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11541 x_wm_set_window_state (f, NormalState);
3afe33e7 11542#ifdef USE_X_TOOLKIT
d7a38a2e 11543 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11544 XtMapWidget (f->output_data.x->widget);
3afe33e7 11545#else /* not USE_X_TOOLKIT */
7f9c7f94 11546 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11547#endif /* not USE_X_TOOLKIT */
0134a210
RS
11548#if 0 /* This seems to bring back scroll bars in the wrong places
11549 if the window configuration has changed. They seem
11550 to come back ok without this. */
ab648270 11551 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11552 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11553#endif
90e65f07 11554 }
dc6f92b8 11555
334208b7 11556 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11557
0dacf791
RS
11558 /* Synchronize to ensure Emacs knows the frame is visible
11559 before we do anything else. We do this loop with input not blocked
11560 so that incoming events are handled. */
11561 {
11562 Lisp_Object frame;
12ce2351 11563 int count;
28c01ffe
RS
11564 /* This must be before UNBLOCK_INPUT
11565 since events that arrive in response to the actions above
11566 will set it when they are handled. */
11567 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11568
11569 original_left = f->output_data.x->left_pos;
11570 original_top = f->output_data.x->top_pos;
c0a04927
RS
11571
11572 /* This must come after we set COUNT. */
11573 UNBLOCK_INPUT;
11574
2745e6c4 11575 /* We unblock here so that arriving X events are processed. */
1aa6072f 11576
dcb07ae9
RS
11577 /* Now move the window back to where it was "supposed to be".
11578 But don't do it if the gravity is negative.
11579 When the gravity is negative, this uses a position
28c01ffe
RS
11580 that is 3 pixels too low. Perhaps that's really the border width.
11581
11582 Don't do this if the window has never been visible before,
11583 because the window manager may choose the position
11584 and we don't want to override it. */
1aa6072f 11585
4d3f5d9a 11586 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11587 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11588 && previously_visible)
1aa6072f 11589 {
2745e6c4
RS
11590 Drawable rootw;
11591 int x, y;
11592 unsigned int width, height, border, depth;
06a2c219 11593
1aa6072f 11594 BLOCK_INPUT;
9829ddba 11595
06a2c219
GM
11596 /* On some window managers (such as FVWM) moving an existing
11597 window, even to the same place, causes the window manager
11598 to introduce an offset. This can cause the window to move
11599 to an unexpected location. Check the geometry (a little
11600 slow here) and then verify that the window is in the right
11601 place. If the window is not in the right place, move it
11602 there, and take the potential window manager hit. */
2745e6c4
RS
11603 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11604 &rootw, &x, &y, &width, &height, &border, &depth);
11605
11606 if (original_left != x || original_top != y)
11607 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11608 original_left, original_top);
11609
1aa6072f
RS
11610 UNBLOCK_INPUT;
11611 }
9829ddba 11612
e0c1aef2 11613 XSETFRAME (frame, f);
c0a04927 11614
12ce2351
GM
11615 /* Wait until the frame is visible. Process X events until a
11616 MapNotify event has been seen, or until we think we won't get a
11617 MapNotify at all.. */
11618 for (count = input_signal_count + 10;
11619 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11620 {
12ce2351 11621 /* Force processing of queued events. */
334208b7 11622 x_sync (f);
12ce2351
GM
11623
11624 /* Machines that do polling rather than SIGIO have been
11625 observed to go into a busy-wait here. So we'll fake an
11626 alarm signal to let the handler know that there's something
11627 to be read. We used to raise a real alarm, but it seems
11628 that the handler isn't always enabled here. This is
11629 probably a bug. */
8b2f8d4e 11630 if (input_polling_used ())
3b2fa4e6 11631 {
12ce2351
GM
11632 /* It could be confusing if a real alarm arrives while
11633 processing the fake one. Turn it off and let the
11634 handler reset it. */
bffcfca9
GM
11635 int old_poll_suppress_count = poll_suppress_count;
11636 poll_suppress_count = 1;
11637 poll_for_input_1 ();
11638 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11639 }
12ce2351
GM
11640
11641 /* See if a MapNotify event has been processed. */
11642 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11643 }
0dacf791 11644 }
dc6f92b8
JB
11645}
11646
06a2c219 11647/* Change from mapped state to withdrawn state. */
dc6f92b8 11648
d047c4eb
KH
11649/* Make the frame visible (mapped and not iconified). */
11650
dfcf069d 11651void
f676886a
JB
11652x_make_frame_invisible (f)
11653 struct frame *f;
dc6f92b8 11654{
546e6d5b
RS
11655 Window window;
11656
11657#ifdef USE_X_TOOLKIT
11658 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11659 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11660#else /* not USE_X_TOOLKIT */
11661 window = FRAME_X_WINDOW (f);
11662#endif /* not USE_X_TOOLKIT */
dc6f92b8 11663
9319ae23 11664 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11665 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11666 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11667
5627c40e 11668#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11669 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11670 return;
5627c40e 11671#endif
dc6f92b8
JB
11672
11673 BLOCK_INPUT;
c118dd06 11674
af31d76f
RS
11675 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11676 that the current position of the window is user-specified, rather than
11677 program-specified, so that when the window is mapped again, it will be
11678 placed at the same location, without forcing the user to position it
11679 by hand again (they have already done that once for this window.) */
c32cdd9a 11680 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11681
c118dd06
JB
11682#ifdef HAVE_X11R4
11683
334208b7
RS
11684 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11685 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11686 {
11687 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11688 error ("Can't notify window manager of window withdrawal");
c118dd06 11689 }
c118dd06 11690#else /* ! defined (HAVE_X11R4) */
16bd92ea 11691
c118dd06 11692 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11693 if (! EQ (Vx_no_window_manager, Qt))
11694 {
16bd92ea 11695 XEvent unmap;
dc6f92b8 11696
16bd92ea 11697 unmap.xunmap.type = UnmapNotify;
546e6d5b 11698 unmap.xunmap.window = window;
334208b7 11699 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11700 unmap.xunmap.from_configure = False;
334208b7
RS
11701 if (! XSendEvent (FRAME_X_DISPLAY (f),
11702 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11703 False,
06a2c219 11704 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11705 &unmap))
11706 {
11707 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11708 error ("Can't notify window manager of withdrawal");
16bd92ea 11709 }
dc6f92b8
JB
11710 }
11711
16bd92ea 11712 /* Unmap the window ourselves. Cheeky! */
334208b7 11713 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11714#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11715
5627c40e
RS
11716 /* We can't distinguish this from iconification
11717 just by the event that we get from the server.
11718 So we can't win using the usual strategy of letting
11719 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11720 and synchronize with the server to make sure we agree. */
11721 f->visible = 0;
11722 FRAME_ICONIFIED_P (f) = 0;
11723 f->async_visible = 0;
11724 f->async_iconified = 0;
11725
334208b7 11726 x_sync (f);
5627c40e 11727
dc6f92b8
JB
11728 UNBLOCK_INPUT;
11729}
11730
06a2c219 11731/* Change window state from mapped to iconified. */
dc6f92b8 11732
dfcf069d 11733void
f676886a
JB
11734x_iconify_frame (f)
11735 struct frame *f;
dc6f92b8 11736{
3afe33e7 11737 int result;
990ba854 11738 Lisp_Object type;
dc6f92b8 11739
9319ae23 11740 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11741 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11742 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11743
3a88c238 11744 if (f->async_iconified)
dc6f92b8
JB
11745 return;
11746
3afe33e7 11747 BLOCK_INPUT;
546e6d5b 11748
9af3143a
RS
11749 FRAME_SAMPLE_VISIBILITY (f);
11750
990ba854
RS
11751 type = x_icon_type (f);
11752 if (!NILP (type))
11753 x_bitmap_icon (f, type);
bdcd49ba
RS
11754
11755#ifdef USE_X_TOOLKIT
11756
546e6d5b
RS
11757 if (! FRAME_VISIBLE_P (f))
11758 {
11759 if (! EQ (Vx_no_window_manager, Qt))
11760 x_wm_set_window_state (f, IconicState);
11761 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11762 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11763 /* The server won't give us any event to indicate
11764 that an invisible frame was changed to an icon,
11765 so we have to record it here. */
11766 f->iconified = 1;
1e6bc770 11767 f->visible = 1;
9cf30a30 11768 f->async_iconified = 1;
1e6bc770 11769 f->async_visible = 0;
546e6d5b
RS
11770 UNBLOCK_INPUT;
11771 return;
11772 }
11773
334208b7 11774 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11775 XtWindow (f->output_data.x->widget),
334208b7 11776 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11777 UNBLOCK_INPUT;
11778
11779 if (!result)
546e6d5b 11780 error ("Can't notify window manager of iconification");
3afe33e7
RS
11781
11782 f->async_iconified = 1;
1e6bc770
RS
11783 f->async_visible = 0;
11784
8c002a25
KH
11785
11786 BLOCK_INPUT;
334208b7 11787 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11788 UNBLOCK_INPUT;
3afe33e7
RS
11789#else /* not USE_X_TOOLKIT */
11790
fd13dbb2
RS
11791 /* Make sure the X server knows where the window should be positioned,
11792 in case the user deiconifies with the window manager. */
11793 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11794 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11795
16bd92ea
JB
11796 /* Since we don't know which revision of X we're running, we'll use both
11797 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11798
11799 /* X11R4: send a ClientMessage to the window manager using the
11800 WM_CHANGE_STATE type. */
11801 {
11802 XEvent message;
58769bee 11803
c118dd06 11804 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11805 message.xclient.type = ClientMessage;
334208b7 11806 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11807 message.xclient.format = 32;
11808 message.xclient.data.l[0] = IconicState;
11809
334208b7
RS
11810 if (! XSendEvent (FRAME_X_DISPLAY (f),
11811 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11812 False,
11813 SubstructureRedirectMask | SubstructureNotifyMask,
11814 &message))
dc6f92b8
JB
11815 {
11816 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11817 error ("Can't notify window manager of iconification");
dc6f92b8 11818 }
16bd92ea 11819 }
dc6f92b8 11820
58769bee 11821 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11822 IconicState. */
11823 x_wm_set_window_state (f, IconicState);
dc6f92b8 11824
a9c00105
RS
11825 if (!FRAME_VISIBLE_P (f))
11826 {
11827 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11828 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11829 }
11830
3a88c238 11831 f->async_iconified = 1;
1e6bc770 11832 f->async_visible = 0;
dc6f92b8 11833
334208b7 11834 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11835 UNBLOCK_INPUT;
8c002a25 11836#endif /* not USE_X_TOOLKIT */
dc6f92b8 11837}
d047c4eb 11838\f
c0ff3fab 11839/* Destroy the X window of frame F. */
dc6f92b8 11840
dfcf069d 11841void
c0ff3fab 11842x_destroy_window (f)
f676886a 11843 struct frame *f;
dc6f92b8 11844{
7f9c7f94
RS
11845 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11846
dc6f92b8 11847 BLOCK_INPUT;
c0ff3fab 11848
6186a4a0
RS
11849 /* If a display connection is dead, don't try sending more
11850 commands to the X server. */
11851 if (dpyinfo->display != 0)
11852 {
11853 if (f->output_data.x->icon_desc != 0)
11854 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 11855#ifdef HAVE_X_I18N
f5d11644
GM
11856 if (FRAME_XIC (f))
11857 free_frame_xic (f);
31f41daf 11858#endif
6186a4a0 11859 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11860#ifdef USE_X_TOOLKIT
06a2c219
GM
11861 if (f->output_data.x->widget)
11862 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11863 free_frame_menubar (f);
3afe33e7
RS
11864#endif /* USE_X_TOOLKIT */
11865
6186a4a0
RS
11866 free_frame_faces (f);
11867 XFlush (FRAME_X_DISPLAY (f));
11868 }
dc6f92b8 11869
df89d8a4 11870 if (f->output_data.x->saved_menu_event)
06a2c219 11871 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11872
7556890b
RS
11873 xfree (f->output_data.x);
11874 f->output_data.x = 0;
0f941935
KH
11875 if (f == dpyinfo->x_focus_frame)
11876 dpyinfo->x_focus_frame = 0;
11877 if (f == dpyinfo->x_focus_event_frame)
11878 dpyinfo->x_focus_event_frame = 0;
11879 if (f == dpyinfo->x_highlight_frame)
11880 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11881
7f9c7f94
RS
11882 dpyinfo->reference_count--;
11883
11884 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11885 {
7f9c7f94
RS
11886 dpyinfo->mouse_face_beg_row
11887 = dpyinfo->mouse_face_beg_col = -1;
11888 dpyinfo->mouse_face_end_row
11889 = dpyinfo->mouse_face_end_col = -1;
11890 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11891 dpyinfo->mouse_face_deferred_gc = 0;
11892 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11893 }
0134a210 11894
c0ff3fab 11895 UNBLOCK_INPUT;
dc6f92b8
JB
11896}
11897\f
f451eb13
JB
11898/* Setting window manager hints. */
11899
af31d76f
RS
11900/* Set the normal size hints for the window manager, for frame F.
11901 FLAGS is the flags word to use--or 0 meaning preserve the flags
11902 that the window now has.
11903 If USER_POSITION is nonzero, we set the USPosition
11904 flag (this is useful when FLAGS is 0). */
6dba1858 11905
dfcf069d 11906void
af31d76f 11907x_wm_set_size_hint (f, flags, user_position)
f676886a 11908 struct frame *f;
af31d76f
RS
11909 long flags;
11910 int user_position;
dc6f92b8
JB
11911{
11912 XSizeHints size_hints;
3afe33e7
RS
11913
11914#ifdef USE_X_TOOLKIT
7e4f2521
FP
11915 Arg al[2];
11916 int ac = 0;
11917 Dimension widget_width, widget_height;
7556890b 11918 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11919#else /* not USE_X_TOOLKIT */
c118dd06 11920 Window window = FRAME_X_WINDOW (f);
3afe33e7 11921#endif /* not USE_X_TOOLKIT */
dc6f92b8 11922
b72a58fd
RS
11923 /* Setting PMaxSize caused various problems. */
11924 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11925
7556890b
RS
11926 size_hints.x = f->output_data.x->left_pos;
11927 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11928
7e4f2521
FP
11929#ifdef USE_X_TOOLKIT
11930 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11931 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11932 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11933 size_hints.height = widget_height;
11934 size_hints.width = widget_width;
11935#else /* not USE_X_TOOLKIT */
f676886a
JB
11936 size_hints.height = PIXEL_HEIGHT (f);
11937 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11938#endif /* not USE_X_TOOLKIT */
7553a6b7 11939
7556890b
RS
11940 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11941 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11942 size_hints.max_width
11943 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11944 size_hints.max_height
11945 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11946
d067ea8b
KH
11947 /* Calculate the base and minimum sizes.
11948
11949 (When we use the X toolkit, we don't do it here.
11950 Instead we copy the values that the widgets are using, below.) */
11951#ifndef USE_X_TOOLKIT
b1c884c3 11952 {
b0342f17 11953 int base_width, base_height;
0134a210 11954 int min_rows = 0, min_cols = 0;
b0342f17 11955
f451eb13
JB
11956 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11957 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11958
0134a210 11959 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11960
0134a210
RS
11961 /* The window manager uses the base width hints to calculate the
11962 current number of rows and columns in the frame while
11963 resizing; min_width and min_height aren't useful for this
11964 purpose, since they might not give the dimensions for a
11965 zero-row, zero-column frame.
58769bee 11966
0134a210
RS
11967 We use the base_width and base_height members if we have
11968 them; otherwise, we set the min_width and min_height members
11969 to the size for a zero x zero frame. */
b0342f17
JB
11970
11971#ifdef HAVE_X11R4
0134a210
RS
11972 size_hints.flags |= PBaseSize;
11973 size_hints.base_width = base_width;
11974 size_hints.base_height = base_height;
11975 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11976 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11977#else
0134a210
RS
11978 size_hints.min_width = base_width;
11979 size_hints.min_height = base_height;
b0342f17 11980#endif
b1c884c3 11981 }
dc6f92b8 11982
d067ea8b 11983 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11984 if (flags)
dc6f92b8 11985 {
d067ea8b
KH
11986 size_hints.flags |= flags;
11987 goto no_read;
11988 }
11989#endif /* not USE_X_TOOLKIT */
11990
11991 {
11992 XSizeHints hints; /* Sometimes I hate X Windows... */
11993 long supplied_return;
11994 int value;
af31d76f
RS
11995
11996#ifdef HAVE_X11R4
d067ea8b
KH
11997 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11998 &supplied_return);
af31d76f 11999#else
d067ea8b 12000 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12001#endif
58769bee 12002
d067ea8b
KH
12003#ifdef USE_X_TOOLKIT
12004 size_hints.base_height = hints.base_height;
12005 size_hints.base_width = hints.base_width;
12006 size_hints.min_height = hints.min_height;
12007 size_hints.min_width = hints.min_width;
12008#endif
12009
12010 if (flags)
12011 size_hints.flags |= flags;
12012 else
12013 {
12014 if (value == 0)
12015 hints.flags = 0;
12016 if (hints.flags & PSize)
12017 size_hints.flags |= PSize;
12018 if (hints.flags & PPosition)
12019 size_hints.flags |= PPosition;
12020 if (hints.flags & USPosition)
12021 size_hints.flags |= USPosition;
12022 if (hints.flags & USSize)
12023 size_hints.flags |= USSize;
12024 }
12025 }
12026
06a2c219 12027#ifndef USE_X_TOOLKIT
d067ea8b 12028 no_read:
06a2c219 12029#endif
0134a210 12030
af31d76f 12031#ifdef PWinGravity
7556890b 12032 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12033 size_hints.flags |= PWinGravity;
dc05a16b 12034
af31d76f 12035 if (user_position)
6dba1858 12036 {
af31d76f
RS
12037 size_hints.flags &= ~ PPosition;
12038 size_hints.flags |= USPosition;
6dba1858 12039 }
2554751d 12040#endif /* PWinGravity */
6dba1858 12041
b0342f17 12042#ifdef HAVE_X11R4
334208b7 12043 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12044#else
334208b7 12045 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12046#endif
dc6f92b8
JB
12047}
12048
12049/* Used for IconicState or NormalState */
06a2c219 12050
dfcf069d 12051void
f676886a
JB
12052x_wm_set_window_state (f, state)
12053 struct frame *f;
dc6f92b8
JB
12054 int state;
12055{
3afe33e7 12056#ifdef USE_X_TOOLKIT
546e6d5b
RS
12057 Arg al[1];
12058
12059 XtSetArg (al[0], XtNinitialState, state);
7556890b 12060 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12061#else /* not USE_X_TOOLKIT */
c118dd06 12062 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12063
7556890b
RS
12064 f->output_data.x->wm_hints.flags |= StateHint;
12065 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12066
7556890b 12067 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12068#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12069}
12070
dfcf069d 12071void
7f2ae036 12072x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12073 struct frame *f;
7f2ae036 12074 int pixmap_id;
dc6f92b8 12075{
d2bd6bc4
RS
12076 Pixmap icon_pixmap;
12077
06a2c219 12078#ifndef USE_X_TOOLKIT
c118dd06 12079 Window window = FRAME_X_WINDOW (f);
75231bad 12080#endif
dc6f92b8 12081
7f2ae036 12082 if (pixmap_id > 0)
dbc4e1c1 12083 {
d2bd6bc4 12084 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12085 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12086 }
12087 else
68568555
RS
12088 {
12089 /* It seems there is no way to turn off use of an icon pixmap.
12090 The following line does it, only if no icon has yet been created,
12091 for some window managers. But with mwm it crashes.
12092 Some people say it should clear the IconPixmapHint bit in this case,
12093 but that doesn't work, and the X consortium said it isn't the
12094 right thing at all. Since there is no way to win,
12095 best to explicitly give up. */
12096#if 0
12097 f->output_data.x->wm_hints.icon_pixmap = None;
12098#else
12099 return;
12100#endif
12101 }
b1c884c3 12102
d2bd6bc4
RS
12103#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12104
12105 {
12106 Arg al[1];
12107 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12108 XtSetValues (f->output_data.x->widget, al, 1);
12109 }
12110
12111#else /* not USE_X_TOOLKIT */
12112
7556890b
RS
12113 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12114 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12115
12116#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12117}
12118
dfcf069d 12119void
f676886a
JB
12120x_wm_set_icon_position (f, icon_x, icon_y)
12121 struct frame *f;
dc6f92b8
JB
12122 int icon_x, icon_y;
12123{
75231bad 12124#ifdef USE_X_TOOLKIT
7556890b 12125 Window window = XtWindow (f->output_data.x->widget);
75231bad 12126#else
c118dd06 12127 Window window = FRAME_X_WINDOW (f);
75231bad 12128#endif
dc6f92b8 12129
7556890b
RS
12130 f->output_data.x->wm_hints.flags |= IconPositionHint;
12131 f->output_data.x->wm_hints.icon_x = icon_x;
12132 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12133
7556890b 12134 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12135}
12136
12137\f
06a2c219
GM
12138/***********************************************************************
12139 Fonts
12140 ***********************************************************************/
dc43ef94
KH
12141
12142/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12143
dc43ef94
KH
12144struct font_info *
12145x_get_font_info (f, font_idx)
12146 FRAME_PTR f;
12147 int font_idx;
12148{
12149 return (FRAME_X_FONT_TABLE (f) + font_idx);
12150}
12151
12152
12153/* Return a list of names of available fonts matching PATTERN on frame
12154 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12155 to be listed. Frame F NULL means we have not yet created any
12156 frame on X, and consult the first display in x_display_list.
12157 MAXNAMES sets a limit on how many fonts to match. */
12158
12159Lisp_Object
12160x_list_fonts (f, pattern, size, maxnames)
12161 FRAME_PTR f;
12162 Lisp_Object pattern;
12163 int size;
12164 int maxnames;
12165{
06a2c219
GM
12166 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12167 Lisp_Object tem, second_best;
dc43ef94 12168 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12169 int try_XLoadQueryFont = 0;
53ca4657 12170 int count;
dc43ef94 12171
6b0efe73 12172 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12173 if (NILP (patterns))
12174 patterns = Fcons (pattern, Qnil);
81ba44e5 12175
09c6077f
KH
12176 if (maxnames == 1 && !size)
12177 /* We can return any single font matching PATTERN. */
12178 try_XLoadQueryFont = 1;
9a32686f 12179
8e713be6 12180 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12181 {
dc43ef94
KH
12182 int num_fonts;
12183 char **names;
12184
8e713be6 12185 pattern = XCAR (patterns);
536f4067
RS
12186 /* See if we cached the result for this particular query.
12187 The cache is an alist of the form:
12188 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12189 */
8e713be6 12190 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12191 key = Fcons (pattern, make_number (maxnames)),
12192 !NILP (list = Fassoc (key, tem))))
12193 {
12194 list = Fcdr_safe (list);
12195 /* We have a cashed list. Don't have to get the list again. */
12196 goto label_cached;
12197 }
12198
12199 /* At first, put PATTERN in the cache. */
09c6077f 12200
dc43ef94 12201 BLOCK_INPUT;
17d85edc
KH
12202 count = x_catch_errors (dpy);
12203
09c6077f
KH
12204 if (try_XLoadQueryFont)
12205 {
12206 XFontStruct *font;
12207 unsigned long value;
12208
12209 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12210 if (x_had_errors_p (dpy))
12211 {
12212 /* This error is perhaps due to insufficient memory on X
12213 server. Let's just ignore it. */
12214 font = NULL;
12215 x_clear_errors (dpy);
12216 }
12217
09c6077f
KH
12218 if (font
12219 && XGetFontProperty (font, XA_FONT, &value))
12220 {
12221 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12222 int len = strlen (name);
01c752b5 12223 char *tmp;
09c6077f 12224
6f6512e8
KH
12225 /* If DXPC (a Differential X Protocol Compressor)
12226 Ver.3.7 is running, XGetAtomName will return null
12227 string. We must avoid such a name. */
12228 if (len == 0)
12229 try_XLoadQueryFont = 0;
12230 else
12231 {
12232 num_fonts = 1;
12233 names = (char **) alloca (sizeof (char *));
12234 /* Some systems only allow alloca assigned to a
12235 simple var. */
12236 tmp = (char *) alloca (len + 1); names[0] = tmp;
12237 bcopy (name, names[0], len + 1);
12238 XFree (name);
12239 }
09c6077f
KH
12240 }
12241 else
12242 try_XLoadQueryFont = 0;
a083fd23
RS
12243
12244 if (font)
12245 XFreeFont (dpy, font);
09c6077f
KH
12246 }
12247
12248 if (!try_XLoadQueryFont)
17d85edc
KH
12249 {
12250 /* We try at least 10 fonts because XListFonts will return
12251 auto-scaled fonts at the head. */
12252 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12253 &num_fonts);
12254 if (x_had_errors_p (dpy))
12255 {
12256 /* This error is perhaps due to insufficient memory on X
12257 server. Let's just ignore it. */
12258 names = NULL;
12259 x_clear_errors (dpy);
12260 }
12261 }
12262
12263 x_uncatch_errors (dpy, count);
dc43ef94
KH
12264 UNBLOCK_INPUT;
12265
12266 if (names)
12267 {
12268 int i;
dc43ef94
KH
12269
12270 /* Make a list of all the fonts we got back.
12271 Store that in the font cache for the display. */
12272 for (i = 0; i < num_fonts; i++)
12273 {
06a2c219 12274 int width = 0;
dc43ef94 12275 char *p = names[i];
06a2c219
GM
12276 int average_width = -1, dashes = 0;
12277
dc43ef94 12278 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12279 14 dashes, and the field value following 12th dash
12280 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12281 is usually too ugly to be used for editing. Let's
12282 ignore it. */
dc43ef94
KH
12283 while (*p)
12284 if (*p++ == '-')
12285 {
12286 dashes++;
12287 if (dashes == 7) /* PIXEL_SIZE field */
12288 width = atoi (p);
12289 else if (dashes == 12) /* AVERAGE_WIDTH field */
12290 average_width = atoi (p);
12291 }
12292 if (dashes < 14 || average_width != 0)
12293 {
12294 tem = build_string (names[i]);
12295 if (NILP (Fassoc (tem, list)))
12296 {
12297 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12298 && ((fast_c_string_match_ignore_case
12299 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12300 >= 0))
12301 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12302 width of this font. */
dc43ef94
KH
12303 list = Fcons (Fcons (tem, make_number (width)), list);
12304 else
12305 /* For the moment, width is not known. */
12306 list = Fcons (Fcons (tem, Qnil), list);
12307 }
12308 }
12309 }
09c6077f
KH
12310 if (!try_XLoadQueryFont)
12311 XFreeFontNames (names);
dc43ef94
KH
12312 }
12313
b5210ea7 12314 /* Now store the result in the cache. */
dc43ef94 12315 if (f != NULL)
8e713be6 12316 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12317 = Fcons (Fcons (key, list),
8e713be6 12318 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12319
b5210ea7
KH
12320 label_cached:
12321 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12322
b5210ea7
KH
12323 newlist = second_best = Qnil;
12324 /* Make a list of the fonts that have the right width. */
8e713be6 12325 for (; CONSP (list); list = XCDR (list))
b5210ea7 12326 {
536f4067
RS
12327 int found_size;
12328
8e713be6 12329 tem = XCAR (list);
dc43ef94 12330
8e713be6 12331 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12332 continue;
12333 if (!size)
12334 {
8e713be6 12335 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12336 continue;
12337 }
dc43ef94 12338
8e713be6 12339 if (!INTEGERP (XCDR (tem)))
dc43ef94 12340 {
b5210ea7
KH
12341 /* Since we have not yet known the size of this font, we
12342 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12343 XFontStruct *thisinfo;
12344
12345 BLOCK_INPUT;
17d85edc 12346 count = x_catch_errors (dpy);
dc43ef94 12347 thisinfo = XLoadQueryFont (dpy,
8e713be6 12348 XSTRING (XCAR (tem))->data);
17d85edc
KH
12349 if (x_had_errors_p (dpy))
12350 {
12351 /* This error is perhaps due to insufficient memory on X
12352 server. Let's just ignore it. */
12353 thisinfo = NULL;
12354 x_clear_errors (dpy);
12355 }
12356 x_uncatch_errors (dpy, count);
dc43ef94
KH
12357 UNBLOCK_INPUT;
12358
12359 if (thisinfo)
12360 {
8e713be6 12361 XCDR (tem)
536f4067
RS
12362 = (thisinfo->min_bounds.width == 0
12363 ? make_number (0)
12364 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12365 XFreeFont (dpy, thisinfo);
12366 }
12367 else
b5210ea7 12368 /* For unknown reason, the previous call of XListFont had
06a2c219 12369 returned a font which can't be opened. Record the size
b5210ea7 12370 as 0 not to try to open it again. */
8e713be6 12371 XCDR (tem) = make_number (0);
dc43ef94 12372 }
536f4067 12373
8e713be6 12374 found_size = XINT (XCDR (tem));
536f4067 12375 if (found_size == size)
8e713be6 12376 newlist = Fcons (XCAR (tem), newlist);
536f4067 12377 else if (found_size > 0)
b5210ea7 12378 {
536f4067 12379 if (NILP (second_best))
b5210ea7 12380 second_best = tem;
536f4067
RS
12381 else if (found_size < size)
12382 {
8e713be6
KR
12383 if (XINT (XCDR (second_best)) > size
12384 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12385 second_best = tem;
12386 }
12387 else
12388 {
8e713be6
KR
12389 if (XINT (XCDR (second_best)) > size
12390 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12391 second_best = tem;
12392 }
b5210ea7
KH
12393 }
12394 }
12395 if (!NILP (newlist))
12396 break;
12397 else if (!NILP (second_best))
12398 {
8e713be6 12399 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12400 break;
dc43ef94 12401 }
dc43ef94
KH
12402 }
12403
12404 return newlist;
12405}
12406
06a2c219
GM
12407
12408#if GLYPH_DEBUG
12409
12410/* Check that FONT is valid on frame F. It is if it can be found in F's
12411 font table. */
12412
12413static void
12414x_check_font (f, font)
12415 struct frame *f;
12416 XFontStruct *font;
12417{
12418 int i;
12419 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12420
12421 xassert (font != NULL);
12422
12423 for (i = 0; i < dpyinfo->n_fonts; i++)
12424 if (dpyinfo->font_table[i].name
12425 && font == dpyinfo->font_table[i].font)
12426 break;
12427
12428 xassert (i < dpyinfo->n_fonts);
12429}
12430
12431#endif /* GLYPH_DEBUG != 0 */
12432
12433/* Set *W to the minimum width, *H to the minimum font height of FONT.
12434 Note: There are (broken) X fonts out there with invalid XFontStruct
12435 min_bounds contents. For example, handa@etl.go.jp reports that
12436 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12437 have font->min_bounds.width == 0. */
12438
12439static INLINE void
12440x_font_min_bounds (font, w, h)
12441 XFontStruct *font;
12442 int *w, *h;
12443{
12444 *h = FONT_HEIGHT (font);
12445 *w = font->min_bounds.width;
12446
12447 /* Try to handle the case where FONT->min_bounds has invalid
12448 contents. Since the only font known to have invalid min_bounds
12449 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12450 if (*w <= 0)
12451 *w = font->max_bounds.width;
12452}
12453
12454
12455/* Compute the smallest character width and smallest font height over
12456 all fonts available on frame F. Set the members smallest_char_width
12457 and smallest_font_height in F's x_display_info structure to
12458 the values computed. Value is non-zero if smallest_font_height or
12459 smallest_char_width become smaller than they were before. */
12460
12461static int
12462x_compute_min_glyph_bounds (f)
12463 struct frame *f;
12464{
12465 int i;
12466 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12467 XFontStruct *font;
12468 int old_width = dpyinfo->smallest_char_width;
12469 int old_height = dpyinfo->smallest_font_height;
12470
12471 dpyinfo->smallest_font_height = 100000;
12472 dpyinfo->smallest_char_width = 100000;
12473
12474 for (i = 0; i < dpyinfo->n_fonts; ++i)
12475 if (dpyinfo->font_table[i].name)
12476 {
12477 struct font_info *fontp = dpyinfo->font_table + i;
12478 int w, h;
12479
12480 font = (XFontStruct *) fontp->font;
12481 xassert (font != (XFontStruct *) ~0);
12482 x_font_min_bounds (font, &w, &h);
12483
12484 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12485 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12486 }
12487
12488 xassert (dpyinfo->smallest_char_width > 0
12489 && dpyinfo->smallest_font_height > 0);
12490
12491 return (dpyinfo->n_fonts == 1
12492 || dpyinfo->smallest_char_width < old_width
12493 || dpyinfo->smallest_font_height < old_height);
12494}
12495
12496
dc43ef94
KH
12497/* Load font named FONTNAME of the size SIZE for frame F, and return a
12498 pointer to the structure font_info while allocating it dynamically.
12499 If SIZE is 0, load any size of font.
12500 If loading is failed, return NULL. */
12501
12502struct font_info *
12503x_load_font (f, fontname, size)
12504 struct frame *f;
12505 register char *fontname;
12506 int size;
12507{
12508 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12509 Lisp_Object font_names;
d645aaa4 12510 int count;
dc43ef94
KH
12511
12512 /* Get a list of all the fonts that match this name. Once we
12513 have a list of matching fonts, we compare them against the fonts
12514 we already have by comparing names. */
09c6077f 12515 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12516
12517 if (!NILP (font_names))
12518 {
12519 Lisp_Object tail;
12520 int i;
12521
12522 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12523 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12524 if (dpyinfo->font_table[i].name
12525 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12526 XSTRING (XCAR (tail))->data)
06a2c219 12527 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12528 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12529 return (dpyinfo->font_table + i);
12530 }
12531
12532 /* Load the font and add it to the table. */
12533 {
12534 char *full_name;
12535 XFontStruct *font;
12536 struct font_info *fontp;
12537 unsigned long value;
06a2c219 12538 int i;
dc43ef94 12539
2da424f1
KH
12540 /* If we have found fonts by x_list_font, load one of them. If
12541 not, we still try to load a font by the name given as FONTNAME
12542 because XListFonts (called in x_list_font) of some X server has
12543 a bug of not finding a font even if the font surely exists and
12544 is loadable by XLoadQueryFont. */
e1d6d5b9 12545 if (size > 0 && !NILP (font_names))
8e713be6 12546 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12547
12548 BLOCK_INPUT;
d645aaa4 12549 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12550 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12551 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12552 {
12553 /* This error is perhaps due to insufficient memory on X
12554 server. Let's just ignore it. */
12555 font = NULL;
12556 x_clear_errors (FRAME_X_DISPLAY (f));
12557 }
12558 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12559 UNBLOCK_INPUT;
b5210ea7 12560 if (!font)
dc43ef94
KH
12561 return NULL;
12562
06a2c219
GM
12563 /* Find a free slot in the font table. */
12564 for (i = 0; i < dpyinfo->n_fonts; ++i)
12565 if (dpyinfo->font_table[i].name == NULL)
12566 break;
12567
12568 /* If no free slot found, maybe enlarge the font table. */
12569 if (i == dpyinfo->n_fonts
12570 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12571 {
06a2c219
GM
12572 int sz;
12573 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12574 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12575 dpyinfo->font_table
06a2c219 12576 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12577 }
12578
06a2c219
GM
12579 fontp = dpyinfo->font_table + i;
12580 if (i == dpyinfo->n_fonts)
12581 ++dpyinfo->n_fonts;
dc43ef94
KH
12582
12583 /* Now fill in the slots of *FONTP. */
12584 BLOCK_INPUT;
12585 fontp->font = font;
06a2c219 12586 fontp->font_idx = i;
dc43ef94
KH
12587 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12588 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12589
12590 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12591 full_name = 0;
12592 if (XGetFontProperty (font, XA_FONT, &value))
12593 {
12594 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12595 char *p = name;
12596 int dashes = 0;
12597
12598 /* Count the number of dashes in the "full name".
12599 If it is too few, this isn't really the font's full name,
12600 so don't use it.
12601 In X11R4, the fonts did not come with their canonical names
12602 stored in them. */
12603 while (*p)
12604 {
12605 if (*p == '-')
12606 dashes++;
12607 p++;
12608 }
12609
12610 if (dashes >= 13)
12611 {
12612 full_name = (char *) xmalloc (p - name + 1);
12613 bcopy (name, full_name, p - name + 1);
12614 }
12615
12616 XFree (name);
12617 }
12618
12619 if (full_name != 0)
12620 fontp->full_name = full_name;
12621 else
12622 fontp->full_name = fontp->name;
12623
12624 fontp->size = font->max_bounds.width;
d5749adb
KH
12625 fontp->height = FONT_HEIGHT (font);
12626 {
12627 /* For some font, ascent and descent in max_bounds field is
12628 larger than the above value. */
12629 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12630 if (max_height > fontp->height)
74848a96 12631 fontp->height = max_height;
d5749adb 12632 }
dc43ef94 12633
2da424f1
KH
12634 if (NILP (font_names))
12635 {
12636 /* We come here because of a bug of XListFonts mentioned at
12637 the head of this block. Let's store this information in
12638 the cache for x_list_fonts. */
12639 Lisp_Object lispy_name = build_string (fontname);
12640 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12641
8e713be6 12642 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12643 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12644 Fcons (Fcons (lispy_full_name,
12645 make_number (fontp->size)),
12646 Qnil)),
8e713be6 12647 XCDR (dpyinfo->name_list_element));
2da424f1 12648 if (full_name)
8e713be6 12649 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12650 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12651 Fcons (Fcons (lispy_full_name,
12652 make_number (fontp->size)),
12653 Qnil)),
8e713be6 12654 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12655 }
12656
dc43ef94
KH
12657 /* The slot `encoding' specifies how to map a character
12658 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12659 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12660 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12661 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12662 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12663 which is never used by any charset. If mapping can't be
12664 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12665 fontp->encoding[1]
12666 = (font->max_byte1 == 0
12667 /* 1-byte font */
12668 ? (font->min_char_or_byte2 < 0x80
12669 ? (font->max_char_or_byte2 < 0x80
12670 ? 0 /* 0x20..0x7F */
8ff102bd 12671 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12672 : 1) /* 0xA0..0xFF */
12673 /* 2-byte font */
12674 : (font->min_byte1 < 0x80
12675 ? (font->max_byte1 < 0x80
12676 ? (font->min_char_or_byte2 < 0x80
12677 ? (font->max_char_or_byte2 < 0x80
12678 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12679 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12680 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12681 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12682 : (font->min_char_or_byte2 < 0x80
12683 ? (font->max_char_or_byte2 < 0x80
12684 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12685 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12686 : 1))); /* 0xA0A0..0xFFFF */
12687
12688 fontp->baseline_offset
12689 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12690 ? (long) value : 0);
12691 fontp->relative_compose
12692 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12693 ? (long) value : 0);
f78798df
KH
12694 fontp->default_ascent
12695 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12696 ? (long) value : 0);
dc43ef94 12697
06a2c219
GM
12698 /* Set global flag fonts_changed_p to non-zero if the font loaded
12699 has a character with a smaller width than any other character
12700 before, or if the font loaded has a smalle>r height than any
12701 other font loaded before. If this happens, it will make a
12702 glyph matrix reallocation necessary. */
12703 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12704 UNBLOCK_INPUT;
dc43ef94
KH
12705 return fontp;
12706 }
12707}
12708
06a2c219
GM
12709
12710/* Return a pointer to struct font_info of a font named FONTNAME for
12711 frame F. If no such font is loaded, return NULL. */
12712
dc43ef94
KH
12713struct font_info *
12714x_query_font (f, fontname)
12715 struct frame *f;
12716 register char *fontname;
12717{
12718 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12719 int i;
12720
12721 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12722 if (dpyinfo->font_table[i].name
12723 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12724 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12725 return (dpyinfo->font_table + i);
12726 return NULL;
12727}
12728
06a2c219
GM
12729
12730/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12731 `encoder' of the structure. */
12732
12733void
12734x_find_ccl_program (fontp)
12735 struct font_info *fontp;
12736{
a42f54e6 12737 Lisp_Object list, elt;
a6582676 12738
8e713be6 12739 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12740 {
8e713be6 12741 elt = XCAR (list);
a6582676 12742 if (CONSP (elt)
8e713be6
KR
12743 && STRINGP (XCAR (elt))
12744 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12745 >= 0))
a42f54e6
KH
12746 break;
12747 }
12748 if (! NILP (list))
12749 {
d27f8ca7
KH
12750 struct ccl_program *ccl
12751 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12752
8e713be6 12753 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12754 xfree (ccl);
12755 else
12756 fontp->font_encoder = ccl;
a6582676
KH
12757 }
12758}
12759
06a2c219 12760
dc43ef94 12761\f
06a2c219
GM
12762/***********************************************************************
12763 Initialization
12764 ***********************************************************************/
f451eb13 12765
3afe33e7
RS
12766#ifdef USE_X_TOOLKIT
12767static XrmOptionDescRec emacs_options[] = {
12768 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12769 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12770
12771 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12772 XrmoptionSepArg, NULL},
12773 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12774
12775 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12776 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12777 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12778 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12779 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12780 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12781 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12782};
12783#endif /* USE_X_TOOLKIT */
12784
7a13e894
RS
12785static int x_initialized;
12786
29b38361
KH
12787#ifdef MULTI_KBOARD
12788/* Test whether two display-name strings agree up to the dot that separates
12789 the screen number from the server number. */
12790static int
12791same_x_server (name1, name2)
12792 char *name1, *name2;
12793{
12794 int seen_colon = 0;
cf591cc1
RS
12795 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12796 int system_name_length = strlen (system_name);
12797 int length_until_period = 0;
12798
12799 while (system_name[length_until_period] != 0
12800 && system_name[length_until_period] != '.')
12801 length_until_period++;
12802
12803 /* Treat `unix' like an empty host name. */
12804 if (! strncmp (name1, "unix:", 5))
12805 name1 += 4;
12806 if (! strncmp (name2, "unix:", 5))
12807 name2 += 4;
12808 /* Treat this host's name like an empty host name. */
12809 if (! strncmp (name1, system_name, system_name_length)
12810 && name1[system_name_length] == ':')
12811 name1 += system_name_length;
12812 if (! strncmp (name2, system_name, system_name_length)
12813 && name2[system_name_length] == ':')
12814 name2 += system_name_length;
12815 /* Treat this host's domainless name like an empty host name. */
12816 if (! strncmp (name1, system_name, length_until_period)
12817 && name1[length_until_period] == ':')
12818 name1 += length_until_period;
12819 if (! strncmp (name2, system_name, length_until_period)
12820 && name2[length_until_period] == ':')
12821 name2 += length_until_period;
12822
29b38361
KH
12823 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12824 {
12825 if (*name1 == ':')
12826 seen_colon++;
12827 if (seen_colon && *name1 == '.')
12828 return 1;
12829 }
12830 return (seen_colon
12831 && (*name1 == '.' || *name1 == '\0')
12832 && (*name2 == '.' || *name2 == '\0'));
12833}
12834#endif
12835
334208b7 12836struct x_display_info *
1f8255f2 12837x_term_init (display_name, xrm_option, resource_name)
334208b7 12838 Lisp_Object display_name;
1f8255f2
RS
12839 char *xrm_option;
12840 char *resource_name;
dc6f92b8 12841{
334208b7 12842 int connection;
7a13e894 12843 Display *dpy;
334208b7
RS
12844 struct x_display_info *dpyinfo;
12845 XrmDatabase xrdb;
12846
60439948
KH
12847 BLOCK_INPUT;
12848
7a13e894
RS
12849 if (!x_initialized)
12850 {
12851 x_initialize ();
12852 x_initialized = 1;
12853 }
dc6f92b8 12854
3afe33e7 12855#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12856 /* weiner@footloose.sps.mot.com reports that this causes
12857 errors with X11R5:
12858 X protocol error: BadAtom (invalid Atom parameter)
12859 on protocol request 18skiloaf.
12860 So let's not use it until R6. */
12861#ifdef HAVE_X11XTR6
bdcd49ba
RS
12862 XtSetLanguageProc (NULL, NULL, NULL);
12863#endif
12864
7f9c7f94
RS
12865 {
12866 int argc = 0;
12867 char *argv[3];
12868
12869 argv[0] = "";
12870 argc = 1;
12871 if (xrm_option)
12872 {
12873 argv[argc++] = "-xrm";
12874 argv[argc++] = xrm_option;
12875 }
12876 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12877 resource_name, EMACS_CLASS,
12878 emacs_options, XtNumber (emacs_options),
12879 &argc, argv);
39d8bb4d
KH
12880
12881#ifdef HAVE_X11XTR6
10537cb1 12882 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12883 fixup_locale ();
39d8bb4d 12884#endif
7f9c7f94 12885 }
3afe33e7
RS
12886
12887#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12888#ifdef HAVE_X11R5
12889 XSetLocaleModifiers ("");
12890#endif
7a13e894 12891 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12892#endif /* not USE_X_TOOLKIT */
334208b7 12893
7a13e894
RS
12894 /* Detect failure. */
12895 if (dpy == 0)
60439948
KH
12896 {
12897 UNBLOCK_INPUT;
12898 return 0;
12899 }
7a13e894
RS
12900
12901 /* We have definitely succeeded. Record the new connection. */
12902
12903 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12904
29b38361
KH
12905#ifdef MULTI_KBOARD
12906 {
12907 struct x_display_info *share;
12908 Lisp_Object tail;
12909
12910 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12911 share = share->next, tail = XCDR (tail))
12912 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12913 XSTRING (display_name)->data))
12914 break;
12915 if (share)
12916 dpyinfo->kboard = share->kboard;
12917 else
12918 {
12919 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12920 init_kboard (dpyinfo->kboard);
59e755be
KH
12921 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12922 {
12923 char *vendor = ServerVendor (dpy);
9b6ed9f3 12924 UNBLOCK_INPUT;
59e755be
KH
12925 dpyinfo->kboard->Vsystem_key_alist
12926 = call1 (Qvendor_specific_keysyms,
12927 build_string (vendor ? vendor : ""));
9b6ed9f3 12928 BLOCK_INPUT;
59e755be
KH
12929 }
12930
29b38361
KH
12931 dpyinfo->kboard->next_kboard = all_kboards;
12932 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12933 /* Don't let the initial kboard remain current longer than necessary.
12934 That would cause problems if a file loaded on startup tries to
06a2c219 12935 prompt in the mini-buffer. */
0ad5446c
KH
12936 if (current_kboard == initial_kboard)
12937 current_kboard = dpyinfo->kboard;
29b38361
KH
12938 }
12939 dpyinfo->kboard->reference_count++;
12940 }
b9737ad3
KH
12941#endif
12942
7a13e894
RS
12943 /* Put this display on the chain. */
12944 dpyinfo->next = x_display_list;
12945 x_display_list = dpyinfo;
12946
12947 /* Put it on x_display_name_list as well, to keep them parallel. */
12948 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12949 x_display_name_list);
8e713be6 12950 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12951
12952 dpyinfo->display = dpy;
dc6f92b8 12953
dc6f92b8 12954#if 0
7a13e894 12955 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12956#endif /* ! 0 */
7a13e894
RS
12957
12958 dpyinfo->x_id_name
fc932ac6
RS
12959 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12960 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12961 + 2);
12962 sprintf (dpyinfo->x_id_name, "%s@%s",
12963 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12964
12965 /* Figure out which modifier bits mean what. */
334208b7 12966 x_find_modifier_meanings (dpyinfo);
f451eb13 12967
ab648270 12968 /* Get the scroll bar cursor. */
7a13e894 12969 dpyinfo->vertical_scroll_bar_cursor
334208b7 12970 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12971
334208b7
RS
12972 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12973 resource_name, EMACS_CLASS);
12974#ifdef HAVE_XRMSETDATABASE
12975 XrmSetDatabase (dpyinfo->display, xrdb);
12976#else
12977 dpyinfo->display->db = xrdb;
12978#endif
547d9db8 12979 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12980 all versions. */
12981 dpyinfo->xrdb = xrdb;
334208b7
RS
12982
12983 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12984 DefaultScreen (dpyinfo->display));
5ff67d81 12985 select_visual (dpyinfo);
43bd1b2b 12986 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
12987 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12988 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12989 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12990 dpyinfo->grabbed = 0;
12991 dpyinfo->reference_count = 0;
12992 dpyinfo->icon_bitmap_id = -1;
06a2c219 12993 dpyinfo->font_table = NULL;
7a13e894
RS
12994 dpyinfo->n_fonts = 0;
12995 dpyinfo->font_table_size = 0;
12996 dpyinfo->bitmaps = 0;
12997 dpyinfo->bitmaps_size = 0;
12998 dpyinfo->bitmaps_last = 0;
12999 dpyinfo->scratch_cursor_gc = 0;
13000 dpyinfo->mouse_face_mouse_frame = 0;
13001 dpyinfo->mouse_face_deferred_gc = 0;
13002 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13003 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13004 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13005 dpyinfo->mouse_face_window = Qnil;
13006 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13007 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13008 dpyinfo->x_focus_frame = 0;
13009 dpyinfo->x_focus_event_frame = 0;
13010 dpyinfo->x_highlight_frame = 0;
06a2c219 13011 dpyinfo->image_cache = make_image_cache ();
334208b7 13012
43bd1b2b 13013 /* See if a private colormap is requested. */
5ff67d81
GM
13014 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13015 {
13016 if (dpyinfo->visual->class == PseudoColor)
13017 {
13018 Lisp_Object value;
13019 value = display_x_get_resource (dpyinfo,
13020 build_string ("privateColormap"),
13021 build_string ("PrivateColormap"),
13022 Qnil, Qnil);
13023 if (STRINGP (value)
13024 && (!strcmp (XSTRING (value)->data, "true")
13025 || !strcmp (XSTRING (value)->data, "on")))
13026 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13027 }
43bd1b2b 13028 }
5ff67d81
GM
13029 else
13030 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13031 dpyinfo->visual, AllocNone);
43bd1b2b 13032
06a2c219
GM
13033 {
13034 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13035 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13036 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13037 dpyinfo->resy = pixels * 25.4 / mm;
13038 pixels = DisplayWidth (dpyinfo->display, screen_number);
13039 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13040 dpyinfo->resx = pixels * 25.4 / mm;
13041 }
13042
334208b7
RS
13043 dpyinfo->Xatom_wm_protocols
13044 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13045 dpyinfo->Xatom_wm_take_focus
13046 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13047 dpyinfo->Xatom_wm_save_yourself
13048 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13049 dpyinfo->Xatom_wm_delete_window
13050 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13051 dpyinfo->Xatom_wm_change_state
13052 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13053 dpyinfo->Xatom_wm_configure_denied
13054 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13055 dpyinfo->Xatom_wm_window_moved
13056 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13057 dpyinfo->Xatom_editres
13058 = XInternAtom (dpyinfo->display, "Editres", False);
13059 dpyinfo->Xatom_CLIPBOARD
13060 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13061 dpyinfo->Xatom_TIMESTAMP
13062 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13063 dpyinfo->Xatom_TEXT
13064 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13065 dpyinfo->Xatom_COMPOUND_TEXT
13066 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13067 dpyinfo->Xatom_DELETE
13068 = XInternAtom (dpyinfo->display, "DELETE", False);
13069 dpyinfo->Xatom_MULTIPLE
13070 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13071 dpyinfo->Xatom_INCR
13072 = XInternAtom (dpyinfo->display, "INCR", False);
13073 dpyinfo->Xatom_EMACS_TMP
13074 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13075 dpyinfo->Xatom_TARGETS
13076 = XInternAtom (dpyinfo->display, "TARGETS", False);
13077 dpyinfo->Xatom_NULL
13078 = XInternAtom (dpyinfo->display, "NULL", False);
13079 dpyinfo->Xatom_ATOM_PAIR
13080 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13081 /* For properties of font. */
13082 dpyinfo->Xatom_PIXEL_SIZE
13083 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13084 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13085 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13086 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13087 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13088 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13089 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13090
06a2c219
GM
13091 /* Ghostscript support. */
13092 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13093 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13094
13095 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13096 False);
13097
547d9db8
KH
13098 dpyinfo->cut_buffers_initialized = 0;
13099
334208b7
RS
13100 connection = ConnectionNumber (dpyinfo->display);
13101 dpyinfo->connection = connection;
13102
dc43ef94 13103 {
5d7cc324
RS
13104 char null_bits[1];
13105
13106 null_bits[0] = 0x00;
dc43ef94
KH
13107
13108 dpyinfo->null_pixel
13109 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13110 null_bits, 1, 1, (long) 0, (long) 0,
13111 1);
13112 }
13113
06a2c219
GM
13114 {
13115 extern int gray_bitmap_width, gray_bitmap_height;
13116 extern unsigned char *gray_bitmap_bits;
13117 dpyinfo->gray
13118 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13119 gray_bitmap_bits,
13120 gray_bitmap_width, gray_bitmap_height,
13121 (unsigned long) 1, (unsigned long) 0, 1);
13122 }
13123
f5d11644
GM
13124#ifdef HAVE_X_I18N
13125 xim_initialize (dpyinfo, resource_name);
13126#endif
13127
87485d6f
MW
13128#ifdef subprocesses
13129 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13130 if (connection != 0)
7a13e894 13131 add_keyboard_wait_descriptor (connection);
87485d6f 13132#endif
6d4238f3 13133
041b69ac 13134#ifndef F_SETOWN_BUG
dc6f92b8 13135#ifdef F_SETOWN
dc6f92b8 13136#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13137 /* stdin is a socket here */
334208b7 13138 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13139#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13140 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13141#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13142#endif /* ! defined (F_SETOWN) */
041b69ac 13143#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13144
13145#ifdef SIGIO
eee20f6a
KH
13146 if (interrupt_input)
13147 init_sigio (connection);
c118dd06 13148#endif /* ! defined (SIGIO) */
dc6f92b8 13149
51b592fb 13150#ifdef USE_LUCID
f8c39f51 13151#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13152 /* Make sure that we have a valid font for dialog boxes
13153 so that Xt does not crash. */
13154 {
13155 Display *dpy = dpyinfo->display;
13156 XrmValue d, fr, to;
13157 Font font;
e99db5a1 13158 int count;
51b592fb
RS
13159
13160 d.addr = (XPointer)&dpy;
13161 d.size = sizeof (Display *);
13162 fr.addr = XtDefaultFont;
13163 fr.size = sizeof (XtDefaultFont);
13164 to.size = sizeof (Font *);
13165 to.addr = (XPointer)&font;
e99db5a1 13166 count = x_catch_errors (dpy);
51b592fb
RS
13167 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13168 abort ();
13169 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13170 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13171 x_uncatch_errors (dpy, count);
51b592fb
RS
13172 }
13173#endif
f8c39f51 13174#endif
51b592fb 13175
34e23e5a
GM
13176 /* See if we should run in synchronous mode. This is useful
13177 for debugging X code. */
13178 {
13179 Lisp_Object value;
13180 value = display_x_get_resource (dpyinfo,
13181 build_string ("synchronous"),
13182 build_string ("Synchronous"),
13183 Qnil, Qnil);
13184 if (STRINGP (value)
13185 && (!strcmp (XSTRING (value)->data, "true")
13186 || !strcmp (XSTRING (value)->data, "on")))
13187 XSynchronize (dpyinfo->display, True);
13188 }
13189
60439948
KH
13190 UNBLOCK_INPUT;
13191
7a13e894
RS
13192 return dpyinfo;
13193}
13194\f
13195/* Get rid of display DPYINFO, assuming all frames are already gone,
13196 and without sending any more commands to the X server. */
dc6f92b8 13197
7a13e894
RS
13198void
13199x_delete_display (dpyinfo)
13200 struct x_display_info *dpyinfo;
13201{
13202 delete_keyboard_wait_descriptor (dpyinfo->connection);
13203
13204 /* Discard this display from x_display_name_list and x_display_list.
13205 We can't use Fdelq because that can quit. */
13206 if (! NILP (x_display_name_list)
8e713be6
KR
13207 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13208 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13209 else
13210 {
13211 Lisp_Object tail;
13212
13213 tail = x_display_name_list;
8e713be6 13214 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13215 {
bffcfca9 13216 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13217 {
8e713be6 13218 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13219 break;
13220 }
8e713be6 13221 tail = XCDR (tail);
7a13e894
RS
13222 }
13223 }
13224
9bda743f
GM
13225 if (next_noop_dpyinfo == dpyinfo)
13226 next_noop_dpyinfo = dpyinfo->next;
13227
7a13e894
RS
13228 if (x_display_list == dpyinfo)
13229 x_display_list = dpyinfo->next;
7f9c7f94
RS
13230 else
13231 {
13232 struct x_display_info *tail;
7a13e894 13233
7f9c7f94
RS
13234 for (tail = x_display_list; tail; tail = tail->next)
13235 if (tail->next == dpyinfo)
13236 tail->next = tail->next->next;
13237 }
7a13e894 13238
0d777288
RS
13239#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13240#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13241 XrmDestroyDatabase (dpyinfo->xrdb);
13242#endif
0d777288 13243#endif
29b38361
KH
13244#ifdef MULTI_KBOARD
13245 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13246 delete_kboard (dpyinfo->kboard);
b9737ad3 13247#endif
f5d11644
GM
13248#ifdef HAVE_X_I18N
13249 if (dpyinfo->xim)
13250 xim_close_dpy (dpyinfo);
13251#endif
13252
b9737ad3
KH
13253 xfree (dpyinfo->font_table);
13254 xfree (dpyinfo->x_id_name);
13255 xfree (dpyinfo);
7a13e894
RS
13256}
13257\f
13258/* Set up use of X before we make the first connection. */
13259
06a2c219
GM
13260static struct redisplay_interface x_redisplay_interface =
13261{
13262 x_produce_glyphs,
13263 x_write_glyphs,
13264 x_insert_glyphs,
13265 x_clear_end_of_line,
13266 x_scroll_run,
13267 x_after_update_window_line,
13268 x_update_window_begin,
13269 x_update_window_end,
13270 XTcursor_to,
13271 x_flush,
66ac4b0e
GM
13272 x_get_glyph_overhangs,
13273 x_fix_overlapping_area
06a2c219
GM
13274};
13275
dfcf069d 13276void
7a13e894
RS
13277x_initialize ()
13278{
06a2c219
GM
13279 rif = &x_redisplay_interface;
13280
13281 clear_frame_hook = x_clear_frame;
13282 ins_del_lines_hook = x_ins_del_lines;
13283 change_line_highlight_hook = x_change_line_highlight;
13284 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13285 ring_bell_hook = XTring_bell;
13286 reset_terminal_modes_hook = XTreset_terminal_modes;
13287 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13288 update_begin_hook = x_update_begin;
13289 update_end_hook = x_update_end;
dc6f92b8
JB
13290 set_terminal_window_hook = XTset_terminal_window;
13291 read_socket_hook = XTread_socket;
b8009dd1 13292 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13293 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13294 mouse_position_hook = XTmouse_position;
f451eb13 13295 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13296 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13297 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13298 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13299 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13300 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13301 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13302
f676886a 13303 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13304 char_ins_del_ok = 0; /* just as fast to write the line */
13305 line_ins_del_ok = 1; /* we'll just blt 'em */
13306 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13307 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13308 off the bottom */
13309 baud_rate = 19200;
13310
7a13e894 13311 x_noop_count = 0;
9ea173e8 13312 last_tool_bar_item = -1;
06a2c219
GM
13313 any_help_event_p = 0;
13314
b30b24cb
RS
13315 /* Try to use interrupt input; if we can't, then start polling. */
13316 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13317
7f9c7f94
RS
13318#ifdef USE_X_TOOLKIT
13319 XtToolkitInitialize ();
13320 Xt_app_con = XtCreateApplicationContext ();
665881ad 13321 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13322
13323 /* Install an asynchronous timer that processes Xt timeout events
13324 every 0.1s. This is necessary because some widget sets use
13325 timeouts internally, for example the LessTif menu bar, or the
13326 Xaw3d scroll bar. When Xt timouts aren't processed, these
13327 widgets don't behave normally. */
13328 {
13329 EMACS_TIME interval;
13330 EMACS_SET_SECS_USECS (interval, 0, 100000);
13331 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13332 }
db74249b 13333#endif
bffcfca9 13334
db74249b 13335#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13336 xaw3d_arrow_scroll = False;
13337 xaw3d_pick_top = True;
7f9c7f94
RS
13338#endif
13339
58769bee 13340 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13341 original error handler. */
e99db5a1 13342 XSetErrorHandler (x_error_handler);
334208b7 13343 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13344
06a2c219 13345 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13346#ifdef SIGWINCH
13347 signal (SIGWINCH, SIG_DFL);
c118dd06 13348#endif /* ! defined (SIGWINCH) */
dc6f92b8 13349
92e2441b 13350 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13351}
55123275 13352
06a2c219 13353
55123275
JB
13354void
13355syms_of_xterm ()
13356{
e99db5a1
RS
13357 staticpro (&x_error_message_string);
13358 x_error_message_string = Qnil;
13359
7a13e894
RS
13360 staticpro (&x_display_name_list);
13361 x_display_name_list = Qnil;
334208b7 13362
ab648270 13363 staticpro (&last_mouse_scroll_bar);
e53cb100 13364 last_mouse_scroll_bar = Qnil;
59e755be
KH
13365
13366 staticpro (&Qvendor_specific_keysyms);
13367 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13368
13369 staticpro (&last_mouse_press_frame);
13370 last_mouse_press_frame = Qnil;
06a2c219
GM
13371
13372 staticpro (&help_echo);
13373 help_echo = Qnil;
13374 staticpro (&previous_help_echo);
13375 previous_help_echo = Qnil;
13376
13377 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13378 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13379For example, if a block cursor is over a tab, it will be drawn as\n\
13380wide as that tab on the display.");
13381 x_stretch_cursor_p = 0;
13382
13383 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13384 "If not nil, Emacs uses toolkit scroll bars.");
13385#if USE_TOOLKIT_SCROLL_BARS
13386 x_toolkit_scroll_bars_p = 1;
13387#else
13388 x_toolkit_scroll_bars_p = 0;
13389#endif
13390
06a2c219
GM
13391 staticpro (&last_mouse_motion_frame);
13392 last_mouse_motion_frame = Qnil;
55123275 13393}
6cf0ae86
RS
13394
13395#endif /* not HAVE_X_WINDOWS */
06a2c219 13396