*** empty log message ***
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
06a2c219
GM
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999
3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8 46
16bd92ea 47#ifndef USG
dc6f92b8
JB
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06
JB
52#endif /* makedev */
53#endif /* USG */
dc6f92b8 54
6df54671 55#ifdef BSD_SYSTEM
dc6f92b8 56#include <sys/ioctl.h>
6df54671 57#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 58
2d368234 59#include "systty.h"
3a2712f9 60#include "systime.h"
dc6f92b8 61
b8009dd1 62#ifndef INCLUDED_FCNTL
dc6f92b8 63#include <fcntl.h>
b8009dd1 64#endif
dc6f92b8
JB
65#include <ctype.h>
66#include <errno.h>
67#include <setjmp.h>
68#include <sys/stat.h>
a0a7635f
RS
69/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70/* #include <sys/param.h> */
dc6f92b8 71
dc43ef94
KH
72#include "charset.h"
73#include "ccl.h"
7a13e894 74#include "frame.h"
a1dfb88a 75#include "fontset.h"
dc6f92b8
JB
76#include "dispextern.h"
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 *));
455static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
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 *,
1097 XChar2b *));
1098static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1099 int, XChar2b *, int));
1100static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1101static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1102static void x_append_glyph P_ ((struct it *));
b4192550 1103static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1104static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1105 int, int, double));
1106static void x_produce_glyphs P_ ((struct it *));
06a2c219
GM
1107static void x_produce_image_glyph P_ ((struct it *it));
1108
1109
1110/* Return a pointer to per-char metric information in FONT of a
1111 character pointed by B which is a pointer to an XChar2b. */
dc43ef94
KH
1112
1113#define PER_CHAR_METRIC(font, b) \
1114 ((font)->per_char \
1115 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1116 + (((font)->min_byte1 || (font)->max_byte1) \
1117 ? (((b)->byte1 - (font)->min_byte1) \
1118 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1119 : 0)) \
1120 : &((font)->max_bounds))
1121
dc6f92b8 1122
06a2c219
GM
1123/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1124 If CHAR2B is not contained in FONT, the font's default character
1125 metric is returned. */
dc43ef94 1126
06a2c219
GM
1127static INLINE XCharStruct *
1128x_per_char_metric (font, char2b)
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
4a4dc352 1189
06a2c219
GM
1190 if (pcm == NULL || pcm->width == 0)
1191 {
1192 /* Character not contained in the font. FONT->default_char
1193 gives the character that will be printed. FONT->default_char
1194 is a 16-bit character code with byte1 in the most significant
1195 byte and byte2 in the least significant byte. */
1196 XChar2b default_char;
1197 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1198 default_char.byte2 = font->default_char & 0xff;
1199
1200 /* Avoid an endless recursion if FONT->default_char itself
1201 hasn't per char metrics. handa@etl.go.jp reports that some
1202 fonts have this problem. */
1203 if (default_char.byte1 != char2b->byte1
1204 || default_char.byte2 != char2b->byte2)
1205 pcm = x_per_char_metric (font, &default_char);
1206 else
1207 pcm = &font->max_bounds;
1208 }
1209
1210 return pcm;
1211}
b73b6aaf 1212
57b03282 1213
06a2c219
GM
1214/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1215 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1216
06a2c219
GM
1217static INLINE void
1218x_encode_char (c, char2b, font_info)
1219 int c;
1220 XChar2b *char2b;
1221 struct font_info *font_info;
1222{
1223 int charset = CHAR_CHARSET (c);
1224 XFontStruct *font = font_info->font;
1225
1226 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1227 This may be either a program in a special encoder language or a
1228 fixed encoding. */
1229 if (font_info->font_encoder)
1230 {
1231 /* It's a program. */
1232 struct ccl_program *ccl = font_info->font_encoder;
1233
1234 if (CHARSET_DIMENSION (charset) == 1)
1235 {
1236 ccl->reg[0] = charset;
1237 ccl->reg[1] = char2b->byte2;
1238 }
1239 else
1240 {
1241 ccl->reg[0] = charset;
1242 ccl->reg[1] = char2b->byte1;
1243 ccl->reg[2] = char2b->byte2;
1244 }
1245
1246 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1247
1248 /* We assume that MSBs are appropriately set/reset by CCL
1249 program. */
1250 if (font->max_byte1 == 0) /* 1-byte font */
1251 char2b->byte2 = ccl->reg[1];
1252 else
1253 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1254 }
1255 else if (font_info->encoding[charset])
1256 {
1257 /* Fixed encoding scheme. See fontset.h for the meaning of the
1258 encoding numbers. */
1259 int enc = font_info->encoding[charset];
1260
1261 if ((enc == 1 || enc == 2)
1262 && CHARSET_DIMENSION (charset) == 2)
1263 char2b->byte1 |= 0x80;
1264
1265 if (enc == 1 || enc == 3)
1266 char2b->byte2 |= 0x80;
1267 }
1268}
1269
1270
1271/* Get face and two-byte form of character C in face FACE_ID on frame
1272 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1273 means we want to display multibyte text. Value is a pointer to a
1274 realized face that is ready for display. */
1275
1276static INLINE struct face *
1277x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1278 struct frame *f;
1279 int c, face_id;
1280 XChar2b *char2b;
1281 int multibyte_p;
1282{
1283 struct face *face = FACE_FROM_ID (f, face_id);
1284
1285 if (!multibyte_p)
1286 {
1287 /* Unibyte case. We don't have to encode, but we have to make
1288 sure to use a face suitable for unibyte. */
1289 char2b->byte1 = 0;
1290 char2b->byte2 = c;
1291
1292 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1293 {
1294 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1295 face = FACE_FROM_ID (f, face_id);
1296 }
1297 }
1298 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1299 {
1300 /* Case of ASCII in a face known to fit ASCII. */
1301 char2b->byte1 = 0;
1302 char2b->byte2 = c;
1303 }
1304 else
1305 {
1306 int c1, c2, charset;
1307
1308 /* Split characters into bytes. If c2 is -1 afterwards, C is
1309 really a one-byte character so that byte1 is zero. */
1310 SPLIT_CHAR (c, charset, c1, c2);
1311 if (c2 > 0)
1312 char2b->byte1 = c1, char2b->byte2 = c2;
1313 else
1314 char2b->byte1 = 0, char2b->byte2 = c1;
1315
1316 /* Get the face for displaying C. If `face' is not suitable for
1317 charset, get the one that fits. (This can happen for the
b4192550
KH
1318 translations of a composition where the glyph
1319 specifies a face for the first component, but the other
1320 components have a different charset.) */
06a2c219
GM
1321 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1322 {
1323 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1324 face = FACE_FROM_ID (f, face_id);
1325 }
1326
1327 /* Maybe encode the character in *CHAR2B. */
1328 if (charset != CHARSET_ASCII)
1329 {
1330 struct font_info *font_info
1331 = FONT_INFO_FROM_ID (f, face->font_info_id);
1332 if (font_info)
1333 {
1334 x_encode_char (c, char2b, font_info);
1335 if (charset == charset_latin_iso8859_1)
1336 {
1337 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1338 >= 0x80);
1339 char2b->byte2 |= 0x80;
1340 }
1341 }
1342 }
1343 }
1344
1345 /* Make sure X resources of the face are allocated. */
1346 xassert (face != NULL);
1347 PREPARE_FACE_FOR_DISPLAY (f, face);
1348
1349 return face;
1350}
1351
1352
1353/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1354 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1355 a pointer to a realized face that is ready for display. */
1356
1357static INLINE struct face *
1358x_get_glyph_face_and_encoding (f, glyph, char2b)
1359 struct frame *f;
1360 struct glyph *glyph;
1361 XChar2b *char2b;
1362{
1363 struct face *face;
1364
1365 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1366 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219
GM
1367
1368 if (!glyph->multibyte_p)
1369 {
1370 /* Unibyte case. We don't have to encode, but we have to make
1371 sure to use a face suitable for unibyte. */
1372 char2b->byte1 = 0;
43d120d8 1373 char2b->byte2 = glyph->u.ch;
06a2c219 1374 }
43d120d8
KH
1375 else if (glyph->u.ch < 128
1376 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1377 {
1378 /* Case of ASCII in a face known to fit ASCII. */
1379 char2b->byte1 = 0;
43d120d8 1380 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1381 }
1382 else
1383 {
1384 int c1, c2, charset;
1385
1386 /* Split characters into bytes. If c2 is -1 afterwards, C is
1387 really a one-byte character so that byte1 is zero. */
43d120d8 1388 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1389 if (c2 > 0)
1390 char2b->byte1 = c1, char2b->byte2 = c2;
1391 else
1392 char2b->byte1 = 0, char2b->byte2 = c1;
1393
1394 /* Maybe encode the character in *CHAR2B. */
1395 if (charset != CHARSET_ASCII)
1396 {
1397 struct font_info *font_info
1398 = FONT_INFO_FROM_ID (f, face->font_info_id);
1399 if (font_info)
1400 {
43d120d8 1401 x_encode_char (glyph->u.ch, char2b, font_info);
06a2c219
GM
1402 if (charset == charset_latin_iso8859_1)
1403 char2b->byte2 |= 0x80;
1404 }
1405 }
1406 }
1407
1408 /* Make sure X resources of the face are allocated. */
1409 xassert (face != NULL);
1410 PREPARE_FACE_FOR_DISPLAY (f, face);
1411 return face;
1412}
1413
1414
1415/* Store one glyph for IT->char_to_display in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418static INLINE void
1419x_append_glyph (it)
1420 struct it *it;
1421{
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
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 = CHAR_GLYPH;
1437 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1438 glyph->u.ch = it->char_to_display;
1439 glyph->face_id = it->face_id;
06a2c219
GM
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;
66ac4b0e
GM
1446 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1447 || it->phys_descent > it->descent);
06a2c219
GM
1448 ++it->glyph_row->used[area];
1449 }
1450}
1451
b4192550
KH
1452/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1453 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1454
1455static INLINE void
1456x_append_composite_glyph (it)
1457 struct it *it;
1458{
1459 struct glyph *glyph;
1460 enum glyph_row_area area = it->area;
1461
1462 xassert (it->glyph_row);
1463
1464 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1465 if (glyph < it->glyph_row->glyphs[area + 1])
1466 {
1467 /* Play it safe. If sub-structures of the glyph are not all the
1468 same size, it otherwise be that some bits stay set. This
1469 would prevent a comparison with GLYPH_EQUAL_P. */
1470 glyph->u.val = 0;
1471
1472 glyph->type = COMPOSITE_GLYPH;
1473 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1474 glyph->u.cmp_id = it->cmp_id;
1475 glyph->face_id = it->face_id;
b4192550
KH
1476 glyph->charpos = CHARPOS (it->position);
1477 glyph->object = it->object;
1478 glyph->left_box_line_p = it->start_of_box_run_p;
1479 glyph->right_box_line_p = it->end_of_box_run_p;
1480 glyph->voffset = it->voffset;
1481 glyph->multibyte_p = it->multibyte_p;
1482 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1483 || it->phys_descent > it->descent);
1484 ++it->glyph_row->used[area];
1485 }
1486}
1487
06a2c219
GM
1488
1489/* Change IT->ascent and IT->height according to the setting of
1490 IT->voffset. */
1491
1492static INLINE void
1493take_vertical_position_into_account (it)
1494 struct it *it;
1495{
1496 if (it->voffset)
1497 {
1498 if (it->voffset < 0)
1499 /* Increase the ascent so that we can display the text higher
1500 in the line. */
1501 it->ascent += abs (it->voffset);
1502 else
1503 /* Increase the descent so that we can display the text lower
1504 in the line. */
1505 it->descent += it->voffset;
1506 }
1507}
1508
1509
1510/* Produce glyphs/get display metrics for the image IT is loaded with.
1511 See the description of struct display_iterator in dispextern.h for
1512 an overview of struct display_iterator. */
1513
1514static void
1515x_produce_image_glyph (it)
1516 struct it *it;
1517{
1518 struct image *img;
1519 struct face *face;
1520
1521 xassert (it->what == IT_IMAGE);
1522
1523 face = FACE_FROM_ID (it->f, it->face_id);
1524 img = IMAGE_FROM_ID (it->f, it->image_id);
1525 xassert (img);
1526
1527 /* Make sure X resources of the face and image are loaded. */
1528 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1529 prepare_image_for_display (it->f, img);
1530
66ac4b0e
GM
1531 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1532 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1533 it->pixel_width = img->width + 2 * img->margin;
1534
1535 it->nglyphs = 1;
1536
1537 if (face->box != FACE_NO_BOX)
1538 {
1539 it->ascent += face->box_line_width;
1540 it->descent += face->box_line_width;
1541
1542 if (it->start_of_box_run_p)
1543 it->pixel_width += face->box_line_width;
1544 if (it->end_of_box_run_p)
1545 it->pixel_width += face->box_line_width;
1546 }
1547
1548 take_vertical_position_into_account (it);
1549
1550 if (it->glyph_row)
1551 {
1552 struct glyph *glyph;
1553 enum glyph_row_area area = it->area;
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 = IMAGE_GLYPH;
43d120d8
KH
1559 glyph->u.img_id = img->id;
1560 glyph->face_id = it->face_id;
06a2c219
GM
1561 glyph->pixel_width = it->pixel_width;
1562 glyph->charpos = CHARPOS (it->position);
1563 glyph->object = it->object;
1564 glyph->left_box_line_p = it->start_of_box_run_p;
1565 glyph->right_box_line_p = it->end_of_box_run_p;
1566 glyph->voffset = it->voffset;
1567 glyph->multibyte_p = it->multibyte_p;
1568 ++it->glyph_row->used[area];
1569 }
1570 }
1571}
1572
1573
1574/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1575 of the glyph, WIDTH and HEIGHT are the width and height of the
1576 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1577 ascent of the glyph (0 <= ASCENT <= 1). */
1578
1579static void
1580x_append_stretch_glyph (it, object, width, height, ascent)
1581 struct it *it;
1582 Lisp_Object object;
1583 int width, height;
1584 double ascent;
1585{
1586 struct glyph *glyph;
1587 enum glyph_row_area area = it->area;
1588
1589 xassert (ascent >= 0 && ascent <= 1);
1590
1591 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1592 if (glyph < it->glyph_row->glyphs[area + 1])
1593 {
1594 glyph->type = STRETCH_GLYPH;
1595 glyph->u.stretch.ascent = height * ascent;
1596 glyph->u.stretch.height = height;
43d120d8 1597 glyph->face_id = it->face_id;
06a2c219
GM
1598 glyph->pixel_width = width;
1599 glyph->charpos = CHARPOS (it->position);
1600 glyph->object = object;
1601 glyph->left_box_line_p = it->start_of_box_run_p;
1602 glyph->right_box_line_p = it->end_of_box_run_p;
1603 glyph->voffset = it->voffset;
1604 glyph->multibyte_p = it->multibyte_p;
1605 ++it->glyph_row->used[area];
1606 }
1607}
1608
1609
1610/* Produce a stretch glyph for iterator IT. IT->object is the value
1611 of the glyph property displayed. The value must be a list
1612 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1613 being recognized:
1614
1615 1. `:width WIDTH' specifies that the space should be WIDTH *
1616 canonical char width wide. WIDTH may be an integer or floating
1617 point number.
1618
1619 2. `:relative-width FACTOR' specifies that the width of the stretch
1620 should be computed from the width of the first character having the
1621 `glyph' property, and should be FACTOR times that width.
1622
1623 3. `:align-to HPOS' specifies that the space should be wide enough
1624 to reach HPOS, a value in canonical character units.
1625
1626 Exactly one of the above pairs must be present.
1627
1628 4. `:height HEIGHT' specifies that the height of the stretch produced
1629 should be HEIGHT, measured in canonical character units.
1630
1631 5. `:relative-height FACTOR' specifies that the height of the the
1632 stretch should be FACTOR times the height of the characters having
1633 the glyph property.
1634
1635 Either none or exactly one of 4 or 5 must be present.
1636
1637 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1638 of the stretch should be used for the ascent of the stretch.
1639 ASCENT must be in the range 0 <= ASCENT <= 100. */
1640
1641#define NUMVAL(X) \
1642 ((INTEGERP (X) || FLOATP (X)) \
1643 ? XFLOATINT (X) \
1644 : - 1)
1645
1646
1647static void
1648x_produce_stretch_glyph (it)
1649 struct it *it;
1650{
1651 /* (space :width WIDTH :height HEIGHT. */
1652 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1653 extern Lisp_Object QCrelative_width, QCrelative_height;
1654 extern Lisp_Object QCalign_to;
1655 Lisp_Object prop, plist;
1656 double width = 0, height = 0, ascent = 0;
1657 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1658 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1659
1660 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1661
1662 /* List should start with `space'. */
1663 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1664 plist = XCDR (it->object);
1665
1666 /* Compute the width of the stretch. */
1667 if (prop = Fplist_get (plist, QCwidth),
1668 NUMVAL (prop) > 0)
1669 /* Absolute width `:width WIDTH' specified and valid. */
1670 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1671 else if (prop = Fplist_get (plist, QCrelative_width),
1672 NUMVAL (prop) > 0)
1673 {
1674 /* Relative width `:relative-width FACTOR' specified and valid.
1675 Compute the width of the characters having the `glyph'
1676 property. */
1677 struct it it2;
1678 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1679
1680 it2 = *it;
1681 if (it->multibyte_p)
1682 {
1683 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1684 - IT_BYTEPOS (*it));
1685 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1686 }
1687 else
1688 it2.c = *p, it2.len = 1;
1689
1690 it2.glyph_row = NULL;
1691 it2.what = IT_CHARACTER;
1692 x_produce_glyphs (&it2);
1693 width = NUMVAL (prop) * it2.pixel_width;
1694 }
1695 else if (prop = Fplist_get (plist, QCalign_to),
1696 NUMVAL (prop) > 0)
1697 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1698 else
1699 /* Nothing specified -> width defaults to canonical char width. */
1700 width = CANON_X_UNIT (it->f);
1701
1702 /* Compute height. */
1703 if (prop = Fplist_get (plist, QCheight),
1704 NUMVAL (prop) > 0)
1705 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1706 else if (prop = Fplist_get (plist, QCrelative_height),
1707 NUMVAL (prop) > 0)
1708 height = FONT_HEIGHT (font) * NUMVAL (prop);
1709 else
1710 height = FONT_HEIGHT (font);
1711
1712 /* Compute percentage of height used for ascent. If
1713 `:ascent ASCENT' is present and valid, use that. Otherwise,
1714 derive the ascent from the font in use. */
1715 if (prop = Fplist_get (plist, QCascent),
1716 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1717 ascent = NUMVAL (prop) / 100.0;
1718 else
1719 ascent = (double) font->ascent / FONT_HEIGHT (font);
1720
1721 if (width <= 0)
1722 width = 1;
1723 if (height <= 0)
1724 height = 1;
1725
1726 if (it->glyph_row)
1727 {
1728 Lisp_Object object = it->stack[it->sp - 1].string;
1729 if (!STRINGP (object))
1730 object = it->w->buffer;
1731 x_append_stretch_glyph (it, object, width, height, ascent);
1732 }
1733
1734 it->pixel_width = width;
66ac4b0e
GM
1735 it->ascent = it->phys_ascent = height * ascent;
1736 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1737 it->nglyphs = 1;
1738
1739 if (face->box != FACE_NO_BOX)
1740 {
1741 it->ascent += face->box_line_width;
1742 it->descent += face->box_line_width;
1743
1744 if (it->start_of_box_run_p)
1745 it->pixel_width += face->box_line_width;
1746 if (it->end_of_box_run_p)
1747 it->pixel_width += face->box_line_width;
1748 }
1749
1750 take_vertical_position_into_account (it);
1751}
1752
b4192550
KH
1753/* Return proper value to be used as baseline offset of font that has
1754 ASCENT and DESCENT to draw characters by the font at the vertical
1755 center of the line of frame F.
1756
1757 Here, out task is to find the value of BOFF in the following figure;
1758
1759 -------------------------+-----------+-
1760 -+-+---------+-+ | |
1761 | | | | | |
1762 | | | | F_ASCENT F_HEIGHT
1763 | | | ASCENT | |
1764 HEIGHT | | | | |
1765 | | |-|-+------+-----------|------- baseline
1766 | | | | BOFF | |
1767 | |---------|-+-+ | |
1768 | | | DESCENT | |
1769 -+-+---------+-+ F_DESCENT |
1770 -------------------------+-----------+-
1771
1772 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1773 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1774 DESCENT = FONT->descent
1775 HEIGHT = FONT_HEIGHT (FONT)
1776 F_DESCENT = (F->output_data.x->font->descent
1777 - F->output_data.x->baseline_offset)
1778 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1779*/
1780
1781#define VCENTER_BASELINE_OFFSET(FONT, F) \
1782 ((FONT)->descent \
1783 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1784 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1785
1786/* Produce glyphs/get display metrics for the display element IT is
1787 loaded with. See the description of struct display_iterator in
1788 dispextern.h for an overview of struct display_iterator. */
1789
1790static void
1791x_produce_glyphs (it)
1792 struct it *it;
1793{
1794 if (it->what == IT_CHARACTER)
1795 {
1796 XChar2b char2b;
1797 XFontStruct *font;
1798 struct face *face;
1799 XCharStruct *pcm;
06a2c219 1800 int font_not_found_p;
b4192550
KH
1801 struct font_info *font_info;
1802 int boff; /* baseline offset */
06a2c219
GM
1803
1804 /* Maybe translate single-byte characters to multibyte. */
1805 it->char_to_display = it->c;
1806 if (unibyte_display_via_language_environment
1807 && SINGLE_BYTE_CHAR_P (it->c)
1808 && (it->c >= 0240
1809 || (it->c >= 0200
1810 && !NILP (Vnonascii_translation_table))))
1811 {
1812 it->char_to_display = unibyte_char_to_multibyte (it->c);
1813 it->charset = CHAR_CHARSET (it->char_to_display);
1814 }
1815
1816 /* Get face and font to use. Encode IT->char_to_display. */
1817 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1818 it->face_id, &char2b,
1819 it->multibyte_p);
1820 font = face->font;
1821
1822 /* When no suitable font found, use the default font. */
1823 font_not_found_p = font == NULL;
1824 if (font_not_found_p)
b4192550
KH
1825 {
1826 font = FRAME_FONT (it->f);
1827 boff = it->f->output_data.x->baseline_offset;
1828 font_info = NULL;
1829 }
1830 else
1831 {
1832 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1833 boff = font_info->baseline_offset;
1834 if (font_info->vertical_centering)
1835 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1836 }
06a2c219
GM
1837
1838 if (it->char_to_display >= ' '
1839 && (!it->multibyte_p || it->char_to_display < 128))
1840 {
1841 /* Either unibyte or ASCII. */
1842 int stretched_p;
1843
1844 it->nglyphs = 1;
06a2c219
GM
1845
1846 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1847 it->ascent = font->ascent + boff;
1848 it->descent = font->descent - boff;
1849 it->phys_ascent = pcm->ascent + boff;
1850 it->phys_descent = pcm->descent - boff;
06a2c219
GM
1851 it->pixel_width = pcm->width;
1852
1853 /* If this is a space inside a region of text with
1854 `space-width' property, change its width. */
1855 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1856 if (stretched_p)
1857 it->pixel_width *= XFLOATINT (it->space_width);
1858
1859 /* If face has a box, add the box thickness to the character
1860 height. If character has a box line to the left and/or
1861 right, add the box line width to the character's width. */
1862 if (face->box != FACE_NO_BOX)
1863 {
1864 int thick = face->box_line_width;
1865
1866 it->ascent += thick;
1867 it->descent += thick;
1868
1869 if (it->start_of_box_run_p)
1870 it->pixel_width += thick;
1871 if (it->end_of_box_run_p)
1872 it->pixel_width += thick;
1873 }
1874
1875 /* If face has an overline, add the height of the overline
1876 (1 pixel) and a 1 pixel margin to the character height. */
1877 if (face->overline_p)
1878 it->ascent += 2;
1879
1880 take_vertical_position_into_account (it);
1881
1882 /* If we have to actually produce glyphs, do it. */
1883 if (it->glyph_row)
1884 {
1885 if (stretched_p)
1886 {
1887 /* Translate a space with a `space-width' property
1888 into a stretch glyph. */
1889 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1890 x_append_stretch_glyph (it, it->object, it->pixel_width,
1891 it->ascent + it->descent, ascent);
1892 }
1893 else
1894 x_append_glyph (it);
1895
1896 /* If characters with lbearing or rbearing are displayed
1897 in this line, record that fact in a flag of the
1898 glyph row. This is used to optimize X output code. */
1899 if (pcm->lbearing < 0
1900 || pcm->rbearing > pcm->width)
1901 it->glyph_row->contains_overlapping_glyphs_p = 1;
1902 }
1903 }
1904 else if (it->char_to_display == '\n')
1905 {
1906 /* A newline has no width but we need the height of the line. */
1907 it->pixel_width = 0;
1908 it->nglyphs = 0;
b4192550
KH
1909 it->ascent = it->phys_ascent = font->ascent + boff;
1910 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1911
1912 if (face->box != FACE_NO_BOX)
1913 {
1914 int thick = face->box_line_width;
1915 it->ascent += thick;
1916 it->descent += thick;
1917 }
1918 }
1919 else if (it->char_to_display == '\t')
1920 {
1921 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1922 int x = (it->current_x
1923 - it->prompt_width
1924 + it->continuation_lines_width);
1925 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1926
1927 it->pixel_width = next_tab_x - x;
1928 it->nglyphs = 1;
b4192550
KH
1929 it->ascent = it->phys_ascent = font->ascent + boff;
1930 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1931
1932 if (it->glyph_row)
1933 {
1934 double ascent = (double) it->ascent / (it->ascent + it->descent);
1935 x_append_stretch_glyph (it, it->object, it->pixel_width,
1936 it->ascent + it->descent, ascent);
1937 }
1938 }
1939 else
1940 {
1941 /* A multi-byte character. Assume that the display width of the
1942 character is the width of the character multiplied by the
b4192550 1943 width of the font. */
06a2c219 1944
b4192550
KH
1945 /* If we found a font, this font should give us the right
1946 metrics. If we didn't find a font, use the frame's
1947 default font and calculate the width of the character
1948 from the charset width; this is what old redisplay code
1949 did. */
1950 pcm = x_per_char_metric (font, &char2b);
1951 it->pixel_width = pcm->width;
1952 if (font_not_found_p)
1953 it->pixel_width *= CHARSET_WIDTH (it->charset);
1954 it->nglyphs = 1;
1955 it->ascent = font->ascent + boff;
1956 it->descent = font->descent - boff;
1957 it->phys_ascent = pcm->ascent + boff;
1958 it->phys_descent = pcm->descent - boff;
1959 if (it->glyph_row
1960 && (pcm->lbearing < 0
1961 || pcm->rbearing > pcm->width))
1962 it->glyph_row->contains_overlapping_glyphs_p = 1;
06a2c219
GM
1963
1964 if (face->box != FACE_NO_BOX)
1965 {
1966 int thick = face->box_line_width;
1967 it->ascent += thick;
1968 it->descent += thick;
1969
1970 if (it->start_of_box_run_p)
1971 it->pixel_width += thick;
1972 if (it->end_of_box_run_p)
1973 it->pixel_width += thick;
1974 }
1975
1976 /* If face has an overline, add the height of the overline
1977 (1 pixel) and a 1 pixel margin to the character height. */
1978 if (face->overline_p)
1979 it->ascent += 2;
1980
1981 take_vertical_position_into_account (it);
1982
1983 if (it->glyph_row)
1984 x_append_glyph (it);
1985 }
1986 }
b4192550
KH
1987 else if (it->what == IT_COMPOSITION)
1988 {
1989 /* Note: A composition is represented as one glyph in the
1990 glyph matrix. There are no padding glyphs. */
1991 XChar2b char2b;
1992 XFontStruct *font;
1993 struct face *face;
1994 XCharStruct *pcm;
1995 int font_not_found_p;
1996 struct font_info *font_info;
1997 int boff; /* baseline offset */
1998 struct composition *cmp = composition_table[it->cmp_id];
1999
2000 /* Maybe translate single-byte characters to multibyte. */
2001 it->char_to_display = it->c;
2002 if (unibyte_display_via_language_environment
2003 && SINGLE_BYTE_CHAR_P (it->c)
2004 && (it->c >= 0240
2005 || (it->c >= 0200
2006 && !NILP (Vnonascii_translation_table))))
2007 {
2008 it->char_to_display = unibyte_char_to_multibyte (it->c);
2009 it->charset = CHAR_CHARSET (it->char_to_display);
2010 }
2011
2012 /* Get face and font to use. Encode IT->char_to_display. */
2013 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
2014 it->face_id, &char2b,
2015 it->multibyte_p);
2016 font = face->font;
2017
2018 /* When no suitable font found, use the default font. */
2019 font_not_found_p = font == NULL;
2020 if (font_not_found_p)
2021 {
2022 font = FRAME_FONT (it->f);
2023 boff = it->f->output_data.x->baseline_offset;
2024 font_info = NULL;
2025 }
2026 else
2027 {
2028 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2029 boff = font_info->baseline_offset;
2030 if (font_info->vertical_centering)
2031 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2032 }
2033
2034 /* There are no padding glyphs, so there is only one glyph to
2035 produce for the composition. Important is that pixel_width,
2036 ascent and descent are the values of what is drawn by
2037 draw_glyphs (i.e. the values of the overall glyphs composed). */
2038 it->nglyphs = 1;
2039
2040 /* If we have not yet calculated pixel size data of glyphs of
2041 the composition for the current face font, calculate them
2042 now. Theoretically, we have to check all fonts for the
2043 glyphs, but that requires much time and memory space. So,
2044 here we check only the font of the first glyph. This leads
2045 to incorrect display very rarely, and C-l (recenter) can
2046 correct the display anyway. */
2047 if (cmp->font != (void *) font)
2048 {
2049 /* Ascent and descent of the font of the first character of
2050 this composition (adjusted by baseline offset). Ascent
2051 and descent of overall glyphs should not be less than
2052 them respectively. */
2053 int font_ascent = font->ascent + boff;
2054 int font_descent = font->descent - boff;
2055 /* Bounding box of the overall glyphs. */
2056 int leftmost, rightmost, lowest, highest;
2057 int i;
2058
2059 cmp->font = (void *) font;
2060
2061 /* Initialize the bounding box. */
2062 pcm = x_per_char_metric (font, &char2b);
2063 leftmost = 0;
2064 rightmost = pcm->width;
2065 lowest = - pcm->descent + boff;
2066 highest = pcm->ascent + boff;
2067 if (font_info
2068 && font_info->default_ascent
2069 && CHAR_TABLE_P (Vuse_default_ascent)
2070 && !NILP (Faref (Vuse_default_ascent,
2071 make_number (it->char_to_display))))
2072 highest = font_info->default_ascent + boff;
2073
2074 /* Draw the first glyph at the normal position. It may be
2075 shifted to right later if some other glyphs are drawn at
2076 the left. */
2077 cmp->offsets[0] = 0;
2078 cmp->offsets[1] = boff;
2079
2080 /* Set cmp->offsets for the remaining glyphs. */
2081 for (i = 1; i < cmp->glyph_len; i++)
2082 {
2083 int left, right, btm, top;
2084 int ch = COMPOSITION_GLYPH (cmp, i);
2085
2086 face = x_get_char_face_and_encoding (it->f, ch,
2087 it->face_id, &char2b,
2088 it->multibyte_p);
2089 font = face->font;
2090 if (font == NULL)
2091 {
2092 font = FRAME_FONT (it->f);
2093 boff = it->f->output_data.x->baseline_offset;
2094 font_info = NULL;
2095 }
2096 else
2097 {
2098 font_info
2099 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2100 boff = font_info->baseline_offset;
2101 if (font_info->vertical_centering)
2102 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2103 }
2104
2105 pcm = x_per_char_metric (font, &char2b);
2106
2107 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2108 {
2109 /* Relative composition with or without
2110 alternate chars. */
2111 left = (leftmost + rightmost - pcm->width) / 2;
2112 btm = - pcm->descent + boff;
2113 if (font_info && font_info->relative_compose
2114 && (! CHAR_TABLE_P (Vignore_relative_composition)
2115 || NILP (Faref (Vignore_relative_composition,
2116 make_number (ch)))))
2117 {
2118
2119 if (- pcm->descent
2120 >= font_info->relative_compose)
2121 /* One extra pixel between two glyphs. */
2122 btm = highest + 1;
2123 else if (pcm->ascent <= 0)
2124 /* One extra pixel between two glyphs. */
2125 btm = lowest - 1 - pcm->ascent - pcm->descent;
2126 }
2127 }
2128 else
2129 {
2130 /* A composition rule is specified by an integer
2131 value that encodes global and new reference
2132 points (GREF and NREF). GREF and NREF are
2133 specified by numbers as below:
2134
2135 0---1---2 -- ascent
2136 | |
2137 | |
2138 | |
2139 9--10--11 -- center
2140 | |
2141 ---3---4---5--- baseline
2142 | |
2143 6---7---8 -- descent
2144 */
2145 int rule = COMPOSITION_RULE (cmp, i);
2146 int gref, nref, grefx, grefy, nrefx, nrefy;
2147
2148 COMPOSITION_DECODE_RULE (rule, gref, nref);
2149 grefx = gref % 3, nrefx = nref % 3;
2150 grefy = gref / 3, nrefy = nref / 3;
2151
2152 left = (leftmost
2153 + grefx * (rightmost - leftmost) / 2
2154 - nrefx * pcm->width / 2);
2155 btm = ((grefy == 0 ? highest
2156 : grefy == 1 ? 0
2157 : grefy == 2 ? lowest
2158 : (highest + lowest) / 2)
2159 - (nrefy == 0 ? pcm->ascent + pcm->descent
2160 : nrefy == 1 ? pcm->descent - boff
2161 : nrefy == 2 ? 0
2162 : (pcm->ascent + pcm->descent) / 2));
2163 }
2164
2165 cmp->offsets[i * 2] = left;
2166 cmp->offsets[i * 2 + 1] = btm + pcm->descent;
2167
2168 /* Update the bounding box of the overall glyphs. */
2169 right = left + pcm->width;
2170 top = btm + pcm->descent + pcm->ascent;
2171 if (left < leftmost)
2172 leftmost = left;
2173 if (right > rightmost)
2174 rightmost = right;
2175 if (top > highest)
2176 highest = top;
2177 if (btm < lowest)
2178 lowest = btm;
2179 }
2180
2181 /* If there are glyphs whose x-offsets are negative,
2182 shift all glyphs to the right and make all x-offsets
2183 non-negative. */
2184 if (leftmost < 0)
2185 {
2186 for (i = 0; i < cmp->glyph_len; i++)
2187 cmp->offsets[i * 2] -= leftmost;
2188 rightmost -= leftmost;
2189 }
2190
2191 cmp->pixel_width = rightmost;
2192 cmp->ascent = highest;
2193 cmp->descent = - lowest;
2194 if (cmp->ascent < font_ascent)
2195 cmp->ascent = font_ascent;
2196 if (cmp->descent < font_descent)
2197 cmp->descent = font_descent;
2198 }
2199
2200 it->pixel_width = cmp->pixel_width;
2201 it->ascent = it->phys_ascent = cmp->ascent;
2202 it->descent = it->phys_descent = cmp->descent;
2203
2204 if (face->box != FACE_NO_BOX)
2205 {
2206 int thick = face->box_line_width;
2207 it->ascent += thick;
2208 it->descent += thick;
2209
2210 if (it->start_of_box_run_p)
2211 it->pixel_width += thick;
2212 if (it->end_of_box_run_p)
2213 it->pixel_width += thick;
2214 }
2215
2216 /* If face has an overline, add the height of the overline
2217 (1 pixel) and a 1 pixel margin to the character height. */
2218 if (face->overline_p)
2219 it->ascent += 2;
2220
2221 take_vertical_position_into_account (it);
2222
2223 if (it->glyph_row)
2224 x_append_composite_glyph (it);
2225 }
06a2c219
GM
2226 else if (it->what == IT_IMAGE)
2227 x_produce_image_glyph (it);
2228 else if (it->what == IT_STRETCH)
2229 x_produce_stretch_glyph (it);
2230
2231 /* Accumulate dimensions. */
2232 xassert (it->ascent >= 0 && it->descent > 0);
2233 if (it->area == TEXT_AREA)
2234 it->current_x += it->pixel_width;
66ac4b0e 2235
06a2c219
GM
2236 it->max_ascent = max (it->max_ascent, it->ascent);
2237 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2238 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2239 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2240}
2241
2242
2243/* Estimate the pixel height of the mode or top line on frame F.
2244 FACE_ID specifies what line's height to estimate. */
2245
2246int
2247x_estimate_mode_line_height (f, face_id)
2248 struct frame *f;
2249 enum face_id face_id;
2250{
2251 int height = 1;
2252
2253 /* This function is called so early when Emacs starts that the face
2254 cache and mode line face are not yet initialized. */
2255 if (FRAME_FACE_CACHE (f))
2256 {
2257 struct face *face = FACE_FROM_ID (f, face_id);
2258 if (face)
2259 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2260 }
2261
2262 return height;
2263}
2264
2265\f
2266/***********************************************************************
2267 Glyph display
2268 ***********************************************************************/
2269
2270/* A sequence of glyphs to be drawn in the same face.
2271
2272 This data structure is not really completely X specific, so it
2273 could possibly, at least partially, be useful for other systems. It
2274 is currently not part of the external redisplay interface because
2275 it's not clear what other systems will need. */
2276
2277struct glyph_string
2278{
2279 /* X-origin of the string. */
2280 int x;
2281
2282 /* Y-origin and y-position of the base line of this string. */
2283 int y, ybase;
2284
2285 /* The width of the string, not including a face extension. */
2286 int width;
2287
2288 /* The width of the string, including a face extension. */
2289 int background_width;
2290
2291 /* The height of this string. This is the height of the line this
2292 string is drawn in, and can be different from the height of the
2293 font the string is drawn in. */
2294 int height;
2295
2296 /* Number of pixels this string overwrites in front of its x-origin.
2297 This number is zero if the string has an lbearing >= 0; it is
2298 -lbearing, if the string has an lbearing < 0. */
2299 int left_overhang;
2300
2301 /* Number of pixels this string overwrites past its right-most
2302 nominal x-position, i.e. x + width. Zero if the string's
2303 rbearing is <= its nominal width, rbearing - width otherwise. */
2304 int right_overhang;
2305
2306 /* The frame on which the glyph string is drawn. */
2307 struct frame *f;
2308
2309 /* The window on which the glyph string is drawn. */
2310 struct window *w;
2311
2312 /* X display and window for convenience. */
2313 Display *display;
2314 Window window;
2315
2316 /* The glyph row for which this string was built. It determines the
2317 y-origin and height of the string. */
2318 struct glyph_row *row;
2319
2320 /* The area within row. */
2321 enum glyph_row_area area;
2322
2323 /* Characters to be drawn, and number of characters. */
2324 XChar2b *char2b;
2325 int nchars;
2326
2327 /* Character set of this glyph string. */
2328 int charset;
2329
2330 /* A face-override for drawing cursors, mouse face and similar. */
2331 enum draw_glyphs_face hl;
2332
2333 /* Face in which this string is to be drawn. */
2334 struct face *face;
2335
2336 /* Font in which this string is to be drawn. */
2337 XFontStruct *font;
2338
2339 /* Font info for this string. */
2340 struct font_info *font_info;
2341
b4192550
KH
2342 /* Non-null means this string describes (part of) a composition.
2343 All characters from char2b are drawn composed. */
2344 struct composition *cmp;
06a2c219
GM
2345
2346 /* Index of this glyph string's first character in the glyph
b4192550
KH
2347 definition of CMP. If this is zero, this glyph string describes
2348 the first character of a composition. */
06a2c219
GM
2349 int gidx;
2350
2351 /* 1 means this glyph strings face has to be drawn to the right end
2352 of the window's drawing area. */
2353 unsigned extends_to_end_of_line_p : 1;
2354
2355 /* 1 means the background of this string has been drawn. */
2356 unsigned background_filled_p : 1;
2357
2358 /* 1 means glyph string must be drawn with 16-bit functions. */
2359 unsigned two_byte_p : 1;
2360
2361 /* 1 means that the original font determined for drawing this glyph
2362 string could not be loaded. The member `font' has been set to
2363 the frame's default font in this case. */
2364 unsigned font_not_found_p : 1;
2365
2366 /* 1 means that the face in which this glyph string is drawn has a
2367 stipple pattern. */
2368 unsigned stippled_p : 1;
2369
66ac4b0e
GM
2370 /* 1 means only the foreground of this glyph string must be drawn,
2371 and we should use the physical height of the line this glyph
2372 string appears in as clip rect. */
2373 unsigned for_overlaps_p : 1;
2374
06a2c219
GM
2375 /* The GC to use for drawing this glyph string. */
2376 GC gc;
2377
2378 /* A pointer to the first glyph in the string. This glyph
2379 corresponds to char2b[0]. Needed to draw rectangles if
2380 font_not_found_p is 1. */
2381 struct glyph *first_glyph;
2382
2383 /* Image, if any. */
2384 struct image *img;
2385
2386 struct glyph_string *next, *prev;
2387};
2388
2389
5c187dee 2390#if 0
06a2c219
GM
2391
2392static void
2393x_dump_glyph_string (s)
2394 struct glyph_string *s;
2395{
2396 fprintf (stderr, "glyph string\n");
2397 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2398 s->x, s->y, s->width, s->height);
2399 fprintf (stderr, " ybase = %d\n", s->ybase);
2400 fprintf (stderr, " hl = %d\n", s->hl);
2401 fprintf (stderr, " left overhang = %d, right = %d\n",
2402 s->left_overhang, s->right_overhang);
2403 fprintf (stderr, " nchars = %d\n", s->nchars);
2404 fprintf (stderr, " extends to end of line = %d\n",
2405 s->extends_to_end_of_line_p);
2406 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2407 fprintf (stderr, " bg width = %d\n", s->background_width);
2408}
2409
2410#endif /* GLYPH_DEBUG */
2411
2412
2413
2414static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2415 struct glyph_string **,
2416 struct glyph_string *,
2417 struct glyph_string *));
2418static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2419 struct glyph_string **,
2420 struct glyph_string *,
2421 struct glyph_string *));
2422static void x_append_glyph_string P_ ((struct glyph_string **,
2423 struct glyph_string **,
2424 struct glyph_string *));
2425static int x_left_overwritten P_ ((struct glyph_string *));
2426static int x_left_overwriting P_ ((struct glyph_string *));
2427static int x_right_overwritten P_ ((struct glyph_string *));
2428static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2429static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2430 int));
06a2c219
GM
2431static void x_init_glyph_string P_ ((struct glyph_string *,
2432 XChar2b *, struct window *,
2433 struct glyph_row *,
2434 enum glyph_row_area, int,
2435 enum draw_glyphs_face));
2436static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2437 enum glyph_row_area, int, int,
66ac4b0e 2438 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2439static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2440static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2441static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2442 int));
2443static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2444static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2445static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2446static void x_draw_glyph_string P_ ((struct glyph_string *));
2447static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2448static void x_set_cursor_gc P_ ((struct glyph_string *));
2449static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2450static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2451static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2452 int *, int *));
2453static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2454static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2455 unsigned long *, double, int));
06a2c219 2456static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2457 double, int, unsigned long));
06a2c219
GM
2458static void x_setup_relief_colors P_ ((struct glyph_string *));
2459static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2460static void x_draw_image_relief P_ ((struct glyph_string *));
2461static void x_draw_image_foreground P_ ((struct glyph_string *));
2462static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2463static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2464static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2465 int, int, int));
2466static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2467 int, int, int, int, XRectangle *));
2468static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2469 int, int, int, XRectangle *));
66ac4b0e
GM
2470static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2471 enum glyph_row_area));
06a2c219
GM
2472
2473
06a2c219
GM
2474/* Append the list of glyph strings with head H and tail T to the list
2475 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2476
2477static INLINE void
2478x_append_glyph_string_lists (head, tail, h, t)
2479 struct glyph_string **head, **tail;
2480 struct glyph_string *h, *t;
2481{
2482 if (h)
2483 {
2484 if (*head)
2485 (*tail)->next = h;
2486 else
2487 *head = h;
2488 h->prev = *tail;
2489 *tail = t;
2490 }
2491}
2492
2493
2494/* Prepend the list of glyph strings with head H and tail T to the
2495 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2496 result. */
2497
2498static INLINE void
2499x_prepend_glyph_string_lists (head, tail, h, t)
2500 struct glyph_string **head, **tail;
2501 struct glyph_string *h, *t;
2502{
2503 if (h)
2504 {
2505 if (*head)
2506 (*head)->prev = t;
2507 else
2508 *tail = t;
2509 t->next = *head;
2510 *head = h;
2511 }
2512}
2513
2514
2515/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2516 Set *HEAD and *TAIL to the resulting list. */
2517
2518static INLINE void
2519x_append_glyph_string (head, tail, s)
2520 struct glyph_string **head, **tail;
2521 struct glyph_string *s;
2522{
2523 s->next = s->prev = NULL;
2524 x_append_glyph_string_lists (head, tail, s, s);
2525}
2526
2527
2528/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2529 face. */
2530
2531static void
2532x_set_cursor_gc (s)
2533 struct glyph_string *s;
2534{
2535 if (s->font == FRAME_FONT (s->f)
2536 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2537 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2538 && !s->cmp)
06a2c219
GM
2539 s->gc = s->f->output_data.x->cursor_gc;
2540 else
2541 {
2542 /* Cursor on non-default face: must merge. */
2543 XGCValues xgcv;
2544 unsigned long mask;
2545
2546 xgcv.background = s->f->output_data.x->cursor_pixel;
2547 xgcv.foreground = s->face->background;
2548
2549 /* If the glyph would be invisible, try a different foreground. */
2550 if (xgcv.foreground == xgcv.background)
2551 xgcv.foreground = s->face->foreground;
2552 if (xgcv.foreground == xgcv.background)
2553 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2554 if (xgcv.foreground == xgcv.background)
2555 xgcv.foreground = s->face->foreground;
2556
2557 /* Make sure the cursor is distinct from text in this face. */
2558 if (xgcv.background == s->face->background
2559 && xgcv.foreground == s->face->foreground)
2560 {
2561 xgcv.background = s->face->foreground;
2562 xgcv.foreground = s->face->background;
2563 }
2564
2565 IF_DEBUG (x_check_font (s->f, s->font));
2566 xgcv.font = s->font->fid;
2567 xgcv.graphics_exposures = False;
2568 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2569
2570 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2571 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2572 mask, &xgcv);
2573 else
2574 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2575 = XCreateGC (s->display, s->window, mask, &xgcv);
2576
2577 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2578 }
2579}
2580
2581
2582/* Set up S->gc of glyph string S for drawing text in mouse face. */
2583
2584static void
2585x_set_mouse_face_gc (s)
2586 struct glyph_string *s;
2587{
2588 int face_id;
2589
2590 /* What face has to be used for the mouse face? */
2591 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2592 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2593 s->face = FACE_FROM_ID (s->f, face_id);
2594 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2595
2596 /* If font in this face is same as S->font, use it. */
2597 if (s->font == s->face->font)
2598 s->gc = s->face->gc;
2599 else
2600 {
2601 /* Otherwise construct scratch_cursor_gc with values from FACE
2602 but font FONT. */
2603 XGCValues xgcv;
2604 unsigned long mask;
2605
2606 xgcv.background = s->face->background;
2607 xgcv.foreground = s->face->foreground;
2608 IF_DEBUG (x_check_font (s->f, s->font));
2609 xgcv.font = s->font->fid;
2610 xgcv.graphics_exposures = False;
2611 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2612
2613 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2614 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2615 mask, &xgcv);
2616 else
2617 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2618 = XCreateGC (s->display, s->window, mask, &xgcv);
2619
2620 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2621 }
2622
2623 xassert (s->gc != 0);
2624}
2625
2626
2627/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2628 Faces to use in the mode line have already been computed when the
2629 matrix was built, so there isn't much to do, here. */
2630
2631static INLINE void
2632x_set_mode_line_face_gc (s)
2633 struct glyph_string *s;
2634{
2635 s->gc = s->face->gc;
2636 xassert (s->gc != 0);
2637}
2638
2639
2640/* Set S->gc of glyph string S for drawing that glyph string. Set
2641 S->stippled_p to a non-zero value if the face of S has a stipple
2642 pattern. */
2643
2644static INLINE void
2645x_set_glyph_string_gc (s)
2646 struct glyph_string *s;
2647{
2648 if (s->hl == DRAW_NORMAL_TEXT)
2649 {
2650 s->gc = s->face->gc;
2651 s->stippled_p = s->face->stipple != 0;
2652 }
2653 else if (s->hl == DRAW_INVERSE_VIDEO)
2654 {
2655 x_set_mode_line_face_gc (s);
2656 s->stippled_p = s->face->stipple != 0;
2657 }
2658 else if (s->hl == DRAW_CURSOR)
2659 {
2660 x_set_cursor_gc (s);
2661 s->stippled_p = 0;
2662 }
2663 else if (s->hl == DRAW_MOUSE_FACE)
2664 {
2665 x_set_mouse_face_gc (s);
2666 s->stippled_p = s->face->stipple != 0;
2667 }
2668 else if (s->hl == DRAW_IMAGE_RAISED
2669 || s->hl == DRAW_IMAGE_SUNKEN)
2670 {
2671 s->gc = s->face->gc;
2672 s->stippled_p = s->face->stipple != 0;
2673 }
2674 else
2675 {
2676 s->gc = s->face->gc;
2677 s->stippled_p = s->face->stipple != 0;
2678 }
2679
2680 /* GC must have been set. */
2681 xassert (s->gc != 0);
2682}
2683
2684
2685/* Return in *R the clipping rectangle for glyph string S. */
2686
2687static void
2688x_get_glyph_string_clip_rect (s, r)
2689 struct glyph_string *s;
2690 XRectangle *r;
2691{
2692 if (s->row->full_width_p)
2693 {
2694 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2695 int canon_x = CANON_X_UNIT (s->f);
2696
2697 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2698 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2699
2700 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2701 {
1da3fd71 2702 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2703 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2704 r->x -= width;
2705 }
2706
b9432a85 2707 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2708
06a2c219
GM
2709 /* Unless displaying a mode or menu bar line, which are always
2710 fully visible, clip to the visible part of the row. */
2711 if (s->w->pseudo_window_p)
2712 r->height = s->row->visible_height;
2713 else
2714 r->height = s->height;
2715 }
2716 else
2717 {
2718 /* This is a text line that may be partially visible. */
2719 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2720 r->width = window_box_width (s->w, s->area);
2721 r->height = s->row->visible_height;
2722 }
2723
2724 /* Don't use S->y for clipping because it doesn't take partially
2725 visible lines into account. For example, it can be negative for
2726 partially visible lines at the top of a window. */
2727 if (!s->row->full_width_p
2728 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2729 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2730 else
2731 r->y = max (0, s->row->y);
06a2c219 2732
9ea173e8 2733 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2734 at the top of the window. */
9ea173e8 2735 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2736 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2737
2738 /* If S draws overlapping rows, it's sufficient to use the top and
2739 bottom of the window for clipping because this glyph string
2740 intentionally draws over other lines. */
2741 if (s->for_overlaps_p)
2742 {
045dee35 2743 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2744 r->height = window_text_bottom_y (s->w) - r->y;
2745 }
2746
2747 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2748}
2749
2750
2751/* Set clipping for output of glyph string S. S may be part of a mode
2752 line or menu if we don't have X toolkit support. */
2753
2754static INLINE void
2755x_set_glyph_string_clipping (s)
2756 struct glyph_string *s;
2757{
2758 XRectangle r;
2759 x_get_glyph_string_clip_rect (s, &r);
2760 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2761}
2762
2763
2764/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2765 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2766
2767static INLINE void
2768x_compute_glyph_string_overhangs (s)
2769 struct glyph_string *s;
2770{
b4192550 2771 if (s->cmp == NULL
06a2c219
GM
2772 && s->first_glyph->type == CHAR_GLYPH)
2773 {
2774 XCharStruct cs;
2775 int direction, font_ascent, font_descent;
2776 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2777 &font_ascent, &font_descent, &cs);
2778 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2779 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2780 }
2781}
2782
2783
2784/* Compute overhangs and x-positions for glyph string S and its
2785 predecessors, or successors. X is the starting x-position for S.
2786 BACKWARD_P non-zero means process predecessors. */
2787
2788static void
2789x_compute_overhangs_and_x (s, x, backward_p)
2790 struct glyph_string *s;
2791 int x;
2792 int backward_p;
2793{
2794 if (backward_p)
2795 {
2796 while (s)
2797 {
2798 x_compute_glyph_string_overhangs (s);
2799 x -= s->width;
2800 s->x = x;
2801 s = s->prev;
2802 }
2803 }
2804 else
2805 {
2806 while (s)
2807 {
2808 x_compute_glyph_string_overhangs (s);
2809 s->x = x;
2810 x += s->width;
2811 s = s->next;
2812 }
2813 }
2814}
2815
2816
2817/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2818 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2819 assumed to be zero. */
06a2c219
GM
2820
2821static void
2822x_get_glyph_overhangs (glyph, f, left, right)
2823 struct glyph *glyph;
2824 struct frame *f;
2825 int *left, *right;
2826{
2827 int c;
2828
2829 *left = *right = 0;
2830
b4192550 2831 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2832 {
2833 XFontStruct *font;
2834 struct face *face;
2835 struct font_info *font_info;
2836 XChar2b char2b;
2837
2838 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2839 font = face->font;
2840 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2841 if (font)
2842 {
2843 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2844
2845 if (pcm->rbearing > pcm->width)
2846 *right = pcm->rbearing - pcm->width;
2847 if (pcm->lbearing < 0)
2848 *left = -pcm->lbearing;
2849 }
2850 }
2851}
2852
2853
2854/* Return the index of the first glyph preceding glyph string S that
2855 is overwritten by S because of S's left overhang. Value is -1
2856 if no glyphs are overwritten. */
2857
2858static int
2859x_left_overwritten (s)
2860 struct glyph_string *s;
2861{
2862 int k;
2863
2864 if (s->left_overhang)
2865 {
2866 int x = 0, i;
2867 struct glyph *glyphs = s->row->glyphs[s->area];
2868 int first = s->first_glyph - glyphs;
2869
2870 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2871 x -= glyphs[i].pixel_width;
2872
2873 k = i + 1;
2874 }
2875 else
2876 k = -1;
2877
2878 return k;
2879}
2880
2881
2882/* Return the index of the first glyph preceding glyph string S that
2883 is overwriting S because of its right overhang. Value is -1 if no
2884 glyph in front of S overwrites S. */
2885
2886static int
2887x_left_overwriting (s)
2888 struct glyph_string *s;
2889{
2890 int i, k, x;
2891 struct glyph *glyphs = s->row->glyphs[s->area];
2892 int first = s->first_glyph - glyphs;
2893
2894 k = -1;
2895 x = 0;
2896 for (i = first - 1; i >= 0; --i)
2897 {
2898 int left, right;
2899 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2900 if (x + right > 0)
2901 k = i;
2902 x -= glyphs[i].pixel_width;
2903 }
2904
2905 return k;
2906}
2907
2908
2909/* Return the index of the last glyph following glyph string S that is
2910 not overwritten by S because of S's right overhang. Value is -1 if
2911 no such glyph is found. */
2912
2913static int
2914x_right_overwritten (s)
2915 struct glyph_string *s;
2916{
2917 int k = -1;
2918
2919 if (s->right_overhang)
2920 {
2921 int x = 0, i;
2922 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2923 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2924 int end = s->row->used[s->area];
2925
2926 for (i = first; i < end && s->right_overhang > x; ++i)
2927 x += glyphs[i].pixel_width;
2928
2929 k = i;
2930 }
2931
2932 return k;
2933}
2934
2935
2936/* Return the index of the last glyph following glyph string S that
2937 overwrites S because of its left overhang. Value is negative
2938 if no such glyph is found. */
2939
2940static int
2941x_right_overwriting (s)
2942 struct glyph_string *s;
2943{
2944 int i, k, x;
2945 int end = s->row->used[s->area];
2946 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2947 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2948
2949 k = -1;
2950 x = 0;
2951 for (i = first; i < end; ++i)
2952 {
2953 int left, right;
2954 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2955 if (x - left < 0)
2956 k = i;
2957 x += glyphs[i].pixel_width;
2958 }
2959
2960 return k;
2961}
2962
2963
2964/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2965
2966static INLINE void
2967x_clear_glyph_string_rect (s, x, y, w, h)
2968 struct glyph_string *s;
2969 int x, y, w, h;
2970{
2971 XGCValues xgcv;
2972 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2973 XSetForeground (s->display, s->gc, xgcv.background);
2974 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2975 XSetForeground (s->display, s->gc, xgcv.foreground);
2976}
2977
2978
2979/* Draw the background of glyph_string S. If S->background_filled_p
2980 is non-zero don't draw it. FORCE_P non-zero means draw the
2981 background even if it wouldn't be drawn normally. This is used
b4192550
KH
2982 when a string preceding S draws into the background of S, or S
2983 contains the first component of a composition. */
06a2c219
GM
2984
2985static void
2986x_draw_glyph_string_background (s, force_p)
2987 struct glyph_string *s;
2988 int force_p;
2989{
2990 /* Nothing to do if background has already been drawn or if it
2991 shouldn't be drawn in the first place. */
2992 if (!s->background_filled_p)
2993 {
b4192550 2994 if (s->stippled_p)
06a2c219
GM
2995 {
2996 /* Fill background with a stipple pattern. */
2997 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2998 XFillRectangle (s->display, s->window, s->gc, s->x,
2999 s->y + s->face->box_line_width,
3000 s->background_width,
3001 s->height - 2 * s->face->box_line_width);
3002 XSetFillStyle (s->display, s->gc, FillSolid);
3003 s->background_filled_p = 1;
3004 }
3005 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3006 || s->font_not_found_p
3007 || s->extends_to_end_of_line_p
06a2c219
GM
3008 || force_p)
3009 {
3010 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3011 s->background_width,
3012 s->height - 2 * s->face->box_line_width);
3013 s->background_filled_p = 1;
3014 }
3015 }
3016}
3017
3018
3019/* Draw the foreground of glyph string S. */
3020
3021static void
3022x_draw_glyph_string_foreground (s)
3023 struct glyph_string *s;
3024{
3025 int i, x;
3026
3027 /* If first glyph of S has a left box line, start drawing the text
3028 of S to the right of that box line. */
3029 if (s->face->box != FACE_NO_BOX
3030 && s->first_glyph->left_box_line_p)
3031 x = s->x + s->face->box_line_width;
3032 else
3033 x = s->x;
3034
b4192550
KH
3035 /* Draw characters of S as rectangles if S's font could not be
3036 loaded. */
3037 if (s->font_not_found_p)
06a2c219 3038 {
b4192550 3039 for (i = 0; i < s->nchars; ++i)
06a2c219 3040 {
b4192550
KH
3041 struct glyph *g = s->first_glyph + i;
3042 XDrawRectangle (s->display, s->window,
3043 s->gc, x, s->y, g->pixel_width - 1,
3044 s->height - 1);
3045 x += g->pixel_width;
06a2c219
GM
3046 }
3047 }
3048 else
3049 {
b4192550
KH
3050 char *char1b = (char *) s->char2b;
3051 int boff = s->font_info->baseline_offset;
06a2c219 3052
b4192550
KH
3053 if (s->font_info->vertical_centering)
3054 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3055
3056 /* If we can use 8-bit functions, condense S->char2b. */
3057 if (!s->two_byte_p)
3058 for (i = 0; i < s->nchars; ++i)
3059 char1b[i] = s->char2b[i].byte2;
3060
3061 /* Draw text with XDrawString if background has already been
3062 filled. Otherwise, use XDrawImageString. (Note that
3063 XDrawImageString is usually faster than XDrawString.) Always
3064 use XDrawImageString when drawing the cursor so that there is
3065 no chance that characters under a box cursor are invisible. */
3066 if (s->for_overlaps_p
3067 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3068 {
3069 /* Draw characters with 16-bit or 8-bit functions. */
3070 if (s->two_byte_p)
3071 XDrawString16 (s->display, s->window, s->gc, x,
3072 s->ybase - boff, s->char2b, s->nchars);
3073 else
3074 XDrawString (s->display, s->window, s->gc, x,
3075 s->ybase - boff, char1b, s->nchars);
3076 }
06a2c219
GM
3077 else
3078 {
b4192550
KH
3079 if (s->two_byte_p)
3080 XDrawImageString16 (s->display, s->window, s->gc, x,
3081 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3082 else
b4192550
KH
3083 XDrawImageString (s->display, s->window, s->gc, x,
3084 s->ybase - boff, char1b, s->nchars);
3085 }
3086 }
3087}
06a2c219 3088
b4192550 3089/* Draw the foreground of composite glyph string S. */
06a2c219 3090
b4192550
KH
3091static void
3092x_draw_composite_glyph_string_foreground (s)
3093 struct glyph_string *s;
3094{
3095 int i, x;
06a2c219 3096
b4192550
KH
3097 /* If first glyph of S has a left box line, start drawing the text
3098 of S to the right of that box line. */
3099 if (s->face->box != FACE_NO_BOX
3100 && s->first_glyph->left_box_line_p)
3101 x = s->x + s->face->box_line_width;
3102 else
3103 x = s->x;
06a2c219 3104
b4192550
KH
3105 /* S is a glyph string for a composition. S->gidx is the index of
3106 the first character drawn for glyphs of this composition.
3107 S->gidx == 0 means we are drawing the very first character of
3108 this composition. */
06a2c219 3109
b4192550
KH
3110 /* Draw a rectangle for the composition if the font for the very
3111 first character of the composition could not be loaded. */
3112 if (s->font_not_found_p)
3113 {
3114 if (s->gidx == 0)
3115 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3116 s->width - 1, s->height - 1);
3117 }
3118 else
3119 {
3120 for (i = 0; i < s->nchars; i++, ++s->gidx)
3121 XDrawString16 (s->display, s->window, s->gc,
3122 x + s->cmp->offsets[s->gidx * 2],
3123 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3124 s->char2b + i, 1);
06a2c219
GM
3125 }
3126}
3127
3128
80c32bcc
GM
3129#ifdef USE_X_TOOLKIT
3130
3131/* Allocate the color COLOR->pixel on the screen and display of
3132 widget WIDGET in colormap CMAP. If an exact match cannot be
3133 allocated, try the nearest color available. Value is non-zero
3134 if successful. This is called from lwlib. */
3135
3136int
3137x_alloc_nearest_color_for_widget (widget, cmap, color)
3138 Widget widget;
3139 Colormap cmap;
3140 XColor *color;
3141{
3142 struct frame *f;
3143 struct x_display_info *dpyinfo;
5c187dee 3144 Lisp_Object tail;
80c32bcc
GM
3145
3146 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3147
3148 /* Find the top-level shell of the widget. Note that this function
3149 can be called when the widget is not yet realized, so XtWindow
3150 (widget) == 0. That's the reason we can't simply use
3151 x_any_window_to_frame. */
3152 while (!XtIsTopLevelShell (widget))
3153 widget = XtParent (widget);
3154
3155 /* Look for a frame with that top-level widget. Allocate the color
3156 on that frame to get the right gamma correction value. */
3157 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3158 if (GC_FRAMEP (XCAR (tail))
3159 && (f = XFRAME (XCAR (tail)),
3160 (f->output_data.nothing != 1
3161 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3162 && f->output_data.x->widget == widget)
3163 return x_alloc_nearest_color (f, cmap, color);
3164
3165 abort ();
3166}
3167
3168#endif /* USE_X_TOOLKIT */
3169
3170
06a2c219
GM
3171/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3172 CMAP. If an exact match can't be allocated, try the nearest color
3173 available. Value is non-zero if successful. Set *COLOR to the
3174 color allocated. */
3175
3176int
80c32bcc
GM
3177x_alloc_nearest_color (f, cmap, color)
3178 struct frame *f;
06a2c219
GM
3179 Colormap cmap;
3180 XColor *color;
3181{
80c32bcc
GM
3182 Display *display = FRAME_X_DISPLAY (f);
3183 Screen *screen = FRAME_X_SCREEN (f);
3184 int rc;
3185
3186 gamma_correct (f, color);
3187 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3188 if (rc == 0)
3189 {
3190 /* If we got to this point, the colormap is full, so we're going
3191 to try to get the next closest color. The algorithm used is
3192 a least-squares matching, which is what X uses for closest
3193 color matching with StaticColor visuals. */
3194 int nearest, i;
3195 unsigned long nearest_delta = ~0;
3196 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3197 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3198
3199 for (i = 0; i < ncells; ++i)
3200 cells[i].pixel = i;
3201 XQueryColors (display, cmap, cells, ncells);
3202
3203 for (nearest = i = 0; i < ncells; ++i)
3204 {
3205 long dred = (color->red >> 8) - (cells[i].red >> 8);
3206 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3207 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3208 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3209
3210 if (delta < nearest_delta)
3211 {
3212 nearest = i;
3213 nearest_delta = delta;
3214 }
3215 }
3216
3217 color->red = cells[nearest].red;
3218 color->green = cells[nearest].green;
3219 color->blue = cells[nearest].blue;
3220 rc = XAllocColor (display, cmap, color);
3221 }
3222
3223 return rc;
3224}
3225
3226
3227/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3228 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3229 If this produces the same color as PIXEL, try a color where all RGB
3230 values have DELTA added. Return the allocated color in *PIXEL.
3231 DISPLAY is the X display, CMAP is the colormap to operate on.
3232 Value is non-zero if successful. */
3233
3234static int
3235x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3236 struct frame *f;
3237 Display *display;
3238 Colormap cmap;
3239 unsigned long *pixel;
68c45bf0 3240 double factor;
06a2c219
GM
3241 int delta;
3242{
3243 XColor color, new;
3244 int success_p;
3245
3246 /* Get RGB color values. */
3247 color.pixel = *pixel;
3248 XQueryColor (display, cmap, &color);
3249
3250 /* Change RGB values by specified FACTOR. Avoid overflow! */
3251 xassert (factor >= 0);
3252 new.red = min (0xffff, factor * color.red);
3253 new.green = min (0xffff, factor * color.green);
3254 new.blue = min (0xffff, factor * color.blue);
3255
3256 /* Try to allocate the color. */
80c32bcc 3257 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3258 if (success_p)
3259 {
3260 if (new.pixel == *pixel)
3261 {
3262 /* If we end up with the same color as before, try adding
3263 delta to the RGB values. */
3264 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3265
3266 /* If display has an immutable color map, freeing colors is
3267 not necessary and some servers don't allow it. So don't
3268 do it. */
3269 if (class != StaticColor
3270 && class != StaticGray
3271 && class != TrueColor)
3272 XFreeColors (display, cmap, &new.pixel, 1, 0);
3273
3274 new.red = min (0xffff, delta + color.red);
3275 new.green = min (0xffff, delta + color.green);
3276 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3277 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3278 }
3279 else
3280 success_p = 1;
3281 *pixel = new.pixel;
3282 }
3283
3284 return success_p;
3285}
3286
3287
3288/* Set up the foreground color for drawing relief lines of glyph
3289 string S. RELIEF is a pointer to a struct relief containing the GC
3290 with which lines will be drawn. Use a color that is FACTOR or
3291 DELTA lighter or darker than the relief's background which is found
3292 in S->f->output_data.x->relief_background. If such a color cannot
3293 be allocated, use DEFAULT_PIXEL, instead. */
3294
3295static void
3296x_setup_relief_color (f, relief, factor, delta, default_pixel)
3297 struct frame *f;
3298 struct relief *relief;
68c45bf0 3299 double factor;
06a2c219
GM
3300 int delta;
3301 unsigned long default_pixel;
3302{
3303 XGCValues xgcv;
3304 struct x_output *di = f->output_data.x;
3305 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3306 unsigned long pixel;
3307 unsigned long background = di->relief_background;
3308 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
dcd08bfb
GM
3309 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3310 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3311
3312 xgcv.graphics_exposures = False;
3313 xgcv.line_width = 1;
3314
3315 /* Free previously allocated color. The color cell will be reused
3316 when it has been freed as many times as it was allocated, so this
3317 doesn't affect faces using the same colors. */
3318 if (relief->gc
3319 && relief->allocated_p)
3320 {
3321 /* If display has an immutable color map, freeing colors is not
3322 necessary and some servers don't allow it. So don't do it. */
dcd08bfb 3323 int class = dpyinfo->visual->class;
06a2c219
GM
3324 if (class != StaticColor
3325 && class != StaticGray
3326 && class != TrueColor)
dcd08bfb 3327 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
06a2c219
GM
3328 relief->allocated_p = 0;
3329 }
3330
3331 /* Allocate new color. */
3332 xgcv.foreground = default_pixel;
3333 pixel = background;
dcd08bfb
GM
3334 if (dpyinfo->n_planes != 1
3335 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3336 {
3337 relief->allocated_p = 1;
3338 xgcv.foreground = relief->pixel = pixel;
3339 }
3340
3341 if (relief->gc == 0)
3342 {
dcd08bfb 3343 xgcv.stipple = dpyinfo->gray;
06a2c219 3344 mask |= GCStipple;
dcd08bfb 3345 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3346 }
3347 else
dcd08bfb 3348 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3349}
3350
3351
3352/* Set up colors for the relief lines around glyph string S. */
3353
3354static void
3355x_setup_relief_colors (s)
3356 struct glyph_string *s;
3357{
3358 struct x_output *di = s->f->output_data.x;
3359 unsigned long color;
3360
3361 if (s->face->use_box_color_for_shadows_p)
3362 color = s->face->box_color;
3363 else
3364 {
3365 XGCValues xgcv;
3366
3367 /* Get the background color of the face. */
3368 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3369 color = xgcv.background;
3370 }
3371
3372 if (di->white_relief.gc == 0
3373 || color != di->relief_background)
3374 {
3375 di->relief_background = color;
3376 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3377 WHITE_PIX_DEFAULT (s->f));
3378 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3379 BLACK_PIX_DEFAULT (s->f));
3380 }
3381}
3382
3383
3384/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3385 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3386 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3387 relief. LEFT_P non-zero means draw a relief on the left side of
3388 the rectangle. RIGHT_P non-zero means draw a relief on the right
3389 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3390 when drawing. */
3391
3392static void
3393x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3394 raised_p, left_p, right_p, clip_rect)
3395 struct frame *f;
3396 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3397 XRectangle *clip_rect;
3398{
3399 int i;
3400 GC gc;
3401
3402 if (raised_p)
3403 gc = f->output_data.x->white_relief.gc;
3404 else
3405 gc = f->output_data.x->black_relief.gc;
3406 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3407
3408 /* Top. */
3409 for (i = 0; i < width; ++i)
3410 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3411 left_x + i * left_p, top_y + i,
3412 right_x + 1 - i * right_p, top_y + i);
3413
3414 /* Left. */
3415 if (left_p)
3416 for (i = 0; i < width; ++i)
3417 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3418 left_x + i, top_y + i, left_x + i, bottom_y - i);
3419
3420 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3421 if (raised_p)
3422 gc = f->output_data.x->black_relief.gc;
3423 else
3424 gc = f->output_data.x->white_relief.gc;
3425 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3426
3427 /* Bottom. */
3428 for (i = 0; i < width; ++i)
3429 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3430 left_x + i * left_p, bottom_y - i,
3431 right_x + 1 - i * right_p, bottom_y - i);
3432
3433 /* Right. */
3434 if (right_p)
3435 for (i = 0; i < width; ++i)
3436 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3437 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3438
3439 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3440}
3441
3442
3443/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3444 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3445 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3446 left side of the rectangle. RIGHT_P non-zero means draw a line
3447 on the right side of the rectangle. CLIP_RECT is the clipping
3448 rectangle to use when drawing. */
3449
3450static void
3451x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3452 left_p, right_p, clip_rect)
3453 struct glyph_string *s;
3454 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3455 XRectangle *clip_rect;
3456{
3457 XGCValues xgcv;
3458
3459 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3460 XSetForeground (s->display, s->gc, s->face->box_color);
3461 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3462
3463 /* Top. */
3464 XFillRectangle (s->display, s->window, s->gc,
3465 left_x, top_y, right_x - left_x, width);
3466
3467 /* Left. */
3468 if (left_p)
3469 XFillRectangle (s->display, s->window, s->gc,
3470 left_x, top_y, width, bottom_y - top_y);
3471
3472 /* Bottom. */
3473 XFillRectangle (s->display, s->window, s->gc,
3474 left_x, bottom_y - width, right_x - left_x, width);
3475
3476 /* Right. */
3477 if (right_p)
3478 XFillRectangle (s->display, s->window, s->gc,
3479 right_x - width, top_y, width, bottom_y - top_y);
3480
3481 XSetForeground (s->display, s->gc, xgcv.foreground);
3482 XSetClipMask (s->display, s->gc, None);
3483}
3484
3485
3486/* Draw a box around glyph string S. */
3487
3488static void
3489x_draw_glyph_string_box (s)
3490 struct glyph_string *s;
3491{
3492 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3493 int left_p, right_p;
3494 struct glyph *last_glyph;
3495 XRectangle clip_rect;
3496
3497 last_x = window_box_right (s->w, s->area);
3498 if (s->row->full_width_p
3499 && !s->w->pseudo_window_p)
3500 {
110859fc 3501 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3502 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3503 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3504 }
3505
3506 /* The glyph that may have a right box line. */
b4192550 3507 last_glyph = (s->cmp || s->img
06a2c219
GM
3508 ? s->first_glyph
3509 : s->first_glyph + s->nchars - 1);
3510
3511 width = s->face->box_line_width;
3512 raised_p = s->face->box == FACE_RAISED_BOX;
3513 left_x = s->x;
3514 right_x = ((s->row->full_width_p
1da3fd71 3515 ? last_x - 1
a7aeb2de 3516 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3517 top_y = s->y;
3518 bottom_y = top_y + s->height - 1;
3519
3520 left_p = (s->first_glyph->left_box_line_p
3521 || (s->hl == DRAW_MOUSE_FACE
3522 && (s->prev == NULL
3523 || s->prev->hl != s->hl)));
3524 right_p = (last_glyph->right_box_line_p
3525 || (s->hl == DRAW_MOUSE_FACE
3526 && (s->next == NULL
3527 || s->next->hl != s->hl)));
3528
3529 x_get_glyph_string_clip_rect (s, &clip_rect);
3530
3531 if (s->face->box == FACE_SIMPLE_BOX)
3532 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3533 left_p, right_p, &clip_rect);
3534 else
3535 {
3536 x_setup_relief_colors (s);
3537 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3538 width, raised_p, left_p, right_p, &clip_rect);
3539 }
3540}
3541
3542
3543/* Draw foreground of image glyph string S. */
3544
3545static void
3546x_draw_image_foreground (s)
3547 struct glyph_string *s;
3548{
3549 int x;
3550 int y = s->ybase - IMAGE_ASCENT (s->img);
3551
3552 /* If first glyph of S has a left box line, start drawing it to the
3553 right of that line. */
3554 if (s->face->box != FACE_NO_BOX
3555 && s->first_glyph->left_box_line_p)
3556 x = s->x + s->face->box_line_width;
3557 else
3558 x = s->x;
3559
3560 /* If there is a margin around the image, adjust x- and y-position
3561 by that margin. */
3562 if (s->img->margin)
3563 {
3564 x += s->img->margin;
3565 y += s->img->margin;
3566 }
3567
3568 if (s->img->pixmap)
3569 {
3570 if (s->img->mask)
3571 {
3572 /* We can't set both a clip mask and use XSetClipRectangles
3573 because the latter also sets a clip mask. We also can't
3574 trust on the shape extension to be available
3575 (XShapeCombineRegion). So, compute the rectangle to draw
3576 manually. */
3577 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3578 | GCFunction);
3579 XGCValues xgcv;
3580 XRectangle clip_rect, image_rect, r;
3581
3582 xgcv.clip_mask = s->img->mask;
3583 xgcv.clip_x_origin = x;
3584 xgcv.clip_y_origin = y;
3585 xgcv.function = GXcopy;
3586 XChangeGC (s->display, s->gc, mask, &xgcv);
3587
3588 x_get_glyph_string_clip_rect (s, &clip_rect);
3589 image_rect.x = x;
3590 image_rect.y = y;
3591 image_rect.width = s->img->width;
3592 image_rect.height = s->img->height;
3593 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3594 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3595 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3596 }
3597 else
3598 {
3599 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3600 0, 0, s->img->width, s->img->height, x, y);
3601
3602 /* When the image has a mask, we can expect that at
3603 least part of a mouse highlight or a block cursor will
3604 be visible. If the image doesn't have a mask, make
3605 a block cursor visible by drawing a rectangle around
3606 the image. I believe it's looking better if we do
3607 nothing here for mouse-face. */
3608 if (s->hl == DRAW_CURSOR)
3609 XDrawRectangle (s->display, s->window, s->gc, x, y,
3610 s->img->width - 1, s->img->height - 1);
3611 }
3612 }
3613 else
3614 /* Draw a rectangle if image could not be loaded. */
3615 XDrawRectangle (s->display, s->window, s->gc, x, y,
3616 s->img->width - 1, s->img->height - 1);
3617}
3618
3619
3620/* Draw a relief around the image glyph string S. */
3621
3622static void
3623x_draw_image_relief (s)
3624 struct glyph_string *s;
3625{
3626 int x0, y0, x1, y1, thick, raised_p;
3627 XRectangle r;
3628 int x;
3629 int y = s->ybase - IMAGE_ASCENT (s->img);
3630
3631 /* If first glyph of S has a left box line, start drawing it to the
3632 right of that line. */
3633 if (s->face->box != FACE_NO_BOX
3634 && s->first_glyph->left_box_line_p)
3635 x = s->x + s->face->box_line_width;
3636 else
3637 x = s->x;
3638
3639 /* If there is a margin around the image, adjust x- and y-position
3640 by that margin. */
3641 if (s->img->margin)
3642 {
3643 x += s->img->margin;
3644 y += s->img->margin;
3645 }
3646
3647 if (s->hl == DRAW_IMAGE_SUNKEN
3648 || s->hl == DRAW_IMAGE_RAISED)
3649 {
9ea173e8 3650 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3651 raised_p = s->hl == DRAW_IMAGE_RAISED;
3652 }
3653 else
3654 {
3655 thick = abs (s->img->relief);
3656 raised_p = s->img->relief > 0;
3657 }
3658
3659 x0 = x - thick;
3660 y0 = y - thick;
3661 x1 = x + s->img->width + thick - 1;
3662 y1 = y + s->img->height + thick - 1;
3663
3664 x_setup_relief_colors (s);
3665 x_get_glyph_string_clip_rect (s, &r);
3666 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3667}
3668
3669
3670/* Draw the foreground of image glyph string S to PIXMAP. */
3671
3672static void
3673x_draw_image_foreground_1 (s, pixmap)
3674 struct glyph_string *s;
3675 Pixmap pixmap;
3676{
3677 int x;
3678 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3679
3680 /* If first glyph of S has a left box line, start drawing it to the
3681 right of that line. */
3682 if (s->face->box != FACE_NO_BOX
3683 && s->first_glyph->left_box_line_p)
3684 x = s->face->box_line_width;
3685 else
3686 x = 0;
3687
3688 /* If there is a margin around the image, adjust x- and y-position
3689 by that margin. */
3690 if (s->img->margin)
3691 {
3692 x += s->img->margin;
3693 y += s->img->margin;
3694 }
dc43ef94 3695
06a2c219
GM
3696 if (s->img->pixmap)
3697 {
3698 if (s->img->mask)
3699 {
3700 /* We can't set both a clip mask and use XSetClipRectangles
3701 because the latter also sets a clip mask. We also can't
3702 trust on the shape extension to be available
3703 (XShapeCombineRegion). So, compute the rectangle to draw
3704 manually. */
3705 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3706 | GCFunction);
3707 XGCValues xgcv;
3708
3709 xgcv.clip_mask = s->img->mask;
3710 xgcv.clip_x_origin = x;
3711 xgcv.clip_y_origin = y;
3712 xgcv.function = GXcopy;
3713 XChangeGC (s->display, s->gc, mask, &xgcv);
3714
3715 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3716 0, 0, s->img->width, s->img->height, x, y);
3717 XSetClipMask (s->display, s->gc, None);
3718 }
3719 else
3720 {
3721 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3722 0, 0, s->img->width, s->img->height, x, y);
3723
3724 /* When the image has a mask, we can expect that at
3725 least part of a mouse highlight or a block cursor will
3726 be visible. If the image doesn't have a mask, make
3727 a block cursor visible by drawing a rectangle around
3728 the image. I believe it's looking better if we do
3729 nothing here for mouse-face. */
3730 if (s->hl == DRAW_CURSOR)
3731 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3732 s->img->width - 1, s->img->height - 1);
3733 }
3734 }
3735 else
3736 /* Draw a rectangle if image could not be loaded. */
3737 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3738 s->img->width - 1, s->img->height - 1);
3739}
dc43ef94 3740
990ba854 3741
06a2c219
GM
3742/* Draw part of the background of glyph string S. X, Y, W, and H
3743 give the rectangle to draw. */
a9a5b0a5 3744
06a2c219
GM
3745static void
3746x_draw_glyph_string_bg_rect (s, x, y, w, h)
3747 struct glyph_string *s;
3748 int x, y, w, h;
3749{
3750 if (s->stippled_p)
3751 {
3752 /* Fill background with a stipple pattern. */
3753 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3754 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3755 XSetFillStyle (s->display, s->gc, FillSolid);
3756 }
3757 else
3758 x_clear_glyph_string_rect (s, x, y, w, h);
3759}
07e34cb0 3760
b5210ea7 3761
06a2c219 3762/* Draw image glyph string S.
dc43ef94 3763
06a2c219
GM
3764 s->y
3765 s->x +-------------------------
3766 | s->face->box
3767 |
3768 | +-------------------------
3769 | | s->img->margin
3770 | |
3771 | | +-------------------
3772 | | | the image
dc43ef94 3773
06a2c219 3774 */
dc43ef94 3775
06a2c219
GM
3776static void
3777x_draw_image_glyph_string (s)
3778 struct glyph_string *s;
3779{
3780 int x, y;
3781 int box_line_width = s->face->box_line_width;
3782 int margin = s->img->margin;
3783 int height;
3784 Pixmap pixmap = None;
3785
3786 height = s->height - 2 * box_line_width;
3787
3788 /* Fill background with face under the image. Do it only if row is
3789 taller than image or if image has a clip mask to reduce
3790 flickering. */
3791 s->stippled_p = s->face->stipple != 0;
3792 if (height > s->img->height
3793 || margin
3794 || s->img->mask
3795 || s->img->pixmap == 0
3796 || s->width != s->background_width)
3797 {
3798 if (box_line_width && s->first_glyph->left_box_line_p)
3799 x = s->x + box_line_width;
3800 else
3801 x = s->x;
3802
3803 y = s->y + box_line_width;
3804
3805 if (s->img->mask)
3806 {
3807 /* Create a pixmap as large as the glyph string Fill it with
3808 the background color. Copy the image to it, using its
3809 mask. Copy the temporary pixmap to the display. */
3810 Screen *screen = FRAME_X_SCREEN (s->f);
3811 int depth = DefaultDepthOfScreen (screen);
3812
3813 /* Create a pixmap as large as the glyph string. */
3814 pixmap = XCreatePixmap (s->display, s->window,
3815 s->background_width,
3816 s->height, depth);
3817
3818 /* Don't clip in the following because we're working on the
3819 pixmap. */
3820 XSetClipMask (s->display, s->gc, None);
3821
3822 /* Fill the pixmap with the background color/stipple. */
3823 if (s->stippled_p)
3824 {
3825 /* Fill background with a stipple pattern. */
3826 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3827 XFillRectangle (s->display, pixmap, s->gc,
3828 0, 0, s->background_width, s->height);
3829 XSetFillStyle (s->display, s->gc, FillSolid);
3830 }
3831 else
3832 {
3833 XGCValues xgcv;
3834 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3835 &xgcv);
3836 XSetForeground (s->display, s->gc, xgcv.background);
3837 XFillRectangle (s->display, pixmap, s->gc,
3838 0, 0, s->background_width, s->height);
3839 XSetForeground (s->display, s->gc, xgcv.foreground);
3840 }
3841 }
3842 else
3843 /* Implementation idea: Is it possible to construct a mask?
3844 We could look at the color at the margins of the image, and
3845 say that this color is probably the background color of the
3846 image. */
3847 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3848
3849 s->background_filled_p = 1;
3850 }
dc43ef94 3851
06a2c219
GM
3852 /* Draw the foreground. */
3853 if (pixmap != None)
3854 {
3855 x_draw_image_foreground_1 (s, pixmap);
3856 x_set_glyph_string_clipping (s);
3857 XCopyArea (s->display, pixmap, s->window, s->gc,
3858 0, 0, s->background_width, s->height, s->x, s->y);
3859 XFreePixmap (s->display, pixmap);
3860 }
3861 else
3862 x_draw_image_foreground (s);
b5210ea7 3863
06a2c219
GM
3864 /* If we must draw a relief around the image, do it. */
3865 if (s->img->relief
3866 || s->hl == DRAW_IMAGE_RAISED
3867 || s->hl == DRAW_IMAGE_SUNKEN)
3868 x_draw_image_relief (s);
3869}
8c1a6a84 3870
990ba854 3871
06a2c219 3872/* Draw stretch glyph string S. */
dc43ef94 3873
06a2c219
GM
3874static void
3875x_draw_stretch_glyph_string (s)
3876 struct glyph_string *s;
3877{
3878 xassert (s->first_glyph->type == STRETCH_GLYPH);
3879 s->stippled_p = s->face->stipple != 0;
990ba854 3880
06a2c219
GM
3881 if (s->hl == DRAW_CURSOR
3882 && !x_stretch_cursor_p)
3883 {
3884 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3885 as wide as the stretch glyph. */
3886 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3887
06a2c219
GM
3888 /* Draw cursor. */
3889 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3890
06a2c219
GM
3891 /* Clear rest using the GC of the original non-cursor face. */
3892 if (width < s->background_width)
3893 {
3894 GC gc = s->face->gc;
3895 int x = s->x + width, y = s->y;
3896 int w = s->background_width - width, h = s->height;
3897 XRectangle r;
dc43ef94 3898
06a2c219
GM
3899 x_get_glyph_string_clip_rect (s, &r);
3900 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3901
06a2c219
GM
3902 if (s->face->stipple)
3903 {
3904 /* Fill background with a stipple pattern. */
3905 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3906 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3907 XSetFillStyle (s->display, gc, FillSolid);
3908 }
3909 else
3910 {
3911 XGCValues xgcv;
3912 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3913 XSetForeground (s->display, gc, xgcv.background);
3914 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3915 XSetForeground (s->display, gc, xgcv.foreground);
3916 }
3917 }
3918 }
3919 else
3920 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3921 s->height);
3922
3923 s->background_filled_p = 1;
3924}
3925
3926
3927/* Draw glyph string S. */
3928
3929static void
3930x_draw_glyph_string (s)
3931 struct glyph_string *s;
3932{
3933 /* If S draws into the background of its successor, draw the
3934 background of the successor first so that S can draw into it.
3935 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3936 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3937 {
3938 xassert (s->next->img == NULL);
3939 x_set_glyph_string_gc (s->next);
3940 x_set_glyph_string_clipping (s->next);
3941 x_draw_glyph_string_background (s->next, 1);
3942 }
97210f4e 3943
06a2c219
GM
3944 /* Set up S->gc, set clipping and draw S. */
3945 x_set_glyph_string_gc (s);
3946 x_set_glyph_string_clipping (s);
3947
3948 switch (s->first_glyph->type)
3949 {
3950 case IMAGE_GLYPH:
3951 x_draw_image_glyph_string (s);
3952 break;
3953
3954 case STRETCH_GLYPH:
3955 x_draw_stretch_glyph_string (s);
3956 break;
3957
3958 case CHAR_GLYPH:
66ac4b0e
GM
3959 if (s->for_overlaps_p)
3960 s->background_filled_p = 1;
3961 else
3962 x_draw_glyph_string_background (s, 0);
06a2c219
GM
3963 x_draw_glyph_string_foreground (s);
3964 break;
3965
b4192550
KH
3966 case COMPOSITE_GLYPH:
3967 if (s->for_overlaps_p || s->gidx > 0)
3968 s->background_filled_p = 1;
3969 else
3970 x_draw_glyph_string_background (s, 1);
3971 x_draw_composite_glyph_string_foreground (s);
3972 break;
3973
06a2c219
GM
3974 default:
3975 abort ();
3976 }
3977
66ac4b0e 3978 if (!s->for_overlaps_p)
06a2c219 3979 {
66ac4b0e
GM
3980 /* Draw underline. */
3981 if (s->face->underline_p)
3982 {
3983 unsigned long dy, h;
06a2c219 3984
66ac4b0e
GM
3985 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3986 h = 1;
3987 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3988 dy = s->height - h;
06a2c219 3989
66ac4b0e
GM
3990 if (s->face->underline_defaulted_p)
3991 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3992 s->width, h);
3993 else
3994 {
3995 XGCValues xgcv;
3996 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3997 XSetForeground (s->display, s->gc, s->face->underline_color);
3998 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3999 s->width, h);
4000 XSetForeground (s->display, s->gc, xgcv.foreground);
4001 }
dc6f92b8 4002 }
07e34cb0 4003
66ac4b0e
GM
4004 /* Draw overline. */
4005 if (s->face->overline_p)
06a2c219 4006 {
66ac4b0e
GM
4007 unsigned long dy = 0, h = 1;
4008
4009 if (s->face->overline_color_defaulted_p)
4010 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4011 s->width, h);
4012 else
4013 {
4014 XGCValues xgcv;
4015 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4016 XSetForeground (s->display, s->gc, s->face->overline_color);
4017 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4018 s->width, h);
4019 XSetForeground (s->display, s->gc, xgcv.foreground);
4020 }
06a2c219 4021 }
06a2c219 4022
66ac4b0e
GM
4023 /* Draw strike-through. */
4024 if (s->face->strike_through_p)
06a2c219 4025 {
66ac4b0e
GM
4026 unsigned long h = 1;
4027 unsigned long dy = (s->height - h) / 2;
4028
4029 if (s->face->strike_through_color_defaulted_p)
4030 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4031 s->width, h);
4032 else
4033 {
4034 XGCValues xgcv;
4035 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4036 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4037 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4038 s->width, h);
4039 XSetForeground (s->display, s->gc, xgcv.foreground);
4040 }
06a2c219 4041 }
06a2c219 4042
66ac4b0e
GM
4043 /* Draw relief. */
4044 if (s->face->box != FACE_NO_BOX)
4045 x_draw_glyph_string_box (s);
4046 }
06a2c219
GM
4047
4048 /* Reset clipping. */
4049 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4050}
07e34cb0 4051
06a2c219 4052
b4192550
KH
4053static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4054 struct face **, int));
06a2c219 4055
06a2c219 4056
b4192550
KH
4057/* Load glyph string S with a composition components specified by S->cmp.
4058 FACES is an array of faces for all components of this composition.
4059 S->gidx is the index of the first component for S.
4060 OVERLAPS_P non-zero means S should draw the foreground only, and
4061 use its lines physical height for clipping.
06a2c219 4062
b4192550 4063 Value is the index of a component not in S. */
07e34cb0 4064
b4192550
KH
4065static int
4066x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4067 struct glyph_string *s;
b4192550 4068 struct face **faces;
66ac4b0e 4069 int overlaps_p;
07e34cb0 4070{
b4192550 4071 int i;
06a2c219 4072
b4192550 4073 xassert (s);
06a2c219 4074
b4192550 4075 s->for_overlaps_p = overlaps_p;
06a2c219 4076
b4192550
KH
4077 s->face = faces[s->gidx];
4078 s->font = s->face->font;
4079 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4080
b4192550
KH
4081 /* For all glyphs of this composition, starting at the offset
4082 S->gidx, until we reach the end of the definition or encounter a
4083 glyph that requires the different face, add it to S. */
4084 ++s->nchars;
4085 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4086 ++s->nchars;
06a2c219 4087
b4192550
KH
4088 /* All glyph strings for the same composition has the same width,
4089 i.e. the width set for the first component of the composition. */
06a2c219 4090
06a2c219
GM
4091 s->width = s->first_glyph->pixel_width;
4092
4093 /* If the specified font could not be loaded, use the frame's
4094 default font, but record the fact that we couldn't load it in
4095 the glyph string so that we can draw rectangles for the
4096 characters of the glyph string. */
4097 if (s->font == NULL)
4098 {
4099 s->font_not_found_p = 1;
4100 s->font = FRAME_FONT (s->f);
4101 }
4102
4103 /* Adjust base line for subscript/superscript text. */
4104 s->ybase += s->first_glyph->voffset;
4105
4106 xassert (s->face && s->face->gc);
4107
4108 /* This glyph string must always be drawn with 16-bit functions. */
4109 s->two_byte_p = 1;
b4192550
KH
4110
4111 return s->gidx + s->nchars;
06a2c219
GM
4112}
4113
4114
b4192550 4115/* Load glyph string S with a sequence characters.
06a2c219 4116 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4117 first glyph to consider, END is the index of the last + 1.
4118 OVERLAPS_P non-zero means S should draw the foreground only, and
4119 use its lines physical height for clipping.
4120
4121 Value is the index of the first glyph not in S. */
06a2c219
GM
4122
4123static int
66ac4b0e 4124x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4125 struct glyph_string *s;
4126 int face_id;
66ac4b0e 4127 int start, end, overlaps_p;
06a2c219
GM
4128{
4129 struct glyph *glyph, *last;
4130 int voffset;
4131
06a2c219
GM
4132 xassert (s->f == XFRAME (s->w->frame));
4133 xassert (s->nchars == 0);
4134 xassert (start >= 0 && end > start);
4135
66ac4b0e 4136 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4137 glyph = s->row->glyphs[s->area] + start;
4138 last = s->row->glyphs[s->area] + end;
4139 voffset = glyph->voffset;
4140
4141 while (glyph < last
4142 && glyph->type == CHAR_GLYPH
4143 && glyph->voffset == voffset
4144 /* Same face id implies same charset, nowadays. */
43d120d8 4145 && glyph->face_id == face_id)
06a2c219
GM
4146 {
4147 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4148 s->char2b + s->nchars);
4149 if (s->char2b[s->nchars].byte2 != 0)
4150 s->two_byte_p = 1;
4151
4152 ++s->nchars;
4153 xassert (s->nchars <= end - start);
4154 s->width += glyph->pixel_width;
4155 ++glyph;
4156 }
4157
4158 s->font = s->face->font;
4159 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4160
4161 /* If the specified font could not be loaded, use the frame's font,
4162 but record the fact that we couldn't load it in
4163 S->font_not_found_p so that we can draw rectangles for the
4164 characters of the glyph string. */
4165 if (s->font == NULL)
4166 {
4167 s->font_not_found_p = 1;
4168 s->font = FRAME_FONT (s->f);
4169 }
4170
4171 /* Adjust base line for subscript/superscript text. */
4172 s->ybase += voffset;
66ac4b0e 4173
06a2c219
GM
4174 xassert (s->face && s->face->gc);
4175 return glyph - s->row->glyphs[s->area];
07e34cb0 4176}
dc6f92b8 4177
06a2c219
GM
4178
4179/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4180
dfcf069d 4181static void
06a2c219
GM
4182x_fill_image_glyph_string (s)
4183 struct glyph_string *s;
4184{
4185 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4186 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4187 xassert (s->img);
43d120d8 4188 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4189 s->font = s->face->font;
4190 s->width = s->first_glyph->pixel_width;
4191
4192 /* Adjust base line for subscript/superscript text. */
4193 s->ybase += s->first_glyph->voffset;
4194}
4195
4196
4197/* Fill glyph string S from stretch glyph S->first_glyph. */
4198
4199static void
4200x_fill_stretch_glyph_string (s)
4201 struct glyph_string *s;
4202{
4203 xassert (s->first_glyph->type == STRETCH_GLYPH);
43d120d8 4204 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4205 s->font = s->face->font;
4206 s->width = s->first_glyph->pixel_width;
4207
4208 /* Adjust base line for subscript/superscript text. */
4209 s->ybase += s->first_glyph->voffset;
4210}
4211
4212
4213/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4214 of XChar2b structures for S; it can't be allocated in
4215 x_init_glyph_string because it must be allocated via `alloca'. W
4216 is the window on which S is drawn. ROW and AREA are the glyph row
4217 and area within the row from which S is constructed. START is the
4218 index of the first glyph structure covered by S. HL is a
4219 face-override for drawing S. */
4220
4221static void
4222x_init_glyph_string (s, char2b, w, row, area, start, hl)
4223 struct glyph_string *s;
4224 XChar2b *char2b;
4225 struct window *w;
4226 struct glyph_row *row;
4227 enum glyph_row_area area;
4228 int start;
4229 enum draw_glyphs_face hl;
4230{
4231 bzero (s, sizeof *s);
4232 s->w = w;
4233 s->f = XFRAME (w->frame);
4234 s->display = FRAME_X_DISPLAY (s->f);
4235 s->window = FRAME_X_WINDOW (s->f);
4236 s->char2b = char2b;
4237 s->hl = hl;
4238 s->row = row;
4239 s->area = area;
4240 s->first_glyph = row->glyphs[area] + start;
4241 s->height = row->height;
4242 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4243
9ea173e8
GM
4244 /* Display the internal border below the tool-bar window. */
4245 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4246 s->y -= s->f->output_data.x->internal_border_width;
4247
4248 s->ybase = s->y + row->ascent;
4249}
4250
4251
4252/* Set background width of glyph string S. START is the index of the
4253 first glyph following S. LAST_X is the right-most x-position + 1
4254 in the drawing area. */
4255
4256static INLINE void
4257x_set_glyph_string_background_width (s, start, last_x)
4258 struct glyph_string *s;
4259 int start;
4260 int last_x;
4261{
4262 /* If the face of this glyph string has to be drawn to the end of
4263 the drawing area, set S->extends_to_end_of_line_p. */
4264 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4265
4266 if (start == s->row->used[s->area]
4267 && s->hl == DRAW_NORMAL_TEXT
4268 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4269 || s->face->background != default_face->background
4270 || s->face->stipple != default_face->stipple))
4271 s->extends_to_end_of_line_p = 1;
4272
4273 /* If S extends its face to the end of the line, set its
4274 background_width to the distance to the right edge of the drawing
4275 area. */
4276 if (s->extends_to_end_of_line_p)
1da3fd71 4277 s->background_width = last_x - s->x + 1;
06a2c219
GM
4278 else
4279 s->background_width = s->width;
4280}
4281
4282
4283/* Add a glyph string for a stretch glyph to the list of strings
4284 between HEAD and TAIL. START is the index of the stretch glyph in
4285 row area AREA of glyph row ROW. END is the index of the last glyph
4286 in that glyph row area. X is the current output position assigned
4287 to the new glyph string constructed. HL overrides that face of the
4288 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4289 is the right-most x-position of the drawing area. */
4290
8abee2e1
DL
4291/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4292 and below -- keep them on one line. */
4293#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4294 do \
4295 { \
4296 s = (struct glyph_string *) alloca (sizeof *s); \
4297 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4298 x_fill_stretch_glyph_string (s); \
4299 x_append_glyph_string (&HEAD, &TAIL, s); \
4300 ++START; \
4301 s->x = (X); \
4302 } \
4303 while (0)
4304
4305
4306/* Add a glyph string for an image glyph to the list of strings
4307 between HEAD and TAIL. START is the index of the image glyph in
4308 row area AREA of glyph row ROW. END is the index of the last glyph
4309 in that glyph row area. X is the current output position assigned
4310 to the new glyph string constructed. HL overrides that face of the
4311 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4312 is the right-most x-position of the drawing area. */
4313
8abee2e1 4314#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4315 do \
4316 { \
4317 s = (struct glyph_string *) alloca (sizeof *s); \
4318 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4319 x_fill_image_glyph_string (s); \
4320 x_append_glyph_string (&HEAD, &TAIL, s); \
4321 ++START; \
4322 s->x = (X); \
4323 } \
4324 while (0)
4325
4326
4327/* Add a glyph string for a sequence of character glyphs to the list
4328 of strings between HEAD and TAIL. START is the index of the first
4329 glyph in row area AREA of glyph row ROW that is part of the new
4330 glyph string. END is the index of the last glyph in that glyph row
4331 area. X is the current output position assigned to the new glyph
4332 string constructed. HL overrides that face of the glyph; e.g. it
4333 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4334 right-most x-position of the drawing area. */
4335
8abee2e1 4336#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4337 do \
4338 { \
4339 int c, charset, face_id; \
4340 XChar2b *char2b; \
4341 \
43d120d8 4342 c = (ROW)->glyphs[AREA][START].u.ch; \
06a2c219 4343 charset = CHAR_CHARSET (c); \
43d120d8 4344 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4345 \
b4192550
KH
4346 s = (struct glyph_string *) alloca (sizeof *s); \
4347 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4348 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4349 x_append_glyph_string (&HEAD, &TAIL, s); \
4350 s->charset = charset; \
4351 s->x = (X); \
4352 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4353 OVERLAPS_P); \
06a2c219
GM
4354 } \
4355 while (0)
4356
4357
b4192550
KH
4358/* Add a glyph string for a composite sequence to the list of strings
4359 between HEAD and TAIL. START is the index of the first glyph in
4360 row area AREA of glyph row ROW that is part of the new glyph
4361 string. END is the index of the last glyph in that glyph row area.
4362 X is the current output position assigned to the new glyph string
4363 constructed. HL overrides that face of the glyph; e.g. it is
4364 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4365 x-position of the drawing area. */
4366
6c27ec25 4367#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4368 do { \
43d120d8
KH
4369 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4370 int face_id = (ROW)->glyphs[AREA][START].face_id; \
b4192550
KH
4371 struct composition *cmp = composition_table[cmp_id]; \
4372 int glyph_len = cmp->glyph_len; \
4373 XChar2b *char2b; \
4374 struct face **faces; \
4375 struct glyph_string *first_s = NULL; \
4376 int n; \
4377 \
4378 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4379 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4380 /* At first, fill in `char2b' and `faces'. */ \
4381 for (n = 0; n < glyph_len; n++) \
4382 { \
43d120d8 4383 int c = COMPOSITION_GLYPH (cmp, n); \
b4192550
KH
4384 faces[n] = x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4385 face_id, char2b + n, 1); \
4386 } \
4387 \
4388 /* Make glyph_strings for each glyph sequence that is drawable by \
4389 the same face, and append them to HEAD/TAIL. */ \
4390 for (n = 0; n < cmp->glyph_len;) \
4391 { \
4392 s = (struct glyph_string *) alloca (sizeof *s); \
4393 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4394 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4395 s->cmp = cmp; \
4396 s->gidx = n; \
4397 s->charset = 0; \
4398 s->x = (X); \
4399 \
4400 if (n == 0) \
4401 first_s = s; \
4402 \
4403 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4404 } \
4405 \
4406 ++START; \
4407 s = first_s; \
4408 } while (0)
4409
4410
06a2c219
GM
4411/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4412 of AREA of glyph row ROW on window W between indices START and END.
4413 HL overrides the face for drawing glyph strings, e.g. it is
4414 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4415 x-positions of the drawing area.
4416
4417 This is an ugly monster macro construct because we must use alloca
4418 to allocate glyph strings (because x_draw_glyphs can be called
4419 asynchronously). */
4420
8abee2e1 4421#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4422 do \
4423 { \
4424 HEAD = TAIL = NULL; \
4425 while (START < END) \
4426 { \
4427 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4428 switch (first_glyph->type) \
4429 { \
4430 case CHAR_GLYPH: \
4431 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4432 TAIL, HL, X, LAST_X, \
4433 OVERLAPS_P); \
06a2c219
GM
4434 break; \
4435 \
b4192550
KH
4436 case COMPOSITE_GLYPH: \
4437 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4438 HEAD, TAIL, HL, X, LAST_X,\
4439 OVERLAPS_P); \
4440 break; \
4441 \
06a2c219
GM
4442 case STRETCH_GLYPH: \
4443 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4444 HEAD, TAIL, HL, X, LAST_X); \
4445 break; \
4446 \
4447 case IMAGE_GLYPH: \
4448 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4449 TAIL, HL, X, LAST_X); \
4450 break; \
4451 \
4452 default: \
4453 abort (); \
4454 } \
4455 \
4456 x_set_glyph_string_background_width (s, START, LAST_X); \
4457 (X) += s->width; \
4458 } \
4459 } \
4460 while (0)
4461
4462
4463/* Draw glyphs between START and END in AREA of ROW on window W,
4464 starting at x-position X. X is relative to AREA in W. HL is a
4465 face-override with the following meaning:
4466
4467 DRAW_NORMAL_TEXT draw normally
4468 DRAW_CURSOR draw in cursor face
4469 DRAW_MOUSE_FACE draw in mouse face.
4470 DRAW_INVERSE_VIDEO draw in mode line face
4471 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4472 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4473
4474 If REAL_START is non-null, return in *REAL_START the real starting
4475 position for display. This can be different from START in case
4476 overlapping glyphs must be displayed. If REAL_END is non-null,
4477 return in *REAL_END the real end position for display. This can be
4478 different from END in case overlapping glyphs must be displayed.
4479
66ac4b0e
GM
4480 If OVERLAPS_P is non-zero, draw only the foreground of characters
4481 and clip to the physical height of ROW.
4482
06a2c219
GM
4483 Value is the x-position reached, relative to AREA of W. */
4484
4485static int
66ac4b0e
GM
4486x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4487 overlaps_p)
06a2c219
GM
4488 struct window *w;
4489 int x;
4490 struct glyph_row *row;
4491 enum glyph_row_area area;
4492 int start, end;
4493 enum draw_glyphs_face hl;
4494 int *real_start, *real_end;
66ac4b0e 4495 int overlaps_p;
dc6f92b8 4496{
06a2c219
GM
4497 struct glyph_string *head, *tail;
4498 struct glyph_string *s;
4499 int last_x, area_width;
4500 int x_reached;
4501 int i, j;
4502
4503 /* Let's rather be paranoid than getting a SEGV. */
4504 start = max (0, start);
4505 end = min (end, row->used[area]);
4506 if (real_start)
4507 *real_start = start;
4508 if (real_end)
4509 *real_end = end;
4510
4511 /* Translate X to frame coordinates. Set last_x to the right
4512 end of the drawing area. */
4513 if (row->full_width_p)
4514 {
4515 /* X is relative to the left edge of W, without scroll bars
4516 or flag areas. */
4517 struct frame *f = XFRAME (w->frame);
110859fc 4518 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4519 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4520
06a2c219
GM
4521 x += window_left_x;
4522 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4523 last_x = window_left_x + area_width;
4524
4525 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4526 {
110859fc 4527 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4528 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4529 last_x += width;
4530 else
4531 x -= width;
4532 }
dc6f92b8 4533
b9432a85
GM
4534 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4535 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4536 }
4537 else
dc6f92b8 4538 {
06a2c219
GM
4539 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4540 area_width = window_box_width (w, area);
4541 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4542 }
4543
06a2c219
GM
4544 /* Build a doubly-linked list of glyph_string structures between
4545 head and tail from what we have to draw. Note that the macro
4546 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4547 the reason we use a separate variable `i'. */
4548 i = start;
66ac4b0e
GM
4549 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4550 overlaps_p);
06a2c219
GM
4551 if (tail)
4552 x_reached = tail->x + tail->background_width;
4553 else
4554 x_reached = x;
90e65f07 4555
06a2c219
GM
4556 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4557 the row, redraw some glyphs in front or following the glyph
4558 strings built above. */
66ac4b0e 4559 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4560 {
4561 int dummy_x = 0;
4562 struct glyph_string *h, *t;
4563
4564 /* Compute overhangs for all glyph strings. */
4565 for (s = head; s; s = s->next)
4566 x_compute_glyph_string_overhangs (s);
4567
4568 /* Prepend glyph strings for glyphs in front of the first glyph
4569 string that are overwritten because of the first glyph
4570 string's left overhang. The background of all strings
4571 prepended must be drawn because the first glyph string
4572 draws over it. */
4573 i = x_left_overwritten (head);
4574 if (i >= 0)
4575 {
4576 j = i;
4577 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4578 DRAW_NORMAL_TEXT, dummy_x, last_x,
4579 overlaps_p);
06a2c219
GM
4580 start = i;
4581 if (real_start)
4582 *real_start = start;
4583 x_compute_overhangs_and_x (t, head->x, 1);
4584 x_prepend_glyph_string_lists (&head, &tail, h, t);
4585 }
58769bee 4586
06a2c219
GM
4587 /* Prepend glyph strings for glyphs in front of the first glyph
4588 string that overwrite that glyph string because of their
4589 right overhang. For these strings, only the foreground must
4590 be drawn, because it draws over the glyph string at `head'.
4591 The background must not be drawn because this would overwrite
4592 right overhangs of preceding glyphs for which no glyph
4593 strings exist. */
4594 i = x_left_overwriting (head);
4595 if (i >= 0)
4596 {
4597 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4598 DRAW_NORMAL_TEXT, dummy_x, last_x,
4599 overlaps_p);
06a2c219
GM
4600 for (s = h; s; s = s->next)
4601 s->background_filled_p = 1;
4602 if (real_start)
4603 *real_start = i;
4604 x_compute_overhangs_and_x (t, head->x, 1);
4605 x_prepend_glyph_string_lists (&head, &tail, h, t);
4606 }
dbcb258a 4607
06a2c219
GM
4608 /* Append glyphs strings for glyphs following the last glyph
4609 string tail that are overwritten by tail. The background of
4610 these strings has to be drawn because tail's foreground draws
4611 over it. */
4612 i = x_right_overwritten (tail);
4613 if (i >= 0)
4614 {
4615 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4616 DRAW_NORMAL_TEXT, x, last_x,
4617 overlaps_p);
06a2c219
GM
4618 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4619 x_append_glyph_string_lists (&head, &tail, h, t);
4620 if (real_end)
4621 *real_end = i;
4622 }
dc6f92b8 4623
06a2c219
GM
4624 /* Append glyph strings for glyphs following the last glyph
4625 string tail that overwrite tail. The foreground of such
4626 glyphs has to be drawn because it writes into the background
4627 of tail. The background must not be drawn because it could
4628 paint over the foreground of following glyphs. */
4629 i = x_right_overwriting (tail);
4630 if (i >= 0)
4631 {
4632 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4633 DRAW_NORMAL_TEXT, x, last_x,
4634 overlaps_p);
06a2c219
GM
4635 for (s = h; s; s = s->next)
4636 s->background_filled_p = 1;
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 }
4642 }
58769bee 4643
06a2c219
GM
4644 /* Draw all strings. */
4645 for (s = head; s; s = s->next)
4646 x_draw_glyph_string (s);
dc6f92b8 4647
06a2c219
GM
4648 /* Value is the x-position up to which drawn, relative to AREA of W.
4649 This doesn't include parts drawn because of overhangs. */
4650 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4651 if (!row->full_width_p)
4652 {
4653 if (area > LEFT_MARGIN_AREA)
4654 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4655 if (area > TEXT_AREA)
4656 x_reached -= window_box_width (w, TEXT_AREA);
4657 }
4658 return x_reached;
4659}
dc6f92b8 4660
dc6f92b8 4661
66ac4b0e
GM
4662/* Fix the display of area AREA of overlapping row ROW in window W. */
4663
4664static void
4665x_fix_overlapping_area (w, row, area)
4666 struct window *w;
4667 struct glyph_row *row;
4668 enum glyph_row_area area;
4669{
4670 int i, x;
4671
4672 BLOCK_INPUT;
4673
4674 if (area == LEFT_MARGIN_AREA)
4675 x = 0;
4676 else if (area == TEXT_AREA)
4677 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4678 else
4679 x = (window_box_width (w, LEFT_MARGIN_AREA)
4680 + window_box_width (w, TEXT_AREA));
4681
4682 for (i = 0; i < row->used[area];)
4683 {
4684 if (row->glyphs[area][i].overlaps_vertically_p)
4685 {
4686 int start = i, start_x = x;
4687
4688 do
4689 {
4690 x += row->glyphs[area][i].pixel_width;
4691 ++i;
4692 }
4693 while (i < row->used[area]
4694 && row->glyphs[area][i].overlaps_vertically_p);
4695
4696 x_draw_glyphs (w, start_x, row, area, start, i,
4697 (row->inverse_p
4698 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4699 NULL, NULL, 1);
4700 }
4701 else
4702 {
4703 x += row->glyphs[area][i].pixel_width;
4704 ++i;
4705 }
4706 }
4707
4708 UNBLOCK_INPUT;
4709}
4710
4711
06a2c219
GM
4712/* Output LEN glyphs starting at START at the nominal cursor position.
4713 Advance the nominal cursor over the text. The global variable
4714 updated_window contains the window being updated, updated_row is
4715 the glyph row being updated, and updated_area is the area of that
4716 row being updated. */
dc6f92b8 4717
06a2c219
GM
4718static void
4719x_write_glyphs (start, len)
4720 struct glyph *start;
4721 int len;
4722{
4723 int x, hpos, real_start, real_end;
d9cdbb3d 4724
06a2c219 4725 xassert (updated_window && updated_row);
dc6f92b8 4726 BLOCK_INPUT;
06a2c219
GM
4727
4728 /* Write glyphs. */
dc6f92b8 4729
06a2c219
GM
4730 hpos = start - updated_row->glyphs[updated_area];
4731 x = x_draw_glyphs (updated_window, output_cursor.x,
4732 updated_row, updated_area,
4733 hpos, hpos + len,
4734 (updated_row->inverse_p
4735 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4736 &real_start, &real_end, 0);
b30ec466 4737
06a2c219
GM
4738 /* If we drew over the cursor, note that it is not visible any more. */
4739 note_overwritten_text_cursor (updated_window, real_start,
4740 real_end - real_start);
dc6f92b8
JB
4741
4742 UNBLOCK_INPUT;
06a2c219
GM
4743
4744 /* Advance the output cursor. */
4745 output_cursor.hpos += len;
4746 output_cursor.x = x;
dc6f92b8
JB
4747}
4748
0cdd0c9f 4749
06a2c219 4750/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4751
06a2c219
GM
4752static void
4753x_insert_glyphs (start, len)
4754 struct glyph *start;
4755 register int len;
4756{
4757 struct frame *f;
4758 struct window *w;
4759 int line_height, shift_by_width, shifted_region_width;
4760 struct glyph_row *row;
4761 struct glyph *glyph;
4762 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4763
06a2c219 4764 xassert (updated_window && updated_row);
0cdd0c9f 4765 BLOCK_INPUT;
06a2c219
GM
4766 w = updated_window;
4767 f = XFRAME (WINDOW_FRAME (w));
4768
4769 /* Get the height of the line we are in. */
4770 row = updated_row;
4771 line_height = row->height;
4772
4773 /* Get the width of the glyphs to insert. */
4774 shift_by_width = 0;
4775 for (glyph = start; glyph < start + len; ++glyph)
4776 shift_by_width += glyph->pixel_width;
4777
4778 /* Get the width of the region to shift right. */
4779 shifted_region_width = (window_box_width (w, updated_area)
4780 - output_cursor.x
4781 - shift_by_width);
4782
4783 /* Shift right. */
4784 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4785 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4786 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4787 f->output_data.x->normal_gc,
4788 frame_x, frame_y,
4789 shifted_region_width, line_height,
4790 frame_x + shift_by_width, frame_y);
4791
4792 /* Write the glyphs. */
4793 hpos = start - row->glyphs[updated_area];
4794 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4795 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4796 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4797
4798 /* Advance the output cursor. */
4799 output_cursor.hpos += len;
4800 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4801 UNBLOCK_INPUT;
4802}
0cdd0c9f 4803
0cdd0c9f 4804
06a2c219
GM
4805/* Delete N glyphs at the nominal cursor position. Not implemented
4806 for X frames. */
c83febd7
RS
4807
4808static void
06a2c219
GM
4809x_delete_glyphs (n)
4810 register int n;
c83febd7 4811{
06a2c219 4812 abort ();
c83febd7
RS
4813}
4814
0cdd0c9f 4815
06a2c219
GM
4816/* Erase the current text line from the nominal cursor position
4817 (inclusive) to pixel column TO_X (exclusive). The idea is that
4818 everything from TO_X onward is already erased.
4819
4820 TO_X is a pixel position relative to updated_area of
4821 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4822
06a2c219
GM
4823static void
4824x_clear_end_of_line (to_x)
4825 int to_x;
4826{
4827 struct frame *f;
4828 struct window *w = updated_window;
4829 int max_x, min_y, max_y;
4830 int from_x, from_y, to_y;
4831
4832 xassert (updated_window && updated_row);
4833 f = XFRAME (w->frame);
4834
4835 if (updated_row->full_width_p)
4836 {
4837 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4838 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4839 && !w->pseudo_window_p)
4840 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4841 }
06a2c219
GM
4842 else
4843 max_x = window_box_width (w, updated_area);
4844 max_y = window_text_bottom_y (w);
dc6f92b8 4845
06a2c219
GM
4846 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4847 of window. For TO_X > 0, truncate to end of drawing area. */
4848 if (to_x == 0)
4849 return;
4850 else if (to_x < 0)
4851 to_x = max_x;
4852 else
4853 to_x = min (to_x, max_x);
dbc4e1c1 4854
06a2c219
GM
4855 to_y = min (max_y, output_cursor.y + updated_row->height);
4856
4857 /* Notice if the cursor will be cleared by this operation. */
4858 if (!updated_row->full_width_p)
4859 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4860
06a2c219
GM
4861 from_x = output_cursor.x;
4862
4863 /* Translate to frame coordinates. */
4864 if (updated_row->full_width_p)
4865 {
4866 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4867 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4868 }
0cdd0c9f
RS
4869 else
4870 {
06a2c219
GM
4871 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4872 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4873 }
4874
045dee35 4875 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4876 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4877 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4878
4879 /* Prevent inadvertently clearing to end of the X window. */
4880 if (to_x > from_x && to_y > from_y)
4881 {
4882 BLOCK_INPUT;
4883 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4884 from_x, from_y, to_x - from_x, to_y - from_y,
4885 False);
4886 UNBLOCK_INPUT;
0cdd0c9f 4887 }
0cdd0c9f 4888}
dbc4e1c1 4889
0cdd0c9f 4890
06a2c219 4891/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4892 frame. Otherwise clear the selected frame. */
06a2c219
GM
4893
4894static void
4895x_clear_frame ()
0cdd0c9f 4896{
06a2c219 4897 struct frame *f;
0cdd0c9f 4898
06a2c219
GM
4899 if (updating_frame)
4900 f = updating_frame;
0cdd0c9f 4901 else
b86bd3dd 4902 f = SELECTED_FRAME ();
58769bee 4903
06a2c219
GM
4904 /* Clearing the frame will erase any cursor, so mark them all as no
4905 longer visible. */
4906 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4907 output_cursor.hpos = output_cursor.vpos = 0;
4908 output_cursor.x = -1;
4909
4910 /* We don't set the output cursor here because there will always
4911 follow an explicit cursor_to. */
4912 BLOCK_INPUT;
4913 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4914
4915 /* We have to clear the scroll bars, too. If we have changed
4916 colors or something like that, then they should be notified. */
4917 x_scroll_bar_clear (f);
0cdd0c9f 4918
06a2c219
GM
4919 XFlush (FRAME_X_DISPLAY (f));
4920 UNBLOCK_INPUT;
dc6f92b8 4921}
06a2c219
GM
4922
4923
dc6f92b8 4924\f
dbc4e1c1
JB
4925/* Invert the middle quarter of the frame for .15 sec. */
4926
06a2c219
GM
4927/* We use the select system call to do the waiting, so we have to make
4928 sure it's available. If it isn't, we just won't do visual bells. */
4929
dbc4e1c1
JB
4930#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4931
06a2c219
GM
4932
4933/* Subtract the `struct timeval' values X and Y, storing the result in
4934 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4935
4936static int
4937timeval_subtract (result, x, y)
4938 struct timeval *result, x, y;
4939{
06a2c219
GM
4940 /* Perform the carry for the later subtraction by updating y. This
4941 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4942 if (x.tv_usec < y.tv_usec)
4943 {
4944 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4945 y.tv_usec -= 1000000 * nsec;
4946 y.tv_sec += nsec;
4947 }
06a2c219 4948
dbc4e1c1
JB
4949 if (x.tv_usec - y.tv_usec > 1000000)
4950 {
4951 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4952 y.tv_usec += 1000000 * nsec;
4953 y.tv_sec -= nsec;
4954 }
4955
06a2c219
GM
4956 /* Compute the time remaining to wait. tv_usec is certainly
4957 positive. */
dbc4e1c1
JB
4958 result->tv_sec = x.tv_sec - y.tv_sec;
4959 result->tv_usec = x.tv_usec - y.tv_usec;
4960
06a2c219
GM
4961 /* Return indication of whether the result should be considered
4962 negative. */
dbc4e1c1
JB
4963 return x.tv_sec < y.tv_sec;
4964}
dc6f92b8 4965
dfcf069d 4966void
f676886a
JB
4967XTflash (f)
4968 struct frame *f;
dc6f92b8 4969{
dbc4e1c1 4970 BLOCK_INPUT;
dc6f92b8 4971
dbc4e1c1
JB
4972 {
4973 GC gc;
dc6f92b8 4974
06a2c219
GM
4975 /* Create a GC that will use the GXxor function to flip foreground
4976 pixels into background pixels. */
dbc4e1c1
JB
4977 {
4978 XGCValues values;
dc6f92b8 4979
dbc4e1c1 4980 values.function = GXxor;
7556890b
RS
4981 values.foreground = (f->output_data.x->foreground_pixel
4982 ^ f->output_data.x->background_pixel);
58769bee 4983
334208b7 4984 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
4985 GCFunction | GCForeground, &values);
4986 }
dc6f92b8 4987
dbc4e1c1 4988 {
e84e14c3
RS
4989 /* Get the height not including a menu bar widget. */
4990 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4991 /* Height of each line to flash. */
4992 int flash_height = FRAME_LINE_HEIGHT (f);
4993 /* These will be the left and right margins of the rectangles. */
4994 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4995 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4996
4997 int width;
4998
4999 /* Don't flash the area between a scroll bar and the frame
5000 edge it is next to. */
5001 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5002 {
5003 case vertical_scroll_bar_left:
5004 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5005 break;
5006
5007 case vertical_scroll_bar_right:
5008 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5009 break;
06a2c219
GM
5010
5011 default:
5012 break;
e84e14c3
RS
5013 }
5014
5015 width = flash_right - flash_left;
5016
5017 /* If window is tall, flash top and bottom line. */
5018 if (height > 3 * FRAME_LINE_HEIGHT (f))
5019 {
5020 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5021 flash_left,
5022 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5023 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5024 width, flash_height);
5025 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5026 flash_left,
5027 (height - flash_height
5028 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5029 width, flash_height);
5030 }
5031 else
5032 /* If it is short, flash it all. */
5033 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5034 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5035 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5036
06a2c219 5037 x_flush (f);
dc6f92b8 5038
dbc4e1c1 5039 {
06a2c219 5040 struct timeval wakeup;
dc6f92b8 5041
66c30ea1 5042 EMACS_GET_TIME (wakeup);
dc6f92b8 5043
dbc4e1c1
JB
5044 /* Compute time to wait until, propagating carry from usecs. */
5045 wakeup.tv_usec += 150000;
5046 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5047 wakeup.tv_usec %= 1000000;
5048
5049 /* Keep waiting until past the time wakeup. */
5050 while (1)
5051 {
5052 struct timeval timeout;
5053
66c30ea1 5054 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5055
5056 /* In effect, timeout = wakeup - timeout.
5057 Break if result would be negative. */
5058 if (timeval_subtract (&timeout, wakeup, timeout))
5059 break;
5060
5061 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5062 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5063 }
5064 }
58769bee 5065
e84e14c3
RS
5066 /* If window is tall, flash top and bottom line. */
5067 if (height > 3 * FRAME_LINE_HEIGHT (f))
5068 {
5069 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5070 flash_left,
5071 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5072 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5073 width, flash_height);
5074 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5075 flash_left,
5076 (height - flash_height
5077 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5078 width, flash_height);
5079 }
5080 else
5081 /* If it is short, flash it all. */
5082 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5083 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5084 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5085
334208b7 5086 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5087 x_flush (f);
dc6f92b8 5088 }
dbc4e1c1
JB
5089 }
5090
5091 UNBLOCK_INPUT;
dc6f92b8
JB
5092}
5093
06a2c219 5094#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5095
5096
dc6f92b8
JB
5097/* Make audible bell. */
5098
dfcf069d 5099void
dc6f92b8
JB
5100XTring_bell ()
5101{
b86bd3dd
GM
5102 struct frame *f = SELECTED_FRAME ();
5103
5104 if (FRAME_X_DISPLAY (f))
5105 {
dbc4e1c1 5106#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5107 if (visible_bell)
5108 XTflash (f);
5109 else
dbc4e1c1 5110#endif
b86bd3dd
GM
5111 {
5112 BLOCK_INPUT;
5113 XBell (FRAME_X_DISPLAY (f), 0);
5114 XFlush (FRAME_X_DISPLAY (f));
5115 UNBLOCK_INPUT;
5116 }
dc6f92b8
JB
5117 }
5118}
06a2c219 5119
dc6f92b8 5120\f
06a2c219
GM
5121/* Specify how many text lines, from the top of the window,
5122 should be affected by insert-lines and delete-lines operations.
5123 This, and those operations, are used only within an update
5124 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5125
dfcf069d 5126static void
06a2c219
GM
5127XTset_terminal_window (n)
5128 register int n;
dc6f92b8 5129{
06a2c219 5130 /* This function intentionally left blank. */
dc6f92b8
JB
5131}
5132
06a2c219
GM
5133
5134\f
5135/***********************************************************************
5136 Line Dance
5137 ***********************************************************************/
5138
5139/* Perform an insert-lines or delete-lines operation, inserting N
5140 lines or deleting -N lines at vertical position VPOS. */
5141
dfcf069d 5142static void
06a2c219
GM
5143x_ins_del_lines (vpos, n)
5144 int vpos, n;
dc6f92b8
JB
5145{
5146 abort ();
5147}
06a2c219
GM
5148
5149
5150/* Scroll part of the display as described by RUN. */
dc6f92b8 5151
dfcf069d 5152static void
06a2c219
GM
5153x_scroll_run (w, run)
5154 struct window *w;
5155 struct run *run;
dc6f92b8 5156{
06a2c219
GM
5157 struct frame *f = XFRAME (w->frame);
5158 int x, y, width, height, from_y, to_y, bottom_y;
5159
5160 /* Get frame-relative bounding box of the text display area of W,
5161 without mode lines. Include in this box the flags areas to the
5162 left and right of W. */
5163 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5164 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5165 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5166
5167 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5168 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5169 bottom_y = y + height;
dc6f92b8 5170
06a2c219
GM
5171 if (to_y < from_y)
5172 {
5173 /* Scrolling up. Make sure we don't copy part of the mode
5174 line at the bottom. */
5175 if (from_y + run->height > bottom_y)
5176 height = bottom_y - from_y;
5177 else
5178 height = run->height;
5179 }
dc6f92b8 5180 else
06a2c219
GM
5181 {
5182 /* Scolling down. Make sure we don't copy over the mode line.
5183 at the bottom. */
5184 if (to_y + run->height > bottom_y)
5185 height = bottom_y - to_y;
5186 else
5187 height = run->height;
5188 }
7a13e894 5189
06a2c219
GM
5190 BLOCK_INPUT;
5191
5192 /* Cursor off. Will be switched on again in x_update_window_end. */
5193 updated_window = w;
5194 x_clear_cursor (w);
5195
5196 XCopyArea (FRAME_X_DISPLAY (f),
5197 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5198 f->output_data.x->normal_gc,
5199 x, from_y,
5200 width, height,
5201 x, to_y);
5202
5203 UNBLOCK_INPUT;
5204}
dc6f92b8 5205
dc6f92b8 5206
06a2c219
GM
5207\f
5208/***********************************************************************
5209 Exposure Events
5210 ***********************************************************************/
5211
5212/* Redisplay an exposed area of frame F. X and Y are the upper-left
5213 corner of the exposed rectangle. W and H are width and height of
5214 the exposed area. All are pixel values. W or H zero means redraw
5215 the entire frame. */
dc6f92b8 5216
06a2c219
GM
5217static void
5218expose_frame (f, x, y, w, h)
5219 struct frame *f;
5220 int x, y, w, h;
dc6f92b8 5221{
06a2c219 5222 XRectangle r;
dc6f92b8 5223
06a2c219 5224 TRACE ((stderr, "expose_frame "));
dc6f92b8 5225
06a2c219
GM
5226 /* No need to redraw if frame will be redrawn soon. */
5227 if (FRAME_GARBAGED_P (f))
dc6f92b8 5228 {
06a2c219
GM
5229 TRACE ((stderr, " garbaged\n"));
5230 return;
5231 }
5232
5233 /* If basic faces haven't been realized yet, there is no point in
5234 trying to redraw anything. This can happen when we get an expose
5235 event while Emacs is starting, e.g. by moving another window. */
5236 if (FRAME_FACE_CACHE (f) == NULL
5237 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5238 {
5239 TRACE ((stderr, " no faces\n"));
5240 return;
58769bee 5241 }
06a2c219
GM
5242
5243 if (w == 0 || h == 0)
58769bee 5244 {
06a2c219
GM
5245 r.x = r.y = 0;
5246 r.width = CANON_X_UNIT (f) * f->width;
5247 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5248 }
5249 else
5250 {
06a2c219
GM
5251 r.x = x;
5252 r.y = y;
5253 r.width = w;
5254 r.height = h;
5255 }
5256
5257 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5258 expose_window_tree (XWINDOW (f->root_window), &r);
5259
9ea173e8 5260 if (WINDOWP (f->tool_bar_window))
06a2c219 5261 {
9ea173e8 5262 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5263 XRectangle window_rect;
5264 XRectangle intersection_rect;
5265 int window_x, window_y, window_width, window_height;
5266
5267
5268 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5269 window_rect.x = window_x;
5270 window_rect.y = window_y;
5271 window_rect.width = window_width;
5272 window_rect.height = window_height;
5273
5274 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5275 expose_window (w, &intersection_rect);
5276 }
5277
5278#ifndef USE_X_TOOLKIT
5279 if (WINDOWP (f->menu_bar_window))
5280 {
5281 struct window *w = XWINDOW (f->menu_bar_window);
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);
dc6f92b8 5295 }
06a2c219 5296#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5297}
5298
06a2c219
GM
5299
5300/* Redraw (parts) of all windows in the window tree rooted at W that
5301 intersect R. R contains frame pixel coordinates. */
5302
58769bee 5303static void
06a2c219
GM
5304expose_window_tree (w, r)
5305 struct window *w;
5306 XRectangle *r;
dc6f92b8 5307{
06a2c219
GM
5308 while (w)
5309 {
5310 if (!NILP (w->hchild))
5311 expose_window_tree (XWINDOW (w->hchild), r);
5312 else if (!NILP (w->vchild))
5313 expose_window_tree (XWINDOW (w->vchild), r);
5314 else
5315 {
5316 XRectangle window_rect;
5317 XRectangle intersection_rect;
5318 struct frame *f = XFRAME (w->frame);
5319 int window_x, window_y, window_width, window_height;
5320
5321 /* Frame-relative pixel rectangle of W. */
5322 window_box (w, -1, &window_x, &window_y, &window_width,
5323 &window_height);
5324 window_rect.x
5325 = (window_x
110859fc 5326 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5327 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5328 window_rect.y = window_y;
5329 window_rect.width
5330 = (window_width
110859fc 5331 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5332 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5333 window_rect.height
5334 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5335
5336 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5337 expose_window (w, &intersection_rect);
5338 }
58769bee 5339
06a2c219
GM
5340 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5341 }
5342}
58769bee 5343
dc6f92b8 5344
06a2c219
GM
5345/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5346 which intersects rectangle R. R is in window-relative coordinates. */
5347
5348static void
5349expose_area (w, row, r, area)
5350 struct window *w;
5351 struct glyph_row *row;
5352 XRectangle *r;
5353 enum glyph_row_area area;
5354{
5355 int x;
5356 struct glyph *first = row->glyphs[area];
5357 struct glyph *end = row->glyphs[area] + row->used[area];
5358 struct glyph *last;
5359 int first_x;
5360
5361 /* Set x to the window-relative start position for drawing glyphs of
5362 AREA. The first glyph of the text area can be partially visible.
5363 The first glyphs of other areas cannot. */
5364 if (area == LEFT_MARGIN_AREA)
5365 x = 0;
5366 else if (area == TEXT_AREA)
5367 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5368 else
5369 x = (window_box_width (w, LEFT_MARGIN_AREA)
5370 + window_box_width (w, TEXT_AREA));
5371
6fb13182
GM
5372 if (area == TEXT_AREA && row->fill_line_p)
5373 /* If row extends face to end of line write the whole line. */
5374 x_draw_glyphs (w, x, row, area,
5375 0, row->used[area],
06a2c219 5376 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5377 NULL, NULL, 0);
6fb13182
GM
5378 else
5379 {
5380 /* Find the first glyph that must be redrawn. */
5381 while (first < end
5382 && x + first->pixel_width < r->x)
5383 {
5384 x += first->pixel_width;
5385 ++first;
5386 }
5387
5388 /* Find the last one. */
5389 last = first;
5390 first_x = x;
5391 while (last < end
5392 && x < r->x + r->width)
5393 {
5394 x += last->pixel_width;
5395 ++last;
5396 }
5397
5398 /* Repaint. */
5399 if (last > first)
5400 x_draw_glyphs (w, first_x, row, area,
5401 first - row->glyphs[area],
5402 last - row->glyphs[area],
5403 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5404 NULL, NULL, 0);
5405 }
06a2c219
GM
5406}
5407
58769bee 5408
06a2c219
GM
5409/* Redraw the parts of the glyph row ROW on window W intersecting
5410 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5411
06a2c219
GM
5412static void
5413expose_line (w, row, r)
5414 struct window *w;
5415 struct glyph_row *row;
5416 XRectangle *r;
5417{
5418 xassert (row->enabled_p);
5419
5420 if (row->mode_line_p || w->pseudo_window_p)
5421 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5422 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5423 NULL, NULL, 0);
06a2c219
GM
5424 else
5425 {
5426 if (row->used[LEFT_MARGIN_AREA])
5427 expose_area (w, row, r, LEFT_MARGIN_AREA);
5428 if (row->used[TEXT_AREA])
5429 expose_area (w, row, r, TEXT_AREA);
5430 if (row->used[RIGHT_MARGIN_AREA])
5431 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5432 x_draw_row_bitmaps (w, row);
5433 }
5434}
dc6f92b8 5435
58769bee 5436
06a2c219
GM
5437/* Return non-zero if W's cursor intersects rectangle R. */
5438
5439static int
5440x_phys_cursor_in_rect_p (w, r)
5441 struct window *w;
5442 XRectangle *r;
5443{
5444 XRectangle cr, result;
5445 struct glyph *cursor_glyph;
5446
5447 cursor_glyph = get_phys_cursor_glyph (w);
5448 if (cursor_glyph)
5449 {
5450 cr.x = w->phys_cursor.x;
5451 cr.y = w->phys_cursor.y;
5452 cr.width = cursor_glyph->pixel_width;
5453 cr.height = w->phys_cursor_height;
5454 return x_intersect_rectangles (&cr, r, &result);
5455 }
5456 else
5457 return 0;
dc6f92b8 5458}
dc6f92b8 5459
06a2c219
GM
5460
5461/* Redraw a rectangle of window W. R is a rectangle in window
5462 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5463
5464static void
06a2c219
GM
5465expose_window (w, r)
5466 struct window *w;
5467 XRectangle *r;
dc6f92b8 5468{
06a2c219
GM
5469 struct glyph_row *row;
5470 int y;
5471 int yb = window_text_bottom_y (w);
5472 int cursor_cleared_p;
dc6f92b8 5473
80c32bcc
GM
5474 /* If window is not yet fully initialized, do nothing. This can
5475 happen when toolkit scroll bars are used and a window is split.
5476 Reconfiguring the scroll bar will generate an expose for a newly
5477 created window. */
5478 if (w->current_matrix == NULL)
5479 return;
5480
06a2c219
GM
5481 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5482 r->x, r->y, r->width, r->height));
dc6f92b8 5483
06a2c219
GM
5484 /* Convert to window coordinates. */
5485 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5486 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5487
06a2c219
GM
5488 /* Turn off the cursor. */
5489 if (!w->pseudo_window_p
5490 && x_phys_cursor_in_rect_p (w, r))
5491 {
5492 x_clear_cursor (w);
5493 cursor_cleared_p = 1;
5494 }
5495 else
5496 cursor_cleared_p = 0;
5497
5498 /* Find the first row intersecting the rectangle R. */
5499 row = w->current_matrix->rows;
5500 y = 0;
5501 while (row->enabled_p
5502 && y < yb
5503 && y + row->height < r->y)
5504 {
5505 y += row->height;
5506 ++row;
5507 }
5508
dc6f92b8 5509 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5510 while (row->enabled_p
5511 && y < yb
5512 && y < r->y + r->height)
5513 {
5514 expose_line (w, row, r);
5515 y += row->height;
5516 ++row;
5517 }
5518
5519 /* Display the mode line if there is one. */
5520 if (WINDOW_WANTS_MODELINE_P (w)
5521 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5522 row->enabled_p)
5523 && row->y < r->y + r->height)
5524 expose_line (w, row, r);
dc6f92b8 5525
06a2c219 5526 if (!w->pseudo_window_p)
dc6f92b8 5527 {
06a2c219
GM
5528 /* Draw border between windows. */
5529 x_draw_vertical_border (w);
5530
5531 /* Turn the cursor on again. */
5532 if (cursor_cleared_p)
5533 x_update_window_cursor (w, 1);
5534 }
5535}
dc6f92b8 5536
dc6f92b8 5537
06a2c219
GM
5538/* Determine the intersection of two rectangles R1 and R2. Return
5539 the intersection in *RESULT. Value is non-zero if RESULT is not
5540 empty. */
5541
5542static int
5543x_intersect_rectangles (r1, r2, result)
5544 XRectangle *r1, *r2, *result;
5545{
5546 XRectangle *left, *right;
5547 XRectangle *upper, *lower;
5548 int intersection_p = 0;
5549
5550 /* Rearrange so that R1 is the left-most rectangle. */
5551 if (r1->x < r2->x)
5552 left = r1, right = r2;
5553 else
5554 left = r2, right = r1;
5555
5556 /* X0 of the intersection is right.x0, if this is inside R1,
5557 otherwise there is no intersection. */
5558 if (right->x <= left->x + left->width)
5559 {
5560 result->x = right->x;
5561
5562 /* The right end of the intersection is the minimum of the
5563 the right ends of left and right. */
5564 result->width = (min (left->x + left->width, right->x + right->width)
5565 - result->x);
5566
5567 /* Same game for Y. */
5568 if (r1->y < r2->y)
5569 upper = r1, lower = r2;
5570 else
5571 upper = r2, lower = r1;
5572
5573 /* The upper end of the intersection is lower.y0, if this is inside
5574 of upper. Otherwise, there is no intersection. */
5575 if (lower->y <= upper->y + upper->height)
dc43ef94 5576 {
06a2c219
GM
5577 result->y = lower->y;
5578
5579 /* The lower end of the intersection is the minimum of the lower
5580 ends of upper and lower. */
5581 result->height = (min (lower->y + lower->height,
5582 upper->y + upper->height)
5583 - result->y);
5584 intersection_p = 1;
dc43ef94 5585 }
dc6f92b8
JB
5586 }
5587
06a2c219 5588 return intersection_p;
dc6f92b8 5589}
06a2c219
GM
5590
5591
5592
5593
dc6f92b8 5594\f
dc6f92b8 5595static void
334208b7
RS
5596frame_highlight (f)
5597 struct frame *f;
dc6f92b8 5598{
b3e1e05c
JB
5599 /* We used to only do this if Vx_no_window_manager was non-nil, but
5600 the ICCCM (section 4.1.6) says that the window's border pixmap
5601 and border pixel are window attributes which are "private to the
5602 client", so we can always change it to whatever we want. */
5603 BLOCK_INPUT;
334208b7 5604 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5605 f->output_data.x->border_pixel);
b3e1e05c 5606 UNBLOCK_INPUT;
5d46f928 5607 x_update_cursor (f, 1);
dc6f92b8
JB
5608}
5609
5610static void
334208b7
RS
5611frame_unhighlight (f)
5612 struct frame *f;
dc6f92b8 5613{
b3e1e05c
JB
5614 /* We used to only do this if Vx_no_window_manager was non-nil, but
5615 the ICCCM (section 4.1.6) says that the window's border pixmap
5616 and border pixel are window attributes which are "private to the
5617 client", so we can always change it to whatever we want. */
5618 BLOCK_INPUT;
334208b7 5619 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5620 f->output_data.x->border_tile);
b3e1e05c 5621 UNBLOCK_INPUT;
5d46f928 5622 x_update_cursor (f, 1);
dc6f92b8 5623}
dc6f92b8 5624
f676886a
JB
5625/* The focus has changed. Update the frames as necessary to reflect
5626 the new situation. Note that we can't change the selected frame
c5acd733 5627 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5628 Each event gets marked with the frame in which it occurred, so the
c5acd733 5629 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5630
6d4238f3 5631static void
0f941935
KH
5632x_new_focus_frame (dpyinfo, frame)
5633 struct x_display_info *dpyinfo;
f676886a 5634 struct frame *frame;
dc6f92b8 5635{
0f941935 5636 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5637
0f941935 5638 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5639 {
58769bee 5640 /* Set this before calling other routines, so that they see
f676886a 5641 the correct value of x_focus_frame. */
0f941935 5642 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5643
5644 if (old_focus && old_focus->auto_lower)
f676886a 5645 x_lower_frame (old_focus);
dc6f92b8
JB
5646
5647#if 0
f676886a 5648 selected_frame = frame;
e0c1aef2
KH
5649 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5650 selected_frame);
f676886a
JB
5651 Fselect_window (selected_frame->selected_window);
5652 choose_minibuf_frame ();
c118dd06 5653#endif /* ! 0 */
dc6f92b8 5654
0f941935
KH
5655 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5656 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5657 else
5658 pending_autoraise_frame = 0;
6d4238f3 5659 }
dc6f92b8 5660
0f941935 5661 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5662}
5663
37c2c98b
RS
5664/* Handle an event saying the mouse has moved out of an Emacs frame. */
5665
5666void
0f941935
KH
5667x_mouse_leave (dpyinfo)
5668 struct x_display_info *dpyinfo;
37c2c98b 5669{
0f941935 5670 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5671}
6d4238f3 5672
f451eb13
JB
5673/* The focus has changed, or we have redirected a frame's focus to
5674 another frame (this happens when a frame uses a surrogate
06a2c219 5675 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5676
5677 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5678 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5679 the appropriate X display info. */
06a2c219 5680
6d4238f3 5681static void
0f941935
KH
5682XTframe_rehighlight (frame)
5683 struct frame *frame;
6d4238f3 5684{
0f941935
KH
5685 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5686}
6d4238f3 5687
0f941935
KH
5688static void
5689x_frame_rehighlight (dpyinfo)
5690 struct x_display_info *dpyinfo;
5691{
5692 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5693
5694 if (dpyinfo->x_focus_frame)
6d4238f3 5695 {
0f941935
KH
5696 dpyinfo->x_highlight_frame
5697 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5698 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5699 : dpyinfo->x_focus_frame);
5700 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5701 {
0f941935
KH
5702 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5703 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5704 }
dc6f92b8 5705 }
6d4238f3 5706 else
0f941935 5707 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5708
0f941935 5709 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5710 {
5711 if (old_highlight)
f676886a 5712 frame_unhighlight (old_highlight);
0f941935
KH
5713 if (dpyinfo->x_highlight_frame)
5714 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5715 }
dc6f92b8 5716}
06a2c219
GM
5717
5718
dc6f92b8 5719\f
06a2c219 5720/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5721
28430d3c
JB
5722/* Initialize mode_switch_bit and modifier_meaning. */
5723static void
334208b7
RS
5724x_find_modifier_meanings (dpyinfo)
5725 struct x_display_info *dpyinfo;
28430d3c 5726{
f689eb05 5727 int min_code, max_code;
28430d3c
JB
5728 KeySym *syms;
5729 int syms_per_code;
5730 XModifierKeymap *mods;
5731
334208b7
RS
5732 dpyinfo->meta_mod_mask = 0;
5733 dpyinfo->shift_lock_mask = 0;
5734 dpyinfo->alt_mod_mask = 0;
5735 dpyinfo->super_mod_mask = 0;
5736 dpyinfo->hyper_mod_mask = 0;
58769bee 5737
9658a521 5738#ifdef HAVE_X11R4
334208b7 5739 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5740#else
4a60f8c5
RS
5741 min_code = dpyinfo->display->min_keycode;
5742 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5743#endif
5744
334208b7 5745 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5746 min_code, max_code - min_code + 1,
5747 &syms_per_code);
334208b7 5748 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5749
58769bee 5750 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5751 Alt keysyms are on. */
28430d3c 5752 {
06a2c219 5753 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5754
5755 for (row = 3; row < 8; row++)
5756 for (col = 0; col < mods->max_keypermod; col++)
5757 {
0299d313
RS
5758 KeyCode code
5759 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5760
af92970c
KH
5761 /* Zeroes are used for filler. Skip them. */
5762 if (code == 0)
5763 continue;
5764
28430d3c
JB
5765 /* Are any of this keycode's keysyms a meta key? */
5766 {
5767 int code_col;
5768
5769 for (code_col = 0; code_col < syms_per_code; code_col++)
5770 {
f689eb05 5771 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5772
f689eb05 5773 switch (sym)
28430d3c 5774 {
f689eb05
JB
5775 case XK_Meta_L:
5776 case XK_Meta_R:
334208b7 5777 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5778 break;
f689eb05
JB
5779
5780 case XK_Alt_L:
5781 case XK_Alt_R:
334208b7 5782 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5783 break;
5784
5785 case XK_Hyper_L:
5786 case XK_Hyper_R:
334208b7 5787 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5788 break;
5789
5790 case XK_Super_L:
5791 case XK_Super_R:
334208b7 5792 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5793 break;
11edeb03
JB
5794
5795 case XK_Shift_Lock:
5796 /* Ignore this if it's not on the lock modifier. */
5797 if ((1 << row) == LockMask)
334208b7 5798 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5799 break;
28430d3c
JB
5800 }
5801 }
5802 }
5803 }
5804 }
5805
f689eb05 5806 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5807 if (! dpyinfo->meta_mod_mask)
a3c44b14 5808 {
334208b7
RS
5809 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5810 dpyinfo->alt_mod_mask = 0;
a3c44b14 5811 }
f689eb05 5812
148c4b70
RS
5813 /* If some keys are both alt and meta,
5814 make them just meta, not alt. */
334208b7 5815 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5816 {
334208b7 5817 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5818 }
58769bee 5819
28430d3c 5820 XFree ((char *) syms);
f689eb05 5821 XFreeModifiermap (mods);
28430d3c
JB
5822}
5823
dfeccd2d
JB
5824/* Convert between the modifier bits X uses and the modifier bits
5825 Emacs uses. */
06a2c219 5826
7c5283e4 5827static unsigned int
334208b7
RS
5828x_x_to_emacs_modifiers (dpyinfo, state)
5829 struct x_display_info *dpyinfo;
dc6f92b8
JB
5830 unsigned int state;
5831{
334208b7
RS
5832 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5833 | ((state & ControlMask) ? ctrl_modifier : 0)
5834 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5835 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5836 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5837 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5838}
5839
dfeccd2d 5840static unsigned int
334208b7
RS
5841x_emacs_to_x_modifiers (dpyinfo, state)
5842 struct x_display_info *dpyinfo;
dfeccd2d
JB
5843 unsigned int state;
5844{
334208b7
RS
5845 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5846 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5847 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5848 | ((state & shift_modifier) ? ShiftMask : 0)
5849 | ((state & ctrl_modifier) ? ControlMask : 0)
5850 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5851}
d047c4eb
KH
5852
5853/* Convert a keysym to its name. */
5854
5855char *
5856x_get_keysym_name (keysym)
5857 KeySym keysym;
5858{
5859 char *value;
5860
5861 BLOCK_INPUT;
5862 value = XKeysymToString (keysym);
5863 UNBLOCK_INPUT;
5864
5865 return value;
5866}
06a2c219
GM
5867
5868
e4571a43
JB
5869\f
5870/* Mouse clicks and mouse movement. Rah. */
e4571a43 5871
06a2c219
GM
5872/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5873 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5874 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5875 not force the value into range. */
69388238 5876
c8dba240 5877void
69388238 5878pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5879 FRAME_PTR f;
69388238 5880 register int pix_x, pix_y;
e4571a43
JB
5881 register int *x, *y;
5882 XRectangle *bounds;
69388238 5883 int noclip;
e4571a43 5884{
06a2c219 5885 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5886 even for negative values. */
5887 if (pix_x < 0)
7556890b 5888 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5889 if (pix_y < 0)
7556890b 5890 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5891
e4571a43
JB
5892 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5893 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5894
5895 if (bounds)
5896 {
7556890b
RS
5897 bounds->width = FONT_WIDTH (f->output_data.x->font);
5898 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5899 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5900 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5901 }
5902
69388238
RS
5903 if (!noclip)
5904 {
5905 if (pix_x < 0)
5906 pix_x = 0;
3cbd2e0b
RS
5907 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5908 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5909
5910 if (pix_y < 0)
5911 pix_y = 0;
5912 else if (pix_y > f->height)
5913 pix_y = f->height;
5914 }
e4571a43
JB
5915
5916 *x = pix_x;
5917 *y = pix_y;
5918}
5919
06a2c219
GM
5920
5921/* Given HPOS/VPOS in the current matrix of W, return corresponding
5922 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5923 can't tell the positions because W's display is not up to date,
5924 return 0. */
5925
5926int
5927glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5928 struct window *w;
5929 int hpos, vpos;
5930 int *frame_x, *frame_y;
2b5c9e71 5931{
06a2c219
GM
5932 int success_p;
5933
5934 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5935 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5936
5937 if (display_completed)
5938 {
5939 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5940 struct glyph *glyph = row->glyphs[TEXT_AREA];
5941 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5942
5943 *frame_y = row->y;
5944 *frame_x = row->x;
5945 while (glyph < end)
5946 {
5947 *frame_x += glyph->pixel_width;
5948 ++glyph;
5949 }
5950
5951 success_p = 1;
5952 }
5953 else
5954 {
5955 *frame_y = *frame_x = 0;
5956 success_p = 0;
5957 }
5958
5959 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5960 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5961 return success_p;
2b5c9e71
RS
5962}
5963
06a2c219 5964
dc6f92b8
JB
5965/* Prepare a mouse-event in *RESULT for placement in the input queue.
5966
5967 If the event is a button press, then note that we have grabbed
f451eb13 5968 the mouse. */
dc6f92b8
JB
5969
5970static Lisp_Object
f451eb13 5971construct_mouse_click (result, event, f)
dc6f92b8
JB
5972 struct input_event *result;
5973 XButtonEvent *event;
f676886a 5974 struct frame *f;
dc6f92b8 5975{
f451eb13 5976 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5977 otherwise. */
f451eb13 5978 result->kind = mouse_click;
69388238 5979 result->code = event->button - Button1;
1113d9db 5980 result->timestamp = event->time;
334208b7
RS
5981 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5982 event->state)
f689eb05 5983 | (event->type == ButtonRelease
58769bee 5984 ? up_modifier
f689eb05 5985 : down_modifier));
dc6f92b8 5986
06a2c219
GM
5987 XSETINT (result->x, event->x);
5988 XSETINT (result->y, event->y);
5989 XSETFRAME (result->frame_or_window, f);
5990 return Qnil;
dc6f92b8 5991}
b849c413 5992
06a2c219
GM
5993#if 0 /* This function isn't called. --gerd */
5994
b849c413
RS
5995/* Prepare a menu-event in *RESULT for placement in the input queue. */
5996
5997static Lisp_Object
5998construct_menu_click (result, event, f)
5999 struct input_event *result;
6000 XButtonEvent *event;
6001 struct frame *f;
6002{
6003 /* Make the event type no_event; we'll change that when we decide
6004 otherwise. */
6005 result->kind = mouse_click;
26459b28 6006 result->code = event->button - Button1;
b849c413 6007 result->timestamp = event->time;
334208b7
RS
6008 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6009 event->state)
b849c413 6010 | (event->type == ButtonRelease
58769bee 6011 ? up_modifier
b849c413
RS
6012 : down_modifier));
6013
e0c1aef2
KH
6014 XSETINT (result->x, event->x);
6015 XSETINT (result->y, -1);
6016 XSETFRAME (result->frame_or_window, f);
b849c413 6017}
06a2c219
GM
6018
6019#endif /* 0 */
6020
69388238 6021\f
90e65f07
JB
6022/* Function to report a mouse movement to the mainstream Emacs code.
6023 The input handler calls this.
6024
6025 We have received a mouse movement event, which is given in *event.
6026 If the mouse is over a different glyph than it was last time, tell
6027 the mainstream emacs code by setting mouse_moved. If not, ask for
6028 another motion event, so we can check again the next time it moves. */
b8009dd1 6029
06a2c219
GM
6030static XMotionEvent last_mouse_motion_event;
6031static Lisp_Object last_mouse_motion_frame;
6032
90e65f07 6033static void
12ba150f 6034note_mouse_movement (frame, event)
f676886a 6035 FRAME_PTR frame;
90e65f07 6036 XMotionEvent *event;
90e65f07 6037{
e5d77022 6038 last_mouse_movement_time = event->time;
06a2c219
GM
6039 last_mouse_motion_event = *event;
6040 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6041
27f338af
RS
6042 if (event->window != FRAME_X_WINDOW (frame))
6043 {
39d8bb4d 6044 frame->mouse_moved = 1;
27f338af 6045 last_mouse_scroll_bar = Qnil;
27f338af 6046 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6047 }
6048
90e65f07 6049 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6050 else if (event->x < last_mouse_glyph.x
6051 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6052 || event->y < last_mouse_glyph.y
6053 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6054 {
39d8bb4d 6055 frame->mouse_moved = 1;
ab648270 6056 last_mouse_scroll_bar = Qnil;
b8009dd1 6057 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6058 }
6059}
6060
bf1c0ba1 6061/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6062
06a2c219
GM
6063 int disable_mouse_highlight;
6064
6065
6066\f
6067/************************************************************************
6068 Mouse Face
6069 ************************************************************************/
6070
6071/* Find the glyph under window-relative coordinates X/Y in window W.
6072 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6073 strings. Return in *HPOS and *VPOS the row and column number of
6074 the glyph found. Return in *AREA the glyph area containing X.
6075 Value is a pointer to the glyph found or null if X/Y is not on
6076 text, or we can't tell because W's current matrix is not up to
6077 date. */
6078
6079static struct glyph *
6080x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6081 struct window *w;
6082 int x, y;
6083 int *hpos, *vpos, *area;
6084{
6085 struct glyph *glyph, *end;
6086 struct glyph_row *row;
6087 int x0, i, left_area_width;
6088
6089 /* Find row containing Y. Give up if some row is not enabled. */
6090 for (i = 0; i < w->current_matrix->nrows; ++i)
6091 {
6092 row = MATRIX_ROW (w->current_matrix, i);
6093 if (!row->enabled_p)
6094 return NULL;
6095 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6096 break;
6097 }
6098
6099 *vpos = i;
6100 *hpos = 0;
6101
6102 /* Give up if Y is not in the window. */
6103 if (i == w->current_matrix->nrows)
6104 return NULL;
6105
6106 /* Get the glyph area containing X. */
6107 if (w->pseudo_window_p)
6108 {
6109 *area = TEXT_AREA;
6110 x0 = 0;
6111 }
6112 else
6113 {
6114 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6115 if (x < left_area_width)
6116 {
6117 *area = LEFT_MARGIN_AREA;
6118 x0 = 0;
6119 }
6120 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6121 {
6122 *area = TEXT_AREA;
6123 x0 = row->x + left_area_width;
6124 }
6125 else
6126 {
6127 *area = RIGHT_MARGIN_AREA;
6128 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6129 }
6130 }
6131
6132 /* Find glyph containing X. */
6133 glyph = row->glyphs[*area];
6134 end = glyph + row->used[*area];
6135 while (glyph < end)
6136 {
6137 if (x < x0 + glyph->pixel_width)
6138 {
6139 if (w->pseudo_window_p)
6140 break;
6141 else if (BUFFERP (glyph->object))
6142 break;
6143 }
6144
6145 x0 += glyph->pixel_width;
6146 ++glyph;
6147 }
6148
6149 if (glyph == end)
6150 return NULL;
6151
6152 *hpos = glyph - row->glyphs[*area];
6153 return glyph;
6154}
6155
6156
6157/* Convert frame-relative x/y to coordinates relative to window W.
6158 Takes pseudo-windows into account. */
6159
6160static void
6161frame_to_window_pixel_xy (w, x, y)
6162 struct window *w;
6163 int *x, *y;
6164{
6165 if (w->pseudo_window_p)
6166 {
6167 /* A pseudo-window is always full-width, and starts at the
6168 left edge of the frame, plus a frame border. */
6169 struct frame *f = XFRAME (w->frame);
6170 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6171 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6172 }
6173 else
6174 {
6175 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6176 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6177 }
6178}
6179
6180
6181/* Take proper action when mouse has moved to the mode or top line of
6182 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6183 mode line. X is relative to the start of the text display area of
6184 W, so the width of bitmap areas and scroll bars must be subtracted
6185 to get a position relative to the start of the mode line. */
6186
6187static void
6188note_mode_line_highlight (w, x, mode_line_p)
6189 struct window *w;
6190 int x, mode_line_p;
6191{
6192 struct frame *f = XFRAME (w->frame);
6193 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6194 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6195 struct glyph_row *row;
6196
6197 if (mode_line_p)
6198 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6199 else
045dee35 6200 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6201
6202 if (row->enabled_p)
6203 {
6204 struct glyph *glyph, *end;
6205 Lisp_Object help, map;
6206 int x0;
6207
6208 /* Find the glyph under X. */
6209 glyph = row->glyphs[TEXT_AREA];
6210 end = glyph + row->used[TEXT_AREA];
6211 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6212 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6213 while (glyph < end
6214 && x >= x0 + glyph->pixel_width)
6215 {
6216 x0 += glyph->pixel_width;
6217 ++glyph;
6218 }
6219
6220 if (glyph < end
6221 && STRINGP (glyph->object)
6222 && XSTRING (glyph->object)->intervals
6223 && glyph->charpos >= 0
6224 && glyph->charpos < XSTRING (glyph->object)->size)
6225 {
6226 /* If we're on a string with `help-echo' text property,
6227 arrange for the help to be displayed. This is done by
6228 setting the global variable help_echo to the help string. */
6229 help = Fget_text_property (make_number (glyph->charpos),
6230 Qhelp_echo, glyph->object);
6231 if (STRINGP (help))
6232 help_echo = help;
6233
6234 /* Change the mouse pointer according to what is under X/Y. */
6235 map = Fget_text_property (make_number (glyph->charpos),
6236 Qlocal_map, glyph->object);
6237 if (!NILP (Fkeymapp (map)))
6238 cursor = f->output_data.x->nontext_cursor;
6239 }
6240 }
6241
6242 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6243}
6244
6245
6246/* Take proper action when the mouse has moved to position X, Y on
6247 frame F as regards highlighting characters that have mouse-face
6248 properties. Also de-highlighting chars where the mouse was before.
27f338af 6249 X and Y can be negative or out of range. */
b8009dd1
RS
6250
6251static void
6252note_mouse_highlight (f, x, y)
06a2c219 6253 struct frame *f;
c32cdd9a 6254 int x, y;
b8009dd1 6255{
06a2c219
GM
6256 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6257 int portion;
b8009dd1
RS
6258 Lisp_Object window;
6259 struct window *w;
6260
06a2c219
GM
6261 /* When a menu is active, don't highlight because this looks odd. */
6262#ifdef USE_X_TOOLKIT
6263 if (popup_activated ())
6264 return;
6265#endif
6266
bf1c0ba1
RS
6267 if (disable_mouse_highlight)
6268 return;
6269
06a2c219
GM
6270 dpyinfo->mouse_face_mouse_x = x;
6271 dpyinfo->mouse_face_mouse_y = y;
6272 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6273
06a2c219 6274 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6275 return;
6276
514e4681
RS
6277 if (gc_in_progress)
6278 {
06a2c219 6279 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6280 return;
6281 }
6282
b8009dd1 6283 /* Which window is that in? */
06a2c219 6284 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6285
6286 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6287 if (! EQ (window, dpyinfo->mouse_face_window))
6288 clear_mouse_face (dpyinfo);
6289
6290 /* Not on a window -> return. */
6291 if (!WINDOWP (window))
6292 return;
6293
6294 /* Convert to window-relative pixel coordinates. */
6295 w = XWINDOW (window);
6296 frame_to_window_pixel_xy (w, &x, &y);
6297
9ea173e8 6298 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6299 buffer. */
9ea173e8 6300 if (EQ (window, f->tool_bar_window))
06a2c219 6301 {
9ea173e8 6302 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6303 return;
6304 }
6305
6306 if (portion == 1 || portion == 3)
6307 {
6308 /* Mouse is on the mode or top line. */
6309 note_mode_line_highlight (w, x, portion == 1);
6310 return;
6311 }
6312 else
6313 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6314 f->output_data.x->text_cursor);
b8009dd1 6315
0cdd0c9f
RS
6316 /* Are we in a window whose display is up to date?
6317 And verify the buffer's text has not changed. */
06a2c219
GM
6318 if (/* Within text portion of the window. */
6319 portion == 0
0cdd0c9f 6320 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6321 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6322 && (XFASTINT (w->last_overlay_modified)
6323 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6324 {
06a2c219
GM
6325 int hpos, vpos, pos, i, area;
6326 struct glyph *glyph;
b8009dd1 6327
06a2c219
GM
6328 /* Find the glyph under X/Y. */
6329 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6330
6331 /* Clear mouse face if X/Y not over text. */
6332 if (glyph == NULL
6333 || area != TEXT_AREA
6334 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6335 {
06a2c219
GM
6336 clear_mouse_face (dpyinfo);
6337 return;
6338 }
6339
6340 pos = glyph->charpos;
6341 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6342
6343 /* Check for mouse-face and help-echo. */
6344 {
6345 Lisp_Object mouse_face, overlay, position;
6346 Lisp_Object *overlay_vec;
6347 int len, noverlays;
6348 struct buffer *obuf;
6349 int obegv, ozv;
6350
6351 /* If we get an out-of-range value, return now; avoid an error. */
6352 if (pos > BUF_Z (XBUFFER (w->buffer)))
6353 return;
6354
6355 /* Make the window's buffer temporarily current for
6356 overlays_at and compute_char_face. */
6357 obuf = current_buffer;
6358 current_buffer = XBUFFER (w->buffer);
6359 obegv = BEGV;
6360 ozv = ZV;
6361 BEGV = BEG;
6362 ZV = Z;
6363
6364 /* Is this char mouse-active or does it have help-echo? */
6365 XSETINT (position, pos);
6366
6367 /* Put all the overlays we want in a vector in overlay_vec.
6368 Store the length in len. If there are more than 10, make
6369 enough space for all, and try again. */
6370 len = 10;
6371 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6372 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6373 if (noverlays > len)
6374 {
6375 len = noverlays;
6376 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6377 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6378 }
6379
6380 noverlays = sort_overlays (overlay_vec, noverlays, w);
6381
6382 /* Check mouse-face highlighting. */
6383 if (! (EQ (window, dpyinfo->mouse_face_window)
6384 && vpos >= dpyinfo->mouse_face_beg_row
6385 && vpos <= dpyinfo->mouse_face_end_row
6386 && (vpos > dpyinfo->mouse_face_beg_row
6387 || hpos >= dpyinfo->mouse_face_beg_col)
6388 && (vpos < dpyinfo->mouse_face_end_row
6389 || hpos < dpyinfo->mouse_face_end_col
6390 || dpyinfo->mouse_face_past_end)))
6391 {
6392 /* Clear the display of the old active region, if any. */
6393 clear_mouse_face (dpyinfo);
6394
6395 /* Find the highest priority overlay that has a mouse-face prop. */
6396 overlay = Qnil;
6397 for (i = 0; i < noverlays; i++)
6398 {
6399 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6400 if (!NILP (mouse_face))
6401 {
6402 overlay = overlay_vec[i];
6403 break;
6404 }
6405 }
6406
6407 /* If no overlay applies, get a text property. */
6408 if (NILP (overlay))
6409 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6410
6411 /* Handle the overlay case. */
6412 if (! NILP (overlay))
6413 {
6414 /* Find the range of text around this char that
6415 should be active. */
6416 Lisp_Object before, after;
6417 int ignore;
6418
6419 before = Foverlay_start (overlay);
6420 after = Foverlay_end (overlay);
6421 /* Record this as the current active region. */
6422 fast_find_position (w, XFASTINT (before),
6423 &dpyinfo->mouse_face_beg_col,
6424 &dpyinfo->mouse_face_beg_row,
6425 &dpyinfo->mouse_face_beg_x,
6426 &dpyinfo->mouse_face_beg_y);
6427 dpyinfo->mouse_face_past_end
6428 = !fast_find_position (w, XFASTINT (after),
6429 &dpyinfo->mouse_face_end_col,
6430 &dpyinfo->mouse_face_end_row,
6431 &dpyinfo->mouse_face_end_x,
6432 &dpyinfo->mouse_face_end_y);
6433 dpyinfo->mouse_face_window = window;
6434 dpyinfo->mouse_face_face_id
6435 = face_at_buffer_position (w, pos, 0, 0,
6436 &ignore, pos + 1, 1);
6437
6438 /* Display it as active. */
6439 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6440 }
6441 /* Handle the text property case. */
6442 else if (! NILP (mouse_face))
6443 {
6444 /* Find the range of text around this char that
6445 should be active. */
6446 Lisp_Object before, after, beginning, end;
6447 int ignore;
6448
6449 beginning = Fmarker_position (w->start);
6450 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6451 - XFASTINT (w->window_end_pos)));
6452 before
6453 = Fprevious_single_property_change (make_number (pos + 1),
6454 Qmouse_face,
6455 w->buffer, beginning);
6456 after
6457 = Fnext_single_property_change (position, Qmouse_face,
6458 w->buffer, end);
6459 /* Record this as the current active region. */
6460 fast_find_position (w, XFASTINT (before),
6461 &dpyinfo->mouse_face_beg_col,
6462 &dpyinfo->mouse_face_beg_row,
6463 &dpyinfo->mouse_face_beg_x,
6464 &dpyinfo->mouse_face_beg_y);
6465 dpyinfo->mouse_face_past_end
6466 = !fast_find_position (w, XFASTINT (after),
6467 &dpyinfo->mouse_face_end_col,
6468 &dpyinfo->mouse_face_end_row,
6469 &dpyinfo->mouse_face_end_x,
6470 &dpyinfo->mouse_face_end_y);
6471 dpyinfo->mouse_face_window = window;
6472 dpyinfo->mouse_face_face_id
6473 = face_at_buffer_position (w, pos, 0, 0,
6474 &ignore, pos + 1, 1);
6475
6476 /* Display it as active. */
6477 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6478 }
6479 }
6480
6481 /* Look for a `help-echo' property. */
6482 {
6483 Lisp_Object help;
6484
6485 /* Check overlays first. */
6486 help = Qnil;
6487 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6488 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6489
6490 /* Try text properties. */
6491 if (!STRINGP (help)
6492 && ((STRINGP (glyph->object)
6493 && glyph->charpos >= 0
6494 && glyph->charpos < XSTRING (glyph->object)->size)
6495 || (BUFFERP (glyph->object)
6496 && glyph->charpos >= BEGV
6497 && glyph->charpos < ZV)))
6498 help = Fget_text_property (make_number (glyph->charpos),
6499 Qhelp_echo, glyph->object);
6500
6501 if (STRINGP (help))
6502 help_echo = help;
6503 }
6504
6505 BEGV = obegv;
6506 ZV = ozv;
6507 current_buffer = obuf;
6508 }
6509 }
6510}
6511
6512static void
6513redo_mouse_highlight ()
6514{
6515 if (!NILP (last_mouse_motion_frame)
6516 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6517 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6518 last_mouse_motion_event.x,
6519 last_mouse_motion_event.y);
6520}
6521
6522
6523\f
6524/***********************************************************************
9ea173e8 6525 Tool-bars
06a2c219
GM
6526 ***********************************************************************/
6527
9ea173e8
GM
6528static int x_tool_bar_item P_ ((struct frame *, int, int,
6529 struct glyph **, int *, int *, int *));
06a2c219 6530
9ea173e8 6531/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6532 or -1. */
6533
9ea173e8 6534static int last_tool_bar_item;
06a2c219
GM
6535
6536
9ea173e8
GM
6537/* Get information about the tool-bar item at position X/Y on frame F.
6538 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6539 the current matrix of the tool-bar window of F, or NULL if not
6540 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6541 item in F->current_tool_bar_items. Value is
06a2c219 6542
9ea173e8 6543 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6544 0 if X/Y is on the same item that was highlighted before.
6545 1 otherwise. */
6546
6547static int
9ea173e8 6548x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6549 struct frame *f;
6550 int x, y;
6551 struct glyph **glyph;
6552 int *hpos, *vpos, *prop_idx;
6553{
6554 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6555 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6556 int area;
6557
6558 /* Find the glyph under X/Y. */
6559 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6560 if (*glyph == NULL)
6561 return -1;
6562
9ea173e8
GM
6563 /* Get the start of this tool-bar item's properties in
6564 f->current_tool_bar_items. */
6565 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6566 return -1;
6567
6568 /* Is mouse on the highlighted item? */
9ea173e8 6569 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6570 && *vpos >= dpyinfo->mouse_face_beg_row
6571 && *vpos <= dpyinfo->mouse_face_end_row
6572 && (*vpos > dpyinfo->mouse_face_beg_row
6573 || *hpos >= dpyinfo->mouse_face_beg_col)
6574 && (*vpos < dpyinfo->mouse_face_end_row
6575 || *hpos < dpyinfo->mouse_face_end_col
6576 || dpyinfo->mouse_face_past_end))
6577 return 0;
6578
6579 return 1;
6580}
6581
6582
9ea173e8 6583/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6584 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6585 or ButtonRelase. */
6586
6587static void
9ea173e8 6588x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6589 struct frame *f;
6590 XButtonEvent *button_event;
6591{
6592 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6593 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6594 int hpos, vpos, prop_idx;
6595 struct glyph *glyph;
6596 Lisp_Object enabled_p;
6597 int x = button_event->x;
6598 int y = button_event->y;
6599
9ea173e8 6600 /* If not on the highlighted tool-bar item, return. */
06a2c219 6601 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6602 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6603 return;
6604
6605 /* If item is disabled, do nothing. */
9ea173e8
GM
6606 enabled_p = (XVECTOR (f->current_tool_bar_items)
6607 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6608 if (NILP (enabled_p))
6609 return;
6610
6611 if (button_event->type == ButtonPress)
6612 {
6613 /* Show item in pressed state. */
6614 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6615 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6616 last_tool_bar_item = prop_idx;
06a2c219
GM
6617 }
6618 else
6619 {
6620 Lisp_Object key, frame;
6621 struct input_event event;
6622
6623 /* Show item in released state. */
6624 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6625 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6626
9ea173e8
GM
6627 key = (XVECTOR (f->current_tool_bar_items)
6628 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6629
6630 XSETFRAME (frame, f);
9ea173e8
GM
6631 event.kind = TOOL_BAR_EVENT;
6632 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6633 kbd_buffer_store_event (&event);
6634
9ea173e8 6635 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6636 event.frame_or_window = Fcons (frame, key);
6637 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6638 button_event->state);
6639 kbd_buffer_store_event (&event);
9ea173e8 6640 last_tool_bar_item = -1;
06a2c219
GM
6641 }
6642}
6643
6644
9ea173e8
GM
6645/* Possibly highlight a tool-bar item on frame F when mouse moves to
6646 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6647 note_mouse_highlight. */
6648
6649static void
9ea173e8 6650note_tool_bar_highlight (f, x, y)
06a2c219
GM
6651 struct frame *f;
6652 int x, y;
6653{
9ea173e8 6654 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6655 struct window *w = XWINDOW (window);
6656 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6657 int hpos, vpos;
6658 struct glyph *glyph;
6659 struct glyph_row *row;
5c187dee 6660 int i;
06a2c219
GM
6661 Lisp_Object enabled_p;
6662 int prop_idx;
6663 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6664 int mouse_down_p, rc;
06a2c219
GM
6665
6666 /* Function note_mouse_highlight is called with negative x(y
6667 values when mouse moves outside of the frame. */
6668 if (x <= 0 || y <= 0)
6669 {
6670 clear_mouse_face (dpyinfo);
6671 return;
6672 }
6673
9ea173e8 6674 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6675 if (rc < 0)
6676 {
9ea173e8 6677 /* Not on tool-bar item. */
06a2c219
GM
6678 clear_mouse_face (dpyinfo);
6679 return;
6680 }
6681 else if (rc == 0)
9ea173e8 6682 /* On same tool-bar item as before. */
06a2c219 6683 goto set_help_echo;
b8009dd1 6684
06a2c219
GM
6685 clear_mouse_face (dpyinfo);
6686
9ea173e8 6687 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6688 mouse_down_p = (dpyinfo->grabbed
6689 && f == last_mouse_frame
6690 && FRAME_LIVE_P (f));
6691 if (mouse_down_p
9ea173e8 6692 && last_tool_bar_item != prop_idx)
06a2c219
GM
6693 return;
6694
6695 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6696 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6697
9ea173e8
GM
6698 /* If tool-bar item is not enabled, don't highlight it. */
6699 enabled_p = (XVECTOR (f->current_tool_bar_items)
6700 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6701 if (!NILP (enabled_p))
6702 {
6703 /* Compute the x-position of the glyph. In front and past the
6704 image is a space. We include this is the highlighted area. */
6705 row = MATRIX_ROW (w->current_matrix, vpos);
6706 for (i = x = 0; i < hpos; ++i)
6707 x += row->glyphs[TEXT_AREA][i].pixel_width;
6708
6709 /* Record this as the current active region. */
6710 dpyinfo->mouse_face_beg_col = hpos;
6711 dpyinfo->mouse_face_beg_row = vpos;
6712 dpyinfo->mouse_face_beg_x = x;
6713 dpyinfo->mouse_face_beg_y = row->y;
6714 dpyinfo->mouse_face_past_end = 0;
6715
6716 dpyinfo->mouse_face_end_col = hpos + 1;
6717 dpyinfo->mouse_face_end_row = vpos;
6718 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6719 dpyinfo->mouse_face_end_y = row->y;
6720 dpyinfo->mouse_face_window = window;
9ea173e8 6721 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6722
6723 /* Display it as active. */
6724 show_mouse_face (dpyinfo, draw);
6725 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6726 }
06a2c219
GM
6727
6728 set_help_echo:
6729
9ea173e8 6730 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6731 XTread_socket does the rest. */
9ea173e8
GM
6732 help_echo = (XVECTOR (f->current_tool_bar_items)
6733 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6734 if (!STRINGP (help_echo))
9ea173e8
GM
6735 help_echo = (XVECTOR (f->current_tool_bar_items)
6736 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6737}
4d73d038 6738
06a2c219
GM
6739
6740\f
6741/* Find the glyph matrix position of buffer position POS in window W.
6742 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6743 current glyphs must be up to date. If POS is above window start
6744 return (0, 0, 0, 0). If POS is after end of W, return end of
6745 last line in W. */
b8009dd1
RS
6746
6747static int
06a2c219
GM
6748fast_find_position (w, pos, hpos, vpos, x, y)
6749 struct window *w;
b8009dd1 6750 int pos;
06a2c219 6751 int *hpos, *vpos, *x, *y;
b8009dd1 6752{
b8009dd1 6753 int i;
bf1c0ba1 6754 int lastcol;
06a2c219
GM
6755 int maybe_next_line_p = 0;
6756 int line_start_position;
6757 int yb = window_text_bottom_y (w);
6758 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6759 struct glyph_row *best_row = row;
6760 int row_vpos = 0, best_row_vpos = 0;
6761 int current_x;
6762
6763 while (row->y < yb)
b8009dd1 6764 {
06a2c219
GM
6765 if (row->used[TEXT_AREA])
6766 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6767 else
6768 line_start_position = 0;
6769
6770 if (line_start_position > pos)
b8009dd1 6771 break;
77b68646
RS
6772 /* If the position sought is the end of the buffer,
6773 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6774 else if (line_start_position == pos
6775 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6776 {
06a2c219 6777 maybe_next_line_p = 1;
77b68646
RS
6778 break;
6779 }
06a2c219
GM
6780 else if (line_start_position > 0)
6781 {
6782 best_row = row;
6783 best_row_vpos = row_vpos;
6784 }
6785
6786 ++row;
6787 ++row_vpos;
b8009dd1 6788 }
06a2c219
GM
6789
6790 /* Find the right column within BEST_ROW. */
6791 lastcol = 0;
6792 current_x = best_row->x;
6793 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6794 {
06a2c219
GM
6795 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6796 int charpos;
6797
6798 charpos = glyph->charpos;
6799 if (charpos == pos)
bf1c0ba1 6800 {
06a2c219
GM
6801 *hpos = i;
6802 *vpos = best_row_vpos;
6803 *x = current_x;
6804 *y = best_row->y;
bf1c0ba1
RS
6805 return 1;
6806 }
06a2c219 6807 else if (charpos > pos)
4d73d038 6808 break;
06a2c219
GM
6809 else if (charpos > 0)
6810 lastcol = i;
6811
6812 current_x += glyph->pixel_width;
bf1c0ba1 6813 }
b8009dd1 6814
77b68646
RS
6815 /* If we're looking for the end of the buffer,
6816 and we didn't find it in the line we scanned,
6817 use the start of the following line. */
06a2c219 6818 if (maybe_next_line_p)
77b68646 6819 {
06a2c219
GM
6820 ++best_row;
6821 ++best_row_vpos;
6822 lastcol = 0;
6823 current_x = best_row->x;
77b68646
RS
6824 }
6825
06a2c219
GM
6826 *vpos = best_row_vpos;
6827 *hpos = lastcol + 1;
6828 *x = current_x;
6829 *y = best_row->y;
b8009dd1
RS
6830 return 0;
6831}
6832
06a2c219 6833
b8009dd1
RS
6834/* Display the active region described by mouse_face_*
6835 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6836
6837static void
06a2c219 6838show_mouse_face (dpyinfo, draw)
7a13e894 6839 struct x_display_info *dpyinfo;
06a2c219 6840 enum draw_glyphs_face draw;
b8009dd1 6841{
7a13e894 6842 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6843 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6844 int i;
06a2c219
GM
6845 int cursor_off_p = 0;
6846 struct cursor_pos saved_cursor;
6847
6848 saved_cursor = output_cursor;
6849
6850 /* If window is in the process of being destroyed, don't bother
6851 to do anything. */
6852 if (w->current_matrix == NULL)
6853 goto set_x_cursor;
6854
6855 /* Recognize when we are called to operate on rows that don't exist
6856 anymore. This can happen when a window is split. */
6857 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6858 goto set_x_cursor;
6859
6860 set_output_cursor (&w->phys_cursor);
6861
6862 /* Note that mouse_face_beg_row etc. are window relative. */
6863 for (i = dpyinfo->mouse_face_beg_row;
6864 i <= dpyinfo->mouse_face_end_row;
6865 i++)
6866 {
6867 int start_hpos, end_hpos, start_x;
6868 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6869
6870 /* Don't do anything if row doesn't have valid contents. */
6871 if (!row->enabled_p)
6872 continue;
6873
6874 /* For all but the first row, the highlight starts at column 0. */
6875 if (i == dpyinfo->mouse_face_beg_row)
6876 {
6877 start_hpos = dpyinfo->mouse_face_beg_col;
6878 start_x = dpyinfo->mouse_face_beg_x;
6879 }
6880 else
6881 {
6882 start_hpos = 0;
6883 start_x = 0;
6884 }
6885
6886 if (i == dpyinfo->mouse_face_end_row)
6887 end_hpos = dpyinfo->mouse_face_end_col;
6888 else
6889 end_hpos = row->used[TEXT_AREA];
6890
6891 /* If the cursor's in the text we are about to rewrite, turn the
6892 cursor off. */
6893 if (!w->pseudo_window_p
6894 && i == output_cursor.vpos
6895 && output_cursor.hpos >= start_hpos - 1
6896 && output_cursor.hpos <= end_hpos)
514e4681 6897 {
06a2c219
GM
6898 x_update_window_cursor (w, 0);
6899 cursor_off_p = 1;
514e4681 6900 }
b8009dd1 6901
06a2c219 6902 if (end_hpos > start_hpos)
54a91a0f 6903 x_draw_glyphs (w, start_x, row, TEXT_AREA,
66ac4b0e 6904 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6905 }
6906
514e4681 6907 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6908 if (cursor_off_p)
6909 x_display_cursor (w, 1,
6910 output_cursor.hpos, output_cursor.vpos,
6911 output_cursor.x, output_cursor.y);
2729a2b5 6912
06a2c219 6913 output_cursor = saved_cursor;
fb3b7de5 6914
06a2c219
GM
6915 set_x_cursor:
6916
6917 /* Change the mouse cursor. */
6918 if (draw == DRAW_NORMAL_TEXT)
6919 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6920 f->output_data.x->text_cursor);
6921 else if (draw == DRAW_MOUSE_FACE)
334208b7 6922 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6923 f->output_data.x->cross_cursor);
27ead1d5 6924 else
334208b7 6925 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6926 f->output_data.x->nontext_cursor);
b8009dd1
RS
6927}
6928
6929/* Clear out the mouse-highlighted active region.
06a2c219 6930 Redraw it un-highlighted first. */
b8009dd1 6931
06a2c219 6932void
7a13e894
RS
6933clear_mouse_face (dpyinfo)
6934 struct x_display_info *dpyinfo;
b8009dd1 6935{
06a2c219
GM
6936 if (tip_frame)
6937 return;
6938
7a13e894 6939 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6940 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6941
7a13e894
RS
6942 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6943 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6944 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6945}
e687d06e
RS
6946
6947/* Just discard the mouse face information for frame F, if any.
6948 This is used when the size of F is changed. */
6949
dfcf069d 6950void
e687d06e
RS
6951cancel_mouse_face (f)
6952 FRAME_PTR f;
6953{
6954 Lisp_Object window;
6955 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6956
6957 window = dpyinfo->mouse_face_window;
6958 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6959 {
6960 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6961 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6962 dpyinfo->mouse_face_window = Qnil;
6963 }
6964}
b8009dd1 6965\f
ab648270
JB
6966static struct scroll_bar *x_window_to_scroll_bar ();
6967static void x_scroll_bar_report_motion ();
12ba150f 6968
90e65f07 6969/* Return the current position of the mouse.
2d7fc7e8 6970 *fp should be a frame which indicates which display to ask about.
90e65f07 6971
2d7fc7e8 6972 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6973 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6974 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6975 position on the scroll bar.
12ba150f 6976
2d7fc7e8 6977 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
6978 mouse is on, *bar_window to nil, and *x and *y to the character cell
6979 the mouse is over.
6980
06a2c219 6981 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
6982 was at this position.
6983
a135645a
RS
6984 Don't store anything if we don't have a valid set of values to report.
6985
90e65f07 6986 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 6987 movement. */
90e65f07
JB
6988
6989static void
1cf412ec 6990XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 6991 FRAME_PTR *fp;
1cf412ec 6992 int insist;
12ba150f 6993 Lisp_Object *bar_window;
ab648270 6994 enum scroll_bar_part *part;
90e65f07 6995 Lisp_Object *x, *y;
e5d77022 6996 unsigned long *time;
90e65f07 6997{
a135645a
RS
6998 FRAME_PTR f1;
6999
90e65f07
JB
7000 BLOCK_INPUT;
7001
8bcee03e 7002 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7003 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7004 else
7005 {
12ba150f
JB
7006 Window root;
7007 int root_x, root_y;
90e65f07 7008
12ba150f
JB
7009 Window dummy_window;
7010 int dummy;
7011
39d8bb4d
KH
7012 Lisp_Object frame, tail;
7013
7014 /* Clear the mouse-moved flag for every frame on this display. */
7015 FOR_EACH_FRAME (tail, frame)
7016 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7017 XFRAME (frame)->mouse_moved = 0;
7018
ab648270 7019 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7020
7021 /* Figure out which root window we're on. */
334208b7
RS
7022 XQueryPointer (FRAME_X_DISPLAY (*fp),
7023 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7024
7025 /* The root window which contains the pointer. */
7026 &root,
7027
7028 /* Trash which we can't trust if the pointer is on
7029 a different screen. */
7030 &dummy_window,
7031
7032 /* The position on that root window. */
58769bee 7033 &root_x, &root_y,
12ba150f
JB
7034
7035 /* More trash we can't trust. */
7036 &dummy, &dummy,
7037
7038 /* Modifier keys and pointer buttons, about which
7039 we don't care. */
7040 (unsigned int *) &dummy);
7041
7042 /* Now we have a position on the root; find the innermost window
7043 containing the pointer. */
7044 {
7045 Window win, child;
7046 int win_x, win_y;
06a2c219 7047 int parent_x = 0, parent_y = 0;
e99db5a1 7048 int count;
12ba150f
JB
7049
7050 win = root;
69388238 7051
2d7fc7e8
RS
7052 /* XTranslateCoordinates can get errors if the window
7053 structure is changing at the same time this function
7054 is running. So at least we must not crash from them. */
7055
e99db5a1 7056 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7057
334208b7 7058 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7059 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7060 {
69388238
RS
7061 /* If mouse was grabbed on a frame, give coords for that frame
7062 even if the mouse is now outside it. */
334208b7 7063 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7064
12ba150f 7065 /* From-window, to-window. */
69388238 7066 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7067
7068 /* From-position, to-position. */
7069 root_x, root_y, &win_x, &win_y,
7070
7071 /* Child of win. */
7072 &child);
69388238
RS
7073 f1 = last_mouse_frame;
7074 }
7075 else
7076 {
7077 while (1)
7078 {
334208b7 7079 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7080
69388238
RS
7081 /* From-window, to-window. */
7082 root, win,
12ba150f 7083
69388238
RS
7084 /* From-position, to-position. */
7085 root_x, root_y, &win_x, &win_y,
7086
7087 /* Child of win. */
7088 &child);
7089
9af3143a 7090 if (child == None || child == win)
69388238
RS
7091 break;
7092
7093 win = child;
7094 parent_x = win_x;
7095 parent_y = win_y;
7096 }
12ba150f 7097
69388238
RS
7098 /* Now we know that:
7099 win is the innermost window containing the pointer
7100 (XTC says it has no child containing the pointer),
7101 win_x and win_y are the pointer's position in it
7102 (XTC did this the last time through), and
7103 parent_x and parent_y are the pointer's position in win's parent.
7104 (They are what win_x and win_y were when win was child.
7105 If win is the root window, it has no parent, and
7106 parent_{x,y} are invalid, but that's okay, because we'll
7107 never use them in that case.) */
7108
7109 /* Is win one of our frames? */
19126e11 7110 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7111 }
58769bee 7112
2d7fc7e8
RS
7113 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7114 f1 = 0;
7115
e99db5a1 7116 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7117
ab648270 7118 /* If not, is it one of our scroll bars? */
a135645a 7119 if (! f1)
12ba150f 7120 {
ab648270 7121 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7122
7123 if (bar)
7124 {
a135645a 7125 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7126 win_x = parent_x;
7127 win_y = parent_y;
7128 }
7129 }
90e65f07 7130
8bcee03e 7131 if (f1 == 0 && insist > 0)
b86bd3dd 7132 f1 = SELECTED_FRAME ();
1cf412ec 7133
a135645a 7134 if (f1)
12ba150f 7135 {
06a2c219
GM
7136 /* Ok, we found a frame. Store all the values.
7137 last_mouse_glyph is a rectangle used to reduce the
7138 generation of mouse events. To not miss any motion
7139 events, we must divide the frame into rectangles of the
7140 size of the smallest character that could be displayed
7141 on it, i.e. into the same rectangles that matrices on
7142 the frame are divided into. */
7143
7144#if OLD_REDISPLAY_CODE
2b5c9e71 7145 int ignore1, ignore2;
2b5c9e71 7146 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7147 &last_mouse_glyph,
1cf412ec
RS
7148 FRAME_X_DISPLAY_INFO (f1)->grabbed
7149 || insist);
06a2c219
GM
7150#else
7151 {
7152 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7153 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7154 int x = win_x;
7155 int y = win_y;
7156
7157 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7158 round down even for negative values. */
7159 if (x < 0)
7160 x -= width - 1;
7161 if (y < 0)
7162 y -= height - 1;
7163
7164 last_mouse_glyph.width = width;
7165 last_mouse_glyph.height = height;
7166 last_mouse_glyph.x = (x + width - 1) / width * width;
7167 last_mouse_glyph.y = (y + height - 1) / height * height;
7168 }
7169#endif
12ba150f
JB
7170
7171 *bar_window = Qnil;
7172 *part = 0;
334208b7 7173 *fp = f1;
e0c1aef2
KH
7174 XSETINT (*x, win_x);
7175 XSETINT (*y, win_y);
12ba150f
JB
7176 *time = last_mouse_movement_time;
7177 }
7178 }
7179 }
90e65f07
JB
7180
7181 UNBLOCK_INPUT;
7182}
f451eb13 7183
06a2c219 7184
06a2c219 7185#ifdef USE_X_TOOLKIT
bffcfca9
GM
7186
7187/* Atimer callback function for TIMER. Called every 0.1s to process
7188 Xt timeouts, if needed. We must avoid calling XtAppPending as
7189 much as possible because that function does an implicit XFlush
7190 that slows us down. */
7191
7192static void
7193x_process_timeouts (timer)
7194 struct atimer *timer;
7195{
7196 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7197 {
7198 BLOCK_INPUT;
7199 while (XtAppPending (Xt_app_con) & XtIMTimer)
7200 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7201 UNBLOCK_INPUT;
7202 }
06a2c219
GM
7203}
7204
bffcfca9 7205#endif /* USE_X_TOOLKIT */
06a2c219
GM
7206
7207\f
7208/* Scroll bar support. */
7209
7210/* Given an X window ID, find the struct scroll_bar which manages it.
7211 This can be called in GC, so we have to make sure to strip off mark
7212 bits. */
bffcfca9 7213
06a2c219
GM
7214static struct scroll_bar *
7215x_window_to_scroll_bar (window_id)
7216 Window window_id;
7217{
7218 Lisp_Object tail;
7219
7220 for (tail = Vframe_list;
7221 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7222 tail = XCDR (tail))
06a2c219
GM
7223 {
7224 Lisp_Object frame, bar, condemned;
7225
8e713be6 7226 frame = XCAR (tail);
06a2c219
GM
7227 /* All elements of Vframe_list should be frames. */
7228 if (! GC_FRAMEP (frame))
7229 abort ();
7230
7231 /* Scan this frame's scroll bar list for a scroll bar with the
7232 right window ID. */
7233 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7234 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7235 /* This trick allows us to search both the ordinary and
7236 condemned scroll bar lists with one loop. */
7237 ! GC_NILP (bar) || (bar = condemned,
7238 condemned = Qnil,
7239 ! GC_NILP (bar));
7240 bar = XSCROLL_BAR (bar)->next)
7241 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7242 return XSCROLL_BAR (bar);
7243 }
7244
7245 return 0;
7246}
7247
7248
7249\f
7250/************************************************************************
7251 Toolkit scroll bars
7252 ************************************************************************/
7253
7254#if USE_TOOLKIT_SCROLL_BARS
7255
7256static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7257static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7258static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7259 struct scroll_bar *));
7260static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7261 int, int, int));
7262
7263
7264/* Id of action hook installed for scroll bars. */
7265
7266static XtActionHookId action_hook_id;
7267
7268/* Lisp window being scrolled. Set when starting to interact with
7269 a toolkit scroll bar, reset to nil when ending the interaction. */
7270
7271static Lisp_Object window_being_scrolled;
7272
7273/* Last scroll bar part sent in xm_scroll_callback. */
7274
7275static int last_scroll_bar_part;
7276
ec18280f
SM
7277/* Whether this is an Xaw with arrow-scrollbars. This should imply
7278 that movements of 1/20 of the screen size are mapped to up/down. */
7279
7280static Boolean xaw3d_arrow_scroll;
7281
7282/* Whether the drag scrolling maintains the mouse at the top of the
7283 thumb. If not, resizing the thumb needs to be done more carefully
7284 to avoid jerkyness. */
7285
7286static Boolean xaw3d_pick_top;
7287
06a2c219
GM
7288
7289/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7290 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7291 the user ends an interaction with the scroll bar, and generates
7292 a `end-scroll' scroll_bar_click' event if so. */
7293
7294static void
7295xt_action_hook (widget, client_data, action_name, event, params,
7296 num_params)
7297 Widget widget;
7298 XtPointer client_data;
7299 String action_name;
7300 XEvent *event;
7301 String *params;
7302 Cardinal *num_params;
7303{
7304 int scroll_bar_p;
7305 char *end_action;
7306
7307#ifdef USE_MOTIF
7308 scroll_bar_p = XmIsScrollBar (widget);
7309 end_action = "Release";
ec18280f 7310#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7311 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7312 end_action = "EndScroll";
ec18280f 7313#endif /* USE_MOTIF */
06a2c219 7314
06a2c219
GM
7315 if (scroll_bar_p
7316 && strcmp (action_name, end_action) == 0
7317 && WINDOWP (window_being_scrolled))
7318 {
7319 struct window *w;
7320
7321 x_send_scroll_bar_event (window_being_scrolled,
7322 scroll_bar_end_scroll, 0, 0);
7323 w = XWINDOW (window_being_scrolled);
7324 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7325 window_being_scrolled = Qnil;
7326 last_scroll_bar_part = -1;
bffcfca9
GM
7327
7328 /* Xt timeouts no longer needed. */
7329 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7330 }
7331}
7332
7333
7334/* Send a client message with message type Xatom_Scrollbar for a
7335 scroll action to the frame of WINDOW. PART is a value identifying
7336 the part of the scroll bar that was clicked on. PORTION is the
7337 amount to scroll of a whole of WHOLE. */
7338
7339static void
7340x_send_scroll_bar_event (window, part, portion, whole)
7341 Lisp_Object window;
7342 int part, portion, whole;
7343{
7344 XEvent event;
7345 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7346 struct frame *f = XFRAME (XWINDOW (window)->frame);
7347
7348 /* Construct a ClientMessage event to send to the frame. */
7349 ev->type = ClientMessage;
7350 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7351 ev->display = FRAME_X_DISPLAY (f);
7352 ev->window = FRAME_X_WINDOW (f);
7353 ev->format = 32;
7354 ev->data.l[0] = (long) window;
7355 ev->data.l[1] = (long) part;
7356 ev->data.l[2] = (long) 0;
7357 ev->data.l[3] = (long) portion;
7358 ev->data.l[4] = (long) whole;
7359
bffcfca9
GM
7360 /* Make Xt timeouts work while the scroll bar is active. */
7361 toolkit_scroll_bar_interaction = 1;
7362
06a2c219
GM
7363 /* Setting the event mask to zero means that the message will
7364 be sent to the client that created the window, and if that
7365 window no longer exists, no event will be sent. */
7366 BLOCK_INPUT;
7367 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7368 UNBLOCK_INPUT;
7369}
7370
7371
7372/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7373 in *IEVENT. */
7374
7375static void
7376x_scroll_bar_to_input_event (event, ievent)
7377 XEvent *event;
7378 struct input_event *ievent;
7379{
7380 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7381 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7382 struct frame *f = XFRAME (XWINDOW (window)->frame);
7383
7384 ievent->kind = scroll_bar_click;
7385 ievent->frame_or_window = window;
7386 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7387 ievent->part = ev->data.l[1];
7388 ievent->code = ev->data.l[2];
7389 ievent->x = make_number ((int) ev->data.l[3]);
7390 ievent->y = make_number ((int) ev->data.l[4]);
7391 ievent->modifiers = 0;
7392}
7393
7394
7395#ifdef USE_MOTIF
7396
7397/* Minimum and maximum values used for Motif scroll bars. */
7398
7399#define XM_SB_MIN 1
7400#define XM_SB_MAX 10000000
7401#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7402
7403
7404/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7405 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7406 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7407
7408static void
7409xm_scroll_callback (widget, client_data, call_data)
7410 Widget widget;
7411 XtPointer client_data, call_data;
7412{
7413 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7414 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7415 double percent;
7416 int part = -1, whole = 0, portion = 0;
7417
7418 switch (cs->reason)
7419 {
7420 case XmCR_DECREMENT:
7421 bar->dragging = Qnil;
7422 part = scroll_bar_up_arrow;
7423 break;
7424
7425 case XmCR_INCREMENT:
7426 bar->dragging = Qnil;
7427 part = scroll_bar_down_arrow;
7428 break;
7429
7430 case XmCR_PAGE_DECREMENT:
7431 bar->dragging = Qnil;
7432 part = scroll_bar_above_handle;
7433 break;
7434
7435 case XmCR_PAGE_INCREMENT:
7436 bar->dragging = Qnil;
7437 part = scroll_bar_below_handle;
7438 break;
7439
7440 case XmCR_TO_TOP:
7441 bar->dragging = Qnil;
7442 part = scroll_bar_to_top;
7443 break;
7444
7445 case XmCR_TO_BOTTOM:
7446 bar->dragging = Qnil;
7447 part = scroll_bar_to_bottom;
7448 break;
7449
7450 case XmCR_DRAG:
7451 {
7452 int slider_size;
7453 int dragging_down_p = (INTEGERP (bar->dragging)
7454 && XINT (bar->dragging) <= cs->value);
7455
7456 /* Get the slider size. */
7457 BLOCK_INPUT;
7458 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7459 UNBLOCK_INPUT;
7460
7461 /* At the max position of the scroll bar, do a line-wise
7462 movement. Without doing anything, the LessTif scroll bar
7463 calls us with the same cs->value again and again. If we
7464 want to make sure that we can reach the end of the buffer,
7465 we have to do something.
7466
7467 Implementation note: setting bar->dragging always to
7468 cs->value gives a smoother movement at the max position.
7469 Setting it to nil when doing line-wise movement gives
7470 a better slider behavior. */
7471
7472 if (cs->value + slider_size == XM_SB_MAX
7473 || (dragging_down_p
7474 && last_scroll_bar_part == scroll_bar_down_arrow))
7475 {
7476 part = scroll_bar_down_arrow;
7477 bar->dragging = Qnil;
7478 }
7479 else
7480 {
7481 whole = XM_SB_RANGE;
7482 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7483 part = scroll_bar_handle;
7484 bar->dragging = make_number (cs->value);
7485 }
7486 }
7487 break;
7488
7489 case XmCR_VALUE_CHANGED:
7490 break;
7491 };
7492
7493 if (part >= 0)
7494 {
7495 window_being_scrolled = bar->window;
7496 last_scroll_bar_part = part;
7497 x_send_scroll_bar_event (bar->window, part, portion, whole);
7498 }
7499}
7500
7501
ec18280f 7502#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7503
7504
ec18280f 7505/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7506 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7507 scroll bar struct. CALL_DATA is a pointer to a float saying where
7508 the thumb is. */
7509
7510static void
ec18280f 7511xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7512 Widget widget;
7513 XtPointer client_data, call_data;
7514{
7515 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7516 float top = *(float *) call_data;
7517 float shown;
ec18280f
SM
7518 int whole, portion, height;
7519 int part;
06a2c219
GM
7520
7521 /* Get the size of the thumb, a value between 0 and 1. */
7522 BLOCK_INPUT;
ec18280f 7523 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7524 UNBLOCK_INPUT;
7525
7526 whole = 10000000;
7527 portion = shown < 1 ? top * whole : 0;
06a2c219 7528
ec18280f
SM
7529 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7530 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7531 the bottom, so we force the scrolling whenever we see that we're
7532 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7533 we try to ensure that we always stay two pixels away from the
7534 bottom). */
06a2c219
GM
7535 part = scroll_bar_down_arrow;
7536 else
7537 part = scroll_bar_handle;
7538
7539 window_being_scrolled = bar->window;
7540 bar->dragging = make_number (portion);
7541 last_scroll_bar_part = part;
7542 x_send_scroll_bar_event (bar->window, part, portion, whole);
7543}
7544
7545
ec18280f
SM
7546/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7547 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7548 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7549 the scroll bar. CALL_DATA is an integer specifying the action that
7550 has taken place. It's magnitude is in the range 0..height of the
7551 scroll bar. Negative values mean scroll towards buffer start.
7552 Values < height of scroll bar mean line-wise movement. */
7553
7554static void
ec18280f 7555xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7556 Widget widget;
7557 XtPointer client_data, call_data;
7558{
7559 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7560 int position = (int) call_data;
7561 Dimension height;
7562 int part;
7563
7564 /* Get the height of the scroll bar. */
7565 BLOCK_INPUT;
7566 XtVaGetValues (widget, XtNheight, &height, NULL);
7567 UNBLOCK_INPUT;
7568
ec18280f
SM
7569 if (abs (position) >= height)
7570 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7571
7572 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7573 it maps line-movement to call_data = max(5, height/20). */
7574 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7575 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7576 else
ec18280f 7577 part = scroll_bar_move_ratio;
06a2c219
GM
7578
7579 window_being_scrolled = bar->window;
7580 bar->dragging = Qnil;
7581 last_scroll_bar_part = part;
ec18280f 7582 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7583}
7584
7585
7586#endif /* not USE_MOTIF */
7587
7588
7589/* Create the widget for scroll bar BAR on frame F. Record the widget
7590 and X window of the scroll bar in BAR. */
7591
7592static void
7593x_create_toolkit_scroll_bar (f, bar)
7594 struct frame *f;
7595 struct scroll_bar *bar;
7596{
7597 Window xwindow;
7598 Widget widget;
7599 Arg av[20];
7600 int ac = 0;
7601 char *scroll_bar_name = "verticalScrollBar";
7602 unsigned long pixel;
7603
7604 BLOCK_INPUT;
7605
7606#ifdef USE_MOTIF
7607 /* LessTif 0.85, problems:
7608
7609 1. When the mouse if over the scroll bar, the scroll bar will
7610 get keyboard events. I didn't find a way to turn this off.
7611
7612 2. Do we have to explicitly set the cursor to get an arrow
7613 cursor (see below)? */
7614
7615 /* Set resources. Create the widget. */
7616 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7617 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7618 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7619 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7620 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7621 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7622 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7623
7624 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7625 if (pixel != -1)
7626 {
7627 XtSetArg (av[ac], XmNforeground, pixel);
7628 ++ac;
7629 }
7630
7631 pixel = f->output_data.x->scroll_bar_background_pixel;
7632 if (pixel != -1)
7633 {
7634 XtSetArg (av[ac], XmNbackground, pixel);
7635 ++ac;
7636 }
7637
7638 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7639 scroll_bar_name, av, ac);
7640
7641 /* Add one callback for everything that can happen. */
7642 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7643 (XtPointer) bar);
7644 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7645 (XtPointer) bar);
7646 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7647 (XtPointer) bar);
7648 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7649 (XtPointer) bar);
7650 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7651 (XtPointer) bar);
7652 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7653 (XtPointer) bar);
7654 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7655 (XtPointer) bar);
7656
7657 /* Realize the widget. Only after that is the X window created. */
7658 XtRealizeWidget (widget);
7659
7660 /* Set the cursor to an arrow. I didn't find a resource to do that.
7661 And I'm wondering why it hasn't an arrow cursor by default. */
7662 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7663 f->output_data.x->nontext_cursor);
7664
ec18280f 7665#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7666
7667 /* Set resources. Create the widget. The background of the
7668 Xaw3d scroll bar widget is a little bit light for my taste.
7669 We don't alter it here to let users change it according
7670 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7671 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7672 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7673 /* For smoother scrolling with Xaw3d -sm */
7674 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7675 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7676
7677 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7678 if (pixel != -1)
7679 {
7680 XtSetArg (av[ac], XtNforeground, pixel);
7681 ++ac;
7682 }
7683
7684 pixel = f->output_data.x->scroll_bar_background_pixel;
7685 if (pixel != -1)
7686 {
7687 XtSetArg (av[ac], XtNbackground, pixel);
7688 ++ac;
7689 }
7690
7691 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7692 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7693
7694 {
7695 char *initial = "";
7696 char *val = initial;
7697 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7698 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7699 if (val == initial)
7700 { /* ARROW_SCROLL */
7701 xaw3d_arrow_scroll = True;
7702 /* Isn't that just a personal preference ? -sm */
7703 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7704 }
7705 }
06a2c219
GM
7706
7707 /* Define callbacks. */
ec18280f
SM
7708 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7709 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7710 (XtPointer) bar);
7711
7712 /* Realize the widget. Only after that is the X window created. */
7713 XtRealizeWidget (widget);
7714
ec18280f 7715#endif /* !USE_MOTIF */
06a2c219
GM
7716
7717 /* Install an action hook that let's us detect when the user
7718 finishes interacting with a scroll bar. */
7719 if (action_hook_id == 0)
7720 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7721
7722 /* Remember X window and widget in the scroll bar vector. */
7723 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7724 xwindow = XtWindow (widget);
7725 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7726
7727 UNBLOCK_INPUT;
7728}
7729
7730
7731/* Set the thumb size and position of scroll bar BAR. We are currently
7732 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7733
7734static void
7735x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7736 struct scroll_bar *bar;
7737 int portion, position, whole;
f451eb13 7738{
06a2c219 7739 float top, shown;
06a2c219 7740 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7741
06a2c219
GM
7742 if (whole == 0)
7743 top = 0, shown = 1;
7744 else
f451eb13 7745 {
06a2c219
GM
7746 top = (float) position / whole;
7747 shown = (float) portion / whole;
7748 }
f451eb13 7749
06a2c219 7750 BLOCK_INPUT;
f451eb13 7751
06a2c219
GM
7752#ifdef USE_MOTIF
7753 {
7754 int size, value;
7755 Boolean arrow1_selected, arrow2_selected;
7756 unsigned char flags;
7757 XmScrollBarWidget sb;
7758
ec18280f 7759 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7760 is the scroll bar's maximum and MIN is the scroll bar's minimum
7761 value. */
7762 size = shown * XM_SB_RANGE;
7763 size = min (size, XM_SB_RANGE);
7764 size = max (size, 1);
7765
7766 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7767 value = top * XM_SB_RANGE;
7768 value = min (value, XM_SB_MAX - size);
7769 value = max (value, XM_SB_MIN);
7770
7771 /* LessTif: Calling XmScrollBarSetValues after an increment or
7772 decrement turns off auto-repeat LessTif-internally. This can
7773 be seen in ScrollBar.c which resets Arrow1Selected and
7774 Arrow2Selected. It also sets internal flags so that LessTif
7775 believes the mouse is in the slider. We either have to change
7776 our code, or work around that by accessing private data. */
7777
7778 sb = (XmScrollBarWidget) widget;
7779 arrow1_selected = sb->scrollBar.arrow1_selected;
7780 arrow2_selected = sb->scrollBar.arrow2_selected;
7781 flags = sb->scrollBar.flags;
7782
7783 if (NILP (bar->dragging))
7784 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7785 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7786 /* This has the negative side effect that the slider value is
ec18280f 7787 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7788 page-wise movement. */
7789 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7790 else
7791 {
7792 /* If currently dragging, only update the slider size.
7793 This reduces flicker effects. */
7794 int old_value, old_size, increment, page_increment;
7795
7796 XmScrollBarGetValues (widget, &old_value, &old_size,
7797 &increment, &page_increment);
7798 XmScrollBarSetValues (widget, old_value,
7799 min (size, XM_SB_RANGE - old_value),
7800 0, 0, False);
7801 }
7802
7803 sb->scrollBar.arrow1_selected = arrow1_selected;
7804 sb->scrollBar.arrow2_selected = arrow2_selected;
7805 sb->scrollBar.flags = flags;
7806 }
ec18280f 7807#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7808 {
ec18280f
SM
7809 float old_top, old_shown;
7810 Dimension height;
7811 XtVaGetValues (widget,
7812 XtNtopOfThumb, &old_top,
7813 XtNshown, &old_shown,
7814 XtNheight, &height,
7815 NULL);
7816
7817 /* Massage the top+shown values. */
7818 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7819 top = max (0, min (1, top));
7820 else
7821 top = old_top;
7822 /* Keep two pixels available for moving the thumb down. */
7823 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7824
7825 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7826 check that your system's configuration file contains a define
7827 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7828 if (top != old_top || shown != old_shown)
eb393530 7829 {
ec18280f 7830 if (NILP (bar->dragging))
eb393530 7831 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7832 else
7833 {
ec18280f
SM
7834#ifdef HAVE_XAW3D
7835 ScrollbarWidget sb = (ScrollbarWidget) widget;
7836 int scroll_mode;
7837
7838 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7839 if (xaw3d_arrow_scroll)
7840 {
7841 /* Xaw3d stupidly ignores resize requests while dragging
7842 so we have to make it believe it's not in dragging mode. */
7843 scroll_mode = sb->scrollbar.scroll_mode;
7844 if (scroll_mode == 2)
7845 sb->scrollbar.scroll_mode = 0;
7846 }
7847#endif
7848 /* Try to make the scrolling a tad smoother. */
7849 if (!xaw3d_pick_top)
7850 shown = min (shown, old_shown);
7851
7852 XawScrollbarSetThumb (widget, top, shown);
7853
7854#ifdef HAVE_XAW3D
7855 if (xaw3d_arrow_scroll && scroll_mode == 2)
7856 sb->scrollbar.scroll_mode = scroll_mode;
7857#endif
06a2c219 7858 }
06a2c219
GM
7859 }
7860 }
ec18280f 7861#endif /* !USE_MOTIF */
06a2c219
GM
7862
7863 UNBLOCK_INPUT;
f451eb13
JB
7864}
7865
06a2c219
GM
7866#endif /* USE_TOOLKIT_SCROLL_BARS */
7867
7868
7869\f
7870/************************************************************************
7871 Scroll bars, general
7872 ************************************************************************/
7873
7874/* Create a scroll bar and return the scroll bar vector for it. W is
7875 the Emacs window on which to create the scroll bar. TOP, LEFT,
7876 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7877 scroll bar. */
7878
ab648270 7879static struct scroll_bar *
06a2c219
GM
7880x_scroll_bar_create (w, top, left, width, height)
7881 struct window *w;
f451eb13
JB
7882 int top, left, width, height;
7883{
06a2c219 7884 struct frame *f = XFRAME (w->frame);
334208b7
RS
7885 struct scroll_bar *bar
7886 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7887
7888 BLOCK_INPUT;
7889
06a2c219
GM
7890#if USE_TOOLKIT_SCROLL_BARS
7891 x_create_toolkit_scroll_bar (f, bar);
7892#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7893 {
7894 XSetWindowAttributes a;
7895 unsigned long mask;
5c187dee 7896 Window window;
06a2c219
GM
7897
7898 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7899 if (a.background_pixel == -1)
7900 a.background_pixel = f->output_data.x->background_pixel;
7901
12ba150f 7902 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7903 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7904 | ExposureMask);
7a13e894 7905 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7906
dbc4e1c1 7907 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7908
06a2c219
GM
7909 /* Clear the area of W that will serve as a scroll bar. This is
7910 for the case that a window has been split horizontally. In
7911 this case, no clear_frame is generated to reduce flickering. */
7912 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7913 left, top, width,
7914 window_box_height (w), False);
7915
7916 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7917 /* Position and size of scroll bar. */
7918 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7919 top,
7920 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7921 height,
7922 /* Border width, depth, class, and visual. */
7923 0,
7924 CopyFromParent,
7925 CopyFromParent,
7926 CopyFromParent,
7927 /* Attributes. */
7928 mask, &a);
7929 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7930 }
06a2c219 7931#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7932
06a2c219 7933 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7934 XSETINT (bar->top, top);
7935 XSETINT (bar->left, left);
7936 XSETINT (bar->width, width);
7937 XSETINT (bar->height, height);
7938 XSETINT (bar->start, 0);
7939 XSETINT (bar->end, 0);
12ba150f 7940 bar->dragging = Qnil;
f451eb13
JB
7941
7942 /* Add bar to its frame's list of scroll bars. */
334208b7 7943 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7944 bar->prev = Qnil;
334208b7 7945 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7946 if (!NILP (bar->next))
e0c1aef2 7947 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7948
06a2c219
GM
7949 /* Map the window/widget. */
7950#if USE_TOOLKIT_SCROLL_BARS
7951 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7952 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7953 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7954 top,
7955 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7956 height, 0);
7957#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7958 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7959#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7960
7961 UNBLOCK_INPUT;
12ba150f 7962 return bar;
f451eb13
JB
7963}
7964
06a2c219 7965
12ba150f 7966/* Draw BAR's handle in the proper position.
06a2c219 7967
12ba150f
JB
7968 If the handle is already drawn from START to END, don't bother
7969 redrawing it, unless REBUILD is non-zero; in that case, always
7970 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7971 events.)
12ba150f
JB
7972
7973 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7974 fit inside its rectangle, but if the user is dragging the scroll
7975 bar handle, we want to let them drag it down all the way, so that
7976 the bar's top is as far down as it goes; otherwise, there's no way
7977 to move to the very end of the buffer. */
7978
5c187dee
GM
7979#ifndef USE_TOOLKIT_SCROLL_BARS
7980
f451eb13 7981static void
ab648270
JB
7982x_scroll_bar_set_handle (bar, start, end, rebuild)
7983 struct scroll_bar *bar;
f451eb13 7984 int start, end;
12ba150f 7985 int rebuild;
f451eb13 7986{
12ba150f 7987 int dragging = ! NILP (bar->dragging);
ab648270 7988 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7989 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7990 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7991
7992 /* If the display is already accurate, do nothing. */
7993 if (! rebuild
7994 && start == XINT (bar->start)
7995 && end == XINT (bar->end))
7996 return;
7997
f451eb13
JB
7998 BLOCK_INPUT;
7999
8000 {
d9cdbb3d
RS
8001 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8002 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8003 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8004
8005 /* Make sure the values are reasonable, and try to preserve
8006 the distance between start and end. */
12ba150f
JB
8007 {
8008 int length = end - start;
8009
8010 if (start < 0)
8011 start = 0;
8012 else if (start > top_range)
8013 start = top_range;
8014 end = start + length;
8015
8016 if (end < start)
8017 end = start;
8018 else if (end > top_range && ! dragging)
8019 end = top_range;
8020 }
f451eb13 8021
ab648270 8022 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8023 XSETINT (bar->start, start);
8024 XSETINT (bar->end, end);
f451eb13 8025
12ba150f
JB
8026 /* Clip the end position, just for display. */
8027 if (end > top_range)
8028 end = top_range;
f451eb13 8029
ab648270 8030 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8031 below top positions, to make sure the handle is always at least
8032 that many pixels tall. */
ab648270 8033 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8034
12ba150f
JB
8035 /* Draw the empty space above the handle. Note that we can't clear
8036 zero-height areas; that means "clear to end of window." */
8037 if (0 < start)
334208b7 8038 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8039
12ba150f 8040 /* x, y, width, height, and exposures. */
ab648270
JB
8041 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8042 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8043 inside_width, start,
8044 False);
f451eb13 8045
06a2c219
GM
8046 /* Change to proper foreground color if one is specified. */
8047 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8048 XSetForeground (FRAME_X_DISPLAY (f), gc,
8049 f->output_data.x->scroll_bar_foreground_pixel);
8050
12ba150f 8051 /* Draw the handle itself. */
334208b7 8052 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8053
12ba150f 8054 /* x, y, width, height */
ab648270
JB
8055 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8056 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8057 inside_width, end - start);
f451eb13 8058
06a2c219
GM
8059 /* Restore the foreground color of the GC if we changed it above. */
8060 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8061 XSetForeground (FRAME_X_DISPLAY (f), gc,
8062 f->output_data.x->foreground_pixel);
f451eb13 8063
12ba150f
JB
8064 /* Draw the empty space below the handle. Note that we can't
8065 clear zero-height areas; that means "clear to end of window." */
8066 if (end < inside_height)
334208b7 8067 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8068
12ba150f 8069 /* x, y, width, height, and exposures. */
ab648270
JB
8070 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8071 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8072 inside_width, inside_height - end,
8073 False);
f451eb13 8074
f451eb13
JB
8075 }
8076
f451eb13
JB
8077 UNBLOCK_INPUT;
8078}
8079
5c187dee 8080#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8081
06a2c219
GM
8082/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8083 nil. */
58769bee 8084
12ba150f 8085static void
ab648270
JB
8086x_scroll_bar_remove (bar)
8087 struct scroll_bar *bar;
12ba150f 8088{
12ba150f
JB
8089 BLOCK_INPUT;
8090
06a2c219
GM
8091#if USE_TOOLKIT_SCROLL_BARS
8092 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8093#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8094 {
8095 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8096 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8097 }
06a2c219
GM
8098#endif /* not USE_TOOLKIT_SCROLL_BARS */
8099
ab648270
JB
8100 /* Disassociate this scroll bar from its window. */
8101 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8102
8103 UNBLOCK_INPUT;
8104}
8105
06a2c219 8106
12ba150f
JB
8107/* Set the handle of the vertical scroll bar for WINDOW to indicate
8108 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8109 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8110 create one. */
06a2c219 8111
12ba150f 8112static void
06a2c219
GM
8113XTset_vertical_scroll_bar (w, portion, whole, position)
8114 struct window *w;
f451eb13
JB
8115 int portion, whole, position;
8116{
06a2c219 8117 struct frame *f = XFRAME (w->frame);
ab648270 8118 struct scroll_bar *bar;
3c6ede7b 8119 int top, height, left, sb_left, width, sb_width;
06a2c219 8120 int window_x, window_y, window_width, window_height;
06a2c219 8121
3c6ede7b 8122 /* Get window dimensions. */
06a2c219 8123 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8124 top = window_y;
8125 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8126 height = window_height;
06a2c219 8127
3c6ede7b 8128 /* Compute the left edge of the scroll bar area. */
06a2c219 8129 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8130 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8131 else
8132 left = XFASTINT (w->left);
8133 left *= CANON_X_UNIT (f);
8134 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8135
8136 /* Compute the width of the scroll bar which might be less than
8137 the width of the area reserved for the scroll bar. */
8138 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8139 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8140 else
3c6ede7b 8141 sb_width = width;
12ba150f 8142
3c6ede7b
GM
8143 /* Compute the left edge of the scroll bar. */
8144#ifdef USE_TOOLKIT_SCROLL_BARS
8145 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8146 sb_left = left + width - sb_width - (width - sb_width) / 2;
8147 else
8148 sb_left = left + (width - sb_width) / 2;
8149#else
8150 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8151 sb_left = left + width - sb_width;
8152 else
8153 sb_left = left;
8154#endif
8155
ab648270 8156 /* Does the scroll bar exist yet? */
06a2c219 8157 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8158 {
80c32bcc 8159 BLOCK_INPUT;
3c6ede7b
GM
8160 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8161 left, top, width, height, False);
80c32bcc 8162 UNBLOCK_INPUT;
3c6ede7b
GM
8163 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8164 }
f451eb13 8165 else
12ba150f
JB
8166 {
8167 /* It may just need to be moved and resized. */
06a2c219
GM
8168 unsigned int mask = 0;
8169
8170 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8171
8172 BLOCK_INPUT;
8173
3c6ede7b 8174 if (sb_left != XINT (bar->left))
06a2c219 8175 mask |= CWX;
3c6ede7b 8176 if (top != XINT (bar->top))
06a2c219 8177 mask |= CWY;
3c6ede7b 8178 if (sb_width != XINT (bar->width))
06a2c219 8179 mask |= CWWidth;
3c6ede7b 8180 if (height != XINT (bar->height))
06a2c219
GM
8181 mask |= CWHeight;
8182
8183#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8184
8185 /* Since toolkit scroll bars are smaller than the space reserved
8186 for them on the frame, we have to clear "under" them. */
8187 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8188 left, top, width, height, False);
06a2c219
GM
8189
8190 /* Move/size the scroll bar widget. */
8191 if (mask)
8192 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8193 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8194 top,
8195 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8196 height, 0);
06a2c219
GM
8197
8198#else /* not USE_TOOLKIT_SCROLL_BARS */
8199
e1f6572f
RS
8200 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8201 {
8202 /* Clear areas not covered by the scroll bar. This makes sure a
8203 previous mode line display is cleared after C-x 2 C-x 1, for
8204 example. Non-toolkit scroll bars are as wide as the area
8205 reserved for scroll bars - trim at both sides. */
8206 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8207 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8208 height, False);
8209 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8210 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8211 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8212 height, False);
8213 }
06a2c219
GM
8214
8215 /* Move/size the scroll bar window. */
8216 if (mask)
8217 {
8218 XWindowChanges wc;
8219
3c6ede7b
GM
8220 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8221 wc.y = top;
8222 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8223 wc.height = height;
06a2c219
GM
8224 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8225 mask, &wc);
8226 }
8227
8228#endif /* not USE_TOOLKIT_SCROLL_BARS */
8229
8230 /* Remember new settings. */
3c6ede7b
GM
8231 XSETINT (bar->left, sb_left);
8232 XSETINT (bar->top, top);
8233 XSETINT (bar->width, sb_width);
8234 XSETINT (bar->height, height);
06a2c219
GM
8235
8236 UNBLOCK_INPUT;
12ba150f 8237 }
f451eb13 8238
06a2c219
GM
8239#if USE_TOOLKIT_SCROLL_BARS
8240 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8241#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8242 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8243 dragged. */
12ba150f 8244 if (NILP (bar->dragging))
f451eb13 8245 {
92857db0 8246 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8247
12ba150f 8248 if (whole == 0)
ab648270 8249 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8250 else
8251 {
43f868f5
JB
8252 int start = ((double) position * top_range) / whole;
8253 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8254 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8255 }
f451eb13 8256 }
06a2c219 8257#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8258
06a2c219 8259 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8260}
8261
12ba150f 8262
f451eb13 8263/* The following three hooks are used when we're doing a thorough
ab648270 8264 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8265 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8266 away is a real pain - "Can you say set-window-configuration, boys
8267 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8268 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8269 from the fiery pit when we actually redisplay its window. */
f451eb13 8270
ab648270
JB
8271/* Arrange for all scroll bars on FRAME to be removed at the next call
8272 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8273 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8274
58769bee 8275static void
ab648270 8276XTcondemn_scroll_bars (frame)
f451eb13
JB
8277 FRAME_PTR frame;
8278{
f9e24cb9
RS
8279 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8280 while (! NILP (FRAME_SCROLL_BARS (frame)))
8281 {
8282 Lisp_Object bar;
8283 bar = FRAME_SCROLL_BARS (frame);
8284 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8285 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8286 XSCROLL_BAR (bar)->prev = Qnil;
8287 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8288 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8289 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8290 }
f451eb13
JB
8291}
8292
06a2c219 8293/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8294 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8295static void
ab648270 8296XTredeem_scroll_bar (window)
12ba150f 8297 struct window *window;
f451eb13 8298{
ab648270 8299 struct scroll_bar *bar;
12ba150f 8300
ab648270
JB
8301 /* We can't redeem this window's scroll bar if it doesn't have one. */
8302 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8303 abort ();
8304
ab648270 8305 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8306
8307 /* Unlink it from the condemned list. */
8308 {
8309 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8310
8311 if (NILP (bar->prev))
8312 {
8313 /* If the prev pointer is nil, it must be the first in one of
8314 the lists. */
ab648270 8315 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8316 /* It's not condemned. Everything's fine. */
8317 return;
ab648270
JB
8318 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8319 window->vertical_scroll_bar))
8320 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8321 else
8322 /* If its prev pointer is nil, it must be at the front of
8323 one or the other! */
8324 abort ();
8325 }
8326 else
ab648270 8327 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8328
8329 if (! NILP (bar->next))
ab648270 8330 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8331
ab648270 8332 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8333 bar->prev = Qnil;
e0c1aef2 8334 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8335 if (! NILP (bar->next))
e0c1aef2 8336 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8337 }
f451eb13
JB
8338}
8339
ab648270
JB
8340/* Remove all scroll bars on FRAME that haven't been saved since the
8341 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8342
f451eb13 8343static void
ab648270 8344XTjudge_scroll_bars (f)
12ba150f 8345 FRAME_PTR f;
f451eb13 8346{
12ba150f 8347 Lisp_Object bar, next;
f451eb13 8348
ab648270 8349 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8350
8351 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8352 more events on the hapless scroll bars. */
8353 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8354
8355 for (; ! NILP (bar); bar = next)
f451eb13 8356 {
ab648270 8357 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8358
ab648270 8359 x_scroll_bar_remove (b);
12ba150f
JB
8360
8361 next = b->next;
8362 b->next = b->prev = Qnil;
f451eb13 8363 }
12ba150f 8364
ab648270 8365 /* Now there should be no references to the condemned scroll bars,
12ba150f 8366 and they should get garbage-collected. */
f451eb13
JB
8367}
8368
8369
06a2c219
GM
8370/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8371 is a no-op when using toolkit scroll bars.
ab648270
JB
8372
8373 This may be called from a signal handler, so we have to ignore GC
8374 mark bits. */
06a2c219 8375
f451eb13 8376static void
ab648270
JB
8377x_scroll_bar_expose (bar, event)
8378 struct scroll_bar *bar;
f451eb13
JB
8379 XEvent *event;
8380{
06a2c219
GM
8381#ifndef USE_TOOLKIT_SCROLL_BARS
8382
ab648270 8383 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8384 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8385 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8386 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8387
f451eb13
JB
8388 BLOCK_INPUT;
8389
ab648270 8390 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8391
06a2c219 8392 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8393 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8394
8395 /* x, y, width, height */
d9cdbb3d 8396 0, 0,
3cbd2e0b 8397 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8398 XINT (bar->height) - 1);
8399
f451eb13 8400 UNBLOCK_INPUT;
06a2c219
GM
8401
8402#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8403}
8404
ab648270
JB
8405/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8406 is set to something other than no_event, it is enqueued.
8407
8408 This may be called from a signal handler, so we have to ignore GC
8409 mark bits. */
06a2c219 8410
5c187dee
GM
8411#ifndef USE_TOOLKIT_SCROLL_BARS
8412
f451eb13 8413static void
ab648270
JB
8414x_scroll_bar_handle_click (bar, event, emacs_event)
8415 struct scroll_bar *bar;
f451eb13
JB
8416 XEvent *event;
8417 struct input_event *emacs_event;
8418{
0299d313 8419 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8420 abort ();
8421
ab648270 8422 emacs_event->kind = scroll_bar_click;
69388238 8423 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8424 emacs_event->modifiers
8425 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8426 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8427 event->xbutton.state)
8428 | (event->type == ButtonRelease
8429 ? up_modifier
8430 : down_modifier));
12ba150f 8431 emacs_event->frame_or_window = bar->window;
f451eb13 8432 emacs_event->timestamp = event->xbutton.time;
12ba150f 8433 {
06a2c219 8434#if 0
d9cdbb3d 8435 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8436 int internal_height
d9cdbb3d 8437 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8438#endif
0299d313 8439 int top_range
d9cdbb3d 8440 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8441 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8442
8443 if (y < 0) y = 0;
8444 if (y > top_range) y = top_range;
8445
8446 if (y < XINT (bar->start))
ab648270
JB
8447 emacs_event->part = scroll_bar_above_handle;
8448 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8449 emacs_event->part = scroll_bar_handle;
12ba150f 8450 else
ab648270 8451 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8452
8453 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8454 they want to drag it. Lisp code needs to be able to decide
8455 whether or not we're dragging. */
929787e1 8456#if 0
12ba150f
JB
8457 /* If the user has just clicked on the handle, record where they're
8458 holding it. */
8459 if (event->type == ButtonPress
ab648270 8460 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8461 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8462#endif
12ba150f
JB
8463
8464 /* If the user has released the handle, set it to its final position. */
8465 if (event->type == ButtonRelease
8466 && ! NILP (bar->dragging))
8467 {
8468 int new_start = y - XINT (bar->dragging);
8469 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8470
ab648270 8471 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8472 bar->dragging = Qnil;
8473 }
f451eb13 8474
5116f055
JB
8475 /* Same deal here as the other #if 0. */
8476#if 0
58769bee 8477 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8478 the handle. */
ab648270 8479 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8480 emacs_event->x = bar->start;
8481 else
e0c1aef2 8482 XSETINT (emacs_event->x, y);
5116f055 8483#else
e0c1aef2 8484 XSETINT (emacs_event->x, y);
5116f055 8485#endif
f451eb13 8486
e0c1aef2 8487 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8488 }
8489}
f451eb13 8490
ab648270
JB
8491/* Handle some mouse motion while someone is dragging the scroll bar.
8492
8493 This may be called from a signal handler, so we have to ignore GC
8494 mark bits. */
06a2c219 8495
f451eb13 8496static void
ab648270
JB
8497x_scroll_bar_note_movement (bar, event)
8498 struct scroll_bar *bar;
f451eb13
JB
8499 XEvent *event;
8500{
39d8bb4d
KH
8501 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8502
f451eb13
JB
8503 last_mouse_movement_time = event->xmotion.time;
8504
39d8bb4d 8505 f->mouse_moved = 1;
e0c1aef2 8506 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8507
8508 /* If we're dragging the bar, display it. */
ab648270 8509 if (! GC_NILP (bar->dragging))
f451eb13
JB
8510 {
8511 /* Where should the handle be now? */
12ba150f 8512 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8513
12ba150f 8514 if (new_start != XINT (bar->start))
f451eb13 8515 {
12ba150f 8516 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8517
ab648270 8518 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8519 }
8520 }
f451eb13
JB
8521}
8522
5c187dee
GM
8523#endif /* !USE_TOOLKIT_SCROLL_BARS */
8524
12ba150f 8525/* Return information to the user about the current position of the mouse
ab648270 8526 on the scroll bar. */
06a2c219 8527
12ba150f 8528static void
334208b7
RS
8529x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8530 FRAME_PTR *fp;
12ba150f 8531 Lisp_Object *bar_window;
ab648270 8532 enum scroll_bar_part *part;
12ba150f
JB
8533 Lisp_Object *x, *y;
8534 unsigned long *time;
8535{
ab648270 8536 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8537 Window w = SCROLL_BAR_X_WINDOW (bar);
8538 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8539 int win_x, win_y;
559cb2fb
JB
8540 Window dummy_window;
8541 int dummy_coord;
8542 unsigned int dummy_mask;
12ba150f 8543
cf7cb199
JB
8544 BLOCK_INPUT;
8545
ab648270 8546 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8547 report that. */
334208b7 8548 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8549
559cb2fb
JB
8550 /* Root, child, root x and root y. */
8551 &dummy_window, &dummy_window,
8552 &dummy_coord, &dummy_coord,
12ba150f 8553
559cb2fb
JB
8554 /* Position relative to scroll bar. */
8555 &win_x, &win_y,
12ba150f 8556
559cb2fb
JB
8557 /* Mouse buttons and modifier keys. */
8558 &dummy_mask))
7a13e894 8559 ;
559cb2fb
JB
8560 else
8561 {
06a2c219 8562#if 0
559cb2fb 8563 int inside_height
d9cdbb3d 8564 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8565#endif
559cb2fb 8566 int top_range
d9cdbb3d 8567 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8568
8569 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8570
8571 if (! NILP (bar->dragging))
8572 win_y -= XINT (bar->dragging);
8573
8574 if (win_y < 0)
8575 win_y = 0;
8576 if (win_y > top_range)
8577 win_y = top_range;
8578
334208b7 8579 *fp = f;
7a13e894 8580 *bar_window = bar->window;
559cb2fb
JB
8581
8582 if (! NILP (bar->dragging))
8583 *part = scroll_bar_handle;
8584 else if (win_y < XINT (bar->start))
8585 *part = scroll_bar_above_handle;
8586 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8587 *part = scroll_bar_handle;
8588 else
8589 *part = scroll_bar_below_handle;
12ba150f 8590
e0c1aef2
KH
8591 XSETINT (*x, win_y);
8592 XSETINT (*y, top_range);
12ba150f 8593
39d8bb4d 8594 f->mouse_moved = 0;
559cb2fb
JB
8595 last_mouse_scroll_bar = Qnil;
8596 }
12ba150f 8597
559cb2fb 8598 *time = last_mouse_movement_time;
cf7cb199 8599
cf7cb199 8600 UNBLOCK_INPUT;
12ba150f
JB
8601}
8602
f451eb13 8603
dbc4e1c1 8604/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8605 background colors, and the scroll bars may need to be redrawn.
8606 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8607 redraw them. */
8608
dfcf069d 8609void
ab648270 8610x_scroll_bar_clear (f)
dbc4e1c1
JB
8611 FRAME_PTR f;
8612{
06a2c219 8613#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8614 Lisp_Object bar;
8615
b80c363e
RS
8616 /* We can have scroll bars even if this is 0,
8617 if we just turned off scroll bar mode.
8618 But in that case we should not clear them. */
8619 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8620 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8621 bar = XSCROLL_BAR (bar)->next)
8622 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8623 0, 0, 0, 0, True);
06a2c219 8624#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8625}
8626
06a2c219 8627/* This processes Expose events from the menu-bar specific X event
19126e11 8628 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8629 when handling menu-bar or pop-up items. */
3afe33e7 8630
06a2c219 8631int
3afe33e7
RS
8632process_expose_from_menu (event)
8633 XEvent event;
8634{
8635 FRAME_PTR f;
19126e11 8636 struct x_display_info *dpyinfo;
06a2c219 8637 int frame_exposed_p = 0;
3afe33e7 8638
f94397b5
KH
8639 BLOCK_INPUT;
8640
19126e11
KH
8641 dpyinfo = x_display_info_for_display (event.xexpose.display);
8642 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8643 if (f)
8644 {
8645 if (f->async_visible == 0)
8646 {
8647 f->async_visible = 1;
8648 f->async_iconified = 0;
06c488fd 8649 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8650 SET_FRAME_GARBAGED (f);
8651 }
8652 else
8653 {
06a2c219
GM
8654 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8655 event.xexpose.x, event.xexpose.y,
8656 event.xexpose.width, event.xexpose.height);
8657 frame_exposed_p = 1;
3afe33e7
RS
8658 }
8659 }
8660 else
8661 {
8662 struct scroll_bar *bar
8663 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8664
3afe33e7
RS
8665 if (bar)
8666 x_scroll_bar_expose (bar, &event);
8667 }
f94397b5
KH
8668
8669 UNBLOCK_INPUT;
06a2c219 8670 return frame_exposed_p;
3afe33e7 8671}
09756a85
RS
8672\f
8673/* Define a queue to save up SelectionRequest events for later handling. */
8674
8675struct selection_event_queue
8676 {
8677 XEvent event;
8678 struct selection_event_queue *next;
8679 };
8680
8681static struct selection_event_queue *queue;
8682
8683/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8684
09756a85
RS
8685static int x_queue_selection_requests;
8686
8687/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8688
09756a85 8689static void
334208b7
RS
8690x_queue_event (f, event)
8691 FRAME_PTR f;
09756a85
RS
8692 XEvent *event;
8693{
8694 struct selection_event_queue *queue_tmp
06a2c219 8695 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8696
58769bee 8697 if (queue_tmp != NULL)
09756a85
RS
8698 {
8699 queue_tmp->event = *event;
8700 queue_tmp->next = queue;
8701 queue = queue_tmp;
8702 }
8703}
8704
8705/* Take all the queued events and put them back
8706 so that they get processed afresh. */
8707
8708static void
db3906fd
RS
8709x_unqueue_events (display)
8710 Display *display;
09756a85 8711{
58769bee 8712 while (queue != NULL)
09756a85
RS
8713 {
8714 struct selection_event_queue *queue_tmp = queue;
db3906fd 8715 XPutBackEvent (display, &queue_tmp->event);
09756a85 8716 queue = queue_tmp->next;
06a2c219 8717 xfree ((char *)queue_tmp);
09756a85
RS
8718 }
8719}
8720
8721/* Start queuing SelectionRequest events. */
8722
8723void
db3906fd
RS
8724x_start_queuing_selection_requests (display)
8725 Display *display;
09756a85
RS
8726{
8727 x_queue_selection_requests++;
8728}
8729
8730/* Stop queuing SelectionRequest events. */
8731
8732void
db3906fd
RS
8733x_stop_queuing_selection_requests (display)
8734 Display *display;
09756a85
RS
8735{
8736 x_queue_selection_requests--;
db3906fd 8737 x_unqueue_events (display);
09756a85 8738}
f451eb13
JB
8739\f
8740/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8741
06a2c219 8742/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8743 but we have to put it out here, since static variables within functions
8744 sometimes don't work. */
06a2c219 8745
dc6f92b8
JB
8746static Time enter_timestamp;
8747
11edeb03 8748/* This holds the state XLookupString needs to implement dead keys
58769bee 8749 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8750 says that a portable program can't use this, but Stephen Gildea assures
8751 me that letting the compiler initialize it to zeros will work okay.
8752
8753 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8754 given for enter_time stamp, above. */
8755
11edeb03
JB
8756static XComposeStatus compose_status;
8757
10e6549c
RS
8758/* Record the last 100 characters stored
8759 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8760
2224b905
RS
8761static int temp_index;
8762static short temp_buffer[100];
10e6549c 8763
7a13e894
RS
8764/* Set this to nonzero to fake an "X I/O error"
8765 on a particular display. */
06a2c219 8766
7a13e894
RS
8767struct x_display_info *XTread_socket_fake_io_error;
8768
2224b905
RS
8769/* When we find no input here, we occasionally do a no-op command
8770 to verify that the X server is still running and we can still talk with it.
8771 We try all the open displays, one by one.
8772 This variable is used for cycling thru the displays. */
06a2c219 8773
2224b905
RS
8774static struct x_display_info *next_noop_dpyinfo;
8775
06a2c219
GM
8776#define SET_SAVED_MENU_EVENT(size) \
8777 do \
8778 { \
8779 if (f->output_data.x->saved_menu_event == 0) \
8780 f->output_data.x->saved_menu_event \
8781 = (XEvent *) xmalloc (sizeof (XEvent)); \
8782 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8783 if (numchars >= 1) \
8784 { \
8785 bufp->kind = menu_bar_activate_event; \
8786 XSETFRAME (bufp->frame_or_window, f); \
8787 bufp++; \
8788 count++; \
8789 numchars--; \
8790 } \
8791 } \
8792 while (0)
8793
8805890a 8794#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8795#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8796
dc6f92b8
JB
8797/* Read events coming from the X server.
8798 This routine is called by the SIGIO handler.
8799 We return as soon as there are no more events to be read.
8800
8801 Events representing keys are stored in buffer BUFP,
8802 which can hold up to NUMCHARS characters.
8803 We return the number of characters stored into the buffer,
8804 thus pretending to be `read'.
8805
dc6f92b8
JB
8806 EXPECTED is nonzero if the caller knows input is available. */
8807
7c5283e4 8808int
f66868ba 8809XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8810 register int sd;
8805890a
KH
8811 /* register */ struct input_event *bufp;
8812 /* register */ int numchars;
dc6f92b8
JB
8813 int expected;
8814{
8815 int count = 0;
8816 int nbytes = 0;
dc6f92b8 8817 XEvent event;
f676886a 8818 struct frame *f;
66f55a9d 8819 int event_found = 0;
334208b7 8820 struct x_display_info *dpyinfo;
dc6f92b8 8821
9ac0d9e0 8822 if (interrupt_input_blocked)
dc6f92b8 8823 {
9ac0d9e0 8824 interrupt_input_pending = 1;
dc6f92b8
JB
8825 return -1;
8826 }
8827
9ac0d9e0 8828 interrupt_input_pending = 0;
dc6f92b8 8829 BLOCK_INPUT;
c0a04927
RS
8830
8831 /* So people can tell when we have read the available input. */
8832 input_signal_count++;
8833
dc6f92b8 8834 if (numchars <= 0)
06a2c219 8835 abort (); /* Don't think this happens. */
dc6f92b8 8836
7a13e894
RS
8837 /* Find the display we are supposed to read input for.
8838 It's the one communicating on descriptor SD. */
8839 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8840 {
8841#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8842#ifdef FIOSNBIO
7a13e894
RS
8843 /* If available, Xlib uses FIOSNBIO to make the socket
8844 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8845 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8846 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8847 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8848#endif /* ! defined (FIOSNBIO) */
7a13e894 8849#endif
dc6f92b8 8850
7a13e894
RS
8851#if 0 /* This code can't be made to work, with multiple displays,
8852 and appears not to be used on any system any more.
8853 Also keyboard.c doesn't turn O_NDELAY on and off
8854 for X connections. */
dc6f92b8
JB
8855#ifndef SIGIO
8856#ifndef HAVE_SELECT
7a13e894
RS
8857 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8858 {
8859 extern int read_alarm_should_throw;
8860 read_alarm_should_throw = 1;
8861 XPeekEvent (dpyinfo->display, &event);
8862 read_alarm_should_throw = 0;
8863 }
c118dd06
JB
8864#endif /* HAVE_SELECT */
8865#endif /* SIGIO */
7a13e894 8866#endif
dc6f92b8 8867
7a13e894
RS
8868 /* For debugging, this gives a way to fake an I/O error. */
8869 if (dpyinfo == XTread_socket_fake_io_error)
8870 {
8871 XTread_socket_fake_io_error = 0;
8872 x_io_error_quitter (dpyinfo->display);
8873 }
dc6f92b8 8874
06a2c219 8875 while (XPending (dpyinfo->display))
dc6f92b8 8876 {
7a13e894 8877 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8878
8879 if (display_busy_cursor_p)
8880 {
8881 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8882 display until the next X event is read and we come
8883 here again. Setting it to 1 inhibits busy-cursor
8884 display for direct commands. */
8885 if (event.type == MotionNotify
8886 || event.type == EnterNotify
8887 || (dpyinfo->grabbed
8888 && event.type != ButtonRelease))
8889 inhibit_busy_cursor = 2;
8890 else
8891 inhibit_busy_cursor = 1;
8892 }
8893
531483fb 8894#ifdef HAVE_X_I18N
d1bc4182 8895 {
f2be1146
GM
8896 /* Filter events for the current X input method.
8897 XFilterEvent returns non-zero if the input method has
8898 consumed the event. We pass the frame's X window to
8899 XFilterEvent because that's the one for which the IC
8900 was created. */
f5d11644
GM
8901 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8902 event.xclient.window);
8903 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
8904 break;
8905 }
0cd6403b 8906#endif
7a13e894
RS
8907 event_found = 1;
8908
8909 switch (event.type)
8910 {
8911 case ClientMessage:
c047688c 8912 {
7a13e894
RS
8913 if (event.xclient.message_type
8914 == dpyinfo->Xatom_wm_protocols
8915 && event.xclient.format == 32)
c047688c 8916 {
7a13e894
RS
8917 if (event.xclient.data.l[0]
8918 == dpyinfo->Xatom_wm_take_focus)
c047688c 8919 {
8c1a6a84
RS
8920 /* Use x_any_window_to_frame because this
8921 could be the shell widget window
8922 if the frame has no title bar. */
8923 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8924#ifdef HAVE_X_I18N
8925 /* Not quite sure this is needed -pd */
8c1a6a84 8926 if (f && FRAME_XIC (f))
6c183ba5
RS
8927 XSetICFocus (FRAME_XIC (f));
8928#endif
f1da8f06
GM
8929#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8930 instructs the WM to set the input focus automatically for
8931 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8932 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8933 it has set the focus. So, XSetInputFocus below is not
8934 needed.
8935
8936 The call to XSetInputFocus below has also caused trouble. In
8937 cases where the XSetInputFocus done by the WM and the one
8938 below are temporally close (on a fast machine), the call
8939 below can generate additional FocusIn events which confuse
8940 Emacs. */
8941
bf7253f4
RS
8942 /* Since we set WM_TAKE_FOCUS, we must call
8943 XSetInputFocus explicitly. But not if f is null,
8944 since that might be an event for a deleted frame. */
7a13e894 8945 if (f)
bf7253f4
RS
8946 {
8947 Display *d = event.xclient.display;
8948 /* Catch and ignore errors, in case window has been
8949 iconified by a window manager such as GWM. */
8950 int count = x_catch_errors (d);
8951 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8952 /* The ICCCM says this is
8953 the only valid choice. */
8954 RevertToParent,
bf7253f4
RS
8955 event.xclient.data.l[1]);
8956 /* This is needed to detect the error
8957 if there is an error. */
8958 XSync (d, False);
8959 x_uncatch_errors (d, count);
8960 }
7a13e894 8961 /* Not certain about handling scroll bars here */
f1da8f06 8962#endif /* 0 */
c047688c 8963 }
7a13e894
RS
8964 else if (event.xclient.data.l[0]
8965 == dpyinfo->Xatom_wm_save_yourself)
8966 {
8967 /* Save state modify the WM_COMMAND property to
06a2c219 8968 something which can reinstate us. This notifies
7a13e894
RS
8969 the session manager, who's looking for such a
8970 PropertyNotify. Can restart processing when
06a2c219 8971 a keyboard or mouse event arrives. */
7a13e894
RS
8972 if (numchars > 0)
8973 {
19126e11
KH
8974 f = x_top_window_to_frame (dpyinfo,
8975 event.xclient.window);
7a13e894
RS
8976
8977 /* This is just so we only give real data once
8978 for a single Emacs process. */
b86bd3dd 8979 if (f == SELECTED_FRAME ())
7a13e894
RS
8980 XSetCommand (FRAME_X_DISPLAY (f),
8981 event.xclient.window,
8982 initial_argv, initial_argc);
f000f5c5 8983 else if (f)
7a13e894
RS
8984 XSetCommand (FRAME_X_DISPLAY (f),
8985 event.xclient.window,
8986 0, 0);
8987 }
8988 }
8989 else if (event.xclient.data.l[0]
8990 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8991 {
19126e11
KH
8992 struct frame *f
8993 = x_any_window_to_frame (dpyinfo,
8994 event.xclient.window);
1fb20991 8995
7a13e894
RS
8996 if (f)
8997 {
8998 if (numchars == 0)
8999 abort ();
1fb20991 9000
7a13e894
RS
9001 bufp->kind = delete_window_event;
9002 XSETFRAME (bufp->frame_or_window, f);
9003 bufp++;
9004
9005 count += 1;
9006 numchars -= 1;
9007 }
1fb20991 9008 }
c047688c 9009 }
7a13e894
RS
9010 else if (event.xclient.message_type
9011 == dpyinfo->Xatom_wm_configure_denied)
9012 {
9013 }
9014 else if (event.xclient.message_type
9015 == dpyinfo->Xatom_wm_window_moved)
9016 {
9017 int new_x, new_y;
19126e11
KH
9018 struct frame *f
9019 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9020
7a13e894
RS
9021 new_x = event.xclient.data.s[0];
9022 new_y = event.xclient.data.s[1];
1fb20991 9023
7a13e894
RS
9024 if (f)
9025 {
7556890b
RS
9026 f->output_data.x->left_pos = new_x;
9027 f->output_data.x->top_pos = new_y;
7a13e894 9028 }
1fb20991 9029 }
0fdff6bb 9030#ifdef HACK_EDITRES
7a13e894
RS
9031 else if (event.xclient.message_type
9032 == dpyinfo->Xatom_editres)
9033 {
19126e11
KH
9034 struct frame *f
9035 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9036 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9037 &event, NULL);
7a13e894 9038 }
0fdff6bb 9039#endif /* HACK_EDITRES */
06a2c219
GM
9040 else if ((event.xclient.message_type
9041 == dpyinfo->Xatom_DONE)
9042 || (event.xclient.message_type
9043 == dpyinfo->Xatom_PAGE))
9044 {
9045 /* Ghostview job completed. Kill it. We could
9046 reply with "Next" if we received "Page", but we
9047 currently never do because we are interested in
9048 images, only, which should have 1 page. */
06a2c219
GM
9049 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9050 struct frame *f
9051 = x_window_to_frame (dpyinfo, event.xclient.window);
9052 x_kill_gs_process (pixmap, f);
9053 expose_frame (f, 0, 0, 0, 0);
9054 }
9055#ifdef USE_TOOLKIT_SCROLL_BARS
9056 /* Scroll bar callbacks send a ClientMessage from which
9057 we construct an input_event. */
9058 else if (event.xclient.message_type
9059 == dpyinfo->Xatom_Scrollbar)
9060 {
f9632fc8
GM
9061 if (display_busy_cursor_p)
9062 inhibit_busy_cursor = 2;
06a2c219
GM
9063 x_scroll_bar_to_input_event (&event, bufp);
9064 ++bufp, ++count, --numchars;
9065 goto out;
9066 }
9067#endif /* USE_TOOLKIT_SCROLL_BARS */
9068 else
9069 goto OTHER;
7a13e894
RS
9070 }
9071 break;
dc6f92b8 9072
7a13e894 9073 case SelectionNotify:
3afe33e7 9074#ifdef USE_X_TOOLKIT
19126e11 9075 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9076 goto OTHER;
3afe33e7 9077#endif /* not USE_X_TOOLKIT */
dfcf069d 9078 x_handle_selection_notify (&event.xselection);
7a13e894 9079 break;
d56a553a 9080
06a2c219 9081 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9082#ifdef USE_X_TOOLKIT
19126e11 9083 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9084 goto OTHER;
3afe33e7 9085#endif /* USE_X_TOOLKIT */
7a13e894
RS
9086 {
9087 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9088
7a13e894
RS
9089 if (numchars == 0)
9090 abort ();
d56a553a 9091
7a13e894
RS
9092 bufp->kind = selection_clear_event;
9093 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9094 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9095 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9096 bufp->frame_or_window = Qnil;
7a13e894 9097 bufp++;
d56a553a 9098
7a13e894
RS
9099 count += 1;
9100 numchars -= 1;
9101 }
9102 break;
dc6f92b8 9103
06a2c219 9104 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9105#ifdef USE_X_TOOLKIT
19126e11 9106 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9107 goto OTHER;
3afe33e7 9108#endif /* USE_X_TOOLKIT */
7a13e894 9109 if (x_queue_selection_requests)
19126e11 9110 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9111 &event);
9112 else
9113 {
9114 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9115
7a13e894
RS
9116 if (numchars == 0)
9117 abort ();
9118
9119 bufp->kind = selection_request_event;
9120 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9121 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9122 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9123 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9124 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9125 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9126 bufp->frame_or_window = Qnil;
7a13e894
RS
9127 bufp++;
9128
9129 count += 1;
9130 numchars -= 1;
9131 }
9132 break;
9133
9134 case PropertyNotify:
3afe33e7 9135#ifdef USE_X_TOOLKIT
19126e11 9136 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9137 goto OTHER;
3afe33e7 9138#endif /* not USE_X_TOOLKIT */
dfcf069d 9139 x_handle_property_notify (&event.xproperty);
7a13e894 9140 break;
dc6f92b8 9141
7a13e894 9142 case ReparentNotify:
19126e11 9143 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9144 if (f)
9145 {
9146 int x, y;
7556890b 9147 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9148 x_real_positions (f, &x, &y);
7556890b
RS
9149 f->output_data.x->left_pos = x;
9150 f->output_data.x->top_pos = y;
7a13e894
RS
9151 }
9152 break;
3bd330d4 9153
7a13e894 9154 case Expose:
19126e11 9155 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9156 if (f)
dc6f92b8 9157 {
7a13e894
RS
9158 if (f->async_visible == 0)
9159 {
9160 f->async_visible = 1;
9161 f->async_iconified = 0;
06c488fd 9162 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9163 SET_FRAME_GARBAGED (f);
9164 }
9165 else
06a2c219
GM
9166 expose_frame (x_window_to_frame (dpyinfo,
9167 event.xexpose.window),
9168 event.xexpose.x, event.xexpose.y,
9169 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9170 }
9171 else
7a13e894 9172 {
06a2c219
GM
9173#ifdef USE_TOOLKIT_SCROLL_BARS
9174 /* Dispatch event to the widget. */
9175 goto OTHER;
9176#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9177 struct scroll_bar *bar
9178 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9179
7a13e894
RS
9180 if (bar)
9181 x_scroll_bar_expose (bar, &event);
3afe33e7 9182#ifdef USE_X_TOOLKIT
7a13e894
RS
9183 else
9184 goto OTHER;
3afe33e7 9185#endif /* USE_X_TOOLKIT */
06a2c219 9186#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9187 }
9188 break;
dc6f92b8 9189
7a13e894
RS
9190 case GraphicsExpose: /* This occurs when an XCopyArea's
9191 source area was obscured or not
9192 available.*/
19126e11 9193 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9194 if (f)
9195 {
06a2c219
GM
9196 expose_frame (f,
9197 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9198 event.xgraphicsexpose.width,
9199 event.xgraphicsexpose.height);
7a13e894 9200 }
3afe33e7 9201#ifdef USE_X_TOOLKIT
7a13e894
RS
9202 else
9203 goto OTHER;
3afe33e7 9204#endif /* USE_X_TOOLKIT */
7a13e894 9205 break;
dc6f92b8 9206
7a13e894 9207 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9208 source area was completely
9209 available */
7a13e894 9210 break;
dc6f92b8 9211
7a13e894 9212 case UnmapNotify:
06a2c219
GM
9213 /* Redo the mouse-highlight after the tooltip has gone. */
9214 if (event.xmap.window == tip_window)
9215 {
9216 tip_window = 0;
9217 redo_mouse_highlight ();
9218 }
9219
91ea2a7a 9220 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9221 if (f) /* F may no longer exist if
9222 the frame was deleted. */
9223 {
9224 /* While a frame is unmapped, display generation is
9225 disabled; you don't want to spend time updating a
9226 display that won't ever be seen. */
9227 f->async_visible = 0;
9228 /* We can't distinguish, from the event, whether the window
9229 has become iconified or invisible. So assume, if it
9230 was previously visible, than now it is iconified.
1aa6072f
RS
9231 But x_make_frame_invisible clears both
9232 the visible flag and the iconified flag;
9233 and that way, we know the window is not iconified now. */
7a13e894 9234 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9235 {
9236 f->async_iconified = 1;
bddd097c 9237
1aa6072f
RS
9238 bufp->kind = iconify_event;
9239 XSETFRAME (bufp->frame_or_window, f);
9240 bufp++;
9241 count++;
9242 numchars--;
9243 }
7a13e894 9244 }
7a13e894 9245 goto OTHER;
dc6f92b8 9246
7a13e894 9247 case MapNotify:
06a2c219
GM
9248 if (event.xmap.window == tip_window)
9249 /* The tooltip has been drawn already. Avoid
9250 the SET_FRAME_GARBAGED below. */
9251 goto OTHER;
9252
9253 /* We use x_top_window_to_frame because map events can
9254 come for sub-windows and they don't mean that the
9255 frame is visible. */
19126e11 9256 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9257 if (f)
9258 {
9259 f->async_visible = 1;
9260 f->async_iconified = 0;
06c488fd 9261 f->output_data.x->has_been_visible = 1;
dc6f92b8 9262
7a13e894
RS
9263 /* wait_reading_process_input will notice this and update
9264 the frame's display structures. */
9265 SET_FRAME_GARBAGED (f);
bddd097c 9266
d806e720
RS
9267 if (f->iconified)
9268 {
9269 bufp->kind = deiconify_event;
9270 XSETFRAME (bufp->frame_or_window, f);
9271 bufp++;
9272 count++;
9273 numchars--;
9274 }
e73ec6fa 9275 else if (! NILP (Vframe_list)
8e713be6 9276 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9277 /* Force a redisplay sooner or later
9278 to update the frame titles
9279 in case this is the second frame. */
9280 record_asynch_buffer_change ();
7a13e894 9281 }
7a13e894 9282 goto OTHER;
dc6f92b8 9283
7a13e894 9284 case KeyPress:
19126e11 9285 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9286
06a2c219
GM
9287#ifdef USE_MOTIF
9288 /* I couldn't find a way to prevent LessTif scroll bars
9289 from consuming key events. */
9290 if (f == 0)
9291 {
9292 Widget widget = XtWindowToWidget (dpyinfo->display,
9293 event.xkey.window);
9294 if (widget && XmIsScrollBar (widget))
9295 {
9296 widget = XtParent (widget);
9297 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9298 }
9299 }
9300#endif /* USE_MOTIF */
9301
7a13e894
RS
9302 if (f != 0)
9303 {
9304 KeySym keysym, orig_keysym;
9305 /* al%imercury@uunet.uu.net says that making this 81 instead of
9306 80 fixed a bug whereby meta chars made his Emacs hang. */
9307 unsigned char copy_buffer[81];
9308 int modifiers;
64bb1782 9309
7a13e894
RS
9310 event.xkey.state
9311 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9312 extra_keyboard_modifiers);
9313 modifiers = event.xkey.state;
3a2712f9 9314
7a13e894 9315 /* This will have to go some day... */
752a043f 9316
7a13e894
RS
9317 /* make_lispy_event turns chars into control chars.
9318 Don't do it here because XLookupString is too eager. */
9319 event.xkey.state &= ~ControlMask;
5d46f928
RS
9320 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9321 | dpyinfo->super_mod_mask
9322 | dpyinfo->hyper_mod_mask
9323 | dpyinfo->alt_mod_mask);
9324
1cf4a0d1
RS
9325 /* In case Meta is ComposeCharacter,
9326 clear its status. According to Markus Ehrnsperger
9327 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9328 this enables ComposeCharacter to work whether or
9329 not it is combined with Meta. */
9330 if (modifiers & dpyinfo->meta_mod_mask)
9331 bzero (&compose_status, sizeof (compose_status));
9332
6c183ba5
RS
9333#ifdef HAVE_X_I18N
9334 if (FRAME_XIC (f))
9335 {
f5d11644
GM
9336 unsigned char *copy_bufptr = copy_buffer;
9337 int copy_bufsiz = sizeof (copy_buffer);
9338 Status status_return;
9339
6c183ba5 9340 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9341 &event.xkey, copy_bufptr,
9342 copy_bufsiz, &keysym,
6c183ba5 9343 &status_return);
f5d11644
GM
9344 if (status_return == XBufferOverflow)
9345 {
9346 copy_bufsiz = nbytes + 1;
9347 copy_bufptr = (char *) alloca (copy_bufsiz);
9348 nbytes = XmbLookupString (FRAME_XIC (f),
9349 &event.xkey, copy_bufptr,
9350 copy_bufsiz, &keysym,
9351 &status_return);
9352 }
9353
1decb680
PE
9354 if (status_return == XLookupNone)
9355 break;
9356 else if (status_return == XLookupChars)
9357 keysym = NoSymbol;
9358 else if (status_return != XLookupKeySym
9359 && status_return != XLookupBoth)
9360 abort ();
6c183ba5
RS
9361 }
9362 else
9363 nbytes = XLookupString (&event.xkey, copy_buffer,
9364 80, &keysym, &compose_status);
9365#else
0299d313
RS
9366 nbytes = XLookupString (&event.xkey, copy_buffer,
9367 80, &keysym, &compose_status);
6c183ba5 9368#endif
dc6f92b8 9369
7a13e894 9370 orig_keysym = keysym;
55123275 9371
7a13e894
RS
9372 if (numchars > 1)
9373 {
9374 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9375 || keysym == XK_Delete
1097aea0 9376#ifdef XK_ISO_Left_Tab
441affdb 9377 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9378#endif
852bff8f 9379 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9380 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9381 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9382#ifdef HPUX
7a13e894
RS
9383 /* This recognizes the "extended function keys".
9384 It seems there's no cleaner way.
9385 Test IsModifierKey to avoid handling mode_switch
9386 incorrectly. */
9387 || ((unsigned) (keysym) >= XK_Select
9388 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9389#endif
9390#ifdef XK_dead_circumflex
7a13e894 9391 || orig_keysym == XK_dead_circumflex
69388238
RS
9392#endif
9393#ifdef XK_dead_grave
7a13e894 9394 || orig_keysym == XK_dead_grave
69388238
RS
9395#endif
9396#ifdef XK_dead_tilde
7a13e894 9397 || orig_keysym == XK_dead_tilde
69388238
RS
9398#endif
9399#ifdef XK_dead_diaeresis
7a13e894 9400 || orig_keysym == XK_dead_diaeresis
69388238
RS
9401#endif
9402#ifdef XK_dead_macron
7a13e894 9403 || orig_keysym == XK_dead_macron
69388238
RS
9404#endif
9405#ifdef XK_dead_degree
7a13e894 9406 || orig_keysym == XK_dead_degree
69388238
RS
9407#endif
9408#ifdef XK_dead_acute
7a13e894 9409 || orig_keysym == XK_dead_acute
69388238
RS
9410#endif
9411#ifdef XK_dead_cedilla
7a13e894 9412 || orig_keysym == XK_dead_cedilla
69388238
RS
9413#endif
9414#ifdef XK_dead_breve
7a13e894 9415 || orig_keysym == XK_dead_breve
69388238
RS
9416#endif
9417#ifdef XK_dead_ogonek
7a13e894 9418 || orig_keysym == XK_dead_ogonek
69388238
RS
9419#endif
9420#ifdef XK_dead_caron
7a13e894 9421 || orig_keysym == XK_dead_caron
69388238
RS
9422#endif
9423#ifdef XK_dead_doubleacute
7a13e894 9424 || orig_keysym == XK_dead_doubleacute
69388238
RS
9425#endif
9426#ifdef XK_dead_abovedot
7a13e894 9427 || orig_keysym == XK_dead_abovedot
c34790e0 9428#endif
7a13e894
RS
9429 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9430 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9431 /* Any "vendor-specific" key is ok. */
9432 || (orig_keysym & (1 << 28)))
9433 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9434#ifndef HAVE_X11R5
9435#ifdef XK_Mode_switch
7a13e894 9436 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9437#endif
9438#ifdef XK_Num_Lock
7a13e894 9439 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9440#endif
9441#endif /* not HAVE_X11R5 */
7a13e894 9442 ))
dc6f92b8 9443 {
10e6549c
RS
9444 if (temp_index == sizeof temp_buffer / sizeof (short))
9445 temp_index = 0;
7a13e894
RS
9446 temp_buffer[temp_index++] = keysym;
9447 bufp->kind = non_ascii_keystroke;
9448 bufp->code = keysym;
e0c1aef2 9449 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9450 bufp->modifiers
9451 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9452 modifiers);
1113d9db 9453 bufp->timestamp = event.xkey.time;
dc6f92b8 9454 bufp++;
7a13e894
RS
9455 count++;
9456 numchars--;
06a2c219
GM
9457
9458 if (display_busy_cursor_p)
9459 if (keysym != XK_Return || minibuf_level == 0)
9460 inhibit_busy_cursor = 2;
dc6f92b8 9461 }
7a13e894
RS
9462 else if (numchars > nbytes)
9463 {
9464 register int i;
9465
9466 for (i = 0; i < nbytes; i++)
9467 {
9468 if (temp_index == sizeof temp_buffer / sizeof (short))
9469 temp_index = 0;
9470 temp_buffer[temp_index++] = copy_buffer[i];
9471 bufp->kind = ascii_keystroke;
9472 bufp->code = copy_buffer[i];
9473 XSETFRAME (bufp->frame_or_window, f);
9474 bufp->modifiers
9475 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9476 modifiers);
9477 bufp->timestamp = event.xkey.time;
9478 bufp++;
9479 }
9480
9481 count += nbytes;
9482 numchars -= nbytes;
1decb680
PE
9483
9484 if (keysym == NoSymbol)
9485 break;
7a13e894
RS
9486 }
9487 else
9488 abort ();
dc6f92b8 9489 }
10e6549c
RS
9490 else
9491 abort ();
dc6f92b8 9492 }
717ca130 9493 goto OTHER;
f451eb13 9494
f5d11644
GM
9495 case KeyRelease:
9496 goto OTHER;
9497
7a13e894 9498 /* Here's a possible interpretation of the whole
06a2c219
GM
9499 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9500 you get a FocusIn event, you have to get a FocusOut
9501 event before you relinquish the focus. If you
9502 haven't received a FocusIn event, then a mere
9503 LeaveNotify is enough to free you. */
f451eb13 9504
7a13e894 9505 case EnterNotify:
06a2c219
GM
9506 {
9507 int from_menu_bar_p = 0;
9508
9509 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9510
9511#ifdef LESSTIF_VERSION
9512 /* When clicking outside of a menu bar popup to close
9513 it, we get a FocusIn/ EnterNotify sequence of
9514 events. The flag event.xcrossing.focus is not set
9515 in the EnterNotify event of that sequence because
9516 the focus is in the menu bar,
9517 event.xcrossing.window is the frame's X window.
9518 Unconditionally setting the focus frame to null in
9519 this case is not the right thing, because no event
9520 follows that could set the focus frame to the right
9521 value.
9522
9523 This could be a LessTif bug, but I wasn't able to
9524 reproduce the behavior in a simple test program.
9525
9526 (gerd, LessTif 0.88.1). */
9527
9528 if (!event.xcrossing.focus
9529 && f
9530 && f->output_data.x->menubar_widget)
9531 {
9532 Window focus;
9533 int revert;
9534
9535 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9536 if (focus == XtWindow (f->output_data.x->menubar_widget))
9537 from_menu_bar_p = 1;
9538 }
9539#endif /* LESSTIF_VERSION */
6d4238f3 9540
06a2c219
GM
9541 if (event.xcrossing.focus || from_menu_bar_p)
9542 {
9543 /* Avoid nasty pop/raise loops. */
9544 if (f && (!(f->auto_raise)
9545 || !(f->auto_lower)
9546 || (event.xcrossing.time - enter_timestamp) > 500))
9547 {
9548 x_new_focus_frame (dpyinfo, f);
9549 enter_timestamp = event.xcrossing.time;
9550 }
9551 }
9552 else if (f == dpyinfo->x_focus_frame)
9553 x_new_focus_frame (dpyinfo, 0);
9554
9555 /* EnterNotify counts as mouse movement,
9556 so update things that depend on mouse position. */
9557 if (f && !f->output_data.x->busy_p)
9558 note_mouse_movement (f, &event.xmotion);
9559 goto OTHER;
9560 }
dc6f92b8 9561
7a13e894 9562 case FocusIn:
19126e11 9563 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9564 if (event.xfocus.detail != NotifyPointer)
0f941935 9565 dpyinfo->x_focus_event_frame = f;
7a13e894 9566 if (f)
0f941935 9567 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9568
6c183ba5
RS
9569#ifdef HAVE_X_I18N
9570 if (f && FRAME_XIC (f))
9571 XSetICFocus (FRAME_XIC (f));
9572#endif
9573
7a13e894 9574 goto OTHER;
10c5e63d 9575
7a13e894 9576 case LeaveNotify:
19126e11 9577 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9578 if (f)
10c5e63d 9579 {
06a2c219
GM
9580 Lisp_Object frame;
9581 int from_menu_bar_p = 0;
9582
7a13e894 9583 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9584 {
9585 /* If we move outside the frame, then we're
9586 certainly no longer on any text in the frame. */
9587 clear_mouse_face (dpyinfo);
9588 dpyinfo->mouse_face_mouse_frame = 0;
9589 }
9590
9591 /* Generate a nil HELP_EVENT to cancel a help-echo.
9592 Do it only if there's something to cancel.
9593 Otherwise, the startup message is cleared when
9594 the mouse leaves the frame. */
9595 if (any_help_event_p)
9596 {
9597 XSETFRAME (frame, f);
9598 bufp->kind = HELP_EVENT;
9599 bufp->frame_or_window = Fcons (frame, Qnil);
9600 ++bufp, ++count, --numchars;
9601 }
7a13e894 9602
06a2c219
GM
9603#ifdef LESSTIF_VERSION
9604 /* Please see the comment at the start of the
9605 EnterNotify case. */
9606 if (!event.xcrossing.focus
9607 && f->output_data.x->menubar_widget)
9608 {
9609 Window focus;
9610 int revert;
9611 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9612 if (focus == XtWindow (f->output_data.x->menubar_widget))
9613 from_menu_bar_p = 1;
9614 }
9615#endif /* LESSTIF_VERSION */
9616
9617 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9618 x_mouse_leave (dpyinfo);
10c5e63d 9619 else
7a13e894 9620 {
0f941935
KH
9621 if (f == dpyinfo->x_focus_event_frame)
9622 dpyinfo->x_focus_event_frame = 0;
9623 if (f == dpyinfo->x_focus_frame)
9624 x_new_focus_frame (dpyinfo, 0);
7a13e894 9625 }
10c5e63d 9626 }
7a13e894 9627 goto OTHER;
dc6f92b8 9628
7a13e894 9629 case FocusOut:
19126e11 9630 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9631 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9632 && f == dpyinfo->x_focus_event_frame)
9633 dpyinfo->x_focus_event_frame = 0;
9634 if (f && f == dpyinfo->x_focus_frame)
9635 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9636
6c183ba5
RS
9637#ifdef HAVE_X_I18N
9638 if (f && FRAME_XIC (f))
9639 XUnsetICFocus (FRAME_XIC (f));
9640#endif
9641
7a13e894 9642 goto OTHER;
dc6f92b8 9643
7a13e894 9644 case MotionNotify:
dc6f92b8 9645 {
06a2c219
GM
9646 previous_help_echo = help_echo;
9647 help_echo = Qnil;
9648
7a13e894
RS
9649 if (dpyinfo->grabbed && last_mouse_frame
9650 && FRAME_LIVE_P (last_mouse_frame))
9651 f = last_mouse_frame;
9652 else
19126e11 9653 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9654
7a13e894
RS
9655 if (f)
9656 note_mouse_movement (f, &event.xmotion);
9657 else
9658 {
e88b3c50 9659#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9660 struct scroll_bar *bar
9661 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9662
7a13e894
RS
9663 if (bar)
9664 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9665#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9666
06a2c219
GM
9667 /* If we move outside the frame, then we're
9668 certainly no longer on any text in the frame. */
7a13e894
RS
9669 clear_mouse_face (dpyinfo);
9670 }
06a2c219
GM
9671
9672 /* If the contents of the global variable help_echo
9673 has changed, generate a HELP_EVENT. */
9674 if (STRINGP (help_echo)
9675 || STRINGP (previous_help_echo))
9676 {
9677 Lisp_Object frame;
9678
9679 if (f)
9680 XSETFRAME (frame, f);
9681 else
9682 frame = Qnil;
9683
9684 any_help_event_p = 1;
9685 bufp->kind = HELP_EVENT;
9686 bufp->frame_or_window = Fcons (frame, help_echo);
9687 ++bufp, ++count, --numchars;
9688 }
9689
9690 goto OTHER;
dc6f92b8 9691 }
dc6f92b8 9692
7a13e894 9693 case ConfigureNotify:
9829ddba
RS
9694 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9695 if (f)
af395ec1 9696 {
5c187dee 9697#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9698 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9699 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9700
2d7fc7e8
RS
9701 /* In the toolkit version, change_frame_size
9702 is called by the code that handles resizing
9703 of the EmacsFrame widget. */
7a13e894 9704
7a13e894
RS
9705 /* Even if the number of character rows and columns has
9706 not changed, the font size may have changed, so we need
9707 to check the pixel dimensions as well. */
9708 if (columns != f->width
9709 || rows != f->height
7556890b
RS
9710 || event.xconfigure.width != f->output_data.x->pixel_width
9711 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9712 {
7d1e984f 9713 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9714 SET_FRAME_GARBAGED (f);
e687d06e 9715 cancel_mouse_face (f);
7a13e894 9716 }
2d7fc7e8 9717#endif
af395ec1 9718
7556890b
RS
9719 f->output_data.x->pixel_width = event.xconfigure.width;
9720 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9721
9722 /* What we have now is the position of Emacs's own window.
9723 Convert that to the position of the window manager window. */
dcb07ae9
RS
9724 x_real_positions (f, &f->output_data.x->left_pos,
9725 &f->output_data.x->top_pos);
9726
f5d11644
GM
9727#ifdef HAVE_X_I18N
9728 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9729 xic_set_statusarea (f);
9730#endif
9731
dcb07ae9
RS
9732 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9733 {
9734 /* Since the WM decorations come below top_pos now,
9735 we must put them below top_pos in the future. */
9736 f->output_data.x->win_gravity = NorthWestGravity;
9737 x_wm_set_size_hint (f, (long) 0, 0);
9738 }
8f08dc93
KH
9739#ifdef USE_MOTIF
9740 /* Some window managers pass (0,0) as the location of
9741 the window, and the Motif event handler stores it
9742 in the emacs widget, which messes up Motif menus. */
9743 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9744 {
9745 event.xconfigure.x = f->output_data.x->widget->core.x;
9746 event.xconfigure.y = f->output_data.x->widget->core.y;
9747 }
06a2c219 9748#endif /* USE_MOTIF */
7a13e894 9749 }
2d7fc7e8 9750 goto OTHER;
dc6f92b8 9751
7a13e894
RS
9752 case ButtonPress:
9753 case ButtonRelease:
9754 {
9755 /* If we decide we want to generate an event to be seen
9756 by the rest of Emacs, we put it here. */
9757 struct input_event emacs_event;
9ea173e8 9758 int tool_bar_p = 0;
06a2c219 9759
7a13e894 9760 emacs_event.kind = no_event;
7a13e894 9761 bzero (&compose_status, sizeof (compose_status));
9b07615b 9762
06a2c219
GM
9763 if (dpyinfo->grabbed
9764 && last_mouse_frame
9f67f20b
RS
9765 && FRAME_LIVE_P (last_mouse_frame))
9766 f = last_mouse_frame;
9767 else
2224b905 9768 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9769
06a2c219
GM
9770 if (f)
9771 {
9ea173e8
GM
9772 /* Is this in the tool-bar? */
9773 if (WINDOWP (f->tool_bar_window)
9774 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9775 {
9776 Lisp_Object window;
9777 int p, x, y;
9778
9779 x = event.xbutton.x;
9780 y = event.xbutton.y;
9781
9782 /* Set x and y. */
9783 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9784 if (EQ (window, f->tool_bar_window))
06a2c219 9785 {
9ea173e8
GM
9786 x_handle_tool_bar_click (f, &event.xbutton);
9787 tool_bar_p = 1;
06a2c219
GM
9788 }
9789 }
9790
9ea173e8 9791 if (!tool_bar_p)
06a2c219
GM
9792 if (!dpyinfo->x_focus_frame
9793 || f == dpyinfo->x_focus_frame)
9794 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9795 }
9796 else
9797 {
06a2c219 9798#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9799 struct scroll_bar *bar
9800 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9801
7a13e894
RS
9802 if (bar)
9803 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9804#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9805 }
9806
9807 if (event.type == ButtonPress)
9808 {
9809 dpyinfo->grabbed |= (1 << event.xbutton.button);
9810 last_mouse_frame = f;
edad46f6
KH
9811 /* Ignore any mouse motion that happened
9812 before this event; any subsequent mouse-movement
9813 Emacs events should reflect only motion after
9814 the ButtonPress. */
a00e91cd
KH
9815 if (f != 0)
9816 f->mouse_moved = 0;
06a2c219 9817
9ea173e8
GM
9818 if (!tool_bar_p)
9819 last_tool_bar_item = -1;
06a2c219
GM
9820 if (display_busy_cursor_p)
9821 inhibit_busy_cursor = 2;
7a13e894 9822 }
3afe33e7
RS
9823 else
9824 {
7a13e894 9825 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9826 }
23faf38f 9827
7a13e894
RS
9828 if (numchars >= 1 && emacs_event.kind != no_event)
9829 {
9830 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9831 bufp++;
9832 count++;
9833 numchars--;
9834 }
3afe33e7
RS
9835
9836#ifdef USE_X_TOOLKIT
2224b905
RS
9837 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9838 /* For a down-event in the menu bar,
9839 don't pass it to Xt right now.
9840 Instead, save it away
9841 and we will pass it to Xt from kbd_buffer_get_event.
9842 That way, we can run some Lisp code first. */
91375f8f
RS
9843 if (f && event.type == ButtonPress
9844 /* Verify the event is really within the menu bar
9845 and not just sent to it due to grabbing. */
9846 && event.xbutton.x >= 0
9847 && event.xbutton.x < f->output_data.x->pixel_width
9848 && event.xbutton.y >= 0
9849 && event.xbutton.y < f->output_data.x->menubar_height
9850 && event.xbutton.same_screen)
2224b905 9851 {
8805890a 9852 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9853 XSETFRAME (last_mouse_press_frame, f);
9854 }
9855 else if (event.type == ButtonPress)
9856 {
9857 last_mouse_press_frame = Qnil;
30e671c3 9858 goto OTHER;
ce89ef46 9859 }
06a2c219 9860
2237cac9
RS
9861#ifdef USE_MOTIF /* This should do not harm for Lucid,
9862 but I am trying to be cautious. */
ce89ef46
RS
9863 else if (event.type == ButtonRelease)
9864 {
2237cac9 9865 if (!NILP (last_mouse_press_frame))
f10ded1c 9866 {
2237cac9
RS
9867 f = XFRAME (last_mouse_press_frame);
9868 if (f->output_data.x)
06a2c219 9869 SET_SAVED_BUTTON_EVENT;
f10ded1c 9870 }
06a2c219 9871 else
30e671c3 9872 goto OTHER;
2224b905 9873 }
2237cac9 9874#endif /* USE_MOTIF */
2224b905
RS
9875 else
9876 goto OTHER;
3afe33e7 9877#endif /* USE_X_TOOLKIT */
7a13e894
RS
9878 }
9879 break;
dc6f92b8 9880
7a13e894 9881 case CirculateNotify:
06a2c219
GM
9882 goto OTHER;
9883
7a13e894 9884 case CirculateRequest:
06a2c219
GM
9885 goto OTHER;
9886
9887 case VisibilityNotify:
9888 goto OTHER;
dc6f92b8 9889
7a13e894
RS
9890 case MappingNotify:
9891 /* Someone has changed the keyboard mapping - update the
9892 local cache. */
9893 switch (event.xmapping.request)
9894 {
9895 case MappingModifier:
9896 x_find_modifier_meanings (dpyinfo);
9897 /* This is meant to fall through. */
9898 case MappingKeyboard:
9899 XRefreshKeyboardMapping (&event.xmapping);
9900 }
7a13e894 9901 goto OTHER;
dc6f92b8 9902
7a13e894 9903 default:
7a13e894 9904 OTHER:
717ca130 9905#ifdef USE_X_TOOLKIT
7a13e894
RS
9906 BLOCK_INPUT;
9907 XtDispatchEvent (&event);
9908 UNBLOCK_INPUT;
3afe33e7 9909#endif /* USE_X_TOOLKIT */
7a13e894
RS
9910 break;
9911 }
dc6f92b8
JB
9912 }
9913 }
9914
06a2c219
GM
9915 out:;
9916
9a5196d0
RS
9917 /* On some systems, an X bug causes Emacs to get no more events
9918 when the window is destroyed. Detect that. (1994.) */
58769bee 9919 if (! event_found)
ef2a22d0 9920 {
ef2a22d0
RS
9921 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9922 One XNOOP in 100 loops will make Emacs terminate.
9923 B. Bretthauer, 1994 */
9924 x_noop_count++;
58769bee 9925 if (x_noop_count >= 100)
ef2a22d0
RS
9926 {
9927 x_noop_count=0;
2224b905
RS
9928
9929 if (next_noop_dpyinfo == 0)
9930 next_noop_dpyinfo = x_display_list;
9931
9932 XNoOp (next_noop_dpyinfo->display);
9933
9934 /* Each time we get here, cycle through the displays now open. */
9935 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9936 }
9937 }
502add23 9938
06a2c219 9939 /* If the focus was just given to an auto-raising frame,
0134a210 9940 raise it now. */
7a13e894 9941 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9942 if (pending_autoraise_frame)
9943 {
9944 x_raise_frame (pending_autoraise_frame);
9945 pending_autoraise_frame = 0;
9946 }
0134a210 9947
dc6f92b8
JB
9948 UNBLOCK_INPUT;
9949 return count;
9950}
06a2c219
GM
9951
9952
9953
dc6f92b8 9954\f
06a2c219
GM
9955/***********************************************************************
9956 Text Cursor
9957 ***********************************************************************/
9958
9959/* Note if the text cursor of window W has been overwritten by a
9960 drawing operation that outputs N glyphs starting at HPOS in the
9961 line given by output_cursor.vpos. N < 0 means all the rest of the
9962 line after HPOS has been written. */
9963
9964static void
9965note_overwritten_text_cursor (w, hpos, n)
9966 struct window *w;
9967 int hpos, n;
9968{
9969 if (updated_area == TEXT_AREA
9970 && output_cursor.vpos == w->phys_cursor.vpos
9971 && hpos <= w->phys_cursor.hpos
9972 && (n < 0
9973 || hpos + n > w->phys_cursor.hpos))
9974 w->phys_cursor_on_p = 0;
9975}
f451eb13
JB
9976
9977
06a2c219
GM
9978/* Set clipping for output in glyph row ROW. W is the window in which
9979 we operate. GC is the graphics context to set clipping in.
9980 WHOLE_LINE_P non-zero means include the areas used for truncation
9981 mark display and alike in the clipping rectangle.
9982
9983 ROW may be a text row or, e.g., a mode line. Text rows must be
9984 clipped to the interior of the window dedicated to text display,
9985 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9986
9987static void
06a2c219
GM
9988x_clip_to_row (w, row, gc, whole_line_p)
9989 struct window *w;
9990 struct glyph_row *row;
9991 GC gc;
9992 int whole_line_p;
dc6f92b8 9993{
06a2c219
GM
9994 struct frame *f = XFRAME (WINDOW_FRAME (w));
9995 XRectangle clip_rect;
9996 int window_x, window_y, window_width, window_height;
dc6f92b8 9997
06a2c219 9998 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9999
06a2c219
GM
10000 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10001 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10002 clip_rect.y = max (clip_rect.y, window_y);
10003 clip_rect.width = window_width;
10004 clip_rect.height = row->visible_height;
5c1aae96 10005
06a2c219
GM
10006 /* If clipping to the whole line, including trunc marks, extend
10007 the rectangle to the left and increase its width. */
10008 if (whole_line_p)
10009 {
110859fc
GM
10010 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10011 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10012 }
5c1aae96 10013
06a2c219 10014 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10015}
10016
06a2c219
GM
10017
10018/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10019
10020static void
06a2c219
GM
10021x_draw_hollow_cursor (w, row)
10022 struct window *w;
10023 struct glyph_row *row;
dc6f92b8 10024{
06a2c219
GM
10025 struct frame *f = XFRAME (WINDOW_FRAME (w));
10026 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10027 Display *dpy = FRAME_X_DISPLAY (f);
10028 int x, y, wd, h;
10029 XGCValues xgcv;
10030 struct glyph *cursor_glyph;
10031 GC gc;
10032
10033 /* Compute frame-relative coordinates from window-relative
10034 coordinates. */
10035 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10036 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10037 + row->ascent - w->phys_cursor_ascent);
10038 h = row->height - 1;
10039
10040 /* Get the glyph the cursor is on. If we can't tell because
10041 the current matrix is invalid or such, give up. */
10042 cursor_glyph = get_phys_cursor_glyph (w);
10043 if (cursor_glyph == NULL)
dc6f92b8
JB
10044 return;
10045
06a2c219
GM
10046 /* Compute the width of the rectangle to draw. If on a stretch
10047 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10048 rectangle as wide as the glyph, but use a canonical character
10049 width instead. */
10050 wd = cursor_glyph->pixel_width - 1;
10051 if (cursor_glyph->type == STRETCH_GLYPH
10052 && !x_stretch_cursor_p)
10053 wd = min (CANON_X_UNIT (f), wd);
10054
10055 /* The foreground of cursor_gc is typically the same as the normal
10056 background color, which can cause the cursor box to be invisible. */
10057 xgcv.foreground = f->output_data.x->cursor_pixel;
10058 if (dpyinfo->scratch_cursor_gc)
10059 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10060 else
10061 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10062 GCForeground, &xgcv);
10063 gc = dpyinfo->scratch_cursor_gc;
10064
10065 /* Set clipping, draw the rectangle, and reset clipping again. */
10066 x_clip_to_row (w, row, gc, 0);
10067 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10068 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10069}
10070
06a2c219
GM
10071
10072/* Draw a bar cursor on window W in glyph row ROW.
10073
10074 Implementation note: One would like to draw a bar cursor with an
10075 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10076 Unfortunately, I didn't find a font yet that has this property set.
10077 --gerd. */
dc6f92b8
JB
10078
10079static void
06a2c219
GM
10080x_draw_bar_cursor (w, row)
10081 struct window *w;
10082 struct glyph_row *row;
dc6f92b8 10083{
06a2c219
GM
10084 /* If cursor hpos is out of bounds, don't draw garbage. This can
10085 happen in mini-buffer windows when switching between echo area
10086 glyphs and mini-buffer. */
10087 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10088 {
10089 struct frame *f = XFRAME (w->frame);
10090 struct glyph *cursor_glyph;
10091 GC gc;
10092 int x;
10093 unsigned long mask;
10094 XGCValues xgcv;
10095 Display *dpy;
10096 Window window;
10097
10098 cursor_glyph = get_phys_cursor_glyph (w);
10099 if (cursor_glyph == NULL)
10100 return;
10101
10102 xgcv.background = f->output_data.x->cursor_pixel;
10103 xgcv.foreground = f->output_data.x->cursor_pixel;
10104 xgcv.graphics_exposures = 0;
10105 mask = GCForeground | GCBackground | GCGraphicsExposures;
10106 dpy = FRAME_X_DISPLAY (f);
10107 window = FRAME_X_WINDOW (f);
10108 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10109
10110 if (gc)
10111 XChangeGC (dpy, gc, mask, &xgcv);
10112 else
10113 {
10114 gc = XCreateGC (dpy, window, mask, &xgcv);
10115 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10116 }
10117
10118 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10119 x_clip_to_row (w, row, gc, 0);
10120 XFillRectangle (dpy, window, gc,
10121 x,
10122 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10123 min (cursor_glyph->pixel_width,
10124 f->output_data.x->cursor_width),
10125 row->height);
10126 XSetClipMask (dpy, gc, None);
10127 }
dc6f92b8
JB
10128}
10129
06a2c219
GM
10130
10131/* Clear the cursor of window W to background color, and mark the
10132 cursor as not shown. This is used when the text where the cursor
10133 is is about to be rewritten. */
10134
dc6f92b8 10135static void
06a2c219
GM
10136x_clear_cursor (w)
10137 struct window *w;
dc6f92b8 10138{
06a2c219
GM
10139 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10140 x_update_window_cursor (w, 0);
10141}
90e65f07 10142
dbc4e1c1 10143
06a2c219
GM
10144/* Draw the cursor glyph of window W in glyph row ROW. See the
10145 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10146
06a2c219
GM
10147static void
10148x_draw_phys_cursor_glyph (w, row, hl)
10149 struct window *w;
10150 struct glyph_row *row;
10151 enum draw_glyphs_face hl;
10152{
10153 /* If cursor hpos is out of bounds, don't draw garbage. This can
10154 happen in mini-buffer windows when switching between echo area
10155 glyphs and mini-buffer. */
10156 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10157 {
10158 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10159 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10160 hl, 0, 0, 0);
10161
10162 /* When we erase the cursor, and ROW is overlapped by other
10163 rows, make sure that these overlapping parts of other rows
10164 are redrawn. */
10165 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10166 {
10167 if (row > w->current_matrix->rows
10168 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10169 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10170
10171 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10172 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10173 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10174 }
10175 }
06a2c219 10176}
dbc4e1c1 10177
eea6af04 10178
06a2c219 10179/* Erase the image of a cursor of window W from the screen. */
eea6af04 10180
06a2c219
GM
10181static void
10182x_erase_phys_cursor (w)
10183 struct window *w;
10184{
10185 struct frame *f = XFRAME (w->frame);
10186 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10187 int hpos = w->phys_cursor.hpos;
10188 int vpos = w->phys_cursor.vpos;
10189 int mouse_face_here_p = 0;
10190 struct glyph_matrix *active_glyphs = w->current_matrix;
10191 struct glyph_row *cursor_row;
10192 struct glyph *cursor_glyph;
10193 enum draw_glyphs_face hl;
10194
10195 /* No cursor displayed or row invalidated => nothing to do on the
10196 screen. */
10197 if (w->phys_cursor_type == NO_CURSOR)
10198 goto mark_cursor_off;
10199
10200 /* VPOS >= active_glyphs->nrows means that window has been resized.
10201 Don't bother to erase the cursor. */
10202 if (vpos >= active_glyphs->nrows)
10203 goto mark_cursor_off;
10204
10205 /* If row containing cursor is marked invalid, there is nothing we
10206 can do. */
10207 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10208 if (!cursor_row->enabled_p)
10209 goto mark_cursor_off;
10210
10211 /* This can happen when the new row is shorter than the old one.
10212 In this case, either x_draw_glyphs or clear_end_of_line
10213 should have cleared the cursor. Note that we wouldn't be
10214 able to erase the cursor in this case because we don't have a
10215 cursor glyph at hand. */
10216 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10217 goto mark_cursor_off;
10218
10219 /* If the cursor is in the mouse face area, redisplay that when
10220 we clear the cursor. */
10221 if (w == XWINDOW (dpyinfo->mouse_face_window)
10222 && (vpos > dpyinfo->mouse_face_beg_row
10223 || (vpos == dpyinfo->mouse_face_beg_row
10224 && hpos >= dpyinfo->mouse_face_beg_col))
10225 && (vpos < dpyinfo->mouse_face_end_row
10226 || (vpos == dpyinfo->mouse_face_end_row
10227 && hpos < dpyinfo->mouse_face_end_col))
10228 /* Don't redraw the cursor's spot in mouse face if it is at the
10229 end of a line (on a newline). The cursor appears there, but
10230 mouse highlighting does not. */
10231 && cursor_row->used[TEXT_AREA] > hpos)
10232 mouse_face_here_p = 1;
10233
10234 /* Maybe clear the display under the cursor. */
10235 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10236 {
10237 int x;
045dee35 10238 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10239
06a2c219
GM
10240 cursor_glyph = get_phys_cursor_glyph (w);
10241 if (cursor_glyph == NULL)
10242 goto mark_cursor_off;
dbc4e1c1 10243
06a2c219
GM
10244 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10245
10246 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10247 x,
045dee35 10248 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10249 cursor_row->y)),
10250 cursor_glyph->pixel_width,
10251 cursor_row->visible_height,
10252 False);
dbc4e1c1 10253 }
06a2c219
GM
10254
10255 /* Erase the cursor by redrawing the character underneath it. */
10256 if (mouse_face_here_p)
10257 hl = DRAW_MOUSE_FACE;
10258 else if (cursor_row->inverse_p)
10259 hl = DRAW_INVERSE_VIDEO;
10260 else
10261 hl = DRAW_NORMAL_TEXT;
10262 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10263
06a2c219
GM
10264 mark_cursor_off:
10265 w->phys_cursor_on_p = 0;
10266 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10267}
10268
10269
06a2c219
GM
10270/* Display or clear cursor of window W. If ON is zero, clear the
10271 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10272 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10273
06a2c219
GM
10274void
10275x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10276 struct window *w;
10277 int on, hpos, vpos, x, y;
dbc4e1c1 10278{
06a2c219
GM
10279 struct frame *f = XFRAME (w->frame);
10280 int new_cursor_type;
10281 struct glyph_matrix *current_glyphs;
10282 struct glyph_row *glyph_row;
10283 struct glyph *glyph;
dbc4e1c1 10284
49d838ea 10285 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10286 windows and frames; in the latter case, the frame or window may
10287 be in the midst of changing its size, and x and y may be off the
10288 window. */
10289 if (! FRAME_VISIBLE_P (f)
10290 || FRAME_GARBAGED_P (f)
10291 || vpos >= w->current_matrix->nrows
10292 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10293 return;
10294
10295 /* If cursor is off and we want it off, return quickly. */
06a2c219 10296 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10297 return;
10298
06a2c219
GM
10299 current_glyphs = w->current_matrix;
10300 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10301 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10302
10303 /* If cursor row is not enabled, we don't really know where to
10304 display the cursor. */
10305 if (!glyph_row->enabled_p)
10306 {
10307 w->phys_cursor_on_p = 0;
10308 return;
10309 }
10310
10311 xassert (interrupt_input_blocked);
10312
10313 /* Set new_cursor_type to the cursor we want to be displayed. In a
10314 mini-buffer window, we want the cursor only to appear if we are
10315 reading input from this window. For the selected window, we want
10316 the cursor type given by the frame parameter. If explicitly
10317 marked off, draw no cursor. In all other cases, we want a hollow
10318 box cursor. */
9b4a7047
GM
10319 if (cursor_in_echo_area
10320 && FRAME_HAS_MINIBUF_P (f)
10321 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10322 {
9b4a7047
GM
10323 if (w == XWINDOW (echo_area_window))
10324 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10325 else
10326 new_cursor_type = HOLLOW_BOX_CURSOR;
10327 }
06a2c219 10328 else
9b4a7047
GM
10329 {
10330 if (w != XWINDOW (selected_window)
10331 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10332 {
10333 if (MINI_WINDOW_P (w))
10334 new_cursor_type = NO_CURSOR;
10335 else
10336 new_cursor_type = HOLLOW_BOX_CURSOR;
10337 }
10338 else if (w->cursor_off_p)
10339 new_cursor_type = NO_CURSOR;
10340 else
10341 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10342 }
06a2c219
GM
10343
10344 /* If cursor is currently being shown and we don't want it to be or
10345 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10346 erase it. */
06a2c219 10347 if (w->phys_cursor_on_p
dc6f92b8 10348 && (!on
06a2c219
GM
10349 || w->phys_cursor.x != x
10350 || w->phys_cursor.y != y
10351 || new_cursor_type != w->phys_cursor_type))
10352 x_erase_phys_cursor (w);
10353
10354 /* If the cursor is now invisible and we want it to be visible,
10355 display it. */
10356 if (on && !w->phys_cursor_on_p)
10357 {
10358 w->phys_cursor_ascent = glyph_row->ascent;
10359 w->phys_cursor_height = glyph_row->height;
10360
10361 /* Set phys_cursor_.* before x_draw_.* is called because some
10362 of them may need the information. */
10363 w->phys_cursor.x = x;
10364 w->phys_cursor.y = glyph_row->y;
10365 w->phys_cursor.hpos = hpos;
10366 w->phys_cursor.vpos = vpos;
10367 w->phys_cursor_type = new_cursor_type;
10368 w->phys_cursor_on_p = 1;
10369
10370 switch (new_cursor_type)
dc6f92b8 10371 {
06a2c219
GM
10372 case HOLLOW_BOX_CURSOR:
10373 x_draw_hollow_cursor (w, glyph_row);
10374 break;
10375
10376 case FILLED_BOX_CURSOR:
10377 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10378 break;
10379
10380 case BAR_CURSOR:
10381 x_draw_bar_cursor (w, glyph_row);
10382 break;
10383
10384 case NO_CURSOR:
10385 break;
dc6f92b8 10386
06a2c219
GM
10387 default:
10388 abort ();
10389 }
dc6f92b8
JB
10390 }
10391
06a2c219 10392#ifndef XFlush
f676886a 10393 if (updating_frame != f)
334208b7 10394 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10395#endif
dc6f92b8
JB
10396}
10397
06a2c219
GM
10398
10399/* Display the cursor on window W, or clear it. X and Y are window
10400 relative pixel coordinates. HPOS and VPOS are glyph matrix
10401 positions. If W is not the selected window, display a hollow
10402 cursor. ON non-zero means display the cursor at X, Y which
10403 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10404
dfcf069d 10405void
06a2c219
GM
10406x_display_cursor (w, on, hpos, vpos, x, y)
10407 struct window *w;
10408 int on, hpos, vpos, x, y;
dc6f92b8 10409{
f94397b5 10410 BLOCK_INPUT;
06a2c219 10411 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
f5d11644
GM
10412
10413#ifdef HAVE_X_I18N
10414 {
10415 struct frame *f = XFRAME (w->frame);
10416
10417 if (w == XWINDOW (f->selected_window))
10418 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10419 xic_set_preeditarea (w, x, y);
10420 }
10421#endif
10422
5d46f928
RS
10423 UNBLOCK_INPUT;
10424}
10425
06a2c219
GM
10426
10427/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10428 Don't change the cursor's position. */
10429
dfcf069d 10430void
06a2c219 10431x_update_cursor (f, on_p)
5d46f928 10432 struct frame *f;
5d46f928 10433{
06a2c219
GM
10434 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10435}
10436
10437
10438/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10439 in the window tree rooted at W. */
10440
10441static void
10442x_update_cursor_in_window_tree (w, on_p)
10443 struct window *w;
10444 int on_p;
10445{
10446 while (w)
10447 {
10448 if (!NILP (w->hchild))
10449 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10450 else if (!NILP (w->vchild))
10451 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10452 else
10453 x_update_window_cursor (w, on_p);
10454
10455 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10456 }
10457}
5d46f928 10458
f94397b5 10459
06a2c219
GM
10460/* Switch the display of W's cursor on or off, according to the value
10461 of ON. */
10462
10463static void
10464x_update_window_cursor (w, on)
10465 struct window *w;
10466 int on;
10467{
10468 BLOCK_INPUT;
10469 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10470 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10471 UNBLOCK_INPUT;
dc6f92b8 10472}
06a2c219
GM
10473
10474
10475
dc6f92b8
JB
10476\f
10477/* Icons. */
10478
f676886a 10479/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10480 when we get an expose event for it. */
dc6f92b8 10481
dfcf069d 10482void
f676886a
JB
10483refreshicon (f)
10484 struct frame *f;
dc6f92b8 10485{
06a2c219 10486 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10487}
10488
dbc4e1c1 10489/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10490
10491int
990ba854 10492x_bitmap_icon (f, file)
f676886a 10493 struct frame *f;
990ba854 10494 Lisp_Object file;
dc6f92b8 10495{
06a2c219 10496 int bitmap_id;
dc6f92b8 10497
c118dd06 10498 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10499 return 1;
10500
990ba854 10501 /* Free up our existing icon bitmap if any. */
7556890b
RS
10502 if (f->output_data.x->icon_bitmap > 0)
10503 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10504 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10505
10506 if (STRINGP (file))
7f2ae036
RS
10507 bitmap_id = x_create_bitmap_from_file (f, file);
10508 else
10509 {
990ba854 10510 /* Create the GNU bitmap if necessary. */
5bf01b68 10511 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10512 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10513 = x_create_bitmap_from_data (f, gnu_bits,
10514 gnu_width, gnu_height);
990ba854
RS
10515
10516 /* The first time we create the GNU bitmap,
06a2c219 10517 this increments the ref-count one extra time.
990ba854
RS
10518 As a result, the GNU bitmap is never freed.
10519 That way, we don't have to worry about allocating it again. */
334208b7 10520 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10521
334208b7 10522 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10523 }
10524
10525 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10526 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10527
10528 return 0;
10529}
10530
10531
1be2d067
KH
10532/* Make the x-window of frame F use a rectangle with text.
10533 Use ICON_NAME as the text. */
dc6f92b8
JB
10534
10535int
f676886a
JB
10536x_text_icon (f, icon_name)
10537 struct frame *f;
dc6f92b8
JB
10538 char *icon_name;
10539{
c118dd06 10540 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10541 return 1;
10542
1be2d067
KH
10543#ifdef HAVE_X11R4
10544 {
10545 XTextProperty text;
10546 text.value = (unsigned char *) icon_name;
10547 text.encoding = XA_STRING;
10548 text.format = 8;
10549 text.nitems = strlen (icon_name);
10550#ifdef USE_X_TOOLKIT
7556890b 10551 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10552 &text);
10553#else /* not USE_X_TOOLKIT */
10554 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10555#endif /* not USE_X_TOOLKIT */
10556 }
10557#else /* not HAVE_X11R4 */
10558 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10559#endif /* not HAVE_X11R4 */
58769bee 10560
7556890b
RS
10561 if (f->output_data.x->icon_bitmap > 0)
10562 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10563 f->output_data.x->icon_bitmap = 0;
b1c884c3 10564 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10565
10566 return 0;
10567}
10568\f
e99db5a1
RS
10569#define X_ERROR_MESSAGE_SIZE 200
10570
10571/* If non-nil, this should be a string.
10572 It means catch X errors and store the error message in this string. */
10573
10574static Lisp_Object x_error_message_string;
10575
10576/* An X error handler which stores the error message in
10577 x_error_message_string. This is called from x_error_handler if
10578 x_catch_errors is in effect. */
10579
06a2c219 10580static void
e99db5a1
RS
10581x_error_catcher (display, error)
10582 Display *display;
10583 XErrorEvent *error;
10584{
10585 XGetErrorText (display, error->error_code,
10586 XSTRING (x_error_message_string)->data,
10587 X_ERROR_MESSAGE_SIZE);
10588}
10589
10590/* Begin trapping X errors for display DPY. Actually we trap X errors
10591 for all displays, but DPY should be the display you are actually
10592 operating on.
10593
10594 After calling this function, X protocol errors no longer cause
10595 Emacs to exit; instead, they are recorded in the string
10596 stored in x_error_message_string.
10597
10598 Calling x_check_errors signals an Emacs error if an X error has
10599 occurred since the last call to x_catch_errors or x_check_errors.
10600
10601 Calling x_uncatch_errors resumes the normal error handling. */
10602
10603void x_check_errors ();
10604static Lisp_Object x_catch_errors_unwind ();
10605
10606int
10607x_catch_errors (dpy)
10608 Display *dpy;
10609{
10610 int count = specpdl_ptr - specpdl;
10611
10612 /* Make sure any errors from previous requests have been dealt with. */
10613 XSync (dpy, False);
10614
10615 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10616
10617 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10618 XSTRING (x_error_message_string)->data[0] = 0;
10619
10620 return count;
10621}
10622
10623/* Unbind the binding that we made to check for X errors. */
10624
10625static Lisp_Object
10626x_catch_errors_unwind (old_val)
10627 Lisp_Object old_val;
10628{
10629 x_error_message_string = old_val;
10630 return Qnil;
10631}
10632
10633/* If any X protocol errors have arrived since the last call to
10634 x_catch_errors or x_check_errors, signal an Emacs error using
10635 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10636
10637void
10638x_check_errors (dpy, format)
10639 Display *dpy;
10640 char *format;
10641{
10642 /* Make sure to catch any errors incurred so far. */
10643 XSync (dpy, False);
10644
10645 if (XSTRING (x_error_message_string)->data[0])
10646 error (format, XSTRING (x_error_message_string)->data);
10647}
10648
9829ddba
RS
10649/* Nonzero if we had any X protocol errors
10650 since we did x_catch_errors on DPY. */
e99db5a1
RS
10651
10652int
10653x_had_errors_p (dpy)
10654 Display *dpy;
10655{
10656 /* Make sure to catch any errors incurred so far. */
10657 XSync (dpy, False);
10658
10659 return XSTRING (x_error_message_string)->data[0] != 0;
10660}
10661
9829ddba
RS
10662/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10663
06a2c219 10664void
9829ddba
RS
10665x_clear_errors (dpy)
10666 Display *dpy;
10667{
10668 XSTRING (x_error_message_string)->data[0] = 0;
10669}
10670
e99db5a1
RS
10671/* Stop catching X protocol errors and let them make Emacs die.
10672 DPY should be the display that was passed to x_catch_errors.
10673 COUNT should be the value that was returned by
10674 the corresponding call to x_catch_errors. */
10675
10676void
10677x_uncatch_errors (dpy, count)
10678 Display *dpy;
10679 int count;
10680{
10681 unbind_to (count, Qnil);
10682}
10683
10684#if 0
10685static unsigned int x_wire_count;
10686x_trace_wire ()
10687{
10688 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10689}
10690#endif /* ! 0 */
10691
10692\f
10693/* Handle SIGPIPE, which can happen when the connection to a server
10694 simply goes away. SIGPIPE is handled by x_connection_signal.
10695 Don't need to do anything, because the write which caused the
10696 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10697 which will do the appropriate cleanup for us. */
e99db5a1
RS
10698
10699static SIGTYPE
10700x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10701 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10702{
10703#ifdef USG
10704 /* USG systems forget handlers when they are used;
10705 must reestablish each time */
10706 signal (signalnum, x_connection_signal);
10707#endif /* USG */
10708}
10709\f
4746118a
JB
10710/* Handling X errors. */
10711
7a13e894 10712/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10713
4746118a 10714static SIGTYPE
7a13e894
RS
10715x_connection_closed (display, error_message)
10716 Display *display;
10717 char *error_message;
4746118a 10718{
7a13e894
RS
10719 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10720 Lisp_Object frame, tail;
10721
6186a4a0
RS
10722 /* Indicate that this display is dead. */
10723
f613a4c8 10724#ifdef USE_X_TOOLKIT
adabc3a9 10725 XtCloseDisplay (display);
f613a4c8 10726#endif
adabc3a9 10727
6186a4a0
RS
10728 dpyinfo->display = 0;
10729
06a2c219 10730 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10731 that are on the dead display. */
10732 FOR_EACH_FRAME (tail, frame)
10733 {
10734 Lisp_Object minibuf_frame;
10735 minibuf_frame
10736 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10737 if (FRAME_X_P (XFRAME (frame))
10738 && FRAME_X_P (XFRAME (minibuf_frame))
10739 && ! EQ (frame, minibuf_frame)
10740 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10741 Fdelete_frame (frame, Qt);
10742 }
10743
10744 /* Now delete all remaining frames on the dead display.
06a2c219 10745 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10746 for another frame that we need to delete. */
10747 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10748 if (FRAME_X_P (XFRAME (frame))
10749 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10750 {
10751 /* Set this to t so that Fdelete_frame won't get confused
10752 trying to find a replacement. */
10753 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10754 Fdelete_frame (frame, Qt);
10755 }
7a13e894 10756
482a1bd2
KH
10757 if (dpyinfo)
10758 x_delete_display (dpyinfo);
7a13e894
RS
10759
10760 if (x_display_list == 0)
10761 {
f8d07b62 10762 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10763 shut_down_emacs (0, 0, Qnil);
10764 exit (70);
10765 }
12ba150f 10766
7a13e894
RS
10767 /* Ordinary stack unwind doesn't deal with these. */
10768#ifdef SIGIO
10769 sigunblock (sigmask (SIGIO));
10770#endif
10771 sigunblock (sigmask (SIGALRM));
10772 TOTALLY_UNBLOCK_INPUT;
10773
aa4d9a9e 10774 clear_waiting_for_input ();
7a13e894 10775 error ("%s", error_message);
4746118a
JB
10776}
10777
7a13e894
RS
10778/* This is the usual handler for X protocol errors.
10779 It kills all frames on the display that we got the error for.
10780 If that was the only one, it prints an error message and kills Emacs. */
10781
06a2c219 10782static void
c118dd06
JB
10783x_error_quitter (display, error)
10784 Display *display;
10785 XErrorEvent *error;
10786{
7a13e894 10787 char buf[256], buf1[356];
dc6f92b8 10788
58769bee 10789 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10790 original error handler. */
dc6f92b8 10791
c118dd06 10792 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10793 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10794 buf, error->request_code);
7a13e894 10795 x_connection_closed (display, buf1);
dc6f92b8
JB
10796}
10797
e99db5a1
RS
10798/* This is the first-level handler for X protocol errors.
10799 It calls x_error_quitter or x_error_catcher. */
7a13e894 10800
8922af5f 10801static int
e99db5a1 10802x_error_handler (display, error)
8922af5f 10803 Display *display;
e99db5a1 10804 XErrorEvent *error;
8922af5f 10805{
e99db5a1
RS
10806 if (! NILP (x_error_message_string))
10807 x_error_catcher (display, error);
10808 else
10809 x_error_quitter (display, error);
06a2c219 10810 return 0;
f9e24cb9 10811}
c118dd06 10812
e99db5a1
RS
10813/* This is the handler for X IO errors, always.
10814 It kills all frames on the display that we lost touch with.
10815 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10816
c118dd06 10817static int
e99db5a1 10818x_io_error_quitter (display)
c118dd06 10819 Display *display;
c118dd06 10820{
e99db5a1 10821 char buf[256];
dc6f92b8 10822
e99db5a1
RS
10823 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10824 x_connection_closed (display, buf);
06a2c219 10825 return 0;
dc6f92b8 10826}
dc6f92b8 10827\f
f451eb13
JB
10828/* Changing the font of the frame. */
10829
76bcdf39
RS
10830/* Give frame F the font named FONTNAME as its default font, and
10831 return the full name of that font. FONTNAME may be a wildcard
10832 pattern; in that case, we choose some font that fits the pattern.
10833 The return value shows which font we chose. */
10834
b5cf7a0e 10835Lisp_Object
f676886a
JB
10836x_new_font (f, fontname)
10837 struct frame *f;
dc6f92b8
JB
10838 register char *fontname;
10839{
dc43ef94
KH
10840 struct font_info *fontp
10841 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10842
dc43ef94
KH
10843 if (!fontp)
10844 return Qnil;
2224a5fc 10845
dc43ef94 10846 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10847 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10848 f->output_data.x->fontset = -1;
10849
b2cad826
KH
10850 /* Compute the scroll bar width in character columns. */
10851 if (f->scroll_bar_pixel_width > 0)
10852 {
7556890b 10853 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10854 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10855 }
10856 else
4e61bddf
RS
10857 {
10858 int wid = FONT_WIDTH (f->output_data.x->font);
10859 f->scroll_bar_cols = (14 + wid - 1) / wid;
10860 }
b2cad826 10861
f676886a 10862 /* Now make the frame display the given font. */
c118dd06 10863 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10864 {
7556890b
RS
10865 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10866 f->output_data.x->font->fid);
10867 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10868 f->output_data.x->font->fid);
10869 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10870 f->output_data.x->font->fid);
f676886a 10871
a27f9f86 10872 frame_update_line_height (f);
0134a210 10873 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10874 }
a27f9f86
RS
10875 else
10876 /* If we are setting a new frame's font for the first time,
10877 there are no faces yet, so this font's height is the line height. */
7556890b 10878 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10879
dc43ef94
KH
10880 return build_string (fontp->full_name);
10881}
10882
10883/* Give frame F the fontset named FONTSETNAME as its default font, and
10884 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10885 pattern; in that case, we choose some fontset that fits the pattern.
10886 The return value shows which fontset we chose. */
b5cf7a0e 10887
dc43ef94
KH
10888Lisp_Object
10889x_new_fontset (f, fontsetname)
10890 struct frame *f;
10891 char *fontsetname;
10892{
10893 int fontset = fs_query_fontset (f, fontsetname);
10894 struct fontset_info *fontsetp;
10895 Lisp_Object result;
b5cf7a0e 10896
dc43ef94
KH
10897 if (fontset < 0)
10898 return Qnil;
b5cf7a0e 10899
2da424f1
KH
10900 if (f->output_data.x->fontset == fontset)
10901 /* This fontset is already set in frame F. There's nothing more
10902 to do. */
10903 return build_string (fontsetname);
10904
dc43ef94
KH
10905 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10906
10907 if (!fontsetp->fontname[CHARSET_ASCII])
10908 /* This fontset doesn't contain ASCII font. */
10909 return Qnil;
10910
10911 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10912
10913 if (!STRINGP (result))
10914 /* Can't load ASCII font. */
10915 return Qnil;
10916
10917 /* Since x_new_font doesn't update any fontset information, do it now. */
10918 f->output_data.x->fontset = fontset;
2da424f1 10919 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10920 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94 10921
f5d11644
GM
10922#ifdef HAVE_X_I18N
10923 if (FRAME_XIC (f)
10924 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
10925 xic_set_xfontset (f, fontsetp->fontname[CHARSET_ASCII]);
10926#endif
10927
dc43ef94 10928 return build_string (fontsetname);
dc6f92b8 10929}
f5d11644
GM
10930
10931\f
10932/***********************************************************************
10933 X Input Methods
10934 ***********************************************************************/
10935
10936#ifdef HAVE_X_I18N
10937
10938#ifdef HAVE_X11R6
10939
10940/* XIM destroy callback function, which is called whenever the
10941 connection to input method XIM dies. CLIENT_DATA contains a
10942 pointer to the x_display_info structure corresponding to XIM. */
10943
10944static void
10945xim_destroy_callback (xim, client_data, call_data)
10946 XIM xim;
10947 XPointer client_data;
10948 XPointer call_data;
10949{
10950 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10951 Lisp_Object frame, tail;
10952
10953 BLOCK_INPUT;
10954
10955 /* No need to call XDestroyIC.. */
10956 FOR_EACH_FRAME (tail, frame)
10957 {
10958 struct frame *f = XFRAME (frame);
10959 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
10960 {
10961 FRAME_XIC (f) = NULL;
10962 if (FRAME_XIC_FONTSET (f))
10963 {
10964 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
10965 FRAME_XIC_FONTSET (f) = NULL;
10966 }
10967 }
10968 }
10969
10970 /* No need to call XCloseIM. */
10971 dpyinfo->xim = NULL;
10972 XFree (dpyinfo->xim_styles);
10973 UNBLOCK_INPUT;
10974}
10975
10976#endif /* HAVE_X11R6 */
10977
10978/* Open the connection to the XIM server on display DPYINFO.
10979 RESOURCE_NAME is the resource name Emacs uses. */
10980
10981static void
10982xim_open_dpy (dpyinfo, resource_name)
10983 struct x_display_info *dpyinfo;
10984 char *resource_name;
10985{
10986 XIM xim;
10987
10988 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
10989 dpyinfo->xim = xim;
10990
10991 if (xim)
10992 {
10993 XIMValuesList *xim_values_list;
10994#ifdef HAVE_X11R6
10995 XIMCallback destroy;
10996#endif
10997
10998 /* Get supported styles and XIM values. */
10999 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11000
11001#ifdef HAVE_X11R6
11002 destroy.callback = xim_destroy_callback;
11003 destroy.client_data = (XPointer)dpyinfo;
11004 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11005#endif
11006 }
11007}
11008
11009
11010#ifdef HAVE_X11R6
11011
11012struct xim_inst_t
11013{
11014 struct x_display_info *dpyinfo;
11015 char *resource_name;
11016};
11017
11018/* XIM instantiate callback function, which is called whenever an XIM
11019 server is available. DISPLAY is teh display of the XIM.
11020 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11021 when the callback was registered. */
11022
11023static void
11024xim_instantiate_callback (display, client_data, call_data)
11025 Display *display;
11026 XPointer client_data;
11027 XPointer call_data;
11028{
11029 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11030 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11031
11032 /* We don't support multiple XIM connections. */
11033 if (dpyinfo->xim)
11034 return;
11035
11036 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11037
11038 /* Create XIC for the existing frames on the same display, as long
11039 as they have no XIC. */
11040 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11041 {
11042 Lisp_Object tail, frame;
11043
11044 BLOCK_INPUT;
11045 FOR_EACH_FRAME (tail, frame)
11046 {
11047 struct frame *f = XFRAME (frame);
11048
11049 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11050 if (FRAME_XIC (f) == NULL)
11051 {
11052 create_frame_xic (f);
11053 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11054 xic_set_statusarea (f);
11055 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11056 {
11057 struct window *w = XWINDOW (f->selected_window);
11058 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11059 }
11060 }
11061 }
11062
11063 UNBLOCK_INPUT;
11064 }
11065}
11066
11067#endif /* HAVE_X11R6 */
11068
11069
11070/* Open a connection to the XIM server on display DPYINFO.
11071 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11072 connection only at the first time. On X11R6, open the connection
11073 in the XIM instantiate callback function. */
11074
11075static void
11076xim_initialize (dpyinfo, resource_name)
11077 struct x_display_info *dpyinfo;
11078 char *resource_name;
11079{
11080#ifdef HAVE_X11R6
11081 struct xim_inst_t *xim_inst;
11082 int len;
11083
11084 dpyinfo->xim = NULL;
11085 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11086 xim_inst->dpyinfo = dpyinfo;
11087 len = strlen (resource_name);
11088 xim_inst->resource_name = (char *) xmalloc (len + 1);
11089 bcopy (resource_name, xim_inst->resource_name, len + 1);
11090 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11091 resource_name, EMACS_CLASS,
11092 xim_instantiate_callback,
11093 (XPointer)xim_inst);
11094#else /* not HAVE_X11R6 */
11095 dpyinfo->xim = NULL;
11096 xim_open_dpy (dpyinfo, resource_name);
11097#endif /* not HAVE_X11R6 */
11098}
11099
11100
11101/* Close the connection to the XIM server on display DPYINFO. */
11102
11103static void
11104xim_close_dpy (dpyinfo)
11105 struct x_display_info *dpyinfo;
11106{
11107#ifdef HAVE_X11R6
11108 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11109 NULL, EMACS_CLASS,
11110 xim_instantiate_callback, NULL);
11111#endif /* HAVE_X11R6 */
11112 XCloseIM (dpyinfo->xim);
11113 dpyinfo->xim = NULL;
11114 XFree (dpyinfo->xim_styles);
11115}
11116
11117#endif /* HAVE_X_I18N */
11118
11119
dc6f92b8 11120\f
2e365682
RS
11121/* Calculate the absolute position in frame F
11122 from its current recorded position values and gravity. */
11123
dfcf069d 11124void
43bca5d5 11125x_calc_absolute_position (f)
f676886a 11126 struct frame *f;
dc6f92b8 11127{
06a2c219 11128 Window child;
6dba1858 11129 int win_x = 0, win_y = 0;
7556890b 11130 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11131 int this_window;
11132
9829ddba
RS
11133 /* We have nothing to do if the current position
11134 is already for the top-left corner. */
11135 if (! ((flags & XNegative) || (flags & YNegative)))
11136 return;
11137
c81412a0 11138#ifdef USE_X_TOOLKIT
7556890b 11139 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11140#else
11141 this_window = FRAME_X_WINDOW (f);
11142#endif
6dba1858
RS
11143
11144 /* Find the position of the outside upper-left corner of
9829ddba
RS
11145 the inner window, with respect to the outer window.
11146 But do this only if we will need the results. */
7556890b 11147 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11148 {
9829ddba
RS
11149 int count;
11150
6dba1858 11151 BLOCK_INPUT;
9829ddba
RS
11152 count = x_catch_errors (FRAME_X_DISPLAY (f));
11153 while (1)
11154 {
11155 x_clear_errors (FRAME_X_DISPLAY (f));
11156 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11157
11158 /* From-window, to-window. */
11159 this_window,
11160 f->output_data.x->parent_desc,
11161
11162 /* From-position, to-position. */
11163 0, 0, &win_x, &win_y,
11164
11165 /* Child of win. */
11166 &child);
11167 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11168 {
11169 Window newroot, newparent = 0xdeadbeef;
11170 Window *newchildren;
11171 int nchildren;
11172
11173 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11174 &newparent, &newchildren, &nchildren))
11175 break;
58769bee 11176
7c3c78a3 11177 XFree ((char *) newchildren);
6dba1858 11178
9829ddba
RS
11179 f->output_data.x->parent_desc = newparent;
11180 }
11181 else
11182 break;
11183 }
6dba1858 11184
9829ddba 11185 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11186 UNBLOCK_INPUT;
11187 }
11188
11189 /* Treat negative positions as relative to the leftmost bottommost
11190 position that fits on the screen. */
20f55f9a 11191 if (flags & XNegative)
7556890b 11192 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11193 - 2 * f->output_data.x->border_width - win_x
11194 - PIXEL_WIDTH (f)
11195 + f->output_data.x->left_pos);
dc6f92b8 11196
20f55f9a 11197 if (flags & YNegative)
06a2c219
GM
11198 {
11199 int menubar_height = 0;
11200
11201#ifdef USE_X_TOOLKIT
11202 if (f->output_data.x->menubar_widget)
11203 menubar_height
11204 = (f->output_data.x->menubar_widget->core.height
11205 + f->output_data.x->menubar_widget->core.border_width);
11206#endif
11207
11208 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11209 - 2 * f->output_data.x->border_width
11210 - win_y
11211 - PIXEL_HEIGHT (f)
11212 - menubar_height
11213 + f->output_data.x->top_pos);
11214 }
2e365682 11215
3a35ab44
RS
11216 /* The left_pos and top_pos
11217 are now relative to the top and left screen edges,
11218 so the flags should correspond. */
7556890b 11219 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11220}
11221
3a35ab44
RS
11222/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11223 to really change the position, and 0 when calling from
11224 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11225 position values). It is -1 when calling from x_set_frame_parameters,
11226 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11227
dfcf069d 11228void
dc05a16b 11229x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11230 struct frame *f;
dc6f92b8 11231 register int xoff, yoff;
dc05a16b 11232 int change_gravity;
dc6f92b8 11233{
4a4cbdd5
KH
11234 int modified_top, modified_left;
11235
aa3ff7c9 11236 if (change_gravity > 0)
3a35ab44 11237 {
7556890b
RS
11238 f->output_data.x->top_pos = yoff;
11239 f->output_data.x->left_pos = xoff;
11240 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11241 if (xoff < 0)
7556890b 11242 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11243 if (yoff < 0)
7556890b
RS
11244 f->output_data.x->size_hint_flags |= YNegative;
11245 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11246 }
43bca5d5 11247 x_calc_absolute_position (f);
dc6f92b8
JB
11248
11249 BLOCK_INPUT;
c32cdd9a 11250 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11251
7556890b
RS
11252 modified_left = f->output_data.x->left_pos;
11253 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11254#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11255 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11256 /* It is a mystery why we need to add the border_width here
11257 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11258 if (change_gravity != 0)
4a4cbdd5 11259 {
7556890b
RS
11260 modified_left += f->output_data.x->border_width;
11261 modified_top += f->output_data.x->border_width;
4a4cbdd5 11262 }
e73ec6fa 11263#endif
4a4cbdd5 11264
3afe33e7 11265#ifdef USE_X_TOOLKIT
7556890b 11266 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11267 modified_left, modified_top);
3afe33e7 11268#else /* not USE_X_TOOLKIT */
334208b7 11269 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11270 modified_left, modified_top);
3afe33e7 11271#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11272 UNBLOCK_INPUT;
11273}
11274
bc20ebbf
FP
11275/* Call this to change the size of frame F's x-window.
11276 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11277 for this size change and subsequent size changes.
11278 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11279
dfcf069d 11280void
bc20ebbf 11281x_set_window_size (f, change_gravity, cols, rows)
f676886a 11282 struct frame *f;
bc20ebbf 11283 int change_gravity;
b1c884c3 11284 int cols, rows;
dc6f92b8 11285{
06a2c219 11286#ifndef USE_X_TOOLKIT
dc6f92b8 11287 int pixelwidth, pixelheight;
06a2c219 11288#endif
dc6f92b8 11289
80fd1fe2 11290 BLOCK_INPUT;
aee9a898
RS
11291
11292#ifdef USE_X_TOOLKIT
3a20653d
RS
11293 {
11294 /* The x and y position of the widget is clobbered by the
11295 call to XtSetValues within EmacsFrameSetCharSize.
11296 This is a real kludge, but I don't understand Xt so I can't
11297 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11298 int xpos = f->output_data.x->widget->core.x;
11299 int ypos = f->output_data.x->widget->core.y;
11300 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11301 f->output_data.x->widget->core.x = xpos;
11302 f->output_data.x->widget->core.y = ypos;
3a20653d 11303 }
80fd1fe2
FP
11304
11305#else /* not USE_X_TOOLKIT */
11306
b1c884c3 11307 check_frame_size (f, &rows, &cols);
7556890b 11308 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11309 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11310 ? 0
11311 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11312 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11313 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11314 f->output_data.x->flags_areas_extra
110859fc 11315 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11316 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11317 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11318
7556890b 11319 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11320 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11321
334208b7
RS
11322 XSync (FRAME_X_DISPLAY (f), False);
11323 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11324 pixelwidth, pixelheight);
b1c884c3
JB
11325
11326 /* Now, strictly speaking, we can't be sure that this is accurate,
11327 but the window manager will get around to dealing with the size
11328 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11329 ConfigureNotify event gets here.
11330
11331 We could just not bother storing any of this information here,
11332 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11333 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11334 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11335 point in the future when the ConfigureNotify event arrives.
11336
11337 We pass 1 for DELAY since we can't run Lisp code inside of
11338 a BLOCK_INPUT. */
7d1e984f 11339 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11340 PIXEL_WIDTH (f) = pixelwidth;
11341 PIXEL_HEIGHT (f) = pixelheight;
11342
aee9a898
RS
11343 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11344 receive in the ConfigureNotify event; if we get what we asked
11345 for, then the event won't cause the screen to become garbaged, so
11346 we have to make sure to do it here. */
11347 SET_FRAME_GARBAGED (f);
11348
11349 XFlush (FRAME_X_DISPLAY (f));
11350
11351#endif /* not USE_X_TOOLKIT */
11352
4d73d038 11353 /* If cursor was outside the new size, mark it as off. */
06a2c219 11354 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11355
aee9a898
RS
11356 /* Clear out any recollection of where the mouse highlighting was,
11357 since it might be in a place that's outside the new frame size.
11358 Actually checking whether it is outside is a pain in the neck,
11359 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11360 cancel_mouse_face (f);
dbc4e1c1 11361
dc6f92b8
JB
11362 UNBLOCK_INPUT;
11363}
dc6f92b8 11364\f
d047c4eb 11365/* Mouse warping. */
dc6f92b8 11366
9b378208 11367void
f676886a
JB
11368x_set_mouse_position (f, x, y)
11369 struct frame *f;
dc6f92b8
JB
11370 int x, y;
11371{
11372 int pix_x, pix_y;
11373
7556890b
RS
11374 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11375 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11376
11377 if (pix_x < 0) pix_x = 0;
11378 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11379
11380 if (pix_y < 0) pix_y = 0;
11381 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11382
11383 BLOCK_INPUT;
dc6f92b8 11384
334208b7
RS
11385 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11386 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11387 UNBLOCK_INPUT;
11388}
11389
9b378208
RS
11390/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11391
11392void
11393x_set_mouse_pixel_position (f, pix_x, pix_y)
11394 struct frame *f;
11395 int pix_x, pix_y;
11396{
11397 BLOCK_INPUT;
11398
334208b7
RS
11399 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11400 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11401 UNBLOCK_INPUT;
11402}
d047c4eb
KH
11403\f
11404/* focus shifting, raising and lowering. */
9b378208 11405
dfcf069d 11406void
f676886a
JB
11407x_focus_on_frame (f)
11408 struct frame *f;
dc6f92b8 11409{
1fb20991 11410#if 0 /* This proves to be unpleasant. */
f676886a 11411 x_raise_frame (f);
1fb20991 11412#endif
6d4238f3
JB
11413#if 0
11414 /* I don't think that the ICCCM allows programs to do things like this
11415 without the interaction of the window manager. Whatever you end up
f676886a 11416 doing with this code, do it to x_unfocus_frame too. */
334208b7 11417 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11418 RevertToPointerRoot, CurrentTime);
c118dd06 11419#endif /* ! 0 */
dc6f92b8
JB
11420}
11421
dfcf069d 11422void
f676886a
JB
11423x_unfocus_frame (f)
11424 struct frame *f;
dc6f92b8 11425{
6d4238f3 11426#if 0
f676886a 11427 /* Look at the remarks in x_focus_on_frame. */
0f941935 11428 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11429 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11430 RevertToPointerRoot, CurrentTime);
c118dd06 11431#endif /* ! 0 */
dc6f92b8
JB
11432}
11433
f676886a 11434/* Raise frame F. */
dc6f92b8 11435
dfcf069d 11436void
f676886a
JB
11437x_raise_frame (f)
11438 struct frame *f;
dc6f92b8 11439{
3a88c238 11440 if (f->async_visible)
dc6f92b8
JB
11441 {
11442 BLOCK_INPUT;
3afe33e7 11443#ifdef USE_X_TOOLKIT
7556890b 11444 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11445#else /* not USE_X_TOOLKIT */
334208b7 11446 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11447#endif /* not USE_X_TOOLKIT */
334208b7 11448 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11449 UNBLOCK_INPUT;
11450 }
11451}
11452
f676886a 11453/* Lower frame F. */
dc6f92b8 11454
dfcf069d 11455void
f676886a
JB
11456x_lower_frame (f)
11457 struct frame *f;
dc6f92b8 11458{
3a88c238 11459 if (f->async_visible)
dc6f92b8
JB
11460 {
11461 BLOCK_INPUT;
3afe33e7 11462#ifdef USE_X_TOOLKIT
7556890b 11463 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11464#else /* not USE_X_TOOLKIT */
334208b7 11465 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11466#endif /* not USE_X_TOOLKIT */
334208b7 11467 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11468 UNBLOCK_INPUT;
11469 }
11470}
11471
dbc4e1c1 11472static void
6b0442dc 11473XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11474 FRAME_PTR f;
6b0442dc 11475 int raise_flag;
dbc4e1c1 11476{
6b0442dc 11477 if (raise_flag)
dbc4e1c1
JB
11478 x_raise_frame (f);
11479 else
11480 x_lower_frame (f);
11481}
d047c4eb
KH
11482\f
11483/* Change of visibility. */
dc6f92b8 11484
9382638d
KH
11485/* This tries to wait until the frame is really visible.
11486 However, if the window manager asks the user where to position
11487 the frame, this will return before the user finishes doing that.
11488 The frame will not actually be visible at that time,
11489 but it will become visible later when the window manager
11490 finishes with it. */
11491
dfcf069d 11492void
f676886a
JB
11493x_make_frame_visible (f)
11494 struct frame *f;
dc6f92b8 11495{
990ba854 11496 Lisp_Object type;
1aa6072f 11497 int original_top, original_left;
dc6f92b8 11498
dc6f92b8 11499 BLOCK_INPUT;
dc6f92b8 11500
990ba854
RS
11501 type = x_icon_type (f);
11502 if (!NILP (type))
11503 x_bitmap_icon (f, type);
bdcd49ba 11504
f676886a 11505 if (! FRAME_VISIBLE_P (f))
90e65f07 11506 {
1aa6072f
RS
11507 /* We test FRAME_GARBAGED_P here to make sure we don't
11508 call x_set_offset a second time
11509 if we get to x_make_frame_visible a second time
11510 before the window gets really visible. */
11511 if (! FRAME_ICONIFIED_P (f)
11512 && ! f->output_data.x->asked_for_visible)
11513 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11514
11515 f->output_data.x->asked_for_visible = 1;
11516
90e65f07 11517 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11518 x_wm_set_window_state (f, NormalState);
3afe33e7 11519#ifdef USE_X_TOOLKIT
d7a38a2e 11520 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11521 XtMapWidget (f->output_data.x->widget);
3afe33e7 11522#else /* not USE_X_TOOLKIT */
7f9c7f94 11523 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11524#endif /* not USE_X_TOOLKIT */
0134a210
RS
11525#if 0 /* This seems to bring back scroll bars in the wrong places
11526 if the window configuration has changed. They seem
11527 to come back ok without this. */
ab648270 11528 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11529 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11530#endif
90e65f07 11531 }
dc6f92b8 11532
334208b7 11533 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11534
0dacf791
RS
11535 /* Synchronize to ensure Emacs knows the frame is visible
11536 before we do anything else. We do this loop with input not blocked
11537 so that incoming events are handled. */
11538 {
11539 Lisp_Object frame;
12ce2351 11540 int count;
28c01ffe
RS
11541 /* This must be before UNBLOCK_INPUT
11542 since events that arrive in response to the actions above
11543 will set it when they are handled. */
11544 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11545
11546 original_left = f->output_data.x->left_pos;
11547 original_top = f->output_data.x->top_pos;
c0a04927
RS
11548
11549 /* This must come after we set COUNT. */
11550 UNBLOCK_INPUT;
11551
2745e6c4 11552 /* We unblock here so that arriving X events are processed. */
1aa6072f 11553
dcb07ae9
RS
11554 /* Now move the window back to where it was "supposed to be".
11555 But don't do it if the gravity is negative.
11556 When the gravity is negative, this uses a position
28c01ffe
RS
11557 that is 3 pixels too low. Perhaps that's really the border width.
11558
11559 Don't do this if the window has never been visible before,
11560 because the window manager may choose the position
11561 and we don't want to override it. */
1aa6072f 11562
4d3f5d9a 11563 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11564 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11565 && previously_visible)
1aa6072f 11566 {
2745e6c4
RS
11567 Drawable rootw;
11568 int x, y;
11569 unsigned int width, height, border, depth;
06a2c219 11570
1aa6072f 11571 BLOCK_INPUT;
9829ddba 11572
06a2c219
GM
11573 /* On some window managers (such as FVWM) moving an existing
11574 window, even to the same place, causes the window manager
11575 to introduce an offset. This can cause the window to move
11576 to an unexpected location. Check the geometry (a little
11577 slow here) and then verify that the window is in the right
11578 place. If the window is not in the right place, move it
11579 there, and take the potential window manager hit. */
2745e6c4
RS
11580 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11581 &rootw, &x, &y, &width, &height, &border, &depth);
11582
11583 if (original_left != x || original_top != y)
11584 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11585 original_left, original_top);
11586
1aa6072f
RS
11587 UNBLOCK_INPUT;
11588 }
9829ddba 11589
e0c1aef2 11590 XSETFRAME (frame, f);
c0a04927 11591
12ce2351
GM
11592 /* Wait until the frame is visible. Process X events until a
11593 MapNotify event has been seen, or until we think we won't get a
11594 MapNotify at all.. */
11595 for (count = input_signal_count + 10;
11596 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11597 {
12ce2351 11598 /* Force processing of queued events. */
334208b7 11599 x_sync (f);
12ce2351
GM
11600
11601 /* Machines that do polling rather than SIGIO have been
11602 observed to go into a busy-wait here. So we'll fake an
11603 alarm signal to let the handler know that there's something
11604 to be read. We used to raise a real alarm, but it seems
11605 that the handler isn't always enabled here. This is
11606 probably a bug. */
8b2f8d4e 11607 if (input_polling_used ())
3b2fa4e6 11608 {
12ce2351
GM
11609 /* It could be confusing if a real alarm arrives while
11610 processing the fake one. Turn it off and let the
11611 handler reset it. */
bffcfca9
GM
11612 int old_poll_suppress_count = poll_suppress_count;
11613 poll_suppress_count = 1;
11614 poll_for_input_1 ();
11615 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11616 }
12ce2351
GM
11617
11618 /* See if a MapNotify event has been processed. */
11619 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11620 }
0dacf791 11621 }
dc6f92b8
JB
11622}
11623
06a2c219 11624/* Change from mapped state to withdrawn state. */
dc6f92b8 11625
d047c4eb
KH
11626/* Make the frame visible (mapped and not iconified). */
11627
dfcf069d 11628void
f676886a
JB
11629x_make_frame_invisible (f)
11630 struct frame *f;
dc6f92b8 11631{
546e6d5b
RS
11632 Window window;
11633
11634#ifdef USE_X_TOOLKIT
11635 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11636 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11637#else /* not USE_X_TOOLKIT */
11638 window = FRAME_X_WINDOW (f);
11639#endif /* not USE_X_TOOLKIT */
dc6f92b8 11640
9319ae23 11641 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11642 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11643 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11644
5627c40e 11645#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11646 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11647 return;
5627c40e 11648#endif
dc6f92b8
JB
11649
11650 BLOCK_INPUT;
c118dd06 11651
af31d76f
RS
11652 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11653 that the current position of the window is user-specified, rather than
11654 program-specified, so that when the window is mapped again, it will be
11655 placed at the same location, without forcing the user to position it
11656 by hand again (they have already done that once for this window.) */
c32cdd9a 11657 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11658
c118dd06
JB
11659#ifdef HAVE_X11R4
11660
334208b7
RS
11661 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11662 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11663 {
11664 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11665 error ("Can't notify window manager of window withdrawal");
c118dd06 11666 }
c118dd06 11667#else /* ! defined (HAVE_X11R4) */
16bd92ea 11668
c118dd06 11669 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11670 if (! EQ (Vx_no_window_manager, Qt))
11671 {
16bd92ea 11672 XEvent unmap;
dc6f92b8 11673
16bd92ea 11674 unmap.xunmap.type = UnmapNotify;
546e6d5b 11675 unmap.xunmap.window = window;
334208b7 11676 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11677 unmap.xunmap.from_configure = False;
334208b7
RS
11678 if (! XSendEvent (FRAME_X_DISPLAY (f),
11679 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11680 False,
06a2c219 11681 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11682 &unmap))
11683 {
11684 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11685 error ("Can't notify window manager of withdrawal");
16bd92ea 11686 }
dc6f92b8
JB
11687 }
11688
16bd92ea 11689 /* Unmap the window ourselves. Cheeky! */
334208b7 11690 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11691#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11692
5627c40e
RS
11693 /* We can't distinguish this from iconification
11694 just by the event that we get from the server.
11695 So we can't win using the usual strategy of letting
11696 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11697 and synchronize with the server to make sure we agree. */
11698 f->visible = 0;
11699 FRAME_ICONIFIED_P (f) = 0;
11700 f->async_visible = 0;
11701 f->async_iconified = 0;
11702
334208b7 11703 x_sync (f);
5627c40e 11704
dc6f92b8
JB
11705 UNBLOCK_INPUT;
11706}
11707
06a2c219 11708/* Change window state from mapped to iconified. */
dc6f92b8 11709
dfcf069d 11710void
f676886a
JB
11711x_iconify_frame (f)
11712 struct frame *f;
dc6f92b8 11713{
3afe33e7 11714 int result;
990ba854 11715 Lisp_Object type;
dc6f92b8 11716
9319ae23 11717 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11718 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11719 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11720
3a88c238 11721 if (f->async_iconified)
dc6f92b8
JB
11722 return;
11723
3afe33e7 11724 BLOCK_INPUT;
546e6d5b 11725
9af3143a
RS
11726 FRAME_SAMPLE_VISIBILITY (f);
11727
990ba854
RS
11728 type = x_icon_type (f);
11729 if (!NILP (type))
11730 x_bitmap_icon (f, type);
bdcd49ba
RS
11731
11732#ifdef USE_X_TOOLKIT
11733
546e6d5b
RS
11734 if (! FRAME_VISIBLE_P (f))
11735 {
11736 if (! EQ (Vx_no_window_manager, Qt))
11737 x_wm_set_window_state (f, IconicState);
11738 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11739 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11740 /* The server won't give us any event to indicate
11741 that an invisible frame was changed to an icon,
11742 so we have to record it here. */
11743 f->iconified = 1;
1e6bc770 11744 f->visible = 1;
9cf30a30 11745 f->async_iconified = 1;
1e6bc770 11746 f->async_visible = 0;
546e6d5b
RS
11747 UNBLOCK_INPUT;
11748 return;
11749 }
11750
334208b7 11751 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11752 XtWindow (f->output_data.x->widget),
334208b7 11753 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11754 UNBLOCK_INPUT;
11755
11756 if (!result)
546e6d5b 11757 error ("Can't notify window manager of iconification");
3afe33e7
RS
11758
11759 f->async_iconified = 1;
1e6bc770
RS
11760 f->async_visible = 0;
11761
8c002a25
KH
11762
11763 BLOCK_INPUT;
334208b7 11764 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11765 UNBLOCK_INPUT;
3afe33e7
RS
11766#else /* not USE_X_TOOLKIT */
11767
fd13dbb2
RS
11768 /* Make sure the X server knows where the window should be positioned,
11769 in case the user deiconifies with the window manager. */
11770 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11771 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11772
16bd92ea
JB
11773 /* Since we don't know which revision of X we're running, we'll use both
11774 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11775
11776 /* X11R4: send a ClientMessage to the window manager using the
11777 WM_CHANGE_STATE type. */
11778 {
11779 XEvent message;
58769bee 11780
c118dd06 11781 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11782 message.xclient.type = ClientMessage;
334208b7 11783 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11784 message.xclient.format = 32;
11785 message.xclient.data.l[0] = IconicState;
11786
334208b7
RS
11787 if (! XSendEvent (FRAME_X_DISPLAY (f),
11788 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11789 False,
11790 SubstructureRedirectMask | SubstructureNotifyMask,
11791 &message))
dc6f92b8
JB
11792 {
11793 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11794 error ("Can't notify window manager of iconification");
dc6f92b8 11795 }
16bd92ea 11796 }
dc6f92b8 11797
58769bee 11798 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11799 IconicState. */
11800 x_wm_set_window_state (f, IconicState);
dc6f92b8 11801
a9c00105
RS
11802 if (!FRAME_VISIBLE_P (f))
11803 {
11804 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11805 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11806 }
11807
3a88c238 11808 f->async_iconified = 1;
1e6bc770 11809 f->async_visible = 0;
dc6f92b8 11810
334208b7 11811 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11812 UNBLOCK_INPUT;
8c002a25 11813#endif /* not USE_X_TOOLKIT */
dc6f92b8 11814}
d047c4eb 11815\f
c0ff3fab 11816/* Destroy the X window of frame F. */
dc6f92b8 11817
dfcf069d 11818void
c0ff3fab 11819x_destroy_window (f)
f676886a 11820 struct frame *f;
dc6f92b8 11821{
7f9c7f94
RS
11822 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11823
dc6f92b8 11824 BLOCK_INPUT;
c0ff3fab 11825
6186a4a0
RS
11826 /* If a display connection is dead, don't try sending more
11827 commands to the X server. */
11828 if (dpyinfo->display != 0)
11829 {
11830 if (f->output_data.x->icon_desc != 0)
11831 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 11832#ifdef HAVE_X_I18N
f5d11644
GM
11833 if (FRAME_XIC (f))
11834 free_frame_xic (f);
31f41daf 11835#endif
6186a4a0 11836 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11837#ifdef USE_X_TOOLKIT
06a2c219
GM
11838 if (f->output_data.x->widget)
11839 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11840 free_frame_menubar (f);
3afe33e7
RS
11841#endif /* USE_X_TOOLKIT */
11842
6186a4a0
RS
11843 free_frame_faces (f);
11844 XFlush (FRAME_X_DISPLAY (f));
11845 }
dc6f92b8 11846
df89d8a4 11847 if (f->output_data.x->saved_menu_event)
06a2c219 11848 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11849
7556890b
RS
11850 xfree (f->output_data.x);
11851 f->output_data.x = 0;
0f941935
KH
11852 if (f == dpyinfo->x_focus_frame)
11853 dpyinfo->x_focus_frame = 0;
11854 if (f == dpyinfo->x_focus_event_frame)
11855 dpyinfo->x_focus_event_frame = 0;
11856 if (f == dpyinfo->x_highlight_frame)
11857 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11858
7f9c7f94
RS
11859 dpyinfo->reference_count--;
11860
11861 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11862 {
7f9c7f94
RS
11863 dpyinfo->mouse_face_beg_row
11864 = dpyinfo->mouse_face_beg_col = -1;
11865 dpyinfo->mouse_face_end_row
11866 = dpyinfo->mouse_face_end_col = -1;
11867 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11868 dpyinfo->mouse_face_deferred_gc = 0;
11869 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11870 }
0134a210 11871
c0ff3fab 11872 UNBLOCK_INPUT;
dc6f92b8
JB
11873}
11874\f
f451eb13
JB
11875/* Setting window manager hints. */
11876
af31d76f
RS
11877/* Set the normal size hints for the window manager, for frame F.
11878 FLAGS is the flags word to use--or 0 meaning preserve the flags
11879 that the window now has.
11880 If USER_POSITION is nonzero, we set the USPosition
11881 flag (this is useful when FLAGS is 0). */
6dba1858 11882
dfcf069d 11883void
af31d76f 11884x_wm_set_size_hint (f, flags, user_position)
f676886a 11885 struct frame *f;
af31d76f
RS
11886 long flags;
11887 int user_position;
dc6f92b8
JB
11888{
11889 XSizeHints size_hints;
3afe33e7
RS
11890
11891#ifdef USE_X_TOOLKIT
7e4f2521
FP
11892 Arg al[2];
11893 int ac = 0;
11894 Dimension widget_width, widget_height;
7556890b 11895 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11896#else /* not USE_X_TOOLKIT */
c118dd06 11897 Window window = FRAME_X_WINDOW (f);
3afe33e7 11898#endif /* not USE_X_TOOLKIT */
dc6f92b8 11899
b72a58fd
RS
11900 /* Setting PMaxSize caused various problems. */
11901 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11902
7556890b
RS
11903 size_hints.x = f->output_data.x->left_pos;
11904 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11905
7e4f2521
FP
11906#ifdef USE_X_TOOLKIT
11907 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11908 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11909 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11910 size_hints.height = widget_height;
11911 size_hints.width = widget_width;
11912#else /* not USE_X_TOOLKIT */
f676886a
JB
11913 size_hints.height = PIXEL_HEIGHT (f);
11914 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11915#endif /* not USE_X_TOOLKIT */
7553a6b7 11916
7556890b
RS
11917 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11918 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11919 size_hints.max_width
11920 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11921 size_hints.max_height
11922 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11923
d067ea8b
KH
11924 /* Calculate the base and minimum sizes.
11925
11926 (When we use the X toolkit, we don't do it here.
11927 Instead we copy the values that the widgets are using, below.) */
11928#ifndef USE_X_TOOLKIT
b1c884c3 11929 {
b0342f17 11930 int base_width, base_height;
0134a210 11931 int min_rows = 0, min_cols = 0;
b0342f17 11932
f451eb13
JB
11933 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11934 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11935
0134a210 11936 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11937
0134a210
RS
11938 /* The window manager uses the base width hints to calculate the
11939 current number of rows and columns in the frame while
11940 resizing; min_width and min_height aren't useful for this
11941 purpose, since they might not give the dimensions for a
11942 zero-row, zero-column frame.
58769bee 11943
0134a210
RS
11944 We use the base_width and base_height members if we have
11945 them; otherwise, we set the min_width and min_height members
11946 to the size for a zero x zero frame. */
b0342f17
JB
11947
11948#ifdef HAVE_X11R4
0134a210
RS
11949 size_hints.flags |= PBaseSize;
11950 size_hints.base_width = base_width;
11951 size_hints.base_height = base_height;
11952 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11953 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11954#else
0134a210
RS
11955 size_hints.min_width = base_width;
11956 size_hints.min_height = base_height;
b0342f17 11957#endif
b1c884c3 11958 }
dc6f92b8 11959
d067ea8b 11960 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11961 if (flags)
dc6f92b8 11962 {
d067ea8b
KH
11963 size_hints.flags |= flags;
11964 goto no_read;
11965 }
11966#endif /* not USE_X_TOOLKIT */
11967
11968 {
11969 XSizeHints hints; /* Sometimes I hate X Windows... */
11970 long supplied_return;
11971 int value;
af31d76f
RS
11972
11973#ifdef HAVE_X11R4
d067ea8b
KH
11974 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11975 &supplied_return);
af31d76f 11976#else
d067ea8b 11977 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11978#endif
58769bee 11979
d067ea8b
KH
11980#ifdef USE_X_TOOLKIT
11981 size_hints.base_height = hints.base_height;
11982 size_hints.base_width = hints.base_width;
11983 size_hints.min_height = hints.min_height;
11984 size_hints.min_width = hints.min_width;
11985#endif
11986
11987 if (flags)
11988 size_hints.flags |= flags;
11989 else
11990 {
11991 if (value == 0)
11992 hints.flags = 0;
11993 if (hints.flags & PSize)
11994 size_hints.flags |= PSize;
11995 if (hints.flags & PPosition)
11996 size_hints.flags |= PPosition;
11997 if (hints.flags & USPosition)
11998 size_hints.flags |= USPosition;
11999 if (hints.flags & USSize)
12000 size_hints.flags |= USSize;
12001 }
12002 }
12003
06a2c219 12004#ifndef USE_X_TOOLKIT
d067ea8b 12005 no_read:
06a2c219 12006#endif
0134a210 12007
af31d76f 12008#ifdef PWinGravity
7556890b 12009 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12010 size_hints.flags |= PWinGravity;
dc05a16b 12011
af31d76f 12012 if (user_position)
6dba1858 12013 {
af31d76f
RS
12014 size_hints.flags &= ~ PPosition;
12015 size_hints.flags |= USPosition;
6dba1858 12016 }
2554751d 12017#endif /* PWinGravity */
6dba1858 12018
b0342f17 12019#ifdef HAVE_X11R4
334208b7 12020 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12021#else
334208b7 12022 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12023#endif
dc6f92b8
JB
12024}
12025
12026/* Used for IconicState or NormalState */
06a2c219 12027
dfcf069d 12028void
f676886a
JB
12029x_wm_set_window_state (f, state)
12030 struct frame *f;
dc6f92b8
JB
12031 int state;
12032{
3afe33e7 12033#ifdef USE_X_TOOLKIT
546e6d5b
RS
12034 Arg al[1];
12035
12036 XtSetArg (al[0], XtNinitialState, state);
7556890b 12037 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12038#else /* not USE_X_TOOLKIT */
c118dd06 12039 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12040
7556890b
RS
12041 f->output_data.x->wm_hints.flags |= StateHint;
12042 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12043
7556890b 12044 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12045#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12046}
12047
dfcf069d 12048void
7f2ae036 12049x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12050 struct frame *f;
7f2ae036 12051 int pixmap_id;
dc6f92b8 12052{
d2bd6bc4
RS
12053 Pixmap icon_pixmap;
12054
06a2c219 12055#ifndef USE_X_TOOLKIT
c118dd06 12056 Window window = FRAME_X_WINDOW (f);
75231bad 12057#endif
dc6f92b8 12058
7f2ae036 12059 if (pixmap_id > 0)
dbc4e1c1 12060 {
d2bd6bc4 12061 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12062 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12063 }
12064 else
68568555
RS
12065 {
12066 /* It seems there is no way to turn off use of an icon pixmap.
12067 The following line does it, only if no icon has yet been created,
12068 for some window managers. But with mwm it crashes.
12069 Some people say it should clear the IconPixmapHint bit in this case,
12070 but that doesn't work, and the X consortium said it isn't the
12071 right thing at all. Since there is no way to win,
12072 best to explicitly give up. */
12073#if 0
12074 f->output_data.x->wm_hints.icon_pixmap = None;
12075#else
12076 return;
12077#endif
12078 }
b1c884c3 12079
d2bd6bc4
RS
12080#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12081
12082 {
12083 Arg al[1];
12084 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12085 XtSetValues (f->output_data.x->widget, al, 1);
12086 }
12087
12088#else /* not USE_X_TOOLKIT */
12089
7556890b
RS
12090 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12091 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12092
12093#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12094}
12095
dfcf069d 12096void
f676886a
JB
12097x_wm_set_icon_position (f, icon_x, icon_y)
12098 struct frame *f;
dc6f92b8
JB
12099 int icon_x, icon_y;
12100{
75231bad 12101#ifdef USE_X_TOOLKIT
7556890b 12102 Window window = XtWindow (f->output_data.x->widget);
75231bad 12103#else
c118dd06 12104 Window window = FRAME_X_WINDOW (f);
75231bad 12105#endif
dc6f92b8 12106
7556890b
RS
12107 f->output_data.x->wm_hints.flags |= IconPositionHint;
12108 f->output_data.x->wm_hints.icon_x = icon_x;
12109 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12110
7556890b 12111 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12112}
12113
12114\f
06a2c219
GM
12115/***********************************************************************
12116 Fonts
12117 ***********************************************************************/
dc43ef94
KH
12118
12119/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12120
dc43ef94
KH
12121struct font_info *
12122x_get_font_info (f, font_idx)
12123 FRAME_PTR f;
12124 int font_idx;
12125{
12126 return (FRAME_X_FONT_TABLE (f) + font_idx);
12127}
12128
12129
12130/* Return a list of names of available fonts matching PATTERN on frame
12131 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12132 to be listed. Frame F NULL means we have not yet created any
12133 frame on X, and consult the first display in x_display_list.
12134 MAXNAMES sets a limit on how many fonts to match. */
12135
12136Lisp_Object
12137x_list_fonts (f, pattern, size, maxnames)
12138 FRAME_PTR f;
12139 Lisp_Object pattern;
12140 int size;
12141 int maxnames;
12142{
06a2c219
GM
12143 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12144 Lisp_Object tem, second_best;
dc43ef94 12145 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12146 int try_XLoadQueryFont = 0;
53ca4657 12147 int count;
dc43ef94 12148
6b0efe73 12149 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12150 if (NILP (patterns))
12151 patterns = Fcons (pattern, Qnil);
81ba44e5 12152
09c6077f
KH
12153 if (maxnames == 1 && !size)
12154 /* We can return any single font matching PATTERN. */
12155 try_XLoadQueryFont = 1;
9a32686f 12156
8e713be6 12157 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12158 {
dc43ef94
KH
12159 int num_fonts;
12160 char **names;
12161
8e713be6 12162 pattern = XCAR (patterns);
536f4067
RS
12163 /* See if we cached the result for this particular query.
12164 The cache is an alist of the form:
12165 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12166 */
8e713be6 12167 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12168 key = Fcons (pattern, make_number (maxnames)),
12169 !NILP (list = Fassoc (key, tem))))
12170 {
12171 list = Fcdr_safe (list);
12172 /* We have a cashed list. Don't have to get the list again. */
12173 goto label_cached;
12174 }
12175
12176 /* At first, put PATTERN in the cache. */
09c6077f 12177
dc43ef94 12178 BLOCK_INPUT;
17d85edc
KH
12179 count = x_catch_errors (dpy);
12180
09c6077f
KH
12181 if (try_XLoadQueryFont)
12182 {
12183 XFontStruct *font;
12184 unsigned long value;
12185
12186 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12187 if (x_had_errors_p (dpy))
12188 {
12189 /* This error is perhaps due to insufficient memory on X
12190 server. Let's just ignore it. */
12191 font = NULL;
12192 x_clear_errors (dpy);
12193 }
12194
09c6077f
KH
12195 if (font
12196 && XGetFontProperty (font, XA_FONT, &value))
12197 {
12198 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12199 int len = strlen (name);
01c752b5 12200 char *tmp;
09c6077f 12201
6f6512e8
KH
12202 /* If DXPC (a Differential X Protocol Compressor)
12203 Ver.3.7 is running, XGetAtomName will return null
12204 string. We must avoid such a name. */
12205 if (len == 0)
12206 try_XLoadQueryFont = 0;
12207 else
12208 {
12209 num_fonts = 1;
12210 names = (char **) alloca (sizeof (char *));
12211 /* Some systems only allow alloca assigned to a
12212 simple var. */
12213 tmp = (char *) alloca (len + 1); names[0] = tmp;
12214 bcopy (name, names[0], len + 1);
12215 XFree (name);
12216 }
09c6077f
KH
12217 }
12218 else
12219 try_XLoadQueryFont = 0;
a083fd23
RS
12220
12221 if (font)
12222 XFreeFont (dpy, font);
09c6077f
KH
12223 }
12224
12225 if (!try_XLoadQueryFont)
17d85edc
KH
12226 {
12227 /* We try at least 10 fonts because XListFonts will return
12228 auto-scaled fonts at the head. */
12229 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12230 &num_fonts);
12231 if (x_had_errors_p (dpy))
12232 {
12233 /* This error is perhaps due to insufficient memory on X
12234 server. Let's just ignore it. */
12235 names = NULL;
12236 x_clear_errors (dpy);
12237 }
12238 }
12239
12240 x_uncatch_errors (dpy, count);
dc43ef94
KH
12241 UNBLOCK_INPUT;
12242
12243 if (names)
12244 {
12245 int i;
dc43ef94
KH
12246
12247 /* Make a list of all the fonts we got back.
12248 Store that in the font cache for the display. */
12249 for (i = 0; i < num_fonts; i++)
12250 {
06a2c219 12251 int width = 0;
dc43ef94 12252 char *p = names[i];
06a2c219
GM
12253 int average_width = -1, dashes = 0;
12254
dc43ef94 12255 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12256 14 dashes, and the field value following 12th dash
12257 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12258 is usually too ugly to be used for editing. Let's
12259 ignore it. */
dc43ef94
KH
12260 while (*p)
12261 if (*p++ == '-')
12262 {
12263 dashes++;
12264 if (dashes == 7) /* PIXEL_SIZE field */
12265 width = atoi (p);
12266 else if (dashes == 12) /* AVERAGE_WIDTH field */
12267 average_width = atoi (p);
12268 }
12269 if (dashes < 14 || average_width != 0)
12270 {
12271 tem = build_string (names[i]);
12272 if (NILP (Fassoc (tem, list)))
12273 {
12274 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12275 && ((fast_c_string_match_ignore_case
12276 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12277 >= 0))
12278 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12279 width of this font. */
dc43ef94
KH
12280 list = Fcons (Fcons (tem, make_number (width)), list);
12281 else
12282 /* For the moment, width is not known. */
12283 list = Fcons (Fcons (tem, Qnil), list);
12284 }
12285 }
12286 }
09c6077f
KH
12287 if (!try_XLoadQueryFont)
12288 XFreeFontNames (names);
dc43ef94
KH
12289 }
12290
b5210ea7 12291 /* Now store the result in the cache. */
dc43ef94 12292 if (f != NULL)
8e713be6 12293 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12294 = Fcons (Fcons (key, list),
8e713be6 12295 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12296
b5210ea7
KH
12297 label_cached:
12298 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12299
b5210ea7
KH
12300 newlist = second_best = Qnil;
12301 /* Make a list of the fonts that have the right width. */
8e713be6 12302 for (; CONSP (list); list = XCDR (list))
b5210ea7 12303 {
536f4067
RS
12304 int found_size;
12305
8e713be6 12306 tem = XCAR (list);
dc43ef94 12307
8e713be6 12308 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12309 continue;
12310 if (!size)
12311 {
8e713be6 12312 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12313 continue;
12314 }
dc43ef94 12315
8e713be6 12316 if (!INTEGERP (XCDR (tem)))
dc43ef94 12317 {
b5210ea7
KH
12318 /* Since we have not yet known the size of this font, we
12319 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12320 XFontStruct *thisinfo;
12321
12322 BLOCK_INPUT;
17d85edc 12323 count = x_catch_errors (dpy);
dc43ef94 12324 thisinfo = XLoadQueryFont (dpy,
8e713be6 12325 XSTRING (XCAR (tem))->data);
17d85edc
KH
12326 if (x_had_errors_p (dpy))
12327 {
12328 /* This error is perhaps due to insufficient memory on X
12329 server. Let's just ignore it. */
12330 thisinfo = NULL;
12331 x_clear_errors (dpy);
12332 }
12333 x_uncatch_errors (dpy, count);
dc43ef94
KH
12334 UNBLOCK_INPUT;
12335
12336 if (thisinfo)
12337 {
8e713be6 12338 XCDR (tem)
536f4067
RS
12339 = (thisinfo->min_bounds.width == 0
12340 ? make_number (0)
12341 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12342 XFreeFont (dpy, thisinfo);
12343 }
12344 else
b5210ea7 12345 /* For unknown reason, the previous call of XListFont had
06a2c219 12346 returned a font which can't be opened. Record the size
b5210ea7 12347 as 0 not to try to open it again. */
8e713be6 12348 XCDR (tem) = make_number (0);
dc43ef94 12349 }
536f4067 12350
8e713be6 12351 found_size = XINT (XCDR (tem));
536f4067 12352 if (found_size == size)
8e713be6 12353 newlist = Fcons (XCAR (tem), newlist);
536f4067 12354 else if (found_size > 0)
b5210ea7 12355 {
536f4067 12356 if (NILP (second_best))
b5210ea7 12357 second_best = tem;
536f4067
RS
12358 else if (found_size < size)
12359 {
8e713be6
KR
12360 if (XINT (XCDR (second_best)) > size
12361 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12362 second_best = tem;
12363 }
12364 else
12365 {
8e713be6
KR
12366 if (XINT (XCDR (second_best)) > size
12367 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12368 second_best = tem;
12369 }
b5210ea7
KH
12370 }
12371 }
12372 if (!NILP (newlist))
12373 break;
12374 else if (!NILP (second_best))
12375 {
8e713be6 12376 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12377 break;
dc43ef94 12378 }
dc43ef94
KH
12379 }
12380
12381 return newlist;
12382}
12383
06a2c219
GM
12384
12385#if GLYPH_DEBUG
12386
12387/* Check that FONT is valid on frame F. It is if it can be found in F's
12388 font table. */
12389
12390static void
12391x_check_font (f, font)
12392 struct frame *f;
12393 XFontStruct *font;
12394{
12395 int i;
12396 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12397
12398 xassert (font != NULL);
12399
12400 for (i = 0; i < dpyinfo->n_fonts; i++)
12401 if (dpyinfo->font_table[i].name
12402 && font == dpyinfo->font_table[i].font)
12403 break;
12404
12405 xassert (i < dpyinfo->n_fonts);
12406}
12407
12408#endif /* GLYPH_DEBUG != 0 */
12409
12410/* Set *W to the minimum width, *H to the minimum font height of FONT.
12411 Note: There are (broken) X fonts out there with invalid XFontStruct
12412 min_bounds contents. For example, handa@etl.go.jp reports that
12413 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12414 have font->min_bounds.width == 0. */
12415
12416static INLINE void
12417x_font_min_bounds (font, w, h)
12418 XFontStruct *font;
12419 int *w, *h;
12420{
12421 *h = FONT_HEIGHT (font);
12422 *w = font->min_bounds.width;
12423
12424 /* Try to handle the case where FONT->min_bounds has invalid
12425 contents. Since the only font known to have invalid min_bounds
12426 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12427 if (*w <= 0)
12428 *w = font->max_bounds.width;
12429}
12430
12431
12432/* Compute the smallest character width and smallest font height over
12433 all fonts available on frame F. Set the members smallest_char_width
12434 and smallest_font_height in F's x_display_info structure to
12435 the values computed. Value is non-zero if smallest_font_height or
12436 smallest_char_width become smaller than they were before. */
12437
12438static int
12439x_compute_min_glyph_bounds (f)
12440 struct frame *f;
12441{
12442 int i;
12443 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12444 XFontStruct *font;
12445 int old_width = dpyinfo->smallest_char_width;
12446 int old_height = dpyinfo->smallest_font_height;
12447
12448 dpyinfo->smallest_font_height = 100000;
12449 dpyinfo->smallest_char_width = 100000;
12450
12451 for (i = 0; i < dpyinfo->n_fonts; ++i)
12452 if (dpyinfo->font_table[i].name)
12453 {
12454 struct font_info *fontp = dpyinfo->font_table + i;
12455 int w, h;
12456
12457 font = (XFontStruct *) fontp->font;
12458 xassert (font != (XFontStruct *) ~0);
12459 x_font_min_bounds (font, &w, &h);
12460
12461 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12462 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12463 }
12464
12465 xassert (dpyinfo->smallest_char_width > 0
12466 && dpyinfo->smallest_font_height > 0);
12467
12468 return (dpyinfo->n_fonts == 1
12469 || dpyinfo->smallest_char_width < old_width
12470 || dpyinfo->smallest_font_height < old_height);
12471}
12472
12473
dc43ef94
KH
12474/* Load font named FONTNAME of the size SIZE for frame F, and return a
12475 pointer to the structure font_info while allocating it dynamically.
12476 If SIZE is 0, load any size of font.
12477 If loading is failed, return NULL. */
12478
12479struct font_info *
12480x_load_font (f, fontname, size)
12481 struct frame *f;
12482 register char *fontname;
12483 int size;
12484{
12485 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12486 Lisp_Object font_names;
d645aaa4 12487 int count;
dc43ef94
KH
12488
12489 /* Get a list of all the fonts that match this name. Once we
12490 have a list of matching fonts, we compare them against the fonts
12491 we already have by comparing names. */
09c6077f 12492 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12493
12494 if (!NILP (font_names))
12495 {
12496 Lisp_Object tail;
12497 int i;
12498
12499 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12500 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12501 if (dpyinfo->font_table[i].name
12502 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12503 XSTRING (XCAR (tail))->data)
06a2c219 12504 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12505 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12506 return (dpyinfo->font_table + i);
12507 }
12508
12509 /* Load the font and add it to the table. */
12510 {
12511 char *full_name;
12512 XFontStruct *font;
12513 struct font_info *fontp;
12514 unsigned long value;
06a2c219 12515 int i;
dc43ef94 12516
2da424f1
KH
12517 /* If we have found fonts by x_list_font, load one of them. If
12518 not, we still try to load a font by the name given as FONTNAME
12519 because XListFonts (called in x_list_font) of some X server has
12520 a bug of not finding a font even if the font surely exists and
12521 is loadable by XLoadQueryFont. */
e1d6d5b9 12522 if (size > 0 && !NILP (font_names))
8e713be6 12523 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12524
12525 BLOCK_INPUT;
d645aaa4 12526 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12527 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12528 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12529 {
12530 /* This error is perhaps due to insufficient memory on X
12531 server. Let's just ignore it. */
12532 font = NULL;
12533 x_clear_errors (FRAME_X_DISPLAY (f));
12534 }
12535 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12536 UNBLOCK_INPUT;
b5210ea7 12537 if (!font)
dc43ef94
KH
12538 return NULL;
12539
06a2c219
GM
12540 /* Find a free slot in the font table. */
12541 for (i = 0; i < dpyinfo->n_fonts; ++i)
12542 if (dpyinfo->font_table[i].name == NULL)
12543 break;
12544
12545 /* If no free slot found, maybe enlarge the font table. */
12546 if (i == dpyinfo->n_fonts
12547 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12548 {
06a2c219
GM
12549 int sz;
12550 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12551 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12552 dpyinfo->font_table
06a2c219 12553 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12554 }
12555
06a2c219
GM
12556 fontp = dpyinfo->font_table + i;
12557 if (i == dpyinfo->n_fonts)
12558 ++dpyinfo->n_fonts;
dc43ef94
KH
12559
12560 /* Now fill in the slots of *FONTP. */
12561 BLOCK_INPUT;
12562 fontp->font = font;
06a2c219 12563 fontp->font_idx = i;
dc43ef94
KH
12564 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12565 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12566
12567 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12568 full_name = 0;
12569 if (XGetFontProperty (font, XA_FONT, &value))
12570 {
12571 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12572 char *p = name;
12573 int dashes = 0;
12574
12575 /* Count the number of dashes in the "full name".
12576 If it is too few, this isn't really the font's full name,
12577 so don't use it.
12578 In X11R4, the fonts did not come with their canonical names
12579 stored in them. */
12580 while (*p)
12581 {
12582 if (*p == '-')
12583 dashes++;
12584 p++;
12585 }
12586
12587 if (dashes >= 13)
12588 {
12589 full_name = (char *) xmalloc (p - name + 1);
12590 bcopy (name, full_name, p - name + 1);
12591 }
12592
12593 XFree (name);
12594 }
12595
12596 if (full_name != 0)
12597 fontp->full_name = full_name;
12598 else
12599 fontp->full_name = fontp->name;
12600
12601 fontp->size = font->max_bounds.width;
d5749adb
KH
12602 fontp->height = FONT_HEIGHT (font);
12603 {
12604 /* For some font, ascent and descent in max_bounds field is
12605 larger than the above value. */
12606 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12607 if (max_height > fontp->height)
74848a96 12608 fontp->height = max_height;
d5749adb 12609 }
dc43ef94 12610
2da424f1
KH
12611 if (NILP (font_names))
12612 {
12613 /* We come here because of a bug of XListFonts mentioned at
12614 the head of this block. Let's store this information in
12615 the cache for x_list_fonts. */
12616 Lisp_Object lispy_name = build_string (fontname);
12617 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12618
8e713be6 12619 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12620 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12621 Fcons (Fcons (lispy_full_name,
12622 make_number (fontp->size)),
12623 Qnil)),
8e713be6 12624 XCDR (dpyinfo->name_list_element));
2da424f1 12625 if (full_name)
8e713be6 12626 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12627 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12628 Fcons (Fcons (lispy_full_name,
12629 make_number (fontp->size)),
12630 Qnil)),
8e713be6 12631 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12632 }
12633
dc43ef94
KH
12634 /* The slot `encoding' specifies how to map a character
12635 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12636 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12637 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12638 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12639 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12640 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12641 which is never used by any charset. If mapping can't be
12642 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12643 fontp->encoding[1]
12644 = (font->max_byte1 == 0
12645 /* 1-byte font */
12646 ? (font->min_char_or_byte2 < 0x80
12647 ? (font->max_char_or_byte2 < 0x80
12648 ? 0 /* 0x20..0x7F */
8ff102bd 12649 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12650 : 1) /* 0xA0..0xFF */
12651 /* 2-byte font */
12652 : (font->min_byte1 < 0x80
12653 ? (font->max_byte1 < 0x80
12654 ? (font->min_char_or_byte2 < 0x80
12655 ? (font->max_char_or_byte2 < 0x80
12656 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12657 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12658 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12659 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12660 : (font->min_char_or_byte2 < 0x80
12661 ? (font->max_char_or_byte2 < 0x80
12662 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12663 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12664 : 1))); /* 0xA0A0..0xFFFF */
12665
12666 fontp->baseline_offset
12667 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12668 ? (long) value : 0);
12669 fontp->relative_compose
12670 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12671 ? (long) value : 0);
f78798df
KH
12672 fontp->default_ascent
12673 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12674 ? (long) value : 0);
dc43ef94 12675
06a2c219
GM
12676 /* Set global flag fonts_changed_p to non-zero if the font loaded
12677 has a character with a smaller width than any other character
12678 before, or if the font loaded has a smalle>r height than any
12679 other font loaded before. If this happens, it will make a
12680 glyph matrix reallocation necessary. */
12681 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12682 UNBLOCK_INPUT;
dc43ef94
KH
12683 return fontp;
12684 }
12685}
12686
06a2c219
GM
12687
12688/* Return a pointer to struct font_info of a font named FONTNAME for
12689 frame F. If no such font is loaded, return NULL. */
12690
dc43ef94
KH
12691struct font_info *
12692x_query_font (f, fontname)
12693 struct frame *f;
12694 register char *fontname;
12695{
12696 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12697 int i;
12698
12699 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12700 if (dpyinfo->font_table[i].name
12701 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12702 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12703 return (dpyinfo->font_table + i);
12704 return NULL;
12705}
12706
06a2c219
GM
12707
12708/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12709 `encoder' of the structure. */
12710
12711void
12712x_find_ccl_program (fontp)
12713 struct font_info *fontp;
12714{
a42f54e6 12715 Lisp_Object list, elt;
a6582676 12716
8e713be6 12717 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12718 {
8e713be6 12719 elt = XCAR (list);
a6582676 12720 if (CONSP (elt)
8e713be6
KR
12721 && STRINGP (XCAR (elt))
12722 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12723 >= 0))
a42f54e6
KH
12724 break;
12725 }
12726 if (! NILP (list))
12727 {
d27f8ca7
KH
12728 struct ccl_program *ccl
12729 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12730
8e713be6 12731 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12732 xfree (ccl);
12733 else
12734 fontp->font_encoder = ccl;
a6582676
KH
12735 }
12736}
12737
06a2c219 12738
dc43ef94 12739\f
06a2c219
GM
12740/***********************************************************************
12741 Initialization
12742 ***********************************************************************/
f451eb13 12743
3afe33e7
RS
12744#ifdef USE_X_TOOLKIT
12745static XrmOptionDescRec emacs_options[] = {
12746 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12747 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12748
12749 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12750 XrmoptionSepArg, NULL},
12751 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12752
12753 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12754 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12755 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12756 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12757 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12758 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12759 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12760};
12761#endif /* USE_X_TOOLKIT */
12762
7a13e894
RS
12763static int x_initialized;
12764
29b38361
KH
12765#ifdef MULTI_KBOARD
12766/* Test whether two display-name strings agree up to the dot that separates
12767 the screen number from the server number. */
12768static int
12769same_x_server (name1, name2)
12770 char *name1, *name2;
12771{
12772 int seen_colon = 0;
cf591cc1
RS
12773 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12774 int system_name_length = strlen (system_name);
12775 int length_until_period = 0;
12776
12777 while (system_name[length_until_period] != 0
12778 && system_name[length_until_period] != '.')
12779 length_until_period++;
12780
12781 /* Treat `unix' like an empty host name. */
12782 if (! strncmp (name1, "unix:", 5))
12783 name1 += 4;
12784 if (! strncmp (name2, "unix:", 5))
12785 name2 += 4;
12786 /* Treat this host's name like an empty host name. */
12787 if (! strncmp (name1, system_name, system_name_length)
12788 && name1[system_name_length] == ':')
12789 name1 += system_name_length;
12790 if (! strncmp (name2, system_name, system_name_length)
12791 && name2[system_name_length] == ':')
12792 name2 += system_name_length;
12793 /* Treat this host's domainless name like an empty host name. */
12794 if (! strncmp (name1, system_name, length_until_period)
12795 && name1[length_until_period] == ':')
12796 name1 += length_until_period;
12797 if (! strncmp (name2, system_name, length_until_period)
12798 && name2[length_until_period] == ':')
12799 name2 += length_until_period;
12800
29b38361
KH
12801 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12802 {
12803 if (*name1 == ':')
12804 seen_colon++;
12805 if (seen_colon && *name1 == '.')
12806 return 1;
12807 }
12808 return (seen_colon
12809 && (*name1 == '.' || *name1 == '\0')
12810 && (*name2 == '.' || *name2 == '\0'));
12811}
12812#endif
12813
334208b7 12814struct x_display_info *
1f8255f2 12815x_term_init (display_name, xrm_option, resource_name)
334208b7 12816 Lisp_Object display_name;
1f8255f2
RS
12817 char *xrm_option;
12818 char *resource_name;
dc6f92b8 12819{
334208b7 12820 int connection;
7a13e894 12821 Display *dpy;
334208b7
RS
12822 struct x_display_info *dpyinfo;
12823 XrmDatabase xrdb;
12824
60439948
KH
12825 BLOCK_INPUT;
12826
7a13e894
RS
12827 if (!x_initialized)
12828 {
12829 x_initialize ();
12830 x_initialized = 1;
12831 }
dc6f92b8 12832
3afe33e7 12833#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12834 /* weiner@footloose.sps.mot.com reports that this causes
12835 errors with X11R5:
12836 X protocol error: BadAtom (invalid Atom parameter)
12837 on protocol request 18skiloaf.
12838 So let's not use it until R6. */
12839#ifdef HAVE_X11XTR6
bdcd49ba
RS
12840 XtSetLanguageProc (NULL, NULL, NULL);
12841#endif
12842
7f9c7f94
RS
12843 {
12844 int argc = 0;
12845 char *argv[3];
12846
12847 argv[0] = "";
12848 argc = 1;
12849 if (xrm_option)
12850 {
12851 argv[argc++] = "-xrm";
12852 argv[argc++] = xrm_option;
12853 }
12854 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12855 resource_name, EMACS_CLASS,
12856 emacs_options, XtNumber (emacs_options),
12857 &argc, argv);
39d8bb4d
KH
12858
12859#ifdef HAVE_X11XTR6
10537cb1 12860 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12861 fixup_locale ();
39d8bb4d 12862#endif
7f9c7f94 12863 }
3afe33e7
RS
12864
12865#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12866#ifdef HAVE_X11R5
12867 XSetLocaleModifiers ("");
12868#endif
7a13e894 12869 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12870#endif /* not USE_X_TOOLKIT */
334208b7 12871
7a13e894
RS
12872 /* Detect failure. */
12873 if (dpy == 0)
60439948
KH
12874 {
12875 UNBLOCK_INPUT;
12876 return 0;
12877 }
7a13e894
RS
12878
12879 /* We have definitely succeeded. Record the new connection. */
12880
12881 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12882
29b38361
KH
12883#ifdef MULTI_KBOARD
12884 {
12885 struct x_display_info *share;
12886 Lisp_Object tail;
12887
12888 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12889 share = share->next, tail = XCDR (tail))
12890 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12891 XSTRING (display_name)->data))
12892 break;
12893 if (share)
12894 dpyinfo->kboard = share->kboard;
12895 else
12896 {
12897 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12898 init_kboard (dpyinfo->kboard);
59e755be
KH
12899 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12900 {
12901 char *vendor = ServerVendor (dpy);
12902 dpyinfo->kboard->Vsystem_key_alist
12903 = call1 (Qvendor_specific_keysyms,
12904 build_string (vendor ? vendor : ""));
12905 }
12906
29b38361
KH
12907 dpyinfo->kboard->next_kboard = all_kboards;
12908 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12909 /* Don't let the initial kboard remain current longer than necessary.
12910 That would cause problems if a file loaded on startup tries to
06a2c219 12911 prompt in the mini-buffer. */
0ad5446c
KH
12912 if (current_kboard == initial_kboard)
12913 current_kboard = dpyinfo->kboard;
29b38361
KH
12914 }
12915 dpyinfo->kboard->reference_count++;
12916 }
b9737ad3
KH
12917#endif
12918
7a13e894
RS
12919 /* Put this display on the chain. */
12920 dpyinfo->next = x_display_list;
12921 x_display_list = dpyinfo;
12922
12923 /* Put it on x_display_name_list as well, to keep them parallel. */
12924 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12925 x_display_name_list);
8e713be6 12926 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12927
12928 dpyinfo->display = dpy;
dc6f92b8 12929
dc6f92b8 12930#if 0
7a13e894 12931 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12932#endif /* ! 0 */
7a13e894
RS
12933
12934 dpyinfo->x_id_name
fc932ac6
RS
12935 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12936 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12937 + 2);
12938 sprintf (dpyinfo->x_id_name, "%s@%s",
12939 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12940
12941 /* Figure out which modifier bits mean what. */
334208b7 12942 x_find_modifier_meanings (dpyinfo);
f451eb13 12943
ab648270 12944 /* Get the scroll bar cursor. */
7a13e894 12945 dpyinfo->vertical_scroll_bar_cursor
334208b7 12946 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12947
334208b7
RS
12948 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12949 resource_name, EMACS_CLASS);
12950#ifdef HAVE_XRMSETDATABASE
12951 XrmSetDatabase (dpyinfo->display, xrdb);
12952#else
12953 dpyinfo->display->db = xrdb;
12954#endif
547d9db8 12955 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12956 all versions. */
12957 dpyinfo->xrdb = xrdb;
334208b7
RS
12958
12959 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12960 DefaultScreen (dpyinfo->display));
12961 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12962 &dpyinfo->n_planes);
12963 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12964 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12965 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12966 dpyinfo->grabbed = 0;
12967 dpyinfo->reference_count = 0;
12968 dpyinfo->icon_bitmap_id = -1;
06a2c219 12969 dpyinfo->font_table = NULL;
7a13e894
RS
12970 dpyinfo->n_fonts = 0;
12971 dpyinfo->font_table_size = 0;
12972 dpyinfo->bitmaps = 0;
12973 dpyinfo->bitmaps_size = 0;
12974 dpyinfo->bitmaps_last = 0;
12975 dpyinfo->scratch_cursor_gc = 0;
12976 dpyinfo->mouse_face_mouse_frame = 0;
12977 dpyinfo->mouse_face_deferred_gc = 0;
12978 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12979 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12980 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12981 dpyinfo->mouse_face_window = Qnil;
12982 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12983 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12984 dpyinfo->x_focus_frame = 0;
12985 dpyinfo->x_focus_event_frame = 0;
12986 dpyinfo->x_highlight_frame = 0;
06a2c219 12987 dpyinfo->image_cache = make_image_cache ();
334208b7 12988
06a2c219
GM
12989 {
12990 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12991 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12992 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12993 dpyinfo->resy = pixels * 25.4 / mm;
12994 pixels = DisplayWidth (dpyinfo->display, screen_number);
12995 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12996 dpyinfo->resx = pixels * 25.4 / mm;
12997 }
12998
334208b7
RS
12999 dpyinfo->Xatom_wm_protocols
13000 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13001 dpyinfo->Xatom_wm_take_focus
13002 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13003 dpyinfo->Xatom_wm_save_yourself
13004 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13005 dpyinfo->Xatom_wm_delete_window
13006 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13007 dpyinfo->Xatom_wm_change_state
13008 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13009 dpyinfo->Xatom_wm_configure_denied
13010 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13011 dpyinfo->Xatom_wm_window_moved
13012 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13013 dpyinfo->Xatom_editres
13014 = XInternAtom (dpyinfo->display, "Editres", False);
13015 dpyinfo->Xatom_CLIPBOARD
13016 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13017 dpyinfo->Xatom_TIMESTAMP
13018 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13019 dpyinfo->Xatom_TEXT
13020 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13021 dpyinfo->Xatom_COMPOUND_TEXT
13022 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13023 dpyinfo->Xatom_DELETE
13024 = XInternAtom (dpyinfo->display, "DELETE", False);
13025 dpyinfo->Xatom_MULTIPLE
13026 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13027 dpyinfo->Xatom_INCR
13028 = XInternAtom (dpyinfo->display, "INCR", False);
13029 dpyinfo->Xatom_EMACS_TMP
13030 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13031 dpyinfo->Xatom_TARGETS
13032 = XInternAtom (dpyinfo->display, "TARGETS", False);
13033 dpyinfo->Xatom_NULL
13034 = XInternAtom (dpyinfo->display, "NULL", False);
13035 dpyinfo->Xatom_ATOM_PAIR
13036 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13037 /* For properties of font. */
13038 dpyinfo->Xatom_PIXEL_SIZE
13039 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13040 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13041 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13042 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13043 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13044 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13045 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13046
06a2c219
GM
13047 /* Ghostscript support. */
13048 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13049 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13050
13051 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13052 False);
13053
547d9db8
KH
13054 dpyinfo->cut_buffers_initialized = 0;
13055
334208b7
RS
13056 connection = ConnectionNumber (dpyinfo->display);
13057 dpyinfo->connection = connection;
13058
dc43ef94 13059 {
5d7cc324
RS
13060 char null_bits[1];
13061
13062 null_bits[0] = 0x00;
dc43ef94
KH
13063
13064 dpyinfo->null_pixel
13065 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13066 null_bits, 1, 1, (long) 0, (long) 0,
13067 1);
13068 }
13069
06a2c219
GM
13070 {
13071 extern int gray_bitmap_width, gray_bitmap_height;
13072 extern unsigned char *gray_bitmap_bits;
13073 dpyinfo->gray
13074 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13075 gray_bitmap_bits,
13076 gray_bitmap_width, gray_bitmap_height,
13077 (unsigned long) 1, (unsigned long) 0, 1);
13078 }
13079
f5d11644
GM
13080#ifdef HAVE_X_I18N
13081 xim_initialize (dpyinfo, resource_name);
13082#endif
13083
87485d6f
MW
13084#ifdef subprocesses
13085 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13086 if (connection != 0)
7a13e894 13087 add_keyboard_wait_descriptor (connection);
87485d6f 13088#endif
6d4238f3 13089
041b69ac 13090#ifndef F_SETOWN_BUG
dc6f92b8 13091#ifdef F_SETOWN
dc6f92b8 13092#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13093 /* stdin is a socket here */
334208b7 13094 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13095#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13096 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13097#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13098#endif /* ! defined (F_SETOWN) */
041b69ac 13099#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13100
13101#ifdef SIGIO
eee20f6a
KH
13102 if (interrupt_input)
13103 init_sigio (connection);
c118dd06 13104#endif /* ! defined (SIGIO) */
dc6f92b8 13105
51b592fb 13106#ifdef USE_LUCID
f8c39f51 13107#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13108 /* Make sure that we have a valid font for dialog boxes
13109 so that Xt does not crash. */
13110 {
13111 Display *dpy = dpyinfo->display;
13112 XrmValue d, fr, to;
13113 Font font;
e99db5a1 13114 int count;
51b592fb
RS
13115
13116 d.addr = (XPointer)&dpy;
13117 d.size = sizeof (Display *);
13118 fr.addr = XtDefaultFont;
13119 fr.size = sizeof (XtDefaultFont);
13120 to.size = sizeof (Font *);
13121 to.addr = (XPointer)&font;
e99db5a1 13122 count = x_catch_errors (dpy);
51b592fb
RS
13123 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13124 abort ();
13125 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13126 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13127 x_uncatch_errors (dpy, count);
51b592fb
RS
13128 }
13129#endif
f8c39f51 13130#endif
51b592fb 13131
60439948
KH
13132 UNBLOCK_INPUT;
13133
7a13e894
RS
13134 return dpyinfo;
13135}
13136\f
13137/* Get rid of display DPYINFO, assuming all frames are already gone,
13138 and without sending any more commands to the X server. */
dc6f92b8 13139
7a13e894
RS
13140void
13141x_delete_display (dpyinfo)
13142 struct x_display_info *dpyinfo;
13143{
13144 delete_keyboard_wait_descriptor (dpyinfo->connection);
13145
13146 /* Discard this display from x_display_name_list and x_display_list.
13147 We can't use Fdelq because that can quit. */
13148 if (! NILP (x_display_name_list)
8e713be6
KR
13149 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13150 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13151 else
13152 {
13153 Lisp_Object tail;
13154
13155 tail = x_display_name_list;
8e713be6 13156 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13157 {
bffcfca9 13158 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13159 {
8e713be6 13160 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13161 break;
13162 }
8e713be6 13163 tail = XCDR (tail);
7a13e894
RS
13164 }
13165 }
13166
13167 if (x_display_list == dpyinfo)
13168 x_display_list = dpyinfo->next;
7f9c7f94
RS
13169 else
13170 {
13171 struct x_display_info *tail;
7a13e894 13172
7f9c7f94
RS
13173 for (tail = x_display_list; tail; tail = tail->next)
13174 if (tail->next == dpyinfo)
13175 tail->next = tail->next->next;
13176 }
7a13e894 13177
0d777288
RS
13178#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13179#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13180 XrmDestroyDatabase (dpyinfo->xrdb);
13181#endif
0d777288 13182#endif
29b38361
KH
13183#ifdef MULTI_KBOARD
13184 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13185 delete_kboard (dpyinfo->kboard);
b9737ad3 13186#endif
f5d11644
GM
13187#ifdef HAVE_X_I18N
13188 if (dpyinfo->xim)
13189 xim_close_dpy (dpyinfo);
13190#endif
13191
b9737ad3
KH
13192 xfree (dpyinfo->font_table);
13193 xfree (dpyinfo->x_id_name);
13194 xfree (dpyinfo);
7a13e894
RS
13195}
13196\f
13197/* Set up use of X before we make the first connection. */
13198
06a2c219
GM
13199static struct redisplay_interface x_redisplay_interface =
13200{
13201 x_produce_glyphs,
13202 x_write_glyphs,
13203 x_insert_glyphs,
13204 x_clear_end_of_line,
13205 x_scroll_run,
13206 x_after_update_window_line,
13207 x_update_window_begin,
13208 x_update_window_end,
13209 XTcursor_to,
13210 x_flush,
66ac4b0e
GM
13211 x_get_glyph_overhangs,
13212 x_fix_overlapping_area
06a2c219
GM
13213};
13214
dfcf069d 13215void
7a13e894
RS
13216x_initialize ()
13217{
06a2c219
GM
13218 rif = &x_redisplay_interface;
13219
13220 clear_frame_hook = x_clear_frame;
13221 ins_del_lines_hook = x_ins_del_lines;
13222 change_line_highlight_hook = x_change_line_highlight;
13223 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13224 ring_bell_hook = XTring_bell;
13225 reset_terminal_modes_hook = XTreset_terminal_modes;
13226 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13227 update_begin_hook = x_update_begin;
13228 update_end_hook = x_update_end;
dc6f92b8
JB
13229 set_terminal_window_hook = XTset_terminal_window;
13230 read_socket_hook = XTread_socket;
b8009dd1 13231 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13232 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13233 mouse_position_hook = XTmouse_position;
f451eb13 13234 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13235 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13236 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13237 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13238 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13239 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13240 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13241
f676886a 13242 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13243 char_ins_del_ok = 0; /* just as fast to write the line */
13244 line_ins_del_ok = 1; /* we'll just blt 'em */
13245 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13246 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13247 off the bottom */
13248 baud_rate = 19200;
13249
7a13e894 13250 x_noop_count = 0;
9ea173e8 13251 last_tool_bar_item = -1;
06a2c219
GM
13252 any_help_event_p = 0;
13253
b30b24cb
RS
13254 /* Try to use interrupt input; if we can't, then start polling. */
13255 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13256
7f9c7f94
RS
13257#ifdef USE_X_TOOLKIT
13258 XtToolkitInitialize ();
13259 Xt_app_con = XtCreateApplicationContext ();
665881ad 13260 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13261
13262 /* Install an asynchronous timer that processes Xt timeout events
13263 every 0.1s. This is necessary because some widget sets use
13264 timeouts internally, for example the LessTif menu bar, or the
13265 Xaw3d scroll bar. When Xt timouts aren't processed, these
13266 widgets don't behave normally. */
13267 {
13268 EMACS_TIME interval;
13269 EMACS_SET_SECS_USECS (interval, 0, 100000);
13270 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13271 }
db74249b 13272#endif
bffcfca9 13273
db74249b 13274#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13275 xaw3d_arrow_scroll = False;
13276 xaw3d_pick_top = True;
7f9c7f94
RS
13277#endif
13278
58769bee 13279 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13280 original error handler. */
e99db5a1 13281 XSetErrorHandler (x_error_handler);
334208b7 13282 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13283
06a2c219 13284 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13285#ifdef SIGWINCH
13286 signal (SIGWINCH, SIG_DFL);
c118dd06 13287#endif /* ! defined (SIGWINCH) */
dc6f92b8 13288
92e2441b 13289 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13290}
55123275 13291
06a2c219 13292
55123275
JB
13293void
13294syms_of_xterm ()
13295{
e99db5a1
RS
13296 staticpro (&x_error_message_string);
13297 x_error_message_string = Qnil;
13298
7a13e894
RS
13299 staticpro (&x_display_name_list);
13300 x_display_name_list = Qnil;
334208b7 13301
ab648270 13302 staticpro (&last_mouse_scroll_bar);
e53cb100 13303 last_mouse_scroll_bar = Qnil;
59e755be
KH
13304
13305 staticpro (&Qvendor_specific_keysyms);
13306 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13307
13308 staticpro (&last_mouse_press_frame);
13309 last_mouse_press_frame = Qnil;
06a2c219
GM
13310
13311 staticpro (&help_echo);
13312 help_echo = Qnil;
13313 staticpro (&previous_help_echo);
13314 previous_help_echo = Qnil;
13315
13316 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13317 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13318For example, if a block cursor is over a tab, it will be drawn as\n\
13319wide as that tab on the display.");
13320 x_stretch_cursor_p = 0;
13321
13322 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13323 "If not nil, Emacs uses toolkit scroll bars.");
13324#if USE_TOOLKIT_SCROLL_BARS
13325 x_toolkit_scroll_bars_p = 1;
13326#else
13327 x_toolkit_scroll_bars_p = 0;
13328#endif
13329
06a2c219
GM
13330 staticpro (&last_mouse_motion_frame);
13331 last_mouse_motion_frame = Qnil;
55123275 13332}
6cf0ae86
RS
13333
13334#endif /* not HAVE_X_WINDOWS */
06a2c219 13335