(x_set_toolkit_scroll_bar_thumb) [USE_MOTIF]: Don't
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
edf36fe6 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
79#if 0
80#include "sink.h"
81#include "sinkmask.h"
c118dd06 82#endif /* ! 0 */
dc6f92b8 83#include "gnu.h"
dc6f92b8 84#include "disptab.h"
dc6f92b8 85#include "buffer.h"
f451eb13 86#include "window.h"
3b2fa4e6 87#include "keyboard.h"
bde7c500 88#include "intervals.h"
dfcf069d 89#include "process.h"
bffcfca9 90#include "atimer.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#ifdef HAVE_SYS_TIME_H
97#include <sys/time.h>
98#endif
99#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
3afe33e7 103#ifdef USE_X_TOOLKIT
06a2c219 104
952291d9
GM
105extern void free_frame_menubar P_ ((struct frame *));
106extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
e4b68333 152#ifndef min
06a2c219 153#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
154#endif
155#ifndef max
06a2c219
GM
156#define max(a,b) ((a) > (b) ? (a) : (b))
157#endif
158
159#define abs(x) ((x) < 0 ? -(x) : (x))
160
161#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163\f
164/* Bitmaps for truncated lines. */
165
166enum bitmap_type
167{
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175};
176
177/* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180#define zv_width 8
181#define zv_height 8
182static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184
185/* An arrow like this: `<-'. */
186
187#define left_width 8
188#define left_height 8
189static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191
110859fc
GM
192/* Right truncation arrow bitmap `->'. */
193
194#define right_width 8
195#define right_height 8
196static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198
06a2c219
GM
199/* Marker for continued lines. */
200
201#define continued_width 8
202#define continued_height 8
203static unsigned char continued_bits[] = {
110859fc
GM
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205
206/* Marker for continuation lines. */
06a2c219
GM
207
208#define continuation_width 8
209#define continuation_height 8
210static unsigned char continuation_bits[] = {
110859fc 211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 212
110859fc 213/* Overlay arrow bitmap. */
06a2c219 214
110859fc
GM
215#if 0
216/* A bomb. */
06a2c219
GM
217#define ov_width 8
218#define ov_height 8
219static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 221#else
110859fc 222/* A triangular arrow. */
06a2c219
GM
223#define ov_width 8
224#define ov_height 8
225static unsigned char ov_bits[] = {
110859fc
GM
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
e4b68333 228#endif
06a2c219
GM
229
230extern Lisp_Object Qhelp_echo;
231
69388238 232\f
5bf04520 233/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 234
5bf04520 235Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
236
237/* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
239
240static Lisp_Object help_echo;
7cea38bc 241static Lisp_Object help_echo_window;
be010514
GM
242static Lisp_Object help_echo_object;
243static int help_echo_pos;
06a2c219
GM
244
245/* Temporary variable for XTread_socket. */
246
247static Lisp_Object previous_help_echo;
248
249/* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252static int any_help_event_p;
253
254/* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
257
258int x_stretch_cursor_p;
259
260/* This is a chain of structures for all the X displays currently in
261 use. */
262
334208b7 263struct x_display_info *x_display_list;
dc6f92b8 264
06a2c219
GM
265/* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
269
7a13e894 270Lisp_Object x_display_name_list;
f451eb13 271
987d2ad1 272/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
276
d0386f2a 277extern struct frame *updating_frame;
dc6f92b8 278
dfcf069d 279extern int waiting_for_input;
0e81d8cd 280
06a2c219
GM
281/* This is a frame waiting to be auto-raised, within XTread_socket. */
282
0134a210
RS
283struct frame *pending_autoraise_frame;
284
7f9c7f94
RS
285#ifdef USE_X_TOOLKIT
286/* The application context for Xt use. */
287XtAppContext Xt_app_con;
06a2c219
GM
288static String Xt_default_resources[] = {0};
289#endif /* USE_X_TOOLKIT */
665881ad 290
06a2c219
GM
291/* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
dc6f92b8 294
06a2c219 295struct cursor_pos output_cursor;
dc6f92b8 296
bffcfca9
GM
297/* Non-zero means user is interacting with a toolkit scroll bar. */
298
299static int toolkit_scroll_bar_interaction;
dc6f92b8 300
69388238
RS
301/* Mouse movement.
302
06a2c219 303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
307
308 Simply asking for MotionNotify all the time seems to work better.
309
69388238
RS
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 319 is off. */
69388238
RS
320
321/* Where the mouse was last time we reported a mouse event. */
69388238 322
06a2c219
GM
323FRAME_PTR last_mouse_frame;
324static XRectangle last_mouse_glyph;
2237cac9
RS
325static Lisp_Object last_mouse_press_frame;
326
69388238
RS
327/* The scroll bar in which the last X motion event occurred.
328
06a2c219
GM
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
331 an ordinary motion.
332
06a2c219
GM
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
336
69388238
RS
337static Lisp_Object last_mouse_scroll_bar;
338
69388238
RS
339/* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
06a2c219 341 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
06a2c219 345
69388238
RS
346static Time last_mouse_movement_time;
347
06a2c219
GM
348/* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
c0a04927
RS
351#ifdef __STDC__
352static int volatile input_signal_count;
353#else
354static int input_signal_count;
355#endif
356
7a13e894 357/* Used locally within XTread_socket. */
06a2c219 358
7a13e894 359static int x_noop_count;
dc6f92b8 360
7a13e894 361/* Initial values of argv and argc. */
06a2c219 362
7a13e894
RS
363extern char **initial_argv;
364extern int initial_argc;
dc6f92b8 365
7a13e894 366extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 367
06a2c219 368/* Tells if a window manager is present or not. */
7a13e894
RS
369
370extern Lisp_Object Vx_no_window_manager;
dc6f92b8 371
c2df547c 372extern Lisp_Object Qface, Qmouse_face;
b8009dd1 373
dc6f92b8
JB
374extern int errno;
375
dfeccd2d 376/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 377
64bb1782
RS
378extern int extra_keyboard_modifiers;
379
59e755be
KH
380static Lisp_Object Qvendor_specific_keysyms;
381
952291d9
GM
382extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 384
7a13e894 385
06a2c219
GM
386/* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
388
389enum draw_glyphs_face
390{
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
397};
398
499b1844 399static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
f04e1297 400static const XColor *x_color_cells P_ ((struct frame *, int *));
71b8321e 401static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
402static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403void x_delete_display P_ ((struct x_display_info *));
404static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
f9db2310
GM
408static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
409 int *, int *, int *, int *, int));
06a2c219
GM
410static void set_output_cursor P_ ((struct cursor_pos *));
411static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
f9db2310 412 int *, int *, int *, int));
06a2c219 413static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 414static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
415static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
416static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
417static void show_mouse_face P_ ((struct x_display_info *,
418 enum draw_glyphs_face));
419static int x_io_error_quitter P_ ((Display *));
420int x_catch_errors P_ ((Display *));
421void x_uncatch_errors P_ ((Display *, int));
422void x_lower_frame P_ ((struct frame *));
423void x_scroll_bar_clear P_ ((struct frame *));
424int x_had_errors_p P_ ((Display *));
425void x_wm_set_size_hint P_ ((struct frame *, long, int));
426void x_raise_frame P_ ((struct frame *));
427void x_set_window_size P_ ((struct frame *, int, int, int));
428void x_wm_set_window_state P_ ((struct frame *, int));
429void x_wm_set_icon_pixmap P_ ((struct frame *, int));
430void x_initialize P_ ((void));
431static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
432static int x_compute_min_glyph_bounds P_ ((struct frame *));
433static void x_draw_phys_cursor_glyph P_ ((struct window *,
434 struct glyph_row *,
435 enum draw_glyphs_face));
436static void x_update_end P_ ((struct frame *));
437static void XTframe_up_to_date P_ ((struct frame *));
438static void XTreassert_line_highlight P_ ((int, int));
439static void x_change_line_highlight P_ ((int, int, int, int));
440static void XTset_terminal_modes P_ ((void));
441static void XTreset_terminal_modes P_ ((void));
442static void XTcursor_to P_ ((int, int, int, int));
443static void x_write_glyphs P_ ((struct glyph *, int));
444static void x_clear_end_of_line P_ ((int));
445static void x_clear_frame P_ ((void));
446static void x_clear_cursor P_ ((struct window *));
447static void frame_highlight P_ ((struct frame *));
448static void frame_unhighlight P_ ((struct frame *));
449static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
450static void XTframe_rehighlight P_ ((struct frame *));
451static void x_frame_rehighlight P_ ((struct x_display_info *));
452static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 453static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
454static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
455 XRectangle *));
456static void expose_frame P_ ((struct frame *, int, int, int, int));
457static void expose_window_tree P_ ((struct window *, XRectangle *));
458static void expose_window P_ ((struct window *, XRectangle *));
459static void expose_area P_ ((struct window *, struct glyph_row *,
460 XRectangle *, enum glyph_row_area));
461static void expose_line P_ ((struct window *, struct glyph_row *,
462 XRectangle *));
463static void x_update_cursor_in_window_tree P_ ((struct window *, int));
464static void x_update_window_cursor P_ ((struct window *, int));
465static void x_erase_phys_cursor P_ ((struct window *));
466void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
467static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
468 enum bitmap_type));
469
470static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
471 GC, int));
472static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
473static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
474static void note_overwritten_text_cursor P_ ((struct window *, int, int));
475static void x_flush P_ ((struct frame *f));
952291d9
GM
476static void x_update_begin P_ ((struct frame *));
477static void x_update_window_begin P_ ((struct window *));
478static void x_draw_vertical_border P_ ((struct window *));
479static void x_after_update_window_line P_ ((struct glyph_row *));
480static INLINE void take_vertical_position_into_account P_ ((struct it *));
481static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
482static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
483static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
484 enum scroll_bar_part *,
485 Lisp_Object *, Lisp_Object *,
486 unsigned long *));
06a2c219
GM
487
488/* Flush display of frame F, or of all frames if F is null. */
489
490static void
491x_flush (f)
492 struct frame *f;
493{
494 BLOCK_INPUT;
495 if (f == NULL)
496 {
497 Lisp_Object rest, frame;
498 FOR_EACH_FRAME (rest, frame)
499 x_flush (XFRAME (frame));
500 }
501 else if (FRAME_X_P (f))
502 XFlush (FRAME_X_DISPLAY (f));
503 UNBLOCK_INPUT;
504}
505
dc6f92b8 506
06a2c219
GM
507/* Remove calls to XFlush by defining XFlush to an empty replacement.
508 Calls to XFlush should be unnecessary because the X output buffer
509 is flushed automatically as needed by calls to XPending,
510 XNextEvent, or XWindowEvent according to the XFlush man page.
511 XTread_socket calls XPending. Removing XFlush improves
512 performance. */
513
514#define XFlush(DISPLAY) (void) 0
b8009dd1 515
334208b7 516\f
06a2c219
GM
517/***********************************************************************
518 Debugging
519 ***********************************************************************/
520
9382638d 521#if 0
06a2c219
GM
522
523/* This is a function useful for recording debugging information about
524 the sequence of occurrences in this file. */
9382638d
KH
525
526struct record
527{
528 char *locus;
529 int type;
530};
531
532struct record event_record[100];
533
534int event_record_index;
535
536record_event (locus, type)
537 char *locus;
538 int type;
539{
540 if (event_record_index == sizeof (event_record) / sizeof (struct record))
541 event_record_index = 0;
542
543 event_record[event_record_index].locus = locus;
544 event_record[event_record_index].type = type;
545 event_record_index++;
546}
547
548#endif /* 0 */
06a2c219
GM
549
550
9382638d 551\f
334208b7
RS
552/* Return the struct x_display_info corresponding to DPY. */
553
554struct x_display_info *
555x_display_info_for_display (dpy)
556 Display *dpy;
557{
558 struct x_display_info *dpyinfo;
559
560 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
561 if (dpyinfo->display == dpy)
562 return dpyinfo;
16bd92ea 563
334208b7
RS
564 return 0;
565}
f451eb13 566
06a2c219
GM
567
568\f
569/***********************************************************************
570 Starting and ending an update
571 ***********************************************************************/
572
573/* Start an update of frame F. This function is installed as a hook
574 for update_begin, i.e. it is called when update_begin is called.
575 This function is called prior to calls to x_update_window_begin for
576 each window being updated. Currently, there is nothing to do here
577 because all interesting stuff is done on a window basis. */
dc6f92b8 578
dfcf069d 579static void
06a2c219 580x_update_begin (f)
f676886a 581 struct frame *f;
58769bee 582{
06a2c219
GM
583 /* Nothing to do. */
584}
dc6f92b8 585
dc6f92b8 586
06a2c219
GM
587/* Start update of window W. Set the global variable updated_window
588 to the window being updated and set output_cursor to the cursor
589 position of W. */
dc6f92b8 590
06a2c219
GM
591static void
592x_update_window_begin (w)
593 struct window *w;
594{
595 struct frame *f = XFRAME (WINDOW_FRAME (w));
596 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
597
598 updated_window = w;
599 set_output_cursor (&w->cursor);
b8009dd1 600
06a2c219 601 BLOCK_INPUT;
d1bc4182 602
06a2c219 603 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 604 {
514e4681 605 /* Don't do highlighting for mouse motion during the update. */
06a2c219 606 display_info->mouse_face_defer = 1;
37c2c98b 607
06a2c219
GM
608 /* If F needs to be redrawn, simply forget about any prior mouse
609 highlighting. */
9f67f20b 610 if (FRAME_GARBAGED_P (f))
06a2c219
GM
611 display_info->mouse_face_window = Qnil;
612
64f26cf5
GM
613#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
614 their mouse_face_p flag set, which means that they are always
615 unequal to rows in a desired matrix which never have that
616 flag set. So, rows containing mouse-face glyphs are never
617 scrolled, and we don't have to switch the mouse highlight off
618 here to prevent it from being scrolled. */
619
06a2c219
GM
620 /* Can we tell that this update does not affect the window
621 where the mouse highlight is? If so, no need to turn off.
622 Likewise, don't do anything if the frame is garbaged;
623 in that case, the frame's current matrix that we would use
624 is all wrong, and we will redisplay that line anyway. */
625 if (!NILP (display_info->mouse_face_window)
626 && w == XWINDOW (display_info->mouse_face_window))
514e4681 627 {
06a2c219 628 int i;
514e4681 629
06a2c219
GM
630 for (i = 0; i < w->desired_matrix->nrows; ++i)
631 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
632 break;
633
06a2c219
GM
634 if (i < w->desired_matrix->nrows)
635 clear_mouse_face (display_info);
514e4681 636 }
64f26cf5 637#endif /* 0 */
b8009dd1 638 }
6ccf47d1 639
dc6f92b8
JB
640 UNBLOCK_INPUT;
641}
642
06a2c219
GM
643
644/* Draw a vertical window border to the right of window W if W doesn't
645 have vertical scroll bars. */
646
dfcf069d 647static void
06a2c219
GM
648x_draw_vertical_border (w)
649 struct window *w;
58769bee 650{
06a2c219
GM
651 struct frame *f = XFRAME (WINDOW_FRAME (w));
652
653 /* Redraw borders between horizontally adjacent windows. Don't
654 do it for frames with vertical scroll bars because either the
655 right scroll bar of a window, or the left scroll bar of its
656 neighbor will suffice as a border. */
657 if (!WINDOW_RIGHTMOST_P (w)
658 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
659 {
660 int x0, x1, y0, y1;
dc6f92b8 661
06a2c219 662 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 663 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
664 y1 -= 1;
665
666 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
667 f->output_data.x->normal_gc, x1, y0, x1, y1);
668 }
669}
670
671
71b8321e
GM
672/* End update of window W (which is equal to updated_window).
673
674 Draw vertical borders between horizontally adjacent windows, and
675 display W's cursor if CURSOR_ON_P is non-zero.
676
677 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
678 glyphs in mouse-face were overwritten. In that case we have to
679 make sure that the mouse-highlight is properly redrawn.
680
681 W may be a menu bar pseudo-window in case we don't have X toolkit
682 support. Such windows don't have a cursor, so don't display it
683 here. */
06a2c219
GM
684
685static void
71b8321e 686x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 687 struct window *w;
71b8321e 688 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
689{
690 if (!w->pseudo_window_p)
691 {
71b8321e
GM
692 struct x_display_info *dpyinfo
693 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
694
06a2c219 695 BLOCK_INPUT;
71b8321e
GM
696
697 /* If a row with mouse-face was overwritten, arrange for
698 XTframe_up_to_date to redisplay the mouse highlight. */
699 if (mouse_face_overwritten_p)
700 {
701 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
702 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
703 dpyinfo->mouse_face_window = Qnil;
704 }
705
06a2c219
GM
706 if (cursor_on_p)
707 x_display_and_set_cursor (w, 1, output_cursor.hpos,
708 output_cursor.vpos,
709 output_cursor.x, output_cursor.y);
71b8321e 710
06a2c219
GM
711 x_draw_vertical_border (w);
712 UNBLOCK_INPUT;
713 }
714
715 updated_window = NULL;
716}
dc6f92b8 717
dc6f92b8 718
06a2c219
GM
719/* End update of frame F. This function is installed as a hook in
720 update_end. */
721
722static void
723x_update_end (f)
724 struct frame *f;
725{
726 /* Mouse highlight may be displayed again. */
aa8bff2e 727 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 728
06a2c219 729 BLOCK_INPUT;
334208b7 730 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
731 UNBLOCK_INPUT;
732}
b8009dd1 733
06a2c219
GM
734
735/* This function is called from various places in xdisp.c whenever a
736 complete update has been performed. The global variable
737 updated_window is not available here. */
b8009dd1 738
dfcf069d 739static void
b8009dd1 740XTframe_up_to_date (f)
06a2c219 741 struct frame *f;
b8009dd1 742{
06a2c219 743 if (FRAME_X_P (f))
514e4681 744 {
06a2c219 745 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 746
06a2c219
GM
747 if (dpyinfo->mouse_face_deferred_gc
748 || f == dpyinfo->mouse_face_mouse_frame)
749 {
750 BLOCK_INPUT;
751 if (dpyinfo->mouse_face_mouse_frame)
752 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
753 dpyinfo->mouse_face_mouse_x,
754 dpyinfo->mouse_face_mouse_y);
755 dpyinfo->mouse_face_deferred_gc = 0;
756 UNBLOCK_INPUT;
757 }
514e4681 758 }
b8009dd1 759}
06a2c219
GM
760
761
762/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
763 arrow bitmaps, or clear the areas where they would be displayed
764 before DESIRED_ROW is made current. The window being updated is
765 found in updated_window. This function It is called from
766 update_window_line only if it is known that there are differences
767 between bitmaps to be drawn between current row and DESIRED_ROW. */
768
769static void
770x_after_update_window_line (desired_row)
771 struct glyph_row *desired_row;
772{
773 struct window *w = updated_window;
774
775 xassert (w);
776
777 if (!desired_row->mode_line_p && !w->pseudo_window_p)
778 {
c5e6e06b
GM
779 struct frame *f;
780 int width;
781
06a2c219
GM
782 BLOCK_INPUT;
783 x_draw_row_bitmaps (w, desired_row);
784
785 /* When a window has disappeared, make sure that no rest of
786 full-width rows stays visible in the internal border. */
c5e6e06b
GM
787 if (windows_or_buffers_changed
788 && (f = XFRAME (w->frame),
789 width = FRAME_INTERNAL_BORDER_WIDTH (f),
790 width != 0))
06a2c219 791 {
06a2c219 792 int height = desired_row->visible_height;
110859fc
GM
793 int x = (window_box_right (w, -1)
794 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
795 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
796
c5e6e06b
GM
797 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
798 x, y, width, height, False);
06a2c219
GM
799 }
800
801 UNBLOCK_INPUT;
802 }
803}
804
805
806/* Draw the bitmap WHICH in one of the areas to the left or right of
807 window W. ROW is the glyph row for which to display the bitmap; it
808 determines the vertical position at which the bitmap has to be
809 drawn. */
810
811static void
812x_draw_bitmap (w, row, which)
813 struct window *w;
814 struct glyph_row *row;
815 enum bitmap_type which;
816{
817 struct frame *f = XFRAME (WINDOW_FRAME (w));
818 Display *display = FRAME_X_DISPLAY (f);
819 Window window = FRAME_X_WINDOW (f);
820 int x, y, wd, h, dy;
821 unsigned char *bits;
822 Pixmap pixmap;
823 GC gc = f->output_data.x->normal_gc;
824 struct face *face;
825 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
826
827 /* Must clip because of partially visible lines. */
828 x_clip_to_row (w, row, gc, 1);
829
830 switch (which)
831 {
832 case LEFT_TRUNCATION_BITMAP:
833 wd = left_width;
834 h = left_height;
835 bits = left_bits;
836 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
837 - wd
110859fc 838 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
839 break;
840
841 case OVERLAY_ARROW_BITMAP:
842 wd = left_width;
843 h = left_height;
844 bits = ov_bits;
845 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
846 - wd
110859fc 847 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
848 break;
849
850 case RIGHT_TRUNCATION_BITMAP:
851 wd = right_width;
852 h = right_height;
853 bits = right_bits;
854 x = window_box_right (w, -1);
110859fc 855 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
856 break;
857
858 case CONTINUED_LINE_BITMAP:
859 wd = right_width;
860 h = right_height;
861 bits = continued_bits;
862 x = window_box_right (w, -1);
110859fc 863 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
864 break;
865
866 case CONTINUATION_LINE_BITMAP:
867 wd = continuation_width;
868 h = continuation_height;
869 bits = continuation_bits;
870 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
871 - wd
110859fc 872 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
873 break;
874
875 case ZV_LINE_BITMAP:
876 wd = zv_width;
877 h = zv_height;
878 bits = zv_bits;
879 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
880 - wd
110859fc 881 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
882 break;
883
884 default:
885 abort ();
886 }
887
888 /* Convert to frame coordinates. Set dy to the offset in the row to
889 start drawing the bitmap. */
890 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
891 dy = (row->height - h) / 2;
892
893 /* Draw the bitmap. I believe these small pixmaps can be cached
894 by the server. */
895 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
896 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
897 face->foreground,
898 face->background, depth);
899 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
900 XFreePixmap (display, pixmap);
901 XSetClipMask (display, gc, None);
902}
903
904
905/* Draw flags bitmaps for glyph row ROW on window W. Call this
906 function with input blocked. */
907
908static void
909x_draw_row_bitmaps (w, row)
910 struct window *w;
911 struct glyph_row *row;
912{
913 struct frame *f = XFRAME (w->frame);
914 enum bitmap_type bitmap;
915 struct face *face;
045dee35 916 int header_line_height = -1;
06a2c219
GM
917
918 xassert (interrupt_input_blocked);
919
920 /* If row is completely invisible, because of vscrolling, we
921 don't have to draw anything. */
922 if (row->visible_height <= 0)
923 return;
924
925 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
926 PREPARE_FACE_FOR_DISPLAY (f, face);
927
928 /* Decide which bitmap to draw at the left side. */
929 if (row->overlay_arrow_p)
930 bitmap = OVERLAY_ARROW_BITMAP;
931 else if (row->truncated_on_left_p)
932 bitmap = LEFT_TRUNCATION_BITMAP;
933 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
934 bitmap = CONTINUATION_LINE_BITMAP;
935 else if (row->indicate_empty_line_p)
936 bitmap = ZV_LINE_BITMAP;
937 else
938 bitmap = NO_BITMAP;
939
940 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
941 the flags area. */
942 if (bitmap == NO_BITMAP
110859fc 943 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
944 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
945 {
946 /* If W has a vertical border to its left, don't draw over it. */
947 int border = ((XFASTINT (w->left) > 0
948 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
949 ? 1 : 0);
950 int left = window_box_left (w, -1);
951
045dee35
GM
952 if (header_line_height < 0)
953 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
954
955 /* In case the same realized face is used for bitmap areas and
956 for something displayed in the text (e.g. face `region' on
957 mono-displays, the fill style may have been changed to
958 FillSolid in x_draw_glyph_string_background. */
959 if (face->stipple)
960 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
961 else
962 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
963
06a2c219
GM
964 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
965 face->gc,
966 (left
110859fc 967 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 968 + border),
045dee35 969 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 970 row->y)),
110859fc 971 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 972 row->visible_height);
dcd08bfb
GM
973 if (!face->stipple)
974 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
975 }
976
977 /* Draw the left bitmap. */
978 if (bitmap != NO_BITMAP)
979 x_draw_bitmap (w, row, bitmap);
980
981 /* Decide which bitmap to draw at the right side. */
982 if (row->truncated_on_right_p)
983 bitmap = RIGHT_TRUNCATION_BITMAP;
984 else if (row->continued_p)
985 bitmap = CONTINUED_LINE_BITMAP;
986 else
987 bitmap = NO_BITMAP;
988
989 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
990 the flags area. */
991 if (bitmap == NO_BITMAP
110859fc 992 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
993 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
994 {
995 int right = window_box_right (w, -1);
996
045dee35
GM
997 if (header_line_height < 0)
998 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
999
1000 /* In case the same realized face is used for bitmap areas and
1001 for something displayed in the text (e.g. face `region' on
1002 mono-displays, the fill style may have been changed to
1003 FillSolid in x_draw_glyph_string_background. */
1004 if (face->stipple)
1005 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1006 else
1007 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
1008 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1009 face->gc,
1010 right,
045dee35 1011 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1012 row->y)),
110859fc 1013 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1014 row->visible_height);
dcd08bfb
GM
1015 if (!face->stipple)
1016 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1017 }
1018
1019 /* Draw the right bitmap. */
1020 if (bitmap != NO_BITMAP)
1021 x_draw_bitmap (w, row, bitmap);
1022}
1023
dc6f92b8 1024\f
06a2c219
GM
1025/***********************************************************************
1026 Line Highlighting
1027 ***********************************************************************/
dc6f92b8 1028
06a2c219
GM
1029/* External interface to control of standout mode. Not used for X
1030 frames. Aborts when called. */
1031
1032static void
dc6f92b8
JB
1033XTreassert_line_highlight (new, vpos)
1034 int new, vpos;
1035{
06a2c219 1036 abort ();
dc6f92b8
JB
1037}
1038
06a2c219
GM
1039
1040/* Call this when about to modify line at position VPOS and change
1041 whether it is highlighted. Not used for X frames. Aborts when
1042 called. */
dc6f92b8 1043
dfcf069d 1044static void
06a2c219
GM
1045x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1046 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1047{
06a2c219 1048 abort ();
dc6f92b8
JB
1049}
1050
06a2c219
GM
1051
1052/* This is called when starting Emacs and when restarting after
1053 suspend. When starting Emacs, no X window is mapped. And nothing
1054 must be done to Emacs's own window if it is suspended (though that
1055 rarely happens). */
dc6f92b8 1056
dfcf069d 1057static void
dc6f92b8
JB
1058XTset_terminal_modes ()
1059{
1060}
1061
06a2c219
GM
1062/* This is called when exiting or suspending Emacs. Exiting will make
1063 the X-windows go away, and suspending requires no action. */
dc6f92b8 1064
dfcf069d 1065static void
dc6f92b8
JB
1066XTreset_terminal_modes ()
1067{
dc6f92b8 1068}
06a2c219
GM
1069
1070
dc6f92b8 1071\f
06a2c219
GM
1072/***********************************************************************
1073 Output Cursor
1074 ***********************************************************************/
1075
1076/* Set the global variable output_cursor to CURSOR. All cursor
1077 positions are relative to updated_window. */
dc6f92b8 1078
dfcf069d 1079static void
06a2c219
GM
1080set_output_cursor (cursor)
1081 struct cursor_pos *cursor;
dc6f92b8 1082{
06a2c219
GM
1083 output_cursor.hpos = cursor->hpos;
1084 output_cursor.vpos = cursor->vpos;
1085 output_cursor.x = cursor->x;
1086 output_cursor.y = cursor->y;
1087}
1088
1089
1090/* Set a nominal cursor position.
dc6f92b8 1091
06a2c219
GM
1092 HPOS and VPOS are column/row positions in a window glyph matrix. X
1093 and Y are window text area relative pixel positions.
1094
1095 If this is done during an update, updated_window will contain the
1096 window that is being updated and the position is the future output
1097 cursor position for that window. If updated_window is null, use
1098 selected_window and display the cursor at the given position. */
1099
1100static void
1101XTcursor_to (vpos, hpos, y, x)
1102 int vpos, hpos, y, x;
1103{
1104 struct window *w;
1105
1106 /* If updated_window is not set, work on selected_window. */
1107 if (updated_window)
1108 w = updated_window;
1109 else
1110 w = XWINDOW (selected_window);
dbcb258a 1111
06a2c219
GM
1112 /* Set the output cursor. */
1113 output_cursor.hpos = hpos;
1114 output_cursor.vpos = vpos;
1115 output_cursor.x = x;
1116 output_cursor.y = y;
dc6f92b8 1117
06a2c219
GM
1118 /* If not called as part of an update, really display the cursor.
1119 This will also set the cursor position of W. */
1120 if (updated_window == NULL)
dc6f92b8
JB
1121 {
1122 BLOCK_INPUT;
06a2c219 1123 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1124 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1125 UNBLOCK_INPUT;
1126 }
1127}
dc43ef94 1128
06a2c219
GM
1129
1130\f
1131/***********************************************************************
1132 Display Iterator
1133 ***********************************************************************/
1134
1135/* Function prototypes of this page. */
1136
1137static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1138 struct glyph *,
ee569018
KH
1139 XChar2b *,
1140 int *));
06a2c219
GM
1141static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1142 int, XChar2b *, int));
1143static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1144static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1145static void x_append_glyph P_ ((struct it *));
b4192550 1146static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1147static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1148 int, int, double));
1149static void x_produce_glyphs P_ ((struct it *));
06a2c219 1150static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1151
1152
e2ef8ee6
GM
1153/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1154 is not contained in the font. */
dc43ef94 1155
06a2c219 1156static INLINE XCharStruct *
ee569018 1157x_per_char_metric (font, char2b)
06a2c219
GM
1158 XFontStruct *font;
1159 XChar2b *char2b;
1160{
1161 /* The result metric information. */
1162 XCharStruct *pcm = NULL;
dc6f92b8 1163
06a2c219 1164 xassert (font && char2b);
dc6f92b8 1165
06a2c219 1166 if (font->per_char != NULL)
dc6f92b8 1167 {
06a2c219 1168 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1169 {
06a2c219
GM
1170 /* min_char_or_byte2 specifies the linear character index
1171 corresponding to the first element of the per_char array,
1172 max_char_or_byte2 is the index of the last character. A
1173 character with non-zero CHAR2B->byte1 is not in the font.
1174 A character with byte2 less than min_char_or_byte2 or
1175 greater max_char_or_byte2 is not in the font. */
1176 if (char2b->byte1 == 0
1177 && char2b->byte2 >= font->min_char_or_byte2
1178 && char2b->byte2 <= font->max_char_or_byte2)
1179 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1180 }
06a2c219 1181 else
dc6f92b8 1182 {
06a2c219
GM
1183 /* If either min_byte1 or max_byte1 are nonzero, both
1184 min_char_or_byte2 and max_char_or_byte2 are less than
1185 256, and the 2-byte character index values corresponding
1186 to the per_char array element N (counting from 0) are:
1187
1188 byte1 = N/D + min_byte1
1189 byte2 = N\D + min_char_or_byte2
1190
1191 where:
1192
1193 D = max_char_or_byte2 - min_char_or_byte2 + 1
1194 / = integer division
1195 \ = integer modulus */
1196 if (char2b->byte1 >= font->min_byte1
1197 && char2b->byte1 <= font->max_byte1
1198 && char2b->byte2 >= font->min_char_or_byte2
1199 && char2b->byte2 <= font->max_char_or_byte2)
1200 {
1201 pcm = (font->per_char
1202 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1203 * (char2b->byte1 - font->min_byte1))
1204 + (char2b->byte2 - font->min_char_or_byte2));
1205 }
dc6f92b8 1206 }
06a2c219
GM
1207 }
1208 else
1209 {
1210 /* If the per_char pointer is null, all glyphs between the first
1211 and last character indexes inclusive have the same
1212 information, as given by both min_bounds and max_bounds. */
1213 if (char2b->byte2 >= font->min_char_or_byte2
1214 && char2b->byte2 <= font->max_char_or_byte2)
1215 pcm = &font->max_bounds;
1216 }
dc6f92b8 1217
ee569018 1218 return ((pcm == NULL
3e71d8f2 1219 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1220 ? NULL : pcm);
06a2c219 1221}
b73b6aaf 1222
57b03282 1223
06a2c219
GM
1224/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1225 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1226
06a2c219
GM
1227static INLINE void
1228x_encode_char (c, char2b, font_info)
1229 int c;
1230 XChar2b *char2b;
1231 struct font_info *font_info;
1232{
1233 int charset = CHAR_CHARSET (c);
1234 XFontStruct *font = font_info->font;
1235
1236 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1237 This may be either a program in a special encoder language or a
1238 fixed encoding. */
1239 if (font_info->font_encoder)
1240 {
1241 /* It's a program. */
1242 struct ccl_program *ccl = font_info->font_encoder;
1243
1244 if (CHARSET_DIMENSION (charset) == 1)
1245 {
1246 ccl->reg[0] = charset;
1247 ccl->reg[1] = char2b->byte2;
1248 }
1249 else
1250 {
1251 ccl->reg[0] = charset;
1252 ccl->reg[1] = char2b->byte1;
1253 ccl->reg[2] = char2b->byte2;
1254 }
1255
1256 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1257
1258 /* We assume that MSBs are appropriately set/reset by CCL
1259 program. */
1260 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1261 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1262 else
1263 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1264 }
1265 else if (font_info->encoding[charset])
1266 {
1267 /* Fixed encoding scheme. See fontset.h for the meaning of the
1268 encoding numbers. */
1269 int enc = font_info->encoding[charset];
1270
1271 if ((enc == 1 || enc == 2)
1272 && CHARSET_DIMENSION (charset) == 2)
1273 char2b->byte1 |= 0x80;
1274
1275 if (enc == 1 || enc == 3)
1276 char2b->byte2 |= 0x80;
1277 }
1278}
1279
1280
1281/* Get face and two-byte form of character C in face FACE_ID on frame
1282 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1283 means we want to display multibyte text. Value is a pointer to a
1284 realized face that is ready for display. */
1285
1286static INLINE struct face *
1287x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1288 struct frame *f;
1289 int c, face_id;
1290 XChar2b *char2b;
1291 int multibyte_p;
1292{
1293 struct face *face = FACE_FROM_ID (f, face_id);
1294
1295 if (!multibyte_p)
1296 {
1297 /* Unibyte case. We don't have to encode, but we have to make
1298 sure to use a face suitable for unibyte. */
1299 char2b->byte1 = 0;
1300 char2b->byte2 = c;
ee569018
KH
1301 face_id = FACE_FOR_CHAR (f, face, c);
1302 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1303 }
1304 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1305 {
1306 /* Case of ASCII in a face known to fit ASCII. */
1307 char2b->byte1 = 0;
1308 char2b->byte2 = c;
1309 }
1310 else
1311 {
1312 int c1, c2, charset;
1313
1314 /* Split characters into bytes. If c2 is -1 afterwards, C is
1315 really a one-byte character so that byte1 is zero. */
1316 SPLIT_CHAR (c, charset, c1, c2);
1317 if (c2 > 0)
1318 char2b->byte1 = c1, char2b->byte2 = c2;
1319 else
1320 char2b->byte1 = 0, char2b->byte2 = c1;
1321
06a2c219 1322 /* Maybe encode the character in *CHAR2B. */
ee569018 1323 if (face->font != NULL)
06a2c219
GM
1324 {
1325 struct font_info *font_info
1326 = FONT_INFO_FROM_ID (f, face->font_info_id);
1327 if (font_info)
ee569018 1328 x_encode_char (c, char2b, font_info);
06a2c219
GM
1329 }
1330 }
1331
1332 /* Make sure X resources of the face are allocated. */
1333 xassert (face != NULL);
1334 PREPARE_FACE_FOR_DISPLAY (f, face);
1335
1336 return face;
1337}
1338
1339
1340/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1341 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1342 a pointer to a realized face that is ready for display. */
1343
1344static INLINE struct face *
ee569018 1345x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1346 struct frame *f;
1347 struct glyph *glyph;
1348 XChar2b *char2b;
ee569018 1349 int *two_byte_p;
06a2c219
GM
1350{
1351 struct face *face;
1352
1353 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1354 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1355
ee569018
KH
1356 if (two_byte_p)
1357 *two_byte_p = 0;
1358
06a2c219
GM
1359 if (!glyph->multibyte_p)
1360 {
1361 /* Unibyte case. We don't have to encode, but we have to make
1362 sure to use a face suitable for unibyte. */
1363 char2b->byte1 = 0;
43d120d8 1364 char2b->byte2 = glyph->u.ch;
06a2c219 1365 }
43d120d8
KH
1366 else if (glyph->u.ch < 128
1367 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1368 {
1369 /* Case of ASCII in a face known to fit ASCII. */
1370 char2b->byte1 = 0;
43d120d8 1371 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1372 }
1373 else
1374 {
1375 int c1, c2, charset;
1376
1377 /* Split characters into bytes. If c2 is -1 afterwards, C is
1378 really a one-byte character so that byte1 is zero. */
43d120d8 1379 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1380 if (c2 > 0)
1381 char2b->byte1 = c1, char2b->byte2 = c2;
1382 else
1383 char2b->byte1 = 0, char2b->byte2 = c1;
1384
1385 /* Maybe encode the character in *CHAR2B. */
1386 if (charset != CHARSET_ASCII)
1387 {
1388 struct font_info *font_info
1389 = FONT_INFO_FROM_ID (f, face->font_info_id);
1390 if (font_info)
1391 {
43d120d8 1392 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1393 if (two_byte_p)
1394 *two_byte_p
1395 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1396 }
1397 }
1398 }
1399
1400 /* Make sure X resources of the face are allocated. */
1401 xassert (face != NULL);
1402 PREPARE_FACE_FOR_DISPLAY (f, face);
1403 return face;
1404}
1405
1406
1407/* Store one glyph for IT->char_to_display in IT->glyph_row.
1408 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1409
1410static INLINE void
1411x_append_glyph (it)
1412 struct it *it;
1413{
1414 struct glyph *glyph;
1415 enum glyph_row_area area = it->area;
1416
1417 xassert (it->glyph_row);
1418 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1419
1420 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1421 if (glyph < it->glyph_row->glyphs[area + 1])
1422 {
06a2c219
GM
1423 glyph->charpos = CHARPOS (it->position);
1424 glyph->object = it->object;
88d75730 1425 glyph->pixel_width = it->pixel_width;
06a2c219 1426 glyph->voffset = it->voffset;
88d75730 1427 glyph->type = CHAR_GLYPH;
06a2c219 1428 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1429 glyph->left_box_line_p = it->start_of_box_run_p;
1430 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1431 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1432 || it->phys_descent > it->descent);
88d75730 1433 glyph->padding_p = 0;
ee569018 1434 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1435 glyph->face_id = it->face_id;
1436 glyph->u.ch = it->char_to_display;
06a2c219
GM
1437 ++it->glyph_row->used[area];
1438 }
1439}
1440
b4192550
KH
1441/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1442 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1443
1444static INLINE void
1445x_append_composite_glyph (it)
1446 struct it *it;
1447{
1448 struct glyph *glyph;
1449 enum glyph_row_area area = it->area;
1450
1451 xassert (it->glyph_row);
1452
1453 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1454 if (glyph < it->glyph_row->glyphs[area + 1])
1455 {
b4192550
KH
1456 glyph->charpos = CHARPOS (it->position);
1457 glyph->object = it->object;
88d75730 1458 glyph->pixel_width = it->pixel_width;
b4192550 1459 glyph->voffset = it->voffset;
88d75730 1460 glyph->type = COMPOSITE_GLYPH;
b4192550 1461 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1462 glyph->left_box_line_p = it->start_of_box_run_p;
1463 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1464 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1465 || it->phys_descent > it->descent);
88d75730
GM
1466 glyph->padding_p = 0;
1467 glyph->glyph_not_available_p = 0;
1468 glyph->face_id = it->face_id;
1469 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1470 ++it->glyph_row->used[area];
1471 }
1472}
1473
06a2c219
GM
1474
1475/* Change IT->ascent and IT->height according to the setting of
1476 IT->voffset. */
1477
1478static INLINE void
1479take_vertical_position_into_account (it)
1480 struct it *it;
1481{
1482 if (it->voffset)
1483 {
1484 if (it->voffset < 0)
1485 /* Increase the ascent so that we can display the text higher
1486 in the line. */
1487 it->ascent += abs (it->voffset);
1488 else
1489 /* Increase the descent so that we can display the text lower
1490 in the line. */
1491 it->descent += it->voffset;
1492 }
1493}
1494
1495
1496/* Produce glyphs/get display metrics for the image IT is loaded with.
1497 See the description of struct display_iterator in dispextern.h for
1498 an overview of struct display_iterator. */
1499
1500static void
1501x_produce_image_glyph (it)
1502 struct it *it;
1503{
1504 struct image *img;
1505 struct face *face;
1506
1507 xassert (it->what == IT_IMAGE);
1508
1509 face = FACE_FROM_ID (it->f, it->face_id);
1510 img = IMAGE_FROM_ID (it->f, it->image_id);
1511 xassert (img);
1512
1513 /* Make sure X resources of the face and image are loaded. */
1514 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1515 prepare_image_for_display (it->f, img);
1516
95af8492 1517 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1518 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1519 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1520
1521 it->nglyphs = 1;
1522
1523 if (face->box != FACE_NO_BOX)
1524 {
ea2ba0d4
KH
1525 if (face->box_line_width > 0)
1526 {
1527 it->ascent += face->box_line_width;
1528 it->descent += face->box_line_width;
1529 }
06a2c219
GM
1530
1531 if (it->start_of_box_run_p)
ea2ba0d4 1532 it->pixel_width += abs (face->box_line_width);
06a2c219 1533 if (it->end_of_box_run_p)
ea2ba0d4 1534 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1535 }
1536
1537 take_vertical_position_into_account (it);
1538
1539 if (it->glyph_row)
1540 {
1541 struct glyph *glyph;
1542 enum glyph_row_area area = it->area;
1543
1544 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1545 if (glyph < it->glyph_row->glyphs[area + 1])
1546 {
06a2c219
GM
1547 glyph->charpos = CHARPOS (it->position);
1548 glyph->object = it->object;
88d75730 1549 glyph->pixel_width = it->pixel_width;
06a2c219 1550 glyph->voffset = it->voffset;
88d75730 1551 glyph->type = IMAGE_GLYPH;
06a2c219 1552 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1553 glyph->left_box_line_p = it->start_of_box_run_p;
1554 glyph->right_box_line_p = it->end_of_box_run_p;
1555 glyph->overlaps_vertically_p = 0;
1556 glyph->padding_p = 0;
1557 glyph->glyph_not_available_p = 0;
1558 glyph->face_id = it->face_id;
1559 glyph->u.img_id = img->id;
06a2c219
GM
1560 ++it->glyph_row->used[area];
1561 }
1562 }
1563}
1564
1565
1566/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1567 of the glyph, WIDTH and HEIGHT are the width and height of the
1568 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1569 ascent of the glyph (0 <= ASCENT <= 1). */
1570
1571static void
1572x_append_stretch_glyph (it, object, width, height, ascent)
1573 struct it *it;
1574 Lisp_Object object;
1575 int width, height;
1576 double ascent;
1577{
1578 struct glyph *glyph;
1579 enum glyph_row_area area = it->area;
1580
1581 xassert (ascent >= 0 && ascent <= 1);
1582
1583 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1584 if (glyph < it->glyph_row->glyphs[area + 1])
1585 {
06a2c219
GM
1586 glyph->charpos = CHARPOS (it->position);
1587 glyph->object = object;
88d75730 1588 glyph->pixel_width = width;
06a2c219 1589 glyph->voffset = it->voffset;
88d75730 1590 glyph->type = STRETCH_GLYPH;
06a2c219 1591 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1592 glyph->left_box_line_p = it->start_of_box_run_p;
1593 glyph->right_box_line_p = it->end_of_box_run_p;
1594 glyph->overlaps_vertically_p = 0;
1595 glyph->padding_p = 0;
1596 glyph->glyph_not_available_p = 0;
1597 glyph->face_id = it->face_id;
1598 glyph->u.stretch.ascent = height * ascent;
1599 glyph->u.stretch.height = height;
06a2c219
GM
1600 ++it->glyph_row->used[area];
1601 }
1602}
1603
1604
1605/* Produce a stretch glyph for iterator IT. IT->object is the value
1606 of the glyph property displayed. The value must be a list
1607 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1608 being recognized:
1609
1610 1. `:width WIDTH' specifies that the space should be WIDTH *
1611 canonical char width wide. WIDTH may be an integer or floating
1612 point number.
1613
1614 2. `:relative-width FACTOR' specifies that the width of the stretch
1615 should be computed from the width of the first character having the
1616 `glyph' property, and should be FACTOR times that width.
1617
1618 3. `:align-to HPOS' specifies that the space should be wide enough
1619 to reach HPOS, a value in canonical character units.
1620
1621 Exactly one of the above pairs must be present.
1622
1623 4. `:height HEIGHT' specifies that the height of the stretch produced
1624 should be HEIGHT, measured in canonical character units.
1625
1626 5. `:relative-height FACTOR' specifies that the height of the the
1627 stretch should be FACTOR times the height of the characters having
1628 the glyph property.
1629
1630 Either none or exactly one of 4 or 5 must be present.
1631
1632 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1633 of the stretch should be used for the ascent of the stretch.
1634 ASCENT must be in the range 0 <= ASCENT <= 100. */
1635
1636#define NUMVAL(X) \
1637 ((INTEGERP (X) || FLOATP (X)) \
1638 ? XFLOATINT (X) \
1639 : - 1)
1640
1641
1642static void
1643x_produce_stretch_glyph (it)
1644 struct it *it;
1645{
1646 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1647#if GLYPH_DEBUG
1648 extern Lisp_Object Qspace;
1649#endif
1650 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1651 extern Lisp_Object QCrelative_width, QCrelative_height;
1652 extern Lisp_Object QCalign_to;
1653 Lisp_Object prop, plist;
1654 double width = 0, height = 0, ascent = 0;
1655 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1656 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1657
1658 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1659
1660 /* List should start with `space'. */
1661 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1662 plist = XCDR (it->object);
1663
1664 /* Compute the width of the stretch. */
1665 if (prop = Fplist_get (plist, QCwidth),
1666 NUMVAL (prop) > 0)
1667 /* Absolute width `:width WIDTH' specified and valid. */
1668 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1669 else if (prop = Fplist_get (plist, QCrelative_width),
1670 NUMVAL (prop) > 0)
1671 {
1672 /* Relative width `:relative-width FACTOR' specified and valid.
1673 Compute the width of the characters having the `glyph'
1674 property. */
1675 struct it it2;
1676 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1677
1678 it2 = *it;
1679 if (it->multibyte_p)
1680 {
1681 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1682 - IT_BYTEPOS (*it));
1683 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1684 }
1685 else
1686 it2.c = *p, it2.len = 1;
1687
1688 it2.glyph_row = NULL;
1689 it2.what = IT_CHARACTER;
1690 x_produce_glyphs (&it2);
1691 width = NUMVAL (prop) * it2.pixel_width;
1692 }
1693 else if (prop = Fplist_get (plist, QCalign_to),
1694 NUMVAL (prop) > 0)
1695 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1696 else
1697 /* Nothing specified -> width defaults to canonical char width. */
1698 width = CANON_X_UNIT (it->f);
1699
1700 /* Compute height. */
1701 if (prop = Fplist_get (plist, QCheight),
1702 NUMVAL (prop) > 0)
1703 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1704 else if (prop = Fplist_get (plist, QCrelative_height),
1705 NUMVAL (prop) > 0)
1706 height = FONT_HEIGHT (font) * NUMVAL (prop);
1707 else
1708 height = FONT_HEIGHT (font);
1709
1710 /* Compute percentage of height used for ascent. If
1711 `:ascent ASCENT' is present and valid, use that. Otherwise,
1712 derive the ascent from the font in use. */
1713 if (prop = Fplist_get (plist, QCascent),
1714 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1715 ascent = NUMVAL (prop) / 100.0;
1716 else
1717 ascent = (double) font->ascent / FONT_HEIGHT (font);
1718
1719 if (width <= 0)
1720 width = 1;
1721 if (height <= 0)
1722 height = 1;
1723
1724 if (it->glyph_row)
1725 {
1726 Lisp_Object object = it->stack[it->sp - 1].string;
1727 if (!STRINGP (object))
1728 object = it->w->buffer;
1729 x_append_stretch_glyph (it, object, width, height, ascent);
1730 }
1731
1732 it->pixel_width = width;
66ac4b0e
GM
1733 it->ascent = it->phys_ascent = height * ascent;
1734 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1735 it->nglyphs = 1;
1736
1737 if (face->box != FACE_NO_BOX)
1738 {
ea2ba0d4
KH
1739 if (face->box_line_width > 0)
1740 {
1741 it->ascent += face->box_line_width;
1742 it->descent += face->box_line_width;
1743 }
06a2c219
GM
1744
1745 if (it->start_of_box_run_p)
ea2ba0d4 1746 it->pixel_width += abs (face->box_line_width);
06a2c219 1747 if (it->end_of_box_run_p)
ea2ba0d4 1748 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1749 }
1750
1751 take_vertical_position_into_account (it);
1752}
1753
b4192550
KH
1754/* Return proper value to be used as baseline offset of font that has
1755 ASCENT and DESCENT to draw characters by the font at the vertical
1756 center of the line of frame F.
1757
1758 Here, out task is to find the value of BOFF in the following figure;
1759
1760 -------------------------+-----------+-
1761 -+-+---------+-+ | |
1762 | | | | | |
1763 | | | | F_ASCENT F_HEIGHT
1764 | | | ASCENT | |
1765 HEIGHT | | | | |
1766 | | |-|-+------+-----------|------- baseline
1767 | | | | BOFF | |
1768 | |---------|-+-+ | |
1769 | | | DESCENT | |
1770 -+-+---------+-+ F_DESCENT |
1771 -------------------------+-----------+-
1772
1773 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1774 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1775 DESCENT = FONT->descent
1776 HEIGHT = FONT_HEIGHT (FONT)
1777 F_DESCENT = (F->output_data.x->font->descent
1778 - F->output_data.x->baseline_offset)
1779 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1780*/
1781
458f45fa
KH
1782#define VCENTER_BASELINE_OFFSET(FONT, F) \
1783 ((FONT)->descent \
1784 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1785 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1786 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1787
1788/* Produce glyphs/get display metrics for the display element IT is
1789 loaded with. See the description of struct display_iterator in
1790 dispextern.h for an overview of struct display_iterator. */
1791
1792static void
1793x_produce_glyphs (it)
1794 struct it *it;
1795{
ee569018
KH
1796 it->glyph_not_available_p = 0;
1797
06a2c219
GM
1798 if (it->what == IT_CHARACTER)
1799 {
1800 XChar2b char2b;
1801 XFontStruct *font;
ee569018 1802 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1803 XCharStruct *pcm;
06a2c219 1804 int font_not_found_p;
b4192550
KH
1805 struct font_info *font_info;
1806 int boff; /* baseline offset */
a4249304
KH
1807 /* We may change it->multibyte_p upon unibyte<->multibyte
1808 conversion. So, save the current value now and restore it
1809 later.
1810
1811 Note: It seems that we don't have to record multibyte_p in
1812 struct glyph because the character code itself tells if or
1813 not the character is multibyte. Thus, in the future, we must
1814 consider eliminating the field `multibyte_p' in the struct
1815 glyph.
1816 */
1817 int saved_multibyte_p = it->multibyte_p;
06a2c219 1818
ee569018
KH
1819 /* Maybe translate single-byte characters to multibyte, or the
1820 other way. */
06a2c219 1821 it->char_to_display = it->c;
ee569018 1822 if (!ASCII_BYTE_P (it->c))
06a2c219 1823 {
ee569018
KH
1824 if (unibyte_display_via_language_environment
1825 && SINGLE_BYTE_CHAR_P (it->c)
1826 && (it->c >= 0240
1827 || !NILP (Vnonascii_translation_table)))
1828 {
1829 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1830 it->multibyte_p = 1;
ee569018
KH
1831 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1832 face = FACE_FROM_ID (it->f, it->face_id);
1833 }
1834 else if (!SINGLE_BYTE_CHAR_P (it->c)
1835 && !it->multibyte_p)
1836 {
1837 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
a4249304 1838 it->multibyte_p = 0;
ee569018
KH
1839 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1840 face = FACE_FROM_ID (it->f, it->face_id);
1841 }
06a2c219
GM
1842 }
1843
ee569018
KH
1844 /* Get font to use. Encode IT->char_to_display. */
1845 x_get_char_face_and_encoding (it->f, it->char_to_display,
1846 it->face_id, &char2b,
1847 it->multibyte_p);
06a2c219
GM
1848 font = face->font;
1849
1850 /* When no suitable font found, use the default font. */
1851 font_not_found_p = font == NULL;
1852 if (font_not_found_p)
b4192550
KH
1853 {
1854 font = FRAME_FONT (it->f);
1855 boff = it->f->output_data.x->baseline_offset;
1856 font_info = NULL;
1857 }
1858 else
1859 {
1860 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1861 boff = font_info->baseline_offset;
1862 if (font_info->vertical_centering)
1863 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1864 }
06a2c219
GM
1865
1866 if (it->char_to_display >= ' '
1867 && (!it->multibyte_p || it->char_to_display < 128))
1868 {
1869 /* Either unibyte or ASCII. */
1870 int stretched_p;
1871
1872 it->nglyphs = 1;
06a2c219
GM
1873
1874 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1875 it->ascent = font->ascent + boff;
1876 it->descent = font->descent - boff;
474848ac
GM
1877
1878 if (pcm)
1879 {
1880 it->phys_ascent = pcm->ascent + boff;
1881 it->phys_descent = pcm->descent - boff;
1882 it->pixel_width = pcm->width;
1883 }
1884 else
1885 {
1886 it->glyph_not_available_p = 1;
1887 it->phys_ascent = font->ascent + boff;
1888 it->phys_descent = font->descent - boff;
1889 it->pixel_width = FONT_WIDTH (font);
1890 }
06a2c219
GM
1891
1892 /* If this is a space inside a region of text with
1893 `space-width' property, change its width. */
1894 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1895 if (stretched_p)
1896 it->pixel_width *= XFLOATINT (it->space_width);
1897
1898 /* If face has a box, add the box thickness to the character
1899 height. If character has a box line to the left and/or
1900 right, add the box line width to the character's width. */
1901 if (face->box != FACE_NO_BOX)
1902 {
1903 int thick = face->box_line_width;
1904
ea2ba0d4
KH
1905 if (thick > 0)
1906 {
1907 it->ascent += thick;
1908 it->descent += thick;
1909 }
1910 else
1911 thick = -thick;
1912
06a2c219
GM
1913 if (it->start_of_box_run_p)
1914 it->pixel_width += thick;
1915 if (it->end_of_box_run_p)
1916 it->pixel_width += thick;
1917 }
1918
1919 /* If face has an overline, add the height of the overline
1920 (1 pixel) and a 1 pixel margin to the character height. */
1921 if (face->overline_p)
1922 it->ascent += 2;
1923
1924 take_vertical_position_into_account (it);
1925
1926 /* If we have to actually produce glyphs, do it. */
1927 if (it->glyph_row)
1928 {
1929 if (stretched_p)
1930 {
1931 /* Translate a space with a `space-width' property
1932 into a stretch glyph. */
1933 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1934 x_append_stretch_glyph (it, it->object, it->pixel_width,
1935 it->ascent + it->descent, ascent);
1936 }
1937 else
1938 x_append_glyph (it);
1939
1940 /* If characters with lbearing or rbearing are displayed
1941 in this line, record that fact in a flag of the
1942 glyph row. This is used to optimize X output code. */
1c7e22fd 1943 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1944 it->glyph_row->contains_overlapping_glyphs_p = 1;
1945 }
1946 }
1947 else if (it->char_to_display == '\n')
1948 {
1949 /* A newline has no width but we need the height of the line. */
1950 it->pixel_width = 0;
1951 it->nglyphs = 0;
b4192550
KH
1952 it->ascent = it->phys_ascent = font->ascent + boff;
1953 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1954
ea2ba0d4
KH
1955 if (face->box != FACE_NO_BOX
1956 && face->box_line_width > 0)
06a2c219 1957 {
ea2ba0d4
KH
1958 it->ascent += face->box_line_width;
1959 it->descent += face->box_line_width;
06a2c219
GM
1960 }
1961 }
1962 else if (it->char_to_display == '\t')
1963 {
1964 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1965 int x = it->current_x + it->continuation_lines_width;
06a2c219 1966 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1967
1968 /* If the distance from the current position to the next tab
1969 stop is less than a canonical character width, use the
1970 tab stop after that. */
1971 if (next_tab_x - x < CANON_X_UNIT (it->f))
1972 next_tab_x += tab_width;
06a2c219
GM
1973
1974 it->pixel_width = next_tab_x - x;
1975 it->nglyphs = 1;
b4192550
KH
1976 it->ascent = it->phys_ascent = font->ascent + boff;
1977 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1978
1979 if (it->glyph_row)
1980 {
1981 double ascent = (double) it->ascent / (it->ascent + it->descent);
1982 x_append_stretch_glyph (it, it->object, it->pixel_width,
1983 it->ascent + it->descent, ascent);
1984 }
1985 }
1986 else
1987 {
1988 /* A multi-byte character. Assume that the display width of the
1989 character is the width of the character multiplied by the
b4192550 1990 width of the font. */
06a2c219 1991
b4192550
KH
1992 /* If we found a font, this font should give us the right
1993 metrics. If we didn't find a font, use the frame's
1994 default font and calculate the width of the character
1995 from the charset width; this is what old redisplay code
1996 did. */
1997 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1998 if (font_not_found_p || !pcm)
1999 {
2000 int charset = CHAR_CHARSET (it->char_to_display);
2001
2002 it->glyph_not_available_p = 1;
2003 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2004 * CHARSET_WIDTH (charset));
2005 it->phys_ascent = font->ascent + boff;
2006 it->phys_descent = font->descent - boff;
2007 }
2008 else
2009 {
2010 it->pixel_width = pcm->width;
2011 it->phys_ascent = pcm->ascent + boff;
2012 it->phys_descent = pcm->descent - boff;
2013 if (it->glyph_row
2014 && (pcm->lbearing < 0
2015 || pcm->rbearing > pcm->width))
2016 it->glyph_row->contains_overlapping_glyphs_p = 1;
2017 }
b4192550
KH
2018 it->nglyphs = 1;
2019 it->ascent = font->ascent + boff;
2020 it->descent = font->descent - boff;
06a2c219
GM
2021 if (face->box != FACE_NO_BOX)
2022 {
2023 int thick = face->box_line_width;
ea2ba0d4
KH
2024
2025 if (thick > 0)
2026 {
2027 it->ascent += thick;
2028 it->descent += thick;
2029 }
2030 else
2031 thick = - thick;
06a2c219
GM
2032
2033 if (it->start_of_box_run_p)
2034 it->pixel_width += thick;
2035 if (it->end_of_box_run_p)
2036 it->pixel_width += thick;
2037 }
2038
2039 /* If face has an overline, add the height of the overline
2040 (1 pixel) and a 1 pixel margin to the character height. */
2041 if (face->overline_p)
2042 it->ascent += 2;
2043
2044 take_vertical_position_into_account (it);
2045
2046 if (it->glyph_row)
2047 x_append_glyph (it);
2048 }
a4249304 2049 it->multibyte_p = saved_multibyte_p;
06a2c219 2050 }
b4192550
KH
2051 else if (it->what == IT_COMPOSITION)
2052 {
2053 /* Note: A composition is represented as one glyph in the
2054 glyph matrix. There are no padding glyphs. */
2055 XChar2b char2b;
2056 XFontStruct *font;
ee569018 2057 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2058 XCharStruct *pcm;
2059 int font_not_found_p;
2060 struct font_info *font_info;
2061 int boff; /* baseline offset */
2062 struct composition *cmp = composition_table[it->cmp_id];
2063
2064 /* Maybe translate single-byte characters to multibyte. */
2065 it->char_to_display = it->c;
2066 if (unibyte_display_via_language_environment
2067 && SINGLE_BYTE_CHAR_P (it->c)
2068 && (it->c >= 0240
2069 || (it->c >= 0200
2070 && !NILP (Vnonascii_translation_table))))
2071 {
2072 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2073 }
2074
2075 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2076 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2077 face = FACE_FROM_ID (it->f, it->face_id);
2078 x_get_char_face_and_encoding (it->f, it->char_to_display,
2079 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2080 font = face->font;
2081
2082 /* When no suitable font found, use the default font. */
2083 font_not_found_p = font == NULL;
2084 if (font_not_found_p)
2085 {
2086 font = FRAME_FONT (it->f);
2087 boff = it->f->output_data.x->baseline_offset;
2088 font_info = NULL;
2089 }
2090 else
2091 {
2092 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2093 boff = font_info->baseline_offset;
2094 if (font_info->vertical_centering)
2095 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2096 }
2097
2098 /* There are no padding glyphs, so there is only one glyph to
2099 produce for the composition. Important is that pixel_width,
2100 ascent and descent are the values of what is drawn by
2101 draw_glyphs (i.e. the values of the overall glyphs composed). */
2102 it->nglyphs = 1;
2103
2104 /* If we have not yet calculated pixel size data of glyphs of
2105 the composition for the current face font, calculate them
2106 now. Theoretically, we have to check all fonts for the
2107 glyphs, but that requires much time and memory space. So,
2108 here we check only the font of the first glyph. This leads
2109 to incorrect display very rarely, and C-l (recenter) can
2110 correct the display anyway. */
2111 if (cmp->font != (void *) font)
2112 {
2113 /* Ascent and descent of the font of the first character of
2114 this composition (adjusted by baseline offset). Ascent
2115 and descent of overall glyphs should not be less than
2116 them respectively. */
2117 int font_ascent = font->ascent + boff;
2118 int font_descent = font->descent - boff;
2119 /* Bounding box of the overall glyphs. */
2120 int leftmost, rightmost, lowest, highest;
329bed06 2121 int i, width, ascent, descent;
b4192550
KH
2122
2123 cmp->font = (void *) font;
2124
2125 /* Initialize the bounding box. */
1bdeec2e
KH
2126 if (font_info
2127 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2128 {
2129 width = pcm->width;
2130 ascent = pcm->ascent;
2131 descent = pcm->descent;
2132 }
2133 else
2134 {
2135 width = FONT_WIDTH (font);
2136 ascent = font->ascent;
2137 descent = font->descent;
2138 }
2139
2140 rightmost = width;
2141 lowest = - descent + boff;
2142 highest = ascent + boff;
b4192550 2143 leftmost = 0;
329bed06 2144
b4192550
KH
2145 if (font_info
2146 && font_info->default_ascent
2147 && CHAR_TABLE_P (Vuse_default_ascent)
2148 && !NILP (Faref (Vuse_default_ascent,
2149 make_number (it->char_to_display))))
2150 highest = font_info->default_ascent + boff;
2151
2152 /* Draw the first glyph at the normal position. It may be
2153 shifted to right later if some other glyphs are drawn at
2154 the left. */
2155 cmp->offsets[0] = 0;
2156 cmp->offsets[1] = boff;
2157
2158 /* Set cmp->offsets for the remaining glyphs. */
2159 for (i = 1; i < cmp->glyph_len; i++)
2160 {
2161 int left, right, btm, top;
2162 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2163 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2164
2165 face = FACE_FROM_ID (it->f, face_id);
2166 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2167 it->multibyte_p);
b4192550
KH
2168 font = face->font;
2169 if (font == NULL)
2170 {
2171 font = FRAME_FONT (it->f);
2172 boff = it->f->output_data.x->baseline_offset;
2173 font_info = NULL;
2174 }
2175 else
2176 {
2177 font_info
2178 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2179 boff = font_info->baseline_offset;
2180 if (font_info->vertical_centering)
2181 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2182 }
2183
1bdeec2e
KH
2184 if (font_info
2185 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2186 {
2187 width = pcm->width;
2188 ascent = pcm->ascent;
2189 descent = pcm->descent;
2190 }
2191 else
2192 {
2193 width = FONT_WIDTH (font);
1bdeec2e
KH
2194 ascent = 1;
2195 descent = 0;
329bed06 2196 }
b4192550
KH
2197
2198 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2199 {
2200 /* Relative composition with or without
2201 alternate chars. */
329bed06
GM
2202 left = (leftmost + rightmost - width) / 2;
2203 btm = - descent + boff;
b4192550
KH
2204 if (font_info && font_info->relative_compose
2205 && (! CHAR_TABLE_P (Vignore_relative_composition)
2206 || NILP (Faref (Vignore_relative_composition,
2207 make_number (ch)))))
2208 {
2209
329bed06 2210 if (- descent >= font_info->relative_compose)
b4192550
KH
2211 /* One extra pixel between two glyphs. */
2212 btm = highest + 1;
329bed06 2213 else if (ascent <= 0)
b4192550 2214 /* One extra pixel between two glyphs. */
329bed06 2215 btm = lowest - 1 - ascent - descent;
b4192550
KH
2216 }
2217 }
2218 else
2219 {
2220 /* A composition rule is specified by an integer
2221 value that encodes global and new reference
2222 points (GREF and NREF). GREF and NREF are
2223 specified by numbers as below:
2224
2225 0---1---2 -- ascent
2226 | |
2227 | |
2228 | |
2229 9--10--11 -- center
2230 | |
2231 ---3---4---5--- baseline
2232 | |
2233 6---7---8 -- descent
2234 */
2235 int rule = COMPOSITION_RULE (cmp, i);
2236 int gref, nref, grefx, grefy, nrefx, nrefy;
2237
2238 COMPOSITION_DECODE_RULE (rule, gref, nref);
2239 grefx = gref % 3, nrefx = nref % 3;
2240 grefy = gref / 3, nrefy = nref / 3;
2241
2242 left = (leftmost
2243 + grefx * (rightmost - leftmost) / 2
329bed06 2244 - nrefx * width / 2);
b4192550
KH
2245 btm = ((grefy == 0 ? highest
2246 : grefy == 1 ? 0
2247 : grefy == 2 ? lowest
2248 : (highest + lowest) / 2)
329bed06
GM
2249 - (nrefy == 0 ? ascent + descent
2250 : nrefy == 1 ? descent - boff
b4192550 2251 : nrefy == 2 ? 0
329bed06 2252 : (ascent + descent) / 2));
b4192550
KH
2253 }
2254
2255 cmp->offsets[i * 2] = left;
329bed06 2256 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2257
2258 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2259 right = left + width;
2260 top = btm + descent + ascent;
b4192550
KH
2261 if (left < leftmost)
2262 leftmost = left;
2263 if (right > rightmost)
2264 rightmost = right;
2265 if (top > highest)
2266 highest = top;
2267 if (btm < lowest)
2268 lowest = btm;
2269 }
2270
2271 /* If there are glyphs whose x-offsets are negative,
2272 shift all glyphs to the right and make all x-offsets
2273 non-negative. */
2274 if (leftmost < 0)
2275 {
2276 for (i = 0; i < cmp->glyph_len; i++)
2277 cmp->offsets[i * 2] -= leftmost;
2278 rightmost -= leftmost;
2279 }
2280
2281 cmp->pixel_width = rightmost;
2282 cmp->ascent = highest;
2283 cmp->descent = - lowest;
2284 if (cmp->ascent < font_ascent)
2285 cmp->ascent = font_ascent;
2286 if (cmp->descent < font_descent)
2287 cmp->descent = font_descent;
2288 }
2289
2290 it->pixel_width = cmp->pixel_width;
2291 it->ascent = it->phys_ascent = cmp->ascent;
2292 it->descent = it->phys_descent = cmp->descent;
2293
2294 if (face->box != FACE_NO_BOX)
2295 {
2296 int thick = face->box_line_width;
ea2ba0d4
KH
2297
2298 if (thick > 0)
2299 {
2300 it->ascent += thick;
2301 it->descent += thick;
2302 }
2303 else
2304 thick = - thick;
b4192550
KH
2305
2306 if (it->start_of_box_run_p)
2307 it->pixel_width += thick;
2308 if (it->end_of_box_run_p)
2309 it->pixel_width += thick;
2310 }
2311
2312 /* If face has an overline, add the height of the overline
2313 (1 pixel) and a 1 pixel margin to the character height. */
2314 if (face->overline_p)
2315 it->ascent += 2;
2316
2317 take_vertical_position_into_account (it);
2318
2319 if (it->glyph_row)
2320 x_append_composite_glyph (it);
2321 }
06a2c219
GM
2322 else if (it->what == IT_IMAGE)
2323 x_produce_image_glyph (it);
2324 else if (it->what == IT_STRETCH)
2325 x_produce_stretch_glyph (it);
2326
3017fdd1
GM
2327 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2328 because this isn't true for images with `:ascent 100'. */
2329 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2330 if (it->area == TEXT_AREA)
2331 it->current_x += it->pixel_width;
66ac4b0e 2332
d365f5bb
GM
2333 it->descent += it->extra_line_spacing;
2334
06a2c219
GM
2335 it->max_ascent = max (it->max_ascent, it->ascent);
2336 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2337 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2338 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2339}
2340
2341
2342/* Estimate the pixel height of the mode or top line on frame F.
2343 FACE_ID specifies what line's height to estimate. */
2344
2345int
2346x_estimate_mode_line_height (f, face_id)
2347 struct frame *f;
2348 enum face_id face_id;
2349{
43281ee3 2350 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2351
2352 /* This function is called so early when Emacs starts that the face
2353 cache and mode line face are not yet initialized. */
2354 if (FRAME_FACE_CACHE (f))
2355 {
2356 struct face *face = FACE_FROM_ID (f, face_id);
2357 if (face)
43281ee3
GM
2358 {
2359 if (face->font)
2360 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2361 if (face->box_line_width > 0)
2362 height += 2 * face->box_line_width;
43281ee3 2363 }
06a2c219
GM
2364 }
2365
2366 return height;
2367}
2368
2369\f
2370/***********************************************************************
2371 Glyph display
2372 ***********************************************************************/
2373
2374/* A sequence of glyphs to be drawn in the same face.
2375
2376 This data structure is not really completely X specific, so it
2377 could possibly, at least partially, be useful for other systems. It
2378 is currently not part of the external redisplay interface because
2379 it's not clear what other systems will need. */
2380
2381struct glyph_string
2382{
2383 /* X-origin of the string. */
2384 int x;
2385
2386 /* Y-origin and y-position of the base line of this string. */
2387 int y, ybase;
2388
2389 /* The width of the string, not including a face extension. */
2390 int width;
2391
2392 /* The width of the string, including a face extension. */
2393 int background_width;
2394
2395 /* The height of this string. This is the height of the line this
2396 string is drawn in, and can be different from the height of the
2397 font the string is drawn in. */
2398 int height;
2399
2400 /* Number of pixels this string overwrites in front of its x-origin.
2401 This number is zero if the string has an lbearing >= 0; it is
2402 -lbearing, if the string has an lbearing < 0. */
2403 int left_overhang;
2404
2405 /* Number of pixels this string overwrites past its right-most
2406 nominal x-position, i.e. x + width. Zero if the string's
2407 rbearing is <= its nominal width, rbearing - width otherwise. */
2408 int right_overhang;
2409
2410 /* The frame on which the glyph string is drawn. */
2411 struct frame *f;
2412
2413 /* The window on which the glyph string is drawn. */
2414 struct window *w;
2415
2416 /* X display and window for convenience. */
2417 Display *display;
2418 Window window;
2419
2420 /* The glyph row for which this string was built. It determines the
2421 y-origin and height of the string. */
2422 struct glyph_row *row;
2423
2424 /* The area within row. */
2425 enum glyph_row_area area;
2426
2427 /* Characters to be drawn, and number of characters. */
2428 XChar2b *char2b;
2429 int nchars;
2430
06a2c219
GM
2431 /* A face-override for drawing cursors, mouse face and similar. */
2432 enum draw_glyphs_face hl;
2433
2434 /* Face in which this string is to be drawn. */
2435 struct face *face;
2436
2437 /* Font in which this string is to be drawn. */
2438 XFontStruct *font;
2439
2440 /* Font info for this string. */
2441 struct font_info *font_info;
2442
b4192550
KH
2443 /* Non-null means this string describes (part of) a composition.
2444 All characters from char2b are drawn composed. */
2445 struct composition *cmp;
06a2c219
GM
2446
2447 /* Index of this glyph string's first character in the glyph
b4192550
KH
2448 definition of CMP. If this is zero, this glyph string describes
2449 the first character of a composition. */
06a2c219
GM
2450 int gidx;
2451
2452 /* 1 means this glyph strings face has to be drawn to the right end
2453 of the window's drawing area. */
2454 unsigned extends_to_end_of_line_p : 1;
2455
2456 /* 1 means the background of this string has been drawn. */
2457 unsigned background_filled_p : 1;
2458
2459 /* 1 means glyph string must be drawn with 16-bit functions. */
2460 unsigned two_byte_p : 1;
2461
2462 /* 1 means that the original font determined for drawing this glyph
2463 string could not be loaded. The member `font' has been set to
2464 the frame's default font in this case. */
2465 unsigned font_not_found_p : 1;
2466
2467 /* 1 means that the face in which this glyph string is drawn has a
2468 stipple pattern. */
2469 unsigned stippled_p : 1;
2470
66ac4b0e
GM
2471 /* 1 means only the foreground of this glyph string must be drawn,
2472 and we should use the physical height of the line this glyph
2473 string appears in as clip rect. */
2474 unsigned for_overlaps_p : 1;
2475
06a2c219
GM
2476 /* The GC to use for drawing this glyph string. */
2477 GC gc;
2478
2479 /* A pointer to the first glyph in the string. This glyph
2480 corresponds to char2b[0]. Needed to draw rectangles if
2481 font_not_found_p is 1. */
2482 struct glyph *first_glyph;
2483
2484 /* Image, if any. */
2485 struct image *img;
2486
2487 struct glyph_string *next, *prev;
2488};
2489
2490
5c187dee 2491#if 0
06a2c219
GM
2492
2493static void
2494x_dump_glyph_string (s)
2495 struct glyph_string *s;
2496{
2497 fprintf (stderr, "glyph string\n");
2498 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2499 s->x, s->y, s->width, s->height);
2500 fprintf (stderr, " ybase = %d\n", s->ybase);
2501 fprintf (stderr, " hl = %d\n", s->hl);
2502 fprintf (stderr, " left overhang = %d, right = %d\n",
2503 s->left_overhang, s->right_overhang);
2504 fprintf (stderr, " nchars = %d\n", s->nchars);
2505 fprintf (stderr, " extends to end of line = %d\n",
2506 s->extends_to_end_of_line_p);
2507 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2508 fprintf (stderr, " bg width = %d\n", s->background_width);
2509}
2510
2511#endif /* GLYPH_DEBUG */
2512
2513
2514
2515static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2516 struct glyph_string **,
2517 struct glyph_string *,
2518 struct glyph_string *));
2519static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2520 struct glyph_string **,
2521 struct glyph_string *,
2522 struct glyph_string *));
2523static void x_append_glyph_string P_ ((struct glyph_string **,
2524 struct glyph_string **,
2525 struct glyph_string *));
2526static int x_left_overwritten P_ ((struct glyph_string *));
2527static int x_left_overwriting P_ ((struct glyph_string *));
2528static int x_right_overwritten P_ ((struct glyph_string *));
2529static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2530static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2531 int));
06a2c219
GM
2532static void x_init_glyph_string P_ ((struct glyph_string *,
2533 XChar2b *, struct window *,
2534 struct glyph_row *,
2535 enum glyph_row_area, int,
2536 enum draw_glyphs_face));
2537static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2538 enum glyph_row_area, int, int,
66ac4b0e 2539 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2540static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2541static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2542static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2543 int));
2544static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2545static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2546static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2547static void x_draw_glyph_string P_ ((struct glyph_string *));
2548static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2549static void x_set_cursor_gc P_ ((struct glyph_string *));
2550static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2551static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2552static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2553 int *, int *));
2554static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2555static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2556 unsigned long *, double, int));
06a2c219 2557static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2558 double, int, unsigned long));
06a2c219
GM
2559static void x_setup_relief_colors P_ ((struct glyph_string *));
2560static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2561static void x_draw_image_relief P_ ((struct glyph_string *));
2562static void x_draw_image_foreground P_ ((struct glyph_string *));
2563static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2564static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2565static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2566 int, int, int));
2567static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2568 int, int, int, int, XRectangle *));
2569static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2570 int, int, int, XRectangle *));
66ac4b0e
GM
2571static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2572 enum glyph_row_area));
209f68d9
GM
2573static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2574 struct glyph_row *,
2575 enum glyph_row_area, int, int));
06a2c219 2576
163dcff3
GM
2577#if GLYPH_DEBUG
2578static void x_check_font P_ ((struct frame *, XFontStruct *));
2579#endif
2580
06a2c219 2581
06a2c219
GM
2582/* Append the list of glyph strings with head H and tail T to the list
2583 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2584
2585static INLINE void
2586x_append_glyph_string_lists (head, tail, h, t)
2587 struct glyph_string **head, **tail;
2588 struct glyph_string *h, *t;
2589{
2590 if (h)
2591 {
2592 if (*head)
2593 (*tail)->next = h;
2594 else
2595 *head = h;
2596 h->prev = *tail;
2597 *tail = t;
2598 }
2599}
2600
2601
2602/* Prepend the list of glyph strings with head H and tail T to the
2603 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2604 result. */
2605
2606static INLINE void
2607x_prepend_glyph_string_lists (head, tail, h, t)
2608 struct glyph_string **head, **tail;
2609 struct glyph_string *h, *t;
2610{
2611 if (h)
2612 {
2613 if (*head)
2614 (*head)->prev = t;
2615 else
2616 *tail = t;
2617 t->next = *head;
2618 *head = h;
2619 }
2620}
2621
2622
2623/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2624 Set *HEAD and *TAIL to the resulting list. */
2625
2626static INLINE void
2627x_append_glyph_string (head, tail, s)
2628 struct glyph_string **head, **tail;
2629 struct glyph_string *s;
2630{
2631 s->next = s->prev = NULL;
2632 x_append_glyph_string_lists (head, tail, s, s);
2633}
2634
2635
2636/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2637 face. */
2638
2639static void
2640x_set_cursor_gc (s)
2641 struct glyph_string *s;
2642{
2643 if (s->font == FRAME_FONT (s->f)
2644 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2645 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2646 && !s->cmp)
06a2c219
GM
2647 s->gc = s->f->output_data.x->cursor_gc;
2648 else
2649 {
2650 /* Cursor on non-default face: must merge. */
2651 XGCValues xgcv;
2652 unsigned long mask;
2653
2654 xgcv.background = s->f->output_data.x->cursor_pixel;
2655 xgcv.foreground = s->face->background;
2656
2657 /* If the glyph would be invisible, try a different foreground. */
2658 if (xgcv.foreground == xgcv.background)
2659 xgcv.foreground = s->face->foreground;
2660 if (xgcv.foreground == xgcv.background)
2661 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2662 if (xgcv.foreground == xgcv.background)
2663 xgcv.foreground = s->face->foreground;
2664
2665 /* Make sure the cursor is distinct from text in this face. */
2666 if (xgcv.background == s->face->background
2667 && xgcv.foreground == s->face->foreground)
2668 {
2669 xgcv.background = s->face->foreground;
2670 xgcv.foreground = s->face->background;
2671 }
2672
2673 IF_DEBUG (x_check_font (s->f, s->font));
2674 xgcv.font = s->font->fid;
2675 xgcv.graphics_exposures = False;
2676 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2677
2678 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2679 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2680 mask, &xgcv);
2681 else
2682 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2683 = XCreateGC (s->display, s->window, mask, &xgcv);
2684
2685 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2686 }
2687}
2688
2689
2690/* Set up S->gc of glyph string S for drawing text in mouse face. */
2691
2692static void
2693x_set_mouse_face_gc (s)
2694 struct glyph_string *s;
2695{
2696 int face_id;
ee569018 2697 struct face *face;
06a2c219 2698
e4ded23c 2699 /* What face has to be used last for the mouse face? */
06a2c219 2700 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2701 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2702 if (face == NULL)
2703 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2704
033e3e18
GM
2705 if (s->first_glyph->type == CHAR_GLYPH)
2706 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2707 else
2708 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2709 s->face = FACE_FROM_ID (s->f, face_id);
2710 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2711
2712 /* If font in this face is same as S->font, use it. */
2713 if (s->font == s->face->font)
2714 s->gc = s->face->gc;
2715 else
2716 {
2717 /* Otherwise construct scratch_cursor_gc with values from FACE
2718 but font FONT. */
2719 XGCValues xgcv;
2720 unsigned long mask;
2721
2722 xgcv.background = s->face->background;
2723 xgcv.foreground = s->face->foreground;
2724 IF_DEBUG (x_check_font (s->f, s->font));
2725 xgcv.font = s->font->fid;
2726 xgcv.graphics_exposures = False;
2727 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2728
2729 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2730 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2731 mask, &xgcv);
2732 else
2733 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2734 = XCreateGC (s->display, s->window, mask, &xgcv);
2735
2736 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2737 }
2738
2739 xassert (s->gc != 0);
2740}
2741
2742
2743/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2744 Faces to use in the mode line have already been computed when the
2745 matrix was built, so there isn't much to do, here. */
2746
2747static INLINE void
2748x_set_mode_line_face_gc (s)
2749 struct glyph_string *s;
2750{
2751 s->gc = s->face->gc;
06a2c219
GM
2752}
2753
2754
2755/* Set S->gc of glyph string S for drawing that glyph string. Set
2756 S->stippled_p to a non-zero value if the face of S has a stipple
2757 pattern. */
2758
2759static INLINE void
2760x_set_glyph_string_gc (s)
2761 struct glyph_string *s;
2762{
209f68d9
GM
2763 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2764
06a2c219
GM
2765 if (s->hl == DRAW_NORMAL_TEXT)
2766 {
2767 s->gc = s->face->gc;
2768 s->stippled_p = s->face->stipple != 0;
2769 }
2770 else if (s->hl == DRAW_INVERSE_VIDEO)
2771 {
2772 x_set_mode_line_face_gc (s);
2773 s->stippled_p = s->face->stipple != 0;
2774 }
2775 else if (s->hl == DRAW_CURSOR)
2776 {
2777 x_set_cursor_gc (s);
2778 s->stippled_p = 0;
2779 }
2780 else if (s->hl == DRAW_MOUSE_FACE)
2781 {
2782 x_set_mouse_face_gc (s);
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else if (s->hl == DRAW_IMAGE_RAISED
2786 || s->hl == DRAW_IMAGE_SUNKEN)
2787 {
2788 s->gc = s->face->gc;
2789 s->stippled_p = s->face->stipple != 0;
2790 }
2791 else
2792 {
2793 s->gc = s->face->gc;
2794 s->stippled_p = s->face->stipple != 0;
2795 }
2796
2797 /* GC must have been set. */
2798 xassert (s->gc != 0);
2799}
2800
2801
2802/* Return in *R the clipping rectangle for glyph string S. */
2803
2804static void
2805x_get_glyph_string_clip_rect (s, r)
2806 struct glyph_string *s;
2807 XRectangle *r;
2808{
2809 if (s->row->full_width_p)
2810 {
2811 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2812 int canon_x = CANON_X_UNIT (s->f);
2813
2814 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2815 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2816
2817 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2818 {
1da3fd71 2819 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2820 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2821 r->x -= width;
2822 }
2823
b9432a85 2824 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2825
06a2c219
GM
2826 /* Unless displaying a mode or menu bar line, which are always
2827 fully visible, clip to the visible part of the row. */
2828 if (s->w->pseudo_window_p)
2829 r->height = s->row->visible_height;
2830 else
2831 r->height = s->height;
2832 }
2833 else
2834 {
2835 /* This is a text line that may be partially visible. */
2836 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2837 r->width = window_box_width (s->w, s->area);
2838 r->height = s->row->visible_height;
2839 }
2840
2841 /* Don't use S->y for clipping because it doesn't take partially
2842 visible lines into account. For example, it can be negative for
2843 partially visible lines at the top of a window. */
2844 if (!s->row->full_width_p
2845 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2846 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2847 else
2848 r->y = max (0, s->row->y);
06a2c219 2849
9ea173e8 2850 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2851 at the top of the window. */
9ea173e8 2852 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2853 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2854
2855 /* If S draws overlapping rows, it's sufficient to use the top and
2856 bottom of the window for clipping because this glyph string
2857 intentionally draws over other lines. */
2858 if (s->for_overlaps_p)
2859 {
045dee35 2860 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2861 r->height = window_text_bottom_y (s->w) - r->y;
2862 }
2863
2864 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2865}
2866
2867
2868/* Set clipping for output of glyph string S. S may be part of a mode
2869 line or menu if we don't have X toolkit support. */
2870
2871static INLINE void
2872x_set_glyph_string_clipping (s)
2873 struct glyph_string *s;
2874{
2875 XRectangle r;
2876 x_get_glyph_string_clip_rect (s, &r);
2877 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2878}
2879
2880
2881/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2882 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2883
2884static INLINE void
2885x_compute_glyph_string_overhangs (s)
2886 struct glyph_string *s;
2887{
b4192550 2888 if (s->cmp == NULL
06a2c219
GM
2889 && s->first_glyph->type == CHAR_GLYPH)
2890 {
2891 XCharStruct cs;
2892 int direction, font_ascent, font_descent;
2893 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2894 &font_ascent, &font_descent, &cs);
2895 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2896 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2897 }
2898}
2899
2900
2901/* Compute overhangs and x-positions for glyph string S and its
2902 predecessors, or successors. X is the starting x-position for S.
2903 BACKWARD_P non-zero means process predecessors. */
2904
2905static void
2906x_compute_overhangs_and_x (s, x, backward_p)
2907 struct glyph_string *s;
2908 int x;
2909 int backward_p;
2910{
2911 if (backward_p)
2912 {
2913 while (s)
2914 {
2915 x_compute_glyph_string_overhangs (s);
2916 x -= s->width;
2917 s->x = x;
2918 s = s->prev;
2919 }
2920 }
2921 else
2922 {
2923 while (s)
2924 {
2925 x_compute_glyph_string_overhangs (s);
2926 s->x = x;
2927 x += s->width;
2928 s = s->next;
2929 }
2930 }
2931}
2932
2933
2934/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2935 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2936 assumed to be zero. */
06a2c219
GM
2937
2938static void
2939x_get_glyph_overhangs (glyph, f, left, right)
2940 struct glyph *glyph;
2941 struct frame *f;
2942 int *left, *right;
2943{
06a2c219
GM
2944 *left = *right = 0;
2945
b4192550 2946 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2947 {
2948 XFontStruct *font;
2949 struct face *face;
2950 struct font_info *font_info;
2951 XChar2b char2b;
ee569018
KH
2952 XCharStruct *pcm;
2953
2954 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2955 font = face->font;
2956 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2957 if (font
2958 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2959 {
06a2c219
GM
2960 if (pcm->rbearing > pcm->width)
2961 *right = pcm->rbearing - pcm->width;
2962 if (pcm->lbearing < 0)
2963 *left = -pcm->lbearing;
2964 }
2965 }
2966}
2967
2968
2969/* Return the index of the first glyph preceding glyph string S that
2970 is overwritten by S because of S's left overhang. Value is -1
2971 if no glyphs are overwritten. */
2972
2973static int
2974x_left_overwritten (s)
2975 struct glyph_string *s;
2976{
2977 int k;
2978
2979 if (s->left_overhang)
2980 {
2981 int x = 0, i;
2982 struct glyph *glyphs = s->row->glyphs[s->area];
2983 int first = s->first_glyph - glyphs;
2984
2985 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2986 x -= glyphs[i].pixel_width;
2987
2988 k = i + 1;
2989 }
2990 else
2991 k = -1;
2992
2993 return k;
2994}
2995
2996
2997/* Return the index of the first glyph preceding glyph string S that
2998 is overwriting S because of its right overhang. Value is -1 if no
2999 glyph in front of S overwrites S. */
3000
3001static int
3002x_left_overwriting (s)
3003 struct glyph_string *s;
3004{
3005 int i, k, x;
3006 struct glyph *glyphs = s->row->glyphs[s->area];
3007 int first = s->first_glyph - glyphs;
3008
3009 k = -1;
3010 x = 0;
3011 for (i = first - 1; i >= 0; --i)
3012 {
3013 int left, right;
3014 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3015 if (x + right > 0)
3016 k = i;
3017 x -= glyphs[i].pixel_width;
3018 }
3019
3020 return k;
3021}
3022
3023
3024/* Return the index of the last glyph following glyph string S that is
3025 not overwritten by S because of S's right overhang. Value is -1 if
3026 no such glyph is found. */
3027
3028static int
3029x_right_overwritten (s)
3030 struct glyph_string *s;
3031{
3032 int k = -1;
3033
3034 if (s->right_overhang)
3035 {
3036 int x = 0, i;
3037 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3038 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3039 int end = s->row->used[s->area];
3040
3041 for (i = first; i < end && s->right_overhang > x; ++i)
3042 x += glyphs[i].pixel_width;
3043
3044 k = i;
3045 }
3046
3047 return k;
3048}
3049
3050
3051/* Return the index of the last glyph following glyph string S that
3052 overwrites S because of its left overhang. Value is negative
3053 if no such glyph is found. */
3054
3055static int
3056x_right_overwriting (s)
3057 struct glyph_string *s;
3058{
3059 int i, k, x;
3060 int end = s->row->used[s->area];
3061 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3062 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3063
3064 k = -1;
3065 x = 0;
3066 for (i = first; i < end; ++i)
3067 {
3068 int left, right;
3069 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3070 if (x - left < 0)
3071 k = i;
3072 x += glyphs[i].pixel_width;
3073 }
3074
3075 return k;
3076}
3077
3078
3079/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3080
3081static INLINE void
3082x_clear_glyph_string_rect (s, x, y, w, h)
3083 struct glyph_string *s;
3084 int x, y, w, h;
3085{
3086 XGCValues xgcv;
3087 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3088 XSetForeground (s->display, s->gc, xgcv.background);
3089 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3090 XSetForeground (s->display, s->gc, xgcv.foreground);
3091}
3092
3093
3094/* Draw the background of glyph_string S. If S->background_filled_p
3095 is non-zero don't draw it. FORCE_P non-zero means draw the
3096 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3097 when a string preceding S draws into the background of S, or S
3098 contains the first component of a composition. */
06a2c219
GM
3099
3100static void
3101x_draw_glyph_string_background (s, force_p)
3102 struct glyph_string *s;
3103 int force_p;
3104{
3105 /* Nothing to do if background has already been drawn or if it
3106 shouldn't be drawn in the first place. */
3107 if (!s->background_filled_p)
3108 {
ea2ba0d4
KH
3109 int box_line_width = max (s->face->box_line_width, 0);
3110
b4192550 3111 if (s->stippled_p)
06a2c219
GM
3112 {
3113 /* Fill background with a stipple pattern. */
3114 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3115 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3116 s->y + box_line_width,
06a2c219 3117 s->background_width,
ea2ba0d4 3118 s->height - 2 * box_line_width);
06a2c219
GM
3119 XSetFillStyle (s->display, s->gc, FillSolid);
3120 s->background_filled_p = 1;
3121 }
ea2ba0d4 3122 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3123 || s->font_not_found_p
3124 || s->extends_to_end_of_line_p
06a2c219
GM
3125 || force_p)
3126 {
ea2ba0d4 3127 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3128 s->background_width,
ea2ba0d4 3129 s->height - 2 * box_line_width);
06a2c219
GM
3130 s->background_filled_p = 1;
3131 }
3132 }
3133}
3134
3135
3136/* Draw the foreground of glyph string S. */
3137
3138static void
3139x_draw_glyph_string_foreground (s)
3140 struct glyph_string *s;
3141{
3142 int i, x;
3143
3144 /* If first glyph of S has a left box line, start drawing the text
3145 of S to the right of that box line. */
3146 if (s->face->box != FACE_NO_BOX
3147 && s->first_glyph->left_box_line_p)
ea2ba0d4 3148 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3149 else
3150 x = s->x;
3151
b4192550
KH
3152 /* Draw characters of S as rectangles if S's font could not be
3153 loaded. */
3154 if (s->font_not_found_p)
06a2c219 3155 {
b4192550 3156 for (i = 0; i < s->nchars; ++i)
06a2c219 3157 {
b4192550
KH
3158 struct glyph *g = s->first_glyph + i;
3159 XDrawRectangle (s->display, s->window,
3160 s->gc, x, s->y, g->pixel_width - 1,
3161 s->height - 1);
3162 x += g->pixel_width;
06a2c219
GM
3163 }
3164 }
3165 else
3166 {
b4192550
KH
3167 char *char1b = (char *) s->char2b;
3168 int boff = s->font_info->baseline_offset;
06a2c219 3169
b4192550
KH
3170 if (s->font_info->vertical_centering)
3171 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3172
3173 /* If we can use 8-bit functions, condense S->char2b. */
3174 if (!s->two_byte_p)
3175 for (i = 0; i < s->nchars; ++i)
3176 char1b[i] = s->char2b[i].byte2;
3177
3178 /* Draw text with XDrawString if background has already been
3179 filled. Otherwise, use XDrawImageString. (Note that
3180 XDrawImageString is usually faster than XDrawString.) Always
3181 use XDrawImageString when drawing the cursor so that there is
3182 no chance that characters under a box cursor are invisible. */
3183 if (s->for_overlaps_p
3184 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3185 {
3186 /* Draw characters with 16-bit or 8-bit functions. */
3187 if (s->two_byte_p)
3188 XDrawString16 (s->display, s->window, s->gc, x,
3189 s->ybase - boff, s->char2b, s->nchars);
3190 else
3191 XDrawString (s->display, s->window, s->gc, x,
3192 s->ybase - boff, char1b, s->nchars);
3193 }
06a2c219
GM
3194 else
3195 {
b4192550
KH
3196 if (s->two_byte_p)
3197 XDrawImageString16 (s->display, s->window, s->gc, x,
3198 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3199 else
b4192550
KH
3200 XDrawImageString (s->display, s->window, s->gc, x,
3201 s->ybase - boff, char1b, s->nchars);
3202 }
3203 }
3204}
06a2c219 3205
b4192550 3206/* Draw the foreground of composite glyph string S. */
06a2c219 3207
b4192550
KH
3208static void
3209x_draw_composite_glyph_string_foreground (s)
3210 struct glyph_string *s;
3211{
3212 int i, x;
06a2c219 3213
b4192550
KH
3214 /* If first glyph of S has a left box line, start drawing the text
3215 of S to the right of that box line. */
3216 if (s->face->box != FACE_NO_BOX
3217 && s->first_glyph->left_box_line_p)
ea2ba0d4 3218 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3219 else
3220 x = s->x;
06a2c219 3221
b4192550
KH
3222 /* S is a glyph string for a composition. S->gidx is the index of
3223 the first character drawn for glyphs of this composition.
3224 S->gidx == 0 means we are drawing the very first character of
3225 this composition. */
06a2c219 3226
b4192550
KH
3227 /* Draw a rectangle for the composition if the font for the very
3228 first character of the composition could not be loaded. */
3229 if (s->font_not_found_p)
3230 {
3231 if (s->gidx == 0)
3232 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3233 s->width - 1, s->height - 1);
3234 }
3235 else
3236 {
3237 for (i = 0; i < s->nchars; i++, ++s->gidx)
3238 XDrawString16 (s->display, s->window, s->gc,
3239 x + s->cmp->offsets[s->gidx * 2],
3240 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3241 s->char2b + i, 1);
06a2c219
GM
3242 }
3243}
3244
3245
80c32bcc
GM
3246#ifdef USE_X_TOOLKIT
3247
3e71d8f2 3248static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3249
3e71d8f2
GM
3250
3251/* Return the frame on which widget WIDGET is used.. Abort if frame
3252 cannot be determined. */
3253
e851c833 3254static struct frame *
3e71d8f2 3255x_frame_of_widget (widget)
80c32bcc 3256 Widget widget;
80c32bcc 3257{
80c32bcc 3258 struct x_display_info *dpyinfo;
5c187dee 3259 Lisp_Object tail;
3e71d8f2
GM
3260 struct frame *f;
3261
80c32bcc
GM
3262 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3263
3264 /* Find the top-level shell of the widget. Note that this function
3265 can be called when the widget is not yet realized, so XtWindow
3266 (widget) == 0. That's the reason we can't simply use
3267 x_any_window_to_frame. */
3268 while (!XtIsTopLevelShell (widget))
3269 widget = XtParent (widget);
3270
3271 /* Look for a frame with that top-level widget. Allocate the color
3272 on that frame to get the right gamma correction value. */
3273 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3274 if (GC_FRAMEP (XCAR (tail))
3275 && (f = XFRAME (XCAR (tail)),
3276 (f->output_data.nothing != 1
3277 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3278 && f->output_data.x->widget == widget)
3e71d8f2 3279 return f;
80c32bcc
GM
3280
3281 abort ();
3282}
3283
3e71d8f2
GM
3284
3285/* Allocate the color COLOR->pixel on the screen and display of
3286 widget WIDGET in colormap CMAP. If an exact match cannot be
3287 allocated, try the nearest color available. Value is non-zero
3288 if successful. This is called from lwlib. */
3289
3290int
3291x_alloc_nearest_color_for_widget (widget, cmap, color)
3292 Widget widget;
3293 Colormap cmap;
3294 XColor *color;
3295{
3296 struct frame *f = x_frame_of_widget (widget);
3297 return x_alloc_nearest_color (f, cmap, color);
3298}
3299
3300
46d516e5
MB
3301/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3302 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3303 If this produces the same color as PIXEL, try a color where all RGB
3304 values have DELTA added. Return the allocated color in *PIXEL.
3305 DISPLAY is the X display, CMAP is the colormap to operate on.
3306 Value is non-zero if successful. */
3307
3308int
3309x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3310 Widget widget;
3311 Display *display;
3312 Colormap cmap;
3313 unsigned long *pixel;
3314 double factor;
3315 int delta;
3316{
3317 struct frame *f = x_frame_of_widget (widget);
3318 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3319}
3320
3321
80c32bcc
GM
3322#endif /* USE_X_TOOLKIT */
3323
3324
f04e1297
GM
3325/* Value is an array of XColor structures for the contents of the
3326 color map of frame F. Set *NCELLS to the size of the array.
3327 Note that this probably shouldn't be called for large color maps,
3328 say a 24-bit TrueColor map. */
3329
3330static const XColor *
3331x_color_cells (f, ncells)
3332 struct frame *f;
3333 int *ncells;
3334{
3335 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3336
3337 if (dpyinfo->color_cells == NULL)
3338 {
3339 Display *display = FRAME_X_DISPLAY (f);
3340 Screen *screen = FRAME_X_SCREEN (f);
3341 int i;
3342
3343 dpyinfo->ncolor_cells
3344 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3345 dpyinfo->color_cells
3346 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3347 * sizeof *dpyinfo->color_cells);
3348
3349 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3350 dpyinfo->color_cells[i].pixel = i;
3351
3352 XQueryColors (display, FRAME_X_COLORMAP (f),
3353 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3354 }
3355
3356 *ncells = dpyinfo->ncolor_cells;
3357 return dpyinfo->color_cells;
3358}
3359
3360
3361/* On frame F, translate pixel colors to RGB values for the NCOLORS
3362 colors in COLORS. Use cached information, if available. */
3363
3364void
3365x_query_colors (f, colors, ncolors)
3366 struct frame *f;
3367 XColor *colors;
3368 int ncolors;
3369{
3370 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3371
3372 if (dpyinfo->color_cells)
3373 {
3374 int i;
3375 for (i = 0; i < ncolors; ++i)
3376 {
3377 unsigned long pixel = colors[i].pixel;
3378 xassert (pixel < dpyinfo->ncolor_cells);
3379 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3380 colors[i] = dpyinfo->color_cells[pixel];
3381 }
3382 }
3383 else
3384 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3385}
3386
3387
3388/* On frame F, translate pixel color to RGB values for the color in
3389 COLOR. Use cached information, if available. */
3390
3391void
3392x_query_color (f, color)
3393 struct frame *f;
3394 XColor *color;
3395{
3396 x_query_colors (f, color, 1);
3397}
3398
3399
06a2c219
GM
3400/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3401 CMAP. If an exact match can't be allocated, try the nearest color
3402 available. Value is non-zero if successful. Set *COLOR to the
3403 color allocated. */
3404
3405int
80c32bcc
GM
3406x_alloc_nearest_color (f, cmap, color)
3407 struct frame *f;
06a2c219
GM
3408 Colormap cmap;
3409 XColor *color;
3410{
80c32bcc
GM
3411 Display *display = FRAME_X_DISPLAY (f);
3412 Screen *screen = FRAME_X_SCREEN (f);
3413 int rc;
3414
3415 gamma_correct (f, color);
3416 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3417 if (rc == 0)
3418 {
3419 /* If we got to this point, the colormap is full, so we're going
3420 to try to get the next closest color. The algorithm used is
3421 a least-squares matching, which is what X uses for closest
3422 color matching with StaticColor visuals. */
3423 int nearest, i;
3424 unsigned long nearest_delta = ~0;
f04e1297
GM
3425 int ncells;
3426 const XColor *cells = x_color_cells (f, &ncells);
06a2c219
GM
3427
3428 for (nearest = i = 0; i < ncells; ++i)
3429 {
3430 long dred = (color->red >> 8) - (cells[i].red >> 8);
3431 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3432 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3433 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3434
3435 if (delta < nearest_delta)
3436 {
3437 nearest = i;
3438 nearest_delta = delta;
3439 }
3440 }
3441
3442 color->red = cells[nearest].red;
3443 color->green = cells[nearest].green;
3444 color->blue = cells[nearest].blue;
3445 rc = XAllocColor (display, cmap, color);
3446 }
35efe0a1
GM
3447 else
3448 {
3449 /* If allocation succeeded, and the allocated pixel color is not
3450 equal to a cached pixel color recorded earlier, there was a
3451 change in the colormap, so clear the color cache. */
3452 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3453 XColor *cached_color;
3454
3455 if (dpyinfo->color_cells
3456 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3457 (cached_color->red != color->red
3458 || cached_color->blue != color->blue
3459 || cached_color->green != color->green)))
35efe0a1
GM
3460 {
3461 xfree (dpyinfo->color_cells);
3462 dpyinfo->color_cells = NULL;
3463 dpyinfo->ncolor_cells = 0;
3464 }
3465 }
06a2c219 3466
d9c545da
GM
3467#ifdef DEBUG_X_COLORS
3468 if (rc)
3469 register_color (color->pixel);
3470#endif /* DEBUG_X_COLORS */
3471
06a2c219
GM
3472 return rc;
3473}
3474
3475
d9c545da
GM
3476/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3477 It's necessary to do this instead of just using PIXEL directly to
3478 get color reference counts right. */
3479
3480unsigned long
3481x_copy_color (f, pixel)
3482 struct frame *f;
3483 unsigned long pixel;
3484{
3485 XColor color;
3486
3487 color.pixel = pixel;
3488 BLOCK_INPUT;
f04e1297 3489 x_query_color (f, &color);
d9c545da
GM
3490 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3491 UNBLOCK_INPUT;
3492#ifdef DEBUG_X_COLORS
3493 register_color (pixel);
3494#endif
3495 return color.pixel;
3496}
3497
3498
3e71d8f2
GM
3499/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3500 It's necessary to do this instead of just using PIXEL directly to
3501 get color reference counts right. */
3502
3503unsigned long
3504x_copy_dpy_color (dpy, cmap, pixel)
3505 Display *dpy;
3506 Colormap cmap;
3507 unsigned long pixel;
3508{
3509 XColor color;
3510
3511 color.pixel = pixel;
3512 BLOCK_INPUT;
3513 XQueryColor (dpy, cmap, &color);
3514 XAllocColor (dpy, cmap, &color);
3515 UNBLOCK_INPUT;
3516#ifdef DEBUG_X_COLORS
3517 register_color (pixel);
3518#endif
3519 return color.pixel;
3520}
3521
3522
6d8b0acd 3523/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3524 boosted.
6d8b0acd 3525
d7361edf
MB
3526 Nominally, highlight colors for `3d' faces are calculated by
3527 brightening an object's color by a constant scale factor, but this
3528 doesn't yield good results for dark colors, so for colors who's
3529 brightness is less than this value (on a scale of 0-65535) have an
3530 use an additional additive factor.
6d8b0acd
MB
3531
3532 The value here is set so that the default menu-bar/mode-line color
3533 (grey75) will not have its highlights changed at all. */
3534#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3535
3536
06a2c219
GM
3537/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3538 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3539 If this produces the same color as PIXEL, try a color where all RGB
3540 values have DELTA added. Return the allocated color in *PIXEL.
3541 DISPLAY is the X display, CMAP is the colormap to operate on.
3542 Value is non-zero if successful. */
3543
3544static int
3545x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3546 struct frame *f;
3547 Display *display;
3548 Colormap cmap;
3549 unsigned long *pixel;
68c45bf0 3550 double factor;
06a2c219
GM
3551 int delta;
3552{
3553 XColor color, new;
6d8b0acd 3554 long bright;
06a2c219
GM
3555 int success_p;
3556
3557 /* Get RGB color values. */
3558 color.pixel = *pixel;
f04e1297 3559 x_query_color (f, &color);
06a2c219
GM
3560
3561 /* Change RGB values by specified FACTOR. Avoid overflow! */
3562 xassert (factor >= 0);
3563 new.red = min (0xffff, factor * color.red);
3564 new.green = min (0xffff, factor * color.green);
3565 new.blue = min (0xffff, factor * color.blue);
3566
d7361edf
MB
3567 /* Calculate brightness of COLOR. */
3568 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3569
3570 /* We only boost colors that are darker than
3571 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3572 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3573 /* Make an additive adjustment to NEW, because it's dark enough so
3574 that scaling by FACTOR alone isn't enough. */
3575 {
3576 /* How far below the limit this color is (0 - 1, 1 being darker). */
3577 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3578 /* The additive adjustment. */
d7361edf 3579 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3580
3581 if (factor < 1)
3582 {
6d8b0acd
MB
3583 new.red = max (0, new.red - min_delta);
3584 new.green = max (0, new.green - min_delta);
3585 new.blue = max (0, new.blue - min_delta);
3586 }
3587 else
3588 {
3589 new.red = min (0xffff, min_delta + new.red);
3590 new.green = min (0xffff, min_delta + new.green);
3591 new.blue = min (0xffff, min_delta + new.blue);
3592 }
3593 }
3594
06a2c219 3595 /* Try to allocate the color. */
80c32bcc 3596 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3597 if (success_p)
3598 {
3599 if (new.pixel == *pixel)
3600 {
3601 /* If we end up with the same color as before, try adding
3602 delta to the RGB values. */
0d605c67 3603 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3604
3605 new.red = min (0xffff, delta + color.red);
3606 new.green = min (0xffff, delta + color.green);
3607 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3608 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3609 }
3610 else
3611 success_p = 1;
3612 *pixel = new.pixel;
3613 }
3614
3615 return success_p;
3616}
3617
3618
3619/* Set up the foreground color for drawing relief lines of glyph
3620 string S. RELIEF is a pointer to a struct relief containing the GC
3621 with which lines will be drawn. Use a color that is FACTOR or
3622 DELTA lighter or darker than the relief's background which is found
3623 in S->f->output_data.x->relief_background. If such a color cannot
3624 be allocated, use DEFAULT_PIXEL, instead. */
3625
3626static void
3627x_setup_relief_color (f, relief, factor, delta, default_pixel)
3628 struct frame *f;
3629 struct relief *relief;
68c45bf0 3630 double factor;
06a2c219
GM
3631 int delta;
3632 unsigned long default_pixel;
3633{
3634 XGCValues xgcv;
3635 struct x_output *di = f->output_data.x;
3636 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3637 unsigned long pixel;
3638 unsigned long background = di->relief_background;
43bd1b2b 3639 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3640 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3641 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3642
3643 xgcv.graphics_exposures = False;
3644 xgcv.line_width = 1;
3645
3646 /* Free previously allocated color. The color cell will be reused
3647 when it has been freed as many times as it was allocated, so this
3648 doesn't affect faces using the same colors. */
3649 if (relief->gc
3650 && relief->allocated_p)
3651 {
0d605c67 3652 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3653 relief->allocated_p = 0;
3654 }
3655
3656 /* Allocate new color. */
3657 xgcv.foreground = default_pixel;
3658 pixel = background;
dcd08bfb
GM
3659 if (dpyinfo->n_planes != 1
3660 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3661 {
3662 relief->allocated_p = 1;
3663 xgcv.foreground = relief->pixel = pixel;
3664 }
3665
3666 if (relief->gc == 0)
3667 {
dcd08bfb 3668 xgcv.stipple = dpyinfo->gray;
06a2c219 3669 mask |= GCStipple;
dcd08bfb 3670 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3671 }
3672 else
dcd08bfb 3673 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3674}
3675
3676
3677/* Set up colors for the relief lines around glyph string S. */
3678
3679static void
3680x_setup_relief_colors (s)
3681 struct glyph_string *s;
3682{
3683 struct x_output *di = s->f->output_data.x;
3684 unsigned long color;
3685
3686 if (s->face->use_box_color_for_shadows_p)
3687 color = s->face->box_color;
3688 else
3689 {
3690 XGCValues xgcv;
3691
3692 /* Get the background color of the face. */
3693 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3694 color = xgcv.background;
3695 }
3696
3697 if (di->white_relief.gc == 0
3698 || color != di->relief_background)
3699 {
3700 di->relief_background = color;
3701 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3702 WHITE_PIX_DEFAULT (s->f));
3703 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3704 BLACK_PIX_DEFAULT (s->f));
3705 }
3706}
3707
3708
3709/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3710 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3711 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3712 relief. LEFT_P non-zero means draw a relief on the left side of
3713 the rectangle. RIGHT_P non-zero means draw a relief on the right
3714 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3715 when drawing. */
3716
3717static void
3718x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3719 raised_p, left_p, right_p, clip_rect)
3720 struct frame *f;
3721 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3722 XRectangle *clip_rect;
3723{
3724 int i;
3725 GC gc;
3726
3727 if (raised_p)
3728 gc = f->output_data.x->white_relief.gc;
3729 else
3730 gc = f->output_data.x->black_relief.gc;
3731 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3732
3733 /* Top. */
3734 for (i = 0; i < width; ++i)
3735 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3736 left_x + i * left_p, top_y + i,
3737 right_x + 1 - i * right_p, top_y + i);
3738
3739 /* Left. */
3740 if (left_p)
3741 for (i = 0; i < width; ++i)
3742 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3743 left_x + i, top_y + i, left_x + i, bottom_y - i);
3744
3745 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3746 if (raised_p)
3747 gc = f->output_data.x->black_relief.gc;
3748 else
3749 gc = f->output_data.x->white_relief.gc;
3750 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3751
3752 /* Bottom. */
3753 for (i = 0; i < width; ++i)
3754 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3755 left_x + i * left_p, bottom_y - i,
3756 right_x + 1 - i * right_p, bottom_y - i);
3757
3758 /* Right. */
3759 if (right_p)
3760 for (i = 0; i < width; ++i)
3761 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3762 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3763
3764 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3765}
3766
3767
3768/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3769 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3770 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3771 left side of the rectangle. RIGHT_P non-zero means draw a line
3772 on the right side of the rectangle. CLIP_RECT is the clipping
3773 rectangle to use when drawing. */
3774
3775static void
3776x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3777 left_p, right_p, clip_rect)
3778 struct glyph_string *s;
3779 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3780 XRectangle *clip_rect;
3781{
3782 XGCValues xgcv;
3783
3784 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3785 XSetForeground (s->display, s->gc, s->face->box_color);
3786 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3787
3788 /* Top. */
3789 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3790 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3791
3792 /* Left. */
3793 if (left_p)
3794 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3795 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3796
3797 /* Bottom. */
3798 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3799 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3800
3801 /* Right. */
3802 if (right_p)
3803 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3804 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3805
3806 XSetForeground (s->display, s->gc, xgcv.foreground);
3807 XSetClipMask (s->display, s->gc, None);
3808}
3809
3810
3811/* Draw a box around glyph string S. */
3812
3813static void
3814x_draw_glyph_string_box (s)
3815 struct glyph_string *s;
3816{
3817 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3818 int left_p, right_p;
3819 struct glyph *last_glyph;
3820 XRectangle clip_rect;
3821
3822 last_x = window_box_right (s->w, s->area);
3823 if (s->row->full_width_p
3824 && !s->w->pseudo_window_p)
3825 {
110859fc 3826 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3827 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3828 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3829 }
3830
3831 /* The glyph that may have a right box line. */
b4192550 3832 last_glyph = (s->cmp || s->img
06a2c219
GM
3833 ? s->first_glyph
3834 : s->first_glyph + s->nchars - 1);
3835
ea2ba0d4 3836 width = abs (s->face->box_line_width);
06a2c219
GM
3837 raised_p = s->face->box == FACE_RAISED_BOX;
3838 left_x = s->x;
3839 right_x = ((s->row->full_width_p
1da3fd71 3840 ? last_x - 1
a7aeb2de 3841 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3842 top_y = s->y;
3843 bottom_y = top_y + s->height - 1;
3844
3845 left_p = (s->first_glyph->left_box_line_p
3846 || (s->hl == DRAW_MOUSE_FACE
3847 && (s->prev == NULL
3848 || s->prev->hl != s->hl)));
3849 right_p = (last_glyph->right_box_line_p
3850 || (s->hl == DRAW_MOUSE_FACE
3851 && (s->next == NULL
3852 || s->next->hl != s->hl)));
3853
3854 x_get_glyph_string_clip_rect (s, &clip_rect);
3855
3856 if (s->face->box == FACE_SIMPLE_BOX)
3857 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3858 left_p, right_p, &clip_rect);
3859 else
3860 {
3861 x_setup_relief_colors (s);
3862 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3863 width, raised_p, left_p, right_p, &clip_rect);
3864 }
3865}
3866
3867
3868/* Draw foreground of image glyph string S. */
3869
3870static void
3871x_draw_image_foreground (s)
3872 struct glyph_string *s;
3873{
3874 int x;
95af8492 3875 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3876
3877 /* If first glyph of S has a left box line, start drawing it to the
3878 right of that line. */
3879 if (s->face->box != FACE_NO_BOX
3880 && s->first_glyph->left_box_line_p)
ea2ba0d4 3881 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3882 else
3883 x = s->x;
3884
3885 /* If there is a margin around the image, adjust x- and y-position
3886 by that margin. */
22d650b8
GM
3887 x += s->img->hmargin;
3888 y += s->img->vmargin;
06a2c219
GM
3889
3890 if (s->img->pixmap)
3891 {
3892 if (s->img->mask)
3893 {
3894 /* We can't set both a clip mask and use XSetClipRectangles
3895 because the latter also sets a clip mask. We also can't
3896 trust on the shape extension to be available
3897 (XShapeCombineRegion). So, compute the rectangle to draw
3898 manually. */
3899 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3900 | GCFunction);
3901 XGCValues xgcv;
3902 XRectangle clip_rect, image_rect, r;
3903
3904 xgcv.clip_mask = s->img->mask;
3905 xgcv.clip_x_origin = x;
3906 xgcv.clip_y_origin = y;
3907 xgcv.function = GXcopy;
3908 XChangeGC (s->display, s->gc, mask, &xgcv);
3909
3910 x_get_glyph_string_clip_rect (s, &clip_rect);
3911 image_rect.x = x;
3912 image_rect.y = y;
3913 image_rect.width = s->img->width;
3914 image_rect.height = s->img->height;
3915 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3916 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3917 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3918 }
3919 else
3920 {
49ad1d99
GM
3921 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3922 XGCValues xgcv;
3923 XRectangle clip_rect, image_rect, r;
3924
3925 x_get_glyph_string_clip_rect (s, &clip_rect);
3926 image_rect.x = x;
3927 image_rect.y = y;
3928 image_rect.width = s->img->width;
3929 image_rect.height = s->img->height;
3930 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3931 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3932 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3933
3934 /* When the image has a mask, we can expect that at
3935 least part of a mouse highlight or a block cursor will
3936 be visible. If the image doesn't have a mask, make
3937 a block cursor visible by drawing a rectangle around
3938 the image. I believe it's looking better if we do
3939 nothing here for mouse-face. */
3940 if (s->hl == DRAW_CURSOR)
3941 XDrawRectangle (s->display, s->window, s->gc, x, y,
3942 s->img->width - 1, s->img->height - 1);
3943 }
3944 }
3945 else
3946 /* Draw a rectangle if image could not be loaded. */
3947 XDrawRectangle (s->display, s->window, s->gc, x, y,
3948 s->img->width - 1, s->img->height - 1);
3949}
3950
3951
3952/* Draw a relief around the image glyph string S. */
3953
3954static void
3955x_draw_image_relief (s)
3956 struct glyph_string *s;
3957{
3958 int x0, y0, x1, y1, thick, raised_p;
3959 XRectangle r;
3960 int x;
95af8492 3961 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3962
3963 /* If first glyph of S has a left box line, start drawing it to the
3964 right of that line. */
3965 if (s->face->box != FACE_NO_BOX
3966 && s->first_glyph->left_box_line_p)
ea2ba0d4 3967 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3968 else
3969 x = s->x;
3970
3971 /* If there is a margin around the image, adjust x- and y-position
3972 by that margin. */
22d650b8
GM
3973 x += s->img->hmargin;
3974 y += s->img->vmargin;
06a2c219
GM
3975
3976 if (s->hl == DRAW_IMAGE_SUNKEN
3977 || s->hl == DRAW_IMAGE_RAISED)
3978 {
9ea173e8 3979 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3980 raised_p = s->hl == DRAW_IMAGE_RAISED;
3981 }
3982 else
3983 {
3984 thick = abs (s->img->relief);
3985 raised_p = s->img->relief > 0;
3986 }
3987
3988 x0 = x - thick;
3989 y0 = y - thick;
3990 x1 = x + s->img->width + thick - 1;
3991 y1 = y + s->img->height + thick - 1;
3992
3993 x_setup_relief_colors (s);
3994 x_get_glyph_string_clip_rect (s, &r);
3995 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3996}
3997
3998
3999/* Draw the foreground of image glyph string S to PIXMAP. */
4000
4001static void
4002x_draw_image_foreground_1 (s, pixmap)
4003 struct glyph_string *s;
4004 Pixmap pixmap;
4005{
4006 int x;
95af8492 4007 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4008
4009 /* If first glyph of S has a left box line, start drawing it to the
4010 right of that line. */
4011 if (s->face->box != FACE_NO_BOX
4012 && s->first_glyph->left_box_line_p)
ea2ba0d4 4013 x = abs (s->face->box_line_width);
06a2c219
GM
4014 else
4015 x = 0;
4016
4017 /* If there is a margin around the image, adjust x- and y-position
4018 by that margin. */
22d650b8
GM
4019 x += s->img->hmargin;
4020 y += s->img->vmargin;
dc43ef94 4021
06a2c219
GM
4022 if (s->img->pixmap)
4023 {
4024 if (s->img->mask)
4025 {
4026 /* We can't set both a clip mask and use XSetClipRectangles
4027 because the latter also sets a clip mask. We also can't
4028 trust on the shape extension to be available
4029 (XShapeCombineRegion). So, compute the rectangle to draw
4030 manually. */
4031 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4032 | GCFunction);
4033 XGCValues xgcv;
4034
4035 xgcv.clip_mask = s->img->mask;
4036 xgcv.clip_x_origin = x;
4037 xgcv.clip_y_origin = y;
4038 xgcv.function = GXcopy;
4039 XChangeGC (s->display, s->gc, mask, &xgcv);
4040
4041 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4042 0, 0, s->img->width, s->img->height, x, y);
4043 XSetClipMask (s->display, s->gc, None);
4044 }
4045 else
4046 {
4047 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4048 0, 0, s->img->width, s->img->height, x, y);
4049
4050 /* When the image has a mask, we can expect that at
4051 least part of a mouse highlight or a block cursor will
4052 be visible. If the image doesn't have a mask, make
4053 a block cursor visible by drawing a rectangle around
4054 the image. I believe it's looking better if we do
4055 nothing here for mouse-face. */
4056 if (s->hl == DRAW_CURSOR)
4057 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4058 s->img->width - 1, s->img->height - 1);
4059 }
4060 }
4061 else
4062 /* Draw a rectangle if image could not be loaded. */
4063 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4064 s->img->width - 1, s->img->height - 1);
4065}
dc43ef94 4066
990ba854 4067
06a2c219
GM
4068/* Draw part of the background of glyph string S. X, Y, W, and H
4069 give the rectangle to draw. */
a9a5b0a5 4070
06a2c219
GM
4071static void
4072x_draw_glyph_string_bg_rect (s, x, y, w, h)
4073 struct glyph_string *s;
4074 int x, y, w, h;
4075{
4076 if (s->stippled_p)
4077 {
4078 /* Fill background with a stipple pattern. */
4079 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4080 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4081 XSetFillStyle (s->display, s->gc, FillSolid);
4082 }
4083 else
4084 x_clear_glyph_string_rect (s, x, y, w, h);
4085}
07e34cb0 4086
b5210ea7 4087
06a2c219 4088/* Draw image glyph string S.
dc43ef94 4089
06a2c219
GM
4090 s->y
4091 s->x +-------------------------
4092 | s->face->box
4093 |
4094 | +-------------------------
4095 | | s->img->margin
4096 | |
4097 | | +-------------------
4098 | | | the image
dc43ef94 4099
06a2c219 4100 */
dc43ef94 4101
06a2c219
GM
4102static void
4103x_draw_image_glyph_string (s)
4104 struct glyph_string *s;
4105{
4106 int x, y;
ea2ba0d4
KH
4107 int box_line_hwidth = abs (s->face->box_line_width);
4108 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4109 int height;
4110 Pixmap pixmap = None;
4111
ea2ba0d4 4112 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4113
4114 /* Fill background with face under the image. Do it only if row is
4115 taller than image or if image has a clip mask to reduce
4116 flickering. */
4117 s->stippled_p = s->face->stipple != 0;
4118 if (height > s->img->height
22d650b8
GM
4119 || s->img->hmargin
4120 || s->img->vmargin
06a2c219
GM
4121 || s->img->mask
4122 || s->img->pixmap == 0
4123 || s->width != s->background_width)
4124 {
ea2ba0d4
KH
4125 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4126 x = s->x + box_line_hwidth;
06a2c219
GM
4127 else
4128 x = s->x;
4129
ea2ba0d4 4130 y = s->y + box_line_vwidth;
06a2c219
GM
4131
4132 if (s->img->mask)
4133 {
f9b5db02
GM
4134 /* Create a pixmap as large as the glyph string. Fill it
4135 with the background color. Copy the image to it, using
4136 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4137 Screen *screen = FRAME_X_SCREEN (s->f);
4138 int depth = DefaultDepthOfScreen (screen);
4139
4140 /* Create a pixmap as large as the glyph string. */
4141 pixmap = XCreatePixmap (s->display, s->window,
4142 s->background_width,
4143 s->height, depth);
4144
4145 /* Don't clip in the following because we're working on the
4146 pixmap. */
4147 XSetClipMask (s->display, s->gc, None);
4148
4149 /* Fill the pixmap with the background color/stipple. */
4150 if (s->stippled_p)
4151 {
4152 /* Fill background with a stipple pattern. */
4153 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4154 XFillRectangle (s->display, pixmap, s->gc,
4155 0, 0, s->background_width, s->height);
4156 XSetFillStyle (s->display, s->gc, FillSolid);
4157 }
4158 else
4159 {
4160 XGCValues xgcv;
4161 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4162 &xgcv);
4163 XSetForeground (s->display, s->gc, xgcv.background);
4164 XFillRectangle (s->display, pixmap, s->gc,
4165 0, 0, s->background_width, s->height);
4166 XSetForeground (s->display, s->gc, xgcv.foreground);
4167 }
4168 }
4169 else
06a2c219
GM
4170 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4171
4172 s->background_filled_p = 1;
4173 }
dc43ef94 4174
06a2c219
GM
4175 /* Draw the foreground. */
4176 if (pixmap != None)
4177 {
4178 x_draw_image_foreground_1 (s, pixmap);
4179 x_set_glyph_string_clipping (s);
4180 XCopyArea (s->display, pixmap, s->window, s->gc,
4181 0, 0, s->background_width, s->height, s->x, s->y);
4182 XFreePixmap (s->display, pixmap);
4183 }
4184 else
4185 x_draw_image_foreground (s);
b5210ea7 4186
06a2c219
GM
4187 /* If we must draw a relief around the image, do it. */
4188 if (s->img->relief
4189 || s->hl == DRAW_IMAGE_RAISED
4190 || s->hl == DRAW_IMAGE_SUNKEN)
4191 x_draw_image_relief (s);
4192}
8c1a6a84 4193
990ba854 4194
06a2c219 4195/* Draw stretch glyph string S. */
dc43ef94 4196
06a2c219
GM
4197static void
4198x_draw_stretch_glyph_string (s)
4199 struct glyph_string *s;
4200{
4201 xassert (s->first_glyph->type == STRETCH_GLYPH);
4202 s->stippled_p = s->face->stipple != 0;
990ba854 4203
06a2c219
GM
4204 if (s->hl == DRAW_CURSOR
4205 && !x_stretch_cursor_p)
4206 {
4207 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4208 as wide as the stretch glyph. */
4209 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4210
06a2c219
GM
4211 /* Draw cursor. */
4212 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4213
06a2c219
GM
4214 /* Clear rest using the GC of the original non-cursor face. */
4215 if (width < s->background_width)
4216 {
4217 GC gc = s->face->gc;
4218 int x = s->x + width, y = s->y;
4219 int w = s->background_width - width, h = s->height;
4220 XRectangle r;
dc43ef94 4221
06a2c219
GM
4222 x_get_glyph_string_clip_rect (s, &r);
4223 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4224
06a2c219
GM
4225 if (s->face->stipple)
4226 {
4227 /* Fill background with a stipple pattern. */
4228 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4229 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4230 XSetFillStyle (s->display, gc, FillSolid);
4231 }
4232 else
4233 {
4234 XGCValues xgcv;
4235 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4236 XSetForeground (s->display, gc, xgcv.background);
4237 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4238 XSetForeground (s->display, gc, xgcv.foreground);
4239 }
4240 }
4241 }
4242 else
4243 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4244 s->height);
4245
4246 s->background_filled_p = 1;
4247}
4248
4249
4250/* Draw glyph string S. */
4251
4252static void
4253x_draw_glyph_string (s)
4254 struct glyph_string *s;
4255{
4256 /* If S draws into the background of its successor, draw the
4257 background of the successor first so that S can draw into it.
4258 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4259 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4260 {
4261 xassert (s->next->img == NULL);
4262 x_set_glyph_string_gc (s->next);
4263 x_set_glyph_string_clipping (s->next);
4264 x_draw_glyph_string_background (s->next, 1);
4265 }
97210f4e 4266
06a2c219
GM
4267 /* Set up S->gc, set clipping and draw S. */
4268 x_set_glyph_string_gc (s);
4269 x_set_glyph_string_clipping (s);
4270
4271 switch (s->first_glyph->type)
4272 {
4273 case IMAGE_GLYPH:
4274 x_draw_image_glyph_string (s);
4275 break;
4276
4277 case STRETCH_GLYPH:
4278 x_draw_stretch_glyph_string (s);
4279 break;
4280
4281 case CHAR_GLYPH:
66ac4b0e
GM
4282 if (s->for_overlaps_p)
4283 s->background_filled_p = 1;
4284 else
4285 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4286 x_draw_glyph_string_foreground (s);
4287 break;
4288
b4192550
KH
4289 case COMPOSITE_GLYPH:
4290 if (s->for_overlaps_p || s->gidx > 0)
4291 s->background_filled_p = 1;
4292 else
4293 x_draw_glyph_string_background (s, 1);
4294 x_draw_composite_glyph_string_foreground (s);
4295 break;
4296
06a2c219
GM
4297 default:
4298 abort ();
4299 }
4300
66ac4b0e 4301 if (!s->for_overlaps_p)
06a2c219 4302 {
66ac4b0e
GM
4303 /* Draw underline. */
4304 if (s->face->underline_p)
4305 {
e24e84cc
GM
4306 unsigned long tem, h;
4307 int y;
06a2c219 4308
e24e84cc 4309 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4310 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4311 h = 1;
e24e84cc
GM
4312
4313 /* Get the underline position. This is the recommended
4314 vertical offset in pixels from the baseline to the top of
4315 the underline. This is a signed value according to the
4316 specs, and its default is
4317
4318 ROUND ((maximum descent) / 2), with
4319 ROUND(x) = floor (x + 0.5) */
4320
4321 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4322 y = s->ybase + (long) tem;
4323 else if (s->face->font)
4324 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4325 else
4326 y = s->height - h;
06a2c219 4327
66ac4b0e 4328 if (s->face->underline_defaulted_p)
e24e84cc
GM
4329 XFillRectangle (s->display, s->window, s->gc,
4330 s->x, y, s->width, h);
66ac4b0e
GM
4331 else
4332 {
4333 XGCValues xgcv;
4334 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4335 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4336 XFillRectangle (s->display, s->window, s->gc,
4337 s->x, y, s->width, h);
66ac4b0e
GM
4338 XSetForeground (s->display, s->gc, xgcv.foreground);
4339 }
dc6f92b8 4340 }
07e34cb0 4341
66ac4b0e
GM
4342 /* Draw overline. */
4343 if (s->face->overline_p)
06a2c219 4344 {
66ac4b0e
GM
4345 unsigned long dy = 0, h = 1;
4346
4347 if (s->face->overline_color_defaulted_p)
4348 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4349 s->width, h);
4350 else
4351 {
4352 XGCValues xgcv;
4353 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4354 XSetForeground (s->display, s->gc, s->face->overline_color);
4355 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4356 s->width, h);
4357 XSetForeground (s->display, s->gc, xgcv.foreground);
4358 }
06a2c219 4359 }
06a2c219 4360
66ac4b0e
GM
4361 /* Draw strike-through. */
4362 if (s->face->strike_through_p)
06a2c219 4363 {
66ac4b0e
GM
4364 unsigned long h = 1;
4365 unsigned long dy = (s->height - h) / 2;
4366
4367 if (s->face->strike_through_color_defaulted_p)
4368 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4369 s->width, h);
4370 else
4371 {
4372 XGCValues xgcv;
4373 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4374 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4375 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4376 s->width, h);
4377 XSetForeground (s->display, s->gc, xgcv.foreground);
4378 }
06a2c219 4379 }
06a2c219 4380
66ac4b0e
GM
4381 /* Draw relief. */
4382 if (s->face->box != FACE_NO_BOX)
4383 x_draw_glyph_string_box (s);
4384 }
06a2c219
GM
4385
4386 /* Reset clipping. */
4387 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4388}
07e34cb0 4389
06a2c219 4390
b4192550
KH
4391static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4392 struct face **, int));
06a2c219 4393
06a2c219 4394
209f68d9
GM
4395/* Fill glyph string S with composition components specified by S->cmp.
4396
b4192550
KH
4397 FACES is an array of faces for all components of this composition.
4398 S->gidx is the index of the first component for S.
4399 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4400 use its physical height for clipping.
06a2c219 4401
b4192550 4402 Value is the index of a component not in S. */
07e34cb0 4403
b4192550
KH
4404static int
4405x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4406 struct glyph_string *s;
b4192550 4407 struct face **faces;
66ac4b0e 4408 int overlaps_p;
07e34cb0 4409{
b4192550 4410 int i;
06a2c219 4411
b4192550 4412 xassert (s);
06a2c219 4413
b4192550 4414 s->for_overlaps_p = overlaps_p;
06a2c219 4415
b4192550
KH
4416 s->face = faces[s->gidx];
4417 s->font = s->face->font;
4418 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4419
b4192550
KH
4420 /* For all glyphs of this composition, starting at the offset
4421 S->gidx, until we reach the end of the definition or encounter a
4422 glyph that requires the different face, add it to S. */
4423 ++s->nchars;
4424 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4425 ++s->nchars;
06a2c219 4426
b4192550
KH
4427 /* All glyph strings for the same composition has the same width,
4428 i.e. the width set for the first component of the composition. */
06a2c219 4429
06a2c219
GM
4430 s->width = s->first_glyph->pixel_width;
4431
4432 /* If the specified font could not be loaded, use the frame's
4433 default font, but record the fact that we couldn't load it in
4434 the glyph string so that we can draw rectangles for the
4435 characters of the glyph string. */
4436 if (s->font == NULL)
4437 {
4438 s->font_not_found_p = 1;
4439 s->font = FRAME_FONT (s->f);
4440 }
4441
4442 /* Adjust base line for subscript/superscript text. */
4443 s->ybase += s->first_glyph->voffset;
4444
4445 xassert (s->face && s->face->gc);
4446
4447 /* This glyph string must always be drawn with 16-bit functions. */
4448 s->two_byte_p = 1;
b4192550
KH
4449
4450 return s->gidx + s->nchars;
06a2c219
GM
4451}
4452
4453
209f68d9
GM
4454/* Fill glyph string S from a sequence of character glyphs.
4455
06a2c219 4456 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4457 first glyph to consider, END is the index of the last + 1.
4458 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4459 use its physical height for clipping.
66ac4b0e
GM
4460
4461 Value is the index of the first glyph not in S. */
06a2c219
GM
4462
4463static int
66ac4b0e 4464x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4465 struct glyph_string *s;
4466 int face_id;
66ac4b0e 4467 int start, end, overlaps_p;
06a2c219
GM
4468{
4469 struct glyph *glyph, *last;
4470 int voffset;
ee569018 4471 int glyph_not_available_p;
06a2c219 4472
06a2c219
GM
4473 xassert (s->f == XFRAME (s->w->frame));
4474 xassert (s->nchars == 0);
4475 xassert (start >= 0 && end > start);
4476
66ac4b0e 4477 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4478 glyph = s->row->glyphs[s->area] + start;
4479 last = s->row->glyphs[s->area] + end;
4480 voffset = glyph->voffset;
4481
ee569018
KH
4482 glyph_not_available_p = glyph->glyph_not_available_p;
4483
06a2c219
GM
4484 while (glyph < last
4485 && glyph->type == CHAR_GLYPH
4486 && glyph->voffset == voffset
ee569018
KH
4487 /* Same face id implies same font, nowadays. */
4488 && glyph->face_id == face_id
4489 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4490 {
ee569018
KH
4491 int two_byte_p;
4492
06a2c219 4493 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4494 s->char2b + s->nchars,
4495 &two_byte_p);
4496 s->two_byte_p = two_byte_p;
06a2c219
GM
4497 ++s->nchars;
4498 xassert (s->nchars <= end - start);
4499 s->width += glyph->pixel_width;
4500 ++glyph;
4501 }
4502
4503 s->font = s->face->font;
4504 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4505
4506 /* If the specified font could not be loaded, use the frame's font,
4507 but record the fact that we couldn't load it in
4508 S->font_not_found_p so that we can draw rectangles for the
4509 characters of the glyph string. */
ee569018 4510 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4511 {
4512 s->font_not_found_p = 1;
4513 s->font = FRAME_FONT (s->f);
4514 }
4515
4516 /* Adjust base line for subscript/superscript text. */
4517 s->ybase += voffset;
66ac4b0e 4518
06a2c219
GM
4519 xassert (s->face && s->face->gc);
4520 return glyph - s->row->glyphs[s->area];
07e34cb0 4521}
dc6f92b8 4522
06a2c219
GM
4523
4524/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4525
dfcf069d 4526static void
06a2c219
GM
4527x_fill_image_glyph_string (s)
4528 struct glyph_string *s;
4529{
4530 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4531 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4532 xassert (s->img);
43d120d8 4533 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4534 s->font = s->face->font;
4535 s->width = s->first_glyph->pixel_width;
4536
4537 /* Adjust base line for subscript/superscript text. */
4538 s->ybase += s->first_glyph->voffset;
4539}
4540
4541
209f68d9 4542/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4543
209f68d9
GM
4544 ROW is the glyph row in which the glyphs are found, AREA is the
4545 area within the row. START is the index of the first glyph to
4546 consider, END is the index of the last + 1.
4547
4548 Value is the index of the first glyph not in S. */
4549
4550static int
4551x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4552 struct glyph_string *s;
209f68d9
GM
4553 struct glyph_row *row;
4554 enum glyph_row_area area;
4555 int start, end;
06a2c219 4556{
209f68d9
GM
4557 struct glyph *glyph, *last;
4558 int voffset, face_id;
4559
06a2c219 4560 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4561
4562 glyph = s->row->glyphs[s->area] + start;
4563 last = s->row->glyphs[s->area] + end;
4564 face_id = glyph->face_id;
4565 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4566 s->font = s->face->font;
209f68d9
GM
4567 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4568 s->width = glyph->pixel_width;
4569 voffset = glyph->voffset;
4570
4571 for (++glyph;
4572 (glyph < last
4573 && glyph->type == STRETCH_GLYPH
4574 && glyph->voffset == voffset
4575 && glyph->face_id == face_id);
4576 ++glyph)
4577 s->width += glyph->pixel_width;
06a2c219
GM
4578
4579 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4580 s->ybase += voffset;
4581
4582 xassert (s->face && s->face->gc);
4583 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4584}
4585
4586
4587/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4588 of XChar2b structures for S; it can't be allocated in
4589 x_init_glyph_string because it must be allocated via `alloca'. W
4590 is the window on which S is drawn. ROW and AREA are the glyph row
4591 and area within the row from which S is constructed. START is the
4592 index of the first glyph structure covered by S. HL is a
4593 face-override for drawing S. */
4594
4595static void
4596x_init_glyph_string (s, char2b, w, row, area, start, hl)
4597 struct glyph_string *s;
4598 XChar2b *char2b;
4599 struct window *w;
4600 struct glyph_row *row;
4601 enum glyph_row_area area;
4602 int start;
4603 enum draw_glyphs_face hl;
4604{
4605 bzero (s, sizeof *s);
4606 s->w = w;
4607 s->f = XFRAME (w->frame);
4608 s->display = FRAME_X_DISPLAY (s->f);
4609 s->window = FRAME_X_WINDOW (s->f);
4610 s->char2b = char2b;
4611 s->hl = hl;
4612 s->row = row;
4613 s->area = area;
4614 s->first_glyph = row->glyphs[area] + start;
4615 s->height = row->height;
4616 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4617
9ea173e8
GM
4618 /* Display the internal border below the tool-bar window. */
4619 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4620 s->y -= s->f->output_data.x->internal_border_width;
4621
4622 s->ybase = s->y + row->ascent;
4623}
4624
4625
4626/* Set background width of glyph string S. START is the index of the
4627 first glyph following S. LAST_X is the right-most x-position + 1
4628 in the drawing area. */
4629
4630static INLINE void
4631x_set_glyph_string_background_width (s, start, last_x)
4632 struct glyph_string *s;
4633 int start;
4634 int last_x;
4635{
4636 /* If the face of this glyph string has to be drawn to the end of
4637 the drawing area, set S->extends_to_end_of_line_p. */
4638 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4639
4640 if (start == s->row->used[s->area]
4641 && s->hl == DRAW_NORMAL_TEXT
eb79f5cc
GM
4642 && s->area == TEXT_AREA
4643 && (s->row->fill_line_p
06a2c219
GM
4644 || s->face->background != default_face->background
4645 || s->face->stipple != default_face->stipple))
4646 s->extends_to_end_of_line_p = 1;
4647
4648 /* If S extends its face to the end of the line, set its
4649 background_width to the distance to the right edge of the drawing
4650 area. */
4651 if (s->extends_to_end_of_line_p)
1da3fd71 4652 s->background_width = last_x - s->x + 1;
06a2c219
GM
4653 else
4654 s->background_width = s->width;
4655}
4656
4657
4658/* Add a glyph string for a stretch glyph to the list of strings
4659 between HEAD and TAIL. START is the index of the stretch glyph in
4660 row area AREA of glyph row ROW. END is the index of the last glyph
4661 in that glyph row area. X is the current output position assigned
4662 to the new glyph string constructed. HL overrides that face of the
4663 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4664 is the right-most x-position of the drawing area. */
4665
8abee2e1
DL
4666/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4667 and below -- keep them on one line. */
4668#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4669 do \
4670 { \
4671 s = (struct glyph_string *) alloca (sizeof *s); \
4672 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4673 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4674 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4675 s->x = (X); \
4676 } \
4677 while (0)
4678
4679
4680/* Add a glyph string for an image glyph to the list of strings
4681 between HEAD and TAIL. START is the index of the image glyph in
4682 row area AREA of glyph row ROW. END is the index of the last glyph
4683 in that glyph row area. X is the current output position assigned
4684 to the new glyph string constructed. HL overrides that face of the
4685 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4686 is the right-most x-position of the drawing area. */
4687
8abee2e1 4688#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4689 do \
4690 { \
4691 s = (struct glyph_string *) alloca (sizeof *s); \
4692 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4693 x_fill_image_glyph_string (s); \
4694 x_append_glyph_string (&HEAD, &TAIL, s); \
4695 ++START; \
4696 s->x = (X); \
4697 } \
4698 while (0)
4699
4700
4701/* Add a glyph string for a sequence of character glyphs to the list
4702 of strings between HEAD and TAIL. START is the index of the first
4703 glyph in row area AREA of glyph row ROW that is part of the new
4704 glyph string. END is the index of the last glyph in that glyph row
4705 area. X is the current output position assigned to the new glyph
4706 string constructed. HL overrides that face of the glyph; e.g. it
4707 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4708 right-most x-position of the drawing area. */
4709
8abee2e1 4710#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4711 do \
4712 { \
3e71d8f2 4713 int c, face_id; \
06a2c219
GM
4714 XChar2b *char2b; \
4715 \
43d120d8 4716 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4717 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4718 \
b4192550
KH
4719 s = (struct glyph_string *) alloca (sizeof *s); \
4720 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4721 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4722 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4723 s->x = (X); \
4724 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4725 OVERLAPS_P); \
06a2c219
GM
4726 } \
4727 while (0)
4728
4729
b4192550
KH
4730/* Add a glyph string for a composite sequence to the list of strings
4731 between HEAD and TAIL. START is the index of the first glyph in
4732 row area AREA of glyph row ROW that is part of the new glyph
4733 string. END is the index of the last glyph in that glyph row area.
4734 X is the current output position assigned to the new glyph string
4735 constructed. HL overrides that face of the glyph; e.g. it is
4736 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4737 x-position of the drawing area. */
4738
6c27ec25 4739#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4740 do { \
43d120d8
KH
4741 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4742 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4743 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4744 struct composition *cmp = composition_table[cmp_id]; \
4745 int glyph_len = cmp->glyph_len; \
4746 XChar2b *char2b; \
4747 struct face **faces; \
4748 struct glyph_string *first_s = NULL; \
4749 int n; \
4750 \
ee569018 4751 base_face = base_face->ascii_face; \
b4192550
KH
4752 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4753 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4754 /* At first, fill in `char2b' and `faces'. */ \
4755 for (n = 0; n < glyph_len; n++) \
4756 { \
43d120d8 4757 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4758 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4759 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4760 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4761 this_face_id, char2b + n, 1); \
b4192550
KH
4762 } \
4763 \
4764 /* Make glyph_strings for each glyph sequence that is drawable by \
4765 the same face, and append them to HEAD/TAIL. */ \
4766 for (n = 0; n < cmp->glyph_len;) \
4767 { \
4768 s = (struct glyph_string *) alloca (sizeof *s); \
4769 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4770 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4771 s->cmp = cmp; \
4772 s->gidx = n; \
b4192550
KH
4773 s->x = (X); \
4774 \
4775 if (n == 0) \
4776 first_s = s; \
4777 \
4778 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4779 } \
4780 \
4781 ++START; \
4782 s = first_s; \
4783 } while (0)
4784
4785
06a2c219
GM
4786/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4787 of AREA of glyph row ROW on window W between indices START and END.
4788 HL overrides the face for drawing glyph strings, e.g. it is
4789 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4790 x-positions of the drawing area.
4791
4792 This is an ugly monster macro construct because we must use alloca
4793 to allocate glyph strings (because x_draw_glyphs can be called
4794 asynchronously). */
4795
8abee2e1 4796#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4797 do \
4798 { \
4799 HEAD = TAIL = NULL; \
4800 while (START < END) \
4801 { \
4802 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4803 switch (first_glyph->type) \
4804 { \
4805 case CHAR_GLYPH: \
4806 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4807 TAIL, HL, X, LAST_X, \
4808 OVERLAPS_P); \
06a2c219
GM
4809 break; \
4810 \
b4192550
KH
4811 case COMPOSITE_GLYPH: \
4812 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4813 HEAD, TAIL, HL, X, LAST_X,\
4814 OVERLAPS_P); \
4815 break; \
4816 \
06a2c219
GM
4817 case STRETCH_GLYPH: \
4818 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4819 HEAD, TAIL, HL, X, LAST_X); \
4820 break; \
4821 \
4822 case IMAGE_GLYPH: \
4823 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4824 TAIL, HL, X, LAST_X); \
4825 break; \
4826 \
4827 default: \
4828 abort (); \
4829 } \
4830 \
4831 x_set_glyph_string_background_width (s, START, LAST_X); \
4832 (X) += s->width; \
4833 } \
4834 } \
4835 while (0)
4836
4837
4838/* Draw glyphs between START and END in AREA of ROW on window W,
4839 starting at x-position X. X is relative to AREA in W. HL is a
4840 face-override with the following meaning:
4841
4842 DRAW_NORMAL_TEXT draw normally
4843 DRAW_CURSOR draw in cursor face
4844 DRAW_MOUSE_FACE draw in mouse face.
4845 DRAW_INVERSE_VIDEO draw in mode line face
4846 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4847 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4848
4849 If REAL_START is non-null, return in *REAL_START the real starting
4850 position for display. This can be different from START in case
4851 overlapping glyphs must be displayed. If REAL_END is non-null,
4852 return in *REAL_END the real end position for display. This can be
4853 different from END in case overlapping glyphs must be displayed.
4854
66ac4b0e
GM
4855 If OVERLAPS_P is non-zero, draw only the foreground of characters
4856 and clip to the physical height of ROW.
4857
06a2c219
GM
4858 Value is the x-position reached, relative to AREA of W. */
4859
4860static int
66ac4b0e
GM
4861x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4862 overlaps_p)
06a2c219
GM
4863 struct window *w;
4864 int x;
4865 struct glyph_row *row;
4866 enum glyph_row_area area;
4867 int start, end;
4868 enum draw_glyphs_face hl;
4869 int *real_start, *real_end;
66ac4b0e 4870 int overlaps_p;
dc6f92b8 4871{
06a2c219
GM
4872 struct glyph_string *head, *tail;
4873 struct glyph_string *s;
4874 int last_x, area_width;
4875 int x_reached;
4876 int i, j;
4877
4878 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 4879 end = min (end, row->used[area]);
a8710abf
GM
4880 start = max (0, start);
4881 start = min (end, start);
06a2c219
GM
4882 if (real_start)
4883 *real_start = start;
4884 if (real_end)
4885 *real_end = end;
4886
4887 /* Translate X to frame coordinates. Set last_x to the right
4888 end of the drawing area. */
4889 if (row->full_width_p)
4890 {
4891 /* X is relative to the left edge of W, without scroll bars
4892 or flag areas. */
4893 struct frame *f = XFRAME (w->frame);
110859fc 4894 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4895 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4896
06a2c219
GM
4897 x += window_left_x;
4898 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4899 last_x = window_left_x + area_width;
4900
4901 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4902 {
110859fc 4903 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4904 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4905 last_x += width;
4906 else
4907 x -= width;
4908 }
dc6f92b8 4909
b9432a85
GM
4910 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4911 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4912 }
4913 else
dc6f92b8 4914 {
06a2c219
GM
4915 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4916 area_width = window_box_width (w, area);
4917 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4918 }
4919
06a2c219
GM
4920 /* Build a doubly-linked list of glyph_string structures between
4921 head and tail from what we have to draw. Note that the macro
4922 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4923 the reason we use a separate variable `i'. */
4924 i = start;
66ac4b0e
GM
4925 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4926 overlaps_p);
06a2c219
GM
4927 if (tail)
4928 x_reached = tail->x + tail->background_width;
4929 else
4930 x_reached = x;
90e65f07 4931
06a2c219
GM
4932 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4933 the row, redraw some glyphs in front or following the glyph
4934 strings built above. */
a8710abf 4935 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4936 {
4937 int dummy_x = 0;
4938 struct glyph_string *h, *t;
4939
4940 /* Compute overhangs for all glyph strings. */
4941 for (s = head; s; s = s->next)
4942 x_compute_glyph_string_overhangs (s);
4943
4944 /* Prepend glyph strings for glyphs in front of the first glyph
4945 string that are overwritten because of the first glyph
4946 string's left overhang. The background of all strings
4947 prepended must be drawn because the first glyph string
4948 draws over it. */
4949 i = x_left_overwritten (head);
4950 if (i >= 0)
4951 {
4952 j = i;
4953 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4954 DRAW_NORMAL_TEXT, dummy_x, last_x,
4955 overlaps_p);
06a2c219
GM
4956 start = i;
4957 if (real_start)
4958 *real_start = start;
4959 x_compute_overhangs_and_x (t, head->x, 1);
4960 x_prepend_glyph_string_lists (&head, &tail, h, t);
4961 }
58769bee 4962
06a2c219
GM
4963 /* Prepend glyph strings for glyphs in front of the first glyph
4964 string that overwrite that glyph string because of their
4965 right overhang. For these strings, only the foreground must
4966 be drawn, because it draws over the glyph string at `head'.
4967 The background must not be drawn because this would overwrite
4968 right overhangs of preceding glyphs for which no glyph
4969 strings exist. */
4970 i = x_left_overwriting (head);
4971 if (i >= 0)
4972 {
4973 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4974 DRAW_NORMAL_TEXT, dummy_x, last_x,
4975 overlaps_p);
06a2c219
GM
4976 for (s = h; s; s = s->next)
4977 s->background_filled_p = 1;
4978 if (real_start)
4979 *real_start = i;
4980 x_compute_overhangs_and_x (t, head->x, 1);
4981 x_prepend_glyph_string_lists (&head, &tail, h, t);
4982 }
dbcb258a 4983
06a2c219
GM
4984 /* Append glyphs strings for glyphs following the last glyph
4985 string tail that are overwritten by tail. The background of
4986 these strings has to be drawn because tail's foreground draws
4987 over it. */
4988 i = x_right_overwritten (tail);
4989 if (i >= 0)
4990 {
4991 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4992 DRAW_NORMAL_TEXT, x, last_x,
4993 overlaps_p);
06a2c219
GM
4994 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4995 x_append_glyph_string_lists (&head, &tail, h, t);
4996 if (real_end)
4997 *real_end = i;
4998 }
dc6f92b8 4999
06a2c219
GM
5000 /* Append glyph strings for glyphs following the last glyph
5001 string tail that overwrite tail. The foreground of such
5002 glyphs has to be drawn because it writes into the background
5003 of tail. The background must not be drawn because it could
5004 paint over the foreground of following glyphs. */
5005 i = x_right_overwriting (tail);
5006 if (i >= 0)
5007 {
5008 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5009 DRAW_NORMAL_TEXT, x, last_x,
5010 overlaps_p);
06a2c219
GM
5011 for (s = h; s; s = s->next)
5012 s->background_filled_p = 1;
5013 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5014 x_append_glyph_string_lists (&head, &tail, h, t);
5015 if (real_end)
5016 *real_end = i;
5017 }
5018 }
58769bee 5019
06a2c219
GM
5020 /* Draw all strings. */
5021 for (s = head; s; s = s->next)
5022 x_draw_glyph_string (s);
dc6f92b8 5023
06a2c219
GM
5024 /* Value is the x-position up to which drawn, relative to AREA of W.
5025 This doesn't include parts drawn because of overhangs. */
5026 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5027 if (!row->full_width_p)
5028 {
5029 if (area > LEFT_MARGIN_AREA)
5030 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5031 if (area > TEXT_AREA)
5032 x_reached -= window_box_width (w, TEXT_AREA);
5033 }
a8710abf 5034
06a2c219
GM
5035 return x_reached;
5036}
dc6f92b8 5037
dc6f92b8 5038
66ac4b0e
GM
5039/* Fix the display of area AREA of overlapping row ROW in window W. */
5040
5041static void
5042x_fix_overlapping_area (w, row, area)
5043 struct window *w;
5044 struct glyph_row *row;
5045 enum glyph_row_area area;
5046{
5047 int i, x;
5048
5049 BLOCK_INPUT;
5050
5051 if (area == LEFT_MARGIN_AREA)
5052 x = 0;
5053 else if (area == TEXT_AREA)
5054 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5055 else
5056 x = (window_box_width (w, LEFT_MARGIN_AREA)
5057 + window_box_width (w, TEXT_AREA));
5058
5059 for (i = 0; i < row->used[area];)
5060 {
5061 if (row->glyphs[area][i].overlaps_vertically_p)
5062 {
5063 int start = i, start_x = x;
5064
5065 do
5066 {
5067 x += row->glyphs[area][i].pixel_width;
5068 ++i;
5069 }
5070 while (i < row->used[area]
5071 && row->glyphs[area][i].overlaps_vertically_p);
5072
5073 x_draw_glyphs (w, start_x, row, area, start, i,
5074 (row->inverse_p
5075 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5076 NULL, NULL, 1);
5077 }
5078 else
5079 {
5080 x += row->glyphs[area][i].pixel_width;
5081 ++i;
5082 }
5083 }
5084
5085 UNBLOCK_INPUT;
5086}
5087
5088
06a2c219
GM
5089/* Output LEN glyphs starting at START at the nominal cursor position.
5090 Advance the nominal cursor over the text. The global variable
5091 updated_window contains the window being updated, updated_row is
5092 the glyph row being updated, and updated_area is the area of that
5093 row being updated. */
dc6f92b8 5094
06a2c219
GM
5095static void
5096x_write_glyphs (start, len)
5097 struct glyph *start;
5098 int len;
5099{
5100 int x, hpos, real_start, real_end;
d9cdbb3d 5101
06a2c219 5102 xassert (updated_window && updated_row);
dc6f92b8 5103 BLOCK_INPUT;
06a2c219
GM
5104
5105 /* Write glyphs. */
dc6f92b8 5106
06a2c219
GM
5107 hpos = start - updated_row->glyphs[updated_area];
5108 x = x_draw_glyphs (updated_window, output_cursor.x,
5109 updated_row, updated_area,
5110 hpos, hpos + len,
5111 (updated_row->inverse_p
5112 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 5113 &real_start, &real_end, 0);
b30ec466 5114
06a2c219
GM
5115 /* If we drew over the cursor, note that it is not visible any more. */
5116 note_overwritten_text_cursor (updated_window, real_start,
5117 real_end - real_start);
dc6f92b8
JB
5118
5119 UNBLOCK_INPUT;
06a2c219
GM
5120
5121 /* Advance the output cursor. */
5122 output_cursor.hpos += len;
5123 output_cursor.x = x;
dc6f92b8
JB
5124}
5125
0cdd0c9f 5126
06a2c219 5127/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5128
06a2c219
GM
5129static void
5130x_insert_glyphs (start, len)
5131 struct glyph *start;
5132 register int len;
5133{
5134 struct frame *f;
5135 struct window *w;
5136 int line_height, shift_by_width, shifted_region_width;
5137 struct glyph_row *row;
5138 struct glyph *glyph;
5139 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 5140
06a2c219 5141 xassert (updated_window && updated_row);
0cdd0c9f 5142 BLOCK_INPUT;
06a2c219
GM
5143 w = updated_window;
5144 f = XFRAME (WINDOW_FRAME (w));
5145
5146 /* Get the height of the line we are in. */
5147 row = updated_row;
5148 line_height = row->height;
5149
5150 /* Get the width of the glyphs to insert. */
5151 shift_by_width = 0;
5152 for (glyph = start; glyph < start + len; ++glyph)
5153 shift_by_width += glyph->pixel_width;
5154
5155 /* Get the width of the region to shift right. */
5156 shifted_region_width = (window_box_width (w, updated_area)
5157 - output_cursor.x
5158 - shift_by_width);
5159
5160 /* Shift right. */
bf0ab8a2 5161 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5162 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5163 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5164 f->output_data.x->normal_gc,
5165 frame_x, frame_y,
5166 shifted_region_width, line_height,
5167 frame_x + shift_by_width, frame_y);
5168
5169 /* Write the glyphs. */
5170 hpos = start - row->glyphs[updated_area];
5171 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 5172 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
5173 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5174
5175 /* Advance the output cursor. */
5176 output_cursor.hpos += len;
5177 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5178 UNBLOCK_INPUT;
5179}
0cdd0c9f 5180
0cdd0c9f 5181
06a2c219
GM
5182/* Delete N glyphs at the nominal cursor position. Not implemented
5183 for X frames. */
c83febd7
RS
5184
5185static void
06a2c219
GM
5186x_delete_glyphs (n)
5187 register int n;
c83febd7 5188{
06a2c219 5189 abort ();
c83febd7
RS
5190}
5191
0cdd0c9f 5192
c5e6e06b
GM
5193/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5194 If they are <= 0, this is probably an error. */
5195
5196void
5197x_clear_area (dpy, window, x, y, width, height, exposures)
5198 Display *dpy;
5199 Window window;
5200 int x, y;
5201 int width, height;
5202 int exposures;
5203{
5204 xassert (width > 0 && height > 0);
5205 XClearArea (dpy, window, x, y, width, height, exposures);
5206}
5207
5208
06a2c219
GM
5209/* Erase the current text line from the nominal cursor position
5210 (inclusive) to pixel column TO_X (exclusive). The idea is that
5211 everything from TO_X onward is already erased.
5212
5213 TO_X is a pixel position relative to updated_area of
5214 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5215
06a2c219
GM
5216static void
5217x_clear_end_of_line (to_x)
5218 int to_x;
5219{
5220 struct frame *f;
5221 struct window *w = updated_window;
5222 int max_x, min_y, max_y;
5223 int from_x, from_y, to_y;
5224
5225 xassert (updated_window && updated_row);
5226 f = XFRAME (w->frame);
5227
5228 if (updated_row->full_width_p)
5229 {
5230 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5231 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5232 && !w->pseudo_window_p)
5233 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5234 }
06a2c219
GM
5235 else
5236 max_x = window_box_width (w, updated_area);
5237 max_y = window_text_bottom_y (w);
dc6f92b8 5238
06a2c219
GM
5239 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5240 of window. For TO_X > 0, truncate to end of drawing area. */
5241 if (to_x == 0)
5242 return;
5243 else if (to_x < 0)
5244 to_x = max_x;
5245 else
5246 to_x = min (to_x, max_x);
dbc4e1c1 5247
06a2c219
GM
5248 to_y = min (max_y, output_cursor.y + updated_row->height);
5249
5250 /* Notice if the cursor will be cleared by this operation. */
5251 if (!updated_row->full_width_p)
5252 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5253
06a2c219
GM
5254 from_x = output_cursor.x;
5255
5256 /* Translate to frame coordinates. */
5257 if (updated_row->full_width_p)
5258 {
5259 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5260 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5261 }
0cdd0c9f
RS
5262 else
5263 {
06a2c219
GM
5264 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5265 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5266 }
5267
045dee35 5268 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5269 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5270 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5271
5272 /* Prevent inadvertently clearing to end of the X window. */
5273 if (to_x > from_x && to_y > from_y)
5274 {
5275 BLOCK_INPUT;
c5e6e06b
GM
5276 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5277 from_x, from_y, to_x - from_x, to_y - from_y,
5278 False);
06a2c219 5279 UNBLOCK_INPUT;
0cdd0c9f 5280 }
0cdd0c9f 5281}
dbc4e1c1 5282
0cdd0c9f 5283
06a2c219 5284/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5285 frame. Otherwise clear the selected frame. */
06a2c219
GM
5286
5287static void
5288x_clear_frame ()
0cdd0c9f 5289{
06a2c219 5290 struct frame *f;
0cdd0c9f 5291
06a2c219
GM
5292 if (updating_frame)
5293 f = updating_frame;
0cdd0c9f 5294 else
b86bd3dd 5295 f = SELECTED_FRAME ();
58769bee 5296
06a2c219
GM
5297 /* Clearing the frame will erase any cursor, so mark them all as no
5298 longer visible. */
5299 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5300 output_cursor.hpos = output_cursor.vpos = 0;
5301 output_cursor.x = -1;
5302
5303 /* We don't set the output cursor here because there will always
5304 follow an explicit cursor_to. */
5305 BLOCK_INPUT;
5306 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5307
5308 /* We have to clear the scroll bars, too. If we have changed
5309 colors or something like that, then they should be notified. */
5310 x_scroll_bar_clear (f);
0cdd0c9f 5311
06a2c219
GM
5312 XFlush (FRAME_X_DISPLAY (f));
5313 UNBLOCK_INPUT;
dc6f92b8 5314}
06a2c219
GM
5315
5316
dc6f92b8 5317\f
dbc4e1c1
JB
5318/* Invert the middle quarter of the frame for .15 sec. */
5319
06a2c219
GM
5320/* We use the select system call to do the waiting, so we have to make
5321 sure it's available. If it isn't, we just won't do visual bells. */
5322
dbc4e1c1
JB
5323#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5324
06a2c219
GM
5325
5326/* Subtract the `struct timeval' values X and Y, storing the result in
5327 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5328
5329static int
5330timeval_subtract (result, x, y)
5331 struct timeval *result, x, y;
5332{
06a2c219
GM
5333 /* Perform the carry for the later subtraction by updating y. This
5334 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5335 if (x.tv_usec < y.tv_usec)
5336 {
5337 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5338 y.tv_usec -= 1000000 * nsec;
5339 y.tv_sec += nsec;
5340 }
06a2c219 5341
dbc4e1c1
JB
5342 if (x.tv_usec - y.tv_usec > 1000000)
5343 {
5344 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5345 y.tv_usec += 1000000 * nsec;
5346 y.tv_sec -= nsec;
5347 }
5348
06a2c219
GM
5349 /* Compute the time remaining to wait. tv_usec is certainly
5350 positive. */
dbc4e1c1
JB
5351 result->tv_sec = x.tv_sec - y.tv_sec;
5352 result->tv_usec = x.tv_usec - y.tv_usec;
5353
06a2c219
GM
5354 /* Return indication of whether the result should be considered
5355 negative. */
dbc4e1c1
JB
5356 return x.tv_sec < y.tv_sec;
5357}
dc6f92b8 5358
dfcf069d 5359void
f676886a
JB
5360XTflash (f)
5361 struct frame *f;
dc6f92b8 5362{
dbc4e1c1 5363 BLOCK_INPUT;
dc6f92b8 5364
dbc4e1c1
JB
5365 {
5366 GC gc;
dc6f92b8 5367
06a2c219
GM
5368 /* Create a GC that will use the GXxor function to flip foreground
5369 pixels into background pixels. */
dbc4e1c1
JB
5370 {
5371 XGCValues values;
dc6f92b8 5372
dbc4e1c1 5373 values.function = GXxor;
7556890b
RS
5374 values.foreground = (f->output_data.x->foreground_pixel
5375 ^ f->output_data.x->background_pixel);
58769bee 5376
334208b7 5377 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5378 GCFunction | GCForeground, &values);
5379 }
dc6f92b8 5380
dbc4e1c1 5381 {
e84e14c3
RS
5382 /* Get the height not including a menu bar widget. */
5383 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5384 /* Height of each line to flash. */
5385 int flash_height = FRAME_LINE_HEIGHT (f);
5386 /* These will be the left and right margins of the rectangles. */
5387 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5388 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5389
5390 int width;
5391
5392 /* Don't flash the area between a scroll bar and the frame
5393 edge it is next to. */
5394 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5395 {
5396 case vertical_scroll_bar_left:
5397 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5398 break;
5399
5400 case vertical_scroll_bar_right:
5401 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5402 break;
06a2c219
GM
5403
5404 default:
5405 break;
e84e14c3
RS
5406 }
5407
5408 width = flash_right - flash_left;
5409
5410 /* If window is tall, flash top and bottom line. */
5411 if (height > 3 * FRAME_LINE_HEIGHT (f))
5412 {
5413 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5414 flash_left,
5415 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5416 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5417 width, flash_height);
5418 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5419 flash_left,
5420 (height - flash_height
5421 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5422 width, flash_height);
5423 }
5424 else
5425 /* If it is short, flash it all. */
5426 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5427 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5428 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5429
06a2c219 5430 x_flush (f);
dc6f92b8 5431
dbc4e1c1 5432 {
06a2c219 5433 struct timeval wakeup;
dc6f92b8 5434
66c30ea1 5435 EMACS_GET_TIME (wakeup);
dc6f92b8 5436
dbc4e1c1
JB
5437 /* Compute time to wait until, propagating carry from usecs. */
5438 wakeup.tv_usec += 150000;
5439 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5440 wakeup.tv_usec %= 1000000;
5441
101922c3
GM
5442 /* Keep waiting until past the time wakeup or any input gets
5443 available. */
5444 while (! detect_input_pending ())
dbc4e1c1 5445 {
101922c3 5446 struct timeval current;
dbc4e1c1
JB
5447 struct timeval timeout;
5448
101922c3 5449 EMACS_GET_TIME (current);
dbc4e1c1 5450
101922c3
GM
5451 /* Break if result would be negative. */
5452 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5453 break;
5454
101922c3
GM
5455 /* How long `select' should wait. */
5456 timeout.tv_sec = 0;
5457 timeout.tv_usec = 10000;
5458
dbc4e1c1 5459 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5460 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5461 }
5462 }
58769bee 5463
e84e14c3
RS
5464 /* If window is tall, flash top and bottom line. */
5465 if (height > 3 * FRAME_LINE_HEIGHT (f))
5466 {
5467 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5468 flash_left,
5469 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5470 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5471 width, flash_height);
5472 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5473 flash_left,
5474 (height - flash_height
5475 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5476 width, flash_height);
5477 }
5478 else
5479 /* If it is short, flash it all. */
5480 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5481 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5482 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5483
334208b7 5484 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5485 x_flush (f);
dc6f92b8 5486 }
dbc4e1c1
JB
5487 }
5488
5489 UNBLOCK_INPUT;
dc6f92b8
JB
5490}
5491
06a2c219 5492#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5493
5494
dc6f92b8
JB
5495/* Make audible bell. */
5496
dfcf069d 5497void
dc6f92b8
JB
5498XTring_bell ()
5499{
b86bd3dd
GM
5500 struct frame *f = SELECTED_FRAME ();
5501
5502 if (FRAME_X_DISPLAY (f))
5503 {
dbc4e1c1 5504#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5505 if (visible_bell)
5506 XTflash (f);
5507 else
dbc4e1c1 5508#endif
b86bd3dd
GM
5509 {
5510 BLOCK_INPUT;
5511 XBell (FRAME_X_DISPLAY (f), 0);
5512 XFlush (FRAME_X_DISPLAY (f));
5513 UNBLOCK_INPUT;
5514 }
dc6f92b8
JB
5515 }
5516}
06a2c219 5517
dc6f92b8 5518\f
06a2c219
GM
5519/* Specify how many text lines, from the top of the window,
5520 should be affected by insert-lines and delete-lines operations.
5521 This, and those operations, are used only within an update
5522 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5523
dfcf069d 5524static void
06a2c219
GM
5525XTset_terminal_window (n)
5526 register int n;
dc6f92b8 5527{
06a2c219 5528 /* This function intentionally left blank. */
dc6f92b8
JB
5529}
5530
06a2c219
GM
5531
5532\f
5533/***********************************************************************
5534 Line Dance
5535 ***********************************************************************/
5536
5537/* Perform an insert-lines or delete-lines operation, inserting N
5538 lines or deleting -N lines at vertical position VPOS. */
5539
dfcf069d 5540static void
06a2c219
GM
5541x_ins_del_lines (vpos, n)
5542 int vpos, n;
dc6f92b8
JB
5543{
5544 abort ();
5545}
06a2c219
GM
5546
5547
5548/* Scroll part of the display as described by RUN. */
dc6f92b8 5549
dfcf069d 5550static void
06a2c219
GM
5551x_scroll_run (w, run)
5552 struct window *w;
5553 struct run *run;
dc6f92b8 5554{
06a2c219
GM
5555 struct frame *f = XFRAME (w->frame);
5556 int x, y, width, height, from_y, to_y, bottom_y;
5557
5558 /* Get frame-relative bounding box of the text display area of W,
5559 without mode lines. Include in this box the flags areas to the
5560 left and right of W. */
5561 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5562 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5563 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5564
5565 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5566 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5567 bottom_y = y + height;
dc6f92b8 5568
06a2c219
GM
5569 if (to_y < from_y)
5570 {
5571 /* Scrolling up. Make sure we don't copy part of the mode
5572 line at the bottom. */
5573 if (from_y + run->height > bottom_y)
5574 height = bottom_y - from_y;
5575 else
5576 height = run->height;
5577 }
dc6f92b8 5578 else
06a2c219
GM
5579 {
5580 /* Scolling down. Make sure we don't copy over the mode line.
5581 at the bottom. */
5582 if (to_y + run->height > bottom_y)
5583 height = bottom_y - to_y;
5584 else
5585 height = run->height;
5586 }
7a13e894 5587
06a2c219
GM
5588 BLOCK_INPUT;
5589
5590 /* Cursor off. Will be switched on again in x_update_window_end. */
5591 updated_window = w;
5592 x_clear_cursor (w);
5593
5594 XCopyArea (FRAME_X_DISPLAY (f),
5595 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5596 f->output_data.x->normal_gc,
5597 x, from_y,
5598 width, height,
5599 x, to_y);
5600
5601 UNBLOCK_INPUT;
5602}
dc6f92b8 5603
dc6f92b8 5604
06a2c219
GM
5605\f
5606/***********************************************************************
5607 Exposure Events
5608 ***********************************************************************/
5609
5610/* Redisplay an exposed area of frame F. X and Y are the upper-left
5611 corner of the exposed rectangle. W and H are width and height of
5612 the exposed area. All are pixel values. W or H zero means redraw
5613 the entire frame. */
dc6f92b8 5614
06a2c219
GM
5615static void
5616expose_frame (f, x, y, w, h)
5617 struct frame *f;
5618 int x, y, w, h;
dc6f92b8 5619{
06a2c219 5620 XRectangle r;
dc6f92b8 5621
06a2c219 5622 TRACE ((stderr, "expose_frame "));
dc6f92b8 5623
06a2c219
GM
5624 /* No need to redraw if frame will be redrawn soon. */
5625 if (FRAME_GARBAGED_P (f))
dc6f92b8 5626 {
06a2c219
GM
5627 TRACE ((stderr, " garbaged\n"));
5628 return;
5629 }
5630
5631 /* If basic faces haven't been realized yet, there is no point in
5632 trying to redraw anything. This can happen when we get an expose
5633 event while Emacs is starting, e.g. by moving another window. */
5634 if (FRAME_FACE_CACHE (f) == NULL
5635 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5636 {
5637 TRACE ((stderr, " no faces\n"));
5638 return;
58769bee 5639 }
06a2c219
GM
5640
5641 if (w == 0 || h == 0)
58769bee 5642 {
06a2c219
GM
5643 r.x = r.y = 0;
5644 r.width = CANON_X_UNIT (f) * f->width;
5645 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5646 }
5647 else
5648 {
06a2c219
GM
5649 r.x = x;
5650 r.y = y;
5651 r.width = w;
5652 r.height = h;
5653 }
5654
5655 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5656 expose_window_tree (XWINDOW (f->root_window), &r);
5657
9ea173e8 5658 if (WINDOWP (f->tool_bar_window))
06a2c219 5659 {
9ea173e8 5660 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5661 XRectangle window_rect;
5662 XRectangle intersection_rect;
5663 int window_x, window_y, window_width, window_height;
5664
5665
5666 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5667 window_rect.x = window_x;
5668 window_rect.y = window_y;
5669 window_rect.width = window_width;
5670 window_rect.height = window_height;
5671
5672 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5673 expose_window (w, &intersection_rect);
5674 }
5675
5676#ifndef USE_X_TOOLKIT
5677 if (WINDOWP (f->menu_bar_window))
5678 {
5679 struct window *w = XWINDOW (f->menu_bar_window);
5680 XRectangle window_rect;
5681 XRectangle intersection_rect;
5682 int window_x, window_y, window_width, window_height;
5683
5684
5685 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5686 window_rect.x = window_x;
5687 window_rect.y = window_y;
5688 window_rect.width = window_width;
5689 window_rect.height = window_height;
5690
5691 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5692 expose_window (w, &intersection_rect);
dc6f92b8 5693 }
06a2c219 5694#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5695}
5696
06a2c219
GM
5697
5698/* Redraw (parts) of all windows in the window tree rooted at W that
5699 intersect R. R contains frame pixel coordinates. */
5700
58769bee 5701static void
06a2c219
GM
5702expose_window_tree (w, r)
5703 struct window *w;
5704 XRectangle *r;
dc6f92b8 5705{
06a2c219
GM
5706 while (w)
5707 {
5708 if (!NILP (w->hchild))
5709 expose_window_tree (XWINDOW (w->hchild), r);
5710 else if (!NILP (w->vchild))
5711 expose_window_tree (XWINDOW (w->vchild), r);
5712 else
5713 {
5714 XRectangle window_rect;
5715 XRectangle intersection_rect;
5716 struct frame *f = XFRAME (w->frame);
5717 int window_x, window_y, window_width, window_height;
5718
5719 /* Frame-relative pixel rectangle of W. */
5720 window_box (w, -1, &window_x, &window_y, &window_width,
5721 &window_height);
5722 window_rect.x
5723 = (window_x
110859fc 5724 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5725 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5726 window_rect.y = window_y;
5727 window_rect.width
5728 = (window_width
110859fc 5729 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5730 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5731 window_rect.height
5732 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5733
5734 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5735 expose_window (w, &intersection_rect);
5736 }
58769bee 5737
06a2c219
GM
5738 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5739 }
5740}
58769bee 5741
dc6f92b8 5742
06a2c219
GM
5743/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5744 which intersects rectangle R. R is in window-relative coordinates. */
5745
5746static void
5747expose_area (w, row, r, area)
5748 struct window *w;
5749 struct glyph_row *row;
5750 XRectangle *r;
5751 enum glyph_row_area area;
5752{
06a2c219
GM
5753 struct glyph *first = row->glyphs[area];
5754 struct glyph *end = row->glyphs[area] + row->used[area];
5755 struct glyph *last;
4bc6dcc7 5756 int first_x, start_x, x;
06a2c219 5757
6fb13182
GM
5758 if (area == TEXT_AREA && row->fill_line_p)
5759 /* If row extends face to end of line write the whole line. */
4bc6dcc7 5760 x_draw_glyphs (w, 0, row, area,
6fb13182 5761 0, row->used[area],
06a2c219 5762 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5763 NULL, NULL, 0);
6fb13182
GM
5764 else
5765 {
4bc6dcc7
GM
5766 /* Set START_X to the window-relative start position for drawing glyphs of
5767 AREA. The first glyph of the text area can be partially visible.
5768 The first glyphs of other areas cannot. */
5769 if (area == LEFT_MARGIN_AREA)
5770 start_x = 0;
5771 else if (area == TEXT_AREA)
5772 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5773 else
5774 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5775 + window_box_width (w, TEXT_AREA));
5776 x = start_x;
5777
6fb13182
GM
5778 /* Find the first glyph that must be redrawn. */
5779 while (first < end
5780 && x + first->pixel_width < r->x)
5781 {
5782 x += first->pixel_width;
5783 ++first;
5784 }
5785
5786 /* Find the last one. */
5787 last = first;
5788 first_x = x;
5789 while (last < end
5790 && x < r->x + r->width)
5791 {
5792 x += last->pixel_width;
5793 ++last;
5794 }
5795
5796 /* Repaint. */
5797 if (last > first)
4bc6dcc7 5798 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5799 first - row->glyphs[area],
5800 last - row->glyphs[area],
5801 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5802 NULL, NULL, 0);
5803 }
06a2c219
GM
5804}
5805
58769bee 5806
06a2c219
GM
5807/* Redraw the parts of the glyph row ROW on window W intersecting
5808 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5809
06a2c219
GM
5810static void
5811expose_line (w, row, r)
5812 struct window *w;
5813 struct glyph_row *row;
5814 XRectangle *r;
5815{
5816 xassert (row->enabled_p);
5817
5818 if (row->mode_line_p || w->pseudo_window_p)
5819 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5820 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5821 NULL, NULL, 0);
06a2c219
GM
5822 else
5823 {
5824 if (row->used[LEFT_MARGIN_AREA])
5825 expose_area (w, row, r, LEFT_MARGIN_AREA);
5826 if (row->used[TEXT_AREA])
5827 expose_area (w, row, r, TEXT_AREA);
5828 if (row->used[RIGHT_MARGIN_AREA])
5829 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5830 x_draw_row_bitmaps (w, row);
5831 }
5832}
dc6f92b8 5833
58769bee 5834
06a2c219
GM
5835/* Return non-zero if W's cursor intersects rectangle R. */
5836
5837static int
5838x_phys_cursor_in_rect_p (w, r)
5839 struct window *w;
5840 XRectangle *r;
5841{
5842 XRectangle cr, result;
5843 struct glyph *cursor_glyph;
5844
5845 cursor_glyph = get_phys_cursor_glyph (w);
5846 if (cursor_glyph)
5847 {
5848 cr.x = w->phys_cursor.x;
5849 cr.y = w->phys_cursor.y;
5850 cr.width = cursor_glyph->pixel_width;
5851 cr.height = w->phys_cursor_height;
5852 return x_intersect_rectangles (&cr, r, &result);
5853 }
5854 else
5855 return 0;
dc6f92b8 5856}
dc6f92b8 5857
06a2c219
GM
5858
5859/* Redraw a rectangle of window W. R is a rectangle in window
5860 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5861
5862static void
06a2c219
GM
5863expose_window (w, r)
5864 struct window *w;
5865 XRectangle *r;
dc6f92b8 5866{
06a2c219
GM
5867 struct glyph_row *row;
5868 int y;
5869 int yb = window_text_bottom_y (w);
5870 int cursor_cleared_p;
dc6f92b8 5871
80c32bcc
GM
5872 /* If window is not yet fully initialized, do nothing. This can
5873 happen when toolkit scroll bars are used and a window is split.
5874 Reconfiguring the scroll bar will generate an expose for a newly
5875 created window. */
c7911417 5876 if (w->current_matrix == NULL || w == updated_window)
80c32bcc
GM
5877 return;
5878
06a2c219
GM
5879 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5880 r->x, r->y, r->width, r->height));
dc6f92b8 5881
06a2c219
GM
5882 /* Convert to window coordinates. */
5883 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5884 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5885
06a2c219
GM
5886 /* Turn off the cursor. */
5887 if (!w->pseudo_window_p
5888 && x_phys_cursor_in_rect_p (w, r))
5889 {
5890 x_clear_cursor (w);
5891 cursor_cleared_p = 1;
5892 }
5893 else
5894 cursor_cleared_p = 0;
5895
5896 /* Find the first row intersecting the rectangle R. */
5897 row = w->current_matrix->rows;
5898 y = 0;
5899 while (row->enabled_p
5900 && y < yb
5901 && y + row->height < r->y)
5902 {
5903 y += row->height;
5904 ++row;
5905 }
5906
dc6f92b8 5907 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5908 while (row->enabled_p
5909 && y < yb
5910 && y < r->y + r->height)
5911 {
5912 expose_line (w, row, r);
5913 y += row->height;
5914 ++row;
5915 }
5916
5917 /* Display the mode line if there is one. */
5918 if (WINDOW_WANTS_MODELINE_P (w)
5919 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5920 row->enabled_p)
5921 && row->y < r->y + r->height)
5922 expose_line (w, row, r);
dc6f92b8 5923
06a2c219 5924 if (!w->pseudo_window_p)
dc6f92b8 5925 {
06a2c219
GM
5926 /* Draw border between windows. */
5927 x_draw_vertical_border (w);
5928
5929 /* Turn the cursor on again. */
5930 if (cursor_cleared_p)
5931 x_update_window_cursor (w, 1);
5932 }
5933}
dc6f92b8 5934
dc6f92b8 5935
06a2c219
GM
5936/* Determine the intersection of two rectangles R1 and R2. Return
5937 the intersection in *RESULT. Value is non-zero if RESULT is not
5938 empty. */
5939
5940static int
5941x_intersect_rectangles (r1, r2, result)
5942 XRectangle *r1, *r2, *result;
5943{
5944 XRectangle *left, *right;
5945 XRectangle *upper, *lower;
5946 int intersection_p = 0;
5947
5948 /* Rearrange so that R1 is the left-most rectangle. */
5949 if (r1->x < r2->x)
5950 left = r1, right = r2;
5951 else
5952 left = r2, right = r1;
5953
5954 /* X0 of the intersection is right.x0, if this is inside R1,
5955 otherwise there is no intersection. */
5956 if (right->x <= left->x + left->width)
5957 {
5958 result->x = right->x;
5959
5960 /* The right end of the intersection is the minimum of the
5961 the right ends of left and right. */
5962 result->width = (min (left->x + left->width, right->x + right->width)
5963 - result->x);
5964
5965 /* Same game for Y. */
5966 if (r1->y < r2->y)
5967 upper = r1, lower = r2;
5968 else
5969 upper = r2, lower = r1;
5970
5971 /* The upper end of the intersection is lower.y0, if this is inside
5972 of upper. Otherwise, there is no intersection. */
5973 if (lower->y <= upper->y + upper->height)
dc43ef94 5974 {
06a2c219
GM
5975 result->y = lower->y;
5976
5977 /* The lower end of the intersection is the minimum of the lower
5978 ends of upper and lower. */
5979 result->height = (min (lower->y + lower->height,
5980 upper->y + upper->height)
5981 - result->y);
5982 intersection_p = 1;
dc43ef94 5983 }
dc6f92b8
JB
5984 }
5985
06a2c219 5986 return intersection_p;
dc6f92b8 5987}
06a2c219
GM
5988
5989
5990
5991
dc6f92b8 5992\f
dc6f92b8 5993static void
334208b7
RS
5994frame_highlight (f)
5995 struct frame *f;
dc6f92b8 5996{
b3e1e05c
JB
5997 /* We used to only do this if Vx_no_window_manager was non-nil, but
5998 the ICCCM (section 4.1.6) says that the window's border pixmap
5999 and border pixel are window attributes which are "private to the
6000 client", so we can always change it to whatever we want. */
6001 BLOCK_INPUT;
334208b7 6002 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6003 f->output_data.x->border_pixel);
b3e1e05c 6004 UNBLOCK_INPUT;
5d46f928 6005 x_update_cursor (f, 1);
dc6f92b8
JB
6006}
6007
6008static void
334208b7
RS
6009frame_unhighlight (f)
6010 struct frame *f;
dc6f92b8 6011{
b3e1e05c
JB
6012 /* We used to only do this if Vx_no_window_manager was non-nil, but
6013 the ICCCM (section 4.1.6) says that the window's border pixmap
6014 and border pixel are window attributes which are "private to the
6015 client", so we can always change it to whatever we want. */
6016 BLOCK_INPUT;
334208b7 6017 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6018 f->output_data.x->border_tile);
b3e1e05c 6019 UNBLOCK_INPUT;
5d46f928 6020 x_update_cursor (f, 1);
dc6f92b8 6021}
dc6f92b8 6022
f676886a
JB
6023/* The focus has changed. Update the frames as necessary to reflect
6024 the new situation. Note that we can't change the selected frame
c5acd733 6025 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6026 Each event gets marked with the frame in which it occurred, so the
c5acd733 6027 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6028
6d4238f3 6029static void
0f941935
KH
6030x_new_focus_frame (dpyinfo, frame)
6031 struct x_display_info *dpyinfo;
f676886a 6032 struct frame *frame;
dc6f92b8 6033{
0f941935 6034 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6035
0f941935 6036 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6037 {
58769bee 6038 /* Set this before calling other routines, so that they see
f676886a 6039 the correct value of x_focus_frame. */
0f941935 6040 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6041
6042 if (old_focus && old_focus->auto_lower)
f676886a 6043 x_lower_frame (old_focus);
dc6f92b8
JB
6044
6045#if 0
f676886a 6046 selected_frame = frame;
e0c1aef2
KH
6047 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6048 selected_frame);
f676886a
JB
6049 Fselect_window (selected_frame->selected_window);
6050 choose_minibuf_frame ();
c118dd06 6051#endif /* ! 0 */
dc6f92b8 6052
0f941935
KH
6053 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6054 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6055 else
6056 pending_autoraise_frame = 0;
6d4238f3 6057 }
dc6f92b8 6058
0f941935 6059 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6060}
6061
37c2c98b
RS
6062/* Handle an event saying the mouse has moved out of an Emacs frame. */
6063
6064void
0f941935
KH
6065x_mouse_leave (dpyinfo)
6066 struct x_display_info *dpyinfo;
37c2c98b 6067{
0f941935 6068 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6069}
6d4238f3 6070
f451eb13
JB
6071/* The focus has changed, or we have redirected a frame's focus to
6072 another frame (this happens when a frame uses a surrogate
06a2c219 6073 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6074
6075 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6076 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6077 the appropriate X display info. */
06a2c219 6078
6d4238f3 6079static void
0f941935
KH
6080XTframe_rehighlight (frame)
6081 struct frame *frame;
6d4238f3 6082{
0f941935
KH
6083 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6084}
6d4238f3 6085
0f941935
KH
6086static void
6087x_frame_rehighlight (dpyinfo)
6088 struct x_display_info *dpyinfo;
6089{
6090 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6091
6092 if (dpyinfo->x_focus_frame)
6d4238f3 6093 {
0f941935
KH
6094 dpyinfo->x_highlight_frame
6095 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6096 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6097 : dpyinfo->x_focus_frame);
6098 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6099 {
0f941935
KH
6100 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6101 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6102 }
dc6f92b8 6103 }
6d4238f3 6104 else
0f941935 6105 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6106
0f941935 6107 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6108 {
6109 if (old_highlight)
f676886a 6110 frame_unhighlight (old_highlight);
0f941935
KH
6111 if (dpyinfo->x_highlight_frame)
6112 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6113 }
dc6f92b8 6114}
06a2c219
GM
6115
6116
dc6f92b8 6117\f
06a2c219 6118/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6119
28430d3c
JB
6120/* Initialize mode_switch_bit and modifier_meaning. */
6121static void
334208b7
RS
6122x_find_modifier_meanings (dpyinfo)
6123 struct x_display_info *dpyinfo;
28430d3c 6124{
f689eb05 6125 int min_code, max_code;
28430d3c
JB
6126 KeySym *syms;
6127 int syms_per_code;
6128 XModifierKeymap *mods;
6129
334208b7
RS
6130 dpyinfo->meta_mod_mask = 0;
6131 dpyinfo->shift_lock_mask = 0;
6132 dpyinfo->alt_mod_mask = 0;
6133 dpyinfo->super_mod_mask = 0;
6134 dpyinfo->hyper_mod_mask = 0;
58769bee 6135
9658a521 6136#ifdef HAVE_X11R4
334208b7 6137 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6138#else
4a60f8c5
RS
6139 min_code = dpyinfo->display->min_keycode;
6140 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6141#endif
6142
334208b7 6143 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6144 min_code, max_code - min_code + 1,
6145 &syms_per_code);
334208b7 6146 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6147
58769bee 6148 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6149 Alt keysyms are on. */
28430d3c 6150 {
06a2c219 6151 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6152
6153 for (row = 3; row < 8; row++)
6154 for (col = 0; col < mods->max_keypermod; col++)
6155 {
0299d313
RS
6156 KeyCode code
6157 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6158
af92970c
KH
6159 /* Zeroes are used for filler. Skip them. */
6160 if (code == 0)
6161 continue;
6162
28430d3c
JB
6163 /* Are any of this keycode's keysyms a meta key? */
6164 {
6165 int code_col;
6166
6167 for (code_col = 0; code_col < syms_per_code; code_col++)
6168 {
f689eb05 6169 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6170
f689eb05 6171 switch (sym)
28430d3c 6172 {
f689eb05
JB
6173 case XK_Meta_L:
6174 case XK_Meta_R:
334208b7 6175 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6176 break;
f689eb05
JB
6177
6178 case XK_Alt_L:
6179 case XK_Alt_R:
334208b7 6180 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6181 break;
6182
6183 case XK_Hyper_L:
6184 case XK_Hyper_R:
334208b7 6185 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6186 break;
6187
6188 case XK_Super_L:
6189 case XK_Super_R:
334208b7 6190 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6191 break;
11edeb03
JB
6192
6193 case XK_Shift_Lock:
6194 /* Ignore this if it's not on the lock modifier. */
6195 if ((1 << row) == LockMask)
334208b7 6196 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6197 break;
28430d3c
JB
6198 }
6199 }
6200 }
6201 }
6202 }
6203
f689eb05 6204 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6205 if (! dpyinfo->meta_mod_mask)
a3c44b14 6206 {
334208b7
RS
6207 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6208 dpyinfo->alt_mod_mask = 0;
a3c44b14 6209 }
f689eb05 6210
148c4b70
RS
6211 /* If some keys are both alt and meta,
6212 make them just meta, not alt. */
334208b7 6213 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6214 {
334208b7 6215 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6216 }
58769bee 6217
28430d3c 6218 XFree ((char *) syms);
f689eb05 6219 XFreeModifiermap (mods);
28430d3c
JB
6220}
6221
dfeccd2d
JB
6222/* Convert between the modifier bits X uses and the modifier bits
6223 Emacs uses. */
06a2c219 6224
7c5283e4 6225static unsigned int
334208b7
RS
6226x_x_to_emacs_modifiers (dpyinfo, state)
6227 struct x_display_info *dpyinfo;
dc6f92b8
JB
6228 unsigned int state;
6229{
334208b7
RS
6230 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6231 | ((state & ControlMask) ? ctrl_modifier : 0)
6232 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6233 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6234 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6235 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6236}
6237
dfeccd2d 6238static unsigned int
334208b7
RS
6239x_emacs_to_x_modifiers (dpyinfo, state)
6240 struct x_display_info *dpyinfo;
dfeccd2d
JB
6241 unsigned int state;
6242{
334208b7
RS
6243 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6244 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6245 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6246 | ((state & shift_modifier) ? ShiftMask : 0)
6247 | ((state & ctrl_modifier) ? ControlMask : 0)
6248 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6249}
d047c4eb
KH
6250
6251/* Convert a keysym to its name. */
6252
6253char *
6254x_get_keysym_name (keysym)
6255 KeySym keysym;
6256{
6257 char *value;
6258
6259 BLOCK_INPUT;
6260 value = XKeysymToString (keysym);
6261 UNBLOCK_INPUT;
6262
6263 return value;
6264}
06a2c219
GM
6265
6266
e4571a43
JB
6267\f
6268/* Mouse clicks and mouse movement. Rah. */
e4571a43 6269
06a2c219
GM
6270/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6271 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6272 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6273 not force the value into range. */
69388238 6274
c8dba240 6275void
69388238 6276pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6277 FRAME_PTR f;
69388238 6278 register int pix_x, pix_y;
e4571a43
JB
6279 register int *x, *y;
6280 XRectangle *bounds;
69388238 6281 int noclip;
e4571a43 6282{
06a2c219 6283 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6284 even for negative values. */
6285 if (pix_x < 0)
7556890b 6286 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6287 if (pix_y < 0)
7556890b 6288 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6289
e4571a43
JB
6290 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6291 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6292
6293 if (bounds)
6294 {
7556890b
RS
6295 bounds->width = FONT_WIDTH (f->output_data.x->font);
6296 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6297 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6298 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6299 }
6300
69388238
RS
6301 if (!noclip)
6302 {
6303 if (pix_x < 0)
6304 pix_x = 0;
3cbd2e0b
RS
6305 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6306 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6307
6308 if (pix_y < 0)
6309 pix_y = 0;
6310 else if (pix_y > f->height)
6311 pix_y = f->height;
6312 }
e4571a43
JB
6313
6314 *x = pix_x;
6315 *y = pix_y;
6316}
6317
06a2c219
GM
6318
6319/* Given HPOS/VPOS in the current matrix of W, return corresponding
6320 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6321 can't tell the positions because W's display is not up to date,
6322 return 0. */
6323
6324int
6325glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6326 struct window *w;
6327 int hpos, vpos;
6328 int *frame_x, *frame_y;
2b5c9e71 6329{
06a2c219
GM
6330 int success_p;
6331
6332 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6333 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6334
6335 if (display_completed)
6336 {
6337 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6338 struct glyph *glyph = row->glyphs[TEXT_AREA];
6339 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6340
6341 *frame_y = row->y;
6342 *frame_x = row->x;
6343 while (glyph < end)
6344 {
6345 *frame_x += glyph->pixel_width;
6346 ++glyph;
6347 }
6348
6349 success_p = 1;
6350 }
6351 else
6352 {
6353 *frame_y = *frame_x = 0;
6354 success_p = 0;
6355 }
6356
6357 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6358 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6359 return success_p;
2b5c9e71
RS
6360}
6361
06a2c219 6362
dc6f92b8
JB
6363/* Prepare a mouse-event in *RESULT for placement in the input queue.
6364
6365 If the event is a button press, then note that we have grabbed
f451eb13 6366 the mouse. */
dc6f92b8
JB
6367
6368static Lisp_Object
f451eb13 6369construct_mouse_click (result, event, f)
dc6f92b8
JB
6370 struct input_event *result;
6371 XButtonEvent *event;
f676886a 6372 struct frame *f;
dc6f92b8 6373{
f451eb13 6374 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6375 otherwise. */
f451eb13 6376 result->kind = mouse_click;
69388238 6377 result->code = event->button - Button1;
1113d9db 6378 result->timestamp = event->time;
334208b7
RS
6379 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6380 event->state)
f689eb05 6381 | (event->type == ButtonRelease
58769bee 6382 ? up_modifier
f689eb05 6383 : down_modifier));
dc6f92b8 6384
06a2c219
GM
6385 XSETINT (result->x, event->x);
6386 XSETINT (result->y, event->y);
6387 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6388 result->arg = Qnil;
06a2c219 6389 return Qnil;
dc6f92b8 6390}
b849c413 6391
69388238 6392\f
90e65f07
JB
6393/* Function to report a mouse movement to the mainstream Emacs code.
6394 The input handler calls this.
6395
6396 We have received a mouse movement event, which is given in *event.
6397 If the mouse is over a different glyph than it was last time, tell
6398 the mainstream emacs code by setting mouse_moved. If not, ask for
6399 another motion event, so we can check again the next time it moves. */
b8009dd1 6400
06a2c219
GM
6401static XMotionEvent last_mouse_motion_event;
6402static Lisp_Object last_mouse_motion_frame;
6403
90e65f07 6404static void
12ba150f 6405note_mouse_movement (frame, event)
f676886a 6406 FRAME_PTR frame;
90e65f07 6407 XMotionEvent *event;
90e65f07 6408{
e5d77022 6409 last_mouse_movement_time = event->time;
06a2c219
GM
6410 last_mouse_motion_event = *event;
6411 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6412
27f338af
RS
6413 if (event->window != FRAME_X_WINDOW (frame))
6414 {
39d8bb4d 6415 frame->mouse_moved = 1;
27f338af 6416 last_mouse_scroll_bar = Qnil;
27f338af 6417 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6418 }
6419
90e65f07 6420 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6421 else if (event->x < last_mouse_glyph.x
6422 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6423 || event->y < last_mouse_glyph.y
6424 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6425 {
39d8bb4d 6426 frame->mouse_moved = 1;
ab648270 6427 last_mouse_scroll_bar = Qnil;
b8009dd1 6428 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6429 }
6430}
6431
bf1c0ba1 6432/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6433
06a2c219
GM
6434 int disable_mouse_highlight;
6435
6436
6437\f
6438/************************************************************************
6439 Mouse Face
6440 ************************************************************************/
6441
6442/* Find the glyph under window-relative coordinates X/Y in window W.
6443 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6444 strings. Return in *HPOS and *VPOS the row and column number of
6445 the glyph found. Return in *AREA the glyph area containing X.
6446 Value is a pointer to the glyph found or null if X/Y is not on
6447 text, or we can't tell because W's current matrix is not up to
6448 date. */
6449
6450static struct glyph *
f9db2310 6451x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
6452 struct window *w;
6453 int x, y;
6454 int *hpos, *vpos, *area;
f9db2310 6455 int buffer_only_p;
06a2c219
GM
6456{
6457 struct glyph *glyph, *end;
3e71d8f2 6458 struct glyph_row *row = NULL;
06a2c219
GM
6459 int x0, i, left_area_width;
6460
6461 /* Find row containing Y. Give up if some row is not enabled. */
6462 for (i = 0; i < w->current_matrix->nrows; ++i)
6463 {
6464 row = MATRIX_ROW (w->current_matrix, i);
6465 if (!row->enabled_p)
6466 return NULL;
6467 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6468 break;
6469 }
6470
6471 *vpos = i;
6472 *hpos = 0;
6473
6474 /* Give up if Y is not in the window. */
6475 if (i == w->current_matrix->nrows)
6476 return NULL;
6477
6478 /* Get the glyph area containing X. */
6479 if (w->pseudo_window_p)
6480 {
6481 *area = TEXT_AREA;
6482 x0 = 0;
6483 }
6484 else
6485 {
6486 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6487 if (x < left_area_width)
6488 {
6489 *area = LEFT_MARGIN_AREA;
6490 x0 = 0;
6491 }
6492 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6493 {
6494 *area = TEXT_AREA;
6495 x0 = row->x + left_area_width;
6496 }
6497 else
6498 {
6499 *area = RIGHT_MARGIN_AREA;
6500 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6501 }
6502 }
6503
6504 /* Find glyph containing X. */
6505 glyph = row->glyphs[*area];
6506 end = glyph + row->used[*area];
6507 while (glyph < end)
6508 {
6509 if (x < x0 + glyph->pixel_width)
6510 {
6511 if (w->pseudo_window_p)
6512 break;
f9db2310 6513 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
6514 break;
6515 }
6516
6517 x0 += glyph->pixel_width;
6518 ++glyph;
6519 }
6520
6521 if (glyph == end)
6522 return NULL;
6523
6524 *hpos = glyph - row->glyphs[*area];
6525 return glyph;
6526}
6527
6528
6529/* Convert frame-relative x/y to coordinates relative to window W.
6530 Takes pseudo-windows into account. */
6531
6532static void
6533frame_to_window_pixel_xy (w, x, y)
6534 struct window *w;
6535 int *x, *y;
6536{
6537 if (w->pseudo_window_p)
6538 {
6539 /* A pseudo-window is always full-width, and starts at the
6540 left edge of the frame, plus a frame border. */
6541 struct frame *f = XFRAME (w->frame);
6542 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6543 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6544 }
6545 else
6546 {
6547 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6548 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6549 }
6550}
6551
6552
e371a781 6553/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6554 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6555 mode line. X is relative to the start of the text display area of
6556 W, so the width of bitmap areas and scroll bars must be subtracted
6557 to get a position relative to the start of the mode line. */
6558
6559static void
6560note_mode_line_highlight (w, x, mode_line_p)
6561 struct window *w;
6562 int x, mode_line_p;
6563{
6564 struct frame *f = XFRAME (w->frame);
6565 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6566 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6567 struct glyph_row *row;
6568
6569 if (mode_line_p)
6570 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6571 else
045dee35 6572 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6573
06a2c219
GM
6574 if (row->enabled_p)
6575 {
6576 struct glyph *glyph, *end;
6577 Lisp_Object help, map;
6578 int x0;
6579
6580 /* Find the glyph under X. */
6581 glyph = row->glyphs[TEXT_AREA];
6582 end = glyph + row->used[TEXT_AREA];
6583 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6584 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
e371a781 6585
06a2c219
GM
6586 while (glyph < end
6587 && x >= x0 + glyph->pixel_width)
6588 {
6589 x0 += glyph->pixel_width;
6590 ++glyph;
6591 }
6592
6593 if (glyph < end
6594 && STRINGP (glyph->object)
6595 && XSTRING (glyph->object)->intervals
6596 && glyph->charpos >= 0
6597 && glyph->charpos < XSTRING (glyph->object)->size)
6598 {
6599 /* If we're on a string with `help-echo' text property,
6600 arrange for the help to be displayed. This is done by
6601 setting the global variable help_echo to the help string. */
6602 help = Fget_text_property (make_number (glyph->charpos),
6603 Qhelp_echo, glyph->object);
b7e80413 6604 if (!NILP (help))
be010514
GM
6605 {
6606 help_echo = help;
7cea38bc 6607 XSETWINDOW (help_echo_window, w);
be010514
GM
6608 help_echo_object = glyph->object;
6609 help_echo_pos = glyph->charpos;
6610 }
06a2c219
GM
6611
6612 /* Change the mouse pointer according to what is under X/Y. */
6613 map = Fget_text_property (make_number (glyph->charpos),
6614 Qlocal_map, glyph->object);
02067692 6615 if (KEYMAPP (map))
06a2c219 6616 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6617 else
6618 {
6619 map = Fget_text_property (make_number (glyph->charpos),
6620 Qkeymap, glyph->object);
02067692 6621 if (KEYMAPP (map))
be010514
GM
6622 cursor = f->output_data.x->nontext_cursor;
6623 }
06a2c219
GM
6624 }
6625 }
6626
6627 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6628}
6629
6630
6631/* Take proper action when the mouse has moved to position X, Y on
6632 frame F as regards highlighting characters that have mouse-face
6633 properties. Also de-highlighting chars where the mouse was before.
27f338af 6634 X and Y can be negative or out of range. */
b8009dd1
RS
6635
6636static void
6637note_mouse_highlight (f, x, y)
06a2c219 6638 struct frame *f;
c32cdd9a 6639 int x, y;
b8009dd1 6640{
06a2c219
GM
6641 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6642 int portion;
b8009dd1
RS
6643 Lisp_Object window;
6644 struct window *w;
6645
06a2c219
GM
6646 /* When a menu is active, don't highlight because this looks odd. */
6647#ifdef USE_X_TOOLKIT
6648 if (popup_activated ())
6649 return;
6650#endif
6651
04fff9c0
GM
6652 if (disable_mouse_highlight
6653 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6654 return;
6655
06a2c219
GM
6656 dpyinfo->mouse_face_mouse_x = x;
6657 dpyinfo->mouse_face_mouse_y = y;
6658 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6659
06a2c219 6660 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6661 return;
6662
514e4681
RS
6663 if (gc_in_progress)
6664 {
06a2c219 6665 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6666 return;
6667 }
6668
b8009dd1 6669 /* Which window is that in? */
06a2c219 6670 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6671
6672 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6673 if (! EQ (window, dpyinfo->mouse_face_window))
6674 clear_mouse_face (dpyinfo);
6675
6676 /* Not on a window -> return. */
6677 if (!WINDOWP (window))
6678 return;
6679
6680 /* Convert to window-relative pixel coordinates. */
6681 w = XWINDOW (window);
6682 frame_to_window_pixel_xy (w, &x, &y);
6683
9ea173e8 6684 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6685 buffer. */
9ea173e8 6686 if (EQ (window, f->tool_bar_window))
06a2c219 6687 {
9ea173e8 6688 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6689 return;
6690 }
6691
6692 if (portion == 1 || portion == 3)
6693 {
6694 /* Mouse is on the mode or top line. */
6695 note_mode_line_highlight (w, x, portion == 1);
6696 return;
6697 }
e371a781
GM
6698 else if (portion == 2)
6699 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6700 f->output_data.x->horizontal_drag_cursor);
06a2c219
GM
6701 else
6702 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6703 f->output_data.x->text_cursor);
b8009dd1 6704
0cdd0c9f
RS
6705 /* Are we in a window whose display is up to date?
6706 And verify the buffer's text has not changed. */
06a2c219
GM
6707 if (/* Within text portion of the window. */
6708 portion == 0
0cdd0c9f 6709 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6710 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6711 && (XFASTINT (w->last_overlay_modified)
6712 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6713 {
06a2c219
GM
6714 int hpos, vpos, pos, i, area;
6715 struct glyph *glyph;
f9db2310 6716 Lisp_Object object;
b8009dd1 6717
06a2c219 6718 /* Find the glyph under X/Y. */
f9db2310 6719 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
6720
6721 /* Clear mouse face if X/Y not over text. */
6722 if (glyph == NULL
6723 || area != TEXT_AREA
6724 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6725 {
06a2c219
GM
6726 clear_mouse_face (dpyinfo);
6727 return;
6728 }
6729
6730 pos = glyph->charpos;
f9db2310
GM
6731 object = glyph->object;
6732 if (!STRINGP (object) && !BUFFERP (object))
6733 return;
06a2c219 6734
06a2c219 6735 {
f9db2310
GM
6736 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6737 Lisp_Object *overlay_vec = NULL;
06a2c219
GM
6738 int len, noverlays;
6739 struct buffer *obuf;
6740 int obegv, ozv;
6741
6742 /* If we get an out-of-range value, return now; avoid an error. */
f9db2310 6743 if (BUFFERP (object) && pos > BUF_Z (XBUFFER (w->buffer)))
06a2c219
GM
6744 return;
6745
6746 /* Make the window's buffer temporarily current for
6747 overlays_at and compute_char_face. */
6748 obuf = current_buffer;
6749 current_buffer = XBUFFER (w->buffer);
6750 obegv = BEGV;
6751 ozv = ZV;
6752 BEGV = BEG;
6753 ZV = Z;
6754
6755 /* Is this char mouse-active or does it have help-echo? */
f9db2310
GM
6756 position = make_number (pos);
6757
6758 if (BUFFERP (object))
06a2c219 6759 {
f9db2310
GM
6760 /* Put all the overlays we want in a vector in overlay_vec.
6761 Store the length in len. If there are more than 10, make
6762 enough space for all, and try again. */
6763 len = 10;
06a2c219 6764 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
f9db2310
GM
6765 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6766 if (noverlays > len)
6767 {
6768 len = noverlays;
6769 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6770 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6771 }
f8349001 6772
f9db2310
GM
6773 /* Sort overlays into increasing priority order. */
6774 noverlays = sort_overlays (overlay_vec, noverlays, w);
6775 }
6776 else
6777 noverlays = 0;
06a2c219
GM
6778
6779 /* Check mouse-face highlighting. */
6780 if (! (EQ (window, dpyinfo->mouse_face_window)
6781 && vpos >= dpyinfo->mouse_face_beg_row
6782 && vpos <= dpyinfo->mouse_face_end_row
6783 && (vpos > dpyinfo->mouse_face_beg_row
6784 || hpos >= dpyinfo->mouse_face_beg_col)
6785 && (vpos < dpyinfo->mouse_face_end_row
6786 || hpos < dpyinfo->mouse_face_end_col
0a61c667
GM
6787 || dpyinfo->mouse_face_past_end))
6788 /* If there exists an overlay with mouse-face overlapping
6789 the one we are currently highlighting, we have to
6790 check if we enter the overlapping overlay, and then
6791 highlight only that. */
6792 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6793 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6794
06a2c219
GM
6795 {
6796 /* Clear the display of the old active region, if any. */
6797 clear_mouse_face (dpyinfo);
6798
f9db2310
GM
6799 /* Find the highest priority overlay that has a mouse-face
6800 property. */
06a2c219 6801 overlay = Qnil;
f9db2310 6802 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
06a2c219
GM
6803 {
6804 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6805 if (!NILP (mouse_face))
f9db2310 6806 overlay = overlay_vec[i];
06a2c219 6807 }
f9db2310
GM
6808 dpyinfo->mouse_face_overlay = overlay;
6809
06a2c219
GM
6810 /* If no overlay applies, get a text property. */
6811 if (NILP (overlay))
f9db2310 6812 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219
GM
6813
6814 /* Handle the overlay case. */
0a61c667 6815 if (!NILP (overlay))
06a2c219
GM
6816 {
6817 /* Find the range of text around this char that
6818 should be active. */
6819 Lisp_Object before, after;
6820 int ignore;
6821
6822 before = Foverlay_start (overlay);
6823 after = Foverlay_end (overlay);
6824 /* Record this as the current active region. */
6825 fast_find_position (w, XFASTINT (before),
6826 &dpyinfo->mouse_face_beg_col,
6827 &dpyinfo->mouse_face_beg_row,
6828 &dpyinfo->mouse_face_beg_x,
6829 &dpyinfo->mouse_face_beg_y);
6830 dpyinfo->mouse_face_past_end
6831 = !fast_find_position (w, XFASTINT (after),
6832 &dpyinfo->mouse_face_end_col,
6833 &dpyinfo->mouse_face_end_row,
6834 &dpyinfo->mouse_face_end_x,
6835 &dpyinfo->mouse_face_end_y);
6836 dpyinfo->mouse_face_window = window;
6837 dpyinfo->mouse_face_face_id
6838 = face_at_buffer_position (w, pos, 0, 0,
6839 &ignore, pos + 1, 1);
6840
6841 /* Display it as active. */
6842 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6843 }
6844 /* Handle the text property case. */
f9db2310 6845 else if (!NILP (mouse_face) && BUFFERP (object))
06a2c219
GM
6846 {
6847 /* Find the range of text around this char that
6848 should be active. */
6849 Lisp_Object before, after, beginning, end;
6850 int ignore;
6851
6852 beginning = Fmarker_position (w->start);
f9db2310
GM
6853 end = make_number (BUF_Z (XBUFFER (object))
6854 - XFASTINT (w->window_end_pos));
06a2c219
GM
6855 before
6856 = Fprevious_single_property_change (make_number (pos + 1),
6857 Qmouse_face,
f9db2310 6858 object, beginning);
06a2c219
GM
6859 after
6860 = Fnext_single_property_change (position, Qmouse_face,
f9db2310
GM
6861 object, end);
6862
06a2c219
GM
6863 /* Record this as the current active region. */
6864 fast_find_position (w, XFASTINT (before),
6865 &dpyinfo->mouse_face_beg_col,
6866 &dpyinfo->mouse_face_beg_row,
6867 &dpyinfo->mouse_face_beg_x,
6868 &dpyinfo->mouse_face_beg_y);
6869 dpyinfo->mouse_face_past_end
6870 = !fast_find_position (w, XFASTINT (after),
6871 &dpyinfo->mouse_face_end_col,
6872 &dpyinfo->mouse_face_end_row,
6873 &dpyinfo->mouse_face_end_x,
6874 &dpyinfo->mouse_face_end_y);
6875 dpyinfo->mouse_face_window = window;
f9db2310
GM
6876
6877 if (BUFFERP (object))
6878 dpyinfo->mouse_face_face_id
6879 = face_at_buffer_position (w, pos, 0, 0,
6880 &ignore, pos + 1, 1);
06a2c219
GM
6881
6882 /* Display it as active. */
6883 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6884 }
f9db2310
GM
6885 else if (!NILP (mouse_face) && STRINGP (object))
6886 {
6887 Lisp_Object b, e;
6888 int ignore;
6889
6890 b = Fprevious_single_property_change (make_number (pos + 1),
6891 Qmouse_face,
6892 object, Qnil);
6893 e = Fnext_single_property_change (position, Qmouse_face,
6894 object, Qnil);
6895 if (NILP (b))
6896 b = make_number (0);
6897 if (NILP (e))
6898 e = make_number (XSTRING (object)->size) - 1;
6899 fast_find_string_pos (w, XINT (b), object,
6900 &dpyinfo->mouse_face_beg_col,
6901 &dpyinfo->mouse_face_beg_row,
6902 &dpyinfo->mouse_face_beg_x,
6903 &dpyinfo->mouse_face_beg_y, 0);
6904 fast_find_string_pos (w, XINT (e), object,
6905 &dpyinfo->mouse_face_end_col,
6906 &dpyinfo->mouse_face_end_row,
6907 &dpyinfo->mouse_face_end_x,
6908 &dpyinfo->mouse_face_end_y, 1);
6909 dpyinfo->mouse_face_past_end = 0;
6910 dpyinfo->mouse_face_window = window;
6911 dpyinfo->mouse_face_face_id
6912 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
6913 glyph->face_id, 1);
6914 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6915 }
06a2c219
GM
6916 }
6917
6918 /* Look for a `help-echo' property. */
6919 {
743934db 6920 Lisp_Object help, overlay;
06a2c219
GM
6921
6922 /* Check overlays first. */
f9b5db02 6923 help = overlay = Qnil;
f8349001 6924 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6925 {
6926 overlay = overlay_vec[i];
6927 help = Foverlay_get (overlay, Qhelp_echo);
6928 }
be010514
GM
6929
6930 if (!NILP (help))
6931 {
6932 help_echo = help;
7cea38bc 6933 help_echo_window = window;
743934db 6934 help_echo_object = overlay;
be010514
GM
6935 help_echo_pos = pos;
6936 }
6937 else
6938 {
7177d86b
GM
6939 Lisp_Object object = glyph->object;
6940 int charpos = glyph->charpos;
6941
be010514 6942 /* Try text properties. */
7177d86b
GM
6943 if (STRINGP (object)
6944 && charpos >= 0
6945 && charpos < XSTRING (object)->size)
6946 {
6947 help = Fget_text_property (make_number (charpos),
6948 Qhelp_echo, object);
6949 if (NILP (help))
6950 {
6951 /* If the string itself doesn't specify a help-echo,
6952 see if the buffer text ``under'' it does. */
6953 struct glyph_row *r
6954 = MATRIX_ROW (w->current_matrix, vpos);
6955 int start = MATRIX_ROW_START_CHARPOS (r);
6956 int pos = string_buffer_position (w, object, start);
6957 if (pos > 0)
6958 {
6959 help = Fget_text_property (make_number (pos),
6960 Qhelp_echo, w->buffer);
6961 if (!NILP (help))
6962 {
6963 charpos = pos;
6964 object = w->buffer;
6965 }
6966 }
6967 }
6968 }
6969 else if (BUFFERP (object)
6970 && charpos >= BEGV
6971 && charpos < ZV)
6972 help = Fget_text_property (make_number (charpos), Qhelp_echo,
6973 object);
06a2c219 6974
be010514
GM
6975 if (!NILP (help))
6976 {
6977 help_echo = help;
7cea38bc 6978 help_echo_window = window;
7177d86b
GM
6979 help_echo_object = object;
6980 help_echo_pos = charpos;
be010514
GM
6981 }
6982 }
06a2c219
GM
6983 }
6984
6985 BEGV = obegv;
6986 ZV = ozv;
6987 current_buffer = obuf;
6988 }
6989 }
6990}
6991
6992static void
6993redo_mouse_highlight ()
6994{
6995 if (!NILP (last_mouse_motion_frame)
6996 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6997 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6998 last_mouse_motion_event.x,
6999 last_mouse_motion_event.y);
7000}
7001
7002
7003\f
7004/***********************************************************************
9ea173e8 7005 Tool-bars
06a2c219
GM
7006 ***********************************************************************/
7007
9ea173e8
GM
7008static int x_tool_bar_item P_ ((struct frame *, int, int,
7009 struct glyph **, int *, int *, int *));
06a2c219 7010
9ea173e8 7011/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
7012 or -1. */
7013
9ea173e8 7014static int last_tool_bar_item;
06a2c219
GM
7015
7016
9ea173e8
GM
7017/* Get information about the tool-bar item at position X/Y on frame F.
7018 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7019 the current matrix of the tool-bar window of F, or NULL if not
7020 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 7021 item in F->tool_bar_items. Value is
06a2c219 7022
9ea173e8 7023 -1 if X/Y is not on a tool-bar item
06a2c219
GM
7024 0 if X/Y is on the same item that was highlighted before.
7025 1 otherwise. */
7026
7027static int
9ea173e8 7028x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
7029 struct frame *f;
7030 int x, y;
7031 struct glyph **glyph;
7032 int *hpos, *vpos, *prop_idx;
7033{
7034 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7035 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7036 int area;
7037
7038 /* Find the glyph under X/Y. */
f9db2310 7039 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
7040 if (*glyph == NULL)
7041 return -1;
7042
9ea173e8 7043 /* Get the start of this tool-bar item's properties in
8daf1204 7044 f->tool_bar_items. */
9ea173e8 7045 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
7046 return -1;
7047
7048 /* Is mouse on the highlighted item? */
9ea173e8 7049 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
7050 && *vpos >= dpyinfo->mouse_face_beg_row
7051 && *vpos <= dpyinfo->mouse_face_end_row
7052 && (*vpos > dpyinfo->mouse_face_beg_row
7053 || *hpos >= dpyinfo->mouse_face_beg_col)
7054 && (*vpos < dpyinfo->mouse_face_end_row
7055 || *hpos < dpyinfo->mouse_face_end_col
7056 || dpyinfo->mouse_face_past_end))
7057 return 0;
7058
7059 return 1;
7060}
7061
7062
9ea173e8 7063/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
7064 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7065 or ButtonRelase. */
7066
7067static void
9ea173e8 7068x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7069 struct frame *f;
7070 XButtonEvent *button_event;
7071{
7072 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7073 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7074 int hpos, vpos, prop_idx;
7075 struct glyph *glyph;
7076 Lisp_Object enabled_p;
7077 int x = button_event->x;
7078 int y = button_event->y;
7079
9ea173e8 7080 /* If not on the highlighted tool-bar item, return. */
06a2c219 7081 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7082 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7083 return;
7084
7085 /* If item is disabled, do nothing. */
8daf1204 7086 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7087 if (NILP (enabled_p))
7088 return;
7089
7090 if (button_event->type == ButtonPress)
7091 {
7092 /* Show item in pressed state. */
7093 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7094 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7095 last_tool_bar_item = prop_idx;
06a2c219
GM
7096 }
7097 else
7098 {
7099 Lisp_Object key, frame;
7100 struct input_event event;
7101
7102 /* Show item in released state. */
7103 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7104 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7105
8daf1204 7106 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7107
7108 XSETFRAME (frame, f);
9ea173e8 7109 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7110 event.frame_or_window = frame;
7111 event.arg = frame;
06a2c219
GM
7112 kbd_buffer_store_event (&event);
7113
9ea173e8 7114 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7115 event.frame_or_window = frame;
7116 event.arg = key;
06a2c219
GM
7117 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7118 button_event->state);
7119 kbd_buffer_store_event (&event);
9ea173e8 7120 last_tool_bar_item = -1;
06a2c219
GM
7121 }
7122}
7123
7124
9ea173e8
GM
7125/* Possibly highlight a tool-bar item on frame F when mouse moves to
7126 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7127 note_mouse_highlight. */
7128
7129static void
9ea173e8 7130note_tool_bar_highlight (f, x, y)
06a2c219
GM
7131 struct frame *f;
7132 int x, y;
7133{
9ea173e8 7134 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7135 struct window *w = XWINDOW (window);
7136 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7137 int hpos, vpos;
7138 struct glyph *glyph;
7139 struct glyph_row *row;
5c187dee 7140 int i;
06a2c219
GM
7141 Lisp_Object enabled_p;
7142 int prop_idx;
7143 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 7144 int mouse_down_p, rc;
06a2c219
GM
7145
7146 /* Function note_mouse_highlight is called with negative x(y
7147 values when mouse moves outside of the frame. */
7148 if (x <= 0 || y <= 0)
7149 {
7150 clear_mouse_face (dpyinfo);
7151 return;
7152 }
7153
9ea173e8 7154 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7155 if (rc < 0)
7156 {
9ea173e8 7157 /* Not on tool-bar item. */
06a2c219
GM
7158 clear_mouse_face (dpyinfo);
7159 return;
7160 }
7161 else if (rc == 0)
9ea173e8 7162 /* On same tool-bar item as before. */
06a2c219 7163 goto set_help_echo;
b8009dd1 7164
06a2c219
GM
7165 clear_mouse_face (dpyinfo);
7166
9ea173e8 7167 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7168 mouse_down_p = (dpyinfo->grabbed
7169 && f == last_mouse_frame
7170 && FRAME_LIVE_P (f));
7171 if (mouse_down_p
9ea173e8 7172 && last_tool_bar_item != prop_idx)
06a2c219
GM
7173 return;
7174
7175 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7176 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7177
9ea173e8 7178 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7179 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7180 if (!NILP (enabled_p))
7181 {
7182 /* Compute the x-position of the glyph. In front and past the
7183 image is a space. We include this is the highlighted area. */
7184 row = MATRIX_ROW (w->current_matrix, vpos);
7185 for (i = x = 0; i < hpos; ++i)
7186 x += row->glyphs[TEXT_AREA][i].pixel_width;
7187
7188 /* Record this as the current active region. */
7189 dpyinfo->mouse_face_beg_col = hpos;
7190 dpyinfo->mouse_face_beg_row = vpos;
7191 dpyinfo->mouse_face_beg_x = x;
7192 dpyinfo->mouse_face_beg_y = row->y;
7193 dpyinfo->mouse_face_past_end = 0;
7194
7195 dpyinfo->mouse_face_end_col = hpos + 1;
7196 dpyinfo->mouse_face_end_row = vpos;
7197 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7198 dpyinfo->mouse_face_end_y = row->y;
7199 dpyinfo->mouse_face_window = window;
9ea173e8 7200 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7201
7202 /* Display it as active. */
7203 show_mouse_face (dpyinfo, draw);
7204 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7205 }
06a2c219
GM
7206
7207 set_help_echo:
7208
9ea173e8 7209 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 7210 XTread_socket does the rest. */
7cea38bc 7211 help_echo_object = help_echo_window = Qnil;
be010514 7212 help_echo_pos = -1;
8daf1204 7213 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7214 if (NILP (help_echo))
8daf1204 7215 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7216}
4d73d038 7217
06a2c219
GM
7218
7219\f
7220/* Find the glyph matrix position of buffer position POS in window W.
7221 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7222 current glyphs must be up to date. If POS is above window start
7223 return (0, 0, 0, 0). If POS is after end of W, return end of
7224 last line in W. */
b8009dd1
RS
7225
7226static int
06a2c219
GM
7227fast_find_position (w, pos, hpos, vpos, x, y)
7228 struct window *w;
b8009dd1 7229 int pos;
06a2c219 7230 int *hpos, *vpos, *x, *y;
b8009dd1 7231{
b8009dd1 7232 int i;
bf1c0ba1 7233 int lastcol;
06a2c219
GM
7234 int maybe_next_line_p = 0;
7235 int line_start_position;
7236 int yb = window_text_bottom_y (w);
f9db2310 7237 struct glyph_row *row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
06a2c219
GM
7238 struct glyph_row *best_row = row;
7239 int row_vpos = 0, best_row_vpos = 0;
7240 int current_x;
7241
7242 while (row->y < yb)
b8009dd1 7243 {
06a2c219
GM
7244 if (row->used[TEXT_AREA])
7245 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7246 else
7247 line_start_position = 0;
7248
7249 if (line_start_position > pos)
b8009dd1 7250 break;
77b68646
RS
7251 /* If the position sought is the end of the buffer,
7252 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7253 else if (line_start_position == pos
7254 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7255 {
06a2c219 7256 maybe_next_line_p = 1;
77b68646
RS
7257 break;
7258 }
06a2c219
GM
7259 else if (line_start_position > 0)
7260 {
7261 best_row = row;
7262 best_row_vpos = row_vpos;
7263 }
4b0bb6f3
GM
7264
7265 if (row->y + row->height >= yb)
7266 break;
06a2c219
GM
7267
7268 ++row;
7269 ++row_vpos;
b8009dd1 7270 }
06a2c219
GM
7271
7272 /* Find the right column within BEST_ROW. */
7273 lastcol = 0;
7274 current_x = best_row->x;
7275 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7276 {
06a2c219
GM
7277 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7278 int charpos;
7279
7280 charpos = glyph->charpos;
7281 if (charpos == pos)
bf1c0ba1 7282 {
06a2c219
GM
7283 *hpos = i;
7284 *vpos = best_row_vpos;
7285 *x = current_x;
7286 *y = best_row->y;
bf1c0ba1
RS
7287 return 1;
7288 }
06a2c219 7289 else if (charpos > pos)
4d73d038 7290 break;
06a2c219
GM
7291 else if (charpos > 0)
7292 lastcol = i;
7293
7294 current_x += glyph->pixel_width;
bf1c0ba1 7295 }
b8009dd1 7296
77b68646
RS
7297 /* If we're looking for the end of the buffer,
7298 and we didn't find it in the line we scanned,
7299 use the start of the following line. */
06a2c219 7300 if (maybe_next_line_p)
77b68646 7301 {
06a2c219
GM
7302 ++best_row;
7303 ++best_row_vpos;
7304 lastcol = 0;
7305 current_x = best_row->x;
77b68646
RS
7306 }
7307
06a2c219
GM
7308 *vpos = best_row_vpos;
7309 *hpos = lastcol + 1;
7310 *x = current_x;
7311 *y = best_row->y;
b8009dd1
RS
7312 return 0;
7313}
7314
06a2c219 7315
f9db2310
GM
7316/* Find the position of the the glyph for position POS in OBJECT in
7317 window W's current matrix, and return in *X/*Y the pixel
7318 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7319
7320 RIGHT_P non-zero means return the position of the right edge of the
7321 glyph, RIGHT_P zero means return the left edge position.
7322
7323 If no glyph for POS exists in the matrix, return the position of
7324 the glyph with the next smaller position that is in the matrix, if
7325 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7326 exists in the matrix, return the position of the glyph with the
7327 next larger position in OBJECT.
7328
7329 Value is non-zero if a glyph was found. */
7330
7331static int
7332fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7333 struct window *w;
7334 int pos;
7335 Lisp_Object object;
7336 int *hpos, *vpos, *x, *y;
7337 int right_p;
7338{
7339 int yb = window_text_bottom_y (w);
7340 struct glyph_row *r;
7341 struct glyph *best_glyph = NULL;
7342 struct glyph_row *best_row = NULL;
7343 int best_x = 0;
7344
7345 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7346 r->enabled_p && r->y < yb;
7347 ++r)
7348 {
7349 struct glyph *g = r->glyphs[TEXT_AREA];
7350 struct glyph *e = g + r->used[TEXT_AREA];
7351 int gx;
7352
7353 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7354 if (EQ (g->object, object))
7355 {
7356 if (g->charpos == pos)
7357 {
7358 best_glyph = g;
7359 best_x = gx;
7360 best_row = r;
7361 goto found;
7362 }
7363 else if (best_glyph == NULL
7364 || ((abs (g->charpos - pos)
7365 < abs (best_glyph->charpos - pos))
7366 && (right_p
7367 ? g->charpos < pos
7368 : g->charpos > pos)))
7369 {
7370 best_glyph = g;
7371 best_x = gx;
7372 best_row = r;
7373 }
7374 }
7375 }
7376
7377 found:
7378
7379 if (best_glyph)
7380 {
7381 *x = best_x;
7382 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7383
7384 if (right_p)
7385 {
7386 *x += best_glyph->pixel_width;
7387 ++*hpos;
7388 }
7389
7390 *y = best_row->y;
7391 *vpos = best_row - w->current_matrix->rows;
7392 }
7393
7394 return best_glyph != NULL;
7395}
7396
7397
b8009dd1
RS
7398/* Display the active region described by mouse_face_*
7399 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7400
7401static void
06a2c219 7402show_mouse_face (dpyinfo, draw)
7a13e894 7403 struct x_display_info *dpyinfo;
06a2c219 7404 enum draw_glyphs_face draw;
b8009dd1 7405{
7a13e894 7406 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7407 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7408 int i;
06a2c219
GM
7409 int cursor_off_p = 0;
7410 struct cursor_pos saved_cursor;
7411
7412 saved_cursor = output_cursor;
7413
7414 /* If window is in the process of being destroyed, don't bother
7415 to do anything. */
7416 if (w->current_matrix == NULL)
7417 goto set_x_cursor;
7418
7419 /* Recognize when we are called to operate on rows that don't exist
7420 anymore. This can happen when a window is split. */
7421 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7422 goto set_x_cursor;
7423
7424 set_output_cursor (&w->phys_cursor);
7425
7426 /* Note that mouse_face_beg_row etc. are window relative. */
7427 for (i = dpyinfo->mouse_face_beg_row;
7428 i <= dpyinfo->mouse_face_end_row;
7429 i++)
7430 {
7431 int start_hpos, end_hpos, start_x;
7432 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7433
7434 /* Don't do anything if row doesn't have valid contents. */
7435 if (!row->enabled_p)
7436 continue;
7437
7438 /* For all but the first row, the highlight starts at column 0. */
7439 if (i == dpyinfo->mouse_face_beg_row)
7440 {
7441 start_hpos = dpyinfo->mouse_face_beg_col;
7442 start_x = dpyinfo->mouse_face_beg_x;
7443 }
7444 else
7445 {
7446 start_hpos = 0;
7447 start_x = 0;
7448 }
7449
7450 if (i == dpyinfo->mouse_face_end_row)
7451 end_hpos = dpyinfo->mouse_face_end_col;
7452 else
7453 end_hpos = row->used[TEXT_AREA];
7454
7455 /* If the cursor's in the text we are about to rewrite, turn the
7456 cursor off. */
7457 if (!w->pseudo_window_p
7458 && i == output_cursor.vpos
7459 && output_cursor.hpos >= start_hpos - 1
7460 && output_cursor.hpos <= end_hpos)
514e4681 7461 {
06a2c219
GM
7462 x_update_window_cursor (w, 0);
7463 cursor_off_p = 1;
514e4681 7464 }
b8009dd1 7465
06a2c219 7466 if (end_hpos > start_hpos)
64f26cf5
GM
7467 {
7468 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7469 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7470 start_hpos, end_hpos, draw, NULL, NULL, 0);
7471 }
b8009dd1
RS
7472 }
7473
514e4681 7474 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7475 if (cursor_off_p)
7476 x_display_cursor (w, 1,
7477 output_cursor.hpos, output_cursor.vpos,
7478 output_cursor.x, output_cursor.y);
2729a2b5 7479
06a2c219 7480 output_cursor = saved_cursor;
fb3b7de5 7481
06a2c219
GM
7482 set_x_cursor:
7483
7484 /* Change the mouse cursor. */
7485 if (draw == DRAW_NORMAL_TEXT)
7486 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7487 f->output_data.x->text_cursor);
7488 else if (draw == DRAW_MOUSE_FACE)
334208b7 7489 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7490 f->output_data.x->cross_cursor);
27ead1d5 7491 else
334208b7 7492 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7493 f->output_data.x->nontext_cursor);
b8009dd1
RS
7494}
7495
7496/* Clear out the mouse-highlighted active region.
06a2c219 7497 Redraw it un-highlighted first. */
b8009dd1 7498
06a2c219 7499void
7a13e894
RS
7500clear_mouse_face (dpyinfo)
7501 struct x_display_info *dpyinfo;
b8009dd1 7502{
607d9f9f
GM
7503#if 0 /* This prevents redrawing tool bar items when changing from one
7504 to another while a tooltip is open, so don't do it. */
685f4368 7505 if (!NILP (tip_frame))
06a2c219 7506 return;
7948d50a 7507#endif
06a2c219 7508
7a13e894 7509 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7510 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7511
7a13e894
RS
7512 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7513 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7514 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7515}
e687d06e 7516
71b8321e
GM
7517
7518/* Clear any mouse-face on window W. This function is part of the
7519 redisplay interface, and is called from try_window_id and similar
7520 functions to ensure the mouse-highlight is off. */
7521
7522static void
7523x_clear_mouse_face (w)
7524 struct window *w;
7525{
7526 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7527 Lisp_Object window;
7528
2e636f9d 7529 BLOCK_INPUT;
71b8321e
GM
7530 XSETWINDOW (window, w);
7531 if (EQ (window, dpyinfo->mouse_face_window))
7532 clear_mouse_face (dpyinfo);
2e636f9d 7533 UNBLOCK_INPUT;
71b8321e
GM
7534}
7535
7536
e687d06e
RS
7537/* Just discard the mouse face information for frame F, if any.
7538 This is used when the size of F is changed. */
7539
dfcf069d 7540void
e687d06e
RS
7541cancel_mouse_face (f)
7542 FRAME_PTR f;
7543{
7544 Lisp_Object window;
7545 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7546
7547 window = dpyinfo->mouse_face_window;
7548 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7549 {
7550 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7551 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7552 dpyinfo->mouse_face_window = Qnil;
7553 }
7554}
b52b65bd 7555
b8009dd1 7556\f
b52b65bd
GM
7557static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7558
7559
7560/* Try to determine frame pixel position and size of the glyph under
7561 frame pixel coordinates X/Y on frame F . Return the position and
7562 size in *RECT. Value is non-zero if we could compute these
7563 values. */
7564
7565static int
7566glyph_rect (f, x, y, rect)
7567 struct frame *f;
7568 int x, y;
7569 XRectangle *rect;
7570{
7571 Lisp_Object window;
7572 int part, found = 0;
7573
7574 window = window_from_coordinates (f, x, y, &part, 0);
7575 if (!NILP (window))
7576 {
7577 struct window *w = XWINDOW (window);
7578 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7579 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7580 int area;
7581
7582 frame_to_window_pixel_xy (w, &x, &y);
7583
7584 for (; !found && r < end && r->enabled_p; ++r)
7585 if (r->y >= y)
7586 {
7587 struct glyph *g = r->glyphs[TEXT_AREA];
7588 struct glyph *end = g + r->used[TEXT_AREA];
7589 int gx;
7590
7591 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7592 if (gx >= x)
7593 {
7594 rect->width = g->pixel_width;
7595 rect->height = r->height;
7596 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7597 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7598 found = 1;
7599 }
7600 }
7601 }
7602
7603 return found;
7604}
7605
12ba150f 7606
90e65f07 7607/* Return the current position of the mouse.
b52b65bd 7608 *FP should be a frame which indicates which display to ask about.
90e65f07 7609
b52b65bd
GM
7610 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7611 and *PART to the frame, window, and scroll bar part that the mouse
7612 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 7613 position on the scroll bar.
12ba150f 7614
b52b65bd
GM
7615 If the mouse movement started elsewhere, set *FP to the frame the
7616 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
7617 the mouse is over.
7618
b52b65bd 7619 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
7620 was at this position.
7621
a135645a
RS
7622 Don't store anything if we don't have a valid set of values to report.
7623
90e65f07 7624 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7625 movement. */
90e65f07
JB
7626
7627static void
1cf412ec 7628XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7629 FRAME_PTR *fp;
1cf412ec 7630 int insist;
12ba150f 7631 Lisp_Object *bar_window;
ab648270 7632 enum scroll_bar_part *part;
90e65f07 7633 Lisp_Object *x, *y;
e5d77022 7634 unsigned long *time;
90e65f07 7635{
a135645a
RS
7636 FRAME_PTR f1;
7637
90e65f07
JB
7638 BLOCK_INPUT;
7639
8bcee03e 7640 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7641 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7642 else
7643 {
12ba150f
JB
7644 Window root;
7645 int root_x, root_y;
90e65f07 7646
12ba150f
JB
7647 Window dummy_window;
7648 int dummy;
7649
39d8bb4d
KH
7650 Lisp_Object frame, tail;
7651
7652 /* Clear the mouse-moved flag for every frame on this display. */
7653 FOR_EACH_FRAME (tail, frame)
7654 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7655 XFRAME (frame)->mouse_moved = 0;
7656
ab648270 7657 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7658
7659 /* Figure out which root window we're on. */
334208b7
RS
7660 XQueryPointer (FRAME_X_DISPLAY (*fp),
7661 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7662
7663 /* The root window which contains the pointer. */
7664 &root,
7665
7666 /* Trash which we can't trust if the pointer is on
7667 a different screen. */
7668 &dummy_window,
7669
7670 /* The position on that root window. */
58769bee 7671 &root_x, &root_y,
12ba150f
JB
7672
7673 /* More trash we can't trust. */
7674 &dummy, &dummy,
7675
7676 /* Modifier keys and pointer buttons, about which
7677 we don't care. */
7678 (unsigned int *) &dummy);
7679
7680 /* Now we have a position on the root; find the innermost window
7681 containing the pointer. */
7682 {
7683 Window win, child;
7684 int win_x, win_y;
06a2c219 7685 int parent_x = 0, parent_y = 0;
e99db5a1 7686 int count;
12ba150f
JB
7687
7688 win = root;
69388238 7689
2d7fc7e8
RS
7690 /* XTranslateCoordinates can get errors if the window
7691 structure is changing at the same time this function
7692 is running. So at least we must not crash from them. */
7693
e99db5a1 7694 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7695
334208b7 7696 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7697 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7698 {
69388238
RS
7699 /* If mouse was grabbed on a frame, give coords for that frame
7700 even if the mouse is now outside it. */
334208b7 7701 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7702
12ba150f 7703 /* From-window, to-window. */
69388238 7704 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7705
7706 /* From-position, to-position. */
7707 root_x, root_y, &win_x, &win_y,
7708
7709 /* Child of win. */
7710 &child);
69388238
RS
7711 f1 = last_mouse_frame;
7712 }
7713 else
7714 {
7715 while (1)
7716 {
334208b7 7717 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7718
69388238
RS
7719 /* From-window, to-window. */
7720 root, win,
12ba150f 7721
69388238
RS
7722 /* From-position, to-position. */
7723 root_x, root_y, &win_x, &win_y,
7724
7725 /* Child of win. */
7726 &child);
7727
9af3143a 7728 if (child == None || child == win)
69388238
RS
7729 break;
7730
7731 win = child;
7732 parent_x = win_x;
7733 parent_y = win_y;
7734 }
12ba150f 7735
69388238
RS
7736 /* Now we know that:
7737 win is the innermost window containing the pointer
7738 (XTC says it has no child containing the pointer),
7739 win_x and win_y are the pointer's position in it
7740 (XTC did this the last time through), and
7741 parent_x and parent_y are the pointer's position in win's parent.
7742 (They are what win_x and win_y were when win was child.
7743 If win is the root window, it has no parent, and
7744 parent_{x,y} are invalid, but that's okay, because we'll
7745 never use them in that case.) */
7746
7747 /* Is win one of our frames? */
19126e11 7748 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7749
7750#ifdef USE_X_TOOLKIT
7751 /* If we end up with the menu bar window, say it's not
7752 on the frame. */
7753 if (f1 != NULL
7754 && f1->output_data.x->menubar_widget
7755 && win == XtWindow (f1->output_data.x->menubar_widget))
7756 f1 = NULL;
7757#endif /* USE_X_TOOLKIT */
69388238 7758 }
58769bee 7759
2d7fc7e8
RS
7760 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7761 f1 = 0;
7762
e99db5a1 7763 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7764
ab648270 7765 /* If not, is it one of our scroll bars? */
a135645a 7766 if (! f1)
12ba150f 7767 {
ab648270 7768 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7769
7770 if (bar)
7771 {
a135645a 7772 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7773 win_x = parent_x;
7774 win_y = parent_y;
7775 }
7776 }
90e65f07 7777
8bcee03e 7778 if (f1 == 0 && insist > 0)
b86bd3dd 7779 f1 = SELECTED_FRAME ();
1cf412ec 7780
a135645a 7781 if (f1)
12ba150f 7782 {
06a2c219
GM
7783 /* Ok, we found a frame. Store all the values.
7784 last_mouse_glyph is a rectangle used to reduce the
7785 generation of mouse events. To not miss any motion
7786 events, we must divide the frame into rectangles of the
7787 size of the smallest character that could be displayed
7788 on it, i.e. into the same rectangles that matrices on
7789 the frame are divided into. */
7790
b52b65bd
GM
7791 int width, height, gx, gy;
7792 XRectangle rect;
7793
7794 if (glyph_rect (f1, win_x, win_y, &rect))
7795 last_mouse_glyph = rect;
7796 else
7797 {
7798 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7799 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7800 gx = win_x;
7801 gy = win_y;
06a2c219 7802
b52b65bd
GM
7803 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7804 round down even for negative values. */
7805 if (gx < 0)
7806 gx -= width - 1;
4f00e84d 7807 if (gy < 0)
b52b65bd
GM
7808 gy -= height - 1;
7809 gx = (gx + width - 1) / width * width;
7810 gy = (gy + height - 1) / height * height;
7811
7812 last_mouse_glyph.width = width;
7813 last_mouse_glyph.height = height;
7814 last_mouse_glyph.x = gx;
7815 last_mouse_glyph.y = gy;
7816 }
12ba150f
JB
7817
7818 *bar_window = Qnil;
7819 *part = 0;
334208b7 7820 *fp = f1;
e0c1aef2
KH
7821 XSETINT (*x, win_x);
7822 XSETINT (*y, win_y);
12ba150f
JB
7823 *time = last_mouse_movement_time;
7824 }
7825 }
7826 }
90e65f07
JB
7827
7828 UNBLOCK_INPUT;
7829}
f451eb13 7830
06a2c219 7831
06a2c219 7832#ifdef USE_X_TOOLKIT
bffcfca9
GM
7833
7834/* Atimer callback function for TIMER. Called every 0.1s to process
7835 Xt timeouts, if needed. We must avoid calling XtAppPending as
7836 much as possible because that function does an implicit XFlush
7837 that slows us down. */
7838
7839static void
7840x_process_timeouts (timer)
7841 struct atimer *timer;
7842{
7843 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7844 {
7845 BLOCK_INPUT;
7846 while (XtAppPending (Xt_app_con) & XtIMTimer)
7847 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7848 UNBLOCK_INPUT;
7849 }
06a2c219
GM
7850}
7851
bffcfca9 7852#endif /* USE_X_TOOLKIT */
06a2c219
GM
7853
7854\f
7855/* Scroll bar support. */
7856
7857/* Given an X window ID, find the struct scroll_bar which manages it.
7858 This can be called in GC, so we have to make sure to strip off mark
7859 bits. */
bffcfca9 7860
06a2c219
GM
7861static struct scroll_bar *
7862x_window_to_scroll_bar (window_id)
7863 Window window_id;
7864{
7865 Lisp_Object tail;
7866
7867 for (tail = Vframe_list;
7868 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7869 tail = XCDR (tail))
06a2c219
GM
7870 {
7871 Lisp_Object frame, bar, condemned;
7872
8e713be6 7873 frame = XCAR (tail);
06a2c219
GM
7874 /* All elements of Vframe_list should be frames. */
7875 if (! GC_FRAMEP (frame))
7876 abort ();
7877
7878 /* Scan this frame's scroll bar list for a scroll bar with the
7879 right window ID. */
7880 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7881 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7882 /* This trick allows us to search both the ordinary and
7883 condemned scroll bar lists with one loop. */
7884 ! GC_NILP (bar) || (bar = condemned,
7885 condemned = Qnil,
7886 ! GC_NILP (bar));
7887 bar = XSCROLL_BAR (bar)->next)
7888 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7889 return XSCROLL_BAR (bar);
7890 }
7891
7892 return 0;
7893}
7894
7895
7896\f
7897/************************************************************************
7898 Toolkit scroll bars
7899 ************************************************************************/
7900
eccc05db 7901#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7902
7903static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7904static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7905static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7906 struct scroll_bar *));
7907static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7908 int, int, int));
7909
7910
7911/* Id of action hook installed for scroll bars. */
7912
7913static XtActionHookId action_hook_id;
7914
7915/* Lisp window being scrolled. Set when starting to interact with
7916 a toolkit scroll bar, reset to nil when ending the interaction. */
7917
7918static Lisp_Object window_being_scrolled;
7919
7920/* Last scroll bar part sent in xm_scroll_callback. */
7921
7922static int last_scroll_bar_part;
7923
ec18280f
SM
7924/* Whether this is an Xaw with arrow-scrollbars. This should imply
7925 that movements of 1/20 of the screen size are mapped to up/down. */
7926
7927static Boolean xaw3d_arrow_scroll;
7928
7929/* Whether the drag scrolling maintains the mouse at the top of the
7930 thumb. If not, resizing the thumb needs to be done more carefully
7931 to avoid jerkyness. */
7932
7933static Boolean xaw3d_pick_top;
7934
06a2c219
GM
7935
7936/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7937 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7938 the user ends an interaction with the scroll bar, and generates
7939 a `end-scroll' scroll_bar_click' event if so. */
7940
7941static void
7942xt_action_hook (widget, client_data, action_name, event, params,
7943 num_params)
7944 Widget widget;
7945 XtPointer client_data;
7946 String action_name;
7947 XEvent *event;
7948 String *params;
7949 Cardinal *num_params;
7950{
7951 int scroll_bar_p;
7952 char *end_action;
7953
7954#ifdef USE_MOTIF
7955 scroll_bar_p = XmIsScrollBar (widget);
7956 end_action = "Release";
ec18280f 7957#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7958 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7959 end_action = "EndScroll";
ec18280f 7960#endif /* USE_MOTIF */
06a2c219 7961
06a2c219
GM
7962 if (scroll_bar_p
7963 && strcmp (action_name, end_action) == 0
7964 && WINDOWP (window_being_scrolled))
7965 {
7966 struct window *w;
7967
7968 x_send_scroll_bar_event (window_being_scrolled,
7969 scroll_bar_end_scroll, 0, 0);
7970 w = XWINDOW (window_being_scrolled);
7971 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7972 window_being_scrolled = Qnil;
7973 last_scroll_bar_part = -1;
bffcfca9
GM
7974
7975 /* Xt timeouts no longer needed. */
7976 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7977 }
7978}
7979
07b3d16e
GM
7980/* A vector of windows used for communication between
7981 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7982
7983static struct window **scroll_bar_windows;
7984static int scroll_bar_windows_size;
7985
06a2c219
GM
7986
7987/* Send a client message with message type Xatom_Scrollbar for a
7988 scroll action to the frame of WINDOW. PART is a value identifying
7989 the part of the scroll bar that was clicked on. PORTION is the
7990 amount to scroll of a whole of WHOLE. */
7991
7992static void
7993x_send_scroll_bar_event (window, part, portion, whole)
7994 Lisp_Object window;
7995 int part, portion, whole;
7996{
7997 XEvent event;
7998 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7999 struct window *w = XWINDOW (window);
8000 struct frame *f = XFRAME (w->frame);
8001 int i;
06a2c219 8002
07b3d16e
GM
8003 BLOCK_INPUT;
8004
06a2c219
GM
8005 /* Construct a ClientMessage event to send to the frame. */
8006 ev->type = ClientMessage;
8007 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8008 ev->display = FRAME_X_DISPLAY (f);
8009 ev->window = FRAME_X_WINDOW (f);
8010 ev->format = 32;
07b3d16e
GM
8011
8012 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8013 not enough to store a pointer or Lisp_Object on a 64 bit system.
8014 So, store the window in scroll_bar_windows and pass the index
8015 into that array in the event. */
8016 for (i = 0; i < scroll_bar_windows_size; ++i)
8017 if (scroll_bar_windows[i] == NULL)
8018 break;
8019
8020 if (i == scroll_bar_windows_size)
8021 {
8022 int new_size = max (10, 2 * scroll_bar_windows_size);
8023 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8024 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8025
8026 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8027 nbytes);
8028 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8029 scroll_bar_windows_size = new_size;
8030 }
8031
8032 scroll_bar_windows[i] = w;
8033 ev->data.l[0] = (long) i;
06a2c219
GM
8034 ev->data.l[1] = (long) part;
8035 ev->data.l[2] = (long) 0;
8036 ev->data.l[3] = (long) portion;
8037 ev->data.l[4] = (long) whole;
8038
bffcfca9
GM
8039 /* Make Xt timeouts work while the scroll bar is active. */
8040 toolkit_scroll_bar_interaction = 1;
8041
06a2c219
GM
8042 /* Setting the event mask to zero means that the message will
8043 be sent to the client that created the window, and if that
8044 window no longer exists, no event will be sent. */
06a2c219
GM
8045 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8046 UNBLOCK_INPUT;
8047}
8048
8049
8050/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8051 in *IEVENT. */
8052
8053static void
8054x_scroll_bar_to_input_event (event, ievent)
8055 XEvent *event;
8056 struct input_event *ievent;
8057{
8058 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
8059 Lisp_Object window;
8060 struct frame *f;
07b3d16e
GM
8061 struct window *w;
8062
8063 w = scroll_bar_windows[ev->data.l[0]];
8064 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 8065
07b3d16e
GM
8066 XSETWINDOW (window, w);
8067 f = XFRAME (w->frame);
06a2c219
GM
8068
8069 ievent->kind = scroll_bar_click;
8070 ievent->frame_or_window = window;
0f8aabe9 8071 ievent->arg = Qnil;
06a2c219
GM
8072 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8073 ievent->part = ev->data.l[1];
8074 ievent->code = ev->data.l[2];
8075 ievent->x = make_number ((int) ev->data.l[3]);
8076 ievent->y = make_number ((int) ev->data.l[4]);
8077 ievent->modifiers = 0;
8078}
8079
8080
8081#ifdef USE_MOTIF
8082
8083/* Minimum and maximum values used for Motif scroll bars. */
8084
8085#define XM_SB_MIN 1
8086#define XM_SB_MAX 10000000
8087#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8088
8089
8090/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8091 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8092 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8093
8094static void
8095xm_scroll_callback (widget, client_data, call_data)
8096 Widget widget;
8097 XtPointer client_data, call_data;
8098{
8099 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8100 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8101 double percent;
8102 int part = -1, whole = 0, portion = 0;
8103
8104 switch (cs->reason)
8105 {
8106 case XmCR_DECREMENT:
8107 bar->dragging = Qnil;
8108 part = scroll_bar_up_arrow;
8109 break;
8110
8111 case XmCR_INCREMENT:
8112 bar->dragging = Qnil;
8113 part = scroll_bar_down_arrow;
8114 break;
8115
8116 case XmCR_PAGE_DECREMENT:
8117 bar->dragging = Qnil;
8118 part = scroll_bar_above_handle;
8119 break;
8120
8121 case XmCR_PAGE_INCREMENT:
8122 bar->dragging = Qnil;
8123 part = scroll_bar_below_handle;
8124 break;
8125
8126 case XmCR_TO_TOP:
8127 bar->dragging = Qnil;
8128 part = scroll_bar_to_top;
8129 break;
8130
8131 case XmCR_TO_BOTTOM:
8132 bar->dragging = Qnil;
8133 part = scroll_bar_to_bottom;
8134 break;
8135
8136 case XmCR_DRAG:
8137 {
8138 int slider_size;
8139 int dragging_down_p = (INTEGERP (bar->dragging)
8140 && XINT (bar->dragging) <= cs->value);
8141
8142 /* Get the slider size. */
8143 BLOCK_INPUT;
8144 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8145 UNBLOCK_INPUT;
8146
8147 /* At the max position of the scroll bar, do a line-wise
23442ae4
GM
8148 movement. Without doing anything, we would be called with
8149 the same cs->value again and again. If we want to make
8150 sure that we can reach the end of the buffer, we have to do
8151 something.
06a2c219
GM
8152
8153 Implementation note: setting bar->dragging always to
8154 cs->value gives a smoother movement at the max position.
8155 Setting it to nil when doing line-wise movement gives
8156 a better slider behavior. */
8157
8158 if (cs->value + slider_size == XM_SB_MAX
8159 || (dragging_down_p
8160 && last_scroll_bar_part == scroll_bar_down_arrow))
8161 {
8162 part = scroll_bar_down_arrow;
8163 bar->dragging = Qnil;
8164 }
8165 else
8166 {
8167 whole = XM_SB_RANGE;
8168 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8169 part = scroll_bar_handle;
8170 bar->dragging = make_number (cs->value);
8171 }
8172 }
8173 break;
8174
8175 case XmCR_VALUE_CHANGED:
8176 break;
8177 };
8178
8179 if (part >= 0)
8180 {
8181 window_being_scrolled = bar->window;
8182 last_scroll_bar_part = part;
8183 x_send_scroll_bar_event (bar->window, part, portion, whole);
8184 }
8185}
8186
8187
ec18280f 8188#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
8189
8190
ec18280f 8191/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8192 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8193 scroll bar struct. CALL_DATA is a pointer to a float saying where
8194 the thumb is. */
8195
8196static void
ec18280f 8197xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8198 Widget widget;
8199 XtPointer client_data, call_data;
8200{
8201 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8202 float top = *(float *) call_data;
8203 float shown;
ec18280f
SM
8204 int whole, portion, height;
8205 int part;
06a2c219
GM
8206
8207 /* Get the size of the thumb, a value between 0 and 1. */
8208 BLOCK_INPUT;
ec18280f 8209 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8210 UNBLOCK_INPUT;
8211
8212 whole = 10000000;
8213 portion = shown < 1 ? top * whole : 0;
06a2c219 8214
ec18280f
SM
8215 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8216 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8217 the bottom, so we force the scrolling whenever we see that we're
8218 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8219 we try to ensure that we always stay two pixels away from the
8220 bottom). */
06a2c219
GM
8221 part = scroll_bar_down_arrow;
8222 else
8223 part = scroll_bar_handle;
8224
8225 window_being_scrolled = bar->window;
8226 bar->dragging = make_number (portion);
8227 last_scroll_bar_part = part;
8228 x_send_scroll_bar_event (bar->window, part, portion, whole);
8229}
8230
8231
ec18280f
SM
8232/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8233 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8234 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8235 the scroll bar. CALL_DATA is an integer specifying the action that
8236 has taken place. It's magnitude is in the range 0..height of the
8237 scroll bar. Negative values mean scroll towards buffer start.
8238 Values < height of scroll bar mean line-wise movement. */
8239
8240static void
ec18280f 8241xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8242 Widget widget;
8243 XtPointer client_data, call_data;
8244{
8245 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8246 int position = (int) call_data;
8247 Dimension height;
8248 int part;
8249
8250 /* Get the height of the scroll bar. */
8251 BLOCK_INPUT;
8252 XtVaGetValues (widget, XtNheight, &height, NULL);
8253 UNBLOCK_INPUT;
8254
ec18280f
SM
8255 if (abs (position) >= height)
8256 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8257
8258 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8259 it maps line-movement to call_data = max(5, height/20). */
8260 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8261 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8262 else
ec18280f 8263 part = scroll_bar_move_ratio;
06a2c219
GM
8264
8265 window_being_scrolled = bar->window;
8266 bar->dragging = Qnil;
8267 last_scroll_bar_part = part;
ec18280f 8268 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8269}
8270
8271
8272#endif /* not USE_MOTIF */
8273
8274
8275/* Create the widget for scroll bar BAR on frame F. Record the widget
8276 and X window of the scroll bar in BAR. */
8277
8278static void
8279x_create_toolkit_scroll_bar (f, bar)
8280 struct frame *f;
8281 struct scroll_bar *bar;
8282{
8283 Window xwindow;
8284 Widget widget;
8285 Arg av[20];
8286 int ac = 0;
8287 char *scroll_bar_name = "verticalScrollBar";
8288 unsigned long pixel;
8289
8290 BLOCK_INPUT;
8291
8292#ifdef USE_MOTIF
06a2c219
GM
8293 /* Set resources. Create the widget. */
8294 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8295 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8296 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8297 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8298 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8299 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8300 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8301
8302 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8303 if (pixel != -1)
8304 {
8305 XtSetArg (av[ac], XmNforeground, pixel);
8306 ++ac;
8307 }
8308
8309 pixel = f->output_data.x->scroll_bar_background_pixel;
8310 if (pixel != -1)
8311 {
8312 XtSetArg (av[ac], XmNbackground, pixel);
8313 ++ac;
8314 }
8315
8316 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8317 scroll_bar_name, av, ac);
8318
8319 /* Add one callback for everything that can happen. */
8320 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8321 (XtPointer) bar);
8322 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8323 (XtPointer) bar);
8324 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8325 (XtPointer) bar);
8326 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8327 (XtPointer) bar);
8328 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8329 (XtPointer) bar);
8330 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8331 (XtPointer) bar);
8332 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8333 (XtPointer) bar);
8334
8335 /* Realize the widget. Only after that is the X window created. */
8336 XtRealizeWidget (widget);
8337
8338 /* Set the cursor to an arrow. I didn't find a resource to do that.
8339 And I'm wondering why it hasn't an arrow cursor by default. */
8340 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8341 f->output_data.x->nontext_cursor);
8342
ec18280f 8343#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8344
8345 /* Set resources. Create the widget. The background of the
8346 Xaw3d scroll bar widget is a little bit light for my taste.
8347 We don't alter it here to let users change it according
8348 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8349 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8350 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8351 /* For smoother scrolling with Xaw3d -sm */
8352 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8353 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8354
8355 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8356 if (pixel != -1)
8357 {
8358 XtSetArg (av[ac], XtNforeground, pixel);
8359 ++ac;
8360 }
8361
8362 pixel = f->output_data.x->scroll_bar_background_pixel;
8363 if (pixel != -1)
8364 {
8365 XtSetArg (av[ac], XtNbackground, pixel);
8366 ++ac;
8367 }
8368
8369 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8370 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8371
8372 {
8373 char *initial = "";
8374 char *val = initial;
8375 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8376 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8377 if (val == initial)
8378 { /* ARROW_SCROLL */
8379 xaw3d_arrow_scroll = True;
8380 /* Isn't that just a personal preference ? -sm */
8381 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8382 }
8383 }
06a2c219
GM
8384
8385 /* Define callbacks. */
ec18280f
SM
8386 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8387 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8388 (XtPointer) bar);
8389
8390 /* Realize the widget. Only after that is the X window created. */
8391 XtRealizeWidget (widget);
8392
ec18280f 8393#endif /* !USE_MOTIF */
06a2c219
GM
8394
8395 /* Install an action hook that let's us detect when the user
8396 finishes interacting with a scroll bar. */
8397 if (action_hook_id == 0)
8398 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8399
8400 /* Remember X window and widget in the scroll bar vector. */
8401 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8402 xwindow = XtWindow (widget);
8403 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8404
8405 UNBLOCK_INPUT;
8406}
8407
8408
8409/* Set the thumb size and position of scroll bar BAR. We are currently
8410 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8411
8412static void
8413x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8414 struct scroll_bar *bar;
8415 int portion, position, whole;
f451eb13 8416{
e83dc917
GM
8417 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8418 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8419 float top, shown;
f451eb13 8420
06a2c219
GM
8421 if (whole == 0)
8422 top = 0, shown = 1;
8423 else
f451eb13 8424 {
06a2c219
GM
8425 top = (float) position / whole;
8426 shown = (float) portion / whole;
8427 }
f451eb13 8428
06a2c219 8429 BLOCK_INPUT;
f451eb13 8430
06a2c219
GM
8431#ifdef USE_MOTIF
8432 {
8433 int size, value;
06a2c219
GM
8434 XmScrollBarWidget sb;
8435
ec18280f 8436 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8437 is the scroll bar's maximum and MIN is the scroll bar's minimum
8438 value. */
8439 size = shown * XM_SB_RANGE;
8440 size = min (size, XM_SB_RANGE);
8441 size = max (size, 1);
8442
8443 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8444 value = top * XM_SB_RANGE;
8445 value = min (value, XM_SB_MAX - size);
8446 value = max (value, XM_SB_MIN);
8447
06a2c219
GM
8448 if (NILP (bar->dragging))
8449 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8450 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8451 /* This has the negative side effect that the slider value is
ec18280f 8452 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8453 page-wise movement. */
8454 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8455 else
8456 {
8457 /* If currently dragging, only update the slider size.
8458 This reduces flicker effects. */
8459 int old_value, old_size, increment, page_increment;
8460
8461 XmScrollBarGetValues (widget, &old_value, &old_size,
8462 &increment, &page_increment);
8463 XmScrollBarSetValues (widget, old_value,
8464 min (size, XM_SB_RANGE - old_value),
8465 0, 0, False);
8466 }
06a2c219 8467 }
ec18280f 8468#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8469 {
ec18280f
SM
8470 float old_top, old_shown;
8471 Dimension height;
8472 XtVaGetValues (widget,
8473 XtNtopOfThumb, &old_top,
8474 XtNshown, &old_shown,
8475 XtNheight, &height,
8476 NULL);
8477
8478 /* Massage the top+shown values. */
8479 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8480 top = max (0, min (1, top));
8481 else
8482 top = old_top;
8483 /* Keep two pixels available for moving the thumb down. */
8484 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8485
8486 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8487 check that your system's configuration file contains a define
8488 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8489 if (top != old_top || shown != old_shown)
eb393530 8490 {
ec18280f 8491 if (NILP (bar->dragging))
eb393530 8492 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8493 else
8494 {
ec18280f
SM
8495#ifdef HAVE_XAW3D
8496 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8497 int scroll_mode = 0;
ec18280f
SM
8498
8499 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8500 if (xaw3d_arrow_scroll)
8501 {
8502 /* Xaw3d stupidly ignores resize requests while dragging
8503 so we have to make it believe it's not in dragging mode. */
8504 scroll_mode = sb->scrollbar.scroll_mode;
8505 if (scroll_mode == 2)
8506 sb->scrollbar.scroll_mode = 0;
8507 }
8508#endif
8509 /* Try to make the scrolling a tad smoother. */
8510 if (!xaw3d_pick_top)
8511 shown = min (shown, old_shown);
8512
8513 XawScrollbarSetThumb (widget, top, shown);
8514
8515#ifdef HAVE_XAW3D
8516 if (xaw3d_arrow_scroll && scroll_mode == 2)
8517 sb->scrollbar.scroll_mode = scroll_mode;
8518#endif
06a2c219 8519 }
06a2c219
GM
8520 }
8521 }
ec18280f 8522#endif /* !USE_MOTIF */
06a2c219
GM
8523
8524 UNBLOCK_INPUT;
f451eb13
JB
8525}
8526
06a2c219
GM
8527#endif /* USE_TOOLKIT_SCROLL_BARS */
8528
8529
8530\f
8531/************************************************************************
8532 Scroll bars, general
8533 ************************************************************************/
8534
8535/* Create a scroll bar and return the scroll bar vector for it. W is
8536 the Emacs window on which to create the scroll bar. TOP, LEFT,
8537 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8538 scroll bar. */
8539
ab648270 8540static struct scroll_bar *
06a2c219
GM
8541x_scroll_bar_create (w, top, left, width, height)
8542 struct window *w;
f451eb13
JB
8543 int top, left, width, height;
8544{
06a2c219 8545 struct frame *f = XFRAME (w->frame);
334208b7
RS
8546 struct scroll_bar *bar
8547 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8548
8549 BLOCK_INPUT;
8550
eccc05db 8551#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8552 x_create_toolkit_scroll_bar (f, bar);
8553#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8554 {
8555 XSetWindowAttributes a;
8556 unsigned long mask;
5c187dee 8557 Window window;
06a2c219
GM
8558
8559 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8560 if (a.background_pixel == -1)
8561 a.background_pixel = f->output_data.x->background_pixel;
8562
12ba150f 8563 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8564 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8565 | ExposureMask);
7a13e894 8566 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8567
dbc4e1c1 8568 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8569
06a2c219
GM
8570 /* Clear the area of W that will serve as a scroll bar. This is
8571 for the case that a window has been split horizontally. In
8572 this case, no clear_frame is generated to reduce flickering. */
c5e6e06b
GM
8573 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8574 left, top, width,
8575 window_box_height (w), False);
06a2c219
GM
8576
8577 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8578 /* Position and size of scroll bar. */
8579 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8580 top,
8581 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8582 height,
8583 /* Border width, depth, class, and visual. */
8584 0,
8585 CopyFromParent,
8586 CopyFromParent,
8587 CopyFromParent,
8588 /* Attributes. */
8589 mask, &a);
8590 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8591 }
06a2c219 8592#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8593
06a2c219 8594 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8595 XSETINT (bar->top, top);
8596 XSETINT (bar->left, left);
8597 XSETINT (bar->width, width);
8598 XSETINT (bar->height, height);
8599 XSETINT (bar->start, 0);
8600 XSETINT (bar->end, 0);
12ba150f 8601 bar->dragging = Qnil;
f451eb13
JB
8602
8603 /* Add bar to its frame's list of scroll bars. */
334208b7 8604 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8605 bar->prev = Qnil;
334208b7 8606 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8607 if (!NILP (bar->next))
e0c1aef2 8608 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8609
06a2c219 8610 /* Map the window/widget. */
eccc05db 8611#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
8612 {
8613 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8614 XtConfigureWidget (scroll_bar,
8615 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8616 top,
8617 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8618 max (height, 1), 0);
8619 XtMapWidget (scroll_bar);
8620 }
06a2c219 8621#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8622 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8623#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8624
8625 UNBLOCK_INPUT;
12ba150f 8626 return bar;
f451eb13
JB
8627}
8628
06a2c219 8629
12ba150f 8630/* Draw BAR's handle in the proper position.
06a2c219 8631
12ba150f
JB
8632 If the handle is already drawn from START to END, don't bother
8633 redrawing it, unless REBUILD is non-zero; in that case, always
8634 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8635 events.)
12ba150f
JB
8636
8637 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8638 fit inside its rectangle, but if the user is dragging the scroll
8639 bar handle, we want to let them drag it down all the way, so that
8640 the bar's top is as far down as it goes; otherwise, there's no way
8641 to move to the very end of the buffer. */
8642
5c187dee
GM
8643#ifndef USE_TOOLKIT_SCROLL_BARS
8644
f451eb13 8645static void
ab648270
JB
8646x_scroll_bar_set_handle (bar, start, end, rebuild)
8647 struct scroll_bar *bar;
f451eb13 8648 int start, end;
12ba150f 8649 int rebuild;
f451eb13 8650{
12ba150f 8651 int dragging = ! NILP (bar->dragging);
ab648270 8652 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8653 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8654 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8655
8656 /* If the display is already accurate, do nothing. */
8657 if (! rebuild
8658 && start == XINT (bar->start)
8659 && end == XINT (bar->end))
8660 return;
8661
f451eb13
JB
8662 BLOCK_INPUT;
8663
8664 {
d9cdbb3d
RS
8665 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8666 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8667 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8668
8669 /* Make sure the values are reasonable, and try to preserve
8670 the distance between start and end. */
12ba150f
JB
8671 {
8672 int length = end - start;
8673
8674 if (start < 0)
8675 start = 0;
8676 else if (start > top_range)
8677 start = top_range;
8678 end = start + length;
8679
8680 if (end < start)
8681 end = start;
8682 else if (end > top_range && ! dragging)
8683 end = top_range;
8684 }
f451eb13 8685
ab648270 8686 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8687 XSETINT (bar->start, start);
8688 XSETINT (bar->end, end);
f451eb13 8689
12ba150f
JB
8690 /* Clip the end position, just for display. */
8691 if (end > top_range)
8692 end = top_range;
f451eb13 8693
ab648270 8694 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8695 below top positions, to make sure the handle is always at least
8696 that many pixels tall. */
ab648270 8697 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8698
12ba150f
JB
8699 /* Draw the empty space above the handle. Note that we can't clear
8700 zero-height areas; that means "clear to end of window." */
8701 if (0 < start)
c5e6e06b
GM
8702 x_clear_area (FRAME_X_DISPLAY (f), w,
8703 /* x, y, width, height, and exposures. */
8704 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8705 VERTICAL_SCROLL_BAR_TOP_BORDER,
8706 inside_width, start,
8707 False);
f451eb13 8708
06a2c219
GM
8709 /* Change to proper foreground color if one is specified. */
8710 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8711 XSetForeground (FRAME_X_DISPLAY (f), gc,
8712 f->output_data.x->scroll_bar_foreground_pixel);
8713
12ba150f 8714 /* Draw the handle itself. */
334208b7 8715 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8716
12ba150f 8717 /* x, y, width, height */
ab648270
JB
8718 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8719 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8720 inside_width, end - start);
f451eb13 8721
06a2c219
GM
8722 /* Restore the foreground color of the GC if we changed it above. */
8723 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8724 XSetForeground (FRAME_X_DISPLAY (f), gc,
8725 f->output_data.x->foreground_pixel);
f451eb13 8726
12ba150f
JB
8727 /* Draw the empty space below the handle. Note that we can't
8728 clear zero-height areas; that means "clear to end of window." */
8729 if (end < inside_height)
c5e6e06b
GM
8730 x_clear_area (FRAME_X_DISPLAY (f), w,
8731 /* x, y, width, height, and exposures. */
8732 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8733 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8734 inside_width, inside_height - end,
8735 False);
f451eb13 8736
f451eb13
JB
8737 }
8738
f451eb13
JB
8739 UNBLOCK_INPUT;
8740}
8741
5c187dee 8742#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8743
06a2c219
GM
8744/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8745 nil. */
58769bee 8746
12ba150f 8747static void
ab648270
JB
8748x_scroll_bar_remove (bar)
8749 struct scroll_bar *bar;
12ba150f 8750{
e83dc917 8751 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8752 BLOCK_INPUT;
8753
eccc05db 8754#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8755 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8756#else
8757 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8758#endif
06a2c219 8759
ab648270
JB
8760 /* Disassociate this scroll bar from its window. */
8761 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8762
8763 UNBLOCK_INPUT;
8764}
8765
06a2c219 8766
12ba150f
JB
8767/* Set the handle of the vertical scroll bar for WINDOW to indicate
8768 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8769 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8770 create one. */
06a2c219 8771
12ba150f 8772static void
06a2c219
GM
8773XTset_vertical_scroll_bar (w, portion, whole, position)
8774 struct window *w;
f451eb13
JB
8775 int portion, whole, position;
8776{
06a2c219 8777 struct frame *f = XFRAME (w->frame);
ab648270 8778 struct scroll_bar *bar;
3c6ede7b 8779 int top, height, left, sb_left, width, sb_width;
06a2c219 8780 int window_x, window_y, window_width, window_height;
06a2c219 8781
3c6ede7b 8782 /* Get window dimensions. */
06a2c219 8783 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8784 top = window_y;
8785 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8786 height = window_height;
06a2c219 8787
3c6ede7b 8788 /* Compute the left edge of the scroll bar area. */
06a2c219 8789 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8790 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8791 else
8792 left = XFASTINT (w->left);
8793 left *= CANON_X_UNIT (f);
8794 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8795
8796 /* Compute the width of the scroll bar which might be less than
8797 the width of the area reserved for the scroll bar. */
8798 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8799 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8800 else
3c6ede7b 8801 sb_width = width;
12ba150f 8802
3c6ede7b
GM
8803 /* Compute the left edge of the scroll bar. */
8804#ifdef USE_TOOLKIT_SCROLL_BARS
8805 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8806 sb_left = left + width - sb_width - (width - sb_width) / 2;
8807 else
8808 sb_left = left + (width - sb_width) / 2;
8809#else
8810 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8811 sb_left = left + width - sb_width;
8812 else
8813 sb_left = left;
8814#endif
8815
ab648270 8816 /* Does the scroll bar exist yet? */
06a2c219 8817 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8818 {
80c32bcc 8819 BLOCK_INPUT;
f964b4d7
GM
8820 if (width && height)
8821 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8822 left, top, width, height, False);
80c32bcc 8823 UNBLOCK_INPUT;
3c6ede7b
GM
8824 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8825 }
f451eb13 8826 else
12ba150f
JB
8827 {
8828 /* It may just need to be moved and resized. */
06a2c219
GM
8829 unsigned int mask = 0;
8830
8831 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8832
8833 BLOCK_INPUT;
8834
3c6ede7b 8835 if (sb_left != XINT (bar->left))
06a2c219 8836 mask |= CWX;
3c6ede7b 8837 if (top != XINT (bar->top))
06a2c219 8838 mask |= CWY;
3c6ede7b 8839 if (sb_width != XINT (bar->width))
06a2c219 8840 mask |= CWWidth;
3c6ede7b 8841 if (height != XINT (bar->height))
06a2c219
GM
8842 mask |= CWHeight;
8843
8844#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8845
8846 /* Since toolkit scroll bars are smaller than the space reserved
8847 for them on the frame, we have to clear "under" them. */
f964b4d7
GM
8848 if (width && height)
8849 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8850 left, top, width, height, False);
06a2c219
GM
8851
8852 /* Move/size the scroll bar widget. */
8853 if (mask)
e83dc917 8854 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8855 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8856 top,
8857 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 8858 max (height, 1), 0);
06a2c219
GM
8859
8860#else /* not USE_TOOLKIT_SCROLL_BARS */
8861
e1f6572f
RS
8862 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8863 {
8864 /* Clear areas not covered by the scroll bar. This makes sure a
8865 previous mode line display is cleared after C-x 2 C-x 1, for
8866 example. Non-toolkit scroll bars are as wide as the area
8867 reserved for scroll bars - trim at both sides. */
c5e6e06b
GM
8868 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8869 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8870 height, False);
8871 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8872 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8873 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8874 height, False);
e1f6572f 8875 }
06a2c219
GM
8876
8877 /* Move/size the scroll bar window. */
8878 if (mask)
8879 {
8880 XWindowChanges wc;
8881
3c6ede7b
GM
8882 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8883 wc.y = top;
8884 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8885 wc.height = height;
06a2c219
GM
8886 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8887 mask, &wc);
8888 }
8889
8890#endif /* not USE_TOOLKIT_SCROLL_BARS */
8891
8892 /* Remember new settings. */
3c6ede7b
GM
8893 XSETINT (bar->left, sb_left);
8894 XSETINT (bar->top, top);
8895 XSETINT (bar->width, sb_width);
8896 XSETINT (bar->height, height);
06a2c219
GM
8897
8898 UNBLOCK_INPUT;
12ba150f 8899 }
f451eb13 8900
eccc05db 8901#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8902 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8903#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8904 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8905 dragged. */
12ba150f 8906 if (NILP (bar->dragging))
f451eb13 8907 {
92857db0 8908 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8909
12ba150f 8910 if (whole == 0)
ab648270 8911 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8912 else
8913 {
43f868f5
JB
8914 int start = ((double) position * top_range) / whole;
8915 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8916 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8917 }
f451eb13 8918 }
06a2c219 8919#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8920
06a2c219 8921 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8922}
8923
12ba150f 8924
f451eb13 8925/* The following three hooks are used when we're doing a thorough
ab648270 8926 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8927 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8928 away is a real pain - "Can you say set-window-configuration, boys
8929 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8930 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8931 from the fiery pit when we actually redisplay its window. */
f451eb13 8932
ab648270
JB
8933/* Arrange for all scroll bars on FRAME to be removed at the next call
8934 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8935 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8936
58769bee 8937static void
ab648270 8938XTcondemn_scroll_bars (frame)
f451eb13
JB
8939 FRAME_PTR frame;
8940{
f9e24cb9
RS
8941 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8942 while (! NILP (FRAME_SCROLL_BARS (frame)))
8943 {
8944 Lisp_Object bar;
8945 bar = FRAME_SCROLL_BARS (frame);
8946 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8947 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8948 XSCROLL_BAR (bar)->prev = Qnil;
8949 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8950 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8951 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8952 }
f451eb13
JB
8953}
8954
fa2dfc30 8955
06a2c219 8956/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8957 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8958
f451eb13 8959static void
ab648270 8960XTredeem_scroll_bar (window)
12ba150f 8961 struct window *window;
f451eb13 8962{
ab648270 8963 struct scroll_bar *bar;
fa2dfc30 8964 struct frame *f;
12ba150f 8965
ab648270
JB
8966 /* We can't redeem this window's scroll bar if it doesn't have one. */
8967 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8968 abort ();
8969
ab648270 8970 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8971
8972 /* Unlink it from the condemned list. */
fa2dfc30
GM
8973 f = XFRAME (WINDOW_FRAME (window));
8974 if (NILP (bar->prev))
8975 {
8976 /* If the prev pointer is nil, it must be the first in one of
8977 the lists. */
8978 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8979 /* It's not condemned. Everything's fine. */
8980 return;
8981 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8982 window->vertical_scroll_bar))
8983 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8984 else
8985 /* If its prev pointer is nil, it must be at the front of
8986 one or the other! */
8987 abort ();
8988 }
8989 else
8990 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8991
fa2dfc30
GM
8992 if (! NILP (bar->next))
8993 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8994
fa2dfc30
GM
8995 bar->next = FRAME_SCROLL_BARS (f);
8996 bar->prev = Qnil;
8997 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8998 if (! NILP (bar->next))
8999 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
9000}
9001
ab648270
JB
9002/* Remove all scroll bars on FRAME that haven't been saved since the
9003 last call to `*condemn_scroll_bars_hook'. */
06a2c219 9004
f451eb13 9005static void
ab648270 9006XTjudge_scroll_bars (f)
12ba150f 9007 FRAME_PTR f;
f451eb13 9008{
12ba150f 9009 Lisp_Object bar, next;
f451eb13 9010
ab648270 9011 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
9012
9013 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
9014 more events on the hapless scroll bars. */
9015 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
9016
9017 for (; ! NILP (bar); bar = next)
f451eb13 9018 {
ab648270 9019 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 9020
ab648270 9021 x_scroll_bar_remove (b);
12ba150f
JB
9022
9023 next = b->next;
9024 b->next = b->prev = Qnil;
f451eb13 9025 }
12ba150f 9026
ab648270 9027 /* Now there should be no references to the condemned scroll bars,
12ba150f 9028 and they should get garbage-collected. */
f451eb13
JB
9029}
9030
9031
06a2c219
GM
9032/* Handle an Expose or GraphicsExpose event on a scroll bar. This
9033 is a no-op when using toolkit scroll bars.
ab648270
JB
9034
9035 This may be called from a signal handler, so we have to ignore GC
9036 mark bits. */
06a2c219 9037
f451eb13 9038static void
ab648270
JB
9039x_scroll_bar_expose (bar, event)
9040 struct scroll_bar *bar;
f451eb13
JB
9041 XEvent *event;
9042{
06a2c219
GM
9043#ifndef USE_TOOLKIT_SCROLL_BARS
9044
ab648270 9045 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 9046 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 9047 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 9048 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 9049
f451eb13
JB
9050 BLOCK_INPUT;
9051
ab648270 9052 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 9053
06a2c219 9054 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 9055 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
9056
9057 /* x, y, width, height */
d9cdbb3d 9058 0, 0,
3cbd2e0b 9059 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
9060 XINT (bar->height) - 1);
9061
f451eb13 9062 UNBLOCK_INPUT;
06a2c219
GM
9063
9064#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
9065}
9066
ab648270
JB
9067/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9068 is set to something other than no_event, it is enqueued.
9069
9070 This may be called from a signal handler, so we have to ignore GC
9071 mark bits. */
06a2c219 9072
5c187dee
GM
9073#ifndef USE_TOOLKIT_SCROLL_BARS
9074
f451eb13 9075static void
ab648270
JB
9076x_scroll_bar_handle_click (bar, event, emacs_event)
9077 struct scroll_bar *bar;
f451eb13
JB
9078 XEvent *event;
9079 struct input_event *emacs_event;
9080{
0299d313 9081 if (! GC_WINDOWP (bar->window))
12ba150f
JB
9082 abort ();
9083
ab648270 9084 emacs_event->kind = scroll_bar_click;
69388238 9085 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
9086 emacs_event->modifiers
9087 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9088 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9089 event->xbutton.state)
9090 | (event->type == ButtonRelease
9091 ? up_modifier
9092 : down_modifier));
12ba150f 9093 emacs_event->frame_or_window = bar->window;
0f8aabe9 9094 emacs_event->arg = Qnil;
f451eb13 9095 emacs_event->timestamp = event->xbutton.time;
12ba150f 9096 {
06a2c219 9097#if 0
d9cdbb3d 9098 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 9099 int internal_height
d9cdbb3d 9100 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9101#endif
0299d313 9102 int top_range
d9cdbb3d 9103 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 9104 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
9105
9106 if (y < 0) y = 0;
9107 if (y > top_range) y = top_range;
9108
9109 if (y < XINT (bar->start))
ab648270
JB
9110 emacs_event->part = scroll_bar_above_handle;
9111 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9112 emacs_event->part = scroll_bar_handle;
12ba150f 9113 else
ab648270 9114 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
9115
9116 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
9117 they want to drag it. Lisp code needs to be able to decide
9118 whether or not we're dragging. */
929787e1 9119#if 0
12ba150f
JB
9120 /* If the user has just clicked on the handle, record where they're
9121 holding it. */
9122 if (event->type == ButtonPress
ab648270 9123 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9124 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9125#endif
12ba150f
JB
9126
9127 /* If the user has released the handle, set it to its final position. */
9128 if (event->type == ButtonRelease
9129 && ! NILP (bar->dragging))
9130 {
9131 int new_start = y - XINT (bar->dragging);
9132 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9133
ab648270 9134 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9135 bar->dragging = Qnil;
9136 }
f451eb13 9137
5116f055
JB
9138 /* Same deal here as the other #if 0. */
9139#if 0
58769bee 9140 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9141 the handle. */
ab648270 9142 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9143 emacs_event->x = bar->start;
9144 else
e0c1aef2 9145 XSETINT (emacs_event->x, y);
5116f055 9146#else
e0c1aef2 9147 XSETINT (emacs_event->x, y);
5116f055 9148#endif
f451eb13 9149
e0c1aef2 9150 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9151 }
9152}
f451eb13 9153
ab648270
JB
9154/* Handle some mouse motion while someone is dragging the scroll bar.
9155
9156 This may be called from a signal handler, so we have to ignore GC
9157 mark bits. */
06a2c219 9158
f451eb13 9159static void
ab648270
JB
9160x_scroll_bar_note_movement (bar, event)
9161 struct scroll_bar *bar;
f451eb13
JB
9162 XEvent *event;
9163{
39d8bb4d
KH
9164 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9165
f451eb13
JB
9166 last_mouse_movement_time = event->xmotion.time;
9167
39d8bb4d 9168 f->mouse_moved = 1;
e0c1aef2 9169 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9170
9171 /* If we're dragging the bar, display it. */
ab648270 9172 if (! GC_NILP (bar->dragging))
f451eb13
JB
9173 {
9174 /* Where should the handle be now? */
12ba150f 9175 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9176
12ba150f 9177 if (new_start != XINT (bar->start))
f451eb13 9178 {
12ba150f 9179 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9180
ab648270 9181 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9182 }
9183 }
f451eb13
JB
9184}
9185
5c187dee
GM
9186#endif /* !USE_TOOLKIT_SCROLL_BARS */
9187
12ba150f 9188/* Return information to the user about the current position of the mouse
ab648270 9189 on the scroll bar. */
06a2c219 9190
12ba150f 9191static void
334208b7
RS
9192x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9193 FRAME_PTR *fp;
12ba150f 9194 Lisp_Object *bar_window;
ab648270 9195 enum scroll_bar_part *part;
12ba150f
JB
9196 Lisp_Object *x, *y;
9197 unsigned long *time;
9198{
ab648270 9199 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9200 Window w = SCROLL_BAR_X_WINDOW (bar);
9201 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9202 int win_x, win_y;
559cb2fb
JB
9203 Window dummy_window;
9204 int dummy_coord;
9205 unsigned int dummy_mask;
12ba150f 9206
cf7cb199
JB
9207 BLOCK_INPUT;
9208
ab648270 9209 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9210 report that. */
334208b7 9211 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9212
559cb2fb
JB
9213 /* Root, child, root x and root y. */
9214 &dummy_window, &dummy_window,
9215 &dummy_coord, &dummy_coord,
12ba150f 9216
559cb2fb
JB
9217 /* Position relative to scroll bar. */
9218 &win_x, &win_y,
12ba150f 9219
559cb2fb
JB
9220 /* Mouse buttons and modifier keys. */
9221 &dummy_mask))
7a13e894 9222 ;
559cb2fb
JB
9223 else
9224 {
06a2c219 9225#if 0
559cb2fb 9226 int inside_height
d9cdbb3d 9227 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9228#endif
559cb2fb 9229 int top_range
d9cdbb3d 9230 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9231
9232 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9233
9234 if (! NILP (bar->dragging))
9235 win_y -= XINT (bar->dragging);
9236
9237 if (win_y < 0)
9238 win_y = 0;
9239 if (win_y > top_range)
9240 win_y = top_range;
9241
334208b7 9242 *fp = f;
7a13e894 9243 *bar_window = bar->window;
559cb2fb
JB
9244
9245 if (! NILP (bar->dragging))
9246 *part = scroll_bar_handle;
9247 else if (win_y < XINT (bar->start))
9248 *part = scroll_bar_above_handle;
9249 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9250 *part = scroll_bar_handle;
9251 else
9252 *part = scroll_bar_below_handle;
12ba150f 9253
e0c1aef2
KH
9254 XSETINT (*x, win_y);
9255 XSETINT (*y, top_range);
12ba150f 9256
39d8bb4d 9257 f->mouse_moved = 0;
559cb2fb
JB
9258 last_mouse_scroll_bar = Qnil;
9259 }
12ba150f 9260
559cb2fb 9261 *time = last_mouse_movement_time;
cf7cb199 9262
cf7cb199 9263 UNBLOCK_INPUT;
12ba150f
JB
9264}
9265
f451eb13 9266
dbc4e1c1 9267/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9268 background colors, and the scroll bars may need to be redrawn.
9269 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9270 redraw them. */
9271
dfcf069d 9272void
ab648270 9273x_scroll_bar_clear (f)
dbc4e1c1
JB
9274 FRAME_PTR f;
9275{
06a2c219 9276#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9277 Lisp_Object bar;
9278
b80c363e
RS
9279 /* We can have scroll bars even if this is 0,
9280 if we just turned off scroll bar mode.
9281 But in that case we should not clear them. */
9282 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9283 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9284 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9285 XClearArea (FRAME_X_DISPLAY (f),
9286 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9287 0, 0, 0, 0, True);
06a2c219 9288#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9289}
9290
06a2c219 9291/* This processes Expose events from the menu-bar specific X event
19126e11 9292 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9293 when handling menu-bar or pop-up items. */
3afe33e7 9294
06a2c219 9295int
3afe33e7
RS
9296process_expose_from_menu (event)
9297 XEvent event;
9298{
9299 FRAME_PTR f;
19126e11 9300 struct x_display_info *dpyinfo;
06a2c219 9301 int frame_exposed_p = 0;
3afe33e7 9302
f94397b5
KH
9303 BLOCK_INPUT;
9304
19126e11
KH
9305 dpyinfo = x_display_info_for_display (event.xexpose.display);
9306 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9307 if (f)
9308 {
9309 if (f->async_visible == 0)
9310 {
9311 f->async_visible = 1;
9312 f->async_iconified = 0;
06c488fd 9313 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9314 SET_FRAME_GARBAGED (f);
9315 }
9316 else
9317 {
06a2c219
GM
9318 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9319 event.xexpose.x, event.xexpose.y,
9320 event.xexpose.width, event.xexpose.height);
9321 frame_exposed_p = 1;
3afe33e7
RS
9322 }
9323 }
9324 else
9325 {
9326 struct scroll_bar *bar
9327 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9328
3afe33e7
RS
9329 if (bar)
9330 x_scroll_bar_expose (bar, &event);
9331 }
f94397b5
KH
9332
9333 UNBLOCK_INPUT;
06a2c219 9334 return frame_exposed_p;
3afe33e7 9335}
09756a85
RS
9336\f
9337/* Define a queue to save up SelectionRequest events for later handling. */
9338
9339struct selection_event_queue
9340 {
9341 XEvent event;
9342 struct selection_event_queue *next;
9343 };
9344
9345static struct selection_event_queue *queue;
9346
9347/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9348
09756a85
RS
9349static int x_queue_selection_requests;
9350
9351/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9352
09756a85 9353static void
334208b7
RS
9354x_queue_event (f, event)
9355 FRAME_PTR f;
09756a85
RS
9356 XEvent *event;
9357{
9358 struct selection_event_queue *queue_tmp
06a2c219 9359 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9360
58769bee 9361 if (queue_tmp != NULL)
09756a85
RS
9362 {
9363 queue_tmp->event = *event;
9364 queue_tmp->next = queue;
9365 queue = queue_tmp;
9366 }
9367}
9368
9369/* Take all the queued events and put them back
9370 so that they get processed afresh. */
9371
9372static void
db3906fd
RS
9373x_unqueue_events (display)
9374 Display *display;
09756a85 9375{
58769bee 9376 while (queue != NULL)
09756a85
RS
9377 {
9378 struct selection_event_queue *queue_tmp = queue;
db3906fd 9379 XPutBackEvent (display, &queue_tmp->event);
09756a85 9380 queue = queue_tmp->next;
06a2c219 9381 xfree ((char *)queue_tmp);
09756a85
RS
9382 }
9383}
9384
9385/* Start queuing SelectionRequest events. */
9386
9387void
db3906fd
RS
9388x_start_queuing_selection_requests (display)
9389 Display *display;
09756a85
RS
9390{
9391 x_queue_selection_requests++;
9392}
9393
9394/* Stop queuing SelectionRequest events. */
9395
9396void
db3906fd
RS
9397x_stop_queuing_selection_requests (display)
9398 Display *display;
09756a85
RS
9399{
9400 x_queue_selection_requests--;
db3906fd 9401 x_unqueue_events (display);
09756a85 9402}
f451eb13
JB
9403\f
9404/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9405
06a2c219 9406/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9407 but we have to put it out here, since static variables within functions
9408 sometimes don't work. */
06a2c219 9409
dc6f92b8
JB
9410static Time enter_timestamp;
9411
11edeb03 9412/* This holds the state XLookupString needs to implement dead keys
58769bee 9413 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9414 says that a portable program can't use this, but Stephen Gildea assures
9415 me that letting the compiler initialize it to zeros will work okay.
9416
9417 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9418 given for enter_time stamp, above. */
9419
11edeb03
JB
9420static XComposeStatus compose_status;
9421
10e6549c
RS
9422/* Record the last 100 characters stored
9423 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9424
2224b905
RS
9425static int temp_index;
9426static short temp_buffer[100];
10e6549c 9427
7a13e894
RS
9428/* Set this to nonzero to fake an "X I/O error"
9429 on a particular display. */
06a2c219 9430
7a13e894
RS
9431struct x_display_info *XTread_socket_fake_io_error;
9432
2224b905
RS
9433/* When we find no input here, we occasionally do a no-op command
9434 to verify that the X server is still running and we can still talk with it.
9435 We try all the open displays, one by one.
9436 This variable is used for cycling thru the displays. */
06a2c219 9437
2224b905
RS
9438static struct x_display_info *next_noop_dpyinfo;
9439
06a2c219
GM
9440#define SET_SAVED_MENU_EVENT(size) \
9441 do \
9442 { \
9443 if (f->output_data.x->saved_menu_event == 0) \
9444 f->output_data.x->saved_menu_event \
9445 = (XEvent *) xmalloc (sizeof (XEvent)); \
9446 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9447 if (numchars >= 1) \
9448 { \
9449 bufp->kind = menu_bar_activate_event; \
9450 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9451 bufp->arg = Qnil; \
06a2c219
GM
9452 bufp++; \
9453 count++; \
9454 numchars--; \
9455 } \
9456 } \
9457 while (0)
9458
8805890a 9459#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9460#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9461
dc6f92b8
JB
9462/* Read events coming from the X server.
9463 This routine is called by the SIGIO handler.
9464 We return as soon as there are no more events to be read.
9465
9466 Events representing keys are stored in buffer BUFP,
9467 which can hold up to NUMCHARS characters.
9468 We return the number of characters stored into the buffer,
9469 thus pretending to be `read'.
9470
dc6f92b8
JB
9471 EXPECTED is nonzero if the caller knows input is available. */
9472
7c5283e4 9473int
f66868ba 9474XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9475 register int sd;
8805890a
KH
9476 /* register */ struct input_event *bufp;
9477 /* register */ int numchars;
dc6f92b8
JB
9478 int expected;
9479{
9480 int count = 0;
9481 int nbytes = 0;
dc6f92b8 9482 XEvent event;
f676886a 9483 struct frame *f;
66f55a9d 9484 int event_found = 0;
334208b7 9485 struct x_display_info *dpyinfo;
379b5ac0 9486 struct coding_system coding;
dc6f92b8 9487
9ac0d9e0 9488 if (interrupt_input_blocked)
dc6f92b8 9489 {
9ac0d9e0 9490 interrupt_input_pending = 1;
dc6f92b8
JB
9491 return -1;
9492 }
9493
9ac0d9e0 9494 interrupt_input_pending = 0;
dc6f92b8 9495 BLOCK_INPUT;
c0a04927
RS
9496
9497 /* So people can tell when we have read the available input. */
9498 input_signal_count++;
9499
dc6f92b8 9500 if (numchars <= 0)
06a2c219 9501 abort (); /* Don't think this happens. */
dc6f92b8 9502
bde5503b
GM
9503 ++handling_signal;
9504
379b5ac0
KH
9505 /* The input should be decoded if it is from XIM. Currently the
9506 locale of XIM is the same as that of the system. So, we can use
9507 Vlocale_coding_system which is initialized properly at Emacs
9508 startup time. */
9509 setup_coding_system (Vlocale_coding_system, &coding);
9510 coding.src_multibyte = 0;
9511 coding.dst_multibyte = 1;
9512 /* The input is converted to events, thus we can't handle
9513 composition. Anyway, there's no XIM that gives us composition
9514 information. */
9515 coding.composing = COMPOSITION_DISABLED;
9516
7a13e894
RS
9517 /* Find the display we are supposed to read input for.
9518 It's the one communicating on descriptor SD. */
9519 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9520 {
9521#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9522#ifdef FIOSNBIO
7a13e894
RS
9523 /* If available, Xlib uses FIOSNBIO to make the socket
9524 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9525 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9526 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9527 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9528#endif /* ! defined (FIOSNBIO) */
7a13e894 9529#endif
dc6f92b8 9530
7a13e894
RS
9531#if 0 /* This code can't be made to work, with multiple displays,
9532 and appears not to be used on any system any more.
9533 Also keyboard.c doesn't turn O_NDELAY on and off
9534 for X connections. */
dc6f92b8
JB
9535#ifndef SIGIO
9536#ifndef HAVE_SELECT
7a13e894
RS
9537 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9538 {
9539 extern int read_alarm_should_throw;
9540 read_alarm_should_throw = 1;
9541 XPeekEvent (dpyinfo->display, &event);
9542 read_alarm_should_throw = 0;
9543 }
c118dd06
JB
9544#endif /* HAVE_SELECT */
9545#endif /* SIGIO */
7a13e894 9546#endif
dc6f92b8 9547
7a13e894
RS
9548 /* For debugging, this gives a way to fake an I/O error. */
9549 if (dpyinfo == XTread_socket_fake_io_error)
9550 {
9551 XTread_socket_fake_io_error = 0;
9552 x_io_error_quitter (dpyinfo->display);
9553 }
dc6f92b8 9554
06a2c219 9555 while (XPending (dpyinfo->display))
dc6f92b8 9556 {
7a13e894 9557 XNextEvent (dpyinfo->display, &event);
06a2c219 9558
531483fb 9559#ifdef HAVE_X_I18N
d1bc4182 9560 {
f2be1146
GM
9561 /* Filter events for the current X input method.
9562 XFilterEvent returns non-zero if the input method has
9563 consumed the event. We pass the frame's X window to
9564 XFilterEvent because that's the one for which the IC
9565 was created. */
f5d11644
GM
9566 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9567 event.xclient.window);
9568 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9569 break;
9570 }
0cd6403b 9571#endif
7a13e894
RS
9572 event_found = 1;
9573
9574 switch (event.type)
9575 {
9576 case ClientMessage:
c047688c 9577 {
7a13e894
RS
9578 if (event.xclient.message_type
9579 == dpyinfo->Xatom_wm_protocols
9580 && event.xclient.format == 32)
c047688c 9581 {
7a13e894
RS
9582 if (event.xclient.data.l[0]
9583 == dpyinfo->Xatom_wm_take_focus)
c047688c 9584 {
8c1a6a84
RS
9585 /* Use x_any_window_to_frame because this
9586 could be the shell widget window
9587 if the frame has no title bar. */
9588 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9589#ifdef HAVE_X_I18N
9590 /* Not quite sure this is needed -pd */
8c1a6a84 9591 if (f && FRAME_XIC (f))
6c183ba5
RS
9592 XSetICFocus (FRAME_XIC (f));
9593#endif
f1da8f06
GM
9594#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9595 instructs the WM to set the input focus automatically for
9596 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9597 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9598 it has set the focus. So, XSetInputFocus below is not
9599 needed.
9600
9601 The call to XSetInputFocus below has also caused trouble. In
9602 cases where the XSetInputFocus done by the WM and the one
9603 below are temporally close (on a fast machine), the call
9604 below can generate additional FocusIn events which confuse
9605 Emacs. */
9606
bf7253f4
RS
9607 /* Since we set WM_TAKE_FOCUS, we must call
9608 XSetInputFocus explicitly. But not if f is null,
9609 since that might be an event for a deleted frame. */
7a13e894 9610 if (f)
bf7253f4
RS
9611 {
9612 Display *d = event.xclient.display;
9613 /* Catch and ignore errors, in case window has been
9614 iconified by a window manager such as GWM. */
9615 int count = x_catch_errors (d);
9616 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9617 /* The ICCCM says this is
9618 the only valid choice. */
9619 RevertToParent,
bf7253f4
RS
9620 event.xclient.data.l[1]);
9621 /* This is needed to detect the error
9622 if there is an error. */
9623 XSync (d, False);
9624 x_uncatch_errors (d, count);
9625 }
7a13e894 9626 /* Not certain about handling scroll bars here */
f1da8f06 9627#endif /* 0 */
c047688c 9628 }
7a13e894
RS
9629 else if (event.xclient.data.l[0]
9630 == dpyinfo->Xatom_wm_save_yourself)
9631 {
9632 /* Save state modify the WM_COMMAND property to
06a2c219 9633 something which can reinstate us. This notifies
7a13e894
RS
9634 the session manager, who's looking for such a
9635 PropertyNotify. Can restart processing when
06a2c219 9636 a keyboard or mouse event arrives. */
7a13e894
RS
9637 if (numchars > 0)
9638 {
19126e11
KH
9639 f = x_top_window_to_frame (dpyinfo,
9640 event.xclient.window);
7a13e894
RS
9641
9642 /* This is just so we only give real data once
9643 for a single Emacs process. */
b86bd3dd 9644 if (f == SELECTED_FRAME ())
7a13e894
RS
9645 XSetCommand (FRAME_X_DISPLAY (f),
9646 event.xclient.window,
9647 initial_argv, initial_argc);
f000f5c5 9648 else if (f)
7a13e894
RS
9649 XSetCommand (FRAME_X_DISPLAY (f),
9650 event.xclient.window,
9651 0, 0);
9652 }
9653 }
9654 else if (event.xclient.data.l[0]
9655 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9656 {
19126e11
KH
9657 struct frame *f
9658 = x_any_window_to_frame (dpyinfo,
9659 event.xclient.window);
1fb20991 9660
7a13e894
RS
9661 if (f)
9662 {
9663 if (numchars == 0)
9664 abort ();
1fb20991 9665
7a13e894
RS
9666 bufp->kind = delete_window_event;
9667 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9668 bufp->arg = Qnil;
7a13e894
RS
9669 bufp++;
9670
9671 count += 1;
9672 numchars -= 1;
9673 }
1fb20991 9674 }
c047688c 9675 }
7a13e894
RS
9676 else if (event.xclient.message_type
9677 == dpyinfo->Xatom_wm_configure_denied)
9678 {
9679 }
9680 else if (event.xclient.message_type
9681 == dpyinfo->Xatom_wm_window_moved)
9682 {
9683 int new_x, new_y;
19126e11
KH
9684 struct frame *f
9685 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9686
7a13e894
RS
9687 new_x = event.xclient.data.s[0];
9688 new_y = event.xclient.data.s[1];
1fb20991 9689
7a13e894
RS
9690 if (f)
9691 {
7556890b
RS
9692 f->output_data.x->left_pos = new_x;
9693 f->output_data.x->top_pos = new_y;
7a13e894 9694 }
1fb20991 9695 }
0fdff6bb 9696#ifdef HACK_EDITRES
7a13e894
RS
9697 else if (event.xclient.message_type
9698 == dpyinfo->Xatom_editres)
9699 {
19126e11
KH
9700 struct frame *f
9701 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9702 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9703 &event, NULL);
7a13e894 9704 }
0fdff6bb 9705#endif /* HACK_EDITRES */
06a2c219
GM
9706 else if ((event.xclient.message_type
9707 == dpyinfo->Xatom_DONE)
9708 || (event.xclient.message_type
9709 == dpyinfo->Xatom_PAGE))
9710 {
9711 /* Ghostview job completed. Kill it. We could
9712 reply with "Next" if we received "Page", but we
9713 currently never do because we are interested in
9714 images, only, which should have 1 page. */
06a2c219
GM
9715 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9716 struct frame *f
9717 = x_window_to_frame (dpyinfo, event.xclient.window);
9718 x_kill_gs_process (pixmap, f);
9719 expose_frame (f, 0, 0, 0, 0);
9720 }
9721#ifdef USE_TOOLKIT_SCROLL_BARS
9722 /* Scroll bar callbacks send a ClientMessage from which
9723 we construct an input_event. */
9724 else if (event.xclient.message_type
9725 == dpyinfo->Xatom_Scrollbar)
9726 {
9727 x_scroll_bar_to_input_event (&event, bufp);
9728 ++bufp, ++count, --numchars;
9729 goto out;
9730 }
9731#endif /* USE_TOOLKIT_SCROLL_BARS */
9732 else
9733 goto OTHER;
7a13e894
RS
9734 }
9735 break;
dc6f92b8 9736
7a13e894 9737 case SelectionNotify:
3afe33e7 9738#ifdef USE_X_TOOLKIT
19126e11 9739 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9740 goto OTHER;
3afe33e7 9741#endif /* not USE_X_TOOLKIT */
dfcf069d 9742 x_handle_selection_notify (&event.xselection);
7a13e894 9743 break;
d56a553a 9744
06a2c219 9745 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9746#ifdef USE_X_TOOLKIT
19126e11 9747 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9748 goto OTHER;
3afe33e7 9749#endif /* USE_X_TOOLKIT */
7a13e894
RS
9750 {
9751 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9752
7a13e894
RS
9753 if (numchars == 0)
9754 abort ();
d56a553a 9755
7a13e894
RS
9756 bufp->kind = selection_clear_event;
9757 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9758 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9759 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9760 bufp->frame_or_window = Qnil;
0f8aabe9 9761 bufp->arg = Qnil;
7a13e894 9762 bufp++;
d56a553a 9763
7a13e894
RS
9764 count += 1;
9765 numchars -= 1;
9766 }
9767 break;
dc6f92b8 9768
06a2c219 9769 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9770#ifdef USE_X_TOOLKIT
19126e11 9771 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9772 goto OTHER;
3afe33e7 9773#endif /* USE_X_TOOLKIT */
7a13e894 9774 if (x_queue_selection_requests)
19126e11 9775 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9776 &event);
9777 else
9778 {
9779 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9780
7a13e894
RS
9781 if (numchars == 0)
9782 abort ();
9783
9784 bufp->kind = selection_request_event;
9785 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9786 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9787 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9788 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9789 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9790 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9791 bufp->frame_or_window = Qnil;
0f8aabe9 9792 bufp->arg = Qnil;
7a13e894
RS
9793 bufp++;
9794
9795 count += 1;
9796 numchars -= 1;
9797 }
9798 break;
9799
9800 case PropertyNotify:
3afe33e7 9801#ifdef USE_X_TOOLKIT
19126e11 9802 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9803 goto OTHER;
3afe33e7 9804#endif /* not USE_X_TOOLKIT */
dfcf069d 9805 x_handle_property_notify (&event.xproperty);
7a13e894 9806 break;
dc6f92b8 9807
7a13e894 9808 case ReparentNotify:
19126e11 9809 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9810 if (f)
9811 {
9812 int x, y;
7556890b 9813 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9814 x_real_positions (f, &x, &y);
7556890b
RS
9815 f->output_data.x->left_pos = x;
9816 f->output_data.x->top_pos = y;
7a13e894
RS
9817 }
9818 break;
3bd330d4 9819
7a13e894 9820 case Expose:
19126e11 9821 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9822 if (f)
dc6f92b8 9823 {
7a13e894
RS
9824 if (f->async_visible == 0)
9825 {
9826 f->async_visible = 1;
9827 f->async_iconified = 0;
06c488fd 9828 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9829 SET_FRAME_GARBAGED (f);
9830 }
9831 else
06a2c219
GM
9832 expose_frame (x_window_to_frame (dpyinfo,
9833 event.xexpose.window),
9834 event.xexpose.x, event.xexpose.y,
9835 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9836 }
9837 else
7a13e894 9838 {
06a2c219
GM
9839#ifdef USE_TOOLKIT_SCROLL_BARS
9840 /* Dispatch event to the widget. */
9841 goto OTHER;
9842#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9843 struct scroll_bar *bar
9844 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9845
7a13e894
RS
9846 if (bar)
9847 x_scroll_bar_expose (bar, &event);
3afe33e7 9848#ifdef USE_X_TOOLKIT
7a13e894
RS
9849 else
9850 goto OTHER;
3afe33e7 9851#endif /* USE_X_TOOLKIT */
06a2c219 9852#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9853 }
9854 break;
dc6f92b8 9855
7a13e894
RS
9856 case GraphicsExpose: /* This occurs when an XCopyArea's
9857 source area was obscured or not
9858 available.*/
19126e11 9859 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9860 if (f)
9861 {
06a2c219
GM
9862 expose_frame (f,
9863 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9864 event.xgraphicsexpose.width,
9865 event.xgraphicsexpose.height);
7a13e894 9866 }
3afe33e7 9867#ifdef USE_X_TOOLKIT
7a13e894
RS
9868 else
9869 goto OTHER;
3afe33e7 9870#endif /* USE_X_TOOLKIT */
7a13e894 9871 break;
dc6f92b8 9872
7a13e894 9873 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9874 source area was completely
9875 available */
7a13e894 9876 break;
dc6f92b8 9877
7a13e894 9878 case UnmapNotify:
06a2c219
GM
9879 /* Redo the mouse-highlight after the tooltip has gone. */
9880 if (event.xmap.window == tip_window)
9881 {
9882 tip_window = 0;
9883 redo_mouse_highlight ();
9884 }
9885
91ea2a7a 9886 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9887 if (f) /* F may no longer exist if
9888 the frame was deleted. */
9889 {
9890 /* While a frame is unmapped, display generation is
9891 disabled; you don't want to spend time updating a
9892 display that won't ever be seen. */
9893 f->async_visible = 0;
9894 /* We can't distinguish, from the event, whether the window
9895 has become iconified or invisible. So assume, if it
9896 was previously visible, than now it is iconified.
1aa6072f
RS
9897 But x_make_frame_invisible clears both
9898 the visible flag and the iconified flag;
9899 and that way, we know the window is not iconified now. */
7a13e894 9900 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9901 {
9902 f->async_iconified = 1;
bddd097c 9903
1aa6072f
RS
9904 bufp->kind = iconify_event;
9905 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9906 bufp->arg = Qnil;
1aa6072f
RS
9907 bufp++;
9908 count++;
9909 numchars--;
9910 }
7a13e894 9911 }
7a13e894 9912 goto OTHER;
dc6f92b8 9913
7a13e894 9914 case MapNotify:
06a2c219
GM
9915 if (event.xmap.window == tip_window)
9916 /* The tooltip has been drawn already. Avoid
9917 the SET_FRAME_GARBAGED below. */
9918 goto OTHER;
9919
9920 /* We use x_top_window_to_frame because map events can
9921 come for sub-windows and they don't mean that the
9922 frame is visible. */
19126e11 9923 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9924 if (f)
9925 {
9926 f->async_visible = 1;
9927 f->async_iconified = 0;
06c488fd 9928 f->output_data.x->has_been_visible = 1;
dc6f92b8 9929
7a13e894
RS
9930 /* wait_reading_process_input will notice this and update
9931 the frame's display structures. */
9932 SET_FRAME_GARBAGED (f);
bddd097c 9933
d806e720
RS
9934 if (f->iconified)
9935 {
9936 bufp->kind = deiconify_event;
9937 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9938 bufp->arg = Qnil;
d806e720
RS
9939 bufp++;
9940 count++;
9941 numchars--;
9942 }
e73ec6fa 9943 else if (! NILP (Vframe_list)
8e713be6 9944 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9945 /* Force a redisplay sooner or later
9946 to update the frame titles
9947 in case this is the second frame. */
9948 record_asynch_buffer_change ();
7a13e894 9949 }
7a13e894 9950 goto OTHER;
dc6f92b8 9951
7a13e894 9952 case KeyPress:
19126e11 9953 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9954
eccc05db 9955#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9956 /* I couldn't find a way to prevent LessTif scroll bars
9957 from consuming key events. */
9958 if (f == 0)
9959 {
9960 Widget widget = XtWindowToWidget (dpyinfo->display,
9961 event.xkey.window);
9962 if (widget && XmIsScrollBar (widget))
9963 {
9964 widget = XtParent (widget);
9965 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9966 }
9967 }
eccc05db 9968#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9969
7a13e894
RS
9970 if (f != 0)
9971 {
9972 KeySym keysym, orig_keysym;
379b5ac0
KH
9973 /* al%imercury@uunet.uu.net says that making this 81
9974 instead of 80 fixed a bug whereby meta chars made
9975 his Emacs hang.
9976
9977 It seems that some version of XmbLookupString has
9978 a bug of not returning XBufferOverflow in
9979 status_return even if the input is too long to
9980 fit in 81 bytes. So, we must prepare sufficient
9981 bytes for copy_buffer. 513 bytes (256 chars for
9982 two-byte character set) seems to be a faily good
9983 approximation. -- 2000.8.10 handa@etl.go.jp */
9984 unsigned char copy_buffer[513];
9985 unsigned char *copy_bufptr = copy_buffer;
9986 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9987 int modifiers;
64bb1782 9988
7a13e894
RS
9989 event.xkey.state
9990 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9991 extra_keyboard_modifiers);
9992 modifiers = event.xkey.state;
3a2712f9 9993
7a13e894 9994 /* This will have to go some day... */
752a043f 9995
7a13e894
RS
9996 /* make_lispy_event turns chars into control chars.
9997 Don't do it here because XLookupString is too eager. */
9998 event.xkey.state &= ~ControlMask;
5d46f928
RS
9999 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10000 | dpyinfo->super_mod_mask
10001 | dpyinfo->hyper_mod_mask
10002 | dpyinfo->alt_mod_mask);
10003
1cf4a0d1
RS
10004 /* In case Meta is ComposeCharacter,
10005 clear its status. According to Markus Ehrnsperger
10006 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10007 this enables ComposeCharacter to work whether or
10008 not it is combined with Meta. */
10009 if (modifiers & dpyinfo->meta_mod_mask)
10010 bzero (&compose_status, sizeof (compose_status));
10011
6c183ba5
RS
10012#ifdef HAVE_X_I18N
10013 if (FRAME_XIC (f))
10014 {
f5d11644
GM
10015 Status status_return;
10016
6c183ba5 10017 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
10018 &event.xkey, copy_bufptr,
10019 copy_bufsiz, &keysym,
6c183ba5 10020 &status_return);
f5d11644
GM
10021 if (status_return == XBufferOverflow)
10022 {
10023 copy_bufsiz = nbytes + 1;
10024 copy_bufptr = (char *) alloca (copy_bufsiz);
10025 nbytes = XmbLookupString (FRAME_XIC (f),
10026 &event.xkey, copy_bufptr,
10027 copy_bufsiz, &keysym,
10028 &status_return);
10029 }
10030
1decb680
PE
10031 if (status_return == XLookupNone)
10032 break;
10033 else if (status_return == XLookupChars)
fdd9d55e
GM
10034 {
10035 keysym = NoSymbol;
10036 modifiers = 0;
10037 }
1decb680
PE
10038 else if (status_return != XLookupKeySym
10039 && status_return != XLookupBoth)
10040 abort ();
6c183ba5
RS
10041 }
10042 else
379b5ac0
KH
10043 nbytes = XLookupString (&event.xkey, copy_bufptr,
10044 copy_bufsiz, &keysym,
10045 &compose_status);
6c183ba5 10046#else
379b5ac0
KH
10047 nbytes = XLookupString (&event.xkey, copy_bufptr,
10048 copy_bufsiz, &keysym,
10049 &compose_status);
6c183ba5 10050#endif
dc6f92b8 10051
7a13e894 10052 orig_keysym = keysym;
55123275 10053
7a13e894
RS
10054 if (numchars > 1)
10055 {
10056 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10057 || keysym == XK_Delete
1097aea0 10058#ifdef XK_ISO_Left_Tab
441affdb 10059 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 10060#endif
852bff8f 10061 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
10062 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10063 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 10064#ifdef HPUX
7a13e894
RS
10065 /* This recognizes the "extended function keys".
10066 It seems there's no cleaner way.
10067 Test IsModifierKey to avoid handling mode_switch
10068 incorrectly. */
10069 || ((unsigned) (keysym) >= XK_Select
10070 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
10071#endif
10072#ifdef XK_dead_circumflex
7a13e894 10073 || orig_keysym == XK_dead_circumflex
69388238
RS
10074#endif
10075#ifdef XK_dead_grave
7a13e894 10076 || orig_keysym == XK_dead_grave
69388238
RS
10077#endif
10078#ifdef XK_dead_tilde
7a13e894 10079 || orig_keysym == XK_dead_tilde
69388238
RS
10080#endif
10081#ifdef XK_dead_diaeresis
7a13e894 10082 || orig_keysym == XK_dead_diaeresis
69388238
RS
10083#endif
10084#ifdef XK_dead_macron
7a13e894 10085 || orig_keysym == XK_dead_macron
69388238
RS
10086#endif
10087#ifdef XK_dead_degree
7a13e894 10088 || orig_keysym == XK_dead_degree
69388238
RS
10089#endif
10090#ifdef XK_dead_acute
7a13e894 10091 || orig_keysym == XK_dead_acute
69388238
RS
10092#endif
10093#ifdef XK_dead_cedilla
7a13e894 10094 || orig_keysym == XK_dead_cedilla
69388238
RS
10095#endif
10096#ifdef XK_dead_breve
7a13e894 10097 || orig_keysym == XK_dead_breve
69388238
RS
10098#endif
10099#ifdef XK_dead_ogonek
7a13e894 10100 || orig_keysym == XK_dead_ogonek
69388238
RS
10101#endif
10102#ifdef XK_dead_caron
7a13e894 10103 || orig_keysym == XK_dead_caron
69388238
RS
10104#endif
10105#ifdef XK_dead_doubleacute
7a13e894 10106 || orig_keysym == XK_dead_doubleacute
69388238
RS
10107#endif
10108#ifdef XK_dead_abovedot
7a13e894 10109 || orig_keysym == XK_dead_abovedot
c34790e0 10110#endif
7a13e894
RS
10111 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10112 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10113 /* Any "vendor-specific" key is ok. */
10114 || (orig_keysym & (1 << 28)))
10115 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
10116#ifndef HAVE_X11R5
10117#ifdef XK_Mode_switch
7a13e894 10118 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
10119#endif
10120#ifdef XK_Num_Lock
7a13e894 10121 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
10122#endif
10123#endif /* not HAVE_X11R5 */
7a13e894 10124 ))
dc6f92b8 10125 {
10e6549c
RS
10126 if (temp_index == sizeof temp_buffer / sizeof (short))
10127 temp_index = 0;
7a13e894
RS
10128 temp_buffer[temp_index++] = keysym;
10129 bufp->kind = non_ascii_keystroke;
10130 bufp->code = keysym;
e0c1aef2 10131 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10132 bufp->arg = Qnil;
334208b7
RS
10133 bufp->modifiers
10134 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10135 modifiers);
1113d9db 10136 bufp->timestamp = event.xkey.time;
dc6f92b8 10137 bufp++;
7a13e894
RS
10138 count++;
10139 numchars--;
dc6f92b8 10140 }
7a13e894
RS
10141 else if (numchars > nbytes)
10142 {
10143 register int i;
379b5ac0 10144 register int c;
379b5ac0 10145 int nchars, len;
7a13e894
RS
10146
10147 for (i = 0; i < nbytes; i++)
10148 {
379b5ac0
KH
10149 if (temp_index == (sizeof temp_buffer
10150 / sizeof (short)))
7a13e894 10151 temp_index = 0;
379b5ac0
KH
10152 temp_buffer[temp_index++] = copy_bufptr[i];
10153 }
10154
10155 if (/* If the event is not from XIM, */
10156 event.xkey.keycode != 0
10157 /* or the current locale doesn't request
10158 decoding of the intup data, ... */
10159 || coding.type == coding_type_raw_text
10160 || coding.type == coding_type_no_conversion)
10161 {
10162 /* ... we can use the input data as is. */
10163 nchars = nbytes;
10164 }
10165 else
10166 {
10167 /* We have to decode the input data. */
10168 int require;
10169 unsigned char *p;
10170
10171 require = decoding_buffer_size (&coding, nbytes);
10172 p = (unsigned char *) alloca (require);
10173 coding.mode |= CODING_MODE_LAST_BLOCK;
10174 decode_coding (&coding, copy_bufptr, p,
10175 nbytes, require);
10176 nbytes = coding.produced;
10177 nchars = coding.produced_char;
10178 copy_bufptr = p;
10179 }
10180
10181 /* Convert the input data to a sequence of
10182 character events. */
10183 for (i = 0; i < nbytes; i += len)
10184 {
10185 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10186 nbytes - i, len);
10187 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10188 ? ascii_keystroke
10189 : multibyte_char_keystroke);
10190 bufp->code = c;
7a13e894 10191 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10192 bufp->arg = Qnil;
7a13e894
RS
10193 bufp->modifiers
10194 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10195 modifiers);
10196 bufp->timestamp = event.xkey.time;
10197 bufp++;
10198 }
10199
379b5ac0
KH
10200 count += nchars;
10201 numchars -= nchars;
1decb680
PE
10202
10203 if (keysym == NoSymbol)
10204 break;
7a13e894
RS
10205 }
10206 else
10207 abort ();
dc6f92b8 10208 }
10e6549c
RS
10209 else
10210 abort ();
dc6f92b8 10211 }
59ddecde
GM
10212#ifdef HAVE_X_I18N
10213 /* Don't dispatch this event since XtDispatchEvent calls
10214 XFilterEvent, and two calls in a row may freeze the
10215 client. */
10216 break;
10217#else
717ca130 10218 goto OTHER;
59ddecde 10219#endif
f451eb13 10220
f5d11644 10221 case KeyRelease:
59ddecde
GM
10222#ifdef HAVE_X_I18N
10223 /* Don't dispatch this event since XtDispatchEvent calls
10224 XFilterEvent, and two calls in a row may freeze the
10225 client. */
10226 break;
10227#else
f5d11644 10228 goto OTHER;
59ddecde 10229#endif
f5d11644 10230
7a13e894 10231 /* Here's a possible interpretation of the whole
06a2c219
GM
10232 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10233 you get a FocusIn event, you have to get a FocusOut
10234 event before you relinquish the focus. If you
10235 haven't received a FocusIn event, then a mere
10236 LeaveNotify is enough to free you. */
f451eb13 10237
7a13e894 10238 case EnterNotify:
06a2c219
GM
10239 {
10240 int from_menu_bar_p = 0;
10241
10242 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10243
10244#ifdef LESSTIF_VERSION
10245 /* When clicking outside of a menu bar popup to close
10246 it, we get a FocusIn/ EnterNotify sequence of
10247 events. The flag event.xcrossing.focus is not set
10248 in the EnterNotify event of that sequence because
10249 the focus is in the menu bar,
10250 event.xcrossing.window is the frame's X window.
10251 Unconditionally setting the focus frame to null in
10252 this case is not the right thing, because no event
10253 follows that could set the focus frame to the right
10254 value.
10255
10256 This could be a LessTif bug, but I wasn't able to
10257 reproduce the behavior in a simple test program.
3be7e417
GM
10258 On the other hand, Motif seems to not have this
10259 problem.
06a2c219 10260
3be7e417 10261 (gerd, LessTif 0.92). */
06a2c219
GM
10262
10263 if (!event.xcrossing.focus
10264 && f
10265 && f->output_data.x->menubar_widget)
10266 {
10267 Window focus;
10268 int revert;
10269
10270 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10271 if (focus == XtWindow (f->output_data.x->menubar_widget))
10272 from_menu_bar_p = 1;
10273 }
10274#endif /* LESSTIF_VERSION */
6d4238f3 10275
06a2c219
GM
10276 if (event.xcrossing.focus || from_menu_bar_p)
10277 {
10278 /* Avoid nasty pop/raise loops. */
10279 if (f && (!(f->auto_raise)
10280 || !(f->auto_lower)
10281 || (event.xcrossing.time - enter_timestamp) > 500))
10282 {
10283 x_new_focus_frame (dpyinfo, f);
10284 enter_timestamp = event.xcrossing.time;
10285 }
10286 }
10287 else if (f == dpyinfo->x_focus_frame)
10288 x_new_focus_frame (dpyinfo, 0);
10289
10290 /* EnterNotify counts as mouse movement,
10291 so update things that depend on mouse position. */
2533c408 10292 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10293 note_mouse_movement (f, &event.xmotion);
10294 goto OTHER;
10295 }
dc6f92b8 10296
7a13e894 10297 case FocusIn:
19126e11 10298 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10299 if (event.xfocus.detail != NotifyPointer)
0f941935 10300 dpyinfo->x_focus_event_frame = f;
7a13e894 10301 if (f)
eb72635f
GM
10302 {
10303 x_new_focus_frame (dpyinfo, f);
10304
10305 /* Don't stop displaying the initial startup message
10306 for a switch-frame event we don't need. */
10307 if (GC_NILP (Vterminal_frame)
10308 && GC_CONSP (Vframe_list)
10309 && !GC_NILP (XCDR (Vframe_list)))
10310 {
10311 bufp->kind = FOCUS_IN_EVENT;
10312 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10313 bufp->arg = Qnil;
eb72635f
GM
10314 ++bufp, ++count, --numchars;
10315 }
10316 }
f9e24cb9 10317
6c183ba5
RS
10318#ifdef HAVE_X_I18N
10319 if (f && FRAME_XIC (f))
10320 XSetICFocus (FRAME_XIC (f));
10321#endif
10322
7a13e894 10323 goto OTHER;
10c5e63d 10324
7a13e894 10325 case LeaveNotify:
19126e11 10326 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10327 if (f)
10c5e63d 10328 {
06a2c219
GM
10329 int from_menu_bar_p = 0;
10330
7a13e894 10331 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10332 {
10333 /* If we move outside the frame, then we're
10334 certainly no longer on any text in the frame. */
10335 clear_mouse_face (dpyinfo);
10336 dpyinfo->mouse_face_mouse_frame = 0;
10337 }
10338
10339 /* Generate a nil HELP_EVENT to cancel a help-echo.
10340 Do it only if there's something to cancel.
10341 Otherwise, the startup message is cleared when
10342 the mouse leaves the frame. */
10343 if (any_help_event_p)
10344 {
be010514
GM
10345 Lisp_Object frame;
10346 int n;
10347
06a2c219 10348 XSETFRAME (frame, f);
82c5d67a 10349 help_echo = Qnil;
5ab2570d
GM
10350 n = gen_help_event (bufp, numchars,
10351 Qnil, frame, Qnil, Qnil, 0);
be010514 10352 bufp += n, count += n, numchars -= n;
06a2c219 10353 }
7a13e894 10354
06a2c219
GM
10355#ifdef LESSTIF_VERSION
10356 /* Please see the comment at the start of the
10357 EnterNotify case. */
10358 if (!event.xcrossing.focus
10359 && f->output_data.x->menubar_widget)
10360 {
10361 Window focus;
10362 int revert;
10363 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10364 if (focus == XtWindow (f->output_data.x->menubar_widget))
10365 from_menu_bar_p = 1;
10366 }
10367#endif /* LESSTIF_VERSION */
10368
10369 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10370 x_mouse_leave (dpyinfo);
10c5e63d 10371 else
7a13e894 10372 {
0f941935
KH
10373 if (f == dpyinfo->x_focus_event_frame)
10374 dpyinfo->x_focus_event_frame = 0;
10375 if (f == dpyinfo->x_focus_frame)
10376 x_new_focus_frame (dpyinfo, 0);
7a13e894 10377 }
10c5e63d 10378 }
7a13e894 10379 goto OTHER;
dc6f92b8 10380
7a13e894 10381 case FocusOut:
19126e11 10382 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10383 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10384 && f == dpyinfo->x_focus_event_frame)
10385 dpyinfo->x_focus_event_frame = 0;
10386 if (f && f == dpyinfo->x_focus_frame)
10387 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10388
6c183ba5
RS
10389#ifdef HAVE_X_I18N
10390 if (f && FRAME_XIC (f))
10391 XUnsetICFocus (FRAME_XIC (f));
10392#endif
10393
7a13e894 10394 goto OTHER;
dc6f92b8 10395
7a13e894 10396 case MotionNotify:
dc6f92b8 10397 {
06a2c219 10398 previous_help_echo = help_echo;
7cea38bc 10399 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10400 help_echo_pos = -1;
06a2c219 10401
7a13e894
RS
10402 if (dpyinfo->grabbed && last_mouse_frame
10403 && FRAME_LIVE_P (last_mouse_frame))
10404 f = last_mouse_frame;
10405 else
19126e11 10406 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10407
7a13e894
RS
10408 if (f)
10409 note_mouse_movement (f, &event.xmotion);
10410 else
10411 {
e88b3c50 10412#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10413 struct scroll_bar *bar
10414 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10415
7a13e894
RS
10416 if (bar)
10417 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10418#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10419
06a2c219
GM
10420 /* If we move outside the frame, then we're
10421 certainly no longer on any text in the frame. */
7a13e894
RS
10422 clear_mouse_face (dpyinfo);
10423 }
06a2c219
GM
10424
10425 /* If the contents of the global variable help_echo
10426 has changed, generate a HELP_EVENT. */
b7e80413
SM
10427 if (!NILP (help_echo)
10428 || !NILP (previous_help_echo))
06a2c219
GM
10429 {
10430 Lisp_Object frame;
be010514 10431 int n;
06a2c219
GM
10432
10433 if (f)
10434 XSETFRAME (frame, f);
10435 else
10436 frame = Qnil;
10437
10438 any_help_event_p = 1;
5ab2570d 10439 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10440 help_echo_window, help_echo_object,
10441 help_echo_pos);
be010514 10442 bufp += n, count += n, numchars -= n;
06a2c219
GM
10443 }
10444
10445 goto OTHER;
dc6f92b8 10446 }
dc6f92b8 10447
7a13e894 10448 case ConfigureNotify:
9829ddba
RS
10449 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10450 if (f)
af395ec1 10451 {
5c187dee 10452#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10453 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10454 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10455
2d7fc7e8
RS
10456 /* In the toolkit version, change_frame_size
10457 is called by the code that handles resizing
10458 of the EmacsFrame widget. */
7a13e894 10459
7a13e894
RS
10460 /* Even if the number of character rows and columns has
10461 not changed, the font size may have changed, so we need
10462 to check the pixel dimensions as well. */
10463 if (columns != f->width
10464 || rows != f->height
7556890b
RS
10465 || event.xconfigure.width != f->output_data.x->pixel_width
10466 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10467 {
7d1e984f 10468 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10469 SET_FRAME_GARBAGED (f);
e687d06e 10470 cancel_mouse_face (f);
7a13e894 10471 }
2d7fc7e8 10472#endif
af395ec1 10473
7556890b
RS
10474 f->output_data.x->pixel_width = event.xconfigure.width;
10475 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10476
10477 /* What we have now is the position of Emacs's own window.
10478 Convert that to the position of the window manager window. */
dcb07ae9
RS
10479 x_real_positions (f, &f->output_data.x->left_pos,
10480 &f->output_data.x->top_pos);
10481
f5d11644
GM
10482#ifdef HAVE_X_I18N
10483 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10484 xic_set_statusarea (f);
10485#endif
10486
dcb07ae9
RS
10487 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10488 {
10489 /* Since the WM decorations come below top_pos now,
10490 we must put them below top_pos in the future. */
10491 f->output_data.x->win_gravity = NorthWestGravity;
10492 x_wm_set_size_hint (f, (long) 0, 0);
10493 }
8f08dc93
KH
10494#ifdef USE_MOTIF
10495 /* Some window managers pass (0,0) as the location of
10496 the window, and the Motif event handler stores it
10497 in the emacs widget, which messes up Motif menus. */
10498 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10499 {
10500 event.xconfigure.x = f->output_data.x->widget->core.x;
10501 event.xconfigure.y = f->output_data.x->widget->core.y;
10502 }
06a2c219 10503#endif /* USE_MOTIF */
7a13e894 10504 }
2d7fc7e8 10505 goto OTHER;
dc6f92b8 10506
7a13e894
RS
10507 case ButtonPress:
10508 case ButtonRelease:
10509 {
10510 /* If we decide we want to generate an event to be seen
10511 by the rest of Emacs, we put it here. */
10512 struct input_event emacs_event;
9ea173e8 10513 int tool_bar_p = 0;
06a2c219 10514
7a13e894 10515 emacs_event.kind = no_event;
7a13e894 10516 bzero (&compose_status, sizeof (compose_status));
9b07615b 10517
06a2c219
GM
10518 if (dpyinfo->grabbed
10519 && last_mouse_frame
9f67f20b
RS
10520 && FRAME_LIVE_P (last_mouse_frame))
10521 f = last_mouse_frame;
10522 else
2224b905 10523 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10524
06a2c219
GM
10525 if (f)
10526 {
9ea173e8
GM
10527 /* Is this in the tool-bar? */
10528 if (WINDOWP (f->tool_bar_window)
10529 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10530 {
10531 Lisp_Object window;
10532 int p, x, y;
10533
10534 x = event.xbutton.x;
10535 y = event.xbutton.y;
10536
10537 /* Set x and y. */
10538 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10539 if (EQ (window, f->tool_bar_window))
06a2c219 10540 {
9ea173e8
GM
10541 x_handle_tool_bar_click (f, &event.xbutton);
10542 tool_bar_p = 1;
06a2c219
GM
10543 }
10544 }
10545
9ea173e8 10546 if (!tool_bar_p)
06a2c219
GM
10547 if (!dpyinfo->x_focus_frame
10548 || f == dpyinfo->x_focus_frame)
10549 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10550 }
10551 else
10552 {
06a2c219 10553#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10554 struct scroll_bar *bar
10555 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10556
7a13e894
RS
10557 if (bar)
10558 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10559#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10560 }
10561
10562 if (event.type == ButtonPress)
10563 {
10564 dpyinfo->grabbed |= (1 << event.xbutton.button);
10565 last_mouse_frame = f;
edad46f6
KH
10566 /* Ignore any mouse motion that happened
10567 before this event; any subsequent mouse-movement
10568 Emacs events should reflect only motion after
10569 the ButtonPress. */
a00e91cd
KH
10570 if (f != 0)
10571 f->mouse_moved = 0;
06a2c219 10572
9ea173e8
GM
10573 if (!tool_bar_p)
10574 last_tool_bar_item = -1;
7a13e894 10575 }
3afe33e7
RS
10576 else
10577 {
7a13e894 10578 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10579 }
23faf38f 10580
7a13e894
RS
10581 if (numchars >= 1 && emacs_event.kind != no_event)
10582 {
10583 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10584 bufp++;
10585 count++;
10586 numchars--;
10587 }
3afe33e7
RS
10588
10589#ifdef USE_X_TOOLKIT
2224b905
RS
10590 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10591 /* For a down-event in the menu bar,
10592 don't pass it to Xt right now.
10593 Instead, save it away
10594 and we will pass it to Xt from kbd_buffer_get_event.
10595 That way, we can run some Lisp code first. */
91375f8f
RS
10596 if (f && event.type == ButtonPress
10597 /* Verify the event is really within the menu bar
10598 and not just sent to it due to grabbing. */
10599 && event.xbutton.x >= 0
10600 && event.xbutton.x < f->output_data.x->pixel_width
10601 && event.xbutton.y >= 0
10602 && event.xbutton.y < f->output_data.x->menubar_height
10603 && event.xbutton.same_screen)
2224b905 10604 {
8805890a 10605 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10606 XSETFRAME (last_mouse_press_frame, f);
10607 }
10608 else if (event.type == ButtonPress)
10609 {
10610 last_mouse_press_frame = Qnil;
30e671c3 10611 goto OTHER;
ce89ef46 10612 }
06a2c219 10613
2237cac9
RS
10614#ifdef USE_MOTIF /* This should do not harm for Lucid,
10615 but I am trying to be cautious. */
ce89ef46
RS
10616 else if (event.type == ButtonRelease)
10617 {
2237cac9 10618 if (!NILP (last_mouse_press_frame))
f10ded1c 10619 {
2237cac9
RS
10620 f = XFRAME (last_mouse_press_frame);
10621 if (f->output_data.x)
06a2c219 10622 SET_SAVED_BUTTON_EVENT;
f10ded1c 10623 }
06a2c219 10624 else
30e671c3 10625 goto OTHER;
2224b905 10626 }
2237cac9 10627#endif /* USE_MOTIF */
2224b905
RS
10628 else
10629 goto OTHER;
3afe33e7 10630#endif /* USE_X_TOOLKIT */
7a13e894
RS
10631 }
10632 break;
dc6f92b8 10633
7a13e894 10634 case CirculateNotify:
06a2c219
GM
10635 goto OTHER;
10636
7a13e894 10637 case CirculateRequest:
06a2c219
GM
10638 goto OTHER;
10639
10640 case VisibilityNotify:
10641 goto OTHER;
dc6f92b8 10642
7a13e894
RS
10643 case MappingNotify:
10644 /* Someone has changed the keyboard mapping - update the
10645 local cache. */
10646 switch (event.xmapping.request)
10647 {
10648 case MappingModifier:
10649 x_find_modifier_meanings (dpyinfo);
10650 /* This is meant to fall through. */
10651 case MappingKeyboard:
10652 XRefreshKeyboardMapping (&event.xmapping);
10653 }
7a13e894 10654 goto OTHER;
dc6f92b8 10655
7a13e894 10656 default:
7a13e894 10657 OTHER:
717ca130 10658#ifdef USE_X_TOOLKIT
7a13e894
RS
10659 BLOCK_INPUT;
10660 XtDispatchEvent (&event);
10661 UNBLOCK_INPUT;
3afe33e7 10662#endif /* USE_X_TOOLKIT */
7a13e894
RS
10663 break;
10664 }
dc6f92b8
JB
10665 }
10666 }
10667
06a2c219
GM
10668 out:;
10669
9a5196d0
RS
10670 /* On some systems, an X bug causes Emacs to get no more events
10671 when the window is destroyed. Detect that. (1994.) */
58769bee 10672 if (! event_found)
ef2a22d0 10673 {
ef2a22d0
RS
10674 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10675 One XNOOP in 100 loops will make Emacs terminate.
10676 B. Bretthauer, 1994 */
10677 x_noop_count++;
58769bee 10678 if (x_noop_count >= 100)
ef2a22d0
RS
10679 {
10680 x_noop_count=0;
2224b905
RS
10681
10682 if (next_noop_dpyinfo == 0)
10683 next_noop_dpyinfo = x_display_list;
10684
10685 XNoOp (next_noop_dpyinfo->display);
10686
10687 /* Each time we get here, cycle through the displays now open. */
10688 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10689 }
10690 }
502add23 10691
06a2c219 10692 /* If the focus was just given to an auto-raising frame,
0134a210 10693 raise it now. */
7a13e894 10694 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10695 if (pending_autoraise_frame)
10696 {
10697 x_raise_frame (pending_autoraise_frame);
10698 pending_autoraise_frame = 0;
10699 }
0134a210 10700
dc6f92b8 10701 UNBLOCK_INPUT;
bde5503b 10702 --handling_signal;
dc6f92b8
JB
10703 return count;
10704}
06a2c219
GM
10705
10706
10707
dc6f92b8 10708\f
06a2c219
GM
10709/***********************************************************************
10710 Text Cursor
10711 ***********************************************************************/
10712
10713/* Note if the text cursor of window W has been overwritten by a
10714 drawing operation that outputs N glyphs starting at HPOS in the
10715 line given by output_cursor.vpos. N < 0 means all the rest of the
10716 line after HPOS has been written. */
10717
10718static void
10719note_overwritten_text_cursor (w, hpos, n)
10720 struct window *w;
10721 int hpos, n;
10722{
10723 if (updated_area == TEXT_AREA
10724 && output_cursor.vpos == w->phys_cursor.vpos
10725 && hpos <= w->phys_cursor.hpos
10726 && (n < 0
10727 || hpos + n > w->phys_cursor.hpos))
10728 w->phys_cursor_on_p = 0;
10729}
f451eb13
JB
10730
10731
06a2c219
GM
10732/* Set clipping for output in glyph row ROW. W is the window in which
10733 we operate. GC is the graphics context to set clipping in.
10734 WHOLE_LINE_P non-zero means include the areas used for truncation
10735 mark display and alike in the clipping rectangle.
10736
10737 ROW may be a text row or, e.g., a mode line. Text rows must be
10738 clipped to the interior of the window dedicated to text display,
10739 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10740
10741static void
06a2c219
GM
10742x_clip_to_row (w, row, gc, whole_line_p)
10743 struct window *w;
10744 struct glyph_row *row;
10745 GC gc;
10746 int whole_line_p;
dc6f92b8 10747{
06a2c219
GM
10748 struct frame *f = XFRAME (WINDOW_FRAME (w));
10749 XRectangle clip_rect;
10750 int window_x, window_y, window_width, window_height;
dc6f92b8 10751
06a2c219 10752 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10753
06a2c219
GM
10754 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10755 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10756 clip_rect.y = max (clip_rect.y, window_y);
10757 clip_rect.width = window_width;
10758 clip_rect.height = row->visible_height;
5c1aae96 10759
06a2c219
GM
10760 /* If clipping to the whole line, including trunc marks, extend
10761 the rectangle to the left and increase its width. */
10762 if (whole_line_p)
10763 {
110859fc
GM
10764 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10765 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10766 }
5c1aae96 10767
06a2c219 10768 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10769}
10770
06a2c219
GM
10771
10772/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10773
10774static void
06a2c219
GM
10775x_draw_hollow_cursor (w, row)
10776 struct window *w;
10777 struct glyph_row *row;
dc6f92b8 10778{
06a2c219
GM
10779 struct frame *f = XFRAME (WINDOW_FRAME (w));
10780 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10781 Display *dpy = FRAME_X_DISPLAY (f);
10782 int x, y, wd, h;
10783 XGCValues xgcv;
10784 struct glyph *cursor_glyph;
10785 GC gc;
10786
10787 /* Compute frame-relative coordinates from window-relative
10788 coordinates. */
10789 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10790 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10791 + row->ascent - w->phys_cursor_ascent);
10792 h = row->height - 1;
10793
10794 /* Get the glyph the cursor is on. If we can't tell because
10795 the current matrix is invalid or such, give up. */
10796 cursor_glyph = get_phys_cursor_glyph (w);
10797 if (cursor_glyph == NULL)
dc6f92b8
JB
10798 return;
10799
06a2c219
GM
10800 /* Compute the width of the rectangle to draw. If on a stretch
10801 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10802 rectangle as wide as the glyph, but use a canonical character
10803 width instead. */
10804 wd = cursor_glyph->pixel_width - 1;
10805 if (cursor_glyph->type == STRETCH_GLYPH
10806 && !x_stretch_cursor_p)
10807 wd = min (CANON_X_UNIT (f), wd);
10808
10809 /* The foreground of cursor_gc is typically the same as the normal
10810 background color, which can cause the cursor box to be invisible. */
10811 xgcv.foreground = f->output_data.x->cursor_pixel;
10812 if (dpyinfo->scratch_cursor_gc)
10813 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10814 else
10815 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10816 GCForeground, &xgcv);
10817 gc = dpyinfo->scratch_cursor_gc;
10818
10819 /* Set clipping, draw the rectangle, and reset clipping again. */
10820 x_clip_to_row (w, row, gc, 0);
10821 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10822 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10823}
10824
06a2c219
GM
10825
10826/* Draw a bar cursor on window W in glyph row ROW.
10827
10828 Implementation note: One would like to draw a bar cursor with an
10829 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10830 Unfortunately, I didn't find a font yet that has this property set.
10831 --gerd. */
dc6f92b8
JB
10832
10833static void
f02d8aa0 10834x_draw_bar_cursor (w, row, width)
06a2c219
GM
10835 struct window *w;
10836 struct glyph_row *row;
f02d8aa0 10837 int width;
dc6f92b8 10838{
92f424df
GM
10839 struct frame *f = XFRAME (w->frame);
10840 struct glyph *cursor_glyph;
10841 GC gc;
10842 int x;
10843 unsigned long mask;
10844 XGCValues xgcv;
10845 Display *dpy;
10846 Window window;
06a2c219 10847
92f424df
GM
10848 /* If cursor is out of bounds, don't draw garbage. This can happen
10849 in mini-buffer windows when switching between echo area glyphs
10850 and mini-buffer. */
10851 cursor_glyph = get_phys_cursor_glyph (w);
10852 if (cursor_glyph == NULL)
10853 return;
06a2c219 10854
92f424df
GM
10855 /* If on an image, draw like a normal cursor. That's usually better
10856 visible than drawing a bar, esp. if the image is large so that
10857 the bar might not be in the window. */
10858 if (cursor_glyph->type == IMAGE_GLYPH)
10859 {
10860 struct glyph_row *row;
10861 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10862 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10863 }
10864 else
10865 {
06a2c219
GM
10866 xgcv.background = f->output_data.x->cursor_pixel;
10867 xgcv.foreground = f->output_data.x->cursor_pixel;
10868 xgcv.graphics_exposures = 0;
10869 mask = GCForeground | GCBackground | GCGraphicsExposures;
10870 dpy = FRAME_X_DISPLAY (f);
10871 window = FRAME_X_WINDOW (f);
10872 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10873
06a2c219
GM
10874 if (gc)
10875 XChangeGC (dpy, gc, mask, &xgcv);
10876 else
10877 {
10878 gc = XCreateGC (dpy, window, mask, &xgcv);
10879 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10880 }
92f424df 10881
f02d8aa0
GM
10882 if (width < 0)
10883 width = f->output_data.x->cursor_width;
92f424df 10884
06a2c219
GM
10885 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10886 x_clip_to_row (w, row, gc, 0);
10887 XFillRectangle (dpy, window, gc,
10888 x,
10889 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10890 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10891 row->height);
10892 XSetClipMask (dpy, gc, None);
10893 }
dc6f92b8
JB
10894}
10895
06a2c219
GM
10896
10897/* Clear the cursor of window W to background color, and mark the
10898 cursor as not shown. This is used when the text where the cursor
10899 is is about to be rewritten. */
10900
dc6f92b8 10901static void
06a2c219
GM
10902x_clear_cursor (w)
10903 struct window *w;
dc6f92b8 10904{
06a2c219
GM
10905 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10906 x_update_window_cursor (w, 0);
10907}
90e65f07 10908
dbc4e1c1 10909
06a2c219
GM
10910/* Draw the cursor glyph of window W in glyph row ROW. See the
10911 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10912
06a2c219
GM
10913static void
10914x_draw_phys_cursor_glyph (w, row, hl)
10915 struct window *w;
10916 struct glyph_row *row;
10917 enum draw_glyphs_face hl;
10918{
10919 /* If cursor hpos is out of bounds, don't draw garbage. This can
10920 happen in mini-buffer windows when switching between echo area
10921 glyphs and mini-buffer. */
10922 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10923 {
10924 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10925 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10926 hl, 0, 0, 0);
10927
10928 /* When we erase the cursor, and ROW is overlapped by other
10929 rows, make sure that these overlapping parts of other rows
10930 are redrawn. */
10931 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10932 {
10933 if (row > w->current_matrix->rows
10934 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10935 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10936
10937 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10938 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10939 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10940 }
10941 }
06a2c219 10942}
dbc4e1c1 10943
eea6af04 10944
06a2c219 10945/* Erase the image of a cursor of window W from the screen. */
eea6af04 10946
06a2c219
GM
10947static void
10948x_erase_phys_cursor (w)
10949 struct window *w;
10950{
10951 struct frame *f = XFRAME (w->frame);
10952 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10953 int hpos = w->phys_cursor.hpos;
10954 int vpos = w->phys_cursor.vpos;
10955 int mouse_face_here_p = 0;
10956 struct glyph_matrix *active_glyphs = w->current_matrix;
10957 struct glyph_row *cursor_row;
10958 struct glyph *cursor_glyph;
10959 enum draw_glyphs_face hl;
10960
10961 /* No cursor displayed or row invalidated => nothing to do on the
10962 screen. */
10963 if (w->phys_cursor_type == NO_CURSOR)
10964 goto mark_cursor_off;
10965
10966 /* VPOS >= active_glyphs->nrows means that window has been resized.
10967 Don't bother to erase the cursor. */
10968 if (vpos >= active_glyphs->nrows)
10969 goto mark_cursor_off;
10970
10971 /* If row containing cursor is marked invalid, there is nothing we
10972 can do. */
10973 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10974 if (!cursor_row->enabled_p)
10975 goto mark_cursor_off;
10976
10977 /* This can happen when the new row is shorter than the old one.
10978 In this case, either x_draw_glyphs or clear_end_of_line
10979 should have cleared the cursor. Note that we wouldn't be
10980 able to erase the cursor in this case because we don't have a
10981 cursor glyph at hand. */
10982 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10983 goto mark_cursor_off;
10984
10985 /* If the cursor is in the mouse face area, redisplay that when
10986 we clear the cursor. */
8801a864
KR
10987 if (! NILP (dpyinfo->mouse_face_window)
10988 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10989 && (vpos > dpyinfo->mouse_face_beg_row
10990 || (vpos == dpyinfo->mouse_face_beg_row
10991 && hpos >= dpyinfo->mouse_face_beg_col))
10992 && (vpos < dpyinfo->mouse_face_end_row
10993 || (vpos == dpyinfo->mouse_face_end_row
10994 && hpos < dpyinfo->mouse_face_end_col))
10995 /* Don't redraw the cursor's spot in mouse face if it is at the
10996 end of a line (on a newline). The cursor appears there, but
10997 mouse highlighting does not. */
10998 && cursor_row->used[TEXT_AREA] > hpos)
10999 mouse_face_here_p = 1;
11000
11001 /* Maybe clear the display under the cursor. */
11002 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11003 {
11004 int x;
045dee35 11005 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 11006
06a2c219
GM
11007 cursor_glyph = get_phys_cursor_glyph (w);
11008 if (cursor_glyph == NULL)
11009 goto mark_cursor_off;
dbc4e1c1 11010
06a2c219
GM
11011 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11012
c5e6e06b
GM
11013 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11014 x,
11015 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11016 cursor_row->y)),
11017 cursor_glyph->pixel_width,
11018 cursor_row->visible_height,
11019 False);
dbc4e1c1 11020 }
06a2c219
GM
11021
11022 /* Erase the cursor by redrawing the character underneath it. */
11023 if (mouse_face_here_p)
11024 hl = DRAW_MOUSE_FACE;
11025 else if (cursor_row->inverse_p)
11026 hl = DRAW_INVERSE_VIDEO;
11027 else
11028 hl = DRAW_NORMAL_TEXT;
11029 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 11030
06a2c219
GM
11031 mark_cursor_off:
11032 w->phys_cursor_on_p = 0;
11033 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
11034}
11035
11036
06a2c219
GM
11037/* Display or clear cursor of window W. If ON is zero, clear the
11038 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11039 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 11040
06a2c219
GM
11041void
11042x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11043 struct window *w;
11044 int on, hpos, vpos, x, y;
dbc4e1c1 11045{
06a2c219
GM
11046 struct frame *f = XFRAME (w->frame);
11047 int new_cursor_type;
f02d8aa0 11048 int new_cursor_width;
06a2c219
GM
11049 struct glyph_matrix *current_glyphs;
11050 struct glyph_row *glyph_row;
11051 struct glyph *glyph;
dbc4e1c1 11052
49d838ea 11053 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
11054 windows and frames; in the latter case, the frame or window may
11055 be in the midst of changing its size, and x and y may be off the
11056 window. */
11057 if (! FRAME_VISIBLE_P (f)
11058 || FRAME_GARBAGED_P (f)
11059 || vpos >= w->current_matrix->nrows
11060 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
11061 return;
11062
11063 /* If cursor is off and we want it off, return quickly. */
06a2c219 11064 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
11065 return;
11066
06a2c219
GM
11067 current_glyphs = w->current_matrix;
11068 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11069 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11070
11071 /* If cursor row is not enabled, we don't really know where to
11072 display the cursor. */
11073 if (!glyph_row->enabled_p)
11074 {
11075 w->phys_cursor_on_p = 0;
11076 return;
11077 }
11078
11079 xassert (interrupt_input_blocked);
11080
11081 /* Set new_cursor_type to the cursor we want to be displayed. In a
11082 mini-buffer window, we want the cursor only to appear if we are
11083 reading input from this window. For the selected window, we want
11084 the cursor type given by the frame parameter. If explicitly
11085 marked off, draw no cursor. In all other cases, we want a hollow
11086 box cursor. */
f02d8aa0 11087 new_cursor_width = -1;
9b4a7047
GM
11088 if (cursor_in_echo_area
11089 && FRAME_HAS_MINIBUF_P (f)
11090 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 11091 {
9b4a7047
GM
11092 if (w == XWINDOW (echo_area_window))
11093 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
11094 else
11095 new_cursor_type = HOLLOW_BOX_CURSOR;
11096 }
06a2c219 11097 else
9b4a7047 11098 {
7a58ab59
GM
11099 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11100 || w != XWINDOW (f->selected_window))
9b4a7047 11101 {
e55a0b79
GM
11102 extern int cursor_in_non_selected_windows;
11103
5cefa566
GM
11104 if (MINI_WINDOW_P (w)
11105 || !cursor_in_non_selected_windows
11106 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
11107 new_cursor_type = NO_CURSOR;
11108 else
11109 new_cursor_type = HOLLOW_BOX_CURSOR;
11110 }
11111 else if (w->cursor_off_p)
11112 new_cursor_type = NO_CURSOR;
11113 else
f02d8aa0
GM
11114 {
11115 struct buffer *b = XBUFFER (w->buffer);
11116
11117 if (EQ (b->cursor_type, Qt))
11118 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11119 else
11120 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11121 &new_cursor_width);
11122 }
9b4a7047 11123 }
06a2c219
GM
11124
11125 /* If cursor is currently being shown and we don't want it to be or
11126 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 11127 erase it. */
06a2c219 11128 if (w->phys_cursor_on_p
dc6f92b8 11129 && (!on
06a2c219
GM
11130 || w->phys_cursor.x != x
11131 || w->phys_cursor.y != y
11132 || new_cursor_type != w->phys_cursor_type))
11133 x_erase_phys_cursor (w);
11134
11135 /* If the cursor is now invisible and we want it to be visible,
11136 display it. */
11137 if (on && !w->phys_cursor_on_p)
11138 {
11139 w->phys_cursor_ascent = glyph_row->ascent;
11140 w->phys_cursor_height = glyph_row->height;
11141
11142 /* Set phys_cursor_.* before x_draw_.* is called because some
11143 of them may need the information. */
11144 w->phys_cursor.x = x;
11145 w->phys_cursor.y = glyph_row->y;
11146 w->phys_cursor.hpos = hpos;
11147 w->phys_cursor.vpos = vpos;
11148 w->phys_cursor_type = new_cursor_type;
11149 w->phys_cursor_on_p = 1;
11150
11151 switch (new_cursor_type)
dc6f92b8 11152 {
06a2c219
GM
11153 case HOLLOW_BOX_CURSOR:
11154 x_draw_hollow_cursor (w, glyph_row);
11155 break;
11156
11157 case FILLED_BOX_CURSOR:
11158 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11159 break;
11160
11161 case BAR_CURSOR:
f02d8aa0 11162 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
11163 break;
11164
11165 case NO_CURSOR:
11166 break;
dc6f92b8 11167
06a2c219
GM
11168 default:
11169 abort ();
11170 }
59ddecde
GM
11171
11172#ifdef HAVE_X_I18N
11173 if (w == XWINDOW (f->selected_window))
11174 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11175 xic_set_preeditarea (w, x, y);
11176#endif
dc6f92b8
JB
11177 }
11178
06a2c219 11179#ifndef XFlush
f676886a 11180 if (updating_frame != f)
334208b7 11181 XFlush (FRAME_X_DISPLAY (f));
06a2c219 11182#endif
dc6f92b8
JB
11183}
11184
06a2c219
GM
11185
11186/* Display the cursor on window W, or clear it. X and Y are window
11187 relative pixel coordinates. HPOS and VPOS are glyph matrix
11188 positions. If W is not the selected window, display a hollow
11189 cursor. ON non-zero means display the cursor at X, Y which
11190 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11191
dfcf069d 11192void
06a2c219
GM
11193x_display_cursor (w, on, hpos, vpos, x, y)
11194 struct window *w;
11195 int on, hpos, vpos, x, y;
dc6f92b8 11196{
f94397b5 11197 BLOCK_INPUT;
06a2c219 11198 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11199 UNBLOCK_INPUT;
11200}
11201
06a2c219
GM
11202
11203/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11204 Don't change the cursor's position. */
11205
dfcf069d 11206void
06a2c219 11207x_update_cursor (f, on_p)
5d46f928 11208 struct frame *f;
5d46f928 11209{
06a2c219
GM
11210 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11211}
11212
11213
11214/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11215 in the window tree rooted at W. */
11216
11217static void
11218x_update_cursor_in_window_tree (w, on_p)
11219 struct window *w;
11220 int on_p;
11221{
11222 while (w)
11223 {
11224 if (!NILP (w->hchild))
11225 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11226 else if (!NILP (w->vchild))
11227 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11228 else
11229 x_update_window_cursor (w, on_p);
11230
11231 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11232 }
11233}
5d46f928 11234
f94397b5 11235
06a2c219
GM
11236/* Switch the display of W's cursor on or off, according to the value
11237 of ON. */
11238
11239static void
11240x_update_window_cursor (w, on)
11241 struct window *w;
11242 int on;
11243{
16b5d424
GM
11244 /* Don't update cursor in windows whose frame is in the process
11245 of being deleted. */
11246 if (w->current_matrix)
11247 {
11248 BLOCK_INPUT;
11249 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11250 w->phys_cursor.x, w->phys_cursor.y);
11251 UNBLOCK_INPUT;
11252 }
dc6f92b8 11253}
06a2c219
GM
11254
11255
11256
dc6f92b8
JB
11257\f
11258/* Icons. */
11259
f676886a 11260/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11261 when we get an expose event for it. */
dc6f92b8 11262
dfcf069d 11263void
f676886a
JB
11264refreshicon (f)
11265 struct frame *f;
dc6f92b8 11266{
06a2c219 11267 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11268}
11269
dbc4e1c1 11270/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11271
11272int
990ba854 11273x_bitmap_icon (f, file)
f676886a 11274 struct frame *f;
990ba854 11275 Lisp_Object file;
dc6f92b8 11276{
06a2c219 11277 int bitmap_id;
dc6f92b8 11278
c118dd06 11279 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11280 return 1;
11281
990ba854 11282 /* Free up our existing icon bitmap if any. */
7556890b
RS
11283 if (f->output_data.x->icon_bitmap > 0)
11284 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11285 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11286
11287 if (STRINGP (file))
7f2ae036
RS
11288 bitmap_id = x_create_bitmap_from_file (f, file);
11289 else
11290 {
990ba854 11291 /* Create the GNU bitmap if necessary. */
5bf01b68 11292 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11293 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11294 = x_create_bitmap_from_data (f, gnu_bits,
11295 gnu_width, gnu_height);
990ba854
RS
11296
11297 /* The first time we create the GNU bitmap,
06a2c219 11298 this increments the ref-count one extra time.
990ba854
RS
11299 As a result, the GNU bitmap is never freed.
11300 That way, we don't have to worry about allocating it again. */
334208b7 11301 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11302
334208b7 11303 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11304 }
11305
11306 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11307 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11308
11309 return 0;
11310}
11311
11312
1be2d067
KH
11313/* Make the x-window of frame F use a rectangle with text.
11314 Use ICON_NAME as the text. */
dc6f92b8
JB
11315
11316int
f676886a
JB
11317x_text_icon (f, icon_name)
11318 struct frame *f;
dc6f92b8
JB
11319 char *icon_name;
11320{
c118dd06 11321 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11322 return 1;
11323
1be2d067
KH
11324#ifdef HAVE_X11R4
11325 {
11326 XTextProperty text;
11327 text.value = (unsigned char *) icon_name;
11328 text.encoding = XA_STRING;
11329 text.format = 8;
11330 text.nitems = strlen (icon_name);
11331#ifdef USE_X_TOOLKIT
7556890b 11332 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11333 &text);
11334#else /* not USE_X_TOOLKIT */
11335 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11336#endif /* not USE_X_TOOLKIT */
11337 }
11338#else /* not HAVE_X11R4 */
11339 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11340#endif /* not HAVE_X11R4 */
58769bee 11341
7556890b
RS
11342 if (f->output_data.x->icon_bitmap > 0)
11343 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11344 f->output_data.x->icon_bitmap = 0;
b1c884c3 11345 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11346
11347 return 0;
11348}
11349\f
e99db5a1
RS
11350#define X_ERROR_MESSAGE_SIZE 200
11351
11352/* If non-nil, this should be a string.
11353 It means catch X errors and store the error message in this string. */
11354
11355static Lisp_Object x_error_message_string;
11356
11357/* An X error handler which stores the error message in
11358 x_error_message_string. This is called from x_error_handler if
11359 x_catch_errors is in effect. */
11360
06a2c219 11361static void
e99db5a1
RS
11362x_error_catcher (display, error)
11363 Display *display;
11364 XErrorEvent *error;
11365{
11366 XGetErrorText (display, error->error_code,
11367 XSTRING (x_error_message_string)->data,
11368 X_ERROR_MESSAGE_SIZE);
11369}
11370
11371/* Begin trapping X errors for display DPY. Actually we trap X errors
11372 for all displays, but DPY should be the display you are actually
11373 operating on.
11374
11375 After calling this function, X protocol errors no longer cause
11376 Emacs to exit; instead, they are recorded in the string
11377 stored in x_error_message_string.
11378
11379 Calling x_check_errors signals an Emacs error if an X error has
11380 occurred since the last call to x_catch_errors or x_check_errors.
11381
11382 Calling x_uncatch_errors resumes the normal error handling. */
11383
11384void x_check_errors ();
11385static Lisp_Object x_catch_errors_unwind ();
11386
11387int
11388x_catch_errors (dpy)
11389 Display *dpy;
11390{
11391 int count = specpdl_ptr - specpdl;
11392
11393 /* Make sure any errors from previous requests have been dealt with. */
11394 XSync (dpy, False);
11395
11396 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11397
11398 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11399 XSTRING (x_error_message_string)->data[0] = 0;
11400
11401 return count;
11402}
11403
11404/* Unbind the binding that we made to check for X errors. */
11405
11406static Lisp_Object
11407x_catch_errors_unwind (old_val)
11408 Lisp_Object old_val;
11409{
11410 x_error_message_string = old_val;
11411 return Qnil;
11412}
11413
11414/* If any X protocol errors have arrived since the last call to
11415 x_catch_errors or x_check_errors, signal an Emacs error using
11416 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11417
11418void
11419x_check_errors (dpy, format)
11420 Display *dpy;
11421 char *format;
11422{
11423 /* Make sure to catch any errors incurred so far. */
11424 XSync (dpy, False);
11425
11426 if (XSTRING (x_error_message_string)->data[0])
11427 error (format, XSTRING (x_error_message_string)->data);
11428}
11429
9829ddba
RS
11430/* Nonzero if we had any X protocol errors
11431 since we did x_catch_errors on DPY. */
e99db5a1
RS
11432
11433int
11434x_had_errors_p (dpy)
11435 Display *dpy;
11436{
11437 /* Make sure to catch any errors incurred so far. */
11438 XSync (dpy, False);
11439
11440 return XSTRING (x_error_message_string)->data[0] != 0;
11441}
11442
9829ddba
RS
11443/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11444
06a2c219 11445void
9829ddba
RS
11446x_clear_errors (dpy)
11447 Display *dpy;
11448{
11449 XSTRING (x_error_message_string)->data[0] = 0;
11450}
11451
e99db5a1
RS
11452/* Stop catching X protocol errors and let them make Emacs die.
11453 DPY should be the display that was passed to x_catch_errors.
11454 COUNT should be the value that was returned by
11455 the corresponding call to x_catch_errors. */
11456
11457void
11458x_uncatch_errors (dpy, count)
11459 Display *dpy;
11460 int count;
11461{
11462 unbind_to (count, Qnil);
11463}
11464
11465#if 0
11466static unsigned int x_wire_count;
11467x_trace_wire ()
11468{
11469 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11470}
11471#endif /* ! 0 */
11472
11473\f
11474/* Handle SIGPIPE, which can happen when the connection to a server
11475 simply goes away. SIGPIPE is handled by x_connection_signal.
11476 Don't need to do anything, because the write which caused the
11477 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11478 which will do the appropriate cleanup for us. */
e99db5a1
RS
11479
11480static SIGTYPE
11481x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11482 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11483{
11484#ifdef USG
11485 /* USG systems forget handlers when they are used;
11486 must reestablish each time */
11487 signal (signalnum, x_connection_signal);
11488#endif /* USG */
11489}
0da1ab50 11490
e99db5a1 11491\f
0da1ab50
GM
11492/************************************************************************
11493 Handling X errors
11494 ************************************************************************/
4746118a 11495
0da1ab50
GM
11496/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11497 the text of an error message that lead to the connection loss. */
16bd92ea 11498
4746118a 11499static SIGTYPE
5978125e
GM
11500x_connection_closed (dpy, error_message)
11501 Display *dpy;
7a13e894 11502 char *error_message;
4746118a 11503{
5978125e 11504 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 11505 Lisp_Object frame, tail;
0da1ab50
GM
11506 int count;
11507 char *msg;
11508
11509 msg = (char *) alloca (strlen (error_message) + 1);
11510 strcpy (msg, error_message);
1a532e54
GM
11511 handling_signal = 0;
11512
0da1ab50
GM
11513 /* Prevent being called recursively because of an error condition
11514 below. Otherwise, we might end up with printing ``can't find per
11515 display information'' in the recursive call instead of printing
11516 the original message here. */
11517 count = x_catch_errors (dpy);
11518
8a4f36cc
GM
11519 /* We have to close the display to inform Xt that it doesn't
11520 exist anymore. If we don't, Xt will continue to wait for
11521 events from the display. As a consequence, a sequence of
11522
11523 M-x make-frame-on-display RET :1 RET
11524 ...kill the new frame, so that we get an IO error...
11525 M-x make-frame-on-display RET :1 RET
11526
11527 will indefinitely wait in Xt for events for display `:1', opened
11528 in the first class to make-frame-on-display.
6186a4a0 11529
8a4f36cc
GM
11530 Closing the display is reported to lead to a bus error on
11531 OpenWindows in certain situations. I suspect that is a bug
11532 in OpenWindows. I don't know how to cicumvent it here. */
11533
f613a4c8 11534#ifdef USE_X_TOOLKIT
ae24cb3b
GM
11535 /* If DPYINFO is null, this means we didn't open the display
11536 in the first place, so don't try to close it. */
11537 if (dpyinfo)
11538 XtCloseDisplay (dpy);
f613a4c8 11539#endif
adabc3a9 11540
8a4f36cc 11541 /* Indicate that this display is dead. */
9e80b57d
KR
11542 if (dpyinfo)
11543 dpyinfo->display = 0;
6186a4a0 11544
06a2c219 11545 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11546 that are on the dead display. */
11547 FOR_EACH_FRAME (tail, frame)
11548 {
11549 Lisp_Object minibuf_frame;
11550 minibuf_frame
11551 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11552 if (FRAME_X_P (XFRAME (frame))
11553 && FRAME_X_P (XFRAME (minibuf_frame))
11554 && ! EQ (frame, minibuf_frame)
11555 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11556 Fdelete_frame (frame, Qt);
11557 }
11558
11559 /* Now delete all remaining frames on the dead display.
06a2c219 11560 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11561 for another frame that we need to delete. */
11562 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11563 if (FRAME_X_P (XFRAME (frame))
11564 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11565 {
11566 /* Set this to t so that Fdelete_frame won't get confused
11567 trying to find a replacement. */
11568 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11569 Fdelete_frame (frame, Qt);
11570 }
7a13e894 11571
482a1bd2
KH
11572 if (dpyinfo)
11573 x_delete_display (dpyinfo);
7a13e894 11574
0da1ab50
GM
11575 x_uncatch_errors (dpy, count);
11576
7a13e894
RS
11577 if (x_display_list == 0)
11578 {
0da1ab50 11579 fprintf (stderr, "%s\n", msg);
7a13e894
RS
11580 shut_down_emacs (0, 0, Qnil);
11581 exit (70);
11582 }
12ba150f 11583
7a13e894
RS
11584 /* Ordinary stack unwind doesn't deal with these. */
11585#ifdef SIGIO
11586 sigunblock (sigmask (SIGIO));
11587#endif
11588 sigunblock (sigmask (SIGALRM));
11589 TOTALLY_UNBLOCK_INPUT;
11590
aa4d9a9e 11591 clear_waiting_for_input ();
0da1ab50 11592 error ("%s", msg);
4746118a
JB
11593}
11594
0da1ab50 11595
7a13e894
RS
11596/* This is the usual handler for X protocol errors.
11597 It kills all frames on the display that we got the error for.
11598 If that was the only one, it prints an error message and kills Emacs. */
11599
06a2c219 11600static void
c118dd06
JB
11601x_error_quitter (display, error)
11602 Display *display;
11603 XErrorEvent *error;
11604{
7a13e894 11605 char buf[256], buf1[356];
dc6f92b8 11606
58769bee 11607 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11608 original error handler. */
dc6f92b8 11609
c118dd06 11610 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11611 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11612 buf, error->request_code);
7a13e894 11613 x_connection_closed (display, buf1);
dc6f92b8
JB
11614}
11615
0da1ab50 11616
e99db5a1
RS
11617/* This is the first-level handler for X protocol errors.
11618 It calls x_error_quitter or x_error_catcher. */
7a13e894 11619
8922af5f 11620static int
e99db5a1 11621x_error_handler (display, error)
8922af5f 11622 Display *display;
e99db5a1 11623 XErrorEvent *error;
8922af5f 11624{
e99db5a1
RS
11625 if (! NILP (x_error_message_string))
11626 x_error_catcher (display, error);
11627 else
11628 x_error_quitter (display, error);
06a2c219 11629 return 0;
f9e24cb9 11630}
c118dd06 11631
e99db5a1
RS
11632/* This is the handler for X IO errors, always.
11633 It kills all frames on the display that we lost touch with.
11634 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11635
c118dd06 11636static int
e99db5a1 11637x_io_error_quitter (display)
c118dd06 11638 Display *display;
c118dd06 11639{
e99db5a1 11640 char buf[256];
dc6f92b8 11641
e99db5a1
RS
11642 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11643 x_connection_closed (display, buf);
06a2c219 11644 return 0;
dc6f92b8 11645}
dc6f92b8 11646\f
f451eb13
JB
11647/* Changing the font of the frame. */
11648
76bcdf39
RS
11649/* Give frame F the font named FONTNAME as its default font, and
11650 return the full name of that font. FONTNAME may be a wildcard
11651 pattern; in that case, we choose some font that fits the pattern.
11652 The return value shows which font we chose. */
11653
b5cf7a0e 11654Lisp_Object
f676886a
JB
11655x_new_font (f, fontname)
11656 struct frame *f;
dc6f92b8
JB
11657 register char *fontname;
11658{
dc43ef94 11659 struct font_info *fontp
ee569018 11660 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11661
dc43ef94
KH
11662 if (!fontp)
11663 return Qnil;
2224a5fc 11664
dc43ef94 11665 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11666 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11667 f->output_data.x->fontset = -1;
11668
b2cad826
KH
11669 /* Compute the scroll bar width in character columns. */
11670 if (f->scroll_bar_pixel_width > 0)
11671 {
7556890b 11672 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11673 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11674 }
11675 else
4e61bddf
RS
11676 {
11677 int wid = FONT_WIDTH (f->output_data.x->font);
11678 f->scroll_bar_cols = (14 + wid - 1) / wid;
11679 }
b2cad826 11680
f676886a 11681 /* Now make the frame display the given font. */
c118dd06 11682 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11683 {
7556890b
RS
11684 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11685 f->output_data.x->font->fid);
11686 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11687 f->output_data.x->font->fid);
11688 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11689 f->output_data.x->font->fid);
f676886a 11690
a27f9f86 11691 frame_update_line_height (f);
3497f73e
GM
11692
11693 /* Don't change the size of a tip frame; there's no point in
11694 doing it because it's done in Fx_show_tip, and it leads to
11695 problems because the tip frame has no widget. */
11696 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11697 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11698 }
a27f9f86
RS
11699 else
11700 /* If we are setting a new frame's font for the first time,
11701 there are no faces yet, so this font's height is the line height. */
7556890b 11702 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11703
dc43ef94
KH
11704 return build_string (fontp->full_name);
11705}
11706
11707/* Give frame F the fontset named FONTSETNAME as its default font, and
11708 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11709 pattern; in that case, we choose some fontset that fits the pattern.
11710 The return value shows which fontset we chose. */
b5cf7a0e 11711
dc43ef94
KH
11712Lisp_Object
11713x_new_fontset (f, fontsetname)
11714 struct frame *f;
11715 char *fontsetname;
11716{
ee569018 11717 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11718 Lisp_Object result;
b5cf7a0e 11719
dc43ef94
KH
11720 if (fontset < 0)
11721 return Qnil;
b5cf7a0e 11722
2da424f1
KH
11723 if (f->output_data.x->fontset == fontset)
11724 /* This fontset is already set in frame F. There's nothing more
11725 to do. */
ee569018 11726 return fontset_name (fontset);
dc43ef94 11727
ee569018 11728 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11729
11730 if (!STRINGP (result))
11731 /* Can't load ASCII font. */
11732 return Qnil;
11733
11734 /* Since x_new_font doesn't update any fontset information, do it now. */
11735 f->output_data.x->fontset = fontset;
dc43ef94 11736
f5d11644
GM
11737#ifdef HAVE_X_I18N
11738 if (FRAME_XIC (f)
11739 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11740 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11741#endif
11742
dc43ef94 11743 return build_string (fontsetname);
dc6f92b8 11744}
f5d11644
GM
11745
11746\f
11747/***********************************************************************
11748 X Input Methods
11749 ***********************************************************************/
11750
11751#ifdef HAVE_X_I18N
11752
11753#ifdef HAVE_X11R6
11754
11755/* XIM destroy callback function, which is called whenever the
11756 connection to input method XIM dies. CLIENT_DATA contains a
11757 pointer to the x_display_info structure corresponding to XIM. */
11758
11759static void
11760xim_destroy_callback (xim, client_data, call_data)
11761 XIM xim;
11762 XPointer client_data;
11763 XPointer call_data;
11764{
11765 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11766 Lisp_Object frame, tail;
11767
11768 BLOCK_INPUT;
11769
11770 /* No need to call XDestroyIC.. */
11771 FOR_EACH_FRAME (tail, frame)
11772 {
11773 struct frame *f = XFRAME (frame);
11774 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11775 {
11776 FRAME_XIC (f) = NULL;
11777 if (FRAME_XIC_FONTSET (f))
11778 {
11779 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11780 FRAME_XIC_FONTSET (f) = NULL;
11781 }
11782 }
11783 }
11784
11785 /* No need to call XCloseIM. */
11786 dpyinfo->xim = NULL;
11787 XFree (dpyinfo->xim_styles);
11788 UNBLOCK_INPUT;
11789}
11790
11791#endif /* HAVE_X11R6 */
11792
11793/* Open the connection to the XIM server on display DPYINFO.
11794 RESOURCE_NAME is the resource name Emacs uses. */
11795
11796static void
11797xim_open_dpy (dpyinfo, resource_name)
11798 struct x_display_info *dpyinfo;
11799 char *resource_name;
11800{
287f7dd6 11801#ifdef USE_XIM
f5d11644
GM
11802 XIM xim;
11803
11804 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11805 dpyinfo->xim = xim;
11806
11807 if (xim)
11808 {
f5d11644
GM
11809#ifdef HAVE_X11R6
11810 XIMCallback destroy;
11811#endif
11812
11813 /* Get supported styles and XIM values. */
11814 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11815
11816#ifdef HAVE_X11R6
11817 destroy.callback = xim_destroy_callback;
11818 destroy.client_data = (XPointer)dpyinfo;
68642df6 11819 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11820 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11821#endif
11822 }
287f7dd6
GM
11823
11824#else /* not USE_XIM */
11825 dpyinfo->xim = NULL;
11826#endif /* not USE_XIM */
f5d11644
GM
11827}
11828
11829
b9de836c 11830#ifdef HAVE_X11R6_XIM
f5d11644
GM
11831
11832struct xim_inst_t
11833{
11834 struct x_display_info *dpyinfo;
11835 char *resource_name;
11836};
11837
11838/* XIM instantiate callback function, which is called whenever an XIM
11839 server is available. DISPLAY is teh display of the XIM.
11840 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11841 when the callback was registered. */
11842
11843static void
11844xim_instantiate_callback (display, client_data, call_data)
11845 Display *display;
11846 XPointer client_data;
11847 XPointer call_data;
11848{
11849 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11850 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11851
11852 /* We don't support multiple XIM connections. */
11853 if (dpyinfo->xim)
11854 return;
11855
11856 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11857
11858 /* Create XIC for the existing frames on the same display, as long
11859 as they have no XIC. */
11860 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11861 {
11862 Lisp_Object tail, frame;
11863
11864 BLOCK_INPUT;
11865 FOR_EACH_FRAME (tail, frame)
11866 {
11867 struct frame *f = XFRAME (frame);
11868
11869 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11870 if (FRAME_XIC (f) == NULL)
11871 {
11872 create_frame_xic (f);
11873 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11874 xic_set_statusarea (f);
11875 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11876 {
11877 struct window *w = XWINDOW (f->selected_window);
11878 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11879 }
11880 }
11881 }
11882
11883 UNBLOCK_INPUT;
11884 }
11885}
11886
b9de836c 11887#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11888
11889
11890/* Open a connection to the XIM server on display DPYINFO.
11891 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11892 connection only at the first time. On X11R6, open the connection
11893 in the XIM instantiate callback function. */
11894
11895static void
11896xim_initialize (dpyinfo, resource_name)
11897 struct x_display_info *dpyinfo;
11898 char *resource_name;
11899{
287f7dd6 11900#ifdef USE_XIM
b9de836c 11901#ifdef HAVE_X11R6_XIM
f5d11644
GM
11902 struct xim_inst_t *xim_inst;
11903 int len;
11904
11905 dpyinfo->xim = NULL;
11906 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11907 xim_inst->dpyinfo = dpyinfo;
11908 len = strlen (resource_name);
11909 xim_inst->resource_name = (char *) xmalloc (len + 1);
11910 bcopy (resource_name, xim_inst->resource_name, len + 1);
11911 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11912 resource_name, EMACS_CLASS,
11913 xim_instantiate_callback,
2ebb2f8b
DL
11914 /* Fixme: This is XPointer in
11915 XFree86 but (XPointer *) on
11916 Tru64, at least. */
11917 (XPointer) xim_inst);
b9de836c 11918#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11919 dpyinfo->xim = NULL;
11920 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11921#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11922
11923#else /* not USE_XIM */
11924 dpyinfo->xim = NULL;
11925#endif /* not USE_XIM */
f5d11644
GM
11926}
11927
11928
11929/* Close the connection to the XIM server on display DPYINFO. */
11930
11931static void
11932xim_close_dpy (dpyinfo)
11933 struct x_display_info *dpyinfo;
11934{
287f7dd6 11935#ifdef USE_XIM
b9de836c 11936#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
11937 if (dpyinfo->display)
11938 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11939 NULL, EMACS_CLASS,
11940 xim_instantiate_callback, NULL);
b9de836c 11941#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
11942 if (dpyinfo->display)
11943 XCloseIM (dpyinfo->xim);
f5d11644
GM
11944 dpyinfo->xim = NULL;
11945 XFree (dpyinfo->xim_styles);
287f7dd6 11946#endif /* USE_XIM */
f5d11644
GM
11947}
11948
b9de836c 11949#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11950
11951
dc6f92b8 11952\f
2e365682
RS
11953/* Calculate the absolute position in frame F
11954 from its current recorded position values and gravity. */
11955
dfcf069d 11956void
43bca5d5 11957x_calc_absolute_position (f)
f676886a 11958 struct frame *f;
dc6f92b8 11959{
06a2c219 11960 Window child;
6dba1858 11961 int win_x = 0, win_y = 0;
7556890b 11962 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11963 int this_window;
11964
9829ddba
RS
11965 /* We have nothing to do if the current position
11966 is already for the top-left corner. */
11967 if (! ((flags & XNegative) || (flags & YNegative)))
11968 return;
11969
c81412a0 11970#ifdef USE_X_TOOLKIT
7556890b 11971 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11972#else
11973 this_window = FRAME_X_WINDOW (f);
11974#endif
6dba1858
RS
11975
11976 /* Find the position of the outside upper-left corner of
9829ddba
RS
11977 the inner window, with respect to the outer window.
11978 But do this only if we will need the results. */
7556890b 11979 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11980 {
9829ddba
RS
11981 int count;
11982
6dba1858 11983 BLOCK_INPUT;
9829ddba
RS
11984 count = x_catch_errors (FRAME_X_DISPLAY (f));
11985 while (1)
11986 {
11987 x_clear_errors (FRAME_X_DISPLAY (f));
11988 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11989
11990 /* From-window, to-window. */
11991 this_window,
11992 f->output_data.x->parent_desc,
11993
11994 /* From-position, to-position. */
11995 0, 0, &win_x, &win_y,
11996
11997 /* Child of win. */
11998 &child);
11999 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12000 {
12001 Window newroot, newparent = 0xdeadbeef;
12002 Window *newchildren;
2ebb2f8b 12003 unsigned int nchildren;
9829ddba
RS
12004
12005 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12006 &newparent, &newchildren, &nchildren))
12007 break;
58769bee 12008
7c3c78a3 12009 XFree ((char *) newchildren);
6dba1858 12010
9829ddba
RS
12011 f->output_data.x->parent_desc = newparent;
12012 }
12013 else
12014 break;
12015 }
6dba1858 12016
9829ddba 12017 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
12018 UNBLOCK_INPUT;
12019 }
12020
12021 /* Treat negative positions as relative to the leftmost bottommost
12022 position that fits on the screen. */
20f55f9a 12023 if (flags & XNegative)
7556890b 12024 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
12025 - 2 * f->output_data.x->border_width - win_x
12026 - PIXEL_WIDTH (f)
12027 + f->output_data.x->left_pos);
dc6f92b8 12028
7708ced0
GM
12029 {
12030 int height = PIXEL_HEIGHT (f);
06a2c219 12031
7708ced0
GM
12032#if defined USE_X_TOOLKIT && defined USE_MOTIF
12033 /* Something is fishy here. When using Motif, starting Emacs with
12034 `-g -0-0', the frame appears too low by a few pixels.
12035
12036 This seems to be so because initially, while Emacs is starting,
12037 the column widget's height and the frame's pixel height are
12038 different. The column widget's height is the right one. In
12039 later invocations, when Emacs is up, the frame's pixel height
12040 is right, though.
12041
12042 It's not obvious where the initial small difference comes from.
12043 2000-12-01, gerd. */
12044
12045 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 12046#endif
2e365682 12047
7708ced0
GM
12048 if (flags & YNegative)
12049 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12050 - 2 * f->output_data.x->border_width
12051 - win_y
12052 - height
12053 + f->output_data.x->top_pos);
12054 }
12055
3a35ab44
RS
12056 /* The left_pos and top_pos
12057 are now relative to the top and left screen edges,
12058 so the flags should correspond. */
7556890b 12059 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
12060}
12061
3a35ab44
RS
12062/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12063 to really change the position, and 0 when calling from
12064 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
12065 position values). It is -1 when calling from x_set_frame_parameters,
12066 which means, do adjust for borders but don't change the gravity. */
3a35ab44 12067
dfcf069d 12068void
dc05a16b 12069x_set_offset (f, xoff, yoff, change_gravity)
f676886a 12070 struct frame *f;
dc6f92b8 12071 register int xoff, yoff;
dc05a16b 12072 int change_gravity;
dc6f92b8 12073{
4a4cbdd5
KH
12074 int modified_top, modified_left;
12075
aa3ff7c9 12076 if (change_gravity > 0)
3a35ab44 12077 {
7556890b
RS
12078 f->output_data.x->top_pos = yoff;
12079 f->output_data.x->left_pos = xoff;
12080 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 12081 if (xoff < 0)
7556890b 12082 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 12083 if (yoff < 0)
7556890b
RS
12084 f->output_data.x->size_hint_flags |= YNegative;
12085 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 12086 }
43bca5d5 12087 x_calc_absolute_position (f);
dc6f92b8
JB
12088
12089 BLOCK_INPUT;
c32cdd9a 12090 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 12091
7556890b
RS
12092 modified_left = f->output_data.x->left_pos;
12093 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
12094#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12095 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12096 /* It is a mystery why we need to add the border_width here
12097 when the frame is already visible, but experiment says we do. */
aa3ff7c9 12098 if (change_gravity != 0)
4a4cbdd5 12099 {
7556890b
RS
12100 modified_left += f->output_data.x->border_width;
12101 modified_top += f->output_data.x->border_width;
4a4cbdd5 12102 }
e73ec6fa 12103#endif
4a4cbdd5 12104
3afe33e7 12105#ifdef USE_X_TOOLKIT
7556890b 12106 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 12107 modified_left, modified_top);
3afe33e7 12108#else /* not USE_X_TOOLKIT */
334208b7 12109 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 12110 modified_left, modified_top);
3afe33e7 12111#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12112 UNBLOCK_INPUT;
12113}
12114
dc6f92b8 12115
499b1844
GM
12116/* Change the size of frame F's X window to COLS/ROWS in the case F
12117 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12118 top-left-corner window gravity for this size change and subsequent
12119 size changes. Otherwise we leave the window gravity unchanged. */
12120
12121static void
12122x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 12123 struct frame *f;
bc20ebbf 12124 int change_gravity;
b1c884c3 12125 int cols, rows;
dc6f92b8
JB
12126{
12127 int pixelwidth, pixelheight;
80fd1fe2 12128
b1c884c3 12129 check_frame_size (f, &rows, &cols);
7556890b 12130 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
12131 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12132 ? 0
12133 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 12134 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 12135 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 12136 f->output_data.x->flags_areas_extra
110859fc 12137 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
12138 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12139 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 12140
7556890b 12141 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 12142 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 12143
334208b7
RS
12144 XSync (FRAME_X_DISPLAY (f), False);
12145 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12146 pixelwidth, pixelheight);
b1c884c3
JB
12147
12148 /* Now, strictly speaking, we can't be sure that this is accurate,
12149 but the window manager will get around to dealing with the size
12150 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
12151 ConfigureNotify event gets here.
12152
12153 We could just not bother storing any of this information here,
12154 and let the ConfigureNotify event set everything up, but that
fddd5ceb 12155 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 12156 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
12157 point in the future when the ConfigureNotify event arrives.
12158
12159 We pass 1 for DELAY since we can't run Lisp code inside of
12160 a BLOCK_INPUT. */
7d1e984f 12161 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
12162 PIXEL_WIDTH (f) = pixelwidth;
12163 PIXEL_HEIGHT (f) = pixelheight;
12164
aee9a898
RS
12165 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12166 receive in the ConfigureNotify event; if we get what we asked
12167 for, then the event won't cause the screen to become garbaged, so
12168 we have to make sure to do it here. */
12169 SET_FRAME_GARBAGED (f);
12170
12171 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
12172}
12173
12174
12175/* Call this to change the size of frame F's x-window.
12176 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12177 for this size change and subsequent size changes.
12178 Otherwise we leave the window gravity unchanged. */
aee9a898 12179
499b1844
GM
12180void
12181x_set_window_size (f, change_gravity, cols, rows)
12182 struct frame *f;
12183 int change_gravity;
12184 int cols, rows;
12185{
12186 BLOCK_INPUT;
12187
12188#ifdef USE_X_TOOLKIT
12189
f1f4d345 12190 if (f->output_data.x->widget != NULL)
499b1844
GM
12191 {
12192 /* The x and y position of the widget is clobbered by the
12193 call to XtSetValues within EmacsFrameSetCharSize.
12194 This is a real kludge, but I don't understand Xt so I can't
12195 figure out a correct fix. Can anyone else tell me? -- rms. */
12196 int xpos = f->output_data.x->widget->core.x;
12197 int ypos = f->output_data.x->widget->core.y;
12198 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12199 f->output_data.x->widget->core.x = xpos;
12200 f->output_data.x->widget->core.y = ypos;
12201 }
12202 else
12203 x_set_window_size_1 (f, change_gravity, cols, rows);
12204
12205#else /* not USE_X_TOOLKIT */
12206
12207 x_set_window_size_1 (f, change_gravity, cols, rows);
12208
aee9a898
RS
12209#endif /* not USE_X_TOOLKIT */
12210
4d73d038 12211 /* If cursor was outside the new size, mark it as off. */
06a2c219 12212 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 12213
aee9a898
RS
12214 /* Clear out any recollection of where the mouse highlighting was,
12215 since it might be in a place that's outside the new frame size.
12216 Actually checking whether it is outside is a pain in the neck,
12217 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 12218 cancel_mouse_face (f);
dbc4e1c1 12219
dc6f92b8
JB
12220 UNBLOCK_INPUT;
12221}
dc6f92b8 12222\f
d047c4eb 12223/* Mouse warping. */
dc6f92b8 12224
9b378208 12225void
f676886a
JB
12226x_set_mouse_position (f, x, y)
12227 struct frame *f;
dc6f92b8
JB
12228 int x, y;
12229{
12230 int pix_x, pix_y;
12231
7556890b
RS
12232 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12233 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
12234
12235 if (pix_x < 0) pix_x = 0;
12236 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12237
12238 if (pix_y < 0) pix_y = 0;
12239 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
12240
12241 BLOCK_INPUT;
dc6f92b8 12242
334208b7
RS
12243 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12244 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
12245 UNBLOCK_INPUT;
12246}
12247
9b378208
RS
12248/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12249
12250void
12251x_set_mouse_pixel_position (f, pix_x, pix_y)
12252 struct frame *f;
12253 int pix_x, pix_y;
12254{
12255 BLOCK_INPUT;
12256
334208b7
RS
12257 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12258 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
12259 UNBLOCK_INPUT;
12260}
d047c4eb
KH
12261\f
12262/* focus shifting, raising and lowering. */
9b378208 12263
dfcf069d 12264void
f676886a
JB
12265x_focus_on_frame (f)
12266 struct frame *f;
dc6f92b8 12267{
1fb20991 12268#if 0 /* This proves to be unpleasant. */
f676886a 12269 x_raise_frame (f);
1fb20991 12270#endif
6d4238f3
JB
12271#if 0
12272 /* I don't think that the ICCCM allows programs to do things like this
12273 without the interaction of the window manager. Whatever you end up
f676886a 12274 doing with this code, do it to x_unfocus_frame too. */
334208b7 12275 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 12276 RevertToPointerRoot, CurrentTime);
c118dd06 12277#endif /* ! 0 */
dc6f92b8
JB
12278}
12279
dfcf069d 12280void
f676886a
JB
12281x_unfocus_frame (f)
12282 struct frame *f;
dc6f92b8 12283{
6d4238f3 12284#if 0
f676886a 12285 /* Look at the remarks in x_focus_on_frame. */
0f941935 12286 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 12287 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 12288 RevertToPointerRoot, CurrentTime);
c118dd06 12289#endif /* ! 0 */
dc6f92b8
JB
12290}
12291
f676886a 12292/* Raise frame F. */
dc6f92b8 12293
dfcf069d 12294void
f676886a
JB
12295x_raise_frame (f)
12296 struct frame *f;
dc6f92b8 12297{
3a88c238 12298 if (f->async_visible)
dc6f92b8
JB
12299 {
12300 BLOCK_INPUT;
3afe33e7 12301#ifdef USE_X_TOOLKIT
7556890b 12302 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12303#else /* not USE_X_TOOLKIT */
334208b7 12304 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12305#endif /* not USE_X_TOOLKIT */
334208b7 12306 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12307 UNBLOCK_INPUT;
12308 }
12309}
12310
f676886a 12311/* Lower frame F. */
dc6f92b8 12312
dfcf069d 12313void
f676886a
JB
12314x_lower_frame (f)
12315 struct frame *f;
dc6f92b8 12316{
3a88c238 12317 if (f->async_visible)
dc6f92b8
JB
12318 {
12319 BLOCK_INPUT;
3afe33e7 12320#ifdef USE_X_TOOLKIT
7556890b 12321 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12322#else /* not USE_X_TOOLKIT */
334208b7 12323 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12324#endif /* not USE_X_TOOLKIT */
334208b7 12325 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12326 UNBLOCK_INPUT;
12327 }
12328}
12329
dbc4e1c1 12330static void
6b0442dc 12331XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12332 FRAME_PTR f;
6b0442dc 12333 int raise_flag;
dbc4e1c1 12334{
6b0442dc 12335 if (raise_flag)
dbc4e1c1
JB
12336 x_raise_frame (f);
12337 else
12338 x_lower_frame (f);
12339}
d047c4eb
KH
12340\f
12341/* Change of visibility. */
dc6f92b8 12342
9382638d
KH
12343/* This tries to wait until the frame is really visible.
12344 However, if the window manager asks the user where to position
12345 the frame, this will return before the user finishes doing that.
12346 The frame will not actually be visible at that time,
12347 but it will become visible later when the window manager
12348 finishes with it. */
12349
dfcf069d 12350void
f676886a
JB
12351x_make_frame_visible (f)
12352 struct frame *f;
dc6f92b8 12353{
990ba854 12354 Lisp_Object type;
1aa6072f 12355 int original_top, original_left;
31be9251
GM
12356 int retry_count = 2;
12357
12358 retry:
dc6f92b8 12359
dc6f92b8 12360 BLOCK_INPUT;
dc6f92b8 12361
990ba854
RS
12362 type = x_icon_type (f);
12363 if (!NILP (type))
12364 x_bitmap_icon (f, type);
bdcd49ba 12365
f676886a 12366 if (! FRAME_VISIBLE_P (f))
90e65f07 12367 {
1aa6072f
RS
12368 /* We test FRAME_GARBAGED_P here to make sure we don't
12369 call x_set_offset a second time
12370 if we get to x_make_frame_visible a second time
12371 before the window gets really visible. */
12372 if (! FRAME_ICONIFIED_P (f)
12373 && ! f->output_data.x->asked_for_visible)
12374 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12375
12376 f->output_data.x->asked_for_visible = 1;
12377
90e65f07 12378 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12379 x_wm_set_window_state (f, NormalState);
3afe33e7 12380#ifdef USE_X_TOOLKIT
d7a38a2e 12381 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12382 XtMapWidget (f->output_data.x->widget);
3afe33e7 12383#else /* not USE_X_TOOLKIT */
7f9c7f94 12384 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12385#endif /* not USE_X_TOOLKIT */
0134a210
RS
12386#if 0 /* This seems to bring back scroll bars in the wrong places
12387 if the window configuration has changed. They seem
12388 to come back ok without this. */
ab648270 12389 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12390 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12391#endif
90e65f07 12392 }
dc6f92b8 12393
334208b7 12394 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12395
0dacf791
RS
12396 /* Synchronize to ensure Emacs knows the frame is visible
12397 before we do anything else. We do this loop with input not blocked
12398 so that incoming events are handled. */
12399 {
12400 Lisp_Object frame;
12ce2351 12401 int count;
28c01ffe
RS
12402 /* This must be before UNBLOCK_INPUT
12403 since events that arrive in response to the actions above
12404 will set it when they are handled. */
12405 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12406
12407 original_left = f->output_data.x->left_pos;
12408 original_top = f->output_data.x->top_pos;
c0a04927
RS
12409
12410 /* This must come after we set COUNT. */
12411 UNBLOCK_INPUT;
12412
2745e6c4 12413 /* We unblock here so that arriving X events are processed. */
1aa6072f 12414
dcb07ae9
RS
12415 /* Now move the window back to where it was "supposed to be".
12416 But don't do it if the gravity is negative.
12417 When the gravity is negative, this uses a position
28c01ffe
RS
12418 that is 3 pixels too low. Perhaps that's really the border width.
12419
12420 Don't do this if the window has never been visible before,
12421 because the window manager may choose the position
12422 and we don't want to override it. */
1aa6072f 12423
4d3f5d9a 12424 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12425 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12426 && previously_visible)
1aa6072f 12427 {
2745e6c4
RS
12428 Drawable rootw;
12429 int x, y;
12430 unsigned int width, height, border, depth;
06a2c219 12431
1aa6072f 12432 BLOCK_INPUT;
9829ddba 12433
06a2c219
GM
12434 /* On some window managers (such as FVWM) moving an existing
12435 window, even to the same place, causes the window manager
12436 to introduce an offset. This can cause the window to move
12437 to an unexpected location. Check the geometry (a little
12438 slow here) and then verify that the window is in the right
12439 place. If the window is not in the right place, move it
12440 there, and take the potential window manager hit. */
2745e6c4
RS
12441 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12442 &rootw, &x, &y, &width, &height, &border, &depth);
12443
12444 if (original_left != x || original_top != y)
12445 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12446 original_left, original_top);
12447
1aa6072f
RS
12448 UNBLOCK_INPUT;
12449 }
9829ddba 12450
e0c1aef2 12451 XSETFRAME (frame, f);
c0a04927 12452
12ce2351
GM
12453 /* Wait until the frame is visible. Process X events until a
12454 MapNotify event has been seen, or until we think we won't get a
12455 MapNotify at all.. */
12456 for (count = input_signal_count + 10;
12457 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12458 {
12ce2351 12459 /* Force processing of queued events. */
334208b7 12460 x_sync (f);
12ce2351
GM
12461
12462 /* Machines that do polling rather than SIGIO have been
12463 observed to go into a busy-wait here. So we'll fake an
12464 alarm signal to let the handler know that there's something
12465 to be read. We used to raise a real alarm, but it seems
12466 that the handler isn't always enabled here. This is
12467 probably a bug. */
8b2f8d4e 12468 if (input_polling_used ())
3b2fa4e6 12469 {
12ce2351
GM
12470 /* It could be confusing if a real alarm arrives while
12471 processing the fake one. Turn it off and let the
12472 handler reset it. */
3e71d8f2 12473 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12474 int old_poll_suppress_count = poll_suppress_count;
12475 poll_suppress_count = 1;
12476 poll_for_input_1 ();
12477 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12478 }
12ce2351
GM
12479
12480 /* See if a MapNotify event has been processed. */
12481 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12482 }
31be9251
GM
12483
12484 /* 2000-09-28: In
12485
12486 (let ((f (selected-frame)))
12487 (iconify-frame f)
12488 (raise-frame f))
12489
12490 the frame is not raised with various window managers on
12491 FreeBSD, Linux and Solaris. It turns out that, for some
12492 unknown reason, the call to XtMapWidget is completely ignored.
12493 Mapping the widget a second time works. */
12494
12495 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12496 goto retry;
0dacf791 12497 }
dc6f92b8
JB
12498}
12499
06a2c219 12500/* Change from mapped state to withdrawn state. */
dc6f92b8 12501
d047c4eb
KH
12502/* Make the frame visible (mapped and not iconified). */
12503
dfcf069d 12504void
f676886a
JB
12505x_make_frame_invisible (f)
12506 struct frame *f;
dc6f92b8 12507{
546e6d5b
RS
12508 Window window;
12509
12510#ifdef USE_X_TOOLKIT
12511 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12512 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12513#else /* not USE_X_TOOLKIT */
12514 window = FRAME_X_WINDOW (f);
12515#endif /* not USE_X_TOOLKIT */
dc6f92b8 12516
9319ae23 12517 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12518 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12519 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12520
5627c40e 12521#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12522 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12523 return;
5627c40e 12524#endif
dc6f92b8
JB
12525
12526 BLOCK_INPUT;
c118dd06 12527
af31d76f
RS
12528 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12529 that the current position of the window is user-specified, rather than
12530 program-specified, so that when the window is mapped again, it will be
12531 placed at the same location, without forcing the user to position it
12532 by hand again (they have already done that once for this window.) */
c32cdd9a 12533 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12534
c118dd06
JB
12535#ifdef HAVE_X11R4
12536
334208b7
RS
12537 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12538 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12539 {
12540 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12541 error ("Can't notify window manager of window withdrawal");
c118dd06 12542 }
c118dd06 12543#else /* ! defined (HAVE_X11R4) */
16bd92ea 12544
c118dd06 12545 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12546 if (! EQ (Vx_no_window_manager, Qt))
12547 {
16bd92ea 12548 XEvent unmap;
dc6f92b8 12549
16bd92ea 12550 unmap.xunmap.type = UnmapNotify;
546e6d5b 12551 unmap.xunmap.window = window;
334208b7 12552 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12553 unmap.xunmap.from_configure = False;
334208b7
RS
12554 if (! XSendEvent (FRAME_X_DISPLAY (f),
12555 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12556 False,
06a2c219 12557 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12558 &unmap))
12559 {
12560 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12561 error ("Can't notify window manager of withdrawal");
16bd92ea 12562 }
dc6f92b8
JB
12563 }
12564
16bd92ea 12565 /* Unmap the window ourselves. Cheeky! */
334208b7 12566 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12567#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12568
5627c40e
RS
12569 /* We can't distinguish this from iconification
12570 just by the event that we get from the server.
12571 So we can't win using the usual strategy of letting
12572 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12573 and synchronize with the server to make sure we agree. */
12574 f->visible = 0;
12575 FRAME_ICONIFIED_P (f) = 0;
12576 f->async_visible = 0;
12577 f->async_iconified = 0;
12578
334208b7 12579 x_sync (f);
5627c40e 12580
dc6f92b8
JB
12581 UNBLOCK_INPUT;
12582}
12583
06a2c219 12584/* Change window state from mapped to iconified. */
dc6f92b8 12585
dfcf069d 12586void
f676886a
JB
12587x_iconify_frame (f)
12588 struct frame *f;
dc6f92b8 12589{
3afe33e7 12590 int result;
990ba854 12591 Lisp_Object type;
dc6f92b8 12592
9319ae23 12593 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12594 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12595 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12596
3a88c238 12597 if (f->async_iconified)
dc6f92b8
JB
12598 return;
12599
3afe33e7 12600 BLOCK_INPUT;
546e6d5b 12601
9af3143a
RS
12602 FRAME_SAMPLE_VISIBILITY (f);
12603
990ba854
RS
12604 type = x_icon_type (f);
12605 if (!NILP (type))
12606 x_bitmap_icon (f, type);
bdcd49ba
RS
12607
12608#ifdef USE_X_TOOLKIT
12609
546e6d5b
RS
12610 if (! FRAME_VISIBLE_P (f))
12611 {
12612 if (! EQ (Vx_no_window_manager, Qt))
12613 x_wm_set_window_state (f, IconicState);
12614 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12615 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12616 /* The server won't give us any event to indicate
12617 that an invisible frame was changed to an icon,
12618 so we have to record it here. */
12619 f->iconified = 1;
1e6bc770 12620 f->visible = 1;
9cf30a30 12621 f->async_iconified = 1;
1e6bc770 12622 f->async_visible = 0;
546e6d5b
RS
12623 UNBLOCK_INPUT;
12624 return;
12625 }
12626
334208b7 12627 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12628 XtWindow (f->output_data.x->widget),
334208b7 12629 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12630 UNBLOCK_INPUT;
12631
12632 if (!result)
546e6d5b 12633 error ("Can't notify window manager of iconification");
3afe33e7
RS
12634
12635 f->async_iconified = 1;
1e6bc770
RS
12636 f->async_visible = 0;
12637
8c002a25
KH
12638
12639 BLOCK_INPUT;
334208b7 12640 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12641 UNBLOCK_INPUT;
3afe33e7
RS
12642#else /* not USE_X_TOOLKIT */
12643
fd13dbb2
RS
12644 /* Make sure the X server knows where the window should be positioned,
12645 in case the user deiconifies with the window manager. */
12646 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12647 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12648
16bd92ea
JB
12649 /* Since we don't know which revision of X we're running, we'll use both
12650 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12651
12652 /* X11R4: send a ClientMessage to the window manager using the
12653 WM_CHANGE_STATE type. */
12654 {
12655 XEvent message;
58769bee 12656
c118dd06 12657 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12658 message.xclient.type = ClientMessage;
334208b7 12659 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12660 message.xclient.format = 32;
12661 message.xclient.data.l[0] = IconicState;
12662
334208b7
RS
12663 if (! XSendEvent (FRAME_X_DISPLAY (f),
12664 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12665 False,
12666 SubstructureRedirectMask | SubstructureNotifyMask,
12667 &message))
dc6f92b8
JB
12668 {
12669 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12670 error ("Can't notify window manager of iconification");
dc6f92b8 12671 }
16bd92ea 12672 }
dc6f92b8 12673
58769bee 12674 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12675 IconicState. */
12676 x_wm_set_window_state (f, IconicState);
dc6f92b8 12677
a9c00105
RS
12678 if (!FRAME_VISIBLE_P (f))
12679 {
12680 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12681 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12682 }
12683
3a88c238 12684 f->async_iconified = 1;
1e6bc770 12685 f->async_visible = 0;
dc6f92b8 12686
334208b7 12687 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12688 UNBLOCK_INPUT;
8c002a25 12689#endif /* not USE_X_TOOLKIT */
dc6f92b8 12690}
19f71add 12691
d047c4eb 12692\f
19f71add 12693/* Free X resources of frame F. */
dc6f92b8 12694
dfcf069d 12695void
19f71add 12696x_free_frame_resources (f)
f676886a 12697 struct frame *f;
dc6f92b8 12698{
7f9c7f94
RS
12699 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12700
dc6f92b8 12701 BLOCK_INPUT;
c0ff3fab 12702
6186a4a0
RS
12703 /* If a display connection is dead, don't try sending more
12704 commands to the X server. */
19f71add 12705 if (dpyinfo->display)
6186a4a0 12706 {
19f71add 12707 if (f->output_data.x->icon_desc)
6186a4a0 12708 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
19f71add 12709
31f41daf 12710#ifdef HAVE_X_I18N
f5d11644
GM
12711 if (FRAME_XIC (f))
12712 free_frame_xic (f);
31f41daf 12713#endif
19f71add 12714
2662734b 12715 if (FRAME_X_WINDOW (f))
19f71add
GM
12716 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12717
3afe33e7 12718#ifdef USE_X_TOOLKIT
06a2c219
GM
12719 if (f->output_data.x->widget)
12720 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12721 free_frame_menubar (f);
3afe33e7
RS
12722#endif /* USE_X_TOOLKIT */
12723
3e71d8f2
GM
12724 unload_color (f, f->output_data.x->foreground_pixel);
12725 unload_color (f, f->output_data.x->background_pixel);
12726 unload_color (f, f->output_data.x->cursor_pixel);
12727 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12728 unload_color (f, f->output_data.x->border_pixel);
12729 unload_color (f, f->output_data.x->mouse_pixel);
19f71add 12730
3e71d8f2
GM
12731 if (f->output_data.x->scroll_bar_background_pixel != -1)
12732 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12733 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12734 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12735 if (f->output_data.x->white_relief.allocated_p)
12736 unload_color (f, f->output_data.x->white_relief.pixel);
12737 if (f->output_data.x->black_relief.allocated_p)
12738 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 12739
19f71add
GM
12740 if (FRAME_FACE_CACHE (f))
12741 free_frame_faces (f);
12742
4ca78676 12743 x_free_gcs (f);
6186a4a0
RS
12744 XFlush (FRAME_X_DISPLAY (f));
12745 }
dc6f92b8 12746
df89d8a4 12747 if (f->output_data.x->saved_menu_event)
06a2c219 12748 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12749
7556890b 12750 xfree (f->output_data.x);
19f71add
GM
12751 f->output_data.x = NULL;
12752
0f941935
KH
12753 if (f == dpyinfo->x_focus_frame)
12754 dpyinfo->x_focus_frame = 0;
12755 if (f == dpyinfo->x_focus_event_frame)
12756 dpyinfo->x_focus_event_frame = 0;
12757 if (f == dpyinfo->x_highlight_frame)
12758 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12759
7f9c7f94 12760 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12761 {
7f9c7f94
RS
12762 dpyinfo->mouse_face_beg_row
12763 = dpyinfo->mouse_face_beg_col = -1;
12764 dpyinfo->mouse_face_end_row
12765 = dpyinfo->mouse_face_end_col = -1;
12766 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12767 dpyinfo->mouse_face_deferred_gc = 0;
12768 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12769 }
0134a210 12770
c0ff3fab 12771 UNBLOCK_INPUT;
dc6f92b8 12772}
19f71add
GM
12773
12774
12775/* Destroy the X window of frame F. */
12776
12777void
12778x_destroy_window (f)
12779 struct frame *f;
12780{
12781 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12782
12783 /* If a display connection is dead, don't try sending more
12784 commands to the X server. */
12785 if (dpyinfo->display != 0)
12786 x_free_frame_resources (f);
12787
12788 dpyinfo->reference_count--;
12789}
12790
dc6f92b8 12791\f
f451eb13
JB
12792/* Setting window manager hints. */
12793
af31d76f
RS
12794/* Set the normal size hints for the window manager, for frame F.
12795 FLAGS is the flags word to use--or 0 meaning preserve the flags
12796 that the window now has.
12797 If USER_POSITION is nonzero, we set the USPosition
12798 flag (this is useful when FLAGS is 0). */
6dba1858 12799
dfcf069d 12800void
af31d76f 12801x_wm_set_size_hint (f, flags, user_position)
f676886a 12802 struct frame *f;
af31d76f
RS
12803 long flags;
12804 int user_position;
dc6f92b8
JB
12805{
12806 XSizeHints size_hints;
3afe33e7
RS
12807
12808#ifdef USE_X_TOOLKIT
7e4f2521
FP
12809 Arg al[2];
12810 int ac = 0;
12811 Dimension widget_width, widget_height;
7556890b 12812 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12813#else /* not USE_X_TOOLKIT */
c118dd06 12814 Window window = FRAME_X_WINDOW (f);
3afe33e7 12815#endif /* not USE_X_TOOLKIT */
dc6f92b8 12816
b72a58fd
RS
12817 /* Setting PMaxSize caused various problems. */
12818 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12819
7556890b
RS
12820 size_hints.x = f->output_data.x->left_pos;
12821 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12822
7e4f2521
FP
12823#ifdef USE_X_TOOLKIT
12824 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12825 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12826 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12827 size_hints.height = widget_height;
12828 size_hints.width = widget_width;
12829#else /* not USE_X_TOOLKIT */
f676886a
JB
12830 size_hints.height = PIXEL_HEIGHT (f);
12831 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12832#endif /* not USE_X_TOOLKIT */
7553a6b7 12833
7556890b
RS
12834 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12835 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12836 size_hints.max_width
12837 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12838 size_hints.max_height
12839 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12840
d067ea8b
KH
12841 /* Calculate the base and minimum sizes.
12842
12843 (When we use the X toolkit, we don't do it here.
12844 Instead we copy the values that the widgets are using, below.) */
12845#ifndef USE_X_TOOLKIT
b1c884c3 12846 {
b0342f17 12847 int base_width, base_height;
0134a210 12848 int min_rows = 0, min_cols = 0;
b0342f17 12849
f451eb13
JB
12850 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12851 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12852
0134a210 12853 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12854
0134a210
RS
12855 /* The window manager uses the base width hints to calculate the
12856 current number of rows and columns in the frame while
12857 resizing; min_width and min_height aren't useful for this
12858 purpose, since they might not give the dimensions for a
12859 zero-row, zero-column frame.
58769bee 12860
0134a210
RS
12861 We use the base_width and base_height members if we have
12862 them; otherwise, we set the min_width and min_height members
12863 to the size for a zero x zero frame. */
b0342f17
JB
12864
12865#ifdef HAVE_X11R4
0134a210
RS
12866 size_hints.flags |= PBaseSize;
12867 size_hints.base_width = base_width;
12868 size_hints.base_height = base_height;
12869 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12870 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12871#else
0134a210
RS
12872 size_hints.min_width = base_width;
12873 size_hints.min_height = base_height;
b0342f17 12874#endif
b1c884c3 12875 }
dc6f92b8 12876
d067ea8b 12877 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12878 if (flags)
dc6f92b8 12879 {
d067ea8b
KH
12880 size_hints.flags |= flags;
12881 goto no_read;
12882 }
12883#endif /* not USE_X_TOOLKIT */
12884
12885 {
12886 XSizeHints hints; /* Sometimes I hate X Windows... */
12887 long supplied_return;
12888 int value;
af31d76f
RS
12889
12890#ifdef HAVE_X11R4
d067ea8b
KH
12891 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12892 &supplied_return);
af31d76f 12893#else
d067ea8b 12894 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12895#endif
58769bee 12896
d067ea8b
KH
12897#ifdef USE_X_TOOLKIT
12898 size_hints.base_height = hints.base_height;
12899 size_hints.base_width = hints.base_width;
12900 size_hints.min_height = hints.min_height;
12901 size_hints.min_width = hints.min_width;
12902#endif
12903
12904 if (flags)
12905 size_hints.flags |= flags;
12906 else
12907 {
12908 if (value == 0)
12909 hints.flags = 0;
12910 if (hints.flags & PSize)
12911 size_hints.flags |= PSize;
12912 if (hints.flags & PPosition)
12913 size_hints.flags |= PPosition;
12914 if (hints.flags & USPosition)
12915 size_hints.flags |= USPosition;
12916 if (hints.flags & USSize)
12917 size_hints.flags |= USSize;
12918 }
12919 }
12920
06a2c219 12921#ifndef USE_X_TOOLKIT
d067ea8b 12922 no_read:
06a2c219 12923#endif
0134a210 12924
af31d76f 12925#ifdef PWinGravity
7556890b 12926 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12927 size_hints.flags |= PWinGravity;
dc05a16b 12928
af31d76f 12929 if (user_position)
6dba1858 12930 {
af31d76f
RS
12931 size_hints.flags &= ~ PPosition;
12932 size_hints.flags |= USPosition;
6dba1858 12933 }
2554751d 12934#endif /* PWinGravity */
6dba1858 12935
b0342f17 12936#ifdef HAVE_X11R4
334208b7 12937 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12938#else
334208b7 12939 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12940#endif
dc6f92b8
JB
12941}
12942
12943/* Used for IconicState or NormalState */
06a2c219 12944
dfcf069d 12945void
f676886a
JB
12946x_wm_set_window_state (f, state)
12947 struct frame *f;
dc6f92b8
JB
12948 int state;
12949{
3afe33e7 12950#ifdef USE_X_TOOLKIT
546e6d5b
RS
12951 Arg al[1];
12952
12953 XtSetArg (al[0], XtNinitialState, state);
7556890b 12954 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12955#else /* not USE_X_TOOLKIT */
c118dd06 12956 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12957
7556890b
RS
12958 f->output_data.x->wm_hints.flags |= StateHint;
12959 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12960
7556890b 12961 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12962#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12963}
12964
dfcf069d 12965void
7f2ae036 12966x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12967 struct frame *f;
7f2ae036 12968 int pixmap_id;
dc6f92b8 12969{
d2bd6bc4
RS
12970 Pixmap icon_pixmap;
12971
06a2c219 12972#ifndef USE_X_TOOLKIT
c118dd06 12973 Window window = FRAME_X_WINDOW (f);
75231bad 12974#endif
dc6f92b8 12975
7f2ae036 12976 if (pixmap_id > 0)
dbc4e1c1 12977 {
d2bd6bc4 12978 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12979 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12980 }
12981 else
68568555
RS
12982 {
12983 /* It seems there is no way to turn off use of an icon pixmap.
12984 The following line does it, only if no icon has yet been created,
12985 for some window managers. But with mwm it crashes.
12986 Some people say it should clear the IconPixmapHint bit in this case,
12987 but that doesn't work, and the X consortium said it isn't the
12988 right thing at all. Since there is no way to win,
12989 best to explicitly give up. */
12990#if 0
12991 f->output_data.x->wm_hints.icon_pixmap = None;
12992#else
12993 return;
12994#endif
12995 }
b1c884c3 12996
d2bd6bc4
RS
12997#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12998
12999 {
13000 Arg al[1];
13001 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13002 XtSetValues (f->output_data.x->widget, al, 1);
13003 }
13004
13005#else /* not USE_X_TOOLKIT */
13006
7556890b
RS
13007 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13008 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
13009
13010#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
13011}
13012
dfcf069d 13013void
f676886a
JB
13014x_wm_set_icon_position (f, icon_x, icon_y)
13015 struct frame *f;
dc6f92b8
JB
13016 int icon_x, icon_y;
13017{
75231bad 13018#ifdef USE_X_TOOLKIT
7556890b 13019 Window window = XtWindow (f->output_data.x->widget);
75231bad 13020#else
c118dd06 13021 Window window = FRAME_X_WINDOW (f);
75231bad 13022#endif
dc6f92b8 13023
7556890b
RS
13024 f->output_data.x->wm_hints.flags |= IconPositionHint;
13025 f->output_data.x->wm_hints.icon_x = icon_x;
13026 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 13027
7556890b 13028 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
13029}
13030
13031\f
06a2c219
GM
13032/***********************************************************************
13033 Fonts
13034 ***********************************************************************/
dc43ef94
KH
13035
13036/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 13037
dc43ef94
KH
13038struct font_info *
13039x_get_font_info (f, font_idx)
13040 FRAME_PTR f;
13041 int font_idx;
13042{
13043 return (FRAME_X_FONT_TABLE (f) + font_idx);
13044}
13045
13046
9c11f79e
GM
13047/* Return a list of names of available fonts matching PATTERN on frame F.
13048
13049 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13050 to be listed.
13051
13052 SIZE < 0 means include scalable fonts.
13053
13054 Frame F null means we have not yet created any frame on X, and
13055 consult the first display in x_display_list. MAXNAMES sets a limit
13056 on how many fonts to match. */
dc43ef94
KH
13057
13058Lisp_Object
13059x_list_fonts (f, pattern, size, maxnames)
9c11f79e 13060 struct frame *f;
dc43ef94
KH
13061 Lisp_Object pattern;
13062 int size;
13063 int maxnames;
13064{
06a2c219
GM
13065 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13066 Lisp_Object tem, second_best;
9c11f79e
GM
13067 struct x_display_info *dpyinfo
13068 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13069 Display *dpy = dpyinfo->display;
09c6077f 13070 int try_XLoadQueryFont = 0;
53ca4657 13071 int count;
9c11f79e
GM
13072 int allow_scalable_fonts_p = 0;
13073
13074 if (size < 0)
13075 {
13076 allow_scalable_fonts_p = 1;
13077 size = 0;
13078 }
dc43ef94 13079
6b0efe73 13080 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
13081 if (NILP (patterns))
13082 patterns = Fcons (pattern, Qnil);
81ba44e5 13083
09c6077f
KH
13084 if (maxnames == 1 && !size)
13085 /* We can return any single font matching PATTERN. */
13086 try_XLoadQueryFont = 1;
9a32686f 13087
8e713be6 13088 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 13089 {
dc43ef94 13090 int num_fonts;
3e71d8f2 13091 char **names = NULL;
dc43ef94 13092
8e713be6 13093 pattern = XCAR (patterns);
536f4067
RS
13094 /* See if we cached the result for this particular query.
13095 The cache is an alist of the form:
9c11f79e
GM
13096 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13097 tem = XCDR (dpyinfo->name_list_element);
13098 key = Fcons (Fcons (pattern, make_number (maxnames)),
13099 allow_scalable_fonts_p ? Qt : Qnil);
13100 list = Fassoc (key, tem);
13101 if (!NILP (list))
b5210ea7
KH
13102 {
13103 list = Fcdr_safe (list);
13104 /* We have a cashed list. Don't have to get the list again. */
13105 goto label_cached;
13106 }
13107
13108 /* At first, put PATTERN in the cache. */
09c6077f 13109
dc43ef94 13110 BLOCK_INPUT;
17d85edc
KH
13111 count = x_catch_errors (dpy);
13112
09c6077f
KH
13113 if (try_XLoadQueryFont)
13114 {
13115 XFontStruct *font;
13116 unsigned long value;
13117
13118 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
13119 if (x_had_errors_p (dpy))
13120 {
13121 /* This error is perhaps due to insufficient memory on X
13122 server. Let's just ignore it. */
13123 font = NULL;
13124 x_clear_errors (dpy);
13125 }
13126
09c6077f
KH
13127 if (font
13128 && XGetFontProperty (font, XA_FONT, &value))
13129 {
13130 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13131 int len = strlen (name);
01c752b5 13132 char *tmp;
09c6077f 13133
6f6512e8
KH
13134 /* If DXPC (a Differential X Protocol Compressor)
13135 Ver.3.7 is running, XGetAtomName will return null
13136 string. We must avoid such a name. */
13137 if (len == 0)
13138 try_XLoadQueryFont = 0;
13139 else
13140 {
13141 num_fonts = 1;
13142 names = (char **) alloca (sizeof (char *));
13143 /* Some systems only allow alloca assigned to a
13144 simple var. */
13145 tmp = (char *) alloca (len + 1); names[0] = tmp;
13146 bcopy (name, names[0], len + 1);
13147 XFree (name);
13148 }
09c6077f
KH
13149 }
13150 else
13151 try_XLoadQueryFont = 0;
a083fd23
RS
13152
13153 if (font)
13154 XFreeFont (dpy, font);
09c6077f
KH
13155 }
13156
13157 if (!try_XLoadQueryFont)
17d85edc
KH
13158 {
13159 /* We try at least 10 fonts because XListFonts will return
13160 auto-scaled fonts at the head. */
13161 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13162 &num_fonts);
13163 if (x_had_errors_p (dpy))
13164 {
13165 /* This error is perhaps due to insufficient memory on X
13166 server. Let's just ignore it. */
13167 names = NULL;
13168 x_clear_errors (dpy);
13169 }
13170 }
13171
13172 x_uncatch_errors (dpy, count);
dc43ef94
KH
13173 UNBLOCK_INPUT;
13174
13175 if (names)
13176 {
13177 int i;
dc43ef94
KH
13178
13179 /* Make a list of all the fonts we got back.
13180 Store that in the font cache for the display. */
13181 for (i = 0; i < num_fonts; i++)
13182 {
06a2c219 13183 int width = 0;
dc43ef94 13184 char *p = names[i];
06a2c219
GM
13185 int average_width = -1, dashes = 0;
13186
dc43ef94 13187 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
13188 14 dashes, and the field value following 12th dash
13189 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13190 is usually too ugly to be used for editing. Let's
13191 ignore it. */
dc43ef94
KH
13192 while (*p)
13193 if (*p++ == '-')
13194 {
13195 dashes++;
13196 if (dashes == 7) /* PIXEL_SIZE field */
13197 width = atoi (p);
13198 else if (dashes == 12) /* AVERAGE_WIDTH field */
13199 average_width = atoi (p);
13200 }
9c11f79e
GM
13201
13202 if (allow_scalable_fonts_p
13203 || dashes < 14 || average_width != 0)
dc43ef94
KH
13204 {
13205 tem = build_string (names[i]);
13206 if (NILP (Fassoc (tem, list)))
13207 {
13208 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
13209 && ((fast_c_string_match_ignore_case
13210 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
13211 >= 0))
13212 /* We can set the value of PIXEL_SIZE to the
b5210ea7 13213 width of this font. */
dc43ef94
KH
13214 list = Fcons (Fcons (tem, make_number (width)), list);
13215 else
13216 /* For the moment, width is not known. */
13217 list = Fcons (Fcons (tem, Qnil), list);
13218 }
13219 }
13220 }
09c6077f
KH
13221 if (!try_XLoadQueryFont)
13222 XFreeFontNames (names);
dc43ef94
KH
13223 }
13224
b5210ea7 13225 /* Now store the result in the cache. */
9c11f79e
GM
13226 XCDR (dpyinfo->name_list_element)
13227 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
dc43ef94 13228
b5210ea7
KH
13229 label_cached:
13230 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 13231
b5210ea7
KH
13232 newlist = second_best = Qnil;
13233 /* Make a list of the fonts that have the right width. */
8e713be6 13234 for (; CONSP (list); list = XCDR (list))
b5210ea7 13235 {
536f4067
RS
13236 int found_size;
13237
8e713be6 13238 tem = XCAR (list);
dc43ef94 13239
8e713be6 13240 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
13241 continue;
13242 if (!size)
13243 {
8e713be6 13244 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
13245 continue;
13246 }
dc43ef94 13247
8e713be6 13248 if (!INTEGERP (XCDR (tem)))
dc43ef94 13249 {
b5210ea7 13250 /* Since we have not yet known the size of this font, we
9c11f79e 13251 must try slow function call XLoadQueryFont. */
dc43ef94
KH
13252 XFontStruct *thisinfo;
13253
13254 BLOCK_INPUT;
17d85edc 13255 count = x_catch_errors (dpy);
dc43ef94 13256 thisinfo = XLoadQueryFont (dpy,
8e713be6 13257 XSTRING (XCAR (tem))->data);
17d85edc
KH
13258 if (x_had_errors_p (dpy))
13259 {
13260 /* This error is perhaps due to insufficient memory on X
13261 server. Let's just ignore it. */
13262 thisinfo = NULL;
13263 x_clear_errors (dpy);
13264 }
13265 x_uncatch_errors (dpy, count);
dc43ef94
KH
13266 UNBLOCK_INPUT;
13267
13268 if (thisinfo)
13269 {
8e713be6 13270 XCDR (tem)
536f4067
RS
13271 = (thisinfo->min_bounds.width == 0
13272 ? make_number (0)
13273 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
13274 XFreeFont (dpy, thisinfo);
13275 }
13276 else
b5210ea7 13277 /* For unknown reason, the previous call of XListFont had
06a2c219 13278 returned a font which can't be opened. Record the size
b5210ea7 13279 as 0 not to try to open it again. */
8e713be6 13280 XCDR (tem) = make_number (0);
dc43ef94 13281 }
536f4067 13282
8e713be6 13283 found_size = XINT (XCDR (tem));
536f4067 13284 if (found_size == size)
8e713be6 13285 newlist = Fcons (XCAR (tem), newlist);
536f4067 13286 else if (found_size > 0)
b5210ea7 13287 {
536f4067 13288 if (NILP (second_best))
b5210ea7 13289 second_best = tem;
536f4067
RS
13290 else if (found_size < size)
13291 {
8e713be6
KR
13292 if (XINT (XCDR (second_best)) > size
13293 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
13294 second_best = tem;
13295 }
13296 else
13297 {
8e713be6
KR
13298 if (XINT (XCDR (second_best)) > size
13299 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
13300 second_best = tem;
13301 }
b5210ea7
KH
13302 }
13303 }
13304 if (!NILP (newlist))
13305 break;
13306 else if (!NILP (second_best))
13307 {
8e713be6 13308 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 13309 break;
dc43ef94 13310 }
dc43ef94
KH
13311 }
13312
13313 return newlist;
13314}
13315
06a2c219
GM
13316
13317#if GLYPH_DEBUG
13318
13319/* Check that FONT is valid on frame F. It is if it can be found in F's
13320 font table. */
13321
13322static void
13323x_check_font (f, font)
13324 struct frame *f;
13325 XFontStruct *font;
13326{
13327 int i;
13328 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13329
13330 xassert (font != NULL);
13331
13332 for (i = 0; i < dpyinfo->n_fonts; i++)
13333 if (dpyinfo->font_table[i].name
13334 && font == dpyinfo->font_table[i].font)
13335 break;
13336
13337 xassert (i < dpyinfo->n_fonts);
13338}
13339
13340#endif /* GLYPH_DEBUG != 0 */
13341
13342/* Set *W to the minimum width, *H to the minimum font height of FONT.
13343 Note: There are (broken) X fonts out there with invalid XFontStruct
13344 min_bounds contents. For example, handa@etl.go.jp reports that
13345 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13346 have font->min_bounds.width == 0. */
13347
13348static INLINE void
13349x_font_min_bounds (font, w, h)
13350 XFontStruct *font;
13351 int *w, *h;
13352{
13353 *h = FONT_HEIGHT (font);
13354 *w = font->min_bounds.width;
13355
13356 /* Try to handle the case where FONT->min_bounds has invalid
13357 contents. Since the only font known to have invalid min_bounds
13358 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13359 if (*w <= 0)
13360 *w = font->max_bounds.width;
13361}
13362
13363
13364/* Compute the smallest character width and smallest font height over
13365 all fonts available on frame F. Set the members smallest_char_width
13366 and smallest_font_height in F's x_display_info structure to
13367 the values computed. Value is non-zero if smallest_font_height or
13368 smallest_char_width become smaller than they were before. */
13369
13370static int
13371x_compute_min_glyph_bounds (f)
13372 struct frame *f;
13373{
13374 int i;
13375 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13376 XFontStruct *font;
13377 int old_width = dpyinfo->smallest_char_width;
13378 int old_height = dpyinfo->smallest_font_height;
13379
13380 dpyinfo->smallest_font_height = 100000;
13381 dpyinfo->smallest_char_width = 100000;
13382
13383 for (i = 0; i < dpyinfo->n_fonts; ++i)
13384 if (dpyinfo->font_table[i].name)
13385 {
13386 struct font_info *fontp = dpyinfo->font_table + i;
13387 int w, h;
13388
13389 font = (XFontStruct *) fontp->font;
13390 xassert (font != (XFontStruct *) ~0);
13391 x_font_min_bounds (font, &w, &h);
13392
13393 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13394 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13395 }
13396
13397 xassert (dpyinfo->smallest_char_width > 0
13398 && dpyinfo->smallest_font_height > 0);
13399
13400 return (dpyinfo->n_fonts == 1
13401 || dpyinfo->smallest_char_width < old_width
13402 || dpyinfo->smallest_font_height < old_height);
13403}
13404
13405
dc43ef94
KH
13406/* Load font named FONTNAME of the size SIZE for frame F, and return a
13407 pointer to the structure font_info while allocating it dynamically.
13408 If SIZE is 0, load any size of font.
13409 If loading is failed, return NULL. */
13410
13411struct font_info *
13412x_load_font (f, fontname, size)
13413 struct frame *f;
13414 register char *fontname;
13415 int size;
13416{
13417 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13418 Lisp_Object font_names;
d645aaa4 13419 int count;
dc43ef94
KH
13420
13421 /* Get a list of all the fonts that match this name. Once we
13422 have a list of matching fonts, we compare them against the fonts
13423 we already have by comparing names. */
09c6077f 13424 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13425
13426 if (!NILP (font_names))
13427 {
13428 Lisp_Object tail;
13429 int i;
13430
13431 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13432 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13433 if (dpyinfo->font_table[i].name
13434 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13435 XSTRING (XCAR (tail))->data)
06a2c219 13436 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13437 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13438 return (dpyinfo->font_table + i);
13439 }
13440
13441 /* Load the font and add it to the table. */
13442 {
13443 char *full_name;
13444 XFontStruct *font;
13445 struct font_info *fontp;
13446 unsigned long value;
06a2c219 13447 int i;
dc43ef94 13448
2da424f1
KH
13449 /* If we have found fonts by x_list_font, load one of them. If
13450 not, we still try to load a font by the name given as FONTNAME
13451 because XListFonts (called in x_list_font) of some X server has
13452 a bug of not finding a font even if the font surely exists and
13453 is loadable by XLoadQueryFont. */
e1d6d5b9 13454 if (size > 0 && !NILP (font_names))
8e713be6 13455 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13456
13457 BLOCK_INPUT;
d645aaa4 13458 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13459 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13460 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13461 {
13462 /* This error is perhaps due to insufficient memory on X
13463 server. Let's just ignore it. */
13464 font = NULL;
13465 x_clear_errors (FRAME_X_DISPLAY (f));
13466 }
13467 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13468 UNBLOCK_INPUT;
b5210ea7 13469 if (!font)
dc43ef94
KH
13470 return NULL;
13471
06a2c219
GM
13472 /* Find a free slot in the font table. */
13473 for (i = 0; i < dpyinfo->n_fonts; ++i)
13474 if (dpyinfo->font_table[i].name == NULL)
13475 break;
13476
13477 /* If no free slot found, maybe enlarge the font table. */
13478 if (i == dpyinfo->n_fonts
13479 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13480 {
06a2c219
GM
13481 int sz;
13482 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13483 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13484 dpyinfo->font_table
06a2c219 13485 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13486 }
13487
06a2c219
GM
13488 fontp = dpyinfo->font_table + i;
13489 if (i == dpyinfo->n_fonts)
13490 ++dpyinfo->n_fonts;
dc43ef94
KH
13491
13492 /* Now fill in the slots of *FONTP. */
13493 BLOCK_INPUT;
13494 fontp->font = font;
06a2c219 13495 fontp->font_idx = i;
dc43ef94
KH
13496 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13497 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13498
13499 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13500 full_name = 0;
13501 if (XGetFontProperty (font, XA_FONT, &value))
13502 {
13503 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13504 char *p = name;
13505 int dashes = 0;
13506
13507 /* Count the number of dashes in the "full name".
13508 If it is too few, this isn't really the font's full name,
13509 so don't use it.
13510 In X11R4, the fonts did not come with their canonical names
13511 stored in them. */
13512 while (*p)
13513 {
13514 if (*p == '-')
13515 dashes++;
13516 p++;
13517 }
13518
13519 if (dashes >= 13)
13520 {
13521 full_name = (char *) xmalloc (p - name + 1);
13522 bcopy (name, full_name, p - name + 1);
13523 }
13524
13525 XFree (name);
13526 }
13527
13528 if (full_name != 0)
13529 fontp->full_name = full_name;
13530 else
13531 fontp->full_name = fontp->name;
13532
13533 fontp->size = font->max_bounds.width;
d5749adb 13534 fontp->height = FONT_HEIGHT (font);
dc43ef94 13535
2da424f1
KH
13536 if (NILP (font_names))
13537 {
13538 /* We come here because of a bug of XListFonts mentioned at
13539 the head of this block. Let's store this information in
13540 the cache for x_list_fonts. */
13541 Lisp_Object lispy_name = build_string (fontname);
13542 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
13543 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13544 Qnil);
2da424f1 13545
8e713be6 13546 XCDR (dpyinfo->name_list_element)
9c11f79e 13547 = Fcons (Fcons (key,
2da424f1
KH
13548 Fcons (Fcons (lispy_full_name,
13549 make_number (fontp->size)),
13550 Qnil)),
8e713be6 13551 XCDR (dpyinfo->name_list_element));
2da424f1 13552 if (full_name)
9c11f79e
GM
13553 {
13554 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13555 Qnil);
13556 XCDR (dpyinfo->name_list_element)
13557 = Fcons (Fcons (key,
13558 Fcons (Fcons (lispy_full_name,
13559 make_number (fontp->size)),
13560 Qnil)),
13561 XCDR (dpyinfo->name_list_element));
13562 }
2da424f1
KH
13563 }
13564
dc43ef94
KH
13565 /* The slot `encoding' specifies how to map a character
13566 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13567 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13568 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13569 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13570 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13571 which is never used by any charset. If mapping can't be
13572 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13573 fontp->encoding[1]
13574 = (font->max_byte1 == 0
13575 /* 1-byte font */
13576 ? (font->min_char_or_byte2 < 0x80
13577 ? (font->max_char_or_byte2 < 0x80
13578 ? 0 /* 0x20..0x7F */
8ff102bd 13579 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13580 : 1) /* 0xA0..0xFF */
13581 /* 2-byte font */
13582 : (font->min_byte1 < 0x80
13583 ? (font->max_byte1 < 0x80
13584 ? (font->min_char_or_byte2 < 0x80
13585 ? (font->max_char_or_byte2 < 0x80
13586 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13587 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13588 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13589 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13590 : (font->min_char_or_byte2 < 0x80
13591 ? (font->max_char_or_byte2 < 0x80
13592 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13593 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13594 : 1))); /* 0xA0A0..0xFFFF */
13595
13596 fontp->baseline_offset
13597 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13598 ? (long) value : 0);
13599 fontp->relative_compose
13600 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13601 ? (long) value : 0);
f78798df
KH
13602 fontp->default_ascent
13603 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13604 ? (long) value : 0);
dc43ef94 13605
06a2c219
GM
13606 /* Set global flag fonts_changed_p to non-zero if the font loaded
13607 has a character with a smaller width than any other character
13608 before, or if the font loaded has a smalle>r height than any
13609 other font loaded before. If this happens, it will make a
13610 glyph matrix reallocation necessary. */
13611 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13612 UNBLOCK_INPUT;
dc43ef94
KH
13613 return fontp;
13614 }
13615}
13616
06a2c219
GM
13617
13618/* Return a pointer to struct font_info of a font named FONTNAME for
13619 frame F. If no such font is loaded, return NULL. */
13620
dc43ef94
KH
13621struct font_info *
13622x_query_font (f, fontname)
13623 struct frame *f;
13624 register char *fontname;
13625{
13626 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13627 int i;
13628
13629 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13630 if (dpyinfo->font_table[i].name
13631 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13632 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13633 return (dpyinfo->font_table + i);
13634 return NULL;
13635}
13636
06a2c219
GM
13637
13638/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13639 `encoder' of the structure. */
13640
13641void
13642x_find_ccl_program (fontp)
13643 struct font_info *fontp;
13644{
a42f54e6 13645 Lisp_Object list, elt;
a6582676 13646
f9b5db02 13647 elt = Qnil;
8e713be6 13648 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13649 {
8e713be6 13650 elt = XCAR (list);
a6582676 13651 if (CONSP (elt)
8e713be6 13652 && STRINGP (XCAR (elt))
9f2feff6
KH
13653 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13654 >= 0)
13655 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13656 >= 0)))
a42f54e6
KH
13657 break;
13658 }
f9b5db02 13659
a42f54e6
KH
13660 if (! NILP (list))
13661 {
d27f8ca7
KH
13662 struct ccl_program *ccl
13663 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13664
8e713be6 13665 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13666 xfree (ccl);
13667 else
13668 fontp->font_encoder = ccl;
a6582676
KH
13669 }
13670}
13671
06a2c219 13672
dc43ef94 13673\f
06a2c219
GM
13674/***********************************************************************
13675 Initialization
13676 ***********************************************************************/
f451eb13 13677
3afe33e7
RS
13678#ifdef USE_X_TOOLKIT
13679static XrmOptionDescRec emacs_options[] = {
13680 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13681 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13682
13683 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13684 XrmoptionSepArg, NULL},
13685 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13686
13687 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13688 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13689 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13690 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13691 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13692 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13693 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13694};
13695#endif /* USE_X_TOOLKIT */
13696
7a13e894
RS
13697static int x_initialized;
13698
29b38361
KH
13699#ifdef MULTI_KBOARD
13700/* Test whether two display-name strings agree up to the dot that separates
13701 the screen number from the server number. */
13702static int
13703same_x_server (name1, name2)
13704 char *name1, *name2;
13705{
13706 int seen_colon = 0;
cf591cc1
RS
13707 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13708 int system_name_length = strlen (system_name);
13709 int length_until_period = 0;
13710
13711 while (system_name[length_until_period] != 0
13712 && system_name[length_until_period] != '.')
13713 length_until_period++;
13714
13715 /* Treat `unix' like an empty host name. */
13716 if (! strncmp (name1, "unix:", 5))
13717 name1 += 4;
13718 if (! strncmp (name2, "unix:", 5))
13719 name2 += 4;
13720 /* Treat this host's name like an empty host name. */
13721 if (! strncmp (name1, system_name, system_name_length)
13722 && name1[system_name_length] == ':')
13723 name1 += system_name_length;
13724 if (! strncmp (name2, system_name, system_name_length)
13725 && name2[system_name_length] == ':')
13726 name2 += system_name_length;
13727 /* Treat this host's domainless name like an empty host name. */
13728 if (! strncmp (name1, system_name, length_until_period)
13729 && name1[length_until_period] == ':')
13730 name1 += length_until_period;
13731 if (! strncmp (name2, system_name, length_until_period)
13732 && name2[length_until_period] == ':')
13733 name2 += length_until_period;
13734
29b38361
KH
13735 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13736 {
13737 if (*name1 == ':')
13738 seen_colon++;
13739 if (seen_colon && *name1 == '.')
13740 return 1;
13741 }
13742 return (seen_colon
13743 && (*name1 == '.' || *name1 == '\0')
13744 && (*name2 == '.' || *name2 == '\0'));
13745}
13746#endif
13747
334208b7 13748struct x_display_info *
1f8255f2 13749x_term_init (display_name, xrm_option, resource_name)
334208b7 13750 Lisp_Object display_name;
1f8255f2
RS
13751 char *xrm_option;
13752 char *resource_name;
dc6f92b8 13753{
334208b7 13754 int connection;
7a13e894 13755 Display *dpy;
334208b7
RS
13756 struct x_display_info *dpyinfo;
13757 XrmDatabase xrdb;
13758
60439948
KH
13759 BLOCK_INPUT;
13760
7a13e894
RS
13761 if (!x_initialized)
13762 {
13763 x_initialize ();
13764 x_initialized = 1;
13765 }
dc6f92b8 13766
3afe33e7 13767#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13768 /* weiner@footloose.sps.mot.com reports that this causes
13769 errors with X11R5:
13770 X protocol error: BadAtom (invalid Atom parameter)
13771 on protocol request 18skiloaf.
13772 So let's not use it until R6. */
13773#ifdef HAVE_X11XTR6
bdcd49ba
RS
13774 XtSetLanguageProc (NULL, NULL, NULL);
13775#endif
13776
7f9c7f94
RS
13777 {
13778 int argc = 0;
13779 char *argv[3];
13780
13781 argv[0] = "";
13782 argc = 1;
13783 if (xrm_option)
13784 {
13785 argv[argc++] = "-xrm";
13786 argv[argc++] = xrm_option;
13787 }
13788 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13789 resource_name, EMACS_CLASS,
13790 emacs_options, XtNumber (emacs_options),
13791 &argc, argv);
39d8bb4d
KH
13792
13793#ifdef HAVE_X11XTR6
10537cb1 13794 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13795 fixup_locale ();
39d8bb4d 13796#endif
7f9c7f94 13797 }
3afe33e7
RS
13798
13799#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13800#ifdef HAVE_X11R5
13801 XSetLocaleModifiers ("");
13802#endif
7a13e894 13803 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13804#endif /* not USE_X_TOOLKIT */
334208b7 13805
7a13e894
RS
13806 /* Detect failure. */
13807 if (dpy == 0)
60439948
KH
13808 {
13809 UNBLOCK_INPUT;
13810 return 0;
13811 }
7a13e894
RS
13812
13813 /* We have definitely succeeded. Record the new connection. */
13814
13815 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13816 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13817
29b38361
KH
13818#ifdef MULTI_KBOARD
13819 {
13820 struct x_display_info *share;
13821 Lisp_Object tail;
13822
13823 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13824 share = share->next, tail = XCDR (tail))
13825 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13826 XSTRING (display_name)->data))
13827 break;
13828 if (share)
13829 dpyinfo->kboard = share->kboard;
13830 else
13831 {
13832 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13833 init_kboard (dpyinfo->kboard);
59e755be
KH
13834 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13835 {
13836 char *vendor = ServerVendor (dpy);
9b6ed9f3 13837 UNBLOCK_INPUT;
59e755be
KH
13838 dpyinfo->kboard->Vsystem_key_alist
13839 = call1 (Qvendor_specific_keysyms,
13840 build_string (vendor ? vendor : ""));
9b6ed9f3 13841 BLOCK_INPUT;
59e755be
KH
13842 }
13843
29b38361
KH
13844 dpyinfo->kboard->next_kboard = all_kboards;
13845 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13846 /* Don't let the initial kboard remain current longer than necessary.
13847 That would cause problems if a file loaded on startup tries to
06a2c219 13848 prompt in the mini-buffer. */
0ad5446c
KH
13849 if (current_kboard == initial_kboard)
13850 current_kboard = dpyinfo->kboard;
29b38361
KH
13851 }
13852 dpyinfo->kboard->reference_count++;
13853 }
b9737ad3
KH
13854#endif
13855
7a13e894
RS
13856 /* Put this display on the chain. */
13857 dpyinfo->next = x_display_list;
13858 x_display_list = dpyinfo;
13859
13860 /* Put it on x_display_name_list as well, to keep them parallel. */
13861 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13862 x_display_name_list);
8e713be6 13863 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13864
13865 dpyinfo->display = dpy;
dc6f92b8 13866
dc6f92b8 13867#if 0
7a13e894 13868 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13869#endif /* ! 0 */
7a13e894
RS
13870
13871 dpyinfo->x_id_name
fc932ac6
RS
13872 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13873 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13874 + 2);
13875 sprintf (dpyinfo->x_id_name, "%s@%s",
13876 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13877
13878 /* Figure out which modifier bits mean what. */
334208b7 13879 x_find_modifier_meanings (dpyinfo);
f451eb13 13880
ab648270 13881 /* Get the scroll bar cursor. */
7a13e894 13882 dpyinfo->vertical_scroll_bar_cursor
334208b7 13883 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13884
334208b7
RS
13885 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13886 resource_name, EMACS_CLASS);
13887#ifdef HAVE_XRMSETDATABASE
13888 XrmSetDatabase (dpyinfo->display, xrdb);
13889#else
13890 dpyinfo->display->db = xrdb;
13891#endif
547d9db8 13892 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13893 all versions. */
13894 dpyinfo->xrdb = xrdb;
334208b7
RS
13895
13896 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13897 DefaultScreen (dpyinfo->display));
5ff67d81 13898 select_visual (dpyinfo);
43bd1b2b 13899 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13900 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13901 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13902 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13903 dpyinfo->grabbed = 0;
13904 dpyinfo->reference_count = 0;
13905 dpyinfo->icon_bitmap_id = -1;
06a2c219 13906 dpyinfo->font_table = NULL;
7a13e894
RS
13907 dpyinfo->n_fonts = 0;
13908 dpyinfo->font_table_size = 0;
13909 dpyinfo->bitmaps = 0;
13910 dpyinfo->bitmaps_size = 0;
13911 dpyinfo->bitmaps_last = 0;
13912 dpyinfo->scratch_cursor_gc = 0;
13913 dpyinfo->mouse_face_mouse_frame = 0;
13914 dpyinfo->mouse_face_deferred_gc = 0;
13915 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13916 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13917 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 13918 dpyinfo->mouse_face_window = Qnil;
0a61c667 13919 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
13920 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13921 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13922 dpyinfo->x_focus_frame = 0;
13923 dpyinfo->x_focus_event_frame = 0;
13924 dpyinfo->x_highlight_frame = 0;
06a2c219 13925 dpyinfo->image_cache = make_image_cache ();
334208b7 13926
43bd1b2b 13927 /* See if a private colormap is requested. */
5ff67d81
GM
13928 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13929 {
13930 if (dpyinfo->visual->class == PseudoColor)
13931 {
13932 Lisp_Object value;
13933 value = display_x_get_resource (dpyinfo,
13934 build_string ("privateColormap"),
13935 build_string ("PrivateColormap"),
13936 Qnil, Qnil);
13937 if (STRINGP (value)
13938 && (!strcmp (XSTRING (value)->data, "true")
13939 || !strcmp (XSTRING (value)->data, "on")))
13940 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13941 }
43bd1b2b 13942 }
5ff67d81
GM
13943 else
13944 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13945 dpyinfo->visual, AllocNone);
43bd1b2b 13946
06a2c219
GM
13947 {
13948 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13949 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13950 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13951 dpyinfo->resy = pixels * 25.4 / mm;
13952 pixels = DisplayWidth (dpyinfo->display, screen_number);
13953 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13954 dpyinfo->resx = pixels * 25.4 / mm;
13955 }
13956
334208b7
RS
13957 dpyinfo->Xatom_wm_protocols
13958 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13959 dpyinfo->Xatom_wm_take_focus
13960 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13961 dpyinfo->Xatom_wm_save_yourself
13962 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13963 dpyinfo->Xatom_wm_delete_window
13964 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13965 dpyinfo->Xatom_wm_change_state
13966 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13967 dpyinfo->Xatom_wm_configure_denied
13968 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13969 dpyinfo->Xatom_wm_window_moved
13970 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13971 dpyinfo->Xatom_editres
13972 = XInternAtom (dpyinfo->display, "Editres", False);
13973 dpyinfo->Xatom_CLIPBOARD
13974 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13975 dpyinfo->Xatom_TIMESTAMP
13976 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13977 dpyinfo->Xatom_TEXT
13978 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13979 dpyinfo->Xatom_COMPOUND_TEXT
13980 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13981 dpyinfo->Xatom_DELETE
13982 = XInternAtom (dpyinfo->display, "DELETE", False);
13983 dpyinfo->Xatom_MULTIPLE
13984 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13985 dpyinfo->Xatom_INCR
13986 = XInternAtom (dpyinfo->display, "INCR", False);
13987 dpyinfo->Xatom_EMACS_TMP
13988 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13989 dpyinfo->Xatom_TARGETS
13990 = XInternAtom (dpyinfo->display, "TARGETS", False);
13991 dpyinfo->Xatom_NULL
13992 = XInternAtom (dpyinfo->display, "NULL", False);
13993 dpyinfo->Xatom_ATOM_PAIR
13994 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13995 /* For properties of font. */
13996 dpyinfo->Xatom_PIXEL_SIZE
13997 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13998 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13999 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14000 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14001 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
14002 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14003 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 14004
06a2c219
GM
14005 /* Ghostscript support. */
14006 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14007 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14008
14009 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14010 False);
14011
547d9db8
KH
14012 dpyinfo->cut_buffers_initialized = 0;
14013
334208b7
RS
14014 connection = ConnectionNumber (dpyinfo->display);
14015 dpyinfo->connection = connection;
14016
dc43ef94 14017 {
5d7cc324
RS
14018 char null_bits[1];
14019
14020 null_bits[0] = 0x00;
dc43ef94
KH
14021
14022 dpyinfo->null_pixel
14023 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14024 null_bits, 1, 1, (long) 0, (long) 0,
14025 1);
14026 }
14027
06a2c219
GM
14028 {
14029 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 14030 extern char *gray_bitmap_bits;
06a2c219
GM
14031 dpyinfo->gray
14032 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14033 gray_bitmap_bits,
14034 gray_bitmap_width, gray_bitmap_height,
14035 (unsigned long) 1, (unsigned long) 0, 1);
14036 }
14037
f5d11644
GM
14038#ifdef HAVE_X_I18N
14039 xim_initialize (dpyinfo, resource_name);
14040#endif
14041
87485d6f
MW
14042#ifdef subprocesses
14043 /* This is only needed for distinguishing keyboard and process input. */
334208b7 14044 if (connection != 0)
7a13e894 14045 add_keyboard_wait_descriptor (connection);
87485d6f 14046#endif
6d4238f3 14047
041b69ac 14048#ifndef F_SETOWN_BUG
dc6f92b8 14049#ifdef F_SETOWN
dc6f92b8 14050#ifdef F_SETOWN_SOCK_NEG
61c3ce62 14051 /* stdin is a socket here */
334208b7 14052 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 14053#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 14054 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
14055#endif /* ! defined (F_SETOWN_SOCK_NEG) */
14056#endif /* ! defined (F_SETOWN) */
041b69ac 14057#endif /* F_SETOWN_BUG */
dc6f92b8
JB
14058
14059#ifdef SIGIO
eee20f6a
KH
14060 if (interrupt_input)
14061 init_sigio (connection);
c118dd06 14062#endif /* ! defined (SIGIO) */
dc6f92b8 14063
51b592fb 14064#ifdef USE_LUCID
f8c39f51 14065#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
14066 /* Make sure that we have a valid font for dialog boxes
14067 so that Xt does not crash. */
14068 {
14069 Display *dpy = dpyinfo->display;
14070 XrmValue d, fr, to;
14071 Font font;
e99db5a1 14072 int count;
51b592fb
RS
14073
14074 d.addr = (XPointer)&dpy;
14075 d.size = sizeof (Display *);
14076 fr.addr = XtDefaultFont;
14077 fr.size = sizeof (XtDefaultFont);
14078 to.size = sizeof (Font *);
14079 to.addr = (XPointer)&font;
e99db5a1 14080 count = x_catch_errors (dpy);
51b592fb
RS
14081 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14082 abort ();
14083 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14084 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 14085 x_uncatch_errors (dpy, count);
51b592fb
RS
14086 }
14087#endif
f8c39f51 14088#endif
51b592fb 14089
34e23e5a
GM
14090 /* See if we should run in synchronous mode. This is useful
14091 for debugging X code. */
14092 {
14093 Lisp_Object value;
14094 value = display_x_get_resource (dpyinfo,
14095 build_string ("synchronous"),
14096 build_string ("Synchronous"),
14097 Qnil, Qnil);
14098 if (STRINGP (value)
14099 && (!strcmp (XSTRING (value)->data, "true")
14100 || !strcmp (XSTRING (value)->data, "on")))
14101 XSynchronize (dpyinfo->display, True);
14102 }
14103
60439948
KH
14104 UNBLOCK_INPUT;
14105
7a13e894
RS
14106 return dpyinfo;
14107}
14108\f
14109/* Get rid of display DPYINFO, assuming all frames are already gone,
14110 and without sending any more commands to the X server. */
dc6f92b8 14111
7a13e894
RS
14112void
14113x_delete_display (dpyinfo)
14114 struct x_display_info *dpyinfo;
14115{
14116 delete_keyboard_wait_descriptor (dpyinfo->connection);
14117
14118 /* Discard this display from x_display_name_list and x_display_list.
14119 We can't use Fdelq because that can quit. */
14120 if (! NILP (x_display_name_list)
8e713be6
KR
14121 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14122 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
14123 else
14124 {
14125 Lisp_Object tail;
14126
14127 tail = x_display_name_list;
8e713be6 14128 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 14129 {
bffcfca9 14130 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 14131 {
8e713be6 14132 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
14133 break;
14134 }
8e713be6 14135 tail = XCDR (tail);
7a13e894
RS
14136 }
14137 }
14138
9bda743f
GM
14139 if (next_noop_dpyinfo == dpyinfo)
14140 next_noop_dpyinfo = dpyinfo->next;
14141
7a13e894
RS
14142 if (x_display_list == dpyinfo)
14143 x_display_list = dpyinfo->next;
7f9c7f94
RS
14144 else
14145 {
14146 struct x_display_info *tail;
7a13e894 14147
7f9c7f94
RS
14148 for (tail = x_display_list; tail; tail = tail->next)
14149 if (tail->next == dpyinfo)
14150 tail->next = tail->next->next;
14151 }
7a13e894 14152
0d777288
RS
14153#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14154#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
14155 XrmDestroyDatabase (dpyinfo->xrdb);
14156#endif
0d777288 14157#endif
29b38361
KH
14158#ifdef MULTI_KBOARD
14159 if (--dpyinfo->kboard->reference_count == 0)
39f79001 14160 delete_kboard (dpyinfo->kboard);
b9737ad3 14161#endif
f5d11644
GM
14162#ifdef HAVE_X_I18N
14163 if (dpyinfo->xim)
14164 xim_close_dpy (dpyinfo);
14165#endif
14166
b9737ad3
KH
14167 xfree (dpyinfo->font_table);
14168 xfree (dpyinfo->x_id_name);
f04e1297 14169 xfree (dpyinfo->color_cells);
b9737ad3 14170 xfree (dpyinfo);
7a13e894 14171}
f04e1297 14172
7a13e894
RS
14173\f
14174/* Set up use of X before we make the first connection. */
14175
06a2c219
GM
14176static struct redisplay_interface x_redisplay_interface =
14177{
14178 x_produce_glyphs,
14179 x_write_glyphs,
14180 x_insert_glyphs,
14181 x_clear_end_of_line,
14182 x_scroll_run,
14183 x_after_update_window_line,
14184 x_update_window_begin,
14185 x_update_window_end,
14186 XTcursor_to,
14187 x_flush,
71b8321e 14188 x_clear_mouse_face,
66ac4b0e
GM
14189 x_get_glyph_overhangs,
14190 x_fix_overlapping_area
06a2c219
GM
14191};
14192
dfcf069d 14193void
7a13e894
RS
14194x_initialize ()
14195{
06a2c219
GM
14196 rif = &x_redisplay_interface;
14197
14198 clear_frame_hook = x_clear_frame;
14199 ins_del_lines_hook = x_ins_del_lines;
14200 change_line_highlight_hook = x_change_line_highlight;
14201 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
14202 ring_bell_hook = XTring_bell;
14203 reset_terminal_modes_hook = XTreset_terminal_modes;
14204 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
14205 update_begin_hook = x_update_begin;
14206 update_end_hook = x_update_end;
dc6f92b8
JB
14207 set_terminal_window_hook = XTset_terminal_window;
14208 read_socket_hook = XTread_socket;
b8009dd1 14209 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 14210 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 14211 mouse_position_hook = XTmouse_position;
f451eb13 14212 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 14213 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
14214 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14215 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14216 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14217 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 14218 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 14219
f676886a 14220 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 14221 char_ins_del_ok = 1;
dc6f92b8
JB
14222 line_ins_del_ok = 1; /* we'll just blt 'em */
14223 fast_clear_end_of_line = 1; /* X does this well */
58769bee 14224 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
14225 off the bottom */
14226 baud_rate = 19200;
14227
7a13e894 14228 x_noop_count = 0;
9ea173e8 14229 last_tool_bar_item = -1;
06a2c219
GM
14230 any_help_event_p = 0;
14231
b30b24cb
RS
14232 /* Try to use interrupt input; if we can't, then start polling. */
14233 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14234
7f9c7f94
RS
14235#ifdef USE_X_TOOLKIT
14236 XtToolkitInitialize ();
14237 Xt_app_con = XtCreateApplicationContext ();
665881ad 14238 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
14239
14240 /* Install an asynchronous timer that processes Xt timeout events
14241 every 0.1s. This is necessary because some widget sets use
14242 timeouts internally, for example the LessTif menu bar, or the
14243 Xaw3d scroll bar. When Xt timouts aren't processed, these
14244 widgets don't behave normally. */
14245 {
14246 EMACS_TIME interval;
14247 EMACS_SET_SECS_USECS (interval, 0, 100000);
14248 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14249 }
db74249b 14250#endif
bffcfca9 14251
eccc05db 14252#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
14253 xaw3d_arrow_scroll = False;
14254 xaw3d_pick_top = True;
7f9c7f94
RS
14255#endif
14256
58769bee 14257 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 14258 original error handler. */
e99db5a1 14259 XSetErrorHandler (x_error_handler);
334208b7 14260 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 14261
06a2c219 14262 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
14263#ifdef SIGWINCH
14264 signal (SIGWINCH, SIG_DFL);
c118dd06 14265#endif /* ! defined (SIGWINCH) */
dc6f92b8 14266
92e2441b 14267 signal (SIGPIPE, x_connection_signal);
dc6f92b8 14268}
55123275 14269
06a2c219 14270
55123275
JB
14271void
14272syms_of_xterm ()
14273{
e99db5a1
RS
14274 staticpro (&x_error_message_string);
14275 x_error_message_string = Qnil;
14276
7a13e894
RS
14277 staticpro (&x_display_name_list);
14278 x_display_name_list = Qnil;
334208b7 14279
ab648270 14280 staticpro (&last_mouse_scroll_bar);
e53cb100 14281 last_mouse_scroll_bar = Qnil;
59e755be
KH
14282
14283 staticpro (&Qvendor_specific_keysyms);
14284 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
14285
14286 staticpro (&last_mouse_press_frame);
14287 last_mouse_press_frame = Qnil;
06a2c219 14288
06a2c219 14289 help_echo = Qnil;
be010514
GM
14290 staticpro (&help_echo);
14291 help_echo_object = Qnil;
14292 staticpro (&help_echo_object);
7cea38bc
GM
14293 help_echo_window = Qnil;
14294 staticpro (&help_echo_window);
06a2c219 14295 previous_help_echo = Qnil;
be010514
GM
14296 staticpro (&previous_help_echo);
14297 help_echo_pos = -1;
06a2c219
GM
14298
14299 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14300 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14301For example, if a block cursor is over a tab, it will be drawn as\n\
14302wide as that tab on the display.");
14303 x_stretch_cursor_p = 0;
14304
5bf04520
GM
14305 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14306 "What X toolkit scroll bars Emacs uses.\n\
14307A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14308Otherwise, value is a symbol describing the X toolkit.");
eccc05db 14309#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
14310#ifdef USE_MOTIF
14311 Vx_toolkit_scroll_bars = intern ("motif");
14312#elif defined HAVE_XAW3D
14313 Vx_toolkit_scroll_bars = intern ("xaw3d");
14314#else
14315 Vx_toolkit_scroll_bars = intern ("xaw");
14316#endif
06a2c219 14317#else
5bf04520 14318 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
14319#endif
14320
06a2c219
GM
14321 staticpro (&last_mouse_motion_frame);
14322 last_mouse_motion_frame = Qnil;
55123275 14323}
6cf0ae86
RS
14324
14325#endif /* not HAVE_X_WINDOWS */