(ses-exercise-signals): Fix use of character constant.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
abfb6b46 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
dc6f92b8 79#include "gnu.h"
dc6f92b8 80#include "disptab.h"
dc6f92b8 81#include "buffer.h"
f451eb13 82#include "window.h"
3b2fa4e6 83#include "keyboard.h"
bde7c500 84#include "intervals.h"
dfcf069d 85#include "process.h"
bffcfca9 86#include "atimer.h"
8feddab4 87#include "keymap.h"
dc6f92b8 88
d2bd6bc4
RS
89#ifdef USE_X_TOOLKIT
90#include <X11/Shell.h>
91#endif
92
06a2c219
GM
93#ifdef HAVE_SYS_TIME_H
94#include <sys/time.h>
95#endif
96#ifdef HAVE_UNISTD_H
97#include <unistd.h>
98#endif
99
488dd4c4
JD
100#ifdef USE_GTK
101#include "gtkutil.h"
102#endif
103
d624284c 104#ifdef USE_LUCID
55a8b3ed 105extern int xlwmenu_window_p P_ ((Widget w, Window window));
d624284c
PJ
106extern void xlwmenu_redisplay P_ ((Widget));
107#endif
108
488dd4c4 109#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
06a2c219 110
952291d9
GM
111extern void free_frame_menubar P_ ((struct frame *));
112extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
488dd4c4 114#endif
06a2c219 115
488dd4c4 116#ifdef USE_X_TOOLKIT
0fdff6bb
RS
117#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118#define HACK_EDITRES
119extern void _XEditResCheckMessages ();
120#endif /* not NO_EDITRES */
06a2c219
GM
121
122/* Include toolkit specific headers for the scroll bar widget. */
123
124#ifdef USE_TOOLKIT_SCROLL_BARS
125#if defined USE_MOTIF
126#include <Xm/Xm.h> /* for LESSTIF_VERSION */
127#include <Xm/ScrollBar.h>
ec18280f
SM
128#else /* !USE_MOTIF i.e. use Xaw */
129
130#ifdef HAVE_XAW3D
06a2c219 131#include <X11/Xaw3d/Simple.h>
06a2c219
GM
132#include <X11/Xaw3d/Scrollbar.h>
133#define ARROW_SCROLLBAR
134#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
135#else /* !HAVE_XAW3D */
136#include <X11/Xaw/Simple.h>
137#include <X11/Xaw/Scrollbar.h>
138#endif /* !HAVE_XAW3D */
139#ifndef XtNpickTop
140#define XtNpickTop "pickTop"
141#endif /* !XtNpickTop */
142#endif /* !USE_MOTIF */
06a2c219
GM
143#endif /* USE_TOOLKIT_SCROLL_BARS */
144
3afe33e7
RS
145#endif /* USE_X_TOOLKIT */
146
488dd4c4 147#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
b849c413 148#define x_any_window_to_frame x_window_to_frame
5627c40e 149#define x_top_window_to_frame x_window_to_frame
b849c413
RS
150#endif
151
546e6d5b 152#ifdef USE_X_TOOLKIT
d067ea8b 153#include "widget.h"
546e6d5b
RS
154#ifndef XtNinitialState
155#define XtNinitialState "initialState"
156#endif
157#endif
158
06a2c219
GM
159#define abs(x) ((x) < 0 ? -(x) : (x))
160
161#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163\f
3f332ef3 164/* Fringe bitmaps. */
06a2c219 165
3f332ef3 166enum fringe_bitmap_type
06a2c219 167{
3f332ef3 168 NO_FRINGE_BITMAP,
06a2c219
GM
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175};
176
177/* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180#define zv_width 8
976b73d7
KS
181#define zv_height 72
182#define zv_period 3
06a2c219 183static unsigned char zv_bits[] = {
976b73d7
KS
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
186 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
187 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
188 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
189 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
190 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
191 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
06a2c219
GM
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
5bf04520 241/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 242
5bf04520 243Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
244
245/* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
7d0393cf 247
06a2c219 248static Lisp_Object help_echo;
7cea38bc 249static Lisp_Object help_echo_window;
be010514
GM
250static Lisp_Object help_echo_object;
251static int help_echo_pos;
06a2c219
GM
252
253/* Temporary variable for XTread_socket. */
254
255static Lisp_Object previous_help_echo;
256
257/* Non-zero means that a HELP_EVENT has been generated since Emacs
258 start. */
259
260static int any_help_event_p;
261
503e457e
PJ
262/* Non-zero means autoselect window with the mouse cursor. */
263
ee8ceff8 264int mouse_autoselect_window;
5bc62483 265
ee8ceff8 266/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
5bc62483 267static Lisp_Object last_window;
503e457e 268
06a2c219
GM
269/* Non-zero means draw block and hollow cursor as wide as the glyph
270 under it. For example, if a block cursor is over a tab, it will be
271 drawn as wide as that tab on the display. */
272
273int x_stretch_cursor_p;
274
a72d5ce5
GM
275/* Non-zero means make use of UNDERLINE_POSITION font properties. */
276
277int x_use_underline_position_properties;
278
06a2c219
GM
279/* This is a chain of structures for all the X displays currently in
280 use. */
281
334208b7 282struct x_display_info *x_display_list;
dc6f92b8 283
06a2c219
GM
284/* This is a list of cons cells, each of the form (NAME
285 . FONT-LIST-CACHE), one for each element of x_display_list and in
286 the same order. NAME is the name of the frame. FONT-LIST-CACHE
287 records previous values returned by x-list-fonts. */
288
7a13e894 289Lisp_Object x_display_name_list;
f451eb13 290
987d2ad1 291/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
292 This is set by update_begin and looked at by all the XT functions.
293 It is zero while not inside an update. In that case, the XT
294 functions assume that `selected_frame' is the frame to apply to. */
295
d0386f2a 296extern struct frame *updating_frame;
dc6f92b8 297
06a2c219
GM
298/* This is a frame waiting to be auto-raised, within XTread_socket. */
299
0134a210
RS
300struct frame *pending_autoraise_frame;
301
7f9c7f94
RS
302#ifdef USE_X_TOOLKIT
303/* The application context for Xt use. */
304XtAppContext Xt_app_con;
06a2c219
GM
305static String Xt_default_resources[] = {0};
306#endif /* USE_X_TOOLKIT */
665881ad 307
7d0393cf 308/* Nominal cursor position -- where to draw output.
06a2c219
GM
309 HPOS and VPOS are window relative glyph matrix coordinates.
310 X and Y are window relative pixel coordinates. */
dc6f92b8 311
06a2c219 312struct cursor_pos output_cursor;
dc6f92b8 313
bffcfca9
GM
314/* Non-zero means user is interacting with a toolkit scroll bar. */
315
316static int toolkit_scroll_bar_interaction;
dc6f92b8 317
69388238
RS
318/* Mouse movement.
319
06a2c219 320 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
321 so that we would have to call XQueryPointer after each MotionNotify
322 event to ask for another such event. However, this made mouse tracking
323 slow, and there was a bug that made it eventually stop.
324
325 Simply asking for MotionNotify all the time seems to work better.
326
69388238
RS
327 In order to avoid asking for motion events and then throwing most
328 of them away or busy-polling the server for mouse positions, we ask
329 the server for pointer motion hints. This means that we get only
330 one event per group of mouse movements. "Groups" are delimited by
331 other kinds of events (focus changes and button clicks, for
332 example), or by XQueryPointer calls; when one of these happens, we
333 get another MotionNotify event the next time the mouse moves. This
334 is at least as efficient as getting motion events when mouse
335 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 336 is off. */
69388238
RS
337
338/* Where the mouse was last time we reported a mouse event. */
69388238 339
06a2c219
GM
340FRAME_PTR last_mouse_frame;
341static XRectangle last_mouse_glyph;
2237cac9
RS
342static Lisp_Object last_mouse_press_frame;
343
69388238
RS
344/* The scroll bar in which the last X motion event occurred.
345
06a2c219
GM
346 If the last X motion event occurred in a scroll bar, we set this so
347 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
348 an ordinary motion.
349
06a2c219
GM
350 If the last X motion event didn't occur in a scroll bar, we set
351 this to Qnil, to tell XTmouse_position to return an ordinary motion
352 event. */
353
69388238
RS
354static Lisp_Object last_mouse_scroll_bar;
355
69388238
RS
356/* This is a hack. We would really prefer that XTmouse_position would
357 return the time associated with the position it returns, but there
06a2c219 358 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
359 along with the position query. So, we just keep track of the time
360 of the last movement we received, and return that in hopes that
361 it's somewhat accurate. */
06a2c219 362
69388238
RS
363static Time last_mouse_movement_time;
364
06a2c219
GM
365/* Incremented by XTread_socket whenever it really tries to read
366 events. */
367
c0a04927
RS
368#ifdef __STDC__
369static int volatile input_signal_count;
370#else
371static int input_signal_count;
372#endif
373
7a13e894 374/* Used locally within XTread_socket. */
06a2c219 375
7a13e894 376static int x_noop_count;
dc6f92b8 377
7a13e894 378/* Initial values of argv and argc. */
06a2c219 379
7a13e894
RS
380extern char **initial_argv;
381extern int initial_argc;
dc6f92b8 382
7a13e894 383extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 384
06a2c219 385/* Tells if a window manager is present or not. */
7a13e894
RS
386
387extern Lisp_Object Vx_no_window_manager;
dc6f92b8 388
dbd4b028 389extern Lisp_Object Qface, Qmouse_face, Qeql;
b8009dd1 390
dc6f92b8
JB
391extern int errno;
392
dfeccd2d 393/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 394
31ade731 395extern EMACS_INT extra_keyboard_modifiers;
64bb1782 396
98659da6
KG
397/* The keysyms to use for the various modifiers. */
398
399Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
dbd4b028 400Lisp_Object Vx_keysym_table;
98659da6
KG
401static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
402
59e755be 403static Lisp_Object Qvendor_specific_keysyms;
c997eae5 404static Lisp_Object Qlatin_1, Qutf_8;
59e755be 405
952291d9
GM
406extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
407extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 408
7a13e894 409
06a2c219
GM
410/* Enumeration for overriding/changing the face to use for drawing
411 glyphs in x_draw_glyphs. */
412
413enum draw_glyphs_face
414{
415 DRAW_NORMAL_TEXT,
416 DRAW_INVERSE_VIDEO,
417 DRAW_CURSOR,
418 DRAW_MOUSE_FACE,
419 DRAW_IMAGE_RAISED,
420 DRAW_IMAGE_SUNKEN
421};
422
b7f83f9e 423static int cursor_in_mouse_face_p P_ ((struct window *));
fa262c07 424static int clear_mouse_face P_ ((struct x_display_info *));
651f03b6 425static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
499b1844 426static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
651f03b6 427static const XColor *x_color_cells P_ ((Display *, int *));
71b8321e 428static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
429static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
430void x_delete_display P_ ((struct x_display_info *));
431static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
432 unsigned));
433static int fast_find_position P_ ((struct window *, int, int *, int *,
7e376260 434 int *, int *, Lisp_Object));
f9db2310
GM
435static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
436 int *, int *, int *, int *, int));
06a2c219
GM
437static void set_output_cursor P_ ((struct cursor_pos *));
438static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
f9db2310 439 int *, int *, int *, int));
b436feb6
GM
440static void note_mode_line_or_margin_highlight P_ ((struct window *, int,
441 int, int));
06a2c219 442static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
443static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
444static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
445static void show_mouse_face P_ ((struct x_display_info *,
446 enum draw_glyphs_face));
447static int x_io_error_quitter P_ ((Display *));
448int x_catch_errors P_ ((Display *));
449void x_uncatch_errors P_ ((Display *, int));
450void x_lower_frame P_ ((struct frame *));
451void x_scroll_bar_clear P_ ((struct frame *));
452int x_had_errors_p P_ ((Display *));
453void x_wm_set_size_hint P_ ((struct frame *, long, int));
454void x_raise_frame P_ ((struct frame *));
455void x_set_window_size P_ ((struct frame *, int, int, int));
456void x_wm_set_window_state P_ ((struct frame *, int));
457void x_wm_set_icon_pixmap P_ ((struct frame *, int));
458void x_initialize P_ ((void));
459static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
460static int x_compute_min_glyph_bounds P_ ((struct frame *));
461static void x_draw_phys_cursor_glyph P_ ((struct window *,
462 struct glyph_row *,
463 enum draw_glyphs_face));
464static void x_update_end P_ ((struct frame *));
465static void XTframe_up_to_date P_ ((struct frame *));
06a2c219
GM
466static void XTset_terminal_modes P_ ((void));
467static void XTreset_terminal_modes P_ ((void));
468static void XTcursor_to P_ ((int, int, int, int));
469static void x_write_glyphs P_ ((struct glyph *, int));
470static void x_clear_end_of_line P_ ((int));
471static void x_clear_frame P_ ((void));
472static void x_clear_cursor P_ ((struct window *));
473static void frame_highlight P_ ((struct frame *));
474static void frame_unhighlight P_ ((struct frame *));
475static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
cab34f50
JD
476static int x_focus_changed P_ ((int,
477 int,
478 struct x_display_info *,
479 struct frame *,
480 struct input_event *,
481 int));
482static int x_detect_focus_change P_ ((struct x_display_info *,
483 XEvent *,
484 struct input_event *,
485 int));
06a2c219
GM
486static void XTframe_rehighlight P_ ((struct frame *));
487static void x_frame_rehighlight P_ ((struct x_display_info *));
488static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
65c26775
EZ
489static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
490 enum text_cursor_kinds));
06a2c219
GM
491static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
492 XRectangle *));
493static void expose_frame P_ ((struct frame *, int, int, int, int));
82f053ab 494static int expose_window_tree P_ ((struct window *, XRectangle *));
4f53f3b8
RS
495static void expose_overlaps P_ ((struct window *, struct glyph_row *,
496 struct glyph_row *));
a39202f6 497static int expose_window P_ ((struct window *, XRectangle *));
06a2c219
GM
498static void expose_area P_ ((struct window *, struct glyph_row *,
499 XRectangle *, enum glyph_row_area));
82f053ab 500static int expose_line P_ ((struct window *, struct glyph_row *,
06a2c219
GM
501 XRectangle *));
502static void x_update_cursor_in_window_tree P_ ((struct window *, int));
503static void x_update_window_cursor P_ ((struct window *, int));
504static void x_erase_phys_cursor P_ ((struct window *));
505void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
7d0393cf 506static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
976b73d7 507 enum fringe_bitmap_type, int left_p));
06a2c219
GM
508
509static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
510 GC, int));
511static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
3f332ef3 512static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
60626bab
GM
513static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
514 int, int, int, int));
06a2c219 515static void x_flush P_ ((struct frame *f));
952291d9
GM
516static void x_update_begin P_ ((struct frame *));
517static void x_update_window_begin P_ ((struct window *));
518static void x_draw_vertical_border P_ ((struct window *));
519static void x_after_update_window_line P_ ((struct glyph_row *));
520static INLINE void take_vertical_position_into_account P_ ((struct it *));
521static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
522static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
523static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
524 enum scroll_bar_part *,
525 Lisp_Object *, Lisp_Object *,
526 unsigned long *));
d0fa3e56
EZ
527static void x_check_fullscreen P_ ((struct frame *));
528static void x_check_fullscreen_move P_ ((struct frame *));
488dd4c4
JD
529static int handle_one_xevent P_ ((struct x_display_info *,
530 XEvent *,
531 struct input_event **,
532 int *,
533 int *));
534
06a2c219
GM
535
536/* Flush display of frame F, or of all frames if F is null. */
537
538static void
539x_flush (f)
540 struct frame *f;
541{
542 BLOCK_INPUT;
543 if (f == NULL)
544 {
545 Lisp_Object rest, frame;
546 FOR_EACH_FRAME (rest, frame)
547 x_flush (XFRAME (frame));
548 }
549 else if (FRAME_X_P (f))
550 XFlush (FRAME_X_DISPLAY (f));
551 UNBLOCK_INPUT;
552}
553
dc6f92b8 554
06a2c219
GM
555/* Remove calls to XFlush by defining XFlush to an empty replacement.
556 Calls to XFlush should be unnecessary because the X output buffer
557 is flushed automatically as needed by calls to XPending,
558 XNextEvent, or XWindowEvent according to the XFlush man page.
559 XTread_socket calls XPending. Removing XFlush improves
560 performance. */
561
7d0393cf 562#define XFlush(DISPLAY) (void) 0
b8009dd1 563
334208b7 564\f
06a2c219
GM
565/***********************************************************************
566 Debugging
567 ***********************************************************************/
568
9382638d 569#if 0
06a2c219
GM
570
571/* This is a function useful for recording debugging information about
572 the sequence of occurrences in this file. */
9382638d 573
7d0393cf 574struct record
9382638d
KH
575{
576 char *locus;
577 int type;
578};
579
580struct record event_record[100];
581
582int event_record_index;
583
584record_event (locus, type)
585 char *locus;
586 int type;
587{
588 if (event_record_index == sizeof (event_record) / sizeof (struct record))
589 event_record_index = 0;
590
591 event_record[event_record_index].locus = locus;
592 event_record[event_record_index].type = type;
593 event_record_index++;
594}
595
596#endif /* 0 */
06a2c219
GM
597
598
9382638d 599\f
334208b7
RS
600/* Return the struct x_display_info corresponding to DPY. */
601
602struct x_display_info *
603x_display_info_for_display (dpy)
604 Display *dpy;
605{
606 struct x_display_info *dpyinfo;
607
608 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
609 if (dpyinfo->display == dpy)
610 return dpyinfo;
16bd92ea 611
334208b7
RS
612 return 0;
613}
f451eb13 614
06a2c219
GM
615
616\f
617/***********************************************************************
618 Starting and ending an update
619 ***********************************************************************/
7d0393cf 620
06a2c219
GM
621/* Start an update of frame F. This function is installed as a hook
622 for update_begin, i.e. it is called when update_begin is called.
623 This function is called prior to calls to x_update_window_begin for
624 each window being updated. Currently, there is nothing to do here
625 because all interesting stuff is done on a window basis. */
dc6f92b8 626
dfcf069d 627static void
06a2c219 628x_update_begin (f)
f676886a 629 struct frame *f;
58769bee 630{
06a2c219
GM
631 /* Nothing to do. */
632}
dc6f92b8 633
dc6f92b8 634
06a2c219
GM
635/* Start update of window W. Set the global variable updated_window
636 to the window being updated and set output_cursor to the cursor
637 position of W. */
dc6f92b8 638
06a2c219
GM
639static void
640x_update_window_begin (w)
641 struct window *w;
642{
643 struct frame *f = XFRAME (WINDOW_FRAME (w));
644 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
7d0393cf 645
06a2c219
GM
646 updated_window = w;
647 set_output_cursor (&w->cursor);
b8009dd1 648
06a2c219 649 BLOCK_INPUT;
d1bc4182 650
06a2c219 651 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 652 {
514e4681 653 /* Don't do highlighting for mouse motion during the update. */
06a2c219 654 display_info->mouse_face_defer = 1;
37c2c98b 655
06a2c219
GM
656 /* If F needs to be redrawn, simply forget about any prior mouse
657 highlighting. */
9f67f20b 658 if (FRAME_GARBAGED_P (f))
06a2c219
GM
659 display_info->mouse_face_window = Qnil;
660
64f26cf5
GM
661#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
662 their mouse_face_p flag set, which means that they are always
663 unequal to rows in a desired matrix which never have that
664 flag set. So, rows containing mouse-face glyphs are never
665 scrolled, and we don't have to switch the mouse highlight off
666 here to prevent it from being scrolled. */
7d0393cf 667
06a2c219
GM
668 /* Can we tell that this update does not affect the window
669 where the mouse highlight is? If so, no need to turn off.
670 Likewise, don't do anything if the frame is garbaged;
671 in that case, the frame's current matrix that we would use
672 is all wrong, and we will redisplay that line anyway. */
673 if (!NILP (display_info->mouse_face_window)
674 && w == XWINDOW (display_info->mouse_face_window))
514e4681 675 {
06a2c219 676 int i;
514e4681 677
06a2c219
GM
678 for (i = 0; i < w->desired_matrix->nrows; ++i)
679 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
680 break;
681
06a2c219
GM
682 if (i < w->desired_matrix->nrows)
683 clear_mouse_face (display_info);
514e4681 684 }
64f26cf5 685#endif /* 0 */
b8009dd1 686 }
6ccf47d1 687
dc6f92b8
JB
688 UNBLOCK_INPUT;
689}
690
06a2c219
GM
691
692/* Draw a vertical window border to the right of window W if W doesn't
693 have vertical scroll bars. */
694
dfcf069d 695static void
06a2c219
GM
696x_draw_vertical_border (w)
697 struct window *w;
58769bee 698{
06a2c219 699 struct frame *f = XFRAME (WINDOW_FRAME (w));
7d0393cf 700
06a2c219
GM
701 /* Redraw borders between horizontally adjacent windows. Don't
702 do it for frames with vertical scroll bars because either the
703 right scroll bar of a window, or the left scroll bar of its
704 neighbor will suffice as a border. */
705 if (!WINDOW_RIGHTMOST_P (w)
706 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
707 {
708 int x0, x1, y0, y1;
dc6f92b8 709
06a2c219 710 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
3f332ef3 711 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
06a2c219 712 y1 -= 1;
7d0393cf
JB
713
714 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219
GM
715 f->output_data.x->normal_gc, x1, y0, x1, y1);
716 }
717}
7d0393cf
JB
718
719
71b8321e
GM
720/* End update of window W (which is equal to updated_window).
721
722 Draw vertical borders between horizontally adjacent windows, and
723 display W's cursor if CURSOR_ON_P is non-zero.
724
725 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
726 glyphs in mouse-face were overwritten. In that case we have to
727 make sure that the mouse-highlight is properly redrawn.
728
729 W may be a menu bar pseudo-window in case we don't have X toolkit
730 support. Such windows don't have a cursor, so don't display it
731 here. */
06a2c219
GM
732
733static void
71b8321e 734x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 735 struct window *w;
71b8321e 736 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 737{
140330de 738 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7d0393cf 739
06a2c219
GM
740 if (!w->pseudo_window_p)
741 {
742 BLOCK_INPUT;
71b8321e 743
06a2c219
GM
744 if (cursor_on_p)
745 x_display_and_set_cursor (w, 1, output_cursor.hpos,
746 output_cursor.vpos,
747 output_cursor.x, output_cursor.y);
7d0393cf 748
06a2c219
GM
749 x_draw_vertical_border (w);
750 UNBLOCK_INPUT;
751 }
7d0393cf 752
140330de
GM
753 /* If a row with mouse-face was overwritten, arrange for
754 XTframe_up_to_date to redisplay the mouse highlight. */
755 if (mouse_face_overwritten_p)
756 {
757 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
758 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
759 dpyinfo->mouse_face_window = Qnil;
760 }
7d0393cf 761
06a2c219
GM
762 updated_window = NULL;
763}
dc6f92b8 764
dc6f92b8 765
06a2c219
GM
766/* End update of frame F. This function is installed as a hook in
767 update_end. */
768
769static void
770x_update_end (f)
771 struct frame *f;
772{
773 /* Mouse highlight may be displayed again. */
aa8bff2e 774 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 775
06a2c219 776 BLOCK_INPUT;
334208b7 777 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
778 UNBLOCK_INPUT;
779}
b8009dd1 780
06a2c219
GM
781
782/* This function is called from various places in xdisp.c whenever a
783 complete update has been performed. The global variable
784 updated_window is not available here. */
b8009dd1 785
dfcf069d 786static void
b8009dd1 787XTframe_up_to_date (f)
06a2c219 788 struct frame *f;
b8009dd1 789{
06a2c219 790 if (FRAME_X_P (f))
514e4681 791 {
06a2c219 792 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 793
06a2c219
GM
794 if (dpyinfo->mouse_face_deferred_gc
795 || f == dpyinfo->mouse_face_mouse_frame)
796 {
797 BLOCK_INPUT;
798 if (dpyinfo->mouse_face_mouse_frame)
799 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
800 dpyinfo->mouse_face_mouse_x,
801 dpyinfo->mouse_face_mouse_y);
802 dpyinfo->mouse_face_deferred_gc = 0;
803 UNBLOCK_INPUT;
804 }
514e4681 805 }
b8009dd1 806}
06a2c219
GM
807
808
809/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 810 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
811 before DESIRED_ROW is made current. The window being updated is
812 found in updated_window. This function It is called from
813 update_window_line only if it is known that there are differences
814 between bitmaps to be drawn between current row and DESIRED_ROW. */
815
816static void
817x_after_update_window_line (desired_row)
818 struct glyph_row *desired_row;
819{
820 struct window *w = updated_window;
ef253080 821 struct frame *f;
259cf6bc 822 int width, height;
7d0393cf 823
06a2c219 824 xassert (w);
7d0393cf 825
06a2c219
GM
826 if (!desired_row->mode_line_p && !w->pseudo_window_p)
827 {
828 BLOCK_INPUT;
3f332ef3 829 x_draw_row_fringe_bitmaps (w, desired_row);
ef253080
GM
830 UNBLOCK_INPUT;
831 }
7d0393cf 832
ef253080
GM
833 /* When a window has disappeared, make sure that no rest of
834 full-width rows stays visible in the internal border. Could
835 check here if updated_window is the leftmost/rightmost window,
836 but I guess it's not worth doing since vertically split windows
837 are almost never used, internal border is rarely set, and the
838 overhead is very small. */
839 if (windows_or_buffers_changed
840 && desired_row->full_width_p
841 && (f = XFRAME (w->frame),
842 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
843 width != 0)
844 && (height = desired_row->visible_height,
845 height > 0))
ef253080 846 {
ef253080 847 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 848
ef253080
GM
849 /* Internal border is drawn below the tool bar. */
850 if (WINDOWP (f->tool_bar_window)
851 && w == XWINDOW (f->tool_bar_window))
852 y -= width;
7d0393cf 853
ef253080
GM
854 BLOCK_INPUT;
855 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
856 0, y, width, height, False);
857 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
858 f->output_data.x->pixel_width - width,
859 y, width, height, False);
06a2c219
GM
860 UNBLOCK_INPUT;
861 }
862}
863
864
3f332ef3 865/* Draw the bitmap WHICH in one of the left or right fringes of
06a2c219
GM
866 window W. ROW is the glyph row for which to display the bitmap; it
867 determines the vertical position at which the bitmap has to be
868 drawn. */
869
870static void
976b73d7 871x_draw_fringe_bitmap (w, row, which, left_p)
06a2c219
GM
872 struct window *w;
873 struct glyph_row *row;
3f332ef3 874 enum fringe_bitmap_type which;
976b73d7 875 int left_p;
06a2c219
GM
876{
877 struct frame *f = XFRAME (WINDOW_FRAME (w));
878 Display *display = FRAME_X_DISPLAY (f);
879 Window window = FRAME_X_WINDOW (f);
880 int x, y, wd, h, dy;
976b73d7 881 int b1, b2;
b436feb6 882 unsigned char *bits = NULL;
06a2c219
GM
883 Pixmap pixmap;
884 GC gc = f->output_data.x->normal_gc;
885 struct face *face;
886 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
887
888 /* Must clip because of partially visible lines. */
889 x_clip_to_row (w, row, gc, 1);
890
976b73d7
KS
891 /* Convert row to frame coordinates. */
892 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
893
06a2c219
GM
894 switch (which)
895 {
976b73d7
KS
896 case NO_FRINGE_BITMAP:
897 wd = 0;
898 h = 0;
899 break;
900
06a2c219
GM
901 case LEFT_TRUNCATION_BITMAP:
902 wd = left_width;
903 h = left_height;
904 bits = left_bits;
06a2c219 905 break;
7d0393cf 906
06a2c219 907 case OVERLAY_ARROW_BITMAP:
976b73d7
KS
908 wd = ov_width;
909 h = ov_height;
06a2c219 910 bits = ov_bits;
06a2c219 911 break;
7d0393cf 912
06a2c219
GM
913 case RIGHT_TRUNCATION_BITMAP:
914 wd = right_width;
915 h = right_height;
916 bits = right_bits;
06a2c219
GM
917 break;
918
919 case CONTINUED_LINE_BITMAP:
976b73d7
KS
920 wd = continued_width;
921 h = continued_height;
06a2c219 922 bits = continued_bits;
06a2c219 923 break;
7d0393cf 924
06a2c219
GM
925 case CONTINUATION_LINE_BITMAP:
926 wd = continuation_width;
927 h = continuation_height;
928 bits = continuation_bits;
06a2c219
GM
929 break;
930
931 case ZV_LINE_BITMAP:
932 wd = zv_width;
976b73d7
KS
933 h = zv_height - (y % zv_period);
934 bits = zv_bits + (y % zv_period);
06a2c219
GM
935 break;
936
937 default:
938 abort ();
939 }
940
976b73d7
KS
941 /* Clip bitmap if too high. */
942 if (h > row->height)
943 h = row->height;
944
945 /* Set dy to the offset in the row to start drawing the bitmap. */
06a2c219
GM
946 dy = (row->height - h) / 2;
947
976b73d7
KS
948 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
949 PREPARE_FACE_FOR_DISPLAY (f, face);
950
951 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
952 the fringe. */
dbdc9702 953 b1 = b2 = -1;
976b73d7
KS
954 if (left_p)
955 {
956 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
957 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
958 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
959 - wd
960 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
961 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
962 {
963 /* If W has a vertical border to its left, don't draw over it. */
964 int border = ((XFASTINT (w->left) > 0
965 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
966 ? 1 : 0);
967 b1 = (window_box_left (w, -1)
968 - FRAME_X_LEFT_FRINGE_WIDTH (f)
969 + border);
970 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
971 }
972 }
973 else
974 {
975 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
976 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
977 x = (window_box_right (w, -1)
978 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
979 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
980 the fringe. */
981 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
982 {
983 b1 = window_box_right (w, -1);
984 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
985 }
986 }
987
988 if (b1 >= 0)
989 {
990 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
7d0393cf 991
976b73d7
KS
992 /* In case the same realized face is used for fringes and
993 for something displayed in the text (e.g. face `region' on
994 mono-displays, the fill style may have been changed to
995 FillSolid in x_draw_glyph_string_background. */
996 if (face->stipple)
997 XSetFillStyle (display, face->gc, FillOpaqueStippled);
998 else
999 XSetForeground (display, face->gc, face->background);
7d0393cf 1000
976b73d7
KS
1001 XFillRectangle (display, window, face->gc,
1002 b1,
1003 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1004 row->y)),
1005 b2,
1006 row->visible_height);
1007 if (!face->stipple)
1008 XSetForeground (display, face->gc, face->foreground);
1009 }
1010
dbdc9702
GM
1011 if (which != NO_FRINGE_BITMAP)
1012 {
1013 /* Draw the bitmap. I believe these small pixmaps can be cached
1014 by the server. */
1015 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
1016 face->foreground,
1017 face->background, depth);
1018 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
1019 XFreePixmap (display, pixmap);
1020 }
7d0393cf 1021
06a2c219
GM
1022 XSetClipMask (display, gc, None);
1023}
1024
1025
3f332ef3 1026/* Draw fringe bitmaps for glyph row ROW on window W. Call this
06a2c219
GM
1027 function with input blocked. */
1028
1029static void
3f332ef3 1030x_draw_row_fringe_bitmaps (w, row)
06a2c219
GM
1031 struct window *w;
1032 struct glyph_row *row;
1033{
1034 struct frame *f = XFRAME (w->frame);
3f332ef3 1035 enum fringe_bitmap_type bitmap;
06a2c219
GM
1036
1037 xassert (interrupt_input_blocked);
1038
1039 /* If row is completely invisible, because of vscrolling, we
1040 don't have to draw anything. */
1041 if (row->visible_height <= 0)
1042 return;
1043
976b73d7
KS
1044 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1045 {
1046 /* Decide which bitmap to draw in the left fringe. */
1047 if (row->overlay_arrow_p)
1048 bitmap = OVERLAY_ARROW_BITMAP;
1049 else if (row->truncated_on_left_p)
1050 bitmap = LEFT_TRUNCATION_BITMAP;
1051 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1052 bitmap = CONTINUATION_LINE_BITMAP;
1053 else if (row->indicate_empty_line_p)
1054 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1055 else
976b73d7 1056 bitmap = NO_FRINGE_BITMAP;
06a2c219 1057
976b73d7
KS
1058 x_draw_fringe_bitmap (w, row, bitmap, 1);
1059 }
06a2c219 1060
976b73d7 1061 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
06a2c219 1062 {
976b73d7
KS
1063 /* Decide which bitmap to draw in the right fringe. */
1064 if (row->truncated_on_right_p)
1065 bitmap = RIGHT_TRUNCATION_BITMAP;
1066 else if (row->continued_p)
1067 bitmap = CONTINUED_LINE_BITMAP;
1068 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1069 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1070 else
976b73d7 1071 bitmap = NO_FRINGE_BITMAP;
06a2c219 1072
976b73d7
KS
1073 x_draw_fringe_bitmap (w, row, bitmap, 0);
1074 }
06a2c219
GM
1075}
1076
dc6f92b8 1077\f
06a2c219
GM
1078
1079/* This is called when starting Emacs and when restarting after
1080 suspend. When starting Emacs, no X window is mapped. And nothing
1081 must be done to Emacs's own window if it is suspended (though that
1082 rarely happens). */
dc6f92b8 1083
dfcf069d 1084static void
dc6f92b8
JB
1085XTset_terminal_modes ()
1086{
1087}
1088
06a2c219
GM
1089/* This is called when exiting or suspending Emacs. Exiting will make
1090 the X-windows go away, and suspending requires no action. */
dc6f92b8 1091
dfcf069d 1092static void
dc6f92b8
JB
1093XTreset_terminal_modes ()
1094{
dc6f92b8 1095}
06a2c219
GM
1096
1097
dc6f92b8 1098\f
06a2c219
GM
1099/***********************************************************************
1100 Output Cursor
1101 ***********************************************************************/
1102
1103/* Set the global variable output_cursor to CURSOR. All cursor
1104 positions are relative to updated_window. */
dc6f92b8 1105
dfcf069d 1106static void
06a2c219
GM
1107set_output_cursor (cursor)
1108 struct cursor_pos *cursor;
dc6f92b8 1109{
06a2c219
GM
1110 output_cursor.hpos = cursor->hpos;
1111 output_cursor.vpos = cursor->vpos;
1112 output_cursor.x = cursor->x;
1113 output_cursor.y = cursor->y;
1114}
1115
1116
1117/* Set a nominal cursor position.
dc6f92b8 1118
06a2c219
GM
1119 HPOS and VPOS are column/row positions in a window glyph matrix. X
1120 and Y are window text area relative pixel positions.
7d0393cf 1121
06a2c219
GM
1122 If this is done during an update, updated_window will contain the
1123 window that is being updated and the position is the future output
1124 cursor position for that window. If updated_window is null, use
1125 selected_window and display the cursor at the given position. */
1126
1127static void
1128XTcursor_to (vpos, hpos, y, x)
1129 int vpos, hpos, y, x;
1130{
1131 struct window *w;
1132
1133 /* If updated_window is not set, work on selected_window. */
1134 if (updated_window)
1135 w = updated_window;
1136 else
1137 w = XWINDOW (selected_window);
dbcb258a 1138
06a2c219
GM
1139 /* Set the output cursor. */
1140 output_cursor.hpos = hpos;
1141 output_cursor.vpos = vpos;
1142 output_cursor.x = x;
1143 output_cursor.y = y;
dc6f92b8 1144
06a2c219
GM
1145 /* If not called as part of an update, really display the cursor.
1146 This will also set the cursor position of W. */
1147 if (updated_window == NULL)
dc6f92b8
JB
1148 {
1149 BLOCK_INPUT;
06a2c219 1150 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1151 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1152 UNBLOCK_INPUT;
1153 }
1154}
dc43ef94 1155
06a2c219
GM
1156
1157\f
1158/***********************************************************************
1159 Display Iterator
1160 ***********************************************************************/
1161
1162/* Function prototypes of this page. */
1163
1164static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1165 struct glyph *,
ee569018
KH
1166 XChar2b *,
1167 int *));
06a2c219 1168static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
abfb6b46
GM
1169 int, XChar2b *, int,
1170 int));
06a2c219
GM
1171static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1172static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1173static void x_append_glyph P_ ((struct it *));
b4192550 1174static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1175static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1176 int, int, double));
1177static void x_produce_glyphs P_ ((struct it *));
06a2c219 1178static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1179
1180
e2ef8ee6
GM
1181/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1182 is not contained in the font. */
dc43ef94 1183
06a2c219 1184static INLINE XCharStruct *
ee569018 1185x_per_char_metric (font, char2b)
06a2c219
GM
1186 XFontStruct *font;
1187 XChar2b *char2b;
1188{
1189 /* The result metric information. */
1190 XCharStruct *pcm = NULL;
dc6f92b8 1191
06a2c219 1192 xassert (font && char2b);
dc6f92b8 1193
06a2c219 1194 if (font->per_char != NULL)
dc6f92b8 1195 {
06a2c219 1196 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1197 {
06a2c219
GM
1198 /* min_char_or_byte2 specifies the linear character index
1199 corresponding to the first element of the per_char array,
1200 max_char_or_byte2 is the index of the last character. A
1201 character with non-zero CHAR2B->byte1 is not in the font.
1202 A character with byte2 less than min_char_or_byte2 or
1203 greater max_char_or_byte2 is not in the font. */
1204 if (char2b->byte1 == 0
1205 && char2b->byte2 >= font->min_char_or_byte2
1206 && char2b->byte2 <= font->max_char_or_byte2)
1207 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1208 }
06a2c219 1209 else
dc6f92b8 1210 {
06a2c219
GM
1211 /* If either min_byte1 or max_byte1 are nonzero, both
1212 min_char_or_byte2 and max_char_or_byte2 are less than
1213 256, and the 2-byte character index values corresponding
1214 to the per_char array element N (counting from 0) are:
1215
1216 byte1 = N/D + min_byte1
1217 byte2 = N\D + min_char_or_byte2
1218
1219 where:
1220
1221 D = max_char_or_byte2 - min_char_or_byte2 + 1
1222 / = integer division
1223 \ = integer modulus */
1224 if (char2b->byte1 >= font->min_byte1
1225 && char2b->byte1 <= font->max_byte1
1226 && char2b->byte2 >= font->min_char_or_byte2
1227 && char2b->byte2 <= font->max_char_or_byte2)
1228 {
1229 pcm = (font->per_char
1230 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1231 * (char2b->byte1 - font->min_byte1))
1232 + (char2b->byte2 - font->min_char_or_byte2));
1233 }
dc6f92b8 1234 }
06a2c219
GM
1235 }
1236 else
1237 {
1238 /* If the per_char pointer is null, all glyphs between the first
1239 and last character indexes inclusive have the same
1240 information, as given by both min_bounds and max_bounds. */
1241 if (char2b->byte2 >= font->min_char_or_byte2
1242 && char2b->byte2 <= font->max_char_or_byte2)
1243 pcm = &font->max_bounds;
1244 }
dc6f92b8 1245
ee569018 1246 return ((pcm == NULL
3e71d8f2 1247 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1248 ? NULL : pcm);
06a2c219 1249}
b73b6aaf 1250
57b03282 1251
06a2c219
GM
1252/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1253 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1254
06a2c219
GM
1255static INLINE void
1256x_encode_char (c, char2b, font_info)
1257 int c;
1258 XChar2b *char2b;
1259 struct font_info *font_info;
1260{
1261 int charset = CHAR_CHARSET (c);
1262 XFontStruct *font = font_info->font;
1263
1264 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1265 This may be either a program in a special encoder language or a
1266 fixed encoding. */
1267 if (font_info->font_encoder)
1268 {
1269 /* It's a program. */
1270 struct ccl_program *ccl = font_info->font_encoder;
1271
1272 if (CHARSET_DIMENSION (charset) == 1)
1273 {
1274 ccl->reg[0] = charset;
1275 ccl->reg[1] = char2b->byte2;
6e666629 1276 ccl->reg[2] = -1;
06a2c219
GM
1277 }
1278 else
1279 {
1280 ccl->reg[0] = charset;
1281 ccl->reg[1] = char2b->byte1;
1282 ccl->reg[2] = char2b->byte2;
1283 }
7d0393cf 1284
06a2c219 1285 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
7d0393cf 1286
06a2c219
GM
1287 /* We assume that MSBs are appropriately set/reset by CCL
1288 program. */
1289 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1290 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1291 else
1292 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1293 }
1294 else if (font_info->encoding[charset])
1295 {
1296 /* Fixed encoding scheme. See fontset.h for the meaning of the
1297 encoding numbers. */
1298 int enc = font_info->encoding[charset];
7d0393cf 1299
06a2c219
GM
1300 if ((enc == 1 || enc == 2)
1301 && CHARSET_DIMENSION (charset) == 2)
1302 char2b->byte1 |= 0x80;
7d0393cf 1303
06a2c219
GM
1304 if (enc == 1 || enc == 3)
1305 char2b->byte2 |= 0x80;
1306 }
1307}
1308
1309
1310/* Get face and two-byte form of character C in face FACE_ID on frame
1311 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
abfb6b46
GM
1312 means we want to display multibyte text. DISPLAY_P non-zero means
1313 make sure that X resources for the face returned are allocated.
1314 Value is a pointer to a realized face that is ready for display if
1315 DISPLAY_P is non-zero. */
06a2c219
GM
1316
1317static INLINE struct face *
abfb6b46 1318x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
06a2c219
GM
1319 struct frame *f;
1320 int c, face_id;
1321 XChar2b *char2b;
abfb6b46 1322 int multibyte_p, display_p;
06a2c219
GM
1323{
1324 struct face *face = FACE_FROM_ID (f, face_id);
1325
1326 if (!multibyte_p)
1327 {
1328 /* Unibyte case. We don't have to encode, but we have to make
1329 sure to use a face suitable for unibyte. */
1330 char2b->byte1 = 0;
1331 char2b->byte2 = c;
ee569018
KH
1332 face_id = FACE_FOR_CHAR (f, face, c);
1333 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1334 }
1335 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1336 {
1337 /* Case of ASCII in a face known to fit ASCII. */
1338 char2b->byte1 = 0;
1339 char2b->byte2 = c;
1340 }
1341 else
1342 {
1343 int c1, c2, charset;
7d0393cf 1344
06a2c219
GM
1345 /* Split characters into bytes. If c2 is -1 afterwards, C is
1346 really a one-byte character so that byte1 is zero. */
1347 SPLIT_CHAR (c, charset, c1, c2);
1348 if (c2 > 0)
1349 char2b->byte1 = c1, char2b->byte2 = c2;
1350 else
1351 char2b->byte1 = 0, char2b->byte2 = c1;
1352
06a2c219 1353 /* Maybe encode the character in *CHAR2B. */
ee569018 1354 if (face->font != NULL)
06a2c219
GM
1355 {
1356 struct font_info *font_info
1357 = FONT_INFO_FROM_ID (f, face->font_info_id);
1358 if (font_info)
ee569018 1359 x_encode_char (c, char2b, font_info);
06a2c219
GM
1360 }
1361 }
1362
1363 /* Make sure X resources of the face are allocated. */
abfb6b46
GM
1364 if (display_p)
1365 {
1366 xassert (face != NULL);
1367 PREPARE_FACE_FOR_DISPLAY (f, face);
1368 }
7d0393cf 1369
06a2c219
GM
1370 return face;
1371}
1372
1373
1374/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1375 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1376 a pointer to a realized face that is ready for display. */
1377
1378static INLINE struct face *
ee569018 1379x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1380 struct frame *f;
1381 struct glyph *glyph;
1382 XChar2b *char2b;
ee569018 1383 int *two_byte_p;
06a2c219
GM
1384{
1385 struct face *face;
1386
1387 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1388 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1389
ee569018
KH
1390 if (two_byte_p)
1391 *two_byte_p = 0;
1392
06a2c219
GM
1393 if (!glyph->multibyte_p)
1394 {
1395 /* Unibyte case. We don't have to encode, but we have to make
1396 sure to use a face suitable for unibyte. */
1397 char2b->byte1 = 0;
43d120d8 1398 char2b->byte2 = glyph->u.ch;
06a2c219 1399 }
43d120d8
KH
1400 else if (glyph->u.ch < 128
1401 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1402 {
1403 /* Case of ASCII in a face known to fit ASCII. */
1404 char2b->byte1 = 0;
43d120d8 1405 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1406 }
1407 else
1408 {
1409 int c1, c2, charset;
7d0393cf 1410
06a2c219
GM
1411 /* Split characters into bytes. If c2 is -1 afterwards, C is
1412 really a one-byte character so that byte1 is zero. */
43d120d8 1413 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1414 if (c2 > 0)
1415 char2b->byte1 = c1, char2b->byte2 = c2;
1416 else
1417 char2b->byte1 = 0, char2b->byte2 = c1;
1418
1419 /* Maybe encode the character in *CHAR2B. */
1420 if (charset != CHARSET_ASCII)
1421 {
1422 struct font_info *font_info
1423 = FONT_INFO_FROM_ID (f, face->font_info_id);
1424 if (font_info)
1425 {
43d120d8 1426 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1427 if (two_byte_p)
1428 *two_byte_p
1429 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1430 }
1431 }
1432 }
1433
1434 /* Make sure X resources of the face are allocated. */
1435 xassert (face != NULL);
1436 PREPARE_FACE_FOR_DISPLAY (f, face);
1437 return face;
1438}
1439
1440
7d0393cf 1441/* Store one glyph for IT->char_to_display in IT->glyph_row.
06a2c219
GM
1442 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1443
1444static INLINE void
1445x_append_glyph (it)
1446 struct it *it;
1447{
1448 struct glyph *glyph;
1449 enum glyph_row_area area = it->area;
7d0393cf 1450
06a2c219
GM
1451 xassert (it->glyph_row);
1452 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
7d0393cf 1453
06a2c219
GM
1454 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1455 if (glyph < it->glyph_row->glyphs[area + 1])
1456 {
06a2c219
GM
1457 glyph->charpos = CHARPOS (it->position);
1458 glyph->object = it->object;
88d75730 1459 glyph->pixel_width = it->pixel_width;
06a2c219 1460 glyph->voffset = it->voffset;
88d75730 1461 glyph->type = CHAR_GLYPH;
06a2c219 1462 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1463 glyph->left_box_line_p = it->start_of_box_run_p;
1464 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1465 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1466 || it->phys_descent > it->descent);
88d75730 1467 glyph->padding_p = 0;
ee569018 1468 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1469 glyph->face_id = it->face_id;
1470 glyph->u.ch = it->char_to_display;
06a2c219
GM
1471 ++it->glyph_row->used[area];
1472 }
1473}
1474
7d0393cf 1475/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
b4192550
KH
1476 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1477
1478static INLINE void
1479x_append_composite_glyph (it)
1480 struct it *it;
1481{
1482 struct glyph *glyph;
1483 enum glyph_row_area area = it->area;
7d0393cf 1484
b4192550 1485 xassert (it->glyph_row);
7d0393cf 1486
b4192550
KH
1487 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1488 if (glyph < it->glyph_row->glyphs[area + 1])
1489 {
b4192550
KH
1490 glyph->charpos = CHARPOS (it->position);
1491 glyph->object = it->object;
88d75730 1492 glyph->pixel_width = it->pixel_width;
b4192550 1493 glyph->voffset = it->voffset;
88d75730 1494 glyph->type = COMPOSITE_GLYPH;
b4192550 1495 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1496 glyph->left_box_line_p = it->start_of_box_run_p;
1497 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1498 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1499 || it->phys_descent > it->descent);
88d75730
GM
1500 glyph->padding_p = 0;
1501 glyph->glyph_not_available_p = 0;
1502 glyph->face_id = it->face_id;
1503 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1504 ++it->glyph_row->used[area];
1505 }
1506}
1507
06a2c219
GM
1508
1509/* Change IT->ascent and IT->height according to the setting of
1510 IT->voffset. */
1511
1512static INLINE void
1513take_vertical_position_into_account (it)
1514 struct it *it;
1515{
1516 if (it->voffset)
1517 {
1518 if (it->voffset < 0)
1519 /* Increase the ascent so that we can display the text higher
1520 in the line. */
1521 it->ascent += abs (it->voffset);
1522 else
1523 /* Increase the descent so that we can display the text lower
1524 in the line. */
1525 it->descent += it->voffset;
1526 }
1527}
1528
1529
1530/* Produce glyphs/get display metrics for the image IT is loaded with.
1531 See the description of struct display_iterator in dispextern.h for
1532 an overview of struct display_iterator. */
1533
1534static void
1535x_produce_image_glyph (it)
1536 struct it *it;
1537{
1538 struct image *img;
1539 struct face *face;
1540
1541 xassert (it->what == IT_IMAGE);
1542
1543 face = FACE_FROM_ID (it->f, it->face_id);
1544 img = IMAGE_FROM_ID (it->f, it->image_id);
1545 xassert (img);
1546
1547 /* Make sure X resources of the face and image are loaded. */
1548 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1549 prepare_image_for_display (it->f, img);
1550
95af8492 1551 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1552 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1553 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1554
1555 it->nglyphs = 1;
7d0393cf 1556
06a2c219
GM
1557 if (face->box != FACE_NO_BOX)
1558 {
ea2ba0d4
KH
1559 if (face->box_line_width > 0)
1560 {
1561 it->ascent += face->box_line_width;
1562 it->descent += face->box_line_width;
1563 }
7d0393cf 1564
06a2c219 1565 if (it->start_of_box_run_p)
ea2ba0d4 1566 it->pixel_width += abs (face->box_line_width);
06a2c219 1567 if (it->end_of_box_run_p)
ea2ba0d4 1568 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1569 }
1570
1571 take_vertical_position_into_account (it);
7d0393cf 1572
06a2c219
GM
1573 if (it->glyph_row)
1574 {
1575 struct glyph *glyph;
1576 enum glyph_row_area area = it->area;
7d0393cf 1577
06a2c219
GM
1578 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1579 if (glyph < it->glyph_row->glyphs[area + 1])
1580 {
06a2c219
GM
1581 glyph->charpos = CHARPOS (it->position);
1582 glyph->object = it->object;
88d75730 1583 glyph->pixel_width = it->pixel_width;
06a2c219 1584 glyph->voffset = it->voffset;
88d75730 1585 glyph->type = IMAGE_GLYPH;
06a2c219 1586 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1587 glyph->left_box_line_p = it->start_of_box_run_p;
1588 glyph->right_box_line_p = it->end_of_box_run_p;
1589 glyph->overlaps_vertically_p = 0;
1590 glyph->padding_p = 0;
1591 glyph->glyph_not_available_p = 0;
1592 glyph->face_id = it->face_id;
1593 glyph->u.img_id = img->id;
06a2c219
GM
1594 ++it->glyph_row->used[area];
1595 }
1596 }
1597}
1598
1599
1600/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
7d0393cf
JB
1601 of the glyph, WIDTH and HEIGHT are the width and height of the
1602 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
06a2c219 1603 ascent of the glyph (0 <= ASCENT <= 1). */
7d0393cf 1604
06a2c219
GM
1605static void
1606x_append_stretch_glyph (it, object, width, height, ascent)
1607 struct it *it;
1608 Lisp_Object object;
1609 int width, height;
1610 double ascent;
1611{
1612 struct glyph *glyph;
1613 enum glyph_row_area area = it->area;
1614
1615 xassert (ascent >= 0 && ascent <= 1);
7d0393cf 1616
06a2c219
GM
1617 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1618 if (glyph < it->glyph_row->glyphs[area + 1])
1619 {
06a2c219
GM
1620 glyph->charpos = CHARPOS (it->position);
1621 glyph->object = object;
88d75730 1622 glyph->pixel_width = width;
06a2c219 1623 glyph->voffset = it->voffset;
88d75730 1624 glyph->type = STRETCH_GLYPH;
06a2c219 1625 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1626 glyph->left_box_line_p = it->start_of_box_run_p;
1627 glyph->right_box_line_p = it->end_of_box_run_p;
1628 glyph->overlaps_vertically_p = 0;
1629 glyph->padding_p = 0;
1630 glyph->glyph_not_available_p = 0;
1631 glyph->face_id = it->face_id;
1632 glyph->u.stretch.ascent = height * ascent;
1633 glyph->u.stretch.height = height;
06a2c219
GM
1634 ++it->glyph_row->used[area];
1635 }
1636}
1637
1638
1639/* Produce a stretch glyph for iterator IT. IT->object is the value
1640 of the glyph property displayed. The value must be a list
1641 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1642 being recognized:
1643
1644 1. `:width WIDTH' specifies that the space should be WIDTH *
7d0393cf 1645 canonical char width wide. WIDTH may be an integer or floating
06a2c219
GM
1646 point number.
1647
1648 2. `:relative-width FACTOR' specifies that the width of the stretch
1649 should be computed from the width of the first character having the
1650 `glyph' property, and should be FACTOR times that width.
1651
1652 3. `:align-to HPOS' specifies that the space should be wide enough
1653 to reach HPOS, a value in canonical character units.
1654
7d0393cf 1655 Exactly one of the above pairs must be present.
06a2c219
GM
1656
1657 4. `:height HEIGHT' specifies that the height of the stretch produced
1658 should be HEIGHT, measured in canonical character units.
1659
269b7745 1660 5. `:relative-height FACTOR' specifies that the height of the
06a2c219
GM
1661 stretch should be FACTOR times the height of the characters having
1662 the glyph property.
1663
1664 Either none or exactly one of 4 or 5 must be present.
1665
1666 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1667 of the stretch should be used for the ascent of the stretch.
1668 ASCENT must be in the range 0 <= ASCENT <= 100. */
1669
1670#define NUMVAL(X) \
1671 ((INTEGERP (X) || FLOATP (X)) \
1672 ? XFLOATINT (X) \
1673 : - 1)
1674
1675
1676static void
1677x_produce_stretch_glyph (it)
1678 struct it *it;
1679{
1680 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1681#if GLYPH_DEBUG
1682 extern Lisp_Object Qspace;
1683#endif
1684 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1685 extern Lisp_Object QCrelative_width, QCrelative_height;
1686 extern Lisp_Object QCalign_to;
1687 Lisp_Object prop, plist;
1688 double width = 0, height = 0, ascent = 0;
1689 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1690 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1691
1692 PREPARE_FACE_FOR_DISPLAY (it->f, face);
7d0393cf 1693
06a2c219
GM
1694 /* List should start with `space'. */
1695 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1696 plist = XCDR (it->object);
1697
1698 /* Compute the width of the stretch. */
1699 if (prop = Fplist_get (plist, QCwidth),
1700 NUMVAL (prop) > 0)
1701 /* Absolute width `:width WIDTH' specified and valid. */
1702 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1703 else if (prop = Fplist_get (plist, QCrelative_width),
1704 NUMVAL (prop) > 0)
1705 {
1706 /* Relative width `:relative-width FACTOR' specified and valid.
1707 Compute the width of the characters having the `glyph'
1708 property. */
1709 struct it it2;
1710 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7d0393cf 1711
06a2c219
GM
1712 it2 = *it;
1713 if (it->multibyte_p)
1714 {
1715 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1716 - IT_BYTEPOS (*it));
1717 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1718 }
1719 else
1720 it2.c = *p, it2.len = 1;
1721
1722 it2.glyph_row = NULL;
1723 it2.what = IT_CHARACTER;
1724 x_produce_glyphs (&it2);
1725 width = NUMVAL (prop) * it2.pixel_width;
1726 }
1727 else if (prop = Fplist_get (plist, QCalign_to),
1728 NUMVAL (prop) > 0)
1729 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1730 else
1731 /* Nothing specified -> width defaults to canonical char width. */
1732 width = CANON_X_UNIT (it->f);
7d0393cf 1733
06a2c219
GM
1734 /* Compute height. */
1735 if (prop = Fplist_get (plist, QCheight),
1736 NUMVAL (prop) > 0)
1737 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1738 else if (prop = Fplist_get (plist, QCrelative_height),
1739 NUMVAL (prop) > 0)
1740 height = FONT_HEIGHT (font) * NUMVAL (prop);
1741 else
1742 height = FONT_HEIGHT (font);
1743
7d0393cf 1744 /* Compute percentage of height used for ascent. If
06a2c219
GM
1745 `:ascent ASCENT' is present and valid, use that. Otherwise,
1746 derive the ascent from the font in use. */
1747 if (prop = Fplist_get (plist, QCascent),
1748 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1749 ascent = NUMVAL (prop) / 100.0;
1750 else
1751 ascent = (double) font->ascent / FONT_HEIGHT (font);
1752
1753 if (width <= 0)
1754 width = 1;
1755 if (height <= 0)
1756 height = 1;
1757
1758 if (it->glyph_row)
1759 {
1760 Lisp_Object object = it->stack[it->sp - 1].string;
1761 if (!STRINGP (object))
1762 object = it->w->buffer;
1763 x_append_stretch_glyph (it, object, width, height, ascent);
1764 }
1765
1766 it->pixel_width = width;
66ac4b0e
GM
1767 it->ascent = it->phys_ascent = height * ascent;
1768 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1769 it->nglyphs = 1;
1770
1771 if (face->box != FACE_NO_BOX)
1772 {
ea2ba0d4
KH
1773 if (face->box_line_width > 0)
1774 {
1775 it->ascent += face->box_line_width;
1776 it->descent += face->box_line_width;
1777 }
7d0393cf 1778
06a2c219 1779 if (it->start_of_box_run_p)
ea2ba0d4 1780 it->pixel_width += abs (face->box_line_width);
06a2c219 1781 if (it->end_of_box_run_p)
ea2ba0d4 1782 it->pixel_width += abs (face->box_line_width);
06a2c219 1783 }
7d0393cf 1784
06a2c219
GM
1785 take_vertical_position_into_account (it);
1786}
1787
b4192550
KH
1788/* Return proper value to be used as baseline offset of font that has
1789 ASCENT and DESCENT to draw characters by the font at the vertical
1790 center of the line of frame F.
1791
1792 Here, out task is to find the value of BOFF in the following figure;
1793
1794 -------------------------+-----------+-
1795 -+-+---------+-+ | |
1796 | | | | | |
1797 | | | | F_ASCENT F_HEIGHT
1798 | | | ASCENT | |
1799 HEIGHT | | | | |
1800 | | |-|-+------+-----------|------- baseline
1801 | | | | BOFF | |
1802 | |---------|-+-+ | |
1803 | | | DESCENT | |
1804 -+-+---------+-+ F_DESCENT |
1805 -------------------------+-----------+-
1806
1807 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1808 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1809 DESCENT = FONT->descent
1810 HEIGHT = FONT_HEIGHT (FONT)
1811 F_DESCENT = (F->output_data.x->font->descent
1812 - F->output_data.x->baseline_offset)
1813 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1814*/
1815
458f45fa
KH
1816#define VCENTER_BASELINE_OFFSET(FONT, F) \
1817 ((FONT)->descent \
1818 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1819 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1820 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1821
1822/* Produce glyphs/get display metrics for the display element IT is
1823 loaded with. See the description of struct display_iterator in
1824 dispextern.h for an overview of struct display_iterator. */
1825
1826static void
1827x_produce_glyphs (it)
1828 struct it *it;
1829{
ee569018
KH
1830 it->glyph_not_available_p = 0;
1831
06a2c219
GM
1832 if (it->what == IT_CHARACTER)
1833 {
1834 XChar2b char2b;
1835 XFontStruct *font;
ee569018 1836 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1837 XCharStruct *pcm;
06a2c219 1838 int font_not_found_p;
b4192550
KH
1839 struct font_info *font_info;
1840 int boff; /* baseline offset */
a4249304
KH
1841 /* We may change it->multibyte_p upon unibyte<->multibyte
1842 conversion. So, save the current value now and restore it
1843 later.
1844
1845 Note: It seems that we don't have to record multibyte_p in
1846 struct glyph because the character code itself tells if or
1847 not the character is multibyte. Thus, in the future, we must
1848 consider eliminating the field `multibyte_p' in the struct
c347a1c3 1849 glyph. */
a4249304 1850 int saved_multibyte_p = it->multibyte_p;
06a2c219 1851
ee569018
KH
1852 /* Maybe translate single-byte characters to multibyte, or the
1853 other way. */
06a2c219 1854 it->char_to_display = it->c;
ee569018 1855 if (!ASCII_BYTE_P (it->c))
06a2c219 1856 {
ee569018
KH
1857 if (unibyte_display_via_language_environment
1858 && SINGLE_BYTE_CHAR_P (it->c)
1859 && (it->c >= 0240
1860 || !NILP (Vnonascii_translation_table)))
1861 {
1862 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1863 it->multibyte_p = 1;
ee569018
KH
1864 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1865 face = FACE_FROM_ID (it->f, it->face_id);
1866 }
1867 else if (!SINGLE_BYTE_CHAR_P (it->c)
1868 && !it->multibyte_p)
1869 {
c347a1c3 1870 it->multibyte_p = 1;
ee569018
KH
1871 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1872 face = FACE_FROM_ID (it->f, it->face_id);
1873 }
06a2c219 1874 }
7d0393cf 1875
ee569018
KH
1876 /* Get font to use. Encode IT->char_to_display. */
1877 x_get_char_face_and_encoding (it->f, it->char_to_display,
1878 it->face_id, &char2b,
abfb6b46 1879 it->multibyte_p, 0);
06a2c219
GM
1880 font = face->font;
1881
1882 /* When no suitable font found, use the default font. */
1883 font_not_found_p = font == NULL;
1884 if (font_not_found_p)
b4192550
KH
1885 {
1886 font = FRAME_FONT (it->f);
1887 boff = it->f->output_data.x->baseline_offset;
1888 font_info = NULL;
1889 }
1890 else
1891 {
1892 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1893 boff = font_info->baseline_offset;
1894 if (font_info->vertical_centering)
1895 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1896 }
06a2c219
GM
1897
1898 if (it->char_to_display >= ' '
1899 && (!it->multibyte_p || it->char_to_display < 128))
1900 {
1901 /* Either unibyte or ASCII. */
1902 int stretched_p;
1903
1904 it->nglyphs = 1;
06a2c219
GM
1905
1906 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1907 it->ascent = font->ascent + boff;
1908 it->descent = font->descent - boff;
474848ac
GM
1909
1910 if (pcm)
1911 {
1912 it->phys_ascent = pcm->ascent + boff;
1913 it->phys_descent = pcm->descent - boff;
1914 it->pixel_width = pcm->width;
1915 }
1916 else
1917 {
1918 it->glyph_not_available_p = 1;
1919 it->phys_ascent = font->ascent + boff;
1920 it->phys_descent = font->descent - boff;
1921 it->pixel_width = FONT_WIDTH (font);
1922 }
06a2c219
GM
1923
1924 /* If this is a space inside a region of text with
1925 `space-width' property, change its width. */
1926 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1927 if (stretched_p)
1928 it->pixel_width *= XFLOATINT (it->space_width);
1929
1930 /* If face has a box, add the box thickness to the character
1931 height. If character has a box line to the left and/or
1932 right, add the box line width to the character's width. */
1933 if (face->box != FACE_NO_BOX)
1934 {
1935 int thick = face->box_line_width;
7d0393cf 1936
ea2ba0d4
KH
1937 if (thick > 0)
1938 {
1939 it->ascent += thick;
1940 it->descent += thick;
1941 }
1942 else
1943 thick = -thick;
1944
06a2c219
GM
1945 if (it->start_of_box_run_p)
1946 it->pixel_width += thick;
1947 if (it->end_of_box_run_p)
1948 it->pixel_width += thick;
1949 }
1950
1951 /* If face has an overline, add the height of the overline
1952 (1 pixel) and a 1 pixel margin to the character height. */
1953 if (face->overline_p)
1954 it->ascent += 2;
1955
1956 take_vertical_position_into_account (it);
7d0393cf 1957
06a2c219
GM
1958 /* If we have to actually produce glyphs, do it. */
1959 if (it->glyph_row)
1960 {
1961 if (stretched_p)
1962 {
1963 /* Translate a space with a `space-width' property
1964 into a stretch glyph. */
1965 double ascent = (double) font->ascent / FONT_HEIGHT (font);
7d0393cf 1966 x_append_stretch_glyph (it, it->object, it->pixel_width,
06a2c219
GM
1967 it->ascent + it->descent, ascent);
1968 }
1969 else
1970 x_append_glyph (it);
1971
1972 /* If characters with lbearing or rbearing are displayed
1973 in this line, record that fact in a flag of the
1974 glyph row. This is used to optimize X output code. */
1c7e22fd 1975 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1976 it->glyph_row->contains_overlapping_glyphs_p = 1;
1977 }
1978 }
1979 else if (it->char_to_display == '\n')
1980 {
1981 /* A newline has no width but we need the height of the line. */
1982 it->pixel_width = 0;
1983 it->nglyphs = 0;
b4192550
KH
1984 it->ascent = it->phys_ascent = font->ascent + boff;
1985 it->descent = it->phys_descent = font->descent - boff;
7d0393cf 1986
ea2ba0d4
KH
1987 if (face->box != FACE_NO_BOX
1988 && face->box_line_width > 0)
06a2c219 1989 {
ea2ba0d4
KH
1990 it->ascent += face->box_line_width;
1991 it->descent += face->box_line_width;
06a2c219
GM
1992 }
1993 }
1994 else if (it->char_to_display == '\t')
1995 {
1996 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1997 int x = it->current_x + it->continuation_lines_width;
06a2c219 1998 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1999
2000 /* If the distance from the current position to the next tab
2001 stop is less than a canonical character width, use the
2002 tab stop after that. */
2003 if (next_tab_x - x < CANON_X_UNIT (it->f))
2004 next_tab_x += tab_width;
7d0393cf 2005
06a2c219
GM
2006 it->pixel_width = next_tab_x - x;
2007 it->nglyphs = 1;
b4192550
KH
2008 it->ascent = it->phys_ascent = font->ascent + boff;
2009 it->descent = it->phys_descent = font->descent - boff;
7d0393cf 2010
06a2c219
GM
2011 if (it->glyph_row)
2012 {
2013 double ascent = (double) it->ascent / (it->ascent + it->descent);
7d0393cf 2014 x_append_stretch_glyph (it, it->object, it->pixel_width,
06a2c219
GM
2015 it->ascent + it->descent, ascent);
2016 }
2017 }
7d0393cf 2018 else
06a2c219
GM
2019 {
2020 /* A multi-byte character. Assume that the display width of the
2021 character is the width of the character multiplied by the
b4192550 2022 width of the font. */
06a2c219 2023
b4192550
KH
2024 /* If we found a font, this font should give us the right
2025 metrics. If we didn't find a font, use the frame's
2026 default font and calculate the width of the character
2027 from the charset width; this is what old redisplay code
2028 did. */
2029 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
2030 if (font_not_found_p || !pcm)
2031 {
2032 int charset = CHAR_CHARSET (it->char_to_display);
2033
2034 it->glyph_not_available_p = 1;
2035 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2036 * CHARSET_WIDTH (charset));
2037 it->phys_ascent = font->ascent + boff;
2038 it->phys_descent = font->descent - boff;
2039 }
2040 else
2041 {
2042 it->pixel_width = pcm->width;
2043 it->phys_ascent = pcm->ascent + boff;
2044 it->phys_descent = pcm->descent - boff;
2045 if (it->glyph_row
2046 && (pcm->lbearing < 0
2047 || pcm->rbearing > pcm->width))
2048 it->glyph_row->contains_overlapping_glyphs_p = 1;
2049 }
b4192550
KH
2050 it->nglyphs = 1;
2051 it->ascent = font->ascent + boff;
2052 it->descent = font->descent - boff;
06a2c219
GM
2053 if (face->box != FACE_NO_BOX)
2054 {
2055 int thick = face->box_line_width;
ea2ba0d4
KH
2056
2057 if (thick > 0)
2058 {
2059 it->ascent += thick;
2060 it->descent += thick;
2061 }
2062 else
2063 thick = - thick;
7d0393cf 2064
06a2c219
GM
2065 if (it->start_of_box_run_p)
2066 it->pixel_width += thick;
2067 if (it->end_of_box_run_p)
2068 it->pixel_width += thick;
2069 }
7d0393cf 2070
06a2c219
GM
2071 /* If face has an overline, add the height of the overline
2072 (1 pixel) and a 1 pixel margin to the character height. */
2073 if (face->overline_p)
2074 it->ascent += 2;
2075
2076 take_vertical_position_into_account (it);
7d0393cf 2077
06a2c219
GM
2078 if (it->glyph_row)
2079 x_append_glyph (it);
2080 }
a4249304 2081 it->multibyte_p = saved_multibyte_p;
06a2c219 2082 }
b4192550
KH
2083 else if (it->what == IT_COMPOSITION)
2084 {
2085 /* Note: A composition is represented as one glyph in the
2086 glyph matrix. There are no padding glyphs. */
2087 XChar2b char2b;
2088 XFontStruct *font;
ee569018 2089 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2090 XCharStruct *pcm;
2091 int font_not_found_p;
2092 struct font_info *font_info;
2093 int boff; /* baseline offset */
2094 struct composition *cmp = composition_table[it->cmp_id];
2095
2096 /* Maybe translate single-byte characters to multibyte. */
2097 it->char_to_display = it->c;
2098 if (unibyte_display_via_language_environment
2099 && SINGLE_BYTE_CHAR_P (it->c)
2100 && (it->c >= 0240
2101 || (it->c >= 0200
2102 && !NILP (Vnonascii_translation_table))))
2103 {
2104 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550 2105 }
7d0393cf 2106
b4192550 2107 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2108 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2109 face = FACE_FROM_ID (it->f, it->face_id);
2110 x_get_char_face_and_encoding (it->f, it->char_to_display,
abfb6b46 2111 it->face_id, &char2b, it->multibyte_p, 0);
b4192550
KH
2112 font = face->font;
2113
2114 /* When no suitable font found, use the default font. */
2115 font_not_found_p = font == NULL;
2116 if (font_not_found_p)
2117 {
2118 font = FRAME_FONT (it->f);
2119 boff = it->f->output_data.x->baseline_offset;
2120 font_info = NULL;
2121 }
2122 else
2123 {
2124 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2125 boff = font_info->baseline_offset;
2126 if (font_info->vertical_centering)
2127 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2128 }
2129
2130 /* There are no padding glyphs, so there is only one glyph to
2131 produce for the composition. Important is that pixel_width,
2132 ascent and descent are the values of what is drawn by
2133 draw_glyphs (i.e. the values of the overall glyphs composed). */
2134 it->nglyphs = 1;
2135
2136 /* If we have not yet calculated pixel size data of glyphs of
2137 the composition for the current face font, calculate them
2138 now. Theoretically, we have to check all fonts for the
2139 glyphs, but that requires much time and memory space. So,
2140 here we check only the font of the first glyph. This leads
2141 to incorrect display very rarely, and C-l (recenter) can
2142 correct the display anyway. */
2143 if (cmp->font != (void *) font)
2144 {
2145 /* Ascent and descent of the font of the first character of
2146 this composition (adjusted by baseline offset). Ascent
2147 and descent of overall glyphs should not be less than
2148 them respectively. */
2149 int font_ascent = font->ascent + boff;
2150 int font_descent = font->descent - boff;
2151 /* Bounding box of the overall glyphs. */
2152 int leftmost, rightmost, lowest, highest;
329bed06 2153 int i, width, ascent, descent;
b4192550
KH
2154
2155 cmp->font = (void *) font;
2156
2157 /* Initialize the bounding box. */
1bdeec2e
KH
2158 if (font_info
2159 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2160 {
2161 width = pcm->width;
2162 ascent = pcm->ascent;
2163 descent = pcm->descent;
2164 }
2165 else
2166 {
2167 width = FONT_WIDTH (font);
2168 ascent = font->ascent;
2169 descent = font->descent;
2170 }
7d0393cf 2171
329bed06
GM
2172 rightmost = width;
2173 lowest = - descent + boff;
2174 highest = ascent + boff;
b4192550 2175 leftmost = 0;
7d0393cf 2176
b4192550
KH
2177 if (font_info
2178 && font_info->default_ascent
2179 && CHAR_TABLE_P (Vuse_default_ascent)
2180 && !NILP (Faref (Vuse_default_ascent,
2181 make_number (it->char_to_display))))
2182 highest = font_info->default_ascent + boff;
2183
2184 /* Draw the first glyph at the normal position. It may be
2185 shifted to right later if some other glyphs are drawn at
2186 the left. */
2187 cmp->offsets[0] = 0;
2188 cmp->offsets[1] = boff;
2189
2190 /* Set cmp->offsets for the remaining glyphs. */
2191 for (i = 1; i < cmp->glyph_len; i++)
2192 {
2193 int left, right, btm, top;
2194 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018 2195 int face_id = FACE_FOR_CHAR (it->f, face, ch);
7d0393cf 2196
ee569018
KH
2197 face = FACE_FROM_ID (it->f, face_id);
2198 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
abfb6b46 2199 it->multibyte_p, 0);
b4192550
KH
2200 font = face->font;
2201 if (font == NULL)
2202 {
2203 font = FRAME_FONT (it->f);
2204 boff = it->f->output_data.x->baseline_offset;
2205 font_info = NULL;
2206 }
2207 else
2208 {
2209 font_info
2210 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2211 boff = font_info->baseline_offset;
2212 if (font_info->vertical_centering)
2213 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2214 }
2215
1bdeec2e
KH
2216 if (font_info
2217 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2218 {
2219 width = pcm->width;
2220 ascent = pcm->ascent;
2221 descent = pcm->descent;
2222 }
2223 else
2224 {
2225 width = FONT_WIDTH (font);
1bdeec2e
KH
2226 ascent = 1;
2227 descent = 0;
329bed06 2228 }
b4192550
KH
2229
2230 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2231 {
2232 /* Relative composition with or without
2233 alternate chars. */
329bed06
GM
2234 left = (leftmost + rightmost - width) / 2;
2235 btm = - descent + boff;
b4192550
KH
2236 if (font_info && font_info->relative_compose
2237 && (! CHAR_TABLE_P (Vignore_relative_composition)
2238 || NILP (Faref (Vignore_relative_composition,
2239 make_number (ch)))))
2240 {
2241
329bed06 2242 if (- descent >= font_info->relative_compose)
b4192550
KH
2243 /* One extra pixel between two glyphs. */
2244 btm = highest + 1;
329bed06 2245 else if (ascent <= 0)
b4192550 2246 /* One extra pixel between two glyphs. */
329bed06 2247 btm = lowest - 1 - ascent - descent;
b4192550
KH
2248 }
2249 }
2250 else
2251 {
2252 /* A composition rule is specified by an integer
2253 value that encodes global and new reference
2254 points (GREF and NREF). GREF and NREF are
2255 specified by numbers as below:
2256
2257 0---1---2 -- ascent
2258 | |
2259 | |
2260 | |
2261 9--10--11 -- center
2262 | |
2263 ---3---4---5--- baseline
2264 | |
2265 6---7---8 -- descent
2266 */
2267 int rule = COMPOSITION_RULE (cmp, i);
2268 int gref, nref, grefx, grefy, nrefx, nrefy;
2269
2270 COMPOSITION_DECODE_RULE (rule, gref, nref);
2271 grefx = gref % 3, nrefx = nref % 3;
2272 grefy = gref / 3, nrefy = nref / 3;
2273
2274 left = (leftmost
2275 + grefx * (rightmost - leftmost) / 2
329bed06 2276 - nrefx * width / 2);
b4192550
KH
2277 btm = ((grefy == 0 ? highest
2278 : grefy == 1 ? 0
2279 : grefy == 2 ? lowest
2280 : (highest + lowest) / 2)
329bed06
GM
2281 - (nrefy == 0 ? ascent + descent
2282 : nrefy == 1 ? descent - boff
b4192550 2283 : nrefy == 2 ? 0
329bed06 2284 : (ascent + descent) / 2));
b4192550
KH
2285 }
2286
2287 cmp->offsets[i * 2] = left;
329bed06 2288 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2289
2290 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2291 right = left + width;
2292 top = btm + descent + ascent;
b4192550
KH
2293 if (left < leftmost)
2294 leftmost = left;
2295 if (right > rightmost)
2296 rightmost = right;
2297 if (top > highest)
2298 highest = top;
2299 if (btm < lowest)
2300 lowest = btm;
2301 }
2302
2303 /* If there are glyphs whose x-offsets are negative,
2304 shift all glyphs to the right and make all x-offsets
2305 non-negative. */
2306 if (leftmost < 0)
2307 {
2308 for (i = 0; i < cmp->glyph_len; i++)
2309 cmp->offsets[i * 2] -= leftmost;
2310 rightmost -= leftmost;
2311 }
2312
2313 cmp->pixel_width = rightmost;
2314 cmp->ascent = highest;
2315 cmp->descent = - lowest;
2316 if (cmp->ascent < font_ascent)
2317 cmp->ascent = font_ascent;
2318 if (cmp->descent < font_descent)
2319 cmp->descent = font_descent;
2320 }
2321
2322 it->pixel_width = cmp->pixel_width;
2323 it->ascent = it->phys_ascent = cmp->ascent;
2324 it->descent = it->phys_descent = cmp->descent;
2325
2326 if (face->box != FACE_NO_BOX)
2327 {
2328 int thick = face->box_line_width;
ea2ba0d4
KH
2329
2330 if (thick > 0)
2331 {
2332 it->ascent += thick;
2333 it->descent += thick;
2334 }
2335 else
2336 thick = - thick;
7d0393cf 2337
b4192550
KH
2338 if (it->start_of_box_run_p)
2339 it->pixel_width += thick;
2340 if (it->end_of_box_run_p)
2341 it->pixel_width += thick;
2342 }
7d0393cf 2343
b4192550
KH
2344 /* If face has an overline, add the height of the overline
2345 (1 pixel) and a 1 pixel margin to the character height. */
2346 if (face->overline_p)
2347 it->ascent += 2;
2348
2349 take_vertical_position_into_account (it);
7d0393cf 2350
b4192550
KH
2351 if (it->glyph_row)
2352 x_append_composite_glyph (it);
2353 }
06a2c219
GM
2354 else if (it->what == IT_IMAGE)
2355 x_produce_image_glyph (it);
2356 else if (it->what == IT_STRETCH)
2357 x_produce_stretch_glyph (it);
2358
3017fdd1
GM
2359 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2360 because this isn't true for images with `:ascent 100'. */
2361 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2362 if (it->area == TEXT_AREA)
2363 it->current_x += it->pixel_width;
7d0393cf 2364
d365f5bb 2365 it->descent += it->extra_line_spacing;
7d0393cf 2366
06a2c219
GM
2367 it->max_ascent = max (it->max_ascent, it->ascent);
2368 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2369 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2370 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2371}
2372
2373
2374/* Estimate the pixel height of the mode or top line on frame F.
2375 FACE_ID specifies what line's height to estimate. */
2376
2377int
2378x_estimate_mode_line_height (f, face_id)
2379 struct frame *f;
2380 enum face_id face_id;
2381{
43281ee3 2382 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2383
2384 /* This function is called so early when Emacs starts that the face
2385 cache and mode line face are not yet initialized. */
2386 if (FRAME_FACE_CACHE (f))
2387 {
2388 struct face *face = FACE_FROM_ID (f, face_id);
2389 if (face)
43281ee3
GM
2390 {
2391 if (face->font)
2392 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2393 if (face->box_line_width > 0)
2394 height += 2 * face->box_line_width;
43281ee3 2395 }
06a2c219 2396 }
7d0393cf 2397
06a2c219
GM
2398 return height;
2399}
2400
2401\f
2402/***********************************************************************
2403 Glyph display
2404 ***********************************************************************/
2405
2406/* A sequence of glyphs to be drawn in the same face.
2407
2408 This data structure is not really completely X specific, so it
2409 could possibly, at least partially, be useful for other systems. It
2410 is currently not part of the external redisplay interface because
2411 it's not clear what other systems will need. */
2412
2413struct glyph_string
2414{
2415 /* X-origin of the string. */
2416 int x;
2417
2418 /* Y-origin and y-position of the base line of this string. */
2419 int y, ybase;
2420
2421 /* The width of the string, not including a face extension. */
2422 int width;
2423
2424 /* The width of the string, including a face extension. */
2425 int background_width;
2426
2427 /* The height of this string. This is the height of the line this
2428 string is drawn in, and can be different from the height of the
2429 font the string is drawn in. */
2430 int height;
2431
2432 /* Number of pixels this string overwrites in front of its x-origin.
2433 This number is zero if the string has an lbearing >= 0; it is
2434 -lbearing, if the string has an lbearing < 0. */
2435 int left_overhang;
2436
2437 /* Number of pixels this string overwrites past its right-most
2438 nominal x-position, i.e. x + width. Zero if the string's
2439 rbearing is <= its nominal width, rbearing - width otherwise. */
2440 int right_overhang;
2441
2442 /* The frame on which the glyph string is drawn. */
2443 struct frame *f;
2444
2445 /* The window on which the glyph string is drawn. */
2446 struct window *w;
2447
2448 /* X display and window for convenience. */
2449 Display *display;
2450 Window window;
2451
2452 /* The glyph row for which this string was built. It determines the
2453 y-origin and height of the string. */
2454 struct glyph_row *row;
2455
2456 /* The area within row. */
2457 enum glyph_row_area area;
2458
2459 /* Characters to be drawn, and number of characters. */
2460 XChar2b *char2b;
2461 int nchars;
2462
06a2c219
GM
2463 /* A face-override for drawing cursors, mouse face and similar. */
2464 enum draw_glyphs_face hl;
2465
2466 /* Face in which this string is to be drawn. */
2467 struct face *face;
2468
2469 /* Font in which this string is to be drawn. */
2470 XFontStruct *font;
2471
2472 /* Font info for this string. */
2473 struct font_info *font_info;
2474
b4192550
KH
2475 /* Non-null means this string describes (part of) a composition.
2476 All characters from char2b are drawn composed. */
2477 struct composition *cmp;
06a2c219
GM
2478
2479 /* Index of this glyph string's first character in the glyph
b4192550
KH
2480 definition of CMP. If this is zero, this glyph string describes
2481 the first character of a composition. */
06a2c219
GM
2482 int gidx;
2483
2484 /* 1 means this glyph strings face has to be drawn to the right end
2485 of the window's drawing area. */
2486 unsigned extends_to_end_of_line_p : 1;
2487
2488 /* 1 means the background of this string has been drawn. */
2489 unsigned background_filled_p : 1;
2490
2491 /* 1 means glyph string must be drawn with 16-bit functions. */
2492 unsigned two_byte_p : 1;
2493
2494 /* 1 means that the original font determined for drawing this glyph
2495 string could not be loaded. The member `font' has been set to
2496 the frame's default font in this case. */
2497 unsigned font_not_found_p : 1;
2498
2499 /* 1 means that the face in which this glyph string is drawn has a
2500 stipple pattern. */
2501 unsigned stippled_p : 1;
2502
66ac4b0e
GM
2503 /* 1 means only the foreground of this glyph string must be drawn,
2504 and we should use the physical height of the line this glyph
2505 string appears in as clip rect. */
2506 unsigned for_overlaps_p : 1;
2507
06a2c219
GM
2508 /* The GC to use for drawing this glyph string. */
2509 GC gc;
2510
2511 /* A pointer to the first glyph in the string. This glyph
2512 corresponds to char2b[0]. Needed to draw rectangles if
2513 font_not_found_p is 1. */
2514 struct glyph *first_glyph;
2515
2516 /* Image, if any. */
2517 struct image *img;
2518
2519 struct glyph_string *next, *prev;
2520};
2521
2522
61869b99 2523#if GLYPH_DEBUG
06a2c219
GM
2524
2525static void
2526x_dump_glyph_string (s)
2527 struct glyph_string *s;
2528{
2529 fprintf (stderr, "glyph string\n");
2530 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2531 s->x, s->y, s->width, s->height);
2532 fprintf (stderr, " ybase = %d\n", s->ybase);
2533 fprintf (stderr, " hl = %d\n", s->hl);
2534 fprintf (stderr, " left overhang = %d, right = %d\n",
2535 s->left_overhang, s->right_overhang);
2536 fprintf (stderr, " nchars = %d\n", s->nchars);
2537 fprintf (stderr, " extends to end of line = %d\n",
2538 s->extends_to_end_of_line_p);
2539 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2540 fprintf (stderr, " bg width = %d\n", s->background_width);
2541}
2542
2543#endif /* GLYPH_DEBUG */
2544
2545
2546
2547static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2548 struct glyph_string **,
2549 struct glyph_string *,
2550 struct glyph_string *));
2551static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2552 struct glyph_string **,
2553 struct glyph_string *,
2554 struct glyph_string *));
2555static void x_append_glyph_string P_ ((struct glyph_string **,
2556 struct glyph_string **,
2557 struct glyph_string *));
2558static int x_left_overwritten P_ ((struct glyph_string *));
2559static int x_left_overwriting P_ ((struct glyph_string *));
2560static int x_right_overwritten P_ ((struct glyph_string *));
2561static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2562static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2563 int));
06a2c219
GM
2564static void x_init_glyph_string P_ ((struct glyph_string *,
2565 XChar2b *, struct window *,
2566 struct glyph_row *,
7d0393cf 2567 enum glyph_row_area, int,
06a2c219
GM
2568 enum draw_glyphs_face));
2569static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2570 enum glyph_row_area, int, int,
f0a48a01 2571 enum draw_glyphs_face, int));
06a2c219
GM
2572static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2573static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2574static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2575 int));
2576static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2577static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2578static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2579static void x_draw_glyph_string P_ ((struct glyph_string *));
2580static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2581static void x_set_cursor_gc P_ ((struct glyph_string *));
2582static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2583static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2584static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2585 int *, int *));
2586static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2587static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2588 unsigned long *, double, int));
06a2c219 2589static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2590 double, int, unsigned long));
06a2c219
GM
2591static void x_setup_relief_colors P_ ((struct glyph_string *));
2592static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2593static void x_draw_image_relief P_ ((struct glyph_string *));
2594static void x_draw_image_foreground P_ ((struct glyph_string *));
2595static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2596static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2597static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2598 int, int, int));
2599static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2600 int, int, int, int, XRectangle *));
2601static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2602 int, int, int, XRectangle *));
66ac4b0e
GM
2603static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2604 enum glyph_row_area));
209f68d9
GM
2605static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2606 struct glyph_row *,
2607 enum glyph_row_area, int, int));
06a2c219 2608
163dcff3
GM
2609#if GLYPH_DEBUG
2610static void x_check_font P_ ((struct frame *, XFontStruct *));
2611#endif
2612
06a2c219 2613
06a2c219
GM
2614/* Append the list of glyph strings with head H and tail T to the list
2615 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2616
2617static INLINE void
2618x_append_glyph_string_lists (head, tail, h, t)
2619 struct glyph_string **head, **tail;
2620 struct glyph_string *h, *t;
2621{
2622 if (h)
2623 {
2624 if (*head)
2625 (*tail)->next = h;
2626 else
2627 *head = h;
2628 h->prev = *tail;
2629 *tail = t;
2630 }
2631}
2632
2633
2634/* Prepend the list of glyph strings with head H and tail T to the
2635 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2636 result. */
2637
2638static INLINE void
2639x_prepend_glyph_string_lists (head, tail, h, t)
2640 struct glyph_string **head, **tail;
2641 struct glyph_string *h, *t;
2642{
2643 if (h)
2644 {
2645 if (*head)
2646 (*head)->prev = t;
2647 else
2648 *tail = t;
2649 t->next = *head;
2650 *head = h;
2651 }
2652}
2653
2654
2655/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2656 Set *HEAD and *TAIL to the resulting list. */
2657
2658static INLINE void
2659x_append_glyph_string (head, tail, s)
2660 struct glyph_string **head, **tail;
2661 struct glyph_string *s;
2662{
2663 s->next = s->prev = NULL;
2664 x_append_glyph_string_lists (head, tail, s, s);
2665}
2666
2667
2668/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2669 face. */
2670
2671static void
2672x_set_cursor_gc (s)
2673 struct glyph_string *s;
2674{
2675 if (s->font == FRAME_FONT (s->f)
2676 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2677 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2678 && !s->cmp)
06a2c219
GM
2679 s->gc = s->f->output_data.x->cursor_gc;
2680 else
2681 {
2682 /* Cursor on non-default face: must merge. */
2683 XGCValues xgcv;
2684 unsigned long mask;
2685
2686 xgcv.background = s->f->output_data.x->cursor_pixel;
2687 xgcv.foreground = s->face->background;
2688
2689 /* If the glyph would be invisible, try a different foreground. */
2690 if (xgcv.foreground == xgcv.background)
2691 xgcv.foreground = s->face->foreground;
2692 if (xgcv.foreground == xgcv.background)
2693 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2694 if (xgcv.foreground == xgcv.background)
2695 xgcv.foreground = s->face->foreground;
2696
2697 /* Make sure the cursor is distinct from text in this face. */
2698 if (xgcv.background == s->face->background
2699 && xgcv.foreground == s->face->foreground)
2700 {
2701 xgcv.background = s->face->foreground;
2702 xgcv.foreground = s->face->background;
2703 }
2704
2705 IF_DEBUG (x_check_font (s->f, s->font));
2706 xgcv.font = s->font->fid;
2707 xgcv.graphics_exposures = False;
2708 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2709
2710 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2711 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2712 mask, &xgcv);
2713 else
2714 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2715 = XCreateGC (s->display, s->window, mask, &xgcv);
2716
2717 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2718 }
2719}
2720
2721
2722/* Set up S->gc of glyph string S for drawing text in mouse face. */
7d0393cf 2723
06a2c219
GM
2724static void
2725x_set_mouse_face_gc (s)
2726 struct glyph_string *s;
7d0393cf 2727{
06a2c219 2728 int face_id;
ee569018 2729 struct face *face;
06a2c219 2730
e4ded23c 2731 /* What face has to be used last for the mouse face? */
06a2c219 2732 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2733 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2734 if (face == NULL)
2735 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
7d0393cf 2736
033e3e18
GM
2737 if (s->first_glyph->type == CHAR_GLYPH)
2738 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2739 else
2740 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2741 s->face = FACE_FROM_ID (s->f, face_id);
2742 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2743
2744 /* If font in this face is same as S->font, use it. */
2745 if (s->font == s->face->font)
2746 s->gc = s->face->gc;
2747 else
2748 {
2749 /* Otherwise construct scratch_cursor_gc with values from FACE
2750 but font FONT. */
2751 XGCValues xgcv;
2752 unsigned long mask;
7d0393cf 2753
06a2c219
GM
2754 xgcv.background = s->face->background;
2755 xgcv.foreground = s->face->foreground;
2756 IF_DEBUG (x_check_font (s->f, s->font));
2757 xgcv.font = s->font->fid;
2758 xgcv.graphics_exposures = False;
2759 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
7d0393cf 2760
06a2c219
GM
2761 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2762 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2763 mask, &xgcv);
2764 else
2765 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2766 = XCreateGC (s->display, s->window, mask, &xgcv);
7d0393cf 2767
06a2c219
GM
2768 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2769 }
2770
2771 xassert (s->gc != 0);
2772}
2773
2774
2775/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2776 Faces to use in the mode line have already been computed when the
2777 matrix was built, so there isn't much to do, here. */
2778
2779static INLINE void
2780x_set_mode_line_face_gc (s)
2781 struct glyph_string *s;
7d0393cf 2782{
06a2c219 2783 s->gc = s->face->gc;
06a2c219
GM
2784}
2785
2786
2787/* Set S->gc of glyph string S for drawing that glyph string. Set
2788 S->stippled_p to a non-zero value if the face of S has a stipple
2789 pattern. */
2790
2791static INLINE void
2792x_set_glyph_string_gc (s)
2793 struct glyph_string *s;
2794{
209f68d9 2795 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
7d0393cf 2796
06a2c219
GM
2797 if (s->hl == DRAW_NORMAL_TEXT)
2798 {
2799 s->gc = s->face->gc;
2800 s->stippled_p = s->face->stipple != 0;
2801 }
2802 else if (s->hl == DRAW_INVERSE_VIDEO)
2803 {
2804 x_set_mode_line_face_gc (s);
2805 s->stippled_p = s->face->stipple != 0;
2806 }
2807 else if (s->hl == DRAW_CURSOR)
2808 {
2809 x_set_cursor_gc (s);
2810 s->stippled_p = 0;
2811 }
2812 else if (s->hl == DRAW_MOUSE_FACE)
2813 {
2814 x_set_mouse_face_gc (s);
2815 s->stippled_p = s->face->stipple != 0;
2816 }
2817 else if (s->hl == DRAW_IMAGE_RAISED
2818 || s->hl == DRAW_IMAGE_SUNKEN)
2819 {
2820 s->gc = s->face->gc;
2821 s->stippled_p = s->face->stipple != 0;
2822 }
2823 else
2824 {
2825 s->gc = s->face->gc;
2826 s->stippled_p = s->face->stipple != 0;
2827 }
2828
2829 /* GC must have been set. */
2830 xassert (s->gc != 0);
2831}
2832
2833
2834/* Return in *R the clipping rectangle for glyph string S. */
2835
2836static void
2837x_get_glyph_string_clip_rect (s, r)
2838 struct glyph_string *s;
2839 XRectangle *r;
2840{
2841 if (s->row->full_width_p)
2842 {
2843 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71 2844 int canon_x = CANON_X_UNIT (s->f);
7d0393cf 2845
1da3fd71
GM
2846 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2847 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2848
2849 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2850 {
1da3fd71 2851 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2852 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2853 r->x -= width;
2854 }
7d0393cf 2855
b9432a85 2856 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
7d0393cf 2857
06a2c219
GM
2858 /* Unless displaying a mode or menu bar line, which are always
2859 fully visible, clip to the visible part of the row. */
2860 if (s->w->pseudo_window_p)
2861 r->height = s->row->visible_height;
2862 else
2863 r->height = s->height;
2864 }
2865 else
2866 {
2867 /* This is a text line that may be partially visible. */
2868 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2869 r->width = window_box_width (s->w, s->area);
2870 r->height = s->row->visible_height;
2871 }
2872
66ac4b0e
GM
2873 /* If S draws overlapping rows, it's sufficient to use the top and
2874 bottom of the window for clipping because this glyph string
2875 intentionally draws over other lines. */
2876 if (s->for_overlaps_p)
2877 {
045dee35 2878 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2879 r->height = window_text_bottom_y (s->w) - r->y;
2880 }
98b8a90f
GM
2881 else
2882 {
2883 /* Don't use S->y for clipping because it doesn't take partially
2884 visible lines into account. For example, it can be negative for
2885 partially visible lines at the top of a window. */
2886 if (!s->row->full_width_p
2887 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2888 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2889 else
2890 r->y = max (0, s->row->y);
2891
2892 /* If drawing a tool-bar window, draw it over the internal border
2893 at the top of the window. */
2894 if (s->w == XWINDOW (s->f->tool_bar_window))
2895 r->y -= s->f->output_data.x->internal_border_width;
2896 }
2897
66ac4b0e 2898 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2899}
2900
2901
2902/* Set clipping for output of glyph string S. S may be part of a mode
2903 line or menu if we don't have X toolkit support. */
2904
2905static INLINE void
2906x_set_glyph_string_clipping (s)
2907 struct glyph_string *s;
2908{
2909 XRectangle r;
2910 x_get_glyph_string_clip_rect (s, &r);
2911 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2912}
2913
2914
2915/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2916 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2917
2918static INLINE void
2919x_compute_glyph_string_overhangs (s)
2920 struct glyph_string *s;
2921{
b4192550 2922 if (s->cmp == NULL
06a2c219
GM
2923 && s->first_glyph->type == CHAR_GLYPH)
2924 {
2925 XCharStruct cs;
2926 int direction, font_ascent, font_descent;
2927 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2928 &font_ascent, &font_descent, &cs);
2929 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2930 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2931 }
2932}
2933
2934
2935/* Compute overhangs and x-positions for glyph string S and its
2936 predecessors, or successors. X is the starting x-position for S.
2937 BACKWARD_P non-zero means process predecessors. */
7d0393cf 2938
06a2c219
GM
2939static void
2940x_compute_overhangs_and_x (s, x, backward_p)
2941 struct glyph_string *s;
2942 int x;
2943 int backward_p;
2944{
2945 if (backward_p)
2946 {
2947 while (s)
2948 {
2949 x_compute_glyph_string_overhangs (s);
2950 x -= s->width;
2951 s->x = x;
2952 s = s->prev;
2953 }
2954 }
2955 else
2956 {
2957 while (s)
2958 {
2959 x_compute_glyph_string_overhangs (s);
2960 s->x = x;
2961 x += s->width;
2962 s = s->next;
2963 }
2964 }
2965}
2966
2967
2968/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2969 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2970 assumed to be zero. */
06a2c219
GM
2971
2972static void
2973x_get_glyph_overhangs (glyph, f, left, right)
2974 struct glyph *glyph;
2975 struct frame *f;
2976 int *left, *right;
2977{
06a2c219 2978 *left = *right = 0;
7d0393cf 2979
b4192550 2980 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2981 {
2982 XFontStruct *font;
2983 struct face *face;
2984 struct font_info *font_info;
2985 XChar2b char2b;
ee569018
KH
2986 XCharStruct *pcm;
2987
2988 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2989 font = face->font;
2990 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2991 if (font
2992 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2993 {
06a2c219
GM
2994 if (pcm->rbearing > pcm->width)
2995 *right = pcm->rbearing - pcm->width;
2996 if (pcm->lbearing < 0)
2997 *left = -pcm->lbearing;
2998 }
2999 }
3000}
3001
3002
3003/* Return the index of the first glyph preceding glyph string S that
3004 is overwritten by S because of S's left overhang. Value is -1
3005 if no glyphs are overwritten. */
3006
3007static int
3008x_left_overwritten (s)
3009 struct glyph_string *s;
3010{
3011 int k;
7d0393cf 3012
06a2c219
GM
3013 if (s->left_overhang)
3014 {
3015 int x = 0, i;
3016 struct glyph *glyphs = s->row->glyphs[s->area];
3017 int first = s->first_glyph - glyphs;
3018
3019 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3020 x -= glyphs[i].pixel_width;
3021
3022 k = i + 1;
3023 }
3024 else
3025 k = -1;
3026
3027 return k;
3028}
3029
3030
3031/* Return the index of the first glyph preceding glyph string S that
3032 is overwriting S because of its right overhang. Value is -1 if no
3033 glyph in front of S overwrites S. */
3034
3035static int
3036x_left_overwriting (s)
3037 struct glyph_string *s;
3038{
3039 int i, k, x;
3040 struct glyph *glyphs = s->row->glyphs[s->area];
3041 int first = s->first_glyph - glyphs;
3042
3043 k = -1;
3044 x = 0;
3045 for (i = first - 1; i >= 0; --i)
3046 {
3047 int left, right;
3048 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3049 if (x + right > 0)
3050 k = i;
3051 x -= glyphs[i].pixel_width;
3052 }
3053
3054 return k;
3055}
3056
3057
3058/* Return the index of the last glyph following glyph string S that is
3059 not overwritten by S because of S's right overhang. Value is -1 if
3060 no such glyph is found. */
3061
3062static int
3063x_right_overwritten (s)
3064 struct glyph_string *s;
3065{
3066 int k = -1;
3067
3068 if (s->right_overhang)
3069 {
3070 int x = 0, i;
3071 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3072 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219 3073 int end = s->row->used[s->area];
7d0393cf 3074
06a2c219
GM
3075 for (i = first; i < end && s->right_overhang > x; ++i)
3076 x += glyphs[i].pixel_width;
3077
3078 k = i;
3079 }
3080
3081 return k;
3082}
3083
3084
3085/* Return the index of the last glyph following glyph string S that
3086 overwrites S because of its left overhang. Value is negative
3087 if no such glyph is found. */
3088
3089static int
3090x_right_overwriting (s)
3091 struct glyph_string *s;
3092{
3093 int i, k, x;
3094 int end = s->row->used[s->area];
3095 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3096 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3097
3098 k = -1;
3099 x = 0;
3100 for (i = first; i < end; ++i)
3101 {
3102 int left, right;
3103 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3104 if (x - left < 0)
3105 k = i;
3106 x += glyphs[i].pixel_width;
3107 }
3108
3109 return k;
3110}
3111
3112
3113/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3114
3115static INLINE void
3116x_clear_glyph_string_rect (s, x, y, w, h)
3117 struct glyph_string *s;
3118 int x, y, w, h;
3119{
3120 XGCValues xgcv;
3121 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3122 XSetForeground (s->display, s->gc, xgcv.background);
3123 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3124 XSetForeground (s->display, s->gc, xgcv.foreground);
3125}
3126
3127
3128/* Draw the background of glyph_string S. If S->background_filled_p
3129 is non-zero don't draw it. FORCE_P non-zero means draw the
3130 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3131 when a string preceding S draws into the background of S, or S
3132 contains the first component of a composition. */
06a2c219
GM
3133
3134static void
3135x_draw_glyph_string_background (s, force_p)
3136 struct glyph_string *s;
3137 int force_p;
3138{
3139 /* Nothing to do if background has already been drawn or if it
3140 shouldn't be drawn in the first place. */
3141 if (!s->background_filled_p)
3142 {
ea2ba0d4
KH
3143 int box_line_width = max (s->face->box_line_width, 0);
3144
b4192550 3145 if (s->stippled_p)
06a2c219
GM
3146 {
3147 /* Fill background with a stipple pattern. */
3148 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3149 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3150 s->y + box_line_width,
06a2c219 3151 s->background_width,
ea2ba0d4 3152 s->height - 2 * box_line_width);
06a2c219
GM
3153 XSetFillStyle (s->display, s->gc, FillSolid);
3154 s->background_filled_p = 1;
3155 }
ea2ba0d4 3156 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3157 || s->font_not_found_p
3158 || s->extends_to_end_of_line_p
06a2c219
GM
3159 || force_p)
3160 {
ea2ba0d4 3161 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3162 s->background_width,
ea2ba0d4 3163 s->height - 2 * box_line_width);
06a2c219
GM
3164 s->background_filled_p = 1;
3165 }
3166 }
3167}
3168
3169
3170/* Draw the foreground of glyph string S. */
3171
3172static void
3173x_draw_glyph_string_foreground (s)
3174 struct glyph_string *s;
3175{
3176 int i, x;
3177
3178 /* If first glyph of S has a left box line, start drawing the text
3179 of S to the right of that box line. */
3180 if (s->face->box != FACE_NO_BOX
3181 && s->first_glyph->left_box_line_p)
ea2ba0d4 3182 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3183 else
3184 x = s->x;
3185
b4192550
KH
3186 /* Draw characters of S as rectangles if S's font could not be
3187 loaded. */
3188 if (s->font_not_found_p)
06a2c219 3189 {
b4192550 3190 for (i = 0; i < s->nchars; ++i)
06a2c219 3191 {
b4192550
KH
3192 struct glyph *g = s->first_glyph + i;
3193 XDrawRectangle (s->display, s->window,
3194 s->gc, x, s->y, g->pixel_width - 1,
3195 s->height - 1);
3196 x += g->pixel_width;
06a2c219
GM
3197 }
3198 }
3199 else
3200 {
b4192550
KH
3201 char *char1b = (char *) s->char2b;
3202 int boff = s->font_info->baseline_offset;
06a2c219 3203
b4192550
KH
3204 if (s->font_info->vertical_centering)
3205 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3206
3207 /* If we can use 8-bit functions, condense S->char2b. */
3208 if (!s->two_byte_p)
3209 for (i = 0; i < s->nchars; ++i)
3210 char1b[i] = s->char2b[i].byte2;
3211
3212 /* Draw text with XDrawString if background has already been
3213 filled. Otherwise, use XDrawImageString. (Note that
3214 XDrawImageString is usually faster than XDrawString.) Always
3215 use XDrawImageString when drawing the cursor so that there is
3216 no chance that characters under a box cursor are invisible. */
3217 if (s->for_overlaps_p
3218 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3219 {
3220 /* Draw characters with 16-bit or 8-bit functions. */
3221 if (s->two_byte_p)
3222 XDrawString16 (s->display, s->window, s->gc, x,
3223 s->ybase - boff, s->char2b, s->nchars);
3224 else
3225 XDrawString (s->display, s->window, s->gc, x,
3226 s->ybase - boff, char1b, s->nchars);
3227 }
06a2c219
GM
3228 else
3229 {
b4192550
KH
3230 if (s->two_byte_p)
3231 XDrawImageString16 (s->display, s->window, s->gc, x,
3232 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3233 else
b4192550
KH
3234 XDrawImageString (s->display, s->window, s->gc, x,
3235 s->ybase - boff, char1b, s->nchars);
3236 }
fce13449
MB
3237
3238 if (s->face->overstrike)
3239 {
3240 /* For overstriking (to simulate bold-face), draw the
3241 characters again shifted to the right by one pixel. */
3242 if (s->two_byte_p)
3243 XDrawString16 (s->display, s->window, s->gc, x + 1,
3244 s->ybase - boff, s->char2b, s->nchars);
3245 else
3246 XDrawString (s->display, s->window, s->gc, x + 1,
3247 s->ybase - boff, char1b, s->nchars);
3248 }
b4192550
KH
3249 }
3250}
06a2c219 3251
b4192550 3252/* Draw the foreground of composite glyph string S. */
06a2c219 3253
b4192550
KH
3254static void
3255x_draw_composite_glyph_string_foreground (s)
3256 struct glyph_string *s;
3257{
3258 int i, x;
06a2c219 3259
b4192550
KH
3260 /* If first glyph of S has a left box line, start drawing the text
3261 of S to the right of that box line. */
3262 if (s->face->box != FACE_NO_BOX
3263 && s->first_glyph->left_box_line_p)
ea2ba0d4 3264 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3265 else
3266 x = s->x;
06a2c219 3267
b4192550
KH
3268 /* S is a glyph string for a composition. S->gidx is the index of
3269 the first character drawn for glyphs of this composition.
3270 S->gidx == 0 means we are drawing the very first character of
3271 this composition. */
06a2c219 3272
b4192550
KH
3273 /* Draw a rectangle for the composition if the font for the very
3274 first character of the composition could not be loaded. */
3275 if (s->font_not_found_p)
3276 {
3277 if (s->gidx == 0)
3278 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3279 s->width - 1, s->height - 1);
3280 }
3281 else
3282 {
3283 for (i = 0; i < s->nchars; i++, ++s->gidx)
fce13449
MB
3284 {
3285 XDrawString16 (s->display, s->window, s->gc,
3286 x + s->cmp->offsets[s->gidx * 2],
3287 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3288 s->char2b + i, 1);
3289 if (s->face->overstrike)
3290 XDrawString16 (s->display, s->window, s->gc,
3291 x + s->cmp->offsets[s->gidx * 2] + 1,
3292 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3293 s->char2b + i, 1);
3294 }
06a2c219
GM
3295 }
3296}
3297
3298
80c32bcc
GM
3299#ifdef USE_X_TOOLKIT
3300
3e71d8f2 3301static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
3302static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3303 XrmValue *, XrmValue *, XtPointer *));
3304static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3305 XrmValue *, Cardinal *));
80c32bcc 3306
3e71d8f2
GM
3307
3308/* Return the frame on which widget WIDGET is used.. Abort if frame
3309 cannot be determined. */
3310
e851c833 3311static struct frame *
3e71d8f2 3312x_frame_of_widget (widget)
80c32bcc 3313 Widget widget;
80c32bcc 3314{
80c32bcc 3315 struct x_display_info *dpyinfo;
5c187dee 3316 Lisp_Object tail;
3e71d8f2 3317 struct frame *f;
7d0393cf 3318
80c32bcc 3319 dpyinfo = x_display_info_for_display (XtDisplay (widget));
7d0393cf 3320
80c32bcc
GM
3321 /* Find the top-level shell of the widget. Note that this function
3322 can be called when the widget is not yet realized, so XtWindow
3323 (widget) == 0. That's the reason we can't simply use
3324 x_any_window_to_frame. */
3325 while (!XtIsTopLevelShell (widget))
3326 widget = XtParent (widget);
3327
3328 /* Look for a frame with that top-level widget. Allocate the color
3329 on that frame to get the right gamma correction value. */
3330 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3331 if (GC_FRAMEP (XCAR (tail))
3332 && (f = XFRAME (XCAR (tail)),
3333 (f->output_data.nothing != 1
3334 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3335 && f->output_data.x->widget == widget)
3e71d8f2 3336 return f;
80c32bcc
GM
3337
3338 abort ();
3339}
3340
3e71d8f2
GM
3341
3342/* Allocate the color COLOR->pixel on the screen and display of
3343 widget WIDGET in colormap CMAP. If an exact match cannot be
3344 allocated, try the nearest color available. Value is non-zero
3345 if successful. This is called from lwlib. */
3346
3347int
3348x_alloc_nearest_color_for_widget (widget, cmap, color)
3349 Widget widget;
3350 Colormap cmap;
3351 XColor *color;
3352{
3353 struct frame *f = x_frame_of_widget (widget);
3354 return x_alloc_nearest_color (f, cmap, color);
3355}
3356
3357
46d516e5
MB
3358/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3359 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3360 If this produces the same color as PIXEL, try a color where all RGB
3361 values have DELTA added. Return the allocated color in *PIXEL.
3362 DISPLAY is the X display, CMAP is the colormap to operate on.
3363 Value is non-zero if successful. */
3364
3365int
3366x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3367 Widget widget;
3368 Display *display;
3369 Colormap cmap;
3370 unsigned long *pixel;
3371 double factor;
3372 int delta;
3373{
3374 struct frame *f = x_frame_of_widget (widget);
3375 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3376}
3377
3378
651f03b6
GM
3379/* Structure specifying which arguments should be passed by Xt to
3380 cvt_string_to_pixel. We want the widget's screen and colormap. */
3381
3382static XtConvertArgRec cvt_string_to_pixel_args[] =
3383 {
3384 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3385 sizeof (Screen *)},
3386 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3387 sizeof (Colormap)}
3388 };
3389
3390
3391/* The address of this variable is returned by
3392 cvt_string_to_pixel. */
3393
3394static Pixel cvt_string_to_pixel_value;
3395
3396
3397/* Convert a color name to a pixel color.
3398
3399 DPY is the display we are working on.
3400
3401 ARGS is an array of *NARGS XrmValue structures holding additional
3402 information about the widget for which the conversion takes place.
3403 The contents of this array are determined by the specification
3404 in cvt_string_to_pixel_args.
3405
3406 FROM is a pointer to an XrmValue which points to the color name to
3407 convert. TO is an XrmValue in which to return the pixel color.
3408
3409 CLOSURE_RET is a pointer to user-data, in which we record if
3410 we allocated the color or not.
3411
3412 Value is True if successful, False otherwise. */
3413
3414static Boolean
3415cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3416 Display *dpy;
3417 XrmValue *args;
3418 Cardinal *nargs;
3419 XrmValue *from, *to;
3420 XtPointer *closure_ret;
3421{
3422 Screen *screen;
3423 Colormap cmap;
3424 Pixel pixel;
3425 String color_name;
3426 XColor color;
3427
3428 if (*nargs != 2)
3429 {
3430 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3431 "wrongParameters", "cvt_string_to_pixel",
3432 "XtToolkitError",
3433 "Screen and colormap args required", NULL, NULL);
3434 return False;
3435 }
3436
3437 screen = *(Screen **) args[0].addr;
3438 cmap = *(Colormap *) args[1].addr;
3439 color_name = (String) from->addr;
3440
3441 if (strcmp (color_name, XtDefaultBackground) == 0)
3442 {
3443 *closure_ret = (XtPointer) False;
3444 pixel = WhitePixelOfScreen (screen);
3445 }
3446 else if (strcmp (color_name, XtDefaultForeground) == 0)
3447 {
3448 *closure_ret = (XtPointer) False;
3449 pixel = BlackPixelOfScreen (screen);
3450 }
3451 else if (XParseColor (dpy, cmap, color_name, &color)
3452 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3453 {
3454 pixel = color.pixel;
3455 *closure_ret = (XtPointer) True;
3456 }
3457 else
3458 {
3459 String params[1];
3460 Cardinal nparams = 1;
7d0393cf 3461
651f03b6
GM
3462 params[0] = color_name;
3463 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3464 "badValue", "cvt_string_to_pixel",
3465 "XtToolkitError", "Invalid color `%s'",
3466 params, &nparams);
3467 return False;
3468 }
3469
3470 if (to->addr != NULL)
3471 {
3472 if (to->size < sizeof (Pixel))
3473 {
3474 to->size = sizeof (Pixel);
3475 return False;
3476 }
7d0393cf 3477
651f03b6
GM
3478 *(Pixel *) to->addr = pixel;
3479 }
3480 else
3481 {
3482 cvt_string_to_pixel_value = pixel;
3483 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3484 }
7d0393cf 3485
651f03b6
GM
3486 to->size = sizeof (Pixel);
3487 return True;
3488}
3489
3490
3491/* Free a pixel color which was previously allocated via
3492 cvt_string_to_pixel. This is registered as the destructor
3493 for this type of resource via XtSetTypeConverter.
3494
3495 APP is the application context in which we work.
3496
3497 TO is a pointer to an XrmValue holding the color to free.
3498 CLOSURE is the value we stored in CLOSURE_RET for this color
3499 in cvt_string_to_pixel.
3500
3501 ARGS and NARGS are like for cvt_string_to_pixel. */
3502
3503static void
3504cvt_pixel_dtor (app, to, closure, args, nargs)
3505 XtAppContext app;
3506 XrmValuePtr to;
3507 XtPointer closure;
3508 XrmValuePtr args;
3509 Cardinal *nargs;
3510{
3511 if (*nargs != 2)
3512 {
3513 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3514 "XtToolkitError",
3515 "Screen and colormap arguments required",
3516 NULL, NULL);
3517 }
3518 else if (closure != NULL)
3519 {
3520 /* We did allocate the pixel, so free it. */
3521 Screen *screen = *(Screen **) args[0].addr;
3522 Colormap cmap = *(Colormap *) args[1].addr;
3523 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 3524 (Pixel *) to->addr, 1);
651f03b6
GM
3525 }
3526}
3527
3528
80c32bcc
GM
3529#endif /* USE_X_TOOLKIT */
3530
3531
f04e1297 3532/* Value is an array of XColor structures for the contents of the
651f03b6 3533 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
3534 Note that this probably shouldn't be called for large color maps,
3535 say a 24-bit TrueColor map. */
3536
3537static const XColor *
651f03b6
GM
3538x_color_cells (dpy, ncells)
3539 Display *dpy;
f04e1297
GM
3540 int *ncells;
3541{
651f03b6 3542 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
3543
3544 if (dpyinfo->color_cells == NULL)
3545 {
651f03b6 3546 Screen *screen = dpyinfo->screen;
f04e1297 3547 int i;
7d0393cf 3548
f04e1297 3549 dpyinfo->ncolor_cells
651f03b6 3550 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
3551 dpyinfo->color_cells
3552 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3553 * sizeof *dpyinfo->color_cells);
7d0393cf 3554
f04e1297
GM
3555 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3556 dpyinfo->color_cells[i].pixel = i;
7d0393cf 3557
651f03b6 3558 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
3559 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3560 }
3561
3562 *ncells = dpyinfo->ncolor_cells;
3563 return dpyinfo->color_cells;
3564}
3565
3566
3567/* On frame F, translate pixel colors to RGB values for the NCOLORS
3568 colors in COLORS. Use cached information, if available. */
3569
3570void
3571x_query_colors (f, colors, ncolors)
3572 struct frame *f;
3573 XColor *colors;
3574 int ncolors;
3575{
3576 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3577
3578 if (dpyinfo->color_cells)
3579 {
3580 int i;
3581 for (i = 0; i < ncolors; ++i)
3582 {
3583 unsigned long pixel = colors[i].pixel;
3584 xassert (pixel < dpyinfo->ncolor_cells);
3585 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3586 colors[i] = dpyinfo->color_cells[pixel];
3587 }
3588 }
3589 else
3590 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3591}
3592
3593
3594/* On frame F, translate pixel color to RGB values for the color in
3595 COLOR. Use cached information, if available. */
3596
3597void
3598x_query_color (f, color)
3599 struct frame *f;
3600 XColor *color;
3601{
3602 x_query_colors (f, color, 1);
3603}
7d0393cf 3604
f04e1297 3605
651f03b6
GM
3606/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3607 exact match can't be allocated, try the nearest color available.
3608 Value is non-zero if successful. Set *COLOR to the color
3609 allocated. */
06a2c219 3610
651f03b6
GM
3611static int
3612x_alloc_nearest_color_1 (dpy, cmap, color)
3613 Display *dpy;
06a2c219
GM
3614 Colormap cmap;
3615 XColor *color;
3616{
80c32bcc
GM
3617 int rc;
3618
651f03b6 3619 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
3620 if (rc == 0)
3621 {
3622 /* If we got to this point, the colormap is full, so we're going
3623 to try to get the next closest color. The algorithm used is
3624 a least-squares matching, which is what X uses for closest
3625 color matching with StaticColor visuals. */
3626 int nearest, i;
3627 unsigned long nearest_delta = ~0;
f04e1297 3628 int ncells;
651f03b6 3629 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
3630
3631 for (nearest = i = 0; i < ncells; ++i)
3632 {
3633 long dred = (color->red >> 8) - (cells[i].red >> 8);
3634 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3635 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3636 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3637
3638 if (delta < nearest_delta)
3639 {
3640 nearest = i;
3641 nearest_delta = delta;
3642 }
3643 }
7d0393cf 3644
06a2c219
GM
3645 color->red = cells[nearest].red;
3646 color->green = cells[nearest].green;
3647 color->blue = cells[nearest].blue;
651f03b6 3648 rc = XAllocColor (dpy, cmap, color);
06a2c219 3649 }
35efe0a1
GM
3650 else
3651 {
3652 /* If allocation succeeded, and the allocated pixel color is not
3653 equal to a cached pixel color recorded earlier, there was a
3654 change in the colormap, so clear the color cache. */
651f03b6 3655 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1 3656 XColor *cached_color;
7d0393cf 3657
35efe0a1
GM
3658 if (dpyinfo->color_cells
3659 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3660 (cached_color->red != color->red
3661 || cached_color->blue != color->blue
3662 || cached_color->green != color->green)))
35efe0a1
GM
3663 {
3664 xfree (dpyinfo->color_cells);
3665 dpyinfo->color_cells = NULL;
3666 dpyinfo->ncolor_cells = 0;
3667 }
3668 }
06a2c219 3669
d9c545da
GM
3670#ifdef DEBUG_X_COLORS
3671 if (rc)
3672 register_color (color->pixel);
3673#endif /* DEBUG_X_COLORS */
7d0393cf 3674
06a2c219
GM
3675 return rc;
3676}
3677
3678
651f03b6
GM
3679/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3680 exact match can't be allocated, try the nearest color available.
3681 Value is non-zero if successful. Set *COLOR to the color
3682 allocated. */
3683
3684int
3685x_alloc_nearest_color (f, cmap, color)
3686 struct frame *f;
3687 Colormap cmap;
3688 XColor *color;
3689{
3690 gamma_correct (f, color);
3691 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3692}
3693
3694
d9c545da
GM
3695/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3696 It's necessary to do this instead of just using PIXEL directly to
3697 get color reference counts right. */
3698
3699unsigned long
3700x_copy_color (f, pixel)
3701 struct frame *f;
3702 unsigned long pixel;
3703{
3704 XColor color;
3705
3706 color.pixel = pixel;
3707 BLOCK_INPUT;
f04e1297 3708 x_query_color (f, &color);
d9c545da
GM
3709 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3710 UNBLOCK_INPUT;
3711#ifdef DEBUG_X_COLORS
3712 register_color (pixel);
3713#endif
3714 return color.pixel;
3715}
3716
3717
3e71d8f2
GM
3718/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3719 It's necessary to do this instead of just using PIXEL directly to
3720 get color reference counts right. */
3721
3722unsigned long
3723x_copy_dpy_color (dpy, cmap, pixel)
3724 Display *dpy;
3725 Colormap cmap;
3726 unsigned long pixel;
3727{
3728 XColor color;
3729
3730 color.pixel = pixel;
3731 BLOCK_INPUT;
3732 XQueryColor (dpy, cmap, &color);
3733 XAllocColor (dpy, cmap, &color);
3734 UNBLOCK_INPUT;
3735#ifdef DEBUG_X_COLORS
3736 register_color (pixel);
3737#endif
3738 return color.pixel;
3739}
3740
3741
6d8b0acd 3742/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3743 boosted.
6d8b0acd 3744
d7361edf
MB
3745 Nominally, highlight colors for `3d' faces are calculated by
3746 brightening an object's color by a constant scale factor, but this
3747 doesn't yield good results for dark colors, so for colors who's
3748 brightness is less than this value (on a scale of 0-65535) have an
3749 use an additional additive factor.
6d8b0acd
MB
3750
3751 The value here is set so that the default menu-bar/mode-line color
3752 (grey75) will not have its highlights changed at all. */
3753#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3754
3755
06a2c219
GM
3756/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3757 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3758 If this produces the same color as PIXEL, try a color where all RGB
3759 values have DELTA added. Return the allocated color in *PIXEL.
3760 DISPLAY is the X display, CMAP is the colormap to operate on.
3761 Value is non-zero if successful. */
3762
3763static int
3764x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3765 struct frame *f;
3766 Display *display;
3767 Colormap cmap;
3768 unsigned long *pixel;
68c45bf0 3769 double factor;
06a2c219
GM
3770 int delta;
3771{
3772 XColor color, new;
6d8b0acd 3773 long bright;
06a2c219
GM
3774 int success_p;
3775
3776 /* Get RGB color values. */
3777 color.pixel = *pixel;
f04e1297 3778 x_query_color (f, &color);
06a2c219
GM
3779
3780 /* Change RGB values by specified FACTOR. Avoid overflow! */
3781 xassert (factor >= 0);
3782 new.red = min (0xffff, factor * color.red);
3783 new.green = min (0xffff, factor * color.green);
3784 new.blue = min (0xffff, factor * color.blue);
3785
d7361edf
MB
3786 /* Calculate brightness of COLOR. */
3787 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3788
3789 /* We only boost colors that are darker than
3790 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3791 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3792 /* Make an additive adjustment to NEW, because it's dark enough so
3793 that scaling by FACTOR alone isn't enough. */
3794 {
3795 /* How far below the limit this color is (0 - 1, 1 being darker). */
3796 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3797 /* The additive adjustment. */
d7361edf 3798 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3799
3800 if (factor < 1)
3801 {
6d8b0acd
MB
3802 new.red = max (0, new.red - min_delta);
3803 new.green = max (0, new.green - min_delta);
3804 new.blue = max (0, new.blue - min_delta);
3805 }
3806 else
3807 {
3808 new.red = min (0xffff, min_delta + new.red);
3809 new.green = min (0xffff, min_delta + new.green);
3810 new.blue = min (0xffff, min_delta + new.blue);
3811 }
3812 }
3813
06a2c219 3814 /* Try to allocate the color. */
80c32bcc 3815 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3816 if (success_p)
3817 {
3818 if (new.pixel == *pixel)
3819 {
3820 /* If we end up with the same color as before, try adding
3821 delta to the RGB values. */
0d605c67 3822 x_free_colors (f, &new.pixel, 1);
7d0393cf 3823
06a2c219
GM
3824 new.red = min (0xffff, delta + color.red);
3825 new.green = min (0xffff, delta + color.green);
3826 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3827 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3828 }
3829 else
3830 success_p = 1;
3831 *pixel = new.pixel;
3832 }
7d0393cf 3833
06a2c219
GM
3834 return success_p;
3835}
3836
3837
3838/* Set up the foreground color for drawing relief lines of glyph
3839 string S. RELIEF is a pointer to a struct relief containing the GC
3840 with which lines will be drawn. Use a color that is FACTOR or
3841 DELTA lighter or darker than the relief's background which is found
3842 in S->f->output_data.x->relief_background. If such a color cannot
3843 be allocated, use DEFAULT_PIXEL, instead. */
7d0393cf 3844
06a2c219
GM
3845static void
3846x_setup_relief_color (f, relief, factor, delta, default_pixel)
3847 struct frame *f;
3848 struct relief *relief;
68c45bf0 3849 double factor;
06a2c219
GM
3850 int delta;
3851 unsigned long default_pixel;
3852{
3853 XGCValues xgcv;
3854 struct x_output *di = f->output_data.x;
3855 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3856 unsigned long pixel;
3857 unsigned long background = di->relief_background;
43bd1b2b 3858 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3859 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3860 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3861
3862 xgcv.graphics_exposures = False;
3863 xgcv.line_width = 1;
3864
3865 /* Free previously allocated color. The color cell will be reused
3866 when it has been freed as many times as it was allocated, so this
3867 doesn't affect faces using the same colors. */
3868 if (relief->gc
3869 && relief->allocated_p)
3870 {
0d605c67 3871 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3872 relief->allocated_p = 0;
3873 }
3874
3875 /* Allocate new color. */
3876 xgcv.foreground = default_pixel;
3877 pixel = background;
dcd08bfb
GM
3878 if (dpyinfo->n_planes != 1
3879 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3880 {
3881 relief->allocated_p = 1;
3882 xgcv.foreground = relief->pixel = pixel;
3883 }
7d0393cf 3884
06a2c219
GM
3885 if (relief->gc == 0)
3886 {
dcd08bfb 3887 xgcv.stipple = dpyinfo->gray;
06a2c219 3888 mask |= GCStipple;
dcd08bfb 3889 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3890 }
3891 else
dcd08bfb 3892 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3893}
3894
3895
3896/* Set up colors for the relief lines around glyph string S. */
3897
3898static void
3899x_setup_relief_colors (s)
3900 struct glyph_string *s;
3901{
3902 struct x_output *di = s->f->output_data.x;
3903 unsigned long color;
3904
3905 if (s->face->use_box_color_for_shadows_p)
3906 color = s->face->box_color;
e2a57b34 3907 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 3908 && s->img->pixmap
e2a57b34
MB
3909 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3910 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
3911 else
3912 {
3913 XGCValues xgcv;
7d0393cf 3914
06a2c219
GM
3915 /* Get the background color of the face. */
3916 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3917 color = xgcv.background;
3918 }
3919
3920 if (di->white_relief.gc == 0
3921 || color != di->relief_background)
3922 {
3923 di->relief_background = color;
3924 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3925 WHITE_PIX_DEFAULT (s->f));
3926 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3927 BLACK_PIX_DEFAULT (s->f));
3928 }
3929}
3930
3931
3932/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3933 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3934 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3935 relief. LEFT_P non-zero means draw a relief on the left side of
3936 the rectangle. RIGHT_P non-zero means draw a relief on the right
3937 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3938 when drawing. */
3939
3940static void
3941x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3942 raised_p, left_p, right_p, clip_rect)
3943 struct frame *f;
0971e730 3944 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
06a2c219
GM
3945 XRectangle *clip_rect;
3946{
de507556
GM
3947 Display *dpy = FRAME_X_DISPLAY (f);
3948 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
3949 int i;
3950 GC gc;
7d0393cf 3951
06a2c219
GM
3952 if (raised_p)
3953 gc = f->output_data.x->white_relief.gc;
3954 else
3955 gc = f->output_data.x->black_relief.gc;
de507556 3956 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3957
3958 /* Top. */
3959 for (i = 0; i < width; ++i)
de507556 3960 XDrawLine (dpy, window, gc,
06a2c219
GM
3961 left_x + i * left_p, top_y + i,
3962 right_x + 1 - i * right_p, top_y + i);
3963
3964 /* Left. */
3965 if (left_p)
3966 for (i = 0; i < width; ++i)
de507556 3967 XDrawLine (dpy, window, gc,
44655e77 3968 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 3969
de507556 3970 XSetClipMask (dpy, gc, None);
06a2c219
GM
3971 if (raised_p)
3972 gc = f->output_data.x->black_relief.gc;
3973 else
3974 gc = f->output_data.x->white_relief.gc;
de507556 3975 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 3976
06a2c219
GM
3977 /* Bottom. */
3978 for (i = 0; i < width; ++i)
de507556
GM
3979 XDrawLine (dpy, window, gc,
3980 left_x + i * left_p, bottom_y - i,
327f42ee 3981 right_x + 1 - i * right_p, bottom_y - i);
7d0393cf 3982
06a2c219
GM
3983 /* Right. */
3984 if (right_p)
3985 for (i = 0; i < width; ++i)
de507556 3986 XDrawLine (dpy, window, gc,
06a2c219
GM
3987 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3988
de507556 3989 XSetClipMask (dpy, gc, None);
06a2c219
GM
3990}
3991
3992
3993/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3994 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3995 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3996 left side of the rectangle. RIGHT_P non-zero means draw a line
3997 on the right side of the rectangle. CLIP_RECT is the clipping
3998 rectangle to use when drawing. */
3999
4000static void
4001x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4002 left_p, right_p, clip_rect)
4003 struct glyph_string *s;
0971e730 4004 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
06a2c219
GM
4005 XRectangle *clip_rect;
4006{
4007 XGCValues xgcv;
7d0393cf 4008
06a2c219
GM
4009 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4010 XSetForeground (s->display, s->gc, s->face->box_color);
4011 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 4012
06a2c219
GM
4013 /* Top. */
4014 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 4015 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
4016
4017 /* Left. */
4018 if (left_p)
4019 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 4020 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
4021
4022 /* Bottom. */
4023 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 4024 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
7d0393cf 4025
06a2c219
GM
4026 /* Right. */
4027 if (right_p)
4028 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 4029 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
4030
4031 XSetForeground (s->display, s->gc, xgcv.foreground);
4032 XSetClipMask (s->display, s->gc, None);
4033}
4034
4035
4036/* Draw a box around glyph string S. */
4037
4038static void
4039x_draw_glyph_string_box (s)
4040 struct glyph_string *s;
4041{
4042 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
4043 int left_p, right_p;
4044 struct glyph *last_glyph;
4045 XRectangle clip_rect;
4046
4047 last_x = window_box_right (s->w, s->area);
4048 if (s->row->full_width_p
4049 && !s->w->pseudo_window_p)
4050 {
3f332ef3 4051 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
06a2c219
GM
4052 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4053 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4054 }
7d0393cf 4055
06a2c219 4056 /* The glyph that may have a right box line. */
b4192550 4057 last_glyph = (s->cmp || s->img
06a2c219
GM
4058 ? s->first_glyph
4059 : s->first_glyph + s->nchars - 1);
4060
ea2ba0d4 4061 width = abs (s->face->box_line_width);
06a2c219
GM
4062 raised_p = s->face->box == FACE_RAISED_BOX;
4063 left_x = s->x;
57ac7c81
GM
4064 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4065 ? last_x - 1
4066 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
4067 top_y = s->y;
4068 bottom_y = top_y + s->height - 1;
4069
4070 left_p = (s->first_glyph->left_box_line_p
4071 || (s->hl == DRAW_MOUSE_FACE
4072 && (s->prev == NULL
4073 || s->prev->hl != s->hl)));
4074 right_p = (last_glyph->right_box_line_p
4075 || (s->hl == DRAW_MOUSE_FACE
4076 && (s->next == NULL
4077 || s->next->hl != s->hl)));
327f42ee 4078
06a2c219
GM
4079 x_get_glyph_string_clip_rect (s, &clip_rect);
4080
4081 if (s->face->box == FACE_SIMPLE_BOX)
4082 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4083 left_p, right_p, &clip_rect);
4084 else
4085 {
4086 x_setup_relief_colors (s);
4087 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4088 width, raised_p, left_p, right_p, &clip_rect);
4089 }
4090}
4091
4092
4093/* Draw foreground of image glyph string S. */
4094
4095static void
4096x_draw_image_foreground (s)
4097 struct glyph_string *s;
4098{
4099 int x;
95af8492 4100 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4101
4102 /* If first glyph of S has a left box line, start drawing it to the
4103 right of that line. */
4104 if (s->face->box != FACE_NO_BOX
4105 && s->first_glyph->left_box_line_p)
ea2ba0d4 4106 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4107 else
4108 x = s->x;
4109
4110 /* If there is a margin around the image, adjust x- and y-position
4111 by that margin. */
22d650b8
GM
4112 x += s->img->hmargin;
4113 y += s->img->vmargin;
06a2c219
GM
4114
4115 if (s->img->pixmap)
4116 {
4117 if (s->img->mask)
4118 {
4119 /* We can't set both a clip mask and use XSetClipRectangles
4120 because the latter also sets a clip mask. We also can't
4121 trust on the shape extension to be available
4122 (XShapeCombineRegion). So, compute the rectangle to draw
4123 manually. */
4124 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4125 | GCFunction);
4126 XGCValues xgcv;
4127 XRectangle clip_rect, image_rect, r;
4128
4129 xgcv.clip_mask = s->img->mask;
4130 xgcv.clip_x_origin = x;
4131 xgcv.clip_y_origin = y;
4132 xgcv.function = GXcopy;
4133 XChangeGC (s->display, s->gc, mask, &xgcv);
7d0393cf 4134
06a2c219
GM
4135 x_get_glyph_string_clip_rect (s, &clip_rect);
4136 image_rect.x = x;
4137 image_rect.y = y;
4138 image_rect.width = s->img->width;
4139 image_rect.height = s->img->height;
4140 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4141 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4142 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4143 }
4144 else
4145 {
49ad1d99
GM
4146 XRectangle clip_rect, image_rect, r;
4147
4148 x_get_glyph_string_clip_rect (s, &clip_rect);
4149 image_rect.x = x;
4150 image_rect.y = y;
4151 image_rect.width = s->img->width;
4152 image_rect.height = s->img->height;
4153 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4154 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4155 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
7d0393cf 4156
06a2c219
GM
4157 /* When the image has a mask, we can expect that at
4158 least part of a mouse highlight or a block cursor will
4159 be visible. If the image doesn't have a mask, make
4160 a block cursor visible by drawing a rectangle around
4161 the image. I believe it's looking better if we do
4162 nothing here for mouse-face. */
4163 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4164 {
4165 int r = s->img->relief;
4166 if (r < 0) r = -r;
4167 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4168 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4169 }
06a2c219
GM
4170 }
4171 }
4172 else
4173 /* Draw a rectangle if image could not be loaded. */
4174 XDrawRectangle (s->display, s->window, s->gc, x, y,
4175 s->img->width - 1, s->img->height - 1);
4176}
4177
4178
4179/* Draw a relief around the image glyph string S. */
4180
4181static void
4182x_draw_image_relief (s)
4183 struct glyph_string *s;
4184{
4185 int x0, y0, x1, y1, thick, raised_p;
4186 XRectangle r;
4187 int x;
95af8492 4188 int y = s->ybase - image_ascent (s->img, s->face);
7d0393cf 4189
06a2c219
GM
4190 /* If first glyph of S has a left box line, start drawing it to the
4191 right of that line. */
4192 if (s->face->box != FACE_NO_BOX
4193 && s->first_glyph->left_box_line_p)
ea2ba0d4 4194 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4195 else
4196 x = s->x;
7d0393cf 4197
06a2c219
GM
4198 /* If there is a margin around the image, adjust x- and y-position
4199 by that margin. */
22d650b8
GM
4200 x += s->img->hmargin;
4201 y += s->img->vmargin;
7d0393cf 4202
06a2c219
GM
4203 if (s->hl == DRAW_IMAGE_SUNKEN
4204 || s->hl == DRAW_IMAGE_RAISED)
4205 {
62854fe2 4206 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
4207 raised_p = s->hl == DRAW_IMAGE_RAISED;
4208 }
4209 else
4210 {
4211 thick = abs (s->img->relief);
4212 raised_p = s->img->relief > 0;
4213 }
7d0393cf 4214
06a2c219
GM
4215 x0 = x - thick;
4216 y0 = y - thick;
4217 x1 = x + s->img->width + thick - 1;
4218 y1 = y + s->img->height + thick - 1;
7d0393cf 4219
06a2c219
GM
4220 x_setup_relief_colors (s);
4221 x_get_glyph_string_clip_rect (s, &r);
4222 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4223}
4224
4225
4226/* Draw the foreground of image glyph string S to PIXMAP. */
4227
4228static void
4229x_draw_image_foreground_1 (s, pixmap)
4230 struct glyph_string *s;
4231 Pixmap pixmap;
4232{
4233 int x;
95af8492 4234 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4235
4236 /* If first glyph of S has a left box line, start drawing it to the
4237 right of that line. */
4238 if (s->face->box != FACE_NO_BOX
4239 && s->first_glyph->left_box_line_p)
ea2ba0d4 4240 x = abs (s->face->box_line_width);
06a2c219
GM
4241 else
4242 x = 0;
4243
4244 /* If there is a margin around the image, adjust x- and y-position
4245 by that margin. */
22d650b8
GM
4246 x += s->img->hmargin;
4247 y += s->img->vmargin;
dc43ef94 4248
06a2c219
GM
4249 if (s->img->pixmap)
4250 {
4251 if (s->img->mask)
4252 {
4253 /* We can't set both a clip mask and use XSetClipRectangles
4254 because the latter also sets a clip mask. We also can't
4255 trust on the shape extension to be available
4256 (XShapeCombineRegion). So, compute the rectangle to draw
4257 manually. */
4258 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4259 | GCFunction);
4260 XGCValues xgcv;
4261
4262 xgcv.clip_mask = s->img->mask;
4263 xgcv.clip_x_origin = x;
4264 xgcv.clip_y_origin = y;
4265 xgcv.function = GXcopy;
4266 XChangeGC (s->display, s->gc, mask, &xgcv);
4267
4268 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4269 0, 0, s->img->width, s->img->height, x, y);
4270 XSetClipMask (s->display, s->gc, None);
4271 }
4272 else
4273 {
4274 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4275 0, 0, s->img->width, s->img->height, x, y);
7d0393cf 4276
06a2c219
GM
4277 /* When the image has a mask, we can expect that at
4278 least part of a mouse highlight or a block cursor will
4279 be visible. If the image doesn't have a mask, make
4280 a block cursor visible by drawing a rectangle around
4281 the image. I believe it's looking better if we do
4282 nothing here for mouse-face. */
4283 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4284 {
4285 int r = s->img->relief;
4286 if (r < 0) r = -r;
4287 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4288 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4289 }
06a2c219
GM
4290 }
4291 }
4292 else
4293 /* Draw a rectangle if image could not be loaded. */
4294 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4295 s->img->width - 1, s->img->height - 1);
4296}
dc43ef94 4297
990ba854 4298
06a2c219
GM
4299/* Draw part of the background of glyph string S. X, Y, W, and H
4300 give the rectangle to draw. */
a9a5b0a5 4301
06a2c219
GM
4302static void
4303x_draw_glyph_string_bg_rect (s, x, y, w, h)
4304 struct glyph_string *s;
4305 int x, y, w, h;
4306{
4307 if (s->stippled_p)
4308 {
4309 /* Fill background with a stipple pattern. */
4310 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4311 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4312 XSetFillStyle (s->display, s->gc, FillSolid);
4313 }
4314 else
4315 x_clear_glyph_string_rect (s, x, y, w, h);
4316}
07e34cb0 4317
b5210ea7 4318
7d0393cf 4319/* Draw image glyph string S.
dc43ef94 4320
06a2c219
GM
4321 s->y
4322 s->x +-------------------------
4323 | s->face->box
4324 |
4325 | +-------------------------
4326 | | s->img->margin
4327 | |
4328 | | +-------------------
4329 | | | the image
dc43ef94 4330
06a2c219 4331 */
dc43ef94 4332
06a2c219
GM
4333static void
4334x_draw_image_glyph_string (s)
4335 struct glyph_string *s;
4336{
4337 int x, y;
ea2ba0d4
KH
4338 int box_line_hwidth = abs (s->face->box_line_width);
4339 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4340 int height;
4341 Pixmap pixmap = None;
4342
ea2ba0d4 4343 height = s->height - 2 * box_line_vwidth;
06a2c219 4344
488dd4c4 4345
06a2c219
GM
4346 /* Fill background with face under the image. Do it only if row is
4347 taller than image or if image has a clip mask to reduce
4348 flickering. */
4349 s->stippled_p = s->face->stipple != 0;
4350 if (height > s->img->height
22d650b8
GM
4351 || s->img->hmargin
4352 || s->img->vmargin
06a2c219
GM
4353 || s->img->mask
4354 || s->img->pixmap == 0
4355 || s->width != s->background_width)
4356 {
ea2ba0d4
KH
4357 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4358 x = s->x + box_line_hwidth;
06a2c219
GM
4359 else
4360 x = s->x;
7d0393cf 4361
ea2ba0d4 4362 y = s->y + box_line_vwidth;
7d0393cf 4363
06a2c219
GM
4364 if (s->img->mask)
4365 {
f9b5db02
GM
4366 /* Create a pixmap as large as the glyph string. Fill it
4367 with the background color. Copy the image to it, using
4368 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4369 Screen *screen = FRAME_X_SCREEN (s->f);
4370 int depth = DefaultDepthOfScreen (screen);
4371
4372 /* Create a pixmap as large as the glyph string. */
4373 pixmap = XCreatePixmap (s->display, s->window,
4374 s->background_width,
4375 s->height, depth);
7d0393cf 4376
06a2c219
GM
4377 /* Don't clip in the following because we're working on the
4378 pixmap. */
4379 XSetClipMask (s->display, s->gc, None);
4380
4381 /* Fill the pixmap with the background color/stipple. */
4382 if (s->stippled_p)
4383 {
4384 /* Fill background with a stipple pattern. */
4385 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4386 XFillRectangle (s->display, pixmap, s->gc,
4387 0, 0, s->background_width, s->height);
4388 XSetFillStyle (s->display, s->gc, FillSolid);
4389 }
4390 else
4391 {
4392 XGCValues xgcv;
4393 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4394 &xgcv);
4395 XSetForeground (s->display, s->gc, xgcv.background);
4396 XFillRectangle (s->display, pixmap, s->gc,
4397 0, 0, s->background_width, s->height);
4398 XSetForeground (s->display, s->gc, xgcv.foreground);
4399 }
4400 }
4401 else
06a2c219 4402 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
7d0393cf 4403
06a2c219
GM
4404 s->background_filled_p = 1;
4405 }
dc43ef94 4406
06a2c219
GM
4407 /* Draw the foreground. */
4408 if (pixmap != None)
4409 {
4410 x_draw_image_foreground_1 (s, pixmap);
4411 x_set_glyph_string_clipping (s);
4412 XCopyArea (s->display, pixmap, s->window, s->gc,
4413 0, 0, s->background_width, s->height, s->x, s->y);
4414 XFreePixmap (s->display, pixmap);
4415 }
4416 else
4417 x_draw_image_foreground (s);
b5210ea7 4418
06a2c219
GM
4419 /* If we must draw a relief around the image, do it. */
4420 if (s->img->relief
4421 || s->hl == DRAW_IMAGE_RAISED
4422 || s->hl == DRAW_IMAGE_SUNKEN)
4423 x_draw_image_relief (s);
4424}
8c1a6a84 4425
990ba854 4426
06a2c219 4427/* Draw stretch glyph string S. */
dc43ef94 4428
06a2c219
GM
4429static void
4430x_draw_stretch_glyph_string (s)
4431 struct glyph_string *s;
4432{
4433 xassert (s->first_glyph->type == STRETCH_GLYPH);
4434 s->stippled_p = s->face->stipple != 0;
990ba854 4435
06a2c219
GM
4436 if (s->hl == DRAW_CURSOR
4437 && !x_stretch_cursor_p)
4438 {
4439 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4440 as wide as the stretch glyph. */
4441 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4442
06a2c219
GM
4443 /* Draw cursor. */
4444 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4445
06a2c219
GM
4446 /* Clear rest using the GC of the original non-cursor face. */
4447 if (width < s->background_width)
4448 {
06a2c219
GM
4449 int x = s->x + width, y = s->y;
4450 int w = s->background_width - width, h = s->height;
4451 XRectangle r;
b7f83f9e 4452 GC gc;
dc43ef94 4453
b7f83f9e
GM
4454 if (s->row->mouse_face_p
4455 && cursor_in_mouse_face_p (s->w))
4456 {
4457 x_set_mouse_face_gc (s);
4458 gc = s->gc;
4459 }
4460 else
4461 gc = s->face->gc;
7d0393cf 4462
06a2c219
GM
4463 x_get_glyph_string_clip_rect (s, &r);
4464 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
7d0393cf 4465
06a2c219
GM
4466 if (s->face->stipple)
4467 {
4468 /* Fill background with a stipple pattern. */
4469 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4470 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4471 XSetFillStyle (s->display, gc, FillSolid);
4472 }
4473 else
4474 {
4475 XGCValues xgcv;
4476 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4477 XSetForeground (s->display, gc, xgcv.background);
4478 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4479 XSetForeground (s->display, gc, xgcv.foreground);
4480 }
4481 }
4482 }
61e9f9f3 4483 else if (!s->background_filled_p)
06a2c219
GM
4484 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4485 s->height);
7d0393cf 4486
06a2c219
GM
4487 s->background_filled_p = 1;
4488}
4489
4490
4491/* Draw glyph string S. */
4492
4493static void
4494x_draw_glyph_string (s)
4495 struct glyph_string *s;
4496{
4458cf11
KH
4497 int relief_drawn_p = 0;
4498
06a2c219
GM
4499 /* If S draws into the background of its successor, draw the
4500 background of the successor first so that S can draw into it.
4501 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4502 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4503 {
4504 xassert (s->next->img == NULL);
4505 x_set_glyph_string_gc (s->next);
4506 x_set_glyph_string_clipping (s->next);
4507 x_draw_glyph_string_background (s->next, 1);
4508 }
97210f4e 4509
06a2c219
GM
4510 /* Set up S->gc, set clipping and draw S. */
4511 x_set_glyph_string_gc (s);
06a2c219 4512
4458cf11
KH
4513 /* Draw relief (if any) in advance for char/composition so that the
4514 glyph string can be drawn over it. */
4515 if (!s->for_overlaps_p
4516 && s->face->box != FACE_NO_BOX
4517 && (s->first_glyph->type == CHAR_GLYPH
4518 || s->first_glyph->type == COMPOSITE_GLYPH))
4519
4520 {
e6269cbb 4521 x_set_glyph_string_clipping (s);
4458cf11
KH
4522 x_draw_glyph_string_background (s, 1);
4523 x_draw_glyph_string_box (s);
e6269cbb 4524 x_set_glyph_string_clipping (s);
4458cf11
KH
4525 relief_drawn_p = 1;
4526 }
e6269cbb
GM
4527 else
4528 x_set_glyph_string_clipping (s);
4458cf11 4529
06a2c219
GM
4530 switch (s->first_glyph->type)
4531 {
4532 case IMAGE_GLYPH:
4533 x_draw_image_glyph_string (s);
4534 break;
4535
4536 case STRETCH_GLYPH:
4537 x_draw_stretch_glyph_string (s);
4538 break;
4539
4540 case CHAR_GLYPH:
66ac4b0e
GM
4541 if (s->for_overlaps_p)
4542 s->background_filled_p = 1;
4543 else
4544 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4545 x_draw_glyph_string_foreground (s);
4546 break;
4547
b4192550
KH
4548 case COMPOSITE_GLYPH:
4549 if (s->for_overlaps_p || s->gidx > 0)
4550 s->background_filled_p = 1;
4551 else
4552 x_draw_glyph_string_background (s, 1);
4553 x_draw_composite_glyph_string_foreground (s);
4554 break;
4555
06a2c219
GM
4556 default:
4557 abort ();
4558 }
4559
66ac4b0e 4560 if (!s->for_overlaps_p)
06a2c219 4561 {
66ac4b0e
GM
4562 /* Draw underline. */
4563 if (s->face->underline_p)
4564 {
e24e84cc
GM
4565 unsigned long tem, h;
4566 int y;
06a2c219 4567
e24e84cc 4568 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4569 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4570 h = 1;
e24e84cc
GM
4571
4572 /* Get the underline position. This is the recommended
4573 vertical offset in pixels from the baseline to the top of
4574 the underline. This is a signed value according to the
4575 specs, and its default is
4576
4577 ROUND ((maximum descent) / 2), with
4578 ROUND(x) = floor (x + 0.5) */
7d0393cf 4579
a72d5ce5
GM
4580 if (x_use_underline_position_properties
4581 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
4582 y = s->ybase + (long) tem;
4583 else if (s->face->font)
4584 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4585 else
a02f1be0 4586 y = s->y + s->height - h;
7d0393cf 4587
66ac4b0e 4588 if (s->face->underline_defaulted_p)
e24e84cc
GM
4589 XFillRectangle (s->display, s->window, s->gc,
4590 s->x, y, s->width, h);
66ac4b0e
GM
4591 else
4592 {
4593 XGCValues xgcv;
4594 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4595 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4596 XFillRectangle (s->display, s->window, s->gc,
4597 s->x, y, s->width, h);
66ac4b0e
GM
4598 XSetForeground (s->display, s->gc, xgcv.foreground);
4599 }
dc6f92b8 4600 }
07e34cb0 4601
66ac4b0e
GM
4602 /* Draw overline. */
4603 if (s->face->overline_p)
06a2c219 4604 {
66ac4b0e
GM
4605 unsigned long dy = 0, h = 1;
4606
4607 if (s->face->overline_color_defaulted_p)
4608 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4609 s->width, h);
4610 else
4611 {
4612 XGCValues xgcv;
4613 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4614 XSetForeground (s->display, s->gc, s->face->overline_color);
4615 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4616 s->width, h);
4617 XSetForeground (s->display, s->gc, xgcv.foreground);
4618 }
06a2c219 4619 }
7d0393cf 4620
66ac4b0e
GM
4621 /* Draw strike-through. */
4622 if (s->face->strike_through_p)
06a2c219 4623 {
66ac4b0e
GM
4624 unsigned long h = 1;
4625 unsigned long dy = (s->height - h) / 2;
4626
4627 if (s->face->strike_through_color_defaulted_p)
4628 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4629 s->width, h);
4630 else
4631 {
4632 XGCValues xgcv;
4633 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4634 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4635 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4636 s->width, h);
4637 XSetForeground (s->display, s->gc, xgcv.foreground);
4638 }
06a2c219 4639 }
7d0393cf 4640
4458cf11
KH
4641 /* Draw relief if not yet drawn. */
4642 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
4643 x_draw_glyph_string_box (s);
4644 }
7d0393cf 4645
06a2c219
GM
4646 /* Reset clipping. */
4647 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4648}
07e34cb0 4649
06a2c219 4650
b4192550
KH
4651static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4652 struct face **, int));
06a2c219 4653
06a2c219 4654
209f68d9 4655/* Fill glyph string S with composition components specified by S->cmp.
7d0393cf 4656
b4192550
KH
4657 FACES is an array of faces for all components of this composition.
4658 S->gidx is the index of the first component for S.
4659 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4660 use its physical height for clipping.
06a2c219 4661
b4192550 4662 Value is the index of a component not in S. */
07e34cb0 4663
b4192550
KH
4664static int
4665x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4666 struct glyph_string *s;
b4192550 4667 struct face **faces;
66ac4b0e 4668 int overlaps_p;
07e34cb0 4669{
b4192550 4670 int i;
06a2c219 4671
b4192550 4672 xassert (s);
06a2c219 4673
b4192550 4674 s->for_overlaps_p = overlaps_p;
7d0393cf 4675
b4192550
KH
4676 s->face = faces[s->gidx];
4677 s->font = s->face->font;
4678 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4679
b4192550
KH
4680 /* For all glyphs of this composition, starting at the offset
4681 S->gidx, until we reach the end of the definition or encounter a
4682 glyph that requires the different face, add it to S. */
4683 ++s->nchars;
4684 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4685 ++s->nchars;
06a2c219 4686
b4192550
KH
4687 /* All glyph strings for the same composition has the same width,
4688 i.e. the width set for the first component of the composition. */
06a2c219 4689
06a2c219
GM
4690 s->width = s->first_glyph->pixel_width;
4691
4692 /* If the specified font could not be loaded, use the frame's
4693 default font, but record the fact that we couldn't load it in
4694 the glyph string so that we can draw rectangles for the
4695 characters of the glyph string. */
4696 if (s->font == NULL)
4697 {
4698 s->font_not_found_p = 1;
4699 s->font = FRAME_FONT (s->f);
4700 }
4701
4702 /* Adjust base line for subscript/superscript text. */
4703 s->ybase += s->first_glyph->voffset;
7d0393cf 4704
06a2c219
GM
4705 xassert (s->face && s->face->gc);
4706
4707 /* This glyph string must always be drawn with 16-bit functions. */
4708 s->two_byte_p = 1;
b4192550
KH
4709
4710 return s->gidx + s->nchars;
06a2c219
GM
4711}
4712
4713
209f68d9 4714/* Fill glyph string S from a sequence of character glyphs.
7d0393cf 4715
06a2c219 4716 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4717 first glyph to consider, END is the index of the last + 1.
4718 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4719 use its physical height for clipping.
66ac4b0e
GM
4720
4721 Value is the index of the first glyph not in S. */
06a2c219
GM
4722
4723static int
66ac4b0e 4724x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4725 struct glyph_string *s;
4726 int face_id;
66ac4b0e 4727 int start, end, overlaps_p;
06a2c219
GM
4728{
4729 struct glyph *glyph, *last;
4730 int voffset;
ee569018 4731 int glyph_not_available_p;
7d0393cf 4732
06a2c219
GM
4733 xassert (s->f == XFRAME (s->w->frame));
4734 xassert (s->nchars == 0);
4735 xassert (start >= 0 && end > start);
4736
66ac4b0e 4737 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4738 glyph = s->row->glyphs[s->area] + start;
4739 last = s->row->glyphs[s->area] + end;
4740 voffset = glyph->voffset;
7d0393cf 4741
ee569018
KH
4742 glyph_not_available_p = glyph->glyph_not_available_p;
4743
06a2c219
GM
4744 while (glyph < last
4745 && glyph->type == CHAR_GLYPH
4746 && glyph->voffset == voffset
ee569018
KH
4747 /* Same face id implies same font, nowadays. */
4748 && glyph->face_id == face_id
4749 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4750 {
ee569018
KH
4751 int two_byte_p;
4752
06a2c219 4753 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4754 s->char2b + s->nchars,
4755 &two_byte_p);
4756 s->two_byte_p = two_byte_p;
06a2c219
GM
4757 ++s->nchars;
4758 xassert (s->nchars <= end - start);
4759 s->width += glyph->pixel_width;
4760 ++glyph;
4761 }
4762
4763 s->font = s->face->font;
4764 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
7d0393cf 4765
06a2c219
GM
4766 /* If the specified font could not be loaded, use the frame's font,
4767 but record the fact that we couldn't load it in
4768 S->font_not_found_p so that we can draw rectangles for the
4769 characters of the glyph string. */
ee569018 4770 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4771 {
4772 s->font_not_found_p = 1;
4773 s->font = FRAME_FONT (s->f);
4774 }
4775
4776 /* Adjust base line for subscript/superscript text. */
4777 s->ybase += voffset;
66ac4b0e 4778
06a2c219
GM
4779 xassert (s->face && s->face->gc);
4780 return glyph - s->row->glyphs[s->area];
07e34cb0 4781}
dc6f92b8 4782
06a2c219
GM
4783
4784/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4785
dfcf069d 4786static void
06a2c219
GM
4787x_fill_image_glyph_string (s)
4788 struct glyph_string *s;
4789{
4790 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4791 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4792 xassert (s->img);
43d120d8 4793 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4794 s->font = s->face->font;
4795 s->width = s->first_glyph->pixel_width;
7d0393cf 4796
06a2c219
GM
4797 /* Adjust base line for subscript/superscript text. */
4798 s->ybase += s->first_glyph->voffset;
4799}
4800
4801
209f68d9 4802/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4803
209f68d9
GM
4804 ROW is the glyph row in which the glyphs are found, AREA is the
4805 area within the row. START is the index of the first glyph to
4806 consider, END is the index of the last + 1.
4807
4808 Value is the index of the first glyph not in S. */
4809
4810static int
4811x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4812 struct glyph_string *s;
209f68d9
GM
4813 struct glyph_row *row;
4814 enum glyph_row_area area;
4815 int start, end;
06a2c219 4816{
209f68d9
GM
4817 struct glyph *glyph, *last;
4818 int voffset, face_id;
7d0393cf 4819
06a2c219 4820 xassert (s->first_glyph->type == STRETCH_GLYPH);
7d0393cf 4821
209f68d9
GM
4822 glyph = s->row->glyphs[s->area] + start;
4823 last = s->row->glyphs[s->area] + end;
4824 face_id = glyph->face_id;
4825 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4826 s->font = s->face->font;
209f68d9
GM
4827 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4828 s->width = glyph->pixel_width;
4829 voffset = glyph->voffset;
4830
4831 for (++glyph;
4832 (glyph < last
4833 && glyph->type == STRETCH_GLYPH
4834 && glyph->voffset == voffset
4835 && glyph->face_id == face_id);
4836 ++glyph)
4837 s->width += glyph->pixel_width;
7d0393cf 4838
06a2c219 4839 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4840 s->ybase += voffset;
4841
c296fc01
GM
4842 /* The case that face->gc == 0 is handled when drawing the glyph
4843 string by calling PREPARE_FACE_FOR_DISPLAY. */
4844 xassert (s->face);
209f68d9 4845 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4846}
4847
4848
4849/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4850 of XChar2b structures for S; it can't be allocated in
4851 x_init_glyph_string because it must be allocated via `alloca'. W
4852 is the window on which S is drawn. ROW and AREA are the glyph row
4853 and area within the row from which S is constructed. START is the
4854 index of the first glyph structure covered by S. HL is a
4855 face-override for drawing S. */
7d0393cf 4856
06a2c219
GM
4857static void
4858x_init_glyph_string (s, char2b, w, row, area, start, hl)
4859 struct glyph_string *s;
4860 XChar2b *char2b;
4861 struct window *w;
4862 struct glyph_row *row;
4863 enum glyph_row_area area;
4864 int start;
4865 enum draw_glyphs_face hl;
4866{
4867 bzero (s, sizeof *s);
4868 s->w = w;
4869 s->f = XFRAME (w->frame);
4870 s->display = FRAME_X_DISPLAY (s->f);
4871 s->window = FRAME_X_WINDOW (s->f);
4872 s->char2b = char2b;
4873 s->hl = hl;
4874 s->row = row;
4875 s->area = area;
4876 s->first_glyph = row->glyphs[area] + start;
4877 s->height = row->height;
4878 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4879
9ea173e8
GM
4880 /* Display the internal border below the tool-bar window. */
4881 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 4882 s->y -= s->f->output_data.x->internal_border_width;
7d0393cf 4883
06a2c219
GM
4884 s->ybase = s->y + row->ascent;
4885}
4886
4887
4888/* Set background width of glyph string S. START is the index of the
4889 first glyph following S. LAST_X is the right-most x-position + 1
4890 in the drawing area. */
4891
4892static INLINE void
4893x_set_glyph_string_background_width (s, start, last_x)
4894 struct glyph_string *s;
4895 int start;
4896 int last_x;
4897{
4898 /* If the face of this glyph string has to be drawn to the end of
4899 the drawing area, set S->extends_to_end_of_line_p. */
4900 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
7d0393cf 4901
06a2c219 4902 if (start == s->row->used[s->area]
eb79f5cc 4903 && s->area == TEXT_AREA
7b0870b2
GM
4904 && ((s->hl == DRAW_NORMAL_TEXT
4905 && (s->row->fill_line_p
4906 || s->face->background != default_face->background
4907 || s->face->stipple != default_face->stipple
4908 || s->row->mouse_face_p))
327f42ee
GM
4909 || s->hl == DRAW_MOUSE_FACE
4910 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4911 && s->row->fill_line_p)))
7b0870b2 4912 s->extends_to_end_of_line_p = 1;
7d0393cf 4913
06a2c219
GM
4914 /* If S extends its face to the end of the line, set its
4915 background_width to the distance to the right edge of the drawing
4916 area. */
4917 if (s->extends_to_end_of_line_p)
1da3fd71 4918 s->background_width = last_x - s->x + 1;
06a2c219
GM
4919 else
4920 s->background_width = s->width;
4921}
4922
4923
4924/* Add a glyph string for a stretch glyph to the list of strings
4925 between HEAD and TAIL. START is the index of the stretch glyph in
4926 row area AREA of glyph row ROW. END is the index of the last glyph
4927 in that glyph row area. X is the current output position assigned
4928 to the new glyph string constructed. HL overrides that face of the
4929 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4930 is the right-most x-position of the drawing area. */
4931
8abee2e1
DL
4932/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4933 and below -- keep them on one line. */
4934#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4935 do \
4936 { \
4937 s = (struct glyph_string *) alloca (sizeof *s); \
4938 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4939 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4940 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4941 s->x = (X); \
4942 } \
4943 while (0)
4944
4945
4946/* Add a glyph string for an image glyph to the list of strings
4947 between HEAD and TAIL. START is the index of the image glyph in
4948 row area AREA of glyph row ROW. END is the index of the last glyph
4949 in that glyph row area. X is the current output position assigned
4950 to the new glyph string constructed. HL overrides that face of the
4951 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4952 is the right-most x-position of the drawing area. */
4953
8abee2e1 4954#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4955 do \
4956 { \
4957 s = (struct glyph_string *) alloca (sizeof *s); \
4958 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4959 x_fill_image_glyph_string (s); \
4960 x_append_glyph_string (&HEAD, &TAIL, s); \
4961 ++START; \
4962 s->x = (X); \
4963 } \
4964 while (0)
4965
4966
4967/* Add a glyph string for a sequence of character glyphs to the list
4968 of strings between HEAD and TAIL. START is the index of the first
4969 glyph in row area AREA of glyph row ROW that is part of the new
4970 glyph string. END is the index of the last glyph in that glyph row
4971 area. X is the current output position assigned to the new glyph
4972 string constructed. HL overrides that face of the glyph; e.g. it
4973 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4974 right-most x-position of the drawing area. */
4975
8abee2e1 4976#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4977 do \
4978 { \
3e71d8f2 4979 int c, face_id; \
06a2c219
GM
4980 XChar2b *char2b; \
4981 \
43d120d8 4982 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4983 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4984 \
b4192550
KH
4985 s = (struct glyph_string *) alloca (sizeof *s); \
4986 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4987 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4988 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4989 s->x = (X); \
4990 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4991 OVERLAPS_P); \
06a2c219
GM
4992 } \
4993 while (0)
7d0393cf 4994
06a2c219 4995
b4192550
KH
4996/* Add a glyph string for a composite sequence to the list of strings
4997 between HEAD and TAIL. START is the index of the first glyph in
4998 row area AREA of glyph row ROW that is part of the new glyph
4999 string. END is the index of the last glyph in that glyph row area.
5000 X is the current output position assigned to the new glyph string
5001 constructed. HL overrides that face of the glyph; e.g. it is
5002 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
5003 x-position of the drawing area. */
5004
6c27ec25 5005#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 5006 do { \
43d120d8
KH
5007 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
5008 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 5009 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
5010 struct composition *cmp = composition_table[cmp_id]; \
5011 int glyph_len = cmp->glyph_len; \
5012 XChar2b *char2b; \
5013 struct face **faces; \
5014 struct glyph_string *first_s = NULL; \
5015 int n; \
5016 \
ee569018 5017 base_face = base_face->ascii_face; \
b4192550
KH
5018 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
5019 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
5020 /* At first, fill in `char2b' and `faces'. */ \
5021 for (n = 0; n < glyph_len; n++) \
5022 { \
43d120d8 5023 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
5024 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
5025 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
5026 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
abfb6b46 5027 this_face_id, char2b + n, 1, 1); \
b4192550
KH
5028 } \
5029 \
5030 /* Make glyph_strings for each glyph sequence that is drawable by \
5031 the same face, and append them to HEAD/TAIL. */ \
5032 for (n = 0; n < cmp->glyph_len;) \
5033 { \
5034 s = (struct glyph_string *) alloca (sizeof *s); \
5035 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
5036 x_append_glyph_string (&(HEAD), &(TAIL), s); \
5037 s->cmp = cmp; \
5038 s->gidx = n; \
b4192550
KH
5039 s->x = (X); \
5040 \
5041 if (n == 0) \
5042 first_s = s; \
5043 \
5044 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
5045 } \
5046 \
5047 ++START; \
5048 s = first_s; \
5049 } while (0)
7d0393cf 5050
b4192550 5051
06a2c219
GM
5052/* Build a list of glyph strings between HEAD and TAIL for the glyphs
5053 of AREA of glyph row ROW on window W between indices START and END.
5054 HL overrides the face for drawing glyph strings, e.g. it is
5055 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5056 x-positions of the drawing area.
5057
5058 This is an ugly monster macro construct because we must use alloca
5059 to allocate glyph strings (because x_draw_glyphs can be called
5060 asynchronously). */
5061
8abee2e1 5062#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
5063 do \
5064 { \
5065 HEAD = TAIL = NULL; \
5066 while (START < END) \
5067 { \
5068 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5069 switch (first_glyph->type) \
5070 { \
5071 case CHAR_GLYPH: \
5072 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
5073 TAIL, HL, X, LAST_X, \
5074 OVERLAPS_P); \
06a2c219
GM
5075 break; \
5076 \
b4192550
KH
5077 case COMPOSITE_GLYPH: \
5078 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5079 HEAD, TAIL, HL, X, LAST_X,\
5080 OVERLAPS_P); \
5081 break; \
5082 \
06a2c219
GM
5083 case STRETCH_GLYPH: \
5084 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5085 HEAD, TAIL, HL, X, LAST_X); \
5086 break; \
5087 \
5088 case IMAGE_GLYPH: \
5089 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5090 TAIL, HL, X, LAST_X); \
5091 break; \
5092 \
5093 default: \
5094 abort (); \
5095 } \
5096 \
5097 x_set_glyph_string_background_width (s, START, LAST_X); \
5098 (X) += s->width; \
5099 } \
5100 } \
5101 while (0)
5102
5103
5104/* Draw glyphs between START and END in AREA of ROW on window W,
5105 starting at x-position X. X is relative to AREA in W. HL is a
5106 face-override with the following meaning:
5107
5108 DRAW_NORMAL_TEXT draw normally
5109 DRAW_CURSOR draw in cursor face
5110 DRAW_MOUSE_FACE draw in mouse face.
5111 DRAW_INVERSE_VIDEO draw in mode line face
5112 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5113 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5114
66ac4b0e
GM
5115 If OVERLAPS_P is non-zero, draw only the foreground of characters
5116 and clip to the physical height of ROW.
5117
06a2c219 5118 Value is the x-position reached, relative to AREA of W. */
7d0393cf 5119
06a2c219 5120static int
f0a48a01 5121x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
06a2c219
GM
5122 struct window *w;
5123 int x;
5124 struct glyph_row *row;
5125 enum glyph_row_area area;
5126 int start, end;
5127 enum draw_glyphs_face hl;
66ac4b0e 5128 int overlaps_p;
dc6f92b8 5129{
06a2c219
GM
5130 struct glyph_string *head, *tail;
5131 struct glyph_string *s;
5132 int last_x, area_width;
5133 int x_reached;
5134 int i, j;
5135
5136 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 5137 end = min (end, row->used[area]);
a8710abf
GM
5138 start = max (0, start);
5139 start = min (end, start);
06a2c219
GM
5140
5141 /* Translate X to frame coordinates. Set last_x to the right
5142 end of the drawing area. */
5143 if (row->full_width_p)
5144 {
5145 /* X is relative to the left edge of W, without scroll bars
3f332ef3 5146 or fringes. */
06a2c219 5147 struct frame *f = XFRAME (w->frame);
06a2c219 5148 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 5149
06a2c219
GM
5150 x += window_left_x;
5151 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5152 last_x = window_left_x + area_width;
5153
5154 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5155 {
110859fc 5156 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
5157 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5158 last_x += width;
5159 else
5160 x -= width;
5161 }
dc6f92b8 5162
b9432a85 5163 x += FRAME_INTERNAL_BORDER_WIDTH (f);
98fedd97 5164 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
5165 }
5166 else
dc6f92b8 5167 {
06a2c219
GM
5168 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5169 area_width = window_box_width (w, area);
5170 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
5171 }
5172
06a2c219
GM
5173 /* Build a doubly-linked list of glyph_string structures between
5174 head and tail from what we have to draw. Note that the macro
5175 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5176 the reason we use a separate variable `i'. */
5177 i = start;
66ac4b0e
GM
5178 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5179 overlaps_p);
06a2c219
GM
5180 if (tail)
5181 x_reached = tail->x + tail->background_width;
5182 else
5183 x_reached = x;
90e65f07 5184
06a2c219
GM
5185 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5186 the row, redraw some glyphs in front or following the glyph
5187 strings built above. */
a8710abf 5188 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
5189 {
5190 int dummy_x = 0;
5191 struct glyph_string *h, *t;
5192
5193 /* Compute overhangs for all glyph strings. */
5194 for (s = head; s; s = s->next)
5195 x_compute_glyph_string_overhangs (s);
5196
5197 /* Prepend glyph strings for glyphs in front of the first glyph
5198 string that are overwritten because of the first glyph
5199 string's left overhang. The background of all strings
7d0393cf 5200 prepended must be drawn because the first glyph string
06a2c219
GM
5201 draws over it. */
5202 i = x_left_overwritten (head);
5203 if (i >= 0)
5204 {
5205 j = i;
5206 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
5207 DRAW_NORMAL_TEXT, dummy_x, last_x,
5208 overlaps_p);
06a2c219 5209 start = i;
06a2c219
GM
5210 x_compute_overhangs_and_x (t, head->x, 1);
5211 x_prepend_glyph_string_lists (&head, &tail, h, t);
5212 }
58769bee 5213
06a2c219
GM
5214 /* Prepend glyph strings for glyphs in front of the first glyph
5215 string that overwrite that glyph string because of their
5216 right overhang. For these strings, only the foreground must
5217 be drawn, because it draws over the glyph string at `head'.
5218 The background must not be drawn because this would overwrite
5219 right overhangs of preceding glyphs for which no glyph
5220 strings exist. */
5221 i = x_left_overwriting (head);
5222 if (i >= 0)
5223 {
5224 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
5225 DRAW_NORMAL_TEXT, dummy_x, last_x,
5226 overlaps_p);
06a2c219
GM
5227 for (s = h; s; s = s->next)
5228 s->background_filled_p = 1;
06a2c219
GM
5229 x_compute_overhangs_and_x (t, head->x, 1);
5230 x_prepend_glyph_string_lists (&head, &tail, h, t);
5231 }
dbcb258a 5232
06a2c219
GM
5233 /* Append glyphs strings for glyphs following the last glyph
5234 string tail that are overwritten by tail. The background of
5235 these strings has to be drawn because tail's foreground draws
5236 over it. */
5237 i = x_right_overwritten (tail);
5238 if (i >= 0)
5239 {
5240 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5241 DRAW_NORMAL_TEXT, x, last_x,
5242 overlaps_p);
06a2c219
GM
5243 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5244 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219 5245 }
dc6f92b8 5246
06a2c219
GM
5247 /* Append glyph strings for glyphs following the last glyph
5248 string tail that overwrite tail. The foreground of such
5249 glyphs has to be drawn because it writes into the background
5250 of tail. The background must not be drawn because it could
5251 paint over the foreground of following glyphs. */
5252 i = x_right_overwriting (tail);
5253 if (i >= 0)
5254 {
5255 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5256 DRAW_NORMAL_TEXT, x, last_x,
5257 overlaps_p);
06a2c219
GM
5258 for (s = h; s; s = s->next)
5259 s->background_filled_p = 1;
5260 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5261 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219
GM
5262 }
5263 }
58769bee 5264
06a2c219
GM
5265 /* Draw all strings. */
5266 for (s = head; s; s = s->next)
5267 x_draw_glyph_string (s);
dc6f92b8 5268
bb313871
GM
5269 if (area == TEXT_AREA
5270 && !row->full_width_p
5271 /* When drawing overlapping rows, only the glyph strings'
5272 foreground is drawn, which doesn't erase a cursor
5273 completely. */
5274 && !overlaps_p)
f0a48a01
GM
5275 {
5276 int x0 = head ? head->x : x;
5277 int x1 = tail ? tail->x + tail->background_width : x;
7d0393cf 5278
f0a48a01
GM
5279 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5280 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
7d0393cf 5281
bb313871 5282 if (XFASTINT (w->left_margin_width) != 0)
f0a48a01
GM
5283 {
5284 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5285 x0 -= left_area_width;
5286 x1 -= left_area_width;
5287 }
5288
60626bab
GM
5289 notice_overwritten_cursor (w, area, x0, x1,
5290 row->y, MATRIX_ROW_BOTTOM_Y (row));
f0a48a01
GM
5291 }
5292
06a2c219
GM
5293 /* Value is the x-position up to which drawn, relative to AREA of W.
5294 This doesn't include parts drawn because of overhangs. */
5295 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5296 if (!row->full_width_p)
5297 {
f0a48a01 5298 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
06a2c219
GM
5299 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5300 if (area > TEXT_AREA)
5301 x_reached -= window_box_width (w, TEXT_AREA);
5302 }
7d0393cf 5303
06a2c219
GM
5304 return x_reached;
5305}
dc6f92b8 5306
dc6f92b8 5307
66ac4b0e
GM
5308/* Fix the display of area AREA of overlapping row ROW in window W. */
5309
5310static void
5311x_fix_overlapping_area (w, row, area)
5312 struct window *w;
5313 struct glyph_row *row;
5314 enum glyph_row_area area;
5315{
5316 int i, x;
7d0393cf 5317
66ac4b0e 5318 BLOCK_INPUT;
7d0393cf 5319
66ac4b0e
GM
5320 if (area == LEFT_MARGIN_AREA)
5321 x = 0;
5322 else if (area == TEXT_AREA)
5323 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5324 else
5325 x = (window_box_width (w, LEFT_MARGIN_AREA)
5326 + window_box_width (w, TEXT_AREA));
5327
5328 for (i = 0; i < row->used[area];)
5329 {
5330 if (row->glyphs[area][i].overlaps_vertically_p)
5331 {
5332 int start = i, start_x = x;
5333
5334 do
5335 {
5336 x += row->glyphs[area][i].pixel_width;
5337 ++i;
5338 }
5339 while (i < row->used[area]
5340 && row->glyphs[area][i].overlaps_vertically_p);
5341
5342 x_draw_glyphs (w, start_x, row, area, start, i,
f0a48a01 5343 DRAW_NORMAL_TEXT, 1);
66ac4b0e
GM
5344 }
5345 else
5346 {
5347 x += row->glyphs[area][i].pixel_width;
5348 ++i;
5349 }
5350 }
7d0393cf 5351
66ac4b0e
GM
5352 UNBLOCK_INPUT;
5353}
5354
5355
06a2c219
GM
5356/* Output LEN glyphs starting at START at the nominal cursor position.
5357 Advance the nominal cursor over the text. The global variable
5358 updated_window contains the window being updated, updated_row is
5359 the glyph row being updated, and updated_area is the area of that
5360 row being updated. */
dc6f92b8 5361
06a2c219
GM
5362static void
5363x_write_glyphs (start, len)
5364 struct glyph *start;
5365 int len;
5366{
f0a48a01 5367 int x, hpos;
d9cdbb3d 5368
06a2c219 5369 xassert (updated_window && updated_row);
dc6f92b8 5370 BLOCK_INPUT;
7d0393cf 5371
06a2c219 5372 /* Write glyphs. */
dc6f92b8 5373
06a2c219
GM
5374 hpos = start - updated_row->glyphs[updated_area];
5375 x = x_draw_glyphs (updated_window, output_cursor.x,
5376 updated_row, updated_area,
5377 hpos, hpos + len,
f0a48a01 5378 DRAW_NORMAL_TEXT, 0);
dc6f92b8 5379
6065f9e2
KS
5380 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
5381 if (updated_area == TEXT_AREA
5382 && updated_window->phys_cursor_on_p
5383 && updated_window->phys_cursor.vpos == output_cursor.vpos
5384 && updated_window->phys_cursor.hpos >= hpos
5385 && updated_window->phys_cursor.hpos < hpos + len)
5386 updated_window->phys_cursor_on_p = 0;
5387
dc6f92b8 5388 UNBLOCK_INPUT;
7d0393cf 5389
06a2c219
GM
5390 /* Advance the output cursor. */
5391 output_cursor.hpos += len;
5392 output_cursor.x = x;
dc6f92b8
JB
5393}
5394
0cdd0c9f 5395
06a2c219 5396/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5397
06a2c219
GM
5398static void
5399x_insert_glyphs (start, len)
5400 struct glyph *start;
5401 register int len;
5402{
5403 struct frame *f;
5404 struct window *w;
5405 int line_height, shift_by_width, shifted_region_width;
5406 struct glyph_row *row;
5407 struct glyph *glyph;
2beb36f9 5408 int frame_x, frame_y, hpos;
58769bee 5409
06a2c219 5410 xassert (updated_window && updated_row);
0cdd0c9f 5411 BLOCK_INPUT;
06a2c219
GM
5412 w = updated_window;
5413 f = XFRAME (WINDOW_FRAME (w));
5414
5415 /* Get the height of the line we are in. */
5416 row = updated_row;
5417 line_height = row->height;
5418
5419 /* Get the width of the glyphs to insert. */
5420 shift_by_width = 0;
5421 for (glyph = start; glyph < start + len; ++glyph)
5422 shift_by_width += glyph->pixel_width;
5423
5424 /* Get the width of the region to shift right. */
5425 shifted_region_width = (window_box_width (w, updated_area)
5426 - output_cursor.x
5427 - shift_by_width);
5428
5429 /* Shift right. */
bf0ab8a2 5430 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5431 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5432 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5433 f->output_data.x->normal_gc,
5434 frame_x, frame_y,
5435 shifted_region_width, line_height,
5436 frame_x + shift_by_width, frame_y);
5437
5438 /* Write the glyphs. */
5439 hpos = start - row->glyphs[updated_area];
5440 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
f0a48a01 5441 DRAW_NORMAL_TEXT, 0);
7d0393cf 5442
06a2c219
GM
5443 /* Advance the output cursor. */
5444 output_cursor.hpos += len;
5445 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5446 UNBLOCK_INPUT;
5447}
0cdd0c9f 5448
0cdd0c9f 5449
06a2c219
GM
5450/* Delete N glyphs at the nominal cursor position. Not implemented
5451 for X frames. */
c83febd7
RS
5452
5453static void
06a2c219
GM
5454x_delete_glyphs (n)
5455 register int n;
c83febd7 5456{
06a2c219 5457 abort ();
c83febd7
RS
5458}
5459
0cdd0c9f 5460
c5e6e06b
GM
5461/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5462 If they are <= 0, this is probably an error. */
5463
5464void
5465x_clear_area (dpy, window, x, y, width, height, exposures)
5466 Display *dpy;
5467 Window window;
5468 int x, y;
5469 int width, height;
5470 int exposures;
5471{
5472 xassert (width > 0 && height > 0);
5473 XClearArea (dpy, window, x, y, width, height, exposures);
5474}
5475
5476
06a2c219
GM
5477/* Erase the current text line from the nominal cursor position
5478 (inclusive) to pixel column TO_X (exclusive). The idea is that
5479 everything from TO_X onward is already erased.
5480
5481 TO_X is a pixel position relative to updated_area of
5482 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5483
06a2c219
GM
5484static void
5485x_clear_end_of_line (to_x)
5486 int to_x;
5487{
5488 struct frame *f;
5489 struct window *w = updated_window;
5490 int max_x, min_y, max_y;
5491 int from_x, from_y, to_y;
7d0393cf 5492
06a2c219
GM
5493 xassert (updated_window && updated_row);
5494 f = XFRAME (w->frame);
7d0393cf 5495
06a2c219
GM
5496 if (updated_row->full_width_p)
5497 {
5498 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5499 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5500 && !w->pseudo_window_p)
5501 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5502 }
06a2c219
GM
5503 else
5504 max_x = window_box_width (w, updated_area);
5505 max_y = window_text_bottom_y (w);
dc6f92b8 5506
06a2c219
GM
5507 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5508 of window. For TO_X > 0, truncate to end of drawing area. */
5509 if (to_x == 0)
5510 return;
5511 else if (to_x < 0)
5512 to_x = max_x;
5513 else
5514 to_x = min (to_x, max_x);
dbc4e1c1 5515
06a2c219 5516 to_y = min (max_y, output_cursor.y + updated_row->height);
7d0393cf 5517
06a2c219
GM
5518 /* Notice if the cursor will be cleared by this operation. */
5519 if (!updated_row->full_width_p)
60626bab
GM
5520 notice_overwritten_cursor (w, updated_area,
5521 output_cursor.x, -1,
5522 updated_row->y,
5523 MATRIX_ROW_BOTTOM_Y (updated_row));
dbc4e1c1 5524
06a2c219 5525 from_x = output_cursor.x;
7d0393cf 5526
06a2c219
GM
5527 /* Translate to frame coordinates. */
5528 if (updated_row->full_width_p)
5529 {
5530 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5531 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5532 }
0cdd0c9f
RS
5533 else
5534 {
06a2c219
GM
5535 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5536 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5537 }
7d0393cf 5538
045dee35 5539 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5540 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5541 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
7d0393cf 5542
06a2c219
GM
5543 /* Prevent inadvertently clearing to end of the X window. */
5544 if (to_x > from_x && to_y > from_y)
5545 {
5546 BLOCK_INPUT;
c5e6e06b
GM
5547 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5548 from_x, from_y, to_x - from_x, to_y - from_y,
5549 False);
06a2c219 5550 UNBLOCK_INPUT;
0cdd0c9f 5551 }
0cdd0c9f 5552}
dbc4e1c1 5553
0cdd0c9f 5554
06a2c219 5555/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5556 frame. Otherwise clear the selected frame. */
06a2c219
GM
5557
5558static void
5559x_clear_frame ()
0cdd0c9f 5560{
06a2c219 5561 struct frame *f;
0cdd0c9f 5562
06a2c219
GM
5563 if (updating_frame)
5564 f = updating_frame;
0cdd0c9f 5565 else
b86bd3dd 5566 f = SELECTED_FRAME ();
58769bee 5567
06a2c219
GM
5568 /* Clearing the frame will erase any cursor, so mark them all as no
5569 longer visible. */
5570 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5571 output_cursor.hpos = output_cursor.vpos = 0;
5572 output_cursor.x = -1;
5573
5574 /* We don't set the output cursor here because there will always
5575 follow an explicit cursor_to. */
5576 BLOCK_INPUT;
5577 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5578
5579 /* We have to clear the scroll bars, too. If we have changed
5580 colors or something like that, then they should be notified. */
5581 x_scroll_bar_clear (f);
0cdd0c9f 5582
06a2c219
GM
5583 XFlush (FRAME_X_DISPLAY (f));
5584 UNBLOCK_INPUT;
dc6f92b8 5585}
06a2c219
GM
5586
5587
dc6f92b8 5588\f
dbc4e1c1
JB
5589/* Invert the middle quarter of the frame for .15 sec. */
5590
06a2c219
GM
5591/* We use the select system call to do the waiting, so we have to make
5592 sure it's available. If it isn't, we just won't do visual bells. */
5593
dbc4e1c1
JB
5594#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5595
06a2c219
GM
5596
5597/* Subtract the `struct timeval' values X and Y, storing the result in
5598 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5599
5600static int
5601timeval_subtract (result, x, y)
5602 struct timeval *result, x, y;
5603{
06a2c219
GM
5604 /* Perform the carry for the later subtraction by updating y. This
5605 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5606 if (x.tv_usec < y.tv_usec)
5607 {
5608 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5609 y.tv_usec -= 1000000 * nsec;
5610 y.tv_sec += nsec;
5611 }
7d0393cf 5612
dbc4e1c1
JB
5613 if (x.tv_usec - y.tv_usec > 1000000)
5614 {
5615 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5616 y.tv_usec += 1000000 * nsec;
5617 y.tv_sec -= nsec;
5618 }
5619
06a2c219
GM
5620 /* Compute the time remaining to wait. tv_usec is certainly
5621 positive. */
dbc4e1c1
JB
5622 result->tv_sec = x.tv_sec - y.tv_sec;
5623 result->tv_usec = x.tv_usec - y.tv_usec;
5624
06a2c219
GM
5625 /* Return indication of whether the result should be considered
5626 negative. */
dbc4e1c1
JB
5627 return x.tv_sec < y.tv_sec;
5628}
dc6f92b8 5629
dfcf069d 5630void
f676886a
JB
5631XTflash (f)
5632 struct frame *f;
dc6f92b8 5633{
dbc4e1c1 5634 BLOCK_INPUT;
dc6f92b8 5635
dbc4e1c1
JB
5636 {
5637 GC gc;
dc6f92b8 5638
06a2c219
GM
5639 /* Create a GC that will use the GXxor function to flip foreground
5640 pixels into background pixels. */
dbc4e1c1
JB
5641 {
5642 XGCValues values;
dc6f92b8 5643
dbc4e1c1 5644 values.function = GXxor;
7556890b
RS
5645 values.foreground = (f->output_data.x->foreground_pixel
5646 ^ f->output_data.x->background_pixel);
58769bee 5647
334208b7 5648 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5649 GCFunction | GCForeground, &values);
5650 }
dc6f92b8 5651
dbc4e1c1 5652 {
e84e14c3
RS
5653 /* Get the height not including a menu bar widget. */
5654 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5655 /* Height of each line to flash. */
5656 int flash_height = FRAME_LINE_HEIGHT (f);
5657 /* These will be the left and right margins of the rectangles. */
5658 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5659 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5660
5661 int width;
5662
5663 /* Don't flash the area between a scroll bar and the frame
5664 edge it is next to. */
5665 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5666 {
5667 case vertical_scroll_bar_left:
5668 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5669 break;
5670
5671 case vertical_scroll_bar_right:
5672 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5673 break;
06a2c219
GM
5674
5675 default:
5676 break;
e84e14c3
RS
5677 }
5678
5679 width = flash_right - flash_left;
5680
5681 /* If window is tall, flash top and bottom line. */
5682 if (height > 3 * FRAME_LINE_HEIGHT (f))
5683 {
5684 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5685 flash_left,
5686 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5687 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5688 width, flash_height);
5689 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5690 flash_left,
5691 (height - flash_height
5692 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5693 width, flash_height);
5694 }
5695 else
7d0393cf 5696 /* If it is short, flash it all. */
e84e14c3
RS
5697 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5698 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5699 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5700
06a2c219 5701 x_flush (f);
dc6f92b8 5702
dbc4e1c1 5703 {
06a2c219 5704 struct timeval wakeup;
dc6f92b8 5705
66c30ea1 5706 EMACS_GET_TIME (wakeup);
dc6f92b8 5707
dbc4e1c1
JB
5708 /* Compute time to wait until, propagating carry from usecs. */
5709 wakeup.tv_usec += 150000;
5710 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5711 wakeup.tv_usec %= 1000000;
5712
101922c3
GM
5713 /* Keep waiting until past the time wakeup or any input gets
5714 available. */
5715 while (! detect_input_pending ())
dbc4e1c1 5716 {
101922c3 5717 struct timeval current;
dbc4e1c1
JB
5718 struct timeval timeout;
5719
101922c3 5720 EMACS_GET_TIME (current);
dbc4e1c1 5721
101922c3
GM
5722 /* Break if result would be negative. */
5723 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5724 break;
5725
101922c3
GM
5726 /* How long `select' should wait. */
5727 timeout.tv_sec = 0;
5728 timeout.tv_usec = 10000;
5729
dbc4e1c1 5730 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5731 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5732 }
5733 }
58769bee 5734
e84e14c3
RS
5735 /* If window is tall, flash top and bottom line. */
5736 if (height > 3 * FRAME_LINE_HEIGHT (f))
5737 {
5738 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5739 flash_left,
5740 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5741 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5742 width, flash_height);
5743 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5744 flash_left,
5745 (height - flash_height
5746 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5747 width, flash_height);
5748 }
5749 else
7d0393cf 5750 /* If it is short, flash it all. */
e84e14c3
RS
5751 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5752 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5753 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5754
334208b7 5755 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5756 x_flush (f);
dc6f92b8 5757 }
dbc4e1c1
JB
5758 }
5759
5760 UNBLOCK_INPUT;
dc6f92b8
JB
5761}
5762
06a2c219 5763#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5764
5765
dc6f92b8
JB
5766/* Make audible bell. */
5767
dfcf069d 5768void
dc6f92b8
JB
5769XTring_bell ()
5770{
b86bd3dd 5771 struct frame *f = SELECTED_FRAME ();
7d0393cf 5772
b86bd3dd
GM
5773 if (FRAME_X_DISPLAY (f))
5774 {
dbc4e1c1 5775#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5776 if (visible_bell)
5777 XTflash (f);
5778 else
dbc4e1c1 5779#endif
b86bd3dd
GM
5780 {
5781 BLOCK_INPUT;
5782 XBell (FRAME_X_DISPLAY (f), 0);
5783 XFlush (FRAME_X_DISPLAY (f));
5784 UNBLOCK_INPUT;
5785 }
dc6f92b8
JB
5786 }
5787}
06a2c219 5788
dc6f92b8 5789\f
06a2c219
GM
5790/* Specify how many text lines, from the top of the window,
5791 should be affected by insert-lines and delete-lines operations.
5792 This, and those operations, are used only within an update
5793 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5794
dfcf069d 5795static void
06a2c219
GM
5796XTset_terminal_window (n)
5797 register int n;
dc6f92b8 5798{
06a2c219 5799 /* This function intentionally left blank. */
dc6f92b8
JB
5800}
5801
06a2c219
GM
5802
5803\f
5804/***********************************************************************
5805 Line Dance
5806 ***********************************************************************/
5807
5808/* Perform an insert-lines or delete-lines operation, inserting N
5809 lines or deleting -N lines at vertical position VPOS. */
5810
dfcf069d 5811static void
06a2c219
GM
5812x_ins_del_lines (vpos, n)
5813 int vpos, n;
dc6f92b8
JB
5814{
5815 abort ();
5816}
06a2c219
GM
5817
5818
5819/* Scroll part of the display as described by RUN. */
dc6f92b8 5820
dfcf069d 5821static void
06a2c219
GM
5822x_scroll_run (w, run)
5823 struct window *w;
5824 struct run *run;
dc6f92b8 5825{
06a2c219
GM
5826 struct frame *f = XFRAME (w->frame);
5827 int x, y, width, height, from_y, to_y, bottom_y;
5828
5829 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
5830 without mode lines. Include in this box the left and right
5831 fringe of W. */
06a2c219 5832 window_box (w, -1, &x, &y, &width, &height);
3f332ef3
KS
5833 width += FRAME_X_FRINGE_WIDTH (f);
5834 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
06a2c219
GM
5835
5836 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5837 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5838 bottom_y = y + height;
dc6f92b8 5839
06a2c219
GM
5840 if (to_y < from_y)
5841 {
5842 /* Scrolling up. Make sure we don't copy part of the mode
5843 line at the bottom. */
5844 if (from_y + run->height > bottom_y)
5845 height = bottom_y - from_y;
5846 else
5847 height = run->height;
5848 }
dc6f92b8 5849 else
06a2c219
GM
5850 {
5851 /* Scolling down. Make sure we don't copy over the mode line.
5852 at the bottom. */
5853 if (to_y + run->height > bottom_y)
5854 height = bottom_y - to_y;
5855 else
5856 height = run->height;
5857 }
7a13e894 5858
06a2c219 5859 BLOCK_INPUT;
7d0393cf 5860
06a2c219
GM
5861 /* Cursor off. Will be switched on again in x_update_window_end. */
5862 updated_window = w;
5863 x_clear_cursor (w);
5864
5865 XCopyArea (FRAME_X_DISPLAY (f),
5866 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5867 f->output_data.x->normal_gc,
5868 x, from_y,
5869 width, height,
5870 x, to_y);
7d0393cf 5871
06a2c219
GM
5872 UNBLOCK_INPUT;
5873}
dc6f92b8 5874
dc6f92b8 5875
06a2c219
GM
5876\f
5877/***********************************************************************
5878 Exposure Events
5879 ***********************************************************************/
7d0393cf 5880
06a2c219
GM
5881/* Redisplay an exposed area of frame F. X and Y are the upper-left
5882 corner of the exposed rectangle. W and H are width and height of
5883 the exposed area. All are pixel values. W or H zero means redraw
5884 the entire frame. */
dc6f92b8 5885
06a2c219
GM
5886static void
5887expose_frame (f, x, y, w, h)
5888 struct frame *f;
5889 int x, y, w, h;
dc6f92b8 5890{
06a2c219 5891 XRectangle r;
82f053ab 5892 int mouse_face_overwritten_p = 0;
dc6f92b8 5893
06a2c219 5894 TRACE ((stderr, "expose_frame "));
dc6f92b8 5895
06a2c219
GM
5896 /* No need to redraw if frame will be redrawn soon. */
5897 if (FRAME_GARBAGED_P (f))
dc6f92b8 5898 {
06a2c219
GM
5899 TRACE ((stderr, " garbaged\n"));
5900 return;
5901 }
5902
5903 /* If basic faces haven't been realized yet, there is no point in
5904 trying to redraw anything. This can happen when we get an expose
5905 event while Emacs is starting, e.g. by moving another window. */
5906 if (FRAME_FACE_CACHE (f) == NULL
5907 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5908 {
5909 TRACE ((stderr, " no faces\n"));
5910 return;
58769bee 5911 }
06a2c219
GM
5912
5913 if (w == 0 || h == 0)
58769bee 5914 {
06a2c219
GM
5915 r.x = r.y = 0;
5916 r.width = CANON_X_UNIT (f) * f->width;
5917 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5918 }
5919 else
5920 {
06a2c219
GM
5921 r.x = x;
5922 r.y = y;
5923 r.width = w;
5924 r.height = h;
5925 }
5926
5927 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
82f053ab 5928 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
06a2c219 5929
9ea173e8 5930 if (WINDOWP (f->tool_bar_window))
82f053ab
GM
5931 mouse_face_overwritten_p
5932 |= expose_window (XWINDOW (f->tool_bar_window), &r);
06a2c219
GM
5933
5934#ifndef USE_X_TOOLKIT
5935 if (WINDOWP (f->menu_bar_window))
82f053ab
GM
5936 mouse_face_overwritten_p
5937 |= expose_window (XWINDOW (f->menu_bar_window), &r);
06a2c219 5938#endif /* not USE_X_TOOLKIT */
82f053ab
GM
5939
5940 /* Some window managers support a focus-follows-mouse style with
5941 delayed raising of frames. Imagine a partially obscured frame,
5942 and moving the mouse into partially obscured mouse-face on that
5943 frame. The visible part of the mouse-face will be highlighted,
5944 then the WM raises the obscured frame. With at least one WM, KDE
5945 2.1, Emacs is not getting any event for the raising of the frame
5946 (even tried with SubstructureRedirectMask), only Expose events.
5947 These expose events will draw text normally, i.e. not
5948 highlighted. Which means we must redo the highlight here.
5949 Subsume it under ``we love X''. --gerd 2001-08-15 */
5950 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5951 {
5952 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5953 if (f == dpyinfo->mouse_face_mouse_frame)
5954 {
5955 int x = dpyinfo->mouse_face_mouse_x;
5956 int y = dpyinfo->mouse_face_mouse_y;
5957 clear_mouse_face (dpyinfo);
5958 note_mouse_highlight (f, x, y);
5959 }
5960 }
dc6f92b8
JB
5961}
5962
06a2c219
GM
5963
5964/* Redraw (parts) of all windows in the window tree rooted at W that
82f053ab
GM
5965 intersect R. R contains frame pixel coordinates. Value is
5966 non-zero if the exposure overwrites mouse-face. */
06a2c219 5967
82f053ab 5968static int
06a2c219
GM
5969expose_window_tree (w, r)
5970 struct window *w;
5971 XRectangle *r;
dc6f92b8 5972{
82f053ab
GM
5973 struct frame *f = XFRAME (w->frame);
5974 int mouse_face_overwritten_p = 0;
7d0393cf 5975
82f053ab 5976 while (w && !FRAME_GARBAGED_P (f))
06a2c219
GM
5977 {
5978 if (!NILP (w->hchild))
82f053ab
GM
5979 mouse_face_overwritten_p
5980 |= expose_window_tree (XWINDOW (w->hchild), r);
06a2c219 5981 else if (!NILP (w->vchild))
82f053ab
GM
5982 mouse_face_overwritten_p
5983 |= expose_window_tree (XWINDOW (w->vchild), r);
5984 else
5985 mouse_face_overwritten_p |= expose_window (w, r);
7d0393cf 5986
a02f1be0 5987 w = NILP (w->next) ? NULL : XWINDOW (w->next);
06a2c219 5988 }
82f053ab
GM
5989
5990 return mouse_face_overwritten_p;
06a2c219 5991}
58769bee 5992
dc6f92b8 5993
06a2c219
GM
5994/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5995 which intersects rectangle R. R is in window-relative coordinates. */
5996
5997static void
5998expose_area (w, row, r, area)
5999 struct window *w;
6000 struct glyph_row *row;
6001 XRectangle *r;
6002 enum glyph_row_area area;
6003{
06a2c219
GM
6004 struct glyph *first = row->glyphs[area];
6005 struct glyph *end = row->glyphs[area] + row->used[area];
6006 struct glyph *last;
4bc6dcc7 6007 int first_x, start_x, x;
06a2c219 6008
6fb13182
GM
6009 if (area == TEXT_AREA && row->fill_line_p)
6010 /* If row extends face to end of line write the whole line. */
153f5ed7 6011 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
f0a48a01 6012 DRAW_NORMAL_TEXT, 0);
6fb13182
GM
6013 else
6014 {
4bc6dcc7
GM
6015 /* Set START_X to the window-relative start position for drawing glyphs of
6016 AREA. The first glyph of the text area can be partially visible.
6017 The first glyphs of other areas cannot. */
6018 if (area == LEFT_MARGIN_AREA)
6019 start_x = 0;
6020 else if (area == TEXT_AREA)
6021 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
6022 else
6023 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
6024 + window_box_width (w, TEXT_AREA));
6025 x = start_x;
6026
6fb13182
GM
6027 /* Find the first glyph that must be redrawn. */
6028 while (first < end
6029 && x + first->pixel_width < r->x)
6030 {
6031 x += first->pixel_width;
6032 ++first;
6033 }
7d0393cf 6034
6fb13182
GM
6035 /* Find the last one. */
6036 last = first;
6037 first_x = x;
6038 while (last < end
6039 && x < r->x + r->width)
6040 {
6041 x += last->pixel_width;
6042 ++last;
6043 }
7d0393cf 6044
6fb13182
GM
6045 /* Repaint. */
6046 if (last > first)
4bc6dcc7 6047 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
6048 first - row->glyphs[area],
6049 last - row->glyphs[area],
f0a48a01 6050 DRAW_NORMAL_TEXT, 0);
6fb13182 6051 }
06a2c219 6052}
7d0393cf 6053
58769bee 6054
06a2c219 6055/* Redraw the parts of the glyph row ROW on window W intersecting
82f053ab
GM
6056 rectangle R. R is in window-relative coordinates. Value is
6057 non-zero if mouse-face was overwritten. */
dc6f92b8 6058
82f053ab 6059static int
06a2c219
GM
6060expose_line (w, row, r)
6061 struct window *w;
6062 struct glyph_row *row;
6063 XRectangle *r;
6064{
6065 xassert (row->enabled_p);
7d0393cf 6066
06a2c219
GM
6067 if (row->mode_line_p || w->pseudo_window_p)
6068 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
f0a48a01 6069 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
6070 else
6071 {
6072 if (row->used[LEFT_MARGIN_AREA])
6073 expose_area (w, row, r, LEFT_MARGIN_AREA);
6074 if (row->used[TEXT_AREA])
6075 expose_area (w, row, r, TEXT_AREA);
6076 if (row->used[RIGHT_MARGIN_AREA])
6077 expose_area (w, row, r, RIGHT_MARGIN_AREA);
3f332ef3 6078 x_draw_row_fringe_bitmaps (w, row);
06a2c219 6079 }
82f053ab
GM
6080
6081 return row->mouse_face_p;
06a2c219 6082}
dc6f92b8 6083
58769bee 6084
06a2c219
GM
6085/* Return non-zero if W's cursor intersects rectangle R. */
6086
6087static int
6088x_phys_cursor_in_rect_p (w, r)
6089 struct window *w;
6090 XRectangle *r;
6091{
6092 XRectangle cr, result;
6093 struct glyph *cursor_glyph;
6094
6095 cursor_glyph = get_phys_cursor_glyph (w);
6096 if (cursor_glyph)
6097 {
6098 cr.x = w->phys_cursor.x;
6099 cr.y = w->phys_cursor.y;
6100 cr.width = cursor_glyph->pixel_width;
6101 cr.height = w->phys_cursor_height;
6102 return x_intersect_rectangles (&cr, r, &result);
6103 }
6104 else
6105 return 0;
dc6f92b8 6106}
dc6f92b8 6107
06a2c219 6108
4f53f3b8
RS
6109/* Redraw those parts of glyphs rows during expose event handling that
6110 overlap other rows. Redrawing of an exposed line writes over parts
6111 of lines overlapping that exposed line; this function fixes that.
6112
6113 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
6114 row in W's current matrix that is exposed and overlaps other rows.
6115 LAST_OVERLAPPING_ROW is the last such row. */
6116
6117static void
6118expose_overlaps (w, first_overlapping_row, last_overlapping_row)
6119 struct window *w;
6120 struct glyph_row *first_overlapping_row;
6121 struct glyph_row *last_overlapping_row;
6122{
6123 struct glyph_row *row;
7d0393cf 6124
4f53f3b8
RS
6125 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
6126 if (row->overlapping_p)
6127 {
6128 xassert (row->enabled_p && !row->mode_line_p);
7d0393cf 6129
4f53f3b8
RS
6130 if (row->used[LEFT_MARGIN_AREA])
6131 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
7d0393cf 6132
4f53f3b8
RS
6133 if (row->used[TEXT_AREA])
6134 x_fix_overlapping_area (w, row, TEXT_AREA);
7d0393cf 6135
4f53f3b8
RS
6136 if (row->used[RIGHT_MARGIN_AREA])
6137 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
6138 }
6139}
6140
6141
a02f1be0
GM
6142/* Redraw the part of window W intersection rectangle FR. Pixel
6143 coordinates in FR are frame-relative. Call this function with
82f053ab
GM
6144 input blocked. Value is non-zero if the exposure overwrites
6145 mouse-face. */
dc6f92b8 6146
a39202f6 6147static int
a02f1be0 6148expose_window (w, fr)
06a2c219 6149 struct window *w;
a02f1be0 6150 XRectangle *fr;
dc6f92b8 6151{
a02f1be0 6152 struct frame *f = XFRAME (w->frame);
a02f1be0 6153 XRectangle wr, r;
82f053ab 6154 int mouse_face_overwritten_p = 0;
dc6f92b8 6155
80c32bcc
GM
6156 /* If window is not yet fully initialized, do nothing. This can
6157 happen when toolkit scroll bars are used and a window is split.
6158 Reconfiguring the scroll bar will generate an expose for a newly
6159 created window. */
a39202f6 6160 if (w->current_matrix == NULL)
82f053ab 6161 return 0;
a39202f6
GM
6162
6163 /* When we're currently updating the window, display and current
6164 matrix usually don't agree. Arrange for a thorough display
6165 later. */
6166 if (w == updated_window)
6167 {
6168 SET_FRAME_GARBAGED (f);
6169 return 0;
6170 }
80c32bcc 6171
a39202f6 6172 /* Frame-relative pixel rectangle of W. */
a02f1be0
GM
6173 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6174 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6175 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6176 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6177
a39202f6
GM
6178 if (x_intersect_rectangles (fr, &wr, &r))
6179 {
6180 int yb = window_text_bottom_y (w);
6181 struct glyph_row *row;
6182 int cursor_cleared_p;
4f53f3b8 6183 struct glyph_row *first_overlapping_row, *last_overlapping_row;
7d0393cf 6184
a39202f6
GM
6185 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6186 r.x, r.y, r.width, r.height));
dc6f92b8 6187
a39202f6
GM
6188 /* Convert to window coordinates. */
6189 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6190 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
dc6f92b8 6191
a39202f6
GM
6192 /* Turn off the cursor. */
6193 if (!w->pseudo_window_p
6194 && x_phys_cursor_in_rect_p (w, &r))
6195 {
6196 x_clear_cursor (w);
6197 cursor_cleared_p = 1;
6198 }
6199 else
6200 cursor_cleared_p = 0;
06a2c219 6201
4f53f3b8
RS
6202 /* Update lines intersecting rectangle R. */
6203 first_overlapping_row = last_overlapping_row = NULL;
a39202f6
GM
6204 for (row = w->current_matrix->rows;
6205 row->enabled_p;
6206 ++row)
6207 {
6208 int y0 = row->y;
6209 int y1 = MATRIX_ROW_BOTTOM_Y (row);
7d0393cf 6210
a39202f6
GM
6211 if ((y0 >= r.y && y0 < r.y + r.height)
6212 || (y1 > r.y && y1 < r.y + r.height)
6213 || (r.y >= y0 && r.y < y1)
6214 || (r.y + r.height > y0 && r.y + r.height < y1))
82f053ab 6215 {
4f53f3b8
RS
6216 if (row->overlapping_p)
6217 {
6218 if (first_overlapping_row == NULL)
6219 first_overlapping_row = row;
6220 last_overlapping_row = row;
6221 }
7d0393cf 6222
82f053ab
GM
6223 if (expose_line (w, row, &r))
6224 mouse_face_overwritten_p = 1;
6225 }
7d0393cf 6226
a39202f6
GM
6227 if (y1 >= yb)
6228 break;
6229 }
dc6f92b8 6230
a39202f6
GM
6231 /* Display the mode line if there is one. */
6232 if (WINDOW_WANTS_MODELINE_P (w)
6233 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6234 row->enabled_p)
6235 && row->y < r.y + r.height)
82f053ab
GM
6236 {
6237 if (expose_line (w, row, &r))
6238 mouse_face_overwritten_p = 1;
6239 }
a39202f6
GM
6240
6241 if (!w->pseudo_window_p)
6242 {
4f53f3b8
RS
6243 /* Fix the display of overlapping rows. */
6244 if (first_overlapping_row)
6245 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
7d0393cf 6246
a39202f6
GM
6247 /* Draw border between windows. */
6248 x_draw_vertical_border (w);
7d0393cf 6249
a39202f6
GM
6250 /* Turn the cursor on again. */
6251 if (cursor_cleared_p)
6252 x_update_window_cursor (w, 1);
6253 }
06a2c219 6254 }
82f053ab
GM
6255
6256 return mouse_face_overwritten_p;
06a2c219 6257}
dc6f92b8 6258
dc6f92b8 6259
06a2c219
GM
6260/* Determine the intersection of two rectangles R1 and R2. Return
6261 the intersection in *RESULT. Value is non-zero if RESULT is not
6262 empty. */
6263
6264static int
6265x_intersect_rectangles (r1, r2, result)
6266 XRectangle *r1, *r2, *result;
6267{
6268 XRectangle *left, *right;
6269 XRectangle *upper, *lower;
6270 int intersection_p = 0;
7d0393cf 6271
06a2c219
GM
6272 /* Rearrange so that R1 is the left-most rectangle. */
6273 if (r1->x < r2->x)
6274 left = r1, right = r2;
6275 else
6276 left = r2, right = r1;
6277
6278 /* X0 of the intersection is right.x0, if this is inside R1,
6279 otherwise there is no intersection. */
6280 if (right->x <= left->x + left->width)
6281 {
6282 result->x = right->x;
7d0393cf 6283
06a2c219
GM
6284 /* The right end of the intersection is the minimum of the
6285 the right ends of left and right. */
6286 result->width = (min (left->x + left->width, right->x + right->width)
6287 - result->x);
6288
6289 /* Same game for Y. */
6290 if (r1->y < r2->y)
6291 upper = r1, lower = r2;
6292 else
6293 upper = r2, lower = r1;
6294
6295 /* The upper end of the intersection is lower.y0, if this is inside
6296 of upper. Otherwise, there is no intersection. */
6297 if (lower->y <= upper->y + upper->height)
dc43ef94 6298 {
06a2c219 6299 result->y = lower->y;
7d0393cf 6300
06a2c219
GM
6301 /* The lower end of the intersection is the minimum of the lower
6302 ends of upper and lower. */
7d0393cf 6303 result->height = (min (lower->y + lower->height,
06a2c219
GM
6304 upper->y + upper->height)
6305 - result->y);
6306 intersection_p = 1;
dc43ef94 6307 }
dc6f92b8
JB
6308 }
6309
06a2c219 6310 return intersection_p;
dc6f92b8 6311}
06a2c219
GM
6312
6313
6314
6315
dc6f92b8 6316\f
dc6f92b8 6317static void
334208b7
RS
6318frame_highlight (f)
6319 struct frame *f;
dc6f92b8 6320{
b3e1e05c
JB
6321 /* We used to only do this if Vx_no_window_manager was non-nil, but
6322 the ICCCM (section 4.1.6) says that the window's border pixmap
6323 and border pixel are window attributes which are "private to the
6324 client", so we can always change it to whatever we want. */
6325 BLOCK_INPUT;
334208b7 6326 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6327 f->output_data.x->border_pixel);
b3e1e05c 6328 UNBLOCK_INPUT;
5d46f928 6329 x_update_cursor (f, 1);
dc6f92b8
JB
6330}
6331
6332static void
334208b7
RS
6333frame_unhighlight (f)
6334 struct frame *f;
dc6f92b8 6335{
b3e1e05c
JB
6336 /* We used to only do this if Vx_no_window_manager was non-nil, but
6337 the ICCCM (section 4.1.6) says that the window's border pixmap
6338 and border pixel are window attributes which are "private to the
6339 client", so we can always change it to whatever we want. */
6340 BLOCK_INPUT;
334208b7 6341 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6342 f->output_data.x->border_tile);
b3e1e05c 6343 UNBLOCK_INPUT;
5d46f928 6344 x_update_cursor (f, 1);
dc6f92b8 6345}
dc6f92b8 6346
f676886a
JB
6347/* The focus has changed. Update the frames as necessary to reflect
6348 the new situation. Note that we can't change the selected frame
c5acd733 6349 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6350 Each event gets marked with the frame in which it occurred, so the
c5acd733 6351 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6352
6d4238f3 6353static void
0f941935
KH
6354x_new_focus_frame (dpyinfo, frame)
6355 struct x_display_info *dpyinfo;
f676886a 6356 struct frame *frame;
dc6f92b8 6357{
0f941935 6358 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6359
0f941935 6360 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6361 {
58769bee 6362 /* Set this before calling other routines, so that they see
f676886a 6363 the correct value of x_focus_frame. */
0f941935 6364 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6365
6366 if (old_focus && old_focus->auto_lower)
f676886a 6367 x_lower_frame (old_focus);
dc6f92b8
JB
6368
6369#if 0
f676886a 6370 selected_frame = frame;
e0c1aef2
KH
6371 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6372 selected_frame);
f676886a
JB
6373 Fselect_window (selected_frame->selected_window);
6374 choose_minibuf_frame ();
c118dd06 6375#endif /* ! 0 */
dc6f92b8 6376
0f941935
KH
6377 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6378 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6379 else
6380 pending_autoraise_frame = 0;
6d4238f3 6381 }
dc6f92b8 6382
0f941935 6383 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6384}
6385
cab34f50
JD
6386/* Handle FocusIn and FocusOut state changes for FRAME.
6387 If FRAME has focus and there exists more than one frame, puts
7d0393cf 6388 a FOCUS_IN_EVENT into BUFP.
cab34f50
JD
6389 Returns number of events inserted into BUFP. */
6390
6391static int
6392x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
6393 int type;
6394 int state;
6395 struct x_display_info *dpyinfo;
6396 struct frame *frame;
6397 struct input_event *bufp;
6398 int numchars;
6399{
6400 int nr_events = 0;
6401
6402 if (type == FocusIn)
6403 {
6404 if (dpyinfo->x_focus_event_frame != frame)
6405 {
6406 x_new_focus_frame (dpyinfo, frame);
6407 dpyinfo->x_focus_event_frame = frame;
7d0393cf 6408
cab34f50
JD
6409 /* Don't stop displaying the initial startup message
6410 for a switch-frame event we don't need. */
6411 if (numchars > 0
6412 && GC_NILP (Vterminal_frame)
6413 && GC_CONSP (Vframe_list)
6414 && !GC_NILP (XCDR (Vframe_list)))
6415 {
6416 bufp->kind = FOCUS_IN_EVENT;
6417 XSETFRAME (bufp->frame_or_window, frame);
6418 bufp->arg = Qnil;
6419 ++bufp;
6420 numchars--;
6421 ++nr_events;
6422 }
6423 }
6424
6425 frame->output_data.x->focus_state |= state;
6426
6427#ifdef HAVE_X_I18N
6428 if (FRAME_XIC (frame))
6429 XSetICFocus (FRAME_XIC (frame));
6430#endif
6431 }
6432 else if (type == FocusOut)
6433 {
6434 frame->output_data.x->focus_state &= ~state;
7d0393cf 6435
cab34f50
JD
6436 if (dpyinfo->x_focus_event_frame == frame)
6437 {
6438 dpyinfo->x_focus_event_frame = 0;
6439 x_new_focus_frame (dpyinfo, 0);
6440 }
6441
6442#ifdef HAVE_X_I18N
6443 if (FRAME_XIC (frame))
6444 XUnsetICFocus (FRAME_XIC (frame));
6445#endif
6446 }
6447
6448 return nr_events;
6449}
6450
6451/* The focus may have changed. Figure out if it is a real focus change,
6452 by checking both FocusIn/Out and Enter/LeaveNotify events.
6453
6454 Returns number of events inserted into BUFP. */
6455
6456static int
6457x_detect_focus_change (dpyinfo, event, bufp, numchars)
6458 struct x_display_info *dpyinfo;
6459 XEvent *event;
6460 struct input_event *bufp;
6461 int numchars;
6462{
6463 struct frame *frame;
6464 int nr_events = 0;
7d0393cf 6465
cab34f50
JD
6466 frame = x_top_window_to_frame (dpyinfo, event->xany.window);
6467 if (! frame) return nr_events;
7d0393cf 6468
cab34f50
JD
6469 switch (event->type)
6470 {
6471 case EnterNotify:
6472 case LeaveNotify:
6473 if (event->xcrossing.detail != NotifyInferior
6474 && event->xcrossing.focus
6475 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
6476 nr_events = x_focus_changed ((event->type == EnterNotify
6477 ? FocusIn : FocusOut),
6478 FOCUS_IMPLICIT,
6479 dpyinfo,
6480 frame,
6481 bufp,
6482 numchars);
6483 break;
6484
6485 case FocusIn:
6486 case FocusOut:
6487 nr_events = x_focus_changed (event->type,
6488 (event->xfocus.detail == NotifyPointer
6489 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
6490 dpyinfo,
6491 frame,
6492 bufp,
6493 numchars);
6494 break;
6495 }
6496
6497 return nr_events;
6498}
6499
6500
37c2c98b
RS
6501/* Handle an event saying the mouse has moved out of an Emacs frame. */
6502
6503void
0f941935
KH
6504x_mouse_leave (dpyinfo)
6505 struct x_display_info *dpyinfo;
37c2c98b 6506{
0f941935 6507 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6508}
6d4238f3 6509
f451eb13
JB
6510/* The focus has changed, or we have redirected a frame's focus to
6511 another frame (this happens when a frame uses a surrogate
06a2c219 6512 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6513
6514 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6515 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6516 the appropriate X display info. */
06a2c219 6517
6d4238f3 6518static void
0f941935
KH
6519XTframe_rehighlight (frame)
6520 struct frame *frame;
6d4238f3 6521{
0f941935
KH
6522 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6523}
6d4238f3 6524
0f941935
KH
6525static void
6526x_frame_rehighlight (dpyinfo)
6527 struct x_display_info *dpyinfo;
6528{
6529 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6530
6531 if (dpyinfo->x_focus_frame)
6d4238f3 6532 {
0f941935
KH
6533 dpyinfo->x_highlight_frame
6534 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6535 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6536 : dpyinfo->x_focus_frame);
6537 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6538 {
0f941935
KH
6539 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6540 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6541 }
dc6f92b8 6542 }
6d4238f3 6543 else
0f941935 6544 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6545
0f941935 6546 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6547 {
6548 if (old_highlight)
f676886a 6549 frame_unhighlight (old_highlight);
0f941935
KH
6550 if (dpyinfo->x_highlight_frame)
6551 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6552 }
dc6f92b8 6553}
06a2c219
GM
6554
6555
dc6f92b8 6556\f
06a2c219 6557/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6558
28430d3c
JB
6559/* Initialize mode_switch_bit and modifier_meaning. */
6560static void
334208b7
RS
6561x_find_modifier_meanings (dpyinfo)
6562 struct x_display_info *dpyinfo;
28430d3c 6563{
f689eb05 6564 int min_code, max_code;
28430d3c
JB
6565 KeySym *syms;
6566 int syms_per_code;
6567 XModifierKeymap *mods;
6568
334208b7
RS
6569 dpyinfo->meta_mod_mask = 0;
6570 dpyinfo->shift_lock_mask = 0;
6571 dpyinfo->alt_mod_mask = 0;
6572 dpyinfo->super_mod_mask = 0;
6573 dpyinfo->hyper_mod_mask = 0;
58769bee 6574
9658a521 6575#ifdef HAVE_X11R4
334208b7 6576 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6577#else
4a60f8c5
RS
6578 min_code = dpyinfo->display->min_keycode;
6579 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6580#endif
6581
334208b7 6582 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6583 min_code, max_code - min_code + 1,
6584 &syms_per_code);
334208b7 6585 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6586
58769bee 6587 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6588 Alt keysyms are on. */
28430d3c 6589 {
06a2c219 6590 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6591
6592 for (row = 3; row < 8; row++)
6593 for (col = 0; col < mods->max_keypermod; col++)
6594 {
0299d313
RS
6595 KeyCode code
6596 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6597
af92970c
KH
6598 /* Zeroes are used for filler. Skip them. */
6599 if (code == 0)
6600 continue;
6601
28430d3c
JB
6602 /* Are any of this keycode's keysyms a meta key? */
6603 {
6604 int code_col;
6605
6606 for (code_col = 0; code_col < syms_per_code; code_col++)
6607 {
f689eb05 6608 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6609
f689eb05 6610 switch (sym)
28430d3c 6611 {
f689eb05
JB
6612 case XK_Meta_L:
6613 case XK_Meta_R:
334208b7 6614 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6615 break;
f689eb05
JB
6616
6617 case XK_Alt_L:
6618 case XK_Alt_R:
334208b7 6619 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6620 break;
6621
6622 case XK_Hyper_L:
6623 case XK_Hyper_R:
334208b7 6624 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6625 break;
6626
6627 case XK_Super_L:
6628 case XK_Super_R:
334208b7 6629 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6630 break;
11edeb03
JB
6631
6632 case XK_Shift_Lock:
6633 /* Ignore this if it's not on the lock modifier. */
6634 if ((1 << row) == LockMask)
334208b7 6635 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6636 break;
28430d3c
JB
6637 }
6638 }
6639 }
6640 }
6641 }
6642
f689eb05 6643 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6644 if (! dpyinfo->meta_mod_mask)
a3c44b14 6645 {
334208b7
RS
6646 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6647 dpyinfo->alt_mod_mask = 0;
a3c44b14 6648 }
f689eb05 6649
148c4b70
RS
6650 /* If some keys are both alt and meta,
6651 make them just meta, not alt. */
334208b7 6652 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6653 {
334208b7 6654 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6655 }
58769bee 6656
28430d3c 6657 XFree ((char *) syms);
f689eb05 6658 XFreeModifiermap (mods);
28430d3c
JB
6659}
6660
dfeccd2d
JB
6661/* Convert between the modifier bits X uses and the modifier bits
6662 Emacs uses. */
06a2c219 6663
7c5283e4 6664static unsigned int
334208b7
RS
6665x_x_to_emacs_modifiers (dpyinfo, state)
6666 struct x_display_info *dpyinfo;
dc6f92b8
JB
6667 unsigned int state;
6668{
98659da6
KG
6669 EMACS_UINT mod_meta = meta_modifier;
6670 EMACS_UINT mod_alt = alt_modifier;
6671 EMACS_UINT mod_hyper = hyper_modifier;
6672 EMACS_UINT mod_super = super_modifier;
6673 Lisp_Object tem;
7d0393cf 6674
98659da6
KG
6675 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6676 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6677 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6678 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6679 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6680 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6681 tem = Fget (Vx_super_keysym, Qmodifier_value);
6682 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf 6683
98659da6 6684
334208b7 6685 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
6686 | ((state & ControlMask) ? ctrl_modifier : 0)
6687 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6688 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6689 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6690 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
6691}
6692
dfeccd2d 6693static unsigned int
334208b7
RS
6694x_emacs_to_x_modifiers (dpyinfo, state)
6695 struct x_display_info *dpyinfo;
dfeccd2d
JB
6696 unsigned int state;
6697{
98659da6
KG
6698 EMACS_UINT mod_meta = meta_modifier;
6699 EMACS_UINT mod_alt = alt_modifier;
6700 EMACS_UINT mod_hyper = hyper_modifier;
6701 EMACS_UINT mod_super = super_modifier;
7d0393cf 6702
98659da6 6703 Lisp_Object tem;
7d0393cf 6704
98659da6
KG
6705 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6706 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6707 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6708 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6709 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6710 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6711 tem = Fget (Vx_super_keysym, Qmodifier_value);
6712 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf
JB
6713
6714
98659da6
KG
6715 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6716 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6717 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6718 | ((state & shift_modifier) ? ShiftMask : 0)
6719 | ((state & ctrl_modifier) ? ControlMask : 0)
6720 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6721}
d047c4eb
KH
6722
6723/* Convert a keysym to its name. */
6724
6725char *
6726x_get_keysym_name (keysym)
6727 KeySym keysym;
6728{
6729 char *value;
6730
6731 BLOCK_INPUT;
6732 value = XKeysymToString (keysym);
6733 UNBLOCK_INPUT;
6734
6735 return value;
6736}
06a2c219
GM
6737
6738
e4571a43
JB
6739\f
6740/* Mouse clicks and mouse movement. Rah. */
e4571a43 6741
06a2c219
GM
6742/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6743 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6744 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6745 not force the value into range. */
69388238 6746
c8dba240 6747void
69388238 6748pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6749 FRAME_PTR f;
69388238 6750 register int pix_x, pix_y;
e4571a43
JB
6751 register int *x, *y;
6752 XRectangle *bounds;
69388238 6753 int noclip;
e4571a43 6754{
06a2c219 6755 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6756 even for negative values. */
6757 if (pix_x < 0)
7556890b 6758 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6759 if (pix_y < 0)
7556890b 6760 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6761
e4571a43
JB
6762 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6763 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6764
6765 if (bounds)
6766 {
7556890b
RS
6767 bounds->width = FONT_WIDTH (f->output_data.x->font);
6768 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6769 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6770 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6771 }
6772
69388238
RS
6773 if (!noclip)
6774 {
6775 if (pix_x < 0)
6776 pix_x = 0;
3cbd2e0b
RS
6777 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6778 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6779
6780 if (pix_y < 0)
6781 pix_y = 0;
6782 else if (pix_y > f->height)
6783 pix_y = f->height;
6784 }
e4571a43
JB
6785
6786 *x = pix_x;
6787 *y = pix_y;
6788}
6789
06a2c219
GM
6790
6791/* Given HPOS/VPOS in the current matrix of W, return corresponding
6792 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6793 can't tell the positions because W's display is not up to date,
6794 return 0. */
6795
6796int
6797glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6798 struct window *w;
6799 int hpos, vpos;
6800 int *frame_x, *frame_y;
2b5c9e71 6801{
06a2c219
GM
6802 int success_p;
6803
6804 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6805 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6806
6807 if (display_completed)
6808 {
6809 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6810 struct glyph *glyph = row->glyphs[TEXT_AREA];
6811 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6812
6813 *frame_y = row->y;
6814 *frame_x = row->x;
6815 while (glyph < end)
6816 {
6817 *frame_x += glyph->pixel_width;
6818 ++glyph;
6819 }
6820
6821 success_p = 1;
6822 }
6823 else
6824 {
6825 *frame_y = *frame_x = 0;
6826 success_p = 0;
6827 }
6828
6829 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6830 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6831 return success_p;
2b5c9e71
RS
6832}
6833
06a2c219 6834
dc6f92b8
JB
6835/* Prepare a mouse-event in *RESULT for placement in the input queue.
6836
6837 If the event is a button press, then note that we have grabbed
f451eb13 6838 the mouse. */
dc6f92b8
JB
6839
6840static Lisp_Object
f451eb13 6841construct_mouse_click (result, event, f)
dc6f92b8
JB
6842 struct input_event *result;
6843 XButtonEvent *event;
f676886a 6844 struct frame *f;
dc6f92b8 6845{
3b8f9651 6846 /* Make the event type NO_EVENT; we'll change that when we decide
dc6f92b8 6847 otherwise. */
3b8f9651 6848 result->kind = MOUSE_CLICK_EVENT;
69388238 6849 result->code = event->button - Button1;
1113d9db 6850 result->timestamp = event->time;
334208b7
RS
6851 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6852 event->state)
f689eb05 6853 | (event->type == ButtonRelease
58769bee 6854 ? up_modifier
f689eb05 6855 : down_modifier));
dc6f92b8 6856
06a2c219
GM
6857 XSETINT (result->x, event->x);
6858 XSETINT (result->y, event->y);
6859 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6860 result->arg = Qnil;
06a2c219 6861 return Qnil;
dc6f92b8 6862}
b849c413 6863
69388238 6864\f
90e65f07
JB
6865/* Function to report a mouse movement to the mainstream Emacs code.
6866 The input handler calls this.
6867
6868 We have received a mouse movement event, which is given in *event.
6869 If the mouse is over a different glyph than it was last time, tell
6870 the mainstream emacs code by setting mouse_moved. If not, ask for
6871 another motion event, so we can check again the next time it moves. */
b8009dd1 6872
06a2c219
GM
6873static XMotionEvent last_mouse_motion_event;
6874static Lisp_Object last_mouse_motion_frame;
6875
90e65f07 6876static void
12ba150f 6877note_mouse_movement (frame, event)
f676886a 6878 FRAME_PTR frame;
90e65f07 6879 XMotionEvent *event;
90e65f07 6880{
e5d77022 6881 last_mouse_movement_time = event->time;
06a2c219
GM
6882 last_mouse_motion_event = *event;
6883 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6884
27f338af
RS
6885 if (event->window != FRAME_X_WINDOW (frame))
6886 {
39d8bb4d 6887 frame->mouse_moved = 1;
27f338af 6888 last_mouse_scroll_bar = Qnil;
27f338af 6889 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6890 }
6891
90e65f07 6892 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6893 else if (event->x < last_mouse_glyph.x
6894 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6895 || event->y < last_mouse_glyph.y
6896 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6897 {
39d8bb4d 6898 frame->mouse_moved = 1;
ab648270 6899 last_mouse_scroll_bar = Qnil;
b8009dd1 6900 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6901 }
6902}
6903
06a2c219
GM
6904\f
6905/************************************************************************
6906 Mouse Face
6907 ************************************************************************/
6908
6909/* Find the glyph under window-relative coordinates X/Y in window W.
6910 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6911 strings. Return in *HPOS and *VPOS the row and column number of
6912 the glyph found. Return in *AREA the glyph area containing X.
6913 Value is a pointer to the glyph found or null if X/Y is not on
6914 text, or we can't tell because W's current matrix is not up to
6915 date. */
6916
6917static struct glyph *
f9db2310 6918x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
6919 struct window *w;
6920 int x, y;
6921 int *hpos, *vpos, *area;
f9db2310 6922 int buffer_only_p;
06a2c219
GM
6923{
6924 struct glyph *glyph, *end;
3e71d8f2 6925 struct glyph_row *row = NULL;
06a2c219
GM
6926 int x0, i, left_area_width;
6927
6928 /* Find row containing Y. Give up if some row is not enabled. */
6929 for (i = 0; i < w->current_matrix->nrows; ++i)
6930 {
6931 row = MATRIX_ROW (w->current_matrix, i);
6932 if (!row->enabled_p)
6933 return NULL;
6934 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6935 break;
6936 }
6937
6938 *vpos = i;
6939 *hpos = 0;
6940
6941 /* Give up if Y is not in the window. */
6942 if (i == w->current_matrix->nrows)
6943 return NULL;
6944
6945 /* Get the glyph area containing X. */
6946 if (w->pseudo_window_p)
6947 {
6948 *area = TEXT_AREA;
6949 x0 = 0;
6950 }
6951 else
6952 {
6953 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6954 if (x < left_area_width)
6955 {
6956 *area = LEFT_MARGIN_AREA;
6957 x0 = 0;
6958 }
6959 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6960 {
6961 *area = TEXT_AREA;
6962 x0 = row->x + left_area_width;
6963 }
6964 else
6965 {
6966 *area = RIGHT_MARGIN_AREA;
6967 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6968 }
6969 }
6970
6971 /* Find glyph containing X. */
6972 glyph = row->glyphs[*area];
6973 end = glyph + row->used[*area];
6974 while (glyph < end)
6975 {
6976 if (x < x0 + glyph->pixel_width)
6977 {
6978 if (w->pseudo_window_p)
6979 break;
f9db2310 6980 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
6981 break;
6982 }
7d0393cf 6983
06a2c219
GM
6984 x0 += glyph->pixel_width;
6985 ++glyph;
6986 }
6987
6988 if (glyph == end)
6989 return NULL;
6990
6991 *hpos = glyph - row->glyphs[*area];
6992 return glyph;
6993}
6994
6995
6996/* Convert frame-relative x/y to coordinates relative to window W.
6997 Takes pseudo-windows into account. */
6998
6999static void
7000frame_to_window_pixel_xy (w, x, y)
7001 struct window *w;
7002 int *x, *y;
7003{
7004 if (w->pseudo_window_p)
7005 {
7006 /* A pseudo-window is always full-width, and starts at the
7007 left edge of the frame, plus a frame border. */
7008 struct frame *f = XFRAME (w->frame);
7009 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
7010 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
7011 }
7012 else
7013 {
7014 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
7015 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
7016 }
7017}
7018
7019
b436feb6
GM
7020/* Take proper action when mouse has moved to the mode or header line
7021 or marginal area of window W, x-position X and y-position Y. Area
7022 is 1, 3, 6 or 7 for the mode line, header line, left and right
7023 marginal area respectively. X is relative to the start of the text
7024 display area of W, so the width of bitmap areas and scroll bars
7025 must be subtracted to get a position relative to the start of the
7026 mode line. */
7d0393cf 7027
06a2c219 7028static void
b436feb6 7029note_mode_line_or_margin_highlight (w, x, y, portion)
06a2c219 7030 struct window *w;
b436feb6 7031 int x, y, portion;
06a2c219
GM
7032{
7033 struct frame *f = XFRAME (w->frame);
7034 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7035 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
2a793c0c 7036 int charpos;
b436feb6 7037 Lisp_Object string, help, map, pos;
06a2c219 7038
b436feb6
GM
7039 if (portion == 1 || portion == 3)
7040 string = mode_line_string (w, x, y, portion == 1, &charpos);
7041 else
7042 string = marginal_area_string (w, x, y, portion, &charpos);
e371a781 7043
b436feb6 7044 if (STRINGP (string))
06a2c219 7045 {
b436feb6 7046 pos = make_number (charpos);
7d0393cf 7047
b436feb6
GM
7048 /* If we're on a string with `help-echo' text property, arrange
7049 for the help to be displayed. This is done by setting the
7050 global variable help_echo to the help string. */
7051 help = Fget_text_property (pos, Qhelp_echo, string);
7052 if (!NILP (help))
06a2c219 7053 {
b436feb6
GM
7054 help_echo = help;
7055 XSETWINDOW (help_echo_window, w);
7056 help_echo_object = string;
7057 help_echo_pos = charpos;
06a2c219
GM
7058 }
7059
b436feb6
GM
7060 /* Change the mouse pointer according to what is under X/Y. */
7061 map = Fget_text_property (pos, Qlocal_map, string);
7062 if (!KEYMAPP (map))
7063 map = Fget_text_property (pos, Qkeymap, string);
7064 if (KEYMAPP (map))
7065 cursor = f->output_data.x->nontext_cursor;
06a2c219 7066 }
7d0393cf 7067
06a2c219
GM
7068 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7069}
7070
7071
7072/* Take proper action when the mouse has moved to position X, Y on
7073 frame F as regards highlighting characters that have mouse-face
7074 properties. Also de-highlighting chars where the mouse was before.
27f338af 7075 X and Y can be negative or out of range. */
b8009dd1
RS
7076
7077static void
7078note_mouse_highlight (f, x, y)
06a2c219 7079 struct frame *f;
c32cdd9a 7080 int x, y;
b8009dd1 7081{
06a2c219
GM
7082 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7083 int portion;
b8009dd1
RS
7084 Lisp_Object window;
7085 struct window *w;
0d487c52
GM
7086 Cursor cursor = None;
7087 struct buffer *b;
b8009dd1 7088
06a2c219 7089 /* When a menu is active, don't highlight because this looks odd. */
488dd4c4 7090#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
06a2c219
GM
7091 if (popup_activated ())
7092 return;
7093#endif
7094
66301061 7095 if (NILP (Vmouse_highlight)
04fff9c0 7096 || !f->glyphs_initialized_p)
bf1c0ba1
RS
7097 return;
7098
06a2c219
GM
7099 dpyinfo->mouse_face_mouse_x = x;
7100 dpyinfo->mouse_face_mouse_y = y;
7101 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 7102
06a2c219 7103 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
7104 return;
7105
514e4681
RS
7106 if (gc_in_progress)
7107 {
06a2c219 7108 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
7109 return;
7110 }
7111
b8009dd1 7112 /* Which window is that in? */
06a2c219 7113 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
7114
7115 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
7116 if (! EQ (window, dpyinfo->mouse_face_window))
7117 clear_mouse_face (dpyinfo);
7118
7119 /* Not on a window -> return. */
7120 if (!WINDOWP (window))
7121 return;
7122
7123 /* Convert to window-relative pixel coordinates. */
7124 w = XWINDOW (window);
7125 frame_to_window_pixel_xy (w, &x, &y);
7126
9ea173e8 7127 /* Handle tool-bar window differently since it doesn't display a
06a2c219 7128 buffer. */
9ea173e8 7129 if (EQ (window, f->tool_bar_window))
06a2c219 7130 {
9ea173e8 7131 note_tool_bar_highlight (f, x, y);
06a2c219
GM
7132 return;
7133 }
7134
b436feb6
GM
7135 /* Mouse is on the mode, header line or margin? */
7136 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
06a2c219 7137 {
b436feb6 7138 note_mode_line_or_margin_highlight (w, x, y, portion);
06a2c219
GM
7139 return;
7140 }
7d0393cf 7141
0d487c52
GM
7142 if (portion == 2)
7143 cursor = f->output_data.x->horizontal_drag_cursor;
06a2c219 7144 else
0d487c52 7145 cursor = f->output_data.x->text_cursor;
b8009dd1 7146
0cdd0c9f
RS
7147 /* Are we in a window whose display is up to date?
7148 And verify the buffer's text has not changed. */
0d487c52 7149 b = XBUFFER (w->buffer);
06a2c219
GM
7150 if (/* Within text portion of the window. */
7151 portion == 0
0cdd0c9f 7152 && EQ (w->window_end_valid, w->buffer)
0d487c52
GM
7153 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
7154 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
b8009dd1 7155 {
06a2c219
GM
7156 int hpos, vpos, pos, i, area;
7157 struct glyph *glyph;
f9db2310 7158 Lisp_Object object;
0d487c52
GM
7159 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
7160 Lisp_Object *overlay_vec = NULL;
7161 int len, noverlays;
7162 struct buffer *obuf;
7163 int obegv, ozv, same_region;
b8009dd1 7164
06a2c219 7165 /* Find the glyph under X/Y. */
f9db2310 7166 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
7167
7168 /* Clear mouse face if X/Y not over text. */
7169 if (glyph == NULL
7170 || area != TEXT_AREA
7171 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 7172 {
fa262c07
GM
7173 if (clear_mouse_face (dpyinfo))
7174 cursor = None;
7175 goto set_cursor;
06a2c219
GM
7176 }
7177
7178 pos = glyph->charpos;
f9db2310
GM
7179 object = glyph->object;
7180 if (!STRINGP (object) && !BUFFERP (object))
fa262c07 7181 goto set_cursor;
06a2c219 7182
0d487c52
GM
7183 /* If we get an out-of-range value, return now; avoid an error. */
7184 if (BUFFERP (object) && pos > BUF_Z (b))
fa262c07 7185 goto set_cursor;
06a2c219 7186
0d487c52
GM
7187 /* Make the window's buffer temporarily current for
7188 overlays_at and compute_char_face. */
7189 obuf = current_buffer;
7190 current_buffer = b;
7191 obegv = BEGV;
7192 ozv = ZV;
7193 BEGV = BEG;
7194 ZV = Z;
06a2c219 7195
0d487c52
GM
7196 /* Is this char mouse-active or does it have help-echo? */
7197 position = make_number (pos);
f9db2310 7198
0d487c52
GM
7199 if (BUFFERP (object))
7200 {
7201 /* Put all the overlays we want in a vector in overlay_vec.
7202 Store the length in len. If there are more than 10, make
7203 enough space for all, and try again. */
7204 len = 10;
7205 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7206 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7207 if (noverlays > len)
7208 {
7209 len = noverlays;
7210 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7211 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7212 }
f8349001 7213
0d487c52
GM
7214 /* Sort overlays into increasing priority order. */
7215 noverlays = sort_overlays (overlay_vec, noverlays, w);
7216 }
7217 else
7218 noverlays = 0;
7219
7220 same_region = (EQ (window, dpyinfo->mouse_face_window)
7221 && vpos >= dpyinfo->mouse_face_beg_row
7222 && vpos <= dpyinfo->mouse_face_end_row
7223 && (vpos > dpyinfo->mouse_face_beg_row
7224 || hpos >= dpyinfo->mouse_face_beg_col)
7225 && (vpos < dpyinfo->mouse_face_end_row
7226 || hpos < dpyinfo->mouse_face_end_col
7227 || dpyinfo->mouse_face_past_end));
7228
7229 if (same_region)
7230 cursor = None;
7231
7232 /* Check mouse-face highlighting. */
7233 if (! same_region
7234 /* If there exists an overlay with mouse-face overlapping
7235 the one we are currently highlighting, we have to
7236 check if we enter the overlapping overlay, and then
7237 highlight only that. */
7238 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7239 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7240 {
0d487c52
GM
7241 /* Find the highest priority overlay that has a mouse-face
7242 property. */
7243 overlay = Qnil;
7244 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7245 {
7246 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7247 if (!NILP (mouse_face))
7248 overlay = overlay_vec[i];
7249 }
7d0393cf 7250
8bd189fb
GM
7251 /* If we're actually highlighting the same overlay as
7252 before, there's no need to do that again. */
7253 if (!NILP (overlay)
7254 && EQ (overlay, dpyinfo->mouse_face_overlay))
7255 goto check_help_echo;
7d0393cf 7256
8bd189fb
GM
7257 dpyinfo->mouse_face_overlay = overlay;
7258
7259 /* Clear the display of the old active region, if any. */
7260 if (clear_mouse_face (dpyinfo))
7261 cursor = None;
7262
0d487c52
GM
7263 /* If no overlay applies, get a text property. */
7264 if (NILP (overlay))
7265 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219 7266
0d487c52
GM
7267 /* Handle the overlay case. */
7268 if (!NILP (overlay))
7269 {
7270 /* Find the range of text around this char that
7271 should be active. */
7272 Lisp_Object before, after;
7273 int ignore;
7274
7275 before = Foverlay_start (overlay);
7276 after = Foverlay_end (overlay);
7277 /* Record this as the current active region. */
7278 fast_find_position (w, XFASTINT (before),
7279 &dpyinfo->mouse_face_beg_col,
7280 &dpyinfo->mouse_face_beg_row,
7281 &dpyinfo->mouse_face_beg_x,
7e376260 7282 &dpyinfo->mouse_face_beg_y, Qnil);
7d0393cf 7283
0d487c52
GM
7284 dpyinfo->mouse_face_past_end
7285 = !fast_find_position (w, XFASTINT (after),
7286 &dpyinfo->mouse_face_end_col,
7287 &dpyinfo->mouse_face_end_row,
7288 &dpyinfo->mouse_face_end_x,
7e376260 7289 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7290 dpyinfo->mouse_face_window = window;
7291 dpyinfo->mouse_face_face_id
7292 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
7293 &ignore, pos + 1,
7294 !dpyinfo->mouse_face_hidden);
0d487c52
GM
7295
7296 /* Display it as active. */
7297 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7298 cursor = None;
0d487c52
GM
7299 }
7300 /* Handle the text property case. */
7301 else if (!NILP (mouse_face) && BUFFERP (object))
7302 {
7303 /* Find the range of text around this char that
7304 should be active. */
7305 Lisp_Object before, after, beginning, end;
7306 int ignore;
7307
7308 beginning = Fmarker_position (w->start);
7309 end = make_number (BUF_Z (XBUFFER (object))
7310 - XFASTINT (w->window_end_pos));
7311 before
7312 = Fprevious_single_property_change (make_number (pos + 1),
7313 Qmouse_face,
7314 object, beginning);
7315 after
7316 = Fnext_single_property_change (position, Qmouse_face,
7317 object, end);
7d0393cf 7318
0d487c52
GM
7319 /* Record this as the current active region. */
7320 fast_find_position (w, XFASTINT (before),
7321 &dpyinfo->mouse_face_beg_col,
7322 &dpyinfo->mouse_face_beg_row,
7323 &dpyinfo->mouse_face_beg_x,
7e376260 7324 &dpyinfo->mouse_face_beg_y, Qnil);
0d487c52
GM
7325 dpyinfo->mouse_face_past_end
7326 = !fast_find_position (w, XFASTINT (after),
7327 &dpyinfo->mouse_face_end_col,
7328 &dpyinfo->mouse_face_end_row,
7329 &dpyinfo->mouse_face_end_x,
7e376260 7330 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7331 dpyinfo->mouse_face_window = window;
7332
7333 if (BUFFERP (object))
06a2c219
GM
7334 dpyinfo->mouse_face_face_id
7335 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
7336 &ignore, pos + 1,
7337 !dpyinfo->mouse_face_hidden);
06a2c219 7338
0d487c52
GM
7339 /* Display it as active. */
7340 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7341 cursor = None;
0d487c52
GM
7342 }
7343 else if (!NILP (mouse_face) && STRINGP (object))
7344 {
7345 Lisp_Object b, e;
7346 int ignore;
7d0393cf 7347
0d487c52
GM
7348 b = Fprevious_single_property_change (make_number (pos + 1),
7349 Qmouse_face,
7350 object, Qnil);
7351 e = Fnext_single_property_change (position, Qmouse_face,
7352 object, Qnil);
7353 if (NILP (b))
7354 b = make_number (0);
7355 if (NILP (e))
d5db4077 7356 e = make_number (SCHARS (object) - 1);
0d487c52 7357 fast_find_string_pos (w, XINT (b), object,
06a2c219
GM
7358 &dpyinfo->mouse_face_beg_col,
7359 &dpyinfo->mouse_face_beg_row,
7360 &dpyinfo->mouse_face_beg_x,
0d487c52
GM
7361 &dpyinfo->mouse_face_beg_y, 0);
7362 fast_find_string_pos (w, XINT (e), object,
7363 &dpyinfo->mouse_face_end_col,
7364 &dpyinfo->mouse_face_end_row,
7365 &dpyinfo->mouse_face_end_x,
7366 &dpyinfo->mouse_face_end_y, 1);
7367 dpyinfo->mouse_face_past_end = 0;
7368 dpyinfo->mouse_face_window = window;
7369 dpyinfo->mouse_face_face_id
7370 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7371 glyph->face_id, 1);
7372 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7373 cursor = None;
0d487c52 7374 }
7e376260
GM
7375 else if (STRINGP (object) && NILP (mouse_face))
7376 {
7377 /* A string which doesn't have mouse-face, but
7378 the text ``under'' it might have. */
7379 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7380 int start = MATRIX_ROW_START_CHARPOS (r);
7d0393cf 7381
7e376260
GM
7382 pos = string_buffer_position (w, object, start);
7383 if (pos > 0)
7384 mouse_face = get_char_property_and_overlay (make_number (pos),
7385 Qmouse_face,
7386 w->buffer,
7387 &overlay);
7388 if (!NILP (mouse_face) && !NILP (overlay))
7389 {
7390 Lisp_Object before = Foverlay_start (overlay);
7391 Lisp_Object after = Foverlay_end (overlay);
91c153e2 7392 int ignore;
7e376260
GM
7393
7394 /* Note that we might not be able to find position
7395 BEFORE in the glyph matrix if the overlay is
7396 entirely covered by a `display' property. In
7397 this case, we overshoot. So let's stop in
7398 the glyph matrix before glyphs for OBJECT. */
7399 fast_find_position (w, XFASTINT (before),
7400 &dpyinfo->mouse_face_beg_col,
7401 &dpyinfo->mouse_face_beg_row,
7402 &dpyinfo->mouse_face_beg_x,
7403 &dpyinfo->mouse_face_beg_y,
7404 object);
7d0393cf 7405
7e376260
GM
7406 dpyinfo->mouse_face_past_end
7407 = !fast_find_position (w, XFASTINT (after),
7408 &dpyinfo->mouse_face_end_col,
7409 &dpyinfo->mouse_face_end_row,
7410 &dpyinfo->mouse_face_end_x,
7411 &dpyinfo->mouse_face_end_y,
7412 Qnil);
7413 dpyinfo->mouse_face_window = window;
7414 dpyinfo->mouse_face_face_id
7415 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
7416 &ignore, pos + 1,
7417 !dpyinfo->mouse_face_hidden);
7e376260
GM
7418
7419 /* Display it as active. */
7420 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7421 cursor = None;
7422 }
7423 }
0d487c52 7424 }
06a2c219 7425
8bd189fb
GM
7426 check_help_echo:
7427
0d487c52
GM
7428 /* Look for a `help-echo' property. */
7429 {
7430 Lisp_Object help, overlay;
06a2c219 7431
0d487c52
GM
7432 /* Check overlays first. */
7433 help = overlay = Qnil;
7434 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7435 {
7436 overlay = overlay_vec[i];
7437 help = Foverlay_get (overlay, Qhelp_echo);
7438 }
be010514 7439
0d487c52
GM
7440 if (!NILP (help))
7441 {
7442 help_echo = help;
7443 help_echo_window = window;
7444 help_echo_object = overlay;
7445 help_echo_pos = pos;
7446 }
7447 else
7448 {
7449 Lisp_Object object = glyph->object;
7450 int charpos = glyph->charpos;
7d0393cf 7451
0d487c52
GM
7452 /* Try text properties. */
7453 if (STRINGP (object)
7454 && charpos >= 0
d5db4077 7455 && charpos < SCHARS (object))
0d487c52
GM
7456 {
7457 help = Fget_text_property (make_number (charpos),
7458 Qhelp_echo, object);
7459 if (NILP (help))
7460 {
7461 /* If the string itself doesn't specify a help-echo,
7462 see if the buffer text ``under'' it does. */
7463 struct glyph_row *r
7464 = MATRIX_ROW (w->current_matrix, vpos);
7465 int start = MATRIX_ROW_START_CHARPOS (r);
7466 int pos = string_buffer_position (w, object, start);
7467 if (pos > 0)
7468 {
7e376260 7469 help = Fget_char_property (make_number (pos),
0d487c52
GM
7470 Qhelp_echo, w->buffer);
7471 if (!NILP (help))
7472 {
7473 charpos = pos;
7474 object = w->buffer;
7475 }
7476 }
7477 }
7478 }
7479 else if (BUFFERP (object)
7480 && charpos >= BEGV
7481 && charpos < ZV)
7482 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7483 object);
7d0393cf 7484
0d487c52
GM
7485 if (!NILP (help))
7486 {
7487 help_echo = help;
7488 help_echo_window = window;
7489 help_echo_object = object;
7490 help_echo_pos = charpos;
7491 }
7492 }
06a2c219 7493 }
7d0393cf 7494
0d487c52
GM
7495 BEGV = obegv;
7496 ZV = ozv;
7497 current_buffer = obuf;
06a2c219 7498 }
0d487c52 7499
fa262c07 7500 set_cursor:
7d0393cf 7501
0d487c52
GM
7502 if (cursor != None)
7503 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219
GM
7504}
7505
7506static void
7507redo_mouse_highlight ()
7508{
7509 if (!NILP (last_mouse_motion_frame)
7510 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7511 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7512 last_mouse_motion_event.x,
7513 last_mouse_motion_event.y);
7514}
7515
7516
7517\f
7518/***********************************************************************
9ea173e8 7519 Tool-bars
06a2c219
GM
7520 ***********************************************************************/
7521
9ea173e8
GM
7522static int x_tool_bar_item P_ ((struct frame *, int, int,
7523 struct glyph **, int *, int *, int *));
06a2c219 7524
9ea173e8 7525/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
7526 or -1. */
7527
9ea173e8 7528static int last_tool_bar_item;
06a2c219
GM
7529
7530
9ea173e8
GM
7531/* Get information about the tool-bar item at position X/Y on frame F.
7532 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7533 the current matrix of the tool-bar window of F, or NULL if not
7534 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 7535 item in F->tool_bar_items. Value is
06a2c219 7536
9ea173e8 7537 -1 if X/Y is not on a tool-bar item
06a2c219
GM
7538 0 if X/Y is on the same item that was highlighted before.
7539 1 otherwise. */
7540
7541static int
9ea173e8 7542x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
7543 struct frame *f;
7544 int x, y;
7545 struct glyph **glyph;
7546 int *hpos, *vpos, *prop_idx;
7547{
7548 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7549 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7550 int area;
7551
7552 /* Find the glyph under X/Y. */
f9db2310 7553 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
7554 if (*glyph == NULL)
7555 return -1;
7556
9ea173e8 7557 /* Get the start of this tool-bar item's properties in
8daf1204 7558 f->tool_bar_items. */
9ea173e8 7559 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
7560 return -1;
7561
7562 /* Is mouse on the highlighted item? */
9ea173e8 7563 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
7564 && *vpos >= dpyinfo->mouse_face_beg_row
7565 && *vpos <= dpyinfo->mouse_face_end_row
7566 && (*vpos > dpyinfo->mouse_face_beg_row
7567 || *hpos >= dpyinfo->mouse_face_beg_col)
7568 && (*vpos < dpyinfo->mouse_face_end_row
7569 || *hpos < dpyinfo->mouse_face_end_col
7570 || dpyinfo->mouse_face_past_end))
7571 return 0;
7d0393cf 7572
06a2c219
GM
7573 return 1;
7574}
7575
7576
9ea173e8 7577/* Handle mouse button event on the tool-bar of frame F, at
6c4a22e6 7578 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
06a2c219
GM
7579 or ButtonRelase. */
7580
7581static void
9ea173e8 7582x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7583 struct frame *f;
7584 XButtonEvent *button_event;
7585{
7586 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7587 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7588 int hpos, vpos, prop_idx;
7589 struct glyph *glyph;
7590 Lisp_Object enabled_p;
7591 int x = button_event->x;
7592 int y = button_event->y;
7d0393cf 7593
9ea173e8 7594 /* If not on the highlighted tool-bar item, return. */
06a2c219 7595 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7596 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7597 return;
7598
7599 /* If item is disabled, do nothing. */
8daf1204 7600 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7601 if (NILP (enabled_p))
7602 return;
7d0393cf 7603
06a2c219
GM
7604 if (button_event->type == ButtonPress)
7605 {
7606 /* Show item in pressed state. */
7607 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7608 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7609 last_tool_bar_item = prop_idx;
06a2c219
GM
7610 }
7611 else
7612 {
7613 Lisp_Object key, frame;
7614 struct input_event event;
7615
7616 /* Show item in released state. */
7617 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7618 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7619
8daf1204 7620 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7621
7622 XSETFRAME (frame, f);
9ea173e8 7623 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7624 event.frame_or_window = frame;
7625 event.arg = frame;
06a2c219
GM
7626 kbd_buffer_store_event (&event);
7627
9ea173e8 7628 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7629 event.frame_or_window = frame;
7630 event.arg = key;
06a2c219
GM
7631 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7632 button_event->state);
7633 kbd_buffer_store_event (&event);
9ea173e8 7634 last_tool_bar_item = -1;
06a2c219
GM
7635 }
7636}
7637
7638
9ea173e8
GM
7639/* Possibly highlight a tool-bar item on frame F when mouse moves to
7640 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7641 note_mouse_highlight. */
7642
7643static void
9ea173e8 7644note_tool_bar_highlight (f, x, y)
06a2c219
GM
7645 struct frame *f;
7646 int x, y;
7647{
9ea173e8 7648 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7649 struct window *w = XWINDOW (window);
7650 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7651 int hpos, vpos;
7652 struct glyph *glyph;
7653 struct glyph_row *row;
5c187dee 7654 int i;
06a2c219
GM
7655 Lisp_Object enabled_p;
7656 int prop_idx;
140330de 7657 enum draw_glyphs_face draw;
5c187dee 7658 int mouse_down_p, rc;
06a2c219
GM
7659
7660 /* Function note_mouse_highlight is called with negative x(y
7661 values when mouse moves outside of the frame. */
7662 if (x <= 0 || y <= 0)
7663 {
7664 clear_mouse_face (dpyinfo);
7665 return;
7666 }
7667
9ea173e8 7668 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7669 if (rc < 0)
7670 {
9ea173e8 7671 /* Not on tool-bar item. */
06a2c219
GM
7672 clear_mouse_face (dpyinfo);
7673 return;
7674 }
7675 else if (rc == 0)
06a2c219 7676 goto set_help_echo;
b8009dd1 7677
06a2c219 7678 clear_mouse_face (dpyinfo);
7d0393cf 7679
9ea173e8 7680 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7681 mouse_down_p = (dpyinfo->grabbed
7682 && f == last_mouse_frame
7683 && FRAME_LIVE_P (f));
7684 if (mouse_down_p
9ea173e8 7685 && last_tool_bar_item != prop_idx)
06a2c219
GM
7686 return;
7687
7688 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7689 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7d0393cf 7690
9ea173e8 7691 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7692 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7693 if (!NILP (enabled_p))
7694 {
7695 /* Compute the x-position of the glyph. In front and past the
7696 image is a space. We include this is the highlighted area. */
7697 row = MATRIX_ROW (w->current_matrix, vpos);
7698 for (i = x = 0; i < hpos; ++i)
7699 x += row->glyphs[TEXT_AREA][i].pixel_width;
7d0393cf 7700
06a2c219
GM
7701 /* Record this as the current active region. */
7702 dpyinfo->mouse_face_beg_col = hpos;
7703 dpyinfo->mouse_face_beg_row = vpos;
7704 dpyinfo->mouse_face_beg_x = x;
7705 dpyinfo->mouse_face_beg_y = row->y;
7706 dpyinfo->mouse_face_past_end = 0;
7d0393cf 7707
06a2c219
GM
7708 dpyinfo->mouse_face_end_col = hpos + 1;
7709 dpyinfo->mouse_face_end_row = vpos;
7710 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7711 dpyinfo->mouse_face_end_y = row->y;
7712 dpyinfo->mouse_face_window = window;
9ea173e8 7713 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7d0393cf 7714
06a2c219
GM
7715 /* Display it as active. */
7716 show_mouse_face (dpyinfo, draw);
7717 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7718 }
7d0393cf 7719
06a2c219 7720 set_help_echo:
7d0393cf 7721
50590060 7722 /* Set help_echo to a help string to display for this tool-bar item.
06a2c219 7723 XTread_socket does the rest. */
7cea38bc 7724 help_echo_object = help_echo_window = Qnil;
be010514 7725 help_echo_pos = -1;
8daf1204 7726 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7727 if (NILP (help_echo))
8daf1204 7728 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7729}
4d73d038 7730
06a2c219
GM
7731
7732\f
9f8531e5
GM
7733/* Find the glyph matrix position of buffer position CHARPOS in window
7734 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7735 current glyphs must be up to date. If CHARPOS is above window
7736 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7737 of last line in W. In the row containing CHARPOS, stop before glyphs
7e376260 7738 having STOP as object. */
b8009dd1 7739
9f8531e5
GM
7740#if 0 /* This is a version of fast_find_position that's more correct
7741 in the presence of hscrolling, for example. I didn't install
7742 it right away because the problem fixed is minor, it failed
7d0393cf 7743 in 20.x as well, and I think it's too risky to install
9f8531e5
GM
7744 so near the release of 21.1. 2001-09-25 gerd. */
7745
7746static int
7747fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7748 struct window *w;
7749 int charpos;
7750 int *hpos, *vpos, *x, *y;
7751 Lisp_Object stop;
7752{
7753 struct glyph_row *row, *first;
7754 struct glyph *glyph, *end;
7755 int i, past_end = 0;
7756
7757 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 7758 row = row_containing_pos (w, charpos, first, NULL, 0);
9f8531e5
GM
7759 if (row == NULL)
7760 {
7761 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7762 {
7763 *x = *y = *hpos = *vpos = 0;
7764 return 0;
7765 }
7766 else
7767 {
7768 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7769 past_end = 1;
7770 }
7771 }
7772
7773 *x = row->x;
7774 *y = row->y;
7775 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7d0393cf 7776
9f8531e5
GM
7777 glyph = row->glyphs[TEXT_AREA];
7778 end = glyph + row->used[TEXT_AREA];
7d0393cf 7779
9f8531e5
GM
7780 /* Skip over glyphs not having an object at the start of the row.
7781 These are special glyphs like truncation marks on terminal
7782 frames. */
7783 if (row->displays_text_p)
7784 while (glyph < end
7785 && INTEGERP (glyph->object)
7786 && !EQ (stop, glyph->object)
7787 && glyph->charpos < 0)
7788 {
7789 *x += glyph->pixel_width;
7790 ++glyph;
7791 }
7792
7793 while (glyph < end
7794 && !INTEGERP (glyph->object)
7795 && !EQ (stop, glyph->object)
7796 && (!BUFFERP (glyph->object)
7797 || glyph->charpos < charpos))
7798 {
7799 *x += glyph->pixel_width;
7800 ++glyph;
7801 }
7802
7803 *hpos = glyph - row->glyphs[TEXT_AREA];
7804 return past_end;
7805}
7806
7807#else /* not 0 */
7808
b8009dd1 7809static int
7e376260 7810fast_find_position (w, pos, hpos, vpos, x, y, stop)
06a2c219 7811 struct window *w;
b8009dd1 7812 int pos;
06a2c219 7813 int *hpos, *vpos, *x, *y;
7e376260 7814 Lisp_Object stop;
b8009dd1 7815{
b8009dd1 7816 int i;
bf1c0ba1 7817 int lastcol;
06a2c219
GM
7818 int maybe_next_line_p = 0;
7819 int line_start_position;
7820 int yb = window_text_bottom_y (w);
03d1a189
GM
7821 struct glyph_row *row, *best_row;
7822 int row_vpos, best_row_vpos;
06a2c219
GM
7823 int current_x;
7824
03d1a189
GM
7825 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7826 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7827
06a2c219 7828 while (row->y < yb)
b8009dd1 7829 {
06a2c219
GM
7830 if (row->used[TEXT_AREA])
7831 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7832 else
7833 line_start_position = 0;
7834
7835 if (line_start_position > pos)
b8009dd1 7836 break;
77b68646
RS
7837 /* If the position sought is the end of the buffer,
7838 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7839 else if (line_start_position == pos
7840 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7841 {
06a2c219 7842 maybe_next_line_p = 1;
77b68646
RS
7843 break;
7844 }
06a2c219
GM
7845 else if (line_start_position > 0)
7846 {
7847 best_row = row;
7848 best_row_vpos = row_vpos;
7849 }
4b0bb6f3
GM
7850
7851 if (row->y + row->height >= yb)
7852 break;
7d0393cf 7853
06a2c219
GM
7854 ++row;
7855 ++row_vpos;
b8009dd1 7856 }
7d0393cf 7857
06a2c219
GM
7858 /* Find the right column within BEST_ROW. */
7859 lastcol = 0;
7860 current_x = best_row->x;
7861 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7862 {
06a2c219 7863 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7e376260 7864 int charpos = glyph->charpos;
06a2c219 7865
7e376260 7866 if (BUFFERP (glyph->object))
bf1c0ba1 7867 {
7e376260
GM
7868 if (charpos == pos)
7869 {
7870 *hpos = i;
7871 *vpos = best_row_vpos;
7872 *x = current_x;
7873 *y = best_row->y;
7874 return 1;
7875 }
7876 else if (charpos > pos)
7877 break;
bf1c0ba1 7878 }
7e376260 7879 else if (EQ (glyph->object, stop))
4d73d038 7880 break;
06a2c219 7881
7e376260
GM
7882 if (charpos > 0)
7883 lastcol = i;
06a2c219 7884 current_x += glyph->pixel_width;
bf1c0ba1 7885 }
b8009dd1 7886
77b68646
RS
7887 /* If we're looking for the end of the buffer,
7888 and we didn't find it in the line we scanned,
7889 use the start of the following line. */
06a2c219 7890 if (maybe_next_line_p)
77b68646 7891 {
06a2c219
GM
7892 ++best_row;
7893 ++best_row_vpos;
7894 lastcol = 0;
7895 current_x = best_row->x;
77b68646
RS
7896 }
7897
06a2c219
GM
7898 *vpos = best_row_vpos;
7899 *hpos = lastcol + 1;
7900 *x = current_x;
7901 *y = best_row->y;
b8009dd1
RS
7902 return 0;
7903}
7904
9f8531e5
GM
7905#endif /* not 0 */
7906
06a2c219 7907
269b7745 7908/* Find the position of the glyph for position POS in OBJECT in
7a4bce14
PJ
7909 window W's current matrix, and return in *X, *Y the pixel
7910 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
f9db2310
GM
7911
7912 RIGHT_P non-zero means return the position of the right edge of the
7913 glyph, RIGHT_P zero means return the left edge position.
7914
7915 If no glyph for POS exists in the matrix, return the position of
7916 the glyph with the next smaller position that is in the matrix, if
7917 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7918 exists in the matrix, return the position of the glyph with the
7919 next larger position in OBJECT.
7920
7921 Value is non-zero if a glyph was found. */
7922
7923static int
7924fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7925 struct window *w;
7926 int pos;
7927 Lisp_Object object;
7928 int *hpos, *vpos, *x, *y;
7929 int right_p;
7930{
7931 int yb = window_text_bottom_y (w);
7932 struct glyph_row *r;
7933 struct glyph *best_glyph = NULL;
7934 struct glyph_row *best_row = NULL;
7935 int best_x = 0;
7936
7937 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7938 r->enabled_p && r->y < yb;
7939 ++r)
7940 {
7941 struct glyph *g = r->glyphs[TEXT_AREA];
7942 struct glyph *e = g + r->used[TEXT_AREA];
7943 int gx;
7944
7945 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7946 if (EQ (g->object, object))
7947 {
7948 if (g->charpos == pos)
7949 {
7950 best_glyph = g;
7951 best_x = gx;
7952 best_row = r;
7953 goto found;
7954 }
7955 else if (best_glyph == NULL
7956 || ((abs (g->charpos - pos)
7957 < abs (best_glyph->charpos - pos))
7958 && (right_p
7959 ? g->charpos < pos
7960 : g->charpos > pos)))
7961 {
7962 best_glyph = g;
7963 best_x = gx;
7964 best_row = r;
7965 }
7966 }
7967 }
7968
7969 found:
7970
7971 if (best_glyph)
7972 {
7973 *x = best_x;
7974 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7975
7976 if (right_p)
7977 {
7978 *x += best_glyph->pixel_width;
7979 ++*hpos;
7980 }
7d0393cf 7981
f9db2310
GM
7982 *y = best_row->y;
7983 *vpos = best_row - w->current_matrix->rows;
7984 }
7985
7986 return best_glyph != NULL;
7987}
7988
7989
b8009dd1
RS
7990/* Display the active region described by mouse_face_*
7991 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7992
7993static void
06a2c219 7994show_mouse_face (dpyinfo, draw)
7a13e894 7995 struct x_display_info *dpyinfo;
06a2c219 7996 enum draw_glyphs_face draw;
b8009dd1 7997{
7a13e894 7998 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7999 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 8000
b2bbd509
GM
8001 if (/* If window is in the process of being destroyed, don't bother
8002 to do anything. */
8003 w->current_matrix != NULL
66301061
KS
8004 /* Don't update mouse highlight if hidden */
8005 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
b2bbd509
GM
8006 /* Recognize when we are called to operate on rows that don't exist
8007 anymore. This can happen when a window is split. */
8008 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
06a2c219 8009 {
b2bbd509
GM
8010 int phys_cursor_on_p = w->phys_cursor_on_p;
8011 struct glyph_row *row, *first, *last;
06a2c219 8012
b2bbd509
GM
8013 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
8014 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7d0393cf 8015
b2bbd509 8016 for (row = first; row <= last && row->enabled_p; ++row)
06a2c219 8017 {
b2bbd509 8018 int start_hpos, end_hpos, start_x;
06a2c219 8019
b2bbd509
GM
8020 /* For all but the first row, the highlight starts at column 0. */
8021 if (row == first)
8022 {
8023 start_hpos = dpyinfo->mouse_face_beg_col;
8024 start_x = dpyinfo->mouse_face_beg_x;
8025 }
8026 else
8027 {
8028 start_hpos = 0;
8029 start_x = 0;
8030 }
06a2c219 8031
b2bbd509
GM
8032 if (row == last)
8033 end_hpos = dpyinfo->mouse_face_end_col;
8034 else
8035 end_hpos = row->used[TEXT_AREA];
b8009dd1 8036
b2bbd509
GM
8037 if (end_hpos > start_hpos)
8038 {
7d0393cf 8039 x_draw_glyphs (w, start_x, row, TEXT_AREA,
b2bbd509 8040 start_hpos, end_hpos, draw, 0);
b8009dd1 8041
60626bab
GM
8042 row->mouse_face_p
8043 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
b2bbd509
GM
8044 }
8045 }
2729a2b5 8046
b2bbd509
GM
8047 /* When we've written over the cursor, arrange for it to
8048 be displayed again. */
8049 if (phys_cursor_on_p && !w->phys_cursor_on_p)
8050 x_display_cursor (w, 1,
8051 w->phys_cursor.hpos, w->phys_cursor.vpos,
8052 w->phys_cursor.x, w->phys_cursor.y);
8053 }
fb3b7de5 8054
06a2c219
GM
8055 /* Change the mouse cursor. */
8056 if (draw == DRAW_NORMAL_TEXT)
8057 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8058 f->output_data.x->text_cursor);
8059 else if (draw == DRAW_MOUSE_FACE)
334208b7 8060 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 8061 f->output_data.x->cross_cursor);
27ead1d5 8062 else
334208b7 8063 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 8064 f->output_data.x->nontext_cursor);
b8009dd1
RS
8065}
8066
8067/* Clear out the mouse-highlighted active region.
fa262c07
GM
8068 Redraw it un-highlighted first. Value is non-zero if mouse
8069 face was actually drawn unhighlighted. */
b8009dd1 8070
fa262c07 8071static int
7a13e894
RS
8072clear_mouse_face (dpyinfo)
8073 struct x_display_info *dpyinfo;
b8009dd1 8074{
fa262c07 8075 int cleared = 0;
7d0393cf 8076
fa262c07
GM
8077 if (!NILP (dpyinfo->mouse_face_window))
8078 {
8079 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
8080 cleared = 1;
8081 }
b8009dd1 8082
7a13e894
RS
8083 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8084 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8085 dpyinfo->mouse_face_window = Qnil;
1b85dc1c 8086 dpyinfo->mouse_face_overlay = Qnil;
fa262c07 8087 return cleared;
b8009dd1 8088}
e687d06e 8089
71b8321e
GM
8090
8091/* Clear any mouse-face on window W. This function is part of the
8092 redisplay interface, and is called from try_window_id and similar
8093 functions to ensure the mouse-highlight is off. */
8094
8095static void
8096x_clear_mouse_face (w)
8097 struct window *w;
8098{
8099 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
8100 Lisp_Object window;
8101
2e636f9d 8102 BLOCK_INPUT;
71b8321e
GM
8103 XSETWINDOW (window, w);
8104 if (EQ (window, dpyinfo->mouse_face_window))
8105 clear_mouse_face (dpyinfo);
2e636f9d 8106 UNBLOCK_INPUT;
71b8321e
GM
8107}
8108
8109
e687d06e
RS
8110/* Just discard the mouse face information for frame F, if any.
8111 This is used when the size of F is changed. */
8112
dfcf069d 8113void
e687d06e
RS
8114cancel_mouse_face (f)
8115 FRAME_PTR f;
8116{
8117 Lisp_Object window;
8118 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8119
8120 window = dpyinfo->mouse_face_window;
8121 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
8122 {
8123 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8124 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8125 dpyinfo->mouse_face_window = Qnil;
8126 }
8127}
b52b65bd 8128
b8009dd1 8129\f
b52b65bd
GM
8130static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
8131
8132
8133/* Try to determine frame pixel position and size of the glyph under
8134 frame pixel coordinates X/Y on frame F . Return the position and
8135 size in *RECT. Value is non-zero if we could compute these
8136 values. */
8137
8138static int
8139glyph_rect (f, x, y, rect)
8140 struct frame *f;
8141 int x, y;
8142 XRectangle *rect;
8143{
8144 Lisp_Object window;
8145 int part, found = 0;
8146
8147 window = window_from_coordinates (f, x, y, &part, 0);
8148 if (!NILP (window))
8149 {
8150 struct window *w = XWINDOW (window);
8151 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8152 struct glyph_row *end = r + w->current_matrix->nrows - 1;
b52b65bd
GM
8153
8154 frame_to_window_pixel_xy (w, &x, &y);
7d0393cf 8155
b52b65bd
GM
8156 for (; !found && r < end && r->enabled_p; ++r)
8157 if (r->y >= y)
8158 {
8159 struct glyph *g = r->glyphs[TEXT_AREA];
8160 struct glyph *end = g + r->used[TEXT_AREA];
8161 int gx;
7d0393cf 8162
b52b65bd
GM
8163 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
8164 if (gx >= x)
8165 {
8166 rect->width = g->pixel_width;
8167 rect->height = r->height;
8168 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
8169 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
8170 found = 1;
8171 }
8172 }
8173 }
8174
8175 return found;
8176}
8177
12ba150f 8178
90e65f07 8179/* Return the current position of the mouse.
b52b65bd 8180 *FP should be a frame which indicates which display to ask about.
90e65f07 8181
b52b65bd
GM
8182 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
8183 and *PART to the frame, window, and scroll bar part that the mouse
8184 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 8185 position on the scroll bar.
12ba150f 8186
b52b65bd
GM
8187 If the mouse movement started elsewhere, set *FP to the frame the
8188 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
8189 the mouse is over.
8190
b52b65bd 8191 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
8192 was at this position.
8193
a135645a
RS
8194 Don't store anything if we don't have a valid set of values to report.
8195
90e65f07 8196 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 8197 movement. */
90e65f07
JB
8198
8199static void
1cf412ec 8200XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 8201 FRAME_PTR *fp;
1cf412ec 8202 int insist;
12ba150f 8203 Lisp_Object *bar_window;
ab648270 8204 enum scroll_bar_part *part;
90e65f07 8205 Lisp_Object *x, *y;
e5d77022 8206 unsigned long *time;
90e65f07 8207{
a135645a
RS
8208 FRAME_PTR f1;
8209
90e65f07
JB
8210 BLOCK_INPUT;
8211
8bcee03e 8212 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 8213 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
8214 else
8215 {
12ba150f
JB
8216 Window root;
8217 int root_x, root_y;
90e65f07 8218
12ba150f
JB
8219 Window dummy_window;
8220 int dummy;
8221
39d8bb4d
KH
8222 Lisp_Object frame, tail;
8223
8224 /* Clear the mouse-moved flag for every frame on this display. */
8225 FOR_EACH_FRAME (tail, frame)
8226 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8227 XFRAME (frame)->mouse_moved = 0;
8228
ab648270 8229 last_mouse_scroll_bar = Qnil;
12ba150f
JB
8230
8231 /* Figure out which root window we're on. */
334208b7
RS
8232 XQueryPointer (FRAME_X_DISPLAY (*fp),
8233 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
8234
8235 /* The root window which contains the pointer. */
8236 &root,
8237
8238 /* Trash which we can't trust if the pointer is on
8239 a different screen. */
8240 &dummy_window,
8241
8242 /* The position on that root window. */
58769bee 8243 &root_x, &root_y,
12ba150f
JB
8244
8245 /* More trash we can't trust. */
8246 &dummy, &dummy,
8247
8248 /* Modifier keys and pointer buttons, about which
8249 we don't care. */
8250 (unsigned int *) &dummy);
8251
8252 /* Now we have a position on the root; find the innermost window
8253 containing the pointer. */
8254 {
8255 Window win, child;
8256 int win_x, win_y;
06a2c219 8257 int parent_x = 0, parent_y = 0;
e99db5a1 8258 int count;
12ba150f
JB
8259
8260 win = root;
69388238 8261
2d7fc7e8
RS
8262 /* XTranslateCoordinates can get errors if the window
8263 structure is changing at the same time this function
8264 is running. So at least we must not crash from them. */
8265
e99db5a1 8266 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 8267
334208b7 8268 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 8269 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 8270 {
69388238
RS
8271 /* If mouse was grabbed on a frame, give coords for that frame
8272 even if the mouse is now outside it. */
334208b7 8273 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 8274
12ba150f 8275 /* From-window, to-window. */
69388238 8276 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
8277
8278 /* From-position, to-position. */
8279 root_x, root_y, &win_x, &win_y,
8280
8281 /* Child of win. */
8282 &child);
69388238
RS
8283 f1 = last_mouse_frame;
8284 }
8285 else
8286 {
8287 while (1)
8288 {
334208b7 8289 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 8290
69388238
RS
8291 /* From-window, to-window. */
8292 root, win,
12ba150f 8293
69388238
RS
8294 /* From-position, to-position. */
8295 root_x, root_y, &win_x, &win_y,
8296
8297 /* Child of win. */
8298 &child);
8299
9af3143a 8300 if (child == None || child == win)
69388238
RS
8301 break;
8302
8303 win = child;
8304 parent_x = win_x;
8305 parent_y = win_y;
8306 }
12ba150f 8307
69388238
RS
8308 /* Now we know that:
8309 win is the innermost window containing the pointer
8310 (XTC says it has no child containing the pointer),
8311 win_x and win_y are the pointer's position in it
8312 (XTC did this the last time through), and
8313 parent_x and parent_y are the pointer's position in win's parent.
8314 (They are what win_x and win_y were when win was child.
8315 If win is the root window, it has no parent, and
8316 parent_{x,y} are invalid, but that's okay, because we'll
8317 never use them in that case.) */
8318
8319 /* Is win one of our frames? */
19126e11 8320 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
8321
8322#ifdef USE_X_TOOLKIT
8323 /* If we end up with the menu bar window, say it's not
8324 on the frame. */
8325 if (f1 != NULL
8326 && f1->output_data.x->menubar_widget
8327 && win == XtWindow (f1->output_data.x->menubar_widget))
8328 f1 = NULL;
8329#endif /* USE_X_TOOLKIT */
69388238 8330 }
58769bee 8331
2d7fc7e8
RS
8332 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8333 f1 = 0;
8334
e99db5a1 8335 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 8336
ab648270 8337 /* If not, is it one of our scroll bars? */
a135645a 8338 if (! f1)
12ba150f 8339 {
ab648270 8340 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
8341
8342 if (bar)
8343 {
a135645a 8344 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8345 win_x = parent_x;
8346 win_y = parent_y;
8347 }
8348 }
90e65f07 8349
8bcee03e 8350 if (f1 == 0 && insist > 0)
b86bd3dd 8351 f1 = SELECTED_FRAME ();
1cf412ec 8352
a135645a 8353 if (f1)
12ba150f 8354 {
06a2c219
GM
8355 /* Ok, we found a frame. Store all the values.
8356 last_mouse_glyph is a rectangle used to reduce the
8357 generation of mouse events. To not miss any motion
8358 events, we must divide the frame into rectangles of the
8359 size of the smallest character that could be displayed
8360 on it, i.e. into the same rectangles that matrices on
8361 the frame are divided into. */
8362
b52b65bd
GM
8363 int width, height, gx, gy;
8364 XRectangle rect;
7d0393cf 8365
b52b65bd
GM
8366 if (glyph_rect (f1, win_x, win_y, &rect))
8367 last_mouse_glyph = rect;
8368 else
8369 {
8370 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8371 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8372 gx = win_x;
8373 gy = win_y;
7d0393cf 8374
b52b65bd
GM
8375 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8376 round down even for negative values. */
8377 if (gx < 0)
8378 gx -= width - 1;
4f00e84d 8379 if (gy < 0)
b52b65bd
GM
8380 gy -= height - 1;
8381 gx = (gx + width - 1) / width * width;
8382 gy = (gy + height - 1) / height * height;
7d0393cf 8383
b52b65bd
GM
8384 last_mouse_glyph.width = width;
8385 last_mouse_glyph.height = height;
8386 last_mouse_glyph.x = gx;
8387 last_mouse_glyph.y = gy;
8388 }
12ba150f
JB
8389
8390 *bar_window = Qnil;
8391 *part = 0;
334208b7 8392 *fp = f1;
e0c1aef2
KH
8393 XSETINT (*x, win_x);
8394 XSETINT (*y, win_y);
12ba150f
JB
8395 *time = last_mouse_movement_time;
8396 }
8397 }
8398 }
90e65f07
JB
8399
8400 UNBLOCK_INPUT;
8401}
f451eb13 8402
06a2c219 8403
06a2c219 8404#ifdef USE_X_TOOLKIT
bffcfca9
GM
8405
8406/* Atimer callback function for TIMER. Called every 0.1s to process
8407 Xt timeouts, if needed. We must avoid calling XtAppPending as
8408 much as possible because that function does an implicit XFlush
8409 that slows us down. */
8410
8411static void
8412x_process_timeouts (timer)
8413 struct atimer *timer;
8414{
488dd4c4 8415 if (toolkit_scroll_bar_interaction || popup_activated ())
bffcfca9
GM
8416 {
8417 BLOCK_INPUT;
8418 while (XtAppPending (Xt_app_con) & XtIMTimer)
8419 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8420 UNBLOCK_INPUT;
8421 }
06a2c219
GM
8422}
8423
bffcfca9 8424#endif /* USE_X_TOOLKIT */
06a2c219
GM
8425
8426\f
8427/* Scroll bar support. */
8428
8429/* Given an X window ID, find the struct scroll_bar which manages it.
8430 This can be called in GC, so we have to make sure to strip off mark
8431 bits. */
bffcfca9 8432
06a2c219
GM
8433static struct scroll_bar *
8434x_window_to_scroll_bar (window_id)
8435 Window window_id;
8436{
8437 Lisp_Object tail;
8438
8439 for (tail = Vframe_list;
8440 XGCTYPE (tail) == Lisp_Cons;
8e713be6 8441 tail = XCDR (tail))
06a2c219
GM
8442 {
8443 Lisp_Object frame, bar, condemned;
8444
8e713be6 8445 frame = XCAR (tail);
06a2c219
GM
8446 /* All elements of Vframe_list should be frames. */
8447 if (! GC_FRAMEP (frame))
8448 abort ();
8449
8450 /* Scan this frame's scroll bar list for a scroll bar with the
8451 right window ID. */
8452 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8453 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8454 /* This trick allows us to search both the ordinary and
8455 condemned scroll bar lists with one loop. */
8456 ! GC_NILP (bar) || (bar = condemned,
8457 condemned = Qnil,
8458 ! GC_NILP (bar));
8459 bar = XSCROLL_BAR (bar)->next)
8460 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8461 return XSCROLL_BAR (bar);
8462 }
8463
8464 return 0;
8465}
8466
8467
01f67d2c 8468#if defined USE_LUCID
c95fc5f1
GM
8469
8470/* Return the Lucid menu bar WINDOW is part of. Return null
8471 if WINDOW is not part of a menu bar. */
8472
8473static Widget
8474x_window_to_menu_bar (window)
8475 Window window;
8476{
8477 Lisp_Object tail;
7d0393cf 8478
c95fc5f1
GM
8479 for (tail = Vframe_list;
8480 XGCTYPE (tail) == Lisp_Cons;
8481 tail = XCDR (tail))
8482 {
8483 Lisp_Object frame = XCAR (tail);
8484 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
7d0393cf 8485
c95fc5f1
GM
8486 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8487 return menu_bar;
8488 }
8489
8490 return NULL;
8491}
8492
01f67d2c 8493#endif /* USE_LUCID */
c95fc5f1 8494
06a2c219
GM
8495\f
8496/************************************************************************
8497 Toolkit scroll bars
8498 ************************************************************************/
8499
eccc05db 8500#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8501
8502static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8503static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8504static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8505 struct scroll_bar *));
8506static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8507 int, int, int));
8508
8509
06a2c219
GM
8510/* Lisp window being scrolled. Set when starting to interact with
8511 a toolkit scroll bar, reset to nil when ending the interaction. */
8512
8513static Lisp_Object window_being_scrolled;
8514
8515/* Last scroll bar part sent in xm_scroll_callback. */
8516
8517static int last_scroll_bar_part;
8518
ec18280f
SM
8519/* Whether this is an Xaw with arrow-scrollbars. This should imply
8520 that movements of 1/20 of the screen size are mapped to up/down. */
8521
488dd4c4
JD
8522#ifndef USE_GTK
8523/* Id of action hook installed for scroll bars. */
8524
8525static XtActionHookId action_hook_id;
8526
ec18280f
SM
8527static Boolean xaw3d_arrow_scroll;
8528
8529/* Whether the drag scrolling maintains the mouse at the top of the
8530 thumb. If not, resizing the thumb needs to be done more carefully
8531 to avoid jerkyness. */
8532
8533static Boolean xaw3d_pick_top;
8534
06a2c219
GM
8535
8536/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 8537 bars are used.. The hook is responsible for detecting when
06a2c219 8538 the user ends an interaction with the scroll bar, and generates
3b8f9651 8539 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
8540
8541static void
8542xt_action_hook (widget, client_data, action_name, event, params,
8543 num_params)
8544 Widget widget;
8545 XtPointer client_data;
8546 String action_name;
8547 XEvent *event;
8548 String *params;
8549 Cardinal *num_params;
8550{
8551 int scroll_bar_p;
8552 char *end_action;
7d0393cf 8553
06a2c219
GM
8554#ifdef USE_MOTIF
8555 scroll_bar_p = XmIsScrollBar (widget);
8556 end_action = "Release";
ec18280f 8557#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8558 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8559 end_action = "EndScroll";
ec18280f 8560#endif /* USE_MOTIF */
06a2c219 8561
06a2c219
GM
8562 if (scroll_bar_p
8563 && strcmp (action_name, end_action) == 0
8564 && WINDOWP (window_being_scrolled))
8565 {
8566 struct window *w;
7d0393cf 8567
06a2c219
GM
8568 x_send_scroll_bar_event (window_being_scrolled,
8569 scroll_bar_end_scroll, 0, 0);
8570 w = XWINDOW (window_being_scrolled);
8571 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8572 window_being_scrolled = Qnil;
8573 last_scroll_bar_part = -1;
bffcfca9
GM
8574
8575 /* Xt timeouts no longer needed. */
8576 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
8577 }
8578}
488dd4c4 8579#endif /* not USE_GTK */
06a2c219 8580
07b3d16e
GM
8581/* A vector of windows used for communication between
8582 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8583
8584static struct window **scroll_bar_windows;
8585static int scroll_bar_windows_size;
8586
06a2c219
GM
8587
8588/* Send a client message with message type Xatom_Scrollbar for a
8589 scroll action to the frame of WINDOW. PART is a value identifying
8590 the part of the scroll bar that was clicked on. PORTION is the
8591 amount to scroll of a whole of WHOLE. */
8592
8593static void
8594x_send_scroll_bar_event (window, part, portion, whole)
8595 Lisp_Object window;
8596 int part, portion, whole;
8597{
8598 XEvent event;
8599 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
8600 struct window *w = XWINDOW (window);
8601 struct frame *f = XFRAME (w->frame);
8602 int i;
06a2c219 8603
07b3d16e 8604 BLOCK_INPUT;
7d0393cf 8605
06a2c219
GM
8606 /* Construct a ClientMessage event to send to the frame. */
8607 ev->type = ClientMessage;
8608 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8609 ev->display = FRAME_X_DISPLAY (f);
8610 ev->window = FRAME_X_WINDOW (f);
8611 ev->format = 32;
07b3d16e
GM
8612
8613 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8614 not enough to store a pointer or Lisp_Object on a 64 bit system.
8615 So, store the window in scroll_bar_windows and pass the index
8616 into that array in the event. */
8617 for (i = 0; i < scroll_bar_windows_size; ++i)
8618 if (scroll_bar_windows[i] == NULL)
8619 break;
8620
8621 if (i == scroll_bar_windows_size)
8622 {
8623 int new_size = max (10, 2 * scroll_bar_windows_size);
8624 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8625 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7d0393cf 8626
07b3d16e
GM
8627 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8628 nbytes);
8629 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8630 scroll_bar_windows_size = new_size;
8631 }
8632
8633 scroll_bar_windows[i] = w;
8634 ev->data.l[0] = (long) i;
06a2c219
GM
8635 ev->data.l[1] = (long) part;
8636 ev->data.l[2] = (long) 0;
8637 ev->data.l[3] = (long) portion;
8638 ev->data.l[4] = (long) whole;
8639
bffcfca9
GM
8640 /* Make Xt timeouts work while the scroll bar is active. */
8641 toolkit_scroll_bar_interaction = 1;
8642
06a2c219
GM
8643 /* Setting the event mask to zero means that the message will
8644 be sent to the client that created the window, and if that
8645 window no longer exists, no event will be sent. */
06a2c219
GM
8646 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8647 UNBLOCK_INPUT;
8648}
8649
8650
8651/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8652 in *IEVENT. */
8653
8654static void
8655x_scroll_bar_to_input_event (event, ievent)
8656 XEvent *event;
8657 struct input_event *ievent;
8658{
8659 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
8660 Lisp_Object window;
8661 struct frame *f;
07b3d16e 8662 struct window *w;
7d0393cf 8663
07b3d16e
GM
8664 w = scroll_bar_windows[ev->data.l[0]];
8665 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 8666
07b3d16e
GM
8667 XSETWINDOW (window, w);
8668 f = XFRAME (w->frame);
7d0393cf 8669
3b8f9651 8670 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 8671 ievent->frame_or_window = window;
0f8aabe9 8672 ievent->arg = Qnil;
488dd4c4
JD
8673#ifdef USE_GTK
8674 ievent->timestamp = CurrentTime;
8675#else
06a2c219 8676 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
488dd4c4 8677#endif
06a2c219
GM
8678 ievent->part = ev->data.l[1];
8679 ievent->code = ev->data.l[2];
8680 ievent->x = make_number ((int) ev->data.l[3]);
8681 ievent->y = make_number ((int) ev->data.l[4]);
8682 ievent->modifiers = 0;
8683}
8684
8685
8686#ifdef USE_MOTIF
8687
8688/* Minimum and maximum values used for Motif scroll bars. */
8689
8690#define XM_SB_MIN 1
8691#define XM_SB_MAX 10000000
8692#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8693
8694
8695/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8696 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4696802b 8697 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
06a2c219
GM
8698
8699static void
8700xm_scroll_callback (widget, client_data, call_data)
8701 Widget widget;
8702 XtPointer client_data, call_data;
8703{
8704 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8705 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
8706 int part = -1, whole = 0, portion = 0;
8707
8708 switch (cs->reason)
8709 {
8710 case XmCR_DECREMENT:
8711 bar->dragging = Qnil;
8712 part = scroll_bar_up_arrow;
8713 break;
8714
8715 case XmCR_INCREMENT:
8716 bar->dragging = Qnil;
8717 part = scroll_bar_down_arrow;
8718 break;
8719
8720 case XmCR_PAGE_DECREMENT:
8721 bar->dragging = Qnil;
8722 part = scroll_bar_above_handle;
8723 break;
8724
8725 case XmCR_PAGE_INCREMENT:
8726 bar->dragging = Qnil;
8727 part = scroll_bar_below_handle;
8728 break;
8729
8730 case XmCR_TO_TOP:
8731 bar->dragging = Qnil;
8732 part = scroll_bar_to_top;
8733 break;
7d0393cf 8734
06a2c219
GM
8735 case XmCR_TO_BOTTOM:
8736 bar->dragging = Qnil;
8737 part = scroll_bar_to_bottom;
8738 break;
8739
8740 case XmCR_DRAG:
8741 {
8742 int slider_size;
8743 int dragging_down_p = (INTEGERP (bar->dragging)
8744 && XINT (bar->dragging) <= cs->value);
8745
8746 /* Get the slider size. */
8747 BLOCK_INPUT;
8748 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8749 UNBLOCK_INPUT;
8750
6f2a7a68
SM
8751 whole = XM_SB_RANGE - slider_size;
8752 portion = min (cs->value - XM_SB_MIN, whole);
8753 part = scroll_bar_handle;
8754 bar->dragging = make_number (cs->value);
06a2c219
GM
8755 }
8756 break;
7d0393cf 8757
06a2c219
GM
8758 case XmCR_VALUE_CHANGED:
8759 break;
8760 };
8761
8762 if (part >= 0)
8763 {
8764 window_being_scrolled = bar->window;
8765 last_scroll_bar_part = part;
8766 x_send_scroll_bar_event (bar->window, part, portion, whole);
8767 }
8768}
8769
8770
488dd4c4
JD
8771#else /* !USE_MOTIF, i.e. Xaw or GTK */
8772#ifdef USE_GTK
8773/* Scroll bar callback for Gtk scroll bars. WIDGET is the scroll
8774 bar adjustment widget. DATA is a pointer to the scroll_bar structure. */
8775
8776static void
8777xg_scroll_callback (widget, data)
8778 GtkWidget *widget;
8779 gpointer data;
8780{
8781 struct scroll_bar *bar = (struct scroll_bar *) data;
8782 gdouble previous;
8783 gdouble position;
8784 gdouble *p;
8785 int diff;
177c0ea7 8786
488dd4c4
JD
8787 int part = -1, whole = 0, portion = 0;
8788 GtkAdjustment *adj = GTK_ADJUSTMENT (widget);
177c0ea7 8789
488dd4c4 8790 if (xg_ignore_gtk_scrollbar) return;
177c0ea7 8791
488dd4c4
JD
8792 position = gtk_adjustment_get_value (adj);
8793
8794 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
8795 if (! p)
8796 {
8797 p = (gdouble*) xmalloc (sizeof (gdouble));
8798 *p = XG_SB_MIN;
8799 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
8800 }
8801
8802 previous = *p;
8803 *p = position;
8804
8805 diff = (int) (position - previous);
177c0ea7 8806
488dd4c4
JD
8807 if (diff == (int) adj->step_increment)
8808 {
8809 part = scroll_bar_down_arrow;
8810 bar->dragging = Qnil;
8811 }
8812 else if (-diff == (int) adj->step_increment)
8813 {
8814 part = scroll_bar_up_arrow;
8815 bar->dragging = Qnil;
8816 }
8817 else if (diff == (int) adj->page_increment)
8818 {
8819 part = scroll_bar_below_handle;
8820 bar->dragging = Qnil;
8821 }
8822 else if (-diff == (int) adj->page_increment)
8823 {
8824 part = scroll_bar_above_handle;
8825 bar->dragging = Qnil;
8826 }
8827 else
8828 {
8829 part = scroll_bar_handle;
8830 whole = adj->upper - adj->page_size;
8831 portion = min (position, whole);
8832 bar->dragging = make_number (portion);
8833 }
177c0ea7 8834
488dd4c4
JD
8835 if (part >= 0)
8836 {
8837 xg_ignore_next_thumb = 1;
8838 window_being_scrolled = bar->window;
8839 last_scroll_bar_part = part;
8840 x_send_scroll_bar_event (bar->window, part, portion, whole);
8841 }
8842}
06a2c219 8843
488dd4c4 8844#else /* not USE_GTK */
06a2c219 8845
ec18280f 8846/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8847 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8848 scroll bar struct. CALL_DATA is a pointer to a float saying where
8849 the thumb is. */
8850
8851static void
ec18280f 8852xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8853 Widget widget;
8854 XtPointer client_data, call_data;
8855{
8856 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8857 float top = *(float *) call_data;
8858 float shown;
ec18280f
SM
8859 int whole, portion, height;
8860 int part;
06a2c219
GM
8861
8862 /* Get the size of the thumb, a value between 0 and 1. */
8863 BLOCK_INPUT;
ec18280f 8864 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8865 UNBLOCK_INPUT;
8866
8867 whole = 10000000;
8868 portion = shown < 1 ? top * whole : 0;
06a2c219 8869
ec18280f
SM
8870 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8871 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8872 the bottom, so we force the scrolling whenever we see that we're
8873 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8874 we try to ensure that we always stay two pixels away from the
8875 bottom). */
06a2c219
GM
8876 part = scroll_bar_down_arrow;
8877 else
8878 part = scroll_bar_handle;
8879
8880 window_being_scrolled = bar->window;
8881 bar->dragging = make_number (portion);
8882 last_scroll_bar_part = part;
8883 x_send_scroll_bar_event (bar->window, part, portion, whole);
8884}
8885
8886
ec18280f
SM
8887/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8888 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8889 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8890 the scroll bar. CALL_DATA is an integer specifying the action that
dbd4b028 8891 has taken place. Its magnitude is in the range 0..height of the
06a2c219
GM
8892 scroll bar. Negative values mean scroll towards buffer start.
8893 Values < height of scroll bar mean line-wise movement. */
8894
8895static void
ec18280f 8896xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8897 Widget widget;
8898 XtPointer client_data, call_data;
8899{
8900 struct scroll_bar *bar = (struct scroll_bar *) client_data;
560d7ceb
DL
8901 /* The position really is stored cast to a pointer. */
8902 int position = (long) call_data;
06a2c219
GM
8903 Dimension height;
8904 int part;
8905
8906 /* Get the height of the scroll bar. */
8907 BLOCK_INPUT;
8908 XtVaGetValues (widget, XtNheight, &height, NULL);
8909 UNBLOCK_INPUT;
8910
ec18280f
SM
8911 if (abs (position) >= height)
8912 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8913
8914 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8915 it maps line-movement to call_data = max(5, height/20). */
8916 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8917 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8918 else
ec18280f 8919 part = scroll_bar_move_ratio;
06a2c219
GM
8920
8921 window_being_scrolled = bar->window;
8922 bar->dragging = Qnil;
8923 last_scroll_bar_part = part;
ec18280f 8924 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8925}
8926
488dd4c4 8927#endif /* not USE_GTK */
06a2c219
GM
8928#endif /* not USE_MOTIF */
8929
488dd4c4 8930#define SCROLL_BAR_NAME "verticalScrollBar"
06a2c219
GM
8931
8932/* Create the widget for scroll bar BAR on frame F. Record the widget
8933 and X window of the scroll bar in BAR. */
8934
488dd4c4
JD
8935#ifdef USE_GTK
8936static void
8937x_create_toolkit_scroll_bar (f, bar)
8938 struct frame *f;
8939 struct scroll_bar *bar;
8940{
8941 char *scroll_bar_name = SCROLL_BAR_NAME;
8942
8943 BLOCK_INPUT;
8944 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
8945 scroll_bar_name);
8946 UNBLOCK_INPUT;
8947}
8948
8949#else /* not USE_GTK */
8950
06a2c219
GM
8951static void
8952x_create_toolkit_scroll_bar (f, bar)
8953 struct frame *f;
8954 struct scroll_bar *bar;
8955{
8956 Window xwindow;
8957 Widget widget;
8958 Arg av[20];
8959 int ac = 0;
488dd4c4 8960 char *scroll_bar_name = SCROLL_BAR_NAME;
06a2c219
GM
8961 unsigned long pixel;
8962
8963 BLOCK_INPUT;
8964
8965#ifdef USE_MOTIF
06a2c219
GM
8966 /* Set resources. Create the widget. */
8967 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8968 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8969 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8970 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8971 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8972 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8973 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8974
8975 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8976 if (pixel != -1)
8977 {
8978 XtSetArg (av[ac], XmNforeground, pixel);
8979 ++ac;
8980 }
7d0393cf 8981
06a2c219
GM
8982 pixel = f->output_data.x->scroll_bar_background_pixel;
8983 if (pixel != -1)
8984 {
8985 XtSetArg (av[ac], XmNbackground, pixel);
8986 ++ac;
8987 }
7d0393cf 8988
06a2c219
GM
8989 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8990 scroll_bar_name, av, ac);
8991
8992 /* Add one callback for everything that can happen. */
8993 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8994 (XtPointer) bar);
8995 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8996 (XtPointer) bar);
8997 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8998 (XtPointer) bar);
8999 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
9000 (XtPointer) bar);
9001 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
9002 (XtPointer) bar);
9003 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
9004 (XtPointer) bar);
9005 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
9006 (XtPointer) bar);
7d0393cf 9007
06a2c219
GM
9008 /* Realize the widget. Only after that is the X window created. */
9009 XtRealizeWidget (widget);
9010
9011 /* Set the cursor to an arrow. I didn't find a resource to do that.
9012 And I'm wondering why it hasn't an arrow cursor by default. */
9013 XDefineCursor (XtDisplay (widget), XtWindow (widget),
9014 f->output_data.x->nontext_cursor);
7d0393cf 9015
ec18280f 9016#else /* !USE_MOTIF i.e. use Xaw */
7d0393cf 9017
06a2c219
GM
9018 /* Set resources. Create the widget. The background of the
9019 Xaw3d scroll bar widget is a little bit light for my taste.
9020 We don't alter it here to let users change it according
9021 to their taste with `emacs*verticalScrollBar.background: xxx'. */
9022 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
9023 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
9024 /* For smoother scrolling with Xaw3d -sm */
9025 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7d0393cf 9026
06a2c219
GM
9027 pixel = f->output_data.x->scroll_bar_foreground_pixel;
9028 if (pixel != -1)
9029 {
9030 XtSetArg (av[ac], XtNforeground, pixel);
9031 ++ac;
9032 }
7d0393cf 9033
06a2c219
GM
9034 pixel = f->output_data.x->scroll_bar_background_pixel;
9035 if (pixel != -1)
9036 {
9037 XtSetArg (av[ac], XtNbackground, pixel);
9038 ++ac;
9039 }
7c1bef7a
MB
9040
9041 /* Top/bottom shadow colors. */
9042
9043 /* Allocate them, if necessary. */
9044 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
9045 {
9046 pixel = f->output_data.x->scroll_bar_background_pixel;
9047 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
9048 &pixel, 1.2, 0x8000))
9049 pixel = -1;
9050 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
9051 }
9052 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
9053 {
9054 pixel = f->output_data.x->scroll_bar_background_pixel;
9055 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
9056 &pixel, 0.6, 0x4000))
9057 pixel = -1;
9058 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
9059 }
9060
9061 /* Tell the toolkit about them. */
9062 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
9063 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
9064 /* We tried to allocate a color for the top/bottom shadow, and
9065 failed, so tell Xaw3d to use dithering instead. */
9066 {
9067 XtSetArg (av[ac], XtNbeNiceToColormap, True);
9068 ++ac;
9069 }
9070 else
9071 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
9072 be more consistent with other emacs 3d colors, and since Xaw3d is
9073 not good at dealing with allocation failure. */
9074 {
9075 /* This tells Xaw3d to use real colors instead of dithering for
9076 the shadows. */
9077 XtSetArg (av[ac], XtNbeNiceToColormap, False);
9078 ++ac;
9079
9080 /* Specify the colors. */
9081 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
9082 if (pixel != -1)
9083 {
9084 XtSetArg (av[ac], "topShadowPixel", pixel);
9085 ++ac;
9086 }
9087 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
9088 if (pixel != -1)
9089 {
9090 XtSetArg (av[ac], "bottomShadowPixel", pixel);
9091 ++ac;
9092 }
9093 }
9094
06a2c219
GM
9095 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
9096 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
9097
9098 {
9099 char *initial = "";
9100 char *val = initial;
9101 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
9102 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
9103 if (val == initial)
9104 { /* ARROW_SCROLL */
9105 xaw3d_arrow_scroll = True;
9106 /* Isn't that just a personal preference ? -sm */
9107 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
9108 }
9109 }
7d0393cf 9110
06a2c219 9111 /* Define callbacks. */
ec18280f
SM
9112 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
9113 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219 9114 (XtPointer) bar);
7d0393cf 9115
06a2c219
GM
9116 /* Realize the widget. Only after that is the X window created. */
9117 XtRealizeWidget (widget);
7d0393cf 9118
ec18280f 9119#endif /* !USE_MOTIF */
06a2c219 9120
2a51c026 9121 /* Install an action hook that lets us detect when the user
06a2c219
GM
9122 finishes interacting with a scroll bar. */
9123 if (action_hook_id == 0)
9124 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7d0393cf 9125
06a2c219
GM
9126 /* Remember X window and widget in the scroll bar vector. */
9127 SET_SCROLL_BAR_X_WIDGET (bar, widget);
9128 xwindow = XtWindow (widget);
9129 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
9130
9131 UNBLOCK_INPUT;
9132}
488dd4c4 9133#endif /* not USE_GTK */
06a2c219
GM
9134
9135
9136/* Set the thumb size and position of scroll bar BAR. We are currently
9137 displaying PORTION out of a whole WHOLE, and our position POSITION. */
9138
488dd4c4
JD
9139#ifdef USE_GTK
9140static void
9141x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
9142 struct scroll_bar *bar;
9143 int portion, position, whole;
9144{
9145 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9146}
9147
9148#else /* not USE_GTK */
06a2c219
GM
9149static void
9150x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
9151 struct scroll_bar *bar;
9152 int portion, position, whole;
f451eb13 9153{
e83dc917
GM
9154 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9155 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 9156 float top, shown;
f451eb13 9157
6f2a7a68
SM
9158 BLOCK_INPUT;
9159
9160#ifdef USE_MOTIF
9161
9162 /* We use an estimate of 30 chars per line rather than the real
9163 `portion' value. This has the disadvantage that the thumb size
9164 is not very representative, but it makes our life a lot easier.
9165 Otherwise, we have to constantly adjust the thumb size, which
9166 we can't always do quickly enough: while dragging, the size of
9167 the thumb might prevent the user from dragging the thumb all the
9168 way to the end. but Motif and some versions of Xaw3d don't allow
9169 updating the thumb size while dragging. Also, even if we can update
9170 its size, the update will often happen too late.
9171 If you don't believe it, check out revision 1.650 of xterm.c to see
9172 what hoops we were going through and the still poor behavior we got. */
9173 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
9174 /* When the thumb is at the bottom, position == whole.
9175 So we need to increase `whole' to make space for the thumb. */
9176 whole += portion;
9177
9178 if (whole <= 0)
06a2c219
GM
9179 top = 0, shown = 1;
9180 else
f451eb13 9181 {
06a2c219
GM
9182 top = (float) position / whole;
9183 shown = (float) portion / whole;
9184 }
f451eb13 9185
6f2a7a68
SM
9186 if (NILP (bar->dragging))
9187 {
9188 int size, value;
06a2c219 9189
6f2a7a68
SM
9190 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
9191 is the scroll bar's maximum and MIN is the scroll bar's minimum
9192 value. */
9193 size = shown * XM_SB_RANGE;
9194 size = min (size, XM_SB_RANGE);
9195 size = max (size, 1);
06a2c219 9196
6f2a7a68
SM
9197 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
9198 value = top * XM_SB_RANGE;
9199 value = min (value, XM_SB_MAX - size);
9200 value = max (value, XM_SB_MIN);
7d0393cf 9201
06a2c219 9202 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 9203 }
ec18280f 9204#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
9205
9206 if (whole == 0)
9207 top = 0, shown = 1;
9208 else
9209 {
9210 top = (float) position / whole;
9211 shown = (float) portion / whole;
9212 }
9213
06a2c219 9214 {
ec18280f
SM
9215 float old_top, old_shown;
9216 Dimension height;
9217 XtVaGetValues (widget,
9218 XtNtopOfThumb, &old_top,
9219 XtNshown, &old_shown,
9220 XtNheight, &height,
9221 NULL);
9222
9223 /* Massage the top+shown values. */
9224 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
9225 top = max (0, min (1, top));
9226 else
9227 top = old_top;
9228 /* Keep two pixels available for moving the thumb down. */
9229 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
9230
9231 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
9232 check that your system's configuration file contains a define
9233 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 9234 if (top != old_top || shown != old_shown)
eb393530 9235 {
ec18280f 9236 if (NILP (bar->dragging))
eb393530 9237 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
9238 else
9239 {
ec18280f
SM
9240#ifdef HAVE_XAW3D
9241 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 9242 int scroll_mode = 0;
7d0393cf 9243
ec18280f
SM
9244 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
9245 if (xaw3d_arrow_scroll)
9246 {
9247 /* Xaw3d stupidly ignores resize requests while dragging
9248 so we have to make it believe it's not in dragging mode. */
9249 scroll_mode = sb->scrollbar.scroll_mode;
9250 if (scroll_mode == 2)
9251 sb->scrollbar.scroll_mode = 0;
9252 }
9253#endif
9254 /* Try to make the scrolling a tad smoother. */
9255 if (!xaw3d_pick_top)
9256 shown = min (shown, old_shown);
7d0393cf 9257
ec18280f 9258 XawScrollbarSetThumb (widget, top, shown);
7d0393cf 9259
ec18280f
SM
9260#ifdef HAVE_XAW3D
9261 if (xaw3d_arrow_scroll && scroll_mode == 2)
9262 sb->scrollbar.scroll_mode = scroll_mode;
9263#endif
06a2c219 9264 }
06a2c219
GM
9265 }
9266 }
ec18280f 9267#endif /* !USE_MOTIF */
06a2c219
GM
9268
9269 UNBLOCK_INPUT;
f451eb13 9270}
488dd4c4 9271#endif /* not USE_GTK */
f451eb13 9272
06a2c219
GM
9273#endif /* USE_TOOLKIT_SCROLL_BARS */
9274
9275
9276\f
9277/************************************************************************
9278 Scroll bars, general
9279 ************************************************************************/
7d0393cf 9280
06a2c219
GM
9281/* Create a scroll bar and return the scroll bar vector for it. W is
9282 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 9283 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
9284 scroll bar. */
9285
ab648270 9286static struct scroll_bar *
06a2c219
GM
9287x_scroll_bar_create (w, top, left, width, height)
9288 struct window *w;
f451eb13
JB
9289 int top, left, width, height;
9290{
06a2c219 9291 struct frame *f = XFRAME (w->frame);
334208b7
RS
9292 struct scroll_bar *bar
9293 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
9294
9295 BLOCK_INPUT;
9296
eccc05db 9297#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9298 x_create_toolkit_scroll_bar (f, bar);
9299#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9300 {
9301 XSetWindowAttributes a;
9302 unsigned long mask;
5c187dee 9303 Window window;
06a2c219
GM
9304
9305 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9306 if (a.background_pixel == -1)
9307 a.background_pixel = f->output_data.x->background_pixel;
7d0393cf 9308
12ba150f 9309 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 9310 | ButtonMotionMask | PointerMotionHintMask
12ba150f 9311 | ExposureMask);
7a13e894 9312 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 9313
dbc4e1c1 9314 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 9315
06a2c219
GM
9316 /* Clear the area of W that will serve as a scroll bar. This is
9317 for the case that a window has been split horizontally. In
9318 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
9319 if (width > 0 && height > 0)
9320 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9321 left, top, width,
9322 window_box_height (w), False);
06a2c219
GM
9323
9324 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9325 /* Position and size of scroll bar. */
9326 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9327 top,
9328 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9329 height,
9330 /* Border width, depth, class, and visual. */
9331 0,
9332 CopyFromParent,
9333 CopyFromParent,
9334 CopyFromParent,
9335 /* Attributes. */
9336 mask, &a);
9337 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 9338 }
06a2c219 9339#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9340
06a2c219 9341 XSETWINDOW (bar->window, w);
e0c1aef2
KH
9342 XSETINT (bar->top, top);
9343 XSETINT (bar->left, left);
9344 XSETINT (bar->width, width);
9345 XSETINT (bar->height, height);
9346 XSETINT (bar->start, 0);
9347 XSETINT (bar->end, 0);
12ba150f 9348 bar->dragging = Qnil;
f451eb13
JB
9349
9350 /* Add bar to its frame's list of scroll bars. */
334208b7 9351 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 9352 bar->prev = Qnil;
334208b7 9353 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 9354 if (!NILP (bar->next))
e0c1aef2 9355 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 9356
06a2c219 9357 /* Map the window/widget. */
eccc05db 9358#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7 9359 {
488dd4c4
JD
9360#ifdef USE_GTK
9361 xg_update_scrollbar_pos (f,
9362 SCROLL_BAR_X_WINDOW (bar),
9363 top,
9364 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9365 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9366 max (height, 1));
9367 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
9368#else /* not USE_GTK */
f964b4d7
GM
9369 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9370 XtConfigureWidget (scroll_bar,
9371 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9372 top,
9373 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9374 max (height, 1), 0);
9375 XtMapWidget (scroll_bar);
488dd4c4 9376#endif /* not USE_GTK */
f964b4d7 9377 }
06a2c219 9378#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 9379 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 9380#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9381
9382 UNBLOCK_INPUT;
12ba150f 9383 return bar;
f451eb13
JB
9384}
9385
06a2c219 9386
12ba150f 9387/* Draw BAR's handle in the proper position.
7d0393cf 9388
12ba150f
JB
9389 If the handle is already drawn from START to END, don't bother
9390 redrawing it, unless REBUILD is non-zero; in that case, always
9391 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 9392 events.)
12ba150f
JB
9393
9394 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
9395 fit inside its rectangle, but if the user is dragging the scroll
9396 bar handle, we want to let them drag it down all the way, so that
9397 the bar's top is as far down as it goes; otherwise, there's no way
9398 to move to the very end of the buffer. */
9399
5c187dee
GM
9400#ifndef USE_TOOLKIT_SCROLL_BARS
9401
f451eb13 9402static void
ab648270
JB
9403x_scroll_bar_set_handle (bar, start, end, rebuild)
9404 struct scroll_bar *bar;
f451eb13 9405 int start, end;
12ba150f 9406 int rebuild;
f451eb13 9407{
12ba150f 9408 int dragging = ! NILP (bar->dragging);
ab648270 9409 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9410 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9411 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
9412
9413 /* If the display is already accurate, do nothing. */
9414 if (! rebuild
9415 && start == XINT (bar->start)
9416 && end == XINT (bar->end))
9417 return;
9418
f451eb13
JB
9419 BLOCK_INPUT;
9420
9421 {
d9cdbb3d
RS
9422 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9423 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9424 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
9425
9426 /* Make sure the values are reasonable, and try to preserve
9427 the distance between start and end. */
12ba150f
JB
9428 {
9429 int length = end - start;
9430
9431 if (start < 0)
9432 start = 0;
9433 else if (start > top_range)
9434 start = top_range;
9435 end = start + length;
9436
9437 if (end < start)
9438 end = start;
9439 else if (end > top_range && ! dragging)
9440 end = top_range;
9441 }
f451eb13 9442
ab648270 9443 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
9444 XSETINT (bar->start, start);
9445 XSETINT (bar->end, end);
f451eb13 9446
12ba150f
JB
9447 /* Clip the end position, just for display. */
9448 if (end > top_range)
9449 end = top_range;
f451eb13 9450
ab648270 9451 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
9452 below top positions, to make sure the handle is always at least
9453 that many pixels tall. */
ab648270 9454 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 9455
12ba150f
JB
9456 /* Draw the empty space above the handle. Note that we can't clear
9457 zero-height areas; that means "clear to end of window." */
9458 if (0 < start)
c5e6e06b
GM
9459 x_clear_area (FRAME_X_DISPLAY (f), w,
9460 /* x, y, width, height, and exposures. */
9461 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9462 VERTICAL_SCROLL_BAR_TOP_BORDER,
9463 inside_width, start,
9464 False);
f451eb13 9465
06a2c219
GM
9466 /* Change to proper foreground color if one is specified. */
9467 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9468 XSetForeground (FRAME_X_DISPLAY (f), gc,
9469 f->output_data.x->scroll_bar_foreground_pixel);
9470
12ba150f 9471 /* Draw the handle itself. */
334208b7 9472 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 9473 /* x, y, width, height */
ab648270
JB
9474 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9475 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 9476 inside_width, end - start);
f451eb13 9477
06a2c219
GM
9478 /* Restore the foreground color of the GC if we changed it above. */
9479 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9480 XSetForeground (FRAME_X_DISPLAY (f), gc,
9481 f->output_data.x->foreground_pixel);
f451eb13 9482
12ba150f
JB
9483 /* Draw the empty space below the handle. Note that we can't
9484 clear zero-height areas; that means "clear to end of window." */
9485 if (end < inside_height)
c5e6e06b
GM
9486 x_clear_area (FRAME_X_DISPLAY (f), w,
9487 /* x, y, width, height, and exposures. */
9488 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9489 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9490 inside_width, inside_height - end,
9491 False);
f451eb13 9492
f451eb13
JB
9493 }
9494
f451eb13
JB
9495 UNBLOCK_INPUT;
9496}
9497
5c187dee 9498#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 9499
06a2c219
GM
9500/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9501 nil. */
58769bee 9502
12ba150f 9503static void
ab648270
JB
9504x_scroll_bar_remove (bar)
9505 struct scroll_bar *bar;
12ba150f 9506{
e83dc917 9507 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
9508 BLOCK_INPUT;
9509
eccc05db 9510#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4
JD
9511#ifdef USE_GTK
9512 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
9513#else /* not USE_GTK */
e83dc917 9514 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
488dd4c4 9515#endif /* not USE_GTK */
e83dc917
GM
9516#else
9517 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9518#endif
7d0393cf 9519
ab648270
JB
9520 /* Disassociate this scroll bar from its window. */
9521 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
9522
9523 UNBLOCK_INPUT;
9524}
9525
06a2c219 9526
12ba150f
JB
9527/* Set the handle of the vertical scroll bar for WINDOW to indicate
9528 that we are displaying PORTION characters out of a total of WHOLE
ab648270 9529 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 9530 create one. */
06a2c219 9531
12ba150f 9532static void
06a2c219
GM
9533XTset_vertical_scroll_bar (w, portion, whole, position)
9534 struct window *w;
f451eb13
JB
9535 int portion, whole, position;
9536{
06a2c219 9537 struct frame *f = XFRAME (w->frame);
ab648270 9538 struct scroll_bar *bar;
3c6ede7b 9539 int top, height, left, sb_left, width, sb_width;
06a2c219 9540 int window_x, window_y, window_width, window_height;
06a2c219 9541
3c6ede7b 9542 /* Get window dimensions. */
06a2c219 9543 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
9544 top = window_y;
9545 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9546 height = window_height;
06a2c219 9547
3c6ede7b 9548 /* Compute the left edge of the scroll bar area. */
06a2c219 9549 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
9550 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9551 else
9552 left = XFASTINT (w->left);
9553 left *= CANON_X_UNIT (f);
9554 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9555
9556 /* Compute the width of the scroll bar which might be less than
9557 the width of the area reserved for the scroll bar. */
9558 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9559 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 9560 else
3c6ede7b 9561 sb_width = width;
12ba150f 9562
3c6ede7b
GM
9563 /* Compute the left edge of the scroll bar. */
9564#ifdef USE_TOOLKIT_SCROLL_BARS
9565 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7d0393cf 9566 sb_left = left + width - sb_width - (width - sb_width) / 2;
3c6ede7b
GM
9567 else
9568 sb_left = left + (width - sb_width) / 2;
9569#else
9570 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7d0393cf 9571 sb_left = left + width - sb_width;
3c6ede7b
GM
9572 else
9573 sb_left = left;
9574#endif
7d0393cf 9575
ab648270 9576 /* Does the scroll bar exist yet? */
06a2c219 9577 if (NILP (w->vertical_scroll_bar))
3c6ede7b 9578 {
7b49b9d2 9579 if (width > 0 && height > 0)
b547b6e8
GM
9580 {
9581 BLOCK_INPUT;
9582 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9583 left, top, width, height, False);
9584 UNBLOCK_INPUT;
9585 }
7d0393cf 9586
3c6ede7b
GM
9587 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9588 }
f451eb13 9589 else
12ba150f
JB
9590 {
9591 /* It may just need to be moved and resized. */
06a2c219 9592 unsigned int mask = 0;
7d0393cf 9593
06a2c219
GM
9594 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9595
9596 BLOCK_INPUT;
9597
3c6ede7b 9598 if (sb_left != XINT (bar->left))
06a2c219 9599 mask |= CWX;
3c6ede7b 9600 if (top != XINT (bar->top))
06a2c219 9601 mask |= CWY;
3c6ede7b 9602 if (sb_width != XINT (bar->width))
06a2c219 9603 mask |= CWWidth;
7d0393cf 9604 if (height != XINT (bar->height))
06a2c219 9605 mask |= CWHeight;
7d0393cf 9606
06a2c219 9607#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9 9608
488dd4c4
JD
9609#ifdef USE_GTK
9610 if (mask)
9611 xg_update_scrollbar_pos (f,
9612 SCROLL_BAR_X_WINDOW (bar),
9613 top,
9614 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9615 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9616 max (height, 1));
9617#else /* not USE_GTK */
9618
fe6f39d9 9619 /* Since toolkit scroll bars are smaller than the space reserved
488dd4c4 9620 for them on the frame, we have to clear "under" them. */
7b49b9d2 9621 if (width > 0 && height > 0)
488dd4c4
JD
9622 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9623 left, top, width, height, False);
06a2c219
GM
9624 /* Move/size the scroll bar widget. */
9625 if (mask)
488dd4c4
JD
9626 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9627 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9628 top,
9629 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9630 max (height, 1), 0);
06a2c219 9631
488dd4c4 9632#endif /* not USE_GTK */
06a2c219 9633#else /* not USE_TOOLKIT_SCROLL_BARS */
7d0393cf 9634
357e7376
GM
9635 /* Clear areas not covered by the scroll bar because of
9636 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
9637 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9638 {
c5e6e06b
GM
9639 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9640 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9641 height, False);
9642 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9643 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9644 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9645 height, False);
e1f6572f 9646 }
357e7376
GM
9647
9648 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 9649 wide as the area reserved for it. This makes sure a
357e7376
GM
9650 previous mode line display is cleared after C-x 2 C-x 1, for
9651 example. */
9652 {
9653 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9654 int rest = area_width - sb_width;
38d2af0c
GM
9655 if (rest > 0 && height > 0)
9656 {
9657 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9658 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9659 left + area_width - rest, top,
9660 rest, height, False);
9661 else
9662 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9663 left, top, rest, height, False);
9664 }
357e7376 9665 }
7d0393cf 9666
06a2c219
GM
9667 /* Move/size the scroll bar window. */
9668 if (mask)
9669 {
9670 XWindowChanges wc;
7d0393cf 9671
3c6ede7b
GM
9672 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9673 wc.y = top;
9674 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9675 wc.height = height;
06a2c219
GM
9676 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9677 mask, &wc);
9678 }
7d0393cf 9679
06a2c219
GM
9680#endif /* not USE_TOOLKIT_SCROLL_BARS */
9681
9682 /* Remember new settings. */
3c6ede7b
GM
9683 XSETINT (bar->left, sb_left);
9684 XSETINT (bar->top, top);
9685 XSETINT (bar->width, sb_width);
9686 XSETINT (bar->height, height);
7d0393cf 9687
06a2c219 9688 UNBLOCK_INPUT;
12ba150f 9689 }
f451eb13 9690
eccc05db 9691#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9692 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9693#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 9694 /* Set the scroll bar's current state, unless we're currently being
f451eb13 9695 dragged. */
12ba150f 9696 if (NILP (bar->dragging))
f451eb13 9697 {
92857db0 9698 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 9699
12ba150f 9700 if (whole == 0)
ab648270 9701 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
9702 else
9703 {
43f868f5
JB
9704 int start = ((double) position * top_range) / whole;
9705 int end = ((double) (position + portion) * top_range) / whole;
ab648270 9706 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 9707 }
f451eb13 9708 }
06a2c219 9709#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9710
06a2c219 9711 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
9712}
9713
12ba150f 9714
f451eb13 9715/* The following three hooks are used when we're doing a thorough
ab648270 9716 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 9717 are going to be deleted, because keeping track of when windows go
12ba150f
JB
9718 away is a real pain - "Can you say set-window-configuration, boys
9719 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 9720 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 9721 from the fiery pit when we actually redisplay its window. */
f451eb13 9722
ab648270
JB
9723/* Arrange for all scroll bars on FRAME to be removed at the next call
9724 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
9725 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9726
58769bee 9727static void
ab648270 9728XTcondemn_scroll_bars (frame)
f451eb13
JB
9729 FRAME_PTR frame;
9730{
f9e24cb9
RS
9731 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9732 while (! NILP (FRAME_SCROLL_BARS (frame)))
9733 {
9734 Lisp_Object bar;
9735 bar = FRAME_SCROLL_BARS (frame);
9736 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9737 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9738 XSCROLL_BAR (bar)->prev = Qnil;
9739 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9740 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9741 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9742 }
f451eb13
JB
9743}
9744
fa2dfc30 9745
06a2c219 9746/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 9747 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 9748
f451eb13 9749static void
ab648270 9750XTredeem_scroll_bar (window)
12ba150f 9751 struct window *window;
f451eb13 9752{
ab648270 9753 struct scroll_bar *bar;
fa2dfc30 9754 struct frame *f;
12ba150f 9755
ab648270
JB
9756 /* We can't redeem this window's scroll bar if it doesn't have one. */
9757 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
9758 abort ();
9759
ab648270 9760 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
9761
9762 /* Unlink it from the condemned list. */
fa2dfc30
GM
9763 f = XFRAME (WINDOW_FRAME (window));
9764 if (NILP (bar->prev))
9765 {
9766 /* If the prev pointer is nil, it must be the first in one of
9767 the lists. */
9768 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9769 /* It's not condemned. Everything's fine. */
9770 return;
9771 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9772 window->vertical_scroll_bar))
9773 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9774 else
9775 /* If its prev pointer is nil, it must be at the front of
9776 one or the other! */
9777 abort ();
9778 }
9779 else
9780 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 9781
fa2dfc30
GM
9782 if (! NILP (bar->next))
9783 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 9784
fa2dfc30
GM
9785 bar->next = FRAME_SCROLL_BARS (f);
9786 bar->prev = Qnil;
9787 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9788 if (! NILP (bar->next))
9789 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
9790}
9791
ab648270
JB
9792/* Remove all scroll bars on FRAME that haven't been saved since the
9793 last call to `*condemn_scroll_bars_hook'. */
06a2c219 9794
f451eb13 9795static void
ab648270 9796XTjudge_scroll_bars (f)
12ba150f 9797 FRAME_PTR f;
f451eb13 9798{
12ba150f 9799 Lisp_Object bar, next;
f451eb13 9800
ab648270 9801 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
9802
9803 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
9804 more events on the hapless scroll bars. */
9805 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
9806
9807 for (; ! NILP (bar); bar = next)
f451eb13 9808 {
ab648270 9809 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 9810
ab648270 9811 x_scroll_bar_remove (b);
12ba150f
JB
9812
9813 next = b->next;
9814 b->next = b->prev = Qnil;
f451eb13 9815 }
12ba150f 9816
ab648270 9817 /* Now there should be no references to the condemned scroll bars,
12ba150f 9818 and they should get garbage-collected. */
f451eb13
JB
9819}
9820
9821
06a2c219
GM
9822/* Handle an Expose or GraphicsExpose event on a scroll bar. This
9823 is a no-op when using toolkit scroll bars.
ab648270
JB
9824
9825 This may be called from a signal handler, so we have to ignore GC
9826 mark bits. */
06a2c219 9827
f451eb13 9828static void
ab648270
JB
9829x_scroll_bar_expose (bar, event)
9830 struct scroll_bar *bar;
f451eb13
JB
9831 XEvent *event;
9832{
06a2c219
GM
9833#ifndef USE_TOOLKIT_SCROLL_BARS
9834
ab648270 9835 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9836 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9837 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 9838 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 9839
f451eb13
JB
9840 BLOCK_INPUT;
9841
ab648270 9842 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 9843
06a2c219 9844 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 9845 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
9846
9847 /* x, y, width, height */
d9cdbb3d 9848 0, 0,
3cbd2e0b 9849 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d 9850 XINT (bar->height) - 1);
7d0393cf 9851
f451eb13 9852 UNBLOCK_INPUT;
06a2c219
GM
9853
9854#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9855}
9856
ab648270 9857/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 9858 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
9859
9860 This may be called from a signal handler, so we have to ignore GC
9861 mark bits. */
06a2c219 9862
5c187dee
GM
9863#ifndef USE_TOOLKIT_SCROLL_BARS
9864
f451eb13 9865static void
ab648270
JB
9866x_scroll_bar_handle_click (bar, event, emacs_event)
9867 struct scroll_bar *bar;
f451eb13
JB
9868 XEvent *event;
9869 struct input_event *emacs_event;
9870{
0299d313 9871 if (! GC_WINDOWP (bar->window))
12ba150f
JB
9872 abort ();
9873
3b8f9651 9874 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 9875 emacs_event->code = event->xbutton.button - Button1;
0299d313 9876 emacs_event->modifiers
7d0393cf 9877 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
0299d313
RS
9878 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9879 event->xbutton.state)
9880 | (event->type == ButtonRelease
9881 ? up_modifier
9882 : down_modifier));
12ba150f 9883 emacs_event->frame_or_window = bar->window;
0f8aabe9 9884 emacs_event->arg = Qnil;
f451eb13 9885 emacs_event->timestamp = event->xbutton.time;
12ba150f 9886 {
06a2c219 9887#if 0
d9cdbb3d 9888 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 9889 int internal_height
d9cdbb3d 9890 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9891#endif
0299d313 9892 int top_range
d9cdbb3d 9893 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 9894 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
9895
9896 if (y < 0) y = 0;
9897 if (y > top_range) y = top_range;
9898
9899 if (y < XINT (bar->start))
ab648270
JB
9900 emacs_event->part = scroll_bar_above_handle;
9901 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9902 emacs_event->part = scroll_bar_handle;
12ba150f 9903 else
ab648270 9904 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
9905
9906 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
9907 they want to drag it. Lisp code needs to be able to decide
9908 whether or not we're dragging. */
929787e1 9909#if 0
12ba150f
JB
9910 /* If the user has just clicked on the handle, record where they're
9911 holding it. */
9912 if (event->type == ButtonPress
ab648270 9913 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9914 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9915#endif
12ba150f
JB
9916
9917 /* If the user has released the handle, set it to its final position. */
9918 if (event->type == ButtonRelease
9919 && ! NILP (bar->dragging))
9920 {
9921 int new_start = y - XINT (bar->dragging);
9922 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9923
ab648270 9924 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9925 bar->dragging = Qnil;
9926 }
f451eb13 9927
5116f055
JB
9928 /* Same deal here as the other #if 0. */
9929#if 0
58769bee 9930 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9931 the handle. */
ab648270 9932 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9933 emacs_event->x = bar->start;
9934 else
e0c1aef2 9935 XSETINT (emacs_event->x, y);
5116f055 9936#else
e0c1aef2 9937 XSETINT (emacs_event->x, y);
5116f055 9938#endif
f451eb13 9939
e0c1aef2 9940 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9941 }
9942}
f451eb13 9943
ab648270
JB
9944/* Handle some mouse motion while someone is dragging the scroll bar.
9945
9946 This may be called from a signal handler, so we have to ignore GC
9947 mark bits. */
06a2c219 9948
f451eb13 9949static void
ab648270
JB
9950x_scroll_bar_note_movement (bar, event)
9951 struct scroll_bar *bar;
f451eb13
JB
9952 XEvent *event;
9953{
39d8bb4d
KH
9954 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9955
f451eb13
JB
9956 last_mouse_movement_time = event->xmotion.time;
9957
39d8bb4d 9958 f->mouse_moved = 1;
e0c1aef2 9959 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9960
9961 /* If we're dragging the bar, display it. */
ab648270 9962 if (! GC_NILP (bar->dragging))
f451eb13
JB
9963 {
9964 /* Where should the handle be now? */
12ba150f 9965 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9966
12ba150f 9967 if (new_start != XINT (bar->start))
f451eb13 9968 {
12ba150f 9969 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9970
ab648270 9971 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9972 }
9973 }
f451eb13
JB
9974}
9975
5c187dee
GM
9976#endif /* !USE_TOOLKIT_SCROLL_BARS */
9977
12ba150f 9978/* Return information to the user about the current position of the mouse
ab648270 9979 on the scroll bar. */
06a2c219 9980
12ba150f 9981static void
334208b7
RS
9982x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9983 FRAME_PTR *fp;
12ba150f 9984 Lisp_Object *bar_window;
ab648270 9985 enum scroll_bar_part *part;
12ba150f
JB
9986 Lisp_Object *x, *y;
9987 unsigned long *time;
9988{
ab648270 9989 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9990 Window w = SCROLL_BAR_X_WINDOW (bar);
9991 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9992 int win_x, win_y;
559cb2fb
JB
9993 Window dummy_window;
9994 int dummy_coord;
9995 unsigned int dummy_mask;
12ba150f 9996
cf7cb199
JB
9997 BLOCK_INPUT;
9998
ab648270 9999 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 10000 report that. */
334208b7 10001 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 10002
559cb2fb
JB
10003 /* Root, child, root x and root y. */
10004 &dummy_window, &dummy_window,
10005 &dummy_coord, &dummy_coord,
12ba150f 10006
559cb2fb
JB
10007 /* Position relative to scroll bar. */
10008 &win_x, &win_y,
12ba150f 10009
559cb2fb
JB
10010 /* Mouse buttons and modifier keys. */
10011 &dummy_mask))
7a13e894 10012 ;
559cb2fb
JB
10013 else
10014 {
06a2c219 10015#if 0
559cb2fb 10016 int inside_height
d9cdbb3d 10017 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 10018#endif
559cb2fb 10019 int top_range
d9cdbb3d 10020 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
10021
10022 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
10023
10024 if (! NILP (bar->dragging))
10025 win_y -= XINT (bar->dragging);
10026
10027 if (win_y < 0)
10028 win_y = 0;
10029 if (win_y > top_range)
10030 win_y = top_range;
10031
334208b7 10032 *fp = f;
7a13e894 10033 *bar_window = bar->window;
559cb2fb
JB
10034
10035 if (! NILP (bar->dragging))
10036 *part = scroll_bar_handle;
10037 else if (win_y < XINT (bar->start))
10038 *part = scroll_bar_above_handle;
10039 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
10040 *part = scroll_bar_handle;
10041 else
10042 *part = scroll_bar_below_handle;
12ba150f 10043
e0c1aef2
KH
10044 XSETINT (*x, win_y);
10045 XSETINT (*y, top_range);
12ba150f 10046
39d8bb4d 10047 f->mouse_moved = 0;
559cb2fb
JB
10048 last_mouse_scroll_bar = Qnil;
10049 }
12ba150f 10050
559cb2fb 10051 *time = last_mouse_movement_time;
cf7cb199 10052
cf7cb199 10053 UNBLOCK_INPUT;
12ba150f
JB
10054}
10055
f451eb13 10056
dbc4e1c1 10057/* The screen has been cleared so we may have changed foreground or
ab648270
JB
10058 background colors, and the scroll bars may need to be redrawn.
10059 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
10060 redraw them. */
10061
dfcf069d 10062void
ab648270 10063x_scroll_bar_clear (f)
dbc4e1c1
JB
10064 FRAME_PTR f;
10065{
06a2c219 10066#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
10067 Lisp_Object bar;
10068
b80c363e
RS
10069 /* We can have scroll bars even if this is 0,
10070 if we just turned off scroll bar mode.
10071 But in that case we should not clear them. */
10072 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10073 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
10074 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
10075 XClearArea (FRAME_X_DISPLAY (f),
10076 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 10077 0, 0, 0, 0, True);
06a2c219 10078#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
10079}
10080
09756a85
RS
10081\f
10082/* Define a queue to save up SelectionRequest events for later handling. */
10083
10084struct selection_event_queue
10085 {
10086 XEvent event;
10087 struct selection_event_queue *next;
10088 };
10089
10090static struct selection_event_queue *queue;
10091
10092/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 10093
09756a85
RS
10094static int x_queue_selection_requests;
10095
10096/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 10097
09756a85 10098static void
334208b7
RS
10099x_queue_event (f, event)
10100 FRAME_PTR f;
09756a85
RS
10101 XEvent *event;
10102{
10103 struct selection_event_queue *queue_tmp
06a2c219 10104 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 10105
58769bee 10106 if (queue_tmp != NULL)
09756a85
RS
10107 {
10108 queue_tmp->event = *event;
10109 queue_tmp->next = queue;
10110 queue = queue_tmp;
10111 }
10112}
10113
10114/* Take all the queued events and put them back
10115 so that they get processed afresh. */
10116
10117static void
db3906fd
RS
10118x_unqueue_events (display)
10119 Display *display;
09756a85 10120{
58769bee 10121 while (queue != NULL)
09756a85
RS
10122 {
10123 struct selection_event_queue *queue_tmp = queue;
db3906fd 10124 XPutBackEvent (display, &queue_tmp->event);
09756a85 10125 queue = queue_tmp->next;
06a2c219 10126 xfree ((char *)queue_tmp);
09756a85
RS
10127 }
10128}
10129
10130/* Start queuing SelectionRequest events. */
10131
10132void
db3906fd
RS
10133x_start_queuing_selection_requests (display)
10134 Display *display;
09756a85
RS
10135{
10136 x_queue_selection_requests++;
10137}
10138
10139/* Stop queuing SelectionRequest events. */
10140
10141void
db3906fd
RS
10142x_stop_queuing_selection_requests (display)
10143 Display *display;
09756a85
RS
10144{
10145 x_queue_selection_requests--;
db3906fd 10146 x_unqueue_events (display);
09756a85 10147}
f451eb13
JB
10148\f
10149/* The main X event-reading loop - XTread_socket. */
dc6f92b8 10150
042c33d3 10151#if 0
06a2c219 10152/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
10153 but we have to put it out here, since static variables within functions
10154 sometimes don't work. */
06a2c219 10155
dc6f92b8 10156static Time enter_timestamp;
042c33d3 10157#endif
dc6f92b8 10158
11edeb03 10159/* This holds the state XLookupString needs to implement dead keys
58769bee 10160 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
10161 says that a portable program can't use this, but Stephen Gildea assures
10162 me that letting the compiler initialize it to zeros will work okay.
10163
10164 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 10165 given for enter_timestamp, above. */
06a2c219 10166
11edeb03
JB
10167static XComposeStatus compose_status;
10168
10e6549c
RS
10169/* Record the last 100 characters stored
10170 to help debug the loss-of-chars-during-GC problem. */
06a2c219 10171
2224b905
RS
10172static int temp_index;
10173static short temp_buffer[100];
10e6549c 10174
7a13e894
RS
10175/* Set this to nonzero to fake an "X I/O error"
10176 on a particular display. */
06a2c219 10177
7a13e894
RS
10178struct x_display_info *XTread_socket_fake_io_error;
10179
2224b905
RS
10180/* When we find no input here, we occasionally do a no-op command
10181 to verify that the X server is still running and we can still talk with it.
10182 We try all the open displays, one by one.
10183 This variable is used for cycling thru the displays. */
06a2c219 10184
2224b905
RS
10185static struct x_display_info *next_noop_dpyinfo;
10186
06a2c219
GM
10187#define SET_SAVED_MENU_EVENT(size) \
10188 do \
10189 { \
10190 if (f->output_data.x->saved_menu_event == 0) \
10191 f->output_data.x->saved_menu_event \
10192 = (XEvent *) xmalloc (sizeof (XEvent)); \
10193 bcopy (&event, f->output_data.x->saved_menu_event, size); \
10194 if (numchars >= 1) \
10195 { \
3b8f9651 10196 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
06a2c219 10197 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 10198 bufp->arg = Qnil; \
06a2c219
GM
10199 bufp++; \
10200 count++; \
10201 numchars--; \
10202 } \
10203 } \
10204 while (0)
10205
8805890a 10206#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 10207#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 10208
dc6f92b8 10209
bf338245
JD
10210enum
10211{
10212 X_EVENT_NORMAL,
10213 X_EVENT_GOTO_OUT,
10214 X_EVENT_DROP
10215};
dc6f92b8 10216
1fcfb866
JD
10217/* Filter events for the current X input method.
10218 DPYINFO is the display this event is for.
10219 EVENT is the X event to filter.
10220
10221 Returns non-zero if the event was filtered, caller shall not process
10222 this event further.
10223 Returns zero if event is wasn't filtered. */
177c0ea7 10224
1fcfb866
JD
10225#ifdef HAVE_X_I18N
10226static int
10227x_filter_event (dpyinfo, event)
10228 struct x_display_info *dpyinfo;
10229 XEvent *event;
10230{
10231 /* XFilterEvent returns non-zero if the input method has
10232 consumed the event. We pass the frame's X window to
10233 XFilterEvent because that's the one for which the IC
10234 was created. */
10235
10236 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10237 event->xclient.window);
10238
10239 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
10240}
10241#endif
10242
488dd4c4
JD
10243#ifdef USE_GTK
10244static struct x_display_info *current_dpyinfo;
10245static struct input_event **current_bufp;
10246static int *current_numcharsp;
10247static int current_count;
10248static int current_finish;
10249
10250/* This is the filter function invoked by the GTK event loop.
10251 It is invoked before the XEvent is translated to a GdkEvent,
10252 so we have a chanse to act on the event before GTK. */
10253static GdkFilterReturn
10254event_handler_gdk (gxev, ev, data)
10255 GdkXEvent *gxev;
10256 GdkEvent *ev;
10257 gpointer data;
10258{
10259 XEvent *xev = (XEvent*)gxev;
10260
10261 if (current_numcharsp)
1fcfb866
JD
10262 {
10263#ifdef HAVE_X_I18N
10264 /* Filter events for the current X input method.
10265 GTK calls XFilterEvent but not for key press and release,
10266 so we do it here. */
10267 if (xev->type == KeyPress || xev->type == KeyRelease)
10268 if (x_filter_event (current_dpyinfo, xev))
10269 return GDK_FILTER_REMOVE;
10270#endif
10271 current_count += handle_one_xevent (current_dpyinfo,
10272 xev,
10273 current_bufp,
10274 current_numcharsp,
10275 &current_finish);
10276 }
488dd4c4 10277 else
1fcfb866 10278 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
488dd4c4
JD
10279
10280 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
10281 return GDK_FILTER_REMOVE;
10282
10283 return GDK_FILTER_CONTINUE;
10284}
10285#endif /* USE_GTK */
10286
10287
bf338245 10288/* Handles the XEvent EVENT on display DPYINFO.
177c0ea7 10289
bf338245
JD
10290 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
10291 *FINISH is zero if caller should continue reading events.
10292 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
10293
10294 Events representing keys are stored in buffer *BUFP_R,
10295 which can hold up to *NUMCHARSP characters.
10296 We return the number of characters stored into the buffer. */
177c0ea7 10297
6f2a7a68 10298static int
bf338245
JD
10299handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
10300 struct x_display_info *dpyinfo;
10301 XEvent *eventp;
10302 /* register */ struct input_event **bufp_r;
10303 /* register */ int *numcharsp;
10304 int *finish;
dc6f92b8
JB
10305{
10306 int count = 0;
10307 int nbytes = 0;
f676886a 10308 struct frame *f;
379b5ac0 10309 struct coding_system coding;
bf338245
JD
10310 struct input_event *bufp = *bufp_r;
10311 int numchars = *numcharsp;
10312 XEvent event = *eventp;
dc6f92b8 10313
bf338245 10314 *finish = X_EVENT_NORMAL;
177c0ea7 10315
bf338245 10316 switch (event.type)
7a13e894 10317 {
bf338245
JD
10318 case ClientMessage:
10319 {
10320 if (event.xclient.message_type
10321 == dpyinfo->Xatom_wm_protocols
10322 && event.xclient.format == 32)
10323 {
10324 if (event.xclient.data.l[0]
10325 == dpyinfo->Xatom_wm_take_focus)
10326 {
10327 /* Use x_any_window_to_frame because this
10328 could be the shell widget window
10329 if the frame has no title bar. */
10330 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5 10331#ifdef HAVE_X_I18N
bf338245
JD
10332 /* Not quite sure this is needed -pd */
10333 if (f && FRAME_XIC (f))
10334 XSetICFocus (FRAME_XIC (f));
6c183ba5 10335#endif
f1da8f06
GM
10336#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10337 instructs the WM to set the input focus automatically for
10338 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10339 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10340 it has set the focus. So, XSetInputFocus below is not
10341 needed.
10342
10343 The call to XSetInputFocus below has also caused trouble. In
10344 cases where the XSetInputFocus done by the WM and the one
10345 below are temporally close (on a fast machine), the call
10346 below can generate additional FocusIn events which confuse
10347 Emacs. */
7d0393cf 10348
bf338245
JD
10349 /* Since we set WM_TAKE_FOCUS, we must call
10350 XSetInputFocus explicitly. But not if f is null,
10351 since that might be an event for a deleted frame. */
10352 if (f)
10353 {
10354 Display *d = event.xclient.display;
10355 /* Catch and ignore errors, in case window has been
10356 iconified by a window manager such as GWM. */
10357 int count = x_catch_errors (d);
10358 XSetInputFocus (d, event.xclient.window,
10359 /* The ICCCM says this is
10360 the only valid choice. */
10361 RevertToParent,
10362 event.xclient.data.l[1]);
10363 /* This is needed to detect the error
10364 if there is an error. */
10365 XSync (d, False);
10366 x_uncatch_errors (d, count);
10367 }
10368 /* Not certain about handling scroll bars here */
f1da8f06 10369#endif /* 0 */
bf338245
JD
10370 }
10371 else if (event.xclient.data.l[0]
10372 == dpyinfo->Xatom_wm_save_yourself)
10373 {
10374 /* Save state modify the WM_COMMAND property to
10375 something which can reinstate us. This notifies
10376 the session manager, who's looking for such a
10377 PropertyNotify. Can restart processing when
10378 a keyboard or mouse event arrives. */
10379 /* If we have a session manager, don't set this.
10380 KDE will then start two Emacsen, one for the
10381 session manager and one for this. */
10382 if (numchars > 0
5f30b957 10383#ifdef HAVE_X_SM
bf338245 10384 && ! x_session_have_connection ()
5f30b957 10385#endif
bf338245
JD
10386 )
10387 {
10388 f = x_top_window_to_frame (dpyinfo,
10389 event.xclient.window);
10390 /* This is just so we only give real data once
10391 for a single Emacs process. */
10392 if (f == SELECTED_FRAME ())
10393 XSetCommand (FRAME_X_DISPLAY (f),
10394 event.xclient.window,
10395 initial_argv, initial_argc);
10396 else if (f)
10397 XSetCommand (FRAME_X_DISPLAY (f),
10398 event.xclient.window,
10399 0, 0);
10400 }
10401 }
10402 else if (event.xclient.data.l[0]
10403 == dpyinfo->Xatom_wm_delete_window)
10404 {
10405 struct frame *f
10406 = x_any_window_to_frame (dpyinfo,
10407 event.xclient.window);
7a13e894 10408
bf338245
JD
10409 if (f)
10410 {
10411 if (numchars == 0)
10412 abort ();
58769bee 10413
bf338245
JD
10414 bufp->kind = DELETE_WINDOW_EVENT;
10415 XSETFRAME (bufp->frame_or_window, f);
10416 bufp->arg = Qnil;
10417 bufp++;
1fb20991 10418
bf338245
JD
10419 count += 1;
10420 numchars -= 1;
10421 }
10422 else
10423 goto OTHER; /* May be a dialog that is to be removed */
10424 }
10425 }
10426 else if (event.xclient.message_type
10427 == dpyinfo->Xatom_wm_configure_denied)
10428 {
10429 }
10430 else if (event.xclient.message_type
10431 == dpyinfo->Xatom_wm_window_moved)
10432 {
10433 int new_x, new_y;
10434 struct frame *f
10435 = x_window_to_frame (dpyinfo, event.xclient.window);
10436
10437 new_x = event.xclient.data.s[0];
10438 new_y = event.xclient.data.s[1];
10439
10440 if (f)
10441 {
10442 f->output_data.x->left_pos = new_x;
10443 f->output_data.x->top_pos = new_y;
10444 }
10445 }
0fdff6bb 10446#ifdef HACK_EDITRES
bf338245
JD
10447 else if (event.xclient.message_type
10448 == dpyinfo->Xatom_editres)
10449 {
10450 struct frame *f
10451 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10452 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10453 &event, NULL);
10454 }
0fdff6bb 10455#endif /* HACK_EDITRES */
bf338245
JD
10456 else if ((event.xclient.message_type
10457 == dpyinfo->Xatom_DONE)
10458 || (event.xclient.message_type
10459 == dpyinfo->Xatom_PAGE))
10460 {
10461 /* Ghostview job completed. Kill it. We could
10462 reply with "Next" if we received "Page", but we
10463 currently never do because we are interested in
10464 images, only, which should have 1 page. */
10465 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10466 struct frame *f
10467 = x_window_to_frame (dpyinfo, event.xclient.window);
10468 x_kill_gs_process (pixmap, f);
10469 expose_frame (f, 0, 0, 0, 0);
10470 }
06a2c219 10471#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
10472 /* Scroll bar callbacks send a ClientMessage from which
10473 we construct an input_event. */
10474 else if (event.xclient.message_type
10475 == dpyinfo->Xatom_Scrollbar)
10476 {
10477 x_scroll_bar_to_input_event (&event, bufp);
10478 ++bufp, ++count, --numchars;
10479 goto out;
10480 }
06a2c219 10481#endif /* USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
10482 else
10483 goto OTHER;
10484 }
10485 break;
dc6f92b8 10486
bf338245 10487 case SelectionNotify:
3afe33e7 10488#ifdef USE_X_TOOLKIT
bf338245
JD
10489 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10490 goto OTHER;
3afe33e7 10491#endif /* not USE_X_TOOLKIT */
bf338245
JD
10492 x_handle_selection_notify (&event.xselection);
10493 break;
d56a553a 10494
bf338245 10495 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 10496#ifdef USE_X_TOOLKIT
bf338245
JD
10497 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10498 goto OTHER;
3afe33e7 10499#endif /* USE_X_TOOLKIT */
bf338245
JD
10500 {
10501 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 10502
bf338245
JD
10503 if (numchars == 0)
10504 abort ();
d56a553a 10505
bf338245
JD
10506 bufp->kind = SELECTION_CLEAR_EVENT;
10507 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10508 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10509 SELECTION_EVENT_TIME (bufp) = eventp->time;
10510 bufp->frame_or_window = Qnil;
10511 bufp->arg = Qnil;
10512 bufp++;
d56a553a 10513
bf338245
JD
10514 count += 1;
10515 numchars -= 1;
10516 }
10517 break;
dc6f92b8 10518
bf338245 10519 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 10520#ifdef USE_X_TOOLKIT
bf338245
JD
10521 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10522 goto OTHER;
3afe33e7 10523#endif /* USE_X_TOOLKIT */
bf338245
JD
10524 if (x_queue_selection_requests)
10525 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10526 &event);
10527 else
10528 {
10529 XSelectionRequestEvent *eventp
10530 = (XSelectionRequestEvent *) &event;
10531
10532 if (numchars == 0)
10533 abort ();
10534
10535 bufp->kind = SELECTION_REQUEST_EVENT;
10536 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10537 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10538 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10539 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10540 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10541 SELECTION_EVENT_TIME (bufp) = eventp->time;
10542 bufp->frame_or_window = Qnil;
10543 bufp->arg = Qnil;
10544 bufp++;
10545
10546 count += 1;
10547 numchars -= 1;
10548 }
10549 break;
7a13e894 10550
bf338245 10551 case PropertyNotify:
1d2b2268
GM
10552#if 0 /* This is plain wrong. In the case that we are waiting for a
10553 PropertyNotify used as an ACK in incremental selection
10554 transfer, the property will be on the receiver's window. */
10555#if defined USE_X_TOOLKIT
bf338245
JD
10556 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10557 goto OTHER;
1d2b2268
GM
10558#endif
10559#endif
bf338245
JD
10560 x_handle_property_notify (&event.xproperty);
10561 goto OTHER;
dc6f92b8 10562
bf338245
JD
10563 case ReparentNotify:
10564 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10565 if (f)
10566 {
10567 int x, y;
10568 f->output_data.x->parent_desc = event.xreparent.parent;
10569 x_real_positions (f, &x, &y);
10570 f->output_data.x->left_pos = x;
10571 f->output_data.x->top_pos = y;
10572 goto OTHER;
10573 }
10574 break;
3bd330d4 10575
bf338245
JD
10576 case Expose:
10577 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10578 if (f)
10579 {
10580 x_check_fullscreen (f);
d0fa3e56 10581
bf338245
JD
10582 if (f->async_visible == 0)
10583 {
10584 f->async_visible = 1;
10585 f->async_iconified = 0;
10586 f->output_data.x->has_been_visible = 1;
10587 SET_FRAME_GARBAGED (f);
10588 }
10589 else
10590 expose_frame (x_window_to_frame (dpyinfo,
10591 event.xexpose.window),
10592 event.xexpose.x, event.xexpose.y,
10593 event.xexpose.width, event.xexpose.height);
10594 }
10595 else
10596 {
12949a7f 10597#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245 10598 struct scroll_bar *bar;
12949a7f 10599#endif
01f67d2c 10600#if defined USE_LUCID
bf338245
JD
10601 /* Submenus of the Lucid menu bar aren't widgets
10602 themselves, so there's no way to dispatch events
10603 to them. Recognize this case separately. */
10604 {
10605 Widget widget
10606 = x_window_to_menu_bar (event.xexpose.window);
10607 if (widget)
10608 xlwmenu_redisplay (widget);
10609 }
01f67d2c
PJ
10610#endif /* USE_LUCID */
10611
06a2c219 10612#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
10613 /* Dispatch event to the widget. */
10614 goto OTHER;
06a2c219 10615#else /* not USE_TOOLKIT_SCROLL_BARS */
bf338245 10616 bar = x_window_to_scroll_bar (event.xexpose.window);
58769bee 10617
bf338245
JD
10618 if (bar)
10619 x_scroll_bar_expose (bar, &event);
3afe33e7 10620#ifdef USE_X_TOOLKIT
bf338245
JD
10621 else
10622 goto OTHER;
3afe33e7 10623#endif /* USE_X_TOOLKIT */
06a2c219 10624#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
10625 }
10626 break;
dc6f92b8 10627
bf338245
JD
10628 case GraphicsExpose: /* This occurs when an XCopyArea's
10629 source area was obscured or not
10630 available. */
10631 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10632 if (f)
10633 {
10634 expose_frame (f,
10635 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10636 event.xgraphicsexpose.width,
10637 event.xgraphicsexpose.height);
10638 }
3afe33e7 10639#ifdef USE_X_TOOLKIT
bf338245
JD
10640 else
10641 goto OTHER;
3afe33e7 10642#endif /* USE_X_TOOLKIT */
bf338245 10643 break;
7d0393cf 10644
bf338245
JD
10645 case NoExpose: /* This occurs when an XCopyArea's
10646 source area was completely
10647 available. */
10648 break;
dc6f92b8 10649
bf338245
JD
10650 case UnmapNotify:
10651 /* Redo the mouse-highlight after the tooltip has gone. */
10652 if (event.xmap.window == tip_window)
10653 {
10654 tip_window = 0;
10655 redo_mouse_highlight ();
10656 }
bddd097c 10657
bf338245
JD
10658 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10659 if (f) /* F may no longer exist if
10660 the frame was deleted. */
10661 {
10662 /* While a frame is unmapped, display generation is
10663 disabled; you don't want to spend time updating a
10664 display that won't ever be seen. */
10665 f->async_visible = 0;
10666 /* We can't distinguish, from the event, whether the window
10667 has become iconified or invisible. So assume, if it
10668 was previously visible, than now it is iconified.
10669 But x_make_frame_invisible clears both
10670 the visible flag and the iconified flag;
10671 and that way, we know the window is not iconified now. */
10672 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10673 {
10674 f->async_iconified = 1;
dc6f92b8 10675
bf338245
JD
10676 bufp->kind = ICONIFY_EVENT;
10677 XSETFRAME (bufp->frame_or_window, f);
10678 bufp->arg = Qnil;
10679 bufp++;
10680 count++;
10681 numchars--;
10682 }
10683 }
10684 goto OTHER;
10685
10686 case MapNotify:
10687 if (event.xmap.window == tip_window)
10688 /* The tooltip has been drawn already. Avoid
10689 the SET_FRAME_GARBAGED below. */
10690 goto OTHER;
10691
10692 /* We use x_top_window_to_frame because map events can
10693 come for sub-windows and they don't mean that the
10694 frame is visible. */
10695 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10696 if (f)
10697 {
10698 /* wait_reading_process_input will notice this and update
10699 the frame's display structures.
10700 If we where iconified, we should not set garbaged,
10701 because that stops redrawing on Expose events. This looks
10702 bad if we are called from a recursive event loop
10703 (x_dispatch_event), for example when a dialog is up. */
10704 if (! f->async_iconified)
10705 SET_FRAME_GARBAGED (f);
10706
10707 f->async_visible = 1;
10708 f->async_iconified = 0;
10709 f->output_data.x->has_been_visible = 1;
10710
10711 if (f->iconified)
10712 {
10713 bufp->kind = DEICONIFY_EVENT;
10714 XSETFRAME (bufp->frame_or_window, f);
10715 bufp->arg = Qnil;
10716 bufp++;
10717 count++;
10718 numchars--;
10719 }
10720 else if (! NILP (Vframe_list)
10721 && ! NILP (XCDR (Vframe_list)))
10722 /* Force a redisplay sooner or later
10723 to update the frame titles
10724 in case this is the second frame. */
10725 record_asynch_buffer_change ();
10726 }
10727 goto OTHER;
2a793c0c 10728
bf338245 10729 case KeyPress:
2a793c0c 10730
22174d10 10731#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245 10732 /* Dispatch KeyPress events when in menu. */
488dd4c4 10733 if (popup_activated ())
bf338245 10734 goto OTHER;
22174d10 10735#endif
488dd4c4 10736
bf338245 10737 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 10738
bf338245
JD
10739 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10740 {
10741 dpyinfo->mouse_face_hidden = 1;
10742 clear_mouse_face (dpyinfo);
10743 }
66301061 10744
eccc05db 10745#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
bf338245
JD
10746 if (f == 0)
10747 {
10748 /* Scroll bars consume key events, but we want
10749 the keys to go to the scroll bar's frame. */
10750 Widget widget = XtWindowToWidget (dpyinfo->display,
10751 event.xkey.window);
10752 if (widget && XmIsScrollBar (widget))
10753 {
10754 widget = XtParent (widget);
10755 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10756 }
10757 }
eccc05db 10758#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 10759
bf338245
JD
10760 if (f != 0)
10761 {
10762 KeySym keysym, orig_keysym;
10763 /* al%imercury@uunet.uu.net says that making this 81
10764 instead of 80 fixed a bug whereby meta chars made
10765 his Emacs hang.
10766
10767 It seems that some version of XmbLookupString has
10768 a bug of not returning XBufferOverflow in
10769 status_return even if the input is too long to
10770 fit in 81 bytes. So, we must prepare sufficient
10771 bytes for copy_buffer. 513 bytes (256 chars for
10772 two-byte character set) seems to be a fairly good
10773 approximation. -- 2000.8.10 handa@etl.go.jp */
10774 unsigned char copy_buffer[513];
10775 unsigned char *copy_bufptr = copy_buffer;
10776 int copy_bufsiz = sizeof (copy_buffer);
10777 int modifiers;
10778 Lisp_Object coding_system = Qlatin_1;
10779
10780 event.xkey.state
10781 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10782 extra_keyboard_modifiers);
10783 modifiers = event.xkey.state;
10784
10785 /* This will have to go some day... */
10786
10787 /* make_lispy_event turns chars into control chars.
10788 Don't do it here because XLookupString is too eager. */
10789 event.xkey.state &= ~ControlMask;
10790 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10791 | dpyinfo->super_mod_mask
10792 | dpyinfo->hyper_mod_mask
10793 | dpyinfo->alt_mod_mask);
10794
10795 /* In case Meta is ComposeCharacter,
10796 clear its status. According to Markus Ehrnsperger
10797 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10798 this enables ComposeCharacter to work whether or
10799 not it is combined with Meta. */
10800 if (modifiers & dpyinfo->meta_mod_mask)
10801 bzero (&compose_status, sizeof (compose_status));
1cf4a0d1 10802
6c183ba5 10803#ifdef HAVE_X_I18N
bf338245
JD
10804 if (FRAME_XIC (f))
10805 {
10806 Status status_return;
10807
10808 coding_system = Vlocale_coding_system;
10809 nbytes = XmbLookupString (FRAME_XIC (f),
10810 &event.xkey, copy_bufptr,
10811 copy_bufsiz, &keysym,
10812 &status_return);
10813 if (status_return == XBufferOverflow)
10814 {
10815 copy_bufsiz = nbytes + 1;
10816 copy_bufptr = (char *) alloca (copy_bufsiz);
10817 nbytes = XmbLookupString (FRAME_XIC (f),
10818 &event.xkey, copy_bufptr,
10819 copy_bufsiz, &keysym,
10820 &status_return);
10821 }
10822 /* Xutf8LookupString is a new but already deprecated interface. -stef */
96035dca 10823#if 0 && defined X_HAVE_UTF8_STRING
bf338245
JD
10824 else if (status_return == XLookupKeySym)
10825 { /* Try again but with utf-8. */
10826 coding_system = Qutf_8;
10827 nbytes = Xutf8LookupString (FRAME_XIC (f),
10828 &event.xkey, copy_bufptr,
10829 copy_bufsiz, &keysym,
10830 &status_return);
10831 if (status_return == XBufferOverflow)
10832 {
10833 copy_bufsiz = nbytes + 1;
10834 copy_bufptr = (char *) alloca (copy_bufsiz);
10835 nbytes = Xutf8LookupString (FRAME_XIC (f),
10836 &event.xkey,
10837 copy_bufptr,
10838 copy_bufsiz, &keysym,
10839 &status_return);
10840 }
10841 }
c997eae5 10842#endif
f5d11644 10843
bf338245
JD
10844 if (status_return == XLookupNone)
10845 break;
10846 else if (status_return == XLookupChars)
10847 {
10848 keysym = NoSymbol;
10849 modifiers = 0;
10850 }
10851 else if (status_return != XLookupKeySym
10852 && status_return != XLookupBoth)
10853 abort ();
10854 }
10855 else
10856 nbytes = XLookupString (&event.xkey, copy_bufptr,
10857 copy_bufsiz, &keysym,
10858 &compose_status);
6c183ba5 10859#else
bf338245
JD
10860 nbytes = XLookupString (&event.xkey, copy_bufptr,
10861 copy_bufsiz, &keysym,
10862 &compose_status);
6c183ba5 10863#endif
dc6f92b8 10864
bf338245 10865 orig_keysym = keysym;
55123275 10866
bf338245
JD
10867 if (numchars > 1)
10868 {
10869 Lisp_Object c;
10870
10871 /* First deal with keysyms which have defined
10872 translations to characters. */
10873 if (keysym >= 32 && keysym < 128)
10874 /* Avoid explicitly decoding each ASCII character. */
10875 {
10876 bufp->kind = ASCII_KEYSTROKE_EVENT;
10877 bufp->code = keysym;
10878 XSETFRAME (bufp->frame_or_window, f);
10879 bufp->arg = Qnil;
10880 bufp->modifiers
10881 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10882 modifiers);
10883 bufp->timestamp = event.xkey.time;
10884 bufp++;
10885 count++;
10886 numchars--;
10887 }
10888 /* Now non-ASCII. */
10889 else if (HASH_TABLE_P (Vx_keysym_table)
10890 && (NATNUMP (c = Fgethash (make_number (keysym),
10891 Vx_keysym_table,
10892 Qnil))))
10893 {
10894 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
10895 ? ASCII_KEYSTROKE_EVENT
10896 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
10897 bufp->code = XFASTINT (c);
10898 XSETFRAME (bufp->frame_or_window, f);
10899 bufp->arg = Qnil;
10900 bufp->modifiers
10901 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10902 modifiers);
10903 bufp->timestamp = event.xkey.time;
10904 bufp++;
10905 count++;
10906 numchars--;
10907 }
10908 /* Random non-modifier sorts of keysyms. */
10909 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10910 || keysym == XK_Delete
1097aea0 10911#ifdef XK_ISO_Left_Tab
bf338245
JD
10912 || (keysym >= XK_ISO_Left_Tab
10913 && keysym <= XK_ISO_Enter)
1097aea0 10914#endif
bf338245
JD
10915 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10916 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 10917#ifdef HPUX
bf338245
JD
10918 /* This recognizes the "extended function
10919 keys". It seems there's no cleaner way.
10920 Test IsModifierKey to avoid handling
10921 mode_switch incorrectly. */
10922 || ((unsigned) (keysym) >= XK_Select
10923 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
10924#endif
10925#ifdef XK_dead_circumflex
bf338245 10926 || orig_keysym == XK_dead_circumflex
69388238
RS
10927#endif
10928#ifdef XK_dead_grave
bf338245 10929 || orig_keysym == XK_dead_grave
69388238
RS
10930#endif
10931#ifdef XK_dead_tilde
bf338245 10932 || orig_keysym == XK_dead_tilde
69388238
RS
10933#endif
10934#ifdef XK_dead_diaeresis
bf338245 10935 || orig_keysym == XK_dead_diaeresis
69388238
RS
10936#endif
10937#ifdef XK_dead_macron
bf338245 10938 || orig_keysym == XK_dead_macron
69388238
RS
10939#endif
10940#ifdef XK_dead_degree
bf338245 10941 || orig_keysym == XK_dead_degree
69388238
RS
10942#endif
10943#ifdef XK_dead_acute
bf338245 10944 || orig_keysym == XK_dead_acute
69388238
RS
10945#endif
10946#ifdef XK_dead_cedilla
bf338245 10947 || orig_keysym == XK_dead_cedilla
69388238
RS
10948#endif
10949#ifdef XK_dead_breve
bf338245 10950 || orig_keysym == XK_dead_breve
69388238
RS
10951#endif
10952#ifdef XK_dead_ogonek
bf338245 10953 || orig_keysym == XK_dead_ogonek
69388238
RS
10954#endif
10955#ifdef XK_dead_caron
bf338245 10956 || orig_keysym == XK_dead_caron
69388238
RS
10957#endif
10958#ifdef XK_dead_doubleacute
bf338245 10959 || orig_keysym == XK_dead_doubleacute
69388238
RS
10960#endif
10961#ifdef XK_dead_abovedot
bf338245 10962 || orig_keysym == XK_dead_abovedot
c34790e0 10963#endif
bf338245
JD
10964 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10965 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10966 /* Any "vendor-specific" key is ok. */
10967 || (orig_keysym & (1 << 28))
10968 || (keysym != NoSymbol && nbytes == 0))
10969 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
10970#ifndef HAVE_X11R5
10971#ifdef XK_Mode_switch
bf338245 10972 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
10973#endif
10974#ifdef XK_Num_Lock
bf338245 10975 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
10976#endif
10977#endif /* not HAVE_X11R5 */
bf338245
JD
10978 /* The symbols from XK_ISO_Lock
10979 to XK_ISO_Last_Group_Lock
10980 don't have real modifiers but
10981 should be treated similarly to
10982 Mode_switch by Emacs. */
7ef18d79 10983#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
bf338245
JD
10984 || ((unsigned)(orig_keysym)
10985 >= XK_ISO_Lock
10986 && (unsigned)(orig_keysym)
10987 <= XK_ISO_Last_Group_Lock)
7ef18d79 10988#endif
bf338245
JD
10989 ))
10990 {
10991 if (temp_index == sizeof temp_buffer / sizeof (short))
10992 temp_index = 0;
10993 temp_buffer[temp_index++] = keysym;
10994 /* make_lispy_event will convert this to a symbolic
10995 key. */
10996 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
10997 bufp->code = keysym;
10998 XSETFRAME (bufp->frame_or_window, f);
10999 bufp->arg = Qnil;
11000 bufp->modifiers
11001 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
11002 modifiers);
11003 bufp->timestamp = event.xkey.time;
11004 bufp++;
11005 count++;
11006 numchars--;
11007 }
11008 else if (numchars > nbytes)
11009 { /* Raw bytes, not keysym. */
11010 register int i;
11011 register int c;
11012 int nchars, len;
11013
11014 /* The input should be decoded with `coding_system'
11015 which depends on which X*LookupString function
11016 we used just above and the locale. */
11017 setup_coding_system (coding_system, &coding);
11018 coding.src_multibyte = 0;
11019 coding.dst_multibyte = 1;
11020 /* The input is converted to events, thus we can't
11021 handle composition. Anyway, there's no XIM that
11022 gives us composition information. */
11023 coding.composing = COMPOSITION_DISABLED;
11024
11025 for (i = 0; i < nbytes; i++)
11026 {
11027 if (temp_index == (sizeof temp_buffer
11028 / sizeof (short)))
11029 temp_index = 0;
11030 temp_buffer[temp_index++] = copy_bufptr[i];
11031 }
379b5ac0 11032
bf338245
JD
11033 {
11034 /* Decode the input data. */
11035 int require;
11036 unsigned char *p;
11037
11038 require = decoding_buffer_size (&coding, nbytes);
11039 p = (unsigned char *) alloca (require);
11040 coding.mode |= CODING_MODE_LAST_BLOCK;
11041 /* We explicitely disable composition
11042 handling because key data should
11043 not contain any composition
11044 sequence. */
11045 coding.composing = COMPOSITION_DISABLED;
11046 decode_coding (&coding, copy_bufptr, p,
11047 nbytes, require);
11048 nbytes = coding.produced;
11049 nchars = coding.produced_char;
11050 copy_bufptr = p;
11051 }
379b5ac0 11052
bf338245
JD
11053 /* Convert the input data to a sequence of
11054 character events. */
11055 for (i = 0; i < nbytes; i += len)
11056 {
11057 if (nchars == nbytes)
11058 c = copy_bufptr[i], len = 1;
11059 else
11060 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
11061 nbytes - i, len);
11062
11063 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
11064 ? ASCII_KEYSTROKE_EVENT
11065 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
11066 bufp->code = c;
11067 XSETFRAME (bufp->frame_or_window, f);
11068 bufp->arg = Qnil;
11069 bufp->modifiers
11070 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
11071 modifiers);
11072 bufp->timestamp = event.xkey.time;
11073 bufp++;
11074 }
11075
11076 count += nchars;
11077 numchars -= nchars;
11078
11079 if (keysym == NoSymbol)
11080 break;
11081 }
11082 else
11083 abort ();
11084 }
11085 else
11086 abort ();
11087 }
59ddecde 11088#ifdef HAVE_X_I18N
bf338245
JD
11089 /* Don't dispatch this event since XtDispatchEvent calls
11090 XFilterEvent, and two calls in a row may freeze the
11091 client. */
11092 break;
59ddecde 11093#else
bf338245 11094 goto OTHER;
59ddecde 11095#endif
f451eb13 11096
bf338245 11097 case KeyRelease:
59ddecde 11098#ifdef HAVE_X_I18N
bf338245
JD
11099 /* Don't dispatch this event since XtDispatchEvent calls
11100 XFilterEvent, and two calls in a row may freeze the
11101 client. */
11102 break;
59ddecde 11103#else
bf338245 11104 goto OTHER;
59ddecde 11105#endif
f5d11644 11106
bf338245
JD
11107 case EnterNotify:
11108 {
11109 int n;
cab34f50 11110
bf338245
JD
11111 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11112 if (n > 0)
11113 {
11114 bufp += n, count += n, numchars -= n;
11115 }
7d0393cf 11116
bf338245 11117 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
06a2c219 11118
2c850e26 11119#if 0
bf338245
JD
11120 if (event.xcrossing.focus)
11121 {
11122 /* Avoid nasty pop/raise loops. */
11123 if (f && (!(f->auto_raise)
11124 || !(f->auto_lower)
11125 || (event.xcrossing.time - enter_timestamp) > 500))
fd42232c 11126 {
bf338245
JD
11127 x_new_focus_frame (dpyinfo, f);
11128 enter_timestamp = event.xcrossing.time;
fd42232c 11129 }
bf338245
JD
11130 }
11131 else if (f == dpyinfo->x_focus_frame)
11132 x_new_focus_frame (dpyinfo, 0);
11133#endif
f9e24cb9 11134
bf338245
JD
11135 /* EnterNotify counts as mouse movement,
11136 so update things that depend on mouse position. */
11137 if (f && !f->output_data.x->hourglass_p)
11138 note_mouse_movement (f, &event.xmotion);
11139 goto OTHER;
11140 }
cab34f50 11141
bf338245
JD
11142 case FocusIn:
11143 {
11144 int n;
cab34f50 11145
bf338245
JD
11146 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11147 if (n > 0)
11148 {
11149 bufp += n, count += n, numchars -= n;
11150 }
11151 }
06a2c219 11152
bf338245 11153 goto OTHER;
7a13e894 11154
bf338245
JD
11155 case LeaveNotify:
11156 {
11157 int n;
dc6f92b8 11158
bf338245
JD
11159 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11160 if (n > 0)
11161 {
11162 bufp += n, count += n, numchars -= n;
11163 }
11164 }
cab34f50 11165
bf338245
JD
11166 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
11167 if (f)
11168 {
11169 if (f == dpyinfo->mouse_face_mouse_frame)
11170 {
11171 /* If we move outside the frame, then we're
11172 certainly no longer on any text in the frame. */
11173 clear_mouse_face (dpyinfo);
11174 dpyinfo->mouse_face_mouse_frame = 0;
11175 }
6c183ba5 11176
bf338245
JD
11177 /* Generate a nil HELP_EVENT to cancel a help-echo.
11178 Do it only if there's something to cancel.
11179 Otherwise, the startup message is cleared when
11180 the mouse leaves the frame. */
11181 if (any_help_event_p)
11182 {
11183 Lisp_Object frame;
11184 int n;
11185
11186 XSETFRAME (frame, f);
11187 help_echo = Qnil;
11188 n = gen_help_event (bufp, numchars,
11189 Qnil, frame, Qnil, Qnil, 0);
11190 bufp += n, count += n, numchars -= n;
11191 }
dc6f92b8 11192
bf338245
JD
11193 }
11194 goto OTHER;
7d0393cf 11195
bf338245
JD
11196 case FocusOut:
11197 {
11198 int n;
06a2c219 11199
bf338245
JD
11200 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11201 if (n > 0)
11202 {
11203 bufp += n, count += n, numchars -= n;
11204 }
11205 }
66301061 11206
bf338245 11207 goto OTHER;
5bc62483 11208
bf338245
JD
11209 case MotionNotify:
11210 {
11211 previous_help_echo = help_echo;
11212 help_echo = help_echo_object = help_echo_window = Qnil;
11213 help_echo_pos = -1;
11214
11215 if (dpyinfo->grabbed && last_mouse_frame
11216 && FRAME_LIVE_P (last_mouse_frame))
11217 f = last_mouse_frame;
11218 else
11219 f = x_window_to_frame (dpyinfo, event.xmotion.window);
11220
11221 if (dpyinfo->mouse_face_hidden)
11222 {
11223 dpyinfo->mouse_face_hidden = 0;
11224 clear_mouse_face (dpyinfo);
11225 }
11226
11227 if (f)
11228 {
11229
11230 /* Generate SELECT_WINDOW_EVENTs when needed. */
11231 if (mouse_autoselect_window)
11232 {
11233 Lisp_Object window;
11234 int area;
11235
11236 window = window_from_coordinates (f,
11237 event.xmotion.x, event.xmotion.y,
11238 &area, 0);
11239
11240 /* Window will be selected only when it is not selected now and
11241 last mouse movement event was not in it. Minibuffer window
11242 will be selected iff it is active. */
11243 if (WINDOWP(window)
11244 && !EQ (window, last_window)
11245 && !EQ (window, selected_window)
11246 && numchars > 0)
11247 {
11248 bufp->kind = SELECT_WINDOW_EVENT;
11249 bufp->frame_or_window = window;
11250 bufp->arg = Qnil;
11251 ++bufp, ++count, --numchars;
11252 }
5bc62483 11253
bf338245
JD
11254 last_window=window;
11255 }
11256 note_mouse_movement (f, &event.xmotion);
11257 }
11258 else
11259 {
e88b3c50 11260#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
11261 struct scroll_bar *bar
11262 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 11263
bf338245
JD
11264 if (bar)
11265 x_scroll_bar_note_movement (bar, &event);
e88b3c50 11266#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 11267
bf338245
JD
11268 /* If we move outside the frame, then we're
11269 certainly no longer on any text in the frame. */
11270 clear_mouse_face (dpyinfo);
11271 }
11272
11273 /* If the contents of the global variable help_echo
11274 has changed, generate a HELP_EVENT. */
11275 if (!NILP (help_echo)
11276 || !NILP (previous_help_echo))
11277 {
11278 Lisp_Object frame;
11279 int n;
11280
11281 if (f)
11282 XSETFRAME (frame, f);
11283 else
11284 frame = Qnil;
11285
11286 any_help_event_p = 1;
11287 n = gen_help_event (bufp, numchars, help_echo, frame,
11288 help_echo_window, help_echo_object,
11289 help_echo_pos);
11290 bufp += n, count += n, numchars -= n;
11291 }
11292
11293 goto OTHER;
11294 }
dc6f92b8 11295
bf338245
JD
11296 case ConfigureNotify:
11297 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
11298 if (f)
11299 {
7a4bce14 11300#ifndef USE_X_TOOLKIT
488dd4c4
JD
11301#ifdef USE_GTK
11302 xg_resize_widgets (f, event.xconfigure.width,
11303 event.xconfigure.height);
11304#else /* not USE_GTK */
bf338245
JD
11305 /* If there is a pending resize for fullscreen, don't
11306 do this one, the right one will come later.
11307 The toolkit version doesn't seem to need this, but we
11308 need to reset it below. */
11309 int dont_resize =
11310 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11311 && FRAME_NEW_WIDTH (f) != 0);
11312 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
11313 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
11314 if (dont_resize)
11315 goto OTHER;
11316
11317 /* In the toolkit version, change_frame_size
11318 is called by the code that handles resizing
11319 of the EmacsFrame widget. */
11320
11321 /* Even if the number of character rows and columns has
11322 not changed, the font size may have changed, so we need
11323 to check the pixel dimensions as well. */
11324 if (columns != f->width
11325 || rows != f->height
11326 || event.xconfigure.width != f->output_data.x->pixel_width
11327 || event.xconfigure.height != f->output_data.x->pixel_height)
11328 {
11329 change_frame_size (f, rows, columns, 0, 1, 0);
11330 SET_FRAME_GARBAGED (f);
11331 cancel_mouse_face (f);
11332 }
488dd4c4 11333#endif /* not USE_GTK */
2d7fc7e8 11334#endif
af395ec1 11335
bf338245
JD
11336 f->output_data.x->pixel_width = event.xconfigure.width;
11337 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894 11338
488dd4c4
JD
11339#ifdef USE_GTK
11340 /* GTK creates windows but doesn't map them.
11341 Only get real positions and check fullscreen when mapped. */
11342 if (FRAME_GTK_OUTER_WIDGET (f)
11343 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
11344 {
11345#endif
bf338245
JD
11346 /* What we have now is the position of Emacs's own window.
11347 Convert that to the position of the window manager window. */
11348 x_real_positions (f, &f->output_data.x->left_pos,
11349 &f->output_data.x->top_pos);
dcb07ae9 11350
488dd4c4 11351 x_check_fullscreen_move (f);
bf338245
JD
11352 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11353 f->output_data.x->want_fullscreen &=
11354 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
488dd4c4
JD
11355#ifdef USE_GTK
11356 }
11357#endif
f5d11644 11358#ifdef HAVE_X_I18N
bf338245
JD
11359 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11360 xic_set_statusarea (f);
f5d11644
GM
11361#endif
11362
bf338245
JD
11363 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11364 {
11365 /* Since the WM decorations come below top_pos now,
11366 we must put them below top_pos in the future. */
11367 f->output_data.x->win_gravity = NorthWestGravity;
11368 x_wm_set_size_hint (f, (long) 0, 0);
11369 }
11370 }
11371 goto OTHER;
dc6f92b8 11372
bf338245 11373 case ButtonRelease:
488dd4c4 11374 case ButtonPress:
bf338245
JD
11375 {
11376 /* If we decide we want to generate an event to be seen
11377 by the rest of Emacs, we put it here. */
11378 struct input_event emacs_event;
11379 int tool_bar_p = 0;
11380
11381 emacs_event.kind = NO_EVENT;
11382 bzero (&compose_status, sizeof (compose_status));
11383
11384 if (dpyinfo->grabbed
11385 && last_mouse_frame
11386 && FRAME_LIVE_P (last_mouse_frame))
11387 f = last_mouse_frame;
11388 else
11389 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11390
11391 if (f)
11392 {
11393 /* Is this in the tool-bar? */
11394 if (WINDOWP (f->tool_bar_window)
11395 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11396 {
11397 Lisp_Object window;
11398 int p, x, y;
06a2c219 11399
bf338245
JD
11400 x = event.xbutton.x;
11401 y = event.xbutton.y;
06a2c219 11402
bf338245
JD
11403 /* Set x and y. */
11404 window = window_from_coordinates (f, x, y, &p, 1);
11405 if (EQ (window, f->tool_bar_window))
11406 {
11407 x_handle_tool_bar_click (f, &event.xbutton);
11408 tool_bar_p = 1;
11409 }
11410 }
06a2c219 11411
bf338245
JD
11412 if (!tool_bar_p)
11413 if (!dpyinfo->x_focus_frame
11414 || f == dpyinfo->x_focus_frame)
488dd4c4 11415 {
22174d10 11416#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
488dd4c4 11417 if (! popup_activated ())
22174d10 11418#endif
488dd4c4
JD
11419 construct_mouse_click (&emacs_event, &event, f);
11420 }
bf338245
JD
11421 }
11422 else
11423 {
06a2c219 11424#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
11425 struct scroll_bar *bar
11426 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 11427
bf338245
JD
11428 if (bar)
11429 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 11430#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
11431 }
11432
11433 if (event.type == ButtonPress)
11434 {
11435 dpyinfo->grabbed |= (1 << event.xbutton.button);
11436 last_mouse_frame = f;
11437 /* Ignore any mouse motion that happened
11438 before this event; any subsequent mouse-movement
11439 Emacs events should reflect only motion after
11440 the ButtonPress. */
11441 if (f != 0)
11442 f->mouse_moved = 0;
11443
11444 if (!tool_bar_p)
11445 last_tool_bar_item = -1;
11446 }
11447 else
488dd4c4 11448 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
bf338245
JD
11449
11450 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
11451 {
11452 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11453 bufp++;
11454 count++;
11455 numchars--;
11456 }
3afe33e7 11457
488dd4c4 11458#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245
JD
11459 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11460 /* For a down-event in the menu bar,
11461 don't pass it to Xt right now.
11462 Instead, save it away
11463 and we will pass it to Xt from kbd_buffer_get_event.
11464 That way, we can run some Lisp code first. */
488dd4c4
JD
11465 if (
11466#ifdef USE_GTK
11467 ! popup_activated ()
11468 &&
11469#endif
11470 f && event.type == ButtonPress
bf338245
JD
11471 /* Verify the event is really within the menu bar
11472 and not just sent to it due to grabbing. */
11473 && event.xbutton.x >= 0
11474 && event.xbutton.x < f->output_data.x->pixel_width
11475 && event.xbutton.y >= 0
11476 && event.xbutton.y < f->output_data.x->menubar_height
11477 && event.xbutton.same_screen)
11478 {
11479 SET_SAVED_BUTTON_EVENT;
11480 XSETFRAME (last_mouse_press_frame, f);
488dd4c4
JD
11481#ifdef USE_GTK
11482 *finish = X_EVENT_DROP;
11483#endif
bf338245
JD
11484 }
11485 else if (event.type == ButtonPress)
11486 {
11487 last_mouse_press_frame = Qnil;
11488 goto OTHER;
11489 }
06a2c219 11490
2237cac9
RS
11491#ifdef USE_MOTIF /* This should do not harm for Lucid,
11492 but I am trying to be cautious. */
bf338245
JD
11493 else if (event.type == ButtonRelease)
11494 {
11495 if (!NILP (last_mouse_press_frame))
11496 {
11497 f = XFRAME (last_mouse_press_frame);
11498 if (f->output_data.x)
11499 SET_SAVED_BUTTON_EVENT;
11500 }
11501 else
11502 goto OTHER;
11503 }
2237cac9 11504#endif /* USE_MOTIF */
bf338245
JD
11505 else
11506 goto OTHER;
488dd4c4 11507#endif /* USE_X_TOOLKIT || USE_GTK */
bf338245
JD
11508 }
11509 break;
dc6f92b8 11510
bf338245
JD
11511 case CirculateNotify:
11512 goto OTHER;
7d0393cf 11513
bf338245
JD
11514 case CirculateRequest:
11515 goto OTHER;
06a2c219 11516
bf338245
JD
11517 case VisibilityNotify:
11518 goto OTHER;
dc6f92b8 11519
bf338245
JD
11520 case MappingNotify:
11521 /* Someone has changed the keyboard mapping - update the
11522 local cache. */
11523 switch (event.xmapping.request)
11524 {
11525 case MappingModifier:
11526 x_find_modifier_meanings (dpyinfo);
11527 /* This is meant to fall through. */
11528 case MappingKeyboard:
11529 XRefreshKeyboardMapping (&event.xmapping);
11530 }
11531 goto OTHER;
dc6f92b8 11532
bf338245
JD
11533 default:
11534 OTHER:
717ca130 11535#ifdef USE_X_TOOLKIT
bf338245
JD
11536 BLOCK_INPUT;
11537 XtDispatchEvent (&event);
11538 UNBLOCK_INPUT;
3afe33e7 11539#endif /* USE_X_TOOLKIT */
bf338245
JD
11540 break;
11541 }
11542
11543 goto ret;
177c0ea7 11544
bf338245
JD
11545 out:
11546 *finish = X_EVENT_GOTO_OUT;
11547
11548 ret:
11549 *bufp_r = bufp;
11550 *numcharsp = numchars;
11551 *eventp = event;
177c0ea7 11552
bf338245
JD
11553 return count;
11554}
11555
11556
11557/* Handles the XEvent EVENT on display DISPLAY.
11558 This is used for event loops outside the normal event handling,
1fcfb866
JD
11559 i.e. looping while a popup menu or a dialog is posted.
11560
11561 Returns the value handle_one_xevent sets in the finish argument. */
11562int
bf338245
JD
11563x_dispatch_event (event, display)
11564 XEvent *event;
11565 Display *display;
11566{
11567 struct x_display_info *dpyinfo;
11568 struct input_event bufp[10];
11569 struct input_event *bufpp = bufp;
11570 int numchars = 10;
1fcfb866 11571 int finish = X_EVENT_NORMAL;
177c0ea7 11572
bf338245
JD
11573 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
11574 if (dpyinfo->display == display)
11575 break;
177c0ea7 11576
bf338245
JD
11577 if (dpyinfo)
11578 {
11579 int i, events;
11580 events = handle_one_xevent (dpyinfo,
11581 event,
11582 &bufpp,
11583 &numchars,
11584 &finish);
11585 for (i = 0; i < events; ++i)
11586 kbd_buffer_store_event (&bufp[i]);
11587 }
1fcfb866
JD
11588
11589 return finish;
bf338245
JD
11590}
11591
11592
11593/* Read events coming from the X server.
11594 This routine is called by the SIGIO handler.
11595 We return as soon as there are no more events to be read.
11596
11597 Events representing keys are stored in buffer BUFP,
11598 which can hold up to NUMCHARS characters.
11599 We return the number of characters stored into the buffer,
11600 thus pretending to be `read'.
11601
11602 EXPECTED is nonzero if the caller knows input is available. */
11603
11604static int
11605XTread_socket (sd, bufp, numchars, expected)
11606 register int sd;
11607 /* register */ struct input_event *bufp;
11608 /* register */ int numchars;
11609 int expected;
11610{
11611 int count = 0;
11612 int nbytes = 0;
11613 XEvent event;
11614 int event_found = 0;
11615 struct x_display_info *dpyinfo;
11616
11617 if (interrupt_input_blocked)
11618 {
11619 interrupt_input_pending = 1;
11620 return -1;
11621 }
11622
11623 interrupt_input_pending = 0;
11624 BLOCK_INPUT;
11625
11626 /* So people can tell when we have read the available input. */
11627 input_signal_count++;
11628
11629 if (numchars <= 0)
11630 abort (); /* Don't think this happens. */
11631
11632 ++handling_signal;
11633
11634 /* Find the display we are supposed to read input for.
11635 It's the one communicating on descriptor SD. */
11636 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
11637 {
11638#if 0 /* This ought to be unnecessary; let's verify it. */
11639#ifdef FIOSNBIO
11640 /* If available, Xlib uses FIOSNBIO to make the socket
11641 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
11642 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
11643 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
11644 fcntl (dpyinfo->connection, F_SETFL, 0);
11645#endif /* ! defined (FIOSNBIO) */
11646#endif
11647
11648#if 0 /* This code can't be made to work, with multiple displays,
11649 and appears not to be used on any system any more.
11650 Also keyboard.c doesn't turn O_NDELAY on and off
11651 for X connections. */
11652#ifndef SIGIO
11653#ifndef HAVE_SELECT
11654 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
11655 {
11656 extern int read_alarm_should_throw;
11657 read_alarm_should_throw = 1;
11658 XPeekEvent (dpyinfo->display, &event);
11659 read_alarm_should_throw = 0;
11660 }
11661#endif /* HAVE_SELECT */
11662#endif /* SIGIO */
11663#endif
11664
11665 /* For debugging, this gives a way to fake an I/O error. */
11666 if (dpyinfo == XTread_socket_fake_io_error)
11667 {
11668 XTread_socket_fake_io_error = 0;
11669 x_io_error_quitter (dpyinfo->display);
dc6f92b8 11670 }
bf338245
JD
11671
11672#ifdef HAVE_X_SM
11673 BLOCK_INPUT;
11674 count += x_session_check_input (bufp, &numchars);
11675 UNBLOCK_INPUT;
11676#endif
11677
488dd4c4
JD
11678#ifdef USE_GTK
11679 /* For GTK we must use the GTK event loop. But XEvents gets passed
11680 to our filter function above, and then to the big event switch.
11681 We use a bunch of globals to communicate with our filter function,
11682 that is kind of ugly, but it works. */
11683 current_dpyinfo = dpyinfo;
177c0ea7 11684
488dd4c4
JD
11685 while (gtk_events_pending ())
11686 {
11687 static int nr = 0;
11688 current_count = count;
11689 current_numcharsp = &numchars;
11690 current_bufp = &bufp;
11691
11692 gtk_main_iteration ();
11693
11694 count = current_count;
11695 current_bufp = 0;
11696 current_numcharsp = 0;
11697
11698 if (current_finish == X_EVENT_GOTO_OUT)
11699 goto out;
11700 }
11701
11702#else /* not USE_GTK */
bf338245
JD
11703 while (XPending (dpyinfo->display))
11704 {
11705 int finish;
177c0ea7 11706
bf338245
JD
11707 XNextEvent (dpyinfo->display, &event);
11708
11709#ifdef HAVE_X_I18N
1fcfb866
JD
11710 /* Filter events for the current X input method. */
11711 if (x_filter_event (dpyinfo, &event))
11712 break;
bf338245
JD
11713#endif
11714 event_found = 1;
11715
11716 count += handle_one_xevent (dpyinfo,
11717 &event,
11718 &bufp,
11719 &numchars,
11720 &finish);
11721
11722 if (finish == X_EVENT_GOTO_OUT)
11723 goto out;
11724 }
488dd4c4 11725#endif /* USE_GTK */
dc6f92b8
JB
11726 }
11727
06a2c219
GM
11728 out:;
11729
9a5196d0
RS
11730 /* On some systems, an X bug causes Emacs to get no more events
11731 when the window is destroyed. Detect that. (1994.) */
58769bee 11732 if (! event_found)
ef2a22d0 11733 {
ef2a22d0
RS
11734 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11735 One XNOOP in 100 loops will make Emacs terminate.
11736 B. Bretthauer, 1994 */
11737 x_noop_count++;
58769bee 11738 if (x_noop_count >= 100)
ef2a22d0
RS
11739 {
11740 x_noop_count=0;
2224b905
RS
11741
11742 if (next_noop_dpyinfo == 0)
11743 next_noop_dpyinfo = x_display_list;
11744
11745 XNoOp (next_noop_dpyinfo->display);
11746
11747 /* Each time we get here, cycle through the displays now open. */
11748 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
11749 }
11750 }
502add23 11751
06a2c219 11752 /* If the focus was just given to an auto-raising frame,
0134a210 11753 raise it now. */
7a13e894 11754 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
11755 if (pending_autoraise_frame)
11756 {
11757 x_raise_frame (pending_autoraise_frame);
11758 pending_autoraise_frame = 0;
11759 }
0134a210 11760
dc6f92b8 11761 UNBLOCK_INPUT;
bde5503b 11762 --handling_signal;
dc6f92b8
JB
11763 return count;
11764}
06a2c219
GM
11765
11766
11767
dc6f92b8 11768\f
06a2c219
GM
11769/***********************************************************************
11770 Text Cursor
11771 ***********************************************************************/
11772
60626bab
GM
11773/* Notice when the text cursor of window W has been completely
11774 overwritten by a drawing operation that outputs glyphs in AREA
11775 starting at X0 and ending at X1 in the line starting at Y0 and
11776 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11777 the rest of the line after X0 has been written. Y coordinates
11778 are window-relative. */
06a2c219
GM
11779
11780static void
60626bab 11781notice_overwritten_cursor (w, area, x0, x1, y0, y1)
06a2c219 11782 struct window *w;
60626bab
GM
11783 enum glyph_row_area area;
11784 int x0, y0, x1, y1;
06a2c219 11785{
b3738089
GM
11786 if (area == TEXT_AREA && w->phys_cursor_on_p)
11787 {
11788 int cx0 = w->phys_cursor.x;
11789 int cx1 = cx0 + w->phys_cursor_width;
11790 int cy0 = w->phys_cursor.y;
11791 int cy1 = cy0 + w->phys_cursor_height;
11792
11793 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11794 {
11795 /* The cursor image will be completely removed from the
11796 screen if the output area intersects the cursor area in
11797 y-direction. When we draw in [y0 y1[, and some part of
11798 the cursor is at y < y0, that part must have been drawn
11799 before. When scrolling, the cursor is erased before
11800 actually scrolling, so we don't come here. When not
11801 scrolling, the rows above the old cursor row must have
11802 changed, and in this case these rows must have written
516074a1
GM
11803 over the cursor image.
11804
11805 Likewise if part of the cursor is below y1, with the
11806 exception of the cursor being in the first blank row at
11807 the buffer and window end because update_text_area
4398e673
GM
11808 doesn't draw that row. (Except when it does, but
11809 that's handled in update_text_area.) */
7d0393cf 11810
f7c5994d 11811 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
516074a1 11812 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
b3738089
GM
11813 w->phys_cursor_on_p = 0;
11814 }
11815 }
06a2c219 11816}
f451eb13
JB
11817
11818
06a2c219
GM
11819/* Set clipping for output in glyph row ROW. W is the window in which
11820 we operate. GC is the graphics context to set clipping in.
11821 WHOLE_LINE_P non-zero means include the areas used for truncation
11822 mark display and alike in the clipping rectangle.
11823
11824 ROW may be a text row or, e.g., a mode line. Text rows must be
11825 clipped to the interior of the window dedicated to text display,
11826 mode lines must be clipped to the whole window. */
dc6f92b8
JB
11827
11828static void
06a2c219
GM
11829x_clip_to_row (w, row, gc, whole_line_p)
11830 struct window *w;
11831 struct glyph_row *row;
11832 GC gc;
11833 int whole_line_p;
dc6f92b8 11834{
06a2c219
GM
11835 struct frame *f = XFRAME (WINDOW_FRAME (w));
11836 XRectangle clip_rect;
11837 int window_x, window_y, window_width, window_height;
dc6f92b8 11838
06a2c219 11839 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 11840
06a2c219
GM
11841 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11842 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11843 clip_rect.y = max (clip_rect.y, window_y);
11844 clip_rect.width = window_width;
11845 clip_rect.height = row->visible_height;
5c1aae96 11846
06a2c219
GM
11847 /* If clipping to the whole line, including trunc marks, extend
11848 the rectangle to the left and increase its width. */
11849 if (whole_line_p)
11850 {
3f332ef3
KS
11851 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11852 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
06a2c219 11853 }
5c1aae96 11854
06a2c219 11855 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
11856}
11857
06a2c219
GM
11858
11859/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
11860
11861static void
06a2c219
GM
11862x_draw_hollow_cursor (w, row)
11863 struct window *w;
11864 struct glyph_row *row;
dc6f92b8 11865{
06a2c219
GM
11866 struct frame *f = XFRAME (WINDOW_FRAME (w));
11867 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11868 Display *dpy = FRAME_X_DISPLAY (f);
11869 int x, y, wd, h;
11870 XGCValues xgcv;
11871 struct glyph *cursor_glyph;
11872 GC gc;
11873
11874 /* Compute frame-relative coordinates from window-relative
11875 coordinates. */
11876 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11877 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11878 + row->ascent - w->phys_cursor_ascent);
11879 h = row->height - 1;
11880
11881 /* Get the glyph the cursor is on. If we can't tell because
11882 the current matrix is invalid or such, give up. */
11883 cursor_glyph = get_phys_cursor_glyph (w);
11884 if (cursor_glyph == NULL)
dc6f92b8
JB
11885 return;
11886
06a2c219
GM
11887 /* Compute the width of the rectangle to draw. If on a stretch
11888 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11889 rectangle as wide as the glyph, but use a canonical character
11890 width instead. */
11891 wd = cursor_glyph->pixel_width - 1;
11892 if (cursor_glyph->type == STRETCH_GLYPH
11893 && !x_stretch_cursor_p)
11894 wd = min (CANON_X_UNIT (f), wd);
b3738089 11895 w->phys_cursor_width = wd;
7d0393cf 11896
06a2c219
GM
11897 /* The foreground of cursor_gc is typically the same as the normal
11898 background color, which can cause the cursor box to be invisible. */
11899 xgcv.foreground = f->output_data.x->cursor_pixel;
11900 if (dpyinfo->scratch_cursor_gc)
11901 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11902 else
11903 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11904 GCForeground, &xgcv);
11905 gc = dpyinfo->scratch_cursor_gc;
11906
11907 /* Set clipping, draw the rectangle, and reset clipping again. */
11908 x_clip_to_row (w, row, gc, 0);
11909 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11910 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
11911}
11912
06a2c219
GM
11913
11914/* Draw a bar cursor on window W in glyph row ROW.
11915
11916 Implementation note: One would like to draw a bar cursor with an
11917 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11918 Unfortunately, I didn't find a font yet that has this property set.
11919 --gerd. */
dc6f92b8
JB
11920
11921static void
65c26775 11922x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
11923 struct window *w;
11924 struct glyph_row *row;
f02d8aa0 11925 int width;
65c26775 11926 enum text_cursor_kinds kind;
dc6f92b8 11927{
92f424df
GM
11928 struct frame *f = XFRAME (w->frame);
11929 struct glyph *cursor_glyph;
7d0393cf 11930
92f424df
GM
11931 /* If cursor is out of bounds, don't draw garbage. This can happen
11932 in mini-buffer windows when switching between echo area glyphs
11933 and mini-buffer. */
11934 cursor_glyph = get_phys_cursor_glyph (w);
11935 if (cursor_glyph == NULL)
11936 return;
06a2c219 11937
92f424df
GM
11938 /* If on an image, draw like a normal cursor. That's usually better
11939 visible than drawing a bar, esp. if the image is large so that
11940 the bar might not be in the window. */
11941 if (cursor_glyph->type == IMAGE_GLYPH)
11942 {
11943 struct glyph_row *row;
11944 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11945 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11946 }
11947 else
11948 {
34e5d0af
GM
11949 Display *dpy = FRAME_X_DISPLAY (f);
11950 Window window = FRAME_X_WINDOW (f);
11951 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11952 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11953 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11954 XGCValues xgcv;
11955
11956 /* If the glyph's background equals the color we normally draw
11957 the bar cursor in, the bar cursor in its normal color is
11958 invisible. Use the glyph's foreground color instead in this
11959 case, on the assumption that the glyph's colors are chosen so
11960 that the glyph is legible. */
11961 if (face->background == f->output_data.x->cursor_pixel)
11962 xgcv.background = xgcv.foreground = face->foreground;
11963 else
11964 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 11965 xgcv.graphics_exposures = 0;
7d0393cf 11966
06a2c219
GM
11967 if (gc)
11968 XChangeGC (dpy, gc, mask, &xgcv);
11969 else
11970 {
11971 gc = XCreateGC (dpy, window, mask, &xgcv);
11972 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11973 }
7d0393cf 11974
f02d8aa0 11975 if (width < 0)
fdbe859c 11976 width = FRAME_CURSOR_WIDTH (f);
34e5d0af 11977 width = min (cursor_glyph->pixel_width, width);
7d0393cf 11978
b3738089 11979 w->phys_cursor_width = width;
06a2c219 11980 x_clip_to_row (w, row, gc, 0);
7d0393cf 11981
65c26775
EZ
11982 if (kind == BAR_CURSOR)
11983 XFillRectangle (dpy, window, gc,
11984 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11985 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11986 width, row->height);
11987 else
11988 XFillRectangle (dpy, window, gc,
11989 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11990 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11991 row->height - width),
11992 cursor_glyph->pixel_width,
11993 width);
11994
06a2c219
GM
11995 XSetClipMask (dpy, gc, None);
11996 }
dc6f92b8
JB
11997}
11998
06a2c219
GM
11999
12000/* Clear the cursor of window W to background color, and mark the
12001 cursor as not shown. This is used when the text where the cursor
12002 is is about to be rewritten. */
12003
dc6f92b8 12004static void
06a2c219
GM
12005x_clear_cursor (w)
12006 struct window *w;
dc6f92b8 12007{
06a2c219
GM
12008 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
12009 x_update_window_cursor (w, 0);
12010}
90e65f07 12011
dbc4e1c1 12012
06a2c219
GM
12013/* Draw the cursor glyph of window W in glyph row ROW. See the
12014 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 12015
06a2c219
GM
12016static void
12017x_draw_phys_cursor_glyph (w, row, hl)
12018 struct window *w;
12019 struct glyph_row *row;
12020 enum draw_glyphs_face hl;
12021{
12022 /* If cursor hpos is out of bounds, don't draw garbage. This can
12023 happen in mini-buffer windows when switching between echo area
12024 glyphs and mini-buffer. */
12025 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e 12026 {
f0a48a01 12027 int on_p = w->phys_cursor_on_p;
56a0382c 12028 int x1;
7d0393cf 12029
56a0382c
GM
12030 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
12031 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
12032 hl, 0);
f0a48a01 12033 w->phys_cursor_on_p = on_p;
66ac4b0e 12034
b3738089 12035 if (hl == DRAW_CURSOR)
56a0382c 12036 w->phys_cursor_width = x1 - w->phys_cursor.x;
b3738089 12037
66ac4b0e
GM
12038 /* When we erase the cursor, and ROW is overlapped by other
12039 rows, make sure that these overlapping parts of other rows
12040 are redrawn. */
b3738089 12041 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
66ac4b0e
GM
12042 {
12043 if (row > w->current_matrix->rows
12044 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
12045 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
12046
12047 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
12048 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
12049 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
12050 }
12051 }
06a2c219 12052}
dbc4e1c1 12053
eea6af04 12054
06a2c219 12055/* Erase the image of a cursor of window W from the screen. */
eea6af04 12056
06a2c219
GM
12057static void
12058x_erase_phys_cursor (w)
12059 struct window *w;
12060{
12061 struct frame *f = XFRAME (w->frame);
12062 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12063 int hpos = w->phys_cursor.hpos;
12064 int vpos = w->phys_cursor.vpos;
12065 int mouse_face_here_p = 0;
12066 struct glyph_matrix *active_glyphs = w->current_matrix;
12067 struct glyph_row *cursor_row;
12068 struct glyph *cursor_glyph;
12069 enum draw_glyphs_face hl;
12070
12071 /* No cursor displayed or row invalidated => nothing to do on the
12072 screen. */
12073 if (w->phys_cursor_type == NO_CURSOR)
12074 goto mark_cursor_off;
7d0393cf 12075
06a2c219
GM
12076 /* VPOS >= active_glyphs->nrows means that window has been resized.
12077 Don't bother to erase the cursor. */
12078 if (vpos >= active_glyphs->nrows)
12079 goto mark_cursor_off;
12080
12081 /* If row containing cursor is marked invalid, there is nothing we
12082 can do. */
12083 cursor_row = MATRIX_ROW (active_glyphs, vpos);
12084 if (!cursor_row->enabled_p)
12085 goto mark_cursor_off;
7d0393cf 12086
a2c6de8e
KS
12087 /* If row is completely invisible, don't attempt to delete a cursor which
12088 isn't there. This can happen if cursor is at top of a window, and
12089 we switch to a buffer with a header line in that window. */
12090 if (cursor_row->visible_height <= 0)
12091 goto mark_cursor_off;
7d0393cf 12092
06a2c219
GM
12093 /* This can happen when the new row is shorter than the old one.
12094 In this case, either x_draw_glyphs or clear_end_of_line
12095 should have cleared the cursor. Note that we wouldn't be
12096 able to erase the cursor in this case because we don't have a
12097 cursor glyph at hand. */
12098 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
12099 goto mark_cursor_off;
7d0393cf 12100
06a2c219
GM
12101 /* If the cursor is in the mouse face area, redisplay that when
12102 we clear the cursor. */
8801a864
KR
12103 if (! NILP (dpyinfo->mouse_face_window)
12104 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
12105 && (vpos > dpyinfo->mouse_face_beg_row
12106 || (vpos == dpyinfo->mouse_face_beg_row
12107 && hpos >= dpyinfo->mouse_face_beg_col))
12108 && (vpos < dpyinfo->mouse_face_end_row
12109 || (vpos == dpyinfo->mouse_face_end_row
12110 && hpos < dpyinfo->mouse_face_end_col))
12111 /* Don't redraw the cursor's spot in mouse face if it is at the
12112 end of a line (on a newline). The cursor appears there, but
12113 mouse highlighting does not. */
12114 && cursor_row->used[TEXT_AREA] > hpos)
12115 mouse_face_here_p = 1;
12116
12117 /* Maybe clear the display under the cursor. */
12118 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
12119 {
12120 int x;
045dee35 12121 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 12122
06a2c219
GM
12123 cursor_glyph = get_phys_cursor_glyph (w);
12124 if (cursor_glyph == NULL)
12125 goto mark_cursor_off;
dbc4e1c1 12126
e0300d33 12127 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7d0393cf 12128
c5e6e06b
GM
12129 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12130 x,
12131 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
12132 cursor_row->y)),
12133 cursor_glyph->pixel_width,
12134 cursor_row->visible_height,
12135 False);
dbc4e1c1 12136 }
7d0393cf 12137
06a2c219
GM
12138 /* Erase the cursor by redrawing the character underneath it. */
12139 if (mouse_face_here_p)
12140 hl = DRAW_MOUSE_FACE;
06a2c219
GM
12141 else
12142 hl = DRAW_NORMAL_TEXT;
12143 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 12144
06a2c219
GM
12145 mark_cursor_off:
12146 w->phys_cursor_on_p = 0;
12147 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
12148}
12149
12150
b7f83f9e
GM
12151/* Non-zero if physical cursor of window W is within mouse face. */
12152
12153static int
12154cursor_in_mouse_face_p (w)
12155 struct window *w;
12156{
12157 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
12158 int in_mouse_face = 0;
7d0393cf 12159
b7f83f9e
GM
12160 if (WINDOWP (dpyinfo->mouse_face_window)
12161 && XWINDOW (dpyinfo->mouse_face_window) == w)
12162 {
12163 int hpos = w->phys_cursor.hpos;
12164 int vpos = w->phys_cursor.vpos;
12165
12166 if (vpos >= dpyinfo->mouse_face_beg_row
12167 && vpos <= dpyinfo->mouse_face_end_row
12168 && (vpos > dpyinfo->mouse_face_beg_row
12169 || hpos >= dpyinfo->mouse_face_beg_col)
12170 && (vpos < dpyinfo->mouse_face_end_row
12171 || hpos < dpyinfo->mouse_face_end_col
12172 || dpyinfo->mouse_face_past_end))
12173 in_mouse_face = 1;
12174 }
12175
12176 return in_mouse_face;
12177}
12178
12179
06a2c219
GM
12180/* Display or clear cursor of window W. If ON is zero, clear the
12181 cursor. If it is non-zero, display the cursor. If ON is nonzero,
12182 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 12183
06a2c219
GM
12184void
12185x_display_and_set_cursor (w, on, hpos, vpos, x, y)
12186 struct window *w;
12187 int on, hpos, vpos, x, y;
dbc4e1c1 12188{
06a2c219
GM
12189 struct frame *f = XFRAME (w->frame);
12190 int new_cursor_type;
f02d8aa0 12191 int new_cursor_width;
e9c99027 12192 int active_cursor;
06a2c219
GM
12193 struct glyph_matrix *current_glyphs;
12194 struct glyph_row *glyph_row;
12195 struct glyph *glyph;
dbc4e1c1 12196
49d838ea 12197 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
12198 windows and frames; in the latter case, the frame or window may
12199 be in the midst of changing its size, and x and y may be off the
12200 window. */
12201 if (! FRAME_VISIBLE_P (f)
12202 || FRAME_GARBAGED_P (f)
12203 || vpos >= w->current_matrix->nrows
12204 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
12205 return;
12206
12207 /* If cursor is off and we want it off, return quickly. */
06a2c219 12208 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
12209 return;
12210
06a2c219
GM
12211 current_glyphs = w->current_matrix;
12212 glyph_row = MATRIX_ROW (current_glyphs, vpos);
12213 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
7d0393cf
JB
12214
12215 /* If cursor row is not enabled, we don't really know where to
06a2c219
GM
12216 display the cursor. */
12217 if (!glyph_row->enabled_p)
12218 {
12219 w->phys_cursor_on_p = 0;
12220 return;
12221 }
12222
12223 xassert (interrupt_input_blocked);
12224
fdbe859c 12225 /* Set new_cursor_type to the cursor we want to be displayed. */
e9c99027 12226 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
f7e039de 12227
06a2c219
GM
12228 /* If cursor is currently being shown and we don't want it to be or
12229 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 12230 erase it. */
06a2c219 12231 if (w->phys_cursor_on_p
dc6f92b8 12232 && (!on
06a2c219
GM
12233 || w->phys_cursor.x != x
12234 || w->phys_cursor.y != y
e1429afe 12235 || new_cursor_type != w->phys_cursor_type
fdbe859c 12236 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
e1429afe 12237 && new_cursor_width != w->phys_cursor_width)))
06a2c219
GM
12238 x_erase_phys_cursor (w);
12239
56a0382c
GM
12240 /* Don't check phys_cursor_on_p here because that flag is only set
12241 to zero in some cases where we know that the cursor has been
12242 completely erased, to avoid the extra work of erasing the cursor
12243 twice. In other words, phys_cursor_on_p can be 1 and the cursor
12244 still not be visible, or it has only been partly erased. */
12245 if (on)
06a2c219
GM
12246 {
12247 w->phys_cursor_ascent = glyph_row->ascent;
12248 w->phys_cursor_height = glyph_row->height;
7d0393cf 12249
06a2c219
GM
12250 /* Set phys_cursor_.* before x_draw_.* is called because some
12251 of them may need the information. */
12252 w->phys_cursor.x = x;
12253 w->phys_cursor.y = glyph_row->y;
12254 w->phys_cursor.hpos = hpos;
12255 w->phys_cursor.vpos = vpos;
12256 w->phys_cursor_type = new_cursor_type;
12257 w->phys_cursor_on_p = 1;
12258
12259 switch (new_cursor_type)
dc6f92b8 12260 {
06a2c219
GM
12261 case HOLLOW_BOX_CURSOR:
12262 x_draw_hollow_cursor (w, glyph_row);
12263 break;
12264
12265 case FILLED_BOX_CURSOR:
12266 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
12267 break;
12268
12269 case BAR_CURSOR:
65c26775
EZ
12270 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
12271 break;
12272
12273 case HBAR_CURSOR:
b3738089 12274 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
06a2c219
GM
12275 break;
12276
12277 case NO_CURSOR:
4398e673 12278 w->phys_cursor_width = 0;
06a2c219 12279 break;
dc6f92b8 12280
06a2c219
GM
12281 default:
12282 abort ();
12283 }
7d0393cf 12284
59ddecde
GM
12285#ifdef HAVE_X_I18N
12286 if (w == XWINDOW (f->selected_window))
12287 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
12288 xic_set_preeditarea (w, x, y);
12289#endif
dc6f92b8
JB
12290 }
12291
06a2c219 12292#ifndef XFlush
f676886a 12293 if (updating_frame != f)
334208b7 12294 XFlush (FRAME_X_DISPLAY (f));
06a2c219 12295#endif
dc6f92b8
JB
12296}
12297
06a2c219
GM
12298
12299/* Display the cursor on window W, or clear it. X and Y are window
12300 relative pixel coordinates. HPOS and VPOS are glyph matrix
12301 positions. If W is not the selected window, display a hollow
12302 cursor. ON non-zero means display the cursor at X, Y which
12303 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 12304
dfcf069d 12305void
06a2c219
GM
12306x_display_cursor (w, on, hpos, vpos, x, y)
12307 struct window *w;
12308 int on, hpos, vpos, x, y;
dc6f92b8 12309{
f94397b5 12310 BLOCK_INPUT;
06a2c219 12311 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
12312 UNBLOCK_INPUT;
12313}
12314
06a2c219
GM
12315
12316/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
12317 Don't change the cursor's position. */
12318
dfcf069d 12319void
06a2c219 12320x_update_cursor (f, on_p)
5d46f928 12321 struct frame *f;
0971e730 12322 int on_p;
5d46f928 12323{
06a2c219
GM
12324 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
12325}
12326
12327
12328/* Call x_update_window_cursor with parameter ON_P on all leaf windows
12329 in the window tree rooted at W. */
12330
12331static void
12332x_update_cursor_in_window_tree (w, on_p)
12333 struct window *w;
12334 int on_p;
12335{
12336 while (w)
12337 {
12338 if (!NILP (w->hchild))
12339 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
12340 else if (!NILP (w->vchild))
12341 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
12342 else
12343 x_update_window_cursor (w, on_p);
12344
12345 w = NILP (w->next) ? 0 : XWINDOW (w->next);
12346 }
12347}
5d46f928 12348
f94397b5 12349
06a2c219
GM
12350/* Switch the display of W's cursor on or off, according to the value
12351 of ON. */
12352
12353static void
12354x_update_window_cursor (w, on)
12355 struct window *w;
12356 int on;
12357{
16b5d424
GM
12358 /* Don't update cursor in windows whose frame is in the process
12359 of being deleted. */
12360 if (w->current_matrix)
12361 {
12362 BLOCK_INPUT;
12363 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
12364 w->phys_cursor.x, w->phys_cursor.y);
12365 UNBLOCK_INPUT;
12366 }
dc6f92b8 12367}
06a2c219
GM
12368
12369
12370
dc6f92b8
JB
12371\f
12372/* Icons. */
12373
dbc4e1c1 12374/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
12375
12376int
990ba854 12377x_bitmap_icon (f, file)
f676886a 12378 struct frame *f;
990ba854 12379 Lisp_Object file;
dc6f92b8 12380{
06a2c219 12381 int bitmap_id;
dc6f92b8 12382
c118dd06 12383 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
12384 return 1;
12385
990ba854 12386 /* Free up our existing icon bitmap if any. */
7556890b
RS
12387 if (f->output_data.x->icon_bitmap > 0)
12388 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12389 f->output_data.x->icon_bitmap = 0;
990ba854
RS
12390
12391 if (STRINGP (file))
7f2ae036
RS
12392 bitmap_id = x_create_bitmap_from_file (f, file);
12393 else
12394 {
990ba854 12395 /* Create the GNU bitmap if necessary. */
5bf01b68 12396 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
12397 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
12398 = x_create_bitmap_from_data (f, gnu_bits,
12399 gnu_width, gnu_height);
990ba854
RS
12400
12401 /* The first time we create the GNU bitmap,
06a2c219 12402 this increments the ref-count one extra time.
990ba854
RS
12403 As a result, the GNU bitmap is never freed.
12404 That way, we don't have to worry about allocating it again. */
334208b7 12405 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 12406
334208b7 12407 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
12408 }
12409
12410 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 12411 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
12412
12413 return 0;
12414}
12415
12416
1be2d067
KH
12417/* Make the x-window of frame F use a rectangle with text.
12418 Use ICON_NAME as the text. */
dc6f92b8
JB
12419
12420int
f676886a
JB
12421x_text_icon (f, icon_name)
12422 struct frame *f;
dc6f92b8
JB
12423 char *icon_name;
12424{
c118dd06 12425 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
12426 return 1;
12427
1be2d067
KH
12428#ifdef HAVE_X11R4
12429 {
12430 XTextProperty text;
12431 text.value = (unsigned char *) icon_name;
12432 text.encoding = XA_STRING;
12433 text.format = 8;
12434 text.nitems = strlen (icon_name);
2436a4e4 12435 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1be2d067
KH
12436 }
12437#else /* not HAVE_X11R4 */
2436a4e4 12438 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
1be2d067 12439#endif /* not HAVE_X11R4 */
58769bee 12440
7556890b
RS
12441 if (f->output_data.x->icon_bitmap > 0)
12442 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12443 f->output_data.x->icon_bitmap = 0;
b1c884c3 12444 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
12445
12446 return 0;
12447}
12448\f
e99db5a1
RS
12449#define X_ERROR_MESSAGE_SIZE 200
12450
12451/* If non-nil, this should be a string.
12452 It means catch X errors and store the error message in this string. */
12453
12454static Lisp_Object x_error_message_string;
12455
12456/* An X error handler which stores the error message in
12457 x_error_message_string. This is called from x_error_handler if
12458 x_catch_errors is in effect. */
12459
06a2c219 12460static void
e99db5a1
RS
12461x_error_catcher (display, error)
12462 Display *display;
12463 XErrorEvent *error;
12464{
12465 XGetErrorText (display, error->error_code,
d5db4077 12466 SDATA (x_error_message_string),
e99db5a1
RS
12467 X_ERROR_MESSAGE_SIZE);
12468}
12469
12470/* Begin trapping X errors for display DPY. Actually we trap X errors
12471 for all displays, but DPY should be the display you are actually
12472 operating on.
12473
12474 After calling this function, X protocol errors no longer cause
12475 Emacs to exit; instead, they are recorded in the string
12476 stored in x_error_message_string.
12477
12478 Calling x_check_errors signals an Emacs error if an X error has
12479 occurred since the last call to x_catch_errors or x_check_errors.
12480
12481 Calling x_uncatch_errors resumes the normal error handling. */
12482
12483void x_check_errors ();
12484static Lisp_Object x_catch_errors_unwind ();
12485
12486int
12487x_catch_errors (dpy)
12488 Display *dpy;
12489{
aed13378 12490 int count = SPECPDL_INDEX ();
e99db5a1
RS
12491
12492 /* Make sure any errors from previous requests have been dealt with. */
12493 XSync (dpy, False);
12494
781d152a
RS
12495 record_unwind_protect (x_catch_errors_unwind,
12496 Fcons (make_save_value (dpy, 0),
12497 x_error_message_string));
e99db5a1
RS
12498
12499 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7da167cd 12500 SSET (x_error_message_string, 0, 0);
e99db5a1
RS
12501
12502 return count;
12503}
12504
12505/* Unbind the binding that we made to check for X errors. */
12506
12507static Lisp_Object
12508x_catch_errors_unwind (old_val)
12509 Lisp_Object old_val;
12510{
781d152a
RS
12511 Lisp_Object first;
12512
12513 first = XCAR (old_val);
12514
12515 XSync (XSAVE_VALUE (first)->pointer, False);
12516
12517 x_error_message_string = XCDR (old_val);
e99db5a1
RS
12518 return Qnil;
12519}
12520
12521/* If any X protocol errors have arrived since the last call to
12522 x_catch_errors or x_check_errors, signal an Emacs error using
12523 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12524
12525void
12526x_check_errors (dpy, format)
12527 Display *dpy;
12528 char *format;
12529{
12530 /* Make sure to catch any errors incurred so far. */
12531 XSync (dpy, False);
12532
d5db4077
KR
12533 if (SREF (x_error_message_string, 0))
12534 error (format, SDATA (x_error_message_string));
e99db5a1
RS
12535}
12536
9829ddba
RS
12537/* Nonzero if we had any X protocol errors
12538 since we did x_catch_errors on DPY. */
e99db5a1
RS
12539
12540int
12541x_had_errors_p (dpy)
12542 Display *dpy;
12543{
12544 /* Make sure to catch any errors incurred so far. */
12545 XSync (dpy, False);
12546
d5db4077 12547 return SREF (x_error_message_string, 0) != 0;
e99db5a1
RS
12548}
12549
9829ddba
RS
12550/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12551
06a2c219 12552void
9829ddba
RS
12553x_clear_errors (dpy)
12554 Display *dpy;
12555{
7da167cd 12556 SSET (x_error_message_string, 0, 0);
9829ddba
RS
12557}
12558
e99db5a1
RS
12559/* Stop catching X protocol errors and let them make Emacs die.
12560 DPY should be the display that was passed to x_catch_errors.
12561 COUNT should be the value that was returned by
12562 the corresponding call to x_catch_errors. */
12563
12564void
12565x_uncatch_errors (dpy, count)
12566 Display *dpy;
12567 int count;
12568{
12569 unbind_to (count, Qnil);
12570}
12571
12572#if 0
12573static unsigned int x_wire_count;
12574x_trace_wire ()
12575{
12576 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12577}
12578#endif /* ! 0 */
12579
12580\f
12581/* Handle SIGPIPE, which can happen when the connection to a server
12582 simply goes away. SIGPIPE is handled by x_connection_signal.
7d0393cf 12583 Don't need to do anything, because the write which caused the
e99db5a1 12584 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 12585 which will do the appropriate cleanup for us. */
7d0393cf 12586
e99db5a1
RS
12587static SIGTYPE
12588x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 12589 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
12590{
12591#ifdef USG
12592 /* USG systems forget handlers when they are used;
12593 must reestablish each time */
12594 signal (signalnum, x_connection_signal);
12595#endif /* USG */
12596}
0da1ab50 12597
e99db5a1 12598\f
0da1ab50
GM
12599/************************************************************************
12600 Handling X errors
12601 ************************************************************************/
4746118a 12602
f0e299de
GM
12603/* Error message passed to x_connection_closed. */
12604
12605static char *error_msg;
12606
a7248e4f 12607/* Function installed as fatal_error_signal_hook in
f0e299de
GM
12608 x_connection_closed. Print the X error message, and exit normally,
12609 instead of dumping core when XtCloseDisplay fails. */
12610
12611static void
12612x_fatal_error_signal ()
12613{
12614 fprintf (stderr, "%s\n", error_msg);
12615 exit (70);
12616}
12617
0da1ab50
GM
12618/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12619 the text of an error message that lead to the connection loss. */
16bd92ea 12620
4746118a 12621static SIGTYPE
5978125e
GM
12622x_connection_closed (dpy, error_message)
12623 Display *dpy;
7a13e894 12624 char *error_message;
4746118a 12625{
5978125e 12626 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 12627 Lisp_Object frame, tail;
0da1ab50 12628 int count;
7d0393cf 12629
f0e299de
GM
12630 error_msg = (char *) alloca (strlen (error_message) + 1);
12631 strcpy (error_msg, error_message);
1a532e54 12632 handling_signal = 0;
7d0393cf 12633
0da1ab50
GM
12634 /* Prevent being called recursively because of an error condition
12635 below. Otherwise, we might end up with printing ``can't find per
12636 display information'' in the recursive call instead of printing
12637 the original message here. */
12638 count = x_catch_errors (dpy);
7d0393cf 12639
8a4f36cc
GM
12640 /* We have to close the display to inform Xt that it doesn't
12641 exist anymore. If we don't, Xt will continue to wait for
12642 events from the display. As a consequence, a sequence of
12643
12644 M-x make-frame-on-display RET :1 RET
12645 ...kill the new frame, so that we get an IO error...
12646 M-x make-frame-on-display RET :1 RET
12647
12648 will indefinitely wait in Xt for events for display `:1', opened
12649 in the first class to make-frame-on-display.
6186a4a0 12650
8a4f36cc
GM
12651 Closing the display is reported to lead to a bus error on
12652 OpenWindows in certain situations. I suspect that is a bug
12653 in OpenWindows. I don't know how to cicumvent it here. */
7d0393cf 12654
f613a4c8 12655#ifdef USE_X_TOOLKIT
ae24cb3b
GM
12656 /* If DPYINFO is null, this means we didn't open the display
12657 in the first place, so don't try to close it. */
12658 if (dpyinfo)
f0e299de
GM
12659 {
12660 extern void (*fatal_error_signal_hook) P_ ((void));
12661 fatal_error_signal_hook = x_fatal_error_signal;
12662 XtCloseDisplay (dpy);
12663 fatal_error_signal_hook = NULL;
12664 }
f613a4c8 12665#endif
adabc3a9 12666
8a4f36cc 12667 /* Indicate that this display is dead. */
9e80b57d
KR
12668 if (dpyinfo)
12669 dpyinfo->display = 0;
6186a4a0 12670
06a2c219 12671 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
12672 that are on the dead display. */
12673 FOR_EACH_FRAME (tail, frame)
12674 {
12675 Lisp_Object minibuf_frame;
12676 minibuf_frame
12677 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
12678 if (FRAME_X_P (XFRAME (frame))
12679 && FRAME_X_P (XFRAME (minibuf_frame))
12680 && ! EQ (frame, minibuf_frame)
12681 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
12682 Fdelete_frame (frame, Qt);
12683 }
12684
12685 /* Now delete all remaining frames on the dead display.
06a2c219 12686 We are now sure none of these is used as the mini-buffer
7a13e894
RS
12687 for another frame that we need to delete. */
12688 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
12689 if (FRAME_X_P (XFRAME (frame))
12690 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
12691 {
12692 /* Set this to t so that Fdelete_frame won't get confused
12693 trying to find a replacement. */
12694 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12695 Fdelete_frame (frame, Qt);
12696 }
7a13e894 12697
482a1bd2
KH
12698 if (dpyinfo)
12699 x_delete_display (dpyinfo);
7a13e894 12700
0da1ab50 12701 x_uncatch_errors (dpy, count);
7d0393cf 12702
7a13e894
RS
12703 if (x_display_list == 0)
12704 {
f0e299de 12705 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
12706 shut_down_emacs (0, 0, Qnil);
12707 exit (70);
12708 }
12ba150f 12709
7a13e894
RS
12710 /* Ordinary stack unwind doesn't deal with these. */
12711#ifdef SIGIO
12712 sigunblock (sigmask (SIGIO));
12713#endif
12714 sigunblock (sigmask (SIGALRM));
12715 TOTALLY_UNBLOCK_INPUT;
12716
aa4d9a9e 12717 clear_waiting_for_input ();
f0e299de 12718 error ("%s", error_msg);
4746118a
JB
12719}
12720
0da1ab50 12721
7a13e894
RS
12722/* This is the usual handler for X protocol errors.
12723 It kills all frames on the display that we got the error for.
12724 If that was the only one, it prints an error message and kills Emacs. */
12725
06a2c219 12726static void
c118dd06
JB
12727x_error_quitter (display, error)
12728 Display *display;
12729 XErrorEvent *error;
12730{
7a13e894 12731 char buf[256], buf1[356];
dc6f92b8 12732
58769bee 12733 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12734 original error handler. */
dc6f92b8 12735
c118dd06 12736 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 12737 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 12738 buf, error->request_code);
7a13e894 12739 x_connection_closed (display, buf1);
dc6f92b8
JB
12740}
12741
0da1ab50 12742
e99db5a1
RS
12743/* This is the first-level handler for X protocol errors.
12744 It calls x_error_quitter or x_error_catcher. */
7a13e894 12745
8922af5f 12746static int
e99db5a1 12747x_error_handler (display, error)
8922af5f 12748 Display *display;
e99db5a1 12749 XErrorEvent *error;
8922af5f 12750{
e99db5a1
RS
12751 if (! NILP (x_error_message_string))
12752 x_error_catcher (display, error);
12753 else
12754 x_error_quitter (display, error);
06a2c219 12755 return 0;
f9e24cb9 12756}
c118dd06 12757
e99db5a1
RS
12758/* This is the handler for X IO errors, always.
12759 It kills all frames on the display that we lost touch with.
12760 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 12761
c118dd06 12762static int
e99db5a1 12763x_io_error_quitter (display)
c118dd06 12764 Display *display;
c118dd06 12765{
e99db5a1 12766 char buf[256];
dc6f92b8 12767
e99db5a1
RS
12768 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12769 x_connection_closed (display, buf);
06a2c219 12770 return 0;
dc6f92b8 12771}
dc6f92b8 12772\f
f451eb13
JB
12773/* Changing the font of the frame. */
12774
76bcdf39
RS
12775/* Give frame F the font named FONTNAME as its default font, and
12776 return the full name of that font. FONTNAME may be a wildcard
12777 pattern; in that case, we choose some font that fits the pattern.
12778 The return value shows which font we chose. */
12779
b5cf7a0e 12780Lisp_Object
f676886a
JB
12781x_new_font (f, fontname)
12782 struct frame *f;
dc6f92b8
JB
12783 register char *fontname;
12784{
dc43ef94 12785 struct font_info *fontp
ee569018 12786 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 12787
dc43ef94
KH
12788 if (!fontp)
12789 return Qnil;
2224a5fc 12790
dc43ef94 12791 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 12792 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94 12793 f->output_data.x->fontset = -1;
976b73d7
KS
12794
12795 x_compute_fringe_widths (f, 1);
12796
b2cad826
KH
12797 /* Compute the scroll bar width in character columns. */
12798 if (f->scroll_bar_pixel_width > 0)
12799 {
7556890b 12800 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
12801 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12802 }
12803 else
4e61bddf
RS
12804 {
12805 int wid = FONT_WIDTH (f->output_data.x->font);
12806 f->scroll_bar_cols = (14 + wid - 1) / wid;
12807 }
b2cad826 12808
f676886a 12809 /* Now make the frame display the given font. */
c118dd06 12810 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 12811 {
7556890b
RS
12812 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12813 f->output_data.x->font->fid);
12814 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12815 f->output_data.x->font->fid);
12816 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12817 f->output_data.x->font->fid);
f676886a 12818
a27f9f86 12819 frame_update_line_height (f);
3497f73e
GM
12820
12821 /* Don't change the size of a tip frame; there's no point in
12822 doing it because it's done in Fx_show_tip, and it leads to
12823 problems because the tip frame has no widget. */
12824 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12825 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 12826 }
a27f9f86
RS
12827 else
12828 /* If we are setting a new frame's font for the first time,
12829 there are no faces yet, so this font's height is the line height. */
7556890b 12830 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 12831
dc43ef94
KH
12832 return build_string (fontp->full_name);
12833}
12834
12835/* Give frame F the fontset named FONTSETNAME as its default font, and
12836 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
12837 pattern; in that case, we choose some fontset that fits the pattern.
12838 The return value shows which fontset we chose. */
b5cf7a0e 12839
dc43ef94
KH
12840Lisp_Object
12841x_new_fontset (f, fontsetname)
12842 struct frame *f;
12843 char *fontsetname;
12844{
ee569018 12845 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 12846 Lisp_Object result;
b5cf7a0e 12847
dc43ef94
KH
12848 if (fontset < 0)
12849 return Qnil;
b5cf7a0e 12850
2da424f1
KH
12851 if (f->output_data.x->fontset == fontset)
12852 /* This fontset is already set in frame F. There's nothing more
12853 to do. */
ee569018 12854 return fontset_name (fontset);
dc43ef94 12855
d5db4077 12856 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
dc43ef94
KH
12857
12858 if (!STRINGP (result))
12859 /* Can't load ASCII font. */
12860 return Qnil;
12861
12862 /* Since x_new_font doesn't update any fontset information, do it now. */
12863 f->output_data.x->fontset = fontset;
dc43ef94 12864
f5d11644
GM
12865#ifdef HAVE_X_I18N
12866 if (FRAME_XIC (f)
12867 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
d5db4077 12868 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
f5d11644 12869#endif
7d0393cf 12870
dc43ef94 12871 return build_string (fontsetname);
dc6f92b8 12872}
f5d11644 12873
976b73d7
KS
12874/* Compute actual fringe widths */
12875
12876void
12877x_compute_fringe_widths (f, redraw)
12878 struct frame *f;
12879 int redraw;
12880{
12881 int o_left = f->output_data.x->left_fringe_width;
12882 int o_right = f->output_data.x->right_fringe_width;
12883 int o_cols = f->output_data.x->fringe_cols;
12884
12885 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12886 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12887 int left_fringe_width, right_fringe_width;
12888
12889 if (!NILP (left_fringe))
12890 left_fringe = Fcdr (left_fringe);
12891 if (!NILP (right_fringe))
12892 right_fringe = Fcdr (right_fringe);
12893
12894 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12895 XINT (left_fringe));
12896 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12897 XINT (right_fringe));
12898
12899 if (left_fringe_width || right_fringe_width)
12900 {
12901 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12902 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12903 int conf_wid = left_wid + right_wid;
12904 int font_wid = FONT_WIDTH (f->output_data.x->font);
12905 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12906 int real_wid = cols * font_wid;
12907 if (left_wid && right_wid)
12908 {
12909 if (left_fringe_width < 0)
12910 {
12911 /* Left fringe width is fixed, adjust right fringe if necessary */
12912 f->output_data.x->left_fringe_width = left_wid;
12913 f->output_data.x->right_fringe_width = real_wid - left_wid;
12914 }
12915 else if (right_fringe_width < 0)
12916 {
12917 /* Right fringe width is fixed, adjust left fringe if necessary */
12918 f->output_data.x->left_fringe_width = real_wid - right_wid;
12919 f->output_data.x->right_fringe_width = right_wid;
12920 }
12921 else
12922 {
12923 /* Adjust both fringes with an equal amount.
12924 Note that we are doing integer arithmetic here, so don't
12925 lose a pixel if the total width is an odd number. */
12926 int fill = real_wid - conf_wid;
12927 f->output_data.x->left_fringe_width = left_wid + fill/2;
12928 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12929 }
12930 }
12931 else if (left_fringe_width)
12932 {
12933 f->output_data.x->left_fringe_width = real_wid;
12934 f->output_data.x->right_fringe_width = 0;
12935 }
12936 else
12937 {
12938 f->output_data.x->left_fringe_width = 0;
12939 f->output_data.x->right_fringe_width = real_wid;
12940 }
12941 f->output_data.x->fringe_cols = cols;
12942 f->output_data.x->fringes_extra = real_wid;
12943 }
12944 else
12945 {
12946 f->output_data.x->left_fringe_width = 0;
12947 f->output_data.x->right_fringe_width = 0;
12948 f->output_data.x->fringe_cols = 0;
12949 f->output_data.x->fringes_extra = 0;
12950 }
12951
12952 if (redraw && FRAME_VISIBLE_P (f))
12953 if (o_left != f->output_data.x->left_fringe_width ||
12954 o_right != f->output_data.x->right_fringe_width ||
12955 o_cols != f->output_data.x->fringe_cols)
12956 redraw_frame (f);
12957}
f5d11644
GM
12958\f
12959/***********************************************************************
12960 X Input Methods
12961 ***********************************************************************/
12962
12963#ifdef HAVE_X_I18N
12964
12965#ifdef HAVE_X11R6
12966
12967/* XIM destroy callback function, which is called whenever the
12968 connection to input method XIM dies. CLIENT_DATA contains a
12969 pointer to the x_display_info structure corresponding to XIM. */
12970
12971static void
12972xim_destroy_callback (xim, client_data, call_data)
12973 XIM xim;
12974 XPointer client_data;
12975 XPointer call_data;
12976{
12977 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12978 Lisp_Object frame, tail;
7d0393cf 12979
f5d11644 12980 BLOCK_INPUT;
7d0393cf 12981
f5d11644
GM
12982 /* No need to call XDestroyIC.. */
12983 FOR_EACH_FRAME (tail, frame)
12984 {
12985 struct frame *f = XFRAME (frame);
12986 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12987 {
12988 FRAME_XIC (f) = NULL;
12989 if (FRAME_XIC_FONTSET (f))
12990 {
12991 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12992 FRAME_XIC_FONTSET (f) = NULL;
12993 }
12994 }
12995 }
7d0393cf 12996
f5d11644
GM
12997 /* No need to call XCloseIM. */
12998 dpyinfo->xim = NULL;
12999 XFree (dpyinfo->xim_styles);
13000 UNBLOCK_INPUT;
13001}
13002
13003#endif /* HAVE_X11R6 */
13004
dbd4b028
DL
13005#ifdef HAVE_X11R6
13006/* This isn't prototyped in OSF 5.0 or 5.1a. */
13007extern char *XSetIMValues P_ ((XIM, ...));
13008#endif
13009
f5d11644
GM
13010/* Open the connection to the XIM server on display DPYINFO.
13011 RESOURCE_NAME is the resource name Emacs uses. */
13012
13013static void
13014xim_open_dpy (dpyinfo, resource_name)
13015 struct x_display_info *dpyinfo;
13016 char *resource_name;
13017{
287f7dd6 13018#ifdef USE_XIM
f5d11644
GM
13019 XIM xim;
13020
13021 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
13022 dpyinfo->xim = xim;
13023
13024 if (xim)
13025 {
f5d11644
GM
13026#ifdef HAVE_X11R6
13027 XIMCallback destroy;
13028#endif
7d0393cf 13029
f5d11644
GM
13030 /* Get supported styles and XIM values. */
13031 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7d0393cf 13032
f5d11644
GM
13033#ifdef HAVE_X11R6
13034 destroy.callback = xim_destroy_callback;
13035 destroy.client_data = (XPointer)dpyinfo;
13036 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
13037#endif
13038 }
7d0393cf 13039
287f7dd6
GM
13040#else /* not USE_XIM */
13041 dpyinfo->xim = NULL;
13042#endif /* not USE_XIM */
f5d11644
GM
13043}
13044
13045
b9de836c 13046#ifdef HAVE_X11R6_XIM
f5d11644
GM
13047
13048struct xim_inst_t
13049{
13050 struct x_display_info *dpyinfo;
13051 char *resource_name;
13052};
13053
13054/* XIM instantiate callback function, which is called whenever an XIM
1fcfb866 13055 server is available. DISPLAY is the display of the XIM.
f5d11644
GM
13056 CLIENT_DATA contains a pointer to an xim_inst_t structure created
13057 when the callback was registered. */
13058
13059static void
13060xim_instantiate_callback (display, client_data, call_data)
13061 Display *display;
13062 XPointer client_data;
13063 XPointer call_data;
13064{
13065 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
13066 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
13067
13068 /* We don't support multiple XIM connections. */
13069 if (dpyinfo->xim)
13070 return;
7d0393cf 13071
f5d11644
GM
13072 xim_open_dpy (dpyinfo, xim_inst->resource_name);
13073
13074 /* Create XIC for the existing frames on the same display, as long
13075 as they have no XIC. */
13076 if (dpyinfo->xim && dpyinfo->reference_count > 0)
13077 {
13078 Lisp_Object tail, frame;
13079
13080 BLOCK_INPUT;
13081 FOR_EACH_FRAME (tail, frame)
13082 {
13083 struct frame *f = XFRAME (frame);
7d0393cf 13084
f5d11644
GM
13085 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
13086 if (FRAME_XIC (f) == NULL)
13087 {
13088 create_frame_xic (f);
13089 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
13090 xic_set_statusarea (f);
13091 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
13092 {
13093 struct window *w = XWINDOW (f->selected_window);
13094 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
13095 }
13096 }
13097 }
7d0393cf 13098
f5d11644
GM
13099 UNBLOCK_INPUT;
13100 }
13101}
13102
b9de836c 13103#endif /* HAVE_X11R6_XIM */
f5d11644
GM
13104
13105
13106/* Open a connection to the XIM server on display DPYINFO.
13107 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
13108 connection only at the first time. On X11R6, open the connection
13109 in the XIM instantiate callback function. */
13110
13111static void
13112xim_initialize (dpyinfo, resource_name)
13113 struct x_display_info *dpyinfo;
13114 char *resource_name;
13115{
287f7dd6 13116#ifdef USE_XIM
b9de836c 13117#ifdef HAVE_X11R6_XIM
f5d11644
GM
13118 struct xim_inst_t *xim_inst;
13119 int len;
7d0393cf 13120
f5d11644
GM
13121 dpyinfo->xim = NULL;
13122 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
13123 xim_inst->dpyinfo = dpyinfo;
13124 len = strlen (resource_name);
13125 xim_inst->resource_name = (char *) xmalloc (len + 1);
13126 bcopy (resource_name, xim_inst->resource_name, len + 1);
13127 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
13128 resource_name, EMACS_CLASS,
13129 xim_instantiate_callback,
2ebb2f8b
DL
13130 /* Fixme: This is XPointer in
13131 XFree86 but (XPointer *) on
13132 Tru64, at least. */
13133 (XPointer) xim_inst);
b9de836c 13134#else /* not HAVE_X11R6_XIM */
f5d11644
GM
13135 dpyinfo->xim = NULL;
13136 xim_open_dpy (dpyinfo, resource_name);
b9de836c 13137#endif /* not HAVE_X11R6_XIM */
7d0393cf 13138
287f7dd6
GM
13139#else /* not USE_XIM */
13140 dpyinfo->xim = NULL;
13141#endif /* not USE_XIM */
f5d11644
GM
13142}
13143
13144
13145/* Close the connection to the XIM server on display DPYINFO. */
13146
13147static void
13148xim_close_dpy (dpyinfo)
13149 struct x_display_info *dpyinfo;
13150{
287f7dd6 13151#ifdef USE_XIM
b9de836c 13152#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
13153 if (dpyinfo->display)
13154 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
13155 NULL, EMACS_CLASS,
13156 xim_instantiate_callback, NULL);
b9de836c 13157#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
13158 if (dpyinfo->display)
13159 XCloseIM (dpyinfo->xim);
f5d11644
GM
13160 dpyinfo->xim = NULL;
13161 XFree (dpyinfo->xim_styles);
287f7dd6 13162#endif /* USE_XIM */
f5d11644
GM
13163}
13164
b9de836c 13165#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
13166
13167
dc6f92b8 13168\f
2e365682
RS
13169/* Calculate the absolute position in frame F
13170 from its current recorded position values and gravity. */
13171
dfcf069d 13172void
43bca5d5 13173x_calc_absolute_position (f)
f676886a 13174 struct frame *f;
dc6f92b8 13175{
06a2c219 13176 Window child;
6dba1858 13177 int win_x = 0, win_y = 0;
7556890b 13178 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
13179 int this_window;
13180
9829ddba
RS
13181 /* We have nothing to do if the current position
13182 is already for the top-left corner. */
13183 if (! ((flags & XNegative) || (flags & YNegative)))
13184 return;
13185
488dd4c4 13186 this_window = FRAME_OUTER_WINDOW (f);
6dba1858
RS
13187
13188 /* Find the position of the outside upper-left corner of
9829ddba
RS
13189 the inner window, with respect to the outer window.
13190 But do this only if we will need the results. */
7556890b 13191 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 13192 {
9829ddba
RS
13193 int count;
13194
6dba1858 13195 BLOCK_INPUT;
9829ddba
RS
13196 count = x_catch_errors (FRAME_X_DISPLAY (f));
13197 while (1)
13198 {
13199 x_clear_errors (FRAME_X_DISPLAY (f));
13200 XTranslateCoordinates (FRAME_X_DISPLAY (f),
13201
13202 /* From-window, to-window. */
13203 this_window,
13204 f->output_data.x->parent_desc,
13205
13206 /* From-position, to-position. */
13207 0, 0, &win_x, &win_y,
13208
13209 /* Child of win. */
13210 &child);
13211 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13212 {
13213 Window newroot, newparent = 0xdeadbeef;
13214 Window *newchildren;
2ebb2f8b 13215 unsigned int nchildren;
9829ddba
RS
13216
13217 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
13218 &newparent, &newchildren, &nchildren))
13219 break;
58769bee 13220
7c3c78a3 13221 XFree ((char *) newchildren);
6dba1858 13222
9829ddba
RS
13223 f->output_data.x->parent_desc = newparent;
13224 }
13225 else
13226 break;
13227 }
6dba1858 13228
9829ddba 13229 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
13230 UNBLOCK_INPUT;
13231 }
13232
13233 /* Treat negative positions as relative to the leftmost bottommost
13234 position that fits on the screen. */
20f55f9a 13235 if (flags & XNegative)
7556890b 13236 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
13237 - 2 * f->output_data.x->border_width - win_x
13238 - PIXEL_WIDTH (f)
13239 + f->output_data.x->left_pos);
dc6f92b8 13240
7708ced0
GM
13241 {
13242 int height = PIXEL_HEIGHT (f);
06a2c219 13243
7708ced0
GM
13244#if defined USE_X_TOOLKIT && defined USE_MOTIF
13245 /* Something is fishy here. When using Motif, starting Emacs with
13246 `-g -0-0', the frame appears too low by a few pixels.
13247
13248 This seems to be so because initially, while Emacs is starting,
13249 the column widget's height and the frame's pixel height are
13250 different. The column widget's height is the right one. In
13251 later invocations, when Emacs is up, the frame's pixel height
13252 is right, though.
13253
13254 It's not obvious where the initial small difference comes from.
13255 2000-12-01, gerd. */
7d0393cf 13256
7708ced0 13257 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 13258#endif
2e365682 13259
7708ced0
GM
13260 if (flags & YNegative)
13261 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
13262 - 2 * f->output_data.x->border_width
13263 - win_y
13264 - height
13265 + f->output_data.x->top_pos);
13266 }
7d0393cf 13267
3a35ab44
RS
13268 /* The left_pos and top_pos
13269 are now relative to the top and left screen edges,
13270 so the flags should correspond. */
7556890b 13271 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
13272}
13273
3a35ab44
RS
13274/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
13275 to really change the position, and 0 when calling from
13276 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
13277 position values). It is -1 when calling from x_set_frame_parameters,
13278 which means, do adjust for borders but don't change the gravity. */
3a35ab44 13279
dfcf069d 13280void
dc05a16b 13281x_set_offset (f, xoff, yoff, change_gravity)
f676886a 13282 struct frame *f;
dc6f92b8 13283 register int xoff, yoff;
dc05a16b 13284 int change_gravity;
dc6f92b8 13285{
4a4cbdd5
KH
13286 int modified_top, modified_left;
13287
aa3ff7c9 13288 if (change_gravity > 0)
3a35ab44 13289 {
7556890b
RS
13290 f->output_data.x->top_pos = yoff;
13291 f->output_data.x->left_pos = xoff;
13292 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 13293 if (xoff < 0)
7556890b 13294 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 13295 if (yoff < 0)
7556890b
RS
13296 f->output_data.x->size_hint_flags |= YNegative;
13297 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 13298 }
43bca5d5 13299 x_calc_absolute_position (f);
dc6f92b8
JB
13300
13301 BLOCK_INPUT;
c32cdd9a 13302 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 13303
7556890b
RS
13304 modified_left = f->output_data.x->left_pos;
13305 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
13306#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
13307 this seems to be unnecessary and incorrect. rms, 4/17/97. */
13308 /* It is a mystery why we need to add the border_width here
13309 when the frame is already visible, but experiment says we do. */
aa3ff7c9 13310 if (change_gravity != 0)
4a4cbdd5 13311 {
7556890b
RS
13312 modified_left += f->output_data.x->border_width;
13313 modified_top += f->output_data.x->border_width;
4a4cbdd5 13314 }
e73ec6fa 13315#endif
4a4cbdd5 13316
488dd4c4
JD
13317 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13318 modified_left, modified_top);
dc6f92b8
JB
13319 UNBLOCK_INPUT;
13320}
13321
d0fa3e56
EZ
13322/* Check if we need to resize the frame due to a fullscreen request.
13323 If so needed, resize the frame. */
13324static void
13325x_check_fullscreen (f)
13326 struct frame *f;
13327{
13328 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
13329 {
13330 int width, height, ign;
7d0393cf 13331
d0fa3e56
EZ
13332 x_real_positions (f, &f->output_data.x->left_pos,
13333 &f->output_data.x->top_pos);
13334
13335 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
7d0393cf 13336
d0fa3e56
EZ
13337 /* We do not need to move the window, it shall be taken care of
13338 when setting WM manager hints.
13339 If the frame is visible already, the position is checked by
13340 x_check_fullscreen_move. */
13341 if (f->width != width || f->height != height)
13342 {
13343 change_frame_size (f, height, width, 0, 1, 0);
13344 SET_FRAME_GARBAGED (f);
13345 cancel_mouse_face (f);
13346
13347 /* Wait for the change of frame size to occur */
13348 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
7d0393cf 13349
d0fa3e56
EZ
13350 }
13351 }
13352}
13353
13354/* If frame parameters are set after the frame is mapped, we need to move
13355 the window. This is done in xfns.c.
13356 Some window managers moves the window to the right position, some
13357 moves the outer window manager window to the specified position.
13358 Here we check that we are in the right spot. If not, make a second
13359 move, assuming we are dealing with the second kind of window manager. */
13360static void
13361x_check_fullscreen_move (f)
13362 struct frame *f;
13363{
13364 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
13365 {
13366 int expect_top = f->output_data.x->top_pos;
13367 int expect_left = f->output_data.x->left_pos;
13368
13369 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13370 expect_top = 0;
13371 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13372 expect_left = 0;
7d0393cf 13373
d0fa3e56
EZ
13374 if (expect_top != f->output_data.x->top_pos
13375 || expect_left != f->output_data.x->left_pos)
13376 x_set_offset (f, expect_left, expect_top, 1);
13377
13378 /* Just do this once */
13379 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
13380 }
13381}
13382
13383
13384/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
13385 wanted positions of the WM window (not emacs window).
13386 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
13387 window (FRAME_X_WINDOW).
13388 */
13389void
13390x_fullscreen_adjust (f, width, height, top_pos, left_pos)
13391 struct frame *f;
13392 int *width;
13393 int *height;
13394 int *top_pos;
13395 int *left_pos;
13396{
13397 int newwidth = f->width, newheight = f->height;
13398
13399 *top_pos = f->output_data.x->top_pos;
13400 *left_pos = f->output_data.x->left_pos;
7d0393cf 13401
d0fa3e56
EZ
13402 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13403 {
13404 int ph;
7d0393cf 13405
d0fa3e56
EZ
13406 ph = FRAME_X_DISPLAY_INFO (f)->height;
13407 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13408 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
13409 - f->output_data.x->y_pixels_diff;
13410 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13411 *top_pos = 0;
13412 }
13413
13414 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13415 {
13416 int pw;
7d0393cf 13417
d0fa3e56
EZ
13418 pw = FRAME_X_DISPLAY_INFO (f)->width;
13419 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13420 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
13421 - f->output_data.x->x_pixels_diff;
13422 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13423 *left_pos = 0;
13424 }
13425
13426 *width = newwidth;
13427 *height = newheight;
13428}
13429
dc6f92b8 13430
499b1844
GM
13431/* Change the size of frame F's X window to COLS/ROWS in the case F
13432 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
13433 top-left-corner window gravity for this size change and subsequent
13434 size changes. Otherwise we leave the window gravity unchanged. */
13435
13436static void
13437x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 13438 struct frame *f;
bc20ebbf 13439 int change_gravity;
b1c884c3 13440 int cols, rows;
dc6f92b8
JB
13441{
13442 int pixelwidth, pixelheight;
80fd1fe2 13443
b1c884c3 13444 check_frame_size (f, &rows, &cols);
7556890b 13445 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
13446 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
13447 ? 0
13448 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 13449 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 13450 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
976b73d7
KS
13451
13452 x_compute_fringe_widths (f, 0);
13453
f451eb13
JB
13454 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
13455 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 13456
7556890b 13457 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 13458 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 13459
334208b7
RS
13460 XSync (FRAME_X_DISPLAY (f), False);
13461 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13462 pixelwidth, pixelheight);
b1c884c3
JB
13463
13464 /* Now, strictly speaking, we can't be sure that this is accurate,
13465 but the window manager will get around to dealing with the size
13466 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
13467 ConfigureNotify event gets here.
13468
13469 We could just not bother storing any of this information here,
13470 and let the ConfigureNotify event set everything up, but that
fddd5ceb 13471 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 13472 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
13473 point in the future when the ConfigureNotify event arrives.
13474
13475 We pass 1 for DELAY since we can't run Lisp code inside of
13476 a BLOCK_INPUT. */
7d1e984f 13477 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
13478 PIXEL_WIDTH (f) = pixelwidth;
13479 PIXEL_HEIGHT (f) = pixelheight;
13480
aee9a898
RS
13481 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
13482 receive in the ConfigureNotify event; if we get what we asked
13483 for, then the event won't cause the screen to become garbaged, so
13484 we have to make sure to do it here. */
13485 SET_FRAME_GARBAGED (f);
13486
13487 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
13488}
13489
13490
13491/* Call this to change the size of frame F's x-window.
13492 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
13493 for this size change and subsequent size changes.
13494 Otherwise we leave the window gravity unchanged. */
aee9a898 13495
499b1844
GM
13496void
13497x_set_window_size (f, change_gravity, cols, rows)
13498 struct frame *f;
13499 int change_gravity;
13500 int cols, rows;
13501{
13502 BLOCK_INPUT;
13503
488dd4c4
JD
13504#ifdef USE_GTK
13505 if (FRAME_GTK_WIDGET (f))
13506 xg_frame_set_char_size (f, cols, rows);
13507 else
13508 x_set_window_size_1 (f, change_gravity, cols, rows);
13509#elif USE_X_TOOLKIT
7d0393cf 13510
f1f4d345 13511 if (f->output_data.x->widget != NULL)
499b1844
GM
13512 {
13513 /* The x and y position of the widget is clobbered by the
13514 call to XtSetValues within EmacsFrameSetCharSize.
13515 This is a real kludge, but I don't understand Xt so I can't
13516 figure out a correct fix. Can anyone else tell me? -- rms. */
13517 int xpos = f->output_data.x->widget->core.x;
13518 int ypos = f->output_data.x->widget->core.y;
13519 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13520 f->output_data.x->widget->core.x = xpos;
13521 f->output_data.x->widget->core.y = ypos;
13522 }
13523 else
13524 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 13525
499b1844 13526#else /* not USE_X_TOOLKIT */
7d0393cf 13527
499b1844 13528 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 13529
aee9a898
RS
13530#endif /* not USE_X_TOOLKIT */
13531
4d73d038 13532 /* If cursor was outside the new size, mark it as off. */
06a2c219 13533 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 13534
aee9a898 13535 /* Clear out any recollection of where the mouse highlighting was,
7d0393cf 13536 since it might be in a place that's outside the new frame size.
aee9a898
RS
13537 Actually checking whether it is outside is a pain in the neck,
13538 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 13539 cancel_mouse_face (f);
dbc4e1c1 13540
dc6f92b8
JB
13541 UNBLOCK_INPUT;
13542}
dc6f92b8 13543\f
d047c4eb 13544/* Mouse warping. */
dc6f92b8 13545
9b378208 13546void
f676886a
JB
13547x_set_mouse_position (f, x, y)
13548 struct frame *f;
dc6f92b8
JB
13549 int x, y;
13550{
13551 int pix_x, pix_y;
13552
7556890b
RS
13553 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13554 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
13555
13556 if (pix_x < 0) pix_x = 0;
13557 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13558
13559 if (pix_y < 0) pix_y = 0;
13560 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
13561
13562 BLOCK_INPUT;
dc6f92b8 13563
334208b7
RS
13564 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13565 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
13566 UNBLOCK_INPUT;
13567}
13568
9b378208
RS
13569/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13570
13571void
13572x_set_mouse_pixel_position (f, pix_x, pix_y)
13573 struct frame *f;
13574 int pix_x, pix_y;
13575{
13576 BLOCK_INPUT;
13577
334208b7
RS
13578 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13579 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
13580 UNBLOCK_INPUT;
13581}
d047c4eb
KH
13582\f
13583/* focus shifting, raising and lowering. */
9b378208 13584
dfcf069d 13585void
f676886a
JB
13586x_focus_on_frame (f)
13587 struct frame *f;
dc6f92b8 13588{
1fb20991 13589#if 0 /* This proves to be unpleasant. */
f676886a 13590 x_raise_frame (f);
1fb20991 13591#endif
6d4238f3
JB
13592#if 0
13593 /* I don't think that the ICCCM allows programs to do things like this
13594 without the interaction of the window manager. Whatever you end up
f676886a 13595 doing with this code, do it to x_unfocus_frame too. */
334208b7 13596 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 13597 RevertToPointerRoot, CurrentTime);
c118dd06 13598#endif /* ! 0 */
dc6f92b8
JB
13599}
13600
dfcf069d 13601void
f676886a
JB
13602x_unfocus_frame (f)
13603 struct frame *f;
dc6f92b8 13604{
6d4238f3 13605#if 0
f676886a 13606 /* Look at the remarks in x_focus_on_frame. */
0f941935 13607 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 13608 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 13609 RevertToPointerRoot, CurrentTime);
c118dd06 13610#endif /* ! 0 */
dc6f92b8
JB
13611}
13612
f676886a 13613/* Raise frame F. */
dc6f92b8 13614
dfcf069d 13615void
f676886a
JB
13616x_raise_frame (f)
13617 struct frame *f;
dc6f92b8 13618{
3a88c238 13619 if (f->async_visible)
dc6f92b8
JB
13620 {
13621 BLOCK_INPUT;
2436a4e4 13622 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 13623 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13624 UNBLOCK_INPUT;
13625 }
13626}
13627
f676886a 13628/* Lower frame F. */
dc6f92b8 13629
dfcf069d 13630void
f676886a
JB
13631x_lower_frame (f)
13632 struct frame *f;
dc6f92b8 13633{
3a88c238 13634 if (f->async_visible)
dc6f92b8
JB
13635 {
13636 BLOCK_INPUT;
2436a4e4 13637 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 13638 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13639 UNBLOCK_INPUT;
13640 }
13641}
13642
dbc4e1c1 13643static void
6b0442dc 13644XTframe_raise_lower (f, raise_flag)
dbc4e1c1 13645 FRAME_PTR f;
6b0442dc 13646 int raise_flag;
dbc4e1c1 13647{
6b0442dc 13648 if (raise_flag)
dbc4e1c1
JB
13649 x_raise_frame (f);
13650 else
13651 x_lower_frame (f);
13652}
d047c4eb
KH
13653\f
13654/* Change of visibility. */
dc6f92b8 13655
9382638d
KH
13656/* This tries to wait until the frame is really visible.
13657 However, if the window manager asks the user where to position
13658 the frame, this will return before the user finishes doing that.
13659 The frame will not actually be visible at that time,
13660 but it will become visible later when the window manager
13661 finishes with it. */
13662
dfcf069d 13663void
f676886a
JB
13664x_make_frame_visible (f)
13665 struct frame *f;
dc6f92b8 13666{
990ba854 13667 Lisp_Object type;
1aa6072f 13668 int original_top, original_left;
31be9251
GM
13669 int retry_count = 2;
13670
13671 retry:
dc6f92b8 13672
dc6f92b8 13673 BLOCK_INPUT;
dc6f92b8 13674
990ba854
RS
13675 type = x_icon_type (f);
13676 if (!NILP (type))
13677 x_bitmap_icon (f, type);
bdcd49ba 13678
f676886a 13679 if (! FRAME_VISIBLE_P (f))
90e65f07 13680 {
1aa6072f
RS
13681 /* We test FRAME_GARBAGED_P here to make sure we don't
13682 call x_set_offset a second time
13683 if we get to x_make_frame_visible a second time
13684 before the window gets really visible. */
13685 if (! FRAME_ICONIFIED_P (f)
13686 && ! f->output_data.x->asked_for_visible)
13687 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13688
13689 f->output_data.x->asked_for_visible = 1;
13690
90e65f07 13691 if (! EQ (Vx_no_window_manager, Qt))
f676886a 13692 x_wm_set_window_state (f, NormalState);
3afe33e7 13693#ifdef USE_X_TOOLKIT
d7a38a2e 13694 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13695 XtMapWidget (f->output_data.x->widget);
3afe33e7 13696#else /* not USE_X_TOOLKIT */
488dd4c4
JD
13697#ifdef USE_GTK
13698 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
7b76ca1c 13699 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
488dd4c4 13700#else
7f9c7f94 13701 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
488dd4c4 13702#endif /* not USE_GTK */
3afe33e7 13703#endif /* not USE_X_TOOLKIT */
0134a210
RS
13704#if 0 /* This seems to bring back scroll bars in the wrong places
13705 if the window configuration has changed. They seem
13706 to come back ok without this. */
ab648270 13707 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 13708 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 13709#endif
90e65f07 13710 }
dc6f92b8 13711
334208b7 13712 XFlush (FRAME_X_DISPLAY (f));
90e65f07 13713
0dacf791
RS
13714 /* Synchronize to ensure Emacs knows the frame is visible
13715 before we do anything else. We do this loop with input not blocked
13716 so that incoming events are handled. */
13717 {
13718 Lisp_Object frame;
12ce2351 13719 int count;
28c01ffe
RS
13720 /* This must be before UNBLOCK_INPUT
13721 since events that arrive in response to the actions above
13722 will set it when they are handled. */
13723 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
13724
13725 original_left = f->output_data.x->left_pos;
13726 original_top = f->output_data.x->top_pos;
c0a04927
RS
13727
13728 /* This must come after we set COUNT. */
13729 UNBLOCK_INPUT;
13730
2745e6c4 13731 /* We unblock here so that arriving X events are processed. */
1aa6072f 13732
dcb07ae9
RS
13733 /* Now move the window back to where it was "supposed to be".
13734 But don't do it if the gravity is negative.
13735 When the gravity is negative, this uses a position
28c01ffe
RS
13736 that is 3 pixels too low. Perhaps that's really the border width.
13737
13738 Don't do this if the window has never been visible before,
13739 because the window manager may choose the position
13740 and we don't want to override it. */
1aa6072f 13741
4d3f5d9a 13742 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 13743 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 13744 && previously_visible)
1aa6072f 13745 {
2745e6c4
RS
13746 Drawable rootw;
13747 int x, y;
13748 unsigned int width, height, border, depth;
7d0393cf 13749
1aa6072f 13750 BLOCK_INPUT;
9829ddba 13751
06a2c219
GM
13752 /* On some window managers (such as FVWM) moving an existing
13753 window, even to the same place, causes the window manager
13754 to introduce an offset. This can cause the window to move
13755 to an unexpected location. Check the geometry (a little
13756 slow here) and then verify that the window is in the right
13757 place. If the window is not in the right place, move it
13758 there, and take the potential window manager hit. */
2745e6c4
RS
13759 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13760 &rootw, &x, &y, &width, &height, &border, &depth);
13761
13762 if (original_left != x || original_top != y)
13763 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13764 original_left, original_top);
13765
1aa6072f
RS
13766 UNBLOCK_INPUT;
13767 }
9829ddba 13768
e0c1aef2 13769 XSETFRAME (frame, f);
c0a04927 13770
12ce2351
GM
13771 /* Wait until the frame is visible. Process X events until a
13772 MapNotify event has been seen, or until we think we won't get a
13773 MapNotify at all.. */
13774 for (count = input_signal_count + 10;
13775 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 13776 {
12ce2351 13777 /* Force processing of queued events. */
334208b7 13778 x_sync (f);
12ce2351
GM
13779
13780 /* Machines that do polling rather than SIGIO have been
13781 observed to go into a busy-wait here. So we'll fake an
13782 alarm signal to let the handler know that there's something
13783 to be read. We used to raise a real alarm, but it seems
13784 that the handler isn't always enabled here. This is
13785 probably a bug. */
8b2f8d4e 13786 if (input_polling_used ())
3b2fa4e6 13787 {
12ce2351
GM
13788 /* It could be confusing if a real alarm arrives while
13789 processing the fake one. Turn it off and let the
13790 handler reset it. */
3e71d8f2 13791 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
13792 int old_poll_suppress_count = poll_suppress_count;
13793 poll_suppress_count = 1;
13794 poll_for_input_1 ();
13795 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 13796 }
12ce2351
GM
13797
13798 /* See if a MapNotify event has been processed. */
13799 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 13800 }
31be9251
GM
13801
13802 /* 2000-09-28: In
13803
13804 (let ((f (selected-frame)))
13805 (iconify-frame f)
13806 (raise-frame f))
13807
13808 the frame is not raised with various window managers on
13809 FreeBSD, Linux and Solaris. It turns out that, for some
13810 unknown reason, the call to XtMapWidget is completely ignored.
13811 Mapping the widget a second time works. */
7d0393cf 13812
31be9251
GM
13813 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13814 goto retry;
0dacf791 13815 }
dc6f92b8
JB
13816}
13817
06a2c219 13818/* Change from mapped state to withdrawn state. */
dc6f92b8 13819
d047c4eb
KH
13820/* Make the frame visible (mapped and not iconified). */
13821
dfcf069d 13822void
f676886a
JB
13823x_make_frame_invisible (f)
13824 struct frame *f;
dc6f92b8 13825{
546e6d5b
RS
13826 Window window;
13827
546e6d5b 13828 /* Use the frame's outermost window, not the one we normally draw on. */
2436a4e4 13829 window = FRAME_OUTER_WINDOW (f);
dc6f92b8 13830
9319ae23 13831 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13832 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13833 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13834
5627c40e 13835#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 13836 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 13837 return;
5627c40e 13838#endif
dc6f92b8
JB
13839
13840 BLOCK_INPUT;
c118dd06 13841
af31d76f
RS
13842 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13843 that the current position of the window is user-specified, rather than
13844 program-specified, so that when the window is mapped again, it will be
13845 placed at the same location, without forcing the user to position it
13846 by hand again (they have already done that once for this window.) */
c32cdd9a 13847 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 13848
488dd4c4
JD
13849#ifdef USE_GTK
13850 if (FRAME_GTK_OUTER_WIDGET (f))
13851 {
13852 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
13853 goto out;
13854 }
13855#endif
13856
c118dd06
JB
13857#ifdef HAVE_X11R4
13858
334208b7
RS
13859 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13860 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
13861 {
13862 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13863 error ("Can't notify window manager of window withdrawal");
c118dd06 13864 }
c118dd06 13865#else /* ! defined (HAVE_X11R4) */
16bd92ea 13866
c118dd06 13867 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
13868 if (! EQ (Vx_no_window_manager, Qt))
13869 {
16bd92ea 13870 XEvent unmap;
dc6f92b8 13871
16bd92ea 13872 unmap.xunmap.type = UnmapNotify;
546e6d5b 13873 unmap.xunmap.window = window;
334208b7 13874 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 13875 unmap.xunmap.from_configure = False;
334208b7
RS
13876 if (! XSendEvent (FRAME_X_DISPLAY (f),
13877 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 13878 False,
06a2c219 13879 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
13880 &unmap))
13881 {
13882 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13883 error ("Can't notify window manager of withdrawal");
16bd92ea 13884 }
dc6f92b8
JB
13885 }
13886
16bd92ea 13887 /* Unmap the window ourselves. Cheeky! */
334208b7 13888 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 13889#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 13890
488dd4c4 13891 out:
5627c40e
RS
13892 /* We can't distinguish this from iconification
13893 just by the event that we get from the server.
13894 So we can't win using the usual strategy of letting
13895 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13896 and synchronize with the server to make sure we agree. */
13897 f->visible = 0;
13898 FRAME_ICONIFIED_P (f) = 0;
13899 f->async_visible = 0;
13900 f->async_iconified = 0;
13901
334208b7 13902 x_sync (f);
5627c40e 13903
dc6f92b8
JB
13904 UNBLOCK_INPUT;
13905}
13906
06a2c219 13907/* Change window state from mapped to iconified. */
dc6f92b8 13908
dfcf069d 13909void
f676886a
JB
13910x_iconify_frame (f)
13911 struct frame *f;
dc6f92b8 13912{
3afe33e7 13913 int result;
990ba854 13914 Lisp_Object type;
dc6f92b8 13915
9319ae23 13916 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13917 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13918 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13919
3a88c238 13920 if (f->async_iconified)
dc6f92b8
JB
13921 return;
13922
3afe33e7 13923 BLOCK_INPUT;
546e6d5b 13924
9af3143a
RS
13925 FRAME_SAMPLE_VISIBILITY (f);
13926
990ba854
RS
13927 type = x_icon_type (f);
13928 if (!NILP (type))
13929 x_bitmap_icon (f, type);
bdcd49ba 13930
488dd4c4
JD
13931#ifdef USE_GTK
13932 if (FRAME_GTK_OUTER_WIDGET (f))
13933 {
13934 if (! FRAME_VISIBLE_P (f))
13935 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
13936
13937 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
13938 f->iconified = 1;
13939 f->visible = 1;
13940 f->async_iconified = 1;
13941 f->async_visible = 0;
13942 UNBLOCK_INPUT;
13943 return;
13944 }
13945#endif
13946
bdcd49ba
RS
13947#ifdef USE_X_TOOLKIT
13948
546e6d5b
RS
13949 if (! FRAME_VISIBLE_P (f))
13950 {
13951 if (! EQ (Vx_no_window_manager, Qt))
13952 x_wm_set_window_state (f, IconicState);
13953 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13954 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
13955 /* The server won't give us any event to indicate
13956 that an invisible frame was changed to an icon,
13957 so we have to record it here. */
13958 f->iconified = 1;
1e6bc770 13959 f->visible = 1;
9cf30a30 13960 f->async_iconified = 1;
1e6bc770 13961 f->async_visible = 0;
546e6d5b
RS
13962 UNBLOCK_INPUT;
13963 return;
13964 }
13965
334208b7 13966 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 13967 XtWindow (f->output_data.x->widget),
334208b7 13968 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
13969 UNBLOCK_INPUT;
13970
13971 if (!result)
546e6d5b 13972 error ("Can't notify window manager of iconification");
3afe33e7
RS
13973
13974 f->async_iconified = 1;
1e6bc770
RS
13975 f->async_visible = 0;
13976
8c002a25
KH
13977
13978 BLOCK_INPUT;
334208b7 13979 XFlush (FRAME_X_DISPLAY (f));
8c002a25 13980 UNBLOCK_INPUT;
3afe33e7
RS
13981#else /* not USE_X_TOOLKIT */
13982
fd13dbb2
RS
13983 /* Make sure the X server knows where the window should be positioned,
13984 in case the user deiconifies with the window manager. */
13985 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 13986 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 13987
16bd92ea
JB
13988 /* Since we don't know which revision of X we're running, we'll use both
13989 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13990
13991 /* X11R4: send a ClientMessage to the window manager using the
13992 WM_CHANGE_STATE type. */
13993 {
13994 XEvent message;
58769bee 13995
c118dd06 13996 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 13997 message.xclient.type = ClientMessage;
334208b7 13998 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
13999 message.xclient.format = 32;
14000 message.xclient.data.l[0] = IconicState;
14001
334208b7
RS
14002 if (! XSendEvent (FRAME_X_DISPLAY (f),
14003 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
14004 False,
14005 SubstructureRedirectMask | SubstructureNotifyMask,
14006 &message))
dc6f92b8
JB
14007 {
14008 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 14009 error ("Can't notify window manager of iconification");
dc6f92b8 14010 }
16bd92ea 14011 }
dc6f92b8 14012
58769bee 14013 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
14014 IconicState. */
14015 x_wm_set_window_state (f, IconicState);
dc6f92b8 14016
a9c00105
RS
14017 if (!FRAME_VISIBLE_P (f))
14018 {
14019 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 14020 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
14021 }
14022
3a88c238 14023 f->async_iconified = 1;
1e6bc770 14024 f->async_visible = 0;
dc6f92b8 14025
334208b7 14026 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 14027 UNBLOCK_INPUT;
8c002a25 14028#endif /* not USE_X_TOOLKIT */
dc6f92b8 14029}
19f71add 14030
d047c4eb 14031\f
19f71add 14032/* Free X resources of frame F. */
dc6f92b8 14033
dfcf069d 14034void
19f71add 14035x_free_frame_resources (f)
f676886a 14036 struct frame *f;
dc6f92b8 14037{
7f9c7f94 14038 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
14039 Lisp_Object bar;
14040 struct scroll_bar *b;
7f9c7f94 14041
dc6f92b8 14042 BLOCK_INPUT;
c0ff3fab 14043
6186a4a0
RS
14044 /* If a display connection is dead, don't try sending more
14045 commands to the X server. */
19f71add 14046 if (dpyinfo->display)
6186a4a0 14047 {
19f71add 14048 if (f->output_data.x->icon_desc)
6186a4a0 14049 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
14050
14051#ifdef USE_X_TOOLKIT
14052 /* Explicitly destroy the scroll bars of the frame. Without
14053 this, we get "BadDrawable" errors from the toolkit later on,
14054 presumably from expose events generated for the disappearing
14055 toolkit scroll bars. */
14056 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
14057 {
14058 b = XSCROLL_BAR (bar);
14059 x_scroll_bar_remove (b);
14060 }
14061#endif
14062
31f41daf 14063#ifdef HAVE_X_I18N
f5d11644
GM
14064 if (FRAME_XIC (f))
14065 free_frame_xic (f);
31f41daf 14066#endif
c6ea2775 14067
3afe33e7 14068#ifdef USE_X_TOOLKIT
06a2c219 14069 if (f->output_data.x->widget)
30ca89f5
GM
14070 {
14071 XtDestroyWidget (f->output_data.x->widget);
14072 f->output_data.x->widget = NULL;
14073 }
c6ea2775
EZ
14074 /* Tooltips don't have widgets, only a simple X window, even if
14075 we are using a toolkit. */
14076 else if (FRAME_X_WINDOW (f))
14077 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
14078
6186a4a0 14079 free_frame_menubar (f);
c6ea2775 14080#else /* !USE_X_TOOLKIT */
488dd4c4
JD
14081
14082#ifdef USE_GTK
14083 /* In the GTK version, tooltips are normal X
14084 frames. We must check and free both types. */
14085 if (FRAME_GTK_OUTER_WIDGET (f))
14086 {
14087 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
14088 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
14089 FRAME_GTK_OUTER_WIDGET (f) = 0;
14090 }
14091#endif /* USE_GTK */
177c0ea7 14092
c6ea2775
EZ
14093 if (FRAME_X_WINDOW (f))
14094 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
14095#endif /* !USE_X_TOOLKIT */
3afe33e7 14096
3e71d8f2
GM
14097 unload_color (f, f->output_data.x->foreground_pixel);
14098 unload_color (f, f->output_data.x->background_pixel);
14099 unload_color (f, f->output_data.x->cursor_pixel);
14100 unload_color (f, f->output_data.x->cursor_foreground_pixel);
14101 unload_color (f, f->output_data.x->border_pixel);
14102 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 14103
3e71d8f2
GM
14104 if (f->output_data.x->scroll_bar_background_pixel != -1)
14105 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
14106 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
14107 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
14108#ifdef USE_TOOLKIT_SCROLL_BARS
14109 /* Scrollbar shadow colors. */
14110 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
14111 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
14112 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
14113 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
14114#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
14115 if (f->output_data.x->white_relief.allocated_p)
14116 unload_color (f, f->output_data.x->white_relief.pixel);
14117 if (f->output_data.x->black_relief.allocated_p)
14118 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 14119
19f71add
GM
14120 if (FRAME_FACE_CACHE (f))
14121 free_frame_faces (f);
7d0393cf 14122
4ca78676 14123 x_free_gcs (f);
6186a4a0
RS
14124 XFlush (FRAME_X_DISPLAY (f));
14125 }
dc6f92b8 14126
df89d8a4 14127 if (f->output_data.x->saved_menu_event)
06a2c219 14128 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 14129
7556890b 14130 xfree (f->output_data.x);
19f71add 14131 f->output_data.x = NULL;
7d0393cf 14132
0f941935
KH
14133 if (f == dpyinfo->x_focus_frame)
14134 dpyinfo->x_focus_frame = 0;
14135 if (f == dpyinfo->x_focus_event_frame)
14136 dpyinfo->x_focus_event_frame = 0;
14137 if (f == dpyinfo->x_highlight_frame)
14138 dpyinfo->x_highlight_frame = 0;
c0ff3fab 14139
7f9c7f94 14140 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 14141 {
7f9c7f94
RS
14142 dpyinfo->mouse_face_beg_row
14143 = dpyinfo->mouse_face_beg_col = -1;
14144 dpyinfo->mouse_face_end_row
14145 = dpyinfo->mouse_face_end_col = -1;
14146 dpyinfo->mouse_face_window = Qnil;
21323706
RS
14147 dpyinfo->mouse_face_deferred_gc = 0;
14148 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 14149 }
0134a210 14150
c0ff3fab 14151 UNBLOCK_INPUT;
dc6f92b8 14152}
19f71add
GM
14153
14154
14155/* Destroy the X window of frame F. */
14156
14157void
14158x_destroy_window (f)
14159 struct frame *f;
14160{
14161 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14162
14163 /* If a display connection is dead, don't try sending more
14164 commands to the X server. */
14165 if (dpyinfo->display != 0)
14166 x_free_frame_resources (f);
14167
14168 dpyinfo->reference_count--;
14169}
14170
dc6f92b8 14171\f
f451eb13
JB
14172/* Setting window manager hints. */
14173
af31d76f
RS
14174/* Set the normal size hints for the window manager, for frame F.
14175 FLAGS is the flags word to use--or 0 meaning preserve the flags
14176 that the window now has.
14177 If USER_POSITION is nonzero, we set the USPosition
488dd4c4
JD
14178 flag (this is useful when FLAGS is 0).
14179 The GTK version is in gtkutils.c */
6dba1858 14180
488dd4c4 14181#ifndef USE_GTK
dfcf069d 14182void
af31d76f 14183x_wm_set_size_hint (f, flags, user_position)
f676886a 14184 struct frame *f;
af31d76f
RS
14185 long flags;
14186 int user_position;
dc6f92b8
JB
14187{
14188 XSizeHints size_hints;
3afe33e7
RS
14189
14190#ifdef USE_X_TOOLKIT
7e4f2521
FP
14191 Arg al[2];
14192 int ac = 0;
14193 Dimension widget_width, widget_height;
488dd4c4 14194#endif
177c0ea7 14195
488dd4c4 14196 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 14197
b72a58fd
RS
14198 /* Setting PMaxSize caused various problems. */
14199 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 14200
7556890b
RS
14201 size_hints.x = f->output_data.x->left_pos;
14202 size_hints.y = f->output_data.x->top_pos;
7553a6b7 14203
7e4f2521
FP
14204#ifdef USE_X_TOOLKIT
14205 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
14206 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 14207 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
14208 size_hints.height = widget_height;
14209 size_hints.width = widget_width;
14210#else /* not USE_X_TOOLKIT */
f676886a
JB
14211 size_hints.height = PIXEL_HEIGHT (f);
14212 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 14213#endif /* not USE_X_TOOLKIT */
7553a6b7 14214
7556890b
RS
14215 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
14216 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
14217 size_hints.max_width
14218 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
14219 size_hints.max_height
14220 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 14221
d067ea8b
KH
14222 /* Calculate the base and minimum sizes.
14223
14224 (When we use the X toolkit, we don't do it here.
14225 Instead we copy the values that the widgets are using, below.) */
14226#ifndef USE_X_TOOLKIT
b1c884c3 14227 {
b0342f17 14228 int base_width, base_height;
0134a210 14229 int min_rows = 0, min_cols = 0;
b0342f17 14230
f451eb13
JB
14231 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
14232 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 14233
0134a210 14234 check_frame_size (f, &min_rows, &min_cols);
b0342f17 14235
0134a210
RS
14236 /* The window manager uses the base width hints to calculate the
14237 current number of rows and columns in the frame while
14238 resizing; min_width and min_height aren't useful for this
14239 purpose, since they might not give the dimensions for a
14240 zero-row, zero-column frame.
58769bee 14241
0134a210
RS
14242 We use the base_width and base_height members if we have
14243 them; otherwise, we set the min_width and min_height members
14244 to the size for a zero x zero frame. */
b0342f17
JB
14245
14246#ifdef HAVE_X11R4
0134a210
RS
14247 size_hints.flags |= PBaseSize;
14248 size_hints.base_width = base_width;
14249 size_hints.base_height = base_height;
14250 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
14251 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 14252#else
0134a210
RS
14253 size_hints.min_width = base_width;
14254 size_hints.min_height = base_height;
b0342f17 14255#endif
b1c884c3 14256 }
dc6f92b8 14257
d067ea8b 14258 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 14259 if (flags)
dc6f92b8 14260 {
d067ea8b
KH
14261 size_hints.flags |= flags;
14262 goto no_read;
14263 }
14264#endif /* not USE_X_TOOLKIT */
14265
14266 {
14267 XSizeHints hints; /* Sometimes I hate X Windows... */
14268 long supplied_return;
14269 int value;
af31d76f
RS
14270
14271#ifdef HAVE_X11R4
d067ea8b
KH
14272 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
14273 &supplied_return);
af31d76f 14274#else
d067ea8b 14275 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 14276#endif
58769bee 14277
d067ea8b
KH
14278#ifdef USE_X_TOOLKIT
14279 size_hints.base_height = hints.base_height;
14280 size_hints.base_width = hints.base_width;
14281 size_hints.min_height = hints.min_height;
14282 size_hints.min_width = hints.min_width;
14283#endif
14284
14285 if (flags)
14286 size_hints.flags |= flags;
14287 else
14288 {
14289 if (value == 0)
14290 hints.flags = 0;
14291 if (hints.flags & PSize)
14292 size_hints.flags |= PSize;
14293 if (hints.flags & PPosition)
14294 size_hints.flags |= PPosition;
14295 if (hints.flags & USPosition)
14296 size_hints.flags |= USPosition;
14297 if (hints.flags & USSize)
14298 size_hints.flags |= USSize;
14299 }
14300 }
14301
06a2c219 14302#ifndef USE_X_TOOLKIT
d067ea8b 14303 no_read:
06a2c219 14304#endif
0134a210 14305
af31d76f 14306#ifdef PWinGravity
7556890b 14307 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 14308 size_hints.flags |= PWinGravity;
dc05a16b 14309
af31d76f 14310 if (user_position)
6dba1858 14311 {
af31d76f
RS
14312 size_hints.flags &= ~ PPosition;
14313 size_hints.flags |= USPosition;
6dba1858 14314 }
2554751d 14315#endif /* PWinGravity */
6dba1858 14316
b0342f17 14317#ifdef HAVE_X11R4
334208b7 14318 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 14319#else
334208b7 14320 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 14321#endif
dc6f92b8 14322}
488dd4c4 14323#endif /* not USE_GTK */
dc6f92b8
JB
14324
14325/* Used for IconicState or NormalState */
06a2c219 14326
dfcf069d 14327void
f676886a
JB
14328x_wm_set_window_state (f, state)
14329 struct frame *f;
dc6f92b8
JB
14330 int state;
14331{
3afe33e7 14332#ifdef USE_X_TOOLKIT
546e6d5b
RS
14333 Arg al[1];
14334
14335 XtSetArg (al[0], XtNinitialState, state);
7556890b 14336 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 14337#else /* not USE_X_TOOLKIT */
c118dd06 14338 Window window = FRAME_X_WINDOW (f);
dc6f92b8 14339
7556890b
RS
14340 f->output_data.x->wm_hints.flags |= StateHint;
14341 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 14342
7556890b 14343 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 14344#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
14345}
14346
dfcf069d 14347void
7f2ae036 14348x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 14349 struct frame *f;
7f2ae036 14350 int pixmap_id;
dc6f92b8 14351{
d2bd6bc4
RS
14352 Pixmap icon_pixmap;
14353
06a2c219 14354#ifndef USE_X_TOOLKIT
488dd4c4 14355 Window window = FRAME_OUTER_WINDOW (f);
75231bad 14356#endif
dc6f92b8 14357
7f2ae036 14358 if (pixmap_id > 0)
dbc4e1c1 14359 {
d2bd6bc4 14360 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 14361 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
14362 }
14363 else
68568555
RS
14364 {
14365 /* It seems there is no way to turn off use of an icon pixmap.
14366 The following line does it, only if no icon has yet been created,
14367 for some window managers. But with mwm it crashes.
14368 Some people say it should clear the IconPixmapHint bit in this case,
14369 but that doesn't work, and the X consortium said it isn't the
14370 right thing at all. Since there is no way to win,
14371 best to explicitly give up. */
14372#if 0
14373 f->output_data.x->wm_hints.icon_pixmap = None;
14374#else
14375 return;
14376#endif
14377 }
b1c884c3 14378
d2bd6bc4
RS
14379#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
14380
14381 {
14382 Arg al[1];
14383 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
14384 XtSetValues (f->output_data.x->widget, al, 1);
14385 }
14386
14387#else /* not USE_X_TOOLKIT */
7d0393cf 14388
7556890b
RS
14389 f->output_data.x->wm_hints.flags |= IconPixmapHint;
14390 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
14391
14392#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
14393}
14394
dfcf069d 14395void
f676886a
JB
14396x_wm_set_icon_position (f, icon_x, icon_y)
14397 struct frame *f;
dc6f92b8
JB
14398 int icon_x, icon_y;
14399{
2436a4e4 14400 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 14401
7556890b
RS
14402 f->output_data.x->wm_hints.flags |= IconPositionHint;
14403 f->output_data.x->wm_hints.icon_x = icon_x;
14404 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 14405
7556890b 14406 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
14407}
14408
14409\f
06a2c219
GM
14410/***********************************************************************
14411 Fonts
14412 ***********************************************************************/
dc43ef94
KH
14413
14414/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 14415
dc43ef94
KH
14416struct font_info *
14417x_get_font_info (f, font_idx)
14418 FRAME_PTR f;
14419 int font_idx;
14420{
14421 return (FRAME_X_FONT_TABLE (f) + font_idx);
14422}
14423
14424
9c11f79e
GM
14425/* Return a list of names of available fonts matching PATTERN on frame F.
14426
14427 If SIZE is > 0, it is the size (maximum bounds width) of fonts
14428 to be listed.
14429
14430 SIZE < 0 means include scalable fonts.
14431
14432 Frame F null means we have not yet created any frame on X, and
14433 consult the first display in x_display_list. MAXNAMES sets a limit
14434 on how many fonts to match. */
dc43ef94
KH
14435
14436Lisp_Object
14437x_list_fonts (f, pattern, size, maxnames)
9c11f79e 14438 struct frame *f;
dc43ef94
KH
14439 Lisp_Object pattern;
14440 int size;
14441 int maxnames;
14442{
06a2c219
GM
14443 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
14444 Lisp_Object tem, second_best;
9c11f79e
GM
14445 struct x_display_info *dpyinfo
14446 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
14447 Display *dpy = dpyinfo->display;
09c6077f 14448 int try_XLoadQueryFont = 0;
53ca4657 14449 int count;
9c11f79e
GM
14450 int allow_scalable_fonts_p = 0;
14451
14452 if (size < 0)
14453 {
14454 allow_scalable_fonts_p = 1;
14455 size = 0;
14456 }
dc43ef94 14457
6b0efe73 14458 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
14459 if (NILP (patterns))
14460 patterns = Fcons (pattern, Qnil);
81ba44e5 14461
09c6077f
KH
14462 if (maxnames == 1 && !size)
14463 /* We can return any single font matching PATTERN. */
14464 try_XLoadQueryFont = 1;
9a32686f 14465
8e713be6 14466 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 14467 {
dc43ef94 14468 int num_fonts;
3e71d8f2 14469 char **names = NULL;
dc43ef94 14470
8e713be6 14471 pattern = XCAR (patterns);
536f4067
RS
14472 /* See if we cached the result for this particular query.
14473 The cache is an alist of the form:
9c11f79e
GM
14474 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
14475 tem = XCDR (dpyinfo->name_list_element);
14476 key = Fcons (Fcons (pattern, make_number (maxnames)),
14477 allow_scalable_fonts_p ? Qt : Qnil);
14478 list = Fassoc (key, tem);
14479 if (!NILP (list))
b5210ea7
KH
14480 {
14481 list = Fcdr_safe (list);
14482 /* We have a cashed list. Don't have to get the list again. */
14483 goto label_cached;
14484 }
14485
14486 /* At first, put PATTERN in the cache. */
09c6077f 14487
dc43ef94 14488 BLOCK_INPUT;
17d85edc
KH
14489 count = x_catch_errors (dpy);
14490
09c6077f
KH
14491 if (try_XLoadQueryFont)
14492 {
14493 XFontStruct *font;
14494 unsigned long value;
14495
d5db4077 14496 font = XLoadQueryFont (dpy, SDATA (pattern));
17d85edc
KH
14497 if (x_had_errors_p (dpy))
14498 {
14499 /* This error is perhaps due to insufficient memory on X
14500 server. Let's just ignore it. */
14501 font = NULL;
14502 x_clear_errors (dpy);
14503 }
14504
09c6077f
KH
14505 if (font
14506 && XGetFontProperty (font, XA_FONT, &value))
14507 {
14508 char *name = (char *) XGetAtomName (dpy, (Atom) value);
14509 int len = strlen (name);
01c752b5 14510 char *tmp;
09c6077f 14511
6f6512e8
KH
14512 /* If DXPC (a Differential X Protocol Compressor)
14513 Ver.3.7 is running, XGetAtomName will return null
14514 string. We must avoid such a name. */
14515 if (len == 0)
14516 try_XLoadQueryFont = 0;
14517 else
14518 {
14519 num_fonts = 1;
14520 names = (char **) alloca (sizeof (char *));
14521 /* Some systems only allow alloca assigned to a
14522 simple var. */
14523 tmp = (char *) alloca (len + 1); names[0] = tmp;
14524 bcopy (name, names[0], len + 1);
14525 XFree (name);
14526 }
09c6077f
KH
14527 }
14528 else
14529 try_XLoadQueryFont = 0;
a083fd23
RS
14530
14531 if (font)
14532 XFreeFont (dpy, font);
09c6077f
KH
14533 }
14534
14535 if (!try_XLoadQueryFont)
17d85edc
KH
14536 {
14537 /* We try at least 10 fonts because XListFonts will return
14538 auto-scaled fonts at the head. */
d5db4077 14539 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
17d85edc
KH
14540 &num_fonts);
14541 if (x_had_errors_p (dpy))
14542 {
14543 /* This error is perhaps due to insufficient memory on X
14544 server. Let's just ignore it. */
14545 names = NULL;
14546 x_clear_errors (dpy);
14547 }
14548 }
14549
14550 x_uncatch_errors (dpy, count);
dc43ef94
KH
14551 UNBLOCK_INPUT;
14552
14553 if (names)
14554 {
14555 int i;
dc43ef94
KH
14556
14557 /* Make a list of all the fonts we got back.
14558 Store that in the font cache for the display. */
14559 for (i = 0; i < num_fonts; i++)
14560 {
06a2c219 14561 int width = 0;
dc43ef94 14562 char *p = names[i];
06a2c219 14563 int average_width = -1, dashes = 0;
7d0393cf 14564
dc43ef94 14565 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
14566 14 dashes, and the field value following 12th dash
14567 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14568 is usually too ugly to be used for editing. Let's
14569 ignore it. */
dc43ef94
KH
14570 while (*p)
14571 if (*p++ == '-')
14572 {
14573 dashes++;
14574 if (dashes == 7) /* PIXEL_SIZE field */
14575 width = atoi (p);
14576 else if (dashes == 12) /* AVERAGE_WIDTH field */
14577 average_width = atoi (p);
14578 }
7d0393cf 14579
9c11f79e
GM
14580 if (allow_scalable_fonts_p
14581 || dashes < 14 || average_width != 0)
dc43ef94
KH
14582 {
14583 tem = build_string (names[i]);
14584 if (NILP (Fassoc (tem, list)))
14585 {
14586 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
14587 && ((fast_c_string_match_ignore_case
14588 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
14589 >= 0))
14590 /* We can set the value of PIXEL_SIZE to the
b5210ea7 14591 width of this font. */
dc43ef94
KH
14592 list = Fcons (Fcons (tem, make_number (width)), list);
14593 else
14594 /* For the moment, width is not known. */
14595 list = Fcons (Fcons (tem, Qnil), list);
14596 }
14597 }
14598 }
7d0393cf 14599
09c6077f 14600 if (!try_XLoadQueryFont)
e38f4136
GM
14601 {
14602 BLOCK_INPUT;
14603 XFreeFontNames (names);
14604 UNBLOCK_INPUT;
14605 }
dc43ef94
KH
14606 }
14607
b5210ea7 14608 /* Now store the result in the cache. */
f3fbd155
KR
14609 XSETCDR (dpyinfo->name_list_element,
14610 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 14611
b5210ea7
KH
14612 label_cached:
14613 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 14614
b5210ea7
KH
14615 newlist = second_best = Qnil;
14616 /* Make a list of the fonts that have the right width. */
8e713be6 14617 for (; CONSP (list); list = XCDR (list))
b5210ea7 14618 {
536f4067
RS
14619 int found_size;
14620
8e713be6 14621 tem = XCAR (list);
dc43ef94 14622
8e713be6 14623 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
14624 continue;
14625 if (!size)
14626 {
8e713be6 14627 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
14628 continue;
14629 }
dc43ef94 14630
8e713be6 14631 if (!INTEGERP (XCDR (tem)))
dc43ef94 14632 {
b5210ea7 14633 /* Since we have not yet known the size of this font, we
9c11f79e 14634 must try slow function call XLoadQueryFont. */
dc43ef94
KH
14635 XFontStruct *thisinfo;
14636
14637 BLOCK_INPUT;
17d85edc 14638 count = x_catch_errors (dpy);
dc43ef94 14639 thisinfo = XLoadQueryFont (dpy,
d5db4077 14640 SDATA (XCAR (tem)));
17d85edc
KH
14641 if (x_had_errors_p (dpy))
14642 {
14643 /* This error is perhaps due to insufficient memory on X
14644 server. Let's just ignore it. */
14645 thisinfo = NULL;
14646 x_clear_errors (dpy);
14647 }
14648 x_uncatch_errors (dpy, count);
dc43ef94
KH
14649 UNBLOCK_INPUT;
14650
14651 if (thisinfo)
14652 {
f3fbd155
KR
14653 XSETCDR (tem,
14654 (thisinfo->min_bounds.width == 0
14655 ? make_number (0)
14656 : make_number (thisinfo->max_bounds.width)));
e38f4136 14657 BLOCK_INPUT;
dc43ef94 14658 XFreeFont (dpy, thisinfo);
e38f4136 14659 UNBLOCK_INPUT;
dc43ef94
KH
14660 }
14661 else
b5210ea7 14662 /* For unknown reason, the previous call of XListFont had
06a2c219 14663 returned a font which can't be opened. Record the size
b5210ea7 14664 as 0 not to try to open it again. */
f3fbd155 14665 XSETCDR (tem, make_number (0));
dc43ef94 14666 }
536f4067 14667
8e713be6 14668 found_size = XINT (XCDR (tem));
536f4067 14669 if (found_size == size)
8e713be6 14670 newlist = Fcons (XCAR (tem), newlist);
536f4067 14671 else if (found_size > 0)
b5210ea7 14672 {
536f4067 14673 if (NILP (second_best))
b5210ea7 14674 second_best = tem;
536f4067
RS
14675 else if (found_size < size)
14676 {
8e713be6
KR
14677 if (XINT (XCDR (second_best)) > size
14678 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
14679 second_best = tem;
14680 }
14681 else
14682 {
8e713be6
KR
14683 if (XINT (XCDR (second_best)) > size
14684 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
14685 second_best = tem;
14686 }
b5210ea7
KH
14687 }
14688 }
14689 if (!NILP (newlist))
14690 break;
14691 else if (!NILP (second_best))
14692 {
8e713be6 14693 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 14694 break;
dc43ef94 14695 }
dc43ef94
KH
14696 }
14697
14698 return newlist;
7d0393cf 14699}
dc43ef94 14700
06a2c219
GM
14701
14702#if GLYPH_DEBUG
14703
14704/* Check that FONT is valid on frame F. It is if it can be found in F's
14705 font table. */
14706
14707static void
14708x_check_font (f, font)
14709 struct frame *f;
14710 XFontStruct *font;
14711{
14712 int i;
14713 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14714
14715 xassert (font != NULL);
14716
14717 for (i = 0; i < dpyinfo->n_fonts; i++)
7d0393cf 14718 if (dpyinfo->font_table[i].name
06a2c219
GM
14719 && font == dpyinfo->font_table[i].font)
14720 break;
14721
14722 xassert (i < dpyinfo->n_fonts);
14723}
14724
14725#endif /* GLYPH_DEBUG != 0 */
14726
14727/* Set *W to the minimum width, *H to the minimum font height of FONT.
14728 Note: There are (broken) X fonts out there with invalid XFontStruct
14729 min_bounds contents. For example, handa@etl.go.jp reports that
14730 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14731 have font->min_bounds.width == 0. */
14732
14733static INLINE void
14734x_font_min_bounds (font, w, h)
14735 XFontStruct *font;
14736 int *w, *h;
14737{
14738 *h = FONT_HEIGHT (font);
14739 *w = font->min_bounds.width;
14740
14741 /* Try to handle the case where FONT->min_bounds has invalid
14742 contents. Since the only font known to have invalid min_bounds
14743 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14744 if (*w <= 0)
14745 *w = font->max_bounds.width;
14746}
14747
14748
14749/* Compute the smallest character width and smallest font height over
14750 all fonts available on frame F. Set the members smallest_char_width
14751 and smallest_font_height in F's x_display_info structure to
14752 the values computed. Value is non-zero if smallest_font_height or
14753 smallest_char_width become smaller than they were before. */
14754
14755static int
14756x_compute_min_glyph_bounds (f)
14757 struct frame *f;
14758{
14759 int i;
14760 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14761 XFontStruct *font;
14762 int old_width = dpyinfo->smallest_char_width;
14763 int old_height = dpyinfo->smallest_font_height;
7d0393cf 14764
06a2c219
GM
14765 dpyinfo->smallest_font_height = 100000;
14766 dpyinfo->smallest_char_width = 100000;
7d0393cf 14767
06a2c219
GM
14768 for (i = 0; i < dpyinfo->n_fonts; ++i)
14769 if (dpyinfo->font_table[i].name)
14770 {
14771 struct font_info *fontp = dpyinfo->font_table + i;
14772 int w, h;
7d0393cf 14773
06a2c219
GM
14774 font = (XFontStruct *) fontp->font;
14775 xassert (font != (XFontStruct *) ~0);
14776 x_font_min_bounds (font, &w, &h);
7d0393cf 14777
06a2c219
GM
14778 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14779 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14780 }
14781
14782 xassert (dpyinfo->smallest_char_width > 0
14783 && dpyinfo->smallest_font_height > 0);
14784
14785 return (dpyinfo->n_fonts == 1
14786 || dpyinfo->smallest_char_width < old_width
14787 || dpyinfo->smallest_font_height < old_height);
14788}
14789
14790
dc43ef94
KH
14791/* Load font named FONTNAME of the size SIZE for frame F, and return a
14792 pointer to the structure font_info while allocating it dynamically.
14793 If SIZE is 0, load any size of font.
14794 If loading is failed, return NULL. */
14795
14796struct font_info *
14797x_load_font (f, fontname, size)
14798 struct frame *f;
14799 register char *fontname;
14800 int size;
14801{
14802 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14803 Lisp_Object font_names;
d645aaa4 14804 int count;
dc43ef94
KH
14805
14806 /* Get a list of all the fonts that match this name. Once we
14807 have a list of matching fonts, we compare them against the fonts
14808 we already have by comparing names. */
09c6077f 14809 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
14810
14811 if (!NILP (font_names))
14812 {
14813 Lisp_Object tail;
14814 int i;
14815
14816 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 14817 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
14818 if (dpyinfo->font_table[i].name
14819 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 14820 SDATA (XCAR (tail)))
06a2c219 14821 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 14822 SDATA (XCAR (tail)))))
dc43ef94
KH
14823 return (dpyinfo->font_table + i);
14824 }
14825
14826 /* Load the font and add it to the table. */
14827 {
14828 char *full_name;
14829 XFontStruct *font;
14830 struct font_info *fontp;
14831 unsigned long value;
06a2c219 14832 int i;
dc43ef94 14833
2da424f1
KH
14834 /* If we have found fonts by x_list_font, load one of them. If
14835 not, we still try to load a font by the name given as FONTNAME
14836 because XListFonts (called in x_list_font) of some X server has
14837 a bug of not finding a font even if the font surely exists and
14838 is loadable by XLoadQueryFont. */
e1d6d5b9 14839 if (size > 0 && !NILP (font_names))
d5db4077 14840 fontname = (char *) SDATA (XCAR (font_names));
dc43ef94
KH
14841
14842 BLOCK_INPUT;
d645aaa4 14843 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 14844 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
14845 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14846 {
14847 /* This error is perhaps due to insufficient memory on X
14848 server. Let's just ignore it. */
14849 font = NULL;
14850 x_clear_errors (FRAME_X_DISPLAY (f));
14851 }
14852 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 14853 UNBLOCK_INPUT;
b5210ea7 14854 if (!font)
dc43ef94
KH
14855 return NULL;
14856
06a2c219
GM
14857 /* Find a free slot in the font table. */
14858 for (i = 0; i < dpyinfo->n_fonts; ++i)
14859 if (dpyinfo->font_table[i].name == NULL)
14860 break;
14861
14862 /* If no free slot found, maybe enlarge the font table. */
14863 if (i == dpyinfo->n_fonts
14864 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 14865 {
06a2c219
GM
14866 int sz;
14867 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14868 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 14869 dpyinfo->font_table
06a2c219 14870 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
14871 }
14872
06a2c219
GM
14873 fontp = dpyinfo->font_table + i;
14874 if (i == dpyinfo->n_fonts)
14875 ++dpyinfo->n_fonts;
dc43ef94
KH
14876
14877 /* Now fill in the slots of *FONTP. */
14878 BLOCK_INPUT;
14879 fontp->font = font;
06a2c219 14880 fontp->font_idx = i;
dc43ef94
KH
14881 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14882 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14883
14884 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14885 full_name = 0;
14886 if (XGetFontProperty (font, XA_FONT, &value))
14887 {
14888 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14889 char *p = name;
14890 int dashes = 0;
14891
14892 /* Count the number of dashes in the "full name".
14893 If it is too few, this isn't really the font's full name,
14894 so don't use it.
14895 In X11R4, the fonts did not come with their canonical names
14896 stored in them. */
14897 while (*p)
14898 {
14899 if (*p == '-')
14900 dashes++;
14901 p++;
14902 }
14903
14904 if (dashes >= 13)
14905 {
14906 full_name = (char *) xmalloc (p - name + 1);
14907 bcopy (name, full_name, p - name + 1);
14908 }
14909
14910 XFree (name);
14911 }
7d0393cf 14912
dc43ef94
KH
14913 if (full_name != 0)
14914 fontp->full_name = full_name;
14915 else
14916 fontp->full_name = fontp->name;
14917
14918 fontp->size = font->max_bounds.width;
d5749adb 14919 fontp->height = FONT_HEIGHT (font);
dc43ef94 14920
2da424f1
KH
14921 if (NILP (font_names))
14922 {
14923 /* We come here because of a bug of XListFonts mentioned at
14924 the head of this block. Let's store this information in
14925 the cache for x_list_fonts. */
14926 Lisp_Object lispy_name = build_string (fontname);
14927 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
14928 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14929 Qnil);
2da424f1 14930
f3fbd155
KR
14931 XSETCDR (dpyinfo->name_list_element,
14932 Fcons (Fcons (key,
14933 Fcons (Fcons (lispy_full_name,
14934 make_number (fontp->size)),
14935 Qnil)),
14936 XCDR (dpyinfo->name_list_element)));
2da424f1 14937 if (full_name)
9c11f79e
GM
14938 {
14939 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14940 Qnil);
f3fbd155
KR
14941 XSETCDR (dpyinfo->name_list_element,
14942 Fcons (Fcons (key,
14943 Fcons (Fcons (lispy_full_name,
14944 make_number (fontp->size)),
14945 Qnil)),
14946 XCDR (dpyinfo->name_list_element)));
9c11f79e 14947 }
2da424f1
KH
14948 }
14949
dc43ef94
KH
14950 /* The slot `encoding' specifies how to map a character
14951 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
14952 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14953 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 14954 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 14955 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
14956 which is never used by any charset. If mapping can't be
14957 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
14958 fontp->encoding[1]
14959 = (font->max_byte1 == 0
14960 /* 1-byte font */
14961 ? (font->min_char_or_byte2 < 0x80
14962 ? (font->max_char_or_byte2 < 0x80
14963 ? 0 /* 0x20..0x7F */
8ff102bd 14964 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
14965 : 1) /* 0xA0..0xFF */
14966 /* 2-byte font */
14967 : (font->min_byte1 < 0x80
14968 ? (font->max_byte1 < 0x80
14969 ? (font->min_char_or_byte2 < 0x80
14970 ? (font->max_char_or_byte2 < 0x80
14971 ? 0 /* 0x2020..0x7F7F */
8ff102bd 14972 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 14973 : 3) /* 0x20A0..0x7FFF */
8ff102bd 14974 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
14975 : (font->min_char_or_byte2 < 0x80
14976 ? (font->max_char_or_byte2 < 0x80
14977 ? 2 /* 0xA020..0xFF7F */
8ff102bd 14978 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
14979 : 1))); /* 0xA0A0..0xFFFF */
14980
14981 fontp->baseline_offset
14982 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14983 ? (long) value : 0);
14984 fontp->relative_compose
14985 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14986 ? (long) value : 0);
f78798df
KH
14987 fontp->default_ascent
14988 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14989 ? (long) value : 0);
dc43ef94 14990
06a2c219
GM
14991 /* Set global flag fonts_changed_p to non-zero if the font loaded
14992 has a character with a smaller width than any other character
437dfb9f 14993 before, or if the font loaded has a smaller height than any
06a2c219
GM
14994 other font loaded before. If this happens, it will make a
14995 glyph matrix reallocation necessary. */
437dfb9f 14996 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 14997 UNBLOCK_INPUT;
dc43ef94
KH
14998 return fontp;
14999 }
15000}
15001
06a2c219
GM
15002
15003/* Return a pointer to struct font_info of a font named FONTNAME for
15004 frame F. If no such font is loaded, return NULL. */
15005
dc43ef94
KH
15006struct font_info *
15007x_query_font (f, fontname)
15008 struct frame *f;
15009 register char *fontname;
15010{
15011 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
15012 int i;
15013
15014 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
15015 if (dpyinfo->font_table[i].name
15016 && (!strcmp (dpyinfo->font_table[i].name, fontname)
15017 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
15018 return (dpyinfo->font_table + i);
15019 return NULL;
15020}
15021
06a2c219
GM
15022
15023/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
15024 `encoder' of the structure. */
15025
15026void
15027x_find_ccl_program (fontp)
15028 struct font_info *fontp;
15029{
a42f54e6 15030 Lisp_Object list, elt;
a6582676 15031
f9b5db02 15032 elt = Qnil;
8e713be6 15033 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 15034 {
8e713be6 15035 elt = XCAR (list);
a6582676 15036 if (CONSP (elt)
8e713be6 15037 && STRINGP (XCAR (elt))
9f2feff6
KH
15038 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
15039 >= 0)
15040 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
15041 >= 0)))
a42f54e6
KH
15042 break;
15043 }
7d0393cf 15044
a42f54e6
KH
15045 if (! NILP (list))
15046 {
d27f8ca7
KH
15047 struct ccl_program *ccl
15048 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 15049
8e713be6 15050 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
15051 xfree (ccl);
15052 else
15053 fontp->font_encoder = ccl;
a6582676
KH
15054 }
15055}
15056
06a2c219 15057
dc43ef94 15058\f
06a2c219
GM
15059/***********************************************************************
15060 Initialization
15061 ***********************************************************************/
f451eb13 15062
3afe33e7
RS
15063#ifdef USE_X_TOOLKIT
15064static XrmOptionDescRec emacs_options[] = {
15065 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
15066 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
15067
15068 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
15069 XrmoptionSepArg, NULL},
15070 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
15071
15072 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
15073 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
15074 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
15075 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
15076 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
15077 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
15078 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
15079};
15080#endif /* USE_X_TOOLKIT */
15081
7a13e894
RS
15082static int x_initialized;
15083
29b38361
KH
15084#ifdef MULTI_KBOARD
15085/* Test whether two display-name strings agree up to the dot that separates
15086 the screen number from the server number. */
15087static int
15088same_x_server (name1, name2)
59ab1dc7 15089 const char *name1, *name2;
29b38361
KH
15090{
15091 int seen_colon = 0;
59ab1dc7 15092 const unsigned char *system_name = SDATA (Vsystem_name);
cf591cc1
RS
15093 int system_name_length = strlen (system_name);
15094 int length_until_period = 0;
15095
15096 while (system_name[length_until_period] != 0
15097 && system_name[length_until_period] != '.')
15098 length_until_period++;
15099
15100 /* Treat `unix' like an empty host name. */
15101 if (! strncmp (name1, "unix:", 5))
15102 name1 += 4;
15103 if (! strncmp (name2, "unix:", 5))
15104 name2 += 4;
15105 /* Treat this host's name like an empty host name. */
15106 if (! strncmp (name1, system_name, system_name_length)
15107 && name1[system_name_length] == ':')
15108 name1 += system_name_length;
15109 if (! strncmp (name2, system_name, system_name_length)
15110 && name2[system_name_length] == ':')
15111 name2 += system_name_length;
15112 /* Treat this host's domainless name like an empty host name. */
15113 if (! strncmp (name1, system_name, length_until_period)
15114 && name1[length_until_period] == ':')
15115 name1 += length_until_period;
15116 if (! strncmp (name2, system_name, length_until_period)
15117 && name2[length_until_period] == ':')
15118 name2 += length_until_period;
15119
29b38361
KH
15120 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
15121 {
15122 if (*name1 == ':')
15123 seen_colon++;
15124 if (seen_colon && *name1 == '.')
15125 return 1;
15126 }
15127 return (seen_colon
15128 && (*name1 == '.' || *name1 == '\0')
15129 && (*name2 == '.' || *name2 == '\0'));
15130}
15131#endif
15132
334208b7 15133struct x_display_info *
1f8255f2 15134x_term_init (display_name, xrm_option, resource_name)
334208b7 15135 Lisp_Object display_name;
1f8255f2
RS
15136 char *xrm_option;
15137 char *resource_name;
dc6f92b8 15138{
334208b7 15139 int connection;
7a13e894 15140 Display *dpy;
334208b7
RS
15141 struct x_display_info *dpyinfo;
15142 XrmDatabase xrdb;
15143
60439948
KH
15144 BLOCK_INPUT;
15145
7a13e894
RS
15146 if (!x_initialized)
15147 {
15148 x_initialize ();
15149 x_initialized = 1;
15150 }
dc6f92b8 15151
488dd4c4
JD
15152#ifdef USE_GTK
15153 {
15154#define NUM_ARGV 10
15155 int argc;
15156 char *argv[NUM_ARGV];
15157 char **argv2 = argv;
15158 GdkAtom atom;
15159
15160 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
15161 than one, but this remains to be implemented. */
15162 if (x_initialized > 1)
15163 return 0;
15164
15165 x_initialized++;
15166
15167 for (argc = 0; argc < NUM_ARGV; ++argc)
15168 argv[argc] = 0;
15169
15170 argc = 0;
15171 argv[argc++] = initial_argv[0];
15172
15173 if (! NILP (display_name))
15174 {
15175 argv[argc++] = "--display";
15176 argv[argc++] = SDATA (display_name);
15177 }
15178
15179 argv[argc++] = "--name";
15180 argv[argc++] = resource_name;
177c0ea7 15181
1fcfb866
JD
15182#ifdef HAVE_X11R5
15183 XSetLocaleModifiers ("");
15184#endif
15185
488dd4c4
JD
15186 gtk_init (&argc, &argv2);
15187
15188 /* gtk_init does set_locale. We must fix locale after calling it. */
15189 fixup_locale ();
15190 xg_initialize ();
15191
15192 dpy = GDK_DISPLAY ();
177c0ea7 15193
488dd4c4
JD
15194 /* NULL window -> events for all windows go to our function */
15195 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
15196
15197 /* Load our own gtkrc if it exists. */
15198 {
15199 struct gcpro gcpro1, gcpro2;
15200 char *file = "~/.emacs.d/gtkrc";
15201 Lisp_Object s, abs_file;
15202
15203 GCPRO2 (str, abs_file);
15204 s = make_string (file, strlen (file));
15205 abs_file = Fexpand_file_name(s, Qnil);
15206
15207 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
15208 gtk_rc_parse (SDATA (abs_file));
177c0ea7 15209
488dd4c4
JD
15210 UNGCPRO;
15211 }
177c0ea7 15212
488dd4c4
JD
15213 XSetErrorHandler (x_error_handler);
15214 XSetIOErrorHandler (x_io_error_quitter);
15215 }
15216#else /* not USE_GTK */
3afe33e7 15217#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
15218 /* weiner@footloose.sps.mot.com reports that this causes
15219 errors with X11R5:
15220 X protocol error: BadAtom (invalid Atom parameter)
15221 on protocol request 18skiloaf.
15222 So let's not use it until R6. */
15223#ifdef HAVE_X11XTR6
bdcd49ba
RS
15224 XtSetLanguageProc (NULL, NULL, NULL);
15225#endif
15226
7f9c7f94
RS
15227 {
15228 int argc = 0;
15229 char *argv[3];
15230
15231 argv[0] = "";
15232 argc = 1;
15233 if (xrm_option)
15234 {
15235 argv[argc++] = "-xrm";
15236 argv[argc++] = xrm_option;
15237 }
bc4e60fc 15238 turn_on_atimers (0);
d5db4077 15239 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
7f9c7f94
RS
15240 resource_name, EMACS_CLASS,
15241 emacs_options, XtNumber (emacs_options),
15242 &argc, argv);
bc4e60fc 15243 turn_on_atimers (1);
39d8bb4d
KH
15244
15245#ifdef HAVE_X11XTR6
10537cb1 15246 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 15247 fixup_locale ();
39d8bb4d 15248#endif
7f9c7f94 15249 }
3afe33e7
RS
15250
15251#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
15252#ifdef HAVE_X11R5
15253 XSetLocaleModifiers ("");
15254#endif
d5db4077 15255 dpy = XOpenDisplay (SDATA (display_name));
3afe33e7 15256#endif /* not USE_X_TOOLKIT */
488dd4c4 15257#endif /* not USE_GTK*/
334208b7 15258
7a13e894
RS
15259 /* Detect failure. */
15260 if (dpy == 0)
60439948
KH
15261 {
15262 UNBLOCK_INPUT;
15263 return 0;
15264 }
7a13e894
RS
15265
15266 /* We have definitely succeeded. Record the new connection. */
15267
15268 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 15269 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 15270
29b38361
KH
15271#ifdef MULTI_KBOARD
15272 {
15273 struct x_display_info *share;
15274 Lisp_Object tail;
15275
15276 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6 15277 share = share->next, tail = XCDR (tail))
d5db4077
KR
15278 if (same_x_server (SDATA (XCAR (XCAR (tail))),
15279 SDATA (display_name)))
29b38361
KH
15280 break;
15281 if (share)
15282 dpyinfo->kboard = share->kboard;
15283 else
15284 {
15285 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
15286 init_kboard (dpyinfo->kboard);
59e755be
KH
15287 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
15288 {
15289 char *vendor = ServerVendor (dpy);
9b6ed9f3 15290 UNBLOCK_INPUT;
59e755be
KH
15291 dpyinfo->kboard->Vsystem_key_alist
15292 = call1 (Qvendor_specific_keysyms,
15293 build_string (vendor ? vendor : ""));
9b6ed9f3 15294 BLOCK_INPUT;
59e755be
KH
15295 }
15296
29b38361
KH
15297 dpyinfo->kboard->next_kboard = all_kboards;
15298 all_kboards = dpyinfo->kboard;
0ad5446c
KH
15299 /* Don't let the initial kboard remain current longer than necessary.
15300 That would cause problems if a file loaded on startup tries to
06a2c219 15301 prompt in the mini-buffer. */
0ad5446c
KH
15302 if (current_kboard == initial_kboard)
15303 current_kboard = dpyinfo->kboard;
29b38361
KH
15304 }
15305 dpyinfo->kboard->reference_count++;
15306 }
b9737ad3
KH
15307#endif
15308
7a13e894
RS
15309 /* Put this display on the chain. */
15310 dpyinfo->next = x_display_list;
15311 x_display_list = dpyinfo;
15312
7d0393cf 15313 /* Put it on x_display_name_list as well, to keep them parallel. */
7a13e894
RS
15314 x_display_name_list = Fcons (Fcons (display_name, Qnil),
15315 x_display_name_list);
8e713be6 15316 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
15317
15318 dpyinfo->display = dpy;
dc6f92b8 15319
dc6f92b8 15320#if 0
7a13e894 15321 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 15322#endif /* ! 0 */
7a13e894
RS
15323
15324 dpyinfo->x_id_name
d5db4077
KR
15325 = (char *) xmalloc (SBYTES (Vinvocation_name)
15326 + SBYTES (Vsystem_name)
7a13e894
RS
15327 + 2);
15328 sprintf (dpyinfo->x_id_name, "%s@%s",
d5db4077 15329 SDATA (Vinvocation_name), SDATA (Vsystem_name));
28430d3c
JB
15330
15331 /* Figure out which modifier bits mean what. */
334208b7 15332 x_find_modifier_meanings (dpyinfo);
f451eb13 15333
ab648270 15334 /* Get the scroll bar cursor. */
7a13e894 15335 dpyinfo->vertical_scroll_bar_cursor
334208b7 15336 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 15337
334208b7
RS
15338 xrdb = x_load_resources (dpyinfo->display, xrm_option,
15339 resource_name, EMACS_CLASS);
15340#ifdef HAVE_XRMSETDATABASE
15341 XrmSetDatabase (dpyinfo->display, xrdb);
15342#else
15343 dpyinfo->display->db = xrdb;
15344#endif
547d9db8 15345 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
15346 all versions. */
15347 dpyinfo->xrdb = xrdb;
334208b7
RS
15348
15349 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
15350 DefaultScreen (dpyinfo->display));
5ff67d81 15351 select_visual (dpyinfo);
43bd1b2b 15352 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
15353 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
15354 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
15355 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
15356 dpyinfo->grabbed = 0;
15357 dpyinfo->reference_count = 0;
15358 dpyinfo->icon_bitmap_id = -1;
06a2c219 15359 dpyinfo->font_table = NULL;
7a13e894
RS
15360 dpyinfo->n_fonts = 0;
15361 dpyinfo->font_table_size = 0;
15362 dpyinfo->bitmaps = 0;
15363 dpyinfo->bitmaps_size = 0;
15364 dpyinfo->bitmaps_last = 0;
15365 dpyinfo->scratch_cursor_gc = 0;
15366 dpyinfo->mouse_face_mouse_frame = 0;
15367 dpyinfo->mouse_face_deferred_gc = 0;
15368 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
15369 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 15370 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 15371 dpyinfo->mouse_face_window = Qnil;
0a61c667 15372 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
15373 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
15374 dpyinfo->mouse_face_defer = 0;
66301061 15375 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
15376 dpyinfo->x_focus_frame = 0;
15377 dpyinfo->x_focus_event_frame = 0;
15378 dpyinfo->x_highlight_frame = 0;
06a2c219 15379 dpyinfo->image_cache = make_image_cache ();
334208b7 15380
43bd1b2b 15381 /* See if a private colormap is requested. */
5ff67d81
GM
15382 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
15383 {
15384 if (dpyinfo->visual->class == PseudoColor)
15385 {
15386 Lisp_Object value;
15387 value = display_x_get_resource (dpyinfo,
15388 build_string ("privateColormap"),
15389 build_string ("PrivateColormap"),
15390 Qnil, Qnil);
15391 if (STRINGP (value)
d5db4077
KR
15392 && (!strcmp (SDATA (value), "true")
15393 || !strcmp (SDATA (value), "on")))
5ff67d81
GM
15394 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
15395 }
43bd1b2b 15396 }
5ff67d81
GM
15397 else
15398 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
15399 dpyinfo->visual, AllocNone);
7d0393cf 15400
06a2c219
GM
15401 {
15402 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
15403 double pixels = DisplayHeight (dpyinfo->display, screen_number);
15404 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
15405 dpyinfo->resy = pixels * 25.4 / mm;
15406 pixels = DisplayWidth (dpyinfo->display, screen_number);
15407 mm = DisplayWidthMM (dpyinfo->display, screen_number);
15408 dpyinfo->resx = pixels * 25.4 / mm;
15409 }
7d0393cf 15410
334208b7
RS
15411 dpyinfo->Xatom_wm_protocols
15412 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
15413 dpyinfo->Xatom_wm_take_focus
15414 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
15415 dpyinfo->Xatom_wm_save_yourself
15416 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
15417 dpyinfo->Xatom_wm_delete_window
15418 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
15419 dpyinfo->Xatom_wm_change_state
15420 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
15421 dpyinfo->Xatom_wm_configure_denied
15422 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
15423 dpyinfo->Xatom_wm_window_moved
15424 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
15425 dpyinfo->Xatom_editres
15426 = XInternAtom (dpyinfo->display, "Editres", False);
15427 dpyinfo->Xatom_CLIPBOARD
15428 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
15429 dpyinfo->Xatom_TIMESTAMP
15430 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
15431 dpyinfo->Xatom_TEXT
15432 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
15433 dpyinfo->Xatom_COMPOUND_TEXT
15434 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
c62525b7
KH
15435 dpyinfo->Xatom_UTF8_STRING
15436 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
334208b7
RS
15437 dpyinfo->Xatom_DELETE
15438 = XInternAtom (dpyinfo->display, "DELETE", False);
15439 dpyinfo->Xatom_MULTIPLE
15440 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
15441 dpyinfo->Xatom_INCR
15442 = XInternAtom (dpyinfo->display, "INCR", False);
15443 dpyinfo->Xatom_EMACS_TMP
15444 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
15445 dpyinfo->Xatom_TARGETS
15446 = XInternAtom (dpyinfo->display, "TARGETS", False);
15447 dpyinfo->Xatom_NULL
15448 = XInternAtom (dpyinfo->display, "NULL", False);
15449 dpyinfo->Xatom_ATOM_PAIR
15450 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
15451 /* For properties of font. */
15452 dpyinfo->Xatom_PIXEL_SIZE
15453 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
15454 dpyinfo->Xatom_MULE_BASELINE_OFFSET
15455 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
15456 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
15457 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
15458 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
15459 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 15460
06a2c219
GM
15461 /* Ghostscript support. */
15462 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
15463 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
7d0393cf 15464
06a2c219
GM
15465 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
15466 False);
15467
547d9db8
KH
15468 dpyinfo->cut_buffers_initialized = 0;
15469
334208b7
RS
15470 connection = ConnectionNumber (dpyinfo->display);
15471 dpyinfo->connection = connection;
15472
dc43ef94 15473 {
5d7cc324
RS
15474 char null_bits[1];
15475
15476 null_bits[0] = 0x00;
dc43ef94
KH
15477
15478 dpyinfo->null_pixel
7d0393cf 15479 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
dc43ef94
KH
15480 null_bits, 1, 1, (long) 0, (long) 0,
15481 1);
15482 }
15483
06a2c219
GM
15484 {
15485 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 15486 extern char *gray_bitmap_bits;
06a2c219
GM
15487 dpyinfo->gray
15488 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15489 gray_bitmap_bits,
15490 gray_bitmap_width, gray_bitmap_height,
15491 (unsigned long) 1, (unsigned long) 0, 1);
15492 }
15493
f5d11644
GM
15494#ifdef HAVE_X_I18N
15495 xim_initialize (dpyinfo, resource_name);
15496#endif
7d0393cf 15497
87485d6f
MW
15498#ifdef subprocesses
15499 /* This is only needed for distinguishing keyboard and process input. */
334208b7 15500 if (connection != 0)
7a13e894 15501 add_keyboard_wait_descriptor (connection);
87485d6f 15502#endif
6d4238f3 15503
041b69ac 15504#ifndef F_SETOWN_BUG
dc6f92b8 15505#ifdef F_SETOWN
dc6f92b8 15506#ifdef F_SETOWN_SOCK_NEG
61c3ce62 15507 /* stdin is a socket here */
334208b7 15508 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 15509#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 15510 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
15511#endif /* ! defined (F_SETOWN_SOCK_NEG) */
15512#endif /* ! defined (F_SETOWN) */
041b69ac 15513#endif /* F_SETOWN_BUG */
dc6f92b8
JB
15514
15515#ifdef SIGIO
eee20f6a
KH
15516 if (interrupt_input)
15517 init_sigio (connection);
c118dd06 15518#endif /* ! defined (SIGIO) */
dc6f92b8 15519
51b592fb 15520#ifdef USE_LUCID
f8c39f51 15521#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
15522 /* Make sure that we have a valid font for dialog boxes
15523 so that Xt does not crash. */
15524 {
15525 Display *dpy = dpyinfo->display;
15526 XrmValue d, fr, to;
15527 Font font;
e99db5a1 15528 int count;
7d0393cf 15529
51b592fb
RS
15530 d.addr = (XPointer)&dpy;
15531 d.size = sizeof (Display *);
15532 fr.addr = XtDefaultFont;
15533 fr.size = sizeof (XtDefaultFont);
15534 to.size = sizeof (Font *);
15535 to.addr = (XPointer)&font;
e99db5a1 15536 count = x_catch_errors (dpy);
51b592fb
RS
15537 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15538 abort ();
15539 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15540 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 15541 x_uncatch_errors (dpy, count);
51b592fb
RS
15542 }
15543#endif
f8c39f51 15544#endif
51b592fb 15545
34e23e5a
GM
15546 /* See if we should run in synchronous mode. This is useful
15547 for debugging X code. */
15548 {
15549 Lisp_Object value;
15550 value = display_x_get_resource (dpyinfo,
15551 build_string ("synchronous"),
15552 build_string ("Synchronous"),
15553 Qnil, Qnil);
15554 if (STRINGP (value)
d5db4077
KR
15555 && (!strcmp (SDATA (value), "true")
15556 || !strcmp (SDATA (value), "on")))
34e23e5a
GM
15557 XSynchronize (dpyinfo->display, True);
15558 }
7d0393cf 15559
60439948
KH
15560 UNBLOCK_INPUT;
15561
7a13e894
RS
15562 return dpyinfo;
15563}
15564\f
15565/* Get rid of display DPYINFO, assuming all frames are already gone,
15566 and without sending any more commands to the X server. */
dc6f92b8 15567
7a13e894
RS
15568void
15569x_delete_display (dpyinfo)
15570 struct x_display_info *dpyinfo;
15571{
15572 delete_keyboard_wait_descriptor (dpyinfo->connection);
15573
15574 /* Discard this display from x_display_name_list and x_display_list.
15575 We can't use Fdelq because that can quit. */
15576 if (! NILP (x_display_name_list)
8e713be6
KR
15577 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15578 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
15579 else
15580 {
15581 Lisp_Object tail;
15582
15583 tail = x_display_name_list;
8e713be6 15584 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 15585 {
bffcfca9 15586 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 15587 {
f3fbd155 15588 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
15589 break;
15590 }
8e713be6 15591 tail = XCDR (tail);
7a13e894
RS
15592 }
15593 }
15594
9bda743f
GM
15595 if (next_noop_dpyinfo == dpyinfo)
15596 next_noop_dpyinfo = dpyinfo->next;
15597
7a13e894
RS
15598 if (x_display_list == dpyinfo)
15599 x_display_list = dpyinfo->next;
7f9c7f94
RS
15600 else
15601 {
15602 struct x_display_info *tail;
7a13e894 15603
7f9c7f94
RS
15604 for (tail = x_display_list; tail; tail = tail->next)
15605 if (tail->next == dpyinfo)
15606 tail->next = tail->next->next;
15607 }
7a13e894 15608
0d777288
RS
15609#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15610#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
15611 XrmDestroyDatabase (dpyinfo->xrdb);
15612#endif
0d777288 15613#endif
29b38361
KH
15614#ifdef MULTI_KBOARD
15615 if (--dpyinfo->kboard->reference_count == 0)
39f79001 15616 delete_kboard (dpyinfo->kboard);
b9737ad3 15617#endif
f5d11644
GM
15618#ifdef HAVE_X_I18N
15619 if (dpyinfo->xim)
15620 xim_close_dpy (dpyinfo);
15621#endif
7d0393cf 15622
b9737ad3
KH
15623 xfree (dpyinfo->font_table);
15624 xfree (dpyinfo->x_id_name);
f04e1297 15625 xfree (dpyinfo->color_cells);
b9737ad3 15626 xfree (dpyinfo);
7a13e894 15627}
f04e1297 15628
7a13e894
RS
15629\f
15630/* Set up use of X before we make the first connection. */
15631
06a2c219
GM
15632static struct redisplay_interface x_redisplay_interface =
15633{
15634 x_produce_glyphs,
15635 x_write_glyphs,
15636 x_insert_glyphs,
15637 x_clear_end_of_line,
15638 x_scroll_run,
15639 x_after_update_window_line,
15640 x_update_window_begin,
15641 x_update_window_end,
15642 XTcursor_to,
15643 x_flush,
71b8321e 15644 x_clear_mouse_face,
66ac4b0e
GM
15645 x_get_glyph_overhangs,
15646 x_fix_overlapping_area
06a2c219
GM
15647};
15648
dfcf069d 15649void
7a13e894
RS
15650x_initialize ()
15651{
06a2c219
GM
15652 rif = &x_redisplay_interface;
15653
15654 clear_frame_hook = x_clear_frame;
15655 ins_del_lines_hook = x_ins_del_lines;
06a2c219 15656 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
15657 ring_bell_hook = XTring_bell;
15658 reset_terminal_modes_hook = XTreset_terminal_modes;
15659 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
15660 update_begin_hook = x_update_begin;
15661 update_end_hook = x_update_end;
dc6f92b8
JB
15662 set_terminal_window_hook = XTset_terminal_window;
15663 read_socket_hook = XTread_socket;
b8009dd1 15664 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 15665 mouse_position_hook = XTmouse_position;
f451eb13 15666 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 15667 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
15668 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15669 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15670 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15671 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 15672 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 15673
f676886a 15674 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 15675 char_ins_del_ok = 1;
dc6f92b8
JB
15676 line_ins_del_ok = 1; /* we'll just blt 'em */
15677 fast_clear_end_of_line = 1; /* X does this well */
58769bee 15678 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
15679 off the bottom */
15680 baud_rate = 19200;
15681
7a13e894 15682 x_noop_count = 0;
9ea173e8 15683 last_tool_bar_item = -1;
06a2c219 15684 any_help_event_p = 0;
7d0393cf 15685
b30b24cb
RS
15686 /* Try to use interrupt input; if we can't, then start polling. */
15687 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15688
7f9c7f94
RS
15689#ifdef USE_X_TOOLKIT
15690 XtToolkitInitialize ();
651f03b6 15691
7f9c7f94 15692 Xt_app_con = XtCreateApplicationContext ();
7d0393cf 15693
651f03b6
GM
15694 /* Register a converter from strings to pixels, which uses
15695 Emacs' color allocation infrastructure. */
15696 XtAppSetTypeConverter (Xt_app_con,
15697 XtRString, XtRPixel, cvt_string_to_pixel,
15698 cvt_string_to_pixel_args,
15699 XtNumber (cvt_string_to_pixel_args),
15700 XtCacheByDisplay, cvt_pixel_dtor);
15701
665881ad 15702 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
15703
15704 /* Install an asynchronous timer that processes Xt timeout events
15705 every 0.1s. This is necessary because some widget sets use
15706 timeouts internally, for example the LessTif menu bar, or the
15707 Xaw3d scroll bar. When Xt timouts aren't processed, these
15708 widgets don't behave normally. */
15709 {
15710 EMACS_TIME interval;
15711 EMACS_SET_SECS_USECS (interval, 0, 100000);
15712 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15713 }
db74249b 15714#endif
7d0393cf 15715
eccc05db 15716#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4 15717#ifndef USE_GTK
ec18280f
SM
15718 xaw3d_arrow_scroll = False;
15719 xaw3d_pick_top = True;
488dd4c4 15720#endif
7f9c7f94
RS
15721#endif
15722
58769bee 15723 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 15724 original error handler. */
e99db5a1 15725 XSetErrorHandler (x_error_handler);
334208b7 15726 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 15727
06a2c219 15728 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
15729#ifdef SIGWINCH
15730 signal (SIGWINCH, SIG_DFL);
a59facca 15731#endif /* SIGWINCH */
dc6f92b8 15732
92e2441b 15733 signal (SIGPIPE, x_connection_signal);
5f30b957
JD
15734
15735#ifdef HAVE_X_SM
15736 x_session_initialize ();
15737#endif
dc6f92b8 15738}
55123275 15739
06a2c219 15740
55123275
JB
15741void
15742syms_of_xterm ()
15743{
e99db5a1
RS
15744 staticpro (&x_error_message_string);
15745 x_error_message_string = Qnil;
15746
7a13e894
RS
15747 staticpro (&x_display_name_list);
15748 x_display_name_list = Qnil;
334208b7 15749
ab648270 15750 staticpro (&last_mouse_scroll_bar);
e53cb100 15751 last_mouse_scroll_bar = Qnil;
59e755be
KH
15752
15753 staticpro (&Qvendor_specific_keysyms);
15754 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 15755
c997eae5
SM
15756 staticpro (&Qutf_8);
15757 Qutf_8 = intern ("utf-8");
15758 staticpro (&Qlatin_1);
15759 Qlatin_1 = intern ("latin-1");
15760
2237cac9
RS
15761 staticpro (&last_mouse_press_frame);
15762 last_mouse_press_frame = Qnil;
06a2c219 15763
06a2c219 15764 help_echo = Qnil;
be010514
GM
15765 staticpro (&help_echo);
15766 help_echo_object = Qnil;
15767 staticpro (&help_echo_object);
7cea38bc
GM
15768 help_echo_window = Qnil;
15769 staticpro (&help_echo_window);
06a2c219 15770 previous_help_echo = Qnil;
be010514
GM
15771 staticpro (&previous_help_echo);
15772 help_echo_pos = -1;
06a2c219 15773
ee8ceff8 15774 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
503e457e 15775 doc: /* *Non-nil means autoselect window with mouse pointer. */);
ee8ceff8 15776 mouse_autoselect_window = 0;
503e457e 15777
7ee72033
MB
15778 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15779 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
228299fa
GM
15780For example, if a block cursor is over a tab, it will be drawn as
15781wide as that tab on the display. */);
06a2c219
GM
15782 x_stretch_cursor_p = 0;
15783
a72d5ce5 15784 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
15785 &x_use_underline_position_properties,
15786 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 15787nil means ignore them. If you encounter fonts with bogus
228299fa
GM
15788UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15789to 4.1, set this to nil. */);
a72d5ce5
GM
15790 x_use_underline_position_properties = 1;
15791
7ee72033
MB
15792 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15793 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
15794A value of nil means Emacs doesn't use X toolkit scroll bars.
15795Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 15796#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
15797#ifdef USE_MOTIF
15798 Vx_toolkit_scroll_bars = intern ("motif");
15799#elif defined HAVE_XAW3D
15800 Vx_toolkit_scroll_bars = intern ("xaw3d");
488dd4c4
JD
15801#elif USE_GTK
15802 Vx_toolkit_scroll_bars = intern ("gtk");
5bf04520
GM
15803#else
15804 Vx_toolkit_scroll_bars = intern ("xaw");
15805#endif
06a2c219 15806#else
5bf04520 15807 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
15808#endif
15809
06a2c219
GM
15810 staticpro (&last_mouse_motion_frame);
15811 last_mouse_motion_frame = Qnil;
7d0393cf 15812
98659da6
KG
15813 Qmodifier_value = intern ("modifier-value");
15814 Qalt = intern ("alt");
15815 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15816 Qhyper = intern ("hyper");
15817 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15818 Qmeta = intern ("meta");
15819 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15820 Qsuper = intern ("super");
15821 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
7d0393cf 15822
98659da6
KG
15823 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15824 doc: /* Which keys Emacs uses for the alt modifier.
15825This should be one of the symbols `alt', `hyper', `meta', `super'.
15826For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15827is nil, which is the same as `alt'. */);
15828 Vx_alt_keysym = Qnil;
7d0393cf 15829
98659da6
KG
15830 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15831 doc: /* Which keys Emacs uses for the hyper modifier.
15832This should be one of the symbols `alt', `hyper', `meta', `super'.
15833For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15834default is nil, which is the same as `hyper'. */);
15835 Vx_hyper_keysym = Qnil;
7d0393cf 15836
98659da6
KG
15837 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15838 doc: /* Which keys Emacs uses for the meta modifier.
15839This should be one of the symbols `alt', `hyper', `meta', `super'.
15840For example, `meta' means use the Meta_L and Meta_R keysyms. The
15841default is nil, which is the same as `meta'. */);
15842 Vx_meta_keysym = Qnil;
7d0393cf 15843
98659da6
KG
15844 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15845 doc: /* Which keys Emacs uses for the super modifier.
15846This should be one of the symbols `alt', `hyper', `meta', `super'.
15847For example, `super' means use the Super_L and Super_R keysyms. The
15848default is nil, which is the same as `super'. */);
15849 Vx_super_keysym = Qnil;
15850
dbd4b028
DL
15851 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
15852 doc: /* Hash table of character codes indexed by X keysym codes. */);
15853 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
15854 make_float (DEFAULT_REHASH_SIZE),
15855 make_float (DEFAULT_REHASH_THRESHOLD),
15856 Qnil, Qnil, Qnil);
55123275 15857}
6cf0ae86 15858
1d6c120a 15859#endif /* HAVE_X_WINDOWS */