(comment-normalize-vars): Use [ \t] for the trailing
[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 *));
408static void set_output_cursor P_ ((struct cursor_pos *));
409static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
410 int *, int *, int *));
411static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 412static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
413static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
414static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
415static void show_mouse_face P_ ((struct x_display_info *,
416 enum draw_glyphs_face));
417static int x_io_error_quitter P_ ((Display *));
418int x_catch_errors P_ ((Display *));
419void x_uncatch_errors P_ ((Display *, int));
420void x_lower_frame P_ ((struct frame *));
421void x_scroll_bar_clear P_ ((struct frame *));
422int x_had_errors_p P_ ((Display *));
423void x_wm_set_size_hint P_ ((struct frame *, long, int));
424void x_raise_frame P_ ((struct frame *));
425void x_set_window_size P_ ((struct frame *, int, int, int));
426void x_wm_set_window_state P_ ((struct frame *, int));
427void x_wm_set_icon_pixmap P_ ((struct frame *, int));
428void x_initialize P_ ((void));
429static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
430static int x_compute_min_glyph_bounds P_ ((struct frame *));
431static void x_draw_phys_cursor_glyph P_ ((struct window *,
432 struct glyph_row *,
433 enum draw_glyphs_face));
434static void x_update_end P_ ((struct frame *));
435static void XTframe_up_to_date P_ ((struct frame *));
436static void XTreassert_line_highlight P_ ((int, int));
437static void x_change_line_highlight P_ ((int, int, int, int));
438static void XTset_terminal_modes P_ ((void));
439static void XTreset_terminal_modes P_ ((void));
440static void XTcursor_to P_ ((int, int, int, int));
441static void x_write_glyphs P_ ((struct glyph *, int));
442static void x_clear_end_of_line P_ ((int));
443static void x_clear_frame P_ ((void));
444static void x_clear_cursor P_ ((struct window *));
445static void frame_highlight P_ ((struct frame *));
446static void frame_unhighlight P_ ((struct frame *));
447static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
448static void XTframe_rehighlight P_ ((struct frame *));
449static void x_frame_rehighlight P_ ((struct x_display_info *));
450static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 451static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
452static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
453 XRectangle *));
454static void expose_frame P_ ((struct frame *, int, int, int, int));
455static void expose_window_tree P_ ((struct window *, XRectangle *));
456static void expose_window P_ ((struct window *, XRectangle *));
457static void expose_area P_ ((struct window *, struct glyph_row *,
458 XRectangle *, enum glyph_row_area));
459static void expose_line P_ ((struct window *, struct glyph_row *,
460 XRectangle *));
461static void x_update_cursor_in_window_tree P_ ((struct window *, int));
462static void x_update_window_cursor P_ ((struct window *, int));
463static void x_erase_phys_cursor P_ ((struct window *));
464void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
465static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
466 enum bitmap_type));
467
468static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
469 GC, int));
470static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
471static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
472static void note_overwritten_text_cursor P_ ((struct window *, int, int));
473static void x_flush P_ ((struct frame *f));
952291d9
GM
474static void x_update_begin P_ ((struct frame *));
475static void x_update_window_begin P_ ((struct window *));
476static void x_draw_vertical_border P_ ((struct window *));
477static void x_after_update_window_line P_ ((struct glyph_row *));
478static INLINE void take_vertical_position_into_account P_ ((struct it *));
479static void x_produce_stretch_glyph P_ ((struct it *));
480
06a2c219
GM
481
482/* Flush display of frame F, or of all frames if F is null. */
483
484static void
485x_flush (f)
486 struct frame *f;
487{
488 BLOCK_INPUT;
489 if (f == NULL)
490 {
491 Lisp_Object rest, frame;
492 FOR_EACH_FRAME (rest, frame)
493 x_flush (XFRAME (frame));
494 }
495 else if (FRAME_X_P (f))
496 XFlush (FRAME_X_DISPLAY (f));
497 UNBLOCK_INPUT;
498}
499
dc6f92b8 500
06a2c219
GM
501/* Remove calls to XFlush by defining XFlush to an empty replacement.
502 Calls to XFlush should be unnecessary because the X output buffer
503 is flushed automatically as needed by calls to XPending,
504 XNextEvent, or XWindowEvent according to the XFlush man page.
505 XTread_socket calls XPending. Removing XFlush improves
506 performance. */
507
508#define XFlush(DISPLAY) (void) 0
b8009dd1 509
334208b7 510\f
06a2c219
GM
511/***********************************************************************
512 Debugging
513 ***********************************************************************/
514
9382638d 515#if 0
06a2c219
GM
516
517/* This is a function useful for recording debugging information about
518 the sequence of occurrences in this file. */
9382638d
KH
519
520struct record
521{
522 char *locus;
523 int type;
524};
525
526struct record event_record[100];
527
528int event_record_index;
529
530record_event (locus, type)
531 char *locus;
532 int type;
533{
534 if (event_record_index == sizeof (event_record) / sizeof (struct record))
535 event_record_index = 0;
536
537 event_record[event_record_index].locus = locus;
538 event_record[event_record_index].type = type;
539 event_record_index++;
540}
541
542#endif /* 0 */
06a2c219
GM
543
544
9382638d 545\f
334208b7
RS
546/* Return the struct x_display_info corresponding to DPY. */
547
548struct x_display_info *
549x_display_info_for_display (dpy)
550 Display *dpy;
551{
552 struct x_display_info *dpyinfo;
553
554 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
555 if (dpyinfo->display == dpy)
556 return dpyinfo;
16bd92ea 557
334208b7
RS
558 return 0;
559}
f451eb13 560
06a2c219
GM
561
562\f
563/***********************************************************************
564 Starting and ending an update
565 ***********************************************************************/
566
567/* Start an update of frame F. This function is installed as a hook
568 for update_begin, i.e. it is called when update_begin is called.
569 This function is called prior to calls to x_update_window_begin for
570 each window being updated. Currently, there is nothing to do here
571 because all interesting stuff is done on a window basis. */
dc6f92b8 572
dfcf069d 573static void
06a2c219 574x_update_begin (f)
f676886a 575 struct frame *f;
58769bee 576{
06a2c219
GM
577 /* Nothing to do. */
578}
dc6f92b8 579
dc6f92b8 580
06a2c219
GM
581/* Start update of window W. Set the global variable updated_window
582 to the window being updated and set output_cursor to the cursor
583 position of W. */
dc6f92b8 584
06a2c219
GM
585static void
586x_update_window_begin (w)
587 struct window *w;
588{
589 struct frame *f = XFRAME (WINDOW_FRAME (w));
590 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
591
592 updated_window = w;
593 set_output_cursor (&w->cursor);
b8009dd1 594
06a2c219 595 BLOCK_INPUT;
d1bc4182 596
06a2c219 597 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 598 {
514e4681 599 /* Don't do highlighting for mouse motion during the update. */
06a2c219 600 display_info->mouse_face_defer = 1;
37c2c98b 601
06a2c219
GM
602 /* If F needs to be redrawn, simply forget about any prior mouse
603 highlighting. */
9f67f20b 604 if (FRAME_GARBAGED_P (f))
06a2c219
GM
605 display_info->mouse_face_window = Qnil;
606
64f26cf5
GM
607#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
608 their mouse_face_p flag set, which means that they are always
609 unequal to rows in a desired matrix which never have that
610 flag set. So, rows containing mouse-face glyphs are never
611 scrolled, and we don't have to switch the mouse highlight off
612 here to prevent it from being scrolled. */
613
06a2c219
GM
614 /* Can we tell that this update does not affect the window
615 where the mouse highlight is? If so, no need to turn off.
616 Likewise, don't do anything if the frame is garbaged;
617 in that case, the frame's current matrix that we would use
618 is all wrong, and we will redisplay that line anyway. */
619 if (!NILP (display_info->mouse_face_window)
620 && w == XWINDOW (display_info->mouse_face_window))
514e4681 621 {
06a2c219 622 int i;
514e4681 623
06a2c219
GM
624 for (i = 0; i < w->desired_matrix->nrows; ++i)
625 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
626 break;
627
06a2c219
GM
628 if (i < w->desired_matrix->nrows)
629 clear_mouse_face (display_info);
514e4681 630 }
64f26cf5 631#endif /* 0 */
b8009dd1 632 }
6ccf47d1 633
dc6f92b8
JB
634 UNBLOCK_INPUT;
635}
636
06a2c219
GM
637
638/* Draw a vertical window border to the right of window W if W doesn't
639 have vertical scroll bars. */
640
dfcf069d 641static void
06a2c219
GM
642x_draw_vertical_border (w)
643 struct window *w;
58769bee 644{
06a2c219
GM
645 struct frame *f = XFRAME (WINDOW_FRAME (w));
646
647 /* Redraw borders between horizontally adjacent windows. Don't
648 do it for frames with vertical scroll bars because either the
649 right scroll bar of a window, or the left scroll bar of its
650 neighbor will suffice as a border. */
651 if (!WINDOW_RIGHTMOST_P (w)
652 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
653 {
654 int x0, x1, y0, y1;
dc6f92b8 655
06a2c219 656 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 657 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
658 y1 -= 1;
659
660 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
661 f->output_data.x->normal_gc, x1, y0, x1, y1);
662 }
663}
664
665
71b8321e
GM
666/* End update of window W (which is equal to updated_window).
667
668 Draw vertical borders between horizontally adjacent windows, and
669 display W's cursor if CURSOR_ON_P is non-zero.
670
671 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
672 glyphs in mouse-face were overwritten. In that case we have to
673 make sure that the mouse-highlight is properly redrawn.
674
675 W may be a menu bar pseudo-window in case we don't have X toolkit
676 support. Such windows don't have a cursor, so don't display it
677 here. */
06a2c219
GM
678
679static void
71b8321e 680x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 681 struct window *w;
71b8321e 682 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
683{
684 if (!w->pseudo_window_p)
685 {
71b8321e
GM
686 struct x_display_info *dpyinfo
687 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
688
06a2c219 689 BLOCK_INPUT;
71b8321e
GM
690
691 /* If a row with mouse-face was overwritten, arrange for
692 XTframe_up_to_date to redisplay the mouse highlight. */
693 if (mouse_face_overwritten_p)
694 {
695 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
696 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
697 dpyinfo->mouse_face_window = Qnil;
698 }
699
06a2c219
GM
700 if (cursor_on_p)
701 x_display_and_set_cursor (w, 1, output_cursor.hpos,
702 output_cursor.vpos,
703 output_cursor.x, output_cursor.y);
71b8321e 704
06a2c219
GM
705 x_draw_vertical_border (w);
706 UNBLOCK_INPUT;
707 }
708
709 updated_window = NULL;
710}
dc6f92b8 711
dc6f92b8 712
06a2c219
GM
713/* End update of frame F. This function is installed as a hook in
714 update_end. */
715
716static void
717x_update_end (f)
718 struct frame *f;
719{
720 /* Mouse highlight may be displayed again. */
aa8bff2e 721 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 722
06a2c219 723 BLOCK_INPUT;
334208b7 724 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
725 UNBLOCK_INPUT;
726}
b8009dd1 727
06a2c219
GM
728
729/* This function is called from various places in xdisp.c whenever a
730 complete update has been performed. The global variable
731 updated_window is not available here. */
b8009dd1 732
dfcf069d 733static void
b8009dd1 734XTframe_up_to_date (f)
06a2c219 735 struct frame *f;
b8009dd1 736{
06a2c219 737 if (FRAME_X_P (f))
514e4681 738 {
06a2c219 739 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 740
06a2c219
GM
741 if (dpyinfo->mouse_face_deferred_gc
742 || f == dpyinfo->mouse_face_mouse_frame)
743 {
744 BLOCK_INPUT;
745 if (dpyinfo->mouse_face_mouse_frame)
746 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
747 dpyinfo->mouse_face_mouse_x,
748 dpyinfo->mouse_face_mouse_y);
749 dpyinfo->mouse_face_deferred_gc = 0;
750 UNBLOCK_INPUT;
751 }
514e4681 752 }
b8009dd1 753}
06a2c219
GM
754
755
756/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
757 arrow bitmaps, or clear the areas where they would be displayed
758 before DESIRED_ROW is made current. The window being updated is
759 found in updated_window. This function It is called from
760 update_window_line only if it is known that there are differences
761 between bitmaps to be drawn between current row and DESIRED_ROW. */
762
763static void
764x_after_update_window_line (desired_row)
765 struct glyph_row *desired_row;
766{
767 struct window *w = updated_window;
768
769 xassert (w);
770
771 if (!desired_row->mode_line_p && !w->pseudo_window_p)
772 {
c5e6e06b
GM
773 struct frame *f;
774 int width;
775
06a2c219
GM
776 BLOCK_INPUT;
777 x_draw_row_bitmaps (w, desired_row);
778
779 /* When a window has disappeared, make sure that no rest of
780 full-width rows stays visible in the internal border. */
c5e6e06b
GM
781 if (windows_or_buffers_changed
782 && (f = XFRAME (w->frame),
783 width = FRAME_INTERNAL_BORDER_WIDTH (f),
784 width != 0))
06a2c219 785 {
06a2c219 786 int height = desired_row->visible_height;
110859fc
GM
787 int x = (window_box_right (w, -1)
788 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
789 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
790
c5e6e06b
GM
791 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
792 x, y, width, height, False);
06a2c219
GM
793 }
794
795 UNBLOCK_INPUT;
796 }
797}
798
799
800/* Draw the bitmap WHICH in one of the areas to the left or right of
801 window W. ROW is the glyph row for which to display the bitmap; it
802 determines the vertical position at which the bitmap has to be
803 drawn. */
804
805static void
806x_draw_bitmap (w, row, which)
807 struct window *w;
808 struct glyph_row *row;
809 enum bitmap_type which;
810{
811 struct frame *f = XFRAME (WINDOW_FRAME (w));
812 Display *display = FRAME_X_DISPLAY (f);
813 Window window = FRAME_X_WINDOW (f);
814 int x, y, wd, h, dy;
815 unsigned char *bits;
816 Pixmap pixmap;
817 GC gc = f->output_data.x->normal_gc;
818 struct face *face;
819 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
820
821 /* Must clip because of partially visible lines. */
822 x_clip_to_row (w, row, gc, 1);
823
824 switch (which)
825 {
826 case LEFT_TRUNCATION_BITMAP:
827 wd = left_width;
828 h = left_height;
829 bits = left_bits;
830 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
831 - wd
110859fc 832 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
833 break;
834
835 case OVERLAY_ARROW_BITMAP:
836 wd = left_width;
837 h = left_height;
838 bits = ov_bits;
839 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
840 - wd
110859fc 841 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
842 break;
843
844 case RIGHT_TRUNCATION_BITMAP:
845 wd = right_width;
846 h = right_height;
847 bits = right_bits;
848 x = window_box_right (w, -1);
110859fc 849 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
850 break;
851
852 case CONTINUED_LINE_BITMAP:
853 wd = right_width;
854 h = right_height;
855 bits = continued_bits;
856 x = window_box_right (w, -1);
110859fc 857 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
858 break;
859
860 case CONTINUATION_LINE_BITMAP:
861 wd = continuation_width;
862 h = continuation_height;
863 bits = continuation_bits;
864 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
865 - wd
110859fc 866 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
867 break;
868
869 case ZV_LINE_BITMAP:
870 wd = zv_width;
871 h = zv_height;
872 bits = zv_bits;
873 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
874 - wd
110859fc 875 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
876 break;
877
878 default:
879 abort ();
880 }
881
882 /* Convert to frame coordinates. Set dy to the offset in the row to
883 start drawing the bitmap. */
884 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
885 dy = (row->height - h) / 2;
886
887 /* Draw the bitmap. I believe these small pixmaps can be cached
888 by the server. */
889 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
890 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
891 face->foreground,
892 face->background, depth);
893 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
894 XFreePixmap (display, pixmap);
895 XSetClipMask (display, gc, None);
896}
897
898
899/* Draw flags bitmaps for glyph row ROW on window W. Call this
900 function with input blocked. */
901
902static void
903x_draw_row_bitmaps (w, row)
904 struct window *w;
905 struct glyph_row *row;
906{
907 struct frame *f = XFRAME (w->frame);
908 enum bitmap_type bitmap;
909 struct face *face;
045dee35 910 int header_line_height = -1;
06a2c219
GM
911
912 xassert (interrupt_input_blocked);
913
914 /* If row is completely invisible, because of vscrolling, we
915 don't have to draw anything. */
916 if (row->visible_height <= 0)
917 return;
918
919 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
920 PREPARE_FACE_FOR_DISPLAY (f, face);
921
922 /* Decide which bitmap to draw at the left side. */
923 if (row->overlay_arrow_p)
924 bitmap = OVERLAY_ARROW_BITMAP;
925 else if (row->truncated_on_left_p)
926 bitmap = LEFT_TRUNCATION_BITMAP;
927 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
928 bitmap = CONTINUATION_LINE_BITMAP;
929 else if (row->indicate_empty_line_p)
930 bitmap = ZV_LINE_BITMAP;
931 else
932 bitmap = NO_BITMAP;
933
934 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
935 the flags area. */
936 if (bitmap == NO_BITMAP
110859fc 937 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
938 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
939 {
940 /* If W has a vertical border to its left, don't draw over it. */
941 int border = ((XFASTINT (w->left) > 0
942 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
943 ? 1 : 0);
944 int left = window_box_left (w, -1);
945
045dee35
GM
946 if (header_line_height < 0)
947 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
948
949 /* In case the same realized face is used for bitmap areas and
950 for something displayed in the text (e.g. face `region' on
951 mono-displays, the fill style may have been changed to
952 FillSolid in x_draw_glyph_string_background. */
953 if (face->stipple)
954 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
955 else
956 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
957
06a2c219
GM
958 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
959 face->gc,
960 (left
110859fc 961 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 962 + border),
045dee35 963 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 964 row->y)),
110859fc 965 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 966 row->visible_height);
dcd08bfb
GM
967 if (!face->stipple)
968 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
969 }
970
971 /* Draw the left bitmap. */
972 if (bitmap != NO_BITMAP)
973 x_draw_bitmap (w, row, bitmap);
974
975 /* Decide which bitmap to draw at the right side. */
976 if (row->truncated_on_right_p)
977 bitmap = RIGHT_TRUNCATION_BITMAP;
978 else if (row->continued_p)
979 bitmap = CONTINUED_LINE_BITMAP;
980 else
981 bitmap = NO_BITMAP;
982
983 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
984 the flags area. */
985 if (bitmap == NO_BITMAP
110859fc 986 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
987 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
988 {
989 int right = window_box_right (w, -1);
990
045dee35
GM
991 if (header_line_height < 0)
992 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
993
994 /* In case the same realized face is used for bitmap areas and
995 for something displayed in the text (e.g. face `region' on
996 mono-displays, the fill style may have been changed to
997 FillSolid in x_draw_glyph_string_background. */
998 if (face->stipple)
999 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1000 else
1001 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
1002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1003 face->gc,
1004 right,
045dee35 1005 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1006 row->y)),
110859fc 1007 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1008 row->visible_height);
dcd08bfb
GM
1009 if (!face->stipple)
1010 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1011 }
1012
1013 /* Draw the right bitmap. */
1014 if (bitmap != NO_BITMAP)
1015 x_draw_bitmap (w, row, bitmap);
1016}
1017
dc6f92b8 1018\f
06a2c219
GM
1019/***********************************************************************
1020 Line Highlighting
1021 ***********************************************************************/
dc6f92b8 1022
06a2c219
GM
1023/* External interface to control of standout mode. Not used for X
1024 frames. Aborts when called. */
1025
1026static void
dc6f92b8
JB
1027XTreassert_line_highlight (new, vpos)
1028 int new, vpos;
1029{
06a2c219 1030 abort ();
dc6f92b8
JB
1031}
1032
06a2c219
GM
1033
1034/* Call this when about to modify line at position VPOS and change
1035 whether it is highlighted. Not used for X frames. Aborts when
1036 called. */
dc6f92b8 1037
dfcf069d 1038static void
06a2c219
GM
1039x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1040 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1041{
06a2c219 1042 abort ();
dc6f92b8
JB
1043}
1044
06a2c219
GM
1045
1046/* This is called when starting Emacs and when restarting after
1047 suspend. When starting Emacs, no X window is mapped. And nothing
1048 must be done to Emacs's own window if it is suspended (though that
1049 rarely happens). */
dc6f92b8 1050
dfcf069d 1051static void
dc6f92b8
JB
1052XTset_terminal_modes ()
1053{
1054}
1055
06a2c219
GM
1056/* This is called when exiting or suspending Emacs. Exiting will make
1057 the X-windows go away, and suspending requires no action. */
dc6f92b8 1058
dfcf069d 1059static void
dc6f92b8
JB
1060XTreset_terminal_modes ()
1061{
dc6f92b8 1062}
06a2c219
GM
1063
1064
dc6f92b8 1065\f
06a2c219
GM
1066/***********************************************************************
1067 Output Cursor
1068 ***********************************************************************/
1069
1070/* Set the global variable output_cursor to CURSOR. All cursor
1071 positions are relative to updated_window. */
dc6f92b8 1072
dfcf069d 1073static void
06a2c219
GM
1074set_output_cursor (cursor)
1075 struct cursor_pos *cursor;
dc6f92b8 1076{
06a2c219
GM
1077 output_cursor.hpos = cursor->hpos;
1078 output_cursor.vpos = cursor->vpos;
1079 output_cursor.x = cursor->x;
1080 output_cursor.y = cursor->y;
1081}
1082
1083
1084/* Set a nominal cursor position.
dc6f92b8 1085
06a2c219
GM
1086 HPOS and VPOS are column/row positions in a window glyph matrix. X
1087 and Y are window text area relative pixel positions.
1088
1089 If this is done during an update, updated_window will contain the
1090 window that is being updated and the position is the future output
1091 cursor position for that window. If updated_window is null, use
1092 selected_window and display the cursor at the given position. */
1093
1094static void
1095XTcursor_to (vpos, hpos, y, x)
1096 int vpos, hpos, y, x;
1097{
1098 struct window *w;
1099
1100 /* If updated_window is not set, work on selected_window. */
1101 if (updated_window)
1102 w = updated_window;
1103 else
1104 w = XWINDOW (selected_window);
dbcb258a 1105
06a2c219
GM
1106 /* Set the output cursor. */
1107 output_cursor.hpos = hpos;
1108 output_cursor.vpos = vpos;
1109 output_cursor.x = x;
1110 output_cursor.y = y;
dc6f92b8 1111
06a2c219
GM
1112 /* If not called as part of an update, really display the cursor.
1113 This will also set the cursor position of W. */
1114 if (updated_window == NULL)
dc6f92b8
JB
1115 {
1116 BLOCK_INPUT;
06a2c219 1117 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1118 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1119 UNBLOCK_INPUT;
1120 }
1121}
dc43ef94 1122
06a2c219
GM
1123
1124\f
1125/***********************************************************************
1126 Display Iterator
1127 ***********************************************************************/
1128
1129/* Function prototypes of this page. */
1130
1131static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1132 struct glyph *,
ee569018
KH
1133 XChar2b *,
1134 int *));
06a2c219
GM
1135static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1136 int, XChar2b *, int));
1137static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1138static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1139static void x_append_glyph P_ ((struct it *));
b4192550 1140static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1141static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1142 int, int, double));
1143static void x_produce_glyphs P_ ((struct it *));
06a2c219 1144static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1145
1146
e2ef8ee6
GM
1147/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1148 is not contained in the font. */
dc43ef94 1149
06a2c219 1150static INLINE XCharStruct *
ee569018 1151x_per_char_metric (font, char2b)
06a2c219
GM
1152 XFontStruct *font;
1153 XChar2b *char2b;
1154{
1155 /* The result metric information. */
1156 XCharStruct *pcm = NULL;
dc6f92b8 1157
06a2c219 1158 xassert (font && char2b);
dc6f92b8 1159
06a2c219 1160 if (font->per_char != NULL)
dc6f92b8 1161 {
06a2c219 1162 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1163 {
06a2c219
GM
1164 /* min_char_or_byte2 specifies the linear character index
1165 corresponding to the first element of the per_char array,
1166 max_char_or_byte2 is the index of the last character. A
1167 character with non-zero CHAR2B->byte1 is not in the font.
1168 A character with byte2 less than min_char_or_byte2 or
1169 greater max_char_or_byte2 is not in the font. */
1170 if (char2b->byte1 == 0
1171 && char2b->byte2 >= font->min_char_or_byte2
1172 && char2b->byte2 <= font->max_char_or_byte2)
1173 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1174 }
06a2c219 1175 else
dc6f92b8 1176 {
06a2c219
GM
1177 /* If either min_byte1 or max_byte1 are nonzero, both
1178 min_char_or_byte2 and max_char_or_byte2 are less than
1179 256, and the 2-byte character index values corresponding
1180 to the per_char array element N (counting from 0) are:
1181
1182 byte1 = N/D + min_byte1
1183 byte2 = N\D + min_char_or_byte2
1184
1185 where:
1186
1187 D = max_char_or_byte2 - min_char_or_byte2 + 1
1188 / = integer division
1189 \ = integer modulus */
1190 if (char2b->byte1 >= font->min_byte1
1191 && char2b->byte1 <= font->max_byte1
1192 && char2b->byte2 >= font->min_char_or_byte2
1193 && char2b->byte2 <= font->max_char_or_byte2)
1194 {
1195 pcm = (font->per_char
1196 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1197 * (char2b->byte1 - font->min_byte1))
1198 + (char2b->byte2 - font->min_char_or_byte2));
1199 }
dc6f92b8 1200 }
06a2c219
GM
1201 }
1202 else
1203 {
1204 /* If the per_char pointer is null, all glyphs between the first
1205 and last character indexes inclusive have the same
1206 information, as given by both min_bounds and max_bounds. */
1207 if (char2b->byte2 >= font->min_char_or_byte2
1208 && char2b->byte2 <= font->max_char_or_byte2)
1209 pcm = &font->max_bounds;
1210 }
dc6f92b8 1211
ee569018 1212 return ((pcm == NULL
3e71d8f2 1213 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1214 ? NULL : pcm);
06a2c219 1215}
b73b6aaf 1216
57b03282 1217
06a2c219
GM
1218/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1219 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1220
06a2c219
GM
1221static INLINE void
1222x_encode_char (c, char2b, font_info)
1223 int c;
1224 XChar2b *char2b;
1225 struct font_info *font_info;
1226{
1227 int charset = CHAR_CHARSET (c);
1228 XFontStruct *font = font_info->font;
1229
1230 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1231 This may be either a program in a special encoder language or a
1232 fixed encoding. */
1233 if (font_info->font_encoder)
1234 {
1235 /* It's a program. */
1236 struct ccl_program *ccl = font_info->font_encoder;
1237
1238 if (CHARSET_DIMENSION (charset) == 1)
1239 {
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte2;
1242 }
1243 else
1244 {
1245 ccl->reg[0] = charset;
1246 ccl->reg[1] = char2b->byte1;
1247 ccl->reg[2] = char2b->byte2;
1248 }
1249
1250 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1251
1252 /* We assume that MSBs are appropriately set/reset by CCL
1253 program. */
1254 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1255 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1256 else
1257 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1258 }
1259 else if (font_info->encoding[charset])
1260 {
1261 /* Fixed encoding scheme. See fontset.h for the meaning of the
1262 encoding numbers. */
1263 int enc = font_info->encoding[charset];
1264
1265 if ((enc == 1 || enc == 2)
1266 && CHARSET_DIMENSION (charset) == 2)
1267 char2b->byte1 |= 0x80;
1268
1269 if (enc == 1 || enc == 3)
1270 char2b->byte2 |= 0x80;
1271 }
1272}
1273
1274
1275/* Get face and two-byte form of character C in face FACE_ID on frame
1276 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1277 means we want to display multibyte text. Value is a pointer to a
1278 realized face that is ready for display. */
1279
1280static INLINE struct face *
1281x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1282 struct frame *f;
1283 int c, face_id;
1284 XChar2b *char2b;
1285 int multibyte_p;
1286{
1287 struct face *face = FACE_FROM_ID (f, face_id);
1288
1289 if (!multibyte_p)
1290 {
1291 /* Unibyte case. We don't have to encode, but we have to make
1292 sure to use a face suitable for unibyte. */
1293 char2b->byte1 = 0;
1294 char2b->byte2 = c;
ee569018
KH
1295 face_id = FACE_FOR_CHAR (f, face, c);
1296 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1297 }
1298 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1299 {
1300 /* Case of ASCII in a face known to fit ASCII. */
1301 char2b->byte1 = 0;
1302 char2b->byte2 = c;
1303 }
1304 else
1305 {
1306 int c1, c2, charset;
1307
1308 /* Split characters into bytes. If c2 is -1 afterwards, C is
1309 really a one-byte character so that byte1 is zero. */
1310 SPLIT_CHAR (c, charset, c1, c2);
1311 if (c2 > 0)
1312 char2b->byte1 = c1, char2b->byte2 = c2;
1313 else
1314 char2b->byte1 = 0, char2b->byte2 = c1;
1315
06a2c219 1316 /* Maybe encode the character in *CHAR2B. */
ee569018 1317 if (face->font != NULL)
06a2c219
GM
1318 {
1319 struct font_info *font_info
1320 = FONT_INFO_FROM_ID (f, face->font_info_id);
1321 if (font_info)
ee569018 1322 x_encode_char (c, char2b, font_info);
06a2c219
GM
1323 }
1324 }
1325
1326 /* Make sure X resources of the face are allocated. */
1327 xassert (face != NULL);
1328 PREPARE_FACE_FOR_DISPLAY (f, face);
1329
1330 return face;
1331}
1332
1333
1334/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1335 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1336 a pointer to a realized face that is ready for display. */
1337
1338static INLINE struct face *
ee569018 1339x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1340 struct frame *f;
1341 struct glyph *glyph;
1342 XChar2b *char2b;
ee569018 1343 int *two_byte_p;
06a2c219
GM
1344{
1345 struct face *face;
1346
1347 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1348 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1349
ee569018
KH
1350 if (two_byte_p)
1351 *two_byte_p = 0;
1352
06a2c219
GM
1353 if (!glyph->multibyte_p)
1354 {
1355 /* Unibyte case. We don't have to encode, but we have to make
1356 sure to use a face suitable for unibyte. */
1357 char2b->byte1 = 0;
43d120d8 1358 char2b->byte2 = glyph->u.ch;
06a2c219 1359 }
43d120d8
KH
1360 else if (glyph->u.ch < 128
1361 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1362 {
1363 /* Case of ASCII in a face known to fit ASCII. */
1364 char2b->byte1 = 0;
43d120d8 1365 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1366 }
1367 else
1368 {
1369 int c1, c2, charset;
1370
1371 /* Split characters into bytes. If c2 is -1 afterwards, C is
1372 really a one-byte character so that byte1 is zero. */
43d120d8 1373 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1374 if (c2 > 0)
1375 char2b->byte1 = c1, char2b->byte2 = c2;
1376 else
1377 char2b->byte1 = 0, char2b->byte2 = c1;
1378
1379 /* Maybe encode the character in *CHAR2B. */
1380 if (charset != CHARSET_ASCII)
1381 {
1382 struct font_info *font_info
1383 = FONT_INFO_FROM_ID (f, face->font_info_id);
1384 if (font_info)
1385 {
43d120d8 1386 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1387 if (two_byte_p)
1388 *two_byte_p
1389 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1390 }
1391 }
1392 }
1393
1394 /* Make sure X resources of the face are allocated. */
1395 xassert (face != NULL);
1396 PREPARE_FACE_FOR_DISPLAY (f, face);
1397 return face;
1398}
1399
1400
1401/* Store one glyph for IT->char_to_display in IT->glyph_row.
1402 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1403
1404static INLINE void
1405x_append_glyph (it)
1406 struct it *it;
1407{
1408 struct glyph *glyph;
1409 enum glyph_row_area area = it->area;
1410
1411 xassert (it->glyph_row);
1412 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1413
1414 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1415 if (glyph < it->glyph_row->glyphs[area + 1])
1416 {
06a2c219
GM
1417 glyph->charpos = CHARPOS (it->position);
1418 glyph->object = it->object;
88d75730 1419 glyph->pixel_width = it->pixel_width;
06a2c219 1420 glyph->voffset = it->voffset;
88d75730 1421 glyph->type = CHAR_GLYPH;
06a2c219 1422 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1423 glyph->left_box_line_p = it->start_of_box_run_p;
1424 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1425 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1426 || it->phys_descent > it->descent);
88d75730 1427 glyph->padding_p = 0;
ee569018 1428 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1429 glyph->face_id = it->face_id;
1430 glyph->u.ch = it->char_to_display;
06a2c219
GM
1431 ++it->glyph_row->used[area];
1432 }
1433}
1434
b4192550
KH
1435/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1436 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1437
1438static INLINE void
1439x_append_composite_glyph (it)
1440 struct it *it;
1441{
1442 struct glyph *glyph;
1443 enum glyph_row_area area = it->area;
1444
1445 xassert (it->glyph_row);
1446
1447 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1448 if (glyph < it->glyph_row->glyphs[area + 1])
1449 {
b4192550
KH
1450 glyph->charpos = CHARPOS (it->position);
1451 glyph->object = it->object;
88d75730 1452 glyph->pixel_width = it->pixel_width;
b4192550 1453 glyph->voffset = it->voffset;
88d75730 1454 glyph->type = COMPOSITE_GLYPH;
b4192550 1455 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1456 glyph->left_box_line_p = it->start_of_box_run_p;
1457 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1458 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1459 || it->phys_descent > it->descent);
88d75730
GM
1460 glyph->padding_p = 0;
1461 glyph->glyph_not_available_p = 0;
1462 glyph->face_id = it->face_id;
1463 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1464 ++it->glyph_row->used[area];
1465 }
1466}
1467
06a2c219
GM
1468
1469/* Change IT->ascent and IT->height according to the setting of
1470 IT->voffset. */
1471
1472static INLINE void
1473take_vertical_position_into_account (it)
1474 struct it *it;
1475{
1476 if (it->voffset)
1477 {
1478 if (it->voffset < 0)
1479 /* Increase the ascent so that we can display the text higher
1480 in the line. */
1481 it->ascent += abs (it->voffset);
1482 else
1483 /* Increase the descent so that we can display the text lower
1484 in the line. */
1485 it->descent += it->voffset;
1486 }
1487}
1488
1489
1490/* Produce glyphs/get display metrics for the image IT is loaded with.
1491 See the description of struct display_iterator in dispextern.h for
1492 an overview of struct display_iterator. */
1493
1494static void
1495x_produce_image_glyph (it)
1496 struct it *it;
1497{
1498 struct image *img;
1499 struct face *face;
1500
1501 xassert (it->what == IT_IMAGE);
1502
1503 face = FACE_FROM_ID (it->f, it->face_id);
1504 img = IMAGE_FROM_ID (it->f, it->image_id);
1505 xassert (img);
1506
1507 /* Make sure X resources of the face and image are loaded. */
1508 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1509 prepare_image_for_display (it->f, img);
1510
95af8492 1511 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1512 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1513 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1514
1515 it->nglyphs = 1;
1516
1517 if (face->box != FACE_NO_BOX)
1518 {
ea2ba0d4
KH
1519 if (face->box_line_width > 0)
1520 {
1521 it->ascent += face->box_line_width;
1522 it->descent += face->box_line_width;
1523 }
06a2c219
GM
1524
1525 if (it->start_of_box_run_p)
ea2ba0d4 1526 it->pixel_width += abs (face->box_line_width);
06a2c219 1527 if (it->end_of_box_run_p)
ea2ba0d4 1528 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1529 }
1530
1531 take_vertical_position_into_account (it);
1532
1533 if (it->glyph_row)
1534 {
1535 struct glyph *glyph;
1536 enum glyph_row_area area = it->area;
1537
1538 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1539 if (glyph < it->glyph_row->glyphs[area + 1])
1540 {
06a2c219
GM
1541 glyph->charpos = CHARPOS (it->position);
1542 glyph->object = it->object;
88d75730 1543 glyph->pixel_width = it->pixel_width;
06a2c219 1544 glyph->voffset = it->voffset;
88d75730 1545 glyph->type = IMAGE_GLYPH;
06a2c219 1546 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1547 glyph->left_box_line_p = it->start_of_box_run_p;
1548 glyph->right_box_line_p = it->end_of_box_run_p;
1549 glyph->overlaps_vertically_p = 0;
1550 glyph->padding_p = 0;
1551 glyph->glyph_not_available_p = 0;
1552 glyph->face_id = it->face_id;
1553 glyph->u.img_id = img->id;
06a2c219
GM
1554 ++it->glyph_row->used[area];
1555 }
1556 }
1557}
1558
1559
1560/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1561 of the glyph, WIDTH and HEIGHT are the width and height of the
1562 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1563 ascent of the glyph (0 <= ASCENT <= 1). */
1564
1565static void
1566x_append_stretch_glyph (it, object, width, height, ascent)
1567 struct it *it;
1568 Lisp_Object object;
1569 int width, height;
1570 double ascent;
1571{
1572 struct glyph *glyph;
1573 enum glyph_row_area area = it->area;
1574
1575 xassert (ascent >= 0 && ascent <= 1);
1576
1577 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1578 if (glyph < it->glyph_row->glyphs[area + 1])
1579 {
06a2c219
GM
1580 glyph->charpos = CHARPOS (it->position);
1581 glyph->object = object;
88d75730 1582 glyph->pixel_width = width;
06a2c219 1583 glyph->voffset = it->voffset;
88d75730 1584 glyph->type = STRETCH_GLYPH;
06a2c219 1585 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1586 glyph->left_box_line_p = it->start_of_box_run_p;
1587 glyph->right_box_line_p = it->end_of_box_run_p;
1588 glyph->overlaps_vertically_p = 0;
1589 glyph->padding_p = 0;
1590 glyph->glyph_not_available_p = 0;
1591 glyph->face_id = it->face_id;
1592 glyph->u.stretch.ascent = height * ascent;
1593 glyph->u.stretch.height = height;
06a2c219
GM
1594 ++it->glyph_row->used[area];
1595 }
1596}
1597
1598
1599/* Produce a stretch glyph for iterator IT. IT->object is the value
1600 of the glyph property displayed. The value must be a list
1601 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1602 being recognized:
1603
1604 1. `:width WIDTH' specifies that the space should be WIDTH *
1605 canonical char width wide. WIDTH may be an integer or floating
1606 point number.
1607
1608 2. `:relative-width FACTOR' specifies that the width of the stretch
1609 should be computed from the width of the first character having the
1610 `glyph' property, and should be FACTOR times that width.
1611
1612 3. `:align-to HPOS' specifies that the space should be wide enough
1613 to reach HPOS, a value in canonical character units.
1614
1615 Exactly one of the above pairs must be present.
1616
1617 4. `:height HEIGHT' specifies that the height of the stretch produced
1618 should be HEIGHT, measured in canonical character units.
1619
1620 5. `:relative-height FACTOR' specifies that the height of the the
1621 stretch should be FACTOR times the height of the characters having
1622 the glyph property.
1623
1624 Either none or exactly one of 4 or 5 must be present.
1625
1626 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1627 of the stretch should be used for the ascent of the stretch.
1628 ASCENT must be in the range 0 <= ASCENT <= 100. */
1629
1630#define NUMVAL(X) \
1631 ((INTEGERP (X) || FLOATP (X)) \
1632 ? XFLOATINT (X) \
1633 : - 1)
1634
1635
1636static void
1637x_produce_stretch_glyph (it)
1638 struct it *it;
1639{
1640 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1641#if GLYPH_DEBUG
1642 extern Lisp_Object Qspace;
1643#endif
1644 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1645 extern Lisp_Object QCrelative_width, QCrelative_height;
1646 extern Lisp_Object QCalign_to;
1647 Lisp_Object prop, plist;
1648 double width = 0, height = 0, ascent = 0;
1649 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1650 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1651
1652 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1653
1654 /* List should start with `space'. */
1655 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1656 plist = XCDR (it->object);
1657
1658 /* Compute the width of the stretch. */
1659 if (prop = Fplist_get (plist, QCwidth),
1660 NUMVAL (prop) > 0)
1661 /* Absolute width `:width WIDTH' specified and valid. */
1662 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1663 else if (prop = Fplist_get (plist, QCrelative_width),
1664 NUMVAL (prop) > 0)
1665 {
1666 /* Relative width `:relative-width FACTOR' specified and valid.
1667 Compute the width of the characters having the `glyph'
1668 property. */
1669 struct it it2;
1670 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1671
1672 it2 = *it;
1673 if (it->multibyte_p)
1674 {
1675 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1676 - IT_BYTEPOS (*it));
1677 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1678 }
1679 else
1680 it2.c = *p, it2.len = 1;
1681
1682 it2.glyph_row = NULL;
1683 it2.what = IT_CHARACTER;
1684 x_produce_glyphs (&it2);
1685 width = NUMVAL (prop) * it2.pixel_width;
1686 }
1687 else if (prop = Fplist_get (plist, QCalign_to),
1688 NUMVAL (prop) > 0)
1689 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1690 else
1691 /* Nothing specified -> width defaults to canonical char width. */
1692 width = CANON_X_UNIT (it->f);
1693
1694 /* Compute height. */
1695 if (prop = Fplist_get (plist, QCheight),
1696 NUMVAL (prop) > 0)
1697 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1698 else if (prop = Fplist_get (plist, QCrelative_height),
1699 NUMVAL (prop) > 0)
1700 height = FONT_HEIGHT (font) * NUMVAL (prop);
1701 else
1702 height = FONT_HEIGHT (font);
1703
1704 /* Compute percentage of height used for ascent. If
1705 `:ascent ASCENT' is present and valid, use that. Otherwise,
1706 derive the ascent from the font in use. */
1707 if (prop = Fplist_get (plist, QCascent),
1708 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1709 ascent = NUMVAL (prop) / 100.0;
1710 else
1711 ascent = (double) font->ascent / FONT_HEIGHT (font);
1712
1713 if (width <= 0)
1714 width = 1;
1715 if (height <= 0)
1716 height = 1;
1717
1718 if (it->glyph_row)
1719 {
1720 Lisp_Object object = it->stack[it->sp - 1].string;
1721 if (!STRINGP (object))
1722 object = it->w->buffer;
1723 x_append_stretch_glyph (it, object, width, height, ascent);
1724 }
1725
1726 it->pixel_width = width;
66ac4b0e
GM
1727 it->ascent = it->phys_ascent = height * ascent;
1728 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1729 it->nglyphs = 1;
1730
1731 if (face->box != FACE_NO_BOX)
1732 {
ea2ba0d4
KH
1733 if (face->box_line_width > 0)
1734 {
1735 it->ascent += face->box_line_width;
1736 it->descent += face->box_line_width;
1737 }
06a2c219
GM
1738
1739 if (it->start_of_box_run_p)
ea2ba0d4 1740 it->pixel_width += abs (face->box_line_width);
06a2c219 1741 if (it->end_of_box_run_p)
ea2ba0d4 1742 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1743 }
1744
1745 take_vertical_position_into_account (it);
1746}
1747
b4192550
KH
1748/* Return proper value to be used as baseline offset of font that has
1749 ASCENT and DESCENT to draw characters by the font at the vertical
1750 center of the line of frame F.
1751
1752 Here, out task is to find the value of BOFF in the following figure;
1753
1754 -------------------------+-----------+-
1755 -+-+---------+-+ | |
1756 | | | | | |
1757 | | | | F_ASCENT F_HEIGHT
1758 | | | ASCENT | |
1759 HEIGHT | | | | |
1760 | | |-|-+------+-----------|------- baseline
1761 | | | | BOFF | |
1762 | |---------|-+-+ | |
1763 | | | DESCENT | |
1764 -+-+---------+-+ F_DESCENT |
1765 -------------------------+-----------+-
1766
1767 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1768 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1769 DESCENT = FONT->descent
1770 HEIGHT = FONT_HEIGHT (FONT)
1771 F_DESCENT = (F->output_data.x->font->descent
1772 - F->output_data.x->baseline_offset)
1773 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1774*/
1775
458f45fa
KH
1776#define VCENTER_BASELINE_OFFSET(FONT, F) \
1777 ((FONT)->descent \
1778 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1779 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1780 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1781
1782/* Produce glyphs/get display metrics for the display element IT is
1783 loaded with. See the description of struct display_iterator in
1784 dispextern.h for an overview of struct display_iterator. */
1785
1786static void
1787x_produce_glyphs (it)
1788 struct it *it;
1789{
ee569018
KH
1790 it->glyph_not_available_p = 0;
1791
06a2c219
GM
1792 if (it->what == IT_CHARACTER)
1793 {
1794 XChar2b char2b;
1795 XFontStruct *font;
ee569018 1796 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1797 XCharStruct *pcm;
06a2c219 1798 int font_not_found_p;
b4192550
KH
1799 struct font_info *font_info;
1800 int boff; /* baseline offset */
a4249304
KH
1801 /* We may change it->multibyte_p upon unibyte<->multibyte
1802 conversion. So, save the current value now and restore it
1803 later.
1804
1805 Note: It seems that we don't have to record multibyte_p in
1806 struct glyph because the character code itself tells if or
1807 not the character is multibyte. Thus, in the future, we must
1808 consider eliminating the field `multibyte_p' in the struct
1809 glyph.
1810 */
1811 int saved_multibyte_p = it->multibyte_p;
06a2c219 1812
ee569018
KH
1813 /* Maybe translate single-byte characters to multibyte, or the
1814 other way. */
06a2c219 1815 it->char_to_display = it->c;
ee569018 1816 if (!ASCII_BYTE_P (it->c))
06a2c219 1817 {
ee569018
KH
1818 if (unibyte_display_via_language_environment
1819 && SINGLE_BYTE_CHAR_P (it->c)
1820 && (it->c >= 0240
1821 || !NILP (Vnonascii_translation_table)))
1822 {
1823 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1824 it->multibyte_p = 1;
ee569018
KH
1825 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1826 face = FACE_FROM_ID (it->f, it->face_id);
1827 }
1828 else if (!SINGLE_BYTE_CHAR_P (it->c)
1829 && !it->multibyte_p)
1830 {
1831 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
a4249304 1832 it->multibyte_p = 0;
ee569018
KH
1833 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1834 face = FACE_FROM_ID (it->f, it->face_id);
1835 }
06a2c219
GM
1836 }
1837
ee569018
KH
1838 /* Get font to use. Encode IT->char_to_display. */
1839 x_get_char_face_and_encoding (it->f, it->char_to_display,
1840 it->face_id, &char2b,
1841 it->multibyte_p);
06a2c219
GM
1842 font = face->font;
1843
1844 /* When no suitable font found, use the default font. */
1845 font_not_found_p = font == NULL;
1846 if (font_not_found_p)
b4192550
KH
1847 {
1848 font = FRAME_FONT (it->f);
1849 boff = it->f->output_data.x->baseline_offset;
1850 font_info = NULL;
1851 }
1852 else
1853 {
1854 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1855 boff = font_info->baseline_offset;
1856 if (font_info->vertical_centering)
1857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1858 }
06a2c219
GM
1859
1860 if (it->char_to_display >= ' '
1861 && (!it->multibyte_p || it->char_to_display < 128))
1862 {
1863 /* Either unibyte or ASCII. */
1864 int stretched_p;
1865
1866 it->nglyphs = 1;
06a2c219
GM
1867
1868 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1869 it->ascent = font->ascent + boff;
1870 it->descent = font->descent - boff;
474848ac
GM
1871
1872 if (pcm)
1873 {
1874 it->phys_ascent = pcm->ascent + boff;
1875 it->phys_descent = pcm->descent - boff;
1876 it->pixel_width = pcm->width;
1877 }
1878 else
1879 {
1880 it->glyph_not_available_p = 1;
1881 it->phys_ascent = font->ascent + boff;
1882 it->phys_descent = font->descent - boff;
1883 it->pixel_width = FONT_WIDTH (font);
1884 }
06a2c219
GM
1885
1886 /* If this is a space inside a region of text with
1887 `space-width' property, change its width. */
1888 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1889 if (stretched_p)
1890 it->pixel_width *= XFLOATINT (it->space_width);
1891
1892 /* If face has a box, add the box thickness to the character
1893 height. If character has a box line to the left and/or
1894 right, add the box line width to the character's width. */
1895 if (face->box != FACE_NO_BOX)
1896 {
1897 int thick = face->box_line_width;
1898
ea2ba0d4
KH
1899 if (thick > 0)
1900 {
1901 it->ascent += thick;
1902 it->descent += thick;
1903 }
1904 else
1905 thick = -thick;
1906
06a2c219
GM
1907 if (it->start_of_box_run_p)
1908 it->pixel_width += thick;
1909 if (it->end_of_box_run_p)
1910 it->pixel_width += thick;
1911 }
1912
1913 /* If face has an overline, add the height of the overline
1914 (1 pixel) and a 1 pixel margin to the character height. */
1915 if (face->overline_p)
1916 it->ascent += 2;
1917
1918 take_vertical_position_into_account (it);
1919
1920 /* If we have to actually produce glyphs, do it. */
1921 if (it->glyph_row)
1922 {
1923 if (stretched_p)
1924 {
1925 /* Translate a space with a `space-width' property
1926 into a stretch glyph. */
1927 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1928 x_append_stretch_glyph (it, it->object, it->pixel_width,
1929 it->ascent + it->descent, ascent);
1930 }
1931 else
1932 x_append_glyph (it);
1933
1934 /* If characters with lbearing or rbearing are displayed
1935 in this line, record that fact in a flag of the
1936 glyph row. This is used to optimize X output code. */
1c7e22fd 1937 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1938 it->glyph_row->contains_overlapping_glyphs_p = 1;
1939 }
1940 }
1941 else if (it->char_to_display == '\n')
1942 {
1943 /* A newline has no width but we need the height of the line. */
1944 it->pixel_width = 0;
1945 it->nglyphs = 0;
b4192550
KH
1946 it->ascent = it->phys_ascent = font->ascent + boff;
1947 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1948
ea2ba0d4
KH
1949 if (face->box != FACE_NO_BOX
1950 && face->box_line_width > 0)
06a2c219 1951 {
ea2ba0d4
KH
1952 it->ascent += face->box_line_width;
1953 it->descent += face->box_line_width;
06a2c219
GM
1954 }
1955 }
1956 else if (it->char_to_display == '\t')
1957 {
1958 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1959 int x = it->current_x + it->continuation_lines_width;
06a2c219 1960 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1961
1962 /* If the distance from the current position to the next tab
1963 stop is less than a canonical character width, use the
1964 tab stop after that. */
1965 if (next_tab_x - x < CANON_X_UNIT (it->f))
1966 next_tab_x += tab_width;
06a2c219
GM
1967
1968 it->pixel_width = next_tab_x - x;
1969 it->nglyphs = 1;
b4192550
KH
1970 it->ascent = it->phys_ascent = font->ascent + boff;
1971 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1972
1973 if (it->glyph_row)
1974 {
1975 double ascent = (double) it->ascent / (it->ascent + it->descent);
1976 x_append_stretch_glyph (it, it->object, it->pixel_width,
1977 it->ascent + it->descent, ascent);
1978 }
1979 }
1980 else
1981 {
1982 /* A multi-byte character. Assume that the display width of the
1983 character is the width of the character multiplied by the
b4192550 1984 width of the font. */
06a2c219 1985
b4192550
KH
1986 /* If we found a font, this font should give us the right
1987 metrics. If we didn't find a font, use the frame's
1988 default font and calculate the width of the character
1989 from the charset width; this is what old redisplay code
1990 did. */
1991 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1992 if (font_not_found_p || !pcm)
1993 {
1994 int charset = CHAR_CHARSET (it->char_to_display);
1995
1996 it->glyph_not_available_p = 1;
1997 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1998 * CHARSET_WIDTH (charset));
1999 it->phys_ascent = font->ascent + boff;
2000 it->phys_descent = font->descent - boff;
2001 }
2002 else
2003 {
2004 it->pixel_width = pcm->width;
2005 it->phys_ascent = pcm->ascent + boff;
2006 it->phys_descent = pcm->descent - boff;
2007 if (it->glyph_row
2008 && (pcm->lbearing < 0
2009 || pcm->rbearing > pcm->width))
2010 it->glyph_row->contains_overlapping_glyphs_p = 1;
2011 }
b4192550
KH
2012 it->nglyphs = 1;
2013 it->ascent = font->ascent + boff;
2014 it->descent = font->descent - boff;
06a2c219
GM
2015 if (face->box != FACE_NO_BOX)
2016 {
2017 int thick = face->box_line_width;
ea2ba0d4
KH
2018
2019 if (thick > 0)
2020 {
2021 it->ascent += thick;
2022 it->descent += thick;
2023 }
2024 else
2025 thick = - thick;
06a2c219
GM
2026
2027 if (it->start_of_box_run_p)
2028 it->pixel_width += thick;
2029 if (it->end_of_box_run_p)
2030 it->pixel_width += thick;
2031 }
2032
2033 /* If face has an overline, add the height of the overline
2034 (1 pixel) and a 1 pixel margin to the character height. */
2035 if (face->overline_p)
2036 it->ascent += 2;
2037
2038 take_vertical_position_into_account (it);
2039
2040 if (it->glyph_row)
2041 x_append_glyph (it);
2042 }
a4249304 2043 it->multibyte_p = saved_multibyte_p;
06a2c219 2044 }
b4192550
KH
2045 else if (it->what == IT_COMPOSITION)
2046 {
2047 /* Note: A composition is represented as one glyph in the
2048 glyph matrix. There are no padding glyphs. */
2049 XChar2b char2b;
2050 XFontStruct *font;
ee569018 2051 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2052 XCharStruct *pcm;
2053 int font_not_found_p;
2054 struct font_info *font_info;
2055 int boff; /* baseline offset */
2056 struct composition *cmp = composition_table[it->cmp_id];
2057
2058 /* Maybe translate single-byte characters to multibyte. */
2059 it->char_to_display = it->c;
2060 if (unibyte_display_via_language_environment
2061 && SINGLE_BYTE_CHAR_P (it->c)
2062 && (it->c >= 0240
2063 || (it->c >= 0200
2064 && !NILP (Vnonascii_translation_table))))
2065 {
2066 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2067 }
2068
2069 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2070 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2071 face = FACE_FROM_ID (it->f, it->face_id);
2072 x_get_char_face_and_encoding (it->f, it->char_to_display,
2073 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2074 font = face->font;
2075
2076 /* When no suitable font found, use the default font. */
2077 font_not_found_p = font == NULL;
2078 if (font_not_found_p)
2079 {
2080 font = FRAME_FONT (it->f);
2081 boff = it->f->output_data.x->baseline_offset;
2082 font_info = NULL;
2083 }
2084 else
2085 {
2086 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2087 boff = font_info->baseline_offset;
2088 if (font_info->vertical_centering)
2089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2090 }
2091
2092 /* There are no padding glyphs, so there is only one glyph to
2093 produce for the composition. Important is that pixel_width,
2094 ascent and descent are the values of what is drawn by
2095 draw_glyphs (i.e. the values of the overall glyphs composed). */
2096 it->nglyphs = 1;
2097
2098 /* If we have not yet calculated pixel size data of glyphs of
2099 the composition for the current face font, calculate them
2100 now. Theoretically, we have to check all fonts for the
2101 glyphs, but that requires much time and memory space. So,
2102 here we check only the font of the first glyph. This leads
2103 to incorrect display very rarely, and C-l (recenter) can
2104 correct the display anyway. */
2105 if (cmp->font != (void *) font)
2106 {
2107 /* Ascent and descent of the font of the first character of
2108 this composition (adjusted by baseline offset). Ascent
2109 and descent of overall glyphs should not be less than
2110 them respectively. */
2111 int font_ascent = font->ascent + boff;
2112 int font_descent = font->descent - boff;
2113 /* Bounding box of the overall glyphs. */
2114 int leftmost, rightmost, lowest, highest;
329bed06 2115 int i, width, ascent, descent;
b4192550
KH
2116
2117 cmp->font = (void *) font;
2118
2119 /* Initialize the bounding box. */
1bdeec2e
KH
2120 if (font_info
2121 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2122 {
2123 width = pcm->width;
2124 ascent = pcm->ascent;
2125 descent = pcm->descent;
2126 }
2127 else
2128 {
2129 width = FONT_WIDTH (font);
2130 ascent = font->ascent;
2131 descent = font->descent;
2132 }
2133
2134 rightmost = width;
2135 lowest = - descent + boff;
2136 highest = ascent + boff;
b4192550 2137 leftmost = 0;
329bed06 2138
b4192550
KH
2139 if (font_info
2140 && font_info->default_ascent
2141 && CHAR_TABLE_P (Vuse_default_ascent)
2142 && !NILP (Faref (Vuse_default_ascent,
2143 make_number (it->char_to_display))))
2144 highest = font_info->default_ascent + boff;
2145
2146 /* Draw the first glyph at the normal position. It may be
2147 shifted to right later if some other glyphs are drawn at
2148 the left. */
2149 cmp->offsets[0] = 0;
2150 cmp->offsets[1] = boff;
2151
2152 /* Set cmp->offsets for the remaining glyphs. */
2153 for (i = 1; i < cmp->glyph_len; i++)
2154 {
2155 int left, right, btm, top;
2156 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2157 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2158
2159 face = FACE_FROM_ID (it->f, face_id);
2160 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2161 it->multibyte_p);
b4192550
KH
2162 font = face->font;
2163 if (font == NULL)
2164 {
2165 font = FRAME_FONT (it->f);
2166 boff = it->f->output_data.x->baseline_offset;
2167 font_info = NULL;
2168 }
2169 else
2170 {
2171 font_info
2172 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2173 boff = font_info->baseline_offset;
2174 if (font_info->vertical_centering)
2175 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2176 }
2177
1bdeec2e
KH
2178 if (font_info
2179 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2180 {
2181 width = pcm->width;
2182 ascent = pcm->ascent;
2183 descent = pcm->descent;
2184 }
2185 else
2186 {
2187 width = FONT_WIDTH (font);
1bdeec2e
KH
2188 ascent = 1;
2189 descent = 0;
329bed06 2190 }
b4192550
KH
2191
2192 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2193 {
2194 /* Relative composition with or without
2195 alternate chars. */
329bed06
GM
2196 left = (leftmost + rightmost - width) / 2;
2197 btm = - descent + boff;
b4192550
KH
2198 if (font_info && font_info->relative_compose
2199 && (! CHAR_TABLE_P (Vignore_relative_composition)
2200 || NILP (Faref (Vignore_relative_composition,
2201 make_number (ch)))))
2202 {
2203
329bed06 2204 if (- descent >= font_info->relative_compose)
b4192550
KH
2205 /* One extra pixel between two glyphs. */
2206 btm = highest + 1;
329bed06 2207 else if (ascent <= 0)
b4192550 2208 /* One extra pixel between two glyphs. */
329bed06 2209 btm = lowest - 1 - ascent - descent;
b4192550
KH
2210 }
2211 }
2212 else
2213 {
2214 /* A composition rule is specified by an integer
2215 value that encodes global and new reference
2216 points (GREF and NREF). GREF and NREF are
2217 specified by numbers as below:
2218
2219 0---1---2 -- ascent
2220 | |
2221 | |
2222 | |
2223 9--10--11 -- center
2224 | |
2225 ---3---4---5--- baseline
2226 | |
2227 6---7---8 -- descent
2228 */
2229 int rule = COMPOSITION_RULE (cmp, i);
2230 int gref, nref, grefx, grefy, nrefx, nrefy;
2231
2232 COMPOSITION_DECODE_RULE (rule, gref, nref);
2233 grefx = gref % 3, nrefx = nref % 3;
2234 grefy = gref / 3, nrefy = nref / 3;
2235
2236 left = (leftmost
2237 + grefx * (rightmost - leftmost) / 2
329bed06 2238 - nrefx * width / 2);
b4192550
KH
2239 btm = ((grefy == 0 ? highest
2240 : grefy == 1 ? 0
2241 : grefy == 2 ? lowest
2242 : (highest + lowest) / 2)
329bed06
GM
2243 - (nrefy == 0 ? ascent + descent
2244 : nrefy == 1 ? descent - boff
b4192550 2245 : nrefy == 2 ? 0
329bed06 2246 : (ascent + descent) / 2));
b4192550
KH
2247 }
2248
2249 cmp->offsets[i * 2] = left;
329bed06 2250 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2251
2252 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2253 right = left + width;
2254 top = btm + descent + ascent;
b4192550
KH
2255 if (left < leftmost)
2256 leftmost = left;
2257 if (right > rightmost)
2258 rightmost = right;
2259 if (top > highest)
2260 highest = top;
2261 if (btm < lowest)
2262 lowest = btm;
2263 }
2264
2265 /* If there are glyphs whose x-offsets are negative,
2266 shift all glyphs to the right and make all x-offsets
2267 non-negative. */
2268 if (leftmost < 0)
2269 {
2270 for (i = 0; i < cmp->glyph_len; i++)
2271 cmp->offsets[i * 2] -= leftmost;
2272 rightmost -= leftmost;
2273 }
2274
2275 cmp->pixel_width = rightmost;
2276 cmp->ascent = highest;
2277 cmp->descent = - lowest;
2278 if (cmp->ascent < font_ascent)
2279 cmp->ascent = font_ascent;
2280 if (cmp->descent < font_descent)
2281 cmp->descent = font_descent;
2282 }
2283
2284 it->pixel_width = cmp->pixel_width;
2285 it->ascent = it->phys_ascent = cmp->ascent;
2286 it->descent = it->phys_descent = cmp->descent;
2287
2288 if (face->box != FACE_NO_BOX)
2289 {
2290 int thick = face->box_line_width;
ea2ba0d4
KH
2291
2292 if (thick > 0)
2293 {
2294 it->ascent += thick;
2295 it->descent += thick;
2296 }
2297 else
2298 thick = - thick;
b4192550
KH
2299
2300 if (it->start_of_box_run_p)
2301 it->pixel_width += thick;
2302 if (it->end_of_box_run_p)
2303 it->pixel_width += thick;
2304 }
2305
2306 /* If face has an overline, add the height of the overline
2307 (1 pixel) and a 1 pixel margin to the character height. */
2308 if (face->overline_p)
2309 it->ascent += 2;
2310
2311 take_vertical_position_into_account (it);
2312
2313 if (it->glyph_row)
2314 x_append_composite_glyph (it);
2315 }
06a2c219
GM
2316 else if (it->what == IT_IMAGE)
2317 x_produce_image_glyph (it);
2318 else if (it->what == IT_STRETCH)
2319 x_produce_stretch_glyph (it);
2320
3017fdd1
GM
2321 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2322 because this isn't true for images with `:ascent 100'. */
2323 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2324 if (it->area == TEXT_AREA)
2325 it->current_x += it->pixel_width;
66ac4b0e 2326
d365f5bb
GM
2327 it->descent += it->extra_line_spacing;
2328
06a2c219
GM
2329 it->max_ascent = max (it->max_ascent, it->ascent);
2330 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2331 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2332 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2333}
2334
2335
2336/* Estimate the pixel height of the mode or top line on frame F.
2337 FACE_ID specifies what line's height to estimate. */
2338
2339int
2340x_estimate_mode_line_height (f, face_id)
2341 struct frame *f;
2342 enum face_id face_id;
2343{
43281ee3 2344 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2345
2346 /* This function is called so early when Emacs starts that the face
2347 cache and mode line face are not yet initialized. */
2348 if (FRAME_FACE_CACHE (f))
2349 {
2350 struct face *face = FACE_FROM_ID (f, face_id);
2351 if (face)
43281ee3
GM
2352 {
2353 if (face->font)
2354 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2355 if (face->box_line_width > 0)
2356 height += 2 * face->box_line_width;
43281ee3 2357 }
06a2c219
GM
2358 }
2359
2360 return height;
2361}
2362
2363\f
2364/***********************************************************************
2365 Glyph display
2366 ***********************************************************************/
2367
2368/* A sequence of glyphs to be drawn in the same face.
2369
2370 This data structure is not really completely X specific, so it
2371 could possibly, at least partially, be useful for other systems. It
2372 is currently not part of the external redisplay interface because
2373 it's not clear what other systems will need. */
2374
2375struct glyph_string
2376{
2377 /* X-origin of the string. */
2378 int x;
2379
2380 /* Y-origin and y-position of the base line of this string. */
2381 int y, ybase;
2382
2383 /* The width of the string, not including a face extension. */
2384 int width;
2385
2386 /* The width of the string, including a face extension. */
2387 int background_width;
2388
2389 /* The height of this string. This is the height of the line this
2390 string is drawn in, and can be different from the height of the
2391 font the string is drawn in. */
2392 int height;
2393
2394 /* Number of pixels this string overwrites in front of its x-origin.
2395 This number is zero if the string has an lbearing >= 0; it is
2396 -lbearing, if the string has an lbearing < 0. */
2397 int left_overhang;
2398
2399 /* Number of pixels this string overwrites past its right-most
2400 nominal x-position, i.e. x + width. Zero if the string's
2401 rbearing is <= its nominal width, rbearing - width otherwise. */
2402 int right_overhang;
2403
2404 /* The frame on which the glyph string is drawn. */
2405 struct frame *f;
2406
2407 /* The window on which the glyph string is drawn. */
2408 struct window *w;
2409
2410 /* X display and window for convenience. */
2411 Display *display;
2412 Window window;
2413
2414 /* The glyph row for which this string was built. It determines the
2415 y-origin and height of the string. */
2416 struct glyph_row *row;
2417
2418 /* The area within row. */
2419 enum glyph_row_area area;
2420
2421 /* Characters to be drawn, and number of characters. */
2422 XChar2b *char2b;
2423 int nchars;
2424
06a2c219
GM
2425 /* A face-override for drawing cursors, mouse face and similar. */
2426 enum draw_glyphs_face hl;
2427
2428 /* Face in which this string is to be drawn. */
2429 struct face *face;
2430
2431 /* Font in which this string is to be drawn. */
2432 XFontStruct *font;
2433
2434 /* Font info for this string. */
2435 struct font_info *font_info;
2436
b4192550
KH
2437 /* Non-null means this string describes (part of) a composition.
2438 All characters from char2b are drawn composed. */
2439 struct composition *cmp;
06a2c219
GM
2440
2441 /* Index of this glyph string's first character in the glyph
b4192550
KH
2442 definition of CMP. If this is zero, this glyph string describes
2443 the first character of a composition. */
06a2c219
GM
2444 int gidx;
2445
2446 /* 1 means this glyph strings face has to be drawn to the right end
2447 of the window's drawing area. */
2448 unsigned extends_to_end_of_line_p : 1;
2449
2450 /* 1 means the background of this string has been drawn. */
2451 unsigned background_filled_p : 1;
2452
2453 /* 1 means glyph string must be drawn with 16-bit functions. */
2454 unsigned two_byte_p : 1;
2455
2456 /* 1 means that the original font determined for drawing this glyph
2457 string could not be loaded. The member `font' has been set to
2458 the frame's default font in this case. */
2459 unsigned font_not_found_p : 1;
2460
2461 /* 1 means that the face in which this glyph string is drawn has a
2462 stipple pattern. */
2463 unsigned stippled_p : 1;
2464
66ac4b0e
GM
2465 /* 1 means only the foreground of this glyph string must be drawn,
2466 and we should use the physical height of the line this glyph
2467 string appears in as clip rect. */
2468 unsigned for_overlaps_p : 1;
2469
06a2c219
GM
2470 /* The GC to use for drawing this glyph string. */
2471 GC gc;
2472
2473 /* A pointer to the first glyph in the string. This glyph
2474 corresponds to char2b[0]. Needed to draw rectangles if
2475 font_not_found_p is 1. */
2476 struct glyph *first_glyph;
2477
2478 /* Image, if any. */
2479 struct image *img;
2480
2481 struct glyph_string *next, *prev;
2482};
2483
2484
5c187dee 2485#if 0
06a2c219
GM
2486
2487static void
2488x_dump_glyph_string (s)
2489 struct glyph_string *s;
2490{
2491 fprintf (stderr, "glyph string\n");
2492 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2493 s->x, s->y, s->width, s->height);
2494 fprintf (stderr, " ybase = %d\n", s->ybase);
2495 fprintf (stderr, " hl = %d\n", s->hl);
2496 fprintf (stderr, " left overhang = %d, right = %d\n",
2497 s->left_overhang, s->right_overhang);
2498 fprintf (stderr, " nchars = %d\n", s->nchars);
2499 fprintf (stderr, " extends to end of line = %d\n",
2500 s->extends_to_end_of_line_p);
2501 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2502 fprintf (stderr, " bg width = %d\n", s->background_width);
2503}
2504
2505#endif /* GLYPH_DEBUG */
2506
2507
2508
2509static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2510 struct glyph_string **,
2511 struct glyph_string *,
2512 struct glyph_string *));
2513static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2514 struct glyph_string **,
2515 struct glyph_string *,
2516 struct glyph_string *));
2517static void x_append_glyph_string P_ ((struct glyph_string **,
2518 struct glyph_string **,
2519 struct glyph_string *));
2520static int x_left_overwritten P_ ((struct glyph_string *));
2521static int x_left_overwriting P_ ((struct glyph_string *));
2522static int x_right_overwritten P_ ((struct glyph_string *));
2523static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2524static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2525 int));
06a2c219
GM
2526static void x_init_glyph_string P_ ((struct glyph_string *,
2527 XChar2b *, struct window *,
2528 struct glyph_row *,
2529 enum glyph_row_area, int,
2530 enum draw_glyphs_face));
2531static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2532 enum glyph_row_area, int, int,
66ac4b0e 2533 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2534static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2535static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2536static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2537 int));
2538static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2539static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2540static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2541static void x_draw_glyph_string P_ ((struct glyph_string *));
2542static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2543static void x_set_cursor_gc P_ ((struct glyph_string *));
2544static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2545static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2546static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2547 int *, int *));
2548static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2549static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2550 unsigned long *, double, int));
06a2c219 2551static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2552 double, int, unsigned long));
06a2c219
GM
2553static void x_setup_relief_colors P_ ((struct glyph_string *));
2554static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2555static void x_draw_image_relief P_ ((struct glyph_string *));
2556static void x_draw_image_foreground P_ ((struct glyph_string *));
2557static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2558static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2559static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2560 int, int, int));
2561static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2562 int, int, int, int, XRectangle *));
2563static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2564 int, int, int, XRectangle *));
66ac4b0e
GM
2565static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2566 enum glyph_row_area));
209f68d9
GM
2567static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2568 struct glyph_row *,
2569 enum glyph_row_area, int, int));
06a2c219 2570
163dcff3
GM
2571#if GLYPH_DEBUG
2572static void x_check_font P_ ((struct frame *, XFontStruct *));
2573#endif
2574
06a2c219 2575
06a2c219
GM
2576/* Append the list of glyph strings with head H and tail T to the list
2577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2578
2579static INLINE void
2580x_append_glyph_string_lists (head, tail, h, t)
2581 struct glyph_string **head, **tail;
2582 struct glyph_string *h, *t;
2583{
2584 if (h)
2585 {
2586 if (*head)
2587 (*tail)->next = h;
2588 else
2589 *head = h;
2590 h->prev = *tail;
2591 *tail = t;
2592 }
2593}
2594
2595
2596/* Prepend the list of glyph strings with head H and tail T to the
2597 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2598 result. */
2599
2600static INLINE void
2601x_prepend_glyph_string_lists (head, tail, h, t)
2602 struct glyph_string **head, **tail;
2603 struct glyph_string *h, *t;
2604{
2605 if (h)
2606 {
2607 if (*head)
2608 (*head)->prev = t;
2609 else
2610 *tail = t;
2611 t->next = *head;
2612 *head = h;
2613 }
2614}
2615
2616
2617/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2618 Set *HEAD and *TAIL to the resulting list. */
2619
2620static INLINE void
2621x_append_glyph_string (head, tail, s)
2622 struct glyph_string **head, **tail;
2623 struct glyph_string *s;
2624{
2625 s->next = s->prev = NULL;
2626 x_append_glyph_string_lists (head, tail, s, s);
2627}
2628
2629
2630/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2631 face. */
2632
2633static void
2634x_set_cursor_gc (s)
2635 struct glyph_string *s;
2636{
2637 if (s->font == FRAME_FONT (s->f)
2638 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2639 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2640 && !s->cmp)
06a2c219
GM
2641 s->gc = s->f->output_data.x->cursor_gc;
2642 else
2643 {
2644 /* Cursor on non-default face: must merge. */
2645 XGCValues xgcv;
2646 unsigned long mask;
2647
2648 xgcv.background = s->f->output_data.x->cursor_pixel;
2649 xgcv.foreground = s->face->background;
2650
2651 /* If the glyph would be invisible, try a different foreground. */
2652 if (xgcv.foreground == xgcv.background)
2653 xgcv.foreground = s->face->foreground;
2654 if (xgcv.foreground == xgcv.background)
2655 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2656 if (xgcv.foreground == xgcv.background)
2657 xgcv.foreground = s->face->foreground;
2658
2659 /* Make sure the cursor is distinct from text in this face. */
2660 if (xgcv.background == s->face->background
2661 && xgcv.foreground == s->face->foreground)
2662 {
2663 xgcv.background = s->face->foreground;
2664 xgcv.foreground = s->face->background;
2665 }
2666
2667 IF_DEBUG (x_check_font (s->f, s->font));
2668 xgcv.font = s->font->fid;
2669 xgcv.graphics_exposures = False;
2670 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2671
2672 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2673 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2674 mask, &xgcv);
2675 else
2676 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2677 = XCreateGC (s->display, s->window, mask, &xgcv);
2678
2679 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2680 }
2681}
2682
2683
2684/* Set up S->gc of glyph string S for drawing text in mouse face. */
2685
2686static void
2687x_set_mouse_face_gc (s)
2688 struct glyph_string *s;
2689{
2690 int face_id;
ee569018 2691 struct face *face;
06a2c219 2692
e4ded23c 2693 /* What face has to be used last for the mouse face? */
06a2c219 2694 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2695 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2696 if (face == NULL)
2697 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2698
033e3e18
GM
2699 if (s->first_glyph->type == CHAR_GLYPH)
2700 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2701 else
2702 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2703 s->face = FACE_FROM_ID (s->f, face_id);
2704 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2705
2706 /* If font in this face is same as S->font, use it. */
2707 if (s->font == s->face->font)
2708 s->gc = s->face->gc;
2709 else
2710 {
2711 /* Otherwise construct scratch_cursor_gc with values from FACE
2712 but font FONT. */
2713 XGCValues xgcv;
2714 unsigned long mask;
2715
2716 xgcv.background = s->face->background;
2717 xgcv.foreground = s->face->foreground;
2718 IF_DEBUG (x_check_font (s->f, s->font));
2719 xgcv.font = s->font->fid;
2720 xgcv.graphics_exposures = False;
2721 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2722
2723 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2724 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2725 mask, &xgcv);
2726 else
2727 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2728 = XCreateGC (s->display, s->window, mask, &xgcv);
2729
2730 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2731 }
2732
2733 xassert (s->gc != 0);
2734}
2735
2736
2737/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2738 Faces to use in the mode line have already been computed when the
2739 matrix was built, so there isn't much to do, here. */
2740
2741static INLINE void
2742x_set_mode_line_face_gc (s)
2743 struct glyph_string *s;
2744{
2745 s->gc = s->face->gc;
06a2c219
GM
2746}
2747
2748
2749/* Set S->gc of glyph string S for drawing that glyph string. Set
2750 S->stippled_p to a non-zero value if the face of S has a stipple
2751 pattern. */
2752
2753static INLINE void
2754x_set_glyph_string_gc (s)
2755 struct glyph_string *s;
2756{
209f68d9
GM
2757 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2758
06a2c219
GM
2759 if (s->hl == DRAW_NORMAL_TEXT)
2760 {
2761 s->gc = s->face->gc;
2762 s->stippled_p = s->face->stipple != 0;
2763 }
2764 else if (s->hl == DRAW_INVERSE_VIDEO)
2765 {
2766 x_set_mode_line_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_CURSOR)
2770 {
2771 x_set_cursor_gc (s);
2772 s->stippled_p = 0;
2773 }
2774 else if (s->hl == DRAW_MOUSE_FACE)
2775 {
2776 x_set_mouse_face_gc (s);
2777 s->stippled_p = s->face->stipple != 0;
2778 }
2779 else if (s->hl == DRAW_IMAGE_RAISED
2780 || s->hl == DRAW_IMAGE_SUNKEN)
2781 {
2782 s->gc = s->face->gc;
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790
2791 /* GC must have been set. */
2792 xassert (s->gc != 0);
2793}
2794
2795
2796/* Return in *R the clipping rectangle for glyph string S. */
2797
2798static void
2799x_get_glyph_string_clip_rect (s, r)
2800 struct glyph_string *s;
2801 XRectangle *r;
2802{
2803 if (s->row->full_width_p)
2804 {
2805 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2806 int canon_x = CANON_X_UNIT (s->f);
2807
2808 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2809 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2810
2811 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2812 {
1da3fd71 2813 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2814 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2815 r->x -= width;
2816 }
2817
b9432a85 2818 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2819
06a2c219
GM
2820 /* Unless displaying a mode or menu bar line, which are always
2821 fully visible, clip to the visible part of the row. */
2822 if (s->w->pseudo_window_p)
2823 r->height = s->row->visible_height;
2824 else
2825 r->height = s->height;
2826 }
2827 else
2828 {
2829 /* This is a text line that may be partially visible. */
2830 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2831 r->width = window_box_width (s->w, s->area);
2832 r->height = s->row->visible_height;
2833 }
2834
2835 /* Don't use S->y for clipping because it doesn't take partially
2836 visible lines into account. For example, it can be negative for
2837 partially visible lines at the top of a window. */
2838 if (!s->row->full_width_p
2839 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2841 else
2842 r->y = max (0, s->row->y);
06a2c219 2843
9ea173e8 2844 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2845 at the top of the window. */
9ea173e8 2846 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2847 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2848
2849 /* If S draws overlapping rows, it's sufficient to use the top and
2850 bottom of the window for clipping because this glyph string
2851 intentionally draws over other lines. */
2852 if (s->for_overlaps_p)
2853 {
045dee35 2854 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2855 r->height = window_text_bottom_y (s->w) - r->y;
2856 }
2857
2858 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2859}
2860
2861
2862/* Set clipping for output of glyph string S. S may be part of a mode
2863 line or menu if we don't have X toolkit support. */
2864
2865static INLINE void
2866x_set_glyph_string_clipping (s)
2867 struct glyph_string *s;
2868{
2869 XRectangle r;
2870 x_get_glyph_string_clip_rect (s, &r);
2871 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2872}
2873
2874
2875/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2876 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2877
2878static INLINE void
2879x_compute_glyph_string_overhangs (s)
2880 struct glyph_string *s;
2881{
b4192550 2882 if (s->cmp == NULL
06a2c219
GM
2883 && s->first_glyph->type == CHAR_GLYPH)
2884 {
2885 XCharStruct cs;
2886 int direction, font_ascent, font_descent;
2887 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2888 &font_ascent, &font_descent, &cs);
2889 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2890 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2891 }
2892}
2893
2894
2895/* Compute overhangs and x-positions for glyph string S and its
2896 predecessors, or successors. X is the starting x-position for S.
2897 BACKWARD_P non-zero means process predecessors. */
2898
2899static void
2900x_compute_overhangs_and_x (s, x, backward_p)
2901 struct glyph_string *s;
2902 int x;
2903 int backward_p;
2904{
2905 if (backward_p)
2906 {
2907 while (s)
2908 {
2909 x_compute_glyph_string_overhangs (s);
2910 x -= s->width;
2911 s->x = x;
2912 s = s->prev;
2913 }
2914 }
2915 else
2916 {
2917 while (s)
2918 {
2919 x_compute_glyph_string_overhangs (s);
2920 s->x = x;
2921 x += s->width;
2922 s = s->next;
2923 }
2924 }
2925}
2926
2927
2928/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2929 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2930 assumed to be zero. */
06a2c219
GM
2931
2932static void
2933x_get_glyph_overhangs (glyph, f, left, right)
2934 struct glyph *glyph;
2935 struct frame *f;
2936 int *left, *right;
2937{
06a2c219
GM
2938 *left = *right = 0;
2939
b4192550 2940 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2941 {
2942 XFontStruct *font;
2943 struct face *face;
2944 struct font_info *font_info;
2945 XChar2b char2b;
ee569018
KH
2946 XCharStruct *pcm;
2947
2948 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2949 font = face->font;
2950 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2951 if (font
2952 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2953 {
06a2c219
GM
2954 if (pcm->rbearing > pcm->width)
2955 *right = pcm->rbearing - pcm->width;
2956 if (pcm->lbearing < 0)
2957 *left = -pcm->lbearing;
2958 }
2959 }
2960}
2961
2962
2963/* Return the index of the first glyph preceding glyph string S that
2964 is overwritten by S because of S's left overhang. Value is -1
2965 if no glyphs are overwritten. */
2966
2967static int
2968x_left_overwritten (s)
2969 struct glyph_string *s;
2970{
2971 int k;
2972
2973 if (s->left_overhang)
2974 {
2975 int x = 0, i;
2976 struct glyph *glyphs = s->row->glyphs[s->area];
2977 int first = s->first_glyph - glyphs;
2978
2979 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2980 x -= glyphs[i].pixel_width;
2981
2982 k = i + 1;
2983 }
2984 else
2985 k = -1;
2986
2987 return k;
2988}
2989
2990
2991/* Return the index of the first glyph preceding glyph string S that
2992 is overwriting S because of its right overhang. Value is -1 if no
2993 glyph in front of S overwrites S. */
2994
2995static int
2996x_left_overwriting (s)
2997 struct glyph_string *s;
2998{
2999 int i, k, x;
3000 struct glyph *glyphs = s->row->glyphs[s->area];
3001 int first = s->first_glyph - glyphs;
3002
3003 k = -1;
3004 x = 0;
3005 for (i = first - 1; i >= 0; --i)
3006 {
3007 int left, right;
3008 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3009 if (x + right > 0)
3010 k = i;
3011 x -= glyphs[i].pixel_width;
3012 }
3013
3014 return k;
3015}
3016
3017
3018/* Return the index of the last glyph following glyph string S that is
3019 not overwritten by S because of S's right overhang. Value is -1 if
3020 no such glyph is found. */
3021
3022static int
3023x_right_overwritten (s)
3024 struct glyph_string *s;
3025{
3026 int k = -1;
3027
3028 if (s->right_overhang)
3029 {
3030 int x = 0, i;
3031 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3032 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3033 int end = s->row->used[s->area];
3034
3035 for (i = first; i < end && s->right_overhang > x; ++i)
3036 x += glyphs[i].pixel_width;
3037
3038 k = i;
3039 }
3040
3041 return k;
3042}
3043
3044
3045/* Return the index of the last glyph following glyph string S that
3046 overwrites S because of its left overhang. Value is negative
3047 if no such glyph is found. */
3048
3049static int
3050x_right_overwriting (s)
3051 struct glyph_string *s;
3052{
3053 int i, k, x;
3054 int end = s->row->used[s->area];
3055 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3056 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3057
3058 k = -1;
3059 x = 0;
3060 for (i = first; i < end; ++i)
3061 {
3062 int left, right;
3063 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3064 if (x - left < 0)
3065 k = i;
3066 x += glyphs[i].pixel_width;
3067 }
3068
3069 return k;
3070}
3071
3072
3073/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3074
3075static INLINE void
3076x_clear_glyph_string_rect (s, x, y, w, h)
3077 struct glyph_string *s;
3078 int x, y, w, h;
3079{
3080 XGCValues xgcv;
3081 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3082 XSetForeground (s->display, s->gc, xgcv.background);
3083 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3084 XSetForeground (s->display, s->gc, xgcv.foreground);
3085}
3086
3087
3088/* Draw the background of glyph_string S. If S->background_filled_p
3089 is non-zero don't draw it. FORCE_P non-zero means draw the
3090 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3091 when a string preceding S draws into the background of S, or S
3092 contains the first component of a composition. */
06a2c219
GM
3093
3094static void
3095x_draw_glyph_string_background (s, force_p)
3096 struct glyph_string *s;
3097 int force_p;
3098{
3099 /* Nothing to do if background has already been drawn or if it
3100 shouldn't be drawn in the first place. */
3101 if (!s->background_filled_p)
3102 {
ea2ba0d4
KH
3103 int box_line_width = max (s->face->box_line_width, 0);
3104
b4192550 3105 if (s->stippled_p)
06a2c219
GM
3106 {
3107 /* Fill background with a stipple pattern. */
3108 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3109 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3110 s->y + box_line_width,
06a2c219 3111 s->background_width,
ea2ba0d4 3112 s->height - 2 * box_line_width);
06a2c219
GM
3113 XSetFillStyle (s->display, s->gc, FillSolid);
3114 s->background_filled_p = 1;
3115 }
ea2ba0d4 3116 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3117 || s->font_not_found_p
3118 || s->extends_to_end_of_line_p
06a2c219
GM
3119 || force_p)
3120 {
ea2ba0d4 3121 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3122 s->background_width,
ea2ba0d4 3123 s->height - 2 * box_line_width);
06a2c219
GM
3124 s->background_filled_p = 1;
3125 }
3126 }
3127}
3128
3129
3130/* Draw the foreground of glyph string S. */
3131
3132static void
3133x_draw_glyph_string_foreground (s)
3134 struct glyph_string *s;
3135{
3136 int i, x;
3137
3138 /* If first glyph of S has a left box line, start drawing the text
3139 of S to the right of that box line. */
3140 if (s->face->box != FACE_NO_BOX
3141 && s->first_glyph->left_box_line_p)
ea2ba0d4 3142 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3143 else
3144 x = s->x;
3145
b4192550
KH
3146 /* Draw characters of S as rectangles if S's font could not be
3147 loaded. */
3148 if (s->font_not_found_p)
06a2c219 3149 {
b4192550 3150 for (i = 0; i < s->nchars; ++i)
06a2c219 3151 {
b4192550
KH
3152 struct glyph *g = s->first_glyph + i;
3153 XDrawRectangle (s->display, s->window,
3154 s->gc, x, s->y, g->pixel_width - 1,
3155 s->height - 1);
3156 x += g->pixel_width;
06a2c219
GM
3157 }
3158 }
3159 else
3160 {
b4192550
KH
3161 char *char1b = (char *) s->char2b;
3162 int boff = s->font_info->baseline_offset;
06a2c219 3163
b4192550
KH
3164 if (s->font_info->vertical_centering)
3165 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3166
3167 /* If we can use 8-bit functions, condense S->char2b. */
3168 if (!s->two_byte_p)
3169 for (i = 0; i < s->nchars; ++i)
3170 char1b[i] = s->char2b[i].byte2;
3171
3172 /* Draw text with XDrawString if background has already been
3173 filled. Otherwise, use XDrawImageString. (Note that
3174 XDrawImageString is usually faster than XDrawString.) Always
3175 use XDrawImageString when drawing the cursor so that there is
3176 no chance that characters under a box cursor are invisible. */
3177 if (s->for_overlaps_p
3178 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3179 {
3180 /* Draw characters with 16-bit or 8-bit functions. */
3181 if (s->two_byte_p)
3182 XDrawString16 (s->display, s->window, s->gc, x,
3183 s->ybase - boff, s->char2b, s->nchars);
3184 else
3185 XDrawString (s->display, s->window, s->gc, x,
3186 s->ybase - boff, char1b, s->nchars);
3187 }
06a2c219
GM
3188 else
3189 {
b4192550
KH
3190 if (s->two_byte_p)
3191 XDrawImageString16 (s->display, s->window, s->gc, x,
3192 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3193 else
b4192550
KH
3194 XDrawImageString (s->display, s->window, s->gc, x,
3195 s->ybase - boff, char1b, s->nchars);
3196 }
3197 }
3198}
06a2c219 3199
b4192550 3200/* Draw the foreground of composite glyph string S. */
06a2c219 3201
b4192550
KH
3202static void
3203x_draw_composite_glyph_string_foreground (s)
3204 struct glyph_string *s;
3205{
3206 int i, x;
06a2c219 3207
b4192550
KH
3208 /* If first glyph of S has a left box line, start drawing the text
3209 of S to the right of that box line. */
3210 if (s->face->box != FACE_NO_BOX
3211 && s->first_glyph->left_box_line_p)
ea2ba0d4 3212 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3213 else
3214 x = s->x;
06a2c219 3215
b4192550
KH
3216 /* S is a glyph string for a composition. S->gidx is the index of
3217 the first character drawn for glyphs of this composition.
3218 S->gidx == 0 means we are drawing the very first character of
3219 this composition. */
06a2c219 3220
b4192550
KH
3221 /* Draw a rectangle for the composition if the font for the very
3222 first character of the composition could not be loaded. */
3223 if (s->font_not_found_p)
3224 {
3225 if (s->gidx == 0)
3226 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3227 s->width - 1, s->height - 1);
3228 }
3229 else
3230 {
3231 for (i = 0; i < s->nchars; i++, ++s->gidx)
3232 XDrawString16 (s->display, s->window, s->gc,
3233 x + s->cmp->offsets[s->gidx * 2],
3234 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3235 s->char2b + i, 1);
06a2c219
GM
3236 }
3237}
3238
3239
80c32bcc
GM
3240#ifdef USE_X_TOOLKIT
3241
3e71d8f2 3242static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3243
3e71d8f2
GM
3244
3245/* Return the frame on which widget WIDGET is used.. Abort if frame
3246 cannot be determined. */
3247
e851c833 3248static struct frame *
3e71d8f2 3249x_frame_of_widget (widget)
80c32bcc 3250 Widget widget;
80c32bcc 3251{
80c32bcc 3252 struct x_display_info *dpyinfo;
5c187dee 3253 Lisp_Object tail;
3e71d8f2
GM
3254 struct frame *f;
3255
80c32bcc
GM
3256 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3257
3258 /* Find the top-level shell of the widget. Note that this function
3259 can be called when the widget is not yet realized, so XtWindow
3260 (widget) == 0. That's the reason we can't simply use
3261 x_any_window_to_frame. */
3262 while (!XtIsTopLevelShell (widget))
3263 widget = XtParent (widget);
3264
3265 /* Look for a frame with that top-level widget. Allocate the color
3266 on that frame to get the right gamma correction value. */
3267 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3268 if (GC_FRAMEP (XCAR (tail))
3269 && (f = XFRAME (XCAR (tail)),
3270 (f->output_data.nothing != 1
3271 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3272 && f->output_data.x->widget == widget)
3e71d8f2 3273 return f;
80c32bcc
GM
3274
3275 abort ();
3276}
3277
3e71d8f2
GM
3278
3279/* Allocate the color COLOR->pixel on the screen and display of
3280 widget WIDGET in colormap CMAP. If an exact match cannot be
3281 allocated, try the nearest color available. Value is non-zero
3282 if successful. This is called from lwlib. */
3283
3284int
3285x_alloc_nearest_color_for_widget (widget, cmap, color)
3286 Widget widget;
3287 Colormap cmap;
3288 XColor *color;
3289{
3290 struct frame *f = x_frame_of_widget (widget);
3291 return x_alloc_nearest_color (f, cmap, color);
3292}
3293
3294
46d516e5
MB
3295/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3296 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3297 If this produces the same color as PIXEL, try a color where all RGB
3298 values have DELTA added. Return the allocated color in *PIXEL.
3299 DISPLAY is the X display, CMAP is the colormap to operate on.
3300 Value is non-zero if successful. */
3301
3302int
3303x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3304 Widget widget;
3305 Display *display;
3306 Colormap cmap;
3307 unsigned long *pixel;
3308 double factor;
3309 int delta;
3310{
3311 struct frame *f = x_frame_of_widget (widget);
3312 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3313}
3314
3315
80c32bcc
GM
3316#endif /* USE_X_TOOLKIT */
3317
3318
f04e1297
GM
3319/* Value is an array of XColor structures for the contents of the
3320 color map of frame F. Set *NCELLS to the size of the array.
3321 Note that this probably shouldn't be called for large color maps,
3322 say a 24-bit TrueColor map. */
3323
3324static const XColor *
3325x_color_cells (f, ncells)
3326 struct frame *f;
3327 int *ncells;
3328{
3329 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3330
3331 if (dpyinfo->color_cells == NULL)
3332 {
3333 Display *display = FRAME_X_DISPLAY (f);
3334 Screen *screen = FRAME_X_SCREEN (f);
3335 int i;
3336
3337 dpyinfo->ncolor_cells
3338 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3339 dpyinfo->color_cells
3340 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3341 * sizeof *dpyinfo->color_cells);
3342
3343 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3344 dpyinfo->color_cells[i].pixel = i;
3345
3346 XQueryColors (display, FRAME_X_COLORMAP (f),
3347 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3348 }
3349
3350 *ncells = dpyinfo->ncolor_cells;
3351 return dpyinfo->color_cells;
3352}
3353
3354
3355/* On frame F, translate pixel colors to RGB values for the NCOLORS
3356 colors in COLORS. Use cached information, if available. */
3357
3358void
3359x_query_colors (f, colors, ncolors)
3360 struct frame *f;
3361 XColor *colors;
3362 int ncolors;
3363{
3364 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3365
3366 if (dpyinfo->color_cells)
3367 {
3368 int i;
3369 for (i = 0; i < ncolors; ++i)
3370 {
3371 unsigned long pixel = colors[i].pixel;
3372 xassert (pixel < dpyinfo->ncolor_cells);
3373 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3374 colors[i] = dpyinfo->color_cells[pixel];
3375 }
3376 }
3377 else
3378 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3379}
3380
3381
3382/* On frame F, translate pixel color to RGB values for the color in
3383 COLOR. Use cached information, if available. */
3384
3385void
3386x_query_color (f, color)
3387 struct frame *f;
3388 XColor *color;
3389{
3390 x_query_colors (f, color, 1);
3391}
3392
3393
06a2c219
GM
3394/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3395 CMAP. If an exact match can't be allocated, try the nearest color
3396 available. Value is non-zero if successful. Set *COLOR to the
3397 color allocated. */
3398
3399int
80c32bcc
GM
3400x_alloc_nearest_color (f, cmap, color)
3401 struct frame *f;
06a2c219
GM
3402 Colormap cmap;
3403 XColor *color;
3404{
80c32bcc
GM
3405 Display *display = FRAME_X_DISPLAY (f);
3406 Screen *screen = FRAME_X_SCREEN (f);
3407 int rc;
3408
3409 gamma_correct (f, color);
3410 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3411 if (rc == 0)
3412 {
3413 /* If we got to this point, the colormap is full, so we're going
3414 to try to get the next closest color. The algorithm used is
3415 a least-squares matching, which is what X uses for closest
3416 color matching with StaticColor visuals. */
3417 int nearest, i;
3418 unsigned long nearest_delta = ~0;
f04e1297
GM
3419 int ncells;
3420 const XColor *cells = x_color_cells (f, &ncells);
06a2c219
GM
3421
3422 for (nearest = i = 0; i < ncells; ++i)
3423 {
3424 long dred = (color->red >> 8) - (cells[i].red >> 8);
3425 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3426 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3427 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3428
3429 if (delta < nearest_delta)
3430 {
3431 nearest = i;
3432 nearest_delta = delta;
3433 }
3434 }
3435
3436 color->red = cells[nearest].red;
3437 color->green = cells[nearest].green;
3438 color->blue = cells[nearest].blue;
3439 rc = XAllocColor (display, cmap, color);
3440 }
35efe0a1
GM
3441 else
3442 {
3443 /* If allocation succeeded, and the allocated pixel color is not
3444 equal to a cached pixel color recorded earlier, there was a
3445 change in the colormap, so clear the color cache. */
3446 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3447 XColor *cached_color;
3448
3449 if (dpyinfo->color_cells
3450 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3451 (cached_color->red != color->red
3452 || cached_color->blue != color->blue
3453 || cached_color->green != color->green)))
35efe0a1
GM
3454 {
3455 xfree (dpyinfo->color_cells);
3456 dpyinfo->color_cells = NULL;
3457 dpyinfo->ncolor_cells = 0;
3458 }
3459 }
06a2c219 3460
d9c545da
GM
3461#ifdef DEBUG_X_COLORS
3462 if (rc)
3463 register_color (color->pixel);
3464#endif /* DEBUG_X_COLORS */
3465
06a2c219
GM
3466 return rc;
3467}
3468
3469
d9c545da
GM
3470/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3471 It's necessary to do this instead of just using PIXEL directly to
3472 get color reference counts right. */
3473
3474unsigned long
3475x_copy_color (f, pixel)
3476 struct frame *f;
3477 unsigned long pixel;
3478{
3479 XColor color;
3480
3481 color.pixel = pixel;
3482 BLOCK_INPUT;
f04e1297 3483 x_query_color (f, &color);
d9c545da
GM
3484 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3485 UNBLOCK_INPUT;
3486#ifdef DEBUG_X_COLORS
3487 register_color (pixel);
3488#endif
3489 return color.pixel;
3490}
3491
3492
3e71d8f2
GM
3493/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3494 It's necessary to do this instead of just using PIXEL directly to
3495 get color reference counts right. */
3496
3497unsigned long
3498x_copy_dpy_color (dpy, cmap, pixel)
3499 Display *dpy;
3500 Colormap cmap;
3501 unsigned long pixel;
3502{
3503 XColor color;
3504
3505 color.pixel = pixel;
3506 BLOCK_INPUT;
3507 XQueryColor (dpy, cmap, &color);
3508 XAllocColor (dpy, cmap, &color);
3509 UNBLOCK_INPUT;
3510#ifdef DEBUG_X_COLORS
3511 register_color (pixel);
3512#endif
3513 return color.pixel;
3514}
3515
3516
6d8b0acd 3517/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3518 boosted.
6d8b0acd 3519
d7361edf
MB
3520 Nominally, highlight colors for `3d' faces are calculated by
3521 brightening an object's color by a constant scale factor, but this
3522 doesn't yield good results for dark colors, so for colors who's
3523 brightness is less than this value (on a scale of 0-65535) have an
3524 use an additional additive factor.
6d8b0acd
MB
3525
3526 The value here is set so that the default menu-bar/mode-line color
3527 (grey75) will not have its highlights changed at all. */
3528#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3529
3530
06a2c219
GM
3531/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3532 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3533 If this produces the same color as PIXEL, try a color where all RGB
3534 values have DELTA added. Return the allocated color in *PIXEL.
3535 DISPLAY is the X display, CMAP is the colormap to operate on.
3536 Value is non-zero if successful. */
3537
3538static int
3539x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3540 struct frame *f;
3541 Display *display;
3542 Colormap cmap;
3543 unsigned long *pixel;
68c45bf0 3544 double factor;
06a2c219
GM
3545 int delta;
3546{
3547 XColor color, new;
6d8b0acd 3548 long bright;
06a2c219
GM
3549 int success_p;
3550
3551 /* Get RGB color values. */
3552 color.pixel = *pixel;
f04e1297 3553 x_query_color (f, &color);
06a2c219
GM
3554
3555 /* Change RGB values by specified FACTOR. Avoid overflow! */
3556 xassert (factor >= 0);
3557 new.red = min (0xffff, factor * color.red);
3558 new.green = min (0xffff, factor * color.green);
3559 new.blue = min (0xffff, factor * color.blue);
3560
d7361edf
MB
3561 /* Calculate brightness of COLOR. */
3562 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3563
3564 /* We only boost colors that are darker than
3565 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3566 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3567 /* Make an additive adjustment to NEW, because it's dark enough so
3568 that scaling by FACTOR alone isn't enough. */
3569 {
3570 /* How far below the limit this color is (0 - 1, 1 being darker). */
3571 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3572 /* The additive adjustment. */
d7361edf 3573 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3574
3575 if (factor < 1)
3576 {
6d8b0acd
MB
3577 new.red = max (0, new.red - min_delta);
3578 new.green = max (0, new.green - min_delta);
3579 new.blue = max (0, new.blue - min_delta);
3580 }
3581 else
3582 {
3583 new.red = min (0xffff, min_delta + new.red);
3584 new.green = min (0xffff, min_delta + new.green);
3585 new.blue = min (0xffff, min_delta + new.blue);
3586 }
3587 }
3588
06a2c219 3589 /* Try to allocate the color. */
80c32bcc 3590 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3591 if (success_p)
3592 {
3593 if (new.pixel == *pixel)
3594 {
3595 /* If we end up with the same color as before, try adding
3596 delta to the RGB values. */
0d605c67 3597 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3598
3599 new.red = min (0xffff, delta + color.red);
3600 new.green = min (0xffff, delta + color.green);
3601 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3602 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3603 }
3604 else
3605 success_p = 1;
3606 *pixel = new.pixel;
3607 }
3608
3609 return success_p;
3610}
3611
3612
3613/* Set up the foreground color for drawing relief lines of glyph
3614 string S. RELIEF is a pointer to a struct relief containing the GC
3615 with which lines will be drawn. Use a color that is FACTOR or
3616 DELTA lighter or darker than the relief's background which is found
3617 in S->f->output_data.x->relief_background. If such a color cannot
3618 be allocated, use DEFAULT_PIXEL, instead. */
3619
3620static void
3621x_setup_relief_color (f, relief, factor, delta, default_pixel)
3622 struct frame *f;
3623 struct relief *relief;
68c45bf0 3624 double factor;
06a2c219
GM
3625 int delta;
3626 unsigned long default_pixel;
3627{
3628 XGCValues xgcv;
3629 struct x_output *di = f->output_data.x;
3630 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3631 unsigned long pixel;
3632 unsigned long background = di->relief_background;
43bd1b2b 3633 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3634 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3635 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3636
3637 xgcv.graphics_exposures = False;
3638 xgcv.line_width = 1;
3639
3640 /* Free previously allocated color. The color cell will be reused
3641 when it has been freed as many times as it was allocated, so this
3642 doesn't affect faces using the same colors. */
3643 if (relief->gc
3644 && relief->allocated_p)
3645 {
0d605c67 3646 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3647 relief->allocated_p = 0;
3648 }
3649
3650 /* Allocate new color. */
3651 xgcv.foreground = default_pixel;
3652 pixel = background;
dcd08bfb
GM
3653 if (dpyinfo->n_planes != 1
3654 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3655 {
3656 relief->allocated_p = 1;
3657 xgcv.foreground = relief->pixel = pixel;
3658 }
3659
3660 if (relief->gc == 0)
3661 {
dcd08bfb 3662 xgcv.stipple = dpyinfo->gray;
06a2c219 3663 mask |= GCStipple;
dcd08bfb 3664 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3665 }
3666 else
dcd08bfb 3667 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3668}
3669
3670
3671/* Set up colors for the relief lines around glyph string S. */
3672
3673static void
3674x_setup_relief_colors (s)
3675 struct glyph_string *s;
3676{
3677 struct x_output *di = s->f->output_data.x;
3678 unsigned long color;
3679
3680 if (s->face->use_box_color_for_shadows_p)
3681 color = s->face->box_color;
3682 else
3683 {
3684 XGCValues xgcv;
3685
3686 /* Get the background color of the face. */
3687 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3688 color = xgcv.background;
3689 }
3690
3691 if (di->white_relief.gc == 0
3692 || color != di->relief_background)
3693 {
3694 di->relief_background = color;
3695 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3696 WHITE_PIX_DEFAULT (s->f));
3697 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3698 BLACK_PIX_DEFAULT (s->f));
3699 }
3700}
3701
3702
3703/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3704 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3705 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3706 relief. LEFT_P non-zero means draw a relief on the left side of
3707 the rectangle. RIGHT_P non-zero means draw a relief on the right
3708 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3709 when drawing. */
3710
3711static void
3712x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3713 raised_p, left_p, right_p, clip_rect)
3714 struct frame *f;
3715 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3716 XRectangle *clip_rect;
3717{
3718 int i;
3719 GC gc;
3720
3721 if (raised_p)
3722 gc = f->output_data.x->white_relief.gc;
3723 else
3724 gc = f->output_data.x->black_relief.gc;
3725 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3726
3727 /* Top. */
3728 for (i = 0; i < width; ++i)
3729 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3730 left_x + i * left_p, top_y + i,
3731 right_x + 1 - i * right_p, top_y + i);
3732
3733 /* Left. */
3734 if (left_p)
3735 for (i = 0; i < width; ++i)
3736 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3737 left_x + i, top_y + i, left_x + i, bottom_y - i);
3738
3739 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3740 if (raised_p)
3741 gc = f->output_data.x->black_relief.gc;
3742 else
3743 gc = f->output_data.x->white_relief.gc;
3744 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3745
3746 /* Bottom. */
3747 for (i = 0; i < width; ++i)
3748 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3749 left_x + i * left_p, bottom_y - i,
3750 right_x + 1 - i * right_p, bottom_y - i);
3751
3752 /* Right. */
3753 if (right_p)
3754 for (i = 0; i < width; ++i)
3755 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3756 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3757
3758 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3759}
3760
3761
3762/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3763 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3764 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3765 left side of the rectangle. RIGHT_P non-zero means draw a line
3766 on the right side of the rectangle. CLIP_RECT is the clipping
3767 rectangle to use when drawing. */
3768
3769static void
3770x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3771 left_p, right_p, clip_rect)
3772 struct glyph_string *s;
3773 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3774 XRectangle *clip_rect;
3775{
3776 XGCValues xgcv;
3777
3778 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3779 XSetForeground (s->display, s->gc, s->face->box_color);
3780 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3781
3782 /* Top. */
3783 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3784 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3785
3786 /* Left. */
3787 if (left_p)
3788 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3789 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3790
3791 /* Bottom. */
3792 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3793 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3794
3795 /* Right. */
3796 if (right_p)
3797 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3798 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3799
3800 XSetForeground (s->display, s->gc, xgcv.foreground);
3801 XSetClipMask (s->display, s->gc, None);
3802}
3803
3804
3805/* Draw a box around glyph string S. */
3806
3807static void
3808x_draw_glyph_string_box (s)
3809 struct glyph_string *s;
3810{
3811 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3812 int left_p, right_p;
3813 struct glyph *last_glyph;
3814 XRectangle clip_rect;
3815
3816 last_x = window_box_right (s->w, s->area);
3817 if (s->row->full_width_p
3818 && !s->w->pseudo_window_p)
3819 {
110859fc 3820 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3821 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3822 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3823 }
3824
3825 /* The glyph that may have a right box line. */
b4192550 3826 last_glyph = (s->cmp || s->img
06a2c219
GM
3827 ? s->first_glyph
3828 : s->first_glyph + s->nchars - 1);
3829
ea2ba0d4 3830 width = abs (s->face->box_line_width);
06a2c219
GM
3831 raised_p = s->face->box == FACE_RAISED_BOX;
3832 left_x = s->x;
3833 right_x = ((s->row->full_width_p
1da3fd71 3834 ? last_x - 1
a7aeb2de 3835 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3836 top_y = s->y;
3837 bottom_y = top_y + s->height - 1;
3838
3839 left_p = (s->first_glyph->left_box_line_p
3840 || (s->hl == DRAW_MOUSE_FACE
3841 && (s->prev == NULL
3842 || s->prev->hl != s->hl)));
3843 right_p = (last_glyph->right_box_line_p
3844 || (s->hl == DRAW_MOUSE_FACE
3845 && (s->next == NULL
3846 || s->next->hl != s->hl)));
3847
3848 x_get_glyph_string_clip_rect (s, &clip_rect);
3849
3850 if (s->face->box == FACE_SIMPLE_BOX)
3851 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3852 left_p, right_p, &clip_rect);
3853 else
3854 {
3855 x_setup_relief_colors (s);
3856 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3857 width, raised_p, left_p, right_p, &clip_rect);
3858 }
3859}
3860
3861
3862/* Draw foreground of image glyph string S. */
3863
3864static void
3865x_draw_image_foreground (s)
3866 struct glyph_string *s;
3867{
3868 int x;
95af8492 3869 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3870
3871 /* If first glyph of S has a left box line, start drawing it to the
3872 right of that line. */
3873 if (s->face->box != FACE_NO_BOX
3874 && s->first_glyph->left_box_line_p)
ea2ba0d4 3875 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3876 else
3877 x = s->x;
3878
3879 /* If there is a margin around the image, adjust x- and y-position
3880 by that margin. */
22d650b8
GM
3881 x += s->img->hmargin;
3882 y += s->img->vmargin;
06a2c219
GM
3883
3884 if (s->img->pixmap)
3885 {
3886 if (s->img->mask)
3887 {
3888 /* We can't set both a clip mask and use XSetClipRectangles
3889 because the latter also sets a clip mask. We also can't
3890 trust on the shape extension to be available
3891 (XShapeCombineRegion). So, compute the rectangle to draw
3892 manually. */
3893 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3894 | GCFunction);
3895 XGCValues xgcv;
3896 XRectangle clip_rect, image_rect, r;
3897
3898 xgcv.clip_mask = s->img->mask;
3899 xgcv.clip_x_origin = x;
3900 xgcv.clip_y_origin = y;
3901 xgcv.function = GXcopy;
3902 XChangeGC (s->display, s->gc, mask, &xgcv);
3903
3904 x_get_glyph_string_clip_rect (s, &clip_rect);
3905 image_rect.x = x;
3906 image_rect.y = y;
3907 image_rect.width = s->img->width;
3908 image_rect.height = s->img->height;
3909 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3910 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3911 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3912 }
3913 else
3914 {
49ad1d99
GM
3915 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3916 XGCValues xgcv;
3917 XRectangle clip_rect, image_rect, r;
3918
3919 x_get_glyph_string_clip_rect (s, &clip_rect);
3920 image_rect.x = x;
3921 image_rect.y = y;
3922 image_rect.width = s->img->width;
3923 image_rect.height = s->img->height;
3924 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3925 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3926 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3927
3928 /* When the image has a mask, we can expect that at
3929 least part of a mouse highlight or a block cursor will
3930 be visible. If the image doesn't have a mask, make
3931 a block cursor visible by drawing a rectangle around
3932 the image. I believe it's looking better if we do
3933 nothing here for mouse-face. */
3934 if (s->hl == DRAW_CURSOR)
3935 XDrawRectangle (s->display, s->window, s->gc, x, y,
3936 s->img->width - 1, s->img->height - 1);
3937 }
3938 }
3939 else
3940 /* Draw a rectangle if image could not be loaded. */
3941 XDrawRectangle (s->display, s->window, s->gc, x, y,
3942 s->img->width - 1, s->img->height - 1);
3943}
3944
3945
3946/* Draw a relief around the image glyph string S. */
3947
3948static void
3949x_draw_image_relief (s)
3950 struct glyph_string *s;
3951{
3952 int x0, y0, x1, y1, thick, raised_p;
3953 XRectangle r;
3954 int x;
95af8492 3955 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3956
3957 /* If first glyph of S has a left box line, start drawing it to the
3958 right of that line. */
3959 if (s->face->box != FACE_NO_BOX
3960 && s->first_glyph->left_box_line_p)
ea2ba0d4 3961 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3962 else
3963 x = s->x;
3964
3965 /* If there is a margin around the image, adjust x- and y-position
3966 by that margin. */
22d650b8
GM
3967 x += s->img->hmargin;
3968 y += s->img->vmargin;
06a2c219
GM
3969
3970 if (s->hl == DRAW_IMAGE_SUNKEN
3971 || s->hl == DRAW_IMAGE_RAISED)
3972 {
9ea173e8 3973 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3974 raised_p = s->hl == DRAW_IMAGE_RAISED;
3975 }
3976 else
3977 {
3978 thick = abs (s->img->relief);
3979 raised_p = s->img->relief > 0;
3980 }
3981
3982 x0 = x - thick;
3983 y0 = y - thick;
3984 x1 = x + s->img->width + thick - 1;
3985 y1 = y + s->img->height + thick - 1;
3986
3987 x_setup_relief_colors (s);
3988 x_get_glyph_string_clip_rect (s, &r);
3989 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3990}
3991
3992
3993/* Draw the foreground of image glyph string S to PIXMAP. */
3994
3995static void
3996x_draw_image_foreground_1 (s, pixmap)
3997 struct glyph_string *s;
3998 Pixmap pixmap;
3999{
4000 int x;
95af8492 4001 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4002
4003 /* If first glyph of S has a left box line, start drawing it to the
4004 right of that line. */
4005 if (s->face->box != FACE_NO_BOX
4006 && s->first_glyph->left_box_line_p)
ea2ba0d4 4007 x = abs (s->face->box_line_width);
06a2c219
GM
4008 else
4009 x = 0;
4010
4011 /* If there is a margin around the image, adjust x- and y-position
4012 by that margin. */
22d650b8
GM
4013 x += s->img->hmargin;
4014 y += s->img->vmargin;
dc43ef94 4015
06a2c219
GM
4016 if (s->img->pixmap)
4017 {
4018 if (s->img->mask)
4019 {
4020 /* We can't set both a clip mask and use XSetClipRectangles
4021 because the latter also sets a clip mask. We also can't
4022 trust on the shape extension to be available
4023 (XShapeCombineRegion). So, compute the rectangle to draw
4024 manually. */
4025 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4026 | GCFunction);
4027 XGCValues xgcv;
4028
4029 xgcv.clip_mask = s->img->mask;
4030 xgcv.clip_x_origin = x;
4031 xgcv.clip_y_origin = y;
4032 xgcv.function = GXcopy;
4033 XChangeGC (s->display, s->gc, mask, &xgcv);
4034
4035 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4036 0, 0, s->img->width, s->img->height, x, y);
4037 XSetClipMask (s->display, s->gc, None);
4038 }
4039 else
4040 {
4041 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4042 0, 0, s->img->width, s->img->height, x, y);
4043
4044 /* When the image has a mask, we can expect that at
4045 least part of a mouse highlight or a block cursor will
4046 be visible. If the image doesn't have a mask, make
4047 a block cursor visible by drawing a rectangle around
4048 the image. I believe it's looking better if we do
4049 nothing here for mouse-face. */
4050 if (s->hl == DRAW_CURSOR)
4051 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4052 s->img->width - 1, s->img->height - 1);
4053 }
4054 }
4055 else
4056 /* Draw a rectangle if image could not be loaded. */
4057 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4058 s->img->width - 1, s->img->height - 1);
4059}
dc43ef94 4060
990ba854 4061
06a2c219
GM
4062/* Draw part of the background of glyph string S. X, Y, W, and H
4063 give the rectangle to draw. */
a9a5b0a5 4064
06a2c219
GM
4065static void
4066x_draw_glyph_string_bg_rect (s, x, y, w, h)
4067 struct glyph_string *s;
4068 int x, y, w, h;
4069{
4070 if (s->stippled_p)
4071 {
4072 /* Fill background with a stipple pattern. */
4073 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4074 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4075 XSetFillStyle (s->display, s->gc, FillSolid);
4076 }
4077 else
4078 x_clear_glyph_string_rect (s, x, y, w, h);
4079}
07e34cb0 4080
b5210ea7 4081
06a2c219 4082/* Draw image glyph string S.
dc43ef94 4083
06a2c219
GM
4084 s->y
4085 s->x +-------------------------
4086 | s->face->box
4087 |
4088 | +-------------------------
4089 | | s->img->margin
4090 | |
4091 | | +-------------------
4092 | | | the image
dc43ef94 4093
06a2c219 4094 */
dc43ef94 4095
06a2c219
GM
4096static void
4097x_draw_image_glyph_string (s)
4098 struct glyph_string *s;
4099{
4100 int x, y;
ea2ba0d4
KH
4101 int box_line_hwidth = abs (s->face->box_line_width);
4102 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4103 int height;
4104 Pixmap pixmap = None;
4105
ea2ba0d4 4106 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4107
4108 /* Fill background with face under the image. Do it only if row is
4109 taller than image or if image has a clip mask to reduce
4110 flickering. */
4111 s->stippled_p = s->face->stipple != 0;
4112 if (height > s->img->height
22d650b8
GM
4113 || s->img->hmargin
4114 || s->img->vmargin
06a2c219
GM
4115 || s->img->mask
4116 || s->img->pixmap == 0
4117 || s->width != s->background_width)
4118 {
ea2ba0d4
KH
4119 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4120 x = s->x + box_line_hwidth;
06a2c219
GM
4121 else
4122 x = s->x;
4123
ea2ba0d4 4124 y = s->y + box_line_vwidth;
06a2c219
GM
4125
4126 if (s->img->mask)
4127 {
f9b5db02
GM
4128 /* Create a pixmap as large as the glyph string. Fill it
4129 with the background color. Copy the image to it, using
4130 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4131 Screen *screen = FRAME_X_SCREEN (s->f);
4132 int depth = DefaultDepthOfScreen (screen);
4133
4134 /* Create a pixmap as large as the glyph string. */
4135 pixmap = XCreatePixmap (s->display, s->window,
4136 s->background_width,
4137 s->height, depth);
4138
4139 /* Don't clip in the following because we're working on the
4140 pixmap. */
4141 XSetClipMask (s->display, s->gc, None);
4142
4143 /* Fill the pixmap with the background color/stipple. */
4144 if (s->stippled_p)
4145 {
4146 /* Fill background with a stipple pattern. */
4147 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4148 XFillRectangle (s->display, pixmap, s->gc,
4149 0, 0, s->background_width, s->height);
4150 XSetFillStyle (s->display, s->gc, FillSolid);
4151 }
4152 else
4153 {
4154 XGCValues xgcv;
4155 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4156 &xgcv);
4157 XSetForeground (s->display, s->gc, xgcv.background);
4158 XFillRectangle (s->display, pixmap, s->gc,
4159 0, 0, s->background_width, s->height);
4160 XSetForeground (s->display, s->gc, xgcv.foreground);
4161 }
4162 }
4163 else
06a2c219
GM
4164 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4165
4166 s->background_filled_p = 1;
4167 }
dc43ef94 4168
06a2c219
GM
4169 /* Draw the foreground. */
4170 if (pixmap != None)
4171 {
4172 x_draw_image_foreground_1 (s, pixmap);
4173 x_set_glyph_string_clipping (s);
4174 XCopyArea (s->display, pixmap, s->window, s->gc,
4175 0, 0, s->background_width, s->height, s->x, s->y);
4176 XFreePixmap (s->display, pixmap);
4177 }
4178 else
4179 x_draw_image_foreground (s);
b5210ea7 4180
06a2c219
GM
4181 /* If we must draw a relief around the image, do it. */
4182 if (s->img->relief
4183 || s->hl == DRAW_IMAGE_RAISED
4184 || s->hl == DRAW_IMAGE_SUNKEN)
4185 x_draw_image_relief (s);
4186}
8c1a6a84 4187
990ba854 4188
06a2c219 4189/* Draw stretch glyph string S. */
dc43ef94 4190
06a2c219
GM
4191static void
4192x_draw_stretch_glyph_string (s)
4193 struct glyph_string *s;
4194{
4195 xassert (s->first_glyph->type == STRETCH_GLYPH);
4196 s->stippled_p = s->face->stipple != 0;
990ba854 4197
06a2c219
GM
4198 if (s->hl == DRAW_CURSOR
4199 && !x_stretch_cursor_p)
4200 {
4201 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4202 as wide as the stretch glyph. */
4203 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4204
06a2c219
GM
4205 /* Draw cursor. */
4206 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4207
06a2c219
GM
4208 /* Clear rest using the GC of the original non-cursor face. */
4209 if (width < s->background_width)
4210 {
4211 GC gc = s->face->gc;
4212 int x = s->x + width, y = s->y;
4213 int w = s->background_width - width, h = s->height;
4214 XRectangle r;
dc43ef94 4215
06a2c219
GM
4216 x_get_glyph_string_clip_rect (s, &r);
4217 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4218
06a2c219
GM
4219 if (s->face->stipple)
4220 {
4221 /* Fill background with a stipple pattern. */
4222 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4223 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4224 XSetFillStyle (s->display, gc, FillSolid);
4225 }
4226 else
4227 {
4228 XGCValues xgcv;
4229 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4230 XSetForeground (s->display, gc, xgcv.background);
4231 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4232 XSetForeground (s->display, gc, xgcv.foreground);
4233 }
4234 }
4235 }
4236 else
4237 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4238 s->height);
4239
4240 s->background_filled_p = 1;
4241}
4242
4243
4244/* Draw glyph string S. */
4245
4246static void
4247x_draw_glyph_string (s)
4248 struct glyph_string *s;
4249{
4250 /* If S draws into the background of its successor, draw the
4251 background of the successor first so that S can draw into it.
4252 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4253 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4254 {
4255 xassert (s->next->img == NULL);
4256 x_set_glyph_string_gc (s->next);
4257 x_set_glyph_string_clipping (s->next);
4258 x_draw_glyph_string_background (s->next, 1);
4259 }
97210f4e 4260
06a2c219
GM
4261 /* Set up S->gc, set clipping and draw S. */
4262 x_set_glyph_string_gc (s);
4263 x_set_glyph_string_clipping (s);
4264
4265 switch (s->first_glyph->type)
4266 {
4267 case IMAGE_GLYPH:
4268 x_draw_image_glyph_string (s);
4269 break;
4270
4271 case STRETCH_GLYPH:
4272 x_draw_stretch_glyph_string (s);
4273 break;
4274
4275 case CHAR_GLYPH:
66ac4b0e
GM
4276 if (s->for_overlaps_p)
4277 s->background_filled_p = 1;
4278 else
4279 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4280 x_draw_glyph_string_foreground (s);
4281 break;
4282
b4192550
KH
4283 case COMPOSITE_GLYPH:
4284 if (s->for_overlaps_p || s->gidx > 0)
4285 s->background_filled_p = 1;
4286 else
4287 x_draw_glyph_string_background (s, 1);
4288 x_draw_composite_glyph_string_foreground (s);
4289 break;
4290
06a2c219
GM
4291 default:
4292 abort ();
4293 }
4294
66ac4b0e 4295 if (!s->for_overlaps_p)
06a2c219 4296 {
66ac4b0e
GM
4297 /* Draw underline. */
4298 if (s->face->underline_p)
4299 {
e24e84cc
GM
4300 unsigned long tem, h;
4301 int y;
06a2c219 4302
e24e84cc 4303 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4304 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4305 h = 1;
e24e84cc
GM
4306
4307 /* Get the underline position. This is the recommended
4308 vertical offset in pixels from the baseline to the top of
4309 the underline. This is a signed value according to the
4310 specs, and its default is
4311
4312 ROUND ((maximum descent) / 2), with
4313 ROUND(x) = floor (x + 0.5) */
4314
4315 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4316 y = s->ybase + (long) tem;
4317 else if (s->face->font)
4318 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4319 else
4320 y = s->height - h;
06a2c219 4321
66ac4b0e 4322 if (s->face->underline_defaulted_p)
e24e84cc
GM
4323 XFillRectangle (s->display, s->window, s->gc,
4324 s->x, y, s->width, h);
66ac4b0e
GM
4325 else
4326 {
4327 XGCValues xgcv;
4328 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4329 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4330 XFillRectangle (s->display, s->window, s->gc,
4331 s->x, y, s->width, h);
66ac4b0e
GM
4332 XSetForeground (s->display, s->gc, xgcv.foreground);
4333 }
dc6f92b8 4334 }
07e34cb0 4335
66ac4b0e
GM
4336 /* Draw overline. */
4337 if (s->face->overline_p)
06a2c219 4338 {
66ac4b0e
GM
4339 unsigned long dy = 0, h = 1;
4340
4341 if (s->face->overline_color_defaulted_p)
4342 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4343 s->width, h);
4344 else
4345 {
4346 XGCValues xgcv;
4347 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4348 XSetForeground (s->display, s->gc, s->face->overline_color);
4349 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4350 s->width, h);
4351 XSetForeground (s->display, s->gc, xgcv.foreground);
4352 }
06a2c219 4353 }
06a2c219 4354
66ac4b0e
GM
4355 /* Draw strike-through. */
4356 if (s->face->strike_through_p)
06a2c219 4357 {
66ac4b0e
GM
4358 unsigned long h = 1;
4359 unsigned long dy = (s->height - h) / 2;
4360
4361 if (s->face->strike_through_color_defaulted_p)
4362 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4363 s->width, h);
4364 else
4365 {
4366 XGCValues xgcv;
4367 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4368 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4369 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4370 s->width, h);
4371 XSetForeground (s->display, s->gc, xgcv.foreground);
4372 }
06a2c219 4373 }
06a2c219 4374
66ac4b0e
GM
4375 /* Draw relief. */
4376 if (s->face->box != FACE_NO_BOX)
4377 x_draw_glyph_string_box (s);
4378 }
06a2c219
GM
4379
4380 /* Reset clipping. */
4381 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4382}
07e34cb0 4383
06a2c219 4384
b4192550
KH
4385static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4386 struct face **, int));
06a2c219 4387
06a2c219 4388
209f68d9
GM
4389/* Fill glyph string S with composition components specified by S->cmp.
4390
b4192550
KH
4391 FACES is an array of faces for all components of this composition.
4392 S->gidx is the index of the first component for S.
4393 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4394 use its physical height for clipping.
06a2c219 4395
b4192550 4396 Value is the index of a component not in S. */
07e34cb0 4397
b4192550
KH
4398static int
4399x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4400 struct glyph_string *s;
b4192550 4401 struct face **faces;
66ac4b0e 4402 int overlaps_p;
07e34cb0 4403{
b4192550 4404 int i;
06a2c219 4405
b4192550 4406 xassert (s);
06a2c219 4407
b4192550 4408 s->for_overlaps_p = overlaps_p;
06a2c219 4409
b4192550
KH
4410 s->face = faces[s->gidx];
4411 s->font = s->face->font;
4412 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4413
b4192550
KH
4414 /* For all glyphs of this composition, starting at the offset
4415 S->gidx, until we reach the end of the definition or encounter a
4416 glyph that requires the different face, add it to S. */
4417 ++s->nchars;
4418 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4419 ++s->nchars;
06a2c219 4420
b4192550
KH
4421 /* All glyph strings for the same composition has the same width,
4422 i.e. the width set for the first component of the composition. */
06a2c219 4423
06a2c219
GM
4424 s->width = s->first_glyph->pixel_width;
4425
4426 /* If the specified font could not be loaded, use the frame's
4427 default font, but record the fact that we couldn't load it in
4428 the glyph string so that we can draw rectangles for the
4429 characters of the glyph string. */
4430 if (s->font == NULL)
4431 {
4432 s->font_not_found_p = 1;
4433 s->font = FRAME_FONT (s->f);
4434 }
4435
4436 /* Adjust base line for subscript/superscript text. */
4437 s->ybase += s->first_glyph->voffset;
4438
4439 xassert (s->face && s->face->gc);
4440
4441 /* This glyph string must always be drawn with 16-bit functions. */
4442 s->two_byte_p = 1;
b4192550
KH
4443
4444 return s->gidx + s->nchars;
06a2c219
GM
4445}
4446
4447
209f68d9
GM
4448/* Fill glyph string S from a sequence of character glyphs.
4449
06a2c219 4450 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4451 first glyph to consider, END is the index of the last + 1.
4452 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4453 use its physical height for clipping.
66ac4b0e
GM
4454
4455 Value is the index of the first glyph not in S. */
06a2c219
GM
4456
4457static int
66ac4b0e 4458x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4459 struct glyph_string *s;
4460 int face_id;
66ac4b0e 4461 int start, end, overlaps_p;
06a2c219
GM
4462{
4463 struct glyph *glyph, *last;
4464 int voffset;
ee569018 4465 int glyph_not_available_p;
06a2c219 4466
06a2c219
GM
4467 xassert (s->f == XFRAME (s->w->frame));
4468 xassert (s->nchars == 0);
4469 xassert (start >= 0 && end > start);
4470
66ac4b0e 4471 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4472 glyph = s->row->glyphs[s->area] + start;
4473 last = s->row->glyphs[s->area] + end;
4474 voffset = glyph->voffset;
4475
ee569018
KH
4476 glyph_not_available_p = glyph->glyph_not_available_p;
4477
06a2c219
GM
4478 while (glyph < last
4479 && glyph->type == CHAR_GLYPH
4480 && glyph->voffset == voffset
ee569018
KH
4481 /* Same face id implies same font, nowadays. */
4482 && glyph->face_id == face_id
4483 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4484 {
ee569018
KH
4485 int two_byte_p;
4486
06a2c219 4487 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4488 s->char2b + s->nchars,
4489 &two_byte_p);
4490 s->two_byte_p = two_byte_p;
06a2c219
GM
4491 ++s->nchars;
4492 xassert (s->nchars <= end - start);
4493 s->width += glyph->pixel_width;
4494 ++glyph;
4495 }
4496
4497 s->font = s->face->font;
4498 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4499
4500 /* If the specified font could not be loaded, use the frame's font,
4501 but record the fact that we couldn't load it in
4502 S->font_not_found_p so that we can draw rectangles for the
4503 characters of the glyph string. */
ee569018 4504 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4505 {
4506 s->font_not_found_p = 1;
4507 s->font = FRAME_FONT (s->f);
4508 }
4509
4510 /* Adjust base line for subscript/superscript text. */
4511 s->ybase += voffset;
66ac4b0e 4512
06a2c219
GM
4513 xassert (s->face && s->face->gc);
4514 return glyph - s->row->glyphs[s->area];
07e34cb0 4515}
dc6f92b8 4516
06a2c219
GM
4517
4518/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4519
dfcf069d 4520static void
06a2c219
GM
4521x_fill_image_glyph_string (s)
4522 struct glyph_string *s;
4523{
4524 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4525 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4526 xassert (s->img);
43d120d8 4527 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4528 s->font = s->face->font;
4529 s->width = s->first_glyph->pixel_width;
4530
4531 /* Adjust base line for subscript/superscript text. */
4532 s->ybase += s->first_glyph->voffset;
4533}
4534
4535
209f68d9 4536/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4537
209f68d9
GM
4538 ROW is the glyph row in which the glyphs are found, AREA is the
4539 area within the row. START is the index of the first glyph to
4540 consider, END is the index of the last + 1.
4541
4542 Value is the index of the first glyph not in S. */
4543
4544static int
4545x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4546 struct glyph_string *s;
209f68d9
GM
4547 struct glyph_row *row;
4548 enum glyph_row_area area;
4549 int start, end;
06a2c219 4550{
209f68d9
GM
4551 struct glyph *glyph, *last;
4552 int voffset, face_id;
4553
06a2c219 4554 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4555
4556 glyph = s->row->glyphs[s->area] + start;
4557 last = s->row->glyphs[s->area] + end;
4558 face_id = glyph->face_id;
4559 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4560 s->font = s->face->font;
209f68d9
GM
4561 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4562 s->width = glyph->pixel_width;
4563 voffset = glyph->voffset;
4564
4565 for (++glyph;
4566 (glyph < last
4567 && glyph->type == STRETCH_GLYPH
4568 && glyph->voffset == voffset
4569 && glyph->face_id == face_id);
4570 ++glyph)
4571 s->width += glyph->pixel_width;
06a2c219
GM
4572
4573 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4574 s->ybase += voffset;
4575
4576 xassert (s->face && s->face->gc);
4577 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4578}
4579
4580
4581/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4582 of XChar2b structures for S; it can't be allocated in
4583 x_init_glyph_string because it must be allocated via `alloca'. W
4584 is the window on which S is drawn. ROW and AREA are the glyph row
4585 and area within the row from which S is constructed. START is the
4586 index of the first glyph structure covered by S. HL is a
4587 face-override for drawing S. */
4588
4589static void
4590x_init_glyph_string (s, char2b, w, row, area, start, hl)
4591 struct glyph_string *s;
4592 XChar2b *char2b;
4593 struct window *w;
4594 struct glyph_row *row;
4595 enum glyph_row_area area;
4596 int start;
4597 enum draw_glyphs_face hl;
4598{
4599 bzero (s, sizeof *s);
4600 s->w = w;
4601 s->f = XFRAME (w->frame);
4602 s->display = FRAME_X_DISPLAY (s->f);
4603 s->window = FRAME_X_WINDOW (s->f);
4604 s->char2b = char2b;
4605 s->hl = hl;
4606 s->row = row;
4607 s->area = area;
4608 s->first_glyph = row->glyphs[area] + start;
4609 s->height = row->height;
4610 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4611
9ea173e8
GM
4612 /* Display the internal border below the tool-bar window. */
4613 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4614 s->y -= s->f->output_data.x->internal_border_width;
4615
4616 s->ybase = s->y + row->ascent;
4617}
4618
4619
4620/* Set background width of glyph string S. START is the index of the
4621 first glyph following S. LAST_X is the right-most x-position + 1
4622 in the drawing area. */
4623
4624static INLINE void
4625x_set_glyph_string_background_width (s, start, last_x)
4626 struct glyph_string *s;
4627 int start;
4628 int last_x;
4629{
4630 /* If the face of this glyph string has to be drawn to the end of
4631 the drawing area, set S->extends_to_end_of_line_p. */
4632 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4633
4634 if (start == s->row->used[s->area]
4635 && s->hl == DRAW_NORMAL_TEXT
eb79f5cc
GM
4636 && s->area == TEXT_AREA
4637 && (s->row->fill_line_p
06a2c219
GM
4638 || s->face->background != default_face->background
4639 || s->face->stipple != default_face->stipple))
4640 s->extends_to_end_of_line_p = 1;
4641
4642 /* If S extends its face to the end of the line, set its
4643 background_width to the distance to the right edge of the drawing
4644 area. */
4645 if (s->extends_to_end_of_line_p)
1da3fd71 4646 s->background_width = last_x - s->x + 1;
06a2c219
GM
4647 else
4648 s->background_width = s->width;
4649}
4650
4651
4652/* Add a glyph string for a stretch glyph to the list of strings
4653 between HEAD and TAIL. START is the index of the stretch glyph in
4654 row area AREA of glyph row ROW. END is the index of the last glyph
4655 in that glyph row area. X is the current output position assigned
4656 to the new glyph string constructed. HL overrides that face of the
4657 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4658 is the right-most x-position of the drawing area. */
4659
8abee2e1
DL
4660/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4661 and below -- keep them on one line. */
4662#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4663 do \
4664 { \
4665 s = (struct glyph_string *) alloca (sizeof *s); \
4666 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4667 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4668 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4669 s->x = (X); \
4670 } \
4671 while (0)
4672
4673
4674/* Add a glyph string for an image glyph to the list of strings
4675 between HEAD and TAIL. START is the index of the image glyph in
4676 row area AREA of glyph row ROW. END is the index of the last glyph
4677 in that glyph row area. X is the current output position assigned
4678 to the new glyph string constructed. HL overrides that face of the
4679 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4680 is the right-most x-position of the drawing area. */
4681
8abee2e1 4682#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4683 do \
4684 { \
4685 s = (struct glyph_string *) alloca (sizeof *s); \
4686 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4687 x_fill_image_glyph_string (s); \
4688 x_append_glyph_string (&HEAD, &TAIL, s); \
4689 ++START; \
4690 s->x = (X); \
4691 } \
4692 while (0)
4693
4694
4695/* Add a glyph string for a sequence of character glyphs to the list
4696 of strings between HEAD and TAIL. START is the index of the first
4697 glyph in row area AREA of glyph row ROW that is part of the new
4698 glyph string. END is the index of the last glyph in that glyph row
4699 area. X is the current output position assigned to the new glyph
4700 string constructed. HL overrides that face of the glyph; e.g. it
4701 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4702 right-most x-position of the drawing area. */
4703
8abee2e1 4704#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4705 do \
4706 { \
3e71d8f2 4707 int c, face_id; \
06a2c219
GM
4708 XChar2b *char2b; \
4709 \
43d120d8 4710 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4711 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4712 \
b4192550
KH
4713 s = (struct glyph_string *) alloca (sizeof *s); \
4714 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4715 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4716 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4717 s->x = (X); \
4718 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4719 OVERLAPS_P); \
06a2c219
GM
4720 } \
4721 while (0)
4722
4723
b4192550
KH
4724/* Add a glyph string for a composite sequence to the list of strings
4725 between HEAD and TAIL. START is the index of the first glyph in
4726 row area AREA of glyph row ROW that is part of the new glyph
4727 string. END is the index of the last glyph in that glyph row area.
4728 X is the current output position assigned to the new glyph string
4729 constructed. HL overrides that face of the glyph; e.g. it is
4730 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4731 x-position of the drawing area. */
4732
6c27ec25 4733#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4734 do { \
43d120d8
KH
4735 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4736 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4737 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4738 struct composition *cmp = composition_table[cmp_id]; \
4739 int glyph_len = cmp->glyph_len; \
4740 XChar2b *char2b; \
4741 struct face **faces; \
4742 struct glyph_string *first_s = NULL; \
4743 int n; \
4744 \
ee569018 4745 base_face = base_face->ascii_face; \
b4192550
KH
4746 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4747 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4748 /* At first, fill in `char2b' and `faces'. */ \
4749 for (n = 0; n < glyph_len; n++) \
4750 { \
43d120d8 4751 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4752 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4753 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4754 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4755 this_face_id, char2b + n, 1); \
b4192550
KH
4756 } \
4757 \
4758 /* Make glyph_strings for each glyph sequence that is drawable by \
4759 the same face, and append them to HEAD/TAIL. */ \
4760 for (n = 0; n < cmp->glyph_len;) \
4761 { \
4762 s = (struct glyph_string *) alloca (sizeof *s); \
4763 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4764 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4765 s->cmp = cmp; \
4766 s->gidx = n; \
b4192550
KH
4767 s->x = (X); \
4768 \
4769 if (n == 0) \
4770 first_s = s; \
4771 \
4772 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4773 } \
4774 \
4775 ++START; \
4776 s = first_s; \
4777 } while (0)
4778
4779
06a2c219
GM
4780/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4781 of AREA of glyph row ROW on window W between indices START and END.
4782 HL overrides the face for drawing glyph strings, e.g. it is
4783 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4784 x-positions of the drawing area.
4785
4786 This is an ugly monster macro construct because we must use alloca
4787 to allocate glyph strings (because x_draw_glyphs can be called
4788 asynchronously). */
4789
8abee2e1 4790#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4791 do \
4792 { \
4793 HEAD = TAIL = NULL; \
4794 while (START < END) \
4795 { \
4796 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4797 switch (first_glyph->type) \
4798 { \
4799 case CHAR_GLYPH: \
4800 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4801 TAIL, HL, X, LAST_X, \
4802 OVERLAPS_P); \
06a2c219
GM
4803 break; \
4804 \
b4192550
KH
4805 case COMPOSITE_GLYPH: \
4806 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4807 HEAD, TAIL, HL, X, LAST_X,\
4808 OVERLAPS_P); \
4809 break; \
4810 \
06a2c219
GM
4811 case STRETCH_GLYPH: \
4812 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4813 HEAD, TAIL, HL, X, LAST_X); \
4814 break; \
4815 \
4816 case IMAGE_GLYPH: \
4817 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4818 TAIL, HL, X, LAST_X); \
4819 break; \
4820 \
4821 default: \
4822 abort (); \
4823 } \
4824 \
4825 x_set_glyph_string_background_width (s, START, LAST_X); \
4826 (X) += s->width; \
4827 } \
4828 } \
4829 while (0)
4830
4831
4832/* Draw glyphs between START and END in AREA of ROW on window W,
4833 starting at x-position X. X is relative to AREA in W. HL is a
4834 face-override with the following meaning:
4835
4836 DRAW_NORMAL_TEXT draw normally
4837 DRAW_CURSOR draw in cursor face
4838 DRAW_MOUSE_FACE draw in mouse face.
4839 DRAW_INVERSE_VIDEO draw in mode line face
4840 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4841 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4842
4843 If REAL_START is non-null, return in *REAL_START the real starting
4844 position for display. This can be different from START in case
4845 overlapping glyphs must be displayed. If REAL_END is non-null,
4846 return in *REAL_END the real end position for display. This can be
4847 different from END in case overlapping glyphs must be displayed.
4848
66ac4b0e
GM
4849 If OVERLAPS_P is non-zero, draw only the foreground of characters
4850 and clip to the physical height of ROW.
4851
06a2c219
GM
4852 Value is the x-position reached, relative to AREA of W. */
4853
4854static int
66ac4b0e
GM
4855x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4856 overlaps_p)
06a2c219
GM
4857 struct window *w;
4858 int x;
4859 struct glyph_row *row;
4860 enum glyph_row_area area;
4861 int start, end;
4862 enum draw_glyphs_face hl;
4863 int *real_start, *real_end;
66ac4b0e 4864 int overlaps_p;
dc6f92b8 4865{
06a2c219
GM
4866 struct glyph_string *head, *tail;
4867 struct glyph_string *s;
4868 int last_x, area_width;
4869 int x_reached;
4870 int i, j;
4871
4872 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 4873 end = min (end, row->used[area]);
a8710abf
GM
4874 start = max (0, start);
4875 start = min (end, start);
06a2c219
GM
4876 if (real_start)
4877 *real_start = start;
4878 if (real_end)
4879 *real_end = end;
4880
4881 /* Translate X to frame coordinates. Set last_x to the right
4882 end of the drawing area. */
4883 if (row->full_width_p)
4884 {
4885 /* X is relative to the left edge of W, without scroll bars
4886 or flag areas. */
4887 struct frame *f = XFRAME (w->frame);
110859fc 4888 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4889 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4890
06a2c219
GM
4891 x += window_left_x;
4892 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4893 last_x = window_left_x + area_width;
4894
4895 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4896 {
110859fc 4897 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4898 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4899 last_x += width;
4900 else
4901 x -= width;
4902 }
dc6f92b8 4903
b9432a85
GM
4904 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4905 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4906 }
4907 else
dc6f92b8 4908 {
06a2c219
GM
4909 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4910 area_width = window_box_width (w, area);
4911 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4912 }
4913
06a2c219
GM
4914 /* Build a doubly-linked list of glyph_string structures between
4915 head and tail from what we have to draw. Note that the macro
4916 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4917 the reason we use a separate variable `i'. */
4918 i = start;
66ac4b0e
GM
4919 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4920 overlaps_p);
06a2c219
GM
4921 if (tail)
4922 x_reached = tail->x + tail->background_width;
4923 else
4924 x_reached = x;
90e65f07 4925
06a2c219
GM
4926 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4927 the row, redraw some glyphs in front or following the glyph
4928 strings built above. */
a8710abf 4929 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4930 {
4931 int dummy_x = 0;
4932 struct glyph_string *h, *t;
4933
4934 /* Compute overhangs for all glyph strings. */
4935 for (s = head; s; s = s->next)
4936 x_compute_glyph_string_overhangs (s);
4937
4938 /* Prepend glyph strings for glyphs in front of the first glyph
4939 string that are overwritten because of the first glyph
4940 string's left overhang. The background of all strings
4941 prepended must be drawn because the first glyph string
4942 draws over it. */
4943 i = x_left_overwritten (head);
4944 if (i >= 0)
4945 {
4946 j = i;
4947 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4948 DRAW_NORMAL_TEXT, dummy_x, last_x,
4949 overlaps_p);
06a2c219
GM
4950 start = i;
4951 if (real_start)
4952 *real_start = start;
4953 x_compute_overhangs_and_x (t, head->x, 1);
4954 x_prepend_glyph_string_lists (&head, &tail, h, t);
4955 }
58769bee 4956
06a2c219
GM
4957 /* Prepend glyph strings for glyphs in front of the first glyph
4958 string that overwrite that glyph string because of their
4959 right overhang. For these strings, only the foreground must
4960 be drawn, because it draws over the glyph string at `head'.
4961 The background must not be drawn because this would overwrite
4962 right overhangs of preceding glyphs for which no glyph
4963 strings exist. */
4964 i = x_left_overwriting (head);
4965 if (i >= 0)
4966 {
4967 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4968 DRAW_NORMAL_TEXT, dummy_x, last_x,
4969 overlaps_p);
06a2c219
GM
4970 for (s = h; s; s = s->next)
4971 s->background_filled_p = 1;
4972 if (real_start)
4973 *real_start = i;
4974 x_compute_overhangs_and_x (t, head->x, 1);
4975 x_prepend_glyph_string_lists (&head, &tail, h, t);
4976 }
dbcb258a 4977
06a2c219
GM
4978 /* Append glyphs strings for glyphs following the last glyph
4979 string tail that are overwritten by tail. The background of
4980 these strings has to be drawn because tail's foreground draws
4981 over it. */
4982 i = x_right_overwritten (tail);
4983 if (i >= 0)
4984 {
4985 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4986 DRAW_NORMAL_TEXT, x, last_x,
4987 overlaps_p);
06a2c219
GM
4988 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4989 x_append_glyph_string_lists (&head, &tail, h, t);
4990 if (real_end)
4991 *real_end = i;
4992 }
dc6f92b8 4993
06a2c219
GM
4994 /* Append glyph strings for glyphs following the last glyph
4995 string tail that overwrite tail. The foreground of such
4996 glyphs has to be drawn because it writes into the background
4997 of tail. The background must not be drawn because it could
4998 paint over the foreground of following glyphs. */
4999 i = x_right_overwriting (tail);
5000 if (i >= 0)
5001 {
5002 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5003 DRAW_NORMAL_TEXT, x, last_x,
5004 overlaps_p);
06a2c219
GM
5005 for (s = h; s; s = s->next)
5006 s->background_filled_p = 1;
5007 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5008 x_append_glyph_string_lists (&head, &tail, h, t);
5009 if (real_end)
5010 *real_end = i;
5011 }
5012 }
58769bee 5013
06a2c219
GM
5014 /* Draw all strings. */
5015 for (s = head; s; s = s->next)
5016 x_draw_glyph_string (s);
dc6f92b8 5017
06a2c219
GM
5018 /* Value is the x-position up to which drawn, relative to AREA of W.
5019 This doesn't include parts drawn because of overhangs. */
5020 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5021 if (!row->full_width_p)
5022 {
5023 if (area > LEFT_MARGIN_AREA)
5024 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5025 if (area > TEXT_AREA)
5026 x_reached -= window_box_width (w, TEXT_AREA);
5027 }
a8710abf 5028
06a2c219
GM
5029 return x_reached;
5030}
dc6f92b8 5031
dc6f92b8 5032
66ac4b0e
GM
5033/* Fix the display of area AREA of overlapping row ROW in window W. */
5034
5035static void
5036x_fix_overlapping_area (w, row, area)
5037 struct window *w;
5038 struct glyph_row *row;
5039 enum glyph_row_area area;
5040{
5041 int i, x;
5042
5043 BLOCK_INPUT;
5044
5045 if (area == LEFT_MARGIN_AREA)
5046 x = 0;
5047 else if (area == TEXT_AREA)
5048 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5049 else
5050 x = (window_box_width (w, LEFT_MARGIN_AREA)
5051 + window_box_width (w, TEXT_AREA));
5052
5053 for (i = 0; i < row->used[area];)
5054 {
5055 if (row->glyphs[area][i].overlaps_vertically_p)
5056 {
5057 int start = i, start_x = x;
5058
5059 do
5060 {
5061 x += row->glyphs[area][i].pixel_width;
5062 ++i;
5063 }
5064 while (i < row->used[area]
5065 && row->glyphs[area][i].overlaps_vertically_p);
5066
5067 x_draw_glyphs (w, start_x, row, area, start, i,
5068 (row->inverse_p
5069 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5070 NULL, NULL, 1);
5071 }
5072 else
5073 {
5074 x += row->glyphs[area][i].pixel_width;
5075 ++i;
5076 }
5077 }
5078
5079 UNBLOCK_INPUT;
5080}
5081
5082
06a2c219
GM
5083/* Output LEN glyphs starting at START at the nominal cursor position.
5084 Advance the nominal cursor over the text. The global variable
5085 updated_window contains the window being updated, updated_row is
5086 the glyph row being updated, and updated_area is the area of that
5087 row being updated. */
dc6f92b8 5088
06a2c219
GM
5089static void
5090x_write_glyphs (start, len)
5091 struct glyph *start;
5092 int len;
5093{
5094 int x, hpos, real_start, real_end;
d9cdbb3d 5095
06a2c219 5096 xassert (updated_window && updated_row);
dc6f92b8 5097 BLOCK_INPUT;
06a2c219
GM
5098
5099 /* Write glyphs. */
dc6f92b8 5100
06a2c219
GM
5101 hpos = start - updated_row->glyphs[updated_area];
5102 x = x_draw_glyphs (updated_window, output_cursor.x,
5103 updated_row, updated_area,
5104 hpos, hpos + len,
5105 (updated_row->inverse_p
5106 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 5107 &real_start, &real_end, 0);
b30ec466 5108
06a2c219
GM
5109 /* If we drew over the cursor, note that it is not visible any more. */
5110 note_overwritten_text_cursor (updated_window, real_start,
5111 real_end - real_start);
dc6f92b8
JB
5112
5113 UNBLOCK_INPUT;
06a2c219
GM
5114
5115 /* Advance the output cursor. */
5116 output_cursor.hpos += len;
5117 output_cursor.x = x;
dc6f92b8
JB
5118}
5119
0cdd0c9f 5120
06a2c219 5121/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5122
06a2c219
GM
5123static void
5124x_insert_glyphs (start, len)
5125 struct glyph *start;
5126 register int len;
5127{
5128 struct frame *f;
5129 struct window *w;
5130 int line_height, shift_by_width, shifted_region_width;
5131 struct glyph_row *row;
5132 struct glyph *glyph;
5133 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 5134
06a2c219 5135 xassert (updated_window && updated_row);
0cdd0c9f 5136 BLOCK_INPUT;
06a2c219
GM
5137 w = updated_window;
5138 f = XFRAME (WINDOW_FRAME (w));
5139
5140 /* Get the height of the line we are in. */
5141 row = updated_row;
5142 line_height = row->height;
5143
5144 /* Get the width of the glyphs to insert. */
5145 shift_by_width = 0;
5146 for (glyph = start; glyph < start + len; ++glyph)
5147 shift_by_width += glyph->pixel_width;
5148
5149 /* Get the width of the region to shift right. */
5150 shifted_region_width = (window_box_width (w, updated_area)
5151 - output_cursor.x
5152 - shift_by_width);
5153
5154 /* Shift right. */
bf0ab8a2 5155 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5156 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5157 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5158 f->output_data.x->normal_gc,
5159 frame_x, frame_y,
5160 shifted_region_width, line_height,
5161 frame_x + shift_by_width, frame_y);
5162
5163 /* Write the glyphs. */
5164 hpos = start - row->glyphs[updated_area];
5165 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 5166 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
5167 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5168
5169 /* Advance the output cursor. */
5170 output_cursor.hpos += len;
5171 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5172 UNBLOCK_INPUT;
5173}
0cdd0c9f 5174
0cdd0c9f 5175
06a2c219
GM
5176/* Delete N glyphs at the nominal cursor position. Not implemented
5177 for X frames. */
c83febd7
RS
5178
5179static void
06a2c219
GM
5180x_delete_glyphs (n)
5181 register int n;
c83febd7 5182{
06a2c219 5183 abort ();
c83febd7
RS
5184}
5185
0cdd0c9f 5186
c5e6e06b
GM
5187/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5188 If they are <= 0, this is probably an error. */
5189
5190void
5191x_clear_area (dpy, window, x, y, width, height, exposures)
5192 Display *dpy;
5193 Window window;
5194 int x, y;
5195 int width, height;
5196 int exposures;
5197{
5198 xassert (width > 0 && height > 0);
5199 XClearArea (dpy, window, x, y, width, height, exposures);
5200}
5201
5202
06a2c219
GM
5203/* Erase the current text line from the nominal cursor position
5204 (inclusive) to pixel column TO_X (exclusive). The idea is that
5205 everything from TO_X onward is already erased.
5206
5207 TO_X is a pixel position relative to updated_area of
5208 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5209
06a2c219
GM
5210static void
5211x_clear_end_of_line (to_x)
5212 int to_x;
5213{
5214 struct frame *f;
5215 struct window *w = updated_window;
5216 int max_x, min_y, max_y;
5217 int from_x, from_y, to_y;
5218
5219 xassert (updated_window && updated_row);
5220 f = XFRAME (w->frame);
5221
5222 if (updated_row->full_width_p)
5223 {
5224 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5225 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5226 && !w->pseudo_window_p)
5227 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5228 }
06a2c219
GM
5229 else
5230 max_x = window_box_width (w, updated_area);
5231 max_y = window_text_bottom_y (w);
dc6f92b8 5232
06a2c219
GM
5233 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5234 of window. For TO_X > 0, truncate to end of drawing area. */
5235 if (to_x == 0)
5236 return;
5237 else if (to_x < 0)
5238 to_x = max_x;
5239 else
5240 to_x = min (to_x, max_x);
dbc4e1c1 5241
06a2c219
GM
5242 to_y = min (max_y, output_cursor.y + updated_row->height);
5243
5244 /* Notice if the cursor will be cleared by this operation. */
5245 if (!updated_row->full_width_p)
5246 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5247
06a2c219
GM
5248 from_x = output_cursor.x;
5249
5250 /* Translate to frame coordinates. */
5251 if (updated_row->full_width_p)
5252 {
5253 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5254 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5255 }
0cdd0c9f
RS
5256 else
5257 {
06a2c219
GM
5258 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5259 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5260 }
5261
045dee35 5262 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5263 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5264 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5265
5266 /* Prevent inadvertently clearing to end of the X window. */
5267 if (to_x > from_x && to_y > from_y)
5268 {
5269 BLOCK_INPUT;
c5e6e06b
GM
5270 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5271 from_x, from_y, to_x - from_x, to_y - from_y,
5272 False);
06a2c219 5273 UNBLOCK_INPUT;
0cdd0c9f 5274 }
0cdd0c9f 5275}
dbc4e1c1 5276
0cdd0c9f 5277
06a2c219 5278/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5279 frame. Otherwise clear the selected frame. */
06a2c219
GM
5280
5281static void
5282x_clear_frame ()
0cdd0c9f 5283{
06a2c219 5284 struct frame *f;
0cdd0c9f 5285
06a2c219
GM
5286 if (updating_frame)
5287 f = updating_frame;
0cdd0c9f 5288 else
b86bd3dd 5289 f = SELECTED_FRAME ();
58769bee 5290
06a2c219
GM
5291 /* Clearing the frame will erase any cursor, so mark them all as no
5292 longer visible. */
5293 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5294 output_cursor.hpos = output_cursor.vpos = 0;
5295 output_cursor.x = -1;
5296
5297 /* We don't set the output cursor here because there will always
5298 follow an explicit cursor_to. */
5299 BLOCK_INPUT;
5300 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5301
5302 /* We have to clear the scroll bars, too. If we have changed
5303 colors or something like that, then they should be notified. */
5304 x_scroll_bar_clear (f);
0cdd0c9f 5305
06a2c219
GM
5306 XFlush (FRAME_X_DISPLAY (f));
5307 UNBLOCK_INPUT;
dc6f92b8 5308}
06a2c219
GM
5309
5310
dc6f92b8 5311\f
dbc4e1c1
JB
5312/* Invert the middle quarter of the frame for .15 sec. */
5313
06a2c219
GM
5314/* We use the select system call to do the waiting, so we have to make
5315 sure it's available. If it isn't, we just won't do visual bells. */
5316
dbc4e1c1
JB
5317#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5318
06a2c219
GM
5319
5320/* Subtract the `struct timeval' values X and Y, storing the result in
5321 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5322
5323static int
5324timeval_subtract (result, x, y)
5325 struct timeval *result, x, y;
5326{
06a2c219
GM
5327 /* Perform the carry for the later subtraction by updating y. This
5328 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5329 if (x.tv_usec < y.tv_usec)
5330 {
5331 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5332 y.tv_usec -= 1000000 * nsec;
5333 y.tv_sec += nsec;
5334 }
06a2c219 5335
dbc4e1c1
JB
5336 if (x.tv_usec - y.tv_usec > 1000000)
5337 {
5338 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5339 y.tv_usec += 1000000 * nsec;
5340 y.tv_sec -= nsec;
5341 }
5342
06a2c219
GM
5343 /* Compute the time remaining to wait. tv_usec is certainly
5344 positive. */
dbc4e1c1
JB
5345 result->tv_sec = x.tv_sec - y.tv_sec;
5346 result->tv_usec = x.tv_usec - y.tv_usec;
5347
06a2c219
GM
5348 /* Return indication of whether the result should be considered
5349 negative. */
dbc4e1c1
JB
5350 return x.tv_sec < y.tv_sec;
5351}
dc6f92b8 5352
dfcf069d 5353void
f676886a
JB
5354XTflash (f)
5355 struct frame *f;
dc6f92b8 5356{
dbc4e1c1 5357 BLOCK_INPUT;
dc6f92b8 5358
dbc4e1c1
JB
5359 {
5360 GC gc;
dc6f92b8 5361
06a2c219
GM
5362 /* Create a GC that will use the GXxor function to flip foreground
5363 pixels into background pixels. */
dbc4e1c1
JB
5364 {
5365 XGCValues values;
dc6f92b8 5366
dbc4e1c1 5367 values.function = GXxor;
7556890b
RS
5368 values.foreground = (f->output_data.x->foreground_pixel
5369 ^ f->output_data.x->background_pixel);
58769bee 5370
334208b7 5371 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5372 GCFunction | GCForeground, &values);
5373 }
dc6f92b8 5374
dbc4e1c1 5375 {
e84e14c3
RS
5376 /* Get the height not including a menu bar widget. */
5377 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5378 /* Height of each line to flash. */
5379 int flash_height = FRAME_LINE_HEIGHT (f);
5380 /* These will be the left and right margins of the rectangles. */
5381 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5382 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5383
5384 int width;
5385
5386 /* Don't flash the area between a scroll bar and the frame
5387 edge it is next to. */
5388 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5389 {
5390 case vertical_scroll_bar_left:
5391 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5392 break;
5393
5394 case vertical_scroll_bar_right:
5395 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5396 break;
06a2c219
GM
5397
5398 default:
5399 break;
e84e14c3
RS
5400 }
5401
5402 width = flash_right - flash_left;
5403
5404 /* If window is tall, flash top and bottom line. */
5405 if (height > 3 * FRAME_LINE_HEIGHT (f))
5406 {
5407 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5408 flash_left,
5409 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5410 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5411 width, flash_height);
5412 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5413 flash_left,
5414 (height - flash_height
5415 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5416 width, flash_height);
5417 }
5418 else
5419 /* If it is short, flash it all. */
5420 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5421 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5422 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5423
06a2c219 5424 x_flush (f);
dc6f92b8 5425
dbc4e1c1 5426 {
06a2c219 5427 struct timeval wakeup;
dc6f92b8 5428
66c30ea1 5429 EMACS_GET_TIME (wakeup);
dc6f92b8 5430
dbc4e1c1
JB
5431 /* Compute time to wait until, propagating carry from usecs. */
5432 wakeup.tv_usec += 150000;
5433 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5434 wakeup.tv_usec %= 1000000;
5435
5436 /* Keep waiting until past the time wakeup. */
5437 while (1)
5438 {
5439 struct timeval timeout;
5440
66c30ea1 5441 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5442
5443 /* In effect, timeout = wakeup - timeout.
5444 Break if result would be negative. */
5445 if (timeval_subtract (&timeout, wakeup, timeout))
5446 break;
5447
5448 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5449 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5450 }
5451 }
58769bee 5452
e84e14c3
RS
5453 /* If window is tall, flash top and bottom line. */
5454 if (height > 3 * FRAME_LINE_HEIGHT (f))
5455 {
5456 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5457 flash_left,
5458 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5459 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5460 width, flash_height);
5461 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5462 flash_left,
5463 (height - flash_height
5464 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5465 width, flash_height);
5466 }
5467 else
5468 /* If it is short, flash it all. */
5469 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5470 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5471 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5472
334208b7 5473 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5474 x_flush (f);
dc6f92b8 5475 }
dbc4e1c1
JB
5476 }
5477
5478 UNBLOCK_INPUT;
dc6f92b8
JB
5479}
5480
06a2c219 5481#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5482
5483
dc6f92b8
JB
5484/* Make audible bell. */
5485
dfcf069d 5486void
dc6f92b8
JB
5487XTring_bell ()
5488{
b86bd3dd
GM
5489 struct frame *f = SELECTED_FRAME ();
5490
5491 if (FRAME_X_DISPLAY (f))
5492 {
dbc4e1c1 5493#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5494 if (visible_bell)
5495 XTflash (f);
5496 else
dbc4e1c1 5497#endif
b86bd3dd
GM
5498 {
5499 BLOCK_INPUT;
5500 XBell (FRAME_X_DISPLAY (f), 0);
5501 XFlush (FRAME_X_DISPLAY (f));
5502 UNBLOCK_INPUT;
5503 }
dc6f92b8
JB
5504 }
5505}
06a2c219 5506
dc6f92b8 5507\f
06a2c219
GM
5508/* Specify how many text lines, from the top of the window,
5509 should be affected by insert-lines and delete-lines operations.
5510 This, and those operations, are used only within an update
5511 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5512
dfcf069d 5513static void
06a2c219
GM
5514XTset_terminal_window (n)
5515 register int n;
dc6f92b8 5516{
06a2c219 5517 /* This function intentionally left blank. */
dc6f92b8
JB
5518}
5519
06a2c219
GM
5520
5521\f
5522/***********************************************************************
5523 Line Dance
5524 ***********************************************************************/
5525
5526/* Perform an insert-lines or delete-lines operation, inserting N
5527 lines or deleting -N lines at vertical position VPOS. */
5528
dfcf069d 5529static void
06a2c219
GM
5530x_ins_del_lines (vpos, n)
5531 int vpos, n;
dc6f92b8
JB
5532{
5533 abort ();
5534}
06a2c219
GM
5535
5536
5537/* Scroll part of the display as described by RUN. */
dc6f92b8 5538
dfcf069d 5539static void
06a2c219
GM
5540x_scroll_run (w, run)
5541 struct window *w;
5542 struct run *run;
dc6f92b8 5543{
06a2c219
GM
5544 struct frame *f = XFRAME (w->frame);
5545 int x, y, width, height, from_y, to_y, bottom_y;
5546
5547 /* Get frame-relative bounding box of the text display area of W,
5548 without mode lines. Include in this box the flags areas to the
5549 left and right of W. */
5550 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5551 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5552 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5553
5554 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5555 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5556 bottom_y = y + height;
dc6f92b8 5557
06a2c219
GM
5558 if (to_y < from_y)
5559 {
5560 /* Scrolling up. Make sure we don't copy part of the mode
5561 line at the bottom. */
5562 if (from_y + run->height > bottom_y)
5563 height = bottom_y - from_y;
5564 else
5565 height = run->height;
5566 }
dc6f92b8 5567 else
06a2c219
GM
5568 {
5569 /* Scolling down. Make sure we don't copy over the mode line.
5570 at the bottom. */
5571 if (to_y + run->height > bottom_y)
5572 height = bottom_y - to_y;
5573 else
5574 height = run->height;
5575 }
7a13e894 5576
06a2c219
GM
5577 BLOCK_INPUT;
5578
5579 /* Cursor off. Will be switched on again in x_update_window_end. */
5580 updated_window = w;
5581 x_clear_cursor (w);
5582
5583 XCopyArea (FRAME_X_DISPLAY (f),
5584 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5585 f->output_data.x->normal_gc,
5586 x, from_y,
5587 width, height,
5588 x, to_y);
5589
5590 UNBLOCK_INPUT;
5591}
dc6f92b8 5592
dc6f92b8 5593
06a2c219
GM
5594\f
5595/***********************************************************************
5596 Exposure Events
5597 ***********************************************************************/
5598
5599/* Redisplay an exposed area of frame F. X and Y are the upper-left
5600 corner of the exposed rectangle. W and H are width and height of
5601 the exposed area. All are pixel values. W or H zero means redraw
5602 the entire frame. */
dc6f92b8 5603
06a2c219
GM
5604static void
5605expose_frame (f, x, y, w, h)
5606 struct frame *f;
5607 int x, y, w, h;
dc6f92b8 5608{
06a2c219 5609 XRectangle r;
dc6f92b8 5610
06a2c219 5611 TRACE ((stderr, "expose_frame "));
dc6f92b8 5612
06a2c219
GM
5613 /* No need to redraw if frame will be redrawn soon. */
5614 if (FRAME_GARBAGED_P (f))
dc6f92b8 5615 {
06a2c219
GM
5616 TRACE ((stderr, " garbaged\n"));
5617 return;
5618 }
5619
5620 /* If basic faces haven't been realized yet, there is no point in
5621 trying to redraw anything. This can happen when we get an expose
5622 event while Emacs is starting, e.g. by moving another window. */
5623 if (FRAME_FACE_CACHE (f) == NULL
5624 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5625 {
5626 TRACE ((stderr, " no faces\n"));
5627 return;
58769bee 5628 }
06a2c219
GM
5629
5630 if (w == 0 || h == 0)
58769bee 5631 {
06a2c219
GM
5632 r.x = r.y = 0;
5633 r.width = CANON_X_UNIT (f) * f->width;
5634 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5635 }
5636 else
5637 {
06a2c219
GM
5638 r.x = x;
5639 r.y = y;
5640 r.width = w;
5641 r.height = h;
5642 }
5643
5644 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5645 expose_window_tree (XWINDOW (f->root_window), &r);
5646
9ea173e8 5647 if (WINDOWP (f->tool_bar_window))
06a2c219 5648 {
9ea173e8 5649 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5650 XRectangle window_rect;
5651 XRectangle intersection_rect;
5652 int window_x, window_y, window_width, window_height;
5653
5654
5655 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5656 window_rect.x = window_x;
5657 window_rect.y = window_y;
5658 window_rect.width = window_width;
5659 window_rect.height = window_height;
5660
5661 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5662 expose_window (w, &intersection_rect);
5663 }
5664
5665#ifndef USE_X_TOOLKIT
5666 if (WINDOWP (f->menu_bar_window))
5667 {
5668 struct window *w = XWINDOW (f->menu_bar_window);
5669 XRectangle window_rect;
5670 XRectangle intersection_rect;
5671 int window_x, window_y, window_width, window_height;
5672
5673
5674 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5675 window_rect.x = window_x;
5676 window_rect.y = window_y;
5677 window_rect.width = window_width;
5678 window_rect.height = window_height;
5679
5680 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5681 expose_window (w, &intersection_rect);
dc6f92b8 5682 }
06a2c219 5683#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5684}
5685
06a2c219
GM
5686
5687/* Redraw (parts) of all windows in the window tree rooted at W that
5688 intersect R. R contains frame pixel coordinates. */
5689
58769bee 5690static void
06a2c219
GM
5691expose_window_tree (w, r)
5692 struct window *w;
5693 XRectangle *r;
dc6f92b8 5694{
06a2c219
GM
5695 while (w)
5696 {
5697 if (!NILP (w->hchild))
5698 expose_window_tree (XWINDOW (w->hchild), r);
5699 else if (!NILP (w->vchild))
5700 expose_window_tree (XWINDOW (w->vchild), r);
5701 else
5702 {
5703 XRectangle window_rect;
5704 XRectangle intersection_rect;
5705 struct frame *f = XFRAME (w->frame);
5706 int window_x, window_y, window_width, window_height;
5707
5708 /* Frame-relative pixel rectangle of W. */
5709 window_box (w, -1, &window_x, &window_y, &window_width,
5710 &window_height);
5711 window_rect.x
5712 = (window_x
110859fc 5713 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5714 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5715 window_rect.y = window_y;
5716 window_rect.width
5717 = (window_width
110859fc 5718 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5719 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5720 window_rect.height
5721 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5722
5723 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5724 expose_window (w, &intersection_rect);
5725 }
58769bee 5726
06a2c219
GM
5727 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5728 }
5729}
58769bee 5730
dc6f92b8 5731
06a2c219
GM
5732/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5733 which intersects rectangle R. R is in window-relative coordinates. */
5734
5735static void
5736expose_area (w, row, r, area)
5737 struct window *w;
5738 struct glyph_row *row;
5739 XRectangle *r;
5740 enum glyph_row_area area;
5741{
06a2c219
GM
5742 struct glyph *first = row->glyphs[area];
5743 struct glyph *end = row->glyphs[area] + row->used[area];
5744 struct glyph *last;
4bc6dcc7 5745 int first_x, start_x, x;
06a2c219 5746
6fb13182
GM
5747 if (area == TEXT_AREA && row->fill_line_p)
5748 /* If row extends face to end of line write the whole line. */
4bc6dcc7 5749 x_draw_glyphs (w, 0, row, area,
6fb13182 5750 0, row->used[area],
06a2c219 5751 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5752 NULL, NULL, 0);
6fb13182
GM
5753 else
5754 {
4bc6dcc7
GM
5755 /* Set START_X to the window-relative start position for drawing glyphs of
5756 AREA. The first glyph of the text area can be partially visible.
5757 The first glyphs of other areas cannot. */
5758 if (area == LEFT_MARGIN_AREA)
5759 start_x = 0;
5760 else if (area == TEXT_AREA)
5761 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5762 else
5763 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5764 + window_box_width (w, TEXT_AREA));
5765 x = start_x;
5766
6fb13182
GM
5767 /* Find the first glyph that must be redrawn. */
5768 while (first < end
5769 && x + first->pixel_width < r->x)
5770 {
5771 x += first->pixel_width;
5772 ++first;
5773 }
5774
5775 /* Find the last one. */
5776 last = first;
5777 first_x = x;
5778 while (last < end
5779 && x < r->x + r->width)
5780 {
5781 x += last->pixel_width;
5782 ++last;
5783 }
5784
5785 /* Repaint. */
5786 if (last > first)
4bc6dcc7 5787 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5788 first - row->glyphs[area],
5789 last - row->glyphs[area],
5790 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5791 NULL, NULL, 0);
5792 }
06a2c219
GM
5793}
5794
58769bee 5795
06a2c219
GM
5796/* Redraw the parts of the glyph row ROW on window W intersecting
5797 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5798
06a2c219
GM
5799static void
5800expose_line (w, row, r)
5801 struct window *w;
5802 struct glyph_row *row;
5803 XRectangle *r;
5804{
5805 xassert (row->enabled_p);
5806
5807 if (row->mode_line_p || w->pseudo_window_p)
5808 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5809 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5810 NULL, NULL, 0);
06a2c219
GM
5811 else
5812 {
5813 if (row->used[LEFT_MARGIN_AREA])
5814 expose_area (w, row, r, LEFT_MARGIN_AREA);
5815 if (row->used[TEXT_AREA])
5816 expose_area (w, row, r, TEXT_AREA);
5817 if (row->used[RIGHT_MARGIN_AREA])
5818 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5819 x_draw_row_bitmaps (w, row);
5820 }
5821}
dc6f92b8 5822
58769bee 5823
06a2c219
GM
5824/* Return non-zero if W's cursor intersects rectangle R. */
5825
5826static int
5827x_phys_cursor_in_rect_p (w, r)
5828 struct window *w;
5829 XRectangle *r;
5830{
5831 XRectangle cr, result;
5832 struct glyph *cursor_glyph;
5833
5834 cursor_glyph = get_phys_cursor_glyph (w);
5835 if (cursor_glyph)
5836 {
5837 cr.x = w->phys_cursor.x;
5838 cr.y = w->phys_cursor.y;
5839 cr.width = cursor_glyph->pixel_width;
5840 cr.height = w->phys_cursor_height;
5841 return x_intersect_rectangles (&cr, r, &result);
5842 }
5843 else
5844 return 0;
dc6f92b8 5845}
dc6f92b8 5846
06a2c219
GM
5847
5848/* Redraw a rectangle of window W. R is a rectangle in window
5849 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5850
5851static void
06a2c219
GM
5852expose_window (w, r)
5853 struct window *w;
5854 XRectangle *r;
dc6f92b8 5855{
06a2c219
GM
5856 struct glyph_row *row;
5857 int y;
5858 int yb = window_text_bottom_y (w);
5859 int cursor_cleared_p;
dc6f92b8 5860
80c32bcc
GM
5861 /* If window is not yet fully initialized, do nothing. This can
5862 happen when toolkit scroll bars are used and a window is split.
5863 Reconfiguring the scroll bar will generate an expose for a newly
5864 created window. */
c7911417 5865 if (w->current_matrix == NULL || w == updated_window)
80c32bcc
GM
5866 return;
5867
06a2c219
GM
5868 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5869 r->x, r->y, r->width, r->height));
dc6f92b8 5870
06a2c219
GM
5871 /* Convert to window coordinates. */
5872 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5873 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5874
06a2c219
GM
5875 /* Turn off the cursor. */
5876 if (!w->pseudo_window_p
5877 && x_phys_cursor_in_rect_p (w, r))
5878 {
5879 x_clear_cursor (w);
5880 cursor_cleared_p = 1;
5881 }
5882 else
5883 cursor_cleared_p = 0;
5884
5885 /* Find the first row intersecting the rectangle R. */
5886 row = w->current_matrix->rows;
5887 y = 0;
5888 while (row->enabled_p
5889 && y < yb
5890 && y + row->height < r->y)
5891 {
5892 y += row->height;
5893 ++row;
5894 }
5895
dc6f92b8 5896 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5897 while (row->enabled_p
5898 && y < yb
5899 && y < r->y + r->height)
5900 {
5901 expose_line (w, row, r);
5902 y += row->height;
5903 ++row;
5904 }
5905
5906 /* Display the mode line if there is one. */
5907 if (WINDOW_WANTS_MODELINE_P (w)
5908 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5909 row->enabled_p)
5910 && row->y < r->y + r->height)
5911 expose_line (w, row, r);
dc6f92b8 5912
06a2c219 5913 if (!w->pseudo_window_p)
dc6f92b8 5914 {
06a2c219
GM
5915 /* Draw border between windows. */
5916 x_draw_vertical_border (w);
5917
5918 /* Turn the cursor on again. */
5919 if (cursor_cleared_p)
5920 x_update_window_cursor (w, 1);
5921 }
5922}
dc6f92b8 5923
dc6f92b8 5924
06a2c219
GM
5925/* Determine the intersection of two rectangles R1 and R2. Return
5926 the intersection in *RESULT. Value is non-zero if RESULT is not
5927 empty. */
5928
5929static int
5930x_intersect_rectangles (r1, r2, result)
5931 XRectangle *r1, *r2, *result;
5932{
5933 XRectangle *left, *right;
5934 XRectangle *upper, *lower;
5935 int intersection_p = 0;
5936
5937 /* Rearrange so that R1 is the left-most rectangle. */
5938 if (r1->x < r2->x)
5939 left = r1, right = r2;
5940 else
5941 left = r2, right = r1;
5942
5943 /* X0 of the intersection is right.x0, if this is inside R1,
5944 otherwise there is no intersection. */
5945 if (right->x <= left->x + left->width)
5946 {
5947 result->x = right->x;
5948
5949 /* The right end of the intersection is the minimum of the
5950 the right ends of left and right. */
5951 result->width = (min (left->x + left->width, right->x + right->width)
5952 - result->x);
5953
5954 /* Same game for Y. */
5955 if (r1->y < r2->y)
5956 upper = r1, lower = r2;
5957 else
5958 upper = r2, lower = r1;
5959
5960 /* The upper end of the intersection is lower.y0, if this is inside
5961 of upper. Otherwise, there is no intersection. */
5962 if (lower->y <= upper->y + upper->height)
dc43ef94 5963 {
06a2c219
GM
5964 result->y = lower->y;
5965
5966 /* The lower end of the intersection is the minimum of the lower
5967 ends of upper and lower. */
5968 result->height = (min (lower->y + lower->height,
5969 upper->y + upper->height)
5970 - result->y);
5971 intersection_p = 1;
dc43ef94 5972 }
dc6f92b8
JB
5973 }
5974
06a2c219 5975 return intersection_p;
dc6f92b8 5976}
06a2c219
GM
5977
5978
5979
5980
dc6f92b8 5981\f
dc6f92b8 5982static void
334208b7
RS
5983frame_highlight (f)
5984 struct frame *f;
dc6f92b8 5985{
b3e1e05c
JB
5986 /* We used to only do this if Vx_no_window_manager was non-nil, but
5987 the ICCCM (section 4.1.6) says that the window's border pixmap
5988 and border pixel are window attributes which are "private to the
5989 client", so we can always change it to whatever we want. */
5990 BLOCK_INPUT;
334208b7 5991 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5992 f->output_data.x->border_pixel);
b3e1e05c 5993 UNBLOCK_INPUT;
5d46f928 5994 x_update_cursor (f, 1);
dc6f92b8
JB
5995}
5996
5997static void
334208b7
RS
5998frame_unhighlight (f)
5999 struct frame *f;
dc6f92b8 6000{
b3e1e05c
JB
6001 /* We used to only do this if Vx_no_window_manager was non-nil, but
6002 the ICCCM (section 4.1.6) says that the window's border pixmap
6003 and border pixel are window attributes which are "private to the
6004 client", so we can always change it to whatever we want. */
6005 BLOCK_INPUT;
334208b7 6006 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6007 f->output_data.x->border_tile);
b3e1e05c 6008 UNBLOCK_INPUT;
5d46f928 6009 x_update_cursor (f, 1);
dc6f92b8 6010}
dc6f92b8 6011
f676886a
JB
6012/* The focus has changed. Update the frames as necessary to reflect
6013 the new situation. Note that we can't change the selected frame
c5acd733 6014 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6015 Each event gets marked with the frame in which it occurred, so the
c5acd733 6016 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6017
6d4238f3 6018static void
0f941935
KH
6019x_new_focus_frame (dpyinfo, frame)
6020 struct x_display_info *dpyinfo;
f676886a 6021 struct frame *frame;
dc6f92b8 6022{
0f941935 6023 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6024
0f941935 6025 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6026 {
58769bee 6027 /* Set this before calling other routines, so that they see
f676886a 6028 the correct value of x_focus_frame. */
0f941935 6029 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6030
6031 if (old_focus && old_focus->auto_lower)
f676886a 6032 x_lower_frame (old_focus);
dc6f92b8
JB
6033
6034#if 0
f676886a 6035 selected_frame = frame;
e0c1aef2
KH
6036 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6037 selected_frame);
f676886a
JB
6038 Fselect_window (selected_frame->selected_window);
6039 choose_minibuf_frame ();
c118dd06 6040#endif /* ! 0 */
dc6f92b8 6041
0f941935
KH
6042 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6043 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6044 else
6045 pending_autoraise_frame = 0;
6d4238f3 6046 }
dc6f92b8 6047
0f941935 6048 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6049}
6050
37c2c98b
RS
6051/* Handle an event saying the mouse has moved out of an Emacs frame. */
6052
6053void
0f941935
KH
6054x_mouse_leave (dpyinfo)
6055 struct x_display_info *dpyinfo;
37c2c98b 6056{
0f941935 6057 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6058}
6d4238f3 6059
f451eb13
JB
6060/* The focus has changed, or we have redirected a frame's focus to
6061 another frame (this happens when a frame uses a surrogate
06a2c219 6062 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6063
6064 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6065 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6066 the appropriate X display info. */
06a2c219 6067
6d4238f3 6068static void
0f941935
KH
6069XTframe_rehighlight (frame)
6070 struct frame *frame;
6d4238f3 6071{
0f941935
KH
6072 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6073}
6d4238f3 6074
0f941935
KH
6075static void
6076x_frame_rehighlight (dpyinfo)
6077 struct x_display_info *dpyinfo;
6078{
6079 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6080
6081 if (dpyinfo->x_focus_frame)
6d4238f3 6082 {
0f941935
KH
6083 dpyinfo->x_highlight_frame
6084 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6085 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6086 : dpyinfo->x_focus_frame);
6087 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6088 {
0f941935
KH
6089 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6090 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6091 }
dc6f92b8 6092 }
6d4238f3 6093 else
0f941935 6094 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6095
0f941935 6096 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6097 {
6098 if (old_highlight)
f676886a 6099 frame_unhighlight (old_highlight);
0f941935
KH
6100 if (dpyinfo->x_highlight_frame)
6101 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6102 }
dc6f92b8 6103}
06a2c219
GM
6104
6105
dc6f92b8 6106\f
06a2c219 6107/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6108
28430d3c
JB
6109/* Initialize mode_switch_bit and modifier_meaning. */
6110static void
334208b7
RS
6111x_find_modifier_meanings (dpyinfo)
6112 struct x_display_info *dpyinfo;
28430d3c 6113{
f689eb05 6114 int min_code, max_code;
28430d3c
JB
6115 KeySym *syms;
6116 int syms_per_code;
6117 XModifierKeymap *mods;
6118
334208b7
RS
6119 dpyinfo->meta_mod_mask = 0;
6120 dpyinfo->shift_lock_mask = 0;
6121 dpyinfo->alt_mod_mask = 0;
6122 dpyinfo->super_mod_mask = 0;
6123 dpyinfo->hyper_mod_mask = 0;
58769bee 6124
9658a521 6125#ifdef HAVE_X11R4
334208b7 6126 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6127#else
4a60f8c5
RS
6128 min_code = dpyinfo->display->min_keycode;
6129 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6130#endif
6131
334208b7 6132 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6133 min_code, max_code - min_code + 1,
6134 &syms_per_code);
334208b7 6135 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6136
58769bee 6137 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6138 Alt keysyms are on. */
28430d3c 6139 {
06a2c219 6140 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6141
6142 for (row = 3; row < 8; row++)
6143 for (col = 0; col < mods->max_keypermod; col++)
6144 {
0299d313
RS
6145 KeyCode code
6146 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6147
af92970c
KH
6148 /* Zeroes are used for filler. Skip them. */
6149 if (code == 0)
6150 continue;
6151
28430d3c
JB
6152 /* Are any of this keycode's keysyms a meta key? */
6153 {
6154 int code_col;
6155
6156 for (code_col = 0; code_col < syms_per_code; code_col++)
6157 {
f689eb05 6158 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6159
f689eb05 6160 switch (sym)
28430d3c 6161 {
f689eb05
JB
6162 case XK_Meta_L:
6163 case XK_Meta_R:
334208b7 6164 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6165 break;
f689eb05
JB
6166
6167 case XK_Alt_L:
6168 case XK_Alt_R:
334208b7 6169 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6170 break;
6171
6172 case XK_Hyper_L:
6173 case XK_Hyper_R:
334208b7 6174 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6175 break;
6176
6177 case XK_Super_L:
6178 case XK_Super_R:
334208b7 6179 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6180 break;
11edeb03
JB
6181
6182 case XK_Shift_Lock:
6183 /* Ignore this if it's not on the lock modifier. */
6184 if ((1 << row) == LockMask)
334208b7 6185 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6186 break;
28430d3c
JB
6187 }
6188 }
6189 }
6190 }
6191 }
6192
f689eb05 6193 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6194 if (! dpyinfo->meta_mod_mask)
a3c44b14 6195 {
334208b7
RS
6196 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6197 dpyinfo->alt_mod_mask = 0;
a3c44b14 6198 }
f689eb05 6199
148c4b70
RS
6200 /* If some keys are both alt and meta,
6201 make them just meta, not alt. */
334208b7 6202 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6203 {
334208b7 6204 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6205 }
58769bee 6206
28430d3c 6207 XFree ((char *) syms);
f689eb05 6208 XFreeModifiermap (mods);
28430d3c
JB
6209}
6210
dfeccd2d
JB
6211/* Convert between the modifier bits X uses and the modifier bits
6212 Emacs uses. */
06a2c219 6213
7c5283e4 6214static unsigned int
334208b7
RS
6215x_x_to_emacs_modifiers (dpyinfo, state)
6216 struct x_display_info *dpyinfo;
dc6f92b8
JB
6217 unsigned int state;
6218{
334208b7
RS
6219 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6220 | ((state & ControlMask) ? ctrl_modifier : 0)
6221 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6222 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6223 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6224 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6225}
6226
dfeccd2d 6227static unsigned int
334208b7
RS
6228x_emacs_to_x_modifiers (dpyinfo, state)
6229 struct x_display_info *dpyinfo;
dfeccd2d
JB
6230 unsigned int state;
6231{
334208b7
RS
6232 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6233 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6234 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6235 | ((state & shift_modifier) ? ShiftMask : 0)
6236 | ((state & ctrl_modifier) ? ControlMask : 0)
6237 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6238}
d047c4eb
KH
6239
6240/* Convert a keysym to its name. */
6241
6242char *
6243x_get_keysym_name (keysym)
6244 KeySym keysym;
6245{
6246 char *value;
6247
6248 BLOCK_INPUT;
6249 value = XKeysymToString (keysym);
6250 UNBLOCK_INPUT;
6251
6252 return value;
6253}
06a2c219
GM
6254
6255
e4571a43
JB
6256\f
6257/* Mouse clicks and mouse movement. Rah. */
e4571a43 6258
06a2c219
GM
6259/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6260 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6261 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6262 not force the value into range. */
69388238 6263
c8dba240 6264void
69388238 6265pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6266 FRAME_PTR f;
69388238 6267 register int pix_x, pix_y;
e4571a43
JB
6268 register int *x, *y;
6269 XRectangle *bounds;
69388238 6270 int noclip;
e4571a43 6271{
06a2c219 6272 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6273 even for negative values. */
6274 if (pix_x < 0)
7556890b 6275 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6276 if (pix_y < 0)
7556890b 6277 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6278
e4571a43
JB
6279 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6280 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6281
6282 if (bounds)
6283 {
7556890b
RS
6284 bounds->width = FONT_WIDTH (f->output_data.x->font);
6285 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6286 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6287 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6288 }
6289
69388238
RS
6290 if (!noclip)
6291 {
6292 if (pix_x < 0)
6293 pix_x = 0;
3cbd2e0b
RS
6294 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6295 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6296
6297 if (pix_y < 0)
6298 pix_y = 0;
6299 else if (pix_y > f->height)
6300 pix_y = f->height;
6301 }
e4571a43
JB
6302
6303 *x = pix_x;
6304 *y = pix_y;
6305}
6306
06a2c219
GM
6307
6308/* Given HPOS/VPOS in the current matrix of W, return corresponding
6309 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6310 can't tell the positions because W's display is not up to date,
6311 return 0. */
6312
6313int
6314glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6315 struct window *w;
6316 int hpos, vpos;
6317 int *frame_x, *frame_y;
2b5c9e71 6318{
06a2c219
GM
6319 int success_p;
6320
6321 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6322 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6323
6324 if (display_completed)
6325 {
6326 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6327 struct glyph *glyph = row->glyphs[TEXT_AREA];
6328 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6329
6330 *frame_y = row->y;
6331 *frame_x = row->x;
6332 while (glyph < end)
6333 {
6334 *frame_x += glyph->pixel_width;
6335 ++glyph;
6336 }
6337
6338 success_p = 1;
6339 }
6340 else
6341 {
6342 *frame_y = *frame_x = 0;
6343 success_p = 0;
6344 }
6345
6346 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6347 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6348 return success_p;
2b5c9e71
RS
6349}
6350
06a2c219 6351
dc6f92b8
JB
6352/* Prepare a mouse-event in *RESULT for placement in the input queue.
6353
6354 If the event is a button press, then note that we have grabbed
f451eb13 6355 the mouse. */
dc6f92b8
JB
6356
6357static Lisp_Object
f451eb13 6358construct_mouse_click (result, event, f)
dc6f92b8
JB
6359 struct input_event *result;
6360 XButtonEvent *event;
f676886a 6361 struct frame *f;
dc6f92b8 6362{
f451eb13 6363 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6364 otherwise. */
f451eb13 6365 result->kind = mouse_click;
69388238 6366 result->code = event->button - Button1;
1113d9db 6367 result->timestamp = event->time;
334208b7
RS
6368 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6369 event->state)
f689eb05 6370 | (event->type == ButtonRelease
58769bee 6371 ? up_modifier
f689eb05 6372 : down_modifier));
dc6f92b8 6373
06a2c219
GM
6374 XSETINT (result->x, event->x);
6375 XSETINT (result->y, event->y);
6376 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6377 result->arg = Qnil;
06a2c219 6378 return Qnil;
dc6f92b8 6379}
b849c413 6380
69388238 6381\f
90e65f07
JB
6382/* Function to report a mouse movement to the mainstream Emacs code.
6383 The input handler calls this.
6384
6385 We have received a mouse movement event, which is given in *event.
6386 If the mouse is over a different glyph than it was last time, tell
6387 the mainstream emacs code by setting mouse_moved. If not, ask for
6388 another motion event, so we can check again the next time it moves. */
b8009dd1 6389
06a2c219
GM
6390static XMotionEvent last_mouse_motion_event;
6391static Lisp_Object last_mouse_motion_frame;
6392
90e65f07 6393static void
12ba150f 6394note_mouse_movement (frame, event)
f676886a 6395 FRAME_PTR frame;
90e65f07 6396 XMotionEvent *event;
90e65f07 6397{
e5d77022 6398 last_mouse_movement_time = event->time;
06a2c219
GM
6399 last_mouse_motion_event = *event;
6400 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6401
27f338af
RS
6402 if (event->window != FRAME_X_WINDOW (frame))
6403 {
39d8bb4d 6404 frame->mouse_moved = 1;
27f338af 6405 last_mouse_scroll_bar = Qnil;
27f338af 6406 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6407 }
6408
90e65f07 6409 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6410 else if (event->x < last_mouse_glyph.x
6411 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6412 || event->y < last_mouse_glyph.y
6413 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6414 {
39d8bb4d 6415 frame->mouse_moved = 1;
ab648270 6416 last_mouse_scroll_bar = Qnil;
b8009dd1 6417 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6418 }
6419}
6420
bf1c0ba1 6421/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6422
06a2c219
GM
6423 int disable_mouse_highlight;
6424
6425
6426\f
6427/************************************************************************
6428 Mouse Face
6429 ************************************************************************/
6430
6431/* Find the glyph under window-relative coordinates X/Y in window W.
6432 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6433 strings. Return in *HPOS and *VPOS the row and column number of
6434 the glyph found. Return in *AREA the glyph area containing X.
6435 Value is a pointer to the glyph found or null if X/Y is not on
6436 text, or we can't tell because W's current matrix is not up to
6437 date. */
6438
6439static struct glyph *
6440x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6441 struct window *w;
6442 int x, y;
6443 int *hpos, *vpos, *area;
6444{
6445 struct glyph *glyph, *end;
3e71d8f2 6446 struct glyph_row *row = NULL;
06a2c219
GM
6447 int x0, i, left_area_width;
6448
6449 /* Find row containing Y. Give up if some row is not enabled. */
6450 for (i = 0; i < w->current_matrix->nrows; ++i)
6451 {
6452 row = MATRIX_ROW (w->current_matrix, i);
6453 if (!row->enabled_p)
6454 return NULL;
6455 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6456 break;
6457 }
6458
6459 *vpos = i;
6460 *hpos = 0;
6461
6462 /* Give up if Y is not in the window. */
6463 if (i == w->current_matrix->nrows)
6464 return NULL;
6465
6466 /* Get the glyph area containing X. */
6467 if (w->pseudo_window_p)
6468 {
6469 *area = TEXT_AREA;
6470 x0 = 0;
6471 }
6472 else
6473 {
6474 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6475 if (x < left_area_width)
6476 {
6477 *area = LEFT_MARGIN_AREA;
6478 x0 = 0;
6479 }
6480 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6481 {
6482 *area = TEXT_AREA;
6483 x0 = row->x + left_area_width;
6484 }
6485 else
6486 {
6487 *area = RIGHT_MARGIN_AREA;
6488 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6489 }
6490 }
6491
6492 /* Find glyph containing X. */
6493 glyph = row->glyphs[*area];
6494 end = glyph + row->used[*area];
6495 while (glyph < end)
6496 {
6497 if (x < x0 + glyph->pixel_width)
6498 {
6499 if (w->pseudo_window_p)
6500 break;
6501 else if (BUFFERP (glyph->object))
6502 break;
6503 }
6504
6505 x0 += glyph->pixel_width;
6506 ++glyph;
6507 }
6508
6509 if (glyph == end)
6510 return NULL;
6511
6512 *hpos = glyph - row->glyphs[*area];
6513 return glyph;
6514}
6515
6516
6517/* Convert frame-relative x/y to coordinates relative to window W.
6518 Takes pseudo-windows into account. */
6519
6520static void
6521frame_to_window_pixel_xy (w, x, y)
6522 struct window *w;
6523 int *x, *y;
6524{
6525 if (w->pseudo_window_p)
6526 {
6527 /* A pseudo-window is always full-width, and starts at the
6528 left edge of the frame, plus a frame border. */
6529 struct frame *f = XFRAME (w->frame);
6530 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6531 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6532 }
6533 else
6534 {
6535 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6536 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6537 }
6538}
6539
6540
e371a781 6541/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6542 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6543 mode line. X is relative to the start of the text display area of
6544 W, so the width of bitmap areas and scroll bars must be subtracted
6545 to get a position relative to the start of the mode line. */
6546
6547static void
6548note_mode_line_highlight (w, x, mode_line_p)
6549 struct window *w;
6550 int x, mode_line_p;
6551{
6552 struct frame *f = XFRAME (w->frame);
6553 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6554 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6555 struct glyph_row *row;
6556
6557 if (mode_line_p)
6558 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6559 else
045dee35 6560 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6561
06a2c219
GM
6562 if (row->enabled_p)
6563 {
6564 struct glyph *glyph, *end;
6565 Lisp_Object help, map;
6566 int x0;
6567
6568 /* Find the glyph under X. */
6569 glyph = row->glyphs[TEXT_AREA];
6570 end = glyph + row->used[TEXT_AREA];
6571 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6572 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
e371a781 6573
06a2c219
GM
6574 while (glyph < end
6575 && x >= x0 + glyph->pixel_width)
6576 {
6577 x0 += glyph->pixel_width;
6578 ++glyph;
6579 }
6580
6581 if (glyph < end
6582 && STRINGP (glyph->object)
6583 && XSTRING (glyph->object)->intervals
6584 && glyph->charpos >= 0
6585 && glyph->charpos < XSTRING (glyph->object)->size)
6586 {
6587 /* If we're on a string with `help-echo' text property,
6588 arrange for the help to be displayed. This is done by
6589 setting the global variable help_echo to the help string. */
6590 help = Fget_text_property (make_number (glyph->charpos),
6591 Qhelp_echo, glyph->object);
b7e80413 6592 if (!NILP (help))
be010514
GM
6593 {
6594 help_echo = help;
7cea38bc 6595 XSETWINDOW (help_echo_window, w);
be010514
GM
6596 help_echo_object = glyph->object;
6597 help_echo_pos = glyph->charpos;
6598 }
06a2c219
GM
6599
6600 /* Change the mouse pointer according to what is under X/Y. */
6601 map = Fget_text_property (make_number (glyph->charpos),
6602 Qlocal_map, glyph->object);
02067692 6603 if (KEYMAPP (map))
06a2c219 6604 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6605 else
6606 {
6607 map = Fget_text_property (make_number (glyph->charpos),
6608 Qkeymap, glyph->object);
02067692 6609 if (KEYMAPP (map))
be010514
GM
6610 cursor = f->output_data.x->nontext_cursor;
6611 }
06a2c219
GM
6612 }
6613 }
6614
6615 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6616}
6617
6618
6619/* Take proper action when the mouse has moved to position X, Y on
6620 frame F as regards highlighting characters that have mouse-face
6621 properties. Also de-highlighting chars where the mouse was before.
27f338af 6622 X and Y can be negative or out of range. */
b8009dd1
RS
6623
6624static void
6625note_mouse_highlight (f, x, y)
06a2c219 6626 struct frame *f;
c32cdd9a 6627 int x, y;
b8009dd1 6628{
06a2c219
GM
6629 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6630 int portion;
b8009dd1
RS
6631 Lisp_Object window;
6632 struct window *w;
6633
06a2c219
GM
6634 /* When a menu is active, don't highlight because this looks odd. */
6635#ifdef USE_X_TOOLKIT
6636 if (popup_activated ())
6637 return;
6638#endif
6639
04fff9c0
GM
6640 if (disable_mouse_highlight
6641 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6642 return;
6643
06a2c219
GM
6644 dpyinfo->mouse_face_mouse_x = x;
6645 dpyinfo->mouse_face_mouse_y = y;
6646 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6647
06a2c219 6648 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6649 return;
6650
514e4681
RS
6651 if (gc_in_progress)
6652 {
06a2c219 6653 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6654 return;
6655 }
6656
b8009dd1 6657 /* Which window is that in? */
06a2c219 6658 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6659
6660 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6661 if (! EQ (window, dpyinfo->mouse_face_window))
6662 clear_mouse_face (dpyinfo);
6663
6664 /* Not on a window -> return. */
6665 if (!WINDOWP (window))
6666 return;
6667
6668 /* Convert to window-relative pixel coordinates. */
6669 w = XWINDOW (window);
6670 frame_to_window_pixel_xy (w, &x, &y);
6671
9ea173e8 6672 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6673 buffer. */
9ea173e8 6674 if (EQ (window, f->tool_bar_window))
06a2c219 6675 {
9ea173e8 6676 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6677 return;
6678 }
6679
6680 if (portion == 1 || portion == 3)
6681 {
6682 /* Mouse is on the mode or top line. */
6683 note_mode_line_highlight (w, x, portion == 1);
6684 return;
6685 }
e371a781
GM
6686 else if (portion == 2)
6687 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6688 f->output_data.x->horizontal_drag_cursor);
06a2c219
GM
6689 else
6690 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6691 f->output_data.x->text_cursor);
b8009dd1 6692
0cdd0c9f
RS
6693 /* Are we in a window whose display is up to date?
6694 And verify the buffer's text has not changed. */
06a2c219
GM
6695 if (/* Within text portion of the window. */
6696 portion == 0
0cdd0c9f 6697 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6698 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6699 && (XFASTINT (w->last_overlay_modified)
6700 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6701 {
06a2c219
GM
6702 int hpos, vpos, pos, i, area;
6703 struct glyph *glyph;
b8009dd1 6704
06a2c219
GM
6705 /* Find the glyph under X/Y. */
6706 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6707
6708 /* Clear mouse face if X/Y not over text. */
6709 if (glyph == NULL
6710 || area != TEXT_AREA
6711 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6712 {
06a2c219
GM
6713 clear_mouse_face (dpyinfo);
6714 return;
6715 }
6716
6717 pos = glyph->charpos;
6718 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6719
6720 /* Check for mouse-face and help-echo. */
6721 {
6722 Lisp_Object mouse_face, overlay, position;
6723 Lisp_Object *overlay_vec;
6724 int len, noverlays;
6725 struct buffer *obuf;
6726 int obegv, ozv;
6727
6728 /* If we get an out-of-range value, return now; avoid an error. */
6729 if (pos > BUF_Z (XBUFFER (w->buffer)))
6730 return;
6731
6732 /* Make the window's buffer temporarily current for
6733 overlays_at and compute_char_face. */
6734 obuf = current_buffer;
6735 current_buffer = XBUFFER (w->buffer);
6736 obegv = BEGV;
6737 ozv = ZV;
6738 BEGV = BEG;
6739 ZV = Z;
6740
6741 /* Is this char mouse-active or does it have help-echo? */
6742 XSETINT (position, pos);
6743
6744 /* Put all the overlays we want in a vector in overlay_vec.
6745 Store the length in len. If there are more than 10, make
6746 enough space for all, and try again. */
6747 len = 10;
6748 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6749 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6750 if (noverlays > len)
6751 {
6752 len = noverlays;
6753 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6754 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6755 }
f8349001
GM
6756
6757 /* Sort overlays into increasing priority order. */
06a2c219
GM
6758 noverlays = sort_overlays (overlay_vec, noverlays, w);
6759
6760 /* Check mouse-face highlighting. */
6761 if (! (EQ (window, dpyinfo->mouse_face_window)
6762 && vpos >= dpyinfo->mouse_face_beg_row
6763 && vpos <= dpyinfo->mouse_face_end_row
6764 && (vpos > dpyinfo->mouse_face_beg_row
6765 || hpos >= dpyinfo->mouse_face_beg_col)
6766 && (vpos < dpyinfo->mouse_face_end_row
6767 || hpos < dpyinfo->mouse_face_end_col
6768 || dpyinfo->mouse_face_past_end)))
6769 {
6770 /* Clear the display of the old active region, if any. */
6771 clear_mouse_face (dpyinfo);
6772
6773 /* Find the highest priority overlay that has a mouse-face prop. */
6774 overlay = Qnil;
f8349001 6775 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6776 {
6777 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6778 if (!NILP (mouse_face))
6779 {
6780 overlay = overlay_vec[i];
6781 break;
6782 }
6783 }
6784
6785 /* If no overlay applies, get a text property. */
6786 if (NILP (overlay))
6787 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6788
6789 /* Handle the overlay case. */
6790 if (! NILP (overlay))
6791 {
6792 /* Find the range of text around this char that
6793 should be active. */
6794 Lisp_Object before, after;
6795 int ignore;
6796
6797 before = Foverlay_start (overlay);
6798 after = Foverlay_end (overlay);
6799 /* Record this as the current active region. */
6800 fast_find_position (w, XFASTINT (before),
6801 &dpyinfo->mouse_face_beg_col,
6802 &dpyinfo->mouse_face_beg_row,
6803 &dpyinfo->mouse_face_beg_x,
6804 &dpyinfo->mouse_face_beg_y);
6805 dpyinfo->mouse_face_past_end
6806 = !fast_find_position (w, XFASTINT (after),
6807 &dpyinfo->mouse_face_end_col,
6808 &dpyinfo->mouse_face_end_row,
6809 &dpyinfo->mouse_face_end_x,
6810 &dpyinfo->mouse_face_end_y);
6811 dpyinfo->mouse_face_window = window;
6812 dpyinfo->mouse_face_face_id
6813 = face_at_buffer_position (w, pos, 0, 0,
6814 &ignore, pos + 1, 1);
6815
6816 /* Display it as active. */
6817 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6818 }
6819 /* Handle the text property case. */
6820 else if (! NILP (mouse_face))
6821 {
6822 /* Find the range of text around this char that
6823 should be active. */
6824 Lisp_Object before, after, beginning, end;
6825 int ignore;
6826
6827 beginning = Fmarker_position (w->start);
6828 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6829 - XFASTINT (w->window_end_pos)));
6830 before
6831 = Fprevious_single_property_change (make_number (pos + 1),
6832 Qmouse_face,
6833 w->buffer, beginning);
6834 after
6835 = Fnext_single_property_change (position, Qmouse_face,
6836 w->buffer, end);
6837 /* Record this as the current active region. */
6838 fast_find_position (w, XFASTINT (before),
6839 &dpyinfo->mouse_face_beg_col,
6840 &dpyinfo->mouse_face_beg_row,
6841 &dpyinfo->mouse_face_beg_x,
6842 &dpyinfo->mouse_face_beg_y);
6843 dpyinfo->mouse_face_past_end
6844 = !fast_find_position (w, XFASTINT (after),
6845 &dpyinfo->mouse_face_end_col,
6846 &dpyinfo->mouse_face_end_row,
6847 &dpyinfo->mouse_face_end_x,
6848 &dpyinfo->mouse_face_end_y);
6849 dpyinfo->mouse_face_window = window;
6850 dpyinfo->mouse_face_face_id
6851 = face_at_buffer_position (w, pos, 0, 0,
6852 &ignore, pos + 1, 1);
6853
6854 /* Display it as active. */
6855 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6856 }
6857 }
6858
6859 /* Look for a `help-echo' property. */
6860 {
743934db 6861 Lisp_Object help, overlay;
06a2c219
GM
6862
6863 /* Check overlays first. */
f9b5db02 6864 help = overlay = Qnil;
f8349001 6865 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6866 {
6867 overlay = overlay_vec[i];
6868 help = Foverlay_get (overlay, Qhelp_echo);
6869 }
be010514
GM
6870
6871 if (!NILP (help))
6872 {
6873 help_echo = help;
7cea38bc 6874 help_echo_window = window;
743934db 6875 help_echo_object = overlay;
be010514
GM
6876 help_echo_pos = pos;
6877 }
6878 else
6879 {
6880 /* Try text properties. */
6881 if ((STRINGP (glyph->object)
06a2c219
GM
6882 && glyph->charpos >= 0
6883 && glyph->charpos < XSTRING (glyph->object)->size)
6884 || (BUFFERP (glyph->object)
6885 && glyph->charpos >= BEGV
be010514
GM
6886 && glyph->charpos < ZV))
6887 help = Fget_text_property (make_number (glyph->charpos),
6888 Qhelp_echo, glyph->object);
06a2c219 6889
be010514
GM
6890 if (!NILP (help))
6891 {
6892 help_echo = help;
7cea38bc 6893 help_echo_window = window;
be010514
GM
6894 help_echo_object = glyph->object;
6895 help_echo_pos = glyph->charpos;
6896 }
6897 }
06a2c219
GM
6898 }
6899
6900 BEGV = obegv;
6901 ZV = ozv;
6902 current_buffer = obuf;
6903 }
6904 }
6905}
6906
6907static void
6908redo_mouse_highlight ()
6909{
6910 if (!NILP (last_mouse_motion_frame)
6911 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6912 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6913 last_mouse_motion_event.x,
6914 last_mouse_motion_event.y);
6915}
6916
6917
6918\f
6919/***********************************************************************
9ea173e8 6920 Tool-bars
06a2c219
GM
6921 ***********************************************************************/
6922
9ea173e8
GM
6923static int x_tool_bar_item P_ ((struct frame *, int, int,
6924 struct glyph **, int *, int *, int *));
06a2c219 6925
9ea173e8 6926/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6927 or -1. */
6928
9ea173e8 6929static int last_tool_bar_item;
06a2c219
GM
6930
6931
9ea173e8
GM
6932/* Get information about the tool-bar item at position X/Y on frame F.
6933 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6934 the current matrix of the tool-bar window of F, or NULL if not
6935 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 6936 item in F->tool_bar_items. Value is
06a2c219 6937
9ea173e8 6938 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6939 0 if X/Y is on the same item that was highlighted before.
6940 1 otherwise. */
6941
6942static int
9ea173e8 6943x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6944 struct frame *f;
6945 int x, y;
6946 struct glyph **glyph;
6947 int *hpos, *vpos, *prop_idx;
6948{
6949 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6950 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6951 int area;
6952
6953 /* Find the glyph under X/Y. */
6954 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6955 if (*glyph == NULL)
6956 return -1;
6957
9ea173e8 6958 /* Get the start of this tool-bar item's properties in
8daf1204 6959 f->tool_bar_items. */
9ea173e8 6960 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6961 return -1;
6962
6963 /* Is mouse on the highlighted item? */
9ea173e8 6964 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6965 && *vpos >= dpyinfo->mouse_face_beg_row
6966 && *vpos <= dpyinfo->mouse_face_end_row
6967 && (*vpos > dpyinfo->mouse_face_beg_row
6968 || *hpos >= dpyinfo->mouse_face_beg_col)
6969 && (*vpos < dpyinfo->mouse_face_end_row
6970 || *hpos < dpyinfo->mouse_face_end_col
6971 || dpyinfo->mouse_face_past_end))
6972 return 0;
6973
6974 return 1;
6975}
6976
6977
9ea173e8 6978/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6979 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6980 or ButtonRelase. */
6981
6982static void
9ea173e8 6983x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6984 struct frame *f;
6985 XButtonEvent *button_event;
6986{
6987 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6988 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6989 int hpos, vpos, prop_idx;
6990 struct glyph *glyph;
6991 Lisp_Object enabled_p;
6992 int x = button_event->x;
6993 int y = button_event->y;
6994
9ea173e8 6995 /* If not on the highlighted tool-bar item, return. */
06a2c219 6996 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6997 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6998 return;
6999
7000 /* If item is disabled, do nothing. */
8daf1204 7001 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7002 if (NILP (enabled_p))
7003 return;
7004
7005 if (button_event->type == ButtonPress)
7006 {
7007 /* Show item in pressed state. */
7008 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7009 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7010 last_tool_bar_item = prop_idx;
06a2c219
GM
7011 }
7012 else
7013 {
7014 Lisp_Object key, frame;
7015 struct input_event event;
7016
7017 /* Show item in released state. */
7018 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7019 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7020
8daf1204 7021 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7022
7023 XSETFRAME (frame, f);
9ea173e8 7024 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7025 event.frame_or_window = frame;
7026 event.arg = frame;
06a2c219
GM
7027 kbd_buffer_store_event (&event);
7028
9ea173e8 7029 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7030 event.frame_or_window = frame;
7031 event.arg = key;
06a2c219
GM
7032 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7033 button_event->state);
7034 kbd_buffer_store_event (&event);
9ea173e8 7035 last_tool_bar_item = -1;
06a2c219
GM
7036 }
7037}
7038
7039
9ea173e8
GM
7040/* Possibly highlight a tool-bar item on frame F when mouse moves to
7041 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7042 note_mouse_highlight. */
7043
7044static void
9ea173e8 7045note_tool_bar_highlight (f, x, y)
06a2c219
GM
7046 struct frame *f;
7047 int x, y;
7048{
9ea173e8 7049 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7050 struct window *w = XWINDOW (window);
7051 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7052 int hpos, vpos;
7053 struct glyph *glyph;
7054 struct glyph_row *row;
5c187dee 7055 int i;
06a2c219
GM
7056 Lisp_Object enabled_p;
7057 int prop_idx;
7058 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 7059 int mouse_down_p, rc;
06a2c219
GM
7060
7061 /* Function note_mouse_highlight is called with negative x(y
7062 values when mouse moves outside of the frame. */
7063 if (x <= 0 || y <= 0)
7064 {
7065 clear_mouse_face (dpyinfo);
7066 return;
7067 }
7068
9ea173e8 7069 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7070 if (rc < 0)
7071 {
9ea173e8 7072 /* Not on tool-bar item. */
06a2c219
GM
7073 clear_mouse_face (dpyinfo);
7074 return;
7075 }
7076 else if (rc == 0)
9ea173e8 7077 /* On same tool-bar item as before. */
06a2c219 7078 goto set_help_echo;
b8009dd1 7079
06a2c219
GM
7080 clear_mouse_face (dpyinfo);
7081
9ea173e8 7082 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7083 mouse_down_p = (dpyinfo->grabbed
7084 && f == last_mouse_frame
7085 && FRAME_LIVE_P (f));
7086 if (mouse_down_p
9ea173e8 7087 && last_tool_bar_item != prop_idx)
06a2c219
GM
7088 return;
7089
7090 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7091 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7092
9ea173e8 7093 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7094 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7095 if (!NILP (enabled_p))
7096 {
7097 /* Compute the x-position of the glyph. In front and past the
7098 image is a space. We include this is the highlighted area. */
7099 row = MATRIX_ROW (w->current_matrix, vpos);
7100 for (i = x = 0; i < hpos; ++i)
7101 x += row->glyphs[TEXT_AREA][i].pixel_width;
7102
7103 /* Record this as the current active region. */
7104 dpyinfo->mouse_face_beg_col = hpos;
7105 dpyinfo->mouse_face_beg_row = vpos;
7106 dpyinfo->mouse_face_beg_x = x;
7107 dpyinfo->mouse_face_beg_y = row->y;
7108 dpyinfo->mouse_face_past_end = 0;
7109
7110 dpyinfo->mouse_face_end_col = hpos + 1;
7111 dpyinfo->mouse_face_end_row = vpos;
7112 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7113 dpyinfo->mouse_face_end_y = row->y;
7114 dpyinfo->mouse_face_window = window;
9ea173e8 7115 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7116
7117 /* Display it as active. */
7118 show_mouse_face (dpyinfo, draw);
7119 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7120 }
06a2c219
GM
7121
7122 set_help_echo:
7123
9ea173e8 7124 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 7125 XTread_socket does the rest. */
7cea38bc 7126 help_echo_object = help_echo_window = Qnil;
be010514 7127 help_echo_pos = -1;
8daf1204 7128 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7129 if (NILP (help_echo))
8daf1204 7130 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7131}
4d73d038 7132
06a2c219
GM
7133
7134\f
7135/* Find the glyph matrix position of buffer position POS in window W.
7136 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7137 current glyphs must be up to date. If POS is above window start
7138 return (0, 0, 0, 0). If POS is after end of W, return end of
7139 last line in W. */
b8009dd1
RS
7140
7141static int
06a2c219
GM
7142fast_find_position (w, pos, hpos, vpos, x, y)
7143 struct window *w;
b8009dd1 7144 int pos;
06a2c219 7145 int *hpos, *vpos, *x, *y;
b8009dd1 7146{
b8009dd1 7147 int i;
bf1c0ba1 7148 int lastcol;
06a2c219
GM
7149 int maybe_next_line_p = 0;
7150 int line_start_position;
7151 int yb = window_text_bottom_y (w);
7152 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7153 struct glyph_row *best_row = row;
7154 int row_vpos = 0, best_row_vpos = 0;
7155 int current_x;
7156
7157 while (row->y < yb)
b8009dd1 7158 {
06a2c219
GM
7159 if (row->used[TEXT_AREA])
7160 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7161 else
7162 line_start_position = 0;
7163
7164 if (line_start_position > pos)
b8009dd1 7165 break;
77b68646
RS
7166 /* If the position sought is the end of the buffer,
7167 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7168 else if (line_start_position == pos
7169 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7170 {
06a2c219 7171 maybe_next_line_p = 1;
77b68646
RS
7172 break;
7173 }
06a2c219
GM
7174 else if (line_start_position > 0)
7175 {
7176 best_row = row;
7177 best_row_vpos = row_vpos;
7178 }
4b0bb6f3
GM
7179
7180 if (row->y + row->height >= yb)
7181 break;
06a2c219
GM
7182
7183 ++row;
7184 ++row_vpos;
b8009dd1 7185 }
06a2c219
GM
7186
7187 /* Find the right column within BEST_ROW. */
7188 lastcol = 0;
7189 current_x = best_row->x;
7190 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7191 {
06a2c219
GM
7192 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7193 int charpos;
7194
7195 charpos = glyph->charpos;
7196 if (charpos == pos)
bf1c0ba1 7197 {
06a2c219
GM
7198 *hpos = i;
7199 *vpos = best_row_vpos;
7200 *x = current_x;
7201 *y = best_row->y;
bf1c0ba1
RS
7202 return 1;
7203 }
06a2c219 7204 else if (charpos > pos)
4d73d038 7205 break;
06a2c219
GM
7206 else if (charpos > 0)
7207 lastcol = i;
7208
7209 current_x += glyph->pixel_width;
bf1c0ba1 7210 }
b8009dd1 7211
77b68646
RS
7212 /* If we're looking for the end of the buffer,
7213 and we didn't find it in the line we scanned,
7214 use the start of the following line. */
06a2c219 7215 if (maybe_next_line_p)
77b68646 7216 {
06a2c219
GM
7217 ++best_row;
7218 ++best_row_vpos;
7219 lastcol = 0;
7220 current_x = best_row->x;
77b68646
RS
7221 }
7222
06a2c219
GM
7223 *vpos = best_row_vpos;
7224 *hpos = lastcol + 1;
7225 *x = current_x;
7226 *y = best_row->y;
b8009dd1
RS
7227 return 0;
7228}
7229
06a2c219 7230
b8009dd1
RS
7231/* Display the active region described by mouse_face_*
7232 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7233
7234static void
06a2c219 7235show_mouse_face (dpyinfo, draw)
7a13e894 7236 struct x_display_info *dpyinfo;
06a2c219 7237 enum draw_glyphs_face draw;
b8009dd1 7238{
7a13e894 7239 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7240 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7241 int i;
06a2c219
GM
7242 int cursor_off_p = 0;
7243 struct cursor_pos saved_cursor;
7244
7245 saved_cursor = output_cursor;
7246
7247 /* If window is in the process of being destroyed, don't bother
7248 to do anything. */
7249 if (w->current_matrix == NULL)
7250 goto set_x_cursor;
7251
7252 /* Recognize when we are called to operate on rows that don't exist
7253 anymore. This can happen when a window is split. */
7254 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7255 goto set_x_cursor;
7256
7257 set_output_cursor (&w->phys_cursor);
7258
7259 /* Note that mouse_face_beg_row etc. are window relative. */
7260 for (i = dpyinfo->mouse_face_beg_row;
7261 i <= dpyinfo->mouse_face_end_row;
7262 i++)
7263 {
7264 int start_hpos, end_hpos, start_x;
7265 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7266
7267 /* Don't do anything if row doesn't have valid contents. */
7268 if (!row->enabled_p)
7269 continue;
7270
7271 /* For all but the first row, the highlight starts at column 0. */
7272 if (i == dpyinfo->mouse_face_beg_row)
7273 {
7274 start_hpos = dpyinfo->mouse_face_beg_col;
7275 start_x = dpyinfo->mouse_face_beg_x;
7276 }
7277 else
7278 {
7279 start_hpos = 0;
7280 start_x = 0;
7281 }
7282
7283 if (i == dpyinfo->mouse_face_end_row)
7284 end_hpos = dpyinfo->mouse_face_end_col;
7285 else
7286 end_hpos = row->used[TEXT_AREA];
7287
7288 /* If the cursor's in the text we are about to rewrite, turn the
7289 cursor off. */
7290 if (!w->pseudo_window_p
7291 && i == output_cursor.vpos
7292 && output_cursor.hpos >= start_hpos - 1
7293 && output_cursor.hpos <= end_hpos)
514e4681 7294 {
06a2c219
GM
7295 x_update_window_cursor (w, 0);
7296 cursor_off_p = 1;
514e4681 7297 }
b8009dd1 7298
06a2c219 7299 if (end_hpos > start_hpos)
64f26cf5
GM
7300 {
7301 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7302 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7303 start_hpos, end_hpos, draw, NULL, NULL, 0);
7304 }
b8009dd1
RS
7305 }
7306
514e4681 7307 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7308 if (cursor_off_p)
7309 x_display_cursor (w, 1,
7310 output_cursor.hpos, output_cursor.vpos,
7311 output_cursor.x, output_cursor.y);
2729a2b5 7312
06a2c219 7313 output_cursor = saved_cursor;
fb3b7de5 7314
06a2c219
GM
7315 set_x_cursor:
7316
7317 /* Change the mouse cursor. */
7318 if (draw == DRAW_NORMAL_TEXT)
7319 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7320 f->output_data.x->text_cursor);
7321 else if (draw == DRAW_MOUSE_FACE)
334208b7 7322 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7323 f->output_data.x->cross_cursor);
27ead1d5 7324 else
334208b7 7325 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7326 f->output_data.x->nontext_cursor);
b8009dd1
RS
7327}
7328
7329/* Clear out the mouse-highlighted active region.
06a2c219 7330 Redraw it un-highlighted first. */
b8009dd1 7331
06a2c219 7332void
7a13e894
RS
7333clear_mouse_face (dpyinfo)
7334 struct x_display_info *dpyinfo;
b8009dd1 7335{
607d9f9f
GM
7336#if 0 /* This prevents redrawing tool bar items when changing from one
7337 to another while a tooltip is open, so don't do it. */
685f4368 7338 if (!NILP (tip_frame))
06a2c219 7339 return;
7948d50a 7340#endif
06a2c219 7341
7a13e894 7342 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7343 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7344
7a13e894
RS
7345 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7346 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7347 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7348}
e687d06e 7349
71b8321e
GM
7350
7351/* Clear any mouse-face on window W. This function is part of the
7352 redisplay interface, and is called from try_window_id and similar
7353 functions to ensure the mouse-highlight is off. */
7354
7355static void
7356x_clear_mouse_face (w)
7357 struct window *w;
7358{
7359 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7360 Lisp_Object window;
7361
2e636f9d 7362 BLOCK_INPUT;
71b8321e
GM
7363 XSETWINDOW (window, w);
7364 if (EQ (window, dpyinfo->mouse_face_window))
7365 clear_mouse_face (dpyinfo);
2e636f9d 7366 UNBLOCK_INPUT;
71b8321e
GM
7367}
7368
7369
e687d06e
RS
7370/* Just discard the mouse face information for frame F, if any.
7371 This is used when the size of F is changed. */
7372
dfcf069d 7373void
e687d06e
RS
7374cancel_mouse_face (f)
7375 FRAME_PTR f;
7376{
7377 Lisp_Object window;
7378 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7379
7380 window = dpyinfo->mouse_face_window;
7381 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7382 {
7383 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7384 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7385 dpyinfo->mouse_face_window = Qnil;
7386 }
7387}
b8009dd1 7388\f
ab648270
JB
7389static struct scroll_bar *x_window_to_scroll_bar ();
7390static void x_scroll_bar_report_motion ();
12ba150f 7391
90e65f07 7392/* Return the current position of the mouse.
2d7fc7e8 7393 *fp should be a frame which indicates which display to ask about.
90e65f07 7394
2d7fc7e8 7395 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7396 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7397 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7398 position on the scroll bar.
12ba150f 7399
2d7fc7e8 7400 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7401 mouse is on, *bar_window to nil, and *x and *y to the character cell
7402 the mouse is over.
7403
06a2c219 7404 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7405 was at this position.
7406
a135645a
RS
7407 Don't store anything if we don't have a valid set of values to report.
7408
90e65f07 7409 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7410 movement. */
90e65f07
JB
7411
7412static void
1cf412ec 7413XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7414 FRAME_PTR *fp;
1cf412ec 7415 int insist;
12ba150f 7416 Lisp_Object *bar_window;
ab648270 7417 enum scroll_bar_part *part;
90e65f07 7418 Lisp_Object *x, *y;
e5d77022 7419 unsigned long *time;
90e65f07 7420{
a135645a
RS
7421 FRAME_PTR f1;
7422
90e65f07
JB
7423 BLOCK_INPUT;
7424
8bcee03e 7425 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7426 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7427 else
7428 {
12ba150f
JB
7429 Window root;
7430 int root_x, root_y;
90e65f07 7431
12ba150f
JB
7432 Window dummy_window;
7433 int dummy;
7434
39d8bb4d
KH
7435 Lisp_Object frame, tail;
7436
7437 /* Clear the mouse-moved flag for every frame on this display. */
7438 FOR_EACH_FRAME (tail, frame)
7439 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7440 XFRAME (frame)->mouse_moved = 0;
7441
ab648270 7442 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7443
7444 /* Figure out which root window we're on. */
334208b7
RS
7445 XQueryPointer (FRAME_X_DISPLAY (*fp),
7446 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7447
7448 /* The root window which contains the pointer. */
7449 &root,
7450
7451 /* Trash which we can't trust if the pointer is on
7452 a different screen. */
7453 &dummy_window,
7454
7455 /* The position on that root window. */
58769bee 7456 &root_x, &root_y,
12ba150f
JB
7457
7458 /* More trash we can't trust. */
7459 &dummy, &dummy,
7460
7461 /* Modifier keys and pointer buttons, about which
7462 we don't care. */
7463 (unsigned int *) &dummy);
7464
7465 /* Now we have a position on the root; find the innermost window
7466 containing the pointer. */
7467 {
7468 Window win, child;
7469 int win_x, win_y;
06a2c219 7470 int parent_x = 0, parent_y = 0;
e99db5a1 7471 int count;
12ba150f
JB
7472
7473 win = root;
69388238 7474
2d7fc7e8
RS
7475 /* XTranslateCoordinates can get errors if the window
7476 structure is changing at the same time this function
7477 is running. So at least we must not crash from them. */
7478
e99db5a1 7479 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7480
334208b7 7481 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7482 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7483 {
69388238
RS
7484 /* If mouse was grabbed on a frame, give coords for that frame
7485 even if the mouse is now outside it. */
334208b7 7486 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7487
12ba150f 7488 /* From-window, to-window. */
69388238 7489 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7490
7491 /* From-position, to-position. */
7492 root_x, root_y, &win_x, &win_y,
7493
7494 /* Child of win. */
7495 &child);
69388238
RS
7496 f1 = last_mouse_frame;
7497 }
7498 else
7499 {
7500 while (1)
7501 {
334208b7 7502 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7503
69388238
RS
7504 /* From-window, to-window. */
7505 root, win,
12ba150f 7506
69388238
RS
7507 /* From-position, to-position. */
7508 root_x, root_y, &win_x, &win_y,
7509
7510 /* Child of win. */
7511 &child);
7512
9af3143a 7513 if (child == None || child == win)
69388238
RS
7514 break;
7515
7516 win = child;
7517 parent_x = win_x;
7518 parent_y = win_y;
7519 }
12ba150f 7520
69388238
RS
7521 /* Now we know that:
7522 win is the innermost window containing the pointer
7523 (XTC says it has no child containing the pointer),
7524 win_x and win_y are the pointer's position in it
7525 (XTC did this the last time through), and
7526 parent_x and parent_y are the pointer's position in win's parent.
7527 (They are what win_x and win_y were when win was child.
7528 If win is the root window, it has no parent, and
7529 parent_{x,y} are invalid, but that's okay, because we'll
7530 never use them in that case.) */
7531
7532 /* Is win one of our frames? */
19126e11 7533 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7534
7535#ifdef USE_X_TOOLKIT
7536 /* If we end up with the menu bar window, say it's not
7537 on the frame. */
7538 if (f1 != NULL
7539 && f1->output_data.x->menubar_widget
7540 && win == XtWindow (f1->output_data.x->menubar_widget))
7541 f1 = NULL;
7542#endif /* USE_X_TOOLKIT */
69388238 7543 }
58769bee 7544
2d7fc7e8
RS
7545 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7546 f1 = 0;
7547
e99db5a1 7548 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7549
ab648270 7550 /* If not, is it one of our scroll bars? */
a135645a 7551 if (! f1)
12ba150f 7552 {
ab648270 7553 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7554
7555 if (bar)
7556 {
a135645a 7557 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7558 win_x = parent_x;
7559 win_y = parent_y;
7560 }
7561 }
90e65f07 7562
8bcee03e 7563 if (f1 == 0 && insist > 0)
b86bd3dd 7564 f1 = SELECTED_FRAME ();
1cf412ec 7565
a135645a 7566 if (f1)
12ba150f 7567 {
06a2c219
GM
7568 /* Ok, we found a frame. Store all the values.
7569 last_mouse_glyph is a rectangle used to reduce the
7570 generation of mouse events. To not miss any motion
7571 events, we must divide the frame into rectangles of the
7572 size of the smallest character that could be displayed
7573 on it, i.e. into the same rectangles that matrices on
7574 the frame are divided into. */
7575
7576#if OLD_REDISPLAY_CODE
2b5c9e71 7577 int ignore1, ignore2;
2b5c9e71 7578 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7579 &last_mouse_glyph,
1cf412ec
RS
7580 FRAME_X_DISPLAY_INFO (f1)->grabbed
7581 || insist);
06a2c219
GM
7582#else
7583 {
7584 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7585 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7586 int x = win_x;
7587 int y = win_y;
7588
7589 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7590 round down even for negative values. */
7591 if (x < 0)
7592 x -= width - 1;
7593 if (y < 0)
7594 y -= height - 1;
7595
7596 last_mouse_glyph.width = width;
7597 last_mouse_glyph.height = height;
7598 last_mouse_glyph.x = (x + width - 1) / width * width;
7599 last_mouse_glyph.y = (y + height - 1) / height * height;
7600 }
7601#endif
12ba150f
JB
7602
7603 *bar_window = Qnil;
7604 *part = 0;
334208b7 7605 *fp = f1;
e0c1aef2
KH
7606 XSETINT (*x, win_x);
7607 XSETINT (*y, win_y);
12ba150f
JB
7608 *time = last_mouse_movement_time;
7609 }
7610 }
7611 }
90e65f07
JB
7612
7613 UNBLOCK_INPUT;
7614}
f451eb13 7615
06a2c219 7616
06a2c219 7617#ifdef USE_X_TOOLKIT
bffcfca9
GM
7618
7619/* Atimer callback function for TIMER. Called every 0.1s to process
7620 Xt timeouts, if needed. We must avoid calling XtAppPending as
7621 much as possible because that function does an implicit XFlush
7622 that slows us down. */
7623
7624static void
7625x_process_timeouts (timer)
7626 struct atimer *timer;
7627{
7628 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7629 {
7630 BLOCK_INPUT;
7631 while (XtAppPending (Xt_app_con) & XtIMTimer)
7632 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7633 UNBLOCK_INPUT;
7634 }
06a2c219
GM
7635}
7636
bffcfca9 7637#endif /* USE_X_TOOLKIT */
06a2c219
GM
7638
7639\f
7640/* Scroll bar support. */
7641
7642/* Given an X window ID, find the struct scroll_bar which manages it.
7643 This can be called in GC, so we have to make sure to strip off mark
7644 bits. */
bffcfca9 7645
06a2c219
GM
7646static struct scroll_bar *
7647x_window_to_scroll_bar (window_id)
7648 Window window_id;
7649{
7650 Lisp_Object tail;
7651
7652 for (tail = Vframe_list;
7653 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7654 tail = XCDR (tail))
06a2c219
GM
7655 {
7656 Lisp_Object frame, bar, condemned;
7657
8e713be6 7658 frame = XCAR (tail);
06a2c219
GM
7659 /* All elements of Vframe_list should be frames. */
7660 if (! GC_FRAMEP (frame))
7661 abort ();
7662
7663 /* Scan this frame's scroll bar list for a scroll bar with the
7664 right window ID. */
7665 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7666 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7667 /* This trick allows us to search both the ordinary and
7668 condemned scroll bar lists with one loop. */
7669 ! GC_NILP (bar) || (bar = condemned,
7670 condemned = Qnil,
7671 ! GC_NILP (bar));
7672 bar = XSCROLL_BAR (bar)->next)
7673 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7674 return XSCROLL_BAR (bar);
7675 }
7676
7677 return 0;
7678}
7679
7680
7681\f
7682/************************************************************************
7683 Toolkit scroll bars
7684 ************************************************************************/
7685
eccc05db 7686#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7687
7688static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7689static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7690static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7691 struct scroll_bar *));
7692static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7693 int, int, int));
7694
7695
7696/* Id of action hook installed for scroll bars. */
7697
7698static XtActionHookId action_hook_id;
7699
7700/* Lisp window being scrolled. Set when starting to interact with
7701 a toolkit scroll bar, reset to nil when ending the interaction. */
7702
7703static Lisp_Object window_being_scrolled;
7704
7705/* Last scroll bar part sent in xm_scroll_callback. */
7706
7707static int last_scroll_bar_part;
7708
ec18280f
SM
7709/* Whether this is an Xaw with arrow-scrollbars. This should imply
7710 that movements of 1/20 of the screen size are mapped to up/down. */
7711
7712static Boolean xaw3d_arrow_scroll;
7713
7714/* Whether the drag scrolling maintains the mouse at the top of the
7715 thumb. If not, resizing the thumb needs to be done more carefully
7716 to avoid jerkyness. */
7717
7718static Boolean xaw3d_pick_top;
7719
06a2c219
GM
7720
7721/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7722 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7723 the user ends an interaction with the scroll bar, and generates
7724 a `end-scroll' scroll_bar_click' event if so. */
7725
7726static void
7727xt_action_hook (widget, client_data, action_name, event, params,
7728 num_params)
7729 Widget widget;
7730 XtPointer client_data;
7731 String action_name;
7732 XEvent *event;
7733 String *params;
7734 Cardinal *num_params;
7735{
7736 int scroll_bar_p;
7737 char *end_action;
7738
7739#ifdef USE_MOTIF
7740 scroll_bar_p = XmIsScrollBar (widget);
7741 end_action = "Release";
ec18280f 7742#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7743 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7744 end_action = "EndScroll";
ec18280f 7745#endif /* USE_MOTIF */
06a2c219 7746
06a2c219
GM
7747 if (scroll_bar_p
7748 && strcmp (action_name, end_action) == 0
7749 && WINDOWP (window_being_scrolled))
7750 {
7751 struct window *w;
7752
7753 x_send_scroll_bar_event (window_being_scrolled,
7754 scroll_bar_end_scroll, 0, 0);
7755 w = XWINDOW (window_being_scrolled);
7756 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7757 window_being_scrolled = Qnil;
7758 last_scroll_bar_part = -1;
bffcfca9
GM
7759
7760 /* Xt timeouts no longer needed. */
7761 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7762 }
7763}
7764
07b3d16e
GM
7765/* A vector of windows used for communication between
7766 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7767
7768static struct window **scroll_bar_windows;
7769static int scroll_bar_windows_size;
7770
06a2c219
GM
7771
7772/* Send a client message with message type Xatom_Scrollbar for a
7773 scroll action to the frame of WINDOW. PART is a value identifying
7774 the part of the scroll bar that was clicked on. PORTION is the
7775 amount to scroll of a whole of WHOLE. */
7776
7777static void
7778x_send_scroll_bar_event (window, part, portion, whole)
7779 Lisp_Object window;
7780 int part, portion, whole;
7781{
7782 XEvent event;
7783 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7784 struct window *w = XWINDOW (window);
7785 struct frame *f = XFRAME (w->frame);
7786 int i;
06a2c219 7787
07b3d16e
GM
7788 BLOCK_INPUT;
7789
06a2c219
GM
7790 /* Construct a ClientMessage event to send to the frame. */
7791 ev->type = ClientMessage;
7792 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7793 ev->display = FRAME_X_DISPLAY (f);
7794 ev->window = FRAME_X_WINDOW (f);
7795 ev->format = 32;
07b3d16e
GM
7796
7797 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7798 not enough to store a pointer or Lisp_Object on a 64 bit system.
7799 So, store the window in scroll_bar_windows and pass the index
7800 into that array in the event. */
7801 for (i = 0; i < scroll_bar_windows_size; ++i)
7802 if (scroll_bar_windows[i] == NULL)
7803 break;
7804
7805 if (i == scroll_bar_windows_size)
7806 {
7807 int new_size = max (10, 2 * scroll_bar_windows_size);
7808 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7809 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7810
7811 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7812 nbytes);
7813 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7814 scroll_bar_windows_size = new_size;
7815 }
7816
7817 scroll_bar_windows[i] = w;
7818 ev->data.l[0] = (long) i;
06a2c219
GM
7819 ev->data.l[1] = (long) part;
7820 ev->data.l[2] = (long) 0;
7821 ev->data.l[3] = (long) portion;
7822 ev->data.l[4] = (long) whole;
7823
bffcfca9
GM
7824 /* Make Xt timeouts work while the scroll bar is active. */
7825 toolkit_scroll_bar_interaction = 1;
7826
06a2c219
GM
7827 /* Setting the event mask to zero means that the message will
7828 be sent to the client that created the window, and if that
7829 window no longer exists, no event will be sent. */
06a2c219
GM
7830 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7831 UNBLOCK_INPUT;
7832}
7833
7834
7835/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7836 in *IEVENT. */
7837
7838static void
7839x_scroll_bar_to_input_event (event, ievent)
7840 XEvent *event;
7841 struct input_event *ievent;
7842{
7843 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7844 Lisp_Object window;
7845 struct frame *f;
07b3d16e
GM
7846 struct window *w;
7847
7848 w = scroll_bar_windows[ev->data.l[0]];
7849 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 7850
07b3d16e
GM
7851 XSETWINDOW (window, w);
7852 f = XFRAME (w->frame);
06a2c219
GM
7853
7854 ievent->kind = scroll_bar_click;
7855 ievent->frame_or_window = window;
0f8aabe9 7856 ievent->arg = Qnil;
06a2c219
GM
7857 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7858 ievent->part = ev->data.l[1];
7859 ievent->code = ev->data.l[2];
7860 ievent->x = make_number ((int) ev->data.l[3]);
7861 ievent->y = make_number ((int) ev->data.l[4]);
7862 ievent->modifiers = 0;
7863}
7864
7865
7866#ifdef USE_MOTIF
7867
7868/* Minimum and maximum values used for Motif scroll bars. */
7869
7870#define XM_SB_MIN 1
7871#define XM_SB_MAX 10000000
7872#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7873
7874
7875/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7876 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7877 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7878
7879static void
7880xm_scroll_callback (widget, client_data, call_data)
7881 Widget widget;
7882 XtPointer client_data, call_data;
7883{
7884 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7885 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7886 double percent;
7887 int part = -1, whole = 0, portion = 0;
7888
7889 switch (cs->reason)
7890 {
7891 case XmCR_DECREMENT:
7892 bar->dragging = Qnil;
7893 part = scroll_bar_up_arrow;
7894 break;
7895
7896 case XmCR_INCREMENT:
7897 bar->dragging = Qnil;
7898 part = scroll_bar_down_arrow;
7899 break;
7900
7901 case XmCR_PAGE_DECREMENT:
7902 bar->dragging = Qnil;
7903 part = scroll_bar_above_handle;
7904 break;
7905
7906 case XmCR_PAGE_INCREMENT:
7907 bar->dragging = Qnil;
7908 part = scroll_bar_below_handle;
7909 break;
7910
7911 case XmCR_TO_TOP:
7912 bar->dragging = Qnil;
7913 part = scroll_bar_to_top;
7914 break;
7915
7916 case XmCR_TO_BOTTOM:
7917 bar->dragging = Qnil;
7918 part = scroll_bar_to_bottom;
7919 break;
7920
7921 case XmCR_DRAG:
7922 {
7923 int slider_size;
7924 int dragging_down_p = (INTEGERP (bar->dragging)
7925 && XINT (bar->dragging) <= cs->value);
7926
7927 /* Get the slider size. */
7928 BLOCK_INPUT;
7929 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7930 UNBLOCK_INPUT;
7931
7932 /* At the max position of the scroll bar, do a line-wise
7933 movement. Without doing anything, the LessTif scroll bar
7934 calls us with the same cs->value again and again. If we
7935 want to make sure that we can reach the end of the buffer,
7936 we have to do something.
7937
7938 Implementation note: setting bar->dragging always to
7939 cs->value gives a smoother movement at the max position.
7940 Setting it to nil when doing line-wise movement gives
7941 a better slider behavior. */
7942
7943 if (cs->value + slider_size == XM_SB_MAX
7944 || (dragging_down_p
7945 && last_scroll_bar_part == scroll_bar_down_arrow))
7946 {
7947 part = scroll_bar_down_arrow;
7948 bar->dragging = Qnil;
7949 }
7950 else
7951 {
7952 whole = XM_SB_RANGE;
7953 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7954 part = scroll_bar_handle;
7955 bar->dragging = make_number (cs->value);
7956 }
7957 }
7958 break;
7959
7960 case XmCR_VALUE_CHANGED:
7961 break;
7962 };
7963
7964 if (part >= 0)
7965 {
7966 window_being_scrolled = bar->window;
7967 last_scroll_bar_part = part;
7968 x_send_scroll_bar_event (bar->window, part, portion, whole);
7969 }
7970}
7971
7972
ec18280f 7973#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7974
7975
ec18280f 7976/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7977 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7978 scroll bar struct. CALL_DATA is a pointer to a float saying where
7979 the thumb is. */
7980
7981static void
ec18280f 7982xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7983 Widget widget;
7984 XtPointer client_data, call_data;
7985{
7986 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7987 float top = *(float *) call_data;
7988 float shown;
ec18280f
SM
7989 int whole, portion, height;
7990 int part;
06a2c219
GM
7991
7992 /* Get the size of the thumb, a value between 0 and 1. */
7993 BLOCK_INPUT;
ec18280f 7994 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7995 UNBLOCK_INPUT;
7996
7997 whole = 10000000;
7998 portion = shown < 1 ? top * whole : 0;
06a2c219 7999
ec18280f
SM
8000 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8001 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8002 the bottom, so we force the scrolling whenever we see that we're
8003 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8004 we try to ensure that we always stay two pixels away from the
8005 bottom). */
06a2c219
GM
8006 part = scroll_bar_down_arrow;
8007 else
8008 part = scroll_bar_handle;
8009
8010 window_being_scrolled = bar->window;
8011 bar->dragging = make_number (portion);
8012 last_scroll_bar_part = part;
8013 x_send_scroll_bar_event (bar->window, part, portion, whole);
8014}
8015
8016
ec18280f
SM
8017/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8018 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8019 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8020 the scroll bar. CALL_DATA is an integer specifying the action that
8021 has taken place. It's magnitude is in the range 0..height of the
8022 scroll bar. Negative values mean scroll towards buffer start.
8023 Values < height of scroll bar mean line-wise movement. */
8024
8025static void
ec18280f 8026xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8027 Widget widget;
8028 XtPointer client_data, call_data;
8029{
8030 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8031 int position = (int) call_data;
8032 Dimension height;
8033 int part;
8034
8035 /* Get the height of the scroll bar. */
8036 BLOCK_INPUT;
8037 XtVaGetValues (widget, XtNheight, &height, NULL);
8038 UNBLOCK_INPUT;
8039
ec18280f
SM
8040 if (abs (position) >= height)
8041 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8042
8043 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8044 it maps line-movement to call_data = max(5, height/20). */
8045 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8046 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8047 else
ec18280f 8048 part = scroll_bar_move_ratio;
06a2c219
GM
8049
8050 window_being_scrolled = bar->window;
8051 bar->dragging = Qnil;
8052 last_scroll_bar_part = part;
ec18280f 8053 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8054}
8055
8056
8057#endif /* not USE_MOTIF */
8058
8059
8060/* Create the widget for scroll bar BAR on frame F. Record the widget
8061 and X window of the scroll bar in BAR. */
8062
8063static void
8064x_create_toolkit_scroll_bar (f, bar)
8065 struct frame *f;
8066 struct scroll_bar *bar;
8067{
8068 Window xwindow;
8069 Widget widget;
8070 Arg av[20];
8071 int ac = 0;
8072 char *scroll_bar_name = "verticalScrollBar";
8073 unsigned long pixel;
8074
8075 BLOCK_INPUT;
8076
8077#ifdef USE_MOTIF
8078 /* LessTif 0.85, problems:
8079
8080 1. When the mouse if over the scroll bar, the scroll bar will
8081 get keyboard events. I didn't find a way to turn this off.
8082
8083 2. Do we have to explicitly set the cursor to get an arrow
8084 cursor (see below)? */
8085
8086 /* Set resources. Create the widget. */
8087 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8088 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8089 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8090 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8091 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8092 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8093 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8094
8095 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8096 if (pixel != -1)
8097 {
8098 XtSetArg (av[ac], XmNforeground, pixel);
8099 ++ac;
8100 }
8101
8102 pixel = f->output_data.x->scroll_bar_background_pixel;
8103 if (pixel != -1)
8104 {
8105 XtSetArg (av[ac], XmNbackground, pixel);
8106 ++ac;
8107 }
8108
8109 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8110 scroll_bar_name, av, ac);
8111
8112 /* Add one callback for everything that can happen. */
8113 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8114 (XtPointer) bar);
8115 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8116 (XtPointer) bar);
8117 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8118 (XtPointer) bar);
8119 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8120 (XtPointer) bar);
8121 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8122 (XtPointer) bar);
8123 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8124 (XtPointer) bar);
8125 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8126 (XtPointer) bar);
8127
8128 /* Realize the widget. Only after that is the X window created. */
8129 XtRealizeWidget (widget);
8130
8131 /* Set the cursor to an arrow. I didn't find a resource to do that.
8132 And I'm wondering why it hasn't an arrow cursor by default. */
8133 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8134 f->output_data.x->nontext_cursor);
8135
ec18280f 8136#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8137
8138 /* Set resources. Create the widget. The background of the
8139 Xaw3d scroll bar widget is a little bit light for my taste.
8140 We don't alter it here to let users change it according
8141 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8142 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8143 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8144 /* For smoother scrolling with Xaw3d -sm */
8145 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8146 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8147
8148 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8149 if (pixel != -1)
8150 {
8151 XtSetArg (av[ac], XtNforeground, pixel);
8152 ++ac;
8153 }
8154
8155 pixel = f->output_data.x->scroll_bar_background_pixel;
8156 if (pixel != -1)
8157 {
8158 XtSetArg (av[ac], XtNbackground, pixel);
8159 ++ac;
8160 }
8161
8162 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8163 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8164
8165 {
8166 char *initial = "";
8167 char *val = initial;
8168 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8169 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8170 if (val == initial)
8171 { /* ARROW_SCROLL */
8172 xaw3d_arrow_scroll = True;
8173 /* Isn't that just a personal preference ? -sm */
8174 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8175 }
8176 }
06a2c219
GM
8177
8178 /* Define callbacks. */
ec18280f
SM
8179 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8180 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8181 (XtPointer) bar);
8182
8183 /* Realize the widget. Only after that is the X window created. */
8184 XtRealizeWidget (widget);
8185
ec18280f 8186#endif /* !USE_MOTIF */
06a2c219
GM
8187
8188 /* Install an action hook that let's us detect when the user
8189 finishes interacting with a scroll bar. */
8190 if (action_hook_id == 0)
8191 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8192
8193 /* Remember X window and widget in the scroll bar vector. */
8194 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8195 xwindow = XtWindow (widget);
8196 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8197
8198 UNBLOCK_INPUT;
8199}
8200
8201
8202/* Set the thumb size and position of scroll bar BAR. We are currently
8203 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8204
8205static void
8206x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8207 struct scroll_bar *bar;
8208 int portion, position, whole;
f451eb13 8209{
e83dc917
GM
8210 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8211 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8212 float top, shown;
f451eb13 8213
06a2c219
GM
8214 if (whole == 0)
8215 top = 0, shown = 1;
8216 else
f451eb13 8217 {
06a2c219
GM
8218 top = (float) position / whole;
8219 shown = (float) portion / whole;
8220 }
f451eb13 8221
06a2c219 8222 BLOCK_INPUT;
f451eb13 8223
06a2c219
GM
8224#ifdef USE_MOTIF
8225 {
8226 int size, value;
8227 Boolean arrow1_selected, arrow2_selected;
8228 unsigned char flags;
8229 XmScrollBarWidget sb;
8230
ec18280f 8231 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8232 is the scroll bar's maximum and MIN is the scroll bar's minimum
8233 value. */
8234 size = shown * XM_SB_RANGE;
8235 size = min (size, XM_SB_RANGE);
8236 size = max (size, 1);
8237
8238 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8239 value = top * XM_SB_RANGE;
8240 value = min (value, XM_SB_MAX - size);
8241 value = max (value, XM_SB_MIN);
8242
8243 /* LessTif: Calling XmScrollBarSetValues after an increment or
8244 decrement turns off auto-repeat LessTif-internally. This can
8245 be seen in ScrollBar.c which resets Arrow1Selected and
8246 Arrow2Selected. It also sets internal flags so that LessTif
8247 believes the mouse is in the slider. We either have to change
8248 our code, or work around that by accessing private data. */
8249
8250 sb = (XmScrollBarWidget) widget;
8251 arrow1_selected = sb->scrollBar.arrow1_selected;
8252 arrow2_selected = sb->scrollBar.arrow2_selected;
8253 flags = sb->scrollBar.flags;
8254
8255 if (NILP (bar->dragging))
8256 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8257 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8258 /* This has the negative side effect that the slider value is
ec18280f 8259 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8260 page-wise movement. */
8261 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8262 else
8263 {
8264 /* If currently dragging, only update the slider size.
8265 This reduces flicker effects. */
8266 int old_value, old_size, increment, page_increment;
8267
8268 XmScrollBarGetValues (widget, &old_value, &old_size,
8269 &increment, &page_increment);
8270 XmScrollBarSetValues (widget, old_value,
8271 min (size, XM_SB_RANGE - old_value),
8272 0, 0, False);
8273 }
8274
8275 sb->scrollBar.arrow1_selected = arrow1_selected;
8276 sb->scrollBar.arrow2_selected = arrow2_selected;
8277 sb->scrollBar.flags = flags;
8278 }
ec18280f 8279#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8280 {
ec18280f
SM
8281 float old_top, old_shown;
8282 Dimension height;
8283 XtVaGetValues (widget,
8284 XtNtopOfThumb, &old_top,
8285 XtNshown, &old_shown,
8286 XtNheight, &height,
8287 NULL);
8288
8289 /* Massage the top+shown values. */
8290 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8291 top = max (0, min (1, top));
8292 else
8293 top = old_top;
8294 /* Keep two pixels available for moving the thumb down. */
8295 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8296
8297 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8298 check that your system's configuration file contains a define
8299 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8300 if (top != old_top || shown != old_shown)
eb393530 8301 {
ec18280f 8302 if (NILP (bar->dragging))
eb393530 8303 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8304 else
8305 {
ec18280f
SM
8306#ifdef HAVE_XAW3D
8307 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8308 int scroll_mode = 0;
ec18280f
SM
8309
8310 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8311 if (xaw3d_arrow_scroll)
8312 {
8313 /* Xaw3d stupidly ignores resize requests while dragging
8314 so we have to make it believe it's not in dragging mode. */
8315 scroll_mode = sb->scrollbar.scroll_mode;
8316 if (scroll_mode == 2)
8317 sb->scrollbar.scroll_mode = 0;
8318 }
8319#endif
8320 /* Try to make the scrolling a tad smoother. */
8321 if (!xaw3d_pick_top)
8322 shown = min (shown, old_shown);
8323
8324 XawScrollbarSetThumb (widget, top, shown);
8325
8326#ifdef HAVE_XAW3D
8327 if (xaw3d_arrow_scroll && scroll_mode == 2)
8328 sb->scrollbar.scroll_mode = scroll_mode;
8329#endif
06a2c219 8330 }
06a2c219
GM
8331 }
8332 }
ec18280f 8333#endif /* !USE_MOTIF */
06a2c219
GM
8334
8335 UNBLOCK_INPUT;
f451eb13
JB
8336}
8337
06a2c219
GM
8338#endif /* USE_TOOLKIT_SCROLL_BARS */
8339
8340
8341\f
8342/************************************************************************
8343 Scroll bars, general
8344 ************************************************************************/
8345
8346/* Create a scroll bar and return the scroll bar vector for it. W is
8347 the Emacs window on which to create the scroll bar. TOP, LEFT,
8348 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8349 scroll bar. */
8350
ab648270 8351static struct scroll_bar *
06a2c219
GM
8352x_scroll_bar_create (w, top, left, width, height)
8353 struct window *w;
f451eb13
JB
8354 int top, left, width, height;
8355{
06a2c219 8356 struct frame *f = XFRAME (w->frame);
334208b7
RS
8357 struct scroll_bar *bar
8358 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8359
8360 BLOCK_INPUT;
8361
eccc05db 8362#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8363 x_create_toolkit_scroll_bar (f, bar);
8364#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8365 {
8366 XSetWindowAttributes a;
8367 unsigned long mask;
5c187dee 8368 Window window;
06a2c219
GM
8369
8370 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8371 if (a.background_pixel == -1)
8372 a.background_pixel = f->output_data.x->background_pixel;
8373
12ba150f 8374 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8375 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8376 | ExposureMask);
7a13e894 8377 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8378
dbc4e1c1 8379 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8380
06a2c219
GM
8381 /* Clear the area of W that will serve as a scroll bar. This is
8382 for the case that a window has been split horizontally. In
8383 this case, no clear_frame is generated to reduce flickering. */
c5e6e06b
GM
8384 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8385 left, top, width,
8386 window_box_height (w), False);
06a2c219
GM
8387
8388 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8389 /* Position and size of scroll bar. */
8390 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8391 top,
8392 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8393 height,
8394 /* Border width, depth, class, and visual. */
8395 0,
8396 CopyFromParent,
8397 CopyFromParent,
8398 CopyFromParent,
8399 /* Attributes. */
8400 mask, &a);
8401 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8402 }
06a2c219 8403#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8404
06a2c219 8405 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8406 XSETINT (bar->top, top);
8407 XSETINT (bar->left, left);
8408 XSETINT (bar->width, width);
8409 XSETINT (bar->height, height);
8410 XSETINT (bar->start, 0);
8411 XSETINT (bar->end, 0);
12ba150f 8412 bar->dragging = Qnil;
f451eb13
JB
8413
8414 /* Add bar to its frame's list of scroll bars. */
334208b7 8415 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8416 bar->prev = Qnil;
334208b7 8417 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8418 if (!NILP (bar->next))
e0c1aef2 8419 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8420
06a2c219 8421 /* Map the window/widget. */
eccc05db 8422#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
8423 {
8424 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8425 XtConfigureWidget (scroll_bar,
8426 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8427 top,
8428 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8429 max (height, 1), 0);
8430 XtMapWidget (scroll_bar);
8431 }
06a2c219 8432#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8433 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8434#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8435
8436 UNBLOCK_INPUT;
12ba150f 8437 return bar;
f451eb13
JB
8438}
8439
06a2c219 8440
12ba150f 8441/* Draw BAR's handle in the proper position.
06a2c219 8442
12ba150f
JB
8443 If the handle is already drawn from START to END, don't bother
8444 redrawing it, unless REBUILD is non-zero; in that case, always
8445 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8446 events.)
12ba150f
JB
8447
8448 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8449 fit inside its rectangle, but if the user is dragging the scroll
8450 bar handle, we want to let them drag it down all the way, so that
8451 the bar's top is as far down as it goes; otherwise, there's no way
8452 to move to the very end of the buffer. */
8453
5c187dee
GM
8454#ifndef USE_TOOLKIT_SCROLL_BARS
8455
f451eb13 8456static void
ab648270
JB
8457x_scroll_bar_set_handle (bar, start, end, rebuild)
8458 struct scroll_bar *bar;
f451eb13 8459 int start, end;
12ba150f 8460 int rebuild;
f451eb13 8461{
12ba150f 8462 int dragging = ! NILP (bar->dragging);
ab648270 8463 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8464 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8465 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8466
8467 /* If the display is already accurate, do nothing. */
8468 if (! rebuild
8469 && start == XINT (bar->start)
8470 && end == XINT (bar->end))
8471 return;
8472
f451eb13
JB
8473 BLOCK_INPUT;
8474
8475 {
d9cdbb3d
RS
8476 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8477 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8478 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8479
8480 /* Make sure the values are reasonable, and try to preserve
8481 the distance between start and end. */
12ba150f
JB
8482 {
8483 int length = end - start;
8484
8485 if (start < 0)
8486 start = 0;
8487 else if (start > top_range)
8488 start = top_range;
8489 end = start + length;
8490
8491 if (end < start)
8492 end = start;
8493 else if (end > top_range && ! dragging)
8494 end = top_range;
8495 }
f451eb13 8496
ab648270 8497 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8498 XSETINT (bar->start, start);
8499 XSETINT (bar->end, end);
f451eb13 8500
12ba150f
JB
8501 /* Clip the end position, just for display. */
8502 if (end > top_range)
8503 end = top_range;
f451eb13 8504
ab648270 8505 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8506 below top positions, to make sure the handle is always at least
8507 that many pixels tall. */
ab648270 8508 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8509
12ba150f
JB
8510 /* Draw the empty space above the handle. Note that we can't clear
8511 zero-height areas; that means "clear to end of window." */
8512 if (0 < start)
c5e6e06b
GM
8513 x_clear_area (FRAME_X_DISPLAY (f), w,
8514 /* x, y, width, height, and exposures. */
8515 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8516 VERTICAL_SCROLL_BAR_TOP_BORDER,
8517 inside_width, start,
8518 False);
f451eb13 8519
06a2c219
GM
8520 /* Change to proper foreground color if one is specified. */
8521 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8522 XSetForeground (FRAME_X_DISPLAY (f), gc,
8523 f->output_data.x->scroll_bar_foreground_pixel);
8524
12ba150f 8525 /* Draw the handle itself. */
334208b7 8526 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8527
12ba150f 8528 /* x, y, width, height */
ab648270
JB
8529 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8530 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8531 inside_width, end - start);
f451eb13 8532
06a2c219
GM
8533 /* Restore the foreground color of the GC if we changed it above. */
8534 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8535 XSetForeground (FRAME_X_DISPLAY (f), gc,
8536 f->output_data.x->foreground_pixel);
f451eb13 8537
12ba150f
JB
8538 /* Draw the empty space below the handle. Note that we can't
8539 clear zero-height areas; that means "clear to end of window." */
8540 if (end < inside_height)
c5e6e06b
GM
8541 x_clear_area (FRAME_X_DISPLAY (f), w,
8542 /* x, y, width, height, and exposures. */
8543 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8544 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8545 inside_width, inside_height - end,
8546 False);
f451eb13 8547
f451eb13
JB
8548 }
8549
f451eb13
JB
8550 UNBLOCK_INPUT;
8551}
8552
5c187dee 8553#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8554
06a2c219
GM
8555/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8556 nil. */
58769bee 8557
12ba150f 8558static void
ab648270
JB
8559x_scroll_bar_remove (bar)
8560 struct scroll_bar *bar;
12ba150f 8561{
e83dc917 8562 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8563 BLOCK_INPUT;
8564
eccc05db 8565#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8566 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8567#else
8568 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8569#endif
06a2c219 8570
ab648270
JB
8571 /* Disassociate this scroll bar from its window. */
8572 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8573
8574 UNBLOCK_INPUT;
8575}
8576
06a2c219 8577
12ba150f
JB
8578/* Set the handle of the vertical scroll bar for WINDOW to indicate
8579 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8580 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8581 create one. */
06a2c219 8582
12ba150f 8583static void
06a2c219
GM
8584XTset_vertical_scroll_bar (w, portion, whole, position)
8585 struct window *w;
f451eb13
JB
8586 int portion, whole, position;
8587{
06a2c219 8588 struct frame *f = XFRAME (w->frame);
ab648270 8589 struct scroll_bar *bar;
3c6ede7b 8590 int top, height, left, sb_left, width, sb_width;
06a2c219 8591 int window_x, window_y, window_width, window_height;
06a2c219 8592
3c6ede7b 8593 /* Get window dimensions. */
06a2c219 8594 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8595 top = window_y;
8596 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8597 height = window_height;
06a2c219 8598
3c6ede7b 8599 /* Compute the left edge of the scroll bar area. */
06a2c219 8600 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8601 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8602 else
8603 left = XFASTINT (w->left);
8604 left *= CANON_X_UNIT (f);
8605 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8606
8607 /* Compute the width of the scroll bar which might be less than
8608 the width of the area reserved for the scroll bar. */
8609 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8610 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8611 else
3c6ede7b 8612 sb_width = width;
12ba150f 8613
3c6ede7b
GM
8614 /* Compute the left edge of the scroll bar. */
8615#ifdef USE_TOOLKIT_SCROLL_BARS
8616 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8617 sb_left = left + width - sb_width - (width - sb_width) / 2;
8618 else
8619 sb_left = left + (width - sb_width) / 2;
8620#else
8621 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8622 sb_left = left + width - sb_width;
8623 else
8624 sb_left = left;
8625#endif
8626
ab648270 8627 /* Does the scroll bar exist yet? */
06a2c219 8628 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8629 {
80c32bcc 8630 BLOCK_INPUT;
f964b4d7
GM
8631 if (width && height)
8632 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8633 left, top, width, height, False);
80c32bcc 8634 UNBLOCK_INPUT;
3c6ede7b
GM
8635 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8636 }
f451eb13 8637 else
12ba150f
JB
8638 {
8639 /* It may just need to be moved and resized. */
06a2c219
GM
8640 unsigned int mask = 0;
8641
8642 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8643
8644 BLOCK_INPUT;
8645
3c6ede7b 8646 if (sb_left != XINT (bar->left))
06a2c219 8647 mask |= CWX;
3c6ede7b 8648 if (top != XINT (bar->top))
06a2c219 8649 mask |= CWY;
3c6ede7b 8650 if (sb_width != XINT (bar->width))
06a2c219 8651 mask |= CWWidth;
3c6ede7b 8652 if (height != XINT (bar->height))
06a2c219
GM
8653 mask |= CWHeight;
8654
8655#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8656
8657 /* Since toolkit scroll bars are smaller than the space reserved
8658 for them on the frame, we have to clear "under" them. */
f964b4d7
GM
8659 if (width && height)
8660 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8661 left, top, width, height, False);
06a2c219
GM
8662
8663 /* Move/size the scroll bar widget. */
8664 if (mask)
e83dc917 8665 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8666 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8667 top,
8668 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 8669 max (height, 1), 0);
06a2c219
GM
8670
8671#else /* not USE_TOOLKIT_SCROLL_BARS */
8672
e1f6572f
RS
8673 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8674 {
8675 /* Clear areas not covered by the scroll bar. This makes sure a
8676 previous mode line display is cleared after C-x 2 C-x 1, for
8677 example. Non-toolkit scroll bars are as wide as the area
8678 reserved for scroll bars - trim at both sides. */
c5e6e06b
GM
8679 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8680 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8681 height, False);
8682 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8683 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8684 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8685 height, False);
e1f6572f 8686 }
06a2c219
GM
8687
8688 /* Move/size the scroll bar window. */
8689 if (mask)
8690 {
8691 XWindowChanges wc;
8692
3c6ede7b
GM
8693 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8694 wc.y = top;
8695 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8696 wc.height = height;
06a2c219
GM
8697 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8698 mask, &wc);
8699 }
8700
8701#endif /* not USE_TOOLKIT_SCROLL_BARS */
8702
8703 /* Remember new settings. */
3c6ede7b
GM
8704 XSETINT (bar->left, sb_left);
8705 XSETINT (bar->top, top);
8706 XSETINT (bar->width, sb_width);
8707 XSETINT (bar->height, height);
06a2c219
GM
8708
8709 UNBLOCK_INPUT;
12ba150f 8710 }
f451eb13 8711
eccc05db 8712#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8713 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8714#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8715 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8716 dragged. */
12ba150f 8717 if (NILP (bar->dragging))
f451eb13 8718 {
92857db0 8719 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8720
12ba150f 8721 if (whole == 0)
ab648270 8722 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8723 else
8724 {
43f868f5
JB
8725 int start = ((double) position * top_range) / whole;
8726 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8727 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8728 }
f451eb13 8729 }
06a2c219 8730#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8731
06a2c219 8732 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8733}
8734
12ba150f 8735
f451eb13 8736/* The following three hooks are used when we're doing a thorough
ab648270 8737 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8738 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8739 away is a real pain - "Can you say set-window-configuration, boys
8740 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8741 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8742 from the fiery pit when we actually redisplay its window. */
f451eb13 8743
ab648270
JB
8744/* Arrange for all scroll bars on FRAME to be removed at the next call
8745 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8746 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8747
58769bee 8748static void
ab648270 8749XTcondemn_scroll_bars (frame)
f451eb13
JB
8750 FRAME_PTR frame;
8751{
f9e24cb9
RS
8752 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8753 while (! NILP (FRAME_SCROLL_BARS (frame)))
8754 {
8755 Lisp_Object bar;
8756 bar = FRAME_SCROLL_BARS (frame);
8757 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8758 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8759 XSCROLL_BAR (bar)->prev = Qnil;
8760 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8761 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8762 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8763 }
f451eb13
JB
8764}
8765
fa2dfc30 8766
06a2c219 8767/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8768 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8769
f451eb13 8770static void
ab648270 8771XTredeem_scroll_bar (window)
12ba150f 8772 struct window *window;
f451eb13 8773{
ab648270 8774 struct scroll_bar *bar;
fa2dfc30 8775 struct frame *f;
12ba150f 8776
ab648270
JB
8777 /* We can't redeem this window's scroll bar if it doesn't have one. */
8778 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8779 abort ();
8780
ab648270 8781 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8782
8783 /* Unlink it from the condemned list. */
fa2dfc30
GM
8784 f = XFRAME (WINDOW_FRAME (window));
8785 if (NILP (bar->prev))
8786 {
8787 /* If the prev pointer is nil, it must be the first in one of
8788 the lists. */
8789 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8790 /* It's not condemned. Everything's fine. */
8791 return;
8792 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8793 window->vertical_scroll_bar))
8794 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8795 else
8796 /* If its prev pointer is nil, it must be at the front of
8797 one or the other! */
8798 abort ();
8799 }
8800 else
8801 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8802
fa2dfc30
GM
8803 if (! NILP (bar->next))
8804 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8805
fa2dfc30
GM
8806 bar->next = FRAME_SCROLL_BARS (f);
8807 bar->prev = Qnil;
8808 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8809 if (! NILP (bar->next))
8810 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
8811}
8812
ab648270
JB
8813/* Remove all scroll bars on FRAME that haven't been saved since the
8814 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8815
f451eb13 8816static void
ab648270 8817XTjudge_scroll_bars (f)
12ba150f 8818 FRAME_PTR f;
f451eb13 8819{
12ba150f 8820 Lisp_Object bar, next;
f451eb13 8821
ab648270 8822 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8823
8824 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8825 more events on the hapless scroll bars. */
8826 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8827
8828 for (; ! NILP (bar); bar = next)
f451eb13 8829 {
ab648270 8830 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8831
ab648270 8832 x_scroll_bar_remove (b);
12ba150f
JB
8833
8834 next = b->next;
8835 b->next = b->prev = Qnil;
f451eb13 8836 }
12ba150f 8837
ab648270 8838 /* Now there should be no references to the condemned scroll bars,
12ba150f 8839 and they should get garbage-collected. */
f451eb13
JB
8840}
8841
8842
06a2c219
GM
8843/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8844 is a no-op when using toolkit scroll bars.
ab648270
JB
8845
8846 This may be called from a signal handler, so we have to ignore GC
8847 mark bits. */
06a2c219 8848
f451eb13 8849static void
ab648270
JB
8850x_scroll_bar_expose (bar, event)
8851 struct scroll_bar *bar;
f451eb13
JB
8852 XEvent *event;
8853{
06a2c219
GM
8854#ifndef USE_TOOLKIT_SCROLL_BARS
8855
ab648270 8856 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8857 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8858 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8859 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8860
f451eb13
JB
8861 BLOCK_INPUT;
8862
ab648270 8863 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8864
06a2c219 8865 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8866 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8867
8868 /* x, y, width, height */
d9cdbb3d 8869 0, 0,
3cbd2e0b 8870 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8871 XINT (bar->height) - 1);
8872
f451eb13 8873 UNBLOCK_INPUT;
06a2c219
GM
8874
8875#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8876}
8877
ab648270
JB
8878/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8879 is set to something other than no_event, it is enqueued.
8880
8881 This may be called from a signal handler, so we have to ignore GC
8882 mark bits. */
06a2c219 8883
5c187dee
GM
8884#ifndef USE_TOOLKIT_SCROLL_BARS
8885
f451eb13 8886static void
ab648270
JB
8887x_scroll_bar_handle_click (bar, event, emacs_event)
8888 struct scroll_bar *bar;
f451eb13
JB
8889 XEvent *event;
8890 struct input_event *emacs_event;
8891{
0299d313 8892 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8893 abort ();
8894
ab648270 8895 emacs_event->kind = scroll_bar_click;
69388238 8896 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8897 emacs_event->modifiers
8898 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8899 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8900 event->xbutton.state)
8901 | (event->type == ButtonRelease
8902 ? up_modifier
8903 : down_modifier));
12ba150f 8904 emacs_event->frame_or_window = bar->window;
0f8aabe9 8905 emacs_event->arg = Qnil;
f451eb13 8906 emacs_event->timestamp = event->xbutton.time;
12ba150f 8907 {
06a2c219 8908#if 0
d9cdbb3d 8909 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8910 int internal_height
d9cdbb3d 8911 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8912#endif
0299d313 8913 int top_range
d9cdbb3d 8914 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8915 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8916
8917 if (y < 0) y = 0;
8918 if (y > top_range) y = top_range;
8919
8920 if (y < XINT (bar->start))
ab648270
JB
8921 emacs_event->part = scroll_bar_above_handle;
8922 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8923 emacs_event->part = scroll_bar_handle;
12ba150f 8924 else
ab648270 8925 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8926
8927 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8928 they want to drag it. Lisp code needs to be able to decide
8929 whether or not we're dragging. */
929787e1 8930#if 0
12ba150f
JB
8931 /* If the user has just clicked on the handle, record where they're
8932 holding it. */
8933 if (event->type == ButtonPress
ab648270 8934 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8935 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8936#endif
12ba150f
JB
8937
8938 /* If the user has released the handle, set it to its final position. */
8939 if (event->type == ButtonRelease
8940 && ! NILP (bar->dragging))
8941 {
8942 int new_start = y - XINT (bar->dragging);
8943 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8944
ab648270 8945 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8946 bar->dragging = Qnil;
8947 }
f451eb13 8948
5116f055
JB
8949 /* Same deal here as the other #if 0. */
8950#if 0
58769bee 8951 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8952 the handle. */
ab648270 8953 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8954 emacs_event->x = bar->start;
8955 else
e0c1aef2 8956 XSETINT (emacs_event->x, y);
5116f055 8957#else
e0c1aef2 8958 XSETINT (emacs_event->x, y);
5116f055 8959#endif
f451eb13 8960
e0c1aef2 8961 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8962 }
8963}
f451eb13 8964
ab648270
JB
8965/* Handle some mouse motion while someone is dragging the scroll bar.
8966
8967 This may be called from a signal handler, so we have to ignore GC
8968 mark bits. */
06a2c219 8969
f451eb13 8970static void
ab648270
JB
8971x_scroll_bar_note_movement (bar, event)
8972 struct scroll_bar *bar;
f451eb13
JB
8973 XEvent *event;
8974{
39d8bb4d
KH
8975 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8976
f451eb13
JB
8977 last_mouse_movement_time = event->xmotion.time;
8978
39d8bb4d 8979 f->mouse_moved = 1;
e0c1aef2 8980 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8981
8982 /* If we're dragging the bar, display it. */
ab648270 8983 if (! GC_NILP (bar->dragging))
f451eb13
JB
8984 {
8985 /* Where should the handle be now? */
12ba150f 8986 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8987
12ba150f 8988 if (new_start != XINT (bar->start))
f451eb13 8989 {
12ba150f 8990 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8991
ab648270 8992 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8993 }
8994 }
f451eb13
JB
8995}
8996
5c187dee
GM
8997#endif /* !USE_TOOLKIT_SCROLL_BARS */
8998
12ba150f 8999/* Return information to the user about the current position of the mouse
ab648270 9000 on the scroll bar. */
06a2c219 9001
12ba150f 9002static void
334208b7
RS
9003x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9004 FRAME_PTR *fp;
12ba150f 9005 Lisp_Object *bar_window;
ab648270 9006 enum scroll_bar_part *part;
12ba150f
JB
9007 Lisp_Object *x, *y;
9008 unsigned long *time;
9009{
ab648270 9010 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9011 Window w = SCROLL_BAR_X_WINDOW (bar);
9012 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9013 int win_x, win_y;
559cb2fb
JB
9014 Window dummy_window;
9015 int dummy_coord;
9016 unsigned int dummy_mask;
12ba150f 9017
cf7cb199
JB
9018 BLOCK_INPUT;
9019
ab648270 9020 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9021 report that. */
334208b7 9022 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9023
559cb2fb
JB
9024 /* Root, child, root x and root y. */
9025 &dummy_window, &dummy_window,
9026 &dummy_coord, &dummy_coord,
12ba150f 9027
559cb2fb
JB
9028 /* Position relative to scroll bar. */
9029 &win_x, &win_y,
12ba150f 9030
559cb2fb
JB
9031 /* Mouse buttons and modifier keys. */
9032 &dummy_mask))
7a13e894 9033 ;
559cb2fb
JB
9034 else
9035 {
06a2c219 9036#if 0
559cb2fb 9037 int inside_height
d9cdbb3d 9038 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9039#endif
559cb2fb 9040 int top_range
d9cdbb3d 9041 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9042
9043 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9044
9045 if (! NILP (bar->dragging))
9046 win_y -= XINT (bar->dragging);
9047
9048 if (win_y < 0)
9049 win_y = 0;
9050 if (win_y > top_range)
9051 win_y = top_range;
9052
334208b7 9053 *fp = f;
7a13e894 9054 *bar_window = bar->window;
559cb2fb
JB
9055
9056 if (! NILP (bar->dragging))
9057 *part = scroll_bar_handle;
9058 else if (win_y < XINT (bar->start))
9059 *part = scroll_bar_above_handle;
9060 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9061 *part = scroll_bar_handle;
9062 else
9063 *part = scroll_bar_below_handle;
12ba150f 9064
e0c1aef2
KH
9065 XSETINT (*x, win_y);
9066 XSETINT (*y, top_range);
12ba150f 9067
39d8bb4d 9068 f->mouse_moved = 0;
559cb2fb
JB
9069 last_mouse_scroll_bar = Qnil;
9070 }
12ba150f 9071
559cb2fb 9072 *time = last_mouse_movement_time;
cf7cb199 9073
cf7cb199 9074 UNBLOCK_INPUT;
12ba150f
JB
9075}
9076
f451eb13 9077
dbc4e1c1 9078/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9079 background colors, and the scroll bars may need to be redrawn.
9080 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9081 redraw them. */
9082
dfcf069d 9083void
ab648270 9084x_scroll_bar_clear (f)
dbc4e1c1
JB
9085 FRAME_PTR f;
9086{
06a2c219 9087#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9088 Lisp_Object bar;
9089
b80c363e
RS
9090 /* We can have scroll bars even if this is 0,
9091 if we just turned off scroll bar mode.
9092 But in that case we should not clear them. */
9093 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9094 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9095 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9096 XClearArea (FRAME_X_DISPLAY (f),
9097 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9098 0, 0, 0, 0, True);
06a2c219 9099#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9100}
9101
06a2c219 9102/* This processes Expose events from the menu-bar specific X event
19126e11 9103 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9104 when handling menu-bar or pop-up items. */
3afe33e7 9105
06a2c219 9106int
3afe33e7
RS
9107process_expose_from_menu (event)
9108 XEvent event;
9109{
9110 FRAME_PTR f;
19126e11 9111 struct x_display_info *dpyinfo;
06a2c219 9112 int frame_exposed_p = 0;
3afe33e7 9113
f94397b5
KH
9114 BLOCK_INPUT;
9115
19126e11
KH
9116 dpyinfo = x_display_info_for_display (event.xexpose.display);
9117 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9118 if (f)
9119 {
9120 if (f->async_visible == 0)
9121 {
9122 f->async_visible = 1;
9123 f->async_iconified = 0;
06c488fd 9124 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9125 SET_FRAME_GARBAGED (f);
9126 }
9127 else
9128 {
06a2c219
GM
9129 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9130 event.xexpose.x, event.xexpose.y,
9131 event.xexpose.width, event.xexpose.height);
9132 frame_exposed_p = 1;
3afe33e7
RS
9133 }
9134 }
9135 else
9136 {
9137 struct scroll_bar *bar
9138 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9139
3afe33e7
RS
9140 if (bar)
9141 x_scroll_bar_expose (bar, &event);
9142 }
f94397b5
KH
9143
9144 UNBLOCK_INPUT;
06a2c219 9145 return frame_exposed_p;
3afe33e7 9146}
09756a85
RS
9147\f
9148/* Define a queue to save up SelectionRequest events for later handling. */
9149
9150struct selection_event_queue
9151 {
9152 XEvent event;
9153 struct selection_event_queue *next;
9154 };
9155
9156static struct selection_event_queue *queue;
9157
9158/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9159
09756a85
RS
9160static int x_queue_selection_requests;
9161
9162/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9163
09756a85 9164static void
334208b7
RS
9165x_queue_event (f, event)
9166 FRAME_PTR f;
09756a85
RS
9167 XEvent *event;
9168{
9169 struct selection_event_queue *queue_tmp
06a2c219 9170 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9171
58769bee 9172 if (queue_tmp != NULL)
09756a85
RS
9173 {
9174 queue_tmp->event = *event;
9175 queue_tmp->next = queue;
9176 queue = queue_tmp;
9177 }
9178}
9179
9180/* Take all the queued events and put them back
9181 so that they get processed afresh. */
9182
9183static void
db3906fd
RS
9184x_unqueue_events (display)
9185 Display *display;
09756a85 9186{
58769bee 9187 while (queue != NULL)
09756a85
RS
9188 {
9189 struct selection_event_queue *queue_tmp = queue;
db3906fd 9190 XPutBackEvent (display, &queue_tmp->event);
09756a85 9191 queue = queue_tmp->next;
06a2c219 9192 xfree ((char *)queue_tmp);
09756a85
RS
9193 }
9194}
9195
9196/* Start queuing SelectionRequest events. */
9197
9198void
db3906fd
RS
9199x_start_queuing_selection_requests (display)
9200 Display *display;
09756a85
RS
9201{
9202 x_queue_selection_requests++;
9203}
9204
9205/* Stop queuing SelectionRequest events. */
9206
9207void
db3906fd
RS
9208x_stop_queuing_selection_requests (display)
9209 Display *display;
09756a85
RS
9210{
9211 x_queue_selection_requests--;
db3906fd 9212 x_unqueue_events (display);
09756a85 9213}
f451eb13
JB
9214\f
9215/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9216
06a2c219 9217/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9218 but we have to put it out here, since static variables within functions
9219 sometimes don't work. */
06a2c219 9220
dc6f92b8
JB
9221static Time enter_timestamp;
9222
11edeb03 9223/* This holds the state XLookupString needs to implement dead keys
58769bee 9224 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9225 says that a portable program can't use this, but Stephen Gildea assures
9226 me that letting the compiler initialize it to zeros will work okay.
9227
9228 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9229 given for enter_time stamp, above. */
9230
11edeb03
JB
9231static XComposeStatus compose_status;
9232
10e6549c
RS
9233/* Record the last 100 characters stored
9234 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9235
2224b905
RS
9236static int temp_index;
9237static short temp_buffer[100];
10e6549c 9238
7a13e894
RS
9239/* Set this to nonzero to fake an "X I/O error"
9240 on a particular display. */
06a2c219 9241
7a13e894
RS
9242struct x_display_info *XTread_socket_fake_io_error;
9243
2224b905
RS
9244/* When we find no input here, we occasionally do a no-op command
9245 to verify that the X server is still running and we can still talk with it.
9246 We try all the open displays, one by one.
9247 This variable is used for cycling thru the displays. */
06a2c219 9248
2224b905
RS
9249static struct x_display_info *next_noop_dpyinfo;
9250
06a2c219
GM
9251#define SET_SAVED_MENU_EVENT(size) \
9252 do \
9253 { \
9254 if (f->output_data.x->saved_menu_event == 0) \
9255 f->output_data.x->saved_menu_event \
9256 = (XEvent *) xmalloc (sizeof (XEvent)); \
9257 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9258 if (numchars >= 1) \
9259 { \
9260 bufp->kind = menu_bar_activate_event; \
9261 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9262 bufp->arg = Qnil; \
06a2c219
GM
9263 bufp++; \
9264 count++; \
9265 numchars--; \
9266 } \
9267 } \
9268 while (0)
9269
8805890a 9270#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9271#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9272
dc6f92b8
JB
9273/* Read events coming from the X server.
9274 This routine is called by the SIGIO handler.
9275 We return as soon as there are no more events to be read.
9276
9277 Events representing keys are stored in buffer BUFP,
9278 which can hold up to NUMCHARS characters.
9279 We return the number of characters stored into the buffer,
9280 thus pretending to be `read'.
9281
dc6f92b8
JB
9282 EXPECTED is nonzero if the caller knows input is available. */
9283
7c5283e4 9284int
f66868ba 9285XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9286 register int sd;
8805890a
KH
9287 /* register */ struct input_event *bufp;
9288 /* register */ int numchars;
dc6f92b8
JB
9289 int expected;
9290{
9291 int count = 0;
9292 int nbytes = 0;
dc6f92b8 9293 XEvent event;
f676886a 9294 struct frame *f;
66f55a9d 9295 int event_found = 0;
334208b7 9296 struct x_display_info *dpyinfo;
379b5ac0 9297 struct coding_system coding;
dc6f92b8 9298
9ac0d9e0 9299 if (interrupt_input_blocked)
dc6f92b8 9300 {
9ac0d9e0 9301 interrupt_input_pending = 1;
dc6f92b8
JB
9302 return -1;
9303 }
9304
9ac0d9e0 9305 interrupt_input_pending = 0;
dc6f92b8 9306 BLOCK_INPUT;
c0a04927
RS
9307
9308 /* So people can tell when we have read the available input. */
9309 input_signal_count++;
9310
dc6f92b8 9311 if (numchars <= 0)
06a2c219 9312 abort (); /* Don't think this happens. */
dc6f92b8 9313
bde5503b
GM
9314 ++handling_signal;
9315
379b5ac0
KH
9316 /* The input should be decoded if it is from XIM. Currently the
9317 locale of XIM is the same as that of the system. So, we can use
9318 Vlocale_coding_system which is initialized properly at Emacs
9319 startup time. */
9320 setup_coding_system (Vlocale_coding_system, &coding);
9321 coding.src_multibyte = 0;
9322 coding.dst_multibyte = 1;
9323 /* The input is converted to events, thus we can't handle
9324 composition. Anyway, there's no XIM that gives us composition
9325 information. */
9326 coding.composing = COMPOSITION_DISABLED;
9327
7a13e894
RS
9328 /* Find the display we are supposed to read input for.
9329 It's the one communicating on descriptor SD. */
9330 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9331 {
9332#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9333#ifdef FIOSNBIO
7a13e894
RS
9334 /* If available, Xlib uses FIOSNBIO to make the socket
9335 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9336 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9337 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9338 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9339#endif /* ! defined (FIOSNBIO) */
7a13e894 9340#endif
dc6f92b8 9341
7a13e894
RS
9342#if 0 /* This code can't be made to work, with multiple displays,
9343 and appears not to be used on any system any more.
9344 Also keyboard.c doesn't turn O_NDELAY on and off
9345 for X connections. */
dc6f92b8
JB
9346#ifndef SIGIO
9347#ifndef HAVE_SELECT
7a13e894
RS
9348 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9349 {
9350 extern int read_alarm_should_throw;
9351 read_alarm_should_throw = 1;
9352 XPeekEvent (dpyinfo->display, &event);
9353 read_alarm_should_throw = 0;
9354 }
c118dd06
JB
9355#endif /* HAVE_SELECT */
9356#endif /* SIGIO */
7a13e894 9357#endif
dc6f92b8 9358
7a13e894
RS
9359 /* For debugging, this gives a way to fake an I/O error. */
9360 if (dpyinfo == XTread_socket_fake_io_error)
9361 {
9362 XTread_socket_fake_io_error = 0;
9363 x_io_error_quitter (dpyinfo->display);
9364 }
dc6f92b8 9365
06a2c219 9366 while (XPending (dpyinfo->display))
dc6f92b8 9367 {
7a13e894 9368 XNextEvent (dpyinfo->display, &event);
06a2c219 9369
531483fb 9370#ifdef HAVE_X_I18N
d1bc4182 9371 {
f2be1146
GM
9372 /* Filter events for the current X input method.
9373 XFilterEvent returns non-zero if the input method has
9374 consumed the event. We pass the frame's X window to
9375 XFilterEvent because that's the one for which the IC
9376 was created. */
f5d11644
GM
9377 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9378 event.xclient.window);
9379 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9380 break;
9381 }
0cd6403b 9382#endif
7a13e894
RS
9383 event_found = 1;
9384
9385 switch (event.type)
9386 {
9387 case ClientMessage:
c047688c 9388 {
7a13e894
RS
9389 if (event.xclient.message_type
9390 == dpyinfo->Xatom_wm_protocols
9391 && event.xclient.format == 32)
c047688c 9392 {
7a13e894
RS
9393 if (event.xclient.data.l[0]
9394 == dpyinfo->Xatom_wm_take_focus)
c047688c 9395 {
8c1a6a84
RS
9396 /* Use x_any_window_to_frame because this
9397 could be the shell widget window
9398 if the frame has no title bar. */
9399 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9400#ifdef HAVE_X_I18N
9401 /* Not quite sure this is needed -pd */
8c1a6a84 9402 if (f && FRAME_XIC (f))
6c183ba5
RS
9403 XSetICFocus (FRAME_XIC (f));
9404#endif
f1da8f06
GM
9405#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9406 instructs the WM to set the input focus automatically for
9407 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9408 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9409 it has set the focus. So, XSetInputFocus below is not
9410 needed.
9411
9412 The call to XSetInputFocus below has also caused trouble. In
9413 cases where the XSetInputFocus done by the WM and the one
9414 below are temporally close (on a fast machine), the call
9415 below can generate additional FocusIn events which confuse
9416 Emacs. */
9417
bf7253f4
RS
9418 /* Since we set WM_TAKE_FOCUS, we must call
9419 XSetInputFocus explicitly. But not if f is null,
9420 since that might be an event for a deleted frame. */
7a13e894 9421 if (f)
bf7253f4
RS
9422 {
9423 Display *d = event.xclient.display;
9424 /* Catch and ignore errors, in case window has been
9425 iconified by a window manager such as GWM. */
9426 int count = x_catch_errors (d);
9427 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9428 /* The ICCCM says this is
9429 the only valid choice. */
9430 RevertToParent,
bf7253f4
RS
9431 event.xclient.data.l[1]);
9432 /* This is needed to detect the error
9433 if there is an error. */
9434 XSync (d, False);
9435 x_uncatch_errors (d, count);
9436 }
7a13e894 9437 /* Not certain about handling scroll bars here */
f1da8f06 9438#endif /* 0 */
c047688c 9439 }
7a13e894
RS
9440 else if (event.xclient.data.l[0]
9441 == dpyinfo->Xatom_wm_save_yourself)
9442 {
9443 /* Save state modify the WM_COMMAND property to
06a2c219 9444 something which can reinstate us. This notifies
7a13e894
RS
9445 the session manager, who's looking for such a
9446 PropertyNotify. Can restart processing when
06a2c219 9447 a keyboard or mouse event arrives. */
7a13e894
RS
9448 if (numchars > 0)
9449 {
19126e11
KH
9450 f = x_top_window_to_frame (dpyinfo,
9451 event.xclient.window);
7a13e894
RS
9452
9453 /* This is just so we only give real data once
9454 for a single Emacs process. */
b86bd3dd 9455 if (f == SELECTED_FRAME ())
7a13e894
RS
9456 XSetCommand (FRAME_X_DISPLAY (f),
9457 event.xclient.window,
9458 initial_argv, initial_argc);
f000f5c5 9459 else if (f)
7a13e894
RS
9460 XSetCommand (FRAME_X_DISPLAY (f),
9461 event.xclient.window,
9462 0, 0);
9463 }
9464 }
9465 else if (event.xclient.data.l[0]
9466 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9467 {
19126e11
KH
9468 struct frame *f
9469 = x_any_window_to_frame (dpyinfo,
9470 event.xclient.window);
1fb20991 9471
7a13e894
RS
9472 if (f)
9473 {
9474 if (numchars == 0)
9475 abort ();
1fb20991 9476
7a13e894
RS
9477 bufp->kind = delete_window_event;
9478 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9479 bufp->arg = Qnil;
7a13e894
RS
9480 bufp++;
9481
9482 count += 1;
9483 numchars -= 1;
9484 }
1fb20991 9485 }
c047688c 9486 }
7a13e894
RS
9487 else if (event.xclient.message_type
9488 == dpyinfo->Xatom_wm_configure_denied)
9489 {
9490 }
9491 else if (event.xclient.message_type
9492 == dpyinfo->Xatom_wm_window_moved)
9493 {
9494 int new_x, new_y;
19126e11
KH
9495 struct frame *f
9496 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9497
7a13e894
RS
9498 new_x = event.xclient.data.s[0];
9499 new_y = event.xclient.data.s[1];
1fb20991 9500
7a13e894
RS
9501 if (f)
9502 {
7556890b
RS
9503 f->output_data.x->left_pos = new_x;
9504 f->output_data.x->top_pos = new_y;
7a13e894 9505 }
1fb20991 9506 }
0fdff6bb 9507#ifdef HACK_EDITRES
7a13e894
RS
9508 else if (event.xclient.message_type
9509 == dpyinfo->Xatom_editres)
9510 {
19126e11
KH
9511 struct frame *f
9512 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9513 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9514 &event, NULL);
7a13e894 9515 }
0fdff6bb 9516#endif /* HACK_EDITRES */
06a2c219
GM
9517 else if ((event.xclient.message_type
9518 == dpyinfo->Xatom_DONE)
9519 || (event.xclient.message_type
9520 == dpyinfo->Xatom_PAGE))
9521 {
9522 /* Ghostview job completed. Kill it. We could
9523 reply with "Next" if we received "Page", but we
9524 currently never do because we are interested in
9525 images, only, which should have 1 page. */
06a2c219
GM
9526 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9527 struct frame *f
9528 = x_window_to_frame (dpyinfo, event.xclient.window);
9529 x_kill_gs_process (pixmap, f);
9530 expose_frame (f, 0, 0, 0, 0);
9531 }
9532#ifdef USE_TOOLKIT_SCROLL_BARS
9533 /* Scroll bar callbacks send a ClientMessage from which
9534 we construct an input_event. */
9535 else if (event.xclient.message_type
9536 == dpyinfo->Xatom_Scrollbar)
9537 {
9538 x_scroll_bar_to_input_event (&event, bufp);
9539 ++bufp, ++count, --numchars;
9540 goto out;
9541 }
9542#endif /* USE_TOOLKIT_SCROLL_BARS */
9543 else
9544 goto OTHER;
7a13e894
RS
9545 }
9546 break;
dc6f92b8 9547
7a13e894 9548 case SelectionNotify:
3afe33e7 9549#ifdef USE_X_TOOLKIT
19126e11 9550 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9551 goto OTHER;
3afe33e7 9552#endif /* not USE_X_TOOLKIT */
dfcf069d 9553 x_handle_selection_notify (&event.xselection);
7a13e894 9554 break;
d56a553a 9555
06a2c219 9556 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9557#ifdef USE_X_TOOLKIT
19126e11 9558 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9559 goto OTHER;
3afe33e7 9560#endif /* USE_X_TOOLKIT */
7a13e894
RS
9561 {
9562 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9563
7a13e894
RS
9564 if (numchars == 0)
9565 abort ();
d56a553a 9566
7a13e894
RS
9567 bufp->kind = selection_clear_event;
9568 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9569 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9570 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9571 bufp->frame_or_window = Qnil;
0f8aabe9 9572 bufp->arg = Qnil;
7a13e894 9573 bufp++;
d56a553a 9574
7a13e894
RS
9575 count += 1;
9576 numchars -= 1;
9577 }
9578 break;
dc6f92b8 9579
06a2c219 9580 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9581#ifdef USE_X_TOOLKIT
19126e11 9582 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9583 goto OTHER;
3afe33e7 9584#endif /* USE_X_TOOLKIT */
7a13e894 9585 if (x_queue_selection_requests)
19126e11 9586 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9587 &event);
9588 else
9589 {
9590 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9591
7a13e894
RS
9592 if (numchars == 0)
9593 abort ();
9594
9595 bufp->kind = selection_request_event;
9596 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9597 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9598 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9599 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9600 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9601 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9602 bufp->frame_or_window = Qnil;
0f8aabe9 9603 bufp->arg = Qnil;
7a13e894
RS
9604 bufp++;
9605
9606 count += 1;
9607 numchars -= 1;
9608 }
9609 break;
9610
9611 case PropertyNotify:
3afe33e7 9612#ifdef USE_X_TOOLKIT
19126e11 9613 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9614 goto OTHER;
3afe33e7 9615#endif /* not USE_X_TOOLKIT */
dfcf069d 9616 x_handle_property_notify (&event.xproperty);
7a13e894 9617 break;
dc6f92b8 9618
7a13e894 9619 case ReparentNotify:
19126e11 9620 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9621 if (f)
9622 {
9623 int x, y;
7556890b 9624 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9625 x_real_positions (f, &x, &y);
7556890b
RS
9626 f->output_data.x->left_pos = x;
9627 f->output_data.x->top_pos = y;
7a13e894
RS
9628 }
9629 break;
3bd330d4 9630
7a13e894 9631 case Expose:
19126e11 9632 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9633 if (f)
dc6f92b8 9634 {
7a13e894
RS
9635 if (f->async_visible == 0)
9636 {
9637 f->async_visible = 1;
9638 f->async_iconified = 0;
06c488fd 9639 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9640 SET_FRAME_GARBAGED (f);
9641 }
9642 else
06a2c219
GM
9643 expose_frame (x_window_to_frame (dpyinfo,
9644 event.xexpose.window),
9645 event.xexpose.x, event.xexpose.y,
9646 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9647 }
9648 else
7a13e894 9649 {
06a2c219
GM
9650#ifdef USE_TOOLKIT_SCROLL_BARS
9651 /* Dispatch event to the widget. */
9652 goto OTHER;
9653#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9654 struct scroll_bar *bar
9655 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9656
7a13e894
RS
9657 if (bar)
9658 x_scroll_bar_expose (bar, &event);
3afe33e7 9659#ifdef USE_X_TOOLKIT
7a13e894
RS
9660 else
9661 goto OTHER;
3afe33e7 9662#endif /* USE_X_TOOLKIT */
06a2c219 9663#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9664 }
9665 break;
dc6f92b8 9666
7a13e894
RS
9667 case GraphicsExpose: /* This occurs when an XCopyArea's
9668 source area was obscured or not
9669 available.*/
19126e11 9670 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9671 if (f)
9672 {
06a2c219
GM
9673 expose_frame (f,
9674 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9675 event.xgraphicsexpose.width,
9676 event.xgraphicsexpose.height);
7a13e894 9677 }
3afe33e7 9678#ifdef USE_X_TOOLKIT
7a13e894
RS
9679 else
9680 goto OTHER;
3afe33e7 9681#endif /* USE_X_TOOLKIT */
7a13e894 9682 break;
dc6f92b8 9683
7a13e894 9684 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9685 source area was completely
9686 available */
7a13e894 9687 break;
dc6f92b8 9688
7a13e894 9689 case UnmapNotify:
06a2c219
GM
9690 /* Redo the mouse-highlight after the tooltip has gone. */
9691 if (event.xmap.window == tip_window)
9692 {
9693 tip_window = 0;
9694 redo_mouse_highlight ();
9695 }
9696
91ea2a7a 9697 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9698 if (f) /* F may no longer exist if
9699 the frame was deleted. */
9700 {
9701 /* While a frame is unmapped, display generation is
9702 disabled; you don't want to spend time updating a
9703 display that won't ever be seen. */
9704 f->async_visible = 0;
9705 /* We can't distinguish, from the event, whether the window
9706 has become iconified or invisible. So assume, if it
9707 was previously visible, than now it is iconified.
1aa6072f
RS
9708 But x_make_frame_invisible clears both
9709 the visible flag and the iconified flag;
9710 and that way, we know the window is not iconified now. */
7a13e894 9711 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9712 {
9713 f->async_iconified = 1;
bddd097c 9714
1aa6072f
RS
9715 bufp->kind = iconify_event;
9716 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9717 bufp->arg = Qnil;
1aa6072f
RS
9718 bufp++;
9719 count++;
9720 numchars--;
9721 }
7a13e894 9722 }
7a13e894 9723 goto OTHER;
dc6f92b8 9724
7a13e894 9725 case MapNotify:
06a2c219
GM
9726 if (event.xmap.window == tip_window)
9727 /* The tooltip has been drawn already. Avoid
9728 the SET_FRAME_GARBAGED below. */
9729 goto OTHER;
9730
9731 /* We use x_top_window_to_frame because map events can
9732 come for sub-windows and they don't mean that the
9733 frame is visible. */
19126e11 9734 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9735 if (f)
9736 {
9737 f->async_visible = 1;
9738 f->async_iconified = 0;
06c488fd 9739 f->output_data.x->has_been_visible = 1;
dc6f92b8 9740
7a13e894
RS
9741 /* wait_reading_process_input will notice this and update
9742 the frame's display structures. */
9743 SET_FRAME_GARBAGED (f);
bddd097c 9744
d806e720
RS
9745 if (f->iconified)
9746 {
9747 bufp->kind = deiconify_event;
9748 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9749 bufp->arg = Qnil;
d806e720
RS
9750 bufp++;
9751 count++;
9752 numchars--;
9753 }
e73ec6fa 9754 else if (! NILP (Vframe_list)
8e713be6 9755 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9756 /* Force a redisplay sooner or later
9757 to update the frame titles
9758 in case this is the second frame. */
9759 record_asynch_buffer_change ();
7a13e894 9760 }
7a13e894 9761 goto OTHER;
dc6f92b8 9762
7a13e894 9763 case KeyPress:
19126e11 9764 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9765
eccc05db 9766#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9767 /* I couldn't find a way to prevent LessTif scroll bars
9768 from consuming key events. */
9769 if (f == 0)
9770 {
9771 Widget widget = XtWindowToWidget (dpyinfo->display,
9772 event.xkey.window);
9773 if (widget && XmIsScrollBar (widget))
9774 {
9775 widget = XtParent (widget);
9776 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9777 }
9778 }
eccc05db 9779#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9780
7a13e894
RS
9781 if (f != 0)
9782 {
9783 KeySym keysym, orig_keysym;
379b5ac0
KH
9784 /* al%imercury@uunet.uu.net says that making this 81
9785 instead of 80 fixed a bug whereby meta chars made
9786 his Emacs hang.
9787
9788 It seems that some version of XmbLookupString has
9789 a bug of not returning XBufferOverflow in
9790 status_return even if the input is too long to
9791 fit in 81 bytes. So, we must prepare sufficient
9792 bytes for copy_buffer. 513 bytes (256 chars for
9793 two-byte character set) seems to be a faily good
9794 approximation. -- 2000.8.10 handa@etl.go.jp */
9795 unsigned char copy_buffer[513];
9796 unsigned char *copy_bufptr = copy_buffer;
9797 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9798 int modifiers;
64bb1782 9799
7a13e894
RS
9800 event.xkey.state
9801 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9802 extra_keyboard_modifiers);
9803 modifiers = event.xkey.state;
3a2712f9 9804
7a13e894 9805 /* This will have to go some day... */
752a043f 9806
7a13e894
RS
9807 /* make_lispy_event turns chars into control chars.
9808 Don't do it here because XLookupString is too eager. */
9809 event.xkey.state &= ~ControlMask;
5d46f928
RS
9810 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9811 | dpyinfo->super_mod_mask
9812 | dpyinfo->hyper_mod_mask
9813 | dpyinfo->alt_mod_mask);
9814
1cf4a0d1
RS
9815 /* In case Meta is ComposeCharacter,
9816 clear its status. According to Markus Ehrnsperger
9817 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9818 this enables ComposeCharacter to work whether or
9819 not it is combined with Meta. */
9820 if (modifiers & dpyinfo->meta_mod_mask)
9821 bzero (&compose_status, sizeof (compose_status));
9822
6c183ba5
RS
9823#ifdef HAVE_X_I18N
9824 if (FRAME_XIC (f))
9825 {
f5d11644
GM
9826 Status status_return;
9827
6c183ba5 9828 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9829 &event.xkey, copy_bufptr,
9830 copy_bufsiz, &keysym,
6c183ba5 9831 &status_return);
f5d11644
GM
9832 if (status_return == XBufferOverflow)
9833 {
9834 copy_bufsiz = nbytes + 1;
9835 copy_bufptr = (char *) alloca (copy_bufsiz);
9836 nbytes = XmbLookupString (FRAME_XIC (f),
9837 &event.xkey, copy_bufptr,
9838 copy_bufsiz, &keysym,
9839 &status_return);
9840 }
9841
1decb680
PE
9842 if (status_return == XLookupNone)
9843 break;
9844 else if (status_return == XLookupChars)
fdd9d55e
GM
9845 {
9846 keysym = NoSymbol;
9847 modifiers = 0;
9848 }
1decb680
PE
9849 else if (status_return != XLookupKeySym
9850 && status_return != XLookupBoth)
9851 abort ();
6c183ba5
RS
9852 }
9853 else
379b5ac0
KH
9854 nbytes = XLookupString (&event.xkey, copy_bufptr,
9855 copy_bufsiz, &keysym,
9856 &compose_status);
6c183ba5 9857#else
379b5ac0
KH
9858 nbytes = XLookupString (&event.xkey, copy_bufptr,
9859 copy_bufsiz, &keysym,
9860 &compose_status);
6c183ba5 9861#endif
dc6f92b8 9862
7a13e894 9863 orig_keysym = keysym;
55123275 9864
7a13e894
RS
9865 if (numchars > 1)
9866 {
9867 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9868 || keysym == XK_Delete
1097aea0 9869#ifdef XK_ISO_Left_Tab
441affdb 9870 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9871#endif
852bff8f 9872 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9873 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9874 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9875#ifdef HPUX
7a13e894
RS
9876 /* This recognizes the "extended function keys".
9877 It seems there's no cleaner way.
9878 Test IsModifierKey to avoid handling mode_switch
9879 incorrectly. */
9880 || ((unsigned) (keysym) >= XK_Select
9881 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9882#endif
9883#ifdef XK_dead_circumflex
7a13e894 9884 || orig_keysym == XK_dead_circumflex
69388238
RS
9885#endif
9886#ifdef XK_dead_grave
7a13e894 9887 || orig_keysym == XK_dead_grave
69388238
RS
9888#endif
9889#ifdef XK_dead_tilde
7a13e894 9890 || orig_keysym == XK_dead_tilde
69388238
RS
9891#endif
9892#ifdef XK_dead_diaeresis
7a13e894 9893 || orig_keysym == XK_dead_diaeresis
69388238
RS
9894#endif
9895#ifdef XK_dead_macron
7a13e894 9896 || orig_keysym == XK_dead_macron
69388238
RS
9897#endif
9898#ifdef XK_dead_degree
7a13e894 9899 || orig_keysym == XK_dead_degree
69388238
RS
9900#endif
9901#ifdef XK_dead_acute
7a13e894 9902 || orig_keysym == XK_dead_acute
69388238
RS
9903#endif
9904#ifdef XK_dead_cedilla
7a13e894 9905 || orig_keysym == XK_dead_cedilla
69388238
RS
9906#endif
9907#ifdef XK_dead_breve
7a13e894 9908 || orig_keysym == XK_dead_breve
69388238
RS
9909#endif
9910#ifdef XK_dead_ogonek
7a13e894 9911 || orig_keysym == XK_dead_ogonek
69388238
RS
9912#endif
9913#ifdef XK_dead_caron
7a13e894 9914 || orig_keysym == XK_dead_caron
69388238
RS
9915#endif
9916#ifdef XK_dead_doubleacute
7a13e894 9917 || orig_keysym == XK_dead_doubleacute
69388238
RS
9918#endif
9919#ifdef XK_dead_abovedot
7a13e894 9920 || orig_keysym == XK_dead_abovedot
c34790e0 9921#endif
7a13e894
RS
9922 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9923 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9924 /* Any "vendor-specific" key is ok. */
9925 || (orig_keysym & (1 << 28)))
9926 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9927#ifndef HAVE_X11R5
9928#ifdef XK_Mode_switch
7a13e894 9929 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9930#endif
9931#ifdef XK_Num_Lock
7a13e894 9932 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9933#endif
9934#endif /* not HAVE_X11R5 */
7a13e894 9935 ))
dc6f92b8 9936 {
10e6549c
RS
9937 if (temp_index == sizeof temp_buffer / sizeof (short))
9938 temp_index = 0;
7a13e894
RS
9939 temp_buffer[temp_index++] = keysym;
9940 bufp->kind = non_ascii_keystroke;
9941 bufp->code = keysym;
e0c1aef2 9942 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9943 bufp->arg = Qnil;
334208b7
RS
9944 bufp->modifiers
9945 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9946 modifiers);
1113d9db 9947 bufp->timestamp = event.xkey.time;
dc6f92b8 9948 bufp++;
7a13e894
RS
9949 count++;
9950 numchars--;
dc6f92b8 9951 }
7a13e894
RS
9952 else if (numchars > nbytes)
9953 {
9954 register int i;
379b5ac0 9955 register int c;
379b5ac0 9956 int nchars, len;
7a13e894
RS
9957
9958 for (i = 0; i < nbytes; i++)
9959 {
379b5ac0
KH
9960 if (temp_index == (sizeof temp_buffer
9961 / sizeof (short)))
7a13e894 9962 temp_index = 0;
379b5ac0
KH
9963 temp_buffer[temp_index++] = copy_bufptr[i];
9964 }
9965
9966 if (/* If the event is not from XIM, */
9967 event.xkey.keycode != 0
9968 /* or the current locale doesn't request
9969 decoding of the intup data, ... */
9970 || coding.type == coding_type_raw_text
9971 || coding.type == coding_type_no_conversion)
9972 {
9973 /* ... we can use the input data as is. */
9974 nchars = nbytes;
9975 }
9976 else
9977 {
9978 /* We have to decode the input data. */
9979 int require;
9980 unsigned char *p;
9981
9982 require = decoding_buffer_size (&coding, nbytes);
9983 p = (unsigned char *) alloca (require);
9984 coding.mode |= CODING_MODE_LAST_BLOCK;
9985 decode_coding (&coding, copy_bufptr, p,
9986 nbytes, require);
9987 nbytes = coding.produced;
9988 nchars = coding.produced_char;
9989 copy_bufptr = p;
9990 }
9991
9992 /* Convert the input data to a sequence of
9993 character events. */
9994 for (i = 0; i < nbytes; i += len)
9995 {
9996 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9997 nbytes - i, len);
9998 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9999 ? ascii_keystroke
10000 : multibyte_char_keystroke);
10001 bufp->code = c;
7a13e894 10002 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10003 bufp->arg = Qnil;
7a13e894
RS
10004 bufp->modifiers
10005 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10006 modifiers);
10007 bufp->timestamp = event.xkey.time;
10008 bufp++;
10009 }
10010
379b5ac0
KH
10011 count += nchars;
10012 numchars -= nchars;
1decb680
PE
10013
10014 if (keysym == NoSymbol)
10015 break;
7a13e894
RS
10016 }
10017 else
10018 abort ();
dc6f92b8 10019 }
10e6549c
RS
10020 else
10021 abort ();
dc6f92b8 10022 }
59ddecde
GM
10023#ifdef HAVE_X_I18N
10024 /* Don't dispatch this event since XtDispatchEvent calls
10025 XFilterEvent, and two calls in a row may freeze the
10026 client. */
10027 break;
10028#else
717ca130 10029 goto OTHER;
59ddecde 10030#endif
f451eb13 10031
f5d11644 10032 case KeyRelease:
59ddecde
GM
10033#ifdef HAVE_X_I18N
10034 /* Don't dispatch this event since XtDispatchEvent calls
10035 XFilterEvent, and two calls in a row may freeze the
10036 client. */
10037 break;
10038#else
f5d11644 10039 goto OTHER;
59ddecde 10040#endif
f5d11644 10041
7a13e894 10042 /* Here's a possible interpretation of the whole
06a2c219
GM
10043 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10044 you get a FocusIn event, you have to get a FocusOut
10045 event before you relinquish the focus. If you
10046 haven't received a FocusIn event, then a mere
10047 LeaveNotify is enough to free you. */
f451eb13 10048
7a13e894 10049 case EnterNotify:
06a2c219
GM
10050 {
10051 int from_menu_bar_p = 0;
10052
10053 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10054
10055#ifdef LESSTIF_VERSION
10056 /* When clicking outside of a menu bar popup to close
10057 it, we get a FocusIn/ EnterNotify sequence of
10058 events. The flag event.xcrossing.focus is not set
10059 in the EnterNotify event of that sequence because
10060 the focus is in the menu bar,
10061 event.xcrossing.window is the frame's X window.
10062 Unconditionally setting the focus frame to null in
10063 this case is not the right thing, because no event
10064 follows that could set the focus frame to the right
10065 value.
10066
10067 This could be a LessTif bug, but I wasn't able to
10068 reproduce the behavior in a simple test program.
3be7e417
GM
10069 On the other hand, Motif seems to not have this
10070 problem.
06a2c219 10071
3be7e417 10072 (gerd, LessTif 0.92). */
06a2c219
GM
10073
10074 if (!event.xcrossing.focus
10075 && f
10076 && f->output_data.x->menubar_widget)
10077 {
10078 Window focus;
10079 int revert;
10080
10081 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10082 if (focus == XtWindow (f->output_data.x->menubar_widget))
10083 from_menu_bar_p = 1;
10084 }
10085#endif /* LESSTIF_VERSION */
6d4238f3 10086
06a2c219
GM
10087 if (event.xcrossing.focus || from_menu_bar_p)
10088 {
10089 /* Avoid nasty pop/raise loops. */
10090 if (f && (!(f->auto_raise)
10091 || !(f->auto_lower)
10092 || (event.xcrossing.time - enter_timestamp) > 500))
10093 {
10094 x_new_focus_frame (dpyinfo, f);
10095 enter_timestamp = event.xcrossing.time;
10096 }
10097 }
10098 else if (f == dpyinfo->x_focus_frame)
10099 x_new_focus_frame (dpyinfo, 0);
10100
10101 /* EnterNotify counts as mouse movement,
10102 so update things that depend on mouse position. */
2533c408 10103 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10104 note_mouse_movement (f, &event.xmotion);
10105 goto OTHER;
10106 }
dc6f92b8 10107
7a13e894 10108 case FocusIn:
19126e11 10109 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10110 if (event.xfocus.detail != NotifyPointer)
0f941935 10111 dpyinfo->x_focus_event_frame = f;
7a13e894 10112 if (f)
eb72635f
GM
10113 {
10114 x_new_focus_frame (dpyinfo, f);
10115
10116 /* Don't stop displaying the initial startup message
10117 for a switch-frame event we don't need. */
10118 if (GC_NILP (Vterminal_frame)
10119 && GC_CONSP (Vframe_list)
10120 && !GC_NILP (XCDR (Vframe_list)))
10121 {
10122 bufp->kind = FOCUS_IN_EVENT;
10123 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10124 bufp->arg = Qnil;
eb72635f
GM
10125 ++bufp, ++count, --numchars;
10126 }
10127 }
f9e24cb9 10128
6c183ba5
RS
10129#ifdef HAVE_X_I18N
10130 if (f && FRAME_XIC (f))
10131 XSetICFocus (FRAME_XIC (f));
10132#endif
10133
7a13e894 10134 goto OTHER;
10c5e63d 10135
7a13e894 10136 case LeaveNotify:
19126e11 10137 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10138 if (f)
10c5e63d 10139 {
06a2c219
GM
10140 int from_menu_bar_p = 0;
10141
7a13e894 10142 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10143 {
10144 /* If we move outside the frame, then we're
10145 certainly no longer on any text in the frame. */
10146 clear_mouse_face (dpyinfo);
10147 dpyinfo->mouse_face_mouse_frame = 0;
10148 }
10149
10150 /* Generate a nil HELP_EVENT to cancel a help-echo.
10151 Do it only if there's something to cancel.
10152 Otherwise, the startup message is cleared when
10153 the mouse leaves the frame. */
10154 if (any_help_event_p)
10155 {
be010514
GM
10156 Lisp_Object frame;
10157 int n;
10158
06a2c219 10159 XSETFRAME (frame, f);
82c5d67a 10160 help_echo = Qnil;
5ab2570d
GM
10161 n = gen_help_event (bufp, numchars,
10162 Qnil, frame, Qnil, Qnil, 0);
be010514 10163 bufp += n, count += n, numchars -= n;
06a2c219 10164 }
7a13e894 10165
06a2c219
GM
10166#ifdef LESSTIF_VERSION
10167 /* Please see the comment at the start of the
10168 EnterNotify case. */
10169 if (!event.xcrossing.focus
10170 && f->output_data.x->menubar_widget)
10171 {
10172 Window focus;
10173 int revert;
10174 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10175 if (focus == XtWindow (f->output_data.x->menubar_widget))
10176 from_menu_bar_p = 1;
10177 }
10178#endif /* LESSTIF_VERSION */
10179
10180 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10181 x_mouse_leave (dpyinfo);
10c5e63d 10182 else
7a13e894 10183 {
0f941935
KH
10184 if (f == dpyinfo->x_focus_event_frame)
10185 dpyinfo->x_focus_event_frame = 0;
10186 if (f == dpyinfo->x_focus_frame)
10187 x_new_focus_frame (dpyinfo, 0);
7a13e894 10188 }
10c5e63d 10189 }
7a13e894 10190 goto OTHER;
dc6f92b8 10191
7a13e894 10192 case FocusOut:
19126e11 10193 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10194 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10195 && f == dpyinfo->x_focus_event_frame)
10196 dpyinfo->x_focus_event_frame = 0;
10197 if (f && f == dpyinfo->x_focus_frame)
10198 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10199
6c183ba5
RS
10200#ifdef HAVE_X_I18N
10201 if (f && FRAME_XIC (f))
10202 XUnsetICFocus (FRAME_XIC (f));
10203#endif
10204
7a13e894 10205 goto OTHER;
dc6f92b8 10206
7a13e894 10207 case MotionNotify:
dc6f92b8 10208 {
06a2c219 10209 previous_help_echo = help_echo;
7cea38bc 10210 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10211 help_echo_pos = -1;
06a2c219 10212
7a13e894
RS
10213 if (dpyinfo->grabbed && last_mouse_frame
10214 && FRAME_LIVE_P (last_mouse_frame))
10215 f = last_mouse_frame;
10216 else
19126e11 10217 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10218
7a13e894
RS
10219 if (f)
10220 note_mouse_movement (f, &event.xmotion);
10221 else
10222 {
e88b3c50 10223#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10224 struct scroll_bar *bar
10225 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10226
7a13e894
RS
10227 if (bar)
10228 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10229#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10230
06a2c219
GM
10231 /* If we move outside the frame, then we're
10232 certainly no longer on any text in the frame. */
7a13e894
RS
10233 clear_mouse_face (dpyinfo);
10234 }
06a2c219
GM
10235
10236 /* If the contents of the global variable help_echo
10237 has changed, generate a HELP_EVENT. */
b7e80413
SM
10238 if (!NILP (help_echo)
10239 || !NILP (previous_help_echo))
06a2c219
GM
10240 {
10241 Lisp_Object frame;
be010514 10242 int n;
06a2c219
GM
10243
10244 if (f)
10245 XSETFRAME (frame, f);
10246 else
10247 frame = Qnil;
10248
10249 any_help_event_p = 1;
5ab2570d 10250 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10251 help_echo_window, help_echo_object,
10252 help_echo_pos);
be010514 10253 bufp += n, count += n, numchars -= n;
06a2c219
GM
10254 }
10255
10256 goto OTHER;
dc6f92b8 10257 }
dc6f92b8 10258
7a13e894 10259 case ConfigureNotify:
9829ddba
RS
10260 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10261 if (f)
af395ec1 10262 {
5c187dee 10263#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10264 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10265 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10266
2d7fc7e8
RS
10267 /* In the toolkit version, change_frame_size
10268 is called by the code that handles resizing
10269 of the EmacsFrame widget. */
7a13e894 10270
7a13e894
RS
10271 /* Even if the number of character rows and columns has
10272 not changed, the font size may have changed, so we need
10273 to check the pixel dimensions as well. */
10274 if (columns != f->width
10275 || rows != f->height
7556890b
RS
10276 || event.xconfigure.width != f->output_data.x->pixel_width
10277 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10278 {
7d1e984f 10279 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10280 SET_FRAME_GARBAGED (f);
e687d06e 10281 cancel_mouse_face (f);
7a13e894 10282 }
2d7fc7e8 10283#endif
af395ec1 10284
7556890b
RS
10285 f->output_data.x->pixel_width = event.xconfigure.width;
10286 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10287
10288 /* What we have now is the position of Emacs's own window.
10289 Convert that to the position of the window manager window. */
dcb07ae9
RS
10290 x_real_positions (f, &f->output_data.x->left_pos,
10291 &f->output_data.x->top_pos);
10292
f5d11644
GM
10293#ifdef HAVE_X_I18N
10294 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10295 xic_set_statusarea (f);
10296#endif
10297
dcb07ae9
RS
10298 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10299 {
10300 /* Since the WM decorations come below top_pos now,
10301 we must put them below top_pos in the future. */
10302 f->output_data.x->win_gravity = NorthWestGravity;
10303 x_wm_set_size_hint (f, (long) 0, 0);
10304 }
8f08dc93
KH
10305#ifdef USE_MOTIF
10306 /* Some window managers pass (0,0) as the location of
10307 the window, and the Motif event handler stores it
10308 in the emacs widget, which messes up Motif menus. */
10309 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10310 {
10311 event.xconfigure.x = f->output_data.x->widget->core.x;
10312 event.xconfigure.y = f->output_data.x->widget->core.y;
10313 }
06a2c219 10314#endif /* USE_MOTIF */
7a13e894 10315 }
2d7fc7e8 10316 goto OTHER;
dc6f92b8 10317
7a13e894
RS
10318 case ButtonPress:
10319 case ButtonRelease:
10320 {
10321 /* If we decide we want to generate an event to be seen
10322 by the rest of Emacs, we put it here. */
10323 struct input_event emacs_event;
9ea173e8 10324 int tool_bar_p = 0;
06a2c219 10325
7a13e894 10326 emacs_event.kind = no_event;
7a13e894 10327 bzero (&compose_status, sizeof (compose_status));
9b07615b 10328
06a2c219
GM
10329 if (dpyinfo->grabbed
10330 && last_mouse_frame
9f67f20b
RS
10331 && FRAME_LIVE_P (last_mouse_frame))
10332 f = last_mouse_frame;
10333 else
2224b905 10334 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10335
06a2c219
GM
10336 if (f)
10337 {
9ea173e8
GM
10338 /* Is this in the tool-bar? */
10339 if (WINDOWP (f->tool_bar_window)
10340 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10341 {
10342 Lisp_Object window;
10343 int p, x, y;
10344
10345 x = event.xbutton.x;
10346 y = event.xbutton.y;
10347
10348 /* Set x and y. */
10349 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10350 if (EQ (window, f->tool_bar_window))
06a2c219 10351 {
9ea173e8
GM
10352 x_handle_tool_bar_click (f, &event.xbutton);
10353 tool_bar_p = 1;
06a2c219
GM
10354 }
10355 }
10356
9ea173e8 10357 if (!tool_bar_p)
06a2c219
GM
10358 if (!dpyinfo->x_focus_frame
10359 || f == dpyinfo->x_focus_frame)
10360 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10361 }
10362 else
10363 {
06a2c219 10364#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10365 struct scroll_bar *bar
10366 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10367
7a13e894
RS
10368 if (bar)
10369 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10370#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10371 }
10372
10373 if (event.type == ButtonPress)
10374 {
10375 dpyinfo->grabbed |= (1 << event.xbutton.button);
10376 last_mouse_frame = f;
edad46f6
KH
10377 /* Ignore any mouse motion that happened
10378 before this event; any subsequent mouse-movement
10379 Emacs events should reflect only motion after
10380 the ButtonPress. */
a00e91cd
KH
10381 if (f != 0)
10382 f->mouse_moved = 0;
06a2c219 10383
9ea173e8
GM
10384 if (!tool_bar_p)
10385 last_tool_bar_item = -1;
7a13e894 10386 }
3afe33e7
RS
10387 else
10388 {
7a13e894 10389 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10390 }
23faf38f 10391
7a13e894
RS
10392 if (numchars >= 1 && emacs_event.kind != no_event)
10393 {
10394 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10395 bufp++;
10396 count++;
10397 numchars--;
10398 }
3afe33e7
RS
10399
10400#ifdef USE_X_TOOLKIT
2224b905
RS
10401 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10402 /* For a down-event in the menu bar,
10403 don't pass it to Xt right now.
10404 Instead, save it away
10405 and we will pass it to Xt from kbd_buffer_get_event.
10406 That way, we can run some Lisp code first. */
91375f8f
RS
10407 if (f && event.type == ButtonPress
10408 /* Verify the event is really within the menu bar
10409 and not just sent to it due to grabbing. */
10410 && event.xbutton.x >= 0
10411 && event.xbutton.x < f->output_data.x->pixel_width
10412 && event.xbutton.y >= 0
10413 && event.xbutton.y < f->output_data.x->menubar_height
10414 && event.xbutton.same_screen)
2224b905 10415 {
8805890a 10416 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10417 XSETFRAME (last_mouse_press_frame, f);
10418 }
10419 else if (event.type == ButtonPress)
10420 {
10421 last_mouse_press_frame = Qnil;
30e671c3 10422 goto OTHER;
ce89ef46 10423 }
06a2c219 10424
2237cac9
RS
10425#ifdef USE_MOTIF /* This should do not harm for Lucid,
10426 but I am trying to be cautious. */
ce89ef46
RS
10427 else if (event.type == ButtonRelease)
10428 {
2237cac9 10429 if (!NILP (last_mouse_press_frame))
f10ded1c 10430 {
2237cac9
RS
10431 f = XFRAME (last_mouse_press_frame);
10432 if (f->output_data.x)
06a2c219 10433 SET_SAVED_BUTTON_EVENT;
f10ded1c 10434 }
06a2c219 10435 else
30e671c3 10436 goto OTHER;
2224b905 10437 }
2237cac9 10438#endif /* USE_MOTIF */
2224b905
RS
10439 else
10440 goto OTHER;
3afe33e7 10441#endif /* USE_X_TOOLKIT */
7a13e894
RS
10442 }
10443 break;
dc6f92b8 10444
7a13e894 10445 case CirculateNotify:
06a2c219
GM
10446 goto OTHER;
10447
7a13e894 10448 case CirculateRequest:
06a2c219
GM
10449 goto OTHER;
10450
10451 case VisibilityNotify:
10452 goto OTHER;
dc6f92b8 10453
7a13e894
RS
10454 case MappingNotify:
10455 /* Someone has changed the keyboard mapping - update the
10456 local cache. */
10457 switch (event.xmapping.request)
10458 {
10459 case MappingModifier:
10460 x_find_modifier_meanings (dpyinfo);
10461 /* This is meant to fall through. */
10462 case MappingKeyboard:
10463 XRefreshKeyboardMapping (&event.xmapping);
10464 }
7a13e894 10465 goto OTHER;
dc6f92b8 10466
7a13e894 10467 default:
7a13e894 10468 OTHER:
717ca130 10469#ifdef USE_X_TOOLKIT
7a13e894
RS
10470 BLOCK_INPUT;
10471 XtDispatchEvent (&event);
10472 UNBLOCK_INPUT;
3afe33e7 10473#endif /* USE_X_TOOLKIT */
7a13e894
RS
10474 break;
10475 }
dc6f92b8
JB
10476 }
10477 }
10478
06a2c219
GM
10479 out:;
10480
9a5196d0
RS
10481 /* On some systems, an X bug causes Emacs to get no more events
10482 when the window is destroyed. Detect that. (1994.) */
58769bee 10483 if (! event_found)
ef2a22d0 10484 {
ef2a22d0
RS
10485 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10486 One XNOOP in 100 loops will make Emacs terminate.
10487 B. Bretthauer, 1994 */
10488 x_noop_count++;
58769bee 10489 if (x_noop_count >= 100)
ef2a22d0
RS
10490 {
10491 x_noop_count=0;
2224b905
RS
10492
10493 if (next_noop_dpyinfo == 0)
10494 next_noop_dpyinfo = x_display_list;
10495
10496 XNoOp (next_noop_dpyinfo->display);
10497
10498 /* Each time we get here, cycle through the displays now open. */
10499 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10500 }
10501 }
502add23 10502
06a2c219 10503 /* If the focus was just given to an auto-raising frame,
0134a210 10504 raise it now. */
7a13e894 10505 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10506 if (pending_autoraise_frame)
10507 {
10508 x_raise_frame (pending_autoraise_frame);
10509 pending_autoraise_frame = 0;
10510 }
0134a210 10511
dc6f92b8 10512 UNBLOCK_INPUT;
bde5503b 10513 --handling_signal;
dc6f92b8
JB
10514 return count;
10515}
06a2c219
GM
10516
10517
10518
dc6f92b8 10519\f
06a2c219
GM
10520/***********************************************************************
10521 Text Cursor
10522 ***********************************************************************/
10523
10524/* Note if the text cursor of window W has been overwritten by a
10525 drawing operation that outputs N glyphs starting at HPOS in the
10526 line given by output_cursor.vpos. N < 0 means all the rest of the
10527 line after HPOS has been written. */
10528
10529static void
10530note_overwritten_text_cursor (w, hpos, n)
10531 struct window *w;
10532 int hpos, n;
10533{
10534 if (updated_area == TEXT_AREA
10535 && output_cursor.vpos == w->phys_cursor.vpos
10536 && hpos <= w->phys_cursor.hpos
10537 && (n < 0
10538 || hpos + n > w->phys_cursor.hpos))
10539 w->phys_cursor_on_p = 0;
10540}
f451eb13
JB
10541
10542
06a2c219
GM
10543/* Set clipping for output in glyph row ROW. W is the window in which
10544 we operate. GC is the graphics context to set clipping in.
10545 WHOLE_LINE_P non-zero means include the areas used for truncation
10546 mark display and alike in the clipping rectangle.
10547
10548 ROW may be a text row or, e.g., a mode line. Text rows must be
10549 clipped to the interior of the window dedicated to text display,
10550 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10551
10552static void
06a2c219
GM
10553x_clip_to_row (w, row, gc, whole_line_p)
10554 struct window *w;
10555 struct glyph_row *row;
10556 GC gc;
10557 int whole_line_p;
dc6f92b8 10558{
06a2c219
GM
10559 struct frame *f = XFRAME (WINDOW_FRAME (w));
10560 XRectangle clip_rect;
10561 int window_x, window_y, window_width, window_height;
dc6f92b8 10562
06a2c219 10563 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10564
06a2c219
GM
10565 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10566 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10567 clip_rect.y = max (clip_rect.y, window_y);
10568 clip_rect.width = window_width;
10569 clip_rect.height = row->visible_height;
5c1aae96 10570
06a2c219
GM
10571 /* If clipping to the whole line, including trunc marks, extend
10572 the rectangle to the left and increase its width. */
10573 if (whole_line_p)
10574 {
110859fc
GM
10575 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10576 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10577 }
5c1aae96 10578
06a2c219 10579 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10580}
10581
06a2c219
GM
10582
10583/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10584
10585static void
06a2c219
GM
10586x_draw_hollow_cursor (w, row)
10587 struct window *w;
10588 struct glyph_row *row;
dc6f92b8 10589{
06a2c219
GM
10590 struct frame *f = XFRAME (WINDOW_FRAME (w));
10591 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10592 Display *dpy = FRAME_X_DISPLAY (f);
10593 int x, y, wd, h;
10594 XGCValues xgcv;
10595 struct glyph *cursor_glyph;
10596 GC gc;
10597
10598 /* Compute frame-relative coordinates from window-relative
10599 coordinates. */
10600 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10601 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10602 + row->ascent - w->phys_cursor_ascent);
10603 h = row->height - 1;
10604
10605 /* Get the glyph the cursor is on. If we can't tell because
10606 the current matrix is invalid or such, give up. */
10607 cursor_glyph = get_phys_cursor_glyph (w);
10608 if (cursor_glyph == NULL)
dc6f92b8
JB
10609 return;
10610
06a2c219
GM
10611 /* Compute the width of the rectangle to draw. If on a stretch
10612 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10613 rectangle as wide as the glyph, but use a canonical character
10614 width instead. */
10615 wd = cursor_glyph->pixel_width - 1;
10616 if (cursor_glyph->type == STRETCH_GLYPH
10617 && !x_stretch_cursor_p)
10618 wd = min (CANON_X_UNIT (f), wd);
10619
10620 /* The foreground of cursor_gc is typically the same as the normal
10621 background color, which can cause the cursor box to be invisible. */
10622 xgcv.foreground = f->output_data.x->cursor_pixel;
10623 if (dpyinfo->scratch_cursor_gc)
10624 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10625 else
10626 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10627 GCForeground, &xgcv);
10628 gc = dpyinfo->scratch_cursor_gc;
10629
10630 /* Set clipping, draw the rectangle, and reset clipping again. */
10631 x_clip_to_row (w, row, gc, 0);
10632 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10633 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10634}
10635
06a2c219
GM
10636
10637/* Draw a bar cursor on window W in glyph row ROW.
10638
10639 Implementation note: One would like to draw a bar cursor with an
10640 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10641 Unfortunately, I didn't find a font yet that has this property set.
10642 --gerd. */
dc6f92b8
JB
10643
10644static void
f02d8aa0 10645x_draw_bar_cursor (w, row, width)
06a2c219
GM
10646 struct window *w;
10647 struct glyph_row *row;
f02d8aa0 10648 int width;
dc6f92b8 10649{
92f424df
GM
10650 struct frame *f = XFRAME (w->frame);
10651 struct glyph *cursor_glyph;
10652 GC gc;
10653 int x;
10654 unsigned long mask;
10655 XGCValues xgcv;
10656 Display *dpy;
10657 Window window;
06a2c219 10658
92f424df
GM
10659 /* If cursor is out of bounds, don't draw garbage. This can happen
10660 in mini-buffer windows when switching between echo area glyphs
10661 and mini-buffer. */
10662 cursor_glyph = get_phys_cursor_glyph (w);
10663 if (cursor_glyph == NULL)
10664 return;
06a2c219 10665
92f424df
GM
10666 /* If on an image, draw like a normal cursor. That's usually better
10667 visible than drawing a bar, esp. if the image is large so that
10668 the bar might not be in the window. */
10669 if (cursor_glyph->type == IMAGE_GLYPH)
10670 {
10671 struct glyph_row *row;
10672 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10673 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10674 }
10675 else
10676 {
06a2c219
GM
10677 xgcv.background = f->output_data.x->cursor_pixel;
10678 xgcv.foreground = f->output_data.x->cursor_pixel;
10679 xgcv.graphics_exposures = 0;
10680 mask = GCForeground | GCBackground | GCGraphicsExposures;
10681 dpy = FRAME_X_DISPLAY (f);
10682 window = FRAME_X_WINDOW (f);
10683 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10684
06a2c219
GM
10685 if (gc)
10686 XChangeGC (dpy, gc, mask, &xgcv);
10687 else
10688 {
10689 gc = XCreateGC (dpy, window, mask, &xgcv);
10690 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10691 }
92f424df 10692
f02d8aa0
GM
10693 if (width < 0)
10694 width = f->output_data.x->cursor_width;
92f424df 10695
06a2c219
GM
10696 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10697 x_clip_to_row (w, row, gc, 0);
10698 XFillRectangle (dpy, window, gc,
10699 x,
10700 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10701 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10702 row->height);
10703 XSetClipMask (dpy, gc, None);
10704 }
dc6f92b8
JB
10705}
10706
06a2c219
GM
10707
10708/* Clear the cursor of window W to background color, and mark the
10709 cursor as not shown. This is used when the text where the cursor
10710 is is about to be rewritten. */
10711
dc6f92b8 10712static void
06a2c219
GM
10713x_clear_cursor (w)
10714 struct window *w;
dc6f92b8 10715{
06a2c219
GM
10716 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10717 x_update_window_cursor (w, 0);
10718}
90e65f07 10719
dbc4e1c1 10720
06a2c219
GM
10721/* Draw the cursor glyph of window W in glyph row ROW. See the
10722 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10723
06a2c219
GM
10724static void
10725x_draw_phys_cursor_glyph (w, row, hl)
10726 struct window *w;
10727 struct glyph_row *row;
10728 enum draw_glyphs_face hl;
10729{
10730 /* If cursor hpos is out of bounds, don't draw garbage. This can
10731 happen in mini-buffer windows when switching between echo area
10732 glyphs and mini-buffer. */
10733 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10734 {
10735 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10736 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10737 hl, 0, 0, 0);
10738
10739 /* When we erase the cursor, and ROW is overlapped by other
10740 rows, make sure that these overlapping parts of other rows
10741 are redrawn. */
10742 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10743 {
10744 if (row > w->current_matrix->rows
10745 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10746 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10747
10748 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10749 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10750 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10751 }
10752 }
06a2c219 10753}
dbc4e1c1 10754
eea6af04 10755
06a2c219 10756/* Erase the image of a cursor of window W from the screen. */
eea6af04 10757
06a2c219
GM
10758static void
10759x_erase_phys_cursor (w)
10760 struct window *w;
10761{
10762 struct frame *f = XFRAME (w->frame);
10763 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10764 int hpos = w->phys_cursor.hpos;
10765 int vpos = w->phys_cursor.vpos;
10766 int mouse_face_here_p = 0;
10767 struct glyph_matrix *active_glyphs = w->current_matrix;
10768 struct glyph_row *cursor_row;
10769 struct glyph *cursor_glyph;
10770 enum draw_glyphs_face hl;
10771
10772 /* No cursor displayed or row invalidated => nothing to do on the
10773 screen. */
10774 if (w->phys_cursor_type == NO_CURSOR)
10775 goto mark_cursor_off;
10776
10777 /* VPOS >= active_glyphs->nrows means that window has been resized.
10778 Don't bother to erase the cursor. */
10779 if (vpos >= active_glyphs->nrows)
10780 goto mark_cursor_off;
10781
10782 /* If row containing cursor is marked invalid, there is nothing we
10783 can do. */
10784 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10785 if (!cursor_row->enabled_p)
10786 goto mark_cursor_off;
10787
10788 /* This can happen when the new row is shorter than the old one.
10789 In this case, either x_draw_glyphs or clear_end_of_line
10790 should have cleared the cursor. Note that we wouldn't be
10791 able to erase the cursor in this case because we don't have a
10792 cursor glyph at hand. */
10793 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10794 goto mark_cursor_off;
10795
10796 /* If the cursor is in the mouse face area, redisplay that when
10797 we clear the cursor. */
8801a864
KR
10798 if (! NILP (dpyinfo->mouse_face_window)
10799 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10800 && (vpos > dpyinfo->mouse_face_beg_row
10801 || (vpos == dpyinfo->mouse_face_beg_row
10802 && hpos >= dpyinfo->mouse_face_beg_col))
10803 && (vpos < dpyinfo->mouse_face_end_row
10804 || (vpos == dpyinfo->mouse_face_end_row
10805 && hpos < dpyinfo->mouse_face_end_col))
10806 /* Don't redraw the cursor's spot in mouse face if it is at the
10807 end of a line (on a newline). The cursor appears there, but
10808 mouse highlighting does not. */
10809 && cursor_row->used[TEXT_AREA] > hpos)
10810 mouse_face_here_p = 1;
10811
10812 /* Maybe clear the display under the cursor. */
10813 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10814 {
10815 int x;
045dee35 10816 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10817
06a2c219
GM
10818 cursor_glyph = get_phys_cursor_glyph (w);
10819 if (cursor_glyph == NULL)
10820 goto mark_cursor_off;
dbc4e1c1 10821
06a2c219
GM
10822 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10823
c5e6e06b
GM
10824 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10825 x,
10826 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10827 cursor_row->y)),
10828 cursor_glyph->pixel_width,
10829 cursor_row->visible_height,
10830 False);
dbc4e1c1 10831 }
06a2c219
GM
10832
10833 /* Erase the cursor by redrawing the character underneath it. */
10834 if (mouse_face_here_p)
10835 hl = DRAW_MOUSE_FACE;
10836 else if (cursor_row->inverse_p)
10837 hl = DRAW_INVERSE_VIDEO;
10838 else
10839 hl = DRAW_NORMAL_TEXT;
10840 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10841
06a2c219
GM
10842 mark_cursor_off:
10843 w->phys_cursor_on_p = 0;
10844 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10845}
10846
10847
06a2c219
GM
10848/* Display or clear cursor of window W. If ON is zero, clear the
10849 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10850 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10851
06a2c219
GM
10852void
10853x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10854 struct window *w;
10855 int on, hpos, vpos, x, y;
dbc4e1c1 10856{
06a2c219
GM
10857 struct frame *f = XFRAME (w->frame);
10858 int new_cursor_type;
f02d8aa0 10859 int new_cursor_width;
06a2c219
GM
10860 struct glyph_matrix *current_glyphs;
10861 struct glyph_row *glyph_row;
10862 struct glyph *glyph;
dbc4e1c1 10863
49d838ea 10864 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10865 windows and frames; in the latter case, the frame or window may
10866 be in the midst of changing its size, and x and y may be off the
10867 window. */
10868 if (! FRAME_VISIBLE_P (f)
10869 || FRAME_GARBAGED_P (f)
10870 || vpos >= w->current_matrix->nrows
10871 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10872 return;
10873
10874 /* If cursor is off and we want it off, return quickly. */
06a2c219 10875 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10876 return;
10877
06a2c219
GM
10878 current_glyphs = w->current_matrix;
10879 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10880 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10881
10882 /* If cursor row is not enabled, we don't really know where to
10883 display the cursor. */
10884 if (!glyph_row->enabled_p)
10885 {
10886 w->phys_cursor_on_p = 0;
10887 return;
10888 }
10889
10890 xassert (interrupt_input_blocked);
10891
10892 /* Set new_cursor_type to the cursor we want to be displayed. In a
10893 mini-buffer window, we want the cursor only to appear if we are
10894 reading input from this window. For the selected window, we want
10895 the cursor type given by the frame parameter. If explicitly
10896 marked off, draw no cursor. In all other cases, we want a hollow
10897 box cursor. */
f02d8aa0 10898 new_cursor_width = -1;
9b4a7047
GM
10899 if (cursor_in_echo_area
10900 && FRAME_HAS_MINIBUF_P (f)
10901 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10902 {
9b4a7047
GM
10903 if (w == XWINDOW (echo_area_window))
10904 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10905 else
10906 new_cursor_type = HOLLOW_BOX_CURSOR;
10907 }
06a2c219 10908 else
9b4a7047 10909 {
7a58ab59
GM
10910 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
10911 || w != XWINDOW (f->selected_window))
9b4a7047 10912 {
e55a0b79
GM
10913 extern int cursor_in_non_selected_windows;
10914
5cefa566
GM
10915 if (MINI_WINDOW_P (w)
10916 || !cursor_in_non_selected_windows
10917 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
10918 new_cursor_type = NO_CURSOR;
10919 else
10920 new_cursor_type = HOLLOW_BOX_CURSOR;
10921 }
10922 else if (w->cursor_off_p)
10923 new_cursor_type = NO_CURSOR;
10924 else
f02d8aa0
GM
10925 {
10926 struct buffer *b = XBUFFER (w->buffer);
10927
10928 if (EQ (b->cursor_type, Qt))
10929 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10930 else
10931 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10932 &new_cursor_width);
10933 }
9b4a7047 10934 }
06a2c219
GM
10935
10936 /* If cursor is currently being shown and we don't want it to be or
10937 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10938 erase it. */
06a2c219 10939 if (w->phys_cursor_on_p
dc6f92b8 10940 && (!on
06a2c219
GM
10941 || w->phys_cursor.x != x
10942 || w->phys_cursor.y != y
10943 || new_cursor_type != w->phys_cursor_type))
10944 x_erase_phys_cursor (w);
10945
10946 /* If the cursor is now invisible and we want it to be visible,
10947 display it. */
10948 if (on && !w->phys_cursor_on_p)
10949 {
10950 w->phys_cursor_ascent = glyph_row->ascent;
10951 w->phys_cursor_height = glyph_row->height;
10952
10953 /* Set phys_cursor_.* before x_draw_.* is called because some
10954 of them may need the information. */
10955 w->phys_cursor.x = x;
10956 w->phys_cursor.y = glyph_row->y;
10957 w->phys_cursor.hpos = hpos;
10958 w->phys_cursor.vpos = vpos;
10959 w->phys_cursor_type = new_cursor_type;
10960 w->phys_cursor_on_p = 1;
10961
10962 switch (new_cursor_type)
dc6f92b8 10963 {
06a2c219
GM
10964 case HOLLOW_BOX_CURSOR:
10965 x_draw_hollow_cursor (w, glyph_row);
10966 break;
10967
10968 case FILLED_BOX_CURSOR:
10969 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10970 break;
10971
10972 case BAR_CURSOR:
f02d8aa0 10973 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10974 break;
10975
10976 case NO_CURSOR:
10977 break;
dc6f92b8 10978
06a2c219
GM
10979 default:
10980 abort ();
10981 }
59ddecde
GM
10982
10983#ifdef HAVE_X_I18N
10984 if (w == XWINDOW (f->selected_window))
10985 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10986 xic_set_preeditarea (w, x, y);
10987#endif
dc6f92b8
JB
10988 }
10989
06a2c219 10990#ifndef XFlush
f676886a 10991 if (updating_frame != f)
334208b7 10992 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10993#endif
dc6f92b8
JB
10994}
10995
06a2c219
GM
10996
10997/* Display the cursor on window W, or clear it. X and Y are window
10998 relative pixel coordinates. HPOS and VPOS are glyph matrix
10999 positions. If W is not the selected window, display a hollow
11000 cursor. ON non-zero means display the cursor at X, Y which
11001 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11002
dfcf069d 11003void
06a2c219
GM
11004x_display_cursor (w, on, hpos, vpos, x, y)
11005 struct window *w;
11006 int on, hpos, vpos, x, y;
dc6f92b8 11007{
f94397b5 11008 BLOCK_INPUT;
06a2c219 11009 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11010 UNBLOCK_INPUT;
11011}
11012
06a2c219
GM
11013
11014/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11015 Don't change the cursor's position. */
11016
dfcf069d 11017void
06a2c219 11018x_update_cursor (f, on_p)
5d46f928 11019 struct frame *f;
5d46f928 11020{
06a2c219
GM
11021 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11022}
11023
11024
11025/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11026 in the window tree rooted at W. */
11027
11028static void
11029x_update_cursor_in_window_tree (w, on_p)
11030 struct window *w;
11031 int on_p;
11032{
11033 while (w)
11034 {
11035 if (!NILP (w->hchild))
11036 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11037 else if (!NILP (w->vchild))
11038 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11039 else
11040 x_update_window_cursor (w, on_p);
11041
11042 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11043 }
11044}
5d46f928 11045
f94397b5 11046
06a2c219
GM
11047/* Switch the display of W's cursor on or off, according to the value
11048 of ON. */
11049
11050static void
11051x_update_window_cursor (w, on)
11052 struct window *w;
11053 int on;
11054{
16b5d424
GM
11055 /* Don't update cursor in windows whose frame is in the process
11056 of being deleted. */
11057 if (w->current_matrix)
11058 {
11059 BLOCK_INPUT;
11060 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11061 w->phys_cursor.x, w->phys_cursor.y);
11062 UNBLOCK_INPUT;
11063 }
dc6f92b8 11064}
06a2c219
GM
11065
11066
11067
dc6f92b8
JB
11068\f
11069/* Icons. */
11070
f676886a 11071/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11072 when we get an expose event for it. */
dc6f92b8 11073
dfcf069d 11074void
f676886a
JB
11075refreshicon (f)
11076 struct frame *f;
dc6f92b8 11077{
06a2c219 11078 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11079}
11080
dbc4e1c1 11081/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11082
11083int
990ba854 11084x_bitmap_icon (f, file)
f676886a 11085 struct frame *f;
990ba854 11086 Lisp_Object file;
dc6f92b8 11087{
06a2c219 11088 int bitmap_id;
dc6f92b8 11089
c118dd06 11090 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11091 return 1;
11092
990ba854 11093 /* Free up our existing icon bitmap if any. */
7556890b
RS
11094 if (f->output_data.x->icon_bitmap > 0)
11095 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11096 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11097
11098 if (STRINGP (file))
7f2ae036
RS
11099 bitmap_id = x_create_bitmap_from_file (f, file);
11100 else
11101 {
990ba854 11102 /* Create the GNU bitmap if necessary. */
5bf01b68 11103 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11104 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11105 = x_create_bitmap_from_data (f, gnu_bits,
11106 gnu_width, gnu_height);
990ba854
RS
11107
11108 /* The first time we create the GNU bitmap,
06a2c219 11109 this increments the ref-count one extra time.
990ba854
RS
11110 As a result, the GNU bitmap is never freed.
11111 That way, we don't have to worry about allocating it again. */
334208b7 11112 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11113
334208b7 11114 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11115 }
11116
11117 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11118 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11119
11120 return 0;
11121}
11122
11123
1be2d067
KH
11124/* Make the x-window of frame F use a rectangle with text.
11125 Use ICON_NAME as the text. */
dc6f92b8
JB
11126
11127int
f676886a
JB
11128x_text_icon (f, icon_name)
11129 struct frame *f;
dc6f92b8
JB
11130 char *icon_name;
11131{
c118dd06 11132 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11133 return 1;
11134
1be2d067
KH
11135#ifdef HAVE_X11R4
11136 {
11137 XTextProperty text;
11138 text.value = (unsigned char *) icon_name;
11139 text.encoding = XA_STRING;
11140 text.format = 8;
11141 text.nitems = strlen (icon_name);
11142#ifdef USE_X_TOOLKIT
7556890b 11143 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11144 &text);
11145#else /* not USE_X_TOOLKIT */
11146 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11147#endif /* not USE_X_TOOLKIT */
11148 }
11149#else /* not HAVE_X11R4 */
11150 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11151#endif /* not HAVE_X11R4 */
58769bee 11152
7556890b
RS
11153 if (f->output_data.x->icon_bitmap > 0)
11154 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11155 f->output_data.x->icon_bitmap = 0;
b1c884c3 11156 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11157
11158 return 0;
11159}
11160\f
e99db5a1
RS
11161#define X_ERROR_MESSAGE_SIZE 200
11162
11163/* If non-nil, this should be a string.
11164 It means catch X errors and store the error message in this string. */
11165
11166static Lisp_Object x_error_message_string;
11167
11168/* An X error handler which stores the error message in
11169 x_error_message_string. This is called from x_error_handler if
11170 x_catch_errors is in effect. */
11171
06a2c219 11172static void
e99db5a1
RS
11173x_error_catcher (display, error)
11174 Display *display;
11175 XErrorEvent *error;
11176{
11177 XGetErrorText (display, error->error_code,
11178 XSTRING (x_error_message_string)->data,
11179 X_ERROR_MESSAGE_SIZE);
11180}
11181
11182/* Begin trapping X errors for display DPY. Actually we trap X errors
11183 for all displays, but DPY should be the display you are actually
11184 operating on.
11185
11186 After calling this function, X protocol errors no longer cause
11187 Emacs to exit; instead, they are recorded in the string
11188 stored in x_error_message_string.
11189
11190 Calling x_check_errors signals an Emacs error if an X error has
11191 occurred since the last call to x_catch_errors or x_check_errors.
11192
11193 Calling x_uncatch_errors resumes the normal error handling. */
11194
11195void x_check_errors ();
11196static Lisp_Object x_catch_errors_unwind ();
11197
11198int
11199x_catch_errors (dpy)
11200 Display *dpy;
11201{
11202 int count = specpdl_ptr - specpdl;
11203
11204 /* Make sure any errors from previous requests have been dealt with. */
11205 XSync (dpy, False);
11206
11207 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11208
11209 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11210 XSTRING (x_error_message_string)->data[0] = 0;
11211
11212 return count;
11213}
11214
11215/* Unbind the binding that we made to check for X errors. */
11216
11217static Lisp_Object
11218x_catch_errors_unwind (old_val)
11219 Lisp_Object old_val;
11220{
11221 x_error_message_string = old_val;
11222 return Qnil;
11223}
11224
11225/* If any X protocol errors have arrived since the last call to
11226 x_catch_errors or x_check_errors, signal an Emacs error using
11227 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11228
11229void
11230x_check_errors (dpy, format)
11231 Display *dpy;
11232 char *format;
11233{
11234 /* Make sure to catch any errors incurred so far. */
11235 XSync (dpy, False);
11236
11237 if (XSTRING (x_error_message_string)->data[0])
11238 error (format, XSTRING (x_error_message_string)->data);
11239}
11240
9829ddba
RS
11241/* Nonzero if we had any X protocol errors
11242 since we did x_catch_errors on DPY. */
e99db5a1
RS
11243
11244int
11245x_had_errors_p (dpy)
11246 Display *dpy;
11247{
11248 /* Make sure to catch any errors incurred so far. */
11249 XSync (dpy, False);
11250
11251 return XSTRING (x_error_message_string)->data[0] != 0;
11252}
11253
9829ddba
RS
11254/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11255
06a2c219 11256void
9829ddba
RS
11257x_clear_errors (dpy)
11258 Display *dpy;
11259{
11260 XSTRING (x_error_message_string)->data[0] = 0;
11261}
11262
e99db5a1
RS
11263/* Stop catching X protocol errors and let them make Emacs die.
11264 DPY should be the display that was passed to x_catch_errors.
11265 COUNT should be the value that was returned by
11266 the corresponding call to x_catch_errors. */
11267
11268void
11269x_uncatch_errors (dpy, count)
11270 Display *dpy;
11271 int count;
11272{
11273 unbind_to (count, Qnil);
11274}
11275
11276#if 0
11277static unsigned int x_wire_count;
11278x_trace_wire ()
11279{
11280 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11281}
11282#endif /* ! 0 */
11283
11284\f
11285/* Handle SIGPIPE, which can happen when the connection to a server
11286 simply goes away. SIGPIPE is handled by x_connection_signal.
11287 Don't need to do anything, because the write which caused the
11288 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11289 which will do the appropriate cleanup for us. */
e99db5a1
RS
11290
11291static SIGTYPE
11292x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11293 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11294{
11295#ifdef USG
11296 /* USG systems forget handlers when they are used;
11297 must reestablish each time */
11298 signal (signalnum, x_connection_signal);
11299#endif /* USG */
11300}
0da1ab50 11301
e99db5a1 11302\f
0da1ab50
GM
11303/************************************************************************
11304 Handling X errors
11305 ************************************************************************/
4746118a 11306
0da1ab50
GM
11307/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11308 the text of an error message that lead to the connection loss. */
16bd92ea 11309
4746118a 11310static SIGTYPE
5978125e
GM
11311x_connection_closed (dpy, error_message)
11312 Display *dpy;
7a13e894 11313 char *error_message;
4746118a 11314{
5978125e 11315 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 11316 Lisp_Object frame, tail;
0da1ab50
GM
11317 int count;
11318 char *msg;
11319
11320 msg = (char *) alloca (strlen (error_message) + 1);
11321 strcpy (msg, error_message);
1a532e54
GM
11322 handling_signal = 0;
11323
0da1ab50
GM
11324 /* Prevent being called recursively because of an error condition
11325 below. Otherwise, we might end up with printing ``can't find per
11326 display information'' in the recursive call instead of printing
11327 the original message here. */
11328 count = x_catch_errors (dpy);
11329
8a4f36cc
GM
11330 /* We have to close the display to inform Xt that it doesn't
11331 exist anymore. If we don't, Xt will continue to wait for
11332 events from the display. As a consequence, a sequence of
11333
11334 M-x make-frame-on-display RET :1 RET
11335 ...kill the new frame, so that we get an IO error...
11336 M-x make-frame-on-display RET :1 RET
11337
11338 will indefinitely wait in Xt for events for display `:1', opened
11339 in the first class to make-frame-on-display.
6186a4a0 11340
8a4f36cc
GM
11341 Closing the display is reported to lead to a bus error on
11342 OpenWindows in certain situations. I suspect that is a bug
11343 in OpenWindows. I don't know how to cicumvent it here. */
11344
f613a4c8 11345#ifdef USE_X_TOOLKIT
ae24cb3b
GM
11346 /* If DPYINFO is null, this means we didn't open the display
11347 in the first place, so don't try to close it. */
11348 if (dpyinfo)
11349 XtCloseDisplay (dpy);
f613a4c8 11350#endif
adabc3a9 11351
8a4f36cc 11352 /* Indicate that this display is dead. */
9e80b57d
KR
11353 if (dpyinfo)
11354 dpyinfo->display = 0;
6186a4a0 11355
06a2c219 11356 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11357 that are on the dead display. */
11358 FOR_EACH_FRAME (tail, frame)
11359 {
11360 Lisp_Object minibuf_frame;
11361 minibuf_frame
11362 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11363 if (FRAME_X_P (XFRAME (frame))
11364 && FRAME_X_P (XFRAME (minibuf_frame))
11365 && ! EQ (frame, minibuf_frame)
11366 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11367 Fdelete_frame (frame, Qt);
11368 }
11369
11370 /* Now delete all remaining frames on the dead display.
06a2c219 11371 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11372 for another frame that we need to delete. */
11373 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11374 if (FRAME_X_P (XFRAME (frame))
11375 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11376 {
11377 /* Set this to t so that Fdelete_frame won't get confused
11378 trying to find a replacement. */
11379 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11380 Fdelete_frame (frame, Qt);
11381 }
7a13e894 11382
482a1bd2
KH
11383 if (dpyinfo)
11384 x_delete_display (dpyinfo);
7a13e894 11385
0da1ab50
GM
11386 x_uncatch_errors (dpy, count);
11387
7a13e894
RS
11388 if (x_display_list == 0)
11389 {
0da1ab50 11390 fprintf (stderr, "%s\n", msg);
7a13e894
RS
11391 shut_down_emacs (0, 0, Qnil);
11392 exit (70);
11393 }
12ba150f 11394
7a13e894
RS
11395 /* Ordinary stack unwind doesn't deal with these. */
11396#ifdef SIGIO
11397 sigunblock (sigmask (SIGIO));
11398#endif
11399 sigunblock (sigmask (SIGALRM));
11400 TOTALLY_UNBLOCK_INPUT;
11401
aa4d9a9e 11402 clear_waiting_for_input ();
0da1ab50 11403 error ("%s", msg);
4746118a
JB
11404}
11405
0da1ab50 11406
7a13e894
RS
11407/* This is the usual handler for X protocol errors.
11408 It kills all frames on the display that we got the error for.
11409 If that was the only one, it prints an error message and kills Emacs. */
11410
06a2c219 11411static void
c118dd06
JB
11412x_error_quitter (display, error)
11413 Display *display;
11414 XErrorEvent *error;
11415{
7a13e894 11416 char buf[256], buf1[356];
dc6f92b8 11417
58769bee 11418 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11419 original error handler. */
dc6f92b8 11420
c118dd06 11421 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11422 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11423 buf, error->request_code);
7a13e894 11424 x_connection_closed (display, buf1);
dc6f92b8
JB
11425}
11426
0da1ab50 11427
e99db5a1
RS
11428/* This is the first-level handler for X protocol errors.
11429 It calls x_error_quitter or x_error_catcher. */
7a13e894 11430
8922af5f 11431static int
e99db5a1 11432x_error_handler (display, error)
8922af5f 11433 Display *display;
e99db5a1 11434 XErrorEvent *error;
8922af5f 11435{
e99db5a1
RS
11436 if (! NILP (x_error_message_string))
11437 x_error_catcher (display, error);
11438 else
11439 x_error_quitter (display, error);
06a2c219 11440 return 0;
f9e24cb9 11441}
c118dd06 11442
e99db5a1
RS
11443/* This is the handler for X IO errors, always.
11444 It kills all frames on the display that we lost touch with.
11445 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11446
c118dd06 11447static int
e99db5a1 11448x_io_error_quitter (display)
c118dd06 11449 Display *display;
c118dd06 11450{
e99db5a1 11451 char buf[256];
dc6f92b8 11452
e99db5a1
RS
11453 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11454 x_connection_closed (display, buf);
06a2c219 11455 return 0;
dc6f92b8 11456}
dc6f92b8 11457\f
f451eb13
JB
11458/* Changing the font of the frame. */
11459
76bcdf39
RS
11460/* Give frame F the font named FONTNAME as its default font, and
11461 return the full name of that font. FONTNAME may be a wildcard
11462 pattern; in that case, we choose some font that fits the pattern.
11463 The return value shows which font we chose. */
11464
b5cf7a0e 11465Lisp_Object
f676886a
JB
11466x_new_font (f, fontname)
11467 struct frame *f;
dc6f92b8
JB
11468 register char *fontname;
11469{
dc43ef94 11470 struct font_info *fontp
ee569018 11471 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11472
dc43ef94
KH
11473 if (!fontp)
11474 return Qnil;
2224a5fc 11475
dc43ef94 11476 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11477 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11478 f->output_data.x->fontset = -1;
11479
b2cad826
KH
11480 /* Compute the scroll bar width in character columns. */
11481 if (f->scroll_bar_pixel_width > 0)
11482 {
7556890b 11483 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11484 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11485 }
11486 else
4e61bddf
RS
11487 {
11488 int wid = FONT_WIDTH (f->output_data.x->font);
11489 f->scroll_bar_cols = (14 + wid - 1) / wid;
11490 }
b2cad826 11491
f676886a 11492 /* Now make the frame display the given font. */
c118dd06 11493 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11494 {
7556890b
RS
11495 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11496 f->output_data.x->font->fid);
11497 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11498 f->output_data.x->font->fid);
11499 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11500 f->output_data.x->font->fid);
f676886a 11501
a27f9f86 11502 frame_update_line_height (f);
3497f73e
GM
11503
11504 /* Don't change the size of a tip frame; there's no point in
11505 doing it because it's done in Fx_show_tip, and it leads to
11506 problems because the tip frame has no widget. */
11507 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11508 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11509 }
a27f9f86
RS
11510 else
11511 /* If we are setting a new frame's font for the first time,
11512 there are no faces yet, so this font's height is the line height. */
7556890b 11513 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11514
dc43ef94
KH
11515 return build_string (fontp->full_name);
11516}
11517
11518/* Give frame F the fontset named FONTSETNAME as its default font, and
11519 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11520 pattern; in that case, we choose some fontset that fits the pattern.
11521 The return value shows which fontset we chose. */
b5cf7a0e 11522
dc43ef94
KH
11523Lisp_Object
11524x_new_fontset (f, fontsetname)
11525 struct frame *f;
11526 char *fontsetname;
11527{
ee569018 11528 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11529 Lisp_Object result;
b5cf7a0e 11530
dc43ef94
KH
11531 if (fontset < 0)
11532 return Qnil;
b5cf7a0e 11533
2da424f1
KH
11534 if (f->output_data.x->fontset == fontset)
11535 /* This fontset is already set in frame F. There's nothing more
11536 to do. */
ee569018 11537 return fontset_name (fontset);
dc43ef94 11538
ee569018 11539 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11540
11541 if (!STRINGP (result))
11542 /* Can't load ASCII font. */
11543 return Qnil;
11544
11545 /* Since x_new_font doesn't update any fontset information, do it now. */
11546 f->output_data.x->fontset = fontset;
dc43ef94 11547
f5d11644
GM
11548#ifdef HAVE_X_I18N
11549 if (FRAME_XIC (f)
11550 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11551 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11552#endif
11553
dc43ef94 11554 return build_string (fontsetname);
dc6f92b8 11555}
f5d11644
GM
11556
11557\f
11558/***********************************************************************
11559 X Input Methods
11560 ***********************************************************************/
11561
11562#ifdef HAVE_X_I18N
11563
11564#ifdef HAVE_X11R6
11565
11566/* XIM destroy callback function, which is called whenever the
11567 connection to input method XIM dies. CLIENT_DATA contains a
11568 pointer to the x_display_info structure corresponding to XIM. */
11569
11570static void
11571xim_destroy_callback (xim, client_data, call_data)
11572 XIM xim;
11573 XPointer client_data;
11574 XPointer call_data;
11575{
11576 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11577 Lisp_Object frame, tail;
11578
11579 BLOCK_INPUT;
11580
11581 /* No need to call XDestroyIC.. */
11582 FOR_EACH_FRAME (tail, frame)
11583 {
11584 struct frame *f = XFRAME (frame);
11585 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11586 {
11587 FRAME_XIC (f) = NULL;
11588 if (FRAME_XIC_FONTSET (f))
11589 {
11590 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11591 FRAME_XIC_FONTSET (f) = NULL;
11592 }
11593 }
11594 }
11595
11596 /* No need to call XCloseIM. */
11597 dpyinfo->xim = NULL;
11598 XFree (dpyinfo->xim_styles);
11599 UNBLOCK_INPUT;
11600}
11601
11602#endif /* HAVE_X11R6 */
11603
11604/* Open the connection to the XIM server on display DPYINFO.
11605 RESOURCE_NAME is the resource name Emacs uses. */
11606
11607static void
11608xim_open_dpy (dpyinfo, resource_name)
11609 struct x_display_info *dpyinfo;
11610 char *resource_name;
11611{
287f7dd6 11612#ifdef USE_XIM
f5d11644
GM
11613 XIM xim;
11614
11615 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11616 dpyinfo->xim = xim;
11617
11618 if (xim)
11619 {
f5d11644
GM
11620#ifdef HAVE_X11R6
11621 XIMCallback destroy;
11622#endif
11623
11624 /* Get supported styles and XIM values. */
11625 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11626
11627#ifdef HAVE_X11R6
11628 destroy.callback = xim_destroy_callback;
11629 destroy.client_data = (XPointer)dpyinfo;
68642df6 11630 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11631 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11632#endif
11633 }
287f7dd6
GM
11634
11635#else /* not USE_XIM */
11636 dpyinfo->xim = NULL;
11637#endif /* not USE_XIM */
f5d11644
GM
11638}
11639
11640
b9de836c 11641#ifdef HAVE_X11R6_XIM
f5d11644
GM
11642
11643struct xim_inst_t
11644{
11645 struct x_display_info *dpyinfo;
11646 char *resource_name;
11647};
11648
11649/* XIM instantiate callback function, which is called whenever an XIM
11650 server is available. DISPLAY is teh display of the XIM.
11651 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11652 when the callback was registered. */
11653
11654static void
11655xim_instantiate_callback (display, client_data, call_data)
11656 Display *display;
11657 XPointer client_data;
11658 XPointer call_data;
11659{
11660 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11661 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11662
11663 /* We don't support multiple XIM connections. */
11664 if (dpyinfo->xim)
11665 return;
11666
11667 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11668
11669 /* Create XIC for the existing frames on the same display, as long
11670 as they have no XIC. */
11671 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11672 {
11673 Lisp_Object tail, frame;
11674
11675 BLOCK_INPUT;
11676 FOR_EACH_FRAME (tail, frame)
11677 {
11678 struct frame *f = XFRAME (frame);
11679
11680 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11681 if (FRAME_XIC (f) == NULL)
11682 {
11683 create_frame_xic (f);
11684 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11685 xic_set_statusarea (f);
11686 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11687 {
11688 struct window *w = XWINDOW (f->selected_window);
11689 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11690 }
11691 }
11692 }
11693
11694 UNBLOCK_INPUT;
11695 }
11696}
11697
b9de836c 11698#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11699
11700
11701/* Open a connection to the XIM server on display DPYINFO.
11702 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11703 connection only at the first time. On X11R6, open the connection
11704 in the XIM instantiate callback function. */
11705
11706static void
11707xim_initialize (dpyinfo, resource_name)
11708 struct x_display_info *dpyinfo;
11709 char *resource_name;
11710{
287f7dd6 11711#ifdef USE_XIM
b9de836c 11712#ifdef HAVE_X11R6_XIM
f5d11644
GM
11713 struct xim_inst_t *xim_inst;
11714 int len;
11715
11716 dpyinfo->xim = NULL;
11717 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11718 xim_inst->dpyinfo = dpyinfo;
11719 len = strlen (resource_name);
11720 xim_inst->resource_name = (char *) xmalloc (len + 1);
11721 bcopy (resource_name, xim_inst->resource_name, len + 1);
11722 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11723 resource_name, EMACS_CLASS,
11724 xim_instantiate_callback,
2ebb2f8b
DL
11725 /* Fixme: This is XPointer in
11726 XFree86 but (XPointer *) on
11727 Tru64, at least. */
11728 (XPointer) xim_inst);
b9de836c 11729#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11730 dpyinfo->xim = NULL;
11731 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11732#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11733
11734#else /* not USE_XIM */
11735 dpyinfo->xim = NULL;
11736#endif /* not USE_XIM */
f5d11644
GM
11737}
11738
11739
11740/* Close the connection to the XIM server on display DPYINFO. */
11741
11742static void
11743xim_close_dpy (dpyinfo)
11744 struct x_display_info *dpyinfo;
11745{
287f7dd6 11746#ifdef USE_XIM
b9de836c 11747#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
11748 if (dpyinfo->display)
11749 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11750 NULL, EMACS_CLASS,
11751 xim_instantiate_callback, NULL);
b9de836c 11752#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
11753 if (dpyinfo->display)
11754 XCloseIM (dpyinfo->xim);
f5d11644
GM
11755 dpyinfo->xim = NULL;
11756 XFree (dpyinfo->xim_styles);
287f7dd6 11757#endif /* USE_XIM */
f5d11644
GM
11758}
11759
b9de836c 11760#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11761
11762
dc6f92b8 11763\f
2e365682
RS
11764/* Calculate the absolute position in frame F
11765 from its current recorded position values and gravity. */
11766
dfcf069d 11767void
43bca5d5 11768x_calc_absolute_position (f)
f676886a 11769 struct frame *f;
dc6f92b8 11770{
06a2c219 11771 Window child;
6dba1858 11772 int win_x = 0, win_y = 0;
7556890b 11773 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11774 int this_window;
11775
9829ddba
RS
11776 /* We have nothing to do if the current position
11777 is already for the top-left corner. */
11778 if (! ((flags & XNegative) || (flags & YNegative)))
11779 return;
11780
c81412a0 11781#ifdef USE_X_TOOLKIT
7556890b 11782 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11783#else
11784 this_window = FRAME_X_WINDOW (f);
11785#endif
6dba1858
RS
11786
11787 /* Find the position of the outside upper-left corner of
9829ddba
RS
11788 the inner window, with respect to the outer window.
11789 But do this only if we will need the results. */
7556890b 11790 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11791 {
9829ddba
RS
11792 int count;
11793
6dba1858 11794 BLOCK_INPUT;
9829ddba
RS
11795 count = x_catch_errors (FRAME_X_DISPLAY (f));
11796 while (1)
11797 {
11798 x_clear_errors (FRAME_X_DISPLAY (f));
11799 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11800
11801 /* From-window, to-window. */
11802 this_window,
11803 f->output_data.x->parent_desc,
11804
11805 /* From-position, to-position. */
11806 0, 0, &win_x, &win_y,
11807
11808 /* Child of win. */
11809 &child);
11810 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11811 {
11812 Window newroot, newparent = 0xdeadbeef;
11813 Window *newchildren;
2ebb2f8b 11814 unsigned int nchildren;
9829ddba
RS
11815
11816 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11817 &newparent, &newchildren, &nchildren))
11818 break;
58769bee 11819
7c3c78a3 11820 XFree ((char *) newchildren);
6dba1858 11821
9829ddba
RS
11822 f->output_data.x->parent_desc = newparent;
11823 }
11824 else
11825 break;
11826 }
6dba1858 11827
9829ddba 11828 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11829 UNBLOCK_INPUT;
11830 }
11831
11832 /* Treat negative positions as relative to the leftmost bottommost
11833 position that fits on the screen. */
20f55f9a 11834 if (flags & XNegative)
7556890b 11835 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11836 - 2 * f->output_data.x->border_width - win_x
11837 - PIXEL_WIDTH (f)
11838 + f->output_data.x->left_pos);
dc6f92b8 11839
7708ced0
GM
11840 {
11841 int height = PIXEL_HEIGHT (f);
06a2c219 11842
7708ced0
GM
11843#if defined USE_X_TOOLKIT && defined USE_MOTIF
11844 /* Something is fishy here. When using Motif, starting Emacs with
11845 `-g -0-0', the frame appears too low by a few pixels.
11846
11847 This seems to be so because initially, while Emacs is starting,
11848 the column widget's height and the frame's pixel height are
11849 different. The column widget's height is the right one. In
11850 later invocations, when Emacs is up, the frame's pixel height
11851 is right, though.
11852
11853 It's not obvious where the initial small difference comes from.
11854 2000-12-01, gerd. */
11855
11856 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 11857#endif
2e365682 11858
7708ced0
GM
11859 if (flags & YNegative)
11860 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11861 - 2 * f->output_data.x->border_width
11862 - win_y
11863 - height
11864 + f->output_data.x->top_pos);
11865 }
11866
3a35ab44
RS
11867 /* The left_pos and top_pos
11868 are now relative to the top and left screen edges,
11869 so the flags should correspond. */
7556890b 11870 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11871}
11872
3a35ab44
RS
11873/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11874 to really change the position, and 0 when calling from
11875 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11876 position values). It is -1 when calling from x_set_frame_parameters,
11877 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11878
dfcf069d 11879void
dc05a16b 11880x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11881 struct frame *f;
dc6f92b8 11882 register int xoff, yoff;
dc05a16b 11883 int change_gravity;
dc6f92b8 11884{
4a4cbdd5
KH
11885 int modified_top, modified_left;
11886
aa3ff7c9 11887 if (change_gravity > 0)
3a35ab44 11888 {
7556890b
RS
11889 f->output_data.x->top_pos = yoff;
11890 f->output_data.x->left_pos = xoff;
11891 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11892 if (xoff < 0)
7556890b 11893 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11894 if (yoff < 0)
7556890b
RS
11895 f->output_data.x->size_hint_flags |= YNegative;
11896 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11897 }
43bca5d5 11898 x_calc_absolute_position (f);
dc6f92b8
JB
11899
11900 BLOCK_INPUT;
c32cdd9a 11901 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11902
7556890b
RS
11903 modified_left = f->output_data.x->left_pos;
11904 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11905#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11906 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11907 /* It is a mystery why we need to add the border_width here
11908 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11909 if (change_gravity != 0)
4a4cbdd5 11910 {
7556890b
RS
11911 modified_left += f->output_data.x->border_width;
11912 modified_top += f->output_data.x->border_width;
4a4cbdd5 11913 }
e73ec6fa 11914#endif
4a4cbdd5 11915
3afe33e7 11916#ifdef USE_X_TOOLKIT
7556890b 11917 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11918 modified_left, modified_top);
3afe33e7 11919#else /* not USE_X_TOOLKIT */
334208b7 11920 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11921 modified_left, modified_top);
3afe33e7 11922#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11923 UNBLOCK_INPUT;
11924}
11925
dc6f92b8 11926
499b1844
GM
11927/* Change the size of frame F's X window to COLS/ROWS in the case F
11928 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
11929 top-left-corner window gravity for this size change and subsequent
11930 size changes. Otherwise we leave the window gravity unchanged. */
11931
11932static void
11933x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 11934 struct frame *f;
bc20ebbf 11935 int change_gravity;
b1c884c3 11936 int cols, rows;
dc6f92b8
JB
11937{
11938 int pixelwidth, pixelheight;
80fd1fe2 11939
b1c884c3 11940 check_frame_size (f, &rows, &cols);
7556890b 11941 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11942 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11943 ? 0
11944 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11945 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11946 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11947 f->output_data.x->flags_areas_extra
110859fc 11948 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11949 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11950 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11951
7556890b 11952 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11953 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11954
334208b7
RS
11955 XSync (FRAME_X_DISPLAY (f), False);
11956 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11957 pixelwidth, pixelheight);
b1c884c3
JB
11958
11959 /* Now, strictly speaking, we can't be sure that this is accurate,
11960 but the window manager will get around to dealing with the size
11961 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11962 ConfigureNotify event gets here.
11963
11964 We could just not bother storing any of this information here,
11965 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11966 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11967 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11968 point in the future when the ConfigureNotify event arrives.
11969
11970 We pass 1 for DELAY since we can't run Lisp code inside of
11971 a BLOCK_INPUT. */
7d1e984f 11972 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11973 PIXEL_WIDTH (f) = pixelwidth;
11974 PIXEL_HEIGHT (f) = pixelheight;
11975
aee9a898
RS
11976 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11977 receive in the ConfigureNotify event; if we get what we asked
11978 for, then the event won't cause the screen to become garbaged, so
11979 we have to make sure to do it here. */
11980 SET_FRAME_GARBAGED (f);
11981
11982 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
11983}
11984
11985
11986/* Call this to change the size of frame F's x-window.
11987 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11988 for this size change and subsequent size changes.
11989 Otherwise we leave the window gravity unchanged. */
aee9a898 11990
499b1844
GM
11991void
11992x_set_window_size (f, change_gravity, cols, rows)
11993 struct frame *f;
11994 int change_gravity;
11995 int cols, rows;
11996{
11997 BLOCK_INPUT;
11998
11999#ifdef USE_X_TOOLKIT
12000
f1f4d345 12001 if (f->output_data.x->widget != NULL)
499b1844
GM
12002 {
12003 /* The x and y position of the widget is clobbered by the
12004 call to XtSetValues within EmacsFrameSetCharSize.
12005 This is a real kludge, but I don't understand Xt so I can't
12006 figure out a correct fix. Can anyone else tell me? -- rms. */
12007 int xpos = f->output_data.x->widget->core.x;
12008 int ypos = f->output_data.x->widget->core.y;
12009 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12010 f->output_data.x->widget->core.x = xpos;
12011 f->output_data.x->widget->core.y = ypos;
12012 }
12013 else
12014 x_set_window_size_1 (f, change_gravity, cols, rows);
12015
12016#else /* not USE_X_TOOLKIT */
12017
12018 x_set_window_size_1 (f, change_gravity, cols, rows);
12019
aee9a898
RS
12020#endif /* not USE_X_TOOLKIT */
12021
4d73d038 12022 /* If cursor was outside the new size, mark it as off. */
06a2c219 12023 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 12024
aee9a898
RS
12025 /* Clear out any recollection of where the mouse highlighting was,
12026 since it might be in a place that's outside the new frame size.
12027 Actually checking whether it is outside is a pain in the neck,
12028 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 12029 cancel_mouse_face (f);
dbc4e1c1 12030
dc6f92b8
JB
12031 UNBLOCK_INPUT;
12032}
dc6f92b8 12033\f
d047c4eb 12034/* Mouse warping. */
dc6f92b8 12035
9b378208 12036void
f676886a
JB
12037x_set_mouse_position (f, x, y)
12038 struct frame *f;
dc6f92b8
JB
12039 int x, y;
12040{
12041 int pix_x, pix_y;
12042
7556890b
RS
12043 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12044 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
12045
12046 if (pix_x < 0) pix_x = 0;
12047 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12048
12049 if (pix_y < 0) pix_y = 0;
12050 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
12051
12052 BLOCK_INPUT;
dc6f92b8 12053
334208b7
RS
12054 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12055 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
12056 UNBLOCK_INPUT;
12057}
12058
9b378208
RS
12059/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12060
12061void
12062x_set_mouse_pixel_position (f, pix_x, pix_y)
12063 struct frame *f;
12064 int pix_x, pix_y;
12065{
12066 BLOCK_INPUT;
12067
334208b7
RS
12068 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12069 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
12070 UNBLOCK_INPUT;
12071}
d047c4eb
KH
12072\f
12073/* focus shifting, raising and lowering. */
9b378208 12074
dfcf069d 12075void
f676886a
JB
12076x_focus_on_frame (f)
12077 struct frame *f;
dc6f92b8 12078{
1fb20991 12079#if 0 /* This proves to be unpleasant. */
f676886a 12080 x_raise_frame (f);
1fb20991 12081#endif
6d4238f3
JB
12082#if 0
12083 /* I don't think that the ICCCM allows programs to do things like this
12084 without the interaction of the window manager. Whatever you end up
f676886a 12085 doing with this code, do it to x_unfocus_frame too. */
334208b7 12086 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 12087 RevertToPointerRoot, CurrentTime);
c118dd06 12088#endif /* ! 0 */
dc6f92b8
JB
12089}
12090
dfcf069d 12091void
f676886a
JB
12092x_unfocus_frame (f)
12093 struct frame *f;
dc6f92b8 12094{
6d4238f3 12095#if 0
f676886a 12096 /* Look at the remarks in x_focus_on_frame. */
0f941935 12097 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 12098 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 12099 RevertToPointerRoot, CurrentTime);
c118dd06 12100#endif /* ! 0 */
dc6f92b8
JB
12101}
12102
f676886a 12103/* Raise frame F. */
dc6f92b8 12104
dfcf069d 12105void
f676886a
JB
12106x_raise_frame (f)
12107 struct frame *f;
dc6f92b8 12108{
3a88c238 12109 if (f->async_visible)
dc6f92b8
JB
12110 {
12111 BLOCK_INPUT;
3afe33e7 12112#ifdef USE_X_TOOLKIT
7556890b 12113 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12114#else /* not USE_X_TOOLKIT */
334208b7 12115 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12116#endif /* not USE_X_TOOLKIT */
334208b7 12117 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12118 UNBLOCK_INPUT;
12119 }
12120}
12121
f676886a 12122/* Lower frame F. */
dc6f92b8 12123
dfcf069d 12124void
f676886a
JB
12125x_lower_frame (f)
12126 struct frame *f;
dc6f92b8 12127{
3a88c238 12128 if (f->async_visible)
dc6f92b8
JB
12129 {
12130 BLOCK_INPUT;
3afe33e7 12131#ifdef USE_X_TOOLKIT
7556890b 12132 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12133#else /* not USE_X_TOOLKIT */
334208b7 12134 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12135#endif /* not USE_X_TOOLKIT */
334208b7 12136 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12137 UNBLOCK_INPUT;
12138 }
12139}
12140
dbc4e1c1 12141static void
6b0442dc 12142XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12143 FRAME_PTR f;
6b0442dc 12144 int raise_flag;
dbc4e1c1 12145{
6b0442dc 12146 if (raise_flag)
dbc4e1c1
JB
12147 x_raise_frame (f);
12148 else
12149 x_lower_frame (f);
12150}
d047c4eb
KH
12151\f
12152/* Change of visibility. */
dc6f92b8 12153
9382638d
KH
12154/* This tries to wait until the frame is really visible.
12155 However, if the window manager asks the user where to position
12156 the frame, this will return before the user finishes doing that.
12157 The frame will not actually be visible at that time,
12158 but it will become visible later when the window manager
12159 finishes with it. */
12160
dfcf069d 12161void
f676886a
JB
12162x_make_frame_visible (f)
12163 struct frame *f;
dc6f92b8 12164{
990ba854 12165 Lisp_Object type;
1aa6072f 12166 int original_top, original_left;
31be9251
GM
12167 int retry_count = 2;
12168
12169 retry:
dc6f92b8 12170
dc6f92b8 12171 BLOCK_INPUT;
dc6f92b8 12172
990ba854
RS
12173 type = x_icon_type (f);
12174 if (!NILP (type))
12175 x_bitmap_icon (f, type);
bdcd49ba 12176
f676886a 12177 if (! FRAME_VISIBLE_P (f))
90e65f07 12178 {
1aa6072f
RS
12179 /* We test FRAME_GARBAGED_P here to make sure we don't
12180 call x_set_offset a second time
12181 if we get to x_make_frame_visible a second time
12182 before the window gets really visible. */
12183 if (! FRAME_ICONIFIED_P (f)
12184 && ! f->output_data.x->asked_for_visible)
12185 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12186
12187 f->output_data.x->asked_for_visible = 1;
12188
90e65f07 12189 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12190 x_wm_set_window_state (f, NormalState);
3afe33e7 12191#ifdef USE_X_TOOLKIT
d7a38a2e 12192 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12193 XtMapWidget (f->output_data.x->widget);
3afe33e7 12194#else /* not USE_X_TOOLKIT */
7f9c7f94 12195 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12196#endif /* not USE_X_TOOLKIT */
0134a210
RS
12197#if 0 /* This seems to bring back scroll bars in the wrong places
12198 if the window configuration has changed. They seem
12199 to come back ok without this. */
ab648270 12200 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12201 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12202#endif
90e65f07 12203 }
dc6f92b8 12204
334208b7 12205 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12206
0dacf791
RS
12207 /* Synchronize to ensure Emacs knows the frame is visible
12208 before we do anything else. We do this loop with input not blocked
12209 so that incoming events are handled. */
12210 {
12211 Lisp_Object frame;
12ce2351 12212 int count;
28c01ffe
RS
12213 /* This must be before UNBLOCK_INPUT
12214 since events that arrive in response to the actions above
12215 will set it when they are handled. */
12216 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12217
12218 original_left = f->output_data.x->left_pos;
12219 original_top = f->output_data.x->top_pos;
c0a04927
RS
12220
12221 /* This must come after we set COUNT. */
12222 UNBLOCK_INPUT;
12223
2745e6c4 12224 /* We unblock here so that arriving X events are processed. */
1aa6072f 12225
dcb07ae9
RS
12226 /* Now move the window back to where it was "supposed to be".
12227 But don't do it if the gravity is negative.
12228 When the gravity is negative, this uses a position
28c01ffe
RS
12229 that is 3 pixels too low. Perhaps that's really the border width.
12230
12231 Don't do this if the window has never been visible before,
12232 because the window manager may choose the position
12233 and we don't want to override it. */
1aa6072f 12234
4d3f5d9a 12235 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12236 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12237 && previously_visible)
1aa6072f 12238 {
2745e6c4
RS
12239 Drawable rootw;
12240 int x, y;
12241 unsigned int width, height, border, depth;
06a2c219 12242
1aa6072f 12243 BLOCK_INPUT;
9829ddba 12244
06a2c219
GM
12245 /* On some window managers (such as FVWM) moving an existing
12246 window, even to the same place, causes the window manager
12247 to introduce an offset. This can cause the window to move
12248 to an unexpected location. Check the geometry (a little
12249 slow here) and then verify that the window is in the right
12250 place. If the window is not in the right place, move it
12251 there, and take the potential window manager hit. */
2745e6c4
RS
12252 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12253 &rootw, &x, &y, &width, &height, &border, &depth);
12254
12255 if (original_left != x || original_top != y)
12256 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12257 original_left, original_top);
12258
1aa6072f
RS
12259 UNBLOCK_INPUT;
12260 }
9829ddba 12261
e0c1aef2 12262 XSETFRAME (frame, f);
c0a04927 12263
12ce2351
GM
12264 /* Wait until the frame is visible. Process X events until a
12265 MapNotify event has been seen, or until we think we won't get a
12266 MapNotify at all.. */
12267 for (count = input_signal_count + 10;
12268 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12269 {
12ce2351 12270 /* Force processing of queued events. */
334208b7 12271 x_sync (f);
12ce2351
GM
12272
12273 /* Machines that do polling rather than SIGIO have been
12274 observed to go into a busy-wait here. So we'll fake an
12275 alarm signal to let the handler know that there's something
12276 to be read. We used to raise a real alarm, but it seems
12277 that the handler isn't always enabled here. This is
12278 probably a bug. */
8b2f8d4e 12279 if (input_polling_used ())
3b2fa4e6 12280 {
12ce2351
GM
12281 /* It could be confusing if a real alarm arrives while
12282 processing the fake one. Turn it off and let the
12283 handler reset it. */
3e71d8f2 12284 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12285 int old_poll_suppress_count = poll_suppress_count;
12286 poll_suppress_count = 1;
12287 poll_for_input_1 ();
12288 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12289 }
12ce2351
GM
12290
12291 /* See if a MapNotify event has been processed. */
12292 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12293 }
31be9251
GM
12294
12295 /* 2000-09-28: In
12296
12297 (let ((f (selected-frame)))
12298 (iconify-frame f)
12299 (raise-frame f))
12300
12301 the frame is not raised with various window managers on
12302 FreeBSD, Linux and Solaris. It turns out that, for some
12303 unknown reason, the call to XtMapWidget is completely ignored.
12304 Mapping the widget a second time works. */
12305
12306 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12307 goto retry;
0dacf791 12308 }
dc6f92b8
JB
12309}
12310
06a2c219 12311/* Change from mapped state to withdrawn state. */
dc6f92b8 12312
d047c4eb
KH
12313/* Make the frame visible (mapped and not iconified). */
12314
dfcf069d 12315void
f676886a
JB
12316x_make_frame_invisible (f)
12317 struct frame *f;
dc6f92b8 12318{
546e6d5b
RS
12319 Window window;
12320
12321#ifdef USE_X_TOOLKIT
12322 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12323 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12324#else /* not USE_X_TOOLKIT */
12325 window = FRAME_X_WINDOW (f);
12326#endif /* not USE_X_TOOLKIT */
dc6f92b8 12327
9319ae23 12328 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12329 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12330 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12331
5627c40e 12332#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12333 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12334 return;
5627c40e 12335#endif
dc6f92b8
JB
12336
12337 BLOCK_INPUT;
c118dd06 12338
af31d76f
RS
12339 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12340 that the current position of the window is user-specified, rather than
12341 program-specified, so that when the window is mapped again, it will be
12342 placed at the same location, without forcing the user to position it
12343 by hand again (they have already done that once for this window.) */
c32cdd9a 12344 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12345
c118dd06
JB
12346#ifdef HAVE_X11R4
12347
334208b7
RS
12348 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12349 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12350 {
12351 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12352 error ("Can't notify window manager of window withdrawal");
c118dd06 12353 }
c118dd06 12354#else /* ! defined (HAVE_X11R4) */
16bd92ea 12355
c118dd06 12356 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12357 if (! EQ (Vx_no_window_manager, Qt))
12358 {
16bd92ea 12359 XEvent unmap;
dc6f92b8 12360
16bd92ea 12361 unmap.xunmap.type = UnmapNotify;
546e6d5b 12362 unmap.xunmap.window = window;
334208b7 12363 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12364 unmap.xunmap.from_configure = False;
334208b7
RS
12365 if (! XSendEvent (FRAME_X_DISPLAY (f),
12366 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12367 False,
06a2c219 12368 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12369 &unmap))
12370 {
12371 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12372 error ("Can't notify window manager of withdrawal");
16bd92ea 12373 }
dc6f92b8
JB
12374 }
12375
16bd92ea 12376 /* Unmap the window ourselves. Cheeky! */
334208b7 12377 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12378#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12379
5627c40e
RS
12380 /* We can't distinguish this from iconification
12381 just by the event that we get from the server.
12382 So we can't win using the usual strategy of letting
12383 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12384 and synchronize with the server to make sure we agree. */
12385 f->visible = 0;
12386 FRAME_ICONIFIED_P (f) = 0;
12387 f->async_visible = 0;
12388 f->async_iconified = 0;
12389
334208b7 12390 x_sync (f);
5627c40e 12391
dc6f92b8
JB
12392 UNBLOCK_INPUT;
12393}
12394
06a2c219 12395/* Change window state from mapped to iconified. */
dc6f92b8 12396
dfcf069d 12397void
f676886a
JB
12398x_iconify_frame (f)
12399 struct frame *f;
dc6f92b8 12400{
3afe33e7 12401 int result;
990ba854 12402 Lisp_Object type;
dc6f92b8 12403
9319ae23 12404 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12405 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12406 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12407
3a88c238 12408 if (f->async_iconified)
dc6f92b8
JB
12409 return;
12410
3afe33e7 12411 BLOCK_INPUT;
546e6d5b 12412
9af3143a
RS
12413 FRAME_SAMPLE_VISIBILITY (f);
12414
990ba854
RS
12415 type = x_icon_type (f);
12416 if (!NILP (type))
12417 x_bitmap_icon (f, type);
bdcd49ba
RS
12418
12419#ifdef USE_X_TOOLKIT
12420
546e6d5b
RS
12421 if (! FRAME_VISIBLE_P (f))
12422 {
12423 if (! EQ (Vx_no_window_manager, Qt))
12424 x_wm_set_window_state (f, IconicState);
12425 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12426 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12427 /* The server won't give us any event to indicate
12428 that an invisible frame was changed to an icon,
12429 so we have to record it here. */
12430 f->iconified = 1;
1e6bc770 12431 f->visible = 1;
9cf30a30 12432 f->async_iconified = 1;
1e6bc770 12433 f->async_visible = 0;
546e6d5b
RS
12434 UNBLOCK_INPUT;
12435 return;
12436 }
12437
334208b7 12438 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12439 XtWindow (f->output_data.x->widget),
334208b7 12440 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12441 UNBLOCK_INPUT;
12442
12443 if (!result)
546e6d5b 12444 error ("Can't notify window manager of iconification");
3afe33e7
RS
12445
12446 f->async_iconified = 1;
1e6bc770
RS
12447 f->async_visible = 0;
12448
8c002a25
KH
12449
12450 BLOCK_INPUT;
334208b7 12451 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12452 UNBLOCK_INPUT;
3afe33e7
RS
12453#else /* not USE_X_TOOLKIT */
12454
fd13dbb2
RS
12455 /* Make sure the X server knows where the window should be positioned,
12456 in case the user deiconifies with the window manager. */
12457 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12458 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12459
16bd92ea
JB
12460 /* Since we don't know which revision of X we're running, we'll use both
12461 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12462
12463 /* X11R4: send a ClientMessage to the window manager using the
12464 WM_CHANGE_STATE type. */
12465 {
12466 XEvent message;
58769bee 12467
c118dd06 12468 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12469 message.xclient.type = ClientMessage;
334208b7 12470 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12471 message.xclient.format = 32;
12472 message.xclient.data.l[0] = IconicState;
12473
334208b7
RS
12474 if (! XSendEvent (FRAME_X_DISPLAY (f),
12475 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12476 False,
12477 SubstructureRedirectMask | SubstructureNotifyMask,
12478 &message))
dc6f92b8
JB
12479 {
12480 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12481 error ("Can't notify window manager of iconification");
dc6f92b8 12482 }
16bd92ea 12483 }
dc6f92b8 12484
58769bee 12485 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12486 IconicState. */
12487 x_wm_set_window_state (f, IconicState);
dc6f92b8 12488
a9c00105
RS
12489 if (!FRAME_VISIBLE_P (f))
12490 {
12491 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12492 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12493 }
12494
3a88c238 12495 f->async_iconified = 1;
1e6bc770 12496 f->async_visible = 0;
dc6f92b8 12497
334208b7 12498 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12499 UNBLOCK_INPUT;
8c002a25 12500#endif /* not USE_X_TOOLKIT */
dc6f92b8 12501}
19f71add 12502
d047c4eb 12503\f
19f71add 12504/* Free X resources of frame F. */
dc6f92b8 12505
dfcf069d 12506void
19f71add 12507x_free_frame_resources (f)
f676886a 12508 struct frame *f;
dc6f92b8 12509{
7f9c7f94
RS
12510 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12511
dc6f92b8 12512 BLOCK_INPUT;
c0ff3fab 12513
6186a4a0
RS
12514 /* If a display connection is dead, don't try sending more
12515 commands to the X server. */
19f71add 12516 if (dpyinfo->display)
6186a4a0 12517 {
19f71add 12518 if (f->output_data.x->icon_desc)
6186a4a0 12519 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
19f71add 12520
31f41daf 12521#ifdef HAVE_X_I18N
f5d11644
GM
12522 if (FRAME_XIC (f))
12523 free_frame_xic (f);
31f41daf 12524#endif
19f71add 12525
2662734b 12526 if (FRAME_X_WINDOW (f))
19f71add
GM
12527 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12528
3afe33e7 12529#ifdef USE_X_TOOLKIT
06a2c219
GM
12530 if (f->output_data.x->widget)
12531 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12532 free_frame_menubar (f);
3afe33e7
RS
12533#endif /* USE_X_TOOLKIT */
12534
3e71d8f2
GM
12535 unload_color (f, f->output_data.x->foreground_pixel);
12536 unload_color (f, f->output_data.x->background_pixel);
12537 unload_color (f, f->output_data.x->cursor_pixel);
12538 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12539 unload_color (f, f->output_data.x->border_pixel);
12540 unload_color (f, f->output_data.x->mouse_pixel);
19f71add 12541
3e71d8f2
GM
12542 if (f->output_data.x->scroll_bar_background_pixel != -1)
12543 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12544 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12545 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12546 if (f->output_data.x->white_relief.allocated_p)
12547 unload_color (f, f->output_data.x->white_relief.pixel);
12548 if (f->output_data.x->black_relief.allocated_p)
12549 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 12550
19f71add
GM
12551 if (FRAME_FACE_CACHE (f))
12552 free_frame_faces (f);
12553
4ca78676 12554 x_free_gcs (f);
6186a4a0
RS
12555 XFlush (FRAME_X_DISPLAY (f));
12556 }
dc6f92b8 12557
df89d8a4 12558 if (f->output_data.x->saved_menu_event)
06a2c219 12559 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12560
7556890b 12561 xfree (f->output_data.x);
19f71add
GM
12562 f->output_data.x = NULL;
12563
0f941935
KH
12564 if (f == dpyinfo->x_focus_frame)
12565 dpyinfo->x_focus_frame = 0;
12566 if (f == dpyinfo->x_focus_event_frame)
12567 dpyinfo->x_focus_event_frame = 0;
12568 if (f == dpyinfo->x_highlight_frame)
12569 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12570
7f9c7f94 12571 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12572 {
7f9c7f94
RS
12573 dpyinfo->mouse_face_beg_row
12574 = dpyinfo->mouse_face_beg_col = -1;
12575 dpyinfo->mouse_face_end_row
12576 = dpyinfo->mouse_face_end_col = -1;
12577 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12578 dpyinfo->mouse_face_deferred_gc = 0;
12579 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12580 }
0134a210 12581
c0ff3fab 12582 UNBLOCK_INPUT;
dc6f92b8 12583}
19f71add
GM
12584
12585
12586/* Destroy the X window of frame F. */
12587
12588void
12589x_destroy_window (f)
12590 struct frame *f;
12591{
12592 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12593
12594 /* If a display connection is dead, don't try sending more
12595 commands to the X server. */
12596 if (dpyinfo->display != 0)
12597 x_free_frame_resources (f);
12598
12599 dpyinfo->reference_count--;
12600}
12601
dc6f92b8 12602\f
f451eb13
JB
12603/* Setting window manager hints. */
12604
af31d76f
RS
12605/* Set the normal size hints for the window manager, for frame F.
12606 FLAGS is the flags word to use--or 0 meaning preserve the flags
12607 that the window now has.
12608 If USER_POSITION is nonzero, we set the USPosition
12609 flag (this is useful when FLAGS is 0). */
6dba1858 12610
dfcf069d 12611void
af31d76f 12612x_wm_set_size_hint (f, flags, user_position)
f676886a 12613 struct frame *f;
af31d76f
RS
12614 long flags;
12615 int user_position;
dc6f92b8
JB
12616{
12617 XSizeHints size_hints;
3afe33e7
RS
12618
12619#ifdef USE_X_TOOLKIT
7e4f2521
FP
12620 Arg al[2];
12621 int ac = 0;
12622 Dimension widget_width, widget_height;
7556890b 12623 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12624#else /* not USE_X_TOOLKIT */
c118dd06 12625 Window window = FRAME_X_WINDOW (f);
3afe33e7 12626#endif /* not USE_X_TOOLKIT */
dc6f92b8 12627
b72a58fd
RS
12628 /* Setting PMaxSize caused various problems. */
12629 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12630
7556890b
RS
12631 size_hints.x = f->output_data.x->left_pos;
12632 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12633
7e4f2521
FP
12634#ifdef USE_X_TOOLKIT
12635 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12636 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12637 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12638 size_hints.height = widget_height;
12639 size_hints.width = widget_width;
12640#else /* not USE_X_TOOLKIT */
f676886a
JB
12641 size_hints.height = PIXEL_HEIGHT (f);
12642 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12643#endif /* not USE_X_TOOLKIT */
7553a6b7 12644
7556890b
RS
12645 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12646 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12647 size_hints.max_width
12648 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12649 size_hints.max_height
12650 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12651
d067ea8b
KH
12652 /* Calculate the base and minimum sizes.
12653
12654 (When we use the X toolkit, we don't do it here.
12655 Instead we copy the values that the widgets are using, below.) */
12656#ifndef USE_X_TOOLKIT
b1c884c3 12657 {
b0342f17 12658 int base_width, base_height;
0134a210 12659 int min_rows = 0, min_cols = 0;
b0342f17 12660
f451eb13
JB
12661 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12662 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12663
0134a210 12664 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12665
0134a210
RS
12666 /* The window manager uses the base width hints to calculate the
12667 current number of rows and columns in the frame while
12668 resizing; min_width and min_height aren't useful for this
12669 purpose, since they might not give the dimensions for a
12670 zero-row, zero-column frame.
58769bee 12671
0134a210
RS
12672 We use the base_width and base_height members if we have
12673 them; otherwise, we set the min_width and min_height members
12674 to the size for a zero x zero frame. */
b0342f17
JB
12675
12676#ifdef HAVE_X11R4
0134a210
RS
12677 size_hints.flags |= PBaseSize;
12678 size_hints.base_width = base_width;
12679 size_hints.base_height = base_height;
12680 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12681 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12682#else
0134a210
RS
12683 size_hints.min_width = base_width;
12684 size_hints.min_height = base_height;
b0342f17 12685#endif
b1c884c3 12686 }
dc6f92b8 12687
d067ea8b 12688 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12689 if (flags)
dc6f92b8 12690 {
d067ea8b
KH
12691 size_hints.flags |= flags;
12692 goto no_read;
12693 }
12694#endif /* not USE_X_TOOLKIT */
12695
12696 {
12697 XSizeHints hints; /* Sometimes I hate X Windows... */
12698 long supplied_return;
12699 int value;
af31d76f
RS
12700
12701#ifdef HAVE_X11R4
d067ea8b
KH
12702 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12703 &supplied_return);
af31d76f 12704#else
d067ea8b 12705 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12706#endif
58769bee 12707
d067ea8b
KH
12708#ifdef USE_X_TOOLKIT
12709 size_hints.base_height = hints.base_height;
12710 size_hints.base_width = hints.base_width;
12711 size_hints.min_height = hints.min_height;
12712 size_hints.min_width = hints.min_width;
12713#endif
12714
12715 if (flags)
12716 size_hints.flags |= flags;
12717 else
12718 {
12719 if (value == 0)
12720 hints.flags = 0;
12721 if (hints.flags & PSize)
12722 size_hints.flags |= PSize;
12723 if (hints.flags & PPosition)
12724 size_hints.flags |= PPosition;
12725 if (hints.flags & USPosition)
12726 size_hints.flags |= USPosition;
12727 if (hints.flags & USSize)
12728 size_hints.flags |= USSize;
12729 }
12730 }
12731
06a2c219 12732#ifndef USE_X_TOOLKIT
d067ea8b 12733 no_read:
06a2c219 12734#endif
0134a210 12735
af31d76f 12736#ifdef PWinGravity
7556890b 12737 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12738 size_hints.flags |= PWinGravity;
dc05a16b 12739
af31d76f 12740 if (user_position)
6dba1858 12741 {
af31d76f
RS
12742 size_hints.flags &= ~ PPosition;
12743 size_hints.flags |= USPosition;
6dba1858 12744 }
2554751d 12745#endif /* PWinGravity */
6dba1858 12746
b0342f17 12747#ifdef HAVE_X11R4
334208b7 12748 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12749#else
334208b7 12750 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12751#endif
dc6f92b8
JB
12752}
12753
12754/* Used for IconicState or NormalState */
06a2c219 12755
dfcf069d 12756void
f676886a
JB
12757x_wm_set_window_state (f, state)
12758 struct frame *f;
dc6f92b8
JB
12759 int state;
12760{
3afe33e7 12761#ifdef USE_X_TOOLKIT
546e6d5b
RS
12762 Arg al[1];
12763
12764 XtSetArg (al[0], XtNinitialState, state);
7556890b 12765 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12766#else /* not USE_X_TOOLKIT */
c118dd06 12767 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12768
7556890b
RS
12769 f->output_data.x->wm_hints.flags |= StateHint;
12770 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12771
7556890b 12772 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12773#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12774}
12775
dfcf069d 12776void
7f2ae036 12777x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12778 struct frame *f;
7f2ae036 12779 int pixmap_id;
dc6f92b8 12780{
d2bd6bc4
RS
12781 Pixmap icon_pixmap;
12782
06a2c219 12783#ifndef USE_X_TOOLKIT
c118dd06 12784 Window window = FRAME_X_WINDOW (f);
75231bad 12785#endif
dc6f92b8 12786
7f2ae036 12787 if (pixmap_id > 0)
dbc4e1c1 12788 {
d2bd6bc4 12789 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12790 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12791 }
12792 else
68568555
RS
12793 {
12794 /* It seems there is no way to turn off use of an icon pixmap.
12795 The following line does it, only if no icon has yet been created,
12796 for some window managers. But with mwm it crashes.
12797 Some people say it should clear the IconPixmapHint bit in this case,
12798 but that doesn't work, and the X consortium said it isn't the
12799 right thing at all. Since there is no way to win,
12800 best to explicitly give up. */
12801#if 0
12802 f->output_data.x->wm_hints.icon_pixmap = None;
12803#else
12804 return;
12805#endif
12806 }
b1c884c3 12807
d2bd6bc4
RS
12808#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12809
12810 {
12811 Arg al[1];
12812 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12813 XtSetValues (f->output_data.x->widget, al, 1);
12814 }
12815
12816#else /* not USE_X_TOOLKIT */
12817
7556890b
RS
12818 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12819 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12820
12821#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12822}
12823
dfcf069d 12824void
f676886a
JB
12825x_wm_set_icon_position (f, icon_x, icon_y)
12826 struct frame *f;
dc6f92b8
JB
12827 int icon_x, icon_y;
12828{
75231bad 12829#ifdef USE_X_TOOLKIT
7556890b 12830 Window window = XtWindow (f->output_data.x->widget);
75231bad 12831#else
c118dd06 12832 Window window = FRAME_X_WINDOW (f);
75231bad 12833#endif
dc6f92b8 12834
7556890b
RS
12835 f->output_data.x->wm_hints.flags |= IconPositionHint;
12836 f->output_data.x->wm_hints.icon_x = icon_x;
12837 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12838
7556890b 12839 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12840}
12841
12842\f
06a2c219
GM
12843/***********************************************************************
12844 Fonts
12845 ***********************************************************************/
dc43ef94
KH
12846
12847/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12848
dc43ef94
KH
12849struct font_info *
12850x_get_font_info (f, font_idx)
12851 FRAME_PTR f;
12852 int font_idx;
12853{
12854 return (FRAME_X_FONT_TABLE (f) + font_idx);
12855}
12856
12857
9c11f79e
GM
12858/* Return a list of names of available fonts matching PATTERN on frame F.
12859
12860 If SIZE is > 0, it is the size (maximum bounds width) of fonts
12861 to be listed.
12862
12863 SIZE < 0 means include scalable fonts.
12864
12865 Frame F null means we have not yet created any frame on X, and
12866 consult the first display in x_display_list. MAXNAMES sets a limit
12867 on how many fonts to match. */
dc43ef94
KH
12868
12869Lisp_Object
12870x_list_fonts (f, pattern, size, maxnames)
9c11f79e 12871 struct frame *f;
dc43ef94
KH
12872 Lisp_Object pattern;
12873 int size;
12874 int maxnames;
12875{
06a2c219
GM
12876 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12877 Lisp_Object tem, second_best;
9c11f79e
GM
12878 struct x_display_info *dpyinfo
12879 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
12880 Display *dpy = dpyinfo->display;
09c6077f 12881 int try_XLoadQueryFont = 0;
53ca4657 12882 int count;
9c11f79e
GM
12883 int allow_scalable_fonts_p = 0;
12884
12885 if (size < 0)
12886 {
12887 allow_scalable_fonts_p = 1;
12888 size = 0;
12889 }
dc43ef94 12890
6b0efe73 12891 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12892 if (NILP (patterns))
12893 patterns = Fcons (pattern, Qnil);
81ba44e5 12894
09c6077f
KH
12895 if (maxnames == 1 && !size)
12896 /* We can return any single font matching PATTERN. */
12897 try_XLoadQueryFont = 1;
9a32686f 12898
8e713be6 12899 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12900 {
dc43ef94 12901 int num_fonts;
3e71d8f2 12902 char **names = NULL;
dc43ef94 12903
8e713be6 12904 pattern = XCAR (patterns);
536f4067
RS
12905 /* See if we cached the result for this particular query.
12906 The cache is an alist of the form:
9c11f79e
GM
12907 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
12908 tem = XCDR (dpyinfo->name_list_element);
12909 key = Fcons (Fcons (pattern, make_number (maxnames)),
12910 allow_scalable_fonts_p ? Qt : Qnil);
12911 list = Fassoc (key, tem);
12912 if (!NILP (list))
b5210ea7
KH
12913 {
12914 list = Fcdr_safe (list);
12915 /* We have a cashed list. Don't have to get the list again. */
12916 goto label_cached;
12917 }
12918
12919 /* At first, put PATTERN in the cache. */
09c6077f 12920
dc43ef94 12921 BLOCK_INPUT;
17d85edc
KH
12922 count = x_catch_errors (dpy);
12923
09c6077f
KH
12924 if (try_XLoadQueryFont)
12925 {
12926 XFontStruct *font;
12927 unsigned long value;
12928
12929 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12930 if (x_had_errors_p (dpy))
12931 {
12932 /* This error is perhaps due to insufficient memory on X
12933 server. Let's just ignore it. */
12934 font = NULL;
12935 x_clear_errors (dpy);
12936 }
12937
09c6077f
KH
12938 if (font
12939 && XGetFontProperty (font, XA_FONT, &value))
12940 {
12941 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12942 int len = strlen (name);
01c752b5 12943 char *tmp;
09c6077f 12944
6f6512e8
KH
12945 /* If DXPC (a Differential X Protocol Compressor)
12946 Ver.3.7 is running, XGetAtomName will return null
12947 string. We must avoid such a name. */
12948 if (len == 0)
12949 try_XLoadQueryFont = 0;
12950 else
12951 {
12952 num_fonts = 1;
12953 names = (char **) alloca (sizeof (char *));
12954 /* Some systems only allow alloca assigned to a
12955 simple var. */
12956 tmp = (char *) alloca (len + 1); names[0] = tmp;
12957 bcopy (name, names[0], len + 1);
12958 XFree (name);
12959 }
09c6077f
KH
12960 }
12961 else
12962 try_XLoadQueryFont = 0;
a083fd23
RS
12963
12964 if (font)
12965 XFreeFont (dpy, font);
09c6077f
KH
12966 }
12967
12968 if (!try_XLoadQueryFont)
17d85edc
KH
12969 {
12970 /* We try at least 10 fonts because XListFonts will return
12971 auto-scaled fonts at the head. */
12972 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12973 &num_fonts);
12974 if (x_had_errors_p (dpy))
12975 {
12976 /* This error is perhaps due to insufficient memory on X
12977 server. Let's just ignore it. */
12978 names = NULL;
12979 x_clear_errors (dpy);
12980 }
12981 }
12982
12983 x_uncatch_errors (dpy, count);
dc43ef94
KH
12984 UNBLOCK_INPUT;
12985
12986 if (names)
12987 {
12988 int i;
dc43ef94
KH
12989
12990 /* Make a list of all the fonts we got back.
12991 Store that in the font cache for the display. */
12992 for (i = 0; i < num_fonts; i++)
12993 {
06a2c219 12994 int width = 0;
dc43ef94 12995 char *p = names[i];
06a2c219
GM
12996 int average_width = -1, dashes = 0;
12997
dc43ef94 12998 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
12999 14 dashes, and the field value following 12th dash
13000 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13001 is usually too ugly to be used for editing. Let's
13002 ignore it. */
dc43ef94
KH
13003 while (*p)
13004 if (*p++ == '-')
13005 {
13006 dashes++;
13007 if (dashes == 7) /* PIXEL_SIZE field */
13008 width = atoi (p);
13009 else if (dashes == 12) /* AVERAGE_WIDTH field */
13010 average_width = atoi (p);
13011 }
9c11f79e
GM
13012
13013 if (allow_scalable_fonts_p
13014 || dashes < 14 || average_width != 0)
dc43ef94
KH
13015 {
13016 tem = build_string (names[i]);
13017 if (NILP (Fassoc (tem, list)))
13018 {
13019 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
13020 && ((fast_c_string_match_ignore_case
13021 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
13022 >= 0))
13023 /* We can set the value of PIXEL_SIZE to the
b5210ea7 13024 width of this font. */
dc43ef94
KH
13025 list = Fcons (Fcons (tem, make_number (width)), list);
13026 else
13027 /* For the moment, width is not known. */
13028 list = Fcons (Fcons (tem, Qnil), list);
13029 }
13030 }
13031 }
09c6077f
KH
13032 if (!try_XLoadQueryFont)
13033 XFreeFontNames (names);
dc43ef94
KH
13034 }
13035
b5210ea7 13036 /* Now store the result in the cache. */
9c11f79e
GM
13037 XCDR (dpyinfo->name_list_element)
13038 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
dc43ef94 13039
b5210ea7
KH
13040 label_cached:
13041 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 13042
b5210ea7
KH
13043 newlist = second_best = Qnil;
13044 /* Make a list of the fonts that have the right width. */
8e713be6 13045 for (; CONSP (list); list = XCDR (list))
b5210ea7 13046 {
536f4067
RS
13047 int found_size;
13048
8e713be6 13049 tem = XCAR (list);
dc43ef94 13050
8e713be6 13051 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
13052 continue;
13053 if (!size)
13054 {
8e713be6 13055 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
13056 continue;
13057 }
dc43ef94 13058
8e713be6 13059 if (!INTEGERP (XCDR (tem)))
dc43ef94 13060 {
b5210ea7 13061 /* Since we have not yet known the size of this font, we
9c11f79e 13062 must try slow function call XLoadQueryFont. */
dc43ef94
KH
13063 XFontStruct *thisinfo;
13064
13065 BLOCK_INPUT;
17d85edc 13066 count = x_catch_errors (dpy);
dc43ef94 13067 thisinfo = XLoadQueryFont (dpy,
8e713be6 13068 XSTRING (XCAR (tem))->data);
17d85edc
KH
13069 if (x_had_errors_p (dpy))
13070 {
13071 /* This error is perhaps due to insufficient memory on X
13072 server. Let's just ignore it. */
13073 thisinfo = NULL;
13074 x_clear_errors (dpy);
13075 }
13076 x_uncatch_errors (dpy, count);
dc43ef94
KH
13077 UNBLOCK_INPUT;
13078
13079 if (thisinfo)
13080 {
8e713be6 13081 XCDR (tem)
536f4067
RS
13082 = (thisinfo->min_bounds.width == 0
13083 ? make_number (0)
13084 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
13085 XFreeFont (dpy, thisinfo);
13086 }
13087 else
b5210ea7 13088 /* For unknown reason, the previous call of XListFont had
06a2c219 13089 returned a font which can't be opened. Record the size
b5210ea7 13090 as 0 not to try to open it again. */
8e713be6 13091 XCDR (tem) = make_number (0);
dc43ef94 13092 }
536f4067 13093
8e713be6 13094 found_size = XINT (XCDR (tem));
536f4067 13095 if (found_size == size)
8e713be6 13096 newlist = Fcons (XCAR (tem), newlist);
536f4067 13097 else if (found_size > 0)
b5210ea7 13098 {
536f4067 13099 if (NILP (second_best))
b5210ea7 13100 second_best = tem;
536f4067
RS
13101 else if (found_size < size)
13102 {
8e713be6
KR
13103 if (XINT (XCDR (second_best)) > size
13104 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
13105 second_best = tem;
13106 }
13107 else
13108 {
8e713be6
KR
13109 if (XINT (XCDR (second_best)) > size
13110 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
13111 second_best = tem;
13112 }
b5210ea7
KH
13113 }
13114 }
13115 if (!NILP (newlist))
13116 break;
13117 else if (!NILP (second_best))
13118 {
8e713be6 13119 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 13120 break;
dc43ef94 13121 }
dc43ef94
KH
13122 }
13123
13124 return newlist;
13125}
13126
06a2c219
GM
13127
13128#if GLYPH_DEBUG
13129
13130/* Check that FONT is valid on frame F. It is if it can be found in F's
13131 font table. */
13132
13133static void
13134x_check_font (f, font)
13135 struct frame *f;
13136 XFontStruct *font;
13137{
13138 int i;
13139 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13140
13141 xassert (font != NULL);
13142
13143 for (i = 0; i < dpyinfo->n_fonts; i++)
13144 if (dpyinfo->font_table[i].name
13145 && font == dpyinfo->font_table[i].font)
13146 break;
13147
13148 xassert (i < dpyinfo->n_fonts);
13149}
13150
13151#endif /* GLYPH_DEBUG != 0 */
13152
13153/* Set *W to the minimum width, *H to the minimum font height of FONT.
13154 Note: There are (broken) X fonts out there with invalid XFontStruct
13155 min_bounds contents. For example, handa@etl.go.jp reports that
13156 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13157 have font->min_bounds.width == 0. */
13158
13159static INLINE void
13160x_font_min_bounds (font, w, h)
13161 XFontStruct *font;
13162 int *w, *h;
13163{
13164 *h = FONT_HEIGHT (font);
13165 *w = font->min_bounds.width;
13166
13167 /* Try to handle the case where FONT->min_bounds has invalid
13168 contents. Since the only font known to have invalid min_bounds
13169 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13170 if (*w <= 0)
13171 *w = font->max_bounds.width;
13172}
13173
13174
13175/* Compute the smallest character width and smallest font height over
13176 all fonts available on frame F. Set the members smallest_char_width
13177 and smallest_font_height in F's x_display_info structure to
13178 the values computed. Value is non-zero if smallest_font_height or
13179 smallest_char_width become smaller than they were before. */
13180
13181static int
13182x_compute_min_glyph_bounds (f)
13183 struct frame *f;
13184{
13185 int i;
13186 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13187 XFontStruct *font;
13188 int old_width = dpyinfo->smallest_char_width;
13189 int old_height = dpyinfo->smallest_font_height;
13190
13191 dpyinfo->smallest_font_height = 100000;
13192 dpyinfo->smallest_char_width = 100000;
13193
13194 for (i = 0; i < dpyinfo->n_fonts; ++i)
13195 if (dpyinfo->font_table[i].name)
13196 {
13197 struct font_info *fontp = dpyinfo->font_table + i;
13198 int w, h;
13199
13200 font = (XFontStruct *) fontp->font;
13201 xassert (font != (XFontStruct *) ~0);
13202 x_font_min_bounds (font, &w, &h);
13203
13204 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13205 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13206 }
13207
13208 xassert (dpyinfo->smallest_char_width > 0
13209 && dpyinfo->smallest_font_height > 0);
13210
13211 return (dpyinfo->n_fonts == 1
13212 || dpyinfo->smallest_char_width < old_width
13213 || dpyinfo->smallest_font_height < old_height);
13214}
13215
13216
dc43ef94
KH
13217/* Load font named FONTNAME of the size SIZE for frame F, and return a
13218 pointer to the structure font_info while allocating it dynamically.
13219 If SIZE is 0, load any size of font.
13220 If loading is failed, return NULL. */
13221
13222struct font_info *
13223x_load_font (f, fontname, size)
13224 struct frame *f;
13225 register char *fontname;
13226 int size;
13227{
13228 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13229 Lisp_Object font_names;
d645aaa4 13230 int count;
dc43ef94
KH
13231
13232 /* Get a list of all the fonts that match this name. Once we
13233 have a list of matching fonts, we compare them against the fonts
13234 we already have by comparing names. */
09c6077f 13235 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13236
13237 if (!NILP (font_names))
13238 {
13239 Lisp_Object tail;
13240 int i;
13241
13242 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13243 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13244 if (dpyinfo->font_table[i].name
13245 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13246 XSTRING (XCAR (tail))->data)
06a2c219 13247 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13248 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13249 return (dpyinfo->font_table + i);
13250 }
13251
13252 /* Load the font and add it to the table. */
13253 {
13254 char *full_name;
13255 XFontStruct *font;
13256 struct font_info *fontp;
13257 unsigned long value;
06a2c219 13258 int i;
dc43ef94 13259
2da424f1
KH
13260 /* If we have found fonts by x_list_font, load one of them. If
13261 not, we still try to load a font by the name given as FONTNAME
13262 because XListFonts (called in x_list_font) of some X server has
13263 a bug of not finding a font even if the font surely exists and
13264 is loadable by XLoadQueryFont. */
e1d6d5b9 13265 if (size > 0 && !NILP (font_names))
8e713be6 13266 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13267
13268 BLOCK_INPUT;
d645aaa4 13269 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13270 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13271 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13272 {
13273 /* This error is perhaps due to insufficient memory on X
13274 server. Let's just ignore it. */
13275 font = NULL;
13276 x_clear_errors (FRAME_X_DISPLAY (f));
13277 }
13278 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13279 UNBLOCK_INPUT;
b5210ea7 13280 if (!font)
dc43ef94
KH
13281 return NULL;
13282
06a2c219
GM
13283 /* Find a free slot in the font table. */
13284 for (i = 0; i < dpyinfo->n_fonts; ++i)
13285 if (dpyinfo->font_table[i].name == NULL)
13286 break;
13287
13288 /* If no free slot found, maybe enlarge the font table. */
13289 if (i == dpyinfo->n_fonts
13290 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13291 {
06a2c219
GM
13292 int sz;
13293 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13294 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13295 dpyinfo->font_table
06a2c219 13296 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13297 }
13298
06a2c219
GM
13299 fontp = dpyinfo->font_table + i;
13300 if (i == dpyinfo->n_fonts)
13301 ++dpyinfo->n_fonts;
dc43ef94
KH
13302
13303 /* Now fill in the slots of *FONTP. */
13304 BLOCK_INPUT;
13305 fontp->font = font;
06a2c219 13306 fontp->font_idx = i;
dc43ef94
KH
13307 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13308 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13309
13310 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13311 full_name = 0;
13312 if (XGetFontProperty (font, XA_FONT, &value))
13313 {
13314 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13315 char *p = name;
13316 int dashes = 0;
13317
13318 /* Count the number of dashes in the "full name".
13319 If it is too few, this isn't really the font's full name,
13320 so don't use it.
13321 In X11R4, the fonts did not come with their canonical names
13322 stored in them. */
13323 while (*p)
13324 {
13325 if (*p == '-')
13326 dashes++;
13327 p++;
13328 }
13329
13330 if (dashes >= 13)
13331 {
13332 full_name = (char *) xmalloc (p - name + 1);
13333 bcopy (name, full_name, p - name + 1);
13334 }
13335
13336 XFree (name);
13337 }
13338
13339 if (full_name != 0)
13340 fontp->full_name = full_name;
13341 else
13342 fontp->full_name = fontp->name;
13343
13344 fontp->size = font->max_bounds.width;
d5749adb 13345 fontp->height = FONT_HEIGHT (font);
dc43ef94 13346
2da424f1
KH
13347 if (NILP (font_names))
13348 {
13349 /* We come here because of a bug of XListFonts mentioned at
13350 the head of this block. Let's store this information in
13351 the cache for x_list_fonts. */
13352 Lisp_Object lispy_name = build_string (fontname);
13353 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
13354 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13355 Qnil);
2da424f1 13356
8e713be6 13357 XCDR (dpyinfo->name_list_element)
9c11f79e 13358 = Fcons (Fcons (key,
2da424f1
KH
13359 Fcons (Fcons (lispy_full_name,
13360 make_number (fontp->size)),
13361 Qnil)),
8e713be6 13362 XCDR (dpyinfo->name_list_element));
2da424f1 13363 if (full_name)
9c11f79e
GM
13364 {
13365 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13366 Qnil);
13367 XCDR (dpyinfo->name_list_element)
13368 = Fcons (Fcons (key,
13369 Fcons (Fcons (lispy_full_name,
13370 make_number (fontp->size)),
13371 Qnil)),
13372 XCDR (dpyinfo->name_list_element));
13373 }
2da424f1
KH
13374 }
13375
dc43ef94
KH
13376 /* The slot `encoding' specifies how to map a character
13377 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13378 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13379 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13380 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13381 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13382 which is never used by any charset. If mapping can't be
13383 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13384 fontp->encoding[1]
13385 = (font->max_byte1 == 0
13386 /* 1-byte font */
13387 ? (font->min_char_or_byte2 < 0x80
13388 ? (font->max_char_or_byte2 < 0x80
13389 ? 0 /* 0x20..0x7F */
8ff102bd 13390 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13391 : 1) /* 0xA0..0xFF */
13392 /* 2-byte font */
13393 : (font->min_byte1 < 0x80
13394 ? (font->max_byte1 < 0x80
13395 ? (font->min_char_or_byte2 < 0x80
13396 ? (font->max_char_or_byte2 < 0x80
13397 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13398 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13399 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13400 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13401 : (font->min_char_or_byte2 < 0x80
13402 ? (font->max_char_or_byte2 < 0x80
13403 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13404 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13405 : 1))); /* 0xA0A0..0xFFFF */
13406
13407 fontp->baseline_offset
13408 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13409 ? (long) value : 0);
13410 fontp->relative_compose
13411 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13412 ? (long) value : 0);
f78798df
KH
13413 fontp->default_ascent
13414 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13415 ? (long) value : 0);
dc43ef94 13416
06a2c219
GM
13417 /* Set global flag fonts_changed_p to non-zero if the font loaded
13418 has a character with a smaller width than any other character
13419 before, or if the font loaded has a smalle>r height than any
13420 other font loaded before. If this happens, it will make a
13421 glyph matrix reallocation necessary. */
13422 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13423 UNBLOCK_INPUT;
dc43ef94
KH
13424 return fontp;
13425 }
13426}
13427
06a2c219
GM
13428
13429/* Return a pointer to struct font_info of a font named FONTNAME for
13430 frame F. If no such font is loaded, return NULL. */
13431
dc43ef94
KH
13432struct font_info *
13433x_query_font (f, fontname)
13434 struct frame *f;
13435 register char *fontname;
13436{
13437 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13438 int i;
13439
13440 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13441 if (dpyinfo->font_table[i].name
13442 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13443 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13444 return (dpyinfo->font_table + i);
13445 return NULL;
13446}
13447
06a2c219
GM
13448
13449/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13450 `encoder' of the structure. */
13451
13452void
13453x_find_ccl_program (fontp)
13454 struct font_info *fontp;
13455{
a42f54e6 13456 Lisp_Object list, elt;
a6582676 13457
f9b5db02 13458 elt = Qnil;
8e713be6 13459 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13460 {
8e713be6 13461 elt = XCAR (list);
a6582676 13462 if (CONSP (elt)
8e713be6 13463 && STRINGP (XCAR (elt))
9f2feff6
KH
13464 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13465 >= 0)
13466 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13467 >= 0)))
a42f54e6
KH
13468 break;
13469 }
f9b5db02 13470
a42f54e6
KH
13471 if (! NILP (list))
13472 {
d27f8ca7
KH
13473 struct ccl_program *ccl
13474 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13475
8e713be6 13476 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13477 xfree (ccl);
13478 else
13479 fontp->font_encoder = ccl;
a6582676
KH
13480 }
13481}
13482
06a2c219 13483
dc43ef94 13484\f
06a2c219
GM
13485/***********************************************************************
13486 Initialization
13487 ***********************************************************************/
f451eb13 13488
3afe33e7
RS
13489#ifdef USE_X_TOOLKIT
13490static XrmOptionDescRec emacs_options[] = {
13491 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13492 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13493
13494 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13495 XrmoptionSepArg, NULL},
13496 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13497
13498 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13499 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13500 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13501 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13502 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13503 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13504 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13505};
13506#endif /* USE_X_TOOLKIT */
13507
7a13e894
RS
13508static int x_initialized;
13509
29b38361
KH
13510#ifdef MULTI_KBOARD
13511/* Test whether two display-name strings agree up to the dot that separates
13512 the screen number from the server number. */
13513static int
13514same_x_server (name1, name2)
13515 char *name1, *name2;
13516{
13517 int seen_colon = 0;
cf591cc1
RS
13518 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13519 int system_name_length = strlen (system_name);
13520 int length_until_period = 0;
13521
13522 while (system_name[length_until_period] != 0
13523 && system_name[length_until_period] != '.')
13524 length_until_period++;
13525
13526 /* Treat `unix' like an empty host name. */
13527 if (! strncmp (name1, "unix:", 5))
13528 name1 += 4;
13529 if (! strncmp (name2, "unix:", 5))
13530 name2 += 4;
13531 /* Treat this host's name like an empty host name. */
13532 if (! strncmp (name1, system_name, system_name_length)
13533 && name1[system_name_length] == ':')
13534 name1 += system_name_length;
13535 if (! strncmp (name2, system_name, system_name_length)
13536 && name2[system_name_length] == ':')
13537 name2 += system_name_length;
13538 /* Treat this host's domainless name like an empty host name. */
13539 if (! strncmp (name1, system_name, length_until_period)
13540 && name1[length_until_period] == ':')
13541 name1 += length_until_period;
13542 if (! strncmp (name2, system_name, length_until_period)
13543 && name2[length_until_period] == ':')
13544 name2 += length_until_period;
13545
29b38361
KH
13546 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13547 {
13548 if (*name1 == ':')
13549 seen_colon++;
13550 if (seen_colon && *name1 == '.')
13551 return 1;
13552 }
13553 return (seen_colon
13554 && (*name1 == '.' || *name1 == '\0')
13555 && (*name2 == '.' || *name2 == '\0'));
13556}
13557#endif
13558
334208b7 13559struct x_display_info *
1f8255f2 13560x_term_init (display_name, xrm_option, resource_name)
334208b7 13561 Lisp_Object display_name;
1f8255f2
RS
13562 char *xrm_option;
13563 char *resource_name;
dc6f92b8 13564{
334208b7 13565 int connection;
7a13e894 13566 Display *dpy;
334208b7
RS
13567 struct x_display_info *dpyinfo;
13568 XrmDatabase xrdb;
13569
60439948
KH
13570 BLOCK_INPUT;
13571
7a13e894
RS
13572 if (!x_initialized)
13573 {
13574 x_initialize ();
13575 x_initialized = 1;
13576 }
dc6f92b8 13577
3afe33e7 13578#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13579 /* weiner@footloose.sps.mot.com reports that this causes
13580 errors with X11R5:
13581 X protocol error: BadAtom (invalid Atom parameter)
13582 on protocol request 18skiloaf.
13583 So let's not use it until R6. */
13584#ifdef HAVE_X11XTR6
bdcd49ba
RS
13585 XtSetLanguageProc (NULL, NULL, NULL);
13586#endif
13587
7f9c7f94
RS
13588 {
13589 int argc = 0;
13590 char *argv[3];
13591
13592 argv[0] = "";
13593 argc = 1;
13594 if (xrm_option)
13595 {
13596 argv[argc++] = "-xrm";
13597 argv[argc++] = xrm_option;
13598 }
13599 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13600 resource_name, EMACS_CLASS,
13601 emacs_options, XtNumber (emacs_options),
13602 &argc, argv);
39d8bb4d
KH
13603
13604#ifdef HAVE_X11XTR6
10537cb1 13605 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13606 fixup_locale ();
39d8bb4d 13607#endif
7f9c7f94 13608 }
3afe33e7
RS
13609
13610#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13611#ifdef HAVE_X11R5
13612 XSetLocaleModifiers ("");
13613#endif
7a13e894 13614 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13615#endif /* not USE_X_TOOLKIT */
334208b7 13616
7a13e894
RS
13617 /* Detect failure. */
13618 if (dpy == 0)
60439948
KH
13619 {
13620 UNBLOCK_INPUT;
13621 return 0;
13622 }
7a13e894
RS
13623
13624 /* We have definitely succeeded. Record the new connection. */
13625
13626 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13627 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13628
29b38361
KH
13629#ifdef MULTI_KBOARD
13630 {
13631 struct x_display_info *share;
13632 Lisp_Object tail;
13633
13634 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13635 share = share->next, tail = XCDR (tail))
13636 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13637 XSTRING (display_name)->data))
13638 break;
13639 if (share)
13640 dpyinfo->kboard = share->kboard;
13641 else
13642 {
13643 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13644 init_kboard (dpyinfo->kboard);
59e755be
KH
13645 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13646 {
13647 char *vendor = ServerVendor (dpy);
9b6ed9f3 13648 UNBLOCK_INPUT;
59e755be
KH
13649 dpyinfo->kboard->Vsystem_key_alist
13650 = call1 (Qvendor_specific_keysyms,
13651 build_string (vendor ? vendor : ""));
9b6ed9f3 13652 BLOCK_INPUT;
59e755be
KH
13653 }
13654
29b38361
KH
13655 dpyinfo->kboard->next_kboard = all_kboards;
13656 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13657 /* Don't let the initial kboard remain current longer than necessary.
13658 That would cause problems if a file loaded on startup tries to
06a2c219 13659 prompt in the mini-buffer. */
0ad5446c
KH
13660 if (current_kboard == initial_kboard)
13661 current_kboard = dpyinfo->kboard;
29b38361
KH
13662 }
13663 dpyinfo->kboard->reference_count++;
13664 }
b9737ad3
KH
13665#endif
13666
7a13e894
RS
13667 /* Put this display on the chain. */
13668 dpyinfo->next = x_display_list;
13669 x_display_list = dpyinfo;
13670
13671 /* Put it on x_display_name_list as well, to keep them parallel. */
13672 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13673 x_display_name_list);
8e713be6 13674 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13675
13676 dpyinfo->display = dpy;
dc6f92b8 13677
dc6f92b8 13678#if 0
7a13e894 13679 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13680#endif /* ! 0 */
7a13e894
RS
13681
13682 dpyinfo->x_id_name
fc932ac6
RS
13683 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13684 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13685 + 2);
13686 sprintf (dpyinfo->x_id_name, "%s@%s",
13687 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13688
13689 /* Figure out which modifier bits mean what. */
334208b7 13690 x_find_modifier_meanings (dpyinfo);
f451eb13 13691
ab648270 13692 /* Get the scroll bar cursor. */
7a13e894 13693 dpyinfo->vertical_scroll_bar_cursor
334208b7 13694 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13695
334208b7
RS
13696 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13697 resource_name, EMACS_CLASS);
13698#ifdef HAVE_XRMSETDATABASE
13699 XrmSetDatabase (dpyinfo->display, xrdb);
13700#else
13701 dpyinfo->display->db = xrdb;
13702#endif
547d9db8 13703 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13704 all versions. */
13705 dpyinfo->xrdb = xrdb;
334208b7
RS
13706
13707 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13708 DefaultScreen (dpyinfo->display));
5ff67d81 13709 select_visual (dpyinfo);
43bd1b2b 13710 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13711 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13712 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13713 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13714 dpyinfo->grabbed = 0;
13715 dpyinfo->reference_count = 0;
13716 dpyinfo->icon_bitmap_id = -1;
06a2c219 13717 dpyinfo->font_table = NULL;
7a13e894
RS
13718 dpyinfo->n_fonts = 0;
13719 dpyinfo->font_table_size = 0;
13720 dpyinfo->bitmaps = 0;
13721 dpyinfo->bitmaps_size = 0;
13722 dpyinfo->bitmaps_last = 0;
13723 dpyinfo->scratch_cursor_gc = 0;
13724 dpyinfo->mouse_face_mouse_frame = 0;
13725 dpyinfo->mouse_face_deferred_gc = 0;
13726 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13727 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13728 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13729 dpyinfo->mouse_face_window = Qnil;
13730 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13731 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13732 dpyinfo->x_focus_frame = 0;
13733 dpyinfo->x_focus_event_frame = 0;
13734 dpyinfo->x_highlight_frame = 0;
06a2c219 13735 dpyinfo->image_cache = make_image_cache ();
334208b7 13736
43bd1b2b 13737 /* See if a private colormap is requested. */
5ff67d81
GM
13738 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13739 {
13740 if (dpyinfo->visual->class == PseudoColor)
13741 {
13742 Lisp_Object value;
13743 value = display_x_get_resource (dpyinfo,
13744 build_string ("privateColormap"),
13745 build_string ("PrivateColormap"),
13746 Qnil, Qnil);
13747 if (STRINGP (value)
13748 && (!strcmp (XSTRING (value)->data, "true")
13749 || !strcmp (XSTRING (value)->data, "on")))
13750 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13751 }
43bd1b2b 13752 }
5ff67d81
GM
13753 else
13754 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13755 dpyinfo->visual, AllocNone);
43bd1b2b 13756
06a2c219
GM
13757 {
13758 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13759 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13760 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13761 dpyinfo->resy = pixels * 25.4 / mm;
13762 pixels = DisplayWidth (dpyinfo->display, screen_number);
13763 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13764 dpyinfo->resx = pixels * 25.4 / mm;
13765 }
13766
334208b7
RS
13767 dpyinfo->Xatom_wm_protocols
13768 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13769 dpyinfo->Xatom_wm_take_focus
13770 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13771 dpyinfo->Xatom_wm_save_yourself
13772 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13773 dpyinfo->Xatom_wm_delete_window
13774 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13775 dpyinfo->Xatom_wm_change_state
13776 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13777 dpyinfo->Xatom_wm_configure_denied
13778 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13779 dpyinfo->Xatom_wm_window_moved
13780 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13781 dpyinfo->Xatom_editres
13782 = XInternAtom (dpyinfo->display, "Editres", False);
13783 dpyinfo->Xatom_CLIPBOARD
13784 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13785 dpyinfo->Xatom_TIMESTAMP
13786 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13787 dpyinfo->Xatom_TEXT
13788 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13789 dpyinfo->Xatom_COMPOUND_TEXT
13790 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13791 dpyinfo->Xatom_DELETE
13792 = XInternAtom (dpyinfo->display, "DELETE", False);
13793 dpyinfo->Xatom_MULTIPLE
13794 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13795 dpyinfo->Xatom_INCR
13796 = XInternAtom (dpyinfo->display, "INCR", False);
13797 dpyinfo->Xatom_EMACS_TMP
13798 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13799 dpyinfo->Xatom_TARGETS
13800 = XInternAtom (dpyinfo->display, "TARGETS", False);
13801 dpyinfo->Xatom_NULL
13802 = XInternAtom (dpyinfo->display, "NULL", False);
13803 dpyinfo->Xatom_ATOM_PAIR
13804 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13805 /* For properties of font. */
13806 dpyinfo->Xatom_PIXEL_SIZE
13807 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13808 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13809 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13810 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13811 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13812 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13813 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13814
06a2c219
GM
13815 /* Ghostscript support. */
13816 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13817 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13818
13819 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13820 False);
13821
547d9db8
KH
13822 dpyinfo->cut_buffers_initialized = 0;
13823
334208b7
RS
13824 connection = ConnectionNumber (dpyinfo->display);
13825 dpyinfo->connection = connection;
13826
dc43ef94 13827 {
5d7cc324
RS
13828 char null_bits[1];
13829
13830 null_bits[0] = 0x00;
dc43ef94
KH
13831
13832 dpyinfo->null_pixel
13833 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13834 null_bits, 1, 1, (long) 0, (long) 0,
13835 1);
13836 }
13837
06a2c219
GM
13838 {
13839 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 13840 extern char *gray_bitmap_bits;
06a2c219
GM
13841 dpyinfo->gray
13842 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13843 gray_bitmap_bits,
13844 gray_bitmap_width, gray_bitmap_height,
13845 (unsigned long) 1, (unsigned long) 0, 1);
13846 }
13847
f5d11644
GM
13848#ifdef HAVE_X_I18N
13849 xim_initialize (dpyinfo, resource_name);
13850#endif
13851
87485d6f
MW
13852#ifdef subprocesses
13853 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13854 if (connection != 0)
7a13e894 13855 add_keyboard_wait_descriptor (connection);
87485d6f 13856#endif
6d4238f3 13857
041b69ac 13858#ifndef F_SETOWN_BUG
dc6f92b8 13859#ifdef F_SETOWN
dc6f92b8 13860#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13861 /* stdin is a socket here */
334208b7 13862 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13863#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13864 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13865#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13866#endif /* ! defined (F_SETOWN) */
041b69ac 13867#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13868
13869#ifdef SIGIO
eee20f6a
KH
13870 if (interrupt_input)
13871 init_sigio (connection);
c118dd06 13872#endif /* ! defined (SIGIO) */
dc6f92b8 13873
51b592fb 13874#ifdef USE_LUCID
f8c39f51 13875#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13876 /* Make sure that we have a valid font for dialog boxes
13877 so that Xt does not crash. */
13878 {
13879 Display *dpy = dpyinfo->display;
13880 XrmValue d, fr, to;
13881 Font font;
e99db5a1 13882 int count;
51b592fb
RS
13883
13884 d.addr = (XPointer)&dpy;
13885 d.size = sizeof (Display *);
13886 fr.addr = XtDefaultFont;
13887 fr.size = sizeof (XtDefaultFont);
13888 to.size = sizeof (Font *);
13889 to.addr = (XPointer)&font;
e99db5a1 13890 count = x_catch_errors (dpy);
51b592fb
RS
13891 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13892 abort ();
13893 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13894 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13895 x_uncatch_errors (dpy, count);
51b592fb
RS
13896 }
13897#endif
f8c39f51 13898#endif
51b592fb 13899
34e23e5a
GM
13900 /* See if we should run in synchronous mode. This is useful
13901 for debugging X code. */
13902 {
13903 Lisp_Object value;
13904 value = display_x_get_resource (dpyinfo,
13905 build_string ("synchronous"),
13906 build_string ("Synchronous"),
13907 Qnil, Qnil);
13908 if (STRINGP (value)
13909 && (!strcmp (XSTRING (value)->data, "true")
13910 || !strcmp (XSTRING (value)->data, "on")))
13911 XSynchronize (dpyinfo->display, True);
13912 }
13913
60439948
KH
13914 UNBLOCK_INPUT;
13915
7a13e894
RS
13916 return dpyinfo;
13917}
13918\f
13919/* Get rid of display DPYINFO, assuming all frames are already gone,
13920 and without sending any more commands to the X server. */
dc6f92b8 13921
7a13e894
RS
13922void
13923x_delete_display (dpyinfo)
13924 struct x_display_info *dpyinfo;
13925{
13926 delete_keyboard_wait_descriptor (dpyinfo->connection);
13927
13928 /* Discard this display from x_display_name_list and x_display_list.
13929 We can't use Fdelq because that can quit. */
13930 if (! NILP (x_display_name_list)
8e713be6
KR
13931 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13932 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13933 else
13934 {
13935 Lisp_Object tail;
13936
13937 tail = x_display_name_list;
8e713be6 13938 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13939 {
bffcfca9 13940 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13941 {
8e713be6 13942 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13943 break;
13944 }
8e713be6 13945 tail = XCDR (tail);
7a13e894
RS
13946 }
13947 }
13948
9bda743f
GM
13949 if (next_noop_dpyinfo == dpyinfo)
13950 next_noop_dpyinfo = dpyinfo->next;
13951
7a13e894
RS
13952 if (x_display_list == dpyinfo)
13953 x_display_list = dpyinfo->next;
7f9c7f94
RS
13954 else
13955 {
13956 struct x_display_info *tail;
7a13e894 13957
7f9c7f94
RS
13958 for (tail = x_display_list; tail; tail = tail->next)
13959 if (tail->next == dpyinfo)
13960 tail->next = tail->next->next;
13961 }
7a13e894 13962
0d777288
RS
13963#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13964#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13965 XrmDestroyDatabase (dpyinfo->xrdb);
13966#endif
0d777288 13967#endif
29b38361
KH
13968#ifdef MULTI_KBOARD
13969 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13970 delete_kboard (dpyinfo->kboard);
b9737ad3 13971#endif
f5d11644
GM
13972#ifdef HAVE_X_I18N
13973 if (dpyinfo->xim)
13974 xim_close_dpy (dpyinfo);
13975#endif
13976
b9737ad3
KH
13977 xfree (dpyinfo->font_table);
13978 xfree (dpyinfo->x_id_name);
f04e1297 13979 xfree (dpyinfo->color_cells);
b9737ad3 13980 xfree (dpyinfo);
7a13e894 13981}
f04e1297 13982
7a13e894
RS
13983\f
13984/* Set up use of X before we make the first connection. */
13985
06a2c219
GM
13986static struct redisplay_interface x_redisplay_interface =
13987{
13988 x_produce_glyphs,
13989 x_write_glyphs,
13990 x_insert_glyphs,
13991 x_clear_end_of_line,
13992 x_scroll_run,
13993 x_after_update_window_line,
13994 x_update_window_begin,
13995 x_update_window_end,
13996 XTcursor_to,
13997 x_flush,
71b8321e 13998 x_clear_mouse_face,
66ac4b0e
GM
13999 x_get_glyph_overhangs,
14000 x_fix_overlapping_area
06a2c219
GM
14001};
14002
dfcf069d 14003void
7a13e894
RS
14004x_initialize ()
14005{
06a2c219
GM
14006 rif = &x_redisplay_interface;
14007
14008 clear_frame_hook = x_clear_frame;
14009 ins_del_lines_hook = x_ins_del_lines;
14010 change_line_highlight_hook = x_change_line_highlight;
14011 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
14012 ring_bell_hook = XTring_bell;
14013 reset_terminal_modes_hook = XTreset_terminal_modes;
14014 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
14015 update_begin_hook = x_update_begin;
14016 update_end_hook = x_update_end;
dc6f92b8
JB
14017 set_terminal_window_hook = XTset_terminal_window;
14018 read_socket_hook = XTread_socket;
b8009dd1 14019 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 14020 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 14021 mouse_position_hook = XTmouse_position;
f451eb13 14022 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 14023 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
14024 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14025 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14026 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14027 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 14028 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 14029
f676886a 14030 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 14031 char_ins_del_ok = 1;
dc6f92b8
JB
14032 line_ins_del_ok = 1; /* we'll just blt 'em */
14033 fast_clear_end_of_line = 1; /* X does this well */
58769bee 14034 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
14035 off the bottom */
14036 baud_rate = 19200;
14037
7a13e894 14038 x_noop_count = 0;
9ea173e8 14039 last_tool_bar_item = -1;
06a2c219
GM
14040 any_help_event_p = 0;
14041
b30b24cb
RS
14042 /* Try to use interrupt input; if we can't, then start polling. */
14043 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14044
7f9c7f94
RS
14045#ifdef USE_X_TOOLKIT
14046 XtToolkitInitialize ();
14047 Xt_app_con = XtCreateApplicationContext ();
665881ad 14048 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
14049
14050 /* Install an asynchronous timer that processes Xt timeout events
14051 every 0.1s. This is necessary because some widget sets use
14052 timeouts internally, for example the LessTif menu bar, or the
14053 Xaw3d scroll bar. When Xt timouts aren't processed, these
14054 widgets don't behave normally. */
14055 {
14056 EMACS_TIME interval;
14057 EMACS_SET_SECS_USECS (interval, 0, 100000);
14058 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14059 }
db74249b 14060#endif
bffcfca9 14061
eccc05db 14062#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
14063 xaw3d_arrow_scroll = False;
14064 xaw3d_pick_top = True;
7f9c7f94
RS
14065#endif
14066
58769bee 14067 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 14068 original error handler. */
e99db5a1 14069 XSetErrorHandler (x_error_handler);
334208b7 14070 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 14071
06a2c219 14072 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
14073#ifdef SIGWINCH
14074 signal (SIGWINCH, SIG_DFL);
c118dd06 14075#endif /* ! defined (SIGWINCH) */
dc6f92b8 14076
92e2441b 14077 signal (SIGPIPE, x_connection_signal);
dc6f92b8 14078}
55123275 14079
06a2c219 14080
55123275
JB
14081void
14082syms_of_xterm ()
14083{
e99db5a1
RS
14084 staticpro (&x_error_message_string);
14085 x_error_message_string = Qnil;
14086
7a13e894
RS
14087 staticpro (&x_display_name_list);
14088 x_display_name_list = Qnil;
334208b7 14089
ab648270 14090 staticpro (&last_mouse_scroll_bar);
e53cb100 14091 last_mouse_scroll_bar = Qnil;
59e755be
KH
14092
14093 staticpro (&Qvendor_specific_keysyms);
14094 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
14095
14096 staticpro (&last_mouse_press_frame);
14097 last_mouse_press_frame = Qnil;
06a2c219 14098
06a2c219 14099 help_echo = Qnil;
be010514
GM
14100 staticpro (&help_echo);
14101 help_echo_object = Qnil;
14102 staticpro (&help_echo_object);
7cea38bc
GM
14103 help_echo_window = Qnil;
14104 staticpro (&help_echo_window);
06a2c219 14105 previous_help_echo = Qnil;
be010514
GM
14106 staticpro (&previous_help_echo);
14107 help_echo_pos = -1;
06a2c219
GM
14108
14109 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14110 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14111For example, if a block cursor is over a tab, it will be drawn as\n\
14112wide as that tab on the display.");
14113 x_stretch_cursor_p = 0;
14114
5bf04520
GM
14115 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14116 "What X toolkit scroll bars Emacs uses.\n\
14117A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14118Otherwise, value is a symbol describing the X toolkit.");
eccc05db 14119#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
14120#ifdef USE_MOTIF
14121 Vx_toolkit_scroll_bars = intern ("motif");
14122#elif defined HAVE_XAW3D
14123 Vx_toolkit_scroll_bars = intern ("xaw3d");
14124#else
14125 Vx_toolkit_scroll_bars = intern ("xaw");
14126#endif
06a2c219 14127#else
5bf04520 14128 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
14129#endif
14130
06a2c219
GM
14131 staticpro (&last_mouse_motion_frame);
14132 last_mouse_motion_frame = Qnil;
55123275 14133}
6cf0ae86
RS
14134
14135#endif /* not HAVE_X_WINDOWS */