*** 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;
6c183ba5
RS
8821#ifdef HAVE_X_I18N
8822 Status status_return;
8823#endif
dc6f92b8 8824
9ac0d9e0 8825 if (interrupt_input_blocked)
dc6f92b8 8826 {
9ac0d9e0 8827 interrupt_input_pending = 1;
dc6f92b8
JB
8828 return -1;
8829 }
8830
9ac0d9e0 8831 interrupt_input_pending = 0;
dc6f92b8 8832 BLOCK_INPUT;
c0a04927
RS
8833
8834 /* So people can tell when we have read the available input. */
8835 input_signal_count++;
8836
dc6f92b8 8837 if (numchars <= 0)
06a2c219 8838 abort (); /* Don't think this happens. */
dc6f92b8 8839
7a13e894
RS
8840 /* Find the display we are supposed to read input for.
8841 It's the one communicating on descriptor SD. */
8842 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8843 {
8844#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8845#ifdef FIOSNBIO
7a13e894
RS
8846 /* If available, Xlib uses FIOSNBIO to make the socket
8847 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8848 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8849 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8850 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8851#endif /* ! defined (FIOSNBIO) */
7a13e894 8852#endif
dc6f92b8 8853
7a13e894
RS
8854#if 0 /* This code can't be made to work, with multiple displays,
8855 and appears not to be used on any system any more.
8856 Also keyboard.c doesn't turn O_NDELAY on and off
8857 for X connections. */
dc6f92b8
JB
8858#ifndef SIGIO
8859#ifndef HAVE_SELECT
7a13e894
RS
8860 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8861 {
8862 extern int read_alarm_should_throw;
8863 read_alarm_should_throw = 1;
8864 XPeekEvent (dpyinfo->display, &event);
8865 read_alarm_should_throw = 0;
8866 }
c118dd06
JB
8867#endif /* HAVE_SELECT */
8868#endif /* SIGIO */
7a13e894 8869#endif
dc6f92b8 8870
7a13e894
RS
8871 /* For debugging, this gives a way to fake an I/O error. */
8872 if (dpyinfo == XTread_socket_fake_io_error)
8873 {
8874 XTread_socket_fake_io_error = 0;
8875 x_io_error_quitter (dpyinfo->display);
8876 }
dc6f92b8 8877
06a2c219 8878 while (XPending (dpyinfo->display))
dc6f92b8 8879 {
7a13e894 8880 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8881
8882 if (display_busy_cursor_p)
8883 {
8884 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8885 display until the next X event is read and we come
8886 here again. Setting it to 1 inhibits busy-cursor
8887 display for direct commands. */
8888 if (event.type == MotionNotify
8889 || event.type == EnterNotify
8890 || (dpyinfo->grabbed
8891 && event.type != ButtonRelease))
8892 inhibit_busy_cursor = 2;
8893 else
8894 inhibit_busy_cursor = 1;
8895 }
8896
531483fb 8897#ifdef HAVE_X_I18N
d1bc4182 8898 {
f2be1146
GM
8899 /* Filter events for the current X input method.
8900 XFilterEvent returns non-zero if the input method has
8901 consumed the event. We pass the frame's X window to
8902 XFilterEvent because that's the one for which the IC
8903 was created. */
8904 struct frame *f1
8905 = x_any_window_to_frame (dpyinfo, event.xclient.window);
8906 if (f1
8907 && FRAME_XIC (f1)
8908 && XFilterEvent (&event, FRAME_X_WINDOW (f1)))
d1bc4182
RS
8909 break;
8910 }
0cd6403b 8911#endif
7a13e894
RS
8912 event_found = 1;
8913
8914 switch (event.type)
8915 {
8916 case ClientMessage:
c047688c 8917 {
7a13e894
RS
8918 if (event.xclient.message_type
8919 == dpyinfo->Xatom_wm_protocols
8920 && event.xclient.format == 32)
c047688c 8921 {
7a13e894
RS
8922 if (event.xclient.data.l[0]
8923 == dpyinfo->Xatom_wm_take_focus)
c047688c 8924 {
8c1a6a84
RS
8925 /* Use x_any_window_to_frame because this
8926 could be the shell widget window
8927 if the frame has no title bar. */
8928 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8929#ifdef HAVE_X_I18N
8930 /* Not quite sure this is needed -pd */
8c1a6a84 8931 if (f && FRAME_XIC (f))
6c183ba5
RS
8932 XSetICFocus (FRAME_XIC (f));
8933#endif
f1da8f06
GM
8934#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8935 instructs the WM to set the input focus automatically for
8936 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8937 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8938 it has set the focus. So, XSetInputFocus below is not
8939 needed.
8940
8941 The call to XSetInputFocus below has also caused trouble. In
8942 cases where the XSetInputFocus done by the WM and the one
8943 below are temporally close (on a fast machine), the call
8944 below can generate additional FocusIn events which confuse
8945 Emacs. */
8946
bf7253f4
RS
8947 /* Since we set WM_TAKE_FOCUS, we must call
8948 XSetInputFocus explicitly. But not if f is null,
8949 since that might be an event for a deleted frame. */
7a13e894 8950 if (f)
bf7253f4
RS
8951 {
8952 Display *d = event.xclient.display;
8953 /* Catch and ignore errors, in case window has been
8954 iconified by a window manager such as GWM. */
8955 int count = x_catch_errors (d);
8956 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8957 /* The ICCCM says this is
8958 the only valid choice. */
8959 RevertToParent,
bf7253f4
RS
8960 event.xclient.data.l[1]);
8961 /* This is needed to detect the error
8962 if there is an error. */
8963 XSync (d, False);
8964 x_uncatch_errors (d, count);
8965 }
7a13e894 8966 /* Not certain about handling scroll bars here */
f1da8f06 8967#endif /* 0 */
c047688c 8968 }
7a13e894
RS
8969 else if (event.xclient.data.l[0]
8970 == dpyinfo->Xatom_wm_save_yourself)
8971 {
8972 /* Save state modify the WM_COMMAND property to
06a2c219 8973 something which can reinstate us. This notifies
7a13e894
RS
8974 the session manager, who's looking for such a
8975 PropertyNotify. Can restart processing when
06a2c219 8976 a keyboard or mouse event arrives. */
7a13e894
RS
8977 if (numchars > 0)
8978 {
19126e11
KH
8979 f = x_top_window_to_frame (dpyinfo,
8980 event.xclient.window);
7a13e894
RS
8981
8982 /* This is just so we only give real data once
8983 for a single Emacs process. */
b86bd3dd 8984 if (f == SELECTED_FRAME ())
7a13e894
RS
8985 XSetCommand (FRAME_X_DISPLAY (f),
8986 event.xclient.window,
8987 initial_argv, initial_argc);
f000f5c5 8988 else if (f)
7a13e894
RS
8989 XSetCommand (FRAME_X_DISPLAY (f),
8990 event.xclient.window,
8991 0, 0);
8992 }
8993 }
8994 else if (event.xclient.data.l[0]
8995 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8996 {
19126e11
KH
8997 struct frame *f
8998 = x_any_window_to_frame (dpyinfo,
8999 event.xclient.window);
1fb20991 9000
7a13e894
RS
9001 if (f)
9002 {
9003 if (numchars == 0)
9004 abort ();
1fb20991 9005
7a13e894
RS
9006 bufp->kind = delete_window_event;
9007 XSETFRAME (bufp->frame_or_window, f);
9008 bufp++;
9009
9010 count += 1;
9011 numchars -= 1;
9012 }
1fb20991 9013 }
c047688c 9014 }
7a13e894
RS
9015 else if (event.xclient.message_type
9016 == dpyinfo->Xatom_wm_configure_denied)
9017 {
9018 }
9019 else if (event.xclient.message_type
9020 == dpyinfo->Xatom_wm_window_moved)
9021 {
9022 int new_x, new_y;
19126e11
KH
9023 struct frame *f
9024 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9025
7a13e894
RS
9026 new_x = event.xclient.data.s[0];
9027 new_y = event.xclient.data.s[1];
1fb20991 9028
7a13e894
RS
9029 if (f)
9030 {
7556890b
RS
9031 f->output_data.x->left_pos = new_x;
9032 f->output_data.x->top_pos = new_y;
7a13e894 9033 }
1fb20991 9034 }
0fdff6bb 9035#ifdef HACK_EDITRES
7a13e894
RS
9036 else if (event.xclient.message_type
9037 == dpyinfo->Xatom_editres)
9038 {
19126e11
KH
9039 struct frame *f
9040 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9041 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9042 &event, NULL);
7a13e894 9043 }
0fdff6bb 9044#endif /* HACK_EDITRES */
06a2c219
GM
9045 else if ((event.xclient.message_type
9046 == dpyinfo->Xatom_DONE)
9047 || (event.xclient.message_type
9048 == dpyinfo->Xatom_PAGE))
9049 {
9050 /* Ghostview job completed. Kill it. We could
9051 reply with "Next" if we received "Page", but we
9052 currently never do because we are interested in
9053 images, only, which should have 1 page. */
06a2c219
GM
9054 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9055 struct frame *f
9056 = x_window_to_frame (dpyinfo, event.xclient.window);
9057 x_kill_gs_process (pixmap, f);
9058 expose_frame (f, 0, 0, 0, 0);
9059 }
9060#ifdef USE_TOOLKIT_SCROLL_BARS
9061 /* Scroll bar callbacks send a ClientMessage from which
9062 we construct an input_event. */
9063 else if (event.xclient.message_type
9064 == dpyinfo->Xatom_Scrollbar)
9065 {
f9632fc8
GM
9066 if (display_busy_cursor_p)
9067 inhibit_busy_cursor = 2;
06a2c219
GM
9068 x_scroll_bar_to_input_event (&event, bufp);
9069 ++bufp, ++count, --numchars;
9070 goto out;
9071 }
9072#endif /* USE_TOOLKIT_SCROLL_BARS */
9073 else
9074 goto OTHER;
7a13e894
RS
9075 }
9076 break;
dc6f92b8 9077
7a13e894 9078 case SelectionNotify:
3afe33e7 9079#ifdef USE_X_TOOLKIT
19126e11 9080 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9081 goto OTHER;
3afe33e7 9082#endif /* not USE_X_TOOLKIT */
dfcf069d 9083 x_handle_selection_notify (&event.xselection);
7a13e894 9084 break;
d56a553a 9085
06a2c219 9086 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9087#ifdef USE_X_TOOLKIT
19126e11 9088 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9089 goto OTHER;
3afe33e7 9090#endif /* USE_X_TOOLKIT */
7a13e894
RS
9091 {
9092 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9093
7a13e894
RS
9094 if (numchars == 0)
9095 abort ();
d56a553a 9096
7a13e894
RS
9097 bufp->kind = selection_clear_event;
9098 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9099 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9100 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9101 bufp->frame_or_window = Qnil;
7a13e894 9102 bufp++;
d56a553a 9103
7a13e894
RS
9104 count += 1;
9105 numchars -= 1;
9106 }
9107 break;
dc6f92b8 9108
06a2c219 9109 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9110#ifdef USE_X_TOOLKIT
19126e11 9111 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9112 goto OTHER;
3afe33e7 9113#endif /* USE_X_TOOLKIT */
7a13e894 9114 if (x_queue_selection_requests)
19126e11 9115 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9116 &event);
9117 else
9118 {
9119 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9120
7a13e894
RS
9121 if (numchars == 0)
9122 abort ();
9123
9124 bufp->kind = selection_request_event;
9125 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9126 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9127 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9128 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9129 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9130 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9131 bufp->frame_or_window = Qnil;
7a13e894
RS
9132 bufp++;
9133
9134 count += 1;
9135 numchars -= 1;
9136 }
9137 break;
9138
9139 case PropertyNotify:
3afe33e7 9140#ifdef USE_X_TOOLKIT
19126e11 9141 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9142 goto OTHER;
3afe33e7 9143#endif /* not USE_X_TOOLKIT */
dfcf069d 9144 x_handle_property_notify (&event.xproperty);
7a13e894 9145 break;
dc6f92b8 9146
7a13e894 9147 case ReparentNotify:
19126e11 9148 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9149 if (f)
9150 {
9151 int x, y;
7556890b 9152 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9153 x_real_positions (f, &x, &y);
7556890b
RS
9154 f->output_data.x->left_pos = x;
9155 f->output_data.x->top_pos = y;
7a13e894
RS
9156 }
9157 break;
3bd330d4 9158
7a13e894 9159 case Expose:
19126e11 9160 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9161 if (f)
dc6f92b8 9162 {
7a13e894
RS
9163 if (f->async_visible == 0)
9164 {
9165 f->async_visible = 1;
9166 f->async_iconified = 0;
06c488fd 9167 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9168 SET_FRAME_GARBAGED (f);
9169 }
9170 else
06a2c219
GM
9171 expose_frame (x_window_to_frame (dpyinfo,
9172 event.xexpose.window),
9173 event.xexpose.x, event.xexpose.y,
9174 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9175 }
9176 else
7a13e894 9177 {
06a2c219
GM
9178#ifdef USE_TOOLKIT_SCROLL_BARS
9179 /* Dispatch event to the widget. */
9180 goto OTHER;
9181#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9182 struct scroll_bar *bar
9183 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9184
7a13e894
RS
9185 if (bar)
9186 x_scroll_bar_expose (bar, &event);
3afe33e7 9187#ifdef USE_X_TOOLKIT
7a13e894
RS
9188 else
9189 goto OTHER;
3afe33e7 9190#endif /* USE_X_TOOLKIT */
06a2c219 9191#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9192 }
9193 break;
dc6f92b8 9194
7a13e894
RS
9195 case GraphicsExpose: /* This occurs when an XCopyArea's
9196 source area was obscured or not
9197 available.*/
19126e11 9198 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9199 if (f)
9200 {
06a2c219
GM
9201 expose_frame (f,
9202 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9203 event.xgraphicsexpose.width,
9204 event.xgraphicsexpose.height);
7a13e894 9205 }
3afe33e7 9206#ifdef USE_X_TOOLKIT
7a13e894
RS
9207 else
9208 goto OTHER;
3afe33e7 9209#endif /* USE_X_TOOLKIT */
7a13e894 9210 break;
dc6f92b8 9211
7a13e894 9212 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9213 source area was completely
9214 available */
7a13e894 9215 break;
dc6f92b8 9216
7a13e894 9217 case UnmapNotify:
06a2c219
GM
9218 /* Redo the mouse-highlight after the tooltip has gone. */
9219 if (event.xmap.window == tip_window)
9220 {
9221 tip_window = 0;
9222 redo_mouse_highlight ();
9223 }
9224
91ea2a7a 9225 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9226 if (f) /* F may no longer exist if
9227 the frame was deleted. */
9228 {
9229 /* While a frame is unmapped, display generation is
9230 disabled; you don't want to spend time updating a
9231 display that won't ever be seen. */
9232 f->async_visible = 0;
9233 /* We can't distinguish, from the event, whether the window
9234 has become iconified or invisible. So assume, if it
9235 was previously visible, than now it is iconified.
1aa6072f
RS
9236 But x_make_frame_invisible clears both
9237 the visible flag and the iconified flag;
9238 and that way, we know the window is not iconified now. */
7a13e894 9239 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9240 {
9241 f->async_iconified = 1;
bddd097c 9242
1aa6072f
RS
9243 bufp->kind = iconify_event;
9244 XSETFRAME (bufp->frame_or_window, f);
9245 bufp++;
9246 count++;
9247 numchars--;
9248 }
7a13e894 9249 }
7a13e894 9250 goto OTHER;
dc6f92b8 9251
7a13e894 9252 case MapNotify:
06a2c219
GM
9253 if (event.xmap.window == tip_window)
9254 /* The tooltip has been drawn already. Avoid
9255 the SET_FRAME_GARBAGED below. */
9256 goto OTHER;
9257
9258 /* We use x_top_window_to_frame because map events can
9259 come for sub-windows and they don't mean that the
9260 frame is visible. */
19126e11 9261 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9262 if (f)
9263 {
9264 f->async_visible = 1;
9265 f->async_iconified = 0;
06c488fd 9266 f->output_data.x->has_been_visible = 1;
dc6f92b8 9267
7a13e894
RS
9268 /* wait_reading_process_input will notice this and update
9269 the frame's display structures. */
9270 SET_FRAME_GARBAGED (f);
bddd097c 9271
d806e720
RS
9272 if (f->iconified)
9273 {
9274 bufp->kind = deiconify_event;
9275 XSETFRAME (bufp->frame_or_window, f);
9276 bufp++;
9277 count++;
9278 numchars--;
9279 }
e73ec6fa 9280 else if (! NILP (Vframe_list)
8e713be6 9281 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9282 /* Force a redisplay sooner or later
9283 to update the frame titles
9284 in case this is the second frame. */
9285 record_asynch_buffer_change ();
7a13e894 9286 }
7a13e894 9287 goto OTHER;
dc6f92b8 9288
7a13e894 9289 case KeyPress:
19126e11 9290 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9291
06a2c219
GM
9292#ifdef USE_MOTIF
9293 /* I couldn't find a way to prevent LessTif scroll bars
9294 from consuming key events. */
9295 if (f == 0)
9296 {
9297 Widget widget = XtWindowToWidget (dpyinfo->display,
9298 event.xkey.window);
9299 if (widget && XmIsScrollBar (widget))
9300 {
9301 widget = XtParent (widget);
9302 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9303 }
9304 }
9305#endif /* USE_MOTIF */
9306
7a13e894
RS
9307 if (f != 0)
9308 {
9309 KeySym keysym, orig_keysym;
9310 /* al%imercury@uunet.uu.net says that making this 81 instead of
9311 80 fixed a bug whereby meta chars made his Emacs hang. */
9312 unsigned char copy_buffer[81];
9313 int modifiers;
64bb1782 9314
7a13e894
RS
9315 event.xkey.state
9316 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9317 extra_keyboard_modifiers);
9318 modifiers = event.xkey.state;
3a2712f9 9319
7a13e894 9320 /* This will have to go some day... */
752a043f 9321
7a13e894
RS
9322 /* make_lispy_event turns chars into control chars.
9323 Don't do it here because XLookupString is too eager. */
9324 event.xkey.state &= ~ControlMask;
5d46f928
RS
9325 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9326 | dpyinfo->super_mod_mask
9327 | dpyinfo->hyper_mod_mask
9328 | dpyinfo->alt_mod_mask);
9329
1cf4a0d1
RS
9330 /* In case Meta is ComposeCharacter,
9331 clear its status. According to Markus Ehrnsperger
9332 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9333 this enables ComposeCharacter to work whether or
9334 not it is combined with Meta. */
9335 if (modifiers & dpyinfo->meta_mod_mask)
9336 bzero (&compose_status, sizeof (compose_status));
9337
6c183ba5
RS
9338#ifdef HAVE_X_I18N
9339 if (FRAME_XIC (f))
9340 {
6c183ba5
RS
9341 nbytes = XmbLookupString (FRAME_XIC (f),
9342 &event.xkey, copy_buffer,
9343 80, &keysym,
9344 &status_return);
1decb680
PE
9345 if (status_return == XLookupNone)
9346 break;
9347 else if (status_return == XLookupChars)
9348 keysym = NoSymbol;
9349 else if (status_return != XLookupKeySym
9350 && status_return != XLookupBoth)
9351 abort ();
6c183ba5
RS
9352 }
9353 else
9354 nbytes = XLookupString (&event.xkey, copy_buffer,
9355 80, &keysym, &compose_status);
9356#else
0299d313
RS
9357 nbytes = XLookupString (&event.xkey, copy_buffer,
9358 80, &keysym, &compose_status);
6c183ba5 9359#endif
dc6f92b8 9360
7a13e894 9361 orig_keysym = keysym;
55123275 9362
7a13e894
RS
9363 if (numchars > 1)
9364 {
9365 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9366 || keysym == XK_Delete
1097aea0 9367#ifdef XK_ISO_Left_Tab
441affdb 9368 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9369#endif
852bff8f 9370 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9371 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9372 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9373#ifdef HPUX
7a13e894
RS
9374 /* This recognizes the "extended function keys".
9375 It seems there's no cleaner way.
9376 Test IsModifierKey to avoid handling mode_switch
9377 incorrectly. */
9378 || ((unsigned) (keysym) >= XK_Select
9379 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9380#endif
9381#ifdef XK_dead_circumflex
7a13e894 9382 || orig_keysym == XK_dead_circumflex
69388238
RS
9383#endif
9384#ifdef XK_dead_grave
7a13e894 9385 || orig_keysym == XK_dead_grave
69388238
RS
9386#endif
9387#ifdef XK_dead_tilde
7a13e894 9388 || orig_keysym == XK_dead_tilde
69388238
RS
9389#endif
9390#ifdef XK_dead_diaeresis
7a13e894 9391 || orig_keysym == XK_dead_diaeresis
69388238
RS
9392#endif
9393#ifdef XK_dead_macron
7a13e894 9394 || orig_keysym == XK_dead_macron
69388238
RS
9395#endif
9396#ifdef XK_dead_degree
7a13e894 9397 || orig_keysym == XK_dead_degree
69388238
RS
9398#endif
9399#ifdef XK_dead_acute
7a13e894 9400 || orig_keysym == XK_dead_acute
69388238
RS
9401#endif
9402#ifdef XK_dead_cedilla
7a13e894 9403 || orig_keysym == XK_dead_cedilla
69388238
RS
9404#endif
9405#ifdef XK_dead_breve
7a13e894 9406 || orig_keysym == XK_dead_breve
69388238
RS
9407#endif
9408#ifdef XK_dead_ogonek
7a13e894 9409 || orig_keysym == XK_dead_ogonek
69388238
RS
9410#endif
9411#ifdef XK_dead_caron
7a13e894 9412 || orig_keysym == XK_dead_caron
69388238
RS
9413#endif
9414#ifdef XK_dead_doubleacute
7a13e894 9415 || orig_keysym == XK_dead_doubleacute
69388238
RS
9416#endif
9417#ifdef XK_dead_abovedot
7a13e894 9418 || orig_keysym == XK_dead_abovedot
c34790e0 9419#endif
7a13e894
RS
9420 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9421 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9422 /* Any "vendor-specific" key is ok. */
9423 || (orig_keysym & (1 << 28)))
9424 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9425#ifndef HAVE_X11R5
9426#ifdef XK_Mode_switch
7a13e894 9427 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9428#endif
9429#ifdef XK_Num_Lock
7a13e894 9430 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9431#endif
9432#endif /* not HAVE_X11R5 */
7a13e894 9433 ))
dc6f92b8 9434 {
10e6549c
RS
9435 if (temp_index == sizeof temp_buffer / sizeof (short))
9436 temp_index = 0;
7a13e894
RS
9437 temp_buffer[temp_index++] = keysym;
9438 bufp->kind = non_ascii_keystroke;
9439 bufp->code = keysym;
e0c1aef2 9440 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9441 bufp->modifiers
9442 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9443 modifiers);
1113d9db 9444 bufp->timestamp = event.xkey.time;
dc6f92b8 9445 bufp++;
7a13e894
RS
9446 count++;
9447 numchars--;
06a2c219
GM
9448
9449 if (display_busy_cursor_p)
9450 if (keysym != XK_Return || minibuf_level == 0)
9451 inhibit_busy_cursor = 2;
dc6f92b8 9452 }
7a13e894
RS
9453 else if (numchars > nbytes)
9454 {
9455 register int i;
9456
9457 for (i = 0; i < nbytes; i++)
9458 {
9459 if (temp_index == sizeof temp_buffer / sizeof (short))
9460 temp_index = 0;
9461 temp_buffer[temp_index++] = copy_buffer[i];
9462 bufp->kind = ascii_keystroke;
9463 bufp->code = copy_buffer[i];
9464 XSETFRAME (bufp->frame_or_window, f);
9465 bufp->modifiers
9466 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9467 modifiers);
9468 bufp->timestamp = event.xkey.time;
9469 bufp++;
9470 }
9471
9472 count += nbytes;
9473 numchars -= nbytes;
1decb680
PE
9474
9475 if (keysym == NoSymbol)
9476 break;
7a13e894
RS
9477 }
9478 else
9479 abort ();
dc6f92b8 9480 }
10e6549c
RS
9481 else
9482 abort ();
dc6f92b8 9483 }
717ca130 9484 goto OTHER;
f451eb13 9485
7a13e894 9486 /* Here's a possible interpretation of the whole
06a2c219
GM
9487 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9488 you get a FocusIn event, you have to get a FocusOut
9489 event before you relinquish the focus. If you
9490 haven't received a FocusIn event, then a mere
9491 LeaveNotify is enough to free you. */
f451eb13 9492
7a13e894 9493 case EnterNotify:
06a2c219
GM
9494 {
9495 int from_menu_bar_p = 0;
9496
9497 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9498
9499#ifdef LESSTIF_VERSION
9500 /* When clicking outside of a menu bar popup to close
9501 it, we get a FocusIn/ EnterNotify sequence of
9502 events. The flag event.xcrossing.focus is not set
9503 in the EnterNotify event of that sequence because
9504 the focus is in the menu bar,
9505 event.xcrossing.window is the frame's X window.
9506 Unconditionally setting the focus frame to null in
9507 this case is not the right thing, because no event
9508 follows that could set the focus frame to the right
9509 value.
9510
9511 This could be a LessTif bug, but I wasn't able to
9512 reproduce the behavior in a simple test program.
9513
9514 (gerd, LessTif 0.88.1). */
9515
9516 if (!event.xcrossing.focus
9517 && f
9518 && f->output_data.x->menubar_widget)
9519 {
9520 Window focus;
9521 int revert;
9522
9523 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9524 if (focus == XtWindow (f->output_data.x->menubar_widget))
9525 from_menu_bar_p = 1;
9526 }
9527#endif /* LESSTIF_VERSION */
6d4238f3 9528
06a2c219
GM
9529 if (event.xcrossing.focus || from_menu_bar_p)
9530 {
9531 /* Avoid nasty pop/raise loops. */
9532 if (f && (!(f->auto_raise)
9533 || !(f->auto_lower)
9534 || (event.xcrossing.time - enter_timestamp) > 500))
9535 {
9536 x_new_focus_frame (dpyinfo, f);
9537 enter_timestamp = event.xcrossing.time;
9538 }
9539 }
9540 else if (f == dpyinfo->x_focus_frame)
9541 x_new_focus_frame (dpyinfo, 0);
9542
9543 /* EnterNotify counts as mouse movement,
9544 so update things that depend on mouse position. */
9545 if (f && !f->output_data.x->busy_p)
9546 note_mouse_movement (f, &event.xmotion);
9547 goto OTHER;
9548 }
dc6f92b8 9549
7a13e894 9550 case FocusIn:
19126e11 9551 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9552 if (event.xfocus.detail != NotifyPointer)
0f941935 9553 dpyinfo->x_focus_event_frame = f;
7a13e894 9554 if (f)
0f941935 9555 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9556
6c183ba5
RS
9557#ifdef HAVE_X_I18N
9558 if (f && FRAME_XIC (f))
9559 XSetICFocus (FRAME_XIC (f));
9560#endif
9561
7a13e894 9562 goto OTHER;
10c5e63d 9563
7a13e894 9564 case LeaveNotify:
19126e11 9565 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9566 if (f)
10c5e63d 9567 {
06a2c219
GM
9568 Lisp_Object frame;
9569 int from_menu_bar_p = 0;
9570
7a13e894 9571 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9572 {
9573 /* If we move outside the frame, then we're
9574 certainly no longer on any text in the frame. */
9575 clear_mouse_face (dpyinfo);
9576 dpyinfo->mouse_face_mouse_frame = 0;
9577 }
9578
9579 /* Generate a nil HELP_EVENT to cancel a help-echo.
9580 Do it only if there's something to cancel.
9581 Otherwise, the startup message is cleared when
9582 the mouse leaves the frame. */
9583 if (any_help_event_p)
9584 {
9585 XSETFRAME (frame, f);
9586 bufp->kind = HELP_EVENT;
9587 bufp->frame_or_window = Fcons (frame, Qnil);
9588 ++bufp, ++count, --numchars;
9589 }
7a13e894 9590
06a2c219
GM
9591#ifdef LESSTIF_VERSION
9592 /* Please see the comment at the start of the
9593 EnterNotify case. */
9594 if (!event.xcrossing.focus
9595 && f->output_data.x->menubar_widget)
9596 {
9597 Window focus;
9598 int revert;
9599 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9600 if (focus == XtWindow (f->output_data.x->menubar_widget))
9601 from_menu_bar_p = 1;
9602 }
9603#endif /* LESSTIF_VERSION */
9604
9605 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9606 x_mouse_leave (dpyinfo);
10c5e63d 9607 else
7a13e894 9608 {
0f941935
KH
9609 if (f == dpyinfo->x_focus_event_frame)
9610 dpyinfo->x_focus_event_frame = 0;
9611 if (f == dpyinfo->x_focus_frame)
9612 x_new_focus_frame (dpyinfo, 0);
7a13e894 9613 }
10c5e63d 9614 }
7a13e894 9615 goto OTHER;
dc6f92b8 9616
7a13e894 9617 case FocusOut:
19126e11 9618 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9619 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9620 && f == dpyinfo->x_focus_event_frame)
9621 dpyinfo->x_focus_event_frame = 0;
9622 if (f && f == dpyinfo->x_focus_frame)
9623 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9624
6c183ba5
RS
9625#ifdef HAVE_X_I18N
9626 if (f && FRAME_XIC (f))
9627 XUnsetICFocus (FRAME_XIC (f));
9628#endif
9629
7a13e894 9630 goto OTHER;
dc6f92b8 9631
7a13e894 9632 case MotionNotify:
dc6f92b8 9633 {
06a2c219
GM
9634 previous_help_echo = help_echo;
9635 help_echo = Qnil;
9636
7a13e894
RS
9637 if (dpyinfo->grabbed && last_mouse_frame
9638 && FRAME_LIVE_P (last_mouse_frame))
9639 f = last_mouse_frame;
9640 else
19126e11 9641 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9642
7a13e894
RS
9643 if (f)
9644 note_mouse_movement (f, &event.xmotion);
9645 else
9646 {
e88b3c50 9647#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9648 struct scroll_bar *bar
9649 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9650
7a13e894
RS
9651 if (bar)
9652 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9653#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9654
06a2c219
GM
9655 /* If we move outside the frame, then we're
9656 certainly no longer on any text in the frame. */
7a13e894
RS
9657 clear_mouse_face (dpyinfo);
9658 }
06a2c219
GM
9659
9660 /* If the contents of the global variable help_echo
9661 has changed, generate a HELP_EVENT. */
9662 if (STRINGP (help_echo)
9663 || STRINGP (previous_help_echo))
9664 {
9665 Lisp_Object frame;
9666
9667 if (f)
9668 XSETFRAME (frame, f);
9669 else
9670 frame = Qnil;
9671
9672 any_help_event_p = 1;
9673 bufp->kind = HELP_EVENT;
9674 bufp->frame_or_window = Fcons (frame, help_echo);
9675 ++bufp, ++count, --numchars;
9676 }
9677
9678 goto OTHER;
dc6f92b8 9679 }
dc6f92b8 9680
7a13e894 9681 case ConfigureNotify:
9829ddba
RS
9682 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9683 if (f)
af395ec1 9684 {
5c187dee 9685#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9686 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9687 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9688
2d7fc7e8
RS
9689 /* In the toolkit version, change_frame_size
9690 is called by the code that handles resizing
9691 of the EmacsFrame widget. */
7a13e894 9692
7a13e894
RS
9693 /* Even if the number of character rows and columns has
9694 not changed, the font size may have changed, so we need
9695 to check the pixel dimensions as well. */
9696 if (columns != f->width
9697 || rows != f->height
7556890b
RS
9698 || event.xconfigure.width != f->output_data.x->pixel_width
9699 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9700 {
7d1e984f 9701 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9702 SET_FRAME_GARBAGED (f);
e687d06e 9703 cancel_mouse_face (f);
7a13e894 9704 }
2d7fc7e8 9705#endif
af395ec1 9706
7556890b
RS
9707 f->output_data.x->pixel_width = event.xconfigure.width;
9708 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9709
9710 /* What we have now is the position of Emacs's own window.
9711 Convert that to the position of the window manager window. */
dcb07ae9
RS
9712 x_real_positions (f, &f->output_data.x->left_pos,
9713 &f->output_data.x->top_pos);
9714
9715 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9716 {
9717 /* Since the WM decorations come below top_pos now,
9718 we must put them below top_pos in the future. */
9719 f->output_data.x->win_gravity = NorthWestGravity;
9720 x_wm_set_size_hint (f, (long) 0, 0);
9721 }
8f08dc93
KH
9722#ifdef USE_MOTIF
9723 /* Some window managers pass (0,0) as the location of
9724 the window, and the Motif event handler stores it
9725 in the emacs widget, which messes up Motif menus. */
9726 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9727 {
9728 event.xconfigure.x = f->output_data.x->widget->core.x;
9729 event.xconfigure.y = f->output_data.x->widget->core.y;
9730 }
06a2c219 9731#endif /* USE_MOTIF */
7a13e894 9732 }
2d7fc7e8 9733 goto OTHER;
dc6f92b8 9734
7a13e894
RS
9735 case ButtonPress:
9736 case ButtonRelease:
9737 {
9738 /* If we decide we want to generate an event to be seen
9739 by the rest of Emacs, we put it here. */
9740 struct input_event emacs_event;
9ea173e8 9741 int tool_bar_p = 0;
06a2c219 9742
7a13e894 9743 emacs_event.kind = no_event;
7a13e894 9744 bzero (&compose_status, sizeof (compose_status));
9b07615b 9745
06a2c219
GM
9746 if (dpyinfo->grabbed
9747 && last_mouse_frame
9f67f20b
RS
9748 && FRAME_LIVE_P (last_mouse_frame))
9749 f = last_mouse_frame;
9750 else
2224b905 9751 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9752
06a2c219
GM
9753 if (f)
9754 {
9ea173e8
GM
9755 /* Is this in the tool-bar? */
9756 if (WINDOWP (f->tool_bar_window)
9757 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9758 {
9759 Lisp_Object window;
9760 int p, x, y;
9761
9762 x = event.xbutton.x;
9763 y = event.xbutton.y;
9764
9765 /* Set x and y. */
9766 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9767 if (EQ (window, f->tool_bar_window))
06a2c219 9768 {
9ea173e8
GM
9769 x_handle_tool_bar_click (f, &event.xbutton);
9770 tool_bar_p = 1;
06a2c219
GM
9771 }
9772 }
9773
9ea173e8 9774 if (!tool_bar_p)
06a2c219
GM
9775 if (!dpyinfo->x_focus_frame
9776 || f == dpyinfo->x_focus_frame)
9777 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9778 }
9779 else
9780 {
06a2c219 9781#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9782 struct scroll_bar *bar
9783 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9784
7a13e894
RS
9785 if (bar)
9786 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9787#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9788 }
9789
9790 if (event.type == ButtonPress)
9791 {
9792 dpyinfo->grabbed |= (1 << event.xbutton.button);
9793 last_mouse_frame = f;
edad46f6
KH
9794 /* Ignore any mouse motion that happened
9795 before this event; any subsequent mouse-movement
9796 Emacs events should reflect only motion after
9797 the ButtonPress. */
a00e91cd
KH
9798 if (f != 0)
9799 f->mouse_moved = 0;
06a2c219 9800
9ea173e8
GM
9801 if (!tool_bar_p)
9802 last_tool_bar_item = -1;
06a2c219
GM
9803 if (display_busy_cursor_p)
9804 inhibit_busy_cursor = 2;
7a13e894 9805 }
3afe33e7
RS
9806 else
9807 {
7a13e894 9808 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9809 }
23faf38f 9810
7a13e894
RS
9811 if (numchars >= 1 && emacs_event.kind != no_event)
9812 {
9813 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9814 bufp++;
9815 count++;
9816 numchars--;
9817 }
3afe33e7
RS
9818
9819#ifdef USE_X_TOOLKIT
2224b905
RS
9820 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9821 /* For a down-event in the menu bar,
9822 don't pass it to Xt right now.
9823 Instead, save it away
9824 and we will pass it to Xt from kbd_buffer_get_event.
9825 That way, we can run some Lisp code first. */
91375f8f
RS
9826 if (f && event.type == ButtonPress
9827 /* Verify the event is really within the menu bar
9828 and not just sent to it due to grabbing. */
9829 && event.xbutton.x >= 0
9830 && event.xbutton.x < f->output_data.x->pixel_width
9831 && event.xbutton.y >= 0
9832 && event.xbutton.y < f->output_data.x->menubar_height
9833 && event.xbutton.same_screen)
2224b905 9834 {
8805890a 9835 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9836 XSETFRAME (last_mouse_press_frame, f);
9837 }
9838 else if (event.type == ButtonPress)
9839 {
9840 last_mouse_press_frame = Qnil;
30e671c3 9841 goto OTHER;
ce89ef46 9842 }
06a2c219 9843
2237cac9
RS
9844#ifdef USE_MOTIF /* This should do not harm for Lucid,
9845 but I am trying to be cautious. */
ce89ef46
RS
9846 else if (event.type == ButtonRelease)
9847 {
2237cac9 9848 if (!NILP (last_mouse_press_frame))
f10ded1c 9849 {
2237cac9
RS
9850 f = XFRAME (last_mouse_press_frame);
9851 if (f->output_data.x)
06a2c219 9852 SET_SAVED_BUTTON_EVENT;
f10ded1c 9853 }
06a2c219 9854 else
30e671c3 9855 goto OTHER;
2224b905 9856 }
2237cac9 9857#endif /* USE_MOTIF */
2224b905
RS
9858 else
9859 goto OTHER;
3afe33e7 9860#endif /* USE_X_TOOLKIT */
7a13e894
RS
9861 }
9862 break;
dc6f92b8 9863
7a13e894 9864 case CirculateNotify:
06a2c219
GM
9865 goto OTHER;
9866
7a13e894 9867 case CirculateRequest:
06a2c219
GM
9868 goto OTHER;
9869
9870 case VisibilityNotify:
9871 goto OTHER;
dc6f92b8 9872
7a13e894
RS
9873 case MappingNotify:
9874 /* Someone has changed the keyboard mapping - update the
9875 local cache. */
9876 switch (event.xmapping.request)
9877 {
9878 case MappingModifier:
9879 x_find_modifier_meanings (dpyinfo);
9880 /* This is meant to fall through. */
9881 case MappingKeyboard:
9882 XRefreshKeyboardMapping (&event.xmapping);
9883 }
7a13e894 9884 goto OTHER;
dc6f92b8 9885
7a13e894 9886 default:
7a13e894 9887 OTHER:
717ca130 9888#ifdef USE_X_TOOLKIT
7a13e894
RS
9889 BLOCK_INPUT;
9890 XtDispatchEvent (&event);
9891 UNBLOCK_INPUT;
3afe33e7 9892#endif /* USE_X_TOOLKIT */
7a13e894
RS
9893 break;
9894 }
dc6f92b8
JB
9895 }
9896 }
9897
06a2c219
GM
9898 out:;
9899
9a5196d0
RS
9900 /* On some systems, an X bug causes Emacs to get no more events
9901 when the window is destroyed. Detect that. (1994.) */
58769bee 9902 if (! event_found)
ef2a22d0 9903 {
ef2a22d0
RS
9904 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9905 One XNOOP in 100 loops will make Emacs terminate.
9906 B. Bretthauer, 1994 */
9907 x_noop_count++;
58769bee 9908 if (x_noop_count >= 100)
ef2a22d0
RS
9909 {
9910 x_noop_count=0;
2224b905
RS
9911
9912 if (next_noop_dpyinfo == 0)
9913 next_noop_dpyinfo = x_display_list;
9914
9915 XNoOp (next_noop_dpyinfo->display);
9916
9917 /* Each time we get here, cycle through the displays now open. */
9918 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9919 }
9920 }
502add23 9921
06a2c219 9922 /* If the focus was just given to an auto-raising frame,
0134a210 9923 raise it now. */
7a13e894 9924 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9925 if (pending_autoraise_frame)
9926 {
9927 x_raise_frame (pending_autoraise_frame);
9928 pending_autoraise_frame = 0;
9929 }
0134a210 9930
dc6f92b8
JB
9931 UNBLOCK_INPUT;
9932 return count;
9933}
06a2c219
GM
9934
9935
9936
dc6f92b8 9937\f
06a2c219
GM
9938/***********************************************************************
9939 Text Cursor
9940 ***********************************************************************/
9941
9942/* Note if the text cursor of window W has been overwritten by a
9943 drawing operation that outputs N glyphs starting at HPOS in the
9944 line given by output_cursor.vpos. N < 0 means all the rest of the
9945 line after HPOS has been written. */
9946
9947static void
9948note_overwritten_text_cursor (w, hpos, n)
9949 struct window *w;
9950 int hpos, n;
9951{
9952 if (updated_area == TEXT_AREA
9953 && output_cursor.vpos == w->phys_cursor.vpos
9954 && hpos <= w->phys_cursor.hpos
9955 && (n < 0
9956 || hpos + n > w->phys_cursor.hpos))
9957 w->phys_cursor_on_p = 0;
9958}
f451eb13
JB
9959
9960
06a2c219
GM
9961/* Set clipping for output in glyph row ROW. W is the window in which
9962 we operate. GC is the graphics context to set clipping in.
9963 WHOLE_LINE_P non-zero means include the areas used for truncation
9964 mark display and alike in the clipping rectangle.
9965
9966 ROW may be a text row or, e.g., a mode line. Text rows must be
9967 clipped to the interior of the window dedicated to text display,
9968 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9969
9970static void
06a2c219
GM
9971x_clip_to_row (w, row, gc, whole_line_p)
9972 struct window *w;
9973 struct glyph_row *row;
9974 GC gc;
9975 int whole_line_p;
dc6f92b8 9976{
06a2c219
GM
9977 struct frame *f = XFRAME (WINDOW_FRAME (w));
9978 XRectangle clip_rect;
9979 int window_x, window_y, window_width, window_height;
dc6f92b8 9980
06a2c219 9981 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9982
06a2c219
GM
9983 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9984 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9985 clip_rect.y = max (clip_rect.y, window_y);
9986 clip_rect.width = window_width;
9987 clip_rect.height = row->visible_height;
5c1aae96 9988
06a2c219
GM
9989 /* If clipping to the whole line, including trunc marks, extend
9990 the rectangle to the left and increase its width. */
9991 if (whole_line_p)
9992 {
110859fc
GM
9993 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
9994 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 9995 }
5c1aae96 9996
06a2c219 9997 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9998}
9999
06a2c219
GM
10000
10001/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10002
10003static void
06a2c219
GM
10004x_draw_hollow_cursor (w, row)
10005 struct window *w;
10006 struct glyph_row *row;
dc6f92b8 10007{
06a2c219
GM
10008 struct frame *f = XFRAME (WINDOW_FRAME (w));
10009 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10010 Display *dpy = FRAME_X_DISPLAY (f);
10011 int x, y, wd, h;
10012 XGCValues xgcv;
10013 struct glyph *cursor_glyph;
10014 GC gc;
10015
10016 /* Compute frame-relative coordinates from window-relative
10017 coordinates. */
10018 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10019 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10020 + row->ascent - w->phys_cursor_ascent);
10021 h = row->height - 1;
10022
10023 /* Get the glyph the cursor is on. If we can't tell because
10024 the current matrix is invalid or such, give up. */
10025 cursor_glyph = get_phys_cursor_glyph (w);
10026 if (cursor_glyph == NULL)
dc6f92b8
JB
10027 return;
10028
06a2c219
GM
10029 /* Compute the width of the rectangle to draw. If on a stretch
10030 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10031 rectangle as wide as the glyph, but use a canonical character
10032 width instead. */
10033 wd = cursor_glyph->pixel_width - 1;
10034 if (cursor_glyph->type == STRETCH_GLYPH
10035 && !x_stretch_cursor_p)
10036 wd = min (CANON_X_UNIT (f), wd);
10037
10038 /* The foreground of cursor_gc is typically the same as the normal
10039 background color, which can cause the cursor box to be invisible. */
10040 xgcv.foreground = f->output_data.x->cursor_pixel;
10041 if (dpyinfo->scratch_cursor_gc)
10042 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10043 else
10044 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10045 GCForeground, &xgcv);
10046 gc = dpyinfo->scratch_cursor_gc;
10047
10048 /* Set clipping, draw the rectangle, and reset clipping again. */
10049 x_clip_to_row (w, row, gc, 0);
10050 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10051 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10052}
10053
06a2c219
GM
10054
10055/* Draw a bar cursor on window W in glyph row ROW.
10056
10057 Implementation note: One would like to draw a bar cursor with an
10058 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10059 Unfortunately, I didn't find a font yet that has this property set.
10060 --gerd. */
dc6f92b8
JB
10061
10062static void
06a2c219
GM
10063x_draw_bar_cursor (w, row)
10064 struct window *w;
10065 struct glyph_row *row;
dc6f92b8 10066{
06a2c219
GM
10067 /* If cursor hpos is out of bounds, don't draw garbage. This can
10068 happen in mini-buffer windows when switching between echo area
10069 glyphs and mini-buffer. */
10070 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10071 {
10072 struct frame *f = XFRAME (w->frame);
10073 struct glyph *cursor_glyph;
10074 GC gc;
10075 int x;
10076 unsigned long mask;
10077 XGCValues xgcv;
10078 Display *dpy;
10079 Window window;
10080
10081 cursor_glyph = get_phys_cursor_glyph (w);
10082 if (cursor_glyph == NULL)
10083 return;
10084
10085 xgcv.background = f->output_data.x->cursor_pixel;
10086 xgcv.foreground = f->output_data.x->cursor_pixel;
10087 xgcv.graphics_exposures = 0;
10088 mask = GCForeground | GCBackground | GCGraphicsExposures;
10089 dpy = FRAME_X_DISPLAY (f);
10090 window = FRAME_X_WINDOW (f);
10091 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10092
10093 if (gc)
10094 XChangeGC (dpy, gc, mask, &xgcv);
10095 else
10096 {
10097 gc = XCreateGC (dpy, window, mask, &xgcv);
10098 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10099 }
10100
10101 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10102 x_clip_to_row (w, row, gc, 0);
10103 XFillRectangle (dpy, window, gc,
10104 x,
10105 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10106 min (cursor_glyph->pixel_width,
10107 f->output_data.x->cursor_width),
10108 row->height);
10109 XSetClipMask (dpy, gc, None);
10110 }
dc6f92b8
JB
10111}
10112
06a2c219
GM
10113
10114/* Clear the cursor of window W to background color, and mark the
10115 cursor as not shown. This is used when the text where the cursor
10116 is is about to be rewritten. */
10117
dc6f92b8 10118static void
06a2c219
GM
10119x_clear_cursor (w)
10120 struct window *w;
dc6f92b8 10121{
06a2c219
GM
10122 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10123 x_update_window_cursor (w, 0);
10124}
90e65f07 10125
dbc4e1c1 10126
06a2c219
GM
10127/* Draw the cursor glyph of window W in glyph row ROW. See the
10128 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10129
06a2c219
GM
10130static void
10131x_draw_phys_cursor_glyph (w, row, hl)
10132 struct window *w;
10133 struct glyph_row *row;
10134 enum draw_glyphs_face hl;
10135{
10136 /* If cursor hpos is out of bounds, don't draw garbage. This can
10137 happen in mini-buffer windows when switching between echo area
10138 glyphs and mini-buffer. */
10139 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10140 {
10141 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10142 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10143 hl, 0, 0, 0);
10144
10145 /* When we erase the cursor, and ROW is overlapped by other
10146 rows, make sure that these overlapping parts of other rows
10147 are redrawn. */
10148 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10149 {
10150 if (row > w->current_matrix->rows
10151 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10152 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10153
10154 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10155 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10156 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10157 }
10158 }
06a2c219 10159}
dbc4e1c1 10160
eea6af04 10161
06a2c219 10162/* Erase the image of a cursor of window W from the screen. */
eea6af04 10163
06a2c219
GM
10164static void
10165x_erase_phys_cursor (w)
10166 struct window *w;
10167{
10168 struct frame *f = XFRAME (w->frame);
10169 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10170 int hpos = w->phys_cursor.hpos;
10171 int vpos = w->phys_cursor.vpos;
10172 int mouse_face_here_p = 0;
10173 struct glyph_matrix *active_glyphs = w->current_matrix;
10174 struct glyph_row *cursor_row;
10175 struct glyph *cursor_glyph;
10176 enum draw_glyphs_face hl;
10177
10178 /* No cursor displayed or row invalidated => nothing to do on the
10179 screen. */
10180 if (w->phys_cursor_type == NO_CURSOR)
10181 goto mark_cursor_off;
10182
10183 /* VPOS >= active_glyphs->nrows means that window has been resized.
10184 Don't bother to erase the cursor. */
10185 if (vpos >= active_glyphs->nrows)
10186 goto mark_cursor_off;
10187
10188 /* If row containing cursor is marked invalid, there is nothing we
10189 can do. */
10190 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10191 if (!cursor_row->enabled_p)
10192 goto mark_cursor_off;
10193
10194 /* This can happen when the new row is shorter than the old one.
10195 In this case, either x_draw_glyphs or clear_end_of_line
10196 should have cleared the cursor. Note that we wouldn't be
10197 able to erase the cursor in this case because we don't have a
10198 cursor glyph at hand. */
10199 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10200 goto mark_cursor_off;
10201
10202 /* If the cursor is in the mouse face area, redisplay that when
10203 we clear the cursor. */
10204 if (w == XWINDOW (dpyinfo->mouse_face_window)
10205 && (vpos > dpyinfo->mouse_face_beg_row
10206 || (vpos == dpyinfo->mouse_face_beg_row
10207 && hpos >= dpyinfo->mouse_face_beg_col))
10208 && (vpos < dpyinfo->mouse_face_end_row
10209 || (vpos == dpyinfo->mouse_face_end_row
10210 && hpos < dpyinfo->mouse_face_end_col))
10211 /* Don't redraw the cursor's spot in mouse face if it is at the
10212 end of a line (on a newline). The cursor appears there, but
10213 mouse highlighting does not. */
10214 && cursor_row->used[TEXT_AREA] > hpos)
10215 mouse_face_here_p = 1;
10216
10217 /* Maybe clear the display under the cursor. */
10218 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10219 {
10220 int x;
045dee35 10221 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10222
06a2c219
GM
10223 cursor_glyph = get_phys_cursor_glyph (w);
10224 if (cursor_glyph == NULL)
10225 goto mark_cursor_off;
dbc4e1c1 10226
06a2c219
GM
10227 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10228
10229 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10230 x,
045dee35 10231 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10232 cursor_row->y)),
10233 cursor_glyph->pixel_width,
10234 cursor_row->visible_height,
10235 False);
dbc4e1c1 10236 }
06a2c219
GM
10237
10238 /* Erase the cursor by redrawing the character underneath it. */
10239 if (mouse_face_here_p)
10240 hl = DRAW_MOUSE_FACE;
10241 else if (cursor_row->inverse_p)
10242 hl = DRAW_INVERSE_VIDEO;
10243 else
10244 hl = DRAW_NORMAL_TEXT;
10245 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10246
06a2c219
GM
10247 mark_cursor_off:
10248 w->phys_cursor_on_p = 0;
10249 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10250}
10251
10252
06a2c219
GM
10253/* Display or clear cursor of window W. If ON is zero, clear the
10254 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10255 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10256
06a2c219
GM
10257void
10258x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10259 struct window *w;
10260 int on, hpos, vpos, x, y;
dbc4e1c1 10261{
06a2c219
GM
10262 struct frame *f = XFRAME (w->frame);
10263 int new_cursor_type;
10264 struct glyph_matrix *current_glyphs;
10265 struct glyph_row *glyph_row;
10266 struct glyph *glyph;
dbc4e1c1 10267
49d838ea 10268 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10269 windows and frames; in the latter case, the frame or window may
10270 be in the midst of changing its size, and x and y may be off the
10271 window. */
10272 if (! FRAME_VISIBLE_P (f)
10273 || FRAME_GARBAGED_P (f)
10274 || vpos >= w->current_matrix->nrows
10275 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10276 return;
10277
10278 /* If cursor is off and we want it off, return quickly. */
06a2c219 10279 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10280 return;
10281
06a2c219
GM
10282 current_glyphs = w->current_matrix;
10283 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10284 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10285
10286 /* If cursor row is not enabled, we don't really know where to
10287 display the cursor. */
10288 if (!glyph_row->enabled_p)
10289 {
10290 w->phys_cursor_on_p = 0;
10291 return;
10292 }
10293
10294 xassert (interrupt_input_blocked);
10295
10296 /* Set new_cursor_type to the cursor we want to be displayed. In a
10297 mini-buffer window, we want the cursor only to appear if we are
10298 reading input from this window. For the selected window, we want
10299 the cursor type given by the frame parameter. If explicitly
10300 marked off, draw no cursor. In all other cases, we want a hollow
10301 box cursor. */
9b4a7047
GM
10302 if (cursor_in_echo_area
10303 && FRAME_HAS_MINIBUF_P (f)
10304 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10305 {
9b4a7047
GM
10306 if (w == XWINDOW (echo_area_window))
10307 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10308 else
10309 new_cursor_type = HOLLOW_BOX_CURSOR;
10310 }
06a2c219 10311 else
9b4a7047
GM
10312 {
10313 if (w != XWINDOW (selected_window)
10314 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10315 {
10316 if (MINI_WINDOW_P (w))
10317 new_cursor_type = NO_CURSOR;
10318 else
10319 new_cursor_type = HOLLOW_BOX_CURSOR;
10320 }
10321 else if (w->cursor_off_p)
10322 new_cursor_type = NO_CURSOR;
10323 else
10324 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10325 }
06a2c219
GM
10326
10327 /* If cursor is currently being shown and we don't want it to be or
10328 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10329 erase it. */
06a2c219 10330 if (w->phys_cursor_on_p
dc6f92b8 10331 && (!on
06a2c219
GM
10332 || w->phys_cursor.x != x
10333 || w->phys_cursor.y != y
10334 || new_cursor_type != w->phys_cursor_type))
10335 x_erase_phys_cursor (w);
10336
10337 /* If the cursor is now invisible and we want it to be visible,
10338 display it. */
10339 if (on && !w->phys_cursor_on_p)
10340 {
10341 w->phys_cursor_ascent = glyph_row->ascent;
10342 w->phys_cursor_height = glyph_row->height;
10343
10344 /* Set phys_cursor_.* before x_draw_.* is called because some
10345 of them may need the information. */
10346 w->phys_cursor.x = x;
10347 w->phys_cursor.y = glyph_row->y;
10348 w->phys_cursor.hpos = hpos;
10349 w->phys_cursor.vpos = vpos;
10350 w->phys_cursor_type = new_cursor_type;
10351 w->phys_cursor_on_p = 1;
10352
10353 switch (new_cursor_type)
dc6f92b8 10354 {
06a2c219
GM
10355 case HOLLOW_BOX_CURSOR:
10356 x_draw_hollow_cursor (w, glyph_row);
10357 break;
10358
10359 case FILLED_BOX_CURSOR:
10360 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10361 break;
10362
10363 case BAR_CURSOR:
10364 x_draw_bar_cursor (w, glyph_row);
10365 break;
10366
10367 case NO_CURSOR:
10368 break;
dc6f92b8 10369
06a2c219
GM
10370 default:
10371 abort ();
10372 }
dc6f92b8
JB
10373 }
10374
06a2c219 10375#ifndef XFlush
f676886a 10376 if (updating_frame != f)
334208b7 10377 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10378#endif
dc6f92b8
JB
10379}
10380
06a2c219
GM
10381
10382/* Display the cursor on window W, or clear it. X and Y are window
10383 relative pixel coordinates. HPOS and VPOS are glyph matrix
10384 positions. If W is not the selected window, display a hollow
10385 cursor. ON non-zero means display the cursor at X, Y which
10386 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10387
dfcf069d 10388void
06a2c219
GM
10389x_display_cursor (w, on, hpos, vpos, x, y)
10390 struct window *w;
10391 int on, hpos, vpos, x, y;
dc6f92b8 10392{
f94397b5 10393 BLOCK_INPUT;
06a2c219 10394 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10395 UNBLOCK_INPUT;
10396}
10397
06a2c219
GM
10398
10399/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10400 Don't change the cursor's position. */
10401
dfcf069d 10402void
06a2c219 10403x_update_cursor (f, on_p)
5d46f928 10404 struct frame *f;
5d46f928 10405{
06a2c219
GM
10406 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10407}
10408
10409
10410/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10411 in the window tree rooted at W. */
10412
10413static void
10414x_update_cursor_in_window_tree (w, on_p)
10415 struct window *w;
10416 int on_p;
10417{
10418 while (w)
10419 {
10420 if (!NILP (w->hchild))
10421 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10422 else if (!NILP (w->vchild))
10423 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10424 else
10425 x_update_window_cursor (w, on_p);
10426
10427 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10428 }
10429}
5d46f928 10430
f94397b5 10431
06a2c219
GM
10432/* Switch the display of W's cursor on or off, according to the value
10433 of ON. */
10434
10435static void
10436x_update_window_cursor (w, on)
10437 struct window *w;
10438 int on;
10439{
10440 BLOCK_INPUT;
10441 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10442 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10443 UNBLOCK_INPUT;
dc6f92b8 10444}
06a2c219
GM
10445
10446
10447
dc6f92b8
JB
10448\f
10449/* Icons. */
10450
f676886a 10451/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10452 when we get an expose event for it. */
dc6f92b8 10453
dfcf069d 10454void
f676886a
JB
10455refreshicon (f)
10456 struct frame *f;
dc6f92b8 10457{
06a2c219 10458 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10459}
10460
dbc4e1c1 10461/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10462
10463int
990ba854 10464x_bitmap_icon (f, file)
f676886a 10465 struct frame *f;
990ba854 10466 Lisp_Object file;
dc6f92b8 10467{
06a2c219 10468 int bitmap_id;
dc6f92b8 10469
c118dd06 10470 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10471 return 1;
10472
990ba854 10473 /* Free up our existing icon bitmap if any. */
7556890b
RS
10474 if (f->output_data.x->icon_bitmap > 0)
10475 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10476 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10477
10478 if (STRINGP (file))
7f2ae036
RS
10479 bitmap_id = x_create_bitmap_from_file (f, file);
10480 else
10481 {
990ba854 10482 /* Create the GNU bitmap if necessary. */
5bf01b68 10483 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10484 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10485 = x_create_bitmap_from_data (f, gnu_bits,
10486 gnu_width, gnu_height);
990ba854
RS
10487
10488 /* The first time we create the GNU bitmap,
06a2c219 10489 this increments the ref-count one extra time.
990ba854
RS
10490 As a result, the GNU bitmap is never freed.
10491 That way, we don't have to worry about allocating it again. */
334208b7 10492 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10493
334208b7 10494 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10495 }
10496
10497 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10498 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10499
10500 return 0;
10501}
10502
10503
1be2d067
KH
10504/* Make the x-window of frame F use a rectangle with text.
10505 Use ICON_NAME as the text. */
dc6f92b8
JB
10506
10507int
f676886a
JB
10508x_text_icon (f, icon_name)
10509 struct frame *f;
dc6f92b8
JB
10510 char *icon_name;
10511{
c118dd06 10512 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10513 return 1;
10514
1be2d067
KH
10515#ifdef HAVE_X11R4
10516 {
10517 XTextProperty text;
10518 text.value = (unsigned char *) icon_name;
10519 text.encoding = XA_STRING;
10520 text.format = 8;
10521 text.nitems = strlen (icon_name);
10522#ifdef USE_X_TOOLKIT
7556890b 10523 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10524 &text);
10525#else /* not USE_X_TOOLKIT */
10526 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10527#endif /* not USE_X_TOOLKIT */
10528 }
10529#else /* not HAVE_X11R4 */
10530 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10531#endif /* not HAVE_X11R4 */
58769bee 10532
7556890b
RS
10533 if (f->output_data.x->icon_bitmap > 0)
10534 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10535 f->output_data.x->icon_bitmap = 0;
b1c884c3 10536 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10537
10538 return 0;
10539}
10540\f
e99db5a1
RS
10541#define X_ERROR_MESSAGE_SIZE 200
10542
10543/* If non-nil, this should be a string.
10544 It means catch X errors and store the error message in this string. */
10545
10546static Lisp_Object x_error_message_string;
10547
10548/* An X error handler which stores the error message in
10549 x_error_message_string. This is called from x_error_handler if
10550 x_catch_errors is in effect. */
10551
06a2c219 10552static void
e99db5a1
RS
10553x_error_catcher (display, error)
10554 Display *display;
10555 XErrorEvent *error;
10556{
10557 XGetErrorText (display, error->error_code,
10558 XSTRING (x_error_message_string)->data,
10559 X_ERROR_MESSAGE_SIZE);
10560}
10561
10562/* Begin trapping X errors for display DPY. Actually we trap X errors
10563 for all displays, but DPY should be the display you are actually
10564 operating on.
10565
10566 After calling this function, X protocol errors no longer cause
10567 Emacs to exit; instead, they are recorded in the string
10568 stored in x_error_message_string.
10569
10570 Calling x_check_errors signals an Emacs error if an X error has
10571 occurred since the last call to x_catch_errors or x_check_errors.
10572
10573 Calling x_uncatch_errors resumes the normal error handling. */
10574
10575void x_check_errors ();
10576static Lisp_Object x_catch_errors_unwind ();
10577
10578int
10579x_catch_errors (dpy)
10580 Display *dpy;
10581{
10582 int count = specpdl_ptr - specpdl;
10583
10584 /* Make sure any errors from previous requests have been dealt with. */
10585 XSync (dpy, False);
10586
10587 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10588
10589 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10590 XSTRING (x_error_message_string)->data[0] = 0;
10591
10592 return count;
10593}
10594
10595/* Unbind the binding that we made to check for X errors. */
10596
10597static Lisp_Object
10598x_catch_errors_unwind (old_val)
10599 Lisp_Object old_val;
10600{
10601 x_error_message_string = old_val;
10602 return Qnil;
10603}
10604
10605/* If any X protocol errors have arrived since the last call to
10606 x_catch_errors or x_check_errors, signal an Emacs error using
10607 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10608
10609void
10610x_check_errors (dpy, format)
10611 Display *dpy;
10612 char *format;
10613{
10614 /* Make sure to catch any errors incurred so far. */
10615 XSync (dpy, False);
10616
10617 if (XSTRING (x_error_message_string)->data[0])
10618 error (format, XSTRING (x_error_message_string)->data);
10619}
10620
9829ddba
RS
10621/* Nonzero if we had any X protocol errors
10622 since we did x_catch_errors on DPY. */
e99db5a1
RS
10623
10624int
10625x_had_errors_p (dpy)
10626 Display *dpy;
10627{
10628 /* Make sure to catch any errors incurred so far. */
10629 XSync (dpy, False);
10630
10631 return XSTRING (x_error_message_string)->data[0] != 0;
10632}
10633
9829ddba
RS
10634/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10635
06a2c219 10636void
9829ddba
RS
10637x_clear_errors (dpy)
10638 Display *dpy;
10639{
10640 XSTRING (x_error_message_string)->data[0] = 0;
10641}
10642
e99db5a1
RS
10643/* Stop catching X protocol errors and let them make Emacs die.
10644 DPY should be the display that was passed to x_catch_errors.
10645 COUNT should be the value that was returned by
10646 the corresponding call to x_catch_errors. */
10647
10648void
10649x_uncatch_errors (dpy, count)
10650 Display *dpy;
10651 int count;
10652{
10653 unbind_to (count, Qnil);
10654}
10655
10656#if 0
10657static unsigned int x_wire_count;
10658x_trace_wire ()
10659{
10660 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10661}
10662#endif /* ! 0 */
10663
10664\f
10665/* Handle SIGPIPE, which can happen when the connection to a server
10666 simply goes away. SIGPIPE is handled by x_connection_signal.
10667 Don't need to do anything, because the write which caused the
10668 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10669 which will do the appropriate cleanup for us. */
e99db5a1
RS
10670
10671static SIGTYPE
10672x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10673 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10674{
10675#ifdef USG
10676 /* USG systems forget handlers when they are used;
10677 must reestablish each time */
10678 signal (signalnum, x_connection_signal);
10679#endif /* USG */
10680}
10681\f
4746118a
JB
10682/* Handling X errors. */
10683
7a13e894 10684/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10685
4746118a 10686static SIGTYPE
7a13e894
RS
10687x_connection_closed (display, error_message)
10688 Display *display;
10689 char *error_message;
4746118a 10690{
7a13e894
RS
10691 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10692 Lisp_Object frame, tail;
10693
6186a4a0
RS
10694 /* Indicate that this display is dead. */
10695
f613a4c8 10696#ifdef USE_X_TOOLKIT
adabc3a9 10697 XtCloseDisplay (display);
f613a4c8 10698#endif
adabc3a9 10699
6186a4a0
RS
10700 dpyinfo->display = 0;
10701
06a2c219 10702 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10703 that are on the dead display. */
10704 FOR_EACH_FRAME (tail, frame)
10705 {
10706 Lisp_Object minibuf_frame;
10707 minibuf_frame
10708 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10709 if (FRAME_X_P (XFRAME (frame))
10710 && FRAME_X_P (XFRAME (minibuf_frame))
10711 && ! EQ (frame, minibuf_frame)
10712 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10713 Fdelete_frame (frame, Qt);
10714 }
10715
10716 /* Now delete all remaining frames on the dead display.
06a2c219 10717 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10718 for another frame that we need to delete. */
10719 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10720 if (FRAME_X_P (XFRAME (frame))
10721 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10722 {
10723 /* Set this to t so that Fdelete_frame won't get confused
10724 trying to find a replacement. */
10725 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10726 Fdelete_frame (frame, Qt);
10727 }
7a13e894 10728
482a1bd2
KH
10729 if (dpyinfo)
10730 x_delete_display (dpyinfo);
7a13e894
RS
10731
10732 if (x_display_list == 0)
10733 {
f8d07b62 10734 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10735 shut_down_emacs (0, 0, Qnil);
10736 exit (70);
10737 }
12ba150f 10738
7a13e894
RS
10739 /* Ordinary stack unwind doesn't deal with these. */
10740#ifdef SIGIO
10741 sigunblock (sigmask (SIGIO));
10742#endif
10743 sigunblock (sigmask (SIGALRM));
10744 TOTALLY_UNBLOCK_INPUT;
10745
aa4d9a9e 10746 clear_waiting_for_input ();
7a13e894 10747 error ("%s", error_message);
4746118a
JB
10748}
10749
7a13e894
RS
10750/* This is the usual handler for X protocol errors.
10751 It kills all frames on the display that we got the error for.
10752 If that was the only one, it prints an error message and kills Emacs. */
10753
06a2c219 10754static void
c118dd06
JB
10755x_error_quitter (display, error)
10756 Display *display;
10757 XErrorEvent *error;
10758{
7a13e894 10759 char buf[256], buf1[356];
dc6f92b8 10760
58769bee 10761 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10762 original error handler. */
dc6f92b8 10763
c118dd06 10764 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10765 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10766 buf, error->request_code);
7a13e894 10767 x_connection_closed (display, buf1);
dc6f92b8
JB
10768}
10769
e99db5a1
RS
10770/* This is the first-level handler for X protocol errors.
10771 It calls x_error_quitter or x_error_catcher. */
7a13e894 10772
8922af5f 10773static int
e99db5a1 10774x_error_handler (display, error)
8922af5f 10775 Display *display;
e99db5a1 10776 XErrorEvent *error;
8922af5f 10777{
e99db5a1
RS
10778 if (! NILP (x_error_message_string))
10779 x_error_catcher (display, error);
10780 else
10781 x_error_quitter (display, error);
06a2c219 10782 return 0;
f9e24cb9 10783}
c118dd06 10784
e99db5a1
RS
10785/* This is the handler for X IO errors, always.
10786 It kills all frames on the display that we lost touch with.
10787 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10788
c118dd06 10789static int
e99db5a1 10790x_io_error_quitter (display)
c118dd06 10791 Display *display;
c118dd06 10792{
e99db5a1 10793 char buf[256];
dc6f92b8 10794
e99db5a1
RS
10795 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10796 x_connection_closed (display, buf);
06a2c219 10797 return 0;
dc6f92b8 10798}
dc6f92b8 10799\f
f451eb13
JB
10800/* Changing the font of the frame. */
10801
76bcdf39
RS
10802/* Give frame F the font named FONTNAME as its default font, and
10803 return the full name of that font. FONTNAME may be a wildcard
10804 pattern; in that case, we choose some font that fits the pattern.
10805 The return value shows which font we chose. */
10806
b5cf7a0e 10807Lisp_Object
f676886a
JB
10808x_new_font (f, fontname)
10809 struct frame *f;
dc6f92b8
JB
10810 register char *fontname;
10811{
dc43ef94
KH
10812 struct font_info *fontp
10813 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10814
dc43ef94
KH
10815 if (!fontp)
10816 return Qnil;
2224a5fc 10817
dc43ef94 10818 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10819 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10820 f->output_data.x->fontset = -1;
10821
b2cad826
KH
10822 /* Compute the scroll bar width in character columns. */
10823 if (f->scroll_bar_pixel_width > 0)
10824 {
7556890b 10825 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10826 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10827 }
10828 else
4e61bddf
RS
10829 {
10830 int wid = FONT_WIDTH (f->output_data.x->font);
10831 f->scroll_bar_cols = (14 + wid - 1) / wid;
10832 }
b2cad826 10833
f676886a 10834 /* Now make the frame display the given font. */
c118dd06 10835 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10836 {
7556890b
RS
10837 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10838 f->output_data.x->font->fid);
10839 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10840 f->output_data.x->font->fid);
10841 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10842 f->output_data.x->font->fid);
f676886a 10843
a27f9f86 10844 frame_update_line_height (f);
0134a210 10845 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10846 }
a27f9f86
RS
10847 else
10848 /* If we are setting a new frame's font for the first time,
10849 there are no faces yet, so this font's height is the line height. */
7556890b 10850 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10851
dc43ef94
KH
10852 return build_string (fontp->full_name);
10853}
10854
10855/* Give frame F the fontset named FONTSETNAME as its default font, and
10856 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10857 pattern; in that case, we choose some fontset that fits the pattern.
10858 The return value shows which fontset we chose. */
b5cf7a0e 10859
dc43ef94
KH
10860Lisp_Object
10861x_new_fontset (f, fontsetname)
10862 struct frame *f;
10863 char *fontsetname;
10864{
10865 int fontset = fs_query_fontset (f, fontsetname);
10866 struct fontset_info *fontsetp;
10867 Lisp_Object result;
b5cf7a0e 10868
dc43ef94
KH
10869 if (fontset < 0)
10870 return Qnil;
b5cf7a0e 10871
2da424f1
KH
10872 if (f->output_data.x->fontset == fontset)
10873 /* This fontset is already set in frame F. There's nothing more
10874 to do. */
10875 return build_string (fontsetname);
10876
dc43ef94
KH
10877 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10878
10879 if (!fontsetp->fontname[CHARSET_ASCII])
10880 /* This fontset doesn't contain ASCII font. */
10881 return Qnil;
10882
10883 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10884
10885 if (!STRINGP (result))
10886 /* Can't load ASCII font. */
10887 return Qnil;
10888
10889 /* Since x_new_font doesn't update any fontset information, do it now. */
10890 f->output_data.x->fontset = fontset;
2da424f1 10891 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10892 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94
KH
10893
10894 return build_string (fontsetname);
dc6f92b8 10895}
dc6f92b8 10896\f
2e365682
RS
10897/* Calculate the absolute position in frame F
10898 from its current recorded position values and gravity. */
10899
dfcf069d 10900void
43bca5d5 10901x_calc_absolute_position (f)
f676886a 10902 struct frame *f;
dc6f92b8 10903{
06a2c219 10904 Window child;
6dba1858 10905 int win_x = 0, win_y = 0;
7556890b 10906 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10907 int this_window;
10908
9829ddba
RS
10909 /* We have nothing to do if the current position
10910 is already for the top-left corner. */
10911 if (! ((flags & XNegative) || (flags & YNegative)))
10912 return;
10913
c81412a0 10914#ifdef USE_X_TOOLKIT
7556890b 10915 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
10916#else
10917 this_window = FRAME_X_WINDOW (f);
10918#endif
6dba1858
RS
10919
10920 /* Find the position of the outside upper-left corner of
9829ddba
RS
10921 the inner window, with respect to the outer window.
10922 But do this only if we will need the results. */
7556890b 10923 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10924 {
9829ddba
RS
10925 int count;
10926
6dba1858 10927 BLOCK_INPUT;
9829ddba
RS
10928 count = x_catch_errors (FRAME_X_DISPLAY (f));
10929 while (1)
10930 {
10931 x_clear_errors (FRAME_X_DISPLAY (f));
10932 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10933
10934 /* From-window, to-window. */
10935 this_window,
10936 f->output_data.x->parent_desc,
10937
10938 /* From-position, to-position. */
10939 0, 0, &win_x, &win_y,
10940
10941 /* Child of win. */
10942 &child);
10943 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10944 {
10945 Window newroot, newparent = 0xdeadbeef;
10946 Window *newchildren;
10947 int nchildren;
10948
10949 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10950 &newparent, &newchildren, &nchildren))
10951 break;
58769bee 10952
7c3c78a3 10953 XFree ((char *) newchildren);
6dba1858 10954
9829ddba
RS
10955 f->output_data.x->parent_desc = newparent;
10956 }
10957 else
10958 break;
10959 }
6dba1858 10960
9829ddba 10961 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10962 UNBLOCK_INPUT;
10963 }
10964
10965 /* Treat negative positions as relative to the leftmost bottommost
10966 position that fits on the screen. */
20f55f9a 10967 if (flags & XNegative)
7556890b 10968 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10969 - 2 * f->output_data.x->border_width - win_x
10970 - PIXEL_WIDTH (f)
10971 + f->output_data.x->left_pos);
dc6f92b8 10972
20f55f9a 10973 if (flags & YNegative)
06a2c219
GM
10974 {
10975 int menubar_height = 0;
10976
10977#ifdef USE_X_TOOLKIT
10978 if (f->output_data.x->menubar_widget)
10979 menubar_height
10980 = (f->output_data.x->menubar_widget->core.height
10981 + f->output_data.x->menubar_widget->core.border_width);
10982#endif
10983
10984 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10985 - 2 * f->output_data.x->border_width
10986 - win_y
10987 - PIXEL_HEIGHT (f)
10988 - menubar_height
10989 + f->output_data.x->top_pos);
10990 }
2e365682 10991
3a35ab44
RS
10992 /* The left_pos and top_pos
10993 are now relative to the top and left screen edges,
10994 so the flags should correspond. */
7556890b 10995 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10996}
10997
3a35ab44
RS
10998/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10999 to really change the position, and 0 when calling from
11000 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11001 position values). It is -1 when calling from x_set_frame_parameters,
11002 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11003
dfcf069d 11004void
dc05a16b 11005x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11006 struct frame *f;
dc6f92b8 11007 register int xoff, yoff;
dc05a16b 11008 int change_gravity;
dc6f92b8 11009{
4a4cbdd5
KH
11010 int modified_top, modified_left;
11011
aa3ff7c9 11012 if (change_gravity > 0)
3a35ab44 11013 {
7556890b
RS
11014 f->output_data.x->top_pos = yoff;
11015 f->output_data.x->left_pos = xoff;
11016 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11017 if (xoff < 0)
7556890b 11018 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11019 if (yoff < 0)
7556890b
RS
11020 f->output_data.x->size_hint_flags |= YNegative;
11021 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11022 }
43bca5d5 11023 x_calc_absolute_position (f);
dc6f92b8
JB
11024
11025 BLOCK_INPUT;
c32cdd9a 11026 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11027
7556890b
RS
11028 modified_left = f->output_data.x->left_pos;
11029 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11030#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11031 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11032 /* It is a mystery why we need to add the border_width here
11033 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11034 if (change_gravity != 0)
4a4cbdd5 11035 {
7556890b
RS
11036 modified_left += f->output_data.x->border_width;
11037 modified_top += f->output_data.x->border_width;
4a4cbdd5 11038 }
e73ec6fa 11039#endif
4a4cbdd5 11040
3afe33e7 11041#ifdef USE_X_TOOLKIT
7556890b 11042 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11043 modified_left, modified_top);
3afe33e7 11044#else /* not USE_X_TOOLKIT */
334208b7 11045 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11046 modified_left, modified_top);
3afe33e7 11047#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11048 UNBLOCK_INPUT;
11049}
11050
bc20ebbf
FP
11051/* Call this to change the size of frame F's x-window.
11052 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11053 for this size change and subsequent size changes.
11054 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11055
dfcf069d 11056void
bc20ebbf 11057x_set_window_size (f, change_gravity, cols, rows)
f676886a 11058 struct frame *f;
bc20ebbf 11059 int change_gravity;
b1c884c3 11060 int cols, rows;
dc6f92b8 11061{
06a2c219 11062#ifndef USE_X_TOOLKIT
dc6f92b8 11063 int pixelwidth, pixelheight;
06a2c219 11064#endif
dc6f92b8 11065
80fd1fe2 11066 BLOCK_INPUT;
aee9a898
RS
11067
11068#ifdef USE_X_TOOLKIT
3a20653d
RS
11069 {
11070 /* The x and y position of the widget is clobbered by the
11071 call to XtSetValues within EmacsFrameSetCharSize.
11072 This is a real kludge, but I don't understand Xt so I can't
11073 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11074 int xpos = f->output_data.x->widget->core.x;
11075 int ypos = f->output_data.x->widget->core.y;
11076 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11077 f->output_data.x->widget->core.x = xpos;
11078 f->output_data.x->widget->core.y = ypos;
3a20653d 11079 }
80fd1fe2
FP
11080
11081#else /* not USE_X_TOOLKIT */
11082
b1c884c3 11083 check_frame_size (f, &rows, &cols);
7556890b 11084 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11085 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11086 ? 0
11087 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11088 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11089 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11090 f->output_data.x->flags_areas_extra
110859fc 11091 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11092 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11093 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11094
7556890b 11095 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11096 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11097
334208b7
RS
11098 XSync (FRAME_X_DISPLAY (f), False);
11099 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11100 pixelwidth, pixelheight);
b1c884c3
JB
11101
11102 /* Now, strictly speaking, we can't be sure that this is accurate,
11103 but the window manager will get around to dealing with the size
11104 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11105 ConfigureNotify event gets here.
11106
11107 We could just not bother storing any of this information here,
11108 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11109 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11110 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11111 point in the future when the ConfigureNotify event arrives.
11112
11113 We pass 1 for DELAY since we can't run Lisp code inside of
11114 a BLOCK_INPUT. */
7d1e984f 11115 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11116 PIXEL_WIDTH (f) = pixelwidth;
11117 PIXEL_HEIGHT (f) = pixelheight;
11118
aee9a898
RS
11119 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11120 receive in the ConfigureNotify event; if we get what we asked
11121 for, then the event won't cause the screen to become garbaged, so
11122 we have to make sure to do it here. */
11123 SET_FRAME_GARBAGED (f);
11124
11125 XFlush (FRAME_X_DISPLAY (f));
11126
11127#endif /* not USE_X_TOOLKIT */
11128
4d73d038 11129 /* If cursor was outside the new size, mark it as off. */
06a2c219 11130 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11131
aee9a898
RS
11132 /* Clear out any recollection of where the mouse highlighting was,
11133 since it might be in a place that's outside the new frame size.
11134 Actually checking whether it is outside is a pain in the neck,
11135 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11136 cancel_mouse_face (f);
dbc4e1c1 11137
dc6f92b8
JB
11138 UNBLOCK_INPUT;
11139}
dc6f92b8 11140\f
d047c4eb 11141/* Mouse warping. */
dc6f92b8 11142
9b378208 11143void
f676886a
JB
11144x_set_mouse_position (f, x, y)
11145 struct frame *f;
dc6f92b8
JB
11146 int x, y;
11147{
11148 int pix_x, pix_y;
11149
7556890b
RS
11150 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11151 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11152
11153 if (pix_x < 0) pix_x = 0;
11154 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11155
11156 if (pix_y < 0) pix_y = 0;
11157 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11158
11159 BLOCK_INPUT;
dc6f92b8 11160
334208b7
RS
11161 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11162 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11163 UNBLOCK_INPUT;
11164}
11165
9b378208
RS
11166/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11167
11168void
11169x_set_mouse_pixel_position (f, pix_x, pix_y)
11170 struct frame *f;
11171 int pix_x, pix_y;
11172{
11173 BLOCK_INPUT;
11174
334208b7
RS
11175 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11176 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11177 UNBLOCK_INPUT;
11178}
d047c4eb
KH
11179\f
11180/* focus shifting, raising and lowering. */
9b378208 11181
dfcf069d 11182void
f676886a
JB
11183x_focus_on_frame (f)
11184 struct frame *f;
dc6f92b8 11185{
1fb20991 11186#if 0 /* This proves to be unpleasant. */
f676886a 11187 x_raise_frame (f);
1fb20991 11188#endif
6d4238f3
JB
11189#if 0
11190 /* I don't think that the ICCCM allows programs to do things like this
11191 without the interaction of the window manager. Whatever you end up
f676886a 11192 doing with this code, do it to x_unfocus_frame too. */
334208b7 11193 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11194 RevertToPointerRoot, CurrentTime);
c118dd06 11195#endif /* ! 0 */
dc6f92b8
JB
11196}
11197
dfcf069d 11198void
f676886a
JB
11199x_unfocus_frame (f)
11200 struct frame *f;
dc6f92b8 11201{
6d4238f3 11202#if 0
f676886a 11203 /* Look at the remarks in x_focus_on_frame. */
0f941935 11204 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11205 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11206 RevertToPointerRoot, CurrentTime);
c118dd06 11207#endif /* ! 0 */
dc6f92b8
JB
11208}
11209
f676886a 11210/* Raise frame F. */
dc6f92b8 11211
dfcf069d 11212void
f676886a
JB
11213x_raise_frame (f)
11214 struct frame *f;
dc6f92b8 11215{
3a88c238 11216 if (f->async_visible)
dc6f92b8
JB
11217 {
11218 BLOCK_INPUT;
3afe33e7 11219#ifdef USE_X_TOOLKIT
7556890b 11220 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11221#else /* not USE_X_TOOLKIT */
334208b7 11222 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11223#endif /* not USE_X_TOOLKIT */
334208b7 11224 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11225 UNBLOCK_INPUT;
11226 }
11227}
11228
f676886a 11229/* Lower frame F. */
dc6f92b8 11230
dfcf069d 11231void
f676886a
JB
11232x_lower_frame (f)
11233 struct frame *f;
dc6f92b8 11234{
3a88c238 11235 if (f->async_visible)
dc6f92b8
JB
11236 {
11237 BLOCK_INPUT;
3afe33e7 11238#ifdef USE_X_TOOLKIT
7556890b 11239 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11240#else /* not USE_X_TOOLKIT */
334208b7 11241 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11242#endif /* not USE_X_TOOLKIT */
334208b7 11243 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11244 UNBLOCK_INPUT;
11245 }
11246}
11247
dbc4e1c1 11248static void
6b0442dc 11249XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11250 FRAME_PTR f;
6b0442dc 11251 int raise_flag;
dbc4e1c1 11252{
6b0442dc 11253 if (raise_flag)
dbc4e1c1
JB
11254 x_raise_frame (f);
11255 else
11256 x_lower_frame (f);
11257}
d047c4eb
KH
11258\f
11259/* Change of visibility. */
dc6f92b8 11260
9382638d
KH
11261/* This tries to wait until the frame is really visible.
11262 However, if the window manager asks the user where to position
11263 the frame, this will return before the user finishes doing that.
11264 The frame will not actually be visible at that time,
11265 but it will become visible later when the window manager
11266 finishes with it. */
11267
dfcf069d 11268void
f676886a
JB
11269x_make_frame_visible (f)
11270 struct frame *f;
dc6f92b8 11271{
990ba854 11272 Lisp_Object type;
1aa6072f 11273 int original_top, original_left;
dc6f92b8 11274
dc6f92b8 11275 BLOCK_INPUT;
dc6f92b8 11276
990ba854
RS
11277 type = x_icon_type (f);
11278 if (!NILP (type))
11279 x_bitmap_icon (f, type);
bdcd49ba 11280
f676886a 11281 if (! FRAME_VISIBLE_P (f))
90e65f07 11282 {
1aa6072f
RS
11283 /* We test FRAME_GARBAGED_P here to make sure we don't
11284 call x_set_offset a second time
11285 if we get to x_make_frame_visible a second time
11286 before the window gets really visible. */
11287 if (! FRAME_ICONIFIED_P (f)
11288 && ! f->output_data.x->asked_for_visible)
11289 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11290
11291 f->output_data.x->asked_for_visible = 1;
11292
90e65f07 11293 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11294 x_wm_set_window_state (f, NormalState);
3afe33e7 11295#ifdef USE_X_TOOLKIT
d7a38a2e 11296 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11297 XtMapWidget (f->output_data.x->widget);
3afe33e7 11298#else /* not USE_X_TOOLKIT */
7f9c7f94 11299 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11300#endif /* not USE_X_TOOLKIT */
0134a210
RS
11301#if 0 /* This seems to bring back scroll bars in the wrong places
11302 if the window configuration has changed. They seem
11303 to come back ok without this. */
ab648270 11304 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11305 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11306#endif
90e65f07 11307 }
dc6f92b8 11308
334208b7 11309 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11310
0dacf791
RS
11311 /* Synchronize to ensure Emacs knows the frame is visible
11312 before we do anything else. We do this loop with input not blocked
11313 so that incoming events are handled. */
11314 {
11315 Lisp_Object frame;
12ce2351 11316 int count;
28c01ffe
RS
11317 /* This must be before UNBLOCK_INPUT
11318 since events that arrive in response to the actions above
11319 will set it when they are handled. */
11320 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11321
11322 original_left = f->output_data.x->left_pos;
11323 original_top = f->output_data.x->top_pos;
c0a04927
RS
11324
11325 /* This must come after we set COUNT. */
11326 UNBLOCK_INPUT;
11327
2745e6c4 11328 /* We unblock here so that arriving X events are processed. */
1aa6072f 11329
dcb07ae9
RS
11330 /* Now move the window back to where it was "supposed to be".
11331 But don't do it if the gravity is negative.
11332 When the gravity is negative, this uses a position
28c01ffe
RS
11333 that is 3 pixels too low. Perhaps that's really the border width.
11334
11335 Don't do this if the window has never been visible before,
11336 because the window manager may choose the position
11337 and we don't want to override it. */
1aa6072f 11338
4d3f5d9a 11339 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11340 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11341 && previously_visible)
1aa6072f 11342 {
2745e6c4
RS
11343 Drawable rootw;
11344 int x, y;
11345 unsigned int width, height, border, depth;
06a2c219 11346
1aa6072f 11347 BLOCK_INPUT;
9829ddba 11348
06a2c219
GM
11349 /* On some window managers (such as FVWM) moving an existing
11350 window, even to the same place, causes the window manager
11351 to introduce an offset. This can cause the window to move
11352 to an unexpected location. Check the geometry (a little
11353 slow here) and then verify that the window is in the right
11354 place. If the window is not in the right place, move it
11355 there, and take the potential window manager hit. */
2745e6c4
RS
11356 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11357 &rootw, &x, &y, &width, &height, &border, &depth);
11358
11359 if (original_left != x || original_top != y)
11360 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11361 original_left, original_top);
11362
1aa6072f
RS
11363 UNBLOCK_INPUT;
11364 }
9829ddba 11365
e0c1aef2 11366 XSETFRAME (frame, f);
c0a04927 11367
12ce2351
GM
11368 /* Wait until the frame is visible. Process X events until a
11369 MapNotify event has been seen, or until we think we won't get a
11370 MapNotify at all.. */
11371 for (count = input_signal_count + 10;
11372 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11373 {
12ce2351 11374 /* Force processing of queued events. */
334208b7 11375 x_sync (f);
12ce2351
GM
11376
11377 /* Machines that do polling rather than SIGIO have been
11378 observed to go into a busy-wait here. So we'll fake an
11379 alarm signal to let the handler know that there's something
11380 to be read. We used to raise a real alarm, but it seems
11381 that the handler isn't always enabled here. This is
11382 probably a bug. */
8b2f8d4e 11383 if (input_polling_used ())
3b2fa4e6 11384 {
12ce2351
GM
11385 /* It could be confusing if a real alarm arrives while
11386 processing the fake one. Turn it off and let the
11387 handler reset it. */
bffcfca9
GM
11388 int old_poll_suppress_count = poll_suppress_count;
11389 poll_suppress_count = 1;
11390 poll_for_input_1 ();
11391 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11392 }
12ce2351
GM
11393
11394 /* See if a MapNotify event has been processed. */
11395 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11396 }
0dacf791 11397 }
dc6f92b8
JB
11398}
11399
06a2c219 11400/* Change from mapped state to withdrawn state. */
dc6f92b8 11401
d047c4eb
KH
11402/* Make the frame visible (mapped and not iconified). */
11403
dfcf069d 11404void
f676886a
JB
11405x_make_frame_invisible (f)
11406 struct frame *f;
dc6f92b8 11407{
546e6d5b
RS
11408 Window window;
11409
11410#ifdef USE_X_TOOLKIT
11411 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11412 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11413#else /* not USE_X_TOOLKIT */
11414 window = FRAME_X_WINDOW (f);
11415#endif /* not USE_X_TOOLKIT */
dc6f92b8 11416
9319ae23 11417 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11418 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11419 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11420
5627c40e 11421#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11422 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11423 return;
5627c40e 11424#endif
dc6f92b8
JB
11425
11426 BLOCK_INPUT;
c118dd06 11427
af31d76f
RS
11428 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11429 that the current position of the window is user-specified, rather than
11430 program-specified, so that when the window is mapped again, it will be
11431 placed at the same location, without forcing the user to position it
11432 by hand again (they have already done that once for this window.) */
c32cdd9a 11433 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11434
c118dd06
JB
11435#ifdef HAVE_X11R4
11436
334208b7
RS
11437 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11438 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11439 {
11440 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11441 error ("Can't notify window manager of window withdrawal");
c118dd06 11442 }
c118dd06 11443#else /* ! defined (HAVE_X11R4) */
16bd92ea 11444
c118dd06 11445 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11446 if (! EQ (Vx_no_window_manager, Qt))
11447 {
16bd92ea 11448 XEvent unmap;
dc6f92b8 11449
16bd92ea 11450 unmap.xunmap.type = UnmapNotify;
546e6d5b 11451 unmap.xunmap.window = window;
334208b7 11452 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11453 unmap.xunmap.from_configure = False;
334208b7
RS
11454 if (! XSendEvent (FRAME_X_DISPLAY (f),
11455 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11456 False,
06a2c219 11457 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11458 &unmap))
11459 {
11460 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11461 error ("Can't notify window manager of withdrawal");
16bd92ea 11462 }
dc6f92b8
JB
11463 }
11464
16bd92ea 11465 /* Unmap the window ourselves. Cheeky! */
334208b7 11466 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11467#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11468
5627c40e
RS
11469 /* We can't distinguish this from iconification
11470 just by the event that we get from the server.
11471 So we can't win using the usual strategy of letting
11472 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11473 and synchronize with the server to make sure we agree. */
11474 f->visible = 0;
11475 FRAME_ICONIFIED_P (f) = 0;
11476 f->async_visible = 0;
11477 f->async_iconified = 0;
11478
334208b7 11479 x_sync (f);
5627c40e 11480
dc6f92b8
JB
11481 UNBLOCK_INPUT;
11482}
11483
06a2c219 11484/* Change window state from mapped to iconified. */
dc6f92b8 11485
dfcf069d 11486void
f676886a
JB
11487x_iconify_frame (f)
11488 struct frame *f;
dc6f92b8 11489{
3afe33e7 11490 int result;
990ba854 11491 Lisp_Object type;
dc6f92b8 11492
9319ae23 11493 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11494 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11495 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11496
3a88c238 11497 if (f->async_iconified)
dc6f92b8
JB
11498 return;
11499
3afe33e7 11500 BLOCK_INPUT;
546e6d5b 11501
9af3143a
RS
11502 FRAME_SAMPLE_VISIBILITY (f);
11503
990ba854
RS
11504 type = x_icon_type (f);
11505 if (!NILP (type))
11506 x_bitmap_icon (f, type);
bdcd49ba
RS
11507
11508#ifdef USE_X_TOOLKIT
11509
546e6d5b
RS
11510 if (! FRAME_VISIBLE_P (f))
11511 {
11512 if (! EQ (Vx_no_window_manager, Qt))
11513 x_wm_set_window_state (f, IconicState);
11514 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11515 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11516 /* The server won't give us any event to indicate
11517 that an invisible frame was changed to an icon,
11518 so we have to record it here. */
11519 f->iconified = 1;
1e6bc770 11520 f->visible = 1;
9cf30a30 11521 f->async_iconified = 1;
1e6bc770 11522 f->async_visible = 0;
546e6d5b
RS
11523 UNBLOCK_INPUT;
11524 return;
11525 }
11526
334208b7 11527 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11528 XtWindow (f->output_data.x->widget),
334208b7 11529 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11530 UNBLOCK_INPUT;
11531
11532 if (!result)
546e6d5b 11533 error ("Can't notify window manager of iconification");
3afe33e7
RS
11534
11535 f->async_iconified = 1;
1e6bc770
RS
11536 f->async_visible = 0;
11537
8c002a25
KH
11538
11539 BLOCK_INPUT;
334208b7 11540 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11541 UNBLOCK_INPUT;
3afe33e7
RS
11542#else /* not USE_X_TOOLKIT */
11543
fd13dbb2
RS
11544 /* Make sure the X server knows where the window should be positioned,
11545 in case the user deiconifies with the window manager. */
11546 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11547 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11548
16bd92ea
JB
11549 /* Since we don't know which revision of X we're running, we'll use both
11550 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11551
11552 /* X11R4: send a ClientMessage to the window manager using the
11553 WM_CHANGE_STATE type. */
11554 {
11555 XEvent message;
58769bee 11556
c118dd06 11557 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11558 message.xclient.type = ClientMessage;
334208b7 11559 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11560 message.xclient.format = 32;
11561 message.xclient.data.l[0] = IconicState;
11562
334208b7
RS
11563 if (! XSendEvent (FRAME_X_DISPLAY (f),
11564 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11565 False,
11566 SubstructureRedirectMask | SubstructureNotifyMask,
11567 &message))
dc6f92b8
JB
11568 {
11569 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11570 error ("Can't notify window manager of iconification");
dc6f92b8 11571 }
16bd92ea 11572 }
dc6f92b8 11573
58769bee 11574 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11575 IconicState. */
11576 x_wm_set_window_state (f, IconicState);
dc6f92b8 11577
a9c00105
RS
11578 if (!FRAME_VISIBLE_P (f))
11579 {
11580 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11581 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11582 }
11583
3a88c238 11584 f->async_iconified = 1;
1e6bc770 11585 f->async_visible = 0;
dc6f92b8 11586
334208b7 11587 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11588 UNBLOCK_INPUT;
8c002a25 11589#endif /* not USE_X_TOOLKIT */
dc6f92b8 11590}
d047c4eb 11591\f
c0ff3fab 11592/* Destroy the X window of frame F. */
dc6f92b8 11593
dfcf069d 11594void
c0ff3fab 11595x_destroy_window (f)
f676886a 11596 struct frame *f;
dc6f92b8 11597{
7f9c7f94
RS
11598 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11599
dc6f92b8 11600 BLOCK_INPUT;
c0ff3fab 11601
6186a4a0
RS
11602 /* If a display connection is dead, don't try sending more
11603 commands to the X server. */
11604 if (dpyinfo->display != 0)
11605 {
11606 if (f->output_data.x->icon_desc != 0)
11607 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf
RS
11608#ifdef HAVE_X_I18N
11609 if (FRAME_XIM (f))
11610 {
11611 XDestroyIC (FRAME_XIC (f));
408be661
RS
11612#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11613 /* This line causes crashes on Solaris with Openwin,
11614 due to an apparent bug in XCloseIM.
11615 X11R6 seems not to have the bug. */
31f41daf 11616 XCloseIM (FRAME_XIM (f));
a9978dd8 11617#endif
31f41daf
RS
11618 }
11619#endif
6186a4a0 11620 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11621#ifdef USE_X_TOOLKIT
06a2c219
GM
11622 if (f->output_data.x->widget)
11623 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11624 free_frame_menubar (f);
3afe33e7
RS
11625#endif /* USE_X_TOOLKIT */
11626
6186a4a0
RS
11627 free_frame_faces (f);
11628 XFlush (FRAME_X_DISPLAY (f));
11629 }
dc6f92b8 11630
df89d8a4 11631 if (f->output_data.x->saved_menu_event)
06a2c219 11632 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11633
7556890b
RS
11634 xfree (f->output_data.x);
11635 f->output_data.x = 0;
0f941935
KH
11636 if (f == dpyinfo->x_focus_frame)
11637 dpyinfo->x_focus_frame = 0;
11638 if (f == dpyinfo->x_focus_event_frame)
11639 dpyinfo->x_focus_event_frame = 0;
11640 if (f == dpyinfo->x_highlight_frame)
11641 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11642
7f9c7f94
RS
11643 dpyinfo->reference_count--;
11644
11645 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11646 {
7f9c7f94
RS
11647 dpyinfo->mouse_face_beg_row
11648 = dpyinfo->mouse_face_beg_col = -1;
11649 dpyinfo->mouse_face_end_row
11650 = dpyinfo->mouse_face_end_col = -1;
11651 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11652 dpyinfo->mouse_face_deferred_gc = 0;
11653 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11654 }
0134a210 11655
c0ff3fab 11656 UNBLOCK_INPUT;
dc6f92b8
JB
11657}
11658\f
f451eb13
JB
11659/* Setting window manager hints. */
11660
af31d76f
RS
11661/* Set the normal size hints for the window manager, for frame F.
11662 FLAGS is the flags word to use--or 0 meaning preserve the flags
11663 that the window now has.
11664 If USER_POSITION is nonzero, we set the USPosition
11665 flag (this is useful when FLAGS is 0). */
6dba1858 11666
dfcf069d 11667void
af31d76f 11668x_wm_set_size_hint (f, flags, user_position)
f676886a 11669 struct frame *f;
af31d76f
RS
11670 long flags;
11671 int user_position;
dc6f92b8
JB
11672{
11673 XSizeHints size_hints;
3afe33e7
RS
11674
11675#ifdef USE_X_TOOLKIT
7e4f2521
FP
11676 Arg al[2];
11677 int ac = 0;
11678 Dimension widget_width, widget_height;
7556890b 11679 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11680#else /* not USE_X_TOOLKIT */
c118dd06 11681 Window window = FRAME_X_WINDOW (f);
3afe33e7 11682#endif /* not USE_X_TOOLKIT */
dc6f92b8 11683
b72a58fd
RS
11684 /* Setting PMaxSize caused various problems. */
11685 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11686
7556890b
RS
11687 size_hints.x = f->output_data.x->left_pos;
11688 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11689
7e4f2521
FP
11690#ifdef USE_X_TOOLKIT
11691 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11692 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11693 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11694 size_hints.height = widget_height;
11695 size_hints.width = widget_width;
11696#else /* not USE_X_TOOLKIT */
f676886a
JB
11697 size_hints.height = PIXEL_HEIGHT (f);
11698 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11699#endif /* not USE_X_TOOLKIT */
7553a6b7 11700
7556890b
RS
11701 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11702 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11703 size_hints.max_width
11704 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11705 size_hints.max_height
11706 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11707
d067ea8b
KH
11708 /* Calculate the base and minimum sizes.
11709
11710 (When we use the X toolkit, we don't do it here.
11711 Instead we copy the values that the widgets are using, below.) */
11712#ifndef USE_X_TOOLKIT
b1c884c3 11713 {
b0342f17 11714 int base_width, base_height;
0134a210 11715 int min_rows = 0, min_cols = 0;
b0342f17 11716
f451eb13
JB
11717 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11718 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11719
0134a210 11720 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11721
0134a210
RS
11722 /* The window manager uses the base width hints to calculate the
11723 current number of rows and columns in the frame while
11724 resizing; min_width and min_height aren't useful for this
11725 purpose, since they might not give the dimensions for a
11726 zero-row, zero-column frame.
58769bee 11727
0134a210
RS
11728 We use the base_width and base_height members if we have
11729 them; otherwise, we set the min_width and min_height members
11730 to the size for a zero x zero frame. */
b0342f17
JB
11731
11732#ifdef HAVE_X11R4
0134a210
RS
11733 size_hints.flags |= PBaseSize;
11734 size_hints.base_width = base_width;
11735 size_hints.base_height = base_height;
11736 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11737 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11738#else
0134a210
RS
11739 size_hints.min_width = base_width;
11740 size_hints.min_height = base_height;
b0342f17 11741#endif
b1c884c3 11742 }
dc6f92b8 11743
d067ea8b 11744 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11745 if (flags)
dc6f92b8 11746 {
d067ea8b
KH
11747 size_hints.flags |= flags;
11748 goto no_read;
11749 }
11750#endif /* not USE_X_TOOLKIT */
11751
11752 {
11753 XSizeHints hints; /* Sometimes I hate X Windows... */
11754 long supplied_return;
11755 int value;
af31d76f
RS
11756
11757#ifdef HAVE_X11R4
d067ea8b
KH
11758 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11759 &supplied_return);
af31d76f 11760#else
d067ea8b 11761 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11762#endif
58769bee 11763
d067ea8b
KH
11764#ifdef USE_X_TOOLKIT
11765 size_hints.base_height = hints.base_height;
11766 size_hints.base_width = hints.base_width;
11767 size_hints.min_height = hints.min_height;
11768 size_hints.min_width = hints.min_width;
11769#endif
11770
11771 if (flags)
11772 size_hints.flags |= flags;
11773 else
11774 {
11775 if (value == 0)
11776 hints.flags = 0;
11777 if (hints.flags & PSize)
11778 size_hints.flags |= PSize;
11779 if (hints.flags & PPosition)
11780 size_hints.flags |= PPosition;
11781 if (hints.flags & USPosition)
11782 size_hints.flags |= USPosition;
11783 if (hints.flags & USSize)
11784 size_hints.flags |= USSize;
11785 }
11786 }
11787
06a2c219 11788#ifndef USE_X_TOOLKIT
d067ea8b 11789 no_read:
06a2c219 11790#endif
0134a210 11791
af31d76f 11792#ifdef PWinGravity
7556890b 11793 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11794 size_hints.flags |= PWinGravity;
dc05a16b 11795
af31d76f 11796 if (user_position)
6dba1858 11797 {
af31d76f
RS
11798 size_hints.flags &= ~ PPosition;
11799 size_hints.flags |= USPosition;
6dba1858 11800 }
2554751d 11801#endif /* PWinGravity */
6dba1858 11802
b0342f17 11803#ifdef HAVE_X11R4
334208b7 11804 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11805#else
334208b7 11806 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11807#endif
dc6f92b8
JB
11808}
11809
11810/* Used for IconicState or NormalState */
06a2c219 11811
dfcf069d 11812void
f676886a
JB
11813x_wm_set_window_state (f, state)
11814 struct frame *f;
dc6f92b8
JB
11815 int state;
11816{
3afe33e7 11817#ifdef USE_X_TOOLKIT
546e6d5b
RS
11818 Arg al[1];
11819
11820 XtSetArg (al[0], XtNinitialState, state);
7556890b 11821 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11822#else /* not USE_X_TOOLKIT */
c118dd06 11823 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11824
7556890b
RS
11825 f->output_data.x->wm_hints.flags |= StateHint;
11826 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11827
7556890b 11828 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11829#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11830}
11831
dfcf069d 11832void
7f2ae036 11833x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11834 struct frame *f;
7f2ae036 11835 int pixmap_id;
dc6f92b8 11836{
d2bd6bc4
RS
11837 Pixmap icon_pixmap;
11838
06a2c219 11839#ifndef USE_X_TOOLKIT
c118dd06 11840 Window window = FRAME_X_WINDOW (f);
75231bad 11841#endif
dc6f92b8 11842
7f2ae036 11843 if (pixmap_id > 0)
dbc4e1c1 11844 {
d2bd6bc4 11845 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11846 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11847 }
11848 else
68568555
RS
11849 {
11850 /* It seems there is no way to turn off use of an icon pixmap.
11851 The following line does it, only if no icon has yet been created,
11852 for some window managers. But with mwm it crashes.
11853 Some people say it should clear the IconPixmapHint bit in this case,
11854 but that doesn't work, and the X consortium said it isn't the
11855 right thing at all. Since there is no way to win,
11856 best to explicitly give up. */
11857#if 0
11858 f->output_data.x->wm_hints.icon_pixmap = None;
11859#else
11860 return;
11861#endif
11862 }
b1c884c3 11863
d2bd6bc4
RS
11864#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11865
11866 {
11867 Arg al[1];
11868 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11869 XtSetValues (f->output_data.x->widget, al, 1);
11870 }
11871
11872#else /* not USE_X_TOOLKIT */
11873
7556890b
RS
11874 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11875 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11876
11877#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11878}
11879
dfcf069d 11880void
f676886a
JB
11881x_wm_set_icon_position (f, icon_x, icon_y)
11882 struct frame *f;
dc6f92b8
JB
11883 int icon_x, icon_y;
11884{
75231bad 11885#ifdef USE_X_TOOLKIT
7556890b 11886 Window window = XtWindow (f->output_data.x->widget);
75231bad 11887#else
c118dd06 11888 Window window = FRAME_X_WINDOW (f);
75231bad 11889#endif
dc6f92b8 11890
7556890b
RS
11891 f->output_data.x->wm_hints.flags |= IconPositionHint;
11892 f->output_data.x->wm_hints.icon_x = icon_x;
11893 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11894
7556890b 11895 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11896}
11897
11898\f
06a2c219
GM
11899/***********************************************************************
11900 Fonts
11901 ***********************************************************************/
dc43ef94
KH
11902
11903/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11904
dc43ef94
KH
11905struct font_info *
11906x_get_font_info (f, font_idx)
11907 FRAME_PTR f;
11908 int font_idx;
11909{
11910 return (FRAME_X_FONT_TABLE (f) + font_idx);
11911}
11912
11913
11914/* Return a list of names of available fonts matching PATTERN on frame
11915 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11916 to be listed. Frame F NULL means we have not yet created any
11917 frame on X, and consult the first display in x_display_list.
11918 MAXNAMES sets a limit on how many fonts to match. */
11919
11920Lisp_Object
11921x_list_fonts (f, pattern, size, maxnames)
11922 FRAME_PTR f;
11923 Lisp_Object pattern;
11924 int size;
11925 int maxnames;
11926{
06a2c219
GM
11927 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11928 Lisp_Object tem, second_best;
dc43ef94 11929 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 11930 int try_XLoadQueryFont = 0;
53ca4657 11931 int count;
dc43ef94 11932
6b0efe73 11933 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11934 if (NILP (patterns))
11935 patterns = Fcons (pattern, Qnil);
81ba44e5 11936
09c6077f
KH
11937 if (maxnames == 1 && !size)
11938 /* We can return any single font matching PATTERN. */
11939 try_XLoadQueryFont = 1;
9a32686f 11940
8e713be6 11941 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 11942 {
dc43ef94
KH
11943 int num_fonts;
11944 char **names;
11945
8e713be6 11946 pattern = XCAR (patterns);
536f4067
RS
11947 /* See if we cached the result for this particular query.
11948 The cache is an alist of the form:
11949 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11950 */
8e713be6 11951 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
11952 key = Fcons (pattern, make_number (maxnames)),
11953 !NILP (list = Fassoc (key, tem))))
11954 {
11955 list = Fcdr_safe (list);
11956 /* We have a cashed list. Don't have to get the list again. */
11957 goto label_cached;
11958 }
11959
11960 /* At first, put PATTERN in the cache. */
09c6077f 11961
dc43ef94 11962 BLOCK_INPUT;
17d85edc
KH
11963 count = x_catch_errors (dpy);
11964
09c6077f
KH
11965 if (try_XLoadQueryFont)
11966 {
11967 XFontStruct *font;
11968 unsigned long value;
11969
11970 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
11971 if (x_had_errors_p (dpy))
11972 {
11973 /* This error is perhaps due to insufficient memory on X
11974 server. Let's just ignore it. */
11975 font = NULL;
11976 x_clear_errors (dpy);
11977 }
11978
09c6077f
KH
11979 if (font
11980 && XGetFontProperty (font, XA_FONT, &value))
11981 {
11982 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11983 int len = strlen (name);
01c752b5 11984 char *tmp;
09c6077f 11985
6f6512e8
KH
11986 /* If DXPC (a Differential X Protocol Compressor)
11987 Ver.3.7 is running, XGetAtomName will return null
11988 string. We must avoid such a name. */
11989 if (len == 0)
11990 try_XLoadQueryFont = 0;
11991 else
11992 {
11993 num_fonts = 1;
11994 names = (char **) alloca (sizeof (char *));
11995 /* Some systems only allow alloca assigned to a
11996 simple var. */
11997 tmp = (char *) alloca (len + 1); names[0] = tmp;
11998 bcopy (name, names[0], len + 1);
11999 XFree (name);
12000 }
09c6077f
KH
12001 }
12002 else
12003 try_XLoadQueryFont = 0;
a083fd23
RS
12004
12005 if (font)
12006 XFreeFont (dpy, font);
09c6077f
KH
12007 }
12008
12009 if (!try_XLoadQueryFont)
17d85edc
KH
12010 {
12011 /* We try at least 10 fonts because XListFonts will return
12012 auto-scaled fonts at the head. */
12013 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12014 &num_fonts);
12015 if (x_had_errors_p (dpy))
12016 {
12017 /* This error is perhaps due to insufficient memory on X
12018 server. Let's just ignore it. */
12019 names = NULL;
12020 x_clear_errors (dpy);
12021 }
12022 }
12023
12024 x_uncatch_errors (dpy, count);
dc43ef94
KH
12025 UNBLOCK_INPUT;
12026
12027 if (names)
12028 {
12029 int i;
dc43ef94
KH
12030
12031 /* Make a list of all the fonts we got back.
12032 Store that in the font cache for the display. */
12033 for (i = 0; i < num_fonts; i++)
12034 {
06a2c219 12035 int width = 0;
dc43ef94 12036 char *p = names[i];
06a2c219
GM
12037 int average_width = -1, dashes = 0;
12038
dc43ef94 12039 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12040 14 dashes, and the field value following 12th dash
12041 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12042 is usually too ugly to be used for editing. Let's
12043 ignore it. */
dc43ef94
KH
12044 while (*p)
12045 if (*p++ == '-')
12046 {
12047 dashes++;
12048 if (dashes == 7) /* PIXEL_SIZE field */
12049 width = atoi (p);
12050 else if (dashes == 12) /* AVERAGE_WIDTH field */
12051 average_width = atoi (p);
12052 }
12053 if (dashes < 14 || average_width != 0)
12054 {
12055 tem = build_string (names[i]);
12056 if (NILP (Fassoc (tem, list)))
12057 {
12058 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12059 && ((fast_c_string_match_ignore_case
12060 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12061 >= 0))
12062 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12063 width of this font. */
dc43ef94
KH
12064 list = Fcons (Fcons (tem, make_number (width)), list);
12065 else
12066 /* For the moment, width is not known. */
12067 list = Fcons (Fcons (tem, Qnil), list);
12068 }
12069 }
12070 }
09c6077f
KH
12071 if (!try_XLoadQueryFont)
12072 XFreeFontNames (names);
dc43ef94
KH
12073 }
12074
b5210ea7 12075 /* Now store the result in the cache. */
dc43ef94 12076 if (f != NULL)
8e713be6 12077 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12078 = Fcons (Fcons (key, list),
8e713be6 12079 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12080
b5210ea7
KH
12081 label_cached:
12082 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12083
b5210ea7
KH
12084 newlist = second_best = Qnil;
12085 /* Make a list of the fonts that have the right width. */
8e713be6 12086 for (; CONSP (list); list = XCDR (list))
b5210ea7 12087 {
536f4067
RS
12088 int found_size;
12089
8e713be6 12090 tem = XCAR (list);
dc43ef94 12091
8e713be6 12092 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12093 continue;
12094 if (!size)
12095 {
8e713be6 12096 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12097 continue;
12098 }
dc43ef94 12099
8e713be6 12100 if (!INTEGERP (XCDR (tem)))
dc43ef94 12101 {
b5210ea7
KH
12102 /* Since we have not yet known the size of this font, we
12103 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12104 XFontStruct *thisinfo;
12105
12106 BLOCK_INPUT;
17d85edc 12107 count = x_catch_errors (dpy);
dc43ef94 12108 thisinfo = XLoadQueryFont (dpy,
8e713be6 12109 XSTRING (XCAR (tem))->data);
17d85edc
KH
12110 if (x_had_errors_p (dpy))
12111 {
12112 /* This error is perhaps due to insufficient memory on X
12113 server. Let's just ignore it. */
12114 thisinfo = NULL;
12115 x_clear_errors (dpy);
12116 }
12117 x_uncatch_errors (dpy, count);
dc43ef94
KH
12118 UNBLOCK_INPUT;
12119
12120 if (thisinfo)
12121 {
8e713be6 12122 XCDR (tem)
536f4067
RS
12123 = (thisinfo->min_bounds.width == 0
12124 ? make_number (0)
12125 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12126 XFreeFont (dpy, thisinfo);
12127 }
12128 else
b5210ea7 12129 /* For unknown reason, the previous call of XListFont had
06a2c219 12130 returned a font which can't be opened. Record the size
b5210ea7 12131 as 0 not to try to open it again. */
8e713be6 12132 XCDR (tem) = make_number (0);
dc43ef94 12133 }
536f4067 12134
8e713be6 12135 found_size = XINT (XCDR (tem));
536f4067 12136 if (found_size == size)
8e713be6 12137 newlist = Fcons (XCAR (tem), newlist);
536f4067 12138 else if (found_size > 0)
b5210ea7 12139 {
536f4067 12140 if (NILP (second_best))
b5210ea7 12141 second_best = tem;
536f4067
RS
12142 else if (found_size < size)
12143 {
8e713be6
KR
12144 if (XINT (XCDR (second_best)) > size
12145 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12146 second_best = tem;
12147 }
12148 else
12149 {
8e713be6
KR
12150 if (XINT (XCDR (second_best)) > size
12151 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12152 second_best = tem;
12153 }
b5210ea7
KH
12154 }
12155 }
12156 if (!NILP (newlist))
12157 break;
12158 else if (!NILP (second_best))
12159 {
8e713be6 12160 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12161 break;
dc43ef94 12162 }
dc43ef94
KH
12163 }
12164
12165 return newlist;
12166}
12167
06a2c219
GM
12168
12169#if GLYPH_DEBUG
12170
12171/* Check that FONT is valid on frame F. It is if it can be found in F's
12172 font table. */
12173
12174static void
12175x_check_font (f, font)
12176 struct frame *f;
12177 XFontStruct *font;
12178{
12179 int i;
12180 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12181
12182 xassert (font != NULL);
12183
12184 for (i = 0; i < dpyinfo->n_fonts; i++)
12185 if (dpyinfo->font_table[i].name
12186 && font == dpyinfo->font_table[i].font)
12187 break;
12188
12189 xassert (i < dpyinfo->n_fonts);
12190}
12191
12192#endif /* GLYPH_DEBUG != 0 */
12193
12194/* Set *W to the minimum width, *H to the minimum font height of FONT.
12195 Note: There are (broken) X fonts out there with invalid XFontStruct
12196 min_bounds contents. For example, handa@etl.go.jp reports that
12197 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12198 have font->min_bounds.width == 0. */
12199
12200static INLINE void
12201x_font_min_bounds (font, w, h)
12202 XFontStruct *font;
12203 int *w, *h;
12204{
12205 *h = FONT_HEIGHT (font);
12206 *w = font->min_bounds.width;
12207
12208 /* Try to handle the case where FONT->min_bounds has invalid
12209 contents. Since the only font known to have invalid min_bounds
12210 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12211 if (*w <= 0)
12212 *w = font->max_bounds.width;
12213}
12214
12215
12216/* Compute the smallest character width and smallest font height over
12217 all fonts available on frame F. Set the members smallest_char_width
12218 and smallest_font_height in F's x_display_info structure to
12219 the values computed. Value is non-zero if smallest_font_height or
12220 smallest_char_width become smaller than they were before. */
12221
12222static int
12223x_compute_min_glyph_bounds (f)
12224 struct frame *f;
12225{
12226 int i;
12227 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12228 XFontStruct *font;
12229 int old_width = dpyinfo->smallest_char_width;
12230 int old_height = dpyinfo->smallest_font_height;
12231
12232 dpyinfo->smallest_font_height = 100000;
12233 dpyinfo->smallest_char_width = 100000;
12234
12235 for (i = 0; i < dpyinfo->n_fonts; ++i)
12236 if (dpyinfo->font_table[i].name)
12237 {
12238 struct font_info *fontp = dpyinfo->font_table + i;
12239 int w, h;
12240
12241 font = (XFontStruct *) fontp->font;
12242 xassert (font != (XFontStruct *) ~0);
12243 x_font_min_bounds (font, &w, &h);
12244
12245 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12246 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12247 }
12248
12249 xassert (dpyinfo->smallest_char_width > 0
12250 && dpyinfo->smallest_font_height > 0);
12251
12252 return (dpyinfo->n_fonts == 1
12253 || dpyinfo->smallest_char_width < old_width
12254 || dpyinfo->smallest_font_height < old_height);
12255}
12256
12257
dc43ef94
KH
12258/* Load font named FONTNAME of the size SIZE for frame F, and return a
12259 pointer to the structure font_info while allocating it dynamically.
12260 If SIZE is 0, load any size of font.
12261 If loading is failed, return NULL. */
12262
12263struct font_info *
12264x_load_font (f, fontname, size)
12265 struct frame *f;
12266 register char *fontname;
12267 int size;
12268{
12269 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12270 Lisp_Object font_names;
d645aaa4 12271 int count;
dc43ef94
KH
12272
12273 /* Get a list of all the fonts that match this name. Once we
12274 have a list of matching fonts, we compare them against the fonts
12275 we already have by comparing names. */
09c6077f 12276 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12277
12278 if (!NILP (font_names))
12279 {
12280 Lisp_Object tail;
12281 int i;
12282
12283 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12284 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12285 if (dpyinfo->font_table[i].name
12286 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12287 XSTRING (XCAR (tail))->data)
06a2c219 12288 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12289 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12290 return (dpyinfo->font_table + i);
12291 }
12292
12293 /* Load the font and add it to the table. */
12294 {
12295 char *full_name;
12296 XFontStruct *font;
12297 struct font_info *fontp;
12298 unsigned long value;
06a2c219 12299 int i;
dc43ef94 12300
2da424f1
KH
12301 /* If we have found fonts by x_list_font, load one of them. If
12302 not, we still try to load a font by the name given as FONTNAME
12303 because XListFonts (called in x_list_font) of some X server has
12304 a bug of not finding a font even if the font surely exists and
12305 is loadable by XLoadQueryFont. */
e1d6d5b9 12306 if (size > 0 && !NILP (font_names))
8e713be6 12307 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12308
12309 BLOCK_INPUT;
d645aaa4 12310 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12311 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12312 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12313 {
12314 /* This error is perhaps due to insufficient memory on X
12315 server. Let's just ignore it. */
12316 font = NULL;
12317 x_clear_errors (FRAME_X_DISPLAY (f));
12318 }
12319 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12320 UNBLOCK_INPUT;
b5210ea7 12321 if (!font)
dc43ef94
KH
12322 return NULL;
12323
06a2c219
GM
12324 /* Find a free slot in the font table. */
12325 for (i = 0; i < dpyinfo->n_fonts; ++i)
12326 if (dpyinfo->font_table[i].name == NULL)
12327 break;
12328
12329 /* If no free slot found, maybe enlarge the font table. */
12330 if (i == dpyinfo->n_fonts
12331 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12332 {
06a2c219
GM
12333 int sz;
12334 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12335 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12336 dpyinfo->font_table
06a2c219 12337 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12338 }
12339
06a2c219
GM
12340 fontp = dpyinfo->font_table + i;
12341 if (i == dpyinfo->n_fonts)
12342 ++dpyinfo->n_fonts;
dc43ef94
KH
12343
12344 /* Now fill in the slots of *FONTP. */
12345 BLOCK_INPUT;
12346 fontp->font = font;
06a2c219 12347 fontp->font_idx = i;
dc43ef94
KH
12348 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12349 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12350
12351 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12352 full_name = 0;
12353 if (XGetFontProperty (font, XA_FONT, &value))
12354 {
12355 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12356 char *p = name;
12357 int dashes = 0;
12358
12359 /* Count the number of dashes in the "full name".
12360 If it is too few, this isn't really the font's full name,
12361 so don't use it.
12362 In X11R4, the fonts did not come with their canonical names
12363 stored in them. */
12364 while (*p)
12365 {
12366 if (*p == '-')
12367 dashes++;
12368 p++;
12369 }
12370
12371 if (dashes >= 13)
12372 {
12373 full_name = (char *) xmalloc (p - name + 1);
12374 bcopy (name, full_name, p - name + 1);
12375 }
12376
12377 XFree (name);
12378 }
12379
12380 if (full_name != 0)
12381 fontp->full_name = full_name;
12382 else
12383 fontp->full_name = fontp->name;
12384
12385 fontp->size = font->max_bounds.width;
d5749adb
KH
12386 fontp->height = FONT_HEIGHT (font);
12387 {
12388 /* For some font, ascent and descent in max_bounds field is
12389 larger than the above value. */
12390 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12391 if (max_height > fontp->height)
74848a96 12392 fontp->height = max_height;
d5749adb 12393 }
dc43ef94 12394
2da424f1
KH
12395 if (NILP (font_names))
12396 {
12397 /* We come here because of a bug of XListFonts mentioned at
12398 the head of this block. Let's store this information in
12399 the cache for x_list_fonts. */
12400 Lisp_Object lispy_name = build_string (fontname);
12401 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12402
8e713be6 12403 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12404 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12405 Fcons (Fcons (lispy_full_name,
12406 make_number (fontp->size)),
12407 Qnil)),
8e713be6 12408 XCDR (dpyinfo->name_list_element));
2da424f1 12409 if (full_name)
8e713be6 12410 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12411 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12412 Fcons (Fcons (lispy_full_name,
12413 make_number (fontp->size)),
12414 Qnil)),
8e713be6 12415 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12416 }
12417
dc43ef94
KH
12418 /* The slot `encoding' specifies how to map a character
12419 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12420 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12421 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12422 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12423 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12424 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12425 which is never used by any charset. If mapping can't be
12426 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12427 fontp->encoding[1]
12428 = (font->max_byte1 == 0
12429 /* 1-byte font */
12430 ? (font->min_char_or_byte2 < 0x80
12431 ? (font->max_char_or_byte2 < 0x80
12432 ? 0 /* 0x20..0x7F */
8ff102bd 12433 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12434 : 1) /* 0xA0..0xFF */
12435 /* 2-byte font */
12436 : (font->min_byte1 < 0x80
12437 ? (font->max_byte1 < 0x80
12438 ? (font->min_char_or_byte2 < 0x80
12439 ? (font->max_char_or_byte2 < 0x80
12440 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12441 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12442 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12443 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12444 : (font->min_char_or_byte2 < 0x80
12445 ? (font->max_char_or_byte2 < 0x80
12446 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12447 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12448 : 1))); /* 0xA0A0..0xFFFF */
12449
12450 fontp->baseline_offset
12451 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12452 ? (long) value : 0);
12453 fontp->relative_compose
12454 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12455 ? (long) value : 0);
f78798df
KH
12456 fontp->default_ascent
12457 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12458 ? (long) value : 0);
dc43ef94 12459
06a2c219
GM
12460 /* Set global flag fonts_changed_p to non-zero if the font loaded
12461 has a character with a smaller width than any other character
12462 before, or if the font loaded has a smalle>r height than any
12463 other font loaded before. If this happens, it will make a
12464 glyph matrix reallocation necessary. */
12465 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12466 UNBLOCK_INPUT;
dc43ef94
KH
12467 return fontp;
12468 }
12469}
12470
06a2c219
GM
12471
12472/* Return a pointer to struct font_info of a font named FONTNAME for
12473 frame F. If no such font is loaded, return NULL. */
12474
dc43ef94
KH
12475struct font_info *
12476x_query_font (f, fontname)
12477 struct frame *f;
12478 register char *fontname;
12479{
12480 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12481 int i;
12482
12483 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12484 if (dpyinfo->font_table[i].name
12485 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12486 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12487 return (dpyinfo->font_table + i);
12488 return NULL;
12489}
12490
06a2c219
GM
12491
12492/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12493 `encoder' of the structure. */
12494
12495void
12496x_find_ccl_program (fontp)
12497 struct font_info *fontp;
12498{
a42f54e6 12499 Lisp_Object list, elt;
a6582676 12500
8e713be6 12501 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12502 {
8e713be6 12503 elt = XCAR (list);
a6582676 12504 if (CONSP (elt)
8e713be6
KR
12505 && STRINGP (XCAR (elt))
12506 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12507 >= 0))
a42f54e6
KH
12508 break;
12509 }
12510 if (! NILP (list))
12511 {
d27f8ca7
KH
12512 struct ccl_program *ccl
12513 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12514
8e713be6 12515 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12516 xfree (ccl);
12517 else
12518 fontp->font_encoder = ccl;
a6582676
KH
12519 }
12520}
12521
06a2c219 12522
dc43ef94 12523\f
06a2c219
GM
12524/***********************************************************************
12525 Initialization
12526 ***********************************************************************/
f451eb13 12527
3afe33e7
RS
12528#ifdef USE_X_TOOLKIT
12529static XrmOptionDescRec emacs_options[] = {
12530 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12531 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12532
12533 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12534 XrmoptionSepArg, NULL},
12535 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12536
12537 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12538 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12539 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12540 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12541 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12542 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12543 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12544};
12545#endif /* USE_X_TOOLKIT */
12546
7a13e894
RS
12547static int x_initialized;
12548
29b38361
KH
12549#ifdef MULTI_KBOARD
12550/* Test whether two display-name strings agree up to the dot that separates
12551 the screen number from the server number. */
12552static int
12553same_x_server (name1, name2)
12554 char *name1, *name2;
12555{
12556 int seen_colon = 0;
cf591cc1
RS
12557 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12558 int system_name_length = strlen (system_name);
12559 int length_until_period = 0;
12560
12561 while (system_name[length_until_period] != 0
12562 && system_name[length_until_period] != '.')
12563 length_until_period++;
12564
12565 /* Treat `unix' like an empty host name. */
12566 if (! strncmp (name1, "unix:", 5))
12567 name1 += 4;
12568 if (! strncmp (name2, "unix:", 5))
12569 name2 += 4;
12570 /* Treat this host's name like an empty host name. */
12571 if (! strncmp (name1, system_name, system_name_length)
12572 && name1[system_name_length] == ':')
12573 name1 += system_name_length;
12574 if (! strncmp (name2, system_name, system_name_length)
12575 && name2[system_name_length] == ':')
12576 name2 += system_name_length;
12577 /* Treat this host's domainless name like an empty host name. */
12578 if (! strncmp (name1, system_name, length_until_period)
12579 && name1[length_until_period] == ':')
12580 name1 += length_until_period;
12581 if (! strncmp (name2, system_name, length_until_period)
12582 && name2[length_until_period] == ':')
12583 name2 += length_until_period;
12584
29b38361
KH
12585 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12586 {
12587 if (*name1 == ':')
12588 seen_colon++;
12589 if (seen_colon && *name1 == '.')
12590 return 1;
12591 }
12592 return (seen_colon
12593 && (*name1 == '.' || *name1 == '\0')
12594 && (*name2 == '.' || *name2 == '\0'));
12595}
12596#endif
12597
334208b7 12598struct x_display_info *
1f8255f2 12599x_term_init (display_name, xrm_option, resource_name)
334208b7 12600 Lisp_Object display_name;
1f8255f2
RS
12601 char *xrm_option;
12602 char *resource_name;
dc6f92b8 12603{
334208b7 12604 int connection;
7a13e894 12605 Display *dpy;
334208b7
RS
12606 struct x_display_info *dpyinfo;
12607 XrmDatabase xrdb;
12608
60439948
KH
12609 BLOCK_INPUT;
12610
7a13e894
RS
12611 if (!x_initialized)
12612 {
12613 x_initialize ();
12614 x_initialized = 1;
12615 }
dc6f92b8 12616
3afe33e7 12617#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12618 /* weiner@footloose.sps.mot.com reports that this causes
12619 errors with X11R5:
12620 X protocol error: BadAtom (invalid Atom parameter)
12621 on protocol request 18skiloaf.
12622 So let's not use it until R6. */
12623#ifdef HAVE_X11XTR6
bdcd49ba
RS
12624 XtSetLanguageProc (NULL, NULL, NULL);
12625#endif
12626
7f9c7f94
RS
12627 {
12628 int argc = 0;
12629 char *argv[3];
12630
12631 argv[0] = "";
12632 argc = 1;
12633 if (xrm_option)
12634 {
12635 argv[argc++] = "-xrm";
12636 argv[argc++] = xrm_option;
12637 }
12638 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12639 resource_name, EMACS_CLASS,
12640 emacs_options, XtNumber (emacs_options),
12641 &argc, argv);
39d8bb4d
KH
12642
12643#ifdef HAVE_X11XTR6
10537cb1 12644 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12645 fixup_locale ();
39d8bb4d 12646#endif
7f9c7f94 12647 }
3afe33e7
RS
12648
12649#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12650#ifdef HAVE_X11R5
12651 XSetLocaleModifiers ("");
12652#endif
7a13e894 12653 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12654#endif /* not USE_X_TOOLKIT */
334208b7 12655
7a13e894
RS
12656 /* Detect failure. */
12657 if (dpy == 0)
60439948
KH
12658 {
12659 UNBLOCK_INPUT;
12660 return 0;
12661 }
7a13e894
RS
12662
12663 /* We have definitely succeeded. Record the new connection. */
12664
12665 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12666
29b38361
KH
12667#ifdef MULTI_KBOARD
12668 {
12669 struct x_display_info *share;
12670 Lisp_Object tail;
12671
12672 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12673 share = share->next, tail = XCDR (tail))
12674 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12675 XSTRING (display_name)->data))
12676 break;
12677 if (share)
12678 dpyinfo->kboard = share->kboard;
12679 else
12680 {
12681 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12682 init_kboard (dpyinfo->kboard);
59e755be
KH
12683 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12684 {
12685 char *vendor = ServerVendor (dpy);
12686 dpyinfo->kboard->Vsystem_key_alist
12687 = call1 (Qvendor_specific_keysyms,
12688 build_string (vendor ? vendor : ""));
12689 }
12690
29b38361
KH
12691 dpyinfo->kboard->next_kboard = all_kboards;
12692 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12693 /* Don't let the initial kboard remain current longer than necessary.
12694 That would cause problems if a file loaded on startup tries to
06a2c219 12695 prompt in the mini-buffer. */
0ad5446c
KH
12696 if (current_kboard == initial_kboard)
12697 current_kboard = dpyinfo->kboard;
29b38361
KH
12698 }
12699 dpyinfo->kboard->reference_count++;
12700 }
b9737ad3
KH
12701#endif
12702
7a13e894
RS
12703 /* Put this display on the chain. */
12704 dpyinfo->next = x_display_list;
12705 x_display_list = dpyinfo;
12706
12707 /* Put it on x_display_name_list as well, to keep them parallel. */
12708 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12709 x_display_name_list);
8e713be6 12710 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12711
12712 dpyinfo->display = dpy;
dc6f92b8 12713
dc6f92b8 12714#if 0
7a13e894 12715 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12716#endif /* ! 0 */
7a13e894
RS
12717
12718 dpyinfo->x_id_name
fc932ac6
RS
12719 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12720 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12721 + 2);
12722 sprintf (dpyinfo->x_id_name, "%s@%s",
12723 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12724
12725 /* Figure out which modifier bits mean what. */
334208b7 12726 x_find_modifier_meanings (dpyinfo);
f451eb13 12727
ab648270 12728 /* Get the scroll bar cursor. */
7a13e894 12729 dpyinfo->vertical_scroll_bar_cursor
334208b7 12730 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12731
334208b7
RS
12732 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12733 resource_name, EMACS_CLASS);
12734#ifdef HAVE_XRMSETDATABASE
12735 XrmSetDatabase (dpyinfo->display, xrdb);
12736#else
12737 dpyinfo->display->db = xrdb;
12738#endif
547d9db8 12739 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12740 all versions. */
12741 dpyinfo->xrdb = xrdb;
334208b7
RS
12742
12743 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12744 DefaultScreen (dpyinfo->display));
12745 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12746 &dpyinfo->n_planes);
12747 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12748 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12749 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12750 dpyinfo->grabbed = 0;
12751 dpyinfo->reference_count = 0;
12752 dpyinfo->icon_bitmap_id = -1;
06a2c219 12753 dpyinfo->font_table = NULL;
7a13e894
RS
12754 dpyinfo->n_fonts = 0;
12755 dpyinfo->font_table_size = 0;
12756 dpyinfo->bitmaps = 0;
12757 dpyinfo->bitmaps_size = 0;
12758 dpyinfo->bitmaps_last = 0;
12759 dpyinfo->scratch_cursor_gc = 0;
12760 dpyinfo->mouse_face_mouse_frame = 0;
12761 dpyinfo->mouse_face_deferred_gc = 0;
12762 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12763 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12764 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12765 dpyinfo->mouse_face_window = Qnil;
12766 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12767 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12768 dpyinfo->x_focus_frame = 0;
12769 dpyinfo->x_focus_event_frame = 0;
12770 dpyinfo->x_highlight_frame = 0;
06a2c219 12771 dpyinfo->image_cache = make_image_cache ();
334208b7 12772
06a2c219
GM
12773 {
12774 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12775 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12776 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12777 dpyinfo->resy = pixels * 25.4 / mm;
12778 pixels = DisplayWidth (dpyinfo->display, screen_number);
12779 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12780 dpyinfo->resx = pixels * 25.4 / mm;
12781 }
12782
334208b7
RS
12783 dpyinfo->Xatom_wm_protocols
12784 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12785 dpyinfo->Xatom_wm_take_focus
12786 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12787 dpyinfo->Xatom_wm_save_yourself
12788 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12789 dpyinfo->Xatom_wm_delete_window
12790 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12791 dpyinfo->Xatom_wm_change_state
12792 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12793 dpyinfo->Xatom_wm_configure_denied
12794 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12795 dpyinfo->Xatom_wm_window_moved
12796 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12797 dpyinfo->Xatom_editres
12798 = XInternAtom (dpyinfo->display, "Editres", False);
12799 dpyinfo->Xatom_CLIPBOARD
12800 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12801 dpyinfo->Xatom_TIMESTAMP
12802 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12803 dpyinfo->Xatom_TEXT
12804 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12805 dpyinfo->Xatom_COMPOUND_TEXT
12806 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
12807 dpyinfo->Xatom_DELETE
12808 = XInternAtom (dpyinfo->display, "DELETE", False);
12809 dpyinfo->Xatom_MULTIPLE
12810 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12811 dpyinfo->Xatom_INCR
12812 = XInternAtom (dpyinfo->display, "INCR", False);
12813 dpyinfo->Xatom_EMACS_TMP
12814 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12815 dpyinfo->Xatom_TARGETS
12816 = XInternAtom (dpyinfo->display, "TARGETS", False);
12817 dpyinfo->Xatom_NULL
12818 = XInternAtom (dpyinfo->display, "NULL", False);
12819 dpyinfo->Xatom_ATOM_PAIR
12820 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12821 /* For properties of font. */
12822 dpyinfo->Xatom_PIXEL_SIZE
12823 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12824 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12825 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12826 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12827 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12828 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12829 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12830
06a2c219
GM
12831 /* Ghostscript support. */
12832 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12833 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12834
12835 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12836 False);
12837
547d9db8
KH
12838 dpyinfo->cut_buffers_initialized = 0;
12839
334208b7
RS
12840 connection = ConnectionNumber (dpyinfo->display);
12841 dpyinfo->connection = connection;
12842
dc43ef94 12843 {
5d7cc324
RS
12844 char null_bits[1];
12845
12846 null_bits[0] = 0x00;
dc43ef94
KH
12847
12848 dpyinfo->null_pixel
12849 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12850 null_bits, 1, 1, (long) 0, (long) 0,
12851 1);
12852 }
12853
06a2c219
GM
12854 {
12855 extern int gray_bitmap_width, gray_bitmap_height;
12856 extern unsigned char *gray_bitmap_bits;
12857 dpyinfo->gray
12858 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12859 gray_bitmap_bits,
12860 gray_bitmap_width, gray_bitmap_height,
12861 (unsigned long) 1, (unsigned long) 0, 1);
12862 }
12863
87485d6f
MW
12864#ifdef subprocesses
12865 /* This is only needed for distinguishing keyboard and process input. */
334208b7 12866 if (connection != 0)
7a13e894 12867 add_keyboard_wait_descriptor (connection);
87485d6f 12868#endif
6d4238f3 12869
041b69ac 12870#ifndef F_SETOWN_BUG
dc6f92b8 12871#ifdef F_SETOWN
dc6f92b8 12872#ifdef F_SETOWN_SOCK_NEG
61c3ce62 12873 /* stdin is a socket here */
334208b7 12874 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 12875#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 12876 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
12877#endif /* ! defined (F_SETOWN_SOCK_NEG) */
12878#endif /* ! defined (F_SETOWN) */
041b69ac 12879#endif /* F_SETOWN_BUG */
dc6f92b8
JB
12880
12881#ifdef SIGIO
eee20f6a
KH
12882 if (interrupt_input)
12883 init_sigio (connection);
c118dd06 12884#endif /* ! defined (SIGIO) */
dc6f92b8 12885
51b592fb 12886#ifdef USE_LUCID
f8c39f51 12887#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
12888 /* Make sure that we have a valid font for dialog boxes
12889 so that Xt does not crash. */
12890 {
12891 Display *dpy = dpyinfo->display;
12892 XrmValue d, fr, to;
12893 Font font;
e99db5a1 12894 int count;
51b592fb
RS
12895
12896 d.addr = (XPointer)&dpy;
12897 d.size = sizeof (Display *);
12898 fr.addr = XtDefaultFont;
12899 fr.size = sizeof (XtDefaultFont);
12900 to.size = sizeof (Font *);
12901 to.addr = (XPointer)&font;
e99db5a1 12902 count = x_catch_errors (dpy);
51b592fb
RS
12903 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12904 abort ();
12905 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12906 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 12907 x_uncatch_errors (dpy, count);
51b592fb
RS
12908 }
12909#endif
f8c39f51 12910#endif
51b592fb 12911
60439948
KH
12912 UNBLOCK_INPUT;
12913
7a13e894
RS
12914 return dpyinfo;
12915}
12916\f
12917/* Get rid of display DPYINFO, assuming all frames are already gone,
12918 and without sending any more commands to the X server. */
dc6f92b8 12919
7a13e894
RS
12920void
12921x_delete_display (dpyinfo)
12922 struct x_display_info *dpyinfo;
12923{
12924 delete_keyboard_wait_descriptor (dpyinfo->connection);
12925
12926 /* Discard this display from x_display_name_list and x_display_list.
12927 We can't use Fdelq because that can quit. */
12928 if (! NILP (x_display_name_list)
8e713be6
KR
12929 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
12930 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
12931 else
12932 {
12933 Lisp_Object tail;
12934
12935 tail = x_display_name_list;
8e713be6 12936 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 12937 {
bffcfca9 12938 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 12939 {
8e713be6 12940 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
12941 break;
12942 }
8e713be6 12943 tail = XCDR (tail);
7a13e894
RS
12944 }
12945 }
12946
12947 if (x_display_list == dpyinfo)
12948 x_display_list = dpyinfo->next;
7f9c7f94
RS
12949 else
12950 {
12951 struct x_display_info *tail;
7a13e894 12952
7f9c7f94
RS
12953 for (tail = x_display_list; tail; tail = tail->next)
12954 if (tail->next == dpyinfo)
12955 tail->next = tail->next->next;
12956 }
7a13e894 12957
0d777288
RS
12958#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12959#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
12960 XrmDestroyDatabase (dpyinfo->xrdb);
12961#endif
0d777288 12962#endif
29b38361
KH
12963#ifdef MULTI_KBOARD
12964 if (--dpyinfo->kboard->reference_count == 0)
39f79001 12965 delete_kboard (dpyinfo->kboard);
b9737ad3
KH
12966#endif
12967 xfree (dpyinfo->font_table);
12968 xfree (dpyinfo->x_id_name);
12969 xfree (dpyinfo);
7a13e894
RS
12970}
12971\f
12972/* Set up use of X before we make the first connection. */
12973
06a2c219
GM
12974static struct redisplay_interface x_redisplay_interface =
12975{
12976 x_produce_glyphs,
12977 x_write_glyphs,
12978 x_insert_glyphs,
12979 x_clear_end_of_line,
12980 x_scroll_run,
12981 x_after_update_window_line,
12982 x_update_window_begin,
12983 x_update_window_end,
12984 XTcursor_to,
12985 x_flush,
66ac4b0e
GM
12986 x_get_glyph_overhangs,
12987 x_fix_overlapping_area
06a2c219
GM
12988};
12989
dfcf069d 12990void
7a13e894
RS
12991x_initialize ()
12992{
06a2c219
GM
12993 rif = &x_redisplay_interface;
12994
12995 clear_frame_hook = x_clear_frame;
12996 ins_del_lines_hook = x_ins_del_lines;
12997 change_line_highlight_hook = x_change_line_highlight;
12998 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
12999 ring_bell_hook = XTring_bell;
13000 reset_terminal_modes_hook = XTreset_terminal_modes;
13001 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13002 update_begin_hook = x_update_begin;
13003 update_end_hook = x_update_end;
dc6f92b8
JB
13004 set_terminal_window_hook = XTset_terminal_window;
13005 read_socket_hook = XTread_socket;
b8009dd1 13006 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13007 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13008 mouse_position_hook = XTmouse_position;
f451eb13 13009 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13010 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13011 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13012 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13013 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13014 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13015 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13016
f676886a 13017 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13018 char_ins_del_ok = 0; /* just as fast to write the line */
13019 line_ins_del_ok = 1; /* we'll just blt 'em */
13020 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13021 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13022 off the bottom */
13023 baud_rate = 19200;
13024
7a13e894 13025 x_noop_count = 0;
9ea173e8 13026 last_tool_bar_item = -1;
06a2c219
GM
13027 any_help_event_p = 0;
13028
b30b24cb
RS
13029 /* Try to use interrupt input; if we can't, then start polling. */
13030 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13031
7f9c7f94
RS
13032#ifdef USE_X_TOOLKIT
13033 XtToolkitInitialize ();
13034 Xt_app_con = XtCreateApplicationContext ();
665881ad 13035 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13036
13037 /* Install an asynchronous timer that processes Xt timeout events
13038 every 0.1s. This is necessary because some widget sets use
13039 timeouts internally, for example the LessTif menu bar, or the
13040 Xaw3d scroll bar. When Xt timouts aren't processed, these
13041 widgets don't behave normally. */
13042 {
13043 EMACS_TIME interval;
13044 EMACS_SET_SECS_USECS (interval, 0, 100000);
13045 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13046 }
db74249b 13047#endif
bffcfca9 13048
db74249b 13049#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13050 xaw3d_arrow_scroll = False;
13051 xaw3d_pick_top = True;
7f9c7f94
RS
13052#endif
13053
58769bee 13054 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13055 original error handler. */
e99db5a1 13056 XSetErrorHandler (x_error_handler);
334208b7 13057 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13058
06a2c219 13059 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13060#ifdef SIGWINCH
13061 signal (SIGWINCH, SIG_DFL);
c118dd06 13062#endif /* ! defined (SIGWINCH) */
dc6f92b8 13063
92e2441b 13064 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13065}
55123275 13066
06a2c219 13067
55123275
JB
13068void
13069syms_of_xterm ()
13070{
e99db5a1
RS
13071 staticpro (&x_error_message_string);
13072 x_error_message_string = Qnil;
13073
7a13e894
RS
13074 staticpro (&x_display_name_list);
13075 x_display_name_list = Qnil;
334208b7 13076
ab648270 13077 staticpro (&last_mouse_scroll_bar);
e53cb100 13078 last_mouse_scroll_bar = Qnil;
59e755be
KH
13079
13080 staticpro (&Qvendor_specific_keysyms);
13081 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13082
13083 staticpro (&last_mouse_press_frame);
13084 last_mouse_press_frame = Qnil;
06a2c219
GM
13085
13086 staticpro (&help_echo);
13087 help_echo = Qnil;
13088 staticpro (&previous_help_echo);
13089 previous_help_echo = Qnil;
13090
13091 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13092 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13093For example, if a block cursor is over a tab, it will be drawn as\n\
13094wide as that tab on the display.");
13095 x_stretch_cursor_p = 0;
13096
13097 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13098 "If not nil, Emacs uses toolkit scroll bars.");
13099#if USE_TOOLKIT_SCROLL_BARS
13100 x_toolkit_scroll_bars_p = 1;
13101#else
13102 x_toolkit_scroll_bars_p = 0;
13103#endif
13104
06a2c219
GM
13105 staticpro (&last_mouse_motion_frame);
13106 last_mouse_motion_frame = Qnil;
55123275 13107}
6cf0ae86
RS
13108
13109#endif /* not HAVE_X_WINDOWS */
06a2c219 13110