(decode_coding_emacs_mule): Check coding->cmp_data.
[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 *));
4f53f3b8
RS
488static void expose_overlaps P_ ((struct window *, struct glyph_row *,
489 struct glyph_row *));
a39202f6 490static int expose_window P_ ((struct window *, XRectangle *));
06a2c219
GM
491static void expose_area P_ ((struct window *, struct glyph_row *,
492 XRectangle *, enum glyph_row_area));
82f053ab 493static int expose_line P_ ((struct window *, struct glyph_row *,
06a2c219
GM
494 XRectangle *));
495static void x_update_cursor_in_window_tree P_ ((struct window *, int));
496static void x_update_window_cursor P_ ((struct window *, int));
497static void x_erase_phys_cursor P_ ((struct window *));
498void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
3f332ef3 499static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
976b73d7 500 enum fringe_bitmap_type, int left_p));
06a2c219
GM
501
502static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
503 GC, int));
504static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
3f332ef3 505static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
60626bab
GM
506static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
507 int, int, int, int));
06a2c219 508static void x_flush P_ ((struct frame *f));
952291d9
GM
509static void x_update_begin P_ ((struct frame *));
510static void x_update_window_begin P_ ((struct window *));
511static void x_draw_vertical_border P_ ((struct window *));
512static void x_after_update_window_line P_ ((struct glyph_row *));
513static INLINE void take_vertical_position_into_account P_ ((struct it *));
514static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
515static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
516static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
517 enum scroll_bar_part *,
518 Lisp_Object *, Lisp_Object *,
519 unsigned long *));
d0fa3e56
EZ
520static void x_check_fullscreen P_ ((struct frame *));
521static void x_check_fullscreen_move P_ ((struct frame *));
06a2c219
GM
522
523/* Flush display of frame F, or of all frames if F is null. */
524
525static void
526x_flush (f)
527 struct frame *f;
528{
529 BLOCK_INPUT;
530 if (f == NULL)
531 {
532 Lisp_Object rest, frame;
533 FOR_EACH_FRAME (rest, frame)
534 x_flush (XFRAME (frame));
535 }
536 else if (FRAME_X_P (f))
537 XFlush (FRAME_X_DISPLAY (f));
538 UNBLOCK_INPUT;
539}
540
dc6f92b8 541
06a2c219
GM
542/* Remove calls to XFlush by defining XFlush to an empty replacement.
543 Calls to XFlush should be unnecessary because the X output buffer
544 is flushed automatically as needed by calls to XPending,
545 XNextEvent, or XWindowEvent according to the XFlush man page.
546 XTread_socket calls XPending. Removing XFlush improves
547 performance. */
548
549#define XFlush(DISPLAY) (void) 0
b8009dd1 550
334208b7 551\f
06a2c219
GM
552/***********************************************************************
553 Debugging
554 ***********************************************************************/
555
9382638d 556#if 0
06a2c219
GM
557
558/* This is a function useful for recording debugging information about
559 the sequence of occurrences in this file. */
9382638d
KH
560
561struct record
562{
563 char *locus;
564 int type;
565};
566
567struct record event_record[100];
568
569int event_record_index;
570
571record_event (locus, type)
572 char *locus;
573 int type;
574{
575 if (event_record_index == sizeof (event_record) / sizeof (struct record))
576 event_record_index = 0;
577
578 event_record[event_record_index].locus = locus;
579 event_record[event_record_index].type = type;
580 event_record_index++;
581}
582
583#endif /* 0 */
06a2c219
GM
584
585
9382638d 586\f
334208b7
RS
587/* Return the struct x_display_info corresponding to DPY. */
588
589struct x_display_info *
590x_display_info_for_display (dpy)
591 Display *dpy;
592{
593 struct x_display_info *dpyinfo;
594
595 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
596 if (dpyinfo->display == dpy)
597 return dpyinfo;
16bd92ea 598
334208b7
RS
599 return 0;
600}
f451eb13 601
06a2c219
GM
602
603\f
604/***********************************************************************
605 Starting and ending an update
606 ***********************************************************************/
607
608/* Start an update of frame F. This function is installed as a hook
609 for update_begin, i.e. it is called when update_begin is called.
610 This function is called prior to calls to x_update_window_begin for
611 each window being updated. Currently, there is nothing to do here
612 because all interesting stuff is done on a window basis. */
dc6f92b8 613
dfcf069d 614static void
06a2c219 615x_update_begin (f)
f676886a 616 struct frame *f;
58769bee 617{
06a2c219
GM
618 /* Nothing to do. */
619}
dc6f92b8 620
dc6f92b8 621
06a2c219
GM
622/* Start update of window W. Set the global variable updated_window
623 to the window being updated and set output_cursor to the cursor
624 position of W. */
dc6f92b8 625
06a2c219
GM
626static void
627x_update_window_begin (w)
628 struct window *w;
629{
630 struct frame *f = XFRAME (WINDOW_FRAME (w));
631 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
632
633 updated_window = w;
634 set_output_cursor (&w->cursor);
b8009dd1 635
06a2c219 636 BLOCK_INPUT;
d1bc4182 637
06a2c219 638 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 639 {
514e4681 640 /* Don't do highlighting for mouse motion during the update. */
06a2c219 641 display_info->mouse_face_defer = 1;
37c2c98b 642
06a2c219
GM
643 /* If F needs to be redrawn, simply forget about any prior mouse
644 highlighting. */
9f67f20b 645 if (FRAME_GARBAGED_P (f))
06a2c219
GM
646 display_info->mouse_face_window = Qnil;
647
64f26cf5
GM
648#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
649 their mouse_face_p flag set, which means that they are always
650 unequal to rows in a desired matrix which never have that
651 flag set. So, rows containing mouse-face glyphs are never
652 scrolled, and we don't have to switch the mouse highlight off
653 here to prevent it from being scrolled. */
654
06a2c219
GM
655 /* Can we tell that this update does not affect the window
656 where the mouse highlight is? If so, no need to turn off.
657 Likewise, don't do anything if the frame is garbaged;
658 in that case, the frame's current matrix that we would use
659 is all wrong, and we will redisplay that line anyway. */
660 if (!NILP (display_info->mouse_face_window)
661 && w == XWINDOW (display_info->mouse_face_window))
514e4681 662 {
06a2c219 663 int i;
514e4681 664
06a2c219
GM
665 for (i = 0; i < w->desired_matrix->nrows; ++i)
666 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
667 break;
668
06a2c219
GM
669 if (i < w->desired_matrix->nrows)
670 clear_mouse_face (display_info);
514e4681 671 }
64f26cf5 672#endif /* 0 */
b8009dd1 673 }
6ccf47d1 674
dc6f92b8
JB
675 UNBLOCK_INPUT;
676}
677
06a2c219
GM
678
679/* Draw a vertical window border to the right of window W if W doesn't
680 have vertical scroll bars. */
681
dfcf069d 682static void
06a2c219
GM
683x_draw_vertical_border (w)
684 struct window *w;
58769bee 685{
06a2c219
GM
686 struct frame *f = XFRAME (WINDOW_FRAME (w));
687
688 /* Redraw borders between horizontally adjacent windows. Don't
689 do it for frames with vertical scroll bars because either the
690 right scroll bar of a window, or the left scroll bar of its
691 neighbor will suffice as a border. */
692 if (!WINDOW_RIGHTMOST_P (w)
693 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
694 {
695 int x0, x1, y0, y1;
dc6f92b8 696
06a2c219 697 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
3f332ef3 698 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
06a2c219
GM
699 y1 -= 1;
700
701 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
702 f->output_data.x->normal_gc, x1, y0, x1, y1);
703 }
704}
705
706
71b8321e
GM
707/* End update of window W (which is equal to updated_window).
708
709 Draw vertical borders between horizontally adjacent windows, and
710 display W's cursor if CURSOR_ON_P is non-zero.
711
712 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
713 glyphs in mouse-face were overwritten. In that case we have to
714 make sure that the mouse-highlight is properly redrawn.
715
716 W may be a menu bar pseudo-window in case we don't have X toolkit
717 support. Such windows don't have a cursor, so don't display it
718 here. */
06a2c219
GM
719
720static void
71b8321e 721x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 722 struct window *w;
71b8321e 723 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 724{
140330de
GM
725 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
726
06a2c219
GM
727 if (!w->pseudo_window_p)
728 {
729 BLOCK_INPUT;
71b8321e 730
06a2c219
GM
731 if (cursor_on_p)
732 x_display_and_set_cursor (w, 1, output_cursor.hpos,
733 output_cursor.vpos,
734 output_cursor.x, output_cursor.y);
71b8321e 735
06a2c219
GM
736 x_draw_vertical_border (w);
737 UNBLOCK_INPUT;
738 }
739
140330de
GM
740 /* If a row with mouse-face was overwritten, arrange for
741 XTframe_up_to_date to redisplay the mouse highlight. */
742 if (mouse_face_overwritten_p)
743 {
744 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
745 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
746 dpyinfo->mouse_face_window = Qnil;
747 }
748
06a2c219
GM
749 updated_window = NULL;
750}
dc6f92b8 751
dc6f92b8 752
06a2c219
GM
753/* End update of frame F. This function is installed as a hook in
754 update_end. */
755
756static void
757x_update_end (f)
758 struct frame *f;
759{
760 /* Mouse highlight may be displayed again. */
aa8bff2e 761 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 762
06a2c219 763 BLOCK_INPUT;
334208b7 764 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
765 UNBLOCK_INPUT;
766}
b8009dd1 767
06a2c219
GM
768
769/* This function is called from various places in xdisp.c whenever a
770 complete update has been performed. The global variable
771 updated_window is not available here. */
b8009dd1 772
dfcf069d 773static void
b8009dd1 774XTframe_up_to_date (f)
06a2c219 775 struct frame *f;
b8009dd1 776{
06a2c219 777 if (FRAME_X_P (f))
514e4681 778 {
06a2c219 779 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 780
06a2c219
GM
781 if (dpyinfo->mouse_face_deferred_gc
782 || f == dpyinfo->mouse_face_mouse_frame)
783 {
784 BLOCK_INPUT;
785 if (dpyinfo->mouse_face_mouse_frame)
786 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
787 dpyinfo->mouse_face_mouse_x,
788 dpyinfo->mouse_face_mouse_y);
789 dpyinfo->mouse_face_deferred_gc = 0;
790 UNBLOCK_INPUT;
791 }
514e4681 792 }
b8009dd1 793}
06a2c219
GM
794
795
796/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 797 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
798 before DESIRED_ROW is made current. The window being updated is
799 found in updated_window. This function It is called from
800 update_window_line only if it is known that there are differences
801 between bitmaps to be drawn between current row and DESIRED_ROW. */
802
803static void
804x_after_update_window_line (desired_row)
805 struct glyph_row *desired_row;
806{
807 struct window *w = updated_window;
ef253080 808 struct frame *f;
259cf6bc 809 int width, height;
06a2c219
GM
810
811 xassert (w);
812
813 if (!desired_row->mode_line_p && !w->pseudo_window_p)
814 {
815 BLOCK_INPUT;
3f332ef3 816 x_draw_row_fringe_bitmaps (w, desired_row);
ef253080
GM
817 UNBLOCK_INPUT;
818 }
819
820 /* When a window has disappeared, make sure that no rest of
821 full-width rows stays visible in the internal border. Could
822 check here if updated_window is the leftmost/rightmost window,
823 but I guess it's not worth doing since vertically split windows
824 are almost never used, internal border is rarely set, and the
825 overhead is very small. */
826 if (windows_or_buffers_changed
827 && desired_row->full_width_p
828 && (f = XFRAME (w->frame),
829 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
830 width != 0)
831 && (height = desired_row->visible_height,
832 height > 0))
ef253080 833 {
ef253080 834 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 835
ef253080
GM
836 /* Internal border is drawn below the tool bar. */
837 if (WINDOWP (f->tool_bar_window)
838 && w == XWINDOW (f->tool_bar_window))
839 y -= width;
06a2c219 840
ef253080
GM
841 BLOCK_INPUT;
842 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
843 0, y, width, height, False);
844 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
845 f->output_data.x->pixel_width - width,
846 y, width, height, False);
06a2c219
GM
847 UNBLOCK_INPUT;
848 }
849}
850
851
3f332ef3 852/* Draw the bitmap WHICH in one of the left or right fringes of
06a2c219
GM
853 window W. ROW is the glyph row for which to display the bitmap; it
854 determines the vertical position at which the bitmap has to be
855 drawn. */
856
857static void
976b73d7 858x_draw_fringe_bitmap (w, row, which, left_p)
06a2c219
GM
859 struct window *w;
860 struct glyph_row *row;
3f332ef3 861 enum fringe_bitmap_type which;
976b73d7 862 int left_p;
06a2c219
GM
863{
864 struct frame *f = XFRAME (WINDOW_FRAME (w));
865 Display *display = FRAME_X_DISPLAY (f);
866 Window window = FRAME_X_WINDOW (f);
867 int x, y, wd, h, dy;
976b73d7 868 int b1, b2;
b436feb6 869 unsigned char *bits = NULL;
06a2c219
GM
870 Pixmap pixmap;
871 GC gc = f->output_data.x->normal_gc;
872 struct face *face;
873 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
874
875 /* Must clip because of partially visible lines. */
876 x_clip_to_row (w, row, gc, 1);
877
976b73d7
KS
878 /* Convert row to frame coordinates. */
879 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
880
06a2c219
GM
881 switch (which)
882 {
976b73d7
KS
883 case NO_FRINGE_BITMAP:
884 wd = 0;
885 h = 0;
886 break;
887
06a2c219
GM
888 case LEFT_TRUNCATION_BITMAP:
889 wd = left_width;
890 h = left_height;
891 bits = left_bits;
06a2c219
GM
892 break;
893
894 case OVERLAY_ARROW_BITMAP:
976b73d7
KS
895 wd = ov_width;
896 h = ov_height;
06a2c219 897 bits = ov_bits;
06a2c219
GM
898 break;
899
900 case RIGHT_TRUNCATION_BITMAP:
901 wd = right_width;
902 h = right_height;
903 bits = right_bits;
06a2c219
GM
904 break;
905
906 case CONTINUED_LINE_BITMAP:
976b73d7
KS
907 wd = continued_width;
908 h = continued_height;
06a2c219 909 bits = continued_bits;
06a2c219
GM
910 break;
911
912 case CONTINUATION_LINE_BITMAP:
913 wd = continuation_width;
914 h = continuation_height;
915 bits = continuation_bits;
06a2c219
GM
916 break;
917
918 case ZV_LINE_BITMAP:
919 wd = zv_width;
976b73d7
KS
920 h = zv_height - (y % zv_period);
921 bits = zv_bits + (y % zv_period);
06a2c219
GM
922 break;
923
924 default:
925 abort ();
926 }
927
976b73d7
KS
928 /* Clip bitmap if too high. */
929 if (h > row->height)
930 h = row->height;
931
932 /* Set dy to the offset in the row to start drawing the bitmap. */
06a2c219
GM
933 dy = (row->height - h) / 2;
934
976b73d7
KS
935 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
936 PREPARE_FACE_FOR_DISPLAY (f, face);
937
938 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
939 the fringe. */
dbdc9702 940 b1 = b2 = -1;
976b73d7
KS
941 if (left_p)
942 {
943 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
944 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
945 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
946 - wd
947 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
948 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
949 {
950 /* If W has a vertical border to its left, don't draw over it. */
951 int border = ((XFASTINT (w->left) > 0
952 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
953 ? 1 : 0);
954 b1 = (window_box_left (w, -1)
955 - FRAME_X_LEFT_FRINGE_WIDTH (f)
956 + border);
957 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
958 }
959 }
960 else
961 {
962 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
963 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
964 x = (window_box_right (w, -1)
965 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
966 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
967 the fringe. */
968 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
969 {
970 b1 = window_box_right (w, -1);
971 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
972 }
973 }
974
975 if (b1 >= 0)
976 {
977 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
978
979 /* In case the same realized face is used for fringes and
980 for something displayed in the text (e.g. face `region' on
981 mono-displays, the fill style may have been changed to
982 FillSolid in x_draw_glyph_string_background. */
983 if (face->stipple)
984 XSetFillStyle (display, face->gc, FillOpaqueStippled);
985 else
986 XSetForeground (display, face->gc, face->background);
987
988 XFillRectangle (display, window, face->gc,
989 b1,
990 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
991 row->y)),
992 b2,
993 row->visible_height);
994 if (!face->stipple)
995 XSetForeground (display, face->gc, face->foreground);
996 }
997
dbdc9702
GM
998 if (which != NO_FRINGE_BITMAP)
999 {
1000 /* Draw the bitmap. I believe these small pixmaps can be cached
1001 by the server. */
1002 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
1003 face->foreground,
1004 face->background, depth);
1005 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
1006 XFreePixmap (display, pixmap);
1007 }
1008
06a2c219
GM
1009 XSetClipMask (display, gc, None);
1010}
1011
1012
3f332ef3 1013/* Draw fringe bitmaps for glyph row ROW on window W. Call this
06a2c219
GM
1014 function with input blocked. */
1015
1016static void
3f332ef3 1017x_draw_row_fringe_bitmaps (w, row)
06a2c219
GM
1018 struct window *w;
1019 struct glyph_row *row;
1020{
1021 struct frame *f = XFRAME (w->frame);
3f332ef3 1022 enum fringe_bitmap_type bitmap;
06a2c219
GM
1023
1024 xassert (interrupt_input_blocked);
1025
1026 /* If row is completely invisible, because of vscrolling, we
1027 don't have to draw anything. */
1028 if (row->visible_height <= 0)
1029 return;
1030
976b73d7
KS
1031 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1032 {
1033 /* Decide which bitmap to draw in the left fringe. */
1034 if (row->overlay_arrow_p)
1035 bitmap = OVERLAY_ARROW_BITMAP;
1036 else if (row->truncated_on_left_p)
1037 bitmap = LEFT_TRUNCATION_BITMAP;
1038 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1039 bitmap = CONTINUATION_LINE_BITMAP;
1040 else if (row->indicate_empty_line_p)
1041 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1042 else
976b73d7 1043 bitmap = NO_FRINGE_BITMAP;
06a2c219 1044
976b73d7
KS
1045 x_draw_fringe_bitmap (w, row, bitmap, 1);
1046 }
06a2c219 1047
976b73d7 1048 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
06a2c219 1049 {
976b73d7
KS
1050 /* Decide which bitmap to draw in the right fringe. */
1051 if (row->truncated_on_right_p)
1052 bitmap = RIGHT_TRUNCATION_BITMAP;
1053 else if (row->continued_p)
1054 bitmap = CONTINUED_LINE_BITMAP;
1055 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1056 bitmap = ZV_LINE_BITMAP;
dcd08bfb 1057 else
976b73d7 1058 bitmap = NO_FRINGE_BITMAP;
06a2c219 1059
976b73d7
KS
1060 x_draw_fringe_bitmap (w, row, bitmap, 0);
1061 }
06a2c219
GM
1062}
1063
dc6f92b8 1064\f
06a2c219
GM
1065
1066/* This is called when starting Emacs and when restarting after
1067 suspend. When starting Emacs, no X window is mapped. And nothing
1068 must be done to Emacs's own window if it is suspended (though that
1069 rarely happens). */
dc6f92b8 1070
dfcf069d 1071static void
dc6f92b8
JB
1072XTset_terminal_modes ()
1073{
1074}
1075
06a2c219
GM
1076/* This is called when exiting or suspending Emacs. Exiting will make
1077 the X-windows go away, and suspending requires no action. */
dc6f92b8 1078
dfcf069d 1079static void
dc6f92b8
JB
1080XTreset_terminal_modes ()
1081{
dc6f92b8 1082}
06a2c219
GM
1083
1084
dc6f92b8 1085\f
06a2c219
GM
1086/***********************************************************************
1087 Output Cursor
1088 ***********************************************************************/
1089
1090/* Set the global variable output_cursor to CURSOR. All cursor
1091 positions are relative to updated_window. */
dc6f92b8 1092
dfcf069d 1093static void
06a2c219
GM
1094set_output_cursor (cursor)
1095 struct cursor_pos *cursor;
dc6f92b8 1096{
06a2c219
GM
1097 output_cursor.hpos = cursor->hpos;
1098 output_cursor.vpos = cursor->vpos;
1099 output_cursor.x = cursor->x;
1100 output_cursor.y = cursor->y;
1101}
1102
1103
1104/* Set a nominal cursor position.
dc6f92b8 1105
06a2c219
GM
1106 HPOS and VPOS are column/row positions in a window glyph matrix. X
1107 and Y are window text area relative pixel positions.
1108
1109 If this is done during an update, updated_window will contain the
1110 window that is being updated and the position is the future output
1111 cursor position for that window. If updated_window is null, use
1112 selected_window and display the cursor at the given position. */
1113
1114static void
1115XTcursor_to (vpos, hpos, y, x)
1116 int vpos, hpos, y, x;
1117{
1118 struct window *w;
1119
1120 /* If updated_window is not set, work on selected_window. */
1121 if (updated_window)
1122 w = updated_window;
1123 else
1124 w = XWINDOW (selected_window);
dbcb258a 1125
06a2c219
GM
1126 /* Set the output cursor. */
1127 output_cursor.hpos = hpos;
1128 output_cursor.vpos = vpos;
1129 output_cursor.x = x;
1130 output_cursor.y = y;
dc6f92b8 1131
06a2c219
GM
1132 /* If not called as part of an update, really display the cursor.
1133 This will also set the cursor position of W. */
1134 if (updated_window == NULL)
dc6f92b8
JB
1135 {
1136 BLOCK_INPUT;
06a2c219 1137 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1138 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1139 UNBLOCK_INPUT;
1140 }
1141}
dc43ef94 1142
06a2c219
GM
1143
1144\f
1145/***********************************************************************
1146 Display Iterator
1147 ***********************************************************************/
1148
1149/* Function prototypes of this page. */
1150
1151static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1152 struct glyph *,
ee569018
KH
1153 XChar2b *,
1154 int *));
06a2c219 1155static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
abfb6b46
GM
1156 int, XChar2b *, int,
1157 int));
06a2c219
GM
1158static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1159static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1160static void x_append_glyph P_ ((struct it *));
b4192550 1161static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1162static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1163 int, int, double));
1164static void x_produce_glyphs P_ ((struct it *));
06a2c219 1165static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1166
1167
e2ef8ee6
GM
1168/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1169 is not contained in the font. */
dc43ef94 1170
06a2c219 1171static INLINE XCharStruct *
ee569018 1172x_per_char_metric (font, char2b)
06a2c219
GM
1173 XFontStruct *font;
1174 XChar2b *char2b;
1175{
1176 /* The result metric information. */
1177 XCharStruct *pcm = NULL;
dc6f92b8 1178
06a2c219 1179 xassert (font && char2b);
dc6f92b8 1180
06a2c219 1181 if (font->per_char != NULL)
dc6f92b8 1182 {
06a2c219 1183 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1184 {
06a2c219
GM
1185 /* min_char_or_byte2 specifies the linear character index
1186 corresponding to the first element of the per_char array,
1187 max_char_or_byte2 is the index of the last character. A
1188 character with non-zero CHAR2B->byte1 is not in the font.
1189 A character with byte2 less than min_char_or_byte2 or
1190 greater max_char_or_byte2 is not in the font. */
1191 if (char2b->byte1 == 0
1192 && char2b->byte2 >= font->min_char_or_byte2
1193 && char2b->byte2 <= font->max_char_or_byte2)
1194 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1195 }
06a2c219 1196 else
dc6f92b8 1197 {
06a2c219
GM
1198 /* If either min_byte1 or max_byte1 are nonzero, both
1199 min_char_or_byte2 and max_char_or_byte2 are less than
1200 256, and the 2-byte character index values corresponding
1201 to the per_char array element N (counting from 0) are:
1202
1203 byte1 = N/D + min_byte1
1204 byte2 = N\D + min_char_or_byte2
1205
1206 where:
1207
1208 D = max_char_or_byte2 - min_char_or_byte2 + 1
1209 / = integer division
1210 \ = integer modulus */
1211 if (char2b->byte1 >= font->min_byte1
1212 && char2b->byte1 <= font->max_byte1
1213 && char2b->byte2 >= font->min_char_or_byte2
1214 && char2b->byte2 <= font->max_char_or_byte2)
1215 {
1216 pcm = (font->per_char
1217 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1218 * (char2b->byte1 - font->min_byte1))
1219 + (char2b->byte2 - font->min_char_or_byte2));
1220 }
dc6f92b8 1221 }
06a2c219
GM
1222 }
1223 else
1224 {
1225 /* If the per_char pointer is null, all glyphs between the first
1226 and last character indexes inclusive have the same
1227 information, as given by both min_bounds and max_bounds. */
1228 if (char2b->byte2 >= font->min_char_or_byte2
1229 && char2b->byte2 <= font->max_char_or_byte2)
1230 pcm = &font->max_bounds;
1231 }
dc6f92b8 1232
ee569018 1233 return ((pcm == NULL
3e71d8f2 1234 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1235 ? NULL : pcm);
06a2c219 1236}
b73b6aaf 1237
57b03282 1238
06a2c219
GM
1239/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1240 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1241
06a2c219
GM
1242static INLINE void
1243x_encode_char (c, char2b, font_info)
1244 int c;
1245 XChar2b *char2b;
1246 struct font_info *font_info;
1247{
1248 int charset = CHAR_CHARSET (c);
1249 XFontStruct *font = font_info->font;
1250
1251 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1252 This may be either a program in a special encoder language or a
1253 fixed encoding. */
1254 if (font_info->font_encoder)
1255 {
1256 /* It's a program. */
1257 struct ccl_program *ccl = font_info->font_encoder;
1258
1259 if (CHARSET_DIMENSION (charset) == 1)
1260 {
1261 ccl->reg[0] = charset;
1262 ccl->reg[1] = char2b->byte2;
1263 }
1264 else
1265 {
1266 ccl->reg[0] = charset;
1267 ccl->reg[1] = char2b->byte1;
1268 ccl->reg[2] = char2b->byte2;
1269 }
1270
1271 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1272
1273 /* We assume that MSBs are appropriately set/reset by CCL
1274 program. */
1275 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1276 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1277 else
1278 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1279 }
1280 else if (font_info->encoding[charset])
1281 {
1282 /* Fixed encoding scheme. See fontset.h for the meaning of the
1283 encoding numbers. */
1284 int enc = font_info->encoding[charset];
1285
1286 if ((enc == 1 || enc == 2)
1287 && CHARSET_DIMENSION (charset) == 2)
1288 char2b->byte1 |= 0x80;
1289
1290 if (enc == 1 || enc == 3)
1291 char2b->byte2 |= 0x80;
1292 }
1293}
1294
1295
1296/* Get face and two-byte form of character C in face FACE_ID on frame
1297 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
abfb6b46
GM
1298 means we want to display multibyte text. DISPLAY_P non-zero means
1299 make sure that X resources for the face returned are allocated.
1300 Value is a pointer to a realized face that is ready for display if
1301 DISPLAY_P is non-zero. */
06a2c219
GM
1302
1303static INLINE struct face *
abfb6b46 1304x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
06a2c219
GM
1305 struct frame *f;
1306 int c, face_id;
1307 XChar2b *char2b;
abfb6b46 1308 int multibyte_p, display_p;
06a2c219
GM
1309{
1310 struct face *face = FACE_FROM_ID (f, face_id);
1311
1312 if (!multibyte_p)
1313 {
1314 /* Unibyte case. We don't have to encode, but we have to make
1315 sure to use a face suitable for unibyte. */
1316 char2b->byte1 = 0;
1317 char2b->byte2 = c;
ee569018
KH
1318 face_id = FACE_FOR_CHAR (f, face, c);
1319 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1320 }
1321 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1322 {
1323 /* Case of ASCII in a face known to fit ASCII. */
1324 char2b->byte1 = 0;
1325 char2b->byte2 = c;
1326 }
1327 else
1328 {
1329 int c1, c2, charset;
1330
1331 /* Split characters into bytes. If c2 is -1 afterwards, C is
1332 really a one-byte character so that byte1 is zero. */
1333 SPLIT_CHAR (c, charset, c1, c2);
1334 if (c2 > 0)
1335 char2b->byte1 = c1, char2b->byte2 = c2;
1336 else
1337 char2b->byte1 = 0, char2b->byte2 = c1;
1338
06a2c219 1339 /* Maybe encode the character in *CHAR2B. */
ee569018 1340 if (face->font != NULL)
06a2c219
GM
1341 {
1342 struct font_info *font_info
1343 = FONT_INFO_FROM_ID (f, face->font_info_id);
1344 if (font_info)
ee569018 1345 x_encode_char (c, char2b, font_info);
06a2c219
GM
1346 }
1347 }
1348
1349 /* Make sure X resources of the face are allocated. */
abfb6b46
GM
1350 if (display_p)
1351 {
1352 xassert (face != NULL);
1353 PREPARE_FACE_FOR_DISPLAY (f, face);
1354 }
06a2c219
GM
1355
1356 return face;
1357}
1358
1359
1360/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1361 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1362 a pointer to a realized face that is ready for display. */
1363
1364static INLINE struct face *
ee569018 1365x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1366 struct frame *f;
1367 struct glyph *glyph;
1368 XChar2b *char2b;
ee569018 1369 int *two_byte_p;
06a2c219
GM
1370{
1371 struct face *face;
1372
1373 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1374 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1375
ee569018
KH
1376 if (two_byte_p)
1377 *two_byte_p = 0;
1378
06a2c219
GM
1379 if (!glyph->multibyte_p)
1380 {
1381 /* Unibyte case. We don't have to encode, but we have to make
1382 sure to use a face suitable for unibyte. */
1383 char2b->byte1 = 0;
43d120d8 1384 char2b->byte2 = glyph->u.ch;
06a2c219 1385 }
43d120d8
KH
1386 else if (glyph->u.ch < 128
1387 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1388 {
1389 /* Case of ASCII in a face known to fit ASCII. */
1390 char2b->byte1 = 0;
43d120d8 1391 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1392 }
1393 else
1394 {
1395 int c1, c2, charset;
1396
1397 /* Split characters into bytes. If c2 is -1 afterwards, C is
1398 really a one-byte character so that byte1 is zero. */
43d120d8 1399 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1400 if (c2 > 0)
1401 char2b->byte1 = c1, char2b->byte2 = c2;
1402 else
1403 char2b->byte1 = 0, char2b->byte2 = c1;
1404
1405 /* Maybe encode the character in *CHAR2B. */
1406 if (charset != CHARSET_ASCII)
1407 {
1408 struct font_info *font_info
1409 = FONT_INFO_FROM_ID (f, face->font_info_id);
1410 if (font_info)
1411 {
43d120d8 1412 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1413 if (two_byte_p)
1414 *two_byte_p
1415 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1416 }
1417 }
1418 }
1419
1420 /* Make sure X resources of the face are allocated. */
1421 xassert (face != NULL);
1422 PREPARE_FACE_FOR_DISPLAY (f, face);
1423 return face;
1424}
1425
1426
1427/* Store one glyph for IT->char_to_display in IT->glyph_row.
1428 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1429
1430static INLINE void
1431x_append_glyph (it)
1432 struct it *it;
1433{
1434 struct glyph *glyph;
1435 enum glyph_row_area area = it->area;
1436
1437 xassert (it->glyph_row);
1438 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1439
1440 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1441 if (glyph < it->glyph_row->glyphs[area + 1])
1442 {
06a2c219
GM
1443 glyph->charpos = CHARPOS (it->position);
1444 glyph->object = it->object;
88d75730 1445 glyph->pixel_width = it->pixel_width;
06a2c219 1446 glyph->voffset = it->voffset;
88d75730 1447 glyph->type = CHAR_GLYPH;
06a2c219 1448 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1449 glyph->left_box_line_p = it->start_of_box_run_p;
1450 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1451 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1452 || it->phys_descent > it->descent);
88d75730 1453 glyph->padding_p = 0;
ee569018 1454 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1455 glyph->face_id = it->face_id;
1456 glyph->u.ch = it->char_to_display;
06a2c219
GM
1457 ++it->glyph_row->used[area];
1458 }
1459}
1460
b4192550
KH
1461/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1462 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1463
1464static INLINE void
1465x_append_composite_glyph (it)
1466 struct it *it;
1467{
1468 struct glyph *glyph;
1469 enum glyph_row_area area = it->area;
1470
1471 xassert (it->glyph_row);
1472
1473 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1474 if (glyph < it->glyph_row->glyphs[area + 1])
1475 {
b4192550
KH
1476 glyph->charpos = CHARPOS (it->position);
1477 glyph->object = it->object;
88d75730 1478 glyph->pixel_width = it->pixel_width;
b4192550 1479 glyph->voffset = it->voffset;
88d75730 1480 glyph->type = COMPOSITE_GLYPH;
b4192550 1481 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1482 glyph->left_box_line_p = it->start_of_box_run_p;
1483 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1484 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1485 || it->phys_descent > it->descent);
88d75730
GM
1486 glyph->padding_p = 0;
1487 glyph->glyph_not_available_p = 0;
1488 glyph->face_id = it->face_id;
1489 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1490 ++it->glyph_row->used[area];
1491 }
1492}
1493
06a2c219
GM
1494
1495/* Change IT->ascent and IT->height according to the setting of
1496 IT->voffset. */
1497
1498static INLINE void
1499take_vertical_position_into_account (it)
1500 struct it *it;
1501{
1502 if (it->voffset)
1503 {
1504 if (it->voffset < 0)
1505 /* Increase the ascent so that we can display the text higher
1506 in the line. */
1507 it->ascent += abs (it->voffset);
1508 else
1509 /* Increase the descent so that we can display the text lower
1510 in the line. */
1511 it->descent += it->voffset;
1512 }
1513}
1514
1515
1516/* Produce glyphs/get display metrics for the image IT is loaded with.
1517 See the description of struct display_iterator in dispextern.h for
1518 an overview of struct display_iterator. */
1519
1520static void
1521x_produce_image_glyph (it)
1522 struct it *it;
1523{
1524 struct image *img;
1525 struct face *face;
1526
1527 xassert (it->what == IT_IMAGE);
1528
1529 face = FACE_FROM_ID (it->f, it->face_id);
1530 img = IMAGE_FROM_ID (it->f, it->image_id);
1531 xassert (img);
1532
1533 /* Make sure X resources of the face and image are loaded. */
1534 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1535 prepare_image_for_display (it->f, img);
1536
95af8492 1537 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1538 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1539 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1540
1541 it->nglyphs = 1;
1542
1543 if (face->box != FACE_NO_BOX)
1544 {
ea2ba0d4
KH
1545 if (face->box_line_width > 0)
1546 {
1547 it->ascent += face->box_line_width;
1548 it->descent += face->box_line_width;
1549 }
06a2c219
GM
1550
1551 if (it->start_of_box_run_p)
ea2ba0d4 1552 it->pixel_width += abs (face->box_line_width);
06a2c219 1553 if (it->end_of_box_run_p)
ea2ba0d4 1554 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1555 }
1556
1557 take_vertical_position_into_account (it);
1558
1559 if (it->glyph_row)
1560 {
1561 struct glyph *glyph;
1562 enum glyph_row_area area = it->area;
1563
1564 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1565 if (glyph < it->glyph_row->glyphs[area + 1])
1566 {
06a2c219
GM
1567 glyph->charpos = CHARPOS (it->position);
1568 glyph->object = it->object;
88d75730 1569 glyph->pixel_width = it->pixel_width;
06a2c219 1570 glyph->voffset = it->voffset;
88d75730 1571 glyph->type = IMAGE_GLYPH;
06a2c219 1572 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1573 glyph->left_box_line_p = it->start_of_box_run_p;
1574 glyph->right_box_line_p = it->end_of_box_run_p;
1575 glyph->overlaps_vertically_p = 0;
1576 glyph->padding_p = 0;
1577 glyph->glyph_not_available_p = 0;
1578 glyph->face_id = it->face_id;
1579 glyph->u.img_id = img->id;
06a2c219
GM
1580 ++it->glyph_row->used[area];
1581 }
1582 }
1583}
1584
1585
1586/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1587 of the glyph, WIDTH and HEIGHT are the width and height of the
1588 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1589 ascent of the glyph (0 <= ASCENT <= 1). */
1590
1591static void
1592x_append_stretch_glyph (it, object, width, height, ascent)
1593 struct it *it;
1594 Lisp_Object object;
1595 int width, height;
1596 double ascent;
1597{
1598 struct glyph *glyph;
1599 enum glyph_row_area area = it->area;
1600
1601 xassert (ascent >= 0 && ascent <= 1);
1602
1603 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1604 if (glyph < it->glyph_row->glyphs[area + 1])
1605 {
06a2c219
GM
1606 glyph->charpos = CHARPOS (it->position);
1607 glyph->object = object;
88d75730 1608 glyph->pixel_width = width;
06a2c219 1609 glyph->voffset = it->voffset;
88d75730 1610 glyph->type = STRETCH_GLYPH;
06a2c219 1611 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1612 glyph->left_box_line_p = it->start_of_box_run_p;
1613 glyph->right_box_line_p = it->end_of_box_run_p;
1614 glyph->overlaps_vertically_p = 0;
1615 glyph->padding_p = 0;
1616 glyph->glyph_not_available_p = 0;
1617 glyph->face_id = it->face_id;
1618 glyph->u.stretch.ascent = height * ascent;
1619 glyph->u.stretch.height = height;
06a2c219
GM
1620 ++it->glyph_row->used[area];
1621 }
1622}
1623
1624
1625/* Produce a stretch glyph for iterator IT. IT->object is the value
1626 of the glyph property displayed. The value must be a list
1627 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1628 being recognized:
1629
1630 1. `:width WIDTH' specifies that the space should be WIDTH *
1631 canonical char width wide. WIDTH may be an integer or floating
1632 point number.
1633
1634 2. `:relative-width FACTOR' specifies that the width of the stretch
1635 should be computed from the width of the first character having the
1636 `glyph' property, and should be FACTOR times that width.
1637
1638 3. `:align-to HPOS' specifies that the space should be wide enough
1639 to reach HPOS, a value in canonical character units.
1640
1641 Exactly one of the above pairs must be present.
1642
1643 4. `:height HEIGHT' specifies that the height of the stretch produced
1644 should be HEIGHT, measured in canonical character units.
1645
269b7745 1646 5. `:relative-height FACTOR' specifies that the height of the
06a2c219
GM
1647 stretch should be FACTOR times the height of the characters having
1648 the glyph property.
1649
1650 Either none or exactly one of 4 or 5 must be present.
1651
1652 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1653 of the stretch should be used for the ascent of the stretch.
1654 ASCENT must be in the range 0 <= ASCENT <= 100. */
1655
1656#define NUMVAL(X) \
1657 ((INTEGERP (X) || FLOATP (X)) \
1658 ? XFLOATINT (X) \
1659 : - 1)
1660
1661
1662static void
1663x_produce_stretch_glyph (it)
1664 struct it *it;
1665{
1666 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1667#if GLYPH_DEBUG
1668 extern Lisp_Object Qspace;
1669#endif
1670 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1671 extern Lisp_Object QCrelative_width, QCrelative_height;
1672 extern Lisp_Object QCalign_to;
1673 Lisp_Object prop, plist;
1674 double width = 0, height = 0, ascent = 0;
1675 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1676 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1677
1678 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1679
1680 /* List should start with `space'. */
1681 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1682 plist = XCDR (it->object);
1683
1684 /* Compute the width of the stretch. */
1685 if (prop = Fplist_get (plist, QCwidth),
1686 NUMVAL (prop) > 0)
1687 /* Absolute width `:width WIDTH' specified and valid. */
1688 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1689 else if (prop = Fplist_get (plist, QCrelative_width),
1690 NUMVAL (prop) > 0)
1691 {
1692 /* Relative width `:relative-width FACTOR' specified and valid.
1693 Compute the width of the characters having the `glyph'
1694 property. */
1695 struct it it2;
1696 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1697
1698 it2 = *it;
1699 if (it->multibyte_p)
1700 {
1701 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1702 - IT_BYTEPOS (*it));
1703 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1704 }
1705 else
1706 it2.c = *p, it2.len = 1;
1707
1708 it2.glyph_row = NULL;
1709 it2.what = IT_CHARACTER;
1710 x_produce_glyphs (&it2);
1711 width = NUMVAL (prop) * it2.pixel_width;
1712 }
1713 else if (prop = Fplist_get (plist, QCalign_to),
1714 NUMVAL (prop) > 0)
1715 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1716 else
1717 /* Nothing specified -> width defaults to canonical char width. */
1718 width = CANON_X_UNIT (it->f);
1719
1720 /* Compute height. */
1721 if (prop = Fplist_get (plist, QCheight),
1722 NUMVAL (prop) > 0)
1723 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1724 else if (prop = Fplist_get (plist, QCrelative_height),
1725 NUMVAL (prop) > 0)
1726 height = FONT_HEIGHT (font) * NUMVAL (prop);
1727 else
1728 height = FONT_HEIGHT (font);
1729
1730 /* Compute percentage of height used for ascent. If
1731 `:ascent ASCENT' is present and valid, use that. Otherwise,
1732 derive the ascent from the font in use. */
1733 if (prop = Fplist_get (plist, QCascent),
1734 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1735 ascent = NUMVAL (prop) / 100.0;
1736 else
1737 ascent = (double) font->ascent / FONT_HEIGHT (font);
1738
1739 if (width <= 0)
1740 width = 1;
1741 if (height <= 0)
1742 height = 1;
1743
1744 if (it->glyph_row)
1745 {
1746 Lisp_Object object = it->stack[it->sp - 1].string;
1747 if (!STRINGP (object))
1748 object = it->w->buffer;
1749 x_append_stretch_glyph (it, object, width, height, ascent);
1750 }
1751
1752 it->pixel_width = width;
66ac4b0e
GM
1753 it->ascent = it->phys_ascent = height * ascent;
1754 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1755 it->nglyphs = 1;
1756
1757 if (face->box != FACE_NO_BOX)
1758 {
ea2ba0d4
KH
1759 if (face->box_line_width > 0)
1760 {
1761 it->ascent += face->box_line_width;
1762 it->descent += face->box_line_width;
1763 }
06a2c219
GM
1764
1765 if (it->start_of_box_run_p)
ea2ba0d4 1766 it->pixel_width += abs (face->box_line_width);
06a2c219 1767 if (it->end_of_box_run_p)
ea2ba0d4 1768 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1769 }
1770
1771 take_vertical_position_into_account (it);
1772}
1773
b4192550
KH
1774/* Return proper value to be used as baseline offset of font that has
1775 ASCENT and DESCENT to draw characters by the font at the vertical
1776 center of the line of frame F.
1777
1778 Here, out task is to find the value of BOFF in the following figure;
1779
1780 -------------------------+-----------+-
1781 -+-+---------+-+ | |
1782 | | | | | |
1783 | | | | F_ASCENT F_HEIGHT
1784 | | | ASCENT | |
1785 HEIGHT | | | | |
1786 | | |-|-+------+-----------|------- baseline
1787 | | | | BOFF | |
1788 | |---------|-+-+ | |
1789 | | | DESCENT | |
1790 -+-+---------+-+ F_DESCENT |
1791 -------------------------+-----------+-
1792
1793 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1794 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1795 DESCENT = FONT->descent
1796 HEIGHT = FONT_HEIGHT (FONT)
1797 F_DESCENT = (F->output_data.x->font->descent
1798 - F->output_data.x->baseline_offset)
1799 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1800*/
1801
458f45fa
KH
1802#define VCENTER_BASELINE_OFFSET(FONT, F) \
1803 ((FONT)->descent \
1804 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1805 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1806 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1807
1808/* Produce glyphs/get display metrics for the display element IT is
1809 loaded with. See the description of struct display_iterator in
1810 dispextern.h for an overview of struct display_iterator. */
1811
1812static void
1813x_produce_glyphs (it)
1814 struct it *it;
1815{
ee569018
KH
1816 it->glyph_not_available_p = 0;
1817
06a2c219
GM
1818 if (it->what == IT_CHARACTER)
1819 {
1820 XChar2b char2b;
1821 XFontStruct *font;
ee569018 1822 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1823 XCharStruct *pcm;
06a2c219 1824 int font_not_found_p;
b4192550
KH
1825 struct font_info *font_info;
1826 int boff; /* baseline offset */
a4249304
KH
1827 /* We may change it->multibyte_p upon unibyte<->multibyte
1828 conversion. So, save the current value now and restore it
1829 later.
1830
1831 Note: It seems that we don't have to record multibyte_p in
1832 struct glyph because the character code itself tells if or
1833 not the character is multibyte. Thus, in the future, we must
1834 consider eliminating the field `multibyte_p' in the struct
c347a1c3 1835 glyph. */
a4249304 1836 int saved_multibyte_p = it->multibyte_p;
06a2c219 1837
ee569018
KH
1838 /* Maybe translate single-byte characters to multibyte, or the
1839 other way. */
06a2c219 1840 it->char_to_display = it->c;
ee569018 1841 if (!ASCII_BYTE_P (it->c))
06a2c219 1842 {
ee569018
KH
1843 if (unibyte_display_via_language_environment
1844 && SINGLE_BYTE_CHAR_P (it->c)
1845 && (it->c >= 0240
1846 || !NILP (Vnonascii_translation_table)))
1847 {
1848 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1849 it->multibyte_p = 1;
ee569018
KH
1850 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1851 face = FACE_FROM_ID (it->f, it->face_id);
1852 }
1853 else if (!SINGLE_BYTE_CHAR_P (it->c)
1854 && !it->multibyte_p)
1855 {
c347a1c3 1856 it->multibyte_p = 1;
ee569018
KH
1857 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1858 face = FACE_FROM_ID (it->f, it->face_id);
1859 }
06a2c219
GM
1860 }
1861
ee569018
KH
1862 /* Get font to use. Encode IT->char_to_display. */
1863 x_get_char_face_and_encoding (it->f, it->char_to_display,
1864 it->face_id, &char2b,
abfb6b46 1865 it->multibyte_p, 0);
06a2c219
GM
1866 font = face->font;
1867
1868 /* When no suitable font found, use the default font. */
1869 font_not_found_p = font == NULL;
1870 if (font_not_found_p)
b4192550
KH
1871 {
1872 font = FRAME_FONT (it->f);
1873 boff = it->f->output_data.x->baseline_offset;
1874 font_info = NULL;
1875 }
1876 else
1877 {
1878 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1879 boff = font_info->baseline_offset;
1880 if (font_info->vertical_centering)
1881 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1882 }
06a2c219
GM
1883
1884 if (it->char_to_display >= ' '
1885 && (!it->multibyte_p || it->char_to_display < 128))
1886 {
1887 /* Either unibyte or ASCII. */
1888 int stretched_p;
1889
1890 it->nglyphs = 1;
06a2c219
GM
1891
1892 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1893 it->ascent = font->ascent + boff;
1894 it->descent = font->descent - boff;
474848ac
GM
1895
1896 if (pcm)
1897 {
1898 it->phys_ascent = pcm->ascent + boff;
1899 it->phys_descent = pcm->descent - boff;
1900 it->pixel_width = pcm->width;
1901 }
1902 else
1903 {
1904 it->glyph_not_available_p = 1;
1905 it->phys_ascent = font->ascent + boff;
1906 it->phys_descent = font->descent - boff;
1907 it->pixel_width = FONT_WIDTH (font);
1908 }
06a2c219
GM
1909
1910 /* If this is a space inside a region of text with
1911 `space-width' property, change its width. */
1912 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1913 if (stretched_p)
1914 it->pixel_width *= XFLOATINT (it->space_width);
1915
1916 /* If face has a box, add the box thickness to the character
1917 height. If character has a box line to the left and/or
1918 right, add the box line width to the character's width. */
1919 if (face->box != FACE_NO_BOX)
1920 {
1921 int thick = face->box_line_width;
1922
ea2ba0d4
KH
1923 if (thick > 0)
1924 {
1925 it->ascent += thick;
1926 it->descent += thick;
1927 }
1928 else
1929 thick = -thick;
1930
06a2c219
GM
1931 if (it->start_of_box_run_p)
1932 it->pixel_width += thick;
1933 if (it->end_of_box_run_p)
1934 it->pixel_width += thick;
1935 }
1936
1937 /* If face has an overline, add the height of the overline
1938 (1 pixel) and a 1 pixel margin to the character height. */
1939 if (face->overline_p)
1940 it->ascent += 2;
1941
1942 take_vertical_position_into_account (it);
1943
1944 /* If we have to actually produce glyphs, do it. */
1945 if (it->glyph_row)
1946 {
1947 if (stretched_p)
1948 {
1949 /* Translate a space with a `space-width' property
1950 into a stretch glyph. */
1951 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1952 x_append_stretch_glyph (it, it->object, it->pixel_width,
1953 it->ascent + it->descent, ascent);
1954 }
1955 else
1956 x_append_glyph (it);
1957
1958 /* If characters with lbearing or rbearing are displayed
1959 in this line, record that fact in a flag of the
1960 glyph row. This is used to optimize X output code. */
1c7e22fd 1961 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1962 it->glyph_row->contains_overlapping_glyphs_p = 1;
1963 }
1964 }
1965 else if (it->char_to_display == '\n')
1966 {
1967 /* A newline has no width but we need the height of the line. */
1968 it->pixel_width = 0;
1969 it->nglyphs = 0;
b4192550
KH
1970 it->ascent = it->phys_ascent = font->ascent + boff;
1971 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1972
ea2ba0d4
KH
1973 if (face->box != FACE_NO_BOX
1974 && face->box_line_width > 0)
06a2c219 1975 {
ea2ba0d4
KH
1976 it->ascent += face->box_line_width;
1977 it->descent += face->box_line_width;
06a2c219
GM
1978 }
1979 }
1980 else if (it->char_to_display == '\t')
1981 {
1982 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1983 int x = it->current_x + it->continuation_lines_width;
06a2c219 1984 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1985
1986 /* If the distance from the current position to the next tab
1987 stop is less than a canonical character width, use the
1988 tab stop after that. */
1989 if (next_tab_x - x < CANON_X_UNIT (it->f))
1990 next_tab_x += tab_width;
06a2c219
GM
1991
1992 it->pixel_width = next_tab_x - x;
1993 it->nglyphs = 1;
b4192550
KH
1994 it->ascent = it->phys_ascent = font->ascent + boff;
1995 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1996
1997 if (it->glyph_row)
1998 {
1999 double ascent = (double) it->ascent / (it->ascent + it->descent);
2000 x_append_stretch_glyph (it, it->object, it->pixel_width,
2001 it->ascent + it->descent, ascent);
2002 }
2003 }
2004 else
2005 {
2006 /* A multi-byte character. Assume that the display width of the
2007 character is the width of the character multiplied by the
b4192550 2008 width of the font. */
06a2c219 2009
b4192550
KH
2010 /* If we found a font, this font should give us the right
2011 metrics. If we didn't find a font, use the frame's
2012 default font and calculate the width of the character
2013 from the charset width; this is what old redisplay code
2014 did. */
2015 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
2016 if (font_not_found_p || !pcm)
2017 {
2018 int charset = CHAR_CHARSET (it->char_to_display);
2019
2020 it->glyph_not_available_p = 1;
2021 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2022 * CHARSET_WIDTH (charset));
2023 it->phys_ascent = font->ascent + boff;
2024 it->phys_descent = font->descent - boff;
2025 }
2026 else
2027 {
2028 it->pixel_width = pcm->width;
2029 it->phys_ascent = pcm->ascent + boff;
2030 it->phys_descent = pcm->descent - boff;
2031 if (it->glyph_row
2032 && (pcm->lbearing < 0
2033 || pcm->rbearing > pcm->width))
2034 it->glyph_row->contains_overlapping_glyphs_p = 1;
2035 }
b4192550
KH
2036 it->nglyphs = 1;
2037 it->ascent = font->ascent + boff;
2038 it->descent = font->descent - boff;
06a2c219
GM
2039 if (face->box != FACE_NO_BOX)
2040 {
2041 int thick = face->box_line_width;
ea2ba0d4
KH
2042
2043 if (thick > 0)
2044 {
2045 it->ascent += thick;
2046 it->descent += thick;
2047 }
2048 else
2049 thick = - thick;
06a2c219
GM
2050
2051 if (it->start_of_box_run_p)
2052 it->pixel_width += thick;
2053 if (it->end_of_box_run_p)
2054 it->pixel_width += thick;
2055 }
2056
2057 /* If face has an overline, add the height of the overline
2058 (1 pixel) and a 1 pixel margin to the character height. */
2059 if (face->overline_p)
2060 it->ascent += 2;
2061
2062 take_vertical_position_into_account (it);
2063
2064 if (it->glyph_row)
2065 x_append_glyph (it);
2066 }
a4249304 2067 it->multibyte_p = saved_multibyte_p;
06a2c219 2068 }
b4192550
KH
2069 else if (it->what == IT_COMPOSITION)
2070 {
2071 /* Note: A composition is represented as one glyph in the
2072 glyph matrix. There are no padding glyphs. */
2073 XChar2b char2b;
2074 XFontStruct *font;
ee569018 2075 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2076 XCharStruct *pcm;
2077 int font_not_found_p;
2078 struct font_info *font_info;
2079 int boff; /* baseline offset */
2080 struct composition *cmp = composition_table[it->cmp_id];
2081
2082 /* Maybe translate single-byte characters to multibyte. */
2083 it->char_to_display = it->c;
2084 if (unibyte_display_via_language_environment
2085 && SINGLE_BYTE_CHAR_P (it->c)
2086 && (it->c >= 0240
2087 || (it->c >= 0200
2088 && !NILP (Vnonascii_translation_table))))
2089 {
2090 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2091 }
2092
2093 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2094 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2095 face = FACE_FROM_ID (it->f, it->face_id);
2096 x_get_char_face_and_encoding (it->f, it->char_to_display,
abfb6b46 2097 it->face_id, &char2b, it->multibyte_p, 0);
b4192550
KH
2098 font = face->font;
2099
2100 /* When no suitable font found, use the default font. */
2101 font_not_found_p = font == NULL;
2102 if (font_not_found_p)
2103 {
2104 font = FRAME_FONT (it->f);
2105 boff = it->f->output_data.x->baseline_offset;
2106 font_info = NULL;
2107 }
2108 else
2109 {
2110 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2111 boff = font_info->baseline_offset;
2112 if (font_info->vertical_centering)
2113 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2114 }
2115
2116 /* There are no padding glyphs, so there is only one glyph to
2117 produce for the composition. Important is that pixel_width,
2118 ascent and descent are the values of what is drawn by
2119 draw_glyphs (i.e. the values of the overall glyphs composed). */
2120 it->nglyphs = 1;
2121
2122 /* If we have not yet calculated pixel size data of glyphs of
2123 the composition for the current face font, calculate them
2124 now. Theoretically, we have to check all fonts for the
2125 glyphs, but that requires much time and memory space. So,
2126 here we check only the font of the first glyph. This leads
2127 to incorrect display very rarely, and C-l (recenter) can
2128 correct the display anyway. */
2129 if (cmp->font != (void *) font)
2130 {
2131 /* Ascent and descent of the font of the first character of
2132 this composition (adjusted by baseline offset). Ascent
2133 and descent of overall glyphs should not be less than
2134 them respectively. */
2135 int font_ascent = font->ascent + boff;
2136 int font_descent = font->descent - boff;
2137 /* Bounding box of the overall glyphs. */
2138 int leftmost, rightmost, lowest, highest;
329bed06 2139 int i, width, ascent, descent;
b4192550
KH
2140
2141 cmp->font = (void *) font;
2142
2143 /* Initialize the bounding box. */
1bdeec2e
KH
2144 if (font_info
2145 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2146 {
2147 width = pcm->width;
2148 ascent = pcm->ascent;
2149 descent = pcm->descent;
2150 }
2151 else
2152 {
2153 width = FONT_WIDTH (font);
2154 ascent = font->ascent;
2155 descent = font->descent;
2156 }
2157
2158 rightmost = width;
2159 lowest = - descent + boff;
2160 highest = ascent + boff;
b4192550 2161 leftmost = 0;
329bed06 2162
b4192550
KH
2163 if (font_info
2164 && font_info->default_ascent
2165 && CHAR_TABLE_P (Vuse_default_ascent)
2166 && !NILP (Faref (Vuse_default_ascent,
2167 make_number (it->char_to_display))))
2168 highest = font_info->default_ascent + boff;
2169
2170 /* Draw the first glyph at the normal position. It may be
2171 shifted to right later if some other glyphs are drawn at
2172 the left. */
2173 cmp->offsets[0] = 0;
2174 cmp->offsets[1] = boff;
2175
2176 /* Set cmp->offsets for the remaining glyphs. */
2177 for (i = 1; i < cmp->glyph_len; i++)
2178 {
2179 int left, right, btm, top;
2180 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2181 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2182
2183 face = FACE_FROM_ID (it->f, face_id);
2184 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
abfb6b46 2185 it->multibyte_p, 0);
b4192550
KH
2186 font = face->font;
2187 if (font == NULL)
2188 {
2189 font = FRAME_FONT (it->f);
2190 boff = it->f->output_data.x->baseline_offset;
2191 font_info = NULL;
2192 }
2193 else
2194 {
2195 font_info
2196 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2197 boff = font_info->baseline_offset;
2198 if (font_info->vertical_centering)
2199 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2200 }
2201
1bdeec2e
KH
2202 if (font_info
2203 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2204 {
2205 width = pcm->width;
2206 ascent = pcm->ascent;
2207 descent = pcm->descent;
2208 }
2209 else
2210 {
2211 width = FONT_WIDTH (font);
1bdeec2e
KH
2212 ascent = 1;
2213 descent = 0;
329bed06 2214 }
b4192550
KH
2215
2216 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2217 {
2218 /* Relative composition with or without
2219 alternate chars. */
329bed06
GM
2220 left = (leftmost + rightmost - width) / 2;
2221 btm = - descent + boff;
b4192550
KH
2222 if (font_info && font_info->relative_compose
2223 && (! CHAR_TABLE_P (Vignore_relative_composition)
2224 || NILP (Faref (Vignore_relative_composition,
2225 make_number (ch)))))
2226 {
2227
329bed06 2228 if (- descent >= font_info->relative_compose)
b4192550
KH
2229 /* One extra pixel between two glyphs. */
2230 btm = highest + 1;
329bed06 2231 else if (ascent <= 0)
b4192550 2232 /* One extra pixel between two glyphs. */
329bed06 2233 btm = lowest - 1 - ascent - descent;
b4192550
KH
2234 }
2235 }
2236 else
2237 {
2238 /* A composition rule is specified by an integer
2239 value that encodes global and new reference
2240 points (GREF and NREF). GREF and NREF are
2241 specified by numbers as below:
2242
2243 0---1---2 -- ascent
2244 | |
2245 | |
2246 | |
2247 9--10--11 -- center
2248 | |
2249 ---3---4---5--- baseline
2250 | |
2251 6---7---8 -- descent
2252 */
2253 int rule = COMPOSITION_RULE (cmp, i);
2254 int gref, nref, grefx, grefy, nrefx, nrefy;
2255
2256 COMPOSITION_DECODE_RULE (rule, gref, nref);
2257 grefx = gref % 3, nrefx = nref % 3;
2258 grefy = gref / 3, nrefy = nref / 3;
2259
2260 left = (leftmost
2261 + grefx * (rightmost - leftmost) / 2
329bed06 2262 - nrefx * width / 2);
b4192550
KH
2263 btm = ((grefy == 0 ? highest
2264 : grefy == 1 ? 0
2265 : grefy == 2 ? lowest
2266 : (highest + lowest) / 2)
329bed06
GM
2267 - (nrefy == 0 ? ascent + descent
2268 : nrefy == 1 ? descent - boff
b4192550 2269 : nrefy == 2 ? 0
329bed06 2270 : (ascent + descent) / 2));
b4192550
KH
2271 }
2272
2273 cmp->offsets[i * 2] = left;
329bed06 2274 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2275
2276 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2277 right = left + width;
2278 top = btm + descent + ascent;
b4192550
KH
2279 if (left < leftmost)
2280 leftmost = left;
2281 if (right > rightmost)
2282 rightmost = right;
2283 if (top > highest)
2284 highest = top;
2285 if (btm < lowest)
2286 lowest = btm;
2287 }
2288
2289 /* If there are glyphs whose x-offsets are negative,
2290 shift all glyphs to the right and make all x-offsets
2291 non-negative. */
2292 if (leftmost < 0)
2293 {
2294 for (i = 0; i < cmp->glyph_len; i++)
2295 cmp->offsets[i * 2] -= leftmost;
2296 rightmost -= leftmost;
2297 }
2298
2299 cmp->pixel_width = rightmost;
2300 cmp->ascent = highest;
2301 cmp->descent = - lowest;
2302 if (cmp->ascent < font_ascent)
2303 cmp->ascent = font_ascent;
2304 if (cmp->descent < font_descent)
2305 cmp->descent = font_descent;
2306 }
2307
2308 it->pixel_width = cmp->pixel_width;
2309 it->ascent = it->phys_ascent = cmp->ascent;
2310 it->descent = it->phys_descent = cmp->descent;
2311
2312 if (face->box != FACE_NO_BOX)
2313 {
2314 int thick = face->box_line_width;
ea2ba0d4
KH
2315
2316 if (thick > 0)
2317 {
2318 it->ascent += thick;
2319 it->descent += thick;
2320 }
2321 else
2322 thick = - thick;
b4192550
KH
2323
2324 if (it->start_of_box_run_p)
2325 it->pixel_width += thick;
2326 if (it->end_of_box_run_p)
2327 it->pixel_width += thick;
2328 }
2329
2330 /* If face has an overline, add the height of the overline
2331 (1 pixel) and a 1 pixel margin to the character height. */
2332 if (face->overline_p)
2333 it->ascent += 2;
2334
2335 take_vertical_position_into_account (it);
2336
2337 if (it->glyph_row)
2338 x_append_composite_glyph (it);
2339 }
06a2c219
GM
2340 else if (it->what == IT_IMAGE)
2341 x_produce_image_glyph (it);
2342 else if (it->what == IT_STRETCH)
2343 x_produce_stretch_glyph (it);
2344
3017fdd1
GM
2345 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2346 because this isn't true for images with `:ascent 100'. */
2347 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2348 if (it->area == TEXT_AREA)
2349 it->current_x += it->pixel_width;
66ac4b0e 2350
d365f5bb
GM
2351 it->descent += it->extra_line_spacing;
2352
06a2c219
GM
2353 it->max_ascent = max (it->max_ascent, it->ascent);
2354 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2355 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2356 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2357}
2358
2359
2360/* Estimate the pixel height of the mode or top line on frame F.
2361 FACE_ID specifies what line's height to estimate. */
2362
2363int
2364x_estimate_mode_line_height (f, face_id)
2365 struct frame *f;
2366 enum face_id face_id;
2367{
43281ee3 2368 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2369
2370 /* This function is called so early when Emacs starts that the face
2371 cache and mode line face are not yet initialized. */
2372 if (FRAME_FACE_CACHE (f))
2373 {
2374 struct face *face = FACE_FROM_ID (f, face_id);
2375 if (face)
43281ee3
GM
2376 {
2377 if (face->font)
2378 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2379 if (face->box_line_width > 0)
2380 height += 2 * face->box_line_width;
43281ee3 2381 }
06a2c219
GM
2382 }
2383
2384 return height;
2385}
2386
2387\f
2388/***********************************************************************
2389 Glyph display
2390 ***********************************************************************/
2391
2392/* A sequence of glyphs to be drawn in the same face.
2393
2394 This data structure is not really completely X specific, so it
2395 could possibly, at least partially, be useful for other systems. It
2396 is currently not part of the external redisplay interface because
2397 it's not clear what other systems will need. */
2398
2399struct glyph_string
2400{
2401 /* X-origin of the string. */
2402 int x;
2403
2404 /* Y-origin and y-position of the base line of this string. */
2405 int y, ybase;
2406
2407 /* The width of the string, not including a face extension. */
2408 int width;
2409
2410 /* The width of the string, including a face extension. */
2411 int background_width;
2412
2413 /* The height of this string. This is the height of the line this
2414 string is drawn in, and can be different from the height of the
2415 font the string is drawn in. */
2416 int height;
2417
2418 /* Number of pixels this string overwrites in front of its x-origin.
2419 This number is zero if the string has an lbearing >= 0; it is
2420 -lbearing, if the string has an lbearing < 0. */
2421 int left_overhang;
2422
2423 /* Number of pixels this string overwrites past its right-most
2424 nominal x-position, i.e. x + width. Zero if the string's
2425 rbearing is <= its nominal width, rbearing - width otherwise. */
2426 int right_overhang;
2427
2428 /* The frame on which the glyph string is drawn. */
2429 struct frame *f;
2430
2431 /* The window on which the glyph string is drawn. */
2432 struct window *w;
2433
2434 /* X display and window for convenience. */
2435 Display *display;
2436 Window window;
2437
2438 /* The glyph row for which this string was built. It determines the
2439 y-origin and height of the string. */
2440 struct glyph_row *row;
2441
2442 /* The area within row. */
2443 enum glyph_row_area area;
2444
2445 /* Characters to be drawn, and number of characters. */
2446 XChar2b *char2b;
2447 int nchars;
2448
06a2c219
GM
2449 /* A face-override for drawing cursors, mouse face and similar. */
2450 enum draw_glyphs_face hl;
2451
2452 /* Face in which this string is to be drawn. */
2453 struct face *face;
2454
2455 /* Font in which this string is to be drawn. */
2456 XFontStruct *font;
2457
2458 /* Font info for this string. */
2459 struct font_info *font_info;
2460
b4192550
KH
2461 /* Non-null means this string describes (part of) a composition.
2462 All characters from char2b are drawn composed. */
2463 struct composition *cmp;
06a2c219
GM
2464
2465 /* Index of this glyph string's first character in the glyph
b4192550
KH
2466 definition of CMP. If this is zero, this glyph string describes
2467 the first character of a composition. */
06a2c219
GM
2468 int gidx;
2469
2470 /* 1 means this glyph strings face has to be drawn to the right end
2471 of the window's drawing area. */
2472 unsigned extends_to_end_of_line_p : 1;
2473
2474 /* 1 means the background of this string has been drawn. */
2475 unsigned background_filled_p : 1;
2476
2477 /* 1 means glyph string must be drawn with 16-bit functions. */
2478 unsigned two_byte_p : 1;
2479
2480 /* 1 means that the original font determined for drawing this glyph
2481 string could not be loaded. The member `font' has been set to
2482 the frame's default font in this case. */
2483 unsigned font_not_found_p : 1;
2484
2485 /* 1 means that the face in which this glyph string is drawn has a
2486 stipple pattern. */
2487 unsigned stippled_p : 1;
2488
66ac4b0e
GM
2489 /* 1 means only the foreground of this glyph string must be drawn,
2490 and we should use the physical height of the line this glyph
2491 string appears in as clip rect. */
2492 unsigned for_overlaps_p : 1;
2493
06a2c219
GM
2494 /* The GC to use for drawing this glyph string. */
2495 GC gc;
2496
2497 /* A pointer to the first glyph in the string. This glyph
2498 corresponds to char2b[0]. Needed to draw rectangles if
2499 font_not_found_p is 1. */
2500 struct glyph *first_glyph;
2501
2502 /* Image, if any. */
2503 struct image *img;
2504
2505 struct glyph_string *next, *prev;
2506};
2507
2508
61869b99 2509#if GLYPH_DEBUG
06a2c219
GM
2510
2511static void
2512x_dump_glyph_string (s)
2513 struct glyph_string *s;
2514{
2515 fprintf (stderr, "glyph string\n");
2516 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2517 s->x, s->y, s->width, s->height);
2518 fprintf (stderr, " ybase = %d\n", s->ybase);
2519 fprintf (stderr, " hl = %d\n", s->hl);
2520 fprintf (stderr, " left overhang = %d, right = %d\n",
2521 s->left_overhang, s->right_overhang);
2522 fprintf (stderr, " nchars = %d\n", s->nchars);
2523 fprintf (stderr, " extends to end of line = %d\n",
2524 s->extends_to_end_of_line_p);
2525 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2526 fprintf (stderr, " bg width = %d\n", s->background_width);
2527}
2528
2529#endif /* GLYPH_DEBUG */
2530
2531
2532
2533static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2534 struct glyph_string **,
2535 struct glyph_string *,
2536 struct glyph_string *));
2537static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2538 struct glyph_string **,
2539 struct glyph_string *,
2540 struct glyph_string *));
2541static void x_append_glyph_string P_ ((struct glyph_string **,
2542 struct glyph_string **,
2543 struct glyph_string *));
2544static int x_left_overwritten P_ ((struct glyph_string *));
2545static int x_left_overwriting P_ ((struct glyph_string *));
2546static int x_right_overwritten P_ ((struct glyph_string *));
2547static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2548static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2549 int));
06a2c219
GM
2550static void x_init_glyph_string P_ ((struct glyph_string *,
2551 XChar2b *, struct window *,
2552 struct glyph_row *,
2553 enum glyph_row_area, int,
2554 enum draw_glyphs_face));
2555static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2556 enum glyph_row_area, int, int,
f0a48a01 2557 enum draw_glyphs_face, int));
06a2c219
GM
2558static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2559static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2560static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2561 int));
2562static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2563static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2564static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2565static void x_draw_glyph_string P_ ((struct glyph_string *));
2566static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2567static void x_set_cursor_gc P_ ((struct glyph_string *));
2568static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2569static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2570static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2571 int *, int *));
2572static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2573static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2574 unsigned long *, double, int));
06a2c219 2575static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2576 double, int, unsigned long));
06a2c219
GM
2577static void x_setup_relief_colors P_ ((struct glyph_string *));
2578static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2579static void x_draw_image_relief P_ ((struct glyph_string *));
2580static void x_draw_image_foreground P_ ((struct glyph_string *));
2581static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2582static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2583static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2584 int, int, int));
2585static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2586 int, int, int, int, XRectangle *));
2587static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2588 int, int, int, XRectangle *));
66ac4b0e
GM
2589static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2590 enum glyph_row_area));
209f68d9
GM
2591static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2592 struct glyph_row *,
2593 enum glyph_row_area, int, int));
06a2c219 2594
163dcff3
GM
2595#if GLYPH_DEBUG
2596static void x_check_font P_ ((struct frame *, XFontStruct *));
2597#endif
2598
06a2c219 2599
06a2c219
GM
2600/* Append the list of glyph strings with head H and tail T to the list
2601 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2602
2603static INLINE void
2604x_append_glyph_string_lists (head, tail, h, t)
2605 struct glyph_string **head, **tail;
2606 struct glyph_string *h, *t;
2607{
2608 if (h)
2609 {
2610 if (*head)
2611 (*tail)->next = h;
2612 else
2613 *head = h;
2614 h->prev = *tail;
2615 *tail = t;
2616 }
2617}
2618
2619
2620/* Prepend the list of glyph strings with head H and tail T to the
2621 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2622 result. */
2623
2624static INLINE void
2625x_prepend_glyph_string_lists (head, tail, h, t)
2626 struct glyph_string **head, **tail;
2627 struct glyph_string *h, *t;
2628{
2629 if (h)
2630 {
2631 if (*head)
2632 (*head)->prev = t;
2633 else
2634 *tail = t;
2635 t->next = *head;
2636 *head = h;
2637 }
2638}
2639
2640
2641/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2642 Set *HEAD and *TAIL to the resulting list. */
2643
2644static INLINE void
2645x_append_glyph_string (head, tail, s)
2646 struct glyph_string **head, **tail;
2647 struct glyph_string *s;
2648{
2649 s->next = s->prev = NULL;
2650 x_append_glyph_string_lists (head, tail, s, s);
2651}
2652
2653
2654/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2655 face. */
2656
2657static void
2658x_set_cursor_gc (s)
2659 struct glyph_string *s;
2660{
2661 if (s->font == FRAME_FONT (s->f)
2662 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2663 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2664 && !s->cmp)
06a2c219
GM
2665 s->gc = s->f->output_data.x->cursor_gc;
2666 else
2667 {
2668 /* Cursor on non-default face: must merge. */
2669 XGCValues xgcv;
2670 unsigned long mask;
2671
2672 xgcv.background = s->f->output_data.x->cursor_pixel;
2673 xgcv.foreground = s->face->background;
2674
2675 /* If the glyph would be invisible, try a different foreground. */
2676 if (xgcv.foreground == xgcv.background)
2677 xgcv.foreground = s->face->foreground;
2678 if (xgcv.foreground == xgcv.background)
2679 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2680 if (xgcv.foreground == xgcv.background)
2681 xgcv.foreground = s->face->foreground;
2682
2683 /* Make sure the cursor is distinct from text in this face. */
2684 if (xgcv.background == s->face->background
2685 && xgcv.foreground == s->face->foreground)
2686 {
2687 xgcv.background = s->face->foreground;
2688 xgcv.foreground = s->face->background;
2689 }
2690
2691 IF_DEBUG (x_check_font (s->f, s->font));
2692 xgcv.font = s->font->fid;
2693 xgcv.graphics_exposures = False;
2694 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2695
2696 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2697 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2698 mask, &xgcv);
2699 else
2700 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2701 = XCreateGC (s->display, s->window, mask, &xgcv);
2702
2703 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2704 }
2705}
2706
2707
2708/* Set up S->gc of glyph string S for drawing text in mouse face. */
2709
2710static void
2711x_set_mouse_face_gc (s)
2712 struct glyph_string *s;
2713{
2714 int face_id;
ee569018 2715 struct face *face;
06a2c219 2716
e4ded23c 2717 /* What face has to be used last for the mouse face? */
06a2c219 2718 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2719 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2720 if (face == NULL)
2721 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2722
033e3e18
GM
2723 if (s->first_glyph->type == CHAR_GLYPH)
2724 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2725 else
2726 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2727 s->face = FACE_FROM_ID (s->f, face_id);
2728 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2729
2730 /* If font in this face is same as S->font, use it. */
2731 if (s->font == s->face->font)
2732 s->gc = s->face->gc;
2733 else
2734 {
2735 /* Otherwise construct scratch_cursor_gc with values from FACE
2736 but font FONT. */
2737 XGCValues xgcv;
2738 unsigned long mask;
2739
2740 xgcv.background = s->face->background;
2741 xgcv.foreground = s->face->foreground;
2742 IF_DEBUG (x_check_font (s->f, s->font));
2743 xgcv.font = s->font->fid;
2744 xgcv.graphics_exposures = False;
2745 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2746
2747 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2748 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2749 mask, &xgcv);
2750 else
2751 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2752 = XCreateGC (s->display, s->window, mask, &xgcv);
2753
2754 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2755 }
2756
2757 xassert (s->gc != 0);
2758}
2759
2760
2761/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2762 Faces to use in the mode line have already been computed when the
2763 matrix was built, so there isn't much to do, here. */
2764
2765static INLINE void
2766x_set_mode_line_face_gc (s)
2767 struct glyph_string *s;
2768{
2769 s->gc = s->face->gc;
06a2c219
GM
2770}
2771
2772
2773/* Set S->gc of glyph string S for drawing that glyph string. Set
2774 S->stippled_p to a non-zero value if the face of S has a stipple
2775 pattern. */
2776
2777static INLINE void
2778x_set_glyph_string_gc (s)
2779 struct glyph_string *s;
2780{
209f68d9
GM
2781 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2782
06a2c219
GM
2783 if (s->hl == DRAW_NORMAL_TEXT)
2784 {
2785 s->gc = s->face->gc;
2786 s->stippled_p = s->face->stipple != 0;
2787 }
2788 else if (s->hl == DRAW_INVERSE_VIDEO)
2789 {
2790 x_set_mode_line_face_gc (s);
2791 s->stippled_p = s->face->stipple != 0;
2792 }
2793 else if (s->hl == DRAW_CURSOR)
2794 {
2795 x_set_cursor_gc (s);
2796 s->stippled_p = 0;
2797 }
2798 else if (s->hl == DRAW_MOUSE_FACE)
2799 {
2800 x_set_mouse_face_gc (s);
2801 s->stippled_p = s->face->stipple != 0;
2802 }
2803 else if (s->hl == DRAW_IMAGE_RAISED
2804 || s->hl == DRAW_IMAGE_SUNKEN)
2805 {
2806 s->gc = s->face->gc;
2807 s->stippled_p = s->face->stipple != 0;
2808 }
2809 else
2810 {
2811 s->gc = s->face->gc;
2812 s->stippled_p = s->face->stipple != 0;
2813 }
2814
2815 /* GC must have been set. */
2816 xassert (s->gc != 0);
2817}
2818
2819
2820/* Return in *R the clipping rectangle for glyph string S. */
2821
2822static void
2823x_get_glyph_string_clip_rect (s, r)
2824 struct glyph_string *s;
2825 XRectangle *r;
2826{
2827 if (s->row->full_width_p)
2828 {
2829 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2830 int canon_x = CANON_X_UNIT (s->f);
2831
2832 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2833 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2834
2835 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2836 {
1da3fd71 2837 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2838 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2839 r->x -= width;
2840 }
2841
b9432a85 2842 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2843
06a2c219
GM
2844 /* Unless displaying a mode or menu bar line, which are always
2845 fully visible, clip to the visible part of the row. */
2846 if (s->w->pseudo_window_p)
2847 r->height = s->row->visible_height;
2848 else
2849 r->height = s->height;
2850 }
2851 else
2852 {
2853 /* This is a text line that may be partially visible. */
2854 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2855 r->width = window_box_width (s->w, s->area);
2856 r->height = s->row->visible_height;
2857 }
2858
66ac4b0e
GM
2859 /* If S draws overlapping rows, it's sufficient to use the top and
2860 bottom of the window for clipping because this glyph string
2861 intentionally draws over other lines. */
2862 if (s->for_overlaps_p)
2863 {
045dee35 2864 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2865 r->height = window_text_bottom_y (s->w) - r->y;
2866 }
98b8a90f
GM
2867 else
2868 {
2869 /* Don't use S->y for clipping because it doesn't take partially
2870 visible lines into account. For example, it can be negative for
2871 partially visible lines at the top of a window. */
2872 if (!s->row->full_width_p
2873 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2874 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2875 else
2876 r->y = max (0, s->row->y);
2877
2878 /* If drawing a tool-bar window, draw it over the internal border
2879 at the top of the window. */
2880 if (s->w == XWINDOW (s->f->tool_bar_window))
2881 r->y -= s->f->output_data.x->internal_border_width;
2882 }
2883
66ac4b0e 2884 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2885}
2886
2887
2888/* Set clipping for output of glyph string S. S may be part of a mode
2889 line or menu if we don't have X toolkit support. */
2890
2891static INLINE void
2892x_set_glyph_string_clipping (s)
2893 struct glyph_string *s;
2894{
2895 XRectangle r;
2896 x_get_glyph_string_clip_rect (s, &r);
2897 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2898}
2899
2900
2901/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2902 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2903
2904static INLINE void
2905x_compute_glyph_string_overhangs (s)
2906 struct glyph_string *s;
2907{
b4192550 2908 if (s->cmp == NULL
06a2c219
GM
2909 && s->first_glyph->type == CHAR_GLYPH)
2910 {
2911 XCharStruct cs;
2912 int direction, font_ascent, font_descent;
2913 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2914 &font_ascent, &font_descent, &cs);
2915 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2916 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2917 }
2918}
2919
2920
2921/* Compute overhangs and x-positions for glyph string S and its
2922 predecessors, or successors. X is the starting x-position for S.
2923 BACKWARD_P non-zero means process predecessors. */
2924
2925static void
2926x_compute_overhangs_and_x (s, x, backward_p)
2927 struct glyph_string *s;
2928 int x;
2929 int backward_p;
2930{
2931 if (backward_p)
2932 {
2933 while (s)
2934 {
2935 x_compute_glyph_string_overhangs (s);
2936 x -= s->width;
2937 s->x = x;
2938 s = s->prev;
2939 }
2940 }
2941 else
2942 {
2943 while (s)
2944 {
2945 x_compute_glyph_string_overhangs (s);
2946 s->x = x;
2947 x += s->width;
2948 s = s->next;
2949 }
2950 }
2951}
2952
2953
2954/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2955 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2956 assumed to be zero. */
06a2c219
GM
2957
2958static void
2959x_get_glyph_overhangs (glyph, f, left, right)
2960 struct glyph *glyph;
2961 struct frame *f;
2962 int *left, *right;
2963{
06a2c219
GM
2964 *left = *right = 0;
2965
b4192550 2966 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2967 {
2968 XFontStruct *font;
2969 struct face *face;
2970 struct font_info *font_info;
2971 XChar2b char2b;
ee569018
KH
2972 XCharStruct *pcm;
2973
2974 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2975 font = face->font;
2976 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2977 if (font
2978 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2979 {
06a2c219
GM
2980 if (pcm->rbearing > pcm->width)
2981 *right = pcm->rbearing - pcm->width;
2982 if (pcm->lbearing < 0)
2983 *left = -pcm->lbearing;
2984 }
2985 }
2986}
2987
2988
2989/* Return the index of the first glyph preceding glyph string S that
2990 is overwritten by S because of S's left overhang. Value is -1
2991 if no glyphs are overwritten. */
2992
2993static int
2994x_left_overwritten (s)
2995 struct glyph_string *s;
2996{
2997 int k;
2998
2999 if (s->left_overhang)
3000 {
3001 int x = 0, i;
3002 struct glyph *glyphs = s->row->glyphs[s->area];
3003 int first = s->first_glyph - glyphs;
3004
3005 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3006 x -= glyphs[i].pixel_width;
3007
3008 k = i + 1;
3009 }
3010 else
3011 k = -1;
3012
3013 return k;
3014}
3015
3016
3017/* Return the index of the first glyph preceding glyph string S that
3018 is overwriting S because of its right overhang. Value is -1 if no
3019 glyph in front of S overwrites S. */
3020
3021static int
3022x_left_overwriting (s)
3023 struct glyph_string *s;
3024{
3025 int i, k, x;
3026 struct glyph *glyphs = s->row->glyphs[s->area];
3027 int first = s->first_glyph - glyphs;
3028
3029 k = -1;
3030 x = 0;
3031 for (i = first - 1; i >= 0; --i)
3032 {
3033 int left, right;
3034 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3035 if (x + right > 0)
3036 k = i;
3037 x -= glyphs[i].pixel_width;
3038 }
3039
3040 return k;
3041}
3042
3043
3044/* Return the index of the last glyph following glyph string S that is
3045 not overwritten by S because of S's right overhang. Value is -1 if
3046 no such glyph is found. */
3047
3048static int
3049x_right_overwritten (s)
3050 struct glyph_string *s;
3051{
3052 int k = -1;
3053
3054 if (s->right_overhang)
3055 {
3056 int x = 0, i;
3057 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3058 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3059 int end = s->row->used[s->area];
3060
3061 for (i = first; i < end && s->right_overhang > x; ++i)
3062 x += glyphs[i].pixel_width;
3063
3064 k = i;
3065 }
3066
3067 return k;
3068}
3069
3070
3071/* Return the index of the last glyph following glyph string S that
3072 overwrites S because of its left overhang. Value is negative
3073 if no such glyph is found. */
3074
3075static int
3076x_right_overwriting (s)
3077 struct glyph_string *s;
3078{
3079 int i, k, x;
3080 int end = s->row->used[s->area];
3081 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3082 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3083
3084 k = -1;
3085 x = 0;
3086 for (i = first; i < end; ++i)
3087 {
3088 int left, right;
3089 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3090 if (x - left < 0)
3091 k = i;
3092 x += glyphs[i].pixel_width;
3093 }
3094
3095 return k;
3096}
3097
3098
3099/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3100
3101static INLINE void
3102x_clear_glyph_string_rect (s, x, y, w, h)
3103 struct glyph_string *s;
3104 int x, y, w, h;
3105{
3106 XGCValues xgcv;
3107 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3108 XSetForeground (s->display, s->gc, xgcv.background);
3109 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3110 XSetForeground (s->display, s->gc, xgcv.foreground);
3111}
3112
3113
3114/* Draw the background of glyph_string S. If S->background_filled_p
3115 is non-zero don't draw it. FORCE_P non-zero means draw the
3116 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3117 when a string preceding S draws into the background of S, or S
3118 contains the first component of a composition. */
06a2c219
GM
3119
3120static void
3121x_draw_glyph_string_background (s, force_p)
3122 struct glyph_string *s;
3123 int force_p;
3124{
3125 /* Nothing to do if background has already been drawn or if it
3126 shouldn't be drawn in the first place. */
3127 if (!s->background_filled_p)
3128 {
ea2ba0d4
KH
3129 int box_line_width = max (s->face->box_line_width, 0);
3130
b4192550 3131 if (s->stippled_p)
06a2c219
GM
3132 {
3133 /* Fill background with a stipple pattern. */
3134 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3135 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3136 s->y + box_line_width,
06a2c219 3137 s->background_width,
ea2ba0d4 3138 s->height - 2 * box_line_width);
06a2c219
GM
3139 XSetFillStyle (s->display, s->gc, FillSolid);
3140 s->background_filled_p = 1;
3141 }
ea2ba0d4 3142 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3143 || s->font_not_found_p
3144 || s->extends_to_end_of_line_p
06a2c219
GM
3145 || force_p)
3146 {
ea2ba0d4 3147 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3148 s->background_width,
ea2ba0d4 3149 s->height - 2 * box_line_width);
06a2c219
GM
3150 s->background_filled_p = 1;
3151 }
3152 }
3153}
3154
3155
3156/* Draw the foreground of glyph string S. */
3157
3158static void
3159x_draw_glyph_string_foreground (s)
3160 struct glyph_string *s;
3161{
3162 int i, x;
3163
3164 /* If first glyph of S has a left box line, start drawing the text
3165 of S to the right of that box line. */
3166 if (s->face->box != FACE_NO_BOX
3167 && s->first_glyph->left_box_line_p)
ea2ba0d4 3168 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3169 else
3170 x = s->x;
3171
b4192550
KH
3172 /* Draw characters of S as rectangles if S's font could not be
3173 loaded. */
3174 if (s->font_not_found_p)
06a2c219 3175 {
b4192550 3176 for (i = 0; i < s->nchars; ++i)
06a2c219 3177 {
b4192550
KH
3178 struct glyph *g = s->first_glyph + i;
3179 XDrawRectangle (s->display, s->window,
3180 s->gc, x, s->y, g->pixel_width - 1,
3181 s->height - 1);
3182 x += g->pixel_width;
06a2c219
GM
3183 }
3184 }
3185 else
3186 {
b4192550
KH
3187 char *char1b = (char *) s->char2b;
3188 int boff = s->font_info->baseline_offset;
06a2c219 3189
b4192550
KH
3190 if (s->font_info->vertical_centering)
3191 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3192
3193 /* If we can use 8-bit functions, condense S->char2b. */
3194 if (!s->two_byte_p)
3195 for (i = 0; i < s->nchars; ++i)
3196 char1b[i] = s->char2b[i].byte2;
3197
3198 /* Draw text with XDrawString if background has already been
3199 filled. Otherwise, use XDrawImageString. (Note that
3200 XDrawImageString is usually faster than XDrawString.) Always
3201 use XDrawImageString when drawing the cursor so that there is
3202 no chance that characters under a box cursor are invisible. */
3203 if (s->for_overlaps_p
3204 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3205 {
3206 /* Draw characters with 16-bit or 8-bit functions. */
3207 if (s->two_byte_p)
3208 XDrawString16 (s->display, s->window, s->gc, x,
3209 s->ybase - boff, s->char2b, s->nchars);
3210 else
3211 XDrawString (s->display, s->window, s->gc, x,
3212 s->ybase - boff, char1b, s->nchars);
3213 }
06a2c219
GM
3214 else
3215 {
b4192550
KH
3216 if (s->two_byte_p)
3217 XDrawImageString16 (s->display, s->window, s->gc, x,
3218 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3219 else
b4192550
KH
3220 XDrawImageString (s->display, s->window, s->gc, x,
3221 s->ybase - boff, char1b, s->nchars);
3222 }
3223 }
3224}
06a2c219 3225
b4192550 3226/* Draw the foreground of composite glyph string S. */
06a2c219 3227
b4192550
KH
3228static void
3229x_draw_composite_glyph_string_foreground (s)
3230 struct glyph_string *s;
3231{
3232 int i, x;
06a2c219 3233
b4192550
KH
3234 /* If first glyph of S has a left box line, start drawing the text
3235 of S to the right of that box line. */
3236 if (s->face->box != FACE_NO_BOX
3237 && s->first_glyph->left_box_line_p)
ea2ba0d4 3238 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3239 else
3240 x = s->x;
06a2c219 3241
b4192550
KH
3242 /* S is a glyph string for a composition. S->gidx is the index of
3243 the first character drawn for glyphs of this composition.
3244 S->gidx == 0 means we are drawing the very first character of
3245 this composition. */
06a2c219 3246
b4192550
KH
3247 /* Draw a rectangle for the composition if the font for the very
3248 first character of the composition could not be loaded. */
3249 if (s->font_not_found_p)
3250 {
3251 if (s->gidx == 0)
3252 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3253 s->width - 1, s->height - 1);
3254 }
3255 else
3256 {
3257 for (i = 0; i < s->nchars; i++, ++s->gidx)
3258 XDrawString16 (s->display, s->window, s->gc,
3259 x + s->cmp->offsets[s->gidx * 2],
3260 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3261 s->char2b + i, 1);
06a2c219
GM
3262 }
3263}
3264
3265
80c32bcc
GM
3266#ifdef USE_X_TOOLKIT
3267
3e71d8f2 3268static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
3269static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3270 XrmValue *, XrmValue *, XtPointer *));
3271static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3272 XrmValue *, Cardinal *));
80c32bcc 3273
3e71d8f2
GM
3274
3275/* Return the frame on which widget WIDGET is used.. Abort if frame
3276 cannot be determined. */
3277
e851c833 3278static struct frame *
3e71d8f2 3279x_frame_of_widget (widget)
80c32bcc 3280 Widget widget;
80c32bcc 3281{
80c32bcc 3282 struct x_display_info *dpyinfo;
5c187dee 3283 Lisp_Object tail;
3e71d8f2
GM
3284 struct frame *f;
3285
80c32bcc
GM
3286 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3287
3288 /* Find the top-level shell of the widget. Note that this function
3289 can be called when the widget is not yet realized, so XtWindow
3290 (widget) == 0. That's the reason we can't simply use
3291 x_any_window_to_frame. */
3292 while (!XtIsTopLevelShell (widget))
3293 widget = XtParent (widget);
3294
3295 /* Look for a frame with that top-level widget. Allocate the color
3296 on that frame to get the right gamma correction value. */
3297 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3298 if (GC_FRAMEP (XCAR (tail))
3299 && (f = XFRAME (XCAR (tail)),
3300 (f->output_data.nothing != 1
3301 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3302 && f->output_data.x->widget == widget)
3e71d8f2 3303 return f;
80c32bcc
GM
3304
3305 abort ();
3306}
3307
3e71d8f2
GM
3308
3309/* Allocate the color COLOR->pixel on the screen and display of
3310 widget WIDGET in colormap CMAP. If an exact match cannot be
3311 allocated, try the nearest color available. Value is non-zero
3312 if successful. This is called from lwlib. */
3313
3314int
3315x_alloc_nearest_color_for_widget (widget, cmap, color)
3316 Widget widget;
3317 Colormap cmap;
3318 XColor *color;
3319{
3320 struct frame *f = x_frame_of_widget (widget);
3321 return x_alloc_nearest_color (f, cmap, color);
3322}
3323
3324
46d516e5
MB
3325/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3326 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3327 If this produces the same color as PIXEL, try a color where all RGB
3328 values have DELTA added. Return the allocated color in *PIXEL.
3329 DISPLAY is the X display, CMAP is the colormap to operate on.
3330 Value is non-zero if successful. */
3331
3332int
3333x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3334 Widget widget;
3335 Display *display;
3336 Colormap cmap;
3337 unsigned long *pixel;
3338 double factor;
3339 int delta;
3340{
3341 struct frame *f = x_frame_of_widget (widget);
3342 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3343}
3344
3345
651f03b6
GM
3346/* Structure specifying which arguments should be passed by Xt to
3347 cvt_string_to_pixel. We want the widget's screen and colormap. */
3348
3349static XtConvertArgRec cvt_string_to_pixel_args[] =
3350 {
3351 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3352 sizeof (Screen *)},
3353 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3354 sizeof (Colormap)}
3355 };
3356
3357
3358/* The address of this variable is returned by
3359 cvt_string_to_pixel. */
3360
3361static Pixel cvt_string_to_pixel_value;
3362
3363
3364/* Convert a color name to a pixel color.
3365
3366 DPY is the display we are working on.
3367
3368 ARGS is an array of *NARGS XrmValue structures holding additional
3369 information about the widget for which the conversion takes place.
3370 The contents of this array are determined by the specification
3371 in cvt_string_to_pixel_args.
3372
3373 FROM is a pointer to an XrmValue which points to the color name to
3374 convert. TO is an XrmValue in which to return the pixel color.
3375
3376 CLOSURE_RET is a pointer to user-data, in which we record if
3377 we allocated the color or not.
3378
3379 Value is True if successful, False otherwise. */
3380
3381static Boolean
3382cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3383 Display *dpy;
3384 XrmValue *args;
3385 Cardinal *nargs;
3386 XrmValue *from, *to;
3387 XtPointer *closure_ret;
3388{
3389 Screen *screen;
3390 Colormap cmap;
3391 Pixel pixel;
3392 String color_name;
3393 XColor color;
3394
3395 if (*nargs != 2)
3396 {
3397 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3398 "wrongParameters", "cvt_string_to_pixel",
3399 "XtToolkitError",
3400 "Screen and colormap args required", NULL, NULL);
3401 return False;
3402 }
3403
3404 screen = *(Screen **) args[0].addr;
3405 cmap = *(Colormap *) args[1].addr;
3406 color_name = (String) from->addr;
3407
3408 if (strcmp (color_name, XtDefaultBackground) == 0)
3409 {
3410 *closure_ret = (XtPointer) False;
3411 pixel = WhitePixelOfScreen (screen);
3412 }
3413 else if (strcmp (color_name, XtDefaultForeground) == 0)
3414 {
3415 *closure_ret = (XtPointer) False;
3416 pixel = BlackPixelOfScreen (screen);
3417 }
3418 else if (XParseColor (dpy, cmap, color_name, &color)
3419 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3420 {
3421 pixel = color.pixel;
3422 *closure_ret = (XtPointer) True;
3423 }
3424 else
3425 {
3426 String params[1];
3427 Cardinal nparams = 1;
3428
3429 params[0] = color_name;
3430 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3431 "badValue", "cvt_string_to_pixel",
3432 "XtToolkitError", "Invalid color `%s'",
3433 params, &nparams);
3434 return False;
3435 }
3436
3437 if (to->addr != NULL)
3438 {
3439 if (to->size < sizeof (Pixel))
3440 {
3441 to->size = sizeof (Pixel);
3442 return False;
3443 }
3444
3445 *(Pixel *) to->addr = pixel;
3446 }
3447 else
3448 {
3449 cvt_string_to_pixel_value = pixel;
3450 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3451 }
3452
3453 to->size = sizeof (Pixel);
3454 return True;
3455}
3456
3457
3458/* Free a pixel color which was previously allocated via
3459 cvt_string_to_pixel. This is registered as the destructor
3460 for this type of resource via XtSetTypeConverter.
3461
3462 APP is the application context in which we work.
3463
3464 TO is a pointer to an XrmValue holding the color to free.
3465 CLOSURE is the value we stored in CLOSURE_RET for this color
3466 in cvt_string_to_pixel.
3467
3468 ARGS and NARGS are like for cvt_string_to_pixel. */
3469
3470static void
3471cvt_pixel_dtor (app, to, closure, args, nargs)
3472 XtAppContext app;
3473 XrmValuePtr to;
3474 XtPointer closure;
3475 XrmValuePtr args;
3476 Cardinal *nargs;
3477{
3478 if (*nargs != 2)
3479 {
3480 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3481 "XtToolkitError",
3482 "Screen and colormap arguments required",
3483 NULL, NULL);
3484 }
3485 else if (closure != NULL)
3486 {
3487 /* We did allocate the pixel, so free it. */
3488 Screen *screen = *(Screen **) args[0].addr;
3489 Colormap cmap = *(Colormap *) args[1].addr;
3490 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 3491 (Pixel *) to->addr, 1);
651f03b6
GM
3492 }
3493}
3494
3495
80c32bcc
GM
3496#endif /* USE_X_TOOLKIT */
3497
3498
f04e1297 3499/* Value is an array of XColor structures for the contents of the
651f03b6 3500 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
3501 Note that this probably shouldn't be called for large color maps,
3502 say a 24-bit TrueColor map. */
3503
3504static const XColor *
651f03b6
GM
3505x_color_cells (dpy, ncells)
3506 Display *dpy;
f04e1297
GM
3507 int *ncells;
3508{
651f03b6 3509 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
3510
3511 if (dpyinfo->color_cells == NULL)
3512 {
651f03b6 3513 Screen *screen = dpyinfo->screen;
f04e1297
GM
3514 int i;
3515
3516 dpyinfo->ncolor_cells
651f03b6 3517 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
3518 dpyinfo->color_cells
3519 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3520 * sizeof *dpyinfo->color_cells);
3521
3522 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3523 dpyinfo->color_cells[i].pixel = i;
3524
651f03b6 3525 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
3526 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3527 }
3528
3529 *ncells = dpyinfo->ncolor_cells;
3530 return dpyinfo->color_cells;
3531}
3532
3533
3534/* On frame F, translate pixel colors to RGB values for the NCOLORS
3535 colors in COLORS. Use cached information, if available. */
3536
3537void
3538x_query_colors (f, colors, ncolors)
3539 struct frame *f;
3540 XColor *colors;
3541 int ncolors;
3542{
3543 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3544
3545 if (dpyinfo->color_cells)
3546 {
3547 int i;
3548 for (i = 0; i < ncolors; ++i)
3549 {
3550 unsigned long pixel = colors[i].pixel;
3551 xassert (pixel < dpyinfo->ncolor_cells);
3552 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3553 colors[i] = dpyinfo->color_cells[pixel];
3554 }
3555 }
3556 else
3557 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3558}
3559
3560
3561/* On frame F, translate pixel color to RGB values for the color in
3562 COLOR. Use cached information, if available. */
3563
3564void
3565x_query_color (f, color)
3566 struct frame *f;
3567 XColor *color;
3568{
3569 x_query_colors (f, color, 1);
3570}
3571
3572
651f03b6
GM
3573/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3574 exact match can't be allocated, try the nearest color available.
3575 Value is non-zero if successful. Set *COLOR to the color
3576 allocated. */
06a2c219 3577
651f03b6
GM
3578static int
3579x_alloc_nearest_color_1 (dpy, cmap, color)
3580 Display *dpy;
06a2c219
GM
3581 Colormap cmap;
3582 XColor *color;
3583{
80c32bcc
GM
3584 int rc;
3585
651f03b6 3586 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
3587 if (rc == 0)
3588 {
3589 /* If we got to this point, the colormap is full, so we're going
3590 to try to get the next closest color. The algorithm used is
3591 a least-squares matching, which is what X uses for closest
3592 color matching with StaticColor visuals. */
3593 int nearest, i;
3594 unsigned long nearest_delta = ~0;
f04e1297 3595 int ncells;
651f03b6 3596 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
3597
3598 for (nearest = i = 0; i < ncells; ++i)
3599 {
3600 long dred = (color->red >> 8) - (cells[i].red >> 8);
3601 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3602 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3603 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3604
3605 if (delta < nearest_delta)
3606 {
3607 nearest = i;
3608 nearest_delta = delta;
3609 }
3610 }
3611
3612 color->red = cells[nearest].red;
3613 color->green = cells[nearest].green;
3614 color->blue = cells[nearest].blue;
651f03b6 3615 rc = XAllocColor (dpy, cmap, color);
06a2c219 3616 }
35efe0a1
GM
3617 else
3618 {
3619 /* If allocation succeeded, and the allocated pixel color is not
3620 equal to a cached pixel color recorded earlier, there was a
3621 change in the colormap, so clear the color cache. */
651f03b6 3622 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1
GM
3623 XColor *cached_color;
3624
3625 if (dpyinfo->color_cells
3626 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3627 (cached_color->red != color->red
3628 || cached_color->blue != color->blue
3629 || cached_color->green != color->green)))
35efe0a1
GM
3630 {
3631 xfree (dpyinfo->color_cells);
3632 dpyinfo->color_cells = NULL;
3633 dpyinfo->ncolor_cells = 0;
3634 }
3635 }
06a2c219 3636
d9c545da
GM
3637#ifdef DEBUG_X_COLORS
3638 if (rc)
3639 register_color (color->pixel);
3640#endif /* DEBUG_X_COLORS */
3641
06a2c219
GM
3642 return rc;
3643}
3644
3645
651f03b6
GM
3646/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3647 exact match can't be allocated, try the nearest color available.
3648 Value is non-zero if successful. Set *COLOR to the color
3649 allocated. */
3650
3651int
3652x_alloc_nearest_color (f, cmap, color)
3653 struct frame *f;
3654 Colormap cmap;
3655 XColor *color;
3656{
3657 gamma_correct (f, color);
3658 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3659}
3660
3661
d9c545da
GM
3662/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3663 It's necessary to do this instead of just using PIXEL directly to
3664 get color reference counts right. */
3665
3666unsigned long
3667x_copy_color (f, pixel)
3668 struct frame *f;
3669 unsigned long pixel;
3670{
3671 XColor color;
3672
3673 color.pixel = pixel;
3674 BLOCK_INPUT;
f04e1297 3675 x_query_color (f, &color);
d9c545da
GM
3676 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3677 UNBLOCK_INPUT;
3678#ifdef DEBUG_X_COLORS
3679 register_color (pixel);
3680#endif
3681 return color.pixel;
3682}
3683
3684
3e71d8f2
GM
3685/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3686 It's necessary to do this instead of just using PIXEL directly to
3687 get color reference counts right. */
3688
3689unsigned long
3690x_copy_dpy_color (dpy, cmap, pixel)
3691 Display *dpy;
3692 Colormap cmap;
3693 unsigned long pixel;
3694{
3695 XColor color;
3696
3697 color.pixel = pixel;
3698 BLOCK_INPUT;
3699 XQueryColor (dpy, cmap, &color);
3700 XAllocColor (dpy, cmap, &color);
3701 UNBLOCK_INPUT;
3702#ifdef DEBUG_X_COLORS
3703 register_color (pixel);
3704#endif
3705 return color.pixel;
3706}
3707
3708
6d8b0acd 3709/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3710 boosted.
6d8b0acd 3711
d7361edf
MB
3712 Nominally, highlight colors for `3d' faces are calculated by
3713 brightening an object's color by a constant scale factor, but this
3714 doesn't yield good results for dark colors, so for colors who's
3715 brightness is less than this value (on a scale of 0-65535) have an
3716 use an additional additive factor.
6d8b0acd
MB
3717
3718 The value here is set so that the default menu-bar/mode-line color
3719 (grey75) will not have its highlights changed at all. */
3720#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3721
3722
06a2c219
GM
3723/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3724 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3725 If this produces the same color as PIXEL, try a color where all RGB
3726 values have DELTA added. Return the allocated color in *PIXEL.
3727 DISPLAY is the X display, CMAP is the colormap to operate on.
3728 Value is non-zero if successful. */
3729
3730static int
3731x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3732 struct frame *f;
3733 Display *display;
3734 Colormap cmap;
3735 unsigned long *pixel;
68c45bf0 3736 double factor;
06a2c219
GM
3737 int delta;
3738{
3739 XColor color, new;
6d8b0acd 3740 long bright;
06a2c219
GM
3741 int success_p;
3742
3743 /* Get RGB color values. */
3744 color.pixel = *pixel;
f04e1297 3745 x_query_color (f, &color);
06a2c219
GM
3746
3747 /* Change RGB values by specified FACTOR. Avoid overflow! */
3748 xassert (factor >= 0);
3749 new.red = min (0xffff, factor * color.red);
3750 new.green = min (0xffff, factor * color.green);
3751 new.blue = min (0xffff, factor * color.blue);
3752
d7361edf
MB
3753 /* Calculate brightness of COLOR. */
3754 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3755
3756 /* We only boost colors that are darker than
3757 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3758 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3759 /* Make an additive adjustment to NEW, because it's dark enough so
3760 that scaling by FACTOR alone isn't enough. */
3761 {
3762 /* How far below the limit this color is (0 - 1, 1 being darker). */
3763 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3764 /* The additive adjustment. */
d7361edf 3765 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3766
3767 if (factor < 1)
3768 {
6d8b0acd
MB
3769 new.red = max (0, new.red - min_delta);
3770 new.green = max (0, new.green - min_delta);
3771 new.blue = max (0, new.blue - min_delta);
3772 }
3773 else
3774 {
3775 new.red = min (0xffff, min_delta + new.red);
3776 new.green = min (0xffff, min_delta + new.green);
3777 new.blue = min (0xffff, min_delta + new.blue);
3778 }
3779 }
3780
06a2c219 3781 /* Try to allocate the color. */
80c32bcc 3782 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3783 if (success_p)
3784 {
3785 if (new.pixel == *pixel)
3786 {
3787 /* If we end up with the same color as before, try adding
3788 delta to the RGB values. */
0d605c67 3789 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3790
3791 new.red = min (0xffff, delta + color.red);
3792 new.green = min (0xffff, delta + color.green);
3793 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3794 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3795 }
3796 else
3797 success_p = 1;
3798 *pixel = new.pixel;
3799 }
3800
3801 return success_p;
3802}
3803
3804
3805/* Set up the foreground color for drawing relief lines of glyph
3806 string S. RELIEF is a pointer to a struct relief containing the GC
3807 with which lines will be drawn. Use a color that is FACTOR or
3808 DELTA lighter or darker than the relief's background which is found
3809 in S->f->output_data.x->relief_background. If such a color cannot
3810 be allocated, use DEFAULT_PIXEL, instead. */
3811
3812static void
3813x_setup_relief_color (f, relief, factor, delta, default_pixel)
3814 struct frame *f;
3815 struct relief *relief;
68c45bf0 3816 double factor;
06a2c219
GM
3817 int delta;
3818 unsigned long default_pixel;
3819{
3820 XGCValues xgcv;
3821 struct x_output *di = f->output_data.x;
3822 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3823 unsigned long pixel;
3824 unsigned long background = di->relief_background;
43bd1b2b 3825 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3826 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3827 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3828
3829 xgcv.graphics_exposures = False;
3830 xgcv.line_width = 1;
3831
3832 /* Free previously allocated color. The color cell will be reused
3833 when it has been freed as many times as it was allocated, so this
3834 doesn't affect faces using the same colors. */
3835 if (relief->gc
3836 && relief->allocated_p)
3837 {
0d605c67 3838 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3839 relief->allocated_p = 0;
3840 }
3841
3842 /* Allocate new color. */
3843 xgcv.foreground = default_pixel;
3844 pixel = background;
dcd08bfb
GM
3845 if (dpyinfo->n_planes != 1
3846 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3847 {
3848 relief->allocated_p = 1;
3849 xgcv.foreground = relief->pixel = pixel;
3850 }
3851
3852 if (relief->gc == 0)
3853 {
dcd08bfb 3854 xgcv.stipple = dpyinfo->gray;
06a2c219 3855 mask |= GCStipple;
dcd08bfb 3856 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3857 }
3858 else
dcd08bfb 3859 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3860}
3861
3862
3863/* Set up colors for the relief lines around glyph string S. */
3864
3865static void
3866x_setup_relief_colors (s)
3867 struct glyph_string *s;
3868{
3869 struct x_output *di = s->f->output_data.x;
3870 unsigned long color;
3871
3872 if (s->face->use_box_color_for_shadows_p)
3873 color = s->face->box_color;
e2a57b34 3874 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 3875 && s->img->pixmap
e2a57b34
MB
3876 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3877 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
3878 else
3879 {
3880 XGCValues xgcv;
3881
3882 /* Get the background color of the face. */
3883 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3884 color = xgcv.background;
3885 }
3886
3887 if (di->white_relief.gc == 0
3888 || color != di->relief_background)
3889 {
3890 di->relief_background = color;
3891 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3892 WHITE_PIX_DEFAULT (s->f));
3893 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3894 BLACK_PIX_DEFAULT (s->f));
3895 }
3896}
3897
3898
3899/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3900 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3901 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3902 relief. LEFT_P non-zero means draw a relief on the left side of
3903 the rectangle. RIGHT_P non-zero means draw a relief on the right
3904 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3905 when drawing. */
3906
3907static void
3908x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3909 raised_p, left_p, right_p, clip_rect)
3910 struct frame *f;
3911 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3912 XRectangle *clip_rect;
3913{
de507556
GM
3914 Display *dpy = FRAME_X_DISPLAY (f);
3915 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
3916 int i;
3917 GC gc;
3918
3919 if (raised_p)
3920 gc = f->output_data.x->white_relief.gc;
3921 else
3922 gc = f->output_data.x->black_relief.gc;
de507556 3923 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3924
3925 /* Top. */
3926 for (i = 0; i < width; ++i)
de507556 3927 XDrawLine (dpy, window, gc,
06a2c219
GM
3928 left_x + i * left_p, top_y + i,
3929 right_x + 1 - i * right_p, top_y + i);
3930
3931 /* Left. */
3932 if (left_p)
3933 for (i = 0; i < width; ++i)
de507556 3934 XDrawLine (dpy, window, gc,
44655e77 3935 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 3936
de507556 3937 XSetClipMask (dpy, gc, None);
06a2c219
GM
3938 if (raised_p)
3939 gc = f->output_data.x->black_relief.gc;
3940 else
3941 gc = f->output_data.x->white_relief.gc;
de507556 3942 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
3943
3944 /* Bottom. */
3945 for (i = 0; i < width; ++i)
de507556
GM
3946 XDrawLine (dpy, window, gc,
3947 left_x + i * left_p, bottom_y - i,
327f42ee 3948 right_x + 1 - i * right_p, bottom_y - i);
06a2c219
GM
3949
3950 /* Right. */
3951 if (right_p)
3952 for (i = 0; i < width; ++i)
de507556 3953 XDrawLine (dpy, window, gc,
06a2c219
GM
3954 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3955
de507556 3956 XSetClipMask (dpy, gc, None);
06a2c219
GM
3957}
3958
3959
3960/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3961 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3962 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3963 left side of the rectangle. RIGHT_P non-zero means draw a line
3964 on the right side of the rectangle. CLIP_RECT is the clipping
3965 rectangle to use when drawing. */
3966
3967static void
3968x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3969 left_p, right_p, clip_rect)
3970 struct glyph_string *s;
3971 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3972 XRectangle *clip_rect;
3973{
3974 XGCValues xgcv;
3975
3976 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3977 XSetForeground (s->display, s->gc, s->face->box_color);
3978 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3979
3980 /* Top. */
3981 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3982 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3983
3984 /* Left. */
3985 if (left_p)
3986 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3987 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3988
3989 /* Bottom. */
3990 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3991 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3992
3993 /* Right. */
3994 if (right_p)
3995 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3996 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3997
3998 XSetForeground (s->display, s->gc, xgcv.foreground);
3999 XSetClipMask (s->display, s->gc, None);
4000}
4001
4002
4003/* Draw a box around glyph string S. */
4004
4005static void
4006x_draw_glyph_string_box (s)
4007 struct glyph_string *s;
4008{
4009 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
4010 int left_p, right_p;
4011 struct glyph *last_glyph;
4012 XRectangle clip_rect;
4013
4014 last_x = window_box_right (s->w, s->area);
4015 if (s->row->full_width_p
4016 && !s->w->pseudo_window_p)
4017 {
3f332ef3 4018 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
06a2c219
GM
4019 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4020 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4021 }
4022
4023 /* The glyph that may have a right box line. */
b4192550 4024 last_glyph = (s->cmp || s->img
06a2c219
GM
4025 ? s->first_glyph
4026 : s->first_glyph + s->nchars - 1);
4027
ea2ba0d4 4028 width = abs (s->face->box_line_width);
06a2c219
GM
4029 raised_p = s->face->box == FACE_RAISED_BOX;
4030 left_x = s->x;
57ac7c81
GM
4031 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4032 ? last_x - 1
4033 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
4034 top_y = s->y;
4035 bottom_y = top_y + s->height - 1;
4036
4037 left_p = (s->first_glyph->left_box_line_p
4038 || (s->hl == DRAW_MOUSE_FACE
4039 && (s->prev == NULL
4040 || s->prev->hl != s->hl)));
4041 right_p = (last_glyph->right_box_line_p
4042 || (s->hl == DRAW_MOUSE_FACE
4043 && (s->next == NULL
4044 || s->next->hl != s->hl)));
327f42ee 4045
06a2c219
GM
4046 x_get_glyph_string_clip_rect (s, &clip_rect);
4047
4048 if (s->face->box == FACE_SIMPLE_BOX)
4049 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4050 left_p, right_p, &clip_rect);
4051 else
4052 {
4053 x_setup_relief_colors (s);
4054 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4055 width, raised_p, left_p, right_p, &clip_rect);
4056 }
4057}
4058
4059
4060/* Draw foreground of image glyph string S. */
4061
4062static void
4063x_draw_image_foreground (s)
4064 struct glyph_string *s;
4065{
4066 int x;
95af8492 4067 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4068
4069 /* If first glyph of S has a left box line, start drawing it to the
4070 right of that line. */
4071 if (s->face->box != FACE_NO_BOX
4072 && s->first_glyph->left_box_line_p)
ea2ba0d4 4073 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4074 else
4075 x = s->x;
4076
4077 /* If there is a margin around the image, adjust x- and y-position
4078 by that margin. */
22d650b8
GM
4079 x += s->img->hmargin;
4080 y += s->img->vmargin;
06a2c219
GM
4081
4082 if (s->img->pixmap)
4083 {
4084 if (s->img->mask)
4085 {
4086 /* We can't set both a clip mask and use XSetClipRectangles
4087 because the latter also sets a clip mask. We also can't
4088 trust on the shape extension to be available
4089 (XShapeCombineRegion). So, compute the rectangle to draw
4090 manually. */
4091 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4092 | GCFunction);
4093 XGCValues xgcv;
4094 XRectangle clip_rect, image_rect, r;
4095
4096 xgcv.clip_mask = s->img->mask;
4097 xgcv.clip_x_origin = x;
4098 xgcv.clip_y_origin = y;
4099 xgcv.function = GXcopy;
4100 XChangeGC (s->display, s->gc, mask, &xgcv);
4101
4102 x_get_glyph_string_clip_rect (s, &clip_rect);
4103 image_rect.x = x;
4104 image_rect.y = y;
4105 image_rect.width = s->img->width;
4106 image_rect.height = s->img->height;
4107 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4108 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4109 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4110 }
4111 else
4112 {
49ad1d99
GM
4113 XRectangle clip_rect, image_rect, r;
4114
4115 x_get_glyph_string_clip_rect (s, &clip_rect);
4116 image_rect.x = x;
4117 image_rect.y = y;
4118 image_rect.width = s->img->width;
4119 image_rect.height = s->img->height;
4120 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4121 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4122 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
4123
4124 /* When the image has a mask, we can expect that at
4125 least part of a mouse highlight or a block cursor will
4126 be visible. If the image doesn't have a mask, make
4127 a block cursor visible by drawing a rectangle around
4128 the image. I believe it's looking better if we do
4129 nothing here for mouse-face. */
4130 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4131 {
4132 int r = s->img->relief;
4133 if (r < 0) r = -r;
4134 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4135 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4136 }
06a2c219
GM
4137 }
4138 }
4139 else
4140 /* Draw a rectangle if image could not be loaded. */
4141 XDrawRectangle (s->display, s->window, s->gc, x, y,
4142 s->img->width - 1, s->img->height - 1);
4143}
4144
4145
4146/* Draw a relief around the image glyph string S. */
4147
4148static void
4149x_draw_image_relief (s)
4150 struct glyph_string *s;
4151{
4152 int x0, y0, x1, y1, thick, raised_p;
4153 XRectangle r;
4154 int x;
95af8492 4155 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
4156
4157 /* If first glyph of S has a left box line, start drawing it to the
4158 right of that line. */
4159 if (s->face->box != FACE_NO_BOX
4160 && s->first_glyph->left_box_line_p)
ea2ba0d4 4161 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
4162 else
4163 x = s->x;
4164
4165 /* If there is a margin around the image, adjust x- and y-position
4166 by that margin. */
22d650b8
GM
4167 x += s->img->hmargin;
4168 y += s->img->vmargin;
06a2c219
GM
4169
4170 if (s->hl == DRAW_IMAGE_SUNKEN
4171 || s->hl == DRAW_IMAGE_RAISED)
4172 {
62854fe2 4173 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
4174 raised_p = s->hl == DRAW_IMAGE_RAISED;
4175 }
4176 else
4177 {
4178 thick = abs (s->img->relief);
4179 raised_p = s->img->relief > 0;
4180 }
4181
4182 x0 = x - thick;
4183 y0 = y - thick;
4184 x1 = x + s->img->width + thick - 1;
4185 y1 = y + s->img->height + thick - 1;
4186
4187 x_setup_relief_colors (s);
4188 x_get_glyph_string_clip_rect (s, &r);
4189 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4190}
4191
4192
4193/* Draw the foreground of image glyph string S to PIXMAP. */
4194
4195static void
4196x_draw_image_foreground_1 (s, pixmap)
4197 struct glyph_string *s;
4198 Pixmap pixmap;
4199{
4200 int x;
95af8492 4201 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4202
4203 /* If first glyph of S has a left box line, start drawing it to the
4204 right of that line. */
4205 if (s->face->box != FACE_NO_BOX
4206 && s->first_glyph->left_box_line_p)
ea2ba0d4 4207 x = abs (s->face->box_line_width);
06a2c219
GM
4208 else
4209 x = 0;
4210
4211 /* If there is a margin around the image, adjust x- and y-position
4212 by that margin. */
22d650b8
GM
4213 x += s->img->hmargin;
4214 y += s->img->vmargin;
dc43ef94 4215
06a2c219
GM
4216 if (s->img->pixmap)
4217 {
4218 if (s->img->mask)
4219 {
4220 /* We can't set both a clip mask and use XSetClipRectangles
4221 because the latter also sets a clip mask. We also can't
4222 trust on the shape extension to be available
4223 (XShapeCombineRegion). So, compute the rectangle to draw
4224 manually. */
4225 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4226 | GCFunction);
4227 XGCValues xgcv;
4228
4229 xgcv.clip_mask = s->img->mask;
4230 xgcv.clip_x_origin = x;
4231 xgcv.clip_y_origin = y;
4232 xgcv.function = GXcopy;
4233 XChangeGC (s->display, s->gc, mask, &xgcv);
4234
4235 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4236 0, 0, s->img->width, s->img->height, x, y);
4237 XSetClipMask (s->display, s->gc, None);
4238 }
4239 else
4240 {
4241 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4242 0, 0, s->img->width, s->img->height, x, y);
4243
4244 /* When the image has a mask, we can expect that at
4245 least part of a mouse highlight or a block cursor will
4246 be visible. If the image doesn't have a mask, make
4247 a block cursor visible by drawing a rectangle around
4248 the image. I believe it's looking better if we do
4249 nothing here for mouse-face. */
4250 if (s->hl == DRAW_CURSOR)
534c20b2
KS
4251 {
4252 int r = s->img->relief;
4253 if (r < 0) r = -r;
4254 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4255 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4256 }
06a2c219
GM
4257 }
4258 }
4259 else
4260 /* Draw a rectangle if image could not be loaded. */
4261 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4262 s->img->width - 1, s->img->height - 1);
4263}
dc43ef94 4264
990ba854 4265
06a2c219
GM
4266/* Draw part of the background of glyph string S. X, Y, W, and H
4267 give the rectangle to draw. */
a9a5b0a5 4268
06a2c219
GM
4269static void
4270x_draw_glyph_string_bg_rect (s, x, y, w, h)
4271 struct glyph_string *s;
4272 int x, y, w, h;
4273{
4274 if (s->stippled_p)
4275 {
4276 /* Fill background with a stipple pattern. */
4277 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4278 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4279 XSetFillStyle (s->display, s->gc, FillSolid);
4280 }
4281 else
4282 x_clear_glyph_string_rect (s, x, y, w, h);
4283}
07e34cb0 4284
b5210ea7 4285
06a2c219 4286/* Draw image glyph string S.
dc43ef94 4287
06a2c219
GM
4288 s->y
4289 s->x +-------------------------
4290 | s->face->box
4291 |
4292 | +-------------------------
4293 | | s->img->margin
4294 | |
4295 | | +-------------------
4296 | | | the image
dc43ef94 4297
06a2c219 4298 */
dc43ef94 4299
06a2c219
GM
4300static void
4301x_draw_image_glyph_string (s)
4302 struct glyph_string *s;
4303{
4304 int x, y;
ea2ba0d4
KH
4305 int box_line_hwidth = abs (s->face->box_line_width);
4306 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4307 int height;
4308 Pixmap pixmap = None;
4309
ea2ba0d4 4310 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4311
4312 /* Fill background with face under the image. Do it only if row is
4313 taller than image or if image has a clip mask to reduce
4314 flickering. */
4315 s->stippled_p = s->face->stipple != 0;
4316 if (height > s->img->height
22d650b8
GM
4317 || s->img->hmargin
4318 || s->img->vmargin
06a2c219
GM
4319 || s->img->mask
4320 || s->img->pixmap == 0
4321 || s->width != s->background_width)
4322 {
ea2ba0d4
KH
4323 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4324 x = s->x + box_line_hwidth;
06a2c219
GM
4325 else
4326 x = s->x;
4327
ea2ba0d4 4328 y = s->y + box_line_vwidth;
06a2c219
GM
4329
4330 if (s->img->mask)
4331 {
f9b5db02
GM
4332 /* Create a pixmap as large as the glyph string. Fill it
4333 with the background color. Copy the image to it, using
4334 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4335 Screen *screen = FRAME_X_SCREEN (s->f);
4336 int depth = DefaultDepthOfScreen (screen);
4337
4338 /* Create a pixmap as large as the glyph string. */
4339 pixmap = XCreatePixmap (s->display, s->window,
4340 s->background_width,
4341 s->height, depth);
4342
4343 /* Don't clip in the following because we're working on the
4344 pixmap. */
4345 XSetClipMask (s->display, s->gc, None);
4346
4347 /* Fill the pixmap with the background color/stipple. */
4348 if (s->stippled_p)
4349 {
4350 /* Fill background with a stipple pattern. */
4351 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4352 XFillRectangle (s->display, pixmap, s->gc,
4353 0, 0, s->background_width, s->height);
4354 XSetFillStyle (s->display, s->gc, FillSolid);
4355 }
4356 else
4357 {
4358 XGCValues xgcv;
4359 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4360 &xgcv);
4361 XSetForeground (s->display, s->gc, xgcv.background);
4362 XFillRectangle (s->display, pixmap, s->gc,
4363 0, 0, s->background_width, s->height);
4364 XSetForeground (s->display, s->gc, xgcv.foreground);
4365 }
4366 }
4367 else
06a2c219
GM
4368 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4369
4370 s->background_filled_p = 1;
4371 }
dc43ef94 4372
06a2c219
GM
4373 /* Draw the foreground. */
4374 if (pixmap != None)
4375 {
4376 x_draw_image_foreground_1 (s, pixmap);
4377 x_set_glyph_string_clipping (s);
4378 XCopyArea (s->display, pixmap, s->window, s->gc,
4379 0, 0, s->background_width, s->height, s->x, s->y);
4380 XFreePixmap (s->display, pixmap);
4381 }
4382 else
4383 x_draw_image_foreground (s);
b5210ea7 4384
06a2c219
GM
4385 /* If we must draw a relief around the image, do it. */
4386 if (s->img->relief
4387 || s->hl == DRAW_IMAGE_RAISED
4388 || s->hl == DRAW_IMAGE_SUNKEN)
4389 x_draw_image_relief (s);
4390}
8c1a6a84 4391
990ba854 4392
06a2c219 4393/* Draw stretch glyph string S. */
dc43ef94 4394
06a2c219
GM
4395static void
4396x_draw_stretch_glyph_string (s)
4397 struct glyph_string *s;
4398{
4399 xassert (s->first_glyph->type == STRETCH_GLYPH);
4400 s->stippled_p = s->face->stipple != 0;
990ba854 4401
06a2c219
GM
4402 if (s->hl == DRAW_CURSOR
4403 && !x_stretch_cursor_p)
4404 {
4405 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4406 as wide as the stretch glyph. */
4407 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4408
06a2c219
GM
4409 /* Draw cursor. */
4410 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4411
06a2c219
GM
4412 /* Clear rest using the GC of the original non-cursor face. */
4413 if (width < s->background_width)
4414 {
06a2c219
GM
4415 int x = s->x + width, y = s->y;
4416 int w = s->background_width - width, h = s->height;
4417 XRectangle r;
b7f83f9e 4418 GC gc;
dc43ef94 4419
b7f83f9e
GM
4420 if (s->row->mouse_face_p
4421 && cursor_in_mouse_face_p (s->w))
4422 {
4423 x_set_mouse_face_gc (s);
4424 gc = s->gc;
4425 }
4426 else
4427 gc = s->face->gc;
4428
06a2c219
GM
4429 x_get_glyph_string_clip_rect (s, &r);
4430 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
b7f83f9e 4431
06a2c219
GM
4432 if (s->face->stipple)
4433 {
4434 /* Fill background with a stipple pattern. */
4435 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4436 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4437 XSetFillStyle (s->display, gc, FillSolid);
4438 }
4439 else
4440 {
4441 XGCValues xgcv;
4442 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4443 XSetForeground (s->display, gc, xgcv.background);
4444 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4445 XSetForeground (s->display, gc, xgcv.foreground);
4446 }
4447 }
4448 }
61e9f9f3 4449 else if (!s->background_filled_p)
06a2c219
GM
4450 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4451 s->height);
4452
4453 s->background_filled_p = 1;
4454}
4455
4456
4457/* Draw glyph string S. */
4458
4459static void
4460x_draw_glyph_string (s)
4461 struct glyph_string *s;
4462{
4458cf11
KH
4463 int relief_drawn_p = 0;
4464
06a2c219
GM
4465 /* If S draws into the background of its successor, draw the
4466 background of the successor first so that S can draw into it.
4467 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4468 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4469 {
4470 xassert (s->next->img == NULL);
4471 x_set_glyph_string_gc (s->next);
4472 x_set_glyph_string_clipping (s->next);
4473 x_draw_glyph_string_background (s->next, 1);
4474 }
97210f4e 4475
06a2c219
GM
4476 /* Set up S->gc, set clipping and draw S. */
4477 x_set_glyph_string_gc (s);
06a2c219 4478
4458cf11
KH
4479 /* Draw relief (if any) in advance for char/composition so that the
4480 glyph string can be drawn over it. */
4481 if (!s->for_overlaps_p
4482 && s->face->box != FACE_NO_BOX
4483 && (s->first_glyph->type == CHAR_GLYPH
4484 || s->first_glyph->type == COMPOSITE_GLYPH))
4485
4486 {
e6269cbb 4487 x_set_glyph_string_clipping (s);
4458cf11
KH
4488 x_draw_glyph_string_background (s, 1);
4489 x_draw_glyph_string_box (s);
e6269cbb 4490 x_set_glyph_string_clipping (s);
4458cf11
KH
4491 relief_drawn_p = 1;
4492 }
e6269cbb
GM
4493 else
4494 x_set_glyph_string_clipping (s);
4458cf11 4495
06a2c219
GM
4496 switch (s->first_glyph->type)
4497 {
4498 case IMAGE_GLYPH:
4499 x_draw_image_glyph_string (s);
4500 break;
4501
4502 case STRETCH_GLYPH:
4503 x_draw_stretch_glyph_string (s);
4504 break;
4505
4506 case CHAR_GLYPH:
66ac4b0e
GM
4507 if (s->for_overlaps_p)
4508 s->background_filled_p = 1;
4509 else
4510 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4511 x_draw_glyph_string_foreground (s);
4512 break;
4513
b4192550
KH
4514 case COMPOSITE_GLYPH:
4515 if (s->for_overlaps_p || s->gidx > 0)
4516 s->background_filled_p = 1;
4517 else
4518 x_draw_glyph_string_background (s, 1);
4519 x_draw_composite_glyph_string_foreground (s);
4520 break;
4521
06a2c219
GM
4522 default:
4523 abort ();
4524 }
4525
66ac4b0e 4526 if (!s->for_overlaps_p)
06a2c219 4527 {
66ac4b0e
GM
4528 /* Draw underline. */
4529 if (s->face->underline_p)
4530 {
e24e84cc
GM
4531 unsigned long tem, h;
4532 int y;
06a2c219 4533
e24e84cc 4534 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4535 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4536 h = 1;
e24e84cc
GM
4537
4538 /* Get the underline position. This is the recommended
4539 vertical offset in pixels from the baseline to the top of
4540 the underline. This is a signed value according to the
4541 specs, and its default is
4542
4543 ROUND ((maximum descent) / 2), with
4544 ROUND(x) = floor (x + 0.5) */
4545
a72d5ce5
GM
4546 if (x_use_underline_position_properties
4547 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
4548 y = s->ybase + (long) tem;
4549 else if (s->face->font)
4550 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4551 else
a02f1be0 4552 y = s->y + s->height - h;
06a2c219 4553
66ac4b0e 4554 if (s->face->underline_defaulted_p)
e24e84cc
GM
4555 XFillRectangle (s->display, s->window, s->gc,
4556 s->x, y, s->width, h);
66ac4b0e
GM
4557 else
4558 {
4559 XGCValues xgcv;
4560 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4561 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4562 XFillRectangle (s->display, s->window, s->gc,
4563 s->x, y, s->width, h);
66ac4b0e
GM
4564 XSetForeground (s->display, s->gc, xgcv.foreground);
4565 }
dc6f92b8 4566 }
07e34cb0 4567
66ac4b0e
GM
4568 /* Draw overline. */
4569 if (s->face->overline_p)
06a2c219 4570 {
66ac4b0e
GM
4571 unsigned long dy = 0, h = 1;
4572
4573 if (s->face->overline_color_defaulted_p)
4574 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4575 s->width, h);
4576 else
4577 {
4578 XGCValues xgcv;
4579 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4580 XSetForeground (s->display, s->gc, s->face->overline_color);
4581 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4582 s->width, h);
4583 XSetForeground (s->display, s->gc, xgcv.foreground);
4584 }
06a2c219 4585 }
06a2c219 4586
66ac4b0e
GM
4587 /* Draw strike-through. */
4588 if (s->face->strike_through_p)
06a2c219 4589 {
66ac4b0e
GM
4590 unsigned long h = 1;
4591 unsigned long dy = (s->height - h) / 2;
4592
4593 if (s->face->strike_through_color_defaulted_p)
4594 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4595 s->width, h);
4596 else
4597 {
4598 XGCValues xgcv;
4599 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4600 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4601 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4602 s->width, h);
4603 XSetForeground (s->display, s->gc, xgcv.foreground);
4604 }
06a2c219 4605 }
06a2c219 4606
4458cf11
KH
4607 /* Draw relief if not yet drawn. */
4608 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
4609 x_draw_glyph_string_box (s);
4610 }
06a2c219
GM
4611
4612 /* Reset clipping. */
4613 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4614}
07e34cb0 4615
06a2c219 4616
b4192550
KH
4617static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4618 struct face **, int));
06a2c219 4619
06a2c219 4620
209f68d9
GM
4621/* Fill glyph string S with composition components specified by S->cmp.
4622
b4192550
KH
4623 FACES is an array of faces for all components of this composition.
4624 S->gidx is the index of the first component for S.
4625 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4626 use its physical height for clipping.
06a2c219 4627
b4192550 4628 Value is the index of a component not in S. */
07e34cb0 4629
b4192550
KH
4630static int
4631x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4632 struct glyph_string *s;
b4192550 4633 struct face **faces;
66ac4b0e 4634 int overlaps_p;
07e34cb0 4635{
b4192550 4636 int i;
06a2c219 4637
b4192550 4638 xassert (s);
06a2c219 4639
b4192550 4640 s->for_overlaps_p = overlaps_p;
06a2c219 4641
b4192550
KH
4642 s->face = faces[s->gidx];
4643 s->font = s->face->font;
4644 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4645
b4192550
KH
4646 /* For all glyphs of this composition, starting at the offset
4647 S->gidx, until we reach the end of the definition or encounter a
4648 glyph that requires the different face, add it to S. */
4649 ++s->nchars;
4650 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4651 ++s->nchars;
06a2c219 4652
b4192550
KH
4653 /* All glyph strings for the same composition has the same width,
4654 i.e. the width set for the first component of the composition. */
06a2c219 4655
06a2c219
GM
4656 s->width = s->first_glyph->pixel_width;
4657
4658 /* If the specified font could not be loaded, use the frame's
4659 default font, but record the fact that we couldn't load it in
4660 the glyph string so that we can draw rectangles for the
4661 characters of the glyph string. */
4662 if (s->font == NULL)
4663 {
4664 s->font_not_found_p = 1;
4665 s->font = FRAME_FONT (s->f);
4666 }
4667
4668 /* Adjust base line for subscript/superscript text. */
4669 s->ybase += s->first_glyph->voffset;
4670
4671 xassert (s->face && s->face->gc);
4672
4673 /* This glyph string must always be drawn with 16-bit functions. */
4674 s->two_byte_p = 1;
b4192550
KH
4675
4676 return s->gidx + s->nchars;
06a2c219
GM
4677}
4678
4679
209f68d9
GM
4680/* Fill glyph string S from a sequence of character glyphs.
4681
06a2c219 4682 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4683 first glyph to consider, END is the index of the last + 1.
4684 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4685 use its physical height for clipping.
66ac4b0e
GM
4686
4687 Value is the index of the first glyph not in S. */
06a2c219
GM
4688
4689static int
66ac4b0e 4690x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4691 struct glyph_string *s;
4692 int face_id;
66ac4b0e 4693 int start, end, overlaps_p;
06a2c219
GM
4694{
4695 struct glyph *glyph, *last;
4696 int voffset;
ee569018 4697 int glyph_not_available_p;
06a2c219 4698
06a2c219
GM
4699 xassert (s->f == XFRAME (s->w->frame));
4700 xassert (s->nchars == 0);
4701 xassert (start >= 0 && end > start);
4702
66ac4b0e 4703 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4704 glyph = s->row->glyphs[s->area] + start;
4705 last = s->row->glyphs[s->area] + end;
4706 voffset = glyph->voffset;
4707
ee569018
KH
4708 glyph_not_available_p = glyph->glyph_not_available_p;
4709
06a2c219
GM
4710 while (glyph < last
4711 && glyph->type == CHAR_GLYPH
4712 && glyph->voffset == voffset
ee569018
KH
4713 /* Same face id implies same font, nowadays. */
4714 && glyph->face_id == face_id
4715 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4716 {
ee569018
KH
4717 int two_byte_p;
4718
06a2c219 4719 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4720 s->char2b + s->nchars,
4721 &two_byte_p);
4722 s->two_byte_p = two_byte_p;
06a2c219
GM
4723 ++s->nchars;
4724 xassert (s->nchars <= end - start);
4725 s->width += glyph->pixel_width;
4726 ++glyph;
4727 }
4728
4729 s->font = s->face->font;
4730 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4731
4732 /* If the specified font could not be loaded, use the frame's font,
4733 but record the fact that we couldn't load it in
4734 S->font_not_found_p so that we can draw rectangles for the
4735 characters of the glyph string. */
ee569018 4736 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4737 {
4738 s->font_not_found_p = 1;
4739 s->font = FRAME_FONT (s->f);
4740 }
4741
4742 /* Adjust base line for subscript/superscript text. */
4743 s->ybase += voffset;
66ac4b0e 4744
06a2c219
GM
4745 xassert (s->face && s->face->gc);
4746 return glyph - s->row->glyphs[s->area];
07e34cb0 4747}
dc6f92b8 4748
06a2c219
GM
4749
4750/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4751
dfcf069d 4752static void
06a2c219
GM
4753x_fill_image_glyph_string (s)
4754 struct glyph_string *s;
4755{
4756 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4757 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4758 xassert (s->img);
43d120d8 4759 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4760 s->font = s->face->font;
4761 s->width = s->first_glyph->pixel_width;
4762
4763 /* Adjust base line for subscript/superscript text. */
4764 s->ybase += s->first_glyph->voffset;
4765}
4766
4767
209f68d9 4768/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4769
209f68d9
GM
4770 ROW is the glyph row in which the glyphs are found, AREA is the
4771 area within the row. START is the index of the first glyph to
4772 consider, END is the index of the last + 1.
4773
4774 Value is the index of the first glyph not in S. */
4775
4776static int
4777x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4778 struct glyph_string *s;
209f68d9
GM
4779 struct glyph_row *row;
4780 enum glyph_row_area area;
4781 int start, end;
06a2c219 4782{
209f68d9
GM
4783 struct glyph *glyph, *last;
4784 int voffset, face_id;
4785
06a2c219 4786 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4787
4788 glyph = s->row->glyphs[s->area] + start;
4789 last = s->row->glyphs[s->area] + end;
4790 face_id = glyph->face_id;
4791 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4792 s->font = s->face->font;
209f68d9
GM
4793 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4794 s->width = glyph->pixel_width;
4795 voffset = glyph->voffset;
4796
4797 for (++glyph;
4798 (glyph < last
4799 && glyph->type == STRETCH_GLYPH
4800 && glyph->voffset == voffset
4801 && glyph->face_id == face_id);
4802 ++glyph)
4803 s->width += glyph->pixel_width;
06a2c219
GM
4804
4805 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4806 s->ybase += voffset;
4807
c296fc01
GM
4808 /* The case that face->gc == 0 is handled when drawing the glyph
4809 string by calling PREPARE_FACE_FOR_DISPLAY. */
4810 xassert (s->face);
209f68d9 4811 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4812}
4813
4814
4815/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4816 of XChar2b structures for S; it can't be allocated in
4817 x_init_glyph_string because it must be allocated via `alloca'. W
4818 is the window on which S is drawn. ROW and AREA are the glyph row
4819 and area within the row from which S is constructed. START is the
4820 index of the first glyph structure covered by S. HL is a
4821 face-override for drawing S. */
4822
4823static void
4824x_init_glyph_string (s, char2b, w, row, area, start, hl)
4825 struct glyph_string *s;
4826 XChar2b *char2b;
4827 struct window *w;
4828 struct glyph_row *row;
4829 enum glyph_row_area area;
4830 int start;
4831 enum draw_glyphs_face hl;
4832{
4833 bzero (s, sizeof *s);
4834 s->w = w;
4835 s->f = XFRAME (w->frame);
4836 s->display = FRAME_X_DISPLAY (s->f);
4837 s->window = FRAME_X_WINDOW (s->f);
4838 s->char2b = char2b;
4839 s->hl = hl;
4840 s->row = row;
4841 s->area = area;
4842 s->first_glyph = row->glyphs[area] + start;
4843 s->height = row->height;
4844 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4845
9ea173e8
GM
4846 /* Display the internal border below the tool-bar window. */
4847 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4848 s->y -= s->f->output_data.x->internal_border_width;
4849
4850 s->ybase = s->y + row->ascent;
4851}
4852
4853
4854/* Set background width of glyph string S. START is the index of the
4855 first glyph following S. LAST_X is the right-most x-position + 1
4856 in the drawing area. */
4857
4858static INLINE void
4859x_set_glyph_string_background_width (s, start, last_x)
4860 struct glyph_string *s;
4861 int start;
4862 int last_x;
4863{
4864 /* If the face of this glyph string has to be drawn to the end of
4865 the drawing area, set S->extends_to_end_of_line_p. */
4866 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4867
4868 if (start == s->row->used[s->area]
eb79f5cc 4869 && s->area == TEXT_AREA
7b0870b2
GM
4870 && ((s->hl == DRAW_NORMAL_TEXT
4871 && (s->row->fill_line_p
4872 || s->face->background != default_face->background
4873 || s->face->stipple != default_face->stipple
4874 || s->row->mouse_face_p))
327f42ee
GM
4875 || s->hl == DRAW_MOUSE_FACE
4876 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4877 && s->row->fill_line_p)))
7b0870b2 4878 s->extends_to_end_of_line_p = 1;
06a2c219
GM
4879
4880 /* If S extends its face to the end of the line, set its
4881 background_width to the distance to the right edge of the drawing
4882 area. */
4883 if (s->extends_to_end_of_line_p)
1da3fd71 4884 s->background_width = last_x - s->x + 1;
06a2c219
GM
4885 else
4886 s->background_width = s->width;
4887}
4888
4889
4890/* Add a glyph string for a stretch glyph to the list of strings
4891 between HEAD and TAIL. START is the index of the stretch glyph in
4892 row area AREA of glyph row ROW. END is the index of the last glyph
4893 in that glyph row area. X is the current output position assigned
4894 to the new glyph string constructed. HL overrides that face of the
4895 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4896 is the right-most x-position of the drawing area. */
4897
8abee2e1
DL
4898/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4899 and below -- keep them on one line. */
4900#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4901 do \
4902 { \
4903 s = (struct glyph_string *) alloca (sizeof *s); \
4904 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4905 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4906 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4907 s->x = (X); \
4908 } \
4909 while (0)
4910
4911
4912/* Add a glyph string for an image glyph to the list of strings
4913 between HEAD and TAIL. START is the index of the image glyph in
4914 row area AREA of glyph row ROW. END is the index of the last glyph
4915 in that glyph row area. X is the current output position assigned
4916 to the new glyph string constructed. HL overrides that face of the
4917 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4918 is the right-most x-position of the drawing area. */
4919
8abee2e1 4920#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4921 do \
4922 { \
4923 s = (struct glyph_string *) alloca (sizeof *s); \
4924 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4925 x_fill_image_glyph_string (s); \
4926 x_append_glyph_string (&HEAD, &TAIL, s); \
4927 ++START; \
4928 s->x = (X); \
4929 } \
4930 while (0)
4931
4932
4933/* Add a glyph string for a sequence of character glyphs to the list
4934 of strings between HEAD and TAIL. START is the index of the first
4935 glyph in row area AREA of glyph row ROW that is part of the new
4936 glyph string. END is the index of the last glyph in that glyph row
4937 area. X is the current output position assigned to the new glyph
4938 string constructed. HL overrides that face of the glyph; e.g. it
4939 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4940 right-most x-position of the drawing area. */
4941
8abee2e1 4942#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4943 do \
4944 { \
3e71d8f2 4945 int c, face_id; \
06a2c219
GM
4946 XChar2b *char2b; \
4947 \
43d120d8 4948 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4949 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4950 \
b4192550
KH
4951 s = (struct glyph_string *) alloca (sizeof *s); \
4952 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4953 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4954 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4955 s->x = (X); \
4956 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4957 OVERLAPS_P); \
06a2c219
GM
4958 } \
4959 while (0)
4960
4961
b4192550
KH
4962/* Add a glyph string for a composite sequence to the list of strings
4963 between HEAD and TAIL. START is the index of the first glyph in
4964 row area AREA of glyph row ROW that is part of the new glyph
4965 string. END is the index of the last glyph in that glyph row area.
4966 X is the current output position assigned to the new glyph string
4967 constructed. HL overrides that face of the glyph; e.g. it is
4968 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4969 x-position of the drawing area. */
4970
6c27ec25 4971#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4972 do { \
43d120d8
KH
4973 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4974 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4975 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4976 struct composition *cmp = composition_table[cmp_id]; \
4977 int glyph_len = cmp->glyph_len; \
4978 XChar2b *char2b; \
4979 struct face **faces; \
4980 struct glyph_string *first_s = NULL; \
4981 int n; \
4982 \
ee569018 4983 base_face = base_face->ascii_face; \
b4192550
KH
4984 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4985 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4986 /* At first, fill in `char2b' and `faces'. */ \
4987 for (n = 0; n < glyph_len; n++) \
4988 { \
43d120d8 4989 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4990 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4991 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4992 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
abfb6b46 4993 this_face_id, char2b + n, 1, 1); \
b4192550
KH
4994 } \
4995 \
4996 /* Make glyph_strings for each glyph sequence that is drawable by \
4997 the same face, and append them to HEAD/TAIL. */ \
4998 for (n = 0; n < cmp->glyph_len;) \
4999 { \
5000 s = (struct glyph_string *) alloca (sizeof *s); \
5001 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
5002 x_append_glyph_string (&(HEAD), &(TAIL), s); \
5003 s->cmp = cmp; \
5004 s->gidx = n; \
b4192550
KH
5005 s->x = (X); \
5006 \
5007 if (n == 0) \
5008 first_s = s; \
5009 \
5010 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
5011 } \
5012 \
5013 ++START; \
5014 s = first_s; \
5015 } while (0)
5016
5017
06a2c219
GM
5018/* Build a list of glyph strings between HEAD and TAIL for the glyphs
5019 of AREA of glyph row ROW on window W between indices START and END.
5020 HL overrides the face for drawing glyph strings, e.g. it is
5021 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5022 x-positions of the drawing area.
5023
5024 This is an ugly monster macro construct because we must use alloca
5025 to allocate glyph strings (because x_draw_glyphs can be called
5026 asynchronously). */
5027
8abee2e1 5028#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
5029 do \
5030 { \
5031 HEAD = TAIL = NULL; \
5032 while (START < END) \
5033 { \
5034 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5035 switch (first_glyph->type) \
5036 { \
5037 case CHAR_GLYPH: \
5038 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
5039 TAIL, HL, X, LAST_X, \
5040 OVERLAPS_P); \
06a2c219
GM
5041 break; \
5042 \
b4192550
KH
5043 case COMPOSITE_GLYPH: \
5044 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5045 HEAD, TAIL, HL, X, LAST_X,\
5046 OVERLAPS_P); \
5047 break; \
5048 \
06a2c219
GM
5049 case STRETCH_GLYPH: \
5050 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5051 HEAD, TAIL, HL, X, LAST_X); \
5052 break; \
5053 \
5054 case IMAGE_GLYPH: \
5055 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5056 TAIL, HL, X, LAST_X); \
5057 break; \
5058 \
5059 default: \
5060 abort (); \
5061 } \
5062 \
5063 x_set_glyph_string_background_width (s, START, LAST_X); \
5064 (X) += s->width; \
5065 } \
5066 } \
5067 while (0)
5068
5069
5070/* Draw glyphs between START and END in AREA of ROW on window W,
5071 starting at x-position X. X is relative to AREA in W. HL is a
5072 face-override with the following meaning:
5073
5074 DRAW_NORMAL_TEXT draw normally
5075 DRAW_CURSOR draw in cursor face
5076 DRAW_MOUSE_FACE draw in mouse face.
5077 DRAW_INVERSE_VIDEO draw in mode line face
5078 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5079 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5080
66ac4b0e
GM
5081 If OVERLAPS_P is non-zero, draw only the foreground of characters
5082 and clip to the physical height of ROW.
5083
06a2c219
GM
5084 Value is the x-position reached, relative to AREA of W. */
5085
5086static int
f0a48a01 5087x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
06a2c219
GM
5088 struct window *w;
5089 int x;
5090 struct glyph_row *row;
5091 enum glyph_row_area area;
5092 int start, end;
5093 enum draw_glyphs_face hl;
66ac4b0e 5094 int overlaps_p;
dc6f92b8 5095{
06a2c219
GM
5096 struct glyph_string *head, *tail;
5097 struct glyph_string *s;
5098 int last_x, area_width;
5099 int x_reached;
5100 int i, j;
5101
5102 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 5103 end = min (end, row->used[area]);
a8710abf
GM
5104 start = max (0, start);
5105 start = min (end, start);
06a2c219
GM
5106
5107 /* Translate X to frame coordinates. Set last_x to the right
5108 end of the drawing area. */
5109 if (row->full_width_p)
5110 {
5111 /* X is relative to the left edge of W, without scroll bars
3f332ef3 5112 or fringes. */
06a2c219 5113 struct frame *f = XFRAME (w->frame);
06a2c219 5114 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 5115
06a2c219
GM
5116 x += window_left_x;
5117 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5118 last_x = window_left_x + area_width;
5119
5120 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5121 {
110859fc 5122 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
5123 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5124 last_x += width;
5125 else
5126 x -= width;
5127 }
dc6f92b8 5128
b9432a85 5129 x += FRAME_INTERNAL_BORDER_WIDTH (f);
98fedd97 5130 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
5131 }
5132 else
dc6f92b8 5133 {
06a2c219
GM
5134 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5135 area_width = window_box_width (w, area);
5136 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
5137 }
5138
06a2c219
GM
5139 /* Build a doubly-linked list of glyph_string structures between
5140 head and tail from what we have to draw. Note that the macro
5141 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5142 the reason we use a separate variable `i'. */
5143 i = start;
66ac4b0e
GM
5144 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5145 overlaps_p);
06a2c219
GM
5146 if (tail)
5147 x_reached = tail->x + tail->background_width;
5148 else
5149 x_reached = x;
90e65f07 5150
06a2c219
GM
5151 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5152 the row, redraw some glyphs in front or following the glyph
5153 strings built above. */
a8710abf 5154 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
5155 {
5156 int dummy_x = 0;
5157 struct glyph_string *h, *t;
5158
5159 /* Compute overhangs for all glyph strings. */
5160 for (s = head; s; s = s->next)
5161 x_compute_glyph_string_overhangs (s);
5162
5163 /* Prepend glyph strings for glyphs in front of the first glyph
5164 string that are overwritten because of the first glyph
5165 string's left overhang. The background of all strings
5166 prepended must be drawn because the first glyph string
5167 draws over it. */
5168 i = x_left_overwritten (head);
5169 if (i >= 0)
5170 {
5171 j = i;
5172 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
5173 DRAW_NORMAL_TEXT, dummy_x, last_x,
5174 overlaps_p);
06a2c219 5175 start = i;
06a2c219
GM
5176 x_compute_overhangs_and_x (t, head->x, 1);
5177 x_prepend_glyph_string_lists (&head, &tail, h, t);
5178 }
58769bee 5179
06a2c219
GM
5180 /* Prepend glyph strings for glyphs in front of the first glyph
5181 string that overwrite that glyph string because of their
5182 right overhang. For these strings, only the foreground must
5183 be drawn, because it draws over the glyph string at `head'.
5184 The background must not be drawn because this would overwrite
5185 right overhangs of preceding glyphs for which no glyph
5186 strings exist. */
5187 i = x_left_overwriting (head);
5188 if (i >= 0)
5189 {
5190 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
5191 DRAW_NORMAL_TEXT, dummy_x, last_x,
5192 overlaps_p);
06a2c219
GM
5193 for (s = h; s; s = s->next)
5194 s->background_filled_p = 1;
06a2c219
GM
5195 x_compute_overhangs_and_x (t, head->x, 1);
5196 x_prepend_glyph_string_lists (&head, &tail, h, t);
5197 }
dbcb258a 5198
06a2c219
GM
5199 /* Append glyphs strings for glyphs following the last glyph
5200 string tail that are overwritten by tail. The background of
5201 these strings has to be drawn because tail's foreground draws
5202 over it. */
5203 i = x_right_overwritten (tail);
5204 if (i >= 0)
5205 {
5206 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5207 DRAW_NORMAL_TEXT, x, last_x,
5208 overlaps_p);
06a2c219
GM
5209 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5210 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219 5211 }
dc6f92b8 5212
06a2c219
GM
5213 /* Append glyph strings for glyphs following the last glyph
5214 string tail that overwrite tail. The foreground of such
5215 glyphs has to be drawn because it writes into the background
5216 of tail. The background must not be drawn because it could
5217 paint over the foreground of following glyphs. */
5218 i = x_right_overwriting (tail);
5219 if (i >= 0)
5220 {
5221 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5222 DRAW_NORMAL_TEXT, x, last_x,
5223 overlaps_p);
06a2c219
GM
5224 for (s = h; s; s = s->next)
5225 s->background_filled_p = 1;
5226 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5227 x_append_glyph_string_lists (&head, &tail, h, t);
06a2c219
GM
5228 }
5229 }
58769bee 5230
06a2c219
GM
5231 /* Draw all strings. */
5232 for (s = head; s; s = s->next)
5233 x_draw_glyph_string (s);
dc6f92b8 5234
bb313871
GM
5235 if (area == TEXT_AREA
5236 && !row->full_width_p
5237 /* When drawing overlapping rows, only the glyph strings'
5238 foreground is drawn, which doesn't erase a cursor
5239 completely. */
5240 && !overlaps_p)
f0a48a01
GM
5241 {
5242 int x0 = head ? head->x : x;
5243 int x1 = tail ? tail->x + tail->background_width : x;
5244
5245 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5246 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5247
bb313871 5248 if (XFASTINT (w->left_margin_width) != 0)
f0a48a01
GM
5249 {
5250 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5251 x0 -= left_area_width;
5252 x1 -= left_area_width;
5253 }
5254
60626bab
GM
5255 notice_overwritten_cursor (w, area, x0, x1,
5256 row->y, MATRIX_ROW_BOTTOM_Y (row));
f0a48a01
GM
5257 }
5258
06a2c219
GM
5259 /* Value is the x-position up to which drawn, relative to AREA of W.
5260 This doesn't include parts drawn because of overhangs. */
5261 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5262 if (!row->full_width_p)
5263 {
f0a48a01 5264 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
06a2c219
GM
5265 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5266 if (area > TEXT_AREA)
5267 x_reached -= window_box_width (w, TEXT_AREA);
5268 }
a8710abf 5269
06a2c219
GM
5270 return x_reached;
5271}
dc6f92b8 5272
dc6f92b8 5273
66ac4b0e
GM
5274/* Fix the display of area AREA of overlapping row ROW in window W. */
5275
5276static void
5277x_fix_overlapping_area (w, row, area)
5278 struct window *w;
5279 struct glyph_row *row;
5280 enum glyph_row_area area;
5281{
5282 int i, x;
5283
5284 BLOCK_INPUT;
5285
5286 if (area == LEFT_MARGIN_AREA)
5287 x = 0;
5288 else if (area == TEXT_AREA)
5289 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5290 else
5291 x = (window_box_width (w, LEFT_MARGIN_AREA)
5292 + window_box_width (w, TEXT_AREA));
5293
5294 for (i = 0; i < row->used[area];)
5295 {
5296 if (row->glyphs[area][i].overlaps_vertically_p)
5297 {
5298 int start = i, start_x = x;
5299
5300 do
5301 {
5302 x += row->glyphs[area][i].pixel_width;
5303 ++i;
5304 }
5305 while (i < row->used[area]
5306 && row->glyphs[area][i].overlaps_vertically_p);
5307
5308 x_draw_glyphs (w, start_x, row, area, start, i,
f0a48a01 5309 DRAW_NORMAL_TEXT, 1);
66ac4b0e
GM
5310 }
5311 else
5312 {
5313 x += row->glyphs[area][i].pixel_width;
5314 ++i;
5315 }
5316 }
5317
5318 UNBLOCK_INPUT;
5319}
5320
5321
06a2c219
GM
5322/* Output LEN glyphs starting at START at the nominal cursor position.
5323 Advance the nominal cursor over the text. The global variable
5324 updated_window contains the window being updated, updated_row is
5325 the glyph row being updated, and updated_area is the area of that
5326 row being updated. */
dc6f92b8 5327
06a2c219
GM
5328static void
5329x_write_glyphs (start, len)
5330 struct glyph *start;
5331 int len;
5332{
f0a48a01 5333 int x, hpos;
d9cdbb3d 5334
06a2c219 5335 xassert (updated_window && updated_row);
dc6f92b8 5336 BLOCK_INPUT;
06a2c219
GM
5337
5338 /* Write glyphs. */
dc6f92b8 5339
06a2c219
GM
5340 hpos = start - updated_row->glyphs[updated_area];
5341 x = x_draw_glyphs (updated_window, output_cursor.x,
5342 updated_row, updated_area,
5343 hpos, hpos + len,
f0a48a01 5344 DRAW_NORMAL_TEXT, 0);
dc6f92b8 5345
6065f9e2
KS
5346 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
5347 if (updated_area == TEXT_AREA
5348 && updated_window->phys_cursor_on_p
5349 && updated_window->phys_cursor.vpos == output_cursor.vpos
5350 && updated_window->phys_cursor.hpos >= hpos
5351 && updated_window->phys_cursor.hpos < hpos + len)
5352 updated_window->phys_cursor_on_p = 0;
5353
dc6f92b8 5354 UNBLOCK_INPUT;
06a2c219
GM
5355
5356 /* Advance the output cursor. */
5357 output_cursor.hpos += len;
5358 output_cursor.x = x;
dc6f92b8
JB
5359}
5360
0cdd0c9f 5361
06a2c219 5362/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5363
06a2c219
GM
5364static void
5365x_insert_glyphs (start, len)
5366 struct glyph *start;
5367 register int len;
5368{
5369 struct frame *f;
5370 struct window *w;
5371 int line_height, shift_by_width, shifted_region_width;
5372 struct glyph_row *row;
5373 struct glyph *glyph;
2beb36f9 5374 int frame_x, frame_y, hpos;
58769bee 5375
06a2c219 5376 xassert (updated_window && updated_row);
0cdd0c9f 5377 BLOCK_INPUT;
06a2c219
GM
5378 w = updated_window;
5379 f = XFRAME (WINDOW_FRAME (w));
5380
5381 /* Get the height of the line we are in. */
5382 row = updated_row;
5383 line_height = row->height;
5384
5385 /* Get the width of the glyphs to insert. */
5386 shift_by_width = 0;
5387 for (glyph = start; glyph < start + len; ++glyph)
5388 shift_by_width += glyph->pixel_width;
5389
5390 /* Get the width of the region to shift right. */
5391 shifted_region_width = (window_box_width (w, updated_area)
5392 - output_cursor.x
5393 - shift_by_width);
5394
5395 /* Shift right. */
bf0ab8a2 5396 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5397 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5398 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5399 f->output_data.x->normal_gc,
5400 frame_x, frame_y,
5401 shifted_region_width, line_height,
5402 frame_x + shift_by_width, frame_y);
5403
5404 /* Write the glyphs. */
5405 hpos = start - row->glyphs[updated_area];
5406 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
f0a48a01 5407 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
5408
5409 /* Advance the output cursor. */
5410 output_cursor.hpos += len;
5411 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5412 UNBLOCK_INPUT;
5413}
0cdd0c9f 5414
0cdd0c9f 5415
06a2c219
GM
5416/* Delete N glyphs at the nominal cursor position. Not implemented
5417 for X frames. */
c83febd7
RS
5418
5419static void
06a2c219
GM
5420x_delete_glyphs (n)
5421 register int n;
c83febd7 5422{
06a2c219 5423 abort ();
c83febd7
RS
5424}
5425
0cdd0c9f 5426
c5e6e06b
GM
5427/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5428 If they are <= 0, this is probably an error. */
5429
5430void
5431x_clear_area (dpy, window, x, y, width, height, exposures)
5432 Display *dpy;
5433 Window window;
5434 int x, y;
5435 int width, height;
5436 int exposures;
5437{
5438 xassert (width > 0 && height > 0);
5439 XClearArea (dpy, window, x, y, width, height, exposures);
5440}
5441
5442
06a2c219
GM
5443/* Erase the current text line from the nominal cursor position
5444 (inclusive) to pixel column TO_X (exclusive). The idea is that
5445 everything from TO_X onward is already erased.
5446
5447 TO_X is a pixel position relative to updated_area of
5448 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5449
06a2c219
GM
5450static void
5451x_clear_end_of_line (to_x)
5452 int to_x;
5453{
5454 struct frame *f;
5455 struct window *w = updated_window;
5456 int max_x, min_y, max_y;
5457 int from_x, from_y, to_y;
5458
5459 xassert (updated_window && updated_row);
5460 f = XFRAME (w->frame);
5461
5462 if (updated_row->full_width_p)
5463 {
5464 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5465 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5466 && !w->pseudo_window_p)
5467 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5468 }
06a2c219
GM
5469 else
5470 max_x = window_box_width (w, updated_area);
5471 max_y = window_text_bottom_y (w);
dc6f92b8 5472
06a2c219
GM
5473 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5474 of window. For TO_X > 0, truncate to end of drawing area. */
5475 if (to_x == 0)
5476 return;
5477 else if (to_x < 0)
5478 to_x = max_x;
5479 else
5480 to_x = min (to_x, max_x);
dbc4e1c1 5481
06a2c219
GM
5482 to_y = min (max_y, output_cursor.y + updated_row->height);
5483
5484 /* Notice if the cursor will be cleared by this operation. */
5485 if (!updated_row->full_width_p)
60626bab
GM
5486 notice_overwritten_cursor (w, updated_area,
5487 output_cursor.x, -1,
5488 updated_row->y,
5489 MATRIX_ROW_BOTTOM_Y (updated_row));
dbc4e1c1 5490
06a2c219
GM
5491 from_x = output_cursor.x;
5492
5493 /* Translate to frame coordinates. */
5494 if (updated_row->full_width_p)
5495 {
5496 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5497 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5498 }
0cdd0c9f
RS
5499 else
5500 {
06a2c219
GM
5501 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5502 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5503 }
5504
045dee35 5505 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5506 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5507 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5508
5509 /* Prevent inadvertently clearing to end of the X window. */
5510 if (to_x > from_x && to_y > from_y)
5511 {
5512 BLOCK_INPUT;
c5e6e06b
GM
5513 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5514 from_x, from_y, to_x - from_x, to_y - from_y,
5515 False);
06a2c219 5516 UNBLOCK_INPUT;
0cdd0c9f 5517 }
0cdd0c9f 5518}
dbc4e1c1 5519
0cdd0c9f 5520
06a2c219 5521/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5522 frame. Otherwise clear the selected frame. */
06a2c219
GM
5523
5524static void
5525x_clear_frame ()
0cdd0c9f 5526{
06a2c219 5527 struct frame *f;
0cdd0c9f 5528
06a2c219
GM
5529 if (updating_frame)
5530 f = updating_frame;
0cdd0c9f 5531 else
b86bd3dd 5532 f = SELECTED_FRAME ();
58769bee 5533
06a2c219
GM
5534 /* Clearing the frame will erase any cursor, so mark them all as no
5535 longer visible. */
5536 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5537 output_cursor.hpos = output_cursor.vpos = 0;
5538 output_cursor.x = -1;
5539
5540 /* We don't set the output cursor here because there will always
5541 follow an explicit cursor_to. */
5542 BLOCK_INPUT;
5543 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5544
5545 /* We have to clear the scroll bars, too. If we have changed
5546 colors or something like that, then they should be notified. */
5547 x_scroll_bar_clear (f);
0cdd0c9f 5548
06a2c219
GM
5549 XFlush (FRAME_X_DISPLAY (f));
5550 UNBLOCK_INPUT;
dc6f92b8 5551}
06a2c219
GM
5552
5553
dc6f92b8 5554\f
dbc4e1c1
JB
5555/* Invert the middle quarter of the frame for .15 sec. */
5556
06a2c219
GM
5557/* We use the select system call to do the waiting, so we have to make
5558 sure it's available. If it isn't, we just won't do visual bells. */
5559
dbc4e1c1
JB
5560#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5561
06a2c219
GM
5562
5563/* Subtract the `struct timeval' values X and Y, storing the result in
5564 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5565
5566static int
5567timeval_subtract (result, x, y)
5568 struct timeval *result, x, y;
5569{
06a2c219
GM
5570 /* Perform the carry for the later subtraction by updating y. This
5571 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5572 if (x.tv_usec < y.tv_usec)
5573 {
5574 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5575 y.tv_usec -= 1000000 * nsec;
5576 y.tv_sec += nsec;
5577 }
06a2c219 5578
dbc4e1c1
JB
5579 if (x.tv_usec - y.tv_usec > 1000000)
5580 {
5581 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5582 y.tv_usec += 1000000 * nsec;
5583 y.tv_sec -= nsec;
5584 }
5585
06a2c219
GM
5586 /* Compute the time remaining to wait. tv_usec is certainly
5587 positive. */
dbc4e1c1
JB
5588 result->tv_sec = x.tv_sec - y.tv_sec;
5589 result->tv_usec = x.tv_usec - y.tv_usec;
5590
06a2c219
GM
5591 /* Return indication of whether the result should be considered
5592 negative. */
dbc4e1c1
JB
5593 return x.tv_sec < y.tv_sec;
5594}
dc6f92b8 5595
dfcf069d 5596void
f676886a
JB
5597XTflash (f)
5598 struct frame *f;
dc6f92b8 5599{
dbc4e1c1 5600 BLOCK_INPUT;
dc6f92b8 5601
dbc4e1c1
JB
5602 {
5603 GC gc;
dc6f92b8 5604
06a2c219
GM
5605 /* Create a GC that will use the GXxor function to flip foreground
5606 pixels into background pixels. */
dbc4e1c1
JB
5607 {
5608 XGCValues values;
dc6f92b8 5609
dbc4e1c1 5610 values.function = GXxor;
7556890b
RS
5611 values.foreground = (f->output_data.x->foreground_pixel
5612 ^ f->output_data.x->background_pixel);
58769bee 5613
334208b7 5614 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5615 GCFunction | GCForeground, &values);
5616 }
dc6f92b8 5617
dbc4e1c1 5618 {
e84e14c3
RS
5619 /* Get the height not including a menu bar widget. */
5620 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5621 /* Height of each line to flash. */
5622 int flash_height = FRAME_LINE_HEIGHT (f);
5623 /* These will be the left and right margins of the rectangles. */
5624 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5625 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5626
5627 int width;
5628
5629 /* Don't flash the area between a scroll bar and the frame
5630 edge it is next to. */
5631 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5632 {
5633 case vertical_scroll_bar_left:
5634 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5635 break;
5636
5637 case vertical_scroll_bar_right:
5638 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5639 break;
06a2c219
GM
5640
5641 default:
5642 break;
e84e14c3
RS
5643 }
5644
5645 width = flash_right - flash_left;
5646
5647 /* If window is tall, flash top and bottom line. */
5648 if (height > 3 * FRAME_LINE_HEIGHT (f))
5649 {
5650 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5651 flash_left,
5652 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5653 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5654 width, flash_height);
5655 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5656 flash_left,
5657 (height - flash_height
5658 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5659 width, flash_height);
5660 }
5661 else
5662 /* If it is short, flash it all. */
5663 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5664 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5665 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5666
06a2c219 5667 x_flush (f);
dc6f92b8 5668
dbc4e1c1 5669 {
06a2c219 5670 struct timeval wakeup;
dc6f92b8 5671
66c30ea1 5672 EMACS_GET_TIME (wakeup);
dc6f92b8 5673
dbc4e1c1
JB
5674 /* Compute time to wait until, propagating carry from usecs. */
5675 wakeup.tv_usec += 150000;
5676 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5677 wakeup.tv_usec %= 1000000;
5678
101922c3
GM
5679 /* Keep waiting until past the time wakeup or any input gets
5680 available. */
5681 while (! detect_input_pending ())
dbc4e1c1 5682 {
101922c3 5683 struct timeval current;
dbc4e1c1
JB
5684 struct timeval timeout;
5685
101922c3 5686 EMACS_GET_TIME (current);
dbc4e1c1 5687
101922c3
GM
5688 /* Break if result would be negative. */
5689 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5690 break;
5691
101922c3
GM
5692 /* How long `select' should wait. */
5693 timeout.tv_sec = 0;
5694 timeout.tv_usec = 10000;
5695
dbc4e1c1 5696 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5697 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5698 }
5699 }
58769bee 5700
e84e14c3
RS
5701 /* If window is tall, flash top and bottom line. */
5702 if (height > 3 * FRAME_LINE_HEIGHT (f))
5703 {
5704 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5705 flash_left,
5706 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5707 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5708 width, flash_height);
5709 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5710 flash_left,
5711 (height - flash_height
5712 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5713 width, flash_height);
5714 }
5715 else
5716 /* If it is short, flash it all. */
5717 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5718 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5719 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5720
334208b7 5721 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5722 x_flush (f);
dc6f92b8 5723 }
dbc4e1c1
JB
5724 }
5725
5726 UNBLOCK_INPUT;
dc6f92b8
JB
5727}
5728
06a2c219 5729#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5730
5731
dc6f92b8
JB
5732/* Make audible bell. */
5733
dfcf069d 5734void
dc6f92b8
JB
5735XTring_bell ()
5736{
b86bd3dd
GM
5737 struct frame *f = SELECTED_FRAME ();
5738
5739 if (FRAME_X_DISPLAY (f))
5740 {
dbc4e1c1 5741#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5742 if (visible_bell)
5743 XTflash (f);
5744 else
dbc4e1c1 5745#endif
b86bd3dd
GM
5746 {
5747 BLOCK_INPUT;
5748 XBell (FRAME_X_DISPLAY (f), 0);
5749 XFlush (FRAME_X_DISPLAY (f));
5750 UNBLOCK_INPUT;
5751 }
dc6f92b8
JB
5752 }
5753}
06a2c219 5754
dc6f92b8 5755\f
06a2c219
GM
5756/* Specify how many text lines, from the top of the window,
5757 should be affected by insert-lines and delete-lines operations.
5758 This, and those operations, are used only within an update
5759 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5760
dfcf069d 5761static void
06a2c219
GM
5762XTset_terminal_window (n)
5763 register int n;
dc6f92b8 5764{
06a2c219 5765 /* This function intentionally left blank. */
dc6f92b8
JB
5766}
5767
06a2c219
GM
5768
5769\f
5770/***********************************************************************
5771 Line Dance
5772 ***********************************************************************/
5773
5774/* Perform an insert-lines or delete-lines operation, inserting N
5775 lines or deleting -N lines at vertical position VPOS. */
5776
dfcf069d 5777static void
06a2c219
GM
5778x_ins_del_lines (vpos, n)
5779 int vpos, n;
dc6f92b8
JB
5780{
5781 abort ();
5782}
06a2c219
GM
5783
5784
5785/* Scroll part of the display as described by RUN. */
dc6f92b8 5786
dfcf069d 5787static void
06a2c219
GM
5788x_scroll_run (w, run)
5789 struct window *w;
5790 struct run *run;
dc6f92b8 5791{
06a2c219
GM
5792 struct frame *f = XFRAME (w->frame);
5793 int x, y, width, height, from_y, to_y, bottom_y;
5794
5795 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
5796 without mode lines. Include in this box the left and right
5797 fringe of W. */
06a2c219 5798 window_box (w, -1, &x, &y, &width, &height);
3f332ef3
KS
5799 width += FRAME_X_FRINGE_WIDTH (f);
5800 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
06a2c219
GM
5801
5802 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5803 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5804 bottom_y = y + height;
dc6f92b8 5805
06a2c219
GM
5806 if (to_y < from_y)
5807 {
5808 /* Scrolling up. Make sure we don't copy part of the mode
5809 line at the bottom. */
5810 if (from_y + run->height > bottom_y)
5811 height = bottom_y - from_y;
5812 else
5813 height = run->height;
5814 }
dc6f92b8 5815 else
06a2c219
GM
5816 {
5817 /* Scolling down. Make sure we don't copy over the mode line.
5818 at the bottom. */
5819 if (to_y + run->height > bottom_y)
5820 height = bottom_y - to_y;
5821 else
5822 height = run->height;
5823 }
7a13e894 5824
06a2c219
GM
5825 BLOCK_INPUT;
5826
5827 /* Cursor off. Will be switched on again in x_update_window_end. */
5828 updated_window = w;
5829 x_clear_cursor (w);
5830
5831 XCopyArea (FRAME_X_DISPLAY (f),
5832 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5833 f->output_data.x->normal_gc,
5834 x, from_y,
5835 width, height,
5836 x, to_y);
5837
5838 UNBLOCK_INPUT;
5839}
dc6f92b8 5840
dc6f92b8 5841
06a2c219
GM
5842\f
5843/***********************************************************************
5844 Exposure Events
5845 ***********************************************************************/
5846
5847/* Redisplay an exposed area of frame F. X and Y are the upper-left
5848 corner of the exposed rectangle. W and H are width and height of
5849 the exposed area. All are pixel values. W or H zero means redraw
5850 the entire frame. */
dc6f92b8 5851
06a2c219
GM
5852static void
5853expose_frame (f, x, y, w, h)
5854 struct frame *f;
5855 int x, y, w, h;
dc6f92b8 5856{
06a2c219 5857 XRectangle r;
82f053ab 5858 int mouse_face_overwritten_p = 0;
dc6f92b8 5859
06a2c219 5860 TRACE ((stderr, "expose_frame "));
dc6f92b8 5861
06a2c219
GM
5862 /* No need to redraw if frame will be redrawn soon. */
5863 if (FRAME_GARBAGED_P (f))
dc6f92b8 5864 {
06a2c219
GM
5865 TRACE ((stderr, " garbaged\n"));
5866 return;
5867 }
5868
5869 /* If basic faces haven't been realized yet, there is no point in
5870 trying to redraw anything. This can happen when we get an expose
5871 event while Emacs is starting, e.g. by moving another window. */
5872 if (FRAME_FACE_CACHE (f) == NULL
5873 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5874 {
5875 TRACE ((stderr, " no faces\n"));
5876 return;
58769bee 5877 }
06a2c219
GM
5878
5879 if (w == 0 || h == 0)
58769bee 5880 {
06a2c219
GM
5881 r.x = r.y = 0;
5882 r.width = CANON_X_UNIT (f) * f->width;
5883 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5884 }
5885 else
5886 {
06a2c219
GM
5887 r.x = x;
5888 r.y = y;
5889 r.width = w;
5890 r.height = h;
5891 }
5892
5893 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
82f053ab 5894 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
06a2c219 5895
9ea173e8 5896 if (WINDOWP (f->tool_bar_window))
82f053ab
GM
5897 mouse_face_overwritten_p
5898 |= expose_window (XWINDOW (f->tool_bar_window), &r);
06a2c219
GM
5899
5900#ifndef USE_X_TOOLKIT
5901 if (WINDOWP (f->menu_bar_window))
82f053ab
GM
5902 mouse_face_overwritten_p
5903 |= expose_window (XWINDOW (f->menu_bar_window), &r);
06a2c219 5904#endif /* not USE_X_TOOLKIT */
82f053ab
GM
5905
5906 /* Some window managers support a focus-follows-mouse style with
5907 delayed raising of frames. Imagine a partially obscured frame,
5908 and moving the mouse into partially obscured mouse-face on that
5909 frame. The visible part of the mouse-face will be highlighted,
5910 then the WM raises the obscured frame. With at least one WM, KDE
5911 2.1, Emacs is not getting any event for the raising of the frame
5912 (even tried with SubstructureRedirectMask), only Expose events.
5913 These expose events will draw text normally, i.e. not
5914 highlighted. Which means we must redo the highlight here.
5915 Subsume it under ``we love X''. --gerd 2001-08-15 */
5916 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5917 {
5918 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5919 if (f == dpyinfo->mouse_face_mouse_frame)
5920 {
5921 int x = dpyinfo->mouse_face_mouse_x;
5922 int y = dpyinfo->mouse_face_mouse_y;
5923 clear_mouse_face (dpyinfo);
5924 note_mouse_highlight (f, x, y);
5925 }
5926 }
dc6f92b8
JB
5927}
5928
06a2c219
GM
5929
5930/* Redraw (parts) of all windows in the window tree rooted at W that
82f053ab
GM
5931 intersect R. R contains frame pixel coordinates. Value is
5932 non-zero if the exposure overwrites mouse-face. */
06a2c219 5933
82f053ab 5934static int
06a2c219
GM
5935expose_window_tree (w, r)
5936 struct window *w;
5937 XRectangle *r;
dc6f92b8 5938{
82f053ab
GM
5939 struct frame *f = XFRAME (w->frame);
5940 int mouse_face_overwritten_p = 0;
5941
5942 while (w && !FRAME_GARBAGED_P (f))
06a2c219
GM
5943 {
5944 if (!NILP (w->hchild))
82f053ab
GM
5945 mouse_face_overwritten_p
5946 |= expose_window_tree (XWINDOW (w->hchild), r);
06a2c219 5947 else if (!NILP (w->vchild))
82f053ab
GM
5948 mouse_face_overwritten_p
5949 |= expose_window_tree (XWINDOW (w->vchild), r);
5950 else
5951 mouse_face_overwritten_p |= expose_window (w, r);
5952
a02f1be0 5953 w = NILP (w->next) ? NULL : XWINDOW (w->next);
06a2c219 5954 }
82f053ab
GM
5955
5956 return mouse_face_overwritten_p;
06a2c219 5957}
58769bee 5958
dc6f92b8 5959
06a2c219
GM
5960/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5961 which intersects rectangle R. R is in window-relative coordinates. */
5962
5963static void
5964expose_area (w, row, r, area)
5965 struct window *w;
5966 struct glyph_row *row;
5967 XRectangle *r;
5968 enum glyph_row_area area;
5969{
06a2c219
GM
5970 struct glyph *first = row->glyphs[area];
5971 struct glyph *end = row->glyphs[area] + row->used[area];
5972 struct glyph *last;
4bc6dcc7 5973 int first_x, start_x, x;
06a2c219 5974
6fb13182
GM
5975 if (area == TEXT_AREA && row->fill_line_p)
5976 /* If row extends face to end of line write the whole line. */
153f5ed7 5977 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
f0a48a01 5978 DRAW_NORMAL_TEXT, 0);
6fb13182
GM
5979 else
5980 {
4bc6dcc7
GM
5981 /* Set START_X to the window-relative start position for drawing glyphs of
5982 AREA. The first glyph of the text area can be partially visible.
5983 The first glyphs of other areas cannot. */
5984 if (area == LEFT_MARGIN_AREA)
5985 start_x = 0;
5986 else if (area == TEXT_AREA)
5987 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5988 else
5989 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5990 + window_box_width (w, TEXT_AREA));
5991 x = start_x;
5992
6fb13182
GM
5993 /* Find the first glyph that must be redrawn. */
5994 while (first < end
5995 && x + first->pixel_width < r->x)
5996 {
5997 x += first->pixel_width;
5998 ++first;
5999 }
6000
6001 /* Find the last one. */
6002 last = first;
6003 first_x = x;
6004 while (last < end
6005 && x < r->x + r->width)
6006 {
6007 x += last->pixel_width;
6008 ++last;
6009 }
6010
6011 /* Repaint. */
6012 if (last > first)
4bc6dcc7 6013 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
6014 first - row->glyphs[area],
6015 last - row->glyphs[area],
f0a48a01 6016 DRAW_NORMAL_TEXT, 0);
6fb13182 6017 }
06a2c219
GM
6018}
6019
58769bee 6020
06a2c219 6021/* Redraw the parts of the glyph row ROW on window W intersecting
82f053ab
GM
6022 rectangle R. R is in window-relative coordinates. Value is
6023 non-zero if mouse-face was overwritten. */
dc6f92b8 6024
82f053ab 6025static int
06a2c219
GM
6026expose_line (w, row, r)
6027 struct window *w;
6028 struct glyph_row *row;
6029 XRectangle *r;
6030{
6031 xassert (row->enabled_p);
6032
6033 if (row->mode_line_p || w->pseudo_window_p)
6034 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
f0a48a01 6035 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
6036 else
6037 {
6038 if (row->used[LEFT_MARGIN_AREA])
6039 expose_area (w, row, r, LEFT_MARGIN_AREA);
6040 if (row->used[TEXT_AREA])
6041 expose_area (w, row, r, TEXT_AREA);
6042 if (row->used[RIGHT_MARGIN_AREA])
6043 expose_area (w, row, r, RIGHT_MARGIN_AREA);
3f332ef3 6044 x_draw_row_fringe_bitmaps (w, row);
06a2c219 6045 }
82f053ab
GM
6046
6047 return row->mouse_face_p;
06a2c219 6048}
dc6f92b8 6049
58769bee 6050
06a2c219
GM
6051/* Return non-zero if W's cursor intersects rectangle R. */
6052
6053static int
6054x_phys_cursor_in_rect_p (w, r)
6055 struct window *w;
6056 XRectangle *r;
6057{
6058 XRectangle cr, result;
6059 struct glyph *cursor_glyph;
6060
6061 cursor_glyph = get_phys_cursor_glyph (w);
6062 if (cursor_glyph)
6063 {
6064 cr.x = w->phys_cursor.x;
6065 cr.y = w->phys_cursor.y;
6066 cr.width = cursor_glyph->pixel_width;
6067 cr.height = w->phys_cursor_height;
6068 return x_intersect_rectangles (&cr, r, &result);
6069 }
6070 else
6071 return 0;
dc6f92b8 6072}
dc6f92b8 6073
06a2c219 6074
4f53f3b8
RS
6075/* Redraw those parts of glyphs rows during expose event handling that
6076 overlap other rows. Redrawing of an exposed line writes over parts
6077 of lines overlapping that exposed line; this function fixes that.
6078
6079 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
6080 row in W's current matrix that is exposed and overlaps other rows.
6081 LAST_OVERLAPPING_ROW is the last such row. */
6082
6083static void
6084expose_overlaps (w, first_overlapping_row, last_overlapping_row)
6085 struct window *w;
6086 struct glyph_row *first_overlapping_row;
6087 struct glyph_row *last_overlapping_row;
6088{
6089 struct glyph_row *row;
6090
6091 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
6092 if (row->overlapping_p)
6093 {
6094 xassert (row->enabled_p && !row->mode_line_p);
6095
6096 if (row->used[LEFT_MARGIN_AREA])
6097 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
6098
6099 if (row->used[TEXT_AREA])
6100 x_fix_overlapping_area (w, row, TEXT_AREA);
6101
6102 if (row->used[RIGHT_MARGIN_AREA])
6103 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
6104 }
6105}
6106
6107
a02f1be0
GM
6108/* Redraw the part of window W intersection rectangle FR. Pixel
6109 coordinates in FR are frame-relative. Call this function with
82f053ab
GM
6110 input blocked. Value is non-zero if the exposure overwrites
6111 mouse-face. */
dc6f92b8 6112
a39202f6 6113static int
a02f1be0 6114expose_window (w, fr)
06a2c219 6115 struct window *w;
a02f1be0 6116 XRectangle *fr;
dc6f92b8 6117{
a02f1be0 6118 struct frame *f = XFRAME (w->frame);
a02f1be0 6119 XRectangle wr, r;
82f053ab 6120 int mouse_face_overwritten_p = 0;
dc6f92b8 6121
80c32bcc
GM
6122 /* If window is not yet fully initialized, do nothing. This can
6123 happen when toolkit scroll bars are used and a window is split.
6124 Reconfiguring the scroll bar will generate an expose for a newly
6125 created window. */
a39202f6 6126 if (w->current_matrix == NULL)
82f053ab 6127 return 0;
a39202f6
GM
6128
6129 /* When we're currently updating the window, display and current
6130 matrix usually don't agree. Arrange for a thorough display
6131 later. */
6132 if (w == updated_window)
6133 {
6134 SET_FRAME_GARBAGED (f);
6135 return 0;
6136 }
80c32bcc 6137
a39202f6 6138 /* Frame-relative pixel rectangle of W. */
a02f1be0
GM
6139 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6140 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6141 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6142 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6143
a39202f6
GM
6144 if (x_intersect_rectangles (fr, &wr, &r))
6145 {
6146 int yb = window_text_bottom_y (w);
6147 struct glyph_row *row;
6148 int cursor_cleared_p;
4f53f3b8 6149 struct glyph_row *first_overlapping_row, *last_overlapping_row;
a02f1be0 6150
a39202f6
GM
6151 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6152 r.x, r.y, r.width, r.height));
dc6f92b8 6153
a39202f6
GM
6154 /* Convert to window coordinates. */
6155 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6156 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
dc6f92b8 6157
a39202f6
GM
6158 /* Turn off the cursor. */
6159 if (!w->pseudo_window_p
6160 && x_phys_cursor_in_rect_p (w, &r))
6161 {
6162 x_clear_cursor (w);
6163 cursor_cleared_p = 1;
6164 }
6165 else
6166 cursor_cleared_p = 0;
06a2c219 6167
4f53f3b8
RS
6168 /* Update lines intersecting rectangle R. */
6169 first_overlapping_row = last_overlapping_row = NULL;
a39202f6
GM
6170 for (row = w->current_matrix->rows;
6171 row->enabled_p;
6172 ++row)
6173 {
6174 int y0 = row->y;
6175 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6176
6177 if ((y0 >= r.y && y0 < r.y + r.height)
6178 || (y1 > r.y && y1 < r.y + r.height)
6179 || (r.y >= y0 && r.y < y1)
6180 || (r.y + r.height > y0 && r.y + r.height < y1))
82f053ab 6181 {
4f53f3b8
RS
6182 if (row->overlapping_p)
6183 {
6184 if (first_overlapping_row == NULL)
6185 first_overlapping_row = row;
6186 last_overlapping_row = row;
6187 }
6188
82f053ab
GM
6189 if (expose_line (w, row, &r))
6190 mouse_face_overwritten_p = 1;
6191 }
6192
a39202f6
GM
6193 if (y1 >= yb)
6194 break;
6195 }
dc6f92b8 6196
a39202f6
GM
6197 /* Display the mode line if there is one. */
6198 if (WINDOW_WANTS_MODELINE_P (w)
6199 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6200 row->enabled_p)
6201 && row->y < r.y + r.height)
82f053ab
GM
6202 {
6203 if (expose_line (w, row, &r))
6204 mouse_face_overwritten_p = 1;
6205 }
a39202f6
GM
6206
6207 if (!w->pseudo_window_p)
6208 {
4f53f3b8
RS
6209 /* Fix the display of overlapping rows. */
6210 if (first_overlapping_row)
6211 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
6212
a39202f6
GM
6213 /* Draw border between windows. */
6214 x_draw_vertical_border (w);
06a2c219 6215
a39202f6
GM
6216 /* Turn the cursor on again. */
6217 if (cursor_cleared_p)
6218 x_update_window_cursor (w, 1);
6219 }
06a2c219 6220 }
82f053ab
GM
6221
6222 return mouse_face_overwritten_p;
06a2c219 6223}
dc6f92b8 6224
dc6f92b8 6225
06a2c219
GM
6226/* Determine the intersection of two rectangles R1 and R2. Return
6227 the intersection in *RESULT. Value is non-zero if RESULT is not
6228 empty. */
6229
6230static int
6231x_intersect_rectangles (r1, r2, result)
6232 XRectangle *r1, *r2, *result;
6233{
6234 XRectangle *left, *right;
6235 XRectangle *upper, *lower;
6236 int intersection_p = 0;
6237
6238 /* Rearrange so that R1 is the left-most rectangle. */
6239 if (r1->x < r2->x)
6240 left = r1, right = r2;
6241 else
6242 left = r2, right = r1;
6243
6244 /* X0 of the intersection is right.x0, if this is inside R1,
6245 otherwise there is no intersection. */
6246 if (right->x <= left->x + left->width)
6247 {
6248 result->x = right->x;
6249
6250 /* The right end of the intersection is the minimum of the
6251 the right ends of left and right. */
6252 result->width = (min (left->x + left->width, right->x + right->width)
6253 - result->x);
6254
6255 /* Same game for Y. */
6256 if (r1->y < r2->y)
6257 upper = r1, lower = r2;
6258 else
6259 upper = r2, lower = r1;
6260
6261 /* The upper end of the intersection is lower.y0, if this is inside
6262 of upper. Otherwise, there is no intersection. */
6263 if (lower->y <= upper->y + upper->height)
dc43ef94 6264 {
06a2c219
GM
6265 result->y = lower->y;
6266
6267 /* The lower end of the intersection is the minimum of the lower
6268 ends of upper and lower. */
6269 result->height = (min (lower->y + lower->height,
6270 upper->y + upper->height)
6271 - result->y);
6272 intersection_p = 1;
dc43ef94 6273 }
dc6f92b8
JB
6274 }
6275
06a2c219 6276 return intersection_p;
dc6f92b8 6277}
06a2c219
GM
6278
6279
6280
6281
dc6f92b8 6282\f
dc6f92b8 6283static void
334208b7
RS
6284frame_highlight (f)
6285 struct frame *f;
dc6f92b8 6286{
b3e1e05c
JB
6287 /* We used to only do this if Vx_no_window_manager was non-nil, but
6288 the ICCCM (section 4.1.6) says that the window's border pixmap
6289 and border pixel are window attributes which are "private to the
6290 client", so we can always change it to whatever we want. */
6291 BLOCK_INPUT;
334208b7 6292 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6293 f->output_data.x->border_pixel);
b3e1e05c 6294 UNBLOCK_INPUT;
5d46f928 6295 x_update_cursor (f, 1);
dc6f92b8
JB
6296}
6297
6298static void
334208b7
RS
6299frame_unhighlight (f)
6300 struct frame *f;
dc6f92b8 6301{
b3e1e05c
JB
6302 /* We used to only do this if Vx_no_window_manager was non-nil, but
6303 the ICCCM (section 4.1.6) says that the window's border pixmap
6304 and border pixel are window attributes which are "private to the
6305 client", so we can always change it to whatever we want. */
6306 BLOCK_INPUT;
334208b7 6307 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6308 f->output_data.x->border_tile);
b3e1e05c 6309 UNBLOCK_INPUT;
5d46f928 6310 x_update_cursor (f, 1);
dc6f92b8 6311}
dc6f92b8 6312
f676886a
JB
6313/* The focus has changed. Update the frames as necessary to reflect
6314 the new situation. Note that we can't change the selected frame
c5acd733 6315 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6316 Each event gets marked with the frame in which it occurred, so the
c5acd733 6317 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6318
6d4238f3 6319static void
0f941935
KH
6320x_new_focus_frame (dpyinfo, frame)
6321 struct x_display_info *dpyinfo;
f676886a 6322 struct frame *frame;
dc6f92b8 6323{
0f941935 6324 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6325
0f941935 6326 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6327 {
58769bee 6328 /* Set this before calling other routines, so that they see
f676886a 6329 the correct value of x_focus_frame. */
0f941935 6330 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6331
6332 if (old_focus && old_focus->auto_lower)
f676886a 6333 x_lower_frame (old_focus);
dc6f92b8
JB
6334
6335#if 0
f676886a 6336 selected_frame = frame;
e0c1aef2
KH
6337 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6338 selected_frame);
f676886a
JB
6339 Fselect_window (selected_frame->selected_window);
6340 choose_minibuf_frame ();
c118dd06 6341#endif /* ! 0 */
dc6f92b8 6342
0f941935
KH
6343 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6344 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6345 else
6346 pending_autoraise_frame = 0;
6d4238f3 6347 }
dc6f92b8 6348
0f941935 6349 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6350}
6351
cab34f50
JD
6352/* Handle FocusIn and FocusOut state changes for FRAME.
6353 If FRAME has focus and there exists more than one frame, puts
6354 an FOCUS_IN_EVENT into BUFP.
6355 Returns number of events inserted into BUFP. */
6356
6357static int
6358x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
6359 int type;
6360 int state;
6361 struct x_display_info *dpyinfo;
6362 struct frame *frame;
6363 struct input_event *bufp;
6364 int numchars;
6365{
6366 int nr_events = 0;
6367
6368 if (type == FocusIn)
6369 {
6370 if (dpyinfo->x_focus_event_frame != frame)
6371 {
6372 x_new_focus_frame (dpyinfo, frame);
6373 dpyinfo->x_focus_event_frame = frame;
6374
6375 /* Don't stop displaying the initial startup message
6376 for a switch-frame event we don't need. */
6377 if (numchars > 0
6378 && GC_NILP (Vterminal_frame)
6379 && GC_CONSP (Vframe_list)
6380 && !GC_NILP (XCDR (Vframe_list)))
6381 {
6382 bufp->kind = FOCUS_IN_EVENT;
6383 XSETFRAME (bufp->frame_or_window, frame);
6384 bufp->arg = Qnil;
6385 ++bufp;
6386 numchars--;
6387 ++nr_events;
6388 }
6389 }
6390
6391 frame->output_data.x->focus_state |= state;
6392
6393#ifdef HAVE_X_I18N
6394 if (FRAME_XIC (frame))
6395 XSetICFocus (FRAME_XIC (frame));
6396#endif
6397 }
6398 else if (type == FocusOut)
6399 {
6400 frame->output_data.x->focus_state &= ~state;
6401
6402 if (dpyinfo->x_focus_event_frame == frame)
6403 {
6404 dpyinfo->x_focus_event_frame = 0;
6405 x_new_focus_frame (dpyinfo, 0);
6406 }
6407
6408#ifdef HAVE_X_I18N
6409 if (FRAME_XIC (frame))
6410 XUnsetICFocus (FRAME_XIC (frame));
6411#endif
6412 }
6413
6414 return nr_events;
6415}
6416
6417/* The focus may have changed. Figure out if it is a real focus change,
6418 by checking both FocusIn/Out and Enter/LeaveNotify events.
6419
6420 Returns number of events inserted into BUFP. */
6421
6422static int
6423x_detect_focus_change (dpyinfo, event, bufp, numchars)
6424 struct x_display_info *dpyinfo;
6425 XEvent *event;
6426 struct input_event *bufp;
6427 int numchars;
6428{
6429 struct frame *frame;
6430 int nr_events = 0;
6431
6432 frame = x_top_window_to_frame (dpyinfo, event->xany.window);
6433 if (! frame) return nr_events;
6434
6435 switch (event->type)
6436 {
6437 case EnterNotify:
6438 case LeaveNotify:
6439 if (event->xcrossing.detail != NotifyInferior
6440 && event->xcrossing.focus
6441 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
6442 nr_events = x_focus_changed ((event->type == EnterNotify
6443 ? FocusIn : FocusOut),
6444 FOCUS_IMPLICIT,
6445 dpyinfo,
6446 frame,
6447 bufp,
6448 numchars);
6449 break;
6450
6451 case FocusIn:
6452 case FocusOut:
6453 nr_events = x_focus_changed (event->type,
6454 (event->xfocus.detail == NotifyPointer
6455 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
6456 dpyinfo,
6457 frame,
6458 bufp,
6459 numchars);
6460 break;
6461 }
6462
6463 return nr_events;
6464}
6465
6466
37c2c98b
RS
6467/* Handle an event saying the mouse has moved out of an Emacs frame. */
6468
6469void
0f941935
KH
6470x_mouse_leave (dpyinfo)
6471 struct x_display_info *dpyinfo;
37c2c98b 6472{
0f941935 6473 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6474}
6d4238f3 6475
f451eb13
JB
6476/* The focus has changed, or we have redirected a frame's focus to
6477 another frame (this happens when a frame uses a surrogate
06a2c219 6478 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6479
6480 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6481 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6482 the appropriate X display info. */
06a2c219 6483
6d4238f3 6484static void
0f941935
KH
6485XTframe_rehighlight (frame)
6486 struct frame *frame;
6d4238f3 6487{
0f941935
KH
6488 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6489}
6d4238f3 6490
0f941935
KH
6491static void
6492x_frame_rehighlight (dpyinfo)
6493 struct x_display_info *dpyinfo;
6494{
6495 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6496
6497 if (dpyinfo->x_focus_frame)
6d4238f3 6498 {
0f941935
KH
6499 dpyinfo->x_highlight_frame
6500 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6501 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6502 : dpyinfo->x_focus_frame);
6503 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6504 {
0f941935
KH
6505 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6506 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6507 }
dc6f92b8 6508 }
6d4238f3 6509 else
0f941935 6510 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6511
0f941935 6512 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6513 {
6514 if (old_highlight)
f676886a 6515 frame_unhighlight (old_highlight);
0f941935
KH
6516 if (dpyinfo->x_highlight_frame)
6517 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6518 }
dc6f92b8 6519}
06a2c219
GM
6520
6521
dc6f92b8 6522\f
06a2c219 6523/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6524
28430d3c
JB
6525/* Initialize mode_switch_bit and modifier_meaning. */
6526static void
334208b7
RS
6527x_find_modifier_meanings (dpyinfo)
6528 struct x_display_info *dpyinfo;
28430d3c 6529{
f689eb05 6530 int min_code, max_code;
28430d3c
JB
6531 KeySym *syms;
6532 int syms_per_code;
6533 XModifierKeymap *mods;
6534
334208b7
RS
6535 dpyinfo->meta_mod_mask = 0;
6536 dpyinfo->shift_lock_mask = 0;
6537 dpyinfo->alt_mod_mask = 0;
6538 dpyinfo->super_mod_mask = 0;
6539 dpyinfo->hyper_mod_mask = 0;
58769bee 6540
9658a521 6541#ifdef HAVE_X11R4
334208b7 6542 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6543#else
4a60f8c5
RS
6544 min_code = dpyinfo->display->min_keycode;
6545 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6546#endif
6547
334208b7 6548 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6549 min_code, max_code - min_code + 1,
6550 &syms_per_code);
334208b7 6551 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6552
58769bee 6553 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6554 Alt keysyms are on. */
28430d3c 6555 {
06a2c219 6556 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6557
6558 for (row = 3; row < 8; row++)
6559 for (col = 0; col < mods->max_keypermod; col++)
6560 {
0299d313
RS
6561 KeyCode code
6562 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6563
af92970c
KH
6564 /* Zeroes are used for filler. Skip them. */
6565 if (code == 0)
6566 continue;
6567
28430d3c
JB
6568 /* Are any of this keycode's keysyms a meta key? */
6569 {
6570 int code_col;
6571
6572 for (code_col = 0; code_col < syms_per_code; code_col++)
6573 {
f689eb05 6574 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6575
f689eb05 6576 switch (sym)
28430d3c 6577 {
f689eb05
JB
6578 case XK_Meta_L:
6579 case XK_Meta_R:
334208b7 6580 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6581 break;
f689eb05
JB
6582
6583 case XK_Alt_L:
6584 case XK_Alt_R:
334208b7 6585 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6586 break;
6587
6588 case XK_Hyper_L:
6589 case XK_Hyper_R:
334208b7 6590 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6591 break;
6592
6593 case XK_Super_L:
6594 case XK_Super_R:
334208b7 6595 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6596 break;
11edeb03
JB
6597
6598 case XK_Shift_Lock:
6599 /* Ignore this if it's not on the lock modifier. */
6600 if ((1 << row) == LockMask)
334208b7 6601 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6602 break;
28430d3c
JB
6603 }
6604 }
6605 }
6606 }
6607 }
6608
f689eb05 6609 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6610 if (! dpyinfo->meta_mod_mask)
a3c44b14 6611 {
334208b7
RS
6612 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6613 dpyinfo->alt_mod_mask = 0;
a3c44b14 6614 }
f689eb05 6615
148c4b70
RS
6616 /* If some keys are both alt and meta,
6617 make them just meta, not alt. */
334208b7 6618 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6619 {
334208b7 6620 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6621 }
58769bee 6622
28430d3c 6623 XFree ((char *) syms);
f689eb05 6624 XFreeModifiermap (mods);
28430d3c
JB
6625}
6626
dfeccd2d
JB
6627/* Convert between the modifier bits X uses and the modifier bits
6628 Emacs uses. */
06a2c219 6629
7c5283e4 6630static unsigned int
334208b7
RS
6631x_x_to_emacs_modifiers (dpyinfo, state)
6632 struct x_display_info *dpyinfo;
dc6f92b8
JB
6633 unsigned int state;
6634{
98659da6
KG
6635 EMACS_UINT mod_meta = meta_modifier;
6636 EMACS_UINT mod_alt = alt_modifier;
6637 EMACS_UINT mod_hyper = hyper_modifier;
6638 EMACS_UINT mod_super = super_modifier;
6639 Lisp_Object tem;
6640
6641 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6642 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6643 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6644 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6645 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6646 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6647 tem = Fget (Vx_super_keysym, Qmodifier_value);
6648 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6649
6650
334208b7 6651 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
6652 | ((state & ControlMask) ? ctrl_modifier : 0)
6653 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6654 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6655 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6656 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
6657}
6658
dfeccd2d 6659static unsigned int
334208b7
RS
6660x_emacs_to_x_modifiers (dpyinfo, state)
6661 struct x_display_info *dpyinfo;
dfeccd2d
JB
6662 unsigned int state;
6663{
98659da6
KG
6664 EMACS_UINT mod_meta = meta_modifier;
6665 EMACS_UINT mod_alt = alt_modifier;
6666 EMACS_UINT mod_hyper = hyper_modifier;
6667 EMACS_UINT mod_super = super_modifier;
6668
6669 Lisp_Object tem;
6670
6671 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6672 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6673 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6674 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6675 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6676 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6677 tem = Fget (Vx_super_keysym, Qmodifier_value);
6678 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6679
6680
6681 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6682 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6683 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6684 | ((state & shift_modifier) ? ShiftMask : 0)
6685 | ((state & ctrl_modifier) ? ControlMask : 0)
6686 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6687}
d047c4eb
KH
6688
6689/* Convert a keysym to its name. */
6690
6691char *
6692x_get_keysym_name (keysym)
6693 KeySym keysym;
6694{
6695 char *value;
6696
6697 BLOCK_INPUT;
6698 value = XKeysymToString (keysym);
6699 UNBLOCK_INPUT;
6700
6701 return value;
6702}
06a2c219
GM
6703
6704
e4571a43
JB
6705\f
6706/* Mouse clicks and mouse movement. Rah. */
e4571a43 6707
06a2c219
GM
6708/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6709 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6710 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6711 not force the value into range. */
69388238 6712
c8dba240 6713void
69388238 6714pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6715 FRAME_PTR f;
69388238 6716 register int pix_x, pix_y;
e4571a43
JB
6717 register int *x, *y;
6718 XRectangle *bounds;
69388238 6719 int noclip;
e4571a43 6720{
06a2c219 6721 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6722 even for negative values. */
6723 if (pix_x < 0)
7556890b 6724 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6725 if (pix_y < 0)
7556890b 6726 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6727
e4571a43
JB
6728 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6729 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6730
6731 if (bounds)
6732 {
7556890b
RS
6733 bounds->width = FONT_WIDTH (f->output_data.x->font);
6734 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6735 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6736 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6737 }
6738
69388238
RS
6739 if (!noclip)
6740 {
6741 if (pix_x < 0)
6742 pix_x = 0;
3cbd2e0b
RS
6743 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6744 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6745
6746 if (pix_y < 0)
6747 pix_y = 0;
6748 else if (pix_y > f->height)
6749 pix_y = f->height;
6750 }
e4571a43
JB
6751
6752 *x = pix_x;
6753 *y = pix_y;
6754}
6755
06a2c219
GM
6756
6757/* Given HPOS/VPOS in the current matrix of W, return corresponding
6758 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6759 can't tell the positions because W's display is not up to date,
6760 return 0. */
6761
6762int
6763glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6764 struct window *w;
6765 int hpos, vpos;
6766 int *frame_x, *frame_y;
2b5c9e71 6767{
06a2c219
GM
6768 int success_p;
6769
6770 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6771 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6772
6773 if (display_completed)
6774 {
6775 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6776 struct glyph *glyph = row->glyphs[TEXT_AREA];
6777 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6778
6779 *frame_y = row->y;
6780 *frame_x = row->x;
6781 while (glyph < end)
6782 {
6783 *frame_x += glyph->pixel_width;
6784 ++glyph;
6785 }
6786
6787 success_p = 1;
6788 }
6789 else
6790 {
6791 *frame_y = *frame_x = 0;
6792 success_p = 0;
6793 }
6794
6795 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6796 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6797 return success_p;
2b5c9e71
RS
6798}
6799
06a2c219 6800
dc6f92b8
JB
6801/* Prepare a mouse-event in *RESULT for placement in the input queue.
6802
6803 If the event is a button press, then note that we have grabbed
f451eb13 6804 the mouse. */
dc6f92b8
JB
6805
6806static Lisp_Object
f451eb13 6807construct_mouse_click (result, event, f)
dc6f92b8
JB
6808 struct input_event *result;
6809 XButtonEvent *event;
f676886a 6810 struct frame *f;
dc6f92b8 6811{
3b8f9651 6812 /* Make the event type NO_EVENT; we'll change that when we decide
dc6f92b8 6813 otherwise. */
3b8f9651 6814 result->kind = MOUSE_CLICK_EVENT;
69388238 6815 result->code = event->button - Button1;
1113d9db 6816 result->timestamp = event->time;
334208b7
RS
6817 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6818 event->state)
f689eb05 6819 | (event->type == ButtonRelease
58769bee 6820 ? up_modifier
f689eb05 6821 : down_modifier));
dc6f92b8 6822
06a2c219
GM
6823 XSETINT (result->x, event->x);
6824 XSETINT (result->y, event->y);
6825 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6826 result->arg = Qnil;
06a2c219 6827 return Qnil;
dc6f92b8 6828}
b849c413 6829
69388238 6830\f
90e65f07
JB
6831/* Function to report a mouse movement to the mainstream Emacs code.
6832 The input handler calls this.
6833
6834 We have received a mouse movement event, which is given in *event.
6835 If the mouse is over a different glyph than it was last time, tell
6836 the mainstream emacs code by setting mouse_moved. If not, ask for
6837 another motion event, so we can check again the next time it moves. */
b8009dd1 6838
06a2c219
GM
6839static XMotionEvent last_mouse_motion_event;
6840static Lisp_Object last_mouse_motion_frame;
6841
90e65f07 6842static void
12ba150f 6843note_mouse_movement (frame, event)
f676886a 6844 FRAME_PTR frame;
90e65f07 6845 XMotionEvent *event;
90e65f07 6846{
e5d77022 6847 last_mouse_movement_time = event->time;
06a2c219
GM
6848 last_mouse_motion_event = *event;
6849 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6850
27f338af
RS
6851 if (event->window != FRAME_X_WINDOW (frame))
6852 {
39d8bb4d 6853 frame->mouse_moved = 1;
27f338af 6854 last_mouse_scroll_bar = Qnil;
27f338af 6855 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6856 }
6857
90e65f07 6858 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6859 else if (event->x < last_mouse_glyph.x
6860 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6861 || event->y < last_mouse_glyph.y
6862 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6863 {
39d8bb4d 6864 frame->mouse_moved = 1;
ab648270 6865 last_mouse_scroll_bar = Qnil;
b8009dd1 6866 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6867 }
6868}
6869
06a2c219
GM
6870\f
6871/************************************************************************
6872 Mouse Face
6873 ************************************************************************/
6874
6875/* Find the glyph under window-relative coordinates X/Y in window W.
6876 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6877 strings. Return in *HPOS and *VPOS the row and column number of
6878 the glyph found. Return in *AREA the glyph area containing X.
6879 Value is a pointer to the glyph found or null if X/Y is not on
6880 text, or we can't tell because W's current matrix is not up to
6881 date. */
6882
6883static struct glyph *
f9db2310 6884x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
6885 struct window *w;
6886 int x, y;
6887 int *hpos, *vpos, *area;
f9db2310 6888 int buffer_only_p;
06a2c219
GM
6889{
6890 struct glyph *glyph, *end;
3e71d8f2 6891 struct glyph_row *row = NULL;
06a2c219
GM
6892 int x0, i, left_area_width;
6893
6894 /* Find row containing Y. Give up if some row is not enabled. */
6895 for (i = 0; i < w->current_matrix->nrows; ++i)
6896 {
6897 row = MATRIX_ROW (w->current_matrix, i);
6898 if (!row->enabled_p)
6899 return NULL;
6900 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6901 break;
6902 }
6903
6904 *vpos = i;
6905 *hpos = 0;
6906
6907 /* Give up if Y is not in the window. */
6908 if (i == w->current_matrix->nrows)
6909 return NULL;
6910
6911 /* Get the glyph area containing X. */
6912 if (w->pseudo_window_p)
6913 {
6914 *area = TEXT_AREA;
6915 x0 = 0;
6916 }
6917 else
6918 {
6919 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6920 if (x < left_area_width)
6921 {
6922 *area = LEFT_MARGIN_AREA;
6923 x0 = 0;
6924 }
6925 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6926 {
6927 *area = TEXT_AREA;
6928 x0 = row->x + left_area_width;
6929 }
6930 else
6931 {
6932 *area = RIGHT_MARGIN_AREA;
6933 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6934 }
6935 }
6936
6937 /* Find glyph containing X. */
6938 glyph = row->glyphs[*area];
6939 end = glyph + row->used[*area];
6940 while (glyph < end)
6941 {
6942 if (x < x0 + glyph->pixel_width)
6943 {
6944 if (w->pseudo_window_p)
6945 break;
f9db2310 6946 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
6947 break;
6948 }
6949
6950 x0 += glyph->pixel_width;
6951 ++glyph;
6952 }
6953
6954 if (glyph == end)
6955 return NULL;
6956
6957 *hpos = glyph - row->glyphs[*area];
6958 return glyph;
6959}
6960
6961
6962/* Convert frame-relative x/y to coordinates relative to window W.
6963 Takes pseudo-windows into account. */
6964
6965static void
6966frame_to_window_pixel_xy (w, x, y)
6967 struct window *w;
6968 int *x, *y;
6969{
6970 if (w->pseudo_window_p)
6971 {
6972 /* A pseudo-window is always full-width, and starts at the
6973 left edge of the frame, plus a frame border. */
6974 struct frame *f = XFRAME (w->frame);
6975 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6976 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6977 }
6978 else
6979 {
6980 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6981 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6982 }
6983}
6984
6985
b436feb6
GM
6986/* Take proper action when mouse has moved to the mode or header line
6987 or marginal area of window W, x-position X and y-position Y. Area
6988 is 1, 3, 6 or 7 for the mode line, header line, left and right
6989 marginal area respectively. X is relative to the start of the text
6990 display area of W, so the width of bitmap areas and scroll bars
6991 must be subtracted to get a position relative to the start of the
6992 mode line. */
6993
06a2c219 6994static void
b436feb6 6995note_mode_line_or_margin_highlight (w, x, y, portion)
06a2c219 6996 struct window *w;
b436feb6 6997 int x, y, portion;
06a2c219
GM
6998{
6999 struct frame *f = XFRAME (w->frame);
7000 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7001 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
2a793c0c 7002 int charpos;
b436feb6 7003 Lisp_Object string, help, map, pos;
06a2c219 7004
b436feb6
GM
7005 if (portion == 1 || portion == 3)
7006 string = mode_line_string (w, x, y, portion == 1, &charpos);
7007 else
7008 string = marginal_area_string (w, x, y, portion, &charpos);
e371a781 7009
b436feb6 7010 if (STRINGP (string))
06a2c219 7011 {
b436feb6 7012 pos = make_number (charpos);
e371a781 7013
b436feb6
GM
7014 /* If we're on a string with `help-echo' text property, arrange
7015 for the help to be displayed. This is done by setting the
7016 global variable help_echo to the help string. */
7017 help = Fget_text_property (pos, Qhelp_echo, string);
7018 if (!NILP (help))
06a2c219 7019 {
b436feb6
GM
7020 help_echo = help;
7021 XSETWINDOW (help_echo_window, w);
7022 help_echo_object = string;
7023 help_echo_pos = charpos;
06a2c219
GM
7024 }
7025
b436feb6
GM
7026 /* Change the mouse pointer according to what is under X/Y. */
7027 map = Fget_text_property (pos, Qlocal_map, string);
7028 if (!KEYMAPP (map))
7029 map = Fget_text_property (pos, Qkeymap, string);
7030 if (KEYMAPP (map))
7031 cursor = f->output_data.x->nontext_cursor;
06a2c219 7032 }
b436feb6 7033
06a2c219
GM
7034 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7035}
7036
7037
7038/* Take proper action when the mouse has moved to position X, Y on
7039 frame F as regards highlighting characters that have mouse-face
7040 properties. Also de-highlighting chars where the mouse was before.
27f338af 7041 X and Y can be negative or out of range. */
b8009dd1
RS
7042
7043static void
7044note_mouse_highlight (f, x, y)
06a2c219 7045 struct frame *f;
c32cdd9a 7046 int x, y;
b8009dd1 7047{
06a2c219
GM
7048 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7049 int portion;
b8009dd1
RS
7050 Lisp_Object window;
7051 struct window *w;
0d487c52
GM
7052 Cursor cursor = None;
7053 struct buffer *b;
b8009dd1 7054
06a2c219
GM
7055 /* When a menu is active, don't highlight because this looks odd. */
7056#ifdef USE_X_TOOLKIT
7057 if (popup_activated ())
7058 return;
7059#endif
7060
66301061 7061 if (NILP (Vmouse_highlight)
04fff9c0 7062 || !f->glyphs_initialized_p)
bf1c0ba1
RS
7063 return;
7064
06a2c219
GM
7065 dpyinfo->mouse_face_mouse_x = x;
7066 dpyinfo->mouse_face_mouse_y = y;
7067 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 7068
06a2c219 7069 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
7070 return;
7071
514e4681
RS
7072 if (gc_in_progress)
7073 {
06a2c219 7074 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
7075 return;
7076 }
7077
b8009dd1 7078 /* Which window is that in? */
06a2c219 7079 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
7080
7081 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
7082 if (! EQ (window, dpyinfo->mouse_face_window))
7083 clear_mouse_face (dpyinfo);
7084
7085 /* Not on a window -> return. */
7086 if (!WINDOWP (window))
7087 return;
7088
7089 /* Convert to window-relative pixel coordinates. */
7090 w = XWINDOW (window);
7091 frame_to_window_pixel_xy (w, &x, &y);
7092
9ea173e8 7093 /* Handle tool-bar window differently since it doesn't display a
06a2c219 7094 buffer. */
9ea173e8 7095 if (EQ (window, f->tool_bar_window))
06a2c219 7096 {
9ea173e8 7097 note_tool_bar_highlight (f, x, y);
06a2c219
GM
7098 return;
7099 }
7100
b436feb6
GM
7101 /* Mouse is on the mode, header line or margin? */
7102 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
06a2c219 7103 {
b436feb6 7104 note_mode_line_or_margin_highlight (w, x, y, portion);
06a2c219
GM
7105 return;
7106 }
0d487c52
GM
7107
7108 if (portion == 2)
7109 cursor = f->output_data.x->horizontal_drag_cursor;
06a2c219 7110 else
0d487c52 7111 cursor = f->output_data.x->text_cursor;
b8009dd1 7112
0cdd0c9f
RS
7113 /* Are we in a window whose display is up to date?
7114 And verify the buffer's text has not changed. */
0d487c52 7115 b = XBUFFER (w->buffer);
06a2c219
GM
7116 if (/* Within text portion of the window. */
7117 portion == 0
0cdd0c9f 7118 && EQ (w->window_end_valid, w->buffer)
0d487c52
GM
7119 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
7120 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
b8009dd1 7121 {
06a2c219
GM
7122 int hpos, vpos, pos, i, area;
7123 struct glyph *glyph;
f9db2310 7124 Lisp_Object object;
0d487c52
GM
7125 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
7126 Lisp_Object *overlay_vec = NULL;
7127 int len, noverlays;
7128 struct buffer *obuf;
7129 int obegv, ozv, same_region;
b8009dd1 7130
06a2c219 7131 /* Find the glyph under X/Y. */
f9db2310 7132 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
7133
7134 /* Clear mouse face if X/Y not over text. */
7135 if (glyph == NULL
7136 || area != TEXT_AREA
7137 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 7138 {
fa262c07
GM
7139 if (clear_mouse_face (dpyinfo))
7140 cursor = None;
7141 goto set_cursor;
06a2c219
GM
7142 }
7143
7144 pos = glyph->charpos;
f9db2310
GM
7145 object = glyph->object;
7146 if (!STRINGP (object) && !BUFFERP (object))
fa262c07 7147 goto set_cursor;
06a2c219 7148
0d487c52
GM
7149 /* If we get an out-of-range value, return now; avoid an error. */
7150 if (BUFFERP (object) && pos > BUF_Z (b))
fa262c07 7151 goto set_cursor;
06a2c219 7152
0d487c52
GM
7153 /* Make the window's buffer temporarily current for
7154 overlays_at and compute_char_face. */
7155 obuf = current_buffer;
7156 current_buffer = b;
7157 obegv = BEGV;
7158 ozv = ZV;
7159 BEGV = BEG;
7160 ZV = Z;
06a2c219 7161
0d487c52
GM
7162 /* Is this char mouse-active or does it have help-echo? */
7163 position = make_number (pos);
f9db2310 7164
0d487c52
GM
7165 if (BUFFERP (object))
7166 {
7167 /* Put all the overlays we want in a vector in overlay_vec.
7168 Store the length in len. If there are more than 10, make
7169 enough space for all, and try again. */
7170 len = 10;
7171 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7172 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7173 if (noverlays > len)
7174 {
7175 len = noverlays;
7176 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7177 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7178 }
f8349001 7179
0d487c52
GM
7180 /* Sort overlays into increasing priority order. */
7181 noverlays = sort_overlays (overlay_vec, noverlays, w);
7182 }
7183 else
7184 noverlays = 0;
7185
7186 same_region = (EQ (window, dpyinfo->mouse_face_window)
7187 && vpos >= dpyinfo->mouse_face_beg_row
7188 && vpos <= dpyinfo->mouse_face_end_row
7189 && (vpos > dpyinfo->mouse_face_beg_row
7190 || hpos >= dpyinfo->mouse_face_beg_col)
7191 && (vpos < dpyinfo->mouse_face_end_row
7192 || hpos < dpyinfo->mouse_face_end_col
7193 || dpyinfo->mouse_face_past_end));
7194
7195 if (same_region)
7196 cursor = None;
7197
7198 /* Check mouse-face highlighting. */
7199 if (! same_region
7200 /* If there exists an overlay with mouse-face overlapping
7201 the one we are currently highlighting, we have to
7202 check if we enter the overlapping overlay, and then
7203 highlight only that. */
7204 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7205 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7206 {
0d487c52
GM
7207 /* Find the highest priority overlay that has a mouse-face
7208 property. */
7209 overlay = Qnil;
7210 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7211 {
7212 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7213 if (!NILP (mouse_face))
7214 overlay = overlay_vec[i];
7215 }
8bd189fb
GM
7216
7217 /* If we're actually highlighting the same overlay as
7218 before, there's no need to do that again. */
7219 if (!NILP (overlay)
7220 && EQ (overlay, dpyinfo->mouse_face_overlay))
7221 goto check_help_echo;
f9db2310 7222
8bd189fb
GM
7223 dpyinfo->mouse_face_overlay = overlay;
7224
7225 /* Clear the display of the old active region, if any. */
7226 if (clear_mouse_face (dpyinfo))
7227 cursor = None;
7228
0d487c52
GM
7229 /* If no overlay applies, get a text property. */
7230 if (NILP (overlay))
7231 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219 7232
0d487c52
GM
7233 /* Handle the overlay case. */
7234 if (!NILP (overlay))
7235 {
7236 /* Find the range of text around this char that
7237 should be active. */
7238 Lisp_Object before, after;
7239 int ignore;
7240
7241 before = Foverlay_start (overlay);
7242 after = Foverlay_end (overlay);
7243 /* Record this as the current active region. */
7244 fast_find_position (w, XFASTINT (before),
7245 &dpyinfo->mouse_face_beg_col,
7246 &dpyinfo->mouse_face_beg_row,
7247 &dpyinfo->mouse_face_beg_x,
7e376260
GM
7248 &dpyinfo->mouse_face_beg_y, Qnil);
7249
0d487c52
GM
7250 dpyinfo->mouse_face_past_end
7251 = !fast_find_position (w, XFASTINT (after),
7252 &dpyinfo->mouse_face_end_col,
7253 &dpyinfo->mouse_face_end_row,
7254 &dpyinfo->mouse_face_end_x,
7e376260 7255 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7256 dpyinfo->mouse_face_window = window;
7257 dpyinfo->mouse_face_face_id
7258 = face_at_buffer_position (w, pos, 0, 0,
7259 &ignore, pos + 1, 1);
7260
7261 /* Display it as active. */
7262 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7263 cursor = None;
0d487c52
GM
7264 }
7265 /* Handle the text property case. */
7266 else if (!NILP (mouse_face) && BUFFERP (object))
7267 {
7268 /* Find the range of text around this char that
7269 should be active. */
7270 Lisp_Object before, after, beginning, end;
7271 int ignore;
7272
7273 beginning = Fmarker_position (w->start);
7274 end = make_number (BUF_Z (XBUFFER (object))
7275 - XFASTINT (w->window_end_pos));
7276 before
7277 = Fprevious_single_property_change (make_number (pos + 1),
7278 Qmouse_face,
7279 object, beginning);
7280 after
7281 = Fnext_single_property_change (position, Qmouse_face,
7282 object, end);
7283
7284 /* Record this as the current active region. */
7285 fast_find_position (w, XFASTINT (before),
7286 &dpyinfo->mouse_face_beg_col,
7287 &dpyinfo->mouse_face_beg_row,
7288 &dpyinfo->mouse_face_beg_x,
7e376260 7289 &dpyinfo->mouse_face_beg_y, Qnil);
0d487c52
GM
7290 dpyinfo->mouse_face_past_end
7291 = !fast_find_position (w, XFASTINT (after),
7292 &dpyinfo->mouse_face_end_col,
7293 &dpyinfo->mouse_face_end_row,
7294 &dpyinfo->mouse_face_end_x,
7e376260 7295 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
7296 dpyinfo->mouse_face_window = window;
7297
7298 if (BUFFERP (object))
06a2c219
GM
7299 dpyinfo->mouse_face_face_id
7300 = face_at_buffer_position (w, pos, 0, 0,
7301 &ignore, pos + 1, 1);
7302
0d487c52
GM
7303 /* Display it as active. */
7304 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7305 cursor = None;
0d487c52
GM
7306 }
7307 else if (!NILP (mouse_face) && STRINGP (object))
7308 {
7309 Lisp_Object b, e;
7310 int ignore;
f9db2310 7311
0d487c52
GM
7312 b = Fprevious_single_property_change (make_number (pos + 1),
7313 Qmouse_face,
7314 object, Qnil);
7315 e = Fnext_single_property_change (position, Qmouse_face,
7316 object, Qnil);
7317 if (NILP (b))
7318 b = make_number (0);
7319 if (NILP (e))
d5db4077 7320 e = make_number (SCHARS (object) - 1);
0d487c52 7321 fast_find_string_pos (w, XINT (b), object,
06a2c219
GM
7322 &dpyinfo->mouse_face_beg_col,
7323 &dpyinfo->mouse_face_beg_row,
7324 &dpyinfo->mouse_face_beg_x,
0d487c52
GM
7325 &dpyinfo->mouse_face_beg_y, 0);
7326 fast_find_string_pos (w, XINT (e), object,
7327 &dpyinfo->mouse_face_end_col,
7328 &dpyinfo->mouse_face_end_row,
7329 &dpyinfo->mouse_face_end_x,
7330 &dpyinfo->mouse_face_end_y, 1);
7331 dpyinfo->mouse_face_past_end = 0;
7332 dpyinfo->mouse_face_window = window;
7333 dpyinfo->mouse_face_face_id
7334 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7335 glyph->face_id, 1);
7336 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 7337 cursor = None;
0d487c52 7338 }
7e376260
GM
7339 else if (STRINGP (object) && NILP (mouse_face))
7340 {
7341 /* A string which doesn't have mouse-face, but
7342 the text ``under'' it might have. */
7343 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7344 int start = MATRIX_ROW_START_CHARPOS (r);
7345
7346 pos = string_buffer_position (w, object, start);
7347 if (pos > 0)
7348 mouse_face = get_char_property_and_overlay (make_number (pos),
7349 Qmouse_face,
7350 w->buffer,
7351 &overlay);
7352 if (!NILP (mouse_face) && !NILP (overlay))
7353 {
7354 Lisp_Object before = Foverlay_start (overlay);
7355 Lisp_Object after = Foverlay_end (overlay);
91c153e2 7356 int ignore;
7e376260
GM
7357
7358 /* Note that we might not be able to find position
7359 BEFORE in the glyph matrix if the overlay is
7360 entirely covered by a `display' property. In
7361 this case, we overshoot. So let's stop in
7362 the glyph matrix before glyphs for OBJECT. */
7363 fast_find_position (w, XFASTINT (before),
7364 &dpyinfo->mouse_face_beg_col,
7365 &dpyinfo->mouse_face_beg_row,
7366 &dpyinfo->mouse_face_beg_x,
7367 &dpyinfo->mouse_face_beg_y,
7368 object);
7369
7370 dpyinfo->mouse_face_past_end
7371 = !fast_find_position (w, XFASTINT (after),
7372 &dpyinfo->mouse_face_end_col,
7373 &dpyinfo->mouse_face_end_row,
7374 &dpyinfo->mouse_face_end_x,
7375 &dpyinfo->mouse_face_end_y,
7376 Qnil);
7377 dpyinfo->mouse_face_window = window;
7378 dpyinfo->mouse_face_face_id
7379 = face_at_buffer_position (w, pos, 0, 0,
7380 &ignore, pos + 1, 1);
7381
7382 /* Display it as active. */
7383 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7384 cursor = None;
7385 }
7386 }
0d487c52 7387 }
06a2c219 7388
8bd189fb
GM
7389 check_help_echo:
7390
0d487c52
GM
7391 /* Look for a `help-echo' property. */
7392 {
7393 Lisp_Object help, overlay;
06a2c219 7394
0d487c52
GM
7395 /* Check overlays first. */
7396 help = overlay = Qnil;
7397 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7398 {
7399 overlay = overlay_vec[i];
7400 help = Foverlay_get (overlay, Qhelp_echo);
7401 }
be010514 7402
0d487c52
GM
7403 if (!NILP (help))
7404 {
7405 help_echo = help;
7406 help_echo_window = window;
7407 help_echo_object = overlay;
7408 help_echo_pos = pos;
7409 }
7410 else
7411 {
7412 Lisp_Object object = glyph->object;
7413 int charpos = glyph->charpos;
7177d86b 7414
0d487c52
GM
7415 /* Try text properties. */
7416 if (STRINGP (object)
7417 && charpos >= 0
d5db4077 7418 && charpos < SCHARS (object))
0d487c52
GM
7419 {
7420 help = Fget_text_property (make_number (charpos),
7421 Qhelp_echo, object);
7422 if (NILP (help))
7423 {
7424 /* If the string itself doesn't specify a help-echo,
7425 see if the buffer text ``under'' it does. */
7426 struct glyph_row *r
7427 = MATRIX_ROW (w->current_matrix, vpos);
7428 int start = MATRIX_ROW_START_CHARPOS (r);
7429 int pos = string_buffer_position (w, object, start);
7430 if (pos > 0)
7431 {
7e376260 7432 help = Fget_char_property (make_number (pos),
0d487c52
GM
7433 Qhelp_echo, w->buffer);
7434 if (!NILP (help))
7435 {
7436 charpos = pos;
7437 object = w->buffer;
7438 }
7439 }
7440 }
7441 }
7442 else if (BUFFERP (object)
7443 && charpos >= BEGV
7444 && charpos < ZV)
7445 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7446 object);
06a2c219 7447
0d487c52
GM
7448 if (!NILP (help))
7449 {
7450 help_echo = help;
7451 help_echo_window = window;
7452 help_echo_object = object;
7453 help_echo_pos = charpos;
7454 }
7455 }
06a2c219 7456 }
0d487c52
GM
7457
7458 BEGV = obegv;
7459 ZV = ozv;
7460 current_buffer = obuf;
06a2c219 7461 }
0d487c52 7462
fa262c07
GM
7463 set_cursor:
7464
0d487c52
GM
7465 if (cursor != None)
7466 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219
GM
7467}
7468
7469static void
7470redo_mouse_highlight ()
7471{
7472 if (!NILP (last_mouse_motion_frame)
7473 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7474 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7475 last_mouse_motion_event.x,
7476 last_mouse_motion_event.y);
7477}
7478
7479
7480\f
7481/***********************************************************************
9ea173e8 7482 Tool-bars
06a2c219
GM
7483 ***********************************************************************/
7484
9ea173e8
GM
7485static int x_tool_bar_item P_ ((struct frame *, int, int,
7486 struct glyph **, int *, int *, int *));
06a2c219 7487
9ea173e8 7488/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
7489 or -1. */
7490
9ea173e8 7491static int last_tool_bar_item;
06a2c219
GM
7492
7493
9ea173e8
GM
7494/* Get information about the tool-bar item at position X/Y on frame F.
7495 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7496 the current matrix of the tool-bar window of F, or NULL if not
7497 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 7498 item in F->tool_bar_items. Value is
06a2c219 7499
9ea173e8 7500 -1 if X/Y is not on a tool-bar item
06a2c219
GM
7501 0 if X/Y is on the same item that was highlighted before.
7502 1 otherwise. */
7503
7504static int
9ea173e8 7505x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
7506 struct frame *f;
7507 int x, y;
7508 struct glyph **glyph;
7509 int *hpos, *vpos, *prop_idx;
7510{
7511 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7512 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7513 int area;
7514
7515 /* Find the glyph under X/Y. */
f9db2310 7516 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
7517 if (*glyph == NULL)
7518 return -1;
7519
9ea173e8 7520 /* Get the start of this tool-bar item's properties in
8daf1204 7521 f->tool_bar_items. */
9ea173e8 7522 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
7523 return -1;
7524
7525 /* Is mouse on the highlighted item? */
9ea173e8 7526 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
7527 && *vpos >= dpyinfo->mouse_face_beg_row
7528 && *vpos <= dpyinfo->mouse_face_end_row
7529 && (*vpos > dpyinfo->mouse_face_beg_row
7530 || *hpos >= dpyinfo->mouse_face_beg_col)
7531 && (*vpos < dpyinfo->mouse_face_end_row
7532 || *hpos < dpyinfo->mouse_face_end_col
7533 || dpyinfo->mouse_face_past_end))
7534 return 0;
7535
7536 return 1;
7537}
7538
7539
9ea173e8 7540/* Handle mouse button event on the tool-bar of frame F, at
6c4a22e6 7541 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
06a2c219
GM
7542 or ButtonRelase. */
7543
7544static void
9ea173e8 7545x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7546 struct frame *f;
7547 XButtonEvent *button_event;
7548{
7549 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7550 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7551 int hpos, vpos, prop_idx;
7552 struct glyph *glyph;
7553 Lisp_Object enabled_p;
7554 int x = button_event->x;
7555 int y = button_event->y;
7556
9ea173e8 7557 /* If not on the highlighted tool-bar item, return. */
06a2c219 7558 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7559 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7560 return;
7561
7562 /* If item is disabled, do nothing. */
8daf1204 7563 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7564 if (NILP (enabled_p))
7565 return;
7566
7567 if (button_event->type == ButtonPress)
7568 {
7569 /* Show item in pressed state. */
7570 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7571 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7572 last_tool_bar_item = prop_idx;
06a2c219
GM
7573 }
7574 else
7575 {
7576 Lisp_Object key, frame;
7577 struct input_event event;
7578
7579 /* Show item in released state. */
7580 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7581 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7582
8daf1204 7583 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7584
7585 XSETFRAME (frame, f);
9ea173e8 7586 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7587 event.frame_or_window = frame;
7588 event.arg = frame;
06a2c219
GM
7589 kbd_buffer_store_event (&event);
7590
9ea173e8 7591 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7592 event.frame_or_window = frame;
7593 event.arg = key;
06a2c219
GM
7594 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7595 button_event->state);
7596 kbd_buffer_store_event (&event);
9ea173e8 7597 last_tool_bar_item = -1;
06a2c219
GM
7598 }
7599}
7600
7601
9ea173e8
GM
7602/* Possibly highlight a tool-bar item on frame F when mouse moves to
7603 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7604 note_mouse_highlight. */
7605
7606static void
9ea173e8 7607note_tool_bar_highlight (f, x, y)
06a2c219
GM
7608 struct frame *f;
7609 int x, y;
7610{
9ea173e8 7611 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7612 struct window *w = XWINDOW (window);
7613 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7614 int hpos, vpos;
7615 struct glyph *glyph;
7616 struct glyph_row *row;
5c187dee 7617 int i;
06a2c219
GM
7618 Lisp_Object enabled_p;
7619 int prop_idx;
140330de 7620 enum draw_glyphs_face draw;
5c187dee 7621 int mouse_down_p, rc;
06a2c219
GM
7622
7623 /* Function note_mouse_highlight is called with negative x(y
7624 values when mouse moves outside of the frame. */
7625 if (x <= 0 || y <= 0)
7626 {
7627 clear_mouse_face (dpyinfo);
7628 return;
7629 }
7630
9ea173e8 7631 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7632 if (rc < 0)
7633 {
9ea173e8 7634 /* Not on tool-bar item. */
06a2c219
GM
7635 clear_mouse_face (dpyinfo);
7636 return;
7637 }
7638 else if (rc == 0)
06a2c219 7639 goto set_help_echo;
b8009dd1 7640
06a2c219
GM
7641 clear_mouse_face (dpyinfo);
7642
9ea173e8 7643 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7644 mouse_down_p = (dpyinfo->grabbed
7645 && f == last_mouse_frame
7646 && FRAME_LIVE_P (f));
7647 if (mouse_down_p
9ea173e8 7648 && last_tool_bar_item != prop_idx)
06a2c219
GM
7649 return;
7650
7651 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7652 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7653
9ea173e8 7654 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7655 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7656 if (!NILP (enabled_p))
7657 {
7658 /* Compute the x-position of the glyph. In front and past the
7659 image is a space. We include this is the highlighted area. */
7660 row = MATRIX_ROW (w->current_matrix, vpos);
7661 for (i = x = 0; i < hpos; ++i)
7662 x += row->glyphs[TEXT_AREA][i].pixel_width;
7663
7664 /* Record this as the current active region. */
7665 dpyinfo->mouse_face_beg_col = hpos;
7666 dpyinfo->mouse_face_beg_row = vpos;
7667 dpyinfo->mouse_face_beg_x = x;
7668 dpyinfo->mouse_face_beg_y = row->y;
7669 dpyinfo->mouse_face_past_end = 0;
7670
7671 dpyinfo->mouse_face_end_col = hpos + 1;
7672 dpyinfo->mouse_face_end_row = vpos;
7673 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7674 dpyinfo->mouse_face_end_y = row->y;
7675 dpyinfo->mouse_face_window = window;
9ea173e8 7676 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7677
7678 /* Display it as active. */
7679 show_mouse_face (dpyinfo, draw);
7680 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7681 }
06a2c219
GM
7682
7683 set_help_echo:
7684
50590060 7685 /* Set help_echo to a help string to display for this tool-bar item.
06a2c219 7686 XTread_socket does the rest. */
7cea38bc 7687 help_echo_object = help_echo_window = Qnil;
be010514 7688 help_echo_pos = -1;
8daf1204 7689 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7690 if (NILP (help_echo))
8daf1204 7691 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7692}
4d73d038 7693
06a2c219
GM
7694
7695\f
9f8531e5
GM
7696/* Find the glyph matrix position of buffer position CHARPOS in window
7697 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7698 current glyphs must be up to date. If CHARPOS is above window
7699 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7700 of last line in W. In the row containing CHARPOS, stop before glyphs
7e376260 7701 having STOP as object. */
b8009dd1 7702
9f8531e5
GM
7703#if 0 /* This is a version of fast_find_position that's more correct
7704 in the presence of hscrolling, for example. I didn't install
7705 it right away because the problem fixed is minor, it failed
7706 in 20.x as well, and I think it's too risky to install
7707 so near the release of 21.1. 2001-09-25 gerd. */
7708
7709static int
7710fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7711 struct window *w;
7712 int charpos;
7713 int *hpos, *vpos, *x, *y;
7714 Lisp_Object stop;
7715{
7716 struct glyph_row *row, *first;
7717 struct glyph *glyph, *end;
7718 int i, past_end = 0;
7719
7720 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 7721 row = row_containing_pos (w, charpos, first, NULL, 0);
9f8531e5
GM
7722 if (row == NULL)
7723 {
7724 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7725 {
7726 *x = *y = *hpos = *vpos = 0;
7727 return 0;
7728 }
7729 else
7730 {
7731 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7732 past_end = 1;
7733 }
7734 }
7735
7736 *x = row->x;
7737 *y = row->y;
7738 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7739
7740 glyph = row->glyphs[TEXT_AREA];
7741 end = glyph + row->used[TEXT_AREA];
7742
7743 /* Skip over glyphs not having an object at the start of the row.
7744 These are special glyphs like truncation marks on terminal
7745 frames. */
7746 if (row->displays_text_p)
7747 while (glyph < end
7748 && INTEGERP (glyph->object)
7749 && !EQ (stop, glyph->object)
7750 && glyph->charpos < 0)
7751 {
7752 *x += glyph->pixel_width;
7753 ++glyph;
7754 }
7755
7756 while (glyph < end
7757 && !INTEGERP (glyph->object)
7758 && !EQ (stop, glyph->object)
7759 && (!BUFFERP (glyph->object)
7760 || glyph->charpos < charpos))
7761 {
7762 *x += glyph->pixel_width;
7763 ++glyph;
7764 }
7765
7766 *hpos = glyph - row->glyphs[TEXT_AREA];
7767 return past_end;
7768}
7769
7770#else /* not 0 */
7771
b8009dd1 7772static int
7e376260 7773fast_find_position (w, pos, hpos, vpos, x, y, stop)
06a2c219 7774 struct window *w;
b8009dd1 7775 int pos;
06a2c219 7776 int *hpos, *vpos, *x, *y;
7e376260 7777 Lisp_Object stop;
b8009dd1 7778{
b8009dd1 7779 int i;
bf1c0ba1 7780 int lastcol;
06a2c219
GM
7781 int maybe_next_line_p = 0;
7782 int line_start_position;
7783 int yb = window_text_bottom_y (w);
03d1a189
GM
7784 struct glyph_row *row, *best_row;
7785 int row_vpos, best_row_vpos;
06a2c219
GM
7786 int current_x;
7787
03d1a189
GM
7788 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7789 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7790
06a2c219 7791 while (row->y < yb)
b8009dd1 7792 {
06a2c219
GM
7793 if (row->used[TEXT_AREA])
7794 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7795 else
7796 line_start_position = 0;
7797
7798 if (line_start_position > pos)
b8009dd1 7799 break;
77b68646
RS
7800 /* If the position sought is the end of the buffer,
7801 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7802 else if (line_start_position == pos
7803 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7804 {
06a2c219 7805 maybe_next_line_p = 1;
77b68646
RS
7806 break;
7807 }
06a2c219
GM
7808 else if (line_start_position > 0)
7809 {
7810 best_row = row;
7811 best_row_vpos = row_vpos;
7812 }
4b0bb6f3
GM
7813
7814 if (row->y + row->height >= yb)
7815 break;
06a2c219
GM
7816
7817 ++row;
7818 ++row_vpos;
b8009dd1 7819 }
06a2c219
GM
7820
7821 /* Find the right column within BEST_ROW. */
7822 lastcol = 0;
7823 current_x = best_row->x;
7824 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7825 {
06a2c219 7826 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7e376260 7827 int charpos = glyph->charpos;
06a2c219 7828
7e376260 7829 if (BUFFERP (glyph->object))
bf1c0ba1 7830 {
7e376260
GM
7831 if (charpos == pos)
7832 {
7833 *hpos = i;
7834 *vpos = best_row_vpos;
7835 *x = current_x;
7836 *y = best_row->y;
7837 return 1;
7838 }
7839 else if (charpos > pos)
7840 break;
bf1c0ba1 7841 }
7e376260 7842 else if (EQ (glyph->object, stop))
4d73d038 7843 break;
06a2c219 7844
7e376260
GM
7845 if (charpos > 0)
7846 lastcol = i;
06a2c219 7847 current_x += glyph->pixel_width;
bf1c0ba1 7848 }
b8009dd1 7849
77b68646
RS
7850 /* If we're looking for the end of the buffer,
7851 and we didn't find it in the line we scanned,
7852 use the start of the following line. */
06a2c219 7853 if (maybe_next_line_p)
77b68646 7854 {
06a2c219
GM
7855 ++best_row;
7856 ++best_row_vpos;
7857 lastcol = 0;
7858 current_x = best_row->x;
77b68646
RS
7859 }
7860
06a2c219
GM
7861 *vpos = best_row_vpos;
7862 *hpos = lastcol + 1;
7863 *x = current_x;
7864 *y = best_row->y;
b8009dd1
RS
7865 return 0;
7866}
7867
9f8531e5
GM
7868#endif /* not 0 */
7869
06a2c219 7870
269b7745 7871/* Find the position of the glyph for position POS in OBJECT in
7a4bce14
PJ
7872 window W's current matrix, and return in *X, *Y the pixel
7873 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
f9db2310
GM
7874
7875 RIGHT_P non-zero means return the position of the right edge of the
7876 glyph, RIGHT_P zero means return the left edge position.
7877
7878 If no glyph for POS exists in the matrix, return the position of
7879 the glyph with the next smaller position that is in the matrix, if
7880 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7881 exists in the matrix, return the position of the glyph with the
7882 next larger position in OBJECT.
7883
7884 Value is non-zero if a glyph was found. */
7885
7886static int
7887fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7888 struct window *w;
7889 int pos;
7890 Lisp_Object object;
7891 int *hpos, *vpos, *x, *y;
7892 int right_p;
7893{
7894 int yb = window_text_bottom_y (w);
7895 struct glyph_row *r;
7896 struct glyph *best_glyph = NULL;
7897 struct glyph_row *best_row = NULL;
7898 int best_x = 0;
7899
7900 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7901 r->enabled_p && r->y < yb;
7902 ++r)
7903 {
7904 struct glyph *g = r->glyphs[TEXT_AREA];
7905 struct glyph *e = g + r->used[TEXT_AREA];
7906 int gx;
7907
7908 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7909 if (EQ (g->object, object))
7910 {
7911 if (g->charpos == pos)
7912 {
7913 best_glyph = g;
7914 best_x = gx;
7915 best_row = r;
7916 goto found;
7917 }
7918 else if (best_glyph == NULL
7919 || ((abs (g->charpos - pos)
7920 < abs (best_glyph->charpos - pos))
7921 && (right_p
7922 ? g->charpos < pos
7923 : g->charpos > pos)))
7924 {
7925 best_glyph = g;
7926 best_x = gx;
7927 best_row = r;
7928 }
7929 }
7930 }
7931
7932 found:
7933
7934 if (best_glyph)
7935 {
7936 *x = best_x;
7937 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7938
7939 if (right_p)
7940 {
7941 *x += best_glyph->pixel_width;
7942 ++*hpos;
7943 }
7944
7945 *y = best_row->y;
7946 *vpos = best_row - w->current_matrix->rows;
7947 }
7948
7949 return best_glyph != NULL;
7950}
7951
7952
b8009dd1
RS
7953/* Display the active region described by mouse_face_*
7954 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7955
7956static void
06a2c219 7957show_mouse_face (dpyinfo, draw)
7a13e894 7958 struct x_display_info *dpyinfo;
06a2c219 7959 enum draw_glyphs_face draw;
b8009dd1 7960{
7a13e894 7961 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7962 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 7963
b2bbd509
GM
7964 if (/* If window is in the process of being destroyed, don't bother
7965 to do anything. */
7966 w->current_matrix != NULL
66301061
KS
7967 /* Don't update mouse highlight if hidden */
7968 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
b2bbd509
GM
7969 /* Recognize when we are called to operate on rows that don't exist
7970 anymore. This can happen when a window is split. */
7971 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
06a2c219 7972 {
b2bbd509
GM
7973 int phys_cursor_on_p = w->phys_cursor_on_p;
7974 struct glyph_row *row, *first, *last;
06a2c219 7975
b2bbd509
GM
7976 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7977 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7978
7979 for (row = first; row <= last && row->enabled_p; ++row)
06a2c219 7980 {
b2bbd509 7981 int start_hpos, end_hpos, start_x;
06a2c219 7982
b2bbd509
GM
7983 /* For all but the first row, the highlight starts at column 0. */
7984 if (row == first)
7985 {
7986 start_hpos = dpyinfo->mouse_face_beg_col;
7987 start_x = dpyinfo->mouse_face_beg_x;
7988 }
7989 else
7990 {
7991 start_hpos = 0;
7992 start_x = 0;
7993 }
06a2c219 7994
b2bbd509
GM
7995 if (row == last)
7996 end_hpos = dpyinfo->mouse_face_end_col;
7997 else
7998 end_hpos = row->used[TEXT_AREA];
b8009dd1 7999
b2bbd509
GM
8000 if (end_hpos > start_hpos)
8001 {
8002 x_draw_glyphs (w, start_x, row, TEXT_AREA,
8003 start_hpos, end_hpos, draw, 0);
b8009dd1 8004
60626bab
GM
8005 row->mouse_face_p
8006 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
b2bbd509
GM
8007 }
8008 }
2729a2b5 8009
b2bbd509
GM
8010 /* When we've written over the cursor, arrange for it to
8011 be displayed again. */
8012 if (phys_cursor_on_p && !w->phys_cursor_on_p)
8013 x_display_cursor (w, 1,
8014 w->phys_cursor.hpos, w->phys_cursor.vpos,
8015 w->phys_cursor.x, w->phys_cursor.y);
8016 }
fb3b7de5 8017
06a2c219
GM
8018 /* Change the mouse cursor. */
8019 if (draw == DRAW_NORMAL_TEXT)
8020 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8021 f->output_data.x->text_cursor);
8022 else if (draw == DRAW_MOUSE_FACE)
334208b7 8023 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 8024 f->output_data.x->cross_cursor);
27ead1d5 8025 else
334208b7 8026 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 8027 f->output_data.x->nontext_cursor);
b8009dd1
RS
8028}
8029
8030/* Clear out the mouse-highlighted active region.
fa262c07
GM
8031 Redraw it un-highlighted first. Value is non-zero if mouse
8032 face was actually drawn unhighlighted. */
b8009dd1 8033
fa262c07 8034static int
7a13e894
RS
8035clear_mouse_face (dpyinfo)
8036 struct x_display_info *dpyinfo;
b8009dd1 8037{
fa262c07 8038 int cleared = 0;
06a2c219 8039
fa262c07
GM
8040 if (!NILP (dpyinfo->mouse_face_window))
8041 {
8042 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
8043 cleared = 1;
8044 }
b8009dd1 8045
7a13e894
RS
8046 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8047 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8048 dpyinfo->mouse_face_window = Qnil;
1b85dc1c 8049 dpyinfo->mouse_face_overlay = Qnil;
fa262c07 8050 return cleared;
b8009dd1 8051}
e687d06e 8052
71b8321e
GM
8053
8054/* Clear any mouse-face on window W. This function is part of the
8055 redisplay interface, and is called from try_window_id and similar
8056 functions to ensure the mouse-highlight is off. */
8057
8058static void
8059x_clear_mouse_face (w)
8060 struct window *w;
8061{
8062 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
8063 Lisp_Object window;
8064
2e636f9d 8065 BLOCK_INPUT;
71b8321e
GM
8066 XSETWINDOW (window, w);
8067 if (EQ (window, dpyinfo->mouse_face_window))
8068 clear_mouse_face (dpyinfo);
2e636f9d 8069 UNBLOCK_INPUT;
71b8321e
GM
8070}
8071
8072
e687d06e
RS
8073/* Just discard the mouse face information for frame F, if any.
8074 This is used when the size of F is changed. */
8075
dfcf069d 8076void
e687d06e
RS
8077cancel_mouse_face (f)
8078 FRAME_PTR f;
8079{
8080 Lisp_Object window;
8081 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8082
8083 window = dpyinfo->mouse_face_window;
8084 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
8085 {
8086 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8087 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8088 dpyinfo->mouse_face_window = Qnil;
8089 }
8090}
b52b65bd 8091
b8009dd1 8092\f
b52b65bd
GM
8093static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
8094
8095
8096/* Try to determine frame pixel position and size of the glyph under
8097 frame pixel coordinates X/Y on frame F . Return the position and
8098 size in *RECT. Value is non-zero if we could compute these
8099 values. */
8100
8101static int
8102glyph_rect (f, x, y, rect)
8103 struct frame *f;
8104 int x, y;
8105 XRectangle *rect;
8106{
8107 Lisp_Object window;
8108 int part, found = 0;
8109
8110 window = window_from_coordinates (f, x, y, &part, 0);
8111 if (!NILP (window))
8112 {
8113 struct window *w = XWINDOW (window);
8114 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8115 struct glyph_row *end = r + w->current_matrix->nrows - 1;
b52b65bd
GM
8116
8117 frame_to_window_pixel_xy (w, &x, &y);
8118
8119 for (; !found && r < end && r->enabled_p; ++r)
8120 if (r->y >= y)
8121 {
8122 struct glyph *g = r->glyphs[TEXT_AREA];
8123 struct glyph *end = g + r->used[TEXT_AREA];
8124 int gx;
8125
8126 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
8127 if (gx >= x)
8128 {
8129 rect->width = g->pixel_width;
8130 rect->height = r->height;
8131 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
8132 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
8133 found = 1;
8134 }
8135 }
8136 }
8137
8138 return found;
8139}
8140
12ba150f 8141
90e65f07 8142/* Return the current position of the mouse.
b52b65bd 8143 *FP should be a frame which indicates which display to ask about.
90e65f07 8144
b52b65bd
GM
8145 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
8146 and *PART to the frame, window, and scroll bar part that the mouse
8147 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 8148 position on the scroll bar.
12ba150f 8149
b52b65bd
GM
8150 If the mouse movement started elsewhere, set *FP to the frame the
8151 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
8152 the mouse is over.
8153
b52b65bd 8154 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
8155 was at this position.
8156
a135645a
RS
8157 Don't store anything if we don't have a valid set of values to report.
8158
90e65f07 8159 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 8160 movement. */
90e65f07
JB
8161
8162static void
1cf412ec 8163XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 8164 FRAME_PTR *fp;
1cf412ec 8165 int insist;
12ba150f 8166 Lisp_Object *bar_window;
ab648270 8167 enum scroll_bar_part *part;
90e65f07 8168 Lisp_Object *x, *y;
e5d77022 8169 unsigned long *time;
90e65f07 8170{
a135645a
RS
8171 FRAME_PTR f1;
8172
90e65f07
JB
8173 BLOCK_INPUT;
8174
8bcee03e 8175 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 8176 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
8177 else
8178 {
12ba150f
JB
8179 Window root;
8180 int root_x, root_y;
90e65f07 8181
12ba150f
JB
8182 Window dummy_window;
8183 int dummy;
8184
39d8bb4d
KH
8185 Lisp_Object frame, tail;
8186
8187 /* Clear the mouse-moved flag for every frame on this display. */
8188 FOR_EACH_FRAME (tail, frame)
8189 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8190 XFRAME (frame)->mouse_moved = 0;
8191
ab648270 8192 last_mouse_scroll_bar = Qnil;
12ba150f
JB
8193
8194 /* Figure out which root window we're on. */
334208b7
RS
8195 XQueryPointer (FRAME_X_DISPLAY (*fp),
8196 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
8197
8198 /* The root window which contains the pointer. */
8199 &root,
8200
8201 /* Trash which we can't trust if the pointer is on
8202 a different screen. */
8203 &dummy_window,
8204
8205 /* The position on that root window. */
58769bee 8206 &root_x, &root_y,
12ba150f
JB
8207
8208 /* More trash we can't trust. */
8209 &dummy, &dummy,
8210
8211 /* Modifier keys and pointer buttons, about which
8212 we don't care. */
8213 (unsigned int *) &dummy);
8214
8215 /* Now we have a position on the root; find the innermost window
8216 containing the pointer. */
8217 {
8218 Window win, child;
8219 int win_x, win_y;
06a2c219 8220 int parent_x = 0, parent_y = 0;
e99db5a1 8221 int count;
12ba150f
JB
8222
8223 win = root;
69388238 8224
2d7fc7e8
RS
8225 /* XTranslateCoordinates can get errors if the window
8226 structure is changing at the same time this function
8227 is running. So at least we must not crash from them. */
8228
e99db5a1 8229 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 8230
334208b7 8231 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 8232 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 8233 {
69388238
RS
8234 /* If mouse was grabbed on a frame, give coords for that frame
8235 even if the mouse is now outside it. */
334208b7 8236 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 8237
12ba150f 8238 /* From-window, to-window. */
69388238 8239 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
8240
8241 /* From-position, to-position. */
8242 root_x, root_y, &win_x, &win_y,
8243
8244 /* Child of win. */
8245 &child);
69388238
RS
8246 f1 = last_mouse_frame;
8247 }
8248 else
8249 {
8250 while (1)
8251 {
334208b7 8252 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 8253
69388238
RS
8254 /* From-window, to-window. */
8255 root, win,
12ba150f 8256
69388238
RS
8257 /* From-position, to-position. */
8258 root_x, root_y, &win_x, &win_y,
8259
8260 /* Child of win. */
8261 &child);
8262
9af3143a 8263 if (child == None || child == win)
69388238
RS
8264 break;
8265
8266 win = child;
8267 parent_x = win_x;
8268 parent_y = win_y;
8269 }
12ba150f 8270
69388238
RS
8271 /* Now we know that:
8272 win is the innermost window containing the pointer
8273 (XTC says it has no child containing the pointer),
8274 win_x and win_y are the pointer's position in it
8275 (XTC did this the last time through), and
8276 parent_x and parent_y are the pointer's position in win's parent.
8277 (They are what win_x and win_y were when win was child.
8278 If win is the root window, it has no parent, and
8279 parent_{x,y} are invalid, but that's okay, because we'll
8280 never use them in that case.) */
8281
8282 /* Is win one of our frames? */
19126e11 8283 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
8284
8285#ifdef USE_X_TOOLKIT
8286 /* If we end up with the menu bar window, say it's not
8287 on the frame. */
8288 if (f1 != NULL
8289 && f1->output_data.x->menubar_widget
8290 && win == XtWindow (f1->output_data.x->menubar_widget))
8291 f1 = NULL;
8292#endif /* USE_X_TOOLKIT */
69388238 8293 }
58769bee 8294
2d7fc7e8
RS
8295 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8296 f1 = 0;
8297
e99db5a1 8298 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 8299
ab648270 8300 /* If not, is it one of our scroll bars? */
a135645a 8301 if (! f1)
12ba150f 8302 {
ab648270 8303 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
8304
8305 if (bar)
8306 {
a135645a 8307 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8308 win_x = parent_x;
8309 win_y = parent_y;
8310 }
8311 }
90e65f07 8312
8bcee03e 8313 if (f1 == 0 && insist > 0)
b86bd3dd 8314 f1 = SELECTED_FRAME ();
1cf412ec 8315
a135645a 8316 if (f1)
12ba150f 8317 {
06a2c219
GM
8318 /* Ok, we found a frame. Store all the values.
8319 last_mouse_glyph is a rectangle used to reduce the
8320 generation of mouse events. To not miss any motion
8321 events, we must divide the frame into rectangles of the
8322 size of the smallest character that could be displayed
8323 on it, i.e. into the same rectangles that matrices on
8324 the frame are divided into. */
8325
b52b65bd
GM
8326 int width, height, gx, gy;
8327 XRectangle rect;
8328
8329 if (glyph_rect (f1, win_x, win_y, &rect))
8330 last_mouse_glyph = rect;
8331 else
8332 {
8333 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8334 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8335 gx = win_x;
8336 gy = win_y;
06a2c219 8337
b52b65bd
GM
8338 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8339 round down even for negative values. */
8340 if (gx < 0)
8341 gx -= width - 1;
4f00e84d 8342 if (gy < 0)
b52b65bd
GM
8343 gy -= height - 1;
8344 gx = (gx + width - 1) / width * width;
8345 gy = (gy + height - 1) / height * height;
8346
8347 last_mouse_glyph.width = width;
8348 last_mouse_glyph.height = height;
8349 last_mouse_glyph.x = gx;
8350 last_mouse_glyph.y = gy;
8351 }
12ba150f
JB
8352
8353 *bar_window = Qnil;
8354 *part = 0;
334208b7 8355 *fp = f1;
e0c1aef2
KH
8356 XSETINT (*x, win_x);
8357 XSETINT (*y, win_y);
12ba150f
JB
8358 *time = last_mouse_movement_time;
8359 }
8360 }
8361 }
90e65f07
JB
8362
8363 UNBLOCK_INPUT;
8364}
f451eb13 8365
06a2c219 8366
06a2c219 8367#ifdef USE_X_TOOLKIT
bffcfca9
GM
8368
8369/* Atimer callback function for TIMER. Called every 0.1s to process
8370 Xt timeouts, if needed. We must avoid calling XtAppPending as
8371 much as possible because that function does an implicit XFlush
8372 that slows us down. */
8373
8374static void
8375x_process_timeouts (timer)
8376 struct atimer *timer;
8377{
8378 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8379 {
8380 BLOCK_INPUT;
8381 while (XtAppPending (Xt_app_con) & XtIMTimer)
8382 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8383 UNBLOCK_INPUT;
8384 }
06a2c219
GM
8385}
8386
bffcfca9 8387#endif /* USE_X_TOOLKIT */
06a2c219
GM
8388
8389\f
8390/* Scroll bar support. */
8391
8392/* Given an X window ID, find the struct scroll_bar which manages it.
8393 This can be called in GC, so we have to make sure to strip off mark
8394 bits. */
bffcfca9 8395
06a2c219
GM
8396static struct scroll_bar *
8397x_window_to_scroll_bar (window_id)
8398 Window window_id;
8399{
8400 Lisp_Object tail;
8401
8402 for (tail = Vframe_list;
8403 XGCTYPE (tail) == Lisp_Cons;
8e713be6 8404 tail = XCDR (tail))
06a2c219
GM
8405 {
8406 Lisp_Object frame, bar, condemned;
8407
8e713be6 8408 frame = XCAR (tail);
06a2c219
GM
8409 /* All elements of Vframe_list should be frames. */
8410 if (! GC_FRAMEP (frame))
8411 abort ();
8412
8413 /* Scan this frame's scroll bar list for a scroll bar with the
8414 right window ID. */
8415 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8416 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8417 /* This trick allows us to search both the ordinary and
8418 condemned scroll bar lists with one loop. */
8419 ! GC_NILP (bar) || (bar = condemned,
8420 condemned = Qnil,
8421 ! GC_NILP (bar));
8422 bar = XSCROLL_BAR (bar)->next)
8423 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8424 return XSCROLL_BAR (bar);
8425 }
8426
8427 return 0;
8428}
8429
8430
01f67d2c 8431#if defined USE_LUCID
c95fc5f1
GM
8432
8433/* Return the Lucid menu bar WINDOW is part of. Return null
8434 if WINDOW is not part of a menu bar. */
8435
8436static Widget
8437x_window_to_menu_bar (window)
8438 Window window;
8439{
8440 Lisp_Object tail;
8441
8442 for (tail = Vframe_list;
8443 XGCTYPE (tail) == Lisp_Cons;
8444 tail = XCDR (tail))
8445 {
8446 Lisp_Object frame = XCAR (tail);
8447 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8448
8449 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8450 return menu_bar;
8451 }
8452
8453 return NULL;
8454}
8455
01f67d2c 8456#endif /* USE_LUCID */
c95fc5f1 8457
06a2c219
GM
8458\f
8459/************************************************************************
8460 Toolkit scroll bars
8461 ************************************************************************/
8462
eccc05db 8463#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8464
8465static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8466static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8467static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8468 struct scroll_bar *));
8469static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8470 int, int, int));
8471
8472
8473/* Id of action hook installed for scroll bars. */
8474
8475static XtActionHookId action_hook_id;
8476
8477/* Lisp window being scrolled. Set when starting to interact with
8478 a toolkit scroll bar, reset to nil when ending the interaction. */
8479
8480static Lisp_Object window_being_scrolled;
8481
8482/* Last scroll bar part sent in xm_scroll_callback. */
8483
8484static int last_scroll_bar_part;
8485
ec18280f
SM
8486/* Whether this is an Xaw with arrow-scrollbars. This should imply
8487 that movements of 1/20 of the screen size are mapped to up/down. */
8488
8489static Boolean xaw3d_arrow_scroll;
8490
8491/* Whether the drag scrolling maintains the mouse at the top of the
8492 thumb. If not, resizing the thumb needs to be done more carefully
8493 to avoid jerkyness. */
8494
8495static Boolean xaw3d_pick_top;
8496
06a2c219
GM
8497
8498/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 8499 bars are used.. The hook is responsible for detecting when
06a2c219 8500 the user ends an interaction with the scroll bar, and generates
3b8f9651 8501 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
8502
8503static void
8504xt_action_hook (widget, client_data, action_name, event, params,
8505 num_params)
8506 Widget widget;
8507 XtPointer client_data;
8508 String action_name;
8509 XEvent *event;
8510 String *params;
8511 Cardinal *num_params;
8512{
8513 int scroll_bar_p;
8514 char *end_action;
8515
8516#ifdef USE_MOTIF
8517 scroll_bar_p = XmIsScrollBar (widget);
8518 end_action = "Release";
ec18280f 8519#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8520 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8521 end_action = "EndScroll";
ec18280f 8522#endif /* USE_MOTIF */
06a2c219 8523
06a2c219
GM
8524 if (scroll_bar_p
8525 && strcmp (action_name, end_action) == 0
8526 && WINDOWP (window_being_scrolled))
8527 {
8528 struct window *w;
8529
8530 x_send_scroll_bar_event (window_being_scrolled,
8531 scroll_bar_end_scroll, 0, 0);
8532 w = XWINDOW (window_being_scrolled);
8533 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8534 window_being_scrolled = Qnil;
8535 last_scroll_bar_part = -1;
bffcfca9
GM
8536
8537 /* Xt timeouts no longer needed. */
8538 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
8539 }
8540}
8541
07b3d16e
GM
8542/* A vector of windows used for communication between
8543 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8544
8545static struct window **scroll_bar_windows;
8546static int scroll_bar_windows_size;
8547
06a2c219
GM
8548
8549/* Send a client message with message type Xatom_Scrollbar for a
8550 scroll action to the frame of WINDOW. PART is a value identifying
8551 the part of the scroll bar that was clicked on. PORTION is the
8552 amount to scroll of a whole of WHOLE. */
8553
8554static void
8555x_send_scroll_bar_event (window, part, portion, whole)
8556 Lisp_Object window;
8557 int part, portion, whole;
8558{
8559 XEvent event;
8560 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
8561 struct window *w = XWINDOW (window);
8562 struct frame *f = XFRAME (w->frame);
8563 int i;
06a2c219 8564
07b3d16e
GM
8565 BLOCK_INPUT;
8566
06a2c219
GM
8567 /* Construct a ClientMessage event to send to the frame. */
8568 ev->type = ClientMessage;
8569 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8570 ev->display = FRAME_X_DISPLAY (f);
8571 ev->window = FRAME_X_WINDOW (f);
8572 ev->format = 32;
07b3d16e
GM
8573
8574 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8575 not enough to store a pointer or Lisp_Object on a 64 bit system.
8576 So, store the window in scroll_bar_windows and pass the index
8577 into that array in the event. */
8578 for (i = 0; i < scroll_bar_windows_size; ++i)
8579 if (scroll_bar_windows[i] == NULL)
8580 break;
8581
8582 if (i == scroll_bar_windows_size)
8583 {
8584 int new_size = max (10, 2 * scroll_bar_windows_size);
8585 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8586 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8587
8588 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8589 nbytes);
8590 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8591 scroll_bar_windows_size = new_size;
8592 }
8593
8594 scroll_bar_windows[i] = w;
8595 ev->data.l[0] = (long) i;
06a2c219
GM
8596 ev->data.l[1] = (long) part;
8597 ev->data.l[2] = (long) 0;
8598 ev->data.l[3] = (long) portion;
8599 ev->data.l[4] = (long) whole;
8600
bffcfca9
GM
8601 /* Make Xt timeouts work while the scroll bar is active. */
8602 toolkit_scroll_bar_interaction = 1;
8603
06a2c219
GM
8604 /* Setting the event mask to zero means that the message will
8605 be sent to the client that created the window, and if that
8606 window no longer exists, no event will be sent. */
06a2c219
GM
8607 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8608 UNBLOCK_INPUT;
8609}
8610
8611
8612/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8613 in *IEVENT. */
8614
8615static void
8616x_scroll_bar_to_input_event (event, ievent)
8617 XEvent *event;
8618 struct input_event *ievent;
8619{
8620 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
8621 Lisp_Object window;
8622 struct frame *f;
07b3d16e
GM
8623 struct window *w;
8624
8625 w = scroll_bar_windows[ev->data.l[0]];
8626 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 8627
07b3d16e
GM
8628 XSETWINDOW (window, w);
8629 f = XFRAME (w->frame);
06a2c219 8630
3b8f9651 8631 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 8632 ievent->frame_or_window = window;
0f8aabe9 8633 ievent->arg = Qnil;
06a2c219
GM
8634 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8635 ievent->part = ev->data.l[1];
8636 ievent->code = ev->data.l[2];
8637 ievent->x = make_number ((int) ev->data.l[3]);
8638 ievent->y = make_number ((int) ev->data.l[4]);
8639 ievent->modifiers = 0;
8640}
8641
8642
8643#ifdef USE_MOTIF
8644
8645/* Minimum and maximum values used for Motif scroll bars. */
8646
8647#define XM_SB_MIN 1
8648#define XM_SB_MAX 10000000
8649#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8650
8651
8652/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8653 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4696802b 8654 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
06a2c219
GM
8655
8656static void
8657xm_scroll_callback (widget, client_data, call_data)
8658 Widget widget;
8659 XtPointer client_data, call_data;
8660{
8661 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8662 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
8663 int part = -1, whole = 0, portion = 0;
8664
8665 switch (cs->reason)
8666 {
8667 case XmCR_DECREMENT:
8668 bar->dragging = Qnil;
8669 part = scroll_bar_up_arrow;
8670 break;
8671
8672 case XmCR_INCREMENT:
8673 bar->dragging = Qnil;
8674 part = scroll_bar_down_arrow;
8675 break;
8676
8677 case XmCR_PAGE_DECREMENT:
8678 bar->dragging = Qnil;
8679 part = scroll_bar_above_handle;
8680 break;
8681
8682 case XmCR_PAGE_INCREMENT:
8683 bar->dragging = Qnil;
8684 part = scroll_bar_below_handle;
8685 break;
8686
8687 case XmCR_TO_TOP:
8688 bar->dragging = Qnil;
8689 part = scroll_bar_to_top;
8690 break;
8691
8692 case XmCR_TO_BOTTOM:
8693 bar->dragging = Qnil;
8694 part = scroll_bar_to_bottom;
8695 break;
8696
8697 case XmCR_DRAG:
8698 {
8699 int slider_size;
8700 int dragging_down_p = (INTEGERP (bar->dragging)
8701 && XINT (bar->dragging) <= cs->value);
8702
8703 /* Get the slider size. */
8704 BLOCK_INPUT;
8705 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8706 UNBLOCK_INPUT;
8707
6f2a7a68
SM
8708 whole = XM_SB_RANGE - slider_size;
8709 portion = min (cs->value - XM_SB_MIN, whole);
8710 part = scroll_bar_handle;
8711 bar->dragging = make_number (cs->value);
06a2c219
GM
8712 }
8713 break;
8714
8715 case XmCR_VALUE_CHANGED:
8716 break;
8717 };
8718
8719 if (part >= 0)
8720 {
8721 window_being_scrolled = bar->window;
8722 last_scroll_bar_part = part;
8723 x_send_scroll_bar_event (bar->window, part, portion, whole);
8724 }
8725}
8726
8727
ec18280f 8728#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
8729
8730
ec18280f 8731/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8732 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8733 scroll bar struct. CALL_DATA is a pointer to a float saying where
8734 the thumb is. */
8735
8736static void
ec18280f 8737xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8738 Widget widget;
8739 XtPointer client_data, call_data;
8740{
8741 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8742 float top = *(float *) call_data;
8743 float shown;
ec18280f
SM
8744 int whole, portion, height;
8745 int part;
06a2c219
GM
8746
8747 /* Get the size of the thumb, a value between 0 and 1. */
8748 BLOCK_INPUT;
ec18280f 8749 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8750 UNBLOCK_INPUT;
8751
8752 whole = 10000000;
8753 portion = shown < 1 ? top * whole : 0;
06a2c219 8754
ec18280f
SM
8755 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8756 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8757 the bottom, so we force the scrolling whenever we see that we're
8758 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8759 we try to ensure that we always stay two pixels away from the
8760 bottom). */
06a2c219
GM
8761 part = scroll_bar_down_arrow;
8762 else
8763 part = scroll_bar_handle;
8764
8765 window_being_scrolled = bar->window;
8766 bar->dragging = make_number (portion);
8767 last_scroll_bar_part = part;
8768 x_send_scroll_bar_event (bar->window, part, portion, whole);
8769}
8770
8771
ec18280f
SM
8772/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8773 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8774 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8775 the scroll bar. CALL_DATA is an integer specifying the action that
8776 has taken place. It's magnitude is in the range 0..height of the
8777 scroll bar. Negative values mean scroll towards buffer start.
8778 Values < height of scroll bar mean line-wise movement. */
8779
8780static void
ec18280f 8781xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8782 Widget widget;
8783 XtPointer client_data, call_data;
8784{
8785 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8786 int position = (int) call_data;
8787 Dimension height;
8788 int part;
8789
8790 /* Get the height of the scroll bar. */
8791 BLOCK_INPUT;
8792 XtVaGetValues (widget, XtNheight, &height, NULL);
8793 UNBLOCK_INPUT;
8794
ec18280f
SM
8795 if (abs (position) >= height)
8796 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8797
8798 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8799 it maps line-movement to call_data = max(5, height/20). */
8800 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8801 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8802 else
ec18280f 8803 part = scroll_bar_move_ratio;
06a2c219
GM
8804
8805 window_being_scrolled = bar->window;
8806 bar->dragging = Qnil;
8807 last_scroll_bar_part = part;
ec18280f 8808 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8809}
8810
8811
8812#endif /* not USE_MOTIF */
8813
8814
8815/* Create the widget for scroll bar BAR on frame F. Record the widget
8816 and X window of the scroll bar in BAR. */
8817
8818static void
8819x_create_toolkit_scroll_bar (f, bar)
8820 struct frame *f;
8821 struct scroll_bar *bar;
8822{
8823 Window xwindow;
8824 Widget widget;
8825 Arg av[20];
8826 int ac = 0;
8827 char *scroll_bar_name = "verticalScrollBar";
8828 unsigned long pixel;
8829
8830 BLOCK_INPUT;
8831
8832#ifdef USE_MOTIF
06a2c219
GM
8833 /* Set resources. Create the widget. */
8834 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8835 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8836 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8837 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8838 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8839 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8840 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8841
8842 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8843 if (pixel != -1)
8844 {
8845 XtSetArg (av[ac], XmNforeground, pixel);
8846 ++ac;
8847 }
8848
8849 pixel = f->output_data.x->scroll_bar_background_pixel;
8850 if (pixel != -1)
8851 {
8852 XtSetArg (av[ac], XmNbackground, pixel);
8853 ++ac;
8854 }
8855
8856 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8857 scroll_bar_name, av, ac);
8858
8859 /* Add one callback for everything that can happen. */
8860 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8861 (XtPointer) bar);
8862 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8863 (XtPointer) bar);
8864 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8865 (XtPointer) bar);
8866 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8867 (XtPointer) bar);
8868 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8869 (XtPointer) bar);
8870 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8871 (XtPointer) bar);
8872 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8873 (XtPointer) bar);
8874
8875 /* Realize the widget. Only after that is the X window created. */
8876 XtRealizeWidget (widget);
8877
8878 /* Set the cursor to an arrow. I didn't find a resource to do that.
8879 And I'm wondering why it hasn't an arrow cursor by default. */
8880 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8881 f->output_data.x->nontext_cursor);
8882
ec18280f 8883#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8884
8885 /* Set resources. Create the widget. The background of the
8886 Xaw3d scroll bar widget is a little bit light for my taste.
8887 We don't alter it here to let users change it according
8888 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8889 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8890 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8891 /* For smoother scrolling with Xaw3d -sm */
8892 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
06a2c219
GM
8893
8894 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8895 if (pixel != -1)
8896 {
8897 XtSetArg (av[ac], XtNforeground, pixel);
8898 ++ac;
8899 }
8900
8901 pixel = f->output_data.x->scroll_bar_background_pixel;
8902 if (pixel != -1)
8903 {
8904 XtSetArg (av[ac], XtNbackground, pixel);
8905 ++ac;
8906 }
7c1bef7a
MB
8907
8908 /* Top/bottom shadow colors. */
8909
8910 /* Allocate them, if necessary. */
8911 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8912 {
8913 pixel = f->output_data.x->scroll_bar_background_pixel;
8914 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8915 &pixel, 1.2, 0x8000))
8916 pixel = -1;
8917 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8918 }
8919 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8920 {
8921 pixel = f->output_data.x->scroll_bar_background_pixel;
8922 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8923 &pixel, 0.6, 0x4000))
8924 pixel = -1;
8925 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8926 }
8927
8928 /* Tell the toolkit about them. */
8929 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8930 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8931 /* We tried to allocate a color for the top/bottom shadow, and
8932 failed, so tell Xaw3d to use dithering instead. */
8933 {
8934 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8935 ++ac;
8936 }
8937 else
8938 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8939 be more consistent with other emacs 3d colors, and since Xaw3d is
8940 not good at dealing with allocation failure. */
8941 {
8942 /* This tells Xaw3d to use real colors instead of dithering for
8943 the shadows. */
8944 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8945 ++ac;
8946
8947 /* Specify the colors. */
8948 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8949 if (pixel != -1)
8950 {
8951 XtSetArg (av[ac], "topShadowPixel", pixel);
8952 ++ac;
8953 }
8954 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8955 if (pixel != -1)
8956 {
8957 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8958 ++ac;
8959 }
8960 }
8961
06a2c219
GM
8962 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8963 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8964
8965 {
8966 char *initial = "";
8967 char *val = initial;
8968 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8969 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8970 if (val == initial)
8971 { /* ARROW_SCROLL */
8972 xaw3d_arrow_scroll = True;
8973 /* Isn't that just a personal preference ? -sm */
8974 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8975 }
8976 }
06a2c219
GM
8977
8978 /* Define callbacks. */
ec18280f
SM
8979 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8980 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8981 (XtPointer) bar);
8982
8983 /* Realize the widget. Only after that is the X window created. */
8984 XtRealizeWidget (widget);
8985
ec18280f 8986#endif /* !USE_MOTIF */
06a2c219 8987
2a51c026 8988 /* Install an action hook that lets us detect when the user
06a2c219
GM
8989 finishes interacting with a scroll bar. */
8990 if (action_hook_id == 0)
8991 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8992
8993 /* Remember X window and widget in the scroll bar vector. */
8994 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8995 xwindow = XtWindow (widget);
8996 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8997
8998 UNBLOCK_INPUT;
8999}
9000
9001
9002/* Set the thumb size and position of scroll bar BAR. We are currently
9003 displaying PORTION out of a whole WHOLE, and our position POSITION. */
9004
9005static void
9006x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
9007 struct scroll_bar *bar;
9008 int portion, position, whole;
f451eb13 9009{
e83dc917
GM
9010 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9011 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 9012 float top, shown;
f451eb13 9013
6f2a7a68
SM
9014 BLOCK_INPUT;
9015
9016#ifdef USE_MOTIF
9017
9018 /* We use an estimate of 30 chars per line rather than the real
9019 `portion' value. This has the disadvantage that the thumb size
9020 is not very representative, but it makes our life a lot easier.
9021 Otherwise, we have to constantly adjust the thumb size, which
9022 we can't always do quickly enough: while dragging, the size of
9023 the thumb might prevent the user from dragging the thumb all the
9024 way to the end. but Motif and some versions of Xaw3d don't allow
9025 updating the thumb size while dragging. Also, even if we can update
9026 its size, the update will often happen too late.
9027 If you don't believe it, check out revision 1.650 of xterm.c to see
9028 what hoops we were going through and the still poor behavior we got. */
9029 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
9030 /* When the thumb is at the bottom, position == whole.
9031 So we need to increase `whole' to make space for the thumb. */
9032 whole += portion;
9033
9034 if (whole <= 0)
06a2c219
GM
9035 top = 0, shown = 1;
9036 else
f451eb13 9037 {
06a2c219
GM
9038 top = (float) position / whole;
9039 shown = (float) portion / whole;
9040 }
f451eb13 9041
6f2a7a68
SM
9042 if (NILP (bar->dragging))
9043 {
9044 int size, value;
06a2c219 9045
6f2a7a68
SM
9046 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
9047 is the scroll bar's maximum and MIN is the scroll bar's minimum
9048 value. */
9049 size = shown * XM_SB_RANGE;
9050 size = min (size, XM_SB_RANGE);
9051 size = max (size, 1);
06a2c219 9052
6f2a7a68
SM
9053 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
9054 value = top * XM_SB_RANGE;
9055 value = min (value, XM_SB_MAX - size);
9056 value = max (value, XM_SB_MIN);
9057
06a2c219 9058 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 9059 }
ec18280f 9060#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
9061
9062 if (whole == 0)
9063 top = 0, shown = 1;
9064 else
9065 {
9066 top = (float) position / whole;
9067 shown = (float) portion / whole;
9068 }
9069
06a2c219 9070 {
ec18280f
SM
9071 float old_top, old_shown;
9072 Dimension height;
9073 XtVaGetValues (widget,
9074 XtNtopOfThumb, &old_top,
9075 XtNshown, &old_shown,
9076 XtNheight, &height,
9077 NULL);
9078
9079 /* Massage the top+shown values. */
9080 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
9081 top = max (0, min (1, top));
9082 else
9083 top = old_top;
9084 /* Keep two pixels available for moving the thumb down. */
9085 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
9086
9087 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
9088 check that your system's configuration file contains a define
9089 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 9090 if (top != old_top || shown != old_shown)
eb393530 9091 {
ec18280f 9092 if (NILP (bar->dragging))
eb393530 9093 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
9094 else
9095 {
ec18280f
SM
9096#ifdef HAVE_XAW3D
9097 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 9098 int scroll_mode = 0;
ec18280f
SM
9099
9100 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
9101 if (xaw3d_arrow_scroll)
9102 {
9103 /* Xaw3d stupidly ignores resize requests while dragging
9104 so we have to make it believe it's not in dragging mode. */
9105 scroll_mode = sb->scrollbar.scroll_mode;
9106 if (scroll_mode == 2)
9107 sb->scrollbar.scroll_mode = 0;
9108 }
9109#endif
9110 /* Try to make the scrolling a tad smoother. */
9111 if (!xaw3d_pick_top)
9112 shown = min (shown, old_shown);
9113
9114 XawScrollbarSetThumb (widget, top, shown);
9115
9116#ifdef HAVE_XAW3D
9117 if (xaw3d_arrow_scroll && scroll_mode == 2)
9118 sb->scrollbar.scroll_mode = scroll_mode;
9119#endif
06a2c219 9120 }
06a2c219
GM
9121 }
9122 }
ec18280f 9123#endif /* !USE_MOTIF */
06a2c219
GM
9124
9125 UNBLOCK_INPUT;
f451eb13
JB
9126}
9127
06a2c219
GM
9128#endif /* USE_TOOLKIT_SCROLL_BARS */
9129
9130
9131\f
9132/************************************************************************
9133 Scroll bars, general
9134 ************************************************************************/
9135
9136/* Create a scroll bar and return the scroll bar vector for it. W is
9137 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 9138 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
9139 scroll bar. */
9140
ab648270 9141static struct scroll_bar *
06a2c219
GM
9142x_scroll_bar_create (w, top, left, width, height)
9143 struct window *w;
f451eb13
JB
9144 int top, left, width, height;
9145{
06a2c219 9146 struct frame *f = XFRAME (w->frame);
334208b7
RS
9147 struct scroll_bar *bar
9148 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
9149
9150 BLOCK_INPUT;
9151
eccc05db 9152#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9153 x_create_toolkit_scroll_bar (f, bar);
9154#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9155 {
9156 XSetWindowAttributes a;
9157 unsigned long mask;
5c187dee 9158 Window window;
06a2c219
GM
9159
9160 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9161 if (a.background_pixel == -1)
9162 a.background_pixel = f->output_data.x->background_pixel;
9163
12ba150f 9164 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 9165 | ButtonMotionMask | PointerMotionHintMask
12ba150f 9166 | ExposureMask);
7a13e894 9167 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 9168
dbc4e1c1 9169 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 9170
06a2c219
GM
9171 /* Clear the area of W that will serve as a scroll bar. This is
9172 for the case that a window has been split horizontally. In
9173 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
9174 if (width > 0 && height > 0)
9175 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9176 left, top, width,
9177 window_box_height (w), False);
06a2c219
GM
9178
9179 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9180 /* Position and size of scroll bar. */
9181 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9182 top,
9183 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9184 height,
9185 /* Border width, depth, class, and visual. */
9186 0,
9187 CopyFromParent,
9188 CopyFromParent,
9189 CopyFromParent,
9190 /* Attributes. */
9191 mask, &a);
9192 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 9193 }
06a2c219 9194#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9195
06a2c219 9196 XSETWINDOW (bar->window, w);
e0c1aef2
KH
9197 XSETINT (bar->top, top);
9198 XSETINT (bar->left, left);
9199 XSETINT (bar->width, width);
9200 XSETINT (bar->height, height);
9201 XSETINT (bar->start, 0);
9202 XSETINT (bar->end, 0);
12ba150f 9203 bar->dragging = Qnil;
f451eb13
JB
9204
9205 /* Add bar to its frame's list of scroll bars. */
334208b7 9206 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 9207 bar->prev = Qnil;
334208b7 9208 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 9209 if (!NILP (bar->next))
e0c1aef2 9210 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 9211
06a2c219 9212 /* Map the window/widget. */
eccc05db 9213#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
9214 {
9215 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9216 XtConfigureWidget (scroll_bar,
9217 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9218 top,
9219 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9220 max (height, 1), 0);
9221 XtMapWidget (scroll_bar);
9222 }
06a2c219 9223#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 9224 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 9225#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9226
9227 UNBLOCK_INPUT;
12ba150f 9228 return bar;
f451eb13
JB
9229}
9230
06a2c219 9231
12ba150f 9232/* Draw BAR's handle in the proper position.
06a2c219 9233
12ba150f
JB
9234 If the handle is already drawn from START to END, don't bother
9235 redrawing it, unless REBUILD is non-zero; in that case, always
9236 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 9237 events.)
12ba150f
JB
9238
9239 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
9240 fit inside its rectangle, but if the user is dragging the scroll
9241 bar handle, we want to let them drag it down all the way, so that
9242 the bar's top is as far down as it goes; otherwise, there's no way
9243 to move to the very end of the buffer. */
9244
5c187dee
GM
9245#ifndef USE_TOOLKIT_SCROLL_BARS
9246
f451eb13 9247static void
ab648270
JB
9248x_scroll_bar_set_handle (bar, start, end, rebuild)
9249 struct scroll_bar *bar;
f451eb13 9250 int start, end;
12ba150f 9251 int rebuild;
f451eb13 9252{
12ba150f 9253 int dragging = ! NILP (bar->dragging);
ab648270 9254 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9255 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9256 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
9257
9258 /* If the display is already accurate, do nothing. */
9259 if (! rebuild
9260 && start == XINT (bar->start)
9261 && end == XINT (bar->end))
9262 return;
9263
f451eb13
JB
9264 BLOCK_INPUT;
9265
9266 {
d9cdbb3d
RS
9267 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9268 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9269 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
9270
9271 /* Make sure the values are reasonable, and try to preserve
9272 the distance between start and end. */
12ba150f
JB
9273 {
9274 int length = end - start;
9275
9276 if (start < 0)
9277 start = 0;
9278 else if (start > top_range)
9279 start = top_range;
9280 end = start + length;
9281
9282 if (end < start)
9283 end = start;
9284 else if (end > top_range && ! dragging)
9285 end = top_range;
9286 }
f451eb13 9287
ab648270 9288 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
9289 XSETINT (bar->start, start);
9290 XSETINT (bar->end, end);
f451eb13 9291
12ba150f
JB
9292 /* Clip the end position, just for display. */
9293 if (end > top_range)
9294 end = top_range;
f451eb13 9295
ab648270 9296 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
9297 below top positions, to make sure the handle is always at least
9298 that many pixels tall. */
ab648270 9299 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 9300
12ba150f
JB
9301 /* Draw the empty space above the handle. Note that we can't clear
9302 zero-height areas; that means "clear to end of window." */
9303 if (0 < start)
c5e6e06b
GM
9304 x_clear_area (FRAME_X_DISPLAY (f), w,
9305 /* x, y, width, height, and exposures. */
9306 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9307 VERTICAL_SCROLL_BAR_TOP_BORDER,
9308 inside_width, start,
9309 False);
f451eb13 9310
06a2c219
GM
9311 /* Change to proper foreground color if one is specified. */
9312 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9313 XSetForeground (FRAME_X_DISPLAY (f), gc,
9314 f->output_data.x->scroll_bar_foreground_pixel);
9315
12ba150f 9316 /* Draw the handle itself. */
334208b7 9317 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 9318 /* x, y, width, height */
ab648270
JB
9319 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9320 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 9321 inside_width, end - start);
f451eb13 9322
06a2c219
GM
9323 /* Restore the foreground color of the GC if we changed it above. */
9324 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9325 XSetForeground (FRAME_X_DISPLAY (f), gc,
9326 f->output_data.x->foreground_pixel);
f451eb13 9327
12ba150f
JB
9328 /* Draw the empty space below the handle. Note that we can't
9329 clear zero-height areas; that means "clear to end of window." */
9330 if (end < inside_height)
c5e6e06b
GM
9331 x_clear_area (FRAME_X_DISPLAY (f), w,
9332 /* x, y, width, height, and exposures. */
9333 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9334 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9335 inside_width, inside_height - end,
9336 False);
f451eb13 9337
f451eb13
JB
9338 }
9339
f451eb13
JB
9340 UNBLOCK_INPUT;
9341}
9342
5c187dee 9343#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 9344
06a2c219
GM
9345/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9346 nil. */
58769bee 9347
12ba150f 9348static void
ab648270
JB
9349x_scroll_bar_remove (bar)
9350 struct scroll_bar *bar;
12ba150f 9351{
e83dc917 9352 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
9353 BLOCK_INPUT;
9354
eccc05db 9355#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
9356 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9357#else
9358 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9359#endif
06a2c219 9360
ab648270
JB
9361 /* Disassociate this scroll bar from its window. */
9362 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
9363
9364 UNBLOCK_INPUT;
9365}
9366
06a2c219 9367
12ba150f
JB
9368/* Set the handle of the vertical scroll bar for WINDOW to indicate
9369 that we are displaying PORTION characters out of a total of WHOLE
ab648270 9370 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 9371 create one. */
06a2c219 9372
12ba150f 9373static void
06a2c219
GM
9374XTset_vertical_scroll_bar (w, portion, whole, position)
9375 struct window *w;
f451eb13
JB
9376 int portion, whole, position;
9377{
06a2c219 9378 struct frame *f = XFRAME (w->frame);
ab648270 9379 struct scroll_bar *bar;
3c6ede7b 9380 int top, height, left, sb_left, width, sb_width;
06a2c219 9381 int window_x, window_y, window_width, window_height;
06a2c219 9382
3c6ede7b 9383 /* Get window dimensions. */
06a2c219 9384 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
9385 top = window_y;
9386 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9387 height = window_height;
06a2c219 9388
3c6ede7b 9389 /* Compute the left edge of the scroll bar area. */
06a2c219 9390 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
9391 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9392 else
9393 left = XFASTINT (w->left);
9394 left *= CANON_X_UNIT (f);
9395 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9396
9397 /* Compute the width of the scroll bar which might be less than
9398 the width of the area reserved for the scroll bar. */
9399 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9400 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 9401 else
3c6ede7b 9402 sb_width = width;
12ba150f 9403
3c6ede7b
GM
9404 /* Compute the left edge of the scroll bar. */
9405#ifdef USE_TOOLKIT_SCROLL_BARS
9406 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9407 sb_left = left + width - sb_width - (width - sb_width) / 2;
9408 else
9409 sb_left = left + (width - sb_width) / 2;
9410#else
9411 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9412 sb_left = left + width - sb_width;
9413 else
9414 sb_left = left;
9415#endif
9416
ab648270 9417 /* Does the scroll bar exist yet? */
06a2c219 9418 if (NILP (w->vertical_scroll_bar))
3c6ede7b 9419 {
7b49b9d2 9420 if (width > 0 && height > 0)
b547b6e8
GM
9421 {
9422 BLOCK_INPUT;
9423 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9424 left, top, width, height, False);
9425 UNBLOCK_INPUT;
9426 }
9427
3c6ede7b
GM
9428 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9429 }
f451eb13 9430 else
12ba150f
JB
9431 {
9432 /* It may just need to be moved and resized. */
06a2c219
GM
9433 unsigned int mask = 0;
9434
9435 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9436
9437 BLOCK_INPUT;
9438
3c6ede7b 9439 if (sb_left != XINT (bar->left))
06a2c219 9440 mask |= CWX;
3c6ede7b 9441 if (top != XINT (bar->top))
06a2c219 9442 mask |= CWY;
3c6ede7b 9443 if (sb_width != XINT (bar->width))
06a2c219 9444 mask |= CWWidth;
3c6ede7b 9445 if (height != XINT (bar->height))
06a2c219
GM
9446 mask |= CWHeight;
9447
9448#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
9449
9450 /* Since toolkit scroll bars are smaller than the space reserved
9451 for them on the frame, we have to clear "under" them. */
7b49b9d2 9452 if (width > 0 && height > 0)
f964b4d7
GM
9453 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9454 left, top, width, height, False);
06a2c219
GM
9455
9456 /* Move/size the scroll bar widget. */
9457 if (mask)
e83dc917 9458 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
9459 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9460 top,
9461 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 9462 max (height, 1), 0);
06a2c219
GM
9463
9464#else /* not USE_TOOLKIT_SCROLL_BARS */
9465
357e7376
GM
9466 /* Clear areas not covered by the scroll bar because of
9467 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
9468 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9469 {
c5e6e06b
GM
9470 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9471 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9472 height, False);
9473 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9474 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9475 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9476 height, False);
e1f6572f 9477 }
357e7376
GM
9478
9479 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 9480 wide as the area reserved for it. This makes sure a
357e7376
GM
9481 previous mode line display is cleared after C-x 2 C-x 1, for
9482 example. */
9483 {
9484 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9485 int rest = area_width - sb_width;
38d2af0c
GM
9486 if (rest > 0 && height > 0)
9487 {
9488 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9489 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9490 left + area_width - rest, top,
9491 rest, height, False);
9492 else
9493 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9494 left, top, rest, height, False);
9495 }
357e7376 9496 }
06a2c219
GM
9497
9498 /* Move/size the scroll bar window. */
9499 if (mask)
9500 {
9501 XWindowChanges wc;
9502
3c6ede7b
GM
9503 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9504 wc.y = top;
9505 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9506 wc.height = height;
06a2c219
GM
9507 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9508 mask, &wc);
9509 }
9510
9511#endif /* not USE_TOOLKIT_SCROLL_BARS */
9512
9513 /* Remember new settings. */
3c6ede7b
GM
9514 XSETINT (bar->left, sb_left);
9515 XSETINT (bar->top, top);
9516 XSETINT (bar->width, sb_width);
9517 XSETINT (bar->height, height);
06a2c219
GM
9518
9519 UNBLOCK_INPUT;
12ba150f 9520 }
f451eb13 9521
eccc05db 9522#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9523 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9524#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 9525 /* Set the scroll bar's current state, unless we're currently being
f451eb13 9526 dragged. */
12ba150f 9527 if (NILP (bar->dragging))
f451eb13 9528 {
92857db0 9529 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 9530
12ba150f 9531 if (whole == 0)
ab648270 9532 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
9533 else
9534 {
43f868f5
JB
9535 int start = ((double) position * top_range) / whole;
9536 int end = ((double) (position + portion) * top_range) / whole;
ab648270 9537 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 9538 }
f451eb13 9539 }
06a2c219 9540#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 9541
06a2c219 9542 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
9543}
9544
12ba150f 9545
f451eb13 9546/* The following three hooks are used when we're doing a thorough
ab648270 9547 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 9548 are going to be deleted, because keeping track of when windows go
12ba150f
JB
9549 away is a real pain - "Can you say set-window-configuration, boys
9550 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 9551 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 9552 from the fiery pit when we actually redisplay its window. */
f451eb13 9553
ab648270
JB
9554/* Arrange for all scroll bars on FRAME to be removed at the next call
9555 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
9556 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9557
58769bee 9558static void
ab648270 9559XTcondemn_scroll_bars (frame)
f451eb13
JB
9560 FRAME_PTR frame;
9561{
f9e24cb9
RS
9562 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9563 while (! NILP (FRAME_SCROLL_BARS (frame)))
9564 {
9565 Lisp_Object bar;
9566 bar = FRAME_SCROLL_BARS (frame);
9567 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9568 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9569 XSCROLL_BAR (bar)->prev = Qnil;
9570 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9571 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9572 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9573 }
f451eb13
JB
9574}
9575
fa2dfc30 9576
06a2c219 9577/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 9578 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 9579
f451eb13 9580static void
ab648270 9581XTredeem_scroll_bar (window)
12ba150f 9582 struct window *window;
f451eb13 9583{
ab648270 9584 struct scroll_bar *bar;
fa2dfc30 9585 struct frame *f;
12ba150f 9586
ab648270
JB
9587 /* We can't redeem this window's scroll bar if it doesn't have one. */
9588 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
9589 abort ();
9590
ab648270 9591 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
9592
9593 /* Unlink it from the condemned list. */
fa2dfc30
GM
9594 f = XFRAME (WINDOW_FRAME (window));
9595 if (NILP (bar->prev))
9596 {
9597 /* If the prev pointer is nil, it must be the first in one of
9598 the lists. */
9599 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9600 /* It's not condemned. Everything's fine. */
9601 return;
9602 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9603 window->vertical_scroll_bar))
9604 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9605 else
9606 /* If its prev pointer is nil, it must be at the front of
9607 one or the other! */
9608 abort ();
9609 }
9610 else
9611 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 9612
fa2dfc30
GM
9613 if (! NILP (bar->next))
9614 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 9615
fa2dfc30
GM
9616 bar->next = FRAME_SCROLL_BARS (f);
9617 bar->prev = Qnil;
9618 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9619 if (! NILP (bar->next))
9620 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
9621}
9622
ab648270
JB
9623/* Remove all scroll bars on FRAME that haven't been saved since the
9624 last call to `*condemn_scroll_bars_hook'. */
06a2c219 9625
f451eb13 9626static void
ab648270 9627XTjudge_scroll_bars (f)
12ba150f 9628 FRAME_PTR f;
f451eb13 9629{
12ba150f 9630 Lisp_Object bar, next;
f451eb13 9631
ab648270 9632 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
9633
9634 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
9635 more events on the hapless scroll bars. */
9636 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
9637
9638 for (; ! NILP (bar); bar = next)
f451eb13 9639 {
ab648270 9640 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 9641
ab648270 9642 x_scroll_bar_remove (b);
12ba150f
JB
9643
9644 next = b->next;
9645 b->next = b->prev = Qnil;
f451eb13 9646 }
12ba150f 9647
ab648270 9648 /* Now there should be no references to the condemned scroll bars,
12ba150f 9649 and they should get garbage-collected. */
f451eb13
JB
9650}
9651
9652
06a2c219
GM
9653/* Handle an Expose or GraphicsExpose event on a scroll bar. This
9654 is a no-op when using toolkit scroll bars.
ab648270
JB
9655
9656 This may be called from a signal handler, so we have to ignore GC
9657 mark bits. */
06a2c219 9658
f451eb13 9659static void
ab648270
JB
9660x_scroll_bar_expose (bar, event)
9661 struct scroll_bar *bar;
f451eb13
JB
9662 XEvent *event;
9663{
06a2c219
GM
9664#ifndef USE_TOOLKIT_SCROLL_BARS
9665
ab648270 9666 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9667 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9668 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 9669 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 9670
f451eb13
JB
9671 BLOCK_INPUT;
9672
ab648270 9673 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 9674
06a2c219 9675 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 9676 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
9677
9678 /* x, y, width, height */
d9cdbb3d 9679 0, 0,
3cbd2e0b 9680 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
9681 XINT (bar->height) - 1);
9682
f451eb13 9683 UNBLOCK_INPUT;
06a2c219
GM
9684
9685#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9686}
9687
ab648270 9688/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 9689 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
9690
9691 This may be called from a signal handler, so we have to ignore GC
9692 mark bits. */
06a2c219 9693
5c187dee
GM
9694#ifndef USE_TOOLKIT_SCROLL_BARS
9695
f451eb13 9696static void
ab648270
JB
9697x_scroll_bar_handle_click (bar, event, emacs_event)
9698 struct scroll_bar *bar;
f451eb13
JB
9699 XEvent *event;
9700 struct input_event *emacs_event;
9701{
0299d313 9702 if (! GC_WINDOWP (bar->window))
12ba150f
JB
9703 abort ();
9704
3b8f9651 9705 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 9706 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
9707 emacs_event->modifiers
9708 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9709 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9710 event->xbutton.state)
9711 | (event->type == ButtonRelease
9712 ? up_modifier
9713 : down_modifier));
12ba150f 9714 emacs_event->frame_or_window = bar->window;
0f8aabe9 9715 emacs_event->arg = Qnil;
f451eb13 9716 emacs_event->timestamp = event->xbutton.time;
12ba150f 9717 {
06a2c219 9718#if 0
d9cdbb3d 9719 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 9720 int internal_height
d9cdbb3d 9721 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9722#endif
0299d313 9723 int top_range
d9cdbb3d 9724 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 9725 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
9726
9727 if (y < 0) y = 0;
9728 if (y > top_range) y = top_range;
9729
9730 if (y < XINT (bar->start))
ab648270
JB
9731 emacs_event->part = scroll_bar_above_handle;
9732 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9733 emacs_event->part = scroll_bar_handle;
12ba150f 9734 else
ab648270 9735 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
9736
9737 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
9738 they want to drag it. Lisp code needs to be able to decide
9739 whether or not we're dragging. */
929787e1 9740#if 0
12ba150f
JB
9741 /* If the user has just clicked on the handle, record where they're
9742 holding it. */
9743 if (event->type == ButtonPress
ab648270 9744 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9745 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9746#endif
12ba150f
JB
9747
9748 /* If the user has released the handle, set it to its final position. */
9749 if (event->type == ButtonRelease
9750 && ! NILP (bar->dragging))
9751 {
9752 int new_start = y - XINT (bar->dragging);
9753 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9754
ab648270 9755 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9756 bar->dragging = Qnil;
9757 }
f451eb13 9758
5116f055
JB
9759 /* Same deal here as the other #if 0. */
9760#if 0
58769bee 9761 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9762 the handle. */
ab648270 9763 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9764 emacs_event->x = bar->start;
9765 else
e0c1aef2 9766 XSETINT (emacs_event->x, y);
5116f055 9767#else
e0c1aef2 9768 XSETINT (emacs_event->x, y);
5116f055 9769#endif
f451eb13 9770
e0c1aef2 9771 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9772 }
9773}
f451eb13 9774
ab648270
JB
9775/* Handle some mouse motion while someone is dragging the scroll bar.
9776
9777 This may be called from a signal handler, so we have to ignore GC
9778 mark bits. */
06a2c219 9779
f451eb13 9780static void
ab648270
JB
9781x_scroll_bar_note_movement (bar, event)
9782 struct scroll_bar *bar;
f451eb13
JB
9783 XEvent *event;
9784{
39d8bb4d
KH
9785 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9786
f451eb13
JB
9787 last_mouse_movement_time = event->xmotion.time;
9788
39d8bb4d 9789 f->mouse_moved = 1;
e0c1aef2 9790 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9791
9792 /* If we're dragging the bar, display it. */
ab648270 9793 if (! GC_NILP (bar->dragging))
f451eb13
JB
9794 {
9795 /* Where should the handle be now? */
12ba150f 9796 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9797
12ba150f 9798 if (new_start != XINT (bar->start))
f451eb13 9799 {
12ba150f 9800 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9801
ab648270 9802 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9803 }
9804 }
f451eb13
JB
9805}
9806
5c187dee
GM
9807#endif /* !USE_TOOLKIT_SCROLL_BARS */
9808
12ba150f 9809/* Return information to the user about the current position of the mouse
ab648270 9810 on the scroll bar. */
06a2c219 9811
12ba150f 9812static void
334208b7
RS
9813x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9814 FRAME_PTR *fp;
12ba150f 9815 Lisp_Object *bar_window;
ab648270 9816 enum scroll_bar_part *part;
12ba150f
JB
9817 Lisp_Object *x, *y;
9818 unsigned long *time;
9819{
ab648270 9820 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9821 Window w = SCROLL_BAR_X_WINDOW (bar);
9822 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9823 int win_x, win_y;
559cb2fb
JB
9824 Window dummy_window;
9825 int dummy_coord;
9826 unsigned int dummy_mask;
12ba150f 9827
cf7cb199
JB
9828 BLOCK_INPUT;
9829
ab648270 9830 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9831 report that. */
334208b7 9832 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9833
559cb2fb
JB
9834 /* Root, child, root x and root y. */
9835 &dummy_window, &dummy_window,
9836 &dummy_coord, &dummy_coord,
12ba150f 9837
559cb2fb
JB
9838 /* Position relative to scroll bar. */
9839 &win_x, &win_y,
12ba150f 9840
559cb2fb
JB
9841 /* Mouse buttons and modifier keys. */
9842 &dummy_mask))
7a13e894 9843 ;
559cb2fb
JB
9844 else
9845 {
06a2c219 9846#if 0
559cb2fb 9847 int inside_height
d9cdbb3d 9848 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9849#endif
559cb2fb 9850 int top_range
d9cdbb3d 9851 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9852
9853 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9854
9855 if (! NILP (bar->dragging))
9856 win_y -= XINT (bar->dragging);
9857
9858 if (win_y < 0)
9859 win_y = 0;
9860 if (win_y > top_range)
9861 win_y = top_range;
9862
334208b7 9863 *fp = f;
7a13e894 9864 *bar_window = bar->window;
559cb2fb
JB
9865
9866 if (! NILP (bar->dragging))
9867 *part = scroll_bar_handle;
9868 else if (win_y < XINT (bar->start))
9869 *part = scroll_bar_above_handle;
9870 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9871 *part = scroll_bar_handle;
9872 else
9873 *part = scroll_bar_below_handle;
12ba150f 9874
e0c1aef2
KH
9875 XSETINT (*x, win_y);
9876 XSETINT (*y, top_range);
12ba150f 9877
39d8bb4d 9878 f->mouse_moved = 0;
559cb2fb
JB
9879 last_mouse_scroll_bar = Qnil;
9880 }
12ba150f 9881
559cb2fb 9882 *time = last_mouse_movement_time;
cf7cb199 9883
cf7cb199 9884 UNBLOCK_INPUT;
12ba150f
JB
9885}
9886
f451eb13 9887
dbc4e1c1 9888/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9889 background colors, and the scroll bars may need to be redrawn.
9890 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9891 redraw them. */
9892
dfcf069d 9893void
ab648270 9894x_scroll_bar_clear (f)
dbc4e1c1
JB
9895 FRAME_PTR f;
9896{
06a2c219 9897#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9898 Lisp_Object bar;
9899
b80c363e
RS
9900 /* We can have scroll bars even if this is 0,
9901 if we just turned off scroll bar mode.
9902 But in that case we should not clear them. */
9903 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9904 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9905 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9906 XClearArea (FRAME_X_DISPLAY (f),
9907 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9908 0, 0, 0, 0, True);
06a2c219 9909#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9910}
9911
06a2c219 9912/* This processes Expose events from the menu-bar specific X event
19126e11 9913 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9914 when handling menu-bar or pop-up items. */
3afe33e7 9915
06a2c219 9916int
3afe33e7
RS
9917process_expose_from_menu (event)
9918 XEvent event;
9919{
9920 FRAME_PTR f;
19126e11 9921 struct x_display_info *dpyinfo;
06a2c219 9922 int frame_exposed_p = 0;
3afe33e7 9923
f94397b5
KH
9924 BLOCK_INPUT;
9925
19126e11
KH
9926 dpyinfo = x_display_info_for_display (event.xexpose.display);
9927 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9928 if (f)
9929 {
9930 if (f->async_visible == 0)
9931 {
9932 f->async_visible = 1;
9933 f->async_iconified = 0;
06c488fd 9934 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9935 SET_FRAME_GARBAGED (f);
9936 }
9937 else
9938 {
06a2c219
GM
9939 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9940 event.xexpose.x, event.xexpose.y,
9941 event.xexpose.width, event.xexpose.height);
9942 frame_exposed_p = 1;
3afe33e7
RS
9943 }
9944 }
9945 else
9946 {
9947 struct scroll_bar *bar
9948 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9949
3afe33e7
RS
9950 if (bar)
9951 x_scroll_bar_expose (bar, &event);
9952 }
f94397b5
KH
9953
9954 UNBLOCK_INPUT;
06a2c219 9955 return frame_exposed_p;
3afe33e7 9956}
09756a85
RS
9957\f
9958/* Define a queue to save up SelectionRequest events for later handling. */
9959
9960struct selection_event_queue
9961 {
9962 XEvent event;
9963 struct selection_event_queue *next;
9964 };
9965
9966static struct selection_event_queue *queue;
9967
9968/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9969
09756a85
RS
9970static int x_queue_selection_requests;
9971
9972/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9973
09756a85 9974static void
334208b7
RS
9975x_queue_event (f, event)
9976 FRAME_PTR f;
09756a85
RS
9977 XEvent *event;
9978{
9979 struct selection_event_queue *queue_tmp
06a2c219 9980 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9981
58769bee 9982 if (queue_tmp != NULL)
09756a85
RS
9983 {
9984 queue_tmp->event = *event;
9985 queue_tmp->next = queue;
9986 queue = queue_tmp;
9987 }
9988}
9989
9990/* Take all the queued events and put them back
9991 so that they get processed afresh. */
9992
9993static void
db3906fd
RS
9994x_unqueue_events (display)
9995 Display *display;
09756a85 9996{
58769bee 9997 while (queue != NULL)
09756a85
RS
9998 {
9999 struct selection_event_queue *queue_tmp = queue;
db3906fd 10000 XPutBackEvent (display, &queue_tmp->event);
09756a85 10001 queue = queue_tmp->next;
06a2c219 10002 xfree ((char *)queue_tmp);
09756a85
RS
10003 }
10004}
10005
10006/* Start queuing SelectionRequest events. */
10007
10008void
db3906fd
RS
10009x_start_queuing_selection_requests (display)
10010 Display *display;
09756a85
RS
10011{
10012 x_queue_selection_requests++;
10013}
10014
10015/* Stop queuing SelectionRequest events. */
10016
10017void
db3906fd
RS
10018x_stop_queuing_selection_requests (display)
10019 Display *display;
09756a85
RS
10020{
10021 x_queue_selection_requests--;
db3906fd 10022 x_unqueue_events (display);
09756a85 10023}
f451eb13
JB
10024\f
10025/* The main X event-reading loop - XTread_socket. */
dc6f92b8 10026
042c33d3 10027#if 0
06a2c219 10028/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
10029 but we have to put it out here, since static variables within functions
10030 sometimes don't work. */
06a2c219 10031
dc6f92b8 10032static Time enter_timestamp;
042c33d3 10033#endif
dc6f92b8 10034
11edeb03 10035/* This holds the state XLookupString needs to implement dead keys
58769bee 10036 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
10037 says that a portable program can't use this, but Stephen Gildea assures
10038 me that letting the compiler initialize it to zeros will work okay.
10039
10040 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 10041 given for enter_timestamp, above. */
06a2c219 10042
11edeb03
JB
10043static XComposeStatus compose_status;
10044
10e6549c
RS
10045/* Record the last 100 characters stored
10046 to help debug the loss-of-chars-during-GC problem. */
06a2c219 10047
2224b905
RS
10048static int temp_index;
10049static short temp_buffer[100];
10e6549c 10050
7a13e894
RS
10051/* Set this to nonzero to fake an "X I/O error"
10052 on a particular display. */
06a2c219 10053
7a13e894
RS
10054struct x_display_info *XTread_socket_fake_io_error;
10055
2224b905
RS
10056/* When we find no input here, we occasionally do a no-op command
10057 to verify that the X server is still running and we can still talk with it.
10058 We try all the open displays, one by one.
10059 This variable is used for cycling thru the displays. */
06a2c219 10060
2224b905
RS
10061static struct x_display_info *next_noop_dpyinfo;
10062
06a2c219
GM
10063#define SET_SAVED_MENU_EVENT(size) \
10064 do \
10065 { \
10066 if (f->output_data.x->saved_menu_event == 0) \
10067 f->output_data.x->saved_menu_event \
10068 = (XEvent *) xmalloc (sizeof (XEvent)); \
10069 bcopy (&event, f->output_data.x->saved_menu_event, size); \
10070 if (numchars >= 1) \
10071 { \
3b8f9651 10072 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
06a2c219 10073 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 10074 bufp->arg = Qnil; \
06a2c219
GM
10075 bufp++; \
10076 count++; \
10077 numchars--; \
10078 } \
10079 } \
10080 while (0)
10081
8805890a 10082#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 10083#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 10084
dc6f92b8
JB
10085/* Read events coming from the X server.
10086 This routine is called by the SIGIO handler.
10087 We return as soon as there are no more events to be read.
10088
10089 Events representing keys are stored in buffer BUFP,
10090 which can hold up to NUMCHARS characters.
10091 We return the number of characters stored into the buffer,
10092 thus pretending to be `read'.
10093
dc6f92b8
JB
10094 EXPECTED is nonzero if the caller knows input is available. */
10095
6f2a7a68 10096static int
f66868ba 10097XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 10098 register int sd;
8805890a
KH
10099 /* register */ struct input_event *bufp;
10100 /* register */ int numchars;
dc6f92b8
JB
10101 int expected;
10102{
10103 int count = 0;
10104 int nbytes = 0;
dc6f92b8 10105 XEvent event;
f676886a 10106 struct frame *f;
66f55a9d 10107 int event_found = 0;
334208b7 10108 struct x_display_info *dpyinfo;
379b5ac0 10109 struct coding_system coding;
dc6f92b8 10110
9ac0d9e0 10111 if (interrupt_input_blocked)
dc6f92b8 10112 {
9ac0d9e0 10113 interrupt_input_pending = 1;
dc6f92b8
JB
10114 return -1;
10115 }
10116
9ac0d9e0 10117 interrupt_input_pending = 0;
dc6f92b8 10118 BLOCK_INPUT;
c0a04927
RS
10119
10120 /* So people can tell when we have read the available input. */
10121 input_signal_count++;
10122
dc6f92b8 10123 if (numchars <= 0)
06a2c219 10124 abort (); /* Don't think this happens. */
dc6f92b8 10125
bde5503b
GM
10126 ++handling_signal;
10127
7a13e894
RS
10128 /* Find the display we are supposed to read input for.
10129 It's the one communicating on descriptor SD. */
10130 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
10131 {
10132#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 10133#ifdef FIOSNBIO
7a13e894
RS
10134 /* If available, Xlib uses FIOSNBIO to make the socket
10135 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 10136 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 10137 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 10138 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 10139#endif /* ! defined (FIOSNBIO) */
7a13e894 10140#endif
dc6f92b8 10141
7a13e894
RS
10142#if 0 /* This code can't be made to work, with multiple displays,
10143 and appears not to be used on any system any more.
10144 Also keyboard.c doesn't turn O_NDELAY on and off
10145 for X connections. */
dc6f92b8
JB
10146#ifndef SIGIO
10147#ifndef HAVE_SELECT
7a13e894
RS
10148 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10149 {
10150 extern int read_alarm_should_throw;
10151 read_alarm_should_throw = 1;
10152 XPeekEvent (dpyinfo->display, &event);
10153 read_alarm_should_throw = 0;
10154 }
c118dd06
JB
10155#endif /* HAVE_SELECT */
10156#endif /* SIGIO */
7a13e894 10157#endif
dc6f92b8 10158
7a13e894
RS
10159 /* For debugging, this gives a way to fake an I/O error. */
10160 if (dpyinfo == XTread_socket_fake_io_error)
10161 {
10162 XTread_socket_fake_io_error = 0;
10163 x_io_error_quitter (dpyinfo->display);
10164 }
dc6f92b8 10165
5f30b957
JD
10166#ifdef HAVE_X_SM
10167 BLOCK_INPUT;
10168 count += x_session_check_input (bufp, &numchars);
10169 UNBLOCK_INPUT;
10170#endif
10171
06a2c219 10172 while (XPending (dpyinfo->display))
dc6f92b8 10173 {
7a13e894 10174 XNextEvent (dpyinfo->display, &event);
06a2c219 10175
531483fb 10176#ifdef HAVE_X_I18N
d1bc4182 10177 {
f2be1146
GM
10178 /* Filter events for the current X input method.
10179 XFilterEvent returns non-zero if the input method has
10180 consumed the event. We pass the frame's X window to
10181 XFilterEvent because that's the one for which the IC
10182 was created. */
f5d11644
GM
10183 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10184 event.xclient.window);
10185 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
10186 break;
10187 }
0cd6403b 10188#endif
7a13e894
RS
10189 event_found = 1;
10190
10191 switch (event.type)
10192 {
10193 case ClientMessage:
c047688c 10194 {
7a13e894
RS
10195 if (event.xclient.message_type
10196 == dpyinfo->Xatom_wm_protocols
10197 && event.xclient.format == 32)
c047688c 10198 {
7a13e894
RS
10199 if (event.xclient.data.l[0]
10200 == dpyinfo->Xatom_wm_take_focus)
c047688c 10201 {
8c1a6a84
RS
10202 /* Use x_any_window_to_frame because this
10203 could be the shell widget window
10204 if the frame has no title bar. */
10205 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
10206#ifdef HAVE_X_I18N
10207 /* Not quite sure this is needed -pd */
8c1a6a84 10208 if (f && FRAME_XIC (f))
6c183ba5
RS
10209 XSetICFocus (FRAME_XIC (f));
10210#endif
f1da8f06
GM
10211#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10212 instructs the WM to set the input focus automatically for
10213 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10214 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10215 it has set the focus. So, XSetInputFocus below is not
10216 needed.
10217
10218 The call to XSetInputFocus below has also caused trouble. In
10219 cases where the XSetInputFocus done by the WM and the one
10220 below are temporally close (on a fast machine), the call
10221 below can generate additional FocusIn events which confuse
10222 Emacs. */
10223
bf7253f4
RS
10224 /* Since we set WM_TAKE_FOCUS, we must call
10225 XSetInputFocus explicitly. But not if f is null,
10226 since that might be an event for a deleted frame. */
7a13e894 10227 if (f)
bf7253f4
RS
10228 {
10229 Display *d = event.xclient.display;
10230 /* Catch and ignore errors, in case window has been
10231 iconified by a window manager such as GWM. */
10232 int count = x_catch_errors (d);
10233 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
10234 /* The ICCCM says this is
10235 the only valid choice. */
10236 RevertToParent,
bf7253f4
RS
10237 event.xclient.data.l[1]);
10238 /* This is needed to detect the error
10239 if there is an error. */
10240 XSync (d, False);
10241 x_uncatch_errors (d, count);
10242 }
7a13e894 10243 /* Not certain about handling scroll bars here */
f1da8f06 10244#endif /* 0 */
c047688c 10245 }
7a13e894
RS
10246 else if (event.xclient.data.l[0]
10247 == dpyinfo->Xatom_wm_save_yourself)
10248 {
10249 /* Save state modify the WM_COMMAND property to
06a2c219 10250 something which can reinstate us. This notifies
7a13e894
RS
10251 the session manager, who's looking for such a
10252 PropertyNotify. Can restart processing when
06a2c219 10253 a keyboard or mouse event arrives. */
5f30b957
JD
10254 /* If we have a session manager, don't set this.
10255 KDE will then start two Emacsen, one for the
10256 session manager and one for this. */
10257 if (numchars > 0
10258#ifdef HAVE_X_SM
10259 && ! x_session_have_connection ()
10260#endif
10261 )
7a13e894 10262 {
19126e11
KH
10263 f = x_top_window_to_frame (dpyinfo,
10264 event.xclient.window);
7a13e894
RS
10265 /* This is just so we only give real data once
10266 for a single Emacs process. */
b86bd3dd 10267 if (f == SELECTED_FRAME ())
7a13e894
RS
10268 XSetCommand (FRAME_X_DISPLAY (f),
10269 event.xclient.window,
10270 initial_argv, initial_argc);
f000f5c5 10271 else if (f)
7a13e894
RS
10272 XSetCommand (FRAME_X_DISPLAY (f),
10273 event.xclient.window,
10274 0, 0);
10275 }
10276 }
10277 else if (event.xclient.data.l[0]
10278 == dpyinfo->Xatom_wm_delete_window)
1fb20991 10279 {
19126e11
KH
10280 struct frame *f
10281 = x_any_window_to_frame (dpyinfo,
10282 event.xclient.window);
1fb20991 10283
7a13e894
RS
10284 if (f)
10285 {
10286 if (numchars == 0)
10287 abort ();
1fb20991 10288
3b8f9651 10289 bufp->kind = DELETE_WINDOW_EVENT;
7a13e894 10290 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10291 bufp->arg = Qnil;
7a13e894
RS
10292 bufp++;
10293
10294 count += 1;
10295 numchars -= 1;
10296 }
1fb20991 10297 }
c047688c 10298 }
7a13e894
RS
10299 else if (event.xclient.message_type
10300 == dpyinfo->Xatom_wm_configure_denied)
10301 {
10302 }
10303 else if (event.xclient.message_type
10304 == dpyinfo->Xatom_wm_window_moved)
10305 {
10306 int new_x, new_y;
19126e11
KH
10307 struct frame *f
10308 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 10309
7a13e894
RS
10310 new_x = event.xclient.data.s[0];
10311 new_y = event.xclient.data.s[1];
1fb20991 10312
7a13e894
RS
10313 if (f)
10314 {
7556890b
RS
10315 f->output_data.x->left_pos = new_x;
10316 f->output_data.x->top_pos = new_y;
7a13e894 10317 }
1fb20991 10318 }
0fdff6bb 10319#ifdef HACK_EDITRES
7a13e894
RS
10320 else if (event.xclient.message_type
10321 == dpyinfo->Xatom_editres)
10322 {
19126e11
KH
10323 struct frame *f
10324 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 10325 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 10326 &event, NULL);
7a13e894 10327 }
0fdff6bb 10328#endif /* HACK_EDITRES */
06a2c219
GM
10329 else if ((event.xclient.message_type
10330 == dpyinfo->Xatom_DONE)
10331 || (event.xclient.message_type
10332 == dpyinfo->Xatom_PAGE))
10333 {
10334 /* Ghostview job completed. Kill it. We could
10335 reply with "Next" if we received "Page", but we
10336 currently never do because we are interested in
10337 images, only, which should have 1 page. */
06a2c219
GM
10338 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10339 struct frame *f
10340 = x_window_to_frame (dpyinfo, event.xclient.window);
10341 x_kill_gs_process (pixmap, f);
10342 expose_frame (f, 0, 0, 0, 0);
10343 }
10344#ifdef USE_TOOLKIT_SCROLL_BARS
10345 /* Scroll bar callbacks send a ClientMessage from which
10346 we construct an input_event. */
10347 else if (event.xclient.message_type
10348 == dpyinfo->Xatom_Scrollbar)
10349 {
10350 x_scroll_bar_to_input_event (&event, bufp);
10351 ++bufp, ++count, --numchars;
10352 goto out;
10353 }
10354#endif /* USE_TOOLKIT_SCROLL_BARS */
10355 else
10356 goto OTHER;
7a13e894
RS
10357 }
10358 break;
dc6f92b8 10359
7a13e894 10360 case SelectionNotify:
3afe33e7 10361#ifdef USE_X_TOOLKIT
19126e11 10362 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 10363 goto OTHER;
3afe33e7 10364#endif /* not USE_X_TOOLKIT */
dfcf069d 10365 x_handle_selection_notify (&event.xselection);
7a13e894 10366 break;
d56a553a 10367
06a2c219 10368 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 10369#ifdef USE_X_TOOLKIT
19126e11 10370 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 10371 goto OTHER;
3afe33e7 10372#endif /* USE_X_TOOLKIT */
7a13e894
RS
10373 {
10374 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 10375
7a13e894
RS
10376 if (numchars == 0)
10377 abort ();
d56a553a 10378
3b8f9651 10379 bufp->kind = SELECTION_CLEAR_EVENT;
7a13e894
RS
10380 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10381 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10382 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10383 bufp->frame_or_window = Qnil;
0f8aabe9 10384 bufp->arg = Qnil;
7a13e894 10385 bufp++;
d56a553a 10386
7a13e894
RS
10387 count += 1;
10388 numchars -= 1;
10389 }
10390 break;
dc6f92b8 10391
06a2c219 10392 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 10393#ifdef USE_X_TOOLKIT
19126e11 10394 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 10395 goto OTHER;
3afe33e7 10396#endif /* USE_X_TOOLKIT */
7a13e894 10397 if (x_queue_selection_requests)
19126e11 10398 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
10399 &event);
10400 else
10401 {
1d2b2268
GM
10402 XSelectionRequestEvent *eventp
10403 = (XSelectionRequestEvent *) &event;
dc6f92b8 10404
7a13e894
RS
10405 if (numchars == 0)
10406 abort ();
10407
3b8f9651 10408 bufp->kind = SELECTION_REQUEST_EVENT;
7a13e894
RS
10409 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10410 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10411 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10412 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10413 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10414 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 10415 bufp->frame_or_window = Qnil;
0f8aabe9 10416 bufp->arg = Qnil;
7a13e894
RS
10417 bufp++;
10418
10419 count += 1;
10420 numchars -= 1;
10421 }
10422 break;
10423
10424 case PropertyNotify:
1d2b2268
GM
10425#if 0 /* This is plain wrong. In the case that we are waiting for a
10426 PropertyNotify used as an ACK in incremental selection
10427 transfer, the property will be on the receiver's window. */
10428#if defined USE_X_TOOLKIT
19126e11 10429 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 10430 goto OTHER;
1d2b2268
GM
10431#endif
10432#endif
dfcf069d 10433 x_handle_property_notify (&event.xproperty);
1d2b2268 10434 goto OTHER;
dc6f92b8 10435
7a13e894 10436 case ReparentNotify:
19126e11 10437 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
10438 if (f)
10439 {
10440 int x, y;
7556890b 10441 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 10442 x_real_positions (f, &x, &y);
7556890b
RS
10443 f->output_data.x->left_pos = x;
10444 f->output_data.x->top_pos = y;
7a13e894
RS
10445 }
10446 break;
3bd330d4 10447
7a13e894 10448 case Expose:
19126e11 10449 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 10450 if (f)
dc6f92b8 10451 {
d0fa3e56
EZ
10452 x_check_fullscreen (f);
10453
7a13e894
RS
10454 if (f->async_visible == 0)
10455 {
10456 f->async_visible = 1;
10457 f->async_iconified = 0;
06c488fd 10458 f->output_data.x->has_been_visible = 1;
7a13e894
RS
10459 SET_FRAME_GARBAGED (f);
10460 }
10461 else
06a2c219
GM
10462 expose_frame (x_window_to_frame (dpyinfo,
10463 event.xexpose.window),
10464 event.xexpose.x, event.xexpose.y,
10465 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
10466 }
10467 else
7a13e894 10468 {
12949a7f
EZ
10469#ifndef USE_TOOLKIT_SCROLL_BARS
10470 struct scroll_bar *bar;
10471#endif
01f67d2c 10472#if defined USE_LUCID
c95fc5f1
GM
10473 /* Submenus of the Lucid menu bar aren't widgets
10474 themselves, so there's no way to dispatch events
10475 to them. Recognize this case separately. */
10476 {
10477 Widget widget
10478 = x_window_to_menu_bar (event.xexpose.window);
10479 if (widget)
10480 xlwmenu_redisplay (widget);
10481 }
01f67d2c
PJ
10482#endif /* USE_LUCID */
10483
06a2c219
GM
10484#ifdef USE_TOOLKIT_SCROLL_BARS
10485 /* Dispatch event to the widget. */
10486 goto OTHER;
10487#else /* not USE_TOOLKIT_SCROLL_BARS */
12949a7f 10488 bar = x_window_to_scroll_bar (event.xexpose.window);
58769bee 10489
7a13e894
RS
10490 if (bar)
10491 x_scroll_bar_expose (bar, &event);
3afe33e7 10492#ifdef USE_X_TOOLKIT
7a13e894
RS
10493 else
10494 goto OTHER;
3afe33e7 10495#endif /* USE_X_TOOLKIT */
06a2c219 10496#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10497 }
10498 break;
dc6f92b8 10499
7a13e894 10500 case GraphicsExpose: /* This occurs when an XCopyArea's
d624284c
PJ
10501 source area was obscured or not
10502 available. */
19126e11 10503 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
10504 if (f)
10505 {
06a2c219
GM
10506 expose_frame (f,
10507 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10508 event.xgraphicsexpose.width,
10509 event.xgraphicsexpose.height);
7a13e894 10510 }
3afe33e7 10511#ifdef USE_X_TOOLKIT
7a13e894
RS
10512 else
10513 goto OTHER;
3afe33e7 10514#endif /* USE_X_TOOLKIT */
7a13e894 10515 break;
dc6f92b8 10516
7a13e894 10517 case NoExpose: /* This occurs when an XCopyArea's
06a2c219 10518 source area was completely
d624284c 10519 available. */
7a13e894 10520 break;
dc6f92b8 10521
7a13e894 10522 case UnmapNotify:
06a2c219
GM
10523 /* Redo the mouse-highlight after the tooltip has gone. */
10524 if (event.xmap.window == tip_window)
10525 {
10526 tip_window = 0;
10527 redo_mouse_highlight ();
10528 }
10529
91ea2a7a 10530 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894 10531 if (f) /* F may no longer exist if
d624284c 10532 the frame was deleted. */
7a13e894
RS
10533 {
10534 /* While a frame is unmapped, display generation is
10535 disabled; you don't want to spend time updating a
10536 display that won't ever be seen. */
10537 f->async_visible = 0;
10538 /* We can't distinguish, from the event, whether the window
10539 has become iconified or invisible. So assume, if it
10540 was previously visible, than now it is iconified.
1aa6072f
RS
10541 But x_make_frame_invisible clears both
10542 the visible flag and the iconified flag;
10543 and that way, we know the window is not iconified now. */
7a13e894 10544 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
10545 {
10546 f->async_iconified = 1;
bddd097c 10547
3b8f9651 10548 bufp->kind = ICONIFY_EVENT;
1aa6072f 10549 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10550 bufp->arg = Qnil;
1aa6072f
RS
10551 bufp++;
10552 count++;
10553 numchars--;
10554 }
7a13e894 10555 }
7a13e894 10556 goto OTHER;
dc6f92b8 10557
7a13e894 10558 case MapNotify:
06a2c219
GM
10559 if (event.xmap.window == tip_window)
10560 /* The tooltip has been drawn already. Avoid
10561 the SET_FRAME_GARBAGED below. */
10562 goto OTHER;
10563
10564 /* We use x_top_window_to_frame because map events can
10565 come for sub-windows and they don't mean that the
10566 frame is visible. */
19126e11 10567 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
10568 if (f)
10569 {
10570 f->async_visible = 1;
10571 f->async_iconified = 0;
06c488fd 10572 f->output_data.x->has_been_visible = 1;
dc6f92b8 10573
7a13e894
RS
10574 /* wait_reading_process_input will notice this and update
10575 the frame's display structures. */
10576 SET_FRAME_GARBAGED (f);
bddd097c 10577
d806e720
RS
10578 if (f->iconified)
10579 {
3b8f9651 10580 bufp->kind = DEICONIFY_EVENT;
d806e720 10581 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10582 bufp->arg = Qnil;
d806e720
RS
10583 bufp++;
10584 count++;
10585 numchars--;
10586 }
e73ec6fa 10587 else if (! NILP (Vframe_list)
8e713be6 10588 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
10589 /* Force a redisplay sooner or later
10590 to update the frame titles
10591 in case this is the second frame. */
10592 record_asynch_buffer_change ();
7a13e894 10593 }
7a13e894 10594 goto OTHER;
dc6f92b8 10595
7a13e894 10596 case KeyPress:
2a793c0c
PJ
10597
10598 /* Dispatch KeyPress events when in menu. */
10599 if (popup_activated_flag)
10600 goto OTHER;
10601
19126e11 10602 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 10603
66301061
KS
10604 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10605 {
10606 dpyinfo->mouse_face_hidden = 1;
10607 clear_mouse_face (dpyinfo);
10608 }
10609
eccc05db 10610#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
10611 if (f == 0)
10612 {
2564ea1b
GM
10613 /* Scroll bars consume key events, but we want
10614 the keys to go to the scroll bar's frame. */
06a2c219
GM
10615 Widget widget = XtWindowToWidget (dpyinfo->display,
10616 event.xkey.window);
10617 if (widget && XmIsScrollBar (widget))
10618 {
10619 widget = XtParent (widget);
10620 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10621 }
10622 }
eccc05db 10623#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 10624
7a13e894
RS
10625 if (f != 0)
10626 {
10627 KeySym keysym, orig_keysym;
379b5ac0
KH
10628 /* al%imercury@uunet.uu.net says that making this 81
10629 instead of 80 fixed a bug whereby meta chars made
10630 his Emacs hang.
10631
10632 It seems that some version of XmbLookupString has
10633 a bug of not returning XBufferOverflow in
10634 status_return even if the input is too long to
10635 fit in 81 bytes. So, we must prepare sufficient
10636 bytes for copy_buffer. 513 bytes (256 chars for
6c4a22e6 10637 two-byte character set) seems to be a fairly good
379b5ac0
KH
10638 approximation. -- 2000.8.10 handa@etl.go.jp */
10639 unsigned char copy_buffer[513];
10640 unsigned char *copy_bufptr = copy_buffer;
10641 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 10642 int modifiers;
c997eae5 10643 Lisp_Object coding_system = Qlatin_1;
64bb1782 10644
7a13e894
RS
10645 event.xkey.state
10646 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10647 extra_keyboard_modifiers);
10648 modifiers = event.xkey.state;
3a2712f9 10649
7a13e894 10650 /* This will have to go some day... */
752a043f 10651
7a13e894
RS
10652 /* make_lispy_event turns chars into control chars.
10653 Don't do it here because XLookupString is too eager. */
10654 event.xkey.state &= ~ControlMask;
5d46f928
RS
10655 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10656 | dpyinfo->super_mod_mask
10657 | dpyinfo->hyper_mod_mask
10658 | dpyinfo->alt_mod_mask);
10659
1cf4a0d1
RS
10660 /* In case Meta is ComposeCharacter,
10661 clear its status. According to Markus Ehrnsperger
10662 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10663 this enables ComposeCharacter to work whether or
10664 not it is combined with Meta. */
10665 if (modifiers & dpyinfo->meta_mod_mask)
10666 bzero (&compose_status, sizeof (compose_status));
10667
6c183ba5
RS
10668#ifdef HAVE_X_I18N
10669 if (FRAME_XIC (f))
10670 {
f5d11644
GM
10671 Status status_return;
10672
c997eae5 10673 coding_system = Vlocale_coding_system;
6c183ba5 10674 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
10675 &event.xkey, copy_bufptr,
10676 copy_bufsiz, &keysym,
6c183ba5 10677 &status_return);
f5d11644
GM
10678 if (status_return == XBufferOverflow)
10679 {
10680 copy_bufsiz = nbytes + 1;
10681 copy_bufptr = (char *) alloca (copy_bufsiz);
10682 nbytes = XmbLookupString (FRAME_XIC (f),
10683 &event.xkey, copy_bufptr,
10684 copy_bufsiz, &keysym,
10685 &status_return);
10686 }
96035dca
SM
10687/* Xutf8LookupString is a new but already deprecated interface. -stef */
10688#if 0 && defined X_HAVE_UTF8_STRING
c997eae5
SM
10689 else if (status_return == XLookupKeySym)
10690 { /* Try again but with utf-8. */
10691 coding_system = Qutf_8;
10692 nbytes = Xutf8LookupString (FRAME_XIC (f),
10693 &event.xkey, copy_bufptr,
10694 copy_bufsiz, &keysym,
10695 &status_return);
10696 if (status_return == XBufferOverflow)
10697 {
10698 copy_bufsiz = nbytes + 1;
10699 copy_bufptr = (char *) alloca (copy_bufsiz);
10700 nbytes = Xutf8LookupString (FRAME_XIC (f),
10701 &event.xkey,
10702 copy_bufptr,
10703 copy_bufsiz, &keysym,
10704 &status_return);
10705 }
10706 }
10707#endif
f5d11644 10708
1decb680
PE
10709 if (status_return == XLookupNone)
10710 break;
10711 else if (status_return == XLookupChars)
fdd9d55e
GM
10712 {
10713 keysym = NoSymbol;
10714 modifiers = 0;
10715 }
1decb680
PE
10716 else if (status_return != XLookupKeySym
10717 && status_return != XLookupBoth)
10718 abort ();
6c183ba5
RS
10719 }
10720 else
379b5ac0
KH
10721 nbytes = XLookupString (&event.xkey, copy_bufptr,
10722 copy_bufsiz, &keysym,
10723 &compose_status);
6c183ba5 10724#else
379b5ac0
KH
10725 nbytes = XLookupString (&event.xkey, copy_bufptr,
10726 copy_bufsiz, &keysym,
10727 &compose_status);
6c183ba5 10728#endif
dc6f92b8 10729
7a13e894 10730 orig_keysym = keysym;
55123275 10731
7a13e894
RS
10732 if (numchars > 1)
10733 {
10734 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10735 || keysym == XK_Delete
1097aea0 10736#ifdef XK_ISO_Left_Tab
441affdb 10737 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 10738#endif
852bff8f 10739 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
10740 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10741 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 10742#ifdef HPUX
7a13e894
RS
10743 /* This recognizes the "extended function keys".
10744 It seems there's no cleaner way.
10745 Test IsModifierKey to avoid handling mode_switch
10746 incorrectly. */
10747 || ((unsigned) (keysym) >= XK_Select
10748 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
10749#endif
10750#ifdef XK_dead_circumflex
7a13e894 10751 || orig_keysym == XK_dead_circumflex
69388238
RS
10752#endif
10753#ifdef XK_dead_grave
7a13e894 10754 || orig_keysym == XK_dead_grave
69388238
RS
10755#endif
10756#ifdef XK_dead_tilde
7a13e894 10757 || orig_keysym == XK_dead_tilde
69388238
RS
10758#endif
10759#ifdef XK_dead_diaeresis
7a13e894 10760 || orig_keysym == XK_dead_diaeresis
69388238
RS
10761#endif
10762#ifdef XK_dead_macron
7a13e894 10763 || orig_keysym == XK_dead_macron
69388238
RS
10764#endif
10765#ifdef XK_dead_degree
7a13e894 10766 || orig_keysym == XK_dead_degree
69388238
RS
10767#endif
10768#ifdef XK_dead_acute
7a13e894 10769 || orig_keysym == XK_dead_acute
69388238
RS
10770#endif
10771#ifdef XK_dead_cedilla
7a13e894 10772 || orig_keysym == XK_dead_cedilla
69388238
RS
10773#endif
10774#ifdef XK_dead_breve
7a13e894 10775 || orig_keysym == XK_dead_breve
69388238
RS
10776#endif
10777#ifdef XK_dead_ogonek
7a13e894 10778 || orig_keysym == XK_dead_ogonek
69388238
RS
10779#endif
10780#ifdef XK_dead_caron
7a13e894 10781 || orig_keysym == XK_dead_caron
69388238
RS
10782#endif
10783#ifdef XK_dead_doubleacute
7a13e894 10784 || orig_keysym == XK_dead_doubleacute
69388238
RS
10785#endif
10786#ifdef XK_dead_abovedot
7a13e894 10787 || orig_keysym == XK_dead_abovedot
c34790e0 10788#endif
7a13e894
RS
10789 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10790 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10791 /* Any "vendor-specific" key is ok. */
f0e299de
GM
10792 || (orig_keysym & (1 << 28))
10793 || (keysym != NoSymbol && nbytes == 0))
7a13e894 10794 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
10795#ifndef HAVE_X11R5
10796#ifdef XK_Mode_switch
7a13e894 10797 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
10798#endif
10799#ifdef XK_Num_Lock
7a13e894 10800 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
10801#endif
10802#endif /* not HAVE_X11R5 */
7ef18d79
EZ
10803 /* The symbols from XK_ISO_Lock to
10804 XK_ISO_Last_Group_Lock doesn't have real
10805 modifiers but should be treated similarly
10806 to Mode_switch by Emacs. */
10807#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10808 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10809 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10810#endif
7a13e894 10811 ))
dc6f92b8 10812 {
10e6549c
RS
10813 if (temp_index == sizeof temp_buffer / sizeof (short))
10814 temp_index = 0;
7a13e894 10815 temp_buffer[temp_index++] = keysym;
3b8f9651 10816 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
7a13e894 10817 bufp->code = keysym;
e0c1aef2 10818 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10819 bufp->arg = Qnil;
334208b7
RS
10820 bufp->modifiers
10821 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10822 modifiers);
1113d9db 10823 bufp->timestamp = event.xkey.time;
dc6f92b8 10824 bufp++;
7a13e894
RS
10825 count++;
10826 numchars--;
dc6f92b8 10827 }
7a13e894
RS
10828 else if (numchars > nbytes)
10829 {
10830 register int i;
379b5ac0 10831 register int c;
379b5ac0 10832 int nchars, len;
7a13e894 10833
c997eae5
SM
10834 /* The input should be decoded with `coding_system'
10835 which depends on which X*LookupString function
10836 we used just above and the locale. */
10837 setup_coding_system (coding_system, &coding);
10838 coding.src_multibyte = 0;
10839 coding.dst_multibyte = 1;
10840 /* The input is converted to events, thus we can't
10841 handle composition. Anyway, there's no XIM that
10842 gives us composition information. */
10843 coding.composing = COMPOSITION_DISABLED;
10844
7a13e894
RS
10845 for (i = 0; i < nbytes; i++)
10846 {
379b5ac0
KH
10847 if (temp_index == (sizeof temp_buffer
10848 / sizeof (short)))
7a13e894 10849 temp_index = 0;
379b5ac0
KH
10850 temp_buffer[temp_index++] = copy_bufptr[i];
10851 }
10852
c997eae5
SM
10853 {
10854 /* Decode the input data. */
10855 int require;
10856 unsigned char *p;
10857
10858 require = decoding_buffer_size (&coding, nbytes);
10859 p = (unsigned char *) alloca (require);
10860 coding.mode |= CODING_MODE_LAST_BLOCK;
d337aad5
KH
10861 /* We explicitely disable composition
10862 handling because key data should
10863 not contain any composition
10864 sequence. */
10865 coding.composing = COMPOSITION_DISABLED;
c997eae5
SM
10866 decode_coding (&coding, copy_bufptr, p,
10867 nbytes, require);
10868 nbytes = coding.produced;
10869 nchars = coding.produced_char;
10870 copy_bufptr = p;
10871 }
379b5ac0
KH
10872
10873 /* Convert the input data to a sequence of
10874 character events. */
10875 for (i = 0; i < nbytes; i += len)
10876 {
fee2aedc
GM
10877 if (nchars == nbytes)
10878 c = copy_bufptr[i], len = 1;
10879 else
10880 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10881 nbytes - i, len);
10882
379b5ac0 10883 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
3b8f9651
PJ
10884 ? ASCII_KEYSTROKE_EVENT
10885 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
379b5ac0 10886 bufp->code = c;
7a13e894 10887 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10888 bufp->arg = Qnil;
7a13e894
RS
10889 bufp->modifiers
10890 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10891 modifiers);
10892 bufp->timestamp = event.xkey.time;
10893 bufp++;
10894 }
10895
379b5ac0
KH
10896 count += nchars;
10897 numchars -= nchars;
1decb680
PE
10898
10899 if (keysym == NoSymbol)
10900 break;
7a13e894
RS
10901 }
10902 else
10903 abort ();
dc6f92b8 10904 }
10e6549c
RS
10905 else
10906 abort ();
dc6f92b8 10907 }
59ddecde
GM
10908#ifdef HAVE_X_I18N
10909 /* Don't dispatch this event since XtDispatchEvent calls
10910 XFilterEvent, and two calls in a row may freeze the
10911 client. */
10912 break;
10913#else
717ca130 10914 goto OTHER;
59ddecde 10915#endif
f451eb13 10916
f5d11644 10917 case KeyRelease:
59ddecde
GM
10918#ifdef HAVE_X_I18N
10919 /* Don't dispatch this event since XtDispatchEvent calls
10920 XFilterEvent, and two calls in a row may freeze the
10921 client. */
10922 break;
10923#else
f5d11644 10924 goto OTHER;
59ddecde 10925#endif
f5d11644 10926
7a13e894 10927 case EnterNotify:
06a2c219 10928 {
cab34f50
JD
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
06a2c219
GM
10937 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10938
2c850e26 10939#if 0
582c60f8 10940 if (event.xcrossing.focus)
06a2c219
GM
10941 {
10942 /* Avoid nasty pop/raise loops. */
10943 if (f && (!(f->auto_raise)
10944 || !(f->auto_lower)
10945 || (event.xcrossing.time - enter_timestamp) > 500))
10946 {
10947 x_new_focus_frame (dpyinfo, f);
10948 enter_timestamp = event.xcrossing.time;
10949 }
10950 }
10951 else if (f == dpyinfo->x_focus_frame)
10952 x_new_focus_frame (dpyinfo, 0);
2c850e26
RS
10953#endif
10954
06a2c219
GM
10955 /* EnterNotify counts as mouse movement,
10956 so update things that depend on mouse position. */
2533c408 10957 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10958 note_mouse_movement (f, &event.xmotion);
10959 goto OTHER;
10960 }
dc6f92b8 10961
7a13e894 10962 case FocusIn:
fd42232c 10963 {
cab34f50 10964 int n;
eb72635f 10965
cab34f50
JD
10966 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10967 if (n > 0)
fd42232c
JD
10968 {
10969 bufp += n, count += n, numchars -= n;
10970 }
10971 }
f9e24cb9 10972
7a13e894 10973 goto OTHER;
10c5e63d 10974
7a13e894 10975 case LeaveNotify:
cab34f50
JD
10976 {
10977 int n;
10978
10979 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10980 if (n > 0)
10981 {
10982 bufp += n, count += n, numchars -= n;
10983 }
10984 }
10985
19126e11 10986 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10987 if (f)
10c5e63d 10988 {
7a13e894 10989 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10990 {
10991 /* If we move outside the frame, then we're
10992 certainly no longer on any text in the frame. */
10993 clear_mouse_face (dpyinfo);
10994 dpyinfo->mouse_face_mouse_frame = 0;
10995 }
10996
10997 /* Generate a nil HELP_EVENT to cancel a help-echo.
10998 Do it only if there's something to cancel.
10999 Otherwise, the startup message is cleared when
11000 the mouse leaves the frame. */
11001 if (any_help_event_p)
11002 {
be010514
GM
11003 Lisp_Object frame;
11004 int n;
11005
06a2c219 11006 XSETFRAME (frame, f);
82c5d67a 11007 help_echo = Qnil;
5ab2570d
GM
11008 n = gen_help_event (bufp, numchars,
11009 Qnil, frame, Qnil, Qnil, 0);
be010514 11010 bufp += n, count += n, numchars -= n;
06a2c219 11011 }
7a13e894 11012
10c5e63d 11013 }
7a13e894 11014 goto OTHER;
dc6f92b8 11015
7a13e894 11016 case FocusOut:
cab34f50
JD
11017 {
11018 int n;
11019
11020 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11021 if (n > 0)
11022 {
11023 bufp += n, count += n, numchars -= n;
11024 }
11025 }
6c183ba5 11026
7a13e894 11027 goto OTHER;
dc6f92b8 11028
7a13e894 11029 case MotionNotify:
dc6f92b8 11030 {
06a2c219 11031 previous_help_echo = help_echo;
7cea38bc 11032 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 11033 help_echo_pos = -1;
06a2c219 11034
7a13e894
RS
11035 if (dpyinfo->grabbed && last_mouse_frame
11036 && FRAME_LIVE_P (last_mouse_frame))
11037 f = last_mouse_frame;
11038 else
19126e11 11039 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 11040
66301061
KS
11041 if (dpyinfo->mouse_face_hidden)
11042 {
11043 dpyinfo->mouse_face_hidden = 0;
11044 clear_mouse_face (dpyinfo);
11045 }
11046
7a13e894 11047 if (f)
5bc62483
PJ
11048 {
11049
11050 /* Generate SELECT_WINDOW_EVENTs when needed. */
ee8ceff8 11051 if (mouse_autoselect_window)
5bc62483
PJ
11052 {
11053 Lisp_Object window;
11054 int area;
11055
11056 window = window_from_coordinates (f,
0fc8f792 11057 event.xmotion.x, event.xmotion.y,
5bc62483
PJ
11058 &area, 0);
11059
11060 /* Window will be selected only when it is not selected now and
11061 last mouse movement event was not in it. Minibuffer window
11062 will be selected iff it is active. */
ce1295bd
PJ
11063 if (WINDOWP(window)
11064 && !EQ (window, last_window)
5bc62483
PJ
11065 && !EQ (window, selected_window)
11066 && numchars > 0)
11067 {
11068 bufp->kind = SELECT_WINDOW_EVENT;
11069 bufp->frame_or_window = window;
11070 bufp->arg = Qnil;
11071 ++bufp, ++count, --numchars;
11072 }
11073
11074 last_window=window;
11075 }
11076 note_mouse_movement (f, &event.xmotion);
11077 }
7a13e894
RS
11078 else
11079 {
e88b3c50 11080#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
11081 struct scroll_bar *bar
11082 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 11083
7a13e894
RS
11084 if (bar)
11085 x_scroll_bar_note_movement (bar, &event);
e88b3c50 11086#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 11087
06a2c219
GM
11088 /* If we move outside the frame, then we're
11089 certainly no longer on any text in the frame. */
7a13e894
RS
11090 clear_mouse_face (dpyinfo);
11091 }
06a2c219
GM
11092
11093 /* If the contents of the global variable help_echo
11094 has changed, generate a HELP_EVENT. */
b7e80413
SM
11095 if (!NILP (help_echo)
11096 || !NILP (previous_help_echo))
06a2c219
GM
11097 {
11098 Lisp_Object frame;
be010514 11099 int n;
06a2c219
GM
11100
11101 if (f)
11102 XSETFRAME (frame, f);
11103 else
11104 frame = Qnil;
11105
11106 any_help_event_p = 1;
5ab2570d 11107 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
11108 help_echo_window, help_echo_object,
11109 help_echo_pos);
be010514 11110 bufp += n, count += n, numchars -= n;
06a2c219
GM
11111 }
11112
11113 goto OTHER;
dc6f92b8 11114 }
dc6f92b8 11115
7a13e894 11116 case ConfigureNotify:
9829ddba
RS
11117 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
11118 if (f)
af395ec1 11119 {
7a4bce14 11120#ifndef USE_X_TOOLKIT
d0fa3e56
EZ
11121 /* If there is a pending resize for fullscreen, don't
11122 do this one, the right one will come later.
11123 The toolkit version doesn't seem to need this, but we
7a4bce14 11124 need to reset it below. */
d0fa3e56
EZ
11125 int dont_resize =
11126 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11127 && FRAME_NEW_WIDTH (f) != 0);
bf1b7b30
KH
11128 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
11129 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
d0fa3e56
EZ
11130 if (dont_resize)
11131 goto OTHER;
5c187dee 11132
2d7fc7e8
RS
11133 /* In the toolkit version, change_frame_size
11134 is called by the code that handles resizing
11135 of the EmacsFrame widget. */
7a13e894 11136
7a13e894
RS
11137 /* Even if the number of character rows and columns has
11138 not changed, the font size may have changed, so we need
11139 to check the pixel dimensions as well. */
11140 if (columns != f->width
11141 || rows != f->height
7556890b
RS
11142 || event.xconfigure.width != f->output_data.x->pixel_width
11143 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 11144 {
7d1e984f 11145 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 11146 SET_FRAME_GARBAGED (f);
e687d06e 11147 cancel_mouse_face (f);
7a13e894 11148 }
2d7fc7e8 11149#endif
af395ec1 11150
7556890b
RS
11151 f->output_data.x->pixel_width = event.xconfigure.width;
11152 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
11153
11154 /* What we have now is the position of Emacs's own window.
11155 Convert that to the position of the window manager window. */
dcb07ae9
RS
11156 x_real_positions (f, &f->output_data.x->left_pos,
11157 &f->output_data.x->top_pos);
11158
d0fa3e56
EZ
11159 x_check_fullscreen_move(f);
11160 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11161 f->output_data.x->want_fullscreen &=
11162 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
f5d11644
GM
11163#ifdef HAVE_X_I18N
11164 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11165 xic_set_statusarea (f);
11166#endif
11167
dcb07ae9
RS
11168 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11169 {
11170 /* Since the WM decorations come below top_pos now,
11171 we must put them below top_pos in the future. */
11172 f->output_data.x->win_gravity = NorthWestGravity;
11173 x_wm_set_size_hint (f, (long) 0, 0);
11174 }
8f08dc93
KH
11175#ifdef USE_MOTIF
11176 /* Some window managers pass (0,0) as the location of
11177 the window, and the Motif event handler stores it
11178 in the emacs widget, which messes up Motif menus. */
11179 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11180 {
11181 event.xconfigure.x = f->output_data.x->widget->core.x;
11182 event.xconfigure.y = f->output_data.x->widget->core.y;
11183 }
06a2c219 11184#endif /* USE_MOTIF */
7a13e894 11185 }
2d7fc7e8 11186 goto OTHER;
dc6f92b8 11187
7a13e894
RS
11188 case ButtonPress:
11189 case ButtonRelease:
11190 {
11191 /* If we decide we want to generate an event to be seen
11192 by the rest of Emacs, we put it here. */
11193 struct input_event emacs_event;
9ea173e8 11194 int tool_bar_p = 0;
06a2c219 11195
3b8f9651 11196 emacs_event.kind = NO_EVENT;
7a13e894 11197 bzero (&compose_status, sizeof (compose_status));
9b07615b 11198
06a2c219
GM
11199 if (dpyinfo->grabbed
11200 && last_mouse_frame
9f67f20b
RS
11201 && FRAME_LIVE_P (last_mouse_frame))
11202 f = last_mouse_frame;
11203 else
2224b905 11204 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 11205
06a2c219
GM
11206 if (f)
11207 {
9ea173e8
GM
11208 /* Is this in the tool-bar? */
11209 if (WINDOWP (f->tool_bar_window)
11210 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
11211 {
11212 Lisp_Object window;
11213 int p, x, y;
11214
11215 x = event.xbutton.x;
11216 y = event.xbutton.y;
11217
11218 /* Set x and y. */
11219 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 11220 if (EQ (window, f->tool_bar_window))
06a2c219 11221 {
9ea173e8
GM
11222 x_handle_tool_bar_click (f, &event.xbutton);
11223 tool_bar_p = 1;
06a2c219
GM
11224 }
11225 }
11226
9ea173e8 11227 if (!tool_bar_p)
06a2c219
GM
11228 if (!dpyinfo->x_focus_frame
11229 || f == dpyinfo->x_focus_frame)
11230 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
11231 }
11232 else
11233 {
06a2c219 11234#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
11235 struct scroll_bar *bar
11236 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 11237
7a13e894
RS
11238 if (bar)
11239 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 11240#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
11241 }
11242
11243 if (event.type == ButtonPress)
11244 {
11245 dpyinfo->grabbed |= (1 << event.xbutton.button);
11246 last_mouse_frame = f;
edad46f6
KH
11247 /* Ignore any mouse motion that happened
11248 before this event; any subsequent mouse-movement
11249 Emacs events should reflect only motion after
11250 the ButtonPress. */
a00e91cd
KH
11251 if (f != 0)
11252 f->mouse_moved = 0;
06a2c219 11253
9ea173e8
GM
11254 if (!tool_bar_p)
11255 last_tool_bar_item = -1;
7a13e894 11256 }
3afe33e7
RS
11257 else
11258 {
7a13e894 11259 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 11260 }
23faf38f 11261
3b8f9651 11262 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
7a13e894
RS
11263 {
11264 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11265 bufp++;
11266 count++;
11267 numchars--;
11268 }
3afe33e7
RS
11269
11270#ifdef USE_X_TOOLKIT
2224b905
RS
11271 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11272 /* For a down-event in the menu bar,
11273 don't pass it to Xt right now.
11274 Instead, save it away
11275 and we will pass it to Xt from kbd_buffer_get_event.
11276 That way, we can run some Lisp code first. */
91375f8f
RS
11277 if (f && event.type == ButtonPress
11278 /* Verify the event is really within the menu bar
11279 and not just sent to it due to grabbing. */
11280 && event.xbutton.x >= 0
11281 && event.xbutton.x < f->output_data.x->pixel_width
11282 && event.xbutton.y >= 0
11283 && event.xbutton.y < f->output_data.x->menubar_height
11284 && event.xbutton.same_screen)
2224b905 11285 {
8805890a 11286 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
11287 XSETFRAME (last_mouse_press_frame, f);
11288 }
11289 else if (event.type == ButtonPress)
11290 {
11291 last_mouse_press_frame = Qnil;
30e671c3 11292 goto OTHER;
ce89ef46 11293 }
06a2c219 11294
2237cac9
RS
11295#ifdef USE_MOTIF /* This should do not harm for Lucid,
11296 but I am trying to be cautious. */
ce89ef46
RS
11297 else if (event.type == ButtonRelease)
11298 {
2237cac9 11299 if (!NILP (last_mouse_press_frame))
f10ded1c 11300 {
2237cac9
RS
11301 f = XFRAME (last_mouse_press_frame);
11302 if (f->output_data.x)
06a2c219 11303 SET_SAVED_BUTTON_EVENT;
f10ded1c 11304 }
06a2c219 11305 else
30e671c3 11306 goto OTHER;
2224b905 11307 }
2237cac9 11308#endif /* USE_MOTIF */
2224b905
RS
11309 else
11310 goto OTHER;
3afe33e7 11311#endif /* USE_X_TOOLKIT */
7a13e894
RS
11312 }
11313 break;
dc6f92b8 11314
7a13e894 11315 case CirculateNotify:
06a2c219
GM
11316 goto OTHER;
11317
7a13e894 11318 case CirculateRequest:
06a2c219
GM
11319 goto OTHER;
11320
11321 case VisibilityNotify:
11322 goto OTHER;
dc6f92b8 11323
7a13e894
RS
11324 case MappingNotify:
11325 /* Someone has changed the keyboard mapping - update the
11326 local cache. */
11327 switch (event.xmapping.request)
11328 {
11329 case MappingModifier:
11330 x_find_modifier_meanings (dpyinfo);
11331 /* This is meant to fall through. */
11332 case MappingKeyboard:
11333 XRefreshKeyboardMapping (&event.xmapping);
11334 }
7a13e894 11335 goto OTHER;
dc6f92b8 11336
7a13e894 11337 default:
7a13e894 11338 OTHER:
717ca130 11339#ifdef USE_X_TOOLKIT
7a13e894
RS
11340 BLOCK_INPUT;
11341 XtDispatchEvent (&event);
11342 UNBLOCK_INPUT;
3afe33e7 11343#endif /* USE_X_TOOLKIT */
7a13e894
RS
11344 break;
11345 }
dc6f92b8
JB
11346 }
11347 }
11348
06a2c219
GM
11349 out:;
11350
9a5196d0
RS
11351 /* On some systems, an X bug causes Emacs to get no more events
11352 when the window is destroyed. Detect that. (1994.) */
58769bee 11353 if (! event_found)
ef2a22d0 11354 {
ef2a22d0
RS
11355 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11356 One XNOOP in 100 loops will make Emacs terminate.
11357 B. Bretthauer, 1994 */
11358 x_noop_count++;
58769bee 11359 if (x_noop_count >= 100)
ef2a22d0
RS
11360 {
11361 x_noop_count=0;
2224b905
RS
11362
11363 if (next_noop_dpyinfo == 0)
11364 next_noop_dpyinfo = x_display_list;
11365
11366 XNoOp (next_noop_dpyinfo->display);
11367
11368 /* Each time we get here, cycle through the displays now open. */
11369 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
11370 }
11371 }
502add23 11372
06a2c219 11373 /* If the focus was just given to an auto-raising frame,
0134a210 11374 raise it now. */
7a13e894 11375 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
11376 if (pending_autoraise_frame)
11377 {
11378 x_raise_frame (pending_autoraise_frame);
11379 pending_autoraise_frame = 0;
11380 }
0134a210 11381
dc6f92b8 11382 UNBLOCK_INPUT;
bde5503b 11383 --handling_signal;
dc6f92b8
JB
11384 return count;
11385}
06a2c219
GM
11386
11387
11388
dc6f92b8 11389\f
06a2c219
GM
11390/***********************************************************************
11391 Text Cursor
11392 ***********************************************************************/
11393
60626bab
GM
11394/* Notice when the text cursor of window W has been completely
11395 overwritten by a drawing operation that outputs glyphs in AREA
11396 starting at X0 and ending at X1 in the line starting at Y0 and
11397 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11398 the rest of the line after X0 has been written. Y coordinates
11399 are window-relative. */
06a2c219
GM
11400
11401static void
60626bab 11402notice_overwritten_cursor (w, area, x0, x1, y0, y1)
06a2c219 11403 struct window *w;
60626bab
GM
11404 enum glyph_row_area area;
11405 int x0, y0, x1, y1;
06a2c219 11406{
b3738089
GM
11407 if (area == TEXT_AREA && w->phys_cursor_on_p)
11408 {
11409 int cx0 = w->phys_cursor.x;
11410 int cx1 = cx0 + w->phys_cursor_width;
11411 int cy0 = w->phys_cursor.y;
11412 int cy1 = cy0 + w->phys_cursor_height;
11413
11414 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11415 {
11416 /* The cursor image will be completely removed from the
11417 screen if the output area intersects the cursor area in
11418 y-direction. When we draw in [y0 y1[, and some part of
11419 the cursor is at y < y0, that part must have been drawn
11420 before. When scrolling, the cursor is erased before
11421 actually scrolling, so we don't come here. When not
11422 scrolling, the rows above the old cursor row must have
11423 changed, and in this case these rows must have written
516074a1
GM
11424 over the cursor image.
11425
11426 Likewise if part of the cursor is below y1, with the
11427 exception of the cursor being in the first blank row at
11428 the buffer and window end because update_text_area
4398e673
GM
11429 doesn't draw that row. (Except when it does, but
11430 that's handled in update_text_area.) */
11431
f7c5994d 11432 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
516074a1 11433 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
b3738089
GM
11434 w->phys_cursor_on_p = 0;
11435 }
11436 }
06a2c219 11437}
f451eb13
JB
11438
11439
06a2c219
GM
11440/* Set clipping for output in glyph row ROW. W is the window in which
11441 we operate. GC is the graphics context to set clipping in.
11442 WHOLE_LINE_P non-zero means include the areas used for truncation
11443 mark display and alike in the clipping rectangle.
11444
11445 ROW may be a text row or, e.g., a mode line. Text rows must be
11446 clipped to the interior of the window dedicated to text display,
11447 mode lines must be clipped to the whole window. */
dc6f92b8
JB
11448
11449static void
06a2c219
GM
11450x_clip_to_row (w, row, gc, whole_line_p)
11451 struct window *w;
11452 struct glyph_row *row;
11453 GC gc;
11454 int whole_line_p;
dc6f92b8 11455{
06a2c219
GM
11456 struct frame *f = XFRAME (WINDOW_FRAME (w));
11457 XRectangle clip_rect;
11458 int window_x, window_y, window_width, window_height;
dc6f92b8 11459
06a2c219 11460 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 11461
06a2c219
GM
11462 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11463 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11464 clip_rect.y = max (clip_rect.y, window_y);
11465 clip_rect.width = window_width;
11466 clip_rect.height = row->visible_height;
5c1aae96 11467
06a2c219
GM
11468 /* If clipping to the whole line, including trunc marks, extend
11469 the rectangle to the left and increase its width. */
11470 if (whole_line_p)
11471 {
3f332ef3
KS
11472 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11473 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
06a2c219 11474 }
5c1aae96 11475
06a2c219 11476 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
11477}
11478
06a2c219
GM
11479
11480/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
11481
11482static void
06a2c219
GM
11483x_draw_hollow_cursor (w, row)
11484 struct window *w;
11485 struct glyph_row *row;
dc6f92b8 11486{
06a2c219
GM
11487 struct frame *f = XFRAME (WINDOW_FRAME (w));
11488 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11489 Display *dpy = FRAME_X_DISPLAY (f);
11490 int x, y, wd, h;
11491 XGCValues xgcv;
11492 struct glyph *cursor_glyph;
11493 GC gc;
11494
11495 /* Compute frame-relative coordinates from window-relative
11496 coordinates. */
11497 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11498 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11499 + row->ascent - w->phys_cursor_ascent);
11500 h = row->height - 1;
11501
11502 /* Get the glyph the cursor is on. If we can't tell because
11503 the current matrix is invalid or such, give up. */
11504 cursor_glyph = get_phys_cursor_glyph (w);
11505 if (cursor_glyph == NULL)
dc6f92b8
JB
11506 return;
11507
06a2c219
GM
11508 /* Compute the width of the rectangle to draw. If on a stretch
11509 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11510 rectangle as wide as the glyph, but use a canonical character
11511 width instead. */
11512 wd = cursor_glyph->pixel_width - 1;
11513 if (cursor_glyph->type == STRETCH_GLYPH
11514 && !x_stretch_cursor_p)
11515 wd = min (CANON_X_UNIT (f), wd);
b3738089 11516 w->phys_cursor_width = wd;
06a2c219
GM
11517
11518 /* The foreground of cursor_gc is typically the same as the normal
11519 background color, which can cause the cursor box to be invisible. */
11520 xgcv.foreground = f->output_data.x->cursor_pixel;
11521 if (dpyinfo->scratch_cursor_gc)
11522 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11523 else
11524 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11525 GCForeground, &xgcv);
11526 gc = dpyinfo->scratch_cursor_gc;
11527
11528 /* Set clipping, draw the rectangle, and reset clipping again. */
11529 x_clip_to_row (w, row, gc, 0);
11530 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11531 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
11532}
11533
06a2c219
GM
11534
11535/* Draw a bar cursor on window W in glyph row ROW.
11536
11537 Implementation note: One would like to draw a bar cursor with an
11538 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11539 Unfortunately, I didn't find a font yet that has this property set.
11540 --gerd. */
dc6f92b8
JB
11541
11542static void
65c26775 11543x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
11544 struct window *w;
11545 struct glyph_row *row;
f02d8aa0 11546 int width;
65c26775 11547 enum text_cursor_kinds kind;
dc6f92b8 11548{
92f424df
GM
11549 struct frame *f = XFRAME (w->frame);
11550 struct glyph *cursor_glyph;
06a2c219 11551
92f424df
GM
11552 /* If cursor is out of bounds, don't draw garbage. This can happen
11553 in mini-buffer windows when switching between echo area glyphs
11554 and mini-buffer. */
11555 cursor_glyph = get_phys_cursor_glyph (w);
11556 if (cursor_glyph == NULL)
11557 return;
06a2c219 11558
92f424df
GM
11559 /* If on an image, draw like a normal cursor. That's usually better
11560 visible than drawing a bar, esp. if the image is large so that
11561 the bar might not be in the window. */
11562 if (cursor_glyph->type == IMAGE_GLYPH)
11563 {
11564 struct glyph_row *row;
11565 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11566 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11567 }
11568 else
11569 {
34e5d0af
GM
11570 Display *dpy = FRAME_X_DISPLAY (f);
11571 Window window = FRAME_X_WINDOW (f);
11572 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11573 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11574 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11575 XGCValues xgcv;
11576
11577 /* If the glyph's background equals the color we normally draw
11578 the bar cursor in, the bar cursor in its normal color is
11579 invisible. Use the glyph's foreground color instead in this
11580 case, on the assumption that the glyph's colors are chosen so
11581 that the glyph is legible. */
11582 if (face->background == f->output_data.x->cursor_pixel)
11583 xgcv.background = xgcv.foreground = face->foreground;
11584 else
11585 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 11586 xgcv.graphics_exposures = 0;
92f424df 11587
06a2c219
GM
11588 if (gc)
11589 XChangeGC (dpy, gc, mask, &xgcv);
11590 else
11591 {
11592 gc = XCreateGC (dpy, window, mask, &xgcv);
11593 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11594 }
92f424df 11595
f02d8aa0 11596 if (width < 0)
fdbe859c 11597 width = FRAME_CURSOR_WIDTH (f);
34e5d0af 11598 width = min (cursor_glyph->pixel_width, width);
92f424df 11599
b3738089 11600 w->phys_cursor_width = width;
06a2c219 11601 x_clip_to_row (w, row, gc, 0);
b3738089 11602
65c26775
EZ
11603 if (kind == BAR_CURSOR)
11604 XFillRectangle (dpy, window, gc,
11605 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11606 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11607 width, row->height);
11608 else
11609 XFillRectangle (dpy, window, gc,
11610 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11611 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11612 row->height - width),
11613 cursor_glyph->pixel_width,
11614 width);
11615
06a2c219
GM
11616 XSetClipMask (dpy, gc, None);
11617 }
dc6f92b8
JB
11618}
11619
06a2c219
GM
11620
11621/* Clear the cursor of window W to background color, and mark the
11622 cursor as not shown. This is used when the text where the cursor
11623 is is about to be rewritten. */
11624
dc6f92b8 11625static void
06a2c219
GM
11626x_clear_cursor (w)
11627 struct window *w;
dc6f92b8 11628{
06a2c219
GM
11629 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11630 x_update_window_cursor (w, 0);
11631}
90e65f07 11632
dbc4e1c1 11633
06a2c219
GM
11634/* Draw the cursor glyph of window W in glyph row ROW. See the
11635 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 11636
06a2c219
GM
11637static void
11638x_draw_phys_cursor_glyph (w, row, hl)
11639 struct window *w;
11640 struct glyph_row *row;
11641 enum draw_glyphs_face hl;
11642{
11643 /* If cursor hpos is out of bounds, don't draw garbage. This can
11644 happen in mini-buffer windows when switching between echo area
11645 glyphs and mini-buffer. */
11646 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e 11647 {
f0a48a01 11648 int on_p = w->phys_cursor_on_p;
56a0382c 11649 int x1;
f0a48a01 11650
56a0382c
GM
11651 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11652 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11653 hl, 0);
f0a48a01 11654 w->phys_cursor_on_p = on_p;
66ac4b0e 11655
b3738089 11656 if (hl == DRAW_CURSOR)
56a0382c 11657 w->phys_cursor_width = x1 - w->phys_cursor.x;
b3738089 11658
66ac4b0e
GM
11659 /* When we erase the cursor, and ROW is overlapped by other
11660 rows, make sure that these overlapping parts of other rows
11661 are redrawn. */
b3738089 11662 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
66ac4b0e
GM
11663 {
11664 if (row > w->current_matrix->rows
11665 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11666 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11667
11668 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11669 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11670 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11671 }
11672 }
06a2c219 11673}
dbc4e1c1 11674
eea6af04 11675
06a2c219 11676/* Erase the image of a cursor of window W from the screen. */
eea6af04 11677
06a2c219
GM
11678static void
11679x_erase_phys_cursor (w)
11680 struct window *w;
11681{
11682 struct frame *f = XFRAME (w->frame);
11683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11684 int hpos = w->phys_cursor.hpos;
11685 int vpos = w->phys_cursor.vpos;
11686 int mouse_face_here_p = 0;
11687 struct glyph_matrix *active_glyphs = w->current_matrix;
11688 struct glyph_row *cursor_row;
11689 struct glyph *cursor_glyph;
11690 enum draw_glyphs_face hl;
11691
11692 /* No cursor displayed or row invalidated => nothing to do on the
11693 screen. */
11694 if (w->phys_cursor_type == NO_CURSOR)
11695 goto mark_cursor_off;
11696
11697 /* VPOS >= active_glyphs->nrows means that window has been resized.
11698 Don't bother to erase the cursor. */
11699 if (vpos >= active_glyphs->nrows)
11700 goto mark_cursor_off;
11701
11702 /* If row containing cursor is marked invalid, there is nothing we
11703 can do. */
11704 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11705 if (!cursor_row->enabled_p)
11706 goto mark_cursor_off;
11707
a2c6de8e
KS
11708 /* If row is completely invisible, don't attempt to delete a cursor which
11709 isn't there. This can happen if cursor is at top of a window, and
11710 we switch to a buffer with a header line in that window. */
11711 if (cursor_row->visible_height <= 0)
11712 goto mark_cursor_off;
11713
06a2c219
GM
11714 /* This can happen when the new row is shorter than the old one.
11715 In this case, either x_draw_glyphs or clear_end_of_line
11716 should have cleared the cursor. Note that we wouldn't be
11717 able to erase the cursor in this case because we don't have a
11718 cursor glyph at hand. */
11719 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11720 goto mark_cursor_off;
11721
11722 /* If the cursor is in the mouse face area, redisplay that when
11723 we clear the cursor. */
8801a864
KR
11724 if (! NILP (dpyinfo->mouse_face_window)
11725 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
11726 && (vpos > dpyinfo->mouse_face_beg_row
11727 || (vpos == dpyinfo->mouse_face_beg_row
11728 && hpos >= dpyinfo->mouse_face_beg_col))
11729 && (vpos < dpyinfo->mouse_face_end_row
11730 || (vpos == dpyinfo->mouse_face_end_row
11731 && hpos < dpyinfo->mouse_face_end_col))
11732 /* Don't redraw the cursor's spot in mouse face if it is at the
11733 end of a line (on a newline). The cursor appears there, but
11734 mouse highlighting does not. */
11735 && cursor_row->used[TEXT_AREA] > hpos)
11736 mouse_face_here_p = 1;
11737
11738 /* Maybe clear the display under the cursor. */
11739 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11740 {
11741 int x;
045dee35 11742 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 11743
06a2c219
GM
11744 cursor_glyph = get_phys_cursor_glyph (w);
11745 if (cursor_glyph == NULL)
11746 goto mark_cursor_off;
dbc4e1c1 11747
e0300d33 11748 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
06a2c219 11749
c5e6e06b
GM
11750 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11751 x,
11752 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11753 cursor_row->y)),
11754 cursor_glyph->pixel_width,
11755 cursor_row->visible_height,
11756 False);
dbc4e1c1 11757 }
06a2c219
GM
11758
11759 /* Erase the cursor by redrawing the character underneath it. */
11760 if (mouse_face_here_p)
11761 hl = DRAW_MOUSE_FACE;
06a2c219
GM
11762 else
11763 hl = DRAW_NORMAL_TEXT;
11764 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 11765
06a2c219
GM
11766 mark_cursor_off:
11767 w->phys_cursor_on_p = 0;
11768 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
11769}
11770
11771
b7f83f9e
GM
11772/* Non-zero if physical cursor of window W is within mouse face. */
11773
11774static int
11775cursor_in_mouse_face_p (w)
11776 struct window *w;
11777{
11778 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11779 int in_mouse_face = 0;
11780
11781 if (WINDOWP (dpyinfo->mouse_face_window)
11782 && XWINDOW (dpyinfo->mouse_face_window) == w)
11783 {
11784 int hpos = w->phys_cursor.hpos;
11785 int vpos = w->phys_cursor.vpos;
11786
11787 if (vpos >= dpyinfo->mouse_face_beg_row
11788 && vpos <= dpyinfo->mouse_face_end_row
11789 && (vpos > dpyinfo->mouse_face_beg_row
11790 || hpos >= dpyinfo->mouse_face_beg_col)
11791 && (vpos < dpyinfo->mouse_face_end_row
11792 || hpos < dpyinfo->mouse_face_end_col
11793 || dpyinfo->mouse_face_past_end))
11794 in_mouse_face = 1;
11795 }
11796
11797 return in_mouse_face;
11798}
11799
11800
06a2c219
GM
11801/* Display or clear cursor of window W. If ON is zero, clear the
11802 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11803 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 11804
06a2c219
GM
11805void
11806x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11807 struct window *w;
11808 int on, hpos, vpos, x, y;
dbc4e1c1 11809{
06a2c219
GM
11810 struct frame *f = XFRAME (w->frame);
11811 int new_cursor_type;
f02d8aa0 11812 int new_cursor_width;
06a2c219
GM
11813 struct glyph_matrix *current_glyphs;
11814 struct glyph_row *glyph_row;
11815 struct glyph *glyph;
dbc4e1c1 11816
49d838ea 11817 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
11818 windows and frames; in the latter case, the frame or window may
11819 be in the midst of changing its size, and x and y may be off the
11820 window. */
11821 if (! FRAME_VISIBLE_P (f)
11822 || FRAME_GARBAGED_P (f)
11823 || vpos >= w->current_matrix->nrows
11824 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
11825 return;
11826
11827 /* If cursor is off and we want it off, return quickly. */
06a2c219 11828 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
11829 return;
11830
06a2c219
GM
11831 current_glyphs = w->current_matrix;
11832 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11833 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11834
11835 /* If cursor row is not enabled, we don't really know where to
11836 display the cursor. */
11837 if (!glyph_row->enabled_p)
11838 {
11839 w->phys_cursor_on_p = 0;
11840 return;
11841 }
11842
11843 xassert (interrupt_input_blocked);
11844
fdbe859c
KS
11845 /* Set new_cursor_type to the cursor we want to be displayed. */
11846 new_cursor_type = get_window_cursor_type (w, &new_cursor_width);
f7e039de 11847
06a2c219
GM
11848 /* If cursor is currently being shown and we don't want it to be or
11849 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 11850 erase it. */
06a2c219 11851 if (w->phys_cursor_on_p
dc6f92b8 11852 && (!on
06a2c219
GM
11853 || w->phys_cursor.x != x
11854 || w->phys_cursor.y != y
e1429afe 11855 || new_cursor_type != w->phys_cursor_type
fdbe859c 11856 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
e1429afe 11857 && new_cursor_width != w->phys_cursor_width)))
06a2c219
GM
11858 x_erase_phys_cursor (w);
11859
56a0382c
GM
11860 /* Don't check phys_cursor_on_p here because that flag is only set
11861 to zero in some cases where we know that the cursor has been
11862 completely erased, to avoid the extra work of erasing the cursor
11863 twice. In other words, phys_cursor_on_p can be 1 and the cursor
11864 still not be visible, or it has only been partly erased. */
11865 if (on)
06a2c219
GM
11866 {
11867 w->phys_cursor_ascent = glyph_row->ascent;
11868 w->phys_cursor_height = glyph_row->height;
11869
11870 /* Set phys_cursor_.* before x_draw_.* is called because some
11871 of them may need the information. */
11872 w->phys_cursor.x = x;
11873 w->phys_cursor.y = glyph_row->y;
11874 w->phys_cursor.hpos = hpos;
11875 w->phys_cursor.vpos = vpos;
11876 w->phys_cursor_type = new_cursor_type;
11877 w->phys_cursor_on_p = 1;
11878
11879 switch (new_cursor_type)
dc6f92b8 11880 {
06a2c219
GM
11881 case HOLLOW_BOX_CURSOR:
11882 x_draw_hollow_cursor (w, glyph_row);
11883 break;
11884
11885 case FILLED_BOX_CURSOR:
11886 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11887 break;
11888
11889 case BAR_CURSOR:
65c26775
EZ
11890 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
11891 break;
11892
11893 case HBAR_CURSOR:
b3738089 11894 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
06a2c219
GM
11895 break;
11896
11897 case NO_CURSOR:
4398e673 11898 w->phys_cursor_width = 0;
06a2c219 11899 break;
dc6f92b8 11900
06a2c219
GM
11901 default:
11902 abort ();
11903 }
59ddecde
GM
11904
11905#ifdef HAVE_X_I18N
11906 if (w == XWINDOW (f->selected_window))
11907 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11908 xic_set_preeditarea (w, x, y);
11909#endif
dc6f92b8
JB
11910 }
11911
06a2c219 11912#ifndef XFlush
f676886a 11913 if (updating_frame != f)
334208b7 11914 XFlush (FRAME_X_DISPLAY (f));
06a2c219 11915#endif
dc6f92b8
JB
11916}
11917
06a2c219
GM
11918
11919/* Display the cursor on window W, or clear it. X and Y are window
11920 relative pixel coordinates. HPOS and VPOS are glyph matrix
11921 positions. If W is not the selected window, display a hollow
11922 cursor. ON non-zero means display the cursor at X, Y which
11923 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11924
dfcf069d 11925void
06a2c219
GM
11926x_display_cursor (w, on, hpos, vpos, x, y)
11927 struct window *w;
11928 int on, hpos, vpos, x, y;
dc6f92b8 11929{
f94397b5 11930 BLOCK_INPUT;
06a2c219 11931 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11932 UNBLOCK_INPUT;
11933}
11934
06a2c219
GM
11935
11936/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11937 Don't change the cursor's position. */
11938
dfcf069d 11939void
06a2c219 11940x_update_cursor (f, on_p)
5d46f928 11941 struct frame *f;
5d46f928 11942{
06a2c219
GM
11943 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11944}
11945
11946
11947/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11948 in the window tree rooted at W. */
11949
11950static void
11951x_update_cursor_in_window_tree (w, on_p)
11952 struct window *w;
11953 int on_p;
11954{
11955 while (w)
11956 {
11957 if (!NILP (w->hchild))
11958 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11959 else if (!NILP (w->vchild))
11960 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11961 else
11962 x_update_window_cursor (w, on_p);
11963
11964 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11965 }
11966}
5d46f928 11967
f94397b5 11968
06a2c219
GM
11969/* Switch the display of W's cursor on or off, according to the value
11970 of ON. */
11971
11972static void
11973x_update_window_cursor (w, on)
11974 struct window *w;
11975 int on;
11976{
16b5d424
GM
11977 /* Don't update cursor in windows whose frame is in the process
11978 of being deleted. */
11979 if (w->current_matrix)
11980 {
11981 BLOCK_INPUT;
11982 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11983 w->phys_cursor.x, w->phys_cursor.y);
11984 UNBLOCK_INPUT;
11985 }
dc6f92b8 11986}
06a2c219
GM
11987
11988
11989
dc6f92b8
JB
11990\f
11991/* Icons. */
11992
dbc4e1c1 11993/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11994
11995int
990ba854 11996x_bitmap_icon (f, file)
f676886a 11997 struct frame *f;
990ba854 11998 Lisp_Object file;
dc6f92b8 11999{
06a2c219 12000 int bitmap_id;
dc6f92b8 12001
c118dd06 12002 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
12003 return 1;
12004
990ba854 12005 /* Free up our existing icon bitmap if any. */
7556890b
RS
12006 if (f->output_data.x->icon_bitmap > 0)
12007 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12008 f->output_data.x->icon_bitmap = 0;
990ba854
RS
12009
12010 if (STRINGP (file))
7f2ae036
RS
12011 bitmap_id = x_create_bitmap_from_file (f, file);
12012 else
12013 {
990ba854 12014 /* Create the GNU bitmap if necessary. */
5bf01b68 12015 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
12016 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
12017 = x_create_bitmap_from_data (f, gnu_bits,
12018 gnu_width, gnu_height);
990ba854
RS
12019
12020 /* The first time we create the GNU bitmap,
06a2c219 12021 this increments the ref-count one extra time.
990ba854
RS
12022 As a result, the GNU bitmap is never freed.
12023 That way, we don't have to worry about allocating it again. */
334208b7 12024 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 12025
334208b7 12026 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
12027 }
12028
12029 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 12030 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
12031
12032 return 0;
12033}
12034
12035
1be2d067
KH
12036/* Make the x-window of frame F use a rectangle with text.
12037 Use ICON_NAME as the text. */
dc6f92b8
JB
12038
12039int
f676886a
JB
12040x_text_icon (f, icon_name)
12041 struct frame *f;
dc6f92b8
JB
12042 char *icon_name;
12043{
c118dd06 12044 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
12045 return 1;
12046
1be2d067
KH
12047#ifdef HAVE_X11R4
12048 {
12049 XTextProperty text;
12050 text.value = (unsigned char *) icon_name;
12051 text.encoding = XA_STRING;
12052 text.format = 8;
12053 text.nitems = strlen (icon_name);
12054#ifdef USE_X_TOOLKIT
7556890b 12055 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
12056 &text);
12057#else /* not USE_X_TOOLKIT */
12058 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
12059#endif /* not USE_X_TOOLKIT */
12060 }
12061#else /* not HAVE_X11R4 */
12062 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
12063#endif /* not HAVE_X11R4 */
58769bee 12064
7556890b
RS
12065 if (f->output_data.x->icon_bitmap > 0)
12066 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12067 f->output_data.x->icon_bitmap = 0;
b1c884c3 12068 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
12069
12070 return 0;
12071}
12072\f
e99db5a1
RS
12073#define X_ERROR_MESSAGE_SIZE 200
12074
12075/* If non-nil, this should be a string.
12076 It means catch X errors and store the error message in this string. */
12077
12078static Lisp_Object x_error_message_string;
12079
12080/* An X error handler which stores the error message in
12081 x_error_message_string. This is called from x_error_handler if
12082 x_catch_errors is in effect. */
12083
06a2c219 12084static void
e99db5a1
RS
12085x_error_catcher (display, error)
12086 Display *display;
12087 XErrorEvent *error;
12088{
12089 XGetErrorText (display, error->error_code,
d5db4077 12090 SDATA (x_error_message_string),
e99db5a1
RS
12091 X_ERROR_MESSAGE_SIZE);
12092}
12093
12094/* Begin trapping X errors for display DPY. Actually we trap X errors
12095 for all displays, but DPY should be the display you are actually
12096 operating on.
12097
12098 After calling this function, X protocol errors no longer cause
12099 Emacs to exit; instead, they are recorded in the string
12100 stored in x_error_message_string.
12101
12102 Calling x_check_errors signals an Emacs error if an X error has
12103 occurred since the last call to x_catch_errors or x_check_errors.
12104
12105 Calling x_uncatch_errors resumes the normal error handling. */
12106
12107void x_check_errors ();
12108static Lisp_Object x_catch_errors_unwind ();
12109
12110int
12111x_catch_errors (dpy)
12112 Display *dpy;
12113{
aed13378 12114 int count = SPECPDL_INDEX ();
e99db5a1
RS
12115
12116 /* Make sure any errors from previous requests have been dealt with. */
12117 XSync (dpy, False);
12118
12119 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
12120
12121 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7da167cd 12122 SSET (x_error_message_string, 0, 0);
e99db5a1
RS
12123
12124 return count;
12125}
12126
12127/* Unbind the binding that we made to check for X errors. */
12128
12129static Lisp_Object
12130x_catch_errors_unwind (old_val)
12131 Lisp_Object old_val;
12132{
12133 x_error_message_string = old_val;
12134 return Qnil;
12135}
12136
12137/* If any X protocol errors have arrived since the last call to
12138 x_catch_errors or x_check_errors, signal an Emacs error using
12139 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12140
12141void
12142x_check_errors (dpy, format)
12143 Display *dpy;
12144 char *format;
12145{
12146 /* Make sure to catch any errors incurred so far. */
12147 XSync (dpy, False);
12148
d5db4077
KR
12149 if (SREF (x_error_message_string, 0))
12150 error (format, SDATA (x_error_message_string));
e99db5a1
RS
12151}
12152
9829ddba
RS
12153/* Nonzero if we had any X protocol errors
12154 since we did x_catch_errors on DPY. */
e99db5a1
RS
12155
12156int
12157x_had_errors_p (dpy)
12158 Display *dpy;
12159{
12160 /* Make sure to catch any errors incurred so far. */
12161 XSync (dpy, False);
12162
d5db4077 12163 return SREF (x_error_message_string, 0) != 0;
e99db5a1
RS
12164}
12165
9829ddba
RS
12166/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12167
06a2c219 12168void
9829ddba
RS
12169x_clear_errors (dpy)
12170 Display *dpy;
12171{
7da167cd 12172 SSET (x_error_message_string, 0, 0);
9829ddba
RS
12173}
12174
e99db5a1
RS
12175/* Stop catching X protocol errors and let them make Emacs die.
12176 DPY should be the display that was passed to x_catch_errors.
12177 COUNT should be the value that was returned by
12178 the corresponding call to x_catch_errors. */
12179
12180void
12181x_uncatch_errors (dpy, count)
12182 Display *dpy;
12183 int count;
12184{
12185 unbind_to (count, Qnil);
12186}
12187
12188#if 0
12189static unsigned int x_wire_count;
12190x_trace_wire ()
12191{
12192 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12193}
12194#endif /* ! 0 */
12195
12196\f
12197/* Handle SIGPIPE, which can happen when the connection to a server
12198 simply goes away. SIGPIPE is handled by x_connection_signal.
12199 Don't need to do anything, because the write which caused the
12200 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 12201 which will do the appropriate cleanup for us. */
e99db5a1
RS
12202
12203static SIGTYPE
12204x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 12205 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
12206{
12207#ifdef USG
12208 /* USG systems forget handlers when they are used;
12209 must reestablish each time */
12210 signal (signalnum, x_connection_signal);
12211#endif /* USG */
12212}
0da1ab50 12213
e99db5a1 12214\f
0da1ab50
GM
12215/************************************************************************
12216 Handling X errors
12217 ************************************************************************/
4746118a 12218
f0e299de
GM
12219/* Error message passed to x_connection_closed. */
12220
12221static char *error_msg;
12222
a7248e4f 12223/* Function installed as fatal_error_signal_hook in
f0e299de
GM
12224 x_connection_closed. Print the X error message, and exit normally,
12225 instead of dumping core when XtCloseDisplay fails. */
12226
12227static void
12228x_fatal_error_signal ()
12229{
12230 fprintf (stderr, "%s\n", error_msg);
12231 exit (70);
12232}
12233
0da1ab50
GM
12234/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12235 the text of an error message that lead to the connection loss. */
16bd92ea 12236
4746118a 12237static SIGTYPE
5978125e
GM
12238x_connection_closed (dpy, error_message)
12239 Display *dpy;
7a13e894 12240 char *error_message;
4746118a 12241{
5978125e 12242 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 12243 Lisp_Object frame, tail;
0da1ab50 12244 int count;
0da1ab50 12245
f0e299de
GM
12246 error_msg = (char *) alloca (strlen (error_message) + 1);
12247 strcpy (error_msg, error_message);
1a532e54
GM
12248 handling_signal = 0;
12249
0da1ab50
GM
12250 /* Prevent being called recursively because of an error condition
12251 below. Otherwise, we might end up with printing ``can't find per
12252 display information'' in the recursive call instead of printing
12253 the original message here. */
12254 count = x_catch_errors (dpy);
12255
8a4f36cc
GM
12256 /* We have to close the display to inform Xt that it doesn't
12257 exist anymore. If we don't, Xt will continue to wait for
12258 events from the display. As a consequence, a sequence of
12259
12260 M-x make-frame-on-display RET :1 RET
12261 ...kill the new frame, so that we get an IO error...
12262 M-x make-frame-on-display RET :1 RET
12263
12264 will indefinitely wait in Xt for events for display `:1', opened
12265 in the first class to make-frame-on-display.
6186a4a0 12266
8a4f36cc
GM
12267 Closing the display is reported to lead to a bus error on
12268 OpenWindows in certain situations. I suspect that is a bug
12269 in OpenWindows. I don't know how to cicumvent it here. */
12270
f613a4c8 12271#ifdef USE_X_TOOLKIT
ae24cb3b
GM
12272 /* If DPYINFO is null, this means we didn't open the display
12273 in the first place, so don't try to close it. */
12274 if (dpyinfo)
f0e299de
GM
12275 {
12276 extern void (*fatal_error_signal_hook) P_ ((void));
12277 fatal_error_signal_hook = x_fatal_error_signal;
12278 XtCloseDisplay (dpy);
12279 fatal_error_signal_hook = NULL;
12280 }
f613a4c8 12281#endif
adabc3a9 12282
8a4f36cc 12283 /* Indicate that this display is dead. */
9e80b57d
KR
12284 if (dpyinfo)
12285 dpyinfo->display = 0;
6186a4a0 12286
06a2c219 12287 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
12288 that are on the dead display. */
12289 FOR_EACH_FRAME (tail, frame)
12290 {
12291 Lisp_Object minibuf_frame;
12292 minibuf_frame
12293 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
12294 if (FRAME_X_P (XFRAME (frame))
12295 && FRAME_X_P (XFRAME (minibuf_frame))
12296 && ! EQ (frame, minibuf_frame)
12297 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
12298 Fdelete_frame (frame, Qt);
12299 }
12300
12301 /* Now delete all remaining frames on the dead display.
06a2c219 12302 We are now sure none of these is used as the mini-buffer
7a13e894
RS
12303 for another frame that we need to delete. */
12304 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
12305 if (FRAME_X_P (XFRAME (frame))
12306 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
12307 {
12308 /* Set this to t so that Fdelete_frame won't get confused
12309 trying to find a replacement. */
12310 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12311 Fdelete_frame (frame, Qt);
12312 }
7a13e894 12313
482a1bd2
KH
12314 if (dpyinfo)
12315 x_delete_display (dpyinfo);
7a13e894 12316
0da1ab50
GM
12317 x_uncatch_errors (dpy, count);
12318
7a13e894
RS
12319 if (x_display_list == 0)
12320 {
f0e299de 12321 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
12322 shut_down_emacs (0, 0, Qnil);
12323 exit (70);
12324 }
12ba150f 12325
7a13e894
RS
12326 /* Ordinary stack unwind doesn't deal with these. */
12327#ifdef SIGIO
12328 sigunblock (sigmask (SIGIO));
12329#endif
12330 sigunblock (sigmask (SIGALRM));
12331 TOTALLY_UNBLOCK_INPUT;
12332
aa4d9a9e 12333 clear_waiting_for_input ();
f0e299de 12334 error ("%s", error_msg);
4746118a
JB
12335}
12336
0da1ab50 12337
7a13e894
RS
12338/* This is the usual handler for X protocol errors.
12339 It kills all frames on the display that we got the error for.
12340 If that was the only one, it prints an error message and kills Emacs. */
12341
06a2c219 12342static void
c118dd06
JB
12343x_error_quitter (display, error)
12344 Display *display;
12345 XErrorEvent *error;
12346{
7a13e894 12347 char buf[256], buf1[356];
dc6f92b8 12348
58769bee 12349 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12350 original error handler. */
dc6f92b8 12351
c118dd06 12352 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 12353 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 12354 buf, error->request_code);
7a13e894 12355 x_connection_closed (display, buf1);
dc6f92b8
JB
12356}
12357
0da1ab50 12358
e99db5a1
RS
12359/* This is the first-level handler for X protocol errors.
12360 It calls x_error_quitter or x_error_catcher. */
7a13e894 12361
8922af5f 12362static int
e99db5a1 12363x_error_handler (display, error)
8922af5f 12364 Display *display;
e99db5a1 12365 XErrorEvent *error;
8922af5f 12366{
e99db5a1
RS
12367 if (! NILP (x_error_message_string))
12368 x_error_catcher (display, error);
12369 else
12370 x_error_quitter (display, error);
06a2c219 12371 return 0;
f9e24cb9 12372}
c118dd06 12373
e99db5a1
RS
12374/* This is the handler for X IO errors, always.
12375 It kills all frames on the display that we lost touch with.
12376 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 12377
c118dd06 12378static int
e99db5a1 12379x_io_error_quitter (display)
c118dd06 12380 Display *display;
c118dd06 12381{
e99db5a1 12382 char buf[256];
dc6f92b8 12383
e99db5a1
RS
12384 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12385 x_connection_closed (display, buf);
06a2c219 12386 return 0;
dc6f92b8 12387}
dc6f92b8 12388\f
f451eb13
JB
12389/* Changing the font of the frame. */
12390
76bcdf39
RS
12391/* Give frame F the font named FONTNAME as its default font, and
12392 return the full name of that font. FONTNAME may be a wildcard
12393 pattern; in that case, we choose some font that fits the pattern.
12394 The return value shows which font we chose. */
12395
b5cf7a0e 12396Lisp_Object
f676886a
JB
12397x_new_font (f, fontname)
12398 struct frame *f;
dc6f92b8
JB
12399 register char *fontname;
12400{
dc43ef94 12401 struct font_info *fontp
ee569018 12402 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 12403
dc43ef94
KH
12404 if (!fontp)
12405 return Qnil;
2224a5fc 12406
dc43ef94 12407 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 12408 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94 12409 f->output_data.x->fontset = -1;
976b73d7
KS
12410
12411 x_compute_fringe_widths (f, 1);
12412
b2cad826
KH
12413 /* Compute the scroll bar width in character columns. */
12414 if (f->scroll_bar_pixel_width > 0)
12415 {
7556890b 12416 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
12417 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12418 }
12419 else
4e61bddf
RS
12420 {
12421 int wid = FONT_WIDTH (f->output_data.x->font);
12422 f->scroll_bar_cols = (14 + wid - 1) / wid;
12423 }
b2cad826 12424
f676886a 12425 /* Now make the frame display the given font. */
c118dd06 12426 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 12427 {
7556890b
RS
12428 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12429 f->output_data.x->font->fid);
12430 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12431 f->output_data.x->font->fid);
12432 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12433 f->output_data.x->font->fid);
f676886a 12434
a27f9f86 12435 frame_update_line_height (f);
3497f73e
GM
12436
12437 /* Don't change the size of a tip frame; there's no point in
12438 doing it because it's done in Fx_show_tip, and it leads to
12439 problems because the tip frame has no widget. */
12440 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12441 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 12442 }
a27f9f86
RS
12443 else
12444 /* If we are setting a new frame's font for the first time,
12445 there are no faces yet, so this font's height is the line height. */
7556890b 12446 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 12447
dc43ef94
KH
12448 return build_string (fontp->full_name);
12449}
12450
12451/* Give frame F the fontset named FONTSETNAME as its default font, and
12452 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
12453 pattern; in that case, we choose some fontset that fits the pattern.
12454 The return value shows which fontset we chose. */
b5cf7a0e 12455
dc43ef94
KH
12456Lisp_Object
12457x_new_fontset (f, fontsetname)
12458 struct frame *f;
12459 char *fontsetname;
12460{
ee569018 12461 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 12462 Lisp_Object result;
b5cf7a0e 12463
dc43ef94
KH
12464 if (fontset < 0)
12465 return Qnil;
b5cf7a0e 12466
2da424f1
KH
12467 if (f->output_data.x->fontset == fontset)
12468 /* This fontset is already set in frame F. There's nothing more
12469 to do. */
ee569018 12470 return fontset_name (fontset);
dc43ef94 12471
d5db4077 12472 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
dc43ef94
KH
12473
12474 if (!STRINGP (result))
12475 /* Can't load ASCII font. */
12476 return Qnil;
12477
12478 /* Since x_new_font doesn't update any fontset information, do it now. */
12479 f->output_data.x->fontset = fontset;
dc43ef94 12480
f5d11644
GM
12481#ifdef HAVE_X_I18N
12482 if (FRAME_XIC (f)
12483 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
d5db4077 12484 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
f5d11644
GM
12485#endif
12486
dc43ef94 12487 return build_string (fontsetname);
dc6f92b8 12488}
f5d11644 12489
976b73d7
KS
12490/* Compute actual fringe widths */
12491
12492void
12493x_compute_fringe_widths (f, redraw)
12494 struct frame *f;
12495 int redraw;
12496{
12497 int o_left = f->output_data.x->left_fringe_width;
12498 int o_right = f->output_data.x->right_fringe_width;
12499 int o_cols = f->output_data.x->fringe_cols;
12500
12501 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12502 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12503 int left_fringe_width, right_fringe_width;
12504
12505 if (!NILP (left_fringe))
12506 left_fringe = Fcdr (left_fringe);
12507 if (!NILP (right_fringe))
12508 right_fringe = Fcdr (right_fringe);
12509
12510 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12511 XINT (left_fringe));
12512 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12513 XINT (right_fringe));
12514
12515 if (left_fringe_width || right_fringe_width)
12516 {
12517 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12518 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12519 int conf_wid = left_wid + right_wid;
12520 int font_wid = FONT_WIDTH (f->output_data.x->font);
12521 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12522 int real_wid = cols * font_wid;
12523 if (left_wid && right_wid)
12524 {
12525 if (left_fringe_width < 0)
12526 {
12527 /* Left fringe width is fixed, adjust right fringe if necessary */
12528 f->output_data.x->left_fringe_width = left_wid;
12529 f->output_data.x->right_fringe_width = real_wid - left_wid;
12530 }
12531 else if (right_fringe_width < 0)
12532 {
12533 /* Right fringe width is fixed, adjust left fringe if necessary */
12534 f->output_data.x->left_fringe_width = real_wid - right_wid;
12535 f->output_data.x->right_fringe_width = right_wid;
12536 }
12537 else
12538 {
12539 /* Adjust both fringes with an equal amount.
12540 Note that we are doing integer arithmetic here, so don't
12541 lose a pixel if the total width is an odd number. */
12542 int fill = real_wid - conf_wid;
12543 f->output_data.x->left_fringe_width = left_wid + fill/2;
12544 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12545 }
12546 }
12547 else if (left_fringe_width)
12548 {
12549 f->output_data.x->left_fringe_width = real_wid;
12550 f->output_data.x->right_fringe_width = 0;
12551 }
12552 else
12553 {
12554 f->output_data.x->left_fringe_width = 0;
12555 f->output_data.x->right_fringe_width = real_wid;
12556 }
12557 f->output_data.x->fringe_cols = cols;
12558 f->output_data.x->fringes_extra = real_wid;
12559 }
12560 else
12561 {
12562 f->output_data.x->left_fringe_width = 0;
12563 f->output_data.x->right_fringe_width = 0;
12564 f->output_data.x->fringe_cols = 0;
12565 f->output_data.x->fringes_extra = 0;
12566 }
12567
12568 if (redraw && FRAME_VISIBLE_P (f))
12569 if (o_left != f->output_data.x->left_fringe_width ||
12570 o_right != f->output_data.x->right_fringe_width ||
12571 o_cols != f->output_data.x->fringe_cols)
12572 redraw_frame (f);
12573}
f5d11644
GM
12574\f
12575/***********************************************************************
12576 X Input Methods
12577 ***********************************************************************/
12578
12579#ifdef HAVE_X_I18N
12580
12581#ifdef HAVE_X11R6
12582
12583/* XIM destroy callback function, which is called whenever the
12584 connection to input method XIM dies. CLIENT_DATA contains a
12585 pointer to the x_display_info structure corresponding to XIM. */
12586
12587static void
12588xim_destroy_callback (xim, client_data, call_data)
12589 XIM xim;
12590 XPointer client_data;
12591 XPointer call_data;
12592{
12593 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12594 Lisp_Object frame, tail;
12595
12596 BLOCK_INPUT;
12597
12598 /* No need to call XDestroyIC.. */
12599 FOR_EACH_FRAME (tail, frame)
12600 {
12601 struct frame *f = XFRAME (frame);
12602 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12603 {
12604 FRAME_XIC (f) = NULL;
12605 if (FRAME_XIC_FONTSET (f))
12606 {
12607 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12608 FRAME_XIC_FONTSET (f) = NULL;
12609 }
12610 }
12611 }
12612
12613 /* No need to call XCloseIM. */
12614 dpyinfo->xim = NULL;
12615 XFree (dpyinfo->xim_styles);
12616 UNBLOCK_INPUT;
12617}
12618
12619#endif /* HAVE_X11R6 */
12620
12621/* Open the connection to the XIM server on display DPYINFO.
12622 RESOURCE_NAME is the resource name Emacs uses. */
12623
12624static void
12625xim_open_dpy (dpyinfo, resource_name)
12626 struct x_display_info *dpyinfo;
12627 char *resource_name;
12628{
287f7dd6 12629#ifdef USE_XIM
f5d11644
GM
12630 XIM xim;
12631
12632 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12633 dpyinfo->xim = xim;
12634
12635 if (xim)
12636 {
f5d11644
GM
12637#ifdef HAVE_X11R6
12638 XIMCallback destroy;
12639#endif
12640
12641 /* Get supported styles and XIM values. */
12642 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12643
12644#ifdef HAVE_X11R6
12645 destroy.callback = xim_destroy_callback;
12646 destroy.client_data = (XPointer)dpyinfo;
cea2ad76 12647 /* This isn't prototyped in OSF 5.0. */
f5d11644
GM
12648 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12649#endif
12650 }
287f7dd6
GM
12651
12652#else /* not USE_XIM */
12653 dpyinfo->xim = NULL;
12654#endif /* not USE_XIM */
f5d11644
GM
12655}
12656
12657
b9de836c 12658#ifdef HAVE_X11R6_XIM
f5d11644
GM
12659
12660struct xim_inst_t
12661{
12662 struct x_display_info *dpyinfo;
12663 char *resource_name;
12664};
12665
12666/* XIM instantiate callback function, which is called whenever an XIM
12667 server is available. DISPLAY is teh display of the XIM.
12668 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12669 when the callback was registered. */
12670
12671static void
12672xim_instantiate_callback (display, client_data, call_data)
12673 Display *display;
12674 XPointer client_data;
12675 XPointer call_data;
12676{
12677 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12678 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12679
12680 /* We don't support multiple XIM connections. */
12681 if (dpyinfo->xim)
12682 return;
12683
12684 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12685
12686 /* Create XIC for the existing frames on the same display, as long
12687 as they have no XIC. */
12688 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12689 {
12690 Lisp_Object tail, frame;
12691
12692 BLOCK_INPUT;
12693 FOR_EACH_FRAME (tail, frame)
12694 {
12695 struct frame *f = XFRAME (frame);
12696
12697 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12698 if (FRAME_XIC (f) == NULL)
12699 {
12700 create_frame_xic (f);
12701 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12702 xic_set_statusarea (f);
12703 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12704 {
12705 struct window *w = XWINDOW (f->selected_window);
12706 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12707 }
12708 }
12709 }
12710
12711 UNBLOCK_INPUT;
12712 }
12713}
12714
b9de836c 12715#endif /* HAVE_X11R6_XIM */
f5d11644
GM
12716
12717
12718/* Open a connection to the XIM server on display DPYINFO.
12719 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12720 connection only at the first time. On X11R6, open the connection
12721 in the XIM instantiate callback function. */
12722
12723static void
12724xim_initialize (dpyinfo, resource_name)
12725 struct x_display_info *dpyinfo;
12726 char *resource_name;
12727{
287f7dd6 12728#ifdef USE_XIM
b9de836c 12729#ifdef HAVE_X11R6_XIM
f5d11644
GM
12730 struct xim_inst_t *xim_inst;
12731 int len;
12732
12733 dpyinfo->xim = NULL;
12734 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12735 xim_inst->dpyinfo = dpyinfo;
12736 len = strlen (resource_name);
12737 xim_inst->resource_name = (char *) xmalloc (len + 1);
12738 bcopy (resource_name, xim_inst->resource_name, len + 1);
12739 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12740 resource_name, EMACS_CLASS,
12741 xim_instantiate_callback,
2ebb2f8b
DL
12742 /* Fixme: This is XPointer in
12743 XFree86 but (XPointer *) on
12744 Tru64, at least. */
12745 (XPointer) xim_inst);
b9de836c 12746#else /* not HAVE_X11R6_XIM */
f5d11644
GM
12747 dpyinfo->xim = NULL;
12748 xim_open_dpy (dpyinfo, resource_name);
b9de836c 12749#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
12750
12751#else /* not USE_XIM */
12752 dpyinfo->xim = NULL;
12753#endif /* not USE_XIM */
f5d11644
GM
12754}
12755
12756
12757/* Close the connection to the XIM server on display DPYINFO. */
12758
12759static void
12760xim_close_dpy (dpyinfo)
12761 struct x_display_info *dpyinfo;
12762{
287f7dd6 12763#ifdef USE_XIM
b9de836c 12764#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
12765 if (dpyinfo->display)
12766 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12767 NULL, EMACS_CLASS,
12768 xim_instantiate_callback, NULL);
b9de836c 12769#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
12770 if (dpyinfo->display)
12771 XCloseIM (dpyinfo->xim);
f5d11644
GM
12772 dpyinfo->xim = NULL;
12773 XFree (dpyinfo->xim_styles);
287f7dd6 12774#endif /* USE_XIM */
f5d11644
GM
12775}
12776
b9de836c 12777#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
12778
12779
dc6f92b8 12780\f
2e365682
RS
12781/* Calculate the absolute position in frame F
12782 from its current recorded position values and gravity. */
12783
dfcf069d 12784void
43bca5d5 12785x_calc_absolute_position (f)
f676886a 12786 struct frame *f;
dc6f92b8 12787{
06a2c219 12788 Window child;
6dba1858 12789 int win_x = 0, win_y = 0;
7556890b 12790 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
12791 int this_window;
12792
9829ddba
RS
12793 /* We have nothing to do if the current position
12794 is already for the top-left corner. */
12795 if (! ((flags & XNegative) || (flags & YNegative)))
12796 return;
12797
c81412a0 12798#ifdef USE_X_TOOLKIT
7556890b 12799 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
12800#else
12801 this_window = FRAME_X_WINDOW (f);
12802#endif
6dba1858
RS
12803
12804 /* Find the position of the outside upper-left corner of
9829ddba
RS
12805 the inner window, with respect to the outer window.
12806 But do this only if we will need the results. */
7556890b 12807 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 12808 {
9829ddba
RS
12809 int count;
12810
6dba1858 12811 BLOCK_INPUT;
9829ddba
RS
12812 count = x_catch_errors (FRAME_X_DISPLAY (f));
12813 while (1)
12814 {
12815 x_clear_errors (FRAME_X_DISPLAY (f));
12816 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12817
12818 /* From-window, to-window. */
12819 this_window,
12820 f->output_data.x->parent_desc,
12821
12822 /* From-position, to-position. */
12823 0, 0, &win_x, &win_y,
12824
12825 /* Child of win. */
12826 &child);
12827 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12828 {
12829 Window newroot, newparent = 0xdeadbeef;
12830 Window *newchildren;
2ebb2f8b 12831 unsigned int nchildren;
9829ddba
RS
12832
12833 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12834 &newparent, &newchildren, &nchildren))
12835 break;
58769bee 12836
7c3c78a3 12837 XFree ((char *) newchildren);
6dba1858 12838
9829ddba
RS
12839 f->output_data.x->parent_desc = newparent;
12840 }
12841 else
12842 break;
12843 }
6dba1858 12844
9829ddba 12845 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
12846 UNBLOCK_INPUT;
12847 }
12848
12849 /* Treat negative positions as relative to the leftmost bottommost
12850 position that fits on the screen. */
20f55f9a 12851 if (flags & XNegative)
7556890b 12852 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
12853 - 2 * f->output_data.x->border_width - win_x
12854 - PIXEL_WIDTH (f)
12855 + f->output_data.x->left_pos);
dc6f92b8 12856
7708ced0
GM
12857 {
12858 int height = PIXEL_HEIGHT (f);
06a2c219 12859
7708ced0
GM
12860#if defined USE_X_TOOLKIT && defined USE_MOTIF
12861 /* Something is fishy here. When using Motif, starting Emacs with
12862 `-g -0-0', the frame appears too low by a few pixels.
12863
12864 This seems to be so because initially, while Emacs is starting,
12865 the column widget's height and the frame's pixel height are
12866 different. The column widget's height is the right one. In
12867 later invocations, when Emacs is up, the frame's pixel height
12868 is right, though.
12869
12870 It's not obvious where the initial small difference comes from.
12871 2000-12-01, gerd. */
12872
12873 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 12874#endif
2e365682 12875
7708ced0
GM
12876 if (flags & YNegative)
12877 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12878 - 2 * f->output_data.x->border_width
12879 - win_y
12880 - height
12881 + f->output_data.x->top_pos);
12882 }
12883
3a35ab44
RS
12884 /* The left_pos and top_pos
12885 are now relative to the top and left screen edges,
12886 so the flags should correspond. */
7556890b 12887 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
12888}
12889
3a35ab44
RS
12890/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12891 to really change the position, and 0 when calling from
12892 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
12893 position values). It is -1 when calling from x_set_frame_parameters,
12894 which means, do adjust for borders but don't change the gravity. */
3a35ab44 12895
dfcf069d 12896void
dc05a16b 12897x_set_offset (f, xoff, yoff, change_gravity)
f676886a 12898 struct frame *f;
dc6f92b8 12899 register int xoff, yoff;
dc05a16b 12900 int change_gravity;
dc6f92b8 12901{
4a4cbdd5
KH
12902 int modified_top, modified_left;
12903
aa3ff7c9 12904 if (change_gravity > 0)
3a35ab44 12905 {
7556890b
RS
12906 f->output_data.x->top_pos = yoff;
12907 f->output_data.x->left_pos = xoff;
12908 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 12909 if (xoff < 0)
7556890b 12910 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 12911 if (yoff < 0)
7556890b
RS
12912 f->output_data.x->size_hint_flags |= YNegative;
12913 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 12914 }
43bca5d5 12915 x_calc_absolute_position (f);
dc6f92b8
JB
12916
12917 BLOCK_INPUT;
c32cdd9a 12918 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 12919
7556890b
RS
12920 modified_left = f->output_data.x->left_pos;
12921 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
12922#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12923 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12924 /* It is a mystery why we need to add the border_width here
12925 when the frame is already visible, but experiment says we do. */
aa3ff7c9 12926 if (change_gravity != 0)
4a4cbdd5 12927 {
7556890b
RS
12928 modified_left += f->output_data.x->border_width;
12929 modified_top += f->output_data.x->border_width;
4a4cbdd5 12930 }
e73ec6fa 12931#endif
4a4cbdd5 12932
3afe33e7 12933#ifdef USE_X_TOOLKIT
7556890b 12934 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 12935 modified_left, modified_top);
3afe33e7 12936#else /* not USE_X_TOOLKIT */
334208b7 12937 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 12938 modified_left, modified_top);
3afe33e7 12939#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12940 UNBLOCK_INPUT;
12941}
12942
d0fa3e56
EZ
12943/* Check if we need to resize the frame due to a fullscreen request.
12944 If so needed, resize the frame. */
12945static void
12946x_check_fullscreen (f)
12947 struct frame *f;
12948{
12949 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12950 {
12951 int width, height, ign;
12952
12953 x_real_positions (f, &f->output_data.x->left_pos,
12954 &f->output_data.x->top_pos);
12955
12956 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12957
12958 /* We do not need to move the window, it shall be taken care of
12959 when setting WM manager hints.
12960 If the frame is visible already, the position is checked by
12961 x_check_fullscreen_move. */
12962 if (f->width != width || f->height != height)
12963 {
12964 change_frame_size (f, height, width, 0, 1, 0);
12965 SET_FRAME_GARBAGED (f);
12966 cancel_mouse_face (f);
12967
12968 /* Wait for the change of frame size to occur */
12969 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12970
12971 }
12972 }
12973}
12974
12975/* If frame parameters are set after the frame is mapped, we need to move
12976 the window. This is done in xfns.c.
12977 Some window managers moves the window to the right position, some
12978 moves the outer window manager window to the specified position.
12979 Here we check that we are in the right spot. If not, make a second
12980 move, assuming we are dealing with the second kind of window manager. */
12981static void
12982x_check_fullscreen_move (f)
12983 struct frame *f;
12984{
12985 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12986 {
12987 int expect_top = f->output_data.x->top_pos;
12988 int expect_left = f->output_data.x->left_pos;
12989
12990 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12991 expect_top = 0;
12992 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12993 expect_left = 0;
12994
12995 if (expect_top != f->output_data.x->top_pos
12996 || expect_left != f->output_data.x->left_pos)
12997 x_set_offset (f, expect_left, expect_top, 1);
12998
12999 /* Just do this once */
13000 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
13001 }
13002}
13003
13004
13005/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
13006 wanted positions of the WM window (not emacs window).
13007 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
13008 window (FRAME_X_WINDOW).
13009 */
13010void
13011x_fullscreen_adjust (f, width, height, top_pos, left_pos)
13012 struct frame *f;
13013 int *width;
13014 int *height;
13015 int *top_pos;
13016 int *left_pos;
13017{
13018 int newwidth = f->width, newheight = f->height;
13019
13020 *top_pos = f->output_data.x->top_pos;
13021 *left_pos = f->output_data.x->left_pos;
13022
13023 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13024 {
13025 int ph;
13026
13027 ph = FRAME_X_DISPLAY_INFO (f)->height;
13028 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13029 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
13030 - f->output_data.x->y_pixels_diff;
13031 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13032 *top_pos = 0;
13033 }
13034
13035 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13036 {
13037 int pw;
13038
13039 pw = FRAME_X_DISPLAY_INFO (f)->width;
13040 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13041 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
13042 - f->output_data.x->x_pixels_diff;
13043 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13044 *left_pos = 0;
13045 }
13046
13047 *width = newwidth;
13048 *height = newheight;
13049}
13050
dc6f92b8 13051
499b1844
GM
13052/* Change the size of frame F's X window to COLS/ROWS in the case F
13053 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
13054 top-left-corner window gravity for this size change and subsequent
13055 size changes. Otherwise we leave the window gravity unchanged. */
13056
13057static void
13058x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 13059 struct frame *f;
bc20ebbf 13060 int change_gravity;
b1c884c3 13061 int cols, rows;
dc6f92b8
JB
13062{
13063 int pixelwidth, pixelheight;
80fd1fe2 13064
b1c884c3 13065 check_frame_size (f, &rows, &cols);
7556890b 13066 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
13067 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
13068 ? 0
13069 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 13070 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 13071 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
976b73d7
KS
13072
13073 x_compute_fringe_widths (f, 0);
13074
f451eb13
JB
13075 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
13076 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 13077
7556890b 13078 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 13079 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 13080
334208b7
RS
13081 XSync (FRAME_X_DISPLAY (f), False);
13082 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13083 pixelwidth, pixelheight);
b1c884c3
JB
13084
13085 /* Now, strictly speaking, we can't be sure that this is accurate,
13086 but the window manager will get around to dealing with the size
13087 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
13088 ConfigureNotify event gets here.
13089
13090 We could just not bother storing any of this information here,
13091 and let the ConfigureNotify event set everything up, but that
fddd5ceb 13092 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 13093 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
13094 point in the future when the ConfigureNotify event arrives.
13095
13096 We pass 1 for DELAY since we can't run Lisp code inside of
13097 a BLOCK_INPUT. */
7d1e984f 13098 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
13099 PIXEL_WIDTH (f) = pixelwidth;
13100 PIXEL_HEIGHT (f) = pixelheight;
13101
aee9a898
RS
13102 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
13103 receive in the ConfigureNotify event; if we get what we asked
13104 for, then the event won't cause the screen to become garbaged, so
13105 we have to make sure to do it here. */
13106 SET_FRAME_GARBAGED (f);
13107
13108 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
13109}
13110
13111
13112/* Call this to change the size of frame F's x-window.
13113 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
13114 for this size change and subsequent size changes.
13115 Otherwise we leave the window gravity unchanged. */
aee9a898 13116
499b1844
GM
13117void
13118x_set_window_size (f, change_gravity, cols, rows)
13119 struct frame *f;
13120 int change_gravity;
13121 int cols, rows;
13122{
13123 BLOCK_INPUT;
13124
13125#ifdef USE_X_TOOLKIT
13126
f1f4d345 13127 if (f->output_data.x->widget != NULL)
499b1844
GM
13128 {
13129 /* The x and y position of the widget is clobbered by the
13130 call to XtSetValues within EmacsFrameSetCharSize.
13131 This is a real kludge, but I don't understand Xt so I can't
13132 figure out a correct fix. Can anyone else tell me? -- rms. */
13133 int xpos = f->output_data.x->widget->core.x;
13134 int ypos = f->output_data.x->widget->core.y;
13135 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13136 f->output_data.x->widget->core.x = xpos;
13137 f->output_data.x->widget->core.y = ypos;
13138 }
13139 else
13140 x_set_window_size_1 (f, change_gravity, cols, rows);
13141
13142#else /* not USE_X_TOOLKIT */
13143
13144 x_set_window_size_1 (f, change_gravity, cols, rows);
13145
aee9a898
RS
13146#endif /* not USE_X_TOOLKIT */
13147
4d73d038 13148 /* If cursor was outside the new size, mark it as off. */
06a2c219 13149 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 13150
aee9a898
RS
13151 /* Clear out any recollection of where the mouse highlighting was,
13152 since it might be in a place that's outside the new frame size.
13153 Actually checking whether it is outside is a pain in the neck,
13154 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 13155 cancel_mouse_face (f);
dbc4e1c1 13156
dc6f92b8
JB
13157 UNBLOCK_INPUT;
13158}
dc6f92b8 13159\f
d047c4eb 13160/* Mouse warping. */
dc6f92b8 13161
9b378208 13162void
f676886a
JB
13163x_set_mouse_position (f, x, y)
13164 struct frame *f;
dc6f92b8
JB
13165 int x, y;
13166{
13167 int pix_x, pix_y;
13168
7556890b
RS
13169 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13170 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
13171
13172 if (pix_x < 0) pix_x = 0;
13173 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13174
13175 if (pix_y < 0) pix_y = 0;
13176 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
13177
13178 BLOCK_INPUT;
dc6f92b8 13179
334208b7
RS
13180 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13181 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
13182 UNBLOCK_INPUT;
13183}
13184
9b378208
RS
13185/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13186
13187void
13188x_set_mouse_pixel_position (f, pix_x, pix_y)
13189 struct frame *f;
13190 int pix_x, pix_y;
13191{
13192 BLOCK_INPUT;
13193
334208b7
RS
13194 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13195 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
13196 UNBLOCK_INPUT;
13197}
d047c4eb
KH
13198\f
13199/* focus shifting, raising and lowering. */
9b378208 13200
dfcf069d 13201void
f676886a
JB
13202x_focus_on_frame (f)
13203 struct frame *f;
dc6f92b8 13204{
1fb20991 13205#if 0 /* This proves to be unpleasant. */
f676886a 13206 x_raise_frame (f);
1fb20991 13207#endif
6d4238f3
JB
13208#if 0
13209 /* I don't think that the ICCCM allows programs to do things like this
13210 without the interaction of the window manager. Whatever you end up
f676886a 13211 doing with this code, do it to x_unfocus_frame too. */
334208b7 13212 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 13213 RevertToPointerRoot, CurrentTime);
c118dd06 13214#endif /* ! 0 */
dc6f92b8
JB
13215}
13216
dfcf069d 13217void
f676886a
JB
13218x_unfocus_frame (f)
13219 struct frame *f;
dc6f92b8 13220{
6d4238f3 13221#if 0
f676886a 13222 /* Look at the remarks in x_focus_on_frame. */
0f941935 13223 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 13224 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 13225 RevertToPointerRoot, CurrentTime);
c118dd06 13226#endif /* ! 0 */
dc6f92b8
JB
13227}
13228
f676886a 13229/* Raise frame F. */
dc6f92b8 13230
dfcf069d 13231void
f676886a
JB
13232x_raise_frame (f)
13233 struct frame *f;
dc6f92b8 13234{
3a88c238 13235 if (f->async_visible)
dc6f92b8
JB
13236 {
13237 BLOCK_INPUT;
3afe33e7 13238#ifdef USE_X_TOOLKIT
7556890b 13239 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13240#else /* not USE_X_TOOLKIT */
334208b7 13241 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13242#endif /* not USE_X_TOOLKIT */
334208b7 13243 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13244 UNBLOCK_INPUT;
13245 }
13246}
13247
f676886a 13248/* Lower frame F. */
dc6f92b8 13249
dfcf069d 13250void
f676886a
JB
13251x_lower_frame (f)
13252 struct frame *f;
dc6f92b8 13253{
3a88c238 13254 if (f->async_visible)
dc6f92b8
JB
13255 {
13256 BLOCK_INPUT;
3afe33e7 13257#ifdef USE_X_TOOLKIT
7556890b 13258 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 13259#else /* not USE_X_TOOLKIT */
334208b7 13260 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13261#endif /* not USE_X_TOOLKIT */
334208b7 13262 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
13263 UNBLOCK_INPUT;
13264 }
13265}
13266
dbc4e1c1 13267static void
6b0442dc 13268XTframe_raise_lower (f, raise_flag)
dbc4e1c1 13269 FRAME_PTR f;
6b0442dc 13270 int raise_flag;
dbc4e1c1 13271{
6b0442dc 13272 if (raise_flag)
dbc4e1c1
JB
13273 x_raise_frame (f);
13274 else
13275 x_lower_frame (f);
13276}
d047c4eb
KH
13277\f
13278/* Change of visibility. */
dc6f92b8 13279
9382638d
KH
13280/* This tries to wait until the frame is really visible.
13281 However, if the window manager asks the user where to position
13282 the frame, this will return before the user finishes doing that.
13283 The frame will not actually be visible at that time,
13284 but it will become visible later when the window manager
13285 finishes with it. */
13286
dfcf069d 13287void
f676886a
JB
13288x_make_frame_visible (f)
13289 struct frame *f;
dc6f92b8 13290{
990ba854 13291 Lisp_Object type;
1aa6072f 13292 int original_top, original_left;
31be9251
GM
13293 int retry_count = 2;
13294
13295 retry:
dc6f92b8 13296
dc6f92b8 13297 BLOCK_INPUT;
dc6f92b8 13298
990ba854
RS
13299 type = x_icon_type (f);
13300 if (!NILP (type))
13301 x_bitmap_icon (f, type);
bdcd49ba 13302
f676886a 13303 if (! FRAME_VISIBLE_P (f))
90e65f07 13304 {
1aa6072f
RS
13305 /* We test FRAME_GARBAGED_P here to make sure we don't
13306 call x_set_offset a second time
13307 if we get to x_make_frame_visible a second time
13308 before the window gets really visible. */
13309 if (! FRAME_ICONIFIED_P (f)
13310 && ! f->output_data.x->asked_for_visible)
13311 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13312
13313 f->output_data.x->asked_for_visible = 1;
13314
90e65f07 13315 if (! EQ (Vx_no_window_manager, Qt))
f676886a 13316 x_wm_set_window_state (f, NormalState);
3afe33e7 13317#ifdef USE_X_TOOLKIT
d7a38a2e 13318 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13319 XtMapWidget (f->output_data.x->widget);
3afe33e7 13320#else /* not USE_X_TOOLKIT */
7f9c7f94 13321 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 13322#endif /* not USE_X_TOOLKIT */
0134a210
RS
13323#if 0 /* This seems to bring back scroll bars in the wrong places
13324 if the window configuration has changed. They seem
13325 to come back ok without this. */
ab648270 13326 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 13327 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 13328#endif
90e65f07 13329 }
dc6f92b8 13330
334208b7 13331 XFlush (FRAME_X_DISPLAY (f));
90e65f07 13332
0dacf791
RS
13333 /* Synchronize to ensure Emacs knows the frame is visible
13334 before we do anything else. We do this loop with input not blocked
13335 so that incoming events are handled. */
13336 {
13337 Lisp_Object frame;
12ce2351 13338 int count;
28c01ffe
RS
13339 /* This must be before UNBLOCK_INPUT
13340 since events that arrive in response to the actions above
13341 will set it when they are handled. */
13342 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
13343
13344 original_left = f->output_data.x->left_pos;
13345 original_top = f->output_data.x->top_pos;
c0a04927
RS
13346
13347 /* This must come after we set COUNT. */
13348 UNBLOCK_INPUT;
13349
2745e6c4 13350 /* We unblock here so that arriving X events are processed. */
1aa6072f 13351
dcb07ae9
RS
13352 /* Now move the window back to where it was "supposed to be".
13353 But don't do it if the gravity is negative.
13354 When the gravity is negative, this uses a position
28c01ffe
RS
13355 that is 3 pixels too low. Perhaps that's really the border width.
13356
13357 Don't do this if the window has never been visible before,
13358 because the window manager may choose the position
13359 and we don't want to override it. */
1aa6072f 13360
4d3f5d9a 13361 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 13362 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 13363 && previously_visible)
1aa6072f 13364 {
2745e6c4
RS
13365 Drawable rootw;
13366 int x, y;
13367 unsigned int width, height, border, depth;
06a2c219 13368
1aa6072f 13369 BLOCK_INPUT;
9829ddba 13370
06a2c219
GM
13371 /* On some window managers (such as FVWM) moving an existing
13372 window, even to the same place, causes the window manager
13373 to introduce an offset. This can cause the window to move
13374 to an unexpected location. Check the geometry (a little
13375 slow here) and then verify that the window is in the right
13376 place. If the window is not in the right place, move it
13377 there, and take the potential window manager hit. */
2745e6c4
RS
13378 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13379 &rootw, &x, &y, &width, &height, &border, &depth);
13380
13381 if (original_left != x || original_top != y)
13382 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13383 original_left, original_top);
13384
1aa6072f
RS
13385 UNBLOCK_INPUT;
13386 }
9829ddba 13387
e0c1aef2 13388 XSETFRAME (frame, f);
c0a04927 13389
12ce2351
GM
13390 /* Wait until the frame is visible. Process X events until a
13391 MapNotify event has been seen, or until we think we won't get a
13392 MapNotify at all.. */
13393 for (count = input_signal_count + 10;
13394 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 13395 {
12ce2351 13396 /* Force processing of queued events. */
334208b7 13397 x_sync (f);
12ce2351
GM
13398
13399 /* Machines that do polling rather than SIGIO have been
13400 observed to go into a busy-wait here. So we'll fake an
13401 alarm signal to let the handler know that there's something
13402 to be read. We used to raise a real alarm, but it seems
13403 that the handler isn't always enabled here. This is
13404 probably a bug. */
8b2f8d4e 13405 if (input_polling_used ())
3b2fa4e6 13406 {
12ce2351
GM
13407 /* It could be confusing if a real alarm arrives while
13408 processing the fake one. Turn it off and let the
13409 handler reset it. */
3e71d8f2 13410 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
13411 int old_poll_suppress_count = poll_suppress_count;
13412 poll_suppress_count = 1;
13413 poll_for_input_1 ();
13414 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 13415 }
12ce2351
GM
13416
13417 /* See if a MapNotify event has been processed. */
13418 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 13419 }
31be9251
GM
13420
13421 /* 2000-09-28: In
13422
13423 (let ((f (selected-frame)))
13424 (iconify-frame f)
13425 (raise-frame f))
13426
13427 the frame is not raised with various window managers on
13428 FreeBSD, Linux and Solaris. It turns out that, for some
13429 unknown reason, the call to XtMapWidget is completely ignored.
13430 Mapping the widget a second time works. */
13431
13432 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13433 goto retry;
0dacf791 13434 }
dc6f92b8
JB
13435}
13436
06a2c219 13437/* Change from mapped state to withdrawn state. */
dc6f92b8 13438
d047c4eb
KH
13439/* Make the frame visible (mapped and not iconified). */
13440
dfcf069d 13441void
f676886a
JB
13442x_make_frame_invisible (f)
13443 struct frame *f;
dc6f92b8 13444{
546e6d5b
RS
13445 Window window;
13446
13447#ifdef USE_X_TOOLKIT
13448 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 13449 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
13450#else /* not USE_X_TOOLKIT */
13451 window = FRAME_X_WINDOW (f);
13452#endif /* not USE_X_TOOLKIT */
dc6f92b8 13453
9319ae23 13454 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13455 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13456 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13457
5627c40e 13458#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 13459 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 13460 return;
5627c40e 13461#endif
dc6f92b8
JB
13462
13463 BLOCK_INPUT;
c118dd06 13464
af31d76f
RS
13465 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13466 that the current position of the window is user-specified, rather than
13467 program-specified, so that when the window is mapped again, it will be
13468 placed at the same location, without forcing the user to position it
13469 by hand again (they have already done that once for this window.) */
c32cdd9a 13470 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 13471
c118dd06
JB
13472#ifdef HAVE_X11R4
13473
334208b7
RS
13474 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13475 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
13476 {
13477 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13478 error ("Can't notify window manager of window withdrawal");
c118dd06 13479 }
c118dd06 13480#else /* ! defined (HAVE_X11R4) */
16bd92ea 13481
c118dd06 13482 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
13483 if (! EQ (Vx_no_window_manager, Qt))
13484 {
16bd92ea 13485 XEvent unmap;
dc6f92b8 13486
16bd92ea 13487 unmap.xunmap.type = UnmapNotify;
546e6d5b 13488 unmap.xunmap.window = window;
334208b7 13489 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 13490 unmap.xunmap.from_configure = False;
334208b7
RS
13491 if (! XSendEvent (FRAME_X_DISPLAY (f),
13492 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 13493 False,
06a2c219 13494 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
13495 &unmap))
13496 {
13497 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13498 error ("Can't notify window manager of withdrawal");
16bd92ea 13499 }
dc6f92b8
JB
13500 }
13501
16bd92ea 13502 /* Unmap the window ourselves. Cheeky! */
334208b7 13503 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 13504#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 13505
5627c40e
RS
13506 /* We can't distinguish this from iconification
13507 just by the event that we get from the server.
13508 So we can't win using the usual strategy of letting
13509 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13510 and synchronize with the server to make sure we agree. */
13511 f->visible = 0;
13512 FRAME_ICONIFIED_P (f) = 0;
13513 f->async_visible = 0;
13514 f->async_iconified = 0;
13515
334208b7 13516 x_sync (f);
5627c40e 13517
dc6f92b8
JB
13518 UNBLOCK_INPUT;
13519}
13520
06a2c219 13521/* Change window state from mapped to iconified. */
dc6f92b8 13522
dfcf069d 13523void
f676886a
JB
13524x_iconify_frame (f)
13525 struct frame *f;
dc6f92b8 13526{
3afe33e7 13527 int result;
990ba854 13528 Lisp_Object type;
dc6f92b8 13529
9319ae23 13530 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
13531 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13532 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 13533
3a88c238 13534 if (f->async_iconified)
dc6f92b8
JB
13535 return;
13536
3afe33e7 13537 BLOCK_INPUT;
546e6d5b 13538
9af3143a
RS
13539 FRAME_SAMPLE_VISIBILITY (f);
13540
990ba854
RS
13541 type = x_icon_type (f);
13542 if (!NILP (type))
13543 x_bitmap_icon (f, type);
bdcd49ba
RS
13544
13545#ifdef USE_X_TOOLKIT
13546
546e6d5b
RS
13547 if (! FRAME_VISIBLE_P (f))
13548 {
13549 if (! EQ (Vx_no_window_manager, Qt))
13550 x_wm_set_window_state (f, IconicState);
13551 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 13552 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
13553 /* The server won't give us any event to indicate
13554 that an invisible frame was changed to an icon,
13555 so we have to record it here. */
13556 f->iconified = 1;
1e6bc770 13557 f->visible = 1;
9cf30a30 13558 f->async_iconified = 1;
1e6bc770 13559 f->async_visible = 0;
546e6d5b
RS
13560 UNBLOCK_INPUT;
13561 return;
13562 }
13563
334208b7 13564 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 13565 XtWindow (f->output_data.x->widget),
334208b7 13566 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
13567 UNBLOCK_INPUT;
13568
13569 if (!result)
546e6d5b 13570 error ("Can't notify window manager of iconification");
3afe33e7
RS
13571
13572 f->async_iconified = 1;
1e6bc770
RS
13573 f->async_visible = 0;
13574
8c002a25
KH
13575
13576 BLOCK_INPUT;
334208b7 13577 XFlush (FRAME_X_DISPLAY (f));
8c002a25 13578 UNBLOCK_INPUT;
3afe33e7
RS
13579#else /* not USE_X_TOOLKIT */
13580
fd13dbb2
RS
13581 /* Make sure the X server knows where the window should be positioned,
13582 in case the user deiconifies with the window manager. */
13583 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 13584 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 13585
16bd92ea
JB
13586 /* Since we don't know which revision of X we're running, we'll use both
13587 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13588
13589 /* X11R4: send a ClientMessage to the window manager using the
13590 WM_CHANGE_STATE type. */
13591 {
13592 XEvent message;
58769bee 13593
c118dd06 13594 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 13595 message.xclient.type = ClientMessage;
334208b7 13596 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
13597 message.xclient.format = 32;
13598 message.xclient.data.l[0] = IconicState;
13599
334208b7
RS
13600 if (! XSendEvent (FRAME_X_DISPLAY (f),
13601 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
13602 False,
13603 SubstructureRedirectMask | SubstructureNotifyMask,
13604 &message))
dc6f92b8
JB
13605 {
13606 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 13607 error ("Can't notify window manager of iconification");
dc6f92b8 13608 }
16bd92ea 13609 }
dc6f92b8 13610
58769bee 13611 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
13612 IconicState. */
13613 x_wm_set_window_state (f, IconicState);
dc6f92b8 13614
a9c00105
RS
13615 if (!FRAME_VISIBLE_P (f))
13616 {
13617 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 13618 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
13619 }
13620
3a88c238 13621 f->async_iconified = 1;
1e6bc770 13622 f->async_visible = 0;
dc6f92b8 13623
334208b7 13624 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 13625 UNBLOCK_INPUT;
8c002a25 13626#endif /* not USE_X_TOOLKIT */
dc6f92b8 13627}
19f71add 13628
d047c4eb 13629\f
19f71add 13630/* Free X resources of frame F. */
dc6f92b8 13631
dfcf069d 13632void
19f71add 13633x_free_frame_resources (f)
f676886a 13634 struct frame *f;
dc6f92b8 13635{
7f9c7f94 13636 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
13637 Lisp_Object bar;
13638 struct scroll_bar *b;
7f9c7f94 13639
dc6f92b8 13640 BLOCK_INPUT;
c0ff3fab 13641
6186a4a0
RS
13642 /* If a display connection is dead, don't try sending more
13643 commands to the X server. */
19f71add 13644 if (dpyinfo->display)
6186a4a0 13645 {
19f71add 13646 if (f->output_data.x->icon_desc)
6186a4a0 13647 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
13648
13649#ifdef USE_X_TOOLKIT
13650 /* Explicitly destroy the scroll bars of the frame. Without
13651 this, we get "BadDrawable" errors from the toolkit later on,
13652 presumably from expose events generated for the disappearing
13653 toolkit scroll bars. */
13654 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13655 {
13656 b = XSCROLL_BAR (bar);
13657 x_scroll_bar_remove (b);
13658 }
13659#endif
13660
31f41daf 13661#ifdef HAVE_X_I18N
f5d11644
GM
13662 if (FRAME_XIC (f))
13663 free_frame_xic (f);
31f41daf 13664#endif
c6ea2775 13665
3afe33e7 13666#ifdef USE_X_TOOLKIT
06a2c219 13667 if (f->output_data.x->widget)
30ca89f5
GM
13668 {
13669 XtDestroyWidget (f->output_data.x->widget);
13670 f->output_data.x->widget = NULL;
13671 }
c6ea2775
EZ
13672 /* Tooltips don't have widgets, only a simple X window, even if
13673 we are using a toolkit. */
13674 else if (FRAME_X_WINDOW (f))
13675 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13676
6186a4a0 13677 free_frame_menubar (f);
c6ea2775
EZ
13678#else /* !USE_X_TOOLKIT */
13679 if (FRAME_X_WINDOW (f))
13680 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13681#endif /* !USE_X_TOOLKIT */
3afe33e7 13682
3e71d8f2
GM
13683 unload_color (f, f->output_data.x->foreground_pixel);
13684 unload_color (f, f->output_data.x->background_pixel);
13685 unload_color (f, f->output_data.x->cursor_pixel);
13686 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13687 unload_color (f, f->output_data.x->border_pixel);
13688 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 13689
3e71d8f2
GM
13690 if (f->output_data.x->scroll_bar_background_pixel != -1)
13691 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13692 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13693 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
13694#ifdef USE_TOOLKIT_SCROLL_BARS
13695 /* Scrollbar shadow colors. */
13696 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13697 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13698 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13699 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13700#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
13701 if (f->output_data.x->white_relief.allocated_p)
13702 unload_color (f, f->output_data.x->white_relief.pixel);
13703 if (f->output_data.x->black_relief.allocated_p)
13704 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 13705
19f71add
GM
13706 if (FRAME_FACE_CACHE (f))
13707 free_frame_faces (f);
13708
4ca78676 13709 x_free_gcs (f);
6186a4a0
RS
13710 XFlush (FRAME_X_DISPLAY (f));
13711 }
dc6f92b8 13712
df89d8a4 13713 if (f->output_data.x->saved_menu_event)
06a2c219 13714 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 13715
7556890b 13716 xfree (f->output_data.x);
19f71add
GM
13717 f->output_data.x = NULL;
13718
0f941935
KH
13719 if (f == dpyinfo->x_focus_frame)
13720 dpyinfo->x_focus_frame = 0;
13721 if (f == dpyinfo->x_focus_event_frame)
13722 dpyinfo->x_focus_event_frame = 0;
13723 if (f == dpyinfo->x_highlight_frame)
13724 dpyinfo->x_highlight_frame = 0;
c0ff3fab 13725
7f9c7f94 13726 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 13727 {
7f9c7f94
RS
13728 dpyinfo->mouse_face_beg_row
13729 = dpyinfo->mouse_face_beg_col = -1;
13730 dpyinfo->mouse_face_end_row
13731 = dpyinfo->mouse_face_end_col = -1;
13732 dpyinfo->mouse_face_window = Qnil;
21323706
RS
13733 dpyinfo->mouse_face_deferred_gc = 0;
13734 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 13735 }
0134a210 13736
c0ff3fab 13737 UNBLOCK_INPUT;
dc6f92b8 13738}
19f71add
GM
13739
13740
13741/* Destroy the X window of frame F. */
13742
13743void
13744x_destroy_window (f)
13745 struct frame *f;
13746{
13747 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13748
13749 /* If a display connection is dead, don't try sending more
13750 commands to the X server. */
13751 if (dpyinfo->display != 0)
13752 x_free_frame_resources (f);
13753
13754 dpyinfo->reference_count--;
13755}
13756
dc6f92b8 13757\f
f451eb13
JB
13758/* Setting window manager hints. */
13759
af31d76f
RS
13760/* Set the normal size hints for the window manager, for frame F.
13761 FLAGS is the flags word to use--or 0 meaning preserve the flags
13762 that the window now has.
13763 If USER_POSITION is nonzero, we set the USPosition
13764 flag (this is useful when FLAGS is 0). */
6dba1858 13765
dfcf069d 13766void
af31d76f 13767x_wm_set_size_hint (f, flags, user_position)
f676886a 13768 struct frame *f;
af31d76f
RS
13769 long flags;
13770 int user_position;
dc6f92b8
JB
13771{
13772 XSizeHints size_hints;
3afe33e7
RS
13773
13774#ifdef USE_X_TOOLKIT
7e4f2521
FP
13775 Arg al[2];
13776 int ac = 0;
13777 Dimension widget_width, widget_height;
7556890b 13778 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 13779#else /* not USE_X_TOOLKIT */
c118dd06 13780 Window window = FRAME_X_WINDOW (f);
3afe33e7 13781#endif /* not USE_X_TOOLKIT */
dc6f92b8 13782
b72a58fd
RS
13783 /* Setting PMaxSize caused various problems. */
13784 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 13785
7556890b
RS
13786 size_hints.x = f->output_data.x->left_pos;
13787 size_hints.y = f->output_data.x->top_pos;
7553a6b7 13788
7e4f2521
FP
13789#ifdef USE_X_TOOLKIT
13790 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13791 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 13792 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
13793 size_hints.height = widget_height;
13794 size_hints.width = widget_width;
13795#else /* not USE_X_TOOLKIT */
f676886a
JB
13796 size_hints.height = PIXEL_HEIGHT (f);
13797 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 13798#endif /* not USE_X_TOOLKIT */
7553a6b7 13799
7556890b
RS
13800 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13801 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
13802 size_hints.max_width
13803 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13804 size_hints.max_height
13805 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 13806
d067ea8b
KH
13807 /* Calculate the base and minimum sizes.
13808
13809 (When we use the X toolkit, we don't do it here.
13810 Instead we copy the values that the widgets are using, below.) */
13811#ifndef USE_X_TOOLKIT
b1c884c3 13812 {
b0342f17 13813 int base_width, base_height;
0134a210 13814 int min_rows = 0, min_cols = 0;
b0342f17 13815
f451eb13
JB
13816 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13817 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 13818
0134a210 13819 check_frame_size (f, &min_rows, &min_cols);
b0342f17 13820
0134a210
RS
13821 /* The window manager uses the base width hints to calculate the
13822 current number of rows and columns in the frame while
13823 resizing; min_width and min_height aren't useful for this
13824 purpose, since they might not give the dimensions for a
13825 zero-row, zero-column frame.
58769bee 13826
0134a210
RS
13827 We use the base_width and base_height members if we have
13828 them; otherwise, we set the min_width and min_height members
13829 to the size for a zero x zero frame. */
b0342f17
JB
13830
13831#ifdef HAVE_X11R4
0134a210
RS
13832 size_hints.flags |= PBaseSize;
13833 size_hints.base_width = base_width;
13834 size_hints.base_height = base_height;
13835 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13836 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 13837#else
0134a210
RS
13838 size_hints.min_width = base_width;
13839 size_hints.min_height = base_height;
b0342f17 13840#endif
b1c884c3 13841 }
dc6f92b8 13842
d067ea8b 13843 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 13844 if (flags)
dc6f92b8 13845 {
d067ea8b
KH
13846 size_hints.flags |= flags;
13847 goto no_read;
13848 }
13849#endif /* not USE_X_TOOLKIT */
13850
13851 {
13852 XSizeHints hints; /* Sometimes I hate X Windows... */
13853 long supplied_return;
13854 int value;
af31d76f
RS
13855
13856#ifdef HAVE_X11R4
d067ea8b
KH
13857 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13858 &supplied_return);
af31d76f 13859#else
d067ea8b 13860 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 13861#endif
58769bee 13862
d067ea8b
KH
13863#ifdef USE_X_TOOLKIT
13864 size_hints.base_height = hints.base_height;
13865 size_hints.base_width = hints.base_width;
13866 size_hints.min_height = hints.min_height;
13867 size_hints.min_width = hints.min_width;
13868#endif
13869
13870 if (flags)
13871 size_hints.flags |= flags;
13872 else
13873 {
13874 if (value == 0)
13875 hints.flags = 0;
13876 if (hints.flags & PSize)
13877 size_hints.flags |= PSize;
13878 if (hints.flags & PPosition)
13879 size_hints.flags |= PPosition;
13880 if (hints.flags & USPosition)
13881 size_hints.flags |= USPosition;
13882 if (hints.flags & USSize)
13883 size_hints.flags |= USSize;
13884 }
13885 }
13886
06a2c219 13887#ifndef USE_X_TOOLKIT
d067ea8b 13888 no_read:
06a2c219 13889#endif
0134a210 13890
af31d76f 13891#ifdef PWinGravity
7556890b 13892 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 13893 size_hints.flags |= PWinGravity;
dc05a16b 13894
af31d76f 13895 if (user_position)
6dba1858 13896 {
af31d76f
RS
13897 size_hints.flags &= ~ PPosition;
13898 size_hints.flags |= USPosition;
6dba1858 13899 }
2554751d 13900#endif /* PWinGravity */
6dba1858 13901
b0342f17 13902#ifdef HAVE_X11R4
334208b7 13903 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13904#else
334208b7 13905 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 13906#endif
dc6f92b8
JB
13907}
13908
13909/* Used for IconicState or NormalState */
06a2c219 13910
dfcf069d 13911void
f676886a
JB
13912x_wm_set_window_state (f, state)
13913 struct frame *f;
dc6f92b8
JB
13914 int state;
13915{
3afe33e7 13916#ifdef USE_X_TOOLKIT
546e6d5b
RS
13917 Arg al[1];
13918
13919 XtSetArg (al[0], XtNinitialState, state);
7556890b 13920 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 13921#else /* not USE_X_TOOLKIT */
c118dd06 13922 Window window = FRAME_X_WINDOW (f);
dc6f92b8 13923
7556890b
RS
13924 f->output_data.x->wm_hints.flags |= StateHint;
13925 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 13926
7556890b 13927 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 13928#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13929}
13930
dfcf069d 13931void
7f2ae036 13932x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 13933 struct frame *f;
7f2ae036 13934 int pixmap_id;
dc6f92b8 13935{
d2bd6bc4
RS
13936 Pixmap icon_pixmap;
13937
06a2c219 13938#ifndef USE_X_TOOLKIT
c118dd06 13939 Window window = FRAME_X_WINDOW (f);
75231bad 13940#endif
dc6f92b8 13941
7f2ae036 13942 if (pixmap_id > 0)
dbc4e1c1 13943 {
d2bd6bc4 13944 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 13945 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
13946 }
13947 else
68568555
RS
13948 {
13949 /* It seems there is no way to turn off use of an icon pixmap.
13950 The following line does it, only if no icon has yet been created,
13951 for some window managers. But with mwm it crashes.
13952 Some people say it should clear the IconPixmapHint bit in this case,
13953 but that doesn't work, and the X consortium said it isn't the
13954 right thing at all. Since there is no way to win,
13955 best to explicitly give up. */
13956#if 0
13957 f->output_data.x->wm_hints.icon_pixmap = None;
13958#else
13959 return;
13960#endif
13961 }
b1c884c3 13962
d2bd6bc4
RS
13963#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13964
13965 {
13966 Arg al[1];
13967 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13968 XtSetValues (f->output_data.x->widget, al, 1);
13969 }
13970
13971#else /* not USE_X_TOOLKIT */
13972
7556890b
RS
13973 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13974 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
13975
13976#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13977}
13978
dfcf069d 13979void
f676886a
JB
13980x_wm_set_icon_position (f, icon_x, icon_y)
13981 struct frame *f;
dc6f92b8
JB
13982 int icon_x, icon_y;
13983{
75231bad 13984#ifdef USE_X_TOOLKIT
7556890b 13985 Window window = XtWindow (f->output_data.x->widget);
75231bad 13986#else
c118dd06 13987 Window window = FRAME_X_WINDOW (f);
75231bad 13988#endif
dc6f92b8 13989
7556890b
RS
13990 f->output_data.x->wm_hints.flags |= IconPositionHint;
13991 f->output_data.x->wm_hints.icon_x = icon_x;
13992 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 13993
7556890b 13994 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
13995}
13996
13997\f
06a2c219
GM
13998/***********************************************************************
13999 Fonts
14000 ***********************************************************************/
dc43ef94
KH
14001
14002/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 14003
dc43ef94
KH
14004struct font_info *
14005x_get_font_info (f, font_idx)
14006 FRAME_PTR f;
14007 int font_idx;
14008{
14009 return (FRAME_X_FONT_TABLE (f) + font_idx);
14010}
14011
14012
9c11f79e
GM
14013/* Return a list of names of available fonts matching PATTERN on frame F.
14014
14015 If SIZE is > 0, it is the size (maximum bounds width) of fonts
14016 to be listed.
14017
14018 SIZE < 0 means include scalable fonts.
14019
14020 Frame F null means we have not yet created any frame on X, and
14021 consult the first display in x_display_list. MAXNAMES sets a limit
14022 on how many fonts to match. */
dc43ef94
KH
14023
14024Lisp_Object
14025x_list_fonts (f, pattern, size, maxnames)
9c11f79e 14026 struct frame *f;
dc43ef94
KH
14027 Lisp_Object pattern;
14028 int size;
14029 int maxnames;
14030{
06a2c219
GM
14031 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
14032 Lisp_Object tem, second_best;
9c11f79e
GM
14033 struct x_display_info *dpyinfo
14034 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
14035 Display *dpy = dpyinfo->display;
09c6077f 14036 int try_XLoadQueryFont = 0;
53ca4657 14037 int count;
9c11f79e
GM
14038 int allow_scalable_fonts_p = 0;
14039
14040 if (size < 0)
14041 {
14042 allow_scalable_fonts_p = 1;
14043 size = 0;
14044 }
dc43ef94 14045
6b0efe73 14046 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
14047 if (NILP (patterns))
14048 patterns = Fcons (pattern, Qnil);
81ba44e5 14049
09c6077f
KH
14050 if (maxnames == 1 && !size)
14051 /* We can return any single font matching PATTERN. */
14052 try_XLoadQueryFont = 1;
9a32686f 14053
8e713be6 14054 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 14055 {
dc43ef94 14056 int num_fonts;
3e71d8f2 14057 char **names = NULL;
dc43ef94 14058
8e713be6 14059 pattern = XCAR (patterns);
536f4067
RS
14060 /* See if we cached the result for this particular query.
14061 The cache is an alist of the form:
9c11f79e
GM
14062 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
14063 tem = XCDR (dpyinfo->name_list_element);
14064 key = Fcons (Fcons (pattern, make_number (maxnames)),
14065 allow_scalable_fonts_p ? Qt : Qnil);
14066 list = Fassoc (key, tem);
14067 if (!NILP (list))
b5210ea7
KH
14068 {
14069 list = Fcdr_safe (list);
14070 /* We have a cashed list. Don't have to get the list again. */
14071 goto label_cached;
14072 }
14073
14074 /* At first, put PATTERN in the cache. */
09c6077f 14075
dc43ef94 14076 BLOCK_INPUT;
17d85edc
KH
14077 count = x_catch_errors (dpy);
14078
09c6077f
KH
14079 if (try_XLoadQueryFont)
14080 {
14081 XFontStruct *font;
14082 unsigned long value;
14083
d5db4077 14084 font = XLoadQueryFont (dpy, SDATA (pattern));
17d85edc
KH
14085 if (x_had_errors_p (dpy))
14086 {
14087 /* This error is perhaps due to insufficient memory on X
14088 server. Let's just ignore it. */
14089 font = NULL;
14090 x_clear_errors (dpy);
14091 }
14092
09c6077f
KH
14093 if (font
14094 && XGetFontProperty (font, XA_FONT, &value))
14095 {
14096 char *name = (char *) XGetAtomName (dpy, (Atom) value);
14097 int len = strlen (name);
01c752b5 14098 char *tmp;
09c6077f 14099
6f6512e8
KH
14100 /* If DXPC (a Differential X Protocol Compressor)
14101 Ver.3.7 is running, XGetAtomName will return null
14102 string. We must avoid such a name. */
14103 if (len == 0)
14104 try_XLoadQueryFont = 0;
14105 else
14106 {
14107 num_fonts = 1;
14108 names = (char **) alloca (sizeof (char *));
14109 /* Some systems only allow alloca assigned to a
14110 simple var. */
14111 tmp = (char *) alloca (len + 1); names[0] = tmp;
14112 bcopy (name, names[0], len + 1);
14113 XFree (name);
14114 }
09c6077f
KH
14115 }
14116 else
14117 try_XLoadQueryFont = 0;
a083fd23
RS
14118
14119 if (font)
14120 XFreeFont (dpy, font);
09c6077f
KH
14121 }
14122
14123 if (!try_XLoadQueryFont)
17d85edc
KH
14124 {
14125 /* We try at least 10 fonts because XListFonts will return
14126 auto-scaled fonts at the head. */
d5db4077 14127 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
17d85edc
KH
14128 &num_fonts);
14129 if (x_had_errors_p (dpy))
14130 {
14131 /* This error is perhaps due to insufficient memory on X
14132 server. Let's just ignore it. */
14133 names = NULL;
14134 x_clear_errors (dpy);
14135 }
14136 }
14137
14138 x_uncatch_errors (dpy, count);
dc43ef94
KH
14139 UNBLOCK_INPUT;
14140
14141 if (names)
14142 {
14143 int i;
dc43ef94
KH
14144
14145 /* Make a list of all the fonts we got back.
14146 Store that in the font cache for the display. */
14147 for (i = 0; i < num_fonts; i++)
14148 {
06a2c219 14149 int width = 0;
dc43ef94 14150 char *p = names[i];
06a2c219
GM
14151 int average_width = -1, dashes = 0;
14152
dc43ef94 14153 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
14154 14 dashes, and the field value following 12th dash
14155 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14156 is usually too ugly to be used for editing. Let's
14157 ignore it. */
dc43ef94
KH
14158 while (*p)
14159 if (*p++ == '-')
14160 {
14161 dashes++;
14162 if (dashes == 7) /* PIXEL_SIZE field */
14163 width = atoi (p);
14164 else if (dashes == 12) /* AVERAGE_WIDTH field */
14165 average_width = atoi (p);
14166 }
9c11f79e
GM
14167
14168 if (allow_scalable_fonts_p
14169 || dashes < 14 || average_width != 0)
dc43ef94
KH
14170 {
14171 tem = build_string (names[i]);
14172 if (NILP (Fassoc (tem, list)))
14173 {
14174 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
14175 && ((fast_c_string_match_ignore_case
14176 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
14177 >= 0))
14178 /* We can set the value of PIXEL_SIZE to the
b5210ea7 14179 width of this font. */
dc43ef94
KH
14180 list = Fcons (Fcons (tem, make_number (width)), list);
14181 else
14182 /* For the moment, width is not known. */
14183 list = Fcons (Fcons (tem, Qnil), list);
14184 }
14185 }
14186 }
e38f4136 14187
09c6077f 14188 if (!try_XLoadQueryFont)
e38f4136
GM
14189 {
14190 BLOCK_INPUT;
14191 XFreeFontNames (names);
14192 UNBLOCK_INPUT;
14193 }
dc43ef94
KH
14194 }
14195
b5210ea7 14196 /* Now store the result in the cache. */
f3fbd155
KR
14197 XSETCDR (dpyinfo->name_list_element,
14198 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 14199
b5210ea7
KH
14200 label_cached:
14201 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 14202
b5210ea7
KH
14203 newlist = second_best = Qnil;
14204 /* Make a list of the fonts that have the right width. */
8e713be6 14205 for (; CONSP (list); list = XCDR (list))
b5210ea7 14206 {
536f4067
RS
14207 int found_size;
14208
8e713be6 14209 tem = XCAR (list);
dc43ef94 14210
8e713be6 14211 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
14212 continue;
14213 if (!size)
14214 {
8e713be6 14215 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
14216 continue;
14217 }
dc43ef94 14218
8e713be6 14219 if (!INTEGERP (XCDR (tem)))
dc43ef94 14220 {
b5210ea7 14221 /* Since we have not yet known the size of this font, we
9c11f79e 14222 must try slow function call XLoadQueryFont. */
dc43ef94
KH
14223 XFontStruct *thisinfo;
14224
14225 BLOCK_INPUT;
17d85edc 14226 count = x_catch_errors (dpy);
dc43ef94 14227 thisinfo = XLoadQueryFont (dpy,
d5db4077 14228 SDATA (XCAR (tem)));
17d85edc
KH
14229 if (x_had_errors_p (dpy))
14230 {
14231 /* This error is perhaps due to insufficient memory on X
14232 server. Let's just ignore it. */
14233 thisinfo = NULL;
14234 x_clear_errors (dpy);
14235 }
14236 x_uncatch_errors (dpy, count);
dc43ef94
KH
14237 UNBLOCK_INPUT;
14238
14239 if (thisinfo)
14240 {
f3fbd155
KR
14241 XSETCDR (tem,
14242 (thisinfo->min_bounds.width == 0
14243 ? make_number (0)
14244 : make_number (thisinfo->max_bounds.width)));
e38f4136 14245 BLOCK_INPUT;
dc43ef94 14246 XFreeFont (dpy, thisinfo);
e38f4136 14247 UNBLOCK_INPUT;
dc43ef94
KH
14248 }
14249 else
b5210ea7 14250 /* For unknown reason, the previous call of XListFont had
06a2c219 14251 returned a font which can't be opened. Record the size
b5210ea7 14252 as 0 not to try to open it again. */
f3fbd155 14253 XSETCDR (tem, make_number (0));
dc43ef94 14254 }
536f4067 14255
8e713be6 14256 found_size = XINT (XCDR (tem));
536f4067 14257 if (found_size == size)
8e713be6 14258 newlist = Fcons (XCAR (tem), newlist);
536f4067 14259 else if (found_size > 0)
b5210ea7 14260 {
536f4067 14261 if (NILP (second_best))
b5210ea7 14262 second_best = tem;
536f4067
RS
14263 else if (found_size < size)
14264 {
8e713be6
KR
14265 if (XINT (XCDR (second_best)) > size
14266 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
14267 second_best = tem;
14268 }
14269 else
14270 {
8e713be6
KR
14271 if (XINT (XCDR (second_best)) > size
14272 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
14273 second_best = tem;
14274 }
b5210ea7
KH
14275 }
14276 }
14277 if (!NILP (newlist))
14278 break;
14279 else if (!NILP (second_best))
14280 {
8e713be6 14281 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 14282 break;
dc43ef94 14283 }
dc43ef94
KH
14284 }
14285
14286 return newlist;
14287}
14288
06a2c219
GM
14289
14290#if GLYPH_DEBUG
14291
14292/* Check that FONT is valid on frame F. It is if it can be found in F's
14293 font table. */
14294
14295static void
14296x_check_font (f, font)
14297 struct frame *f;
14298 XFontStruct *font;
14299{
14300 int i;
14301 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14302
14303 xassert (font != NULL);
14304
14305 for (i = 0; i < dpyinfo->n_fonts; i++)
14306 if (dpyinfo->font_table[i].name
14307 && font == dpyinfo->font_table[i].font)
14308 break;
14309
14310 xassert (i < dpyinfo->n_fonts);
14311}
14312
14313#endif /* GLYPH_DEBUG != 0 */
14314
14315/* Set *W to the minimum width, *H to the minimum font height of FONT.
14316 Note: There are (broken) X fonts out there with invalid XFontStruct
14317 min_bounds contents. For example, handa@etl.go.jp reports that
14318 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14319 have font->min_bounds.width == 0. */
14320
14321static INLINE void
14322x_font_min_bounds (font, w, h)
14323 XFontStruct *font;
14324 int *w, *h;
14325{
14326 *h = FONT_HEIGHT (font);
14327 *w = font->min_bounds.width;
14328
14329 /* Try to handle the case where FONT->min_bounds has invalid
14330 contents. Since the only font known to have invalid min_bounds
14331 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14332 if (*w <= 0)
14333 *w = font->max_bounds.width;
14334}
14335
14336
14337/* Compute the smallest character width and smallest font height over
14338 all fonts available on frame F. Set the members smallest_char_width
14339 and smallest_font_height in F's x_display_info structure to
14340 the values computed. Value is non-zero if smallest_font_height or
14341 smallest_char_width become smaller than they were before. */
14342
14343static int
14344x_compute_min_glyph_bounds (f)
14345 struct frame *f;
14346{
14347 int i;
14348 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14349 XFontStruct *font;
14350 int old_width = dpyinfo->smallest_char_width;
14351 int old_height = dpyinfo->smallest_font_height;
14352
14353 dpyinfo->smallest_font_height = 100000;
14354 dpyinfo->smallest_char_width = 100000;
14355
14356 for (i = 0; i < dpyinfo->n_fonts; ++i)
14357 if (dpyinfo->font_table[i].name)
14358 {
14359 struct font_info *fontp = dpyinfo->font_table + i;
14360 int w, h;
14361
14362 font = (XFontStruct *) fontp->font;
14363 xassert (font != (XFontStruct *) ~0);
14364 x_font_min_bounds (font, &w, &h);
14365
14366 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14367 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14368 }
14369
14370 xassert (dpyinfo->smallest_char_width > 0
14371 && dpyinfo->smallest_font_height > 0);
14372
14373 return (dpyinfo->n_fonts == 1
14374 || dpyinfo->smallest_char_width < old_width
14375 || dpyinfo->smallest_font_height < old_height);
14376}
14377
14378
dc43ef94
KH
14379/* Load font named FONTNAME of the size SIZE for frame F, and return a
14380 pointer to the structure font_info while allocating it dynamically.
14381 If SIZE is 0, load any size of font.
14382 If loading is failed, return NULL. */
14383
14384struct font_info *
14385x_load_font (f, fontname, size)
14386 struct frame *f;
14387 register char *fontname;
14388 int size;
14389{
14390 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14391 Lisp_Object font_names;
d645aaa4 14392 int count;
dc43ef94
KH
14393
14394 /* Get a list of all the fonts that match this name. Once we
14395 have a list of matching fonts, we compare them against the fonts
14396 we already have by comparing names. */
09c6077f 14397 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
14398
14399 if (!NILP (font_names))
14400 {
14401 Lisp_Object tail;
14402 int i;
14403
14404 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 14405 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
14406 if (dpyinfo->font_table[i].name
14407 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 14408 SDATA (XCAR (tail)))
06a2c219 14409 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 14410 SDATA (XCAR (tail)))))
dc43ef94
KH
14411 return (dpyinfo->font_table + i);
14412 }
14413
14414 /* Load the font and add it to the table. */
14415 {
14416 char *full_name;
14417 XFontStruct *font;
14418 struct font_info *fontp;
14419 unsigned long value;
06a2c219 14420 int i;
dc43ef94 14421
2da424f1
KH
14422 /* If we have found fonts by x_list_font, load one of them. If
14423 not, we still try to load a font by the name given as FONTNAME
14424 because XListFonts (called in x_list_font) of some X server has
14425 a bug of not finding a font even if the font surely exists and
14426 is loadable by XLoadQueryFont. */
e1d6d5b9 14427 if (size > 0 && !NILP (font_names))
d5db4077 14428 fontname = (char *) SDATA (XCAR (font_names));
dc43ef94
KH
14429
14430 BLOCK_INPUT;
d645aaa4 14431 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 14432 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
14433 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14434 {
14435 /* This error is perhaps due to insufficient memory on X
14436 server. Let's just ignore it. */
14437 font = NULL;
14438 x_clear_errors (FRAME_X_DISPLAY (f));
14439 }
14440 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 14441 UNBLOCK_INPUT;
b5210ea7 14442 if (!font)
dc43ef94
KH
14443 return NULL;
14444
06a2c219
GM
14445 /* Find a free slot in the font table. */
14446 for (i = 0; i < dpyinfo->n_fonts; ++i)
14447 if (dpyinfo->font_table[i].name == NULL)
14448 break;
14449
14450 /* If no free slot found, maybe enlarge the font table. */
14451 if (i == dpyinfo->n_fonts
14452 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 14453 {
06a2c219
GM
14454 int sz;
14455 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14456 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 14457 dpyinfo->font_table
06a2c219 14458 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
14459 }
14460
06a2c219
GM
14461 fontp = dpyinfo->font_table + i;
14462 if (i == dpyinfo->n_fonts)
14463 ++dpyinfo->n_fonts;
dc43ef94
KH
14464
14465 /* Now fill in the slots of *FONTP. */
14466 BLOCK_INPUT;
14467 fontp->font = font;
06a2c219 14468 fontp->font_idx = i;
dc43ef94
KH
14469 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14470 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14471
14472 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14473 full_name = 0;
14474 if (XGetFontProperty (font, XA_FONT, &value))
14475 {
14476 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14477 char *p = name;
14478 int dashes = 0;
14479
14480 /* Count the number of dashes in the "full name".
14481 If it is too few, this isn't really the font's full name,
14482 so don't use it.
14483 In X11R4, the fonts did not come with their canonical names
14484 stored in them. */
14485 while (*p)
14486 {
14487 if (*p == '-')
14488 dashes++;
14489 p++;
14490 }
14491
14492 if (dashes >= 13)
14493 {
14494 full_name = (char *) xmalloc (p - name + 1);
14495 bcopy (name, full_name, p - name + 1);
14496 }
14497
14498 XFree (name);
14499 }
14500
14501 if (full_name != 0)
14502 fontp->full_name = full_name;
14503 else
14504 fontp->full_name = fontp->name;
14505
14506 fontp->size = font->max_bounds.width;
d5749adb 14507 fontp->height = FONT_HEIGHT (font);
dc43ef94 14508
2da424f1
KH
14509 if (NILP (font_names))
14510 {
14511 /* We come here because of a bug of XListFonts mentioned at
14512 the head of this block. Let's store this information in
14513 the cache for x_list_fonts. */
14514 Lisp_Object lispy_name = build_string (fontname);
14515 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
14516 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14517 Qnil);
2da424f1 14518
f3fbd155
KR
14519 XSETCDR (dpyinfo->name_list_element,
14520 Fcons (Fcons (key,
14521 Fcons (Fcons (lispy_full_name,
14522 make_number (fontp->size)),
14523 Qnil)),
14524 XCDR (dpyinfo->name_list_element)));
2da424f1 14525 if (full_name)
9c11f79e
GM
14526 {
14527 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14528 Qnil);
f3fbd155
KR
14529 XSETCDR (dpyinfo->name_list_element,
14530 Fcons (Fcons (key,
14531 Fcons (Fcons (lispy_full_name,
14532 make_number (fontp->size)),
14533 Qnil)),
14534 XCDR (dpyinfo->name_list_element)));
9c11f79e 14535 }
2da424f1
KH
14536 }
14537
dc43ef94
KH
14538 /* The slot `encoding' specifies how to map a character
14539 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
14540 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14541 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 14542 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 14543 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
14544 which is never used by any charset. If mapping can't be
14545 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
14546 fontp->encoding[1]
14547 = (font->max_byte1 == 0
14548 /* 1-byte font */
14549 ? (font->min_char_or_byte2 < 0x80
14550 ? (font->max_char_or_byte2 < 0x80
14551 ? 0 /* 0x20..0x7F */
8ff102bd 14552 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
14553 : 1) /* 0xA0..0xFF */
14554 /* 2-byte font */
14555 : (font->min_byte1 < 0x80
14556 ? (font->max_byte1 < 0x80
14557 ? (font->min_char_or_byte2 < 0x80
14558 ? (font->max_char_or_byte2 < 0x80
14559 ? 0 /* 0x2020..0x7F7F */
8ff102bd 14560 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 14561 : 3) /* 0x20A0..0x7FFF */
8ff102bd 14562 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
14563 : (font->min_char_or_byte2 < 0x80
14564 ? (font->max_char_or_byte2 < 0x80
14565 ? 2 /* 0xA020..0xFF7F */
8ff102bd 14566 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
14567 : 1))); /* 0xA0A0..0xFFFF */
14568
14569 fontp->baseline_offset
14570 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14571 ? (long) value : 0);
14572 fontp->relative_compose
14573 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14574 ? (long) value : 0);
f78798df
KH
14575 fontp->default_ascent
14576 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14577 ? (long) value : 0);
dc43ef94 14578
06a2c219
GM
14579 /* Set global flag fonts_changed_p to non-zero if the font loaded
14580 has a character with a smaller width than any other character
437dfb9f 14581 before, or if the font loaded has a smaller height than any
06a2c219
GM
14582 other font loaded before. If this happens, it will make a
14583 glyph matrix reallocation necessary. */
437dfb9f 14584 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 14585 UNBLOCK_INPUT;
dc43ef94
KH
14586 return fontp;
14587 }
14588}
14589
06a2c219
GM
14590
14591/* Return a pointer to struct font_info of a font named FONTNAME for
14592 frame F. If no such font is loaded, return NULL. */
14593
dc43ef94
KH
14594struct font_info *
14595x_query_font (f, fontname)
14596 struct frame *f;
14597 register char *fontname;
14598{
14599 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14600 int i;
14601
14602 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
14603 if (dpyinfo->font_table[i].name
14604 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14605 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
14606 return (dpyinfo->font_table + i);
14607 return NULL;
14608}
14609
06a2c219
GM
14610
14611/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
14612 `encoder' of the structure. */
14613
14614void
14615x_find_ccl_program (fontp)
14616 struct font_info *fontp;
14617{
a42f54e6 14618 Lisp_Object list, elt;
a6582676 14619
f9b5db02 14620 elt = Qnil;
8e713be6 14621 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 14622 {
8e713be6 14623 elt = XCAR (list);
a6582676 14624 if (CONSP (elt)
8e713be6 14625 && STRINGP (XCAR (elt))
9f2feff6
KH
14626 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14627 >= 0)
14628 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14629 >= 0)))
a42f54e6
KH
14630 break;
14631 }
f9b5db02 14632
a42f54e6
KH
14633 if (! NILP (list))
14634 {
d27f8ca7
KH
14635 struct ccl_program *ccl
14636 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 14637
8e713be6 14638 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
14639 xfree (ccl);
14640 else
14641 fontp->font_encoder = ccl;
a6582676
KH
14642 }
14643}
14644
06a2c219 14645
dc43ef94 14646\f
06a2c219
GM
14647/***********************************************************************
14648 Initialization
14649 ***********************************************************************/
f451eb13 14650
3afe33e7
RS
14651#ifdef USE_X_TOOLKIT
14652static XrmOptionDescRec emacs_options[] = {
14653 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14654 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14655
14656 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14657 XrmoptionSepArg, NULL},
14658 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14659
14660 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14661 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14662 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14663 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14664 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14665 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14666 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14667};
14668#endif /* USE_X_TOOLKIT */
14669
7a13e894
RS
14670static int x_initialized;
14671
29b38361
KH
14672#ifdef MULTI_KBOARD
14673/* Test whether two display-name strings agree up to the dot that separates
14674 the screen number from the server number. */
14675static int
14676same_x_server (name1, name2)
59ab1dc7 14677 const char *name1, *name2;
29b38361
KH
14678{
14679 int seen_colon = 0;
59ab1dc7 14680 const unsigned char *system_name = SDATA (Vsystem_name);
cf591cc1
RS
14681 int system_name_length = strlen (system_name);
14682 int length_until_period = 0;
14683
14684 while (system_name[length_until_period] != 0
14685 && system_name[length_until_period] != '.')
14686 length_until_period++;
14687
14688 /* Treat `unix' like an empty host name. */
14689 if (! strncmp (name1, "unix:", 5))
14690 name1 += 4;
14691 if (! strncmp (name2, "unix:", 5))
14692 name2 += 4;
14693 /* Treat this host's name like an empty host name. */
14694 if (! strncmp (name1, system_name, system_name_length)
14695 && name1[system_name_length] == ':')
14696 name1 += system_name_length;
14697 if (! strncmp (name2, system_name, system_name_length)
14698 && name2[system_name_length] == ':')
14699 name2 += system_name_length;
14700 /* Treat this host's domainless name like an empty host name. */
14701 if (! strncmp (name1, system_name, length_until_period)
14702 && name1[length_until_period] == ':')
14703 name1 += length_until_period;
14704 if (! strncmp (name2, system_name, length_until_period)
14705 && name2[length_until_period] == ':')
14706 name2 += length_until_period;
14707
29b38361
KH
14708 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14709 {
14710 if (*name1 == ':')
14711 seen_colon++;
14712 if (seen_colon && *name1 == '.')
14713 return 1;
14714 }
14715 return (seen_colon
14716 && (*name1 == '.' || *name1 == '\0')
14717 && (*name2 == '.' || *name2 == '\0'));
14718}
14719#endif
14720
334208b7 14721struct x_display_info *
1f8255f2 14722x_term_init (display_name, xrm_option, resource_name)
334208b7 14723 Lisp_Object display_name;
1f8255f2
RS
14724 char *xrm_option;
14725 char *resource_name;
dc6f92b8 14726{
334208b7 14727 int connection;
7a13e894 14728 Display *dpy;
334208b7
RS
14729 struct x_display_info *dpyinfo;
14730 XrmDatabase xrdb;
14731
60439948
KH
14732 BLOCK_INPUT;
14733
7a13e894
RS
14734 if (!x_initialized)
14735 {
14736 x_initialize ();
14737 x_initialized = 1;
14738 }
dc6f92b8 14739
3afe33e7 14740#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
14741 /* weiner@footloose.sps.mot.com reports that this causes
14742 errors with X11R5:
14743 X protocol error: BadAtom (invalid Atom parameter)
14744 on protocol request 18skiloaf.
14745 So let's not use it until R6. */
14746#ifdef HAVE_X11XTR6
bdcd49ba
RS
14747 XtSetLanguageProc (NULL, NULL, NULL);
14748#endif
14749
7f9c7f94
RS
14750 {
14751 int argc = 0;
14752 char *argv[3];
14753
14754 argv[0] = "";
14755 argc = 1;
14756 if (xrm_option)
14757 {
14758 argv[argc++] = "-xrm";
14759 argv[argc++] = xrm_option;
14760 }
5dbb3f46 14761 stop_polling ();
d5db4077 14762 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
7f9c7f94
RS
14763 resource_name, EMACS_CLASS,
14764 emacs_options, XtNumber (emacs_options),
14765 &argc, argv);
5dbb3f46 14766 start_polling ();
39d8bb4d
KH
14767
14768#ifdef HAVE_X11XTR6
10537cb1 14769 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 14770 fixup_locale ();
39d8bb4d 14771#endif
7f9c7f94 14772 }
3afe33e7
RS
14773
14774#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
14775#ifdef HAVE_X11R5
14776 XSetLocaleModifiers ("");
14777#endif
d5db4077 14778 dpy = XOpenDisplay (SDATA (display_name));
3afe33e7 14779#endif /* not USE_X_TOOLKIT */
334208b7 14780
7a13e894
RS
14781 /* Detect failure. */
14782 if (dpy == 0)
60439948
KH
14783 {
14784 UNBLOCK_INPUT;
14785 return 0;
14786 }
7a13e894
RS
14787
14788 /* We have definitely succeeded. Record the new connection. */
14789
14790 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 14791 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 14792
29b38361
KH
14793#ifdef MULTI_KBOARD
14794 {
14795 struct x_display_info *share;
14796 Lisp_Object tail;
14797
14798 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6 14799 share = share->next, tail = XCDR (tail))
d5db4077
KR
14800 if (same_x_server (SDATA (XCAR (XCAR (tail))),
14801 SDATA (display_name)))
29b38361
KH
14802 break;
14803 if (share)
14804 dpyinfo->kboard = share->kboard;
14805 else
14806 {
14807 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14808 init_kboard (dpyinfo->kboard);
59e755be
KH
14809 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14810 {
14811 char *vendor = ServerVendor (dpy);
9b6ed9f3 14812 UNBLOCK_INPUT;
59e755be
KH
14813 dpyinfo->kboard->Vsystem_key_alist
14814 = call1 (Qvendor_specific_keysyms,
14815 build_string (vendor ? vendor : ""));
9b6ed9f3 14816 BLOCK_INPUT;
59e755be
KH
14817 }
14818
29b38361
KH
14819 dpyinfo->kboard->next_kboard = all_kboards;
14820 all_kboards = dpyinfo->kboard;
0ad5446c
KH
14821 /* Don't let the initial kboard remain current longer than necessary.
14822 That would cause problems if a file loaded on startup tries to
06a2c219 14823 prompt in the mini-buffer. */
0ad5446c
KH
14824 if (current_kboard == initial_kboard)
14825 current_kboard = dpyinfo->kboard;
29b38361
KH
14826 }
14827 dpyinfo->kboard->reference_count++;
14828 }
b9737ad3
KH
14829#endif
14830
7a13e894
RS
14831 /* Put this display on the chain. */
14832 dpyinfo->next = x_display_list;
14833 x_display_list = dpyinfo;
14834
14835 /* Put it on x_display_name_list as well, to keep them parallel. */
14836 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14837 x_display_name_list);
8e713be6 14838 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
14839
14840 dpyinfo->display = dpy;
dc6f92b8 14841
dc6f92b8 14842#if 0
7a13e894 14843 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 14844#endif /* ! 0 */
7a13e894
RS
14845
14846 dpyinfo->x_id_name
d5db4077
KR
14847 = (char *) xmalloc (SBYTES (Vinvocation_name)
14848 + SBYTES (Vsystem_name)
7a13e894
RS
14849 + 2);
14850 sprintf (dpyinfo->x_id_name, "%s@%s",
d5db4077 14851 SDATA (Vinvocation_name), SDATA (Vsystem_name));
28430d3c
JB
14852
14853 /* Figure out which modifier bits mean what. */
334208b7 14854 x_find_modifier_meanings (dpyinfo);
f451eb13 14855
ab648270 14856 /* Get the scroll bar cursor. */
7a13e894 14857 dpyinfo->vertical_scroll_bar_cursor
334208b7 14858 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 14859
334208b7
RS
14860 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14861 resource_name, EMACS_CLASS);
14862#ifdef HAVE_XRMSETDATABASE
14863 XrmSetDatabase (dpyinfo->display, xrdb);
14864#else
14865 dpyinfo->display->db = xrdb;
14866#endif
547d9db8 14867 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
14868 all versions. */
14869 dpyinfo->xrdb = xrdb;
334208b7
RS
14870
14871 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14872 DefaultScreen (dpyinfo->display));
5ff67d81 14873 select_visual (dpyinfo);
43bd1b2b 14874 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
14875 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14876 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14877 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14878 dpyinfo->grabbed = 0;
14879 dpyinfo->reference_count = 0;
14880 dpyinfo->icon_bitmap_id = -1;
06a2c219 14881 dpyinfo->font_table = NULL;
7a13e894
RS
14882 dpyinfo->n_fonts = 0;
14883 dpyinfo->font_table_size = 0;
14884 dpyinfo->bitmaps = 0;
14885 dpyinfo->bitmaps_size = 0;
14886 dpyinfo->bitmaps_last = 0;
14887 dpyinfo->scratch_cursor_gc = 0;
14888 dpyinfo->mouse_face_mouse_frame = 0;
14889 dpyinfo->mouse_face_deferred_gc = 0;
14890 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14891 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 14892 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 14893 dpyinfo->mouse_face_window = Qnil;
0a61c667 14894 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
14895 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14896 dpyinfo->mouse_face_defer = 0;
66301061 14897 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
14898 dpyinfo->x_focus_frame = 0;
14899 dpyinfo->x_focus_event_frame = 0;
14900 dpyinfo->x_highlight_frame = 0;
06a2c219 14901 dpyinfo->image_cache = make_image_cache ();
334208b7 14902
43bd1b2b 14903 /* See if a private colormap is requested. */
5ff67d81
GM
14904 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14905 {
14906 if (dpyinfo->visual->class == PseudoColor)
14907 {
14908 Lisp_Object value;
14909 value = display_x_get_resource (dpyinfo,
14910 build_string ("privateColormap"),
14911 build_string ("PrivateColormap"),
14912 Qnil, Qnil);
14913 if (STRINGP (value)
d5db4077
KR
14914 && (!strcmp (SDATA (value), "true")
14915 || !strcmp (SDATA (value), "on")))
5ff67d81
GM
14916 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14917 }
43bd1b2b 14918 }
5ff67d81
GM
14919 else
14920 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14921 dpyinfo->visual, AllocNone);
43bd1b2b 14922
06a2c219
GM
14923 {
14924 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14925 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14926 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14927 dpyinfo->resy = pixels * 25.4 / mm;
14928 pixels = DisplayWidth (dpyinfo->display, screen_number);
14929 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14930 dpyinfo->resx = pixels * 25.4 / mm;
14931 }
14932
334208b7
RS
14933 dpyinfo->Xatom_wm_protocols
14934 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14935 dpyinfo->Xatom_wm_take_focus
14936 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14937 dpyinfo->Xatom_wm_save_yourself
14938 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14939 dpyinfo->Xatom_wm_delete_window
14940 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14941 dpyinfo->Xatom_wm_change_state
14942 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14943 dpyinfo->Xatom_wm_configure_denied
14944 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14945 dpyinfo->Xatom_wm_window_moved
14946 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14947 dpyinfo->Xatom_editres
14948 = XInternAtom (dpyinfo->display, "Editres", False);
14949 dpyinfo->Xatom_CLIPBOARD
14950 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14951 dpyinfo->Xatom_TIMESTAMP
14952 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14953 dpyinfo->Xatom_TEXT
14954 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
14955 dpyinfo->Xatom_COMPOUND_TEXT
14956 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
c62525b7
KH
14957 dpyinfo->Xatom_UTF8_STRING
14958 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
334208b7
RS
14959 dpyinfo->Xatom_DELETE
14960 = XInternAtom (dpyinfo->display, "DELETE", False);
14961 dpyinfo->Xatom_MULTIPLE
14962 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14963 dpyinfo->Xatom_INCR
14964 = XInternAtom (dpyinfo->display, "INCR", False);
14965 dpyinfo->Xatom_EMACS_TMP
14966 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14967 dpyinfo->Xatom_TARGETS
14968 = XInternAtom (dpyinfo->display, "TARGETS", False);
14969 dpyinfo->Xatom_NULL
14970 = XInternAtom (dpyinfo->display, "NULL", False);
14971 dpyinfo->Xatom_ATOM_PAIR
14972 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
14973 /* For properties of font. */
14974 dpyinfo->Xatom_PIXEL_SIZE
14975 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14976 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14977 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14978 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14979 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
14980 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14981 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 14982
06a2c219
GM
14983 /* Ghostscript support. */
14984 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14985 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14986
14987 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14988 False);
14989
547d9db8
KH
14990 dpyinfo->cut_buffers_initialized = 0;
14991
334208b7
RS
14992 connection = ConnectionNumber (dpyinfo->display);
14993 dpyinfo->connection = connection;
14994
dc43ef94 14995 {
5d7cc324
RS
14996 char null_bits[1];
14997
14998 null_bits[0] = 0x00;
dc43ef94
KH
14999
15000 dpyinfo->null_pixel
15001 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15002 null_bits, 1, 1, (long) 0, (long) 0,
15003 1);
15004 }
15005
06a2c219
GM
15006 {
15007 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 15008 extern char *gray_bitmap_bits;
06a2c219
GM
15009 dpyinfo->gray
15010 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15011 gray_bitmap_bits,
15012 gray_bitmap_width, gray_bitmap_height,
15013 (unsigned long) 1, (unsigned long) 0, 1);
15014 }
15015
f5d11644
GM
15016#ifdef HAVE_X_I18N
15017 xim_initialize (dpyinfo, resource_name);
15018#endif
15019
87485d6f
MW
15020#ifdef subprocesses
15021 /* This is only needed for distinguishing keyboard and process input. */
334208b7 15022 if (connection != 0)
7a13e894 15023 add_keyboard_wait_descriptor (connection);
87485d6f 15024#endif
6d4238f3 15025
041b69ac 15026#ifndef F_SETOWN_BUG
dc6f92b8 15027#ifdef F_SETOWN
dc6f92b8 15028#ifdef F_SETOWN_SOCK_NEG
61c3ce62 15029 /* stdin is a socket here */
334208b7 15030 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 15031#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 15032 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
15033#endif /* ! defined (F_SETOWN_SOCK_NEG) */
15034#endif /* ! defined (F_SETOWN) */
041b69ac 15035#endif /* F_SETOWN_BUG */
dc6f92b8
JB
15036
15037#ifdef SIGIO
eee20f6a
KH
15038 if (interrupt_input)
15039 init_sigio (connection);
c118dd06 15040#endif /* ! defined (SIGIO) */
dc6f92b8 15041
51b592fb 15042#ifdef USE_LUCID
f8c39f51 15043#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
15044 /* Make sure that we have a valid font for dialog boxes
15045 so that Xt does not crash. */
15046 {
15047 Display *dpy = dpyinfo->display;
15048 XrmValue d, fr, to;
15049 Font font;
e99db5a1 15050 int count;
51b592fb
RS
15051
15052 d.addr = (XPointer)&dpy;
15053 d.size = sizeof (Display *);
15054 fr.addr = XtDefaultFont;
15055 fr.size = sizeof (XtDefaultFont);
15056 to.size = sizeof (Font *);
15057 to.addr = (XPointer)&font;
e99db5a1 15058 count = x_catch_errors (dpy);
51b592fb
RS
15059 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15060 abort ();
15061 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15062 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 15063 x_uncatch_errors (dpy, count);
51b592fb
RS
15064 }
15065#endif
f8c39f51 15066#endif
51b592fb 15067
34e23e5a
GM
15068 /* See if we should run in synchronous mode. This is useful
15069 for debugging X code. */
15070 {
15071 Lisp_Object value;
15072 value = display_x_get_resource (dpyinfo,
15073 build_string ("synchronous"),
15074 build_string ("Synchronous"),
15075 Qnil, Qnil);
15076 if (STRINGP (value)
d5db4077
KR
15077 && (!strcmp (SDATA (value), "true")
15078 || !strcmp (SDATA (value), "on")))
34e23e5a
GM
15079 XSynchronize (dpyinfo->display, True);
15080 }
15081
60439948
KH
15082 UNBLOCK_INPUT;
15083
7a13e894
RS
15084 return dpyinfo;
15085}
15086\f
15087/* Get rid of display DPYINFO, assuming all frames are already gone,
15088 and without sending any more commands to the X server. */
dc6f92b8 15089
7a13e894
RS
15090void
15091x_delete_display (dpyinfo)
15092 struct x_display_info *dpyinfo;
15093{
15094 delete_keyboard_wait_descriptor (dpyinfo->connection);
15095
15096 /* Discard this display from x_display_name_list and x_display_list.
15097 We can't use Fdelq because that can quit. */
15098 if (! NILP (x_display_name_list)
8e713be6
KR
15099 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15100 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
15101 else
15102 {
15103 Lisp_Object tail;
15104
15105 tail = x_display_name_list;
8e713be6 15106 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 15107 {
bffcfca9 15108 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 15109 {
f3fbd155 15110 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
15111 break;
15112 }
8e713be6 15113 tail = XCDR (tail);
7a13e894
RS
15114 }
15115 }
15116
9bda743f
GM
15117 if (next_noop_dpyinfo == dpyinfo)
15118 next_noop_dpyinfo = dpyinfo->next;
15119
7a13e894
RS
15120 if (x_display_list == dpyinfo)
15121 x_display_list = dpyinfo->next;
7f9c7f94
RS
15122 else
15123 {
15124 struct x_display_info *tail;
7a13e894 15125
7f9c7f94
RS
15126 for (tail = x_display_list; tail; tail = tail->next)
15127 if (tail->next == dpyinfo)
15128 tail->next = tail->next->next;
15129 }
7a13e894 15130
0d777288
RS
15131#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15132#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
15133 XrmDestroyDatabase (dpyinfo->xrdb);
15134#endif
0d777288 15135#endif
29b38361
KH
15136#ifdef MULTI_KBOARD
15137 if (--dpyinfo->kboard->reference_count == 0)
39f79001 15138 delete_kboard (dpyinfo->kboard);
b9737ad3 15139#endif
f5d11644
GM
15140#ifdef HAVE_X_I18N
15141 if (dpyinfo->xim)
15142 xim_close_dpy (dpyinfo);
15143#endif
15144
b9737ad3
KH
15145 xfree (dpyinfo->font_table);
15146 xfree (dpyinfo->x_id_name);
f04e1297 15147 xfree (dpyinfo->color_cells);
b9737ad3 15148 xfree (dpyinfo);
7a13e894 15149}
f04e1297 15150
7a13e894
RS
15151\f
15152/* Set up use of X before we make the first connection. */
15153
06a2c219
GM
15154static struct redisplay_interface x_redisplay_interface =
15155{
15156 x_produce_glyphs,
15157 x_write_glyphs,
15158 x_insert_glyphs,
15159 x_clear_end_of_line,
15160 x_scroll_run,
15161 x_after_update_window_line,
15162 x_update_window_begin,
15163 x_update_window_end,
15164 XTcursor_to,
15165 x_flush,
71b8321e 15166 x_clear_mouse_face,
66ac4b0e
GM
15167 x_get_glyph_overhangs,
15168 x_fix_overlapping_area
06a2c219
GM
15169};
15170
dfcf069d 15171void
7a13e894
RS
15172x_initialize ()
15173{
06a2c219
GM
15174 rif = &x_redisplay_interface;
15175
15176 clear_frame_hook = x_clear_frame;
15177 ins_del_lines_hook = x_ins_del_lines;
06a2c219 15178 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
15179 ring_bell_hook = XTring_bell;
15180 reset_terminal_modes_hook = XTreset_terminal_modes;
15181 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
15182 update_begin_hook = x_update_begin;
15183 update_end_hook = x_update_end;
dc6f92b8
JB
15184 set_terminal_window_hook = XTset_terminal_window;
15185 read_socket_hook = XTread_socket;
b8009dd1 15186 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 15187 mouse_position_hook = XTmouse_position;
f451eb13 15188 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 15189 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
15190 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15191 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15192 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15193 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 15194 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 15195
f676886a 15196 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 15197 char_ins_del_ok = 1;
dc6f92b8
JB
15198 line_ins_del_ok = 1; /* we'll just blt 'em */
15199 fast_clear_end_of_line = 1; /* X does this well */
58769bee 15200 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
15201 off the bottom */
15202 baud_rate = 19200;
15203
7a13e894 15204 x_noop_count = 0;
9ea173e8 15205 last_tool_bar_item = -1;
06a2c219
GM
15206 any_help_event_p = 0;
15207
b30b24cb
RS
15208 /* Try to use interrupt input; if we can't, then start polling. */
15209 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15210
7f9c7f94
RS
15211#ifdef USE_X_TOOLKIT
15212 XtToolkitInitialize ();
651f03b6 15213
7f9c7f94 15214 Xt_app_con = XtCreateApplicationContext ();
651f03b6
GM
15215
15216 /* Register a converter from strings to pixels, which uses
15217 Emacs' color allocation infrastructure. */
15218 XtAppSetTypeConverter (Xt_app_con,
15219 XtRString, XtRPixel, cvt_string_to_pixel,
15220 cvt_string_to_pixel_args,
15221 XtNumber (cvt_string_to_pixel_args),
15222 XtCacheByDisplay, cvt_pixel_dtor);
15223
665881ad 15224 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
15225
15226 /* Install an asynchronous timer that processes Xt timeout events
15227 every 0.1s. This is necessary because some widget sets use
15228 timeouts internally, for example the LessTif menu bar, or the
15229 Xaw3d scroll bar. When Xt timouts aren't processed, these
15230 widgets don't behave normally. */
15231 {
15232 EMACS_TIME interval;
15233 EMACS_SET_SECS_USECS (interval, 0, 100000);
15234 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15235 }
db74249b 15236#endif
bffcfca9 15237
eccc05db 15238#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
15239 xaw3d_arrow_scroll = False;
15240 xaw3d_pick_top = True;
7f9c7f94
RS
15241#endif
15242
58769bee 15243 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 15244 original error handler. */
e99db5a1 15245 XSetErrorHandler (x_error_handler);
334208b7 15246 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 15247
06a2c219 15248 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
15249#ifdef SIGWINCH
15250 signal (SIGWINCH, SIG_DFL);
a59facca 15251#endif /* SIGWINCH */
dc6f92b8 15252
92e2441b 15253 signal (SIGPIPE, x_connection_signal);
5f30b957
JD
15254
15255#ifdef HAVE_X_SM
15256 x_session_initialize ();
15257#endif
dc6f92b8 15258}
55123275 15259
06a2c219 15260
55123275
JB
15261void
15262syms_of_xterm ()
15263{
e99db5a1
RS
15264 staticpro (&x_error_message_string);
15265 x_error_message_string = Qnil;
15266
7a13e894
RS
15267 staticpro (&x_display_name_list);
15268 x_display_name_list = Qnil;
334208b7 15269
ab648270 15270 staticpro (&last_mouse_scroll_bar);
e53cb100 15271 last_mouse_scroll_bar = Qnil;
59e755be
KH
15272
15273 staticpro (&Qvendor_specific_keysyms);
15274 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 15275
c997eae5
SM
15276 staticpro (&Qutf_8);
15277 Qutf_8 = intern ("utf-8");
15278 staticpro (&Qlatin_1);
15279 Qlatin_1 = intern ("latin-1");
15280
2237cac9
RS
15281 staticpro (&last_mouse_press_frame);
15282 last_mouse_press_frame = Qnil;
06a2c219 15283
06a2c219 15284 help_echo = Qnil;
be010514
GM
15285 staticpro (&help_echo);
15286 help_echo_object = Qnil;
15287 staticpro (&help_echo_object);
7cea38bc
GM
15288 help_echo_window = Qnil;
15289 staticpro (&help_echo_window);
06a2c219 15290 previous_help_echo = Qnil;
be010514
GM
15291 staticpro (&previous_help_echo);
15292 help_echo_pos = -1;
06a2c219 15293
ee8ceff8 15294 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
503e457e 15295 doc: /* *Non-nil means autoselect window with mouse pointer. */);
ee8ceff8 15296 mouse_autoselect_window = 0;
503e457e 15297
7ee72033
MB
15298 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15299 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
228299fa
GM
15300For example, if a block cursor is over a tab, it will be drawn as
15301wide as that tab on the display. */);
06a2c219
GM
15302 x_stretch_cursor_p = 0;
15303
a72d5ce5 15304 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
15305 &x_use_underline_position_properties,
15306 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 15307nil means ignore them. If you encounter fonts with bogus
228299fa
GM
15308UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15309to 4.1, set this to nil. */);
a72d5ce5
GM
15310 x_use_underline_position_properties = 1;
15311
7ee72033
MB
15312 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15313 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
15314A value of nil means Emacs doesn't use X toolkit scroll bars.
15315Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 15316#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
15317#ifdef USE_MOTIF
15318 Vx_toolkit_scroll_bars = intern ("motif");
15319#elif defined HAVE_XAW3D
15320 Vx_toolkit_scroll_bars = intern ("xaw3d");
15321#else
15322 Vx_toolkit_scroll_bars = intern ("xaw");
15323#endif
06a2c219 15324#else
5bf04520 15325 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
15326#endif
15327
06a2c219
GM
15328 staticpro (&last_mouse_motion_frame);
15329 last_mouse_motion_frame = Qnil;
98659da6
KG
15330
15331 Qmodifier_value = intern ("modifier-value");
15332 Qalt = intern ("alt");
15333 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15334 Qhyper = intern ("hyper");
15335 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15336 Qmeta = intern ("meta");
15337 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15338 Qsuper = intern ("super");
15339 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15340
15341 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15342 doc: /* Which keys Emacs uses for the alt modifier.
15343This should be one of the symbols `alt', `hyper', `meta', `super'.
15344For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15345is nil, which is the same as `alt'. */);
15346 Vx_alt_keysym = Qnil;
15347
15348 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15349 doc: /* Which keys Emacs uses for the hyper modifier.
15350This should be one of the symbols `alt', `hyper', `meta', `super'.
15351For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15352default is nil, which is the same as `hyper'. */);
15353 Vx_hyper_keysym = Qnil;
15354
15355 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15356 doc: /* Which keys Emacs uses for the meta modifier.
15357This should be one of the symbols `alt', `hyper', `meta', `super'.
15358For example, `meta' means use the Meta_L and Meta_R keysyms. The
15359default is nil, which is the same as `meta'. */);
15360 Vx_meta_keysym = Qnil;
15361
15362 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15363 doc: /* Which keys Emacs uses for the super modifier.
15364This should be one of the symbols `alt', `hyper', `meta', `super'.
15365For example, `super' means use the Super_L and Super_R keysyms. The
15366default is nil, which is the same as `super'. */);
15367 Vx_super_keysym = Qnil;
15368
55123275 15369}
6cf0ae86 15370
1d6c120a 15371#endif /* HAVE_X_WINDOWS */