(struct x_output): Add focus_state.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
abfb6b46 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
dc6f92b8 79#include "gnu.h"
dc6f92b8 80#include "disptab.h"
dc6f92b8 81#include "buffer.h"
f451eb13 82#include "window.h"
3b2fa4e6 83#include "keyboard.h"
bde7c500 84#include "intervals.h"
dfcf069d 85#include "process.h"
bffcfca9 86#include "atimer.h"
8feddab4 87#include "keymap.h"
dc6f92b8 88
d2bd6bc4
RS
89#ifdef USE_X_TOOLKIT
90#include <X11/Shell.h>
91#endif
92
06a2c219
GM
93#ifdef HAVE_SYS_TIME_H
94#include <sys/time.h>
95#endif
96#ifdef HAVE_UNISTD_H
97#include <unistd.h>
98#endif
99
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
ee8ceff8 258int mouse_autoselect_window;
5bc62483 259
ee8ceff8 260/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
5bc62483 261static Lisp_Object last_window;
503e457e 262
06a2c219
GM
263/* Non-zero means draw block and hollow cursor as wide as the glyph
264 under it. For example, if a block cursor is over a tab, it will be
265 drawn as wide as that tab on the display. */
266
267int x_stretch_cursor_p;
268
a72d5ce5
GM
269/* Non-zero means make use of UNDERLINE_POSITION font properties. */
270
271int x_use_underline_position_properties;
272
06a2c219
GM
273/* This is a chain of structures for all the X displays currently in
274 use. */
275
334208b7 276struct x_display_info *x_display_list;
dc6f92b8 277
06a2c219
GM
278/* This is a list of cons cells, each of the form (NAME
279 . FONT-LIST-CACHE), one for each element of x_display_list and in
280 the same order. NAME is the name of the frame. FONT-LIST-CACHE
281 records previous values returned by x-list-fonts. */
282
7a13e894 283Lisp_Object x_display_name_list;
f451eb13 284
987d2ad1 285/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
286 This is set by update_begin and looked at by all the XT functions.
287 It is zero while not inside an update. In that case, the XT
288 functions assume that `selected_frame' is the frame to apply to. */
289
d0386f2a 290extern struct frame *updating_frame;
dc6f92b8 291
06a2c219
GM
292/* This is a frame waiting to be auto-raised, within XTread_socket. */
293
0134a210
RS
294struct frame *pending_autoraise_frame;
295
7f9c7f94
RS
296#ifdef USE_X_TOOLKIT
297/* The application context for Xt use. */
298XtAppContext Xt_app_con;
06a2c219
GM
299static String Xt_default_resources[] = {0};
300#endif /* USE_X_TOOLKIT */
665881ad 301
06a2c219
GM
302/* Nominal cursor position -- where to draw output.
303 HPOS and VPOS are window relative glyph matrix coordinates.
304 X and Y are window relative pixel coordinates. */
dc6f92b8 305
06a2c219 306struct cursor_pos output_cursor;
dc6f92b8 307
bffcfca9
GM
308/* Non-zero means user is interacting with a toolkit scroll bar. */
309
310static int toolkit_scroll_bar_interaction;
dc6f92b8 311
69388238
RS
312/* Mouse movement.
313
06a2c219 314 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
315 so that we would have to call XQueryPointer after each MotionNotify
316 event to ask for another such event. However, this made mouse tracking
317 slow, and there was a bug that made it eventually stop.
318
319 Simply asking for MotionNotify all the time seems to work better.
320
69388238
RS
321 In order to avoid asking for motion events and then throwing most
322 of them away or busy-polling the server for mouse positions, we ask
323 the server for pointer motion hints. This means that we get only
324 one event per group of mouse movements. "Groups" are delimited by
325 other kinds of events (focus changes and button clicks, for
326 example), or by XQueryPointer calls; when one of these happens, we
327 get another MotionNotify event the next time the mouse moves. This
328 is at least as efficient as getting motion events when mouse
329 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 330 is off. */
69388238
RS
331
332/* Where the mouse was last time we reported a mouse event. */
69388238 333
06a2c219
GM
334FRAME_PTR last_mouse_frame;
335static XRectangle last_mouse_glyph;
2237cac9
RS
336static Lisp_Object last_mouse_press_frame;
337
69388238
RS
338/* The scroll bar in which the last X motion event occurred.
339
06a2c219
GM
340 If the last X motion event occurred in a scroll bar, we set this so
341 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
342 an ordinary motion.
343
06a2c219
GM
344 If the last X motion event didn't occur in a scroll bar, we set
345 this to Qnil, to tell XTmouse_position to return an ordinary motion
346 event. */
347
69388238
RS
348static Lisp_Object last_mouse_scroll_bar;
349
69388238
RS
350/* This is a hack. We would really prefer that XTmouse_position would
351 return the time associated with the position it returns, but there
06a2c219 352 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
353 along with the position query. So, we just keep track of the time
354 of the last movement we received, and return that in hopes that
355 it's somewhat accurate. */
06a2c219 356
69388238
RS
357static Time last_mouse_movement_time;
358
06a2c219
GM
359/* Incremented by XTread_socket whenever it really tries to read
360 events. */
361
c0a04927
RS
362#ifdef __STDC__
363static int volatile input_signal_count;
364#else
365static int input_signal_count;
366#endif
367
7a13e894 368/* Used locally within XTread_socket. */
06a2c219 369
7a13e894 370static int x_noop_count;
dc6f92b8 371
7a13e894 372/* Initial values of argv and argc. */
06a2c219 373
7a13e894
RS
374extern char **initial_argv;
375extern int initial_argc;
dc6f92b8 376
7a13e894 377extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 378
06a2c219 379/* Tells if a window manager is present or not. */
7a13e894
RS
380
381extern Lisp_Object Vx_no_window_manager;
dc6f92b8 382
c2df547c 383extern Lisp_Object Qface, Qmouse_face;
b8009dd1 384
dc6f92b8
JB
385extern int errno;
386
dfeccd2d 387/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 388
31ade731 389extern EMACS_INT extra_keyboard_modifiers;
64bb1782 390
98659da6
KG
391/* The keysyms to use for the various modifiers. */
392
393Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
394static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
395
59e755be 396static Lisp_Object Qvendor_specific_keysyms;
c997eae5 397static Lisp_Object Qlatin_1, Qutf_8;
59e755be 398
952291d9
GM
399extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
400extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 401
7a13e894 402
06a2c219
GM
403/* Enumeration for overriding/changing the face to use for drawing
404 glyphs in x_draw_glyphs. */
405
406enum draw_glyphs_face
407{
408 DRAW_NORMAL_TEXT,
409 DRAW_INVERSE_VIDEO,
410 DRAW_CURSOR,
411 DRAW_MOUSE_FACE,
412 DRAW_IMAGE_RAISED,
413 DRAW_IMAGE_SUNKEN
414};
415
b7f83f9e 416static int cursor_in_mouse_face_p P_ ((struct window *));
fa262c07 417static int clear_mouse_face P_ ((struct x_display_info *));
651f03b6 418static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
499b1844 419static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
651f03b6 420static const XColor *x_color_cells P_ ((Display *, int *));
71b8321e 421static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
422static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
423void x_delete_display P_ ((struct x_display_info *));
424static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
425 unsigned));
426static int fast_find_position P_ ((struct window *, int, int *, int *,
7e376260 427 int *, int *, Lisp_Object));
f9db2310
GM
428static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
429 int *, int *, int *, int *, int));
06a2c219
GM
430static void set_output_cursor P_ ((struct cursor_pos *));
431static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
f9db2310 432 int *, int *, int *, int));
b436feb6
GM
433static void note_mode_line_or_margin_highlight P_ ((struct window *, int,
434 int, int));
06a2c219 435static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
436static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
437static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
438static void show_mouse_face P_ ((struct x_display_info *,
439 enum draw_glyphs_face));
440static int x_io_error_quitter P_ ((Display *));
441int x_catch_errors P_ ((Display *));
442void x_uncatch_errors P_ ((Display *, int));
443void x_lower_frame P_ ((struct frame *));
444void x_scroll_bar_clear P_ ((struct frame *));
445int x_had_errors_p P_ ((Display *));
446void x_wm_set_size_hint P_ ((struct frame *, long, int));
447void x_raise_frame P_ ((struct frame *));
448void x_set_window_size P_ ((struct frame *, int, int, int));
449void x_wm_set_window_state P_ ((struct frame *, int));
450void x_wm_set_icon_pixmap P_ ((struct frame *, int));
451void x_initialize P_ ((void));
452static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
453static int x_compute_min_glyph_bounds P_ ((struct frame *));
454static void x_draw_phys_cursor_glyph P_ ((struct window *,
455 struct glyph_row *,
456 enum draw_glyphs_face));
457static void x_update_end P_ ((struct frame *));
458static void XTframe_up_to_date P_ ((struct frame *));
06a2c219
GM
459static void XTset_terminal_modes P_ ((void));
460static void XTreset_terminal_modes P_ ((void));
461static void XTcursor_to P_ ((int, int, int, int));
462static void x_write_glyphs P_ ((struct glyph *, int));
463static void x_clear_end_of_line P_ ((int));
464static void x_clear_frame P_ ((void));
465static void x_clear_cursor P_ ((struct window *));
466static void frame_highlight P_ ((struct frame *));
467static void frame_unhighlight P_ ((struct frame *));
468static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
469static void XTframe_rehighlight P_ ((struct frame *));
470static void x_frame_rehighlight P_ ((struct x_display_info *));
471static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
65c26775
EZ
472static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
473 enum text_cursor_kinds));
06a2c219
GM
474static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
475 XRectangle *));
476static void expose_frame P_ ((struct frame *, int, int, int, int));
82f053ab 477static int expose_window_tree P_ ((struct window *, XRectangle *));
a39202f6 478static int expose_window P_ ((struct window *, XRectangle *));
06a2c219
GM
479static void expose_area P_ ((struct window *, struct glyph_row *,
480 XRectangle *, enum glyph_row_area));
82f053ab 481static int expose_line P_ ((struct window *, struct glyph_row *,
06a2c219
GM
482 XRectangle *));
483static void x_update_cursor_in_window_tree P_ ((struct window *, int));
484static void x_update_window_cursor P_ ((struct window *, int));
485static void x_erase_phys_cursor P_ ((struct window *));
486void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
3f332ef3 487static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
976b73d7 488 enum fringe_bitmap_type, int left_p));
06a2c219
GM
489
490static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
491 GC, int));
492static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
3f332ef3 493static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
60626bab
GM
494static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
495 int, int, int, int));
06a2c219 496static void x_flush P_ ((struct frame *f));
952291d9
GM
497static void x_update_begin P_ ((struct frame *));
498static void x_update_window_begin P_ ((struct window *));
499static void x_draw_vertical_border P_ ((struct window *));
500static void x_after_update_window_line P_ ((struct glyph_row *));
501static INLINE void take_vertical_position_into_account P_ ((struct it *));
502static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
503static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
504static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
505 enum scroll_bar_part *,
506 Lisp_Object *, Lisp_Object *,
507 unsigned long *));
d0fa3e56
EZ
508static void x_check_fullscreen P_ ((struct frame *));
509static void x_check_fullscreen_move P_ ((struct frame *));
06a2c219
GM
510
511/* Flush display of frame F, or of all frames if F is null. */
512
513static void
514x_flush (f)
515 struct frame *f;
516{
517 BLOCK_INPUT;
518 if (f == NULL)
519 {
520 Lisp_Object rest, frame;
521 FOR_EACH_FRAME (rest, frame)
522 x_flush (XFRAME (frame));
523 }
524 else if (FRAME_X_P (f))
525 XFlush (FRAME_X_DISPLAY (f));
526 UNBLOCK_INPUT;
527}
528
dc6f92b8 529
06a2c219
GM
530/* Remove calls to XFlush by defining XFlush to an empty replacement.
531 Calls to XFlush should be unnecessary because the X output buffer
532 is flushed automatically as needed by calls to XPending,
533 XNextEvent, or XWindowEvent according to the XFlush man page.
534 XTread_socket calls XPending. Removing XFlush improves
535 performance. */
536
537#define XFlush(DISPLAY) (void) 0
b8009dd1 538
334208b7 539\f
06a2c219
GM
540/***********************************************************************
541 Debugging
542 ***********************************************************************/
543
9382638d 544#if 0
06a2c219
GM
545
546/* This is a function useful for recording debugging information about
547 the sequence of occurrences in this file. */
9382638d
KH
548
549struct record
550{
551 char *locus;
552 int type;
553};
554
555struct record event_record[100];
556
557int event_record_index;
558
559record_event (locus, type)
560 char *locus;
561 int type;
562{
563 if (event_record_index == sizeof (event_record) / sizeof (struct record))
564 event_record_index = 0;
565
566 event_record[event_record_index].locus = locus;
567 event_record[event_record_index].type = type;
568 event_record_index++;
569}
570
571#endif /* 0 */
06a2c219
GM
572
573
9382638d 574\f
334208b7
RS
575/* Return the struct x_display_info corresponding to DPY. */
576
577struct x_display_info *
578x_display_info_for_display (dpy)
579 Display *dpy;
580{
581 struct x_display_info *dpyinfo;
582
583 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
584 if (dpyinfo->display == dpy)
585 return dpyinfo;
16bd92ea 586
334208b7
RS
587 return 0;
588}
f451eb13 589
06a2c219
GM
590
591\f
592/***********************************************************************
593 Starting and ending an update
594 ***********************************************************************/
595
596/* Start an update of frame F. This function is installed as a hook
597 for update_begin, i.e. it is called when update_begin is called.
598 This function is called prior to calls to x_update_window_begin for
599 each window being updated. Currently, there is nothing to do here
600 because all interesting stuff is done on a window basis. */
dc6f92b8 601
dfcf069d 602static void
06a2c219 603x_update_begin (f)
f676886a 604 struct frame *f;
58769bee 605{
06a2c219
GM
606 /* Nothing to do. */
607}
dc6f92b8 608
dc6f92b8 609
06a2c219
GM
610/* Start update of window W. Set the global variable updated_window
611 to the window being updated and set output_cursor to the cursor
612 position of W. */
dc6f92b8 613
06a2c219
GM
614static void
615x_update_window_begin (w)
616 struct window *w;
617{
618 struct frame *f = XFRAME (WINDOW_FRAME (w));
619 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
620
621 updated_window = w;
622 set_output_cursor (&w->cursor);
b8009dd1 623
06a2c219 624 BLOCK_INPUT;
d1bc4182 625
06a2c219 626 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 627 {
514e4681 628 /* Don't do highlighting for mouse motion during the update. */
06a2c219 629 display_info->mouse_face_defer = 1;
37c2c98b 630
06a2c219
GM
631 /* If F needs to be redrawn, simply forget about any prior mouse
632 highlighting. */
9f67f20b 633 if (FRAME_GARBAGED_P (f))
06a2c219
GM
634 display_info->mouse_face_window = Qnil;
635
64f26cf5
GM
636#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
637 their mouse_face_p flag set, which means that they are always
638 unequal to rows in a desired matrix which never have that
639 flag set. So, rows containing mouse-face glyphs are never
640 scrolled, and we don't have to switch the mouse highlight off
641 here to prevent it from being scrolled. */
642
06a2c219
GM
643 /* Can we tell that this update does not affect the window
644 where the mouse highlight is? If so, no need to turn off.
645 Likewise, don't do anything if the frame is garbaged;
646 in that case, the frame's current matrix that we would use
647 is all wrong, and we will redisplay that line anyway. */
648 if (!NILP (display_info->mouse_face_window)
649 && w == XWINDOW (display_info->mouse_face_window))
514e4681 650 {
06a2c219 651 int i;
514e4681 652
06a2c219
GM
653 for (i = 0; i < w->desired_matrix->nrows; ++i)
654 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
655 break;
656
06a2c219
GM
657 if (i < w->desired_matrix->nrows)
658 clear_mouse_face (display_info);
514e4681 659 }
64f26cf5 660#endif /* 0 */
b8009dd1 661 }
6ccf47d1 662
dc6f92b8
JB
663 UNBLOCK_INPUT;
664}
665
06a2c219
GM
666
667/* Draw a vertical window border to the right of window W if W doesn't
668 have vertical scroll bars. */
669
dfcf069d 670static void
06a2c219
GM
671x_draw_vertical_border (w)
672 struct window *w;
58769bee 673{
06a2c219
GM
674 struct frame *f = XFRAME (WINDOW_FRAME (w));
675
676 /* Redraw borders between horizontally adjacent windows. Don't
677 do it for frames with vertical scroll bars because either the
678 right scroll bar of a window, or the left scroll bar of its
679 neighbor will suffice as a border. */
680 if (!WINDOW_RIGHTMOST_P (w)
681 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
682 {
683 int x0, x1, y0, y1;
dc6f92b8 684
06a2c219 685 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
3f332ef3 686 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
06a2c219
GM
687 y1 -= 1;
688
689 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
690 f->output_data.x->normal_gc, x1, y0, x1, y1);
691 }
692}
693
694
71b8321e
GM
695/* End update of window W (which is equal to updated_window).
696
697 Draw vertical borders between horizontally adjacent windows, and
698 display W's cursor if CURSOR_ON_P is non-zero.
699
700 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
701 glyphs in mouse-face were overwritten. In that case we have to
702 make sure that the mouse-highlight is properly redrawn.
703
704 W may be a menu bar pseudo-window in case we don't have X toolkit
705 support. Such windows don't have a cursor, so don't display it
706 here. */
06a2c219
GM
707
708static void
71b8321e 709x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 710 struct window *w;
71b8321e 711 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 712{
140330de
GM
713 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
714
06a2c219
GM
715 if (!w->pseudo_window_p)
716 {
717 BLOCK_INPUT;
71b8321e 718
06a2c219
GM
719 if (cursor_on_p)
720 x_display_and_set_cursor (w, 1, output_cursor.hpos,
721 output_cursor.vpos,
722 output_cursor.x, output_cursor.y);
71b8321e 723
06a2c219
GM
724 x_draw_vertical_border (w);
725 UNBLOCK_INPUT;
726 }
727
140330de
GM
728 /* If a row with mouse-face was overwritten, arrange for
729 XTframe_up_to_date to redisplay the mouse highlight. */
730 if (mouse_face_overwritten_p)
731 {
732 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
733 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
734 dpyinfo->mouse_face_window = Qnil;
735 }
736
06a2c219
GM
737 updated_window = NULL;
738}
dc6f92b8 739
dc6f92b8 740
06a2c219
GM
741/* End update of frame F. This function is installed as a hook in
742 update_end. */
743
744static void
745x_update_end (f)
746 struct frame *f;
747{
748 /* Mouse highlight may be displayed again. */
aa8bff2e 749 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 750
06a2c219 751 BLOCK_INPUT;
334208b7 752 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
753 UNBLOCK_INPUT;
754}
b8009dd1 755
06a2c219
GM
756
757/* This function is called from various places in xdisp.c whenever a
758 complete update has been performed. The global variable
759 updated_window is not available here. */
b8009dd1 760
dfcf069d 761static void
b8009dd1 762XTframe_up_to_date (f)
06a2c219 763 struct frame *f;
b8009dd1 764{
06a2c219 765 if (FRAME_X_P (f))
514e4681 766 {
06a2c219 767 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 768
06a2c219
GM
769 if (dpyinfo->mouse_face_deferred_gc
770 || f == dpyinfo->mouse_face_mouse_frame)
771 {
772 BLOCK_INPUT;
773 if (dpyinfo->mouse_face_mouse_frame)
774 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
775 dpyinfo->mouse_face_mouse_x,
776 dpyinfo->mouse_face_mouse_y);
777 dpyinfo->mouse_face_deferred_gc = 0;
778 UNBLOCK_INPUT;
779 }
514e4681 780 }
b8009dd1 781}
06a2c219
GM
782
783
784/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 785 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
786 before DESIRED_ROW is made current. The window being updated is
787 found in updated_window. This function It is called from
788 update_window_line only if it is known that there are differences
789 between bitmaps to be drawn between current row and DESIRED_ROW. */
790
791static void
792x_after_update_window_line (desired_row)
793 struct glyph_row *desired_row;
794{
795 struct window *w = updated_window;
ef253080 796 struct frame *f;
259cf6bc 797 int width, height;
06a2c219
GM
798
799 xassert (w);
800
801 if (!desired_row->mode_line_p && !w->pseudo_window_p)
802 {
803 BLOCK_INPUT;
3f332ef3 804 x_draw_row_fringe_bitmaps (w, desired_row);
ef253080
GM
805 UNBLOCK_INPUT;
806 }
807
808 /* When a window has disappeared, make sure that no rest of
809 full-width rows stays visible in the internal border. Could
810 check here if updated_window is the leftmost/rightmost window,
811 but I guess it's not worth doing since vertically split windows
812 are almost never used, internal border is rarely set, and the
813 overhead is very small. */
814 if (windows_or_buffers_changed
815 && desired_row->full_width_p
816 && (f = XFRAME (w->frame),
817 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
818 width != 0)
819 && (height = desired_row->visible_height,
820 height > 0))
ef253080 821 {
ef253080 822 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 823
ef253080
GM
824 /* Internal border is drawn below the tool bar. */
825 if (WINDOWP (f->tool_bar_window)
826 && w == XWINDOW (f->tool_bar_window))
827 y -= width;
06a2c219 828
ef253080
GM
829 BLOCK_INPUT;
830 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
831 0, y, width, height, False);
832 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
833 f->output_data.x->pixel_width - width,
834 y, width, height, False);
06a2c219
GM
835 UNBLOCK_INPUT;
836 }
837}
838
839
3f332ef3 840/* Draw the bitmap WHICH in one of the left or right fringes of
06a2c219
GM
841 window W. ROW is the glyph row for which to display the bitmap; it
842 determines the vertical position at which the bitmap has to be
843 drawn. */
844
845static void
976b73d7 846x_draw_fringe_bitmap (w, row, which, left_p)
06a2c219
GM
847 struct window *w;
848 struct glyph_row *row;
3f332ef3 849 enum fringe_bitmap_type which;
976b73d7 850 int left_p;
06a2c219
GM
851{
852 struct frame *f = XFRAME (WINDOW_FRAME (w));
853 Display *display = FRAME_X_DISPLAY (f);
854 Window window = FRAME_X_WINDOW (f);
855 int x, y, wd, h, dy;
976b73d7 856 int b1, b2;
b436feb6 857 unsigned char *bits = NULL;
06a2c219
GM
858 Pixmap pixmap;
859 GC gc = f->output_data.x->normal_gc;
860 struct face *face;
861 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
862
863 /* Must clip because of partially visible lines. */
864 x_clip_to_row (w, row, gc, 1);
865
976b73d7
KS
866 /* Convert row to frame coordinates. */
867 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
868
06a2c219
GM
869 switch (which)
870 {
976b73d7
KS
871 case NO_FRINGE_BITMAP:
872 wd = 0;
873 h = 0;
874 break;
875
06a2c219
GM
876 case LEFT_TRUNCATION_BITMAP:
877 wd = left_width;
878 h = left_height;
879 bits = left_bits;
06a2c219
GM
880 break;
881
882 case OVERLAY_ARROW_BITMAP:
976b73d7
KS
883 wd = ov_width;
884 h = ov_height;
06a2c219 885 bits = ov_bits;
06a2c219
GM
886 break;
887
888 case RIGHT_TRUNCATION_BITMAP:
889 wd = right_width;
890 h = right_height;
891 bits = right_bits;
06a2c219
GM
892 break;
893
894 case CONTINUED_LINE_BITMAP:
976b73d7
KS
895 wd = continued_width;
896 h = continued_height;
06a2c219 897 bits = continued_bits;
06a2c219
GM
898 break;
899
900 case CONTINUATION_LINE_BITMAP:
901 wd = continuation_width;
902 h = continuation_height;
903 bits = continuation_bits;
06a2c219
GM
904 break;
905
906 case ZV_LINE_BITMAP:
907 wd = zv_width;
976b73d7
KS
908 h = zv_height - (y % zv_period);
909 bits = zv_bits + (y % zv_period);
06a2c219
GM
910 break;
911
912 default:
913 abort ();
914 }
915
976b73d7
KS
916 /* Clip bitmap if too high. */
917 if (h > row->height)
918 h = row->height;
919
920 /* Set dy to the offset in the row to start drawing the bitmap. */
06a2c219
GM
921 dy = (row->height - h) / 2;
922
976b73d7
KS
923 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
924 PREPARE_FACE_FOR_DISPLAY (f, face);
925
926 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
927 the fringe. */
dbdc9702 928 b1 = b2 = -1;
976b73d7
KS
929 if (left_p)
930 {
931 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
932 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
933 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
934 - wd
935 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
936 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
937 {
938 /* If W has a vertical border to its left, don't draw over it. */
939 int border = ((XFASTINT (w->left) > 0
940 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
941 ? 1 : 0);
942 b1 = (window_box_left (w, -1)
943 - FRAME_X_LEFT_FRINGE_WIDTH (f)
944 + border);
945 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
946 }
947 }
948 else
949 {
950 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
951 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
952 x = (window_box_right (w, -1)
953 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
954 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
955 the fringe. */
956 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
957 {
958 b1 = window_box_right (w, -1);
959 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
960 }
961 }
962
963 if (b1 >= 0)
964 {
965 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
966
967 /* In case the same realized face is used for fringes and
968 for something displayed in the text (e.g. face `region' on
969 mono-displays, the fill style may have been changed to
970 FillSolid in x_draw_glyph_string_background. */
971 if (face->stipple)
972 XSetFillStyle (display, face->gc, FillOpaqueStippled);
973 else
974 XSetForeground (display, face->gc, face->background);
975
976 XFillRectangle (display, window, face->gc,
977 b1,
978 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
979 row->y)),
980 b2,
981 row->visible_height);
982 if (!face->stipple)
983 XSetForeground (display, face->gc, face->foreground);
984 }
985
dbdc9702
GM
986 if (which != NO_FRINGE_BITMAP)
987 {
988 /* Draw the bitmap. I believe these small pixmaps can be cached
989 by the server. */
990 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
991 face->foreground,
992 face->background, depth);
993 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
994 XFreePixmap (display, pixmap);
995 }
996
06a2c219
GM
997 XSetClipMask (display, gc, None);
998}
999
1000
3f332ef3 1001/* Draw fringe bitmaps for glyph row ROW on window W. Call this
06a2c219
GM
1002 function with input blocked. */
1003
1004static void
3f332ef3 1005x_draw_row_fringe_bitmaps (w, row)
06a2c219
GM
1006 struct window *w;
1007 struct glyph_row *row;
1008{
1009 struct frame *f = XFRAME (w->frame);
3f332ef3 1010 enum fringe_bitmap_type bitmap;
06a2c219
GM
1011
1012 xassert (interrupt_input_blocked);
1013
1014 /* If row is completely invisible, because of vscrolling, we
1015 don't have to draw anything. */
1016 if (row->visible_height <= 0)
1017 return;
1018
976b73d7
KS
1019 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1020 {
1021 /* Decide which bitmap to draw in the left fringe. */
1022 if (row->overlay_arrow_p)
1023 bitmap = OVERLAY_ARROW_BITMAP;
1024 else if (row->truncated_on_left_p)
1025 bitmap = LEFT_TRUNCATION_BITMAP;
1026 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1027 bitmap = CONTINUATION_LINE_BITMAP;
1028 else if (row->indicate_empty_line_p)
1029 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1030 else
976b73d7 1031 bitmap = NO_FRINGE_BITMAP;
06a2c219 1032
976b73d7
KS
1033 x_draw_fringe_bitmap (w, row, bitmap, 1);
1034 }
06a2c219 1035
976b73d7 1036 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
06a2c219 1037 {
976b73d7
KS
1038 /* Decide which bitmap to draw in the right fringe. */
1039 if (row->truncated_on_right_p)
1040 bitmap = RIGHT_TRUNCATION_BITMAP;
1041 else if (row->continued_p)
1042 bitmap = CONTINUED_LINE_BITMAP;
1043 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1044 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1045 else
976b73d7 1046 bitmap = NO_FRINGE_BITMAP;
06a2c219 1047
976b73d7
KS
1048 x_draw_fringe_bitmap (w, row, bitmap, 0);
1049 }
06a2c219
GM
1050}
1051
dc6f92b8 1052\f
06a2c219
GM
1053
1054/* This is called when starting Emacs and when restarting after
1055 suspend. When starting Emacs, no X window is mapped. And nothing
1056 must be done to Emacs's own window if it is suspended (though that
1057 rarely happens). */
dc6f92b8 1058
dfcf069d 1059static void
dc6f92b8
JB
1060XTset_terminal_modes ()
1061{
1062}
1063
06a2c219
GM
1064/* This is called when exiting or suspending Emacs. Exiting will make
1065 the X-windows go away, and suspending requires no action. */
dc6f92b8 1066
dfcf069d 1067static void
dc6f92b8
JB
1068XTreset_terminal_modes ()
1069{
dc6f92b8 1070}
06a2c219
GM
1071
1072
dc6f92b8 1073\f
06a2c219
GM
1074/***********************************************************************
1075 Output Cursor
1076 ***********************************************************************/
1077
1078/* Set the global variable output_cursor to CURSOR. All cursor
1079 positions are relative to updated_window. */
dc6f92b8 1080
dfcf069d 1081static void
06a2c219
GM
1082set_output_cursor (cursor)
1083 struct cursor_pos *cursor;
dc6f92b8 1084{
06a2c219
GM
1085 output_cursor.hpos = cursor->hpos;
1086 output_cursor.vpos = cursor->vpos;
1087 output_cursor.x = cursor->x;
1088 output_cursor.y = cursor->y;
1089}
1090
1091
1092/* Set a nominal cursor position.
dc6f92b8 1093
06a2c219
GM
1094 HPOS and VPOS are column/row positions in a window glyph matrix. X
1095 and Y are window text area relative pixel positions.
1096
1097 If this is done during an update, updated_window will contain the
1098 window that is being updated and the position is the future output
1099 cursor position for that window. If updated_window is null, use
1100 selected_window and display the cursor at the given position. */
1101
1102static void
1103XTcursor_to (vpos, hpos, y, x)
1104 int vpos, hpos, y, x;
1105{
1106 struct window *w;
1107
1108 /* If updated_window is not set, work on selected_window. */
1109 if (updated_window)
1110 w = updated_window;
1111 else
1112 w = XWINDOW (selected_window);
dbcb258a 1113
06a2c219
GM
1114 /* Set the output cursor. */
1115 output_cursor.hpos = hpos;
1116 output_cursor.vpos = vpos;
1117 output_cursor.x = x;
1118 output_cursor.y = y;
dc6f92b8 1119
06a2c219
GM
1120 /* If not called as part of an update, really display the cursor.
1121 This will also set the cursor position of W. */
1122 if (updated_window == NULL)
dc6f92b8
JB
1123 {
1124 BLOCK_INPUT;
06a2c219 1125 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1126 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1127 UNBLOCK_INPUT;
1128 }
1129}
dc43ef94 1130
06a2c219
GM
1131
1132\f
1133/***********************************************************************
1134 Display Iterator
1135 ***********************************************************************/
1136
1137/* Function prototypes of this page. */
1138
1139static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1140 struct glyph *,
ee569018
KH
1141 XChar2b *,
1142 int *));
06a2c219 1143static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
abfb6b46
GM
1144 int, XChar2b *, int,
1145 int));
06a2c219
GM
1146static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1147static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1148static void x_append_glyph P_ ((struct it *));
b4192550 1149static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1150static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1151 int, int, double));
1152static void x_produce_glyphs P_ ((struct it *));
06a2c219 1153static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1154
1155
e2ef8ee6
GM
1156/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1157 is not contained in the font. */
dc43ef94 1158
06a2c219 1159static INLINE XCharStruct *
ee569018 1160x_per_char_metric (font, char2b)
06a2c219
GM
1161 XFontStruct *font;
1162 XChar2b *char2b;
1163{
1164 /* The result metric information. */
1165 XCharStruct *pcm = NULL;
dc6f92b8 1166
06a2c219 1167 xassert (font && char2b);
dc6f92b8 1168
06a2c219 1169 if (font->per_char != NULL)
dc6f92b8 1170 {
06a2c219 1171 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1172 {
06a2c219
GM
1173 /* min_char_or_byte2 specifies the linear character index
1174 corresponding to the first element of the per_char array,
1175 max_char_or_byte2 is the index of the last character. A
1176 character with non-zero CHAR2B->byte1 is not in the font.
1177 A character with byte2 less than min_char_or_byte2 or
1178 greater max_char_or_byte2 is not in the font. */
1179 if (char2b->byte1 == 0
1180 && char2b->byte2 >= font->min_char_or_byte2
1181 && char2b->byte2 <= font->max_char_or_byte2)
1182 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1183 }
06a2c219 1184 else
dc6f92b8 1185 {
06a2c219
GM
1186 /* If either min_byte1 or max_byte1 are nonzero, both
1187 min_char_or_byte2 and max_char_or_byte2 are less than
1188 256, and the 2-byte character index values corresponding
1189 to the per_char array element N (counting from 0) are:
1190
1191 byte1 = N/D + min_byte1
1192 byte2 = N\D + min_char_or_byte2
1193
1194 where:
1195
1196 D = max_char_or_byte2 - min_char_or_byte2 + 1
1197 / = integer division
1198 \ = integer modulus */
1199 if (char2b->byte1 >= font->min_byte1
1200 && char2b->byte1 <= font->max_byte1
1201 && char2b->byte2 >= font->min_char_or_byte2
1202 && char2b->byte2 <= font->max_char_or_byte2)
1203 {
1204 pcm = (font->per_char
1205 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1206 * (char2b->byte1 - font->min_byte1))
1207 + (char2b->byte2 - font->min_char_or_byte2));
1208 }
dc6f92b8 1209 }
06a2c219
GM
1210 }
1211 else
1212 {
1213 /* If the per_char pointer is null, all glyphs between the first
1214 and last character indexes inclusive have the same
1215 information, as given by both min_bounds and max_bounds. */
1216 if (char2b->byte2 >= font->min_char_or_byte2
1217 && char2b->byte2 <= font->max_char_or_byte2)
1218 pcm = &font->max_bounds;
1219 }
dc6f92b8 1220
ee569018 1221 return ((pcm == NULL
3e71d8f2 1222 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1223 ? NULL : pcm);
06a2c219 1224}
b73b6aaf 1225
57b03282 1226
06a2c219
GM
1227/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1228 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1229
06a2c219
GM
1230static INLINE void
1231x_encode_char (c, char2b, font_info)
1232 int c;
1233 XChar2b *char2b;
1234 struct font_info *font_info;
1235{
1236 int charset = CHAR_CHARSET (c);
1237 XFontStruct *font = font_info->font;
1238
1239 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1240 This may be either a program in a special encoder language or a
1241 fixed encoding. */
1242 if (font_info->font_encoder)
1243 {
1244 /* It's a program. */
1245 struct ccl_program *ccl = font_info->font_encoder;
1246
1247 if (CHARSET_DIMENSION (charset) == 1)
1248 {
1249 ccl->reg[0] = charset;
1250 ccl->reg[1] = char2b->byte2;
1251 }
1252 else
1253 {
1254 ccl->reg[0] = charset;
1255 ccl->reg[1] = char2b->byte1;
1256 ccl->reg[2] = char2b->byte2;
1257 }
1258
1259 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1260
1261 /* We assume that MSBs are appropriately set/reset by CCL
1262 program. */
1263 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1264 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1265 else
1266 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1267 }
1268 else if (font_info->encoding[charset])
1269 {
1270 /* Fixed encoding scheme. See fontset.h for the meaning of the
1271 encoding numbers. */
1272 int enc = font_info->encoding[charset];
1273
1274 if ((enc == 1 || enc == 2)
1275 && CHARSET_DIMENSION (charset) == 2)
1276 char2b->byte1 |= 0x80;
1277
1278 if (enc == 1 || enc == 3)
1279 char2b->byte2 |= 0x80;
1280 }
1281}
1282
1283
1284/* Get face and two-byte form of character C in face FACE_ID on frame
1285 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
abfb6b46
GM
1286 means we want to display multibyte text. DISPLAY_P non-zero means
1287 make sure that X resources for the face returned are allocated.
1288 Value is a pointer to a realized face that is ready for display if
1289 DISPLAY_P is non-zero. */
06a2c219
GM
1290
1291static INLINE struct face *
abfb6b46 1292x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
06a2c219
GM
1293 struct frame *f;
1294 int c, face_id;
1295 XChar2b *char2b;
abfb6b46 1296 int multibyte_p, display_p;
06a2c219
GM
1297{
1298 struct face *face = FACE_FROM_ID (f, face_id);
1299
1300 if (!multibyte_p)
1301 {
1302 /* Unibyte case. We don't have to encode, but we have to make
1303 sure to use a face suitable for unibyte. */
1304 char2b->byte1 = 0;
1305 char2b->byte2 = c;
ee569018
KH
1306 face_id = FACE_FOR_CHAR (f, face, c);
1307 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1308 }
1309 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1310 {
1311 /* Case of ASCII in a face known to fit ASCII. */
1312 char2b->byte1 = 0;
1313 char2b->byte2 = c;
1314 }
1315 else
1316 {
1317 int c1, c2, charset;
1318
1319 /* Split characters into bytes. If c2 is -1 afterwards, C is
1320 really a one-byte character so that byte1 is zero. */
1321 SPLIT_CHAR (c, charset, c1, c2);
1322 if (c2 > 0)
1323 char2b->byte1 = c1, char2b->byte2 = c2;
1324 else
1325 char2b->byte1 = 0, char2b->byte2 = c1;
1326
06a2c219 1327 /* Maybe encode the character in *CHAR2B. */
ee569018 1328 if (face->font != NULL)
06a2c219
GM
1329 {
1330 struct font_info *font_info
1331 = FONT_INFO_FROM_ID (f, face->font_info_id);
1332 if (font_info)
ee569018 1333 x_encode_char (c, char2b, font_info);
06a2c219
GM
1334 }
1335 }
1336
1337 /* Make sure X resources of the face are allocated. */
abfb6b46
GM
1338 if (display_p)
1339 {
1340 xassert (face != NULL);
1341 PREPARE_FACE_FOR_DISPLAY (f, face);
1342 }
06a2c219
GM
1343
1344 return face;
1345}
1346
1347
1348/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1349 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1350 a pointer to a realized face that is ready for display. */
1351
1352static INLINE struct face *
ee569018 1353x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1354 struct frame *f;
1355 struct glyph *glyph;
1356 XChar2b *char2b;
ee569018 1357 int *two_byte_p;
06a2c219
GM
1358{
1359 struct face *face;
1360
1361 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1362 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1363
ee569018
KH
1364 if (two_byte_p)
1365 *two_byte_p = 0;
1366
06a2c219
GM
1367 if (!glyph->multibyte_p)
1368 {
1369 /* Unibyte case. We don't have to encode, but we have to make
1370 sure to use a face suitable for unibyte. */
1371 char2b->byte1 = 0;
43d120d8 1372 char2b->byte2 = glyph->u.ch;
06a2c219 1373 }
43d120d8
KH
1374 else if (glyph->u.ch < 128
1375 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1376 {
1377 /* Case of ASCII in a face known to fit ASCII. */
1378 char2b->byte1 = 0;
43d120d8 1379 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1380 }
1381 else
1382 {
1383 int c1, c2, charset;
1384
1385 /* Split characters into bytes. If c2 is -1 afterwards, C is
1386 really a one-byte character so that byte1 is zero. */
43d120d8 1387 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1388 if (c2 > 0)
1389 char2b->byte1 = c1, char2b->byte2 = c2;
1390 else
1391 char2b->byte1 = 0, char2b->byte2 = c1;
1392
1393 /* Maybe encode the character in *CHAR2B. */
1394 if (charset != CHARSET_ASCII)
1395 {
1396 struct font_info *font_info
1397 = FONT_INFO_FROM_ID (f, face->font_info_id);
1398 if (font_info)
1399 {
43d120d8 1400 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1401 if (two_byte_p)
1402 *two_byte_p
1403 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1404 }
1405 }
1406 }
1407
1408 /* Make sure X resources of the face are allocated. */
1409 xassert (face != NULL);
1410 PREPARE_FACE_FOR_DISPLAY (f, face);
1411 return face;
1412}
1413
1414
1415/* Store one glyph for IT->char_to_display in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418static INLINE void
1419x_append_glyph (it)
1420 struct it *it;
1421{
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1427
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1430 {
06a2c219
GM
1431 glyph->charpos = CHARPOS (it->position);
1432 glyph->object = it->object;
88d75730 1433 glyph->pixel_width = it->pixel_width;
06a2c219 1434 glyph->voffset = it->voffset;
88d75730 1435 glyph->type = CHAR_GLYPH;
06a2c219 1436 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1437 glyph->left_box_line_p = it->start_of_box_run_p;
1438 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1439 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1440 || it->phys_descent > it->descent);
88d75730 1441 glyph->padding_p = 0;
ee569018 1442 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1443 glyph->face_id = it->face_id;
1444 glyph->u.ch = it->char_to_display;
06a2c219
GM
1445 ++it->glyph_row->used[area];
1446 }
1447}
1448
b4192550
KH
1449/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1450 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1451
1452static INLINE void
1453x_append_composite_glyph (it)
1454 struct it *it;
1455{
1456 struct glyph *glyph;
1457 enum glyph_row_area area = it->area;
1458
1459 xassert (it->glyph_row);
1460
1461 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1462 if (glyph < it->glyph_row->glyphs[area + 1])
1463 {
b4192550
KH
1464 glyph->charpos = CHARPOS (it->position);
1465 glyph->object = it->object;
88d75730 1466 glyph->pixel_width = it->pixel_width;
b4192550 1467 glyph->voffset = it->voffset;
88d75730 1468 glyph->type = COMPOSITE_GLYPH;
b4192550 1469 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1470 glyph->left_box_line_p = it->start_of_box_run_p;
1471 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1472 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1473 || it->phys_descent > it->descent);
88d75730
GM
1474 glyph->padding_p = 0;
1475 glyph->glyph_not_available_p = 0;
1476 glyph->face_id = it->face_id;
1477 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1478 ++it->glyph_row->used[area];
1479 }
1480}
1481
06a2c219
GM
1482
1483/* Change IT->ascent and IT->height according to the setting of
1484 IT->voffset. */
1485
1486static INLINE void
1487take_vertical_position_into_account (it)
1488 struct it *it;
1489{
1490 if (it->voffset)
1491 {
1492 if (it->voffset < 0)
1493 /* Increase the ascent so that we can display the text higher
1494 in the line. */
1495 it->ascent += abs (it->voffset);
1496 else
1497 /* Increase the descent so that we can display the text lower
1498 in the line. */
1499 it->descent += it->voffset;
1500 }
1501}
1502
1503
1504/* Produce glyphs/get display metrics for the image IT is loaded with.
1505 See the description of struct display_iterator in dispextern.h for
1506 an overview of struct display_iterator. */
1507
1508static void
1509x_produce_image_glyph (it)
1510 struct it *it;
1511{
1512 struct image *img;
1513 struct face *face;
1514
1515 xassert (it->what == IT_IMAGE);
1516
1517 face = FACE_FROM_ID (it->f, it->face_id);
1518 img = IMAGE_FROM_ID (it->f, it->image_id);
1519 xassert (img);
1520
1521 /* Make sure X resources of the face and image are loaded. */
1522 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1523 prepare_image_for_display (it->f, img);
1524
95af8492 1525 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1526 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1527 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1528
1529 it->nglyphs = 1;
1530
1531 if (face->box != FACE_NO_BOX)
1532 {
ea2ba0d4
KH
1533 if (face->box_line_width > 0)
1534 {
1535 it->ascent += face->box_line_width;
1536 it->descent += face->box_line_width;
1537 }
06a2c219
GM
1538
1539 if (it->start_of_box_run_p)
ea2ba0d4 1540 it->pixel_width += abs (face->box_line_width);
06a2c219 1541 if (it->end_of_box_run_p)
ea2ba0d4 1542 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1543 }
1544
1545 take_vertical_position_into_account (it);
1546
1547 if (it->glyph_row)
1548 {
1549 struct glyph *glyph;
1550 enum glyph_row_area area = it->area;
1551
1552 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1553 if (glyph < it->glyph_row->glyphs[area + 1])
1554 {
06a2c219
GM
1555 glyph->charpos = CHARPOS (it->position);
1556 glyph->object = it->object;
88d75730 1557 glyph->pixel_width = it->pixel_width;
06a2c219 1558 glyph->voffset = it->voffset;
88d75730 1559 glyph->type = IMAGE_GLYPH;
06a2c219 1560 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1561 glyph->left_box_line_p = it->start_of_box_run_p;
1562 glyph->right_box_line_p = it->end_of_box_run_p;
1563 glyph->overlaps_vertically_p = 0;
1564 glyph->padding_p = 0;
1565 glyph->glyph_not_available_p = 0;
1566 glyph->face_id = it->face_id;
1567 glyph->u.img_id = img->id;
06a2c219
GM
1568 ++it->glyph_row->used[area];
1569 }
1570 }
1571}
1572
1573
1574/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1575 of the glyph, WIDTH and HEIGHT are the width and height of the
1576 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1577 ascent of the glyph (0 <= ASCENT <= 1). */
1578
1579static void
1580x_append_stretch_glyph (it, object, width, height, ascent)
1581 struct it *it;
1582 Lisp_Object object;
1583 int width, height;
1584 double ascent;
1585{
1586 struct glyph *glyph;
1587 enum glyph_row_area area = it->area;
1588
1589 xassert (ascent >= 0 && ascent <= 1);
1590
1591 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1592 if (glyph < it->glyph_row->glyphs[area + 1])
1593 {
06a2c219
GM
1594 glyph->charpos = CHARPOS (it->position);
1595 glyph->object = object;
88d75730 1596 glyph->pixel_width = width;
06a2c219 1597 glyph->voffset = it->voffset;
88d75730 1598 glyph->type = STRETCH_GLYPH;
06a2c219 1599 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1600 glyph->left_box_line_p = it->start_of_box_run_p;
1601 glyph->right_box_line_p = it->end_of_box_run_p;
1602 glyph->overlaps_vertically_p = 0;
1603 glyph->padding_p = 0;
1604 glyph->glyph_not_available_p = 0;
1605 glyph->face_id = it->face_id;
1606 glyph->u.stretch.ascent = height * ascent;
1607 glyph->u.stretch.height = height;
06a2c219
GM
1608 ++it->glyph_row->used[area];
1609 }
1610}
1611
1612
1613/* Produce a stretch glyph for iterator IT. IT->object is the value
1614 of the glyph property displayed. The value must be a list
1615 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1616 being recognized:
1617
1618 1. `:width WIDTH' specifies that the space should be WIDTH *
1619 canonical char width wide. WIDTH may be an integer or floating
1620 point number.
1621
1622 2. `:relative-width FACTOR' specifies that the width of the stretch
1623 should be computed from the width of the first character having the
1624 `glyph' property, and should be FACTOR times that width.
1625
1626 3. `:align-to HPOS' specifies that the space should be wide enough
1627 to reach HPOS, a value in canonical character units.
1628
1629 Exactly one of the above pairs must be present.
1630
1631 4. `:height HEIGHT' specifies that the height of the stretch produced
1632 should be HEIGHT, measured in canonical character units.
1633
269b7745 1634 5. `:relative-height FACTOR' specifies that the height of the
06a2c219
GM
1635 stretch should be FACTOR times the height of the characters having
1636 the glyph property.
1637
1638 Either none or exactly one of 4 or 5 must be present.
1639
1640 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1641 of the stretch should be used for the ascent of the stretch.
1642 ASCENT must be in the range 0 <= ASCENT <= 100. */
1643
1644#define NUMVAL(X) \
1645 ((INTEGERP (X) || FLOATP (X)) \
1646 ? XFLOATINT (X) \
1647 : - 1)
1648
1649
1650static void
1651x_produce_stretch_glyph (it)
1652 struct it *it;
1653{
1654 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1655#if GLYPH_DEBUG
1656 extern Lisp_Object Qspace;
1657#endif
1658 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1659 extern Lisp_Object QCrelative_width, QCrelative_height;
1660 extern Lisp_Object QCalign_to;
1661 Lisp_Object prop, plist;
1662 double width = 0, height = 0, ascent = 0;
1663 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1664 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1665
1666 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1667
1668 /* List should start with `space'. */
1669 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1670 plist = XCDR (it->object);
1671
1672 /* Compute the width of the stretch. */
1673 if (prop = Fplist_get (plist, QCwidth),
1674 NUMVAL (prop) > 0)
1675 /* Absolute width `:width WIDTH' specified and valid. */
1676 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1677 else if (prop = Fplist_get (plist, QCrelative_width),
1678 NUMVAL (prop) > 0)
1679 {
1680 /* Relative width `:relative-width FACTOR' specified and valid.
1681 Compute the width of the characters having the `glyph'
1682 property. */
1683 struct it it2;
1684 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1685
1686 it2 = *it;
1687 if (it->multibyte_p)
1688 {
1689 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1690 - IT_BYTEPOS (*it));
1691 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1692 }
1693 else
1694 it2.c = *p, it2.len = 1;
1695
1696 it2.glyph_row = NULL;
1697 it2.what = IT_CHARACTER;
1698 x_produce_glyphs (&it2);
1699 width = NUMVAL (prop) * it2.pixel_width;
1700 }
1701 else if (prop = Fplist_get (plist, QCalign_to),
1702 NUMVAL (prop) > 0)
1703 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1704 else
1705 /* Nothing specified -> width defaults to canonical char width. */
1706 width = CANON_X_UNIT (it->f);
1707
1708 /* Compute height. */
1709 if (prop = Fplist_get (plist, QCheight),
1710 NUMVAL (prop) > 0)
1711 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1712 else if (prop = Fplist_get (plist, QCrelative_height),
1713 NUMVAL (prop) > 0)
1714 height = FONT_HEIGHT (font) * NUMVAL (prop);
1715 else
1716 height = FONT_HEIGHT (font);
1717
1718 /* Compute percentage of height used for ascent. If
1719 `:ascent ASCENT' is present and valid, use that. Otherwise,
1720 derive the ascent from the font in use. */
1721 if (prop = Fplist_get (plist, QCascent),
1722 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1723 ascent = NUMVAL (prop) / 100.0;
1724 else
1725 ascent = (double) font->ascent / FONT_HEIGHT (font);
1726
1727 if (width <= 0)
1728 width = 1;
1729 if (height <= 0)
1730 height = 1;
1731
1732 if (it->glyph_row)
1733 {
1734 Lisp_Object object = it->stack[it->sp - 1].string;
1735 if (!STRINGP (object))
1736 object = it->w->buffer;
1737 x_append_stretch_glyph (it, object, width, height, ascent);
1738 }
1739
1740 it->pixel_width = width;
66ac4b0e
GM
1741 it->ascent = it->phys_ascent = height * ascent;
1742 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1743 it->nglyphs = 1;
1744
1745 if (face->box != FACE_NO_BOX)
1746 {
ea2ba0d4
KH
1747 if (face->box_line_width > 0)
1748 {
1749 it->ascent += face->box_line_width;
1750 it->descent += face->box_line_width;
1751 }
06a2c219
GM
1752
1753 if (it->start_of_box_run_p)
ea2ba0d4 1754 it->pixel_width += abs (face->box_line_width);
06a2c219 1755 if (it->end_of_box_run_p)
ea2ba0d4 1756 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1757 }
1758
1759 take_vertical_position_into_account (it);
1760}
1761
b4192550
KH
1762/* Return proper value to be used as baseline offset of font that has
1763 ASCENT and DESCENT to draw characters by the font at the vertical
1764 center of the line of frame F.
1765
1766 Here, out task is to find the value of BOFF in the following figure;
1767
1768 -------------------------+-----------+-
1769 -+-+---------+-+ | |
1770 | | | | | |
1771 | | | | F_ASCENT F_HEIGHT
1772 | | | ASCENT | |
1773 HEIGHT | | | | |
1774 | | |-|-+------+-----------|------- baseline
1775 | | | | BOFF | |
1776 | |---------|-+-+ | |
1777 | | | DESCENT | |
1778 -+-+---------+-+ F_DESCENT |
1779 -------------------------+-----------+-
1780
1781 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1782 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1783 DESCENT = FONT->descent
1784 HEIGHT = FONT_HEIGHT (FONT)
1785 F_DESCENT = (F->output_data.x->font->descent
1786 - F->output_data.x->baseline_offset)
1787 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1788*/
1789
458f45fa
KH
1790#define VCENTER_BASELINE_OFFSET(FONT, F) \
1791 ((FONT)->descent \
1792 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1793 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1794 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1795
1796/* Produce glyphs/get display metrics for the display element IT is
1797 loaded with. See the description of struct display_iterator in
1798 dispextern.h for an overview of struct display_iterator. */
1799
1800static void
1801x_produce_glyphs (it)
1802 struct it *it;
1803{
ee569018
KH
1804 it->glyph_not_available_p = 0;
1805
06a2c219
GM
1806 if (it->what == IT_CHARACTER)
1807 {
1808 XChar2b char2b;
1809 XFontStruct *font;
ee569018 1810 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1811 XCharStruct *pcm;
06a2c219 1812 int font_not_found_p;
b4192550
KH
1813 struct font_info *font_info;
1814 int boff; /* baseline offset */
a4249304
KH
1815 /* We may change it->multibyte_p upon unibyte<->multibyte
1816 conversion. So, save the current value now and restore it
1817 later.
1818
1819 Note: It seems that we don't have to record multibyte_p in
1820 struct glyph because the character code itself tells if or
1821 not the character is multibyte. Thus, in the future, we must
1822 consider eliminating the field `multibyte_p' in the struct
c347a1c3 1823 glyph. */
a4249304 1824 int saved_multibyte_p = it->multibyte_p;
06a2c219 1825
ee569018
KH
1826 /* Maybe translate single-byte characters to multibyte, or the
1827 other way. */
06a2c219 1828 it->char_to_display = it->c;
ee569018 1829 if (!ASCII_BYTE_P (it->c))
06a2c219 1830 {
ee569018
KH
1831 if (unibyte_display_via_language_environment
1832 && SINGLE_BYTE_CHAR_P (it->c)
1833 && (it->c >= 0240
1834 || !NILP (Vnonascii_translation_table)))
1835 {
1836 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1837 it->multibyte_p = 1;
ee569018
KH
1838 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1839 face = FACE_FROM_ID (it->f, it->face_id);
1840 }
1841 else if (!SINGLE_BYTE_CHAR_P (it->c)
1842 && !it->multibyte_p)
1843 {
c347a1c3 1844 it->multibyte_p = 1;
ee569018
KH
1845 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1846 face = FACE_FROM_ID (it->f, it->face_id);
1847 }
06a2c219
GM
1848 }
1849
ee569018
KH
1850 /* Get font to use. Encode IT->char_to_display. */
1851 x_get_char_face_and_encoding (it->f, it->char_to_display,
1852 it->face_id, &char2b,
abfb6b46 1853 it->multibyte_p, 0);
06a2c219
GM
1854 font = face->font;
1855
1856 /* When no suitable font found, use the default font. */
1857 font_not_found_p = font == NULL;
1858 if (font_not_found_p)
b4192550
KH
1859 {
1860 font = FRAME_FONT (it->f);
1861 boff = it->f->output_data.x->baseline_offset;
1862 font_info = NULL;
1863 }
1864 else
1865 {
1866 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1867 boff = font_info->baseline_offset;
1868 if (font_info->vertical_centering)
1869 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1870 }
06a2c219
GM
1871
1872 if (it->char_to_display >= ' '
1873 && (!it->multibyte_p || it->char_to_display < 128))
1874 {
1875 /* Either unibyte or ASCII. */
1876 int stretched_p;
1877
1878 it->nglyphs = 1;
06a2c219
GM
1879
1880 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1881 it->ascent = font->ascent + boff;
1882 it->descent = font->descent - boff;
474848ac
GM
1883
1884 if (pcm)
1885 {
1886 it->phys_ascent = pcm->ascent + boff;
1887 it->phys_descent = pcm->descent - boff;
1888 it->pixel_width = pcm->width;
1889 }
1890 else
1891 {
1892 it->glyph_not_available_p = 1;
1893 it->phys_ascent = font->ascent + boff;
1894 it->phys_descent = font->descent - boff;
1895 it->pixel_width = FONT_WIDTH (font);
1896 }
06a2c219
GM
1897
1898 /* If this is a space inside a region of text with
1899 `space-width' property, change its width. */
1900 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1901 if (stretched_p)
1902 it->pixel_width *= XFLOATINT (it->space_width);
1903
1904 /* If face has a box, add the box thickness to the character
1905 height. If character has a box line to the left and/or
1906 right, add the box line width to the character's width. */
1907 if (face->box != FACE_NO_BOX)
1908 {
1909 int thick = face->box_line_width;
1910
ea2ba0d4
KH
1911 if (thick > 0)
1912 {
1913 it->ascent += thick;
1914 it->descent += thick;
1915 }
1916 else
1917 thick = -thick;
1918
06a2c219
GM
1919 if (it->start_of_box_run_p)
1920 it->pixel_width += thick;
1921 if (it->end_of_box_run_p)
1922 it->pixel_width += thick;
1923 }
1924
1925 /* If face has an overline, add the height of the overline
1926 (1 pixel) and a 1 pixel margin to the character height. */
1927 if (face->overline_p)
1928 it->ascent += 2;
1929
1930 take_vertical_position_into_account (it);
1931
1932 /* If we have to actually produce glyphs, do it. */
1933 if (it->glyph_row)
1934 {
1935 if (stretched_p)
1936 {
1937 /* Translate a space with a `space-width' property
1938 into a stretch glyph. */
1939 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1940 x_append_stretch_glyph (it, it->object, it->pixel_width,
1941 it->ascent + it->descent, ascent);
1942 }
1943 else
1944 x_append_glyph (it);
1945
1946 /* If characters with lbearing or rbearing are displayed
1947 in this line, record that fact in a flag of the
1948 glyph row. This is used to optimize X output code. */
1c7e22fd 1949 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1950 it->glyph_row->contains_overlapping_glyphs_p = 1;
1951 }
1952 }
1953 else if (it->char_to_display == '\n')
1954 {
1955 /* A newline has no width but we need the height of the line. */
1956 it->pixel_width = 0;
1957 it->nglyphs = 0;
b4192550
KH
1958 it->ascent = it->phys_ascent = font->ascent + boff;
1959 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1960
ea2ba0d4
KH
1961 if (face->box != FACE_NO_BOX
1962 && face->box_line_width > 0)
06a2c219 1963 {
ea2ba0d4
KH
1964 it->ascent += face->box_line_width;
1965 it->descent += face->box_line_width;
06a2c219
GM
1966 }
1967 }
1968 else if (it->char_to_display == '\t')
1969 {
1970 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1971 int x = it->current_x + it->continuation_lines_width;
06a2c219 1972 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1973
1974 /* If the distance from the current position to the next tab
1975 stop is less than a canonical character width, use the
1976 tab stop after that. */
1977 if (next_tab_x - x < CANON_X_UNIT (it->f))
1978 next_tab_x += tab_width;
06a2c219
GM
1979
1980 it->pixel_width = next_tab_x - x;
1981 it->nglyphs = 1;
b4192550
KH
1982 it->ascent = it->phys_ascent = font->ascent + boff;
1983 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1984
1985 if (it->glyph_row)
1986 {
1987 double ascent = (double) it->ascent / (it->ascent + it->descent);
1988 x_append_stretch_glyph (it, it->object, it->pixel_width,
1989 it->ascent + it->descent, ascent);
1990 }
1991 }
1992 else
1993 {
1994 /* A multi-byte character. Assume that the display width of the
1995 character is the width of the character multiplied by the
b4192550 1996 width of the font. */
06a2c219 1997
b4192550
KH
1998 /* If we found a font, this font should give us the right
1999 metrics. If we didn't find a font, use the frame's
2000 default font and calculate the width of the character
2001 from the charset width; this is what old redisplay code
2002 did. */
2003 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
2004 if (font_not_found_p || !pcm)
2005 {
2006 int charset = CHAR_CHARSET (it->char_to_display);
2007
2008 it->glyph_not_available_p = 1;
2009 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2010 * CHARSET_WIDTH (charset));
2011 it->phys_ascent = font->ascent + boff;
2012 it->phys_descent = font->descent - boff;
2013 }
2014 else
2015 {
2016 it->pixel_width = pcm->width;
2017 it->phys_ascent = pcm->ascent + boff;
2018 it->phys_descent = pcm->descent - boff;
2019 if (it->glyph_row
2020 && (pcm->lbearing < 0
2021 || pcm->rbearing > pcm->width))
2022 it->glyph_row->contains_overlapping_glyphs_p = 1;
2023 }
b4192550
KH
2024 it->nglyphs = 1;
2025 it->ascent = font->ascent + boff;
2026 it->descent = font->descent - boff;
06a2c219
GM
2027 if (face->box != FACE_NO_BOX)
2028 {
2029 int thick = face->box_line_width;
ea2ba0d4
KH
2030
2031 if (thick > 0)
2032 {
2033 it->ascent += thick;
2034 it->descent += thick;
2035 }
2036 else
2037 thick = - thick;
06a2c219
GM
2038
2039 if (it->start_of_box_run_p)
2040 it->pixel_width += thick;
2041 if (it->end_of_box_run_p)
2042 it->pixel_width += thick;
2043 }
2044
2045 /* If face has an overline, add the height of the overline
2046 (1 pixel) and a 1 pixel margin to the character height. */
2047 if (face->overline_p)
2048 it->ascent += 2;
2049
2050 take_vertical_position_into_account (it);
2051
2052 if (it->glyph_row)
2053 x_append_glyph (it);
2054 }
a4249304 2055 it->multibyte_p = saved_multibyte_p;
06a2c219 2056 }
b4192550
KH
2057 else if (it->what == IT_COMPOSITION)
2058 {
2059 /* Note: A composition is represented as one glyph in the
2060 glyph matrix. There are no padding glyphs. */
2061 XChar2b char2b;
2062 XFontStruct *font;
ee569018 2063 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2064 XCharStruct *pcm;
2065 int font_not_found_p;
2066 struct font_info *font_info;
2067 int boff; /* baseline offset */
2068 struct composition *cmp = composition_table[it->cmp_id];
2069
2070 /* Maybe translate single-byte characters to multibyte. */
2071 it->char_to_display = it->c;
2072 if (unibyte_display_via_language_environment
2073 && SINGLE_BYTE_CHAR_P (it->c)
2074 && (it->c >= 0240
2075 || (it->c >= 0200
2076 && !NILP (Vnonascii_translation_table))))
2077 {
2078 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2079 }
2080
2081 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2082 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2083 face = FACE_FROM_ID (it->f, it->face_id);
2084 x_get_char_face_and_encoding (it->f, it->char_to_display,
abfb6b46 2085 it->face_id, &char2b, it->multibyte_p, 0);
b4192550
KH
2086 font = face->font;
2087
2088 /* When no suitable font found, use the default font. */
2089 font_not_found_p = font == NULL;
2090 if (font_not_found_p)
2091 {
2092 font = FRAME_FONT (it->f);
2093 boff = it->f->output_data.x->baseline_offset;
2094 font_info = NULL;
2095 }
2096 else
2097 {
2098 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2099 boff = font_info->baseline_offset;
2100 if (font_info->vertical_centering)
2101 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2102 }
2103
2104 /* There are no padding glyphs, so there is only one glyph to
2105 produce for the composition. Important is that pixel_width,
2106 ascent and descent are the values of what is drawn by
2107 draw_glyphs (i.e. the values of the overall glyphs composed). */
2108 it->nglyphs = 1;
2109
2110 /* If we have not yet calculated pixel size data of glyphs of
2111 the composition for the current face font, calculate them
2112 now. Theoretically, we have to check all fonts for the
2113 glyphs, but that requires much time and memory space. So,
2114 here we check only the font of the first glyph. This leads
2115 to incorrect display very rarely, and C-l (recenter) can
2116 correct the display anyway. */
2117 if (cmp->font != (void *) font)
2118 {
2119 /* Ascent and descent of the font of the first character of
2120 this composition (adjusted by baseline offset). Ascent
2121 and descent of overall glyphs should not be less than
2122 them respectively. */
2123 int font_ascent = font->ascent + boff;
2124 int font_descent = font->descent - boff;
2125 /* Bounding box of the overall glyphs. */
2126 int leftmost, rightmost, lowest, highest;
329bed06 2127 int i, width, ascent, descent;
b4192550
KH
2128
2129 cmp->font = (void *) font;
2130
2131 /* Initialize the bounding box. */
1bdeec2e
KH
2132 if (font_info
2133 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2134 {
2135 width = pcm->width;
2136 ascent = pcm->ascent;
2137 descent = pcm->descent;
2138 }
2139 else
2140 {
2141 width = FONT_WIDTH (font);
2142 ascent = font->ascent;
2143 descent = font->descent;
2144 }
2145
2146 rightmost = width;
2147 lowest = - descent + boff;
2148 highest = ascent + boff;
b4192550 2149 leftmost = 0;
329bed06 2150
b4192550
KH
2151 if (font_info
2152 && font_info->default_ascent
2153 && CHAR_TABLE_P (Vuse_default_ascent)
2154 && !NILP (Faref (Vuse_default_ascent,
2155 make_number (it->char_to_display))))
2156 highest = font_info->default_ascent + boff;
2157
2158 /* Draw the first glyph at the normal position. It may be
2159 shifted to right later if some other glyphs are drawn at
2160 the left. */
2161 cmp->offsets[0] = 0;
2162 cmp->offsets[1] = boff;
2163
2164 /* Set cmp->offsets for the remaining glyphs. */
2165 for (i = 1; i < cmp->glyph_len; i++)
2166 {
2167 int left, right, btm, top;
2168 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2169 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2170
2171 face = FACE_FROM_ID (it->f, face_id);
2172 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
abfb6b46 2173 it->multibyte_p, 0);
b4192550
KH
2174 font = face->font;
2175 if (font == NULL)
2176 {
2177 font = FRAME_FONT (it->f);
2178 boff = it->f->output_data.x->baseline_offset;
2179 font_info = NULL;
2180 }
2181 else
2182 {
2183 font_info
2184 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2185 boff = font_info->baseline_offset;
2186 if (font_info->vertical_centering)
2187 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2188 }
2189
1bdeec2e
KH
2190 if (font_info
2191 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2192 {
2193 width = pcm->width;
2194 ascent = pcm->ascent;
2195 descent = pcm->descent;
2196 }
2197 else
2198 {
2199 width = FONT_WIDTH (font);
1bdeec2e
KH
2200 ascent = 1;
2201 descent = 0;
329bed06 2202 }
b4192550
KH
2203
2204 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2205 {
2206 /* Relative composition with or without
2207 alternate chars. */
329bed06
GM
2208 left = (leftmost + rightmost - width) / 2;
2209 btm = - descent + boff;
b4192550
KH
2210 if (font_info && font_info->relative_compose
2211 && (! CHAR_TABLE_P (Vignore_relative_composition)
2212 || NILP (Faref (Vignore_relative_composition,
2213 make_number (ch)))))
2214 {
2215
329bed06 2216 if (- descent >= font_info->relative_compose)
b4192550
KH
2217 /* One extra pixel between two glyphs. */
2218 btm = highest + 1;
329bed06 2219 else if (ascent <= 0)
b4192550 2220 /* One extra pixel between two glyphs. */
329bed06 2221 btm = lowest - 1 - ascent - descent;
b4192550
KH
2222 }
2223 }
2224 else
2225 {
2226 /* A composition rule is specified by an integer
2227 value that encodes global and new reference
2228 points (GREF and NREF). GREF and NREF are
2229 specified by numbers as below:
2230
2231 0---1---2 -- ascent
2232 | |
2233 | |
2234 | |
2235 9--10--11 -- center
2236 | |
2237 ---3---4---5--- baseline
2238 | |
2239 6---7---8 -- descent
2240 */
2241 int rule = COMPOSITION_RULE (cmp, i);
2242 int gref, nref, grefx, grefy, nrefx, nrefy;
2243
2244 COMPOSITION_DECODE_RULE (rule, gref, nref);
2245 grefx = gref % 3, nrefx = nref % 3;
2246 grefy = gref / 3, nrefy = nref / 3;
2247
2248 left = (leftmost
2249 + grefx * (rightmost - leftmost) / 2
329bed06 2250 - nrefx * width / 2);
b4192550
KH
2251 btm = ((grefy == 0 ? highest
2252 : grefy == 1 ? 0
2253 : grefy == 2 ? lowest
2254 : (highest + lowest) / 2)
329bed06
GM
2255 - (nrefy == 0 ? ascent + descent
2256 : nrefy == 1 ? descent - boff
b4192550 2257 : nrefy == 2 ? 0
329bed06 2258 : (ascent + descent) / 2));
b4192550
KH
2259 }
2260
2261 cmp->offsets[i * 2] = left;
329bed06 2262 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2263
2264 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2265 right = left + width;
2266 top = btm + descent + ascent;
b4192550
KH
2267 if (left < leftmost)
2268 leftmost = left;
2269 if (right > rightmost)
2270 rightmost = right;
2271 if (top > highest)
2272 highest = top;
2273 if (btm < lowest)
2274 lowest = btm;
2275 }
2276
2277 /* If there are glyphs whose x-offsets are negative,
2278 shift all glyphs to the right and make all x-offsets
2279 non-negative. */
2280 if (leftmost < 0)
2281 {
2282 for (i = 0; i < cmp->glyph_len; i++)
2283 cmp->offsets[i * 2] -= leftmost;
2284 rightmost -= leftmost;
2285 }
2286
2287 cmp->pixel_width = rightmost;
2288 cmp->ascent = highest;
2289 cmp->descent = - lowest;
2290 if (cmp->ascent < font_ascent)
2291 cmp->ascent = font_ascent;
2292 if (cmp->descent < font_descent)
2293 cmp->descent = font_descent;
2294 }
2295
2296 it->pixel_width = cmp->pixel_width;
2297 it->ascent = it->phys_ascent = cmp->ascent;
2298 it->descent = it->phys_descent = cmp->descent;
2299
2300 if (face->box != FACE_NO_BOX)
2301 {
2302 int thick = face->box_line_width;
ea2ba0d4
KH
2303
2304 if (thick > 0)
2305 {
2306 it->ascent += thick;
2307 it->descent += thick;
2308 }
2309 else
2310 thick = - thick;
b4192550
KH
2311
2312 if (it->start_of_box_run_p)
2313 it->pixel_width += thick;
2314 if (it->end_of_box_run_p)
2315 it->pixel_width += thick;
2316 }
2317
2318 /* If face has an overline, add the height of the overline
2319 (1 pixel) and a 1 pixel margin to the character height. */
2320 if (face->overline_p)
2321 it->ascent += 2;
2322
2323 take_vertical_position_into_account (it);
2324
2325 if (it->glyph_row)
2326 x_append_composite_glyph (it);
2327 }
06a2c219
GM
2328 else if (it->what == IT_IMAGE)
2329 x_produce_image_glyph (it);
2330 else if (it->what == IT_STRETCH)
2331 x_produce_stretch_glyph (it);
2332
3017fdd1
GM
2333 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2334 because this isn't true for images with `:ascent 100'. */
2335 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2336 if (it->area == TEXT_AREA)
2337 it->current_x += it->pixel_width;
66ac4b0e 2338
d365f5bb
GM
2339 it->descent += it->extra_line_spacing;
2340
06a2c219
GM
2341 it->max_ascent = max (it->max_ascent, it->ascent);
2342 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2343 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2344 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2345}
2346
2347
2348/* Estimate the pixel height of the mode or top line on frame F.
2349 FACE_ID specifies what line's height to estimate. */
2350
2351int
2352x_estimate_mode_line_height (f, face_id)
2353 struct frame *f;
2354 enum face_id face_id;
2355{
43281ee3 2356 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2357
2358 /* This function is called so early when Emacs starts that the face
2359 cache and mode line face are not yet initialized. */
2360 if (FRAME_FACE_CACHE (f))
2361 {
2362 struct face *face = FACE_FROM_ID (f, face_id);
2363 if (face)
43281ee3
GM
2364 {
2365 if (face->font)
2366 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2367 if (face->box_line_width > 0)
2368 height += 2 * face->box_line_width;
43281ee3 2369 }
06a2c219
GM
2370 }
2371
2372 return height;
2373}
2374
2375\f
2376/***********************************************************************
2377 Glyph display
2378 ***********************************************************************/
2379
2380/* A sequence of glyphs to be drawn in the same face.
2381
2382 This data structure is not really completely X specific, so it
2383 could possibly, at least partially, be useful for other systems. It
2384 is currently not part of the external redisplay interface because
2385 it's not clear what other systems will need. */
2386
2387struct glyph_string
2388{
2389 /* X-origin of the string. */
2390 int x;
2391
2392 /* Y-origin and y-position of the base line of this string. */
2393 int y, ybase;
2394
2395 /* The width of the string, not including a face extension. */
2396 int width;
2397
2398 /* The width of the string, including a face extension. */
2399 int background_width;
2400
2401 /* The height of this string. This is the height of the line this
2402 string is drawn in, and can be different from the height of the
2403 font the string is drawn in. */
2404 int height;
2405
2406 /* Number of pixels this string overwrites in front of its x-origin.
2407 This number is zero if the string has an lbearing >= 0; it is
2408 -lbearing, if the string has an lbearing < 0. */
2409 int left_overhang;
2410
2411 /* Number of pixels this string overwrites past its right-most
2412 nominal x-position, i.e. x + width. Zero if the string's
2413 rbearing is <= its nominal width, rbearing - width otherwise. */
2414 int right_overhang;
2415
2416 /* The frame on which the glyph string is drawn. */
2417 struct frame *f;
2418
2419 /* The window on which the glyph string is drawn. */
2420 struct window *w;
2421
2422 /* X display and window for convenience. */
2423 Display *display;
2424 Window window;
2425
2426 /* The glyph row for which this string was built. It determines the
2427 y-origin and height of the string. */
2428 struct glyph_row *row;
2429
2430 /* The area within row. */
2431 enum glyph_row_area area;
2432
2433 /* Characters to be drawn, and number of characters. */
2434 XChar2b *char2b;
2435 int nchars;
2436
06a2c219
GM
2437 /* A face-override for drawing cursors, mouse face and similar. */
2438 enum draw_glyphs_face hl;
2439
2440 /* Face in which this string is to be drawn. */
2441 struct face *face;
2442
2443 /* Font in which this string is to be drawn. */
2444 XFontStruct *font;
2445
2446 /* Font info for this string. */
2447 struct font_info *font_info;
2448
b4192550
KH
2449 /* Non-null means this string describes (part of) a composition.
2450 All characters from char2b are drawn composed. */
2451 struct composition *cmp;
06a2c219
GM
2452
2453 /* Index of this glyph string's first character in the glyph
b4192550
KH
2454 definition of CMP. If this is zero, this glyph string describes
2455 the first character of a composition. */
06a2c219
GM
2456 int gidx;
2457
2458 /* 1 means this glyph strings face has to be drawn to the right end
2459 of the window's drawing area. */
2460 unsigned extends_to_end_of_line_p : 1;
2461
2462 /* 1 means the background of this string has been drawn. */
2463 unsigned background_filled_p : 1;
2464
2465 /* 1 means glyph string must be drawn with 16-bit functions. */
2466 unsigned two_byte_p : 1;
2467
2468 /* 1 means that the original font determined for drawing this glyph
2469 string could not be loaded. The member `font' has been set to
2470 the frame's default font in this case. */
2471 unsigned font_not_found_p : 1;
2472
2473 /* 1 means that the face in which this glyph string is drawn has a
2474 stipple pattern. */
2475 unsigned stippled_p : 1;
2476
66ac4b0e
GM
2477 /* 1 means only the foreground of this glyph string must be drawn,
2478 and we should use the physical height of the line this glyph
2479 string appears in as clip rect. */
2480 unsigned for_overlaps_p : 1;
2481
06a2c219
GM
2482 /* The GC to use for drawing this glyph string. */
2483 GC gc;
2484
2485 /* A pointer to the first glyph in the string. This glyph
2486 corresponds to char2b[0]. Needed to draw rectangles if
2487 font_not_found_p is 1. */
2488 struct glyph *first_glyph;
2489
2490 /* Image, if any. */
2491 struct image *img;
2492
2493 struct glyph_string *next, *prev;
2494};
2495
2496
61869b99 2497#if GLYPH_DEBUG
06a2c219
GM
2498
2499static void
2500x_dump_glyph_string (s)
2501 struct glyph_string *s;
2502{
2503 fprintf (stderr, "glyph string\n");
2504 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2505 s->x, s->y, s->width, s->height);
2506 fprintf (stderr, " ybase = %d\n", s->ybase);
2507 fprintf (stderr, " hl = %d\n", s->hl);
2508 fprintf (stderr, " left overhang = %d, right = %d\n",
2509 s->left_overhang, s->right_overhang);
2510 fprintf (stderr, " nchars = %d\n", s->nchars);
2511 fprintf (stderr, " extends to end of line = %d\n",
2512 s->extends_to_end_of_line_p);
2513 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2514 fprintf (stderr, " bg width = %d\n", s->background_width);
2515}
2516
2517#endif /* GLYPH_DEBUG */
2518
2519
2520
2521static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2522 struct glyph_string **,
2523 struct glyph_string *,
2524 struct glyph_string *));
2525static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2526 struct glyph_string **,
2527 struct glyph_string *,
2528 struct glyph_string *));
2529static void x_append_glyph_string P_ ((struct glyph_string **,
2530 struct glyph_string **,
2531 struct glyph_string *));
2532static int x_left_overwritten P_ ((struct glyph_string *));
2533static int x_left_overwriting P_ ((struct glyph_string *));
2534static int x_right_overwritten P_ ((struct glyph_string *));
2535static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2536static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2537 int));
06a2c219
GM
2538static void x_init_glyph_string P_ ((struct glyph_string *,
2539 XChar2b *, struct window *,
2540 struct glyph_row *,
2541 enum glyph_row_area, int,
2542 enum draw_glyphs_face));
2543static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2544 enum glyph_row_area, int, int,
f0a48a01 2545 enum draw_glyphs_face, int));
06a2c219
GM
2546static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2547static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2548static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2549 int));
2550static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2551static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2552static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2553static void x_draw_glyph_string P_ ((struct glyph_string *));
2554static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2555static void x_set_cursor_gc P_ ((struct glyph_string *));
2556static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2557static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2558static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2559 int *, int *));
2560static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2561static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2562 unsigned long *, double, int));
06a2c219 2563static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2564 double, int, unsigned long));
06a2c219
GM
2565static void x_setup_relief_colors P_ ((struct glyph_string *));
2566static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2567static void x_draw_image_relief P_ ((struct glyph_string *));
2568static void x_draw_image_foreground P_ ((struct glyph_string *));
2569static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2570static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2571static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2572 int, int, int));
2573static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2574 int, int, int, int, XRectangle *));
2575static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2576 int, int, int, XRectangle *));
66ac4b0e
GM
2577static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2578 enum glyph_row_area));
209f68d9
GM
2579static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2580 struct glyph_row *,
2581 enum glyph_row_area, int, int));
06a2c219 2582
163dcff3
GM
2583#if GLYPH_DEBUG
2584static void x_check_font P_ ((struct frame *, XFontStruct *));
2585#endif
2586
06a2c219 2587
06a2c219
GM
2588/* Append the list of glyph strings with head H and tail T to the list
2589 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2590
2591static INLINE void
2592x_append_glyph_string_lists (head, tail, h, t)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *h, *t;
2595{
2596 if (h)
2597 {
2598 if (*head)
2599 (*tail)->next = h;
2600 else
2601 *head = h;
2602 h->prev = *tail;
2603 *tail = t;
2604 }
2605}
2606
2607
2608/* Prepend the list of glyph strings with head H and tail T to the
2609 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2610 result. */
2611
2612static INLINE void
2613x_prepend_glyph_string_lists (head, tail, h, t)
2614 struct glyph_string **head, **tail;
2615 struct glyph_string *h, *t;
2616{
2617 if (h)
2618 {
2619 if (*head)
2620 (*head)->prev = t;
2621 else
2622 *tail = t;
2623 t->next = *head;
2624 *head = h;
2625 }
2626}
2627
2628
2629/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2630 Set *HEAD and *TAIL to the resulting list. */
2631
2632static INLINE void
2633x_append_glyph_string (head, tail, s)
2634 struct glyph_string **head, **tail;
2635 struct glyph_string *s;
2636{
2637 s->next = s->prev = NULL;
2638 x_append_glyph_string_lists (head, tail, s, s);
2639}
2640
2641
2642/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2643 face. */
2644
2645static void
2646x_set_cursor_gc (s)
2647 struct glyph_string *s;
2648{
2649 if (s->font == FRAME_FONT (s->f)
2650 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2651 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2652 && !s->cmp)
06a2c219
GM
2653 s->gc = s->f->output_data.x->cursor_gc;
2654 else
2655 {
2656 /* Cursor on non-default face: must merge. */
2657 XGCValues xgcv;
2658 unsigned long mask;
2659
2660 xgcv.background = s->f->output_data.x->cursor_pixel;
2661 xgcv.foreground = s->face->background;
2662
2663 /* If the glyph would be invisible, try a different foreground. */
2664 if (xgcv.foreground == xgcv.background)
2665 xgcv.foreground = s->face->foreground;
2666 if (xgcv.foreground == xgcv.background)
2667 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2668 if (xgcv.foreground == xgcv.background)
2669 xgcv.foreground = s->face->foreground;
2670
2671 /* Make sure the cursor is distinct from text in this face. */
2672 if (xgcv.background == s->face->background
2673 && xgcv.foreground == s->face->foreground)
2674 {
2675 xgcv.background = s->face->foreground;
2676 xgcv.foreground = s->face->background;
2677 }
2678
2679 IF_DEBUG (x_check_font (s->f, s->font));
2680 xgcv.font = s->font->fid;
2681 xgcv.graphics_exposures = False;
2682 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2683
2684 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2685 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2686 mask, &xgcv);
2687 else
2688 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2689 = XCreateGC (s->display, s->window, mask, &xgcv);
2690
2691 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2692 }
2693}
2694
2695
2696/* Set up S->gc of glyph string S for drawing text in mouse face. */
2697
2698static void
2699x_set_mouse_face_gc (s)
2700 struct glyph_string *s;
2701{
2702 int face_id;
ee569018 2703 struct face *face;
06a2c219 2704
e4ded23c 2705 /* What face has to be used last for the mouse face? */
06a2c219 2706 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2707 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2708 if (face == NULL)
2709 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2710
033e3e18
GM
2711 if (s->first_glyph->type == CHAR_GLYPH)
2712 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2713 else
2714 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2715 s->face = FACE_FROM_ID (s->f, face_id);
2716 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2717
2718 /* If font in this face is same as S->font, use it. */
2719 if (s->font == s->face->font)
2720 s->gc = s->face->gc;
2721 else
2722 {
2723 /* Otherwise construct scratch_cursor_gc with values from FACE
2724 but font FONT. */
2725 XGCValues xgcv;
2726 unsigned long mask;
2727
2728 xgcv.background = s->face->background;
2729 xgcv.foreground = s->face->foreground;
2730 IF_DEBUG (x_check_font (s->f, s->font));
2731 xgcv.font = s->font->fid;
2732 xgcv.graphics_exposures = False;
2733 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2734
2735 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2736 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2737 mask, &xgcv);
2738 else
2739 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2740 = XCreateGC (s->display, s->window, mask, &xgcv);
2741
2742 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2743 }
2744
2745 xassert (s->gc != 0);
2746}
2747
2748
2749/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2750 Faces to use in the mode line have already been computed when the
2751 matrix was built, so there isn't much to do, here. */
2752
2753static INLINE void
2754x_set_mode_line_face_gc (s)
2755 struct glyph_string *s;
2756{
2757 s->gc = s->face->gc;
06a2c219
GM
2758}
2759
2760
2761/* Set S->gc of glyph string S for drawing that glyph string. Set
2762 S->stippled_p to a non-zero value if the face of S has a stipple
2763 pattern. */
2764
2765static INLINE void
2766x_set_glyph_string_gc (s)
2767 struct glyph_string *s;
2768{
209f68d9
GM
2769 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2770
06a2c219
GM
2771 if (s->hl == DRAW_NORMAL_TEXT)
2772 {
2773 s->gc = s->face->gc;
2774 s->stippled_p = s->face->stipple != 0;
2775 }
2776 else if (s->hl == DRAW_INVERSE_VIDEO)
2777 {
2778 x_set_mode_line_face_gc (s);
2779 s->stippled_p = s->face->stipple != 0;
2780 }
2781 else if (s->hl == DRAW_CURSOR)
2782 {
2783 x_set_cursor_gc (s);
2784 s->stippled_p = 0;
2785 }
2786 else if (s->hl == DRAW_MOUSE_FACE)
2787 {
2788 x_set_mouse_face_gc (s);
2789 s->stippled_p = s->face->stipple != 0;
2790 }
2791 else if (s->hl == DRAW_IMAGE_RAISED
2792 || s->hl == DRAW_IMAGE_SUNKEN)
2793 {
2794 s->gc = s->face->gc;
2795 s->stippled_p = s->face->stipple != 0;
2796 }
2797 else
2798 {
2799 s->gc = s->face->gc;
2800 s->stippled_p = s->face->stipple != 0;
2801 }
2802
2803 /* GC must have been set. */
2804 xassert (s->gc != 0);
2805}
2806
2807
2808/* Return in *R the clipping rectangle for glyph string S. */
2809
2810static void
2811x_get_glyph_string_clip_rect (s, r)
2812 struct glyph_string *s;
2813 XRectangle *r;
2814{
2815 if (s->row->full_width_p)
2816 {
2817 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2818 int canon_x = CANON_X_UNIT (s->f);
2819
2820 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2821 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2822
2823 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2824 {
1da3fd71 2825 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2826 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2827 r->x -= width;
2828 }
2829
b9432a85 2830 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2831
06a2c219
GM
2832 /* Unless displaying a mode or menu bar line, which are always
2833 fully visible, clip to the visible part of the row. */
2834 if (s->w->pseudo_window_p)
2835 r->height = s->row->visible_height;
2836 else
2837 r->height = s->height;
2838 }
2839 else
2840 {
2841 /* This is a text line that may be partially visible. */
2842 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2843 r->width = window_box_width (s->w, s->area);
2844 r->height = s->row->visible_height;
2845 }
2846
66ac4b0e
GM
2847 /* If S draws overlapping rows, it's sufficient to use the top and
2848 bottom of the window for clipping because this glyph string
2849 intentionally draws over other lines. */
2850 if (s->for_overlaps_p)
2851 {
045dee35 2852 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2853 r->height = window_text_bottom_y (s->w) - r->y;
2854 }
98b8a90f
GM
2855 else
2856 {
2857 /* Don't use S->y for clipping because it doesn't take partially
2858 visible lines into account. For example, it can be negative for
2859 partially visible lines at the top of a window. */
2860 if (!s->row->full_width_p
2861 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2862 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2863 else
2864 r->y = max (0, s->row->y);
2865
2866 /* If drawing a tool-bar window, draw it over the internal border
2867 at the top of the window. */
2868 if (s->w == XWINDOW (s->f->tool_bar_window))
2869 r->y -= s->f->output_data.x->internal_border_width;
2870 }
2871
66ac4b0e 2872 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2873}
2874
2875
2876/* Set clipping for output of glyph string S. S may be part of a mode
2877 line or menu if we don't have X toolkit support. */
2878
2879static INLINE void
2880x_set_glyph_string_clipping (s)
2881 struct glyph_string *s;
2882{
2883 XRectangle r;
2884 x_get_glyph_string_clip_rect (s, &r);
2885 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2886}
2887
2888
2889/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2890 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2891
2892static INLINE void
2893x_compute_glyph_string_overhangs (s)
2894 struct glyph_string *s;
2895{
b4192550 2896 if (s->cmp == NULL
06a2c219
GM
2897 && s->first_glyph->type == CHAR_GLYPH)
2898 {
2899 XCharStruct cs;
2900 int direction, font_ascent, font_descent;
2901 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2902 &font_ascent, &font_descent, &cs);
2903 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2904 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2905 }
2906}
2907
2908
2909/* Compute overhangs and x-positions for glyph string S and its
2910 predecessors, or successors. X is the starting x-position for S.
2911 BACKWARD_P non-zero means process predecessors. */
2912
2913static void
2914x_compute_overhangs_and_x (s, x, backward_p)
2915 struct glyph_string *s;
2916 int x;
2917 int backward_p;
2918{
2919 if (backward_p)
2920 {
2921 while (s)
2922 {
2923 x_compute_glyph_string_overhangs (s);
2924 x -= s->width;
2925 s->x = x;
2926 s = s->prev;
2927 }
2928 }
2929 else
2930 {
2931 while (s)
2932 {
2933 x_compute_glyph_string_overhangs (s);
2934 s->x = x;
2935 x += s->width;
2936 s = s->next;
2937 }
2938 }
2939}
2940
2941
2942/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2943 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2944 assumed to be zero. */
06a2c219
GM
2945
2946static void
2947x_get_glyph_overhangs (glyph, f, left, right)
2948 struct glyph *glyph;
2949 struct frame *f;
2950 int *left, *right;
2951{
06a2c219
GM
2952 *left = *right = 0;
2953
b4192550 2954 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2955 {
2956 XFontStruct *font;
2957 struct face *face;
2958 struct font_info *font_info;
2959 XChar2b char2b;
ee569018
KH
2960 XCharStruct *pcm;
2961
2962 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2963 font = face->font;
2964 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2965 if (font
2966 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2967 {
06a2c219
GM
2968 if (pcm->rbearing > pcm->width)
2969 *right = pcm->rbearing - pcm->width;
2970 if (pcm->lbearing < 0)
2971 *left = -pcm->lbearing;
2972 }
2973 }
2974}
2975
2976
2977/* Return the index of the first glyph preceding glyph string S that
2978 is overwritten by S because of S's left overhang. Value is -1
2979 if no glyphs are overwritten. */
2980
2981static int
2982x_left_overwritten (s)
2983 struct glyph_string *s;
2984{
2985 int k;
2986
2987 if (s->left_overhang)
2988 {
2989 int x = 0, i;
2990 struct glyph *glyphs = s->row->glyphs[s->area];
2991 int first = s->first_glyph - glyphs;
2992
2993 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2994 x -= glyphs[i].pixel_width;
2995
2996 k = i + 1;
2997 }
2998 else
2999 k = -1;
3000
3001 return k;
3002}
3003
3004
3005/* Return the index of the first glyph preceding glyph string S that
3006 is overwriting S because of its right overhang. Value is -1 if no
3007 glyph in front of S overwrites S. */
3008
3009static int
3010x_left_overwriting (s)
3011 struct glyph_string *s;
3012{
3013 int i, k, x;
3014 struct glyph *glyphs = s->row->glyphs[s->area];
3015 int first = s->first_glyph - glyphs;
3016
3017 k = -1;
3018 x = 0;
3019 for (i = first - 1; i >= 0; --i)
3020 {
3021 int left, right;
3022 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3023 if (x + right > 0)
3024 k = i;
3025 x -= glyphs[i].pixel_width;
3026 }
3027
3028 return k;
3029}
3030
3031
3032/* Return the index of the last glyph following glyph string S that is
3033 not overwritten by S because of S's right overhang. Value is -1 if
3034 no such glyph is found. */
3035
3036static int
3037x_right_overwritten (s)
3038 struct glyph_string *s;
3039{
3040 int k = -1;
3041
3042 if (s->right_overhang)
3043 {
3044 int x = 0, i;
3045 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3046 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3047 int end = s->row->used[s->area];
3048
3049 for (i = first; i < end && s->right_overhang > x; ++i)
3050 x += glyphs[i].pixel_width;
3051
3052 k = i;
3053 }
3054
3055 return k;
3056}
3057
3058
3059/* Return the index of the last glyph following glyph string S that
3060 overwrites S because of its left overhang. Value is negative
3061 if no such glyph is found. */
3062
3063static int
3064x_right_overwriting (s)
3065 struct glyph_string *s;
3066{
3067 int i, k, x;
3068 int end = s->row->used[s->area];
3069 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3070 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3071
3072 k = -1;
3073 x = 0;
3074 for (i = first; i < end; ++i)
3075 {
3076 int left, right;
3077 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3078 if (x - left < 0)
3079 k = i;
3080 x += glyphs[i].pixel_width;
3081 }
3082
3083 return k;
3084}
3085
3086
3087/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3088
3089static INLINE void
3090x_clear_glyph_string_rect (s, x, y, w, h)
3091 struct glyph_string *s;
3092 int x, y, w, h;
3093{
3094 XGCValues xgcv;
3095 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3096 XSetForeground (s->display, s->gc, xgcv.background);
3097 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3098 XSetForeground (s->display, s->gc, xgcv.foreground);
3099}
3100
3101
3102/* Draw the background of glyph_string S. If S->background_filled_p
3103 is non-zero don't draw it. FORCE_P non-zero means draw the
3104 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3105 when a string preceding S draws into the background of S, or S
3106 contains the first component of a composition. */
06a2c219
GM
3107
3108static void
3109x_draw_glyph_string_background (s, force_p)
3110 struct glyph_string *s;
3111 int force_p;
3112{
3113 /* Nothing to do if background has already been drawn or if it
3114 shouldn't be drawn in the first place. */
3115 if (!s->background_filled_p)
3116 {
ea2ba0d4
KH
3117 int box_line_width = max (s->face->box_line_width, 0);
3118
b4192550 3119 if (s->stippled_p)
06a2c219
GM
3120 {
3121 /* Fill background with a stipple pattern. */
3122 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3123 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3124 s->y + box_line_width,
06a2c219 3125 s->background_width,
ea2ba0d4 3126 s->height - 2 * box_line_width);
06a2c219
GM
3127 XSetFillStyle (s->display, s->gc, FillSolid);
3128 s->background_filled_p = 1;
3129 }
ea2ba0d4 3130 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3131 || s->font_not_found_p
3132 || s->extends_to_end_of_line_p
06a2c219
GM
3133 || force_p)
3134 {
ea2ba0d4 3135 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3136 s->background_width,
ea2ba0d4 3137 s->height - 2 * box_line_width);
06a2c219
GM
3138 s->background_filled_p = 1;
3139 }
3140 }
3141}
3142
3143
3144/* Draw the foreground of glyph string S. */
3145
3146static void
3147x_draw_glyph_string_foreground (s)
3148 struct glyph_string *s;
3149{
3150 int i, x;
3151
3152 /* If first glyph of S has a left box line, start drawing the text
3153 of S to the right of that box line. */
3154 if (s->face->box != FACE_NO_BOX
3155 && s->first_glyph->left_box_line_p)
ea2ba0d4 3156 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3157 else
3158 x = s->x;
3159
b4192550
KH
3160 /* Draw characters of S as rectangles if S's font could not be
3161 loaded. */
3162 if (s->font_not_found_p)
06a2c219 3163 {
b4192550 3164 for (i = 0; i < s->nchars; ++i)
06a2c219 3165 {
b4192550
KH
3166 struct glyph *g = s->first_glyph + i;
3167 XDrawRectangle (s->display, s->window,
3168 s->gc, x, s->y, g->pixel_width - 1,
3169 s->height - 1);
3170 x += g->pixel_width;
06a2c219
GM
3171 }
3172 }
3173 else
3174 {
b4192550
KH
3175 char *char1b = (char *) s->char2b;
3176 int boff = s->font_info->baseline_offset;
06a2c219 3177
b4192550
KH
3178 if (s->font_info->vertical_centering)
3179 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3180
3181 /* If we can use 8-bit functions, condense S->char2b. */
3182 if (!s->two_byte_p)
3183 for (i = 0; i < s->nchars; ++i)
3184 char1b[i] = s->char2b[i].byte2;
3185
3186 /* Draw text with XDrawString if background has already been
3187 filled. Otherwise, use XDrawImageString. (Note that
3188 XDrawImageString is usually faster than XDrawString.) Always
3189 use XDrawImageString when drawing the cursor so that there is
3190 no chance that characters under a box cursor are invisible. */
3191 if (s->for_overlaps_p
3192 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3193 {
3194 /* Draw characters with 16-bit or 8-bit functions. */
3195 if (s->two_byte_p)
3196 XDrawString16 (s->display, s->window, s->gc, x,
3197 s->ybase - boff, s->char2b, s->nchars);
3198 else
3199 XDrawString (s->display, s->window, s->gc, x,
3200 s->ybase - boff, char1b, s->nchars);
3201 }
06a2c219
GM
3202 else
3203 {
b4192550
KH
3204 if (s->two_byte_p)
3205 XDrawImageString16 (s->display, s->window, s->gc, x,
3206 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3207 else
b4192550
KH
3208 XDrawImageString (s->display, s->window, s->gc, x,
3209 s->ybase - boff, char1b, s->nchars);
3210 }
3211 }
3212}
06a2c219 3213
b4192550 3214/* Draw the foreground of composite glyph string S. */
06a2c219 3215
b4192550
KH
3216static void
3217x_draw_composite_glyph_string_foreground (s)
3218 struct glyph_string *s;
3219{
3220 int i, x;
06a2c219 3221
b4192550
KH
3222 /* If first glyph of S has a left box line, start drawing the text
3223 of S to the right of that box line. */
3224 if (s->face->box != FACE_NO_BOX
3225 && s->first_glyph->left_box_line_p)
ea2ba0d4 3226 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3227 else
3228 x = s->x;
06a2c219 3229
b4192550
KH
3230 /* S is a glyph string for a composition. S->gidx is the index of
3231 the first character drawn for glyphs of this composition.
3232 S->gidx == 0 means we are drawing the very first character of
3233 this composition. */
06a2c219 3234
b4192550
KH
3235 /* Draw a rectangle for the composition if the font for the very
3236 first character of the composition could not be loaded. */
3237 if (s->font_not_found_p)
3238 {
3239 if (s->gidx == 0)
3240 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3241 s->width - 1, s->height - 1);
3242 }
3243 else
3244 {
3245 for (i = 0; i < s->nchars; i++, ++s->gidx)
3246 XDrawString16 (s->display, s->window, s->gc,
3247 x + s->cmp->offsets[s->gidx * 2],
3248 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3249 s->char2b + i, 1);
06a2c219
GM
3250 }
3251}
3252
3253
80c32bcc
GM
3254#ifdef USE_X_TOOLKIT
3255
3e71d8f2 3256static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
3257static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3258 XrmValue *, XrmValue *, XtPointer *));
3259static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3260 XrmValue *, Cardinal *));
80c32bcc 3261
3e71d8f2
GM
3262
3263/* Return the frame on which widget WIDGET is used.. Abort if frame
3264 cannot be determined. */
3265
e851c833 3266static struct frame *
3e71d8f2 3267x_frame_of_widget (widget)
80c32bcc 3268 Widget widget;
80c32bcc 3269{
80c32bcc 3270 struct x_display_info *dpyinfo;
5c187dee 3271 Lisp_Object tail;
3e71d8f2
GM
3272 struct frame *f;
3273
80c32bcc
GM
3274 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3275
3276 /* Find the top-level shell of the widget. Note that this function
3277 can be called when the widget is not yet realized, so XtWindow
3278 (widget) == 0. That's the reason we can't simply use
3279 x_any_window_to_frame. */
3280 while (!XtIsTopLevelShell (widget))
3281 widget = XtParent (widget);
3282
3283 /* Look for a frame with that top-level widget. Allocate the color
3284 on that frame to get the right gamma correction value. */
3285 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3286 if (GC_FRAMEP (XCAR (tail))
3287 && (f = XFRAME (XCAR (tail)),
3288 (f->output_data.nothing != 1
3289 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3290 && f->output_data.x->widget == widget)
3e71d8f2 3291 return f;
80c32bcc
GM
3292
3293 abort ();
3294}
3295
3e71d8f2
GM
3296
3297/* Allocate the color COLOR->pixel on the screen and display of
3298 widget WIDGET in colormap CMAP. If an exact match cannot be
3299 allocated, try the nearest color available. Value is non-zero
3300 if successful. This is called from lwlib. */
3301
3302int
3303x_alloc_nearest_color_for_widget (widget, cmap, color)
3304 Widget widget;
3305 Colormap cmap;
3306 XColor *color;
3307{
3308 struct frame *f = x_frame_of_widget (widget);
3309 return x_alloc_nearest_color (f, cmap, color);
3310}
3311
3312
46d516e5
MB
3313/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3314 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3315 If this produces the same color as PIXEL, try a color where all RGB
3316 values have DELTA added. Return the allocated color in *PIXEL.
3317 DISPLAY is the X display, CMAP is the colormap to operate on.
3318 Value is non-zero if successful. */
3319
3320int
3321x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3322 Widget widget;
3323 Display *display;
3324 Colormap cmap;
3325 unsigned long *pixel;
3326 double factor;
3327 int delta;
3328{
3329 struct frame *f = x_frame_of_widget (widget);
3330 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3331}
3332
3333
651f03b6
GM
3334/* Structure specifying which arguments should be passed by Xt to
3335 cvt_string_to_pixel. We want the widget's screen and colormap. */
3336
3337static XtConvertArgRec cvt_string_to_pixel_args[] =
3338 {
3339 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3340 sizeof (Screen *)},
3341 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3342 sizeof (Colormap)}
3343 };
3344
3345
3346/* The address of this variable is returned by
3347 cvt_string_to_pixel. */
3348
3349static Pixel cvt_string_to_pixel_value;
3350
3351
3352/* Convert a color name to a pixel color.
3353
3354 DPY is the display we are working on.
3355
3356 ARGS is an array of *NARGS XrmValue structures holding additional
3357 information about the widget for which the conversion takes place.
3358 The contents of this array are determined by the specification
3359 in cvt_string_to_pixel_args.
3360
3361 FROM is a pointer to an XrmValue which points to the color name to
3362 convert. TO is an XrmValue in which to return the pixel color.
3363
3364 CLOSURE_RET is a pointer to user-data, in which we record if
3365 we allocated the color or not.
3366
3367 Value is True if successful, False otherwise. */
3368
3369static Boolean
3370cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3371 Display *dpy;
3372 XrmValue *args;
3373 Cardinal *nargs;
3374 XrmValue *from, *to;
3375 XtPointer *closure_ret;
3376{
3377 Screen *screen;
3378 Colormap cmap;
3379 Pixel pixel;
3380 String color_name;
3381 XColor color;
3382
3383 if (*nargs != 2)
3384 {
3385 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3386 "wrongParameters", "cvt_string_to_pixel",
3387 "XtToolkitError",
3388 "Screen and colormap args required", NULL, NULL);
3389 return False;
3390 }
3391
3392 screen = *(Screen **) args[0].addr;
3393 cmap = *(Colormap *) args[1].addr;
3394 color_name = (String) from->addr;
3395
3396 if (strcmp (color_name, XtDefaultBackground) == 0)
3397 {
3398 *closure_ret = (XtPointer) False;
3399 pixel = WhitePixelOfScreen (screen);
3400 }
3401 else if (strcmp (color_name, XtDefaultForeground) == 0)
3402 {
3403 *closure_ret = (XtPointer) False;
3404 pixel = BlackPixelOfScreen (screen);
3405 }
3406 else if (XParseColor (dpy, cmap, color_name, &color)
3407 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3408 {
3409 pixel = color.pixel;
3410 *closure_ret = (XtPointer) True;
3411 }
3412 else
3413 {
3414 String params[1];
3415 Cardinal nparams = 1;
3416
3417 params[0] = color_name;
3418 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3419 "badValue", "cvt_string_to_pixel",
3420 "XtToolkitError", "Invalid color `%s'",
3421 params, &nparams);
3422 return False;
3423 }
3424
3425 if (to->addr != NULL)
3426 {
3427 if (to->size < sizeof (Pixel))
3428 {
3429 to->size = sizeof (Pixel);
3430 return False;
3431 }
3432
3433 *(Pixel *) to->addr = pixel;
3434 }
3435 else
3436 {
3437 cvt_string_to_pixel_value = pixel;
3438 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3439 }
3440
3441 to->size = sizeof (Pixel);
3442 return True;
3443}
3444
3445
3446/* Free a pixel color which was previously allocated via
3447 cvt_string_to_pixel. This is registered as the destructor
3448 for this type of resource via XtSetTypeConverter.
3449
3450 APP is the application context in which we work.
3451
3452 TO is a pointer to an XrmValue holding the color to free.
3453 CLOSURE is the value we stored in CLOSURE_RET for this color
3454 in cvt_string_to_pixel.
3455
3456 ARGS and NARGS are like for cvt_string_to_pixel. */
3457
3458static void
3459cvt_pixel_dtor (app, to, closure, args, nargs)
3460 XtAppContext app;
3461 XrmValuePtr to;
3462 XtPointer closure;
3463 XrmValuePtr args;
3464 Cardinal *nargs;
3465{
3466 if (*nargs != 2)
3467 {
3468 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3469 "XtToolkitError",
3470 "Screen and colormap arguments required",
3471 NULL, NULL);
3472 }
3473 else if (closure != NULL)
3474 {
3475 /* We did allocate the pixel, so free it. */
3476 Screen *screen = *(Screen **) args[0].addr;
3477 Colormap cmap = *(Colormap *) args[1].addr;
3478 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 3479 (Pixel *) to->addr, 1);
651f03b6
GM
3480 }
3481}
3482
3483
80c32bcc
GM
3484#endif /* USE_X_TOOLKIT */
3485
3486
f04e1297 3487/* Value is an array of XColor structures for the contents of the
651f03b6 3488 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
3489 Note that this probably shouldn't be called for large color maps,
3490 say a 24-bit TrueColor map. */
3491
3492static const XColor *
651f03b6
GM
3493x_color_cells (dpy, ncells)
3494 Display *dpy;
f04e1297
GM
3495 int *ncells;
3496{
651f03b6 3497 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
3498
3499 if (dpyinfo->color_cells == NULL)
3500 {
651f03b6 3501 Screen *screen = dpyinfo->screen;
f04e1297
GM
3502 int i;
3503
3504 dpyinfo->ncolor_cells
651f03b6 3505 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
3506 dpyinfo->color_cells
3507 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3508 * sizeof *dpyinfo->color_cells);
3509
3510 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3511 dpyinfo->color_cells[i].pixel = i;
3512
651f03b6 3513 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
3514 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3515 }
3516
3517 *ncells = dpyinfo->ncolor_cells;
3518 return dpyinfo->color_cells;
3519}
3520
3521
3522/* On frame F, translate pixel colors to RGB values for the NCOLORS
3523 colors in COLORS. Use cached information, if available. */
3524
3525void
3526x_query_colors (f, colors, ncolors)
3527 struct frame *f;
3528 XColor *colors;
3529 int ncolors;
3530{
3531 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3532
3533 if (dpyinfo->color_cells)
3534 {
3535 int i;
3536 for (i = 0; i < ncolors; ++i)
3537 {
3538 unsigned long pixel = colors[i].pixel;
3539 xassert (pixel < dpyinfo->ncolor_cells);
3540 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3541 colors[i] = dpyinfo->color_cells[pixel];
3542 }
3543 }
3544 else
3545 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3546}
3547
3548
3549/* On frame F, translate pixel color to RGB values for the color in
3550 COLOR. Use cached information, if available. */
3551
3552void
3553x_query_color (f, color)
3554 struct frame *f;
3555 XColor *color;
3556{
3557 x_query_colors (f, color, 1);
3558}
3559
3560
651f03b6
GM
3561/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3562 exact match can't be allocated, try the nearest color available.
3563 Value is non-zero if successful. Set *COLOR to the color
3564 allocated. */
06a2c219 3565
651f03b6
GM
3566static int
3567x_alloc_nearest_color_1 (dpy, cmap, color)
3568 Display *dpy;
06a2c219
GM
3569 Colormap cmap;
3570 XColor *color;
3571{
80c32bcc
GM
3572 int rc;
3573
651f03b6 3574 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
3575 if (rc == 0)
3576 {
3577 /* If we got to this point, the colormap is full, so we're going
3578 to try to get the next closest color. The algorithm used is
3579 a least-squares matching, which is what X uses for closest
3580 color matching with StaticColor visuals. */
3581 int nearest, i;
3582 unsigned long nearest_delta = ~0;
f04e1297 3583 int ncells;
651f03b6 3584 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
3585
3586 for (nearest = i = 0; i < ncells; ++i)
3587 {
3588 long dred = (color->red >> 8) - (cells[i].red >> 8);
3589 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3590 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3591 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3592
3593 if (delta < nearest_delta)
3594 {
3595 nearest = i;
3596 nearest_delta = delta;
3597 }
3598 }
3599
3600 color->red = cells[nearest].red;
3601 color->green = cells[nearest].green;
3602 color->blue = cells[nearest].blue;
651f03b6 3603 rc = XAllocColor (dpy, cmap, color);
06a2c219 3604 }
35efe0a1
GM
3605 else
3606 {
3607 /* If allocation succeeded, and the allocated pixel color is not
3608 equal to a cached pixel color recorded earlier, there was a
3609 change in the colormap, so clear the color cache. */
651f03b6 3610 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1
GM
3611 XColor *cached_color;
3612
3613 if (dpyinfo->color_cells
3614 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3615 (cached_color->red != color->red
3616 || cached_color->blue != color->blue
3617 || cached_color->green != color->green)))
35efe0a1
GM
3618 {
3619 xfree (dpyinfo->color_cells);
3620 dpyinfo->color_cells = NULL;
3621 dpyinfo->ncolor_cells = 0;
3622 }
3623 }
06a2c219 3624
d9c545da
GM
3625#ifdef DEBUG_X_COLORS
3626 if (rc)
3627 register_color (color->pixel);
3628#endif /* DEBUG_X_COLORS */
3629
06a2c219
GM
3630 return rc;
3631}
3632
3633
651f03b6
GM
3634/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3635 exact match can't be allocated, try the nearest color available.
3636 Value is non-zero if successful. Set *COLOR to the color
3637 allocated. */
3638
3639int
3640x_alloc_nearest_color (f, cmap, color)
3641 struct frame *f;
3642 Colormap cmap;
3643 XColor *color;
3644{
3645 gamma_correct (f, color);
3646 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3647}
3648
3649
d9c545da
GM
3650/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3651 It's necessary to do this instead of just using PIXEL directly to
3652 get color reference counts right. */
3653
3654unsigned long
3655x_copy_color (f, pixel)
3656 struct frame *f;
3657 unsigned long pixel;
3658{
3659 XColor color;
3660
3661 color.pixel = pixel;
3662 BLOCK_INPUT;
f04e1297 3663 x_query_color (f, &color);
d9c545da
GM
3664 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3665 UNBLOCK_INPUT;
3666#ifdef DEBUG_X_COLORS
3667 register_color (pixel);
3668#endif
3669 return color.pixel;
3670}
3671
3672
3e71d8f2
GM
3673/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3674 It's necessary to do this instead of just using PIXEL directly to
3675 get color reference counts right. */
3676
3677unsigned long
3678x_copy_dpy_color (dpy, cmap, pixel)
3679 Display *dpy;
3680 Colormap cmap;
3681 unsigned long pixel;
3682{
3683 XColor color;
3684
3685 color.pixel = pixel;
3686 BLOCK_INPUT;
3687 XQueryColor (dpy, cmap, &color);
3688 XAllocColor (dpy, cmap, &color);
3689 UNBLOCK_INPUT;
3690#ifdef DEBUG_X_COLORS
3691 register_color (pixel);
3692#endif
3693 return color.pixel;
3694}
3695
3696
6d8b0acd 3697/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3698 boosted.
6d8b0acd 3699
d7361edf
MB
3700 Nominally, highlight colors for `3d' faces are calculated by
3701 brightening an object's color by a constant scale factor, but this
3702 doesn't yield good results for dark colors, so for colors who's
3703 brightness is less than this value (on a scale of 0-65535) have an
3704 use an additional additive factor.
6d8b0acd
MB
3705
3706 The value here is set so that the default menu-bar/mode-line color
3707 (grey75) will not have its highlights changed at all. */
3708#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3709
3710
06a2c219
GM
3711/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3712 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3713 If this produces the same color as PIXEL, try a color where all RGB
3714 values have DELTA added. Return the allocated color in *PIXEL.
3715 DISPLAY is the X display, CMAP is the colormap to operate on.
3716 Value is non-zero if successful. */
3717
3718static int
3719x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3720 struct frame *f;
3721 Display *display;
3722 Colormap cmap;
3723 unsigned long *pixel;
68c45bf0 3724 double factor;
06a2c219
GM
3725 int delta;
3726{
3727 XColor color, new;
6d8b0acd 3728 long bright;
06a2c219
GM
3729 int success_p;
3730
3731 /* Get RGB color values. */
3732 color.pixel = *pixel;
f04e1297 3733 x_query_color (f, &color);
06a2c219
GM
3734
3735 /* Change RGB values by specified FACTOR. Avoid overflow! */
3736 xassert (factor >= 0);
3737 new.red = min (0xffff, factor * color.red);
3738 new.green = min (0xffff, factor * color.green);
3739 new.blue = min (0xffff, factor * color.blue);
3740
d7361edf
MB
3741 /* Calculate brightness of COLOR. */
3742 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3743
3744 /* We only boost colors that are darker than
3745 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3746 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3747 /* Make an additive adjustment to NEW, because it's dark enough so
3748 that scaling by FACTOR alone isn't enough. */
3749 {
3750 /* How far below the limit this color is (0 - 1, 1 being darker). */
3751 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3752 /* The additive adjustment. */
d7361edf 3753 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3754
3755 if (factor < 1)
3756 {
6d8b0acd
MB
3757 new.red = max (0, new.red - min_delta);
3758 new.green = max (0, new.green - min_delta);
3759 new.blue = max (0, new.blue - min_delta);
3760 }
3761 else
3762 {
3763 new.red = min (0xffff, min_delta + new.red);
3764 new.green = min (0xffff, min_delta + new.green);
3765 new.blue = min (0xffff, min_delta + new.blue);
3766 }
3767 }
3768
06a2c219 3769 /* Try to allocate the color. */
80c32bcc 3770 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3771 if (success_p)
3772 {
3773 if (new.pixel == *pixel)
3774 {
3775 /* If we end up with the same color as before, try adding
3776 delta to the RGB values. */
0d605c67 3777 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3778
3779 new.red = min (0xffff, delta + color.red);
3780 new.green = min (0xffff, delta + color.green);
3781 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3782 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3783 }
3784 else
3785 success_p = 1;
3786 *pixel = new.pixel;
3787 }
3788
3789 return success_p;
3790}
3791
3792
3793/* Set up the foreground color for drawing relief lines of glyph
3794 string S. RELIEF is a pointer to a struct relief containing the GC
3795 with which lines will be drawn. Use a color that is FACTOR or
3796 DELTA lighter or darker than the relief's background which is found
3797 in S->f->output_data.x->relief_background. If such a color cannot
3798 be allocated, use DEFAULT_PIXEL, instead. */
3799
3800static void
3801x_setup_relief_color (f, relief, factor, delta, default_pixel)
3802 struct frame *f;
3803 struct relief *relief;
68c45bf0 3804 double factor;
06a2c219
GM
3805 int delta;
3806 unsigned long default_pixel;
3807{
3808 XGCValues xgcv;
3809 struct x_output *di = f->output_data.x;
3810 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3811 unsigned long pixel;
3812 unsigned long background = di->relief_background;
43bd1b2b 3813 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3814 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3815 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3816
3817 xgcv.graphics_exposures = False;
3818 xgcv.line_width = 1;
3819
3820 /* Free previously allocated color. The color cell will be reused
3821 when it has been freed as many times as it was allocated, so this
3822 doesn't affect faces using the same colors. */
3823 if (relief->gc
3824 && relief->allocated_p)
3825 {
0d605c67 3826 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3827 relief->allocated_p = 0;
3828 }
3829
3830 /* Allocate new color. */
3831 xgcv.foreground = default_pixel;
3832 pixel = background;
dcd08bfb
GM
3833 if (dpyinfo->n_planes != 1
3834 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3835 {
3836 relief->allocated_p = 1;
3837 xgcv.foreground = relief->pixel = pixel;
3838 }
3839
3840 if (relief->gc == 0)
3841 {
dcd08bfb 3842 xgcv.stipple = dpyinfo->gray;
06a2c219 3843 mask |= GCStipple;
dcd08bfb 3844 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3845 }
3846 else
dcd08bfb 3847 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3848}
3849
3850
3851/* Set up colors for the relief lines around glyph string S. */
3852
3853static void
3854x_setup_relief_colors (s)
3855 struct glyph_string *s;
3856{
3857 struct x_output *di = s->f->output_data.x;
3858 unsigned long color;
3859
3860 if (s->face->use_box_color_for_shadows_p)
3861 color = s->face->box_color;
e2a57b34 3862 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 3863 && s->img->pixmap
e2a57b34
MB
3864 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3865 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
3866 else
3867 {
3868 XGCValues xgcv;
3869
3870 /* Get the background color of the face. */
3871 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3872 color = xgcv.background;
3873 }
3874
3875 if (di->white_relief.gc == 0
3876 || color != di->relief_background)
3877 {
3878 di->relief_background = color;
3879 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3880 WHITE_PIX_DEFAULT (s->f));
3881 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3882 BLACK_PIX_DEFAULT (s->f));
3883 }
3884}
3885
3886
3887/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3888 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3889 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3890 relief. LEFT_P non-zero means draw a relief on the left side of
3891 the rectangle. RIGHT_P non-zero means draw a relief on the right
3892 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3893 when drawing. */
3894
3895static void
3896x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3897 raised_p, left_p, right_p, clip_rect)
3898 struct frame *f;
3899 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3900 XRectangle *clip_rect;
3901{
de507556
GM
3902 Display *dpy = FRAME_X_DISPLAY (f);
3903 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
3904 int i;
3905 GC gc;
3906
3907 if (raised_p)
3908 gc = f->output_data.x->white_relief.gc;
3909 else
3910 gc = f->output_data.x->black_relief.gc;
de507556 3911 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3912
3913 /* Top. */
3914 for (i = 0; i < width; ++i)
de507556 3915 XDrawLine (dpy, window, gc,
06a2c219
GM
3916 left_x + i * left_p, top_y + i,
3917 right_x + 1 - i * right_p, top_y + i);
3918
3919 /* Left. */
3920 if (left_p)
3921 for (i = 0; i < width; ++i)
de507556 3922 XDrawLine (dpy, window, gc,
44655e77 3923 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 3924
de507556 3925 XSetClipMask (dpy, gc, None);
06a2c219
GM
3926 if (raised_p)
3927 gc = f->output_data.x->black_relief.gc;
3928 else
3929 gc = f->output_data.x->white_relief.gc;
de507556 3930 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3931
3932 /* Bottom. */
3933 for (i = 0; i < width; ++i)
de507556
GM
3934 XDrawLine (dpy, window, gc,
3935 left_x + i * left_p, bottom_y - i,
327f42ee 3936 right_x + 1 - i * right_p, bottom_y - i);
06a2c219
GM
3937
3938 /* Right. */
3939 if (right_p)
3940 for (i = 0; i < width; ++i)
de507556 3941 XDrawLine (dpy, window, gc,
06a2c219
GM
3942 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3943
de507556 3944 XSetClipMask (dpy, gc, None);
06a2c219
GM
3945}
3946
3947
3948/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3949 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3950 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3951 left side of the rectangle. RIGHT_P non-zero means draw a line
3952 on the right side of the rectangle. CLIP_RECT is the clipping
3953 rectangle to use when drawing. */
3954
3955static void
3956x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3957 left_p, right_p, clip_rect)
3958 struct glyph_string *s;
3959 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3960 XRectangle *clip_rect;
3961{
3962 XGCValues xgcv;
3963
3964 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3965 XSetForeground (s->display, s->gc, s->face->box_color);
3966 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3967
3968 /* Top. */
3969 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3970 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3971
3972 /* Left. */
3973 if (left_p)
3974 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3975 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3976
3977 /* Bottom. */
3978 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3979 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3980
3981 /* Right. */
3982 if (right_p)
3983 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3984 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3985
3986 XSetForeground (s->display, s->gc, xgcv.foreground);
3987 XSetClipMask (s->display, s->gc, None);
3988}
3989
3990
3991/* Draw a box around glyph string S. */
3992
3993static void
3994x_draw_glyph_string_box (s)
3995 struct glyph_string *s;
3996{
3997 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3998 int left_p, right_p;
3999 struct glyph *last_glyph;
4000 XRectangle clip_rect;
4001
4002 last_x = window_box_right (s->w, s->area);
4003 if (s->row->full_width_p
4004 && !s->w->pseudo_window_p)
4005 {
3f332ef3 4006 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
06a2c219
GM
4007 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4008 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4009 }
4010
4011 /* The glyph that may have a right box line. */
b4192550 4012 last_glyph = (s->cmp || s->img
06a2c219
GM
4013 ? s->first_glyph
4014 : s->first_glyph + s->nchars - 1);
4015
ea2ba0d4 4016 width = abs (s->face->box_line_width);
06a2c219
GM
4017 raised_p = s->face->box == FACE_RAISED_BOX;
4018 left_x = s->x;
57ac7c81
GM
4019 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4020 ? last_x - 1
4021 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
4022 top_y = s->y;
4023 bottom_y = top_y + s->height - 1;
4024
4025 left_p = (s->first_glyph->left_box_line_p
4026 || (s->hl == DRAW_MOUSE_FACE
4027 && (s->prev == NULL
4028 || s->prev->hl != s->hl)));
4029 right_p = (last_glyph->right_box_line_p
4030 || (s->hl == DRAW_MOUSE_FACE
4031 && (s->next == NULL
4032 || s->next->hl != s->hl)));
327f42ee 4033
06a2c219
GM
4034 x_get_glyph_string_clip_rect (s, &clip_rect);
4035
4036 if (s->face->box == FACE_SIMPLE_BOX)
4037 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4038 left_p, right_p, &clip_rect);
4039 else
4040 {
4041 x_setup_relief_colors (s);
4042 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4043 width, raised_p, left_p, right_p, &clip_rect);
4044 }
4045}
4046
4047
4048/* Draw foreground of image glyph string S. */
4049
4050static void
4051x_draw_image_foreground (s)
4052 struct glyph_string *s;
4053{
4054 int x;
95af8492 4055 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4056
4057 /* If first glyph of S has a left box line, start drawing it to the
4058 right of that line. */
4059 if (s->face->box != FACE_NO_BOX
4060 && s->first_glyph->left_box_line_p)
ea2ba0d4 4061 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4062 else
4063 x = s->x;
4064
4065 /* If there is a margin around the image, adjust x- and y-position
4066 by that margin. */
22d650b8
GM
4067 x += s->img->hmargin;
4068 y += s->img->vmargin;
06a2c219
GM
4069
4070 if (s->img->pixmap)
4071 {
4072 if (s->img->mask)
4073 {
4074 /* We can't set both a clip mask and use XSetClipRectangles
4075 because the latter also sets a clip mask. We also can't
4076 trust on the shape extension to be available
4077 (XShapeCombineRegion). So, compute the rectangle to draw
4078 manually. */
4079 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4080 | GCFunction);
4081 XGCValues xgcv;
4082 XRectangle clip_rect, image_rect, r;
4083
4084 xgcv.clip_mask = s->img->mask;
4085 xgcv.clip_x_origin = x;
4086 xgcv.clip_y_origin = y;
4087 xgcv.function = GXcopy;
4088 XChangeGC (s->display, s->gc, mask, &xgcv);
4089
4090 x_get_glyph_string_clip_rect (s, &clip_rect);
4091 image_rect.x = x;
4092 image_rect.y = y;
4093 image_rect.width = s->img->width;
4094 image_rect.height = s->img->height;
4095 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4096 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4097 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4098 }
4099 else
4100 {
49ad1d99
GM
4101 XRectangle clip_rect, image_rect, r;
4102
4103 x_get_glyph_string_clip_rect (s, &clip_rect);
4104 image_rect.x = x;
4105 image_rect.y = y;
4106 image_rect.width = s->img->width;
4107 image_rect.height = s->img->height;
4108 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4109 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4110 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
4111
4112 /* When the image has a mask, we can expect that at
4113 least part of a mouse highlight or a block cursor will
4114 be visible. If the image doesn't have a mask, make
4115 a block cursor visible by drawing a rectangle around
4116 the image. I believe it's looking better if we do
4117 nothing here for mouse-face. */
4118 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4119 {
4120 int r = s->img->relief;
4121 if (r < 0) r = -r;
4122 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4123 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4124 }
06a2c219
GM
4125 }
4126 }
4127 else
4128 /* Draw a rectangle if image could not be loaded. */
4129 XDrawRectangle (s->display, s->window, s->gc, x, y,
4130 s->img->width - 1, s->img->height - 1);
4131}
4132
4133
4134/* Draw a relief around the image glyph string S. */
4135
4136static void
4137x_draw_image_relief (s)
4138 struct glyph_string *s;
4139{
4140 int x0, y0, x1, y1, thick, raised_p;
4141 XRectangle r;
4142 int x;
95af8492 4143 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4144
4145 /* If first glyph of S has a left box line, start drawing it to the
4146 right of that line. */
4147 if (s->face->box != FACE_NO_BOX
4148 && s->first_glyph->left_box_line_p)
ea2ba0d4 4149 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4150 else
4151 x = s->x;
4152
4153 /* If there is a margin around the image, adjust x- and y-position
4154 by that margin. */
22d650b8
GM
4155 x += s->img->hmargin;
4156 y += s->img->vmargin;
06a2c219
GM
4157
4158 if (s->hl == DRAW_IMAGE_SUNKEN
4159 || s->hl == DRAW_IMAGE_RAISED)
4160 {
62854fe2 4161 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
4162 raised_p = s->hl == DRAW_IMAGE_RAISED;
4163 }
4164 else
4165 {
4166 thick = abs (s->img->relief);
4167 raised_p = s->img->relief > 0;
4168 }
4169
4170 x0 = x - thick;
4171 y0 = y - thick;
4172 x1 = x + s->img->width + thick - 1;
4173 y1 = y + s->img->height + thick - 1;
4174
4175 x_setup_relief_colors (s);
4176 x_get_glyph_string_clip_rect (s, &r);
4177 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4178}
4179
4180
4181/* Draw the foreground of image glyph string S to PIXMAP. */
4182
4183static void
4184x_draw_image_foreground_1 (s, pixmap)
4185 struct glyph_string *s;
4186 Pixmap pixmap;
4187{
4188 int x;
95af8492 4189 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4190
4191 /* If first glyph of S has a left box line, start drawing it to the
4192 right of that line. */
4193 if (s->face->box != FACE_NO_BOX
4194 && s->first_glyph->left_box_line_p)
ea2ba0d4 4195 x = abs (s->face->box_line_width);
06a2c219
GM
4196 else
4197 x = 0;
4198
4199 /* If there is a margin around the image, adjust x- and y-position
4200 by that margin. */
22d650b8
GM
4201 x += s->img->hmargin;
4202 y += s->img->vmargin;
dc43ef94 4203
06a2c219
GM
4204 if (s->img->pixmap)
4205 {
4206 if (s->img->mask)
4207 {
4208 /* We can't set both a clip mask and use XSetClipRectangles
4209 because the latter also sets a clip mask. We also can't
4210 trust on the shape extension to be available
4211 (XShapeCombineRegion). So, compute the rectangle to draw
4212 manually. */
4213 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4214 | GCFunction);
4215 XGCValues xgcv;
4216
4217 xgcv.clip_mask = s->img->mask;
4218 xgcv.clip_x_origin = x;
4219 xgcv.clip_y_origin = y;
4220 xgcv.function = GXcopy;
4221 XChangeGC (s->display, s->gc, mask, &xgcv);
4222
4223 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4224 0, 0, s->img->width, s->img->height, x, y);
4225 XSetClipMask (s->display, s->gc, None);
4226 }
4227 else
4228 {
4229 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4230 0, 0, s->img->width, s->img->height, x, y);
4231
4232 /* When the image has a mask, we can expect that at
4233 least part of a mouse highlight or a block cursor will
4234 be visible. If the image doesn't have a mask, make
4235 a block cursor visible by drawing a rectangle around
4236 the image. I believe it's looking better if we do
4237 nothing here for mouse-face. */
4238 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4239 {
4240 int r = s->img->relief;
4241 if (r < 0) r = -r;
4242 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4243 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4244 }
06a2c219
GM
4245 }
4246 }
4247 else
4248 /* Draw a rectangle if image could not be loaded. */
4249 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4250 s->img->width - 1, s->img->height - 1);
4251}
dc43ef94 4252
990ba854 4253
06a2c219
GM
4254/* Draw part of the background of glyph string S. X, Y, W, and H
4255 give the rectangle to draw. */
a9a5b0a5 4256
06a2c219
GM
4257static void
4258x_draw_glyph_string_bg_rect (s, x, y, w, h)
4259 struct glyph_string *s;
4260 int x, y, w, h;
4261{
4262 if (s->stippled_p)
4263 {
4264 /* Fill background with a stipple pattern. */
4265 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4266 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4267 XSetFillStyle (s->display, s->gc, FillSolid);
4268 }
4269 else
4270 x_clear_glyph_string_rect (s, x, y, w, h);
4271}
07e34cb0 4272
b5210ea7 4273
06a2c219 4274/* Draw image glyph string S.
dc43ef94 4275
06a2c219
GM
4276 s->y
4277 s->x +-------------------------
4278 | s->face->box
4279 |
4280 | +-------------------------
4281 | | s->img->margin
4282 | |
4283 | | +-------------------
4284 | | | the image
dc43ef94 4285
06a2c219 4286 */
dc43ef94 4287
06a2c219
GM
4288static void
4289x_draw_image_glyph_string (s)
4290 struct glyph_string *s;
4291{
4292 int x, y;
ea2ba0d4
KH
4293 int box_line_hwidth = abs (s->face->box_line_width);
4294 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4295 int height;
4296 Pixmap pixmap = None;
4297
ea2ba0d4 4298 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4299
4300 /* Fill background with face under the image. Do it only if row is
4301 taller than image or if image has a clip mask to reduce
4302 flickering. */
4303 s->stippled_p = s->face->stipple != 0;
4304 if (height > s->img->height
22d650b8
GM
4305 || s->img->hmargin
4306 || s->img->vmargin
06a2c219
GM
4307 || s->img->mask
4308 || s->img->pixmap == 0
4309 || s->width != s->background_width)
4310 {
ea2ba0d4
KH
4311 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4312 x = s->x + box_line_hwidth;
06a2c219
GM
4313 else
4314 x = s->x;
4315
ea2ba0d4 4316 y = s->y + box_line_vwidth;
06a2c219
GM
4317
4318 if (s->img->mask)
4319 {
f9b5db02
GM
4320 /* Create a pixmap as large as the glyph string. Fill it
4321 with the background color. Copy the image to it, using
4322 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4323 Screen *screen = FRAME_X_SCREEN (s->f);
4324 int depth = DefaultDepthOfScreen (screen);
4325
4326 /* Create a pixmap as large as the glyph string. */
4327 pixmap = XCreatePixmap (s->display, s->window,
4328 s->background_width,
4329 s->height, depth);
4330
4331 /* Don't clip in the following because we're working on the
4332 pixmap. */
4333 XSetClipMask (s->display, s->gc, None);
4334
4335 /* Fill the pixmap with the background color/stipple. */
4336 if (s->stippled_p)
4337 {
4338 /* Fill background with a stipple pattern. */
4339 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4340 XFillRectangle (s->display, pixmap, s->gc,
4341 0, 0, s->background_width, s->height);
4342 XSetFillStyle (s->display, s->gc, FillSolid);
4343 }
4344 else
4345 {
4346 XGCValues xgcv;
4347 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4348 &xgcv);
4349 XSetForeground (s->display, s->gc, xgcv.background);
4350 XFillRectangle (s->display, pixmap, s->gc,
4351 0, 0, s->background_width, s->height);
4352 XSetForeground (s->display, s->gc, xgcv.foreground);
4353 }
4354 }
4355 else
06a2c219
GM
4356 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4357
4358 s->background_filled_p = 1;
4359 }
dc43ef94 4360
06a2c219
GM
4361 /* Draw the foreground. */
4362 if (pixmap != None)
4363 {
4364 x_draw_image_foreground_1 (s, pixmap);
4365 x_set_glyph_string_clipping (s);
4366 XCopyArea (s->display, pixmap, s->window, s->gc,
4367 0, 0, s->background_width, s->height, s->x, s->y);
4368 XFreePixmap (s->display, pixmap);
4369 }
4370 else
4371 x_draw_image_foreground (s);
b5210ea7 4372
06a2c219
GM
4373 /* If we must draw a relief around the image, do it. */
4374 if (s->img->relief
4375 || s->hl == DRAW_IMAGE_RAISED
4376 || s->hl == DRAW_IMAGE_SUNKEN)
4377 x_draw_image_relief (s);
4378}
8c1a6a84 4379
990ba854 4380
06a2c219 4381/* Draw stretch glyph string S. */
dc43ef94 4382
06a2c219
GM
4383static void
4384x_draw_stretch_glyph_string (s)
4385 struct glyph_string *s;
4386{
4387 xassert (s->first_glyph->type == STRETCH_GLYPH);
4388 s->stippled_p = s->face->stipple != 0;
990ba854 4389
06a2c219
GM
4390 if (s->hl == DRAW_CURSOR
4391 && !x_stretch_cursor_p)
4392 {
4393 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4394 as wide as the stretch glyph. */
4395 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4396
06a2c219
GM
4397 /* Draw cursor. */
4398 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4399
06a2c219
GM
4400 /* Clear rest using the GC of the original non-cursor face. */
4401 if (width < s->background_width)
4402 {
06a2c219
GM
4403 int x = s->x + width, y = s->y;
4404 int w = s->background_width - width, h = s->height;
4405 XRectangle r;
b7f83f9e 4406 GC gc;
dc43ef94 4407
b7f83f9e
GM
4408 if (s->row->mouse_face_p
4409 && cursor_in_mouse_face_p (s->w))
4410 {
4411 x_set_mouse_face_gc (s);
4412 gc = s->gc;
4413 }
4414 else
4415 gc = s->face->gc;
4416
06a2c219
GM
4417 x_get_glyph_string_clip_rect (s, &r);
4418 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
b7f83f9e 4419
06a2c219
GM
4420 if (s->face->stipple)
4421 {
4422 /* Fill background with a stipple pattern. */
4423 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4424 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4425 XSetFillStyle (s->display, gc, FillSolid);
4426 }
4427 else
4428 {
4429 XGCValues xgcv;
4430 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4431 XSetForeground (s->display, gc, xgcv.background);
4432 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4433 XSetForeground (s->display, gc, xgcv.foreground);
4434 }
4435 }
4436 }
61e9f9f3 4437 else if (!s->background_filled_p)
06a2c219
GM
4438 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4439 s->height);
4440
4441 s->background_filled_p = 1;
4442}
4443
4444
4445/* Draw glyph string S. */
4446
4447static void
4448x_draw_glyph_string (s)
4449 struct glyph_string *s;
4450{
4458cf11
KH
4451 int relief_drawn_p = 0;
4452
06a2c219
GM
4453 /* If S draws into the background of its successor, draw the
4454 background of the successor first so that S can draw into it.
4455 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4456 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4457 {
4458 xassert (s->next->img == NULL);
4459 x_set_glyph_string_gc (s->next);
4460 x_set_glyph_string_clipping (s->next);
4461 x_draw_glyph_string_background (s->next, 1);
4462 }
97210f4e 4463
06a2c219
GM
4464 /* Set up S->gc, set clipping and draw S. */
4465 x_set_glyph_string_gc (s);
06a2c219 4466
4458cf11
KH
4467 /* Draw relief (if any) in advance for char/composition so that the
4468 glyph string can be drawn over it. */
4469 if (!s->for_overlaps_p
4470 && s->face->box != FACE_NO_BOX
4471 && (s->first_glyph->type == CHAR_GLYPH
4472 || s->first_glyph->type == COMPOSITE_GLYPH))
4473
4474 {
e6269cbb 4475 x_set_glyph_string_clipping (s);
4458cf11
KH
4476 x_draw_glyph_string_background (s, 1);
4477 x_draw_glyph_string_box (s);
e6269cbb 4478 x_set_glyph_string_clipping (s);
4458cf11
KH
4479 relief_drawn_p = 1;
4480 }
e6269cbb
GM
4481 else
4482 x_set_glyph_string_clipping (s);
4458cf11 4483
06a2c219
GM
4484 switch (s->first_glyph->type)
4485 {
4486 case IMAGE_GLYPH:
4487 x_draw_image_glyph_string (s);
4488 break;
4489
4490 case STRETCH_GLYPH:
4491 x_draw_stretch_glyph_string (s);
4492 break;
4493
4494 case CHAR_GLYPH:
66ac4b0e
GM
4495 if (s->for_overlaps_p)
4496 s->background_filled_p = 1;
4497 else
4498 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4499 x_draw_glyph_string_foreground (s);
4500 break;
4501
b4192550
KH
4502 case COMPOSITE_GLYPH:
4503 if (s->for_overlaps_p || s->gidx > 0)
4504 s->background_filled_p = 1;
4505 else
4506 x_draw_glyph_string_background (s, 1);
4507 x_draw_composite_glyph_string_foreground (s);
4508 break;
4509
06a2c219
GM
4510 default:
4511 abort ();
4512 }
4513
66ac4b0e 4514 if (!s->for_overlaps_p)
06a2c219 4515 {
66ac4b0e
GM
4516 /* Draw underline. */
4517 if (s->face->underline_p)
4518 {
e24e84cc
GM
4519 unsigned long tem, h;
4520 int y;
06a2c219 4521
e24e84cc 4522 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4523 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4524 h = 1;
e24e84cc
GM
4525
4526 /* Get the underline position. This is the recommended
4527 vertical offset in pixels from the baseline to the top of
4528 the underline. This is a signed value according to the
4529 specs, and its default is
4530
4531 ROUND ((maximum descent) / 2), with
4532 ROUND(x) = floor (x + 0.5) */
4533
a72d5ce5
GM
4534 if (x_use_underline_position_properties
4535 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
4536 y = s->ybase + (long) tem;
4537 else if (s->face->font)
4538 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4539 else
a02f1be0 4540 y = s->y + s->height - h;
06a2c219 4541
66ac4b0e 4542 if (s->face->underline_defaulted_p)
e24e84cc
GM
4543 XFillRectangle (s->display, s->window, s->gc,
4544 s->x, y, s->width, h);
66ac4b0e
GM
4545 else
4546 {
4547 XGCValues xgcv;
4548 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4549 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4550 XFillRectangle (s->display, s->window, s->gc,
4551 s->x, y, s->width, h);
66ac4b0e
GM
4552 XSetForeground (s->display, s->gc, xgcv.foreground);
4553 }
dc6f92b8 4554 }
07e34cb0 4555
66ac4b0e
GM
4556 /* Draw overline. */
4557 if (s->face->overline_p)
06a2c219 4558 {
66ac4b0e
GM
4559 unsigned long dy = 0, h = 1;
4560
4561 if (s->face->overline_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->overline_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
66ac4b0e
GM
4575 /* Draw strike-through. */
4576 if (s->face->strike_through_p)
06a2c219 4577 {
66ac4b0e
GM
4578 unsigned long h = 1;
4579 unsigned long dy = (s->height - h) / 2;
4580
4581 if (s->face->strike_through_color_defaulted_p)
4582 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4583 s->width, h);
4584 else
4585 {
4586 XGCValues xgcv;
4587 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4588 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4589 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4590 s->width, h);
4591 XSetForeground (s->display, s->gc, xgcv.foreground);
4592 }
06a2c219 4593 }
06a2c219 4594
4458cf11
KH
4595 /* Draw relief if not yet drawn. */
4596 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
4597 x_draw_glyph_string_box (s);
4598 }
06a2c219
GM
4599
4600 /* Reset clipping. */
4601 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4602}
07e34cb0 4603
06a2c219 4604
b4192550
KH
4605static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4606 struct face **, int));
06a2c219 4607
06a2c219 4608
209f68d9
GM
4609/* Fill glyph string S with composition components specified by S->cmp.
4610
b4192550
KH
4611 FACES is an array of faces for all components of this composition.
4612 S->gidx is the index of the first component for S.
4613 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4614 use its physical height for clipping.
06a2c219 4615
b4192550 4616 Value is the index of a component not in S. */
07e34cb0 4617
b4192550
KH
4618static int
4619x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4620 struct glyph_string *s;
b4192550 4621 struct face **faces;
66ac4b0e 4622 int overlaps_p;
07e34cb0 4623{
b4192550 4624 int i;
06a2c219 4625
b4192550 4626 xassert (s);
06a2c219 4627
b4192550 4628 s->for_overlaps_p = overlaps_p;
06a2c219 4629
b4192550
KH
4630 s->face = faces[s->gidx];
4631 s->font = s->face->font;
4632 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4633
b4192550
KH
4634 /* For all glyphs of this composition, starting at the offset
4635 S->gidx, until we reach the end of the definition or encounter a
4636 glyph that requires the different face, add it to S. */
4637 ++s->nchars;
4638 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4639 ++s->nchars;
06a2c219 4640
b4192550
KH
4641 /* All glyph strings for the same composition has the same width,
4642 i.e. the width set for the first component of the composition. */
06a2c219 4643
06a2c219
GM
4644 s->width = s->first_glyph->pixel_width;
4645
4646 /* If the specified font could not be loaded, use the frame's
4647 default font, but record the fact that we couldn't load it in
4648 the glyph string so that we can draw rectangles for the
4649 characters of the glyph string. */
4650 if (s->font == NULL)
4651 {
4652 s->font_not_found_p = 1;
4653 s->font = FRAME_FONT (s->f);
4654 }
4655
4656 /* Adjust base line for subscript/superscript text. */
4657 s->ybase += s->first_glyph->voffset;
4658
4659 xassert (s->face && s->face->gc);
4660
4661 /* This glyph string must always be drawn with 16-bit functions. */
4662 s->two_byte_p = 1;
b4192550
KH
4663
4664 return s->gidx + s->nchars;
06a2c219
GM
4665}
4666
4667
209f68d9
GM
4668/* Fill glyph string S from a sequence of character glyphs.
4669
06a2c219 4670 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4671 first glyph to consider, END is the index of the last + 1.
4672 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4673 use its physical height for clipping.
66ac4b0e
GM
4674
4675 Value is the index of the first glyph not in S. */
06a2c219
GM
4676
4677static int
66ac4b0e 4678x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4679 struct glyph_string *s;
4680 int face_id;
66ac4b0e 4681 int start, end, overlaps_p;
06a2c219
GM
4682{
4683 struct glyph *glyph, *last;
4684 int voffset;
ee569018 4685 int glyph_not_available_p;
06a2c219 4686
06a2c219
GM
4687 xassert (s->f == XFRAME (s->w->frame));
4688 xassert (s->nchars == 0);
4689 xassert (start >= 0 && end > start);
4690
66ac4b0e 4691 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4692 glyph = s->row->glyphs[s->area] + start;
4693 last = s->row->glyphs[s->area] + end;
4694 voffset = glyph->voffset;
4695
ee569018
KH
4696 glyph_not_available_p = glyph->glyph_not_available_p;
4697
06a2c219
GM
4698 while (glyph < last
4699 && glyph->type == CHAR_GLYPH
4700 && glyph->voffset == voffset
ee569018
KH
4701 /* Same face id implies same font, nowadays. */
4702 && glyph->face_id == face_id
4703 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4704 {
ee569018
KH
4705 int two_byte_p;
4706
06a2c219 4707 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4708 s->char2b + s->nchars,
4709 &two_byte_p);
4710 s->two_byte_p = two_byte_p;
06a2c219
GM
4711 ++s->nchars;
4712 xassert (s->nchars <= end - start);
4713 s->width += glyph->pixel_width;
4714 ++glyph;
4715 }
4716
4717 s->font = s->face->font;
4718 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4719
4720 /* If the specified font could not be loaded, use the frame's font,
4721 but record the fact that we couldn't load it in
4722 S->font_not_found_p so that we can draw rectangles for the
4723 characters of the glyph string. */
ee569018 4724 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4725 {
4726 s->font_not_found_p = 1;
4727 s->font = FRAME_FONT (s->f);
4728 }
4729
4730 /* Adjust base line for subscript/superscript text. */
4731 s->ybase += voffset;
66ac4b0e 4732
06a2c219
GM
4733 xassert (s->face && s->face->gc);
4734 return glyph - s->row->glyphs[s->area];
07e34cb0 4735}
dc6f92b8 4736
06a2c219
GM
4737
4738/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4739
dfcf069d 4740static void
06a2c219
GM
4741x_fill_image_glyph_string (s)
4742 struct glyph_string *s;
4743{
4744 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4745 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4746 xassert (s->img);
43d120d8 4747 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4748 s->font = s->face->font;
4749 s->width = s->first_glyph->pixel_width;
4750
4751 /* Adjust base line for subscript/superscript text. */
4752 s->ybase += s->first_glyph->voffset;
4753}
4754
4755
209f68d9 4756/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4757
209f68d9
GM
4758 ROW is the glyph row in which the glyphs are found, AREA is the
4759 area within the row. START is the index of the first glyph to
4760 consider, END is the index of the last + 1.
4761
4762 Value is the index of the first glyph not in S. */
4763
4764static int
4765x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4766 struct glyph_string *s;
209f68d9
GM
4767 struct glyph_row *row;
4768 enum glyph_row_area area;
4769 int start, end;
06a2c219 4770{
209f68d9
GM
4771 struct glyph *glyph, *last;
4772 int voffset, face_id;
4773
06a2c219 4774 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4775
4776 glyph = s->row->glyphs[s->area] + start;
4777 last = s->row->glyphs[s->area] + end;
4778 face_id = glyph->face_id;
4779 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4780 s->font = s->face->font;
209f68d9
GM
4781 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4782 s->width = glyph->pixel_width;
4783 voffset = glyph->voffset;
4784
4785 for (++glyph;
4786 (glyph < last
4787 && glyph->type == STRETCH_GLYPH
4788 && glyph->voffset == voffset
4789 && glyph->face_id == face_id);
4790 ++glyph)
4791 s->width += glyph->pixel_width;
06a2c219
GM
4792
4793 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4794 s->ybase += voffset;
4795
c296fc01
GM
4796 /* The case that face->gc == 0 is handled when drawing the glyph
4797 string by calling PREPARE_FACE_FOR_DISPLAY. */
4798 xassert (s->face);
209f68d9 4799 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4800}
4801
4802
4803/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4804 of XChar2b structures for S; it can't be allocated in
4805 x_init_glyph_string because it must be allocated via `alloca'. W
4806 is the window on which S is drawn. ROW and AREA are the glyph row
4807 and area within the row from which S is constructed. START is the
4808 index of the first glyph structure covered by S. HL is a
4809 face-override for drawing S. */
4810
4811static void
4812x_init_glyph_string (s, char2b, w, row, area, start, hl)
4813 struct glyph_string *s;
4814 XChar2b *char2b;
4815 struct window *w;
4816 struct glyph_row *row;
4817 enum glyph_row_area area;
4818 int start;
4819 enum draw_glyphs_face hl;
4820{
4821 bzero (s, sizeof *s);
4822 s->w = w;
4823 s->f = XFRAME (w->frame);
4824 s->display = FRAME_X_DISPLAY (s->f);
4825 s->window = FRAME_X_WINDOW (s->f);
4826 s->char2b = char2b;
4827 s->hl = hl;
4828 s->row = row;
4829 s->area = area;
4830 s->first_glyph = row->glyphs[area] + start;
4831 s->height = row->height;
4832 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4833
9ea173e8
GM
4834 /* Display the internal border below the tool-bar window. */
4835 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4836 s->y -= s->f->output_data.x->internal_border_width;
4837
4838 s->ybase = s->y + row->ascent;
4839}
4840
4841
4842/* Set background width of glyph string S. START is the index of the
4843 first glyph following S. LAST_X is the right-most x-position + 1
4844 in the drawing area. */
4845
4846static INLINE void
4847x_set_glyph_string_background_width (s, start, last_x)
4848 struct glyph_string *s;
4849 int start;
4850 int last_x;
4851{
4852 /* If the face of this glyph string has to be drawn to the end of
4853 the drawing area, set S->extends_to_end_of_line_p. */
4854 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4855
4856 if (start == s->row->used[s->area]
eb79f5cc 4857 && s->area == TEXT_AREA
7b0870b2
GM
4858 && ((s->hl == DRAW_NORMAL_TEXT
4859 && (s->row->fill_line_p
4860 || s->face->background != default_face->background
4861 || s->face->stipple != default_face->stipple
4862 || s->row->mouse_face_p))
327f42ee
GM
4863 || s->hl == DRAW_MOUSE_FACE
4864 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4865 && s->row->fill_line_p)))
7b0870b2 4866 s->extends_to_end_of_line_p = 1;
06a2c219
GM
4867
4868 /* If S extends its face to the end of the line, set its
4869 background_width to the distance to the right edge of the drawing
4870 area. */
4871 if (s->extends_to_end_of_line_p)
1da3fd71 4872 s->background_width = last_x - s->x + 1;
06a2c219
GM
4873 else
4874 s->background_width = s->width;
4875}
4876
4877
4878/* Add a glyph string for a stretch glyph to the list of strings
4879 between HEAD and TAIL. START is the index of the stretch glyph in
4880 row area AREA of glyph row ROW. END is the index of the last glyph
4881 in that glyph row area. X is the current output position assigned
4882 to the new glyph string constructed. HL overrides that face of the
4883 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4884 is the right-most x-position of the drawing area. */
4885
8abee2e1
DL
4886/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4887 and below -- keep them on one line. */
4888#define BUILD_STRETCH_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); \
209f68d9 4893 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4894 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4895 s->x = (X); \
4896 } \
4897 while (0)
4898
4899
4900/* Add a glyph string for an image glyph to the list of strings
4901 between HEAD and TAIL. START is the index of the image glyph in
4902 row area AREA of glyph row ROW. END is the index of the last glyph
4903 in that glyph row area. X is the current output position assigned
4904 to the new glyph string constructed. HL overrides that face of the
4905 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4906 is the right-most x-position of the drawing area. */
4907
8abee2e1 4908#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4909 do \
4910 { \
4911 s = (struct glyph_string *) alloca (sizeof *s); \
4912 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4913 x_fill_image_glyph_string (s); \
4914 x_append_glyph_string (&HEAD, &TAIL, s); \
4915 ++START; \
4916 s->x = (X); \
4917 } \
4918 while (0)
4919
4920
4921/* Add a glyph string for a sequence of character glyphs to the list
4922 of strings between HEAD and TAIL. START is the index of the first
4923 glyph in row area AREA of glyph row ROW that is part of the new
4924 glyph string. END is the index of the last glyph in that glyph row
4925 area. X is the current output position assigned to the new glyph
4926 string constructed. HL overrides that face of the glyph; e.g. it
4927 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4928 right-most x-position of the drawing area. */
4929
8abee2e1 4930#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4931 do \
4932 { \
3e71d8f2 4933 int c, face_id; \
06a2c219
GM
4934 XChar2b *char2b; \
4935 \
43d120d8 4936 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4937 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4938 \
b4192550
KH
4939 s = (struct glyph_string *) alloca (sizeof *s); \
4940 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4941 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4942 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4943 s->x = (X); \
4944 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4945 OVERLAPS_P); \
06a2c219
GM
4946 } \
4947 while (0)
4948
4949
b4192550
KH
4950/* Add a glyph string for a composite sequence to the list of strings
4951 between HEAD and TAIL. START is the index of the first glyph in
4952 row area AREA of glyph row ROW that is part of the new glyph
4953 string. END is the index of the last glyph in that glyph row area.
4954 X is the current output position assigned to the new glyph string
4955 constructed. HL overrides that face of the glyph; e.g. it is
4956 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4957 x-position of the drawing area. */
4958
6c27ec25 4959#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4960 do { \
43d120d8
KH
4961 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4962 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4963 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4964 struct composition *cmp = composition_table[cmp_id]; \
4965 int glyph_len = cmp->glyph_len; \
4966 XChar2b *char2b; \
4967 struct face **faces; \
4968 struct glyph_string *first_s = NULL; \
4969 int n; \
4970 \
ee569018 4971 base_face = base_face->ascii_face; \
b4192550
KH
4972 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4973 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4974 /* At first, fill in `char2b' and `faces'. */ \
4975 for (n = 0; n < glyph_len; n++) \
4976 { \
43d120d8 4977 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4978 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4979 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4980 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
abfb6b46 4981 this_face_id, char2b + n, 1, 1); \
b4192550
KH
4982 } \
4983 \
4984 /* Make glyph_strings for each glyph sequence that is drawable by \
4985 the same face, and append them to HEAD/TAIL. */ \
4986 for (n = 0; n < cmp->glyph_len;) \
4987 { \
4988 s = (struct glyph_string *) alloca (sizeof *s); \
4989 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4990 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4991 s->cmp = cmp; \
4992 s->gidx = n; \
b4192550
KH
4993 s->x = (X); \
4994 \
4995 if (n == 0) \
4996 first_s = s; \
4997 \
4998 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4999 } \
5000 \
5001 ++START; \
5002 s = first_s; \
5003 } while (0)
5004
5005
06a2c219
GM
5006/* Build a list of glyph strings between HEAD and TAIL for the glyphs
5007 of AREA of glyph row ROW on window W between indices START and END.
5008 HL overrides the face for drawing glyph strings, e.g. it is
5009 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5010 x-positions of the drawing area.
5011
5012 This is an ugly monster macro construct because we must use alloca
5013 to allocate glyph strings (because x_draw_glyphs can be called
5014 asynchronously). */
5015
8abee2e1 5016#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
5017 do \
5018 { \
5019 HEAD = TAIL = NULL; \
5020 while (START < END) \
5021 { \
5022 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5023 switch (first_glyph->type) \
5024 { \
5025 case CHAR_GLYPH: \
5026 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
5027 TAIL, HL, X, LAST_X, \
5028 OVERLAPS_P); \
06a2c219
GM
5029 break; \
5030 \
b4192550
KH
5031 case COMPOSITE_GLYPH: \
5032 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5033 HEAD, TAIL, HL, X, LAST_X,\
5034 OVERLAPS_P); \
5035 break; \
5036 \
06a2c219
GM
5037 case STRETCH_GLYPH: \
5038 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5039 HEAD, TAIL, HL, X, LAST_X); \
5040 break; \
5041 \
5042 case IMAGE_GLYPH: \
5043 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5044 TAIL, HL, X, LAST_X); \
5045 break; \
5046 \
5047 default: \
5048 abort (); \
5049 } \
5050 \
5051 x_set_glyph_string_background_width (s, START, LAST_X); \
5052 (X) += s->width; \
5053 } \
5054 } \
5055 while (0)
5056
5057
5058/* Draw glyphs between START and END in AREA of ROW on window W,
5059 starting at x-position X. X is relative to AREA in W. HL is a
5060 face-override with the following meaning:
5061
5062 DRAW_NORMAL_TEXT draw normally
5063 DRAW_CURSOR draw in cursor face
5064 DRAW_MOUSE_FACE draw in mouse face.
5065 DRAW_INVERSE_VIDEO draw in mode line face
5066 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5067 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5068
66ac4b0e
GM
5069 If OVERLAPS_P is non-zero, draw only the foreground of characters
5070 and clip to the physical height of ROW.
5071
06a2c219
GM
5072 Value is the x-position reached, relative to AREA of W. */
5073
5074static int
f0a48a01 5075x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
06a2c219
GM
5076 struct window *w;
5077 int x;
5078 struct glyph_row *row;
5079 enum glyph_row_area area;
5080 int start, end;
5081 enum draw_glyphs_face hl;
66ac4b0e 5082 int overlaps_p;
dc6f92b8 5083{
06a2c219
GM
5084 struct glyph_string *head, *tail;
5085 struct glyph_string *s;
5086 int last_x, area_width;
5087 int x_reached;
5088 int i, j;
5089
5090 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 5091 end = min (end, row->used[area]);
a8710abf
GM
5092 start = max (0, start);
5093 start = min (end, start);
06a2c219
GM
5094
5095 /* Translate X to frame coordinates. Set last_x to the right
5096 end of the drawing area. */
5097 if (row->full_width_p)
5098 {
5099 /* X is relative to the left edge of W, without scroll bars
3f332ef3 5100 or fringes. */
06a2c219 5101 struct frame *f = XFRAME (w->frame);
06a2c219 5102 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 5103
06a2c219
GM
5104 x += window_left_x;
5105 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5106 last_x = window_left_x + area_width;
5107
5108 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5109 {
110859fc 5110 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
5111 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5112 last_x += width;
5113 else
5114 x -= width;
5115 }
dc6f92b8 5116
b9432a85 5117 x += FRAME_INTERNAL_BORDER_WIDTH (f);
98fedd97 5118 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
5119 }
5120 else
dc6f92b8 5121 {
06a2c219
GM
5122 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5123 area_width = window_box_width (w, area);
5124 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
5125 }
5126
06a2c219
GM
5127 /* Build a doubly-linked list of glyph_string structures between
5128 head and tail from what we have to draw. Note that the macro
5129 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5130 the reason we use a separate variable `i'. */
5131 i = start;
66ac4b0e
GM
5132 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5133 overlaps_p);
06a2c219
GM
5134 if (tail)
5135 x_reached = tail->x + tail->background_width;
5136 else
5137 x_reached = x;
90e65f07 5138
06a2c219
GM
5139 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5140 the row, redraw some glyphs in front or following the glyph
5141 strings built above. */
a8710abf 5142 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
5143 {
5144 int dummy_x = 0;
5145 struct glyph_string *h, *t;
5146
5147 /* Compute overhangs for all glyph strings. */
5148 for (s = head; s; s = s->next)
5149 x_compute_glyph_string_overhangs (s);
5150
5151 /* Prepend glyph strings for glyphs in front of the first glyph
5152 string that are overwritten because of the first glyph
5153 string's left overhang. The background of all strings
5154 prepended must be drawn because the first glyph string
5155 draws over it. */
5156 i = x_left_overwritten (head);
5157 if (i >= 0)
5158 {
5159 j = i;
5160 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
5161 DRAW_NORMAL_TEXT, dummy_x, last_x,
5162 overlaps_p);
06a2c219 5163 start = i;
06a2c219
GM
5164 x_compute_overhangs_and_x (t, head->x, 1);
5165 x_prepend_glyph_string_lists (&head, &tail, h, t);
5166 }
58769bee 5167
06a2c219
GM
5168 /* Prepend glyph strings for glyphs in front of the first glyph
5169 string that overwrite that glyph string because of their
5170 right overhang. For these strings, only the foreground must
5171 be drawn, because it draws over the glyph string at `head'.
5172 The background must not be drawn because this would overwrite
5173 right overhangs of preceding glyphs for which no glyph
5174 strings exist. */
5175 i = x_left_overwriting (head);
5176 if (i >= 0)
5177 {
5178 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
5179 DRAW_NORMAL_TEXT, dummy_x, last_x,
5180 overlaps_p);
06a2c219
GM
5181 for (s = h; s; s = s->next)
5182 s->background_filled_p = 1;
06a2c219
GM
5183 x_compute_overhangs_and_x (t, head->x, 1);
5184 x_prepend_glyph_string_lists (&head, &tail, h, t);
5185 }
dbcb258a 5186
06a2c219
GM
5187 /* Append glyphs strings for glyphs following the last glyph
5188 string tail that are overwritten by tail. The background of
5189 these strings has to be drawn because tail's foreground draws
5190 over it. */
5191 i = x_right_overwritten (tail);
5192 if (i >= 0)
5193 {
5194 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5195 DRAW_NORMAL_TEXT, x, last_x,
5196 overlaps_p);
06a2c219
GM
5197 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5198 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219 5199 }
dc6f92b8 5200
06a2c219
GM
5201 /* Append glyph strings for glyphs following the last glyph
5202 string tail that overwrite tail. The foreground of such
5203 glyphs has to be drawn because it writes into the background
5204 of tail. The background must not be drawn because it could
5205 paint over the foreground of following glyphs. */
5206 i = x_right_overwriting (tail);
5207 if (i >= 0)
5208 {
5209 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5210 DRAW_NORMAL_TEXT, x, last_x,
5211 overlaps_p);
06a2c219
GM
5212 for (s = h; s; s = s->next)
5213 s->background_filled_p = 1;
5214 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5215 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219
GM
5216 }
5217 }
58769bee 5218
06a2c219
GM
5219 /* Draw all strings. */
5220 for (s = head; s; s = s->next)
5221 x_draw_glyph_string (s);
dc6f92b8 5222
bb313871
GM
5223 if (area == TEXT_AREA
5224 && !row->full_width_p
5225 /* When drawing overlapping rows, only the glyph strings'
5226 foreground is drawn, which doesn't erase a cursor
5227 completely. */
5228 && !overlaps_p)
f0a48a01
GM
5229 {
5230 int x0 = head ? head->x : x;
5231 int x1 = tail ? tail->x + tail->background_width : x;
5232
5233 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5234 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5235
bb313871 5236 if (XFASTINT (w->left_margin_width) != 0)
f0a48a01
GM
5237 {
5238 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5239 x0 -= left_area_width;
5240 x1 -= left_area_width;
5241 }
5242
60626bab
GM
5243 notice_overwritten_cursor (w, area, x0, x1,
5244 row->y, MATRIX_ROW_BOTTOM_Y (row));
f0a48a01
GM
5245 }
5246
06a2c219
GM
5247 /* Value is the x-position up to which drawn, relative to AREA of W.
5248 This doesn't include parts drawn because of overhangs. */
5249 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5250 if (!row->full_width_p)
5251 {
f0a48a01 5252 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
06a2c219
GM
5253 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5254 if (area > TEXT_AREA)
5255 x_reached -= window_box_width (w, TEXT_AREA);
5256 }
a8710abf 5257
06a2c219
GM
5258 return x_reached;
5259}
dc6f92b8 5260
dc6f92b8 5261
66ac4b0e
GM
5262/* Fix the display of area AREA of overlapping row ROW in window W. */
5263
5264static void
5265x_fix_overlapping_area (w, row, area)
5266 struct window *w;
5267 struct glyph_row *row;
5268 enum glyph_row_area area;
5269{
5270 int i, x;
5271
5272 BLOCK_INPUT;
5273
5274 if (area == LEFT_MARGIN_AREA)
5275 x = 0;
5276 else if (area == TEXT_AREA)
5277 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5278 else
5279 x = (window_box_width (w, LEFT_MARGIN_AREA)
5280 + window_box_width (w, TEXT_AREA));
5281
5282 for (i = 0; i < row->used[area];)
5283 {
5284 if (row->glyphs[area][i].overlaps_vertically_p)
5285 {
5286 int start = i, start_x = x;
5287
5288 do
5289 {
5290 x += row->glyphs[area][i].pixel_width;
5291 ++i;
5292 }
5293 while (i < row->used[area]
5294 && row->glyphs[area][i].overlaps_vertically_p);
5295
5296 x_draw_glyphs (w, start_x, row, area, start, i,
f0a48a01 5297 DRAW_NORMAL_TEXT, 1);
66ac4b0e
GM
5298 }
5299 else
5300 {
5301 x += row->glyphs[area][i].pixel_width;
5302 ++i;
5303 }
5304 }
5305
5306 UNBLOCK_INPUT;
5307}
5308
5309
06a2c219
GM
5310/* Output LEN glyphs starting at START at the nominal cursor position.
5311 Advance the nominal cursor over the text. The global variable
5312 updated_window contains the window being updated, updated_row is
5313 the glyph row being updated, and updated_area is the area of that
5314 row being updated. */
dc6f92b8 5315
06a2c219
GM
5316static void
5317x_write_glyphs (start, len)
5318 struct glyph *start;
5319 int len;
5320{
f0a48a01 5321 int x, hpos;
d9cdbb3d 5322
06a2c219 5323 xassert (updated_window && updated_row);
dc6f92b8 5324 BLOCK_INPUT;
06a2c219
GM
5325
5326 /* Write glyphs. */
dc6f92b8 5327
06a2c219
GM
5328 hpos = start - updated_row->glyphs[updated_area];
5329 x = x_draw_glyphs (updated_window, output_cursor.x,
5330 updated_row, updated_area,
5331 hpos, hpos + len,
f0a48a01 5332 DRAW_NORMAL_TEXT, 0);
dc6f92b8 5333
6065f9e2
KS
5334 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
5335 if (updated_area == TEXT_AREA
5336 && updated_window->phys_cursor_on_p
5337 && updated_window->phys_cursor.vpos == output_cursor.vpos
5338 && updated_window->phys_cursor.hpos >= hpos
5339 && updated_window->phys_cursor.hpos < hpos + len)
5340 updated_window->phys_cursor_on_p = 0;
5341
dc6f92b8 5342 UNBLOCK_INPUT;
06a2c219
GM
5343
5344 /* Advance the output cursor. */
5345 output_cursor.hpos += len;
5346 output_cursor.x = x;
dc6f92b8
JB
5347}
5348
0cdd0c9f 5349
06a2c219 5350/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5351
06a2c219
GM
5352static void
5353x_insert_glyphs (start, len)
5354 struct glyph *start;
5355 register int len;
5356{
5357 struct frame *f;
5358 struct window *w;
5359 int line_height, shift_by_width, shifted_region_width;
5360 struct glyph_row *row;
5361 struct glyph *glyph;
2beb36f9 5362 int frame_x, frame_y, hpos;
58769bee 5363
06a2c219 5364 xassert (updated_window && updated_row);
0cdd0c9f 5365 BLOCK_INPUT;
06a2c219
GM
5366 w = updated_window;
5367 f = XFRAME (WINDOW_FRAME (w));
5368
5369 /* Get the height of the line we are in. */
5370 row = updated_row;
5371 line_height = row->height;
5372
5373 /* Get the width of the glyphs to insert. */
5374 shift_by_width = 0;
5375 for (glyph = start; glyph < start + len; ++glyph)
5376 shift_by_width += glyph->pixel_width;
5377
5378 /* Get the width of the region to shift right. */
5379 shifted_region_width = (window_box_width (w, updated_area)
5380 - output_cursor.x
5381 - shift_by_width);
5382
5383 /* Shift right. */
bf0ab8a2 5384 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5385 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5386 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5387 f->output_data.x->normal_gc,
5388 frame_x, frame_y,
5389 shifted_region_width, line_height,
5390 frame_x + shift_by_width, frame_y);
5391
5392 /* Write the glyphs. */
5393 hpos = start - row->glyphs[updated_area];
5394 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
f0a48a01 5395 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
5396
5397 /* Advance the output cursor. */
5398 output_cursor.hpos += len;
5399 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5400 UNBLOCK_INPUT;
5401}
0cdd0c9f 5402
0cdd0c9f 5403
06a2c219
GM
5404/* Delete N glyphs at the nominal cursor position. Not implemented
5405 for X frames. */
c83febd7
RS
5406
5407static void
06a2c219
GM
5408x_delete_glyphs (n)
5409 register int n;
c83febd7 5410{
06a2c219 5411 abort ();
c83febd7
RS
5412}
5413
0cdd0c9f 5414
c5e6e06b
GM
5415/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5416 If they are <= 0, this is probably an error. */
5417
5418void
5419x_clear_area (dpy, window, x, y, width, height, exposures)
5420 Display *dpy;
5421 Window window;
5422 int x, y;
5423 int width, height;
5424 int exposures;
5425{
5426 xassert (width > 0 && height > 0);
5427 XClearArea (dpy, window, x, y, width, height, exposures);
5428}
5429
5430
06a2c219
GM
5431/* Erase the current text line from the nominal cursor position
5432 (inclusive) to pixel column TO_X (exclusive). The idea is that
5433 everything from TO_X onward is already erased.
5434
5435 TO_X is a pixel position relative to updated_area of
5436 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5437
06a2c219
GM
5438static void
5439x_clear_end_of_line (to_x)
5440 int to_x;
5441{
5442 struct frame *f;
5443 struct window *w = updated_window;
5444 int max_x, min_y, max_y;
5445 int from_x, from_y, to_y;
5446
5447 xassert (updated_window && updated_row);
5448 f = XFRAME (w->frame);
5449
5450 if (updated_row->full_width_p)
5451 {
5452 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5453 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5454 && !w->pseudo_window_p)
5455 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5456 }
06a2c219
GM
5457 else
5458 max_x = window_box_width (w, updated_area);
5459 max_y = window_text_bottom_y (w);
dc6f92b8 5460
06a2c219
GM
5461 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5462 of window. For TO_X > 0, truncate to end of drawing area. */
5463 if (to_x == 0)
5464 return;
5465 else if (to_x < 0)
5466 to_x = max_x;
5467 else
5468 to_x = min (to_x, max_x);
dbc4e1c1 5469
06a2c219
GM
5470 to_y = min (max_y, output_cursor.y + updated_row->height);
5471
5472 /* Notice if the cursor will be cleared by this operation. */
5473 if (!updated_row->full_width_p)
60626bab
GM
5474 notice_overwritten_cursor (w, updated_area,
5475 output_cursor.x, -1,
5476 updated_row->y,
5477 MATRIX_ROW_BOTTOM_Y (updated_row));
dbc4e1c1 5478
06a2c219
GM
5479 from_x = output_cursor.x;
5480
5481 /* Translate to frame coordinates. */
5482 if (updated_row->full_width_p)
5483 {
5484 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5485 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5486 }
0cdd0c9f
RS
5487 else
5488 {
06a2c219
GM
5489 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5490 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5491 }
5492
045dee35 5493 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5494 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5495 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5496
5497 /* Prevent inadvertently clearing to end of the X window. */
5498 if (to_x > from_x && to_y > from_y)
5499 {
5500 BLOCK_INPUT;
c5e6e06b
GM
5501 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5502 from_x, from_y, to_x - from_x, to_y - from_y,
5503 False);
06a2c219 5504 UNBLOCK_INPUT;
0cdd0c9f 5505 }
0cdd0c9f 5506}
dbc4e1c1 5507
0cdd0c9f 5508
06a2c219 5509/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5510 frame. Otherwise clear the selected frame. */
06a2c219
GM
5511
5512static void
5513x_clear_frame ()
0cdd0c9f 5514{
06a2c219 5515 struct frame *f;
0cdd0c9f 5516
06a2c219
GM
5517 if (updating_frame)
5518 f = updating_frame;
0cdd0c9f 5519 else
b86bd3dd 5520 f = SELECTED_FRAME ();
58769bee 5521
06a2c219
GM
5522 /* Clearing the frame will erase any cursor, so mark them all as no
5523 longer visible. */
5524 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5525 output_cursor.hpos = output_cursor.vpos = 0;
5526 output_cursor.x = -1;
5527
5528 /* We don't set the output cursor here because there will always
5529 follow an explicit cursor_to. */
5530 BLOCK_INPUT;
5531 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5532
5533 /* We have to clear the scroll bars, too. If we have changed
5534 colors or something like that, then they should be notified. */
5535 x_scroll_bar_clear (f);
0cdd0c9f 5536
06a2c219
GM
5537 XFlush (FRAME_X_DISPLAY (f));
5538 UNBLOCK_INPUT;
dc6f92b8 5539}
06a2c219
GM
5540
5541
dc6f92b8 5542\f
dbc4e1c1
JB
5543/* Invert the middle quarter of the frame for .15 sec. */
5544
06a2c219
GM
5545/* We use the select system call to do the waiting, so we have to make
5546 sure it's available. If it isn't, we just won't do visual bells. */
5547
dbc4e1c1
JB
5548#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5549
06a2c219
GM
5550
5551/* Subtract the `struct timeval' values X and Y, storing the result in
5552 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5553
5554static int
5555timeval_subtract (result, x, y)
5556 struct timeval *result, x, y;
5557{
06a2c219
GM
5558 /* Perform the carry for the later subtraction by updating y. This
5559 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5560 if (x.tv_usec < y.tv_usec)
5561 {
5562 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5563 y.tv_usec -= 1000000 * nsec;
5564 y.tv_sec += nsec;
5565 }
06a2c219 5566
dbc4e1c1
JB
5567 if (x.tv_usec - y.tv_usec > 1000000)
5568 {
5569 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5570 y.tv_usec += 1000000 * nsec;
5571 y.tv_sec -= nsec;
5572 }
5573
06a2c219
GM
5574 /* Compute the time remaining to wait. tv_usec is certainly
5575 positive. */
dbc4e1c1
JB
5576 result->tv_sec = x.tv_sec - y.tv_sec;
5577 result->tv_usec = x.tv_usec - y.tv_usec;
5578
06a2c219
GM
5579 /* Return indication of whether the result should be considered
5580 negative. */
dbc4e1c1
JB
5581 return x.tv_sec < y.tv_sec;
5582}
dc6f92b8 5583
dfcf069d 5584void
f676886a
JB
5585XTflash (f)
5586 struct frame *f;
dc6f92b8 5587{
dbc4e1c1 5588 BLOCK_INPUT;
dc6f92b8 5589
dbc4e1c1
JB
5590 {
5591 GC gc;
dc6f92b8 5592
06a2c219
GM
5593 /* Create a GC that will use the GXxor function to flip foreground
5594 pixels into background pixels. */
dbc4e1c1
JB
5595 {
5596 XGCValues values;
dc6f92b8 5597
dbc4e1c1 5598 values.function = GXxor;
7556890b
RS
5599 values.foreground = (f->output_data.x->foreground_pixel
5600 ^ f->output_data.x->background_pixel);
58769bee 5601
334208b7 5602 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5603 GCFunction | GCForeground, &values);
5604 }
dc6f92b8 5605
dbc4e1c1 5606 {
e84e14c3
RS
5607 /* Get the height not including a menu bar widget. */
5608 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5609 /* Height of each line to flash. */
5610 int flash_height = FRAME_LINE_HEIGHT (f);
5611 /* These will be the left and right margins of the rectangles. */
5612 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5613 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5614
5615 int width;
5616
5617 /* Don't flash the area between a scroll bar and the frame
5618 edge it is next to. */
5619 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5620 {
5621 case vertical_scroll_bar_left:
5622 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5623 break;
5624
5625 case vertical_scroll_bar_right:
5626 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5627 break;
06a2c219
GM
5628
5629 default:
5630 break;
e84e14c3
RS
5631 }
5632
5633 width = flash_right - flash_left;
5634
5635 /* If window is tall, flash top and bottom line. */
5636 if (height > 3 * FRAME_LINE_HEIGHT (f))
5637 {
5638 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5639 flash_left,
5640 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5641 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5642 width, flash_height);
5643 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5644 flash_left,
5645 (height - flash_height
5646 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5647 width, flash_height);
5648 }
5649 else
5650 /* If it is short, flash it all. */
5651 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5652 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5653 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5654
06a2c219 5655 x_flush (f);
dc6f92b8 5656
dbc4e1c1 5657 {
06a2c219 5658 struct timeval wakeup;
dc6f92b8 5659
66c30ea1 5660 EMACS_GET_TIME (wakeup);
dc6f92b8 5661
dbc4e1c1
JB
5662 /* Compute time to wait until, propagating carry from usecs. */
5663 wakeup.tv_usec += 150000;
5664 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5665 wakeup.tv_usec %= 1000000;
5666
101922c3
GM
5667 /* Keep waiting until past the time wakeup or any input gets
5668 available. */
5669 while (! detect_input_pending ())
dbc4e1c1 5670 {
101922c3 5671 struct timeval current;
dbc4e1c1
JB
5672 struct timeval timeout;
5673
101922c3 5674 EMACS_GET_TIME (current);
dbc4e1c1 5675
101922c3
GM
5676 /* Break if result would be negative. */
5677 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5678 break;
5679
101922c3
GM
5680 /* How long `select' should wait. */
5681 timeout.tv_sec = 0;
5682 timeout.tv_usec = 10000;
5683
dbc4e1c1 5684 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5685 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5686 }
5687 }
58769bee 5688
e84e14c3
RS
5689 /* If window is tall, flash top and bottom line. */
5690 if (height > 3 * FRAME_LINE_HEIGHT (f))
5691 {
5692 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5693 flash_left,
5694 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5695 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5696 width, flash_height);
5697 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5698 flash_left,
5699 (height - flash_height
5700 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5701 width, flash_height);
5702 }
5703 else
5704 /* If it is short, flash it all. */
5705 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5706 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5707 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5708
334208b7 5709 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5710 x_flush (f);
dc6f92b8 5711 }
dbc4e1c1
JB
5712 }
5713
5714 UNBLOCK_INPUT;
dc6f92b8
JB
5715}
5716
06a2c219 5717#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5718
5719
dc6f92b8
JB
5720/* Make audible bell. */
5721
dfcf069d 5722void
dc6f92b8
JB
5723XTring_bell ()
5724{
b86bd3dd
GM
5725 struct frame *f = SELECTED_FRAME ();
5726
5727 if (FRAME_X_DISPLAY (f))
5728 {
dbc4e1c1 5729#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5730 if (visible_bell)
5731 XTflash (f);
5732 else
dbc4e1c1 5733#endif
b86bd3dd
GM
5734 {
5735 BLOCK_INPUT;
5736 XBell (FRAME_X_DISPLAY (f), 0);
5737 XFlush (FRAME_X_DISPLAY (f));
5738 UNBLOCK_INPUT;
5739 }
dc6f92b8
JB
5740 }
5741}
06a2c219 5742
dc6f92b8 5743\f
06a2c219
GM
5744/* Specify how many text lines, from the top of the window,
5745 should be affected by insert-lines and delete-lines operations.
5746 This, and those operations, are used only within an update
5747 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5748
dfcf069d 5749static void
06a2c219
GM
5750XTset_terminal_window (n)
5751 register int n;
dc6f92b8 5752{
06a2c219 5753 /* This function intentionally left blank. */
dc6f92b8
JB
5754}
5755
06a2c219
GM
5756
5757\f
5758/***********************************************************************
5759 Line Dance
5760 ***********************************************************************/
5761
5762/* Perform an insert-lines or delete-lines operation, inserting N
5763 lines or deleting -N lines at vertical position VPOS. */
5764
dfcf069d 5765static void
06a2c219
GM
5766x_ins_del_lines (vpos, n)
5767 int vpos, n;
dc6f92b8
JB
5768{
5769 abort ();
5770}
06a2c219
GM
5771
5772
5773/* Scroll part of the display as described by RUN. */
dc6f92b8 5774
dfcf069d 5775static void
06a2c219
GM
5776x_scroll_run (w, run)
5777 struct window *w;
5778 struct run *run;
dc6f92b8 5779{
06a2c219
GM
5780 struct frame *f = XFRAME (w->frame);
5781 int x, y, width, height, from_y, to_y, bottom_y;
5782
5783 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
5784 without mode lines. Include in this box the left and right
5785 fringe of W. */
06a2c219 5786 window_box (w, -1, &x, &y, &width, &height);
3f332ef3
KS
5787 width += FRAME_X_FRINGE_WIDTH (f);
5788 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
06a2c219
GM
5789
5790 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5791 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5792 bottom_y = y + height;
dc6f92b8 5793
06a2c219
GM
5794 if (to_y < from_y)
5795 {
5796 /* Scrolling up. Make sure we don't copy part of the mode
5797 line at the bottom. */
5798 if (from_y + run->height > bottom_y)
5799 height = bottom_y - from_y;
5800 else
5801 height = run->height;
5802 }
dc6f92b8 5803 else
06a2c219
GM
5804 {
5805 /* Scolling down. Make sure we don't copy over the mode line.
5806 at the bottom. */
5807 if (to_y + run->height > bottom_y)
5808 height = bottom_y - to_y;
5809 else
5810 height = run->height;
5811 }
7a13e894 5812
06a2c219
GM
5813 BLOCK_INPUT;
5814
5815 /* Cursor off. Will be switched on again in x_update_window_end. */
5816 updated_window = w;
5817 x_clear_cursor (w);
5818
5819 XCopyArea (FRAME_X_DISPLAY (f),
5820 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5821 f->output_data.x->normal_gc,
5822 x, from_y,
5823 width, height,
5824 x, to_y);
5825
5826 UNBLOCK_INPUT;
5827}
dc6f92b8 5828
dc6f92b8 5829
06a2c219
GM
5830\f
5831/***********************************************************************
5832 Exposure Events
5833 ***********************************************************************/
5834
5835/* Redisplay an exposed area of frame F. X and Y are the upper-left
5836 corner of the exposed rectangle. W and H are width and height of
5837 the exposed area. All are pixel values. W or H zero means redraw
5838 the entire frame. */
dc6f92b8 5839
06a2c219
GM
5840static void
5841expose_frame (f, x, y, w, h)
5842 struct frame *f;
5843 int x, y, w, h;
dc6f92b8 5844{
06a2c219 5845 XRectangle r;
82f053ab 5846 int mouse_face_overwritten_p = 0;
dc6f92b8 5847
06a2c219 5848 TRACE ((stderr, "expose_frame "));
dc6f92b8 5849
06a2c219
GM
5850 /* No need to redraw if frame will be redrawn soon. */
5851 if (FRAME_GARBAGED_P (f))
dc6f92b8 5852 {
06a2c219
GM
5853 TRACE ((stderr, " garbaged\n"));
5854 return;
5855 }
5856
5857 /* If basic faces haven't been realized yet, there is no point in
5858 trying to redraw anything. This can happen when we get an expose
5859 event while Emacs is starting, e.g. by moving another window. */
5860 if (FRAME_FACE_CACHE (f) == NULL
5861 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5862 {
5863 TRACE ((stderr, " no faces\n"));
5864 return;
58769bee 5865 }
06a2c219
GM
5866
5867 if (w == 0 || h == 0)
58769bee 5868 {
06a2c219
GM
5869 r.x = r.y = 0;
5870 r.width = CANON_X_UNIT (f) * f->width;
5871 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5872 }
5873 else
5874 {
06a2c219
GM
5875 r.x = x;
5876 r.y = y;
5877 r.width = w;
5878 r.height = h;
5879 }
5880
5881 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
82f053ab 5882 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
06a2c219 5883
9ea173e8 5884 if (WINDOWP (f->tool_bar_window))
82f053ab
GM
5885 mouse_face_overwritten_p
5886 |= expose_window (XWINDOW (f->tool_bar_window), &r);
06a2c219
GM
5887
5888#ifndef USE_X_TOOLKIT
5889 if (WINDOWP (f->menu_bar_window))
82f053ab
GM
5890 mouse_face_overwritten_p
5891 |= expose_window (XWINDOW (f->menu_bar_window), &r);
06a2c219 5892#endif /* not USE_X_TOOLKIT */
82f053ab
GM
5893
5894 /* Some window managers support a focus-follows-mouse style with
5895 delayed raising of frames. Imagine a partially obscured frame,
5896 and moving the mouse into partially obscured mouse-face on that
5897 frame. The visible part of the mouse-face will be highlighted,
5898 then the WM raises the obscured frame. With at least one WM, KDE
5899 2.1, Emacs is not getting any event for the raising of the frame
5900 (even tried with SubstructureRedirectMask), only Expose events.
5901 These expose events will draw text normally, i.e. not
5902 highlighted. Which means we must redo the highlight here.
5903 Subsume it under ``we love X''. --gerd 2001-08-15 */
5904 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5905 {
5906 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5907 if (f == dpyinfo->mouse_face_mouse_frame)
5908 {
5909 int x = dpyinfo->mouse_face_mouse_x;
5910 int y = dpyinfo->mouse_face_mouse_y;
5911 clear_mouse_face (dpyinfo);
5912 note_mouse_highlight (f, x, y);
5913 }
5914 }
dc6f92b8
JB
5915}
5916
06a2c219
GM
5917
5918/* Redraw (parts) of all windows in the window tree rooted at W that
82f053ab
GM
5919 intersect R. R contains frame pixel coordinates. Value is
5920 non-zero if the exposure overwrites mouse-face. */
06a2c219 5921
82f053ab 5922static int
06a2c219
GM
5923expose_window_tree (w, r)
5924 struct window *w;
5925 XRectangle *r;
dc6f92b8 5926{
82f053ab
GM
5927 struct frame *f = XFRAME (w->frame);
5928 int mouse_face_overwritten_p = 0;
5929
5930 while (w && !FRAME_GARBAGED_P (f))
06a2c219
GM
5931 {
5932 if (!NILP (w->hchild))
82f053ab
GM
5933 mouse_face_overwritten_p
5934 |= expose_window_tree (XWINDOW (w->hchild), r);
06a2c219 5935 else if (!NILP (w->vchild))
82f053ab
GM
5936 mouse_face_overwritten_p
5937 |= expose_window_tree (XWINDOW (w->vchild), r);
5938 else
5939 mouse_face_overwritten_p |= expose_window (w, r);
5940
a02f1be0 5941 w = NILP (w->next) ? NULL : XWINDOW (w->next);
06a2c219 5942 }
82f053ab
GM
5943
5944 return mouse_face_overwritten_p;
06a2c219 5945}
58769bee 5946
dc6f92b8 5947
06a2c219
GM
5948/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5949 which intersects rectangle R. R is in window-relative coordinates. */
5950
5951static void
5952expose_area (w, row, r, area)
5953 struct window *w;
5954 struct glyph_row *row;
5955 XRectangle *r;
5956 enum glyph_row_area area;
5957{
06a2c219
GM
5958 struct glyph *first = row->glyphs[area];
5959 struct glyph *end = row->glyphs[area] + row->used[area];
5960 struct glyph *last;
4bc6dcc7 5961 int first_x, start_x, x;
06a2c219 5962
6fb13182
GM
5963 if (area == TEXT_AREA && row->fill_line_p)
5964 /* If row extends face to end of line write the whole line. */
153f5ed7 5965 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
f0a48a01 5966 DRAW_NORMAL_TEXT, 0);
6fb13182
GM
5967 else
5968 {
4bc6dcc7
GM
5969 /* Set START_X to the window-relative start position for drawing glyphs of
5970 AREA. The first glyph of the text area can be partially visible.
5971 The first glyphs of other areas cannot. */
5972 if (area == LEFT_MARGIN_AREA)
5973 start_x = 0;
5974 else if (area == TEXT_AREA)
5975 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5976 else
5977 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5978 + window_box_width (w, TEXT_AREA));
5979 x = start_x;
5980
6fb13182
GM
5981 /* Find the first glyph that must be redrawn. */
5982 while (first < end
5983 && x + first->pixel_width < r->x)
5984 {
5985 x += first->pixel_width;
5986 ++first;
5987 }
5988
5989 /* Find the last one. */
5990 last = first;
5991 first_x = x;
5992 while (last < end
5993 && x < r->x + r->width)
5994 {
5995 x += last->pixel_width;
5996 ++last;
5997 }
5998
5999 /* Repaint. */
6000 if (last > first)
4bc6dcc7 6001 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
6002 first - row->glyphs[area],
6003 last - row->glyphs[area],
f0a48a01 6004 DRAW_NORMAL_TEXT, 0);
6fb13182 6005 }
06a2c219
GM
6006}
6007
58769bee 6008
06a2c219 6009/* Redraw the parts of the glyph row ROW on window W intersecting
82f053ab
GM
6010 rectangle R. R is in window-relative coordinates. Value is
6011 non-zero if mouse-face was overwritten. */
dc6f92b8 6012
82f053ab 6013static int
06a2c219
GM
6014expose_line (w, row, r)
6015 struct window *w;
6016 struct glyph_row *row;
6017 XRectangle *r;
6018{
6019 xassert (row->enabled_p);
6020
6021 if (row->mode_line_p || w->pseudo_window_p)
6022 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
f0a48a01 6023 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
6024 else
6025 {
6026 if (row->used[LEFT_MARGIN_AREA])
6027 expose_area (w, row, r, LEFT_MARGIN_AREA);
6028 if (row->used[TEXT_AREA])
6029 expose_area (w, row, r, TEXT_AREA);
6030 if (row->used[RIGHT_MARGIN_AREA])
6031 expose_area (w, row, r, RIGHT_MARGIN_AREA);
3f332ef3 6032 x_draw_row_fringe_bitmaps (w, row);
06a2c219 6033 }
82f053ab
GM
6034
6035 return row->mouse_face_p;
06a2c219 6036}
dc6f92b8 6037
58769bee 6038
06a2c219
GM
6039/* Return non-zero if W's cursor intersects rectangle R. */
6040
6041static int
6042x_phys_cursor_in_rect_p (w, r)
6043 struct window *w;
6044 XRectangle *r;
6045{
6046 XRectangle cr, result;
6047 struct glyph *cursor_glyph;
6048
6049 cursor_glyph = get_phys_cursor_glyph (w);
6050 if (cursor_glyph)
6051 {
6052 cr.x = w->phys_cursor.x;
6053 cr.y = w->phys_cursor.y;
6054 cr.width = cursor_glyph->pixel_width;
6055 cr.height = w->phys_cursor_height;
6056 return x_intersect_rectangles (&cr, r, &result);
6057 }
6058 else
6059 return 0;
dc6f92b8 6060}
dc6f92b8 6061
06a2c219 6062
a02f1be0
GM
6063/* Redraw the part of window W intersection rectangle FR. Pixel
6064 coordinates in FR are frame-relative. Call this function with
82f053ab
GM
6065 input blocked. Value is non-zero if the exposure overwrites
6066 mouse-face. */
dc6f92b8 6067
a39202f6 6068static int
a02f1be0 6069expose_window (w, fr)
06a2c219 6070 struct window *w;
a02f1be0 6071 XRectangle *fr;
dc6f92b8 6072{
a02f1be0 6073 struct frame *f = XFRAME (w->frame);
a02f1be0 6074 XRectangle wr, r;
82f053ab 6075 int mouse_face_overwritten_p = 0;
dc6f92b8 6076
80c32bcc
GM
6077 /* If window is not yet fully initialized, do nothing. This can
6078 happen when toolkit scroll bars are used and a window is split.
6079 Reconfiguring the scroll bar will generate an expose for a newly
6080 created window. */
a39202f6 6081 if (w->current_matrix == NULL)
82f053ab 6082 return 0;
a39202f6
GM
6083
6084 /* When we're currently updating the window, display and current
6085 matrix usually don't agree. Arrange for a thorough display
6086 later. */
6087 if (w == updated_window)
6088 {
6089 SET_FRAME_GARBAGED (f);
6090 return 0;
6091 }
80c32bcc 6092
a39202f6 6093 /* Frame-relative pixel rectangle of W. */
a02f1be0
GM
6094 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6095 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6096 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6097 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6098
a39202f6
GM
6099 if (x_intersect_rectangles (fr, &wr, &r))
6100 {
6101 int yb = window_text_bottom_y (w);
6102 struct glyph_row *row;
6103 int cursor_cleared_p;
a02f1be0 6104
a39202f6
GM
6105 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6106 r.x, r.y, r.width, r.height));
dc6f92b8 6107
a39202f6
GM
6108 /* Convert to window coordinates. */
6109 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6110 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
dc6f92b8 6111
a39202f6
GM
6112 /* Turn off the cursor. */
6113 if (!w->pseudo_window_p
6114 && x_phys_cursor_in_rect_p (w, &r))
6115 {
6116 x_clear_cursor (w);
6117 cursor_cleared_p = 1;
6118 }
6119 else
6120 cursor_cleared_p = 0;
06a2c219 6121
a39202f6
GM
6122 /* Find the first row intersecting the rectangle R. */
6123 for (row = w->current_matrix->rows;
6124 row->enabled_p;
6125 ++row)
6126 {
6127 int y0 = row->y;
6128 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6129
6130 if ((y0 >= r.y && y0 < r.y + r.height)
6131 || (y1 > r.y && y1 < r.y + r.height)
6132 || (r.y >= y0 && r.y < y1)
6133 || (r.y + r.height > y0 && r.y + r.height < y1))
82f053ab
GM
6134 {
6135 if (expose_line (w, row, &r))
6136 mouse_face_overwritten_p = 1;
6137 }
6138
a39202f6
GM
6139 if (y1 >= yb)
6140 break;
6141 }
dc6f92b8 6142
a39202f6
GM
6143 /* Display the mode line if there is one. */
6144 if (WINDOW_WANTS_MODELINE_P (w)
6145 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6146 row->enabled_p)
6147 && row->y < r.y + r.height)
82f053ab
GM
6148 {
6149 if (expose_line (w, row, &r))
6150 mouse_face_overwritten_p = 1;
6151 }
a39202f6
GM
6152
6153 if (!w->pseudo_window_p)
6154 {
6155 /* Draw border between windows. */
6156 x_draw_vertical_border (w);
06a2c219 6157
a39202f6
GM
6158 /* Turn the cursor on again. */
6159 if (cursor_cleared_p)
6160 x_update_window_cursor (w, 1);
6161 }
06a2c219 6162 }
82f053ab
GM
6163
6164 return mouse_face_overwritten_p;
06a2c219 6165}
dc6f92b8 6166
dc6f92b8 6167
06a2c219
GM
6168/* Determine the intersection of two rectangles R1 and R2. Return
6169 the intersection in *RESULT. Value is non-zero if RESULT is not
6170 empty. */
6171
6172static int
6173x_intersect_rectangles (r1, r2, result)
6174 XRectangle *r1, *r2, *result;
6175{
6176 XRectangle *left, *right;
6177 XRectangle *upper, *lower;
6178 int intersection_p = 0;
6179
6180 /* Rearrange so that R1 is the left-most rectangle. */
6181 if (r1->x < r2->x)
6182 left = r1, right = r2;
6183 else
6184 left = r2, right = r1;
6185
6186 /* X0 of the intersection is right.x0, if this is inside R1,
6187 otherwise there is no intersection. */
6188 if (right->x <= left->x + left->width)
6189 {
6190 result->x = right->x;
6191
6192 /* The right end of the intersection is the minimum of the
6193 the right ends of left and right. */
6194 result->width = (min (left->x + left->width, right->x + right->width)
6195 - result->x);
6196
6197 /* Same game for Y. */
6198 if (r1->y < r2->y)
6199 upper = r1, lower = r2;
6200 else
6201 upper = r2, lower = r1;
6202
6203 /* The upper end of the intersection is lower.y0, if this is inside
6204 of upper. Otherwise, there is no intersection. */
6205 if (lower->y <= upper->y + upper->height)
dc43ef94 6206 {
06a2c219
GM
6207 result->y = lower->y;
6208
6209 /* The lower end of the intersection is the minimum of the lower
6210 ends of upper and lower. */
6211 result->height = (min (lower->y + lower->height,
6212 upper->y + upper->height)
6213 - result->y);
6214 intersection_p = 1;
dc43ef94 6215 }
dc6f92b8
JB
6216 }
6217
06a2c219 6218 return intersection_p;
dc6f92b8 6219}
06a2c219
GM
6220
6221
6222
6223
dc6f92b8 6224\f
dc6f92b8 6225static void
334208b7
RS
6226frame_highlight (f)
6227 struct frame *f;
dc6f92b8 6228{
b3e1e05c
JB
6229 /* We used to only do this if Vx_no_window_manager was non-nil, but
6230 the ICCCM (section 4.1.6) says that the window's border pixmap
6231 and border pixel are window attributes which are "private to the
6232 client", so we can always change it to whatever we want. */
6233 BLOCK_INPUT;
334208b7 6234 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6235 f->output_data.x->border_pixel);
b3e1e05c 6236 UNBLOCK_INPUT;
5d46f928 6237 x_update_cursor (f, 1);
dc6f92b8
JB
6238}
6239
6240static void
334208b7
RS
6241frame_unhighlight (f)
6242 struct frame *f;
dc6f92b8 6243{
b3e1e05c
JB
6244 /* We used to only do this if Vx_no_window_manager was non-nil, but
6245 the ICCCM (section 4.1.6) says that the window's border pixmap
6246 and border pixel are window attributes which are "private to the
6247 client", so we can always change it to whatever we want. */
6248 BLOCK_INPUT;
334208b7 6249 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6250 f->output_data.x->border_tile);
b3e1e05c 6251 UNBLOCK_INPUT;
5d46f928 6252 x_update_cursor (f, 1);
dc6f92b8 6253}
dc6f92b8 6254
f676886a
JB
6255/* The focus has changed. Update the frames as necessary to reflect
6256 the new situation. Note that we can't change the selected frame
c5acd733 6257 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6258 Each event gets marked with the frame in which it occurred, so the
c5acd733 6259 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6260
6d4238f3 6261static void
0f941935
KH
6262x_new_focus_frame (dpyinfo, frame)
6263 struct x_display_info *dpyinfo;
f676886a 6264 struct frame *frame;
dc6f92b8 6265{
0f941935 6266 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6267
0f941935 6268 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6269 {
58769bee 6270 /* Set this before calling other routines, so that they see
f676886a 6271 the correct value of x_focus_frame. */
0f941935 6272 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6273
6274 if (old_focus && old_focus->auto_lower)
f676886a 6275 x_lower_frame (old_focus);
dc6f92b8
JB
6276
6277#if 0
f676886a 6278 selected_frame = frame;
e0c1aef2
KH
6279 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6280 selected_frame);
f676886a
JB
6281 Fselect_window (selected_frame->selected_window);
6282 choose_minibuf_frame ();
c118dd06 6283#endif /* ! 0 */
dc6f92b8 6284
0f941935
KH
6285 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6286 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6287 else
6288 pending_autoraise_frame = 0;
6d4238f3 6289 }
dc6f92b8 6290
0f941935 6291 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6292}
6293
37c2c98b
RS
6294/* Handle an event saying the mouse has moved out of an Emacs frame. */
6295
6296void
0f941935
KH
6297x_mouse_leave (dpyinfo)
6298 struct x_display_info *dpyinfo;
37c2c98b 6299{
0f941935 6300 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6301}
6d4238f3 6302
f451eb13
JB
6303/* The focus has changed, or we have redirected a frame's focus to
6304 another frame (this happens when a frame uses a surrogate
06a2c219 6305 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6306
6307 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6308 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6309 the appropriate X display info. */
06a2c219 6310
6d4238f3 6311static void
0f941935
KH
6312XTframe_rehighlight (frame)
6313 struct frame *frame;
6d4238f3 6314{
0f941935
KH
6315 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6316}
6d4238f3 6317
0f941935
KH
6318static void
6319x_frame_rehighlight (dpyinfo)
6320 struct x_display_info *dpyinfo;
6321{
6322 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6323
6324 if (dpyinfo->x_focus_frame)
6d4238f3 6325 {
0f941935
KH
6326 dpyinfo->x_highlight_frame
6327 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6328 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6329 : dpyinfo->x_focus_frame);
6330 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6331 {
0f941935
KH
6332 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6333 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6334 }
dc6f92b8 6335 }
6d4238f3 6336 else
0f941935 6337 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6338
0f941935 6339 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6340 {
6341 if (old_highlight)
f676886a 6342 frame_unhighlight (old_highlight);
0f941935
KH
6343 if (dpyinfo->x_highlight_frame)
6344 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6345 }
dc6f92b8 6346}
06a2c219
GM
6347
6348
dc6f92b8 6349\f
06a2c219 6350/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6351
28430d3c
JB
6352/* Initialize mode_switch_bit and modifier_meaning. */
6353static void
334208b7
RS
6354x_find_modifier_meanings (dpyinfo)
6355 struct x_display_info *dpyinfo;
28430d3c 6356{
f689eb05 6357 int min_code, max_code;
28430d3c
JB
6358 KeySym *syms;
6359 int syms_per_code;
6360 XModifierKeymap *mods;
6361
334208b7
RS
6362 dpyinfo->meta_mod_mask = 0;
6363 dpyinfo->shift_lock_mask = 0;
6364 dpyinfo->alt_mod_mask = 0;
6365 dpyinfo->super_mod_mask = 0;
6366 dpyinfo->hyper_mod_mask = 0;
58769bee 6367
9658a521 6368#ifdef HAVE_X11R4
334208b7 6369 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6370#else
4a60f8c5
RS
6371 min_code = dpyinfo->display->min_keycode;
6372 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6373#endif
6374
334208b7 6375 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6376 min_code, max_code - min_code + 1,
6377 &syms_per_code);
334208b7 6378 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6379
58769bee 6380 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6381 Alt keysyms are on. */
28430d3c 6382 {
06a2c219 6383 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6384
6385 for (row = 3; row < 8; row++)
6386 for (col = 0; col < mods->max_keypermod; col++)
6387 {
0299d313
RS
6388 KeyCode code
6389 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6390
af92970c
KH
6391 /* Zeroes are used for filler. Skip them. */
6392 if (code == 0)
6393 continue;
6394
28430d3c
JB
6395 /* Are any of this keycode's keysyms a meta key? */
6396 {
6397 int code_col;
6398
6399 for (code_col = 0; code_col < syms_per_code; code_col++)
6400 {
f689eb05 6401 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6402
f689eb05 6403 switch (sym)
28430d3c 6404 {
f689eb05
JB
6405 case XK_Meta_L:
6406 case XK_Meta_R:
334208b7 6407 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6408 break;
f689eb05
JB
6409
6410 case XK_Alt_L:
6411 case XK_Alt_R:
334208b7 6412 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6413 break;
6414
6415 case XK_Hyper_L:
6416 case XK_Hyper_R:
334208b7 6417 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6418 break;
6419
6420 case XK_Super_L:
6421 case XK_Super_R:
334208b7 6422 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6423 break;
11edeb03
JB
6424
6425 case XK_Shift_Lock:
6426 /* Ignore this if it's not on the lock modifier. */
6427 if ((1 << row) == LockMask)
334208b7 6428 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6429 break;
28430d3c
JB
6430 }
6431 }
6432 }
6433 }
6434 }
6435
f689eb05 6436 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6437 if (! dpyinfo->meta_mod_mask)
a3c44b14 6438 {
334208b7
RS
6439 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6440 dpyinfo->alt_mod_mask = 0;
a3c44b14 6441 }
f689eb05 6442
148c4b70
RS
6443 /* If some keys are both alt and meta,
6444 make them just meta, not alt. */
334208b7 6445 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6446 {
334208b7 6447 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6448 }
58769bee 6449
28430d3c 6450 XFree ((char *) syms);
f689eb05 6451 XFreeModifiermap (mods);
28430d3c
JB
6452}
6453
dfeccd2d
JB
6454/* Convert between the modifier bits X uses and the modifier bits
6455 Emacs uses. */
06a2c219 6456
7c5283e4 6457static unsigned int
334208b7
RS
6458x_x_to_emacs_modifiers (dpyinfo, state)
6459 struct x_display_info *dpyinfo;
dc6f92b8
JB
6460 unsigned int state;
6461{
98659da6
KG
6462 EMACS_UINT mod_meta = meta_modifier;
6463 EMACS_UINT mod_alt = alt_modifier;
6464 EMACS_UINT mod_hyper = hyper_modifier;
6465 EMACS_UINT mod_super = super_modifier;
6466 Lisp_Object tem;
6467
6468 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6469 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6470 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6471 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6472 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6473 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6474 tem = Fget (Vx_super_keysym, Qmodifier_value);
6475 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6476
6477
334208b7 6478 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
6479 | ((state & ControlMask) ? ctrl_modifier : 0)
6480 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6481 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6482 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6483 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
6484}
6485
dfeccd2d 6486static unsigned int
334208b7
RS
6487x_emacs_to_x_modifiers (dpyinfo, state)
6488 struct x_display_info *dpyinfo;
dfeccd2d
JB
6489 unsigned int state;
6490{
98659da6
KG
6491 EMACS_UINT mod_meta = meta_modifier;
6492 EMACS_UINT mod_alt = alt_modifier;
6493 EMACS_UINT mod_hyper = hyper_modifier;
6494 EMACS_UINT mod_super = super_modifier;
6495
6496 Lisp_Object tem;
6497
6498 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6499 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6500 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6501 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6502 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6503 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6504 tem = Fget (Vx_super_keysym, Qmodifier_value);
6505 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6506
6507
6508 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6509 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6510 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6511 | ((state & shift_modifier) ? ShiftMask : 0)
6512 | ((state & ctrl_modifier) ? ControlMask : 0)
6513 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6514}
d047c4eb
KH
6515
6516/* Convert a keysym to its name. */
6517
6518char *
6519x_get_keysym_name (keysym)
6520 KeySym keysym;
6521{
6522 char *value;
6523
6524 BLOCK_INPUT;
6525 value = XKeysymToString (keysym);
6526 UNBLOCK_INPUT;
6527
6528 return value;
6529}
06a2c219
GM
6530
6531
e4571a43
JB
6532\f
6533/* Mouse clicks and mouse movement. Rah. */
e4571a43 6534
06a2c219
GM
6535/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6536 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6537 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6538 not force the value into range. */
69388238 6539
c8dba240 6540void
69388238 6541pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6542 FRAME_PTR f;
69388238 6543 register int pix_x, pix_y;
e4571a43
JB
6544 register int *x, *y;
6545 XRectangle *bounds;
69388238 6546 int noclip;
e4571a43 6547{
06a2c219 6548 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6549 even for negative values. */
6550 if (pix_x < 0)
7556890b 6551 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6552 if (pix_y < 0)
7556890b 6553 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6554
e4571a43
JB
6555 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6556 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6557
6558 if (bounds)
6559 {
7556890b
RS
6560 bounds->width = FONT_WIDTH (f->output_data.x->font);
6561 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6562 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6563 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6564 }
6565
69388238
RS
6566 if (!noclip)
6567 {
6568 if (pix_x < 0)
6569 pix_x = 0;
3cbd2e0b
RS
6570 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6571 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6572
6573 if (pix_y < 0)
6574 pix_y = 0;
6575 else if (pix_y > f->height)
6576 pix_y = f->height;
6577 }
e4571a43
JB
6578
6579 *x = pix_x;
6580 *y = pix_y;
6581}
6582
06a2c219
GM
6583
6584/* Given HPOS/VPOS in the current matrix of W, return corresponding
6585 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6586 can't tell the positions because W's display is not up to date,
6587 return 0. */
6588
6589int
6590glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6591 struct window *w;
6592 int hpos, vpos;
6593 int *frame_x, *frame_y;
2b5c9e71 6594{
06a2c219
GM
6595 int success_p;
6596
6597 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6598 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6599
6600 if (display_completed)
6601 {
6602 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6603 struct glyph *glyph = row->glyphs[TEXT_AREA];
6604 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6605
6606 *frame_y = row->y;
6607 *frame_x = row->x;
6608 while (glyph < end)
6609 {
6610 *frame_x += glyph->pixel_width;
6611 ++glyph;
6612 }
6613
6614 success_p = 1;
6615 }
6616 else
6617 {
6618 *frame_y = *frame_x = 0;
6619 success_p = 0;
6620 }
6621
6622 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6623 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6624 return success_p;
2b5c9e71
RS
6625}
6626
06a2c219 6627
dc6f92b8
JB
6628/* Prepare a mouse-event in *RESULT for placement in the input queue.
6629
6630 If the event is a button press, then note that we have grabbed
f451eb13 6631 the mouse. */
dc6f92b8
JB
6632
6633static Lisp_Object
f451eb13 6634construct_mouse_click (result, event, f)
dc6f92b8
JB
6635 struct input_event *result;
6636 XButtonEvent *event;
f676886a 6637 struct frame *f;
dc6f92b8 6638{
3b8f9651 6639 /* Make the event type NO_EVENT; we'll change that when we decide
dc6f92b8 6640 otherwise. */
3b8f9651 6641 result->kind = MOUSE_CLICK_EVENT;
69388238 6642 result->code = event->button - Button1;
1113d9db 6643 result->timestamp = event->time;
334208b7
RS
6644 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6645 event->state)
f689eb05 6646 | (event->type == ButtonRelease
58769bee 6647 ? up_modifier
f689eb05 6648 : down_modifier));
dc6f92b8 6649
06a2c219
GM
6650 XSETINT (result->x, event->x);
6651 XSETINT (result->y, event->y);
6652 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6653 result->arg = Qnil;
06a2c219 6654 return Qnil;
dc6f92b8 6655}
b849c413 6656
69388238 6657\f
90e65f07
JB
6658/* Function to report a mouse movement to the mainstream Emacs code.
6659 The input handler calls this.
6660
6661 We have received a mouse movement event, which is given in *event.
6662 If the mouse is over a different glyph than it was last time, tell
6663 the mainstream emacs code by setting mouse_moved. If not, ask for
6664 another motion event, so we can check again the next time it moves. */
b8009dd1 6665
06a2c219
GM
6666static XMotionEvent last_mouse_motion_event;
6667static Lisp_Object last_mouse_motion_frame;
6668
90e65f07 6669static void
12ba150f 6670note_mouse_movement (frame, event)
f676886a 6671 FRAME_PTR frame;
90e65f07 6672 XMotionEvent *event;
90e65f07 6673{
e5d77022 6674 last_mouse_movement_time = event->time;
06a2c219
GM
6675 last_mouse_motion_event = *event;
6676 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6677
27f338af
RS
6678 if (event->window != FRAME_X_WINDOW (frame))
6679 {
39d8bb4d 6680 frame->mouse_moved = 1;
27f338af 6681 last_mouse_scroll_bar = Qnil;
27f338af 6682 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6683 }
6684
90e65f07 6685 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6686 else if (event->x < last_mouse_glyph.x
6687 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6688 || event->y < last_mouse_glyph.y
6689 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6690 {
39d8bb4d 6691 frame->mouse_moved = 1;
ab648270 6692 last_mouse_scroll_bar = Qnil;
b8009dd1 6693 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6694 }
6695}
6696
06a2c219
GM
6697\f
6698/************************************************************************
6699 Mouse Face
6700 ************************************************************************/
6701
6702/* Find the glyph under window-relative coordinates X/Y in window W.
6703 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6704 strings. Return in *HPOS and *VPOS the row and column number of
6705 the glyph found. Return in *AREA the glyph area containing X.
6706 Value is a pointer to the glyph found or null if X/Y is not on
6707 text, or we can't tell because W's current matrix is not up to
6708 date. */
6709
6710static struct glyph *
f9db2310 6711x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
6712 struct window *w;
6713 int x, y;
6714 int *hpos, *vpos, *area;
f9db2310 6715 int buffer_only_p;
06a2c219
GM
6716{
6717 struct glyph *glyph, *end;
3e71d8f2 6718 struct glyph_row *row = NULL;
06a2c219
GM
6719 int x0, i, left_area_width;
6720
6721 /* Find row containing Y. Give up if some row is not enabled. */
6722 for (i = 0; i < w->current_matrix->nrows; ++i)
6723 {
6724 row = MATRIX_ROW (w->current_matrix, i);
6725 if (!row->enabled_p)
6726 return NULL;
6727 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6728 break;
6729 }
6730
6731 *vpos = i;
6732 *hpos = 0;
6733
6734 /* Give up if Y is not in the window. */
6735 if (i == w->current_matrix->nrows)
6736 return NULL;
6737
6738 /* Get the glyph area containing X. */
6739 if (w->pseudo_window_p)
6740 {
6741 *area = TEXT_AREA;
6742 x0 = 0;
6743 }
6744 else
6745 {
6746 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6747 if (x < left_area_width)
6748 {
6749 *area = LEFT_MARGIN_AREA;
6750 x0 = 0;
6751 }
6752 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6753 {
6754 *area = TEXT_AREA;
6755 x0 = row->x + left_area_width;
6756 }
6757 else
6758 {
6759 *area = RIGHT_MARGIN_AREA;
6760 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6761 }
6762 }
6763
6764 /* Find glyph containing X. */
6765 glyph = row->glyphs[*area];
6766 end = glyph + row->used[*area];
6767 while (glyph < end)
6768 {
6769 if (x < x0 + glyph->pixel_width)
6770 {
6771 if (w->pseudo_window_p)
6772 break;
f9db2310 6773 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
6774 break;
6775 }
6776
6777 x0 += glyph->pixel_width;
6778 ++glyph;
6779 }
6780
6781 if (glyph == end)
6782 return NULL;
6783
6784 *hpos = glyph - row->glyphs[*area];
6785 return glyph;
6786}
6787
6788
6789/* Convert frame-relative x/y to coordinates relative to window W.
6790 Takes pseudo-windows into account. */
6791
6792static void
6793frame_to_window_pixel_xy (w, x, y)
6794 struct window *w;
6795 int *x, *y;
6796{
6797 if (w->pseudo_window_p)
6798 {
6799 /* A pseudo-window is always full-width, and starts at the
6800 left edge of the frame, plus a frame border. */
6801 struct frame *f = XFRAME (w->frame);
6802 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6803 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6804 }
6805 else
6806 {
6807 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6808 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6809 }
6810}
6811
6812
b436feb6
GM
6813/* Take proper action when mouse has moved to the mode or header line
6814 or marginal area of window W, x-position X and y-position Y. Area
6815 is 1, 3, 6 or 7 for the mode line, header line, left and right
6816 marginal area respectively. X is relative to the start of the text
6817 display area of W, so the width of bitmap areas and scroll bars
6818 must be subtracted to get a position relative to the start of the
6819 mode line. */
6820
06a2c219 6821static void
b436feb6 6822note_mode_line_or_margin_highlight (w, x, y, portion)
06a2c219 6823 struct window *w;
b436feb6 6824 int x, y, portion;
06a2c219
GM
6825{
6826 struct frame *f = XFRAME (w->frame);
6827 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6828 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
2a793c0c 6829 int charpos;
b436feb6 6830 Lisp_Object string, help, map, pos;
06a2c219 6831
b436feb6
GM
6832 if (portion == 1 || portion == 3)
6833 string = mode_line_string (w, x, y, portion == 1, &charpos);
6834 else
6835 string = marginal_area_string (w, x, y, portion, &charpos);
e371a781 6836
b436feb6 6837 if (STRINGP (string))
06a2c219 6838 {
b436feb6 6839 pos = make_number (charpos);
e371a781 6840
b436feb6
GM
6841 /* If we're on a string with `help-echo' text property, arrange
6842 for the help to be displayed. This is done by setting the
6843 global variable help_echo to the help string. */
6844 help = Fget_text_property (pos, Qhelp_echo, string);
6845 if (!NILP (help))
06a2c219 6846 {
b436feb6
GM
6847 help_echo = help;
6848 XSETWINDOW (help_echo_window, w);
6849 help_echo_object = string;
6850 help_echo_pos = charpos;
06a2c219
GM
6851 }
6852
b436feb6
GM
6853 /* Change the mouse pointer according to what is under X/Y. */
6854 map = Fget_text_property (pos, Qlocal_map, string);
6855 if (!KEYMAPP (map))
6856 map = Fget_text_property (pos, Qkeymap, string);
6857 if (KEYMAPP (map))
6858 cursor = f->output_data.x->nontext_cursor;
06a2c219 6859 }
b436feb6 6860
06a2c219
GM
6861 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6862}
6863
6864
6865/* Take proper action when the mouse has moved to position X, Y on
6866 frame F as regards highlighting characters that have mouse-face
6867 properties. Also de-highlighting chars where the mouse was before.
27f338af 6868 X and Y can be negative or out of range. */
b8009dd1
RS
6869
6870static void
6871note_mouse_highlight (f, x, y)
06a2c219 6872 struct frame *f;
c32cdd9a 6873 int x, y;
b8009dd1 6874{
06a2c219
GM
6875 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6876 int portion;
b8009dd1
RS
6877 Lisp_Object window;
6878 struct window *w;
0d487c52
GM
6879 Cursor cursor = None;
6880 struct buffer *b;
b8009dd1 6881
06a2c219
GM
6882 /* When a menu is active, don't highlight because this looks odd. */
6883#ifdef USE_X_TOOLKIT
6884 if (popup_activated ())
6885 return;
6886#endif
6887
66301061 6888 if (NILP (Vmouse_highlight)
04fff9c0 6889 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6890 return;
6891
06a2c219
GM
6892 dpyinfo->mouse_face_mouse_x = x;
6893 dpyinfo->mouse_face_mouse_y = y;
6894 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6895
06a2c219 6896 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6897 return;
6898
514e4681
RS
6899 if (gc_in_progress)
6900 {
06a2c219 6901 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6902 return;
6903 }
6904
b8009dd1 6905 /* Which window is that in? */
06a2c219 6906 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6907
6908 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6909 if (! EQ (window, dpyinfo->mouse_face_window))
6910 clear_mouse_face (dpyinfo);
6911
6912 /* Not on a window -> return. */
6913 if (!WINDOWP (window))
6914 return;
6915
6916 /* Convert to window-relative pixel coordinates. */
6917 w = XWINDOW (window);
6918 frame_to_window_pixel_xy (w, &x, &y);
6919
9ea173e8 6920 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6921 buffer. */
9ea173e8 6922 if (EQ (window, f->tool_bar_window))
06a2c219 6923 {
9ea173e8 6924 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6925 return;
6926 }
6927
b436feb6
GM
6928 /* Mouse is on the mode, header line or margin? */
6929 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
06a2c219 6930 {
b436feb6 6931 note_mode_line_or_margin_highlight (w, x, y, portion);
06a2c219
GM
6932 return;
6933 }
0d487c52
GM
6934
6935 if (portion == 2)
6936 cursor = f->output_data.x->horizontal_drag_cursor;
06a2c219 6937 else
0d487c52 6938 cursor = f->output_data.x->text_cursor;
b8009dd1 6939
0cdd0c9f
RS
6940 /* Are we in a window whose display is up to date?
6941 And verify the buffer's text has not changed. */
0d487c52 6942 b = XBUFFER (w->buffer);
06a2c219
GM
6943 if (/* Within text portion of the window. */
6944 portion == 0
0cdd0c9f 6945 && EQ (w->window_end_valid, w->buffer)
0d487c52
GM
6946 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6947 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
b8009dd1 6948 {
06a2c219
GM
6949 int hpos, vpos, pos, i, area;
6950 struct glyph *glyph;
f9db2310 6951 Lisp_Object object;
0d487c52
GM
6952 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6953 Lisp_Object *overlay_vec = NULL;
6954 int len, noverlays;
6955 struct buffer *obuf;
6956 int obegv, ozv, same_region;
b8009dd1 6957
06a2c219 6958 /* Find the glyph under X/Y. */
f9db2310 6959 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
6960
6961 /* Clear mouse face if X/Y not over text. */
6962 if (glyph == NULL
6963 || area != TEXT_AREA
6964 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6965 {
fa262c07
GM
6966 if (clear_mouse_face (dpyinfo))
6967 cursor = None;
6968 goto set_cursor;
06a2c219
GM
6969 }
6970
6971 pos = glyph->charpos;
f9db2310
GM
6972 object = glyph->object;
6973 if (!STRINGP (object) && !BUFFERP (object))
fa262c07 6974 goto set_cursor;
06a2c219 6975
0d487c52
GM
6976 /* If we get an out-of-range value, return now; avoid an error. */
6977 if (BUFFERP (object) && pos > BUF_Z (b))
fa262c07 6978 goto set_cursor;
06a2c219 6979
0d487c52
GM
6980 /* Make the window's buffer temporarily current for
6981 overlays_at and compute_char_face. */
6982 obuf = current_buffer;
6983 current_buffer = b;
6984 obegv = BEGV;
6985 ozv = ZV;
6986 BEGV = BEG;
6987 ZV = Z;
06a2c219 6988
0d487c52
GM
6989 /* Is this char mouse-active or does it have help-echo? */
6990 position = make_number (pos);
f9db2310 6991
0d487c52
GM
6992 if (BUFFERP (object))
6993 {
6994 /* Put all the overlays we want in a vector in overlay_vec.
6995 Store the length in len. If there are more than 10, make
6996 enough space for all, and try again. */
6997 len = 10;
6998 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6999 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7000 if (noverlays > len)
7001 {
7002 len = noverlays;
7003 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7004 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7005 }
f8349001 7006
0d487c52
GM
7007 /* Sort overlays into increasing priority order. */
7008 noverlays = sort_overlays (overlay_vec, noverlays, w);
7009 }
7010 else
7011 noverlays = 0;
7012
7013 same_region = (EQ (window, dpyinfo->mouse_face_window)
7014 && vpos >= dpyinfo->mouse_face_beg_row
7015 && vpos <= dpyinfo->mouse_face_end_row
7016 && (vpos > dpyinfo->mouse_face_beg_row
7017 || hpos >= dpyinfo->mouse_face_beg_col)
7018 && (vpos < dpyinfo->mouse_face_end_row
7019 || hpos < dpyinfo->mouse_face_end_col
7020 || dpyinfo->mouse_face_past_end));
7021
7022 if (same_region)
7023 cursor = None;
7024
7025 /* Check mouse-face highlighting. */
7026 if (! same_region
7027 /* If there exists an overlay with mouse-face overlapping
7028 the one we are currently highlighting, we have to
7029 check if we enter the overlapping overlay, and then
7030 highlight only that. */
7031 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7032 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7033 {
0d487c52
GM
7034 /* Find the highest priority overlay that has a mouse-face
7035 property. */
7036 overlay = Qnil;
7037 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7038 {
7039 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7040 if (!NILP (mouse_face))
7041 overlay = overlay_vec[i];
7042 }
8bd189fb
GM
7043
7044 /* If we're actually highlighting the same overlay as
7045 before, there's no need to do that again. */
7046 if (!NILP (overlay)
7047 && EQ (overlay, dpyinfo->mouse_face_overlay))
7048 goto check_help_echo;
f9db2310 7049
8bd189fb
GM
7050 dpyinfo->mouse_face_overlay = overlay;
7051
7052 /* Clear the display of the old active region, if any. */
7053 if (clear_mouse_face (dpyinfo))
7054 cursor = None;
7055
0d487c52
GM
7056 /* If no overlay applies, get a text property. */
7057 if (NILP (overlay))
7058 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219 7059
0d487c52
GM
7060 /* Handle the overlay case. */
7061 if (!NILP (overlay))
7062 {
7063 /* Find the range of text around this char that
7064 should be active. */
7065 Lisp_Object before, after;
7066 int ignore;
7067
7068 before = Foverlay_start (overlay);
7069 after = Foverlay_end (overlay);
7070 /* Record this as the current active region. */
7071 fast_find_position (w, XFASTINT (before),
7072 &dpyinfo->mouse_face_beg_col,
7073 &dpyinfo->mouse_face_beg_row,
7074 &dpyinfo->mouse_face_beg_x,
7e376260
GM
7075 &dpyinfo->mouse_face_beg_y, Qnil);
7076
0d487c52
GM
7077 dpyinfo->mouse_face_past_end
7078 = !fast_find_position (w, XFASTINT (after),
7079 &dpyinfo->mouse_face_end_col,
7080 &dpyinfo->mouse_face_end_row,
7081 &dpyinfo->mouse_face_end_x,
7e376260 7082 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7083 dpyinfo->mouse_face_window = window;
7084 dpyinfo->mouse_face_face_id
7085 = face_at_buffer_position (w, pos, 0, 0,
7086 &ignore, pos + 1, 1);
7087
7088 /* Display it as active. */
7089 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7090 cursor = None;
0d487c52
GM
7091 }
7092 /* Handle the text property case. */
7093 else if (!NILP (mouse_face) && BUFFERP (object))
7094 {
7095 /* Find the range of text around this char that
7096 should be active. */
7097 Lisp_Object before, after, beginning, end;
7098 int ignore;
7099
7100 beginning = Fmarker_position (w->start);
7101 end = make_number (BUF_Z (XBUFFER (object))
7102 - XFASTINT (w->window_end_pos));
7103 before
7104 = Fprevious_single_property_change (make_number (pos + 1),
7105 Qmouse_face,
7106 object, beginning);
7107 after
7108 = Fnext_single_property_change (position, Qmouse_face,
7109 object, end);
7110
7111 /* Record this as the current active region. */
7112 fast_find_position (w, XFASTINT (before),
7113 &dpyinfo->mouse_face_beg_col,
7114 &dpyinfo->mouse_face_beg_row,
7115 &dpyinfo->mouse_face_beg_x,
7e376260 7116 &dpyinfo->mouse_face_beg_y, Qnil);
0d487c52
GM
7117 dpyinfo->mouse_face_past_end
7118 = !fast_find_position (w, XFASTINT (after),
7119 &dpyinfo->mouse_face_end_col,
7120 &dpyinfo->mouse_face_end_row,
7121 &dpyinfo->mouse_face_end_x,
7e376260 7122 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7123 dpyinfo->mouse_face_window = window;
7124
7125 if (BUFFERP (object))
06a2c219
GM
7126 dpyinfo->mouse_face_face_id
7127 = face_at_buffer_position (w, pos, 0, 0,
7128 &ignore, pos + 1, 1);
7129
0d487c52
GM
7130 /* Display it as active. */
7131 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7132 cursor = None;
0d487c52
GM
7133 }
7134 else if (!NILP (mouse_face) && STRINGP (object))
7135 {
7136 Lisp_Object b, e;
7137 int ignore;
f9db2310 7138
0d487c52
GM
7139 b = Fprevious_single_property_change (make_number (pos + 1),
7140 Qmouse_face,
7141 object, Qnil);
7142 e = Fnext_single_property_change (position, Qmouse_face,
7143 object, Qnil);
7144 if (NILP (b))
7145 b = make_number (0);
7146 if (NILP (e))
7147 e = make_number (XSTRING (object)->size - 1);
7148 fast_find_string_pos (w, XINT (b), object,
06a2c219
GM
7149 &dpyinfo->mouse_face_beg_col,
7150 &dpyinfo->mouse_face_beg_row,
7151 &dpyinfo->mouse_face_beg_x,
0d487c52
GM
7152 &dpyinfo->mouse_face_beg_y, 0);
7153 fast_find_string_pos (w, XINT (e), object,
7154 &dpyinfo->mouse_face_end_col,
7155 &dpyinfo->mouse_face_end_row,
7156 &dpyinfo->mouse_face_end_x,
7157 &dpyinfo->mouse_face_end_y, 1);
7158 dpyinfo->mouse_face_past_end = 0;
7159 dpyinfo->mouse_face_window = window;
7160 dpyinfo->mouse_face_face_id
7161 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7162 glyph->face_id, 1);
7163 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7164 cursor = None;
0d487c52 7165 }
7e376260
GM
7166 else if (STRINGP (object) && NILP (mouse_face))
7167 {
7168 /* A string which doesn't have mouse-face, but
7169 the text ``under'' it might have. */
7170 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7171 int start = MATRIX_ROW_START_CHARPOS (r);
7172
7173 pos = string_buffer_position (w, object, start);
7174 if (pos > 0)
7175 mouse_face = get_char_property_and_overlay (make_number (pos),
7176 Qmouse_face,
7177 w->buffer,
7178 &overlay);
7179 if (!NILP (mouse_face) && !NILP (overlay))
7180 {
7181 Lisp_Object before = Foverlay_start (overlay);
7182 Lisp_Object after = Foverlay_end (overlay);
91c153e2 7183 int ignore;
7e376260
GM
7184
7185 /* Note that we might not be able to find position
7186 BEFORE in the glyph matrix if the overlay is
7187 entirely covered by a `display' property. In
7188 this case, we overshoot. So let's stop in
7189 the glyph matrix before glyphs for OBJECT. */
7190 fast_find_position (w, XFASTINT (before),
7191 &dpyinfo->mouse_face_beg_col,
7192 &dpyinfo->mouse_face_beg_row,
7193 &dpyinfo->mouse_face_beg_x,
7194 &dpyinfo->mouse_face_beg_y,
7195 object);
7196
7197 dpyinfo->mouse_face_past_end
7198 = !fast_find_position (w, XFASTINT (after),
7199 &dpyinfo->mouse_face_end_col,
7200 &dpyinfo->mouse_face_end_row,
7201 &dpyinfo->mouse_face_end_x,
7202 &dpyinfo->mouse_face_end_y,
7203 Qnil);
7204 dpyinfo->mouse_face_window = window;
7205 dpyinfo->mouse_face_face_id
7206 = face_at_buffer_position (w, pos, 0, 0,
7207 &ignore, pos + 1, 1);
7208
7209 /* Display it as active. */
7210 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7211 cursor = None;
7212 }
7213 }
0d487c52 7214 }
06a2c219 7215
8bd189fb
GM
7216 check_help_echo:
7217
0d487c52
GM
7218 /* Look for a `help-echo' property. */
7219 {
7220 Lisp_Object help, overlay;
06a2c219 7221
0d487c52
GM
7222 /* Check overlays first. */
7223 help = overlay = Qnil;
7224 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7225 {
7226 overlay = overlay_vec[i];
7227 help = Foverlay_get (overlay, Qhelp_echo);
7228 }
be010514 7229
0d487c52
GM
7230 if (!NILP (help))
7231 {
7232 help_echo = help;
7233 help_echo_window = window;
7234 help_echo_object = overlay;
7235 help_echo_pos = pos;
7236 }
7237 else
7238 {
7239 Lisp_Object object = glyph->object;
7240 int charpos = glyph->charpos;
7177d86b 7241
0d487c52
GM
7242 /* Try text properties. */
7243 if (STRINGP (object)
7244 && charpos >= 0
7245 && charpos < XSTRING (object)->size)
7246 {
7247 help = Fget_text_property (make_number (charpos),
7248 Qhelp_echo, object);
7249 if (NILP (help))
7250 {
7251 /* If the string itself doesn't specify a help-echo,
7252 see if the buffer text ``under'' it does. */
7253 struct glyph_row *r
7254 = MATRIX_ROW (w->current_matrix, vpos);
7255 int start = MATRIX_ROW_START_CHARPOS (r);
7256 int pos = string_buffer_position (w, object, start);
7257 if (pos > 0)
7258 {
7e376260 7259 help = Fget_char_property (make_number (pos),
0d487c52
GM
7260 Qhelp_echo, w->buffer);
7261 if (!NILP (help))
7262 {
7263 charpos = pos;
7264 object = w->buffer;
7265 }
7266 }
7267 }
7268 }
7269 else if (BUFFERP (object)
7270 && charpos >= BEGV
7271 && charpos < ZV)
7272 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7273 object);
06a2c219 7274
0d487c52
GM
7275 if (!NILP (help))
7276 {
7277 help_echo = help;
7278 help_echo_window = window;
7279 help_echo_object = object;
7280 help_echo_pos = charpos;
7281 }
7282 }
06a2c219 7283 }
0d487c52
GM
7284
7285 BEGV = obegv;
7286 ZV = ozv;
7287 current_buffer = obuf;
06a2c219 7288 }
0d487c52 7289
fa262c07
GM
7290 set_cursor:
7291
0d487c52
GM
7292 if (cursor != None)
7293 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219
GM
7294}
7295
7296static void
7297redo_mouse_highlight ()
7298{
7299 if (!NILP (last_mouse_motion_frame)
7300 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7301 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7302 last_mouse_motion_event.x,
7303 last_mouse_motion_event.y);
7304}
7305
7306
7307\f
7308/***********************************************************************
9ea173e8 7309 Tool-bars
06a2c219
GM
7310 ***********************************************************************/
7311
9ea173e8
GM
7312static int x_tool_bar_item P_ ((struct frame *, int, int,
7313 struct glyph **, int *, int *, int *));
06a2c219 7314
9ea173e8 7315/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
7316 or -1. */
7317
9ea173e8 7318static int last_tool_bar_item;
06a2c219
GM
7319
7320
9ea173e8
GM
7321/* Get information about the tool-bar item at position X/Y on frame F.
7322 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7323 the current matrix of the tool-bar window of F, or NULL if not
7324 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 7325 item in F->tool_bar_items. Value is
06a2c219 7326
9ea173e8 7327 -1 if X/Y is not on a tool-bar item
06a2c219
GM
7328 0 if X/Y is on the same item that was highlighted before.
7329 1 otherwise. */
7330
7331static int
9ea173e8 7332x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
7333 struct frame *f;
7334 int x, y;
7335 struct glyph **glyph;
7336 int *hpos, *vpos, *prop_idx;
7337{
7338 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7339 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7340 int area;
7341
7342 /* Find the glyph under X/Y. */
f9db2310 7343 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
7344 if (*glyph == NULL)
7345 return -1;
7346
9ea173e8 7347 /* Get the start of this tool-bar item's properties in
8daf1204 7348 f->tool_bar_items. */
9ea173e8 7349 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
7350 return -1;
7351
7352 /* Is mouse on the highlighted item? */
9ea173e8 7353 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
7354 && *vpos >= dpyinfo->mouse_face_beg_row
7355 && *vpos <= dpyinfo->mouse_face_end_row
7356 && (*vpos > dpyinfo->mouse_face_beg_row
7357 || *hpos >= dpyinfo->mouse_face_beg_col)
7358 && (*vpos < dpyinfo->mouse_face_end_row
7359 || *hpos < dpyinfo->mouse_face_end_col
7360 || dpyinfo->mouse_face_past_end))
7361 return 0;
7362
7363 return 1;
7364}
7365
7366
9ea173e8 7367/* Handle mouse button event on the tool-bar of frame F, at
6c4a22e6 7368 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
06a2c219
GM
7369 or ButtonRelase. */
7370
7371static void
9ea173e8 7372x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7373 struct frame *f;
7374 XButtonEvent *button_event;
7375{
7376 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7377 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7378 int hpos, vpos, prop_idx;
7379 struct glyph *glyph;
7380 Lisp_Object enabled_p;
7381 int x = button_event->x;
7382 int y = button_event->y;
7383
9ea173e8 7384 /* If not on the highlighted tool-bar item, return. */
06a2c219 7385 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7386 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7387 return;
7388
7389 /* If item is disabled, do nothing. */
8daf1204 7390 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7391 if (NILP (enabled_p))
7392 return;
7393
7394 if (button_event->type == ButtonPress)
7395 {
7396 /* Show item in pressed state. */
7397 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7398 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7399 last_tool_bar_item = prop_idx;
06a2c219
GM
7400 }
7401 else
7402 {
7403 Lisp_Object key, frame;
7404 struct input_event event;
7405
7406 /* Show item in released state. */
7407 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7408 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7409
8daf1204 7410 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7411
7412 XSETFRAME (frame, f);
9ea173e8 7413 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7414 event.frame_or_window = frame;
7415 event.arg = frame;
06a2c219
GM
7416 kbd_buffer_store_event (&event);
7417
9ea173e8 7418 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7419 event.frame_or_window = frame;
7420 event.arg = key;
06a2c219
GM
7421 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7422 button_event->state);
7423 kbd_buffer_store_event (&event);
9ea173e8 7424 last_tool_bar_item = -1;
06a2c219
GM
7425 }
7426}
7427
7428
9ea173e8
GM
7429/* Possibly highlight a tool-bar item on frame F when mouse moves to
7430 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7431 note_mouse_highlight. */
7432
7433static void
9ea173e8 7434note_tool_bar_highlight (f, x, y)
06a2c219
GM
7435 struct frame *f;
7436 int x, y;
7437{
9ea173e8 7438 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7439 struct window *w = XWINDOW (window);
7440 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7441 int hpos, vpos;
7442 struct glyph *glyph;
7443 struct glyph_row *row;
5c187dee 7444 int i;
06a2c219
GM
7445 Lisp_Object enabled_p;
7446 int prop_idx;
140330de 7447 enum draw_glyphs_face draw;
5c187dee 7448 int mouse_down_p, rc;
06a2c219
GM
7449
7450 /* Function note_mouse_highlight is called with negative x(y
7451 values when mouse moves outside of the frame. */
7452 if (x <= 0 || y <= 0)
7453 {
7454 clear_mouse_face (dpyinfo);
7455 return;
7456 }
7457
9ea173e8 7458 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7459 if (rc < 0)
7460 {
9ea173e8 7461 /* Not on tool-bar item. */
06a2c219
GM
7462 clear_mouse_face (dpyinfo);
7463 return;
7464 }
7465 else if (rc == 0)
06a2c219 7466 goto set_help_echo;
b8009dd1 7467
06a2c219
GM
7468 clear_mouse_face (dpyinfo);
7469
9ea173e8 7470 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7471 mouse_down_p = (dpyinfo->grabbed
7472 && f == last_mouse_frame
7473 && FRAME_LIVE_P (f));
7474 if (mouse_down_p
9ea173e8 7475 && last_tool_bar_item != prop_idx)
06a2c219
GM
7476 return;
7477
7478 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7479 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7480
9ea173e8 7481 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7482 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7483 if (!NILP (enabled_p))
7484 {
7485 /* Compute the x-position of the glyph. In front and past the
7486 image is a space. We include this is the highlighted area. */
7487 row = MATRIX_ROW (w->current_matrix, vpos);
7488 for (i = x = 0; i < hpos; ++i)
7489 x += row->glyphs[TEXT_AREA][i].pixel_width;
7490
7491 /* Record this as the current active region. */
7492 dpyinfo->mouse_face_beg_col = hpos;
7493 dpyinfo->mouse_face_beg_row = vpos;
7494 dpyinfo->mouse_face_beg_x = x;
7495 dpyinfo->mouse_face_beg_y = row->y;
7496 dpyinfo->mouse_face_past_end = 0;
7497
7498 dpyinfo->mouse_face_end_col = hpos + 1;
7499 dpyinfo->mouse_face_end_row = vpos;
7500 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7501 dpyinfo->mouse_face_end_y = row->y;
7502 dpyinfo->mouse_face_window = window;
9ea173e8 7503 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7504
7505 /* Display it as active. */
7506 show_mouse_face (dpyinfo, draw);
7507 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7508 }
06a2c219
GM
7509
7510 set_help_echo:
7511
50590060 7512 /* Set help_echo to a help string to display for this tool-bar item.
06a2c219 7513 XTread_socket does the rest. */
7cea38bc 7514 help_echo_object = help_echo_window = Qnil;
be010514 7515 help_echo_pos = -1;
8daf1204 7516 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7517 if (NILP (help_echo))
8daf1204 7518 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7519}
4d73d038 7520
06a2c219
GM
7521
7522\f
9f8531e5
GM
7523/* Find the glyph matrix position of buffer position CHARPOS in window
7524 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7525 current glyphs must be up to date. If CHARPOS is above window
7526 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7527 of last line in W. In the row containing CHARPOS, stop before glyphs
7e376260 7528 having STOP as object. */
b8009dd1 7529
9f8531e5
GM
7530#if 0 /* This is a version of fast_find_position that's more correct
7531 in the presence of hscrolling, for example. I didn't install
7532 it right away because the problem fixed is minor, it failed
7533 in 20.x as well, and I think it's too risky to install
7534 so near the release of 21.1. 2001-09-25 gerd. */
7535
7536static int
7537fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7538 struct window *w;
7539 int charpos;
7540 int *hpos, *vpos, *x, *y;
7541 Lisp_Object stop;
7542{
7543 struct glyph_row *row, *first;
7544 struct glyph *glyph, *end;
7545 int i, past_end = 0;
7546
7547 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 7548 row = row_containing_pos (w, charpos, first, NULL, 0);
9f8531e5
GM
7549 if (row == NULL)
7550 {
7551 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7552 {
7553 *x = *y = *hpos = *vpos = 0;
7554 return 0;
7555 }
7556 else
7557 {
7558 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7559 past_end = 1;
7560 }
7561 }
7562
7563 *x = row->x;
7564 *y = row->y;
7565 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7566
7567 glyph = row->glyphs[TEXT_AREA];
7568 end = glyph + row->used[TEXT_AREA];
7569
7570 /* Skip over glyphs not having an object at the start of the row.
7571 These are special glyphs like truncation marks on terminal
7572 frames. */
7573 if (row->displays_text_p)
7574 while (glyph < end
7575 && INTEGERP (glyph->object)
7576 && !EQ (stop, glyph->object)
7577 && glyph->charpos < 0)
7578 {
7579 *x += glyph->pixel_width;
7580 ++glyph;
7581 }
7582
7583 while (glyph < end
7584 && !INTEGERP (glyph->object)
7585 && !EQ (stop, glyph->object)
7586 && (!BUFFERP (glyph->object)
7587 || glyph->charpos < charpos))
7588 {
7589 *x += glyph->pixel_width;
7590 ++glyph;
7591 }
7592
7593 *hpos = glyph - row->glyphs[TEXT_AREA];
7594 return past_end;
7595}
7596
7597#else /* not 0 */
7598
b8009dd1 7599static int
7e376260 7600fast_find_position (w, pos, hpos, vpos, x, y, stop)
06a2c219 7601 struct window *w;
b8009dd1 7602 int pos;
06a2c219 7603 int *hpos, *vpos, *x, *y;
7e376260 7604 Lisp_Object stop;
b8009dd1 7605{
b8009dd1 7606 int i;
bf1c0ba1 7607 int lastcol;
06a2c219
GM
7608 int maybe_next_line_p = 0;
7609 int line_start_position;
7610 int yb = window_text_bottom_y (w);
03d1a189
GM
7611 struct glyph_row *row, *best_row;
7612 int row_vpos, best_row_vpos;
06a2c219
GM
7613 int current_x;
7614
03d1a189
GM
7615 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7616 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7617
06a2c219 7618 while (row->y < yb)
b8009dd1 7619 {
06a2c219
GM
7620 if (row->used[TEXT_AREA])
7621 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7622 else
7623 line_start_position = 0;
7624
7625 if (line_start_position > pos)
b8009dd1 7626 break;
77b68646
RS
7627 /* If the position sought is the end of the buffer,
7628 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7629 else if (line_start_position == pos
7630 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7631 {
06a2c219 7632 maybe_next_line_p = 1;
77b68646
RS
7633 break;
7634 }
06a2c219
GM
7635 else if (line_start_position > 0)
7636 {
7637 best_row = row;
7638 best_row_vpos = row_vpos;
7639 }
4b0bb6f3
GM
7640
7641 if (row->y + row->height >= yb)
7642 break;
06a2c219
GM
7643
7644 ++row;
7645 ++row_vpos;
b8009dd1 7646 }
06a2c219
GM
7647
7648 /* Find the right column within BEST_ROW. */
7649 lastcol = 0;
7650 current_x = best_row->x;
7651 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7652 {
06a2c219 7653 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7e376260 7654 int charpos = glyph->charpos;
06a2c219 7655
7e376260 7656 if (BUFFERP (glyph->object))
bf1c0ba1 7657 {
7e376260
GM
7658 if (charpos == pos)
7659 {
7660 *hpos = i;
7661 *vpos = best_row_vpos;
7662 *x = current_x;
7663 *y = best_row->y;
7664 return 1;
7665 }
7666 else if (charpos > pos)
7667 break;
bf1c0ba1 7668 }
7e376260 7669 else if (EQ (glyph->object, stop))
4d73d038 7670 break;
06a2c219 7671
7e376260
GM
7672 if (charpos > 0)
7673 lastcol = i;
06a2c219 7674 current_x += glyph->pixel_width;
bf1c0ba1 7675 }
b8009dd1 7676
77b68646
RS
7677 /* If we're looking for the end of the buffer,
7678 and we didn't find it in the line we scanned,
7679 use the start of the following line. */
06a2c219 7680 if (maybe_next_line_p)
77b68646 7681 {
06a2c219
GM
7682 ++best_row;
7683 ++best_row_vpos;
7684 lastcol = 0;
7685 current_x = best_row->x;
77b68646
RS
7686 }
7687
06a2c219
GM
7688 *vpos = best_row_vpos;
7689 *hpos = lastcol + 1;
7690 *x = current_x;
7691 *y = best_row->y;
b8009dd1
RS
7692 return 0;
7693}
7694
9f8531e5
GM
7695#endif /* not 0 */
7696
06a2c219 7697
269b7745 7698/* Find the position of the glyph for position POS in OBJECT in
7a4bce14
PJ
7699 window W's current matrix, and return in *X, *Y the pixel
7700 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
f9db2310
GM
7701
7702 RIGHT_P non-zero means return the position of the right edge of the
7703 glyph, RIGHT_P zero means return the left edge position.
7704
7705 If no glyph for POS exists in the matrix, return the position of
7706 the glyph with the next smaller position that is in the matrix, if
7707 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7708 exists in the matrix, return the position of the glyph with the
7709 next larger position in OBJECT.
7710
7711 Value is non-zero if a glyph was found. */
7712
7713static int
7714fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7715 struct window *w;
7716 int pos;
7717 Lisp_Object object;
7718 int *hpos, *vpos, *x, *y;
7719 int right_p;
7720{
7721 int yb = window_text_bottom_y (w);
7722 struct glyph_row *r;
7723 struct glyph *best_glyph = NULL;
7724 struct glyph_row *best_row = NULL;
7725 int best_x = 0;
7726
7727 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7728 r->enabled_p && r->y < yb;
7729 ++r)
7730 {
7731 struct glyph *g = r->glyphs[TEXT_AREA];
7732 struct glyph *e = g + r->used[TEXT_AREA];
7733 int gx;
7734
7735 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7736 if (EQ (g->object, object))
7737 {
7738 if (g->charpos == pos)
7739 {
7740 best_glyph = g;
7741 best_x = gx;
7742 best_row = r;
7743 goto found;
7744 }
7745 else if (best_glyph == NULL
7746 || ((abs (g->charpos - pos)
7747 < abs (best_glyph->charpos - pos))
7748 && (right_p
7749 ? g->charpos < pos
7750 : g->charpos > pos)))
7751 {
7752 best_glyph = g;
7753 best_x = gx;
7754 best_row = r;
7755 }
7756 }
7757 }
7758
7759 found:
7760
7761 if (best_glyph)
7762 {
7763 *x = best_x;
7764 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7765
7766 if (right_p)
7767 {
7768 *x += best_glyph->pixel_width;
7769 ++*hpos;
7770 }
7771
7772 *y = best_row->y;
7773 *vpos = best_row - w->current_matrix->rows;
7774 }
7775
7776 return best_glyph != NULL;
7777}
7778
7779
b8009dd1
RS
7780/* Display the active region described by mouse_face_*
7781 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7782
7783static void
06a2c219 7784show_mouse_face (dpyinfo, draw)
7a13e894 7785 struct x_display_info *dpyinfo;
06a2c219 7786 enum draw_glyphs_face draw;
b8009dd1 7787{
7a13e894 7788 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7789 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 7790
b2bbd509
GM
7791 if (/* If window is in the process of being destroyed, don't bother
7792 to do anything. */
7793 w->current_matrix != NULL
66301061
KS
7794 /* Don't update mouse highlight if hidden */
7795 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
b2bbd509
GM
7796 /* Recognize when we are called to operate on rows that don't exist
7797 anymore. This can happen when a window is split. */
7798 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
06a2c219 7799 {
b2bbd509
GM
7800 int phys_cursor_on_p = w->phys_cursor_on_p;
7801 struct glyph_row *row, *first, *last;
06a2c219 7802
b2bbd509
GM
7803 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7804 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7805
7806 for (row = first; row <= last && row->enabled_p; ++row)
06a2c219 7807 {
b2bbd509 7808 int start_hpos, end_hpos, start_x;
06a2c219 7809
b2bbd509
GM
7810 /* For all but the first row, the highlight starts at column 0. */
7811 if (row == first)
7812 {
7813 start_hpos = dpyinfo->mouse_face_beg_col;
7814 start_x = dpyinfo->mouse_face_beg_x;
7815 }
7816 else
7817 {
7818 start_hpos = 0;
7819 start_x = 0;
7820 }
06a2c219 7821
b2bbd509
GM
7822 if (row == last)
7823 end_hpos = dpyinfo->mouse_face_end_col;
7824 else
7825 end_hpos = row->used[TEXT_AREA];
b8009dd1 7826
b2bbd509
GM
7827 if (end_hpos > start_hpos)
7828 {
7829 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7830 start_hpos, end_hpos, draw, 0);
b8009dd1 7831
60626bab
GM
7832 row->mouse_face_p
7833 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
b2bbd509
GM
7834 }
7835 }
2729a2b5 7836
b2bbd509
GM
7837 /* When we've written over the cursor, arrange for it to
7838 be displayed again. */
7839 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7840 x_display_cursor (w, 1,
7841 w->phys_cursor.hpos, w->phys_cursor.vpos,
7842 w->phys_cursor.x, w->phys_cursor.y);
7843 }
fb3b7de5 7844
06a2c219
GM
7845 /* Change the mouse cursor. */
7846 if (draw == DRAW_NORMAL_TEXT)
7847 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7848 f->output_data.x->text_cursor);
7849 else if (draw == DRAW_MOUSE_FACE)
334208b7 7850 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7851 f->output_data.x->cross_cursor);
27ead1d5 7852 else
334208b7 7853 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7854 f->output_data.x->nontext_cursor);
b8009dd1
RS
7855}
7856
7857/* Clear out the mouse-highlighted active region.
fa262c07
GM
7858 Redraw it un-highlighted first. Value is non-zero if mouse
7859 face was actually drawn unhighlighted. */
b8009dd1 7860
fa262c07 7861static int
7a13e894
RS
7862clear_mouse_face (dpyinfo)
7863 struct x_display_info *dpyinfo;
b8009dd1 7864{
fa262c07 7865 int cleared = 0;
06a2c219 7866
fa262c07
GM
7867 if (!NILP (dpyinfo->mouse_face_window))
7868 {
7869 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7870 cleared = 1;
7871 }
b8009dd1 7872
7a13e894
RS
7873 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7874 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7875 dpyinfo->mouse_face_window = Qnil;
1b85dc1c 7876 dpyinfo->mouse_face_overlay = Qnil;
fa262c07 7877 return cleared;
b8009dd1 7878}
e687d06e 7879
71b8321e
GM
7880
7881/* Clear any mouse-face on window W. This function is part of the
7882 redisplay interface, and is called from try_window_id and similar
7883 functions to ensure the mouse-highlight is off. */
7884
7885static void
7886x_clear_mouse_face (w)
7887 struct window *w;
7888{
7889 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7890 Lisp_Object window;
7891
2e636f9d 7892 BLOCK_INPUT;
71b8321e
GM
7893 XSETWINDOW (window, w);
7894 if (EQ (window, dpyinfo->mouse_face_window))
7895 clear_mouse_face (dpyinfo);
2e636f9d 7896 UNBLOCK_INPUT;
71b8321e
GM
7897}
7898
7899
e687d06e
RS
7900/* Just discard the mouse face information for frame F, if any.
7901 This is used when the size of F is changed. */
7902
dfcf069d 7903void
e687d06e
RS
7904cancel_mouse_face (f)
7905 FRAME_PTR f;
7906{
7907 Lisp_Object window;
7908 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7909
7910 window = dpyinfo->mouse_face_window;
7911 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7912 {
7913 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7914 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7915 dpyinfo->mouse_face_window = Qnil;
7916 }
7917}
b52b65bd 7918
b8009dd1 7919\f
b52b65bd
GM
7920static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7921
7922
7923/* Try to determine frame pixel position and size of the glyph under
7924 frame pixel coordinates X/Y on frame F . Return the position and
7925 size in *RECT. Value is non-zero if we could compute these
7926 values. */
7927
7928static int
7929glyph_rect (f, x, y, rect)
7930 struct frame *f;
7931 int x, y;
7932 XRectangle *rect;
7933{
7934 Lisp_Object window;
7935 int part, found = 0;
7936
7937 window = window_from_coordinates (f, x, y, &part, 0);
7938 if (!NILP (window))
7939 {
7940 struct window *w = XWINDOW (window);
7941 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7942 struct glyph_row *end = r + w->current_matrix->nrows - 1;
b52b65bd
GM
7943
7944 frame_to_window_pixel_xy (w, &x, &y);
7945
7946 for (; !found && r < end && r->enabled_p; ++r)
7947 if (r->y >= y)
7948 {
7949 struct glyph *g = r->glyphs[TEXT_AREA];
7950 struct glyph *end = g + r->used[TEXT_AREA];
7951 int gx;
7952
7953 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7954 if (gx >= x)
7955 {
7956 rect->width = g->pixel_width;
7957 rect->height = r->height;
7958 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7959 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7960 found = 1;
7961 }
7962 }
7963 }
7964
7965 return found;
7966}
7967
12ba150f 7968
90e65f07 7969/* Return the current position of the mouse.
b52b65bd 7970 *FP should be a frame which indicates which display to ask about.
90e65f07 7971
b52b65bd
GM
7972 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7973 and *PART to the frame, window, and scroll bar part that the mouse
7974 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 7975 position on the scroll bar.
12ba150f 7976
b52b65bd
GM
7977 If the mouse movement started elsewhere, set *FP to the frame the
7978 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
7979 the mouse is over.
7980
b52b65bd 7981 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
7982 was at this position.
7983
a135645a
RS
7984 Don't store anything if we don't have a valid set of values to report.
7985
90e65f07 7986 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7987 movement. */
90e65f07
JB
7988
7989static void
1cf412ec 7990XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7991 FRAME_PTR *fp;
1cf412ec 7992 int insist;
12ba150f 7993 Lisp_Object *bar_window;
ab648270 7994 enum scroll_bar_part *part;
90e65f07 7995 Lisp_Object *x, *y;
e5d77022 7996 unsigned long *time;
90e65f07 7997{
a135645a
RS
7998 FRAME_PTR f1;
7999
90e65f07
JB
8000 BLOCK_INPUT;
8001
8bcee03e 8002 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 8003 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
8004 else
8005 {
12ba150f
JB
8006 Window root;
8007 int root_x, root_y;
90e65f07 8008
12ba150f
JB
8009 Window dummy_window;
8010 int dummy;
8011
39d8bb4d
KH
8012 Lisp_Object frame, tail;
8013
8014 /* Clear the mouse-moved flag for every frame on this display. */
8015 FOR_EACH_FRAME (tail, frame)
8016 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8017 XFRAME (frame)->mouse_moved = 0;
8018
ab648270 8019 last_mouse_scroll_bar = Qnil;
12ba150f
JB
8020
8021 /* Figure out which root window we're on. */
334208b7
RS
8022 XQueryPointer (FRAME_X_DISPLAY (*fp),
8023 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
8024
8025 /* The root window which contains the pointer. */
8026 &root,
8027
8028 /* Trash which we can't trust if the pointer is on
8029 a different screen. */
8030 &dummy_window,
8031
8032 /* The position on that root window. */
58769bee 8033 &root_x, &root_y,
12ba150f
JB
8034
8035 /* More trash we can't trust. */
8036 &dummy, &dummy,
8037
8038 /* Modifier keys and pointer buttons, about which
8039 we don't care. */
8040 (unsigned int *) &dummy);
8041
8042 /* Now we have a position on the root; find the innermost window
8043 containing the pointer. */
8044 {
8045 Window win, child;
8046 int win_x, win_y;
06a2c219 8047 int parent_x = 0, parent_y = 0;
e99db5a1 8048 int count;
12ba150f
JB
8049
8050 win = root;
69388238 8051
2d7fc7e8
RS
8052 /* XTranslateCoordinates can get errors if the window
8053 structure is changing at the same time this function
8054 is running. So at least we must not crash from them. */
8055
e99db5a1 8056 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 8057
334208b7 8058 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 8059 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 8060 {
69388238
RS
8061 /* If mouse was grabbed on a frame, give coords for that frame
8062 even if the mouse is now outside it. */
334208b7 8063 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 8064
12ba150f 8065 /* From-window, to-window. */
69388238 8066 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
8067
8068 /* From-position, to-position. */
8069 root_x, root_y, &win_x, &win_y,
8070
8071 /* Child of win. */
8072 &child);
69388238
RS
8073 f1 = last_mouse_frame;
8074 }
8075 else
8076 {
8077 while (1)
8078 {
334208b7 8079 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 8080
69388238
RS
8081 /* From-window, to-window. */
8082 root, win,
12ba150f 8083
69388238
RS
8084 /* From-position, to-position. */
8085 root_x, root_y, &win_x, &win_y,
8086
8087 /* Child of win. */
8088 &child);
8089
9af3143a 8090 if (child == None || child == win)
69388238
RS
8091 break;
8092
8093 win = child;
8094 parent_x = win_x;
8095 parent_y = win_y;
8096 }
12ba150f 8097
69388238
RS
8098 /* Now we know that:
8099 win is the innermost window containing the pointer
8100 (XTC says it has no child containing the pointer),
8101 win_x and win_y are the pointer's position in it
8102 (XTC did this the last time through), and
8103 parent_x and parent_y are the pointer's position in win's parent.
8104 (They are what win_x and win_y were when win was child.
8105 If win is the root window, it has no parent, and
8106 parent_{x,y} are invalid, but that's okay, because we'll
8107 never use them in that case.) */
8108
8109 /* Is win one of our frames? */
19126e11 8110 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
8111
8112#ifdef USE_X_TOOLKIT
8113 /* If we end up with the menu bar window, say it's not
8114 on the frame. */
8115 if (f1 != NULL
8116 && f1->output_data.x->menubar_widget
8117 && win == XtWindow (f1->output_data.x->menubar_widget))
8118 f1 = NULL;
8119#endif /* USE_X_TOOLKIT */
69388238 8120 }
58769bee 8121
2d7fc7e8
RS
8122 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8123 f1 = 0;
8124
e99db5a1 8125 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 8126
ab648270 8127 /* If not, is it one of our scroll bars? */
a135645a 8128 if (! f1)
12ba150f 8129 {
ab648270 8130 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
8131
8132 if (bar)
8133 {
a135645a 8134 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8135 win_x = parent_x;
8136 win_y = parent_y;
8137 }
8138 }
90e65f07 8139
8bcee03e 8140 if (f1 == 0 && insist > 0)
b86bd3dd 8141 f1 = SELECTED_FRAME ();
1cf412ec 8142
a135645a 8143 if (f1)
12ba150f 8144 {
06a2c219
GM
8145 /* Ok, we found a frame. Store all the values.
8146 last_mouse_glyph is a rectangle used to reduce the
8147 generation of mouse events. To not miss any motion
8148 events, we must divide the frame into rectangles of the
8149 size of the smallest character that could be displayed
8150 on it, i.e. into the same rectangles that matrices on
8151 the frame are divided into. */
8152
b52b65bd
GM
8153 int width, height, gx, gy;
8154 XRectangle rect;
8155
8156 if (glyph_rect (f1, win_x, win_y, &rect))
8157 last_mouse_glyph = rect;
8158 else
8159 {
8160 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8161 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8162 gx = win_x;
8163 gy = win_y;
06a2c219 8164
b52b65bd
GM
8165 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8166 round down even for negative values. */
8167 if (gx < 0)
8168 gx -= width - 1;
4f00e84d 8169 if (gy < 0)
b52b65bd
GM
8170 gy -= height - 1;
8171 gx = (gx + width - 1) / width * width;
8172 gy = (gy + height - 1) / height * height;
8173
8174 last_mouse_glyph.width = width;
8175 last_mouse_glyph.height = height;
8176 last_mouse_glyph.x = gx;
8177 last_mouse_glyph.y = gy;
8178 }
12ba150f
JB
8179
8180 *bar_window = Qnil;
8181 *part = 0;
334208b7 8182 *fp = f1;
e0c1aef2
KH
8183 XSETINT (*x, win_x);
8184 XSETINT (*y, win_y);
12ba150f
JB
8185 *time = last_mouse_movement_time;
8186 }
8187 }
8188 }
90e65f07
JB
8189
8190 UNBLOCK_INPUT;
8191}
f451eb13 8192
06a2c219 8193
06a2c219 8194#ifdef USE_X_TOOLKIT
bffcfca9
GM
8195
8196/* Atimer callback function for TIMER. Called every 0.1s to process
8197 Xt timeouts, if needed. We must avoid calling XtAppPending as
8198 much as possible because that function does an implicit XFlush
8199 that slows us down. */
8200
8201static void
8202x_process_timeouts (timer)
8203 struct atimer *timer;
8204{
8205 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8206 {
8207 BLOCK_INPUT;
8208 while (XtAppPending (Xt_app_con) & XtIMTimer)
8209 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8210 UNBLOCK_INPUT;
8211 }
06a2c219
GM
8212}
8213
bffcfca9 8214#endif /* USE_X_TOOLKIT */
06a2c219
GM
8215
8216\f
8217/* Scroll bar support. */
8218
8219/* Given an X window ID, find the struct scroll_bar which manages it.
8220 This can be called in GC, so we have to make sure to strip off mark
8221 bits. */
bffcfca9 8222
06a2c219
GM
8223static struct scroll_bar *
8224x_window_to_scroll_bar (window_id)
8225 Window window_id;
8226{
8227 Lisp_Object tail;
8228
8229 for (tail = Vframe_list;
8230 XGCTYPE (tail) == Lisp_Cons;
8e713be6 8231 tail = XCDR (tail))
06a2c219
GM
8232 {
8233 Lisp_Object frame, bar, condemned;
8234
8e713be6 8235 frame = XCAR (tail);
06a2c219
GM
8236 /* All elements of Vframe_list should be frames. */
8237 if (! GC_FRAMEP (frame))
8238 abort ();
8239
8240 /* Scan this frame's scroll bar list for a scroll bar with the
8241 right window ID. */
8242 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8243 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8244 /* This trick allows us to search both the ordinary and
8245 condemned scroll bar lists with one loop. */
8246 ! GC_NILP (bar) || (bar = condemned,
8247 condemned = Qnil,
8248 ! GC_NILP (bar));
8249 bar = XSCROLL_BAR (bar)->next)
8250 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8251 return XSCROLL_BAR (bar);
8252 }
8253
8254 return 0;
8255}
8256
8257
01f67d2c 8258#if defined USE_LUCID
c95fc5f1
GM
8259
8260/* Return the Lucid menu bar WINDOW is part of. Return null
8261 if WINDOW is not part of a menu bar. */
8262
8263static Widget
8264x_window_to_menu_bar (window)
8265 Window window;
8266{
8267 Lisp_Object tail;
8268
8269 for (tail = Vframe_list;
8270 XGCTYPE (tail) == Lisp_Cons;
8271 tail = XCDR (tail))
8272 {
8273 Lisp_Object frame = XCAR (tail);
8274 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8275
8276 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8277 return menu_bar;
8278 }
8279
8280 return NULL;
8281}
8282
01f67d2c 8283#endif /* USE_LUCID */
c95fc5f1 8284
06a2c219
GM
8285\f
8286/************************************************************************
8287 Toolkit scroll bars
8288 ************************************************************************/
8289
eccc05db 8290#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8291
8292static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8293static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8294static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8295 struct scroll_bar *));
8296static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8297 int, int, int));
8298
8299
8300/* Id of action hook installed for scroll bars. */
8301
8302static XtActionHookId action_hook_id;
8303
8304/* Lisp window being scrolled. Set when starting to interact with
8305 a toolkit scroll bar, reset to nil when ending the interaction. */
8306
8307static Lisp_Object window_being_scrolled;
8308
8309/* Last scroll bar part sent in xm_scroll_callback. */
8310
8311static int last_scroll_bar_part;
8312
ec18280f
SM
8313/* Whether this is an Xaw with arrow-scrollbars. This should imply
8314 that movements of 1/20 of the screen size are mapped to up/down. */
8315
8316static Boolean xaw3d_arrow_scroll;
8317
8318/* Whether the drag scrolling maintains the mouse at the top of the
8319 thumb. If not, resizing the thumb needs to be done more carefully
8320 to avoid jerkyness. */
8321
8322static Boolean xaw3d_pick_top;
8323
06a2c219
GM
8324
8325/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 8326 bars are used.. The hook is responsible for detecting when
06a2c219 8327 the user ends an interaction with the scroll bar, and generates
3b8f9651 8328 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
8329
8330static void
8331xt_action_hook (widget, client_data, action_name, event, params,
8332 num_params)
8333 Widget widget;
8334 XtPointer client_data;
8335 String action_name;
8336 XEvent *event;
8337 String *params;
8338 Cardinal *num_params;
8339{
8340 int scroll_bar_p;
8341 char *end_action;
8342
8343#ifdef USE_MOTIF
8344 scroll_bar_p = XmIsScrollBar (widget);
8345 end_action = "Release";
ec18280f 8346#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8347 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8348 end_action = "EndScroll";
ec18280f 8349#endif /* USE_MOTIF */
06a2c219 8350
06a2c219
GM
8351 if (scroll_bar_p
8352 && strcmp (action_name, end_action) == 0
8353 && WINDOWP (window_being_scrolled))
8354 {
8355 struct window *w;
8356
8357 x_send_scroll_bar_event (window_being_scrolled,
8358 scroll_bar_end_scroll, 0, 0);
8359 w = XWINDOW (window_being_scrolled);
8360 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8361 window_being_scrolled = Qnil;
8362 last_scroll_bar_part = -1;
bffcfca9
GM
8363
8364 /* Xt timeouts no longer needed. */
8365 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
8366 }
8367}
8368
07b3d16e
GM
8369/* A vector of windows used for communication between
8370 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8371
8372static struct window **scroll_bar_windows;
8373static int scroll_bar_windows_size;
8374
06a2c219
GM
8375
8376/* Send a client message with message type Xatom_Scrollbar for a
8377 scroll action to the frame of WINDOW. PART is a value identifying
8378 the part of the scroll bar that was clicked on. PORTION is the
8379 amount to scroll of a whole of WHOLE. */
8380
8381static void
8382x_send_scroll_bar_event (window, part, portion, whole)
8383 Lisp_Object window;
8384 int part, portion, whole;
8385{
8386 XEvent event;
8387 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
8388 struct window *w = XWINDOW (window);
8389 struct frame *f = XFRAME (w->frame);
8390 int i;
06a2c219 8391
07b3d16e
GM
8392 BLOCK_INPUT;
8393
06a2c219
GM
8394 /* Construct a ClientMessage event to send to the frame. */
8395 ev->type = ClientMessage;
8396 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8397 ev->display = FRAME_X_DISPLAY (f);
8398 ev->window = FRAME_X_WINDOW (f);
8399 ev->format = 32;
07b3d16e
GM
8400
8401 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8402 not enough to store a pointer or Lisp_Object on a 64 bit system.
8403 So, store the window in scroll_bar_windows and pass the index
8404 into that array in the event. */
8405 for (i = 0; i < scroll_bar_windows_size; ++i)
8406 if (scroll_bar_windows[i] == NULL)
8407 break;
8408
8409 if (i == scroll_bar_windows_size)
8410 {
8411 int new_size = max (10, 2 * scroll_bar_windows_size);
8412 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8413 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8414
8415 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8416 nbytes);
8417 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8418 scroll_bar_windows_size = new_size;
8419 }
8420
8421 scroll_bar_windows[i] = w;
8422 ev->data.l[0] = (long) i;
06a2c219
GM
8423 ev->data.l[1] = (long) part;
8424 ev->data.l[2] = (long) 0;
8425 ev->data.l[3] = (long) portion;
8426 ev->data.l[4] = (long) whole;
8427
bffcfca9
GM
8428 /* Make Xt timeouts work while the scroll bar is active. */
8429 toolkit_scroll_bar_interaction = 1;
8430
06a2c219
GM
8431 /* Setting the event mask to zero means that the message will
8432 be sent to the client that created the window, and if that
8433 window no longer exists, no event will be sent. */
06a2c219
GM
8434 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8435 UNBLOCK_INPUT;
8436}
8437
8438
8439/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8440 in *IEVENT. */
8441
8442static void
8443x_scroll_bar_to_input_event (event, ievent)
8444 XEvent *event;
8445 struct input_event *ievent;
8446{
8447 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
8448 Lisp_Object window;
8449 struct frame *f;
07b3d16e
GM
8450 struct window *w;
8451
8452 w = scroll_bar_windows[ev->data.l[0]];
8453 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 8454
07b3d16e
GM
8455 XSETWINDOW (window, w);
8456 f = XFRAME (w->frame);
06a2c219 8457
3b8f9651 8458 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 8459 ievent->frame_or_window = window;
0f8aabe9 8460 ievent->arg = Qnil;
06a2c219
GM
8461 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8462 ievent->part = ev->data.l[1];
8463 ievent->code = ev->data.l[2];
8464 ievent->x = make_number ((int) ev->data.l[3]);
8465 ievent->y = make_number ((int) ev->data.l[4]);
8466 ievent->modifiers = 0;
8467}
8468
8469
8470#ifdef USE_MOTIF
8471
8472/* Minimum and maximum values used for Motif scroll bars. */
8473
8474#define XM_SB_MIN 1
8475#define XM_SB_MAX 10000000
8476#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8477
8478
8479/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8480 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8481 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8482
8483static void
8484xm_scroll_callback (widget, client_data, call_data)
8485 Widget widget;
8486 XtPointer client_data, call_data;
8487{
8488 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8489 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
8490 int part = -1, whole = 0, portion = 0;
8491
8492 switch (cs->reason)
8493 {
8494 case XmCR_DECREMENT:
8495 bar->dragging = Qnil;
8496 part = scroll_bar_up_arrow;
8497 break;
8498
8499 case XmCR_INCREMENT:
8500 bar->dragging = Qnil;
8501 part = scroll_bar_down_arrow;
8502 break;
8503
8504 case XmCR_PAGE_DECREMENT:
8505 bar->dragging = Qnil;
8506 part = scroll_bar_above_handle;
8507 break;
8508
8509 case XmCR_PAGE_INCREMENT:
8510 bar->dragging = Qnil;
8511 part = scroll_bar_below_handle;
8512 break;
8513
8514 case XmCR_TO_TOP:
8515 bar->dragging = Qnil;
8516 part = scroll_bar_to_top;
8517 break;
8518
8519 case XmCR_TO_BOTTOM:
8520 bar->dragging = Qnil;
8521 part = scroll_bar_to_bottom;
8522 break;
8523
8524 case XmCR_DRAG:
8525 {
8526 int slider_size;
8527 int dragging_down_p = (INTEGERP (bar->dragging)
8528 && XINT (bar->dragging) <= cs->value);
8529
8530 /* Get the slider size. */
8531 BLOCK_INPUT;
8532 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8533 UNBLOCK_INPUT;
8534
6f2a7a68
SM
8535 whole = XM_SB_RANGE - slider_size;
8536 portion = min (cs->value - XM_SB_MIN, whole);
8537 part = scroll_bar_handle;
8538 bar->dragging = make_number (cs->value);
06a2c219
GM
8539 }
8540 break;
8541
8542 case XmCR_VALUE_CHANGED:
8543 break;
8544 };
8545
8546 if (part >= 0)
8547 {
8548 window_being_scrolled = bar->window;
8549 last_scroll_bar_part = part;
8550 x_send_scroll_bar_event (bar->window, part, portion, whole);
8551 }
8552}
8553
8554
ec18280f 8555#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
8556
8557
ec18280f 8558/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8559 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8560 scroll bar struct. CALL_DATA is a pointer to a float saying where
8561 the thumb is. */
8562
8563static void
ec18280f 8564xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8565 Widget widget;
8566 XtPointer client_data, call_data;
8567{
8568 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8569 float top = *(float *) call_data;
8570 float shown;
ec18280f
SM
8571 int whole, portion, height;
8572 int part;
06a2c219
GM
8573
8574 /* Get the size of the thumb, a value between 0 and 1. */
8575 BLOCK_INPUT;
ec18280f 8576 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8577 UNBLOCK_INPUT;
8578
8579 whole = 10000000;
8580 portion = shown < 1 ? top * whole : 0;
06a2c219 8581
ec18280f
SM
8582 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8583 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8584 the bottom, so we force the scrolling whenever we see that we're
8585 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8586 we try to ensure that we always stay two pixels away from the
8587 bottom). */
06a2c219
GM
8588 part = scroll_bar_down_arrow;
8589 else
8590 part = scroll_bar_handle;
8591
8592 window_being_scrolled = bar->window;
8593 bar->dragging = make_number (portion);
8594 last_scroll_bar_part = part;
8595 x_send_scroll_bar_event (bar->window, part, portion, whole);
8596}
8597
8598
ec18280f
SM
8599/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8600 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8601 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8602 the scroll bar. CALL_DATA is an integer specifying the action that
8603 has taken place. It's magnitude is in the range 0..height of the
8604 scroll bar. Negative values mean scroll towards buffer start.
8605 Values < height of scroll bar mean line-wise movement. */
8606
8607static void
ec18280f 8608xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8609 Widget widget;
8610 XtPointer client_data, call_data;
8611{
8612 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8613 int position = (int) call_data;
8614 Dimension height;
8615 int part;
8616
8617 /* Get the height of the scroll bar. */
8618 BLOCK_INPUT;
8619 XtVaGetValues (widget, XtNheight, &height, NULL);
8620 UNBLOCK_INPUT;
8621
ec18280f
SM
8622 if (abs (position) >= height)
8623 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8624
8625 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8626 it maps line-movement to call_data = max(5, height/20). */
8627 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8628 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8629 else
ec18280f 8630 part = scroll_bar_move_ratio;
06a2c219
GM
8631
8632 window_being_scrolled = bar->window;
8633 bar->dragging = Qnil;
8634 last_scroll_bar_part = part;
ec18280f 8635 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8636}
8637
8638
8639#endif /* not USE_MOTIF */
8640
8641
8642/* Create the widget for scroll bar BAR on frame F. Record the widget
8643 and X window of the scroll bar in BAR. */
8644
8645static void
8646x_create_toolkit_scroll_bar (f, bar)
8647 struct frame *f;
8648 struct scroll_bar *bar;
8649{
8650 Window xwindow;
8651 Widget widget;
8652 Arg av[20];
8653 int ac = 0;
8654 char *scroll_bar_name = "verticalScrollBar";
8655 unsigned long pixel;
8656
8657 BLOCK_INPUT;
8658
8659#ifdef USE_MOTIF
06a2c219
GM
8660 /* Set resources. Create the widget. */
8661 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8662 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8663 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8664 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8665 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8666 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8667 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8668
8669 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8670 if (pixel != -1)
8671 {
8672 XtSetArg (av[ac], XmNforeground, pixel);
8673 ++ac;
8674 }
8675
8676 pixel = f->output_data.x->scroll_bar_background_pixel;
8677 if (pixel != -1)
8678 {
8679 XtSetArg (av[ac], XmNbackground, pixel);
8680 ++ac;
8681 }
8682
8683 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8684 scroll_bar_name, av, ac);
8685
8686 /* Add one callback for everything that can happen. */
8687 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8688 (XtPointer) bar);
8689 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8690 (XtPointer) bar);
8691 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8692 (XtPointer) bar);
8693 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8694 (XtPointer) bar);
8695 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8696 (XtPointer) bar);
8697 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8698 (XtPointer) bar);
8699 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8700 (XtPointer) bar);
8701
8702 /* Realize the widget. Only after that is the X window created. */
8703 XtRealizeWidget (widget);
8704
8705 /* Set the cursor to an arrow. I didn't find a resource to do that.
8706 And I'm wondering why it hasn't an arrow cursor by default. */
8707 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8708 f->output_data.x->nontext_cursor);
8709
ec18280f 8710#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8711
8712 /* Set resources. Create the widget. The background of the
8713 Xaw3d scroll bar widget is a little bit light for my taste.
8714 We don't alter it here to let users change it according
8715 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8716 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8717 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8718 /* For smoother scrolling with Xaw3d -sm */
8719 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
06a2c219
GM
8720
8721 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8722 if (pixel != -1)
8723 {
8724 XtSetArg (av[ac], XtNforeground, pixel);
8725 ++ac;
8726 }
8727
8728 pixel = f->output_data.x->scroll_bar_background_pixel;
8729 if (pixel != -1)
8730 {
8731 XtSetArg (av[ac], XtNbackground, pixel);
8732 ++ac;
8733 }
7c1bef7a
MB
8734
8735 /* Top/bottom shadow colors. */
8736
8737 /* Allocate them, if necessary. */
8738 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8739 {
8740 pixel = f->output_data.x->scroll_bar_background_pixel;
8741 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8742 &pixel, 1.2, 0x8000))
8743 pixel = -1;
8744 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8745 }
8746 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8747 {
8748 pixel = f->output_data.x->scroll_bar_background_pixel;
8749 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8750 &pixel, 0.6, 0x4000))
8751 pixel = -1;
8752 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8753 }
8754
8755 /* Tell the toolkit about them. */
8756 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8757 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8758 /* We tried to allocate a color for the top/bottom shadow, and
8759 failed, so tell Xaw3d to use dithering instead. */
8760 {
8761 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8762 ++ac;
8763 }
8764 else
8765 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8766 be more consistent with other emacs 3d colors, and since Xaw3d is
8767 not good at dealing with allocation failure. */
8768 {
8769 /* This tells Xaw3d to use real colors instead of dithering for
8770 the shadows. */
8771 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8772 ++ac;
8773
8774 /* Specify the colors. */
8775 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8776 if (pixel != -1)
8777 {
8778 XtSetArg (av[ac], "topShadowPixel", pixel);
8779 ++ac;
8780 }
8781 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8782 if (pixel != -1)
8783 {
8784 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8785 ++ac;
8786 }
8787 }
8788
06a2c219
GM
8789 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8790 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8791
8792 {
8793 char *initial = "";
8794 char *val = initial;
8795 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8796 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8797 if (val == initial)
8798 { /* ARROW_SCROLL */
8799 xaw3d_arrow_scroll = True;
8800 /* Isn't that just a personal preference ? -sm */
8801 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8802 }
8803 }
06a2c219
GM
8804
8805 /* Define callbacks. */
ec18280f
SM
8806 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8807 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8808 (XtPointer) bar);
8809
8810 /* Realize the widget. Only after that is the X window created. */
8811 XtRealizeWidget (widget);
8812
ec18280f 8813#endif /* !USE_MOTIF */
06a2c219
GM
8814
8815 /* Install an action hook that let's us detect when the user
8816 finishes interacting with a scroll bar. */
8817 if (action_hook_id == 0)
8818 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8819
8820 /* Remember X window and widget in the scroll bar vector. */
8821 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8822 xwindow = XtWindow (widget);
8823 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8824
8825 UNBLOCK_INPUT;
8826}
8827
8828
8829/* Set the thumb size and position of scroll bar BAR. We are currently
8830 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8831
8832static void
8833x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8834 struct scroll_bar *bar;
8835 int portion, position, whole;
f451eb13 8836{
e83dc917
GM
8837 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8838 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8839 float top, shown;
f451eb13 8840
6f2a7a68
SM
8841 BLOCK_INPUT;
8842
8843#ifdef USE_MOTIF
8844
8845 /* We use an estimate of 30 chars per line rather than the real
8846 `portion' value. This has the disadvantage that the thumb size
8847 is not very representative, but it makes our life a lot easier.
8848 Otherwise, we have to constantly adjust the thumb size, which
8849 we can't always do quickly enough: while dragging, the size of
8850 the thumb might prevent the user from dragging the thumb all the
8851 way to the end. but Motif and some versions of Xaw3d don't allow
8852 updating the thumb size while dragging. Also, even if we can update
8853 its size, the update will often happen too late.
8854 If you don't believe it, check out revision 1.650 of xterm.c to see
8855 what hoops we were going through and the still poor behavior we got. */
8856 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
8857 /* When the thumb is at the bottom, position == whole.
8858 So we need to increase `whole' to make space for the thumb. */
8859 whole += portion;
8860
8861 if (whole <= 0)
06a2c219
GM
8862 top = 0, shown = 1;
8863 else
f451eb13 8864 {
06a2c219
GM
8865 top = (float) position / whole;
8866 shown = (float) portion / whole;
8867 }
f451eb13 8868
6f2a7a68
SM
8869 if (NILP (bar->dragging))
8870 {
8871 int size, value;
06a2c219 8872
6f2a7a68
SM
8873 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8874 is the scroll bar's maximum and MIN is the scroll bar's minimum
8875 value. */
8876 size = shown * XM_SB_RANGE;
8877 size = min (size, XM_SB_RANGE);
8878 size = max (size, 1);
06a2c219 8879
6f2a7a68
SM
8880 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8881 value = top * XM_SB_RANGE;
8882 value = min (value, XM_SB_MAX - size);
8883 value = max (value, XM_SB_MIN);
8884
06a2c219 8885 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 8886 }
ec18280f 8887#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
8888
8889 if (whole == 0)
8890 top = 0, shown = 1;
8891 else
8892 {
8893 top = (float) position / whole;
8894 shown = (float) portion / whole;
8895 }
8896
06a2c219 8897 {
ec18280f
SM
8898 float old_top, old_shown;
8899 Dimension height;
8900 XtVaGetValues (widget,
8901 XtNtopOfThumb, &old_top,
8902 XtNshown, &old_shown,
8903 XtNheight, &height,
8904 NULL);
8905
8906 /* Massage the top+shown values. */
8907 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8908 top = max (0, min (1, top));
8909 else
8910 top = old_top;
8911 /* Keep two pixels available for moving the thumb down. */
8912 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8913
8914 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8915 check that your system's configuration file contains a define
8916 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8917 if (top != old_top || shown != old_shown)
eb393530 8918 {
ec18280f 8919 if (NILP (bar->dragging))
eb393530 8920 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8921 else
8922 {
ec18280f
SM
8923#ifdef HAVE_XAW3D
8924 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8925 int scroll_mode = 0;
ec18280f
SM
8926
8927 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8928 if (xaw3d_arrow_scroll)
8929 {
8930 /* Xaw3d stupidly ignores resize requests while dragging
8931 so we have to make it believe it's not in dragging mode. */
8932 scroll_mode = sb->scrollbar.scroll_mode;
8933 if (scroll_mode == 2)
8934 sb->scrollbar.scroll_mode = 0;
8935 }
8936#endif
8937 /* Try to make the scrolling a tad smoother. */
8938 if (!xaw3d_pick_top)
8939 shown = min (shown, old_shown);
8940
8941 XawScrollbarSetThumb (widget, top, shown);
8942
8943#ifdef HAVE_XAW3D
8944 if (xaw3d_arrow_scroll && scroll_mode == 2)
8945 sb->scrollbar.scroll_mode = scroll_mode;
8946#endif
06a2c219 8947 }
06a2c219
GM
8948 }
8949 }
ec18280f 8950#endif /* !USE_MOTIF */
06a2c219
GM
8951
8952 UNBLOCK_INPUT;
f451eb13
JB
8953}
8954
06a2c219
GM
8955#endif /* USE_TOOLKIT_SCROLL_BARS */
8956
8957
8958\f
8959/************************************************************************
8960 Scroll bars, general
8961 ************************************************************************/
8962
8963/* Create a scroll bar and return the scroll bar vector for it. W is
8964 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 8965 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
8966 scroll bar. */
8967
ab648270 8968static struct scroll_bar *
06a2c219
GM
8969x_scroll_bar_create (w, top, left, width, height)
8970 struct window *w;
f451eb13
JB
8971 int top, left, width, height;
8972{
06a2c219 8973 struct frame *f = XFRAME (w->frame);
334208b7
RS
8974 struct scroll_bar *bar
8975 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8976
8977 BLOCK_INPUT;
8978
eccc05db 8979#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8980 x_create_toolkit_scroll_bar (f, bar);
8981#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8982 {
8983 XSetWindowAttributes a;
8984 unsigned long mask;
5c187dee 8985 Window window;
06a2c219
GM
8986
8987 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8988 if (a.background_pixel == -1)
8989 a.background_pixel = f->output_data.x->background_pixel;
8990
12ba150f 8991 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8992 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8993 | ExposureMask);
7a13e894 8994 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8995
dbc4e1c1 8996 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8997
06a2c219
GM
8998 /* Clear the area of W that will serve as a scroll bar. This is
8999 for the case that a window has been split horizontally. In
9000 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
9001 if (width > 0 && height > 0)
9002 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9003 left, top, width,
9004 window_box_height (w), False);
06a2c219
GM
9005
9006 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9007 /* Position and size of scroll bar. */
9008 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9009 top,
9010 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9011 height,
9012 /* Border width, depth, class, and visual. */
9013 0,
9014 CopyFromParent,
9015 CopyFromParent,
9016 CopyFromParent,
9017 /* Attributes. */
9018 mask, &a);
9019 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 9020 }
06a2c219 9021#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9022
06a2c219 9023 XSETWINDOW (bar->window, w);
e0c1aef2
KH
9024 XSETINT (bar->top, top);
9025 XSETINT (bar->left, left);
9026 XSETINT (bar->width, width);
9027 XSETINT (bar->height, height);
9028 XSETINT (bar->start, 0);
9029 XSETINT (bar->end, 0);
12ba150f 9030 bar->dragging = Qnil;
f451eb13
JB
9031
9032 /* Add bar to its frame's list of scroll bars. */
334208b7 9033 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 9034 bar->prev = Qnil;
334208b7 9035 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 9036 if (!NILP (bar->next))
e0c1aef2 9037 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 9038
06a2c219 9039 /* Map the window/widget. */
eccc05db 9040#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
9041 {
9042 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9043 XtConfigureWidget (scroll_bar,
9044 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9045 top,
9046 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9047 max (height, 1), 0);
9048 XtMapWidget (scroll_bar);
9049 }
06a2c219 9050#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 9051 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 9052#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9053
9054 UNBLOCK_INPUT;
12ba150f 9055 return bar;
f451eb13
JB
9056}
9057
06a2c219 9058
12ba150f 9059/* Draw BAR's handle in the proper position.
06a2c219 9060
12ba150f
JB
9061 If the handle is already drawn from START to END, don't bother
9062 redrawing it, unless REBUILD is non-zero; in that case, always
9063 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 9064 events.)
12ba150f
JB
9065
9066 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
9067 fit inside its rectangle, but if the user is dragging the scroll
9068 bar handle, we want to let them drag it down all the way, so that
9069 the bar's top is as far down as it goes; otherwise, there's no way
9070 to move to the very end of the buffer. */
9071
5c187dee
GM
9072#ifndef USE_TOOLKIT_SCROLL_BARS
9073
f451eb13 9074static void
ab648270
JB
9075x_scroll_bar_set_handle (bar, start, end, rebuild)
9076 struct scroll_bar *bar;
f451eb13 9077 int start, end;
12ba150f 9078 int rebuild;
f451eb13 9079{
12ba150f 9080 int dragging = ! NILP (bar->dragging);
ab648270 9081 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9082 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9083 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
9084
9085 /* If the display is already accurate, do nothing. */
9086 if (! rebuild
9087 && start == XINT (bar->start)
9088 && end == XINT (bar->end))
9089 return;
9090
f451eb13
JB
9091 BLOCK_INPUT;
9092
9093 {
d9cdbb3d
RS
9094 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9095 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9096 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
9097
9098 /* Make sure the values are reasonable, and try to preserve
9099 the distance between start and end. */
12ba150f
JB
9100 {
9101 int length = end - start;
9102
9103 if (start < 0)
9104 start = 0;
9105 else if (start > top_range)
9106 start = top_range;
9107 end = start + length;
9108
9109 if (end < start)
9110 end = start;
9111 else if (end > top_range && ! dragging)
9112 end = top_range;
9113 }
f451eb13 9114
ab648270 9115 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
9116 XSETINT (bar->start, start);
9117 XSETINT (bar->end, end);
f451eb13 9118
12ba150f
JB
9119 /* Clip the end position, just for display. */
9120 if (end > top_range)
9121 end = top_range;
f451eb13 9122
ab648270 9123 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
9124 below top positions, to make sure the handle is always at least
9125 that many pixels tall. */
ab648270 9126 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 9127
12ba150f
JB
9128 /* Draw the empty space above the handle. Note that we can't clear
9129 zero-height areas; that means "clear to end of window." */
9130 if (0 < start)
c5e6e06b
GM
9131 x_clear_area (FRAME_X_DISPLAY (f), w,
9132 /* x, y, width, height, and exposures. */
9133 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9134 VERTICAL_SCROLL_BAR_TOP_BORDER,
9135 inside_width, start,
9136 False);
f451eb13 9137
06a2c219
GM
9138 /* Change to proper foreground color if one is specified. */
9139 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9140 XSetForeground (FRAME_X_DISPLAY (f), gc,
9141 f->output_data.x->scroll_bar_foreground_pixel);
9142
12ba150f 9143 /* Draw the handle itself. */
334208b7 9144 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 9145 /* x, y, width, height */
ab648270
JB
9146 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9147 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 9148 inside_width, end - start);
f451eb13 9149
06a2c219
GM
9150 /* Restore the foreground color of the GC if we changed it above. */
9151 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9152 XSetForeground (FRAME_X_DISPLAY (f), gc,
9153 f->output_data.x->foreground_pixel);
f451eb13 9154
12ba150f
JB
9155 /* Draw the empty space below the handle. Note that we can't
9156 clear zero-height areas; that means "clear to end of window." */
9157 if (end < inside_height)
c5e6e06b
GM
9158 x_clear_area (FRAME_X_DISPLAY (f), w,
9159 /* x, y, width, height, and exposures. */
9160 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9161 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9162 inside_width, inside_height - end,
9163 False);
f451eb13 9164
f451eb13
JB
9165 }
9166
f451eb13
JB
9167 UNBLOCK_INPUT;
9168}
9169
5c187dee 9170#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 9171
06a2c219
GM
9172/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9173 nil. */
58769bee 9174
12ba150f 9175static void
ab648270
JB
9176x_scroll_bar_remove (bar)
9177 struct scroll_bar *bar;
12ba150f 9178{
e83dc917 9179 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
9180 BLOCK_INPUT;
9181
eccc05db 9182#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
9183 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9184#else
9185 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9186#endif
06a2c219 9187
ab648270
JB
9188 /* Disassociate this scroll bar from its window. */
9189 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
9190
9191 UNBLOCK_INPUT;
9192}
9193
06a2c219 9194
12ba150f
JB
9195/* Set the handle of the vertical scroll bar for WINDOW to indicate
9196 that we are displaying PORTION characters out of a total of WHOLE
ab648270 9197 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 9198 create one. */
06a2c219 9199
12ba150f 9200static void
06a2c219
GM
9201XTset_vertical_scroll_bar (w, portion, whole, position)
9202 struct window *w;
f451eb13
JB
9203 int portion, whole, position;
9204{
06a2c219 9205 struct frame *f = XFRAME (w->frame);
ab648270 9206 struct scroll_bar *bar;
3c6ede7b 9207 int top, height, left, sb_left, width, sb_width;
06a2c219 9208 int window_x, window_y, window_width, window_height;
06a2c219 9209
3c6ede7b 9210 /* Get window dimensions. */
06a2c219 9211 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
9212 top = window_y;
9213 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9214 height = window_height;
06a2c219 9215
3c6ede7b 9216 /* Compute the left edge of the scroll bar area. */
06a2c219 9217 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
9218 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9219 else
9220 left = XFASTINT (w->left);
9221 left *= CANON_X_UNIT (f);
9222 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9223
9224 /* Compute the width of the scroll bar which might be less than
9225 the width of the area reserved for the scroll bar. */
9226 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9227 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 9228 else
3c6ede7b 9229 sb_width = width;
12ba150f 9230
3c6ede7b
GM
9231 /* Compute the left edge of the scroll bar. */
9232#ifdef USE_TOOLKIT_SCROLL_BARS
9233 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9234 sb_left = left + width - sb_width - (width - sb_width) / 2;
9235 else
9236 sb_left = left + (width - sb_width) / 2;
9237#else
9238 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9239 sb_left = left + width - sb_width;
9240 else
9241 sb_left = left;
9242#endif
9243
ab648270 9244 /* Does the scroll bar exist yet? */
06a2c219 9245 if (NILP (w->vertical_scroll_bar))
3c6ede7b 9246 {
7b49b9d2 9247 if (width > 0 && height > 0)
b547b6e8
GM
9248 {
9249 BLOCK_INPUT;
9250 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9251 left, top, width, height, False);
9252 UNBLOCK_INPUT;
9253 }
9254
3c6ede7b
GM
9255 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9256 }
f451eb13 9257 else
12ba150f
JB
9258 {
9259 /* It may just need to be moved and resized. */
06a2c219
GM
9260 unsigned int mask = 0;
9261
9262 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9263
9264 BLOCK_INPUT;
9265
3c6ede7b 9266 if (sb_left != XINT (bar->left))
06a2c219 9267 mask |= CWX;
3c6ede7b 9268 if (top != XINT (bar->top))
06a2c219 9269 mask |= CWY;
3c6ede7b 9270 if (sb_width != XINT (bar->width))
06a2c219 9271 mask |= CWWidth;
3c6ede7b 9272 if (height != XINT (bar->height))
06a2c219
GM
9273 mask |= CWHeight;
9274
9275#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
9276
9277 /* Since toolkit scroll bars are smaller than the space reserved
9278 for them on the frame, we have to clear "under" them. */
7b49b9d2 9279 if (width > 0 && height > 0)
f964b4d7
GM
9280 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9281 left, top, width, height, False);
06a2c219
GM
9282
9283 /* Move/size the scroll bar widget. */
9284 if (mask)
e83dc917 9285 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
9286 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9287 top,
9288 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 9289 max (height, 1), 0);
06a2c219
GM
9290
9291#else /* not USE_TOOLKIT_SCROLL_BARS */
9292
357e7376
GM
9293 /* Clear areas not covered by the scroll bar because of
9294 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
9295 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9296 {
c5e6e06b
GM
9297 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9298 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9299 height, False);
9300 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9301 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9302 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9303 height, False);
e1f6572f 9304 }
357e7376
GM
9305
9306 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 9307 wide as the area reserved for it. This makes sure a
357e7376
GM
9308 previous mode line display is cleared after C-x 2 C-x 1, for
9309 example. */
9310 {
9311 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9312 int rest = area_width - sb_width;
38d2af0c
GM
9313 if (rest > 0 && height > 0)
9314 {
9315 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9316 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9317 left + area_width - rest, top,
9318 rest, height, False);
9319 else
9320 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9321 left, top, rest, height, False);
9322 }
357e7376 9323 }
06a2c219
GM
9324
9325 /* Move/size the scroll bar window. */
9326 if (mask)
9327 {
9328 XWindowChanges wc;
9329
3c6ede7b
GM
9330 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9331 wc.y = top;
9332 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9333 wc.height = height;
06a2c219
GM
9334 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9335 mask, &wc);
9336 }
9337
9338#endif /* not USE_TOOLKIT_SCROLL_BARS */
9339
9340 /* Remember new settings. */
3c6ede7b
GM
9341 XSETINT (bar->left, sb_left);
9342 XSETINT (bar->top, top);
9343 XSETINT (bar->width, sb_width);
9344 XSETINT (bar->height, height);
06a2c219
GM
9345
9346 UNBLOCK_INPUT;
12ba150f 9347 }
f451eb13 9348
eccc05db 9349#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9350 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9351#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 9352 /* Set the scroll bar's current state, unless we're currently being
f451eb13 9353 dragged. */
12ba150f 9354 if (NILP (bar->dragging))
f451eb13 9355 {
92857db0 9356 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 9357
12ba150f 9358 if (whole == 0)
ab648270 9359 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
9360 else
9361 {
43f868f5
JB
9362 int start = ((double) position * top_range) / whole;
9363 int end = ((double) (position + portion) * top_range) / whole;
ab648270 9364 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 9365 }
f451eb13 9366 }
06a2c219 9367#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9368
06a2c219 9369 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
9370}
9371
12ba150f 9372
f451eb13 9373/* The following three hooks are used when we're doing a thorough
ab648270 9374 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 9375 are going to be deleted, because keeping track of when windows go
12ba150f
JB
9376 away is a real pain - "Can you say set-window-configuration, boys
9377 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 9378 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 9379 from the fiery pit when we actually redisplay its window. */
f451eb13 9380
ab648270
JB
9381/* Arrange for all scroll bars on FRAME to be removed at the next call
9382 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
9383 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9384
58769bee 9385static void
ab648270 9386XTcondemn_scroll_bars (frame)
f451eb13
JB
9387 FRAME_PTR frame;
9388{
f9e24cb9
RS
9389 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9390 while (! NILP (FRAME_SCROLL_BARS (frame)))
9391 {
9392 Lisp_Object bar;
9393 bar = FRAME_SCROLL_BARS (frame);
9394 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9395 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9396 XSCROLL_BAR (bar)->prev = Qnil;
9397 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9398 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9399 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9400 }
f451eb13
JB
9401}
9402
fa2dfc30 9403
06a2c219 9404/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 9405 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 9406
f451eb13 9407static void
ab648270 9408XTredeem_scroll_bar (window)
12ba150f 9409 struct window *window;
f451eb13 9410{
ab648270 9411 struct scroll_bar *bar;
fa2dfc30 9412 struct frame *f;
12ba150f 9413
ab648270
JB
9414 /* We can't redeem this window's scroll bar if it doesn't have one. */
9415 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
9416 abort ();
9417
ab648270 9418 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
9419
9420 /* Unlink it from the condemned list. */
fa2dfc30
GM
9421 f = XFRAME (WINDOW_FRAME (window));
9422 if (NILP (bar->prev))
9423 {
9424 /* If the prev pointer is nil, it must be the first in one of
9425 the lists. */
9426 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9427 /* It's not condemned. Everything's fine. */
9428 return;
9429 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9430 window->vertical_scroll_bar))
9431 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9432 else
9433 /* If its prev pointer is nil, it must be at the front of
9434 one or the other! */
9435 abort ();
9436 }
9437 else
9438 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 9439
fa2dfc30
GM
9440 if (! NILP (bar->next))
9441 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 9442
fa2dfc30
GM
9443 bar->next = FRAME_SCROLL_BARS (f);
9444 bar->prev = Qnil;
9445 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9446 if (! NILP (bar->next))
9447 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
9448}
9449
ab648270
JB
9450/* Remove all scroll bars on FRAME that haven't been saved since the
9451 last call to `*condemn_scroll_bars_hook'. */
06a2c219 9452
f451eb13 9453static void
ab648270 9454XTjudge_scroll_bars (f)
12ba150f 9455 FRAME_PTR f;
f451eb13 9456{
12ba150f 9457 Lisp_Object bar, next;
f451eb13 9458
ab648270 9459 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
9460
9461 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
9462 more events on the hapless scroll bars. */
9463 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
9464
9465 for (; ! NILP (bar); bar = next)
f451eb13 9466 {
ab648270 9467 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 9468
ab648270 9469 x_scroll_bar_remove (b);
12ba150f
JB
9470
9471 next = b->next;
9472 b->next = b->prev = Qnil;
f451eb13 9473 }
12ba150f 9474
ab648270 9475 /* Now there should be no references to the condemned scroll bars,
12ba150f 9476 and they should get garbage-collected. */
f451eb13
JB
9477}
9478
9479
06a2c219
GM
9480/* Handle an Expose or GraphicsExpose event on a scroll bar. This
9481 is a no-op when using toolkit scroll bars.
ab648270
JB
9482
9483 This may be called from a signal handler, so we have to ignore GC
9484 mark bits. */
06a2c219 9485
f451eb13 9486static void
ab648270
JB
9487x_scroll_bar_expose (bar, event)
9488 struct scroll_bar *bar;
f451eb13
JB
9489 XEvent *event;
9490{
06a2c219
GM
9491#ifndef USE_TOOLKIT_SCROLL_BARS
9492
ab648270 9493 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9494 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9495 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 9496 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 9497
f451eb13
JB
9498 BLOCK_INPUT;
9499
ab648270 9500 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 9501
06a2c219 9502 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 9503 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
9504
9505 /* x, y, width, height */
d9cdbb3d 9506 0, 0,
3cbd2e0b 9507 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
9508 XINT (bar->height) - 1);
9509
f451eb13 9510 UNBLOCK_INPUT;
06a2c219
GM
9511
9512#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9513}
9514
ab648270 9515/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 9516 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
9517
9518 This may be called from a signal handler, so we have to ignore GC
9519 mark bits. */
06a2c219 9520
5c187dee
GM
9521#ifndef USE_TOOLKIT_SCROLL_BARS
9522
f451eb13 9523static void
ab648270
JB
9524x_scroll_bar_handle_click (bar, event, emacs_event)
9525 struct scroll_bar *bar;
f451eb13
JB
9526 XEvent *event;
9527 struct input_event *emacs_event;
9528{
0299d313 9529 if (! GC_WINDOWP (bar->window))
12ba150f
JB
9530 abort ();
9531
3b8f9651 9532 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 9533 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
9534 emacs_event->modifiers
9535 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9536 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9537 event->xbutton.state)
9538 | (event->type == ButtonRelease
9539 ? up_modifier
9540 : down_modifier));
12ba150f 9541 emacs_event->frame_or_window = bar->window;
0f8aabe9 9542 emacs_event->arg = Qnil;
f451eb13 9543 emacs_event->timestamp = event->xbutton.time;
12ba150f 9544 {
06a2c219 9545#if 0
d9cdbb3d 9546 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 9547 int internal_height
d9cdbb3d 9548 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9549#endif
0299d313 9550 int top_range
d9cdbb3d 9551 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 9552 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
9553
9554 if (y < 0) y = 0;
9555 if (y > top_range) y = top_range;
9556
9557 if (y < XINT (bar->start))
ab648270
JB
9558 emacs_event->part = scroll_bar_above_handle;
9559 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9560 emacs_event->part = scroll_bar_handle;
12ba150f 9561 else
ab648270 9562 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
9563
9564 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
9565 they want to drag it. Lisp code needs to be able to decide
9566 whether or not we're dragging. */
929787e1 9567#if 0
12ba150f
JB
9568 /* If the user has just clicked on the handle, record where they're
9569 holding it. */
9570 if (event->type == ButtonPress
ab648270 9571 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9572 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9573#endif
12ba150f
JB
9574
9575 /* If the user has released the handle, set it to its final position. */
9576 if (event->type == ButtonRelease
9577 && ! NILP (bar->dragging))
9578 {
9579 int new_start = y - XINT (bar->dragging);
9580 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9581
ab648270 9582 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9583 bar->dragging = Qnil;
9584 }
f451eb13 9585
5116f055
JB
9586 /* Same deal here as the other #if 0. */
9587#if 0
58769bee 9588 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9589 the handle. */
ab648270 9590 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9591 emacs_event->x = bar->start;
9592 else
e0c1aef2 9593 XSETINT (emacs_event->x, y);
5116f055 9594#else
e0c1aef2 9595 XSETINT (emacs_event->x, y);
5116f055 9596#endif
f451eb13 9597
e0c1aef2 9598 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9599 }
9600}
f451eb13 9601
ab648270
JB
9602/* Handle some mouse motion while someone is dragging the scroll bar.
9603
9604 This may be called from a signal handler, so we have to ignore GC
9605 mark bits. */
06a2c219 9606
f451eb13 9607static void
ab648270
JB
9608x_scroll_bar_note_movement (bar, event)
9609 struct scroll_bar *bar;
f451eb13
JB
9610 XEvent *event;
9611{
39d8bb4d
KH
9612 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9613
f451eb13
JB
9614 last_mouse_movement_time = event->xmotion.time;
9615
39d8bb4d 9616 f->mouse_moved = 1;
e0c1aef2 9617 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9618
9619 /* If we're dragging the bar, display it. */
ab648270 9620 if (! GC_NILP (bar->dragging))
f451eb13
JB
9621 {
9622 /* Where should the handle be now? */
12ba150f 9623 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9624
12ba150f 9625 if (new_start != XINT (bar->start))
f451eb13 9626 {
12ba150f 9627 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9628
ab648270 9629 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9630 }
9631 }
f451eb13
JB
9632}
9633
5c187dee
GM
9634#endif /* !USE_TOOLKIT_SCROLL_BARS */
9635
12ba150f 9636/* Return information to the user about the current position of the mouse
ab648270 9637 on the scroll bar. */
06a2c219 9638
12ba150f 9639static void
334208b7
RS
9640x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9641 FRAME_PTR *fp;
12ba150f 9642 Lisp_Object *bar_window;
ab648270 9643 enum scroll_bar_part *part;
12ba150f
JB
9644 Lisp_Object *x, *y;
9645 unsigned long *time;
9646{
ab648270 9647 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9648 Window w = SCROLL_BAR_X_WINDOW (bar);
9649 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9650 int win_x, win_y;
559cb2fb
JB
9651 Window dummy_window;
9652 int dummy_coord;
9653 unsigned int dummy_mask;
12ba150f 9654
cf7cb199
JB
9655 BLOCK_INPUT;
9656
ab648270 9657 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9658 report that. */
334208b7 9659 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9660
559cb2fb
JB
9661 /* Root, child, root x and root y. */
9662 &dummy_window, &dummy_window,
9663 &dummy_coord, &dummy_coord,
12ba150f 9664
559cb2fb
JB
9665 /* Position relative to scroll bar. */
9666 &win_x, &win_y,
12ba150f 9667
559cb2fb
JB
9668 /* Mouse buttons and modifier keys. */
9669 &dummy_mask))
7a13e894 9670 ;
559cb2fb
JB
9671 else
9672 {
06a2c219 9673#if 0
559cb2fb 9674 int inside_height
d9cdbb3d 9675 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9676#endif
559cb2fb 9677 int top_range
d9cdbb3d 9678 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9679
9680 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9681
9682 if (! NILP (bar->dragging))
9683 win_y -= XINT (bar->dragging);
9684
9685 if (win_y < 0)
9686 win_y = 0;
9687 if (win_y > top_range)
9688 win_y = top_range;
9689
334208b7 9690 *fp = f;
7a13e894 9691 *bar_window = bar->window;
559cb2fb
JB
9692
9693 if (! NILP (bar->dragging))
9694 *part = scroll_bar_handle;
9695 else if (win_y < XINT (bar->start))
9696 *part = scroll_bar_above_handle;
9697 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9698 *part = scroll_bar_handle;
9699 else
9700 *part = scroll_bar_below_handle;
12ba150f 9701
e0c1aef2
KH
9702 XSETINT (*x, win_y);
9703 XSETINT (*y, top_range);
12ba150f 9704
39d8bb4d 9705 f->mouse_moved = 0;
559cb2fb
JB
9706 last_mouse_scroll_bar = Qnil;
9707 }
12ba150f 9708
559cb2fb 9709 *time = last_mouse_movement_time;
cf7cb199 9710
cf7cb199 9711 UNBLOCK_INPUT;
12ba150f
JB
9712}
9713
f451eb13 9714
dbc4e1c1 9715/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9716 background colors, and the scroll bars may need to be redrawn.
9717 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9718 redraw them. */
9719
dfcf069d 9720void
ab648270 9721x_scroll_bar_clear (f)
dbc4e1c1
JB
9722 FRAME_PTR f;
9723{
06a2c219 9724#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9725 Lisp_Object bar;
9726
b80c363e
RS
9727 /* We can have scroll bars even if this is 0,
9728 if we just turned off scroll bar mode.
9729 But in that case we should not clear them. */
9730 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9731 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9732 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9733 XClearArea (FRAME_X_DISPLAY (f),
9734 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9735 0, 0, 0, 0, True);
06a2c219 9736#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9737}
9738
06a2c219 9739/* This processes Expose events from the menu-bar specific X event
19126e11 9740 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9741 when handling menu-bar or pop-up items. */
3afe33e7 9742
06a2c219 9743int
3afe33e7
RS
9744process_expose_from_menu (event)
9745 XEvent event;
9746{
9747 FRAME_PTR f;
19126e11 9748 struct x_display_info *dpyinfo;
06a2c219 9749 int frame_exposed_p = 0;
3afe33e7 9750
f94397b5
KH
9751 BLOCK_INPUT;
9752
19126e11
KH
9753 dpyinfo = x_display_info_for_display (event.xexpose.display);
9754 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9755 if (f)
9756 {
9757 if (f->async_visible == 0)
9758 {
9759 f->async_visible = 1;
9760 f->async_iconified = 0;
06c488fd 9761 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9762 SET_FRAME_GARBAGED (f);
9763 }
9764 else
9765 {
06a2c219
GM
9766 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9767 event.xexpose.x, event.xexpose.y,
9768 event.xexpose.width, event.xexpose.height);
9769 frame_exposed_p = 1;
3afe33e7
RS
9770 }
9771 }
9772 else
9773 {
9774 struct scroll_bar *bar
9775 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9776
3afe33e7
RS
9777 if (bar)
9778 x_scroll_bar_expose (bar, &event);
9779 }
f94397b5
KH
9780
9781 UNBLOCK_INPUT;
06a2c219 9782 return frame_exposed_p;
3afe33e7 9783}
09756a85
RS
9784\f
9785/* Define a queue to save up SelectionRequest events for later handling. */
9786
9787struct selection_event_queue
9788 {
9789 XEvent event;
9790 struct selection_event_queue *next;
9791 };
9792
9793static struct selection_event_queue *queue;
9794
9795/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9796
09756a85
RS
9797static int x_queue_selection_requests;
9798
9799/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9800
09756a85 9801static void
334208b7
RS
9802x_queue_event (f, event)
9803 FRAME_PTR f;
09756a85
RS
9804 XEvent *event;
9805{
9806 struct selection_event_queue *queue_tmp
06a2c219 9807 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9808
58769bee 9809 if (queue_tmp != NULL)
09756a85
RS
9810 {
9811 queue_tmp->event = *event;
9812 queue_tmp->next = queue;
9813 queue = queue_tmp;
9814 }
9815}
9816
9817/* Take all the queued events and put them back
9818 so that they get processed afresh. */
9819
9820static void
db3906fd
RS
9821x_unqueue_events (display)
9822 Display *display;
09756a85 9823{
58769bee 9824 while (queue != NULL)
09756a85
RS
9825 {
9826 struct selection_event_queue *queue_tmp = queue;
db3906fd 9827 XPutBackEvent (display, &queue_tmp->event);
09756a85 9828 queue = queue_tmp->next;
06a2c219 9829 xfree ((char *)queue_tmp);
09756a85
RS
9830 }
9831}
9832
9833/* Start queuing SelectionRequest events. */
9834
9835void
db3906fd
RS
9836x_start_queuing_selection_requests (display)
9837 Display *display;
09756a85
RS
9838{
9839 x_queue_selection_requests++;
9840}
9841
9842/* Stop queuing SelectionRequest events. */
9843
9844void
db3906fd
RS
9845x_stop_queuing_selection_requests (display)
9846 Display *display;
09756a85
RS
9847{
9848 x_queue_selection_requests--;
db3906fd 9849 x_unqueue_events (display);
09756a85 9850}
f451eb13
JB
9851\f
9852/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9853
042c33d3 9854#if 0
06a2c219 9855/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9856 but we have to put it out here, since static variables within functions
9857 sometimes don't work. */
06a2c219 9858
dc6f92b8 9859static Time enter_timestamp;
042c33d3 9860#endif
dc6f92b8 9861
11edeb03 9862/* This holds the state XLookupString needs to implement dead keys
58769bee 9863 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9864 says that a portable program can't use this, but Stephen Gildea assures
9865 me that letting the compiler initialize it to zeros will work okay.
9866
9867 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 9868 given for enter_timestamp, above. */
06a2c219 9869
11edeb03
JB
9870static XComposeStatus compose_status;
9871
10e6549c
RS
9872/* Record the last 100 characters stored
9873 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9874
2224b905
RS
9875static int temp_index;
9876static short temp_buffer[100];
10e6549c 9877
7a13e894
RS
9878/* Set this to nonzero to fake an "X I/O error"
9879 on a particular display. */
06a2c219 9880
7a13e894
RS
9881struct x_display_info *XTread_socket_fake_io_error;
9882
2224b905
RS
9883/* When we find no input here, we occasionally do a no-op command
9884 to verify that the X server is still running and we can still talk with it.
9885 We try all the open displays, one by one.
9886 This variable is used for cycling thru the displays. */
06a2c219 9887
2224b905
RS
9888static struct x_display_info *next_noop_dpyinfo;
9889
06a2c219
GM
9890#define SET_SAVED_MENU_EVENT(size) \
9891 do \
9892 { \
9893 if (f->output_data.x->saved_menu_event == 0) \
9894 f->output_data.x->saved_menu_event \
9895 = (XEvent *) xmalloc (sizeof (XEvent)); \
9896 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9897 if (numchars >= 1) \
9898 { \
3b8f9651 9899 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
06a2c219 9900 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9901 bufp->arg = Qnil; \
06a2c219
GM
9902 bufp++; \
9903 count++; \
9904 numchars--; \
9905 } \
9906 } \
9907 while (0)
9908
8805890a 9909#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9910#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9911
dc6f92b8
JB
9912/* Read events coming from the X server.
9913 This routine is called by the SIGIO handler.
9914 We return as soon as there are no more events to be read.
9915
9916 Events representing keys are stored in buffer BUFP,
9917 which can hold up to NUMCHARS characters.
9918 We return the number of characters stored into the buffer,
9919 thus pretending to be `read'.
9920
dc6f92b8
JB
9921 EXPECTED is nonzero if the caller knows input is available. */
9922
6f2a7a68 9923static int
f66868ba 9924XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9925 register int sd;
8805890a
KH
9926 /* register */ struct input_event *bufp;
9927 /* register */ int numchars;
dc6f92b8
JB
9928 int expected;
9929{
9930 int count = 0;
9931 int nbytes = 0;
dc6f92b8 9932 XEvent event;
f676886a 9933 struct frame *f;
66f55a9d 9934 int event_found = 0;
334208b7 9935 struct x_display_info *dpyinfo;
379b5ac0 9936 struct coding_system coding;
dc6f92b8 9937
9ac0d9e0 9938 if (interrupt_input_blocked)
dc6f92b8 9939 {
9ac0d9e0 9940 interrupt_input_pending = 1;
dc6f92b8
JB
9941 return -1;
9942 }
9943
9ac0d9e0 9944 interrupt_input_pending = 0;
dc6f92b8 9945 BLOCK_INPUT;
c0a04927
RS
9946
9947 /* So people can tell when we have read the available input. */
9948 input_signal_count++;
9949
dc6f92b8 9950 if (numchars <= 0)
06a2c219 9951 abort (); /* Don't think this happens. */
dc6f92b8 9952
bde5503b
GM
9953 ++handling_signal;
9954
7a13e894
RS
9955 /* Find the display we are supposed to read input for.
9956 It's the one communicating on descriptor SD. */
9957 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9958 {
9959#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9960#ifdef FIOSNBIO
7a13e894
RS
9961 /* If available, Xlib uses FIOSNBIO to make the socket
9962 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9963 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9964 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9965 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9966#endif /* ! defined (FIOSNBIO) */
7a13e894 9967#endif
dc6f92b8 9968
7a13e894
RS
9969#if 0 /* This code can't be made to work, with multiple displays,
9970 and appears not to be used on any system any more.
9971 Also keyboard.c doesn't turn O_NDELAY on and off
9972 for X connections. */
dc6f92b8
JB
9973#ifndef SIGIO
9974#ifndef HAVE_SELECT
7a13e894
RS
9975 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9976 {
9977 extern int read_alarm_should_throw;
9978 read_alarm_should_throw = 1;
9979 XPeekEvent (dpyinfo->display, &event);
9980 read_alarm_should_throw = 0;
9981 }
c118dd06
JB
9982#endif /* HAVE_SELECT */
9983#endif /* SIGIO */
7a13e894 9984#endif
dc6f92b8 9985
7a13e894
RS
9986 /* For debugging, this gives a way to fake an I/O error. */
9987 if (dpyinfo == XTread_socket_fake_io_error)
9988 {
9989 XTread_socket_fake_io_error = 0;
9990 x_io_error_quitter (dpyinfo->display);
9991 }
dc6f92b8 9992
5f30b957
JD
9993#ifdef HAVE_X_SM
9994 BLOCK_INPUT;
9995 count += x_session_check_input (bufp, &numchars);
9996 UNBLOCK_INPUT;
9997#endif
9998
06a2c219 9999 while (XPending (dpyinfo->display))
dc6f92b8 10000 {
7a13e894 10001 XNextEvent (dpyinfo->display, &event);
06a2c219 10002
531483fb 10003#ifdef HAVE_X_I18N
d1bc4182 10004 {
f2be1146
GM
10005 /* Filter events for the current X input method.
10006 XFilterEvent returns non-zero if the input method has
10007 consumed the event. We pass the frame's X window to
10008 XFilterEvent because that's the one for which the IC
10009 was created. */
f5d11644
GM
10010 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10011 event.xclient.window);
10012 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
10013 break;
10014 }
0cd6403b 10015#endif
7a13e894
RS
10016 event_found = 1;
10017
10018 switch (event.type)
10019 {
10020 case ClientMessage:
c047688c 10021 {
7a13e894
RS
10022 if (event.xclient.message_type
10023 == dpyinfo->Xatom_wm_protocols
10024 && event.xclient.format == 32)
c047688c 10025 {
7a13e894
RS
10026 if (event.xclient.data.l[0]
10027 == dpyinfo->Xatom_wm_take_focus)
c047688c 10028 {
8c1a6a84
RS
10029 /* Use x_any_window_to_frame because this
10030 could be the shell widget window
10031 if the frame has no title bar. */
10032 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
10033#ifdef HAVE_X_I18N
10034 /* Not quite sure this is needed -pd */
8c1a6a84 10035 if (f && FRAME_XIC (f))
6c183ba5
RS
10036 XSetICFocus (FRAME_XIC (f));
10037#endif
f1da8f06
GM
10038#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10039 instructs the WM to set the input focus automatically for
10040 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10041 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10042 it has set the focus. So, XSetInputFocus below is not
10043 needed.
10044
10045 The call to XSetInputFocus below has also caused trouble. In
10046 cases where the XSetInputFocus done by the WM and the one
10047 below are temporally close (on a fast machine), the call
10048 below can generate additional FocusIn events which confuse
10049 Emacs. */
10050
bf7253f4
RS
10051 /* Since we set WM_TAKE_FOCUS, we must call
10052 XSetInputFocus explicitly. But not if f is null,
10053 since that might be an event for a deleted frame. */
7a13e894 10054 if (f)
bf7253f4
RS
10055 {
10056 Display *d = event.xclient.display;
10057 /* Catch and ignore errors, in case window has been
10058 iconified by a window manager such as GWM. */
10059 int count = x_catch_errors (d);
10060 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
10061 /* The ICCCM says this is
10062 the only valid choice. */
10063 RevertToParent,
bf7253f4
RS
10064 event.xclient.data.l[1]);
10065 /* This is needed to detect the error
10066 if there is an error. */
10067 XSync (d, False);
10068 x_uncatch_errors (d, count);
10069 }
7a13e894 10070 /* Not certain about handling scroll bars here */
f1da8f06 10071#endif /* 0 */
c047688c 10072 }
7a13e894
RS
10073 else if (event.xclient.data.l[0]
10074 == dpyinfo->Xatom_wm_save_yourself)
10075 {
10076 /* Save state modify the WM_COMMAND property to
06a2c219 10077 something which can reinstate us. This notifies
7a13e894
RS
10078 the session manager, who's looking for such a
10079 PropertyNotify. Can restart processing when
06a2c219 10080 a keyboard or mouse event arrives. */
5f30b957
JD
10081 /* If we have a session manager, don't set this.
10082 KDE will then start two Emacsen, one for the
10083 session manager and one for this. */
10084 if (numchars > 0
10085#ifdef HAVE_X_SM
10086 && ! x_session_have_connection ()
10087#endif
10088 )
7a13e894 10089 {
19126e11
KH
10090 f = x_top_window_to_frame (dpyinfo,
10091 event.xclient.window);
7a13e894
RS
10092 /* This is just so we only give real data once
10093 for a single Emacs process. */
b86bd3dd 10094 if (f == SELECTED_FRAME ())
7a13e894
RS
10095 XSetCommand (FRAME_X_DISPLAY (f),
10096 event.xclient.window,
10097 initial_argv, initial_argc);
f000f5c5 10098 else if (f)
7a13e894
RS
10099 XSetCommand (FRAME_X_DISPLAY (f),
10100 event.xclient.window,
10101 0, 0);
10102 }
10103 }
10104 else if (event.xclient.data.l[0]
10105 == dpyinfo->Xatom_wm_delete_window)
1fb20991 10106 {
19126e11
KH
10107 struct frame *f
10108 = x_any_window_to_frame (dpyinfo,
10109 event.xclient.window);
1fb20991 10110
7a13e894
RS
10111 if (f)
10112 {
10113 if (numchars == 0)
10114 abort ();
1fb20991 10115
3b8f9651 10116 bufp->kind = DELETE_WINDOW_EVENT;
7a13e894 10117 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10118 bufp->arg = Qnil;
7a13e894
RS
10119 bufp++;
10120
10121 count += 1;
10122 numchars -= 1;
10123 }
1fb20991 10124 }
c047688c 10125 }
7a13e894
RS
10126 else if (event.xclient.message_type
10127 == dpyinfo->Xatom_wm_configure_denied)
10128 {
10129 }
10130 else if (event.xclient.message_type
10131 == dpyinfo->Xatom_wm_window_moved)
10132 {
10133 int new_x, new_y;
19126e11
KH
10134 struct frame *f
10135 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 10136
7a13e894
RS
10137 new_x = event.xclient.data.s[0];
10138 new_y = event.xclient.data.s[1];
1fb20991 10139
7a13e894
RS
10140 if (f)
10141 {
7556890b
RS
10142 f->output_data.x->left_pos = new_x;
10143 f->output_data.x->top_pos = new_y;
7a13e894 10144 }
1fb20991 10145 }
0fdff6bb 10146#ifdef HACK_EDITRES
7a13e894
RS
10147 else if (event.xclient.message_type
10148 == dpyinfo->Xatom_editres)
10149 {
19126e11
KH
10150 struct frame *f
10151 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 10152 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 10153 &event, NULL);
7a13e894 10154 }
0fdff6bb 10155#endif /* HACK_EDITRES */
06a2c219
GM
10156 else if ((event.xclient.message_type
10157 == dpyinfo->Xatom_DONE)
10158 || (event.xclient.message_type
10159 == dpyinfo->Xatom_PAGE))
10160 {
10161 /* Ghostview job completed. Kill it. We could
10162 reply with "Next" if we received "Page", but we
10163 currently never do because we are interested in
10164 images, only, which should have 1 page. */
06a2c219
GM
10165 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10166 struct frame *f
10167 = x_window_to_frame (dpyinfo, event.xclient.window);
10168 x_kill_gs_process (pixmap, f);
10169 expose_frame (f, 0, 0, 0, 0);
10170 }
10171#ifdef USE_TOOLKIT_SCROLL_BARS
10172 /* Scroll bar callbacks send a ClientMessage from which
10173 we construct an input_event. */
10174 else if (event.xclient.message_type
10175 == dpyinfo->Xatom_Scrollbar)
10176 {
10177 x_scroll_bar_to_input_event (&event, bufp);
10178 ++bufp, ++count, --numchars;
10179 goto out;
10180 }
10181#endif /* USE_TOOLKIT_SCROLL_BARS */
10182 else
10183 goto OTHER;
7a13e894
RS
10184 }
10185 break;
dc6f92b8 10186
7a13e894 10187 case SelectionNotify:
3afe33e7 10188#ifdef USE_X_TOOLKIT
19126e11 10189 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 10190 goto OTHER;
3afe33e7 10191#endif /* not USE_X_TOOLKIT */
dfcf069d 10192 x_handle_selection_notify (&event.xselection);
7a13e894 10193 break;
d56a553a 10194
06a2c219 10195 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 10196#ifdef USE_X_TOOLKIT
19126e11 10197 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 10198 goto OTHER;
3afe33e7 10199#endif /* USE_X_TOOLKIT */
7a13e894
RS
10200 {
10201 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 10202
7a13e894
RS
10203 if (numchars == 0)
10204 abort ();
d56a553a 10205
3b8f9651 10206 bufp->kind = SELECTION_CLEAR_EVENT;
7a13e894
RS
10207 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10208 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10209 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10210 bufp->frame_or_window = Qnil;
0f8aabe9 10211 bufp->arg = Qnil;
7a13e894 10212 bufp++;
d56a553a 10213
7a13e894
RS
10214 count += 1;
10215 numchars -= 1;
10216 }
10217 break;
dc6f92b8 10218
06a2c219 10219 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 10220#ifdef USE_X_TOOLKIT
19126e11 10221 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 10222 goto OTHER;
3afe33e7 10223#endif /* USE_X_TOOLKIT */
7a13e894 10224 if (x_queue_selection_requests)
19126e11 10225 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
10226 &event);
10227 else
10228 {
1d2b2268
GM
10229 XSelectionRequestEvent *eventp
10230 = (XSelectionRequestEvent *) &event;
dc6f92b8 10231
7a13e894
RS
10232 if (numchars == 0)
10233 abort ();
10234
3b8f9651 10235 bufp->kind = SELECTION_REQUEST_EVENT;
7a13e894
RS
10236 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10237 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10238 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10239 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10240 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10241 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10242 bufp->frame_or_window = Qnil;
0f8aabe9 10243 bufp->arg = Qnil;
7a13e894
RS
10244 bufp++;
10245
10246 count += 1;
10247 numchars -= 1;
10248 }
10249 break;
10250
10251 case PropertyNotify:
1d2b2268
GM
10252#if 0 /* This is plain wrong. In the case that we are waiting for a
10253 PropertyNotify used as an ACK in incremental selection
10254 transfer, the property will be on the receiver's window. */
10255#if defined USE_X_TOOLKIT
19126e11 10256 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 10257 goto OTHER;
1d2b2268
GM
10258#endif
10259#endif
dfcf069d 10260 x_handle_property_notify (&event.xproperty);
1d2b2268 10261 goto OTHER;
dc6f92b8 10262
7a13e894 10263 case ReparentNotify:
19126e11 10264 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
10265 if (f)
10266 {
10267 int x, y;
7556890b 10268 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 10269 x_real_positions (f, &x, &y);
7556890b
RS
10270 f->output_data.x->left_pos = x;
10271 f->output_data.x->top_pos = y;
7a13e894
RS
10272 }
10273 break;
3bd330d4 10274
7a13e894 10275 case Expose:
19126e11 10276 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 10277 if (f)
dc6f92b8 10278 {
d0fa3e56
EZ
10279 x_check_fullscreen (f);
10280
7a13e894
RS
10281 if (f->async_visible == 0)
10282 {
10283 f->async_visible = 1;
10284 f->async_iconified = 0;
06c488fd 10285 f->output_data.x->has_been_visible = 1;
7a13e894
RS
10286 SET_FRAME_GARBAGED (f);
10287 }
10288 else
06a2c219
GM
10289 expose_frame (x_window_to_frame (dpyinfo,
10290 event.xexpose.window),
10291 event.xexpose.x, event.xexpose.y,
10292 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
10293 }
10294 else
7a13e894 10295 {
12949a7f
EZ
10296#ifndef USE_TOOLKIT_SCROLL_BARS
10297 struct scroll_bar *bar;
10298#endif
01f67d2c 10299#if defined USE_LUCID
c95fc5f1
GM
10300 /* Submenus of the Lucid menu bar aren't widgets
10301 themselves, so there's no way to dispatch events
10302 to them. Recognize this case separately. */
10303 {
10304 Widget widget
10305 = x_window_to_menu_bar (event.xexpose.window);
10306 if (widget)
10307 xlwmenu_redisplay (widget);
10308 }
01f67d2c
PJ
10309#endif /* USE_LUCID */
10310
06a2c219
GM
10311#ifdef USE_TOOLKIT_SCROLL_BARS
10312 /* Dispatch event to the widget. */
10313 goto OTHER;
10314#else /* not USE_TOOLKIT_SCROLL_BARS */
12949a7f 10315 bar = x_window_to_scroll_bar (event.xexpose.window);
58769bee 10316
7a13e894
RS
10317 if (bar)
10318 x_scroll_bar_expose (bar, &event);
3afe33e7 10319#ifdef USE_X_TOOLKIT
7a13e894
RS
10320 else
10321 goto OTHER;
3afe33e7 10322#endif /* USE_X_TOOLKIT */
06a2c219 10323#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10324 }
10325 break;
dc6f92b8 10326
7a13e894 10327 case GraphicsExpose: /* This occurs when an XCopyArea's
d624284c
PJ
10328 source area was obscured or not
10329 available. */
19126e11 10330 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
10331 if (f)
10332 {
06a2c219
GM
10333 expose_frame (f,
10334 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10335 event.xgraphicsexpose.width,
10336 event.xgraphicsexpose.height);
7a13e894 10337 }
3afe33e7 10338#ifdef USE_X_TOOLKIT
7a13e894
RS
10339 else
10340 goto OTHER;
3afe33e7 10341#endif /* USE_X_TOOLKIT */
7a13e894 10342 break;
dc6f92b8 10343
7a13e894 10344 case NoExpose: /* This occurs when an XCopyArea's
06a2c219 10345 source area was completely
d624284c 10346 available. */
7a13e894 10347 break;
dc6f92b8 10348
7a13e894 10349 case UnmapNotify:
06a2c219
GM
10350 /* Redo the mouse-highlight after the tooltip has gone. */
10351 if (event.xmap.window == tip_window)
10352 {
10353 tip_window = 0;
10354 redo_mouse_highlight ();
10355 }
10356
91ea2a7a 10357 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894 10358 if (f) /* F may no longer exist if
d624284c 10359 the frame was deleted. */
7a13e894
RS
10360 {
10361 /* While a frame is unmapped, display generation is
10362 disabled; you don't want to spend time updating a
10363 display that won't ever be seen. */
10364 f->async_visible = 0;
10365 /* We can't distinguish, from the event, whether the window
10366 has become iconified or invisible. So assume, if it
10367 was previously visible, than now it is iconified.
1aa6072f
RS
10368 But x_make_frame_invisible clears both
10369 the visible flag and the iconified flag;
10370 and that way, we know the window is not iconified now. */
7a13e894 10371 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
10372 {
10373 f->async_iconified = 1;
bddd097c 10374
3b8f9651 10375 bufp->kind = ICONIFY_EVENT;
1aa6072f 10376 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10377 bufp->arg = Qnil;
1aa6072f
RS
10378 bufp++;
10379 count++;
10380 numchars--;
10381 }
7a13e894 10382 }
7a13e894 10383 goto OTHER;
dc6f92b8 10384
7a13e894 10385 case MapNotify:
06a2c219
GM
10386 if (event.xmap.window == tip_window)
10387 /* The tooltip has been drawn already. Avoid
10388 the SET_FRAME_GARBAGED below. */
10389 goto OTHER;
10390
10391 /* We use x_top_window_to_frame because map events can
10392 come for sub-windows and they don't mean that the
10393 frame is visible. */
19126e11 10394 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
10395 if (f)
10396 {
10397 f->async_visible = 1;
10398 f->async_iconified = 0;
06c488fd 10399 f->output_data.x->has_been_visible = 1;
dc6f92b8 10400
7a13e894
RS
10401 /* wait_reading_process_input will notice this and update
10402 the frame's display structures. */
10403 SET_FRAME_GARBAGED (f);
bddd097c 10404
d806e720
RS
10405 if (f->iconified)
10406 {
3b8f9651 10407 bufp->kind = DEICONIFY_EVENT;
d806e720 10408 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10409 bufp->arg = Qnil;
d806e720
RS
10410 bufp++;
10411 count++;
10412 numchars--;
10413 }
e73ec6fa 10414 else if (! NILP (Vframe_list)
8e713be6 10415 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
10416 /* Force a redisplay sooner or later
10417 to update the frame titles
10418 in case this is the second frame. */
10419 record_asynch_buffer_change ();
7a13e894 10420 }
7a13e894 10421 goto OTHER;
dc6f92b8 10422
7a13e894 10423 case KeyPress:
2a793c0c
PJ
10424
10425 /* Dispatch KeyPress events when in menu. */
10426 if (popup_activated_flag)
10427 goto OTHER;
10428
19126e11 10429 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 10430
66301061
KS
10431 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10432 {
10433 dpyinfo->mouse_face_hidden = 1;
10434 clear_mouse_face (dpyinfo);
10435 }
10436
eccc05db 10437#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
10438 if (f == 0)
10439 {
2564ea1b
GM
10440 /* Scroll bars consume key events, but we want
10441 the keys to go to the scroll bar's frame. */
06a2c219
GM
10442 Widget widget = XtWindowToWidget (dpyinfo->display,
10443 event.xkey.window);
10444 if (widget && XmIsScrollBar (widget))
10445 {
10446 widget = XtParent (widget);
10447 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10448 }
10449 }
eccc05db 10450#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 10451
7a13e894
RS
10452 if (f != 0)
10453 {
10454 KeySym keysym, orig_keysym;
379b5ac0
KH
10455 /* al%imercury@uunet.uu.net says that making this 81
10456 instead of 80 fixed a bug whereby meta chars made
10457 his Emacs hang.
10458
10459 It seems that some version of XmbLookupString has
10460 a bug of not returning XBufferOverflow in
10461 status_return even if the input is too long to
10462 fit in 81 bytes. So, we must prepare sufficient
10463 bytes for copy_buffer. 513 bytes (256 chars for
6c4a22e6 10464 two-byte character set) seems to be a fairly good
379b5ac0
KH
10465 approximation. -- 2000.8.10 handa@etl.go.jp */
10466 unsigned char copy_buffer[513];
10467 unsigned char *copy_bufptr = copy_buffer;
10468 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 10469 int modifiers;
c997eae5 10470 Lisp_Object coding_system = Qlatin_1;
64bb1782 10471
7a13e894
RS
10472 event.xkey.state
10473 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10474 extra_keyboard_modifiers);
10475 modifiers = event.xkey.state;
3a2712f9 10476
7a13e894 10477 /* This will have to go some day... */
752a043f 10478
7a13e894
RS
10479 /* make_lispy_event turns chars into control chars.
10480 Don't do it here because XLookupString is too eager. */
10481 event.xkey.state &= ~ControlMask;
5d46f928
RS
10482 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10483 | dpyinfo->super_mod_mask
10484 | dpyinfo->hyper_mod_mask
10485 | dpyinfo->alt_mod_mask);
10486
1cf4a0d1
RS
10487 /* In case Meta is ComposeCharacter,
10488 clear its status. According to Markus Ehrnsperger
10489 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10490 this enables ComposeCharacter to work whether or
10491 not it is combined with Meta. */
10492 if (modifiers & dpyinfo->meta_mod_mask)
10493 bzero (&compose_status, sizeof (compose_status));
10494
6c183ba5
RS
10495#ifdef HAVE_X_I18N
10496 if (FRAME_XIC (f))
10497 {
f5d11644
GM
10498 Status status_return;
10499
c997eae5 10500 coding_system = Vlocale_coding_system;
6c183ba5 10501 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
10502 &event.xkey, copy_bufptr,
10503 copy_bufsiz, &keysym,
6c183ba5 10504 &status_return);
f5d11644
GM
10505 if (status_return == XBufferOverflow)
10506 {
10507 copy_bufsiz = nbytes + 1;
10508 copy_bufptr = (char *) alloca (copy_bufsiz);
10509 nbytes = XmbLookupString (FRAME_XIC (f),
10510 &event.xkey, copy_bufptr,
10511 copy_bufsiz, &keysym,
10512 &status_return);
10513 }
96035dca
SM
10514/* Xutf8LookupString is a new but already deprecated interface. -stef */
10515#if 0 && defined X_HAVE_UTF8_STRING
c997eae5
SM
10516 else if (status_return == XLookupKeySym)
10517 { /* Try again but with utf-8. */
10518 coding_system = Qutf_8;
10519 nbytes = Xutf8LookupString (FRAME_XIC (f),
10520 &event.xkey, copy_bufptr,
10521 copy_bufsiz, &keysym,
10522 &status_return);
10523 if (status_return == XBufferOverflow)
10524 {
10525 copy_bufsiz = nbytes + 1;
10526 copy_bufptr = (char *) alloca (copy_bufsiz);
10527 nbytes = Xutf8LookupString (FRAME_XIC (f),
10528 &event.xkey,
10529 copy_bufptr,
10530 copy_bufsiz, &keysym,
10531 &status_return);
10532 }
10533 }
10534#endif
f5d11644 10535
1decb680
PE
10536 if (status_return == XLookupNone)
10537 break;
10538 else if (status_return == XLookupChars)
fdd9d55e
GM
10539 {
10540 keysym = NoSymbol;
10541 modifiers = 0;
10542 }
1decb680
PE
10543 else if (status_return != XLookupKeySym
10544 && status_return != XLookupBoth)
10545 abort ();
6c183ba5
RS
10546 }
10547 else
379b5ac0
KH
10548 nbytes = XLookupString (&event.xkey, copy_bufptr,
10549 copy_bufsiz, &keysym,
10550 &compose_status);
6c183ba5 10551#else
379b5ac0
KH
10552 nbytes = XLookupString (&event.xkey, copy_bufptr,
10553 copy_bufsiz, &keysym,
10554 &compose_status);
6c183ba5 10555#endif
dc6f92b8 10556
7a13e894 10557 orig_keysym = keysym;
55123275 10558
7a13e894
RS
10559 if (numchars > 1)
10560 {
10561 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10562 || keysym == XK_Delete
1097aea0 10563#ifdef XK_ISO_Left_Tab
441affdb 10564 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 10565#endif
852bff8f 10566 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
10567 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10568 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 10569#ifdef HPUX
7a13e894
RS
10570 /* This recognizes the "extended function keys".
10571 It seems there's no cleaner way.
10572 Test IsModifierKey to avoid handling mode_switch
10573 incorrectly. */
10574 || ((unsigned) (keysym) >= XK_Select
10575 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
10576#endif
10577#ifdef XK_dead_circumflex
7a13e894 10578 || orig_keysym == XK_dead_circumflex
69388238
RS
10579#endif
10580#ifdef XK_dead_grave
7a13e894 10581 || orig_keysym == XK_dead_grave
69388238
RS
10582#endif
10583#ifdef XK_dead_tilde
7a13e894 10584 || orig_keysym == XK_dead_tilde
69388238
RS
10585#endif
10586#ifdef XK_dead_diaeresis
7a13e894 10587 || orig_keysym == XK_dead_diaeresis
69388238
RS
10588#endif
10589#ifdef XK_dead_macron
7a13e894 10590 || orig_keysym == XK_dead_macron
69388238
RS
10591#endif
10592#ifdef XK_dead_degree
7a13e894 10593 || orig_keysym == XK_dead_degree
69388238
RS
10594#endif
10595#ifdef XK_dead_acute
7a13e894 10596 || orig_keysym == XK_dead_acute
69388238
RS
10597#endif
10598#ifdef XK_dead_cedilla
7a13e894 10599 || orig_keysym == XK_dead_cedilla
69388238
RS
10600#endif
10601#ifdef XK_dead_breve
7a13e894 10602 || orig_keysym == XK_dead_breve
69388238
RS
10603#endif
10604#ifdef XK_dead_ogonek
7a13e894 10605 || orig_keysym == XK_dead_ogonek
69388238
RS
10606#endif
10607#ifdef XK_dead_caron
7a13e894 10608 || orig_keysym == XK_dead_caron
69388238
RS
10609#endif
10610#ifdef XK_dead_doubleacute
7a13e894 10611 || orig_keysym == XK_dead_doubleacute
69388238
RS
10612#endif
10613#ifdef XK_dead_abovedot
7a13e894 10614 || orig_keysym == XK_dead_abovedot
c34790e0 10615#endif
7a13e894
RS
10616 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10617 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10618 /* Any "vendor-specific" key is ok. */
f0e299de
GM
10619 || (orig_keysym & (1 << 28))
10620 || (keysym != NoSymbol && nbytes == 0))
7a13e894 10621 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
10622#ifndef HAVE_X11R5
10623#ifdef XK_Mode_switch
7a13e894 10624 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
10625#endif
10626#ifdef XK_Num_Lock
7a13e894 10627 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
10628#endif
10629#endif /* not HAVE_X11R5 */
7ef18d79
EZ
10630 /* The symbols from XK_ISO_Lock to
10631 XK_ISO_Last_Group_Lock doesn't have real
10632 modifiers but should be treated similarly
10633 to Mode_switch by Emacs. */
10634#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10635 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10636 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10637#endif
7a13e894 10638 ))
dc6f92b8 10639 {
10e6549c
RS
10640 if (temp_index == sizeof temp_buffer / sizeof (short))
10641 temp_index = 0;
7a13e894 10642 temp_buffer[temp_index++] = keysym;
3b8f9651 10643 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
7a13e894 10644 bufp->code = keysym;
e0c1aef2 10645 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10646 bufp->arg = Qnil;
334208b7
RS
10647 bufp->modifiers
10648 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10649 modifiers);
1113d9db 10650 bufp->timestamp = event.xkey.time;
dc6f92b8 10651 bufp++;
7a13e894
RS
10652 count++;
10653 numchars--;
dc6f92b8 10654 }
7a13e894
RS
10655 else if (numchars > nbytes)
10656 {
10657 register int i;
379b5ac0 10658 register int c;
379b5ac0 10659 int nchars, len;
7a13e894 10660
c997eae5
SM
10661 /* The input should be decoded with `coding_system'
10662 which depends on which X*LookupString function
10663 we used just above and the locale. */
10664 setup_coding_system (coding_system, &coding);
10665 coding.src_multibyte = 0;
10666 coding.dst_multibyte = 1;
10667 /* The input is converted to events, thus we can't
10668 handle composition. Anyway, there's no XIM that
10669 gives us composition information. */
10670 coding.composing = COMPOSITION_DISABLED;
10671
7a13e894
RS
10672 for (i = 0; i < nbytes; i++)
10673 {
379b5ac0
KH
10674 if (temp_index == (sizeof temp_buffer
10675 / sizeof (short)))
7a13e894 10676 temp_index = 0;
379b5ac0
KH
10677 temp_buffer[temp_index++] = copy_bufptr[i];
10678 }
10679
c997eae5
SM
10680 {
10681 /* Decode the input data. */
10682 int require;
10683 unsigned char *p;
10684
10685 require = decoding_buffer_size (&coding, nbytes);
10686 p = (unsigned char *) alloca (require);
10687 coding.mode |= CODING_MODE_LAST_BLOCK;
d337aad5
KH
10688 /* We explicitely disable composition
10689 handling because key data should
10690 not contain any composition
10691 sequence. */
10692 coding.composing = COMPOSITION_DISABLED;
c997eae5
SM
10693 decode_coding (&coding, copy_bufptr, p,
10694 nbytes, require);
10695 nbytes = coding.produced;
10696 nchars = coding.produced_char;
10697 copy_bufptr = p;
10698 }
379b5ac0
KH
10699
10700 /* Convert the input data to a sequence of
10701 character events. */
10702 for (i = 0; i < nbytes; i += len)
10703 {
fee2aedc
GM
10704 if (nchars == nbytes)
10705 c = copy_bufptr[i], len = 1;
10706 else
10707 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10708 nbytes - i, len);
10709
379b5ac0 10710 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
3b8f9651
PJ
10711 ? ASCII_KEYSTROKE_EVENT
10712 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
379b5ac0 10713 bufp->code = c;
7a13e894 10714 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10715 bufp->arg = Qnil;
7a13e894
RS
10716 bufp->modifiers
10717 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10718 modifiers);
10719 bufp->timestamp = event.xkey.time;
10720 bufp++;
10721 }
10722
379b5ac0
KH
10723 count += nchars;
10724 numchars -= nchars;
1decb680
PE
10725
10726 if (keysym == NoSymbol)
10727 break;
7a13e894
RS
10728 }
10729 else
10730 abort ();
dc6f92b8 10731 }
10e6549c
RS
10732 else
10733 abort ();
dc6f92b8 10734 }
59ddecde
GM
10735#ifdef HAVE_X_I18N
10736 /* Don't dispatch this event since XtDispatchEvent calls
10737 XFilterEvent, and two calls in a row may freeze the
10738 client. */
10739 break;
10740#else
717ca130 10741 goto OTHER;
59ddecde 10742#endif
f451eb13 10743
f5d11644 10744 case KeyRelease:
59ddecde
GM
10745#ifdef HAVE_X_I18N
10746 /* Don't dispatch this event since XtDispatchEvent calls
10747 XFilterEvent, and two calls in a row may freeze the
10748 client. */
10749 break;
10750#else
f5d11644 10751 goto OTHER;
59ddecde 10752#endif
f5d11644 10753
7a13e894 10754 /* Here's a possible interpretation of the whole
06a2c219
GM
10755 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10756 you get a FocusIn event, you have to get a FocusOut
10757 event before you relinquish the focus. If you
10758 haven't received a FocusIn event, then a mere
10759 LeaveNotify is enough to free you. */
f451eb13 10760
7a13e894 10761 case EnterNotify:
06a2c219 10762 {
06a2c219
GM
10763 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10764
2c850e26 10765#if 0
582c60f8 10766 if (event.xcrossing.focus)
06a2c219
GM
10767 {
10768 /* Avoid nasty pop/raise loops. */
10769 if (f && (!(f->auto_raise)
10770 || !(f->auto_lower)
10771 || (event.xcrossing.time - enter_timestamp) > 500))
10772 {
10773 x_new_focus_frame (dpyinfo, f);
10774 enter_timestamp = event.xcrossing.time;
10775 }
10776 }
10777 else if (f == dpyinfo->x_focus_frame)
10778 x_new_focus_frame (dpyinfo, 0);
2c850e26
RS
10779#endif
10780
06a2c219
GM
10781 /* EnterNotify counts as mouse movement,
10782 so update things that depend on mouse position. */
2533c408 10783 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10784 note_mouse_movement (f, &event.xmotion);
10785 goto OTHER;
10786 }
dc6f92b8 10787
7a13e894 10788 case FocusIn:
19126e11 10789 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10790 if (event.xfocus.detail != NotifyPointer)
0f941935 10791 dpyinfo->x_focus_event_frame = f;
7a13e894 10792 if (f)
eb72635f
GM
10793 {
10794 x_new_focus_frame (dpyinfo, f);
10795
10796 /* Don't stop displaying the initial startup message
10797 for a switch-frame event we don't need. */
10798 if (GC_NILP (Vterminal_frame)
10799 && GC_CONSP (Vframe_list)
10800 && !GC_NILP (XCDR (Vframe_list)))
10801 {
10802 bufp->kind = FOCUS_IN_EVENT;
10803 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10804 bufp->arg = Qnil;
eb72635f
GM
10805 ++bufp, ++count, --numchars;
10806 }
10807 }
f9e24cb9 10808
6c183ba5
RS
10809#ifdef HAVE_X_I18N
10810 if (f && FRAME_XIC (f))
10811 XSetICFocus (FRAME_XIC (f));
10812#endif
10813
7a13e894 10814 goto OTHER;
10c5e63d 10815
7a13e894 10816 case LeaveNotify:
19126e11 10817 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10818 if (f)
10c5e63d 10819 {
7a13e894 10820 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10821 {
10822 /* If we move outside the frame, then we're
10823 certainly no longer on any text in the frame. */
10824 clear_mouse_face (dpyinfo);
10825 dpyinfo->mouse_face_mouse_frame = 0;
10826 }
10827
10828 /* Generate a nil HELP_EVENT to cancel a help-echo.
10829 Do it only if there's something to cancel.
10830 Otherwise, the startup message is cleared when
10831 the mouse leaves the frame. */
10832 if (any_help_event_p)
10833 {
be010514
GM
10834 Lisp_Object frame;
10835 int n;
10836
06a2c219 10837 XSETFRAME (frame, f);
82c5d67a 10838 help_echo = Qnil;
5ab2570d
GM
10839 n = gen_help_event (bufp, numchars,
10840 Qnil, frame, Qnil, Qnil, 0);
be010514 10841 bufp += n, count += n, numchars -= n;
06a2c219 10842 }
7a13e894 10843
2c850e26 10844#if 0
582c60f8 10845 if (event.xcrossing.focus)
0f941935 10846 x_mouse_leave (dpyinfo);
10c5e63d 10847 else
7a13e894 10848 {
0f941935
KH
10849 if (f == dpyinfo->x_focus_event_frame)
10850 dpyinfo->x_focus_event_frame = 0;
140d6643 10851 if (f == dpyinfo->x_focus_frame)
0f941935 10852 x_new_focus_frame (dpyinfo, 0);
7a13e894 10853 }
2c850e26 10854#endif
10c5e63d 10855 }
7a13e894 10856 goto OTHER;
dc6f92b8 10857
7a13e894 10858 case FocusOut:
19126e11 10859 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10860 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10861 && f == dpyinfo->x_focus_event_frame)
10862 dpyinfo->x_focus_event_frame = 0;
10863 if (f && f == dpyinfo->x_focus_frame)
10864 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10865
6c183ba5
RS
10866#ifdef HAVE_X_I18N
10867 if (f && FRAME_XIC (f))
10868 XUnsetICFocus (FRAME_XIC (f));
10869#endif
10870
7a13e894 10871 goto OTHER;
dc6f92b8 10872
7a13e894 10873 case MotionNotify:
dc6f92b8 10874 {
06a2c219 10875 previous_help_echo = help_echo;
7cea38bc 10876 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10877 help_echo_pos = -1;
06a2c219 10878
7a13e894
RS
10879 if (dpyinfo->grabbed && last_mouse_frame
10880 && FRAME_LIVE_P (last_mouse_frame))
10881 f = last_mouse_frame;
10882 else
19126e11 10883 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10884
66301061
KS
10885 if (dpyinfo->mouse_face_hidden)
10886 {
10887 dpyinfo->mouse_face_hidden = 0;
10888 clear_mouse_face (dpyinfo);
10889 }
10890
7a13e894 10891 if (f)
5bc62483
PJ
10892 {
10893
10894 /* Generate SELECT_WINDOW_EVENTs when needed. */
ee8ceff8 10895 if (mouse_autoselect_window)
5bc62483
PJ
10896 {
10897 Lisp_Object window;
10898 int area;
10899
10900 window = window_from_coordinates (f,
0fc8f792 10901 event.xmotion.x, event.xmotion.y,
5bc62483
PJ
10902 &area, 0);
10903
10904 /* Window will be selected only when it is not selected now and
10905 last mouse movement event was not in it. Minibuffer window
10906 will be selected iff it is active. */
ce1295bd
PJ
10907 if (WINDOWP(window)
10908 && !EQ (window, last_window)
5bc62483
PJ
10909 && !EQ (window, selected_window)
10910 && numchars > 0)
10911 {
10912 bufp->kind = SELECT_WINDOW_EVENT;
10913 bufp->frame_or_window = window;
10914 bufp->arg = Qnil;
10915 ++bufp, ++count, --numchars;
10916 }
10917
10918 last_window=window;
10919 }
10920 note_mouse_movement (f, &event.xmotion);
10921 }
7a13e894
RS
10922 else
10923 {
e88b3c50 10924#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10925 struct scroll_bar *bar
10926 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10927
7a13e894
RS
10928 if (bar)
10929 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10930#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10931
06a2c219
GM
10932 /* If we move outside the frame, then we're
10933 certainly no longer on any text in the frame. */
7a13e894
RS
10934 clear_mouse_face (dpyinfo);
10935 }
06a2c219
GM
10936
10937 /* If the contents of the global variable help_echo
10938 has changed, generate a HELP_EVENT. */
b7e80413
SM
10939 if (!NILP (help_echo)
10940 || !NILP (previous_help_echo))
06a2c219
GM
10941 {
10942 Lisp_Object frame;
be010514 10943 int n;
06a2c219
GM
10944
10945 if (f)
10946 XSETFRAME (frame, f);
10947 else
10948 frame = Qnil;
10949
10950 any_help_event_p = 1;
5ab2570d 10951 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10952 help_echo_window, help_echo_object,
10953 help_echo_pos);
be010514 10954 bufp += n, count += n, numchars -= n;
06a2c219
GM
10955 }
10956
10957 goto OTHER;
dc6f92b8 10958 }
dc6f92b8 10959
7a13e894 10960 case ConfigureNotify:
9829ddba
RS
10961 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10962 if (f)
af395ec1 10963 {
7a4bce14 10964#ifndef USE_X_TOOLKIT
d0fa3e56
EZ
10965 /* If there is a pending resize for fullscreen, don't
10966 do this one, the right one will come later.
10967 The toolkit version doesn't seem to need this, but we
7a4bce14 10968 need to reset it below. */
d0fa3e56
EZ
10969 int dont_resize =
10970 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10971 && FRAME_NEW_WIDTH (f) != 0);
bf1b7b30
KH
10972 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10973 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
d0fa3e56
EZ
10974 if (dont_resize)
10975 goto OTHER;
5c187dee 10976
2d7fc7e8
RS
10977 /* In the toolkit version, change_frame_size
10978 is called by the code that handles resizing
10979 of the EmacsFrame widget. */
7a13e894 10980
7a13e894
RS
10981 /* Even if the number of character rows and columns has
10982 not changed, the font size may have changed, so we need
10983 to check the pixel dimensions as well. */
10984 if (columns != f->width
10985 || rows != f->height
7556890b
RS
10986 || event.xconfigure.width != f->output_data.x->pixel_width
10987 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10988 {
7d1e984f 10989 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10990 SET_FRAME_GARBAGED (f);
e687d06e 10991 cancel_mouse_face (f);
7a13e894 10992 }
2d7fc7e8 10993#endif
af395ec1 10994
7556890b
RS
10995 f->output_data.x->pixel_width = event.xconfigure.width;
10996 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10997
10998 /* What we have now is the position of Emacs's own window.
10999 Convert that to the position of the window manager window. */
dcb07ae9
RS
11000 x_real_positions (f, &f->output_data.x->left_pos,
11001 &f->output_data.x->top_pos);
11002
d0fa3e56
EZ
11003 x_check_fullscreen_move(f);
11004 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11005 f->output_data.x->want_fullscreen &=
11006 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
f5d11644
GM
11007#ifdef HAVE_X_I18N
11008 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11009 xic_set_statusarea (f);
11010#endif
11011
dcb07ae9
RS
11012 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11013 {
11014 /* Since the WM decorations come below top_pos now,
11015 we must put them below top_pos in the future. */
11016 f->output_data.x->win_gravity = NorthWestGravity;
11017 x_wm_set_size_hint (f, (long) 0, 0);
11018 }
8f08dc93
KH
11019#ifdef USE_MOTIF
11020 /* Some window managers pass (0,0) as the location of
11021 the window, and the Motif event handler stores it
11022 in the emacs widget, which messes up Motif menus. */
11023 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11024 {
11025 event.xconfigure.x = f->output_data.x->widget->core.x;
11026 event.xconfigure.y = f->output_data.x->widget->core.y;
11027 }
06a2c219 11028#endif /* USE_MOTIF */
7a13e894 11029 }
2d7fc7e8 11030 goto OTHER;
dc6f92b8 11031
7a13e894
RS
11032 case ButtonPress:
11033 case ButtonRelease:
11034 {
11035 /* If we decide we want to generate an event to be seen
11036 by the rest of Emacs, we put it here. */
11037 struct input_event emacs_event;
9ea173e8 11038 int tool_bar_p = 0;
06a2c219 11039
3b8f9651 11040 emacs_event.kind = NO_EVENT;
7a13e894 11041 bzero (&compose_status, sizeof (compose_status));
9b07615b 11042
06a2c219
GM
11043 if (dpyinfo->grabbed
11044 && last_mouse_frame
9f67f20b
RS
11045 && FRAME_LIVE_P (last_mouse_frame))
11046 f = last_mouse_frame;
11047 else
2224b905 11048 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 11049
06a2c219
GM
11050 if (f)
11051 {
9ea173e8
GM
11052 /* Is this in the tool-bar? */
11053 if (WINDOWP (f->tool_bar_window)
11054 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
11055 {
11056 Lisp_Object window;
11057 int p, x, y;
11058
11059 x = event.xbutton.x;
11060 y = event.xbutton.y;
11061
11062 /* Set x and y. */
11063 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 11064 if (EQ (window, f->tool_bar_window))
06a2c219 11065 {
9ea173e8
GM
11066 x_handle_tool_bar_click (f, &event.xbutton);
11067 tool_bar_p = 1;
06a2c219
GM
11068 }
11069 }
11070
9ea173e8 11071 if (!tool_bar_p)
06a2c219
GM
11072 if (!dpyinfo->x_focus_frame
11073 || f == dpyinfo->x_focus_frame)
11074 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
11075 }
11076 else
11077 {
06a2c219 11078#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
11079 struct scroll_bar *bar
11080 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 11081
7a13e894
RS
11082 if (bar)
11083 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 11084#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
11085 }
11086
11087 if (event.type == ButtonPress)
11088 {
11089 dpyinfo->grabbed |= (1 << event.xbutton.button);
11090 last_mouse_frame = f;
edad46f6
KH
11091 /* Ignore any mouse motion that happened
11092 before this event; any subsequent mouse-movement
11093 Emacs events should reflect only motion after
11094 the ButtonPress. */
a00e91cd
KH
11095 if (f != 0)
11096 f->mouse_moved = 0;
06a2c219 11097
9ea173e8
GM
11098 if (!tool_bar_p)
11099 last_tool_bar_item = -1;
7a13e894 11100 }
3afe33e7
RS
11101 else
11102 {
7a13e894 11103 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 11104 }
23faf38f 11105
3b8f9651 11106 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
7a13e894
RS
11107 {
11108 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11109 bufp++;
11110 count++;
11111 numchars--;
11112 }
3afe33e7
RS
11113
11114#ifdef USE_X_TOOLKIT
2224b905
RS
11115 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11116 /* For a down-event in the menu bar,
11117 don't pass it to Xt right now.
11118 Instead, save it away
11119 and we will pass it to Xt from kbd_buffer_get_event.
11120 That way, we can run some Lisp code first. */
91375f8f
RS
11121 if (f && event.type == ButtonPress
11122 /* Verify the event is really within the menu bar
11123 and not just sent to it due to grabbing. */
11124 && event.xbutton.x >= 0
11125 && event.xbutton.x < f->output_data.x->pixel_width
11126 && event.xbutton.y >= 0
11127 && event.xbutton.y < f->output_data.x->menubar_height
11128 && event.xbutton.same_screen)
2224b905 11129 {
8805890a 11130 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
11131 XSETFRAME (last_mouse_press_frame, f);
11132 }
11133 else if (event.type == ButtonPress)
11134 {
11135 last_mouse_press_frame = Qnil;
30e671c3 11136 goto OTHER;
ce89ef46 11137 }
06a2c219 11138
2237cac9
RS
11139#ifdef USE_MOTIF /* This should do not harm for Lucid,
11140 but I am trying to be cautious. */
ce89ef46
RS
11141 else if (event.type == ButtonRelease)
11142 {
2237cac9 11143 if (!NILP (last_mouse_press_frame))
f10ded1c 11144 {
2237cac9
RS
11145 f = XFRAME (last_mouse_press_frame);
11146 if (f->output_data.x)
06a2c219 11147 SET_SAVED_BUTTON_EVENT;
f10ded1c 11148 }
06a2c219 11149 else
30e671c3 11150 goto OTHER;
2224b905 11151 }
2237cac9 11152#endif /* USE_MOTIF */
2224b905
RS
11153 else
11154 goto OTHER;
3afe33e7 11155#endif /* USE_X_TOOLKIT */
7a13e894
RS
11156 }
11157 break;
dc6f92b8 11158
7a13e894 11159 case CirculateNotify:
06a2c219
GM
11160 goto OTHER;
11161
7a13e894 11162 case CirculateRequest:
06a2c219
GM
11163 goto OTHER;
11164
11165 case VisibilityNotify:
11166 goto OTHER;
dc6f92b8 11167
7a13e894
RS
11168 case MappingNotify:
11169 /* Someone has changed the keyboard mapping - update the
11170 local cache. */
11171 switch (event.xmapping.request)
11172 {
11173 case MappingModifier:
11174 x_find_modifier_meanings (dpyinfo);
11175 /* This is meant to fall through. */
11176 case MappingKeyboard:
11177 XRefreshKeyboardMapping (&event.xmapping);
11178 }
7a13e894 11179 goto OTHER;
dc6f92b8 11180
7a13e894 11181 default:
7a13e894 11182 OTHER:
717ca130 11183#ifdef USE_X_TOOLKIT
7a13e894
RS
11184 BLOCK_INPUT;
11185 XtDispatchEvent (&event);
11186 UNBLOCK_INPUT;
3afe33e7 11187#endif /* USE_X_TOOLKIT */
7a13e894
RS
11188 break;
11189 }
dc6f92b8
JB
11190 }
11191 }
11192
06a2c219
GM
11193 out:;
11194
9a5196d0
RS
11195 /* On some systems, an X bug causes Emacs to get no more events
11196 when the window is destroyed. Detect that. (1994.) */
58769bee 11197 if (! event_found)
ef2a22d0 11198 {
ef2a22d0
RS
11199 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11200 One XNOOP in 100 loops will make Emacs terminate.
11201 B. Bretthauer, 1994 */
11202 x_noop_count++;
58769bee 11203 if (x_noop_count >= 100)
ef2a22d0
RS
11204 {
11205 x_noop_count=0;
2224b905
RS
11206
11207 if (next_noop_dpyinfo == 0)
11208 next_noop_dpyinfo = x_display_list;
11209
11210 XNoOp (next_noop_dpyinfo->display);
11211
11212 /* Each time we get here, cycle through the displays now open. */
11213 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
11214 }
11215 }
502add23 11216
06a2c219 11217 /* If the focus was just given to an auto-raising frame,
0134a210 11218 raise it now. */
7a13e894 11219 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
11220 if (pending_autoraise_frame)
11221 {
11222 x_raise_frame (pending_autoraise_frame);
11223 pending_autoraise_frame = 0;
11224 }
0134a210 11225
dc6f92b8 11226 UNBLOCK_INPUT;
bde5503b 11227 --handling_signal;
dc6f92b8
JB
11228 return count;
11229}
06a2c219
GM
11230
11231
11232
dc6f92b8 11233\f
06a2c219
GM
11234/***********************************************************************
11235 Text Cursor
11236 ***********************************************************************/
11237
60626bab
GM
11238/* Notice when the text cursor of window W has been completely
11239 overwritten by a drawing operation that outputs glyphs in AREA
11240 starting at X0 and ending at X1 in the line starting at Y0 and
11241 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11242 the rest of the line after X0 has been written. Y coordinates
11243 are window-relative. */
06a2c219
GM
11244
11245static void
60626bab 11246notice_overwritten_cursor (w, area, x0, x1, y0, y1)
06a2c219 11247 struct window *w;
60626bab
GM
11248 enum glyph_row_area area;
11249 int x0, y0, x1, y1;
06a2c219 11250{
b3738089
GM
11251 if (area == TEXT_AREA && w->phys_cursor_on_p)
11252 {
11253 int cx0 = w->phys_cursor.x;
11254 int cx1 = cx0 + w->phys_cursor_width;
11255 int cy0 = w->phys_cursor.y;
11256 int cy1 = cy0 + w->phys_cursor_height;
11257
11258 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11259 {
11260 /* The cursor image will be completely removed from the
11261 screen if the output area intersects the cursor area in
11262 y-direction. When we draw in [y0 y1[, and some part of
11263 the cursor is at y < y0, that part must have been drawn
11264 before. When scrolling, the cursor is erased before
11265 actually scrolling, so we don't come here. When not
11266 scrolling, the rows above the old cursor row must have
11267 changed, and in this case these rows must have written
516074a1
GM
11268 over the cursor image.
11269
11270 Likewise if part of the cursor is below y1, with the
11271 exception of the cursor being in the first blank row at
11272 the buffer and window end because update_text_area
4398e673
GM
11273 doesn't draw that row. (Except when it does, but
11274 that's handled in update_text_area.) */
11275
f7c5994d 11276 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
516074a1 11277 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
b3738089
GM
11278 w->phys_cursor_on_p = 0;
11279 }
11280 }
06a2c219 11281}
f451eb13
JB
11282
11283
06a2c219
GM
11284/* Set clipping for output in glyph row ROW. W is the window in which
11285 we operate. GC is the graphics context to set clipping in.
11286 WHOLE_LINE_P non-zero means include the areas used for truncation
11287 mark display and alike in the clipping rectangle.
11288
11289 ROW may be a text row or, e.g., a mode line. Text rows must be
11290 clipped to the interior of the window dedicated to text display,
11291 mode lines must be clipped to the whole window. */
dc6f92b8
JB
11292
11293static void
06a2c219
GM
11294x_clip_to_row (w, row, gc, whole_line_p)
11295 struct window *w;
11296 struct glyph_row *row;
11297 GC gc;
11298 int whole_line_p;
dc6f92b8 11299{
06a2c219
GM
11300 struct frame *f = XFRAME (WINDOW_FRAME (w));
11301 XRectangle clip_rect;
11302 int window_x, window_y, window_width, window_height;
dc6f92b8 11303
06a2c219 11304 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 11305
06a2c219
GM
11306 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11307 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11308 clip_rect.y = max (clip_rect.y, window_y);
11309 clip_rect.width = window_width;
11310 clip_rect.height = row->visible_height;
5c1aae96 11311
06a2c219
GM
11312 /* If clipping to the whole line, including trunc marks, extend
11313 the rectangle to the left and increase its width. */
11314 if (whole_line_p)
11315 {
3f332ef3
KS
11316 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11317 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
06a2c219 11318 }
5c1aae96 11319
06a2c219 11320 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
11321}
11322
06a2c219
GM
11323
11324/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
11325
11326static void
06a2c219
GM
11327x_draw_hollow_cursor (w, row)
11328 struct window *w;
11329 struct glyph_row *row;
dc6f92b8 11330{
06a2c219
GM
11331 struct frame *f = XFRAME (WINDOW_FRAME (w));
11332 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11333 Display *dpy = FRAME_X_DISPLAY (f);
11334 int x, y, wd, h;
11335 XGCValues xgcv;
11336 struct glyph *cursor_glyph;
11337 GC gc;
11338
11339 /* Compute frame-relative coordinates from window-relative
11340 coordinates. */
11341 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11342 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11343 + row->ascent - w->phys_cursor_ascent);
11344 h = row->height - 1;
11345
11346 /* Get the glyph the cursor is on. If we can't tell because
11347 the current matrix is invalid or such, give up. */
11348 cursor_glyph = get_phys_cursor_glyph (w);
11349 if (cursor_glyph == NULL)
dc6f92b8
JB
11350 return;
11351
06a2c219
GM
11352 /* Compute the width of the rectangle to draw. If on a stretch
11353 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11354 rectangle as wide as the glyph, but use a canonical character
11355 width instead. */
11356 wd = cursor_glyph->pixel_width - 1;
11357 if (cursor_glyph->type == STRETCH_GLYPH
11358 && !x_stretch_cursor_p)
11359 wd = min (CANON_X_UNIT (f), wd);
b3738089 11360 w->phys_cursor_width = wd;
06a2c219
GM
11361
11362 /* The foreground of cursor_gc is typically the same as the normal
11363 background color, which can cause the cursor box to be invisible. */
11364 xgcv.foreground = f->output_data.x->cursor_pixel;
11365 if (dpyinfo->scratch_cursor_gc)
11366 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11367 else
11368 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11369 GCForeground, &xgcv);
11370 gc = dpyinfo->scratch_cursor_gc;
11371
11372 /* Set clipping, draw the rectangle, and reset clipping again. */
11373 x_clip_to_row (w, row, gc, 0);
11374 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11375 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
11376}
11377
06a2c219
GM
11378
11379/* Draw a bar cursor on window W in glyph row ROW.
11380
11381 Implementation note: One would like to draw a bar cursor with an
11382 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11383 Unfortunately, I didn't find a font yet that has this property set.
11384 --gerd. */
dc6f92b8
JB
11385
11386static void
65c26775 11387x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
11388 struct window *w;
11389 struct glyph_row *row;
f02d8aa0 11390 int width;
65c26775 11391 enum text_cursor_kinds kind;
dc6f92b8 11392{
92f424df
GM
11393 struct frame *f = XFRAME (w->frame);
11394 struct glyph *cursor_glyph;
06a2c219 11395
92f424df
GM
11396 /* If cursor is out of bounds, don't draw garbage. This can happen
11397 in mini-buffer windows when switching between echo area glyphs
11398 and mini-buffer. */
11399 cursor_glyph = get_phys_cursor_glyph (w);
11400 if (cursor_glyph == NULL)
11401 return;
06a2c219 11402
92f424df
GM
11403 /* If on an image, draw like a normal cursor. That's usually better
11404 visible than drawing a bar, esp. if the image is large so that
11405 the bar might not be in the window. */
11406 if (cursor_glyph->type == IMAGE_GLYPH)
11407 {
11408 struct glyph_row *row;
11409 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11410 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11411 }
11412 else
11413 {
34e5d0af
GM
11414 Display *dpy = FRAME_X_DISPLAY (f);
11415 Window window = FRAME_X_WINDOW (f);
11416 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11417 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11418 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11419 XGCValues xgcv;
11420
11421 /* If the glyph's background equals the color we normally draw
11422 the bar cursor in, the bar cursor in its normal color is
11423 invisible. Use the glyph's foreground color instead in this
11424 case, on the assumption that the glyph's colors are chosen so
11425 that the glyph is legible. */
11426 if (face->background == f->output_data.x->cursor_pixel)
11427 xgcv.background = xgcv.foreground = face->foreground;
11428 else
11429 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 11430 xgcv.graphics_exposures = 0;
92f424df 11431
06a2c219
GM
11432 if (gc)
11433 XChangeGC (dpy, gc, mask, &xgcv);
11434 else
11435 {
11436 gc = XCreateGC (dpy, window, mask, &xgcv);
11437 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11438 }
92f424df 11439
f02d8aa0
GM
11440 if (width < 0)
11441 width = f->output_data.x->cursor_width;
34e5d0af 11442 width = min (cursor_glyph->pixel_width, width);
92f424df 11443
b3738089 11444 w->phys_cursor_width = width;
06a2c219 11445 x_clip_to_row (w, row, gc, 0);
b3738089 11446
65c26775
EZ
11447 if (kind == BAR_CURSOR)
11448 XFillRectangle (dpy, window, gc,
11449 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11450 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11451 width, row->height);
11452 else
11453 XFillRectangle (dpy, window, gc,
11454 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11455 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11456 row->height - width),
11457 cursor_glyph->pixel_width,
11458 width);
11459
06a2c219
GM
11460 XSetClipMask (dpy, gc, None);
11461 }
dc6f92b8
JB
11462}
11463
06a2c219
GM
11464
11465/* Clear the cursor of window W to background color, and mark the
11466 cursor as not shown. This is used when the text where the cursor
11467 is is about to be rewritten. */
11468
dc6f92b8 11469static void
06a2c219
GM
11470x_clear_cursor (w)
11471 struct window *w;
dc6f92b8 11472{
06a2c219
GM
11473 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11474 x_update_window_cursor (w, 0);
11475}
90e65f07 11476
dbc4e1c1 11477
06a2c219
GM
11478/* Draw the cursor glyph of window W in glyph row ROW. See the
11479 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 11480
06a2c219
GM
11481static void
11482x_draw_phys_cursor_glyph (w, row, hl)
11483 struct window *w;
11484 struct glyph_row *row;
11485 enum draw_glyphs_face hl;
11486{
11487 /* If cursor hpos is out of bounds, don't draw garbage. This can
11488 happen in mini-buffer windows when switching between echo area
11489 glyphs and mini-buffer. */
11490 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e 11491 {
f0a48a01 11492 int on_p = w->phys_cursor_on_p;
56a0382c 11493 int x1;
f0a48a01 11494
56a0382c
GM
11495 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11496 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11497 hl, 0);
f0a48a01 11498 w->phys_cursor_on_p = on_p;
66ac4b0e 11499
b3738089 11500 if (hl == DRAW_CURSOR)
56a0382c 11501 w->phys_cursor_width = x1 - w->phys_cursor.x;
b3738089 11502
66ac4b0e
GM
11503 /* When we erase the cursor, and ROW is overlapped by other
11504 rows, make sure that these overlapping parts of other rows
11505 are redrawn. */
b3738089 11506 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
66ac4b0e
GM
11507 {
11508 if (row > w->current_matrix->rows
11509 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11510 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11511
11512 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11513 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11514 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11515 }
11516 }
06a2c219 11517}
dbc4e1c1 11518
eea6af04 11519
06a2c219 11520/* Erase the image of a cursor of window W from the screen. */
eea6af04 11521
06a2c219
GM
11522static void
11523x_erase_phys_cursor (w)
11524 struct window *w;
11525{
11526 struct frame *f = XFRAME (w->frame);
11527 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11528 int hpos = w->phys_cursor.hpos;
11529 int vpos = w->phys_cursor.vpos;
11530 int mouse_face_here_p = 0;
11531 struct glyph_matrix *active_glyphs = w->current_matrix;
11532 struct glyph_row *cursor_row;
11533 struct glyph *cursor_glyph;
11534 enum draw_glyphs_face hl;
11535
11536 /* No cursor displayed or row invalidated => nothing to do on the
11537 screen. */
11538 if (w->phys_cursor_type == NO_CURSOR)
11539 goto mark_cursor_off;
11540
11541 /* VPOS >= active_glyphs->nrows means that window has been resized.
11542 Don't bother to erase the cursor. */
11543 if (vpos >= active_glyphs->nrows)
11544 goto mark_cursor_off;
11545
11546 /* If row containing cursor is marked invalid, there is nothing we
11547 can do. */
11548 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11549 if (!cursor_row->enabled_p)
11550 goto mark_cursor_off;
11551
a2c6de8e
KS
11552 /* If row is completely invisible, don't attempt to delete a cursor which
11553 isn't there. This can happen if cursor is at top of a window, and
11554 we switch to a buffer with a header line in that window. */
11555 if (cursor_row->visible_height <= 0)
11556 goto mark_cursor_off;
11557
06a2c219
GM
11558 /* This can happen when the new row is shorter than the old one.
11559 In this case, either x_draw_glyphs or clear_end_of_line
11560 should have cleared the cursor. Note that we wouldn't be
11561 able to erase the cursor in this case because we don't have a
11562 cursor glyph at hand. */
11563 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11564 goto mark_cursor_off;
11565
11566 /* If the cursor is in the mouse face area, redisplay that when
11567 we clear the cursor. */
8801a864
KR
11568 if (! NILP (dpyinfo->mouse_face_window)
11569 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
11570 && (vpos > dpyinfo->mouse_face_beg_row
11571 || (vpos == dpyinfo->mouse_face_beg_row
11572 && hpos >= dpyinfo->mouse_face_beg_col))
11573 && (vpos < dpyinfo->mouse_face_end_row
11574 || (vpos == dpyinfo->mouse_face_end_row
11575 && hpos < dpyinfo->mouse_face_end_col))
11576 /* Don't redraw the cursor's spot in mouse face if it is at the
11577 end of a line (on a newline). The cursor appears there, but
11578 mouse highlighting does not. */
11579 && cursor_row->used[TEXT_AREA] > hpos)
11580 mouse_face_here_p = 1;
11581
11582 /* Maybe clear the display under the cursor. */
11583 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11584 {
11585 int x;
045dee35 11586 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 11587
06a2c219
GM
11588 cursor_glyph = get_phys_cursor_glyph (w);
11589 if (cursor_glyph == NULL)
11590 goto mark_cursor_off;
dbc4e1c1 11591
e0300d33 11592 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
06a2c219 11593
c5e6e06b
GM
11594 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11595 x,
11596 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11597 cursor_row->y)),
11598 cursor_glyph->pixel_width,
11599 cursor_row->visible_height,
11600 False);
dbc4e1c1 11601 }
06a2c219
GM
11602
11603 /* Erase the cursor by redrawing the character underneath it. */
11604 if (mouse_face_here_p)
11605 hl = DRAW_MOUSE_FACE;
06a2c219
GM
11606 else
11607 hl = DRAW_NORMAL_TEXT;
11608 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 11609
06a2c219
GM
11610 mark_cursor_off:
11611 w->phys_cursor_on_p = 0;
11612 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
11613}
11614
11615
b7f83f9e
GM
11616/* Non-zero if physical cursor of window W is within mouse face. */
11617
11618static int
11619cursor_in_mouse_face_p (w)
11620 struct window *w;
11621{
11622 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11623 int in_mouse_face = 0;
11624
11625 if (WINDOWP (dpyinfo->mouse_face_window)
11626 && XWINDOW (dpyinfo->mouse_face_window) == w)
11627 {
11628 int hpos = w->phys_cursor.hpos;
11629 int vpos = w->phys_cursor.vpos;
11630
11631 if (vpos >= dpyinfo->mouse_face_beg_row
11632 && vpos <= dpyinfo->mouse_face_end_row
11633 && (vpos > dpyinfo->mouse_face_beg_row
11634 || hpos >= dpyinfo->mouse_face_beg_col)
11635 && (vpos < dpyinfo->mouse_face_end_row
11636 || hpos < dpyinfo->mouse_face_end_col
11637 || dpyinfo->mouse_face_past_end))
11638 in_mouse_face = 1;
11639 }
11640
11641 return in_mouse_face;
11642}
11643
11644
06a2c219
GM
11645/* Display or clear cursor of window W. If ON is zero, clear the
11646 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11647 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 11648
06a2c219
GM
11649void
11650x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11651 struct window *w;
11652 int on, hpos, vpos, x, y;
dbc4e1c1 11653{
06a2c219
GM
11654 struct frame *f = XFRAME (w->frame);
11655 int new_cursor_type;
f02d8aa0 11656 int new_cursor_width;
f7e039de 11657 int cursor_off_state = 0;
06a2c219
GM
11658 struct glyph_matrix *current_glyphs;
11659 struct glyph_row *glyph_row;
11660 struct glyph *glyph;
dbc4e1c1 11661
49d838ea 11662 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
11663 windows and frames; in the latter case, the frame or window may
11664 be in the midst of changing its size, and x and y may be off the
11665 window. */
11666 if (! FRAME_VISIBLE_P (f)
11667 || FRAME_GARBAGED_P (f)
11668 || vpos >= w->current_matrix->nrows
11669 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
11670 return;
11671
11672 /* If cursor is off and we want it off, return quickly. */
06a2c219 11673 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
11674 return;
11675
06a2c219
GM
11676 current_glyphs = w->current_matrix;
11677 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11678 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11679
11680 /* If cursor row is not enabled, we don't really know where to
11681 display the cursor. */
11682 if (!glyph_row->enabled_p)
11683 {
11684 w->phys_cursor_on_p = 0;
11685 return;
11686 }
11687
11688 xassert (interrupt_input_blocked);
11689
11690 /* Set new_cursor_type to the cursor we want to be displayed. In a
11691 mini-buffer window, we want the cursor only to appear if we are
11692 reading input from this window. For the selected window, we want
11693 the cursor type given by the frame parameter. If explicitly
11694 marked off, draw no cursor. In all other cases, we want a hollow
11695 box cursor. */
f02d8aa0 11696 new_cursor_width = -1;
f7e039de
RS
11697 new_cursor_type = -2;
11698
11699 /* Echo area */
9b4a7047
GM
11700 if (cursor_in_echo_area
11701 && FRAME_HAS_MINIBUF_P (f)
11702 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 11703 {
9b4a7047
GM
11704 if (w == XWINDOW (echo_area_window))
11705 new_cursor_type = FRAME_DESIRED_CURSOR (f);
f7e039de
RS
11706 else if (NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11707 w->buffer)))
11708 new_cursor_type = NO_CURSOR;
9a7bdceb 11709 else
f7e039de
RS
11710 cursor_off_state = 1;
11711 }
11712
11713 /* Nonselected window or nonselected frame. */
11714 else if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11715 || w != XWINDOW (f->selected_window))
11716 {
11717 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11718 || NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11719 w->buffer))
11720 || NILP (XBUFFER (w->buffer)->cursor_type))
9a7bdceb 11721 new_cursor_type = NO_CURSOR;
f7e039de
RS
11722 else
11723 cursor_off_state = 1;
06a2c219 11724 }
f7e039de
RS
11725
11726 /* If new_cursor_type isn't decided yet, decide it now. */
11727 if (new_cursor_type == -2)
9b4a7047 11728 {
f7e039de
RS
11729 struct buffer *b = XBUFFER (w->buffer);
11730
11731 if (EQ (b->cursor_type, Qt))
11732 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9b4a7047 11733 else
f7e039de
RS
11734 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11735 &new_cursor_width);
11736 }
f02d8aa0 11737
f7e039de
RS
11738 /* Dim out or hollow out the cursor,
11739 if it has blinked off or for nonselected windows. */
11740 if (w->cursor_off_p || cursor_off_state)
11741 {
11742 if (new_cursor_type == FILLED_BOX_CURSOR)
11743 new_cursor_type = HOLLOW_BOX_CURSOR;
11744 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
11745 new_cursor_width = 1;
11746 else
11747 new_cursor_type = NO_CURSOR;
9b4a7047 11748 }
06a2c219 11749
f7e039de
RS
11750 /* Now new_cursor_type is correct. */
11751
06a2c219
GM
11752 /* If cursor is currently being shown and we don't want it to be or
11753 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 11754 erase it. */
06a2c219 11755 if (w->phys_cursor_on_p
dc6f92b8 11756 && (!on
06a2c219
GM
11757 || w->phys_cursor.x != x
11758 || w->phys_cursor.y != y
e1429afe
KS
11759 || new_cursor_type != w->phys_cursor_type
11760 || (new_cursor_type == BAR_CURSOR
11761 && new_cursor_width != w->phys_cursor_width)))
06a2c219
GM
11762 x_erase_phys_cursor (w);
11763
56a0382c
GM
11764 /* Don't check phys_cursor_on_p here because that flag is only set
11765 to zero in some cases where we know that the cursor has been
11766 completely erased, to avoid the extra work of erasing the cursor
11767 twice. In other words, phys_cursor_on_p can be 1 and the cursor
11768 still not be visible, or it has only been partly erased. */
11769 if (on)
06a2c219
GM
11770 {
11771 w->phys_cursor_ascent = glyph_row->ascent;
11772 w->phys_cursor_height = glyph_row->height;
11773
11774 /* Set phys_cursor_.* before x_draw_.* is called because some
11775 of them may need the information. */
11776 w->phys_cursor.x = x;
11777 w->phys_cursor.y = glyph_row->y;
11778 w->phys_cursor.hpos = hpos;
11779 w->phys_cursor.vpos = vpos;
11780 w->phys_cursor_type = new_cursor_type;
11781 w->phys_cursor_on_p = 1;
11782
11783 switch (new_cursor_type)
dc6f92b8 11784 {
06a2c219
GM
11785 case HOLLOW_BOX_CURSOR:
11786 x_draw_hollow_cursor (w, glyph_row);
11787 break;
11788
11789 case FILLED_BOX_CURSOR:
11790 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11791 break;
11792
11793 case BAR_CURSOR:
65c26775
EZ
11794 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
11795 break;
11796
11797 case HBAR_CURSOR:
b3738089 11798 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
06a2c219
GM
11799 break;
11800
11801 case NO_CURSOR:
4398e673 11802 w->phys_cursor_width = 0;
06a2c219 11803 break;
dc6f92b8 11804
06a2c219
GM
11805 default:
11806 abort ();
11807 }
59ddecde
GM
11808
11809#ifdef HAVE_X_I18N
11810 if (w == XWINDOW (f->selected_window))
11811 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11812 xic_set_preeditarea (w, x, y);
11813#endif
dc6f92b8
JB
11814 }
11815
06a2c219 11816#ifndef XFlush
f676886a 11817 if (updating_frame != f)
334208b7 11818 XFlush (FRAME_X_DISPLAY (f));
06a2c219 11819#endif
dc6f92b8
JB
11820}
11821
06a2c219
GM
11822
11823/* Display the cursor on window W, or clear it. X and Y are window
11824 relative pixel coordinates. HPOS and VPOS are glyph matrix
11825 positions. If W is not the selected window, display a hollow
11826 cursor. ON non-zero means display the cursor at X, Y which
11827 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11828
dfcf069d 11829void
06a2c219
GM
11830x_display_cursor (w, on, hpos, vpos, x, y)
11831 struct window *w;
11832 int on, hpos, vpos, x, y;
dc6f92b8 11833{
f94397b5 11834 BLOCK_INPUT;
06a2c219 11835 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11836 UNBLOCK_INPUT;
11837}
11838
06a2c219
GM
11839
11840/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11841 Don't change the cursor's position. */
11842
dfcf069d 11843void
06a2c219 11844x_update_cursor (f, on_p)
5d46f928 11845 struct frame *f;
5d46f928 11846{
06a2c219
GM
11847 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11848}
11849
11850
11851/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11852 in the window tree rooted at W. */
11853
11854static void
11855x_update_cursor_in_window_tree (w, on_p)
11856 struct window *w;
11857 int on_p;
11858{
11859 while (w)
11860 {
11861 if (!NILP (w->hchild))
11862 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11863 else if (!NILP (w->vchild))
11864 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11865 else
11866 x_update_window_cursor (w, on_p);
11867
11868 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11869 }
11870}
5d46f928 11871
f94397b5 11872
06a2c219
GM
11873/* Switch the display of W's cursor on or off, according to the value
11874 of ON. */
11875
11876static void
11877x_update_window_cursor (w, on)
11878 struct window *w;
11879 int on;
11880{
16b5d424
GM
11881 /* Don't update cursor in windows whose frame is in the process
11882 of being deleted. */
11883 if (w->current_matrix)
11884 {
11885 BLOCK_INPUT;
11886 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11887 w->phys_cursor.x, w->phys_cursor.y);
11888 UNBLOCK_INPUT;
11889 }
dc6f92b8 11890}
06a2c219
GM
11891
11892
11893
dc6f92b8
JB
11894\f
11895/* Icons. */
11896
dbc4e1c1 11897/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11898
11899int
990ba854 11900x_bitmap_icon (f, file)
f676886a 11901 struct frame *f;
990ba854 11902 Lisp_Object file;
dc6f92b8 11903{
06a2c219 11904 int bitmap_id;
dc6f92b8 11905
c118dd06 11906 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11907 return 1;
11908
990ba854 11909 /* Free up our existing icon bitmap if any. */
7556890b
RS
11910 if (f->output_data.x->icon_bitmap > 0)
11911 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11912 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11913
11914 if (STRINGP (file))
7f2ae036
RS
11915 bitmap_id = x_create_bitmap_from_file (f, file);
11916 else
11917 {
990ba854 11918 /* Create the GNU bitmap if necessary. */
5bf01b68 11919 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11920 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11921 = x_create_bitmap_from_data (f, gnu_bits,
11922 gnu_width, gnu_height);
990ba854
RS
11923
11924 /* The first time we create the GNU bitmap,
06a2c219 11925 this increments the ref-count one extra time.
990ba854
RS
11926 As a result, the GNU bitmap is never freed.
11927 That way, we don't have to worry about allocating it again. */
334208b7 11928 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11929
334208b7 11930 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11931 }
11932
11933 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11934 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11935
11936 return 0;
11937}
11938
11939
1be2d067
KH
11940/* Make the x-window of frame F use a rectangle with text.
11941 Use ICON_NAME as the text. */
dc6f92b8
JB
11942
11943int
f676886a
JB
11944x_text_icon (f, icon_name)
11945 struct frame *f;
dc6f92b8
JB
11946 char *icon_name;
11947{
c118dd06 11948 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11949 return 1;
11950
1be2d067
KH
11951#ifdef HAVE_X11R4
11952 {
11953 XTextProperty text;
11954 text.value = (unsigned char *) icon_name;
11955 text.encoding = XA_STRING;
11956 text.format = 8;
11957 text.nitems = strlen (icon_name);
11958#ifdef USE_X_TOOLKIT
7556890b 11959 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11960 &text);
11961#else /* not USE_X_TOOLKIT */
11962 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11963#endif /* not USE_X_TOOLKIT */
11964 }
11965#else /* not HAVE_X11R4 */
11966 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11967#endif /* not HAVE_X11R4 */
58769bee 11968
7556890b
RS
11969 if (f->output_data.x->icon_bitmap > 0)
11970 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11971 f->output_data.x->icon_bitmap = 0;
b1c884c3 11972 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11973
11974 return 0;
11975}
11976\f
e99db5a1
RS
11977#define X_ERROR_MESSAGE_SIZE 200
11978
11979/* If non-nil, this should be a string.
11980 It means catch X errors and store the error message in this string. */
11981
11982static Lisp_Object x_error_message_string;
11983
11984/* An X error handler which stores the error message in
11985 x_error_message_string. This is called from x_error_handler if
11986 x_catch_errors is in effect. */
11987
06a2c219 11988static void
e99db5a1
RS
11989x_error_catcher (display, error)
11990 Display *display;
11991 XErrorEvent *error;
11992{
11993 XGetErrorText (display, error->error_code,
11994 XSTRING (x_error_message_string)->data,
11995 X_ERROR_MESSAGE_SIZE);
11996}
11997
11998/* Begin trapping X errors for display DPY. Actually we trap X errors
11999 for all displays, but DPY should be the display you are actually
12000 operating on.
12001
12002 After calling this function, X protocol errors no longer cause
12003 Emacs to exit; instead, they are recorded in the string
12004 stored in x_error_message_string.
12005
12006 Calling x_check_errors signals an Emacs error if an X error has
12007 occurred since the last call to x_catch_errors or x_check_errors.
12008
12009 Calling x_uncatch_errors resumes the normal error handling. */
12010
12011void x_check_errors ();
12012static Lisp_Object x_catch_errors_unwind ();
12013
12014int
12015x_catch_errors (dpy)
12016 Display *dpy;
12017{
12018 int count = specpdl_ptr - specpdl;
12019
12020 /* Make sure any errors from previous requests have been dealt with. */
12021 XSync (dpy, False);
12022
12023 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
12024
12025 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
12026 XSTRING (x_error_message_string)->data[0] = 0;
12027
12028 return count;
12029}
12030
12031/* Unbind the binding that we made to check for X errors. */
12032
12033static Lisp_Object
12034x_catch_errors_unwind (old_val)
12035 Lisp_Object old_val;
12036{
12037 x_error_message_string = old_val;
12038 return Qnil;
12039}
12040
12041/* If any X protocol errors have arrived since the last call to
12042 x_catch_errors or x_check_errors, signal an Emacs error using
12043 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12044
12045void
12046x_check_errors (dpy, format)
12047 Display *dpy;
12048 char *format;
12049{
12050 /* Make sure to catch any errors incurred so far. */
12051 XSync (dpy, False);
12052
12053 if (XSTRING (x_error_message_string)->data[0])
12054 error (format, XSTRING (x_error_message_string)->data);
12055}
12056
9829ddba
RS
12057/* Nonzero if we had any X protocol errors
12058 since we did x_catch_errors on DPY. */
e99db5a1
RS
12059
12060int
12061x_had_errors_p (dpy)
12062 Display *dpy;
12063{
12064 /* Make sure to catch any errors incurred so far. */
12065 XSync (dpy, False);
12066
12067 return XSTRING (x_error_message_string)->data[0] != 0;
12068}
12069
9829ddba
RS
12070/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12071
06a2c219 12072void
9829ddba
RS
12073x_clear_errors (dpy)
12074 Display *dpy;
12075{
12076 XSTRING (x_error_message_string)->data[0] = 0;
12077}
12078
e99db5a1
RS
12079/* Stop catching X protocol errors and let them make Emacs die.
12080 DPY should be the display that was passed to x_catch_errors.
12081 COUNT should be the value that was returned by
12082 the corresponding call to x_catch_errors. */
12083
12084void
12085x_uncatch_errors (dpy, count)
12086 Display *dpy;
12087 int count;
12088{
12089 unbind_to (count, Qnil);
12090}
12091
12092#if 0
12093static unsigned int x_wire_count;
12094x_trace_wire ()
12095{
12096 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12097}
12098#endif /* ! 0 */
12099
12100\f
12101/* Handle SIGPIPE, which can happen when the connection to a server
12102 simply goes away. SIGPIPE is handled by x_connection_signal.
12103 Don't need to do anything, because the write which caused the
12104 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 12105 which will do the appropriate cleanup for us. */
e99db5a1
RS
12106
12107static SIGTYPE
12108x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 12109 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
12110{
12111#ifdef USG
12112 /* USG systems forget handlers when they are used;
12113 must reestablish each time */
12114 signal (signalnum, x_connection_signal);
12115#endif /* USG */
12116}
0da1ab50 12117
e99db5a1 12118\f
0da1ab50
GM
12119/************************************************************************
12120 Handling X errors
12121 ************************************************************************/
4746118a 12122
f0e299de
GM
12123/* Error message passed to x_connection_closed. */
12124
12125static char *error_msg;
12126
a7248e4f 12127/* Function installed as fatal_error_signal_hook in
f0e299de
GM
12128 x_connection_closed. Print the X error message, and exit normally,
12129 instead of dumping core when XtCloseDisplay fails. */
12130
12131static void
12132x_fatal_error_signal ()
12133{
12134 fprintf (stderr, "%s\n", error_msg);
12135 exit (70);
12136}
12137
0da1ab50
GM
12138/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12139 the text of an error message that lead to the connection loss. */
16bd92ea 12140
4746118a 12141static SIGTYPE
5978125e
GM
12142x_connection_closed (dpy, error_message)
12143 Display *dpy;
7a13e894 12144 char *error_message;
4746118a 12145{
5978125e 12146 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 12147 Lisp_Object frame, tail;
0da1ab50 12148 int count;
0da1ab50 12149
f0e299de
GM
12150 error_msg = (char *) alloca (strlen (error_message) + 1);
12151 strcpy (error_msg, error_message);
1a532e54
GM
12152 handling_signal = 0;
12153
0da1ab50
GM
12154 /* Prevent being called recursively because of an error condition
12155 below. Otherwise, we might end up with printing ``can't find per
12156 display information'' in the recursive call instead of printing
12157 the original message here. */
12158 count = x_catch_errors (dpy);
12159
8a4f36cc
GM
12160 /* We have to close the display to inform Xt that it doesn't
12161 exist anymore. If we don't, Xt will continue to wait for
12162 events from the display. As a consequence, a sequence of
12163
12164 M-x make-frame-on-display RET :1 RET
12165 ...kill the new frame, so that we get an IO error...
12166 M-x make-frame-on-display RET :1 RET
12167
12168 will indefinitely wait in Xt for events for display `:1', opened
12169 in the first class to make-frame-on-display.
6186a4a0 12170
8a4f36cc
GM
12171 Closing the display is reported to lead to a bus error on
12172 OpenWindows in certain situations. I suspect that is a bug
12173 in OpenWindows. I don't know how to cicumvent it here. */
12174
f613a4c8 12175#ifdef USE_X_TOOLKIT
ae24cb3b
GM
12176 /* If DPYINFO is null, this means we didn't open the display
12177 in the first place, so don't try to close it. */
12178 if (dpyinfo)
f0e299de
GM
12179 {
12180 extern void (*fatal_error_signal_hook) P_ ((void));
12181 fatal_error_signal_hook = x_fatal_error_signal;
12182 XtCloseDisplay (dpy);
12183 fatal_error_signal_hook = NULL;
12184 }
f613a4c8 12185#endif
adabc3a9 12186
8a4f36cc 12187 /* Indicate that this display is dead. */
9e80b57d
KR
12188 if (dpyinfo)
12189 dpyinfo->display = 0;
6186a4a0 12190
06a2c219 12191 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
12192 that are on the dead display. */
12193 FOR_EACH_FRAME (tail, frame)
12194 {
12195 Lisp_Object minibuf_frame;
12196 minibuf_frame
12197 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
12198 if (FRAME_X_P (XFRAME (frame))
12199 && FRAME_X_P (XFRAME (minibuf_frame))
12200 && ! EQ (frame, minibuf_frame)
12201 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
12202 Fdelete_frame (frame, Qt);
12203 }
12204
12205 /* Now delete all remaining frames on the dead display.
06a2c219 12206 We are now sure none of these is used as the mini-buffer
7a13e894
RS
12207 for another frame that we need to delete. */
12208 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
12209 if (FRAME_X_P (XFRAME (frame))
12210 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
12211 {
12212 /* Set this to t so that Fdelete_frame won't get confused
12213 trying to find a replacement. */
12214 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12215 Fdelete_frame (frame, Qt);
12216 }
7a13e894 12217
482a1bd2
KH
12218 if (dpyinfo)
12219 x_delete_display (dpyinfo);
7a13e894 12220
0da1ab50
GM
12221 x_uncatch_errors (dpy, count);
12222
7a13e894
RS
12223 if (x_display_list == 0)
12224 {
f0e299de 12225 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
12226 shut_down_emacs (0, 0, Qnil);
12227 exit (70);
12228 }
12ba150f 12229
7a13e894
RS
12230 /* Ordinary stack unwind doesn't deal with these. */
12231#ifdef SIGIO
12232 sigunblock (sigmask (SIGIO));
12233#endif
12234 sigunblock (sigmask (SIGALRM));
12235 TOTALLY_UNBLOCK_INPUT;
12236
aa4d9a9e 12237 clear_waiting_for_input ();
f0e299de 12238 error ("%s", error_msg);
4746118a
JB
12239}
12240
0da1ab50 12241
7a13e894
RS
12242/* This is the usual handler for X protocol errors.
12243 It kills all frames on the display that we got the error for.
12244 If that was the only one, it prints an error message and kills Emacs. */
12245
06a2c219 12246static void
c118dd06
JB
12247x_error_quitter (display, error)
12248 Display *display;
12249 XErrorEvent *error;
12250{
7a13e894 12251 char buf[256], buf1[356];
dc6f92b8 12252
58769bee 12253 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12254 original error handler. */
dc6f92b8 12255
c118dd06 12256 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 12257 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 12258 buf, error->request_code);
7a13e894 12259 x_connection_closed (display, buf1);
dc6f92b8
JB
12260}
12261
0da1ab50 12262
e99db5a1
RS
12263/* This is the first-level handler for X protocol errors.
12264 It calls x_error_quitter or x_error_catcher. */
7a13e894 12265
8922af5f 12266static int
e99db5a1 12267x_error_handler (display, error)
8922af5f 12268 Display *display;
e99db5a1 12269 XErrorEvent *error;
8922af5f 12270{
e99db5a1
RS
12271 if (! NILP (x_error_message_string))
12272 x_error_catcher (display, error);
12273 else
12274 x_error_quitter (display, error);
06a2c219 12275 return 0;
f9e24cb9 12276}
c118dd06 12277
e99db5a1
RS
12278/* This is the handler for X IO errors, always.
12279 It kills all frames on the display that we lost touch with.
12280 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 12281
c118dd06 12282static int
e99db5a1 12283x_io_error_quitter (display)
c118dd06 12284 Display *display;
c118dd06 12285{
e99db5a1 12286 char buf[256];
dc6f92b8 12287
e99db5a1
RS
12288 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12289 x_connection_closed (display, buf);
06a2c219 12290 return 0;
dc6f92b8 12291}
dc6f92b8 12292\f
f451eb13
JB
12293/* Changing the font of the frame. */
12294
76bcdf39
RS
12295/* Give frame F the font named FONTNAME as its default font, and
12296 return the full name of that font. FONTNAME may be a wildcard
12297 pattern; in that case, we choose some font that fits the pattern.
12298 The return value shows which font we chose. */
12299
b5cf7a0e 12300Lisp_Object
f676886a
JB
12301x_new_font (f, fontname)
12302 struct frame *f;
dc6f92b8
JB
12303 register char *fontname;
12304{
dc43ef94 12305 struct font_info *fontp
ee569018 12306 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 12307
dc43ef94
KH
12308 if (!fontp)
12309 return Qnil;
2224a5fc 12310
dc43ef94 12311 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 12312 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94 12313 f->output_data.x->fontset = -1;
976b73d7
KS
12314
12315 x_compute_fringe_widths (f, 1);
12316
b2cad826
KH
12317 /* Compute the scroll bar width in character columns. */
12318 if (f->scroll_bar_pixel_width > 0)
12319 {
7556890b 12320 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
12321 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12322 }
12323 else
4e61bddf
RS
12324 {
12325 int wid = FONT_WIDTH (f->output_data.x->font);
12326 f->scroll_bar_cols = (14 + wid - 1) / wid;
12327 }
b2cad826 12328
f676886a 12329 /* Now make the frame display the given font. */
c118dd06 12330 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 12331 {
7556890b
RS
12332 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12333 f->output_data.x->font->fid);
12334 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12335 f->output_data.x->font->fid);
12336 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12337 f->output_data.x->font->fid);
f676886a 12338
a27f9f86 12339 frame_update_line_height (f);
3497f73e
GM
12340
12341 /* Don't change the size of a tip frame; there's no point in
12342 doing it because it's done in Fx_show_tip, and it leads to
12343 problems because the tip frame has no widget. */
12344 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12345 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 12346 }
a27f9f86
RS
12347 else
12348 /* If we are setting a new frame's font for the first time,
12349 there are no faces yet, so this font's height is the line height. */
7556890b 12350 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 12351
dc43ef94
KH
12352 return build_string (fontp->full_name);
12353}
12354
12355/* Give frame F the fontset named FONTSETNAME as its default font, and
12356 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
12357 pattern; in that case, we choose some fontset that fits the pattern.
12358 The return value shows which fontset we chose. */
b5cf7a0e 12359
dc43ef94
KH
12360Lisp_Object
12361x_new_fontset (f, fontsetname)
12362 struct frame *f;
12363 char *fontsetname;
12364{
ee569018 12365 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 12366 Lisp_Object result;
b5cf7a0e 12367
dc43ef94
KH
12368 if (fontset < 0)
12369 return Qnil;
b5cf7a0e 12370
2da424f1
KH
12371 if (f->output_data.x->fontset == fontset)
12372 /* This fontset is already set in frame F. There's nothing more
12373 to do. */
ee569018 12374 return fontset_name (fontset);
dc43ef94 12375
ee569018 12376 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
12377
12378 if (!STRINGP (result))
12379 /* Can't load ASCII font. */
12380 return Qnil;
12381
12382 /* Since x_new_font doesn't update any fontset information, do it now. */
12383 f->output_data.x->fontset = fontset;
dc43ef94 12384
f5d11644
GM
12385#ifdef HAVE_X_I18N
12386 if (FRAME_XIC (f)
12387 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 12388 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
12389#endif
12390
dc43ef94 12391 return build_string (fontsetname);
dc6f92b8 12392}
f5d11644 12393
976b73d7
KS
12394/* Compute actual fringe widths */
12395
12396void
12397x_compute_fringe_widths (f, redraw)
12398 struct frame *f;
12399 int redraw;
12400{
12401 int o_left = f->output_data.x->left_fringe_width;
12402 int o_right = f->output_data.x->right_fringe_width;
12403 int o_cols = f->output_data.x->fringe_cols;
12404
12405 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12406 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12407 int left_fringe_width, right_fringe_width;
12408
12409 if (!NILP (left_fringe))
12410 left_fringe = Fcdr (left_fringe);
12411 if (!NILP (right_fringe))
12412 right_fringe = Fcdr (right_fringe);
12413
12414 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12415 XINT (left_fringe));
12416 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12417 XINT (right_fringe));
12418
12419 if (left_fringe_width || right_fringe_width)
12420 {
12421 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12422 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12423 int conf_wid = left_wid + right_wid;
12424 int font_wid = FONT_WIDTH (f->output_data.x->font);
12425 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12426 int real_wid = cols * font_wid;
12427 if (left_wid && right_wid)
12428 {
12429 if (left_fringe_width < 0)
12430 {
12431 /* Left fringe width is fixed, adjust right fringe if necessary */
12432 f->output_data.x->left_fringe_width = left_wid;
12433 f->output_data.x->right_fringe_width = real_wid - left_wid;
12434 }
12435 else if (right_fringe_width < 0)
12436 {
12437 /* Right fringe width is fixed, adjust left fringe if necessary */
12438 f->output_data.x->left_fringe_width = real_wid - right_wid;
12439 f->output_data.x->right_fringe_width = right_wid;
12440 }
12441 else
12442 {
12443 /* Adjust both fringes with an equal amount.
12444 Note that we are doing integer arithmetic here, so don't
12445 lose a pixel if the total width is an odd number. */
12446 int fill = real_wid - conf_wid;
12447 f->output_data.x->left_fringe_width = left_wid + fill/2;
12448 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12449 }
12450 }
12451 else if (left_fringe_width)
12452 {
12453 f->output_data.x->left_fringe_width = real_wid;
12454 f->output_data.x->right_fringe_width = 0;
12455 }
12456 else
12457 {
12458 f->output_data.x->left_fringe_width = 0;
12459 f->output_data.x->right_fringe_width = real_wid;
12460 }
12461 f->output_data.x->fringe_cols = cols;
12462 f->output_data.x->fringes_extra = real_wid;
12463 }
12464 else
12465 {
12466 f->output_data.x->left_fringe_width = 0;
12467 f->output_data.x->right_fringe_width = 0;
12468 f->output_data.x->fringe_cols = 0;
12469 f->output_data.x->fringes_extra = 0;
12470 }
12471
12472 if (redraw && FRAME_VISIBLE_P (f))
12473 if (o_left != f->output_data.x->left_fringe_width ||
12474 o_right != f->output_data.x->right_fringe_width ||
12475 o_cols != f->output_data.x->fringe_cols)
12476 redraw_frame (f);
12477}
f5d11644
GM
12478\f
12479/***********************************************************************
12480 X Input Methods
12481 ***********************************************************************/
12482
12483#ifdef HAVE_X_I18N
12484
12485#ifdef HAVE_X11R6
12486
12487/* XIM destroy callback function, which is called whenever the
12488 connection to input method XIM dies. CLIENT_DATA contains a
12489 pointer to the x_display_info structure corresponding to XIM. */
12490
12491static void
12492xim_destroy_callback (xim, client_data, call_data)
12493 XIM xim;
12494 XPointer client_data;
12495 XPointer call_data;
12496{
12497 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12498 Lisp_Object frame, tail;
12499
12500 BLOCK_INPUT;
12501
12502 /* No need to call XDestroyIC.. */
12503 FOR_EACH_FRAME (tail, frame)
12504 {
12505 struct frame *f = XFRAME (frame);
12506 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12507 {
12508 FRAME_XIC (f) = NULL;
12509 if (FRAME_XIC_FONTSET (f))
12510 {
12511 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12512 FRAME_XIC_FONTSET (f) = NULL;
12513 }
12514 }
12515 }
12516
12517 /* No need to call XCloseIM. */
12518 dpyinfo->xim = NULL;
12519 XFree (dpyinfo->xim_styles);
12520 UNBLOCK_INPUT;
12521}
12522
12523#endif /* HAVE_X11R6 */
12524
12525/* Open the connection to the XIM server on display DPYINFO.
12526 RESOURCE_NAME is the resource name Emacs uses. */
12527
12528static void
12529xim_open_dpy (dpyinfo, resource_name)
12530 struct x_display_info *dpyinfo;
12531 char *resource_name;
12532{
287f7dd6 12533#ifdef USE_XIM
f5d11644
GM
12534 XIM xim;
12535
12536 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12537 dpyinfo->xim = xim;
12538
12539 if (xim)
12540 {
f5d11644
GM
12541#ifdef HAVE_X11R6
12542 XIMCallback destroy;
12543#endif
12544
12545 /* Get supported styles and XIM values. */
12546 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12547
12548#ifdef HAVE_X11R6
12549 destroy.callback = xim_destroy_callback;
12550 destroy.client_data = (XPointer)dpyinfo;
cea2ad76 12551 /* This isn't prototyped in OSF 5.0. */
f5d11644
GM
12552 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12553#endif
12554 }
287f7dd6
GM
12555
12556#else /* not USE_XIM */
12557 dpyinfo->xim = NULL;
12558#endif /* not USE_XIM */
f5d11644
GM
12559}
12560
12561
b9de836c 12562#ifdef HAVE_X11R6_XIM
f5d11644
GM
12563
12564struct xim_inst_t
12565{
12566 struct x_display_info *dpyinfo;
12567 char *resource_name;
12568};
12569
12570/* XIM instantiate callback function, which is called whenever an XIM
12571 server is available. DISPLAY is teh display of the XIM.
12572 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12573 when the callback was registered. */
12574
12575static void
12576xim_instantiate_callback (display, client_data, call_data)
12577 Display *display;
12578 XPointer client_data;
12579 XPointer call_data;
12580{
12581 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12582 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12583
12584 /* We don't support multiple XIM connections. */
12585 if (dpyinfo->xim)
12586 return;
12587
12588 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12589
12590 /* Create XIC for the existing frames on the same display, as long
12591 as they have no XIC. */
12592 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12593 {
12594 Lisp_Object tail, frame;
12595
12596 BLOCK_INPUT;
12597 FOR_EACH_FRAME (tail, frame)
12598 {
12599 struct frame *f = XFRAME (frame);
12600
12601 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12602 if (FRAME_XIC (f) == NULL)
12603 {
12604 create_frame_xic (f);
12605 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12606 xic_set_statusarea (f);
12607 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12608 {
12609 struct window *w = XWINDOW (f->selected_window);
12610 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12611 }
12612 }
12613 }
12614
12615 UNBLOCK_INPUT;
12616 }
12617}
12618
b9de836c 12619#endif /* HAVE_X11R6_XIM */
f5d11644
GM
12620
12621
12622/* Open a connection to the XIM server on display DPYINFO.
12623 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12624 connection only at the first time. On X11R6, open the connection
12625 in the XIM instantiate callback function. */
12626
12627static void
12628xim_initialize (dpyinfo, resource_name)
12629 struct x_display_info *dpyinfo;
12630 char *resource_name;
12631{
287f7dd6 12632#ifdef USE_XIM
b9de836c 12633#ifdef HAVE_X11R6_XIM
f5d11644
GM
12634 struct xim_inst_t *xim_inst;
12635 int len;
12636
12637 dpyinfo->xim = NULL;
12638 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12639 xim_inst->dpyinfo = dpyinfo;
12640 len = strlen (resource_name);
12641 xim_inst->resource_name = (char *) xmalloc (len + 1);
12642 bcopy (resource_name, xim_inst->resource_name, len + 1);
12643 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12644 resource_name, EMACS_CLASS,
12645 xim_instantiate_callback,
2ebb2f8b
DL
12646 /* Fixme: This is XPointer in
12647 XFree86 but (XPointer *) on
12648 Tru64, at least. */
12649 (XPointer) xim_inst);
b9de836c 12650#else /* not HAVE_X11R6_XIM */
f5d11644
GM
12651 dpyinfo->xim = NULL;
12652 xim_open_dpy (dpyinfo, resource_name);
b9de836c 12653#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
12654
12655#else /* not USE_XIM */
12656 dpyinfo->xim = NULL;
12657#endif /* not USE_XIM */
f5d11644
GM
12658}
12659
12660
12661/* Close the connection to the XIM server on display DPYINFO. */
12662
12663static void
12664xim_close_dpy (dpyinfo)
12665 struct x_display_info *dpyinfo;
12666{
287f7dd6 12667#ifdef USE_XIM
b9de836c 12668#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
12669 if (dpyinfo->display)
12670 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12671 NULL, EMACS_CLASS,
12672 xim_instantiate_callback, NULL);
b9de836c 12673#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
12674 if (dpyinfo->display)
12675 XCloseIM (dpyinfo->xim);
f5d11644
GM
12676 dpyinfo->xim = NULL;
12677 XFree (dpyinfo->xim_styles);
287f7dd6 12678#endif /* USE_XIM */
f5d11644
GM
12679}
12680
b9de836c 12681#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
12682
12683
dc6f92b8 12684\f
2e365682
RS
12685/* Calculate the absolute position in frame F
12686 from its current recorded position values and gravity. */
12687
dfcf069d 12688void
43bca5d5 12689x_calc_absolute_position (f)
f676886a 12690 struct frame *f;
dc6f92b8 12691{
06a2c219 12692 Window child;
6dba1858 12693 int win_x = 0, win_y = 0;
7556890b 12694 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
12695 int this_window;
12696
9829ddba
RS
12697 /* We have nothing to do if the current position
12698 is already for the top-left corner. */
12699 if (! ((flags & XNegative) || (flags & YNegative)))
12700 return;
12701
c81412a0 12702#ifdef USE_X_TOOLKIT
7556890b 12703 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
12704#else
12705 this_window = FRAME_X_WINDOW (f);
12706#endif
6dba1858
RS
12707
12708 /* Find the position of the outside upper-left corner of
9829ddba
RS
12709 the inner window, with respect to the outer window.
12710 But do this only if we will need the results. */
7556890b 12711 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 12712 {
9829ddba
RS
12713 int count;
12714
6dba1858 12715 BLOCK_INPUT;
9829ddba
RS
12716 count = x_catch_errors (FRAME_X_DISPLAY (f));
12717 while (1)
12718 {
12719 x_clear_errors (FRAME_X_DISPLAY (f));
12720 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12721
12722 /* From-window, to-window. */
12723 this_window,
12724 f->output_data.x->parent_desc,
12725
12726 /* From-position, to-position. */
12727 0, 0, &win_x, &win_y,
12728
12729 /* Child of win. */
12730 &child);
12731 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12732 {
12733 Window newroot, newparent = 0xdeadbeef;
12734 Window *newchildren;
2ebb2f8b 12735 unsigned int nchildren;
9829ddba
RS
12736
12737 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12738 &newparent, &newchildren, &nchildren))
12739 break;
58769bee 12740
7c3c78a3 12741 XFree ((char *) newchildren);
6dba1858 12742
9829ddba
RS
12743 f->output_data.x->parent_desc = newparent;
12744 }
12745 else
12746 break;
12747 }
6dba1858 12748
9829ddba 12749 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
12750 UNBLOCK_INPUT;
12751 }
12752
12753 /* Treat negative positions as relative to the leftmost bottommost
12754 position that fits on the screen. */
20f55f9a 12755 if (flags & XNegative)
7556890b 12756 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
12757 - 2 * f->output_data.x->border_width - win_x
12758 - PIXEL_WIDTH (f)
12759 + f->output_data.x->left_pos);
dc6f92b8 12760
7708ced0
GM
12761 {
12762 int height = PIXEL_HEIGHT (f);
06a2c219 12763
7708ced0
GM
12764#if defined USE_X_TOOLKIT && defined USE_MOTIF
12765 /* Something is fishy here. When using Motif, starting Emacs with
12766 `-g -0-0', the frame appears too low by a few pixels.
12767
12768 This seems to be so because initially, while Emacs is starting,
12769 the column widget's height and the frame's pixel height are
12770 different. The column widget's height is the right one. In
12771 later invocations, when Emacs is up, the frame's pixel height
12772 is right, though.
12773
12774 It's not obvious where the initial small difference comes from.
12775 2000-12-01, gerd. */
12776
12777 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 12778#endif
2e365682 12779
7708ced0
GM
12780 if (flags & YNegative)
12781 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12782 - 2 * f->output_data.x->border_width
12783 - win_y
12784 - height
12785 + f->output_data.x->top_pos);
12786 }
12787
3a35ab44
RS
12788 /* The left_pos and top_pos
12789 are now relative to the top and left screen edges,
12790 so the flags should correspond. */
7556890b 12791 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
12792}
12793
3a35ab44
RS
12794/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12795 to really change the position, and 0 when calling from
12796 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
12797 position values). It is -1 when calling from x_set_frame_parameters,
12798 which means, do adjust for borders but don't change the gravity. */
3a35ab44 12799
dfcf069d 12800void
dc05a16b 12801x_set_offset (f, xoff, yoff, change_gravity)
f676886a 12802 struct frame *f;
dc6f92b8 12803 register int xoff, yoff;
dc05a16b 12804 int change_gravity;
dc6f92b8 12805{
4a4cbdd5
KH
12806 int modified_top, modified_left;
12807
aa3ff7c9 12808 if (change_gravity > 0)
3a35ab44 12809 {
7556890b
RS
12810 f->output_data.x->top_pos = yoff;
12811 f->output_data.x->left_pos = xoff;
12812 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 12813 if (xoff < 0)
7556890b 12814 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 12815 if (yoff < 0)
7556890b
RS
12816 f->output_data.x->size_hint_flags |= YNegative;
12817 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 12818 }
43bca5d5 12819 x_calc_absolute_position (f);
dc6f92b8
JB
12820
12821 BLOCK_INPUT;
c32cdd9a 12822 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 12823
7556890b
RS
12824 modified_left = f->output_data.x->left_pos;
12825 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
12826#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12827 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12828 /* It is a mystery why we need to add the border_width here
12829 when the frame is already visible, but experiment says we do. */
aa3ff7c9 12830 if (change_gravity != 0)
4a4cbdd5 12831 {
7556890b
RS
12832 modified_left += f->output_data.x->border_width;
12833 modified_top += f->output_data.x->border_width;
4a4cbdd5 12834 }
e73ec6fa 12835#endif
4a4cbdd5 12836
3afe33e7 12837#ifdef USE_X_TOOLKIT
7556890b 12838 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 12839 modified_left, modified_top);
3afe33e7 12840#else /* not USE_X_TOOLKIT */
334208b7 12841 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 12842 modified_left, modified_top);
3afe33e7 12843#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12844 UNBLOCK_INPUT;
12845}
12846
d0fa3e56
EZ
12847/* Check if we need to resize the frame due to a fullscreen request.
12848 If so needed, resize the frame. */
12849static void
12850x_check_fullscreen (f)
12851 struct frame *f;
12852{
12853 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12854 {
12855 int width, height, ign;
12856
12857 x_real_positions (f, &f->output_data.x->left_pos,
12858 &f->output_data.x->top_pos);
12859
12860 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12861
12862 /* We do not need to move the window, it shall be taken care of
12863 when setting WM manager hints.
12864 If the frame is visible already, the position is checked by
12865 x_check_fullscreen_move. */
12866 if (f->width != width || f->height != height)
12867 {
12868 change_frame_size (f, height, width, 0, 1, 0);
12869 SET_FRAME_GARBAGED (f);
12870 cancel_mouse_face (f);
12871
12872 /* Wait for the change of frame size to occur */
12873 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12874
12875 }
12876 }
12877}
12878
12879/* If frame parameters are set after the frame is mapped, we need to move
12880 the window. This is done in xfns.c.
12881 Some window managers moves the window to the right position, some
12882 moves the outer window manager window to the specified position.
12883 Here we check that we are in the right spot. If not, make a second
12884 move, assuming we are dealing with the second kind of window manager. */
12885static void
12886x_check_fullscreen_move (f)
12887 struct frame *f;
12888{
12889 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12890 {
12891 int expect_top = f->output_data.x->top_pos;
12892 int expect_left = f->output_data.x->left_pos;
12893
12894 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12895 expect_top = 0;
12896 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12897 expect_left = 0;
12898
12899 if (expect_top != f->output_data.x->top_pos
12900 || expect_left != f->output_data.x->left_pos)
12901 x_set_offset (f, expect_left, expect_top, 1);
12902
12903 /* Just do this once */
12904 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12905 }
12906}
12907
12908
12909/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12910 wanted positions of the WM window (not emacs window).
12911 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12912 window (FRAME_X_WINDOW).
12913 */
12914void
12915x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12916 struct frame *f;
12917 int *width;
12918 int *height;
12919 int *top_pos;
12920 int *left_pos;
12921{
12922 int newwidth = f->width, newheight = f->height;
12923
12924 *top_pos = f->output_data.x->top_pos;
12925 *left_pos = f->output_data.x->left_pos;
12926
12927 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12928 {
12929 int ph;
12930
12931 ph = FRAME_X_DISPLAY_INFO (f)->height;
12932 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12933 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12934 - f->output_data.x->y_pixels_diff;
12935 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12936 *top_pos = 0;
12937 }
12938
12939 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12940 {
12941 int pw;
12942
12943 pw = FRAME_X_DISPLAY_INFO (f)->width;
12944 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12945 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12946 - f->output_data.x->x_pixels_diff;
12947 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12948 *left_pos = 0;
12949 }
12950
12951 *width = newwidth;
12952 *height = newheight;
12953}
12954
dc6f92b8 12955
499b1844
GM
12956/* Change the size of frame F's X window to COLS/ROWS in the case F
12957 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12958 top-left-corner window gravity for this size change and subsequent
12959 size changes. Otherwise we leave the window gravity unchanged. */
12960
12961static void
12962x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 12963 struct frame *f;
bc20ebbf 12964 int change_gravity;
b1c884c3 12965 int cols, rows;
dc6f92b8
JB
12966{
12967 int pixelwidth, pixelheight;
80fd1fe2 12968
b1c884c3 12969 check_frame_size (f, &rows, &cols);
7556890b 12970 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
12971 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12972 ? 0
12973 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 12974 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 12975 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
976b73d7
KS
12976
12977 x_compute_fringe_widths (f, 0);
12978
f451eb13
JB
12979 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12980 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 12981
7556890b 12982 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 12983 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 12984
334208b7
RS
12985 XSync (FRAME_X_DISPLAY (f), False);
12986 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12987 pixelwidth, pixelheight);
b1c884c3
JB
12988
12989 /* Now, strictly speaking, we can't be sure that this is accurate,
12990 but the window manager will get around to dealing with the size
12991 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
12992 ConfigureNotify event gets here.
12993
12994 We could just not bother storing any of this information here,
12995 and let the ConfigureNotify event set everything up, but that
fddd5ceb 12996 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 12997 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
12998 point in the future when the ConfigureNotify event arrives.
12999
13000 We pass 1 for DELAY since we can't run Lisp code inside of
13001 a BLOCK_INPUT. */
7d1e984f 13002 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
13003 PIXEL_WIDTH (f) = pixelwidth;
13004 PIXEL_HEIGHT (f) = pixelheight;
13005
aee9a898
RS
13006 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
13007 receive in the ConfigureNotify event; if we get what we asked
13008 for, then the event won't cause the screen to become garbaged, so
13009 we have to make sure to do it here. */
13010 SET_FRAME_GARBAGED (f);
13011
13012 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
13013}
13014
13015
13016/* Call this to change the size of frame F's x-window.
13017 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
13018 for this size change and subsequent size changes.
13019 Otherwise we leave the window gravity unchanged. */
aee9a898 13020
499b1844
GM
13021void
13022x_set_window_size (f, change_gravity, cols, rows)
13023 struct frame *f;
13024 int change_gravity;
13025 int cols, rows;
13026{
13027 BLOCK_INPUT;
13028
13029#ifdef USE_X_TOOLKIT
13030
f1f4d345 13031 if (f->output_data.x->widget != NULL)
499b1844
GM
13032 {
13033 /* The x and y position of the widget is clobbered by the
13034 call to XtSetValues within EmacsFrameSetCharSize.
13035 This is a real kludge, but I don't understand Xt so I can't
13036 figure out a correct fix. Can anyone else tell me? -- rms. */
13037 int xpos = f->output_data.x->widget->core.x;
13038 int ypos = f->output_data.x->widget->core.y;
13039 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13040 f->output_data.x->widget->core.x = xpos;
13041 f->output_data.x->widget->core.y = ypos;
13042 }
13043 else
13044 x_set_window_size_1 (f, change_gravity, cols, rows);
13045
13046#else /* not USE_X_TOOLKIT */
13047
13048 x_set_window_size_1 (f, change_gravity, cols, rows);
13049
aee9a898
RS
13050#endif /* not USE_X_TOOLKIT */
13051
4d73d038 13052 /* If cursor was outside the new size, mark it as off. */
06a2c219 13053 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 13054
aee9a898
RS
13055 /* Clear out any recollection of where the mouse highlighting was,
13056 since it might be in a place that's outside the new frame size.
13057 Actually checking whether it is outside is a pain in the neck,
13058 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 13059 cancel_mouse_face (f);
dbc4e1c1 13060
dc6f92b8
JB
13061 UNBLOCK_INPUT;
13062}
dc6f92b8 13063\f
d047c4eb 13064/* Mouse warping. */
dc6f92b8 13065
9b378208 13066void
f676886a
JB
13067x_set_mouse_position (f, x, y)
13068 struct frame *f;
dc6f92b8
JB
13069 int x, y;
13070{
13071 int pix_x, pix_y;
13072
7556890b
RS
13073 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13074 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
13075
13076 if (pix_x < 0) pix_x = 0;
13077 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13078
13079 if (pix_y < 0) pix_y = 0;
13080 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
13081
13082 BLOCK_INPUT;
dc6f92b8 13083
334208b7
RS
13084 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13085 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
13086 UNBLOCK_INPUT;
13087}
13088
9b378208
RS
13089/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13090
13091void
13092x_set_mouse_pixel_position (f, pix_x, pix_y)
13093 struct frame *f;
13094 int pix_x, pix_y;
13095{
13096 BLOCK_INPUT;
13097
334208b7
RS
13098 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13099 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
13100 UNBLOCK_INPUT;
13101}
d047c4eb
KH
13102\f
13103/* focus shifting, raising and lowering. */
9b378208 13104
dfcf069d 13105void
f676886a
JB
13106x_focus_on_frame (f)
13107 struct frame *f;
dc6f92b8 13108{
1fb20991 13109#if 0 /* This proves to be unpleasant. */
f676886a 13110 x_raise_frame (f);
1fb20991 13111#endif
6d4238f3
JB
13112#if 0
13113 /* I don't think that the ICCCM allows programs to do things like this
13114 without the interaction of the window manager. Whatever you end up
f676886a 13115 doing with this code, do it to x_unfocus_frame too. */
334208b7 13116 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 13117 RevertToPointerRoot, CurrentTime);
c118dd06 13118#endif /* ! 0 */
dc6f92b8
JB
13119}
13120
dfcf069d 13121void
f676886a
JB
13122x_unfocus_frame (f)
13123 struct frame *f;
dc6f92b8 13124{
6d4238f3 13125#if 0
f676886a 13126 /* Look at the remarks in x_focus_on_frame. */
0f941935 13127 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 13128 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 13129 RevertToPointerRoot, CurrentTime);
c118dd06 13130#endif /* ! 0 */
dc6f92b8
JB
13131}
13132
f676886a 13133/* Raise frame F. */
dc6f92b8 13134
dfcf069d 13135void
f676886a
JB
13136x_raise_frame (f)
13137 struct frame *f;
dc6f92b8 13138{
3a88c238 13139 if (f->async_visible)
dc6f92b8
JB
13140 {
13141 BLOCK_INPUT;
3afe33e7 13142#ifdef USE_X_TOOLKIT
7556890b 13143 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13144#else /* not USE_X_TOOLKIT */
334208b7 13145 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13146#endif /* not USE_X_TOOLKIT */
334208b7 13147 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13148 UNBLOCK_INPUT;
13149 }
13150}
13151
f676886a 13152/* Lower frame F. */
dc6f92b8 13153
dfcf069d 13154void
f676886a
JB
13155x_lower_frame (f)
13156 struct frame *f;
dc6f92b8 13157{
3a88c238 13158 if (f->async_visible)
dc6f92b8
JB
13159 {
13160 BLOCK_INPUT;
3afe33e7 13161#ifdef USE_X_TOOLKIT
7556890b 13162 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13163#else /* not USE_X_TOOLKIT */
334208b7 13164 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13165#endif /* not USE_X_TOOLKIT */
334208b7 13166 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13167 UNBLOCK_INPUT;
13168 }
13169}
13170
dbc4e1c1 13171static void
6b0442dc 13172XTframe_raise_lower (f, raise_flag)
dbc4e1c1 13173 FRAME_PTR f;
6b0442dc 13174 int raise_flag;
dbc4e1c1 13175{
6b0442dc 13176 if (raise_flag)
dbc4e1c1
JB
13177 x_raise_frame (f);
13178 else
13179 x_lower_frame (f);
13180}
d047c4eb
KH
13181\f
13182/* Change of visibility. */
dc6f92b8 13183
9382638d
KH
13184/* This tries to wait until the frame is really visible.
13185 However, if the window manager asks the user where to position
13186 the frame, this will return before the user finishes doing that.
13187 The frame will not actually be visible at that time,
13188 but it will become visible later when the window manager
13189 finishes with it. */
13190
dfcf069d 13191void
f676886a
JB
13192x_make_frame_visible (f)
13193 struct frame *f;
dc6f92b8 13194{
990ba854 13195 Lisp_Object type;
1aa6072f 13196 int original_top, original_left;
31be9251
GM
13197 int retry_count = 2;
13198
13199 retry:
dc6f92b8 13200
dc6f92b8 13201 BLOCK_INPUT;
dc6f92b8 13202
990ba854
RS
13203 type = x_icon_type (f);
13204 if (!NILP (type))
13205 x_bitmap_icon (f, type);
bdcd49ba 13206
f676886a 13207 if (! FRAME_VISIBLE_P (f))
90e65f07 13208 {
1aa6072f
RS
13209 /* We test FRAME_GARBAGED_P here to make sure we don't
13210 call x_set_offset a second time
13211 if we get to x_make_frame_visible a second time
13212 before the window gets really visible. */
13213 if (! FRAME_ICONIFIED_P (f)
13214 && ! f->output_data.x->asked_for_visible)
13215 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13216
13217 f->output_data.x->asked_for_visible = 1;
13218
90e65f07 13219 if (! EQ (Vx_no_window_manager, Qt))
f676886a 13220 x_wm_set_window_state (f, NormalState);
3afe33e7 13221#ifdef USE_X_TOOLKIT
d7a38a2e 13222 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13223 XtMapWidget (f->output_data.x->widget);
3afe33e7 13224#else /* not USE_X_TOOLKIT */
7f9c7f94 13225 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13226#endif /* not USE_X_TOOLKIT */
0134a210
RS
13227#if 0 /* This seems to bring back scroll bars in the wrong places
13228 if the window configuration has changed. They seem
13229 to come back ok without this. */
ab648270 13230 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 13231 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 13232#endif
90e65f07 13233 }
dc6f92b8 13234
334208b7 13235 XFlush (FRAME_X_DISPLAY (f));
90e65f07 13236
0dacf791
RS
13237 /* Synchronize to ensure Emacs knows the frame is visible
13238 before we do anything else. We do this loop with input not blocked
13239 so that incoming events are handled. */
13240 {
13241 Lisp_Object frame;
12ce2351 13242 int count;
28c01ffe
RS
13243 /* This must be before UNBLOCK_INPUT
13244 since events that arrive in response to the actions above
13245 will set it when they are handled. */
13246 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
13247
13248 original_left = f->output_data.x->left_pos;
13249 original_top = f->output_data.x->top_pos;
c0a04927
RS
13250
13251 /* This must come after we set COUNT. */
13252 UNBLOCK_INPUT;
13253
2745e6c4 13254 /* We unblock here so that arriving X events are processed. */
1aa6072f 13255
dcb07ae9
RS
13256 /* Now move the window back to where it was "supposed to be".
13257 But don't do it if the gravity is negative.
13258 When the gravity is negative, this uses a position
28c01ffe
RS
13259 that is 3 pixels too low. Perhaps that's really the border width.
13260
13261 Don't do this if the window has never been visible before,
13262 because the window manager may choose the position
13263 and we don't want to override it. */
1aa6072f 13264
4d3f5d9a 13265 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 13266 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 13267 && previously_visible)
1aa6072f 13268 {
2745e6c4
RS
13269 Drawable rootw;
13270 int x, y;
13271 unsigned int width, height, border, depth;
06a2c219 13272
1aa6072f 13273 BLOCK_INPUT;
9829ddba 13274
06a2c219
GM
13275 /* On some window managers (such as FVWM) moving an existing
13276 window, even to the same place, causes the window manager
13277 to introduce an offset. This can cause the window to move
13278 to an unexpected location. Check the geometry (a little
13279 slow here) and then verify that the window is in the right
13280 place. If the window is not in the right place, move it
13281 there, and take the potential window manager hit. */
2745e6c4
RS
13282 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13283 &rootw, &x, &y, &width, &height, &border, &depth);
13284
13285 if (original_left != x || original_top != y)
13286 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13287 original_left, original_top);
13288
1aa6072f
RS
13289 UNBLOCK_INPUT;
13290 }
9829ddba 13291
e0c1aef2 13292 XSETFRAME (frame, f);
c0a04927 13293
12ce2351
GM
13294 /* Wait until the frame is visible. Process X events until a
13295 MapNotify event has been seen, or until we think we won't get a
13296 MapNotify at all.. */
13297 for (count = input_signal_count + 10;
13298 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 13299 {
12ce2351 13300 /* Force processing of queued events. */
334208b7 13301 x_sync (f);
12ce2351
GM
13302
13303 /* Machines that do polling rather than SIGIO have been
13304 observed to go into a busy-wait here. So we'll fake an
13305 alarm signal to let the handler know that there's something
13306 to be read. We used to raise a real alarm, but it seems
13307 that the handler isn't always enabled here. This is
13308 probably a bug. */
8b2f8d4e 13309 if (input_polling_used ())
3b2fa4e6 13310 {
12ce2351
GM
13311 /* It could be confusing if a real alarm arrives while
13312 processing the fake one. Turn it off and let the
13313 handler reset it. */
3e71d8f2 13314 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
13315 int old_poll_suppress_count = poll_suppress_count;
13316 poll_suppress_count = 1;
13317 poll_for_input_1 ();
13318 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 13319 }
12ce2351
GM
13320
13321 /* See if a MapNotify event has been processed. */
13322 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 13323 }
31be9251
GM
13324
13325 /* 2000-09-28: In
13326
13327 (let ((f (selected-frame)))
13328 (iconify-frame f)
13329 (raise-frame f))
13330
13331 the frame is not raised with various window managers on
13332 FreeBSD, Linux and Solaris. It turns out that, for some
13333 unknown reason, the call to XtMapWidget is completely ignored.
13334 Mapping the widget a second time works. */
13335
13336 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13337 goto retry;
0dacf791 13338 }
dc6f92b8
JB
13339}
13340
06a2c219 13341/* Change from mapped state to withdrawn state. */
dc6f92b8 13342
d047c4eb
KH
13343/* Make the frame visible (mapped and not iconified). */
13344
dfcf069d 13345void
f676886a
JB
13346x_make_frame_invisible (f)
13347 struct frame *f;
dc6f92b8 13348{
546e6d5b
RS
13349 Window window;
13350
13351#ifdef USE_X_TOOLKIT
13352 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 13353 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
13354#else /* not USE_X_TOOLKIT */
13355 window = FRAME_X_WINDOW (f);
13356#endif /* not USE_X_TOOLKIT */
dc6f92b8 13357
9319ae23 13358 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13359 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13360 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13361
5627c40e 13362#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 13363 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 13364 return;
5627c40e 13365#endif
dc6f92b8
JB
13366
13367 BLOCK_INPUT;
c118dd06 13368
af31d76f
RS
13369 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13370 that the current position of the window is user-specified, rather than
13371 program-specified, so that when the window is mapped again, it will be
13372 placed at the same location, without forcing the user to position it
13373 by hand again (they have already done that once for this window.) */
c32cdd9a 13374 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 13375
c118dd06
JB
13376#ifdef HAVE_X11R4
13377
334208b7
RS
13378 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13379 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
13380 {
13381 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13382 error ("Can't notify window manager of window withdrawal");
c118dd06 13383 }
c118dd06 13384#else /* ! defined (HAVE_X11R4) */
16bd92ea 13385
c118dd06 13386 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
13387 if (! EQ (Vx_no_window_manager, Qt))
13388 {
16bd92ea 13389 XEvent unmap;
dc6f92b8 13390
16bd92ea 13391 unmap.xunmap.type = UnmapNotify;
546e6d5b 13392 unmap.xunmap.window = window;
334208b7 13393 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 13394 unmap.xunmap.from_configure = False;
334208b7
RS
13395 if (! XSendEvent (FRAME_X_DISPLAY (f),
13396 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 13397 False,
06a2c219 13398 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
13399 &unmap))
13400 {
13401 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13402 error ("Can't notify window manager of withdrawal");
16bd92ea 13403 }
dc6f92b8
JB
13404 }
13405
16bd92ea 13406 /* Unmap the window ourselves. Cheeky! */
334208b7 13407 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 13408#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 13409
5627c40e
RS
13410 /* We can't distinguish this from iconification
13411 just by the event that we get from the server.
13412 So we can't win using the usual strategy of letting
13413 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13414 and synchronize with the server to make sure we agree. */
13415 f->visible = 0;
13416 FRAME_ICONIFIED_P (f) = 0;
13417 f->async_visible = 0;
13418 f->async_iconified = 0;
13419
334208b7 13420 x_sync (f);
5627c40e 13421
dc6f92b8
JB
13422 UNBLOCK_INPUT;
13423}
13424
06a2c219 13425/* Change window state from mapped to iconified. */
dc6f92b8 13426
dfcf069d 13427void
f676886a
JB
13428x_iconify_frame (f)
13429 struct frame *f;
dc6f92b8 13430{
3afe33e7 13431 int result;
990ba854 13432 Lisp_Object type;
dc6f92b8 13433
9319ae23 13434 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13435 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13436 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13437
3a88c238 13438 if (f->async_iconified)
dc6f92b8
JB
13439 return;
13440
3afe33e7 13441 BLOCK_INPUT;
546e6d5b 13442
9af3143a
RS
13443 FRAME_SAMPLE_VISIBILITY (f);
13444
990ba854
RS
13445 type = x_icon_type (f);
13446 if (!NILP (type))
13447 x_bitmap_icon (f, type);
bdcd49ba
RS
13448
13449#ifdef USE_X_TOOLKIT
13450
546e6d5b
RS
13451 if (! FRAME_VISIBLE_P (f))
13452 {
13453 if (! EQ (Vx_no_window_manager, Qt))
13454 x_wm_set_window_state (f, IconicState);
13455 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13456 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
13457 /* The server won't give us any event to indicate
13458 that an invisible frame was changed to an icon,
13459 so we have to record it here. */
13460 f->iconified = 1;
1e6bc770 13461 f->visible = 1;
9cf30a30 13462 f->async_iconified = 1;
1e6bc770 13463 f->async_visible = 0;
546e6d5b
RS
13464 UNBLOCK_INPUT;
13465 return;
13466 }
13467
334208b7 13468 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 13469 XtWindow (f->output_data.x->widget),
334208b7 13470 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
13471 UNBLOCK_INPUT;
13472
13473 if (!result)
546e6d5b 13474 error ("Can't notify window manager of iconification");
3afe33e7
RS
13475
13476 f->async_iconified = 1;
1e6bc770
RS
13477 f->async_visible = 0;
13478
8c002a25
KH
13479
13480 BLOCK_INPUT;
334208b7 13481 XFlush (FRAME_X_DISPLAY (f));
8c002a25 13482 UNBLOCK_INPUT;
3afe33e7
RS
13483#else /* not USE_X_TOOLKIT */
13484
fd13dbb2
RS
13485 /* Make sure the X server knows where the window should be positioned,
13486 in case the user deiconifies with the window manager. */
13487 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 13488 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 13489
16bd92ea
JB
13490 /* Since we don't know which revision of X we're running, we'll use both
13491 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13492
13493 /* X11R4: send a ClientMessage to the window manager using the
13494 WM_CHANGE_STATE type. */
13495 {
13496 XEvent message;
58769bee 13497
c118dd06 13498 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 13499 message.xclient.type = ClientMessage;
334208b7 13500 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
13501 message.xclient.format = 32;
13502 message.xclient.data.l[0] = IconicState;
13503
334208b7
RS
13504 if (! XSendEvent (FRAME_X_DISPLAY (f),
13505 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
13506 False,
13507 SubstructureRedirectMask | SubstructureNotifyMask,
13508 &message))
dc6f92b8
JB
13509 {
13510 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13511 error ("Can't notify window manager of iconification");
dc6f92b8 13512 }
16bd92ea 13513 }
dc6f92b8 13514
58769bee 13515 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
13516 IconicState. */
13517 x_wm_set_window_state (f, IconicState);
dc6f92b8 13518
a9c00105
RS
13519 if (!FRAME_VISIBLE_P (f))
13520 {
13521 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 13522 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
13523 }
13524
3a88c238 13525 f->async_iconified = 1;
1e6bc770 13526 f->async_visible = 0;
dc6f92b8 13527
334208b7 13528 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 13529 UNBLOCK_INPUT;
8c002a25 13530#endif /* not USE_X_TOOLKIT */
dc6f92b8 13531}
19f71add 13532
d047c4eb 13533\f
19f71add 13534/* Free X resources of frame F. */
dc6f92b8 13535
dfcf069d 13536void
19f71add 13537x_free_frame_resources (f)
f676886a 13538 struct frame *f;
dc6f92b8 13539{
7f9c7f94 13540 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
13541 Lisp_Object bar;
13542 struct scroll_bar *b;
7f9c7f94 13543
dc6f92b8 13544 BLOCK_INPUT;
c0ff3fab 13545
6186a4a0
RS
13546 /* If a display connection is dead, don't try sending more
13547 commands to the X server. */
19f71add 13548 if (dpyinfo->display)
6186a4a0 13549 {
19f71add 13550 if (f->output_data.x->icon_desc)
6186a4a0 13551 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
13552
13553#ifdef USE_X_TOOLKIT
13554 /* Explicitly destroy the scroll bars of the frame. Without
13555 this, we get "BadDrawable" errors from the toolkit later on,
13556 presumably from expose events generated for the disappearing
13557 toolkit scroll bars. */
13558 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13559 {
13560 b = XSCROLL_BAR (bar);
13561 x_scroll_bar_remove (b);
13562 }
13563#endif
13564
31f41daf 13565#ifdef HAVE_X_I18N
f5d11644
GM
13566 if (FRAME_XIC (f))
13567 free_frame_xic (f);
31f41daf 13568#endif
c6ea2775 13569
3afe33e7 13570#ifdef USE_X_TOOLKIT
06a2c219 13571 if (f->output_data.x->widget)
30ca89f5
GM
13572 {
13573 XtDestroyWidget (f->output_data.x->widget);
13574 f->output_data.x->widget = NULL;
13575 }
c6ea2775
EZ
13576 /* Tooltips don't have widgets, only a simple X window, even if
13577 we are using a toolkit. */
13578 else if (FRAME_X_WINDOW (f))
13579 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13580
6186a4a0 13581 free_frame_menubar (f);
c6ea2775
EZ
13582#else /* !USE_X_TOOLKIT */
13583 if (FRAME_X_WINDOW (f))
13584 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13585#endif /* !USE_X_TOOLKIT */
3afe33e7 13586
3e71d8f2
GM
13587 unload_color (f, f->output_data.x->foreground_pixel);
13588 unload_color (f, f->output_data.x->background_pixel);
13589 unload_color (f, f->output_data.x->cursor_pixel);
13590 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13591 unload_color (f, f->output_data.x->border_pixel);
13592 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 13593
3e71d8f2
GM
13594 if (f->output_data.x->scroll_bar_background_pixel != -1)
13595 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13596 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13597 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
13598#ifdef USE_TOOLKIT_SCROLL_BARS
13599 /* Scrollbar shadow colors. */
13600 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13601 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13602 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13603 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13604#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
13605 if (f->output_data.x->white_relief.allocated_p)
13606 unload_color (f, f->output_data.x->white_relief.pixel);
13607 if (f->output_data.x->black_relief.allocated_p)
13608 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 13609
19f71add
GM
13610 if (FRAME_FACE_CACHE (f))
13611 free_frame_faces (f);
13612
4ca78676 13613 x_free_gcs (f);
6186a4a0
RS
13614 XFlush (FRAME_X_DISPLAY (f));
13615 }
dc6f92b8 13616
df89d8a4 13617 if (f->output_data.x->saved_menu_event)
06a2c219 13618 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 13619
7556890b 13620 xfree (f->output_data.x);
19f71add
GM
13621 f->output_data.x = NULL;
13622
0f941935
KH
13623 if (f == dpyinfo->x_focus_frame)
13624 dpyinfo->x_focus_frame = 0;
13625 if (f == dpyinfo->x_focus_event_frame)
13626 dpyinfo->x_focus_event_frame = 0;
13627 if (f == dpyinfo->x_highlight_frame)
13628 dpyinfo->x_highlight_frame = 0;
c0ff3fab 13629
7f9c7f94 13630 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 13631 {
7f9c7f94
RS
13632 dpyinfo->mouse_face_beg_row
13633 = dpyinfo->mouse_face_beg_col = -1;
13634 dpyinfo->mouse_face_end_row
13635 = dpyinfo->mouse_face_end_col = -1;
13636 dpyinfo->mouse_face_window = Qnil;
21323706
RS
13637 dpyinfo->mouse_face_deferred_gc = 0;
13638 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 13639 }
0134a210 13640
c0ff3fab 13641 UNBLOCK_INPUT;
dc6f92b8 13642}
19f71add
GM
13643
13644
13645/* Destroy the X window of frame F. */
13646
13647void
13648x_destroy_window (f)
13649 struct frame *f;
13650{
13651 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13652
13653 /* If a display connection is dead, don't try sending more
13654 commands to the X server. */
13655 if (dpyinfo->display != 0)
13656 x_free_frame_resources (f);
13657
13658 dpyinfo->reference_count--;
13659}
13660
dc6f92b8 13661\f
f451eb13
JB
13662/* Setting window manager hints. */
13663
af31d76f
RS
13664/* Set the normal size hints for the window manager, for frame F.
13665 FLAGS is the flags word to use--or 0 meaning preserve the flags
13666 that the window now has.
13667 If USER_POSITION is nonzero, we set the USPosition
13668 flag (this is useful when FLAGS is 0). */
6dba1858 13669
dfcf069d 13670void
af31d76f 13671x_wm_set_size_hint (f, flags, user_position)
f676886a 13672 struct frame *f;
af31d76f
RS
13673 long flags;
13674 int user_position;
dc6f92b8
JB
13675{
13676 XSizeHints size_hints;
3afe33e7
RS
13677
13678#ifdef USE_X_TOOLKIT
7e4f2521
FP
13679 Arg al[2];
13680 int ac = 0;
13681 Dimension widget_width, widget_height;
7556890b 13682 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 13683#else /* not USE_X_TOOLKIT */
c118dd06 13684 Window window = FRAME_X_WINDOW (f);
3afe33e7 13685#endif /* not USE_X_TOOLKIT */
dc6f92b8 13686
b72a58fd
RS
13687 /* Setting PMaxSize caused various problems. */
13688 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 13689
7556890b
RS
13690 size_hints.x = f->output_data.x->left_pos;
13691 size_hints.y = f->output_data.x->top_pos;
7553a6b7 13692
7e4f2521
FP
13693#ifdef USE_X_TOOLKIT
13694 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13695 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 13696 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
13697 size_hints.height = widget_height;
13698 size_hints.width = widget_width;
13699#else /* not USE_X_TOOLKIT */
f676886a
JB
13700 size_hints.height = PIXEL_HEIGHT (f);
13701 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 13702#endif /* not USE_X_TOOLKIT */
7553a6b7 13703
7556890b
RS
13704 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13705 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
13706 size_hints.max_width
13707 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13708 size_hints.max_height
13709 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 13710
d067ea8b
KH
13711 /* Calculate the base and minimum sizes.
13712
13713 (When we use the X toolkit, we don't do it here.
13714 Instead we copy the values that the widgets are using, below.) */
13715#ifndef USE_X_TOOLKIT
b1c884c3 13716 {
b0342f17 13717 int base_width, base_height;
0134a210 13718 int min_rows = 0, min_cols = 0;
b0342f17 13719
f451eb13
JB
13720 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13721 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 13722
0134a210 13723 check_frame_size (f, &min_rows, &min_cols);
b0342f17 13724
0134a210
RS
13725 /* The window manager uses the base width hints to calculate the
13726 current number of rows and columns in the frame while
13727 resizing; min_width and min_height aren't useful for this
13728 purpose, since they might not give the dimensions for a
13729 zero-row, zero-column frame.
58769bee 13730
0134a210
RS
13731 We use the base_width and base_height members if we have
13732 them; otherwise, we set the min_width and min_height members
13733 to the size for a zero x zero frame. */
b0342f17
JB
13734
13735#ifdef HAVE_X11R4
0134a210
RS
13736 size_hints.flags |= PBaseSize;
13737 size_hints.base_width = base_width;
13738 size_hints.base_height = base_height;
13739 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13740 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 13741#else
0134a210
RS
13742 size_hints.min_width = base_width;
13743 size_hints.min_height = base_height;
b0342f17 13744#endif
b1c884c3 13745 }
dc6f92b8 13746
d067ea8b 13747 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 13748 if (flags)
dc6f92b8 13749 {
d067ea8b
KH
13750 size_hints.flags |= flags;
13751 goto no_read;
13752 }
13753#endif /* not USE_X_TOOLKIT */
13754
13755 {
13756 XSizeHints hints; /* Sometimes I hate X Windows... */
13757 long supplied_return;
13758 int value;
af31d76f
RS
13759
13760#ifdef HAVE_X11R4
d067ea8b
KH
13761 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13762 &supplied_return);
af31d76f 13763#else
d067ea8b 13764 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 13765#endif
58769bee 13766
d067ea8b
KH
13767#ifdef USE_X_TOOLKIT
13768 size_hints.base_height = hints.base_height;
13769 size_hints.base_width = hints.base_width;
13770 size_hints.min_height = hints.min_height;
13771 size_hints.min_width = hints.min_width;
13772#endif
13773
13774 if (flags)
13775 size_hints.flags |= flags;
13776 else
13777 {
13778 if (value == 0)
13779 hints.flags = 0;
13780 if (hints.flags & PSize)
13781 size_hints.flags |= PSize;
13782 if (hints.flags & PPosition)
13783 size_hints.flags |= PPosition;
13784 if (hints.flags & USPosition)
13785 size_hints.flags |= USPosition;
13786 if (hints.flags & USSize)
13787 size_hints.flags |= USSize;
13788 }
13789 }
13790
06a2c219 13791#ifndef USE_X_TOOLKIT
d067ea8b 13792 no_read:
06a2c219 13793#endif
0134a210 13794
af31d76f 13795#ifdef PWinGravity
7556890b 13796 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 13797 size_hints.flags |= PWinGravity;
dc05a16b 13798
af31d76f 13799 if (user_position)
6dba1858 13800 {
af31d76f
RS
13801 size_hints.flags &= ~ PPosition;
13802 size_hints.flags |= USPosition;
6dba1858 13803 }
2554751d 13804#endif /* PWinGravity */
6dba1858 13805
b0342f17 13806#ifdef HAVE_X11R4
334208b7 13807 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13808#else
334208b7 13809 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13810#endif
dc6f92b8
JB
13811}
13812
13813/* Used for IconicState or NormalState */
06a2c219 13814
dfcf069d 13815void
f676886a
JB
13816x_wm_set_window_state (f, state)
13817 struct frame *f;
dc6f92b8
JB
13818 int state;
13819{
3afe33e7 13820#ifdef USE_X_TOOLKIT
546e6d5b
RS
13821 Arg al[1];
13822
13823 XtSetArg (al[0], XtNinitialState, state);
7556890b 13824 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 13825#else /* not USE_X_TOOLKIT */
c118dd06 13826 Window window = FRAME_X_WINDOW (f);
dc6f92b8 13827
7556890b
RS
13828 f->output_data.x->wm_hints.flags |= StateHint;
13829 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 13830
7556890b 13831 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 13832#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13833}
13834
dfcf069d 13835void
7f2ae036 13836x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 13837 struct frame *f;
7f2ae036 13838 int pixmap_id;
dc6f92b8 13839{
d2bd6bc4
RS
13840 Pixmap icon_pixmap;
13841
06a2c219 13842#ifndef USE_X_TOOLKIT
c118dd06 13843 Window window = FRAME_X_WINDOW (f);
75231bad 13844#endif
dc6f92b8 13845
7f2ae036 13846 if (pixmap_id > 0)
dbc4e1c1 13847 {
d2bd6bc4 13848 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 13849 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
13850 }
13851 else
68568555
RS
13852 {
13853 /* It seems there is no way to turn off use of an icon pixmap.
13854 The following line does it, only if no icon has yet been created,
13855 for some window managers. But with mwm it crashes.
13856 Some people say it should clear the IconPixmapHint bit in this case,
13857 but that doesn't work, and the X consortium said it isn't the
13858 right thing at all. Since there is no way to win,
13859 best to explicitly give up. */
13860#if 0
13861 f->output_data.x->wm_hints.icon_pixmap = None;
13862#else
13863 return;
13864#endif
13865 }
b1c884c3 13866
d2bd6bc4
RS
13867#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13868
13869 {
13870 Arg al[1];
13871 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13872 XtSetValues (f->output_data.x->widget, al, 1);
13873 }
13874
13875#else /* not USE_X_TOOLKIT */
13876
7556890b
RS
13877 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13878 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
13879
13880#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13881}
13882
dfcf069d 13883void
f676886a
JB
13884x_wm_set_icon_position (f, icon_x, icon_y)
13885 struct frame *f;
dc6f92b8
JB
13886 int icon_x, icon_y;
13887{
75231bad 13888#ifdef USE_X_TOOLKIT
7556890b 13889 Window window = XtWindow (f->output_data.x->widget);
75231bad 13890#else
c118dd06 13891 Window window = FRAME_X_WINDOW (f);
75231bad 13892#endif
dc6f92b8 13893
7556890b
RS
13894 f->output_data.x->wm_hints.flags |= IconPositionHint;
13895 f->output_data.x->wm_hints.icon_x = icon_x;
13896 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 13897
7556890b 13898 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
13899}
13900
13901\f
06a2c219
GM
13902/***********************************************************************
13903 Fonts
13904 ***********************************************************************/
dc43ef94
KH
13905
13906/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 13907
dc43ef94
KH
13908struct font_info *
13909x_get_font_info (f, font_idx)
13910 FRAME_PTR f;
13911 int font_idx;
13912{
13913 return (FRAME_X_FONT_TABLE (f) + font_idx);
13914}
13915
13916
9c11f79e
GM
13917/* Return a list of names of available fonts matching PATTERN on frame F.
13918
13919 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13920 to be listed.
13921
13922 SIZE < 0 means include scalable fonts.
13923
13924 Frame F null means we have not yet created any frame on X, and
13925 consult the first display in x_display_list. MAXNAMES sets a limit
13926 on how many fonts to match. */
dc43ef94
KH
13927
13928Lisp_Object
13929x_list_fonts (f, pattern, size, maxnames)
9c11f79e 13930 struct frame *f;
dc43ef94
KH
13931 Lisp_Object pattern;
13932 int size;
13933 int maxnames;
13934{
06a2c219
GM
13935 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13936 Lisp_Object tem, second_best;
9c11f79e
GM
13937 struct x_display_info *dpyinfo
13938 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13939 Display *dpy = dpyinfo->display;
09c6077f 13940 int try_XLoadQueryFont = 0;
53ca4657 13941 int count;
9c11f79e
GM
13942 int allow_scalable_fonts_p = 0;
13943
13944 if (size < 0)
13945 {
13946 allow_scalable_fonts_p = 1;
13947 size = 0;
13948 }
dc43ef94 13949
6b0efe73 13950 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
13951 if (NILP (patterns))
13952 patterns = Fcons (pattern, Qnil);
81ba44e5 13953
09c6077f
KH
13954 if (maxnames == 1 && !size)
13955 /* We can return any single font matching PATTERN. */
13956 try_XLoadQueryFont = 1;
9a32686f 13957
8e713be6 13958 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 13959 {
dc43ef94 13960 int num_fonts;
3e71d8f2 13961 char **names = NULL;
dc43ef94 13962
8e713be6 13963 pattern = XCAR (patterns);
536f4067
RS
13964 /* See if we cached the result for this particular query.
13965 The cache is an alist of the form:
9c11f79e
GM
13966 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13967 tem = XCDR (dpyinfo->name_list_element);
13968 key = Fcons (Fcons (pattern, make_number (maxnames)),
13969 allow_scalable_fonts_p ? Qt : Qnil);
13970 list = Fassoc (key, tem);
13971 if (!NILP (list))
b5210ea7
KH
13972 {
13973 list = Fcdr_safe (list);
13974 /* We have a cashed list. Don't have to get the list again. */
13975 goto label_cached;
13976 }
13977
13978 /* At first, put PATTERN in the cache. */
09c6077f 13979
dc43ef94 13980 BLOCK_INPUT;
17d85edc
KH
13981 count = x_catch_errors (dpy);
13982
09c6077f
KH
13983 if (try_XLoadQueryFont)
13984 {
13985 XFontStruct *font;
13986 unsigned long value;
13987
13988 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
13989 if (x_had_errors_p (dpy))
13990 {
13991 /* This error is perhaps due to insufficient memory on X
13992 server. Let's just ignore it. */
13993 font = NULL;
13994 x_clear_errors (dpy);
13995 }
13996
09c6077f
KH
13997 if (font
13998 && XGetFontProperty (font, XA_FONT, &value))
13999 {
14000 char *name = (char *) XGetAtomName (dpy, (Atom) value);
14001 int len = strlen (name);
01c752b5 14002 char *tmp;
09c6077f 14003
6f6512e8
KH
14004 /* If DXPC (a Differential X Protocol Compressor)
14005 Ver.3.7 is running, XGetAtomName will return null
14006 string. We must avoid such a name. */
14007 if (len == 0)
14008 try_XLoadQueryFont = 0;
14009 else
14010 {
14011 num_fonts = 1;
14012 names = (char **) alloca (sizeof (char *));
14013 /* Some systems only allow alloca assigned to a
14014 simple var. */
14015 tmp = (char *) alloca (len + 1); names[0] = tmp;
14016 bcopy (name, names[0], len + 1);
14017 XFree (name);
14018 }
09c6077f
KH
14019 }
14020 else
14021 try_XLoadQueryFont = 0;
a083fd23
RS
14022
14023 if (font)
14024 XFreeFont (dpy, font);
09c6077f
KH
14025 }
14026
14027 if (!try_XLoadQueryFont)
17d85edc
KH
14028 {
14029 /* We try at least 10 fonts because XListFonts will return
14030 auto-scaled fonts at the head. */
14031 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
14032 &num_fonts);
14033 if (x_had_errors_p (dpy))
14034 {
14035 /* This error is perhaps due to insufficient memory on X
14036 server. Let's just ignore it. */
14037 names = NULL;
14038 x_clear_errors (dpy);
14039 }
14040 }
14041
14042 x_uncatch_errors (dpy, count);
dc43ef94
KH
14043 UNBLOCK_INPUT;
14044
14045 if (names)
14046 {
14047 int i;
dc43ef94
KH
14048
14049 /* Make a list of all the fonts we got back.
14050 Store that in the font cache for the display. */
14051 for (i = 0; i < num_fonts; i++)
14052 {
06a2c219 14053 int width = 0;
dc43ef94 14054 char *p = names[i];
06a2c219
GM
14055 int average_width = -1, dashes = 0;
14056
dc43ef94 14057 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
14058 14 dashes, and the field value following 12th dash
14059 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14060 is usually too ugly to be used for editing. Let's
14061 ignore it. */
dc43ef94
KH
14062 while (*p)
14063 if (*p++ == '-')
14064 {
14065 dashes++;
14066 if (dashes == 7) /* PIXEL_SIZE field */
14067 width = atoi (p);
14068 else if (dashes == 12) /* AVERAGE_WIDTH field */
14069 average_width = atoi (p);
14070 }
9c11f79e
GM
14071
14072 if (allow_scalable_fonts_p
14073 || dashes < 14 || average_width != 0)
dc43ef94
KH
14074 {
14075 tem = build_string (names[i]);
14076 if (NILP (Fassoc (tem, list)))
14077 {
14078 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
14079 && ((fast_c_string_match_ignore_case
14080 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
14081 >= 0))
14082 /* We can set the value of PIXEL_SIZE to the
b5210ea7 14083 width of this font. */
dc43ef94
KH
14084 list = Fcons (Fcons (tem, make_number (width)), list);
14085 else
14086 /* For the moment, width is not known. */
14087 list = Fcons (Fcons (tem, Qnil), list);
14088 }
14089 }
14090 }
e38f4136 14091
09c6077f 14092 if (!try_XLoadQueryFont)
e38f4136
GM
14093 {
14094 BLOCK_INPUT;
14095 XFreeFontNames (names);
14096 UNBLOCK_INPUT;
14097 }
dc43ef94
KH
14098 }
14099
b5210ea7 14100 /* Now store the result in the cache. */
f3fbd155
KR
14101 XSETCDR (dpyinfo->name_list_element,
14102 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 14103
b5210ea7
KH
14104 label_cached:
14105 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 14106
b5210ea7
KH
14107 newlist = second_best = Qnil;
14108 /* Make a list of the fonts that have the right width. */
8e713be6 14109 for (; CONSP (list); list = XCDR (list))
b5210ea7 14110 {
536f4067
RS
14111 int found_size;
14112
8e713be6 14113 tem = XCAR (list);
dc43ef94 14114
8e713be6 14115 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
14116 continue;
14117 if (!size)
14118 {
8e713be6 14119 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
14120 continue;
14121 }
dc43ef94 14122
8e713be6 14123 if (!INTEGERP (XCDR (tem)))
dc43ef94 14124 {
b5210ea7 14125 /* Since we have not yet known the size of this font, we
9c11f79e 14126 must try slow function call XLoadQueryFont. */
dc43ef94
KH
14127 XFontStruct *thisinfo;
14128
14129 BLOCK_INPUT;
17d85edc 14130 count = x_catch_errors (dpy);
dc43ef94 14131 thisinfo = XLoadQueryFont (dpy,
8e713be6 14132 XSTRING (XCAR (tem))->data);
17d85edc
KH
14133 if (x_had_errors_p (dpy))
14134 {
14135 /* This error is perhaps due to insufficient memory on X
14136 server. Let's just ignore it. */
14137 thisinfo = NULL;
14138 x_clear_errors (dpy);
14139 }
14140 x_uncatch_errors (dpy, count);
dc43ef94
KH
14141 UNBLOCK_INPUT;
14142
14143 if (thisinfo)
14144 {
f3fbd155
KR
14145 XSETCDR (tem,
14146 (thisinfo->min_bounds.width == 0
14147 ? make_number (0)
14148 : make_number (thisinfo->max_bounds.width)));
e38f4136 14149 BLOCK_INPUT;
dc43ef94 14150 XFreeFont (dpy, thisinfo);
e38f4136 14151 UNBLOCK_INPUT;
dc43ef94
KH
14152 }
14153 else
b5210ea7 14154 /* For unknown reason, the previous call of XListFont had
06a2c219 14155 returned a font which can't be opened. Record the size
b5210ea7 14156 as 0 not to try to open it again. */
f3fbd155 14157 XSETCDR (tem, make_number (0));
dc43ef94 14158 }
536f4067 14159
8e713be6 14160 found_size = XINT (XCDR (tem));
536f4067 14161 if (found_size == size)
8e713be6 14162 newlist = Fcons (XCAR (tem), newlist);
536f4067 14163 else if (found_size > 0)
b5210ea7 14164 {
536f4067 14165 if (NILP (second_best))
b5210ea7 14166 second_best = tem;
536f4067
RS
14167 else if (found_size < size)
14168 {
8e713be6
KR
14169 if (XINT (XCDR (second_best)) > size
14170 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
14171 second_best = tem;
14172 }
14173 else
14174 {
8e713be6
KR
14175 if (XINT (XCDR (second_best)) > size
14176 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
14177 second_best = tem;
14178 }
b5210ea7
KH
14179 }
14180 }
14181 if (!NILP (newlist))
14182 break;
14183 else if (!NILP (second_best))
14184 {
8e713be6 14185 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 14186 break;
dc43ef94 14187 }
dc43ef94
KH
14188 }
14189
14190 return newlist;
14191}
14192
06a2c219
GM
14193
14194#if GLYPH_DEBUG
14195
14196/* Check that FONT is valid on frame F. It is if it can be found in F's
14197 font table. */
14198
14199static void
14200x_check_font (f, font)
14201 struct frame *f;
14202 XFontStruct *font;
14203{
14204 int i;
14205 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14206
14207 xassert (font != NULL);
14208
14209 for (i = 0; i < dpyinfo->n_fonts; i++)
14210 if (dpyinfo->font_table[i].name
14211 && font == dpyinfo->font_table[i].font)
14212 break;
14213
14214 xassert (i < dpyinfo->n_fonts);
14215}
14216
14217#endif /* GLYPH_DEBUG != 0 */
14218
14219/* Set *W to the minimum width, *H to the minimum font height of FONT.
14220 Note: There are (broken) X fonts out there with invalid XFontStruct
14221 min_bounds contents. For example, handa@etl.go.jp reports that
14222 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14223 have font->min_bounds.width == 0. */
14224
14225static INLINE void
14226x_font_min_bounds (font, w, h)
14227 XFontStruct *font;
14228 int *w, *h;
14229{
14230 *h = FONT_HEIGHT (font);
14231 *w = font->min_bounds.width;
14232
14233 /* Try to handle the case where FONT->min_bounds has invalid
14234 contents. Since the only font known to have invalid min_bounds
14235 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14236 if (*w <= 0)
14237 *w = font->max_bounds.width;
14238}
14239
14240
14241/* Compute the smallest character width and smallest font height over
14242 all fonts available on frame F. Set the members smallest_char_width
14243 and smallest_font_height in F's x_display_info structure to
14244 the values computed. Value is non-zero if smallest_font_height or
14245 smallest_char_width become smaller than they were before. */
14246
14247static int
14248x_compute_min_glyph_bounds (f)
14249 struct frame *f;
14250{
14251 int i;
14252 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14253 XFontStruct *font;
14254 int old_width = dpyinfo->smallest_char_width;
14255 int old_height = dpyinfo->smallest_font_height;
14256
14257 dpyinfo->smallest_font_height = 100000;
14258 dpyinfo->smallest_char_width = 100000;
14259
14260 for (i = 0; i < dpyinfo->n_fonts; ++i)
14261 if (dpyinfo->font_table[i].name)
14262 {
14263 struct font_info *fontp = dpyinfo->font_table + i;
14264 int w, h;
14265
14266 font = (XFontStruct *) fontp->font;
14267 xassert (font != (XFontStruct *) ~0);
14268 x_font_min_bounds (font, &w, &h);
14269
14270 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14271 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14272 }
14273
14274 xassert (dpyinfo->smallest_char_width > 0
14275 && dpyinfo->smallest_font_height > 0);
14276
14277 return (dpyinfo->n_fonts == 1
14278 || dpyinfo->smallest_char_width < old_width
14279 || dpyinfo->smallest_font_height < old_height);
14280}
14281
14282
dc43ef94
KH
14283/* Load font named FONTNAME of the size SIZE for frame F, and return a
14284 pointer to the structure font_info while allocating it dynamically.
14285 If SIZE is 0, load any size of font.
14286 If loading is failed, return NULL. */
14287
14288struct font_info *
14289x_load_font (f, fontname, size)
14290 struct frame *f;
14291 register char *fontname;
14292 int size;
14293{
14294 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14295 Lisp_Object font_names;
d645aaa4 14296 int count;
dc43ef94
KH
14297
14298 /* Get a list of all the fonts that match this name. Once we
14299 have a list of matching fonts, we compare them against the fonts
14300 we already have by comparing names. */
09c6077f 14301 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
14302
14303 if (!NILP (font_names))
14304 {
14305 Lisp_Object tail;
14306 int i;
14307
14308 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 14309 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
14310 if (dpyinfo->font_table[i].name
14311 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 14312 XSTRING (XCAR (tail))->data)
06a2c219 14313 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 14314 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
14315 return (dpyinfo->font_table + i);
14316 }
14317
14318 /* Load the font and add it to the table. */
14319 {
14320 char *full_name;
14321 XFontStruct *font;
14322 struct font_info *fontp;
14323 unsigned long value;
06a2c219 14324 int i;
dc43ef94 14325
2da424f1
KH
14326 /* If we have found fonts by x_list_font, load one of them. If
14327 not, we still try to load a font by the name given as FONTNAME
14328 because XListFonts (called in x_list_font) of some X server has
14329 a bug of not finding a font even if the font surely exists and
14330 is loadable by XLoadQueryFont. */
e1d6d5b9 14331 if (size > 0 && !NILP (font_names))
8e713be6 14332 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
14333
14334 BLOCK_INPUT;
d645aaa4 14335 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 14336 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
14337 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14338 {
14339 /* This error is perhaps due to insufficient memory on X
14340 server. Let's just ignore it. */
14341 font = NULL;
14342 x_clear_errors (FRAME_X_DISPLAY (f));
14343 }
14344 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 14345 UNBLOCK_INPUT;
b5210ea7 14346 if (!font)
dc43ef94
KH
14347 return NULL;
14348
06a2c219
GM
14349 /* Find a free slot in the font table. */
14350 for (i = 0; i < dpyinfo->n_fonts; ++i)
14351 if (dpyinfo->font_table[i].name == NULL)
14352 break;
14353
14354 /* If no free slot found, maybe enlarge the font table. */
14355 if (i == dpyinfo->n_fonts
14356 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 14357 {
06a2c219
GM
14358 int sz;
14359 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14360 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 14361 dpyinfo->font_table
06a2c219 14362 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
14363 }
14364
06a2c219
GM
14365 fontp = dpyinfo->font_table + i;
14366 if (i == dpyinfo->n_fonts)
14367 ++dpyinfo->n_fonts;
dc43ef94
KH
14368
14369 /* Now fill in the slots of *FONTP. */
14370 BLOCK_INPUT;
14371 fontp->font = font;
06a2c219 14372 fontp->font_idx = i;
dc43ef94
KH
14373 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14374 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14375
14376 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14377 full_name = 0;
14378 if (XGetFontProperty (font, XA_FONT, &value))
14379 {
14380 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14381 char *p = name;
14382 int dashes = 0;
14383
14384 /* Count the number of dashes in the "full name".
14385 If it is too few, this isn't really the font's full name,
14386 so don't use it.
14387 In X11R4, the fonts did not come with their canonical names
14388 stored in them. */
14389 while (*p)
14390 {
14391 if (*p == '-')
14392 dashes++;
14393 p++;
14394 }
14395
14396 if (dashes >= 13)
14397 {
14398 full_name = (char *) xmalloc (p - name + 1);
14399 bcopy (name, full_name, p - name + 1);
14400 }
14401
14402 XFree (name);
14403 }
14404
14405 if (full_name != 0)
14406 fontp->full_name = full_name;
14407 else
14408 fontp->full_name = fontp->name;
14409
14410 fontp->size = font->max_bounds.width;
d5749adb 14411 fontp->height = FONT_HEIGHT (font);
dc43ef94 14412
2da424f1
KH
14413 if (NILP (font_names))
14414 {
14415 /* We come here because of a bug of XListFonts mentioned at
14416 the head of this block. Let's store this information in
14417 the cache for x_list_fonts. */
14418 Lisp_Object lispy_name = build_string (fontname);
14419 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
14420 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14421 Qnil);
2da424f1 14422
f3fbd155
KR
14423 XSETCDR (dpyinfo->name_list_element,
14424 Fcons (Fcons (key,
14425 Fcons (Fcons (lispy_full_name,
14426 make_number (fontp->size)),
14427 Qnil)),
14428 XCDR (dpyinfo->name_list_element)));
2da424f1 14429 if (full_name)
9c11f79e
GM
14430 {
14431 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14432 Qnil);
f3fbd155
KR
14433 XSETCDR (dpyinfo->name_list_element,
14434 Fcons (Fcons (key,
14435 Fcons (Fcons (lispy_full_name,
14436 make_number (fontp->size)),
14437 Qnil)),
14438 XCDR (dpyinfo->name_list_element)));
9c11f79e 14439 }
2da424f1
KH
14440 }
14441
dc43ef94
KH
14442 /* The slot `encoding' specifies how to map a character
14443 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
14444 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14445 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 14446 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 14447 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
14448 which is never used by any charset. If mapping can't be
14449 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
14450 fontp->encoding[1]
14451 = (font->max_byte1 == 0
14452 /* 1-byte font */
14453 ? (font->min_char_or_byte2 < 0x80
14454 ? (font->max_char_or_byte2 < 0x80
14455 ? 0 /* 0x20..0x7F */
8ff102bd 14456 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
14457 : 1) /* 0xA0..0xFF */
14458 /* 2-byte font */
14459 : (font->min_byte1 < 0x80
14460 ? (font->max_byte1 < 0x80
14461 ? (font->min_char_or_byte2 < 0x80
14462 ? (font->max_char_or_byte2 < 0x80
14463 ? 0 /* 0x2020..0x7F7F */
8ff102bd 14464 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 14465 : 3) /* 0x20A0..0x7FFF */
8ff102bd 14466 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
14467 : (font->min_char_or_byte2 < 0x80
14468 ? (font->max_char_or_byte2 < 0x80
14469 ? 2 /* 0xA020..0xFF7F */
8ff102bd 14470 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
14471 : 1))); /* 0xA0A0..0xFFFF */
14472
14473 fontp->baseline_offset
14474 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14475 ? (long) value : 0);
14476 fontp->relative_compose
14477 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14478 ? (long) value : 0);
f78798df
KH
14479 fontp->default_ascent
14480 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14481 ? (long) value : 0);
dc43ef94 14482
06a2c219
GM
14483 /* Set global flag fonts_changed_p to non-zero if the font loaded
14484 has a character with a smaller width than any other character
437dfb9f 14485 before, or if the font loaded has a smaller height than any
06a2c219
GM
14486 other font loaded before. If this happens, it will make a
14487 glyph matrix reallocation necessary. */
437dfb9f 14488 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 14489 UNBLOCK_INPUT;
dc43ef94
KH
14490 return fontp;
14491 }
14492}
14493
06a2c219
GM
14494
14495/* Return a pointer to struct font_info of a font named FONTNAME for
14496 frame F. If no such font is loaded, return NULL. */
14497
dc43ef94
KH
14498struct font_info *
14499x_query_font (f, fontname)
14500 struct frame *f;
14501 register char *fontname;
14502{
14503 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14504 int i;
14505
14506 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
14507 if (dpyinfo->font_table[i].name
14508 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14509 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
14510 return (dpyinfo->font_table + i);
14511 return NULL;
14512}
14513
06a2c219
GM
14514
14515/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
14516 `encoder' of the structure. */
14517
14518void
14519x_find_ccl_program (fontp)
14520 struct font_info *fontp;
14521{
a42f54e6 14522 Lisp_Object list, elt;
a6582676 14523
f9b5db02 14524 elt = Qnil;
8e713be6 14525 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 14526 {
8e713be6 14527 elt = XCAR (list);
a6582676 14528 if (CONSP (elt)
8e713be6 14529 && STRINGP (XCAR (elt))
9f2feff6
KH
14530 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14531 >= 0)
14532 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14533 >= 0)))
a42f54e6
KH
14534 break;
14535 }
f9b5db02 14536
a42f54e6
KH
14537 if (! NILP (list))
14538 {
d27f8ca7
KH
14539 struct ccl_program *ccl
14540 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 14541
8e713be6 14542 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
14543 xfree (ccl);
14544 else
14545 fontp->font_encoder = ccl;
a6582676
KH
14546 }
14547}
14548
06a2c219 14549
dc43ef94 14550\f
06a2c219
GM
14551/***********************************************************************
14552 Initialization
14553 ***********************************************************************/
f451eb13 14554
3afe33e7
RS
14555#ifdef USE_X_TOOLKIT
14556static XrmOptionDescRec emacs_options[] = {
14557 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14558 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14559
14560 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14561 XrmoptionSepArg, NULL},
14562 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14563
14564 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14565 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14566 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14567 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14568 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14569 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14570 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14571};
14572#endif /* USE_X_TOOLKIT */
14573
7a13e894
RS
14574static int x_initialized;
14575
29b38361
KH
14576#ifdef MULTI_KBOARD
14577/* Test whether two display-name strings agree up to the dot that separates
14578 the screen number from the server number. */
14579static int
14580same_x_server (name1, name2)
14581 char *name1, *name2;
14582{
14583 int seen_colon = 0;
cf591cc1
RS
14584 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14585 int system_name_length = strlen (system_name);
14586 int length_until_period = 0;
14587
14588 while (system_name[length_until_period] != 0
14589 && system_name[length_until_period] != '.')
14590 length_until_period++;
14591
14592 /* Treat `unix' like an empty host name. */
14593 if (! strncmp (name1, "unix:", 5))
14594 name1 += 4;
14595 if (! strncmp (name2, "unix:", 5))
14596 name2 += 4;
14597 /* Treat this host's name like an empty host name. */
14598 if (! strncmp (name1, system_name, system_name_length)
14599 && name1[system_name_length] == ':')
14600 name1 += system_name_length;
14601 if (! strncmp (name2, system_name, system_name_length)
14602 && name2[system_name_length] == ':')
14603 name2 += system_name_length;
14604 /* Treat this host's domainless name like an empty host name. */
14605 if (! strncmp (name1, system_name, length_until_period)
14606 && name1[length_until_period] == ':')
14607 name1 += length_until_period;
14608 if (! strncmp (name2, system_name, length_until_period)
14609 && name2[length_until_period] == ':')
14610 name2 += length_until_period;
14611
29b38361
KH
14612 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14613 {
14614 if (*name1 == ':')
14615 seen_colon++;
14616 if (seen_colon && *name1 == '.')
14617 return 1;
14618 }
14619 return (seen_colon
14620 && (*name1 == '.' || *name1 == '\0')
14621 && (*name2 == '.' || *name2 == '\0'));
14622}
14623#endif
14624
334208b7 14625struct x_display_info *
1f8255f2 14626x_term_init (display_name, xrm_option, resource_name)
334208b7 14627 Lisp_Object display_name;
1f8255f2
RS
14628 char *xrm_option;
14629 char *resource_name;
dc6f92b8 14630{
334208b7 14631 int connection;
7a13e894 14632 Display *dpy;
334208b7
RS
14633 struct x_display_info *dpyinfo;
14634 XrmDatabase xrdb;
14635
60439948
KH
14636 BLOCK_INPUT;
14637
7a13e894
RS
14638 if (!x_initialized)
14639 {
14640 x_initialize ();
14641 x_initialized = 1;
14642 }
dc6f92b8 14643
3afe33e7 14644#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
14645 /* weiner@footloose.sps.mot.com reports that this causes
14646 errors with X11R5:
14647 X protocol error: BadAtom (invalid Atom parameter)
14648 on protocol request 18skiloaf.
14649 So let's not use it until R6. */
14650#ifdef HAVE_X11XTR6
bdcd49ba
RS
14651 XtSetLanguageProc (NULL, NULL, NULL);
14652#endif
14653
7f9c7f94
RS
14654 {
14655 int argc = 0;
14656 char *argv[3];
14657
14658 argv[0] = "";
14659 argc = 1;
14660 if (xrm_option)
14661 {
14662 argv[argc++] = "-xrm";
14663 argv[argc++] = xrm_option;
14664 }
14665 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14666 resource_name, EMACS_CLASS,
14667 emacs_options, XtNumber (emacs_options),
14668 &argc, argv);
39d8bb4d
KH
14669
14670#ifdef HAVE_X11XTR6
10537cb1 14671 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 14672 fixup_locale ();
39d8bb4d 14673#endif
7f9c7f94 14674 }
3afe33e7
RS
14675
14676#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
14677#ifdef HAVE_X11R5
14678 XSetLocaleModifiers ("");
14679#endif
7a13e894 14680 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 14681#endif /* not USE_X_TOOLKIT */
334208b7 14682
7a13e894
RS
14683 /* Detect failure. */
14684 if (dpy == 0)
60439948
KH
14685 {
14686 UNBLOCK_INPUT;
14687 return 0;
14688 }
7a13e894
RS
14689
14690 /* We have definitely succeeded. Record the new connection. */
14691
14692 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 14693 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 14694
29b38361
KH
14695#ifdef MULTI_KBOARD
14696 {
14697 struct x_display_info *share;
14698 Lisp_Object tail;
14699
14700 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
14701 share = share->next, tail = XCDR (tail))
14702 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
14703 XSTRING (display_name)->data))
14704 break;
14705 if (share)
14706 dpyinfo->kboard = share->kboard;
14707 else
14708 {
14709 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14710 init_kboard (dpyinfo->kboard);
59e755be
KH
14711 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14712 {
14713 char *vendor = ServerVendor (dpy);
9b6ed9f3 14714 UNBLOCK_INPUT;
59e755be
KH
14715 dpyinfo->kboard->Vsystem_key_alist
14716 = call1 (Qvendor_specific_keysyms,
14717 build_string (vendor ? vendor : ""));
9b6ed9f3 14718 BLOCK_INPUT;
59e755be
KH
14719 }
14720
29b38361
KH
14721 dpyinfo->kboard->next_kboard = all_kboards;
14722 all_kboards = dpyinfo->kboard;
0ad5446c
KH
14723 /* Don't let the initial kboard remain current longer than necessary.
14724 That would cause problems if a file loaded on startup tries to
06a2c219 14725 prompt in the mini-buffer. */
0ad5446c
KH
14726 if (current_kboard == initial_kboard)
14727 current_kboard = dpyinfo->kboard;
29b38361
KH
14728 }
14729 dpyinfo->kboard->reference_count++;
14730 }
b9737ad3
KH
14731#endif
14732
7a13e894
RS
14733 /* Put this display on the chain. */
14734 dpyinfo->next = x_display_list;
14735 x_display_list = dpyinfo;
14736
14737 /* Put it on x_display_name_list as well, to keep them parallel. */
14738 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14739 x_display_name_list);
8e713be6 14740 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
14741
14742 dpyinfo->display = dpy;
dc6f92b8 14743
dc6f92b8 14744#if 0
7a13e894 14745 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 14746#endif /* ! 0 */
7a13e894
RS
14747
14748 dpyinfo->x_id_name
fc932ac6
RS
14749 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14750 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
14751 + 2);
14752 sprintf (dpyinfo->x_id_name, "%s@%s",
14753 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
14754
14755 /* Figure out which modifier bits mean what. */
334208b7 14756 x_find_modifier_meanings (dpyinfo);
f451eb13 14757
ab648270 14758 /* Get the scroll bar cursor. */
7a13e894 14759 dpyinfo->vertical_scroll_bar_cursor
334208b7 14760 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 14761
334208b7
RS
14762 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14763 resource_name, EMACS_CLASS);
14764#ifdef HAVE_XRMSETDATABASE
14765 XrmSetDatabase (dpyinfo->display, xrdb);
14766#else
14767 dpyinfo->display->db = xrdb;
14768#endif
547d9db8 14769 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
14770 all versions. */
14771 dpyinfo->xrdb = xrdb;
334208b7
RS
14772
14773 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14774 DefaultScreen (dpyinfo->display));
5ff67d81 14775 select_visual (dpyinfo);
43bd1b2b 14776 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
14777 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14778 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14779 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14780 dpyinfo->grabbed = 0;
14781 dpyinfo->reference_count = 0;
14782 dpyinfo->icon_bitmap_id = -1;
06a2c219 14783 dpyinfo->font_table = NULL;
7a13e894
RS
14784 dpyinfo->n_fonts = 0;
14785 dpyinfo->font_table_size = 0;
14786 dpyinfo->bitmaps = 0;
14787 dpyinfo->bitmaps_size = 0;
14788 dpyinfo->bitmaps_last = 0;
14789 dpyinfo->scratch_cursor_gc = 0;
14790 dpyinfo->mouse_face_mouse_frame = 0;
14791 dpyinfo->mouse_face_deferred_gc = 0;
14792 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14793 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 14794 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 14795 dpyinfo->mouse_face_window = Qnil;
0a61c667 14796 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
14797 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14798 dpyinfo->mouse_face_defer = 0;
66301061 14799 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
14800 dpyinfo->x_focus_frame = 0;
14801 dpyinfo->x_focus_event_frame = 0;
14802 dpyinfo->x_highlight_frame = 0;
06a2c219 14803 dpyinfo->image_cache = make_image_cache ();
334208b7 14804
43bd1b2b 14805 /* See if a private colormap is requested. */
5ff67d81
GM
14806 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14807 {
14808 if (dpyinfo->visual->class == PseudoColor)
14809 {
14810 Lisp_Object value;
14811 value = display_x_get_resource (dpyinfo,
14812 build_string ("privateColormap"),
14813 build_string ("PrivateColormap"),
14814 Qnil, Qnil);
14815 if (STRINGP (value)
14816 && (!strcmp (XSTRING (value)->data, "true")
14817 || !strcmp (XSTRING (value)->data, "on")))
14818 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14819 }
43bd1b2b 14820 }
5ff67d81
GM
14821 else
14822 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14823 dpyinfo->visual, AllocNone);
43bd1b2b 14824
06a2c219
GM
14825 {
14826 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14827 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14828 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14829 dpyinfo->resy = pixels * 25.4 / mm;
14830 pixels = DisplayWidth (dpyinfo->display, screen_number);
14831 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14832 dpyinfo->resx = pixels * 25.4 / mm;
14833 }
14834
334208b7
RS
14835 dpyinfo->Xatom_wm_protocols
14836 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14837 dpyinfo->Xatom_wm_take_focus
14838 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14839 dpyinfo->Xatom_wm_save_yourself
14840 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14841 dpyinfo->Xatom_wm_delete_window
14842 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14843 dpyinfo->Xatom_wm_change_state
14844 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14845 dpyinfo->Xatom_wm_configure_denied
14846 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14847 dpyinfo->Xatom_wm_window_moved
14848 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14849 dpyinfo->Xatom_editres
14850 = XInternAtom (dpyinfo->display, "Editres", False);
14851 dpyinfo->Xatom_CLIPBOARD
14852 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14853 dpyinfo->Xatom_TIMESTAMP
14854 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14855 dpyinfo->Xatom_TEXT
14856 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
14857 dpyinfo->Xatom_COMPOUND_TEXT
14858 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
14859 dpyinfo->Xatom_DELETE
14860 = XInternAtom (dpyinfo->display, "DELETE", False);
14861 dpyinfo->Xatom_MULTIPLE
14862 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14863 dpyinfo->Xatom_INCR
14864 = XInternAtom (dpyinfo->display, "INCR", False);
14865 dpyinfo->Xatom_EMACS_TMP
14866 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14867 dpyinfo->Xatom_TARGETS
14868 = XInternAtom (dpyinfo->display, "TARGETS", False);
14869 dpyinfo->Xatom_NULL
14870 = XInternAtom (dpyinfo->display, "NULL", False);
14871 dpyinfo->Xatom_ATOM_PAIR
14872 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
14873 /* For properties of font. */
14874 dpyinfo->Xatom_PIXEL_SIZE
14875 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14876 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14877 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14878 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14879 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
14880 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14881 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 14882
06a2c219
GM
14883 /* Ghostscript support. */
14884 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14885 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14886
14887 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14888 False);
14889
547d9db8
KH
14890 dpyinfo->cut_buffers_initialized = 0;
14891
334208b7
RS
14892 connection = ConnectionNumber (dpyinfo->display);
14893 dpyinfo->connection = connection;
14894
dc43ef94 14895 {
5d7cc324
RS
14896 char null_bits[1];
14897
14898 null_bits[0] = 0x00;
dc43ef94
KH
14899
14900 dpyinfo->null_pixel
14901 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14902 null_bits, 1, 1, (long) 0, (long) 0,
14903 1);
14904 }
14905
06a2c219
GM
14906 {
14907 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 14908 extern char *gray_bitmap_bits;
06a2c219
GM
14909 dpyinfo->gray
14910 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14911 gray_bitmap_bits,
14912 gray_bitmap_width, gray_bitmap_height,
14913 (unsigned long) 1, (unsigned long) 0, 1);
14914 }
14915
f5d11644
GM
14916#ifdef HAVE_X_I18N
14917 xim_initialize (dpyinfo, resource_name);
14918#endif
14919
87485d6f
MW
14920#ifdef subprocesses
14921 /* This is only needed for distinguishing keyboard and process input. */
334208b7 14922 if (connection != 0)
7a13e894 14923 add_keyboard_wait_descriptor (connection);
87485d6f 14924#endif
6d4238f3 14925
041b69ac 14926#ifndef F_SETOWN_BUG
dc6f92b8 14927#ifdef F_SETOWN
dc6f92b8 14928#ifdef F_SETOWN_SOCK_NEG
61c3ce62 14929 /* stdin is a socket here */
334208b7 14930 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 14931#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 14932 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
14933#endif /* ! defined (F_SETOWN_SOCK_NEG) */
14934#endif /* ! defined (F_SETOWN) */
041b69ac 14935#endif /* F_SETOWN_BUG */
dc6f92b8
JB
14936
14937#ifdef SIGIO
eee20f6a
KH
14938 if (interrupt_input)
14939 init_sigio (connection);
c118dd06 14940#endif /* ! defined (SIGIO) */
dc6f92b8 14941
51b592fb 14942#ifdef USE_LUCID
f8c39f51 14943#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
14944 /* Make sure that we have a valid font for dialog boxes
14945 so that Xt does not crash. */
14946 {
14947 Display *dpy = dpyinfo->display;
14948 XrmValue d, fr, to;
14949 Font font;
e99db5a1 14950 int count;
51b592fb
RS
14951
14952 d.addr = (XPointer)&dpy;
14953 d.size = sizeof (Display *);
14954 fr.addr = XtDefaultFont;
14955 fr.size = sizeof (XtDefaultFont);
14956 to.size = sizeof (Font *);
14957 to.addr = (XPointer)&font;
e99db5a1 14958 count = x_catch_errors (dpy);
51b592fb
RS
14959 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14960 abort ();
14961 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14962 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 14963 x_uncatch_errors (dpy, count);
51b592fb
RS
14964 }
14965#endif
f8c39f51 14966#endif
51b592fb 14967
34e23e5a
GM
14968 /* See if we should run in synchronous mode. This is useful
14969 for debugging X code. */
14970 {
14971 Lisp_Object value;
14972 value = display_x_get_resource (dpyinfo,
14973 build_string ("synchronous"),
14974 build_string ("Synchronous"),
14975 Qnil, Qnil);
14976 if (STRINGP (value)
14977 && (!strcmp (XSTRING (value)->data, "true")
14978 || !strcmp (XSTRING (value)->data, "on")))
14979 XSynchronize (dpyinfo->display, True);
14980 }
14981
60439948
KH
14982 UNBLOCK_INPUT;
14983
7a13e894
RS
14984 return dpyinfo;
14985}
14986\f
14987/* Get rid of display DPYINFO, assuming all frames are already gone,
14988 and without sending any more commands to the X server. */
dc6f92b8 14989
7a13e894
RS
14990void
14991x_delete_display (dpyinfo)
14992 struct x_display_info *dpyinfo;
14993{
14994 delete_keyboard_wait_descriptor (dpyinfo->connection);
14995
14996 /* Discard this display from x_display_name_list and x_display_list.
14997 We can't use Fdelq because that can quit. */
14998 if (! NILP (x_display_name_list)
8e713be6
KR
14999 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15000 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
15001 else
15002 {
15003 Lisp_Object tail;
15004
15005 tail = x_display_name_list;
8e713be6 15006 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 15007 {
bffcfca9 15008 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 15009 {
f3fbd155 15010 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
15011 break;
15012 }
8e713be6 15013 tail = XCDR (tail);
7a13e894
RS
15014 }
15015 }
15016
9bda743f
GM
15017 if (next_noop_dpyinfo == dpyinfo)
15018 next_noop_dpyinfo = dpyinfo->next;
15019
7a13e894
RS
15020 if (x_display_list == dpyinfo)
15021 x_display_list = dpyinfo->next;
7f9c7f94
RS
15022 else
15023 {
15024 struct x_display_info *tail;
7a13e894 15025
7f9c7f94
RS
15026 for (tail = x_display_list; tail; tail = tail->next)
15027 if (tail->next == dpyinfo)
15028 tail->next = tail->next->next;
15029 }
7a13e894 15030
0d777288
RS
15031#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15032#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
15033 XrmDestroyDatabase (dpyinfo->xrdb);
15034#endif
0d777288 15035#endif
29b38361
KH
15036#ifdef MULTI_KBOARD
15037 if (--dpyinfo->kboard->reference_count == 0)
39f79001 15038 delete_kboard (dpyinfo->kboard);
b9737ad3 15039#endif
f5d11644
GM
15040#ifdef HAVE_X_I18N
15041 if (dpyinfo->xim)
15042 xim_close_dpy (dpyinfo);
15043#endif
15044
b9737ad3
KH
15045 xfree (dpyinfo->font_table);
15046 xfree (dpyinfo->x_id_name);
f04e1297 15047 xfree (dpyinfo->color_cells);
b9737ad3 15048 xfree (dpyinfo);
7a13e894 15049}
f04e1297 15050
7a13e894
RS
15051\f
15052/* Set up use of X before we make the first connection. */
15053
06a2c219
GM
15054static struct redisplay_interface x_redisplay_interface =
15055{
15056 x_produce_glyphs,
15057 x_write_glyphs,
15058 x_insert_glyphs,
15059 x_clear_end_of_line,
15060 x_scroll_run,
15061 x_after_update_window_line,
15062 x_update_window_begin,
15063 x_update_window_end,
15064 XTcursor_to,
15065 x_flush,
71b8321e 15066 x_clear_mouse_face,
66ac4b0e
GM
15067 x_get_glyph_overhangs,
15068 x_fix_overlapping_area
06a2c219
GM
15069};
15070
dfcf069d 15071void
7a13e894
RS
15072x_initialize ()
15073{
06a2c219
GM
15074 rif = &x_redisplay_interface;
15075
15076 clear_frame_hook = x_clear_frame;
15077 ins_del_lines_hook = x_ins_del_lines;
06a2c219 15078 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
15079 ring_bell_hook = XTring_bell;
15080 reset_terminal_modes_hook = XTreset_terminal_modes;
15081 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
15082 update_begin_hook = x_update_begin;
15083 update_end_hook = x_update_end;
dc6f92b8
JB
15084 set_terminal_window_hook = XTset_terminal_window;
15085 read_socket_hook = XTread_socket;
b8009dd1 15086 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 15087 mouse_position_hook = XTmouse_position;
f451eb13 15088 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 15089 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
15090 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15091 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15092 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15093 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 15094 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 15095
f676886a 15096 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 15097 char_ins_del_ok = 1;
dc6f92b8
JB
15098 line_ins_del_ok = 1; /* we'll just blt 'em */
15099 fast_clear_end_of_line = 1; /* X does this well */
58769bee 15100 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
15101 off the bottom */
15102 baud_rate = 19200;
15103
7a13e894 15104 x_noop_count = 0;
9ea173e8 15105 last_tool_bar_item = -1;
06a2c219
GM
15106 any_help_event_p = 0;
15107
b30b24cb
RS
15108 /* Try to use interrupt input; if we can't, then start polling. */
15109 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15110
7f9c7f94
RS
15111#ifdef USE_X_TOOLKIT
15112 XtToolkitInitialize ();
651f03b6 15113
7f9c7f94 15114 Xt_app_con = XtCreateApplicationContext ();
651f03b6
GM
15115
15116 /* Register a converter from strings to pixels, which uses
15117 Emacs' color allocation infrastructure. */
15118 XtAppSetTypeConverter (Xt_app_con,
15119 XtRString, XtRPixel, cvt_string_to_pixel,
15120 cvt_string_to_pixel_args,
15121 XtNumber (cvt_string_to_pixel_args),
15122 XtCacheByDisplay, cvt_pixel_dtor);
15123
665881ad 15124 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
15125
15126 /* Install an asynchronous timer that processes Xt timeout events
15127 every 0.1s. This is necessary because some widget sets use
15128 timeouts internally, for example the LessTif menu bar, or the
15129 Xaw3d scroll bar. When Xt timouts aren't processed, these
15130 widgets don't behave normally. */
15131 {
15132 EMACS_TIME interval;
15133 EMACS_SET_SECS_USECS (interval, 0, 100000);
15134 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15135 }
db74249b 15136#endif
bffcfca9 15137
eccc05db 15138#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
15139 xaw3d_arrow_scroll = False;
15140 xaw3d_pick_top = True;
7f9c7f94
RS
15141#endif
15142
58769bee 15143 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 15144 original error handler. */
e99db5a1 15145 XSetErrorHandler (x_error_handler);
334208b7 15146 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 15147
06a2c219 15148 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
15149#ifdef SIGWINCH
15150 signal (SIGWINCH, SIG_DFL);
a59facca 15151#endif /* SIGWINCH */
dc6f92b8 15152
92e2441b 15153 signal (SIGPIPE, x_connection_signal);
5f30b957
JD
15154
15155#ifdef HAVE_X_SM
15156 x_session_initialize ();
15157#endif
dc6f92b8 15158}
55123275 15159
06a2c219 15160
55123275
JB
15161void
15162syms_of_xterm ()
15163{
e99db5a1
RS
15164 staticpro (&x_error_message_string);
15165 x_error_message_string = Qnil;
15166
7a13e894
RS
15167 staticpro (&x_display_name_list);
15168 x_display_name_list = Qnil;
334208b7 15169
ab648270 15170 staticpro (&last_mouse_scroll_bar);
e53cb100 15171 last_mouse_scroll_bar = Qnil;
59e755be
KH
15172
15173 staticpro (&Qvendor_specific_keysyms);
15174 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 15175
c997eae5
SM
15176 staticpro (&Qutf_8);
15177 Qutf_8 = intern ("utf-8");
15178 staticpro (&Qlatin_1);
15179 Qlatin_1 = intern ("latin-1");
15180
2237cac9
RS
15181 staticpro (&last_mouse_press_frame);
15182 last_mouse_press_frame = Qnil;
06a2c219 15183
06a2c219 15184 help_echo = Qnil;
be010514
GM
15185 staticpro (&help_echo);
15186 help_echo_object = Qnil;
15187 staticpro (&help_echo_object);
7cea38bc
GM
15188 help_echo_window = Qnil;
15189 staticpro (&help_echo_window);
06a2c219 15190 previous_help_echo = Qnil;
be010514
GM
15191 staticpro (&previous_help_echo);
15192 help_echo_pos = -1;
06a2c219 15193
ee8ceff8 15194 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
503e457e 15195 doc: /* *Non-nil means autoselect window with mouse pointer. */);
ee8ceff8 15196 mouse_autoselect_window = 0;
503e457e 15197
7ee72033
MB
15198 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15199 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
228299fa
GM
15200For example, if a block cursor is over a tab, it will be drawn as
15201wide as that tab on the display. */);
06a2c219
GM
15202 x_stretch_cursor_p = 0;
15203
a72d5ce5 15204 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
15205 &x_use_underline_position_properties,
15206 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 15207nil means ignore them. If you encounter fonts with bogus
228299fa
GM
15208UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15209to 4.1, set this to nil. */);
a72d5ce5
GM
15210 x_use_underline_position_properties = 1;
15211
7ee72033
MB
15212 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15213 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
15214A value of nil means Emacs doesn't use X toolkit scroll bars.
15215Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 15216#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
15217#ifdef USE_MOTIF
15218 Vx_toolkit_scroll_bars = intern ("motif");
15219#elif defined HAVE_XAW3D
15220 Vx_toolkit_scroll_bars = intern ("xaw3d");
15221#else
15222 Vx_toolkit_scroll_bars = intern ("xaw");
15223#endif
06a2c219 15224#else
5bf04520 15225 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
15226#endif
15227
06a2c219
GM
15228 staticpro (&last_mouse_motion_frame);
15229 last_mouse_motion_frame = Qnil;
98659da6
KG
15230
15231 Qmodifier_value = intern ("modifier-value");
15232 Qalt = intern ("alt");
15233 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15234 Qhyper = intern ("hyper");
15235 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15236 Qmeta = intern ("meta");
15237 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15238 Qsuper = intern ("super");
15239 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15240
15241 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15242 doc: /* Which keys Emacs uses for the alt modifier.
15243This should be one of the symbols `alt', `hyper', `meta', `super'.
15244For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15245is nil, which is the same as `alt'. */);
15246 Vx_alt_keysym = Qnil;
15247
15248 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15249 doc: /* Which keys Emacs uses for the hyper modifier.
15250This should be one of the symbols `alt', `hyper', `meta', `super'.
15251For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15252default is nil, which is the same as `hyper'. */);
15253 Vx_hyper_keysym = Qnil;
15254
15255 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15256 doc: /* Which keys Emacs uses for the meta modifier.
15257This should be one of the symbols `alt', `hyper', `meta', `super'.
15258For example, `meta' means use the Meta_L and Meta_R keysyms. The
15259default is nil, which is the same as `meta'. */);
15260 Vx_meta_keysym = Qnil;
15261
15262 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15263 doc: /* Which keys Emacs uses for the super modifier.
15264This should be one of the symbols `alt', `hyper', `meta', `super'.
15265For example, `super' means use the Super_L and Super_R keysyms. The
15266default is nil, which is the same as `super'. */);
15267 Vx_super_keysym = Qnil;
15268
55123275 15269}
6cf0ae86 15270
1d6c120a 15271#endif /* HAVE_X_WINDOWS */