Change defvar_int def and vars to use EMACS_INT instead of just int.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
edf36fe6 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
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
d624284c
PJ
100#ifdef USE_LUCID
101extern int xlwmenu_window_p (Widget w, Window window);
102extern void xlwmenu_redisplay P_ ((Widget));
103#endif
104
3afe33e7 105#ifdef USE_X_TOOLKIT
06a2c219 106
952291d9
GM
107extern void free_frame_menubar P_ ((struct frame *));
108extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
109 int));
06a2c219 110
0fdff6bb
RS
111#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
112#define HACK_EDITRES
113extern void _XEditResCheckMessages ();
114#endif /* not NO_EDITRES */
06a2c219
GM
115
116/* Include toolkit specific headers for the scroll bar widget. */
117
118#ifdef USE_TOOLKIT_SCROLL_BARS
119#if defined USE_MOTIF
120#include <Xm/Xm.h> /* for LESSTIF_VERSION */
121#include <Xm/ScrollBar.h>
ec18280f
SM
122#else /* !USE_MOTIF i.e. use Xaw */
123
124#ifdef HAVE_XAW3D
06a2c219 125#include <X11/Xaw3d/Simple.h>
06a2c219
GM
126#include <X11/Xaw3d/Scrollbar.h>
127#define ARROW_SCROLLBAR
128#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
129#else /* !HAVE_XAW3D */
130#include <X11/Xaw/Simple.h>
131#include <X11/Xaw/Scrollbar.h>
132#endif /* !HAVE_XAW3D */
133#ifndef XtNpickTop
134#define XtNpickTop "pickTop"
135#endif /* !XtNpickTop */
136#endif /* !USE_MOTIF */
06a2c219
GM
137#endif /* USE_TOOLKIT_SCROLL_BARS */
138
3afe33e7
RS
139#endif /* USE_X_TOOLKIT */
140
b849c413
RS
141#ifndef USE_X_TOOLKIT
142#define x_any_window_to_frame x_window_to_frame
5627c40e 143#define x_top_window_to_frame x_window_to_frame
b849c413
RS
144#endif
145
546e6d5b 146#ifdef USE_X_TOOLKIT
d067ea8b 147#include "widget.h"
546e6d5b
RS
148#ifndef XtNinitialState
149#define XtNinitialState "initialState"
150#endif
151#endif
152
06a2c219
GM
153#define abs(x) ((x) < 0 ? -(x) : (x))
154
155#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
156
157\f
3f332ef3 158/* Fringe bitmaps. */
06a2c219 159
3f332ef3 160enum fringe_bitmap_type
06a2c219 161{
3f332ef3 162 NO_FRINGE_BITMAP,
06a2c219
GM
163 LEFT_TRUNCATION_BITMAP,
164 RIGHT_TRUNCATION_BITMAP,
165 OVERLAY_ARROW_BITMAP,
166 CONTINUED_LINE_BITMAP,
167 CONTINUATION_LINE_BITMAP,
168 ZV_LINE_BITMAP
169};
170
171/* Bitmap drawn to indicate lines not displaying text if
172 `indicate-empty-lines' is non-nil. */
173
174#define zv_width 8
976b73d7
KS
175#define zv_height 72
176#define zv_period 3
06a2c219 177static unsigned char zv_bits[] = {
976b73d7
KS
178 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
179 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
180 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
181 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
182 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
183 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
06a2c219
GM
186
187/* An arrow like this: `<-'. */
188
189#define left_width 8
190#define left_height 8
191static unsigned char left_bits[] = {
192 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
193
110859fc
GM
194/* Right truncation arrow bitmap `->'. */
195
196#define right_width 8
197#define right_height 8
198static unsigned char right_bits[] = {
199 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
200
06a2c219
GM
201/* Marker for continued lines. */
202
203#define continued_width 8
204#define continued_height 8
205static unsigned char continued_bits[] = {
110859fc
GM
206 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
207
208/* Marker for continuation lines. */
06a2c219
GM
209
210#define continuation_width 8
211#define continuation_height 8
212static unsigned char continuation_bits[] = {
110859fc 213 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 214
110859fc 215/* Overlay arrow bitmap. */
06a2c219 216
110859fc
GM
217#if 0
218/* A bomb. */
06a2c219
GM
219#define ov_width 8
220#define ov_height 8
221static unsigned char ov_bits[] = {
222 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 223#else
110859fc 224/* A triangular arrow. */
06a2c219
GM
225#define ov_width 8
226#define ov_height 8
227static unsigned char ov_bits[] = {
110859fc
GM
228 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
229
e4b68333 230#endif
06a2c219
GM
231
232extern Lisp_Object Qhelp_echo;
233
69388238 234\f
5bf04520 235/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 236
5bf04520 237Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
238
239/* If a string, XTread_socket generates an event to display that string.
240 (The display is done in read_char.) */
241
242static Lisp_Object help_echo;
7cea38bc 243static Lisp_Object help_echo_window;
be010514
GM
244static Lisp_Object help_echo_object;
245static int help_echo_pos;
06a2c219
GM
246
247/* Temporary variable for XTread_socket. */
248
249static Lisp_Object previous_help_echo;
250
251/* Non-zero means that a HELP_EVENT has been generated since Emacs
252 start. */
253
254static int any_help_event_p;
255
503e457e
PJ
256/* Non-zero means autoselect window with the mouse cursor. */
257
258int x_autoselect_window_p;
259
06a2c219
GM
260/* Non-zero means draw block and hollow cursor as wide as the glyph
261 under it. For example, if a block cursor is over a tab, it will be
262 drawn as wide as that tab on the display. */
263
264int x_stretch_cursor_p;
265
a72d5ce5
GM
266/* Non-zero means make use of UNDERLINE_POSITION font properties. */
267
268int x_use_underline_position_properties;
269
06a2c219
GM
270/* This is a chain of structures for all the X displays currently in
271 use. */
272
334208b7 273struct x_display_info *x_display_list;
dc6f92b8 274
06a2c219
GM
275/* This is a list of cons cells, each of the form (NAME
276 . FONT-LIST-CACHE), one for each element of x_display_list and in
277 the same order. NAME is the name of the frame. FONT-LIST-CACHE
278 records previous values returned by x-list-fonts. */
279
7a13e894 280Lisp_Object x_display_name_list;
f451eb13 281
987d2ad1 282/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
283 This is set by update_begin and looked at by all the XT functions.
284 It is zero while not inside an update. In that case, the XT
285 functions assume that `selected_frame' is the frame to apply to. */
286
d0386f2a 287extern struct frame *updating_frame;
dc6f92b8 288
06a2c219
GM
289/* This is a frame waiting to be auto-raised, within XTread_socket. */
290
0134a210
RS
291struct frame *pending_autoraise_frame;
292
7f9c7f94
RS
293#ifdef USE_X_TOOLKIT
294/* The application context for Xt use. */
295XtAppContext Xt_app_con;
06a2c219
GM
296static String Xt_default_resources[] = {0};
297#endif /* USE_X_TOOLKIT */
665881ad 298
06a2c219
GM
299/* Nominal cursor position -- where to draw output.
300 HPOS and VPOS are window relative glyph matrix coordinates.
301 X and Y are window relative pixel coordinates. */
dc6f92b8 302
06a2c219 303struct cursor_pos output_cursor;
dc6f92b8 304
bffcfca9
GM
305/* Non-zero means user is interacting with a toolkit scroll bar. */
306
307static int toolkit_scroll_bar_interaction;
dc6f92b8 308
69388238
RS
309/* Mouse movement.
310
06a2c219 311 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
312 so that we would have to call XQueryPointer after each MotionNotify
313 event to ask for another such event. However, this made mouse tracking
314 slow, and there was a bug that made it eventually stop.
315
316 Simply asking for MotionNotify all the time seems to work better.
317
69388238
RS
318 In order to avoid asking for motion events and then throwing most
319 of them away or busy-polling the server for mouse positions, we ask
320 the server for pointer motion hints. This means that we get only
321 one event per group of mouse movements. "Groups" are delimited by
322 other kinds of events (focus changes and button clicks, for
323 example), or by XQueryPointer calls; when one of these happens, we
324 get another MotionNotify event the next time the mouse moves. This
325 is at least as efficient as getting motion events when mouse
326 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 327 is off. */
69388238
RS
328
329/* Where the mouse was last time we reported a mouse event. */
69388238 330
06a2c219
GM
331FRAME_PTR last_mouse_frame;
332static XRectangle last_mouse_glyph;
2237cac9
RS
333static Lisp_Object last_mouse_press_frame;
334
69388238
RS
335/* The scroll bar in which the last X motion event occurred.
336
06a2c219
GM
337 If the last X motion event occurred in a scroll bar, we set this so
338 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
339 an ordinary motion.
340
06a2c219
GM
341 If the last X motion event didn't occur in a scroll bar, we set
342 this to Qnil, to tell XTmouse_position to return an ordinary motion
343 event. */
344
69388238
RS
345static Lisp_Object last_mouse_scroll_bar;
346
69388238
RS
347/* This is a hack. We would really prefer that XTmouse_position would
348 return the time associated with the position it returns, but there
06a2c219 349 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
350 along with the position query. So, we just keep track of the time
351 of the last movement we received, and return that in hopes that
352 it's somewhat accurate. */
06a2c219 353
69388238
RS
354static Time last_mouse_movement_time;
355
06a2c219
GM
356/* Incremented by XTread_socket whenever it really tries to read
357 events. */
358
c0a04927
RS
359#ifdef __STDC__
360static int volatile input_signal_count;
361#else
362static int input_signal_count;
363#endif
364
7a13e894 365/* Used locally within XTread_socket. */
06a2c219 366
7a13e894 367static int x_noop_count;
dc6f92b8 368
7a13e894 369/* Initial values of argv and argc. */
06a2c219 370
7a13e894
RS
371extern char **initial_argv;
372extern int initial_argc;
dc6f92b8 373
7a13e894 374extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 375
06a2c219 376/* Tells if a window manager is present or not. */
7a13e894
RS
377
378extern Lisp_Object Vx_no_window_manager;
dc6f92b8 379
c2df547c 380extern Lisp_Object Qface, Qmouse_face;
b8009dd1 381
dc6f92b8
JB
382extern int errno;
383
dfeccd2d 384/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 385
31ade731 386extern EMACS_INT extra_keyboard_modifiers;
64bb1782 387
98659da6
KG
388/* The keysyms to use for the various modifiers. */
389
390Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
391static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
392
59e755be
KH
393static Lisp_Object Qvendor_specific_keysyms;
394
952291d9
GM
395extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
396extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 397
7a13e894 398
06a2c219
GM
399/* Enumeration for overriding/changing the face to use for drawing
400 glyphs in x_draw_glyphs. */
401
402enum draw_glyphs_face
403{
404 DRAW_NORMAL_TEXT,
405 DRAW_INVERSE_VIDEO,
406 DRAW_CURSOR,
407 DRAW_MOUSE_FACE,
408 DRAW_IMAGE_RAISED,
409 DRAW_IMAGE_SUNKEN
410};
411
b7f83f9e 412static int cursor_in_mouse_face_p P_ ((struct window *));
fa262c07 413static int clear_mouse_face P_ ((struct x_display_info *));
651f03b6 414static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
499b1844 415static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
651f03b6 416static const XColor *x_color_cells P_ ((Display *, int *));
71b8321e 417static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
418static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
419void x_delete_display P_ ((struct x_display_info *));
420static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
421 unsigned));
422static int fast_find_position P_ ((struct window *, int, int *, int *,
7e376260 423 int *, int *, Lisp_Object));
f9db2310
GM
424static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
425 int *, int *, int *, int *, int));
06a2c219
GM
426static void set_output_cursor P_ ((struct cursor_pos *));
427static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
f9db2310 428 int *, int *, int *, int));
06a2c219 429static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 430static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
431static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
432static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
433static void show_mouse_face P_ ((struct x_display_info *,
434 enum draw_glyphs_face));
435static int x_io_error_quitter P_ ((Display *));
436int x_catch_errors P_ ((Display *));
437void x_uncatch_errors P_ ((Display *, int));
438void x_lower_frame P_ ((struct frame *));
439void x_scroll_bar_clear P_ ((struct frame *));
440int x_had_errors_p P_ ((Display *));
441void x_wm_set_size_hint P_ ((struct frame *, long, int));
442void x_raise_frame P_ ((struct frame *));
443void x_set_window_size P_ ((struct frame *, int, int, int));
444void x_wm_set_window_state P_ ((struct frame *, int));
445void x_wm_set_icon_pixmap P_ ((struct frame *, int));
446void x_initialize P_ ((void));
447static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
448static int x_compute_min_glyph_bounds P_ ((struct frame *));
449static void x_draw_phys_cursor_glyph P_ ((struct window *,
450 struct glyph_row *,
451 enum draw_glyphs_face));
452static void x_update_end P_ ((struct frame *));
453static void XTframe_up_to_date P_ ((struct frame *));
06a2c219
GM
454static void XTset_terminal_modes P_ ((void));
455static void XTreset_terminal_modes P_ ((void));
456static void XTcursor_to P_ ((int, int, int, int));
457static void x_write_glyphs P_ ((struct glyph *, int));
458static void x_clear_end_of_line P_ ((int));
459static void x_clear_frame P_ ((void));
460static void x_clear_cursor P_ ((struct window *));
461static void frame_highlight P_ ((struct frame *));
462static void frame_unhighlight P_ ((struct frame *));
463static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
464static void XTframe_rehighlight P_ ((struct frame *));
465static void x_frame_rehighlight P_ ((struct x_display_info *));
466static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 467static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
468static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
469 XRectangle *));
470static void expose_frame P_ ((struct frame *, int, int, int, int));
82f053ab 471static int expose_window_tree P_ ((struct window *, XRectangle *));
a39202f6 472static int expose_window P_ ((struct window *, XRectangle *));
06a2c219
GM
473static void expose_area P_ ((struct window *, struct glyph_row *,
474 XRectangle *, enum glyph_row_area));
82f053ab 475static int expose_line P_ ((struct window *, struct glyph_row *,
06a2c219
GM
476 XRectangle *));
477static void x_update_cursor_in_window_tree P_ ((struct window *, int));
478static void x_update_window_cursor P_ ((struct window *, int));
479static void x_erase_phys_cursor P_ ((struct window *));
480void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
3f332ef3 481static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
976b73d7 482 enum fringe_bitmap_type, int left_p));
06a2c219
GM
483
484static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
485 GC, int));
486static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
3f332ef3 487static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
60626bab
GM
488static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
489 int, int, int, int));
06a2c219 490static void x_flush P_ ((struct frame *f));
952291d9
GM
491static void x_update_begin P_ ((struct frame *));
492static void x_update_window_begin P_ ((struct window *));
493static void x_draw_vertical_border P_ ((struct window *));
494static void x_after_update_window_line P_ ((struct glyph_row *));
495static INLINE void take_vertical_position_into_account P_ ((struct it *));
496static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
497static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
498static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
499 enum scroll_bar_part *,
500 Lisp_Object *, Lisp_Object *,
501 unsigned long *));
d0fa3e56
EZ
502static void x_check_fullscreen P_ ((struct frame *));
503static void x_check_fullscreen_move P_ ((struct frame *));
06a2c219
GM
504
505/* Flush display of frame F, or of all frames if F is null. */
506
507static void
508x_flush (f)
509 struct frame *f;
510{
511 BLOCK_INPUT;
512 if (f == NULL)
513 {
514 Lisp_Object rest, frame;
515 FOR_EACH_FRAME (rest, frame)
516 x_flush (XFRAME (frame));
517 }
518 else if (FRAME_X_P (f))
519 XFlush (FRAME_X_DISPLAY (f));
520 UNBLOCK_INPUT;
521}
522
dc6f92b8 523
06a2c219
GM
524/* Remove calls to XFlush by defining XFlush to an empty replacement.
525 Calls to XFlush should be unnecessary because the X output buffer
526 is flushed automatically as needed by calls to XPending,
527 XNextEvent, or XWindowEvent according to the XFlush man page.
528 XTread_socket calls XPending. Removing XFlush improves
529 performance. */
530
531#define XFlush(DISPLAY) (void) 0
b8009dd1 532
334208b7 533\f
06a2c219
GM
534/***********************************************************************
535 Debugging
536 ***********************************************************************/
537
9382638d 538#if 0
06a2c219
GM
539
540/* This is a function useful for recording debugging information about
541 the sequence of occurrences in this file. */
9382638d
KH
542
543struct record
544{
545 char *locus;
546 int type;
547};
548
549struct record event_record[100];
550
551int event_record_index;
552
553record_event (locus, type)
554 char *locus;
555 int type;
556{
557 if (event_record_index == sizeof (event_record) / sizeof (struct record))
558 event_record_index = 0;
559
560 event_record[event_record_index].locus = locus;
561 event_record[event_record_index].type = type;
562 event_record_index++;
563}
564
565#endif /* 0 */
06a2c219
GM
566
567
9382638d 568\f
334208b7
RS
569/* Return the struct x_display_info corresponding to DPY. */
570
571struct x_display_info *
572x_display_info_for_display (dpy)
573 Display *dpy;
574{
575 struct x_display_info *dpyinfo;
576
577 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
578 if (dpyinfo->display == dpy)
579 return dpyinfo;
16bd92ea 580
334208b7
RS
581 return 0;
582}
f451eb13 583
06a2c219
GM
584
585\f
586/***********************************************************************
587 Starting and ending an update
588 ***********************************************************************/
589
590/* Start an update of frame F. This function is installed as a hook
591 for update_begin, i.e. it is called when update_begin is called.
592 This function is called prior to calls to x_update_window_begin for
593 each window being updated. Currently, there is nothing to do here
594 because all interesting stuff is done on a window basis. */
dc6f92b8 595
dfcf069d 596static void
06a2c219 597x_update_begin (f)
f676886a 598 struct frame *f;
58769bee 599{
06a2c219
GM
600 /* Nothing to do. */
601}
dc6f92b8 602
dc6f92b8 603
06a2c219
GM
604/* Start update of window W. Set the global variable updated_window
605 to the window being updated and set output_cursor to the cursor
606 position of W. */
dc6f92b8 607
06a2c219
GM
608static void
609x_update_window_begin (w)
610 struct window *w;
611{
612 struct frame *f = XFRAME (WINDOW_FRAME (w));
613 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
614
615 updated_window = w;
616 set_output_cursor (&w->cursor);
b8009dd1 617
06a2c219 618 BLOCK_INPUT;
d1bc4182 619
06a2c219 620 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 621 {
514e4681 622 /* Don't do highlighting for mouse motion during the update. */
06a2c219 623 display_info->mouse_face_defer = 1;
37c2c98b 624
06a2c219
GM
625 /* If F needs to be redrawn, simply forget about any prior mouse
626 highlighting. */
9f67f20b 627 if (FRAME_GARBAGED_P (f))
06a2c219
GM
628 display_info->mouse_face_window = Qnil;
629
64f26cf5
GM
630#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
631 their mouse_face_p flag set, which means that they are always
632 unequal to rows in a desired matrix which never have that
633 flag set. So, rows containing mouse-face glyphs are never
634 scrolled, and we don't have to switch the mouse highlight off
635 here to prevent it from being scrolled. */
636
06a2c219
GM
637 /* Can we tell that this update does not affect the window
638 where the mouse highlight is? If so, no need to turn off.
639 Likewise, don't do anything if the frame is garbaged;
640 in that case, the frame's current matrix that we would use
641 is all wrong, and we will redisplay that line anyway. */
642 if (!NILP (display_info->mouse_face_window)
643 && w == XWINDOW (display_info->mouse_face_window))
514e4681 644 {
06a2c219 645 int i;
514e4681 646
06a2c219
GM
647 for (i = 0; i < w->desired_matrix->nrows; ++i)
648 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
649 break;
650
06a2c219
GM
651 if (i < w->desired_matrix->nrows)
652 clear_mouse_face (display_info);
514e4681 653 }
64f26cf5 654#endif /* 0 */
b8009dd1 655 }
6ccf47d1 656
dc6f92b8
JB
657 UNBLOCK_INPUT;
658}
659
06a2c219
GM
660
661/* Draw a vertical window border to the right of window W if W doesn't
662 have vertical scroll bars. */
663
dfcf069d 664static void
06a2c219
GM
665x_draw_vertical_border (w)
666 struct window *w;
58769bee 667{
06a2c219
GM
668 struct frame *f = XFRAME (WINDOW_FRAME (w));
669
670 /* Redraw borders between horizontally adjacent windows. Don't
671 do it for frames with vertical scroll bars because either the
672 right scroll bar of a window, or the left scroll bar of its
673 neighbor will suffice as a border. */
674 if (!WINDOW_RIGHTMOST_P (w)
675 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
676 {
677 int x0, x1, y0, y1;
dc6f92b8 678
06a2c219 679 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
3f332ef3 680 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
06a2c219
GM
681 y1 -= 1;
682
683 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
684 f->output_data.x->normal_gc, x1, y0, x1, y1);
685 }
686}
687
688
71b8321e
GM
689/* End update of window W (which is equal to updated_window).
690
691 Draw vertical borders between horizontally adjacent windows, and
692 display W's cursor if CURSOR_ON_P is non-zero.
693
694 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
695 glyphs in mouse-face were overwritten. In that case we have to
696 make sure that the mouse-highlight is properly redrawn.
697
698 W may be a menu bar pseudo-window in case we don't have X toolkit
699 support. Such windows don't have a cursor, so don't display it
700 here. */
06a2c219
GM
701
702static void
71b8321e 703x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 704 struct window *w;
71b8321e 705 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 706{
140330de
GM
707 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
708
06a2c219
GM
709 if (!w->pseudo_window_p)
710 {
711 BLOCK_INPUT;
71b8321e 712
06a2c219
GM
713 if (cursor_on_p)
714 x_display_and_set_cursor (w, 1, output_cursor.hpos,
715 output_cursor.vpos,
716 output_cursor.x, output_cursor.y);
71b8321e 717
06a2c219
GM
718 x_draw_vertical_border (w);
719 UNBLOCK_INPUT;
720 }
721
140330de
GM
722 /* If a row with mouse-face was overwritten, arrange for
723 XTframe_up_to_date to redisplay the mouse highlight. */
724 if (mouse_face_overwritten_p)
725 {
726 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
727 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
728 dpyinfo->mouse_face_window = Qnil;
729 }
730
06a2c219
GM
731 updated_window = NULL;
732}
dc6f92b8 733
dc6f92b8 734
06a2c219
GM
735/* End update of frame F. This function is installed as a hook in
736 update_end. */
737
738static void
739x_update_end (f)
740 struct frame *f;
741{
742 /* Mouse highlight may be displayed again. */
aa8bff2e 743 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 744
06a2c219 745 BLOCK_INPUT;
334208b7 746 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
747 UNBLOCK_INPUT;
748}
b8009dd1 749
06a2c219
GM
750
751/* This function is called from various places in xdisp.c whenever a
752 complete update has been performed. The global variable
753 updated_window is not available here. */
b8009dd1 754
dfcf069d 755static void
b8009dd1 756XTframe_up_to_date (f)
06a2c219 757 struct frame *f;
b8009dd1 758{
06a2c219 759 if (FRAME_X_P (f))
514e4681 760 {
06a2c219 761 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 762
06a2c219
GM
763 if (dpyinfo->mouse_face_deferred_gc
764 || f == dpyinfo->mouse_face_mouse_frame)
765 {
766 BLOCK_INPUT;
767 if (dpyinfo->mouse_face_mouse_frame)
768 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
769 dpyinfo->mouse_face_mouse_x,
770 dpyinfo->mouse_face_mouse_y);
771 dpyinfo->mouse_face_deferred_gc = 0;
772 UNBLOCK_INPUT;
773 }
514e4681 774 }
b8009dd1 775}
06a2c219
GM
776
777
778/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 779 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
780 before DESIRED_ROW is made current. The window being updated is
781 found in updated_window. This function It is called from
782 update_window_line only if it is known that there are differences
783 between bitmaps to be drawn between current row and DESIRED_ROW. */
784
785static void
786x_after_update_window_line (desired_row)
787 struct glyph_row *desired_row;
788{
789 struct window *w = updated_window;
ef253080 790 struct frame *f;
259cf6bc 791 int width, height;
06a2c219
GM
792
793 xassert (w);
794
795 if (!desired_row->mode_line_p && !w->pseudo_window_p)
796 {
797 BLOCK_INPUT;
3f332ef3 798 x_draw_row_fringe_bitmaps (w, desired_row);
ef253080
GM
799 UNBLOCK_INPUT;
800 }
801
802 /* When a window has disappeared, make sure that no rest of
803 full-width rows stays visible in the internal border. Could
804 check here if updated_window is the leftmost/rightmost window,
805 but I guess it's not worth doing since vertically split windows
806 are almost never used, internal border is rarely set, and the
807 overhead is very small. */
808 if (windows_or_buffers_changed
809 && desired_row->full_width_p
810 && (f = XFRAME (w->frame),
811 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
812 width != 0)
813 && (height = desired_row->visible_height,
814 height > 0))
ef253080 815 {
ef253080 816 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 817
ef253080
GM
818 /* Internal border is drawn below the tool bar. */
819 if (WINDOWP (f->tool_bar_window)
820 && w == XWINDOW (f->tool_bar_window))
821 y -= width;
06a2c219 822
ef253080
GM
823 BLOCK_INPUT;
824 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
825 0, y, width, height, False);
826 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
827 f->output_data.x->pixel_width - width,
828 y, width, height, False);
06a2c219
GM
829 UNBLOCK_INPUT;
830 }
831}
832
833
3f332ef3 834/* Draw the bitmap WHICH in one of the left or right fringes of
06a2c219
GM
835 window W. ROW is the glyph row for which to display the bitmap; it
836 determines the vertical position at which the bitmap has to be
837 drawn. */
838
839static void
976b73d7 840x_draw_fringe_bitmap (w, row, which, left_p)
06a2c219
GM
841 struct window *w;
842 struct glyph_row *row;
3f332ef3 843 enum fringe_bitmap_type which;
976b73d7 844 int left_p;
06a2c219
GM
845{
846 struct frame *f = XFRAME (WINDOW_FRAME (w));
847 Display *display = FRAME_X_DISPLAY (f);
848 Window window = FRAME_X_WINDOW (f);
849 int x, y, wd, h, dy;
976b73d7 850 int b1, b2;
06a2c219
GM
851 unsigned char *bits;
852 Pixmap pixmap;
853 GC gc = f->output_data.x->normal_gc;
854 struct face *face;
855 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
856
857 /* Must clip because of partially visible lines. */
858 x_clip_to_row (w, row, gc, 1);
859
976b73d7
KS
860 /* Convert row to frame coordinates. */
861 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
862
06a2c219
GM
863 switch (which)
864 {
976b73d7
KS
865 case NO_FRINGE_BITMAP:
866 wd = 0;
867 h = 0;
868 break;
869
06a2c219
GM
870 case LEFT_TRUNCATION_BITMAP:
871 wd = left_width;
872 h = left_height;
873 bits = left_bits;
06a2c219
GM
874 break;
875
876 case OVERLAY_ARROW_BITMAP:
976b73d7
KS
877 wd = ov_width;
878 h = ov_height;
06a2c219 879 bits = ov_bits;
06a2c219
GM
880 break;
881
882 case RIGHT_TRUNCATION_BITMAP:
883 wd = right_width;
884 h = right_height;
885 bits = right_bits;
06a2c219
GM
886 break;
887
888 case CONTINUED_LINE_BITMAP:
976b73d7
KS
889 wd = continued_width;
890 h = continued_height;
06a2c219 891 bits = continued_bits;
06a2c219
GM
892 break;
893
894 case CONTINUATION_LINE_BITMAP:
895 wd = continuation_width;
896 h = continuation_height;
897 bits = continuation_bits;
06a2c219
GM
898 break;
899
900 case ZV_LINE_BITMAP:
901 wd = zv_width;
976b73d7
KS
902 h = zv_height - (y % zv_period);
903 bits = zv_bits + (y % zv_period);
06a2c219
GM
904 break;
905
906 default:
907 abort ();
908 }
909
976b73d7
KS
910 /* Clip bitmap if too high. */
911 if (h > row->height)
912 h = row->height;
913
914 /* Set dy to the offset in the row to start drawing the bitmap. */
06a2c219
GM
915 dy = (row->height - h) / 2;
916
976b73d7
KS
917 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
918 PREPARE_FACE_FOR_DISPLAY (f, face);
919
920 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
921 the fringe. */
dbdc9702 922 b1 = b2 = -1;
976b73d7
KS
923 if (left_p)
924 {
925 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
926 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
927 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
928 - wd
929 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
930 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
931 {
932 /* If W has a vertical border to its left, don't draw over it. */
933 int border = ((XFASTINT (w->left) > 0
934 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
935 ? 1 : 0);
936 b1 = (window_box_left (w, -1)
937 - FRAME_X_LEFT_FRINGE_WIDTH (f)
938 + border);
939 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
940 }
941 }
942 else
943 {
944 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
945 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
946 x = (window_box_right (w, -1)
947 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
948 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
949 the fringe. */
950 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
951 {
952 b1 = window_box_right (w, -1);
953 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
954 }
955 }
956
957 if (b1 >= 0)
958 {
959 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
960
961 /* In case the same realized face is used for fringes and
962 for something displayed in the text (e.g. face `region' on
963 mono-displays, the fill style may have been changed to
964 FillSolid in x_draw_glyph_string_background. */
965 if (face->stipple)
966 XSetFillStyle (display, face->gc, FillOpaqueStippled);
967 else
968 XSetForeground (display, face->gc, face->background);
969
970 XFillRectangle (display, window, face->gc,
971 b1,
972 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
973 row->y)),
974 b2,
975 row->visible_height);
976 if (!face->stipple)
977 XSetForeground (display, face->gc, face->foreground);
978 }
979
dbdc9702
GM
980 if (which != NO_FRINGE_BITMAP)
981 {
982 /* Draw the bitmap. I believe these small pixmaps can be cached
983 by the server. */
984 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
985 face->foreground,
986 face->background, depth);
987 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
988 XFreePixmap (display, pixmap);
989 }
990
06a2c219
GM
991 XSetClipMask (display, gc, None);
992}
993
994
3f332ef3 995/* Draw fringe bitmaps for glyph row ROW on window W. Call this
06a2c219
GM
996 function with input blocked. */
997
998static void
3f332ef3 999x_draw_row_fringe_bitmaps (w, row)
06a2c219
GM
1000 struct window *w;
1001 struct glyph_row *row;
1002{
1003 struct frame *f = XFRAME (w->frame);
3f332ef3 1004 enum fringe_bitmap_type bitmap;
06a2c219
GM
1005
1006 xassert (interrupt_input_blocked);
1007
1008 /* If row is completely invisible, because of vscrolling, we
1009 don't have to draw anything. */
1010 if (row->visible_height <= 0)
1011 return;
1012
976b73d7
KS
1013 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1014 {
1015 /* Decide which bitmap to draw in the left fringe. */
1016 if (row->overlay_arrow_p)
1017 bitmap = OVERLAY_ARROW_BITMAP;
1018 else if (row->truncated_on_left_p)
1019 bitmap = LEFT_TRUNCATION_BITMAP;
1020 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1021 bitmap = CONTINUATION_LINE_BITMAP;
1022 else if (row->indicate_empty_line_p)
1023 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1024 else
976b73d7 1025 bitmap = NO_FRINGE_BITMAP;
06a2c219 1026
976b73d7
KS
1027 x_draw_fringe_bitmap (w, row, bitmap, 1);
1028 }
06a2c219 1029
976b73d7 1030 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
06a2c219 1031 {
976b73d7
KS
1032 /* Decide which bitmap to draw in the right fringe. */
1033 if (row->truncated_on_right_p)
1034 bitmap = RIGHT_TRUNCATION_BITMAP;
1035 else if (row->continued_p)
1036 bitmap = CONTINUED_LINE_BITMAP;
1037 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1038 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1039 else
976b73d7 1040 bitmap = NO_FRINGE_BITMAP;
06a2c219 1041
976b73d7
KS
1042 x_draw_fringe_bitmap (w, row, bitmap, 0);
1043 }
06a2c219
GM
1044}
1045
dc6f92b8 1046\f
06a2c219
GM
1047
1048/* This is called when starting Emacs and when restarting after
1049 suspend. When starting Emacs, no X window is mapped. And nothing
1050 must be done to Emacs's own window if it is suspended (though that
1051 rarely happens). */
dc6f92b8 1052
dfcf069d 1053static void
dc6f92b8
JB
1054XTset_terminal_modes ()
1055{
1056}
1057
06a2c219
GM
1058/* This is called when exiting or suspending Emacs. Exiting will make
1059 the X-windows go away, and suspending requires no action. */
dc6f92b8 1060
dfcf069d 1061static void
dc6f92b8
JB
1062XTreset_terminal_modes ()
1063{
dc6f92b8 1064}
06a2c219
GM
1065
1066
dc6f92b8 1067\f
06a2c219
GM
1068/***********************************************************************
1069 Output Cursor
1070 ***********************************************************************/
1071
1072/* Set the global variable output_cursor to CURSOR. All cursor
1073 positions are relative to updated_window. */
dc6f92b8 1074
dfcf069d 1075static void
06a2c219
GM
1076set_output_cursor (cursor)
1077 struct cursor_pos *cursor;
dc6f92b8 1078{
06a2c219
GM
1079 output_cursor.hpos = cursor->hpos;
1080 output_cursor.vpos = cursor->vpos;
1081 output_cursor.x = cursor->x;
1082 output_cursor.y = cursor->y;
1083}
1084
1085
1086/* Set a nominal cursor position.
dc6f92b8 1087
06a2c219
GM
1088 HPOS and VPOS are column/row positions in a window glyph matrix. X
1089 and Y are window text area relative pixel positions.
1090
1091 If this is done during an update, updated_window will contain the
1092 window that is being updated and the position is the future output
1093 cursor position for that window. If updated_window is null, use
1094 selected_window and display the cursor at the given position. */
1095
1096static void
1097XTcursor_to (vpos, hpos, y, x)
1098 int vpos, hpos, y, x;
1099{
1100 struct window *w;
1101
1102 /* If updated_window is not set, work on selected_window. */
1103 if (updated_window)
1104 w = updated_window;
1105 else
1106 w = XWINDOW (selected_window);
dbcb258a 1107
06a2c219
GM
1108 /* Set the output cursor. */
1109 output_cursor.hpos = hpos;
1110 output_cursor.vpos = vpos;
1111 output_cursor.x = x;
1112 output_cursor.y = y;
dc6f92b8 1113
06a2c219
GM
1114 /* If not called as part of an update, really display the cursor.
1115 This will also set the cursor position of W. */
1116 if (updated_window == NULL)
dc6f92b8
JB
1117 {
1118 BLOCK_INPUT;
06a2c219 1119 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1120 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1121 UNBLOCK_INPUT;
1122 }
1123}
dc43ef94 1124
06a2c219
GM
1125
1126\f
1127/***********************************************************************
1128 Display Iterator
1129 ***********************************************************************/
1130
1131/* Function prototypes of this page. */
1132
1133static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1134 struct glyph *,
ee569018
KH
1135 XChar2b *,
1136 int *));
06a2c219
GM
1137static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1138 int, XChar2b *, int));
1139static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1140static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1141static void x_append_glyph P_ ((struct it *));
b4192550 1142static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1143static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1144 int, int, double));
1145static void x_produce_glyphs P_ ((struct it *));
06a2c219 1146static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1147
1148
e2ef8ee6
GM
1149/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1150 is not contained in the font. */
dc43ef94 1151
06a2c219 1152static INLINE XCharStruct *
ee569018 1153x_per_char_metric (font, char2b)
06a2c219
GM
1154 XFontStruct *font;
1155 XChar2b *char2b;
1156{
1157 /* The result metric information. */
1158 XCharStruct *pcm = NULL;
dc6f92b8 1159
06a2c219 1160 xassert (font && char2b);
dc6f92b8 1161
06a2c219 1162 if (font->per_char != NULL)
dc6f92b8 1163 {
06a2c219 1164 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1165 {
06a2c219
GM
1166 /* min_char_or_byte2 specifies the linear character index
1167 corresponding to the first element of the per_char array,
1168 max_char_or_byte2 is the index of the last character. A
1169 character with non-zero CHAR2B->byte1 is not in the font.
1170 A character with byte2 less than min_char_or_byte2 or
1171 greater max_char_or_byte2 is not in the font. */
1172 if (char2b->byte1 == 0
1173 && char2b->byte2 >= font->min_char_or_byte2
1174 && char2b->byte2 <= font->max_char_or_byte2)
1175 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1176 }
06a2c219 1177 else
dc6f92b8 1178 {
06a2c219
GM
1179 /* If either min_byte1 or max_byte1 are nonzero, both
1180 min_char_or_byte2 and max_char_or_byte2 are less than
1181 256, and the 2-byte character index values corresponding
1182 to the per_char array element N (counting from 0) are:
1183
1184 byte1 = N/D + min_byte1
1185 byte2 = N\D + min_char_or_byte2
1186
1187 where:
1188
1189 D = max_char_or_byte2 - min_char_or_byte2 + 1
1190 / = integer division
1191 \ = integer modulus */
1192 if (char2b->byte1 >= font->min_byte1
1193 && char2b->byte1 <= font->max_byte1
1194 && char2b->byte2 >= font->min_char_or_byte2
1195 && char2b->byte2 <= font->max_char_or_byte2)
1196 {
1197 pcm = (font->per_char
1198 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1199 * (char2b->byte1 - font->min_byte1))
1200 + (char2b->byte2 - font->min_char_or_byte2));
1201 }
dc6f92b8 1202 }
06a2c219
GM
1203 }
1204 else
1205 {
1206 /* If the per_char pointer is null, all glyphs between the first
1207 and last character indexes inclusive have the same
1208 information, as given by both min_bounds and max_bounds. */
1209 if (char2b->byte2 >= font->min_char_or_byte2
1210 && char2b->byte2 <= font->max_char_or_byte2)
1211 pcm = &font->max_bounds;
1212 }
dc6f92b8 1213
ee569018 1214 return ((pcm == NULL
3e71d8f2 1215 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1216 ? NULL : pcm);
06a2c219 1217}
b73b6aaf 1218
57b03282 1219
06a2c219
GM
1220/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1221 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1222
06a2c219
GM
1223static INLINE void
1224x_encode_char (c, char2b, font_info)
1225 int c;
1226 XChar2b *char2b;
1227 struct font_info *font_info;
1228{
1229 int charset = CHAR_CHARSET (c);
1230 XFontStruct *font = font_info->font;
1231
1232 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1233 This may be either a program in a special encoder language or a
1234 fixed encoding. */
1235 if (font_info->font_encoder)
1236 {
1237 /* It's a program. */
1238 struct ccl_program *ccl = font_info->font_encoder;
1239
1240 if (CHARSET_DIMENSION (charset) == 1)
1241 {
1242 ccl->reg[0] = charset;
1243 ccl->reg[1] = char2b->byte2;
1244 }
1245 else
1246 {
1247 ccl->reg[0] = charset;
1248 ccl->reg[1] = char2b->byte1;
1249 ccl->reg[2] = char2b->byte2;
1250 }
1251
1252 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1253
1254 /* We assume that MSBs are appropriately set/reset by CCL
1255 program. */
1256 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1257 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1258 else
1259 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1260 }
1261 else if (font_info->encoding[charset])
1262 {
1263 /* Fixed encoding scheme. See fontset.h for the meaning of the
1264 encoding numbers. */
1265 int enc = font_info->encoding[charset];
1266
1267 if ((enc == 1 || enc == 2)
1268 && CHARSET_DIMENSION (charset) == 2)
1269 char2b->byte1 |= 0x80;
1270
1271 if (enc == 1 || enc == 3)
1272 char2b->byte2 |= 0x80;
1273 }
1274}
1275
1276
1277/* Get face and two-byte form of character C in face FACE_ID on frame
1278 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1279 means we want to display multibyte text. Value is a pointer to a
1280 realized face that is ready for display. */
1281
1282static INLINE struct face *
1283x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1284 struct frame *f;
1285 int c, face_id;
1286 XChar2b *char2b;
1287 int multibyte_p;
1288{
1289 struct face *face = FACE_FROM_ID (f, face_id);
1290
1291 if (!multibyte_p)
1292 {
1293 /* Unibyte case. We don't have to encode, but we have to make
1294 sure to use a face suitable for unibyte. */
1295 char2b->byte1 = 0;
1296 char2b->byte2 = c;
ee569018
KH
1297 face_id = FACE_FOR_CHAR (f, face, c);
1298 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1299 }
1300 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1301 {
1302 /* Case of ASCII in a face known to fit ASCII. */
1303 char2b->byte1 = 0;
1304 char2b->byte2 = c;
1305 }
1306 else
1307 {
1308 int c1, c2, charset;
1309
1310 /* Split characters into bytes. If c2 is -1 afterwards, C is
1311 really a one-byte character so that byte1 is zero. */
1312 SPLIT_CHAR (c, charset, c1, c2);
1313 if (c2 > 0)
1314 char2b->byte1 = c1, char2b->byte2 = c2;
1315 else
1316 char2b->byte1 = 0, char2b->byte2 = c1;
1317
06a2c219 1318 /* Maybe encode the character in *CHAR2B. */
ee569018 1319 if (face->font != NULL)
06a2c219
GM
1320 {
1321 struct font_info *font_info
1322 = FONT_INFO_FROM_ID (f, face->font_info_id);
1323 if (font_info)
ee569018 1324 x_encode_char (c, char2b, font_info);
06a2c219
GM
1325 }
1326 }
1327
1328 /* Make sure X resources of the face are allocated. */
1329 xassert (face != NULL);
1330 PREPARE_FACE_FOR_DISPLAY (f, face);
1331
1332 return face;
1333}
1334
1335
1336/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1337 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1338 a pointer to a realized face that is ready for display. */
1339
1340static INLINE struct face *
ee569018 1341x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1342 struct frame *f;
1343 struct glyph *glyph;
1344 XChar2b *char2b;
ee569018 1345 int *two_byte_p;
06a2c219
GM
1346{
1347 struct face *face;
1348
1349 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1350 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1351
ee569018
KH
1352 if (two_byte_p)
1353 *two_byte_p = 0;
1354
06a2c219
GM
1355 if (!glyph->multibyte_p)
1356 {
1357 /* Unibyte case. We don't have to encode, but we have to make
1358 sure to use a face suitable for unibyte. */
1359 char2b->byte1 = 0;
43d120d8 1360 char2b->byte2 = glyph->u.ch;
06a2c219 1361 }
43d120d8
KH
1362 else if (glyph->u.ch < 128
1363 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1364 {
1365 /* Case of ASCII in a face known to fit ASCII. */
1366 char2b->byte1 = 0;
43d120d8 1367 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1368 }
1369 else
1370 {
1371 int c1, c2, charset;
1372
1373 /* Split characters into bytes. If c2 is -1 afterwards, C is
1374 really a one-byte character so that byte1 is zero. */
43d120d8 1375 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1376 if (c2 > 0)
1377 char2b->byte1 = c1, char2b->byte2 = c2;
1378 else
1379 char2b->byte1 = 0, char2b->byte2 = c1;
1380
1381 /* Maybe encode the character in *CHAR2B. */
1382 if (charset != CHARSET_ASCII)
1383 {
1384 struct font_info *font_info
1385 = FONT_INFO_FROM_ID (f, face->font_info_id);
1386 if (font_info)
1387 {
43d120d8 1388 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1389 if (two_byte_p)
1390 *two_byte_p
1391 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1392 }
1393 }
1394 }
1395
1396 /* Make sure X resources of the face are allocated. */
1397 xassert (face != NULL);
1398 PREPARE_FACE_FOR_DISPLAY (f, face);
1399 return face;
1400}
1401
1402
1403/* Store one glyph for IT->char_to_display in IT->glyph_row.
1404 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1405
1406static INLINE void
1407x_append_glyph (it)
1408 struct it *it;
1409{
1410 struct glyph *glyph;
1411 enum glyph_row_area area = it->area;
1412
1413 xassert (it->glyph_row);
1414 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1415
1416 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1417 if (glyph < it->glyph_row->glyphs[area + 1])
1418 {
06a2c219
GM
1419 glyph->charpos = CHARPOS (it->position);
1420 glyph->object = it->object;
88d75730 1421 glyph->pixel_width = it->pixel_width;
06a2c219 1422 glyph->voffset = it->voffset;
88d75730 1423 glyph->type = CHAR_GLYPH;
06a2c219 1424 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1425 glyph->left_box_line_p = it->start_of_box_run_p;
1426 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1427 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1428 || it->phys_descent > it->descent);
88d75730 1429 glyph->padding_p = 0;
ee569018 1430 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1431 glyph->face_id = it->face_id;
1432 glyph->u.ch = it->char_to_display;
06a2c219
GM
1433 ++it->glyph_row->used[area];
1434 }
1435}
1436
b4192550
KH
1437/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1438 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1439
1440static INLINE void
1441x_append_composite_glyph (it)
1442 struct it *it;
1443{
1444 struct glyph *glyph;
1445 enum glyph_row_area area = it->area;
1446
1447 xassert (it->glyph_row);
1448
1449 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1450 if (glyph < it->glyph_row->glyphs[area + 1])
1451 {
b4192550
KH
1452 glyph->charpos = CHARPOS (it->position);
1453 glyph->object = it->object;
88d75730 1454 glyph->pixel_width = it->pixel_width;
b4192550 1455 glyph->voffset = it->voffset;
88d75730 1456 glyph->type = COMPOSITE_GLYPH;
b4192550 1457 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1458 glyph->left_box_line_p = it->start_of_box_run_p;
1459 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1460 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1461 || it->phys_descent > it->descent);
88d75730
GM
1462 glyph->padding_p = 0;
1463 glyph->glyph_not_available_p = 0;
1464 glyph->face_id = it->face_id;
1465 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1466 ++it->glyph_row->used[area];
1467 }
1468}
1469
06a2c219
GM
1470
1471/* Change IT->ascent and IT->height according to the setting of
1472 IT->voffset. */
1473
1474static INLINE void
1475take_vertical_position_into_account (it)
1476 struct it *it;
1477{
1478 if (it->voffset)
1479 {
1480 if (it->voffset < 0)
1481 /* Increase the ascent so that we can display the text higher
1482 in the line. */
1483 it->ascent += abs (it->voffset);
1484 else
1485 /* Increase the descent so that we can display the text lower
1486 in the line. */
1487 it->descent += it->voffset;
1488 }
1489}
1490
1491
1492/* Produce glyphs/get display metrics for the image IT is loaded with.
1493 See the description of struct display_iterator in dispextern.h for
1494 an overview of struct display_iterator. */
1495
1496static void
1497x_produce_image_glyph (it)
1498 struct it *it;
1499{
1500 struct image *img;
1501 struct face *face;
1502
1503 xassert (it->what == IT_IMAGE);
1504
1505 face = FACE_FROM_ID (it->f, it->face_id);
1506 img = IMAGE_FROM_ID (it->f, it->image_id);
1507 xassert (img);
1508
1509 /* Make sure X resources of the face and image are loaded. */
1510 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1511 prepare_image_for_display (it->f, img);
1512
95af8492 1513 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1514 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1515 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1516
1517 it->nglyphs = 1;
1518
1519 if (face->box != FACE_NO_BOX)
1520 {
ea2ba0d4
KH
1521 if (face->box_line_width > 0)
1522 {
1523 it->ascent += face->box_line_width;
1524 it->descent += face->box_line_width;
1525 }
06a2c219
GM
1526
1527 if (it->start_of_box_run_p)
ea2ba0d4 1528 it->pixel_width += abs (face->box_line_width);
06a2c219 1529 if (it->end_of_box_run_p)
ea2ba0d4 1530 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1531 }
1532
1533 take_vertical_position_into_account (it);
1534
1535 if (it->glyph_row)
1536 {
1537 struct glyph *glyph;
1538 enum glyph_row_area area = it->area;
1539
1540 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1541 if (glyph < it->glyph_row->glyphs[area + 1])
1542 {
06a2c219
GM
1543 glyph->charpos = CHARPOS (it->position);
1544 glyph->object = it->object;
88d75730 1545 glyph->pixel_width = it->pixel_width;
06a2c219 1546 glyph->voffset = it->voffset;
88d75730 1547 glyph->type = IMAGE_GLYPH;
06a2c219 1548 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1549 glyph->left_box_line_p = it->start_of_box_run_p;
1550 glyph->right_box_line_p = it->end_of_box_run_p;
1551 glyph->overlaps_vertically_p = 0;
1552 glyph->padding_p = 0;
1553 glyph->glyph_not_available_p = 0;
1554 glyph->face_id = it->face_id;
1555 glyph->u.img_id = img->id;
06a2c219
GM
1556 ++it->glyph_row->used[area];
1557 }
1558 }
1559}
1560
1561
1562/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1563 of the glyph, WIDTH and HEIGHT are the width and height of the
1564 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1565 ascent of the glyph (0 <= ASCENT <= 1). */
1566
1567static void
1568x_append_stretch_glyph (it, object, width, height, ascent)
1569 struct it *it;
1570 Lisp_Object object;
1571 int width, height;
1572 double ascent;
1573{
1574 struct glyph *glyph;
1575 enum glyph_row_area area = it->area;
1576
1577 xassert (ascent >= 0 && ascent <= 1);
1578
1579 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1580 if (glyph < it->glyph_row->glyphs[area + 1])
1581 {
06a2c219
GM
1582 glyph->charpos = CHARPOS (it->position);
1583 glyph->object = object;
88d75730 1584 glyph->pixel_width = width;
06a2c219 1585 glyph->voffset = it->voffset;
88d75730 1586 glyph->type = STRETCH_GLYPH;
06a2c219 1587 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1588 glyph->left_box_line_p = it->start_of_box_run_p;
1589 glyph->right_box_line_p = it->end_of_box_run_p;
1590 glyph->overlaps_vertically_p = 0;
1591 glyph->padding_p = 0;
1592 glyph->glyph_not_available_p = 0;
1593 glyph->face_id = it->face_id;
1594 glyph->u.stretch.ascent = height * ascent;
1595 glyph->u.stretch.height = height;
06a2c219
GM
1596 ++it->glyph_row->used[area];
1597 }
1598}
1599
1600
1601/* Produce a stretch glyph for iterator IT. IT->object is the value
1602 of the glyph property displayed. The value must be a list
1603 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1604 being recognized:
1605
1606 1. `:width WIDTH' specifies that the space should be WIDTH *
1607 canonical char width wide. WIDTH may be an integer or floating
1608 point number.
1609
1610 2. `:relative-width FACTOR' specifies that the width of the stretch
1611 should be computed from the width of the first character having the
1612 `glyph' property, and should be FACTOR times that width.
1613
1614 3. `:align-to HPOS' specifies that the space should be wide enough
1615 to reach HPOS, a value in canonical character units.
1616
1617 Exactly one of the above pairs must be present.
1618
1619 4. `:height HEIGHT' specifies that the height of the stretch produced
1620 should be HEIGHT, measured in canonical character units.
1621
269b7745 1622 5. `:relative-height FACTOR' specifies that the height of the
06a2c219
GM
1623 stretch should be FACTOR times the height of the characters having
1624 the glyph property.
1625
1626 Either none or exactly one of 4 or 5 must be present.
1627
1628 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1629 of the stretch should be used for the ascent of the stretch.
1630 ASCENT must be in the range 0 <= ASCENT <= 100. */
1631
1632#define NUMVAL(X) \
1633 ((INTEGERP (X) || FLOATP (X)) \
1634 ? XFLOATINT (X) \
1635 : - 1)
1636
1637
1638static void
1639x_produce_stretch_glyph (it)
1640 struct it *it;
1641{
1642 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1643#if GLYPH_DEBUG
1644 extern Lisp_Object Qspace;
1645#endif
1646 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1647 extern Lisp_Object QCrelative_width, QCrelative_height;
1648 extern Lisp_Object QCalign_to;
1649 Lisp_Object prop, plist;
1650 double width = 0, height = 0, ascent = 0;
1651 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1652 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1653
1654 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1655
1656 /* List should start with `space'. */
1657 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1658 plist = XCDR (it->object);
1659
1660 /* Compute the width of the stretch. */
1661 if (prop = Fplist_get (plist, QCwidth),
1662 NUMVAL (prop) > 0)
1663 /* Absolute width `:width WIDTH' specified and valid. */
1664 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1665 else if (prop = Fplist_get (plist, QCrelative_width),
1666 NUMVAL (prop) > 0)
1667 {
1668 /* Relative width `:relative-width FACTOR' specified and valid.
1669 Compute the width of the characters having the `glyph'
1670 property. */
1671 struct it it2;
1672 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1673
1674 it2 = *it;
1675 if (it->multibyte_p)
1676 {
1677 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1678 - IT_BYTEPOS (*it));
1679 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1680 }
1681 else
1682 it2.c = *p, it2.len = 1;
1683
1684 it2.glyph_row = NULL;
1685 it2.what = IT_CHARACTER;
1686 x_produce_glyphs (&it2);
1687 width = NUMVAL (prop) * it2.pixel_width;
1688 }
1689 else if (prop = Fplist_get (plist, QCalign_to),
1690 NUMVAL (prop) > 0)
1691 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1692 else
1693 /* Nothing specified -> width defaults to canonical char width. */
1694 width = CANON_X_UNIT (it->f);
1695
1696 /* Compute height. */
1697 if (prop = Fplist_get (plist, QCheight),
1698 NUMVAL (prop) > 0)
1699 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1700 else if (prop = Fplist_get (plist, QCrelative_height),
1701 NUMVAL (prop) > 0)
1702 height = FONT_HEIGHT (font) * NUMVAL (prop);
1703 else
1704 height = FONT_HEIGHT (font);
1705
1706 /* Compute percentage of height used for ascent. If
1707 `:ascent ASCENT' is present and valid, use that. Otherwise,
1708 derive the ascent from the font in use. */
1709 if (prop = Fplist_get (plist, QCascent),
1710 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1711 ascent = NUMVAL (prop) / 100.0;
1712 else
1713 ascent = (double) font->ascent / FONT_HEIGHT (font);
1714
1715 if (width <= 0)
1716 width = 1;
1717 if (height <= 0)
1718 height = 1;
1719
1720 if (it->glyph_row)
1721 {
1722 Lisp_Object object = it->stack[it->sp - 1].string;
1723 if (!STRINGP (object))
1724 object = it->w->buffer;
1725 x_append_stretch_glyph (it, object, width, height, ascent);
1726 }
1727
1728 it->pixel_width = width;
66ac4b0e
GM
1729 it->ascent = it->phys_ascent = height * ascent;
1730 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1731 it->nglyphs = 1;
1732
1733 if (face->box != FACE_NO_BOX)
1734 {
ea2ba0d4
KH
1735 if (face->box_line_width > 0)
1736 {
1737 it->ascent += face->box_line_width;
1738 it->descent += face->box_line_width;
1739 }
06a2c219
GM
1740
1741 if (it->start_of_box_run_p)
ea2ba0d4 1742 it->pixel_width += abs (face->box_line_width);
06a2c219 1743 if (it->end_of_box_run_p)
ea2ba0d4 1744 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1745 }
1746
1747 take_vertical_position_into_account (it);
1748}
1749
b4192550
KH
1750/* Return proper value to be used as baseline offset of font that has
1751 ASCENT and DESCENT to draw characters by the font at the vertical
1752 center of the line of frame F.
1753
1754 Here, out task is to find the value of BOFF in the following figure;
1755
1756 -------------------------+-----------+-
1757 -+-+---------+-+ | |
1758 | | | | | |
1759 | | | | F_ASCENT F_HEIGHT
1760 | | | ASCENT | |
1761 HEIGHT | | | | |
1762 | | |-|-+------+-----------|------- baseline
1763 | | | | BOFF | |
1764 | |---------|-+-+ | |
1765 | | | DESCENT | |
1766 -+-+---------+-+ F_DESCENT |
1767 -------------------------+-----------+-
1768
1769 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1770 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1771 DESCENT = FONT->descent
1772 HEIGHT = FONT_HEIGHT (FONT)
1773 F_DESCENT = (F->output_data.x->font->descent
1774 - F->output_data.x->baseline_offset)
1775 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1776*/
1777
458f45fa
KH
1778#define VCENTER_BASELINE_OFFSET(FONT, F) \
1779 ((FONT)->descent \
1780 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1781 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1782 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1783
1784/* Produce glyphs/get display metrics for the display element IT is
1785 loaded with. See the description of struct display_iterator in
1786 dispextern.h for an overview of struct display_iterator. */
1787
1788static void
1789x_produce_glyphs (it)
1790 struct it *it;
1791{
ee569018
KH
1792 it->glyph_not_available_p = 0;
1793
06a2c219
GM
1794 if (it->what == IT_CHARACTER)
1795 {
1796 XChar2b char2b;
1797 XFontStruct *font;
ee569018 1798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1799 XCharStruct *pcm;
06a2c219 1800 int font_not_found_p;
b4192550
KH
1801 struct font_info *font_info;
1802 int boff; /* baseline offset */
a4249304
KH
1803 /* We may change it->multibyte_p upon unibyte<->multibyte
1804 conversion. So, save the current value now and restore it
1805 later.
1806
1807 Note: It seems that we don't have to record multibyte_p in
1808 struct glyph because the character code itself tells if or
1809 not the character is multibyte. Thus, in the future, we must
1810 consider eliminating the field `multibyte_p' in the struct
c347a1c3 1811 glyph. */
a4249304 1812 int saved_multibyte_p = it->multibyte_p;
06a2c219 1813
ee569018
KH
1814 /* Maybe translate single-byte characters to multibyte, or the
1815 other way. */
06a2c219 1816 it->char_to_display = it->c;
ee569018 1817 if (!ASCII_BYTE_P (it->c))
06a2c219 1818 {
ee569018
KH
1819 if (unibyte_display_via_language_environment
1820 && SINGLE_BYTE_CHAR_P (it->c)
1821 && (it->c >= 0240
1822 || !NILP (Vnonascii_translation_table)))
1823 {
1824 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1825 it->multibyte_p = 1;
ee569018
KH
1826 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1827 face = FACE_FROM_ID (it->f, it->face_id);
1828 }
1829 else if (!SINGLE_BYTE_CHAR_P (it->c)
1830 && !it->multibyte_p)
1831 {
c347a1c3 1832 it->multibyte_p = 1;
ee569018
KH
1833 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1834 face = FACE_FROM_ID (it->f, it->face_id);
1835 }
06a2c219
GM
1836 }
1837
ee569018
KH
1838 /* Get font to use. Encode IT->char_to_display. */
1839 x_get_char_face_and_encoding (it->f, it->char_to_display,
1840 it->face_id, &char2b,
1841 it->multibyte_p);
06a2c219
GM
1842 font = face->font;
1843
1844 /* When no suitable font found, use the default font. */
1845 font_not_found_p = font == NULL;
1846 if (font_not_found_p)
b4192550
KH
1847 {
1848 font = FRAME_FONT (it->f);
1849 boff = it->f->output_data.x->baseline_offset;
1850 font_info = NULL;
1851 }
1852 else
1853 {
1854 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1855 boff = font_info->baseline_offset;
1856 if (font_info->vertical_centering)
1857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1858 }
06a2c219
GM
1859
1860 if (it->char_to_display >= ' '
1861 && (!it->multibyte_p || it->char_to_display < 128))
1862 {
1863 /* Either unibyte or ASCII. */
1864 int stretched_p;
1865
1866 it->nglyphs = 1;
06a2c219
GM
1867
1868 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1869 it->ascent = font->ascent + boff;
1870 it->descent = font->descent - boff;
474848ac
GM
1871
1872 if (pcm)
1873 {
1874 it->phys_ascent = pcm->ascent + boff;
1875 it->phys_descent = pcm->descent - boff;
1876 it->pixel_width = pcm->width;
1877 }
1878 else
1879 {
1880 it->glyph_not_available_p = 1;
1881 it->phys_ascent = font->ascent + boff;
1882 it->phys_descent = font->descent - boff;
1883 it->pixel_width = FONT_WIDTH (font);
1884 }
06a2c219
GM
1885
1886 /* If this is a space inside a region of text with
1887 `space-width' property, change its width. */
1888 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1889 if (stretched_p)
1890 it->pixel_width *= XFLOATINT (it->space_width);
1891
1892 /* If face has a box, add the box thickness to the character
1893 height. If character has a box line to the left and/or
1894 right, add the box line width to the character's width. */
1895 if (face->box != FACE_NO_BOX)
1896 {
1897 int thick = face->box_line_width;
1898
ea2ba0d4
KH
1899 if (thick > 0)
1900 {
1901 it->ascent += thick;
1902 it->descent += thick;
1903 }
1904 else
1905 thick = -thick;
1906
06a2c219
GM
1907 if (it->start_of_box_run_p)
1908 it->pixel_width += thick;
1909 if (it->end_of_box_run_p)
1910 it->pixel_width += thick;
1911 }
1912
1913 /* If face has an overline, add the height of the overline
1914 (1 pixel) and a 1 pixel margin to the character height. */
1915 if (face->overline_p)
1916 it->ascent += 2;
1917
1918 take_vertical_position_into_account (it);
1919
1920 /* If we have to actually produce glyphs, do it. */
1921 if (it->glyph_row)
1922 {
1923 if (stretched_p)
1924 {
1925 /* Translate a space with a `space-width' property
1926 into a stretch glyph. */
1927 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1928 x_append_stretch_glyph (it, it->object, it->pixel_width,
1929 it->ascent + it->descent, ascent);
1930 }
1931 else
1932 x_append_glyph (it);
1933
1934 /* If characters with lbearing or rbearing are displayed
1935 in this line, record that fact in a flag of the
1936 glyph row. This is used to optimize X output code. */
1c7e22fd 1937 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1938 it->glyph_row->contains_overlapping_glyphs_p = 1;
1939 }
1940 }
1941 else if (it->char_to_display == '\n')
1942 {
1943 /* A newline has no width but we need the height of the line. */
1944 it->pixel_width = 0;
1945 it->nglyphs = 0;
b4192550
KH
1946 it->ascent = it->phys_ascent = font->ascent + boff;
1947 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1948
ea2ba0d4
KH
1949 if (face->box != FACE_NO_BOX
1950 && face->box_line_width > 0)
06a2c219 1951 {
ea2ba0d4
KH
1952 it->ascent += face->box_line_width;
1953 it->descent += face->box_line_width;
06a2c219
GM
1954 }
1955 }
1956 else if (it->char_to_display == '\t')
1957 {
1958 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1959 int x = it->current_x + it->continuation_lines_width;
06a2c219 1960 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1961
1962 /* If the distance from the current position to the next tab
1963 stop is less than a canonical character width, use the
1964 tab stop after that. */
1965 if (next_tab_x - x < CANON_X_UNIT (it->f))
1966 next_tab_x += tab_width;
06a2c219
GM
1967
1968 it->pixel_width = next_tab_x - x;
1969 it->nglyphs = 1;
b4192550
KH
1970 it->ascent = it->phys_ascent = font->ascent + boff;
1971 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1972
1973 if (it->glyph_row)
1974 {
1975 double ascent = (double) it->ascent / (it->ascent + it->descent);
1976 x_append_stretch_glyph (it, it->object, it->pixel_width,
1977 it->ascent + it->descent, ascent);
1978 }
1979 }
1980 else
1981 {
1982 /* A multi-byte character. Assume that the display width of the
1983 character is the width of the character multiplied by the
b4192550 1984 width of the font. */
06a2c219 1985
b4192550
KH
1986 /* If we found a font, this font should give us the right
1987 metrics. If we didn't find a font, use the frame's
1988 default font and calculate the width of the character
1989 from the charset width; this is what old redisplay code
1990 did. */
1991 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1992 if (font_not_found_p || !pcm)
1993 {
1994 int charset = CHAR_CHARSET (it->char_to_display);
1995
1996 it->glyph_not_available_p = 1;
1997 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1998 * CHARSET_WIDTH (charset));
1999 it->phys_ascent = font->ascent + boff;
2000 it->phys_descent = font->descent - boff;
2001 }
2002 else
2003 {
2004 it->pixel_width = pcm->width;
2005 it->phys_ascent = pcm->ascent + boff;
2006 it->phys_descent = pcm->descent - boff;
2007 if (it->glyph_row
2008 && (pcm->lbearing < 0
2009 || pcm->rbearing > pcm->width))
2010 it->glyph_row->contains_overlapping_glyphs_p = 1;
2011 }
b4192550
KH
2012 it->nglyphs = 1;
2013 it->ascent = font->ascent + boff;
2014 it->descent = font->descent - boff;
06a2c219
GM
2015 if (face->box != FACE_NO_BOX)
2016 {
2017 int thick = face->box_line_width;
ea2ba0d4
KH
2018
2019 if (thick > 0)
2020 {
2021 it->ascent += thick;
2022 it->descent += thick;
2023 }
2024 else
2025 thick = - thick;
06a2c219
GM
2026
2027 if (it->start_of_box_run_p)
2028 it->pixel_width += thick;
2029 if (it->end_of_box_run_p)
2030 it->pixel_width += thick;
2031 }
2032
2033 /* If face has an overline, add the height of the overline
2034 (1 pixel) and a 1 pixel margin to the character height. */
2035 if (face->overline_p)
2036 it->ascent += 2;
2037
2038 take_vertical_position_into_account (it);
2039
2040 if (it->glyph_row)
2041 x_append_glyph (it);
2042 }
a4249304 2043 it->multibyte_p = saved_multibyte_p;
06a2c219 2044 }
b4192550
KH
2045 else if (it->what == IT_COMPOSITION)
2046 {
2047 /* Note: A composition is represented as one glyph in the
2048 glyph matrix. There are no padding glyphs. */
2049 XChar2b char2b;
2050 XFontStruct *font;
ee569018 2051 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2052 XCharStruct *pcm;
2053 int font_not_found_p;
2054 struct font_info *font_info;
2055 int boff; /* baseline offset */
2056 struct composition *cmp = composition_table[it->cmp_id];
2057
2058 /* Maybe translate single-byte characters to multibyte. */
2059 it->char_to_display = it->c;
2060 if (unibyte_display_via_language_environment
2061 && SINGLE_BYTE_CHAR_P (it->c)
2062 && (it->c >= 0240
2063 || (it->c >= 0200
2064 && !NILP (Vnonascii_translation_table))))
2065 {
2066 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2067 }
2068
2069 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2070 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2071 face = FACE_FROM_ID (it->f, it->face_id);
2072 x_get_char_face_and_encoding (it->f, it->char_to_display,
2073 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2074 font = face->font;
2075
2076 /* When no suitable font found, use the default font. */
2077 font_not_found_p = font == NULL;
2078 if (font_not_found_p)
2079 {
2080 font = FRAME_FONT (it->f);
2081 boff = it->f->output_data.x->baseline_offset;
2082 font_info = NULL;
2083 }
2084 else
2085 {
2086 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2087 boff = font_info->baseline_offset;
2088 if (font_info->vertical_centering)
2089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2090 }
2091
2092 /* There are no padding glyphs, so there is only one glyph to
2093 produce for the composition. Important is that pixel_width,
2094 ascent and descent are the values of what is drawn by
2095 draw_glyphs (i.e. the values of the overall glyphs composed). */
2096 it->nglyphs = 1;
2097
2098 /* If we have not yet calculated pixel size data of glyphs of
2099 the composition for the current face font, calculate them
2100 now. Theoretically, we have to check all fonts for the
2101 glyphs, but that requires much time and memory space. So,
2102 here we check only the font of the first glyph. This leads
2103 to incorrect display very rarely, and C-l (recenter) can
2104 correct the display anyway. */
2105 if (cmp->font != (void *) font)
2106 {
2107 /* Ascent and descent of the font of the first character of
2108 this composition (adjusted by baseline offset). Ascent
2109 and descent of overall glyphs should not be less than
2110 them respectively. */
2111 int font_ascent = font->ascent + boff;
2112 int font_descent = font->descent - boff;
2113 /* Bounding box of the overall glyphs. */
2114 int leftmost, rightmost, lowest, highest;
329bed06 2115 int i, width, ascent, descent;
b4192550
KH
2116
2117 cmp->font = (void *) font;
2118
2119 /* Initialize the bounding box. */
1bdeec2e
KH
2120 if (font_info
2121 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2122 {
2123 width = pcm->width;
2124 ascent = pcm->ascent;
2125 descent = pcm->descent;
2126 }
2127 else
2128 {
2129 width = FONT_WIDTH (font);
2130 ascent = font->ascent;
2131 descent = font->descent;
2132 }
2133
2134 rightmost = width;
2135 lowest = - descent + boff;
2136 highest = ascent + boff;
b4192550 2137 leftmost = 0;
329bed06 2138
b4192550
KH
2139 if (font_info
2140 && font_info->default_ascent
2141 && CHAR_TABLE_P (Vuse_default_ascent)
2142 && !NILP (Faref (Vuse_default_ascent,
2143 make_number (it->char_to_display))))
2144 highest = font_info->default_ascent + boff;
2145
2146 /* Draw the first glyph at the normal position. It may be
2147 shifted to right later if some other glyphs are drawn at
2148 the left. */
2149 cmp->offsets[0] = 0;
2150 cmp->offsets[1] = boff;
2151
2152 /* Set cmp->offsets for the remaining glyphs. */
2153 for (i = 1; i < cmp->glyph_len; i++)
2154 {
2155 int left, right, btm, top;
2156 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2157 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2158
2159 face = FACE_FROM_ID (it->f, face_id);
2160 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2161 it->multibyte_p);
b4192550
KH
2162 font = face->font;
2163 if (font == NULL)
2164 {
2165 font = FRAME_FONT (it->f);
2166 boff = it->f->output_data.x->baseline_offset;
2167 font_info = NULL;
2168 }
2169 else
2170 {
2171 font_info
2172 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2173 boff = font_info->baseline_offset;
2174 if (font_info->vertical_centering)
2175 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2176 }
2177
1bdeec2e
KH
2178 if (font_info
2179 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2180 {
2181 width = pcm->width;
2182 ascent = pcm->ascent;
2183 descent = pcm->descent;
2184 }
2185 else
2186 {
2187 width = FONT_WIDTH (font);
1bdeec2e
KH
2188 ascent = 1;
2189 descent = 0;
329bed06 2190 }
b4192550
KH
2191
2192 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2193 {
2194 /* Relative composition with or without
2195 alternate chars. */
329bed06
GM
2196 left = (leftmost + rightmost - width) / 2;
2197 btm = - descent + boff;
b4192550
KH
2198 if (font_info && font_info->relative_compose
2199 && (! CHAR_TABLE_P (Vignore_relative_composition)
2200 || NILP (Faref (Vignore_relative_composition,
2201 make_number (ch)))))
2202 {
2203
329bed06 2204 if (- descent >= font_info->relative_compose)
b4192550
KH
2205 /* One extra pixel between two glyphs. */
2206 btm = highest + 1;
329bed06 2207 else if (ascent <= 0)
b4192550 2208 /* One extra pixel between two glyphs. */
329bed06 2209 btm = lowest - 1 - ascent - descent;
b4192550
KH
2210 }
2211 }
2212 else
2213 {
2214 /* A composition rule is specified by an integer
2215 value that encodes global and new reference
2216 points (GREF and NREF). GREF and NREF are
2217 specified by numbers as below:
2218
2219 0---1---2 -- ascent
2220 | |
2221 | |
2222 | |
2223 9--10--11 -- center
2224 | |
2225 ---3---4---5--- baseline
2226 | |
2227 6---7---8 -- descent
2228 */
2229 int rule = COMPOSITION_RULE (cmp, i);
2230 int gref, nref, grefx, grefy, nrefx, nrefy;
2231
2232 COMPOSITION_DECODE_RULE (rule, gref, nref);
2233 grefx = gref % 3, nrefx = nref % 3;
2234 grefy = gref / 3, nrefy = nref / 3;
2235
2236 left = (leftmost
2237 + grefx * (rightmost - leftmost) / 2
329bed06 2238 - nrefx * width / 2);
b4192550
KH
2239 btm = ((grefy == 0 ? highest
2240 : grefy == 1 ? 0
2241 : grefy == 2 ? lowest
2242 : (highest + lowest) / 2)
329bed06
GM
2243 - (nrefy == 0 ? ascent + descent
2244 : nrefy == 1 ? descent - boff
b4192550 2245 : nrefy == 2 ? 0
329bed06 2246 : (ascent + descent) / 2));
b4192550
KH
2247 }
2248
2249 cmp->offsets[i * 2] = left;
329bed06 2250 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2251
2252 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2253 right = left + width;
2254 top = btm + descent + ascent;
b4192550
KH
2255 if (left < leftmost)
2256 leftmost = left;
2257 if (right > rightmost)
2258 rightmost = right;
2259 if (top > highest)
2260 highest = top;
2261 if (btm < lowest)
2262 lowest = btm;
2263 }
2264
2265 /* If there are glyphs whose x-offsets are negative,
2266 shift all glyphs to the right and make all x-offsets
2267 non-negative. */
2268 if (leftmost < 0)
2269 {
2270 for (i = 0; i < cmp->glyph_len; i++)
2271 cmp->offsets[i * 2] -= leftmost;
2272 rightmost -= leftmost;
2273 }
2274
2275 cmp->pixel_width = rightmost;
2276 cmp->ascent = highest;
2277 cmp->descent = - lowest;
2278 if (cmp->ascent < font_ascent)
2279 cmp->ascent = font_ascent;
2280 if (cmp->descent < font_descent)
2281 cmp->descent = font_descent;
2282 }
2283
2284 it->pixel_width = cmp->pixel_width;
2285 it->ascent = it->phys_ascent = cmp->ascent;
2286 it->descent = it->phys_descent = cmp->descent;
2287
2288 if (face->box != FACE_NO_BOX)
2289 {
2290 int thick = face->box_line_width;
ea2ba0d4
KH
2291
2292 if (thick > 0)
2293 {
2294 it->ascent += thick;
2295 it->descent += thick;
2296 }
2297 else
2298 thick = - thick;
b4192550
KH
2299
2300 if (it->start_of_box_run_p)
2301 it->pixel_width += thick;
2302 if (it->end_of_box_run_p)
2303 it->pixel_width += thick;
2304 }
2305
2306 /* If face has an overline, add the height of the overline
2307 (1 pixel) and a 1 pixel margin to the character height. */
2308 if (face->overline_p)
2309 it->ascent += 2;
2310
2311 take_vertical_position_into_account (it);
2312
2313 if (it->glyph_row)
2314 x_append_composite_glyph (it);
2315 }
06a2c219
GM
2316 else if (it->what == IT_IMAGE)
2317 x_produce_image_glyph (it);
2318 else if (it->what == IT_STRETCH)
2319 x_produce_stretch_glyph (it);
2320
3017fdd1
GM
2321 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2322 because this isn't true for images with `:ascent 100'. */
2323 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2324 if (it->area == TEXT_AREA)
2325 it->current_x += it->pixel_width;
66ac4b0e 2326
d365f5bb
GM
2327 it->descent += it->extra_line_spacing;
2328
06a2c219
GM
2329 it->max_ascent = max (it->max_ascent, it->ascent);
2330 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2331 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2332 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2333}
2334
2335
2336/* Estimate the pixel height of the mode or top line on frame F.
2337 FACE_ID specifies what line's height to estimate. */
2338
2339int
2340x_estimate_mode_line_height (f, face_id)
2341 struct frame *f;
2342 enum face_id face_id;
2343{
43281ee3 2344 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2345
2346 /* This function is called so early when Emacs starts that the face
2347 cache and mode line face are not yet initialized. */
2348 if (FRAME_FACE_CACHE (f))
2349 {
2350 struct face *face = FACE_FROM_ID (f, face_id);
2351 if (face)
43281ee3
GM
2352 {
2353 if (face->font)
2354 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2355 if (face->box_line_width > 0)
2356 height += 2 * face->box_line_width;
43281ee3 2357 }
06a2c219
GM
2358 }
2359
2360 return height;
2361}
2362
2363\f
2364/***********************************************************************
2365 Glyph display
2366 ***********************************************************************/
2367
2368/* A sequence of glyphs to be drawn in the same face.
2369
2370 This data structure is not really completely X specific, so it
2371 could possibly, at least partially, be useful for other systems. It
2372 is currently not part of the external redisplay interface because
2373 it's not clear what other systems will need. */
2374
2375struct glyph_string
2376{
2377 /* X-origin of the string. */
2378 int x;
2379
2380 /* Y-origin and y-position of the base line of this string. */
2381 int y, ybase;
2382
2383 /* The width of the string, not including a face extension. */
2384 int width;
2385
2386 /* The width of the string, including a face extension. */
2387 int background_width;
2388
2389 /* The height of this string. This is the height of the line this
2390 string is drawn in, and can be different from the height of the
2391 font the string is drawn in. */
2392 int height;
2393
2394 /* Number of pixels this string overwrites in front of its x-origin.
2395 This number is zero if the string has an lbearing >= 0; it is
2396 -lbearing, if the string has an lbearing < 0. */
2397 int left_overhang;
2398
2399 /* Number of pixels this string overwrites past its right-most
2400 nominal x-position, i.e. x + width. Zero if the string's
2401 rbearing is <= its nominal width, rbearing - width otherwise. */
2402 int right_overhang;
2403
2404 /* The frame on which the glyph string is drawn. */
2405 struct frame *f;
2406
2407 /* The window on which the glyph string is drawn. */
2408 struct window *w;
2409
2410 /* X display and window for convenience. */
2411 Display *display;
2412 Window window;
2413
2414 /* The glyph row for which this string was built. It determines the
2415 y-origin and height of the string. */
2416 struct glyph_row *row;
2417
2418 /* The area within row. */
2419 enum glyph_row_area area;
2420
2421 /* Characters to be drawn, and number of characters. */
2422 XChar2b *char2b;
2423 int nchars;
2424
06a2c219
GM
2425 /* A face-override for drawing cursors, mouse face and similar. */
2426 enum draw_glyphs_face hl;
2427
2428 /* Face in which this string is to be drawn. */
2429 struct face *face;
2430
2431 /* Font in which this string is to be drawn. */
2432 XFontStruct *font;
2433
2434 /* Font info for this string. */
2435 struct font_info *font_info;
2436
b4192550
KH
2437 /* Non-null means this string describes (part of) a composition.
2438 All characters from char2b are drawn composed. */
2439 struct composition *cmp;
06a2c219
GM
2440
2441 /* Index of this glyph string's first character in the glyph
b4192550
KH
2442 definition of CMP. If this is zero, this glyph string describes
2443 the first character of a composition. */
06a2c219
GM
2444 int gidx;
2445
2446 /* 1 means this glyph strings face has to be drawn to the right end
2447 of the window's drawing area. */
2448 unsigned extends_to_end_of_line_p : 1;
2449
2450 /* 1 means the background of this string has been drawn. */
2451 unsigned background_filled_p : 1;
2452
2453 /* 1 means glyph string must be drawn with 16-bit functions. */
2454 unsigned two_byte_p : 1;
2455
2456 /* 1 means that the original font determined for drawing this glyph
2457 string could not be loaded. The member `font' has been set to
2458 the frame's default font in this case. */
2459 unsigned font_not_found_p : 1;
2460
2461 /* 1 means that the face in which this glyph string is drawn has a
2462 stipple pattern. */
2463 unsigned stippled_p : 1;
2464
66ac4b0e
GM
2465 /* 1 means only the foreground of this glyph string must be drawn,
2466 and we should use the physical height of the line this glyph
2467 string appears in as clip rect. */
2468 unsigned for_overlaps_p : 1;
2469
06a2c219
GM
2470 /* The GC to use for drawing this glyph string. */
2471 GC gc;
2472
2473 /* A pointer to the first glyph in the string. This glyph
2474 corresponds to char2b[0]. Needed to draw rectangles if
2475 font_not_found_p is 1. */
2476 struct glyph *first_glyph;
2477
2478 /* Image, if any. */
2479 struct image *img;
2480
2481 struct glyph_string *next, *prev;
2482};
2483
2484
61869b99 2485#if GLYPH_DEBUG
06a2c219
GM
2486
2487static void
2488x_dump_glyph_string (s)
2489 struct glyph_string *s;
2490{
2491 fprintf (stderr, "glyph string\n");
2492 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2493 s->x, s->y, s->width, s->height);
2494 fprintf (stderr, " ybase = %d\n", s->ybase);
2495 fprintf (stderr, " hl = %d\n", s->hl);
2496 fprintf (stderr, " left overhang = %d, right = %d\n",
2497 s->left_overhang, s->right_overhang);
2498 fprintf (stderr, " nchars = %d\n", s->nchars);
2499 fprintf (stderr, " extends to end of line = %d\n",
2500 s->extends_to_end_of_line_p);
2501 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2502 fprintf (stderr, " bg width = %d\n", s->background_width);
2503}
2504
2505#endif /* GLYPH_DEBUG */
2506
2507
2508
2509static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2510 struct glyph_string **,
2511 struct glyph_string *,
2512 struct glyph_string *));
2513static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2514 struct glyph_string **,
2515 struct glyph_string *,
2516 struct glyph_string *));
2517static void x_append_glyph_string P_ ((struct glyph_string **,
2518 struct glyph_string **,
2519 struct glyph_string *));
2520static int x_left_overwritten P_ ((struct glyph_string *));
2521static int x_left_overwriting P_ ((struct glyph_string *));
2522static int x_right_overwritten P_ ((struct glyph_string *));
2523static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2524static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2525 int));
06a2c219
GM
2526static void x_init_glyph_string P_ ((struct glyph_string *,
2527 XChar2b *, struct window *,
2528 struct glyph_row *,
2529 enum glyph_row_area, int,
2530 enum draw_glyphs_face));
2531static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2532 enum glyph_row_area, int, int,
f0a48a01 2533 enum draw_glyphs_face, int));
06a2c219
GM
2534static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2535static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2536static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2537 int));
2538static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2539static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2540static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2541static void x_draw_glyph_string P_ ((struct glyph_string *));
2542static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2543static void x_set_cursor_gc P_ ((struct glyph_string *));
2544static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2545static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2546static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2547 int *, int *));
2548static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2549static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2550 unsigned long *, double, int));
06a2c219 2551static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2552 double, int, unsigned long));
06a2c219
GM
2553static void x_setup_relief_colors P_ ((struct glyph_string *));
2554static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2555static void x_draw_image_relief P_ ((struct glyph_string *));
2556static void x_draw_image_foreground P_ ((struct glyph_string *));
2557static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2558static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2559static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2560 int, int, int));
2561static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2562 int, int, int, int, XRectangle *));
2563static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2564 int, int, int, XRectangle *));
66ac4b0e
GM
2565static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2566 enum glyph_row_area));
209f68d9
GM
2567static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2568 struct glyph_row *,
2569 enum glyph_row_area, int, int));
06a2c219 2570
163dcff3
GM
2571#if GLYPH_DEBUG
2572static void x_check_font P_ ((struct frame *, XFontStruct *));
2573#endif
2574
06a2c219 2575
06a2c219
GM
2576/* Append the list of glyph strings with head H and tail T to the list
2577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2578
2579static INLINE void
2580x_append_glyph_string_lists (head, tail, h, t)
2581 struct glyph_string **head, **tail;
2582 struct glyph_string *h, *t;
2583{
2584 if (h)
2585 {
2586 if (*head)
2587 (*tail)->next = h;
2588 else
2589 *head = h;
2590 h->prev = *tail;
2591 *tail = t;
2592 }
2593}
2594
2595
2596/* Prepend the list of glyph strings with head H and tail T to the
2597 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2598 result. */
2599
2600static INLINE void
2601x_prepend_glyph_string_lists (head, tail, h, t)
2602 struct glyph_string **head, **tail;
2603 struct glyph_string *h, *t;
2604{
2605 if (h)
2606 {
2607 if (*head)
2608 (*head)->prev = t;
2609 else
2610 *tail = t;
2611 t->next = *head;
2612 *head = h;
2613 }
2614}
2615
2616
2617/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2618 Set *HEAD and *TAIL to the resulting list. */
2619
2620static INLINE void
2621x_append_glyph_string (head, tail, s)
2622 struct glyph_string **head, **tail;
2623 struct glyph_string *s;
2624{
2625 s->next = s->prev = NULL;
2626 x_append_glyph_string_lists (head, tail, s, s);
2627}
2628
2629
2630/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2631 face. */
2632
2633static void
2634x_set_cursor_gc (s)
2635 struct glyph_string *s;
2636{
2637 if (s->font == FRAME_FONT (s->f)
2638 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2639 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2640 && !s->cmp)
06a2c219
GM
2641 s->gc = s->f->output_data.x->cursor_gc;
2642 else
2643 {
2644 /* Cursor on non-default face: must merge. */
2645 XGCValues xgcv;
2646 unsigned long mask;
2647
2648 xgcv.background = s->f->output_data.x->cursor_pixel;
2649 xgcv.foreground = s->face->background;
2650
2651 /* If the glyph would be invisible, try a different foreground. */
2652 if (xgcv.foreground == xgcv.background)
2653 xgcv.foreground = s->face->foreground;
2654 if (xgcv.foreground == xgcv.background)
2655 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2656 if (xgcv.foreground == xgcv.background)
2657 xgcv.foreground = s->face->foreground;
2658
2659 /* Make sure the cursor is distinct from text in this face. */
2660 if (xgcv.background == s->face->background
2661 && xgcv.foreground == s->face->foreground)
2662 {
2663 xgcv.background = s->face->foreground;
2664 xgcv.foreground = s->face->background;
2665 }
2666
2667 IF_DEBUG (x_check_font (s->f, s->font));
2668 xgcv.font = s->font->fid;
2669 xgcv.graphics_exposures = False;
2670 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2671
2672 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2673 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2674 mask, &xgcv);
2675 else
2676 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2677 = XCreateGC (s->display, s->window, mask, &xgcv);
2678
2679 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2680 }
2681}
2682
2683
2684/* Set up S->gc of glyph string S for drawing text in mouse face. */
2685
2686static void
2687x_set_mouse_face_gc (s)
2688 struct glyph_string *s;
2689{
2690 int face_id;
ee569018 2691 struct face *face;
06a2c219 2692
e4ded23c 2693 /* What face has to be used last for the mouse face? */
06a2c219 2694 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2695 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2696 if (face == NULL)
2697 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2698
033e3e18
GM
2699 if (s->first_glyph->type == CHAR_GLYPH)
2700 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2701 else
2702 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2703 s->face = FACE_FROM_ID (s->f, face_id);
2704 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2705
2706 /* If font in this face is same as S->font, use it. */
2707 if (s->font == s->face->font)
2708 s->gc = s->face->gc;
2709 else
2710 {
2711 /* Otherwise construct scratch_cursor_gc with values from FACE
2712 but font FONT. */
2713 XGCValues xgcv;
2714 unsigned long mask;
2715
2716 xgcv.background = s->face->background;
2717 xgcv.foreground = s->face->foreground;
2718 IF_DEBUG (x_check_font (s->f, s->font));
2719 xgcv.font = s->font->fid;
2720 xgcv.graphics_exposures = False;
2721 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2722
2723 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2724 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2725 mask, &xgcv);
2726 else
2727 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2728 = XCreateGC (s->display, s->window, mask, &xgcv);
2729
2730 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2731 }
2732
2733 xassert (s->gc != 0);
2734}
2735
2736
2737/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2738 Faces to use in the mode line have already been computed when the
2739 matrix was built, so there isn't much to do, here. */
2740
2741static INLINE void
2742x_set_mode_line_face_gc (s)
2743 struct glyph_string *s;
2744{
2745 s->gc = s->face->gc;
06a2c219
GM
2746}
2747
2748
2749/* Set S->gc of glyph string S for drawing that glyph string. Set
2750 S->stippled_p to a non-zero value if the face of S has a stipple
2751 pattern. */
2752
2753static INLINE void
2754x_set_glyph_string_gc (s)
2755 struct glyph_string *s;
2756{
209f68d9
GM
2757 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2758
06a2c219
GM
2759 if (s->hl == DRAW_NORMAL_TEXT)
2760 {
2761 s->gc = s->face->gc;
2762 s->stippled_p = s->face->stipple != 0;
2763 }
2764 else if (s->hl == DRAW_INVERSE_VIDEO)
2765 {
2766 x_set_mode_line_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_CURSOR)
2770 {
2771 x_set_cursor_gc (s);
2772 s->stippled_p = 0;
2773 }
2774 else if (s->hl == DRAW_MOUSE_FACE)
2775 {
2776 x_set_mouse_face_gc (s);
2777 s->stippled_p = s->face->stipple != 0;
2778 }
2779 else if (s->hl == DRAW_IMAGE_RAISED
2780 || s->hl == DRAW_IMAGE_SUNKEN)
2781 {
2782 s->gc = s->face->gc;
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790
2791 /* GC must have been set. */
2792 xassert (s->gc != 0);
2793}
2794
2795
2796/* Return in *R the clipping rectangle for glyph string S. */
2797
2798static void
2799x_get_glyph_string_clip_rect (s, r)
2800 struct glyph_string *s;
2801 XRectangle *r;
2802{
2803 if (s->row->full_width_p)
2804 {
2805 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2806 int canon_x = CANON_X_UNIT (s->f);
2807
2808 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2809 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2810
2811 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2812 {
1da3fd71 2813 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2814 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2815 r->x -= width;
2816 }
2817
b9432a85 2818 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2819
06a2c219
GM
2820 /* Unless displaying a mode or menu bar line, which are always
2821 fully visible, clip to the visible part of the row. */
2822 if (s->w->pseudo_window_p)
2823 r->height = s->row->visible_height;
2824 else
2825 r->height = s->height;
2826 }
2827 else
2828 {
2829 /* This is a text line that may be partially visible. */
2830 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2831 r->width = window_box_width (s->w, s->area);
2832 r->height = s->row->visible_height;
2833 }
2834
66ac4b0e
GM
2835 /* If S draws overlapping rows, it's sufficient to use the top and
2836 bottom of the window for clipping because this glyph string
2837 intentionally draws over other lines. */
2838 if (s->for_overlaps_p)
2839 {
045dee35 2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2841 r->height = window_text_bottom_y (s->w) - r->y;
2842 }
98b8a90f
GM
2843 else
2844 {
2845 /* Don't use S->y for clipping because it doesn't take partially
2846 visible lines into account. For example, it can be negative for
2847 partially visible lines at the top of a window. */
2848 if (!s->row->full_width_p
2849 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2850 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2851 else
2852 r->y = max (0, s->row->y);
2853
2854 /* If drawing a tool-bar window, draw it over the internal border
2855 at the top of the window. */
2856 if (s->w == XWINDOW (s->f->tool_bar_window))
2857 r->y -= s->f->output_data.x->internal_border_width;
2858 }
2859
66ac4b0e 2860 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2861}
2862
2863
2864/* Set clipping for output of glyph string S. S may be part of a mode
2865 line or menu if we don't have X toolkit support. */
2866
2867static INLINE void
2868x_set_glyph_string_clipping (s)
2869 struct glyph_string *s;
2870{
2871 XRectangle r;
2872 x_get_glyph_string_clip_rect (s, &r);
2873 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2874}
2875
2876
2877/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2878 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2879
2880static INLINE void
2881x_compute_glyph_string_overhangs (s)
2882 struct glyph_string *s;
2883{
b4192550 2884 if (s->cmp == NULL
06a2c219
GM
2885 && s->first_glyph->type == CHAR_GLYPH)
2886 {
2887 XCharStruct cs;
2888 int direction, font_ascent, font_descent;
2889 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2890 &font_ascent, &font_descent, &cs);
2891 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2892 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2893 }
2894}
2895
2896
2897/* Compute overhangs and x-positions for glyph string S and its
2898 predecessors, or successors. X is the starting x-position for S.
2899 BACKWARD_P non-zero means process predecessors. */
2900
2901static void
2902x_compute_overhangs_and_x (s, x, backward_p)
2903 struct glyph_string *s;
2904 int x;
2905 int backward_p;
2906{
2907 if (backward_p)
2908 {
2909 while (s)
2910 {
2911 x_compute_glyph_string_overhangs (s);
2912 x -= s->width;
2913 s->x = x;
2914 s = s->prev;
2915 }
2916 }
2917 else
2918 {
2919 while (s)
2920 {
2921 x_compute_glyph_string_overhangs (s);
2922 s->x = x;
2923 x += s->width;
2924 s = s->next;
2925 }
2926 }
2927}
2928
2929
2930/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2931 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2932 assumed to be zero. */
06a2c219
GM
2933
2934static void
2935x_get_glyph_overhangs (glyph, f, left, right)
2936 struct glyph *glyph;
2937 struct frame *f;
2938 int *left, *right;
2939{
06a2c219
GM
2940 *left = *right = 0;
2941
b4192550 2942 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2943 {
2944 XFontStruct *font;
2945 struct face *face;
2946 struct font_info *font_info;
2947 XChar2b char2b;
ee569018
KH
2948 XCharStruct *pcm;
2949
2950 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2951 font = face->font;
2952 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2953 if (font
2954 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2955 {
06a2c219
GM
2956 if (pcm->rbearing > pcm->width)
2957 *right = pcm->rbearing - pcm->width;
2958 if (pcm->lbearing < 0)
2959 *left = -pcm->lbearing;
2960 }
2961 }
2962}
2963
2964
2965/* Return the index of the first glyph preceding glyph string S that
2966 is overwritten by S because of S's left overhang. Value is -1
2967 if no glyphs are overwritten. */
2968
2969static int
2970x_left_overwritten (s)
2971 struct glyph_string *s;
2972{
2973 int k;
2974
2975 if (s->left_overhang)
2976 {
2977 int x = 0, i;
2978 struct glyph *glyphs = s->row->glyphs[s->area];
2979 int first = s->first_glyph - glyphs;
2980
2981 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2982 x -= glyphs[i].pixel_width;
2983
2984 k = i + 1;
2985 }
2986 else
2987 k = -1;
2988
2989 return k;
2990}
2991
2992
2993/* Return the index of the first glyph preceding glyph string S that
2994 is overwriting S because of its right overhang. Value is -1 if no
2995 glyph in front of S overwrites S. */
2996
2997static int
2998x_left_overwriting (s)
2999 struct glyph_string *s;
3000{
3001 int i, k, x;
3002 struct glyph *glyphs = s->row->glyphs[s->area];
3003 int first = s->first_glyph - glyphs;
3004
3005 k = -1;
3006 x = 0;
3007 for (i = first - 1; i >= 0; --i)
3008 {
3009 int left, right;
3010 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3011 if (x + right > 0)
3012 k = i;
3013 x -= glyphs[i].pixel_width;
3014 }
3015
3016 return k;
3017}
3018
3019
3020/* Return the index of the last glyph following glyph string S that is
3021 not overwritten by S because of S's right overhang. Value is -1 if
3022 no such glyph is found. */
3023
3024static int
3025x_right_overwritten (s)
3026 struct glyph_string *s;
3027{
3028 int k = -1;
3029
3030 if (s->right_overhang)
3031 {
3032 int x = 0, i;
3033 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3034 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3035 int end = s->row->used[s->area];
3036
3037 for (i = first; i < end && s->right_overhang > x; ++i)
3038 x += glyphs[i].pixel_width;
3039
3040 k = i;
3041 }
3042
3043 return k;
3044}
3045
3046
3047/* Return the index of the last glyph following glyph string S that
3048 overwrites S because of its left overhang. Value is negative
3049 if no such glyph is found. */
3050
3051static int
3052x_right_overwriting (s)
3053 struct glyph_string *s;
3054{
3055 int i, k, x;
3056 int end = s->row->used[s->area];
3057 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3058 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3059
3060 k = -1;
3061 x = 0;
3062 for (i = first; i < end; ++i)
3063 {
3064 int left, right;
3065 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3066 if (x - left < 0)
3067 k = i;
3068 x += glyphs[i].pixel_width;
3069 }
3070
3071 return k;
3072}
3073
3074
3075/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3076
3077static INLINE void
3078x_clear_glyph_string_rect (s, x, y, w, h)
3079 struct glyph_string *s;
3080 int x, y, w, h;
3081{
3082 XGCValues xgcv;
3083 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3084 XSetForeground (s->display, s->gc, xgcv.background);
3085 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3086 XSetForeground (s->display, s->gc, xgcv.foreground);
3087}
3088
3089
3090/* Draw the background of glyph_string S. If S->background_filled_p
3091 is non-zero don't draw it. FORCE_P non-zero means draw the
3092 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3093 when a string preceding S draws into the background of S, or S
3094 contains the first component of a composition. */
06a2c219
GM
3095
3096static void
3097x_draw_glyph_string_background (s, force_p)
3098 struct glyph_string *s;
3099 int force_p;
3100{
3101 /* Nothing to do if background has already been drawn or if it
3102 shouldn't be drawn in the first place. */
3103 if (!s->background_filled_p)
3104 {
ea2ba0d4
KH
3105 int box_line_width = max (s->face->box_line_width, 0);
3106
b4192550 3107 if (s->stippled_p)
06a2c219
GM
3108 {
3109 /* Fill background with a stipple pattern. */
3110 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3111 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3112 s->y + box_line_width,
06a2c219 3113 s->background_width,
ea2ba0d4 3114 s->height - 2 * box_line_width);
06a2c219
GM
3115 XSetFillStyle (s->display, s->gc, FillSolid);
3116 s->background_filled_p = 1;
3117 }
ea2ba0d4 3118 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3119 || s->font_not_found_p
3120 || s->extends_to_end_of_line_p
06a2c219
GM
3121 || force_p)
3122 {
ea2ba0d4 3123 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3124 s->background_width,
ea2ba0d4 3125 s->height - 2 * box_line_width);
06a2c219
GM
3126 s->background_filled_p = 1;
3127 }
3128 }
3129}
3130
3131
3132/* Draw the foreground of glyph string S. */
3133
3134static void
3135x_draw_glyph_string_foreground (s)
3136 struct glyph_string *s;
3137{
3138 int i, x;
3139
3140 /* If first glyph of S has a left box line, start drawing the text
3141 of S to the right of that box line. */
3142 if (s->face->box != FACE_NO_BOX
3143 && s->first_glyph->left_box_line_p)
ea2ba0d4 3144 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3145 else
3146 x = s->x;
3147
b4192550
KH
3148 /* Draw characters of S as rectangles if S's font could not be
3149 loaded. */
3150 if (s->font_not_found_p)
06a2c219 3151 {
b4192550 3152 for (i = 0; i < s->nchars; ++i)
06a2c219 3153 {
b4192550
KH
3154 struct glyph *g = s->first_glyph + i;
3155 XDrawRectangle (s->display, s->window,
3156 s->gc, x, s->y, g->pixel_width - 1,
3157 s->height - 1);
3158 x += g->pixel_width;
06a2c219
GM
3159 }
3160 }
3161 else
3162 {
b4192550
KH
3163 char *char1b = (char *) s->char2b;
3164 int boff = s->font_info->baseline_offset;
06a2c219 3165
b4192550
KH
3166 if (s->font_info->vertical_centering)
3167 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3168
3169 /* If we can use 8-bit functions, condense S->char2b. */
3170 if (!s->two_byte_p)
3171 for (i = 0; i < s->nchars; ++i)
3172 char1b[i] = s->char2b[i].byte2;
3173
3174 /* Draw text with XDrawString if background has already been
3175 filled. Otherwise, use XDrawImageString. (Note that
3176 XDrawImageString is usually faster than XDrawString.) Always
3177 use XDrawImageString when drawing the cursor so that there is
3178 no chance that characters under a box cursor are invisible. */
3179 if (s->for_overlaps_p
3180 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3181 {
3182 /* Draw characters with 16-bit or 8-bit functions. */
3183 if (s->two_byte_p)
3184 XDrawString16 (s->display, s->window, s->gc, x,
3185 s->ybase - boff, s->char2b, s->nchars);
3186 else
3187 XDrawString (s->display, s->window, s->gc, x,
3188 s->ybase - boff, char1b, s->nchars);
3189 }
06a2c219
GM
3190 else
3191 {
b4192550
KH
3192 if (s->two_byte_p)
3193 XDrawImageString16 (s->display, s->window, s->gc, x,
3194 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3195 else
b4192550
KH
3196 XDrawImageString (s->display, s->window, s->gc, x,
3197 s->ybase - boff, char1b, s->nchars);
3198 }
3199 }
3200}
06a2c219 3201
b4192550 3202/* Draw the foreground of composite glyph string S. */
06a2c219 3203
b4192550
KH
3204static void
3205x_draw_composite_glyph_string_foreground (s)
3206 struct glyph_string *s;
3207{
3208 int i, x;
06a2c219 3209
b4192550
KH
3210 /* If first glyph of S has a left box line, start drawing the text
3211 of S to the right of that box line. */
3212 if (s->face->box != FACE_NO_BOX
3213 && s->first_glyph->left_box_line_p)
ea2ba0d4 3214 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3215 else
3216 x = s->x;
06a2c219 3217
b4192550
KH
3218 /* S is a glyph string for a composition. S->gidx is the index of
3219 the first character drawn for glyphs of this composition.
3220 S->gidx == 0 means we are drawing the very first character of
3221 this composition. */
06a2c219 3222
b4192550
KH
3223 /* Draw a rectangle for the composition if the font for the very
3224 first character of the composition could not be loaded. */
3225 if (s->font_not_found_p)
3226 {
3227 if (s->gidx == 0)
3228 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3229 s->width - 1, s->height - 1);
3230 }
3231 else
3232 {
3233 for (i = 0; i < s->nchars; i++, ++s->gidx)
3234 XDrawString16 (s->display, s->window, s->gc,
3235 x + s->cmp->offsets[s->gidx * 2],
3236 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3237 s->char2b + i, 1);
06a2c219
GM
3238 }
3239}
3240
3241
80c32bcc
GM
3242#ifdef USE_X_TOOLKIT
3243
3e71d8f2 3244static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
3245static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3246 XrmValue *, XrmValue *, XtPointer *));
3247static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3248 XrmValue *, Cardinal *));
80c32bcc 3249
3e71d8f2
GM
3250
3251/* Return the frame on which widget WIDGET is used.. Abort if frame
3252 cannot be determined. */
3253
e851c833 3254static struct frame *
3e71d8f2 3255x_frame_of_widget (widget)
80c32bcc 3256 Widget widget;
80c32bcc 3257{
80c32bcc 3258 struct x_display_info *dpyinfo;
5c187dee 3259 Lisp_Object tail;
3e71d8f2
GM
3260 struct frame *f;
3261
80c32bcc
GM
3262 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3263
3264 /* Find the top-level shell of the widget. Note that this function
3265 can be called when the widget is not yet realized, so XtWindow
3266 (widget) == 0. That's the reason we can't simply use
3267 x_any_window_to_frame. */
3268 while (!XtIsTopLevelShell (widget))
3269 widget = XtParent (widget);
3270
3271 /* Look for a frame with that top-level widget. Allocate the color
3272 on that frame to get the right gamma correction value. */
3273 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3274 if (GC_FRAMEP (XCAR (tail))
3275 && (f = XFRAME (XCAR (tail)),
3276 (f->output_data.nothing != 1
3277 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3278 && f->output_data.x->widget == widget)
3e71d8f2 3279 return f;
80c32bcc
GM
3280
3281 abort ();
3282}
3283
3e71d8f2
GM
3284
3285/* Allocate the color COLOR->pixel on the screen and display of
3286 widget WIDGET in colormap CMAP. If an exact match cannot be
3287 allocated, try the nearest color available. Value is non-zero
3288 if successful. This is called from lwlib. */
3289
3290int
3291x_alloc_nearest_color_for_widget (widget, cmap, color)
3292 Widget widget;
3293 Colormap cmap;
3294 XColor *color;
3295{
3296 struct frame *f = x_frame_of_widget (widget);
3297 return x_alloc_nearest_color (f, cmap, color);
3298}
3299
3300
46d516e5
MB
3301/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3302 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3303 If this produces the same color as PIXEL, try a color where all RGB
3304 values have DELTA added. Return the allocated color in *PIXEL.
3305 DISPLAY is the X display, CMAP is the colormap to operate on.
3306 Value is non-zero if successful. */
3307
3308int
3309x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3310 Widget widget;
3311 Display *display;
3312 Colormap cmap;
3313 unsigned long *pixel;
3314 double factor;
3315 int delta;
3316{
3317 struct frame *f = x_frame_of_widget (widget);
3318 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3319}
3320
3321
651f03b6
GM
3322/* Structure specifying which arguments should be passed by Xt to
3323 cvt_string_to_pixel. We want the widget's screen and colormap. */
3324
3325static XtConvertArgRec cvt_string_to_pixel_args[] =
3326 {
3327 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3328 sizeof (Screen *)},
3329 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3330 sizeof (Colormap)}
3331 };
3332
3333
3334/* The address of this variable is returned by
3335 cvt_string_to_pixel. */
3336
3337static Pixel cvt_string_to_pixel_value;
3338
3339
3340/* Convert a color name to a pixel color.
3341
3342 DPY is the display we are working on.
3343
3344 ARGS is an array of *NARGS XrmValue structures holding additional
3345 information about the widget for which the conversion takes place.
3346 The contents of this array are determined by the specification
3347 in cvt_string_to_pixel_args.
3348
3349 FROM is a pointer to an XrmValue which points to the color name to
3350 convert. TO is an XrmValue in which to return the pixel color.
3351
3352 CLOSURE_RET is a pointer to user-data, in which we record if
3353 we allocated the color or not.
3354
3355 Value is True if successful, False otherwise. */
3356
3357static Boolean
3358cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3359 Display *dpy;
3360 XrmValue *args;
3361 Cardinal *nargs;
3362 XrmValue *from, *to;
3363 XtPointer *closure_ret;
3364{
3365 Screen *screen;
3366 Colormap cmap;
3367 Pixel pixel;
3368 String color_name;
3369 XColor color;
3370
3371 if (*nargs != 2)
3372 {
3373 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3374 "wrongParameters", "cvt_string_to_pixel",
3375 "XtToolkitError",
3376 "Screen and colormap args required", NULL, NULL);
3377 return False;
3378 }
3379
3380 screen = *(Screen **) args[0].addr;
3381 cmap = *(Colormap *) args[1].addr;
3382 color_name = (String) from->addr;
3383
3384 if (strcmp (color_name, XtDefaultBackground) == 0)
3385 {
3386 *closure_ret = (XtPointer) False;
3387 pixel = WhitePixelOfScreen (screen);
3388 }
3389 else if (strcmp (color_name, XtDefaultForeground) == 0)
3390 {
3391 *closure_ret = (XtPointer) False;
3392 pixel = BlackPixelOfScreen (screen);
3393 }
3394 else if (XParseColor (dpy, cmap, color_name, &color)
3395 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3396 {
3397 pixel = color.pixel;
3398 *closure_ret = (XtPointer) True;
3399 }
3400 else
3401 {
3402 String params[1];
3403 Cardinal nparams = 1;
3404
3405 params[0] = color_name;
3406 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3407 "badValue", "cvt_string_to_pixel",
3408 "XtToolkitError", "Invalid color `%s'",
3409 params, &nparams);
3410 return False;
3411 }
3412
3413 if (to->addr != NULL)
3414 {
3415 if (to->size < sizeof (Pixel))
3416 {
3417 to->size = sizeof (Pixel);
3418 return False;
3419 }
3420
3421 *(Pixel *) to->addr = pixel;
3422 }
3423 else
3424 {
3425 cvt_string_to_pixel_value = pixel;
3426 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3427 }
3428
3429 to->size = sizeof (Pixel);
3430 return True;
3431}
3432
3433
3434/* Free a pixel color which was previously allocated via
3435 cvt_string_to_pixel. This is registered as the destructor
3436 for this type of resource via XtSetTypeConverter.
3437
3438 APP is the application context in which we work.
3439
3440 TO is a pointer to an XrmValue holding the color to free.
3441 CLOSURE is the value we stored in CLOSURE_RET for this color
3442 in cvt_string_to_pixel.
3443
3444 ARGS and NARGS are like for cvt_string_to_pixel. */
3445
3446static void
3447cvt_pixel_dtor (app, to, closure, args, nargs)
3448 XtAppContext app;
3449 XrmValuePtr to;
3450 XtPointer closure;
3451 XrmValuePtr args;
3452 Cardinal *nargs;
3453{
3454 if (*nargs != 2)
3455 {
3456 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3457 "XtToolkitError",
3458 "Screen and colormap arguments required",
3459 NULL, NULL);
3460 }
3461 else if (closure != NULL)
3462 {
3463 /* We did allocate the pixel, so free it. */
3464 Screen *screen = *(Screen **) args[0].addr;
3465 Colormap cmap = *(Colormap *) args[1].addr;
3466 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 3467 (Pixel *) to->addr, 1);
651f03b6
GM
3468 }
3469}
3470
3471
80c32bcc
GM
3472#endif /* USE_X_TOOLKIT */
3473
3474
f04e1297 3475/* Value is an array of XColor structures for the contents of the
651f03b6 3476 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
3477 Note that this probably shouldn't be called for large color maps,
3478 say a 24-bit TrueColor map. */
3479
3480static const XColor *
651f03b6
GM
3481x_color_cells (dpy, ncells)
3482 Display *dpy;
f04e1297
GM
3483 int *ncells;
3484{
651f03b6 3485 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
3486
3487 if (dpyinfo->color_cells == NULL)
3488 {
651f03b6 3489 Screen *screen = dpyinfo->screen;
f04e1297
GM
3490 int i;
3491
3492 dpyinfo->ncolor_cells
651f03b6 3493 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
3494 dpyinfo->color_cells
3495 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3496 * sizeof *dpyinfo->color_cells);
3497
3498 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3499 dpyinfo->color_cells[i].pixel = i;
3500
651f03b6 3501 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
3502 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3503 }
3504
3505 *ncells = dpyinfo->ncolor_cells;
3506 return dpyinfo->color_cells;
3507}
3508
3509
3510/* On frame F, translate pixel colors to RGB values for the NCOLORS
3511 colors in COLORS. Use cached information, if available. */
3512
3513void
3514x_query_colors (f, colors, ncolors)
3515 struct frame *f;
3516 XColor *colors;
3517 int ncolors;
3518{
3519 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3520
3521 if (dpyinfo->color_cells)
3522 {
3523 int i;
3524 for (i = 0; i < ncolors; ++i)
3525 {
3526 unsigned long pixel = colors[i].pixel;
3527 xassert (pixel < dpyinfo->ncolor_cells);
3528 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3529 colors[i] = dpyinfo->color_cells[pixel];
3530 }
3531 }
3532 else
3533 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3534}
3535
3536
3537/* On frame F, translate pixel color to RGB values for the color in
3538 COLOR. Use cached information, if available. */
3539
3540void
3541x_query_color (f, color)
3542 struct frame *f;
3543 XColor *color;
3544{
3545 x_query_colors (f, color, 1);
3546}
3547
3548
651f03b6
GM
3549/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3550 exact match can't be allocated, try the nearest color available.
3551 Value is non-zero if successful. Set *COLOR to the color
3552 allocated. */
06a2c219 3553
651f03b6
GM
3554static int
3555x_alloc_nearest_color_1 (dpy, cmap, color)
3556 Display *dpy;
06a2c219
GM
3557 Colormap cmap;
3558 XColor *color;
3559{
80c32bcc
GM
3560 int rc;
3561
651f03b6 3562 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
3563 if (rc == 0)
3564 {
3565 /* If we got to this point, the colormap is full, so we're going
3566 to try to get the next closest color. The algorithm used is
3567 a least-squares matching, which is what X uses for closest
3568 color matching with StaticColor visuals. */
3569 int nearest, i;
3570 unsigned long nearest_delta = ~0;
f04e1297 3571 int ncells;
651f03b6 3572 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
3573
3574 for (nearest = i = 0; i < ncells; ++i)
3575 {
3576 long dred = (color->red >> 8) - (cells[i].red >> 8);
3577 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3578 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3579 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3580
3581 if (delta < nearest_delta)
3582 {
3583 nearest = i;
3584 nearest_delta = delta;
3585 }
3586 }
3587
3588 color->red = cells[nearest].red;
3589 color->green = cells[nearest].green;
3590 color->blue = cells[nearest].blue;
651f03b6 3591 rc = XAllocColor (dpy, cmap, color);
06a2c219 3592 }
35efe0a1
GM
3593 else
3594 {
3595 /* If allocation succeeded, and the allocated pixel color is not
3596 equal to a cached pixel color recorded earlier, there was a
3597 change in the colormap, so clear the color cache. */
651f03b6 3598 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1
GM
3599 XColor *cached_color;
3600
3601 if (dpyinfo->color_cells
3602 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3603 (cached_color->red != color->red
3604 || cached_color->blue != color->blue
3605 || cached_color->green != color->green)))
35efe0a1
GM
3606 {
3607 xfree (dpyinfo->color_cells);
3608 dpyinfo->color_cells = NULL;
3609 dpyinfo->ncolor_cells = 0;
3610 }
3611 }
06a2c219 3612
d9c545da
GM
3613#ifdef DEBUG_X_COLORS
3614 if (rc)
3615 register_color (color->pixel);
3616#endif /* DEBUG_X_COLORS */
3617
06a2c219
GM
3618 return rc;
3619}
3620
3621
651f03b6
GM
3622/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3623 exact match can't be allocated, try the nearest color available.
3624 Value is non-zero if successful. Set *COLOR to the color
3625 allocated. */
3626
3627int
3628x_alloc_nearest_color (f, cmap, color)
3629 struct frame *f;
3630 Colormap cmap;
3631 XColor *color;
3632{
3633 gamma_correct (f, color);
3634 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3635}
3636
3637
d9c545da
GM
3638/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3639 It's necessary to do this instead of just using PIXEL directly to
3640 get color reference counts right. */
3641
3642unsigned long
3643x_copy_color (f, pixel)
3644 struct frame *f;
3645 unsigned long pixel;
3646{
3647 XColor color;
3648
3649 color.pixel = pixel;
3650 BLOCK_INPUT;
f04e1297 3651 x_query_color (f, &color);
d9c545da
GM
3652 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3653 UNBLOCK_INPUT;
3654#ifdef DEBUG_X_COLORS
3655 register_color (pixel);
3656#endif
3657 return color.pixel;
3658}
3659
3660
3e71d8f2
GM
3661/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3662 It's necessary to do this instead of just using PIXEL directly to
3663 get color reference counts right. */
3664
3665unsigned long
3666x_copy_dpy_color (dpy, cmap, pixel)
3667 Display *dpy;
3668 Colormap cmap;
3669 unsigned long pixel;
3670{
3671 XColor color;
3672
3673 color.pixel = pixel;
3674 BLOCK_INPUT;
3675 XQueryColor (dpy, cmap, &color);
3676 XAllocColor (dpy, cmap, &color);
3677 UNBLOCK_INPUT;
3678#ifdef DEBUG_X_COLORS
3679 register_color (pixel);
3680#endif
3681 return color.pixel;
3682}
3683
3684
6d8b0acd 3685/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3686 boosted.
6d8b0acd 3687
d7361edf
MB
3688 Nominally, highlight colors for `3d' faces are calculated by
3689 brightening an object's color by a constant scale factor, but this
3690 doesn't yield good results for dark colors, so for colors who's
3691 brightness is less than this value (on a scale of 0-65535) have an
3692 use an additional additive factor.
6d8b0acd
MB
3693
3694 The value here is set so that the default menu-bar/mode-line color
3695 (grey75) will not have its highlights changed at all. */
3696#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3697
3698
06a2c219
GM
3699/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3700 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3701 If this produces the same color as PIXEL, try a color where all RGB
3702 values have DELTA added. Return the allocated color in *PIXEL.
3703 DISPLAY is the X display, CMAP is the colormap to operate on.
3704 Value is non-zero if successful. */
3705
3706static int
3707x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3708 struct frame *f;
3709 Display *display;
3710 Colormap cmap;
3711 unsigned long *pixel;
68c45bf0 3712 double factor;
06a2c219
GM
3713 int delta;
3714{
3715 XColor color, new;
6d8b0acd 3716 long bright;
06a2c219
GM
3717 int success_p;
3718
3719 /* Get RGB color values. */
3720 color.pixel = *pixel;
f04e1297 3721 x_query_color (f, &color);
06a2c219
GM
3722
3723 /* Change RGB values by specified FACTOR. Avoid overflow! */
3724 xassert (factor >= 0);
3725 new.red = min (0xffff, factor * color.red);
3726 new.green = min (0xffff, factor * color.green);
3727 new.blue = min (0xffff, factor * color.blue);
3728
d7361edf
MB
3729 /* Calculate brightness of COLOR. */
3730 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3731
3732 /* We only boost colors that are darker than
3733 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3734 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3735 /* Make an additive adjustment to NEW, because it's dark enough so
3736 that scaling by FACTOR alone isn't enough. */
3737 {
3738 /* How far below the limit this color is (0 - 1, 1 being darker). */
3739 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3740 /* The additive adjustment. */
d7361edf 3741 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3742
3743 if (factor < 1)
3744 {
6d8b0acd
MB
3745 new.red = max (0, new.red - min_delta);
3746 new.green = max (0, new.green - min_delta);
3747 new.blue = max (0, new.blue - min_delta);
3748 }
3749 else
3750 {
3751 new.red = min (0xffff, min_delta + new.red);
3752 new.green = min (0xffff, min_delta + new.green);
3753 new.blue = min (0xffff, min_delta + new.blue);
3754 }
3755 }
3756
06a2c219 3757 /* Try to allocate the color. */
80c32bcc 3758 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3759 if (success_p)
3760 {
3761 if (new.pixel == *pixel)
3762 {
3763 /* If we end up with the same color as before, try adding
3764 delta to the RGB values. */
0d605c67 3765 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3766
3767 new.red = min (0xffff, delta + color.red);
3768 new.green = min (0xffff, delta + color.green);
3769 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3770 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3771 }
3772 else
3773 success_p = 1;
3774 *pixel = new.pixel;
3775 }
3776
3777 return success_p;
3778}
3779
3780
3781/* Set up the foreground color for drawing relief lines of glyph
3782 string S. RELIEF is a pointer to a struct relief containing the GC
3783 with which lines will be drawn. Use a color that is FACTOR or
3784 DELTA lighter or darker than the relief's background which is found
3785 in S->f->output_data.x->relief_background. If such a color cannot
3786 be allocated, use DEFAULT_PIXEL, instead. */
3787
3788static void
3789x_setup_relief_color (f, relief, factor, delta, default_pixel)
3790 struct frame *f;
3791 struct relief *relief;
68c45bf0 3792 double factor;
06a2c219
GM
3793 int delta;
3794 unsigned long default_pixel;
3795{
3796 XGCValues xgcv;
3797 struct x_output *di = f->output_data.x;
3798 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3799 unsigned long pixel;
3800 unsigned long background = di->relief_background;
43bd1b2b 3801 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3802 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3803 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3804
3805 xgcv.graphics_exposures = False;
3806 xgcv.line_width = 1;
3807
3808 /* Free previously allocated color. The color cell will be reused
3809 when it has been freed as many times as it was allocated, so this
3810 doesn't affect faces using the same colors. */
3811 if (relief->gc
3812 && relief->allocated_p)
3813 {
0d605c67 3814 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3815 relief->allocated_p = 0;
3816 }
3817
3818 /* Allocate new color. */
3819 xgcv.foreground = default_pixel;
3820 pixel = background;
dcd08bfb
GM
3821 if (dpyinfo->n_planes != 1
3822 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3823 {
3824 relief->allocated_p = 1;
3825 xgcv.foreground = relief->pixel = pixel;
3826 }
3827
3828 if (relief->gc == 0)
3829 {
dcd08bfb 3830 xgcv.stipple = dpyinfo->gray;
06a2c219 3831 mask |= GCStipple;
dcd08bfb 3832 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3833 }
3834 else
dcd08bfb 3835 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3836}
3837
3838
3839/* Set up colors for the relief lines around glyph string S. */
3840
3841static void
3842x_setup_relief_colors (s)
3843 struct glyph_string *s;
3844{
3845 struct x_output *di = s->f->output_data.x;
3846 unsigned long color;
3847
3848 if (s->face->use_box_color_for_shadows_p)
3849 color = s->face->box_color;
e2a57b34 3850 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 3851 && s->img->pixmap
e2a57b34
MB
3852 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3853 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
3854 else
3855 {
3856 XGCValues xgcv;
3857
3858 /* Get the background color of the face. */
3859 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3860 color = xgcv.background;
3861 }
3862
3863 if (di->white_relief.gc == 0
3864 || color != di->relief_background)
3865 {
3866 di->relief_background = color;
3867 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3868 WHITE_PIX_DEFAULT (s->f));
3869 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3870 BLACK_PIX_DEFAULT (s->f));
3871 }
3872}
3873
3874
3875/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3876 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3877 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3878 relief. LEFT_P non-zero means draw a relief on the left side of
3879 the rectangle. RIGHT_P non-zero means draw a relief on the right
3880 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3881 when drawing. */
3882
3883static void
3884x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3885 raised_p, left_p, right_p, clip_rect)
3886 struct frame *f;
3887 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3888 XRectangle *clip_rect;
3889{
de507556
GM
3890 Display *dpy = FRAME_X_DISPLAY (f);
3891 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
3892 int i;
3893 GC gc;
3894
3895 if (raised_p)
3896 gc = f->output_data.x->white_relief.gc;
3897 else
3898 gc = f->output_data.x->black_relief.gc;
de507556 3899 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3900
3901 /* Top. */
3902 for (i = 0; i < width; ++i)
de507556 3903 XDrawLine (dpy, window, gc,
06a2c219
GM
3904 left_x + i * left_p, top_y + i,
3905 right_x + 1 - i * right_p, top_y + i);
3906
3907 /* Left. */
3908 if (left_p)
3909 for (i = 0; i < width; ++i)
de507556 3910 XDrawLine (dpy, window, gc,
44655e77 3911 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 3912
de507556 3913 XSetClipMask (dpy, gc, None);
06a2c219
GM
3914 if (raised_p)
3915 gc = f->output_data.x->black_relief.gc;
3916 else
3917 gc = f->output_data.x->white_relief.gc;
de507556 3918 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3919
3920 /* Bottom. */
3921 for (i = 0; i < width; ++i)
de507556
GM
3922 XDrawLine (dpy, window, gc,
3923 left_x + i * left_p, bottom_y - i,
327f42ee 3924 right_x + 1 - i * right_p, bottom_y - i);
06a2c219
GM
3925
3926 /* Right. */
3927 if (right_p)
3928 for (i = 0; i < width; ++i)
de507556 3929 XDrawLine (dpy, window, gc,
06a2c219
GM
3930 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3931
de507556 3932 XSetClipMask (dpy, gc, None);
06a2c219
GM
3933}
3934
3935
3936/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3937 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3938 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3939 left side of the rectangle. RIGHT_P non-zero means draw a line
3940 on the right side of the rectangle. CLIP_RECT is the clipping
3941 rectangle to use when drawing. */
3942
3943static void
3944x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3945 left_p, right_p, clip_rect)
3946 struct glyph_string *s;
3947 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3948 XRectangle *clip_rect;
3949{
3950 XGCValues xgcv;
3951
3952 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3953 XSetForeground (s->display, s->gc, s->face->box_color);
3954 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3955
3956 /* Top. */
3957 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3958 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3959
3960 /* Left. */
3961 if (left_p)
3962 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3963 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3964
3965 /* Bottom. */
3966 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3967 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3968
3969 /* Right. */
3970 if (right_p)
3971 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3972 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3973
3974 XSetForeground (s->display, s->gc, xgcv.foreground);
3975 XSetClipMask (s->display, s->gc, None);
3976}
3977
3978
3979/* Draw a box around glyph string S. */
3980
3981static void
3982x_draw_glyph_string_box (s)
3983 struct glyph_string *s;
3984{
3985 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3986 int left_p, right_p;
3987 struct glyph *last_glyph;
3988 XRectangle clip_rect;
3989
3990 last_x = window_box_right (s->w, s->area);
3991 if (s->row->full_width_p
3992 && !s->w->pseudo_window_p)
3993 {
3f332ef3 3994 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
06a2c219
GM
3995 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3996 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3997 }
3998
3999 /* The glyph that may have a right box line. */
b4192550 4000 last_glyph = (s->cmp || s->img
06a2c219
GM
4001 ? s->first_glyph
4002 : s->first_glyph + s->nchars - 1);
4003
ea2ba0d4 4004 width = abs (s->face->box_line_width);
06a2c219
GM
4005 raised_p = s->face->box == FACE_RAISED_BOX;
4006 left_x = s->x;
57ac7c81
GM
4007 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4008 ? last_x - 1
4009 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
4010 top_y = s->y;
4011 bottom_y = top_y + s->height - 1;
4012
4013 left_p = (s->first_glyph->left_box_line_p
4014 || (s->hl == DRAW_MOUSE_FACE
4015 && (s->prev == NULL
4016 || s->prev->hl != s->hl)));
4017 right_p = (last_glyph->right_box_line_p
4018 || (s->hl == DRAW_MOUSE_FACE
4019 && (s->next == NULL
4020 || s->next->hl != s->hl)));
327f42ee 4021
06a2c219
GM
4022 x_get_glyph_string_clip_rect (s, &clip_rect);
4023
4024 if (s->face->box == FACE_SIMPLE_BOX)
4025 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4026 left_p, right_p, &clip_rect);
4027 else
4028 {
4029 x_setup_relief_colors (s);
4030 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4031 width, raised_p, left_p, right_p, &clip_rect);
4032 }
4033}
4034
4035
4036/* Draw foreground of image glyph string S. */
4037
4038static void
4039x_draw_image_foreground (s)
4040 struct glyph_string *s;
4041{
4042 int x;
95af8492 4043 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4044
4045 /* If first glyph of S has a left box line, start drawing it to the
4046 right of that line. */
4047 if (s->face->box != FACE_NO_BOX
4048 && s->first_glyph->left_box_line_p)
ea2ba0d4 4049 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4050 else
4051 x = s->x;
4052
4053 /* If there is a margin around the image, adjust x- and y-position
4054 by that margin. */
22d650b8
GM
4055 x += s->img->hmargin;
4056 y += s->img->vmargin;
06a2c219
GM
4057
4058 if (s->img->pixmap)
4059 {
4060 if (s->img->mask)
4061 {
4062 /* We can't set both a clip mask and use XSetClipRectangles
4063 because the latter also sets a clip mask. We also can't
4064 trust on the shape extension to be available
4065 (XShapeCombineRegion). So, compute the rectangle to draw
4066 manually. */
4067 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4068 | GCFunction);
4069 XGCValues xgcv;
4070 XRectangle clip_rect, image_rect, r;
4071
4072 xgcv.clip_mask = s->img->mask;
4073 xgcv.clip_x_origin = x;
4074 xgcv.clip_y_origin = y;
4075 xgcv.function = GXcopy;
4076 XChangeGC (s->display, s->gc, mask, &xgcv);
4077
4078 x_get_glyph_string_clip_rect (s, &clip_rect);
4079 image_rect.x = x;
4080 image_rect.y = y;
4081 image_rect.width = s->img->width;
4082 image_rect.height = s->img->height;
4083 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4084 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4085 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4086 }
4087 else
4088 {
49ad1d99
GM
4089 XRectangle clip_rect, image_rect, r;
4090
4091 x_get_glyph_string_clip_rect (s, &clip_rect);
4092 image_rect.x = x;
4093 image_rect.y = y;
4094 image_rect.width = s->img->width;
4095 image_rect.height = s->img->height;
4096 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4097 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4098 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
4099
4100 /* When the image has a mask, we can expect that at
4101 least part of a mouse highlight or a block cursor will
4102 be visible. If the image doesn't have a mask, make
4103 a block cursor visible by drawing a rectangle around
4104 the image. I believe it's looking better if we do
4105 nothing here for mouse-face. */
4106 if (s->hl == DRAW_CURSOR)
4107 XDrawRectangle (s->display, s->window, s->gc, x, y,
4108 s->img->width - 1, s->img->height - 1);
4109 }
4110 }
4111 else
4112 /* Draw a rectangle if image could not be loaded. */
4113 XDrawRectangle (s->display, s->window, s->gc, x, y,
4114 s->img->width - 1, s->img->height - 1);
4115}
4116
4117
4118/* Draw a relief around the image glyph string S. */
4119
4120static void
4121x_draw_image_relief (s)
4122 struct glyph_string *s;
4123{
4124 int x0, y0, x1, y1, thick, raised_p;
4125 XRectangle r;
4126 int x;
95af8492 4127 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4128
4129 /* If first glyph of S has a left box line, start drawing it to the
4130 right of that line. */
4131 if (s->face->box != FACE_NO_BOX
4132 && s->first_glyph->left_box_line_p)
ea2ba0d4 4133 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4134 else
4135 x = s->x;
4136
4137 /* If there is a margin around the image, adjust x- and y-position
4138 by that margin. */
22d650b8
GM
4139 x += s->img->hmargin;
4140 y += s->img->vmargin;
06a2c219
GM
4141
4142 if (s->hl == DRAW_IMAGE_SUNKEN
4143 || s->hl == DRAW_IMAGE_RAISED)
4144 {
62854fe2 4145 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
4146 raised_p = s->hl == DRAW_IMAGE_RAISED;
4147 }
4148 else
4149 {
4150 thick = abs (s->img->relief);
4151 raised_p = s->img->relief > 0;
4152 }
4153
4154 x0 = x - thick;
4155 y0 = y - thick;
4156 x1 = x + s->img->width + thick - 1;
4157 y1 = y + s->img->height + thick - 1;
4158
4159 x_setup_relief_colors (s);
4160 x_get_glyph_string_clip_rect (s, &r);
4161 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4162}
4163
4164
4165/* Draw the foreground of image glyph string S to PIXMAP. */
4166
4167static void
4168x_draw_image_foreground_1 (s, pixmap)
4169 struct glyph_string *s;
4170 Pixmap pixmap;
4171{
4172 int x;
95af8492 4173 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4174
4175 /* If first glyph of S has a left box line, start drawing it to the
4176 right of that line. */
4177 if (s->face->box != FACE_NO_BOX
4178 && s->first_glyph->left_box_line_p)
ea2ba0d4 4179 x = abs (s->face->box_line_width);
06a2c219
GM
4180 else
4181 x = 0;
4182
4183 /* If there is a margin around the image, adjust x- and y-position
4184 by that margin. */
22d650b8
GM
4185 x += s->img->hmargin;
4186 y += s->img->vmargin;
dc43ef94 4187
06a2c219
GM
4188 if (s->img->pixmap)
4189 {
4190 if (s->img->mask)
4191 {
4192 /* We can't set both a clip mask and use XSetClipRectangles
4193 because the latter also sets a clip mask. We also can't
4194 trust on the shape extension to be available
4195 (XShapeCombineRegion). So, compute the rectangle to draw
4196 manually. */
4197 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4198 | GCFunction);
4199 XGCValues xgcv;
4200
4201 xgcv.clip_mask = s->img->mask;
4202 xgcv.clip_x_origin = x;
4203 xgcv.clip_y_origin = y;
4204 xgcv.function = GXcopy;
4205 XChangeGC (s->display, s->gc, mask, &xgcv);
4206
4207 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4208 0, 0, s->img->width, s->img->height, x, y);
4209 XSetClipMask (s->display, s->gc, None);
4210 }
4211 else
4212 {
4213 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4214 0, 0, s->img->width, s->img->height, x, y);
4215
4216 /* When the image has a mask, we can expect that at
4217 least part of a mouse highlight or a block cursor will
4218 be visible. If the image doesn't have a mask, make
4219 a block cursor visible by drawing a rectangle around
4220 the image. I believe it's looking better if we do
4221 nothing here for mouse-face. */
4222 if (s->hl == DRAW_CURSOR)
4223 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4224 s->img->width - 1, s->img->height - 1);
4225 }
4226 }
4227 else
4228 /* Draw a rectangle if image could not be loaded. */
4229 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4230 s->img->width - 1, s->img->height - 1);
4231}
dc43ef94 4232
990ba854 4233
06a2c219
GM
4234/* Draw part of the background of glyph string S. X, Y, W, and H
4235 give the rectangle to draw. */
a9a5b0a5 4236
06a2c219
GM
4237static void
4238x_draw_glyph_string_bg_rect (s, x, y, w, h)
4239 struct glyph_string *s;
4240 int x, y, w, h;
4241{
4242 if (s->stippled_p)
4243 {
4244 /* Fill background with a stipple pattern. */
4245 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4246 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4247 XSetFillStyle (s->display, s->gc, FillSolid);
4248 }
4249 else
4250 x_clear_glyph_string_rect (s, x, y, w, h);
4251}
07e34cb0 4252
b5210ea7 4253
06a2c219 4254/* Draw image glyph string S.
dc43ef94 4255
06a2c219
GM
4256 s->y
4257 s->x +-------------------------
4258 | s->face->box
4259 |
4260 | +-------------------------
4261 | | s->img->margin
4262 | |
4263 | | +-------------------
4264 | | | the image
dc43ef94 4265
06a2c219 4266 */
dc43ef94 4267
06a2c219
GM
4268static void
4269x_draw_image_glyph_string (s)
4270 struct glyph_string *s;
4271{
4272 int x, y;
ea2ba0d4
KH
4273 int box_line_hwidth = abs (s->face->box_line_width);
4274 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4275 int height;
4276 Pixmap pixmap = None;
4277
ea2ba0d4 4278 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4279
4280 /* Fill background with face under the image. Do it only if row is
4281 taller than image or if image has a clip mask to reduce
4282 flickering. */
4283 s->stippled_p = s->face->stipple != 0;
4284 if (height > s->img->height
22d650b8
GM
4285 || s->img->hmargin
4286 || s->img->vmargin
06a2c219
GM
4287 || s->img->mask
4288 || s->img->pixmap == 0
4289 || s->width != s->background_width)
4290 {
ea2ba0d4
KH
4291 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4292 x = s->x + box_line_hwidth;
06a2c219
GM
4293 else
4294 x = s->x;
4295
ea2ba0d4 4296 y = s->y + box_line_vwidth;
06a2c219
GM
4297
4298 if (s->img->mask)
4299 {
f9b5db02
GM
4300 /* Create a pixmap as large as the glyph string. Fill it
4301 with the background color. Copy the image to it, using
4302 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4303 Screen *screen = FRAME_X_SCREEN (s->f);
4304 int depth = DefaultDepthOfScreen (screen);
4305
4306 /* Create a pixmap as large as the glyph string. */
4307 pixmap = XCreatePixmap (s->display, s->window,
4308 s->background_width,
4309 s->height, depth);
4310
4311 /* Don't clip in the following because we're working on the
4312 pixmap. */
4313 XSetClipMask (s->display, s->gc, None);
4314
4315 /* Fill the pixmap with the background color/stipple. */
4316 if (s->stippled_p)
4317 {
4318 /* Fill background with a stipple pattern. */
4319 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4320 XFillRectangle (s->display, pixmap, s->gc,
4321 0, 0, s->background_width, s->height);
4322 XSetFillStyle (s->display, s->gc, FillSolid);
4323 }
4324 else
4325 {
4326 XGCValues xgcv;
4327 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4328 &xgcv);
4329 XSetForeground (s->display, s->gc, xgcv.background);
4330 XFillRectangle (s->display, pixmap, s->gc,
4331 0, 0, s->background_width, s->height);
4332 XSetForeground (s->display, s->gc, xgcv.foreground);
4333 }
4334 }
4335 else
06a2c219
GM
4336 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4337
4338 s->background_filled_p = 1;
4339 }
dc43ef94 4340
06a2c219
GM
4341 /* Draw the foreground. */
4342 if (pixmap != None)
4343 {
4344 x_draw_image_foreground_1 (s, pixmap);
4345 x_set_glyph_string_clipping (s);
4346 XCopyArea (s->display, pixmap, s->window, s->gc,
4347 0, 0, s->background_width, s->height, s->x, s->y);
4348 XFreePixmap (s->display, pixmap);
4349 }
4350 else
4351 x_draw_image_foreground (s);
b5210ea7 4352
06a2c219
GM
4353 /* If we must draw a relief around the image, do it. */
4354 if (s->img->relief
4355 || s->hl == DRAW_IMAGE_RAISED
4356 || s->hl == DRAW_IMAGE_SUNKEN)
4357 x_draw_image_relief (s);
4358}
8c1a6a84 4359
990ba854 4360
06a2c219 4361/* Draw stretch glyph string S. */
dc43ef94 4362
06a2c219
GM
4363static void
4364x_draw_stretch_glyph_string (s)
4365 struct glyph_string *s;
4366{
4367 xassert (s->first_glyph->type == STRETCH_GLYPH);
4368 s->stippled_p = s->face->stipple != 0;
990ba854 4369
06a2c219
GM
4370 if (s->hl == DRAW_CURSOR
4371 && !x_stretch_cursor_p)
4372 {
4373 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4374 as wide as the stretch glyph. */
4375 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4376
06a2c219
GM
4377 /* Draw cursor. */
4378 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4379
06a2c219
GM
4380 /* Clear rest using the GC of the original non-cursor face. */
4381 if (width < s->background_width)
4382 {
06a2c219
GM
4383 int x = s->x + width, y = s->y;
4384 int w = s->background_width - width, h = s->height;
4385 XRectangle r;
b7f83f9e 4386 GC gc;
dc43ef94 4387
b7f83f9e
GM
4388 if (s->row->mouse_face_p
4389 && cursor_in_mouse_face_p (s->w))
4390 {
4391 x_set_mouse_face_gc (s);
4392 gc = s->gc;
4393 }
4394 else
4395 gc = s->face->gc;
4396
06a2c219
GM
4397 x_get_glyph_string_clip_rect (s, &r);
4398 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
b7f83f9e 4399
06a2c219
GM
4400 if (s->face->stipple)
4401 {
4402 /* Fill background with a stipple pattern. */
4403 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4404 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4405 XSetFillStyle (s->display, gc, FillSolid);
4406 }
4407 else
4408 {
4409 XGCValues xgcv;
4410 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4411 XSetForeground (s->display, gc, xgcv.background);
4412 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4413 XSetForeground (s->display, gc, xgcv.foreground);
4414 }
4415 }
4416 }
61e9f9f3 4417 else if (!s->background_filled_p)
06a2c219
GM
4418 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4419 s->height);
4420
4421 s->background_filled_p = 1;
4422}
4423
4424
4425/* Draw glyph string S. */
4426
4427static void
4428x_draw_glyph_string (s)
4429 struct glyph_string *s;
4430{
4458cf11
KH
4431 int relief_drawn_p = 0;
4432
06a2c219
GM
4433 /* If S draws into the background of its successor, draw the
4434 background of the successor first so that S can draw into it.
4435 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4436 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4437 {
4438 xassert (s->next->img == NULL);
4439 x_set_glyph_string_gc (s->next);
4440 x_set_glyph_string_clipping (s->next);
4441 x_draw_glyph_string_background (s->next, 1);
4442 }
97210f4e 4443
06a2c219
GM
4444 /* Set up S->gc, set clipping and draw S. */
4445 x_set_glyph_string_gc (s);
06a2c219 4446
4458cf11
KH
4447 /* Draw relief (if any) in advance for char/composition so that the
4448 glyph string can be drawn over it. */
4449 if (!s->for_overlaps_p
4450 && s->face->box != FACE_NO_BOX
4451 && (s->first_glyph->type == CHAR_GLYPH
4452 || s->first_glyph->type == COMPOSITE_GLYPH))
4453
4454 {
e6269cbb 4455 x_set_glyph_string_clipping (s);
4458cf11
KH
4456 x_draw_glyph_string_background (s, 1);
4457 x_draw_glyph_string_box (s);
e6269cbb 4458 x_set_glyph_string_clipping (s);
4458cf11
KH
4459 relief_drawn_p = 1;
4460 }
e6269cbb
GM
4461 else
4462 x_set_glyph_string_clipping (s);
4458cf11 4463
06a2c219
GM
4464 switch (s->first_glyph->type)
4465 {
4466 case IMAGE_GLYPH:
4467 x_draw_image_glyph_string (s);
4468 break;
4469
4470 case STRETCH_GLYPH:
4471 x_draw_stretch_glyph_string (s);
4472 break;
4473
4474 case CHAR_GLYPH:
66ac4b0e
GM
4475 if (s->for_overlaps_p)
4476 s->background_filled_p = 1;
4477 else
4478 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4479 x_draw_glyph_string_foreground (s);
4480 break;
4481
b4192550
KH
4482 case COMPOSITE_GLYPH:
4483 if (s->for_overlaps_p || s->gidx > 0)
4484 s->background_filled_p = 1;
4485 else
4486 x_draw_glyph_string_background (s, 1);
4487 x_draw_composite_glyph_string_foreground (s);
4488 break;
4489
06a2c219
GM
4490 default:
4491 abort ();
4492 }
4493
66ac4b0e 4494 if (!s->for_overlaps_p)
06a2c219 4495 {
66ac4b0e
GM
4496 /* Draw underline. */
4497 if (s->face->underline_p)
4498 {
e24e84cc
GM
4499 unsigned long tem, h;
4500 int y;
06a2c219 4501
e24e84cc 4502 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4503 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4504 h = 1;
e24e84cc
GM
4505
4506 /* Get the underline position. This is the recommended
4507 vertical offset in pixels from the baseline to the top of
4508 the underline. This is a signed value according to the
4509 specs, and its default is
4510
4511 ROUND ((maximum descent) / 2), with
4512 ROUND(x) = floor (x + 0.5) */
4513
a72d5ce5
GM
4514 if (x_use_underline_position_properties
4515 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
4516 y = s->ybase + (long) tem;
4517 else if (s->face->font)
4518 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4519 else
a02f1be0 4520 y = s->y + s->height - h;
06a2c219 4521
66ac4b0e 4522 if (s->face->underline_defaulted_p)
e24e84cc
GM
4523 XFillRectangle (s->display, s->window, s->gc,
4524 s->x, y, s->width, h);
66ac4b0e
GM
4525 else
4526 {
4527 XGCValues xgcv;
4528 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4529 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4530 XFillRectangle (s->display, s->window, s->gc,
4531 s->x, y, s->width, h);
66ac4b0e
GM
4532 XSetForeground (s->display, s->gc, xgcv.foreground);
4533 }
dc6f92b8 4534 }
07e34cb0 4535
66ac4b0e
GM
4536 /* Draw overline. */
4537 if (s->face->overline_p)
06a2c219 4538 {
66ac4b0e
GM
4539 unsigned long dy = 0, h = 1;
4540
4541 if (s->face->overline_color_defaulted_p)
4542 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4543 s->width, h);
4544 else
4545 {
4546 XGCValues xgcv;
4547 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4548 XSetForeground (s->display, s->gc, s->face->overline_color);
4549 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4550 s->width, h);
4551 XSetForeground (s->display, s->gc, xgcv.foreground);
4552 }
06a2c219 4553 }
06a2c219 4554
66ac4b0e
GM
4555 /* Draw strike-through. */
4556 if (s->face->strike_through_p)
06a2c219 4557 {
66ac4b0e
GM
4558 unsigned long h = 1;
4559 unsigned long dy = (s->height - h) / 2;
4560
4561 if (s->face->strike_through_color_defaulted_p)
4562 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4563 s->width, h);
4564 else
4565 {
4566 XGCValues xgcv;
4567 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4568 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4569 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4570 s->width, h);
4571 XSetForeground (s->display, s->gc, xgcv.foreground);
4572 }
06a2c219 4573 }
06a2c219 4574
4458cf11
KH
4575 /* Draw relief if not yet drawn. */
4576 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
4577 x_draw_glyph_string_box (s);
4578 }
06a2c219
GM
4579
4580 /* Reset clipping. */
4581 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4582}
07e34cb0 4583
06a2c219 4584
b4192550
KH
4585static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4586 struct face **, int));
06a2c219 4587
06a2c219 4588
209f68d9
GM
4589/* Fill glyph string S with composition components specified by S->cmp.
4590
b4192550
KH
4591 FACES is an array of faces for all components of this composition.
4592 S->gidx is the index of the first component for S.
4593 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4594 use its physical height for clipping.
06a2c219 4595
b4192550 4596 Value is the index of a component not in S. */
07e34cb0 4597
b4192550
KH
4598static int
4599x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4600 struct glyph_string *s;
b4192550 4601 struct face **faces;
66ac4b0e 4602 int overlaps_p;
07e34cb0 4603{
b4192550 4604 int i;
06a2c219 4605
b4192550 4606 xassert (s);
06a2c219 4607
b4192550 4608 s->for_overlaps_p = overlaps_p;
06a2c219 4609
b4192550
KH
4610 s->face = faces[s->gidx];
4611 s->font = s->face->font;
4612 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4613
b4192550
KH
4614 /* For all glyphs of this composition, starting at the offset
4615 S->gidx, until we reach the end of the definition or encounter a
4616 glyph that requires the different face, add it to S. */
4617 ++s->nchars;
4618 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4619 ++s->nchars;
06a2c219 4620
b4192550
KH
4621 /* All glyph strings for the same composition has the same width,
4622 i.e. the width set for the first component of the composition. */
06a2c219 4623
06a2c219
GM
4624 s->width = s->first_glyph->pixel_width;
4625
4626 /* If the specified font could not be loaded, use the frame's
4627 default font, but record the fact that we couldn't load it in
4628 the glyph string so that we can draw rectangles for the
4629 characters of the glyph string. */
4630 if (s->font == NULL)
4631 {
4632 s->font_not_found_p = 1;
4633 s->font = FRAME_FONT (s->f);
4634 }
4635
4636 /* Adjust base line for subscript/superscript text. */
4637 s->ybase += s->first_glyph->voffset;
4638
4639 xassert (s->face && s->face->gc);
4640
4641 /* This glyph string must always be drawn with 16-bit functions. */
4642 s->two_byte_p = 1;
b4192550
KH
4643
4644 return s->gidx + s->nchars;
06a2c219
GM
4645}
4646
4647
209f68d9
GM
4648/* Fill glyph string S from a sequence of character glyphs.
4649
06a2c219 4650 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4651 first glyph to consider, END is the index of the last + 1.
4652 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4653 use its physical height for clipping.
66ac4b0e
GM
4654
4655 Value is the index of the first glyph not in S. */
06a2c219
GM
4656
4657static int
66ac4b0e 4658x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4659 struct glyph_string *s;
4660 int face_id;
66ac4b0e 4661 int start, end, overlaps_p;
06a2c219
GM
4662{
4663 struct glyph *glyph, *last;
4664 int voffset;
ee569018 4665 int glyph_not_available_p;
06a2c219 4666
06a2c219
GM
4667 xassert (s->f == XFRAME (s->w->frame));
4668 xassert (s->nchars == 0);
4669 xassert (start >= 0 && end > start);
4670
66ac4b0e 4671 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4672 glyph = s->row->glyphs[s->area] + start;
4673 last = s->row->glyphs[s->area] + end;
4674 voffset = glyph->voffset;
4675
ee569018
KH
4676 glyph_not_available_p = glyph->glyph_not_available_p;
4677
06a2c219
GM
4678 while (glyph < last
4679 && glyph->type == CHAR_GLYPH
4680 && glyph->voffset == voffset
ee569018
KH
4681 /* Same face id implies same font, nowadays. */
4682 && glyph->face_id == face_id
4683 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4684 {
ee569018
KH
4685 int two_byte_p;
4686
06a2c219 4687 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4688 s->char2b + s->nchars,
4689 &two_byte_p);
4690 s->two_byte_p = two_byte_p;
06a2c219
GM
4691 ++s->nchars;
4692 xassert (s->nchars <= end - start);
4693 s->width += glyph->pixel_width;
4694 ++glyph;
4695 }
4696
4697 s->font = s->face->font;
4698 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4699
4700 /* If the specified font could not be loaded, use the frame's font,
4701 but record the fact that we couldn't load it in
4702 S->font_not_found_p so that we can draw rectangles for the
4703 characters of the glyph string. */
ee569018 4704 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4705 {
4706 s->font_not_found_p = 1;
4707 s->font = FRAME_FONT (s->f);
4708 }
4709
4710 /* Adjust base line for subscript/superscript text. */
4711 s->ybase += voffset;
66ac4b0e 4712
06a2c219
GM
4713 xassert (s->face && s->face->gc);
4714 return glyph - s->row->glyphs[s->area];
07e34cb0 4715}
dc6f92b8 4716
06a2c219
GM
4717
4718/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4719
dfcf069d 4720static void
06a2c219
GM
4721x_fill_image_glyph_string (s)
4722 struct glyph_string *s;
4723{
4724 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4725 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4726 xassert (s->img);
43d120d8 4727 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4728 s->font = s->face->font;
4729 s->width = s->first_glyph->pixel_width;
4730
4731 /* Adjust base line for subscript/superscript text. */
4732 s->ybase += s->first_glyph->voffset;
4733}
4734
4735
209f68d9 4736/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4737
209f68d9
GM
4738 ROW is the glyph row in which the glyphs are found, AREA is the
4739 area within the row. START is the index of the first glyph to
4740 consider, END is the index of the last + 1.
4741
4742 Value is the index of the first glyph not in S. */
4743
4744static int
4745x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4746 struct glyph_string *s;
209f68d9
GM
4747 struct glyph_row *row;
4748 enum glyph_row_area area;
4749 int start, end;
06a2c219 4750{
209f68d9
GM
4751 struct glyph *glyph, *last;
4752 int voffset, face_id;
4753
06a2c219 4754 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4755
4756 glyph = s->row->glyphs[s->area] + start;
4757 last = s->row->glyphs[s->area] + end;
4758 face_id = glyph->face_id;
4759 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4760 s->font = s->face->font;
209f68d9
GM
4761 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4762 s->width = glyph->pixel_width;
4763 voffset = glyph->voffset;
4764
4765 for (++glyph;
4766 (glyph < last
4767 && glyph->type == STRETCH_GLYPH
4768 && glyph->voffset == voffset
4769 && glyph->face_id == face_id);
4770 ++glyph)
4771 s->width += glyph->pixel_width;
06a2c219
GM
4772
4773 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4774 s->ybase += voffset;
4775
c296fc01
GM
4776 /* The case that face->gc == 0 is handled when drawing the glyph
4777 string by calling PREPARE_FACE_FOR_DISPLAY. */
4778 xassert (s->face);
209f68d9 4779 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4780}
4781
4782
4783/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4784 of XChar2b structures for S; it can't be allocated in
4785 x_init_glyph_string because it must be allocated via `alloca'. W
4786 is the window on which S is drawn. ROW and AREA are the glyph row
4787 and area within the row from which S is constructed. START is the
4788 index of the first glyph structure covered by S. HL is a
4789 face-override for drawing S. */
4790
4791static void
4792x_init_glyph_string (s, char2b, w, row, area, start, hl)
4793 struct glyph_string *s;
4794 XChar2b *char2b;
4795 struct window *w;
4796 struct glyph_row *row;
4797 enum glyph_row_area area;
4798 int start;
4799 enum draw_glyphs_face hl;
4800{
4801 bzero (s, sizeof *s);
4802 s->w = w;
4803 s->f = XFRAME (w->frame);
4804 s->display = FRAME_X_DISPLAY (s->f);
4805 s->window = FRAME_X_WINDOW (s->f);
4806 s->char2b = char2b;
4807 s->hl = hl;
4808 s->row = row;
4809 s->area = area;
4810 s->first_glyph = row->glyphs[area] + start;
4811 s->height = row->height;
4812 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4813
9ea173e8
GM
4814 /* Display the internal border below the tool-bar window. */
4815 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4816 s->y -= s->f->output_data.x->internal_border_width;
4817
4818 s->ybase = s->y + row->ascent;
4819}
4820
4821
4822/* Set background width of glyph string S. START is the index of the
4823 first glyph following S. LAST_X is the right-most x-position + 1
4824 in the drawing area. */
4825
4826static INLINE void
4827x_set_glyph_string_background_width (s, start, last_x)
4828 struct glyph_string *s;
4829 int start;
4830 int last_x;
4831{
4832 /* If the face of this glyph string has to be drawn to the end of
4833 the drawing area, set S->extends_to_end_of_line_p. */
4834 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4835
4836 if (start == s->row->used[s->area]
eb79f5cc 4837 && s->area == TEXT_AREA
7b0870b2
GM
4838 && ((s->hl == DRAW_NORMAL_TEXT
4839 && (s->row->fill_line_p
4840 || s->face->background != default_face->background
4841 || s->face->stipple != default_face->stipple
4842 || s->row->mouse_face_p))
327f42ee
GM
4843 || s->hl == DRAW_MOUSE_FACE
4844 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4845 && s->row->fill_line_p)))
7b0870b2 4846 s->extends_to_end_of_line_p = 1;
06a2c219
GM
4847
4848 /* If S extends its face to the end of the line, set its
4849 background_width to the distance to the right edge of the drawing
4850 area. */
4851 if (s->extends_to_end_of_line_p)
1da3fd71 4852 s->background_width = last_x - s->x + 1;
06a2c219
GM
4853 else
4854 s->background_width = s->width;
4855}
4856
4857
4858/* Add a glyph string for a stretch glyph to the list of strings
4859 between HEAD and TAIL. START is the index of the stretch glyph in
4860 row area AREA of glyph row ROW. END is the index of the last glyph
4861 in that glyph row area. X is the current output position assigned
4862 to the new glyph string constructed. HL overrides that face of the
4863 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4864 is the right-most x-position of the drawing area. */
4865
8abee2e1
DL
4866/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4867 and below -- keep them on one line. */
4868#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4869 do \
4870 { \
4871 s = (struct glyph_string *) alloca (sizeof *s); \
4872 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4873 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4874 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4875 s->x = (X); \
4876 } \
4877 while (0)
4878
4879
4880/* Add a glyph string for an image glyph to the list of strings
4881 between HEAD and TAIL. START is the index of the image glyph in
4882 row area AREA of glyph row ROW. END is the index of the last glyph
4883 in that glyph row area. X is the current output position assigned
4884 to the new glyph string constructed. HL overrides that face of the
4885 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4886 is the right-most x-position of the drawing area. */
4887
8abee2e1 4888#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4889 do \
4890 { \
4891 s = (struct glyph_string *) alloca (sizeof *s); \
4892 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4893 x_fill_image_glyph_string (s); \
4894 x_append_glyph_string (&HEAD, &TAIL, s); \
4895 ++START; \
4896 s->x = (X); \
4897 } \
4898 while (0)
4899
4900
4901/* Add a glyph string for a sequence of character glyphs to the list
4902 of strings between HEAD and TAIL. START is the index of the first
4903 glyph in row area AREA of glyph row ROW that is part of the new
4904 glyph string. END is the index of the last glyph in that glyph row
4905 area. X is the current output position assigned to the new glyph
4906 string constructed. HL overrides that face of the glyph; e.g. it
4907 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4908 right-most x-position of the drawing area. */
4909
8abee2e1 4910#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4911 do \
4912 { \
3e71d8f2 4913 int c, face_id; \
06a2c219
GM
4914 XChar2b *char2b; \
4915 \
43d120d8 4916 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4917 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4918 \
b4192550
KH
4919 s = (struct glyph_string *) alloca (sizeof *s); \
4920 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4921 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4922 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4923 s->x = (X); \
4924 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4925 OVERLAPS_P); \
06a2c219
GM
4926 } \
4927 while (0)
4928
4929
b4192550
KH
4930/* Add a glyph string for a composite sequence to the list of strings
4931 between HEAD and TAIL. START is the index of the first glyph in
4932 row area AREA of glyph row ROW that is part of the new glyph
4933 string. END is the index of the last glyph in that glyph row area.
4934 X is the current output position assigned to the new glyph string
4935 constructed. HL overrides that face of the glyph; e.g. it is
4936 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4937 x-position of the drawing area. */
4938
6c27ec25 4939#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4940 do { \
43d120d8
KH
4941 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4942 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4943 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4944 struct composition *cmp = composition_table[cmp_id]; \
4945 int glyph_len = cmp->glyph_len; \
4946 XChar2b *char2b; \
4947 struct face **faces; \
4948 struct glyph_string *first_s = NULL; \
4949 int n; \
4950 \
ee569018 4951 base_face = base_face->ascii_face; \
b4192550
KH
4952 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4953 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4954 /* At first, fill in `char2b' and `faces'. */ \
4955 for (n = 0; n < glyph_len; n++) \
4956 { \
43d120d8 4957 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4958 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4959 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4960 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4961 this_face_id, char2b + n, 1); \
b4192550
KH
4962 } \
4963 \
4964 /* Make glyph_strings for each glyph sequence that is drawable by \
4965 the same face, and append them to HEAD/TAIL. */ \
4966 for (n = 0; n < cmp->glyph_len;) \
4967 { \
4968 s = (struct glyph_string *) alloca (sizeof *s); \
4969 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4970 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4971 s->cmp = cmp; \
4972 s->gidx = n; \
b4192550
KH
4973 s->x = (X); \
4974 \
4975 if (n == 0) \
4976 first_s = s; \
4977 \
4978 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4979 } \
4980 \
4981 ++START; \
4982 s = first_s; \
4983 } while (0)
4984
4985
06a2c219
GM
4986/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4987 of AREA of glyph row ROW on window W between indices START and END.
4988 HL overrides the face for drawing glyph strings, e.g. it is
4989 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4990 x-positions of the drawing area.
4991
4992 This is an ugly monster macro construct because we must use alloca
4993 to allocate glyph strings (because x_draw_glyphs can be called
4994 asynchronously). */
4995
8abee2e1 4996#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4997 do \
4998 { \
4999 HEAD = TAIL = NULL; \
5000 while (START < END) \
5001 { \
5002 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5003 switch (first_glyph->type) \
5004 { \
5005 case CHAR_GLYPH: \
5006 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
5007 TAIL, HL, X, LAST_X, \
5008 OVERLAPS_P); \
06a2c219
GM
5009 break; \
5010 \
b4192550
KH
5011 case COMPOSITE_GLYPH: \
5012 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5013 HEAD, TAIL, HL, X, LAST_X,\
5014 OVERLAPS_P); \
5015 break; \
5016 \
06a2c219
GM
5017 case STRETCH_GLYPH: \
5018 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5019 HEAD, TAIL, HL, X, LAST_X); \
5020 break; \
5021 \
5022 case IMAGE_GLYPH: \
5023 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5024 TAIL, HL, X, LAST_X); \
5025 break; \
5026 \
5027 default: \
5028 abort (); \
5029 } \
5030 \
5031 x_set_glyph_string_background_width (s, START, LAST_X); \
5032 (X) += s->width; \
5033 } \
5034 } \
5035 while (0)
5036
5037
5038/* Draw glyphs between START and END in AREA of ROW on window W,
5039 starting at x-position X. X is relative to AREA in W. HL is a
5040 face-override with the following meaning:
5041
5042 DRAW_NORMAL_TEXT draw normally
5043 DRAW_CURSOR draw in cursor face
5044 DRAW_MOUSE_FACE draw in mouse face.
5045 DRAW_INVERSE_VIDEO draw in mode line face
5046 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5047 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5048
66ac4b0e
GM
5049 If OVERLAPS_P is non-zero, draw only the foreground of characters
5050 and clip to the physical height of ROW.
5051
06a2c219
GM
5052 Value is the x-position reached, relative to AREA of W. */
5053
5054static int
f0a48a01 5055x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
06a2c219
GM
5056 struct window *w;
5057 int x;
5058 struct glyph_row *row;
5059 enum glyph_row_area area;
5060 int start, end;
5061 enum draw_glyphs_face hl;
66ac4b0e 5062 int overlaps_p;
dc6f92b8 5063{
06a2c219
GM
5064 struct glyph_string *head, *tail;
5065 struct glyph_string *s;
5066 int last_x, area_width;
5067 int x_reached;
5068 int i, j;
5069
5070 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 5071 end = min (end, row->used[area]);
a8710abf
GM
5072 start = max (0, start);
5073 start = min (end, start);
06a2c219
GM
5074
5075 /* Translate X to frame coordinates. Set last_x to the right
5076 end of the drawing area. */
5077 if (row->full_width_p)
5078 {
5079 /* X is relative to the left edge of W, without scroll bars
3f332ef3 5080 or fringes. */
06a2c219 5081 struct frame *f = XFRAME (w->frame);
06a2c219 5082 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 5083
06a2c219
GM
5084 x += window_left_x;
5085 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5086 last_x = window_left_x + area_width;
5087
5088 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5089 {
110859fc 5090 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
5091 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5092 last_x += width;
5093 else
5094 x -= width;
5095 }
dc6f92b8 5096
b9432a85 5097 x += FRAME_INTERNAL_BORDER_WIDTH (f);
98fedd97 5098 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
5099 }
5100 else
dc6f92b8 5101 {
06a2c219
GM
5102 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5103 area_width = window_box_width (w, area);
5104 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
5105 }
5106
06a2c219
GM
5107 /* Build a doubly-linked list of glyph_string structures between
5108 head and tail from what we have to draw. Note that the macro
5109 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5110 the reason we use a separate variable `i'. */
5111 i = start;
66ac4b0e
GM
5112 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5113 overlaps_p);
06a2c219
GM
5114 if (tail)
5115 x_reached = tail->x + tail->background_width;
5116 else
5117 x_reached = x;
90e65f07 5118
06a2c219
GM
5119 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5120 the row, redraw some glyphs in front or following the glyph
5121 strings built above. */
a8710abf 5122 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
5123 {
5124 int dummy_x = 0;
5125 struct glyph_string *h, *t;
5126
5127 /* Compute overhangs for all glyph strings. */
5128 for (s = head; s; s = s->next)
5129 x_compute_glyph_string_overhangs (s);
5130
5131 /* Prepend glyph strings for glyphs in front of the first glyph
5132 string that are overwritten because of the first glyph
5133 string's left overhang. The background of all strings
5134 prepended must be drawn because the first glyph string
5135 draws over it. */
5136 i = x_left_overwritten (head);
5137 if (i >= 0)
5138 {
5139 j = i;
5140 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
5141 DRAW_NORMAL_TEXT, dummy_x, last_x,
5142 overlaps_p);
06a2c219 5143 start = i;
06a2c219
GM
5144 x_compute_overhangs_and_x (t, head->x, 1);
5145 x_prepend_glyph_string_lists (&head, &tail, h, t);
5146 }
58769bee 5147
06a2c219
GM
5148 /* Prepend glyph strings for glyphs in front of the first glyph
5149 string that overwrite that glyph string because of their
5150 right overhang. For these strings, only the foreground must
5151 be drawn, because it draws over the glyph string at `head'.
5152 The background must not be drawn because this would overwrite
5153 right overhangs of preceding glyphs for which no glyph
5154 strings exist. */
5155 i = x_left_overwriting (head);
5156 if (i >= 0)
5157 {
5158 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
5159 DRAW_NORMAL_TEXT, dummy_x, last_x,
5160 overlaps_p);
06a2c219
GM
5161 for (s = h; s; s = s->next)
5162 s->background_filled_p = 1;
06a2c219
GM
5163 x_compute_overhangs_and_x (t, head->x, 1);
5164 x_prepend_glyph_string_lists (&head, &tail, h, t);
5165 }
dbcb258a 5166
06a2c219
GM
5167 /* Append glyphs strings for glyphs following the last glyph
5168 string tail that are overwritten by tail. The background of
5169 these strings has to be drawn because tail's foreground draws
5170 over it. */
5171 i = x_right_overwritten (tail);
5172 if (i >= 0)
5173 {
5174 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5175 DRAW_NORMAL_TEXT, x, last_x,
5176 overlaps_p);
06a2c219
GM
5177 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5178 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219 5179 }
dc6f92b8 5180
06a2c219
GM
5181 /* Append glyph strings for glyphs following the last glyph
5182 string tail that overwrite tail. The foreground of such
5183 glyphs has to be drawn because it writes into the background
5184 of tail. The background must not be drawn because it could
5185 paint over the foreground of following glyphs. */
5186 i = x_right_overwriting (tail);
5187 if (i >= 0)
5188 {
5189 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5190 DRAW_NORMAL_TEXT, x, last_x,
5191 overlaps_p);
06a2c219
GM
5192 for (s = h; s; s = s->next)
5193 s->background_filled_p = 1;
5194 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5195 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219
GM
5196 }
5197 }
58769bee 5198
06a2c219
GM
5199 /* Draw all strings. */
5200 for (s = head; s; s = s->next)
5201 x_draw_glyph_string (s);
dc6f92b8 5202
bb313871
GM
5203 if (area == TEXT_AREA
5204 && !row->full_width_p
5205 /* When drawing overlapping rows, only the glyph strings'
5206 foreground is drawn, which doesn't erase a cursor
5207 completely. */
5208 && !overlaps_p)
f0a48a01
GM
5209 {
5210 int x0 = head ? head->x : x;
5211 int x1 = tail ? tail->x + tail->background_width : x;
5212
5213 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5214 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5215
bb313871 5216 if (XFASTINT (w->left_margin_width) != 0)
f0a48a01
GM
5217 {
5218 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5219 x0 -= left_area_width;
5220 x1 -= left_area_width;
5221 }
5222
60626bab
GM
5223 notice_overwritten_cursor (w, area, x0, x1,
5224 row->y, MATRIX_ROW_BOTTOM_Y (row));
f0a48a01
GM
5225 }
5226
06a2c219
GM
5227 /* Value is the x-position up to which drawn, relative to AREA of W.
5228 This doesn't include parts drawn because of overhangs. */
5229 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5230 if (!row->full_width_p)
5231 {
f0a48a01 5232 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
06a2c219
GM
5233 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5234 if (area > TEXT_AREA)
5235 x_reached -= window_box_width (w, TEXT_AREA);
5236 }
a8710abf 5237
06a2c219
GM
5238 return x_reached;
5239}
dc6f92b8 5240
dc6f92b8 5241
66ac4b0e
GM
5242/* Fix the display of area AREA of overlapping row ROW in window W. */
5243
5244static void
5245x_fix_overlapping_area (w, row, area)
5246 struct window *w;
5247 struct glyph_row *row;
5248 enum glyph_row_area area;
5249{
5250 int i, x;
5251
5252 BLOCK_INPUT;
5253
5254 if (area == LEFT_MARGIN_AREA)
5255 x = 0;
5256 else if (area == TEXT_AREA)
5257 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5258 else
5259 x = (window_box_width (w, LEFT_MARGIN_AREA)
5260 + window_box_width (w, TEXT_AREA));
5261
5262 for (i = 0; i < row->used[area];)
5263 {
5264 if (row->glyphs[area][i].overlaps_vertically_p)
5265 {
5266 int start = i, start_x = x;
5267
5268 do
5269 {
5270 x += row->glyphs[area][i].pixel_width;
5271 ++i;
5272 }
5273 while (i < row->used[area]
5274 && row->glyphs[area][i].overlaps_vertically_p);
5275
5276 x_draw_glyphs (w, start_x, row, area, start, i,
f0a48a01 5277 DRAW_NORMAL_TEXT, 1);
66ac4b0e
GM
5278 }
5279 else
5280 {
5281 x += row->glyphs[area][i].pixel_width;
5282 ++i;
5283 }
5284 }
5285
5286 UNBLOCK_INPUT;
5287}
5288
5289
06a2c219
GM
5290/* Output LEN glyphs starting at START at the nominal cursor position.
5291 Advance the nominal cursor over the text. The global variable
5292 updated_window contains the window being updated, updated_row is
5293 the glyph row being updated, and updated_area is the area of that
5294 row being updated. */
dc6f92b8 5295
06a2c219
GM
5296static void
5297x_write_glyphs (start, len)
5298 struct glyph *start;
5299 int len;
5300{
f0a48a01 5301 int x, hpos;
d9cdbb3d 5302
06a2c219 5303 xassert (updated_window && updated_row);
dc6f92b8 5304 BLOCK_INPUT;
06a2c219
GM
5305
5306 /* Write glyphs. */
dc6f92b8 5307
06a2c219
GM
5308 hpos = start - updated_row->glyphs[updated_area];
5309 x = x_draw_glyphs (updated_window, output_cursor.x,
5310 updated_row, updated_area,
5311 hpos, hpos + len,
f0a48a01 5312 DRAW_NORMAL_TEXT, 0);
dc6f92b8
JB
5313
5314 UNBLOCK_INPUT;
06a2c219
GM
5315
5316 /* Advance the output cursor. */
5317 output_cursor.hpos += len;
5318 output_cursor.x = x;
dc6f92b8
JB
5319}
5320
0cdd0c9f 5321
06a2c219 5322/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5323
06a2c219
GM
5324static void
5325x_insert_glyphs (start, len)
5326 struct glyph *start;
5327 register int len;
5328{
5329 struct frame *f;
5330 struct window *w;
5331 int line_height, shift_by_width, shifted_region_width;
5332 struct glyph_row *row;
5333 struct glyph *glyph;
2beb36f9 5334 int frame_x, frame_y, hpos;
58769bee 5335
06a2c219 5336 xassert (updated_window && updated_row);
0cdd0c9f 5337 BLOCK_INPUT;
06a2c219
GM
5338 w = updated_window;
5339 f = XFRAME (WINDOW_FRAME (w));
5340
5341 /* Get the height of the line we are in. */
5342 row = updated_row;
5343 line_height = row->height;
5344
5345 /* Get the width of the glyphs to insert. */
5346 shift_by_width = 0;
5347 for (glyph = start; glyph < start + len; ++glyph)
5348 shift_by_width += glyph->pixel_width;
5349
5350 /* Get the width of the region to shift right. */
5351 shifted_region_width = (window_box_width (w, updated_area)
5352 - output_cursor.x
5353 - shift_by_width);
5354
5355 /* Shift right. */
bf0ab8a2 5356 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5357 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5358 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5359 f->output_data.x->normal_gc,
5360 frame_x, frame_y,
5361 shifted_region_width, line_height,
5362 frame_x + shift_by_width, frame_y);
5363
5364 /* Write the glyphs. */
5365 hpos = start - row->glyphs[updated_area];
5366 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
f0a48a01 5367 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
5368
5369 /* Advance the output cursor. */
5370 output_cursor.hpos += len;
5371 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5372 UNBLOCK_INPUT;
5373}
0cdd0c9f 5374
0cdd0c9f 5375
06a2c219
GM
5376/* Delete N glyphs at the nominal cursor position. Not implemented
5377 for X frames. */
c83febd7
RS
5378
5379static void
06a2c219
GM
5380x_delete_glyphs (n)
5381 register int n;
c83febd7 5382{
06a2c219 5383 abort ();
c83febd7
RS
5384}
5385
0cdd0c9f 5386
c5e6e06b
GM
5387/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5388 If they are <= 0, this is probably an error. */
5389
5390void
5391x_clear_area (dpy, window, x, y, width, height, exposures)
5392 Display *dpy;
5393 Window window;
5394 int x, y;
5395 int width, height;
5396 int exposures;
5397{
5398 xassert (width > 0 && height > 0);
5399 XClearArea (dpy, window, x, y, width, height, exposures);
5400}
5401
5402
06a2c219
GM
5403/* Erase the current text line from the nominal cursor position
5404 (inclusive) to pixel column TO_X (exclusive). The idea is that
5405 everything from TO_X onward is already erased.
5406
5407 TO_X is a pixel position relative to updated_area of
5408 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5409
06a2c219
GM
5410static void
5411x_clear_end_of_line (to_x)
5412 int to_x;
5413{
5414 struct frame *f;
5415 struct window *w = updated_window;
5416 int max_x, min_y, max_y;
5417 int from_x, from_y, to_y;
5418
5419 xassert (updated_window && updated_row);
5420 f = XFRAME (w->frame);
5421
5422 if (updated_row->full_width_p)
5423 {
5424 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5425 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5426 && !w->pseudo_window_p)
5427 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5428 }
06a2c219
GM
5429 else
5430 max_x = window_box_width (w, updated_area);
5431 max_y = window_text_bottom_y (w);
dc6f92b8 5432
06a2c219
GM
5433 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5434 of window. For TO_X > 0, truncate to end of drawing area. */
5435 if (to_x == 0)
5436 return;
5437 else if (to_x < 0)
5438 to_x = max_x;
5439 else
5440 to_x = min (to_x, max_x);
dbc4e1c1 5441
06a2c219
GM
5442 to_y = min (max_y, output_cursor.y + updated_row->height);
5443
5444 /* Notice if the cursor will be cleared by this operation. */
5445 if (!updated_row->full_width_p)
60626bab
GM
5446 notice_overwritten_cursor (w, updated_area,
5447 output_cursor.x, -1,
5448 updated_row->y,
5449 MATRIX_ROW_BOTTOM_Y (updated_row));
dbc4e1c1 5450
06a2c219
GM
5451 from_x = output_cursor.x;
5452
5453 /* Translate to frame coordinates. */
5454 if (updated_row->full_width_p)
5455 {
5456 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5457 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5458 }
0cdd0c9f
RS
5459 else
5460 {
06a2c219
GM
5461 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5462 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5463 }
5464
045dee35 5465 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5466 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5467 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5468
5469 /* Prevent inadvertently clearing to end of the X window. */
5470 if (to_x > from_x && to_y > from_y)
5471 {
5472 BLOCK_INPUT;
c5e6e06b
GM
5473 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5474 from_x, from_y, to_x - from_x, to_y - from_y,
5475 False);
06a2c219 5476 UNBLOCK_INPUT;
0cdd0c9f 5477 }
0cdd0c9f 5478}
dbc4e1c1 5479
0cdd0c9f 5480
06a2c219 5481/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5482 frame. Otherwise clear the selected frame. */
06a2c219
GM
5483
5484static void
5485x_clear_frame ()
0cdd0c9f 5486{
06a2c219 5487 struct frame *f;
0cdd0c9f 5488
06a2c219
GM
5489 if (updating_frame)
5490 f = updating_frame;
0cdd0c9f 5491 else
b86bd3dd 5492 f = SELECTED_FRAME ();
58769bee 5493
06a2c219
GM
5494 /* Clearing the frame will erase any cursor, so mark them all as no
5495 longer visible. */
5496 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5497 output_cursor.hpos = output_cursor.vpos = 0;
5498 output_cursor.x = -1;
5499
5500 /* We don't set the output cursor here because there will always
5501 follow an explicit cursor_to. */
5502 BLOCK_INPUT;
5503 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5504
5505 /* We have to clear the scroll bars, too. If we have changed
5506 colors or something like that, then they should be notified. */
5507 x_scroll_bar_clear (f);
0cdd0c9f 5508
06a2c219
GM
5509 XFlush (FRAME_X_DISPLAY (f));
5510 UNBLOCK_INPUT;
dc6f92b8 5511}
06a2c219
GM
5512
5513
dc6f92b8 5514\f
dbc4e1c1
JB
5515/* Invert the middle quarter of the frame for .15 sec. */
5516
06a2c219
GM
5517/* We use the select system call to do the waiting, so we have to make
5518 sure it's available. If it isn't, we just won't do visual bells. */
5519
dbc4e1c1
JB
5520#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5521
06a2c219
GM
5522
5523/* Subtract the `struct timeval' values X and Y, storing the result in
5524 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5525
5526static int
5527timeval_subtract (result, x, y)
5528 struct timeval *result, x, y;
5529{
06a2c219
GM
5530 /* Perform the carry for the later subtraction by updating y. This
5531 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5532 if (x.tv_usec < y.tv_usec)
5533 {
5534 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5535 y.tv_usec -= 1000000 * nsec;
5536 y.tv_sec += nsec;
5537 }
06a2c219 5538
dbc4e1c1
JB
5539 if (x.tv_usec - y.tv_usec > 1000000)
5540 {
5541 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5542 y.tv_usec += 1000000 * nsec;
5543 y.tv_sec -= nsec;
5544 }
5545
06a2c219
GM
5546 /* Compute the time remaining to wait. tv_usec is certainly
5547 positive. */
dbc4e1c1
JB
5548 result->tv_sec = x.tv_sec - y.tv_sec;
5549 result->tv_usec = x.tv_usec - y.tv_usec;
5550
06a2c219
GM
5551 /* Return indication of whether the result should be considered
5552 negative. */
dbc4e1c1
JB
5553 return x.tv_sec < y.tv_sec;
5554}
dc6f92b8 5555
dfcf069d 5556void
f676886a
JB
5557XTflash (f)
5558 struct frame *f;
dc6f92b8 5559{
dbc4e1c1 5560 BLOCK_INPUT;
dc6f92b8 5561
dbc4e1c1
JB
5562 {
5563 GC gc;
dc6f92b8 5564
06a2c219
GM
5565 /* Create a GC that will use the GXxor function to flip foreground
5566 pixels into background pixels. */
dbc4e1c1
JB
5567 {
5568 XGCValues values;
dc6f92b8 5569
dbc4e1c1 5570 values.function = GXxor;
7556890b
RS
5571 values.foreground = (f->output_data.x->foreground_pixel
5572 ^ f->output_data.x->background_pixel);
58769bee 5573
334208b7 5574 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5575 GCFunction | GCForeground, &values);
5576 }
dc6f92b8 5577
dbc4e1c1 5578 {
e84e14c3
RS
5579 /* Get the height not including a menu bar widget. */
5580 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5581 /* Height of each line to flash. */
5582 int flash_height = FRAME_LINE_HEIGHT (f);
5583 /* These will be the left and right margins of the rectangles. */
5584 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5585 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5586
5587 int width;
5588
5589 /* Don't flash the area between a scroll bar and the frame
5590 edge it is next to. */
5591 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5592 {
5593 case vertical_scroll_bar_left:
5594 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5595 break;
5596
5597 case vertical_scroll_bar_right:
5598 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5599 break;
06a2c219
GM
5600
5601 default:
5602 break;
e84e14c3
RS
5603 }
5604
5605 width = flash_right - flash_left;
5606
5607 /* If window is tall, flash top and bottom line. */
5608 if (height > 3 * FRAME_LINE_HEIGHT (f))
5609 {
5610 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5611 flash_left,
5612 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5613 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5614 width, flash_height);
5615 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5616 flash_left,
5617 (height - flash_height
5618 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5619 width, flash_height);
5620 }
5621 else
5622 /* If it is short, flash it all. */
5623 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5624 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5625 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5626
06a2c219 5627 x_flush (f);
dc6f92b8 5628
dbc4e1c1 5629 {
06a2c219 5630 struct timeval wakeup;
dc6f92b8 5631
66c30ea1 5632 EMACS_GET_TIME (wakeup);
dc6f92b8 5633
dbc4e1c1
JB
5634 /* Compute time to wait until, propagating carry from usecs. */
5635 wakeup.tv_usec += 150000;
5636 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5637 wakeup.tv_usec %= 1000000;
5638
101922c3
GM
5639 /* Keep waiting until past the time wakeup or any input gets
5640 available. */
5641 while (! detect_input_pending ())
dbc4e1c1 5642 {
101922c3 5643 struct timeval current;
dbc4e1c1
JB
5644 struct timeval timeout;
5645
101922c3 5646 EMACS_GET_TIME (current);
dbc4e1c1 5647
101922c3
GM
5648 /* Break if result would be negative. */
5649 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5650 break;
5651
101922c3
GM
5652 /* How long `select' should wait. */
5653 timeout.tv_sec = 0;
5654 timeout.tv_usec = 10000;
5655
dbc4e1c1 5656 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5657 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5658 }
5659 }
58769bee 5660
e84e14c3
RS
5661 /* If window is tall, flash top and bottom line. */
5662 if (height > 3 * FRAME_LINE_HEIGHT (f))
5663 {
5664 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5665 flash_left,
5666 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5667 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5668 width, flash_height);
5669 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5670 flash_left,
5671 (height - flash_height
5672 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5673 width, flash_height);
5674 }
5675 else
5676 /* If it is short, flash it all. */
5677 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5678 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5679 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5680
334208b7 5681 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5682 x_flush (f);
dc6f92b8 5683 }
dbc4e1c1
JB
5684 }
5685
5686 UNBLOCK_INPUT;
dc6f92b8
JB
5687}
5688
06a2c219 5689#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5690
5691
dc6f92b8
JB
5692/* Make audible bell. */
5693
dfcf069d 5694void
dc6f92b8
JB
5695XTring_bell ()
5696{
b86bd3dd
GM
5697 struct frame *f = SELECTED_FRAME ();
5698
5699 if (FRAME_X_DISPLAY (f))
5700 {
dbc4e1c1 5701#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5702 if (visible_bell)
5703 XTflash (f);
5704 else
dbc4e1c1 5705#endif
b86bd3dd
GM
5706 {
5707 BLOCK_INPUT;
5708 XBell (FRAME_X_DISPLAY (f), 0);
5709 XFlush (FRAME_X_DISPLAY (f));
5710 UNBLOCK_INPUT;
5711 }
dc6f92b8
JB
5712 }
5713}
06a2c219 5714
dc6f92b8 5715\f
06a2c219
GM
5716/* Specify how many text lines, from the top of the window,
5717 should be affected by insert-lines and delete-lines operations.
5718 This, and those operations, are used only within an update
5719 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5720
dfcf069d 5721static void
06a2c219
GM
5722XTset_terminal_window (n)
5723 register int n;
dc6f92b8 5724{
06a2c219 5725 /* This function intentionally left blank. */
dc6f92b8
JB
5726}
5727
06a2c219
GM
5728
5729\f
5730/***********************************************************************
5731 Line Dance
5732 ***********************************************************************/
5733
5734/* Perform an insert-lines or delete-lines operation, inserting N
5735 lines or deleting -N lines at vertical position VPOS. */
5736
dfcf069d 5737static void
06a2c219
GM
5738x_ins_del_lines (vpos, n)
5739 int vpos, n;
dc6f92b8
JB
5740{
5741 abort ();
5742}
06a2c219
GM
5743
5744
5745/* Scroll part of the display as described by RUN. */
dc6f92b8 5746
dfcf069d 5747static void
06a2c219
GM
5748x_scroll_run (w, run)
5749 struct window *w;
5750 struct run *run;
dc6f92b8 5751{
06a2c219
GM
5752 struct frame *f = XFRAME (w->frame);
5753 int x, y, width, height, from_y, to_y, bottom_y;
5754
5755 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
5756 without mode lines. Include in this box the left and right
5757 fringe of W. */
06a2c219 5758 window_box (w, -1, &x, &y, &width, &height);
3f332ef3
KS
5759 width += FRAME_X_FRINGE_WIDTH (f);
5760 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
06a2c219
GM
5761
5762 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5763 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5764 bottom_y = y + height;
dc6f92b8 5765
06a2c219
GM
5766 if (to_y < from_y)
5767 {
5768 /* Scrolling up. Make sure we don't copy part of the mode
5769 line at the bottom. */
5770 if (from_y + run->height > bottom_y)
5771 height = bottom_y - from_y;
5772 else
5773 height = run->height;
5774 }
dc6f92b8 5775 else
06a2c219
GM
5776 {
5777 /* Scolling down. Make sure we don't copy over the mode line.
5778 at the bottom. */
5779 if (to_y + run->height > bottom_y)
5780 height = bottom_y - to_y;
5781 else
5782 height = run->height;
5783 }
7a13e894 5784
06a2c219
GM
5785 BLOCK_INPUT;
5786
5787 /* Cursor off. Will be switched on again in x_update_window_end. */
5788 updated_window = w;
5789 x_clear_cursor (w);
5790
5791 XCopyArea (FRAME_X_DISPLAY (f),
5792 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5793 f->output_data.x->normal_gc,
5794 x, from_y,
5795 width, height,
5796 x, to_y);
5797
5798 UNBLOCK_INPUT;
5799}
dc6f92b8 5800
dc6f92b8 5801
06a2c219
GM
5802\f
5803/***********************************************************************
5804 Exposure Events
5805 ***********************************************************************/
5806
5807/* Redisplay an exposed area of frame F. X and Y are the upper-left
5808 corner of the exposed rectangle. W and H are width and height of
5809 the exposed area. All are pixel values. W or H zero means redraw
5810 the entire frame. */
dc6f92b8 5811
06a2c219
GM
5812static void
5813expose_frame (f, x, y, w, h)
5814 struct frame *f;
5815 int x, y, w, h;
dc6f92b8 5816{
06a2c219 5817 XRectangle r;
82f053ab 5818 int mouse_face_overwritten_p = 0;
dc6f92b8 5819
06a2c219 5820 TRACE ((stderr, "expose_frame "));
dc6f92b8 5821
06a2c219
GM
5822 /* No need to redraw if frame will be redrawn soon. */
5823 if (FRAME_GARBAGED_P (f))
dc6f92b8 5824 {
06a2c219
GM
5825 TRACE ((stderr, " garbaged\n"));
5826 return;
5827 }
5828
5829 /* If basic faces haven't been realized yet, there is no point in
5830 trying to redraw anything. This can happen when we get an expose
5831 event while Emacs is starting, e.g. by moving another window. */
5832 if (FRAME_FACE_CACHE (f) == NULL
5833 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5834 {
5835 TRACE ((stderr, " no faces\n"));
5836 return;
58769bee 5837 }
06a2c219
GM
5838
5839 if (w == 0 || h == 0)
58769bee 5840 {
06a2c219
GM
5841 r.x = r.y = 0;
5842 r.width = CANON_X_UNIT (f) * f->width;
5843 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5844 }
5845 else
5846 {
06a2c219
GM
5847 r.x = x;
5848 r.y = y;
5849 r.width = w;
5850 r.height = h;
5851 }
5852
5853 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
82f053ab 5854 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
06a2c219 5855
9ea173e8 5856 if (WINDOWP (f->tool_bar_window))
82f053ab
GM
5857 mouse_face_overwritten_p
5858 |= expose_window (XWINDOW (f->tool_bar_window), &r);
06a2c219
GM
5859
5860#ifndef USE_X_TOOLKIT
5861 if (WINDOWP (f->menu_bar_window))
82f053ab
GM
5862 mouse_face_overwritten_p
5863 |= expose_window (XWINDOW (f->menu_bar_window), &r);
06a2c219 5864#endif /* not USE_X_TOOLKIT */
82f053ab
GM
5865
5866 /* Some window managers support a focus-follows-mouse style with
5867 delayed raising of frames. Imagine a partially obscured frame,
5868 and moving the mouse into partially obscured mouse-face on that
5869 frame. The visible part of the mouse-face will be highlighted,
5870 then the WM raises the obscured frame. With at least one WM, KDE
5871 2.1, Emacs is not getting any event for the raising of the frame
5872 (even tried with SubstructureRedirectMask), only Expose events.
5873 These expose events will draw text normally, i.e. not
5874 highlighted. Which means we must redo the highlight here.
5875 Subsume it under ``we love X''. --gerd 2001-08-15 */
5876 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5877 {
5878 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5879 if (f == dpyinfo->mouse_face_mouse_frame)
5880 {
5881 int x = dpyinfo->mouse_face_mouse_x;
5882 int y = dpyinfo->mouse_face_mouse_y;
5883 clear_mouse_face (dpyinfo);
5884 note_mouse_highlight (f, x, y);
5885 }
5886 }
dc6f92b8
JB
5887}
5888
06a2c219
GM
5889
5890/* Redraw (parts) of all windows in the window tree rooted at W that
82f053ab
GM
5891 intersect R. R contains frame pixel coordinates. Value is
5892 non-zero if the exposure overwrites mouse-face. */
06a2c219 5893
82f053ab 5894static int
06a2c219
GM
5895expose_window_tree (w, r)
5896 struct window *w;
5897 XRectangle *r;
dc6f92b8 5898{
82f053ab
GM
5899 struct frame *f = XFRAME (w->frame);
5900 int mouse_face_overwritten_p = 0;
5901
5902 while (w && !FRAME_GARBAGED_P (f))
06a2c219
GM
5903 {
5904 if (!NILP (w->hchild))
82f053ab
GM
5905 mouse_face_overwritten_p
5906 |= expose_window_tree (XWINDOW (w->hchild), r);
06a2c219 5907 else if (!NILP (w->vchild))
82f053ab
GM
5908 mouse_face_overwritten_p
5909 |= expose_window_tree (XWINDOW (w->vchild), r);
5910 else
5911 mouse_face_overwritten_p |= expose_window (w, r);
5912
a02f1be0 5913 w = NILP (w->next) ? NULL : XWINDOW (w->next);
06a2c219 5914 }
82f053ab
GM
5915
5916 return mouse_face_overwritten_p;
06a2c219 5917}
58769bee 5918
dc6f92b8 5919
06a2c219
GM
5920/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5921 which intersects rectangle R. R is in window-relative coordinates. */
5922
5923static void
5924expose_area (w, row, r, area)
5925 struct window *w;
5926 struct glyph_row *row;
5927 XRectangle *r;
5928 enum glyph_row_area area;
5929{
06a2c219
GM
5930 struct glyph *first = row->glyphs[area];
5931 struct glyph *end = row->glyphs[area] + row->used[area];
5932 struct glyph *last;
4bc6dcc7 5933 int first_x, start_x, x;
06a2c219 5934
6fb13182
GM
5935 if (area == TEXT_AREA && row->fill_line_p)
5936 /* If row extends face to end of line write the whole line. */
153f5ed7 5937 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
f0a48a01 5938 DRAW_NORMAL_TEXT, 0);
6fb13182
GM
5939 else
5940 {
4bc6dcc7
GM
5941 /* Set START_X to the window-relative start position for drawing glyphs of
5942 AREA. The first glyph of the text area can be partially visible.
5943 The first glyphs of other areas cannot. */
5944 if (area == LEFT_MARGIN_AREA)
5945 start_x = 0;
5946 else if (area == TEXT_AREA)
5947 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5948 else
5949 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5950 + window_box_width (w, TEXT_AREA));
5951 x = start_x;
5952
6fb13182
GM
5953 /* Find the first glyph that must be redrawn. */
5954 while (first < end
5955 && x + first->pixel_width < r->x)
5956 {
5957 x += first->pixel_width;
5958 ++first;
5959 }
5960
5961 /* Find the last one. */
5962 last = first;
5963 first_x = x;
5964 while (last < end
5965 && x < r->x + r->width)
5966 {
5967 x += last->pixel_width;
5968 ++last;
5969 }
5970
5971 /* Repaint. */
5972 if (last > first)
4bc6dcc7 5973 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5974 first - row->glyphs[area],
5975 last - row->glyphs[area],
f0a48a01 5976 DRAW_NORMAL_TEXT, 0);
6fb13182 5977 }
06a2c219
GM
5978}
5979
58769bee 5980
06a2c219 5981/* Redraw the parts of the glyph row ROW on window W intersecting
82f053ab
GM
5982 rectangle R. R is in window-relative coordinates. Value is
5983 non-zero if mouse-face was overwritten. */
dc6f92b8 5984
82f053ab 5985static int
06a2c219
GM
5986expose_line (w, row, r)
5987 struct window *w;
5988 struct glyph_row *row;
5989 XRectangle *r;
5990{
5991 xassert (row->enabled_p);
5992
5993 if (row->mode_line_p || w->pseudo_window_p)
5994 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
f0a48a01 5995 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
5996 else
5997 {
5998 if (row->used[LEFT_MARGIN_AREA])
5999 expose_area (w, row, r, LEFT_MARGIN_AREA);
6000 if (row->used[TEXT_AREA])
6001 expose_area (w, row, r, TEXT_AREA);
6002 if (row->used[RIGHT_MARGIN_AREA])
6003 expose_area (w, row, r, RIGHT_MARGIN_AREA);
3f332ef3 6004 x_draw_row_fringe_bitmaps (w, row);
06a2c219 6005 }
82f053ab
GM
6006
6007 return row->mouse_face_p;
06a2c219 6008}
dc6f92b8 6009
58769bee 6010
06a2c219
GM
6011/* Return non-zero if W's cursor intersects rectangle R. */
6012
6013static int
6014x_phys_cursor_in_rect_p (w, r)
6015 struct window *w;
6016 XRectangle *r;
6017{
6018 XRectangle cr, result;
6019 struct glyph *cursor_glyph;
6020
6021 cursor_glyph = get_phys_cursor_glyph (w);
6022 if (cursor_glyph)
6023 {
6024 cr.x = w->phys_cursor.x;
6025 cr.y = w->phys_cursor.y;
6026 cr.width = cursor_glyph->pixel_width;
6027 cr.height = w->phys_cursor_height;
6028 return x_intersect_rectangles (&cr, r, &result);
6029 }
6030 else
6031 return 0;
dc6f92b8 6032}
dc6f92b8 6033
06a2c219 6034
a02f1be0
GM
6035/* Redraw the part of window W intersection rectangle FR. Pixel
6036 coordinates in FR are frame-relative. Call this function with
82f053ab
GM
6037 input blocked. Value is non-zero if the exposure overwrites
6038 mouse-face. */
dc6f92b8 6039
a39202f6 6040static int
a02f1be0 6041expose_window (w, fr)
06a2c219 6042 struct window *w;
a02f1be0 6043 XRectangle *fr;
dc6f92b8 6044{
a02f1be0 6045 struct frame *f = XFRAME (w->frame);
a02f1be0 6046 XRectangle wr, r;
82f053ab 6047 int mouse_face_overwritten_p = 0;
dc6f92b8 6048
80c32bcc
GM
6049 /* If window is not yet fully initialized, do nothing. This can
6050 happen when toolkit scroll bars are used and a window is split.
6051 Reconfiguring the scroll bar will generate an expose for a newly
6052 created window. */
a39202f6 6053 if (w->current_matrix == NULL)
82f053ab 6054 return 0;
a39202f6
GM
6055
6056 /* When we're currently updating the window, display and current
6057 matrix usually don't agree. Arrange for a thorough display
6058 later. */
6059 if (w == updated_window)
6060 {
6061 SET_FRAME_GARBAGED (f);
6062 return 0;
6063 }
80c32bcc 6064
a39202f6 6065 /* Frame-relative pixel rectangle of W. */
a02f1be0
GM
6066 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6067 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6068 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6069 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6070
a39202f6
GM
6071 if (x_intersect_rectangles (fr, &wr, &r))
6072 {
6073 int yb = window_text_bottom_y (w);
6074 struct glyph_row *row;
6075 int cursor_cleared_p;
a02f1be0 6076
a39202f6
GM
6077 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6078 r.x, r.y, r.width, r.height));
dc6f92b8 6079
a39202f6
GM
6080 /* Convert to window coordinates. */
6081 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6082 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
dc6f92b8 6083
a39202f6
GM
6084 /* Turn off the cursor. */
6085 if (!w->pseudo_window_p
6086 && x_phys_cursor_in_rect_p (w, &r))
6087 {
6088 x_clear_cursor (w);
6089 cursor_cleared_p = 1;
6090 }
6091 else
6092 cursor_cleared_p = 0;
06a2c219 6093
a39202f6
GM
6094 /* Find the first row intersecting the rectangle R. */
6095 for (row = w->current_matrix->rows;
6096 row->enabled_p;
6097 ++row)
6098 {
6099 int y0 = row->y;
6100 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6101
6102 if ((y0 >= r.y && y0 < r.y + r.height)
6103 || (y1 > r.y && y1 < r.y + r.height)
6104 || (r.y >= y0 && r.y < y1)
6105 || (r.y + r.height > y0 && r.y + r.height < y1))
82f053ab
GM
6106 {
6107 if (expose_line (w, row, &r))
6108 mouse_face_overwritten_p = 1;
6109 }
6110
a39202f6
GM
6111 if (y1 >= yb)
6112 break;
6113 }
dc6f92b8 6114
a39202f6
GM
6115 /* Display the mode line if there is one. */
6116 if (WINDOW_WANTS_MODELINE_P (w)
6117 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6118 row->enabled_p)
6119 && row->y < r.y + r.height)
82f053ab
GM
6120 {
6121 if (expose_line (w, row, &r))
6122 mouse_face_overwritten_p = 1;
6123 }
a39202f6
GM
6124
6125 if (!w->pseudo_window_p)
6126 {
6127 /* Draw border between windows. */
6128 x_draw_vertical_border (w);
06a2c219 6129
a39202f6
GM
6130 /* Turn the cursor on again. */
6131 if (cursor_cleared_p)
6132 x_update_window_cursor (w, 1);
6133 }
06a2c219 6134 }
82f053ab
GM
6135
6136 return mouse_face_overwritten_p;
06a2c219 6137}
dc6f92b8 6138
dc6f92b8 6139
06a2c219
GM
6140/* Determine the intersection of two rectangles R1 and R2. Return
6141 the intersection in *RESULT. Value is non-zero if RESULT is not
6142 empty. */
6143
6144static int
6145x_intersect_rectangles (r1, r2, result)
6146 XRectangle *r1, *r2, *result;
6147{
6148 XRectangle *left, *right;
6149 XRectangle *upper, *lower;
6150 int intersection_p = 0;
6151
6152 /* Rearrange so that R1 is the left-most rectangle. */
6153 if (r1->x < r2->x)
6154 left = r1, right = r2;
6155 else
6156 left = r2, right = r1;
6157
6158 /* X0 of the intersection is right.x0, if this is inside R1,
6159 otherwise there is no intersection. */
6160 if (right->x <= left->x + left->width)
6161 {
6162 result->x = right->x;
6163
6164 /* The right end of the intersection is the minimum of the
6165 the right ends of left and right. */
6166 result->width = (min (left->x + left->width, right->x + right->width)
6167 - result->x);
6168
6169 /* Same game for Y. */
6170 if (r1->y < r2->y)
6171 upper = r1, lower = r2;
6172 else
6173 upper = r2, lower = r1;
6174
6175 /* The upper end of the intersection is lower.y0, if this is inside
6176 of upper. Otherwise, there is no intersection. */
6177 if (lower->y <= upper->y + upper->height)
dc43ef94 6178 {
06a2c219
GM
6179 result->y = lower->y;
6180
6181 /* The lower end of the intersection is the minimum of the lower
6182 ends of upper and lower. */
6183 result->height = (min (lower->y + lower->height,
6184 upper->y + upper->height)
6185 - result->y);
6186 intersection_p = 1;
dc43ef94 6187 }
dc6f92b8
JB
6188 }
6189
06a2c219 6190 return intersection_p;
dc6f92b8 6191}
06a2c219
GM
6192
6193
6194
6195
dc6f92b8 6196\f
dc6f92b8 6197static void
334208b7
RS
6198frame_highlight (f)
6199 struct frame *f;
dc6f92b8 6200{
b3e1e05c
JB
6201 /* We used to only do this if Vx_no_window_manager was non-nil, but
6202 the ICCCM (section 4.1.6) says that the window's border pixmap
6203 and border pixel are window attributes which are "private to the
6204 client", so we can always change it to whatever we want. */
6205 BLOCK_INPUT;
334208b7 6206 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6207 f->output_data.x->border_pixel);
b3e1e05c 6208 UNBLOCK_INPUT;
5d46f928 6209 x_update_cursor (f, 1);
dc6f92b8
JB
6210}
6211
6212static void
334208b7
RS
6213frame_unhighlight (f)
6214 struct frame *f;
dc6f92b8 6215{
b3e1e05c
JB
6216 /* We used to only do this if Vx_no_window_manager was non-nil, but
6217 the ICCCM (section 4.1.6) says that the window's border pixmap
6218 and border pixel are window attributes which are "private to the
6219 client", so we can always change it to whatever we want. */
6220 BLOCK_INPUT;
334208b7 6221 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6222 f->output_data.x->border_tile);
b3e1e05c 6223 UNBLOCK_INPUT;
5d46f928 6224 x_update_cursor (f, 1);
dc6f92b8 6225}
dc6f92b8 6226
f676886a
JB
6227/* The focus has changed. Update the frames as necessary to reflect
6228 the new situation. Note that we can't change the selected frame
c5acd733 6229 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6230 Each event gets marked with the frame in which it occurred, so the
c5acd733 6231 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6232
6d4238f3 6233static void
0f941935
KH
6234x_new_focus_frame (dpyinfo, frame)
6235 struct x_display_info *dpyinfo;
f676886a 6236 struct frame *frame;
dc6f92b8 6237{
0f941935 6238 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6239
0f941935 6240 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6241 {
58769bee 6242 /* Set this before calling other routines, so that they see
f676886a 6243 the correct value of x_focus_frame. */
0f941935 6244 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6245
6246 if (old_focus && old_focus->auto_lower)
f676886a 6247 x_lower_frame (old_focus);
dc6f92b8
JB
6248
6249#if 0
f676886a 6250 selected_frame = frame;
e0c1aef2
KH
6251 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6252 selected_frame);
f676886a
JB
6253 Fselect_window (selected_frame->selected_window);
6254 choose_minibuf_frame ();
c118dd06 6255#endif /* ! 0 */
dc6f92b8 6256
0f941935
KH
6257 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6258 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6259 else
6260 pending_autoraise_frame = 0;
6d4238f3 6261 }
dc6f92b8 6262
0f941935 6263 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6264}
6265
37c2c98b
RS
6266/* Handle an event saying the mouse has moved out of an Emacs frame. */
6267
6268void
0f941935
KH
6269x_mouse_leave (dpyinfo)
6270 struct x_display_info *dpyinfo;
37c2c98b 6271{
0f941935 6272 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6273}
6d4238f3 6274
f451eb13
JB
6275/* The focus has changed, or we have redirected a frame's focus to
6276 another frame (this happens when a frame uses a surrogate
06a2c219 6277 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6278
6279 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6280 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6281 the appropriate X display info. */
06a2c219 6282
6d4238f3 6283static void
0f941935
KH
6284XTframe_rehighlight (frame)
6285 struct frame *frame;
6d4238f3 6286{
0f941935
KH
6287 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6288}
6d4238f3 6289
0f941935
KH
6290static void
6291x_frame_rehighlight (dpyinfo)
6292 struct x_display_info *dpyinfo;
6293{
6294 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6295
6296 if (dpyinfo->x_focus_frame)
6d4238f3 6297 {
0f941935
KH
6298 dpyinfo->x_highlight_frame
6299 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6300 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6301 : dpyinfo->x_focus_frame);
6302 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6303 {
0f941935
KH
6304 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6305 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6306 }
dc6f92b8 6307 }
6d4238f3 6308 else
0f941935 6309 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6310
0f941935 6311 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6312 {
6313 if (old_highlight)
f676886a 6314 frame_unhighlight (old_highlight);
0f941935
KH
6315 if (dpyinfo->x_highlight_frame)
6316 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6317 }
dc6f92b8 6318}
06a2c219
GM
6319
6320
dc6f92b8 6321\f
06a2c219 6322/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6323
28430d3c
JB
6324/* Initialize mode_switch_bit and modifier_meaning. */
6325static void
334208b7
RS
6326x_find_modifier_meanings (dpyinfo)
6327 struct x_display_info *dpyinfo;
28430d3c 6328{
f689eb05 6329 int min_code, max_code;
28430d3c
JB
6330 KeySym *syms;
6331 int syms_per_code;
6332 XModifierKeymap *mods;
6333
334208b7
RS
6334 dpyinfo->meta_mod_mask = 0;
6335 dpyinfo->shift_lock_mask = 0;
6336 dpyinfo->alt_mod_mask = 0;
6337 dpyinfo->super_mod_mask = 0;
6338 dpyinfo->hyper_mod_mask = 0;
58769bee 6339
9658a521 6340#ifdef HAVE_X11R4
334208b7 6341 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6342#else
4a60f8c5
RS
6343 min_code = dpyinfo->display->min_keycode;
6344 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6345#endif
6346
334208b7 6347 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6348 min_code, max_code - min_code + 1,
6349 &syms_per_code);
334208b7 6350 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6351
58769bee 6352 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6353 Alt keysyms are on. */
28430d3c 6354 {
06a2c219 6355 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6356
6357 for (row = 3; row < 8; row++)
6358 for (col = 0; col < mods->max_keypermod; col++)
6359 {
0299d313
RS
6360 KeyCode code
6361 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6362
af92970c
KH
6363 /* Zeroes are used for filler. Skip them. */
6364 if (code == 0)
6365 continue;
6366
28430d3c
JB
6367 /* Are any of this keycode's keysyms a meta key? */
6368 {
6369 int code_col;
6370
6371 for (code_col = 0; code_col < syms_per_code; code_col++)
6372 {
f689eb05 6373 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6374
f689eb05 6375 switch (sym)
28430d3c 6376 {
f689eb05
JB
6377 case XK_Meta_L:
6378 case XK_Meta_R:
334208b7 6379 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6380 break;
f689eb05
JB
6381
6382 case XK_Alt_L:
6383 case XK_Alt_R:
334208b7 6384 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6385 break;
6386
6387 case XK_Hyper_L:
6388 case XK_Hyper_R:
334208b7 6389 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6390 break;
6391
6392 case XK_Super_L:
6393 case XK_Super_R:
334208b7 6394 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6395 break;
11edeb03
JB
6396
6397 case XK_Shift_Lock:
6398 /* Ignore this if it's not on the lock modifier. */
6399 if ((1 << row) == LockMask)
334208b7 6400 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6401 break;
28430d3c
JB
6402 }
6403 }
6404 }
6405 }
6406 }
6407
f689eb05 6408 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6409 if (! dpyinfo->meta_mod_mask)
a3c44b14 6410 {
334208b7
RS
6411 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6412 dpyinfo->alt_mod_mask = 0;
a3c44b14 6413 }
f689eb05 6414
148c4b70
RS
6415 /* If some keys are both alt and meta,
6416 make them just meta, not alt. */
334208b7 6417 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6418 {
334208b7 6419 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6420 }
58769bee 6421
28430d3c 6422 XFree ((char *) syms);
f689eb05 6423 XFreeModifiermap (mods);
28430d3c
JB
6424}
6425
dfeccd2d
JB
6426/* Convert between the modifier bits X uses and the modifier bits
6427 Emacs uses. */
06a2c219 6428
7c5283e4 6429static unsigned int
334208b7
RS
6430x_x_to_emacs_modifiers (dpyinfo, state)
6431 struct x_display_info *dpyinfo;
dc6f92b8
JB
6432 unsigned int state;
6433{
98659da6
KG
6434 EMACS_UINT mod_meta = meta_modifier;
6435 EMACS_UINT mod_alt = alt_modifier;
6436 EMACS_UINT mod_hyper = hyper_modifier;
6437 EMACS_UINT mod_super = super_modifier;
6438 Lisp_Object tem;
6439
6440 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6441 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6442 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6443 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6444 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6445 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6446 tem = Fget (Vx_super_keysym, Qmodifier_value);
6447 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6448
6449
334208b7 6450 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
6451 | ((state & ControlMask) ? ctrl_modifier : 0)
6452 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6453 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6454 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6455 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
6456}
6457
dfeccd2d 6458static unsigned int
334208b7
RS
6459x_emacs_to_x_modifiers (dpyinfo, state)
6460 struct x_display_info *dpyinfo;
dfeccd2d
JB
6461 unsigned int state;
6462{
98659da6
KG
6463 EMACS_UINT mod_meta = meta_modifier;
6464 EMACS_UINT mod_alt = alt_modifier;
6465 EMACS_UINT mod_hyper = hyper_modifier;
6466 EMACS_UINT mod_super = super_modifier;
6467
6468 Lisp_Object tem;
6469
6470 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6471 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6472 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6473 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6474 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6475 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6476 tem = Fget (Vx_super_keysym, Qmodifier_value);
6477 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6478
6479
6480 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6481 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6482 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6483 | ((state & shift_modifier) ? ShiftMask : 0)
6484 | ((state & ctrl_modifier) ? ControlMask : 0)
6485 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6486}
d047c4eb
KH
6487
6488/* Convert a keysym to its name. */
6489
6490char *
6491x_get_keysym_name (keysym)
6492 KeySym keysym;
6493{
6494 char *value;
6495
6496 BLOCK_INPUT;
6497 value = XKeysymToString (keysym);
6498 UNBLOCK_INPUT;
6499
6500 return value;
6501}
06a2c219
GM
6502
6503
e4571a43
JB
6504\f
6505/* Mouse clicks and mouse movement. Rah. */
e4571a43 6506
06a2c219
GM
6507/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6508 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6509 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6510 not force the value into range. */
69388238 6511
c8dba240 6512void
69388238 6513pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6514 FRAME_PTR f;
69388238 6515 register int pix_x, pix_y;
e4571a43
JB
6516 register int *x, *y;
6517 XRectangle *bounds;
69388238 6518 int noclip;
e4571a43 6519{
06a2c219 6520 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6521 even for negative values. */
6522 if (pix_x < 0)
7556890b 6523 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6524 if (pix_y < 0)
7556890b 6525 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6526
e4571a43
JB
6527 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6528 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6529
6530 if (bounds)
6531 {
7556890b
RS
6532 bounds->width = FONT_WIDTH (f->output_data.x->font);
6533 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6534 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6535 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6536 }
6537
69388238
RS
6538 if (!noclip)
6539 {
6540 if (pix_x < 0)
6541 pix_x = 0;
3cbd2e0b
RS
6542 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6543 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6544
6545 if (pix_y < 0)
6546 pix_y = 0;
6547 else if (pix_y > f->height)
6548 pix_y = f->height;
6549 }
e4571a43
JB
6550
6551 *x = pix_x;
6552 *y = pix_y;
6553}
6554
06a2c219
GM
6555
6556/* Given HPOS/VPOS in the current matrix of W, return corresponding
6557 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6558 can't tell the positions because W's display is not up to date,
6559 return 0. */
6560
6561int
6562glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6563 struct window *w;
6564 int hpos, vpos;
6565 int *frame_x, *frame_y;
2b5c9e71 6566{
06a2c219
GM
6567 int success_p;
6568
6569 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6570 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6571
6572 if (display_completed)
6573 {
6574 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6575 struct glyph *glyph = row->glyphs[TEXT_AREA];
6576 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6577
6578 *frame_y = row->y;
6579 *frame_x = row->x;
6580 while (glyph < end)
6581 {
6582 *frame_x += glyph->pixel_width;
6583 ++glyph;
6584 }
6585
6586 success_p = 1;
6587 }
6588 else
6589 {
6590 *frame_y = *frame_x = 0;
6591 success_p = 0;
6592 }
6593
6594 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6595 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6596 return success_p;
2b5c9e71
RS
6597}
6598
06a2c219 6599
dc6f92b8
JB
6600/* Prepare a mouse-event in *RESULT for placement in the input queue.
6601
6602 If the event is a button press, then note that we have grabbed
f451eb13 6603 the mouse. */
dc6f92b8
JB
6604
6605static Lisp_Object
f451eb13 6606construct_mouse_click (result, event, f)
dc6f92b8
JB
6607 struct input_event *result;
6608 XButtonEvent *event;
f676886a 6609 struct frame *f;
dc6f92b8 6610{
f451eb13 6611 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6612 otherwise. */
f451eb13 6613 result->kind = mouse_click;
69388238 6614 result->code = event->button - Button1;
1113d9db 6615 result->timestamp = event->time;
334208b7
RS
6616 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6617 event->state)
f689eb05 6618 | (event->type == ButtonRelease
58769bee 6619 ? up_modifier
f689eb05 6620 : down_modifier));
dc6f92b8 6621
06a2c219
GM
6622 XSETINT (result->x, event->x);
6623 XSETINT (result->y, event->y);
6624 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6625 result->arg = Qnil;
06a2c219 6626 return Qnil;
dc6f92b8 6627}
b849c413 6628
69388238 6629\f
90e65f07
JB
6630/* Function to report a mouse movement to the mainstream Emacs code.
6631 The input handler calls this.
6632
6633 We have received a mouse movement event, which is given in *event.
6634 If the mouse is over a different glyph than it was last time, tell
6635 the mainstream emacs code by setting mouse_moved. If not, ask for
6636 another motion event, so we can check again the next time it moves. */
b8009dd1 6637
06a2c219
GM
6638static XMotionEvent last_mouse_motion_event;
6639static Lisp_Object last_mouse_motion_frame;
6640
90e65f07 6641static void
12ba150f 6642note_mouse_movement (frame, event)
f676886a 6643 FRAME_PTR frame;
90e65f07 6644 XMotionEvent *event;
90e65f07 6645{
e5d77022 6646 last_mouse_movement_time = event->time;
06a2c219
GM
6647 last_mouse_motion_event = *event;
6648 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6649
503e457e
PJ
6650 if (x_autoselect_window_p)
6651 {
6652 int area;
6653 Lisp_Object window;
6654 static Lisp_Object last_window;
6655
6656 window = window_from_coordinates (frame, XINT (event->x), XINT (event->y), &area, 0);
6657
6658 /* Window will be selected only when it is not selected now and
6659 last mouse movement event was not in it. Minubuffer window
6660 will be selected iff it is active. */
6661 if (!EQ (window, last_window)
6662 && !EQ (window, selected_window)
6663 && (!MINI_WINDOW_P (XWINDOW (window))
6664 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6665 Fselect_window (window);
6666
6667 last_window=window;
6668 }
6669
27f338af
RS
6670 if (event->window != FRAME_X_WINDOW (frame))
6671 {
39d8bb4d 6672 frame->mouse_moved = 1;
27f338af 6673 last_mouse_scroll_bar = Qnil;
27f338af 6674 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6675 }
6676
90e65f07 6677 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6678 else if (event->x < last_mouse_glyph.x
6679 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6680 || event->y < last_mouse_glyph.y
6681 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6682 {
39d8bb4d 6683 frame->mouse_moved = 1;
ab648270 6684 last_mouse_scroll_bar = Qnil;
b8009dd1 6685 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6686 }
6687}
6688
06a2c219
GM
6689\f
6690/************************************************************************
6691 Mouse Face
6692 ************************************************************************/
6693
6694/* Find the glyph under window-relative coordinates X/Y in window W.
6695 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6696 strings. Return in *HPOS and *VPOS the row and column number of
6697 the glyph found. Return in *AREA the glyph area containing X.
6698 Value is a pointer to the glyph found or null if X/Y is not on
6699 text, or we can't tell because W's current matrix is not up to
6700 date. */
6701
6702static struct glyph *
f9db2310 6703x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
6704 struct window *w;
6705 int x, y;
6706 int *hpos, *vpos, *area;
f9db2310 6707 int buffer_only_p;
06a2c219
GM
6708{
6709 struct glyph *glyph, *end;
3e71d8f2 6710 struct glyph_row *row = NULL;
06a2c219
GM
6711 int x0, i, left_area_width;
6712
6713 /* Find row containing Y. Give up if some row is not enabled. */
6714 for (i = 0; i < w->current_matrix->nrows; ++i)
6715 {
6716 row = MATRIX_ROW (w->current_matrix, i);
6717 if (!row->enabled_p)
6718 return NULL;
6719 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6720 break;
6721 }
6722
6723 *vpos = i;
6724 *hpos = 0;
6725
6726 /* Give up if Y is not in the window. */
6727 if (i == w->current_matrix->nrows)
6728 return NULL;
6729
6730 /* Get the glyph area containing X. */
6731 if (w->pseudo_window_p)
6732 {
6733 *area = TEXT_AREA;
6734 x0 = 0;
6735 }
6736 else
6737 {
6738 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6739 if (x < left_area_width)
6740 {
6741 *area = LEFT_MARGIN_AREA;
6742 x0 = 0;
6743 }
6744 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6745 {
6746 *area = TEXT_AREA;
6747 x0 = row->x + left_area_width;
6748 }
6749 else
6750 {
6751 *area = RIGHT_MARGIN_AREA;
6752 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6753 }
6754 }
6755
6756 /* Find glyph containing X. */
6757 glyph = row->glyphs[*area];
6758 end = glyph + row->used[*area];
6759 while (glyph < end)
6760 {
6761 if (x < x0 + glyph->pixel_width)
6762 {
6763 if (w->pseudo_window_p)
6764 break;
f9db2310 6765 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
6766 break;
6767 }
6768
6769 x0 += glyph->pixel_width;
6770 ++glyph;
6771 }
6772
6773 if (glyph == end)
6774 return NULL;
6775
6776 *hpos = glyph - row->glyphs[*area];
6777 return glyph;
6778}
6779
6780
6781/* Convert frame-relative x/y to coordinates relative to window W.
6782 Takes pseudo-windows into account. */
6783
6784static void
6785frame_to_window_pixel_xy (w, x, y)
6786 struct window *w;
6787 int *x, *y;
6788{
6789 if (w->pseudo_window_p)
6790 {
6791 /* A pseudo-window is always full-width, and starts at the
6792 left edge of the frame, plus a frame border. */
6793 struct frame *f = XFRAME (w->frame);
6794 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6795 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6796 }
6797 else
6798 {
6799 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6800 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6801 }
6802}
6803
6804
e371a781 6805/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6806 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6807 mode line. X is relative to the start of the text display area of
3f332ef3 6808 W, so the width of fringes and scroll bars must be subtracted
06a2c219
GM
6809 to get a position relative to the start of the mode line. */
6810
6811static void
6812note_mode_line_highlight (w, x, mode_line_p)
6813 struct window *w;
6814 int x, mode_line_p;
6815{
6816 struct frame *f = XFRAME (w->frame);
6817 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6818 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6819 struct glyph_row *row;
6820
6821 if (mode_line_p)
6822 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6823 else
045dee35 6824 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6825
06a2c219
GM
6826 if (row->enabled_p)
6827 {
6828 struct glyph *glyph, *end;
6829 Lisp_Object help, map;
6830 int x0;
6831
6832 /* Find the glyph under X. */
6833 glyph = row->glyphs[TEXT_AREA];
6834 end = glyph + row->used[TEXT_AREA];
6835 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
3f332ef3 6836 + FRAME_X_LEFT_FRINGE_WIDTH (f));
e371a781 6837
06a2c219
GM
6838 while (glyph < end
6839 && x >= x0 + glyph->pixel_width)
6840 {
6841 x0 += glyph->pixel_width;
6842 ++glyph;
6843 }
6844
6845 if (glyph < end
6846 && STRINGP (glyph->object)
6847 && XSTRING (glyph->object)->intervals
6848 && glyph->charpos >= 0
6849 && glyph->charpos < XSTRING (glyph->object)->size)
6850 {
6851 /* If we're on a string with `help-echo' text property,
6852 arrange for the help to be displayed. This is done by
6853 setting the global variable help_echo to the help string. */
6854 help = Fget_text_property (make_number (glyph->charpos),
6855 Qhelp_echo, glyph->object);
b7e80413 6856 if (!NILP (help))
be010514
GM
6857 {
6858 help_echo = help;
7cea38bc 6859 XSETWINDOW (help_echo_window, w);
be010514
GM
6860 help_echo_object = glyph->object;
6861 help_echo_pos = glyph->charpos;
6862 }
06a2c219
GM
6863
6864 /* Change the mouse pointer according to what is under X/Y. */
6865 map = Fget_text_property (make_number (glyph->charpos),
6866 Qlocal_map, glyph->object);
02067692 6867 if (KEYMAPP (map))
06a2c219 6868 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6869 else
6870 {
6871 map = Fget_text_property (make_number (glyph->charpos),
6872 Qkeymap, glyph->object);
02067692 6873 if (KEYMAPP (map))
be010514
GM
6874 cursor = f->output_data.x->nontext_cursor;
6875 }
06a2c219
GM
6876 }
6877 }
6878
6879 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6880}
6881
6882
6883/* Take proper action when the mouse has moved to position X, Y on
6884 frame F as regards highlighting characters that have mouse-face
6885 properties. Also de-highlighting chars where the mouse was before.
27f338af 6886 X and Y can be negative or out of range. */
b8009dd1
RS
6887
6888static void
6889note_mouse_highlight (f, x, y)
06a2c219 6890 struct frame *f;
c32cdd9a 6891 int x, y;
b8009dd1 6892{
06a2c219
GM
6893 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6894 int portion;
b8009dd1
RS
6895 Lisp_Object window;
6896 struct window *w;
0d487c52
GM
6897 Cursor cursor = None;
6898 struct buffer *b;
b8009dd1 6899
06a2c219
GM
6900 /* When a menu is active, don't highlight because this looks odd. */
6901#ifdef USE_X_TOOLKIT
6902 if (popup_activated ())
6903 return;
6904#endif
6905
66301061 6906 if (NILP (Vmouse_highlight)
04fff9c0 6907 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6908 return;
6909
06a2c219
GM
6910 dpyinfo->mouse_face_mouse_x = x;
6911 dpyinfo->mouse_face_mouse_y = y;
6912 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6913
06a2c219 6914 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6915 return;
6916
514e4681
RS
6917 if (gc_in_progress)
6918 {
06a2c219 6919 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6920 return;
6921 }
6922
b8009dd1 6923 /* Which window is that in? */
06a2c219 6924 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6925
6926 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6927 if (! EQ (window, dpyinfo->mouse_face_window))
6928 clear_mouse_face (dpyinfo);
6929
6930 /* Not on a window -> return. */
6931 if (!WINDOWP (window))
6932 return;
6933
6934 /* Convert to window-relative pixel coordinates. */
6935 w = XWINDOW (window);
6936 frame_to_window_pixel_xy (w, &x, &y);
6937
9ea173e8 6938 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6939 buffer. */
9ea173e8 6940 if (EQ (window, f->tool_bar_window))
06a2c219 6941 {
9ea173e8 6942 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6943 return;
6944 }
6945
0d487c52 6946 /* Mouse is on the mode or header line? */
06a2c219
GM
6947 if (portion == 1 || portion == 3)
6948 {
06a2c219
GM
6949 note_mode_line_highlight (w, x, portion == 1);
6950 return;
6951 }
0d487c52
GM
6952
6953 if (portion == 2)
6954 cursor = f->output_data.x->horizontal_drag_cursor;
06a2c219 6955 else
0d487c52 6956 cursor = f->output_data.x->text_cursor;
b8009dd1 6957
0cdd0c9f
RS
6958 /* Are we in a window whose display is up to date?
6959 And verify the buffer's text has not changed. */
0d487c52 6960 b = XBUFFER (w->buffer);
06a2c219
GM
6961 if (/* Within text portion of the window. */
6962 portion == 0
0cdd0c9f 6963 && EQ (w->window_end_valid, w->buffer)
0d487c52
GM
6964 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6965 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
b8009dd1 6966 {
06a2c219
GM
6967 int hpos, vpos, pos, i, area;
6968 struct glyph *glyph;
f9db2310 6969 Lisp_Object object;
0d487c52
GM
6970 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6971 Lisp_Object *overlay_vec = NULL;
6972 int len, noverlays;
6973 struct buffer *obuf;
6974 int obegv, ozv, same_region;
b8009dd1 6975
06a2c219 6976 /* Find the glyph under X/Y. */
f9db2310 6977 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
6978
6979 /* Clear mouse face if X/Y not over text. */
6980 if (glyph == NULL
6981 || area != TEXT_AREA
6982 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6983 {
fa262c07
GM
6984 if (clear_mouse_face (dpyinfo))
6985 cursor = None;
6986 goto set_cursor;
06a2c219
GM
6987 }
6988
6989 pos = glyph->charpos;
f9db2310
GM
6990 object = glyph->object;
6991 if (!STRINGP (object) && !BUFFERP (object))
fa262c07 6992 goto set_cursor;
06a2c219 6993
0d487c52
GM
6994 /* If we get an out-of-range value, return now; avoid an error. */
6995 if (BUFFERP (object) && pos > BUF_Z (b))
fa262c07 6996 goto set_cursor;
06a2c219 6997
0d487c52
GM
6998 /* Make the window's buffer temporarily current for
6999 overlays_at and compute_char_face. */
7000 obuf = current_buffer;
7001 current_buffer = b;
7002 obegv = BEGV;
7003 ozv = ZV;
7004 BEGV = BEG;
7005 ZV = Z;
06a2c219 7006
0d487c52
GM
7007 /* Is this char mouse-active or does it have help-echo? */
7008 position = make_number (pos);
f9db2310 7009
0d487c52
GM
7010 if (BUFFERP (object))
7011 {
7012 /* Put all the overlays we want in a vector in overlay_vec.
7013 Store the length in len. If there are more than 10, make
7014 enough space for all, and try again. */
7015 len = 10;
7016 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7017 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7018 if (noverlays > len)
7019 {
7020 len = noverlays;
7021 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7022 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7023 }
f8349001 7024
0d487c52
GM
7025 /* Sort overlays into increasing priority order. */
7026 noverlays = sort_overlays (overlay_vec, noverlays, w);
7027 }
7028 else
7029 noverlays = 0;
7030
7031 same_region = (EQ (window, dpyinfo->mouse_face_window)
7032 && vpos >= dpyinfo->mouse_face_beg_row
7033 && vpos <= dpyinfo->mouse_face_end_row
7034 && (vpos > dpyinfo->mouse_face_beg_row
7035 || hpos >= dpyinfo->mouse_face_beg_col)
7036 && (vpos < dpyinfo->mouse_face_end_row
7037 || hpos < dpyinfo->mouse_face_end_col
7038 || dpyinfo->mouse_face_past_end));
7039
7040 if (same_region)
7041 cursor = None;
7042
7043 /* Check mouse-face highlighting. */
7044 if (! same_region
7045 /* If there exists an overlay with mouse-face overlapping
7046 the one we are currently highlighting, we have to
7047 check if we enter the overlapping overlay, and then
7048 highlight only that. */
7049 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7050 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7051 {
0d487c52
GM
7052 /* Find the highest priority overlay that has a mouse-face
7053 property. */
7054 overlay = Qnil;
7055 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7056 {
7057 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7058 if (!NILP (mouse_face))
7059 overlay = overlay_vec[i];
7060 }
8bd189fb
GM
7061
7062 /* If we're actually highlighting the same overlay as
7063 before, there's no need to do that again. */
7064 if (!NILP (overlay)
7065 && EQ (overlay, dpyinfo->mouse_face_overlay))
7066 goto check_help_echo;
f9db2310 7067
8bd189fb
GM
7068 dpyinfo->mouse_face_overlay = overlay;
7069
7070 /* Clear the display of the old active region, if any. */
7071 if (clear_mouse_face (dpyinfo))
7072 cursor = None;
7073
0d487c52
GM
7074 /* If no overlay applies, get a text property. */
7075 if (NILP (overlay))
7076 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219 7077
0d487c52
GM
7078 /* Handle the overlay case. */
7079 if (!NILP (overlay))
7080 {
7081 /* Find the range of text around this char that
7082 should be active. */
7083 Lisp_Object before, after;
7084 int ignore;
7085
7086 before = Foverlay_start (overlay);
7087 after = Foverlay_end (overlay);
7088 /* Record this as the current active region. */
7089 fast_find_position (w, XFASTINT (before),
7090 &dpyinfo->mouse_face_beg_col,
7091 &dpyinfo->mouse_face_beg_row,
7092 &dpyinfo->mouse_face_beg_x,
7e376260
GM
7093 &dpyinfo->mouse_face_beg_y, Qnil);
7094
0d487c52
GM
7095 dpyinfo->mouse_face_past_end
7096 = !fast_find_position (w, XFASTINT (after),
7097 &dpyinfo->mouse_face_end_col,
7098 &dpyinfo->mouse_face_end_row,
7099 &dpyinfo->mouse_face_end_x,
7e376260 7100 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7101 dpyinfo->mouse_face_window = window;
7102 dpyinfo->mouse_face_face_id
7103 = face_at_buffer_position (w, pos, 0, 0,
7104 &ignore, pos + 1, 1);
7105
7106 /* Display it as active. */
7107 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7108 cursor = None;
0d487c52
GM
7109 }
7110 /* Handle the text property case. */
7111 else if (!NILP (mouse_face) && BUFFERP (object))
7112 {
7113 /* Find the range of text around this char that
7114 should be active. */
7115 Lisp_Object before, after, beginning, end;
7116 int ignore;
7117
7118 beginning = Fmarker_position (w->start);
7119 end = make_number (BUF_Z (XBUFFER (object))
7120 - XFASTINT (w->window_end_pos));
7121 before
7122 = Fprevious_single_property_change (make_number (pos + 1),
7123 Qmouse_face,
7124 object, beginning);
7125 after
7126 = Fnext_single_property_change (position, Qmouse_face,
7127 object, end);
7128
7129 /* Record this as the current active region. */
7130 fast_find_position (w, XFASTINT (before),
7131 &dpyinfo->mouse_face_beg_col,
7132 &dpyinfo->mouse_face_beg_row,
7133 &dpyinfo->mouse_face_beg_x,
7e376260 7134 &dpyinfo->mouse_face_beg_y, Qnil);
0d487c52
GM
7135 dpyinfo->mouse_face_past_end
7136 = !fast_find_position (w, XFASTINT (after),
7137 &dpyinfo->mouse_face_end_col,
7138 &dpyinfo->mouse_face_end_row,
7139 &dpyinfo->mouse_face_end_x,
7e376260 7140 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7141 dpyinfo->mouse_face_window = window;
7142
7143 if (BUFFERP (object))
06a2c219
GM
7144 dpyinfo->mouse_face_face_id
7145 = face_at_buffer_position (w, pos, 0, 0,
7146 &ignore, pos + 1, 1);
7147
0d487c52
GM
7148 /* Display it as active. */
7149 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7150 cursor = None;
0d487c52
GM
7151 }
7152 else if (!NILP (mouse_face) && STRINGP (object))
7153 {
7154 Lisp_Object b, e;
7155 int ignore;
f9db2310 7156
0d487c52
GM
7157 b = Fprevious_single_property_change (make_number (pos + 1),
7158 Qmouse_face,
7159 object, Qnil);
7160 e = Fnext_single_property_change (position, Qmouse_face,
7161 object, Qnil);
7162 if (NILP (b))
7163 b = make_number (0);
7164 if (NILP (e))
7165 e = make_number (XSTRING (object)->size - 1);
7166 fast_find_string_pos (w, XINT (b), object,
06a2c219
GM
7167 &dpyinfo->mouse_face_beg_col,
7168 &dpyinfo->mouse_face_beg_row,
7169 &dpyinfo->mouse_face_beg_x,
0d487c52
GM
7170 &dpyinfo->mouse_face_beg_y, 0);
7171 fast_find_string_pos (w, XINT (e), object,
7172 &dpyinfo->mouse_face_end_col,
7173 &dpyinfo->mouse_face_end_row,
7174 &dpyinfo->mouse_face_end_x,
7175 &dpyinfo->mouse_face_end_y, 1);
7176 dpyinfo->mouse_face_past_end = 0;
7177 dpyinfo->mouse_face_window = window;
7178 dpyinfo->mouse_face_face_id
7179 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7180 glyph->face_id, 1);
7181 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7182 cursor = None;
0d487c52 7183 }
7e376260
GM
7184 else if (STRINGP (object) && NILP (mouse_face))
7185 {
7186 /* A string which doesn't have mouse-face, but
7187 the text ``under'' it might have. */
7188 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7189 int start = MATRIX_ROW_START_CHARPOS (r);
7190
7191 pos = string_buffer_position (w, object, start);
7192 if (pos > 0)
7193 mouse_face = get_char_property_and_overlay (make_number (pos),
7194 Qmouse_face,
7195 w->buffer,
7196 &overlay);
7197 if (!NILP (mouse_face) && !NILP (overlay))
7198 {
7199 Lisp_Object before = Foverlay_start (overlay);
7200 Lisp_Object after = Foverlay_end (overlay);
91c153e2 7201 int ignore;
7e376260
GM
7202
7203 /* Note that we might not be able to find position
7204 BEFORE in the glyph matrix if the overlay is
7205 entirely covered by a `display' property. In
7206 this case, we overshoot. So let's stop in
7207 the glyph matrix before glyphs for OBJECT. */
7208 fast_find_position (w, XFASTINT (before),
7209 &dpyinfo->mouse_face_beg_col,
7210 &dpyinfo->mouse_face_beg_row,
7211 &dpyinfo->mouse_face_beg_x,
7212 &dpyinfo->mouse_face_beg_y,
7213 object);
7214
7215 dpyinfo->mouse_face_past_end
7216 = !fast_find_position (w, XFASTINT (after),
7217 &dpyinfo->mouse_face_end_col,
7218 &dpyinfo->mouse_face_end_row,
7219 &dpyinfo->mouse_face_end_x,
7220 &dpyinfo->mouse_face_end_y,
7221 Qnil);
7222 dpyinfo->mouse_face_window = window;
7223 dpyinfo->mouse_face_face_id
7224 = face_at_buffer_position (w, pos, 0, 0,
7225 &ignore, pos + 1, 1);
7226
7227 /* Display it as active. */
7228 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7229 cursor = None;
7230 }
7231 }
0d487c52 7232 }
06a2c219 7233
8bd189fb
GM
7234 check_help_echo:
7235
0d487c52
GM
7236 /* Look for a `help-echo' property. */
7237 {
7238 Lisp_Object help, overlay;
06a2c219 7239
0d487c52
GM
7240 /* Check overlays first. */
7241 help = overlay = Qnil;
7242 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7243 {
7244 overlay = overlay_vec[i];
7245 help = Foverlay_get (overlay, Qhelp_echo);
7246 }
be010514 7247
0d487c52
GM
7248 if (!NILP (help))
7249 {
7250 help_echo = help;
7251 help_echo_window = window;
7252 help_echo_object = overlay;
7253 help_echo_pos = pos;
7254 }
7255 else
7256 {
7257 Lisp_Object object = glyph->object;
7258 int charpos = glyph->charpos;
7177d86b 7259
0d487c52
GM
7260 /* Try text properties. */
7261 if (STRINGP (object)
7262 && charpos >= 0
7263 && charpos < XSTRING (object)->size)
7264 {
7265 help = Fget_text_property (make_number (charpos),
7266 Qhelp_echo, object);
7267 if (NILP (help))
7268 {
7269 /* If the string itself doesn't specify a help-echo,
7270 see if the buffer text ``under'' it does. */
7271 struct glyph_row *r
7272 = MATRIX_ROW (w->current_matrix, vpos);
7273 int start = MATRIX_ROW_START_CHARPOS (r);
7274 int pos = string_buffer_position (w, object, start);
7275 if (pos > 0)
7276 {
7e376260 7277 help = Fget_char_property (make_number (pos),
0d487c52
GM
7278 Qhelp_echo, w->buffer);
7279 if (!NILP (help))
7280 {
7281 charpos = pos;
7282 object = w->buffer;
7283 }
7284 }
7285 }
7286 }
7287 else if (BUFFERP (object)
7288 && charpos >= BEGV
7289 && charpos < ZV)
7290 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7291 object);
06a2c219 7292
0d487c52
GM
7293 if (!NILP (help))
7294 {
7295 help_echo = help;
7296 help_echo_window = window;
7297 help_echo_object = object;
7298 help_echo_pos = charpos;
7299 }
7300 }
06a2c219 7301 }
0d487c52
GM
7302
7303 BEGV = obegv;
7304 ZV = ozv;
7305 current_buffer = obuf;
06a2c219 7306 }
0d487c52 7307
fa262c07
GM
7308 set_cursor:
7309
0d487c52
GM
7310 if (cursor != None)
7311 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219
GM
7312}
7313
7314static void
7315redo_mouse_highlight ()
7316{
7317 if (!NILP (last_mouse_motion_frame)
7318 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7319 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7320 last_mouse_motion_event.x,
7321 last_mouse_motion_event.y);
7322}
7323
7324
7325\f
7326/***********************************************************************
9ea173e8 7327 Tool-bars
06a2c219
GM
7328 ***********************************************************************/
7329
9ea173e8
GM
7330static int x_tool_bar_item P_ ((struct frame *, int, int,
7331 struct glyph **, int *, int *, int *));
06a2c219 7332
9ea173e8 7333/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
7334 or -1. */
7335
9ea173e8 7336static int last_tool_bar_item;
06a2c219
GM
7337
7338
9ea173e8
GM
7339/* Get information about the tool-bar item at position X/Y on frame F.
7340 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7341 the current matrix of the tool-bar window of F, or NULL if not
7342 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 7343 item in F->tool_bar_items. Value is
06a2c219 7344
9ea173e8 7345 -1 if X/Y is not on a tool-bar item
06a2c219
GM
7346 0 if X/Y is on the same item that was highlighted before.
7347 1 otherwise. */
7348
7349static int
9ea173e8 7350x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
7351 struct frame *f;
7352 int x, y;
7353 struct glyph **glyph;
7354 int *hpos, *vpos, *prop_idx;
7355{
7356 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7357 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7358 int area;
7359
7360 /* Find the glyph under X/Y. */
f9db2310 7361 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
7362 if (*glyph == NULL)
7363 return -1;
7364
9ea173e8 7365 /* Get the start of this tool-bar item's properties in
8daf1204 7366 f->tool_bar_items. */
9ea173e8 7367 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
7368 return -1;
7369
7370 /* Is mouse on the highlighted item? */
9ea173e8 7371 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
7372 && *vpos >= dpyinfo->mouse_face_beg_row
7373 && *vpos <= dpyinfo->mouse_face_end_row
7374 && (*vpos > dpyinfo->mouse_face_beg_row
7375 || *hpos >= dpyinfo->mouse_face_beg_col)
7376 && (*vpos < dpyinfo->mouse_face_end_row
7377 || *hpos < dpyinfo->mouse_face_end_col
7378 || dpyinfo->mouse_face_past_end))
7379 return 0;
7380
7381 return 1;
7382}
7383
7384
9ea173e8 7385/* Handle mouse button event on the tool-bar of frame F, at
6c4a22e6 7386 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
06a2c219
GM
7387 or ButtonRelase. */
7388
7389static void
9ea173e8 7390x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7391 struct frame *f;
7392 XButtonEvent *button_event;
7393{
7394 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7395 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7396 int hpos, vpos, prop_idx;
7397 struct glyph *glyph;
7398 Lisp_Object enabled_p;
7399 int x = button_event->x;
7400 int y = button_event->y;
7401
9ea173e8 7402 /* If not on the highlighted tool-bar item, return. */
06a2c219 7403 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7404 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7405 return;
7406
7407 /* If item is disabled, do nothing. */
8daf1204 7408 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7409 if (NILP (enabled_p))
7410 return;
7411
7412 if (button_event->type == ButtonPress)
7413 {
7414 /* Show item in pressed state. */
7415 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7416 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7417 last_tool_bar_item = prop_idx;
06a2c219
GM
7418 }
7419 else
7420 {
7421 Lisp_Object key, frame;
7422 struct input_event event;
7423
7424 /* Show item in released state. */
7425 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7426 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7427
8daf1204 7428 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7429
7430 XSETFRAME (frame, f);
9ea173e8 7431 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7432 event.frame_or_window = frame;
7433 event.arg = frame;
06a2c219
GM
7434 kbd_buffer_store_event (&event);
7435
9ea173e8 7436 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7437 event.frame_or_window = frame;
7438 event.arg = key;
06a2c219
GM
7439 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7440 button_event->state);
7441 kbd_buffer_store_event (&event);
9ea173e8 7442 last_tool_bar_item = -1;
06a2c219
GM
7443 }
7444}
7445
7446
9ea173e8
GM
7447/* Possibly highlight a tool-bar item on frame F when mouse moves to
7448 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7449 note_mouse_highlight. */
7450
7451static void
9ea173e8 7452note_tool_bar_highlight (f, x, y)
06a2c219
GM
7453 struct frame *f;
7454 int x, y;
7455{
9ea173e8 7456 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7457 struct window *w = XWINDOW (window);
7458 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7459 int hpos, vpos;
7460 struct glyph *glyph;
7461 struct glyph_row *row;
5c187dee 7462 int i;
06a2c219
GM
7463 Lisp_Object enabled_p;
7464 int prop_idx;
140330de 7465 enum draw_glyphs_face draw;
5c187dee 7466 int mouse_down_p, rc;
06a2c219
GM
7467
7468 /* Function note_mouse_highlight is called with negative x(y
7469 values when mouse moves outside of the frame. */
7470 if (x <= 0 || y <= 0)
7471 {
7472 clear_mouse_face (dpyinfo);
7473 return;
7474 }
7475
9ea173e8 7476 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7477 if (rc < 0)
7478 {
9ea173e8 7479 /* Not on tool-bar item. */
06a2c219
GM
7480 clear_mouse_face (dpyinfo);
7481 return;
7482 }
7483 else if (rc == 0)
06a2c219 7484 goto set_help_echo;
b8009dd1 7485
06a2c219
GM
7486 clear_mouse_face (dpyinfo);
7487
9ea173e8 7488 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7489 mouse_down_p = (dpyinfo->grabbed
7490 && f == last_mouse_frame
7491 && FRAME_LIVE_P (f));
7492 if (mouse_down_p
9ea173e8 7493 && last_tool_bar_item != prop_idx)
06a2c219
GM
7494 return;
7495
7496 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7497 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7498
9ea173e8 7499 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7500 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7501 if (!NILP (enabled_p))
7502 {
7503 /* Compute the x-position of the glyph. In front and past the
7504 image is a space. We include this is the highlighted area. */
7505 row = MATRIX_ROW (w->current_matrix, vpos);
7506 for (i = x = 0; i < hpos; ++i)
7507 x += row->glyphs[TEXT_AREA][i].pixel_width;
7508
7509 /* Record this as the current active region. */
7510 dpyinfo->mouse_face_beg_col = hpos;
7511 dpyinfo->mouse_face_beg_row = vpos;
7512 dpyinfo->mouse_face_beg_x = x;
7513 dpyinfo->mouse_face_beg_y = row->y;
7514 dpyinfo->mouse_face_past_end = 0;
7515
7516 dpyinfo->mouse_face_end_col = hpos + 1;
7517 dpyinfo->mouse_face_end_row = vpos;
7518 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7519 dpyinfo->mouse_face_end_y = row->y;
7520 dpyinfo->mouse_face_window = window;
9ea173e8 7521 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7522
7523 /* Display it as active. */
7524 show_mouse_face (dpyinfo, draw);
7525 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7526 }
06a2c219
GM
7527
7528 set_help_echo:
7529
50590060 7530 /* Set help_echo to a help string to display for this tool-bar item.
06a2c219 7531 XTread_socket does the rest. */
7cea38bc 7532 help_echo_object = help_echo_window = Qnil;
be010514 7533 help_echo_pos = -1;
8daf1204 7534 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7535 if (NILP (help_echo))
8daf1204 7536 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7537}
4d73d038 7538
06a2c219
GM
7539
7540\f
9f8531e5
GM
7541/* Find the glyph matrix position of buffer position CHARPOS in window
7542 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7543 current glyphs must be up to date. If CHARPOS is above window
7544 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7545 of last line in W. In the row containing CHARPOS, stop before glyphs
7e376260 7546 having STOP as object. */
b8009dd1 7547
9f8531e5
GM
7548#if 0 /* This is a version of fast_find_position that's more correct
7549 in the presence of hscrolling, for example. I didn't install
7550 it right away because the problem fixed is minor, it failed
7551 in 20.x as well, and I think it's too risky to install
7552 so near the release of 21.1. 2001-09-25 gerd. */
7553
7554static int
7555fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7556 struct window *w;
7557 int charpos;
7558 int *hpos, *vpos, *x, *y;
7559 Lisp_Object stop;
7560{
7561 struct glyph_row *row, *first;
7562 struct glyph *glyph, *end;
7563 int i, past_end = 0;
7564
7565 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 7566 row = row_containing_pos (w, charpos, first, NULL, 0);
9f8531e5
GM
7567 if (row == NULL)
7568 {
7569 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7570 {
7571 *x = *y = *hpos = *vpos = 0;
7572 return 0;
7573 }
7574 else
7575 {
7576 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7577 past_end = 1;
7578 }
7579 }
7580
7581 *x = row->x;
7582 *y = row->y;
7583 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7584
7585 glyph = row->glyphs[TEXT_AREA];
7586 end = glyph + row->used[TEXT_AREA];
7587
7588 /* Skip over glyphs not having an object at the start of the row.
7589 These are special glyphs like truncation marks on terminal
7590 frames. */
7591 if (row->displays_text_p)
7592 while (glyph < end
7593 && INTEGERP (glyph->object)
7594 && !EQ (stop, glyph->object)
7595 && glyph->charpos < 0)
7596 {
7597 *x += glyph->pixel_width;
7598 ++glyph;
7599 }
7600
7601 while (glyph < end
7602 && !INTEGERP (glyph->object)
7603 && !EQ (stop, glyph->object)
7604 && (!BUFFERP (glyph->object)
7605 || glyph->charpos < charpos))
7606 {
7607 *x += glyph->pixel_width;
7608 ++glyph;
7609 }
7610
7611 *hpos = glyph - row->glyphs[TEXT_AREA];
7612 return past_end;
7613}
7614
7615#else /* not 0 */
7616
b8009dd1 7617static int
7e376260 7618fast_find_position (w, pos, hpos, vpos, x, y, stop)
06a2c219 7619 struct window *w;
b8009dd1 7620 int pos;
06a2c219 7621 int *hpos, *vpos, *x, *y;
7e376260 7622 Lisp_Object stop;
b8009dd1 7623{
b8009dd1 7624 int i;
bf1c0ba1 7625 int lastcol;
06a2c219
GM
7626 int maybe_next_line_p = 0;
7627 int line_start_position;
7628 int yb = window_text_bottom_y (w);
03d1a189
GM
7629 struct glyph_row *row, *best_row;
7630 int row_vpos, best_row_vpos;
06a2c219
GM
7631 int current_x;
7632
03d1a189
GM
7633 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7634 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7635
06a2c219 7636 while (row->y < yb)
b8009dd1 7637 {
06a2c219
GM
7638 if (row->used[TEXT_AREA])
7639 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7640 else
7641 line_start_position = 0;
7642
7643 if (line_start_position > pos)
b8009dd1 7644 break;
77b68646
RS
7645 /* If the position sought is the end of the buffer,
7646 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7647 else if (line_start_position == pos
7648 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7649 {
06a2c219 7650 maybe_next_line_p = 1;
77b68646
RS
7651 break;
7652 }
06a2c219
GM
7653 else if (line_start_position > 0)
7654 {
7655 best_row = row;
7656 best_row_vpos = row_vpos;
7657 }
4b0bb6f3
GM
7658
7659 if (row->y + row->height >= yb)
7660 break;
06a2c219
GM
7661
7662 ++row;
7663 ++row_vpos;
b8009dd1 7664 }
06a2c219
GM
7665
7666 /* Find the right column within BEST_ROW. */
7667 lastcol = 0;
7668 current_x = best_row->x;
7669 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7670 {
06a2c219 7671 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7e376260 7672 int charpos = glyph->charpos;
06a2c219 7673
7e376260 7674 if (BUFFERP (glyph->object))
bf1c0ba1 7675 {
7e376260
GM
7676 if (charpos == pos)
7677 {
7678 *hpos = i;
7679 *vpos = best_row_vpos;
7680 *x = current_x;
7681 *y = best_row->y;
7682 return 1;
7683 }
7684 else if (charpos > pos)
7685 break;
bf1c0ba1 7686 }
7e376260 7687 else if (EQ (glyph->object, stop))
4d73d038 7688 break;
06a2c219 7689
7e376260
GM
7690 if (charpos > 0)
7691 lastcol = i;
06a2c219 7692 current_x += glyph->pixel_width;
bf1c0ba1 7693 }
b8009dd1 7694
77b68646
RS
7695 /* If we're looking for the end of the buffer,
7696 and we didn't find it in the line we scanned,
7697 use the start of the following line. */
06a2c219 7698 if (maybe_next_line_p)
77b68646 7699 {
06a2c219
GM
7700 ++best_row;
7701 ++best_row_vpos;
7702 lastcol = 0;
7703 current_x = best_row->x;
77b68646
RS
7704 }
7705
06a2c219
GM
7706 *vpos = best_row_vpos;
7707 *hpos = lastcol + 1;
7708 *x = current_x;
7709 *y = best_row->y;
b8009dd1
RS
7710 return 0;
7711}
7712
9f8531e5
GM
7713#endif /* not 0 */
7714
06a2c219 7715
269b7745 7716/* Find the position of the glyph for position POS in OBJECT in
7a4bce14
PJ
7717 window W's current matrix, and return in *X, *Y the pixel
7718 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
f9db2310
GM
7719
7720 RIGHT_P non-zero means return the position of the right edge of the
7721 glyph, RIGHT_P zero means return the left edge position.
7722
7723 If no glyph for POS exists in the matrix, return the position of
7724 the glyph with the next smaller position that is in the matrix, if
7725 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7726 exists in the matrix, return the position of the glyph with the
7727 next larger position in OBJECT.
7728
7729 Value is non-zero if a glyph was found. */
7730
7731static int
7732fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7733 struct window *w;
7734 int pos;
7735 Lisp_Object object;
7736 int *hpos, *vpos, *x, *y;
7737 int right_p;
7738{
7739 int yb = window_text_bottom_y (w);
7740 struct glyph_row *r;
7741 struct glyph *best_glyph = NULL;
7742 struct glyph_row *best_row = NULL;
7743 int best_x = 0;
7744
7745 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7746 r->enabled_p && r->y < yb;
7747 ++r)
7748 {
7749 struct glyph *g = r->glyphs[TEXT_AREA];
7750 struct glyph *e = g + r->used[TEXT_AREA];
7751 int gx;
7752
7753 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7754 if (EQ (g->object, object))
7755 {
7756 if (g->charpos == pos)
7757 {
7758 best_glyph = g;
7759 best_x = gx;
7760 best_row = r;
7761 goto found;
7762 }
7763 else if (best_glyph == NULL
7764 || ((abs (g->charpos - pos)
7765 < abs (best_glyph->charpos - pos))
7766 && (right_p
7767 ? g->charpos < pos
7768 : g->charpos > pos)))
7769 {
7770 best_glyph = g;
7771 best_x = gx;
7772 best_row = r;
7773 }
7774 }
7775 }
7776
7777 found:
7778
7779 if (best_glyph)
7780 {
7781 *x = best_x;
7782 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7783
7784 if (right_p)
7785 {
7786 *x += best_glyph->pixel_width;
7787 ++*hpos;
7788 }
7789
7790 *y = best_row->y;
7791 *vpos = best_row - w->current_matrix->rows;
7792 }
7793
7794 return best_glyph != NULL;
7795}
7796
7797
b8009dd1
RS
7798/* Display the active region described by mouse_face_*
7799 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7800
7801static void
06a2c219 7802show_mouse_face (dpyinfo, draw)
7a13e894 7803 struct x_display_info *dpyinfo;
06a2c219 7804 enum draw_glyphs_face draw;
b8009dd1 7805{
7a13e894 7806 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7807 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 7808
b2bbd509
GM
7809 if (/* If window is in the process of being destroyed, don't bother
7810 to do anything. */
7811 w->current_matrix != NULL
66301061
KS
7812 /* Don't update mouse highlight if hidden */
7813 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
b2bbd509
GM
7814 /* Recognize when we are called to operate on rows that don't exist
7815 anymore. This can happen when a window is split. */
7816 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
06a2c219 7817 {
b2bbd509
GM
7818 int phys_cursor_on_p = w->phys_cursor_on_p;
7819 struct glyph_row *row, *first, *last;
06a2c219 7820
b2bbd509
GM
7821 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7822 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7823
7824 for (row = first; row <= last && row->enabled_p; ++row)
06a2c219 7825 {
b2bbd509 7826 int start_hpos, end_hpos, start_x;
06a2c219 7827
b2bbd509
GM
7828 /* For all but the first row, the highlight starts at column 0. */
7829 if (row == first)
7830 {
7831 start_hpos = dpyinfo->mouse_face_beg_col;
7832 start_x = dpyinfo->mouse_face_beg_x;
7833 }
7834 else
7835 {
7836 start_hpos = 0;
7837 start_x = 0;
7838 }
06a2c219 7839
b2bbd509
GM
7840 if (row == last)
7841 end_hpos = dpyinfo->mouse_face_end_col;
7842 else
7843 end_hpos = row->used[TEXT_AREA];
b8009dd1 7844
b2bbd509
GM
7845 if (end_hpos > start_hpos)
7846 {
7847 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7848 start_hpos, end_hpos, draw, 0);
b8009dd1 7849
60626bab
GM
7850 row->mouse_face_p
7851 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
b2bbd509
GM
7852 }
7853 }
2729a2b5 7854
b2bbd509
GM
7855 /* When we've written over the cursor, arrange for it to
7856 be displayed again. */
7857 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7858 x_display_cursor (w, 1,
7859 w->phys_cursor.hpos, w->phys_cursor.vpos,
7860 w->phys_cursor.x, w->phys_cursor.y);
7861 }
fb3b7de5 7862
06a2c219
GM
7863 /* Change the mouse cursor. */
7864 if (draw == DRAW_NORMAL_TEXT)
7865 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7866 f->output_data.x->text_cursor);
7867 else if (draw == DRAW_MOUSE_FACE)
334208b7 7868 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7869 f->output_data.x->cross_cursor);
27ead1d5 7870 else
334208b7 7871 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7872 f->output_data.x->nontext_cursor);
b8009dd1
RS
7873}
7874
7875/* Clear out the mouse-highlighted active region.
fa262c07
GM
7876 Redraw it un-highlighted first. Value is non-zero if mouse
7877 face was actually drawn unhighlighted. */
b8009dd1 7878
fa262c07 7879static int
7a13e894
RS
7880clear_mouse_face (dpyinfo)
7881 struct x_display_info *dpyinfo;
b8009dd1 7882{
fa262c07 7883 int cleared = 0;
06a2c219 7884
fa262c07
GM
7885 if (!NILP (dpyinfo->mouse_face_window))
7886 {
7887 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7888 cleared = 1;
7889 }
b8009dd1 7890
7a13e894
RS
7891 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7892 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7893 dpyinfo->mouse_face_window = Qnil;
1b85dc1c 7894 dpyinfo->mouse_face_overlay = Qnil;
fa262c07 7895 return cleared;
b8009dd1 7896}
e687d06e 7897
71b8321e
GM
7898
7899/* Clear any mouse-face on window W. This function is part of the
7900 redisplay interface, and is called from try_window_id and similar
7901 functions to ensure the mouse-highlight is off. */
7902
7903static void
7904x_clear_mouse_face (w)
7905 struct window *w;
7906{
7907 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7908 Lisp_Object window;
7909
2e636f9d 7910 BLOCK_INPUT;
71b8321e
GM
7911 XSETWINDOW (window, w);
7912 if (EQ (window, dpyinfo->mouse_face_window))
7913 clear_mouse_face (dpyinfo);
2e636f9d 7914 UNBLOCK_INPUT;
71b8321e
GM
7915}
7916
7917
e687d06e
RS
7918/* Just discard the mouse face information for frame F, if any.
7919 This is used when the size of F is changed. */
7920
dfcf069d 7921void
e687d06e
RS
7922cancel_mouse_face (f)
7923 FRAME_PTR f;
7924{
7925 Lisp_Object window;
7926 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7927
7928 window = dpyinfo->mouse_face_window;
7929 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7930 {
7931 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7932 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7933 dpyinfo->mouse_face_window = Qnil;
7934 }
7935}
b52b65bd 7936
b8009dd1 7937\f
b52b65bd
GM
7938static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7939
7940
7941/* Try to determine frame pixel position and size of the glyph under
7942 frame pixel coordinates X/Y on frame F . Return the position and
7943 size in *RECT. Value is non-zero if we could compute these
7944 values. */
7945
7946static int
7947glyph_rect (f, x, y, rect)
7948 struct frame *f;
7949 int x, y;
7950 XRectangle *rect;
7951{
7952 Lisp_Object window;
7953 int part, found = 0;
7954
7955 window = window_from_coordinates (f, x, y, &part, 0);
7956 if (!NILP (window))
7957 {
7958 struct window *w = XWINDOW (window);
7959 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7960 struct glyph_row *end = r + w->current_matrix->nrows - 1;
b52b65bd
GM
7961
7962 frame_to_window_pixel_xy (w, &x, &y);
7963
7964 for (; !found && r < end && r->enabled_p; ++r)
7965 if (r->y >= y)
7966 {
7967 struct glyph *g = r->glyphs[TEXT_AREA];
7968 struct glyph *end = g + r->used[TEXT_AREA];
7969 int gx;
7970
7971 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7972 if (gx >= x)
7973 {
7974 rect->width = g->pixel_width;
7975 rect->height = r->height;
7976 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7977 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7978 found = 1;
7979 }
7980 }
7981 }
7982
7983 return found;
7984}
7985
12ba150f 7986
90e65f07 7987/* Return the current position of the mouse.
b52b65bd 7988 *FP should be a frame which indicates which display to ask about.
90e65f07 7989
b52b65bd
GM
7990 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7991 and *PART to the frame, window, and scroll bar part that the mouse
7992 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 7993 position on the scroll bar.
12ba150f 7994
b52b65bd
GM
7995 If the mouse movement started elsewhere, set *FP to the frame the
7996 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
7997 the mouse is over.
7998
b52b65bd 7999 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
8000 was at this position.
8001
a135645a
RS
8002 Don't store anything if we don't have a valid set of values to report.
8003
90e65f07 8004 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 8005 movement. */
90e65f07
JB
8006
8007static void
1cf412ec 8008XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 8009 FRAME_PTR *fp;
1cf412ec 8010 int insist;
12ba150f 8011 Lisp_Object *bar_window;
ab648270 8012 enum scroll_bar_part *part;
90e65f07 8013 Lisp_Object *x, *y;
e5d77022 8014 unsigned long *time;
90e65f07 8015{
a135645a
RS
8016 FRAME_PTR f1;
8017
90e65f07
JB
8018 BLOCK_INPUT;
8019
8bcee03e 8020 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 8021 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
8022 else
8023 {
12ba150f
JB
8024 Window root;
8025 int root_x, root_y;
90e65f07 8026
12ba150f
JB
8027 Window dummy_window;
8028 int dummy;
8029
39d8bb4d
KH
8030 Lisp_Object frame, tail;
8031
8032 /* Clear the mouse-moved flag for every frame on this display. */
8033 FOR_EACH_FRAME (tail, frame)
8034 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8035 XFRAME (frame)->mouse_moved = 0;
8036
ab648270 8037 last_mouse_scroll_bar = Qnil;
12ba150f
JB
8038
8039 /* Figure out which root window we're on. */
334208b7
RS
8040 XQueryPointer (FRAME_X_DISPLAY (*fp),
8041 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
8042
8043 /* The root window which contains the pointer. */
8044 &root,
8045
8046 /* Trash which we can't trust if the pointer is on
8047 a different screen. */
8048 &dummy_window,
8049
8050 /* The position on that root window. */
58769bee 8051 &root_x, &root_y,
12ba150f
JB
8052
8053 /* More trash we can't trust. */
8054 &dummy, &dummy,
8055
8056 /* Modifier keys and pointer buttons, about which
8057 we don't care. */
8058 (unsigned int *) &dummy);
8059
8060 /* Now we have a position on the root; find the innermost window
8061 containing the pointer. */
8062 {
8063 Window win, child;
8064 int win_x, win_y;
06a2c219 8065 int parent_x = 0, parent_y = 0;
e99db5a1 8066 int count;
12ba150f
JB
8067
8068 win = root;
69388238 8069
2d7fc7e8
RS
8070 /* XTranslateCoordinates can get errors if the window
8071 structure is changing at the same time this function
8072 is running. So at least we must not crash from them. */
8073
e99db5a1 8074 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 8075
334208b7 8076 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 8077 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 8078 {
69388238
RS
8079 /* If mouse was grabbed on a frame, give coords for that frame
8080 even if the mouse is now outside it. */
334208b7 8081 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 8082
12ba150f 8083 /* From-window, to-window. */
69388238 8084 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
8085
8086 /* From-position, to-position. */
8087 root_x, root_y, &win_x, &win_y,
8088
8089 /* Child of win. */
8090 &child);
69388238
RS
8091 f1 = last_mouse_frame;
8092 }
8093 else
8094 {
8095 while (1)
8096 {
334208b7 8097 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 8098
69388238
RS
8099 /* From-window, to-window. */
8100 root, win,
12ba150f 8101
69388238
RS
8102 /* From-position, to-position. */
8103 root_x, root_y, &win_x, &win_y,
8104
8105 /* Child of win. */
8106 &child);
8107
9af3143a 8108 if (child == None || child == win)
69388238
RS
8109 break;
8110
8111 win = child;
8112 parent_x = win_x;
8113 parent_y = win_y;
8114 }
12ba150f 8115
69388238
RS
8116 /* Now we know that:
8117 win is the innermost window containing the pointer
8118 (XTC says it has no child containing the pointer),
8119 win_x and win_y are the pointer's position in it
8120 (XTC did this the last time through), and
8121 parent_x and parent_y are the pointer's position in win's parent.
8122 (They are what win_x and win_y were when win was child.
8123 If win is the root window, it has no parent, and
8124 parent_{x,y} are invalid, but that's okay, because we'll
8125 never use them in that case.) */
8126
8127 /* Is win one of our frames? */
19126e11 8128 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
8129
8130#ifdef USE_X_TOOLKIT
8131 /* If we end up with the menu bar window, say it's not
8132 on the frame. */
8133 if (f1 != NULL
8134 && f1->output_data.x->menubar_widget
8135 && win == XtWindow (f1->output_data.x->menubar_widget))
8136 f1 = NULL;
8137#endif /* USE_X_TOOLKIT */
69388238 8138 }
58769bee 8139
2d7fc7e8
RS
8140 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8141 f1 = 0;
8142
e99db5a1 8143 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 8144
ab648270 8145 /* If not, is it one of our scroll bars? */
a135645a 8146 if (! f1)
12ba150f 8147 {
ab648270 8148 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
8149
8150 if (bar)
8151 {
a135645a 8152 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8153 win_x = parent_x;
8154 win_y = parent_y;
8155 }
8156 }
90e65f07 8157
8bcee03e 8158 if (f1 == 0 && insist > 0)
b86bd3dd 8159 f1 = SELECTED_FRAME ();
1cf412ec 8160
a135645a 8161 if (f1)
12ba150f 8162 {
06a2c219
GM
8163 /* Ok, we found a frame. Store all the values.
8164 last_mouse_glyph is a rectangle used to reduce the
8165 generation of mouse events. To not miss any motion
8166 events, we must divide the frame into rectangles of the
8167 size of the smallest character that could be displayed
8168 on it, i.e. into the same rectangles that matrices on
8169 the frame are divided into. */
8170
b52b65bd
GM
8171 int width, height, gx, gy;
8172 XRectangle rect;
8173
8174 if (glyph_rect (f1, win_x, win_y, &rect))
8175 last_mouse_glyph = rect;
8176 else
8177 {
8178 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8179 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8180 gx = win_x;
8181 gy = win_y;
06a2c219 8182
b52b65bd
GM
8183 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8184 round down even for negative values. */
8185 if (gx < 0)
8186 gx -= width - 1;
4f00e84d 8187 if (gy < 0)
b52b65bd
GM
8188 gy -= height - 1;
8189 gx = (gx + width - 1) / width * width;
8190 gy = (gy + height - 1) / height * height;
8191
8192 last_mouse_glyph.width = width;
8193 last_mouse_glyph.height = height;
8194 last_mouse_glyph.x = gx;
8195 last_mouse_glyph.y = gy;
8196 }
12ba150f
JB
8197
8198 *bar_window = Qnil;
8199 *part = 0;
334208b7 8200 *fp = f1;
e0c1aef2
KH
8201 XSETINT (*x, win_x);
8202 XSETINT (*y, win_y);
12ba150f
JB
8203 *time = last_mouse_movement_time;
8204 }
8205 }
8206 }
90e65f07
JB
8207
8208 UNBLOCK_INPUT;
8209}
f451eb13 8210
06a2c219 8211
06a2c219 8212#ifdef USE_X_TOOLKIT
bffcfca9
GM
8213
8214/* Atimer callback function for TIMER. Called every 0.1s to process
8215 Xt timeouts, if needed. We must avoid calling XtAppPending as
8216 much as possible because that function does an implicit XFlush
8217 that slows us down. */
8218
8219static void
8220x_process_timeouts (timer)
8221 struct atimer *timer;
8222{
8223 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8224 {
8225 BLOCK_INPUT;
8226 while (XtAppPending (Xt_app_con) & XtIMTimer)
8227 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8228 UNBLOCK_INPUT;
8229 }
06a2c219
GM
8230}
8231
bffcfca9 8232#endif /* USE_X_TOOLKIT */
06a2c219
GM
8233
8234\f
8235/* Scroll bar support. */
8236
8237/* Given an X window ID, find the struct scroll_bar which manages it.
8238 This can be called in GC, so we have to make sure to strip off mark
8239 bits. */
bffcfca9 8240
06a2c219
GM
8241static struct scroll_bar *
8242x_window_to_scroll_bar (window_id)
8243 Window window_id;
8244{
8245 Lisp_Object tail;
8246
8247 for (tail = Vframe_list;
8248 XGCTYPE (tail) == Lisp_Cons;
8e713be6 8249 tail = XCDR (tail))
06a2c219
GM
8250 {
8251 Lisp_Object frame, bar, condemned;
8252
8e713be6 8253 frame = XCAR (tail);
06a2c219
GM
8254 /* All elements of Vframe_list should be frames. */
8255 if (! GC_FRAMEP (frame))
8256 abort ();
8257
8258 /* Scan this frame's scroll bar list for a scroll bar with the
8259 right window ID. */
8260 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8261 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8262 /* This trick allows us to search both the ordinary and
8263 condemned scroll bar lists with one loop. */
8264 ! GC_NILP (bar) || (bar = condemned,
8265 condemned = Qnil,
8266 ! GC_NILP (bar));
8267 bar = XSCROLL_BAR (bar)->next)
8268 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8269 return XSCROLL_BAR (bar);
8270 }
8271
8272 return 0;
8273}
8274
8275
01f67d2c 8276#if defined USE_LUCID
c95fc5f1
GM
8277
8278/* Return the Lucid menu bar WINDOW is part of. Return null
8279 if WINDOW is not part of a menu bar. */
8280
8281static Widget
8282x_window_to_menu_bar (window)
8283 Window window;
8284{
8285 Lisp_Object tail;
8286
8287 for (tail = Vframe_list;
8288 XGCTYPE (tail) == Lisp_Cons;
8289 tail = XCDR (tail))
8290 {
8291 Lisp_Object frame = XCAR (tail);
8292 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8293
8294 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8295 return menu_bar;
8296 }
8297
8298 return NULL;
8299}
8300
01f67d2c 8301#endif /* USE_LUCID */
c95fc5f1 8302
06a2c219
GM
8303\f
8304/************************************************************************
8305 Toolkit scroll bars
8306 ************************************************************************/
8307
eccc05db 8308#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8309
8310static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8311static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8312static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8313 struct scroll_bar *));
8314static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8315 int, int, int));
8316
8317
8318/* Id of action hook installed for scroll bars. */
8319
8320static XtActionHookId action_hook_id;
8321
8322/* Lisp window being scrolled. Set when starting to interact with
8323 a toolkit scroll bar, reset to nil when ending the interaction. */
8324
8325static Lisp_Object window_being_scrolled;
8326
8327/* Last scroll bar part sent in xm_scroll_callback. */
8328
8329static int last_scroll_bar_part;
8330
ec18280f
SM
8331/* Whether this is an Xaw with arrow-scrollbars. This should imply
8332 that movements of 1/20 of the screen size are mapped to up/down. */
8333
8334static Boolean xaw3d_arrow_scroll;
8335
8336/* Whether the drag scrolling maintains the mouse at the top of the
8337 thumb. If not, resizing the thumb needs to be done more carefully
8338 to avoid jerkyness. */
8339
8340static Boolean xaw3d_pick_top;
8341
06a2c219
GM
8342
8343/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 8344 bars are used.. The hook is responsible for detecting when
06a2c219
GM
8345 the user ends an interaction with the scroll bar, and generates
8346 a `end-scroll' scroll_bar_click' event if so. */
8347
8348static void
8349xt_action_hook (widget, client_data, action_name, event, params,
8350 num_params)
8351 Widget widget;
8352 XtPointer client_data;
8353 String action_name;
8354 XEvent *event;
8355 String *params;
8356 Cardinal *num_params;
8357{
8358 int scroll_bar_p;
8359 char *end_action;
8360
8361#ifdef USE_MOTIF
8362 scroll_bar_p = XmIsScrollBar (widget);
8363 end_action = "Release";
ec18280f 8364#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8365 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8366 end_action = "EndScroll";
ec18280f 8367#endif /* USE_MOTIF */
06a2c219 8368
06a2c219
GM
8369 if (scroll_bar_p
8370 && strcmp (action_name, end_action) == 0
8371 && WINDOWP (window_being_scrolled))
8372 {
8373 struct window *w;
8374
8375 x_send_scroll_bar_event (window_being_scrolled,
8376 scroll_bar_end_scroll, 0, 0);
8377 w = XWINDOW (window_being_scrolled);
8378 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8379 window_being_scrolled = Qnil;
8380 last_scroll_bar_part = -1;
bffcfca9
GM
8381
8382 /* Xt timeouts no longer needed. */
8383 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
8384 }
8385}
8386
07b3d16e
GM
8387/* A vector of windows used for communication between
8388 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8389
8390static struct window **scroll_bar_windows;
8391static int scroll_bar_windows_size;
8392
06a2c219
GM
8393
8394/* Send a client message with message type Xatom_Scrollbar for a
8395 scroll action to the frame of WINDOW. PART is a value identifying
8396 the part of the scroll bar that was clicked on. PORTION is the
8397 amount to scroll of a whole of WHOLE. */
8398
8399static void
8400x_send_scroll_bar_event (window, part, portion, whole)
8401 Lisp_Object window;
8402 int part, portion, whole;
8403{
8404 XEvent event;
8405 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
8406 struct window *w = XWINDOW (window);
8407 struct frame *f = XFRAME (w->frame);
8408 int i;
06a2c219 8409
07b3d16e
GM
8410 BLOCK_INPUT;
8411
06a2c219
GM
8412 /* Construct a ClientMessage event to send to the frame. */
8413 ev->type = ClientMessage;
8414 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8415 ev->display = FRAME_X_DISPLAY (f);
8416 ev->window = FRAME_X_WINDOW (f);
8417 ev->format = 32;
07b3d16e
GM
8418
8419 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8420 not enough to store a pointer or Lisp_Object on a 64 bit system.
8421 So, store the window in scroll_bar_windows and pass the index
8422 into that array in the event. */
8423 for (i = 0; i < scroll_bar_windows_size; ++i)
8424 if (scroll_bar_windows[i] == NULL)
8425 break;
8426
8427 if (i == scroll_bar_windows_size)
8428 {
8429 int new_size = max (10, 2 * scroll_bar_windows_size);
8430 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8431 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8432
8433 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8434 nbytes);
8435 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8436 scroll_bar_windows_size = new_size;
8437 }
8438
8439 scroll_bar_windows[i] = w;
8440 ev->data.l[0] = (long) i;
06a2c219
GM
8441 ev->data.l[1] = (long) part;
8442 ev->data.l[2] = (long) 0;
8443 ev->data.l[3] = (long) portion;
8444 ev->data.l[4] = (long) whole;
8445
bffcfca9
GM
8446 /* Make Xt timeouts work while the scroll bar is active. */
8447 toolkit_scroll_bar_interaction = 1;
8448
06a2c219
GM
8449 /* Setting the event mask to zero means that the message will
8450 be sent to the client that created the window, and if that
8451 window no longer exists, no event will be sent. */
06a2c219
GM
8452 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8453 UNBLOCK_INPUT;
8454}
8455
8456
8457/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8458 in *IEVENT. */
8459
8460static void
8461x_scroll_bar_to_input_event (event, ievent)
8462 XEvent *event;
8463 struct input_event *ievent;
8464{
8465 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
8466 Lisp_Object window;
8467 struct frame *f;
07b3d16e
GM
8468 struct window *w;
8469
8470 w = scroll_bar_windows[ev->data.l[0]];
8471 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 8472
07b3d16e
GM
8473 XSETWINDOW (window, w);
8474 f = XFRAME (w->frame);
06a2c219
GM
8475
8476 ievent->kind = scroll_bar_click;
8477 ievent->frame_or_window = window;
0f8aabe9 8478 ievent->arg = Qnil;
06a2c219
GM
8479 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8480 ievent->part = ev->data.l[1];
8481 ievent->code = ev->data.l[2];
8482 ievent->x = make_number ((int) ev->data.l[3]);
8483 ievent->y = make_number ((int) ev->data.l[4]);
8484 ievent->modifiers = 0;
8485}
8486
8487
8488#ifdef USE_MOTIF
8489
8490/* Minimum and maximum values used for Motif scroll bars. */
8491
8492#define XM_SB_MIN 1
8493#define XM_SB_MAX 10000000
8494#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8495
8496
8497/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8498 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8499 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8500
8501static void
8502xm_scroll_callback (widget, client_data, call_data)
8503 Widget widget;
8504 XtPointer client_data, call_data;
8505{
8506 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8507 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
8508 int part = -1, whole = 0, portion = 0;
8509
8510 switch (cs->reason)
8511 {
8512 case XmCR_DECREMENT:
8513 bar->dragging = Qnil;
8514 part = scroll_bar_up_arrow;
8515 break;
8516
8517 case XmCR_INCREMENT:
8518 bar->dragging = Qnil;
8519 part = scroll_bar_down_arrow;
8520 break;
8521
8522 case XmCR_PAGE_DECREMENT:
8523 bar->dragging = Qnil;
8524 part = scroll_bar_above_handle;
8525 break;
8526
8527 case XmCR_PAGE_INCREMENT:
8528 bar->dragging = Qnil;
8529 part = scroll_bar_below_handle;
8530 break;
8531
8532 case XmCR_TO_TOP:
8533 bar->dragging = Qnil;
8534 part = scroll_bar_to_top;
8535 break;
8536
8537 case XmCR_TO_BOTTOM:
8538 bar->dragging = Qnil;
8539 part = scroll_bar_to_bottom;
8540 break;
8541
8542 case XmCR_DRAG:
8543 {
8544 int slider_size;
8545 int dragging_down_p = (INTEGERP (bar->dragging)
8546 && XINT (bar->dragging) <= cs->value);
8547
8548 /* Get the slider size. */
8549 BLOCK_INPUT;
8550 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8551 UNBLOCK_INPUT;
8552
8553 /* At the max position of the scroll bar, do a line-wise
23442ae4
GM
8554 movement. Without doing anything, we would be called with
8555 the same cs->value again and again. If we want to make
8556 sure that we can reach the end of the buffer, we have to do
8557 something.
06a2c219
GM
8558
8559 Implementation note: setting bar->dragging always to
8560 cs->value gives a smoother movement at the max position.
8561 Setting it to nil when doing line-wise movement gives
8562 a better slider behavior. */
8563
8564 if (cs->value + slider_size == XM_SB_MAX
8565 || (dragging_down_p
8566 && last_scroll_bar_part == scroll_bar_down_arrow))
8567 {
8568 part = scroll_bar_down_arrow;
8569 bar->dragging = Qnil;
8570 }
8571 else
8572 {
8573 whole = XM_SB_RANGE;
8574 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8575 part = scroll_bar_handle;
8576 bar->dragging = make_number (cs->value);
8577 }
8578 }
8579 break;
8580
8581 case XmCR_VALUE_CHANGED:
8582 break;
8583 };
8584
8585 if (part >= 0)
8586 {
8587 window_being_scrolled = bar->window;
8588 last_scroll_bar_part = part;
8589 x_send_scroll_bar_event (bar->window, part, portion, whole);
8590 }
8591}
8592
8593
ec18280f 8594#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
8595
8596
ec18280f 8597/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8598 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8599 scroll bar struct. CALL_DATA is a pointer to a float saying where
8600 the thumb is. */
8601
8602static void
ec18280f 8603xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8604 Widget widget;
8605 XtPointer client_data, call_data;
8606{
8607 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8608 float top = *(float *) call_data;
8609 float shown;
ec18280f
SM
8610 int whole, portion, height;
8611 int part;
06a2c219
GM
8612
8613 /* Get the size of the thumb, a value between 0 and 1. */
8614 BLOCK_INPUT;
ec18280f 8615 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8616 UNBLOCK_INPUT;
8617
8618 whole = 10000000;
8619 portion = shown < 1 ? top * whole : 0;
06a2c219 8620
ec18280f
SM
8621 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8622 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8623 the bottom, so we force the scrolling whenever we see that we're
8624 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8625 we try to ensure that we always stay two pixels away from the
8626 bottom). */
06a2c219
GM
8627 part = scroll_bar_down_arrow;
8628 else
8629 part = scroll_bar_handle;
8630
8631 window_being_scrolled = bar->window;
8632 bar->dragging = make_number (portion);
8633 last_scroll_bar_part = part;
8634 x_send_scroll_bar_event (bar->window, part, portion, whole);
8635}
8636
8637
ec18280f
SM
8638/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8639 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8640 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8641 the scroll bar. CALL_DATA is an integer specifying the action that
8642 has taken place. It's magnitude is in the range 0..height of the
8643 scroll bar. Negative values mean scroll towards buffer start.
8644 Values < height of scroll bar mean line-wise movement. */
8645
8646static void
ec18280f 8647xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8648 Widget widget;
8649 XtPointer client_data, call_data;
8650{
8651 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8652 int position = (int) call_data;
8653 Dimension height;
8654 int part;
8655
8656 /* Get the height of the scroll bar. */
8657 BLOCK_INPUT;
8658 XtVaGetValues (widget, XtNheight, &height, NULL);
8659 UNBLOCK_INPUT;
8660
ec18280f
SM
8661 if (abs (position) >= height)
8662 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8663
8664 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8665 it maps line-movement to call_data = max(5, height/20). */
8666 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8667 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8668 else
ec18280f 8669 part = scroll_bar_move_ratio;
06a2c219
GM
8670
8671 window_being_scrolled = bar->window;
8672 bar->dragging = Qnil;
8673 last_scroll_bar_part = part;
ec18280f 8674 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8675}
8676
8677
8678#endif /* not USE_MOTIF */
8679
8680
8681/* Create the widget for scroll bar BAR on frame F. Record the widget
8682 and X window of the scroll bar in BAR. */
8683
8684static void
8685x_create_toolkit_scroll_bar (f, bar)
8686 struct frame *f;
8687 struct scroll_bar *bar;
8688{
8689 Window xwindow;
8690 Widget widget;
8691 Arg av[20];
8692 int ac = 0;
8693 char *scroll_bar_name = "verticalScrollBar";
8694 unsigned long pixel;
8695
8696 BLOCK_INPUT;
8697
8698#ifdef USE_MOTIF
06a2c219
GM
8699 /* Set resources. Create the widget. */
8700 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8701 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8702 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8703 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8704 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8705 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8706 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8707
8708 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8709 if (pixel != -1)
8710 {
8711 XtSetArg (av[ac], XmNforeground, pixel);
8712 ++ac;
8713 }
8714
8715 pixel = f->output_data.x->scroll_bar_background_pixel;
8716 if (pixel != -1)
8717 {
8718 XtSetArg (av[ac], XmNbackground, pixel);
8719 ++ac;
8720 }
8721
8722 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8723 scroll_bar_name, av, ac);
8724
8725 /* Add one callback for everything that can happen. */
8726 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8727 (XtPointer) bar);
8728 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8729 (XtPointer) bar);
8730 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8731 (XtPointer) bar);
8732 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8733 (XtPointer) bar);
8734 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8735 (XtPointer) bar);
8736 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8737 (XtPointer) bar);
8738 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8739 (XtPointer) bar);
8740
8741 /* Realize the widget. Only after that is the X window created. */
8742 XtRealizeWidget (widget);
8743
8744 /* Set the cursor to an arrow. I didn't find a resource to do that.
8745 And I'm wondering why it hasn't an arrow cursor by default. */
8746 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8747 f->output_data.x->nontext_cursor);
8748
ec18280f 8749#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8750
8751 /* Set resources. Create the widget. The background of the
8752 Xaw3d scroll bar widget is a little bit light for my taste.
8753 We don't alter it here to let users change it according
8754 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8755 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8756 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8757 /* For smoother scrolling with Xaw3d -sm */
8758 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
06a2c219
GM
8759
8760 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8761 if (pixel != -1)
8762 {
8763 XtSetArg (av[ac], XtNforeground, pixel);
8764 ++ac;
8765 }
8766
8767 pixel = f->output_data.x->scroll_bar_background_pixel;
8768 if (pixel != -1)
8769 {
8770 XtSetArg (av[ac], XtNbackground, pixel);
8771 ++ac;
8772 }
7c1bef7a
MB
8773
8774 /* Top/bottom shadow colors. */
8775
8776 /* Allocate them, if necessary. */
8777 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8778 {
8779 pixel = f->output_data.x->scroll_bar_background_pixel;
8780 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8781 &pixel, 1.2, 0x8000))
8782 pixel = -1;
8783 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8784 }
8785 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8786 {
8787 pixel = f->output_data.x->scroll_bar_background_pixel;
8788 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8789 &pixel, 0.6, 0x4000))
8790 pixel = -1;
8791 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8792 }
8793
8794 /* Tell the toolkit about them. */
8795 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8796 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8797 /* We tried to allocate a color for the top/bottom shadow, and
8798 failed, so tell Xaw3d to use dithering instead. */
8799 {
8800 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8801 ++ac;
8802 }
8803 else
8804 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8805 be more consistent with other emacs 3d colors, and since Xaw3d is
8806 not good at dealing with allocation failure. */
8807 {
8808 /* This tells Xaw3d to use real colors instead of dithering for
8809 the shadows. */
8810 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8811 ++ac;
8812
8813 /* Specify the colors. */
8814 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8815 if (pixel != -1)
8816 {
8817 XtSetArg (av[ac], "topShadowPixel", pixel);
8818 ++ac;
8819 }
8820 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8821 if (pixel != -1)
8822 {
8823 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8824 ++ac;
8825 }
8826 }
8827
06a2c219
GM
8828 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8829 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8830
8831 {
8832 char *initial = "";
8833 char *val = initial;
8834 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8835 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8836 if (val == initial)
8837 { /* ARROW_SCROLL */
8838 xaw3d_arrow_scroll = True;
8839 /* Isn't that just a personal preference ? -sm */
8840 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8841 }
8842 }
06a2c219
GM
8843
8844 /* Define callbacks. */
ec18280f
SM
8845 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8846 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8847 (XtPointer) bar);
8848
8849 /* Realize the widget. Only after that is the X window created. */
8850 XtRealizeWidget (widget);
8851
ec18280f 8852#endif /* !USE_MOTIF */
06a2c219
GM
8853
8854 /* Install an action hook that let's us detect when the user
8855 finishes interacting with a scroll bar. */
8856 if (action_hook_id == 0)
8857 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8858
8859 /* Remember X window and widget in the scroll bar vector. */
8860 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8861 xwindow = XtWindow (widget);
8862 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8863
8864 UNBLOCK_INPUT;
8865}
8866
8867
8868/* Set the thumb size and position of scroll bar BAR. We are currently
8869 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8870
8871static void
8872x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8873 struct scroll_bar *bar;
8874 int portion, position, whole;
f451eb13 8875{
e83dc917
GM
8876 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8877 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8878 float top, shown;
f451eb13 8879
06a2c219
GM
8880 if (whole == 0)
8881 top = 0, shown = 1;
8882 else
f451eb13 8883 {
06a2c219
GM
8884 top = (float) position / whole;
8885 shown = (float) portion / whole;
8886 }
f451eb13 8887
06a2c219 8888 BLOCK_INPUT;
f451eb13 8889
06a2c219
GM
8890#ifdef USE_MOTIF
8891 {
8892 int size, value;
06a2c219 8893
ec18280f 8894 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8895 is the scroll bar's maximum and MIN is the scroll bar's minimum
8896 value. */
8897 size = shown * XM_SB_RANGE;
8898 size = min (size, XM_SB_RANGE);
8899 size = max (size, 1);
8900
8901 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8902 value = top * XM_SB_RANGE;
8903 value = min (value, XM_SB_MAX - size);
8904 value = max (value, XM_SB_MIN);
8905
06a2c219
GM
8906 if (NILP (bar->dragging))
8907 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8908 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8909 /* This has the negative side effect that the slider value is
ec18280f 8910 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8911 page-wise movement. */
8912 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8913 else
8914 {
8915 /* If currently dragging, only update the slider size.
8916 This reduces flicker effects. */
8917 int old_value, old_size, increment, page_increment;
8918
8919 XmScrollBarGetValues (widget, &old_value, &old_size,
8920 &increment, &page_increment);
8921 XmScrollBarSetValues (widget, old_value,
8922 min (size, XM_SB_RANGE - old_value),
8923 0, 0, False);
8924 }
06a2c219 8925 }
ec18280f 8926#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8927 {
ec18280f
SM
8928 float old_top, old_shown;
8929 Dimension height;
8930 XtVaGetValues (widget,
8931 XtNtopOfThumb, &old_top,
8932 XtNshown, &old_shown,
8933 XtNheight, &height,
8934 NULL);
8935
8936 /* Massage the top+shown values. */
8937 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8938 top = max (0, min (1, top));
8939 else
8940 top = old_top;
8941 /* Keep two pixels available for moving the thumb down. */
8942 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8943
8944 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8945 check that your system's configuration file contains a define
8946 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8947 if (top != old_top || shown != old_shown)
eb393530 8948 {
ec18280f 8949 if (NILP (bar->dragging))
eb393530 8950 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8951 else
8952 {
ec18280f
SM
8953#ifdef HAVE_XAW3D
8954 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8955 int scroll_mode = 0;
ec18280f
SM
8956
8957 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8958 if (xaw3d_arrow_scroll)
8959 {
8960 /* Xaw3d stupidly ignores resize requests while dragging
8961 so we have to make it believe it's not in dragging mode. */
8962 scroll_mode = sb->scrollbar.scroll_mode;
8963 if (scroll_mode == 2)
8964 sb->scrollbar.scroll_mode = 0;
8965 }
8966#endif
8967 /* Try to make the scrolling a tad smoother. */
8968 if (!xaw3d_pick_top)
8969 shown = min (shown, old_shown);
8970
8971 XawScrollbarSetThumb (widget, top, shown);
8972
8973#ifdef HAVE_XAW3D
8974 if (xaw3d_arrow_scroll && scroll_mode == 2)
8975 sb->scrollbar.scroll_mode = scroll_mode;
8976#endif
06a2c219 8977 }
06a2c219
GM
8978 }
8979 }
ec18280f 8980#endif /* !USE_MOTIF */
06a2c219
GM
8981
8982 UNBLOCK_INPUT;
f451eb13
JB
8983}
8984
06a2c219
GM
8985#endif /* USE_TOOLKIT_SCROLL_BARS */
8986
8987
8988\f
8989/************************************************************************
8990 Scroll bars, general
8991 ************************************************************************/
8992
8993/* Create a scroll bar and return the scroll bar vector for it. W is
8994 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 8995 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
8996 scroll bar. */
8997
ab648270 8998static struct scroll_bar *
06a2c219
GM
8999x_scroll_bar_create (w, top, left, width, height)
9000 struct window *w;
f451eb13
JB
9001 int top, left, width, height;
9002{
06a2c219 9003 struct frame *f = XFRAME (w->frame);
334208b7
RS
9004 struct scroll_bar *bar
9005 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
9006
9007 BLOCK_INPUT;
9008
eccc05db 9009#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9010 x_create_toolkit_scroll_bar (f, bar);
9011#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9012 {
9013 XSetWindowAttributes a;
9014 unsigned long mask;
5c187dee 9015 Window window;
06a2c219
GM
9016
9017 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9018 if (a.background_pixel == -1)
9019 a.background_pixel = f->output_data.x->background_pixel;
9020
12ba150f 9021 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 9022 | ButtonMotionMask | PointerMotionHintMask
12ba150f 9023 | ExposureMask);
7a13e894 9024 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 9025
dbc4e1c1 9026 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 9027
06a2c219
GM
9028 /* Clear the area of W that will serve as a scroll bar. This is
9029 for the case that a window has been split horizontally. In
9030 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
9031 if (width > 0 && height > 0)
9032 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9033 left, top, width,
9034 window_box_height (w), False);
06a2c219
GM
9035
9036 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9037 /* Position and size of scroll bar. */
9038 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9039 top,
9040 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9041 height,
9042 /* Border width, depth, class, and visual. */
9043 0,
9044 CopyFromParent,
9045 CopyFromParent,
9046 CopyFromParent,
9047 /* Attributes. */
9048 mask, &a);
9049 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 9050 }
06a2c219 9051#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9052
06a2c219 9053 XSETWINDOW (bar->window, w);
e0c1aef2
KH
9054 XSETINT (bar->top, top);
9055 XSETINT (bar->left, left);
9056 XSETINT (bar->width, width);
9057 XSETINT (bar->height, height);
9058 XSETINT (bar->start, 0);
9059 XSETINT (bar->end, 0);
12ba150f 9060 bar->dragging = Qnil;
f451eb13
JB
9061
9062 /* Add bar to its frame's list of scroll bars. */
334208b7 9063 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 9064 bar->prev = Qnil;
334208b7 9065 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 9066 if (!NILP (bar->next))
e0c1aef2 9067 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 9068
06a2c219 9069 /* Map the window/widget. */
eccc05db 9070#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
9071 {
9072 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9073 XtConfigureWidget (scroll_bar,
9074 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9075 top,
9076 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9077 max (height, 1), 0);
9078 XtMapWidget (scroll_bar);
9079 }
06a2c219 9080#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 9081 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 9082#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9083
9084 UNBLOCK_INPUT;
12ba150f 9085 return bar;
f451eb13
JB
9086}
9087
06a2c219 9088
12ba150f 9089/* Draw BAR's handle in the proper position.
06a2c219 9090
12ba150f
JB
9091 If the handle is already drawn from START to END, don't bother
9092 redrawing it, unless REBUILD is non-zero; in that case, always
9093 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 9094 events.)
12ba150f
JB
9095
9096 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
9097 fit inside its rectangle, but if the user is dragging the scroll
9098 bar handle, we want to let them drag it down all the way, so that
9099 the bar's top is as far down as it goes; otherwise, there's no way
9100 to move to the very end of the buffer. */
9101
5c187dee
GM
9102#ifndef USE_TOOLKIT_SCROLL_BARS
9103
f451eb13 9104static void
ab648270
JB
9105x_scroll_bar_set_handle (bar, start, end, rebuild)
9106 struct scroll_bar *bar;
f451eb13 9107 int start, end;
12ba150f 9108 int rebuild;
f451eb13 9109{
12ba150f 9110 int dragging = ! NILP (bar->dragging);
ab648270 9111 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9112 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9113 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
9114
9115 /* If the display is already accurate, do nothing. */
9116 if (! rebuild
9117 && start == XINT (bar->start)
9118 && end == XINT (bar->end))
9119 return;
9120
f451eb13
JB
9121 BLOCK_INPUT;
9122
9123 {
d9cdbb3d
RS
9124 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9125 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9126 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
9127
9128 /* Make sure the values are reasonable, and try to preserve
9129 the distance between start and end. */
12ba150f
JB
9130 {
9131 int length = end - start;
9132
9133 if (start < 0)
9134 start = 0;
9135 else if (start > top_range)
9136 start = top_range;
9137 end = start + length;
9138
9139 if (end < start)
9140 end = start;
9141 else if (end > top_range && ! dragging)
9142 end = top_range;
9143 }
f451eb13 9144
ab648270 9145 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
9146 XSETINT (bar->start, start);
9147 XSETINT (bar->end, end);
f451eb13 9148
12ba150f
JB
9149 /* Clip the end position, just for display. */
9150 if (end > top_range)
9151 end = top_range;
f451eb13 9152
ab648270 9153 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
9154 below top positions, to make sure the handle is always at least
9155 that many pixels tall. */
ab648270 9156 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 9157
12ba150f
JB
9158 /* Draw the empty space above the handle. Note that we can't clear
9159 zero-height areas; that means "clear to end of window." */
9160 if (0 < start)
c5e6e06b
GM
9161 x_clear_area (FRAME_X_DISPLAY (f), w,
9162 /* x, y, width, height, and exposures. */
9163 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9164 VERTICAL_SCROLL_BAR_TOP_BORDER,
9165 inside_width, start,
9166 False);
f451eb13 9167
06a2c219
GM
9168 /* Change to proper foreground color if one is specified. */
9169 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9170 XSetForeground (FRAME_X_DISPLAY (f), gc,
9171 f->output_data.x->scroll_bar_foreground_pixel);
9172
12ba150f 9173 /* Draw the handle itself. */
334208b7 9174 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 9175 /* x, y, width, height */
ab648270
JB
9176 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9177 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 9178 inside_width, end - start);
f451eb13 9179
06a2c219
GM
9180 /* Restore the foreground color of the GC if we changed it above. */
9181 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9182 XSetForeground (FRAME_X_DISPLAY (f), gc,
9183 f->output_data.x->foreground_pixel);
f451eb13 9184
12ba150f
JB
9185 /* Draw the empty space below the handle. Note that we can't
9186 clear zero-height areas; that means "clear to end of window." */
9187 if (end < inside_height)
c5e6e06b
GM
9188 x_clear_area (FRAME_X_DISPLAY (f), w,
9189 /* x, y, width, height, and exposures. */
9190 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9191 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9192 inside_width, inside_height - end,
9193 False);
f451eb13 9194
f451eb13
JB
9195 }
9196
f451eb13
JB
9197 UNBLOCK_INPUT;
9198}
9199
5c187dee 9200#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 9201
06a2c219
GM
9202/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9203 nil. */
58769bee 9204
12ba150f 9205static void
ab648270
JB
9206x_scroll_bar_remove (bar)
9207 struct scroll_bar *bar;
12ba150f 9208{
e83dc917 9209 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
9210 BLOCK_INPUT;
9211
eccc05db 9212#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
9213 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9214#else
9215 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9216#endif
06a2c219 9217
ab648270
JB
9218 /* Disassociate this scroll bar from its window. */
9219 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
9220
9221 UNBLOCK_INPUT;
9222}
9223
06a2c219 9224
12ba150f
JB
9225/* Set the handle of the vertical scroll bar for WINDOW to indicate
9226 that we are displaying PORTION characters out of a total of WHOLE
ab648270 9227 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 9228 create one. */
06a2c219 9229
12ba150f 9230static void
06a2c219
GM
9231XTset_vertical_scroll_bar (w, portion, whole, position)
9232 struct window *w;
f451eb13
JB
9233 int portion, whole, position;
9234{
06a2c219 9235 struct frame *f = XFRAME (w->frame);
ab648270 9236 struct scroll_bar *bar;
3c6ede7b 9237 int top, height, left, sb_left, width, sb_width;
06a2c219 9238 int window_x, window_y, window_width, window_height;
06a2c219 9239
3c6ede7b 9240 /* Get window dimensions. */
06a2c219 9241 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
9242 top = window_y;
9243 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9244 height = window_height;
06a2c219 9245
3c6ede7b 9246 /* Compute the left edge of the scroll bar area. */
06a2c219 9247 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
9248 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9249 else
9250 left = XFASTINT (w->left);
9251 left *= CANON_X_UNIT (f);
9252 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9253
9254 /* Compute the width of the scroll bar which might be less than
9255 the width of the area reserved for the scroll bar. */
9256 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9257 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 9258 else
3c6ede7b 9259 sb_width = width;
12ba150f 9260
3c6ede7b
GM
9261 /* Compute the left edge of the scroll bar. */
9262#ifdef USE_TOOLKIT_SCROLL_BARS
9263 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9264 sb_left = left + width - sb_width - (width - sb_width) / 2;
9265 else
9266 sb_left = left + (width - sb_width) / 2;
9267#else
9268 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9269 sb_left = left + width - sb_width;
9270 else
9271 sb_left = left;
9272#endif
9273
ab648270 9274 /* Does the scroll bar exist yet? */
06a2c219 9275 if (NILP (w->vertical_scroll_bar))
3c6ede7b 9276 {
7b49b9d2 9277 if (width > 0 && height > 0)
b547b6e8
GM
9278 {
9279 BLOCK_INPUT;
9280 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9281 left, top, width, height, False);
9282 UNBLOCK_INPUT;
9283 }
9284
3c6ede7b
GM
9285 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9286 }
f451eb13 9287 else
12ba150f
JB
9288 {
9289 /* It may just need to be moved and resized. */
06a2c219
GM
9290 unsigned int mask = 0;
9291
9292 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9293
9294 BLOCK_INPUT;
9295
3c6ede7b 9296 if (sb_left != XINT (bar->left))
06a2c219 9297 mask |= CWX;
3c6ede7b 9298 if (top != XINT (bar->top))
06a2c219 9299 mask |= CWY;
3c6ede7b 9300 if (sb_width != XINT (bar->width))
06a2c219 9301 mask |= CWWidth;
3c6ede7b 9302 if (height != XINT (bar->height))
06a2c219
GM
9303 mask |= CWHeight;
9304
9305#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
9306
9307 /* Since toolkit scroll bars are smaller than the space reserved
9308 for them on the frame, we have to clear "under" them. */
7b49b9d2 9309 if (width > 0 && height > 0)
f964b4d7
GM
9310 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9311 left, top, width, height, False);
06a2c219
GM
9312
9313 /* Move/size the scroll bar widget. */
9314 if (mask)
e83dc917 9315 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
9316 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9317 top,
9318 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 9319 max (height, 1), 0);
06a2c219
GM
9320
9321#else /* not USE_TOOLKIT_SCROLL_BARS */
9322
357e7376
GM
9323 /* Clear areas not covered by the scroll bar because of
9324 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
9325 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9326 {
c5e6e06b
GM
9327 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9328 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9329 height, False);
9330 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9331 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9332 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9333 height, False);
e1f6572f 9334 }
357e7376
GM
9335
9336 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 9337 wide as the area reserved for it. This makes sure a
357e7376
GM
9338 previous mode line display is cleared after C-x 2 C-x 1, for
9339 example. */
9340 {
9341 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9342 int rest = area_width - sb_width;
38d2af0c
GM
9343 if (rest > 0 && height > 0)
9344 {
9345 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9346 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9347 left + area_width - rest, top,
9348 rest, height, False);
9349 else
9350 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9351 left, top, rest, height, False);
9352 }
357e7376 9353 }
06a2c219
GM
9354
9355 /* Move/size the scroll bar window. */
9356 if (mask)
9357 {
9358 XWindowChanges wc;
9359
3c6ede7b
GM
9360 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9361 wc.y = top;
9362 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9363 wc.height = height;
06a2c219
GM
9364 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9365 mask, &wc);
9366 }
9367
9368#endif /* not USE_TOOLKIT_SCROLL_BARS */
9369
9370 /* Remember new settings. */
3c6ede7b
GM
9371 XSETINT (bar->left, sb_left);
9372 XSETINT (bar->top, top);
9373 XSETINT (bar->width, sb_width);
9374 XSETINT (bar->height, height);
06a2c219
GM
9375
9376 UNBLOCK_INPUT;
12ba150f 9377 }
f451eb13 9378
eccc05db 9379#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9380 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9381#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 9382 /* Set the scroll bar's current state, unless we're currently being
f451eb13 9383 dragged. */
12ba150f 9384 if (NILP (bar->dragging))
f451eb13 9385 {
92857db0 9386 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 9387
12ba150f 9388 if (whole == 0)
ab648270 9389 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
9390 else
9391 {
43f868f5
JB
9392 int start = ((double) position * top_range) / whole;
9393 int end = ((double) (position + portion) * top_range) / whole;
ab648270 9394 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 9395 }
f451eb13 9396 }
06a2c219 9397#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9398
06a2c219 9399 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
9400}
9401
12ba150f 9402
f451eb13 9403/* The following three hooks are used when we're doing a thorough
ab648270 9404 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 9405 are going to be deleted, because keeping track of when windows go
12ba150f
JB
9406 away is a real pain - "Can you say set-window-configuration, boys
9407 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 9408 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 9409 from the fiery pit when we actually redisplay its window. */
f451eb13 9410
ab648270
JB
9411/* Arrange for all scroll bars on FRAME to be removed at the next call
9412 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
9413 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9414
58769bee 9415static void
ab648270 9416XTcondemn_scroll_bars (frame)
f451eb13
JB
9417 FRAME_PTR frame;
9418{
f9e24cb9
RS
9419 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9420 while (! NILP (FRAME_SCROLL_BARS (frame)))
9421 {
9422 Lisp_Object bar;
9423 bar = FRAME_SCROLL_BARS (frame);
9424 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9425 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9426 XSCROLL_BAR (bar)->prev = Qnil;
9427 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9428 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9429 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9430 }
f451eb13
JB
9431}
9432
fa2dfc30 9433
06a2c219 9434/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 9435 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 9436
f451eb13 9437static void
ab648270 9438XTredeem_scroll_bar (window)
12ba150f 9439 struct window *window;
f451eb13 9440{
ab648270 9441 struct scroll_bar *bar;
fa2dfc30 9442 struct frame *f;
12ba150f 9443
ab648270
JB
9444 /* We can't redeem this window's scroll bar if it doesn't have one. */
9445 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
9446 abort ();
9447
ab648270 9448 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
9449
9450 /* Unlink it from the condemned list. */
fa2dfc30
GM
9451 f = XFRAME (WINDOW_FRAME (window));
9452 if (NILP (bar->prev))
9453 {
9454 /* If the prev pointer is nil, it must be the first in one of
9455 the lists. */
9456 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9457 /* It's not condemned. Everything's fine. */
9458 return;
9459 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9460 window->vertical_scroll_bar))
9461 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9462 else
9463 /* If its prev pointer is nil, it must be at the front of
9464 one or the other! */
9465 abort ();
9466 }
9467 else
9468 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 9469
fa2dfc30
GM
9470 if (! NILP (bar->next))
9471 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 9472
fa2dfc30
GM
9473 bar->next = FRAME_SCROLL_BARS (f);
9474 bar->prev = Qnil;
9475 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9476 if (! NILP (bar->next))
9477 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
9478}
9479
ab648270
JB
9480/* Remove all scroll bars on FRAME that haven't been saved since the
9481 last call to `*condemn_scroll_bars_hook'. */
06a2c219 9482
f451eb13 9483static void
ab648270 9484XTjudge_scroll_bars (f)
12ba150f 9485 FRAME_PTR f;
f451eb13 9486{
12ba150f 9487 Lisp_Object bar, next;
f451eb13 9488
ab648270 9489 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
9490
9491 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
9492 more events on the hapless scroll bars. */
9493 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
9494
9495 for (; ! NILP (bar); bar = next)
f451eb13 9496 {
ab648270 9497 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 9498
ab648270 9499 x_scroll_bar_remove (b);
12ba150f
JB
9500
9501 next = b->next;
9502 b->next = b->prev = Qnil;
f451eb13 9503 }
12ba150f 9504
ab648270 9505 /* Now there should be no references to the condemned scroll bars,
12ba150f 9506 and they should get garbage-collected. */
f451eb13
JB
9507}
9508
9509
06a2c219
GM
9510/* Handle an Expose or GraphicsExpose event on a scroll bar. This
9511 is a no-op when using toolkit scroll bars.
ab648270
JB
9512
9513 This may be called from a signal handler, so we have to ignore GC
9514 mark bits. */
06a2c219 9515
f451eb13 9516static void
ab648270
JB
9517x_scroll_bar_expose (bar, event)
9518 struct scroll_bar *bar;
f451eb13
JB
9519 XEvent *event;
9520{
06a2c219
GM
9521#ifndef USE_TOOLKIT_SCROLL_BARS
9522
ab648270 9523 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9524 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9525 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 9526 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 9527
f451eb13
JB
9528 BLOCK_INPUT;
9529
ab648270 9530 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 9531
06a2c219 9532 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 9533 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
9534
9535 /* x, y, width, height */
d9cdbb3d 9536 0, 0,
3cbd2e0b 9537 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
9538 XINT (bar->height) - 1);
9539
f451eb13 9540 UNBLOCK_INPUT;
06a2c219
GM
9541
9542#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9543}
9544
ab648270
JB
9545/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9546 is set to something other than no_event, it is enqueued.
9547
9548 This may be called from a signal handler, so we have to ignore GC
9549 mark bits. */
06a2c219 9550
5c187dee
GM
9551#ifndef USE_TOOLKIT_SCROLL_BARS
9552
f451eb13 9553static void
ab648270
JB
9554x_scroll_bar_handle_click (bar, event, emacs_event)
9555 struct scroll_bar *bar;
f451eb13
JB
9556 XEvent *event;
9557 struct input_event *emacs_event;
9558{
0299d313 9559 if (! GC_WINDOWP (bar->window))
12ba150f
JB
9560 abort ();
9561
ab648270 9562 emacs_event->kind = scroll_bar_click;
69388238 9563 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
9564 emacs_event->modifiers
9565 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9566 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9567 event->xbutton.state)
9568 | (event->type == ButtonRelease
9569 ? up_modifier
9570 : down_modifier));
12ba150f 9571 emacs_event->frame_or_window = bar->window;
0f8aabe9 9572 emacs_event->arg = Qnil;
f451eb13 9573 emacs_event->timestamp = event->xbutton.time;
12ba150f 9574 {
06a2c219 9575#if 0
d9cdbb3d 9576 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 9577 int internal_height
d9cdbb3d 9578 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9579#endif
0299d313 9580 int top_range
d9cdbb3d 9581 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 9582 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
9583
9584 if (y < 0) y = 0;
9585 if (y > top_range) y = top_range;
9586
9587 if (y < XINT (bar->start))
ab648270
JB
9588 emacs_event->part = scroll_bar_above_handle;
9589 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9590 emacs_event->part = scroll_bar_handle;
12ba150f 9591 else
ab648270 9592 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
9593
9594 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
9595 they want to drag it. Lisp code needs to be able to decide
9596 whether or not we're dragging. */
929787e1 9597#if 0
12ba150f
JB
9598 /* If the user has just clicked on the handle, record where they're
9599 holding it. */
9600 if (event->type == ButtonPress
ab648270 9601 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9602 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9603#endif
12ba150f
JB
9604
9605 /* If the user has released the handle, set it to its final position. */
9606 if (event->type == ButtonRelease
9607 && ! NILP (bar->dragging))
9608 {
9609 int new_start = y - XINT (bar->dragging);
9610 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9611
ab648270 9612 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9613 bar->dragging = Qnil;
9614 }
f451eb13 9615
5116f055
JB
9616 /* Same deal here as the other #if 0. */
9617#if 0
58769bee 9618 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9619 the handle. */
ab648270 9620 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9621 emacs_event->x = bar->start;
9622 else
e0c1aef2 9623 XSETINT (emacs_event->x, y);
5116f055 9624#else
e0c1aef2 9625 XSETINT (emacs_event->x, y);
5116f055 9626#endif
f451eb13 9627
e0c1aef2 9628 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9629 }
9630}
f451eb13 9631
ab648270
JB
9632/* Handle some mouse motion while someone is dragging the scroll bar.
9633
9634 This may be called from a signal handler, so we have to ignore GC
9635 mark bits. */
06a2c219 9636
f451eb13 9637static void
ab648270
JB
9638x_scroll_bar_note_movement (bar, event)
9639 struct scroll_bar *bar;
f451eb13
JB
9640 XEvent *event;
9641{
39d8bb4d
KH
9642 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9643
f451eb13
JB
9644 last_mouse_movement_time = event->xmotion.time;
9645
39d8bb4d 9646 f->mouse_moved = 1;
e0c1aef2 9647 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9648
9649 /* If we're dragging the bar, display it. */
ab648270 9650 if (! GC_NILP (bar->dragging))
f451eb13
JB
9651 {
9652 /* Where should the handle be now? */
12ba150f 9653 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9654
12ba150f 9655 if (new_start != XINT (bar->start))
f451eb13 9656 {
12ba150f 9657 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9658
ab648270 9659 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9660 }
9661 }
f451eb13
JB
9662}
9663
5c187dee
GM
9664#endif /* !USE_TOOLKIT_SCROLL_BARS */
9665
12ba150f 9666/* Return information to the user about the current position of the mouse
ab648270 9667 on the scroll bar. */
06a2c219 9668
12ba150f 9669static void
334208b7
RS
9670x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9671 FRAME_PTR *fp;
12ba150f 9672 Lisp_Object *bar_window;
ab648270 9673 enum scroll_bar_part *part;
12ba150f
JB
9674 Lisp_Object *x, *y;
9675 unsigned long *time;
9676{
ab648270 9677 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9678 Window w = SCROLL_BAR_X_WINDOW (bar);
9679 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9680 int win_x, win_y;
559cb2fb
JB
9681 Window dummy_window;
9682 int dummy_coord;
9683 unsigned int dummy_mask;
12ba150f 9684
cf7cb199
JB
9685 BLOCK_INPUT;
9686
ab648270 9687 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9688 report that. */
334208b7 9689 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9690
559cb2fb
JB
9691 /* Root, child, root x and root y. */
9692 &dummy_window, &dummy_window,
9693 &dummy_coord, &dummy_coord,
12ba150f 9694
559cb2fb
JB
9695 /* Position relative to scroll bar. */
9696 &win_x, &win_y,
12ba150f 9697
559cb2fb
JB
9698 /* Mouse buttons and modifier keys. */
9699 &dummy_mask))
7a13e894 9700 ;
559cb2fb
JB
9701 else
9702 {
06a2c219 9703#if 0
559cb2fb 9704 int inside_height
d9cdbb3d 9705 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9706#endif
559cb2fb 9707 int top_range
d9cdbb3d 9708 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9709
9710 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9711
9712 if (! NILP (bar->dragging))
9713 win_y -= XINT (bar->dragging);
9714
9715 if (win_y < 0)
9716 win_y = 0;
9717 if (win_y > top_range)
9718 win_y = top_range;
9719
334208b7 9720 *fp = f;
7a13e894 9721 *bar_window = bar->window;
559cb2fb
JB
9722
9723 if (! NILP (bar->dragging))
9724 *part = scroll_bar_handle;
9725 else if (win_y < XINT (bar->start))
9726 *part = scroll_bar_above_handle;
9727 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9728 *part = scroll_bar_handle;
9729 else
9730 *part = scroll_bar_below_handle;
12ba150f 9731
e0c1aef2
KH
9732 XSETINT (*x, win_y);
9733 XSETINT (*y, top_range);
12ba150f 9734
39d8bb4d 9735 f->mouse_moved = 0;
559cb2fb
JB
9736 last_mouse_scroll_bar = Qnil;
9737 }
12ba150f 9738
559cb2fb 9739 *time = last_mouse_movement_time;
cf7cb199 9740
cf7cb199 9741 UNBLOCK_INPUT;
12ba150f
JB
9742}
9743
f451eb13 9744
dbc4e1c1 9745/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9746 background colors, and the scroll bars may need to be redrawn.
9747 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9748 redraw them. */
9749
dfcf069d 9750void
ab648270 9751x_scroll_bar_clear (f)
dbc4e1c1
JB
9752 FRAME_PTR f;
9753{
06a2c219 9754#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9755 Lisp_Object bar;
9756
b80c363e
RS
9757 /* We can have scroll bars even if this is 0,
9758 if we just turned off scroll bar mode.
9759 But in that case we should not clear them. */
9760 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9761 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9762 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9763 XClearArea (FRAME_X_DISPLAY (f),
9764 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9765 0, 0, 0, 0, True);
06a2c219 9766#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9767}
9768
06a2c219 9769/* This processes Expose events from the menu-bar specific X event
19126e11 9770 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9771 when handling menu-bar or pop-up items. */
3afe33e7 9772
06a2c219 9773int
3afe33e7
RS
9774process_expose_from_menu (event)
9775 XEvent event;
9776{
9777 FRAME_PTR f;
19126e11 9778 struct x_display_info *dpyinfo;
06a2c219 9779 int frame_exposed_p = 0;
3afe33e7 9780
f94397b5
KH
9781 BLOCK_INPUT;
9782
19126e11
KH
9783 dpyinfo = x_display_info_for_display (event.xexpose.display);
9784 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9785 if (f)
9786 {
9787 if (f->async_visible == 0)
9788 {
9789 f->async_visible = 1;
9790 f->async_iconified = 0;
06c488fd 9791 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9792 SET_FRAME_GARBAGED (f);
9793 }
9794 else
9795 {
06a2c219
GM
9796 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9797 event.xexpose.x, event.xexpose.y,
9798 event.xexpose.width, event.xexpose.height);
9799 frame_exposed_p = 1;
3afe33e7
RS
9800 }
9801 }
9802 else
9803 {
9804 struct scroll_bar *bar
9805 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9806
3afe33e7
RS
9807 if (bar)
9808 x_scroll_bar_expose (bar, &event);
9809 }
f94397b5
KH
9810
9811 UNBLOCK_INPUT;
06a2c219 9812 return frame_exposed_p;
3afe33e7 9813}
09756a85
RS
9814\f
9815/* Define a queue to save up SelectionRequest events for later handling. */
9816
9817struct selection_event_queue
9818 {
9819 XEvent event;
9820 struct selection_event_queue *next;
9821 };
9822
9823static struct selection_event_queue *queue;
9824
9825/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9826
09756a85
RS
9827static int x_queue_selection_requests;
9828
9829/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9830
09756a85 9831static void
334208b7
RS
9832x_queue_event (f, event)
9833 FRAME_PTR f;
09756a85
RS
9834 XEvent *event;
9835{
9836 struct selection_event_queue *queue_tmp
06a2c219 9837 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9838
58769bee 9839 if (queue_tmp != NULL)
09756a85
RS
9840 {
9841 queue_tmp->event = *event;
9842 queue_tmp->next = queue;
9843 queue = queue_tmp;
9844 }
9845}
9846
9847/* Take all the queued events and put them back
9848 so that they get processed afresh. */
9849
9850static void
db3906fd
RS
9851x_unqueue_events (display)
9852 Display *display;
09756a85 9853{
58769bee 9854 while (queue != NULL)
09756a85
RS
9855 {
9856 struct selection_event_queue *queue_tmp = queue;
db3906fd 9857 XPutBackEvent (display, &queue_tmp->event);
09756a85 9858 queue = queue_tmp->next;
06a2c219 9859 xfree ((char *)queue_tmp);
09756a85
RS
9860 }
9861}
9862
9863/* Start queuing SelectionRequest events. */
9864
9865void
db3906fd
RS
9866x_start_queuing_selection_requests (display)
9867 Display *display;
09756a85
RS
9868{
9869 x_queue_selection_requests++;
9870}
9871
9872/* Stop queuing SelectionRequest events. */
9873
9874void
db3906fd
RS
9875x_stop_queuing_selection_requests (display)
9876 Display *display;
09756a85
RS
9877{
9878 x_queue_selection_requests--;
db3906fd 9879 x_unqueue_events (display);
09756a85 9880}
f451eb13
JB
9881\f
9882/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9883
042c33d3 9884#if 0
06a2c219 9885/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9886 but we have to put it out here, since static variables within functions
9887 sometimes don't work. */
06a2c219 9888
dc6f92b8 9889static Time enter_timestamp;
042c33d3 9890#endif
dc6f92b8 9891
11edeb03 9892/* This holds the state XLookupString needs to implement dead keys
58769bee 9893 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9894 says that a portable program can't use this, but Stephen Gildea assures
9895 me that letting the compiler initialize it to zeros will work okay.
9896
9897 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 9898 given for enter_timestamp, above. */
06a2c219 9899
11edeb03
JB
9900static XComposeStatus compose_status;
9901
10e6549c
RS
9902/* Record the last 100 characters stored
9903 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9904
2224b905
RS
9905static int temp_index;
9906static short temp_buffer[100];
10e6549c 9907
7a13e894
RS
9908/* Set this to nonzero to fake an "X I/O error"
9909 on a particular display. */
06a2c219 9910
7a13e894
RS
9911struct x_display_info *XTread_socket_fake_io_error;
9912
2224b905
RS
9913/* When we find no input here, we occasionally do a no-op command
9914 to verify that the X server is still running and we can still talk with it.
9915 We try all the open displays, one by one.
9916 This variable is used for cycling thru the displays. */
06a2c219 9917
2224b905
RS
9918static struct x_display_info *next_noop_dpyinfo;
9919
06a2c219
GM
9920#define SET_SAVED_MENU_EVENT(size) \
9921 do \
9922 { \
9923 if (f->output_data.x->saved_menu_event == 0) \
9924 f->output_data.x->saved_menu_event \
9925 = (XEvent *) xmalloc (sizeof (XEvent)); \
9926 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9927 if (numchars >= 1) \
9928 { \
9929 bufp->kind = menu_bar_activate_event; \
9930 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9931 bufp->arg = Qnil; \
06a2c219
GM
9932 bufp++; \
9933 count++; \
9934 numchars--; \
9935 } \
9936 } \
9937 while (0)
9938
8805890a 9939#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9940#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9941
dc6f92b8
JB
9942/* Read events coming from the X server.
9943 This routine is called by the SIGIO handler.
9944 We return as soon as there are no more events to be read.
9945
9946 Events representing keys are stored in buffer BUFP,
9947 which can hold up to NUMCHARS characters.
9948 We return the number of characters stored into the buffer,
9949 thus pretending to be `read'.
9950
dc6f92b8
JB
9951 EXPECTED is nonzero if the caller knows input is available. */
9952
7c5283e4 9953int
f66868ba 9954XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9955 register int sd;
8805890a
KH
9956 /* register */ struct input_event *bufp;
9957 /* register */ int numchars;
dc6f92b8
JB
9958 int expected;
9959{
9960 int count = 0;
9961 int nbytes = 0;
dc6f92b8 9962 XEvent event;
f676886a 9963 struct frame *f;
66f55a9d 9964 int event_found = 0;
334208b7 9965 struct x_display_info *dpyinfo;
379b5ac0 9966 struct coding_system coding;
dc6f92b8 9967
9ac0d9e0 9968 if (interrupt_input_blocked)
dc6f92b8 9969 {
9ac0d9e0 9970 interrupt_input_pending = 1;
dc6f92b8
JB
9971 return -1;
9972 }
9973
9ac0d9e0 9974 interrupt_input_pending = 0;
dc6f92b8 9975 BLOCK_INPUT;
c0a04927
RS
9976
9977 /* So people can tell when we have read the available input. */
9978 input_signal_count++;
9979
dc6f92b8 9980 if (numchars <= 0)
06a2c219 9981 abort (); /* Don't think this happens. */
dc6f92b8 9982
bde5503b
GM
9983 ++handling_signal;
9984
379b5ac0
KH
9985 /* The input should be decoded if it is from XIM. Currently the
9986 locale of XIM is the same as that of the system. So, we can use
9987 Vlocale_coding_system which is initialized properly at Emacs
9988 startup time. */
9989 setup_coding_system (Vlocale_coding_system, &coding);
9990 coding.src_multibyte = 0;
9991 coding.dst_multibyte = 1;
9992 /* The input is converted to events, thus we can't handle
9993 composition. Anyway, there's no XIM that gives us composition
9994 information. */
9995 coding.composing = COMPOSITION_DISABLED;
9996
7a13e894
RS
9997 /* Find the display we are supposed to read input for.
9998 It's the one communicating on descriptor SD. */
9999 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
10000 {
10001#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 10002#ifdef FIOSNBIO
7a13e894
RS
10003 /* If available, Xlib uses FIOSNBIO to make the socket
10004 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 10005 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 10006 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 10007 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 10008#endif /* ! defined (FIOSNBIO) */
7a13e894 10009#endif
dc6f92b8 10010
7a13e894
RS
10011#if 0 /* This code can't be made to work, with multiple displays,
10012 and appears not to be used on any system any more.
10013 Also keyboard.c doesn't turn O_NDELAY on and off
10014 for X connections. */
dc6f92b8
JB
10015#ifndef SIGIO
10016#ifndef HAVE_SELECT
7a13e894
RS
10017 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10018 {
10019 extern int read_alarm_should_throw;
10020 read_alarm_should_throw = 1;
10021 XPeekEvent (dpyinfo->display, &event);
10022 read_alarm_should_throw = 0;
10023 }
c118dd06
JB
10024#endif /* HAVE_SELECT */
10025#endif /* SIGIO */
7a13e894 10026#endif
dc6f92b8 10027
7a13e894
RS
10028 /* For debugging, this gives a way to fake an I/O error. */
10029 if (dpyinfo == XTread_socket_fake_io_error)
10030 {
10031 XTread_socket_fake_io_error = 0;
10032 x_io_error_quitter (dpyinfo->display);
10033 }
dc6f92b8 10034
06a2c219 10035 while (XPending (dpyinfo->display))
dc6f92b8 10036 {
7a13e894 10037 XNextEvent (dpyinfo->display, &event);
06a2c219 10038
531483fb 10039#ifdef HAVE_X_I18N
d1bc4182 10040 {
f2be1146
GM
10041 /* Filter events for the current X input method.
10042 XFilterEvent returns non-zero if the input method has
10043 consumed the event. We pass the frame's X window to
10044 XFilterEvent because that's the one for which the IC
10045 was created. */
f5d11644
GM
10046 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10047 event.xclient.window);
10048 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
10049 break;
10050 }
0cd6403b 10051#endif
7a13e894
RS
10052 event_found = 1;
10053
10054 switch (event.type)
10055 {
10056 case ClientMessage:
c047688c 10057 {
7a13e894
RS
10058 if (event.xclient.message_type
10059 == dpyinfo->Xatom_wm_protocols
10060 && event.xclient.format == 32)
c047688c 10061 {
7a13e894
RS
10062 if (event.xclient.data.l[0]
10063 == dpyinfo->Xatom_wm_take_focus)
c047688c 10064 {
8c1a6a84
RS
10065 /* Use x_any_window_to_frame because this
10066 could be the shell widget window
10067 if the frame has no title bar. */
10068 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
10069#ifdef HAVE_X_I18N
10070 /* Not quite sure this is needed -pd */
8c1a6a84 10071 if (f && FRAME_XIC (f))
6c183ba5
RS
10072 XSetICFocus (FRAME_XIC (f));
10073#endif
f1da8f06
GM
10074#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10075 instructs the WM to set the input focus automatically for
10076 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10077 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10078 it has set the focus. So, XSetInputFocus below is not
10079 needed.
10080
10081 The call to XSetInputFocus below has also caused trouble. In
10082 cases where the XSetInputFocus done by the WM and the one
10083 below are temporally close (on a fast machine), the call
10084 below can generate additional FocusIn events which confuse
10085 Emacs. */
10086
bf7253f4
RS
10087 /* Since we set WM_TAKE_FOCUS, we must call
10088 XSetInputFocus explicitly. But not if f is null,
10089 since that might be an event for a deleted frame. */
7a13e894 10090 if (f)
bf7253f4
RS
10091 {
10092 Display *d = event.xclient.display;
10093 /* Catch and ignore errors, in case window has been
10094 iconified by a window manager such as GWM. */
10095 int count = x_catch_errors (d);
10096 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
10097 /* The ICCCM says this is
10098 the only valid choice. */
10099 RevertToParent,
bf7253f4
RS
10100 event.xclient.data.l[1]);
10101 /* This is needed to detect the error
10102 if there is an error. */
10103 XSync (d, False);
10104 x_uncatch_errors (d, count);
10105 }
7a13e894 10106 /* Not certain about handling scroll bars here */
f1da8f06 10107#endif /* 0 */
c047688c 10108 }
7a13e894
RS
10109 else if (event.xclient.data.l[0]
10110 == dpyinfo->Xatom_wm_save_yourself)
10111 {
10112 /* Save state modify the WM_COMMAND property to
06a2c219 10113 something which can reinstate us. This notifies
7a13e894
RS
10114 the session manager, who's looking for such a
10115 PropertyNotify. Can restart processing when
06a2c219 10116 a keyboard or mouse event arrives. */
7a13e894
RS
10117 if (numchars > 0)
10118 {
19126e11
KH
10119 f = x_top_window_to_frame (dpyinfo,
10120 event.xclient.window);
7a13e894
RS
10121
10122 /* This is just so we only give real data once
10123 for a single Emacs process. */
b86bd3dd 10124 if (f == SELECTED_FRAME ())
7a13e894
RS
10125 XSetCommand (FRAME_X_DISPLAY (f),
10126 event.xclient.window,
10127 initial_argv, initial_argc);
f000f5c5 10128 else if (f)
7a13e894
RS
10129 XSetCommand (FRAME_X_DISPLAY (f),
10130 event.xclient.window,
10131 0, 0);
10132 }
10133 }
10134 else if (event.xclient.data.l[0]
10135 == dpyinfo->Xatom_wm_delete_window)
1fb20991 10136 {
19126e11
KH
10137 struct frame *f
10138 = x_any_window_to_frame (dpyinfo,
10139 event.xclient.window);
1fb20991 10140
7a13e894
RS
10141 if (f)
10142 {
10143 if (numchars == 0)
10144 abort ();
1fb20991 10145
7a13e894
RS
10146 bufp->kind = delete_window_event;
10147 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10148 bufp->arg = Qnil;
7a13e894
RS
10149 bufp++;
10150
10151 count += 1;
10152 numchars -= 1;
10153 }
1fb20991 10154 }
c047688c 10155 }
7a13e894
RS
10156 else if (event.xclient.message_type
10157 == dpyinfo->Xatom_wm_configure_denied)
10158 {
10159 }
10160 else if (event.xclient.message_type
10161 == dpyinfo->Xatom_wm_window_moved)
10162 {
10163 int new_x, new_y;
19126e11
KH
10164 struct frame *f
10165 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 10166
7a13e894
RS
10167 new_x = event.xclient.data.s[0];
10168 new_y = event.xclient.data.s[1];
1fb20991 10169
7a13e894
RS
10170 if (f)
10171 {
7556890b
RS
10172 f->output_data.x->left_pos = new_x;
10173 f->output_data.x->top_pos = new_y;
7a13e894 10174 }
1fb20991 10175 }
0fdff6bb 10176#ifdef HACK_EDITRES
7a13e894
RS
10177 else if (event.xclient.message_type
10178 == dpyinfo->Xatom_editres)
10179 {
19126e11
KH
10180 struct frame *f
10181 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 10182 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 10183 &event, NULL);
7a13e894 10184 }
0fdff6bb 10185#endif /* HACK_EDITRES */
06a2c219
GM
10186 else if ((event.xclient.message_type
10187 == dpyinfo->Xatom_DONE)
10188 || (event.xclient.message_type
10189 == dpyinfo->Xatom_PAGE))
10190 {
10191 /* Ghostview job completed. Kill it. We could
10192 reply with "Next" if we received "Page", but we
10193 currently never do because we are interested in
10194 images, only, which should have 1 page. */
06a2c219
GM
10195 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10196 struct frame *f
10197 = x_window_to_frame (dpyinfo, event.xclient.window);
10198 x_kill_gs_process (pixmap, f);
10199 expose_frame (f, 0, 0, 0, 0);
10200 }
10201#ifdef USE_TOOLKIT_SCROLL_BARS
10202 /* Scroll bar callbacks send a ClientMessage from which
10203 we construct an input_event. */
10204 else if (event.xclient.message_type
10205 == dpyinfo->Xatom_Scrollbar)
10206 {
10207 x_scroll_bar_to_input_event (&event, bufp);
10208 ++bufp, ++count, --numchars;
10209 goto out;
10210 }
10211#endif /* USE_TOOLKIT_SCROLL_BARS */
10212 else
10213 goto OTHER;
7a13e894
RS
10214 }
10215 break;
dc6f92b8 10216
7a13e894 10217 case SelectionNotify:
3afe33e7 10218#ifdef USE_X_TOOLKIT
19126e11 10219 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 10220 goto OTHER;
3afe33e7 10221#endif /* not USE_X_TOOLKIT */
dfcf069d 10222 x_handle_selection_notify (&event.xselection);
7a13e894 10223 break;
d56a553a 10224
06a2c219 10225 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 10226#ifdef USE_X_TOOLKIT
19126e11 10227 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 10228 goto OTHER;
3afe33e7 10229#endif /* USE_X_TOOLKIT */
7a13e894
RS
10230 {
10231 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 10232
7a13e894
RS
10233 if (numchars == 0)
10234 abort ();
d56a553a 10235
7a13e894
RS
10236 bufp->kind = selection_clear_event;
10237 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10238 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10239 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10240 bufp->frame_or_window = Qnil;
0f8aabe9 10241 bufp->arg = Qnil;
7a13e894 10242 bufp++;
d56a553a 10243
7a13e894
RS
10244 count += 1;
10245 numchars -= 1;
10246 }
10247 break;
dc6f92b8 10248
06a2c219 10249 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 10250#ifdef USE_X_TOOLKIT
19126e11 10251 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 10252 goto OTHER;
3afe33e7 10253#endif /* USE_X_TOOLKIT */
7a13e894 10254 if (x_queue_selection_requests)
19126e11 10255 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
10256 &event);
10257 else
10258 {
1d2b2268
GM
10259 XSelectionRequestEvent *eventp
10260 = (XSelectionRequestEvent *) &event;
dc6f92b8 10261
7a13e894
RS
10262 if (numchars == 0)
10263 abort ();
10264
10265 bufp->kind = selection_request_event;
10266 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10267 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10268 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10269 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10270 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10271 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10272 bufp->frame_or_window = Qnil;
0f8aabe9 10273 bufp->arg = Qnil;
7a13e894
RS
10274 bufp++;
10275
10276 count += 1;
10277 numchars -= 1;
10278 }
10279 break;
10280
10281 case PropertyNotify:
1d2b2268
GM
10282#if 0 /* This is plain wrong. In the case that we are waiting for a
10283 PropertyNotify used as an ACK in incremental selection
10284 transfer, the property will be on the receiver's window. */
10285#if defined USE_X_TOOLKIT
19126e11 10286 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 10287 goto OTHER;
1d2b2268
GM
10288#endif
10289#endif
dfcf069d 10290 x_handle_property_notify (&event.xproperty);
1d2b2268 10291 goto OTHER;
dc6f92b8 10292
7a13e894 10293 case ReparentNotify:
19126e11 10294 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
10295 if (f)
10296 {
10297 int x, y;
7556890b 10298 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 10299 x_real_positions (f, &x, &y);
7556890b
RS
10300 f->output_data.x->left_pos = x;
10301 f->output_data.x->top_pos = y;
7a13e894
RS
10302 }
10303 break;
3bd330d4 10304
7a13e894 10305 case Expose:
19126e11 10306 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 10307 if (f)
dc6f92b8 10308 {
d0fa3e56
EZ
10309 x_check_fullscreen (f);
10310
7a13e894
RS
10311 if (f->async_visible == 0)
10312 {
10313 f->async_visible = 1;
10314 f->async_iconified = 0;
06c488fd 10315 f->output_data.x->has_been_visible = 1;
7a13e894
RS
10316 SET_FRAME_GARBAGED (f);
10317 }
10318 else
06a2c219
GM
10319 expose_frame (x_window_to_frame (dpyinfo,
10320 event.xexpose.window),
10321 event.xexpose.x, event.xexpose.y,
10322 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
10323 }
10324 else
7a13e894 10325 {
12949a7f
EZ
10326#ifndef USE_TOOLKIT_SCROLL_BARS
10327 struct scroll_bar *bar;
10328#endif
01f67d2c 10329#if defined USE_LUCID
c95fc5f1
GM
10330 /* Submenus of the Lucid menu bar aren't widgets
10331 themselves, so there's no way to dispatch events
10332 to them. Recognize this case separately. */
10333 {
10334 Widget widget
10335 = x_window_to_menu_bar (event.xexpose.window);
10336 if (widget)
10337 xlwmenu_redisplay (widget);
10338 }
01f67d2c
PJ
10339#endif /* USE_LUCID */
10340
06a2c219
GM
10341#ifdef USE_TOOLKIT_SCROLL_BARS
10342 /* Dispatch event to the widget. */
10343 goto OTHER;
10344#else /* not USE_TOOLKIT_SCROLL_BARS */
12949a7f 10345 bar = x_window_to_scroll_bar (event.xexpose.window);
58769bee 10346
7a13e894
RS
10347 if (bar)
10348 x_scroll_bar_expose (bar, &event);
3afe33e7 10349#ifdef USE_X_TOOLKIT
7a13e894
RS
10350 else
10351 goto OTHER;
3afe33e7 10352#endif /* USE_X_TOOLKIT */
06a2c219 10353#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10354 }
10355 break;
dc6f92b8 10356
7a13e894 10357 case GraphicsExpose: /* This occurs when an XCopyArea's
d624284c
PJ
10358 source area was obscured or not
10359 available. */
19126e11 10360 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
10361 if (f)
10362 {
06a2c219
GM
10363 expose_frame (f,
10364 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10365 event.xgraphicsexpose.width,
10366 event.xgraphicsexpose.height);
7a13e894 10367 }
3afe33e7 10368#ifdef USE_X_TOOLKIT
7a13e894
RS
10369 else
10370 goto OTHER;
3afe33e7 10371#endif /* USE_X_TOOLKIT */
7a13e894 10372 break;
dc6f92b8 10373
7a13e894 10374 case NoExpose: /* This occurs when an XCopyArea's
06a2c219 10375 source area was completely
d624284c 10376 available. */
7a13e894 10377 break;
dc6f92b8 10378
7a13e894 10379 case UnmapNotify:
06a2c219
GM
10380 /* Redo the mouse-highlight after the tooltip has gone. */
10381 if (event.xmap.window == tip_window)
10382 {
10383 tip_window = 0;
10384 redo_mouse_highlight ();
10385 }
10386
91ea2a7a 10387 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894 10388 if (f) /* F may no longer exist if
d624284c 10389 the frame was deleted. */
7a13e894
RS
10390 {
10391 /* While a frame is unmapped, display generation is
10392 disabled; you don't want to spend time updating a
10393 display that won't ever be seen. */
10394 f->async_visible = 0;
10395 /* We can't distinguish, from the event, whether the window
10396 has become iconified or invisible. So assume, if it
10397 was previously visible, than now it is iconified.
1aa6072f
RS
10398 But x_make_frame_invisible clears both
10399 the visible flag and the iconified flag;
10400 and that way, we know the window is not iconified now. */
7a13e894 10401 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
10402 {
10403 f->async_iconified = 1;
bddd097c 10404
1aa6072f
RS
10405 bufp->kind = iconify_event;
10406 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10407 bufp->arg = Qnil;
1aa6072f
RS
10408 bufp++;
10409 count++;
10410 numchars--;
10411 }
7a13e894 10412 }
7a13e894 10413 goto OTHER;
dc6f92b8 10414
7a13e894 10415 case MapNotify:
06a2c219
GM
10416 if (event.xmap.window == tip_window)
10417 /* The tooltip has been drawn already. Avoid
10418 the SET_FRAME_GARBAGED below. */
10419 goto OTHER;
10420
10421 /* We use x_top_window_to_frame because map events can
10422 come for sub-windows and they don't mean that the
10423 frame is visible. */
19126e11 10424 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
10425 if (f)
10426 {
10427 f->async_visible = 1;
10428 f->async_iconified = 0;
06c488fd 10429 f->output_data.x->has_been_visible = 1;
dc6f92b8 10430
7a13e894
RS
10431 /* wait_reading_process_input will notice this and update
10432 the frame's display structures. */
10433 SET_FRAME_GARBAGED (f);
bddd097c 10434
d806e720
RS
10435 if (f->iconified)
10436 {
10437 bufp->kind = deiconify_event;
10438 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10439 bufp->arg = Qnil;
d806e720
RS
10440 bufp++;
10441 count++;
10442 numchars--;
10443 }
e73ec6fa 10444 else if (! NILP (Vframe_list)
8e713be6 10445 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
10446 /* Force a redisplay sooner or later
10447 to update the frame titles
10448 in case this is the second frame. */
10449 record_asynch_buffer_change ();
7a13e894 10450 }
7a13e894 10451 goto OTHER;
dc6f92b8 10452
7a13e894 10453 case KeyPress:
19126e11 10454 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 10455
66301061
KS
10456 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10457 {
10458 dpyinfo->mouse_face_hidden = 1;
10459 clear_mouse_face (dpyinfo);
10460 }
10461
eccc05db 10462#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
10463 if (f == 0)
10464 {
2564ea1b
GM
10465 /* Scroll bars consume key events, but we want
10466 the keys to go to the scroll bar's frame. */
06a2c219
GM
10467 Widget widget = XtWindowToWidget (dpyinfo->display,
10468 event.xkey.window);
10469 if (widget && XmIsScrollBar (widget))
10470 {
10471 widget = XtParent (widget);
10472 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10473 }
10474 }
eccc05db 10475#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 10476
7a13e894
RS
10477 if (f != 0)
10478 {
10479 KeySym keysym, orig_keysym;
379b5ac0
KH
10480 /* al%imercury@uunet.uu.net says that making this 81
10481 instead of 80 fixed a bug whereby meta chars made
10482 his Emacs hang.
10483
10484 It seems that some version of XmbLookupString has
10485 a bug of not returning XBufferOverflow in
10486 status_return even if the input is too long to
10487 fit in 81 bytes. So, we must prepare sufficient
10488 bytes for copy_buffer. 513 bytes (256 chars for
6c4a22e6 10489 two-byte character set) seems to be a fairly good
379b5ac0
KH
10490 approximation. -- 2000.8.10 handa@etl.go.jp */
10491 unsigned char copy_buffer[513];
10492 unsigned char *copy_bufptr = copy_buffer;
10493 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 10494 int modifiers;
64bb1782 10495
7a13e894
RS
10496 event.xkey.state
10497 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10498 extra_keyboard_modifiers);
10499 modifiers = event.xkey.state;
3a2712f9 10500
7a13e894 10501 /* This will have to go some day... */
752a043f 10502
7a13e894
RS
10503 /* make_lispy_event turns chars into control chars.
10504 Don't do it here because XLookupString is too eager. */
10505 event.xkey.state &= ~ControlMask;
5d46f928
RS
10506 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10507 | dpyinfo->super_mod_mask
10508 | dpyinfo->hyper_mod_mask
10509 | dpyinfo->alt_mod_mask);
10510
1cf4a0d1
RS
10511 /* In case Meta is ComposeCharacter,
10512 clear its status. According to Markus Ehrnsperger
10513 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10514 this enables ComposeCharacter to work whether or
10515 not it is combined with Meta. */
10516 if (modifiers & dpyinfo->meta_mod_mask)
10517 bzero (&compose_status, sizeof (compose_status));
10518
6c183ba5
RS
10519#ifdef HAVE_X_I18N
10520 if (FRAME_XIC (f))
10521 {
f5d11644
GM
10522 Status status_return;
10523
6c183ba5 10524 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
10525 &event.xkey, copy_bufptr,
10526 copy_bufsiz, &keysym,
6c183ba5 10527 &status_return);
f5d11644
GM
10528 if (status_return == XBufferOverflow)
10529 {
10530 copy_bufsiz = nbytes + 1;
10531 copy_bufptr = (char *) alloca (copy_bufsiz);
10532 nbytes = XmbLookupString (FRAME_XIC (f),
10533 &event.xkey, copy_bufptr,
10534 copy_bufsiz, &keysym,
10535 &status_return);
10536 }
10537
1decb680
PE
10538 if (status_return == XLookupNone)
10539 break;
10540 else if (status_return == XLookupChars)
fdd9d55e
GM
10541 {
10542 keysym = NoSymbol;
10543 modifiers = 0;
10544 }
1decb680
PE
10545 else if (status_return != XLookupKeySym
10546 && status_return != XLookupBoth)
10547 abort ();
6c183ba5
RS
10548 }
10549 else
379b5ac0
KH
10550 nbytes = XLookupString (&event.xkey, copy_bufptr,
10551 copy_bufsiz, &keysym,
10552 &compose_status);
6c183ba5 10553#else
379b5ac0
KH
10554 nbytes = XLookupString (&event.xkey, copy_bufptr,
10555 copy_bufsiz, &keysym,
10556 &compose_status);
6c183ba5 10557#endif
dc6f92b8 10558
7a13e894 10559 orig_keysym = keysym;
55123275 10560
7a13e894
RS
10561 if (numchars > 1)
10562 {
10563 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10564 || keysym == XK_Delete
1097aea0 10565#ifdef XK_ISO_Left_Tab
441affdb 10566 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 10567#endif
852bff8f 10568 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
10569 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10570 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 10571#ifdef HPUX
7a13e894
RS
10572 /* This recognizes the "extended function keys".
10573 It seems there's no cleaner way.
10574 Test IsModifierKey to avoid handling mode_switch
10575 incorrectly. */
10576 || ((unsigned) (keysym) >= XK_Select
10577 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
10578#endif
10579#ifdef XK_dead_circumflex
7a13e894 10580 || orig_keysym == XK_dead_circumflex
69388238
RS
10581#endif
10582#ifdef XK_dead_grave
7a13e894 10583 || orig_keysym == XK_dead_grave
69388238
RS
10584#endif
10585#ifdef XK_dead_tilde
7a13e894 10586 || orig_keysym == XK_dead_tilde
69388238
RS
10587#endif
10588#ifdef XK_dead_diaeresis
7a13e894 10589 || orig_keysym == XK_dead_diaeresis
69388238
RS
10590#endif
10591#ifdef XK_dead_macron
7a13e894 10592 || orig_keysym == XK_dead_macron
69388238
RS
10593#endif
10594#ifdef XK_dead_degree
7a13e894 10595 || orig_keysym == XK_dead_degree
69388238
RS
10596#endif
10597#ifdef XK_dead_acute
7a13e894 10598 || orig_keysym == XK_dead_acute
69388238
RS
10599#endif
10600#ifdef XK_dead_cedilla
7a13e894 10601 || orig_keysym == XK_dead_cedilla
69388238
RS
10602#endif
10603#ifdef XK_dead_breve
7a13e894 10604 || orig_keysym == XK_dead_breve
69388238
RS
10605#endif
10606#ifdef XK_dead_ogonek
7a13e894 10607 || orig_keysym == XK_dead_ogonek
69388238
RS
10608#endif
10609#ifdef XK_dead_caron
7a13e894 10610 || orig_keysym == XK_dead_caron
69388238
RS
10611#endif
10612#ifdef XK_dead_doubleacute
7a13e894 10613 || orig_keysym == XK_dead_doubleacute
69388238
RS
10614#endif
10615#ifdef XK_dead_abovedot
7a13e894 10616 || orig_keysym == XK_dead_abovedot
c34790e0 10617#endif
7a13e894
RS
10618 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10619 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10620 /* Any "vendor-specific" key is ok. */
f0e299de
GM
10621 || (orig_keysym & (1 << 28))
10622 || (keysym != NoSymbol && nbytes == 0))
7a13e894 10623 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
10624#ifndef HAVE_X11R5
10625#ifdef XK_Mode_switch
7a13e894 10626 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
10627#endif
10628#ifdef XK_Num_Lock
7a13e894 10629 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
10630#endif
10631#endif /* not HAVE_X11R5 */
7a13e894 10632 ))
dc6f92b8 10633 {
10e6549c
RS
10634 if (temp_index == sizeof temp_buffer / sizeof (short))
10635 temp_index = 0;
7a13e894
RS
10636 temp_buffer[temp_index++] = keysym;
10637 bufp->kind = non_ascii_keystroke;
10638 bufp->code = keysym;
e0c1aef2 10639 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10640 bufp->arg = Qnil;
334208b7
RS
10641 bufp->modifiers
10642 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10643 modifiers);
1113d9db 10644 bufp->timestamp = event.xkey.time;
dc6f92b8 10645 bufp++;
7a13e894
RS
10646 count++;
10647 numchars--;
dc6f92b8 10648 }
7a13e894
RS
10649 else if (numchars > nbytes)
10650 {
10651 register int i;
379b5ac0 10652 register int c;
379b5ac0 10653 int nchars, len;
7a13e894
RS
10654
10655 for (i = 0; i < nbytes; i++)
10656 {
379b5ac0
KH
10657 if (temp_index == (sizeof temp_buffer
10658 / sizeof (short)))
7a13e894 10659 temp_index = 0;
379b5ac0
KH
10660 temp_buffer[temp_index++] = copy_bufptr[i];
10661 }
10662
10663 if (/* If the event is not from XIM, */
10664 event.xkey.keycode != 0
10665 /* or the current locale doesn't request
10666 decoding of the intup data, ... */
10667 || coding.type == coding_type_raw_text
10668 || coding.type == coding_type_no_conversion)
10669 {
10670 /* ... we can use the input data as is. */
10671 nchars = nbytes;
10672 }
10673 else
10674 {
10675 /* We have to decode the input data. */
10676 int require;
10677 unsigned char *p;
10678
10679 require = decoding_buffer_size (&coding, nbytes);
10680 p = (unsigned char *) alloca (require);
10681 coding.mode |= CODING_MODE_LAST_BLOCK;
10682 decode_coding (&coding, copy_bufptr, p,
10683 nbytes, require);
10684 nbytes = coding.produced;
10685 nchars = coding.produced_char;
10686 copy_bufptr = p;
10687 }
10688
10689 /* Convert the input data to a sequence of
10690 character events. */
10691 for (i = 0; i < nbytes; i += len)
10692 {
fee2aedc
GM
10693 if (nchars == nbytes)
10694 c = copy_bufptr[i], len = 1;
10695 else
10696 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10697 nbytes - i, len);
10698
379b5ac0
KH
10699 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10700 ? ascii_keystroke
10701 : multibyte_char_keystroke);
10702 bufp->code = c;
7a13e894 10703 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10704 bufp->arg = Qnil;
7a13e894
RS
10705 bufp->modifiers
10706 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10707 modifiers);
10708 bufp->timestamp = event.xkey.time;
10709 bufp++;
10710 }
10711
379b5ac0
KH
10712 count += nchars;
10713 numchars -= nchars;
1decb680
PE
10714
10715 if (keysym == NoSymbol)
10716 break;
7a13e894
RS
10717 }
10718 else
10719 abort ();
dc6f92b8 10720 }
10e6549c
RS
10721 else
10722 abort ();
dc6f92b8 10723 }
59ddecde
GM
10724#ifdef HAVE_X_I18N
10725 /* Don't dispatch this event since XtDispatchEvent calls
10726 XFilterEvent, and two calls in a row may freeze the
10727 client. */
10728 break;
10729#else
717ca130 10730 goto OTHER;
59ddecde 10731#endif
f451eb13 10732
f5d11644 10733 case KeyRelease:
59ddecde
GM
10734#ifdef HAVE_X_I18N
10735 /* Don't dispatch this event since XtDispatchEvent calls
10736 XFilterEvent, and two calls in a row may freeze the
10737 client. */
10738 break;
10739#else
f5d11644 10740 goto OTHER;
59ddecde 10741#endif
f5d11644 10742
7a13e894 10743 /* Here's a possible interpretation of the whole
06a2c219
GM
10744 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10745 you get a FocusIn event, you have to get a FocusOut
10746 event before you relinquish the focus. If you
10747 haven't received a FocusIn event, then a mere
10748 LeaveNotify is enough to free you. */
f451eb13 10749
7a13e894 10750 case EnterNotify:
06a2c219 10751 {
06a2c219
GM
10752 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10753
2c850e26 10754#if 0
582c60f8 10755 if (event.xcrossing.focus)
06a2c219
GM
10756 {
10757 /* Avoid nasty pop/raise loops. */
10758 if (f && (!(f->auto_raise)
10759 || !(f->auto_lower)
10760 || (event.xcrossing.time - enter_timestamp) > 500))
10761 {
10762 x_new_focus_frame (dpyinfo, f);
10763 enter_timestamp = event.xcrossing.time;
10764 }
10765 }
10766 else if (f == dpyinfo->x_focus_frame)
10767 x_new_focus_frame (dpyinfo, 0);
2c850e26
RS
10768#endif
10769
06a2c219
GM
10770 /* EnterNotify counts as mouse movement,
10771 so update things that depend on mouse position. */
2533c408 10772 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10773 note_mouse_movement (f, &event.xmotion);
10774 goto OTHER;
10775 }
dc6f92b8 10776
7a13e894 10777 case FocusIn:
19126e11 10778 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10779 if (event.xfocus.detail != NotifyPointer)
0f941935 10780 dpyinfo->x_focus_event_frame = f;
7a13e894 10781 if (f)
eb72635f
GM
10782 {
10783 x_new_focus_frame (dpyinfo, f);
10784
10785 /* Don't stop displaying the initial startup message
10786 for a switch-frame event we don't need. */
10787 if (GC_NILP (Vterminal_frame)
10788 && GC_CONSP (Vframe_list)
10789 && !GC_NILP (XCDR (Vframe_list)))
10790 {
10791 bufp->kind = FOCUS_IN_EVENT;
10792 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10793 bufp->arg = Qnil;
eb72635f
GM
10794 ++bufp, ++count, --numchars;
10795 }
10796 }
f9e24cb9 10797
6c183ba5
RS
10798#ifdef HAVE_X_I18N
10799 if (f && FRAME_XIC (f))
10800 XSetICFocus (FRAME_XIC (f));
10801#endif
10802
7a13e894 10803 goto OTHER;
10c5e63d 10804
7a13e894 10805 case LeaveNotify:
19126e11 10806 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10807 if (f)
10c5e63d 10808 {
7a13e894 10809 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10810 {
10811 /* If we move outside the frame, then we're
10812 certainly no longer on any text in the frame. */
10813 clear_mouse_face (dpyinfo);
10814 dpyinfo->mouse_face_mouse_frame = 0;
10815 }
10816
10817 /* Generate a nil HELP_EVENT to cancel a help-echo.
10818 Do it only if there's something to cancel.
10819 Otherwise, the startup message is cleared when
10820 the mouse leaves the frame. */
10821 if (any_help_event_p)
10822 {
be010514
GM
10823 Lisp_Object frame;
10824 int n;
10825
06a2c219 10826 XSETFRAME (frame, f);
82c5d67a 10827 help_echo = Qnil;
5ab2570d
GM
10828 n = gen_help_event (bufp, numchars,
10829 Qnil, frame, Qnil, Qnil, 0);
be010514 10830 bufp += n, count += n, numchars -= n;
06a2c219 10831 }
7a13e894 10832
2c850e26 10833#if 0
582c60f8 10834 if (event.xcrossing.focus)
0f941935 10835 x_mouse_leave (dpyinfo);
10c5e63d 10836 else
7a13e894 10837 {
0f941935
KH
10838 if (f == dpyinfo->x_focus_event_frame)
10839 dpyinfo->x_focus_event_frame = 0;
140d6643 10840 if (f == dpyinfo->x_focus_frame)
0f941935 10841 x_new_focus_frame (dpyinfo, 0);
7a13e894 10842 }
2c850e26 10843#endif
10c5e63d 10844 }
7a13e894 10845 goto OTHER;
dc6f92b8 10846
7a13e894 10847 case FocusOut:
19126e11 10848 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10849 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10850 && f == dpyinfo->x_focus_event_frame)
10851 dpyinfo->x_focus_event_frame = 0;
10852 if (f && f == dpyinfo->x_focus_frame)
10853 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10854
6c183ba5
RS
10855#ifdef HAVE_X_I18N
10856 if (f && FRAME_XIC (f))
10857 XUnsetICFocus (FRAME_XIC (f));
10858#endif
10859
7a13e894 10860 goto OTHER;
dc6f92b8 10861
7a13e894 10862 case MotionNotify:
dc6f92b8 10863 {
06a2c219 10864 previous_help_echo = help_echo;
7cea38bc 10865 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10866 help_echo_pos = -1;
06a2c219 10867
7a13e894
RS
10868 if (dpyinfo->grabbed && last_mouse_frame
10869 && FRAME_LIVE_P (last_mouse_frame))
10870 f = last_mouse_frame;
10871 else
19126e11 10872 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10873
66301061
KS
10874 if (dpyinfo->mouse_face_hidden)
10875 {
10876 dpyinfo->mouse_face_hidden = 0;
10877 clear_mouse_face (dpyinfo);
10878 }
10879
7a13e894
RS
10880 if (f)
10881 note_mouse_movement (f, &event.xmotion);
10882 else
10883 {
e88b3c50 10884#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10885 struct scroll_bar *bar
10886 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10887
7a13e894
RS
10888 if (bar)
10889 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10890#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10891
06a2c219
GM
10892 /* If we move outside the frame, then we're
10893 certainly no longer on any text in the frame. */
7a13e894
RS
10894 clear_mouse_face (dpyinfo);
10895 }
06a2c219
GM
10896
10897 /* If the contents of the global variable help_echo
10898 has changed, generate a HELP_EVENT. */
b7e80413
SM
10899 if (!NILP (help_echo)
10900 || !NILP (previous_help_echo))
06a2c219
GM
10901 {
10902 Lisp_Object frame;
be010514 10903 int n;
06a2c219
GM
10904
10905 if (f)
10906 XSETFRAME (frame, f);
10907 else
10908 frame = Qnil;
10909
10910 any_help_event_p = 1;
5ab2570d 10911 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10912 help_echo_window, help_echo_object,
10913 help_echo_pos);
be010514 10914 bufp += n, count += n, numchars -= n;
06a2c219
GM
10915 }
10916
10917 goto OTHER;
dc6f92b8 10918 }
dc6f92b8 10919
7a13e894 10920 case ConfigureNotify:
9829ddba
RS
10921 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10922 if (f)
af395ec1 10923 {
7a4bce14 10924#ifndef USE_X_TOOLKIT
d0fa3e56
EZ
10925 /* If there is a pending resize for fullscreen, don't
10926 do this one, the right one will come later.
10927 The toolkit version doesn't seem to need this, but we
7a4bce14 10928 need to reset it below. */
d0fa3e56
EZ
10929 int dont_resize =
10930 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10931 && FRAME_NEW_WIDTH (f) != 0);
bf1b7b30
KH
10932 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10933 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
d0fa3e56
EZ
10934 if (dont_resize)
10935 goto OTHER;
5c187dee 10936
2d7fc7e8
RS
10937 /* In the toolkit version, change_frame_size
10938 is called by the code that handles resizing
10939 of the EmacsFrame widget. */
7a13e894 10940
7a13e894
RS
10941 /* Even if the number of character rows and columns has
10942 not changed, the font size may have changed, so we need
10943 to check the pixel dimensions as well. */
10944 if (columns != f->width
10945 || rows != f->height
7556890b
RS
10946 || event.xconfigure.width != f->output_data.x->pixel_width
10947 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10948 {
7d1e984f 10949 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10950 SET_FRAME_GARBAGED (f);
e687d06e 10951 cancel_mouse_face (f);
7a13e894 10952 }
2d7fc7e8 10953#endif
af395ec1 10954
7556890b
RS
10955 f->output_data.x->pixel_width = event.xconfigure.width;
10956 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10957
10958 /* What we have now is the position of Emacs's own window.
10959 Convert that to the position of the window manager window. */
dcb07ae9
RS
10960 x_real_positions (f, &f->output_data.x->left_pos,
10961 &f->output_data.x->top_pos);
10962
d0fa3e56
EZ
10963 x_check_fullscreen_move(f);
10964 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10965 f->output_data.x->want_fullscreen &=
10966 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
f5d11644
GM
10967#ifdef HAVE_X_I18N
10968 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10969 xic_set_statusarea (f);
10970#endif
10971
dcb07ae9
RS
10972 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10973 {
10974 /* Since the WM decorations come below top_pos now,
10975 we must put them below top_pos in the future. */
10976 f->output_data.x->win_gravity = NorthWestGravity;
10977 x_wm_set_size_hint (f, (long) 0, 0);
10978 }
8f08dc93
KH
10979#ifdef USE_MOTIF
10980 /* Some window managers pass (0,0) as the location of
10981 the window, and the Motif event handler stores it
10982 in the emacs widget, which messes up Motif menus. */
10983 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10984 {
10985 event.xconfigure.x = f->output_data.x->widget->core.x;
10986 event.xconfigure.y = f->output_data.x->widget->core.y;
10987 }
06a2c219 10988#endif /* USE_MOTIF */
7a13e894 10989 }
2d7fc7e8 10990 goto OTHER;
dc6f92b8 10991
7a13e894
RS
10992 case ButtonPress:
10993 case ButtonRelease:
10994 {
10995 /* If we decide we want to generate an event to be seen
10996 by the rest of Emacs, we put it here. */
10997 struct input_event emacs_event;
9ea173e8 10998 int tool_bar_p = 0;
06a2c219 10999
7a13e894 11000 emacs_event.kind = no_event;
7a13e894 11001 bzero (&compose_status, sizeof (compose_status));
9b07615b 11002
06a2c219
GM
11003 if (dpyinfo->grabbed
11004 && last_mouse_frame
9f67f20b
RS
11005 && FRAME_LIVE_P (last_mouse_frame))
11006 f = last_mouse_frame;
11007 else
2224b905 11008 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 11009
06a2c219
GM
11010 if (f)
11011 {
9ea173e8
GM
11012 /* Is this in the tool-bar? */
11013 if (WINDOWP (f->tool_bar_window)
11014 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
11015 {
11016 Lisp_Object window;
11017 int p, x, y;
11018
11019 x = event.xbutton.x;
11020 y = event.xbutton.y;
11021
11022 /* Set x and y. */
11023 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 11024 if (EQ (window, f->tool_bar_window))
06a2c219 11025 {
9ea173e8
GM
11026 x_handle_tool_bar_click (f, &event.xbutton);
11027 tool_bar_p = 1;
06a2c219
GM
11028 }
11029 }
11030
9ea173e8 11031 if (!tool_bar_p)
06a2c219
GM
11032 if (!dpyinfo->x_focus_frame
11033 || f == dpyinfo->x_focus_frame)
11034 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
11035 }
11036 else
11037 {
06a2c219 11038#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
11039 struct scroll_bar *bar
11040 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 11041
7a13e894
RS
11042 if (bar)
11043 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 11044#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
11045 }
11046
11047 if (event.type == ButtonPress)
11048 {
11049 dpyinfo->grabbed |= (1 << event.xbutton.button);
11050 last_mouse_frame = f;
edad46f6
KH
11051 /* Ignore any mouse motion that happened
11052 before this event; any subsequent mouse-movement
11053 Emacs events should reflect only motion after
11054 the ButtonPress. */
a00e91cd
KH
11055 if (f != 0)
11056 f->mouse_moved = 0;
06a2c219 11057
9ea173e8
GM
11058 if (!tool_bar_p)
11059 last_tool_bar_item = -1;
7a13e894 11060 }
3afe33e7
RS
11061 else
11062 {
7a13e894 11063 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 11064 }
23faf38f 11065
7a13e894
RS
11066 if (numchars >= 1 && emacs_event.kind != no_event)
11067 {
11068 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11069 bufp++;
11070 count++;
11071 numchars--;
11072 }
3afe33e7
RS
11073
11074#ifdef USE_X_TOOLKIT
2224b905
RS
11075 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11076 /* For a down-event in the menu bar,
11077 don't pass it to Xt right now.
11078 Instead, save it away
11079 and we will pass it to Xt from kbd_buffer_get_event.
11080 That way, we can run some Lisp code first. */
91375f8f
RS
11081 if (f && event.type == ButtonPress
11082 /* Verify the event is really within the menu bar
11083 and not just sent to it due to grabbing. */
11084 && event.xbutton.x >= 0
11085 && event.xbutton.x < f->output_data.x->pixel_width
11086 && event.xbutton.y >= 0
11087 && event.xbutton.y < f->output_data.x->menubar_height
11088 && event.xbutton.same_screen)
2224b905 11089 {
8805890a 11090 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
11091 XSETFRAME (last_mouse_press_frame, f);
11092 }
11093 else if (event.type == ButtonPress)
11094 {
11095 last_mouse_press_frame = Qnil;
30e671c3 11096 goto OTHER;
ce89ef46 11097 }
06a2c219 11098
2237cac9
RS
11099#ifdef USE_MOTIF /* This should do not harm for Lucid,
11100 but I am trying to be cautious. */
ce89ef46
RS
11101 else if (event.type == ButtonRelease)
11102 {
2237cac9 11103 if (!NILP (last_mouse_press_frame))
f10ded1c 11104 {
2237cac9
RS
11105 f = XFRAME (last_mouse_press_frame);
11106 if (f->output_data.x)
06a2c219 11107 SET_SAVED_BUTTON_EVENT;
f10ded1c 11108 }
06a2c219 11109 else
30e671c3 11110 goto OTHER;
2224b905 11111 }
2237cac9 11112#endif /* USE_MOTIF */
2224b905
RS
11113 else
11114 goto OTHER;
3afe33e7 11115#endif /* USE_X_TOOLKIT */
7a13e894
RS
11116 }
11117 break;
dc6f92b8 11118
7a13e894 11119 case CirculateNotify:
06a2c219
GM
11120 goto OTHER;
11121
7a13e894 11122 case CirculateRequest:
06a2c219
GM
11123 goto OTHER;
11124
11125 case VisibilityNotify:
11126 goto OTHER;
dc6f92b8 11127
7a13e894
RS
11128 case MappingNotify:
11129 /* Someone has changed the keyboard mapping - update the
11130 local cache. */
11131 switch (event.xmapping.request)
11132 {
11133 case MappingModifier:
11134 x_find_modifier_meanings (dpyinfo);
11135 /* This is meant to fall through. */
11136 case MappingKeyboard:
11137 XRefreshKeyboardMapping (&event.xmapping);
11138 }
7a13e894 11139 goto OTHER;
dc6f92b8 11140
7a13e894 11141 default:
7a13e894 11142 OTHER:
717ca130 11143#ifdef USE_X_TOOLKIT
7a13e894
RS
11144 BLOCK_INPUT;
11145 XtDispatchEvent (&event);
11146 UNBLOCK_INPUT;
3afe33e7 11147#endif /* USE_X_TOOLKIT */
7a13e894
RS
11148 break;
11149 }
dc6f92b8
JB
11150 }
11151 }
11152
06a2c219
GM
11153 out:;
11154
9a5196d0
RS
11155 /* On some systems, an X bug causes Emacs to get no more events
11156 when the window is destroyed. Detect that. (1994.) */
58769bee 11157 if (! event_found)
ef2a22d0 11158 {
ef2a22d0
RS
11159 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11160 One XNOOP in 100 loops will make Emacs terminate.
11161 B. Bretthauer, 1994 */
11162 x_noop_count++;
58769bee 11163 if (x_noop_count >= 100)
ef2a22d0
RS
11164 {
11165 x_noop_count=0;
2224b905
RS
11166
11167 if (next_noop_dpyinfo == 0)
11168 next_noop_dpyinfo = x_display_list;
11169
11170 XNoOp (next_noop_dpyinfo->display);
11171
11172 /* Each time we get here, cycle through the displays now open. */
11173 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
11174 }
11175 }
502add23 11176
06a2c219 11177 /* If the focus was just given to an auto-raising frame,
0134a210 11178 raise it now. */
7a13e894 11179 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
11180 if (pending_autoraise_frame)
11181 {
11182 x_raise_frame (pending_autoraise_frame);
11183 pending_autoraise_frame = 0;
11184 }
0134a210 11185
dc6f92b8 11186 UNBLOCK_INPUT;
bde5503b 11187 --handling_signal;
dc6f92b8
JB
11188 return count;
11189}
06a2c219
GM
11190
11191
11192
dc6f92b8 11193\f
06a2c219
GM
11194/***********************************************************************
11195 Text Cursor
11196 ***********************************************************************/
11197
60626bab
GM
11198/* Notice when the text cursor of window W has been completely
11199 overwritten by a drawing operation that outputs glyphs in AREA
11200 starting at X0 and ending at X1 in the line starting at Y0 and
11201 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11202 the rest of the line after X0 has been written. Y coordinates
11203 are window-relative. */
06a2c219
GM
11204
11205static void
60626bab 11206notice_overwritten_cursor (w, area, x0, x1, y0, y1)
06a2c219 11207 struct window *w;
60626bab
GM
11208 enum glyph_row_area area;
11209 int x0, y0, x1, y1;
06a2c219 11210{
60626bab 11211 if (area == TEXT_AREA
f0a48a01 11212 && w->phys_cursor_on_p
60626bab
GM
11213 && y0 <= w->phys_cursor.y
11214 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11215 && x0 <= w->phys_cursor.x
11216 && (x1 < 0 || x1 > w->phys_cursor.x))
f0a48a01 11217 w->phys_cursor_on_p = 0;
06a2c219 11218}
f451eb13
JB
11219
11220
06a2c219
GM
11221/* Set clipping for output in glyph row ROW. W is the window in which
11222 we operate. GC is the graphics context to set clipping in.
11223 WHOLE_LINE_P non-zero means include the areas used for truncation
11224 mark display and alike in the clipping rectangle.
11225
11226 ROW may be a text row or, e.g., a mode line. Text rows must be
11227 clipped to the interior of the window dedicated to text display,
11228 mode lines must be clipped to the whole window. */
dc6f92b8
JB
11229
11230static void
06a2c219
GM
11231x_clip_to_row (w, row, gc, whole_line_p)
11232 struct window *w;
11233 struct glyph_row *row;
11234 GC gc;
11235 int whole_line_p;
dc6f92b8 11236{
06a2c219
GM
11237 struct frame *f = XFRAME (WINDOW_FRAME (w));
11238 XRectangle clip_rect;
11239 int window_x, window_y, window_width, window_height;
dc6f92b8 11240
06a2c219 11241 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 11242
06a2c219
GM
11243 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11244 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11245 clip_rect.y = max (clip_rect.y, window_y);
11246 clip_rect.width = window_width;
11247 clip_rect.height = row->visible_height;
5c1aae96 11248
06a2c219
GM
11249 /* If clipping to the whole line, including trunc marks, extend
11250 the rectangle to the left and increase its width. */
11251 if (whole_line_p)
11252 {
3f332ef3
KS
11253 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11254 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
06a2c219 11255 }
5c1aae96 11256
06a2c219 11257 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
11258}
11259
06a2c219
GM
11260
11261/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
11262
11263static void
06a2c219
GM
11264x_draw_hollow_cursor (w, row)
11265 struct window *w;
11266 struct glyph_row *row;
dc6f92b8 11267{
06a2c219
GM
11268 struct frame *f = XFRAME (WINDOW_FRAME (w));
11269 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11270 Display *dpy = FRAME_X_DISPLAY (f);
11271 int x, y, wd, h;
11272 XGCValues xgcv;
11273 struct glyph *cursor_glyph;
11274 GC gc;
11275
11276 /* Compute frame-relative coordinates from window-relative
11277 coordinates. */
11278 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11279 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11280 + row->ascent - w->phys_cursor_ascent);
11281 h = row->height - 1;
11282
11283 /* Get the glyph the cursor is on. If we can't tell because
11284 the current matrix is invalid or such, give up. */
11285 cursor_glyph = get_phys_cursor_glyph (w);
11286 if (cursor_glyph == NULL)
dc6f92b8
JB
11287 return;
11288
06a2c219
GM
11289 /* Compute the width of the rectangle to draw. If on a stretch
11290 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11291 rectangle as wide as the glyph, but use a canonical character
11292 width instead. */
11293 wd = cursor_glyph->pixel_width - 1;
11294 if (cursor_glyph->type == STRETCH_GLYPH
11295 && !x_stretch_cursor_p)
11296 wd = min (CANON_X_UNIT (f), wd);
11297
11298 /* The foreground of cursor_gc is typically the same as the normal
11299 background color, which can cause the cursor box to be invisible. */
11300 xgcv.foreground = f->output_data.x->cursor_pixel;
11301 if (dpyinfo->scratch_cursor_gc)
11302 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11303 else
11304 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11305 GCForeground, &xgcv);
11306 gc = dpyinfo->scratch_cursor_gc;
11307
11308 /* Set clipping, draw the rectangle, and reset clipping again. */
11309 x_clip_to_row (w, row, gc, 0);
11310 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11311 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
11312}
11313
06a2c219
GM
11314
11315/* Draw a bar cursor on window W in glyph row ROW.
11316
11317 Implementation note: One would like to draw a bar cursor with an
11318 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11319 Unfortunately, I didn't find a font yet that has this property set.
11320 --gerd. */
dc6f92b8
JB
11321
11322static void
f02d8aa0 11323x_draw_bar_cursor (w, row, width)
06a2c219
GM
11324 struct window *w;
11325 struct glyph_row *row;
f02d8aa0 11326 int width;
dc6f92b8 11327{
92f424df
GM
11328 struct frame *f = XFRAME (w->frame);
11329 struct glyph *cursor_glyph;
06a2c219 11330
92f424df
GM
11331 /* If cursor is out of bounds, don't draw garbage. This can happen
11332 in mini-buffer windows when switching between echo area glyphs
11333 and mini-buffer. */
11334 cursor_glyph = get_phys_cursor_glyph (w);
11335 if (cursor_glyph == NULL)
11336 return;
06a2c219 11337
92f424df
GM
11338 /* If on an image, draw like a normal cursor. That's usually better
11339 visible than drawing a bar, esp. if the image is large so that
11340 the bar might not be in the window. */
11341 if (cursor_glyph->type == IMAGE_GLYPH)
11342 {
11343 struct glyph_row *row;
11344 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11345 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11346 }
11347 else
11348 {
34e5d0af
GM
11349 Display *dpy = FRAME_X_DISPLAY (f);
11350 Window window = FRAME_X_WINDOW (f);
11351 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11352 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11353 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11354 XGCValues xgcv;
11355
11356 /* If the glyph's background equals the color we normally draw
11357 the bar cursor in, the bar cursor in its normal color is
11358 invisible. Use the glyph's foreground color instead in this
11359 case, on the assumption that the glyph's colors are chosen so
11360 that the glyph is legible. */
11361 if (face->background == f->output_data.x->cursor_pixel)
11362 xgcv.background = xgcv.foreground = face->foreground;
11363 else
11364 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 11365 xgcv.graphics_exposures = 0;
92f424df 11366
06a2c219
GM
11367 if (gc)
11368 XChangeGC (dpy, gc, mask, &xgcv);
11369 else
11370 {
11371 gc = XCreateGC (dpy, window, mask, &xgcv);
11372 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11373 }
92f424df 11374
f02d8aa0
GM
11375 if (width < 0)
11376 width = f->output_data.x->cursor_width;
34e5d0af 11377 width = min (cursor_glyph->pixel_width, width);
92f424df 11378
06a2c219
GM
11379 x_clip_to_row (w, row, gc, 0);
11380 XFillRectangle (dpy, window, gc,
34e5d0af 11381 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
06a2c219 11382 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
34e5d0af 11383 width, row->height);
06a2c219
GM
11384 XSetClipMask (dpy, gc, None);
11385 }
dc6f92b8
JB
11386}
11387
06a2c219
GM
11388
11389/* Clear the cursor of window W to background color, and mark the
11390 cursor as not shown. This is used when the text where the cursor
11391 is is about to be rewritten. */
11392
dc6f92b8 11393static void
06a2c219
GM
11394x_clear_cursor (w)
11395 struct window *w;
dc6f92b8 11396{
06a2c219
GM
11397 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11398 x_update_window_cursor (w, 0);
11399}
90e65f07 11400
dbc4e1c1 11401
06a2c219
GM
11402/* Draw the cursor glyph of window W in glyph row ROW. See the
11403 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 11404
06a2c219
GM
11405static void
11406x_draw_phys_cursor_glyph (w, row, hl)
11407 struct window *w;
11408 struct glyph_row *row;
11409 enum draw_glyphs_face hl;
11410{
11411 /* If cursor hpos is out of bounds, don't draw garbage. This can
11412 happen in mini-buffer windows when switching between echo area
11413 glyphs and mini-buffer. */
11414 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e 11415 {
f0a48a01
GM
11416 int on_p = w->phys_cursor_on_p;
11417
66ac4b0e
GM
11418 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11419 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
f0a48a01
GM
11420 hl, 0);
11421 w->phys_cursor_on_p = on_p;
66ac4b0e
GM
11422
11423 /* When we erase the cursor, and ROW is overlapped by other
11424 rows, make sure that these overlapping parts of other rows
11425 are redrawn. */
11426 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11427 {
11428 if (row > w->current_matrix->rows
11429 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11430 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11431
11432 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11433 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11434 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11435 }
11436 }
06a2c219 11437}
dbc4e1c1 11438
eea6af04 11439
06a2c219 11440/* Erase the image of a cursor of window W from the screen. */
eea6af04 11441
06a2c219
GM
11442static void
11443x_erase_phys_cursor (w)
11444 struct window *w;
11445{
11446 struct frame *f = XFRAME (w->frame);
11447 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11448 int hpos = w->phys_cursor.hpos;
11449 int vpos = w->phys_cursor.vpos;
11450 int mouse_face_here_p = 0;
11451 struct glyph_matrix *active_glyphs = w->current_matrix;
11452 struct glyph_row *cursor_row;
11453 struct glyph *cursor_glyph;
11454 enum draw_glyphs_face hl;
11455
11456 /* No cursor displayed or row invalidated => nothing to do on the
11457 screen. */
11458 if (w->phys_cursor_type == NO_CURSOR)
11459 goto mark_cursor_off;
11460
11461 /* VPOS >= active_glyphs->nrows means that window has been resized.
11462 Don't bother to erase the cursor. */
11463 if (vpos >= active_glyphs->nrows)
11464 goto mark_cursor_off;
11465
11466 /* If row containing cursor is marked invalid, there is nothing we
11467 can do. */
11468 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11469 if (!cursor_row->enabled_p)
11470 goto mark_cursor_off;
11471
a2c6de8e
KS
11472 /* If row is completely invisible, don't attempt to delete a cursor which
11473 isn't there. This can happen if cursor is at top of a window, and
11474 we switch to a buffer with a header line in that window. */
11475 if (cursor_row->visible_height <= 0)
11476 goto mark_cursor_off;
11477
06a2c219
GM
11478 /* This can happen when the new row is shorter than the old one.
11479 In this case, either x_draw_glyphs or clear_end_of_line
11480 should have cleared the cursor. Note that we wouldn't be
11481 able to erase the cursor in this case because we don't have a
11482 cursor glyph at hand. */
11483 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11484 goto mark_cursor_off;
11485
11486 /* If the cursor is in the mouse face area, redisplay that when
11487 we clear the cursor. */
8801a864
KR
11488 if (! NILP (dpyinfo->mouse_face_window)
11489 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
11490 && (vpos > dpyinfo->mouse_face_beg_row
11491 || (vpos == dpyinfo->mouse_face_beg_row
11492 && hpos >= dpyinfo->mouse_face_beg_col))
11493 && (vpos < dpyinfo->mouse_face_end_row
11494 || (vpos == dpyinfo->mouse_face_end_row
11495 && hpos < dpyinfo->mouse_face_end_col))
11496 /* Don't redraw the cursor's spot in mouse face if it is at the
11497 end of a line (on a newline). The cursor appears there, but
11498 mouse highlighting does not. */
11499 && cursor_row->used[TEXT_AREA] > hpos)
11500 mouse_face_here_p = 1;
11501
11502 /* Maybe clear the display under the cursor. */
11503 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11504 {
11505 int x;
045dee35 11506 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 11507
06a2c219
GM
11508 cursor_glyph = get_phys_cursor_glyph (w);
11509 if (cursor_glyph == NULL)
11510 goto mark_cursor_off;
dbc4e1c1 11511
e0300d33 11512 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
06a2c219 11513
c5e6e06b
GM
11514 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11515 x,
11516 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11517 cursor_row->y)),
11518 cursor_glyph->pixel_width,
11519 cursor_row->visible_height,
11520 False);
dbc4e1c1 11521 }
06a2c219
GM
11522
11523 /* Erase the cursor by redrawing the character underneath it. */
11524 if (mouse_face_here_p)
11525 hl = DRAW_MOUSE_FACE;
06a2c219
GM
11526 else
11527 hl = DRAW_NORMAL_TEXT;
11528 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 11529
06a2c219
GM
11530 mark_cursor_off:
11531 w->phys_cursor_on_p = 0;
11532 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
11533}
11534
11535
b7f83f9e
GM
11536/* Non-zero if physical cursor of window W is within mouse face. */
11537
11538static int
11539cursor_in_mouse_face_p (w)
11540 struct window *w;
11541{
11542 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11543 int in_mouse_face = 0;
11544
11545 if (WINDOWP (dpyinfo->mouse_face_window)
11546 && XWINDOW (dpyinfo->mouse_face_window) == w)
11547 {
11548 int hpos = w->phys_cursor.hpos;
11549 int vpos = w->phys_cursor.vpos;
11550
11551 if (vpos >= dpyinfo->mouse_face_beg_row
11552 && vpos <= dpyinfo->mouse_face_end_row
11553 && (vpos > dpyinfo->mouse_face_beg_row
11554 || hpos >= dpyinfo->mouse_face_beg_col)
11555 && (vpos < dpyinfo->mouse_face_end_row
11556 || hpos < dpyinfo->mouse_face_end_col
11557 || dpyinfo->mouse_face_past_end))
11558 in_mouse_face = 1;
11559 }
11560
11561 return in_mouse_face;
11562}
11563
11564
06a2c219
GM
11565/* Display or clear cursor of window W. If ON is zero, clear the
11566 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11567 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 11568
06a2c219
GM
11569void
11570x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11571 struct window *w;
11572 int on, hpos, vpos, x, y;
dbc4e1c1 11573{
06a2c219
GM
11574 struct frame *f = XFRAME (w->frame);
11575 int new_cursor_type;
f02d8aa0 11576 int new_cursor_width;
06a2c219
GM
11577 struct glyph_matrix *current_glyphs;
11578 struct glyph_row *glyph_row;
11579 struct glyph *glyph;
34368a22 11580 int cursor_non_selected;
dbc4e1c1 11581
49d838ea 11582 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
11583 windows and frames; in the latter case, the frame or window may
11584 be in the midst of changing its size, and x and y may be off the
11585 window. */
11586 if (! FRAME_VISIBLE_P (f)
11587 || FRAME_GARBAGED_P (f)
11588 || vpos >= w->current_matrix->nrows
11589 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
11590 return;
11591
11592 /* If cursor is off and we want it off, return quickly. */
06a2c219 11593 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
11594 return;
11595
06a2c219
GM
11596 current_glyphs = w->current_matrix;
11597 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11598 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11599
11600 /* If cursor row is not enabled, we don't really know where to
11601 display the cursor. */
11602 if (!glyph_row->enabled_p)
11603 {
11604 w->phys_cursor_on_p = 0;
11605 return;
11606 }
11607
11608 xassert (interrupt_input_blocked);
11609
11610 /* Set new_cursor_type to the cursor we want to be displayed. In a
11611 mini-buffer window, we want the cursor only to appear if we are
11612 reading input from this window. For the selected window, we want
11613 the cursor type given by the frame parameter. If explicitly
11614 marked off, draw no cursor. In all other cases, we want a hollow
11615 box cursor. */
34368a22
RS
11616 cursor_non_selected
11617 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11618 w->buffer));
f02d8aa0 11619 new_cursor_width = -1;
9b4a7047
GM
11620 if (cursor_in_echo_area
11621 && FRAME_HAS_MINIBUF_P (f)
11622 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 11623 {
9b4a7047
GM
11624 if (w == XWINDOW (echo_area_window))
11625 new_cursor_type = FRAME_DESIRED_CURSOR (f);
34368a22 11626 else if (cursor_non_selected)
06a2c219 11627 new_cursor_type = HOLLOW_BOX_CURSOR;
9a7bdceb
GM
11628 else
11629 new_cursor_type = NO_CURSOR;
06a2c219 11630 }
06a2c219 11631 else
9b4a7047 11632 {
7a58ab59
GM
11633 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11634 || w != XWINDOW (f->selected_window))
9b4a7047 11635 {
2c850e26 11636 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
34368a22 11637 || !cursor_non_selected
5cefa566 11638 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
11639 new_cursor_type = NO_CURSOR;
11640 else
11641 new_cursor_type = HOLLOW_BOX_CURSOR;
11642 }
9b4a7047 11643 else
f02d8aa0
GM
11644 {
11645 struct buffer *b = XBUFFER (w->buffer);
11646
11647 if (EQ (b->cursor_type, Qt))
11648 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11649 else
11650 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11651 &new_cursor_width);
e1429afe
KS
11652 if (w->cursor_off_p)
11653 {
11654 if (new_cursor_type == FILLED_BOX_CURSOR)
11655 new_cursor_type = HOLLOW_BOX_CURSOR;
11656 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
11657 new_cursor_width = 1;
11658 else
11659 new_cursor_type = NO_CURSOR;
11660 }
f02d8aa0 11661 }
9b4a7047 11662 }
06a2c219
GM
11663
11664 /* If cursor is currently being shown and we don't want it to be or
11665 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 11666 erase it. */
06a2c219 11667 if (w->phys_cursor_on_p
dc6f92b8 11668 && (!on
06a2c219
GM
11669 || w->phys_cursor.x != x
11670 || w->phys_cursor.y != y
e1429afe
KS
11671 || new_cursor_type != w->phys_cursor_type
11672 || (new_cursor_type == BAR_CURSOR
11673 && new_cursor_width != w->phys_cursor_width)))
06a2c219
GM
11674 x_erase_phys_cursor (w);
11675
11676 /* If the cursor is now invisible and we want it to be visible,
11677 display it. */
11678 if (on && !w->phys_cursor_on_p)
11679 {
11680 w->phys_cursor_ascent = glyph_row->ascent;
11681 w->phys_cursor_height = glyph_row->height;
11682
11683 /* Set phys_cursor_.* before x_draw_.* is called because some
11684 of them may need the information. */
11685 w->phys_cursor.x = x;
11686 w->phys_cursor.y = glyph_row->y;
11687 w->phys_cursor.hpos = hpos;
11688 w->phys_cursor.vpos = vpos;
11689 w->phys_cursor_type = new_cursor_type;
e1429afe 11690 w->phys_cursor_width = new_cursor_width;
06a2c219
GM
11691 w->phys_cursor_on_p = 1;
11692
11693 switch (new_cursor_type)
dc6f92b8 11694 {
06a2c219
GM
11695 case HOLLOW_BOX_CURSOR:
11696 x_draw_hollow_cursor (w, glyph_row);
11697 break;
11698
11699 case FILLED_BOX_CURSOR:
11700 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11701 break;
11702
11703 case BAR_CURSOR:
f02d8aa0 11704 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
11705 break;
11706
11707 case NO_CURSOR:
11708 break;
dc6f92b8 11709
06a2c219
GM
11710 default:
11711 abort ();
11712 }
59ddecde
GM
11713
11714#ifdef HAVE_X_I18N
11715 if (w == XWINDOW (f->selected_window))
11716 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11717 xic_set_preeditarea (w, x, y);
11718#endif
dc6f92b8
JB
11719 }
11720
06a2c219 11721#ifndef XFlush
f676886a 11722 if (updating_frame != f)
334208b7 11723 XFlush (FRAME_X_DISPLAY (f));
06a2c219 11724#endif
dc6f92b8
JB
11725}
11726
06a2c219
GM
11727
11728/* Display the cursor on window W, or clear it. X and Y are window
11729 relative pixel coordinates. HPOS and VPOS are glyph matrix
11730 positions. If W is not the selected window, display a hollow
11731 cursor. ON non-zero means display the cursor at X, Y which
11732 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11733
dfcf069d 11734void
06a2c219
GM
11735x_display_cursor (w, on, hpos, vpos, x, y)
11736 struct window *w;
11737 int on, hpos, vpos, x, y;
dc6f92b8 11738{
f94397b5 11739 BLOCK_INPUT;
06a2c219 11740 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11741 UNBLOCK_INPUT;
11742}
11743
06a2c219
GM
11744
11745/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11746 Don't change the cursor's position. */
11747
dfcf069d 11748void
06a2c219 11749x_update_cursor (f, on_p)
5d46f928 11750 struct frame *f;
5d46f928 11751{
06a2c219
GM
11752 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11753}
11754
11755
11756/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11757 in the window tree rooted at W. */
11758
11759static void
11760x_update_cursor_in_window_tree (w, on_p)
11761 struct window *w;
11762 int on_p;
11763{
11764 while (w)
11765 {
11766 if (!NILP (w->hchild))
11767 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11768 else if (!NILP (w->vchild))
11769 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11770 else
11771 x_update_window_cursor (w, on_p);
11772
11773 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11774 }
11775}
5d46f928 11776
f94397b5 11777
06a2c219
GM
11778/* Switch the display of W's cursor on or off, according to the value
11779 of ON. */
11780
11781static void
11782x_update_window_cursor (w, on)
11783 struct window *w;
11784 int on;
11785{
16b5d424
GM
11786 /* Don't update cursor in windows whose frame is in the process
11787 of being deleted. */
11788 if (w->current_matrix)
11789 {
11790 BLOCK_INPUT;
11791 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11792 w->phys_cursor.x, w->phys_cursor.y);
11793 UNBLOCK_INPUT;
11794 }
dc6f92b8 11795}
06a2c219
GM
11796
11797
11798
dc6f92b8
JB
11799\f
11800/* Icons. */
11801
dbc4e1c1 11802/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11803
11804int
990ba854 11805x_bitmap_icon (f, file)
f676886a 11806 struct frame *f;
990ba854 11807 Lisp_Object file;
dc6f92b8 11808{
06a2c219 11809 int bitmap_id;
dc6f92b8 11810
c118dd06 11811 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11812 return 1;
11813
990ba854 11814 /* Free up our existing icon bitmap if any. */
7556890b
RS
11815 if (f->output_data.x->icon_bitmap > 0)
11816 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11817 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11818
11819 if (STRINGP (file))
7f2ae036
RS
11820 bitmap_id = x_create_bitmap_from_file (f, file);
11821 else
11822 {
990ba854 11823 /* Create the GNU bitmap if necessary. */
5bf01b68 11824 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11825 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11826 = x_create_bitmap_from_data (f, gnu_bits,
11827 gnu_width, gnu_height);
990ba854
RS
11828
11829 /* The first time we create the GNU bitmap,
06a2c219 11830 this increments the ref-count one extra time.
990ba854
RS
11831 As a result, the GNU bitmap is never freed.
11832 That way, we don't have to worry about allocating it again. */
334208b7 11833 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11834
334208b7 11835 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11836 }
11837
11838 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11839 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11840
11841 return 0;
11842}
11843
11844
1be2d067
KH
11845/* Make the x-window of frame F use a rectangle with text.
11846 Use ICON_NAME as the text. */
dc6f92b8
JB
11847
11848int
f676886a
JB
11849x_text_icon (f, icon_name)
11850 struct frame *f;
dc6f92b8
JB
11851 char *icon_name;
11852{
c118dd06 11853 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11854 return 1;
11855
1be2d067
KH
11856#ifdef HAVE_X11R4
11857 {
11858 XTextProperty text;
11859 text.value = (unsigned char *) icon_name;
11860 text.encoding = XA_STRING;
11861 text.format = 8;
11862 text.nitems = strlen (icon_name);
11863#ifdef USE_X_TOOLKIT
7556890b 11864 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11865 &text);
11866#else /* not USE_X_TOOLKIT */
11867 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11868#endif /* not USE_X_TOOLKIT */
11869 }
11870#else /* not HAVE_X11R4 */
11871 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11872#endif /* not HAVE_X11R4 */
58769bee 11873
7556890b
RS
11874 if (f->output_data.x->icon_bitmap > 0)
11875 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11876 f->output_data.x->icon_bitmap = 0;
b1c884c3 11877 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11878
11879 return 0;
11880}
11881\f
e99db5a1
RS
11882#define X_ERROR_MESSAGE_SIZE 200
11883
11884/* If non-nil, this should be a string.
11885 It means catch X errors and store the error message in this string. */
11886
11887static Lisp_Object x_error_message_string;
11888
11889/* An X error handler which stores the error message in
11890 x_error_message_string. This is called from x_error_handler if
11891 x_catch_errors is in effect. */
11892
06a2c219 11893static void
e99db5a1
RS
11894x_error_catcher (display, error)
11895 Display *display;
11896 XErrorEvent *error;
11897{
11898 XGetErrorText (display, error->error_code,
11899 XSTRING (x_error_message_string)->data,
11900 X_ERROR_MESSAGE_SIZE);
11901}
11902
11903/* Begin trapping X errors for display DPY. Actually we trap X errors
11904 for all displays, but DPY should be the display you are actually
11905 operating on.
11906
11907 After calling this function, X protocol errors no longer cause
11908 Emacs to exit; instead, they are recorded in the string
11909 stored in x_error_message_string.
11910
11911 Calling x_check_errors signals an Emacs error if an X error has
11912 occurred since the last call to x_catch_errors or x_check_errors.
11913
11914 Calling x_uncatch_errors resumes the normal error handling. */
11915
11916void x_check_errors ();
11917static Lisp_Object x_catch_errors_unwind ();
11918
11919int
11920x_catch_errors (dpy)
11921 Display *dpy;
11922{
11923 int count = specpdl_ptr - specpdl;
11924
11925 /* Make sure any errors from previous requests have been dealt with. */
11926 XSync (dpy, False);
11927
11928 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11929
11930 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11931 XSTRING (x_error_message_string)->data[0] = 0;
11932
11933 return count;
11934}
11935
11936/* Unbind the binding that we made to check for X errors. */
11937
11938static Lisp_Object
11939x_catch_errors_unwind (old_val)
11940 Lisp_Object old_val;
11941{
11942 x_error_message_string = old_val;
11943 return Qnil;
11944}
11945
11946/* If any X protocol errors have arrived since the last call to
11947 x_catch_errors or x_check_errors, signal an Emacs error using
11948 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11949
11950void
11951x_check_errors (dpy, format)
11952 Display *dpy;
11953 char *format;
11954{
11955 /* Make sure to catch any errors incurred so far. */
11956 XSync (dpy, False);
11957
11958 if (XSTRING (x_error_message_string)->data[0])
11959 error (format, XSTRING (x_error_message_string)->data);
11960}
11961
9829ddba
RS
11962/* Nonzero if we had any X protocol errors
11963 since we did x_catch_errors on DPY. */
e99db5a1
RS
11964
11965int
11966x_had_errors_p (dpy)
11967 Display *dpy;
11968{
11969 /* Make sure to catch any errors incurred so far. */
11970 XSync (dpy, False);
11971
11972 return XSTRING (x_error_message_string)->data[0] != 0;
11973}
11974
9829ddba
RS
11975/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11976
06a2c219 11977void
9829ddba
RS
11978x_clear_errors (dpy)
11979 Display *dpy;
11980{
11981 XSTRING (x_error_message_string)->data[0] = 0;
11982}
11983
e99db5a1
RS
11984/* Stop catching X protocol errors and let them make Emacs die.
11985 DPY should be the display that was passed to x_catch_errors.
11986 COUNT should be the value that was returned by
11987 the corresponding call to x_catch_errors. */
11988
11989void
11990x_uncatch_errors (dpy, count)
11991 Display *dpy;
11992 int count;
11993{
11994 unbind_to (count, Qnil);
11995}
11996
11997#if 0
11998static unsigned int x_wire_count;
11999x_trace_wire ()
12000{
12001 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12002}
12003#endif /* ! 0 */
12004
12005\f
12006/* Handle SIGPIPE, which can happen when the connection to a server
12007 simply goes away. SIGPIPE is handled by x_connection_signal.
12008 Don't need to do anything, because the write which caused the
12009 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 12010 which will do the appropriate cleanup for us. */
e99db5a1
RS
12011
12012static SIGTYPE
12013x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 12014 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
12015{
12016#ifdef USG
12017 /* USG systems forget handlers when they are used;
12018 must reestablish each time */
12019 signal (signalnum, x_connection_signal);
12020#endif /* USG */
12021}
0da1ab50 12022
e99db5a1 12023\f
0da1ab50
GM
12024/************************************************************************
12025 Handling X errors
12026 ************************************************************************/
4746118a 12027
f0e299de
GM
12028/* Error message passed to x_connection_closed. */
12029
12030static char *error_msg;
12031
a7248e4f 12032/* Function installed as fatal_error_signal_hook in
f0e299de
GM
12033 x_connection_closed. Print the X error message, and exit normally,
12034 instead of dumping core when XtCloseDisplay fails. */
12035
12036static void
12037x_fatal_error_signal ()
12038{
12039 fprintf (stderr, "%s\n", error_msg);
12040 exit (70);
12041}
12042
0da1ab50
GM
12043/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12044 the text of an error message that lead to the connection loss. */
16bd92ea 12045
4746118a 12046static SIGTYPE
5978125e
GM
12047x_connection_closed (dpy, error_message)
12048 Display *dpy;
7a13e894 12049 char *error_message;
4746118a 12050{
5978125e 12051 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 12052 Lisp_Object frame, tail;
0da1ab50 12053 int count;
0da1ab50 12054
f0e299de
GM
12055 error_msg = (char *) alloca (strlen (error_message) + 1);
12056 strcpy (error_msg, error_message);
1a532e54
GM
12057 handling_signal = 0;
12058
0da1ab50
GM
12059 /* Prevent being called recursively because of an error condition
12060 below. Otherwise, we might end up with printing ``can't find per
12061 display information'' in the recursive call instead of printing
12062 the original message here. */
12063 count = x_catch_errors (dpy);
12064
8a4f36cc
GM
12065 /* We have to close the display to inform Xt that it doesn't
12066 exist anymore. If we don't, Xt will continue to wait for
12067 events from the display. As a consequence, a sequence of
12068
12069 M-x make-frame-on-display RET :1 RET
12070 ...kill the new frame, so that we get an IO error...
12071 M-x make-frame-on-display RET :1 RET
12072
12073 will indefinitely wait in Xt for events for display `:1', opened
12074 in the first class to make-frame-on-display.
6186a4a0 12075
8a4f36cc
GM
12076 Closing the display is reported to lead to a bus error on
12077 OpenWindows in certain situations. I suspect that is a bug
12078 in OpenWindows. I don't know how to cicumvent it here. */
12079
f613a4c8 12080#ifdef USE_X_TOOLKIT
ae24cb3b
GM
12081 /* If DPYINFO is null, this means we didn't open the display
12082 in the first place, so don't try to close it. */
12083 if (dpyinfo)
f0e299de
GM
12084 {
12085 extern void (*fatal_error_signal_hook) P_ ((void));
12086 fatal_error_signal_hook = x_fatal_error_signal;
12087 XtCloseDisplay (dpy);
12088 fatal_error_signal_hook = NULL;
12089 }
f613a4c8 12090#endif
adabc3a9 12091
8a4f36cc 12092 /* Indicate that this display is dead. */
9e80b57d
KR
12093 if (dpyinfo)
12094 dpyinfo->display = 0;
6186a4a0 12095
06a2c219 12096 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
12097 that are on the dead display. */
12098 FOR_EACH_FRAME (tail, frame)
12099 {
12100 Lisp_Object minibuf_frame;
12101 minibuf_frame
12102 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
12103 if (FRAME_X_P (XFRAME (frame))
12104 && FRAME_X_P (XFRAME (minibuf_frame))
12105 && ! EQ (frame, minibuf_frame)
12106 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
12107 Fdelete_frame (frame, Qt);
12108 }
12109
12110 /* Now delete all remaining frames on the dead display.
06a2c219 12111 We are now sure none of these is used as the mini-buffer
7a13e894
RS
12112 for another frame that we need to delete. */
12113 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
12114 if (FRAME_X_P (XFRAME (frame))
12115 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
12116 {
12117 /* Set this to t so that Fdelete_frame won't get confused
12118 trying to find a replacement. */
12119 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12120 Fdelete_frame (frame, Qt);
12121 }
7a13e894 12122
482a1bd2
KH
12123 if (dpyinfo)
12124 x_delete_display (dpyinfo);
7a13e894 12125
0da1ab50
GM
12126 x_uncatch_errors (dpy, count);
12127
7a13e894
RS
12128 if (x_display_list == 0)
12129 {
f0e299de 12130 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
12131 shut_down_emacs (0, 0, Qnil);
12132 exit (70);
12133 }
12ba150f 12134
7a13e894
RS
12135 /* Ordinary stack unwind doesn't deal with these. */
12136#ifdef SIGIO
12137 sigunblock (sigmask (SIGIO));
12138#endif
12139 sigunblock (sigmask (SIGALRM));
12140 TOTALLY_UNBLOCK_INPUT;
12141
aa4d9a9e 12142 clear_waiting_for_input ();
f0e299de 12143 error ("%s", error_msg);
4746118a
JB
12144}
12145
0da1ab50 12146
7a13e894
RS
12147/* This is the usual handler for X protocol errors.
12148 It kills all frames on the display that we got the error for.
12149 If that was the only one, it prints an error message and kills Emacs. */
12150
06a2c219 12151static void
c118dd06
JB
12152x_error_quitter (display, error)
12153 Display *display;
12154 XErrorEvent *error;
12155{
7a13e894 12156 char buf[256], buf1[356];
dc6f92b8 12157
58769bee 12158 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12159 original error handler. */
dc6f92b8 12160
c118dd06 12161 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 12162 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 12163 buf, error->request_code);
7a13e894 12164 x_connection_closed (display, buf1);
dc6f92b8
JB
12165}
12166
0da1ab50 12167
e99db5a1
RS
12168/* This is the first-level handler for X protocol errors.
12169 It calls x_error_quitter or x_error_catcher. */
7a13e894 12170
8922af5f 12171static int
e99db5a1 12172x_error_handler (display, error)
8922af5f 12173 Display *display;
e99db5a1 12174 XErrorEvent *error;
8922af5f 12175{
e99db5a1
RS
12176 if (! NILP (x_error_message_string))
12177 x_error_catcher (display, error);
12178 else
12179 x_error_quitter (display, error);
06a2c219 12180 return 0;
f9e24cb9 12181}
c118dd06 12182
e99db5a1
RS
12183/* This is the handler for X IO errors, always.
12184 It kills all frames on the display that we lost touch with.
12185 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 12186
c118dd06 12187static int
e99db5a1 12188x_io_error_quitter (display)
c118dd06 12189 Display *display;
c118dd06 12190{
e99db5a1 12191 char buf[256];
dc6f92b8 12192
e99db5a1
RS
12193 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12194 x_connection_closed (display, buf);
06a2c219 12195 return 0;
dc6f92b8 12196}
dc6f92b8 12197\f
f451eb13
JB
12198/* Changing the font of the frame. */
12199
76bcdf39
RS
12200/* Give frame F the font named FONTNAME as its default font, and
12201 return the full name of that font. FONTNAME may be a wildcard
12202 pattern; in that case, we choose some font that fits the pattern.
12203 The return value shows which font we chose. */
12204
b5cf7a0e 12205Lisp_Object
f676886a
JB
12206x_new_font (f, fontname)
12207 struct frame *f;
dc6f92b8
JB
12208 register char *fontname;
12209{
dc43ef94 12210 struct font_info *fontp
ee569018 12211 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 12212
dc43ef94
KH
12213 if (!fontp)
12214 return Qnil;
2224a5fc 12215
dc43ef94 12216 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 12217 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94 12218 f->output_data.x->fontset = -1;
976b73d7
KS
12219
12220 x_compute_fringe_widths (f, 1);
12221
b2cad826
KH
12222 /* Compute the scroll bar width in character columns. */
12223 if (f->scroll_bar_pixel_width > 0)
12224 {
7556890b 12225 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
12226 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12227 }
12228 else
4e61bddf
RS
12229 {
12230 int wid = FONT_WIDTH (f->output_data.x->font);
12231 f->scroll_bar_cols = (14 + wid - 1) / wid;
12232 }
b2cad826 12233
f676886a 12234 /* Now make the frame display the given font. */
c118dd06 12235 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 12236 {
7556890b
RS
12237 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12238 f->output_data.x->font->fid);
12239 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12240 f->output_data.x->font->fid);
12241 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12242 f->output_data.x->font->fid);
f676886a 12243
a27f9f86 12244 frame_update_line_height (f);
3497f73e
GM
12245
12246 /* Don't change the size of a tip frame; there's no point in
12247 doing it because it's done in Fx_show_tip, and it leads to
12248 problems because the tip frame has no widget. */
12249 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12250 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 12251 }
a27f9f86
RS
12252 else
12253 /* If we are setting a new frame's font for the first time,
12254 there are no faces yet, so this font's height is the line height. */
7556890b 12255 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 12256
dc43ef94
KH
12257 return build_string (fontp->full_name);
12258}
12259
12260/* Give frame F the fontset named FONTSETNAME as its default font, and
12261 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
12262 pattern; in that case, we choose some fontset that fits the pattern.
12263 The return value shows which fontset we chose. */
b5cf7a0e 12264
dc43ef94
KH
12265Lisp_Object
12266x_new_fontset (f, fontsetname)
12267 struct frame *f;
12268 char *fontsetname;
12269{
ee569018 12270 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 12271 Lisp_Object result;
b5cf7a0e 12272
dc43ef94
KH
12273 if (fontset < 0)
12274 return Qnil;
b5cf7a0e 12275
2da424f1
KH
12276 if (f->output_data.x->fontset == fontset)
12277 /* This fontset is already set in frame F. There's nothing more
12278 to do. */
ee569018 12279 return fontset_name (fontset);
dc43ef94 12280
ee569018 12281 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
12282
12283 if (!STRINGP (result))
12284 /* Can't load ASCII font. */
12285 return Qnil;
12286
12287 /* Since x_new_font doesn't update any fontset information, do it now. */
12288 f->output_data.x->fontset = fontset;
dc43ef94 12289
f5d11644
GM
12290#ifdef HAVE_X_I18N
12291 if (FRAME_XIC (f)
12292 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 12293 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
12294#endif
12295
dc43ef94 12296 return build_string (fontsetname);
dc6f92b8 12297}
f5d11644 12298
976b73d7
KS
12299/* Compute actual fringe widths */
12300
12301void
12302x_compute_fringe_widths (f, redraw)
12303 struct frame *f;
12304 int redraw;
12305{
12306 int o_left = f->output_data.x->left_fringe_width;
12307 int o_right = f->output_data.x->right_fringe_width;
12308 int o_cols = f->output_data.x->fringe_cols;
12309
12310 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12311 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12312 int left_fringe_width, right_fringe_width;
12313
12314 if (!NILP (left_fringe))
12315 left_fringe = Fcdr (left_fringe);
12316 if (!NILP (right_fringe))
12317 right_fringe = Fcdr (right_fringe);
12318
12319 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12320 XINT (left_fringe));
12321 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12322 XINT (right_fringe));
12323
12324 if (left_fringe_width || right_fringe_width)
12325 {
12326 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12327 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12328 int conf_wid = left_wid + right_wid;
12329 int font_wid = FONT_WIDTH (f->output_data.x->font);
12330 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12331 int real_wid = cols * font_wid;
12332 if (left_wid && right_wid)
12333 {
12334 if (left_fringe_width < 0)
12335 {
12336 /* Left fringe width is fixed, adjust right fringe if necessary */
12337 f->output_data.x->left_fringe_width = left_wid;
12338 f->output_data.x->right_fringe_width = real_wid - left_wid;
12339 }
12340 else if (right_fringe_width < 0)
12341 {
12342 /* Right fringe width is fixed, adjust left fringe if necessary */
12343 f->output_data.x->left_fringe_width = real_wid - right_wid;
12344 f->output_data.x->right_fringe_width = right_wid;
12345 }
12346 else
12347 {
12348 /* Adjust both fringes with an equal amount.
12349 Note that we are doing integer arithmetic here, so don't
12350 lose a pixel if the total width is an odd number. */
12351 int fill = real_wid - conf_wid;
12352 f->output_data.x->left_fringe_width = left_wid + fill/2;
12353 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12354 }
12355 }
12356 else if (left_fringe_width)
12357 {
12358 f->output_data.x->left_fringe_width = real_wid;
12359 f->output_data.x->right_fringe_width = 0;
12360 }
12361 else
12362 {
12363 f->output_data.x->left_fringe_width = 0;
12364 f->output_data.x->right_fringe_width = real_wid;
12365 }
12366 f->output_data.x->fringe_cols = cols;
12367 f->output_data.x->fringes_extra = real_wid;
12368 }
12369 else
12370 {
12371 f->output_data.x->left_fringe_width = 0;
12372 f->output_data.x->right_fringe_width = 0;
12373 f->output_data.x->fringe_cols = 0;
12374 f->output_data.x->fringes_extra = 0;
12375 }
12376
12377 if (redraw && FRAME_VISIBLE_P (f))
12378 if (o_left != f->output_data.x->left_fringe_width ||
12379 o_right != f->output_data.x->right_fringe_width ||
12380 o_cols != f->output_data.x->fringe_cols)
12381 redraw_frame (f);
12382}
f5d11644
GM
12383\f
12384/***********************************************************************
12385 X Input Methods
12386 ***********************************************************************/
12387
12388#ifdef HAVE_X_I18N
12389
12390#ifdef HAVE_X11R6
12391
12392/* XIM destroy callback function, which is called whenever the
12393 connection to input method XIM dies. CLIENT_DATA contains a
12394 pointer to the x_display_info structure corresponding to XIM. */
12395
12396static void
12397xim_destroy_callback (xim, client_data, call_data)
12398 XIM xim;
12399 XPointer client_data;
12400 XPointer call_data;
12401{
12402 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12403 Lisp_Object frame, tail;
12404
12405 BLOCK_INPUT;
12406
12407 /* No need to call XDestroyIC.. */
12408 FOR_EACH_FRAME (tail, frame)
12409 {
12410 struct frame *f = XFRAME (frame);
12411 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12412 {
12413 FRAME_XIC (f) = NULL;
12414 if (FRAME_XIC_FONTSET (f))
12415 {
12416 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12417 FRAME_XIC_FONTSET (f) = NULL;
12418 }
12419 }
12420 }
12421
12422 /* No need to call XCloseIM. */
12423 dpyinfo->xim = NULL;
12424 XFree (dpyinfo->xim_styles);
12425 UNBLOCK_INPUT;
12426}
12427
12428#endif /* HAVE_X11R6 */
12429
12430/* Open the connection to the XIM server on display DPYINFO.
12431 RESOURCE_NAME is the resource name Emacs uses. */
12432
12433static void
12434xim_open_dpy (dpyinfo, resource_name)
12435 struct x_display_info *dpyinfo;
12436 char *resource_name;
12437{
287f7dd6 12438#ifdef USE_XIM
f5d11644
GM
12439 XIM xim;
12440
12441 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12442 dpyinfo->xim = xim;
12443
12444 if (xim)
12445 {
f5d11644
GM
12446#ifdef HAVE_X11R6
12447 XIMCallback destroy;
12448#endif
12449
12450 /* Get supported styles and XIM values. */
12451 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12452
12453#ifdef HAVE_X11R6
12454 destroy.callback = xim_destroy_callback;
12455 destroy.client_data = (XPointer)dpyinfo;
cea2ad76 12456 /* This isn't prototyped in OSF 5.0. */
f5d11644
GM
12457 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12458#endif
12459 }
287f7dd6
GM
12460
12461#else /* not USE_XIM */
12462 dpyinfo->xim = NULL;
12463#endif /* not USE_XIM */
f5d11644
GM
12464}
12465
12466
b9de836c 12467#ifdef HAVE_X11R6_XIM
f5d11644
GM
12468
12469struct xim_inst_t
12470{
12471 struct x_display_info *dpyinfo;
12472 char *resource_name;
12473};
12474
12475/* XIM instantiate callback function, which is called whenever an XIM
12476 server is available. DISPLAY is teh display of the XIM.
12477 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12478 when the callback was registered. */
12479
12480static void
12481xim_instantiate_callback (display, client_data, call_data)
12482 Display *display;
12483 XPointer client_data;
12484 XPointer call_data;
12485{
12486 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12487 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12488
12489 /* We don't support multiple XIM connections. */
12490 if (dpyinfo->xim)
12491 return;
12492
12493 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12494
12495 /* Create XIC for the existing frames on the same display, as long
12496 as they have no XIC. */
12497 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12498 {
12499 Lisp_Object tail, frame;
12500
12501 BLOCK_INPUT;
12502 FOR_EACH_FRAME (tail, frame)
12503 {
12504 struct frame *f = XFRAME (frame);
12505
12506 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12507 if (FRAME_XIC (f) == NULL)
12508 {
12509 create_frame_xic (f);
12510 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12511 xic_set_statusarea (f);
12512 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12513 {
12514 struct window *w = XWINDOW (f->selected_window);
12515 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12516 }
12517 }
12518 }
12519
12520 UNBLOCK_INPUT;
12521 }
12522}
12523
b9de836c 12524#endif /* HAVE_X11R6_XIM */
f5d11644
GM
12525
12526
12527/* Open a connection to the XIM server on display DPYINFO.
12528 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12529 connection only at the first time. On X11R6, open the connection
12530 in the XIM instantiate callback function. */
12531
12532static void
12533xim_initialize (dpyinfo, resource_name)
12534 struct x_display_info *dpyinfo;
12535 char *resource_name;
12536{
287f7dd6 12537#ifdef USE_XIM
b9de836c 12538#ifdef HAVE_X11R6_XIM
f5d11644
GM
12539 struct xim_inst_t *xim_inst;
12540 int len;
12541
12542 dpyinfo->xim = NULL;
12543 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12544 xim_inst->dpyinfo = dpyinfo;
12545 len = strlen (resource_name);
12546 xim_inst->resource_name = (char *) xmalloc (len + 1);
12547 bcopy (resource_name, xim_inst->resource_name, len + 1);
12548 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12549 resource_name, EMACS_CLASS,
12550 xim_instantiate_callback,
2ebb2f8b
DL
12551 /* Fixme: This is XPointer in
12552 XFree86 but (XPointer *) on
12553 Tru64, at least. */
12554 (XPointer) xim_inst);
b9de836c 12555#else /* not HAVE_X11R6_XIM */
f5d11644
GM
12556 dpyinfo->xim = NULL;
12557 xim_open_dpy (dpyinfo, resource_name);
b9de836c 12558#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
12559
12560#else /* not USE_XIM */
12561 dpyinfo->xim = NULL;
12562#endif /* not USE_XIM */
f5d11644
GM
12563}
12564
12565
12566/* Close the connection to the XIM server on display DPYINFO. */
12567
12568static void
12569xim_close_dpy (dpyinfo)
12570 struct x_display_info *dpyinfo;
12571{
287f7dd6 12572#ifdef USE_XIM
b9de836c 12573#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
12574 if (dpyinfo->display)
12575 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12576 NULL, EMACS_CLASS,
12577 xim_instantiate_callback, NULL);
b9de836c 12578#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
12579 if (dpyinfo->display)
12580 XCloseIM (dpyinfo->xim);
f5d11644
GM
12581 dpyinfo->xim = NULL;
12582 XFree (dpyinfo->xim_styles);
287f7dd6 12583#endif /* USE_XIM */
f5d11644
GM
12584}
12585
b9de836c 12586#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
12587
12588
dc6f92b8 12589\f
2e365682
RS
12590/* Calculate the absolute position in frame F
12591 from its current recorded position values and gravity. */
12592
dfcf069d 12593void
43bca5d5 12594x_calc_absolute_position (f)
f676886a 12595 struct frame *f;
dc6f92b8 12596{
06a2c219 12597 Window child;
6dba1858 12598 int win_x = 0, win_y = 0;
7556890b 12599 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
12600 int this_window;
12601
9829ddba
RS
12602 /* We have nothing to do if the current position
12603 is already for the top-left corner. */
12604 if (! ((flags & XNegative) || (flags & YNegative)))
12605 return;
12606
c81412a0 12607#ifdef USE_X_TOOLKIT
7556890b 12608 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
12609#else
12610 this_window = FRAME_X_WINDOW (f);
12611#endif
6dba1858
RS
12612
12613 /* Find the position of the outside upper-left corner of
9829ddba
RS
12614 the inner window, with respect to the outer window.
12615 But do this only if we will need the results. */
7556890b 12616 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 12617 {
9829ddba
RS
12618 int count;
12619
6dba1858 12620 BLOCK_INPUT;
9829ddba
RS
12621 count = x_catch_errors (FRAME_X_DISPLAY (f));
12622 while (1)
12623 {
12624 x_clear_errors (FRAME_X_DISPLAY (f));
12625 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12626
12627 /* From-window, to-window. */
12628 this_window,
12629 f->output_data.x->parent_desc,
12630
12631 /* From-position, to-position. */
12632 0, 0, &win_x, &win_y,
12633
12634 /* Child of win. */
12635 &child);
12636 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12637 {
12638 Window newroot, newparent = 0xdeadbeef;
12639 Window *newchildren;
2ebb2f8b 12640 unsigned int nchildren;
9829ddba
RS
12641
12642 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12643 &newparent, &newchildren, &nchildren))
12644 break;
58769bee 12645
7c3c78a3 12646 XFree ((char *) newchildren);
6dba1858 12647
9829ddba
RS
12648 f->output_data.x->parent_desc = newparent;
12649 }
12650 else
12651 break;
12652 }
6dba1858 12653
9829ddba 12654 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
12655 UNBLOCK_INPUT;
12656 }
12657
12658 /* Treat negative positions as relative to the leftmost bottommost
12659 position that fits on the screen. */
20f55f9a 12660 if (flags & XNegative)
7556890b 12661 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
12662 - 2 * f->output_data.x->border_width - win_x
12663 - PIXEL_WIDTH (f)
12664 + f->output_data.x->left_pos);
dc6f92b8 12665
7708ced0
GM
12666 {
12667 int height = PIXEL_HEIGHT (f);
06a2c219 12668
7708ced0
GM
12669#if defined USE_X_TOOLKIT && defined USE_MOTIF
12670 /* Something is fishy here. When using Motif, starting Emacs with
12671 `-g -0-0', the frame appears too low by a few pixels.
12672
12673 This seems to be so because initially, while Emacs is starting,
12674 the column widget's height and the frame's pixel height are
12675 different. The column widget's height is the right one. In
12676 later invocations, when Emacs is up, the frame's pixel height
12677 is right, though.
12678
12679 It's not obvious where the initial small difference comes from.
12680 2000-12-01, gerd. */
12681
12682 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 12683#endif
2e365682 12684
7708ced0
GM
12685 if (flags & YNegative)
12686 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12687 - 2 * f->output_data.x->border_width
12688 - win_y
12689 - height
12690 + f->output_data.x->top_pos);
12691 }
12692
3a35ab44
RS
12693 /* The left_pos and top_pos
12694 are now relative to the top and left screen edges,
12695 so the flags should correspond. */
7556890b 12696 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
12697}
12698
3a35ab44
RS
12699/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12700 to really change the position, and 0 when calling from
12701 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
12702 position values). It is -1 when calling from x_set_frame_parameters,
12703 which means, do adjust for borders but don't change the gravity. */
3a35ab44 12704
dfcf069d 12705void
dc05a16b 12706x_set_offset (f, xoff, yoff, change_gravity)
f676886a 12707 struct frame *f;
dc6f92b8 12708 register int xoff, yoff;
dc05a16b 12709 int change_gravity;
dc6f92b8 12710{
4a4cbdd5
KH
12711 int modified_top, modified_left;
12712
aa3ff7c9 12713 if (change_gravity > 0)
3a35ab44 12714 {
7556890b
RS
12715 f->output_data.x->top_pos = yoff;
12716 f->output_data.x->left_pos = xoff;
12717 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 12718 if (xoff < 0)
7556890b 12719 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 12720 if (yoff < 0)
7556890b
RS
12721 f->output_data.x->size_hint_flags |= YNegative;
12722 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 12723 }
43bca5d5 12724 x_calc_absolute_position (f);
dc6f92b8
JB
12725
12726 BLOCK_INPUT;
c32cdd9a 12727 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 12728
7556890b
RS
12729 modified_left = f->output_data.x->left_pos;
12730 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
12731#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12732 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12733 /* It is a mystery why we need to add the border_width here
12734 when the frame is already visible, but experiment says we do. */
aa3ff7c9 12735 if (change_gravity != 0)
4a4cbdd5 12736 {
7556890b
RS
12737 modified_left += f->output_data.x->border_width;
12738 modified_top += f->output_data.x->border_width;
4a4cbdd5 12739 }
e73ec6fa 12740#endif
4a4cbdd5 12741
3afe33e7 12742#ifdef USE_X_TOOLKIT
7556890b 12743 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 12744 modified_left, modified_top);
3afe33e7 12745#else /* not USE_X_TOOLKIT */
334208b7 12746 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 12747 modified_left, modified_top);
3afe33e7 12748#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12749 UNBLOCK_INPUT;
12750}
12751
d0fa3e56
EZ
12752/* Check if we need to resize the frame due to a fullscreen request.
12753 If so needed, resize the frame. */
12754static void
12755x_check_fullscreen (f)
12756 struct frame *f;
12757{
12758 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12759 {
12760 int width, height, ign;
12761
12762 x_real_positions (f, &f->output_data.x->left_pos,
12763 &f->output_data.x->top_pos);
12764
12765 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12766
12767 /* We do not need to move the window, it shall be taken care of
12768 when setting WM manager hints.
12769 If the frame is visible already, the position is checked by
12770 x_check_fullscreen_move. */
12771 if (f->width != width || f->height != height)
12772 {
12773 change_frame_size (f, height, width, 0, 1, 0);
12774 SET_FRAME_GARBAGED (f);
12775 cancel_mouse_face (f);
12776
12777 /* Wait for the change of frame size to occur */
12778 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12779
12780 }
12781 }
12782}
12783
12784/* If frame parameters are set after the frame is mapped, we need to move
12785 the window. This is done in xfns.c.
12786 Some window managers moves the window to the right position, some
12787 moves the outer window manager window to the specified position.
12788 Here we check that we are in the right spot. If not, make a second
12789 move, assuming we are dealing with the second kind of window manager. */
12790static void
12791x_check_fullscreen_move (f)
12792 struct frame *f;
12793{
12794 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12795 {
12796 int expect_top = f->output_data.x->top_pos;
12797 int expect_left = f->output_data.x->left_pos;
12798
12799 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12800 expect_top = 0;
12801 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12802 expect_left = 0;
12803
12804 if (expect_top != f->output_data.x->top_pos
12805 || expect_left != f->output_data.x->left_pos)
12806 x_set_offset (f, expect_left, expect_top, 1);
12807
12808 /* Just do this once */
12809 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12810 }
12811}
12812
12813
12814/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12815 wanted positions of the WM window (not emacs window).
12816 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12817 window (FRAME_X_WINDOW).
12818 */
12819void
12820x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12821 struct frame *f;
12822 int *width;
12823 int *height;
12824 int *top_pos;
12825 int *left_pos;
12826{
12827 int newwidth = f->width, newheight = f->height;
12828
12829 *top_pos = f->output_data.x->top_pos;
12830 *left_pos = f->output_data.x->left_pos;
12831
12832 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12833 {
12834 int ph;
12835
12836 ph = FRAME_X_DISPLAY_INFO (f)->height;
12837 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12838 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12839 - f->output_data.x->y_pixels_diff;
12840 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12841 *top_pos = 0;
12842 }
12843
12844 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12845 {
12846 int pw;
12847
12848 pw = FRAME_X_DISPLAY_INFO (f)->width;
12849 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12850 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12851 - f->output_data.x->x_pixels_diff;
12852 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12853 *left_pos = 0;
12854 }
12855
12856 *width = newwidth;
12857 *height = newheight;
12858}
12859
dc6f92b8 12860
499b1844
GM
12861/* Change the size of frame F's X window to COLS/ROWS in the case F
12862 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12863 top-left-corner window gravity for this size change and subsequent
12864 size changes. Otherwise we leave the window gravity unchanged. */
12865
12866static void
12867x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 12868 struct frame *f;
bc20ebbf 12869 int change_gravity;
b1c884c3 12870 int cols, rows;
dc6f92b8
JB
12871{
12872 int pixelwidth, pixelheight;
80fd1fe2 12873
b1c884c3 12874 check_frame_size (f, &rows, &cols);
7556890b 12875 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
12876 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12877 ? 0
12878 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 12879 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 12880 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
976b73d7
KS
12881
12882 x_compute_fringe_widths (f, 0);
12883
f451eb13
JB
12884 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12885 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 12886
7556890b 12887 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 12888 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 12889
334208b7
RS
12890 XSync (FRAME_X_DISPLAY (f), False);
12891 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12892 pixelwidth, pixelheight);
b1c884c3
JB
12893
12894 /* Now, strictly speaking, we can't be sure that this is accurate,
12895 but the window manager will get around to dealing with the size
12896 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
12897 ConfigureNotify event gets here.
12898
12899 We could just not bother storing any of this information here,
12900 and let the ConfigureNotify event set everything up, but that
fddd5ceb 12901 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 12902 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
12903 point in the future when the ConfigureNotify event arrives.
12904
12905 We pass 1 for DELAY since we can't run Lisp code inside of
12906 a BLOCK_INPUT. */
7d1e984f 12907 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
12908 PIXEL_WIDTH (f) = pixelwidth;
12909 PIXEL_HEIGHT (f) = pixelheight;
12910
aee9a898
RS
12911 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12912 receive in the ConfigureNotify event; if we get what we asked
12913 for, then the event won't cause the screen to become garbaged, so
12914 we have to make sure to do it here. */
12915 SET_FRAME_GARBAGED (f);
12916
12917 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
12918}
12919
12920
12921/* Call this to change the size of frame F's x-window.
12922 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12923 for this size change and subsequent size changes.
12924 Otherwise we leave the window gravity unchanged. */
aee9a898 12925
499b1844
GM
12926void
12927x_set_window_size (f, change_gravity, cols, rows)
12928 struct frame *f;
12929 int change_gravity;
12930 int cols, rows;
12931{
12932 BLOCK_INPUT;
12933
12934#ifdef USE_X_TOOLKIT
12935
f1f4d345 12936 if (f->output_data.x->widget != NULL)
499b1844
GM
12937 {
12938 /* The x and y position of the widget is clobbered by the
12939 call to XtSetValues within EmacsFrameSetCharSize.
12940 This is a real kludge, but I don't understand Xt so I can't
12941 figure out a correct fix. Can anyone else tell me? -- rms. */
12942 int xpos = f->output_data.x->widget->core.x;
12943 int ypos = f->output_data.x->widget->core.y;
12944 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12945 f->output_data.x->widget->core.x = xpos;
12946 f->output_data.x->widget->core.y = ypos;
12947 }
12948 else
12949 x_set_window_size_1 (f, change_gravity, cols, rows);
12950
12951#else /* not USE_X_TOOLKIT */
12952
12953 x_set_window_size_1 (f, change_gravity, cols, rows);
12954
aee9a898
RS
12955#endif /* not USE_X_TOOLKIT */
12956
4d73d038 12957 /* If cursor was outside the new size, mark it as off. */
06a2c219 12958 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 12959
aee9a898
RS
12960 /* Clear out any recollection of where the mouse highlighting was,
12961 since it might be in a place that's outside the new frame size.
12962 Actually checking whether it is outside is a pain in the neck,
12963 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 12964 cancel_mouse_face (f);
dbc4e1c1 12965
dc6f92b8
JB
12966 UNBLOCK_INPUT;
12967}
dc6f92b8 12968\f
d047c4eb 12969/* Mouse warping. */
dc6f92b8 12970
9b378208 12971void
f676886a
JB
12972x_set_mouse_position (f, x, y)
12973 struct frame *f;
dc6f92b8
JB
12974 int x, y;
12975{
12976 int pix_x, pix_y;
12977
7556890b
RS
12978 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12979 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
12980
12981 if (pix_x < 0) pix_x = 0;
12982 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12983
12984 if (pix_y < 0) pix_y = 0;
12985 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
12986
12987 BLOCK_INPUT;
dc6f92b8 12988
334208b7
RS
12989 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12990 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
12991 UNBLOCK_INPUT;
12992}
12993
9b378208
RS
12994/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12995
12996void
12997x_set_mouse_pixel_position (f, pix_x, pix_y)
12998 struct frame *f;
12999 int pix_x, pix_y;
13000{
13001 BLOCK_INPUT;
13002
334208b7
RS
13003 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13004 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
13005 UNBLOCK_INPUT;
13006}
d047c4eb
KH
13007\f
13008/* focus shifting, raising and lowering. */
9b378208 13009
dfcf069d 13010void
f676886a
JB
13011x_focus_on_frame (f)
13012 struct frame *f;
dc6f92b8 13013{
1fb20991 13014#if 0 /* This proves to be unpleasant. */
f676886a 13015 x_raise_frame (f);
1fb20991 13016#endif
6d4238f3
JB
13017#if 0
13018 /* I don't think that the ICCCM allows programs to do things like this
13019 without the interaction of the window manager. Whatever you end up
f676886a 13020 doing with this code, do it to x_unfocus_frame too. */
334208b7 13021 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 13022 RevertToPointerRoot, CurrentTime);
c118dd06 13023#endif /* ! 0 */
dc6f92b8
JB
13024}
13025
dfcf069d 13026void
f676886a
JB
13027x_unfocus_frame (f)
13028 struct frame *f;
dc6f92b8 13029{
6d4238f3 13030#if 0
f676886a 13031 /* Look at the remarks in x_focus_on_frame. */
0f941935 13032 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 13033 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 13034 RevertToPointerRoot, CurrentTime);
c118dd06 13035#endif /* ! 0 */
dc6f92b8
JB
13036}
13037
f676886a 13038/* Raise frame F. */
dc6f92b8 13039
dfcf069d 13040void
f676886a
JB
13041x_raise_frame (f)
13042 struct frame *f;
dc6f92b8 13043{
3a88c238 13044 if (f->async_visible)
dc6f92b8
JB
13045 {
13046 BLOCK_INPUT;
3afe33e7 13047#ifdef USE_X_TOOLKIT
7556890b 13048 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13049#else /* not USE_X_TOOLKIT */
334208b7 13050 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13051#endif /* not USE_X_TOOLKIT */
334208b7 13052 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13053 UNBLOCK_INPUT;
13054 }
13055}
13056
f676886a 13057/* Lower frame F. */
dc6f92b8 13058
dfcf069d 13059void
f676886a
JB
13060x_lower_frame (f)
13061 struct frame *f;
dc6f92b8 13062{
3a88c238 13063 if (f->async_visible)
dc6f92b8
JB
13064 {
13065 BLOCK_INPUT;
3afe33e7 13066#ifdef USE_X_TOOLKIT
7556890b 13067 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13068#else /* not USE_X_TOOLKIT */
334208b7 13069 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13070#endif /* not USE_X_TOOLKIT */
334208b7 13071 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13072 UNBLOCK_INPUT;
13073 }
13074}
13075
dbc4e1c1 13076static void
6b0442dc 13077XTframe_raise_lower (f, raise_flag)
dbc4e1c1 13078 FRAME_PTR f;
6b0442dc 13079 int raise_flag;
dbc4e1c1 13080{
6b0442dc 13081 if (raise_flag)
dbc4e1c1
JB
13082 x_raise_frame (f);
13083 else
13084 x_lower_frame (f);
13085}
d047c4eb
KH
13086\f
13087/* Change of visibility. */
dc6f92b8 13088
9382638d
KH
13089/* This tries to wait until the frame is really visible.
13090 However, if the window manager asks the user where to position
13091 the frame, this will return before the user finishes doing that.
13092 The frame will not actually be visible at that time,
13093 but it will become visible later when the window manager
13094 finishes with it. */
13095
dfcf069d 13096void
f676886a
JB
13097x_make_frame_visible (f)
13098 struct frame *f;
dc6f92b8 13099{
990ba854 13100 Lisp_Object type;
1aa6072f 13101 int original_top, original_left;
31be9251
GM
13102 int retry_count = 2;
13103
13104 retry:
dc6f92b8 13105
dc6f92b8 13106 BLOCK_INPUT;
dc6f92b8 13107
990ba854
RS
13108 type = x_icon_type (f);
13109 if (!NILP (type))
13110 x_bitmap_icon (f, type);
bdcd49ba 13111
f676886a 13112 if (! FRAME_VISIBLE_P (f))
90e65f07 13113 {
1aa6072f
RS
13114 /* We test FRAME_GARBAGED_P here to make sure we don't
13115 call x_set_offset a second time
13116 if we get to x_make_frame_visible a second time
13117 before the window gets really visible. */
13118 if (! FRAME_ICONIFIED_P (f)
13119 && ! f->output_data.x->asked_for_visible)
13120 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13121
13122 f->output_data.x->asked_for_visible = 1;
13123
90e65f07 13124 if (! EQ (Vx_no_window_manager, Qt))
f676886a 13125 x_wm_set_window_state (f, NormalState);
3afe33e7 13126#ifdef USE_X_TOOLKIT
d7a38a2e 13127 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13128 XtMapWidget (f->output_data.x->widget);
3afe33e7 13129#else /* not USE_X_TOOLKIT */
7f9c7f94 13130 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13131#endif /* not USE_X_TOOLKIT */
0134a210
RS
13132#if 0 /* This seems to bring back scroll bars in the wrong places
13133 if the window configuration has changed. They seem
13134 to come back ok without this. */
ab648270 13135 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 13136 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 13137#endif
90e65f07 13138 }
dc6f92b8 13139
334208b7 13140 XFlush (FRAME_X_DISPLAY (f));
90e65f07 13141
0dacf791
RS
13142 /* Synchronize to ensure Emacs knows the frame is visible
13143 before we do anything else. We do this loop with input not blocked
13144 so that incoming events are handled. */
13145 {
13146 Lisp_Object frame;
12ce2351 13147 int count;
28c01ffe
RS
13148 /* This must be before UNBLOCK_INPUT
13149 since events that arrive in response to the actions above
13150 will set it when they are handled. */
13151 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
13152
13153 original_left = f->output_data.x->left_pos;
13154 original_top = f->output_data.x->top_pos;
c0a04927
RS
13155
13156 /* This must come after we set COUNT. */
13157 UNBLOCK_INPUT;
13158
2745e6c4 13159 /* We unblock here so that arriving X events are processed. */
1aa6072f 13160
dcb07ae9
RS
13161 /* Now move the window back to where it was "supposed to be".
13162 But don't do it if the gravity is negative.
13163 When the gravity is negative, this uses a position
28c01ffe
RS
13164 that is 3 pixels too low. Perhaps that's really the border width.
13165
13166 Don't do this if the window has never been visible before,
13167 because the window manager may choose the position
13168 and we don't want to override it. */
1aa6072f 13169
4d3f5d9a 13170 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 13171 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 13172 && previously_visible)
1aa6072f 13173 {
2745e6c4
RS
13174 Drawable rootw;
13175 int x, y;
13176 unsigned int width, height, border, depth;
06a2c219 13177
1aa6072f 13178 BLOCK_INPUT;
9829ddba 13179
06a2c219
GM
13180 /* On some window managers (such as FVWM) moving an existing
13181 window, even to the same place, causes the window manager
13182 to introduce an offset. This can cause the window to move
13183 to an unexpected location. Check the geometry (a little
13184 slow here) and then verify that the window is in the right
13185 place. If the window is not in the right place, move it
13186 there, and take the potential window manager hit. */
2745e6c4
RS
13187 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13188 &rootw, &x, &y, &width, &height, &border, &depth);
13189
13190 if (original_left != x || original_top != y)
13191 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13192 original_left, original_top);
13193
1aa6072f
RS
13194 UNBLOCK_INPUT;
13195 }
9829ddba 13196
e0c1aef2 13197 XSETFRAME (frame, f);
c0a04927 13198
12ce2351
GM
13199 /* Wait until the frame is visible. Process X events until a
13200 MapNotify event has been seen, or until we think we won't get a
13201 MapNotify at all.. */
13202 for (count = input_signal_count + 10;
13203 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 13204 {
12ce2351 13205 /* Force processing of queued events. */
334208b7 13206 x_sync (f);
12ce2351
GM
13207
13208 /* Machines that do polling rather than SIGIO have been
13209 observed to go into a busy-wait here. So we'll fake an
13210 alarm signal to let the handler know that there's something
13211 to be read. We used to raise a real alarm, but it seems
13212 that the handler isn't always enabled here. This is
13213 probably a bug. */
8b2f8d4e 13214 if (input_polling_used ())
3b2fa4e6 13215 {
12ce2351
GM
13216 /* It could be confusing if a real alarm arrives while
13217 processing the fake one. Turn it off and let the
13218 handler reset it. */
3e71d8f2 13219 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
13220 int old_poll_suppress_count = poll_suppress_count;
13221 poll_suppress_count = 1;
13222 poll_for_input_1 ();
13223 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 13224 }
12ce2351
GM
13225
13226 /* See if a MapNotify event has been processed. */
13227 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 13228 }
31be9251
GM
13229
13230 /* 2000-09-28: In
13231
13232 (let ((f (selected-frame)))
13233 (iconify-frame f)
13234 (raise-frame f))
13235
13236 the frame is not raised with various window managers on
13237 FreeBSD, Linux and Solaris. It turns out that, for some
13238 unknown reason, the call to XtMapWidget is completely ignored.
13239 Mapping the widget a second time works. */
13240
13241 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13242 goto retry;
0dacf791 13243 }
dc6f92b8
JB
13244}
13245
06a2c219 13246/* Change from mapped state to withdrawn state. */
dc6f92b8 13247
d047c4eb
KH
13248/* Make the frame visible (mapped and not iconified). */
13249
dfcf069d 13250void
f676886a
JB
13251x_make_frame_invisible (f)
13252 struct frame *f;
dc6f92b8 13253{
546e6d5b
RS
13254 Window window;
13255
13256#ifdef USE_X_TOOLKIT
13257 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 13258 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
13259#else /* not USE_X_TOOLKIT */
13260 window = FRAME_X_WINDOW (f);
13261#endif /* not USE_X_TOOLKIT */
dc6f92b8 13262
9319ae23 13263 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13264 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13265 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13266
5627c40e 13267#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 13268 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 13269 return;
5627c40e 13270#endif
dc6f92b8
JB
13271
13272 BLOCK_INPUT;
c118dd06 13273
af31d76f
RS
13274 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13275 that the current position of the window is user-specified, rather than
13276 program-specified, so that when the window is mapped again, it will be
13277 placed at the same location, without forcing the user to position it
13278 by hand again (they have already done that once for this window.) */
c32cdd9a 13279 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 13280
c118dd06
JB
13281#ifdef HAVE_X11R4
13282
334208b7
RS
13283 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13284 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
13285 {
13286 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13287 error ("Can't notify window manager of window withdrawal");
c118dd06 13288 }
c118dd06 13289#else /* ! defined (HAVE_X11R4) */
16bd92ea 13290
c118dd06 13291 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
13292 if (! EQ (Vx_no_window_manager, Qt))
13293 {
16bd92ea 13294 XEvent unmap;
dc6f92b8 13295
16bd92ea 13296 unmap.xunmap.type = UnmapNotify;
546e6d5b 13297 unmap.xunmap.window = window;
334208b7 13298 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 13299 unmap.xunmap.from_configure = False;
334208b7
RS
13300 if (! XSendEvent (FRAME_X_DISPLAY (f),
13301 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 13302 False,
06a2c219 13303 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
13304 &unmap))
13305 {
13306 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13307 error ("Can't notify window manager of withdrawal");
16bd92ea 13308 }
dc6f92b8
JB
13309 }
13310
16bd92ea 13311 /* Unmap the window ourselves. Cheeky! */
334208b7 13312 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 13313#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 13314
5627c40e
RS
13315 /* We can't distinguish this from iconification
13316 just by the event that we get from the server.
13317 So we can't win using the usual strategy of letting
13318 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13319 and synchronize with the server to make sure we agree. */
13320 f->visible = 0;
13321 FRAME_ICONIFIED_P (f) = 0;
13322 f->async_visible = 0;
13323 f->async_iconified = 0;
13324
334208b7 13325 x_sync (f);
5627c40e 13326
dc6f92b8
JB
13327 UNBLOCK_INPUT;
13328}
13329
06a2c219 13330/* Change window state from mapped to iconified. */
dc6f92b8 13331
dfcf069d 13332void
f676886a
JB
13333x_iconify_frame (f)
13334 struct frame *f;
dc6f92b8 13335{
3afe33e7 13336 int result;
990ba854 13337 Lisp_Object type;
dc6f92b8 13338
9319ae23 13339 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13340 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13341 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13342
3a88c238 13343 if (f->async_iconified)
dc6f92b8
JB
13344 return;
13345
3afe33e7 13346 BLOCK_INPUT;
546e6d5b 13347
9af3143a
RS
13348 FRAME_SAMPLE_VISIBILITY (f);
13349
990ba854
RS
13350 type = x_icon_type (f);
13351 if (!NILP (type))
13352 x_bitmap_icon (f, type);
bdcd49ba
RS
13353
13354#ifdef USE_X_TOOLKIT
13355
546e6d5b
RS
13356 if (! FRAME_VISIBLE_P (f))
13357 {
13358 if (! EQ (Vx_no_window_manager, Qt))
13359 x_wm_set_window_state (f, IconicState);
13360 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13361 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
13362 /* The server won't give us any event to indicate
13363 that an invisible frame was changed to an icon,
13364 so we have to record it here. */
13365 f->iconified = 1;
1e6bc770 13366 f->visible = 1;
9cf30a30 13367 f->async_iconified = 1;
1e6bc770 13368 f->async_visible = 0;
546e6d5b
RS
13369 UNBLOCK_INPUT;
13370 return;
13371 }
13372
334208b7 13373 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 13374 XtWindow (f->output_data.x->widget),
334208b7 13375 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
13376 UNBLOCK_INPUT;
13377
13378 if (!result)
546e6d5b 13379 error ("Can't notify window manager of iconification");
3afe33e7
RS
13380
13381 f->async_iconified = 1;
1e6bc770
RS
13382 f->async_visible = 0;
13383
8c002a25
KH
13384
13385 BLOCK_INPUT;
334208b7 13386 XFlush (FRAME_X_DISPLAY (f));
8c002a25 13387 UNBLOCK_INPUT;
3afe33e7
RS
13388#else /* not USE_X_TOOLKIT */
13389
fd13dbb2
RS
13390 /* Make sure the X server knows where the window should be positioned,
13391 in case the user deiconifies with the window manager. */
13392 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 13393 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 13394
16bd92ea
JB
13395 /* Since we don't know which revision of X we're running, we'll use both
13396 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13397
13398 /* X11R4: send a ClientMessage to the window manager using the
13399 WM_CHANGE_STATE type. */
13400 {
13401 XEvent message;
58769bee 13402
c118dd06 13403 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 13404 message.xclient.type = ClientMessage;
334208b7 13405 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
13406 message.xclient.format = 32;
13407 message.xclient.data.l[0] = IconicState;
13408
334208b7
RS
13409 if (! XSendEvent (FRAME_X_DISPLAY (f),
13410 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
13411 False,
13412 SubstructureRedirectMask | SubstructureNotifyMask,
13413 &message))
dc6f92b8
JB
13414 {
13415 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13416 error ("Can't notify window manager of iconification");
dc6f92b8 13417 }
16bd92ea 13418 }
dc6f92b8 13419
58769bee 13420 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
13421 IconicState. */
13422 x_wm_set_window_state (f, IconicState);
dc6f92b8 13423
a9c00105
RS
13424 if (!FRAME_VISIBLE_P (f))
13425 {
13426 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 13427 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
13428 }
13429
3a88c238 13430 f->async_iconified = 1;
1e6bc770 13431 f->async_visible = 0;
dc6f92b8 13432
334208b7 13433 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 13434 UNBLOCK_INPUT;
8c002a25 13435#endif /* not USE_X_TOOLKIT */
dc6f92b8 13436}
19f71add 13437
d047c4eb 13438\f
19f71add 13439/* Free X resources of frame F. */
dc6f92b8 13440
dfcf069d 13441void
19f71add 13442x_free_frame_resources (f)
f676886a 13443 struct frame *f;
dc6f92b8 13444{
7f9c7f94 13445 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
13446 Lisp_Object bar;
13447 struct scroll_bar *b;
7f9c7f94 13448
dc6f92b8 13449 BLOCK_INPUT;
c0ff3fab 13450
6186a4a0
RS
13451 /* If a display connection is dead, don't try sending more
13452 commands to the X server. */
19f71add 13453 if (dpyinfo->display)
6186a4a0 13454 {
19f71add 13455 if (f->output_data.x->icon_desc)
6186a4a0 13456 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
13457
13458#ifdef USE_X_TOOLKIT
13459 /* Explicitly destroy the scroll bars of the frame. Without
13460 this, we get "BadDrawable" errors from the toolkit later on,
13461 presumably from expose events generated for the disappearing
13462 toolkit scroll bars. */
13463 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13464 {
13465 b = XSCROLL_BAR (bar);
13466 x_scroll_bar_remove (b);
13467 }
13468#endif
13469
31f41daf 13470#ifdef HAVE_X_I18N
f5d11644
GM
13471 if (FRAME_XIC (f))
13472 free_frame_xic (f);
31f41daf 13473#endif
c6ea2775 13474
3afe33e7 13475#ifdef USE_X_TOOLKIT
06a2c219 13476 if (f->output_data.x->widget)
30ca89f5
GM
13477 {
13478 XtDestroyWidget (f->output_data.x->widget);
13479 f->output_data.x->widget = NULL;
13480 }
c6ea2775
EZ
13481 /* Tooltips don't have widgets, only a simple X window, even if
13482 we are using a toolkit. */
13483 else if (FRAME_X_WINDOW (f))
13484 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13485
6186a4a0 13486 free_frame_menubar (f);
c6ea2775
EZ
13487#else /* !USE_X_TOOLKIT */
13488 if (FRAME_X_WINDOW (f))
13489 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13490#endif /* !USE_X_TOOLKIT */
3afe33e7 13491
3e71d8f2
GM
13492 unload_color (f, f->output_data.x->foreground_pixel);
13493 unload_color (f, f->output_data.x->background_pixel);
13494 unload_color (f, f->output_data.x->cursor_pixel);
13495 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13496 unload_color (f, f->output_data.x->border_pixel);
13497 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 13498
3e71d8f2
GM
13499 if (f->output_data.x->scroll_bar_background_pixel != -1)
13500 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13501 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13502 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
13503#ifdef USE_TOOLKIT_SCROLL_BARS
13504 /* Scrollbar shadow colors. */
13505 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13506 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13507 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13508 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13509#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
13510 if (f->output_data.x->white_relief.allocated_p)
13511 unload_color (f, f->output_data.x->white_relief.pixel);
13512 if (f->output_data.x->black_relief.allocated_p)
13513 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 13514
19f71add
GM
13515 if (FRAME_FACE_CACHE (f))
13516 free_frame_faces (f);
13517
4ca78676 13518 x_free_gcs (f);
6186a4a0
RS
13519 XFlush (FRAME_X_DISPLAY (f));
13520 }
dc6f92b8 13521
df89d8a4 13522 if (f->output_data.x->saved_menu_event)
06a2c219 13523 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 13524
7556890b 13525 xfree (f->output_data.x);
19f71add
GM
13526 f->output_data.x = NULL;
13527
0f941935
KH
13528 if (f == dpyinfo->x_focus_frame)
13529 dpyinfo->x_focus_frame = 0;
13530 if (f == dpyinfo->x_focus_event_frame)
13531 dpyinfo->x_focus_event_frame = 0;
13532 if (f == dpyinfo->x_highlight_frame)
13533 dpyinfo->x_highlight_frame = 0;
c0ff3fab 13534
7f9c7f94 13535 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 13536 {
7f9c7f94
RS
13537 dpyinfo->mouse_face_beg_row
13538 = dpyinfo->mouse_face_beg_col = -1;
13539 dpyinfo->mouse_face_end_row
13540 = dpyinfo->mouse_face_end_col = -1;
13541 dpyinfo->mouse_face_window = Qnil;
21323706
RS
13542 dpyinfo->mouse_face_deferred_gc = 0;
13543 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 13544 }
0134a210 13545
c0ff3fab 13546 UNBLOCK_INPUT;
dc6f92b8 13547}
19f71add
GM
13548
13549
13550/* Destroy the X window of frame F. */
13551
13552void
13553x_destroy_window (f)
13554 struct frame *f;
13555{
13556 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13557
13558 /* If a display connection is dead, don't try sending more
13559 commands to the X server. */
13560 if (dpyinfo->display != 0)
13561 x_free_frame_resources (f);
13562
13563 dpyinfo->reference_count--;
13564}
13565
dc6f92b8 13566\f
f451eb13
JB
13567/* Setting window manager hints. */
13568
af31d76f
RS
13569/* Set the normal size hints for the window manager, for frame F.
13570 FLAGS is the flags word to use--or 0 meaning preserve the flags
13571 that the window now has.
13572 If USER_POSITION is nonzero, we set the USPosition
13573 flag (this is useful when FLAGS is 0). */
6dba1858 13574
dfcf069d 13575void
af31d76f 13576x_wm_set_size_hint (f, flags, user_position)
f676886a 13577 struct frame *f;
af31d76f
RS
13578 long flags;
13579 int user_position;
dc6f92b8
JB
13580{
13581 XSizeHints size_hints;
3afe33e7
RS
13582
13583#ifdef USE_X_TOOLKIT
7e4f2521
FP
13584 Arg al[2];
13585 int ac = 0;
13586 Dimension widget_width, widget_height;
7556890b 13587 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 13588#else /* not USE_X_TOOLKIT */
c118dd06 13589 Window window = FRAME_X_WINDOW (f);
3afe33e7 13590#endif /* not USE_X_TOOLKIT */
dc6f92b8 13591
b72a58fd
RS
13592 /* Setting PMaxSize caused various problems. */
13593 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 13594
7556890b
RS
13595 size_hints.x = f->output_data.x->left_pos;
13596 size_hints.y = f->output_data.x->top_pos;
7553a6b7 13597
7e4f2521
FP
13598#ifdef USE_X_TOOLKIT
13599 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13600 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 13601 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
13602 size_hints.height = widget_height;
13603 size_hints.width = widget_width;
13604#else /* not USE_X_TOOLKIT */
f676886a
JB
13605 size_hints.height = PIXEL_HEIGHT (f);
13606 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 13607#endif /* not USE_X_TOOLKIT */
7553a6b7 13608
7556890b
RS
13609 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13610 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
13611 size_hints.max_width
13612 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13613 size_hints.max_height
13614 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 13615
d067ea8b
KH
13616 /* Calculate the base and minimum sizes.
13617
13618 (When we use the X toolkit, we don't do it here.
13619 Instead we copy the values that the widgets are using, below.) */
13620#ifndef USE_X_TOOLKIT
b1c884c3 13621 {
b0342f17 13622 int base_width, base_height;
0134a210 13623 int min_rows = 0, min_cols = 0;
b0342f17 13624
f451eb13
JB
13625 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13626 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 13627
0134a210 13628 check_frame_size (f, &min_rows, &min_cols);
b0342f17 13629
0134a210
RS
13630 /* The window manager uses the base width hints to calculate the
13631 current number of rows and columns in the frame while
13632 resizing; min_width and min_height aren't useful for this
13633 purpose, since they might not give the dimensions for a
13634 zero-row, zero-column frame.
58769bee 13635
0134a210
RS
13636 We use the base_width and base_height members if we have
13637 them; otherwise, we set the min_width and min_height members
13638 to the size for a zero x zero frame. */
b0342f17
JB
13639
13640#ifdef HAVE_X11R4
0134a210
RS
13641 size_hints.flags |= PBaseSize;
13642 size_hints.base_width = base_width;
13643 size_hints.base_height = base_height;
13644 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13645 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 13646#else
0134a210
RS
13647 size_hints.min_width = base_width;
13648 size_hints.min_height = base_height;
b0342f17 13649#endif
b1c884c3 13650 }
dc6f92b8 13651
d067ea8b 13652 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 13653 if (flags)
dc6f92b8 13654 {
d067ea8b
KH
13655 size_hints.flags |= flags;
13656 goto no_read;
13657 }
13658#endif /* not USE_X_TOOLKIT */
13659
13660 {
13661 XSizeHints hints; /* Sometimes I hate X Windows... */
13662 long supplied_return;
13663 int value;
af31d76f
RS
13664
13665#ifdef HAVE_X11R4
d067ea8b
KH
13666 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13667 &supplied_return);
af31d76f 13668#else
d067ea8b 13669 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 13670#endif
58769bee 13671
d067ea8b
KH
13672#ifdef USE_X_TOOLKIT
13673 size_hints.base_height = hints.base_height;
13674 size_hints.base_width = hints.base_width;
13675 size_hints.min_height = hints.min_height;
13676 size_hints.min_width = hints.min_width;
13677#endif
13678
13679 if (flags)
13680 size_hints.flags |= flags;
13681 else
13682 {
13683 if (value == 0)
13684 hints.flags = 0;
13685 if (hints.flags & PSize)
13686 size_hints.flags |= PSize;
13687 if (hints.flags & PPosition)
13688 size_hints.flags |= PPosition;
13689 if (hints.flags & USPosition)
13690 size_hints.flags |= USPosition;
13691 if (hints.flags & USSize)
13692 size_hints.flags |= USSize;
13693 }
13694 }
13695
06a2c219 13696#ifndef USE_X_TOOLKIT
d067ea8b 13697 no_read:
06a2c219 13698#endif
0134a210 13699
af31d76f 13700#ifdef PWinGravity
7556890b 13701 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 13702 size_hints.flags |= PWinGravity;
dc05a16b 13703
af31d76f 13704 if (user_position)
6dba1858 13705 {
af31d76f
RS
13706 size_hints.flags &= ~ PPosition;
13707 size_hints.flags |= USPosition;
6dba1858 13708 }
2554751d 13709#endif /* PWinGravity */
6dba1858 13710
b0342f17 13711#ifdef HAVE_X11R4
334208b7 13712 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13713#else
334208b7 13714 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13715#endif
dc6f92b8
JB
13716}
13717
13718/* Used for IconicState or NormalState */
06a2c219 13719
dfcf069d 13720void
f676886a
JB
13721x_wm_set_window_state (f, state)
13722 struct frame *f;
dc6f92b8
JB
13723 int state;
13724{
3afe33e7 13725#ifdef USE_X_TOOLKIT
546e6d5b
RS
13726 Arg al[1];
13727
13728 XtSetArg (al[0], XtNinitialState, state);
7556890b 13729 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 13730#else /* not USE_X_TOOLKIT */
c118dd06 13731 Window window = FRAME_X_WINDOW (f);
dc6f92b8 13732
7556890b
RS
13733 f->output_data.x->wm_hints.flags |= StateHint;
13734 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 13735
7556890b 13736 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 13737#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13738}
13739
dfcf069d 13740void
7f2ae036 13741x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 13742 struct frame *f;
7f2ae036 13743 int pixmap_id;
dc6f92b8 13744{
d2bd6bc4
RS
13745 Pixmap icon_pixmap;
13746
06a2c219 13747#ifndef USE_X_TOOLKIT
c118dd06 13748 Window window = FRAME_X_WINDOW (f);
75231bad 13749#endif
dc6f92b8 13750
7f2ae036 13751 if (pixmap_id > 0)
dbc4e1c1 13752 {
d2bd6bc4 13753 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 13754 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
13755 }
13756 else
68568555
RS
13757 {
13758 /* It seems there is no way to turn off use of an icon pixmap.
13759 The following line does it, only if no icon has yet been created,
13760 for some window managers. But with mwm it crashes.
13761 Some people say it should clear the IconPixmapHint bit in this case,
13762 but that doesn't work, and the X consortium said it isn't the
13763 right thing at all. Since there is no way to win,
13764 best to explicitly give up. */
13765#if 0
13766 f->output_data.x->wm_hints.icon_pixmap = None;
13767#else
13768 return;
13769#endif
13770 }
b1c884c3 13771
d2bd6bc4
RS
13772#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13773
13774 {
13775 Arg al[1];
13776 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13777 XtSetValues (f->output_data.x->widget, al, 1);
13778 }
13779
13780#else /* not USE_X_TOOLKIT */
13781
7556890b
RS
13782 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13783 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
13784
13785#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13786}
13787
dfcf069d 13788void
f676886a
JB
13789x_wm_set_icon_position (f, icon_x, icon_y)
13790 struct frame *f;
dc6f92b8
JB
13791 int icon_x, icon_y;
13792{
75231bad 13793#ifdef USE_X_TOOLKIT
7556890b 13794 Window window = XtWindow (f->output_data.x->widget);
75231bad 13795#else
c118dd06 13796 Window window = FRAME_X_WINDOW (f);
75231bad 13797#endif
dc6f92b8 13798
7556890b
RS
13799 f->output_data.x->wm_hints.flags |= IconPositionHint;
13800 f->output_data.x->wm_hints.icon_x = icon_x;
13801 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 13802
7556890b 13803 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
13804}
13805
13806\f
06a2c219
GM
13807/***********************************************************************
13808 Fonts
13809 ***********************************************************************/
dc43ef94
KH
13810
13811/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 13812
dc43ef94
KH
13813struct font_info *
13814x_get_font_info (f, font_idx)
13815 FRAME_PTR f;
13816 int font_idx;
13817{
13818 return (FRAME_X_FONT_TABLE (f) + font_idx);
13819}
13820
13821
9c11f79e
GM
13822/* Return a list of names of available fonts matching PATTERN on frame F.
13823
13824 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13825 to be listed.
13826
13827 SIZE < 0 means include scalable fonts.
13828
13829 Frame F null means we have not yet created any frame on X, and
13830 consult the first display in x_display_list. MAXNAMES sets a limit
13831 on how many fonts to match. */
dc43ef94
KH
13832
13833Lisp_Object
13834x_list_fonts (f, pattern, size, maxnames)
9c11f79e 13835 struct frame *f;
dc43ef94
KH
13836 Lisp_Object pattern;
13837 int size;
13838 int maxnames;
13839{
06a2c219
GM
13840 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13841 Lisp_Object tem, second_best;
9c11f79e
GM
13842 struct x_display_info *dpyinfo
13843 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13844 Display *dpy = dpyinfo->display;
09c6077f 13845 int try_XLoadQueryFont = 0;
53ca4657 13846 int count;
9c11f79e
GM
13847 int allow_scalable_fonts_p = 0;
13848
13849 if (size < 0)
13850 {
13851 allow_scalable_fonts_p = 1;
13852 size = 0;
13853 }
dc43ef94 13854
6b0efe73 13855 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
13856 if (NILP (patterns))
13857 patterns = Fcons (pattern, Qnil);
81ba44e5 13858
09c6077f
KH
13859 if (maxnames == 1 && !size)
13860 /* We can return any single font matching PATTERN. */
13861 try_XLoadQueryFont = 1;
9a32686f 13862
8e713be6 13863 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 13864 {
dc43ef94 13865 int num_fonts;
3e71d8f2 13866 char **names = NULL;
dc43ef94 13867
8e713be6 13868 pattern = XCAR (patterns);
536f4067
RS
13869 /* See if we cached the result for this particular query.
13870 The cache is an alist of the form:
9c11f79e
GM
13871 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13872 tem = XCDR (dpyinfo->name_list_element);
13873 key = Fcons (Fcons (pattern, make_number (maxnames)),
13874 allow_scalable_fonts_p ? Qt : Qnil);
13875 list = Fassoc (key, tem);
13876 if (!NILP (list))
b5210ea7
KH
13877 {
13878 list = Fcdr_safe (list);
13879 /* We have a cashed list. Don't have to get the list again. */
13880 goto label_cached;
13881 }
13882
13883 /* At first, put PATTERN in the cache. */
09c6077f 13884
dc43ef94 13885 BLOCK_INPUT;
17d85edc
KH
13886 count = x_catch_errors (dpy);
13887
09c6077f
KH
13888 if (try_XLoadQueryFont)
13889 {
13890 XFontStruct *font;
13891 unsigned long value;
13892
13893 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
13894 if (x_had_errors_p (dpy))
13895 {
13896 /* This error is perhaps due to insufficient memory on X
13897 server. Let's just ignore it. */
13898 font = NULL;
13899 x_clear_errors (dpy);
13900 }
13901
09c6077f
KH
13902 if (font
13903 && XGetFontProperty (font, XA_FONT, &value))
13904 {
13905 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13906 int len = strlen (name);
01c752b5 13907 char *tmp;
09c6077f 13908
6f6512e8
KH
13909 /* If DXPC (a Differential X Protocol Compressor)
13910 Ver.3.7 is running, XGetAtomName will return null
13911 string. We must avoid such a name. */
13912 if (len == 0)
13913 try_XLoadQueryFont = 0;
13914 else
13915 {
13916 num_fonts = 1;
13917 names = (char **) alloca (sizeof (char *));
13918 /* Some systems only allow alloca assigned to a
13919 simple var. */
13920 tmp = (char *) alloca (len + 1); names[0] = tmp;
13921 bcopy (name, names[0], len + 1);
13922 XFree (name);
13923 }
09c6077f
KH
13924 }
13925 else
13926 try_XLoadQueryFont = 0;
a083fd23
RS
13927
13928 if (font)
13929 XFreeFont (dpy, font);
09c6077f
KH
13930 }
13931
13932 if (!try_XLoadQueryFont)
17d85edc
KH
13933 {
13934 /* We try at least 10 fonts because XListFonts will return
13935 auto-scaled fonts at the head. */
13936 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13937 &num_fonts);
13938 if (x_had_errors_p (dpy))
13939 {
13940 /* This error is perhaps due to insufficient memory on X
13941 server. Let's just ignore it. */
13942 names = NULL;
13943 x_clear_errors (dpy);
13944 }
13945 }
13946
13947 x_uncatch_errors (dpy, count);
dc43ef94
KH
13948 UNBLOCK_INPUT;
13949
13950 if (names)
13951 {
13952 int i;
dc43ef94
KH
13953
13954 /* Make a list of all the fonts we got back.
13955 Store that in the font cache for the display. */
13956 for (i = 0; i < num_fonts; i++)
13957 {
06a2c219 13958 int width = 0;
dc43ef94 13959 char *p = names[i];
06a2c219
GM
13960 int average_width = -1, dashes = 0;
13961
dc43ef94 13962 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
13963 14 dashes, and the field value following 12th dash
13964 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13965 is usually too ugly to be used for editing. Let's
13966 ignore it. */
dc43ef94
KH
13967 while (*p)
13968 if (*p++ == '-')
13969 {
13970 dashes++;
13971 if (dashes == 7) /* PIXEL_SIZE field */
13972 width = atoi (p);
13973 else if (dashes == 12) /* AVERAGE_WIDTH field */
13974 average_width = atoi (p);
13975 }
9c11f79e
GM
13976
13977 if (allow_scalable_fonts_p
13978 || dashes < 14 || average_width != 0)
dc43ef94
KH
13979 {
13980 tem = build_string (names[i]);
13981 if (NILP (Fassoc (tem, list)))
13982 {
13983 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
13984 && ((fast_c_string_match_ignore_case
13985 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
13986 >= 0))
13987 /* We can set the value of PIXEL_SIZE to the
b5210ea7 13988 width of this font. */
dc43ef94
KH
13989 list = Fcons (Fcons (tem, make_number (width)), list);
13990 else
13991 /* For the moment, width is not known. */
13992 list = Fcons (Fcons (tem, Qnil), list);
13993 }
13994 }
13995 }
e38f4136 13996
09c6077f 13997 if (!try_XLoadQueryFont)
e38f4136
GM
13998 {
13999 BLOCK_INPUT;
14000 XFreeFontNames (names);
14001 UNBLOCK_INPUT;
14002 }
dc43ef94
KH
14003 }
14004
b5210ea7 14005 /* Now store the result in the cache. */
f3fbd155
KR
14006 XSETCDR (dpyinfo->name_list_element,
14007 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 14008
b5210ea7
KH
14009 label_cached:
14010 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 14011
b5210ea7
KH
14012 newlist = second_best = Qnil;
14013 /* Make a list of the fonts that have the right width. */
8e713be6 14014 for (; CONSP (list); list = XCDR (list))
b5210ea7 14015 {
536f4067
RS
14016 int found_size;
14017
8e713be6 14018 tem = XCAR (list);
dc43ef94 14019
8e713be6 14020 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
14021 continue;
14022 if (!size)
14023 {
8e713be6 14024 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
14025 continue;
14026 }
dc43ef94 14027
8e713be6 14028 if (!INTEGERP (XCDR (tem)))
dc43ef94 14029 {
b5210ea7 14030 /* Since we have not yet known the size of this font, we
9c11f79e 14031 must try slow function call XLoadQueryFont. */
dc43ef94
KH
14032 XFontStruct *thisinfo;
14033
14034 BLOCK_INPUT;
17d85edc 14035 count = x_catch_errors (dpy);
dc43ef94 14036 thisinfo = XLoadQueryFont (dpy,
8e713be6 14037 XSTRING (XCAR (tem))->data);
17d85edc
KH
14038 if (x_had_errors_p (dpy))
14039 {
14040 /* This error is perhaps due to insufficient memory on X
14041 server. Let's just ignore it. */
14042 thisinfo = NULL;
14043 x_clear_errors (dpy);
14044 }
14045 x_uncatch_errors (dpy, count);
dc43ef94
KH
14046 UNBLOCK_INPUT;
14047
14048 if (thisinfo)
14049 {
f3fbd155
KR
14050 XSETCDR (tem,
14051 (thisinfo->min_bounds.width == 0
14052 ? make_number (0)
14053 : make_number (thisinfo->max_bounds.width)));
e38f4136 14054 BLOCK_INPUT;
dc43ef94 14055 XFreeFont (dpy, thisinfo);
e38f4136 14056 UNBLOCK_INPUT;
dc43ef94
KH
14057 }
14058 else
b5210ea7 14059 /* For unknown reason, the previous call of XListFont had
06a2c219 14060 returned a font which can't be opened. Record the size
b5210ea7 14061 as 0 not to try to open it again. */
f3fbd155 14062 XSETCDR (tem, make_number (0));
dc43ef94 14063 }
536f4067 14064
8e713be6 14065 found_size = XINT (XCDR (tem));
536f4067 14066 if (found_size == size)
8e713be6 14067 newlist = Fcons (XCAR (tem), newlist);
536f4067 14068 else if (found_size > 0)
b5210ea7 14069 {
536f4067 14070 if (NILP (second_best))
b5210ea7 14071 second_best = tem;
536f4067
RS
14072 else if (found_size < size)
14073 {
8e713be6
KR
14074 if (XINT (XCDR (second_best)) > size
14075 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
14076 second_best = tem;
14077 }
14078 else
14079 {
8e713be6
KR
14080 if (XINT (XCDR (second_best)) > size
14081 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
14082 second_best = tem;
14083 }
b5210ea7
KH
14084 }
14085 }
14086 if (!NILP (newlist))
14087 break;
14088 else if (!NILP (second_best))
14089 {
8e713be6 14090 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 14091 break;
dc43ef94 14092 }
dc43ef94
KH
14093 }
14094
14095 return newlist;
14096}
14097
06a2c219
GM
14098
14099#if GLYPH_DEBUG
14100
14101/* Check that FONT is valid on frame F. It is if it can be found in F's
14102 font table. */
14103
14104static void
14105x_check_font (f, font)
14106 struct frame *f;
14107 XFontStruct *font;
14108{
14109 int i;
14110 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14111
14112 xassert (font != NULL);
14113
14114 for (i = 0; i < dpyinfo->n_fonts; i++)
14115 if (dpyinfo->font_table[i].name
14116 && font == dpyinfo->font_table[i].font)
14117 break;
14118
14119 xassert (i < dpyinfo->n_fonts);
14120}
14121
14122#endif /* GLYPH_DEBUG != 0 */
14123
14124/* Set *W to the minimum width, *H to the minimum font height of FONT.
14125 Note: There are (broken) X fonts out there with invalid XFontStruct
14126 min_bounds contents. For example, handa@etl.go.jp reports that
14127 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14128 have font->min_bounds.width == 0. */
14129
14130static INLINE void
14131x_font_min_bounds (font, w, h)
14132 XFontStruct *font;
14133 int *w, *h;
14134{
14135 *h = FONT_HEIGHT (font);
14136 *w = font->min_bounds.width;
14137
14138 /* Try to handle the case where FONT->min_bounds has invalid
14139 contents. Since the only font known to have invalid min_bounds
14140 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14141 if (*w <= 0)
14142 *w = font->max_bounds.width;
14143}
14144
14145
14146/* Compute the smallest character width and smallest font height over
14147 all fonts available on frame F. Set the members smallest_char_width
14148 and smallest_font_height in F's x_display_info structure to
14149 the values computed. Value is non-zero if smallest_font_height or
14150 smallest_char_width become smaller than they were before. */
14151
14152static int
14153x_compute_min_glyph_bounds (f)
14154 struct frame *f;
14155{
14156 int i;
14157 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14158 XFontStruct *font;
14159 int old_width = dpyinfo->smallest_char_width;
14160 int old_height = dpyinfo->smallest_font_height;
14161
14162 dpyinfo->smallest_font_height = 100000;
14163 dpyinfo->smallest_char_width = 100000;
14164
14165 for (i = 0; i < dpyinfo->n_fonts; ++i)
14166 if (dpyinfo->font_table[i].name)
14167 {
14168 struct font_info *fontp = dpyinfo->font_table + i;
14169 int w, h;
14170
14171 font = (XFontStruct *) fontp->font;
14172 xassert (font != (XFontStruct *) ~0);
14173 x_font_min_bounds (font, &w, &h);
14174
14175 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14176 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14177 }
14178
14179 xassert (dpyinfo->smallest_char_width > 0
14180 && dpyinfo->smallest_font_height > 0);
14181
14182 return (dpyinfo->n_fonts == 1
14183 || dpyinfo->smallest_char_width < old_width
14184 || dpyinfo->smallest_font_height < old_height);
14185}
14186
14187
dc43ef94
KH
14188/* Load font named FONTNAME of the size SIZE for frame F, and return a
14189 pointer to the structure font_info while allocating it dynamically.
14190 If SIZE is 0, load any size of font.
14191 If loading is failed, return NULL. */
14192
14193struct font_info *
14194x_load_font (f, fontname, size)
14195 struct frame *f;
14196 register char *fontname;
14197 int size;
14198{
14199 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14200 Lisp_Object font_names;
d645aaa4 14201 int count;
dc43ef94
KH
14202
14203 /* Get a list of all the fonts that match this name. Once we
14204 have a list of matching fonts, we compare them against the fonts
14205 we already have by comparing names. */
09c6077f 14206 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
14207
14208 if (!NILP (font_names))
14209 {
14210 Lisp_Object tail;
14211 int i;
14212
14213 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 14214 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
14215 if (dpyinfo->font_table[i].name
14216 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 14217 XSTRING (XCAR (tail))->data)
06a2c219 14218 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 14219 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
14220 return (dpyinfo->font_table + i);
14221 }
14222
14223 /* Load the font and add it to the table. */
14224 {
14225 char *full_name;
14226 XFontStruct *font;
14227 struct font_info *fontp;
14228 unsigned long value;
06a2c219 14229 int i;
dc43ef94 14230
2da424f1
KH
14231 /* If we have found fonts by x_list_font, load one of them. If
14232 not, we still try to load a font by the name given as FONTNAME
14233 because XListFonts (called in x_list_font) of some X server has
14234 a bug of not finding a font even if the font surely exists and
14235 is loadable by XLoadQueryFont. */
e1d6d5b9 14236 if (size > 0 && !NILP (font_names))
8e713be6 14237 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
14238
14239 BLOCK_INPUT;
d645aaa4 14240 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 14241 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
14242 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14243 {
14244 /* This error is perhaps due to insufficient memory on X
14245 server. Let's just ignore it. */
14246 font = NULL;
14247 x_clear_errors (FRAME_X_DISPLAY (f));
14248 }
14249 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 14250 UNBLOCK_INPUT;
b5210ea7 14251 if (!font)
dc43ef94
KH
14252 return NULL;
14253
06a2c219
GM
14254 /* Find a free slot in the font table. */
14255 for (i = 0; i < dpyinfo->n_fonts; ++i)
14256 if (dpyinfo->font_table[i].name == NULL)
14257 break;
14258
14259 /* If no free slot found, maybe enlarge the font table. */
14260 if (i == dpyinfo->n_fonts
14261 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 14262 {
06a2c219
GM
14263 int sz;
14264 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14265 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 14266 dpyinfo->font_table
06a2c219 14267 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
14268 }
14269
06a2c219
GM
14270 fontp = dpyinfo->font_table + i;
14271 if (i == dpyinfo->n_fonts)
14272 ++dpyinfo->n_fonts;
dc43ef94
KH
14273
14274 /* Now fill in the slots of *FONTP. */
14275 BLOCK_INPUT;
14276 fontp->font = font;
06a2c219 14277 fontp->font_idx = i;
dc43ef94
KH
14278 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14279 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14280
14281 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14282 full_name = 0;
14283 if (XGetFontProperty (font, XA_FONT, &value))
14284 {
14285 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14286 char *p = name;
14287 int dashes = 0;
14288
14289 /* Count the number of dashes in the "full name".
14290 If it is too few, this isn't really the font's full name,
14291 so don't use it.
14292 In X11R4, the fonts did not come with their canonical names
14293 stored in them. */
14294 while (*p)
14295 {
14296 if (*p == '-')
14297 dashes++;
14298 p++;
14299 }
14300
14301 if (dashes >= 13)
14302 {
14303 full_name = (char *) xmalloc (p - name + 1);
14304 bcopy (name, full_name, p - name + 1);
14305 }
14306
14307 XFree (name);
14308 }
14309
14310 if (full_name != 0)
14311 fontp->full_name = full_name;
14312 else
14313 fontp->full_name = fontp->name;
14314
14315 fontp->size = font->max_bounds.width;
d5749adb 14316 fontp->height = FONT_HEIGHT (font);
dc43ef94 14317
2da424f1
KH
14318 if (NILP (font_names))
14319 {
14320 /* We come here because of a bug of XListFonts mentioned at
14321 the head of this block. Let's store this information in
14322 the cache for x_list_fonts. */
14323 Lisp_Object lispy_name = build_string (fontname);
14324 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
14325 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14326 Qnil);
2da424f1 14327
f3fbd155
KR
14328 XSETCDR (dpyinfo->name_list_element,
14329 Fcons (Fcons (key,
14330 Fcons (Fcons (lispy_full_name,
14331 make_number (fontp->size)),
14332 Qnil)),
14333 XCDR (dpyinfo->name_list_element)));
2da424f1 14334 if (full_name)
9c11f79e
GM
14335 {
14336 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14337 Qnil);
f3fbd155
KR
14338 XSETCDR (dpyinfo->name_list_element,
14339 Fcons (Fcons (key,
14340 Fcons (Fcons (lispy_full_name,
14341 make_number (fontp->size)),
14342 Qnil)),
14343 XCDR (dpyinfo->name_list_element)));
9c11f79e 14344 }
2da424f1
KH
14345 }
14346
dc43ef94
KH
14347 /* The slot `encoding' specifies how to map a character
14348 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
14349 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14350 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 14351 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 14352 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
14353 which is never used by any charset. If mapping can't be
14354 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
14355 fontp->encoding[1]
14356 = (font->max_byte1 == 0
14357 /* 1-byte font */
14358 ? (font->min_char_or_byte2 < 0x80
14359 ? (font->max_char_or_byte2 < 0x80
14360 ? 0 /* 0x20..0x7F */
8ff102bd 14361 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
14362 : 1) /* 0xA0..0xFF */
14363 /* 2-byte font */
14364 : (font->min_byte1 < 0x80
14365 ? (font->max_byte1 < 0x80
14366 ? (font->min_char_or_byte2 < 0x80
14367 ? (font->max_char_or_byte2 < 0x80
14368 ? 0 /* 0x2020..0x7F7F */
8ff102bd 14369 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 14370 : 3) /* 0x20A0..0x7FFF */
8ff102bd 14371 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
14372 : (font->min_char_or_byte2 < 0x80
14373 ? (font->max_char_or_byte2 < 0x80
14374 ? 2 /* 0xA020..0xFF7F */
8ff102bd 14375 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
14376 : 1))); /* 0xA0A0..0xFFFF */
14377
14378 fontp->baseline_offset
14379 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14380 ? (long) value : 0);
14381 fontp->relative_compose
14382 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14383 ? (long) value : 0);
f78798df
KH
14384 fontp->default_ascent
14385 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14386 ? (long) value : 0);
dc43ef94 14387
06a2c219
GM
14388 /* Set global flag fonts_changed_p to non-zero if the font loaded
14389 has a character with a smaller width than any other character
437dfb9f 14390 before, or if the font loaded has a smaller height than any
06a2c219
GM
14391 other font loaded before. If this happens, it will make a
14392 glyph matrix reallocation necessary. */
437dfb9f 14393 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 14394 UNBLOCK_INPUT;
dc43ef94
KH
14395 return fontp;
14396 }
14397}
14398
06a2c219
GM
14399
14400/* Return a pointer to struct font_info of a font named FONTNAME for
14401 frame F. If no such font is loaded, return NULL. */
14402
dc43ef94
KH
14403struct font_info *
14404x_query_font (f, fontname)
14405 struct frame *f;
14406 register char *fontname;
14407{
14408 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14409 int i;
14410
14411 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
14412 if (dpyinfo->font_table[i].name
14413 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14414 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
14415 return (dpyinfo->font_table + i);
14416 return NULL;
14417}
14418
06a2c219
GM
14419
14420/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
14421 `encoder' of the structure. */
14422
14423void
14424x_find_ccl_program (fontp)
14425 struct font_info *fontp;
14426{
a42f54e6 14427 Lisp_Object list, elt;
a6582676 14428
f9b5db02 14429 elt = Qnil;
8e713be6 14430 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 14431 {
8e713be6 14432 elt = XCAR (list);
a6582676 14433 if (CONSP (elt)
8e713be6 14434 && STRINGP (XCAR (elt))
9f2feff6
KH
14435 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14436 >= 0)
14437 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14438 >= 0)))
a42f54e6
KH
14439 break;
14440 }
f9b5db02 14441
a42f54e6
KH
14442 if (! NILP (list))
14443 {
d27f8ca7
KH
14444 struct ccl_program *ccl
14445 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 14446
8e713be6 14447 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
14448 xfree (ccl);
14449 else
14450 fontp->font_encoder = ccl;
a6582676
KH
14451 }
14452}
14453
06a2c219 14454
dc43ef94 14455\f
06a2c219
GM
14456/***********************************************************************
14457 Initialization
14458 ***********************************************************************/
f451eb13 14459
3afe33e7
RS
14460#ifdef USE_X_TOOLKIT
14461static XrmOptionDescRec emacs_options[] = {
14462 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14463 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14464
14465 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14466 XrmoptionSepArg, NULL},
14467 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14468
14469 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14470 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14471 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14472 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14473 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14474 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14475 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14476};
14477#endif /* USE_X_TOOLKIT */
14478
7a13e894
RS
14479static int x_initialized;
14480
29b38361
KH
14481#ifdef MULTI_KBOARD
14482/* Test whether two display-name strings agree up to the dot that separates
14483 the screen number from the server number. */
14484static int
14485same_x_server (name1, name2)
14486 char *name1, *name2;
14487{
14488 int seen_colon = 0;
cf591cc1
RS
14489 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14490 int system_name_length = strlen (system_name);
14491 int length_until_period = 0;
14492
14493 while (system_name[length_until_period] != 0
14494 && system_name[length_until_period] != '.')
14495 length_until_period++;
14496
14497 /* Treat `unix' like an empty host name. */
14498 if (! strncmp (name1, "unix:", 5))
14499 name1 += 4;
14500 if (! strncmp (name2, "unix:", 5))
14501 name2 += 4;
14502 /* Treat this host's name like an empty host name. */
14503 if (! strncmp (name1, system_name, system_name_length)
14504 && name1[system_name_length] == ':')
14505 name1 += system_name_length;
14506 if (! strncmp (name2, system_name, system_name_length)
14507 && name2[system_name_length] == ':')
14508 name2 += system_name_length;
14509 /* Treat this host's domainless name like an empty host name. */
14510 if (! strncmp (name1, system_name, length_until_period)
14511 && name1[length_until_period] == ':')
14512 name1 += length_until_period;
14513 if (! strncmp (name2, system_name, length_until_period)
14514 && name2[length_until_period] == ':')
14515 name2 += length_until_period;
14516
29b38361
KH
14517 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14518 {
14519 if (*name1 == ':')
14520 seen_colon++;
14521 if (seen_colon && *name1 == '.')
14522 return 1;
14523 }
14524 return (seen_colon
14525 && (*name1 == '.' || *name1 == '\0')
14526 && (*name2 == '.' || *name2 == '\0'));
14527}
14528#endif
14529
334208b7 14530struct x_display_info *
1f8255f2 14531x_term_init (display_name, xrm_option, resource_name)
334208b7 14532 Lisp_Object display_name;
1f8255f2
RS
14533 char *xrm_option;
14534 char *resource_name;
dc6f92b8 14535{
334208b7 14536 int connection;
7a13e894 14537 Display *dpy;
334208b7
RS
14538 struct x_display_info *dpyinfo;
14539 XrmDatabase xrdb;
14540
60439948
KH
14541 BLOCK_INPUT;
14542
7a13e894
RS
14543 if (!x_initialized)
14544 {
14545 x_initialize ();
14546 x_initialized = 1;
14547 }
dc6f92b8 14548
3afe33e7 14549#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
14550 /* weiner@footloose.sps.mot.com reports that this causes
14551 errors with X11R5:
14552 X protocol error: BadAtom (invalid Atom parameter)
14553 on protocol request 18skiloaf.
14554 So let's not use it until R6. */
14555#ifdef HAVE_X11XTR6
bdcd49ba
RS
14556 XtSetLanguageProc (NULL, NULL, NULL);
14557#endif
14558
7f9c7f94
RS
14559 {
14560 int argc = 0;
14561 char *argv[3];
14562
14563 argv[0] = "";
14564 argc = 1;
14565 if (xrm_option)
14566 {
14567 argv[argc++] = "-xrm";
14568 argv[argc++] = xrm_option;
14569 }
14570 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14571 resource_name, EMACS_CLASS,
14572 emacs_options, XtNumber (emacs_options),
14573 &argc, argv);
39d8bb4d
KH
14574
14575#ifdef HAVE_X11XTR6
10537cb1 14576 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 14577 fixup_locale ();
39d8bb4d 14578#endif
7f9c7f94 14579 }
3afe33e7
RS
14580
14581#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
14582#ifdef HAVE_X11R5
14583 XSetLocaleModifiers ("");
14584#endif
7a13e894 14585 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 14586#endif /* not USE_X_TOOLKIT */
334208b7 14587
7a13e894
RS
14588 /* Detect failure. */
14589 if (dpy == 0)
60439948
KH
14590 {
14591 UNBLOCK_INPUT;
14592 return 0;
14593 }
7a13e894
RS
14594
14595 /* We have definitely succeeded. Record the new connection. */
14596
14597 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 14598 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 14599
29b38361
KH
14600#ifdef MULTI_KBOARD
14601 {
14602 struct x_display_info *share;
14603 Lisp_Object tail;
14604
14605 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
14606 share = share->next, tail = XCDR (tail))
14607 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
14608 XSTRING (display_name)->data))
14609 break;
14610 if (share)
14611 dpyinfo->kboard = share->kboard;
14612 else
14613 {
14614 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14615 init_kboard (dpyinfo->kboard);
59e755be
KH
14616 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14617 {
14618 char *vendor = ServerVendor (dpy);
9b6ed9f3 14619 UNBLOCK_INPUT;
59e755be
KH
14620 dpyinfo->kboard->Vsystem_key_alist
14621 = call1 (Qvendor_specific_keysyms,
14622 build_string (vendor ? vendor : ""));
9b6ed9f3 14623 BLOCK_INPUT;
59e755be
KH
14624 }
14625
29b38361
KH
14626 dpyinfo->kboard->next_kboard = all_kboards;
14627 all_kboards = dpyinfo->kboard;
0ad5446c
KH
14628 /* Don't let the initial kboard remain current longer than necessary.
14629 That would cause problems if a file loaded on startup tries to
06a2c219 14630 prompt in the mini-buffer. */
0ad5446c
KH
14631 if (current_kboard == initial_kboard)
14632 current_kboard = dpyinfo->kboard;
29b38361
KH
14633 }
14634 dpyinfo->kboard->reference_count++;
14635 }
b9737ad3
KH
14636#endif
14637
7a13e894
RS
14638 /* Put this display on the chain. */
14639 dpyinfo->next = x_display_list;
14640 x_display_list = dpyinfo;
14641
14642 /* Put it on x_display_name_list as well, to keep them parallel. */
14643 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14644 x_display_name_list);
8e713be6 14645 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
14646
14647 dpyinfo->display = dpy;
dc6f92b8 14648
dc6f92b8 14649#if 0
7a13e894 14650 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 14651#endif /* ! 0 */
7a13e894
RS
14652
14653 dpyinfo->x_id_name
fc932ac6
RS
14654 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14655 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
14656 + 2);
14657 sprintf (dpyinfo->x_id_name, "%s@%s",
14658 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
14659
14660 /* Figure out which modifier bits mean what. */
334208b7 14661 x_find_modifier_meanings (dpyinfo);
f451eb13 14662
ab648270 14663 /* Get the scroll bar cursor. */
7a13e894 14664 dpyinfo->vertical_scroll_bar_cursor
334208b7 14665 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 14666
334208b7
RS
14667 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14668 resource_name, EMACS_CLASS);
14669#ifdef HAVE_XRMSETDATABASE
14670 XrmSetDatabase (dpyinfo->display, xrdb);
14671#else
14672 dpyinfo->display->db = xrdb;
14673#endif
547d9db8 14674 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
14675 all versions. */
14676 dpyinfo->xrdb = xrdb;
334208b7
RS
14677
14678 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14679 DefaultScreen (dpyinfo->display));
5ff67d81 14680 select_visual (dpyinfo);
43bd1b2b 14681 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
14682 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14683 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14684 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14685 dpyinfo->grabbed = 0;
14686 dpyinfo->reference_count = 0;
14687 dpyinfo->icon_bitmap_id = -1;
06a2c219 14688 dpyinfo->font_table = NULL;
7a13e894
RS
14689 dpyinfo->n_fonts = 0;
14690 dpyinfo->font_table_size = 0;
14691 dpyinfo->bitmaps = 0;
14692 dpyinfo->bitmaps_size = 0;
14693 dpyinfo->bitmaps_last = 0;
14694 dpyinfo->scratch_cursor_gc = 0;
14695 dpyinfo->mouse_face_mouse_frame = 0;
14696 dpyinfo->mouse_face_deferred_gc = 0;
14697 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14698 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 14699 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 14700 dpyinfo->mouse_face_window = Qnil;
0a61c667 14701 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
14702 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14703 dpyinfo->mouse_face_defer = 0;
66301061 14704 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
14705 dpyinfo->x_focus_frame = 0;
14706 dpyinfo->x_focus_event_frame = 0;
14707 dpyinfo->x_highlight_frame = 0;
06a2c219 14708 dpyinfo->image_cache = make_image_cache ();
334208b7 14709
43bd1b2b 14710 /* See if a private colormap is requested. */
5ff67d81
GM
14711 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14712 {
14713 if (dpyinfo->visual->class == PseudoColor)
14714 {
14715 Lisp_Object value;
14716 value = display_x_get_resource (dpyinfo,
14717 build_string ("privateColormap"),
14718 build_string ("PrivateColormap"),
14719 Qnil, Qnil);
14720 if (STRINGP (value)
14721 && (!strcmp (XSTRING (value)->data, "true")
14722 || !strcmp (XSTRING (value)->data, "on")))
14723 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14724 }
43bd1b2b 14725 }
5ff67d81
GM
14726 else
14727 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14728 dpyinfo->visual, AllocNone);
43bd1b2b 14729
06a2c219
GM
14730 {
14731 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14732 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14733 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14734 dpyinfo->resy = pixels * 25.4 / mm;
14735 pixels = DisplayWidth (dpyinfo->display, screen_number);
14736 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14737 dpyinfo->resx = pixels * 25.4 / mm;
14738 }
14739
334208b7
RS
14740 dpyinfo->Xatom_wm_protocols
14741 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14742 dpyinfo->Xatom_wm_take_focus
14743 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14744 dpyinfo->Xatom_wm_save_yourself
14745 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14746 dpyinfo->Xatom_wm_delete_window
14747 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14748 dpyinfo->Xatom_wm_change_state
14749 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14750 dpyinfo->Xatom_wm_configure_denied
14751 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14752 dpyinfo->Xatom_wm_window_moved
14753 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14754 dpyinfo->Xatom_editres
14755 = XInternAtom (dpyinfo->display, "Editres", False);
14756 dpyinfo->Xatom_CLIPBOARD
14757 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14758 dpyinfo->Xatom_TIMESTAMP
14759 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14760 dpyinfo->Xatom_TEXT
14761 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
14762 dpyinfo->Xatom_COMPOUND_TEXT
14763 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
14764 dpyinfo->Xatom_DELETE
14765 = XInternAtom (dpyinfo->display, "DELETE", False);
14766 dpyinfo->Xatom_MULTIPLE
14767 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14768 dpyinfo->Xatom_INCR
14769 = XInternAtom (dpyinfo->display, "INCR", False);
14770 dpyinfo->Xatom_EMACS_TMP
14771 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14772 dpyinfo->Xatom_TARGETS
14773 = XInternAtom (dpyinfo->display, "TARGETS", False);
14774 dpyinfo->Xatom_NULL
14775 = XInternAtom (dpyinfo->display, "NULL", False);
14776 dpyinfo->Xatom_ATOM_PAIR
14777 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
14778 /* For properties of font. */
14779 dpyinfo->Xatom_PIXEL_SIZE
14780 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14781 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14782 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14783 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14784 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
14785 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14786 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 14787
06a2c219
GM
14788 /* Ghostscript support. */
14789 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14790 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14791
14792 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14793 False);
14794
547d9db8
KH
14795 dpyinfo->cut_buffers_initialized = 0;
14796
334208b7
RS
14797 connection = ConnectionNumber (dpyinfo->display);
14798 dpyinfo->connection = connection;
14799
dc43ef94 14800 {
5d7cc324
RS
14801 char null_bits[1];
14802
14803 null_bits[0] = 0x00;
dc43ef94
KH
14804
14805 dpyinfo->null_pixel
14806 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14807 null_bits, 1, 1, (long) 0, (long) 0,
14808 1);
14809 }
14810
06a2c219
GM
14811 {
14812 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 14813 extern char *gray_bitmap_bits;
06a2c219
GM
14814 dpyinfo->gray
14815 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14816 gray_bitmap_bits,
14817 gray_bitmap_width, gray_bitmap_height,
14818 (unsigned long) 1, (unsigned long) 0, 1);
14819 }
14820
f5d11644
GM
14821#ifdef HAVE_X_I18N
14822 xim_initialize (dpyinfo, resource_name);
14823#endif
14824
87485d6f
MW
14825#ifdef subprocesses
14826 /* This is only needed for distinguishing keyboard and process input. */
334208b7 14827 if (connection != 0)
7a13e894 14828 add_keyboard_wait_descriptor (connection);
87485d6f 14829#endif
6d4238f3 14830
041b69ac 14831#ifndef F_SETOWN_BUG
dc6f92b8 14832#ifdef F_SETOWN
dc6f92b8 14833#ifdef F_SETOWN_SOCK_NEG
61c3ce62 14834 /* stdin is a socket here */
334208b7 14835 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 14836#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 14837 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
14838#endif /* ! defined (F_SETOWN_SOCK_NEG) */
14839#endif /* ! defined (F_SETOWN) */
041b69ac 14840#endif /* F_SETOWN_BUG */
dc6f92b8
JB
14841
14842#ifdef SIGIO
eee20f6a
KH
14843 if (interrupt_input)
14844 init_sigio (connection);
c118dd06 14845#endif /* ! defined (SIGIO) */
dc6f92b8 14846
51b592fb 14847#ifdef USE_LUCID
f8c39f51 14848#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
14849 /* Make sure that we have a valid font for dialog boxes
14850 so that Xt does not crash. */
14851 {
14852 Display *dpy = dpyinfo->display;
14853 XrmValue d, fr, to;
14854 Font font;
e99db5a1 14855 int count;
51b592fb
RS
14856
14857 d.addr = (XPointer)&dpy;
14858 d.size = sizeof (Display *);
14859 fr.addr = XtDefaultFont;
14860 fr.size = sizeof (XtDefaultFont);
14861 to.size = sizeof (Font *);
14862 to.addr = (XPointer)&font;
e99db5a1 14863 count = x_catch_errors (dpy);
51b592fb
RS
14864 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14865 abort ();
14866 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14867 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 14868 x_uncatch_errors (dpy, count);
51b592fb
RS
14869 }
14870#endif
f8c39f51 14871#endif
51b592fb 14872
34e23e5a
GM
14873 /* See if we should run in synchronous mode. This is useful
14874 for debugging X code. */
14875 {
14876 Lisp_Object value;
14877 value = display_x_get_resource (dpyinfo,
14878 build_string ("synchronous"),
14879 build_string ("Synchronous"),
14880 Qnil, Qnil);
14881 if (STRINGP (value)
14882 && (!strcmp (XSTRING (value)->data, "true")
14883 || !strcmp (XSTRING (value)->data, "on")))
14884 XSynchronize (dpyinfo->display, True);
14885 }
14886
60439948
KH
14887 UNBLOCK_INPUT;
14888
7a13e894
RS
14889 return dpyinfo;
14890}
14891\f
14892/* Get rid of display DPYINFO, assuming all frames are already gone,
14893 and without sending any more commands to the X server. */
dc6f92b8 14894
7a13e894
RS
14895void
14896x_delete_display (dpyinfo)
14897 struct x_display_info *dpyinfo;
14898{
14899 delete_keyboard_wait_descriptor (dpyinfo->connection);
14900
14901 /* Discard this display from x_display_name_list and x_display_list.
14902 We can't use Fdelq because that can quit. */
14903 if (! NILP (x_display_name_list)
8e713be6
KR
14904 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14905 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
14906 else
14907 {
14908 Lisp_Object tail;
14909
14910 tail = x_display_name_list;
8e713be6 14911 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 14912 {
bffcfca9 14913 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 14914 {
f3fbd155 14915 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
14916 break;
14917 }
8e713be6 14918 tail = XCDR (tail);
7a13e894
RS
14919 }
14920 }
14921
9bda743f
GM
14922 if (next_noop_dpyinfo == dpyinfo)
14923 next_noop_dpyinfo = dpyinfo->next;
14924
7a13e894
RS
14925 if (x_display_list == dpyinfo)
14926 x_display_list = dpyinfo->next;
7f9c7f94
RS
14927 else
14928 {
14929 struct x_display_info *tail;
7a13e894 14930
7f9c7f94
RS
14931 for (tail = x_display_list; tail; tail = tail->next)
14932 if (tail->next == dpyinfo)
14933 tail->next = tail->next->next;
14934 }
7a13e894 14935
0d777288
RS
14936#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14937#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
14938 XrmDestroyDatabase (dpyinfo->xrdb);
14939#endif
0d777288 14940#endif
29b38361
KH
14941#ifdef MULTI_KBOARD
14942 if (--dpyinfo->kboard->reference_count == 0)
39f79001 14943 delete_kboard (dpyinfo->kboard);
b9737ad3 14944#endif
f5d11644
GM
14945#ifdef HAVE_X_I18N
14946 if (dpyinfo->xim)
14947 xim_close_dpy (dpyinfo);
14948#endif
14949
b9737ad3
KH
14950 xfree (dpyinfo->font_table);
14951 xfree (dpyinfo->x_id_name);
f04e1297 14952 xfree (dpyinfo->color_cells);
b9737ad3 14953 xfree (dpyinfo);
7a13e894 14954}
f04e1297 14955
7a13e894
RS
14956\f
14957/* Set up use of X before we make the first connection. */
14958
06a2c219
GM
14959static struct redisplay_interface x_redisplay_interface =
14960{
14961 x_produce_glyphs,
14962 x_write_glyphs,
14963 x_insert_glyphs,
14964 x_clear_end_of_line,
14965 x_scroll_run,
14966 x_after_update_window_line,
14967 x_update_window_begin,
14968 x_update_window_end,
14969 XTcursor_to,
14970 x_flush,
71b8321e 14971 x_clear_mouse_face,
66ac4b0e
GM
14972 x_get_glyph_overhangs,
14973 x_fix_overlapping_area
06a2c219
GM
14974};
14975
dfcf069d 14976void
7a13e894
RS
14977x_initialize ()
14978{
06a2c219
GM
14979 rif = &x_redisplay_interface;
14980
14981 clear_frame_hook = x_clear_frame;
14982 ins_del_lines_hook = x_ins_del_lines;
06a2c219 14983 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
14984 ring_bell_hook = XTring_bell;
14985 reset_terminal_modes_hook = XTreset_terminal_modes;
14986 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
14987 update_begin_hook = x_update_begin;
14988 update_end_hook = x_update_end;
dc6f92b8
JB
14989 set_terminal_window_hook = XTset_terminal_window;
14990 read_socket_hook = XTread_socket;
b8009dd1 14991 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 14992 mouse_position_hook = XTmouse_position;
f451eb13 14993 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 14994 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
14995 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14996 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14997 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14998 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 14999 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 15000
f676886a 15001 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 15002 char_ins_del_ok = 1;
dc6f92b8
JB
15003 line_ins_del_ok = 1; /* we'll just blt 'em */
15004 fast_clear_end_of_line = 1; /* X does this well */
58769bee 15005 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
15006 off the bottom */
15007 baud_rate = 19200;
15008
7a13e894 15009 x_noop_count = 0;
9ea173e8 15010 last_tool_bar_item = -1;
06a2c219
GM
15011 any_help_event_p = 0;
15012
b30b24cb
RS
15013 /* Try to use interrupt input; if we can't, then start polling. */
15014 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15015
7f9c7f94
RS
15016#ifdef USE_X_TOOLKIT
15017 XtToolkitInitialize ();
651f03b6 15018
7f9c7f94 15019 Xt_app_con = XtCreateApplicationContext ();
651f03b6
GM
15020
15021 /* Register a converter from strings to pixels, which uses
15022 Emacs' color allocation infrastructure. */
15023 XtAppSetTypeConverter (Xt_app_con,
15024 XtRString, XtRPixel, cvt_string_to_pixel,
15025 cvt_string_to_pixel_args,
15026 XtNumber (cvt_string_to_pixel_args),
15027 XtCacheByDisplay, cvt_pixel_dtor);
15028
665881ad 15029 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
15030
15031 /* Install an asynchronous timer that processes Xt timeout events
15032 every 0.1s. This is necessary because some widget sets use
15033 timeouts internally, for example the LessTif menu bar, or the
15034 Xaw3d scroll bar. When Xt timouts aren't processed, these
15035 widgets don't behave normally. */
15036 {
15037 EMACS_TIME interval;
15038 EMACS_SET_SECS_USECS (interval, 0, 100000);
15039 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15040 }
db74249b 15041#endif
bffcfca9 15042
eccc05db 15043#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
15044 xaw3d_arrow_scroll = False;
15045 xaw3d_pick_top = True;
7f9c7f94
RS
15046#endif
15047
58769bee 15048 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 15049 original error handler. */
e99db5a1 15050 XSetErrorHandler (x_error_handler);
334208b7 15051 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 15052
06a2c219 15053 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
15054#ifdef SIGWINCH
15055 signal (SIGWINCH, SIG_DFL);
c118dd06 15056#endif /* ! defined (SIGWINCH) */
dc6f92b8 15057
92e2441b 15058 signal (SIGPIPE, x_connection_signal);
dc6f92b8 15059}
55123275 15060
06a2c219 15061
55123275
JB
15062void
15063syms_of_xterm ()
15064{
e99db5a1
RS
15065 staticpro (&x_error_message_string);
15066 x_error_message_string = Qnil;
15067
7a13e894
RS
15068 staticpro (&x_display_name_list);
15069 x_display_name_list = Qnil;
334208b7 15070
ab648270 15071 staticpro (&last_mouse_scroll_bar);
e53cb100 15072 last_mouse_scroll_bar = Qnil;
59e755be
KH
15073
15074 staticpro (&Qvendor_specific_keysyms);
15075 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
15076
15077 staticpro (&last_mouse_press_frame);
15078 last_mouse_press_frame = Qnil;
06a2c219 15079
06a2c219 15080 help_echo = Qnil;
be010514
GM
15081 staticpro (&help_echo);
15082 help_echo_object = Qnil;
15083 staticpro (&help_echo_object);
7cea38bc
GM
15084 help_echo_window = Qnil;
15085 staticpro (&help_echo_window);
06a2c219 15086 previous_help_echo = Qnil;
be010514
GM
15087 staticpro (&previous_help_echo);
15088 help_echo_pos = -1;
06a2c219 15089
503e457e
PJ
15090 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
15091 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15092 x_autoselect_window_p = 0;
15093
7ee72033
MB
15094 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15095 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
228299fa
GM
15096For example, if a block cursor is over a tab, it will be drawn as
15097wide as that tab on the display. */);
06a2c219
GM
15098 x_stretch_cursor_p = 0;
15099
a72d5ce5 15100 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
15101 &x_use_underline_position_properties,
15102 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 15103nil means ignore them. If you encounter fonts with bogus
228299fa
GM
15104UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15105to 4.1, set this to nil. */);
a72d5ce5
GM
15106 x_use_underline_position_properties = 1;
15107
7ee72033
MB
15108 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15109 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
15110A value of nil means Emacs doesn't use X toolkit scroll bars.
15111Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 15112#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
15113#ifdef USE_MOTIF
15114 Vx_toolkit_scroll_bars = intern ("motif");
15115#elif defined HAVE_XAW3D
15116 Vx_toolkit_scroll_bars = intern ("xaw3d");
15117#else
15118 Vx_toolkit_scroll_bars = intern ("xaw");
15119#endif
06a2c219 15120#else
5bf04520 15121 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
15122#endif
15123
06a2c219
GM
15124 staticpro (&last_mouse_motion_frame);
15125 last_mouse_motion_frame = Qnil;
98659da6
KG
15126
15127 Qmodifier_value = intern ("modifier-value");
15128 Qalt = intern ("alt");
15129 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15130 Qhyper = intern ("hyper");
15131 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15132 Qmeta = intern ("meta");
15133 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15134 Qsuper = intern ("super");
15135 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15136
15137 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15138 doc: /* Which keys Emacs uses for the alt modifier.
15139This should be one of the symbols `alt', `hyper', `meta', `super'.
15140For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15141is nil, which is the same as `alt'. */);
15142 Vx_alt_keysym = Qnil;
15143
15144 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15145 doc: /* Which keys Emacs uses for the hyper modifier.
15146This should be one of the symbols `alt', `hyper', `meta', `super'.
15147For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15148default is nil, which is the same as `hyper'. */);
15149 Vx_hyper_keysym = Qnil;
15150
15151 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15152 doc: /* Which keys Emacs uses for the meta modifier.
15153This should be one of the symbols `alt', `hyper', `meta', `super'.
15154For example, `meta' means use the Meta_L and Meta_R keysyms. The
15155default is nil, which is the same as `meta'. */);
15156 Vx_meta_keysym = Qnil;
15157
15158 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15159 doc: /* Which keys Emacs uses for the super modifier.
15160This should be one of the symbols `alt', `hyper', `meta', `super'.
15161For example, `super' means use the Super_L and Super_R keysyms. The
15162default is nil, which is the same as `super'. */);
15163 Vx_super_keysym = Qnil;
15164
55123275 15165}
6cf0ae86 15166
1d6c120a 15167#endif /* HAVE_X_WINDOWS */