(xstricmp): String pointer args now point to const.
[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 100#ifdef USE_LUCID
55a8b3ed 101extern int xlwmenu_window_p P_ ((Widget w, Window window));
d624284c
PJ
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 *));
cab34f50
JD
469static int x_focus_changed P_ ((int,
470 int,
471 struct x_display_info *,
472 struct frame *,
473 struct input_event *,
474 int));
475static int x_detect_focus_change P_ ((struct x_display_info *,
476 XEvent *,
477 struct input_event *,
478 int));
06a2c219
GM
479static void XTframe_rehighlight P_ ((struct frame *));
480static void x_frame_rehighlight P_ ((struct x_display_info *));
481static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
65c26775
EZ
482static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
483 enum text_cursor_kinds));
06a2c219
GM
484static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
485 XRectangle *));
486static void expose_frame P_ ((struct frame *, int, int, int, int));
82f053ab 487static int expose_window_tree P_ ((struct window *, XRectangle *));
a39202f6 488static int expose_window P_ ((struct window *, XRectangle *));
06a2c219
GM
489static void expose_area P_ ((struct window *, struct glyph_row *,
490 XRectangle *, enum glyph_row_area));
82f053ab 491static int expose_line P_ ((struct window *, struct glyph_row *,
06a2c219
GM
492 XRectangle *));
493static void x_update_cursor_in_window_tree P_ ((struct window *, int));
494static void x_update_window_cursor P_ ((struct window *, int));
495static void x_erase_phys_cursor P_ ((struct window *));
496void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
3f332ef3 497static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
976b73d7 498 enum fringe_bitmap_type, int left_p));
06a2c219
GM
499
500static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
501 GC, int));
502static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
3f332ef3 503static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
60626bab
GM
504static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
505 int, int, int, int));
06a2c219 506static void x_flush P_ ((struct frame *f));
952291d9
GM
507static void x_update_begin P_ ((struct frame *));
508static void x_update_window_begin P_ ((struct window *));
509static void x_draw_vertical_border P_ ((struct window *));
510static void x_after_update_window_line P_ ((struct glyph_row *));
511static INLINE void take_vertical_position_into_account P_ ((struct it *));
512static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
513static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
514static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
515 enum scroll_bar_part *,
516 Lisp_Object *, Lisp_Object *,
517 unsigned long *));
d0fa3e56
EZ
518static void x_check_fullscreen P_ ((struct frame *));
519static void x_check_fullscreen_move P_ ((struct frame *));
06a2c219
GM
520
521/* Flush display of frame F, or of all frames if F is null. */
522
523static void
524x_flush (f)
525 struct frame *f;
526{
527 BLOCK_INPUT;
528 if (f == NULL)
529 {
530 Lisp_Object rest, frame;
531 FOR_EACH_FRAME (rest, frame)
532 x_flush (XFRAME (frame));
533 }
534 else if (FRAME_X_P (f))
535 XFlush (FRAME_X_DISPLAY (f));
536 UNBLOCK_INPUT;
537}
538
dc6f92b8 539
06a2c219
GM
540/* Remove calls to XFlush by defining XFlush to an empty replacement.
541 Calls to XFlush should be unnecessary because the X output buffer
542 is flushed automatically as needed by calls to XPending,
543 XNextEvent, or XWindowEvent according to the XFlush man page.
544 XTread_socket calls XPending. Removing XFlush improves
545 performance. */
546
547#define XFlush(DISPLAY) (void) 0
b8009dd1 548
334208b7 549\f
06a2c219
GM
550/***********************************************************************
551 Debugging
552 ***********************************************************************/
553
9382638d 554#if 0
06a2c219
GM
555
556/* This is a function useful for recording debugging information about
557 the sequence of occurrences in this file. */
9382638d
KH
558
559struct record
560{
561 char *locus;
562 int type;
563};
564
565struct record event_record[100];
566
567int event_record_index;
568
569record_event (locus, type)
570 char *locus;
571 int type;
572{
573 if (event_record_index == sizeof (event_record) / sizeof (struct record))
574 event_record_index = 0;
575
576 event_record[event_record_index].locus = locus;
577 event_record[event_record_index].type = type;
578 event_record_index++;
579}
580
581#endif /* 0 */
06a2c219
GM
582
583
9382638d 584\f
334208b7
RS
585/* Return the struct x_display_info corresponding to DPY. */
586
587struct x_display_info *
588x_display_info_for_display (dpy)
589 Display *dpy;
590{
591 struct x_display_info *dpyinfo;
592
593 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
594 if (dpyinfo->display == dpy)
595 return dpyinfo;
16bd92ea 596
334208b7
RS
597 return 0;
598}
f451eb13 599
06a2c219
GM
600
601\f
602/***********************************************************************
603 Starting and ending an update
604 ***********************************************************************/
605
606/* Start an update of frame F. This function is installed as a hook
607 for update_begin, i.e. it is called when update_begin is called.
608 This function is called prior to calls to x_update_window_begin for
609 each window being updated. Currently, there is nothing to do here
610 because all interesting stuff is done on a window basis. */
dc6f92b8 611
dfcf069d 612static void
06a2c219 613x_update_begin (f)
f676886a 614 struct frame *f;
58769bee 615{
06a2c219
GM
616 /* Nothing to do. */
617}
dc6f92b8 618
dc6f92b8 619
06a2c219
GM
620/* Start update of window W. Set the global variable updated_window
621 to the window being updated and set output_cursor to the cursor
622 position of W. */
dc6f92b8 623
06a2c219
GM
624static void
625x_update_window_begin (w)
626 struct window *w;
627{
628 struct frame *f = XFRAME (WINDOW_FRAME (w));
629 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
630
631 updated_window = w;
632 set_output_cursor (&w->cursor);
b8009dd1 633
06a2c219 634 BLOCK_INPUT;
d1bc4182 635
06a2c219 636 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 637 {
514e4681 638 /* Don't do highlighting for mouse motion during the update. */
06a2c219 639 display_info->mouse_face_defer = 1;
37c2c98b 640
06a2c219
GM
641 /* If F needs to be redrawn, simply forget about any prior mouse
642 highlighting. */
9f67f20b 643 if (FRAME_GARBAGED_P (f))
06a2c219
GM
644 display_info->mouse_face_window = Qnil;
645
64f26cf5
GM
646#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
647 their mouse_face_p flag set, which means that they are always
648 unequal to rows in a desired matrix which never have that
649 flag set. So, rows containing mouse-face glyphs are never
650 scrolled, and we don't have to switch the mouse highlight off
651 here to prevent it from being scrolled. */
652
06a2c219
GM
653 /* Can we tell that this update does not affect the window
654 where the mouse highlight is? If so, no need to turn off.
655 Likewise, don't do anything if the frame is garbaged;
656 in that case, the frame's current matrix that we would use
657 is all wrong, and we will redisplay that line anyway. */
658 if (!NILP (display_info->mouse_face_window)
659 && w == XWINDOW (display_info->mouse_face_window))
514e4681 660 {
06a2c219 661 int i;
514e4681 662
06a2c219
GM
663 for (i = 0; i < w->desired_matrix->nrows; ++i)
664 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
665 break;
666
06a2c219
GM
667 if (i < w->desired_matrix->nrows)
668 clear_mouse_face (display_info);
514e4681 669 }
64f26cf5 670#endif /* 0 */
b8009dd1 671 }
6ccf47d1 672
dc6f92b8
JB
673 UNBLOCK_INPUT;
674}
675
06a2c219
GM
676
677/* Draw a vertical window border to the right of window W if W doesn't
678 have vertical scroll bars. */
679
dfcf069d 680static void
06a2c219
GM
681x_draw_vertical_border (w)
682 struct window *w;
58769bee 683{
06a2c219
GM
684 struct frame *f = XFRAME (WINDOW_FRAME (w));
685
686 /* Redraw borders between horizontally adjacent windows. Don't
687 do it for frames with vertical scroll bars because either the
688 right scroll bar of a window, or the left scroll bar of its
689 neighbor will suffice as a border. */
690 if (!WINDOW_RIGHTMOST_P (w)
691 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
692 {
693 int x0, x1, y0, y1;
dc6f92b8 694
06a2c219 695 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
3f332ef3 696 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
06a2c219
GM
697 y1 -= 1;
698
699 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
700 f->output_data.x->normal_gc, x1, y0, x1, y1);
701 }
702}
703
704
71b8321e
GM
705/* End update of window W (which is equal to updated_window).
706
707 Draw vertical borders between horizontally adjacent windows, and
708 display W's cursor if CURSOR_ON_P is non-zero.
709
710 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
711 glyphs in mouse-face were overwritten. In that case we have to
712 make sure that the mouse-highlight is properly redrawn.
713
714 W may be a menu bar pseudo-window in case we don't have X toolkit
715 support. Such windows don't have a cursor, so don't display it
716 here. */
06a2c219
GM
717
718static void
71b8321e 719x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 720 struct window *w;
71b8321e 721 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 722{
140330de
GM
723 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
724
06a2c219
GM
725 if (!w->pseudo_window_p)
726 {
727 BLOCK_INPUT;
71b8321e 728
06a2c219
GM
729 if (cursor_on_p)
730 x_display_and_set_cursor (w, 1, output_cursor.hpos,
731 output_cursor.vpos,
732 output_cursor.x, output_cursor.y);
71b8321e 733
06a2c219
GM
734 x_draw_vertical_border (w);
735 UNBLOCK_INPUT;
736 }
737
140330de
GM
738 /* If a row with mouse-face was overwritten, arrange for
739 XTframe_up_to_date to redisplay the mouse highlight. */
740 if (mouse_face_overwritten_p)
741 {
742 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
743 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
744 dpyinfo->mouse_face_window = Qnil;
745 }
746
06a2c219
GM
747 updated_window = NULL;
748}
dc6f92b8 749
dc6f92b8 750
06a2c219
GM
751/* End update of frame F. This function is installed as a hook in
752 update_end. */
753
754static void
755x_update_end (f)
756 struct frame *f;
757{
758 /* Mouse highlight may be displayed again. */
aa8bff2e 759 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 760
06a2c219 761 BLOCK_INPUT;
334208b7 762 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
763 UNBLOCK_INPUT;
764}
b8009dd1 765
06a2c219
GM
766
767/* This function is called from various places in xdisp.c whenever a
768 complete update has been performed. The global variable
769 updated_window is not available here. */
b8009dd1 770
dfcf069d 771static void
b8009dd1 772XTframe_up_to_date (f)
06a2c219 773 struct frame *f;
b8009dd1 774{
06a2c219 775 if (FRAME_X_P (f))
514e4681 776 {
06a2c219 777 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 778
06a2c219
GM
779 if (dpyinfo->mouse_face_deferred_gc
780 || f == dpyinfo->mouse_face_mouse_frame)
781 {
782 BLOCK_INPUT;
783 if (dpyinfo->mouse_face_mouse_frame)
784 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
785 dpyinfo->mouse_face_mouse_x,
786 dpyinfo->mouse_face_mouse_y);
787 dpyinfo->mouse_face_deferred_gc = 0;
788 UNBLOCK_INPUT;
789 }
514e4681 790 }
b8009dd1 791}
06a2c219
GM
792
793
794/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 795 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
796 before DESIRED_ROW is made current. The window being updated is
797 found in updated_window. This function It is called from
798 update_window_line only if it is known that there are differences
799 between bitmaps to be drawn between current row and DESIRED_ROW. */
800
801static void
802x_after_update_window_line (desired_row)
803 struct glyph_row *desired_row;
804{
805 struct window *w = updated_window;
ef253080 806 struct frame *f;
259cf6bc 807 int width, height;
06a2c219
GM
808
809 xassert (w);
810
811 if (!desired_row->mode_line_p && !w->pseudo_window_p)
812 {
813 BLOCK_INPUT;
3f332ef3 814 x_draw_row_fringe_bitmaps (w, desired_row);
ef253080
GM
815 UNBLOCK_INPUT;
816 }
817
818 /* When a window has disappeared, make sure that no rest of
819 full-width rows stays visible in the internal border. Could
820 check here if updated_window is the leftmost/rightmost window,
821 but I guess it's not worth doing since vertically split windows
822 are almost never used, internal border is rarely set, and the
823 overhead is very small. */
824 if (windows_or_buffers_changed
825 && desired_row->full_width_p
826 && (f = XFRAME (w->frame),
827 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
828 width != 0)
829 && (height = desired_row->visible_height,
830 height > 0))
ef253080 831 {
ef253080 832 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 833
ef253080
GM
834 /* Internal border is drawn below the tool bar. */
835 if (WINDOWP (f->tool_bar_window)
836 && w == XWINDOW (f->tool_bar_window))
837 y -= width;
06a2c219 838
ef253080
GM
839 BLOCK_INPUT;
840 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
841 0, y, width, height, False);
842 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
843 f->output_data.x->pixel_width - width,
844 y, width, height, False);
06a2c219
GM
845 UNBLOCK_INPUT;
846 }
847}
848
849
3f332ef3 850/* Draw the bitmap WHICH in one of the left or right fringes of
06a2c219
GM
851 window W. ROW is the glyph row for which to display the bitmap; it
852 determines the vertical position at which the bitmap has to be
853 drawn. */
854
855static void
976b73d7 856x_draw_fringe_bitmap (w, row, which, left_p)
06a2c219
GM
857 struct window *w;
858 struct glyph_row *row;
3f332ef3 859 enum fringe_bitmap_type which;
976b73d7 860 int left_p;
06a2c219
GM
861{
862 struct frame *f = XFRAME (WINDOW_FRAME (w));
863 Display *display = FRAME_X_DISPLAY (f);
864 Window window = FRAME_X_WINDOW (f);
865 int x, y, wd, h, dy;
976b73d7 866 int b1, b2;
b436feb6 867 unsigned char *bits = NULL;
06a2c219
GM
868 Pixmap pixmap;
869 GC gc = f->output_data.x->normal_gc;
870 struct face *face;
871 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
872
873 /* Must clip because of partially visible lines. */
874 x_clip_to_row (w, row, gc, 1);
875
976b73d7
KS
876 /* Convert row to frame coordinates. */
877 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
878
06a2c219
GM
879 switch (which)
880 {
976b73d7
KS
881 case NO_FRINGE_BITMAP:
882 wd = 0;
883 h = 0;
884 break;
885
06a2c219
GM
886 case LEFT_TRUNCATION_BITMAP:
887 wd = left_width;
888 h = left_height;
889 bits = left_bits;
06a2c219
GM
890 break;
891
892 case OVERLAY_ARROW_BITMAP:
976b73d7
KS
893 wd = ov_width;
894 h = ov_height;
06a2c219 895 bits = ov_bits;
06a2c219
GM
896 break;
897
898 case RIGHT_TRUNCATION_BITMAP:
899 wd = right_width;
900 h = right_height;
901 bits = right_bits;
06a2c219
GM
902 break;
903
904 case CONTINUED_LINE_BITMAP:
976b73d7
KS
905 wd = continued_width;
906 h = continued_height;
06a2c219 907 bits = continued_bits;
06a2c219
GM
908 break;
909
910 case CONTINUATION_LINE_BITMAP:
911 wd = continuation_width;
912 h = continuation_height;
913 bits = continuation_bits;
06a2c219
GM
914 break;
915
916 case ZV_LINE_BITMAP:
917 wd = zv_width;
976b73d7
KS
918 h = zv_height - (y % zv_period);
919 bits = zv_bits + (y % zv_period);
06a2c219
GM
920 break;
921
922 default:
923 abort ();
924 }
925
976b73d7
KS
926 /* Clip bitmap if too high. */
927 if (h > row->height)
928 h = row->height;
929
930 /* Set dy to the offset in the row to start drawing the bitmap. */
06a2c219
GM
931 dy = (row->height - h) / 2;
932
976b73d7
KS
933 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
934 PREPARE_FACE_FOR_DISPLAY (f, face);
935
936 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
937 the fringe. */
dbdc9702 938 b1 = b2 = -1;
976b73d7
KS
939 if (left_p)
940 {
941 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
942 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
943 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
944 - wd
945 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
946 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
947 {
948 /* If W has a vertical border to its left, don't draw over it. */
949 int border = ((XFASTINT (w->left) > 0
950 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
951 ? 1 : 0);
952 b1 = (window_box_left (w, -1)
953 - FRAME_X_LEFT_FRINGE_WIDTH (f)
954 + border);
955 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
956 }
957 }
958 else
959 {
960 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
961 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
962 x = (window_box_right (w, -1)
963 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
964 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
965 the fringe. */
966 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
967 {
968 b1 = window_box_right (w, -1);
969 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
970 }
971 }
972
973 if (b1 >= 0)
974 {
975 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
976
977 /* In case the same realized face is used for fringes and
978 for something displayed in the text (e.g. face `region' on
979 mono-displays, the fill style may have been changed to
980 FillSolid in x_draw_glyph_string_background. */
981 if (face->stipple)
982 XSetFillStyle (display, face->gc, FillOpaqueStippled);
983 else
984 XSetForeground (display, face->gc, face->background);
985
986 XFillRectangle (display, window, face->gc,
987 b1,
988 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
989 row->y)),
990 b2,
991 row->visible_height);
992 if (!face->stipple)
993 XSetForeground (display, face->gc, face->foreground);
994 }
995
dbdc9702
GM
996 if (which != NO_FRINGE_BITMAP)
997 {
998 /* Draw the bitmap. I believe these small pixmaps can be cached
999 by the server. */
1000 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
1001 face->foreground,
1002 face->background, depth);
1003 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
1004 XFreePixmap (display, pixmap);
1005 }
1006
06a2c219
GM
1007 XSetClipMask (display, gc, None);
1008}
1009
1010
3f332ef3 1011/* Draw fringe bitmaps for glyph row ROW on window W. Call this
06a2c219
GM
1012 function with input blocked. */
1013
1014static void
3f332ef3 1015x_draw_row_fringe_bitmaps (w, row)
06a2c219
GM
1016 struct window *w;
1017 struct glyph_row *row;
1018{
1019 struct frame *f = XFRAME (w->frame);
3f332ef3 1020 enum fringe_bitmap_type bitmap;
06a2c219
GM
1021
1022 xassert (interrupt_input_blocked);
1023
1024 /* If row is completely invisible, because of vscrolling, we
1025 don't have to draw anything. */
1026 if (row->visible_height <= 0)
1027 return;
1028
976b73d7
KS
1029 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1030 {
1031 /* Decide which bitmap to draw in the left fringe. */
1032 if (row->overlay_arrow_p)
1033 bitmap = OVERLAY_ARROW_BITMAP;
1034 else if (row->truncated_on_left_p)
1035 bitmap = LEFT_TRUNCATION_BITMAP;
1036 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1037 bitmap = CONTINUATION_LINE_BITMAP;
1038 else if (row->indicate_empty_line_p)
1039 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1040 else
976b73d7 1041 bitmap = NO_FRINGE_BITMAP;
06a2c219 1042
976b73d7
KS
1043 x_draw_fringe_bitmap (w, row, bitmap, 1);
1044 }
06a2c219 1045
976b73d7 1046 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
06a2c219 1047 {
976b73d7
KS
1048 /* Decide which bitmap to draw in the right fringe. */
1049 if (row->truncated_on_right_p)
1050 bitmap = RIGHT_TRUNCATION_BITMAP;
1051 else if (row->continued_p)
1052 bitmap = CONTINUED_LINE_BITMAP;
1053 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1054 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1055 else
976b73d7 1056 bitmap = NO_FRINGE_BITMAP;
06a2c219 1057
976b73d7
KS
1058 x_draw_fringe_bitmap (w, row, bitmap, 0);
1059 }
06a2c219
GM
1060}
1061
dc6f92b8 1062\f
06a2c219
GM
1063
1064/* This is called when starting Emacs and when restarting after
1065 suspend. When starting Emacs, no X window is mapped. And nothing
1066 must be done to Emacs's own window if it is suspended (though that
1067 rarely happens). */
dc6f92b8 1068
dfcf069d 1069static void
dc6f92b8
JB
1070XTset_terminal_modes ()
1071{
1072}
1073
06a2c219
GM
1074/* This is called when exiting or suspending Emacs. Exiting will make
1075 the X-windows go away, and suspending requires no action. */
dc6f92b8 1076
dfcf069d 1077static void
dc6f92b8
JB
1078XTreset_terminal_modes ()
1079{
dc6f92b8 1080}
06a2c219
GM
1081
1082
dc6f92b8 1083\f
06a2c219
GM
1084/***********************************************************************
1085 Output Cursor
1086 ***********************************************************************/
1087
1088/* Set the global variable output_cursor to CURSOR. All cursor
1089 positions are relative to updated_window. */
dc6f92b8 1090
dfcf069d 1091static void
06a2c219
GM
1092set_output_cursor (cursor)
1093 struct cursor_pos *cursor;
dc6f92b8 1094{
06a2c219
GM
1095 output_cursor.hpos = cursor->hpos;
1096 output_cursor.vpos = cursor->vpos;
1097 output_cursor.x = cursor->x;
1098 output_cursor.y = cursor->y;
1099}
1100
1101
1102/* Set a nominal cursor position.
dc6f92b8 1103
06a2c219
GM
1104 HPOS and VPOS are column/row positions in a window glyph matrix. X
1105 and Y are window text area relative pixel positions.
1106
1107 If this is done during an update, updated_window will contain the
1108 window that is being updated and the position is the future output
1109 cursor position for that window. If updated_window is null, use
1110 selected_window and display the cursor at the given position. */
1111
1112static void
1113XTcursor_to (vpos, hpos, y, x)
1114 int vpos, hpos, y, x;
1115{
1116 struct window *w;
1117
1118 /* If updated_window is not set, work on selected_window. */
1119 if (updated_window)
1120 w = updated_window;
1121 else
1122 w = XWINDOW (selected_window);
dbcb258a 1123
06a2c219
GM
1124 /* Set the output cursor. */
1125 output_cursor.hpos = hpos;
1126 output_cursor.vpos = vpos;
1127 output_cursor.x = x;
1128 output_cursor.y = y;
dc6f92b8 1129
06a2c219
GM
1130 /* If not called as part of an update, really display the cursor.
1131 This will also set the cursor position of W. */
1132 if (updated_window == NULL)
dc6f92b8
JB
1133 {
1134 BLOCK_INPUT;
06a2c219 1135 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1136 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1137 UNBLOCK_INPUT;
1138 }
1139}
dc43ef94 1140
06a2c219
GM
1141
1142\f
1143/***********************************************************************
1144 Display Iterator
1145 ***********************************************************************/
1146
1147/* Function prototypes of this page. */
1148
1149static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1150 struct glyph *,
ee569018
KH
1151 XChar2b *,
1152 int *));
06a2c219 1153static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
abfb6b46
GM
1154 int, XChar2b *, int,
1155 int));
06a2c219
GM
1156static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1157static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1158static void x_append_glyph P_ ((struct it *));
b4192550 1159static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1160static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1161 int, int, double));
1162static void x_produce_glyphs P_ ((struct it *));
06a2c219 1163static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1164
1165
e2ef8ee6
GM
1166/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1167 is not contained in the font. */
dc43ef94 1168
06a2c219 1169static INLINE XCharStruct *
ee569018 1170x_per_char_metric (font, char2b)
06a2c219
GM
1171 XFontStruct *font;
1172 XChar2b *char2b;
1173{
1174 /* The result metric information. */
1175 XCharStruct *pcm = NULL;
dc6f92b8 1176
06a2c219 1177 xassert (font && char2b);
dc6f92b8 1178
06a2c219 1179 if (font->per_char != NULL)
dc6f92b8 1180 {
06a2c219 1181 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1182 {
06a2c219
GM
1183 /* min_char_or_byte2 specifies the linear character index
1184 corresponding to the first element of the per_char array,
1185 max_char_or_byte2 is the index of the last character. A
1186 character with non-zero CHAR2B->byte1 is not in the font.
1187 A character with byte2 less than min_char_or_byte2 or
1188 greater max_char_or_byte2 is not in the font. */
1189 if (char2b->byte1 == 0
1190 && char2b->byte2 >= font->min_char_or_byte2
1191 && char2b->byte2 <= font->max_char_or_byte2)
1192 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1193 }
06a2c219 1194 else
dc6f92b8 1195 {
06a2c219
GM
1196 /* If either min_byte1 or max_byte1 are nonzero, both
1197 min_char_or_byte2 and max_char_or_byte2 are less than
1198 256, and the 2-byte character index values corresponding
1199 to the per_char array element N (counting from 0) are:
1200
1201 byte1 = N/D + min_byte1
1202 byte2 = N\D + min_char_or_byte2
1203
1204 where:
1205
1206 D = max_char_or_byte2 - min_char_or_byte2 + 1
1207 / = integer division
1208 \ = integer modulus */
1209 if (char2b->byte1 >= font->min_byte1
1210 && char2b->byte1 <= font->max_byte1
1211 && char2b->byte2 >= font->min_char_or_byte2
1212 && char2b->byte2 <= font->max_char_or_byte2)
1213 {
1214 pcm = (font->per_char
1215 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1216 * (char2b->byte1 - font->min_byte1))
1217 + (char2b->byte2 - font->min_char_or_byte2));
1218 }
dc6f92b8 1219 }
06a2c219
GM
1220 }
1221 else
1222 {
1223 /* If the per_char pointer is null, all glyphs between the first
1224 and last character indexes inclusive have the same
1225 information, as given by both min_bounds and max_bounds. */
1226 if (char2b->byte2 >= font->min_char_or_byte2
1227 && char2b->byte2 <= font->max_char_or_byte2)
1228 pcm = &font->max_bounds;
1229 }
dc6f92b8 1230
ee569018 1231 return ((pcm == NULL
3e71d8f2 1232 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1233 ? NULL : pcm);
06a2c219 1234}
b73b6aaf 1235
57b03282 1236
06a2c219
GM
1237/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1238 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1239
06a2c219
GM
1240static INLINE void
1241x_encode_char (c, char2b, font_info)
1242 int c;
1243 XChar2b *char2b;
1244 struct font_info *font_info;
1245{
1246 int charset = CHAR_CHARSET (c);
1247 XFontStruct *font = font_info->font;
1248
1249 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1250 This may be either a program in a special encoder language or a
1251 fixed encoding. */
1252 if (font_info->font_encoder)
1253 {
1254 /* It's a program. */
1255 struct ccl_program *ccl = font_info->font_encoder;
1256
1257 if (CHARSET_DIMENSION (charset) == 1)
1258 {
1259 ccl->reg[0] = charset;
1260 ccl->reg[1] = char2b->byte2;
1261 }
1262 else
1263 {
1264 ccl->reg[0] = charset;
1265 ccl->reg[1] = char2b->byte1;
1266 ccl->reg[2] = char2b->byte2;
1267 }
1268
1269 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1270
1271 /* We assume that MSBs are appropriately set/reset by CCL
1272 program. */
1273 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1274 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1275 else
1276 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1277 }
1278 else if (font_info->encoding[charset])
1279 {
1280 /* Fixed encoding scheme. See fontset.h for the meaning of the
1281 encoding numbers. */
1282 int enc = font_info->encoding[charset];
1283
1284 if ((enc == 1 || enc == 2)
1285 && CHARSET_DIMENSION (charset) == 2)
1286 char2b->byte1 |= 0x80;
1287
1288 if (enc == 1 || enc == 3)
1289 char2b->byte2 |= 0x80;
1290 }
1291}
1292
1293
1294/* Get face and two-byte form of character C in face FACE_ID on frame
1295 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
abfb6b46
GM
1296 means we want to display multibyte text. DISPLAY_P non-zero means
1297 make sure that X resources for the face returned are allocated.
1298 Value is a pointer to a realized face that is ready for display if
1299 DISPLAY_P is non-zero. */
06a2c219
GM
1300
1301static INLINE struct face *
abfb6b46 1302x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
06a2c219
GM
1303 struct frame *f;
1304 int c, face_id;
1305 XChar2b *char2b;
abfb6b46 1306 int multibyte_p, display_p;
06a2c219
GM
1307{
1308 struct face *face = FACE_FROM_ID (f, face_id);
1309
1310 if (!multibyte_p)
1311 {
1312 /* Unibyte case. We don't have to encode, but we have to make
1313 sure to use a face suitable for unibyte. */
1314 char2b->byte1 = 0;
1315 char2b->byte2 = c;
ee569018
KH
1316 face_id = FACE_FOR_CHAR (f, face, c);
1317 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1318 }
1319 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1320 {
1321 /* Case of ASCII in a face known to fit ASCII. */
1322 char2b->byte1 = 0;
1323 char2b->byte2 = c;
1324 }
1325 else
1326 {
1327 int c1, c2, charset;
1328
1329 /* Split characters into bytes. If c2 is -1 afterwards, C is
1330 really a one-byte character so that byte1 is zero. */
1331 SPLIT_CHAR (c, charset, c1, c2);
1332 if (c2 > 0)
1333 char2b->byte1 = c1, char2b->byte2 = c2;
1334 else
1335 char2b->byte1 = 0, char2b->byte2 = c1;
1336
06a2c219 1337 /* Maybe encode the character in *CHAR2B. */
ee569018 1338 if (face->font != NULL)
06a2c219
GM
1339 {
1340 struct font_info *font_info
1341 = FONT_INFO_FROM_ID (f, face->font_info_id);
1342 if (font_info)
ee569018 1343 x_encode_char (c, char2b, font_info);
06a2c219
GM
1344 }
1345 }
1346
1347 /* Make sure X resources of the face are allocated. */
abfb6b46
GM
1348 if (display_p)
1349 {
1350 xassert (face != NULL);
1351 PREPARE_FACE_FOR_DISPLAY (f, face);
1352 }
06a2c219
GM
1353
1354 return face;
1355}
1356
1357
1358/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1359 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1360 a pointer to a realized face that is ready for display. */
1361
1362static INLINE struct face *
ee569018 1363x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1364 struct frame *f;
1365 struct glyph *glyph;
1366 XChar2b *char2b;
ee569018 1367 int *two_byte_p;
06a2c219
GM
1368{
1369 struct face *face;
1370
1371 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1372 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1373
ee569018
KH
1374 if (two_byte_p)
1375 *two_byte_p = 0;
1376
06a2c219
GM
1377 if (!glyph->multibyte_p)
1378 {
1379 /* Unibyte case. We don't have to encode, but we have to make
1380 sure to use a face suitable for unibyte. */
1381 char2b->byte1 = 0;
43d120d8 1382 char2b->byte2 = glyph->u.ch;
06a2c219 1383 }
43d120d8
KH
1384 else if (glyph->u.ch < 128
1385 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1386 {
1387 /* Case of ASCII in a face known to fit ASCII. */
1388 char2b->byte1 = 0;
43d120d8 1389 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1390 }
1391 else
1392 {
1393 int c1, c2, charset;
1394
1395 /* Split characters into bytes. If c2 is -1 afterwards, C is
1396 really a one-byte character so that byte1 is zero. */
43d120d8 1397 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1398 if (c2 > 0)
1399 char2b->byte1 = c1, char2b->byte2 = c2;
1400 else
1401 char2b->byte1 = 0, char2b->byte2 = c1;
1402
1403 /* Maybe encode the character in *CHAR2B. */
1404 if (charset != CHARSET_ASCII)
1405 {
1406 struct font_info *font_info
1407 = FONT_INFO_FROM_ID (f, face->font_info_id);
1408 if (font_info)
1409 {
43d120d8 1410 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1411 if (two_byte_p)
1412 *two_byte_p
1413 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1414 }
1415 }
1416 }
1417
1418 /* Make sure X resources of the face are allocated. */
1419 xassert (face != NULL);
1420 PREPARE_FACE_FOR_DISPLAY (f, face);
1421 return face;
1422}
1423
1424
1425/* Store one glyph for IT->char_to_display in IT->glyph_row.
1426 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1427
1428static INLINE void
1429x_append_glyph (it)
1430 struct it *it;
1431{
1432 struct glyph *glyph;
1433 enum glyph_row_area area = it->area;
1434
1435 xassert (it->glyph_row);
1436 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1437
1438 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1439 if (glyph < it->glyph_row->glyphs[area + 1])
1440 {
06a2c219
GM
1441 glyph->charpos = CHARPOS (it->position);
1442 glyph->object = it->object;
88d75730 1443 glyph->pixel_width = it->pixel_width;
06a2c219 1444 glyph->voffset = it->voffset;
88d75730 1445 glyph->type = CHAR_GLYPH;
06a2c219 1446 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1447 glyph->left_box_line_p = it->start_of_box_run_p;
1448 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1449 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1450 || it->phys_descent > it->descent);
88d75730 1451 glyph->padding_p = 0;
ee569018 1452 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1453 glyph->face_id = it->face_id;
1454 glyph->u.ch = it->char_to_display;
06a2c219
GM
1455 ++it->glyph_row->used[area];
1456 }
1457}
1458
b4192550
KH
1459/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1460 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1461
1462static INLINE void
1463x_append_composite_glyph (it)
1464 struct it *it;
1465{
1466 struct glyph *glyph;
1467 enum glyph_row_area area = it->area;
1468
1469 xassert (it->glyph_row);
1470
1471 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1472 if (glyph < it->glyph_row->glyphs[area + 1])
1473 {
b4192550
KH
1474 glyph->charpos = CHARPOS (it->position);
1475 glyph->object = it->object;
88d75730 1476 glyph->pixel_width = it->pixel_width;
b4192550 1477 glyph->voffset = it->voffset;
88d75730 1478 glyph->type = COMPOSITE_GLYPH;
b4192550 1479 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1480 glyph->left_box_line_p = it->start_of_box_run_p;
1481 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1482 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1483 || it->phys_descent > it->descent);
88d75730
GM
1484 glyph->padding_p = 0;
1485 glyph->glyph_not_available_p = 0;
1486 glyph->face_id = it->face_id;
1487 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1488 ++it->glyph_row->used[area];
1489 }
1490}
1491
06a2c219
GM
1492
1493/* Change IT->ascent and IT->height according to the setting of
1494 IT->voffset. */
1495
1496static INLINE void
1497take_vertical_position_into_account (it)
1498 struct it *it;
1499{
1500 if (it->voffset)
1501 {
1502 if (it->voffset < 0)
1503 /* Increase the ascent so that we can display the text higher
1504 in the line. */
1505 it->ascent += abs (it->voffset);
1506 else
1507 /* Increase the descent so that we can display the text lower
1508 in the line. */
1509 it->descent += it->voffset;
1510 }
1511}
1512
1513
1514/* Produce glyphs/get display metrics for the image IT is loaded with.
1515 See the description of struct display_iterator in dispextern.h for
1516 an overview of struct display_iterator. */
1517
1518static void
1519x_produce_image_glyph (it)
1520 struct it *it;
1521{
1522 struct image *img;
1523 struct face *face;
1524
1525 xassert (it->what == IT_IMAGE);
1526
1527 face = FACE_FROM_ID (it->f, it->face_id);
1528 img = IMAGE_FROM_ID (it->f, it->image_id);
1529 xassert (img);
1530
1531 /* Make sure X resources of the face and image are loaded. */
1532 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1533 prepare_image_for_display (it->f, img);
1534
95af8492 1535 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1536 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1537 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1538
1539 it->nglyphs = 1;
1540
1541 if (face->box != FACE_NO_BOX)
1542 {
ea2ba0d4
KH
1543 if (face->box_line_width > 0)
1544 {
1545 it->ascent += face->box_line_width;
1546 it->descent += face->box_line_width;
1547 }
06a2c219
GM
1548
1549 if (it->start_of_box_run_p)
ea2ba0d4 1550 it->pixel_width += abs (face->box_line_width);
06a2c219 1551 if (it->end_of_box_run_p)
ea2ba0d4 1552 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1553 }
1554
1555 take_vertical_position_into_account (it);
1556
1557 if (it->glyph_row)
1558 {
1559 struct glyph *glyph;
1560 enum glyph_row_area area = it->area;
1561
1562 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1563 if (glyph < it->glyph_row->glyphs[area + 1])
1564 {
06a2c219
GM
1565 glyph->charpos = CHARPOS (it->position);
1566 glyph->object = it->object;
88d75730 1567 glyph->pixel_width = it->pixel_width;
06a2c219 1568 glyph->voffset = it->voffset;
88d75730 1569 glyph->type = IMAGE_GLYPH;
06a2c219 1570 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1571 glyph->left_box_line_p = it->start_of_box_run_p;
1572 glyph->right_box_line_p = it->end_of_box_run_p;
1573 glyph->overlaps_vertically_p = 0;
1574 glyph->padding_p = 0;
1575 glyph->glyph_not_available_p = 0;
1576 glyph->face_id = it->face_id;
1577 glyph->u.img_id = img->id;
06a2c219
GM
1578 ++it->glyph_row->used[area];
1579 }
1580 }
1581}
1582
1583
1584/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1585 of the glyph, WIDTH and HEIGHT are the width and height of the
1586 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1587 ascent of the glyph (0 <= ASCENT <= 1). */
1588
1589static void
1590x_append_stretch_glyph (it, object, width, height, ascent)
1591 struct it *it;
1592 Lisp_Object object;
1593 int width, height;
1594 double ascent;
1595{
1596 struct glyph *glyph;
1597 enum glyph_row_area area = it->area;
1598
1599 xassert (ascent >= 0 && ascent <= 1);
1600
1601 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1602 if (glyph < it->glyph_row->glyphs[area + 1])
1603 {
06a2c219
GM
1604 glyph->charpos = CHARPOS (it->position);
1605 glyph->object = object;
88d75730 1606 glyph->pixel_width = width;
06a2c219 1607 glyph->voffset = it->voffset;
88d75730 1608 glyph->type = STRETCH_GLYPH;
06a2c219 1609 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1610 glyph->left_box_line_p = it->start_of_box_run_p;
1611 glyph->right_box_line_p = it->end_of_box_run_p;
1612 glyph->overlaps_vertically_p = 0;
1613 glyph->padding_p = 0;
1614 glyph->glyph_not_available_p = 0;
1615 glyph->face_id = it->face_id;
1616 glyph->u.stretch.ascent = height * ascent;
1617 glyph->u.stretch.height = height;
06a2c219
GM
1618 ++it->glyph_row->used[area];
1619 }
1620}
1621
1622
1623/* Produce a stretch glyph for iterator IT. IT->object is the value
1624 of the glyph property displayed. The value must be a list
1625 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1626 being recognized:
1627
1628 1. `:width WIDTH' specifies that the space should be WIDTH *
1629 canonical char width wide. WIDTH may be an integer or floating
1630 point number.
1631
1632 2. `:relative-width FACTOR' specifies that the width of the stretch
1633 should be computed from the width of the first character having the
1634 `glyph' property, and should be FACTOR times that width.
1635
1636 3. `:align-to HPOS' specifies that the space should be wide enough
1637 to reach HPOS, a value in canonical character units.
1638
1639 Exactly one of the above pairs must be present.
1640
1641 4. `:height HEIGHT' specifies that the height of the stretch produced
1642 should be HEIGHT, measured in canonical character units.
1643
269b7745 1644 5. `:relative-height FACTOR' specifies that the height of the
06a2c219
GM
1645 stretch should be FACTOR times the height of the characters having
1646 the glyph property.
1647
1648 Either none or exactly one of 4 or 5 must be present.
1649
1650 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1651 of the stretch should be used for the ascent of the stretch.
1652 ASCENT must be in the range 0 <= ASCENT <= 100. */
1653
1654#define NUMVAL(X) \
1655 ((INTEGERP (X) || FLOATP (X)) \
1656 ? XFLOATINT (X) \
1657 : - 1)
1658
1659
1660static void
1661x_produce_stretch_glyph (it)
1662 struct it *it;
1663{
1664 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1665#if GLYPH_DEBUG
1666 extern Lisp_Object Qspace;
1667#endif
1668 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1669 extern Lisp_Object QCrelative_width, QCrelative_height;
1670 extern Lisp_Object QCalign_to;
1671 Lisp_Object prop, plist;
1672 double width = 0, height = 0, ascent = 0;
1673 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1674 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1675
1676 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1677
1678 /* List should start with `space'. */
1679 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1680 plist = XCDR (it->object);
1681
1682 /* Compute the width of the stretch. */
1683 if (prop = Fplist_get (plist, QCwidth),
1684 NUMVAL (prop) > 0)
1685 /* Absolute width `:width WIDTH' specified and valid. */
1686 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1687 else if (prop = Fplist_get (plist, QCrelative_width),
1688 NUMVAL (prop) > 0)
1689 {
1690 /* Relative width `:relative-width FACTOR' specified and valid.
1691 Compute the width of the characters having the `glyph'
1692 property. */
1693 struct it it2;
1694 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1695
1696 it2 = *it;
1697 if (it->multibyte_p)
1698 {
1699 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1700 - IT_BYTEPOS (*it));
1701 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1702 }
1703 else
1704 it2.c = *p, it2.len = 1;
1705
1706 it2.glyph_row = NULL;
1707 it2.what = IT_CHARACTER;
1708 x_produce_glyphs (&it2);
1709 width = NUMVAL (prop) * it2.pixel_width;
1710 }
1711 else if (prop = Fplist_get (plist, QCalign_to),
1712 NUMVAL (prop) > 0)
1713 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1714 else
1715 /* Nothing specified -> width defaults to canonical char width. */
1716 width = CANON_X_UNIT (it->f);
1717
1718 /* Compute height. */
1719 if (prop = Fplist_get (plist, QCheight),
1720 NUMVAL (prop) > 0)
1721 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1722 else if (prop = Fplist_get (plist, QCrelative_height),
1723 NUMVAL (prop) > 0)
1724 height = FONT_HEIGHT (font) * NUMVAL (prop);
1725 else
1726 height = FONT_HEIGHT (font);
1727
1728 /* Compute percentage of height used for ascent. If
1729 `:ascent ASCENT' is present and valid, use that. Otherwise,
1730 derive the ascent from the font in use. */
1731 if (prop = Fplist_get (plist, QCascent),
1732 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1733 ascent = NUMVAL (prop) / 100.0;
1734 else
1735 ascent = (double) font->ascent / FONT_HEIGHT (font);
1736
1737 if (width <= 0)
1738 width = 1;
1739 if (height <= 0)
1740 height = 1;
1741
1742 if (it->glyph_row)
1743 {
1744 Lisp_Object object = it->stack[it->sp - 1].string;
1745 if (!STRINGP (object))
1746 object = it->w->buffer;
1747 x_append_stretch_glyph (it, object, width, height, ascent);
1748 }
1749
1750 it->pixel_width = width;
66ac4b0e
GM
1751 it->ascent = it->phys_ascent = height * ascent;
1752 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1753 it->nglyphs = 1;
1754
1755 if (face->box != FACE_NO_BOX)
1756 {
ea2ba0d4
KH
1757 if (face->box_line_width > 0)
1758 {
1759 it->ascent += face->box_line_width;
1760 it->descent += face->box_line_width;
1761 }
06a2c219
GM
1762
1763 if (it->start_of_box_run_p)
ea2ba0d4 1764 it->pixel_width += abs (face->box_line_width);
06a2c219 1765 if (it->end_of_box_run_p)
ea2ba0d4 1766 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1767 }
1768
1769 take_vertical_position_into_account (it);
1770}
1771
b4192550
KH
1772/* Return proper value to be used as baseline offset of font that has
1773 ASCENT and DESCENT to draw characters by the font at the vertical
1774 center of the line of frame F.
1775
1776 Here, out task is to find the value of BOFF in the following figure;
1777
1778 -------------------------+-----------+-
1779 -+-+---------+-+ | |
1780 | | | | | |
1781 | | | | F_ASCENT F_HEIGHT
1782 | | | ASCENT | |
1783 HEIGHT | | | | |
1784 | | |-|-+------+-----------|------- baseline
1785 | | | | BOFF | |
1786 | |---------|-+-+ | |
1787 | | | DESCENT | |
1788 -+-+---------+-+ F_DESCENT |
1789 -------------------------+-----------+-
1790
1791 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1792 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1793 DESCENT = FONT->descent
1794 HEIGHT = FONT_HEIGHT (FONT)
1795 F_DESCENT = (F->output_data.x->font->descent
1796 - F->output_data.x->baseline_offset)
1797 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1798*/
1799
458f45fa
KH
1800#define VCENTER_BASELINE_OFFSET(FONT, F) \
1801 ((FONT)->descent \
1802 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1803 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1804 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1805
1806/* Produce glyphs/get display metrics for the display element IT is
1807 loaded with. See the description of struct display_iterator in
1808 dispextern.h for an overview of struct display_iterator. */
1809
1810static void
1811x_produce_glyphs (it)
1812 struct it *it;
1813{
ee569018
KH
1814 it->glyph_not_available_p = 0;
1815
06a2c219
GM
1816 if (it->what == IT_CHARACTER)
1817 {
1818 XChar2b char2b;
1819 XFontStruct *font;
ee569018 1820 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1821 XCharStruct *pcm;
06a2c219 1822 int font_not_found_p;
b4192550
KH
1823 struct font_info *font_info;
1824 int boff; /* baseline offset */
a4249304
KH
1825 /* We may change it->multibyte_p upon unibyte<->multibyte
1826 conversion. So, save the current value now and restore it
1827 later.
1828
1829 Note: It seems that we don't have to record multibyte_p in
1830 struct glyph because the character code itself tells if or
1831 not the character is multibyte. Thus, in the future, we must
1832 consider eliminating the field `multibyte_p' in the struct
c347a1c3 1833 glyph. */
a4249304 1834 int saved_multibyte_p = it->multibyte_p;
06a2c219 1835
ee569018
KH
1836 /* Maybe translate single-byte characters to multibyte, or the
1837 other way. */
06a2c219 1838 it->char_to_display = it->c;
ee569018 1839 if (!ASCII_BYTE_P (it->c))
06a2c219 1840 {
ee569018
KH
1841 if (unibyte_display_via_language_environment
1842 && SINGLE_BYTE_CHAR_P (it->c)
1843 && (it->c >= 0240
1844 || !NILP (Vnonascii_translation_table)))
1845 {
1846 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1847 it->multibyte_p = 1;
ee569018
KH
1848 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1849 face = FACE_FROM_ID (it->f, it->face_id);
1850 }
1851 else if (!SINGLE_BYTE_CHAR_P (it->c)
1852 && !it->multibyte_p)
1853 {
c347a1c3 1854 it->multibyte_p = 1;
ee569018
KH
1855 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1856 face = FACE_FROM_ID (it->f, it->face_id);
1857 }
06a2c219
GM
1858 }
1859
ee569018
KH
1860 /* Get font to use. Encode IT->char_to_display. */
1861 x_get_char_face_and_encoding (it->f, it->char_to_display,
1862 it->face_id, &char2b,
abfb6b46 1863 it->multibyte_p, 0);
06a2c219
GM
1864 font = face->font;
1865
1866 /* When no suitable font found, use the default font. */
1867 font_not_found_p = font == NULL;
1868 if (font_not_found_p)
b4192550
KH
1869 {
1870 font = FRAME_FONT (it->f);
1871 boff = it->f->output_data.x->baseline_offset;
1872 font_info = NULL;
1873 }
1874 else
1875 {
1876 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1877 boff = font_info->baseline_offset;
1878 if (font_info->vertical_centering)
1879 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1880 }
06a2c219
GM
1881
1882 if (it->char_to_display >= ' '
1883 && (!it->multibyte_p || it->char_to_display < 128))
1884 {
1885 /* Either unibyte or ASCII. */
1886 int stretched_p;
1887
1888 it->nglyphs = 1;
06a2c219
GM
1889
1890 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1891 it->ascent = font->ascent + boff;
1892 it->descent = font->descent - boff;
474848ac
GM
1893
1894 if (pcm)
1895 {
1896 it->phys_ascent = pcm->ascent + boff;
1897 it->phys_descent = pcm->descent - boff;
1898 it->pixel_width = pcm->width;
1899 }
1900 else
1901 {
1902 it->glyph_not_available_p = 1;
1903 it->phys_ascent = font->ascent + boff;
1904 it->phys_descent = font->descent - boff;
1905 it->pixel_width = FONT_WIDTH (font);
1906 }
06a2c219
GM
1907
1908 /* If this is a space inside a region of text with
1909 `space-width' property, change its width. */
1910 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1911 if (stretched_p)
1912 it->pixel_width *= XFLOATINT (it->space_width);
1913
1914 /* If face has a box, add the box thickness to the character
1915 height. If character has a box line to the left and/or
1916 right, add the box line width to the character's width. */
1917 if (face->box != FACE_NO_BOX)
1918 {
1919 int thick = face->box_line_width;
1920
ea2ba0d4
KH
1921 if (thick > 0)
1922 {
1923 it->ascent += thick;
1924 it->descent += thick;
1925 }
1926 else
1927 thick = -thick;
1928
06a2c219
GM
1929 if (it->start_of_box_run_p)
1930 it->pixel_width += thick;
1931 if (it->end_of_box_run_p)
1932 it->pixel_width += thick;
1933 }
1934
1935 /* If face has an overline, add the height of the overline
1936 (1 pixel) and a 1 pixel margin to the character height. */
1937 if (face->overline_p)
1938 it->ascent += 2;
1939
1940 take_vertical_position_into_account (it);
1941
1942 /* If we have to actually produce glyphs, do it. */
1943 if (it->glyph_row)
1944 {
1945 if (stretched_p)
1946 {
1947 /* Translate a space with a `space-width' property
1948 into a stretch glyph. */
1949 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1950 x_append_stretch_glyph (it, it->object, it->pixel_width,
1951 it->ascent + it->descent, ascent);
1952 }
1953 else
1954 x_append_glyph (it);
1955
1956 /* If characters with lbearing or rbearing are displayed
1957 in this line, record that fact in a flag of the
1958 glyph row. This is used to optimize X output code. */
1c7e22fd 1959 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1960 it->glyph_row->contains_overlapping_glyphs_p = 1;
1961 }
1962 }
1963 else if (it->char_to_display == '\n')
1964 {
1965 /* A newline has no width but we need the height of the line. */
1966 it->pixel_width = 0;
1967 it->nglyphs = 0;
b4192550
KH
1968 it->ascent = it->phys_ascent = font->ascent + boff;
1969 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1970
ea2ba0d4
KH
1971 if (face->box != FACE_NO_BOX
1972 && face->box_line_width > 0)
06a2c219 1973 {
ea2ba0d4
KH
1974 it->ascent += face->box_line_width;
1975 it->descent += face->box_line_width;
06a2c219
GM
1976 }
1977 }
1978 else if (it->char_to_display == '\t')
1979 {
1980 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1981 int x = it->current_x + it->continuation_lines_width;
06a2c219 1982 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1983
1984 /* If the distance from the current position to the next tab
1985 stop is less than a canonical character width, use the
1986 tab stop after that. */
1987 if (next_tab_x - x < CANON_X_UNIT (it->f))
1988 next_tab_x += tab_width;
06a2c219
GM
1989
1990 it->pixel_width = next_tab_x - x;
1991 it->nglyphs = 1;
b4192550
KH
1992 it->ascent = it->phys_ascent = font->ascent + boff;
1993 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1994
1995 if (it->glyph_row)
1996 {
1997 double ascent = (double) it->ascent / (it->ascent + it->descent);
1998 x_append_stretch_glyph (it, it->object, it->pixel_width,
1999 it->ascent + it->descent, ascent);
2000 }
2001 }
2002 else
2003 {
2004 /* A multi-byte character. Assume that the display width of the
2005 character is the width of the character multiplied by the
b4192550 2006 width of the font. */
06a2c219 2007
b4192550
KH
2008 /* If we found a font, this font should give us the right
2009 metrics. If we didn't find a font, use the frame's
2010 default font and calculate the width of the character
2011 from the charset width; this is what old redisplay code
2012 did. */
2013 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
2014 if (font_not_found_p || !pcm)
2015 {
2016 int charset = CHAR_CHARSET (it->char_to_display);
2017
2018 it->glyph_not_available_p = 1;
2019 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2020 * CHARSET_WIDTH (charset));
2021 it->phys_ascent = font->ascent + boff;
2022 it->phys_descent = font->descent - boff;
2023 }
2024 else
2025 {
2026 it->pixel_width = pcm->width;
2027 it->phys_ascent = pcm->ascent + boff;
2028 it->phys_descent = pcm->descent - boff;
2029 if (it->glyph_row
2030 && (pcm->lbearing < 0
2031 || pcm->rbearing > pcm->width))
2032 it->glyph_row->contains_overlapping_glyphs_p = 1;
2033 }
b4192550
KH
2034 it->nglyphs = 1;
2035 it->ascent = font->ascent + boff;
2036 it->descent = font->descent - boff;
06a2c219
GM
2037 if (face->box != FACE_NO_BOX)
2038 {
2039 int thick = face->box_line_width;
ea2ba0d4
KH
2040
2041 if (thick > 0)
2042 {
2043 it->ascent += thick;
2044 it->descent += thick;
2045 }
2046 else
2047 thick = - thick;
06a2c219
GM
2048
2049 if (it->start_of_box_run_p)
2050 it->pixel_width += thick;
2051 if (it->end_of_box_run_p)
2052 it->pixel_width += thick;
2053 }
2054
2055 /* If face has an overline, add the height of the overline
2056 (1 pixel) and a 1 pixel margin to the character height. */
2057 if (face->overline_p)
2058 it->ascent += 2;
2059
2060 take_vertical_position_into_account (it);
2061
2062 if (it->glyph_row)
2063 x_append_glyph (it);
2064 }
a4249304 2065 it->multibyte_p = saved_multibyte_p;
06a2c219 2066 }
b4192550
KH
2067 else if (it->what == IT_COMPOSITION)
2068 {
2069 /* Note: A composition is represented as one glyph in the
2070 glyph matrix. There are no padding glyphs. */
2071 XChar2b char2b;
2072 XFontStruct *font;
ee569018 2073 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2074 XCharStruct *pcm;
2075 int font_not_found_p;
2076 struct font_info *font_info;
2077 int boff; /* baseline offset */
2078 struct composition *cmp = composition_table[it->cmp_id];
2079
2080 /* Maybe translate single-byte characters to multibyte. */
2081 it->char_to_display = it->c;
2082 if (unibyte_display_via_language_environment
2083 && SINGLE_BYTE_CHAR_P (it->c)
2084 && (it->c >= 0240
2085 || (it->c >= 0200
2086 && !NILP (Vnonascii_translation_table))))
2087 {
2088 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2089 }
2090
2091 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2092 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2093 face = FACE_FROM_ID (it->f, it->face_id);
2094 x_get_char_face_and_encoding (it->f, it->char_to_display,
abfb6b46 2095 it->face_id, &char2b, it->multibyte_p, 0);
b4192550
KH
2096 font = face->font;
2097
2098 /* When no suitable font found, use the default font. */
2099 font_not_found_p = font == NULL;
2100 if (font_not_found_p)
2101 {
2102 font = FRAME_FONT (it->f);
2103 boff = it->f->output_data.x->baseline_offset;
2104 font_info = NULL;
2105 }
2106 else
2107 {
2108 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2109 boff = font_info->baseline_offset;
2110 if (font_info->vertical_centering)
2111 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2112 }
2113
2114 /* There are no padding glyphs, so there is only one glyph to
2115 produce for the composition. Important is that pixel_width,
2116 ascent and descent are the values of what is drawn by
2117 draw_glyphs (i.e. the values of the overall glyphs composed). */
2118 it->nglyphs = 1;
2119
2120 /* If we have not yet calculated pixel size data of glyphs of
2121 the composition for the current face font, calculate them
2122 now. Theoretically, we have to check all fonts for the
2123 glyphs, but that requires much time and memory space. So,
2124 here we check only the font of the first glyph. This leads
2125 to incorrect display very rarely, and C-l (recenter) can
2126 correct the display anyway. */
2127 if (cmp->font != (void *) font)
2128 {
2129 /* Ascent and descent of the font of the first character of
2130 this composition (adjusted by baseline offset). Ascent
2131 and descent of overall glyphs should not be less than
2132 them respectively. */
2133 int font_ascent = font->ascent + boff;
2134 int font_descent = font->descent - boff;
2135 /* Bounding box of the overall glyphs. */
2136 int leftmost, rightmost, lowest, highest;
329bed06 2137 int i, width, ascent, descent;
b4192550
KH
2138
2139 cmp->font = (void *) font;
2140
2141 /* Initialize the bounding box. */
1bdeec2e
KH
2142 if (font_info
2143 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2144 {
2145 width = pcm->width;
2146 ascent = pcm->ascent;
2147 descent = pcm->descent;
2148 }
2149 else
2150 {
2151 width = FONT_WIDTH (font);
2152 ascent = font->ascent;
2153 descent = font->descent;
2154 }
2155
2156 rightmost = width;
2157 lowest = - descent + boff;
2158 highest = ascent + boff;
b4192550 2159 leftmost = 0;
329bed06 2160
b4192550
KH
2161 if (font_info
2162 && font_info->default_ascent
2163 && CHAR_TABLE_P (Vuse_default_ascent)
2164 && !NILP (Faref (Vuse_default_ascent,
2165 make_number (it->char_to_display))))
2166 highest = font_info->default_ascent + boff;
2167
2168 /* Draw the first glyph at the normal position. It may be
2169 shifted to right later if some other glyphs are drawn at
2170 the left. */
2171 cmp->offsets[0] = 0;
2172 cmp->offsets[1] = boff;
2173
2174 /* Set cmp->offsets for the remaining glyphs. */
2175 for (i = 1; i < cmp->glyph_len; i++)
2176 {
2177 int left, right, btm, top;
2178 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2179 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2180
2181 face = FACE_FROM_ID (it->f, face_id);
2182 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
abfb6b46 2183 it->multibyte_p, 0);
b4192550
KH
2184 font = face->font;
2185 if (font == NULL)
2186 {
2187 font = FRAME_FONT (it->f);
2188 boff = it->f->output_data.x->baseline_offset;
2189 font_info = NULL;
2190 }
2191 else
2192 {
2193 font_info
2194 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2195 boff = font_info->baseline_offset;
2196 if (font_info->vertical_centering)
2197 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2198 }
2199
1bdeec2e
KH
2200 if (font_info
2201 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2202 {
2203 width = pcm->width;
2204 ascent = pcm->ascent;
2205 descent = pcm->descent;
2206 }
2207 else
2208 {
2209 width = FONT_WIDTH (font);
1bdeec2e
KH
2210 ascent = 1;
2211 descent = 0;
329bed06 2212 }
b4192550
KH
2213
2214 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2215 {
2216 /* Relative composition with or without
2217 alternate chars. */
329bed06
GM
2218 left = (leftmost + rightmost - width) / 2;
2219 btm = - descent + boff;
b4192550
KH
2220 if (font_info && font_info->relative_compose
2221 && (! CHAR_TABLE_P (Vignore_relative_composition)
2222 || NILP (Faref (Vignore_relative_composition,
2223 make_number (ch)))))
2224 {
2225
329bed06 2226 if (- descent >= font_info->relative_compose)
b4192550
KH
2227 /* One extra pixel between two glyphs. */
2228 btm = highest + 1;
329bed06 2229 else if (ascent <= 0)
b4192550 2230 /* One extra pixel between two glyphs. */
329bed06 2231 btm = lowest - 1 - ascent - descent;
b4192550
KH
2232 }
2233 }
2234 else
2235 {
2236 /* A composition rule is specified by an integer
2237 value that encodes global and new reference
2238 points (GREF and NREF). GREF and NREF are
2239 specified by numbers as below:
2240
2241 0---1---2 -- ascent
2242 | |
2243 | |
2244 | |
2245 9--10--11 -- center
2246 | |
2247 ---3---4---5--- baseline
2248 | |
2249 6---7---8 -- descent
2250 */
2251 int rule = COMPOSITION_RULE (cmp, i);
2252 int gref, nref, grefx, grefy, nrefx, nrefy;
2253
2254 COMPOSITION_DECODE_RULE (rule, gref, nref);
2255 grefx = gref % 3, nrefx = nref % 3;
2256 grefy = gref / 3, nrefy = nref / 3;
2257
2258 left = (leftmost
2259 + grefx * (rightmost - leftmost) / 2
329bed06 2260 - nrefx * width / 2);
b4192550
KH
2261 btm = ((grefy == 0 ? highest
2262 : grefy == 1 ? 0
2263 : grefy == 2 ? lowest
2264 : (highest + lowest) / 2)
329bed06
GM
2265 - (nrefy == 0 ? ascent + descent
2266 : nrefy == 1 ? descent - boff
b4192550 2267 : nrefy == 2 ? 0
329bed06 2268 : (ascent + descent) / 2));
b4192550
KH
2269 }
2270
2271 cmp->offsets[i * 2] = left;
329bed06 2272 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2273
2274 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2275 right = left + width;
2276 top = btm + descent + ascent;
b4192550
KH
2277 if (left < leftmost)
2278 leftmost = left;
2279 if (right > rightmost)
2280 rightmost = right;
2281 if (top > highest)
2282 highest = top;
2283 if (btm < lowest)
2284 lowest = btm;
2285 }
2286
2287 /* If there are glyphs whose x-offsets are negative,
2288 shift all glyphs to the right and make all x-offsets
2289 non-negative. */
2290 if (leftmost < 0)
2291 {
2292 for (i = 0; i < cmp->glyph_len; i++)
2293 cmp->offsets[i * 2] -= leftmost;
2294 rightmost -= leftmost;
2295 }
2296
2297 cmp->pixel_width = rightmost;
2298 cmp->ascent = highest;
2299 cmp->descent = - lowest;
2300 if (cmp->ascent < font_ascent)
2301 cmp->ascent = font_ascent;
2302 if (cmp->descent < font_descent)
2303 cmp->descent = font_descent;
2304 }
2305
2306 it->pixel_width = cmp->pixel_width;
2307 it->ascent = it->phys_ascent = cmp->ascent;
2308 it->descent = it->phys_descent = cmp->descent;
2309
2310 if (face->box != FACE_NO_BOX)
2311 {
2312 int thick = face->box_line_width;
ea2ba0d4
KH
2313
2314 if (thick > 0)
2315 {
2316 it->ascent += thick;
2317 it->descent += thick;
2318 }
2319 else
2320 thick = - thick;
b4192550
KH
2321
2322 if (it->start_of_box_run_p)
2323 it->pixel_width += thick;
2324 if (it->end_of_box_run_p)
2325 it->pixel_width += thick;
2326 }
2327
2328 /* If face has an overline, add the height of the overline
2329 (1 pixel) and a 1 pixel margin to the character height. */
2330 if (face->overline_p)
2331 it->ascent += 2;
2332
2333 take_vertical_position_into_account (it);
2334
2335 if (it->glyph_row)
2336 x_append_composite_glyph (it);
2337 }
06a2c219
GM
2338 else if (it->what == IT_IMAGE)
2339 x_produce_image_glyph (it);
2340 else if (it->what == IT_STRETCH)
2341 x_produce_stretch_glyph (it);
2342
3017fdd1
GM
2343 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2344 because this isn't true for images with `:ascent 100'. */
2345 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2346 if (it->area == TEXT_AREA)
2347 it->current_x += it->pixel_width;
66ac4b0e 2348
d365f5bb
GM
2349 it->descent += it->extra_line_spacing;
2350
06a2c219
GM
2351 it->max_ascent = max (it->max_ascent, it->ascent);
2352 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2353 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2354 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2355}
2356
2357
2358/* Estimate the pixel height of the mode or top line on frame F.
2359 FACE_ID specifies what line's height to estimate. */
2360
2361int
2362x_estimate_mode_line_height (f, face_id)
2363 struct frame *f;
2364 enum face_id face_id;
2365{
43281ee3 2366 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2367
2368 /* This function is called so early when Emacs starts that the face
2369 cache and mode line face are not yet initialized. */
2370 if (FRAME_FACE_CACHE (f))
2371 {
2372 struct face *face = FACE_FROM_ID (f, face_id);
2373 if (face)
43281ee3
GM
2374 {
2375 if (face->font)
2376 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2377 if (face->box_line_width > 0)
2378 height += 2 * face->box_line_width;
43281ee3 2379 }
06a2c219
GM
2380 }
2381
2382 return height;
2383}
2384
2385\f
2386/***********************************************************************
2387 Glyph display
2388 ***********************************************************************/
2389
2390/* A sequence of glyphs to be drawn in the same face.
2391
2392 This data structure is not really completely X specific, so it
2393 could possibly, at least partially, be useful for other systems. It
2394 is currently not part of the external redisplay interface because
2395 it's not clear what other systems will need. */
2396
2397struct glyph_string
2398{
2399 /* X-origin of the string. */
2400 int x;
2401
2402 /* Y-origin and y-position of the base line of this string. */
2403 int y, ybase;
2404
2405 /* The width of the string, not including a face extension. */
2406 int width;
2407
2408 /* The width of the string, including a face extension. */
2409 int background_width;
2410
2411 /* The height of this string. This is the height of the line this
2412 string is drawn in, and can be different from the height of the
2413 font the string is drawn in. */
2414 int height;
2415
2416 /* Number of pixels this string overwrites in front of its x-origin.
2417 This number is zero if the string has an lbearing >= 0; it is
2418 -lbearing, if the string has an lbearing < 0. */
2419 int left_overhang;
2420
2421 /* Number of pixels this string overwrites past its right-most
2422 nominal x-position, i.e. x + width. Zero if the string's
2423 rbearing is <= its nominal width, rbearing - width otherwise. */
2424 int right_overhang;
2425
2426 /* The frame on which the glyph string is drawn. */
2427 struct frame *f;
2428
2429 /* The window on which the glyph string is drawn. */
2430 struct window *w;
2431
2432 /* X display and window for convenience. */
2433 Display *display;
2434 Window window;
2435
2436 /* The glyph row for which this string was built. It determines the
2437 y-origin and height of the string. */
2438 struct glyph_row *row;
2439
2440 /* The area within row. */
2441 enum glyph_row_area area;
2442
2443 /* Characters to be drawn, and number of characters. */
2444 XChar2b *char2b;
2445 int nchars;
2446
06a2c219
GM
2447 /* A face-override for drawing cursors, mouse face and similar. */
2448 enum draw_glyphs_face hl;
2449
2450 /* Face in which this string is to be drawn. */
2451 struct face *face;
2452
2453 /* Font in which this string is to be drawn. */
2454 XFontStruct *font;
2455
2456 /* Font info for this string. */
2457 struct font_info *font_info;
2458
b4192550
KH
2459 /* Non-null means this string describes (part of) a composition.
2460 All characters from char2b are drawn composed. */
2461 struct composition *cmp;
06a2c219
GM
2462
2463 /* Index of this glyph string's first character in the glyph
b4192550
KH
2464 definition of CMP. If this is zero, this glyph string describes
2465 the first character of a composition. */
06a2c219
GM
2466 int gidx;
2467
2468 /* 1 means this glyph strings face has to be drawn to the right end
2469 of the window's drawing area. */
2470 unsigned extends_to_end_of_line_p : 1;
2471
2472 /* 1 means the background of this string has been drawn. */
2473 unsigned background_filled_p : 1;
2474
2475 /* 1 means glyph string must be drawn with 16-bit functions. */
2476 unsigned two_byte_p : 1;
2477
2478 /* 1 means that the original font determined for drawing this glyph
2479 string could not be loaded. The member `font' has been set to
2480 the frame's default font in this case. */
2481 unsigned font_not_found_p : 1;
2482
2483 /* 1 means that the face in which this glyph string is drawn has a
2484 stipple pattern. */
2485 unsigned stippled_p : 1;
2486
66ac4b0e
GM
2487 /* 1 means only the foreground of this glyph string must be drawn,
2488 and we should use the physical height of the line this glyph
2489 string appears in as clip rect. */
2490 unsigned for_overlaps_p : 1;
2491
06a2c219
GM
2492 /* The GC to use for drawing this glyph string. */
2493 GC gc;
2494
2495 /* A pointer to the first glyph in the string. This glyph
2496 corresponds to char2b[0]. Needed to draw rectangles if
2497 font_not_found_p is 1. */
2498 struct glyph *first_glyph;
2499
2500 /* Image, if any. */
2501 struct image *img;
2502
2503 struct glyph_string *next, *prev;
2504};
2505
2506
61869b99 2507#if GLYPH_DEBUG
06a2c219
GM
2508
2509static void
2510x_dump_glyph_string (s)
2511 struct glyph_string *s;
2512{
2513 fprintf (stderr, "glyph string\n");
2514 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2515 s->x, s->y, s->width, s->height);
2516 fprintf (stderr, " ybase = %d\n", s->ybase);
2517 fprintf (stderr, " hl = %d\n", s->hl);
2518 fprintf (stderr, " left overhang = %d, right = %d\n",
2519 s->left_overhang, s->right_overhang);
2520 fprintf (stderr, " nchars = %d\n", s->nchars);
2521 fprintf (stderr, " extends to end of line = %d\n",
2522 s->extends_to_end_of_line_p);
2523 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2524 fprintf (stderr, " bg width = %d\n", s->background_width);
2525}
2526
2527#endif /* GLYPH_DEBUG */
2528
2529
2530
2531static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2532 struct glyph_string **,
2533 struct glyph_string *,
2534 struct glyph_string *));
2535static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2536 struct glyph_string **,
2537 struct glyph_string *,
2538 struct glyph_string *));
2539static void x_append_glyph_string P_ ((struct glyph_string **,
2540 struct glyph_string **,
2541 struct glyph_string *));
2542static int x_left_overwritten P_ ((struct glyph_string *));
2543static int x_left_overwriting P_ ((struct glyph_string *));
2544static int x_right_overwritten P_ ((struct glyph_string *));
2545static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2546static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2547 int));
06a2c219
GM
2548static void x_init_glyph_string P_ ((struct glyph_string *,
2549 XChar2b *, struct window *,
2550 struct glyph_row *,
2551 enum glyph_row_area, int,
2552 enum draw_glyphs_face));
2553static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2554 enum glyph_row_area, int, int,
f0a48a01 2555 enum draw_glyphs_face, int));
06a2c219
GM
2556static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2557static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2558static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2559 int));
2560static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2561static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2562static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2563static void x_draw_glyph_string P_ ((struct glyph_string *));
2564static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2565static void x_set_cursor_gc P_ ((struct glyph_string *));
2566static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2567static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2568static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2569 int *, int *));
2570static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2571static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2572 unsigned long *, double, int));
06a2c219 2573static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2574 double, int, unsigned long));
06a2c219
GM
2575static void x_setup_relief_colors P_ ((struct glyph_string *));
2576static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2577static void x_draw_image_relief P_ ((struct glyph_string *));
2578static void x_draw_image_foreground P_ ((struct glyph_string *));
2579static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2580static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2581static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2582 int, int, int));
2583static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2584 int, int, int, int, XRectangle *));
2585static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2586 int, int, int, XRectangle *));
66ac4b0e
GM
2587static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2588 enum glyph_row_area));
209f68d9
GM
2589static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2590 struct glyph_row *,
2591 enum glyph_row_area, int, int));
06a2c219 2592
163dcff3
GM
2593#if GLYPH_DEBUG
2594static void x_check_font P_ ((struct frame *, XFontStruct *));
2595#endif
2596
06a2c219 2597
06a2c219
GM
2598/* Append the list of glyph strings with head H and tail T to the list
2599 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2600
2601static INLINE void
2602x_append_glyph_string_lists (head, tail, h, t)
2603 struct glyph_string **head, **tail;
2604 struct glyph_string *h, *t;
2605{
2606 if (h)
2607 {
2608 if (*head)
2609 (*tail)->next = h;
2610 else
2611 *head = h;
2612 h->prev = *tail;
2613 *tail = t;
2614 }
2615}
2616
2617
2618/* Prepend the list of glyph strings with head H and tail T to the
2619 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2620 result. */
2621
2622static INLINE void
2623x_prepend_glyph_string_lists (head, tail, h, t)
2624 struct glyph_string **head, **tail;
2625 struct glyph_string *h, *t;
2626{
2627 if (h)
2628 {
2629 if (*head)
2630 (*head)->prev = t;
2631 else
2632 *tail = t;
2633 t->next = *head;
2634 *head = h;
2635 }
2636}
2637
2638
2639/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2640 Set *HEAD and *TAIL to the resulting list. */
2641
2642static INLINE void
2643x_append_glyph_string (head, tail, s)
2644 struct glyph_string **head, **tail;
2645 struct glyph_string *s;
2646{
2647 s->next = s->prev = NULL;
2648 x_append_glyph_string_lists (head, tail, s, s);
2649}
2650
2651
2652/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2653 face. */
2654
2655static void
2656x_set_cursor_gc (s)
2657 struct glyph_string *s;
2658{
2659 if (s->font == FRAME_FONT (s->f)
2660 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2661 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2662 && !s->cmp)
06a2c219
GM
2663 s->gc = s->f->output_data.x->cursor_gc;
2664 else
2665 {
2666 /* Cursor on non-default face: must merge. */
2667 XGCValues xgcv;
2668 unsigned long mask;
2669
2670 xgcv.background = s->f->output_data.x->cursor_pixel;
2671 xgcv.foreground = s->face->background;
2672
2673 /* If the glyph would be invisible, try a different foreground. */
2674 if (xgcv.foreground == xgcv.background)
2675 xgcv.foreground = s->face->foreground;
2676 if (xgcv.foreground == xgcv.background)
2677 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2678 if (xgcv.foreground == xgcv.background)
2679 xgcv.foreground = s->face->foreground;
2680
2681 /* Make sure the cursor is distinct from text in this face. */
2682 if (xgcv.background == s->face->background
2683 && xgcv.foreground == s->face->foreground)
2684 {
2685 xgcv.background = s->face->foreground;
2686 xgcv.foreground = s->face->background;
2687 }
2688
2689 IF_DEBUG (x_check_font (s->f, s->font));
2690 xgcv.font = s->font->fid;
2691 xgcv.graphics_exposures = False;
2692 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2693
2694 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2695 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2696 mask, &xgcv);
2697 else
2698 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2699 = XCreateGC (s->display, s->window, mask, &xgcv);
2700
2701 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2702 }
2703}
2704
2705
2706/* Set up S->gc of glyph string S for drawing text in mouse face. */
2707
2708static void
2709x_set_mouse_face_gc (s)
2710 struct glyph_string *s;
2711{
2712 int face_id;
ee569018 2713 struct face *face;
06a2c219 2714
e4ded23c 2715 /* What face has to be used last for the mouse face? */
06a2c219 2716 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2717 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2718 if (face == NULL)
2719 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2720
033e3e18
GM
2721 if (s->first_glyph->type == CHAR_GLYPH)
2722 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2723 else
2724 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2725 s->face = FACE_FROM_ID (s->f, face_id);
2726 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2727
2728 /* If font in this face is same as S->font, use it. */
2729 if (s->font == s->face->font)
2730 s->gc = s->face->gc;
2731 else
2732 {
2733 /* Otherwise construct scratch_cursor_gc with values from FACE
2734 but font FONT. */
2735 XGCValues xgcv;
2736 unsigned long mask;
2737
2738 xgcv.background = s->face->background;
2739 xgcv.foreground = s->face->foreground;
2740 IF_DEBUG (x_check_font (s->f, s->font));
2741 xgcv.font = s->font->fid;
2742 xgcv.graphics_exposures = False;
2743 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2744
2745 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2746 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2747 mask, &xgcv);
2748 else
2749 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2750 = XCreateGC (s->display, s->window, mask, &xgcv);
2751
2752 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2753 }
2754
2755 xassert (s->gc != 0);
2756}
2757
2758
2759/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2760 Faces to use in the mode line have already been computed when the
2761 matrix was built, so there isn't much to do, here. */
2762
2763static INLINE void
2764x_set_mode_line_face_gc (s)
2765 struct glyph_string *s;
2766{
2767 s->gc = s->face->gc;
06a2c219
GM
2768}
2769
2770
2771/* Set S->gc of glyph string S for drawing that glyph string. Set
2772 S->stippled_p to a non-zero value if the face of S has a stipple
2773 pattern. */
2774
2775static INLINE void
2776x_set_glyph_string_gc (s)
2777 struct glyph_string *s;
2778{
209f68d9
GM
2779 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2780
06a2c219
GM
2781 if (s->hl == DRAW_NORMAL_TEXT)
2782 {
2783 s->gc = s->face->gc;
2784 s->stippled_p = s->face->stipple != 0;
2785 }
2786 else if (s->hl == DRAW_INVERSE_VIDEO)
2787 {
2788 x_set_mode_line_face_gc (s);
2789 s->stippled_p = s->face->stipple != 0;
2790 }
2791 else if (s->hl == DRAW_CURSOR)
2792 {
2793 x_set_cursor_gc (s);
2794 s->stippled_p = 0;
2795 }
2796 else if (s->hl == DRAW_MOUSE_FACE)
2797 {
2798 x_set_mouse_face_gc (s);
2799 s->stippled_p = s->face->stipple != 0;
2800 }
2801 else if (s->hl == DRAW_IMAGE_RAISED
2802 || s->hl == DRAW_IMAGE_SUNKEN)
2803 {
2804 s->gc = s->face->gc;
2805 s->stippled_p = s->face->stipple != 0;
2806 }
2807 else
2808 {
2809 s->gc = s->face->gc;
2810 s->stippled_p = s->face->stipple != 0;
2811 }
2812
2813 /* GC must have been set. */
2814 xassert (s->gc != 0);
2815}
2816
2817
2818/* Return in *R the clipping rectangle for glyph string S. */
2819
2820static void
2821x_get_glyph_string_clip_rect (s, r)
2822 struct glyph_string *s;
2823 XRectangle *r;
2824{
2825 if (s->row->full_width_p)
2826 {
2827 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2828 int canon_x = CANON_X_UNIT (s->f);
2829
2830 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2831 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2832
2833 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2834 {
1da3fd71 2835 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2836 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2837 r->x -= width;
2838 }
2839
b9432a85 2840 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2841
06a2c219
GM
2842 /* Unless displaying a mode or menu bar line, which are always
2843 fully visible, clip to the visible part of the row. */
2844 if (s->w->pseudo_window_p)
2845 r->height = s->row->visible_height;
2846 else
2847 r->height = s->height;
2848 }
2849 else
2850 {
2851 /* This is a text line that may be partially visible. */
2852 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2853 r->width = window_box_width (s->w, s->area);
2854 r->height = s->row->visible_height;
2855 }
2856
66ac4b0e
GM
2857 /* If S draws overlapping rows, it's sufficient to use the top and
2858 bottom of the window for clipping because this glyph string
2859 intentionally draws over other lines. */
2860 if (s->for_overlaps_p)
2861 {
045dee35 2862 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2863 r->height = window_text_bottom_y (s->w) - r->y;
2864 }
98b8a90f
GM
2865 else
2866 {
2867 /* Don't use S->y for clipping because it doesn't take partially
2868 visible lines into account. For example, it can be negative for
2869 partially visible lines at the top of a window. */
2870 if (!s->row->full_width_p
2871 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2872 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2873 else
2874 r->y = max (0, s->row->y);
2875
2876 /* If drawing a tool-bar window, draw it over the internal border
2877 at the top of the window. */
2878 if (s->w == XWINDOW (s->f->tool_bar_window))
2879 r->y -= s->f->output_data.x->internal_border_width;
2880 }
2881
66ac4b0e 2882 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2883}
2884
2885
2886/* Set clipping for output of glyph string S. S may be part of a mode
2887 line or menu if we don't have X toolkit support. */
2888
2889static INLINE void
2890x_set_glyph_string_clipping (s)
2891 struct glyph_string *s;
2892{
2893 XRectangle r;
2894 x_get_glyph_string_clip_rect (s, &r);
2895 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2896}
2897
2898
2899/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2900 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2901
2902static INLINE void
2903x_compute_glyph_string_overhangs (s)
2904 struct glyph_string *s;
2905{
b4192550 2906 if (s->cmp == NULL
06a2c219
GM
2907 && s->first_glyph->type == CHAR_GLYPH)
2908 {
2909 XCharStruct cs;
2910 int direction, font_ascent, font_descent;
2911 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2912 &font_ascent, &font_descent, &cs);
2913 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2914 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2915 }
2916}
2917
2918
2919/* Compute overhangs and x-positions for glyph string S and its
2920 predecessors, or successors. X is the starting x-position for S.
2921 BACKWARD_P non-zero means process predecessors. */
2922
2923static void
2924x_compute_overhangs_and_x (s, x, backward_p)
2925 struct glyph_string *s;
2926 int x;
2927 int backward_p;
2928{
2929 if (backward_p)
2930 {
2931 while (s)
2932 {
2933 x_compute_glyph_string_overhangs (s);
2934 x -= s->width;
2935 s->x = x;
2936 s = s->prev;
2937 }
2938 }
2939 else
2940 {
2941 while (s)
2942 {
2943 x_compute_glyph_string_overhangs (s);
2944 s->x = x;
2945 x += s->width;
2946 s = s->next;
2947 }
2948 }
2949}
2950
2951
2952/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2953 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2954 assumed to be zero. */
06a2c219
GM
2955
2956static void
2957x_get_glyph_overhangs (glyph, f, left, right)
2958 struct glyph *glyph;
2959 struct frame *f;
2960 int *left, *right;
2961{
06a2c219
GM
2962 *left = *right = 0;
2963
b4192550 2964 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2965 {
2966 XFontStruct *font;
2967 struct face *face;
2968 struct font_info *font_info;
2969 XChar2b char2b;
ee569018
KH
2970 XCharStruct *pcm;
2971
2972 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2973 font = face->font;
2974 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2975 if (font
2976 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2977 {
06a2c219
GM
2978 if (pcm->rbearing > pcm->width)
2979 *right = pcm->rbearing - pcm->width;
2980 if (pcm->lbearing < 0)
2981 *left = -pcm->lbearing;
2982 }
2983 }
2984}
2985
2986
2987/* Return the index of the first glyph preceding glyph string S that
2988 is overwritten by S because of S's left overhang. Value is -1
2989 if no glyphs are overwritten. */
2990
2991static int
2992x_left_overwritten (s)
2993 struct glyph_string *s;
2994{
2995 int k;
2996
2997 if (s->left_overhang)
2998 {
2999 int x = 0, i;
3000 struct glyph *glyphs = s->row->glyphs[s->area];
3001 int first = s->first_glyph - glyphs;
3002
3003 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3004 x -= glyphs[i].pixel_width;
3005
3006 k = i + 1;
3007 }
3008 else
3009 k = -1;
3010
3011 return k;
3012}
3013
3014
3015/* Return the index of the first glyph preceding glyph string S that
3016 is overwriting S because of its right overhang. Value is -1 if no
3017 glyph in front of S overwrites S. */
3018
3019static int
3020x_left_overwriting (s)
3021 struct glyph_string *s;
3022{
3023 int i, k, x;
3024 struct glyph *glyphs = s->row->glyphs[s->area];
3025 int first = s->first_glyph - glyphs;
3026
3027 k = -1;
3028 x = 0;
3029 for (i = first - 1; i >= 0; --i)
3030 {
3031 int left, right;
3032 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3033 if (x + right > 0)
3034 k = i;
3035 x -= glyphs[i].pixel_width;
3036 }
3037
3038 return k;
3039}
3040
3041
3042/* Return the index of the last glyph following glyph string S that is
3043 not overwritten by S because of S's right overhang. Value is -1 if
3044 no such glyph is found. */
3045
3046static int
3047x_right_overwritten (s)
3048 struct glyph_string *s;
3049{
3050 int k = -1;
3051
3052 if (s->right_overhang)
3053 {
3054 int x = 0, i;
3055 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3056 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3057 int end = s->row->used[s->area];
3058
3059 for (i = first; i < end && s->right_overhang > x; ++i)
3060 x += glyphs[i].pixel_width;
3061
3062 k = i;
3063 }
3064
3065 return k;
3066}
3067
3068
3069/* Return the index of the last glyph following glyph string S that
3070 overwrites S because of its left overhang. Value is negative
3071 if no such glyph is found. */
3072
3073static int
3074x_right_overwriting (s)
3075 struct glyph_string *s;
3076{
3077 int i, k, x;
3078 int end = s->row->used[s->area];
3079 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3080 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3081
3082 k = -1;
3083 x = 0;
3084 for (i = first; i < end; ++i)
3085 {
3086 int left, right;
3087 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3088 if (x - left < 0)
3089 k = i;
3090 x += glyphs[i].pixel_width;
3091 }
3092
3093 return k;
3094}
3095
3096
3097/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3098
3099static INLINE void
3100x_clear_glyph_string_rect (s, x, y, w, h)
3101 struct glyph_string *s;
3102 int x, y, w, h;
3103{
3104 XGCValues xgcv;
3105 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3106 XSetForeground (s->display, s->gc, xgcv.background);
3107 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3108 XSetForeground (s->display, s->gc, xgcv.foreground);
3109}
3110
3111
3112/* Draw the background of glyph_string S. If S->background_filled_p
3113 is non-zero don't draw it. FORCE_P non-zero means draw the
3114 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3115 when a string preceding S draws into the background of S, or S
3116 contains the first component of a composition. */
06a2c219
GM
3117
3118static void
3119x_draw_glyph_string_background (s, force_p)
3120 struct glyph_string *s;
3121 int force_p;
3122{
3123 /* Nothing to do if background has already been drawn or if it
3124 shouldn't be drawn in the first place. */
3125 if (!s->background_filled_p)
3126 {
ea2ba0d4
KH
3127 int box_line_width = max (s->face->box_line_width, 0);
3128
b4192550 3129 if (s->stippled_p)
06a2c219
GM
3130 {
3131 /* Fill background with a stipple pattern. */
3132 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3133 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3134 s->y + box_line_width,
06a2c219 3135 s->background_width,
ea2ba0d4 3136 s->height - 2 * box_line_width);
06a2c219
GM
3137 XSetFillStyle (s->display, s->gc, FillSolid);
3138 s->background_filled_p = 1;
3139 }
ea2ba0d4 3140 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3141 || s->font_not_found_p
3142 || s->extends_to_end_of_line_p
06a2c219
GM
3143 || force_p)
3144 {
ea2ba0d4 3145 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3146 s->background_width,
ea2ba0d4 3147 s->height - 2 * box_line_width);
06a2c219
GM
3148 s->background_filled_p = 1;
3149 }
3150 }
3151}
3152
3153
3154/* Draw the foreground of glyph string S. */
3155
3156static void
3157x_draw_glyph_string_foreground (s)
3158 struct glyph_string *s;
3159{
3160 int i, x;
3161
3162 /* If first glyph of S has a left box line, start drawing the text
3163 of S to the right of that box line. */
3164 if (s->face->box != FACE_NO_BOX
3165 && s->first_glyph->left_box_line_p)
ea2ba0d4 3166 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3167 else
3168 x = s->x;
3169
b4192550
KH
3170 /* Draw characters of S as rectangles if S's font could not be
3171 loaded. */
3172 if (s->font_not_found_p)
06a2c219 3173 {
b4192550 3174 for (i = 0; i < s->nchars; ++i)
06a2c219 3175 {
b4192550
KH
3176 struct glyph *g = s->first_glyph + i;
3177 XDrawRectangle (s->display, s->window,
3178 s->gc, x, s->y, g->pixel_width - 1,
3179 s->height - 1);
3180 x += g->pixel_width;
06a2c219
GM
3181 }
3182 }
3183 else
3184 {
b4192550
KH
3185 char *char1b = (char *) s->char2b;
3186 int boff = s->font_info->baseline_offset;
06a2c219 3187
b4192550
KH
3188 if (s->font_info->vertical_centering)
3189 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3190
3191 /* If we can use 8-bit functions, condense S->char2b. */
3192 if (!s->two_byte_p)
3193 for (i = 0; i < s->nchars; ++i)
3194 char1b[i] = s->char2b[i].byte2;
3195
3196 /* Draw text with XDrawString if background has already been
3197 filled. Otherwise, use XDrawImageString. (Note that
3198 XDrawImageString is usually faster than XDrawString.) Always
3199 use XDrawImageString when drawing the cursor so that there is
3200 no chance that characters under a box cursor are invisible. */
3201 if (s->for_overlaps_p
3202 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3203 {
3204 /* Draw characters with 16-bit or 8-bit functions. */
3205 if (s->two_byte_p)
3206 XDrawString16 (s->display, s->window, s->gc, x,
3207 s->ybase - boff, s->char2b, s->nchars);
3208 else
3209 XDrawString (s->display, s->window, s->gc, x,
3210 s->ybase - boff, char1b, s->nchars);
3211 }
06a2c219
GM
3212 else
3213 {
b4192550
KH
3214 if (s->two_byte_p)
3215 XDrawImageString16 (s->display, s->window, s->gc, x,
3216 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3217 else
b4192550
KH
3218 XDrawImageString (s->display, s->window, s->gc, x,
3219 s->ybase - boff, char1b, s->nchars);
3220 }
3221 }
3222}
06a2c219 3223
b4192550 3224/* Draw the foreground of composite glyph string S. */
06a2c219 3225
b4192550
KH
3226static void
3227x_draw_composite_glyph_string_foreground (s)
3228 struct glyph_string *s;
3229{
3230 int i, x;
06a2c219 3231
b4192550
KH
3232 /* If first glyph of S has a left box line, start drawing the text
3233 of S to the right of that box line. */
3234 if (s->face->box != FACE_NO_BOX
3235 && s->first_glyph->left_box_line_p)
ea2ba0d4 3236 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3237 else
3238 x = s->x;
06a2c219 3239
b4192550
KH
3240 /* S is a glyph string for a composition. S->gidx is the index of
3241 the first character drawn for glyphs of this composition.
3242 S->gidx == 0 means we are drawing the very first character of
3243 this composition. */
06a2c219 3244
b4192550
KH
3245 /* Draw a rectangle for the composition if the font for the very
3246 first character of the composition could not be loaded. */
3247 if (s->font_not_found_p)
3248 {
3249 if (s->gidx == 0)
3250 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3251 s->width - 1, s->height - 1);
3252 }
3253 else
3254 {
3255 for (i = 0; i < s->nchars; i++, ++s->gidx)
3256 XDrawString16 (s->display, s->window, s->gc,
3257 x + s->cmp->offsets[s->gidx * 2],
3258 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3259 s->char2b + i, 1);
06a2c219
GM
3260 }
3261}
3262
3263
80c32bcc
GM
3264#ifdef USE_X_TOOLKIT
3265
3e71d8f2 3266static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
3267static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3268 XrmValue *, XrmValue *, XtPointer *));
3269static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3270 XrmValue *, Cardinal *));
80c32bcc 3271
3e71d8f2
GM
3272
3273/* Return the frame on which widget WIDGET is used.. Abort if frame
3274 cannot be determined. */
3275
e851c833 3276static struct frame *
3e71d8f2 3277x_frame_of_widget (widget)
80c32bcc 3278 Widget widget;
80c32bcc 3279{
80c32bcc 3280 struct x_display_info *dpyinfo;
5c187dee 3281 Lisp_Object tail;
3e71d8f2
GM
3282 struct frame *f;
3283
80c32bcc
GM
3284 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3285
3286 /* Find the top-level shell of the widget. Note that this function
3287 can be called when the widget is not yet realized, so XtWindow
3288 (widget) == 0. That's the reason we can't simply use
3289 x_any_window_to_frame. */
3290 while (!XtIsTopLevelShell (widget))
3291 widget = XtParent (widget);
3292
3293 /* Look for a frame with that top-level widget. Allocate the color
3294 on that frame to get the right gamma correction value. */
3295 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3296 if (GC_FRAMEP (XCAR (tail))
3297 && (f = XFRAME (XCAR (tail)),
3298 (f->output_data.nothing != 1
3299 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3300 && f->output_data.x->widget == widget)
3e71d8f2 3301 return f;
80c32bcc
GM
3302
3303 abort ();
3304}
3305
3e71d8f2
GM
3306
3307/* Allocate the color COLOR->pixel on the screen and display of
3308 widget WIDGET in colormap CMAP. If an exact match cannot be
3309 allocated, try the nearest color available. Value is non-zero
3310 if successful. This is called from lwlib. */
3311
3312int
3313x_alloc_nearest_color_for_widget (widget, cmap, color)
3314 Widget widget;
3315 Colormap cmap;
3316 XColor *color;
3317{
3318 struct frame *f = x_frame_of_widget (widget);
3319 return x_alloc_nearest_color (f, cmap, color);
3320}
3321
3322
46d516e5
MB
3323/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3324 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3325 If this produces the same color as PIXEL, try a color where all RGB
3326 values have DELTA added. Return the allocated color in *PIXEL.
3327 DISPLAY is the X display, CMAP is the colormap to operate on.
3328 Value is non-zero if successful. */
3329
3330int
3331x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3332 Widget widget;
3333 Display *display;
3334 Colormap cmap;
3335 unsigned long *pixel;
3336 double factor;
3337 int delta;
3338{
3339 struct frame *f = x_frame_of_widget (widget);
3340 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3341}
3342
3343
651f03b6
GM
3344/* Structure specifying which arguments should be passed by Xt to
3345 cvt_string_to_pixel. We want the widget's screen and colormap. */
3346
3347static XtConvertArgRec cvt_string_to_pixel_args[] =
3348 {
3349 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3350 sizeof (Screen *)},
3351 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3352 sizeof (Colormap)}
3353 };
3354
3355
3356/* The address of this variable is returned by
3357 cvt_string_to_pixel. */
3358
3359static Pixel cvt_string_to_pixel_value;
3360
3361
3362/* Convert a color name to a pixel color.
3363
3364 DPY is the display we are working on.
3365
3366 ARGS is an array of *NARGS XrmValue structures holding additional
3367 information about the widget for which the conversion takes place.
3368 The contents of this array are determined by the specification
3369 in cvt_string_to_pixel_args.
3370
3371 FROM is a pointer to an XrmValue which points to the color name to
3372 convert. TO is an XrmValue in which to return the pixel color.
3373
3374 CLOSURE_RET is a pointer to user-data, in which we record if
3375 we allocated the color or not.
3376
3377 Value is True if successful, False otherwise. */
3378
3379static Boolean
3380cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3381 Display *dpy;
3382 XrmValue *args;
3383 Cardinal *nargs;
3384 XrmValue *from, *to;
3385 XtPointer *closure_ret;
3386{
3387 Screen *screen;
3388 Colormap cmap;
3389 Pixel pixel;
3390 String color_name;
3391 XColor color;
3392
3393 if (*nargs != 2)
3394 {
3395 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3396 "wrongParameters", "cvt_string_to_pixel",
3397 "XtToolkitError",
3398 "Screen and colormap args required", NULL, NULL);
3399 return False;
3400 }
3401
3402 screen = *(Screen **) args[0].addr;
3403 cmap = *(Colormap *) args[1].addr;
3404 color_name = (String) from->addr;
3405
3406 if (strcmp (color_name, XtDefaultBackground) == 0)
3407 {
3408 *closure_ret = (XtPointer) False;
3409 pixel = WhitePixelOfScreen (screen);
3410 }
3411 else if (strcmp (color_name, XtDefaultForeground) == 0)
3412 {
3413 *closure_ret = (XtPointer) False;
3414 pixel = BlackPixelOfScreen (screen);
3415 }
3416 else if (XParseColor (dpy, cmap, color_name, &color)
3417 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3418 {
3419 pixel = color.pixel;
3420 *closure_ret = (XtPointer) True;
3421 }
3422 else
3423 {
3424 String params[1];
3425 Cardinal nparams = 1;
3426
3427 params[0] = color_name;
3428 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3429 "badValue", "cvt_string_to_pixel",
3430 "XtToolkitError", "Invalid color `%s'",
3431 params, &nparams);
3432 return False;
3433 }
3434
3435 if (to->addr != NULL)
3436 {
3437 if (to->size < sizeof (Pixel))
3438 {
3439 to->size = sizeof (Pixel);
3440 return False;
3441 }
3442
3443 *(Pixel *) to->addr = pixel;
3444 }
3445 else
3446 {
3447 cvt_string_to_pixel_value = pixel;
3448 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3449 }
3450
3451 to->size = sizeof (Pixel);
3452 return True;
3453}
3454
3455
3456/* Free a pixel color which was previously allocated via
3457 cvt_string_to_pixel. This is registered as the destructor
3458 for this type of resource via XtSetTypeConverter.
3459
3460 APP is the application context in which we work.
3461
3462 TO is a pointer to an XrmValue holding the color to free.
3463 CLOSURE is the value we stored in CLOSURE_RET for this color
3464 in cvt_string_to_pixel.
3465
3466 ARGS and NARGS are like for cvt_string_to_pixel. */
3467
3468static void
3469cvt_pixel_dtor (app, to, closure, args, nargs)
3470 XtAppContext app;
3471 XrmValuePtr to;
3472 XtPointer closure;
3473 XrmValuePtr args;
3474 Cardinal *nargs;
3475{
3476 if (*nargs != 2)
3477 {
3478 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3479 "XtToolkitError",
3480 "Screen and colormap arguments required",
3481 NULL, NULL);
3482 }
3483 else if (closure != NULL)
3484 {
3485 /* We did allocate the pixel, so free it. */
3486 Screen *screen = *(Screen **) args[0].addr;
3487 Colormap cmap = *(Colormap *) args[1].addr;
3488 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 3489 (Pixel *) to->addr, 1);
651f03b6
GM
3490 }
3491}
3492
3493
80c32bcc
GM
3494#endif /* USE_X_TOOLKIT */
3495
3496
f04e1297 3497/* Value is an array of XColor structures for the contents of the
651f03b6 3498 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
3499 Note that this probably shouldn't be called for large color maps,
3500 say a 24-bit TrueColor map. */
3501
3502static const XColor *
651f03b6
GM
3503x_color_cells (dpy, ncells)
3504 Display *dpy;
f04e1297
GM
3505 int *ncells;
3506{
651f03b6 3507 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
3508
3509 if (dpyinfo->color_cells == NULL)
3510 {
651f03b6 3511 Screen *screen = dpyinfo->screen;
f04e1297
GM
3512 int i;
3513
3514 dpyinfo->ncolor_cells
651f03b6 3515 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
3516 dpyinfo->color_cells
3517 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3518 * sizeof *dpyinfo->color_cells);
3519
3520 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3521 dpyinfo->color_cells[i].pixel = i;
3522
651f03b6 3523 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
3524 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3525 }
3526
3527 *ncells = dpyinfo->ncolor_cells;
3528 return dpyinfo->color_cells;
3529}
3530
3531
3532/* On frame F, translate pixel colors to RGB values for the NCOLORS
3533 colors in COLORS. Use cached information, if available. */
3534
3535void
3536x_query_colors (f, colors, ncolors)
3537 struct frame *f;
3538 XColor *colors;
3539 int ncolors;
3540{
3541 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3542
3543 if (dpyinfo->color_cells)
3544 {
3545 int i;
3546 for (i = 0; i < ncolors; ++i)
3547 {
3548 unsigned long pixel = colors[i].pixel;
3549 xassert (pixel < dpyinfo->ncolor_cells);
3550 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3551 colors[i] = dpyinfo->color_cells[pixel];
3552 }
3553 }
3554 else
3555 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3556}
3557
3558
3559/* On frame F, translate pixel color to RGB values for the color in
3560 COLOR. Use cached information, if available. */
3561
3562void
3563x_query_color (f, color)
3564 struct frame *f;
3565 XColor *color;
3566{
3567 x_query_colors (f, color, 1);
3568}
3569
3570
651f03b6
GM
3571/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3572 exact match can't be allocated, try the nearest color available.
3573 Value is non-zero if successful. Set *COLOR to the color
3574 allocated. */
06a2c219 3575
651f03b6
GM
3576static int
3577x_alloc_nearest_color_1 (dpy, cmap, color)
3578 Display *dpy;
06a2c219
GM
3579 Colormap cmap;
3580 XColor *color;
3581{
80c32bcc
GM
3582 int rc;
3583
651f03b6 3584 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
3585 if (rc == 0)
3586 {
3587 /* If we got to this point, the colormap is full, so we're going
3588 to try to get the next closest color. The algorithm used is
3589 a least-squares matching, which is what X uses for closest
3590 color matching with StaticColor visuals. */
3591 int nearest, i;
3592 unsigned long nearest_delta = ~0;
f04e1297 3593 int ncells;
651f03b6 3594 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
3595
3596 for (nearest = i = 0; i < ncells; ++i)
3597 {
3598 long dred = (color->red >> 8) - (cells[i].red >> 8);
3599 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3600 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3601 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3602
3603 if (delta < nearest_delta)
3604 {
3605 nearest = i;
3606 nearest_delta = delta;
3607 }
3608 }
3609
3610 color->red = cells[nearest].red;
3611 color->green = cells[nearest].green;
3612 color->blue = cells[nearest].blue;
651f03b6 3613 rc = XAllocColor (dpy, cmap, color);
06a2c219 3614 }
35efe0a1
GM
3615 else
3616 {
3617 /* If allocation succeeded, and the allocated pixel color is not
3618 equal to a cached pixel color recorded earlier, there was a
3619 change in the colormap, so clear the color cache. */
651f03b6 3620 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1
GM
3621 XColor *cached_color;
3622
3623 if (dpyinfo->color_cells
3624 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3625 (cached_color->red != color->red
3626 || cached_color->blue != color->blue
3627 || cached_color->green != color->green)))
35efe0a1
GM
3628 {
3629 xfree (dpyinfo->color_cells);
3630 dpyinfo->color_cells = NULL;
3631 dpyinfo->ncolor_cells = 0;
3632 }
3633 }
06a2c219 3634
d9c545da
GM
3635#ifdef DEBUG_X_COLORS
3636 if (rc)
3637 register_color (color->pixel);
3638#endif /* DEBUG_X_COLORS */
3639
06a2c219
GM
3640 return rc;
3641}
3642
3643
651f03b6
GM
3644/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3645 exact match can't be allocated, try the nearest color available.
3646 Value is non-zero if successful. Set *COLOR to the color
3647 allocated. */
3648
3649int
3650x_alloc_nearest_color (f, cmap, color)
3651 struct frame *f;
3652 Colormap cmap;
3653 XColor *color;
3654{
3655 gamma_correct (f, color);
3656 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3657}
3658
3659
d9c545da
GM
3660/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3661 It's necessary to do this instead of just using PIXEL directly to
3662 get color reference counts right. */
3663
3664unsigned long
3665x_copy_color (f, pixel)
3666 struct frame *f;
3667 unsigned long pixel;
3668{
3669 XColor color;
3670
3671 color.pixel = pixel;
3672 BLOCK_INPUT;
f04e1297 3673 x_query_color (f, &color);
d9c545da
GM
3674 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3675 UNBLOCK_INPUT;
3676#ifdef DEBUG_X_COLORS
3677 register_color (pixel);
3678#endif
3679 return color.pixel;
3680}
3681
3682
3e71d8f2
GM
3683/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3684 It's necessary to do this instead of just using PIXEL directly to
3685 get color reference counts right. */
3686
3687unsigned long
3688x_copy_dpy_color (dpy, cmap, pixel)
3689 Display *dpy;
3690 Colormap cmap;
3691 unsigned long pixel;
3692{
3693 XColor color;
3694
3695 color.pixel = pixel;
3696 BLOCK_INPUT;
3697 XQueryColor (dpy, cmap, &color);
3698 XAllocColor (dpy, cmap, &color);
3699 UNBLOCK_INPUT;
3700#ifdef DEBUG_X_COLORS
3701 register_color (pixel);
3702#endif
3703 return color.pixel;
3704}
3705
3706
6d8b0acd 3707/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3708 boosted.
6d8b0acd 3709
d7361edf
MB
3710 Nominally, highlight colors for `3d' faces are calculated by
3711 brightening an object's color by a constant scale factor, but this
3712 doesn't yield good results for dark colors, so for colors who's
3713 brightness is less than this value (on a scale of 0-65535) have an
3714 use an additional additive factor.
6d8b0acd
MB
3715
3716 The value here is set so that the default menu-bar/mode-line color
3717 (grey75) will not have its highlights changed at all. */
3718#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3719
3720
06a2c219
GM
3721/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3722 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3723 If this produces the same color as PIXEL, try a color where all RGB
3724 values have DELTA added. Return the allocated color in *PIXEL.
3725 DISPLAY is the X display, CMAP is the colormap to operate on.
3726 Value is non-zero if successful. */
3727
3728static int
3729x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3730 struct frame *f;
3731 Display *display;
3732 Colormap cmap;
3733 unsigned long *pixel;
68c45bf0 3734 double factor;
06a2c219
GM
3735 int delta;
3736{
3737 XColor color, new;
6d8b0acd 3738 long bright;
06a2c219
GM
3739 int success_p;
3740
3741 /* Get RGB color values. */
3742 color.pixel = *pixel;
f04e1297 3743 x_query_color (f, &color);
06a2c219
GM
3744
3745 /* Change RGB values by specified FACTOR. Avoid overflow! */
3746 xassert (factor >= 0);
3747 new.red = min (0xffff, factor * color.red);
3748 new.green = min (0xffff, factor * color.green);
3749 new.blue = min (0xffff, factor * color.blue);
3750
d7361edf
MB
3751 /* Calculate brightness of COLOR. */
3752 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3753
3754 /* We only boost colors that are darker than
3755 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3756 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3757 /* Make an additive adjustment to NEW, because it's dark enough so
3758 that scaling by FACTOR alone isn't enough. */
3759 {
3760 /* How far below the limit this color is (0 - 1, 1 being darker). */
3761 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3762 /* The additive adjustment. */
d7361edf 3763 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3764
3765 if (factor < 1)
3766 {
6d8b0acd
MB
3767 new.red = max (0, new.red - min_delta);
3768 new.green = max (0, new.green - min_delta);
3769 new.blue = max (0, new.blue - min_delta);
3770 }
3771 else
3772 {
3773 new.red = min (0xffff, min_delta + new.red);
3774 new.green = min (0xffff, min_delta + new.green);
3775 new.blue = min (0xffff, min_delta + new.blue);
3776 }
3777 }
3778
06a2c219 3779 /* Try to allocate the color. */
80c32bcc 3780 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3781 if (success_p)
3782 {
3783 if (new.pixel == *pixel)
3784 {
3785 /* If we end up with the same color as before, try adding
3786 delta to the RGB values. */
0d605c67 3787 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3788
3789 new.red = min (0xffff, delta + color.red);
3790 new.green = min (0xffff, delta + color.green);
3791 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3792 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3793 }
3794 else
3795 success_p = 1;
3796 *pixel = new.pixel;
3797 }
3798
3799 return success_p;
3800}
3801
3802
3803/* Set up the foreground color for drawing relief lines of glyph
3804 string S. RELIEF is a pointer to a struct relief containing the GC
3805 with which lines will be drawn. Use a color that is FACTOR or
3806 DELTA lighter or darker than the relief's background which is found
3807 in S->f->output_data.x->relief_background. If such a color cannot
3808 be allocated, use DEFAULT_PIXEL, instead. */
3809
3810static void
3811x_setup_relief_color (f, relief, factor, delta, default_pixel)
3812 struct frame *f;
3813 struct relief *relief;
68c45bf0 3814 double factor;
06a2c219
GM
3815 int delta;
3816 unsigned long default_pixel;
3817{
3818 XGCValues xgcv;
3819 struct x_output *di = f->output_data.x;
3820 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3821 unsigned long pixel;
3822 unsigned long background = di->relief_background;
43bd1b2b 3823 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3824 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3825 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3826
3827 xgcv.graphics_exposures = False;
3828 xgcv.line_width = 1;
3829
3830 /* Free previously allocated color. The color cell will be reused
3831 when it has been freed as many times as it was allocated, so this
3832 doesn't affect faces using the same colors. */
3833 if (relief->gc
3834 && relief->allocated_p)
3835 {
0d605c67 3836 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3837 relief->allocated_p = 0;
3838 }
3839
3840 /* Allocate new color. */
3841 xgcv.foreground = default_pixel;
3842 pixel = background;
dcd08bfb
GM
3843 if (dpyinfo->n_planes != 1
3844 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3845 {
3846 relief->allocated_p = 1;
3847 xgcv.foreground = relief->pixel = pixel;
3848 }
3849
3850 if (relief->gc == 0)
3851 {
dcd08bfb 3852 xgcv.stipple = dpyinfo->gray;
06a2c219 3853 mask |= GCStipple;
dcd08bfb 3854 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3855 }
3856 else
dcd08bfb 3857 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3858}
3859
3860
3861/* Set up colors for the relief lines around glyph string S. */
3862
3863static void
3864x_setup_relief_colors (s)
3865 struct glyph_string *s;
3866{
3867 struct x_output *di = s->f->output_data.x;
3868 unsigned long color;
3869
3870 if (s->face->use_box_color_for_shadows_p)
3871 color = s->face->box_color;
e2a57b34 3872 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 3873 && s->img->pixmap
e2a57b34
MB
3874 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3875 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
3876 else
3877 {
3878 XGCValues xgcv;
3879
3880 /* Get the background color of the face. */
3881 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3882 color = xgcv.background;
3883 }
3884
3885 if (di->white_relief.gc == 0
3886 || color != di->relief_background)
3887 {
3888 di->relief_background = color;
3889 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3890 WHITE_PIX_DEFAULT (s->f));
3891 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3892 BLACK_PIX_DEFAULT (s->f));
3893 }
3894}
3895
3896
3897/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3898 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3899 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3900 relief. LEFT_P non-zero means draw a relief on the left side of
3901 the rectangle. RIGHT_P non-zero means draw a relief on the right
3902 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3903 when drawing. */
3904
3905static void
3906x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3907 raised_p, left_p, right_p, clip_rect)
3908 struct frame *f;
3909 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3910 XRectangle *clip_rect;
3911{
de507556
GM
3912 Display *dpy = FRAME_X_DISPLAY (f);
3913 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
3914 int i;
3915 GC gc;
3916
3917 if (raised_p)
3918 gc = f->output_data.x->white_relief.gc;
3919 else
3920 gc = f->output_data.x->black_relief.gc;
de507556 3921 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3922
3923 /* Top. */
3924 for (i = 0; i < width; ++i)
de507556 3925 XDrawLine (dpy, window, gc,
06a2c219
GM
3926 left_x + i * left_p, top_y + i,
3927 right_x + 1 - i * right_p, top_y + i);
3928
3929 /* Left. */
3930 if (left_p)
3931 for (i = 0; i < width; ++i)
de507556 3932 XDrawLine (dpy, window, gc,
44655e77 3933 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 3934
de507556 3935 XSetClipMask (dpy, gc, None);
06a2c219
GM
3936 if (raised_p)
3937 gc = f->output_data.x->black_relief.gc;
3938 else
3939 gc = f->output_data.x->white_relief.gc;
de507556 3940 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3941
3942 /* Bottom. */
3943 for (i = 0; i < width; ++i)
de507556
GM
3944 XDrawLine (dpy, window, gc,
3945 left_x + i * left_p, bottom_y - i,
327f42ee 3946 right_x + 1 - i * right_p, bottom_y - i);
06a2c219
GM
3947
3948 /* Right. */
3949 if (right_p)
3950 for (i = 0; i < width; ++i)
de507556 3951 XDrawLine (dpy, window, gc,
06a2c219
GM
3952 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3953
de507556 3954 XSetClipMask (dpy, gc, None);
06a2c219
GM
3955}
3956
3957
3958/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3959 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3960 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3961 left side of the rectangle. RIGHT_P non-zero means draw a line
3962 on the right side of the rectangle. CLIP_RECT is the clipping
3963 rectangle to use when drawing. */
3964
3965static void
3966x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3967 left_p, right_p, clip_rect)
3968 struct glyph_string *s;
3969 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3970 XRectangle *clip_rect;
3971{
3972 XGCValues xgcv;
3973
3974 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3975 XSetForeground (s->display, s->gc, s->face->box_color);
3976 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3977
3978 /* Top. */
3979 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3980 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3981
3982 /* Left. */
3983 if (left_p)
3984 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3985 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3986
3987 /* Bottom. */
3988 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3989 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3990
3991 /* Right. */
3992 if (right_p)
3993 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3994 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3995
3996 XSetForeground (s->display, s->gc, xgcv.foreground);
3997 XSetClipMask (s->display, s->gc, None);
3998}
3999
4000
4001/* Draw a box around glyph string S. */
4002
4003static void
4004x_draw_glyph_string_box (s)
4005 struct glyph_string *s;
4006{
4007 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
4008 int left_p, right_p;
4009 struct glyph *last_glyph;
4010 XRectangle clip_rect;
4011
4012 last_x = window_box_right (s->w, s->area);
4013 if (s->row->full_width_p
4014 && !s->w->pseudo_window_p)
4015 {
3f332ef3 4016 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
06a2c219
GM
4017 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4018 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4019 }
4020
4021 /* The glyph that may have a right box line. */
b4192550 4022 last_glyph = (s->cmp || s->img
06a2c219
GM
4023 ? s->first_glyph
4024 : s->first_glyph + s->nchars - 1);
4025
ea2ba0d4 4026 width = abs (s->face->box_line_width);
06a2c219
GM
4027 raised_p = s->face->box == FACE_RAISED_BOX;
4028 left_x = s->x;
57ac7c81
GM
4029 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4030 ? last_x - 1
4031 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
4032 top_y = s->y;
4033 bottom_y = top_y + s->height - 1;
4034
4035 left_p = (s->first_glyph->left_box_line_p
4036 || (s->hl == DRAW_MOUSE_FACE
4037 && (s->prev == NULL
4038 || s->prev->hl != s->hl)));
4039 right_p = (last_glyph->right_box_line_p
4040 || (s->hl == DRAW_MOUSE_FACE
4041 && (s->next == NULL
4042 || s->next->hl != s->hl)));
327f42ee 4043
06a2c219
GM
4044 x_get_glyph_string_clip_rect (s, &clip_rect);
4045
4046 if (s->face->box == FACE_SIMPLE_BOX)
4047 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4048 left_p, right_p, &clip_rect);
4049 else
4050 {
4051 x_setup_relief_colors (s);
4052 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4053 width, raised_p, left_p, right_p, &clip_rect);
4054 }
4055}
4056
4057
4058/* Draw foreground of image glyph string S. */
4059
4060static void
4061x_draw_image_foreground (s)
4062 struct glyph_string *s;
4063{
4064 int x;
95af8492 4065 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4066
4067 /* If first glyph of S has a left box line, start drawing it to the
4068 right of that line. */
4069 if (s->face->box != FACE_NO_BOX
4070 && s->first_glyph->left_box_line_p)
ea2ba0d4 4071 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4072 else
4073 x = s->x;
4074
4075 /* If there is a margin around the image, adjust x- and y-position
4076 by that margin. */
22d650b8
GM
4077 x += s->img->hmargin;
4078 y += s->img->vmargin;
06a2c219
GM
4079
4080 if (s->img->pixmap)
4081 {
4082 if (s->img->mask)
4083 {
4084 /* We can't set both a clip mask and use XSetClipRectangles
4085 because the latter also sets a clip mask. We also can't
4086 trust on the shape extension to be available
4087 (XShapeCombineRegion). So, compute the rectangle to draw
4088 manually. */
4089 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4090 | GCFunction);
4091 XGCValues xgcv;
4092 XRectangle clip_rect, image_rect, r;
4093
4094 xgcv.clip_mask = s->img->mask;
4095 xgcv.clip_x_origin = x;
4096 xgcv.clip_y_origin = y;
4097 xgcv.function = GXcopy;
4098 XChangeGC (s->display, s->gc, mask, &xgcv);
4099
4100 x_get_glyph_string_clip_rect (s, &clip_rect);
4101 image_rect.x = x;
4102 image_rect.y = y;
4103 image_rect.width = s->img->width;
4104 image_rect.height = s->img->height;
4105 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4106 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4107 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4108 }
4109 else
4110 {
49ad1d99
GM
4111 XRectangle clip_rect, image_rect, r;
4112
4113 x_get_glyph_string_clip_rect (s, &clip_rect);
4114 image_rect.x = x;
4115 image_rect.y = y;
4116 image_rect.width = s->img->width;
4117 image_rect.height = s->img->height;
4118 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4119 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4120 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
4121
4122 /* When the image has a mask, we can expect that at
4123 least part of a mouse highlight or a block cursor will
4124 be visible. If the image doesn't have a mask, make
4125 a block cursor visible by drawing a rectangle around
4126 the image. I believe it's looking better if we do
4127 nothing here for mouse-face. */
4128 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4129 {
4130 int r = s->img->relief;
4131 if (r < 0) r = -r;
4132 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4133 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4134 }
06a2c219
GM
4135 }
4136 }
4137 else
4138 /* Draw a rectangle if image could not be loaded. */
4139 XDrawRectangle (s->display, s->window, s->gc, x, y,
4140 s->img->width - 1, s->img->height - 1);
4141}
4142
4143
4144/* Draw a relief around the image glyph string S. */
4145
4146static void
4147x_draw_image_relief (s)
4148 struct glyph_string *s;
4149{
4150 int x0, y0, x1, y1, thick, raised_p;
4151 XRectangle r;
4152 int x;
95af8492 4153 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4154
4155 /* If first glyph of S has a left box line, start drawing it to the
4156 right of that line. */
4157 if (s->face->box != FACE_NO_BOX
4158 && s->first_glyph->left_box_line_p)
ea2ba0d4 4159 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4160 else
4161 x = s->x;
4162
4163 /* If there is a margin around the image, adjust x- and y-position
4164 by that margin. */
22d650b8
GM
4165 x += s->img->hmargin;
4166 y += s->img->vmargin;
06a2c219
GM
4167
4168 if (s->hl == DRAW_IMAGE_SUNKEN
4169 || s->hl == DRAW_IMAGE_RAISED)
4170 {
62854fe2 4171 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
4172 raised_p = s->hl == DRAW_IMAGE_RAISED;
4173 }
4174 else
4175 {
4176 thick = abs (s->img->relief);
4177 raised_p = s->img->relief > 0;
4178 }
4179
4180 x0 = x - thick;
4181 y0 = y - thick;
4182 x1 = x + s->img->width + thick - 1;
4183 y1 = y + s->img->height + thick - 1;
4184
4185 x_setup_relief_colors (s);
4186 x_get_glyph_string_clip_rect (s, &r);
4187 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4188}
4189
4190
4191/* Draw the foreground of image glyph string S to PIXMAP. */
4192
4193static void
4194x_draw_image_foreground_1 (s, pixmap)
4195 struct glyph_string *s;
4196 Pixmap pixmap;
4197{
4198 int x;
95af8492 4199 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4200
4201 /* If first glyph of S has a left box line, start drawing it to the
4202 right of that line. */
4203 if (s->face->box != FACE_NO_BOX
4204 && s->first_glyph->left_box_line_p)
ea2ba0d4 4205 x = abs (s->face->box_line_width);
06a2c219
GM
4206 else
4207 x = 0;
4208
4209 /* If there is a margin around the image, adjust x- and y-position
4210 by that margin. */
22d650b8
GM
4211 x += s->img->hmargin;
4212 y += s->img->vmargin;
dc43ef94 4213
06a2c219
GM
4214 if (s->img->pixmap)
4215 {
4216 if (s->img->mask)
4217 {
4218 /* We can't set both a clip mask and use XSetClipRectangles
4219 because the latter also sets a clip mask. We also can't
4220 trust on the shape extension to be available
4221 (XShapeCombineRegion). So, compute the rectangle to draw
4222 manually. */
4223 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4224 | GCFunction);
4225 XGCValues xgcv;
4226
4227 xgcv.clip_mask = s->img->mask;
4228 xgcv.clip_x_origin = x;
4229 xgcv.clip_y_origin = y;
4230 xgcv.function = GXcopy;
4231 XChangeGC (s->display, s->gc, mask, &xgcv);
4232
4233 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4234 0, 0, s->img->width, s->img->height, x, y);
4235 XSetClipMask (s->display, s->gc, None);
4236 }
4237 else
4238 {
4239 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4240 0, 0, s->img->width, s->img->height, x, y);
4241
4242 /* When the image has a mask, we can expect that at
4243 least part of a mouse highlight or a block cursor will
4244 be visible. If the image doesn't have a mask, make
4245 a block cursor visible by drawing a rectangle around
4246 the image. I believe it's looking better if we do
4247 nothing here for mouse-face. */
4248 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4249 {
4250 int r = s->img->relief;
4251 if (r < 0) r = -r;
4252 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4253 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4254 }
06a2c219
GM
4255 }
4256 }
4257 else
4258 /* Draw a rectangle if image could not be loaded. */
4259 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4260 s->img->width - 1, s->img->height - 1);
4261}
dc43ef94 4262
990ba854 4263
06a2c219
GM
4264/* Draw part of the background of glyph string S. X, Y, W, and H
4265 give the rectangle to draw. */
a9a5b0a5 4266
06a2c219
GM
4267static void
4268x_draw_glyph_string_bg_rect (s, x, y, w, h)
4269 struct glyph_string *s;
4270 int x, y, w, h;
4271{
4272 if (s->stippled_p)
4273 {
4274 /* Fill background with a stipple pattern. */
4275 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4276 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4277 XSetFillStyle (s->display, s->gc, FillSolid);
4278 }
4279 else
4280 x_clear_glyph_string_rect (s, x, y, w, h);
4281}
07e34cb0 4282
b5210ea7 4283
06a2c219 4284/* Draw image glyph string S.
dc43ef94 4285
06a2c219
GM
4286 s->y
4287 s->x +-------------------------
4288 | s->face->box
4289 |
4290 | +-------------------------
4291 | | s->img->margin
4292 | |
4293 | | +-------------------
4294 | | | the image
dc43ef94 4295
06a2c219 4296 */
dc43ef94 4297
06a2c219
GM
4298static void
4299x_draw_image_glyph_string (s)
4300 struct glyph_string *s;
4301{
4302 int x, y;
ea2ba0d4
KH
4303 int box_line_hwidth = abs (s->face->box_line_width);
4304 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4305 int height;
4306 Pixmap pixmap = None;
4307
ea2ba0d4 4308 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4309
4310 /* Fill background with face under the image. Do it only if row is
4311 taller than image or if image has a clip mask to reduce
4312 flickering. */
4313 s->stippled_p = s->face->stipple != 0;
4314 if (height > s->img->height
22d650b8
GM
4315 || s->img->hmargin
4316 || s->img->vmargin
06a2c219
GM
4317 || s->img->mask
4318 || s->img->pixmap == 0
4319 || s->width != s->background_width)
4320 {
ea2ba0d4
KH
4321 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4322 x = s->x + box_line_hwidth;
06a2c219
GM
4323 else
4324 x = s->x;
4325
ea2ba0d4 4326 y = s->y + box_line_vwidth;
06a2c219
GM
4327
4328 if (s->img->mask)
4329 {
f9b5db02
GM
4330 /* Create a pixmap as large as the glyph string. Fill it
4331 with the background color. Copy the image to it, using
4332 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4333 Screen *screen = FRAME_X_SCREEN (s->f);
4334 int depth = DefaultDepthOfScreen (screen);
4335
4336 /* Create a pixmap as large as the glyph string. */
4337 pixmap = XCreatePixmap (s->display, s->window,
4338 s->background_width,
4339 s->height, depth);
4340
4341 /* Don't clip in the following because we're working on the
4342 pixmap. */
4343 XSetClipMask (s->display, s->gc, None);
4344
4345 /* Fill the pixmap with the background color/stipple. */
4346 if (s->stippled_p)
4347 {
4348 /* Fill background with a stipple pattern. */
4349 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4350 XFillRectangle (s->display, pixmap, s->gc,
4351 0, 0, s->background_width, s->height);
4352 XSetFillStyle (s->display, s->gc, FillSolid);
4353 }
4354 else
4355 {
4356 XGCValues xgcv;
4357 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4358 &xgcv);
4359 XSetForeground (s->display, s->gc, xgcv.background);
4360 XFillRectangle (s->display, pixmap, s->gc,
4361 0, 0, s->background_width, s->height);
4362 XSetForeground (s->display, s->gc, xgcv.foreground);
4363 }
4364 }
4365 else
06a2c219
GM
4366 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4367
4368 s->background_filled_p = 1;
4369 }
dc43ef94 4370
06a2c219
GM
4371 /* Draw the foreground. */
4372 if (pixmap != None)
4373 {
4374 x_draw_image_foreground_1 (s, pixmap);
4375 x_set_glyph_string_clipping (s);
4376 XCopyArea (s->display, pixmap, s->window, s->gc,
4377 0, 0, s->background_width, s->height, s->x, s->y);
4378 XFreePixmap (s->display, pixmap);
4379 }
4380 else
4381 x_draw_image_foreground (s);
b5210ea7 4382
06a2c219
GM
4383 /* If we must draw a relief around the image, do it. */
4384 if (s->img->relief
4385 || s->hl == DRAW_IMAGE_RAISED
4386 || s->hl == DRAW_IMAGE_SUNKEN)
4387 x_draw_image_relief (s);
4388}
8c1a6a84 4389
990ba854 4390
06a2c219 4391/* Draw stretch glyph string S. */
dc43ef94 4392
06a2c219
GM
4393static void
4394x_draw_stretch_glyph_string (s)
4395 struct glyph_string *s;
4396{
4397 xassert (s->first_glyph->type == STRETCH_GLYPH);
4398 s->stippled_p = s->face->stipple != 0;
990ba854 4399
06a2c219
GM
4400 if (s->hl == DRAW_CURSOR
4401 && !x_stretch_cursor_p)
4402 {
4403 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4404 as wide as the stretch glyph. */
4405 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4406
06a2c219
GM
4407 /* Draw cursor. */
4408 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4409
06a2c219
GM
4410 /* Clear rest using the GC of the original non-cursor face. */
4411 if (width < s->background_width)
4412 {
06a2c219
GM
4413 int x = s->x + width, y = s->y;
4414 int w = s->background_width - width, h = s->height;
4415 XRectangle r;
b7f83f9e 4416 GC gc;
dc43ef94 4417
b7f83f9e
GM
4418 if (s->row->mouse_face_p
4419 && cursor_in_mouse_face_p (s->w))
4420 {
4421 x_set_mouse_face_gc (s);
4422 gc = s->gc;
4423 }
4424 else
4425 gc = s->face->gc;
4426
06a2c219
GM
4427 x_get_glyph_string_clip_rect (s, &r);
4428 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
b7f83f9e 4429
06a2c219
GM
4430 if (s->face->stipple)
4431 {
4432 /* Fill background with a stipple pattern. */
4433 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4434 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4435 XSetFillStyle (s->display, gc, FillSolid);
4436 }
4437 else
4438 {
4439 XGCValues xgcv;
4440 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4441 XSetForeground (s->display, gc, xgcv.background);
4442 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4443 XSetForeground (s->display, gc, xgcv.foreground);
4444 }
4445 }
4446 }
61e9f9f3 4447 else if (!s->background_filled_p)
06a2c219
GM
4448 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4449 s->height);
4450
4451 s->background_filled_p = 1;
4452}
4453
4454
4455/* Draw glyph string S. */
4456
4457static void
4458x_draw_glyph_string (s)
4459 struct glyph_string *s;
4460{
4458cf11
KH
4461 int relief_drawn_p = 0;
4462
06a2c219
GM
4463 /* If S draws into the background of its successor, draw the
4464 background of the successor first so that S can draw into it.
4465 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4466 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4467 {
4468 xassert (s->next->img == NULL);
4469 x_set_glyph_string_gc (s->next);
4470 x_set_glyph_string_clipping (s->next);
4471 x_draw_glyph_string_background (s->next, 1);
4472 }
97210f4e 4473
06a2c219
GM
4474 /* Set up S->gc, set clipping and draw S. */
4475 x_set_glyph_string_gc (s);
06a2c219 4476
4458cf11
KH
4477 /* Draw relief (if any) in advance for char/composition so that the
4478 glyph string can be drawn over it. */
4479 if (!s->for_overlaps_p
4480 && s->face->box != FACE_NO_BOX
4481 && (s->first_glyph->type == CHAR_GLYPH
4482 || s->first_glyph->type == COMPOSITE_GLYPH))
4483
4484 {
e6269cbb 4485 x_set_glyph_string_clipping (s);
4458cf11
KH
4486 x_draw_glyph_string_background (s, 1);
4487 x_draw_glyph_string_box (s);
e6269cbb 4488 x_set_glyph_string_clipping (s);
4458cf11
KH
4489 relief_drawn_p = 1;
4490 }
e6269cbb
GM
4491 else
4492 x_set_glyph_string_clipping (s);
4458cf11 4493
06a2c219
GM
4494 switch (s->first_glyph->type)
4495 {
4496 case IMAGE_GLYPH:
4497 x_draw_image_glyph_string (s);
4498 break;
4499
4500 case STRETCH_GLYPH:
4501 x_draw_stretch_glyph_string (s);
4502 break;
4503
4504 case CHAR_GLYPH:
66ac4b0e
GM
4505 if (s->for_overlaps_p)
4506 s->background_filled_p = 1;
4507 else
4508 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4509 x_draw_glyph_string_foreground (s);
4510 break;
4511
b4192550
KH
4512 case COMPOSITE_GLYPH:
4513 if (s->for_overlaps_p || s->gidx > 0)
4514 s->background_filled_p = 1;
4515 else
4516 x_draw_glyph_string_background (s, 1);
4517 x_draw_composite_glyph_string_foreground (s);
4518 break;
4519
06a2c219
GM
4520 default:
4521 abort ();
4522 }
4523
66ac4b0e 4524 if (!s->for_overlaps_p)
06a2c219 4525 {
66ac4b0e
GM
4526 /* Draw underline. */
4527 if (s->face->underline_p)
4528 {
e24e84cc
GM
4529 unsigned long tem, h;
4530 int y;
06a2c219 4531
e24e84cc 4532 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4533 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4534 h = 1;
e24e84cc
GM
4535
4536 /* Get the underline position. This is the recommended
4537 vertical offset in pixels from the baseline to the top of
4538 the underline. This is a signed value according to the
4539 specs, and its default is
4540
4541 ROUND ((maximum descent) / 2), with
4542 ROUND(x) = floor (x + 0.5) */
4543
a72d5ce5
GM
4544 if (x_use_underline_position_properties
4545 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
4546 y = s->ybase + (long) tem;
4547 else if (s->face->font)
4548 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4549 else
a02f1be0 4550 y = s->y + s->height - h;
06a2c219 4551
66ac4b0e 4552 if (s->face->underline_defaulted_p)
e24e84cc
GM
4553 XFillRectangle (s->display, s->window, s->gc,
4554 s->x, y, s->width, h);
66ac4b0e
GM
4555 else
4556 {
4557 XGCValues xgcv;
4558 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4559 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4560 XFillRectangle (s->display, s->window, s->gc,
4561 s->x, y, s->width, h);
66ac4b0e
GM
4562 XSetForeground (s->display, s->gc, xgcv.foreground);
4563 }
dc6f92b8 4564 }
07e34cb0 4565
66ac4b0e
GM
4566 /* Draw overline. */
4567 if (s->face->overline_p)
06a2c219 4568 {
66ac4b0e
GM
4569 unsigned long dy = 0, h = 1;
4570
4571 if (s->face->overline_color_defaulted_p)
4572 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4573 s->width, h);
4574 else
4575 {
4576 XGCValues xgcv;
4577 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4578 XSetForeground (s->display, s->gc, s->face->overline_color);
4579 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4580 s->width, h);
4581 XSetForeground (s->display, s->gc, xgcv.foreground);
4582 }
06a2c219 4583 }
06a2c219 4584
66ac4b0e
GM
4585 /* Draw strike-through. */
4586 if (s->face->strike_through_p)
06a2c219 4587 {
66ac4b0e
GM
4588 unsigned long h = 1;
4589 unsigned long dy = (s->height - h) / 2;
4590
4591 if (s->face->strike_through_color_defaulted_p)
4592 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4593 s->width, h);
4594 else
4595 {
4596 XGCValues xgcv;
4597 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4598 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4599 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4600 s->width, h);
4601 XSetForeground (s->display, s->gc, xgcv.foreground);
4602 }
06a2c219 4603 }
06a2c219 4604
4458cf11
KH
4605 /* Draw relief if not yet drawn. */
4606 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
4607 x_draw_glyph_string_box (s);
4608 }
06a2c219
GM
4609
4610 /* Reset clipping. */
4611 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4612}
07e34cb0 4613
06a2c219 4614
b4192550
KH
4615static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4616 struct face **, int));
06a2c219 4617
06a2c219 4618
209f68d9
GM
4619/* Fill glyph string S with composition components specified by S->cmp.
4620
b4192550
KH
4621 FACES is an array of faces for all components of this composition.
4622 S->gidx is the index of the first component for S.
4623 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4624 use its physical height for clipping.
06a2c219 4625
b4192550 4626 Value is the index of a component not in S. */
07e34cb0 4627
b4192550
KH
4628static int
4629x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4630 struct glyph_string *s;
b4192550 4631 struct face **faces;
66ac4b0e 4632 int overlaps_p;
07e34cb0 4633{
b4192550 4634 int i;
06a2c219 4635
b4192550 4636 xassert (s);
06a2c219 4637
b4192550 4638 s->for_overlaps_p = overlaps_p;
06a2c219 4639
b4192550
KH
4640 s->face = faces[s->gidx];
4641 s->font = s->face->font;
4642 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4643
b4192550
KH
4644 /* For all glyphs of this composition, starting at the offset
4645 S->gidx, until we reach the end of the definition or encounter a
4646 glyph that requires the different face, add it to S. */
4647 ++s->nchars;
4648 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4649 ++s->nchars;
06a2c219 4650
b4192550
KH
4651 /* All glyph strings for the same composition has the same width,
4652 i.e. the width set for the first component of the composition. */
06a2c219 4653
06a2c219
GM
4654 s->width = s->first_glyph->pixel_width;
4655
4656 /* If the specified font could not be loaded, use the frame's
4657 default font, but record the fact that we couldn't load it in
4658 the glyph string so that we can draw rectangles for the
4659 characters of the glyph string. */
4660 if (s->font == NULL)
4661 {
4662 s->font_not_found_p = 1;
4663 s->font = FRAME_FONT (s->f);
4664 }
4665
4666 /* Adjust base line for subscript/superscript text. */
4667 s->ybase += s->first_glyph->voffset;
4668
4669 xassert (s->face && s->face->gc);
4670
4671 /* This glyph string must always be drawn with 16-bit functions. */
4672 s->two_byte_p = 1;
b4192550
KH
4673
4674 return s->gidx + s->nchars;
06a2c219
GM
4675}
4676
4677
209f68d9
GM
4678/* Fill glyph string S from a sequence of character glyphs.
4679
06a2c219 4680 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4681 first glyph to consider, END is the index of the last + 1.
4682 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4683 use its physical height for clipping.
66ac4b0e
GM
4684
4685 Value is the index of the first glyph not in S. */
06a2c219
GM
4686
4687static int
66ac4b0e 4688x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4689 struct glyph_string *s;
4690 int face_id;
66ac4b0e 4691 int start, end, overlaps_p;
06a2c219
GM
4692{
4693 struct glyph *glyph, *last;
4694 int voffset;
ee569018 4695 int glyph_not_available_p;
06a2c219 4696
06a2c219
GM
4697 xassert (s->f == XFRAME (s->w->frame));
4698 xassert (s->nchars == 0);
4699 xassert (start >= 0 && end > start);
4700
66ac4b0e 4701 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4702 glyph = s->row->glyphs[s->area] + start;
4703 last = s->row->glyphs[s->area] + end;
4704 voffset = glyph->voffset;
4705
ee569018
KH
4706 glyph_not_available_p = glyph->glyph_not_available_p;
4707
06a2c219
GM
4708 while (glyph < last
4709 && glyph->type == CHAR_GLYPH
4710 && glyph->voffset == voffset
ee569018
KH
4711 /* Same face id implies same font, nowadays. */
4712 && glyph->face_id == face_id
4713 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4714 {
ee569018
KH
4715 int two_byte_p;
4716
06a2c219 4717 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4718 s->char2b + s->nchars,
4719 &two_byte_p);
4720 s->two_byte_p = two_byte_p;
06a2c219
GM
4721 ++s->nchars;
4722 xassert (s->nchars <= end - start);
4723 s->width += glyph->pixel_width;
4724 ++glyph;
4725 }
4726
4727 s->font = s->face->font;
4728 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4729
4730 /* If the specified font could not be loaded, use the frame's font,
4731 but record the fact that we couldn't load it in
4732 S->font_not_found_p so that we can draw rectangles for the
4733 characters of the glyph string. */
ee569018 4734 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4735 {
4736 s->font_not_found_p = 1;
4737 s->font = FRAME_FONT (s->f);
4738 }
4739
4740 /* Adjust base line for subscript/superscript text. */
4741 s->ybase += voffset;
66ac4b0e 4742
06a2c219
GM
4743 xassert (s->face && s->face->gc);
4744 return glyph - s->row->glyphs[s->area];
07e34cb0 4745}
dc6f92b8 4746
06a2c219
GM
4747
4748/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4749
dfcf069d 4750static void
06a2c219
GM
4751x_fill_image_glyph_string (s)
4752 struct glyph_string *s;
4753{
4754 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4755 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4756 xassert (s->img);
43d120d8 4757 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4758 s->font = s->face->font;
4759 s->width = s->first_glyph->pixel_width;
4760
4761 /* Adjust base line for subscript/superscript text. */
4762 s->ybase += s->first_glyph->voffset;
4763}
4764
4765
209f68d9 4766/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4767
209f68d9
GM
4768 ROW is the glyph row in which the glyphs are found, AREA is the
4769 area within the row. START is the index of the first glyph to
4770 consider, END is the index of the last + 1.
4771
4772 Value is the index of the first glyph not in S. */
4773
4774static int
4775x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4776 struct glyph_string *s;
209f68d9
GM
4777 struct glyph_row *row;
4778 enum glyph_row_area area;
4779 int start, end;
06a2c219 4780{
209f68d9
GM
4781 struct glyph *glyph, *last;
4782 int voffset, face_id;
4783
06a2c219 4784 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4785
4786 glyph = s->row->glyphs[s->area] + start;
4787 last = s->row->glyphs[s->area] + end;
4788 face_id = glyph->face_id;
4789 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4790 s->font = s->face->font;
209f68d9
GM
4791 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4792 s->width = glyph->pixel_width;
4793 voffset = glyph->voffset;
4794
4795 for (++glyph;
4796 (glyph < last
4797 && glyph->type == STRETCH_GLYPH
4798 && glyph->voffset == voffset
4799 && glyph->face_id == face_id);
4800 ++glyph)
4801 s->width += glyph->pixel_width;
06a2c219
GM
4802
4803 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4804 s->ybase += voffset;
4805
c296fc01
GM
4806 /* The case that face->gc == 0 is handled when drawing the glyph
4807 string by calling PREPARE_FACE_FOR_DISPLAY. */
4808 xassert (s->face);
209f68d9 4809 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4810}
4811
4812
4813/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4814 of XChar2b structures for S; it can't be allocated in
4815 x_init_glyph_string because it must be allocated via `alloca'. W
4816 is the window on which S is drawn. ROW and AREA are the glyph row
4817 and area within the row from which S is constructed. START is the
4818 index of the first glyph structure covered by S. HL is a
4819 face-override for drawing S. */
4820
4821static void
4822x_init_glyph_string (s, char2b, w, row, area, start, hl)
4823 struct glyph_string *s;
4824 XChar2b *char2b;
4825 struct window *w;
4826 struct glyph_row *row;
4827 enum glyph_row_area area;
4828 int start;
4829 enum draw_glyphs_face hl;
4830{
4831 bzero (s, sizeof *s);
4832 s->w = w;
4833 s->f = XFRAME (w->frame);
4834 s->display = FRAME_X_DISPLAY (s->f);
4835 s->window = FRAME_X_WINDOW (s->f);
4836 s->char2b = char2b;
4837 s->hl = hl;
4838 s->row = row;
4839 s->area = area;
4840 s->first_glyph = row->glyphs[area] + start;
4841 s->height = row->height;
4842 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4843
9ea173e8
GM
4844 /* Display the internal border below the tool-bar window. */
4845 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4846 s->y -= s->f->output_data.x->internal_border_width;
4847
4848 s->ybase = s->y + row->ascent;
4849}
4850
4851
4852/* Set background width of glyph string S. START is the index of the
4853 first glyph following S. LAST_X is the right-most x-position + 1
4854 in the drawing area. */
4855
4856static INLINE void
4857x_set_glyph_string_background_width (s, start, last_x)
4858 struct glyph_string *s;
4859 int start;
4860 int last_x;
4861{
4862 /* If the face of this glyph string has to be drawn to the end of
4863 the drawing area, set S->extends_to_end_of_line_p. */
4864 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4865
4866 if (start == s->row->used[s->area]
eb79f5cc 4867 && s->area == TEXT_AREA
7b0870b2
GM
4868 && ((s->hl == DRAW_NORMAL_TEXT
4869 && (s->row->fill_line_p
4870 || s->face->background != default_face->background
4871 || s->face->stipple != default_face->stipple
4872 || s->row->mouse_face_p))
327f42ee
GM
4873 || s->hl == DRAW_MOUSE_FACE
4874 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4875 && s->row->fill_line_p)))
7b0870b2 4876 s->extends_to_end_of_line_p = 1;
06a2c219
GM
4877
4878 /* If S extends its face to the end of the line, set its
4879 background_width to the distance to the right edge of the drawing
4880 area. */
4881 if (s->extends_to_end_of_line_p)
1da3fd71 4882 s->background_width = last_x - s->x + 1;
06a2c219
GM
4883 else
4884 s->background_width = s->width;
4885}
4886
4887
4888/* Add a glyph string for a stretch glyph to the list of strings
4889 between HEAD and TAIL. START is the index of the stretch glyph in
4890 row area AREA of glyph row ROW. END is the index of the last glyph
4891 in that glyph row area. X is the current output position assigned
4892 to the new glyph string constructed. HL overrides that face of the
4893 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4894 is the right-most x-position of the drawing area. */
4895
8abee2e1
DL
4896/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4897 and below -- keep them on one line. */
4898#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4899 do \
4900 { \
4901 s = (struct glyph_string *) alloca (sizeof *s); \
4902 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4903 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4904 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4905 s->x = (X); \
4906 } \
4907 while (0)
4908
4909
4910/* Add a glyph string for an image glyph to the list of strings
4911 between HEAD and TAIL. START is the index of the image glyph in
4912 row area AREA of glyph row ROW. END is the index of the last glyph
4913 in that glyph row area. X is the current output position assigned
4914 to the new glyph string constructed. HL overrides that face of the
4915 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4916 is the right-most x-position of the drawing area. */
4917
8abee2e1 4918#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4919 do \
4920 { \
4921 s = (struct glyph_string *) alloca (sizeof *s); \
4922 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4923 x_fill_image_glyph_string (s); \
4924 x_append_glyph_string (&HEAD, &TAIL, s); \
4925 ++START; \
4926 s->x = (X); \
4927 } \
4928 while (0)
4929
4930
4931/* Add a glyph string for a sequence of character glyphs to the list
4932 of strings between HEAD and TAIL. START is the index of the first
4933 glyph in row area AREA of glyph row ROW that is part of the new
4934 glyph string. END is the index of the last glyph in that glyph row
4935 area. X is the current output position assigned to the new glyph
4936 string constructed. HL overrides that face of the glyph; e.g. it
4937 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4938 right-most x-position of the drawing area. */
4939
8abee2e1 4940#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4941 do \
4942 { \
3e71d8f2 4943 int c, face_id; \
06a2c219
GM
4944 XChar2b *char2b; \
4945 \
43d120d8 4946 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4947 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4948 \
b4192550
KH
4949 s = (struct glyph_string *) alloca (sizeof *s); \
4950 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4951 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4952 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4953 s->x = (X); \
4954 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4955 OVERLAPS_P); \
06a2c219
GM
4956 } \
4957 while (0)
4958
4959
b4192550
KH
4960/* Add a glyph string for a composite sequence to the list of strings
4961 between HEAD and TAIL. START is the index of the first glyph in
4962 row area AREA of glyph row ROW that is part of the new glyph
4963 string. END is the index of the last glyph in that glyph row area.
4964 X is the current output position assigned to the new glyph string
4965 constructed. HL overrides that face of the glyph; e.g. it is
4966 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4967 x-position of the drawing area. */
4968
6c27ec25 4969#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4970 do { \
43d120d8
KH
4971 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4972 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4973 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4974 struct composition *cmp = composition_table[cmp_id]; \
4975 int glyph_len = cmp->glyph_len; \
4976 XChar2b *char2b; \
4977 struct face **faces; \
4978 struct glyph_string *first_s = NULL; \
4979 int n; \
4980 \
ee569018 4981 base_face = base_face->ascii_face; \
b4192550
KH
4982 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4983 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4984 /* At first, fill in `char2b' and `faces'. */ \
4985 for (n = 0; n < glyph_len; n++) \
4986 { \
43d120d8 4987 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4988 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4989 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4990 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
abfb6b46 4991 this_face_id, char2b + n, 1, 1); \
b4192550
KH
4992 } \
4993 \
4994 /* Make glyph_strings for each glyph sequence that is drawable by \
4995 the same face, and append them to HEAD/TAIL. */ \
4996 for (n = 0; n < cmp->glyph_len;) \
4997 { \
4998 s = (struct glyph_string *) alloca (sizeof *s); \
4999 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
5000 x_append_glyph_string (&(HEAD), &(TAIL), s); \
5001 s->cmp = cmp; \
5002 s->gidx = n; \
b4192550
KH
5003 s->x = (X); \
5004 \
5005 if (n == 0) \
5006 first_s = s; \
5007 \
5008 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
5009 } \
5010 \
5011 ++START; \
5012 s = first_s; \
5013 } while (0)
5014
5015
06a2c219
GM
5016/* Build a list of glyph strings between HEAD and TAIL for the glyphs
5017 of AREA of glyph row ROW on window W between indices START and END.
5018 HL overrides the face for drawing glyph strings, e.g. it is
5019 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5020 x-positions of the drawing area.
5021
5022 This is an ugly monster macro construct because we must use alloca
5023 to allocate glyph strings (because x_draw_glyphs can be called
5024 asynchronously). */
5025
8abee2e1 5026#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
5027 do \
5028 { \
5029 HEAD = TAIL = NULL; \
5030 while (START < END) \
5031 { \
5032 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5033 switch (first_glyph->type) \
5034 { \
5035 case CHAR_GLYPH: \
5036 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
5037 TAIL, HL, X, LAST_X, \
5038 OVERLAPS_P); \
06a2c219
GM
5039 break; \
5040 \
b4192550
KH
5041 case COMPOSITE_GLYPH: \
5042 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5043 HEAD, TAIL, HL, X, LAST_X,\
5044 OVERLAPS_P); \
5045 break; \
5046 \
06a2c219
GM
5047 case STRETCH_GLYPH: \
5048 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5049 HEAD, TAIL, HL, X, LAST_X); \
5050 break; \
5051 \
5052 case IMAGE_GLYPH: \
5053 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5054 TAIL, HL, X, LAST_X); \
5055 break; \
5056 \
5057 default: \
5058 abort (); \
5059 } \
5060 \
5061 x_set_glyph_string_background_width (s, START, LAST_X); \
5062 (X) += s->width; \
5063 } \
5064 } \
5065 while (0)
5066
5067
5068/* Draw glyphs between START and END in AREA of ROW on window W,
5069 starting at x-position X. X is relative to AREA in W. HL is a
5070 face-override with the following meaning:
5071
5072 DRAW_NORMAL_TEXT draw normally
5073 DRAW_CURSOR draw in cursor face
5074 DRAW_MOUSE_FACE draw in mouse face.
5075 DRAW_INVERSE_VIDEO draw in mode line face
5076 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5077 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5078
66ac4b0e
GM
5079 If OVERLAPS_P is non-zero, draw only the foreground of characters
5080 and clip to the physical height of ROW.
5081
06a2c219
GM
5082 Value is the x-position reached, relative to AREA of W. */
5083
5084static int
f0a48a01 5085x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
06a2c219
GM
5086 struct window *w;
5087 int x;
5088 struct glyph_row *row;
5089 enum glyph_row_area area;
5090 int start, end;
5091 enum draw_glyphs_face hl;
66ac4b0e 5092 int overlaps_p;
dc6f92b8 5093{
06a2c219
GM
5094 struct glyph_string *head, *tail;
5095 struct glyph_string *s;
5096 int last_x, area_width;
5097 int x_reached;
5098 int i, j;
5099
5100 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 5101 end = min (end, row->used[area]);
a8710abf
GM
5102 start = max (0, start);
5103 start = min (end, start);
06a2c219
GM
5104
5105 /* Translate X to frame coordinates. Set last_x to the right
5106 end of the drawing area. */
5107 if (row->full_width_p)
5108 {
5109 /* X is relative to the left edge of W, without scroll bars
3f332ef3 5110 or fringes. */
06a2c219 5111 struct frame *f = XFRAME (w->frame);
06a2c219 5112 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 5113
06a2c219
GM
5114 x += window_left_x;
5115 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5116 last_x = window_left_x + area_width;
5117
5118 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5119 {
110859fc 5120 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
5121 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5122 last_x += width;
5123 else
5124 x -= width;
5125 }
dc6f92b8 5126
b9432a85 5127 x += FRAME_INTERNAL_BORDER_WIDTH (f);
98fedd97 5128 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
5129 }
5130 else
dc6f92b8 5131 {
06a2c219
GM
5132 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5133 area_width = window_box_width (w, area);
5134 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
5135 }
5136
06a2c219
GM
5137 /* Build a doubly-linked list of glyph_string structures between
5138 head and tail from what we have to draw. Note that the macro
5139 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5140 the reason we use a separate variable `i'. */
5141 i = start;
66ac4b0e
GM
5142 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5143 overlaps_p);
06a2c219
GM
5144 if (tail)
5145 x_reached = tail->x + tail->background_width;
5146 else
5147 x_reached = x;
90e65f07 5148
06a2c219
GM
5149 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5150 the row, redraw some glyphs in front or following the glyph
5151 strings built above. */
a8710abf 5152 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
5153 {
5154 int dummy_x = 0;
5155 struct glyph_string *h, *t;
5156
5157 /* Compute overhangs for all glyph strings. */
5158 for (s = head; s; s = s->next)
5159 x_compute_glyph_string_overhangs (s);
5160
5161 /* Prepend glyph strings for glyphs in front of the first glyph
5162 string that are overwritten because of the first glyph
5163 string's left overhang. The background of all strings
5164 prepended must be drawn because the first glyph string
5165 draws over it. */
5166 i = x_left_overwritten (head);
5167 if (i >= 0)
5168 {
5169 j = i;
5170 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
5171 DRAW_NORMAL_TEXT, dummy_x, last_x,
5172 overlaps_p);
06a2c219 5173 start = i;
06a2c219
GM
5174 x_compute_overhangs_and_x (t, head->x, 1);
5175 x_prepend_glyph_string_lists (&head, &tail, h, t);
5176 }
58769bee 5177
06a2c219
GM
5178 /* Prepend glyph strings for glyphs in front of the first glyph
5179 string that overwrite that glyph string because of their
5180 right overhang. For these strings, only the foreground must
5181 be drawn, because it draws over the glyph string at `head'.
5182 The background must not be drawn because this would overwrite
5183 right overhangs of preceding glyphs for which no glyph
5184 strings exist. */
5185 i = x_left_overwriting (head);
5186 if (i >= 0)
5187 {
5188 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
5189 DRAW_NORMAL_TEXT, dummy_x, last_x,
5190 overlaps_p);
06a2c219
GM
5191 for (s = h; s; s = s->next)
5192 s->background_filled_p = 1;
06a2c219
GM
5193 x_compute_overhangs_and_x (t, head->x, 1);
5194 x_prepend_glyph_string_lists (&head, &tail, h, t);
5195 }
dbcb258a 5196
06a2c219
GM
5197 /* Append glyphs strings for glyphs following the last glyph
5198 string tail that are overwritten by tail. The background of
5199 these strings has to be drawn because tail's foreground draws
5200 over it. */
5201 i = x_right_overwritten (tail);
5202 if (i >= 0)
5203 {
5204 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5205 DRAW_NORMAL_TEXT, x, last_x,
5206 overlaps_p);
06a2c219
GM
5207 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5208 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219 5209 }
dc6f92b8 5210
06a2c219
GM
5211 /* Append glyph strings for glyphs following the last glyph
5212 string tail that overwrite tail. The foreground of such
5213 glyphs has to be drawn because it writes into the background
5214 of tail. The background must not be drawn because it could
5215 paint over the foreground of following glyphs. */
5216 i = x_right_overwriting (tail);
5217 if (i >= 0)
5218 {
5219 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5220 DRAW_NORMAL_TEXT, x, last_x,
5221 overlaps_p);
06a2c219
GM
5222 for (s = h; s; s = s->next)
5223 s->background_filled_p = 1;
5224 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5225 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219
GM
5226 }
5227 }
58769bee 5228
06a2c219
GM
5229 /* Draw all strings. */
5230 for (s = head; s; s = s->next)
5231 x_draw_glyph_string (s);
dc6f92b8 5232
bb313871
GM
5233 if (area == TEXT_AREA
5234 && !row->full_width_p
5235 /* When drawing overlapping rows, only the glyph strings'
5236 foreground is drawn, which doesn't erase a cursor
5237 completely. */
5238 && !overlaps_p)
f0a48a01
GM
5239 {
5240 int x0 = head ? head->x : x;
5241 int x1 = tail ? tail->x + tail->background_width : x;
5242
5243 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5244 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5245
bb313871 5246 if (XFASTINT (w->left_margin_width) != 0)
f0a48a01
GM
5247 {
5248 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5249 x0 -= left_area_width;
5250 x1 -= left_area_width;
5251 }
5252
60626bab
GM
5253 notice_overwritten_cursor (w, area, x0, x1,
5254 row->y, MATRIX_ROW_BOTTOM_Y (row));
f0a48a01
GM
5255 }
5256
06a2c219
GM
5257 /* Value is the x-position up to which drawn, relative to AREA of W.
5258 This doesn't include parts drawn because of overhangs. */
5259 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5260 if (!row->full_width_p)
5261 {
f0a48a01 5262 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
06a2c219
GM
5263 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5264 if (area > TEXT_AREA)
5265 x_reached -= window_box_width (w, TEXT_AREA);
5266 }
a8710abf 5267
06a2c219
GM
5268 return x_reached;
5269}
dc6f92b8 5270
dc6f92b8 5271
66ac4b0e
GM
5272/* Fix the display of area AREA of overlapping row ROW in window W. */
5273
5274static void
5275x_fix_overlapping_area (w, row, area)
5276 struct window *w;
5277 struct glyph_row *row;
5278 enum glyph_row_area area;
5279{
5280 int i, x;
5281
5282 BLOCK_INPUT;
5283
5284 if (area == LEFT_MARGIN_AREA)
5285 x = 0;
5286 else if (area == TEXT_AREA)
5287 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5288 else
5289 x = (window_box_width (w, LEFT_MARGIN_AREA)
5290 + window_box_width (w, TEXT_AREA));
5291
5292 for (i = 0; i < row->used[area];)
5293 {
5294 if (row->glyphs[area][i].overlaps_vertically_p)
5295 {
5296 int start = i, start_x = x;
5297
5298 do
5299 {
5300 x += row->glyphs[area][i].pixel_width;
5301 ++i;
5302 }
5303 while (i < row->used[area]
5304 && row->glyphs[area][i].overlaps_vertically_p);
5305
5306 x_draw_glyphs (w, start_x, row, area, start, i,
f0a48a01 5307 DRAW_NORMAL_TEXT, 1);
66ac4b0e
GM
5308 }
5309 else
5310 {
5311 x += row->glyphs[area][i].pixel_width;
5312 ++i;
5313 }
5314 }
5315
5316 UNBLOCK_INPUT;
5317}
5318
5319
06a2c219
GM
5320/* Output LEN glyphs starting at START at the nominal cursor position.
5321 Advance the nominal cursor over the text. The global variable
5322 updated_window contains the window being updated, updated_row is
5323 the glyph row being updated, and updated_area is the area of that
5324 row being updated. */
dc6f92b8 5325
06a2c219
GM
5326static void
5327x_write_glyphs (start, len)
5328 struct glyph *start;
5329 int len;
5330{
f0a48a01 5331 int x, hpos;
d9cdbb3d 5332
06a2c219 5333 xassert (updated_window && updated_row);
dc6f92b8 5334 BLOCK_INPUT;
06a2c219
GM
5335
5336 /* Write glyphs. */
dc6f92b8 5337
06a2c219
GM
5338 hpos = start - updated_row->glyphs[updated_area];
5339 x = x_draw_glyphs (updated_window, output_cursor.x,
5340 updated_row, updated_area,
5341 hpos, hpos + len,
f0a48a01 5342 DRAW_NORMAL_TEXT, 0);
dc6f92b8 5343
6065f9e2
KS
5344 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
5345 if (updated_area == TEXT_AREA
5346 && updated_window->phys_cursor_on_p
5347 && updated_window->phys_cursor.vpos == output_cursor.vpos
5348 && updated_window->phys_cursor.hpos >= hpos
5349 && updated_window->phys_cursor.hpos < hpos + len)
5350 updated_window->phys_cursor_on_p = 0;
5351
dc6f92b8 5352 UNBLOCK_INPUT;
06a2c219
GM
5353
5354 /* Advance the output cursor. */
5355 output_cursor.hpos += len;
5356 output_cursor.x = x;
dc6f92b8
JB
5357}
5358
0cdd0c9f 5359
06a2c219 5360/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5361
06a2c219
GM
5362static void
5363x_insert_glyphs (start, len)
5364 struct glyph *start;
5365 register int len;
5366{
5367 struct frame *f;
5368 struct window *w;
5369 int line_height, shift_by_width, shifted_region_width;
5370 struct glyph_row *row;
5371 struct glyph *glyph;
2beb36f9 5372 int frame_x, frame_y, hpos;
58769bee 5373
06a2c219 5374 xassert (updated_window && updated_row);
0cdd0c9f 5375 BLOCK_INPUT;
06a2c219
GM
5376 w = updated_window;
5377 f = XFRAME (WINDOW_FRAME (w));
5378
5379 /* Get the height of the line we are in. */
5380 row = updated_row;
5381 line_height = row->height;
5382
5383 /* Get the width of the glyphs to insert. */
5384 shift_by_width = 0;
5385 for (glyph = start; glyph < start + len; ++glyph)
5386 shift_by_width += glyph->pixel_width;
5387
5388 /* Get the width of the region to shift right. */
5389 shifted_region_width = (window_box_width (w, updated_area)
5390 - output_cursor.x
5391 - shift_by_width);
5392
5393 /* Shift right. */
bf0ab8a2 5394 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5395 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5396 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5397 f->output_data.x->normal_gc,
5398 frame_x, frame_y,
5399 shifted_region_width, line_height,
5400 frame_x + shift_by_width, frame_y);
5401
5402 /* Write the glyphs. */
5403 hpos = start - row->glyphs[updated_area];
5404 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
f0a48a01 5405 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
5406
5407 /* Advance the output cursor. */
5408 output_cursor.hpos += len;
5409 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5410 UNBLOCK_INPUT;
5411}
0cdd0c9f 5412
0cdd0c9f 5413
06a2c219
GM
5414/* Delete N glyphs at the nominal cursor position. Not implemented
5415 for X frames. */
c83febd7
RS
5416
5417static void
06a2c219
GM
5418x_delete_glyphs (n)
5419 register int n;
c83febd7 5420{
06a2c219 5421 abort ();
c83febd7
RS
5422}
5423
0cdd0c9f 5424
c5e6e06b
GM
5425/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5426 If they are <= 0, this is probably an error. */
5427
5428void
5429x_clear_area (dpy, window, x, y, width, height, exposures)
5430 Display *dpy;
5431 Window window;
5432 int x, y;
5433 int width, height;
5434 int exposures;
5435{
5436 xassert (width > 0 && height > 0);
5437 XClearArea (dpy, window, x, y, width, height, exposures);
5438}
5439
5440
06a2c219
GM
5441/* Erase the current text line from the nominal cursor position
5442 (inclusive) to pixel column TO_X (exclusive). The idea is that
5443 everything from TO_X onward is already erased.
5444
5445 TO_X is a pixel position relative to updated_area of
5446 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5447
06a2c219
GM
5448static void
5449x_clear_end_of_line (to_x)
5450 int to_x;
5451{
5452 struct frame *f;
5453 struct window *w = updated_window;
5454 int max_x, min_y, max_y;
5455 int from_x, from_y, to_y;
5456
5457 xassert (updated_window && updated_row);
5458 f = XFRAME (w->frame);
5459
5460 if (updated_row->full_width_p)
5461 {
5462 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5463 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5464 && !w->pseudo_window_p)
5465 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5466 }
06a2c219
GM
5467 else
5468 max_x = window_box_width (w, updated_area);
5469 max_y = window_text_bottom_y (w);
dc6f92b8 5470
06a2c219
GM
5471 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5472 of window. For TO_X > 0, truncate to end of drawing area. */
5473 if (to_x == 0)
5474 return;
5475 else if (to_x < 0)
5476 to_x = max_x;
5477 else
5478 to_x = min (to_x, max_x);
dbc4e1c1 5479
06a2c219
GM
5480 to_y = min (max_y, output_cursor.y + updated_row->height);
5481
5482 /* Notice if the cursor will be cleared by this operation. */
5483 if (!updated_row->full_width_p)
60626bab
GM
5484 notice_overwritten_cursor (w, updated_area,
5485 output_cursor.x, -1,
5486 updated_row->y,
5487 MATRIX_ROW_BOTTOM_Y (updated_row));
dbc4e1c1 5488
06a2c219
GM
5489 from_x = output_cursor.x;
5490
5491 /* Translate to frame coordinates. */
5492 if (updated_row->full_width_p)
5493 {
5494 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5495 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5496 }
0cdd0c9f
RS
5497 else
5498 {
06a2c219
GM
5499 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5500 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5501 }
5502
045dee35 5503 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5504 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5505 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5506
5507 /* Prevent inadvertently clearing to end of the X window. */
5508 if (to_x > from_x && to_y > from_y)
5509 {
5510 BLOCK_INPUT;
c5e6e06b
GM
5511 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5512 from_x, from_y, to_x - from_x, to_y - from_y,
5513 False);
06a2c219 5514 UNBLOCK_INPUT;
0cdd0c9f 5515 }
0cdd0c9f 5516}
dbc4e1c1 5517
0cdd0c9f 5518
06a2c219 5519/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5520 frame. Otherwise clear the selected frame. */
06a2c219
GM
5521
5522static void
5523x_clear_frame ()
0cdd0c9f 5524{
06a2c219 5525 struct frame *f;
0cdd0c9f 5526
06a2c219
GM
5527 if (updating_frame)
5528 f = updating_frame;
0cdd0c9f 5529 else
b86bd3dd 5530 f = SELECTED_FRAME ();
58769bee 5531
06a2c219
GM
5532 /* Clearing the frame will erase any cursor, so mark them all as no
5533 longer visible. */
5534 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5535 output_cursor.hpos = output_cursor.vpos = 0;
5536 output_cursor.x = -1;
5537
5538 /* We don't set the output cursor here because there will always
5539 follow an explicit cursor_to. */
5540 BLOCK_INPUT;
5541 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5542
5543 /* We have to clear the scroll bars, too. If we have changed
5544 colors or something like that, then they should be notified. */
5545 x_scroll_bar_clear (f);
0cdd0c9f 5546
06a2c219
GM
5547 XFlush (FRAME_X_DISPLAY (f));
5548 UNBLOCK_INPUT;
dc6f92b8 5549}
06a2c219
GM
5550
5551
dc6f92b8 5552\f
dbc4e1c1
JB
5553/* Invert the middle quarter of the frame for .15 sec. */
5554
06a2c219
GM
5555/* We use the select system call to do the waiting, so we have to make
5556 sure it's available. If it isn't, we just won't do visual bells. */
5557
dbc4e1c1
JB
5558#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5559
06a2c219
GM
5560
5561/* Subtract the `struct timeval' values X and Y, storing the result in
5562 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5563
5564static int
5565timeval_subtract (result, x, y)
5566 struct timeval *result, x, y;
5567{
06a2c219
GM
5568 /* Perform the carry for the later subtraction by updating y. This
5569 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5570 if (x.tv_usec < y.tv_usec)
5571 {
5572 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5573 y.tv_usec -= 1000000 * nsec;
5574 y.tv_sec += nsec;
5575 }
06a2c219 5576
dbc4e1c1
JB
5577 if (x.tv_usec - y.tv_usec > 1000000)
5578 {
5579 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5580 y.tv_usec += 1000000 * nsec;
5581 y.tv_sec -= nsec;
5582 }
5583
06a2c219
GM
5584 /* Compute the time remaining to wait. tv_usec is certainly
5585 positive. */
dbc4e1c1
JB
5586 result->tv_sec = x.tv_sec - y.tv_sec;
5587 result->tv_usec = x.tv_usec - y.tv_usec;
5588
06a2c219
GM
5589 /* Return indication of whether the result should be considered
5590 negative. */
dbc4e1c1
JB
5591 return x.tv_sec < y.tv_sec;
5592}
dc6f92b8 5593
dfcf069d 5594void
f676886a
JB
5595XTflash (f)
5596 struct frame *f;
dc6f92b8 5597{
dbc4e1c1 5598 BLOCK_INPUT;
dc6f92b8 5599
dbc4e1c1
JB
5600 {
5601 GC gc;
dc6f92b8 5602
06a2c219
GM
5603 /* Create a GC that will use the GXxor function to flip foreground
5604 pixels into background pixels. */
dbc4e1c1
JB
5605 {
5606 XGCValues values;
dc6f92b8 5607
dbc4e1c1 5608 values.function = GXxor;
7556890b
RS
5609 values.foreground = (f->output_data.x->foreground_pixel
5610 ^ f->output_data.x->background_pixel);
58769bee 5611
334208b7 5612 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5613 GCFunction | GCForeground, &values);
5614 }
dc6f92b8 5615
dbc4e1c1 5616 {
e84e14c3
RS
5617 /* Get the height not including a menu bar widget. */
5618 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5619 /* Height of each line to flash. */
5620 int flash_height = FRAME_LINE_HEIGHT (f);
5621 /* These will be the left and right margins of the rectangles. */
5622 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5623 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5624
5625 int width;
5626
5627 /* Don't flash the area between a scroll bar and the frame
5628 edge it is next to. */
5629 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5630 {
5631 case vertical_scroll_bar_left:
5632 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5633 break;
5634
5635 case vertical_scroll_bar_right:
5636 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5637 break;
06a2c219
GM
5638
5639 default:
5640 break;
e84e14c3
RS
5641 }
5642
5643 width = flash_right - flash_left;
5644
5645 /* If window is tall, flash top and bottom line. */
5646 if (height > 3 * FRAME_LINE_HEIGHT (f))
5647 {
5648 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5649 flash_left,
5650 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5651 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5652 width, flash_height);
5653 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5654 flash_left,
5655 (height - flash_height
5656 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5657 width, flash_height);
5658 }
5659 else
5660 /* If it is short, flash it all. */
5661 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5662 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5663 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5664
06a2c219 5665 x_flush (f);
dc6f92b8 5666
dbc4e1c1 5667 {
06a2c219 5668 struct timeval wakeup;
dc6f92b8 5669
66c30ea1 5670 EMACS_GET_TIME (wakeup);
dc6f92b8 5671
dbc4e1c1
JB
5672 /* Compute time to wait until, propagating carry from usecs. */
5673 wakeup.tv_usec += 150000;
5674 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5675 wakeup.tv_usec %= 1000000;
5676
101922c3
GM
5677 /* Keep waiting until past the time wakeup or any input gets
5678 available. */
5679 while (! detect_input_pending ())
dbc4e1c1 5680 {
101922c3 5681 struct timeval current;
dbc4e1c1
JB
5682 struct timeval timeout;
5683
101922c3 5684 EMACS_GET_TIME (current);
dbc4e1c1 5685
101922c3
GM
5686 /* Break if result would be negative. */
5687 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5688 break;
5689
101922c3
GM
5690 /* How long `select' should wait. */
5691 timeout.tv_sec = 0;
5692 timeout.tv_usec = 10000;
5693
dbc4e1c1 5694 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5695 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5696 }
5697 }
58769bee 5698
e84e14c3
RS
5699 /* If window is tall, flash top and bottom line. */
5700 if (height > 3 * FRAME_LINE_HEIGHT (f))
5701 {
5702 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5703 flash_left,
5704 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5705 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5706 width, flash_height);
5707 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5708 flash_left,
5709 (height - flash_height
5710 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5711 width, flash_height);
5712 }
5713 else
5714 /* If it is short, flash it all. */
5715 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5716 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5717 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5718
334208b7 5719 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5720 x_flush (f);
dc6f92b8 5721 }
dbc4e1c1
JB
5722 }
5723
5724 UNBLOCK_INPUT;
dc6f92b8
JB
5725}
5726
06a2c219 5727#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5728
5729
dc6f92b8
JB
5730/* Make audible bell. */
5731
dfcf069d 5732void
dc6f92b8
JB
5733XTring_bell ()
5734{
b86bd3dd
GM
5735 struct frame *f = SELECTED_FRAME ();
5736
5737 if (FRAME_X_DISPLAY (f))
5738 {
dbc4e1c1 5739#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5740 if (visible_bell)
5741 XTflash (f);
5742 else
dbc4e1c1 5743#endif
b86bd3dd
GM
5744 {
5745 BLOCK_INPUT;
5746 XBell (FRAME_X_DISPLAY (f), 0);
5747 XFlush (FRAME_X_DISPLAY (f));
5748 UNBLOCK_INPUT;
5749 }
dc6f92b8
JB
5750 }
5751}
06a2c219 5752
dc6f92b8 5753\f
06a2c219
GM
5754/* Specify how many text lines, from the top of the window,
5755 should be affected by insert-lines and delete-lines operations.
5756 This, and those operations, are used only within an update
5757 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5758
dfcf069d 5759static void
06a2c219
GM
5760XTset_terminal_window (n)
5761 register int n;
dc6f92b8 5762{
06a2c219 5763 /* This function intentionally left blank. */
dc6f92b8
JB
5764}
5765
06a2c219
GM
5766
5767\f
5768/***********************************************************************
5769 Line Dance
5770 ***********************************************************************/
5771
5772/* Perform an insert-lines or delete-lines operation, inserting N
5773 lines or deleting -N lines at vertical position VPOS. */
5774
dfcf069d 5775static void
06a2c219
GM
5776x_ins_del_lines (vpos, n)
5777 int vpos, n;
dc6f92b8
JB
5778{
5779 abort ();
5780}
06a2c219
GM
5781
5782
5783/* Scroll part of the display as described by RUN. */
dc6f92b8 5784
dfcf069d 5785static void
06a2c219
GM
5786x_scroll_run (w, run)
5787 struct window *w;
5788 struct run *run;
dc6f92b8 5789{
06a2c219
GM
5790 struct frame *f = XFRAME (w->frame);
5791 int x, y, width, height, from_y, to_y, bottom_y;
5792
5793 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
5794 without mode lines. Include in this box the left and right
5795 fringe of W. */
06a2c219 5796 window_box (w, -1, &x, &y, &width, &height);
3f332ef3
KS
5797 width += FRAME_X_FRINGE_WIDTH (f);
5798 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
06a2c219
GM
5799
5800 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5801 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5802 bottom_y = y + height;
dc6f92b8 5803
06a2c219
GM
5804 if (to_y < from_y)
5805 {
5806 /* Scrolling up. Make sure we don't copy part of the mode
5807 line at the bottom. */
5808 if (from_y + run->height > bottom_y)
5809 height = bottom_y - from_y;
5810 else
5811 height = run->height;
5812 }
dc6f92b8 5813 else
06a2c219
GM
5814 {
5815 /* Scolling down. Make sure we don't copy over the mode line.
5816 at the bottom. */
5817 if (to_y + run->height > bottom_y)
5818 height = bottom_y - to_y;
5819 else
5820 height = run->height;
5821 }
7a13e894 5822
06a2c219
GM
5823 BLOCK_INPUT;
5824
5825 /* Cursor off. Will be switched on again in x_update_window_end. */
5826 updated_window = w;
5827 x_clear_cursor (w);
5828
5829 XCopyArea (FRAME_X_DISPLAY (f),
5830 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5831 f->output_data.x->normal_gc,
5832 x, from_y,
5833 width, height,
5834 x, to_y);
5835
5836 UNBLOCK_INPUT;
5837}
dc6f92b8 5838
dc6f92b8 5839
06a2c219
GM
5840\f
5841/***********************************************************************
5842 Exposure Events
5843 ***********************************************************************/
5844
5845/* Redisplay an exposed area of frame F. X and Y are the upper-left
5846 corner of the exposed rectangle. W and H are width and height of
5847 the exposed area. All are pixel values. W or H zero means redraw
5848 the entire frame. */
dc6f92b8 5849
06a2c219
GM
5850static void
5851expose_frame (f, x, y, w, h)
5852 struct frame *f;
5853 int x, y, w, h;
dc6f92b8 5854{
06a2c219 5855 XRectangle r;
82f053ab 5856 int mouse_face_overwritten_p = 0;
dc6f92b8 5857
06a2c219 5858 TRACE ((stderr, "expose_frame "));
dc6f92b8 5859
06a2c219
GM
5860 /* No need to redraw if frame will be redrawn soon. */
5861 if (FRAME_GARBAGED_P (f))
dc6f92b8 5862 {
06a2c219
GM
5863 TRACE ((stderr, " garbaged\n"));
5864 return;
5865 }
5866
5867 /* If basic faces haven't been realized yet, there is no point in
5868 trying to redraw anything. This can happen when we get an expose
5869 event while Emacs is starting, e.g. by moving another window. */
5870 if (FRAME_FACE_CACHE (f) == NULL
5871 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5872 {
5873 TRACE ((stderr, " no faces\n"));
5874 return;
58769bee 5875 }
06a2c219
GM
5876
5877 if (w == 0 || h == 0)
58769bee 5878 {
06a2c219
GM
5879 r.x = r.y = 0;
5880 r.width = CANON_X_UNIT (f) * f->width;
5881 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5882 }
5883 else
5884 {
06a2c219
GM
5885 r.x = x;
5886 r.y = y;
5887 r.width = w;
5888 r.height = h;
5889 }
5890
5891 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
82f053ab 5892 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
06a2c219 5893
9ea173e8 5894 if (WINDOWP (f->tool_bar_window))
82f053ab
GM
5895 mouse_face_overwritten_p
5896 |= expose_window (XWINDOW (f->tool_bar_window), &r);
06a2c219
GM
5897
5898#ifndef USE_X_TOOLKIT
5899 if (WINDOWP (f->menu_bar_window))
82f053ab
GM
5900 mouse_face_overwritten_p
5901 |= expose_window (XWINDOW (f->menu_bar_window), &r);
06a2c219 5902#endif /* not USE_X_TOOLKIT */
82f053ab
GM
5903
5904 /* Some window managers support a focus-follows-mouse style with
5905 delayed raising of frames. Imagine a partially obscured frame,
5906 and moving the mouse into partially obscured mouse-face on that
5907 frame. The visible part of the mouse-face will be highlighted,
5908 then the WM raises the obscured frame. With at least one WM, KDE
5909 2.1, Emacs is not getting any event for the raising of the frame
5910 (even tried with SubstructureRedirectMask), only Expose events.
5911 These expose events will draw text normally, i.e. not
5912 highlighted. Which means we must redo the highlight here.
5913 Subsume it under ``we love X''. --gerd 2001-08-15 */
5914 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5915 {
5916 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5917 if (f == dpyinfo->mouse_face_mouse_frame)
5918 {
5919 int x = dpyinfo->mouse_face_mouse_x;
5920 int y = dpyinfo->mouse_face_mouse_y;
5921 clear_mouse_face (dpyinfo);
5922 note_mouse_highlight (f, x, y);
5923 }
5924 }
dc6f92b8
JB
5925}
5926
06a2c219
GM
5927
5928/* Redraw (parts) of all windows in the window tree rooted at W that
82f053ab
GM
5929 intersect R. R contains frame pixel coordinates. Value is
5930 non-zero if the exposure overwrites mouse-face. */
06a2c219 5931
82f053ab 5932static int
06a2c219
GM
5933expose_window_tree (w, r)
5934 struct window *w;
5935 XRectangle *r;
dc6f92b8 5936{
82f053ab
GM
5937 struct frame *f = XFRAME (w->frame);
5938 int mouse_face_overwritten_p = 0;
5939
5940 while (w && !FRAME_GARBAGED_P (f))
06a2c219
GM
5941 {
5942 if (!NILP (w->hchild))
82f053ab
GM
5943 mouse_face_overwritten_p
5944 |= expose_window_tree (XWINDOW (w->hchild), r);
06a2c219 5945 else if (!NILP (w->vchild))
82f053ab
GM
5946 mouse_face_overwritten_p
5947 |= expose_window_tree (XWINDOW (w->vchild), r);
5948 else
5949 mouse_face_overwritten_p |= expose_window (w, r);
5950
a02f1be0 5951 w = NILP (w->next) ? NULL : XWINDOW (w->next);
06a2c219 5952 }
82f053ab
GM
5953
5954 return mouse_face_overwritten_p;
06a2c219 5955}
58769bee 5956
dc6f92b8 5957
06a2c219
GM
5958/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5959 which intersects rectangle R. R is in window-relative coordinates. */
5960
5961static void
5962expose_area (w, row, r, area)
5963 struct window *w;
5964 struct glyph_row *row;
5965 XRectangle *r;
5966 enum glyph_row_area area;
5967{
06a2c219
GM
5968 struct glyph *first = row->glyphs[area];
5969 struct glyph *end = row->glyphs[area] + row->used[area];
5970 struct glyph *last;
4bc6dcc7 5971 int first_x, start_x, x;
06a2c219 5972
6fb13182
GM
5973 if (area == TEXT_AREA && row->fill_line_p)
5974 /* If row extends face to end of line write the whole line. */
153f5ed7 5975 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
f0a48a01 5976 DRAW_NORMAL_TEXT, 0);
6fb13182
GM
5977 else
5978 {
4bc6dcc7
GM
5979 /* Set START_X to the window-relative start position for drawing glyphs of
5980 AREA. The first glyph of the text area can be partially visible.
5981 The first glyphs of other areas cannot. */
5982 if (area == LEFT_MARGIN_AREA)
5983 start_x = 0;
5984 else if (area == TEXT_AREA)
5985 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5986 else
5987 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5988 + window_box_width (w, TEXT_AREA));
5989 x = start_x;
5990
6fb13182
GM
5991 /* Find the first glyph that must be redrawn. */
5992 while (first < end
5993 && x + first->pixel_width < r->x)
5994 {
5995 x += first->pixel_width;
5996 ++first;
5997 }
5998
5999 /* Find the last one. */
6000 last = first;
6001 first_x = x;
6002 while (last < end
6003 && x < r->x + r->width)
6004 {
6005 x += last->pixel_width;
6006 ++last;
6007 }
6008
6009 /* Repaint. */
6010 if (last > first)
4bc6dcc7 6011 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
6012 first - row->glyphs[area],
6013 last - row->glyphs[area],
f0a48a01 6014 DRAW_NORMAL_TEXT, 0);
6fb13182 6015 }
06a2c219
GM
6016}
6017
58769bee 6018
06a2c219 6019/* Redraw the parts of the glyph row ROW on window W intersecting
82f053ab
GM
6020 rectangle R. R is in window-relative coordinates. Value is
6021 non-zero if mouse-face was overwritten. */
dc6f92b8 6022
82f053ab 6023static int
06a2c219
GM
6024expose_line (w, row, r)
6025 struct window *w;
6026 struct glyph_row *row;
6027 XRectangle *r;
6028{
6029 xassert (row->enabled_p);
6030
6031 if (row->mode_line_p || w->pseudo_window_p)
6032 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
f0a48a01 6033 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
6034 else
6035 {
6036 if (row->used[LEFT_MARGIN_AREA])
6037 expose_area (w, row, r, LEFT_MARGIN_AREA);
6038 if (row->used[TEXT_AREA])
6039 expose_area (w, row, r, TEXT_AREA);
6040 if (row->used[RIGHT_MARGIN_AREA])
6041 expose_area (w, row, r, RIGHT_MARGIN_AREA);
3f332ef3 6042 x_draw_row_fringe_bitmaps (w, row);
06a2c219 6043 }
82f053ab
GM
6044
6045 return row->mouse_face_p;
06a2c219 6046}
dc6f92b8 6047
58769bee 6048
06a2c219
GM
6049/* Return non-zero if W's cursor intersects rectangle R. */
6050
6051static int
6052x_phys_cursor_in_rect_p (w, r)
6053 struct window *w;
6054 XRectangle *r;
6055{
6056 XRectangle cr, result;
6057 struct glyph *cursor_glyph;
6058
6059 cursor_glyph = get_phys_cursor_glyph (w);
6060 if (cursor_glyph)
6061 {
6062 cr.x = w->phys_cursor.x;
6063 cr.y = w->phys_cursor.y;
6064 cr.width = cursor_glyph->pixel_width;
6065 cr.height = w->phys_cursor_height;
6066 return x_intersect_rectangles (&cr, r, &result);
6067 }
6068 else
6069 return 0;
dc6f92b8 6070}
dc6f92b8 6071
06a2c219 6072
a02f1be0
GM
6073/* Redraw the part of window W intersection rectangle FR. Pixel
6074 coordinates in FR are frame-relative. Call this function with
82f053ab
GM
6075 input blocked. Value is non-zero if the exposure overwrites
6076 mouse-face. */
dc6f92b8 6077
a39202f6 6078static int
a02f1be0 6079expose_window (w, fr)
06a2c219 6080 struct window *w;
a02f1be0 6081 XRectangle *fr;
dc6f92b8 6082{
a02f1be0 6083 struct frame *f = XFRAME (w->frame);
a02f1be0 6084 XRectangle wr, r;
82f053ab 6085 int mouse_face_overwritten_p = 0;
dc6f92b8 6086
80c32bcc
GM
6087 /* If window is not yet fully initialized, do nothing. This can
6088 happen when toolkit scroll bars are used and a window is split.
6089 Reconfiguring the scroll bar will generate an expose for a newly
6090 created window. */
a39202f6 6091 if (w->current_matrix == NULL)
82f053ab 6092 return 0;
a39202f6
GM
6093
6094 /* When we're currently updating the window, display and current
6095 matrix usually don't agree. Arrange for a thorough display
6096 later. */
6097 if (w == updated_window)
6098 {
6099 SET_FRAME_GARBAGED (f);
6100 return 0;
6101 }
80c32bcc 6102
a39202f6 6103 /* Frame-relative pixel rectangle of W. */
a02f1be0
GM
6104 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6105 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6106 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6107 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6108
a39202f6
GM
6109 if (x_intersect_rectangles (fr, &wr, &r))
6110 {
6111 int yb = window_text_bottom_y (w);
6112 struct glyph_row *row;
6113 int cursor_cleared_p;
a02f1be0 6114
a39202f6
GM
6115 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6116 r.x, r.y, r.width, r.height));
dc6f92b8 6117
a39202f6
GM
6118 /* Convert to window coordinates. */
6119 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6120 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
dc6f92b8 6121
a39202f6
GM
6122 /* Turn off the cursor. */
6123 if (!w->pseudo_window_p
6124 && x_phys_cursor_in_rect_p (w, &r))
6125 {
6126 x_clear_cursor (w);
6127 cursor_cleared_p = 1;
6128 }
6129 else
6130 cursor_cleared_p = 0;
06a2c219 6131
a39202f6
GM
6132 /* Find the first row intersecting the rectangle R. */
6133 for (row = w->current_matrix->rows;
6134 row->enabled_p;
6135 ++row)
6136 {
6137 int y0 = row->y;
6138 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6139
6140 if ((y0 >= r.y && y0 < r.y + r.height)
6141 || (y1 > r.y && y1 < r.y + r.height)
6142 || (r.y >= y0 && r.y < y1)
6143 || (r.y + r.height > y0 && r.y + r.height < y1))
82f053ab
GM
6144 {
6145 if (expose_line (w, row, &r))
6146 mouse_face_overwritten_p = 1;
6147 }
6148
a39202f6
GM
6149 if (y1 >= yb)
6150 break;
6151 }
dc6f92b8 6152
a39202f6
GM
6153 /* Display the mode line if there is one. */
6154 if (WINDOW_WANTS_MODELINE_P (w)
6155 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6156 row->enabled_p)
6157 && row->y < r.y + r.height)
82f053ab
GM
6158 {
6159 if (expose_line (w, row, &r))
6160 mouse_face_overwritten_p = 1;
6161 }
a39202f6
GM
6162
6163 if (!w->pseudo_window_p)
6164 {
6165 /* Draw border between windows. */
6166 x_draw_vertical_border (w);
06a2c219 6167
a39202f6
GM
6168 /* Turn the cursor on again. */
6169 if (cursor_cleared_p)
6170 x_update_window_cursor (w, 1);
6171 }
06a2c219 6172 }
82f053ab
GM
6173
6174 return mouse_face_overwritten_p;
06a2c219 6175}
dc6f92b8 6176
dc6f92b8 6177
06a2c219
GM
6178/* Determine the intersection of two rectangles R1 and R2. Return
6179 the intersection in *RESULT. Value is non-zero if RESULT is not
6180 empty. */
6181
6182static int
6183x_intersect_rectangles (r1, r2, result)
6184 XRectangle *r1, *r2, *result;
6185{
6186 XRectangle *left, *right;
6187 XRectangle *upper, *lower;
6188 int intersection_p = 0;
6189
6190 /* Rearrange so that R1 is the left-most rectangle. */
6191 if (r1->x < r2->x)
6192 left = r1, right = r2;
6193 else
6194 left = r2, right = r1;
6195
6196 /* X0 of the intersection is right.x0, if this is inside R1,
6197 otherwise there is no intersection. */
6198 if (right->x <= left->x + left->width)
6199 {
6200 result->x = right->x;
6201
6202 /* The right end of the intersection is the minimum of the
6203 the right ends of left and right. */
6204 result->width = (min (left->x + left->width, right->x + right->width)
6205 - result->x);
6206
6207 /* Same game for Y. */
6208 if (r1->y < r2->y)
6209 upper = r1, lower = r2;
6210 else
6211 upper = r2, lower = r1;
6212
6213 /* The upper end of the intersection is lower.y0, if this is inside
6214 of upper. Otherwise, there is no intersection. */
6215 if (lower->y <= upper->y + upper->height)
dc43ef94 6216 {
06a2c219
GM
6217 result->y = lower->y;
6218
6219 /* The lower end of the intersection is the minimum of the lower
6220 ends of upper and lower. */
6221 result->height = (min (lower->y + lower->height,
6222 upper->y + upper->height)
6223 - result->y);
6224 intersection_p = 1;
dc43ef94 6225 }
dc6f92b8
JB
6226 }
6227
06a2c219 6228 return intersection_p;
dc6f92b8 6229}
06a2c219
GM
6230
6231
6232
6233
dc6f92b8 6234\f
dc6f92b8 6235static void
334208b7
RS
6236frame_highlight (f)
6237 struct frame *f;
dc6f92b8 6238{
b3e1e05c
JB
6239 /* We used to only do this if Vx_no_window_manager was non-nil, but
6240 the ICCCM (section 4.1.6) says that the window's border pixmap
6241 and border pixel are window attributes which are "private to the
6242 client", so we can always change it to whatever we want. */
6243 BLOCK_INPUT;
334208b7 6244 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6245 f->output_data.x->border_pixel);
b3e1e05c 6246 UNBLOCK_INPUT;
5d46f928 6247 x_update_cursor (f, 1);
dc6f92b8
JB
6248}
6249
6250static void
334208b7
RS
6251frame_unhighlight (f)
6252 struct frame *f;
dc6f92b8 6253{
b3e1e05c
JB
6254 /* We used to only do this if Vx_no_window_manager was non-nil, but
6255 the ICCCM (section 4.1.6) says that the window's border pixmap
6256 and border pixel are window attributes which are "private to the
6257 client", so we can always change it to whatever we want. */
6258 BLOCK_INPUT;
334208b7 6259 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6260 f->output_data.x->border_tile);
b3e1e05c 6261 UNBLOCK_INPUT;
5d46f928 6262 x_update_cursor (f, 1);
dc6f92b8 6263}
dc6f92b8 6264
f676886a
JB
6265/* The focus has changed. Update the frames as necessary to reflect
6266 the new situation. Note that we can't change the selected frame
c5acd733 6267 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6268 Each event gets marked with the frame in which it occurred, so the
c5acd733 6269 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6270
6d4238f3 6271static void
0f941935
KH
6272x_new_focus_frame (dpyinfo, frame)
6273 struct x_display_info *dpyinfo;
f676886a 6274 struct frame *frame;
dc6f92b8 6275{
0f941935 6276 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6277
0f941935 6278 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6279 {
58769bee 6280 /* Set this before calling other routines, so that they see
f676886a 6281 the correct value of x_focus_frame. */
0f941935 6282 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6283
6284 if (old_focus && old_focus->auto_lower)
f676886a 6285 x_lower_frame (old_focus);
dc6f92b8
JB
6286
6287#if 0
f676886a 6288 selected_frame = frame;
e0c1aef2
KH
6289 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6290 selected_frame);
f676886a
JB
6291 Fselect_window (selected_frame->selected_window);
6292 choose_minibuf_frame ();
c118dd06 6293#endif /* ! 0 */
dc6f92b8 6294
0f941935
KH
6295 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6296 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6297 else
6298 pending_autoraise_frame = 0;
6d4238f3 6299 }
dc6f92b8 6300
0f941935 6301 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6302}
6303
cab34f50
JD
6304/* Handle FocusIn and FocusOut state changes for FRAME.
6305 If FRAME has focus and there exists more than one frame, puts
6306 an FOCUS_IN_EVENT into BUFP.
6307 Returns number of events inserted into BUFP. */
6308
6309static int
6310x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
6311 int type;
6312 int state;
6313 struct x_display_info *dpyinfo;
6314 struct frame *frame;
6315 struct input_event *bufp;
6316 int numchars;
6317{
6318 int nr_events = 0;
6319
6320 if (type == FocusIn)
6321 {
6322 if (dpyinfo->x_focus_event_frame != frame)
6323 {
6324 x_new_focus_frame (dpyinfo, frame);
6325 dpyinfo->x_focus_event_frame = frame;
6326
6327 /* Don't stop displaying the initial startup message
6328 for a switch-frame event we don't need. */
6329 if (numchars > 0
6330 && GC_NILP (Vterminal_frame)
6331 && GC_CONSP (Vframe_list)
6332 && !GC_NILP (XCDR (Vframe_list)))
6333 {
6334 bufp->kind = FOCUS_IN_EVENT;
6335 XSETFRAME (bufp->frame_or_window, frame);
6336 bufp->arg = Qnil;
6337 ++bufp;
6338 numchars--;
6339 ++nr_events;
6340 }
6341 }
6342
6343 frame->output_data.x->focus_state |= state;
6344
6345#ifdef HAVE_X_I18N
6346 if (FRAME_XIC (frame))
6347 XSetICFocus (FRAME_XIC (frame));
6348#endif
6349 }
6350 else if (type == FocusOut)
6351 {
6352 frame->output_data.x->focus_state &= ~state;
6353
6354 if (dpyinfo->x_focus_event_frame == frame)
6355 {
6356 dpyinfo->x_focus_event_frame = 0;
6357 x_new_focus_frame (dpyinfo, 0);
6358 }
6359
6360#ifdef HAVE_X_I18N
6361 if (FRAME_XIC (frame))
6362 XUnsetICFocus (FRAME_XIC (frame));
6363#endif
6364 }
6365
6366 return nr_events;
6367}
6368
6369/* The focus may have changed. Figure out if it is a real focus change,
6370 by checking both FocusIn/Out and Enter/LeaveNotify events.
6371
6372 Returns number of events inserted into BUFP. */
6373
6374static int
6375x_detect_focus_change (dpyinfo, event, bufp, numchars)
6376 struct x_display_info *dpyinfo;
6377 XEvent *event;
6378 struct input_event *bufp;
6379 int numchars;
6380{
6381 struct frame *frame;
6382 int nr_events = 0;
6383
6384 frame = x_top_window_to_frame (dpyinfo, event->xany.window);
6385 if (! frame) return nr_events;
6386
6387 switch (event->type)
6388 {
6389 case EnterNotify:
6390 case LeaveNotify:
6391 if (event->xcrossing.detail != NotifyInferior
6392 && event->xcrossing.focus
6393 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
6394 nr_events = x_focus_changed ((event->type == EnterNotify
6395 ? FocusIn : FocusOut),
6396 FOCUS_IMPLICIT,
6397 dpyinfo,
6398 frame,
6399 bufp,
6400 numchars);
6401 break;
6402
6403 case FocusIn:
6404 case FocusOut:
6405 nr_events = x_focus_changed (event->type,
6406 (event->xfocus.detail == NotifyPointer
6407 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
6408 dpyinfo,
6409 frame,
6410 bufp,
6411 numchars);
6412 break;
6413 }
6414
6415 return nr_events;
6416}
6417
6418
37c2c98b
RS
6419/* Handle an event saying the mouse has moved out of an Emacs frame. */
6420
6421void
0f941935
KH
6422x_mouse_leave (dpyinfo)
6423 struct x_display_info *dpyinfo;
37c2c98b 6424{
0f941935 6425 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6426}
6d4238f3 6427
f451eb13
JB
6428/* The focus has changed, or we have redirected a frame's focus to
6429 another frame (this happens when a frame uses a surrogate
06a2c219 6430 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6431
6432 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6433 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6434 the appropriate X display info. */
06a2c219 6435
6d4238f3 6436static void
0f941935
KH
6437XTframe_rehighlight (frame)
6438 struct frame *frame;
6d4238f3 6439{
0f941935
KH
6440 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6441}
6d4238f3 6442
0f941935
KH
6443static void
6444x_frame_rehighlight (dpyinfo)
6445 struct x_display_info *dpyinfo;
6446{
6447 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6448
6449 if (dpyinfo->x_focus_frame)
6d4238f3 6450 {
0f941935
KH
6451 dpyinfo->x_highlight_frame
6452 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6453 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6454 : dpyinfo->x_focus_frame);
6455 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6456 {
0f941935
KH
6457 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6458 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6459 }
dc6f92b8 6460 }
6d4238f3 6461 else
0f941935 6462 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6463
0f941935 6464 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6465 {
6466 if (old_highlight)
f676886a 6467 frame_unhighlight (old_highlight);
0f941935
KH
6468 if (dpyinfo->x_highlight_frame)
6469 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6470 }
dc6f92b8 6471}
06a2c219
GM
6472
6473
dc6f92b8 6474\f
06a2c219 6475/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6476
28430d3c
JB
6477/* Initialize mode_switch_bit and modifier_meaning. */
6478static void
334208b7
RS
6479x_find_modifier_meanings (dpyinfo)
6480 struct x_display_info *dpyinfo;
28430d3c 6481{
f689eb05 6482 int min_code, max_code;
28430d3c
JB
6483 KeySym *syms;
6484 int syms_per_code;
6485 XModifierKeymap *mods;
6486
334208b7
RS
6487 dpyinfo->meta_mod_mask = 0;
6488 dpyinfo->shift_lock_mask = 0;
6489 dpyinfo->alt_mod_mask = 0;
6490 dpyinfo->super_mod_mask = 0;
6491 dpyinfo->hyper_mod_mask = 0;
58769bee 6492
9658a521 6493#ifdef HAVE_X11R4
334208b7 6494 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6495#else
4a60f8c5
RS
6496 min_code = dpyinfo->display->min_keycode;
6497 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6498#endif
6499
334208b7 6500 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6501 min_code, max_code - min_code + 1,
6502 &syms_per_code);
334208b7 6503 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6504
58769bee 6505 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6506 Alt keysyms are on. */
28430d3c 6507 {
06a2c219 6508 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6509
6510 for (row = 3; row < 8; row++)
6511 for (col = 0; col < mods->max_keypermod; col++)
6512 {
0299d313
RS
6513 KeyCode code
6514 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6515
af92970c
KH
6516 /* Zeroes are used for filler. Skip them. */
6517 if (code == 0)
6518 continue;
6519
28430d3c
JB
6520 /* Are any of this keycode's keysyms a meta key? */
6521 {
6522 int code_col;
6523
6524 for (code_col = 0; code_col < syms_per_code; code_col++)
6525 {
f689eb05 6526 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6527
f689eb05 6528 switch (sym)
28430d3c 6529 {
f689eb05
JB
6530 case XK_Meta_L:
6531 case XK_Meta_R:
334208b7 6532 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6533 break;
f689eb05
JB
6534
6535 case XK_Alt_L:
6536 case XK_Alt_R:
334208b7 6537 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6538 break;
6539
6540 case XK_Hyper_L:
6541 case XK_Hyper_R:
334208b7 6542 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6543 break;
6544
6545 case XK_Super_L:
6546 case XK_Super_R:
334208b7 6547 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6548 break;
11edeb03
JB
6549
6550 case XK_Shift_Lock:
6551 /* Ignore this if it's not on the lock modifier. */
6552 if ((1 << row) == LockMask)
334208b7 6553 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6554 break;
28430d3c
JB
6555 }
6556 }
6557 }
6558 }
6559 }
6560
f689eb05 6561 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6562 if (! dpyinfo->meta_mod_mask)
a3c44b14 6563 {
334208b7
RS
6564 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6565 dpyinfo->alt_mod_mask = 0;
a3c44b14 6566 }
f689eb05 6567
148c4b70
RS
6568 /* If some keys are both alt and meta,
6569 make them just meta, not alt. */
334208b7 6570 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6571 {
334208b7 6572 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6573 }
58769bee 6574
28430d3c 6575 XFree ((char *) syms);
f689eb05 6576 XFreeModifiermap (mods);
28430d3c
JB
6577}
6578
dfeccd2d
JB
6579/* Convert between the modifier bits X uses and the modifier bits
6580 Emacs uses. */
06a2c219 6581
7c5283e4 6582static unsigned int
334208b7
RS
6583x_x_to_emacs_modifiers (dpyinfo, state)
6584 struct x_display_info *dpyinfo;
dc6f92b8
JB
6585 unsigned int state;
6586{
98659da6
KG
6587 EMACS_UINT mod_meta = meta_modifier;
6588 EMACS_UINT mod_alt = alt_modifier;
6589 EMACS_UINT mod_hyper = hyper_modifier;
6590 EMACS_UINT mod_super = super_modifier;
6591 Lisp_Object tem;
6592
6593 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6594 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6595 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6596 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6597 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6598 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6599 tem = Fget (Vx_super_keysym, Qmodifier_value);
6600 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6601
6602
334208b7 6603 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
6604 | ((state & ControlMask) ? ctrl_modifier : 0)
6605 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6606 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6607 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6608 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
6609}
6610
dfeccd2d 6611static unsigned int
334208b7
RS
6612x_emacs_to_x_modifiers (dpyinfo, state)
6613 struct x_display_info *dpyinfo;
dfeccd2d
JB
6614 unsigned int state;
6615{
98659da6
KG
6616 EMACS_UINT mod_meta = meta_modifier;
6617 EMACS_UINT mod_alt = alt_modifier;
6618 EMACS_UINT mod_hyper = hyper_modifier;
6619 EMACS_UINT mod_super = super_modifier;
6620
6621 Lisp_Object tem;
6622
6623 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6624 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6625 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6626 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6627 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6628 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6629 tem = Fget (Vx_super_keysym, Qmodifier_value);
6630 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6631
6632
6633 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6634 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6635 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6636 | ((state & shift_modifier) ? ShiftMask : 0)
6637 | ((state & ctrl_modifier) ? ControlMask : 0)
6638 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6639}
d047c4eb
KH
6640
6641/* Convert a keysym to its name. */
6642
6643char *
6644x_get_keysym_name (keysym)
6645 KeySym keysym;
6646{
6647 char *value;
6648
6649 BLOCK_INPUT;
6650 value = XKeysymToString (keysym);
6651 UNBLOCK_INPUT;
6652
6653 return value;
6654}
06a2c219
GM
6655
6656
e4571a43
JB
6657\f
6658/* Mouse clicks and mouse movement. Rah. */
e4571a43 6659
06a2c219
GM
6660/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6661 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6662 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6663 not force the value into range. */
69388238 6664
c8dba240 6665void
69388238 6666pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6667 FRAME_PTR f;
69388238 6668 register int pix_x, pix_y;
e4571a43
JB
6669 register int *x, *y;
6670 XRectangle *bounds;
69388238 6671 int noclip;
e4571a43 6672{
06a2c219 6673 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6674 even for negative values. */
6675 if (pix_x < 0)
7556890b 6676 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6677 if (pix_y < 0)
7556890b 6678 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6679
e4571a43
JB
6680 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6681 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6682
6683 if (bounds)
6684 {
7556890b
RS
6685 bounds->width = FONT_WIDTH (f->output_data.x->font);
6686 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6687 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6688 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6689 }
6690
69388238
RS
6691 if (!noclip)
6692 {
6693 if (pix_x < 0)
6694 pix_x = 0;
3cbd2e0b
RS
6695 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6696 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6697
6698 if (pix_y < 0)
6699 pix_y = 0;
6700 else if (pix_y > f->height)
6701 pix_y = f->height;
6702 }
e4571a43
JB
6703
6704 *x = pix_x;
6705 *y = pix_y;
6706}
6707
06a2c219
GM
6708
6709/* Given HPOS/VPOS in the current matrix of W, return corresponding
6710 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6711 can't tell the positions because W's display is not up to date,
6712 return 0. */
6713
6714int
6715glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6716 struct window *w;
6717 int hpos, vpos;
6718 int *frame_x, *frame_y;
2b5c9e71 6719{
06a2c219
GM
6720 int success_p;
6721
6722 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6723 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6724
6725 if (display_completed)
6726 {
6727 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6728 struct glyph *glyph = row->glyphs[TEXT_AREA];
6729 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6730
6731 *frame_y = row->y;
6732 *frame_x = row->x;
6733 while (glyph < end)
6734 {
6735 *frame_x += glyph->pixel_width;
6736 ++glyph;
6737 }
6738
6739 success_p = 1;
6740 }
6741 else
6742 {
6743 *frame_y = *frame_x = 0;
6744 success_p = 0;
6745 }
6746
6747 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6748 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6749 return success_p;
2b5c9e71
RS
6750}
6751
06a2c219 6752
dc6f92b8
JB
6753/* Prepare a mouse-event in *RESULT for placement in the input queue.
6754
6755 If the event is a button press, then note that we have grabbed
f451eb13 6756 the mouse. */
dc6f92b8
JB
6757
6758static Lisp_Object
f451eb13 6759construct_mouse_click (result, event, f)
dc6f92b8
JB
6760 struct input_event *result;
6761 XButtonEvent *event;
f676886a 6762 struct frame *f;
dc6f92b8 6763{
3b8f9651 6764 /* Make the event type NO_EVENT; we'll change that when we decide
dc6f92b8 6765 otherwise. */
3b8f9651 6766 result->kind = MOUSE_CLICK_EVENT;
69388238 6767 result->code = event->button - Button1;
1113d9db 6768 result->timestamp = event->time;
334208b7
RS
6769 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6770 event->state)
f689eb05 6771 | (event->type == ButtonRelease
58769bee 6772 ? up_modifier
f689eb05 6773 : down_modifier));
dc6f92b8 6774
06a2c219
GM
6775 XSETINT (result->x, event->x);
6776 XSETINT (result->y, event->y);
6777 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6778 result->arg = Qnil;
06a2c219 6779 return Qnil;
dc6f92b8 6780}
b849c413 6781
69388238 6782\f
90e65f07
JB
6783/* Function to report a mouse movement to the mainstream Emacs code.
6784 The input handler calls this.
6785
6786 We have received a mouse movement event, which is given in *event.
6787 If the mouse is over a different glyph than it was last time, tell
6788 the mainstream emacs code by setting mouse_moved. If not, ask for
6789 another motion event, so we can check again the next time it moves. */
b8009dd1 6790
06a2c219
GM
6791static XMotionEvent last_mouse_motion_event;
6792static Lisp_Object last_mouse_motion_frame;
6793
90e65f07 6794static void
12ba150f 6795note_mouse_movement (frame, event)
f676886a 6796 FRAME_PTR frame;
90e65f07 6797 XMotionEvent *event;
90e65f07 6798{
e5d77022 6799 last_mouse_movement_time = event->time;
06a2c219
GM
6800 last_mouse_motion_event = *event;
6801 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6802
27f338af
RS
6803 if (event->window != FRAME_X_WINDOW (frame))
6804 {
39d8bb4d 6805 frame->mouse_moved = 1;
27f338af 6806 last_mouse_scroll_bar = Qnil;
27f338af 6807 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6808 }
6809
90e65f07 6810 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6811 else if (event->x < last_mouse_glyph.x
6812 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6813 || event->y < last_mouse_glyph.y
6814 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6815 {
39d8bb4d 6816 frame->mouse_moved = 1;
ab648270 6817 last_mouse_scroll_bar = Qnil;
b8009dd1 6818 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6819 }
6820}
6821
06a2c219
GM
6822\f
6823/************************************************************************
6824 Mouse Face
6825 ************************************************************************/
6826
6827/* Find the glyph under window-relative coordinates X/Y in window W.
6828 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6829 strings. Return in *HPOS and *VPOS the row and column number of
6830 the glyph found. Return in *AREA the glyph area containing X.
6831 Value is a pointer to the glyph found or null if X/Y is not on
6832 text, or we can't tell because W's current matrix is not up to
6833 date. */
6834
6835static struct glyph *
f9db2310 6836x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
6837 struct window *w;
6838 int x, y;
6839 int *hpos, *vpos, *area;
f9db2310 6840 int buffer_only_p;
06a2c219
GM
6841{
6842 struct glyph *glyph, *end;
3e71d8f2 6843 struct glyph_row *row = NULL;
06a2c219
GM
6844 int x0, i, left_area_width;
6845
6846 /* Find row containing Y. Give up if some row is not enabled. */
6847 for (i = 0; i < w->current_matrix->nrows; ++i)
6848 {
6849 row = MATRIX_ROW (w->current_matrix, i);
6850 if (!row->enabled_p)
6851 return NULL;
6852 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6853 break;
6854 }
6855
6856 *vpos = i;
6857 *hpos = 0;
6858
6859 /* Give up if Y is not in the window. */
6860 if (i == w->current_matrix->nrows)
6861 return NULL;
6862
6863 /* Get the glyph area containing X. */
6864 if (w->pseudo_window_p)
6865 {
6866 *area = TEXT_AREA;
6867 x0 = 0;
6868 }
6869 else
6870 {
6871 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6872 if (x < left_area_width)
6873 {
6874 *area = LEFT_MARGIN_AREA;
6875 x0 = 0;
6876 }
6877 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6878 {
6879 *area = TEXT_AREA;
6880 x0 = row->x + left_area_width;
6881 }
6882 else
6883 {
6884 *area = RIGHT_MARGIN_AREA;
6885 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6886 }
6887 }
6888
6889 /* Find glyph containing X. */
6890 glyph = row->glyphs[*area];
6891 end = glyph + row->used[*area];
6892 while (glyph < end)
6893 {
6894 if (x < x0 + glyph->pixel_width)
6895 {
6896 if (w->pseudo_window_p)
6897 break;
f9db2310 6898 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
6899 break;
6900 }
6901
6902 x0 += glyph->pixel_width;
6903 ++glyph;
6904 }
6905
6906 if (glyph == end)
6907 return NULL;
6908
6909 *hpos = glyph - row->glyphs[*area];
6910 return glyph;
6911}
6912
6913
6914/* Convert frame-relative x/y to coordinates relative to window W.
6915 Takes pseudo-windows into account. */
6916
6917static void
6918frame_to_window_pixel_xy (w, x, y)
6919 struct window *w;
6920 int *x, *y;
6921{
6922 if (w->pseudo_window_p)
6923 {
6924 /* A pseudo-window is always full-width, and starts at the
6925 left edge of the frame, plus a frame border. */
6926 struct frame *f = XFRAME (w->frame);
6927 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6928 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6929 }
6930 else
6931 {
6932 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6933 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6934 }
6935}
6936
6937
b436feb6
GM
6938/* Take proper action when mouse has moved to the mode or header line
6939 or marginal area of window W, x-position X and y-position Y. Area
6940 is 1, 3, 6 or 7 for the mode line, header line, left and right
6941 marginal area respectively. X is relative to the start of the text
6942 display area of W, so the width of bitmap areas and scroll bars
6943 must be subtracted to get a position relative to the start of the
6944 mode line. */
6945
06a2c219 6946static void
b436feb6 6947note_mode_line_or_margin_highlight (w, x, y, portion)
06a2c219 6948 struct window *w;
b436feb6 6949 int x, y, portion;
06a2c219
GM
6950{
6951 struct frame *f = XFRAME (w->frame);
6952 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6953 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
2a793c0c 6954 int charpos;
b436feb6 6955 Lisp_Object string, help, map, pos;
06a2c219 6956
b436feb6
GM
6957 if (portion == 1 || portion == 3)
6958 string = mode_line_string (w, x, y, portion == 1, &charpos);
6959 else
6960 string = marginal_area_string (w, x, y, portion, &charpos);
e371a781 6961
b436feb6 6962 if (STRINGP (string))
06a2c219 6963 {
b436feb6 6964 pos = make_number (charpos);
e371a781 6965
b436feb6
GM
6966 /* If we're on a string with `help-echo' text property, arrange
6967 for the help to be displayed. This is done by setting the
6968 global variable help_echo to the help string. */
6969 help = Fget_text_property (pos, Qhelp_echo, string);
6970 if (!NILP (help))
06a2c219 6971 {
b436feb6
GM
6972 help_echo = help;
6973 XSETWINDOW (help_echo_window, w);
6974 help_echo_object = string;
6975 help_echo_pos = charpos;
06a2c219
GM
6976 }
6977
b436feb6
GM
6978 /* Change the mouse pointer according to what is under X/Y. */
6979 map = Fget_text_property (pos, Qlocal_map, string);
6980 if (!KEYMAPP (map))
6981 map = Fget_text_property (pos, Qkeymap, string);
6982 if (KEYMAPP (map))
6983 cursor = f->output_data.x->nontext_cursor;
06a2c219 6984 }
b436feb6 6985
06a2c219
GM
6986 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6987}
6988
6989
6990/* Take proper action when the mouse has moved to position X, Y on
6991 frame F as regards highlighting characters that have mouse-face
6992 properties. Also de-highlighting chars where the mouse was before.
27f338af 6993 X and Y can be negative or out of range. */
b8009dd1
RS
6994
6995static void
6996note_mouse_highlight (f, x, y)
06a2c219 6997 struct frame *f;
c32cdd9a 6998 int x, y;
b8009dd1 6999{
06a2c219
GM
7000 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7001 int portion;
b8009dd1
RS
7002 Lisp_Object window;
7003 struct window *w;
0d487c52
GM
7004 Cursor cursor = None;
7005 struct buffer *b;
b8009dd1 7006
06a2c219
GM
7007 /* When a menu is active, don't highlight because this looks odd. */
7008#ifdef USE_X_TOOLKIT
7009 if (popup_activated ())
7010 return;
7011#endif
7012
66301061 7013 if (NILP (Vmouse_highlight)
04fff9c0 7014 || !f->glyphs_initialized_p)
bf1c0ba1
RS
7015 return;
7016
06a2c219
GM
7017 dpyinfo->mouse_face_mouse_x = x;
7018 dpyinfo->mouse_face_mouse_y = y;
7019 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 7020
06a2c219 7021 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
7022 return;
7023
514e4681
RS
7024 if (gc_in_progress)
7025 {
06a2c219 7026 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
7027 return;
7028 }
7029
b8009dd1 7030 /* Which window is that in? */
06a2c219 7031 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
7032
7033 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
7034 if (! EQ (window, dpyinfo->mouse_face_window))
7035 clear_mouse_face (dpyinfo);
7036
7037 /* Not on a window -> return. */
7038 if (!WINDOWP (window))
7039 return;
7040
7041 /* Convert to window-relative pixel coordinates. */
7042 w = XWINDOW (window);
7043 frame_to_window_pixel_xy (w, &x, &y);
7044
9ea173e8 7045 /* Handle tool-bar window differently since it doesn't display a
06a2c219 7046 buffer. */
9ea173e8 7047 if (EQ (window, f->tool_bar_window))
06a2c219 7048 {
9ea173e8 7049 note_tool_bar_highlight (f, x, y);
06a2c219
GM
7050 return;
7051 }
7052
b436feb6
GM
7053 /* Mouse is on the mode, header line or margin? */
7054 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
06a2c219 7055 {
b436feb6 7056 note_mode_line_or_margin_highlight (w, x, y, portion);
06a2c219
GM
7057 return;
7058 }
0d487c52
GM
7059
7060 if (portion == 2)
7061 cursor = f->output_data.x->horizontal_drag_cursor;
06a2c219 7062 else
0d487c52 7063 cursor = f->output_data.x->text_cursor;
b8009dd1 7064
0cdd0c9f
RS
7065 /* Are we in a window whose display is up to date?
7066 And verify the buffer's text has not changed. */
0d487c52 7067 b = XBUFFER (w->buffer);
06a2c219
GM
7068 if (/* Within text portion of the window. */
7069 portion == 0
0cdd0c9f 7070 && EQ (w->window_end_valid, w->buffer)
0d487c52
GM
7071 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
7072 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
b8009dd1 7073 {
06a2c219
GM
7074 int hpos, vpos, pos, i, area;
7075 struct glyph *glyph;
f9db2310 7076 Lisp_Object object;
0d487c52
GM
7077 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
7078 Lisp_Object *overlay_vec = NULL;
7079 int len, noverlays;
7080 struct buffer *obuf;
7081 int obegv, ozv, same_region;
b8009dd1 7082
06a2c219 7083 /* Find the glyph under X/Y. */
f9db2310 7084 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
7085
7086 /* Clear mouse face if X/Y not over text. */
7087 if (glyph == NULL
7088 || area != TEXT_AREA
7089 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 7090 {
fa262c07
GM
7091 if (clear_mouse_face (dpyinfo))
7092 cursor = None;
7093 goto set_cursor;
06a2c219
GM
7094 }
7095
7096 pos = glyph->charpos;
f9db2310
GM
7097 object = glyph->object;
7098 if (!STRINGP (object) && !BUFFERP (object))
fa262c07 7099 goto set_cursor;
06a2c219 7100
0d487c52
GM
7101 /* If we get an out-of-range value, return now; avoid an error. */
7102 if (BUFFERP (object) && pos > BUF_Z (b))
fa262c07 7103 goto set_cursor;
06a2c219 7104
0d487c52
GM
7105 /* Make the window's buffer temporarily current for
7106 overlays_at and compute_char_face. */
7107 obuf = current_buffer;
7108 current_buffer = b;
7109 obegv = BEGV;
7110 ozv = ZV;
7111 BEGV = BEG;
7112 ZV = Z;
06a2c219 7113
0d487c52
GM
7114 /* Is this char mouse-active or does it have help-echo? */
7115 position = make_number (pos);
f9db2310 7116
0d487c52
GM
7117 if (BUFFERP (object))
7118 {
7119 /* Put all the overlays we want in a vector in overlay_vec.
7120 Store the length in len. If there are more than 10, make
7121 enough space for all, and try again. */
7122 len = 10;
7123 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7124 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7125 if (noverlays > len)
7126 {
7127 len = noverlays;
7128 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7129 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7130 }
f8349001 7131
0d487c52
GM
7132 /* Sort overlays into increasing priority order. */
7133 noverlays = sort_overlays (overlay_vec, noverlays, w);
7134 }
7135 else
7136 noverlays = 0;
7137
7138 same_region = (EQ (window, dpyinfo->mouse_face_window)
7139 && vpos >= dpyinfo->mouse_face_beg_row
7140 && vpos <= dpyinfo->mouse_face_end_row
7141 && (vpos > dpyinfo->mouse_face_beg_row
7142 || hpos >= dpyinfo->mouse_face_beg_col)
7143 && (vpos < dpyinfo->mouse_face_end_row
7144 || hpos < dpyinfo->mouse_face_end_col
7145 || dpyinfo->mouse_face_past_end));
7146
7147 if (same_region)
7148 cursor = None;
7149
7150 /* Check mouse-face highlighting. */
7151 if (! same_region
7152 /* If there exists an overlay with mouse-face overlapping
7153 the one we are currently highlighting, we have to
7154 check if we enter the overlapping overlay, and then
7155 highlight only that. */
7156 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7157 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7158 {
0d487c52
GM
7159 /* Find the highest priority overlay that has a mouse-face
7160 property. */
7161 overlay = Qnil;
7162 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7163 {
7164 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7165 if (!NILP (mouse_face))
7166 overlay = overlay_vec[i];
7167 }
8bd189fb
GM
7168
7169 /* If we're actually highlighting the same overlay as
7170 before, there's no need to do that again. */
7171 if (!NILP (overlay)
7172 && EQ (overlay, dpyinfo->mouse_face_overlay))
7173 goto check_help_echo;
f9db2310 7174
8bd189fb
GM
7175 dpyinfo->mouse_face_overlay = overlay;
7176
7177 /* Clear the display of the old active region, if any. */
7178 if (clear_mouse_face (dpyinfo))
7179 cursor = None;
7180
0d487c52
GM
7181 /* If no overlay applies, get a text property. */
7182 if (NILP (overlay))
7183 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219 7184
0d487c52
GM
7185 /* Handle the overlay case. */
7186 if (!NILP (overlay))
7187 {
7188 /* Find the range of text around this char that
7189 should be active. */
7190 Lisp_Object before, after;
7191 int ignore;
7192
7193 before = Foverlay_start (overlay);
7194 after = Foverlay_end (overlay);
7195 /* Record this as the current active region. */
7196 fast_find_position (w, XFASTINT (before),
7197 &dpyinfo->mouse_face_beg_col,
7198 &dpyinfo->mouse_face_beg_row,
7199 &dpyinfo->mouse_face_beg_x,
7e376260
GM
7200 &dpyinfo->mouse_face_beg_y, Qnil);
7201
0d487c52
GM
7202 dpyinfo->mouse_face_past_end
7203 = !fast_find_position (w, XFASTINT (after),
7204 &dpyinfo->mouse_face_end_col,
7205 &dpyinfo->mouse_face_end_row,
7206 &dpyinfo->mouse_face_end_x,
7e376260 7207 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7208 dpyinfo->mouse_face_window = window;
7209 dpyinfo->mouse_face_face_id
7210 = face_at_buffer_position (w, pos, 0, 0,
7211 &ignore, pos + 1, 1);
7212
7213 /* Display it as active. */
7214 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7215 cursor = None;
0d487c52
GM
7216 }
7217 /* Handle the text property case. */
7218 else if (!NILP (mouse_face) && BUFFERP (object))
7219 {
7220 /* Find the range of text around this char that
7221 should be active. */
7222 Lisp_Object before, after, beginning, end;
7223 int ignore;
7224
7225 beginning = Fmarker_position (w->start);
7226 end = make_number (BUF_Z (XBUFFER (object))
7227 - XFASTINT (w->window_end_pos));
7228 before
7229 = Fprevious_single_property_change (make_number (pos + 1),
7230 Qmouse_face,
7231 object, beginning);
7232 after
7233 = Fnext_single_property_change (position, Qmouse_face,
7234 object, end);
7235
7236 /* Record this as the current active region. */
7237 fast_find_position (w, XFASTINT (before),
7238 &dpyinfo->mouse_face_beg_col,
7239 &dpyinfo->mouse_face_beg_row,
7240 &dpyinfo->mouse_face_beg_x,
7e376260 7241 &dpyinfo->mouse_face_beg_y, Qnil);
0d487c52
GM
7242 dpyinfo->mouse_face_past_end
7243 = !fast_find_position (w, XFASTINT (after),
7244 &dpyinfo->mouse_face_end_col,
7245 &dpyinfo->mouse_face_end_row,
7246 &dpyinfo->mouse_face_end_x,
7e376260 7247 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7248 dpyinfo->mouse_face_window = window;
7249
7250 if (BUFFERP (object))
06a2c219
GM
7251 dpyinfo->mouse_face_face_id
7252 = face_at_buffer_position (w, pos, 0, 0,
7253 &ignore, pos + 1, 1);
7254
0d487c52
GM
7255 /* Display it as active. */
7256 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7257 cursor = None;
0d487c52
GM
7258 }
7259 else if (!NILP (mouse_face) && STRINGP (object))
7260 {
7261 Lisp_Object b, e;
7262 int ignore;
f9db2310 7263
0d487c52
GM
7264 b = Fprevious_single_property_change (make_number (pos + 1),
7265 Qmouse_face,
7266 object, Qnil);
7267 e = Fnext_single_property_change (position, Qmouse_face,
7268 object, Qnil);
7269 if (NILP (b))
7270 b = make_number (0);
7271 if (NILP (e))
d5db4077 7272 e = make_number (SCHARS (object) - 1);
0d487c52 7273 fast_find_string_pos (w, XINT (b), object,
06a2c219
GM
7274 &dpyinfo->mouse_face_beg_col,
7275 &dpyinfo->mouse_face_beg_row,
7276 &dpyinfo->mouse_face_beg_x,
0d487c52
GM
7277 &dpyinfo->mouse_face_beg_y, 0);
7278 fast_find_string_pos (w, XINT (e), object,
7279 &dpyinfo->mouse_face_end_col,
7280 &dpyinfo->mouse_face_end_row,
7281 &dpyinfo->mouse_face_end_x,
7282 &dpyinfo->mouse_face_end_y, 1);
7283 dpyinfo->mouse_face_past_end = 0;
7284 dpyinfo->mouse_face_window = window;
7285 dpyinfo->mouse_face_face_id
7286 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7287 glyph->face_id, 1);
7288 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7289 cursor = None;
0d487c52 7290 }
7e376260
GM
7291 else if (STRINGP (object) && NILP (mouse_face))
7292 {
7293 /* A string which doesn't have mouse-face, but
7294 the text ``under'' it might have. */
7295 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7296 int start = MATRIX_ROW_START_CHARPOS (r);
7297
7298 pos = string_buffer_position (w, object, start);
7299 if (pos > 0)
7300 mouse_face = get_char_property_and_overlay (make_number (pos),
7301 Qmouse_face,
7302 w->buffer,
7303 &overlay);
7304 if (!NILP (mouse_face) && !NILP (overlay))
7305 {
7306 Lisp_Object before = Foverlay_start (overlay);
7307 Lisp_Object after = Foverlay_end (overlay);
91c153e2 7308 int ignore;
7e376260
GM
7309
7310 /* Note that we might not be able to find position
7311 BEFORE in the glyph matrix if the overlay is
7312 entirely covered by a `display' property. In
7313 this case, we overshoot. So let's stop in
7314 the glyph matrix before glyphs for OBJECT. */
7315 fast_find_position (w, XFASTINT (before),
7316 &dpyinfo->mouse_face_beg_col,
7317 &dpyinfo->mouse_face_beg_row,
7318 &dpyinfo->mouse_face_beg_x,
7319 &dpyinfo->mouse_face_beg_y,
7320 object);
7321
7322 dpyinfo->mouse_face_past_end
7323 = !fast_find_position (w, XFASTINT (after),
7324 &dpyinfo->mouse_face_end_col,
7325 &dpyinfo->mouse_face_end_row,
7326 &dpyinfo->mouse_face_end_x,
7327 &dpyinfo->mouse_face_end_y,
7328 Qnil);
7329 dpyinfo->mouse_face_window = window;
7330 dpyinfo->mouse_face_face_id
7331 = face_at_buffer_position (w, pos, 0, 0,
7332 &ignore, pos + 1, 1);
7333
7334 /* Display it as active. */
7335 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7336 cursor = None;
7337 }
7338 }
0d487c52 7339 }
06a2c219 7340
8bd189fb
GM
7341 check_help_echo:
7342
0d487c52
GM
7343 /* Look for a `help-echo' property. */
7344 {
7345 Lisp_Object help, overlay;
06a2c219 7346
0d487c52
GM
7347 /* Check overlays first. */
7348 help = overlay = Qnil;
7349 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7350 {
7351 overlay = overlay_vec[i];
7352 help = Foverlay_get (overlay, Qhelp_echo);
7353 }
be010514 7354
0d487c52
GM
7355 if (!NILP (help))
7356 {
7357 help_echo = help;
7358 help_echo_window = window;
7359 help_echo_object = overlay;
7360 help_echo_pos = pos;
7361 }
7362 else
7363 {
7364 Lisp_Object object = glyph->object;
7365 int charpos = glyph->charpos;
7177d86b 7366
0d487c52
GM
7367 /* Try text properties. */
7368 if (STRINGP (object)
7369 && charpos >= 0
d5db4077 7370 && charpos < SCHARS (object))
0d487c52
GM
7371 {
7372 help = Fget_text_property (make_number (charpos),
7373 Qhelp_echo, object);
7374 if (NILP (help))
7375 {
7376 /* If the string itself doesn't specify a help-echo,
7377 see if the buffer text ``under'' it does. */
7378 struct glyph_row *r
7379 = MATRIX_ROW (w->current_matrix, vpos);
7380 int start = MATRIX_ROW_START_CHARPOS (r);
7381 int pos = string_buffer_position (w, object, start);
7382 if (pos > 0)
7383 {
7e376260 7384 help = Fget_char_property (make_number (pos),
0d487c52
GM
7385 Qhelp_echo, w->buffer);
7386 if (!NILP (help))
7387 {
7388 charpos = pos;
7389 object = w->buffer;
7390 }
7391 }
7392 }
7393 }
7394 else if (BUFFERP (object)
7395 && charpos >= BEGV
7396 && charpos < ZV)
7397 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7398 object);
06a2c219 7399
0d487c52
GM
7400 if (!NILP (help))
7401 {
7402 help_echo = help;
7403 help_echo_window = window;
7404 help_echo_object = object;
7405 help_echo_pos = charpos;
7406 }
7407 }
06a2c219 7408 }
0d487c52
GM
7409
7410 BEGV = obegv;
7411 ZV = ozv;
7412 current_buffer = obuf;
06a2c219 7413 }
0d487c52 7414
fa262c07
GM
7415 set_cursor:
7416
0d487c52
GM
7417 if (cursor != None)
7418 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219
GM
7419}
7420
7421static void
7422redo_mouse_highlight ()
7423{
7424 if (!NILP (last_mouse_motion_frame)
7425 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7426 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7427 last_mouse_motion_event.x,
7428 last_mouse_motion_event.y);
7429}
7430
7431
7432\f
7433/***********************************************************************
9ea173e8 7434 Tool-bars
06a2c219
GM
7435 ***********************************************************************/
7436
9ea173e8
GM
7437static int x_tool_bar_item P_ ((struct frame *, int, int,
7438 struct glyph **, int *, int *, int *));
06a2c219 7439
9ea173e8 7440/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
7441 or -1. */
7442
9ea173e8 7443static int last_tool_bar_item;
06a2c219
GM
7444
7445
9ea173e8
GM
7446/* Get information about the tool-bar item at position X/Y on frame F.
7447 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7448 the current matrix of the tool-bar window of F, or NULL if not
7449 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 7450 item in F->tool_bar_items. Value is
06a2c219 7451
9ea173e8 7452 -1 if X/Y is not on a tool-bar item
06a2c219
GM
7453 0 if X/Y is on the same item that was highlighted before.
7454 1 otherwise. */
7455
7456static int
9ea173e8 7457x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
7458 struct frame *f;
7459 int x, y;
7460 struct glyph **glyph;
7461 int *hpos, *vpos, *prop_idx;
7462{
7463 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7464 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7465 int area;
7466
7467 /* Find the glyph under X/Y. */
f9db2310 7468 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
7469 if (*glyph == NULL)
7470 return -1;
7471
9ea173e8 7472 /* Get the start of this tool-bar item's properties in
8daf1204 7473 f->tool_bar_items. */
9ea173e8 7474 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
7475 return -1;
7476
7477 /* Is mouse on the highlighted item? */
9ea173e8 7478 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
7479 && *vpos >= dpyinfo->mouse_face_beg_row
7480 && *vpos <= dpyinfo->mouse_face_end_row
7481 && (*vpos > dpyinfo->mouse_face_beg_row
7482 || *hpos >= dpyinfo->mouse_face_beg_col)
7483 && (*vpos < dpyinfo->mouse_face_end_row
7484 || *hpos < dpyinfo->mouse_face_end_col
7485 || dpyinfo->mouse_face_past_end))
7486 return 0;
7487
7488 return 1;
7489}
7490
7491
9ea173e8 7492/* Handle mouse button event on the tool-bar of frame F, at
6c4a22e6 7493 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
06a2c219
GM
7494 or ButtonRelase. */
7495
7496static void
9ea173e8 7497x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7498 struct frame *f;
7499 XButtonEvent *button_event;
7500{
7501 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7502 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7503 int hpos, vpos, prop_idx;
7504 struct glyph *glyph;
7505 Lisp_Object enabled_p;
7506 int x = button_event->x;
7507 int y = button_event->y;
7508
9ea173e8 7509 /* If not on the highlighted tool-bar item, return. */
06a2c219 7510 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7511 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7512 return;
7513
7514 /* If item is disabled, do nothing. */
8daf1204 7515 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7516 if (NILP (enabled_p))
7517 return;
7518
7519 if (button_event->type == ButtonPress)
7520 {
7521 /* Show item in pressed state. */
7522 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7523 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7524 last_tool_bar_item = prop_idx;
06a2c219
GM
7525 }
7526 else
7527 {
7528 Lisp_Object key, frame;
7529 struct input_event event;
7530
7531 /* Show item in released state. */
7532 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7533 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7534
8daf1204 7535 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7536
7537 XSETFRAME (frame, f);
9ea173e8 7538 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7539 event.frame_or_window = frame;
7540 event.arg = frame;
06a2c219
GM
7541 kbd_buffer_store_event (&event);
7542
9ea173e8 7543 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7544 event.frame_or_window = frame;
7545 event.arg = key;
06a2c219
GM
7546 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7547 button_event->state);
7548 kbd_buffer_store_event (&event);
9ea173e8 7549 last_tool_bar_item = -1;
06a2c219
GM
7550 }
7551}
7552
7553
9ea173e8
GM
7554/* Possibly highlight a tool-bar item on frame F when mouse moves to
7555 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7556 note_mouse_highlight. */
7557
7558static void
9ea173e8 7559note_tool_bar_highlight (f, x, y)
06a2c219
GM
7560 struct frame *f;
7561 int x, y;
7562{
9ea173e8 7563 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7564 struct window *w = XWINDOW (window);
7565 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7566 int hpos, vpos;
7567 struct glyph *glyph;
7568 struct glyph_row *row;
5c187dee 7569 int i;
06a2c219
GM
7570 Lisp_Object enabled_p;
7571 int prop_idx;
140330de 7572 enum draw_glyphs_face draw;
5c187dee 7573 int mouse_down_p, rc;
06a2c219
GM
7574
7575 /* Function note_mouse_highlight is called with negative x(y
7576 values when mouse moves outside of the frame. */
7577 if (x <= 0 || y <= 0)
7578 {
7579 clear_mouse_face (dpyinfo);
7580 return;
7581 }
7582
9ea173e8 7583 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7584 if (rc < 0)
7585 {
9ea173e8 7586 /* Not on tool-bar item. */
06a2c219
GM
7587 clear_mouse_face (dpyinfo);
7588 return;
7589 }
7590 else if (rc == 0)
06a2c219 7591 goto set_help_echo;
b8009dd1 7592
06a2c219
GM
7593 clear_mouse_face (dpyinfo);
7594
9ea173e8 7595 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7596 mouse_down_p = (dpyinfo->grabbed
7597 && f == last_mouse_frame
7598 && FRAME_LIVE_P (f));
7599 if (mouse_down_p
9ea173e8 7600 && last_tool_bar_item != prop_idx)
06a2c219
GM
7601 return;
7602
7603 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7604 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7605
9ea173e8 7606 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7607 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7608 if (!NILP (enabled_p))
7609 {
7610 /* Compute the x-position of the glyph. In front and past the
7611 image is a space. We include this is the highlighted area. */
7612 row = MATRIX_ROW (w->current_matrix, vpos);
7613 for (i = x = 0; i < hpos; ++i)
7614 x += row->glyphs[TEXT_AREA][i].pixel_width;
7615
7616 /* Record this as the current active region. */
7617 dpyinfo->mouse_face_beg_col = hpos;
7618 dpyinfo->mouse_face_beg_row = vpos;
7619 dpyinfo->mouse_face_beg_x = x;
7620 dpyinfo->mouse_face_beg_y = row->y;
7621 dpyinfo->mouse_face_past_end = 0;
7622
7623 dpyinfo->mouse_face_end_col = hpos + 1;
7624 dpyinfo->mouse_face_end_row = vpos;
7625 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7626 dpyinfo->mouse_face_end_y = row->y;
7627 dpyinfo->mouse_face_window = window;
9ea173e8 7628 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7629
7630 /* Display it as active. */
7631 show_mouse_face (dpyinfo, draw);
7632 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7633 }
06a2c219
GM
7634
7635 set_help_echo:
7636
50590060 7637 /* Set help_echo to a help string to display for this tool-bar item.
06a2c219 7638 XTread_socket does the rest. */
7cea38bc 7639 help_echo_object = help_echo_window = Qnil;
be010514 7640 help_echo_pos = -1;
8daf1204 7641 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7642 if (NILP (help_echo))
8daf1204 7643 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7644}
4d73d038 7645
06a2c219
GM
7646
7647\f
9f8531e5
GM
7648/* Find the glyph matrix position of buffer position CHARPOS in window
7649 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7650 current glyphs must be up to date. If CHARPOS is above window
7651 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7652 of last line in W. In the row containing CHARPOS, stop before glyphs
7e376260 7653 having STOP as object. */
b8009dd1 7654
9f8531e5
GM
7655#if 0 /* This is a version of fast_find_position that's more correct
7656 in the presence of hscrolling, for example. I didn't install
7657 it right away because the problem fixed is minor, it failed
7658 in 20.x as well, and I think it's too risky to install
7659 so near the release of 21.1. 2001-09-25 gerd. */
7660
7661static int
7662fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7663 struct window *w;
7664 int charpos;
7665 int *hpos, *vpos, *x, *y;
7666 Lisp_Object stop;
7667{
7668 struct glyph_row *row, *first;
7669 struct glyph *glyph, *end;
7670 int i, past_end = 0;
7671
7672 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 7673 row = row_containing_pos (w, charpos, first, NULL, 0);
9f8531e5
GM
7674 if (row == NULL)
7675 {
7676 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7677 {
7678 *x = *y = *hpos = *vpos = 0;
7679 return 0;
7680 }
7681 else
7682 {
7683 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7684 past_end = 1;
7685 }
7686 }
7687
7688 *x = row->x;
7689 *y = row->y;
7690 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7691
7692 glyph = row->glyphs[TEXT_AREA];
7693 end = glyph + row->used[TEXT_AREA];
7694
7695 /* Skip over glyphs not having an object at the start of the row.
7696 These are special glyphs like truncation marks on terminal
7697 frames. */
7698 if (row->displays_text_p)
7699 while (glyph < end
7700 && INTEGERP (glyph->object)
7701 && !EQ (stop, glyph->object)
7702 && glyph->charpos < 0)
7703 {
7704 *x += glyph->pixel_width;
7705 ++glyph;
7706 }
7707
7708 while (glyph < end
7709 && !INTEGERP (glyph->object)
7710 && !EQ (stop, glyph->object)
7711 && (!BUFFERP (glyph->object)
7712 || glyph->charpos < charpos))
7713 {
7714 *x += glyph->pixel_width;
7715 ++glyph;
7716 }
7717
7718 *hpos = glyph - row->glyphs[TEXT_AREA];
7719 return past_end;
7720}
7721
7722#else /* not 0 */
7723
b8009dd1 7724static int
7e376260 7725fast_find_position (w, pos, hpos, vpos, x, y, stop)
06a2c219 7726 struct window *w;
b8009dd1 7727 int pos;
06a2c219 7728 int *hpos, *vpos, *x, *y;
7e376260 7729 Lisp_Object stop;
b8009dd1 7730{
b8009dd1 7731 int i;
bf1c0ba1 7732 int lastcol;
06a2c219
GM
7733 int maybe_next_line_p = 0;
7734 int line_start_position;
7735 int yb = window_text_bottom_y (w);
03d1a189
GM
7736 struct glyph_row *row, *best_row;
7737 int row_vpos, best_row_vpos;
06a2c219
GM
7738 int current_x;
7739
03d1a189
GM
7740 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7741 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7742
06a2c219 7743 while (row->y < yb)
b8009dd1 7744 {
06a2c219
GM
7745 if (row->used[TEXT_AREA])
7746 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7747 else
7748 line_start_position = 0;
7749
7750 if (line_start_position > pos)
b8009dd1 7751 break;
77b68646
RS
7752 /* If the position sought is the end of the buffer,
7753 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7754 else if (line_start_position == pos
7755 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7756 {
06a2c219 7757 maybe_next_line_p = 1;
77b68646
RS
7758 break;
7759 }
06a2c219
GM
7760 else if (line_start_position > 0)
7761 {
7762 best_row = row;
7763 best_row_vpos = row_vpos;
7764 }
4b0bb6f3
GM
7765
7766 if (row->y + row->height >= yb)
7767 break;
06a2c219
GM
7768
7769 ++row;
7770 ++row_vpos;
b8009dd1 7771 }
06a2c219
GM
7772
7773 /* Find the right column within BEST_ROW. */
7774 lastcol = 0;
7775 current_x = best_row->x;
7776 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7777 {
06a2c219 7778 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7e376260 7779 int charpos = glyph->charpos;
06a2c219 7780
7e376260 7781 if (BUFFERP (glyph->object))
bf1c0ba1 7782 {
7e376260
GM
7783 if (charpos == pos)
7784 {
7785 *hpos = i;
7786 *vpos = best_row_vpos;
7787 *x = current_x;
7788 *y = best_row->y;
7789 return 1;
7790 }
7791 else if (charpos > pos)
7792 break;
bf1c0ba1 7793 }
7e376260 7794 else if (EQ (glyph->object, stop))
4d73d038 7795 break;
06a2c219 7796
7e376260
GM
7797 if (charpos > 0)
7798 lastcol = i;
06a2c219 7799 current_x += glyph->pixel_width;
bf1c0ba1 7800 }
b8009dd1 7801
77b68646
RS
7802 /* If we're looking for the end of the buffer,
7803 and we didn't find it in the line we scanned,
7804 use the start of the following line. */
06a2c219 7805 if (maybe_next_line_p)
77b68646 7806 {
06a2c219
GM
7807 ++best_row;
7808 ++best_row_vpos;
7809 lastcol = 0;
7810 current_x = best_row->x;
77b68646
RS
7811 }
7812
06a2c219
GM
7813 *vpos = best_row_vpos;
7814 *hpos = lastcol + 1;
7815 *x = current_x;
7816 *y = best_row->y;
b8009dd1
RS
7817 return 0;
7818}
7819
9f8531e5
GM
7820#endif /* not 0 */
7821
06a2c219 7822
269b7745 7823/* Find the position of the glyph for position POS in OBJECT in
7a4bce14
PJ
7824 window W's current matrix, and return in *X, *Y the pixel
7825 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
f9db2310
GM
7826
7827 RIGHT_P non-zero means return the position of the right edge of the
7828 glyph, RIGHT_P zero means return the left edge position.
7829
7830 If no glyph for POS exists in the matrix, return the position of
7831 the glyph with the next smaller position that is in the matrix, if
7832 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7833 exists in the matrix, return the position of the glyph with the
7834 next larger position in OBJECT.
7835
7836 Value is non-zero if a glyph was found. */
7837
7838static int
7839fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7840 struct window *w;
7841 int pos;
7842 Lisp_Object object;
7843 int *hpos, *vpos, *x, *y;
7844 int right_p;
7845{
7846 int yb = window_text_bottom_y (w);
7847 struct glyph_row *r;
7848 struct glyph *best_glyph = NULL;
7849 struct glyph_row *best_row = NULL;
7850 int best_x = 0;
7851
7852 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7853 r->enabled_p && r->y < yb;
7854 ++r)
7855 {
7856 struct glyph *g = r->glyphs[TEXT_AREA];
7857 struct glyph *e = g + r->used[TEXT_AREA];
7858 int gx;
7859
7860 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7861 if (EQ (g->object, object))
7862 {
7863 if (g->charpos == pos)
7864 {
7865 best_glyph = g;
7866 best_x = gx;
7867 best_row = r;
7868 goto found;
7869 }
7870 else if (best_glyph == NULL
7871 || ((abs (g->charpos - pos)
7872 < abs (best_glyph->charpos - pos))
7873 && (right_p
7874 ? g->charpos < pos
7875 : g->charpos > pos)))
7876 {
7877 best_glyph = g;
7878 best_x = gx;
7879 best_row = r;
7880 }
7881 }
7882 }
7883
7884 found:
7885
7886 if (best_glyph)
7887 {
7888 *x = best_x;
7889 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7890
7891 if (right_p)
7892 {
7893 *x += best_glyph->pixel_width;
7894 ++*hpos;
7895 }
7896
7897 *y = best_row->y;
7898 *vpos = best_row - w->current_matrix->rows;
7899 }
7900
7901 return best_glyph != NULL;
7902}
7903
7904
b8009dd1
RS
7905/* Display the active region described by mouse_face_*
7906 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7907
7908static void
06a2c219 7909show_mouse_face (dpyinfo, draw)
7a13e894 7910 struct x_display_info *dpyinfo;
06a2c219 7911 enum draw_glyphs_face draw;
b8009dd1 7912{
7a13e894 7913 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7914 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 7915
b2bbd509
GM
7916 if (/* If window is in the process of being destroyed, don't bother
7917 to do anything. */
7918 w->current_matrix != NULL
66301061
KS
7919 /* Don't update mouse highlight if hidden */
7920 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
b2bbd509
GM
7921 /* Recognize when we are called to operate on rows that don't exist
7922 anymore. This can happen when a window is split. */
7923 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
06a2c219 7924 {
b2bbd509
GM
7925 int phys_cursor_on_p = w->phys_cursor_on_p;
7926 struct glyph_row *row, *first, *last;
06a2c219 7927
b2bbd509
GM
7928 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7929 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7930
7931 for (row = first; row <= last && row->enabled_p; ++row)
06a2c219 7932 {
b2bbd509 7933 int start_hpos, end_hpos, start_x;
06a2c219 7934
b2bbd509
GM
7935 /* For all but the first row, the highlight starts at column 0. */
7936 if (row == first)
7937 {
7938 start_hpos = dpyinfo->mouse_face_beg_col;
7939 start_x = dpyinfo->mouse_face_beg_x;
7940 }
7941 else
7942 {
7943 start_hpos = 0;
7944 start_x = 0;
7945 }
06a2c219 7946
b2bbd509
GM
7947 if (row == last)
7948 end_hpos = dpyinfo->mouse_face_end_col;
7949 else
7950 end_hpos = row->used[TEXT_AREA];
b8009dd1 7951
b2bbd509
GM
7952 if (end_hpos > start_hpos)
7953 {
7954 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7955 start_hpos, end_hpos, draw, 0);
b8009dd1 7956
60626bab
GM
7957 row->mouse_face_p
7958 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
b2bbd509
GM
7959 }
7960 }
2729a2b5 7961
b2bbd509
GM
7962 /* When we've written over the cursor, arrange for it to
7963 be displayed again. */
7964 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7965 x_display_cursor (w, 1,
7966 w->phys_cursor.hpos, w->phys_cursor.vpos,
7967 w->phys_cursor.x, w->phys_cursor.y);
7968 }
fb3b7de5 7969
06a2c219
GM
7970 /* Change the mouse cursor. */
7971 if (draw == DRAW_NORMAL_TEXT)
7972 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7973 f->output_data.x->text_cursor);
7974 else if (draw == DRAW_MOUSE_FACE)
334208b7 7975 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7976 f->output_data.x->cross_cursor);
27ead1d5 7977 else
334208b7 7978 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7979 f->output_data.x->nontext_cursor);
b8009dd1
RS
7980}
7981
7982/* Clear out the mouse-highlighted active region.
fa262c07
GM
7983 Redraw it un-highlighted first. Value is non-zero if mouse
7984 face was actually drawn unhighlighted. */
b8009dd1 7985
fa262c07 7986static int
7a13e894
RS
7987clear_mouse_face (dpyinfo)
7988 struct x_display_info *dpyinfo;
b8009dd1 7989{
fa262c07 7990 int cleared = 0;
06a2c219 7991
fa262c07
GM
7992 if (!NILP (dpyinfo->mouse_face_window))
7993 {
7994 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7995 cleared = 1;
7996 }
b8009dd1 7997
7a13e894
RS
7998 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7999 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8000 dpyinfo->mouse_face_window = Qnil;
1b85dc1c 8001 dpyinfo->mouse_face_overlay = Qnil;
fa262c07 8002 return cleared;
b8009dd1 8003}
e687d06e 8004
71b8321e
GM
8005
8006/* Clear any mouse-face on window W. This function is part of the
8007 redisplay interface, and is called from try_window_id and similar
8008 functions to ensure the mouse-highlight is off. */
8009
8010static void
8011x_clear_mouse_face (w)
8012 struct window *w;
8013{
8014 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
8015 Lisp_Object window;
8016
2e636f9d 8017 BLOCK_INPUT;
71b8321e
GM
8018 XSETWINDOW (window, w);
8019 if (EQ (window, dpyinfo->mouse_face_window))
8020 clear_mouse_face (dpyinfo);
2e636f9d 8021 UNBLOCK_INPUT;
71b8321e
GM
8022}
8023
8024
e687d06e
RS
8025/* Just discard the mouse face information for frame F, if any.
8026 This is used when the size of F is changed. */
8027
dfcf069d 8028void
e687d06e
RS
8029cancel_mouse_face (f)
8030 FRAME_PTR f;
8031{
8032 Lisp_Object window;
8033 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8034
8035 window = dpyinfo->mouse_face_window;
8036 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
8037 {
8038 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8039 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8040 dpyinfo->mouse_face_window = Qnil;
8041 }
8042}
b52b65bd 8043
b8009dd1 8044\f
b52b65bd
GM
8045static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
8046
8047
8048/* Try to determine frame pixel position and size of the glyph under
8049 frame pixel coordinates X/Y on frame F . Return the position and
8050 size in *RECT. Value is non-zero if we could compute these
8051 values. */
8052
8053static int
8054glyph_rect (f, x, y, rect)
8055 struct frame *f;
8056 int x, y;
8057 XRectangle *rect;
8058{
8059 Lisp_Object window;
8060 int part, found = 0;
8061
8062 window = window_from_coordinates (f, x, y, &part, 0);
8063 if (!NILP (window))
8064 {
8065 struct window *w = XWINDOW (window);
8066 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8067 struct glyph_row *end = r + w->current_matrix->nrows - 1;
b52b65bd
GM
8068
8069 frame_to_window_pixel_xy (w, &x, &y);
8070
8071 for (; !found && r < end && r->enabled_p; ++r)
8072 if (r->y >= y)
8073 {
8074 struct glyph *g = r->glyphs[TEXT_AREA];
8075 struct glyph *end = g + r->used[TEXT_AREA];
8076 int gx;
8077
8078 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
8079 if (gx >= x)
8080 {
8081 rect->width = g->pixel_width;
8082 rect->height = r->height;
8083 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
8084 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
8085 found = 1;
8086 }
8087 }
8088 }
8089
8090 return found;
8091}
8092
12ba150f 8093
90e65f07 8094/* Return the current position of the mouse.
b52b65bd 8095 *FP should be a frame which indicates which display to ask about.
90e65f07 8096
b52b65bd
GM
8097 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
8098 and *PART to the frame, window, and scroll bar part that the mouse
8099 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 8100 position on the scroll bar.
12ba150f 8101
b52b65bd
GM
8102 If the mouse movement started elsewhere, set *FP to the frame the
8103 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
8104 the mouse is over.
8105
b52b65bd 8106 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
8107 was at this position.
8108
a135645a
RS
8109 Don't store anything if we don't have a valid set of values to report.
8110
90e65f07 8111 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 8112 movement. */
90e65f07
JB
8113
8114static void
1cf412ec 8115XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 8116 FRAME_PTR *fp;
1cf412ec 8117 int insist;
12ba150f 8118 Lisp_Object *bar_window;
ab648270 8119 enum scroll_bar_part *part;
90e65f07 8120 Lisp_Object *x, *y;
e5d77022 8121 unsigned long *time;
90e65f07 8122{
a135645a
RS
8123 FRAME_PTR f1;
8124
90e65f07
JB
8125 BLOCK_INPUT;
8126
8bcee03e 8127 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 8128 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
8129 else
8130 {
12ba150f
JB
8131 Window root;
8132 int root_x, root_y;
90e65f07 8133
12ba150f
JB
8134 Window dummy_window;
8135 int dummy;
8136
39d8bb4d
KH
8137 Lisp_Object frame, tail;
8138
8139 /* Clear the mouse-moved flag for every frame on this display. */
8140 FOR_EACH_FRAME (tail, frame)
8141 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8142 XFRAME (frame)->mouse_moved = 0;
8143
ab648270 8144 last_mouse_scroll_bar = Qnil;
12ba150f
JB
8145
8146 /* Figure out which root window we're on. */
334208b7
RS
8147 XQueryPointer (FRAME_X_DISPLAY (*fp),
8148 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
8149
8150 /* The root window which contains the pointer. */
8151 &root,
8152
8153 /* Trash which we can't trust if the pointer is on
8154 a different screen. */
8155 &dummy_window,
8156
8157 /* The position on that root window. */
58769bee 8158 &root_x, &root_y,
12ba150f
JB
8159
8160 /* More trash we can't trust. */
8161 &dummy, &dummy,
8162
8163 /* Modifier keys and pointer buttons, about which
8164 we don't care. */
8165 (unsigned int *) &dummy);
8166
8167 /* Now we have a position on the root; find the innermost window
8168 containing the pointer. */
8169 {
8170 Window win, child;
8171 int win_x, win_y;
06a2c219 8172 int parent_x = 0, parent_y = 0;
e99db5a1 8173 int count;
12ba150f
JB
8174
8175 win = root;
69388238 8176
2d7fc7e8
RS
8177 /* XTranslateCoordinates can get errors if the window
8178 structure is changing at the same time this function
8179 is running. So at least we must not crash from them. */
8180
e99db5a1 8181 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 8182
334208b7 8183 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 8184 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 8185 {
69388238
RS
8186 /* If mouse was grabbed on a frame, give coords for that frame
8187 even if the mouse is now outside it. */
334208b7 8188 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 8189
12ba150f 8190 /* From-window, to-window. */
69388238 8191 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
8192
8193 /* From-position, to-position. */
8194 root_x, root_y, &win_x, &win_y,
8195
8196 /* Child of win. */
8197 &child);
69388238
RS
8198 f1 = last_mouse_frame;
8199 }
8200 else
8201 {
8202 while (1)
8203 {
334208b7 8204 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 8205
69388238
RS
8206 /* From-window, to-window. */
8207 root, win,
12ba150f 8208
69388238
RS
8209 /* From-position, to-position. */
8210 root_x, root_y, &win_x, &win_y,
8211
8212 /* Child of win. */
8213 &child);
8214
9af3143a 8215 if (child == None || child == win)
69388238
RS
8216 break;
8217
8218 win = child;
8219 parent_x = win_x;
8220 parent_y = win_y;
8221 }
12ba150f 8222
69388238
RS
8223 /* Now we know that:
8224 win is the innermost window containing the pointer
8225 (XTC says it has no child containing the pointer),
8226 win_x and win_y are the pointer's position in it
8227 (XTC did this the last time through), and
8228 parent_x and parent_y are the pointer's position in win's parent.
8229 (They are what win_x and win_y were when win was child.
8230 If win is the root window, it has no parent, and
8231 parent_{x,y} are invalid, but that's okay, because we'll
8232 never use them in that case.) */
8233
8234 /* Is win one of our frames? */
19126e11 8235 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
8236
8237#ifdef USE_X_TOOLKIT
8238 /* If we end up with the menu bar window, say it's not
8239 on the frame. */
8240 if (f1 != NULL
8241 && f1->output_data.x->menubar_widget
8242 && win == XtWindow (f1->output_data.x->menubar_widget))
8243 f1 = NULL;
8244#endif /* USE_X_TOOLKIT */
69388238 8245 }
58769bee 8246
2d7fc7e8
RS
8247 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8248 f1 = 0;
8249
e99db5a1 8250 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 8251
ab648270 8252 /* If not, is it one of our scroll bars? */
a135645a 8253 if (! f1)
12ba150f 8254 {
ab648270 8255 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
8256
8257 if (bar)
8258 {
a135645a 8259 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8260 win_x = parent_x;
8261 win_y = parent_y;
8262 }
8263 }
90e65f07 8264
8bcee03e 8265 if (f1 == 0 && insist > 0)
b86bd3dd 8266 f1 = SELECTED_FRAME ();
1cf412ec 8267
a135645a 8268 if (f1)
12ba150f 8269 {
06a2c219
GM
8270 /* Ok, we found a frame. Store all the values.
8271 last_mouse_glyph is a rectangle used to reduce the
8272 generation of mouse events. To not miss any motion
8273 events, we must divide the frame into rectangles of the
8274 size of the smallest character that could be displayed
8275 on it, i.e. into the same rectangles that matrices on
8276 the frame are divided into. */
8277
b52b65bd
GM
8278 int width, height, gx, gy;
8279 XRectangle rect;
8280
8281 if (glyph_rect (f1, win_x, win_y, &rect))
8282 last_mouse_glyph = rect;
8283 else
8284 {
8285 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8286 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8287 gx = win_x;
8288 gy = win_y;
06a2c219 8289
b52b65bd
GM
8290 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8291 round down even for negative values. */
8292 if (gx < 0)
8293 gx -= width - 1;
4f00e84d 8294 if (gy < 0)
b52b65bd
GM
8295 gy -= height - 1;
8296 gx = (gx + width - 1) / width * width;
8297 gy = (gy + height - 1) / height * height;
8298
8299 last_mouse_glyph.width = width;
8300 last_mouse_glyph.height = height;
8301 last_mouse_glyph.x = gx;
8302 last_mouse_glyph.y = gy;
8303 }
12ba150f
JB
8304
8305 *bar_window = Qnil;
8306 *part = 0;
334208b7 8307 *fp = f1;
e0c1aef2
KH
8308 XSETINT (*x, win_x);
8309 XSETINT (*y, win_y);
12ba150f
JB
8310 *time = last_mouse_movement_time;
8311 }
8312 }
8313 }
90e65f07
JB
8314
8315 UNBLOCK_INPUT;
8316}
f451eb13 8317
06a2c219 8318
06a2c219 8319#ifdef USE_X_TOOLKIT
bffcfca9
GM
8320
8321/* Atimer callback function for TIMER. Called every 0.1s to process
8322 Xt timeouts, if needed. We must avoid calling XtAppPending as
8323 much as possible because that function does an implicit XFlush
8324 that slows us down. */
8325
8326static void
8327x_process_timeouts (timer)
8328 struct atimer *timer;
8329{
8330 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8331 {
8332 BLOCK_INPUT;
8333 while (XtAppPending (Xt_app_con) & XtIMTimer)
8334 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8335 UNBLOCK_INPUT;
8336 }
06a2c219
GM
8337}
8338
bffcfca9 8339#endif /* USE_X_TOOLKIT */
06a2c219
GM
8340
8341\f
8342/* Scroll bar support. */
8343
8344/* Given an X window ID, find the struct scroll_bar which manages it.
8345 This can be called in GC, so we have to make sure to strip off mark
8346 bits. */
bffcfca9 8347
06a2c219
GM
8348static struct scroll_bar *
8349x_window_to_scroll_bar (window_id)
8350 Window window_id;
8351{
8352 Lisp_Object tail;
8353
8354 for (tail = Vframe_list;
8355 XGCTYPE (tail) == Lisp_Cons;
8e713be6 8356 tail = XCDR (tail))
06a2c219
GM
8357 {
8358 Lisp_Object frame, bar, condemned;
8359
8e713be6 8360 frame = XCAR (tail);
06a2c219
GM
8361 /* All elements of Vframe_list should be frames. */
8362 if (! GC_FRAMEP (frame))
8363 abort ();
8364
8365 /* Scan this frame's scroll bar list for a scroll bar with the
8366 right window ID. */
8367 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8368 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8369 /* This trick allows us to search both the ordinary and
8370 condemned scroll bar lists with one loop. */
8371 ! GC_NILP (bar) || (bar = condemned,
8372 condemned = Qnil,
8373 ! GC_NILP (bar));
8374 bar = XSCROLL_BAR (bar)->next)
8375 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8376 return XSCROLL_BAR (bar);
8377 }
8378
8379 return 0;
8380}
8381
8382
01f67d2c 8383#if defined USE_LUCID
c95fc5f1
GM
8384
8385/* Return the Lucid menu bar WINDOW is part of. Return null
8386 if WINDOW is not part of a menu bar. */
8387
8388static Widget
8389x_window_to_menu_bar (window)
8390 Window window;
8391{
8392 Lisp_Object tail;
8393
8394 for (tail = Vframe_list;
8395 XGCTYPE (tail) == Lisp_Cons;
8396 tail = XCDR (tail))
8397 {
8398 Lisp_Object frame = XCAR (tail);
8399 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8400
8401 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8402 return menu_bar;
8403 }
8404
8405 return NULL;
8406}
8407
01f67d2c 8408#endif /* USE_LUCID */
c95fc5f1 8409
06a2c219
GM
8410\f
8411/************************************************************************
8412 Toolkit scroll bars
8413 ************************************************************************/
8414
eccc05db 8415#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8416
8417static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8418static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8419static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8420 struct scroll_bar *));
8421static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8422 int, int, int));
8423
8424
8425/* Id of action hook installed for scroll bars. */
8426
8427static XtActionHookId action_hook_id;
8428
8429/* Lisp window being scrolled. Set when starting to interact with
8430 a toolkit scroll bar, reset to nil when ending the interaction. */
8431
8432static Lisp_Object window_being_scrolled;
8433
8434/* Last scroll bar part sent in xm_scroll_callback. */
8435
8436static int last_scroll_bar_part;
8437
ec18280f
SM
8438/* Whether this is an Xaw with arrow-scrollbars. This should imply
8439 that movements of 1/20 of the screen size are mapped to up/down. */
8440
8441static Boolean xaw3d_arrow_scroll;
8442
8443/* Whether the drag scrolling maintains the mouse at the top of the
8444 thumb. If not, resizing the thumb needs to be done more carefully
8445 to avoid jerkyness. */
8446
8447static Boolean xaw3d_pick_top;
8448
06a2c219
GM
8449
8450/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 8451 bars are used.. The hook is responsible for detecting when
06a2c219 8452 the user ends an interaction with the scroll bar, and generates
3b8f9651 8453 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
8454
8455static void
8456xt_action_hook (widget, client_data, action_name, event, params,
8457 num_params)
8458 Widget widget;
8459 XtPointer client_data;
8460 String action_name;
8461 XEvent *event;
8462 String *params;
8463 Cardinal *num_params;
8464{
8465 int scroll_bar_p;
8466 char *end_action;
8467
8468#ifdef USE_MOTIF
8469 scroll_bar_p = XmIsScrollBar (widget);
8470 end_action = "Release";
ec18280f 8471#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8472 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8473 end_action = "EndScroll";
ec18280f 8474#endif /* USE_MOTIF */
06a2c219 8475
06a2c219
GM
8476 if (scroll_bar_p
8477 && strcmp (action_name, end_action) == 0
8478 && WINDOWP (window_being_scrolled))
8479 {
8480 struct window *w;
8481
8482 x_send_scroll_bar_event (window_being_scrolled,
8483 scroll_bar_end_scroll, 0, 0);
8484 w = XWINDOW (window_being_scrolled);
8485 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8486 window_being_scrolled = Qnil;
8487 last_scroll_bar_part = -1;
bffcfca9
GM
8488
8489 /* Xt timeouts no longer needed. */
8490 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
8491 }
8492}
8493
07b3d16e
GM
8494/* A vector of windows used for communication between
8495 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8496
8497static struct window **scroll_bar_windows;
8498static int scroll_bar_windows_size;
8499
06a2c219
GM
8500
8501/* Send a client message with message type Xatom_Scrollbar for a
8502 scroll action to the frame of WINDOW. PART is a value identifying
8503 the part of the scroll bar that was clicked on. PORTION is the
8504 amount to scroll of a whole of WHOLE. */
8505
8506static void
8507x_send_scroll_bar_event (window, part, portion, whole)
8508 Lisp_Object window;
8509 int part, portion, whole;
8510{
8511 XEvent event;
8512 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
8513 struct window *w = XWINDOW (window);
8514 struct frame *f = XFRAME (w->frame);
8515 int i;
06a2c219 8516
07b3d16e
GM
8517 BLOCK_INPUT;
8518
06a2c219
GM
8519 /* Construct a ClientMessage event to send to the frame. */
8520 ev->type = ClientMessage;
8521 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8522 ev->display = FRAME_X_DISPLAY (f);
8523 ev->window = FRAME_X_WINDOW (f);
8524 ev->format = 32;
07b3d16e
GM
8525
8526 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8527 not enough to store a pointer or Lisp_Object on a 64 bit system.
8528 So, store the window in scroll_bar_windows and pass the index
8529 into that array in the event. */
8530 for (i = 0; i < scroll_bar_windows_size; ++i)
8531 if (scroll_bar_windows[i] == NULL)
8532 break;
8533
8534 if (i == scroll_bar_windows_size)
8535 {
8536 int new_size = max (10, 2 * scroll_bar_windows_size);
8537 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8538 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8539
8540 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8541 nbytes);
8542 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8543 scroll_bar_windows_size = new_size;
8544 }
8545
8546 scroll_bar_windows[i] = w;
8547 ev->data.l[0] = (long) i;
06a2c219
GM
8548 ev->data.l[1] = (long) part;
8549 ev->data.l[2] = (long) 0;
8550 ev->data.l[3] = (long) portion;
8551 ev->data.l[4] = (long) whole;
8552
bffcfca9
GM
8553 /* Make Xt timeouts work while the scroll bar is active. */
8554 toolkit_scroll_bar_interaction = 1;
8555
06a2c219
GM
8556 /* Setting the event mask to zero means that the message will
8557 be sent to the client that created the window, and if that
8558 window no longer exists, no event will be sent. */
06a2c219
GM
8559 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8560 UNBLOCK_INPUT;
8561}
8562
8563
8564/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8565 in *IEVENT. */
8566
8567static void
8568x_scroll_bar_to_input_event (event, ievent)
8569 XEvent *event;
8570 struct input_event *ievent;
8571{
8572 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
8573 Lisp_Object window;
8574 struct frame *f;
07b3d16e
GM
8575 struct window *w;
8576
8577 w = scroll_bar_windows[ev->data.l[0]];
8578 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 8579
07b3d16e
GM
8580 XSETWINDOW (window, w);
8581 f = XFRAME (w->frame);
06a2c219 8582
3b8f9651 8583 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 8584 ievent->frame_or_window = window;
0f8aabe9 8585 ievent->arg = Qnil;
06a2c219
GM
8586 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8587 ievent->part = ev->data.l[1];
8588 ievent->code = ev->data.l[2];
8589 ievent->x = make_number ((int) ev->data.l[3]);
8590 ievent->y = make_number ((int) ev->data.l[4]);
8591 ievent->modifiers = 0;
8592}
8593
8594
8595#ifdef USE_MOTIF
8596
8597/* Minimum and maximum values used for Motif scroll bars. */
8598
8599#define XM_SB_MIN 1
8600#define XM_SB_MAX 10000000
8601#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8602
8603
8604/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8605 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4696802b 8606 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
06a2c219
GM
8607
8608static void
8609xm_scroll_callback (widget, client_data, call_data)
8610 Widget widget;
8611 XtPointer client_data, call_data;
8612{
8613 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8614 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
8615 int part = -1, whole = 0, portion = 0;
8616
8617 switch (cs->reason)
8618 {
8619 case XmCR_DECREMENT:
8620 bar->dragging = Qnil;
8621 part = scroll_bar_up_arrow;
8622 break;
8623
8624 case XmCR_INCREMENT:
8625 bar->dragging = Qnil;
8626 part = scroll_bar_down_arrow;
8627 break;
8628
8629 case XmCR_PAGE_DECREMENT:
8630 bar->dragging = Qnil;
8631 part = scroll_bar_above_handle;
8632 break;
8633
8634 case XmCR_PAGE_INCREMENT:
8635 bar->dragging = Qnil;
8636 part = scroll_bar_below_handle;
8637 break;
8638
8639 case XmCR_TO_TOP:
8640 bar->dragging = Qnil;
8641 part = scroll_bar_to_top;
8642 break;
8643
8644 case XmCR_TO_BOTTOM:
8645 bar->dragging = Qnil;
8646 part = scroll_bar_to_bottom;
8647 break;
8648
8649 case XmCR_DRAG:
8650 {
8651 int slider_size;
8652 int dragging_down_p = (INTEGERP (bar->dragging)
8653 && XINT (bar->dragging) <= cs->value);
8654
8655 /* Get the slider size. */
8656 BLOCK_INPUT;
8657 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8658 UNBLOCK_INPUT;
8659
6f2a7a68
SM
8660 whole = XM_SB_RANGE - slider_size;
8661 portion = min (cs->value - XM_SB_MIN, whole);
8662 part = scroll_bar_handle;
8663 bar->dragging = make_number (cs->value);
06a2c219
GM
8664 }
8665 break;
8666
8667 case XmCR_VALUE_CHANGED:
8668 break;
8669 };
8670
8671 if (part >= 0)
8672 {
8673 window_being_scrolled = bar->window;
8674 last_scroll_bar_part = part;
8675 x_send_scroll_bar_event (bar->window, part, portion, whole);
8676 }
8677}
8678
8679
ec18280f 8680#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
8681
8682
ec18280f 8683/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8684 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8685 scroll bar struct. CALL_DATA is a pointer to a float saying where
8686 the thumb is. */
8687
8688static void
ec18280f 8689xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8690 Widget widget;
8691 XtPointer client_data, call_data;
8692{
8693 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8694 float top = *(float *) call_data;
8695 float shown;
ec18280f
SM
8696 int whole, portion, height;
8697 int part;
06a2c219
GM
8698
8699 /* Get the size of the thumb, a value between 0 and 1. */
8700 BLOCK_INPUT;
ec18280f 8701 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8702 UNBLOCK_INPUT;
8703
8704 whole = 10000000;
8705 portion = shown < 1 ? top * whole : 0;
06a2c219 8706
ec18280f
SM
8707 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8708 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8709 the bottom, so we force the scrolling whenever we see that we're
8710 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8711 we try to ensure that we always stay two pixels away from the
8712 bottom). */
06a2c219
GM
8713 part = scroll_bar_down_arrow;
8714 else
8715 part = scroll_bar_handle;
8716
8717 window_being_scrolled = bar->window;
8718 bar->dragging = make_number (portion);
8719 last_scroll_bar_part = part;
8720 x_send_scroll_bar_event (bar->window, part, portion, whole);
8721}
8722
8723
ec18280f
SM
8724/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8725 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8726 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8727 the scroll bar. CALL_DATA is an integer specifying the action that
8728 has taken place. It's magnitude is in the range 0..height of the
8729 scroll bar. Negative values mean scroll towards buffer start.
8730 Values < height of scroll bar mean line-wise movement. */
8731
8732static void
ec18280f 8733xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8734 Widget widget;
8735 XtPointer client_data, call_data;
8736{
8737 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8738 int position = (int) call_data;
8739 Dimension height;
8740 int part;
8741
8742 /* Get the height of the scroll bar. */
8743 BLOCK_INPUT;
8744 XtVaGetValues (widget, XtNheight, &height, NULL);
8745 UNBLOCK_INPUT;
8746
ec18280f
SM
8747 if (abs (position) >= height)
8748 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8749
8750 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8751 it maps line-movement to call_data = max(5, height/20). */
8752 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8753 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8754 else
ec18280f 8755 part = scroll_bar_move_ratio;
06a2c219
GM
8756
8757 window_being_scrolled = bar->window;
8758 bar->dragging = Qnil;
8759 last_scroll_bar_part = part;
ec18280f 8760 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8761}
8762
8763
8764#endif /* not USE_MOTIF */
8765
8766
8767/* Create the widget for scroll bar BAR on frame F. Record the widget
8768 and X window of the scroll bar in BAR. */
8769
8770static void
8771x_create_toolkit_scroll_bar (f, bar)
8772 struct frame *f;
8773 struct scroll_bar *bar;
8774{
8775 Window xwindow;
8776 Widget widget;
8777 Arg av[20];
8778 int ac = 0;
8779 char *scroll_bar_name = "verticalScrollBar";
8780 unsigned long pixel;
8781
8782 BLOCK_INPUT;
8783
8784#ifdef USE_MOTIF
06a2c219
GM
8785 /* Set resources. Create the widget. */
8786 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8787 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8788 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8789 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8790 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8791 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8792 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8793
8794 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8795 if (pixel != -1)
8796 {
8797 XtSetArg (av[ac], XmNforeground, pixel);
8798 ++ac;
8799 }
8800
8801 pixel = f->output_data.x->scroll_bar_background_pixel;
8802 if (pixel != -1)
8803 {
8804 XtSetArg (av[ac], XmNbackground, pixel);
8805 ++ac;
8806 }
8807
8808 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8809 scroll_bar_name, av, ac);
8810
8811 /* Add one callback for everything that can happen. */
8812 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8813 (XtPointer) bar);
8814 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8815 (XtPointer) bar);
8816 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8817 (XtPointer) bar);
8818 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8819 (XtPointer) bar);
8820 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8821 (XtPointer) bar);
8822 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8823 (XtPointer) bar);
8824 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8825 (XtPointer) bar);
8826
8827 /* Realize the widget. Only after that is the X window created. */
8828 XtRealizeWidget (widget);
8829
8830 /* Set the cursor to an arrow. I didn't find a resource to do that.
8831 And I'm wondering why it hasn't an arrow cursor by default. */
8832 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8833 f->output_data.x->nontext_cursor);
8834
ec18280f 8835#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8836
8837 /* Set resources. Create the widget. The background of the
8838 Xaw3d scroll bar widget is a little bit light for my taste.
8839 We don't alter it here to let users change it according
8840 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8841 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8842 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8843 /* For smoother scrolling with Xaw3d -sm */
8844 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
06a2c219
GM
8845
8846 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8847 if (pixel != -1)
8848 {
8849 XtSetArg (av[ac], XtNforeground, pixel);
8850 ++ac;
8851 }
8852
8853 pixel = f->output_data.x->scroll_bar_background_pixel;
8854 if (pixel != -1)
8855 {
8856 XtSetArg (av[ac], XtNbackground, pixel);
8857 ++ac;
8858 }
7c1bef7a
MB
8859
8860 /* Top/bottom shadow colors. */
8861
8862 /* Allocate them, if necessary. */
8863 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8864 {
8865 pixel = f->output_data.x->scroll_bar_background_pixel;
8866 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8867 &pixel, 1.2, 0x8000))
8868 pixel = -1;
8869 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8870 }
8871 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8872 {
8873 pixel = f->output_data.x->scroll_bar_background_pixel;
8874 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8875 &pixel, 0.6, 0x4000))
8876 pixel = -1;
8877 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8878 }
8879
8880 /* Tell the toolkit about them. */
8881 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8882 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8883 /* We tried to allocate a color for the top/bottom shadow, and
8884 failed, so tell Xaw3d to use dithering instead. */
8885 {
8886 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8887 ++ac;
8888 }
8889 else
8890 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8891 be more consistent with other emacs 3d colors, and since Xaw3d is
8892 not good at dealing with allocation failure. */
8893 {
8894 /* This tells Xaw3d to use real colors instead of dithering for
8895 the shadows. */
8896 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8897 ++ac;
8898
8899 /* Specify the colors. */
8900 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8901 if (pixel != -1)
8902 {
8903 XtSetArg (av[ac], "topShadowPixel", pixel);
8904 ++ac;
8905 }
8906 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8907 if (pixel != -1)
8908 {
8909 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8910 ++ac;
8911 }
8912 }
8913
06a2c219
GM
8914 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8915 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8916
8917 {
8918 char *initial = "";
8919 char *val = initial;
8920 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8921 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8922 if (val == initial)
8923 { /* ARROW_SCROLL */
8924 xaw3d_arrow_scroll = True;
8925 /* Isn't that just a personal preference ? -sm */
8926 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8927 }
8928 }
06a2c219
GM
8929
8930 /* Define callbacks. */
ec18280f
SM
8931 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8932 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8933 (XtPointer) bar);
8934
8935 /* Realize the widget. Only after that is the X window created. */
8936 XtRealizeWidget (widget);
8937
ec18280f 8938#endif /* !USE_MOTIF */
06a2c219
GM
8939
8940 /* Install an action hook that let's us detect when the user
8941 finishes interacting with a scroll bar. */
8942 if (action_hook_id == 0)
8943 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8944
8945 /* Remember X window and widget in the scroll bar vector. */
8946 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8947 xwindow = XtWindow (widget);
8948 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8949
8950 UNBLOCK_INPUT;
8951}
8952
8953
8954/* Set the thumb size and position of scroll bar BAR. We are currently
8955 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8956
8957static void
8958x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8959 struct scroll_bar *bar;
8960 int portion, position, whole;
f451eb13 8961{
e83dc917
GM
8962 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8963 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8964 float top, shown;
f451eb13 8965
6f2a7a68
SM
8966 BLOCK_INPUT;
8967
8968#ifdef USE_MOTIF
8969
8970 /* We use an estimate of 30 chars per line rather than the real
8971 `portion' value. This has the disadvantage that the thumb size
8972 is not very representative, but it makes our life a lot easier.
8973 Otherwise, we have to constantly adjust the thumb size, which
8974 we can't always do quickly enough: while dragging, the size of
8975 the thumb might prevent the user from dragging the thumb all the
8976 way to the end. but Motif and some versions of Xaw3d don't allow
8977 updating the thumb size while dragging. Also, even if we can update
8978 its size, the update will often happen too late.
8979 If you don't believe it, check out revision 1.650 of xterm.c to see
8980 what hoops we were going through and the still poor behavior we got. */
8981 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
8982 /* When the thumb is at the bottom, position == whole.
8983 So we need to increase `whole' to make space for the thumb. */
8984 whole += portion;
8985
8986 if (whole <= 0)
06a2c219
GM
8987 top = 0, shown = 1;
8988 else
f451eb13 8989 {
06a2c219
GM
8990 top = (float) position / whole;
8991 shown = (float) portion / whole;
8992 }
f451eb13 8993
6f2a7a68
SM
8994 if (NILP (bar->dragging))
8995 {
8996 int size, value;
06a2c219 8997
6f2a7a68
SM
8998 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8999 is the scroll bar's maximum and MIN is the scroll bar's minimum
9000 value. */
9001 size = shown * XM_SB_RANGE;
9002 size = min (size, XM_SB_RANGE);
9003 size = max (size, 1);
06a2c219 9004
6f2a7a68
SM
9005 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
9006 value = top * XM_SB_RANGE;
9007 value = min (value, XM_SB_MAX - size);
9008 value = max (value, XM_SB_MIN);
9009
06a2c219 9010 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 9011 }
ec18280f 9012#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
9013
9014 if (whole == 0)
9015 top = 0, shown = 1;
9016 else
9017 {
9018 top = (float) position / whole;
9019 shown = (float) portion / whole;
9020 }
9021
06a2c219 9022 {
ec18280f
SM
9023 float old_top, old_shown;
9024 Dimension height;
9025 XtVaGetValues (widget,
9026 XtNtopOfThumb, &old_top,
9027 XtNshown, &old_shown,
9028 XtNheight, &height,
9029 NULL);
9030
9031 /* Massage the top+shown values. */
9032 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
9033 top = max (0, min (1, top));
9034 else
9035 top = old_top;
9036 /* Keep two pixels available for moving the thumb down. */
9037 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
9038
9039 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
9040 check that your system's configuration file contains a define
9041 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 9042 if (top != old_top || shown != old_shown)
eb393530 9043 {
ec18280f 9044 if (NILP (bar->dragging))
eb393530 9045 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
9046 else
9047 {
ec18280f
SM
9048#ifdef HAVE_XAW3D
9049 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 9050 int scroll_mode = 0;
ec18280f
SM
9051
9052 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
9053 if (xaw3d_arrow_scroll)
9054 {
9055 /* Xaw3d stupidly ignores resize requests while dragging
9056 so we have to make it believe it's not in dragging mode. */
9057 scroll_mode = sb->scrollbar.scroll_mode;
9058 if (scroll_mode == 2)
9059 sb->scrollbar.scroll_mode = 0;
9060 }
9061#endif
9062 /* Try to make the scrolling a tad smoother. */
9063 if (!xaw3d_pick_top)
9064 shown = min (shown, old_shown);
9065
9066 XawScrollbarSetThumb (widget, top, shown);
9067
9068#ifdef HAVE_XAW3D
9069 if (xaw3d_arrow_scroll && scroll_mode == 2)
9070 sb->scrollbar.scroll_mode = scroll_mode;
9071#endif
06a2c219 9072 }
06a2c219
GM
9073 }
9074 }
ec18280f 9075#endif /* !USE_MOTIF */
06a2c219
GM
9076
9077 UNBLOCK_INPUT;
f451eb13
JB
9078}
9079
06a2c219
GM
9080#endif /* USE_TOOLKIT_SCROLL_BARS */
9081
9082
9083\f
9084/************************************************************************
9085 Scroll bars, general
9086 ************************************************************************/
9087
9088/* Create a scroll bar and return the scroll bar vector for it. W is
9089 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 9090 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
9091 scroll bar. */
9092
ab648270 9093static struct scroll_bar *
06a2c219
GM
9094x_scroll_bar_create (w, top, left, width, height)
9095 struct window *w;
f451eb13
JB
9096 int top, left, width, height;
9097{
06a2c219 9098 struct frame *f = XFRAME (w->frame);
334208b7
RS
9099 struct scroll_bar *bar
9100 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
9101
9102 BLOCK_INPUT;
9103
eccc05db 9104#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9105 x_create_toolkit_scroll_bar (f, bar);
9106#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9107 {
9108 XSetWindowAttributes a;
9109 unsigned long mask;
5c187dee 9110 Window window;
06a2c219
GM
9111
9112 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9113 if (a.background_pixel == -1)
9114 a.background_pixel = f->output_data.x->background_pixel;
9115
12ba150f 9116 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 9117 | ButtonMotionMask | PointerMotionHintMask
12ba150f 9118 | ExposureMask);
7a13e894 9119 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 9120
dbc4e1c1 9121 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 9122
06a2c219
GM
9123 /* Clear the area of W that will serve as a scroll bar. This is
9124 for the case that a window has been split horizontally. In
9125 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
9126 if (width > 0 && height > 0)
9127 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9128 left, top, width,
9129 window_box_height (w), False);
06a2c219
GM
9130
9131 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9132 /* Position and size of scroll bar. */
9133 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9134 top,
9135 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9136 height,
9137 /* Border width, depth, class, and visual. */
9138 0,
9139 CopyFromParent,
9140 CopyFromParent,
9141 CopyFromParent,
9142 /* Attributes. */
9143 mask, &a);
9144 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 9145 }
06a2c219 9146#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9147
06a2c219 9148 XSETWINDOW (bar->window, w);
e0c1aef2
KH
9149 XSETINT (bar->top, top);
9150 XSETINT (bar->left, left);
9151 XSETINT (bar->width, width);
9152 XSETINT (bar->height, height);
9153 XSETINT (bar->start, 0);
9154 XSETINT (bar->end, 0);
12ba150f 9155 bar->dragging = Qnil;
f451eb13
JB
9156
9157 /* Add bar to its frame's list of scroll bars. */
334208b7 9158 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 9159 bar->prev = Qnil;
334208b7 9160 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 9161 if (!NILP (bar->next))
e0c1aef2 9162 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 9163
06a2c219 9164 /* Map the window/widget. */
eccc05db 9165#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
9166 {
9167 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9168 XtConfigureWidget (scroll_bar,
9169 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9170 top,
9171 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9172 max (height, 1), 0);
9173 XtMapWidget (scroll_bar);
9174 }
06a2c219 9175#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 9176 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 9177#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9178
9179 UNBLOCK_INPUT;
12ba150f 9180 return bar;
f451eb13
JB
9181}
9182
06a2c219 9183
12ba150f 9184/* Draw BAR's handle in the proper position.
06a2c219 9185
12ba150f
JB
9186 If the handle is already drawn from START to END, don't bother
9187 redrawing it, unless REBUILD is non-zero; in that case, always
9188 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 9189 events.)
12ba150f
JB
9190
9191 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
9192 fit inside its rectangle, but if the user is dragging the scroll
9193 bar handle, we want to let them drag it down all the way, so that
9194 the bar's top is as far down as it goes; otherwise, there's no way
9195 to move to the very end of the buffer. */
9196
5c187dee
GM
9197#ifndef USE_TOOLKIT_SCROLL_BARS
9198
f451eb13 9199static void
ab648270
JB
9200x_scroll_bar_set_handle (bar, start, end, rebuild)
9201 struct scroll_bar *bar;
f451eb13 9202 int start, end;
12ba150f 9203 int rebuild;
f451eb13 9204{
12ba150f 9205 int dragging = ! NILP (bar->dragging);
ab648270 9206 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9207 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9208 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
9209
9210 /* If the display is already accurate, do nothing. */
9211 if (! rebuild
9212 && start == XINT (bar->start)
9213 && end == XINT (bar->end))
9214 return;
9215
f451eb13
JB
9216 BLOCK_INPUT;
9217
9218 {
d9cdbb3d
RS
9219 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9220 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9221 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
9222
9223 /* Make sure the values are reasonable, and try to preserve
9224 the distance between start and end. */
12ba150f
JB
9225 {
9226 int length = end - start;
9227
9228 if (start < 0)
9229 start = 0;
9230 else if (start > top_range)
9231 start = top_range;
9232 end = start + length;
9233
9234 if (end < start)
9235 end = start;
9236 else if (end > top_range && ! dragging)
9237 end = top_range;
9238 }
f451eb13 9239
ab648270 9240 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
9241 XSETINT (bar->start, start);
9242 XSETINT (bar->end, end);
f451eb13 9243
12ba150f
JB
9244 /* Clip the end position, just for display. */
9245 if (end > top_range)
9246 end = top_range;
f451eb13 9247
ab648270 9248 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
9249 below top positions, to make sure the handle is always at least
9250 that many pixels tall. */
ab648270 9251 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 9252
12ba150f
JB
9253 /* Draw the empty space above the handle. Note that we can't clear
9254 zero-height areas; that means "clear to end of window." */
9255 if (0 < start)
c5e6e06b
GM
9256 x_clear_area (FRAME_X_DISPLAY (f), w,
9257 /* x, y, width, height, and exposures. */
9258 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9259 VERTICAL_SCROLL_BAR_TOP_BORDER,
9260 inside_width, start,
9261 False);
f451eb13 9262
06a2c219
GM
9263 /* Change to proper foreground color if one is specified. */
9264 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9265 XSetForeground (FRAME_X_DISPLAY (f), gc,
9266 f->output_data.x->scroll_bar_foreground_pixel);
9267
12ba150f 9268 /* Draw the handle itself. */
334208b7 9269 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 9270 /* x, y, width, height */
ab648270
JB
9271 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9272 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 9273 inside_width, end - start);
f451eb13 9274
06a2c219
GM
9275 /* Restore the foreground color of the GC if we changed it above. */
9276 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9277 XSetForeground (FRAME_X_DISPLAY (f), gc,
9278 f->output_data.x->foreground_pixel);
f451eb13 9279
12ba150f
JB
9280 /* Draw the empty space below the handle. Note that we can't
9281 clear zero-height areas; that means "clear to end of window." */
9282 if (end < inside_height)
c5e6e06b
GM
9283 x_clear_area (FRAME_X_DISPLAY (f), w,
9284 /* x, y, width, height, and exposures. */
9285 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9286 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9287 inside_width, inside_height - end,
9288 False);
f451eb13 9289
f451eb13
JB
9290 }
9291
f451eb13
JB
9292 UNBLOCK_INPUT;
9293}
9294
5c187dee 9295#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 9296
06a2c219
GM
9297/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9298 nil. */
58769bee 9299
12ba150f 9300static void
ab648270
JB
9301x_scroll_bar_remove (bar)
9302 struct scroll_bar *bar;
12ba150f 9303{
e83dc917 9304 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
9305 BLOCK_INPUT;
9306
eccc05db 9307#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
9308 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9309#else
9310 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9311#endif
06a2c219 9312
ab648270
JB
9313 /* Disassociate this scroll bar from its window. */
9314 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
9315
9316 UNBLOCK_INPUT;
9317}
9318
06a2c219 9319
12ba150f
JB
9320/* Set the handle of the vertical scroll bar for WINDOW to indicate
9321 that we are displaying PORTION characters out of a total of WHOLE
ab648270 9322 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 9323 create one. */
06a2c219 9324
12ba150f 9325static void
06a2c219
GM
9326XTset_vertical_scroll_bar (w, portion, whole, position)
9327 struct window *w;
f451eb13
JB
9328 int portion, whole, position;
9329{
06a2c219 9330 struct frame *f = XFRAME (w->frame);
ab648270 9331 struct scroll_bar *bar;
3c6ede7b 9332 int top, height, left, sb_left, width, sb_width;
06a2c219 9333 int window_x, window_y, window_width, window_height;
06a2c219 9334
3c6ede7b 9335 /* Get window dimensions. */
06a2c219 9336 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
9337 top = window_y;
9338 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9339 height = window_height;
06a2c219 9340
3c6ede7b 9341 /* Compute the left edge of the scroll bar area. */
06a2c219 9342 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
9343 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9344 else
9345 left = XFASTINT (w->left);
9346 left *= CANON_X_UNIT (f);
9347 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9348
9349 /* Compute the width of the scroll bar which might be less than
9350 the width of the area reserved for the scroll bar. */
9351 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9352 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 9353 else
3c6ede7b 9354 sb_width = width;
12ba150f 9355
3c6ede7b
GM
9356 /* Compute the left edge of the scroll bar. */
9357#ifdef USE_TOOLKIT_SCROLL_BARS
9358 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9359 sb_left = left + width - sb_width - (width - sb_width) / 2;
9360 else
9361 sb_left = left + (width - sb_width) / 2;
9362#else
9363 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9364 sb_left = left + width - sb_width;
9365 else
9366 sb_left = left;
9367#endif
9368
ab648270 9369 /* Does the scroll bar exist yet? */
06a2c219 9370 if (NILP (w->vertical_scroll_bar))
3c6ede7b 9371 {
7b49b9d2 9372 if (width > 0 && height > 0)
b547b6e8
GM
9373 {
9374 BLOCK_INPUT;
9375 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9376 left, top, width, height, False);
9377 UNBLOCK_INPUT;
9378 }
9379
3c6ede7b
GM
9380 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9381 }
f451eb13 9382 else
12ba150f
JB
9383 {
9384 /* It may just need to be moved and resized. */
06a2c219
GM
9385 unsigned int mask = 0;
9386
9387 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9388
9389 BLOCK_INPUT;
9390
3c6ede7b 9391 if (sb_left != XINT (bar->left))
06a2c219 9392 mask |= CWX;
3c6ede7b 9393 if (top != XINT (bar->top))
06a2c219 9394 mask |= CWY;
3c6ede7b 9395 if (sb_width != XINT (bar->width))
06a2c219 9396 mask |= CWWidth;
3c6ede7b 9397 if (height != XINT (bar->height))
06a2c219
GM
9398 mask |= CWHeight;
9399
9400#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
9401
9402 /* Since toolkit scroll bars are smaller than the space reserved
9403 for them on the frame, we have to clear "under" them. */
7b49b9d2 9404 if (width > 0 && height > 0)
f964b4d7
GM
9405 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9406 left, top, width, height, False);
06a2c219
GM
9407
9408 /* Move/size the scroll bar widget. */
9409 if (mask)
e83dc917 9410 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
9411 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9412 top,
9413 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 9414 max (height, 1), 0);
06a2c219
GM
9415
9416#else /* not USE_TOOLKIT_SCROLL_BARS */
9417
357e7376
GM
9418 /* Clear areas not covered by the scroll bar because of
9419 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
9420 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9421 {
c5e6e06b
GM
9422 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9423 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9424 height, False);
9425 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9426 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9427 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9428 height, False);
e1f6572f 9429 }
357e7376
GM
9430
9431 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 9432 wide as the area reserved for it. This makes sure a
357e7376
GM
9433 previous mode line display is cleared after C-x 2 C-x 1, for
9434 example. */
9435 {
9436 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9437 int rest = area_width - sb_width;
38d2af0c
GM
9438 if (rest > 0 && height > 0)
9439 {
9440 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9441 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9442 left + area_width - rest, top,
9443 rest, height, False);
9444 else
9445 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9446 left, top, rest, height, False);
9447 }
357e7376 9448 }
06a2c219
GM
9449
9450 /* Move/size the scroll bar window. */
9451 if (mask)
9452 {
9453 XWindowChanges wc;
9454
3c6ede7b
GM
9455 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9456 wc.y = top;
9457 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9458 wc.height = height;
06a2c219
GM
9459 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9460 mask, &wc);
9461 }
9462
9463#endif /* not USE_TOOLKIT_SCROLL_BARS */
9464
9465 /* Remember new settings. */
3c6ede7b
GM
9466 XSETINT (bar->left, sb_left);
9467 XSETINT (bar->top, top);
9468 XSETINT (bar->width, sb_width);
9469 XSETINT (bar->height, height);
06a2c219
GM
9470
9471 UNBLOCK_INPUT;
12ba150f 9472 }
f451eb13 9473
eccc05db 9474#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9475 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9476#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 9477 /* Set the scroll bar's current state, unless we're currently being
f451eb13 9478 dragged. */
12ba150f 9479 if (NILP (bar->dragging))
f451eb13 9480 {
92857db0 9481 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 9482
12ba150f 9483 if (whole == 0)
ab648270 9484 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
9485 else
9486 {
43f868f5
JB
9487 int start = ((double) position * top_range) / whole;
9488 int end = ((double) (position + portion) * top_range) / whole;
ab648270 9489 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 9490 }
f451eb13 9491 }
06a2c219 9492#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9493
06a2c219 9494 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
9495}
9496
12ba150f 9497
f451eb13 9498/* The following three hooks are used when we're doing a thorough
ab648270 9499 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 9500 are going to be deleted, because keeping track of when windows go
12ba150f
JB
9501 away is a real pain - "Can you say set-window-configuration, boys
9502 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 9503 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 9504 from the fiery pit when we actually redisplay its window. */
f451eb13 9505
ab648270
JB
9506/* Arrange for all scroll bars on FRAME to be removed at the next call
9507 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
9508 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9509
58769bee 9510static void
ab648270 9511XTcondemn_scroll_bars (frame)
f451eb13
JB
9512 FRAME_PTR frame;
9513{
f9e24cb9
RS
9514 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9515 while (! NILP (FRAME_SCROLL_BARS (frame)))
9516 {
9517 Lisp_Object bar;
9518 bar = FRAME_SCROLL_BARS (frame);
9519 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9520 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9521 XSCROLL_BAR (bar)->prev = Qnil;
9522 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9523 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9524 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9525 }
f451eb13
JB
9526}
9527
fa2dfc30 9528
06a2c219 9529/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 9530 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 9531
f451eb13 9532static void
ab648270 9533XTredeem_scroll_bar (window)
12ba150f 9534 struct window *window;
f451eb13 9535{
ab648270 9536 struct scroll_bar *bar;
fa2dfc30 9537 struct frame *f;
12ba150f 9538
ab648270
JB
9539 /* We can't redeem this window's scroll bar if it doesn't have one. */
9540 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
9541 abort ();
9542
ab648270 9543 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
9544
9545 /* Unlink it from the condemned list. */
fa2dfc30
GM
9546 f = XFRAME (WINDOW_FRAME (window));
9547 if (NILP (bar->prev))
9548 {
9549 /* If the prev pointer is nil, it must be the first in one of
9550 the lists. */
9551 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9552 /* It's not condemned. Everything's fine. */
9553 return;
9554 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9555 window->vertical_scroll_bar))
9556 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9557 else
9558 /* If its prev pointer is nil, it must be at the front of
9559 one or the other! */
9560 abort ();
9561 }
9562 else
9563 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 9564
fa2dfc30
GM
9565 if (! NILP (bar->next))
9566 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 9567
fa2dfc30
GM
9568 bar->next = FRAME_SCROLL_BARS (f);
9569 bar->prev = Qnil;
9570 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9571 if (! NILP (bar->next))
9572 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
9573}
9574
ab648270
JB
9575/* Remove all scroll bars on FRAME that haven't been saved since the
9576 last call to `*condemn_scroll_bars_hook'. */
06a2c219 9577
f451eb13 9578static void
ab648270 9579XTjudge_scroll_bars (f)
12ba150f 9580 FRAME_PTR f;
f451eb13 9581{
12ba150f 9582 Lisp_Object bar, next;
f451eb13 9583
ab648270 9584 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
9585
9586 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
9587 more events on the hapless scroll bars. */
9588 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
9589
9590 for (; ! NILP (bar); bar = next)
f451eb13 9591 {
ab648270 9592 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 9593
ab648270 9594 x_scroll_bar_remove (b);
12ba150f
JB
9595
9596 next = b->next;
9597 b->next = b->prev = Qnil;
f451eb13 9598 }
12ba150f 9599
ab648270 9600 /* Now there should be no references to the condemned scroll bars,
12ba150f 9601 and they should get garbage-collected. */
f451eb13
JB
9602}
9603
9604
06a2c219
GM
9605/* Handle an Expose or GraphicsExpose event on a scroll bar. This
9606 is a no-op when using toolkit scroll bars.
ab648270
JB
9607
9608 This may be called from a signal handler, so we have to ignore GC
9609 mark bits. */
06a2c219 9610
f451eb13 9611static void
ab648270
JB
9612x_scroll_bar_expose (bar, event)
9613 struct scroll_bar *bar;
f451eb13
JB
9614 XEvent *event;
9615{
06a2c219
GM
9616#ifndef USE_TOOLKIT_SCROLL_BARS
9617
ab648270 9618 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9619 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9620 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 9621 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 9622
f451eb13
JB
9623 BLOCK_INPUT;
9624
ab648270 9625 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 9626
06a2c219 9627 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 9628 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
9629
9630 /* x, y, width, height */
d9cdbb3d 9631 0, 0,
3cbd2e0b 9632 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
9633 XINT (bar->height) - 1);
9634
f451eb13 9635 UNBLOCK_INPUT;
06a2c219
GM
9636
9637#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9638}
9639
ab648270 9640/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 9641 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
9642
9643 This may be called from a signal handler, so we have to ignore GC
9644 mark bits. */
06a2c219 9645
5c187dee
GM
9646#ifndef USE_TOOLKIT_SCROLL_BARS
9647
f451eb13 9648static void
ab648270
JB
9649x_scroll_bar_handle_click (bar, event, emacs_event)
9650 struct scroll_bar *bar;
f451eb13
JB
9651 XEvent *event;
9652 struct input_event *emacs_event;
9653{
0299d313 9654 if (! GC_WINDOWP (bar->window))
12ba150f
JB
9655 abort ();
9656
3b8f9651 9657 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 9658 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
9659 emacs_event->modifiers
9660 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9661 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9662 event->xbutton.state)
9663 | (event->type == ButtonRelease
9664 ? up_modifier
9665 : down_modifier));
12ba150f 9666 emacs_event->frame_or_window = bar->window;
0f8aabe9 9667 emacs_event->arg = Qnil;
f451eb13 9668 emacs_event->timestamp = event->xbutton.time;
12ba150f 9669 {
06a2c219 9670#if 0
d9cdbb3d 9671 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 9672 int internal_height
d9cdbb3d 9673 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9674#endif
0299d313 9675 int top_range
d9cdbb3d 9676 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 9677 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
9678
9679 if (y < 0) y = 0;
9680 if (y > top_range) y = top_range;
9681
9682 if (y < XINT (bar->start))
ab648270
JB
9683 emacs_event->part = scroll_bar_above_handle;
9684 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9685 emacs_event->part = scroll_bar_handle;
12ba150f 9686 else
ab648270 9687 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
9688
9689 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
9690 they want to drag it. Lisp code needs to be able to decide
9691 whether or not we're dragging. */
929787e1 9692#if 0
12ba150f
JB
9693 /* If the user has just clicked on the handle, record where they're
9694 holding it. */
9695 if (event->type == ButtonPress
ab648270 9696 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9697 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9698#endif
12ba150f
JB
9699
9700 /* If the user has released the handle, set it to its final position. */
9701 if (event->type == ButtonRelease
9702 && ! NILP (bar->dragging))
9703 {
9704 int new_start = y - XINT (bar->dragging);
9705 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9706
ab648270 9707 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9708 bar->dragging = Qnil;
9709 }
f451eb13 9710
5116f055
JB
9711 /* Same deal here as the other #if 0. */
9712#if 0
58769bee 9713 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9714 the handle. */
ab648270 9715 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9716 emacs_event->x = bar->start;
9717 else
e0c1aef2 9718 XSETINT (emacs_event->x, y);
5116f055 9719#else
e0c1aef2 9720 XSETINT (emacs_event->x, y);
5116f055 9721#endif
f451eb13 9722
e0c1aef2 9723 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9724 }
9725}
f451eb13 9726
ab648270
JB
9727/* Handle some mouse motion while someone is dragging the scroll bar.
9728
9729 This may be called from a signal handler, so we have to ignore GC
9730 mark bits. */
06a2c219 9731
f451eb13 9732static void
ab648270
JB
9733x_scroll_bar_note_movement (bar, event)
9734 struct scroll_bar *bar;
f451eb13
JB
9735 XEvent *event;
9736{
39d8bb4d
KH
9737 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9738
f451eb13
JB
9739 last_mouse_movement_time = event->xmotion.time;
9740
39d8bb4d 9741 f->mouse_moved = 1;
e0c1aef2 9742 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9743
9744 /* If we're dragging the bar, display it. */
ab648270 9745 if (! GC_NILP (bar->dragging))
f451eb13
JB
9746 {
9747 /* Where should the handle be now? */
12ba150f 9748 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9749
12ba150f 9750 if (new_start != XINT (bar->start))
f451eb13 9751 {
12ba150f 9752 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9753
ab648270 9754 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9755 }
9756 }
f451eb13
JB
9757}
9758
5c187dee
GM
9759#endif /* !USE_TOOLKIT_SCROLL_BARS */
9760
12ba150f 9761/* Return information to the user about the current position of the mouse
ab648270 9762 on the scroll bar. */
06a2c219 9763
12ba150f 9764static void
334208b7
RS
9765x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9766 FRAME_PTR *fp;
12ba150f 9767 Lisp_Object *bar_window;
ab648270 9768 enum scroll_bar_part *part;
12ba150f
JB
9769 Lisp_Object *x, *y;
9770 unsigned long *time;
9771{
ab648270 9772 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9773 Window w = SCROLL_BAR_X_WINDOW (bar);
9774 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9775 int win_x, win_y;
559cb2fb
JB
9776 Window dummy_window;
9777 int dummy_coord;
9778 unsigned int dummy_mask;
12ba150f 9779
cf7cb199
JB
9780 BLOCK_INPUT;
9781
ab648270 9782 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9783 report that. */
334208b7 9784 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9785
559cb2fb
JB
9786 /* Root, child, root x and root y. */
9787 &dummy_window, &dummy_window,
9788 &dummy_coord, &dummy_coord,
12ba150f 9789
559cb2fb
JB
9790 /* Position relative to scroll bar. */
9791 &win_x, &win_y,
12ba150f 9792
559cb2fb
JB
9793 /* Mouse buttons and modifier keys. */
9794 &dummy_mask))
7a13e894 9795 ;
559cb2fb
JB
9796 else
9797 {
06a2c219 9798#if 0
559cb2fb 9799 int inside_height
d9cdbb3d 9800 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9801#endif
559cb2fb 9802 int top_range
d9cdbb3d 9803 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9804
9805 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9806
9807 if (! NILP (bar->dragging))
9808 win_y -= XINT (bar->dragging);
9809
9810 if (win_y < 0)
9811 win_y = 0;
9812 if (win_y > top_range)
9813 win_y = top_range;
9814
334208b7 9815 *fp = f;
7a13e894 9816 *bar_window = bar->window;
559cb2fb
JB
9817
9818 if (! NILP (bar->dragging))
9819 *part = scroll_bar_handle;
9820 else if (win_y < XINT (bar->start))
9821 *part = scroll_bar_above_handle;
9822 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9823 *part = scroll_bar_handle;
9824 else
9825 *part = scroll_bar_below_handle;
12ba150f 9826
e0c1aef2
KH
9827 XSETINT (*x, win_y);
9828 XSETINT (*y, top_range);
12ba150f 9829
39d8bb4d 9830 f->mouse_moved = 0;
559cb2fb
JB
9831 last_mouse_scroll_bar = Qnil;
9832 }
12ba150f 9833
559cb2fb 9834 *time = last_mouse_movement_time;
cf7cb199 9835
cf7cb199 9836 UNBLOCK_INPUT;
12ba150f
JB
9837}
9838
f451eb13 9839
dbc4e1c1 9840/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9841 background colors, and the scroll bars may need to be redrawn.
9842 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9843 redraw them. */
9844
dfcf069d 9845void
ab648270 9846x_scroll_bar_clear (f)
dbc4e1c1
JB
9847 FRAME_PTR f;
9848{
06a2c219 9849#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9850 Lisp_Object bar;
9851
b80c363e
RS
9852 /* We can have scroll bars even if this is 0,
9853 if we just turned off scroll bar mode.
9854 But in that case we should not clear them. */
9855 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9856 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9857 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9858 XClearArea (FRAME_X_DISPLAY (f),
9859 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9860 0, 0, 0, 0, True);
06a2c219 9861#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9862}
9863
06a2c219 9864/* This processes Expose events from the menu-bar specific X event
19126e11 9865 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9866 when handling menu-bar or pop-up items. */
3afe33e7 9867
06a2c219 9868int
3afe33e7
RS
9869process_expose_from_menu (event)
9870 XEvent event;
9871{
9872 FRAME_PTR f;
19126e11 9873 struct x_display_info *dpyinfo;
06a2c219 9874 int frame_exposed_p = 0;
3afe33e7 9875
f94397b5
KH
9876 BLOCK_INPUT;
9877
19126e11
KH
9878 dpyinfo = x_display_info_for_display (event.xexpose.display);
9879 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9880 if (f)
9881 {
9882 if (f->async_visible == 0)
9883 {
9884 f->async_visible = 1;
9885 f->async_iconified = 0;
06c488fd 9886 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9887 SET_FRAME_GARBAGED (f);
9888 }
9889 else
9890 {
06a2c219
GM
9891 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9892 event.xexpose.x, event.xexpose.y,
9893 event.xexpose.width, event.xexpose.height);
9894 frame_exposed_p = 1;
3afe33e7
RS
9895 }
9896 }
9897 else
9898 {
9899 struct scroll_bar *bar
9900 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9901
3afe33e7
RS
9902 if (bar)
9903 x_scroll_bar_expose (bar, &event);
9904 }
f94397b5
KH
9905
9906 UNBLOCK_INPUT;
06a2c219 9907 return frame_exposed_p;
3afe33e7 9908}
09756a85
RS
9909\f
9910/* Define a queue to save up SelectionRequest events for later handling. */
9911
9912struct selection_event_queue
9913 {
9914 XEvent event;
9915 struct selection_event_queue *next;
9916 };
9917
9918static struct selection_event_queue *queue;
9919
9920/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9921
09756a85
RS
9922static int x_queue_selection_requests;
9923
9924/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9925
09756a85 9926static void
334208b7
RS
9927x_queue_event (f, event)
9928 FRAME_PTR f;
09756a85
RS
9929 XEvent *event;
9930{
9931 struct selection_event_queue *queue_tmp
06a2c219 9932 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9933
58769bee 9934 if (queue_tmp != NULL)
09756a85
RS
9935 {
9936 queue_tmp->event = *event;
9937 queue_tmp->next = queue;
9938 queue = queue_tmp;
9939 }
9940}
9941
9942/* Take all the queued events and put them back
9943 so that they get processed afresh. */
9944
9945static void
db3906fd
RS
9946x_unqueue_events (display)
9947 Display *display;
09756a85 9948{
58769bee 9949 while (queue != NULL)
09756a85
RS
9950 {
9951 struct selection_event_queue *queue_tmp = queue;
db3906fd 9952 XPutBackEvent (display, &queue_tmp->event);
09756a85 9953 queue = queue_tmp->next;
06a2c219 9954 xfree ((char *)queue_tmp);
09756a85
RS
9955 }
9956}
9957
9958/* Start queuing SelectionRequest events. */
9959
9960void
db3906fd
RS
9961x_start_queuing_selection_requests (display)
9962 Display *display;
09756a85
RS
9963{
9964 x_queue_selection_requests++;
9965}
9966
9967/* Stop queuing SelectionRequest events. */
9968
9969void
db3906fd
RS
9970x_stop_queuing_selection_requests (display)
9971 Display *display;
09756a85
RS
9972{
9973 x_queue_selection_requests--;
db3906fd 9974 x_unqueue_events (display);
09756a85 9975}
f451eb13
JB
9976\f
9977/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9978
042c33d3 9979#if 0
06a2c219 9980/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9981 but we have to put it out here, since static variables within functions
9982 sometimes don't work. */
06a2c219 9983
dc6f92b8 9984static Time enter_timestamp;
042c33d3 9985#endif
dc6f92b8 9986
11edeb03 9987/* This holds the state XLookupString needs to implement dead keys
58769bee 9988 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9989 says that a portable program can't use this, but Stephen Gildea assures
9990 me that letting the compiler initialize it to zeros will work okay.
9991
9992 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 9993 given for enter_timestamp, above. */
06a2c219 9994
11edeb03
JB
9995static XComposeStatus compose_status;
9996
10e6549c
RS
9997/* Record the last 100 characters stored
9998 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9999
2224b905
RS
10000static int temp_index;
10001static short temp_buffer[100];
10e6549c 10002
7a13e894
RS
10003/* Set this to nonzero to fake an "X I/O error"
10004 on a particular display. */
06a2c219 10005
7a13e894
RS
10006struct x_display_info *XTread_socket_fake_io_error;
10007
2224b905
RS
10008/* When we find no input here, we occasionally do a no-op command
10009 to verify that the X server is still running and we can still talk with it.
10010 We try all the open displays, one by one.
10011 This variable is used for cycling thru the displays. */
06a2c219 10012
2224b905
RS
10013static struct x_display_info *next_noop_dpyinfo;
10014
06a2c219
GM
10015#define SET_SAVED_MENU_EVENT(size) \
10016 do \
10017 { \
10018 if (f->output_data.x->saved_menu_event == 0) \
10019 f->output_data.x->saved_menu_event \
10020 = (XEvent *) xmalloc (sizeof (XEvent)); \
10021 bcopy (&event, f->output_data.x->saved_menu_event, size); \
10022 if (numchars >= 1) \
10023 { \
3b8f9651 10024 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
06a2c219 10025 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 10026 bufp->arg = Qnil; \
06a2c219
GM
10027 bufp++; \
10028 count++; \
10029 numchars--; \
10030 } \
10031 } \
10032 while (0)
10033
8805890a 10034#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 10035#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 10036
dc6f92b8
JB
10037/* Read events coming from the X server.
10038 This routine is called by the SIGIO handler.
10039 We return as soon as there are no more events to be read.
10040
10041 Events representing keys are stored in buffer BUFP,
10042 which can hold up to NUMCHARS characters.
10043 We return the number of characters stored into the buffer,
10044 thus pretending to be `read'.
10045
dc6f92b8
JB
10046 EXPECTED is nonzero if the caller knows input is available. */
10047
6f2a7a68 10048static int
f66868ba 10049XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 10050 register int sd;
8805890a
KH
10051 /* register */ struct input_event *bufp;
10052 /* register */ int numchars;
dc6f92b8
JB
10053 int expected;
10054{
10055 int count = 0;
10056 int nbytes = 0;
dc6f92b8 10057 XEvent event;
f676886a 10058 struct frame *f;
66f55a9d 10059 int event_found = 0;
334208b7 10060 struct x_display_info *dpyinfo;
379b5ac0 10061 struct coding_system coding;
dc6f92b8 10062
9ac0d9e0 10063 if (interrupt_input_blocked)
dc6f92b8 10064 {
9ac0d9e0 10065 interrupt_input_pending = 1;
dc6f92b8
JB
10066 return -1;
10067 }
10068
9ac0d9e0 10069 interrupt_input_pending = 0;
dc6f92b8 10070 BLOCK_INPUT;
c0a04927
RS
10071
10072 /* So people can tell when we have read the available input. */
10073 input_signal_count++;
10074
dc6f92b8 10075 if (numchars <= 0)
06a2c219 10076 abort (); /* Don't think this happens. */
dc6f92b8 10077
bde5503b
GM
10078 ++handling_signal;
10079
7a13e894
RS
10080 /* Find the display we are supposed to read input for.
10081 It's the one communicating on descriptor SD. */
10082 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
10083 {
10084#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 10085#ifdef FIOSNBIO
7a13e894
RS
10086 /* If available, Xlib uses FIOSNBIO to make the socket
10087 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 10088 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 10089 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 10090 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 10091#endif /* ! defined (FIOSNBIO) */
7a13e894 10092#endif
dc6f92b8 10093
7a13e894
RS
10094#if 0 /* This code can't be made to work, with multiple displays,
10095 and appears not to be used on any system any more.
10096 Also keyboard.c doesn't turn O_NDELAY on and off
10097 for X connections. */
dc6f92b8
JB
10098#ifndef SIGIO
10099#ifndef HAVE_SELECT
7a13e894
RS
10100 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10101 {
10102 extern int read_alarm_should_throw;
10103 read_alarm_should_throw = 1;
10104 XPeekEvent (dpyinfo->display, &event);
10105 read_alarm_should_throw = 0;
10106 }
c118dd06
JB
10107#endif /* HAVE_SELECT */
10108#endif /* SIGIO */
7a13e894 10109#endif
dc6f92b8 10110
7a13e894
RS
10111 /* For debugging, this gives a way to fake an I/O error. */
10112 if (dpyinfo == XTread_socket_fake_io_error)
10113 {
10114 XTread_socket_fake_io_error = 0;
10115 x_io_error_quitter (dpyinfo->display);
10116 }
dc6f92b8 10117
5f30b957
JD
10118#ifdef HAVE_X_SM
10119 BLOCK_INPUT;
10120 count += x_session_check_input (bufp, &numchars);
10121 UNBLOCK_INPUT;
10122#endif
10123
06a2c219 10124 while (XPending (dpyinfo->display))
dc6f92b8 10125 {
7a13e894 10126 XNextEvent (dpyinfo->display, &event);
06a2c219 10127
531483fb 10128#ifdef HAVE_X_I18N
d1bc4182 10129 {
f2be1146
GM
10130 /* Filter events for the current X input method.
10131 XFilterEvent returns non-zero if the input method has
10132 consumed the event. We pass the frame's X window to
10133 XFilterEvent because that's the one for which the IC
10134 was created. */
f5d11644
GM
10135 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10136 event.xclient.window);
10137 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
10138 break;
10139 }
0cd6403b 10140#endif
7a13e894
RS
10141 event_found = 1;
10142
10143 switch (event.type)
10144 {
10145 case ClientMessage:
c047688c 10146 {
7a13e894
RS
10147 if (event.xclient.message_type
10148 == dpyinfo->Xatom_wm_protocols
10149 && event.xclient.format == 32)
c047688c 10150 {
7a13e894
RS
10151 if (event.xclient.data.l[0]
10152 == dpyinfo->Xatom_wm_take_focus)
c047688c 10153 {
8c1a6a84
RS
10154 /* Use x_any_window_to_frame because this
10155 could be the shell widget window
10156 if the frame has no title bar. */
10157 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
10158#ifdef HAVE_X_I18N
10159 /* Not quite sure this is needed -pd */
8c1a6a84 10160 if (f && FRAME_XIC (f))
6c183ba5
RS
10161 XSetICFocus (FRAME_XIC (f));
10162#endif
f1da8f06
GM
10163#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10164 instructs the WM to set the input focus automatically for
10165 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10166 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10167 it has set the focus. So, XSetInputFocus below is not
10168 needed.
10169
10170 The call to XSetInputFocus below has also caused trouble. In
10171 cases where the XSetInputFocus done by the WM and the one
10172 below are temporally close (on a fast machine), the call
10173 below can generate additional FocusIn events which confuse
10174 Emacs. */
10175
bf7253f4
RS
10176 /* Since we set WM_TAKE_FOCUS, we must call
10177 XSetInputFocus explicitly. But not if f is null,
10178 since that might be an event for a deleted frame. */
7a13e894 10179 if (f)
bf7253f4
RS
10180 {
10181 Display *d = event.xclient.display;
10182 /* Catch and ignore errors, in case window has been
10183 iconified by a window manager such as GWM. */
10184 int count = x_catch_errors (d);
10185 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
10186 /* The ICCCM says this is
10187 the only valid choice. */
10188 RevertToParent,
bf7253f4
RS
10189 event.xclient.data.l[1]);
10190 /* This is needed to detect the error
10191 if there is an error. */
10192 XSync (d, False);
10193 x_uncatch_errors (d, count);
10194 }
7a13e894 10195 /* Not certain about handling scroll bars here */
f1da8f06 10196#endif /* 0 */
c047688c 10197 }
7a13e894
RS
10198 else if (event.xclient.data.l[0]
10199 == dpyinfo->Xatom_wm_save_yourself)
10200 {
10201 /* Save state modify the WM_COMMAND property to
06a2c219 10202 something which can reinstate us. This notifies
7a13e894
RS
10203 the session manager, who's looking for such a
10204 PropertyNotify. Can restart processing when
06a2c219 10205 a keyboard or mouse event arrives. */
5f30b957
JD
10206 /* If we have a session manager, don't set this.
10207 KDE will then start two Emacsen, one for the
10208 session manager and one for this. */
10209 if (numchars > 0
10210#ifdef HAVE_X_SM
10211 && ! x_session_have_connection ()
10212#endif
10213 )
7a13e894 10214 {
19126e11
KH
10215 f = x_top_window_to_frame (dpyinfo,
10216 event.xclient.window);
7a13e894
RS
10217 /* This is just so we only give real data once
10218 for a single Emacs process. */
b86bd3dd 10219 if (f == SELECTED_FRAME ())
7a13e894
RS
10220 XSetCommand (FRAME_X_DISPLAY (f),
10221 event.xclient.window,
10222 initial_argv, initial_argc);
f000f5c5 10223 else if (f)
7a13e894
RS
10224 XSetCommand (FRAME_X_DISPLAY (f),
10225 event.xclient.window,
10226 0, 0);
10227 }
10228 }
10229 else if (event.xclient.data.l[0]
10230 == dpyinfo->Xatom_wm_delete_window)
1fb20991 10231 {
19126e11
KH
10232 struct frame *f
10233 = x_any_window_to_frame (dpyinfo,
10234 event.xclient.window);
1fb20991 10235
7a13e894
RS
10236 if (f)
10237 {
10238 if (numchars == 0)
10239 abort ();
1fb20991 10240
3b8f9651 10241 bufp->kind = DELETE_WINDOW_EVENT;
7a13e894 10242 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10243 bufp->arg = Qnil;
7a13e894
RS
10244 bufp++;
10245
10246 count += 1;
10247 numchars -= 1;
10248 }
1fb20991 10249 }
c047688c 10250 }
7a13e894
RS
10251 else if (event.xclient.message_type
10252 == dpyinfo->Xatom_wm_configure_denied)
10253 {
10254 }
10255 else if (event.xclient.message_type
10256 == dpyinfo->Xatom_wm_window_moved)
10257 {
10258 int new_x, new_y;
19126e11
KH
10259 struct frame *f
10260 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 10261
7a13e894
RS
10262 new_x = event.xclient.data.s[0];
10263 new_y = event.xclient.data.s[1];
1fb20991 10264
7a13e894
RS
10265 if (f)
10266 {
7556890b
RS
10267 f->output_data.x->left_pos = new_x;
10268 f->output_data.x->top_pos = new_y;
7a13e894 10269 }
1fb20991 10270 }
0fdff6bb 10271#ifdef HACK_EDITRES
7a13e894
RS
10272 else if (event.xclient.message_type
10273 == dpyinfo->Xatom_editres)
10274 {
19126e11
KH
10275 struct frame *f
10276 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 10277 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 10278 &event, NULL);
7a13e894 10279 }
0fdff6bb 10280#endif /* HACK_EDITRES */
06a2c219
GM
10281 else if ((event.xclient.message_type
10282 == dpyinfo->Xatom_DONE)
10283 || (event.xclient.message_type
10284 == dpyinfo->Xatom_PAGE))
10285 {
10286 /* Ghostview job completed. Kill it. We could
10287 reply with "Next" if we received "Page", but we
10288 currently never do because we are interested in
10289 images, only, which should have 1 page. */
06a2c219
GM
10290 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10291 struct frame *f
10292 = x_window_to_frame (dpyinfo, event.xclient.window);
10293 x_kill_gs_process (pixmap, f);
10294 expose_frame (f, 0, 0, 0, 0);
10295 }
10296#ifdef USE_TOOLKIT_SCROLL_BARS
10297 /* Scroll bar callbacks send a ClientMessage from which
10298 we construct an input_event. */
10299 else if (event.xclient.message_type
10300 == dpyinfo->Xatom_Scrollbar)
10301 {
10302 x_scroll_bar_to_input_event (&event, bufp);
10303 ++bufp, ++count, --numchars;
10304 goto out;
10305 }
10306#endif /* USE_TOOLKIT_SCROLL_BARS */
10307 else
10308 goto OTHER;
7a13e894
RS
10309 }
10310 break;
dc6f92b8 10311
7a13e894 10312 case SelectionNotify:
3afe33e7 10313#ifdef USE_X_TOOLKIT
19126e11 10314 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 10315 goto OTHER;
3afe33e7 10316#endif /* not USE_X_TOOLKIT */
dfcf069d 10317 x_handle_selection_notify (&event.xselection);
7a13e894 10318 break;
d56a553a 10319
06a2c219 10320 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 10321#ifdef USE_X_TOOLKIT
19126e11 10322 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 10323 goto OTHER;
3afe33e7 10324#endif /* USE_X_TOOLKIT */
7a13e894
RS
10325 {
10326 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 10327
7a13e894
RS
10328 if (numchars == 0)
10329 abort ();
d56a553a 10330
3b8f9651 10331 bufp->kind = SELECTION_CLEAR_EVENT;
7a13e894
RS
10332 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10333 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10334 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10335 bufp->frame_or_window = Qnil;
0f8aabe9 10336 bufp->arg = Qnil;
7a13e894 10337 bufp++;
d56a553a 10338
7a13e894
RS
10339 count += 1;
10340 numchars -= 1;
10341 }
10342 break;
dc6f92b8 10343
06a2c219 10344 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 10345#ifdef USE_X_TOOLKIT
19126e11 10346 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 10347 goto OTHER;
3afe33e7 10348#endif /* USE_X_TOOLKIT */
7a13e894 10349 if (x_queue_selection_requests)
19126e11 10350 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
10351 &event);
10352 else
10353 {
1d2b2268
GM
10354 XSelectionRequestEvent *eventp
10355 = (XSelectionRequestEvent *) &event;
dc6f92b8 10356
7a13e894
RS
10357 if (numchars == 0)
10358 abort ();
10359
3b8f9651 10360 bufp->kind = SELECTION_REQUEST_EVENT;
7a13e894
RS
10361 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10362 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10363 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10364 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10365 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10366 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10367 bufp->frame_or_window = Qnil;
0f8aabe9 10368 bufp->arg = Qnil;
7a13e894
RS
10369 bufp++;
10370
10371 count += 1;
10372 numchars -= 1;
10373 }
10374 break;
10375
10376 case PropertyNotify:
1d2b2268
GM
10377#if 0 /* This is plain wrong. In the case that we are waiting for a
10378 PropertyNotify used as an ACK in incremental selection
10379 transfer, the property will be on the receiver's window. */
10380#if defined USE_X_TOOLKIT
19126e11 10381 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 10382 goto OTHER;
1d2b2268
GM
10383#endif
10384#endif
dfcf069d 10385 x_handle_property_notify (&event.xproperty);
1d2b2268 10386 goto OTHER;
dc6f92b8 10387
7a13e894 10388 case ReparentNotify:
19126e11 10389 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
10390 if (f)
10391 {
10392 int x, y;
7556890b 10393 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 10394 x_real_positions (f, &x, &y);
7556890b
RS
10395 f->output_data.x->left_pos = x;
10396 f->output_data.x->top_pos = y;
7a13e894
RS
10397 }
10398 break;
3bd330d4 10399
7a13e894 10400 case Expose:
19126e11 10401 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 10402 if (f)
dc6f92b8 10403 {
d0fa3e56
EZ
10404 x_check_fullscreen (f);
10405
7a13e894
RS
10406 if (f->async_visible == 0)
10407 {
10408 f->async_visible = 1;
10409 f->async_iconified = 0;
06c488fd 10410 f->output_data.x->has_been_visible = 1;
7a13e894
RS
10411 SET_FRAME_GARBAGED (f);
10412 }
10413 else
06a2c219
GM
10414 expose_frame (x_window_to_frame (dpyinfo,
10415 event.xexpose.window),
10416 event.xexpose.x, event.xexpose.y,
10417 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
10418 }
10419 else
7a13e894 10420 {
12949a7f
EZ
10421#ifndef USE_TOOLKIT_SCROLL_BARS
10422 struct scroll_bar *bar;
10423#endif
01f67d2c 10424#if defined USE_LUCID
c95fc5f1
GM
10425 /* Submenus of the Lucid menu bar aren't widgets
10426 themselves, so there's no way to dispatch events
10427 to them. Recognize this case separately. */
10428 {
10429 Widget widget
10430 = x_window_to_menu_bar (event.xexpose.window);
10431 if (widget)
10432 xlwmenu_redisplay (widget);
10433 }
01f67d2c
PJ
10434#endif /* USE_LUCID */
10435
06a2c219
GM
10436#ifdef USE_TOOLKIT_SCROLL_BARS
10437 /* Dispatch event to the widget. */
10438 goto OTHER;
10439#else /* not USE_TOOLKIT_SCROLL_BARS */
12949a7f 10440 bar = x_window_to_scroll_bar (event.xexpose.window);
58769bee 10441
7a13e894
RS
10442 if (bar)
10443 x_scroll_bar_expose (bar, &event);
3afe33e7 10444#ifdef USE_X_TOOLKIT
7a13e894
RS
10445 else
10446 goto OTHER;
3afe33e7 10447#endif /* USE_X_TOOLKIT */
06a2c219 10448#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10449 }
10450 break;
dc6f92b8 10451
7a13e894 10452 case GraphicsExpose: /* This occurs when an XCopyArea's
d624284c
PJ
10453 source area was obscured or not
10454 available. */
19126e11 10455 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
10456 if (f)
10457 {
06a2c219
GM
10458 expose_frame (f,
10459 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10460 event.xgraphicsexpose.width,
10461 event.xgraphicsexpose.height);
7a13e894 10462 }
3afe33e7 10463#ifdef USE_X_TOOLKIT
7a13e894
RS
10464 else
10465 goto OTHER;
3afe33e7 10466#endif /* USE_X_TOOLKIT */
7a13e894 10467 break;
dc6f92b8 10468
7a13e894 10469 case NoExpose: /* This occurs when an XCopyArea's
06a2c219 10470 source area was completely
d624284c 10471 available. */
7a13e894 10472 break;
dc6f92b8 10473
7a13e894 10474 case UnmapNotify:
06a2c219
GM
10475 /* Redo the mouse-highlight after the tooltip has gone. */
10476 if (event.xmap.window == tip_window)
10477 {
10478 tip_window = 0;
10479 redo_mouse_highlight ();
10480 }
10481
91ea2a7a 10482 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894 10483 if (f) /* F may no longer exist if
d624284c 10484 the frame was deleted. */
7a13e894
RS
10485 {
10486 /* While a frame is unmapped, display generation is
10487 disabled; you don't want to spend time updating a
10488 display that won't ever be seen. */
10489 f->async_visible = 0;
10490 /* We can't distinguish, from the event, whether the window
10491 has become iconified or invisible. So assume, if it
10492 was previously visible, than now it is iconified.
1aa6072f
RS
10493 But x_make_frame_invisible clears both
10494 the visible flag and the iconified flag;
10495 and that way, we know the window is not iconified now. */
7a13e894 10496 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
10497 {
10498 f->async_iconified = 1;
bddd097c 10499
3b8f9651 10500 bufp->kind = ICONIFY_EVENT;
1aa6072f 10501 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10502 bufp->arg = Qnil;
1aa6072f
RS
10503 bufp++;
10504 count++;
10505 numchars--;
10506 }
7a13e894 10507 }
7a13e894 10508 goto OTHER;
dc6f92b8 10509
7a13e894 10510 case MapNotify:
06a2c219
GM
10511 if (event.xmap.window == tip_window)
10512 /* The tooltip has been drawn already. Avoid
10513 the SET_FRAME_GARBAGED below. */
10514 goto OTHER;
10515
10516 /* We use x_top_window_to_frame because map events can
10517 come for sub-windows and they don't mean that the
10518 frame is visible. */
19126e11 10519 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
10520 if (f)
10521 {
10522 f->async_visible = 1;
10523 f->async_iconified = 0;
06c488fd 10524 f->output_data.x->has_been_visible = 1;
dc6f92b8 10525
7a13e894
RS
10526 /* wait_reading_process_input will notice this and update
10527 the frame's display structures. */
10528 SET_FRAME_GARBAGED (f);
bddd097c 10529
d806e720
RS
10530 if (f->iconified)
10531 {
3b8f9651 10532 bufp->kind = DEICONIFY_EVENT;
d806e720 10533 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10534 bufp->arg = Qnil;
d806e720
RS
10535 bufp++;
10536 count++;
10537 numchars--;
10538 }
e73ec6fa 10539 else if (! NILP (Vframe_list)
8e713be6 10540 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
10541 /* Force a redisplay sooner or later
10542 to update the frame titles
10543 in case this is the second frame. */
10544 record_asynch_buffer_change ();
7a13e894 10545 }
7a13e894 10546 goto OTHER;
dc6f92b8 10547
7a13e894 10548 case KeyPress:
2a793c0c
PJ
10549
10550 /* Dispatch KeyPress events when in menu. */
10551 if (popup_activated_flag)
10552 goto OTHER;
10553
19126e11 10554 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 10555
66301061
KS
10556 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10557 {
10558 dpyinfo->mouse_face_hidden = 1;
10559 clear_mouse_face (dpyinfo);
10560 }
10561
eccc05db 10562#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
10563 if (f == 0)
10564 {
2564ea1b
GM
10565 /* Scroll bars consume key events, but we want
10566 the keys to go to the scroll bar's frame. */
06a2c219
GM
10567 Widget widget = XtWindowToWidget (dpyinfo->display,
10568 event.xkey.window);
10569 if (widget && XmIsScrollBar (widget))
10570 {
10571 widget = XtParent (widget);
10572 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10573 }
10574 }
eccc05db 10575#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 10576
7a13e894
RS
10577 if (f != 0)
10578 {
10579 KeySym keysym, orig_keysym;
379b5ac0
KH
10580 /* al%imercury@uunet.uu.net says that making this 81
10581 instead of 80 fixed a bug whereby meta chars made
10582 his Emacs hang.
10583
10584 It seems that some version of XmbLookupString has
10585 a bug of not returning XBufferOverflow in
10586 status_return even if the input is too long to
10587 fit in 81 bytes. So, we must prepare sufficient
10588 bytes for copy_buffer. 513 bytes (256 chars for
6c4a22e6 10589 two-byte character set) seems to be a fairly good
379b5ac0
KH
10590 approximation. -- 2000.8.10 handa@etl.go.jp */
10591 unsigned char copy_buffer[513];
10592 unsigned char *copy_bufptr = copy_buffer;
10593 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 10594 int modifiers;
c997eae5 10595 Lisp_Object coding_system = Qlatin_1;
64bb1782 10596
7a13e894
RS
10597 event.xkey.state
10598 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10599 extra_keyboard_modifiers);
10600 modifiers = event.xkey.state;
3a2712f9 10601
7a13e894 10602 /* This will have to go some day... */
752a043f 10603
7a13e894
RS
10604 /* make_lispy_event turns chars into control chars.
10605 Don't do it here because XLookupString is too eager. */
10606 event.xkey.state &= ~ControlMask;
5d46f928
RS
10607 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10608 | dpyinfo->super_mod_mask
10609 | dpyinfo->hyper_mod_mask
10610 | dpyinfo->alt_mod_mask);
10611
1cf4a0d1
RS
10612 /* In case Meta is ComposeCharacter,
10613 clear its status. According to Markus Ehrnsperger
10614 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10615 this enables ComposeCharacter to work whether or
10616 not it is combined with Meta. */
10617 if (modifiers & dpyinfo->meta_mod_mask)
10618 bzero (&compose_status, sizeof (compose_status));
10619
6c183ba5
RS
10620#ifdef HAVE_X_I18N
10621 if (FRAME_XIC (f))
10622 {
f5d11644
GM
10623 Status status_return;
10624
c997eae5 10625 coding_system = Vlocale_coding_system;
6c183ba5 10626 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
10627 &event.xkey, copy_bufptr,
10628 copy_bufsiz, &keysym,
6c183ba5 10629 &status_return);
f5d11644
GM
10630 if (status_return == XBufferOverflow)
10631 {
10632 copy_bufsiz = nbytes + 1;
10633 copy_bufptr = (char *) alloca (copy_bufsiz);
10634 nbytes = XmbLookupString (FRAME_XIC (f),
10635 &event.xkey, copy_bufptr,
10636 copy_bufsiz, &keysym,
10637 &status_return);
10638 }
96035dca
SM
10639/* Xutf8LookupString is a new but already deprecated interface. -stef */
10640#if 0 && defined X_HAVE_UTF8_STRING
c997eae5
SM
10641 else if (status_return == XLookupKeySym)
10642 { /* Try again but with utf-8. */
10643 coding_system = Qutf_8;
10644 nbytes = Xutf8LookupString (FRAME_XIC (f),
10645 &event.xkey, copy_bufptr,
10646 copy_bufsiz, &keysym,
10647 &status_return);
10648 if (status_return == XBufferOverflow)
10649 {
10650 copy_bufsiz = nbytes + 1;
10651 copy_bufptr = (char *) alloca (copy_bufsiz);
10652 nbytes = Xutf8LookupString (FRAME_XIC (f),
10653 &event.xkey,
10654 copy_bufptr,
10655 copy_bufsiz, &keysym,
10656 &status_return);
10657 }
10658 }
10659#endif
f5d11644 10660
1decb680
PE
10661 if (status_return == XLookupNone)
10662 break;
10663 else if (status_return == XLookupChars)
fdd9d55e
GM
10664 {
10665 keysym = NoSymbol;
10666 modifiers = 0;
10667 }
1decb680
PE
10668 else if (status_return != XLookupKeySym
10669 && status_return != XLookupBoth)
10670 abort ();
6c183ba5
RS
10671 }
10672 else
379b5ac0
KH
10673 nbytes = XLookupString (&event.xkey, copy_bufptr,
10674 copy_bufsiz, &keysym,
10675 &compose_status);
6c183ba5 10676#else
379b5ac0
KH
10677 nbytes = XLookupString (&event.xkey, copy_bufptr,
10678 copy_bufsiz, &keysym,
10679 &compose_status);
6c183ba5 10680#endif
dc6f92b8 10681
7a13e894 10682 orig_keysym = keysym;
55123275 10683
7a13e894
RS
10684 if (numchars > 1)
10685 {
10686 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10687 || keysym == XK_Delete
1097aea0 10688#ifdef XK_ISO_Left_Tab
441affdb 10689 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 10690#endif
852bff8f 10691 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
10692 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10693 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 10694#ifdef HPUX
7a13e894
RS
10695 /* This recognizes the "extended function keys".
10696 It seems there's no cleaner way.
10697 Test IsModifierKey to avoid handling mode_switch
10698 incorrectly. */
10699 || ((unsigned) (keysym) >= XK_Select
10700 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
10701#endif
10702#ifdef XK_dead_circumflex
7a13e894 10703 || orig_keysym == XK_dead_circumflex
69388238
RS
10704#endif
10705#ifdef XK_dead_grave
7a13e894 10706 || orig_keysym == XK_dead_grave
69388238
RS
10707#endif
10708#ifdef XK_dead_tilde
7a13e894 10709 || orig_keysym == XK_dead_tilde
69388238
RS
10710#endif
10711#ifdef XK_dead_diaeresis
7a13e894 10712 || orig_keysym == XK_dead_diaeresis
69388238
RS
10713#endif
10714#ifdef XK_dead_macron
7a13e894 10715 || orig_keysym == XK_dead_macron
69388238
RS
10716#endif
10717#ifdef XK_dead_degree
7a13e894 10718 || orig_keysym == XK_dead_degree
69388238
RS
10719#endif
10720#ifdef XK_dead_acute
7a13e894 10721 || orig_keysym == XK_dead_acute
69388238
RS
10722#endif
10723#ifdef XK_dead_cedilla
7a13e894 10724 || orig_keysym == XK_dead_cedilla
69388238
RS
10725#endif
10726#ifdef XK_dead_breve
7a13e894 10727 || orig_keysym == XK_dead_breve
69388238
RS
10728#endif
10729#ifdef XK_dead_ogonek
7a13e894 10730 || orig_keysym == XK_dead_ogonek
69388238
RS
10731#endif
10732#ifdef XK_dead_caron
7a13e894 10733 || orig_keysym == XK_dead_caron
69388238
RS
10734#endif
10735#ifdef XK_dead_doubleacute
7a13e894 10736 || orig_keysym == XK_dead_doubleacute
69388238
RS
10737#endif
10738#ifdef XK_dead_abovedot
7a13e894 10739 || orig_keysym == XK_dead_abovedot
c34790e0 10740#endif
7a13e894
RS
10741 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10742 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10743 /* Any "vendor-specific" key is ok. */
f0e299de
GM
10744 || (orig_keysym & (1 << 28))
10745 || (keysym != NoSymbol && nbytes == 0))
7a13e894 10746 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
10747#ifndef HAVE_X11R5
10748#ifdef XK_Mode_switch
7a13e894 10749 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
10750#endif
10751#ifdef XK_Num_Lock
7a13e894 10752 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
10753#endif
10754#endif /* not HAVE_X11R5 */
7ef18d79
EZ
10755 /* The symbols from XK_ISO_Lock to
10756 XK_ISO_Last_Group_Lock doesn't have real
10757 modifiers but should be treated similarly
10758 to Mode_switch by Emacs. */
10759#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10760 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10761 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10762#endif
7a13e894 10763 ))
dc6f92b8 10764 {
10e6549c
RS
10765 if (temp_index == sizeof temp_buffer / sizeof (short))
10766 temp_index = 0;
7a13e894 10767 temp_buffer[temp_index++] = keysym;
3b8f9651 10768 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
7a13e894 10769 bufp->code = keysym;
e0c1aef2 10770 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10771 bufp->arg = Qnil;
334208b7
RS
10772 bufp->modifiers
10773 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10774 modifiers);
1113d9db 10775 bufp->timestamp = event.xkey.time;
dc6f92b8 10776 bufp++;
7a13e894
RS
10777 count++;
10778 numchars--;
dc6f92b8 10779 }
7a13e894
RS
10780 else if (numchars > nbytes)
10781 {
10782 register int i;
379b5ac0 10783 register int c;
379b5ac0 10784 int nchars, len;
7a13e894 10785
c997eae5
SM
10786 /* The input should be decoded with `coding_system'
10787 which depends on which X*LookupString function
10788 we used just above and the locale. */
10789 setup_coding_system (coding_system, &coding);
10790 coding.src_multibyte = 0;
10791 coding.dst_multibyte = 1;
10792 /* The input is converted to events, thus we can't
10793 handle composition. Anyway, there's no XIM that
10794 gives us composition information. */
10795 coding.composing = COMPOSITION_DISABLED;
10796
7a13e894
RS
10797 for (i = 0; i < nbytes; i++)
10798 {
379b5ac0
KH
10799 if (temp_index == (sizeof temp_buffer
10800 / sizeof (short)))
7a13e894 10801 temp_index = 0;
379b5ac0
KH
10802 temp_buffer[temp_index++] = copy_bufptr[i];
10803 }
10804
c997eae5
SM
10805 {
10806 /* Decode the input data. */
10807 int require;
10808 unsigned char *p;
10809
10810 require = decoding_buffer_size (&coding, nbytes);
10811 p = (unsigned char *) alloca (require);
10812 coding.mode |= CODING_MODE_LAST_BLOCK;
d337aad5
KH
10813 /* We explicitely disable composition
10814 handling because key data should
10815 not contain any composition
10816 sequence. */
10817 coding.composing = COMPOSITION_DISABLED;
c997eae5
SM
10818 decode_coding (&coding, copy_bufptr, p,
10819 nbytes, require);
10820 nbytes = coding.produced;
10821 nchars = coding.produced_char;
10822 copy_bufptr = p;
10823 }
379b5ac0
KH
10824
10825 /* Convert the input data to a sequence of
10826 character events. */
10827 for (i = 0; i < nbytes; i += len)
10828 {
fee2aedc
GM
10829 if (nchars == nbytes)
10830 c = copy_bufptr[i], len = 1;
10831 else
10832 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10833 nbytes - i, len);
10834
379b5ac0 10835 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
3b8f9651
PJ
10836 ? ASCII_KEYSTROKE_EVENT
10837 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
379b5ac0 10838 bufp->code = c;
7a13e894 10839 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10840 bufp->arg = Qnil;
7a13e894
RS
10841 bufp->modifiers
10842 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10843 modifiers);
10844 bufp->timestamp = event.xkey.time;
10845 bufp++;
10846 }
10847
379b5ac0
KH
10848 count += nchars;
10849 numchars -= nchars;
1decb680
PE
10850
10851 if (keysym == NoSymbol)
10852 break;
7a13e894
RS
10853 }
10854 else
10855 abort ();
dc6f92b8 10856 }
10e6549c
RS
10857 else
10858 abort ();
dc6f92b8 10859 }
59ddecde
GM
10860#ifdef HAVE_X_I18N
10861 /* Don't dispatch this event since XtDispatchEvent calls
10862 XFilterEvent, and two calls in a row may freeze the
10863 client. */
10864 break;
10865#else
717ca130 10866 goto OTHER;
59ddecde 10867#endif
f451eb13 10868
f5d11644 10869 case KeyRelease:
59ddecde
GM
10870#ifdef HAVE_X_I18N
10871 /* Don't dispatch this event since XtDispatchEvent calls
10872 XFilterEvent, and two calls in a row may freeze the
10873 client. */
10874 break;
10875#else
f5d11644 10876 goto OTHER;
59ddecde 10877#endif
f5d11644 10878
7a13e894 10879 case EnterNotify:
06a2c219 10880 {
cab34f50
JD
10881 int n;
10882
10883 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10884 if (n > 0)
10885 {
10886 bufp += n, count += n, numchars -= n;
10887 }
10888
06a2c219
GM
10889 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10890
2c850e26 10891#if 0
582c60f8 10892 if (event.xcrossing.focus)
06a2c219
GM
10893 {
10894 /* Avoid nasty pop/raise loops. */
10895 if (f && (!(f->auto_raise)
10896 || !(f->auto_lower)
10897 || (event.xcrossing.time - enter_timestamp) > 500))
10898 {
10899 x_new_focus_frame (dpyinfo, f);
10900 enter_timestamp = event.xcrossing.time;
10901 }
10902 }
10903 else if (f == dpyinfo->x_focus_frame)
10904 x_new_focus_frame (dpyinfo, 0);
2c850e26
RS
10905#endif
10906
06a2c219
GM
10907 /* EnterNotify counts as mouse movement,
10908 so update things that depend on mouse position. */
2533c408 10909 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10910 note_mouse_movement (f, &event.xmotion);
10911 goto OTHER;
10912 }
dc6f92b8 10913
7a13e894 10914 case FocusIn:
fd42232c 10915 {
cab34f50 10916 int n;
eb72635f 10917
cab34f50
JD
10918 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10919 if (n > 0)
fd42232c
JD
10920 {
10921 bufp += n, count += n, numchars -= n;
10922 }
10923 }
f9e24cb9 10924
7a13e894 10925 goto OTHER;
10c5e63d 10926
7a13e894 10927 case LeaveNotify:
cab34f50
JD
10928 {
10929 int n;
10930
10931 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10932 if (n > 0)
10933 {
10934 bufp += n, count += n, numchars -= n;
10935 }
10936 }
10937
19126e11 10938 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10939 if (f)
10c5e63d 10940 {
7a13e894 10941 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10942 {
10943 /* If we move outside the frame, then we're
10944 certainly no longer on any text in the frame. */
10945 clear_mouse_face (dpyinfo);
10946 dpyinfo->mouse_face_mouse_frame = 0;
10947 }
10948
10949 /* Generate a nil HELP_EVENT to cancel a help-echo.
10950 Do it only if there's something to cancel.
10951 Otherwise, the startup message is cleared when
10952 the mouse leaves the frame. */
10953 if (any_help_event_p)
10954 {
be010514
GM
10955 Lisp_Object frame;
10956 int n;
10957
06a2c219 10958 XSETFRAME (frame, f);
82c5d67a 10959 help_echo = Qnil;
5ab2570d
GM
10960 n = gen_help_event (bufp, numchars,
10961 Qnil, frame, Qnil, Qnil, 0);
be010514 10962 bufp += n, count += n, numchars -= n;
06a2c219 10963 }
7a13e894 10964
10c5e63d 10965 }
7a13e894 10966 goto OTHER;
dc6f92b8 10967
7a13e894 10968 case FocusOut:
cab34f50
JD
10969 {
10970 int n;
10971
10972 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10973 if (n > 0)
10974 {
10975 bufp += n, count += n, numchars -= n;
10976 }
10977 }
6c183ba5 10978
7a13e894 10979 goto OTHER;
dc6f92b8 10980
7a13e894 10981 case MotionNotify:
dc6f92b8 10982 {
06a2c219 10983 previous_help_echo = help_echo;
7cea38bc 10984 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10985 help_echo_pos = -1;
06a2c219 10986
7a13e894
RS
10987 if (dpyinfo->grabbed && last_mouse_frame
10988 && FRAME_LIVE_P (last_mouse_frame))
10989 f = last_mouse_frame;
10990 else
19126e11 10991 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10992
66301061
KS
10993 if (dpyinfo->mouse_face_hidden)
10994 {
10995 dpyinfo->mouse_face_hidden = 0;
10996 clear_mouse_face (dpyinfo);
10997 }
10998
7a13e894 10999 if (f)
5bc62483
PJ
11000 {
11001
11002 /* Generate SELECT_WINDOW_EVENTs when needed. */
ee8ceff8 11003 if (mouse_autoselect_window)
5bc62483
PJ
11004 {
11005 Lisp_Object window;
11006 int area;
11007
11008 window = window_from_coordinates (f,
0fc8f792 11009 event.xmotion.x, event.xmotion.y,
5bc62483
PJ
11010 &area, 0);
11011
11012 /* Window will be selected only when it is not selected now and
11013 last mouse movement event was not in it. Minibuffer window
11014 will be selected iff it is active. */
ce1295bd
PJ
11015 if (WINDOWP(window)
11016 && !EQ (window, last_window)
5bc62483
PJ
11017 && !EQ (window, selected_window)
11018 && numchars > 0)
11019 {
11020 bufp->kind = SELECT_WINDOW_EVENT;
11021 bufp->frame_or_window = window;
11022 bufp->arg = Qnil;
11023 ++bufp, ++count, --numchars;
11024 }
11025
11026 last_window=window;
11027 }
11028 note_mouse_movement (f, &event.xmotion);
11029 }
7a13e894
RS
11030 else
11031 {
e88b3c50 11032#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
11033 struct scroll_bar *bar
11034 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 11035
7a13e894
RS
11036 if (bar)
11037 x_scroll_bar_note_movement (bar, &event);
e88b3c50 11038#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 11039
06a2c219
GM
11040 /* If we move outside the frame, then we're
11041 certainly no longer on any text in the frame. */
7a13e894
RS
11042 clear_mouse_face (dpyinfo);
11043 }
06a2c219
GM
11044
11045 /* If the contents of the global variable help_echo
11046 has changed, generate a HELP_EVENT. */
b7e80413
SM
11047 if (!NILP (help_echo)
11048 || !NILP (previous_help_echo))
06a2c219
GM
11049 {
11050 Lisp_Object frame;
be010514 11051 int n;
06a2c219
GM
11052
11053 if (f)
11054 XSETFRAME (frame, f);
11055 else
11056 frame = Qnil;
11057
11058 any_help_event_p = 1;
5ab2570d 11059 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
11060 help_echo_window, help_echo_object,
11061 help_echo_pos);
be010514 11062 bufp += n, count += n, numchars -= n;
06a2c219
GM
11063 }
11064
11065 goto OTHER;
dc6f92b8 11066 }
dc6f92b8 11067
7a13e894 11068 case ConfigureNotify:
9829ddba
RS
11069 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
11070 if (f)
af395ec1 11071 {
7a4bce14 11072#ifndef USE_X_TOOLKIT
d0fa3e56
EZ
11073 /* If there is a pending resize for fullscreen, don't
11074 do this one, the right one will come later.
11075 The toolkit version doesn't seem to need this, but we
7a4bce14 11076 need to reset it below. */
d0fa3e56
EZ
11077 int dont_resize =
11078 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11079 && FRAME_NEW_WIDTH (f) != 0);
bf1b7b30
KH
11080 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
11081 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
d0fa3e56
EZ
11082 if (dont_resize)
11083 goto OTHER;
5c187dee 11084
2d7fc7e8
RS
11085 /* In the toolkit version, change_frame_size
11086 is called by the code that handles resizing
11087 of the EmacsFrame widget. */
7a13e894 11088
7a13e894
RS
11089 /* Even if the number of character rows and columns has
11090 not changed, the font size may have changed, so we need
11091 to check the pixel dimensions as well. */
11092 if (columns != f->width
11093 || rows != f->height
7556890b
RS
11094 || event.xconfigure.width != f->output_data.x->pixel_width
11095 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 11096 {
7d1e984f 11097 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 11098 SET_FRAME_GARBAGED (f);
e687d06e 11099 cancel_mouse_face (f);
7a13e894 11100 }
2d7fc7e8 11101#endif
af395ec1 11102
7556890b
RS
11103 f->output_data.x->pixel_width = event.xconfigure.width;
11104 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
11105
11106 /* What we have now is the position of Emacs's own window.
11107 Convert that to the position of the window manager window. */
dcb07ae9
RS
11108 x_real_positions (f, &f->output_data.x->left_pos,
11109 &f->output_data.x->top_pos);
11110
d0fa3e56
EZ
11111 x_check_fullscreen_move(f);
11112 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11113 f->output_data.x->want_fullscreen &=
11114 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
f5d11644
GM
11115#ifdef HAVE_X_I18N
11116 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11117 xic_set_statusarea (f);
11118#endif
11119
dcb07ae9
RS
11120 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11121 {
11122 /* Since the WM decorations come below top_pos now,
11123 we must put them below top_pos in the future. */
11124 f->output_data.x->win_gravity = NorthWestGravity;
11125 x_wm_set_size_hint (f, (long) 0, 0);
11126 }
8f08dc93
KH
11127#ifdef USE_MOTIF
11128 /* Some window managers pass (0,0) as the location of
11129 the window, and the Motif event handler stores it
11130 in the emacs widget, which messes up Motif menus. */
11131 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11132 {
11133 event.xconfigure.x = f->output_data.x->widget->core.x;
11134 event.xconfigure.y = f->output_data.x->widget->core.y;
11135 }
06a2c219 11136#endif /* USE_MOTIF */
7a13e894 11137 }
2d7fc7e8 11138 goto OTHER;
dc6f92b8 11139
7a13e894
RS
11140 case ButtonPress:
11141 case ButtonRelease:
11142 {
11143 /* If we decide we want to generate an event to be seen
11144 by the rest of Emacs, we put it here. */
11145 struct input_event emacs_event;
9ea173e8 11146 int tool_bar_p = 0;
06a2c219 11147
3b8f9651 11148 emacs_event.kind = NO_EVENT;
7a13e894 11149 bzero (&compose_status, sizeof (compose_status));
9b07615b 11150
06a2c219
GM
11151 if (dpyinfo->grabbed
11152 && last_mouse_frame
9f67f20b
RS
11153 && FRAME_LIVE_P (last_mouse_frame))
11154 f = last_mouse_frame;
11155 else
2224b905 11156 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 11157
06a2c219
GM
11158 if (f)
11159 {
9ea173e8
GM
11160 /* Is this in the tool-bar? */
11161 if (WINDOWP (f->tool_bar_window)
11162 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
11163 {
11164 Lisp_Object window;
11165 int p, x, y;
11166
11167 x = event.xbutton.x;
11168 y = event.xbutton.y;
11169
11170 /* Set x and y. */
11171 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 11172 if (EQ (window, f->tool_bar_window))
06a2c219 11173 {
9ea173e8
GM
11174 x_handle_tool_bar_click (f, &event.xbutton);
11175 tool_bar_p = 1;
06a2c219
GM
11176 }
11177 }
11178
9ea173e8 11179 if (!tool_bar_p)
06a2c219
GM
11180 if (!dpyinfo->x_focus_frame
11181 || f == dpyinfo->x_focus_frame)
11182 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
11183 }
11184 else
11185 {
06a2c219 11186#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
11187 struct scroll_bar *bar
11188 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 11189
7a13e894
RS
11190 if (bar)
11191 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 11192#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
11193 }
11194
11195 if (event.type == ButtonPress)
11196 {
11197 dpyinfo->grabbed |= (1 << event.xbutton.button);
11198 last_mouse_frame = f;
edad46f6
KH
11199 /* Ignore any mouse motion that happened
11200 before this event; any subsequent mouse-movement
11201 Emacs events should reflect only motion after
11202 the ButtonPress. */
a00e91cd
KH
11203 if (f != 0)
11204 f->mouse_moved = 0;
06a2c219 11205
9ea173e8
GM
11206 if (!tool_bar_p)
11207 last_tool_bar_item = -1;
7a13e894 11208 }
3afe33e7
RS
11209 else
11210 {
7a13e894 11211 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 11212 }
23faf38f 11213
3b8f9651 11214 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
7a13e894
RS
11215 {
11216 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11217 bufp++;
11218 count++;
11219 numchars--;
11220 }
3afe33e7
RS
11221
11222#ifdef USE_X_TOOLKIT
2224b905
RS
11223 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11224 /* For a down-event in the menu bar,
11225 don't pass it to Xt right now.
11226 Instead, save it away
11227 and we will pass it to Xt from kbd_buffer_get_event.
11228 That way, we can run some Lisp code first. */
91375f8f
RS
11229 if (f && event.type == ButtonPress
11230 /* Verify the event is really within the menu bar
11231 and not just sent to it due to grabbing. */
11232 && event.xbutton.x >= 0
11233 && event.xbutton.x < f->output_data.x->pixel_width
11234 && event.xbutton.y >= 0
11235 && event.xbutton.y < f->output_data.x->menubar_height
11236 && event.xbutton.same_screen)
2224b905 11237 {
8805890a 11238 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
11239 XSETFRAME (last_mouse_press_frame, f);
11240 }
11241 else if (event.type == ButtonPress)
11242 {
11243 last_mouse_press_frame = Qnil;
30e671c3 11244 goto OTHER;
ce89ef46 11245 }
06a2c219 11246
2237cac9
RS
11247#ifdef USE_MOTIF /* This should do not harm for Lucid,
11248 but I am trying to be cautious. */
ce89ef46
RS
11249 else if (event.type == ButtonRelease)
11250 {
2237cac9 11251 if (!NILP (last_mouse_press_frame))
f10ded1c 11252 {
2237cac9
RS
11253 f = XFRAME (last_mouse_press_frame);
11254 if (f->output_data.x)
06a2c219 11255 SET_SAVED_BUTTON_EVENT;
f10ded1c 11256 }
06a2c219 11257 else
30e671c3 11258 goto OTHER;
2224b905 11259 }
2237cac9 11260#endif /* USE_MOTIF */
2224b905
RS
11261 else
11262 goto OTHER;
3afe33e7 11263#endif /* USE_X_TOOLKIT */
7a13e894
RS
11264 }
11265 break;
dc6f92b8 11266
7a13e894 11267 case CirculateNotify:
06a2c219
GM
11268 goto OTHER;
11269
7a13e894 11270 case CirculateRequest:
06a2c219
GM
11271 goto OTHER;
11272
11273 case VisibilityNotify:
11274 goto OTHER;
dc6f92b8 11275
7a13e894
RS
11276 case MappingNotify:
11277 /* Someone has changed the keyboard mapping - update the
11278 local cache. */
11279 switch (event.xmapping.request)
11280 {
11281 case MappingModifier:
11282 x_find_modifier_meanings (dpyinfo);
11283 /* This is meant to fall through. */
11284 case MappingKeyboard:
11285 XRefreshKeyboardMapping (&event.xmapping);
11286 }
7a13e894 11287 goto OTHER;
dc6f92b8 11288
7a13e894 11289 default:
7a13e894 11290 OTHER:
717ca130 11291#ifdef USE_X_TOOLKIT
7a13e894
RS
11292 BLOCK_INPUT;
11293 XtDispatchEvent (&event);
11294 UNBLOCK_INPUT;
3afe33e7 11295#endif /* USE_X_TOOLKIT */
7a13e894
RS
11296 break;
11297 }
dc6f92b8
JB
11298 }
11299 }
11300
06a2c219
GM
11301 out:;
11302
9a5196d0
RS
11303 /* On some systems, an X bug causes Emacs to get no more events
11304 when the window is destroyed. Detect that. (1994.) */
58769bee 11305 if (! event_found)
ef2a22d0 11306 {
ef2a22d0
RS
11307 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11308 One XNOOP in 100 loops will make Emacs terminate.
11309 B. Bretthauer, 1994 */
11310 x_noop_count++;
58769bee 11311 if (x_noop_count >= 100)
ef2a22d0
RS
11312 {
11313 x_noop_count=0;
2224b905
RS
11314
11315 if (next_noop_dpyinfo == 0)
11316 next_noop_dpyinfo = x_display_list;
11317
11318 XNoOp (next_noop_dpyinfo->display);
11319
11320 /* Each time we get here, cycle through the displays now open. */
11321 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
11322 }
11323 }
502add23 11324
06a2c219 11325 /* If the focus was just given to an auto-raising frame,
0134a210 11326 raise it now. */
7a13e894 11327 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
11328 if (pending_autoraise_frame)
11329 {
11330 x_raise_frame (pending_autoraise_frame);
11331 pending_autoraise_frame = 0;
11332 }
0134a210 11333
dc6f92b8 11334 UNBLOCK_INPUT;
bde5503b 11335 --handling_signal;
dc6f92b8
JB
11336 return count;
11337}
06a2c219
GM
11338
11339
11340
dc6f92b8 11341\f
06a2c219
GM
11342/***********************************************************************
11343 Text Cursor
11344 ***********************************************************************/
11345
60626bab
GM
11346/* Notice when the text cursor of window W has been completely
11347 overwritten by a drawing operation that outputs glyphs in AREA
11348 starting at X0 and ending at X1 in the line starting at Y0 and
11349 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11350 the rest of the line after X0 has been written. Y coordinates
11351 are window-relative. */
06a2c219
GM
11352
11353static void
60626bab 11354notice_overwritten_cursor (w, area, x0, x1, y0, y1)
06a2c219 11355 struct window *w;
60626bab
GM
11356 enum glyph_row_area area;
11357 int x0, y0, x1, y1;
06a2c219 11358{
b3738089
GM
11359 if (area == TEXT_AREA && w->phys_cursor_on_p)
11360 {
11361 int cx0 = w->phys_cursor.x;
11362 int cx1 = cx0 + w->phys_cursor_width;
11363 int cy0 = w->phys_cursor.y;
11364 int cy1 = cy0 + w->phys_cursor_height;
11365
11366 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11367 {
11368 /* The cursor image will be completely removed from the
11369 screen if the output area intersects the cursor area in
11370 y-direction. When we draw in [y0 y1[, and some part of
11371 the cursor is at y < y0, that part must have been drawn
11372 before. When scrolling, the cursor is erased before
11373 actually scrolling, so we don't come here. When not
11374 scrolling, the rows above the old cursor row must have
11375 changed, and in this case these rows must have written
516074a1
GM
11376 over the cursor image.
11377
11378 Likewise if part of the cursor is below y1, with the
11379 exception of the cursor being in the first blank row at
11380 the buffer and window end because update_text_area
4398e673
GM
11381 doesn't draw that row. (Except when it does, but
11382 that's handled in update_text_area.) */
11383
f7c5994d 11384 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
516074a1 11385 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
b3738089
GM
11386 w->phys_cursor_on_p = 0;
11387 }
11388 }
06a2c219 11389}
f451eb13
JB
11390
11391
06a2c219
GM
11392/* Set clipping for output in glyph row ROW. W is the window in which
11393 we operate. GC is the graphics context to set clipping in.
11394 WHOLE_LINE_P non-zero means include the areas used for truncation
11395 mark display and alike in the clipping rectangle.
11396
11397 ROW may be a text row or, e.g., a mode line. Text rows must be
11398 clipped to the interior of the window dedicated to text display,
11399 mode lines must be clipped to the whole window. */
dc6f92b8
JB
11400
11401static void
06a2c219
GM
11402x_clip_to_row (w, row, gc, whole_line_p)
11403 struct window *w;
11404 struct glyph_row *row;
11405 GC gc;
11406 int whole_line_p;
dc6f92b8 11407{
06a2c219
GM
11408 struct frame *f = XFRAME (WINDOW_FRAME (w));
11409 XRectangle clip_rect;
11410 int window_x, window_y, window_width, window_height;
dc6f92b8 11411
06a2c219 11412 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 11413
06a2c219
GM
11414 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11415 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11416 clip_rect.y = max (clip_rect.y, window_y);
11417 clip_rect.width = window_width;
11418 clip_rect.height = row->visible_height;
5c1aae96 11419
06a2c219
GM
11420 /* If clipping to the whole line, including trunc marks, extend
11421 the rectangle to the left and increase its width. */
11422 if (whole_line_p)
11423 {
3f332ef3
KS
11424 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11425 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
06a2c219 11426 }
5c1aae96 11427
06a2c219 11428 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
11429}
11430
06a2c219
GM
11431
11432/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
11433
11434static void
06a2c219
GM
11435x_draw_hollow_cursor (w, row)
11436 struct window *w;
11437 struct glyph_row *row;
dc6f92b8 11438{
06a2c219
GM
11439 struct frame *f = XFRAME (WINDOW_FRAME (w));
11440 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11441 Display *dpy = FRAME_X_DISPLAY (f);
11442 int x, y, wd, h;
11443 XGCValues xgcv;
11444 struct glyph *cursor_glyph;
11445 GC gc;
11446
11447 /* Compute frame-relative coordinates from window-relative
11448 coordinates. */
11449 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11450 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11451 + row->ascent - w->phys_cursor_ascent);
11452 h = row->height - 1;
11453
11454 /* Get the glyph the cursor is on. If we can't tell because
11455 the current matrix is invalid or such, give up. */
11456 cursor_glyph = get_phys_cursor_glyph (w);
11457 if (cursor_glyph == NULL)
dc6f92b8
JB
11458 return;
11459
06a2c219
GM
11460 /* Compute the width of the rectangle to draw. If on a stretch
11461 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11462 rectangle as wide as the glyph, but use a canonical character
11463 width instead. */
11464 wd = cursor_glyph->pixel_width - 1;
11465 if (cursor_glyph->type == STRETCH_GLYPH
11466 && !x_stretch_cursor_p)
11467 wd = min (CANON_X_UNIT (f), wd);
b3738089 11468 w->phys_cursor_width = wd;
06a2c219
GM
11469
11470 /* The foreground of cursor_gc is typically the same as the normal
11471 background color, which can cause the cursor box to be invisible. */
11472 xgcv.foreground = f->output_data.x->cursor_pixel;
11473 if (dpyinfo->scratch_cursor_gc)
11474 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11475 else
11476 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11477 GCForeground, &xgcv);
11478 gc = dpyinfo->scratch_cursor_gc;
11479
11480 /* Set clipping, draw the rectangle, and reset clipping again. */
11481 x_clip_to_row (w, row, gc, 0);
11482 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11483 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
11484}
11485
06a2c219
GM
11486
11487/* Draw a bar cursor on window W in glyph row ROW.
11488
11489 Implementation note: One would like to draw a bar cursor with an
11490 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11491 Unfortunately, I didn't find a font yet that has this property set.
11492 --gerd. */
dc6f92b8
JB
11493
11494static void
65c26775 11495x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
11496 struct window *w;
11497 struct glyph_row *row;
f02d8aa0 11498 int width;
65c26775 11499 enum text_cursor_kinds kind;
dc6f92b8 11500{
92f424df
GM
11501 struct frame *f = XFRAME (w->frame);
11502 struct glyph *cursor_glyph;
06a2c219 11503
92f424df
GM
11504 /* If cursor is out of bounds, don't draw garbage. This can happen
11505 in mini-buffer windows when switching between echo area glyphs
11506 and mini-buffer. */
11507 cursor_glyph = get_phys_cursor_glyph (w);
11508 if (cursor_glyph == NULL)
11509 return;
06a2c219 11510
92f424df
GM
11511 /* If on an image, draw like a normal cursor. That's usually better
11512 visible than drawing a bar, esp. if the image is large so that
11513 the bar might not be in the window. */
11514 if (cursor_glyph->type == IMAGE_GLYPH)
11515 {
11516 struct glyph_row *row;
11517 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11518 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11519 }
11520 else
11521 {
34e5d0af
GM
11522 Display *dpy = FRAME_X_DISPLAY (f);
11523 Window window = FRAME_X_WINDOW (f);
11524 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11525 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11526 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11527 XGCValues xgcv;
11528
11529 /* If the glyph's background equals the color we normally draw
11530 the bar cursor in, the bar cursor in its normal color is
11531 invisible. Use the glyph's foreground color instead in this
11532 case, on the assumption that the glyph's colors are chosen so
11533 that the glyph is legible. */
11534 if (face->background == f->output_data.x->cursor_pixel)
11535 xgcv.background = xgcv.foreground = face->foreground;
11536 else
11537 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 11538 xgcv.graphics_exposures = 0;
92f424df 11539
06a2c219
GM
11540 if (gc)
11541 XChangeGC (dpy, gc, mask, &xgcv);
11542 else
11543 {
11544 gc = XCreateGC (dpy, window, mask, &xgcv);
11545 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11546 }
92f424df 11547
f02d8aa0
GM
11548 if (width < 0)
11549 width = f->output_data.x->cursor_width;
34e5d0af 11550 width = min (cursor_glyph->pixel_width, width);
92f424df 11551
b3738089 11552 w->phys_cursor_width = width;
06a2c219 11553 x_clip_to_row (w, row, gc, 0);
b3738089 11554
65c26775
EZ
11555 if (kind == BAR_CURSOR)
11556 XFillRectangle (dpy, window, gc,
11557 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11558 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11559 width, row->height);
11560 else
11561 XFillRectangle (dpy, window, gc,
11562 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11563 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11564 row->height - width),
11565 cursor_glyph->pixel_width,
11566 width);
11567
06a2c219
GM
11568 XSetClipMask (dpy, gc, None);
11569 }
dc6f92b8
JB
11570}
11571
06a2c219
GM
11572
11573/* Clear the cursor of window W to background color, and mark the
11574 cursor as not shown. This is used when the text where the cursor
11575 is is about to be rewritten. */
11576
dc6f92b8 11577static void
06a2c219
GM
11578x_clear_cursor (w)
11579 struct window *w;
dc6f92b8 11580{
06a2c219
GM
11581 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11582 x_update_window_cursor (w, 0);
11583}
90e65f07 11584
dbc4e1c1 11585
06a2c219
GM
11586/* Draw the cursor glyph of window W in glyph row ROW. See the
11587 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 11588
06a2c219
GM
11589static void
11590x_draw_phys_cursor_glyph (w, row, hl)
11591 struct window *w;
11592 struct glyph_row *row;
11593 enum draw_glyphs_face hl;
11594{
11595 /* If cursor hpos is out of bounds, don't draw garbage. This can
11596 happen in mini-buffer windows when switching between echo area
11597 glyphs and mini-buffer. */
11598 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e 11599 {
f0a48a01 11600 int on_p = w->phys_cursor_on_p;
56a0382c 11601 int x1;
f0a48a01 11602
56a0382c
GM
11603 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11604 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11605 hl, 0);
f0a48a01 11606 w->phys_cursor_on_p = on_p;
66ac4b0e 11607
b3738089 11608 if (hl == DRAW_CURSOR)
56a0382c 11609 w->phys_cursor_width = x1 - w->phys_cursor.x;
b3738089 11610
66ac4b0e
GM
11611 /* When we erase the cursor, and ROW is overlapped by other
11612 rows, make sure that these overlapping parts of other rows
11613 are redrawn. */
b3738089 11614 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
66ac4b0e
GM
11615 {
11616 if (row > w->current_matrix->rows
11617 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11618 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11619
11620 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11621 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11622 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11623 }
11624 }
06a2c219 11625}
dbc4e1c1 11626
eea6af04 11627
06a2c219 11628/* Erase the image of a cursor of window W from the screen. */
eea6af04 11629
06a2c219
GM
11630static void
11631x_erase_phys_cursor (w)
11632 struct window *w;
11633{
11634 struct frame *f = XFRAME (w->frame);
11635 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11636 int hpos = w->phys_cursor.hpos;
11637 int vpos = w->phys_cursor.vpos;
11638 int mouse_face_here_p = 0;
11639 struct glyph_matrix *active_glyphs = w->current_matrix;
11640 struct glyph_row *cursor_row;
11641 struct glyph *cursor_glyph;
11642 enum draw_glyphs_face hl;
11643
11644 /* No cursor displayed or row invalidated => nothing to do on the
11645 screen. */
11646 if (w->phys_cursor_type == NO_CURSOR)
11647 goto mark_cursor_off;
11648
11649 /* VPOS >= active_glyphs->nrows means that window has been resized.
11650 Don't bother to erase the cursor. */
11651 if (vpos >= active_glyphs->nrows)
11652 goto mark_cursor_off;
11653
11654 /* If row containing cursor is marked invalid, there is nothing we
11655 can do. */
11656 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11657 if (!cursor_row->enabled_p)
11658 goto mark_cursor_off;
11659
a2c6de8e
KS
11660 /* If row is completely invisible, don't attempt to delete a cursor which
11661 isn't there. This can happen if cursor is at top of a window, and
11662 we switch to a buffer with a header line in that window. */
11663 if (cursor_row->visible_height <= 0)
11664 goto mark_cursor_off;
11665
06a2c219
GM
11666 /* This can happen when the new row is shorter than the old one.
11667 In this case, either x_draw_glyphs or clear_end_of_line
11668 should have cleared the cursor. Note that we wouldn't be
11669 able to erase the cursor in this case because we don't have a
11670 cursor glyph at hand. */
11671 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11672 goto mark_cursor_off;
11673
11674 /* If the cursor is in the mouse face area, redisplay that when
11675 we clear the cursor. */
8801a864
KR
11676 if (! NILP (dpyinfo->mouse_face_window)
11677 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
11678 && (vpos > dpyinfo->mouse_face_beg_row
11679 || (vpos == dpyinfo->mouse_face_beg_row
11680 && hpos >= dpyinfo->mouse_face_beg_col))
11681 && (vpos < dpyinfo->mouse_face_end_row
11682 || (vpos == dpyinfo->mouse_face_end_row
11683 && hpos < dpyinfo->mouse_face_end_col))
11684 /* Don't redraw the cursor's spot in mouse face if it is at the
11685 end of a line (on a newline). The cursor appears there, but
11686 mouse highlighting does not. */
11687 && cursor_row->used[TEXT_AREA] > hpos)
11688 mouse_face_here_p = 1;
11689
11690 /* Maybe clear the display under the cursor. */
11691 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11692 {
11693 int x;
045dee35 11694 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 11695
06a2c219
GM
11696 cursor_glyph = get_phys_cursor_glyph (w);
11697 if (cursor_glyph == NULL)
11698 goto mark_cursor_off;
dbc4e1c1 11699
e0300d33 11700 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
06a2c219 11701
c5e6e06b
GM
11702 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11703 x,
11704 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11705 cursor_row->y)),
11706 cursor_glyph->pixel_width,
11707 cursor_row->visible_height,
11708 False);
dbc4e1c1 11709 }
06a2c219
GM
11710
11711 /* Erase the cursor by redrawing the character underneath it. */
11712 if (mouse_face_here_p)
11713 hl = DRAW_MOUSE_FACE;
06a2c219
GM
11714 else
11715 hl = DRAW_NORMAL_TEXT;
11716 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 11717
06a2c219
GM
11718 mark_cursor_off:
11719 w->phys_cursor_on_p = 0;
11720 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
11721}
11722
11723
b7f83f9e
GM
11724/* Non-zero if physical cursor of window W is within mouse face. */
11725
11726static int
11727cursor_in_mouse_face_p (w)
11728 struct window *w;
11729{
11730 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11731 int in_mouse_face = 0;
11732
11733 if (WINDOWP (dpyinfo->mouse_face_window)
11734 && XWINDOW (dpyinfo->mouse_face_window) == w)
11735 {
11736 int hpos = w->phys_cursor.hpos;
11737 int vpos = w->phys_cursor.vpos;
11738
11739 if (vpos >= dpyinfo->mouse_face_beg_row
11740 && vpos <= dpyinfo->mouse_face_end_row
11741 && (vpos > dpyinfo->mouse_face_beg_row
11742 || hpos >= dpyinfo->mouse_face_beg_col)
11743 && (vpos < dpyinfo->mouse_face_end_row
11744 || hpos < dpyinfo->mouse_face_end_col
11745 || dpyinfo->mouse_face_past_end))
11746 in_mouse_face = 1;
11747 }
11748
11749 return in_mouse_face;
11750}
11751
11752
06a2c219
GM
11753/* Display or clear cursor of window W. If ON is zero, clear the
11754 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11755 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 11756
06a2c219
GM
11757void
11758x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11759 struct window *w;
11760 int on, hpos, vpos, x, y;
dbc4e1c1 11761{
06a2c219
GM
11762 struct frame *f = XFRAME (w->frame);
11763 int new_cursor_type;
f02d8aa0 11764 int new_cursor_width;
f7e039de 11765 int cursor_off_state = 0;
06a2c219
GM
11766 struct glyph_matrix *current_glyphs;
11767 struct glyph_row *glyph_row;
11768 struct glyph *glyph;
dbc4e1c1 11769
49d838ea 11770 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
11771 windows and frames; in the latter case, the frame or window may
11772 be in the midst of changing its size, and x and y may be off the
11773 window. */
11774 if (! FRAME_VISIBLE_P (f)
11775 || FRAME_GARBAGED_P (f)
11776 || vpos >= w->current_matrix->nrows
11777 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
11778 return;
11779
11780 /* If cursor is off and we want it off, return quickly. */
06a2c219 11781 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
11782 return;
11783
06a2c219
GM
11784 current_glyphs = w->current_matrix;
11785 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11786 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11787
11788 /* If cursor row is not enabled, we don't really know where to
11789 display the cursor. */
11790 if (!glyph_row->enabled_p)
11791 {
11792 w->phys_cursor_on_p = 0;
11793 return;
11794 }
11795
11796 xassert (interrupt_input_blocked);
11797
11798 /* Set new_cursor_type to the cursor we want to be displayed. In a
11799 mini-buffer window, we want the cursor only to appear if we are
11800 reading input from this window. For the selected window, we want
11801 the cursor type given by the frame parameter. If explicitly
11802 marked off, draw no cursor. In all other cases, we want a hollow
11803 box cursor. */
f02d8aa0 11804 new_cursor_width = -1;
f7e039de
RS
11805 new_cursor_type = -2;
11806
11807 /* Echo area */
9b4a7047
GM
11808 if (cursor_in_echo_area
11809 && FRAME_HAS_MINIBUF_P (f)
11810 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 11811 {
9b4a7047
GM
11812 if (w == XWINDOW (echo_area_window))
11813 new_cursor_type = FRAME_DESIRED_CURSOR (f);
f7e039de
RS
11814 else if (NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11815 w->buffer)))
11816 new_cursor_type = NO_CURSOR;
9a7bdceb 11817 else
f7e039de
RS
11818 cursor_off_state = 1;
11819 }
11820
11821 /* Nonselected window or nonselected frame. */
11822 else if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11823 || w != XWINDOW (f->selected_window))
11824 {
11825 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11826 || NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11827 w->buffer))
11828 || NILP (XBUFFER (w->buffer)->cursor_type))
9a7bdceb 11829 new_cursor_type = NO_CURSOR;
f7e039de
RS
11830 else
11831 cursor_off_state = 1;
06a2c219 11832 }
f7e039de
RS
11833
11834 /* If new_cursor_type isn't decided yet, decide it now. */
11835 if (new_cursor_type == -2)
9b4a7047 11836 {
f7e039de
RS
11837 struct buffer *b = XBUFFER (w->buffer);
11838
11839 if (EQ (b->cursor_type, Qt))
11840 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9b4a7047 11841 else
f7e039de
RS
11842 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11843 &new_cursor_width);
11844 }
f02d8aa0 11845
f7e039de
RS
11846 /* Dim out or hollow out the cursor,
11847 if it has blinked off or for nonselected windows. */
11848 if (w->cursor_off_p || cursor_off_state)
11849 {
11850 if (new_cursor_type == FILLED_BOX_CURSOR)
11851 new_cursor_type = HOLLOW_BOX_CURSOR;
11852 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
11853 new_cursor_width = 1;
11854 else
11855 new_cursor_type = NO_CURSOR;
9b4a7047 11856 }
06a2c219 11857
f7e039de
RS
11858 /* Now new_cursor_type is correct. */
11859
06a2c219
GM
11860 /* If cursor is currently being shown and we don't want it to be or
11861 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 11862 erase it. */
06a2c219 11863 if (w->phys_cursor_on_p
dc6f92b8 11864 && (!on
06a2c219
GM
11865 || w->phys_cursor.x != x
11866 || w->phys_cursor.y != y
e1429afe
KS
11867 || new_cursor_type != w->phys_cursor_type
11868 || (new_cursor_type == BAR_CURSOR
11869 && new_cursor_width != w->phys_cursor_width)))
06a2c219
GM
11870 x_erase_phys_cursor (w);
11871
56a0382c
GM
11872 /* Don't check phys_cursor_on_p here because that flag is only set
11873 to zero in some cases where we know that the cursor has been
11874 completely erased, to avoid the extra work of erasing the cursor
11875 twice. In other words, phys_cursor_on_p can be 1 and the cursor
11876 still not be visible, or it has only been partly erased. */
11877 if (on)
06a2c219
GM
11878 {
11879 w->phys_cursor_ascent = glyph_row->ascent;
11880 w->phys_cursor_height = glyph_row->height;
11881
11882 /* Set phys_cursor_.* before x_draw_.* is called because some
11883 of them may need the information. */
11884 w->phys_cursor.x = x;
11885 w->phys_cursor.y = glyph_row->y;
11886 w->phys_cursor.hpos = hpos;
11887 w->phys_cursor.vpos = vpos;
11888 w->phys_cursor_type = new_cursor_type;
11889 w->phys_cursor_on_p = 1;
11890
11891 switch (new_cursor_type)
dc6f92b8 11892 {
06a2c219
GM
11893 case HOLLOW_BOX_CURSOR:
11894 x_draw_hollow_cursor (w, glyph_row);
11895 break;
11896
11897 case FILLED_BOX_CURSOR:
11898 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11899 break;
11900
11901 case BAR_CURSOR:
65c26775
EZ
11902 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
11903 break;
11904
11905 case HBAR_CURSOR:
b3738089 11906 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
06a2c219
GM
11907 break;
11908
11909 case NO_CURSOR:
4398e673 11910 w->phys_cursor_width = 0;
06a2c219 11911 break;
dc6f92b8 11912
06a2c219
GM
11913 default:
11914 abort ();
11915 }
59ddecde
GM
11916
11917#ifdef HAVE_X_I18N
11918 if (w == XWINDOW (f->selected_window))
11919 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11920 xic_set_preeditarea (w, x, y);
11921#endif
dc6f92b8
JB
11922 }
11923
06a2c219 11924#ifndef XFlush
f676886a 11925 if (updating_frame != f)
334208b7 11926 XFlush (FRAME_X_DISPLAY (f));
06a2c219 11927#endif
dc6f92b8
JB
11928}
11929
06a2c219
GM
11930
11931/* Display the cursor on window W, or clear it. X and Y are window
11932 relative pixel coordinates. HPOS and VPOS are glyph matrix
11933 positions. If W is not the selected window, display a hollow
11934 cursor. ON non-zero means display the cursor at X, Y which
11935 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11936
dfcf069d 11937void
06a2c219
GM
11938x_display_cursor (w, on, hpos, vpos, x, y)
11939 struct window *w;
11940 int on, hpos, vpos, x, y;
dc6f92b8 11941{
f94397b5 11942 BLOCK_INPUT;
06a2c219 11943 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11944 UNBLOCK_INPUT;
11945}
11946
06a2c219
GM
11947
11948/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11949 Don't change the cursor's position. */
11950
dfcf069d 11951void
06a2c219 11952x_update_cursor (f, on_p)
5d46f928 11953 struct frame *f;
5d46f928 11954{
06a2c219
GM
11955 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11956}
11957
11958
11959/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11960 in the window tree rooted at W. */
11961
11962static void
11963x_update_cursor_in_window_tree (w, on_p)
11964 struct window *w;
11965 int on_p;
11966{
11967 while (w)
11968 {
11969 if (!NILP (w->hchild))
11970 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11971 else if (!NILP (w->vchild))
11972 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11973 else
11974 x_update_window_cursor (w, on_p);
11975
11976 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11977 }
11978}
5d46f928 11979
f94397b5 11980
06a2c219
GM
11981/* Switch the display of W's cursor on or off, according to the value
11982 of ON. */
11983
11984static void
11985x_update_window_cursor (w, on)
11986 struct window *w;
11987 int on;
11988{
16b5d424
GM
11989 /* Don't update cursor in windows whose frame is in the process
11990 of being deleted. */
11991 if (w->current_matrix)
11992 {
11993 BLOCK_INPUT;
11994 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11995 w->phys_cursor.x, w->phys_cursor.y);
11996 UNBLOCK_INPUT;
11997 }
dc6f92b8 11998}
06a2c219
GM
11999
12000
12001
dc6f92b8
JB
12002\f
12003/* Icons. */
12004
dbc4e1c1 12005/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
12006
12007int
990ba854 12008x_bitmap_icon (f, file)
f676886a 12009 struct frame *f;
990ba854 12010 Lisp_Object file;
dc6f92b8 12011{
06a2c219 12012 int bitmap_id;
dc6f92b8 12013
c118dd06 12014 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
12015 return 1;
12016
990ba854 12017 /* Free up our existing icon bitmap if any. */
7556890b
RS
12018 if (f->output_data.x->icon_bitmap > 0)
12019 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12020 f->output_data.x->icon_bitmap = 0;
990ba854
RS
12021
12022 if (STRINGP (file))
7f2ae036
RS
12023 bitmap_id = x_create_bitmap_from_file (f, file);
12024 else
12025 {
990ba854 12026 /* Create the GNU bitmap if necessary. */
5bf01b68 12027 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
12028 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
12029 = x_create_bitmap_from_data (f, gnu_bits,
12030 gnu_width, gnu_height);
990ba854
RS
12031
12032 /* The first time we create the GNU bitmap,
06a2c219 12033 this increments the ref-count one extra time.
990ba854
RS
12034 As a result, the GNU bitmap is never freed.
12035 That way, we don't have to worry about allocating it again. */
334208b7 12036 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 12037
334208b7 12038 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
12039 }
12040
12041 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 12042 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
12043
12044 return 0;
12045}
12046
12047
1be2d067
KH
12048/* Make the x-window of frame F use a rectangle with text.
12049 Use ICON_NAME as the text. */
dc6f92b8
JB
12050
12051int
f676886a
JB
12052x_text_icon (f, icon_name)
12053 struct frame *f;
dc6f92b8
JB
12054 char *icon_name;
12055{
c118dd06 12056 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
12057 return 1;
12058
1be2d067
KH
12059#ifdef HAVE_X11R4
12060 {
12061 XTextProperty text;
12062 text.value = (unsigned char *) icon_name;
12063 text.encoding = XA_STRING;
12064 text.format = 8;
12065 text.nitems = strlen (icon_name);
12066#ifdef USE_X_TOOLKIT
7556890b 12067 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
12068 &text);
12069#else /* not USE_X_TOOLKIT */
12070 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
12071#endif /* not USE_X_TOOLKIT */
12072 }
12073#else /* not HAVE_X11R4 */
12074 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
12075#endif /* not HAVE_X11R4 */
58769bee 12076
7556890b
RS
12077 if (f->output_data.x->icon_bitmap > 0)
12078 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12079 f->output_data.x->icon_bitmap = 0;
b1c884c3 12080 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
12081
12082 return 0;
12083}
12084\f
e99db5a1
RS
12085#define X_ERROR_MESSAGE_SIZE 200
12086
12087/* If non-nil, this should be a string.
12088 It means catch X errors and store the error message in this string. */
12089
12090static Lisp_Object x_error_message_string;
12091
12092/* An X error handler which stores the error message in
12093 x_error_message_string. This is called from x_error_handler if
12094 x_catch_errors is in effect. */
12095
06a2c219 12096static void
e99db5a1
RS
12097x_error_catcher (display, error)
12098 Display *display;
12099 XErrorEvent *error;
12100{
12101 XGetErrorText (display, error->error_code,
d5db4077 12102 SDATA (x_error_message_string),
e99db5a1
RS
12103 X_ERROR_MESSAGE_SIZE);
12104}
12105
12106/* Begin trapping X errors for display DPY. Actually we trap X errors
12107 for all displays, but DPY should be the display you are actually
12108 operating on.
12109
12110 After calling this function, X protocol errors no longer cause
12111 Emacs to exit; instead, they are recorded in the string
12112 stored in x_error_message_string.
12113
12114 Calling x_check_errors signals an Emacs error if an X error has
12115 occurred since the last call to x_catch_errors or x_check_errors.
12116
12117 Calling x_uncatch_errors resumes the normal error handling. */
12118
12119void x_check_errors ();
12120static Lisp_Object x_catch_errors_unwind ();
12121
12122int
12123x_catch_errors (dpy)
12124 Display *dpy;
12125{
aed13378 12126 int count = SPECPDL_INDEX ();
e99db5a1
RS
12127
12128 /* Make sure any errors from previous requests have been dealt with. */
12129 XSync (dpy, False);
12130
12131 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
12132
12133 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7da167cd 12134 SSET (x_error_message_string, 0, 0);
e99db5a1
RS
12135
12136 return count;
12137}
12138
12139/* Unbind the binding that we made to check for X errors. */
12140
12141static Lisp_Object
12142x_catch_errors_unwind (old_val)
12143 Lisp_Object old_val;
12144{
12145 x_error_message_string = old_val;
12146 return Qnil;
12147}
12148
12149/* If any X protocol errors have arrived since the last call to
12150 x_catch_errors or x_check_errors, signal an Emacs error using
12151 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12152
12153void
12154x_check_errors (dpy, format)
12155 Display *dpy;
12156 char *format;
12157{
12158 /* Make sure to catch any errors incurred so far. */
12159 XSync (dpy, False);
12160
d5db4077
KR
12161 if (SREF (x_error_message_string, 0))
12162 error (format, SDATA (x_error_message_string));
e99db5a1
RS
12163}
12164
9829ddba
RS
12165/* Nonzero if we had any X protocol errors
12166 since we did x_catch_errors on DPY. */
e99db5a1
RS
12167
12168int
12169x_had_errors_p (dpy)
12170 Display *dpy;
12171{
12172 /* Make sure to catch any errors incurred so far. */
12173 XSync (dpy, False);
12174
d5db4077 12175 return SREF (x_error_message_string, 0) != 0;
e99db5a1
RS
12176}
12177
9829ddba
RS
12178/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12179
06a2c219 12180void
9829ddba
RS
12181x_clear_errors (dpy)
12182 Display *dpy;
12183{
7da167cd 12184 SSET (x_error_message_string, 0, 0);
9829ddba
RS
12185}
12186
e99db5a1
RS
12187/* Stop catching X protocol errors and let them make Emacs die.
12188 DPY should be the display that was passed to x_catch_errors.
12189 COUNT should be the value that was returned by
12190 the corresponding call to x_catch_errors. */
12191
12192void
12193x_uncatch_errors (dpy, count)
12194 Display *dpy;
12195 int count;
12196{
12197 unbind_to (count, Qnil);
12198}
12199
12200#if 0
12201static unsigned int x_wire_count;
12202x_trace_wire ()
12203{
12204 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12205}
12206#endif /* ! 0 */
12207
12208\f
12209/* Handle SIGPIPE, which can happen when the connection to a server
12210 simply goes away. SIGPIPE is handled by x_connection_signal.
12211 Don't need to do anything, because the write which caused the
12212 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 12213 which will do the appropriate cleanup for us. */
e99db5a1
RS
12214
12215static SIGTYPE
12216x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 12217 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
12218{
12219#ifdef USG
12220 /* USG systems forget handlers when they are used;
12221 must reestablish each time */
12222 signal (signalnum, x_connection_signal);
12223#endif /* USG */
12224}
0da1ab50 12225
e99db5a1 12226\f
0da1ab50
GM
12227/************************************************************************
12228 Handling X errors
12229 ************************************************************************/
4746118a 12230
f0e299de
GM
12231/* Error message passed to x_connection_closed. */
12232
12233static char *error_msg;
12234
a7248e4f 12235/* Function installed as fatal_error_signal_hook in
f0e299de
GM
12236 x_connection_closed. Print the X error message, and exit normally,
12237 instead of dumping core when XtCloseDisplay fails. */
12238
12239static void
12240x_fatal_error_signal ()
12241{
12242 fprintf (stderr, "%s\n", error_msg);
12243 exit (70);
12244}
12245
0da1ab50
GM
12246/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12247 the text of an error message that lead to the connection loss. */
16bd92ea 12248
4746118a 12249static SIGTYPE
5978125e
GM
12250x_connection_closed (dpy, error_message)
12251 Display *dpy;
7a13e894 12252 char *error_message;
4746118a 12253{
5978125e 12254 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 12255 Lisp_Object frame, tail;
0da1ab50 12256 int count;
0da1ab50 12257
f0e299de
GM
12258 error_msg = (char *) alloca (strlen (error_message) + 1);
12259 strcpy (error_msg, error_message);
1a532e54
GM
12260 handling_signal = 0;
12261
0da1ab50
GM
12262 /* Prevent being called recursively because of an error condition
12263 below. Otherwise, we might end up with printing ``can't find per
12264 display information'' in the recursive call instead of printing
12265 the original message here. */
12266 count = x_catch_errors (dpy);
12267
8a4f36cc
GM
12268 /* We have to close the display to inform Xt that it doesn't
12269 exist anymore. If we don't, Xt will continue to wait for
12270 events from the display. As a consequence, a sequence of
12271
12272 M-x make-frame-on-display RET :1 RET
12273 ...kill the new frame, so that we get an IO error...
12274 M-x make-frame-on-display RET :1 RET
12275
12276 will indefinitely wait in Xt for events for display `:1', opened
12277 in the first class to make-frame-on-display.
6186a4a0 12278
8a4f36cc
GM
12279 Closing the display is reported to lead to a bus error on
12280 OpenWindows in certain situations. I suspect that is a bug
12281 in OpenWindows. I don't know how to cicumvent it here. */
12282
f613a4c8 12283#ifdef USE_X_TOOLKIT
ae24cb3b
GM
12284 /* If DPYINFO is null, this means we didn't open the display
12285 in the first place, so don't try to close it. */
12286 if (dpyinfo)
f0e299de
GM
12287 {
12288 extern void (*fatal_error_signal_hook) P_ ((void));
12289 fatal_error_signal_hook = x_fatal_error_signal;
12290 XtCloseDisplay (dpy);
12291 fatal_error_signal_hook = NULL;
12292 }
f613a4c8 12293#endif
adabc3a9 12294
8a4f36cc 12295 /* Indicate that this display is dead. */
9e80b57d
KR
12296 if (dpyinfo)
12297 dpyinfo->display = 0;
6186a4a0 12298
06a2c219 12299 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
12300 that are on the dead display. */
12301 FOR_EACH_FRAME (tail, frame)
12302 {
12303 Lisp_Object minibuf_frame;
12304 minibuf_frame
12305 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
12306 if (FRAME_X_P (XFRAME (frame))
12307 && FRAME_X_P (XFRAME (minibuf_frame))
12308 && ! EQ (frame, minibuf_frame)
12309 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
12310 Fdelete_frame (frame, Qt);
12311 }
12312
12313 /* Now delete all remaining frames on the dead display.
06a2c219 12314 We are now sure none of these is used as the mini-buffer
7a13e894
RS
12315 for another frame that we need to delete. */
12316 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
12317 if (FRAME_X_P (XFRAME (frame))
12318 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
12319 {
12320 /* Set this to t so that Fdelete_frame won't get confused
12321 trying to find a replacement. */
12322 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12323 Fdelete_frame (frame, Qt);
12324 }
7a13e894 12325
482a1bd2
KH
12326 if (dpyinfo)
12327 x_delete_display (dpyinfo);
7a13e894 12328
0da1ab50
GM
12329 x_uncatch_errors (dpy, count);
12330
7a13e894
RS
12331 if (x_display_list == 0)
12332 {
f0e299de 12333 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
12334 shut_down_emacs (0, 0, Qnil);
12335 exit (70);
12336 }
12ba150f 12337
7a13e894
RS
12338 /* Ordinary stack unwind doesn't deal with these. */
12339#ifdef SIGIO
12340 sigunblock (sigmask (SIGIO));
12341#endif
12342 sigunblock (sigmask (SIGALRM));
12343 TOTALLY_UNBLOCK_INPUT;
12344
aa4d9a9e 12345 clear_waiting_for_input ();
f0e299de 12346 error ("%s", error_msg);
4746118a
JB
12347}
12348
0da1ab50 12349
7a13e894
RS
12350/* This is the usual handler for X protocol errors.
12351 It kills all frames on the display that we got the error for.
12352 If that was the only one, it prints an error message and kills Emacs. */
12353
06a2c219 12354static void
c118dd06
JB
12355x_error_quitter (display, error)
12356 Display *display;
12357 XErrorEvent *error;
12358{
7a13e894 12359 char buf[256], buf1[356];
dc6f92b8 12360
58769bee 12361 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12362 original error handler. */
dc6f92b8 12363
c118dd06 12364 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 12365 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 12366 buf, error->request_code);
7a13e894 12367 x_connection_closed (display, buf1);
dc6f92b8
JB
12368}
12369
0da1ab50 12370
e99db5a1
RS
12371/* This is the first-level handler for X protocol errors.
12372 It calls x_error_quitter or x_error_catcher. */
7a13e894 12373
8922af5f 12374static int
e99db5a1 12375x_error_handler (display, error)
8922af5f 12376 Display *display;
e99db5a1 12377 XErrorEvent *error;
8922af5f 12378{
e99db5a1
RS
12379 if (! NILP (x_error_message_string))
12380 x_error_catcher (display, error);
12381 else
12382 x_error_quitter (display, error);
06a2c219 12383 return 0;
f9e24cb9 12384}
c118dd06 12385
e99db5a1
RS
12386/* This is the handler for X IO errors, always.
12387 It kills all frames on the display that we lost touch with.
12388 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 12389
c118dd06 12390static int
e99db5a1 12391x_io_error_quitter (display)
c118dd06 12392 Display *display;
c118dd06 12393{
e99db5a1 12394 char buf[256];
dc6f92b8 12395
e99db5a1
RS
12396 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12397 x_connection_closed (display, buf);
06a2c219 12398 return 0;
dc6f92b8 12399}
dc6f92b8 12400\f
f451eb13
JB
12401/* Changing the font of the frame. */
12402
76bcdf39
RS
12403/* Give frame F the font named FONTNAME as its default font, and
12404 return the full name of that font. FONTNAME may be a wildcard
12405 pattern; in that case, we choose some font that fits the pattern.
12406 The return value shows which font we chose. */
12407
b5cf7a0e 12408Lisp_Object
f676886a
JB
12409x_new_font (f, fontname)
12410 struct frame *f;
dc6f92b8
JB
12411 register char *fontname;
12412{
dc43ef94 12413 struct font_info *fontp
ee569018 12414 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 12415
dc43ef94
KH
12416 if (!fontp)
12417 return Qnil;
2224a5fc 12418
dc43ef94 12419 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 12420 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94 12421 f->output_data.x->fontset = -1;
976b73d7
KS
12422
12423 x_compute_fringe_widths (f, 1);
12424
b2cad826
KH
12425 /* Compute the scroll bar width in character columns. */
12426 if (f->scroll_bar_pixel_width > 0)
12427 {
7556890b 12428 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
12429 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12430 }
12431 else
4e61bddf
RS
12432 {
12433 int wid = FONT_WIDTH (f->output_data.x->font);
12434 f->scroll_bar_cols = (14 + wid - 1) / wid;
12435 }
b2cad826 12436
f676886a 12437 /* Now make the frame display the given font. */
c118dd06 12438 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 12439 {
7556890b
RS
12440 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12441 f->output_data.x->font->fid);
12442 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12443 f->output_data.x->font->fid);
12444 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12445 f->output_data.x->font->fid);
f676886a 12446
a27f9f86 12447 frame_update_line_height (f);
3497f73e
GM
12448
12449 /* Don't change the size of a tip frame; there's no point in
12450 doing it because it's done in Fx_show_tip, and it leads to
12451 problems because the tip frame has no widget. */
12452 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12453 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 12454 }
a27f9f86
RS
12455 else
12456 /* If we are setting a new frame's font for the first time,
12457 there are no faces yet, so this font's height is the line height. */
7556890b 12458 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 12459
dc43ef94
KH
12460 return build_string (fontp->full_name);
12461}
12462
12463/* Give frame F the fontset named FONTSETNAME as its default font, and
12464 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
12465 pattern; in that case, we choose some fontset that fits the pattern.
12466 The return value shows which fontset we chose. */
b5cf7a0e 12467
dc43ef94
KH
12468Lisp_Object
12469x_new_fontset (f, fontsetname)
12470 struct frame *f;
12471 char *fontsetname;
12472{
ee569018 12473 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 12474 Lisp_Object result;
b5cf7a0e 12475
dc43ef94
KH
12476 if (fontset < 0)
12477 return Qnil;
b5cf7a0e 12478
2da424f1
KH
12479 if (f->output_data.x->fontset == fontset)
12480 /* This fontset is already set in frame F. There's nothing more
12481 to do. */
ee569018 12482 return fontset_name (fontset);
dc43ef94 12483
d5db4077 12484 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
dc43ef94
KH
12485
12486 if (!STRINGP (result))
12487 /* Can't load ASCII font. */
12488 return Qnil;
12489
12490 /* Since x_new_font doesn't update any fontset information, do it now. */
12491 f->output_data.x->fontset = fontset;
dc43ef94 12492
f5d11644
GM
12493#ifdef HAVE_X_I18N
12494 if (FRAME_XIC (f)
12495 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
d5db4077 12496 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
f5d11644
GM
12497#endif
12498
dc43ef94 12499 return build_string (fontsetname);
dc6f92b8 12500}
f5d11644 12501
976b73d7
KS
12502/* Compute actual fringe widths */
12503
12504void
12505x_compute_fringe_widths (f, redraw)
12506 struct frame *f;
12507 int redraw;
12508{
12509 int o_left = f->output_data.x->left_fringe_width;
12510 int o_right = f->output_data.x->right_fringe_width;
12511 int o_cols = f->output_data.x->fringe_cols;
12512
12513 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12514 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12515 int left_fringe_width, right_fringe_width;
12516
12517 if (!NILP (left_fringe))
12518 left_fringe = Fcdr (left_fringe);
12519 if (!NILP (right_fringe))
12520 right_fringe = Fcdr (right_fringe);
12521
12522 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12523 XINT (left_fringe));
12524 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12525 XINT (right_fringe));
12526
12527 if (left_fringe_width || right_fringe_width)
12528 {
12529 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12530 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12531 int conf_wid = left_wid + right_wid;
12532 int font_wid = FONT_WIDTH (f->output_data.x->font);
12533 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12534 int real_wid = cols * font_wid;
12535 if (left_wid && right_wid)
12536 {
12537 if (left_fringe_width < 0)
12538 {
12539 /* Left fringe width is fixed, adjust right fringe if necessary */
12540 f->output_data.x->left_fringe_width = left_wid;
12541 f->output_data.x->right_fringe_width = real_wid - left_wid;
12542 }
12543 else if (right_fringe_width < 0)
12544 {
12545 /* Right fringe width is fixed, adjust left fringe if necessary */
12546 f->output_data.x->left_fringe_width = real_wid - right_wid;
12547 f->output_data.x->right_fringe_width = right_wid;
12548 }
12549 else
12550 {
12551 /* Adjust both fringes with an equal amount.
12552 Note that we are doing integer arithmetic here, so don't
12553 lose a pixel if the total width is an odd number. */
12554 int fill = real_wid - conf_wid;
12555 f->output_data.x->left_fringe_width = left_wid + fill/2;
12556 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12557 }
12558 }
12559 else if (left_fringe_width)
12560 {
12561 f->output_data.x->left_fringe_width = real_wid;
12562 f->output_data.x->right_fringe_width = 0;
12563 }
12564 else
12565 {
12566 f->output_data.x->left_fringe_width = 0;
12567 f->output_data.x->right_fringe_width = real_wid;
12568 }
12569 f->output_data.x->fringe_cols = cols;
12570 f->output_data.x->fringes_extra = real_wid;
12571 }
12572 else
12573 {
12574 f->output_data.x->left_fringe_width = 0;
12575 f->output_data.x->right_fringe_width = 0;
12576 f->output_data.x->fringe_cols = 0;
12577 f->output_data.x->fringes_extra = 0;
12578 }
12579
12580 if (redraw && FRAME_VISIBLE_P (f))
12581 if (o_left != f->output_data.x->left_fringe_width ||
12582 o_right != f->output_data.x->right_fringe_width ||
12583 o_cols != f->output_data.x->fringe_cols)
12584 redraw_frame (f);
12585}
f5d11644
GM
12586\f
12587/***********************************************************************
12588 X Input Methods
12589 ***********************************************************************/
12590
12591#ifdef HAVE_X_I18N
12592
12593#ifdef HAVE_X11R6
12594
12595/* XIM destroy callback function, which is called whenever the
12596 connection to input method XIM dies. CLIENT_DATA contains a
12597 pointer to the x_display_info structure corresponding to XIM. */
12598
12599static void
12600xim_destroy_callback (xim, client_data, call_data)
12601 XIM xim;
12602 XPointer client_data;
12603 XPointer call_data;
12604{
12605 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12606 Lisp_Object frame, tail;
12607
12608 BLOCK_INPUT;
12609
12610 /* No need to call XDestroyIC.. */
12611 FOR_EACH_FRAME (tail, frame)
12612 {
12613 struct frame *f = XFRAME (frame);
12614 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12615 {
12616 FRAME_XIC (f) = NULL;
12617 if (FRAME_XIC_FONTSET (f))
12618 {
12619 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12620 FRAME_XIC_FONTSET (f) = NULL;
12621 }
12622 }
12623 }
12624
12625 /* No need to call XCloseIM. */
12626 dpyinfo->xim = NULL;
12627 XFree (dpyinfo->xim_styles);
12628 UNBLOCK_INPUT;
12629}
12630
12631#endif /* HAVE_X11R6 */
12632
12633/* Open the connection to the XIM server on display DPYINFO.
12634 RESOURCE_NAME is the resource name Emacs uses. */
12635
12636static void
12637xim_open_dpy (dpyinfo, resource_name)
12638 struct x_display_info *dpyinfo;
12639 char *resource_name;
12640{
287f7dd6 12641#ifdef USE_XIM
f5d11644
GM
12642 XIM xim;
12643
12644 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12645 dpyinfo->xim = xim;
12646
12647 if (xim)
12648 {
f5d11644
GM
12649#ifdef HAVE_X11R6
12650 XIMCallback destroy;
12651#endif
12652
12653 /* Get supported styles and XIM values. */
12654 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12655
12656#ifdef HAVE_X11R6
12657 destroy.callback = xim_destroy_callback;
12658 destroy.client_data = (XPointer)dpyinfo;
cea2ad76 12659 /* This isn't prototyped in OSF 5.0. */
f5d11644
GM
12660 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12661#endif
12662 }
287f7dd6
GM
12663
12664#else /* not USE_XIM */
12665 dpyinfo->xim = NULL;
12666#endif /* not USE_XIM */
f5d11644
GM
12667}
12668
12669
b9de836c 12670#ifdef HAVE_X11R6_XIM
f5d11644
GM
12671
12672struct xim_inst_t
12673{
12674 struct x_display_info *dpyinfo;
12675 char *resource_name;
12676};
12677
12678/* XIM instantiate callback function, which is called whenever an XIM
12679 server is available. DISPLAY is teh display of the XIM.
12680 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12681 when the callback was registered. */
12682
12683static void
12684xim_instantiate_callback (display, client_data, call_data)
12685 Display *display;
12686 XPointer client_data;
12687 XPointer call_data;
12688{
12689 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12690 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12691
12692 /* We don't support multiple XIM connections. */
12693 if (dpyinfo->xim)
12694 return;
12695
12696 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12697
12698 /* Create XIC for the existing frames on the same display, as long
12699 as they have no XIC. */
12700 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12701 {
12702 Lisp_Object tail, frame;
12703
12704 BLOCK_INPUT;
12705 FOR_EACH_FRAME (tail, frame)
12706 {
12707 struct frame *f = XFRAME (frame);
12708
12709 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12710 if (FRAME_XIC (f) == NULL)
12711 {
12712 create_frame_xic (f);
12713 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12714 xic_set_statusarea (f);
12715 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12716 {
12717 struct window *w = XWINDOW (f->selected_window);
12718 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12719 }
12720 }
12721 }
12722
12723 UNBLOCK_INPUT;
12724 }
12725}
12726
b9de836c 12727#endif /* HAVE_X11R6_XIM */
f5d11644
GM
12728
12729
12730/* Open a connection to the XIM server on display DPYINFO.
12731 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12732 connection only at the first time. On X11R6, open the connection
12733 in the XIM instantiate callback function. */
12734
12735static void
12736xim_initialize (dpyinfo, resource_name)
12737 struct x_display_info *dpyinfo;
12738 char *resource_name;
12739{
287f7dd6 12740#ifdef USE_XIM
b9de836c 12741#ifdef HAVE_X11R6_XIM
f5d11644
GM
12742 struct xim_inst_t *xim_inst;
12743 int len;
12744
12745 dpyinfo->xim = NULL;
12746 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12747 xim_inst->dpyinfo = dpyinfo;
12748 len = strlen (resource_name);
12749 xim_inst->resource_name = (char *) xmalloc (len + 1);
12750 bcopy (resource_name, xim_inst->resource_name, len + 1);
12751 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12752 resource_name, EMACS_CLASS,
12753 xim_instantiate_callback,
2ebb2f8b
DL
12754 /* Fixme: This is XPointer in
12755 XFree86 but (XPointer *) on
12756 Tru64, at least. */
12757 (XPointer) xim_inst);
b9de836c 12758#else /* not HAVE_X11R6_XIM */
f5d11644
GM
12759 dpyinfo->xim = NULL;
12760 xim_open_dpy (dpyinfo, resource_name);
b9de836c 12761#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
12762
12763#else /* not USE_XIM */
12764 dpyinfo->xim = NULL;
12765#endif /* not USE_XIM */
f5d11644
GM
12766}
12767
12768
12769/* Close the connection to the XIM server on display DPYINFO. */
12770
12771static void
12772xim_close_dpy (dpyinfo)
12773 struct x_display_info *dpyinfo;
12774{
287f7dd6 12775#ifdef USE_XIM
b9de836c 12776#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
12777 if (dpyinfo->display)
12778 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12779 NULL, EMACS_CLASS,
12780 xim_instantiate_callback, NULL);
b9de836c 12781#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
12782 if (dpyinfo->display)
12783 XCloseIM (dpyinfo->xim);
f5d11644
GM
12784 dpyinfo->xim = NULL;
12785 XFree (dpyinfo->xim_styles);
287f7dd6 12786#endif /* USE_XIM */
f5d11644
GM
12787}
12788
b9de836c 12789#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
12790
12791
dc6f92b8 12792\f
2e365682
RS
12793/* Calculate the absolute position in frame F
12794 from its current recorded position values and gravity. */
12795
dfcf069d 12796void
43bca5d5 12797x_calc_absolute_position (f)
f676886a 12798 struct frame *f;
dc6f92b8 12799{
06a2c219 12800 Window child;
6dba1858 12801 int win_x = 0, win_y = 0;
7556890b 12802 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
12803 int this_window;
12804
9829ddba
RS
12805 /* We have nothing to do if the current position
12806 is already for the top-left corner. */
12807 if (! ((flags & XNegative) || (flags & YNegative)))
12808 return;
12809
c81412a0 12810#ifdef USE_X_TOOLKIT
7556890b 12811 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
12812#else
12813 this_window = FRAME_X_WINDOW (f);
12814#endif
6dba1858
RS
12815
12816 /* Find the position of the outside upper-left corner of
9829ddba
RS
12817 the inner window, with respect to the outer window.
12818 But do this only if we will need the results. */
7556890b 12819 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 12820 {
9829ddba
RS
12821 int count;
12822
6dba1858 12823 BLOCK_INPUT;
9829ddba
RS
12824 count = x_catch_errors (FRAME_X_DISPLAY (f));
12825 while (1)
12826 {
12827 x_clear_errors (FRAME_X_DISPLAY (f));
12828 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12829
12830 /* From-window, to-window. */
12831 this_window,
12832 f->output_data.x->parent_desc,
12833
12834 /* From-position, to-position. */
12835 0, 0, &win_x, &win_y,
12836
12837 /* Child of win. */
12838 &child);
12839 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12840 {
12841 Window newroot, newparent = 0xdeadbeef;
12842 Window *newchildren;
2ebb2f8b 12843 unsigned int nchildren;
9829ddba
RS
12844
12845 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12846 &newparent, &newchildren, &nchildren))
12847 break;
58769bee 12848
7c3c78a3 12849 XFree ((char *) newchildren);
6dba1858 12850
9829ddba
RS
12851 f->output_data.x->parent_desc = newparent;
12852 }
12853 else
12854 break;
12855 }
6dba1858 12856
9829ddba 12857 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
12858 UNBLOCK_INPUT;
12859 }
12860
12861 /* Treat negative positions as relative to the leftmost bottommost
12862 position that fits on the screen. */
20f55f9a 12863 if (flags & XNegative)
7556890b 12864 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
12865 - 2 * f->output_data.x->border_width - win_x
12866 - PIXEL_WIDTH (f)
12867 + f->output_data.x->left_pos);
dc6f92b8 12868
7708ced0
GM
12869 {
12870 int height = PIXEL_HEIGHT (f);
06a2c219 12871
7708ced0
GM
12872#if defined USE_X_TOOLKIT && defined USE_MOTIF
12873 /* Something is fishy here. When using Motif, starting Emacs with
12874 `-g -0-0', the frame appears too low by a few pixels.
12875
12876 This seems to be so because initially, while Emacs is starting,
12877 the column widget's height and the frame's pixel height are
12878 different. The column widget's height is the right one. In
12879 later invocations, when Emacs is up, the frame's pixel height
12880 is right, though.
12881
12882 It's not obvious where the initial small difference comes from.
12883 2000-12-01, gerd. */
12884
12885 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 12886#endif
2e365682 12887
7708ced0
GM
12888 if (flags & YNegative)
12889 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12890 - 2 * f->output_data.x->border_width
12891 - win_y
12892 - height
12893 + f->output_data.x->top_pos);
12894 }
12895
3a35ab44
RS
12896 /* The left_pos and top_pos
12897 are now relative to the top and left screen edges,
12898 so the flags should correspond. */
7556890b 12899 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
12900}
12901
3a35ab44
RS
12902/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12903 to really change the position, and 0 when calling from
12904 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
12905 position values). It is -1 when calling from x_set_frame_parameters,
12906 which means, do adjust for borders but don't change the gravity. */
3a35ab44 12907
dfcf069d 12908void
dc05a16b 12909x_set_offset (f, xoff, yoff, change_gravity)
f676886a 12910 struct frame *f;
dc6f92b8 12911 register int xoff, yoff;
dc05a16b 12912 int change_gravity;
dc6f92b8 12913{
4a4cbdd5
KH
12914 int modified_top, modified_left;
12915
aa3ff7c9 12916 if (change_gravity > 0)
3a35ab44 12917 {
7556890b
RS
12918 f->output_data.x->top_pos = yoff;
12919 f->output_data.x->left_pos = xoff;
12920 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 12921 if (xoff < 0)
7556890b 12922 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 12923 if (yoff < 0)
7556890b
RS
12924 f->output_data.x->size_hint_flags |= YNegative;
12925 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 12926 }
43bca5d5 12927 x_calc_absolute_position (f);
dc6f92b8
JB
12928
12929 BLOCK_INPUT;
c32cdd9a 12930 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 12931
7556890b
RS
12932 modified_left = f->output_data.x->left_pos;
12933 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
12934#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12935 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12936 /* It is a mystery why we need to add the border_width here
12937 when the frame is already visible, but experiment says we do. */
aa3ff7c9 12938 if (change_gravity != 0)
4a4cbdd5 12939 {
7556890b
RS
12940 modified_left += f->output_data.x->border_width;
12941 modified_top += f->output_data.x->border_width;
4a4cbdd5 12942 }
e73ec6fa 12943#endif
4a4cbdd5 12944
3afe33e7 12945#ifdef USE_X_TOOLKIT
7556890b 12946 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 12947 modified_left, modified_top);
3afe33e7 12948#else /* not USE_X_TOOLKIT */
334208b7 12949 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 12950 modified_left, modified_top);
3afe33e7 12951#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12952 UNBLOCK_INPUT;
12953}
12954
d0fa3e56
EZ
12955/* Check if we need to resize the frame due to a fullscreen request.
12956 If so needed, resize the frame. */
12957static void
12958x_check_fullscreen (f)
12959 struct frame *f;
12960{
12961 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12962 {
12963 int width, height, ign;
12964
12965 x_real_positions (f, &f->output_data.x->left_pos,
12966 &f->output_data.x->top_pos);
12967
12968 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12969
12970 /* We do not need to move the window, it shall be taken care of
12971 when setting WM manager hints.
12972 If the frame is visible already, the position is checked by
12973 x_check_fullscreen_move. */
12974 if (f->width != width || f->height != height)
12975 {
12976 change_frame_size (f, height, width, 0, 1, 0);
12977 SET_FRAME_GARBAGED (f);
12978 cancel_mouse_face (f);
12979
12980 /* Wait for the change of frame size to occur */
12981 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12982
12983 }
12984 }
12985}
12986
12987/* If frame parameters are set after the frame is mapped, we need to move
12988 the window. This is done in xfns.c.
12989 Some window managers moves the window to the right position, some
12990 moves the outer window manager window to the specified position.
12991 Here we check that we are in the right spot. If not, make a second
12992 move, assuming we are dealing with the second kind of window manager. */
12993static void
12994x_check_fullscreen_move (f)
12995 struct frame *f;
12996{
12997 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12998 {
12999 int expect_top = f->output_data.x->top_pos;
13000 int expect_left = f->output_data.x->left_pos;
13001
13002 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13003 expect_top = 0;
13004 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13005 expect_left = 0;
13006
13007 if (expect_top != f->output_data.x->top_pos
13008 || expect_left != f->output_data.x->left_pos)
13009 x_set_offset (f, expect_left, expect_top, 1);
13010
13011 /* Just do this once */
13012 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
13013 }
13014}
13015
13016
13017/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
13018 wanted positions of the WM window (not emacs window).
13019 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
13020 window (FRAME_X_WINDOW).
13021 */
13022void
13023x_fullscreen_adjust (f, width, height, top_pos, left_pos)
13024 struct frame *f;
13025 int *width;
13026 int *height;
13027 int *top_pos;
13028 int *left_pos;
13029{
13030 int newwidth = f->width, newheight = f->height;
13031
13032 *top_pos = f->output_data.x->top_pos;
13033 *left_pos = f->output_data.x->left_pos;
13034
13035 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13036 {
13037 int ph;
13038
13039 ph = FRAME_X_DISPLAY_INFO (f)->height;
13040 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13041 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
13042 - f->output_data.x->y_pixels_diff;
13043 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13044 *top_pos = 0;
13045 }
13046
13047 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13048 {
13049 int pw;
13050
13051 pw = FRAME_X_DISPLAY_INFO (f)->width;
13052 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13053 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
13054 - f->output_data.x->x_pixels_diff;
13055 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13056 *left_pos = 0;
13057 }
13058
13059 *width = newwidth;
13060 *height = newheight;
13061}
13062
dc6f92b8 13063
499b1844
GM
13064/* Change the size of frame F's X window to COLS/ROWS in the case F
13065 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
13066 top-left-corner window gravity for this size change and subsequent
13067 size changes. Otherwise we leave the window gravity unchanged. */
13068
13069static void
13070x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 13071 struct frame *f;
bc20ebbf 13072 int change_gravity;
b1c884c3 13073 int cols, rows;
dc6f92b8
JB
13074{
13075 int pixelwidth, pixelheight;
80fd1fe2 13076
b1c884c3 13077 check_frame_size (f, &rows, &cols);
7556890b 13078 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
13079 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
13080 ? 0
13081 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 13082 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 13083 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
976b73d7
KS
13084
13085 x_compute_fringe_widths (f, 0);
13086
f451eb13
JB
13087 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
13088 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 13089
7556890b 13090 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 13091 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 13092
334208b7
RS
13093 XSync (FRAME_X_DISPLAY (f), False);
13094 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13095 pixelwidth, pixelheight);
b1c884c3
JB
13096
13097 /* Now, strictly speaking, we can't be sure that this is accurate,
13098 but the window manager will get around to dealing with the size
13099 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
13100 ConfigureNotify event gets here.
13101
13102 We could just not bother storing any of this information here,
13103 and let the ConfigureNotify event set everything up, but that
fddd5ceb 13104 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 13105 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
13106 point in the future when the ConfigureNotify event arrives.
13107
13108 We pass 1 for DELAY since we can't run Lisp code inside of
13109 a BLOCK_INPUT. */
7d1e984f 13110 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
13111 PIXEL_WIDTH (f) = pixelwidth;
13112 PIXEL_HEIGHT (f) = pixelheight;
13113
aee9a898
RS
13114 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
13115 receive in the ConfigureNotify event; if we get what we asked
13116 for, then the event won't cause the screen to become garbaged, so
13117 we have to make sure to do it here. */
13118 SET_FRAME_GARBAGED (f);
13119
13120 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
13121}
13122
13123
13124/* Call this to change the size of frame F's x-window.
13125 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
13126 for this size change and subsequent size changes.
13127 Otherwise we leave the window gravity unchanged. */
aee9a898 13128
499b1844
GM
13129void
13130x_set_window_size (f, change_gravity, cols, rows)
13131 struct frame *f;
13132 int change_gravity;
13133 int cols, rows;
13134{
13135 BLOCK_INPUT;
13136
13137#ifdef USE_X_TOOLKIT
13138
f1f4d345 13139 if (f->output_data.x->widget != NULL)
499b1844
GM
13140 {
13141 /* The x and y position of the widget is clobbered by the
13142 call to XtSetValues within EmacsFrameSetCharSize.
13143 This is a real kludge, but I don't understand Xt so I can't
13144 figure out a correct fix. Can anyone else tell me? -- rms. */
13145 int xpos = f->output_data.x->widget->core.x;
13146 int ypos = f->output_data.x->widget->core.y;
13147 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13148 f->output_data.x->widget->core.x = xpos;
13149 f->output_data.x->widget->core.y = ypos;
13150 }
13151 else
13152 x_set_window_size_1 (f, change_gravity, cols, rows);
13153
13154#else /* not USE_X_TOOLKIT */
13155
13156 x_set_window_size_1 (f, change_gravity, cols, rows);
13157
aee9a898
RS
13158#endif /* not USE_X_TOOLKIT */
13159
4d73d038 13160 /* If cursor was outside the new size, mark it as off. */
06a2c219 13161 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 13162
aee9a898
RS
13163 /* Clear out any recollection of where the mouse highlighting was,
13164 since it might be in a place that's outside the new frame size.
13165 Actually checking whether it is outside is a pain in the neck,
13166 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 13167 cancel_mouse_face (f);
dbc4e1c1 13168
dc6f92b8
JB
13169 UNBLOCK_INPUT;
13170}
dc6f92b8 13171\f
d047c4eb 13172/* Mouse warping. */
dc6f92b8 13173
9b378208 13174void
f676886a
JB
13175x_set_mouse_position (f, x, y)
13176 struct frame *f;
dc6f92b8
JB
13177 int x, y;
13178{
13179 int pix_x, pix_y;
13180
7556890b
RS
13181 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13182 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
13183
13184 if (pix_x < 0) pix_x = 0;
13185 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13186
13187 if (pix_y < 0) pix_y = 0;
13188 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
13189
13190 BLOCK_INPUT;
dc6f92b8 13191
334208b7
RS
13192 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13193 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
13194 UNBLOCK_INPUT;
13195}
13196
9b378208
RS
13197/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13198
13199void
13200x_set_mouse_pixel_position (f, pix_x, pix_y)
13201 struct frame *f;
13202 int pix_x, pix_y;
13203{
13204 BLOCK_INPUT;
13205
334208b7
RS
13206 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13207 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
13208 UNBLOCK_INPUT;
13209}
d047c4eb
KH
13210\f
13211/* focus shifting, raising and lowering. */
9b378208 13212
dfcf069d 13213void
f676886a
JB
13214x_focus_on_frame (f)
13215 struct frame *f;
dc6f92b8 13216{
1fb20991 13217#if 0 /* This proves to be unpleasant. */
f676886a 13218 x_raise_frame (f);
1fb20991 13219#endif
6d4238f3
JB
13220#if 0
13221 /* I don't think that the ICCCM allows programs to do things like this
13222 without the interaction of the window manager. Whatever you end up
f676886a 13223 doing with this code, do it to x_unfocus_frame too. */
334208b7 13224 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 13225 RevertToPointerRoot, CurrentTime);
c118dd06 13226#endif /* ! 0 */
dc6f92b8
JB
13227}
13228
dfcf069d 13229void
f676886a
JB
13230x_unfocus_frame (f)
13231 struct frame *f;
dc6f92b8 13232{
6d4238f3 13233#if 0
f676886a 13234 /* Look at the remarks in x_focus_on_frame. */
0f941935 13235 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 13236 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 13237 RevertToPointerRoot, CurrentTime);
c118dd06 13238#endif /* ! 0 */
dc6f92b8
JB
13239}
13240
f676886a 13241/* Raise frame F. */
dc6f92b8 13242
dfcf069d 13243void
f676886a
JB
13244x_raise_frame (f)
13245 struct frame *f;
dc6f92b8 13246{
3a88c238 13247 if (f->async_visible)
dc6f92b8
JB
13248 {
13249 BLOCK_INPUT;
3afe33e7 13250#ifdef USE_X_TOOLKIT
7556890b 13251 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13252#else /* not USE_X_TOOLKIT */
334208b7 13253 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13254#endif /* not USE_X_TOOLKIT */
334208b7 13255 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13256 UNBLOCK_INPUT;
13257 }
13258}
13259
f676886a 13260/* Lower frame F. */
dc6f92b8 13261
dfcf069d 13262void
f676886a
JB
13263x_lower_frame (f)
13264 struct frame *f;
dc6f92b8 13265{
3a88c238 13266 if (f->async_visible)
dc6f92b8
JB
13267 {
13268 BLOCK_INPUT;
3afe33e7 13269#ifdef USE_X_TOOLKIT
7556890b 13270 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13271#else /* not USE_X_TOOLKIT */
334208b7 13272 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13273#endif /* not USE_X_TOOLKIT */
334208b7 13274 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13275 UNBLOCK_INPUT;
13276 }
13277}
13278
dbc4e1c1 13279static void
6b0442dc 13280XTframe_raise_lower (f, raise_flag)
dbc4e1c1 13281 FRAME_PTR f;
6b0442dc 13282 int raise_flag;
dbc4e1c1 13283{
6b0442dc 13284 if (raise_flag)
dbc4e1c1
JB
13285 x_raise_frame (f);
13286 else
13287 x_lower_frame (f);
13288}
d047c4eb
KH
13289\f
13290/* Change of visibility. */
dc6f92b8 13291
9382638d
KH
13292/* This tries to wait until the frame is really visible.
13293 However, if the window manager asks the user where to position
13294 the frame, this will return before the user finishes doing that.
13295 The frame will not actually be visible at that time,
13296 but it will become visible later when the window manager
13297 finishes with it. */
13298
dfcf069d 13299void
f676886a
JB
13300x_make_frame_visible (f)
13301 struct frame *f;
dc6f92b8 13302{
990ba854 13303 Lisp_Object type;
1aa6072f 13304 int original_top, original_left;
31be9251
GM
13305 int retry_count = 2;
13306
13307 retry:
dc6f92b8 13308
dc6f92b8 13309 BLOCK_INPUT;
dc6f92b8 13310
990ba854
RS
13311 type = x_icon_type (f);
13312 if (!NILP (type))
13313 x_bitmap_icon (f, type);
bdcd49ba 13314
f676886a 13315 if (! FRAME_VISIBLE_P (f))
90e65f07 13316 {
1aa6072f
RS
13317 /* We test FRAME_GARBAGED_P here to make sure we don't
13318 call x_set_offset a second time
13319 if we get to x_make_frame_visible a second time
13320 before the window gets really visible. */
13321 if (! FRAME_ICONIFIED_P (f)
13322 && ! f->output_data.x->asked_for_visible)
13323 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13324
13325 f->output_data.x->asked_for_visible = 1;
13326
90e65f07 13327 if (! EQ (Vx_no_window_manager, Qt))
f676886a 13328 x_wm_set_window_state (f, NormalState);
3afe33e7 13329#ifdef USE_X_TOOLKIT
d7a38a2e 13330 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13331 XtMapWidget (f->output_data.x->widget);
3afe33e7 13332#else /* not USE_X_TOOLKIT */
7f9c7f94 13333 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13334#endif /* not USE_X_TOOLKIT */
0134a210
RS
13335#if 0 /* This seems to bring back scroll bars in the wrong places
13336 if the window configuration has changed. They seem
13337 to come back ok without this. */
ab648270 13338 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 13339 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 13340#endif
90e65f07 13341 }
dc6f92b8 13342
334208b7 13343 XFlush (FRAME_X_DISPLAY (f));
90e65f07 13344
0dacf791
RS
13345 /* Synchronize to ensure Emacs knows the frame is visible
13346 before we do anything else. We do this loop with input not blocked
13347 so that incoming events are handled. */
13348 {
13349 Lisp_Object frame;
12ce2351 13350 int count;
28c01ffe
RS
13351 /* This must be before UNBLOCK_INPUT
13352 since events that arrive in response to the actions above
13353 will set it when they are handled. */
13354 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
13355
13356 original_left = f->output_data.x->left_pos;
13357 original_top = f->output_data.x->top_pos;
c0a04927
RS
13358
13359 /* This must come after we set COUNT. */
13360 UNBLOCK_INPUT;
13361
2745e6c4 13362 /* We unblock here so that arriving X events are processed. */
1aa6072f 13363
dcb07ae9
RS
13364 /* Now move the window back to where it was "supposed to be".
13365 But don't do it if the gravity is negative.
13366 When the gravity is negative, this uses a position
28c01ffe
RS
13367 that is 3 pixels too low. Perhaps that's really the border width.
13368
13369 Don't do this if the window has never been visible before,
13370 because the window manager may choose the position
13371 and we don't want to override it. */
1aa6072f 13372
4d3f5d9a 13373 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 13374 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 13375 && previously_visible)
1aa6072f 13376 {
2745e6c4
RS
13377 Drawable rootw;
13378 int x, y;
13379 unsigned int width, height, border, depth;
06a2c219 13380
1aa6072f 13381 BLOCK_INPUT;
9829ddba 13382
06a2c219
GM
13383 /* On some window managers (such as FVWM) moving an existing
13384 window, even to the same place, causes the window manager
13385 to introduce an offset. This can cause the window to move
13386 to an unexpected location. Check the geometry (a little
13387 slow here) and then verify that the window is in the right
13388 place. If the window is not in the right place, move it
13389 there, and take the potential window manager hit. */
2745e6c4
RS
13390 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13391 &rootw, &x, &y, &width, &height, &border, &depth);
13392
13393 if (original_left != x || original_top != y)
13394 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13395 original_left, original_top);
13396
1aa6072f
RS
13397 UNBLOCK_INPUT;
13398 }
9829ddba 13399
e0c1aef2 13400 XSETFRAME (frame, f);
c0a04927 13401
12ce2351
GM
13402 /* Wait until the frame is visible. Process X events until a
13403 MapNotify event has been seen, or until we think we won't get a
13404 MapNotify at all.. */
13405 for (count = input_signal_count + 10;
13406 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 13407 {
12ce2351 13408 /* Force processing of queued events. */
334208b7 13409 x_sync (f);
12ce2351
GM
13410
13411 /* Machines that do polling rather than SIGIO have been
13412 observed to go into a busy-wait here. So we'll fake an
13413 alarm signal to let the handler know that there's something
13414 to be read. We used to raise a real alarm, but it seems
13415 that the handler isn't always enabled here. This is
13416 probably a bug. */
8b2f8d4e 13417 if (input_polling_used ())
3b2fa4e6 13418 {
12ce2351
GM
13419 /* It could be confusing if a real alarm arrives while
13420 processing the fake one. Turn it off and let the
13421 handler reset it. */
3e71d8f2 13422 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
13423 int old_poll_suppress_count = poll_suppress_count;
13424 poll_suppress_count = 1;
13425 poll_for_input_1 ();
13426 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 13427 }
12ce2351
GM
13428
13429 /* See if a MapNotify event has been processed. */
13430 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 13431 }
31be9251
GM
13432
13433 /* 2000-09-28: In
13434
13435 (let ((f (selected-frame)))
13436 (iconify-frame f)
13437 (raise-frame f))
13438
13439 the frame is not raised with various window managers on
13440 FreeBSD, Linux and Solaris. It turns out that, for some
13441 unknown reason, the call to XtMapWidget is completely ignored.
13442 Mapping the widget a second time works. */
13443
13444 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13445 goto retry;
0dacf791 13446 }
dc6f92b8
JB
13447}
13448
06a2c219 13449/* Change from mapped state to withdrawn state. */
dc6f92b8 13450
d047c4eb
KH
13451/* Make the frame visible (mapped and not iconified). */
13452
dfcf069d 13453void
f676886a
JB
13454x_make_frame_invisible (f)
13455 struct frame *f;
dc6f92b8 13456{
546e6d5b
RS
13457 Window window;
13458
13459#ifdef USE_X_TOOLKIT
13460 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 13461 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
13462#else /* not USE_X_TOOLKIT */
13463 window = FRAME_X_WINDOW (f);
13464#endif /* not USE_X_TOOLKIT */
dc6f92b8 13465
9319ae23 13466 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13467 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13468 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13469
5627c40e 13470#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 13471 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 13472 return;
5627c40e 13473#endif
dc6f92b8
JB
13474
13475 BLOCK_INPUT;
c118dd06 13476
af31d76f
RS
13477 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13478 that the current position of the window is user-specified, rather than
13479 program-specified, so that when the window is mapped again, it will be
13480 placed at the same location, without forcing the user to position it
13481 by hand again (they have already done that once for this window.) */
c32cdd9a 13482 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 13483
c118dd06
JB
13484#ifdef HAVE_X11R4
13485
334208b7
RS
13486 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13487 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
13488 {
13489 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13490 error ("Can't notify window manager of window withdrawal");
c118dd06 13491 }
c118dd06 13492#else /* ! defined (HAVE_X11R4) */
16bd92ea 13493
c118dd06 13494 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
13495 if (! EQ (Vx_no_window_manager, Qt))
13496 {
16bd92ea 13497 XEvent unmap;
dc6f92b8 13498
16bd92ea 13499 unmap.xunmap.type = UnmapNotify;
546e6d5b 13500 unmap.xunmap.window = window;
334208b7 13501 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 13502 unmap.xunmap.from_configure = False;
334208b7
RS
13503 if (! XSendEvent (FRAME_X_DISPLAY (f),
13504 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 13505 False,
06a2c219 13506 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
13507 &unmap))
13508 {
13509 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13510 error ("Can't notify window manager of withdrawal");
16bd92ea 13511 }
dc6f92b8
JB
13512 }
13513
16bd92ea 13514 /* Unmap the window ourselves. Cheeky! */
334208b7 13515 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 13516#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 13517
5627c40e
RS
13518 /* We can't distinguish this from iconification
13519 just by the event that we get from the server.
13520 So we can't win using the usual strategy of letting
13521 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13522 and synchronize with the server to make sure we agree. */
13523 f->visible = 0;
13524 FRAME_ICONIFIED_P (f) = 0;
13525 f->async_visible = 0;
13526 f->async_iconified = 0;
13527
334208b7 13528 x_sync (f);
5627c40e 13529
dc6f92b8
JB
13530 UNBLOCK_INPUT;
13531}
13532
06a2c219 13533/* Change window state from mapped to iconified. */
dc6f92b8 13534
dfcf069d 13535void
f676886a
JB
13536x_iconify_frame (f)
13537 struct frame *f;
dc6f92b8 13538{
3afe33e7 13539 int result;
990ba854 13540 Lisp_Object type;
dc6f92b8 13541
9319ae23 13542 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13543 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13544 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13545
3a88c238 13546 if (f->async_iconified)
dc6f92b8
JB
13547 return;
13548
3afe33e7 13549 BLOCK_INPUT;
546e6d5b 13550
9af3143a
RS
13551 FRAME_SAMPLE_VISIBILITY (f);
13552
990ba854
RS
13553 type = x_icon_type (f);
13554 if (!NILP (type))
13555 x_bitmap_icon (f, type);
bdcd49ba
RS
13556
13557#ifdef USE_X_TOOLKIT
13558
546e6d5b
RS
13559 if (! FRAME_VISIBLE_P (f))
13560 {
13561 if (! EQ (Vx_no_window_manager, Qt))
13562 x_wm_set_window_state (f, IconicState);
13563 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13564 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
13565 /* The server won't give us any event to indicate
13566 that an invisible frame was changed to an icon,
13567 so we have to record it here. */
13568 f->iconified = 1;
1e6bc770 13569 f->visible = 1;
9cf30a30 13570 f->async_iconified = 1;
1e6bc770 13571 f->async_visible = 0;
546e6d5b
RS
13572 UNBLOCK_INPUT;
13573 return;
13574 }
13575
334208b7 13576 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 13577 XtWindow (f->output_data.x->widget),
334208b7 13578 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
13579 UNBLOCK_INPUT;
13580
13581 if (!result)
546e6d5b 13582 error ("Can't notify window manager of iconification");
3afe33e7
RS
13583
13584 f->async_iconified = 1;
1e6bc770
RS
13585 f->async_visible = 0;
13586
8c002a25
KH
13587
13588 BLOCK_INPUT;
334208b7 13589 XFlush (FRAME_X_DISPLAY (f));
8c002a25 13590 UNBLOCK_INPUT;
3afe33e7
RS
13591#else /* not USE_X_TOOLKIT */
13592
fd13dbb2
RS
13593 /* Make sure the X server knows where the window should be positioned,
13594 in case the user deiconifies with the window manager. */
13595 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 13596 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 13597
16bd92ea
JB
13598 /* Since we don't know which revision of X we're running, we'll use both
13599 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13600
13601 /* X11R4: send a ClientMessage to the window manager using the
13602 WM_CHANGE_STATE type. */
13603 {
13604 XEvent message;
58769bee 13605
c118dd06 13606 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 13607 message.xclient.type = ClientMessage;
334208b7 13608 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
13609 message.xclient.format = 32;
13610 message.xclient.data.l[0] = IconicState;
13611
334208b7
RS
13612 if (! XSendEvent (FRAME_X_DISPLAY (f),
13613 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
13614 False,
13615 SubstructureRedirectMask | SubstructureNotifyMask,
13616 &message))
dc6f92b8
JB
13617 {
13618 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13619 error ("Can't notify window manager of iconification");
dc6f92b8 13620 }
16bd92ea 13621 }
dc6f92b8 13622
58769bee 13623 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
13624 IconicState. */
13625 x_wm_set_window_state (f, IconicState);
dc6f92b8 13626
a9c00105
RS
13627 if (!FRAME_VISIBLE_P (f))
13628 {
13629 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 13630 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
13631 }
13632
3a88c238 13633 f->async_iconified = 1;
1e6bc770 13634 f->async_visible = 0;
dc6f92b8 13635
334208b7 13636 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 13637 UNBLOCK_INPUT;
8c002a25 13638#endif /* not USE_X_TOOLKIT */
dc6f92b8 13639}
19f71add 13640
d047c4eb 13641\f
19f71add 13642/* Free X resources of frame F. */
dc6f92b8 13643
dfcf069d 13644void
19f71add 13645x_free_frame_resources (f)
f676886a 13646 struct frame *f;
dc6f92b8 13647{
7f9c7f94 13648 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
13649 Lisp_Object bar;
13650 struct scroll_bar *b;
7f9c7f94 13651
dc6f92b8 13652 BLOCK_INPUT;
c0ff3fab 13653
6186a4a0
RS
13654 /* If a display connection is dead, don't try sending more
13655 commands to the X server. */
19f71add 13656 if (dpyinfo->display)
6186a4a0 13657 {
19f71add 13658 if (f->output_data.x->icon_desc)
6186a4a0 13659 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
13660
13661#ifdef USE_X_TOOLKIT
13662 /* Explicitly destroy the scroll bars of the frame. Without
13663 this, we get "BadDrawable" errors from the toolkit later on,
13664 presumably from expose events generated for the disappearing
13665 toolkit scroll bars. */
13666 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13667 {
13668 b = XSCROLL_BAR (bar);
13669 x_scroll_bar_remove (b);
13670 }
13671#endif
13672
31f41daf 13673#ifdef HAVE_X_I18N
f5d11644
GM
13674 if (FRAME_XIC (f))
13675 free_frame_xic (f);
31f41daf 13676#endif
c6ea2775 13677
3afe33e7 13678#ifdef USE_X_TOOLKIT
06a2c219 13679 if (f->output_data.x->widget)
30ca89f5
GM
13680 {
13681 XtDestroyWidget (f->output_data.x->widget);
13682 f->output_data.x->widget = NULL;
13683 }
c6ea2775
EZ
13684 /* Tooltips don't have widgets, only a simple X window, even if
13685 we are using a toolkit. */
13686 else if (FRAME_X_WINDOW (f))
13687 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13688
6186a4a0 13689 free_frame_menubar (f);
c6ea2775
EZ
13690#else /* !USE_X_TOOLKIT */
13691 if (FRAME_X_WINDOW (f))
13692 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13693#endif /* !USE_X_TOOLKIT */
3afe33e7 13694
3e71d8f2
GM
13695 unload_color (f, f->output_data.x->foreground_pixel);
13696 unload_color (f, f->output_data.x->background_pixel);
13697 unload_color (f, f->output_data.x->cursor_pixel);
13698 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13699 unload_color (f, f->output_data.x->border_pixel);
13700 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 13701
3e71d8f2
GM
13702 if (f->output_data.x->scroll_bar_background_pixel != -1)
13703 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13704 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13705 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
13706#ifdef USE_TOOLKIT_SCROLL_BARS
13707 /* Scrollbar shadow colors. */
13708 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13709 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13710 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13711 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13712#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
13713 if (f->output_data.x->white_relief.allocated_p)
13714 unload_color (f, f->output_data.x->white_relief.pixel);
13715 if (f->output_data.x->black_relief.allocated_p)
13716 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 13717
19f71add
GM
13718 if (FRAME_FACE_CACHE (f))
13719 free_frame_faces (f);
13720
4ca78676 13721 x_free_gcs (f);
6186a4a0
RS
13722 XFlush (FRAME_X_DISPLAY (f));
13723 }
dc6f92b8 13724
df89d8a4 13725 if (f->output_data.x->saved_menu_event)
06a2c219 13726 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 13727
7556890b 13728 xfree (f->output_data.x);
19f71add
GM
13729 f->output_data.x = NULL;
13730
0f941935
KH
13731 if (f == dpyinfo->x_focus_frame)
13732 dpyinfo->x_focus_frame = 0;
13733 if (f == dpyinfo->x_focus_event_frame)
13734 dpyinfo->x_focus_event_frame = 0;
13735 if (f == dpyinfo->x_highlight_frame)
13736 dpyinfo->x_highlight_frame = 0;
c0ff3fab 13737
7f9c7f94 13738 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 13739 {
7f9c7f94
RS
13740 dpyinfo->mouse_face_beg_row
13741 = dpyinfo->mouse_face_beg_col = -1;
13742 dpyinfo->mouse_face_end_row
13743 = dpyinfo->mouse_face_end_col = -1;
13744 dpyinfo->mouse_face_window = Qnil;
21323706
RS
13745 dpyinfo->mouse_face_deferred_gc = 0;
13746 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 13747 }
0134a210 13748
c0ff3fab 13749 UNBLOCK_INPUT;
dc6f92b8 13750}
19f71add
GM
13751
13752
13753/* Destroy the X window of frame F. */
13754
13755void
13756x_destroy_window (f)
13757 struct frame *f;
13758{
13759 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13760
13761 /* If a display connection is dead, don't try sending more
13762 commands to the X server. */
13763 if (dpyinfo->display != 0)
13764 x_free_frame_resources (f);
13765
13766 dpyinfo->reference_count--;
13767}
13768
dc6f92b8 13769\f
f451eb13
JB
13770/* Setting window manager hints. */
13771
af31d76f
RS
13772/* Set the normal size hints for the window manager, for frame F.
13773 FLAGS is the flags word to use--or 0 meaning preserve the flags
13774 that the window now has.
13775 If USER_POSITION is nonzero, we set the USPosition
13776 flag (this is useful when FLAGS is 0). */
6dba1858 13777
dfcf069d 13778void
af31d76f 13779x_wm_set_size_hint (f, flags, user_position)
f676886a 13780 struct frame *f;
af31d76f
RS
13781 long flags;
13782 int user_position;
dc6f92b8
JB
13783{
13784 XSizeHints size_hints;
3afe33e7
RS
13785
13786#ifdef USE_X_TOOLKIT
7e4f2521
FP
13787 Arg al[2];
13788 int ac = 0;
13789 Dimension widget_width, widget_height;
7556890b 13790 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 13791#else /* not USE_X_TOOLKIT */
c118dd06 13792 Window window = FRAME_X_WINDOW (f);
3afe33e7 13793#endif /* not USE_X_TOOLKIT */
dc6f92b8 13794
b72a58fd
RS
13795 /* Setting PMaxSize caused various problems. */
13796 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 13797
7556890b
RS
13798 size_hints.x = f->output_data.x->left_pos;
13799 size_hints.y = f->output_data.x->top_pos;
7553a6b7 13800
7e4f2521
FP
13801#ifdef USE_X_TOOLKIT
13802 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13803 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 13804 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
13805 size_hints.height = widget_height;
13806 size_hints.width = widget_width;
13807#else /* not USE_X_TOOLKIT */
f676886a
JB
13808 size_hints.height = PIXEL_HEIGHT (f);
13809 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 13810#endif /* not USE_X_TOOLKIT */
7553a6b7 13811
7556890b
RS
13812 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13813 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
13814 size_hints.max_width
13815 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13816 size_hints.max_height
13817 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 13818
d067ea8b
KH
13819 /* Calculate the base and minimum sizes.
13820
13821 (When we use the X toolkit, we don't do it here.
13822 Instead we copy the values that the widgets are using, below.) */
13823#ifndef USE_X_TOOLKIT
b1c884c3 13824 {
b0342f17 13825 int base_width, base_height;
0134a210 13826 int min_rows = 0, min_cols = 0;
b0342f17 13827
f451eb13
JB
13828 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13829 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 13830
0134a210 13831 check_frame_size (f, &min_rows, &min_cols);
b0342f17 13832
0134a210
RS
13833 /* The window manager uses the base width hints to calculate the
13834 current number of rows and columns in the frame while
13835 resizing; min_width and min_height aren't useful for this
13836 purpose, since they might not give the dimensions for a
13837 zero-row, zero-column frame.
58769bee 13838
0134a210
RS
13839 We use the base_width and base_height members if we have
13840 them; otherwise, we set the min_width and min_height members
13841 to the size for a zero x zero frame. */
b0342f17
JB
13842
13843#ifdef HAVE_X11R4
0134a210
RS
13844 size_hints.flags |= PBaseSize;
13845 size_hints.base_width = base_width;
13846 size_hints.base_height = base_height;
13847 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13848 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 13849#else
0134a210
RS
13850 size_hints.min_width = base_width;
13851 size_hints.min_height = base_height;
b0342f17 13852#endif
b1c884c3 13853 }
dc6f92b8 13854
d067ea8b 13855 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 13856 if (flags)
dc6f92b8 13857 {
d067ea8b
KH
13858 size_hints.flags |= flags;
13859 goto no_read;
13860 }
13861#endif /* not USE_X_TOOLKIT */
13862
13863 {
13864 XSizeHints hints; /* Sometimes I hate X Windows... */
13865 long supplied_return;
13866 int value;
af31d76f
RS
13867
13868#ifdef HAVE_X11R4
d067ea8b
KH
13869 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13870 &supplied_return);
af31d76f 13871#else
d067ea8b 13872 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 13873#endif
58769bee 13874
d067ea8b
KH
13875#ifdef USE_X_TOOLKIT
13876 size_hints.base_height = hints.base_height;
13877 size_hints.base_width = hints.base_width;
13878 size_hints.min_height = hints.min_height;
13879 size_hints.min_width = hints.min_width;
13880#endif
13881
13882 if (flags)
13883 size_hints.flags |= flags;
13884 else
13885 {
13886 if (value == 0)
13887 hints.flags = 0;
13888 if (hints.flags & PSize)
13889 size_hints.flags |= PSize;
13890 if (hints.flags & PPosition)
13891 size_hints.flags |= PPosition;
13892 if (hints.flags & USPosition)
13893 size_hints.flags |= USPosition;
13894 if (hints.flags & USSize)
13895 size_hints.flags |= USSize;
13896 }
13897 }
13898
06a2c219 13899#ifndef USE_X_TOOLKIT
d067ea8b 13900 no_read:
06a2c219 13901#endif
0134a210 13902
af31d76f 13903#ifdef PWinGravity
7556890b 13904 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 13905 size_hints.flags |= PWinGravity;
dc05a16b 13906
af31d76f 13907 if (user_position)
6dba1858 13908 {
af31d76f
RS
13909 size_hints.flags &= ~ PPosition;
13910 size_hints.flags |= USPosition;
6dba1858 13911 }
2554751d 13912#endif /* PWinGravity */
6dba1858 13913
b0342f17 13914#ifdef HAVE_X11R4
334208b7 13915 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13916#else
334208b7 13917 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13918#endif
dc6f92b8
JB
13919}
13920
13921/* Used for IconicState or NormalState */
06a2c219 13922
dfcf069d 13923void
f676886a
JB
13924x_wm_set_window_state (f, state)
13925 struct frame *f;
dc6f92b8
JB
13926 int state;
13927{
3afe33e7 13928#ifdef USE_X_TOOLKIT
546e6d5b
RS
13929 Arg al[1];
13930
13931 XtSetArg (al[0], XtNinitialState, state);
7556890b 13932 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 13933#else /* not USE_X_TOOLKIT */
c118dd06 13934 Window window = FRAME_X_WINDOW (f);
dc6f92b8 13935
7556890b
RS
13936 f->output_data.x->wm_hints.flags |= StateHint;
13937 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 13938
7556890b 13939 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 13940#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13941}
13942
dfcf069d 13943void
7f2ae036 13944x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 13945 struct frame *f;
7f2ae036 13946 int pixmap_id;
dc6f92b8 13947{
d2bd6bc4
RS
13948 Pixmap icon_pixmap;
13949
06a2c219 13950#ifndef USE_X_TOOLKIT
c118dd06 13951 Window window = FRAME_X_WINDOW (f);
75231bad 13952#endif
dc6f92b8 13953
7f2ae036 13954 if (pixmap_id > 0)
dbc4e1c1 13955 {
d2bd6bc4 13956 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 13957 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
13958 }
13959 else
68568555
RS
13960 {
13961 /* It seems there is no way to turn off use of an icon pixmap.
13962 The following line does it, only if no icon has yet been created,
13963 for some window managers. But with mwm it crashes.
13964 Some people say it should clear the IconPixmapHint bit in this case,
13965 but that doesn't work, and the X consortium said it isn't the
13966 right thing at all. Since there is no way to win,
13967 best to explicitly give up. */
13968#if 0
13969 f->output_data.x->wm_hints.icon_pixmap = None;
13970#else
13971 return;
13972#endif
13973 }
b1c884c3 13974
d2bd6bc4
RS
13975#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13976
13977 {
13978 Arg al[1];
13979 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13980 XtSetValues (f->output_data.x->widget, al, 1);
13981 }
13982
13983#else /* not USE_X_TOOLKIT */
13984
7556890b
RS
13985 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13986 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
13987
13988#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13989}
13990
dfcf069d 13991void
f676886a
JB
13992x_wm_set_icon_position (f, icon_x, icon_y)
13993 struct frame *f;
dc6f92b8
JB
13994 int icon_x, icon_y;
13995{
75231bad 13996#ifdef USE_X_TOOLKIT
7556890b 13997 Window window = XtWindow (f->output_data.x->widget);
75231bad 13998#else
c118dd06 13999 Window window = FRAME_X_WINDOW (f);
75231bad 14000#endif
dc6f92b8 14001
7556890b
RS
14002 f->output_data.x->wm_hints.flags |= IconPositionHint;
14003 f->output_data.x->wm_hints.icon_x = icon_x;
14004 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 14005
7556890b 14006 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
14007}
14008
14009\f
06a2c219
GM
14010/***********************************************************************
14011 Fonts
14012 ***********************************************************************/
dc43ef94
KH
14013
14014/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 14015
dc43ef94
KH
14016struct font_info *
14017x_get_font_info (f, font_idx)
14018 FRAME_PTR f;
14019 int font_idx;
14020{
14021 return (FRAME_X_FONT_TABLE (f) + font_idx);
14022}
14023
14024
9c11f79e
GM
14025/* Return a list of names of available fonts matching PATTERN on frame F.
14026
14027 If SIZE is > 0, it is the size (maximum bounds width) of fonts
14028 to be listed.
14029
14030 SIZE < 0 means include scalable fonts.
14031
14032 Frame F null means we have not yet created any frame on X, and
14033 consult the first display in x_display_list. MAXNAMES sets a limit
14034 on how many fonts to match. */
dc43ef94
KH
14035
14036Lisp_Object
14037x_list_fonts (f, pattern, size, maxnames)
9c11f79e 14038 struct frame *f;
dc43ef94
KH
14039 Lisp_Object pattern;
14040 int size;
14041 int maxnames;
14042{
06a2c219
GM
14043 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
14044 Lisp_Object tem, second_best;
9c11f79e
GM
14045 struct x_display_info *dpyinfo
14046 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
14047 Display *dpy = dpyinfo->display;
09c6077f 14048 int try_XLoadQueryFont = 0;
53ca4657 14049 int count;
9c11f79e
GM
14050 int allow_scalable_fonts_p = 0;
14051
14052 if (size < 0)
14053 {
14054 allow_scalable_fonts_p = 1;
14055 size = 0;
14056 }
dc43ef94 14057
6b0efe73 14058 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
14059 if (NILP (patterns))
14060 patterns = Fcons (pattern, Qnil);
81ba44e5 14061
09c6077f
KH
14062 if (maxnames == 1 && !size)
14063 /* We can return any single font matching PATTERN. */
14064 try_XLoadQueryFont = 1;
9a32686f 14065
8e713be6 14066 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 14067 {
dc43ef94 14068 int num_fonts;
3e71d8f2 14069 char **names = NULL;
dc43ef94 14070
8e713be6 14071 pattern = XCAR (patterns);
536f4067
RS
14072 /* See if we cached the result for this particular query.
14073 The cache is an alist of the form:
9c11f79e
GM
14074 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
14075 tem = XCDR (dpyinfo->name_list_element);
14076 key = Fcons (Fcons (pattern, make_number (maxnames)),
14077 allow_scalable_fonts_p ? Qt : Qnil);
14078 list = Fassoc (key, tem);
14079 if (!NILP (list))
b5210ea7
KH
14080 {
14081 list = Fcdr_safe (list);
14082 /* We have a cashed list. Don't have to get the list again. */
14083 goto label_cached;
14084 }
14085
14086 /* At first, put PATTERN in the cache. */
09c6077f 14087
dc43ef94 14088 BLOCK_INPUT;
17d85edc
KH
14089 count = x_catch_errors (dpy);
14090
09c6077f
KH
14091 if (try_XLoadQueryFont)
14092 {
14093 XFontStruct *font;
14094 unsigned long value;
14095
d5db4077 14096 font = XLoadQueryFont (dpy, SDATA (pattern));
17d85edc
KH
14097 if (x_had_errors_p (dpy))
14098 {
14099 /* This error is perhaps due to insufficient memory on X
14100 server. Let's just ignore it. */
14101 font = NULL;
14102 x_clear_errors (dpy);
14103 }
14104
09c6077f
KH
14105 if (font
14106 && XGetFontProperty (font, XA_FONT, &value))
14107 {
14108 char *name = (char *) XGetAtomName (dpy, (Atom) value);
14109 int len = strlen (name);
01c752b5 14110 char *tmp;
09c6077f 14111
6f6512e8
KH
14112 /* If DXPC (a Differential X Protocol Compressor)
14113 Ver.3.7 is running, XGetAtomName will return null
14114 string. We must avoid such a name. */
14115 if (len == 0)
14116 try_XLoadQueryFont = 0;
14117 else
14118 {
14119 num_fonts = 1;
14120 names = (char **) alloca (sizeof (char *));
14121 /* Some systems only allow alloca assigned to a
14122 simple var. */
14123 tmp = (char *) alloca (len + 1); names[0] = tmp;
14124 bcopy (name, names[0], len + 1);
14125 XFree (name);
14126 }
09c6077f
KH
14127 }
14128 else
14129 try_XLoadQueryFont = 0;
a083fd23
RS
14130
14131 if (font)
14132 XFreeFont (dpy, font);
09c6077f
KH
14133 }
14134
14135 if (!try_XLoadQueryFont)
17d85edc
KH
14136 {
14137 /* We try at least 10 fonts because XListFonts will return
14138 auto-scaled fonts at the head. */
d5db4077 14139 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
17d85edc
KH
14140 &num_fonts);
14141 if (x_had_errors_p (dpy))
14142 {
14143 /* This error is perhaps due to insufficient memory on X
14144 server. Let's just ignore it. */
14145 names = NULL;
14146 x_clear_errors (dpy);
14147 }
14148 }
14149
14150 x_uncatch_errors (dpy, count);
dc43ef94
KH
14151 UNBLOCK_INPUT;
14152
14153 if (names)
14154 {
14155 int i;
dc43ef94
KH
14156
14157 /* Make a list of all the fonts we got back.
14158 Store that in the font cache for the display. */
14159 for (i = 0; i < num_fonts; i++)
14160 {
06a2c219 14161 int width = 0;
dc43ef94 14162 char *p = names[i];
06a2c219
GM
14163 int average_width = -1, dashes = 0;
14164
dc43ef94 14165 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
14166 14 dashes, and the field value following 12th dash
14167 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14168 is usually too ugly to be used for editing. Let's
14169 ignore it. */
dc43ef94
KH
14170 while (*p)
14171 if (*p++ == '-')
14172 {
14173 dashes++;
14174 if (dashes == 7) /* PIXEL_SIZE field */
14175 width = atoi (p);
14176 else if (dashes == 12) /* AVERAGE_WIDTH field */
14177 average_width = atoi (p);
14178 }
9c11f79e
GM
14179
14180 if (allow_scalable_fonts_p
14181 || dashes < 14 || average_width != 0)
dc43ef94
KH
14182 {
14183 tem = build_string (names[i]);
14184 if (NILP (Fassoc (tem, list)))
14185 {
14186 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
14187 && ((fast_c_string_match_ignore_case
14188 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
14189 >= 0))
14190 /* We can set the value of PIXEL_SIZE to the
b5210ea7 14191 width of this font. */
dc43ef94
KH
14192 list = Fcons (Fcons (tem, make_number (width)), list);
14193 else
14194 /* For the moment, width is not known. */
14195 list = Fcons (Fcons (tem, Qnil), list);
14196 }
14197 }
14198 }
e38f4136 14199
09c6077f 14200 if (!try_XLoadQueryFont)
e38f4136
GM
14201 {
14202 BLOCK_INPUT;
14203 XFreeFontNames (names);
14204 UNBLOCK_INPUT;
14205 }
dc43ef94
KH
14206 }
14207
b5210ea7 14208 /* Now store the result in the cache. */
f3fbd155
KR
14209 XSETCDR (dpyinfo->name_list_element,
14210 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 14211
b5210ea7
KH
14212 label_cached:
14213 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 14214
b5210ea7
KH
14215 newlist = second_best = Qnil;
14216 /* Make a list of the fonts that have the right width. */
8e713be6 14217 for (; CONSP (list); list = XCDR (list))
b5210ea7 14218 {
536f4067
RS
14219 int found_size;
14220
8e713be6 14221 tem = XCAR (list);
dc43ef94 14222
8e713be6 14223 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
14224 continue;
14225 if (!size)
14226 {
8e713be6 14227 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
14228 continue;
14229 }
dc43ef94 14230
8e713be6 14231 if (!INTEGERP (XCDR (tem)))
dc43ef94 14232 {
b5210ea7 14233 /* Since we have not yet known the size of this font, we
9c11f79e 14234 must try slow function call XLoadQueryFont. */
dc43ef94
KH
14235 XFontStruct *thisinfo;
14236
14237 BLOCK_INPUT;
17d85edc 14238 count = x_catch_errors (dpy);
dc43ef94 14239 thisinfo = XLoadQueryFont (dpy,
d5db4077 14240 SDATA (XCAR (tem)));
17d85edc
KH
14241 if (x_had_errors_p (dpy))
14242 {
14243 /* This error is perhaps due to insufficient memory on X
14244 server. Let's just ignore it. */
14245 thisinfo = NULL;
14246 x_clear_errors (dpy);
14247 }
14248 x_uncatch_errors (dpy, count);
dc43ef94
KH
14249 UNBLOCK_INPUT;
14250
14251 if (thisinfo)
14252 {
f3fbd155
KR
14253 XSETCDR (tem,
14254 (thisinfo->min_bounds.width == 0
14255 ? make_number (0)
14256 : make_number (thisinfo->max_bounds.width)));
e38f4136 14257 BLOCK_INPUT;
dc43ef94 14258 XFreeFont (dpy, thisinfo);
e38f4136 14259 UNBLOCK_INPUT;
dc43ef94
KH
14260 }
14261 else
b5210ea7 14262 /* For unknown reason, the previous call of XListFont had
06a2c219 14263 returned a font which can't be opened. Record the size
b5210ea7 14264 as 0 not to try to open it again. */
f3fbd155 14265 XSETCDR (tem, make_number (0));
dc43ef94 14266 }
536f4067 14267
8e713be6 14268 found_size = XINT (XCDR (tem));
536f4067 14269 if (found_size == size)
8e713be6 14270 newlist = Fcons (XCAR (tem), newlist);
536f4067 14271 else if (found_size > 0)
b5210ea7 14272 {
536f4067 14273 if (NILP (second_best))
b5210ea7 14274 second_best = tem;
536f4067
RS
14275 else if (found_size < size)
14276 {
8e713be6
KR
14277 if (XINT (XCDR (second_best)) > size
14278 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
14279 second_best = tem;
14280 }
14281 else
14282 {
8e713be6
KR
14283 if (XINT (XCDR (second_best)) > size
14284 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
14285 second_best = tem;
14286 }
b5210ea7
KH
14287 }
14288 }
14289 if (!NILP (newlist))
14290 break;
14291 else if (!NILP (second_best))
14292 {
8e713be6 14293 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 14294 break;
dc43ef94 14295 }
dc43ef94
KH
14296 }
14297
14298 return newlist;
14299}
14300
06a2c219
GM
14301
14302#if GLYPH_DEBUG
14303
14304/* Check that FONT is valid on frame F. It is if it can be found in F's
14305 font table. */
14306
14307static void
14308x_check_font (f, font)
14309 struct frame *f;
14310 XFontStruct *font;
14311{
14312 int i;
14313 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14314
14315 xassert (font != NULL);
14316
14317 for (i = 0; i < dpyinfo->n_fonts; i++)
14318 if (dpyinfo->font_table[i].name
14319 && font == dpyinfo->font_table[i].font)
14320 break;
14321
14322 xassert (i < dpyinfo->n_fonts);
14323}
14324
14325#endif /* GLYPH_DEBUG != 0 */
14326
14327/* Set *W to the minimum width, *H to the minimum font height of FONT.
14328 Note: There are (broken) X fonts out there with invalid XFontStruct
14329 min_bounds contents. For example, handa@etl.go.jp reports that
14330 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14331 have font->min_bounds.width == 0. */
14332
14333static INLINE void
14334x_font_min_bounds (font, w, h)
14335 XFontStruct *font;
14336 int *w, *h;
14337{
14338 *h = FONT_HEIGHT (font);
14339 *w = font->min_bounds.width;
14340
14341 /* Try to handle the case where FONT->min_bounds has invalid
14342 contents. Since the only font known to have invalid min_bounds
14343 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14344 if (*w <= 0)
14345 *w = font->max_bounds.width;
14346}
14347
14348
14349/* Compute the smallest character width and smallest font height over
14350 all fonts available on frame F. Set the members smallest_char_width
14351 and smallest_font_height in F's x_display_info structure to
14352 the values computed. Value is non-zero if smallest_font_height or
14353 smallest_char_width become smaller than they were before. */
14354
14355static int
14356x_compute_min_glyph_bounds (f)
14357 struct frame *f;
14358{
14359 int i;
14360 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14361 XFontStruct *font;
14362 int old_width = dpyinfo->smallest_char_width;
14363 int old_height = dpyinfo->smallest_font_height;
14364
14365 dpyinfo->smallest_font_height = 100000;
14366 dpyinfo->smallest_char_width = 100000;
14367
14368 for (i = 0; i < dpyinfo->n_fonts; ++i)
14369 if (dpyinfo->font_table[i].name)
14370 {
14371 struct font_info *fontp = dpyinfo->font_table + i;
14372 int w, h;
14373
14374 font = (XFontStruct *) fontp->font;
14375 xassert (font != (XFontStruct *) ~0);
14376 x_font_min_bounds (font, &w, &h);
14377
14378 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14379 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14380 }
14381
14382 xassert (dpyinfo->smallest_char_width > 0
14383 && dpyinfo->smallest_font_height > 0);
14384
14385 return (dpyinfo->n_fonts == 1
14386 || dpyinfo->smallest_char_width < old_width
14387 || dpyinfo->smallest_font_height < old_height);
14388}
14389
14390
dc43ef94
KH
14391/* Load font named FONTNAME of the size SIZE for frame F, and return a
14392 pointer to the structure font_info while allocating it dynamically.
14393 If SIZE is 0, load any size of font.
14394 If loading is failed, return NULL. */
14395
14396struct font_info *
14397x_load_font (f, fontname, size)
14398 struct frame *f;
14399 register char *fontname;
14400 int size;
14401{
14402 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14403 Lisp_Object font_names;
d645aaa4 14404 int count;
dc43ef94
KH
14405
14406 /* Get a list of all the fonts that match this name. Once we
14407 have a list of matching fonts, we compare them against the fonts
14408 we already have by comparing names. */
09c6077f 14409 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
14410
14411 if (!NILP (font_names))
14412 {
14413 Lisp_Object tail;
14414 int i;
14415
14416 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 14417 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
14418 if (dpyinfo->font_table[i].name
14419 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 14420 SDATA (XCAR (tail)))
06a2c219 14421 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 14422 SDATA (XCAR (tail)))))
dc43ef94
KH
14423 return (dpyinfo->font_table + i);
14424 }
14425
14426 /* Load the font and add it to the table. */
14427 {
14428 char *full_name;
14429 XFontStruct *font;
14430 struct font_info *fontp;
14431 unsigned long value;
06a2c219 14432 int i;
dc43ef94 14433
2da424f1
KH
14434 /* If we have found fonts by x_list_font, load one of them. If
14435 not, we still try to load a font by the name given as FONTNAME
14436 because XListFonts (called in x_list_font) of some X server has
14437 a bug of not finding a font even if the font surely exists and
14438 is loadable by XLoadQueryFont. */
e1d6d5b9 14439 if (size > 0 && !NILP (font_names))
d5db4077 14440 fontname = (char *) SDATA (XCAR (font_names));
dc43ef94
KH
14441
14442 BLOCK_INPUT;
d645aaa4 14443 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 14444 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
14445 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14446 {
14447 /* This error is perhaps due to insufficient memory on X
14448 server. Let's just ignore it. */
14449 font = NULL;
14450 x_clear_errors (FRAME_X_DISPLAY (f));
14451 }
14452 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 14453 UNBLOCK_INPUT;
b5210ea7 14454 if (!font)
dc43ef94
KH
14455 return NULL;
14456
06a2c219
GM
14457 /* Find a free slot in the font table. */
14458 for (i = 0; i < dpyinfo->n_fonts; ++i)
14459 if (dpyinfo->font_table[i].name == NULL)
14460 break;
14461
14462 /* If no free slot found, maybe enlarge the font table. */
14463 if (i == dpyinfo->n_fonts
14464 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 14465 {
06a2c219
GM
14466 int sz;
14467 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14468 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 14469 dpyinfo->font_table
06a2c219 14470 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
14471 }
14472
06a2c219
GM
14473 fontp = dpyinfo->font_table + i;
14474 if (i == dpyinfo->n_fonts)
14475 ++dpyinfo->n_fonts;
dc43ef94
KH
14476
14477 /* Now fill in the slots of *FONTP. */
14478 BLOCK_INPUT;
14479 fontp->font = font;
06a2c219 14480 fontp->font_idx = i;
dc43ef94
KH
14481 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14482 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14483
14484 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14485 full_name = 0;
14486 if (XGetFontProperty (font, XA_FONT, &value))
14487 {
14488 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14489 char *p = name;
14490 int dashes = 0;
14491
14492 /* Count the number of dashes in the "full name".
14493 If it is too few, this isn't really the font's full name,
14494 so don't use it.
14495 In X11R4, the fonts did not come with their canonical names
14496 stored in them. */
14497 while (*p)
14498 {
14499 if (*p == '-')
14500 dashes++;
14501 p++;
14502 }
14503
14504 if (dashes >= 13)
14505 {
14506 full_name = (char *) xmalloc (p - name + 1);
14507 bcopy (name, full_name, p - name + 1);
14508 }
14509
14510 XFree (name);
14511 }
14512
14513 if (full_name != 0)
14514 fontp->full_name = full_name;
14515 else
14516 fontp->full_name = fontp->name;
14517
14518 fontp->size = font->max_bounds.width;
d5749adb 14519 fontp->height = FONT_HEIGHT (font);
dc43ef94 14520
2da424f1
KH
14521 if (NILP (font_names))
14522 {
14523 /* We come here because of a bug of XListFonts mentioned at
14524 the head of this block. Let's store this information in
14525 the cache for x_list_fonts. */
14526 Lisp_Object lispy_name = build_string (fontname);
14527 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
14528 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14529 Qnil);
2da424f1 14530
f3fbd155
KR
14531 XSETCDR (dpyinfo->name_list_element,
14532 Fcons (Fcons (key,
14533 Fcons (Fcons (lispy_full_name,
14534 make_number (fontp->size)),
14535 Qnil)),
14536 XCDR (dpyinfo->name_list_element)));
2da424f1 14537 if (full_name)
9c11f79e
GM
14538 {
14539 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14540 Qnil);
f3fbd155
KR
14541 XSETCDR (dpyinfo->name_list_element,
14542 Fcons (Fcons (key,
14543 Fcons (Fcons (lispy_full_name,
14544 make_number (fontp->size)),
14545 Qnil)),
14546 XCDR (dpyinfo->name_list_element)));
9c11f79e 14547 }
2da424f1
KH
14548 }
14549
dc43ef94
KH
14550 /* The slot `encoding' specifies how to map a character
14551 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
14552 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14553 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 14554 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 14555 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
14556 which is never used by any charset. If mapping can't be
14557 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
14558 fontp->encoding[1]
14559 = (font->max_byte1 == 0
14560 /* 1-byte font */
14561 ? (font->min_char_or_byte2 < 0x80
14562 ? (font->max_char_or_byte2 < 0x80
14563 ? 0 /* 0x20..0x7F */
8ff102bd 14564 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
14565 : 1) /* 0xA0..0xFF */
14566 /* 2-byte font */
14567 : (font->min_byte1 < 0x80
14568 ? (font->max_byte1 < 0x80
14569 ? (font->min_char_or_byte2 < 0x80
14570 ? (font->max_char_or_byte2 < 0x80
14571 ? 0 /* 0x2020..0x7F7F */
8ff102bd 14572 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 14573 : 3) /* 0x20A0..0x7FFF */
8ff102bd 14574 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
14575 : (font->min_char_or_byte2 < 0x80
14576 ? (font->max_char_or_byte2 < 0x80
14577 ? 2 /* 0xA020..0xFF7F */
8ff102bd 14578 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
14579 : 1))); /* 0xA0A0..0xFFFF */
14580
14581 fontp->baseline_offset
14582 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14583 ? (long) value : 0);
14584 fontp->relative_compose
14585 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14586 ? (long) value : 0);
f78798df
KH
14587 fontp->default_ascent
14588 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14589 ? (long) value : 0);
dc43ef94 14590
06a2c219
GM
14591 /* Set global flag fonts_changed_p to non-zero if the font loaded
14592 has a character with a smaller width than any other character
437dfb9f 14593 before, or if the font loaded has a smaller height than any
06a2c219
GM
14594 other font loaded before. If this happens, it will make a
14595 glyph matrix reallocation necessary. */
437dfb9f 14596 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 14597 UNBLOCK_INPUT;
dc43ef94
KH
14598 return fontp;
14599 }
14600}
14601
06a2c219
GM
14602
14603/* Return a pointer to struct font_info of a font named FONTNAME for
14604 frame F. If no such font is loaded, return NULL. */
14605
dc43ef94
KH
14606struct font_info *
14607x_query_font (f, fontname)
14608 struct frame *f;
14609 register char *fontname;
14610{
14611 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14612 int i;
14613
14614 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
14615 if (dpyinfo->font_table[i].name
14616 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14617 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
14618 return (dpyinfo->font_table + i);
14619 return NULL;
14620}
14621
06a2c219
GM
14622
14623/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
14624 `encoder' of the structure. */
14625
14626void
14627x_find_ccl_program (fontp)
14628 struct font_info *fontp;
14629{
a42f54e6 14630 Lisp_Object list, elt;
a6582676 14631
f9b5db02 14632 elt = Qnil;
8e713be6 14633 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 14634 {
8e713be6 14635 elt = XCAR (list);
a6582676 14636 if (CONSP (elt)
8e713be6 14637 && STRINGP (XCAR (elt))
9f2feff6
KH
14638 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14639 >= 0)
14640 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14641 >= 0)))
a42f54e6
KH
14642 break;
14643 }
f9b5db02 14644
a42f54e6
KH
14645 if (! NILP (list))
14646 {
d27f8ca7
KH
14647 struct ccl_program *ccl
14648 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 14649
8e713be6 14650 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
14651 xfree (ccl);
14652 else
14653 fontp->font_encoder = ccl;
a6582676
KH
14654 }
14655}
14656
06a2c219 14657
dc43ef94 14658\f
06a2c219
GM
14659/***********************************************************************
14660 Initialization
14661 ***********************************************************************/
f451eb13 14662
3afe33e7
RS
14663#ifdef USE_X_TOOLKIT
14664static XrmOptionDescRec emacs_options[] = {
14665 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14666 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14667
14668 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14669 XrmoptionSepArg, NULL},
14670 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14671
14672 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14673 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14674 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14675 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14676 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14677 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14678 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14679};
14680#endif /* USE_X_TOOLKIT */
14681
7a13e894
RS
14682static int x_initialized;
14683
29b38361
KH
14684#ifdef MULTI_KBOARD
14685/* Test whether two display-name strings agree up to the dot that separates
14686 the screen number from the server number. */
14687static int
14688same_x_server (name1, name2)
14689 char *name1, *name2;
14690{
14691 int seen_colon = 0;
d5db4077 14692 unsigned char *system_name = SDATA (Vsystem_name);
cf591cc1
RS
14693 int system_name_length = strlen (system_name);
14694 int length_until_period = 0;
14695
14696 while (system_name[length_until_period] != 0
14697 && system_name[length_until_period] != '.')
14698 length_until_period++;
14699
14700 /* Treat `unix' like an empty host name. */
14701 if (! strncmp (name1, "unix:", 5))
14702 name1 += 4;
14703 if (! strncmp (name2, "unix:", 5))
14704 name2 += 4;
14705 /* Treat this host's name like an empty host name. */
14706 if (! strncmp (name1, system_name, system_name_length)
14707 && name1[system_name_length] == ':')
14708 name1 += system_name_length;
14709 if (! strncmp (name2, system_name, system_name_length)
14710 && name2[system_name_length] == ':')
14711 name2 += system_name_length;
14712 /* Treat this host's domainless name like an empty host name. */
14713 if (! strncmp (name1, system_name, length_until_period)
14714 && name1[length_until_period] == ':')
14715 name1 += length_until_period;
14716 if (! strncmp (name2, system_name, length_until_period)
14717 && name2[length_until_period] == ':')
14718 name2 += length_until_period;
14719
29b38361
KH
14720 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14721 {
14722 if (*name1 == ':')
14723 seen_colon++;
14724 if (seen_colon && *name1 == '.')
14725 return 1;
14726 }
14727 return (seen_colon
14728 && (*name1 == '.' || *name1 == '\0')
14729 && (*name2 == '.' || *name2 == '\0'));
14730}
14731#endif
14732
334208b7 14733struct x_display_info *
1f8255f2 14734x_term_init (display_name, xrm_option, resource_name)
334208b7 14735 Lisp_Object display_name;
1f8255f2
RS
14736 char *xrm_option;
14737 char *resource_name;
dc6f92b8 14738{
334208b7 14739 int connection;
7a13e894 14740 Display *dpy;
334208b7
RS
14741 struct x_display_info *dpyinfo;
14742 XrmDatabase xrdb;
14743
60439948
KH
14744 BLOCK_INPUT;
14745
7a13e894
RS
14746 if (!x_initialized)
14747 {
14748 x_initialize ();
14749 x_initialized = 1;
14750 }
dc6f92b8 14751
3afe33e7 14752#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
14753 /* weiner@footloose.sps.mot.com reports that this causes
14754 errors with X11R5:
14755 X protocol error: BadAtom (invalid Atom parameter)
14756 on protocol request 18skiloaf.
14757 So let's not use it until R6. */
14758#ifdef HAVE_X11XTR6
bdcd49ba
RS
14759 XtSetLanguageProc (NULL, NULL, NULL);
14760#endif
14761
7f9c7f94
RS
14762 {
14763 int argc = 0;
14764 char *argv[3];
14765
14766 argv[0] = "";
14767 argc = 1;
14768 if (xrm_option)
14769 {
14770 argv[argc++] = "-xrm";
14771 argv[argc++] = xrm_option;
14772 }
5dbb3f46 14773 stop_polling ();
d5db4077 14774 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
7f9c7f94
RS
14775 resource_name, EMACS_CLASS,
14776 emacs_options, XtNumber (emacs_options),
14777 &argc, argv);
5dbb3f46 14778 start_polling ();
39d8bb4d
KH
14779
14780#ifdef HAVE_X11XTR6
10537cb1 14781 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 14782 fixup_locale ();
39d8bb4d 14783#endif
7f9c7f94 14784 }
3afe33e7
RS
14785
14786#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
14787#ifdef HAVE_X11R5
14788 XSetLocaleModifiers ("");
14789#endif
d5db4077 14790 dpy = XOpenDisplay (SDATA (display_name));
3afe33e7 14791#endif /* not USE_X_TOOLKIT */
334208b7 14792
7a13e894
RS
14793 /* Detect failure. */
14794 if (dpy == 0)
60439948
KH
14795 {
14796 UNBLOCK_INPUT;
14797 return 0;
14798 }
7a13e894
RS
14799
14800 /* We have definitely succeeded. Record the new connection. */
14801
14802 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 14803 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 14804
29b38361
KH
14805#ifdef MULTI_KBOARD
14806 {
14807 struct x_display_info *share;
14808 Lisp_Object tail;
14809
14810 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6 14811 share = share->next, tail = XCDR (tail))
d5db4077
KR
14812 if (same_x_server (SDATA (XCAR (XCAR (tail))),
14813 SDATA (display_name)))
29b38361
KH
14814 break;
14815 if (share)
14816 dpyinfo->kboard = share->kboard;
14817 else
14818 {
14819 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14820 init_kboard (dpyinfo->kboard);
59e755be
KH
14821 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14822 {
14823 char *vendor = ServerVendor (dpy);
9b6ed9f3 14824 UNBLOCK_INPUT;
59e755be
KH
14825 dpyinfo->kboard->Vsystem_key_alist
14826 = call1 (Qvendor_specific_keysyms,
14827 build_string (vendor ? vendor : ""));
9b6ed9f3 14828 BLOCK_INPUT;
59e755be
KH
14829 }
14830
29b38361
KH
14831 dpyinfo->kboard->next_kboard = all_kboards;
14832 all_kboards = dpyinfo->kboard;
0ad5446c
KH
14833 /* Don't let the initial kboard remain current longer than necessary.
14834 That would cause problems if a file loaded on startup tries to
06a2c219 14835 prompt in the mini-buffer. */
0ad5446c
KH
14836 if (current_kboard == initial_kboard)
14837 current_kboard = dpyinfo->kboard;
29b38361
KH
14838 }
14839 dpyinfo->kboard->reference_count++;
14840 }
b9737ad3
KH
14841#endif
14842
7a13e894
RS
14843 /* Put this display on the chain. */
14844 dpyinfo->next = x_display_list;
14845 x_display_list = dpyinfo;
14846
14847 /* Put it on x_display_name_list as well, to keep them parallel. */
14848 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14849 x_display_name_list);
8e713be6 14850 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
14851
14852 dpyinfo->display = dpy;
dc6f92b8 14853
dc6f92b8 14854#if 0
7a13e894 14855 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 14856#endif /* ! 0 */
7a13e894
RS
14857
14858 dpyinfo->x_id_name
d5db4077
KR
14859 = (char *) xmalloc (SBYTES (Vinvocation_name)
14860 + SBYTES (Vsystem_name)
7a13e894
RS
14861 + 2);
14862 sprintf (dpyinfo->x_id_name, "%s@%s",
d5db4077 14863 SDATA (Vinvocation_name), SDATA (Vsystem_name));
28430d3c
JB
14864
14865 /* Figure out which modifier bits mean what. */
334208b7 14866 x_find_modifier_meanings (dpyinfo);
f451eb13 14867
ab648270 14868 /* Get the scroll bar cursor. */
7a13e894 14869 dpyinfo->vertical_scroll_bar_cursor
334208b7 14870 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 14871
334208b7
RS
14872 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14873 resource_name, EMACS_CLASS);
14874#ifdef HAVE_XRMSETDATABASE
14875 XrmSetDatabase (dpyinfo->display, xrdb);
14876#else
14877 dpyinfo->display->db = xrdb;
14878#endif
547d9db8 14879 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
14880 all versions. */
14881 dpyinfo->xrdb = xrdb;
334208b7
RS
14882
14883 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14884 DefaultScreen (dpyinfo->display));
5ff67d81 14885 select_visual (dpyinfo);
43bd1b2b 14886 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
14887 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14888 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14889 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14890 dpyinfo->grabbed = 0;
14891 dpyinfo->reference_count = 0;
14892 dpyinfo->icon_bitmap_id = -1;
06a2c219 14893 dpyinfo->font_table = NULL;
7a13e894
RS
14894 dpyinfo->n_fonts = 0;
14895 dpyinfo->font_table_size = 0;
14896 dpyinfo->bitmaps = 0;
14897 dpyinfo->bitmaps_size = 0;
14898 dpyinfo->bitmaps_last = 0;
14899 dpyinfo->scratch_cursor_gc = 0;
14900 dpyinfo->mouse_face_mouse_frame = 0;
14901 dpyinfo->mouse_face_deferred_gc = 0;
14902 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14903 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 14904 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 14905 dpyinfo->mouse_face_window = Qnil;
0a61c667 14906 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
14907 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14908 dpyinfo->mouse_face_defer = 0;
66301061 14909 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
14910 dpyinfo->x_focus_frame = 0;
14911 dpyinfo->x_focus_event_frame = 0;
14912 dpyinfo->x_highlight_frame = 0;
06a2c219 14913 dpyinfo->image_cache = make_image_cache ();
334208b7 14914
43bd1b2b 14915 /* See if a private colormap is requested. */
5ff67d81
GM
14916 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14917 {
14918 if (dpyinfo->visual->class == PseudoColor)
14919 {
14920 Lisp_Object value;
14921 value = display_x_get_resource (dpyinfo,
14922 build_string ("privateColormap"),
14923 build_string ("PrivateColormap"),
14924 Qnil, Qnil);
14925 if (STRINGP (value)
d5db4077
KR
14926 && (!strcmp (SDATA (value), "true")
14927 || !strcmp (SDATA (value), "on")))
5ff67d81
GM
14928 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14929 }
43bd1b2b 14930 }
5ff67d81
GM
14931 else
14932 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14933 dpyinfo->visual, AllocNone);
43bd1b2b 14934
06a2c219
GM
14935 {
14936 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14937 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14938 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14939 dpyinfo->resy = pixels * 25.4 / mm;
14940 pixels = DisplayWidth (dpyinfo->display, screen_number);
14941 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14942 dpyinfo->resx = pixels * 25.4 / mm;
14943 }
14944
334208b7
RS
14945 dpyinfo->Xatom_wm_protocols
14946 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14947 dpyinfo->Xatom_wm_take_focus
14948 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14949 dpyinfo->Xatom_wm_save_yourself
14950 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14951 dpyinfo->Xatom_wm_delete_window
14952 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14953 dpyinfo->Xatom_wm_change_state
14954 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14955 dpyinfo->Xatom_wm_configure_denied
14956 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14957 dpyinfo->Xatom_wm_window_moved
14958 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14959 dpyinfo->Xatom_editres
14960 = XInternAtom (dpyinfo->display, "Editres", False);
14961 dpyinfo->Xatom_CLIPBOARD
14962 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14963 dpyinfo->Xatom_TIMESTAMP
14964 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14965 dpyinfo->Xatom_TEXT
14966 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
14967 dpyinfo->Xatom_COMPOUND_TEXT
14968 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
14969 dpyinfo->Xatom_DELETE
14970 = XInternAtom (dpyinfo->display, "DELETE", False);
14971 dpyinfo->Xatom_MULTIPLE
14972 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14973 dpyinfo->Xatom_INCR
14974 = XInternAtom (dpyinfo->display, "INCR", False);
14975 dpyinfo->Xatom_EMACS_TMP
14976 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14977 dpyinfo->Xatom_TARGETS
14978 = XInternAtom (dpyinfo->display, "TARGETS", False);
14979 dpyinfo->Xatom_NULL
14980 = XInternAtom (dpyinfo->display, "NULL", False);
14981 dpyinfo->Xatom_ATOM_PAIR
14982 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
14983 /* For properties of font. */
14984 dpyinfo->Xatom_PIXEL_SIZE
14985 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14986 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14987 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14988 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14989 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
14990 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14991 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 14992
06a2c219
GM
14993 /* Ghostscript support. */
14994 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14995 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14996
14997 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14998 False);
14999
547d9db8
KH
15000 dpyinfo->cut_buffers_initialized = 0;
15001
334208b7
RS
15002 connection = ConnectionNumber (dpyinfo->display);
15003 dpyinfo->connection = connection;
15004
dc43ef94 15005 {
5d7cc324
RS
15006 char null_bits[1];
15007
15008 null_bits[0] = 0x00;
dc43ef94
KH
15009
15010 dpyinfo->null_pixel
15011 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15012 null_bits, 1, 1, (long) 0, (long) 0,
15013 1);
15014 }
15015
06a2c219
GM
15016 {
15017 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 15018 extern char *gray_bitmap_bits;
06a2c219
GM
15019 dpyinfo->gray
15020 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15021 gray_bitmap_bits,
15022 gray_bitmap_width, gray_bitmap_height,
15023 (unsigned long) 1, (unsigned long) 0, 1);
15024 }
15025
f5d11644
GM
15026#ifdef HAVE_X_I18N
15027 xim_initialize (dpyinfo, resource_name);
15028#endif
15029
87485d6f
MW
15030#ifdef subprocesses
15031 /* This is only needed for distinguishing keyboard and process input. */
334208b7 15032 if (connection != 0)
7a13e894 15033 add_keyboard_wait_descriptor (connection);
87485d6f 15034#endif
6d4238f3 15035
041b69ac 15036#ifndef F_SETOWN_BUG
dc6f92b8 15037#ifdef F_SETOWN
dc6f92b8 15038#ifdef F_SETOWN_SOCK_NEG
61c3ce62 15039 /* stdin is a socket here */
334208b7 15040 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 15041#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 15042 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
15043#endif /* ! defined (F_SETOWN_SOCK_NEG) */
15044#endif /* ! defined (F_SETOWN) */
041b69ac 15045#endif /* F_SETOWN_BUG */
dc6f92b8
JB
15046
15047#ifdef SIGIO
eee20f6a
KH
15048 if (interrupt_input)
15049 init_sigio (connection);
c118dd06 15050#endif /* ! defined (SIGIO) */
dc6f92b8 15051
51b592fb 15052#ifdef USE_LUCID
f8c39f51 15053#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
15054 /* Make sure that we have a valid font for dialog boxes
15055 so that Xt does not crash. */
15056 {
15057 Display *dpy = dpyinfo->display;
15058 XrmValue d, fr, to;
15059 Font font;
e99db5a1 15060 int count;
51b592fb
RS
15061
15062 d.addr = (XPointer)&dpy;
15063 d.size = sizeof (Display *);
15064 fr.addr = XtDefaultFont;
15065 fr.size = sizeof (XtDefaultFont);
15066 to.size = sizeof (Font *);
15067 to.addr = (XPointer)&font;
e99db5a1 15068 count = x_catch_errors (dpy);
51b592fb
RS
15069 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15070 abort ();
15071 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15072 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 15073 x_uncatch_errors (dpy, count);
51b592fb
RS
15074 }
15075#endif
f8c39f51 15076#endif
51b592fb 15077
34e23e5a
GM
15078 /* See if we should run in synchronous mode. This is useful
15079 for debugging X code. */
15080 {
15081 Lisp_Object value;
15082 value = display_x_get_resource (dpyinfo,
15083 build_string ("synchronous"),
15084 build_string ("Synchronous"),
15085 Qnil, Qnil);
15086 if (STRINGP (value)
d5db4077
KR
15087 && (!strcmp (SDATA (value), "true")
15088 || !strcmp (SDATA (value), "on")))
34e23e5a
GM
15089 XSynchronize (dpyinfo->display, True);
15090 }
15091
60439948
KH
15092 UNBLOCK_INPUT;
15093
7a13e894
RS
15094 return dpyinfo;
15095}
15096\f
15097/* Get rid of display DPYINFO, assuming all frames are already gone,
15098 and without sending any more commands to the X server. */
dc6f92b8 15099
7a13e894
RS
15100void
15101x_delete_display (dpyinfo)
15102 struct x_display_info *dpyinfo;
15103{
15104 delete_keyboard_wait_descriptor (dpyinfo->connection);
15105
15106 /* Discard this display from x_display_name_list and x_display_list.
15107 We can't use Fdelq because that can quit. */
15108 if (! NILP (x_display_name_list)
8e713be6
KR
15109 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15110 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
15111 else
15112 {
15113 Lisp_Object tail;
15114
15115 tail = x_display_name_list;
8e713be6 15116 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 15117 {
bffcfca9 15118 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 15119 {
f3fbd155 15120 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
15121 break;
15122 }
8e713be6 15123 tail = XCDR (tail);
7a13e894
RS
15124 }
15125 }
15126
9bda743f
GM
15127 if (next_noop_dpyinfo == dpyinfo)
15128 next_noop_dpyinfo = dpyinfo->next;
15129
7a13e894
RS
15130 if (x_display_list == dpyinfo)
15131 x_display_list = dpyinfo->next;
7f9c7f94
RS
15132 else
15133 {
15134 struct x_display_info *tail;
7a13e894 15135
7f9c7f94
RS
15136 for (tail = x_display_list; tail; tail = tail->next)
15137 if (tail->next == dpyinfo)
15138 tail->next = tail->next->next;
15139 }
7a13e894 15140
0d777288
RS
15141#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15142#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
15143 XrmDestroyDatabase (dpyinfo->xrdb);
15144#endif
0d777288 15145#endif
29b38361
KH
15146#ifdef MULTI_KBOARD
15147 if (--dpyinfo->kboard->reference_count == 0)
39f79001 15148 delete_kboard (dpyinfo->kboard);
b9737ad3 15149#endif
f5d11644
GM
15150#ifdef HAVE_X_I18N
15151 if (dpyinfo->xim)
15152 xim_close_dpy (dpyinfo);
15153#endif
15154
b9737ad3
KH
15155 xfree (dpyinfo->font_table);
15156 xfree (dpyinfo->x_id_name);
f04e1297 15157 xfree (dpyinfo->color_cells);
b9737ad3 15158 xfree (dpyinfo);
7a13e894 15159}
f04e1297 15160
7a13e894
RS
15161\f
15162/* Set up use of X before we make the first connection. */
15163
06a2c219
GM
15164static struct redisplay_interface x_redisplay_interface =
15165{
15166 x_produce_glyphs,
15167 x_write_glyphs,
15168 x_insert_glyphs,
15169 x_clear_end_of_line,
15170 x_scroll_run,
15171 x_after_update_window_line,
15172 x_update_window_begin,
15173 x_update_window_end,
15174 XTcursor_to,
15175 x_flush,
71b8321e 15176 x_clear_mouse_face,
66ac4b0e
GM
15177 x_get_glyph_overhangs,
15178 x_fix_overlapping_area
06a2c219
GM
15179};
15180
dfcf069d 15181void
7a13e894
RS
15182x_initialize ()
15183{
06a2c219
GM
15184 rif = &x_redisplay_interface;
15185
15186 clear_frame_hook = x_clear_frame;
15187 ins_del_lines_hook = x_ins_del_lines;
06a2c219 15188 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
15189 ring_bell_hook = XTring_bell;
15190 reset_terminal_modes_hook = XTreset_terminal_modes;
15191 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
15192 update_begin_hook = x_update_begin;
15193 update_end_hook = x_update_end;
dc6f92b8
JB
15194 set_terminal_window_hook = XTset_terminal_window;
15195 read_socket_hook = XTread_socket;
b8009dd1 15196 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 15197 mouse_position_hook = XTmouse_position;
f451eb13 15198 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 15199 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
15200 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15201 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15202 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15203 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 15204 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 15205
f676886a 15206 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 15207 char_ins_del_ok = 1;
dc6f92b8
JB
15208 line_ins_del_ok = 1; /* we'll just blt 'em */
15209 fast_clear_end_of_line = 1; /* X does this well */
58769bee 15210 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
15211 off the bottom */
15212 baud_rate = 19200;
15213
7a13e894 15214 x_noop_count = 0;
9ea173e8 15215 last_tool_bar_item = -1;
06a2c219
GM
15216 any_help_event_p = 0;
15217
b30b24cb
RS
15218 /* Try to use interrupt input; if we can't, then start polling. */
15219 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15220
7f9c7f94
RS
15221#ifdef USE_X_TOOLKIT
15222 XtToolkitInitialize ();
651f03b6 15223
7f9c7f94 15224 Xt_app_con = XtCreateApplicationContext ();
651f03b6
GM
15225
15226 /* Register a converter from strings to pixels, which uses
15227 Emacs' color allocation infrastructure. */
15228 XtAppSetTypeConverter (Xt_app_con,
15229 XtRString, XtRPixel, cvt_string_to_pixel,
15230 cvt_string_to_pixel_args,
15231 XtNumber (cvt_string_to_pixel_args),
15232 XtCacheByDisplay, cvt_pixel_dtor);
15233
665881ad 15234 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
15235
15236 /* Install an asynchronous timer that processes Xt timeout events
15237 every 0.1s. This is necessary because some widget sets use
15238 timeouts internally, for example the LessTif menu bar, or the
15239 Xaw3d scroll bar. When Xt timouts aren't processed, these
15240 widgets don't behave normally. */
15241 {
15242 EMACS_TIME interval;
15243 EMACS_SET_SECS_USECS (interval, 0, 100000);
15244 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15245 }
db74249b 15246#endif
bffcfca9 15247
eccc05db 15248#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
15249 xaw3d_arrow_scroll = False;
15250 xaw3d_pick_top = True;
7f9c7f94
RS
15251#endif
15252
58769bee 15253 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 15254 original error handler. */
e99db5a1 15255 XSetErrorHandler (x_error_handler);
334208b7 15256 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 15257
06a2c219 15258 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
15259#ifdef SIGWINCH
15260 signal (SIGWINCH, SIG_DFL);
a59facca 15261#endif /* SIGWINCH */
dc6f92b8 15262
92e2441b 15263 signal (SIGPIPE, x_connection_signal);
5f30b957
JD
15264
15265#ifdef HAVE_X_SM
15266 x_session_initialize ();
15267#endif
dc6f92b8 15268}
55123275 15269
06a2c219 15270
55123275
JB
15271void
15272syms_of_xterm ()
15273{
e99db5a1
RS
15274 staticpro (&x_error_message_string);
15275 x_error_message_string = Qnil;
15276
7a13e894
RS
15277 staticpro (&x_display_name_list);
15278 x_display_name_list = Qnil;
334208b7 15279
ab648270 15280 staticpro (&last_mouse_scroll_bar);
e53cb100 15281 last_mouse_scroll_bar = Qnil;
59e755be
KH
15282
15283 staticpro (&Qvendor_specific_keysyms);
15284 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 15285
c997eae5
SM
15286 staticpro (&Qutf_8);
15287 Qutf_8 = intern ("utf-8");
15288 staticpro (&Qlatin_1);
15289 Qlatin_1 = intern ("latin-1");
15290
2237cac9
RS
15291 staticpro (&last_mouse_press_frame);
15292 last_mouse_press_frame = Qnil;
06a2c219 15293
06a2c219 15294 help_echo = Qnil;
be010514
GM
15295 staticpro (&help_echo);
15296 help_echo_object = Qnil;
15297 staticpro (&help_echo_object);
7cea38bc
GM
15298 help_echo_window = Qnil;
15299 staticpro (&help_echo_window);
06a2c219 15300 previous_help_echo = Qnil;
be010514
GM
15301 staticpro (&previous_help_echo);
15302 help_echo_pos = -1;
06a2c219 15303
ee8ceff8 15304 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
503e457e 15305 doc: /* *Non-nil means autoselect window with mouse pointer. */);
ee8ceff8 15306 mouse_autoselect_window = 0;
503e457e 15307
7ee72033
MB
15308 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15309 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
228299fa
GM
15310For example, if a block cursor is over a tab, it will be drawn as
15311wide as that tab on the display. */);
06a2c219
GM
15312 x_stretch_cursor_p = 0;
15313
a72d5ce5 15314 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
15315 &x_use_underline_position_properties,
15316 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 15317nil means ignore them. If you encounter fonts with bogus
228299fa
GM
15318UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15319to 4.1, set this to nil. */);
a72d5ce5
GM
15320 x_use_underline_position_properties = 1;
15321
7ee72033
MB
15322 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15323 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
15324A value of nil means Emacs doesn't use X toolkit scroll bars.
15325Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 15326#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
15327#ifdef USE_MOTIF
15328 Vx_toolkit_scroll_bars = intern ("motif");
15329#elif defined HAVE_XAW3D
15330 Vx_toolkit_scroll_bars = intern ("xaw3d");
15331#else
15332 Vx_toolkit_scroll_bars = intern ("xaw");
15333#endif
06a2c219 15334#else
5bf04520 15335 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
15336#endif
15337
06a2c219
GM
15338 staticpro (&last_mouse_motion_frame);
15339 last_mouse_motion_frame = Qnil;
98659da6
KG
15340
15341 Qmodifier_value = intern ("modifier-value");
15342 Qalt = intern ("alt");
15343 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15344 Qhyper = intern ("hyper");
15345 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15346 Qmeta = intern ("meta");
15347 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15348 Qsuper = intern ("super");
15349 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15350
15351 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15352 doc: /* Which keys Emacs uses for the alt modifier.
15353This should be one of the symbols `alt', `hyper', `meta', `super'.
15354For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15355is nil, which is the same as `alt'. */);
15356 Vx_alt_keysym = Qnil;
15357
15358 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15359 doc: /* Which keys Emacs uses for the hyper modifier.
15360This should be one of the symbols `alt', `hyper', `meta', `super'.
15361For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15362default is nil, which is the same as `hyper'. */);
15363 Vx_hyper_keysym = Qnil;
15364
15365 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15366 doc: /* Which keys Emacs uses for the meta modifier.
15367This should be one of the symbols `alt', `hyper', `meta', `super'.
15368For example, `meta' means use the Meta_L and Meta_R keysyms. The
15369default is nil, which is the same as `meta'. */);
15370 Vx_meta_keysym = Qnil;
15371
15372 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15373 doc: /* Which keys Emacs uses for the super modifier.
15374This should be one of the symbols `alt', `hyper', `meta', `super'.
15375For example, `super' means use the Super_L and Super_R keysyms. The
15376default is nil, which is the same as `super'. */);
15377 Vx_super_keysym = Qnil;
15378
55123275 15379}
6cf0ae86 15380
1d6c120a 15381#endif /* HAVE_X_WINDOWS */