(toplevel): Don't require `gnus-util' at
[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
f04e1297 399static const XColor *x_color_cells P_ ((struct frame *, int *));
71b8321e 400static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
401static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402void x_delete_display P_ ((struct x_display_info *));
403static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407static void set_output_cursor P_ ((struct cursor_pos *));
408static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *));
410static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 411static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
412static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
413static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
414static void show_mouse_face P_ ((struct x_display_info *,
415 enum draw_glyphs_face));
416static int x_io_error_quitter P_ ((Display *));
417int x_catch_errors P_ ((Display *));
418void x_uncatch_errors P_ ((Display *, int));
419void x_lower_frame P_ ((struct frame *));
420void x_scroll_bar_clear P_ ((struct frame *));
421int x_had_errors_p P_ ((Display *));
422void x_wm_set_size_hint P_ ((struct frame *, long, int));
423void x_raise_frame P_ ((struct frame *));
424void x_set_window_size P_ ((struct frame *, int, int, int));
425void x_wm_set_window_state P_ ((struct frame *, int));
426void x_wm_set_icon_pixmap P_ ((struct frame *, int));
427void x_initialize P_ ((void));
428static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
429static int x_compute_min_glyph_bounds P_ ((struct frame *));
430static void x_draw_phys_cursor_glyph P_ ((struct window *,
431 struct glyph_row *,
432 enum draw_glyphs_face));
433static void x_update_end P_ ((struct frame *));
434static void XTframe_up_to_date P_ ((struct frame *));
435static void XTreassert_line_highlight P_ ((int, int));
436static void x_change_line_highlight P_ ((int, int, int, int));
437static void XTset_terminal_modes P_ ((void));
438static void XTreset_terminal_modes P_ ((void));
439static void XTcursor_to P_ ((int, int, int, int));
440static void x_write_glyphs P_ ((struct glyph *, int));
441static void x_clear_end_of_line P_ ((int));
442static void x_clear_frame P_ ((void));
443static void x_clear_cursor P_ ((struct window *));
444static void frame_highlight P_ ((struct frame *));
445static void frame_unhighlight P_ ((struct frame *));
446static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
447static void XTframe_rehighlight P_ ((struct frame *));
448static void x_frame_rehighlight P_ ((struct x_display_info *));
449static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 450static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
451static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
452 XRectangle *));
453static void expose_frame P_ ((struct frame *, int, int, int, int));
454static void expose_window_tree P_ ((struct window *, XRectangle *));
455static void expose_window P_ ((struct window *, XRectangle *));
456static void expose_area P_ ((struct window *, struct glyph_row *,
457 XRectangle *, enum glyph_row_area));
458static void expose_line P_ ((struct window *, struct glyph_row *,
459 XRectangle *));
460static void x_update_cursor_in_window_tree P_ ((struct window *, int));
461static void x_update_window_cursor P_ ((struct window *, int));
462static void x_erase_phys_cursor P_ ((struct window *));
463void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
464static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
465 enum bitmap_type));
466
467static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
468 GC, int));
469static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
470static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
471static void note_overwritten_text_cursor P_ ((struct window *, int, int));
472static void x_flush P_ ((struct frame *f));
952291d9
GM
473static void x_update_begin P_ ((struct frame *));
474static void x_update_window_begin P_ ((struct window *));
475static void x_draw_vertical_border P_ ((struct window *));
476static void x_after_update_window_line P_ ((struct glyph_row *));
477static INLINE void take_vertical_position_into_account P_ ((struct it *));
478static void x_produce_stretch_glyph P_ ((struct it *));
479
06a2c219
GM
480
481/* Flush display of frame F, or of all frames if F is null. */
482
483static void
484x_flush (f)
485 struct frame *f;
486{
487 BLOCK_INPUT;
488 if (f == NULL)
489 {
490 Lisp_Object rest, frame;
491 FOR_EACH_FRAME (rest, frame)
492 x_flush (XFRAME (frame));
493 }
494 else if (FRAME_X_P (f))
495 XFlush (FRAME_X_DISPLAY (f));
496 UNBLOCK_INPUT;
497}
498
dc6f92b8 499
06a2c219
GM
500/* Remove calls to XFlush by defining XFlush to an empty replacement.
501 Calls to XFlush should be unnecessary because the X output buffer
502 is flushed automatically as needed by calls to XPending,
503 XNextEvent, or XWindowEvent according to the XFlush man page.
504 XTread_socket calls XPending. Removing XFlush improves
505 performance. */
506
507#define XFlush(DISPLAY) (void) 0
b8009dd1 508
334208b7 509\f
06a2c219
GM
510/***********************************************************************
511 Debugging
512 ***********************************************************************/
513
9382638d 514#if 0
06a2c219
GM
515
516/* This is a function useful for recording debugging information about
517 the sequence of occurrences in this file. */
9382638d
KH
518
519struct record
520{
521 char *locus;
522 int type;
523};
524
525struct record event_record[100];
526
527int event_record_index;
528
529record_event (locus, type)
530 char *locus;
531 int type;
532{
533 if (event_record_index == sizeof (event_record) / sizeof (struct record))
534 event_record_index = 0;
535
536 event_record[event_record_index].locus = locus;
537 event_record[event_record_index].type = type;
538 event_record_index++;
539}
540
541#endif /* 0 */
06a2c219
GM
542
543
9382638d 544\f
334208b7
RS
545/* Return the struct x_display_info corresponding to DPY. */
546
547struct x_display_info *
548x_display_info_for_display (dpy)
549 Display *dpy;
550{
551 struct x_display_info *dpyinfo;
552
553 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
554 if (dpyinfo->display == dpy)
555 return dpyinfo;
16bd92ea 556
334208b7
RS
557 return 0;
558}
f451eb13 559
06a2c219
GM
560
561\f
562/***********************************************************************
563 Starting and ending an update
564 ***********************************************************************/
565
566/* Start an update of frame F. This function is installed as a hook
567 for update_begin, i.e. it is called when update_begin is called.
568 This function is called prior to calls to x_update_window_begin for
569 each window being updated. Currently, there is nothing to do here
570 because all interesting stuff is done on a window basis. */
dc6f92b8 571
dfcf069d 572static void
06a2c219 573x_update_begin (f)
f676886a 574 struct frame *f;
58769bee 575{
06a2c219
GM
576 /* Nothing to do. */
577}
dc6f92b8 578
dc6f92b8 579
06a2c219
GM
580/* Start update of window W. Set the global variable updated_window
581 to the window being updated and set output_cursor to the cursor
582 position of W. */
dc6f92b8 583
06a2c219
GM
584static void
585x_update_window_begin (w)
586 struct window *w;
587{
588 struct frame *f = XFRAME (WINDOW_FRAME (w));
589 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
590
591 updated_window = w;
592 set_output_cursor (&w->cursor);
b8009dd1 593
06a2c219 594 BLOCK_INPUT;
d1bc4182 595
06a2c219 596 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 597 {
514e4681 598 /* Don't do highlighting for mouse motion during the update. */
06a2c219 599 display_info->mouse_face_defer = 1;
37c2c98b 600
06a2c219
GM
601 /* If F needs to be redrawn, simply forget about any prior mouse
602 highlighting. */
9f67f20b 603 if (FRAME_GARBAGED_P (f))
06a2c219
GM
604 display_info->mouse_face_window = Qnil;
605
64f26cf5
GM
606#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
607 their mouse_face_p flag set, which means that they are always
608 unequal to rows in a desired matrix which never have that
609 flag set. So, rows containing mouse-face glyphs are never
610 scrolled, and we don't have to switch the mouse highlight off
611 here to prevent it from being scrolled. */
612
06a2c219
GM
613 /* Can we tell that this update does not affect the window
614 where the mouse highlight is? If so, no need to turn off.
615 Likewise, don't do anything if the frame is garbaged;
616 in that case, the frame's current matrix that we would use
617 is all wrong, and we will redisplay that line anyway. */
618 if (!NILP (display_info->mouse_face_window)
619 && w == XWINDOW (display_info->mouse_face_window))
514e4681 620 {
06a2c219 621 int i;
514e4681 622
06a2c219
GM
623 for (i = 0; i < w->desired_matrix->nrows; ++i)
624 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
625 break;
626
06a2c219
GM
627 if (i < w->desired_matrix->nrows)
628 clear_mouse_face (display_info);
514e4681 629 }
64f26cf5 630#endif /* 0 */
b8009dd1 631 }
6ccf47d1 632
dc6f92b8
JB
633 UNBLOCK_INPUT;
634}
635
06a2c219
GM
636
637/* Draw a vertical window border to the right of window W if W doesn't
638 have vertical scroll bars. */
639
dfcf069d 640static void
06a2c219
GM
641x_draw_vertical_border (w)
642 struct window *w;
58769bee 643{
06a2c219
GM
644 struct frame *f = XFRAME (WINDOW_FRAME (w));
645
646 /* Redraw borders between horizontally adjacent windows. Don't
647 do it for frames with vertical scroll bars because either the
648 right scroll bar of a window, or the left scroll bar of its
649 neighbor will suffice as a border. */
650 if (!WINDOW_RIGHTMOST_P (w)
651 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
652 {
653 int x0, x1, y0, y1;
dc6f92b8 654
06a2c219 655 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 656 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
657 y1 -= 1;
658
659 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
660 f->output_data.x->normal_gc, x1, y0, x1, y1);
661 }
662}
663
664
71b8321e
GM
665/* End update of window W (which is equal to updated_window).
666
667 Draw vertical borders between horizontally adjacent windows, and
668 display W's cursor if CURSOR_ON_P is non-zero.
669
670 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
671 glyphs in mouse-face were overwritten. In that case we have to
672 make sure that the mouse-highlight is properly redrawn.
673
674 W may be a menu bar pseudo-window in case we don't have X toolkit
675 support. Such windows don't have a cursor, so don't display it
676 here. */
06a2c219
GM
677
678static void
71b8321e 679x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 680 struct window *w;
71b8321e 681 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
682{
683 if (!w->pseudo_window_p)
684 {
71b8321e
GM
685 struct x_display_info *dpyinfo
686 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
687
06a2c219 688 BLOCK_INPUT;
71b8321e
GM
689
690 /* If a row with mouse-face was overwritten, arrange for
691 XTframe_up_to_date to redisplay the mouse highlight. */
692 if (mouse_face_overwritten_p)
693 {
694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
696 dpyinfo->mouse_face_window = Qnil;
697 }
698
06a2c219
GM
699 if (cursor_on_p)
700 x_display_and_set_cursor (w, 1, output_cursor.hpos,
701 output_cursor.vpos,
702 output_cursor.x, output_cursor.y);
71b8321e 703
06a2c219
GM
704 x_draw_vertical_border (w);
705 UNBLOCK_INPUT;
706 }
707
708 updated_window = NULL;
709}
dc6f92b8 710
dc6f92b8 711
06a2c219
GM
712/* End update of frame F. This function is installed as a hook in
713 update_end. */
714
715static void
716x_update_end (f)
717 struct frame *f;
718{
719 /* Mouse highlight may be displayed again. */
aa8bff2e 720 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 721
06a2c219 722 BLOCK_INPUT;
334208b7 723 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
724 UNBLOCK_INPUT;
725}
b8009dd1 726
06a2c219
GM
727
728/* This function is called from various places in xdisp.c whenever a
729 complete update has been performed. The global variable
730 updated_window is not available here. */
b8009dd1 731
dfcf069d 732static void
b8009dd1 733XTframe_up_to_date (f)
06a2c219 734 struct frame *f;
b8009dd1 735{
06a2c219 736 if (FRAME_X_P (f))
514e4681 737 {
06a2c219 738 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 739
06a2c219
GM
740 if (dpyinfo->mouse_face_deferred_gc
741 || f == dpyinfo->mouse_face_mouse_frame)
742 {
743 BLOCK_INPUT;
744 if (dpyinfo->mouse_face_mouse_frame)
745 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
746 dpyinfo->mouse_face_mouse_x,
747 dpyinfo->mouse_face_mouse_y);
748 dpyinfo->mouse_face_deferred_gc = 0;
749 UNBLOCK_INPUT;
750 }
514e4681 751 }
b8009dd1 752}
06a2c219
GM
753
754
755/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
756 arrow bitmaps, or clear the areas where they would be displayed
757 before DESIRED_ROW is made current. The window being updated is
758 found in updated_window. This function It is called from
759 update_window_line only if it is known that there are differences
760 between bitmaps to be drawn between current row and DESIRED_ROW. */
761
762static void
763x_after_update_window_line (desired_row)
764 struct glyph_row *desired_row;
765{
766 struct window *w = updated_window;
767
768 xassert (w);
769
770 if (!desired_row->mode_line_p && !w->pseudo_window_p)
771 {
772 BLOCK_INPUT;
773 x_draw_row_bitmaps (w, desired_row);
774
775 /* When a window has disappeared, make sure that no rest of
776 full-width rows stays visible in the internal border. */
777 if (windows_or_buffers_changed)
778 {
779 struct frame *f = XFRAME (w->frame);
780 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
781 int height = desired_row->visible_height;
110859fc
GM
782 int x = (window_box_right (w, -1)
783 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
784 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
785
786 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
787 x, y, width, height, False);
788 }
789
790 UNBLOCK_INPUT;
791 }
792}
793
794
795/* Draw the bitmap WHICH in one of the areas to the left or right of
796 window W. ROW is the glyph row for which to display the bitmap; it
797 determines the vertical position at which the bitmap has to be
798 drawn. */
799
800static void
801x_draw_bitmap (w, row, which)
802 struct window *w;
803 struct glyph_row *row;
804 enum bitmap_type which;
805{
806 struct frame *f = XFRAME (WINDOW_FRAME (w));
807 Display *display = FRAME_X_DISPLAY (f);
808 Window window = FRAME_X_WINDOW (f);
809 int x, y, wd, h, dy;
810 unsigned char *bits;
811 Pixmap pixmap;
812 GC gc = f->output_data.x->normal_gc;
813 struct face *face;
814 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
815
816 /* Must clip because of partially visible lines. */
817 x_clip_to_row (w, row, gc, 1);
818
819 switch (which)
820 {
821 case LEFT_TRUNCATION_BITMAP:
822 wd = left_width;
823 h = left_height;
824 bits = left_bits;
825 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
826 - wd
110859fc 827 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
828 break;
829
830 case OVERLAY_ARROW_BITMAP:
831 wd = left_width;
832 h = left_height;
833 bits = ov_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
110859fc 836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
837 break;
838
839 case RIGHT_TRUNCATION_BITMAP:
840 wd = right_width;
841 h = right_height;
842 bits = right_bits;
843 x = window_box_right (w, -1);
110859fc 844 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
845 break;
846
847 case CONTINUED_LINE_BITMAP:
848 wd = right_width;
849 h = right_height;
850 bits = continued_bits;
851 x = window_box_right (w, -1);
110859fc 852 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
853 break;
854
855 case CONTINUATION_LINE_BITMAP:
856 wd = continuation_width;
857 h = continuation_height;
858 bits = continuation_bits;
859 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
860 - wd
110859fc 861 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
862 break;
863
864 case ZV_LINE_BITMAP:
865 wd = zv_width;
866 h = zv_height;
867 bits = zv_bits;
868 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
869 - wd
110859fc 870 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
871 break;
872
873 default:
874 abort ();
875 }
876
877 /* Convert to frame coordinates. Set dy to the offset in the row to
878 start drawing the bitmap. */
879 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
880 dy = (row->height - h) / 2;
881
882 /* Draw the bitmap. I believe these small pixmaps can be cached
883 by the server. */
884 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
885 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
886 face->foreground,
887 face->background, depth);
888 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
889 XFreePixmap (display, pixmap);
890 XSetClipMask (display, gc, None);
891}
892
893
894/* Draw flags bitmaps for glyph row ROW on window W. Call this
895 function with input blocked. */
896
897static void
898x_draw_row_bitmaps (w, row)
899 struct window *w;
900 struct glyph_row *row;
901{
902 struct frame *f = XFRAME (w->frame);
903 enum bitmap_type bitmap;
904 struct face *face;
045dee35 905 int header_line_height = -1;
06a2c219
GM
906
907 xassert (interrupt_input_blocked);
908
909 /* If row is completely invisible, because of vscrolling, we
910 don't have to draw anything. */
911 if (row->visible_height <= 0)
912 return;
913
914 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
915 PREPARE_FACE_FOR_DISPLAY (f, face);
916
917 /* Decide which bitmap to draw at the left side. */
918 if (row->overlay_arrow_p)
919 bitmap = OVERLAY_ARROW_BITMAP;
920 else if (row->truncated_on_left_p)
921 bitmap = LEFT_TRUNCATION_BITMAP;
922 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
923 bitmap = CONTINUATION_LINE_BITMAP;
924 else if (row->indicate_empty_line_p)
925 bitmap = ZV_LINE_BITMAP;
926 else
927 bitmap = NO_BITMAP;
928
929 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
930 the flags area. */
931 if (bitmap == NO_BITMAP
110859fc 932 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
933 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
934 {
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 int left = window_box_left (w, -1);
940
045dee35
GM
941 if (header_line_height < 0)
942 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
943
944 /* In case the same realized face is used for bitmap areas and
945 for something displayed in the text (e.g. face `region' on
946 mono-displays, the fill style may have been changed to
947 FillSolid in x_draw_glyph_string_background. */
948 if (face->stipple)
949 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
950 else
951 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
952
06a2c219
GM
953 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
954 face->gc,
955 (left
110859fc 956 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 957 + border),
045dee35 958 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 959 row->y)),
110859fc 960 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 961 row->visible_height);
dcd08bfb
GM
962 if (!face->stipple)
963 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
964 }
965
966 /* Draw the left bitmap. */
967 if (bitmap != NO_BITMAP)
968 x_draw_bitmap (w, row, bitmap);
969
970 /* Decide which bitmap to draw at the right side. */
971 if (row->truncated_on_right_p)
972 bitmap = RIGHT_TRUNCATION_BITMAP;
973 else if (row->continued_p)
974 bitmap = CONTINUED_LINE_BITMAP;
975 else
976 bitmap = NO_BITMAP;
977
978 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
979 the flags area. */
980 if (bitmap == NO_BITMAP
110859fc 981 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
982 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
983 {
984 int right = window_box_right (w, -1);
985
045dee35
GM
986 if (header_line_height < 0)
987 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
988
989 /* In case the same realized face is used for bitmap areas and
990 for something displayed in the text (e.g. face `region' on
991 mono-displays, the fill style may have been changed to
992 FillSolid in x_draw_glyph_string_background. */
993 if (face->stipple)
994 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
995 else
996 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
998 face->gc,
999 right,
045dee35 1000 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1001 row->y)),
110859fc 1002 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1003 row->visible_height);
dcd08bfb
GM
1004 if (!face->stipple)
1005 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1006 }
1007
1008 /* Draw the right bitmap. */
1009 if (bitmap != NO_BITMAP)
1010 x_draw_bitmap (w, row, bitmap);
1011}
1012
dc6f92b8 1013\f
06a2c219
GM
1014/***********************************************************************
1015 Line Highlighting
1016 ***********************************************************************/
dc6f92b8 1017
06a2c219
GM
1018/* External interface to control of standout mode. Not used for X
1019 frames. Aborts when called. */
1020
1021static void
dc6f92b8
JB
1022XTreassert_line_highlight (new, vpos)
1023 int new, vpos;
1024{
06a2c219 1025 abort ();
dc6f92b8
JB
1026}
1027
06a2c219
GM
1028
1029/* Call this when about to modify line at position VPOS and change
1030 whether it is highlighted. Not used for X frames. Aborts when
1031 called. */
dc6f92b8 1032
dfcf069d 1033static void
06a2c219
GM
1034x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1035 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1036{
06a2c219 1037 abort ();
dc6f92b8
JB
1038}
1039
06a2c219
GM
1040
1041/* This is called when starting Emacs and when restarting after
1042 suspend. When starting Emacs, no X window is mapped. And nothing
1043 must be done to Emacs's own window if it is suspended (though that
1044 rarely happens). */
dc6f92b8 1045
dfcf069d 1046static void
dc6f92b8
JB
1047XTset_terminal_modes ()
1048{
1049}
1050
06a2c219
GM
1051/* This is called when exiting or suspending Emacs. Exiting will make
1052 the X-windows go away, and suspending requires no action. */
dc6f92b8 1053
dfcf069d 1054static void
dc6f92b8
JB
1055XTreset_terminal_modes ()
1056{
dc6f92b8 1057}
06a2c219
GM
1058
1059
dc6f92b8 1060\f
06a2c219
GM
1061/***********************************************************************
1062 Output Cursor
1063 ***********************************************************************/
1064
1065/* Set the global variable output_cursor to CURSOR. All cursor
1066 positions are relative to updated_window. */
dc6f92b8 1067
dfcf069d 1068static void
06a2c219
GM
1069set_output_cursor (cursor)
1070 struct cursor_pos *cursor;
dc6f92b8 1071{
06a2c219
GM
1072 output_cursor.hpos = cursor->hpos;
1073 output_cursor.vpos = cursor->vpos;
1074 output_cursor.x = cursor->x;
1075 output_cursor.y = cursor->y;
1076}
1077
1078
1079/* Set a nominal cursor position.
dc6f92b8 1080
06a2c219
GM
1081 HPOS and VPOS are column/row positions in a window glyph matrix. X
1082 and Y are window text area relative pixel positions.
1083
1084 If this is done during an update, updated_window will contain the
1085 window that is being updated and the position is the future output
1086 cursor position for that window. If updated_window is null, use
1087 selected_window and display the cursor at the given position. */
1088
1089static void
1090XTcursor_to (vpos, hpos, y, x)
1091 int vpos, hpos, y, x;
1092{
1093 struct window *w;
1094
1095 /* If updated_window is not set, work on selected_window. */
1096 if (updated_window)
1097 w = updated_window;
1098 else
1099 w = XWINDOW (selected_window);
dbcb258a 1100
06a2c219
GM
1101 /* Set the output cursor. */
1102 output_cursor.hpos = hpos;
1103 output_cursor.vpos = vpos;
1104 output_cursor.x = x;
1105 output_cursor.y = y;
dc6f92b8 1106
06a2c219
GM
1107 /* If not called as part of an update, really display the cursor.
1108 This will also set the cursor position of W. */
1109 if (updated_window == NULL)
dc6f92b8
JB
1110 {
1111 BLOCK_INPUT;
06a2c219 1112 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1113 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1114 UNBLOCK_INPUT;
1115 }
1116}
dc43ef94 1117
06a2c219
GM
1118
1119\f
1120/***********************************************************************
1121 Display Iterator
1122 ***********************************************************************/
1123
1124/* Function prototypes of this page. */
1125
1126static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1127 struct glyph *,
ee569018
KH
1128 XChar2b *,
1129 int *));
06a2c219
GM
1130static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1131 int, XChar2b *, int));
1132static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1133static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1134static void x_append_glyph P_ ((struct it *));
b4192550 1135static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1136static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1137 int, int, double));
1138static void x_produce_glyphs P_ ((struct it *));
06a2c219 1139static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1140
1141
e2ef8ee6
GM
1142/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1143 is not contained in the font. */
dc43ef94 1144
06a2c219 1145static INLINE XCharStruct *
ee569018 1146x_per_char_metric (font, char2b)
06a2c219
GM
1147 XFontStruct *font;
1148 XChar2b *char2b;
1149{
1150 /* The result metric information. */
1151 XCharStruct *pcm = NULL;
dc6f92b8 1152
06a2c219 1153 xassert (font && char2b);
dc6f92b8 1154
06a2c219 1155 if (font->per_char != NULL)
dc6f92b8 1156 {
06a2c219 1157 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1158 {
06a2c219
GM
1159 /* min_char_or_byte2 specifies the linear character index
1160 corresponding to the first element of the per_char array,
1161 max_char_or_byte2 is the index of the last character. A
1162 character with non-zero CHAR2B->byte1 is not in the font.
1163 A character with byte2 less than min_char_or_byte2 or
1164 greater max_char_or_byte2 is not in the font. */
1165 if (char2b->byte1 == 0
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1169 }
06a2c219 1170 else
dc6f92b8 1171 {
06a2c219
GM
1172 /* If either min_byte1 or max_byte1 are nonzero, both
1173 min_char_or_byte2 and max_char_or_byte2 are less than
1174 256, and the 2-byte character index values corresponding
1175 to the per_char array element N (counting from 0) are:
1176
1177 byte1 = N/D + min_byte1
1178 byte2 = N\D + min_char_or_byte2
1179
1180 where:
1181
1182 D = max_char_or_byte2 - min_char_or_byte2 + 1
1183 / = integer division
1184 \ = integer modulus */
1185 if (char2b->byte1 >= font->min_byte1
1186 && char2b->byte1 <= font->max_byte1
1187 && char2b->byte2 >= font->min_char_or_byte2
1188 && char2b->byte2 <= font->max_char_or_byte2)
1189 {
1190 pcm = (font->per_char
1191 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1192 * (char2b->byte1 - font->min_byte1))
1193 + (char2b->byte2 - font->min_char_or_byte2));
1194 }
dc6f92b8 1195 }
06a2c219
GM
1196 }
1197 else
1198 {
1199 /* If the per_char pointer is null, all glyphs between the first
1200 and last character indexes inclusive have the same
1201 information, as given by both min_bounds and max_bounds. */
1202 if (char2b->byte2 >= font->min_char_or_byte2
1203 && char2b->byte2 <= font->max_char_or_byte2)
1204 pcm = &font->max_bounds;
1205 }
dc6f92b8 1206
ee569018 1207 return ((pcm == NULL
3e71d8f2 1208 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1209 ? NULL : pcm);
06a2c219 1210}
b73b6aaf 1211
57b03282 1212
06a2c219
GM
1213/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1214 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1215
06a2c219
GM
1216static INLINE void
1217x_encode_char (c, char2b, font_info)
1218 int c;
1219 XChar2b *char2b;
1220 struct font_info *font_info;
1221{
1222 int charset = CHAR_CHARSET (c);
1223 XFontStruct *font = font_info->font;
1224
1225 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1226 This may be either a program in a special encoder language or a
1227 fixed encoding. */
1228 if (font_info->font_encoder)
1229 {
1230 /* It's a program. */
1231 struct ccl_program *ccl = font_info->font_encoder;
1232
1233 if (CHARSET_DIMENSION (charset) == 1)
1234 {
1235 ccl->reg[0] = charset;
1236 ccl->reg[1] = char2b->byte2;
1237 }
1238 else
1239 {
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte1;
1242 ccl->reg[2] = char2b->byte2;
1243 }
1244
1245 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1246
1247 /* We assume that MSBs are appropriately set/reset by CCL
1248 program. */
1249 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1250 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1251 else
1252 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1253 }
1254 else if (font_info->encoding[charset])
1255 {
1256 /* Fixed encoding scheme. See fontset.h for the meaning of the
1257 encoding numbers. */
1258 int enc = font_info->encoding[charset];
1259
1260 if ((enc == 1 || enc == 2)
1261 && CHARSET_DIMENSION (charset) == 2)
1262 char2b->byte1 |= 0x80;
1263
1264 if (enc == 1 || enc == 3)
1265 char2b->byte2 |= 0x80;
1266 }
1267}
1268
1269
1270/* Get face and two-byte form of character C in face FACE_ID on frame
1271 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1272 means we want to display multibyte text. Value is a pointer to a
1273 realized face that is ready for display. */
1274
1275static INLINE struct face *
1276x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1277 struct frame *f;
1278 int c, face_id;
1279 XChar2b *char2b;
1280 int multibyte_p;
1281{
1282 struct face *face = FACE_FROM_ID (f, face_id);
1283
1284 if (!multibyte_p)
1285 {
1286 /* Unibyte case. We don't have to encode, but we have to make
1287 sure to use a face suitable for unibyte. */
1288 char2b->byte1 = 0;
1289 char2b->byte2 = c;
ee569018
KH
1290 face_id = FACE_FOR_CHAR (f, face, c);
1291 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1292 }
1293 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1294 {
1295 /* Case of ASCII in a face known to fit ASCII. */
1296 char2b->byte1 = 0;
1297 char2b->byte2 = c;
1298 }
1299 else
1300 {
1301 int c1, c2, charset;
1302
1303 /* Split characters into bytes. If c2 is -1 afterwards, C is
1304 really a one-byte character so that byte1 is zero. */
1305 SPLIT_CHAR (c, charset, c1, c2);
1306 if (c2 > 0)
1307 char2b->byte1 = c1, char2b->byte2 = c2;
1308 else
1309 char2b->byte1 = 0, char2b->byte2 = c1;
1310
06a2c219 1311 /* Maybe encode the character in *CHAR2B. */
ee569018 1312 if (face->font != NULL)
06a2c219
GM
1313 {
1314 struct font_info *font_info
1315 = FONT_INFO_FROM_ID (f, face->font_info_id);
1316 if (font_info)
ee569018 1317 x_encode_char (c, char2b, font_info);
06a2c219
GM
1318 }
1319 }
1320
1321 /* Make sure X resources of the face are allocated. */
1322 xassert (face != NULL);
1323 PREPARE_FACE_FOR_DISPLAY (f, face);
1324
1325 return face;
1326}
1327
1328
1329/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1330 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1331 a pointer to a realized face that is ready for display. */
1332
1333static INLINE struct face *
ee569018 1334x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1335 struct frame *f;
1336 struct glyph *glyph;
1337 XChar2b *char2b;
ee569018 1338 int *two_byte_p;
06a2c219
GM
1339{
1340 struct face *face;
1341
1342 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1343 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1344
ee569018
KH
1345 if (two_byte_p)
1346 *two_byte_p = 0;
1347
06a2c219
GM
1348 if (!glyph->multibyte_p)
1349 {
1350 /* Unibyte case. We don't have to encode, but we have to make
1351 sure to use a face suitable for unibyte. */
1352 char2b->byte1 = 0;
43d120d8 1353 char2b->byte2 = glyph->u.ch;
06a2c219 1354 }
43d120d8
KH
1355 else if (glyph->u.ch < 128
1356 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1357 {
1358 /* Case of ASCII in a face known to fit ASCII. */
1359 char2b->byte1 = 0;
43d120d8 1360 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1361 }
1362 else
1363 {
1364 int c1, c2, charset;
1365
1366 /* Split characters into bytes. If c2 is -1 afterwards, C is
1367 really a one-byte character so that byte1 is zero. */
43d120d8 1368 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1369 if (c2 > 0)
1370 char2b->byte1 = c1, char2b->byte2 = c2;
1371 else
1372 char2b->byte1 = 0, char2b->byte2 = c1;
1373
1374 /* Maybe encode the character in *CHAR2B. */
1375 if (charset != CHARSET_ASCII)
1376 {
1377 struct font_info *font_info
1378 = FONT_INFO_FROM_ID (f, face->font_info_id);
1379 if (font_info)
1380 {
43d120d8 1381 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1382 if (two_byte_p)
1383 *two_byte_p
1384 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1385 }
1386 }
1387 }
1388
1389 /* Make sure X resources of the face are allocated. */
1390 xassert (face != NULL);
1391 PREPARE_FACE_FOR_DISPLAY (f, face);
1392 return face;
1393}
1394
1395
1396/* Store one glyph for IT->char_to_display in IT->glyph_row.
1397 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1398
1399static INLINE void
1400x_append_glyph (it)
1401 struct it *it;
1402{
1403 struct glyph *glyph;
1404 enum glyph_row_area area = it->area;
1405
1406 xassert (it->glyph_row);
1407 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1408
1409 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1410 if (glyph < it->glyph_row->glyphs[area + 1])
1411 {
06a2c219
GM
1412 glyph->charpos = CHARPOS (it->position);
1413 glyph->object = it->object;
88d75730 1414 glyph->pixel_width = it->pixel_width;
06a2c219 1415 glyph->voffset = it->voffset;
88d75730 1416 glyph->type = CHAR_GLYPH;
06a2c219 1417 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1418 glyph->left_box_line_p = it->start_of_box_run_p;
1419 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1420 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1421 || it->phys_descent > it->descent);
88d75730 1422 glyph->padding_p = 0;
ee569018 1423 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1424 glyph->face_id = it->face_id;
1425 glyph->u.ch = it->char_to_display;
06a2c219
GM
1426 ++it->glyph_row->used[area];
1427 }
1428}
1429
b4192550
KH
1430/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1431 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1432
1433static INLINE void
1434x_append_composite_glyph (it)
1435 struct it *it;
1436{
1437 struct glyph *glyph;
1438 enum glyph_row_area area = it->area;
1439
1440 xassert (it->glyph_row);
1441
1442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1443 if (glyph < it->glyph_row->glyphs[area + 1])
1444 {
b4192550
KH
1445 glyph->charpos = CHARPOS (it->position);
1446 glyph->object = it->object;
88d75730 1447 glyph->pixel_width = it->pixel_width;
b4192550 1448 glyph->voffset = it->voffset;
88d75730 1449 glyph->type = COMPOSITE_GLYPH;
b4192550 1450 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1451 glyph->left_box_line_p = it->start_of_box_run_p;
1452 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1453 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1454 || it->phys_descent > it->descent);
88d75730
GM
1455 glyph->padding_p = 0;
1456 glyph->glyph_not_available_p = 0;
1457 glyph->face_id = it->face_id;
1458 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1459 ++it->glyph_row->used[area];
1460 }
1461}
1462
06a2c219
GM
1463
1464/* Change IT->ascent and IT->height according to the setting of
1465 IT->voffset. */
1466
1467static INLINE void
1468take_vertical_position_into_account (it)
1469 struct it *it;
1470{
1471 if (it->voffset)
1472 {
1473 if (it->voffset < 0)
1474 /* Increase the ascent so that we can display the text higher
1475 in the line. */
1476 it->ascent += abs (it->voffset);
1477 else
1478 /* Increase the descent so that we can display the text lower
1479 in the line. */
1480 it->descent += it->voffset;
1481 }
1482}
1483
1484
1485/* Produce glyphs/get display metrics for the image IT is loaded with.
1486 See the description of struct display_iterator in dispextern.h for
1487 an overview of struct display_iterator. */
1488
1489static void
1490x_produce_image_glyph (it)
1491 struct it *it;
1492{
1493 struct image *img;
1494 struct face *face;
1495
1496 xassert (it->what == IT_IMAGE);
1497
1498 face = FACE_FROM_ID (it->f, it->face_id);
1499 img = IMAGE_FROM_ID (it->f, it->image_id);
1500 xassert (img);
1501
1502 /* Make sure X resources of the face and image are loaded. */
1503 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1504 prepare_image_for_display (it->f, img);
1505
95af8492 1506 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1507 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1508 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1509
1510 it->nglyphs = 1;
1511
1512 if (face->box != FACE_NO_BOX)
1513 {
1514 it->ascent += face->box_line_width;
1515 it->descent += face->box_line_width;
1516
1517 if (it->start_of_box_run_p)
1518 it->pixel_width += face->box_line_width;
1519 if (it->end_of_box_run_p)
1520 it->pixel_width += face->box_line_width;
1521 }
1522
1523 take_vertical_position_into_account (it);
1524
1525 if (it->glyph_row)
1526 {
1527 struct glyph *glyph;
1528 enum glyph_row_area area = it->area;
1529
1530 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1531 if (glyph < it->glyph_row->glyphs[area + 1])
1532 {
06a2c219
GM
1533 glyph->charpos = CHARPOS (it->position);
1534 glyph->object = it->object;
88d75730 1535 glyph->pixel_width = it->pixel_width;
06a2c219 1536 glyph->voffset = it->voffset;
88d75730 1537 glyph->type = IMAGE_GLYPH;
06a2c219 1538 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1539 glyph->left_box_line_p = it->start_of_box_run_p;
1540 glyph->right_box_line_p = it->end_of_box_run_p;
1541 glyph->overlaps_vertically_p = 0;
1542 glyph->padding_p = 0;
1543 glyph->glyph_not_available_p = 0;
1544 glyph->face_id = it->face_id;
1545 glyph->u.img_id = img->id;
06a2c219
GM
1546 ++it->glyph_row->used[area];
1547 }
1548 }
1549}
1550
1551
1552/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1553 of the glyph, WIDTH and HEIGHT are the width and height of the
1554 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1555 ascent of the glyph (0 <= ASCENT <= 1). */
1556
1557static void
1558x_append_stretch_glyph (it, object, width, height, ascent)
1559 struct it *it;
1560 Lisp_Object object;
1561 int width, height;
1562 double ascent;
1563{
1564 struct glyph *glyph;
1565 enum glyph_row_area area = it->area;
1566
1567 xassert (ascent >= 0 && ascent <= 1);
1568
1569 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1570 if (glyph < it->glyph_row->glyphs[area + 1])
1571 {
06a2c219
GM
1572 glyph->charpos = CHARPOS (it->position);
1573 glyph->object = object;
88d75730 1574 glyph->pixel_width = width;
06a2c219 1575 glyph->voffset = it->voffset;
88d75730 1576 glyph->type = STRETCH_GLYPH;
06a2c219 1577 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1578 glyph->left_box_line_p = it->start_of_box_run_p;
1579 glyph->right_box_line_p = it->end_of_box_run_p;
1580 glyph->overlaps_vertically_p = 0;
1581 glyph->padding_p = 0;
1582 glyph->glyph_not_available_p = 0;
1583 glyph->face_id = it->face_id;
1584 glyph->u.stretch.ascent = height * ascent;
1585 glyph->u.stretch.height = height;
06a2c219
GM
1586 ++it->glyph_row->used[area];
1587 }
1588}
1589
1590
1591/* Produce a stretch glyph for iterator IT. IT->object is the value
1592 of the glyph property displayed. The value must be a list
1593 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1594 being recognized:
1595
1596 1. `:width WIDTH' specifies that the space should be WIDTH *
1597 canonical char width wide. WIDTH may be an integer or floating
1598 point number.
1599
1600 2. `:relative-width FACTOR' specifies that the width of the stretch
1601 should be computed from the width of the first character having the
1602 `glyph' property, and should be FACTOR times that width.
1603
1604 3. `:align-to HPOS' specifies that the space should be wide enough
1605 to reach HPOS, a value in canonical character units.
1606
1607 Exactly one of the above pairs must be present.
1608
1609 4. `:height HEIGHT' specifies that the height of the stretch produced
1610 should be HEIGHT, measured in canonical character units.
1611
1612 5. `:relative-height FACTOR' specifies that the height of the the
1613 stretch should be FACTOR times the height of the characters having
1614 the glyph property.
1615
1616 Either none or exactly one of 4 or 5 must be present.
1617
1618 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1619 of the stretch should be used for the ascent of the stretch.
1620 ASCENT must be in the range 0 <= ASCENT <= 100. */
1621
1622#define NUMVAL(X) \
1623 ((INTEGERP (X) || FLOATP (X)) \
1624 ? XFLOATINT (X) \
1625 : - 1)
1626
1627
1628static void
1629x_produce_stretch_glyph (it)
1630 struct it *it;
1631{
1632 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1633#if GLYPH_DEBUG
1634 extern Lisp_Object Qspace;
1635#endif
1636 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1637 extern Lisp_Object QCrelative_width, QCrelative_height;
1638 extern Lisp_Object QCalign_to;
1639 Lisp_Object prop, plist;
1640 double width = 0, height = 0, ascent = 0;
1641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1642 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1643
1644 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1645
1646 /* List should start with `space'. */
1647 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1648 plist = XCDR (it->object);
1649
1650 /* Compute the width of the stretch. */
1651 if (prop = Fplist_get (plist, QCwidth),
1652 NUMVAL (prop) > 0)
1653 /* Absolute width `:width WIDTH' specified and valid. */
1654 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1655 else if (prop = Fplist_get (plist, QCrelative_width),
1656 NUMVAL (prop) > 0)
1657 {
1658 /* Relative width `:relative-width FACTOR' specified and valid.
1659 Compute the width of the characters having the `glyph'
1660 property. */
1661 struct it it2;
1662 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1663
1664 it2 = *it;
1665 if (it->multibyte_p)
1666 {
1667 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1668 - IT_BYTEPOS (*it));
1669 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1670 }
1671 else
1672 it2.c = *p, it2.len = 1;
1673
1674 it2.glyph_row = NULL;
1675 it2.what = IT_CHARACTER;
1676 x_produce_glyphs (&it2);
1677 width = NUMVAL (prop) * it2.pixel_width;
1678 }
1679 else if (prop = Fplist_get (plist, QCalign_to),
1680 NUMVAL (prop) > 0)
1681 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1682 else
1683 /* Nothing specified -> width defaults to canonical char width. */
1684 width = CANON_X_UNIT (it->f);
1685
1686 /* Compute height. */
1687 if (prop = Fplist_get (plist, QCheight),
1688 NUMVAL (prop) > 0)
1689 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1690 else if (prop = Fplist_get (plist, QCrelative_height),
1691 NUMVAL (prop) > 0)
1692 height = FONT_HEIGHT (font) * NUMVAL (prop);
1693 else
1694 height = FONT_HEIGHT (font);
1695
1696 /* Compute percentage of height used for ascent. If
1697 `:ascent ASCENT' is present and valid, use that. Otherwise,
1698 derive the ascent from the font in use. */
1699 if (prop = Fplist_get (plist, QCascent),
1700 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1701 ascent = NUMVAL (prop) / 100.0;
1702 else
1703 ascent = (double) font->ascent / FONT_HEIGHT (font);
1704
1705 if (width <= 0)
1706 width = 1;
1707 if (height <= 0)
1708 height = 1;
1709
1710 if (it->glyph_row)
1711 {
1712 Lisp_Object object = it->stack[it->sp - 1].string;
1713 if (!STRINGP (object))
1714 object = it->w->buffer;
1715 x_append_stretch_glyph (it, object, width, height, ascent);
1716 }
1717
1718 it->pixel_width = width;
66ac4b0e
GM
1719 it->ascent = it->phys_ascent = height * ascent;
1720 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1721 it->nglyphs = 1;
1722
1723 if (face->box != FACE_NO_BOX)
1724 {
1725 it->ascent += face->box_line_width;
1726 it->descent += face->box_line_width;
1727
1728 if (it->start_of_box_run_p)
1729 it->pixel_width += face->box_line_width;
1730 if (it->end_of_box_run_p)
1731 it->pixel_width += face->box_line_width;
1732 }
1733
1734 take_vertical_position_into_account (it);
1735}
1736
b4192550
KH
1737/* Return proper value to be used as baseline offset of font that has
1738 ASCENT and DESCENT to draw characters by the font at the vertical
1739 center of the line of frame F.
1740
1741 Here, out task is to find the value of BOFF in the following figure;
1742
1743 -------------------------+-----------+-
1744 -+-+---------+-+ | |
1745 | | | | | |
1746 | | | | F_ASCENT F_HEIGHT
1747 | | | ASCENT | |
1748 HEIGHT | | | | |
1749 | | |-|-+------+-----------|------- baseline
1750 | | | | BOFF | |
1751 | |---------|-+-+ | |
1752 | | | DESCENT | |
1753 -+-+---------+-+ F_DESCENT |
1754 -------------------------+-----------+-
1755
1756 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1757 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1758 DESCENT = FONT->descent
1759 HEIGHT = FONT_HEIGHT (FONT)
1760 F_DESCENT = (F->output_data.x->font->descent
1761 - F->output_data.x->baseline_offset)
1762 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1763*/
1764
458f45fa
KH
1765#define VCENTER_BASELINE_OFFSET(FONT, F) \
1766 ((FONT)->descent \
1767 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1768 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1769 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1770
1771/* Produce glyphs/get display metrics for the display element IT is
1772 loaded with. See the description of struct display_iterator in
1773 dispextern.h for an overview of struct display_iterator. */
1774
1775static void
1776x_produce_glyphs (it)
1777 struct it *it;
1778{
ee569018
KH
1779 it->glyph_not_available_p = 0;
1780
06a2c219
GM
1781 if (it->what == IT_CHARACTER)
1782 {
1783 XChar2b char2b;
1784 XFontStruct *font;
ee569018 1785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1786 XCharStruct *pcm;
06a2c219 1787 int font_not_found_p;
b4192550
KH
1788 struct font_info *font_info;
1789 int boff; /* baseline offset */
a4249304
KH
1790 /* We may change it->multibyte_p upon unibyte<->multibyte
1791 conversion. So, save the current value now and restore it
1792 later.
1793
1794 Note: It seems that we don't have to record multibyte_p in
1795 struct glyph because the character code itself tells if or
1796 not the character is multibyte. Thus, in the future, we must
1797 consider eliminating the field `multibyte_p' in the struct
1798 glyph.
1799 */
1800 int saved_multibyte_p = it->multibyte_p;
06a2c219 1801
ee569018
KH
1802 /* Maybe translate single-byte characters to multibyte, or the
1803 other way. */
06a2c219 1804 it->char_to_display = it->c;
ee569018 1805 if (!ASCII_BYTE_P (it->c))
06a2c219 1806 {
ee569018
KH
1807 if (unibyte_display_via_language_environment
1808 && SINGLE_BYTE_CHAR_P (it->c)
1809 && (it->c >= 0240
1810 || !NILP (Vnonascii_translation_table)))
1811 {
1812 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1813 it->multibyte_p = 1;
ee569018
KH
1814 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1815 face = FACE_FROM_ID (it->f, it->face_id);
1816 }
1817 else if (!SINGLE_BYTE_CHAR_P (it->c)
1818 && !it->multibyte_p)
1819 {
1820 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
a4249304 1821 it->multibyte_p = 0;
ee569018
KH
1822 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1823 face = FACE_FROM_ID (it->f, it->face_id);
1824 }
06a2c219
GM
1825 }
1826
ee569018
KH
1827 /* Get font to use. Encode IT->char_to_display. */
1828 x_get_char_face_and_encoding (it->f, it->char_to_display,
1829 it->face_id, &char2b,
1830 it->multibyte_p);
06a2c219
GM
1831 font = face->font;
1832
1833 /* When no suitable font found, use the default font. */
1834 font_not_found_p = font == NULL;
1835 if (font_not_found_p)
b4192550
KH
1836 {
1837 font = FRAME_FONT (it->f);
1838 boff = it->f->output_data.x->baseline_offset;
1839 font_info = NULL;
1840 }
1841 else
1842 {
1843 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1844 boff = font_info->baseline_offset;
1845 if (font_info->vertical_centering)
1846 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1847 }
06a2c219
GM
1848
1849 if (it->char_to_display >= ' '
1850 && (!it->multibyte_p || it->char_to_display < 128))
1851 {
1852 /* Either unibyte or ASCII. */
1853 int stretched_p;
1854
1855 it->nglyphs = 1;
06a2c219
GM
1856
1857 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1858 it->ascent = font->ascent + boff;
1859 it->descent = font->descent - boff;
474848ac
GM
1860
1861 if (pcm)
1862 {
1863 it->phys_ascent = pcm->ascent + boff;
1864 it->phys_descent = pcm->descent - boff;
1865 it->pixel_width = pcm->width;
1866 }
1867 else
1868 {
1869 it->glyph_not_available_p = 1;
1870 it->phys_ascent = font->ascent + boff;
1871 it->phys_descent = font->descent - boff;
1872 it->pixel_width = FONT_WIDTH (font);
1873 }
06a2c219
GM
1874
1875 /* If this is a space inside a region of text with
1876 `space-width' property, change its width. */
1877 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1878 if (stretched_p)
1879 it->pixel_width *= XFLOATINT (it->space_width);
1880
1881 /* If face has a box, add the box thickness to the character
1882 height. If character has a box line to the left and/or
1883 right, add the box line width to the character's width. */
1884 if (face->box != FACE_NO_BOX)
1885 {
1886 int thick = face->box_line_width;
1887
1888 it->ascent += thick;
1889 it->descent += thick;
1890
1891 if (it->start_of_box_run_p)
1892 it->pixel_width += thick;
1893 if (it->end_of_box_run_p)
1894 it->pixel_width += thick;
1895 }
1896
1897 /* If face has an overline, add the height of the overline
1898 (1 pixel) and a 1 pixel margin to the character height. */
1899 if (face->overline_p)
1900 it->ascent += 2;
1901
1902 take_vertical_position_into_account (it);
1903
1904 /* If we have to actually produce glyphs, do it. */
1905 if (it->glyph_row)
1906 {
1907 if (stretched_p)
1908 {
1909 /* Translate a space with a `space-width' property
1910 into a stretch glyph. */
1911 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1912 x_append_stretch_glyph (it, it->object, it->pixel_width,
1913 it->ascent + it->descent, ascent);
1914 }
1915 else
1916 x_append_glyph (it);
1917
1918 /* If characters with lbearing or rbearing are displayed
1919 in this line, record that fact in a flag of the
1920 glyph row. This is used to optimize X output code. */
1c7e22fd 1921 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1922 it->glyph_row->contains_overlapping_glyphs_p = 1;
1923 }
1924 }
1925 else if (it->char_to_display == '\n')
1926 {
1927 /* A newline has no width but we need the height of the line. */
1928 it->pixel_width = 0;
1929 it->nglyphs = 0;
b4192550
KH
1930 it->ascent = it->phys_ascent = font->ascent + boff;
1931 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1932
1933 if (face->box != FACE_NO_BOX)
1934 {
1935 int thick = face->box_line_width;
1936 it->ascent += thick;
1937 it->descent += thick;
1938 }
1939 }
1940 else if (it->char_to_display == '\t')
1941 {
1942 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1943 int x = it->current_x + it->continuation_lines_width;
06a2c219 1944 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1945
1946 /* If the distance from the current position to the next tab
1947 stop is less than a canonical character width, use the
1948 tab stop after that. */
1949 if (next_tab_x - x < CANON_X_UNIT (it->f))
1950 next_tab_x += tab_width;
06a2c219
GM
1951
1952 it->pixel_width = next_tab_x - x;
1953 it->nglyphs = 1;
b4192550
KH
1954 it->ascent = it->phys_ascent = font->ascent + boff;
1955 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1956
1957 if (it->glyph_row)
1958 {
1959 double ascent = (double) it->ascent / (it->ascent + it->descent);
1960 x_append_stretch_glyph (it, it->object, it->pixel_width,
1961 it->ascent + it->descent, ascent);
1962 }
1963 }
1964 else
1965 {
1966 /* A multi-byte character. Assume that the display width of the
1967 character is the width of the character multiplied by the
b4192550 1968 width of the font. */
06a2c219 1969
b4192550
KH
1970 /* If we found a font, this font should give us the right
1971 metrics. If we didn't find a font, use the frame's
1972 default font and calculate the width of the character
1973 from the charset width; this is what old redisplay code
1974 did. */
1975 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1976 if (font_not_found_p || !pcm)
1977 {
1978 int charset = CHAR_CHARSET (it->char_to_display);
1979
1980 it->glyph_not_available_p = 1;
1981 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1982 * CHARSET_WIDTH (charset));
1983 it->phys_ascent = font->ascent + boff;
1984 it->phys_descent = font->descent - boff;
1985 }
1986 else
1987 {
1988 it->pixel_width = pcm->width;
1989 it->phys_ascent = pcm->ascent + boff;
1990 it->phys_descent = pcm->descent - boff;
1991 if (it->glyph_row
1992 && (pcm->lbearing < 0
1993 || pcm->rbearing > pcm->width))
1994 it->glyph_row->contains_overlapping_glyphs_p = 1;
1995 }
b4192550
KH
1996 it->nglyphs = 1;
1997 it->ascent = font->ascent + boff;
1998 it->descent = font->descent - boff;
06a2c219
GM
1999 if (face->box != FACE_NO_BOX)
2000 {
2001 int thick = face->box_line_width;
2002 it->ascent += thick;
2003 it->descent += thick;
2004
2005 if (it->start_of_box_run_p)
2006 it->pixel_width += thick;
2007 if (it->end_of_box_run_p)
2008 it->pixel_width += thick;
2009 }
2010
2011 /* If face has an overline, add the height of the overline
2012 (1 pixel) and a 1 pixel margin to the character height. */
2013 if (face->overline_p)
2014 it->ascent += 2;
2015
2016 take_vertical_position_into_account (it);
2017
2018 if (it->glyph_row)
2019 x_append_glyph (it);
2020 }
a4249304 2021 it->multibyte_p = saved_multibyte_p;
06a2c219 2022 }
b4192550
KH
2023 else if (it->what == IT_COMPOSITION)
2024 {
2025 /* Note: A composition is represented as one glyph in the
2026 glyph matrix. There are no padding glyphs. */
2027 XChar2b char2b;
2028 XFontStruct *font;
ee569018 2029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2030 XCharStruct *pcm;
2031 int font_not_found_p;
2032 struct font_info *font_info;
2033 int boff; /* baseline offset */
2034 struct composition *cmp = composition_table[it->cmp_id];
2035
2036 /* Maybe translate single-byte characters to multibyte. */
2037 it->char_to_display = it->c;
2038 if (unibyte_display_via_language_environment
2039 && SINGLE_BYTE_CHAR_P (it->c)
2040 && (it->c >= 0240
2041 || (it->c >= 0200
2042 && !NILP (Vnonascii_translation_table))))
2043 {
2044 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2045 }
2046
2047 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2048 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2049 face = FACE_FROM_ID (it->f, it->face_id);
2050 x_get_char_face_and_encoding (it->f, it->char_to_display,
2051 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2052 font = face->font;
2053
2054 /* When no suitable font found, use the default font. */
2055 font_not_found_p = font == NULL;
2056 if (font_not_found_p)
2057 {
2058 font = FRAME_FONT (it->f);
2059 boff = it->f->output_data.x->baseline_offset;
2060 font_info = NULL;
2061 }
2062 else
2063 {
2064 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2065 boff = font_info->baseline_offset;
2066 if (font_info->vertical_centering)
2067 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2068 }
2069
2070 /* There are no padding glyphs, so there is only one glyph to
2071 produce for the composition. Important is that pixel_width,
2072 ascent and descent are the values of what is drawn by
2073 draw_glyphs (i.e. the values of the overall glyphs composed). */
2074 it->nglyphs = 1;
2075
2076 /* If we have not yet calculated pixel size data of glyphs of
2077 the composition for the current face font, calculate them
2078 now. Theoretically, we have to check all fonts for the
2079 glyphs, but that requires much time and memory space. So,
2080 here we check only the font of the first glyph. This leads
2081 to incorrect display very rarely, and C-l (recenter) can
2082 correct the display anyway. */
2083 if (cmp->font != (void *) font)
2084 {
2085 /* Ascent and descent of the font of the first character of
2086 this composition (adjusted by baseline offset). Ascent
2087 and descent of overall glyphs should not be less than
2088 them respectively. */
2089 int font_ascent = font->ascent + boff;
2090 int font_descent = font->descent - boff;
2091 /* Bounding box of the overall glyphs. */
2092 int leftmost, rightmost, lowest, highest;
329bed06 2093 int i, width, ascent, descent;
b4192550
KH
2094
2095 cmp->font = (void *) font;
2096
2097 /* Initialize the bounding box. */
1bdeec2e
KH
2098 if (font_info
2099 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2100 {
2101 width = pcm->width;
2102 ascent = pcm->ascent;
2103 descent = pcm->descent;
2104 }
2105 else
2106 {
2107 width = FONT_WIDTH (font);
2108 ascent = font->ascent;
2109 descent = font->descent;
2110 }
2111
2112 rightmost = width;
2113 lowest = - descent + boff;
2114 highest = ascent + boff;
b4192550 2115 leftmost = 0;
329bed06 2116
b4192550
KH
2117 if (font_info
2118 && font_info->default_ascent
2119 && CHAR_TABLE_P (Vuse_default_ascent)
2120 && !NILP (Faref (Vuse_default_ascent,
2121 make_number (it->char_to_display))))
2122 highest = font_info->default_ascent + boff;
2123
2124 /* Draw the first glyph at the normal position. It may be
2125 shifted to right later if some other glyphs are drawn at
2126 the left. */
2127 cmp->offsets[0] = 0;
2128 cmp->offsets[1] = boff;
2129
2130 /* Set cmp->offsets for the remaining glyphs. */
2131 for (i = 1; i < cmp->glyph_len; i++)
2132 {
2133 int left, right, btm, top;
2134 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2135 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2136
2137 face = FACE_FROM_ID (it->f, face_id);
2138 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2139 it->multibyte_p);
b4192550
KH
2140 font = face->font;
2141 if (font == NULL)
2142 {
2143 font = FRAME_FONT (it->f);
2144 boff = it->f->output_data.x->baseline_offset;
2145 font_info = NULL;
2146 }
2147 else
2148 {
2149 font_info
2150 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2151 boff = font_info->baseline_offset;
2152 if (font_info->vertical_centering)
2153 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2154 }
2155
1bdeec2e
KH
2156 if (font_info
2157 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2158 {
2159 width = pcm->width;
2160 ascent = pcm->ascent;
2161 descent = pcm->descent;
2162 }
2163 else
2164 {
2165 width = FONT_WIDTH (font);
1bdeec2e
KH
2166 ascent = 1;
2167 descent = 0;
329bed06 2168 }
b4192550
KH
2169
2170 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2171 {
2172 /* Relative composition with or without
2173 alternate chars. */
329bed06
GM
2174 left = (leftmost + rightmost - width) / 2;
2175 btm = - descent + boff;
b4192550
KH
2176 if (font_info && font_info->relative_compose
2177 && (! CHAR_TABLE_P (Vignore_relative_composition)
2178 || NILP (Faref (Vignore_relative_composition,
2179 make_number (ch)))))
2180 {
2181
329bed06 2182 if (- descent >= font_info->relative_compose)
b4192550
KH
2183 /* One extra pixel between two glyphs. */
2184 btm = highest + 1;
329bed06 2185 else if (ascent <= 0)
b4192550 2186 /* One extra pixel between two glyphs. */
329bed06 2187 btm = lowest - 1 - ascent - descent;
b4192550
KH
2188 }
2189 }
2190 else
2191 {
2192 /* A composition rule is specified by an integer
2193 value that encodes global and new reference
2194 points (GREF and NREF). GREF and NREF are
2195 specified by numbers as below:
2196
2197 0---1---2 -- ascent
2198 | |
2199 | |
2200 | |
2201 9--10--11 -- center
2202 | |
2203 ---3---4---5--- baseline
2204 | |
2205 6---7---8 -- descent
2206 */
2207 int rule = COMPOSITION_RULE (cmp, i);
2208 int gref, nref, grefx, grefy, nrefx, nrefy;
2209
2210 COMPOSITION_DECODE_RULE (rule, gref, nref);
2211 grefx = gref % 3, nrefx = nref % 3;
2212 grefy = gref / 3, nrefy = nref / 3;
2213
2214 left = (leftmost
2215 + grefx * (rightmost - leftmost) / 2
329bed06 2216 - nrefx * width / 2);
b4192550
KH
2217 btm = ((grefy == 0 ? highest
2218 : grefy == 1 ? 0
2219 : grefy == 2 ? lowest
2220 : (highest + lowest) / 2)
329bed06
GM
2221 - (nrefy == 0 ? ascent + descent
2222 : nrefy == 1 ? descent - boff
b4192550 2223 : nrefy == 2 ? 0
329bed06 2224 : (ascent + descent) / 2));
b4192550
KH
2225 }
2226
2227 cmp->offsets[i * 2] = left;
329bed06 2228 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2229
2230 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2231 right = left + width;
2232 top = btm + descent + ascent;
b4192550
KH
2233 if (left < leftmost)
2234 leftmost = left;
2235 if (right > rightmost)
2236 rightmost = right;
2237 if (top > highest)
2238 highest = top;
2239 if (btm < lowest)
2240 lowest = btm;
2241 }
2242
2243 /* If there are glyphs whose x-offsets are negative,
2244 shift all glyphs to the right and make all x-offsets
2245 non-negative. */
2246 if (leftmost < 0)
2247 {
2248 for (i = 0; i < cmp->glyph_len; i++)
2249 cmp->offsets[i * 2] -= leftmost;
2250 rightmost -= leftmost;
2251 }
2252
2253 cmp->pixel_width = rightmost;
2254 cmp->ascent = highest;
2255 cmp->descent = - lowest;
2256 if (cmp->ascent < font_ascent)
2257 cmp->ascent = font_ascent;
2258 if (cmp->descent < font_descent)
2259 cmp->descent = font_descent;
2260 }
2261
2262 it->pixel_width = cmp->pixel_width;
2263 it->ascent = it->phys_ascent = cmp->ascent;
2264 it->descent = it->phys_descent = cmp->descent;
2265
2266 if (face->box != FACE_NO_BOX)
2267 {
2268 int thick = face->box_line_width;
2269 it->ascent += thick;
2270 it->descent += thick;
2271
2272 if (it->start_of_box_run_p)
2273 it->pixel_width += thick;
2274 if (it->end_of_box_run_p)
2275 it->pixel_width += thick;
2276 }
2277
2278 /* If face has an overline, add the height of the overline
2279 (1 pixel) and a 1 pixel margin to the character height. */
2280 if (face->overline_p)
2281 it->ascent += 2;
2282
2283 take_vertical_position_into_account (it);
2284
2285 if (it->glyph_row)
2286 x_append_composite_glyph (it);
2287 }
06a2c219
GM
2288 else if (it->what == IT_IMAGE)
2289 x_produce_image_glyph (it);
2290 else if (it->what == IT_STRETCH)
2291 x_produce_stretch_glyph (it);
2292
3017fdd1
GM
2293 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2294 because this isn't true for images with `:ascent 100'. */
2295 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2296 if (it->area == TEXT_AREA)
2297 it->current_x += it->pixel_width;
66ac4b0e 2298
d365f5bb
GM
2299 it->descent += it->extra_line_spacing;
2300
06a2c219
GM
2301 it->max_ascent = max (it->max_ascent, it->ascent);
2302 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2303 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2304 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2305}
2306
2307
2308/* Estimate the pixel height of the mode or top line on frame F.
2309 FACE_ID specifies what line's height to estimate. */
2310
2311int
2312x_estimate_mode_line_height (f, face_id)
2313 struct frame *f;
2314 enum face_id face_id;
2315{
43281ee3 2316 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2317
2318 /* This function is called so early when Emacs starts that the face
2319 cache and mode line face are not yet initialized. */
2320 if (FRAME_FACE_CACHE (f))
2321 {
2322 struct face *face = FACE_FROM_ID (f, face_id);
2323 if (face)
43281ee3
GM
2324 {
2325 if (face->font)
2326 height = FONT_HEIGHT (face->font);
2327 height += 2 * face->box_line_width;
2328 }
06a2c219
GM
2329 }
2330
2331 return height;
2332}
2333
2334\f
2335/***********************************************************************
2336 Glyph display
2337 ***********************************************************************/
2338
2339/* A sequence of glyphs to be drawn in the same face.
2340
2341 This data structure is not really completely X specific, so it
2342 could possibly, at least partially, be useful for other systems. It
2343 is currently not part of the external redisplay interface because
2344 it's not clear what other systems will need. */
2345
2346struct glyph_string
2347{
2348 /* X-origin of the string. */
2349 int x;
2350
2351 /* Y-origin and y-position of the base line of this string. */
2352 int y, ybase;
2353
2354 /* The width of the string, not including a face extension. */
2355 int width;
2356
2357 /* The width of the string, including a face extension. */
2358 int background_width;
2359
2360 /* The height of this string. This is the height of the line this
2361 string is drawn in, and can be different from the height of the
2362 font the string is drawn in. */
2363 int height;
2364
2365 /* Number of pixels this string overwrites in front of its x-origin.
2366 This number is zero if the string has an lbearing >= 0; it is
2367 -lbearing, if the string has an lbearing < 0. */
2368 int left_overhang;
2369
2370 /* Number of pixels this string overwrites past its right-most
2371 nominal x-position, i.e. x + width. Zero if the string's
2372 rbearing is <= its nominal width, rbearing - width otherwise. */
2373 int right_overhang;
2374
2375 /* The frame on which the glyph string is drawn. */
2376 struct frame *f;
2377
2378 /* The window on which the glyph string is drawn. */
2379 struct window *w;
2380
2381 /* X display and window for convenience. */
2382 Display *display;
2383 Window window;
2384
2385 /* The glyph row for which this string was built. It determines the
2386 y-origin and height of the string. */
2387 struct glyph_row *row;
2388
2389 /* The area within row. */
2390 enum glyph_row_area area;
2391
2392 /* Characters to be drawn, and number of characters. */
2393 XChar2b *char2b;
2394 int nchars;
2395
06a2c219
GM
2396 /* A face-override for drawing cursors, mouse face and similar. */
2397 enum draw_glyphs_face hl;
2398
2399 /* Face in which this string is to be drawn. */
2400 struct face *face;
2401
2402 /* Font in which this string is to be drawn. */
2403 XFontStruct *font;
2404
2405 /* Font info for this string. */
2406 struct font_info *font_info;
2407
b4192550
KH
2408 /* Non-null means this string describes (part of) a composition.
2409 All characters from char2b are drawn composed. */
2410 struct composition *cmp;
06a2c219
GM
2411
2412 /* Index of this glyph string's first character in the glyph
b4192550
KH
2413 definition of CMP. If this is zero, this glyph string describes
2414 the first character of a composition. */
06a2c219
GM
2415 int gidx;
2416
2417 /* 1 means this glyph strings face has to be drawn to the right end
2418 of the window's drawing area. */
2419 unsigned extends_to_end_of_line_p : 1;
2420
2421 /* 1 means the background of this string has been drawn. */
2422 unsigned background_filled_p : 1;
2423
2424 /* 1 means glyph string must be drawn with 16-bit functions. */
2425 unsigned two_byte_p : 1;
2426
2427 /* 1 means that the original font determined for drawing this glyph
2428 string could not be loaded. The member `font' has been set to
2429 the frame's default font in this case. */
2430 unsigned font_not_found_p : 1;
2431
2432 /* 1 means that the face in which this glyph string is drawn has a
2433 stipple pattern. */
2434 unsigned stippled_p : 1;
2435
66ac4b0e
GM
2436 /* 1 means only the foreground of this glyph string must be drawn,
2437 and we should use the physical height of the line this glyph
2438 string appears in as clip rect. */
2439 unsigned for_overlaps_p : 1;
2440
06a2c219
GM
2441 /* The GC to use for drawing this glyph string. */
2442 GC gc;
2443
2444 /* A pointer to the first glyph in the string. This glyph
2445 corresponds to char2b[0]. Needed to draw rectangles if
2446 font_not_found_p is 1. */
2447 struct glyph *first_glyph;
2448
2449 /* Image, if any. */
2450 struct image *img;
2451
2452 struct glyph_string *next, *prev;
2453};
2454
2455
5c187dee 2456#if 0
06a2c219
GM
2457
2458static void
2459x_dump_glyph_string (s)
2460 struct glyph_string *s;
2461{
2462 fprintf (stderr, "glyph string\n");
2463 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2464 s->x, s->y, s->width, s->height);
2465 fprintf (stderr, " ybase = %d\n", s->ybase);
2466 fprintf (stderr, " hl = %d\n", s->hl);
2467 fprintf (stderr, " left overhang = %d, right = %d\n",
2468 s->left_overhang, s->right_overhang);
2469 fprintf (stderr, " nchars = %d\n", s->nchars);
2470 fprintf (stderr, " extends to end of line = %d\n",
2471 s->extends_to_end_of_line_p);
2472 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2473 fprintf (stderr, " bg width = %d\n", s->background_width);
2474}
2475
2476#endif /* GLYPH_DEBUG */
2477
2478
2479
2480static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2481 struct glyph_string **,
2482 struct glyph_string *,
2483 struct glyph_string *));
2484static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2485 struct glyph_string **,
2486 struct glyph_string *,
2487 struct glyph_string *));
2488static void x_append_glyph_string P_ ((struct glyph_string **,
2489 struct glyph_string **,
2490 struct glyph_string *));
2491static int x_left_overwritten P_ ((struct glyph_string *));
2492static int x_left_overwriting P_ ((struct glyph_string *));
2493static int x_right_overwritten P_ ((struct glyph_string *));
2494static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2495static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2496 int));
06a2c219
GM
2497static void x_init_glyph_string P_ ((struct glyph_string *,
2498 XChar2b *, struct window *,
2499 struct glyph_row *,
2500 enum glyph_row_area, int,
2501 enum draw_glyphs_face));
2502static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2503 enum glyph_row_area, int, int,
66ac4b0e 2504 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2505static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2506static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2507static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2508 int));
2509static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2510static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2511static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2512static void x_draw_glyph_string P_ ((struct glyph_string *));
2513static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2514static void x_set_cursor_gc P_ ((struct glyph_string *));
2515static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2516static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2517static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2518 int *, int *));
2519static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2520static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2521 unsigned long *, double, int));
06a2c219 2522static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2523 double, int, unsigned long));
06a2c219
GM
2524static void x_setup_relief_colors P_ ((struct glyph_string *));
2525static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2526static void x_draw_image_relief P_ ((struct glyph_string *));
2527static void x_draw_image_foreground P_ ((struct glyph_string *));
2528static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2529static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2530static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2531 int, int, int));
2532static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2533 int, int, int, int, XRectangle *));
2534static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2535 int, int, int, XRectangle *));
66ac4b0e
GM
2536static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2537 enum glyph_row_area));
209f68d9
GM
2538static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2539 struct glyph_row *,
2540 enum glyph_row_area, int, int));
06a2c219 2541
163dcff3
GM
2542#if GLYPH_DEBUG
2543static void x_check_font P_ ((struct frame *, XFontStruct *));
2544#endif
2545
06a2c219 2546
06a2c219
GM
2547/* Append the list of glyph strings with head H and tail T to the list
2548 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2549
2550static INLINE void
2551x_append_glyph_string_lists (head, tail, h, t)
2552 struct glyph_string **head, **tail;
2553 struct glyph_string *h, *t;
2554{
2555 if (h)
2556 {
2557 if (*head)
2558 (*tail)->next = h;
2559 else
2560 *head = h;
2561 h->prev = *tail;
2562 *tail = t;
2563 }
2564}
2565
2566
2567/* Prepend the list of glyph strings with head H and tail T to the
2568 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2569 result. */
2570
2571static INLINE void
2572x_prepend_glyph_string_lists (head, tail, h, t)
2573 struct glyph_string **head, **tail;
2574 struct glyph_string *h, *t;
2575{
2576 if (h)
2577 {
2578 if (*head)
2579 (*head)->prev = t;
2580 else
2581 *tail = t;
2582 t->next = *head;
2583 *head = h;
2584 }
2585}
2586
2587
2588/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2589 Set *HEAD and *TAIL to the resulting list. */
2590
2591static INLINE void
2592x_append_glyph_string (head, tail, s)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *s;
2595{
2596 s->next = s->prev = NULL;
2597 x_append_glyph_string_lists (head, tail, s, s);
2598}
2599
2600
2601/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2602 face. */
2603
2604static void
2605x_set_cursor_gc (s)
2606 struct glyph_string *s;
2607{
2608 if (s->font == FRAME_FONT (s->f)
2609 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2610 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2611 && !s->cmp)
06a2c219
GM
2612 s->gc = s->f->output_data.x->cursor_gc;
2613 else
2614 {
2615 /* Cursor on non-default face: must merge. */
2616 XGCValues xgcv;
2617 unsigned long mask;
2618
2619 xgcv.background = s->f->output_data.x->cursor_pixel;
2620 xgcv.foreground = s->face->background;
2621
2622 /* If the glyph would be invisible, try a different foreground. */
2623 if (xgcv.foreground == xgcv.background)
2624 xgcv.foreground = s->face->foreground;
2625 if (xgcv.foreground == xgcv.background)
2626 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2627 if (xgcv.foreground == xgcv.background)
2628 xgcv.foreground = s->face->foreground;
2629
2630 /* Make sure the cursor is distinct from text in this face. */
2631 if (xgcv.background == s->face->background
2632 && xgcv.foreground == s->face->foreground)
2633 {
2634 xgcv.background = s->face->foreground;
2635 xgcv.foreground = s->face->background;
2636 }
2637
2638 IF_DEBUG (x_check_font (s->f, s->font));
2639 xgcv.font = s->font->fid;
2640 xgcv.graphics_exposures = False;
2641 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2642
2643 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2644 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2645 mask, &xgcv);
2646 else
2647 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2648 = XCreateGC (s->display, s->window, mask, &xgcv);
2649
2650 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2651 }
2652}
2653
2654
2655/* Set up S->gc of glyph string S for drawing text in mouse face. */
2656
2657static void
2658x_set_mouse_face_gc (s)
2659 struct glyph_string *s;
2660{
2661 int face_id;
ee569018 2662 struct face *face;
06a2c219 2663
e4ded23c 2664 /* What face has to be used last for the mouse face? */
06a2c219 2665 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2666 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2667 if (face == NULL)
2668 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2669
033e3e18
GM
2670 if (s->first_glyph->type == CHAR_GLYPH)
2671 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2672 else
2673 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2674 s->face = FACE_FROM_ID (s->f, face_id);
2675 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2676
2677 /* If font in this face is same as S->font, use it. */
2678 if (s->font == s->face->font)
2679 s->gc = s->face->gc;
2680 else
2681 {
2682 /* Otherwise construct scratch_cursor_gc with values from FACE
2683 but font FONT. */
2684 XGCValues xgcv;
2685 unsigned long mask;
2686
2687 xgcv.background = s->face->background;
2688 xgcv.foreground = s->face->foreground;
2689 IF_DEBUG (x_check_font (s->f, s->font));
2690 xgcv.font = s->font->fid;
2691 xgcv.graphics_exposures = False;
2692 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2693
2694 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2695 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2696 mask, &xgcv);
2697 else
2698 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2699 = XCreateGC (s->display, s->window, mask, &xgcv);
2700
2701 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2702 }
2703
2704 xassert (s->gc != 0);
2705}
2706
2707
2708/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2709 Faces to use in the mode line have already been computed when the
2710 matrix was built, so there isn't much to do, here. */
2711
2712static INLINE void
2713x_set_mode_line_face_gc (s)
2714 struct glyph_string *s;
2715{
2716 s->gc = s->face->gc;
06a2c219
GM
2717}
2718
2719
2720/* Set S->gc of glyph string S for drawing that glyph string. Set
2721 S->stippled_p to a non-zero value if the face of S has a stipple
2722 pattern. */
2723
2724static INLINE void
2725x_set_glyph_string_gc (s)
2726 struct glyph_string *s;
2727{
209f68d9
GM
2728 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2729
06a2c219
GM
2730 if (s->hl == DRAW_NORMAL_TEXT)
2731 {
2732 s->gc = s->face->gc;
2733 s->stippled_p = s->face->stipple != 0;
2734 }
2735 else if (s->hl == DRAW_INVERSE_VIDEO)
2736 {
2737 x_set_mode_line_face_gc (s);
2738 s->stippled_p = s->face->stipple != 0;
2739 }
2740 else if (s->hl == DRAW_CURSOR)
2741 {
2742 x_set_cursor_gc (s);
2743 s->stippled_p = 0;
2744 }
2745 else if (s->hl == DRAW_MOUSE_FACE)
2746 {
2747 x_set_mouse_face_gc (s);
2748 s->stippled_p = s->face->stipple != 0;
2749 }
2750 else if (s->hl == DRAW_IMAGE_RAISED
2751 || s->hl == DRAW_IMAGE_SUNKEN)
2752 {
2753 s->gc = s->face->gc;
2754 s->stippled_p = s->face->stipple != 0;
2755 }
2756 else
2757 {
2758 s->gc = s->face->gc;
2759 s->stippled_p = s->face->stipple != 0;
2760 }
2761
2762 /* GC must have been set. */
2763 xassert (s->gc != 0);
2764}
2765
2766
2767/* Return in *R the clipping rectangle for glyph string S. */
2768
2769static void
2770x_get_glyph_string_clip_rect (s, r)
2771 struct glyph_string *s;
2772 XRectangle *r;
2773{
2774 if (s->row->full_width_p)
2775 {
2776 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2777 int canon_x = CANON_X_UNIT (s->f);
2778
2779 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2780 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2781
2782 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2783 {
1da3fd71 2784 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2785 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2786 r->x -= width;
2787 }
2788
b9432a85 2789 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2790
06a2c219
GM
2791 /* Unless displaying a mode or menu bar line, which are always
2792 fully visible, clip to the visible part of the row. */
2793 if (s->w->pseudo_window_p)
2794 r->height = s->row->visible_height;
2795 else
2796 r->height = s->height;
2797 }
2798 else
2799 {
2800 /* This is a text line that may be partially visible. */
2801 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2802 r->width = window_box_width (s->w, s->area);
2803 r->height = s->row->visible_height;
2804 }
2805
2806 /* Don't use S->y for clipping because it doesn't take partially
2807 visible lines into account. For example, it can be negative for
2808 partially visible lines at the top of a window. */
2809 if (!s->row->full_width_p
2810 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2811 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2812 else
2813 r->y = max (0, s->row->y);
06a2c219 2814
9ea173e8 2815 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2816 at the top of the window. */
9ea173e8 2817 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2818 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2819
2820 /* If S draws overlapping rows, it's sufficient to use the top and
2821 bottom of the window for clipping because this glyph string
2822 intentionally draws over other lines. */
2823 if (s->for_overlaps_p)
2824 {
045dee35 2825 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2826 r->height = window_text_bottom_y (s->w) - r->y;
2827 }
2828
2829 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2830}
2831
2832
2833/* Set clipping for output of glyph string S. S may be part of a mode
2834 line or menu if we don't have X toolkit support. */
2835
2836static INLINE void
2837x_set_glyph_string_clipping (s)
2838 struct glyph_string *s;
2839{
2840 XRectangle r;
2841 x_get_glyph_string_clip_rect (s, &r);
2842 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2843}
2844
2845
2846/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2847 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2848
2849static INLINE void
2850x_compute_glyph_string_overhangs (s)
2851 struct glyph_string *s;
2852{
b4192550 2853 if (s->cmp == NULL
06a2c219
GM
2854 && s->first_glyph->type == CHAR_GLYPH)
2855 {
2856 XCharStruct cs;
2857 int direction, font_ascent, font_descent;
2858 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2859 &font_ascent, &font_descent, &cs);
2860 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2861 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2862 }
2863}
2864
2865
2866/* Compute overhangs and x-positions for glyph string S and its
2867 predecessors, or successors. X is the starting x-position for S.
2868 BACKWARD_P non-zero means process predecessors. */
2869
2870static void
2871x_compute_overhangs_and_x (s, x, backward_p)
2872 struct glyph_string *s;
2873 int x;
2874 int backward_p;
2875{
2876 if (backward_p)
2877 {
2878 while (s)
2879 {
2880 x_compute_glyph_string_overhangs (s);
2881 x -= s->width;
2882 s->x = x;
2883 s = s->prev;
2884 }
2885 }
2886 else
2887 {
2888 while (s)
2889 {
2890 x_compute_glyph_string_overhangs (s);
2891 s->x = x;
2892 x += s->width;
2893 s = s->next;
2894 }
2895 }
2896}
2897
2898
2899/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2900 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2901 assumed to be zero. */
06a2c219
GM
2902
2903static void
2904x_get_glyph_overhangs (glyph, f, left, right)
2905 struct glyph *glyph;
2906 struct frame *f;
2907 int *left, *right;
2908{
06a2c219
GM
2909 *left = *right = 0;
2910
b4192550 2911 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2912 {
2913 XFontStruct *font;
2914 struct face *face;
2915 struct font_info *font_info;
2916 XChar2b char2b;
ee569018
KH
2917 XCharStruct *pcm;
2918
2919 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2920 font = face->font;
2921 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2922 if (font
2923 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2924 {
06a2c219
GM
2925 if (pcm->rbearing > pcm->width)
2926 *right = pcm->rbearing - pcm->width;
2927 if (pcm->lbearing < 0)
2928 *left = -pcm->lbearing;
2929 }
2930 }
2931}
2932
2933
2934/* Return the index of the first glyph preceding glyph string S that
2935 is overwritten by S because of S's left overhang. Value is -1
2936 if no glyphs are overwritten. */
2937
2938static int
2939x_left_overwritten (s)
2940 struct glyph_string *s;
2941{
2942 int k;
2943
2944 if (s->left_overhang)
2945 {
2946 int x = 0, i;
2947 struct glyph *glyphs = s->row->glyphs[s->area];
2948 int first = s->first_glyph - glyphs;
2949
2950 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2951 x -= glyphs[i].pixel_width;
2952
2953 k = i + 1;
2954 }
2955 else
2956 k = -1;
2957
2958 return k;
2959}
2960
2961
2962/* Return the index of the first glyph preceding glyph string S that
2963 is overwriting S because of its right overhang. Value is -1 if no
2964 glyph in front of S overwrites S. */
2965
2966static int
2967x_left_overwriting (s)
2968 struct glyph_string *s;
2969{
2970 int i, k, x;
2971 struct glyph *glyphs = s->row->glyphs[s->area];
2972 int first = s->first_glyph - glyphs;
2973
2974 k = -1;
2975 x = 0;
2976 for (i = first - 1; i >= 0; --i)
2977 {
2978 int left, right;
2979 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2980 if (x + right > 0)
2981 k = i;
2982 x -= glyphs[i].pixel_width;
2983 }
2984
2985 return k;
2986}
2987
2988
2989/* Return the index of the last glyph following glyph string S that is
2990 not overwritten by S because of S's right overhang. Value is -1 if
2991 no such glyph is found. */
2992
2993static int
2994x_right_overwritten (s)
2995 struct glyph_string *s;
2996{
2997 int k = -1;
2998
2999 if (s->right_overhang)
3000 {
3001 int x = 0, i;
3002 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3003 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3004 int end = s->row->used[s->area];
3005
3006 for (i = first; i < end && s->right_overhang > x; ++i)
3007 x += glyphs[i].pixel_width;
3008
3009 k = i;
3010 }
3011
3012 return k;
3013}
3014
3015
3016/* Return the index of the last glyph following glyph string S that
3017 overwrites S because of its left overhang. Value is negative
3018 if no such glyph is found. */
3019
3020static int
3021x_right_overwriting (s)
3022 struct glyph_string *s;
3023{
3024 int i, k, x;
3025 int end = s->row->used[s->area];
3026 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3027 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3028
3029 k = -1;
3030 x = 0;
3031 for (i = first; i < end; ++i)
3032 {
3033 int left, right;
3034 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3035 if (x - left < 0)
3036 k = i;
3037 x += glyphs[i].pixel_width;
3038 }
3039
3040 return k;
3041}
3042
3043
3044/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3045
3046static INLINE void
3047x_clear_glyph_string_rect (s, x, y, w, h)
3048 struct glyph_string *s;
3049 int x, y, w, h;
3050{
3051 XGCValues xgcv;
3052 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3053 XSetForeground (s->display, s->gc, xgcv.background);
3054 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3055 XSetForeground (s->display, s->gc, xgcv.foreground);
3056}
3057
3058
3059/* Draw the background of glyph_string S. If S->background_filled_p
3060 is non-zero don't draw it. FORCE_P non-zero means draw the
3061 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3062 when a string preceding S draws into the background of S, or S
3063 contains the first component of a composition. */
06a2c219
GM
3064
3065static void
3066x_draw_glyph_string_background (s, force_p)
3067 struct glyph_string *s;
3068 int force_p;
3069{
3070 /* Nothing to do if background has already been drawn or if it
3071 shouldn't be drawn in the first place. */
3072 if (!s->background_filled_p)
3073 {
b4192550 3074 if (s->stippled_p)
06a2c219
GM
3075 {
3076 /* Fill background with a stipple pattern. */
3077 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3078 XFillRectangle (s->display, s->window, s->gc, s->x,
3079 s->y + s->face->box_line_width,
3080 s->background_width,
3081 s->height - 2 * s->face->box_line_width);
3082 XSetFillStyle (s->display, s->gc, FillSolid);
3083 s->background_filled_p = 1;
3084 }
3085 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3086 || s->font_not_found_p
3087 || s->extends_to_end_of_line_p
06a2c219
GM
3088 || force_p)
3089 {
3090 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3091 s->background_width,
3092 s->height - 2 * s->face->box_line_width);
3093 s->background_filled_p = 1;
3094 }
3095 }
3096}
3097
3098
3099/* Draw the foreground of glyph string S. */
3100
3101static void
3102x_draw_glyph_string_foreground (s)
3103 struct glyph_string *s;
3104{
3105 int i, x;
3106
3107 /* If first glyph of S has a left box line, start drawing the text
3108 of S to the right of that box line. */
3109 if (s->face->box != FACE_NO_BOX
3110 && s->first_glyph->left_box_line_p)
3111 x = s->x + s->face->box_line_width;
3112 else
3113 x = s->x;
3114
b4192550
KH
3115 /* Draw characters of S as rectangles if S's font could not be
3116 loaded. */
3117 if (s->font_not_found_p)
06a2c219 3118 {
b4192550 3119 for (i = 0; i < s->nchars; ++i)
06a2c219 3120 {
b4192550
KH
3121 struct glyph *g = s->first_glyph + i;
3122 XDrawRectangle (s->display, s->window,
3123 s->gc, x, s->y, g->pixel_width - 1,
3124 s->height - 1);
3125 x += g->pixel_width;
06a2c219
GM
3126 }
3127 }
3128 else
3129 {
b4192550
KH
3130 char *char1b = (char *) s->char2b;
3131 int boff = s->font_info->baseline_offset;
06a2c219 3132
b4192550
KH
3133 if (s->font_info->vertical_centering)
3134 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3135
3136 /* If we can use 8-bit functions, condense S->char2b. */
3137 if (!s->two_byte_p)
3138 for (i = 0; i < s->nchars; ++i)
3139 char1b[i] = s->char2b[i].byte2;
3140
3141 /* Draw text with XDrawString if background has already been
3142 filled. Otherwise, use XDrawImageString. (Note that
3143 XDrawImageString is usually faster than XDrawString.) Always
3144 use XDrawImageString when drawing the cursor so that there is
3145 no chance that characters under a box cursor are invisible. */
3146 if (s->for_overlaps_p
3147 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3148 {
3149 /* Draw characters with 16-bit or 8-bit functions. */
3150 if (s->two_byte_p)
3151 XDrawString16 (s->display, s->window, s->gc, x,
3152 s->ybase - boff, s->char2b, s->nchars);
3153 else
3154 XDrawString (s->display, s->window, s->gc, x,
3155 s->ybase - boff, char1b, s->nchars);
3156 }
06a2c219
GM
3157 else
3158 {
b4192550
KH
3159 if (s->two_byte_p)
3160 XDrawImageString16 (s->display, s->window, s->gc, x,
3161 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3162 else
b4192550
KH
3163 XDrawImageString (s->display, s->window, s->gc, x,
3164 s->ybase - boff, char1b, s->nchars);
3165 }
3166 }
3167}
06a2c219 3168
b4192550 3169/* Draw the foreground of composite glyph string S. */
06a2c219 3170
b4192550
KH
3171static void
3172x_draw_composite_glyph_string_foreground (s)
3173 struct glyph_string *s;
3174{
3175 int i, x;
06a2c219 3176
b4192550
KH
3177 /* If first glyph of S has a left box line, start drawing the text
3178 of S to the right of that box line. */
3179 if (s->face->box != FACE_NO_BOX
3180 && s->first_glyph->left_box_line_p)
3181 x = s->x + s->face->box_line_width;
3182 else
3183 x = s->x;
06a2c219 3184
b4192550
KH
3185 /* S is a glyph string for a composition. S->gidx is the index of
3186 the first character drawn for glyphs of this composition.
3187 S->gidx == 0 means we are drawing the very first character of
3188 this composition. */
06a2c219 3189
b4192550
KH
3190 /* Draw a rectangle for the composition if the font for the very
3191 first character of the composition could not be loaded. */
3192 if (s->font_not_found_p)
3193 {
3194 if (s->gidx == 0)
3195 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3196 s->width - 1, s->height - 1);
3197 }
3198 else
3199 {
3200 for (i = 0; i < s->nchars; i++, ++s->gidx)
3201 XDrawString16 (s->display, s->window, s->gc,
3202 x + s->cmp->offsets[s->gidx * 2],
3203 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3204 s->char2b + i, 1);
06a2c219
GM
3205 }
3206}
3207
3208
80c32bcc
GM
3209#ifdef USE_X_TOOLKIT
3210
3e71d8f2 3211static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3212
3e71d8f2
GM
3213
3214/* Return the frame on which widget WIDGET is used.. Abort if frame
3215 cannot be determined. */
3216
e851c833 3217static struct frame *
3e71d8f2 3218x_frame_of_widget (widget)
80c32bcc 3219 Widget widget;
80c32bcc 3220{
80c32bcc 3221 struct x_display_info *dpyinfo;
5c187dee 3222 Lisp_Object tail;
3e71d8f2
GM
3223 struct frame *f;
3224
80c32bcc
GM
3225 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3226
3227 /* Find the top-level shell of the widget. Note that this function
3228 can be called when the widget is not yet realized, so XtWindow
3229 (widget) == 0. That's the reason we can't simply use
3230 x_any_window_to_frame. */
3231 while (!XtIsTopLevelShell (widget))
3232 widget = XtParent (widget);
3233
3234 /* Look for a frame with that top-level widget. Allocate the color
3235 on that frame to get the right gamma correction value. */
3236 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3237 if (GC_FRAMEP (XCAR (tail))
3238 && (f = XFRAME (XCAR (tail)),
3239 (f->output_data.nothing != 1
3240 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3241 && f->output_data.x->widget == widget)
3e71d8f2 3242 return f;
80c32bcc
GM
3243
3244 abort ();
3245}
3246
3e71d8f2
GM
3247
3248/* Allocate the color COLOR->pixel on the screen and display of
3249 widget WIDGET in colormap CMAP. If an exact match cannot be
3250 allocated, try the nearest color available. Value is non-zero
3251 if successful. This is called from lwlib. */
3252
3253int
3254x_alloc_nearest_color_for_widget (widget, cmap, color)
3255 Widget widget;
3256 Colormap cmap;
3257 XColor *color;
3258{
3259 struct frame *f = x_frame_of_widget (widget);
3260 return x_alloc_nearest_color (f, cmap, color);
3261}
3262
3263
46d516e5
MB
3264/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3265 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3266 If this produces the same color as PIXEL, try a color where all RGB
3267 values have DELTA added. Return the allocated color in *PIXEL.
3268 DISPLAY is the X display, CMAP is the colormap to operate on.
3269 Value is non-zero if successful. */
3270
3271int
3272x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3273 Widget widget;
3274 Display *display;
3275 Colormap cmap;
3276 unsigned long *pixel;
3277 double factor;
3278 int delta;
3279{
3280 struct frame *f = x_frame_of_widget (widget);
3281 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3282}
3283
3284
80c32bcc
GM
3285#endif /* USE_X_TOOLKIT */
3286
3287
f04e1297
GM
3288/* Value is an array of XColor structures for the contents of the
3289 color map of frame F. Set *NCELLS to the size of the array.
3290 Note that this probably shouldn't be called for large color maps,
3291 say a 24-bit TrueColor map. */
3292
3293static const XColor *
3294x_color_cells (f, ncells)
3295 struct frame *f;
3296 int *ncells;
3297{
3298 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3299
3300 if (dpyinfo->color_cells == NULL)
3301 {
3302 Display *display = FRAME_X_DISPLAY (f);
3303 Screen *screen = FRAME_X_SCREEN (f);
3304 int i;
3305
3306 dpyinfo->ncolor_cells
3307 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3308 dpyinfo->color_cells
3309 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3310 * sizeof *dpyinfo->color_cells);
3311
3312 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3313 dpyinfo->color_cells[i].pixel = i;
3314
3315 XQueryColors (display, FRAME_X_COLORMAP (f),
3316 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3317 }
3318
3319 *ncells = dpyinfo->ncolor_cells;
3320 return dpyinfo->color_cells;
3321}
3322
3323
3324/* On frame F, translate pixel colors to RGB values for the NCOLORS
3325 colors in COLORS. Use cached information, if available. */
3326
3327void
3328x_query_colors (f, colors, ncolors)
3329 struct frame *f;
3330 XColor *colors;
3331 int ncolors;
3332{
3333 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3334
3335 if (dpyinfo->color_cells)
3336 {
3337 int i;
3338 for (i = 0; i < ncolors; ++i)
3339 {
3340 unsigned long pixel = colors[i].pixel;
3341 xassert (pixel < dpyinfo->ncolor_cells);
3342 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3343 colors[i] = dpyinfo->color_cells[pixel];
3344 }
3345 }
3346 else
3347 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3348}
3349
3350
3351/* On frame F, translate pixel color to RGB values for the color in
3352 COLOR. Use cached information, if available. */
3353
3354void
3355x_query_color (f, color)
3356 struct frame *f;
3357 XColor *color;
3358{
3359 x_query_colors (f, color, 1);
3360}
3361
3362
06a2c219
GM
3363/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3364 CMAP. If an exact match can't be allocated, try the nearest color
3365 available. Value is non-zero if successful. Set *COLOR to the
3366 color allocated. */
3367
3368int
80c32bcc
GM
3369x_alloc_nearest_color (f, cmap, color)
3370 struct frame *f;
06a2c219
GM
3371 Colormap cmap;
3372 XColor *color;
3373{
80c32bcc
GM
3374 Display *display = FRAME_X_DISPLAY (f);
3375 Screen *screen = FRAME_X_SCREEN (f);
3376 int rc;
3377
3378 gamma_correct (f, color);
3379 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3380 if (rc == 0)
3381 {
3382 /* If we got to this point, the colormap is full, so we're going
3383 to try to get the next closest color. The algorithm used is
3384 a least-squares matching, which is what X uses for closest
3385 color matching with StaticColor visuals. */
3386 int nearest, i;
3387 unsigned long nearest_delta = ~0;
f04e1297
GM
3388 int ncells;
3389 const XColor *cells = x_color_cells (f, &ncells);
06a2c219
GM
3390
3391 for (nearest = i = 0; i < ncells; ++i)
3392 {
3393 long dred = (color->red >> 8) - (cells[i].red >> 8);
3394 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3395 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3396 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3397
3398 if (delta < nearest_delta)
3399 {
3400 nearest = i;
3401 nearest_delta = delta;
3402 }
3403 }
3404
3405 color->red = cells[nearest].red;
3406 color->green = cells[nearest].green;
3407 color->blue = cells[nearest].blue;
3408 rc = XAllocColor (display, cmap, color);
3409 }
35efe0a1
GM
3410 else
3411 {
3412 /* If allocation succeeded, and the allocated pixel color is not
3413 equal to a cached pixel color recorded earlier, there was a
3414 change in the colormap, so clear the color cache. */
3415 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3416 XColor *cached_color;
3417
3418 if (dpyinfo->color_cells
3419 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3420 (cached_color->red != color->red
3421 || cached_color->blue != color->blue
3422 || cached_color->green != color->green)))
35efe0a1
GM
3423 {
3424 xfree (dpyinfo->color_cells);
3425 dpyinfo->color_cells = NULL;
3426 dpyinfo->ncolor_cells = 0;
3427 }
3428 }
06a2c219 3429
d9c545da
GM
3430#ifdef DEBUG_X_COLORS
3431 if (rc)
3432 register_color (color->pixel);
3433#endif /* DEBUG_X_COLORS */
3434
06a2c219
GM
3435 return rc;
3436}
3437
3438
d9c545da
GM
3439/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3440 It's necessary to do this instead of just using PIXEL directly to
3441 get color reference counts right. */
3442
3443unsigned long
3444x_copy_color (f, pixel)
3445 struct frame *f;
3446 unsigned long pixel;
3447{
3448 XColor color;
3449
3450 color.pixel = pixel;
3451 BLOCK_INPUT;
f04e1297 3452 x_query_color (f, &color);
d9c545da
GM
3453 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3454 UNBLOCK_INPUT;
3455#ifdef DEBUG_X_COLORS
3456 register_color (pixel);
3457#endif
3458 return color.pixel;
3459}
3460
3461
3e71d8f2
GM
3462/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3463 It's necessary to do this instead of just using PIXEL directly to
3464 get color reference counts right. */
3465
3466unsigned long
3467x_copy_dpy_color (dpy, cmap, pixel)
3468 Display *dpy;
3469 Colormap cmap;
3470 unsigned long pixel;
3471{
3472 XColor color;
3473
3474 color.pixel = pixel;
3475 BLOCK_INPUT;
3476 XQueryColor (dpy, cmap, &color);
3477 XAllocColor (dpy, cmap, &color);
3478 UNBLOCK_INPUT;
3479#ifdef DEBUG_X_COLORS
3480 register_color (pixel);
3481#endif
3482 return color.pixel;
3483}
3484
3485
6d8b0acd 3486/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3487 boosted.
6d8b0acd 3488
d7361edf
MB
3489 Nominally, highlight colors for `3d' faces are calculated by
3490 brightening an object's color by a constant scale factor, but this
3491 doesn't yield good results for dark colors, so for colors who's
3492 brightness is less than this value (on a scale of 0-65535) have an
3493 use an additional additive factor.
6d8b0acd
MB
3494
3495 The value here is set so that the default menu-bar/mode-line color
3496 (grey75) will not have its highlights changed at all. */
3497#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3498
3499
06a2c219
GM
3500/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3501 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3502 If this produces the same color as PIXEL, try a color where all RGB
3503 values have DELTA added. Return the allocated color in *PIXEL.
3504 DISPLAY is the X display, CMAP is the colormap to operate on.
3505 Value is non-zero if successful. */
3506
3507static int
3508x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3509 struct frame *f;
3510 Display *display;
3511 Colormap cmap;
3512 unsigned long *pixel;
68c45bf0 3513 double factor;
06a2c219
GM
3514 int delta;
3515{
3516 XColor color, new;
6d8b0acd 3517 long bright;
06a2c219
GM
3518 int success_p;
3519
3520 /* Get RGB color values. */
3521 color.pixel = *pixel;
f04e1297 3522 x_query_color (f, &color);
06a2c219
GM
3523
3524 /* Change RGB values by specified FACTOR. Avoid overflow! */
3525 xassert (factor >= 0);
3526 new.red = min (0xffff, factor * color.red);
3527 new.green = min (0xffff, factor * color.green);
3528 new.blue = min (0xffff, factor * color.blue);
3529
d7361edf
MB
3530 /* Calculate brightness of COLOR. */
3531 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3532
3533 /* We only boost colors that are darker than
3534 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3535 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3536 /* Make an additive adjustment to NEW, because it's dark enough so
3537 that scaling by FACTOR alone isn't enough. */
3538 {
3539 /* How far below the limit this color is (0 - 1, 1 being darker). */
3540 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3541 /* The additive adjustment. */
d7361edf 3542 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3543
3544 if (factor < 1)
3545 {
6d8b0acd
MB
3546 new.red = max (0, new.red - min_delta);
3547 new.green = max (0, new.green - min_delta);
3548 new.blue = max (0, new.blue - min_delta);
3549 }
3550 else
3551 {
3552 new.red = min (0xffff, min_delta + new.red);
3553 new.green = min (0xffff, min_delta + new.green);
3554 new.blue = min (0xffff, min_delta + new.blue);
3555 }
3556 }
3557
06a2c219 3558 /* Try to allocate the color. */
80c32bcc 3559 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3560 if (success_p)
3561 {
3562 if (new.pixel == *pixel)
3563 {
3564 /* If we end up with the same color as before, try adding
3565 delta to the RGB values. */
0d605c67 3566 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3567
3568 new.red = min (0xffff, delta + color.red);
3569 new.green = min (0xffff, delta + color.green);
3570 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3571 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3572 }
3573 else
3574 success_p = 1;
3575 *pixel = new.pixel;
3576 }
3577
3578 return success_p;
3579}
3580
3581
3582/* Set up the foreground color for drawing relief lines of glyph
3583 string S. RELIEF is a pointer to a struct relief containing the GC
3584 with which lines will be drawn. Use a color that is FACTOR or
3585 DELTA lighter or darker than the relief's background which is found
3586 in S->f->output_data.x->relief_background. If such a color cannot
3587 be allocated, use DEFAULT_PIXEL, instead. */
3588
3589static void
3590x_setup_relief_color (f, relief, factor, delta, default_pixel)
3591 struct frame *f;
3592 struct relief *relief;
68c45bf0 3593 double factor;
06a2c219
GM
3594 int delta;
3595 unsigned long default_pixel;
3596{
3597 XGCValues xgcv;
3598 struct x_output *di = f->output_data.x;
3599 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3600 unsigned long pixel;
3601 unsigned long background = di->relief_background;
43bd1b2b 3602 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3603 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3604 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3605
3606 xgcv.graphics_exposures = False;
3607 xgcv.line_width = 1;
3608
3609 /* Free previously allocated color. The color cell will be reused
3610 when it has been freed as many times as it was allocated, so this
3611 doesn't affect faces using the same colors. */
3612 if (relief->gc
3613 && relief->allocated_p)
3614 {
0d605c67 3615 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3616 relief->allocated_p = 0;
3617 }
3618
3619 /* Allocate new color. */
3620 xgcv.foreground = default_pixel;
3621 pixel = background;
dcd08bfb
GM
3622 if (dpyinfo->n_planes != 1
3623 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3624 {
3625 relief->allocated_p = 1;
3626 xgcv.foreground = relief->pixel = pixel;
3627 }
3628
3629 if (relief->gc == 0)
3630 {
dcd08bfb 3631 xgcv.stipple = dpyinfo->gray;
06a2c219 3632 mask |= GCStipple;
dcd08bfb 3633 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3634 }
3635 else
dcd08bfb 3636 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3637}
3638
3639
3640/* Set up colors for the relief lines around glyph string S. */
3641
3642static void
3643x_setup_relief_colors (s)
3644 struct glyph_string *s;
3645{
3646 struct x_output *di = s->f->output_data.x;
3647 unsigned long color;
3648
3649 if (s->face->use_box_color_for_shadows_p)
3650 color = s->face->box_color;
3651 else
3652 {
3653 XGCValues xgcv;
3654
3655 /* Get the background color of the face. */
3656 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3657 color = xgcv.background;
3658 }
3659
3660 if (di->white_relief.gc == 0
3661 || color != di->relief_background)
3662 {
3663 di->relief_background = color;
3664 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3665 WHITE_PIX_DEFAULT (s->f));
3666 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3667 BLACK_PIX_DEFAULT (s->f));
3668 }
3669}
3670
3671
3672/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3673 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3674 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3675 relief. LEFT_P non-zero means draw a relief on the left side of
3676 the rectangle. RIGHT_P non-zero means draw a relief on the right
3677 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3678 when drawing. */
3679
3680static void
3681x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3682 raised_p, left_p, right_p, clip_rect)
3683 struct frame *f;
3684 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3685 XRectangle *clip_rect;
3686{
3687 int i;
3688 GC gc;
3689
3690 if (raised_p)
3691 gc = f->output_data.x->white_relief.gc;
3692 else
3693 gc = f->output_data.x->black_relief.gc;
3694 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3695
3696 /* Top. */
3697 for (i = 0; i < width; ++i)
3698 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3699 left_x + i * left_p, top_y + i,
3700 right_x + 1 - i * right_p, top_y + i);
3701
3702 /* Left. */
3703 if (left_p)
3704 for (i = 0; i < width; ++i)
3705 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3706 left_x + i, top_y + i, left_x + i, bottom_y - i);
3707
3708 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3709 if (raised_p)
3710 gc = f->output_data.x->black_relief.gc;
3711 else
3712 gc = f->output_data.x->white_relief.gc;
3713 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3714
3715 /* Bottom. */
3716 for (i = 0; i < width; ++i)
3717 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3718 left_x + i * left_p, bottom_y - i,
3719 right_x + 1 - i * right_p, bottom_y - i);
3720
3721 /* Right. */
3722 if (right_p)
3723 for (i = 0; i < width; ++i)
3724 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3725 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3726
3727 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3728}
3729
3730
3731/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3732 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3733 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3734 left side of the rectangle. RIGHT_P non-zero means draw a line
3735 on the right side of the rectangle. CLIP_RECT is the clipping
3736 rectangle to use when drawing. */
3737
3738static void
3739x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3740 left_p, right_p, clip_rect)
3741 struct glyph_string *s;
3742 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3743 XRectangle *clip_rect;
3744{
3745 XGCValues xgcv;
3746
3747 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3748 XSetForeground (s->display, s->gc, s->face->box_color);
3749 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3750
3751 /* Top. */
3752 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3753 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3754
3755 /* Left. */
3756 if (left_p)
3757 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3758 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3759
3760 /* Bottom. */
3761 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3762 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3763
3764 /* Right. */
3765 if (right_p)
3766 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3767 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3768
3769 XSetForeground (s->display, s->gc, xgcv.foreground);
3770 XSetClipMask (s->display, s->gc, None);
3771}
3772
3773
3774/* Draw a box around glyph string S. */
3775
3776static void
3777x_draw_glyph_string_box (s)
3778 struct glyph_string *s;
3779{
3780 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3781 int left_p, right_p;
3782 struct glyph *last_glyph;
3783 XRectangle clip_rect;
3784
3785 last_x = window_box_right (s->w, s->area);
3786 if (s->row->full_width_p
3787 && !s->w->pseudo_window_p)
3788 {
110859fc 3789 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3790 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3791 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3792 }
3793
3794 /* The glyph that may have a right box line. */
b4192550 3795 last_glyph = (s->cmp || s->img
06a2c219
GM
3796 ? s->first_glyph
3797 : s->first_glyph + s->nchars - 1);
3798
3799 width = s->face->box_line_width;
3800 raised_p = s->face->box == FACE_RAISED_BOX;
3801 left_x = s->x;
3802 right_x = ((s->row->full_width_p
1da3fd71 3803 ? last_x - 1
a7aeb2de 3804 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3805 top_y = s->y;
3806 bottom_y = top_y + s->height - 1;
3807
3808 left_p = (s->first_glyph->left_box_line_p
3809 || (s->hl == DRAW_MOUSE_FACE
3810 && (s->prev == NULL
3811 || s->prev->hl != s->hl)));
3812 right_p = (last_glyph->right_box_line_p
3813 || (s->hl == DRAW_MOUSE_FACE
3814 && (s->next == NULL
3815 || s->next->hl != s->hl)));
3816
3817 x_get_glyph_string_clip_rect (s, &clip_rect);
3818
3819 if (s->face->box == FACE_SIMPLE_BOX)
3820 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3821 left_p, right_p, &clip_rect);
3822 else
3823 {
3824 x_setup_relief_colors (s);
3825 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3826 width, raised_p, left_p, right_p, &clip_rect);
3827 }
3828}
3829
3830
3831/* Draw foreground of image glyph string S. */
3832
3833static void
3834x_draw_image_foreground (s)
3835 struct glyph_string *s;
3836{
3837 int x;
95af8492 3838 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3839
3840 /* If first glyph of S has a left box line, start drawing it to the
3841 right of that line. */
3842 if (s->face->box != FACE_NO_BOX
3843 && s->first_glyph->left_box_line_p)
3844 x = s->x + s->face->box_line_width;
3845 else
3846 x = s->x;
3847
3848 /* If there is a margin around the image, adjust x- and y-position
3849 by that margin. */
22d650b8
GM
3850 x += s->img->hmargin;
3851 y += s->img->vmargin;
06a2c219
GM
3852
3853 if (s->img->pixmap)
3854 {
3855 if (s->img->mask)
3856 {
3857 /* We can't set both a clip mask and use XSetClipRectangles
3858 because the latter also sets a clip mask. We also can't
3859 trust on the shape extension to be available
3860 (XShapeCombineRegion). So, compute the rectangle to draw
3861 manually. */
3862 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3863 | GCFunction);
3864 XGCValues xgcv;
3865 XRectangle clip_rect, image_rect, r;
3866
3867 xgcv.clip_mask = s->img->mask;
3868 xgcv.clip_x_origin = x;
3869 xgcv.clip_y_origin = y;
3870 xgcv.function = GXcopy;
3871 XChangeGC (s->display, s->gc, mask, &xgcv);
3872
3873 x_get_glyph_string_clip_rect (s, &clip_rect);
3874 image_rect.x = x;
3875 image_rect.y = y;
3876 image_rect.width = s->img->width;
3877 image_rect.height = s->img->height;
3878 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3879 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3880 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3881 }
3882 else
3883 {
49ad1d99
GM
3884 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3885 XGCValues xgcv;
3886 XRectangle clip_rect, image_rect, r;
3887
3888 x_get_glyph_string_clip_rect (s, &clip_rect);
3889 image_rect.x = x;
3890 image_rect.y = y;
3891 image_rect.width = s->img->width;
3892 image_rect.height = s->img->height;
3893 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3894 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3895 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3896
3897 /* When the image has a mask, we can expect that at
3898 least part of a mouse highlight or a block cursor will
3899 be visible. If the image doesn't have a mask, make
3900 a block cursor visible by drawing a rectangle around
3901 the image. I believe it's looking better if we do
3902 nothing here for mouse-face. */
3903 if (s->hl == DRAW_CURSOR)
3904 XDrawRectangle (s->display, s->window, s->gc, x, y,
3905 s->img->width - 1, s->img->height - 1);
3906 }
3907 }
3908 else
3909 /* Draw a rectangle if image could not be loaded. */
3910 XDrawRectangle (s->display, s->window, s->gc, x, y,
3911 s->img->width - 1, s->img->height - 1);
3912}
3913
3914
3915/* Draw a relief around the image glyph string S. */
3916
3917static void
3918x_draw_image_relief (s)
3919 struct glyph_string *s;
3920{
3921 int x0, y0, x1, y1, thick, raised_p;
3922 XRectangle r;
3923 int x;
95af8492 3924 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3925
3926 /* If first glyph of S has a left box line, start drawing it to the
3927 right of that line. */
3928 if (s->face->box != FACE_NO_BOX
3929 && s->first_glyph->left_box_line_p)
3930 x = s->x + s->face->box_line_width;
3931 else
3932 x = s->x;
3933
3934 /* If there is a margin around the image, adjust x- and y-position
3935 by that margin. */
22d650b8
GM
3936 x += s->img->hmargin;
3937 y += s->img->vmargin;
06a2c219
GM
3938
3939 if (s->hl == DRAW_IMAGE_SUNKEN
3940 || s->hl == DRAW_IMAGE_RAISED)
3941 {
9ea173e8 3942 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3943 raised_p = s->hl == DRAW_IMAGE_RAISED;
3944 }
3945 else
3946 {
3947 thick = abs (s->img->relief);
3948 raised_p = s->img->relief > 0;
3949 }
3950
3951 x0 = x - thick;
3952 y0 = y - thick;
3953 x1 = x + s->img->width + thick - 1;
3954 y1 = y + s->img->height + thick - 1;
3955
3956 x_setup_relief_colors (s);
3957 x_get_glyph_string_clip_rect (s, &r);
3958 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3959}
3960
3961
3962/* Draw the foreground of image glyph string S to PIXMAP. */
3963
3964static void
3965x_draw_image_foreground_1 (s, pixmap)
3966 struct glyph_string *s;
3967 Pixmap pixmap;
3968{
3969 int x;
95af8492 3970 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3971
3972 /* If first glyph of S has a left box line, start drawing it to the
3973 right of that line. */
3974 if (s->face->box != FACE_NO_BOX
3975 && s->first_glyph->left_box_line_p)
3976 x = s->face->box_line_width;
3977 else
3978 x = 0;
3979
3980 /* If there is a margin around the image, adjust x- and y-position
3981 by that margin. */
22d650b8
GM
3982 x += s->img->hmargin;
3983 y += s->img->vmargin;
dc43ef94 3984
06a2c219
GM
3985 if (s->img->pixmap)
3986 {
3987 if (s->img->mask)
3988 {
3989 /* We can't set both a clip mask and use XSetClipRectangles
3990 because the latter also sets a clip mask. We also can't
3991 trust on the shape extension to be available
3992 (XShapeCombineRegion). So, compute the rectangle to draw
3993 manually. */
3994 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3995 | GCFunction);
3996 XGCValues xgcv;
3997
3998 xgcv.clip_mask = s->img->mask;
3999 xgcv.clip_x_origin = x;
4000 xgcv.clip_y_origin = y;
4001 xgcv.function = GXcopy;
4002 XChangeGC (s->display, s->gc, mask, &xgcv);
4003
4004 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4005 0, 0, s->img->width, s->img->height, x, y);
4006 XSetClipMask (s->display, s->gc, None);
4007 }
4008 else
4009 {
4010 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4011 0, 0, s->img->width, s->img->height, x, y);
4012
4013 /* When the image has a mask, we can expect that at
4014 least part of a mouse highlight or a block cursor will
4015 be visible. If the image doesn't have a mask, make
4016 a block cursor visible by drawing a rectangle around
4017 the image. I believe it's looking better if we do
4018 nothing here for mouse-face. */
4019 if (s->hl == DRAW_CURSOR)
4020 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4021 s->img->width - 1, s->img->height - 1);
4022 }
4023 }
4024 else
4025 /* Draw a rectangle if image could not be loaded. */
4026 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4027 s->img->width - 1, s->img->height - 1);
4028}
dc43ef94 4029
990ba854 4030
06a2c219
GM
4031/* Draw part of the background of glyph string S. X, Y, W, and H
4032 give the rectangle to draw. */
a9a5b0a5 4033
06a2c219
GM
4034static void
4035x_draw_glyph_string_bg_rect (s, x, y, w, h)
4036 struct glyph_string *s;
4037 int x, y, w, h;
4038{
4039 if (s->stippled_p)
4040 {
4041 /* Fill background with a stipple pattern. */
4042 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4043 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4044 XSetFillStyle (s->display, s->gc, FillSolid);
4045 }
4046 else
4047 x_clear_glyph_string_rect (s, x, y, w, h);
4048}
07e34cb0 4049
b5210ea7 4050
06a2c219 4051/* Draw image glyph string S.
dc43ef94 4052
06a2c219
GM
4053 s->y
4054 s->x +-------------------------
4055 | s->face->box
4056 |
4057 | +-------------------------
4058 | | s->img->margin
4059 | |
4060 | | +-------------------
4061 | | | the image
dc43ef94 4062
06a2c219 4063 */
dc43ef94 4064
06a2c219
GM
4065static void
4066x_draw_image_glyph_string (s)
4067 struct glyph_string *s;
4068{
4069 int x, y;
4070 int box_line_width = s->face->box_line_width;
06a2c219
GM
4071 int height;
4072 Pixmap pixmap = None;
4073
4074 height = s->height - 2 * box_line_width;
4075
4076 /* Fill background with face under the image. Do it only if row is
4077 taller than image or if image has a clip mask to reduce
4078 flickering. */
4079 s->stippled_p = s->face->stipple != 0;
4080 if (height > s->img->height
22d650b8
GM
4081 || s->img->hmargin
4082 || s->img->vmargin
06a2c219
GM
4083 || s->img->mask
4084 || s->img->pixmap == 0
4085 || s->width != s->background_width)
4086 {
4087 if (box_line_width && s->first_glyph->left_box_line_p)
4088 x = s->x + box_line_width;
4089 else
4090 x = s->x;
4091
4092 y = s->y + box_line_width;
4093
4094 if (s->img->mask)
4095 {
f9b5db02
GM
4096 /* Create a pixmap as large as the glyph string. Fill it
4097 with the background color. Copy the image to it, using
4098 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4099 Screen *screen = FRAME_X_SCREEN (s->f);
4100 int depth = DefaultDepthOfScreen (screen);
4101
4102 /* Create a pixmap as large as the glyph string. */
4103 pixmap = XCreatePixmap (s->display, s->window,
4104 s->background_width,
4105 s->height, depth);
4106
4107 /* Don't clip in the following because we're working on the
4108 pixmap. */
4109 XSetClipMask (s->display, s->gc, None);
4110
4111 /* Fill the pixmap with the background color/stipple. */
4112 if (s->stippled_p)
4113 {
4114 /* Fill background with a stipple pattern. */
4115 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4116 XFillRectangle (s->display, pixmap, s->gc,
4117 0, 0, s->background_width, s->height);
4118 XSetFillStyle (s->display, s->gc, FillSolid);
4119 }
4120 else
4121 {
4122 XGCValues xgcv;
4123 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4124 &xgcv);
4125 XSetForeground (s->display, s->gc, xgcv.background);
4126 XFillRectangle (s->display, pixmap, s->gc,
4127 0, 0, s->background_width, s->height);
4128 XSetForeground (s->display, s->gc, xgcv.foreground);
4129 }
4130 }
4131 else
06a2c219
GM
4132 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4133
4134 s->background_filled_p = 1;
4135 }
dc43ef94 4136
06a2c219
GM
4137 /* Draw the foreground. */
4138 if (pixmap != None)
4139 {
4140 x_draw_image_foreground_1 (s, pixmap);
4141 x_set_glyph_string_clipping (s);
4142 XCopyArea (s->display, pixmap, s->window, s->gc,
4143 0, 0, s->background_width, s->height, s->x, s->y);
4144 XFreePixmap (s->display, pixmap);
4145 }
4146 else
4147 x_draw_image_foreground (s);
b5210ea7 4148
06a2c219
GM
4149 /* If we must draw a relief around the image, do it. */
4150 if (s->img->relief
4151 || s->hl == DRAW_IMAGE_RAISED
4152 || s->hl == DRAW_IMAGE_SUNKEN)
4153 x_draw_image_relief (s);
4154}
8c1a6a84 4155
990ba854 4156
06a2c219 4157/* Draw stretch glyph string S. */
dc43ef94 4158
06a2c219
GM
4159static void
4160x_draw_stretch_glyph_string (s)
4161 struct glyph_string *s;
4162{
4163 xassert (s->first_glyph->type == STRETCH_GLYPH);
4164 s->stippled_p = s->face->stipple != 0;
990ba854 4165
06a2c219
GM
4166 if (s->hl == DRAW_CURSOR
4167 && !x_stretch_cursor_p)
4168 {
4169 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4170 as wide as the stretch glyph. */
4171 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4172
06a2c219
GM
4173 /* Draw cursor. */
4174 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4175
06a2c219
GM
4176 /* Clear rest using the GC of the original non-cursor face. */
4177 if (width < s->background_width)
4178 {
4179 GC gc = s->face->gc;
4180 int x = s->x + width, y = s->y;
4181 int w = s->background_width - width, h = s->height;
4182 XRectangle r;
dc43ef94 4183
06a2c219
GM
4184 x_get_glyph_string_clip_rect (s, &r);
4185 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4186
06a2c219
GM
4187 if (s->face->stipple)
4188 {
4189 /* Fill background with a stipple pattern. */
4190 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4191 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4192 XSetFillStyle (s->display, gc, FillSolid);
4193 }
4194 else
4195 {
4196 XGCValues xgcv;
4197 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4198 XSetForeground (s->display, gc, xgcv.background);
4199 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4200 XSetForeground (s->display, gc, xgcv.foreground);
4201 }
4202 }
4203 }
4204 else
4205 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4206 s->height);
4207
4208 s->background_filled_p = 1;
4209}
4210
4211
4212/* Draw glyph string S. */
4213
4214static void
4215x_draw_glyph_string (s)
4216 struct glyph_string *s;
4217{
4218 /* If S draws into the background of its successor, draw the
4219 background of the successor first so that S can draw into it.
4220 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4221 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4222 {
4223 xassert (s->next->img == NULL);
4224 x_set_glyph_string_gc (s->next);
4225 x_set_glyph_string_clipping (s->next);
4226 x_draw_glyph_string_background (s->next, 1);
4227 }
97210f4e 4228
06a2c219
GM
4229 /* Set up S->gc, set clipping and draw S. */
4230 x_set_glyph_string_gc (s);
4231 x_set_glyph_string_clipping (s);
4232
4233 switch (s->first_glyph->type)
4234 {
4235 case IMAGE_GLYPH:
4236 x_draw_image_glyph_string (s);
4237 break;
4238
4239 case STRETCH_GLYPH:
4240 x_draw_stretch_glyph_string (s);
4241 break;
4242
4243 case CHAR_GLYPH:
66ac4b0e
GM
4244 if (s->for_overlaps_p)
4245 s->background_filled_p = 1;
4246 else
4247 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4248 x_draw_glyph_string_foreground (s);
4249 break;
4250
b4192550
KH
4251 case COMPOSITE_GLYPH:
4252 if (s->for_overlaps_p || s->gidx > 0)
4253 s->background_filled_p = 1;
4254 else
4255 x_draw_glyph_string_background (s, 1);
4256 x_draw_composite_glyph_string_foreground (s);
4257 break;
4258
06a2c219
GM
4259 default:
4260 abort ();
4261 }
4262
66ac4b0e 4263 if (!s->for_overlaps_p)
06a2c219 4264 {
66ac4b0e
GM
4265 /* Draw underline. */
4266 if (s->face->underline_p)
4267 {
e24e84cc
GM
4268 unsigned long tem, h;
4269 int y;
06a2c219 4270
e24e84cc 4271 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4272 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4273 h = 1;
e24e84cc
GM
4274
4275 /* Get the underline position. This is the recommended
4276 vertical offset in pixels from the baseline to the top of
4277 the underline. This is a signed value according to the
4278 specs, and its default is
4279
4280 ROUND ((maximum descent) / 2), with
4281 ROUND(x) = floor (x + 0.5) */
4282
4283 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4284 y = s->ybase + (long) tem;
4285 else if (s->face->font)
4286 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4287 else
4288 y = s->height - h;
06a2c219 4289
66ac4b0e 4290 if (s->face->underline_defaulted_p)
e24e84cc
GM
4291 XFillRectangle (s->display, s->window, s->gc,
4292 s->x, y, s->width, h);
66ac4b0e
GM
4293 else
4294 {
4295 XGCValues xgcv;
4296 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4297 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4298 XFillRectangle (s->display, s->window, s->gc,
4299 s->x, y, s->width, h);
66ac4b0e
GM
4300 XSetForeground (s->display, s->gc, xgcv.foreground);
4301 }
dc6f92b8 4302 }
07e34cb0 4303
66ac4b0e
GM
4304 /* Draw overline. */
4305 if (s->face->overline_p)
06a2c219 4306 {
66ac4b0e
GM
4307 unsigned long dy = 0, h = 1;
4308
4309 if (s->face->overline_color_defaulted_p)
4310 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4311 s->width, h);
4312 else
4313 {
4314 XGCValues xgcv;
4315 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4316 XSetForeground (s->display, s->gc, s->face->overline_color);
4317 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4318 s->width, h);
4319 XSetForeground (s->display, s->gc, xgcv.foreground);
4320 }
06a2c219 4321 }
06a2c219 4322
66ac4b0e
GM
4323 /* Draw strike-through. */
4324 if (s->face->strike_through_p)
06a2c219 4325 {
66ac4b0e
GM
4326 unsigned long h = 1;
4327 unsigned long dy = (s->height - h) / 2;
4328
4329 if (s->face->strike_through_color_defaulted_p)
4330 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4331 s->width, h);
4332 else
4333 {
4334 XGCValues xgcv;
4335 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4336 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4337 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4338 s->width, h);
4339 XSetForeground (s->display, s->gc, xgcv.foreground);
4340 }
06a2c219 4341 }
06a2c219 4342
66ac4b0e
GM
4343 /* Draw relief. */
4344 if (s->face->box != FACE_NO_BOX)
4345 x_draw_glyph_string_box (s);
4346 }
06a2c219
GM
4347
4348 /* Reset clipping. */
4349 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4350}
07e34cb0 4351
06a2c219 4352
b4192550
KH
4353static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4354 struct face **, int));
06a2c219 4355
06a2c219 4356
209f68d9
GM
4357/* Fill glyph string S with composition components specified by S->cmp.
4358
b4192550
KH
4359 FACES is an array of faces for all components of this composition.
4360 S->gidx is the index of the first component for S.
4361 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4362 use its physical height for clipping.
06a2c219 4363
b4192550 4364 Value is the index of a component not in S. */
07e34cb0 4365
b4192550
KH
4366static int
4367x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4368 struct glyph_string *s;
b4192550 4369 struct face **faces;
66ac4b0e 4370 int overlaps_p;
07e34cb0 4371{
b4192550 4372 int i;
06a2c219 4373
b4192550 4374 xassert (s);
06a2c219 4375
b4192550 4376 s->for_overlaps_p = overlaps_p;
06a2c219 4377
b4192550
KH
4378 s->face = faces[s->gidx];
4379 s->font = s->face->font;
4380 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4381
b4192550
KH
4382 /* For all glyphs of this composition, starting at the offset
4383 S->gidx, until we reach the end of the definition or encounter a
4384 glyph that requires the different face, add it to S. */
4385 ++s->nchars;
4386 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4387 ++s->nchars;
06a2c219 4388
b4192550
KH
4389 /* All glyph strings for the same composition has the same width,
4390 i.e. the width set for the first component of the composition. */
06a2c219 4391
06a2c219
GM
4392 s->width = s->first_glyph->pixel_width;
4393
4394 /* If the specified font could not be loaded, use the frame's
4395 default font, but record the fact that we couldn't load it in
4396 the glyph string so that we can draw rectangles for the
4397 characters of the glyph string. */
4398 if (s->font == NULL)
4399 {
4400 s->font_not_found_p = 1;
4401 s->font = FRAME_FONT (s->f);
4402 }
4403
4404 /* Adjust base line for subscript/superscript text. */
4405 s->ybase += s->first_glyph->voffset;
4406
4407 xassert (s->face && s->face->gc);
4408
4409 /* This glyph string must always be drawn with 16-bit functions. */
4410 s->two_byte_p = 1;
b4192550
KH
4411
4412 return s->gidx + s->nchars;
06a2c219
GM
4413}
4414
4415
209f68d9
GM
4416/* Fill glyph string S from a sequence of character glyphs.
4417
06a2c219 4418 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4419 first glyph to consider, END is the index of the last + 1.
4420 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4421 use its physical height for clipping.
66ac4b0e
GM
4422
4423 Value is the index of the first glyph not in S. */
06a2c219
GM
4424
4425static int
66ac4b0e 4426x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4427 struct glyph_string *s;
4428 int face_id;
66ac4b0e 4429 int start, end, overlaps_p;
06a2c219
GM
4430{
4431 struct glyph *glyph, *last;
4432 int voffset;
ee569018 4433 int glyph_not_available_p;
06a2c219 4434
06a2c219
GM
4435 xassert (s->f == XFRAME (s->w->frame));
4436 xassert (s->nchars == 0);
4437 xassert (start >= 0 && end > start);
4438
66ac4b0e 4439 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4440 glyph = s->row->glyphs[s->area] + start;
4441 last = s->row->glyphs[s->area] + end;
4442 voffset = glyph->voffset;
4443
ee569018
KH
4444 glyph_not_available_p = glyph->glyph_not_available_p;
4445
06a2c219
GM
4446 while (glyph < last
4447 && glyph->type == CHAR_GLYPH
4448 && glyph->voffset == voffset
ee569018
KH
4449 /* Same face id implies same font, nowadays. */
4450 && glyph->face_id == face_id
4451 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4452 {
ee569018
KH
4453 int two_byte_p;
4454
06a2c219 4455 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4456 s->char2b + s->nchars,
4457 &two_byte_p);
4458 s->two_byte_p = two_byte_p;
06a2c219
GM
4459 ++s->nchars;
4460 xassert (s->nchars <= end - start);
4461 s->width += glyph->pixel_width;
4462 ++glyph;
4463 }
4464
4465 s->font = s->face->font;
4466 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4467
4468 /* If the specified font could not be loaded, use the frame's font,
4469 but record the fact that we couldn't load it in
4470 S->font_not_found_p so that we can draw rectangles for the
4471 characters of the glyph string. */
ee569018 4472 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4473 {
4474 s->font_not_found_p = 1;
4475 s->font = FRAME_FONT (s->f);
4476 }
4477
4478 /* Adjust base line for subscript/superscript text. */
4479 s->ybase += voffset;
66ac4b0e 4480
06a2c219
GM
4481 xassert (s->face && s->face->gc);
4482 return glyph - s->row->glyphs[s->area];
07e34cb0 4483}
dc6f92b8 4484
06a2c219
GM
4485
4486/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4487
dfcf069d 4488static void
06a2c219
GM
4489x_fill_image_glyph_string (s)
4490 struct glyph_string *s;
4491{
4492 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4493 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4494 xassert (s->img);
43d120d8 4495 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4496 s->font = s->face->font;
4497 s->width = s->first_glyph->pixel_width;
4498
4499 /* Adjust base line for subscript/superscript text. */
4500 s->ybase += s->first_glyph->voffset;
4501}
4502
4503
209f68d9 4504/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4505
209f68d9
GM
4506 ROW is the glyph row in which the glyphs are found, AREA is the
4507 area within the row. START is the index of the first glyph to
4508 consider, END is the index of the last + 1.
4509
4510 Value is the index of the first glyph not in S. */
4511
4512static int
4513x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4514 struct glyph_string *s;
209f68d9
GM
4515 struct glyph_row *row;
4516 enum glyph_row_area area;
4517 int start, end;
06a2c219 4518{
209f68d9
GM
4519 struct glyph *glyph, *last;
4520 int voffset, face_id;
4521
06a2c219 4522 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4523
4524 glyph = s->row->glyphs[s->area] + start;
4525 last = s->row->glyphs[s->area] + end;
4526 face_id = glyph->face_id;
4527 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4528 s->font = s->face->font;
209f68d9
GM
4529 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4530 s->width = glyph->pixel_width;
4531 voffset = glyph->voffset;
4532
4533 for (++glyph;
4534 (glyph < last
4535 && glyph->type == STRETCH_GLYPH
4536 && glyph->voffset == voffset
4537 && glyph->face_id == face_id);
4538 ++glyph)
4539 s->width += glyph->pixel_width;
06a2c219
GM
4540
4541 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4542 s->ybase += voffset;
4543
4544 xassert (s->face && s->face->gc);
4545 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4546}
4547
4548
4549/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4550 of XChar2b structures for S; it can't be allocated in
4551 x_init_glyph_string because it must be allocated via `alloca'. W
4552 is the window on which S is drawn. ROW and AREA are the glyph row
4553 and area within the row from which S is constructed. START is the
4554 index of the first glyph structure covered by S. HL is a
4555 face-override for drawing S. */
4556
4557static void
4558x_init_glyph_string (s, char2b, w, row, area, start, hl)
4559 struct glyph_string *s;
4560 XChar2b *char2b;
4561 struct window *w;
4562 struct glyph_row *row;
4563 enum glyph_row_area area;
4564 int start;
4565 enum draw_glyphs_face hl;
4566{
4567 bzero (s, sizeof *s);
4568 s->w = w;
4569 s->f = XFRAME (w->frame);
4570 s->display = FRAME_X_DISPLAY (s->f);
4571 s->window = FRAME_X_WINDOW (s->f);
4572 s->char2b = char2b;
4573 s->hl = hl;
4574 s->row = row;
4575 s->area = area;
4576 s->first_glyph = row->glyphs[area] + start;
4577 s->height = row->height;
4578 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4579
9ea173e8
GM
4580 /* Display the internal border below the tool-bar window. */
4581 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4582 s->y -= s->f->output_data.x->internal_border_width;
4583
4584 s->ybase = s->y + row->ascent;
4585}
4586
4587
4588/* Set background width of glyph string S. START is the index of the
4589 first glyph following S. LAST_X is the right-most x-position + 1
4590 in the drawing area. */
4591
4592static INLINE void
4593x_set_glyph_string_background_width (s, start, last_x)
4594 struct glyph_string *s;
4595 int start;
4596 int last_x;
4597{
4598 /* If the face of this glyph string has to be drawn to the end of
4599 the drawing area, set S->extends_to_end_of_line_p. */
4600 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4601
4602 if (start == s->row->used[s->area]
4603 && s->hl == DRAW_NORMAL_TEXT
4604 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4605 || s->face->background != default_face->background
4606 || s->face->stipple != default_face->stipple))
4607 s->extends_to_end_of_line_p = 1;
4608
4609 /* If S extends its face to the end of the line, set its
4610 background_width to the distance to the right edge of the drawing
4611 area. */
4612 if (s->extends_to_end_of_line_p)
1da3fd71 4613 s->background_width = last_x - s->x + 1;
06a2c219
GM
4614 else
4615 s->background_width = s->width;
4616}
4617
4618
4619/* Add a glyph string for a stretch glyph to the list of strings
4620 between HEAD and TAIL. START is the index of the stretch glyph in
4621 row area AREA of glyph row ROW. END is the index of the last glyph
4622 in that glyph row area. X is the current output position assigned
4623 to the new glyph string constructed. HL overrides that face of the
4624 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4625 is the right-most x-position of the drawing area. */
4626
8abee2e1
DL
4627/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4628 and below -- keep them on one line. */
4629#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4630 do \
4631 { \
4632 s = (struct glyph_string *) alloca (sizeof *s); \
4633 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4634 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4635 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4636 s->x = (X); \
4637 } \
4638 while (0)
4639
4640
4641/* Add a glyph string for an image glyph to the list of strings
4642 between HEAD and TAIL. START is the index of the image glyph in
4643 row area AREA of glyph row ROW. END is the index of the last glyph
4644 in that glyph row area. X is the current output position assigned
4645 to the new glyph string constructed. HL overrides that face of the
4646 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4647 is the right-most x-position of the drawing area. */
4648
8abee2e1 4649#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4650 do \
4651 { \
4652 s = (struct glyph_string *) alloca (sizeof *s); \
4653 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4654 x_fill_image_glyph_string (s); \
4655 x_append_glyph_string (&HEAD, &TAIL, s); \
4656 ++START; \
4657 s->x = (X); \
4658 } \
4659 while (0)
4660
4661
4662/* Add a glyph string for a sequence of character glyphs to the list
4663 of strings between HEAD and TAIL. START is the index of the first
4664 glyph in row area AREA of glyph row ROW that is part of the new
4665 glyph string. END is the index of the last glyph in that glyph row
4666 area. X is the current output position assigned to the new glyph
4667 string constructed. HL overrides that face of the glyph; e.g. it
4668 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4669 right-most x-position of the drawing area. */
4670
8abee2e1 4671#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4672 do \
4673 { \
3e71d8f2 4674 int c, face_id; \
06a2c219
GM
4675 XChar2b *char2b; \
4676 \
43d120d8 4677 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4678 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4679 \
b4192550
KH
4680 s = (struct glyph_string *) alloca (sizeof *s); \
4681 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4682 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4683 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4684 s->x = (X); \
4685 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4686 OVERLAPS_P); \
06a2c219
GM
4687 } \
4688 while (0)
4689
4690
b4192550
KH
4691/* Add a glyph string for a composite sequence to the list of strings
4692 between HEAD and TAIL. START is the index of the first glyph in
4693 row area AREA of glyph row ROW that is part of the new glyph
4694 string. END is the index of the last glyph in that glyph row area.
4695 X is the current output position assigned to the new glyph string
4696 constructed. HL overrides that face of the glyph; e.g. it is
4697 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4698 x-position of the drawing area. */
4699
6c27ec25 4700#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4701 do { \
43d120d8
KH
4702 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4703 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4704 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4705 struct composition *cmp = composition_table[cmp_id]; \
4706 int glyph_len = cmp->glyph_len; \
4707 XChar2b *char2b; \
4708 struct face **faces; \
4709 struct glyph_string *first_s = NULL; \
4710 int n; \
4711 \
ee569018 4712 base_face = base_face->ascii_face; \
b4192550
KH
4713 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4714 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4715 /* At first, fill in `char2b' and `faces'. */ \
4716 for (n = 0; n < glyph_len; n++) \
4717 { \
43d120d8 4718 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4719 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4720 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4721 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4722 this_face_id, char2b + n, 1); \
b4192550
KH
4723 } \
4724 \
4725 /* Make glyph_strings for each glyph sequence that is drawable by \
4726 the same face, and append them to HEAD/TAIL. */ \
4727 for (n = 0; n < cmp->glyph_len;) \
4728 { \
4729 s = (struct glyph_string *) alloca (sizeof *s); \
4730 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4731 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4732 s->cmp = cmp; \
4733 s->gidx = n; \
b4192550
KH
4734 s->x = (X); \
4735 \
4736 if (n == 0) \
4737 first_s = s; \
4738 \
4739 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4740 } \
4741 \
4742 ++START; \
4743 s = first_s; \
4744 } while (0)
4745
4746
06a2c219
GM
4747/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4748 of AREA of glyph row ROW on window W between indices START and END.
4749 HL overrides the face for drawing glyph strings, e.g. it is
4750 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4751 x-positions of the drawing area.
4752
4753 This is an ugly monster macro construct because we must use alloca
4754 to allocate glyph strings (because x_draw_glyphs can be called
4755 asynchronously). */
4756
8abee2e1 4757#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4758 do \
4759 { \
4760 HEAD = TAIL = NULL; \
4761 while (START < END) \
4762 { \
4763 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4764 switch (first_glyph->type) \
4765 { \
4766 case CHAR_GLYPH: \
4767 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4768 TAIL, HL, X, LAST_X, \
4769 OVERLAPS_P); \
06a2c219
GM
4770 break; \
4771 \
b4192550
KH
4772 case COMPOSITE_GLYPH: \
4773 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4774 HEAD, TAIL, HL, X, LAST_X,\
4775 OVERLAPS_P); \
4776 break; \
4777 \
06a2c219
GM
4778 case STRETCH_GLYPH: \
4779 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4780 HEAD, TAIL, HL, X, LAST_X); \
4781 break; \
4782 \
4783 case IMAGE_GLYPH: \
4784 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4785 TAIL, HL, X, LAST_X); \
4786 break; \
4787 \
4788 default: \
4789 abort (); \
4790 } \
4791 \
4792 x_set_glyph_string_background_width (s, START, LAST_X); \
4793 (X) += s->width; \
4794 } \
4795 } \
4796 while (0)
4797
4798
4799/* Draw glyphs between START and END in AREA of ROW on window W,
4800 starting at x-position X. X is relative to AREA in W. HL is a
4801 face-override with the following meaning:
4802
4803 DRAW_NORMAL_TEXT draw normally
4804 DRAW_CURSOR draw in cursor face
4805 DRAW_MOUSE_FACE draw in mouse face.
4806 DRAW_INVERSE_VIDEO draw in mode line face
4807 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4808 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4809
4810 If REAL_START is non-null, return in *REAL_START the real starting
4811 position for display. This can be different from START in case
4812 overlapping glyphs must be displayed. If REAL_END is non-null,
4813 return in *REAL_END the real end position for display. This can be
4814 different from END in case overlapping glyphs must be displayed.
4815
66ac4b0e
GM
4816 If OVERLAPS_P is non-zero, draw only the foreground of characters
4817 and clip to the physical height of ROW.
4818
06a2c219
GM
4819 Value is the x-position reached, relative to AREA of W. */
4820
4821static int
66ac4b0e
GM
4822x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4823 overlaps_p)
06a2c219
GM
4824 struct window *w;
4825 int x;
4826 struct glyph_row *row;
4827 enum glyph_row_area area;
4828 int start, end;
4829 enum draw_glyphs_face hl;
4830 int *real_start, *real_end;
66ac4b0e 4831 int overlaps_p;
dc6f92b8 4832{
06a2c219
GM
4833 struct glyph_string *head, *tail;
4834 struct glyph_string *s;
4835 int last_x, area_width;
4836 int x_reached;
4837 int i, j;
4838
4839 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 4840 end = min (end, row->used[area]);
a8710abf
GM
4841 start = max (0, start);
4842 start = min (end, start);
06a2c219
GM
4843 if (real_start)
4844 *real_start = start;
4845 if (real_end)
4846 *real_end = end;
4847
4848 /* Translate X to frame coordinates. Set last_x to the right
4849 end of the drawing area. */
4850 if (row->full_width_p)
4851 {
4852 /* X is relative to the left edge of W, without scroll bars
4853 or flag areas. */
4854 struct frame *f = XFRAME (w->frame);
110859fc 4855 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4856 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4857
06a2c219
GM
4858 x += window_left_x;
4859 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4860 last_x = window_left_x + area_width;
4861
4862 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4863 {
110859fc 4864 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4865 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4866 last_x += width;
4867 else
4868 x -= width;
4869 }
dc6f92b8 4870
b9432a85
GM
4871 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4872 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4873 }
4874 else
dc6f92b8 4875 {
06a2c219
GM
4876 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4877 area_width = window_box_width (w, area);
4878 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4879 }
4880
06a2c219
GM
4881 /* Build a doubly-linked list of glyph_string structures between
4882 head and tail from what we have to draw. Note that the macro
4883 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4884 the reason we use a separate variable `i'. */
4885 i = start;
66ac4b0e
GM
4886 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4887 overlaps_p);
06a2c219
GM
4888 if (tail)
4889 x_reached = tail->x + tail->background_width;
4890 else
4891 x_reached = x;
90e65f07 4892
06a2c219
GM
4893 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4894 the row, redraw some glyphs in front or following the glyph
4895 strings built above. */
a8710abf 4896 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4897 {
4898 int dummy_x = 0;
4899 struct glyph_string *h, *t;
4900
4901 /* Compute overhangs for all glyph strings. */
4902 for (s = head; s; s = s->next)
4903 x_compute_glyph_string_overhangs (s);
4904
4905 /* Prepend glyph strings for glyphs in front of the first glyph
4906 string that are overwritten because of the first glyph
4907 string's left overhang. The background of all strings
4908 prepended must be drawn because the first glyph string
4909 draws over it. */
4910 i = x_left_overwritten (head);
4911 if (i >= 0)
4912 {
4913 j = i;
4914 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4915 DRAW_NORMAL_TEXT, dummy_x, last_x,
4916 overlaps_p);
06a2c219
GM
4917 start = i;
4918 if (real_start)
4919 *real_start = start;
4920 x_compute_overhangs_and_x (t, head->x, 1);
4921 x_prepend_glyph_string_lists (&head, &tail, h, t);
4922 }
58769bee 4923
06a2c219
GM
4924 /* Prepend glyph strings for glyphs in front of the first glyph
4925 string that overwrite that glyph string because of their
4926 right overhang. For these strings, only the foreground must
4927 be drawn, because it draws over the glyph string at `head'.
4928 The background must not be drawn because this would overwrite
4929 right overhangs of preceding glyphs for which no glyph
4930 strings exist. */
4931 i = x_left_overwriting (head);
4932 if (i >= 0)
4933 {
4934 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4935 DRAW_NORMAL_TEXT, dummy_x, last_x,
4936 overlaps_p);
06a2c219
GM
4937 for (s = h; s; s = s->next)
4938 s->background_filled_p = 1;
4939 if (real_start)
4940 *real_start = i;
4941 x_compute_overhangs_and_x (t, head->x, 1);
4942 x_prepend_glyph_string_lists (&head, &tail, h, t);
4943 }
dbcb258a 4944
06a2c219
GM
4945 /* Append glyphs strings for glyphs following the last glyph
4946 string tail that are overwritten by tail. The background of
4947 these strings has to be drawn because tail's foreground draws
4948 over it. */
4949 i = x_right_overwritten (tail);
4950 if (i >= 0)
4951 {
4952 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4953 DRAW_NORMAL_TEXT, x, last_x,
4954 overlaps_p);
06a2c219
GM
4955 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4956 x_append_glyph_string_lists (&head, &tail, h, t);
4957 if (real_end)
4958 *real_end = i;
4959 }
dc6f92b8 4960
06a2c219
GM
4961 /* Append glyph strings for glyphs following the last glyph
4962 string tail that overwrite tail. The foreground of such
4963 glyphs has to be drawn because it writes into the background
4964 of tail. The background must not be drawn because it could
4965 paint over the foreground of following glyphs. */
4966 i = x_right_overwriting (tail);
4967 if (i >= 0)
4968 {
4969 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4970 DRAW_NORMAL_TEXT, x, last_x,
4971 overlaps_p);
06a2c219
GM
4972 for (s = h; s; s = s->next)
4973 s->background_filled_p = 1;
4974 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4975 x_append_glyph_string_lists (&head, &tail, h, t);
4976 if (real_end)
4977 *real_end = i;
4978 }
4979 }
58769bee 4980
06a2c219
GM
4981 /* Draw all strings. */
4982 for (s = head; s; s = s->next)
4983 x_draw_glyph_string (s);
dc6f92b8 4984
06a2c219
GM
4985 /* Value is the x-position up to which drawn, relative to AREA of W.
4986 This doesn't include parts drawn because of overhangs. */
4987 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4988 if (!row->full_width_p)
4989 {
4990 if (area > LEFT_MARGIN_AREA)
4991 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4992 if (area > TEXT_AREA)
4993 x_reached -= window_box_width (w, TEXT_AREA);
4994 }
a8710abf 4995
06a2c219
GM
4996 return x_reached;
4997}
dc6f92b8 4998
dc6f92b8 4999
66ac4b0e
GM
5000/* Fix the display of area AREA of overlapping row ROW in window W. */
5001
5002static void
5003x_fix_overlapping_area (w, row, area)
5004 struct window *w;
5005 struct glyph_row *row;
5006 enum glyph_row_area area;
5007{
5008 int i, x;
5009
5010 BLOCK_INPUT;
5011
5012 if (area == LEFT_MARGIN_AREA)
5013 x = 0;
5014 else if (area == TEXT_AREA)
5015 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5016 else
5017 x = (window_box_width (w, LEFT_MARGIN_AREA)
5018 + window_box_width (w, TEXT_AREA));
5019
5020 for (i = 0; i < row->used[area];)
5021 {
5022 if (row->glyphs[area][i].overlaps_vertically_p)
5023 {
5024 int start = i, start_x = x;
5025
5026 do
5027 {
5028 x += row->glyphs[area][i].pixel_width;
5029 ++i;
5030 }
5031 while (i < row->used[area]
5032 && row->glyphs[area][i].overlaps_vertically_p);
5033
5034 x_draw_glyphs (w, start_x, row, area, start, i,
5035 (row->inverse_p
5036 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5037 NULL, NULL, 1);
5038 }
5039 else
5040 {
5041 x += row->glyphs[area][i].pixel_width;
5042 ++i;
5043 }
5044 }
5045
5046 UNBLOCK_INPUT;
5047}
5048
5049
06a2c219
GM
5050/* Output LEN glyphs starting at START at the nominal cursor position.
5051 Advance the nominal cursor over the text. The global variable
5052 updated_window contains the window being updated, updated_row is
5053 the glyph row being updated, and updated_area is the area of that
5054 row being updated. */
dc6f92b8 5055
06a2c219
GM
5056static void
5057x_write_glyphs (start, len)
5058 struct glyph *start;
5059 int len;
5060{
5061 int x, hpos, real_start, real_end;
d9cdbb3d 5062
06a2c219 5063 xassert (updated_window && updated_row);
dc6f92b8 5064 BLOCK_INPUT;
06a2c219
GM
5065
5066 /* Write glyphs. */
dc6f92b8 5067
06a2c219
GM
5068 hpos = start - updated_row->glyphs[updated_area];
5069 x = x_draw_glyphs (updated_window, output_cursor.x,
5070 updated_row, updated_area,
5071 hpos, hpos + len,
5072 (updated_row->inverse_p
5073 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 5074 &real_start, &real_end, 0);
b30ec466 5075
06a2c219
GM
5076 /* If we drew over the cursor, note that it is not visible any more. */
5077 note_overwritten_text_cursor (updated_window, real_start,
5078 real_end - real_start);
dc6f92b8
JB
5079
5080 UNBLOCK_INPUT;
06a2c219
GM
5081
5082 /* Advance the output cursor. */
5083 output_cursor.hpos += len;
5084 output_cursor.x = x;
dc6f92b8
JB
5085}
5086
0cdd0c9f 5087
06a2c219 5088/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5089
06a2c219
GM
5090static void
5091x_insert_glyphs (start, len)
5092 struct glyph *start;
5093 register int len;
5094{
5095 struct frame *f;
5096 struct window *w;
5097 int line_height, shift_by_width, shifted_region_width;
5098 struct glyph_row *row;
5099 struct glyph *glyph;
5100 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 5101
06a2c219 5102 xassert (updated_window && updated_row);
0cdd0c9f 5103 BLOCK_INPUT;
06a2c219
GM
5104 w = updated_window;
5105 f = XFRAME (WINDOW_FRAME (w));
5106
5107 /* Get the height of the line we are in. */
5108 row = updated_row;
5109 line_height = row->height;
5110
5111 /* Get the width of the glyphs to insert. */
5112 shift_by_width = 0;
5113 for (glyph = start; glyph < start + len; ++glyph)
5114 shift_by_width += glyph->pixel_width;
5115
5116 /* Get the width of the region to shift right. */
5117 shifted_region_width = (window_box_width (w, updated_area)
5118 - output_cursor.x
5119 - shift_by_width);
5120
5121 /* Shift right. */
5122 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5123 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5124 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5125 f->output_data.x->normal_gc,
5126 frame_x, frame_y,
5127 shifted_region_width, line_height,
5128 frame_x + shift_by_width, frame_y);
5129
5130 /* Write the glyphs. */
5131 hpos = start - row->glyphs[updated_area];
5132 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 5133 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
5134 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5135
5136 /* Advance the output cursor. */
5137 output_cursor.hpos += len;
5138 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5139 UNBLOCK_INPUT;
5140}
0cdd0c9f 5141
0cdd0c9f 5142
06a2c219
GM
5143/* Delete N glyphs at the nominal cursor position. Not implemented
5144 for X frames. */
c83febd7
RS
5145
5146static void
06a2c219
GM
5147x_delete_glyphs (n)
5148 register int n;
c83febd7 5149{
06a2c219 5150 abort ();
c83febd7
RS
5151}
5152
0cdd0c9f 5153
06a2c219
GM
5154/* Erase the current text line from the nominal cursor position
5155 (inclusive) to pixel column TO_X (exclusive). The idea is that
5156 everything from TO_X onward is already erased.
5157
5158 TO_X is a pixel position relative to updated_area of
5159 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5160
06a2c219
GM
5161static void
5162x_clear_end_of_line (to_x)
5163 int to_x;
5164{
5165 struct frame *f;
5166 struct window *w = updated_window;
5167 int max_x, min_y, max_y;
5168 int from_x, from_y, to_y;
5169
5170 xassert (updated_window && updated_row);
5171 f = XFRAME (w->frame);
5172
5173 if (updated_row->full_width_p)
5174 {
5175 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5176 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5177 && !w->pseudo_window_p)
5178 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5179 }
06a2c219
GM
5180 else
5181 max_x = window_box_width (w, updated_area);
5182 max_y = window_text_bottom_y (w);
dc6f92b8 5183
06a2c219
GM
5184 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5185 of window. For TO_X > 0, truncate to end of drawing area. */
5186 if (to_x == 0)
5187 return;
5188 else if (to_x < 0)
5189 to_x = max_x;
5190 else
5191 to_x = min (to_x, max_x);
dbc4e1c1 5192
06a2c219
GM
5193 to_y = min (max_y, output_cursor.y + updated_row->height);
5194
5195 /* Notice if the cursor will be cleared by this operation. */
5196 if (!updated_row->full_width_p)
5197 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5198
06a2c219
GM
5199 from_x = output_cursor.x;
5200
5201 /* Translate to frame coordinates. */
5202 if (updated_row->full_width_p)
5203 {
5204 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5205 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5206 }
0cdd0c9f
RS
5207 else
5208 {
06a2c219
GM
5209 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5210 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5211 }
5212
045dee35 5213 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5214 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5215 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5216
5217 /* Prevent inadvertently clearing to end of the X window. */
5218 if (to_x > from_x && to_y > from_y)
5219 {
5220 BLOCK_INPUT;
5221 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5222 from_x, from_y, to_x - from_x, to_y - from_y,
5223 False);
5224 UNBLOCK_INPUT;
0cdd0c9f 5225 }
0cdd0c9f 5226}
dbc4e1c1 5227
0cdd0c9f 5228
06a2c219 5229/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5230 frame. Otherwise clear the selected frame. */
06a2c219
GM
5231
5232static void
5233x_clear_frame ()
0cdd0c9f 5234{
06a2c219 5235 struct frame *f;
0cdd0c9f 5236
06a2c219
GM
5237 if (updating_frame)
5238 f = updating_frame;
0cdd0c9f 5239 else
b86bd3dd 5240 f = SELECTED_FRAME ();
58769bee 5241
06a2c219
GM
5242 /* Clearing the frame will erase any cursor, so mark them all as no
5243 longer visible. */
5244 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5245 output_cursor.hpos = output_cursor.vpos = 0;
5246 output_cursor.x = -1;
5247
5248 /* We don't set the output cursor here because there will always
5249 follow an explicit cursor_to. */
5250 BLOCK_INPUT;
5251 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5252
5253 /* We have to clear the scroll bars, too. If we have changed
5254 colors or something like that, then they should be notified. */
5255 x_scroll_bar_clear (f);
0cdd0c9f 5256
06a2c219
GM
5257 XFlush (FRAME_X_DISPLAY (f));
5258 UNBLOCK_INPUT;
dc6f92b8 5259}
06a2c219
GM
5260
5261
dc6f92b8 5262\f
dbc4e1c1
JB
5263/* Invert the middle quarter of the frame for .15 sec. */
5264
06a2c219
GM
5265/* We use the select system call to do the waiting, so we have to make
5266 sure it's available. If it isn't, we just won't do visual bells. */
5267
dbc4e1c1
JB
5268#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5269
06a2c219
GM
5270
5271/* Subtract the `struct timeval' values X and Y, storing the result in
5272 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5273
5274static int
5275timeval_subtract (result, x, y)
5276 struct timeval *result, x, y;
5277{
06a2c219
GM
5278 /* Perform the carry for the later subtraction by updating y. This
5279 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5280 if (x.tv_usec < y.tv_usec)
5281 {
5282 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5283 y.tv_usec -= 1000000 * nsec;
5284 y.tv_sec += nsec;
5285 }
06a2c219 5286
dbc4e1c1
JB
5287 if (x.tv_usec - y.tv_usec > 1000000)
5288 {
5289 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5290 y.tv_usec += 1000000 * nsec;
5291 y.tv_sec -= nsec;
5292 }
5293
06a2c219
GM
5294 /* Compute the time remaining to wait. tv_usec is certainly
5295 positive. */
dbc4e1c1
JB
5296 result->tv_sec = x.tv_sec - y.tv_sec;
5297 result->tv_usec = x.tv_usec - y.tv_usec;
5298
06a2c219
GM
5299 /* Return indication of whether the result should be considered
5300 negative. */
dbc4e1c1
JB
5301 return x.tv_sec < y.tv_sec;
5302}
dc6f92b8 5303
dfcf069d 5304void
f676886a
JB
5305XTflash (f)
5306 struct frame *f;
dc6f92b8 5307{
dbc4e1c1 5308 BLOCK_INPUT;
dc6f92b8 5309
dbc4e1c1
JB
5310 {
5311 GC gc;
dc6f92b8 5312
06a2c219
GM
5313 /* Create a GC that will use the GXxor function to flip foreground
5314 pixels into background pixels. */
dbc4e1c1
JB
5315 {
5316 XGCValues values;
dc6f92b8 5317
dbc4e1c1 5318 values.function = GXxor;
7556890b
RS
5319 values.foreground = (f->output_data.x->foreground_pixel
5320 ^ f->output_data.x->background_pixel);
58769bee 5321
334208b7 5322 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5323 GCFunction | GCForeground, &values);
5324 }
dc6f92b8 5325
dbc4e1c1 5326 {
e84e14c3
RS
5327 /* Get the height not including a menu bar widget. */
5328 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5329 /* Height of each line to flash. */
5330 int flash_height = FRAME_LINE_HEIGHT (f);
5331 /* These will be the left and right margins of the rectangles. */
5332 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5333 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5334
5335 int width;
5336
5337 /* Don't flash the area between a scroll bar and the frame
5338 edge it is next to. */
5339 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5340 {
5341 case vertical_scroll_bar_left:
5342 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5343 break;
5344
5345 case vertical_scroll_bar_right:
5346 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5347 break;
06a2c219
GM
5348
5349 default:
5350 break;
e84e14c3
RS
5351 }
5352
5353 width = flash_right - flash_left;
5354
5355 /* If window is tall, flash top and bottom line. */
5356 if (height > 3 * FRAME_LINE_HEIGHT (f))
5357 {
5358 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5359 flash_left,
5360 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5361 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5362 width, flash_height);
5363 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5364 flash_left,
5365 (height - flash_height
5366 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5367 width, flash_height);
5368 }
5369 else
5370 /* If it is short, flash it all. */
5371 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5372 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5373 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5374
06a2c219 5375 x_flush (f);
dc6f92b8 5376
dbc4e1c1 5377 {
06a2c219 5378 struct timeval wakeup;
dc6f92b8 5379
66c30ea1 5380 EMACS_GET_TIME (wakeup);
dc6f92b8 5381
dbc4e1c1
JB
5382 /* Compute time to wait until, propagating carry from usecs. */
5383 wakeup.tv_usec += 150000;
5384 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5385 wakeup.tv_usec %= 1000000;
5386
5387 /* Keep waiting until past the time wakeup. */
5388 while (1)
5389 {
5390 struct timeval timeout;
5391
66c30ea1 5392 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5393
5394 /* In effect, timeout = wakeup - timeout.
5395 Break if result would be negative. */
5396 if (timeval_subtract (&timeout, wakeup, timeout))
5397 break;
5398
5399 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5400 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5401 }
5402 }
58769bee 5403
e84e14c3
RS
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));
5423
334208b7 5424 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5425 x_flush (f);
dc6f92b8 5426 }
dbc4e1c1
JB
5427 }
5428
5429 UNBLOCK_INPUT;
dc6f92b8
JB
5430}
5431
06a2c219 5432#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5433
5434
dc6f92b8
JB
5435/* Make audible bell. */
5436
dfcf069d 5437void
dc6f92b8
JB
5438XTring_bell ()
5439{
b86bd3dd
GM
5440 struct frame *f = SELECTED_FRAME ();
5441
5442 if (FRAME_X_DISPLAY (f))
5443 {
dbc4e1c1 5444#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5445 if (visible_bell)
5446 XTflash (f);
5447 else
dbc4e1c1 5448#endif
b86bd3dd
GM
5449 {
5450 BLOCK_INPUT;
5451 XBell (FRAME_X_DISPLAY (f), 0);
5452 XFlush (FRAME_X_DISPLAY (f));
5453 UNBLOCK_INPUT;
5454 }
dc6f92b8
JB
5455 }
5456}
06a2c219 5457
dc6f92b8 5458\f
06a2c219
GM
5459/* Specify how many text lines, from the top of the window,
5460 should be affected by insert-lines and delete-lines operations.
5461 This, and those operations, are used only within an update
5462 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5463
dfcf069d 5464static void
06a2c219
GM
5465XTset_terminal_window (n)
5466 register int n;
dc6f92b8 5467{
06a2c219 5468 /* This function intentionally left blank. */
dc6f92b8
JB
5469}
5470
06a2c219
GM
5471
5472\f
5473/***********************************************************************
5474 Line Dance
5475 ***********************************************************************/
5476
5477/* Perform an insert-lines or delete-lines operation, inserting N
5478 lines or deleting -N lines at vertical position VPOS. */
5479
dfcf069d 5480static void
06a2c219
GM
5481x_ins_del_lines (vpos, n)
5482 int vpos, n;
dc6f92b8
JB
5483{
5484 abort ();
5485}
06a2c219
GM
5486
5487
5488/* Scroll part of the display as described by RUN. */
dc6f92b8 5489
dfcf069d 5490static void
06a2c219
GM
5491x_scroll_run (w, run)
5492 struct window *w;
5493 struct run *run;
dc6f92b8 5494{
06a2c219
GM
5495 struct frame *f = XFRAME (w->frame);
5496 int x, y, width, height, from_y, to_y, bottom_y;
5497
5498 /* Get frame-relative bounding box of the text display area of W,
5499 without mode lines. Include in this box the flags areas to the
5500 left and right of W. */
5501 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5502 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5503 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5504
5505 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5506 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5507 bottom_y = y + height;
dc6f92b8 5508
06a2c219
GM
5509 if (to_y < from_y)
5510 {
5511 /* Scrolling up. Make sure we don't copy part of the mode
5512 line at the bottom. */
5513 if (from_y + run->height > bottom_y)
5514 height = bottom_y - from_y;
5515 else
5516 height = run->height;
5517 }
dc6f92b8 5518 else
06a2c219
GM
5519 {
5520 /* Scolling down. Make sure we don't copy over the mode line.
5521 at the bottom. */
5522 if (to_y + run->height > bottom_y)
5523 height = bottom_y - to_y;
5524 else
5525 height = run->height;
5526 }
7a13e894 5527
06a2c219
GM
5528 BLOCK_INPUT;
5529
5530 /* Cursor off. Will be switched on again in x_update_window_end. */
5531 updated_window = w;
5532 x_clear_cursor (w);
5533
5534 XCopyArea (FRAME_X_DISPLAY (f),
5535 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5536 f->output_data.x->normal_gc,
5537 x, from_y,
5538 width, height,
5539 x, to_y);
5540
5541 UNBLOCK_INPUT;
5542}
dc6f92b8 5543
dc6f92b8 5544
06a2c219
GM
5545\f
5546/***********************************************************************
5547 Exposure Events
5548 ***********************************************************************/
5549
5550/* Redisplay an exposed area of frame F. X and Y are the upper-left
5551 corner of the exposed rectangle. W and H are width and height of
5552 the exposed area. All are pixel values. W or H zero means redraw
5553 the entire frame. */
dc6f92b8 5554
06a2c219
GM
5555static void
5556expose_frame (f, x, y, w, h)
5557 struct frame *f;
5558 int x, y, w, h;
dc6f92b8 5559{
06a2c219 5560 XRectangle r;
dc6f92b8 5561
06a2c219 5562 TRACE ((stderr, "expose_frame "));
dc6f92b8 5563
06a2c219
GM
5564 /* No need to redraw if frame will be redrawn soon. */
5565 if (FRAME_GARBAGED_P (f))
dc6f92b8 5566 {
06a2c219
GM
5567 TRACE ((stderr, " garbaged\n"));
5568 return;
5569 }
5570
5571 /* If basic faces haven't been realized yet, there is no point in
5572 trying to redraw anything. This can happen when we get an expose
5573 event while Emacs is starting, e.g. by moving another window. */
5574 if (FRAME_FACE_CACHE (f) == NULL
5575 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5576 {
5577 TRACE ((stderr, " no faces\n"));
5578 return;
58769bee 5579 }
06a2c219
GM
5580
5581 if (w == 0 || h == 0)
58769bee 5582 {
06a2c219
GM
5583 r.x = r.y = 0;
5584 r.width = CANON_X_UNIT (f) * f->width;
5585 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5586 }
5587 else
5588 {
06a2c219
GM
5589 r.x = x;
5590 r.y = y;
5591 r.width = w;
5592 r.height = h;
5593 }
5594
5595 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5596 expose_window_tree (XWINDOW (f->root_window), &r);
5597
9ea173e8 5598 if (WINDOWP (f->tool_bar_window))
06a2c219 5599 {
9ea173e8 5600 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5601 XRectangle window_rect;
5602 XRectangle intersection_rect;
5603 int window_x, window_y, window_width, window_height;
5604
5605
5606 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5607 window_rect.x = window_x;
5608 window_rect.y = window_y;
5609 window_rect.width = window_width;
5610 window_rect.height = window_height;
5611
5612 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5613 expose_window (w, &intersection_rect);
5614 }
5615
5616#ifndef USE_X_TOOLKIT
5617 if (WINDOWP (f->menu_bar_window))
5618 {
5619 struct window *w = XWINDOW (f->menu_bar_window);
5620 XRectangle window_rect;
5621 XRectangle intersection_rect;
5622 int window_x, window_y, window_width, window_height;
5623
5624
5625 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5626 window_rect.x = window_x;
5627 window_rect.y = window_y;
5628 window_rect.width = window_width;
5629 window_rect.height = window_height;
5630
5631 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5632 expose_window (w, &intersection_rect);
dc6f92b8 5633 }
06a2c219 5634#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5635}
5636
06a2c219
GM
5637
5638/* Redraw (parts) of all windows in the window tree rooted at W that
5639 intersect R. R contains frame pixel coordinates. */
5640
58769bee 5641static void
06a2c219
GM
5642expose_window_tree (w, r)
5643 struct window *w;
5644 XRectangle *r;
dc6f92b8 5645{
06a2c219
GM
5646 while (w)
5647 {
5648 if (!NILP (w->hchild))
5649 expose_window_tree (XWINDOW (w->hchild), r);
5650 else if (!NILP (w->vchild))
5651 expose_window_tree (XWINDOW (w->vchild), r);
5652 else
5653 {
5654 XRectangle window_rect;
5655 XRectangle intersection_rect;
5656 struct frame *f = XFRAME (w->frame);
5657 int window_x, window_y, window_width, window_height;
5658
5659 /* Frame-relative pixel rectangle of W. */
5660 window_box (w, -1, &window_x, &window_y, &window_width,
5661 &window_height);
5662 window_rect.x
5663 = (window_x
110859fc 5664 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5665 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5666 window_rect.y = window_y;
5667 window_rect.width
5668 = (window_width
110859fc 5669 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5670 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5671 window_rect.height
5672 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5673
5674 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5675 expose_window (w, &intersection_rect);
5676 }
58769bee 5677
06a2c219
GM
5678 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5679 }
5680}
58769bee 5681
dc6f92b8 5682
06a2c219
GM
5683/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5684 which intersects rectangle R. R is in window-relative coordinates. */
5685
5686static void
5687expose_area (w, row, r, area)
5688 struct window *w;
5689 struct glyph_row *row;
5690 XRectangle *r;
5691 enum glyph_row_area area;
5692{
06a2c219
GM
5693 struct glyph *first = row->glyphs[area];
5694 struct glyph *end = row->glyphs[area] + row->used[area];
5695 struct glyph *last;
4bc6dcc7 5696 int first_x, start_x, x;
06a2c219 5697
6fb13182
GM
5698 if (area == TEXT_AREA && row->fill_line_p)
5699 /* If row extends face to end of line write the whole line. */
4bc6dcc7 5700 x_draw_glyphs (w, 0, row, area,
6fb13182 5701 0, row->used[area],
06a2c219 5702 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5703 NULL, NULL, 0);
6fb13182
GM
5704 else
5705 {
4bc6dcc7
GM
5706 /* Set START_X to the window-relative start position for drawing glyphs of
5707 AREA. The first glyph of the text area can be partially visible.
5708 The first glyphs of other areas cannot. */
5709 if (area == LEFT_MARGIN_AREA)
5710 start_x = 0;
5711 else if (area == TEXT_AREA)
5712 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5713 else
5714 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5715 + window_box_width (w, TEXT_AREA));
5716 x = start_x;
5717
6fb13182
GM
5718 /* Find the first glyph that must be redrawn. */
5719 while (first < end
5720 && x + first->pixel_width < r->x)
5721 {
5722 x += first->pixel_width;
5723 ++first;
5724 }
5725
5726 /* Find the last one. */
5727 last = first;
5728 first_x = x;
5729 while (last < end
5730 && x < r->x + r->width)
5731 {
5732 x += last->pixel_width;
5733 ++last;
5734 }
5735
5736 /* Repaint. */
5737 if (last > first)
4bc6dcc7 5738 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5739 first - row->glyphs[area],
5740 last - row->glyphs[area],
5741 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5742 NULL, NULL, 0);
5743 }
06a2c219
GM
5744}
5745
58769bee 5746
06a2c219
GM
5747/* Redraw the parts of the glyph row ROW on window W intersecting
5748 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5749
06a2c219
GM
5750static void
5751expose_line (w, row, r)
5752 struct window *w;
5753 struct glyph_row *row;
5754 XRectangle *r;
5755{
5756 xassert (row->enabled_p);
5757
5758 if (row->mode_line_p || w->pseudo_window_p)
5759 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5760 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5761 NULL, NULL, 0);
06a2c219
GM
5762 else
5763 {
5764 if (row->used[LEFT_MARGIN_AREA])
5765 expose_area (w, row, r, LEFT_MARGIN_AREA);
5766 if (row->used[TEXT_AREA])
5767 expose_area (w, row, r, TEXT_AREA);
5768 if (row->used[RIGHT_MARGIN_AREA])
5769 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5770 x_draw_row_bitmaps (w, row);
5771 }
5772}
dc6f92b8 5773
58769bee 5774
06a2c219
GM
5775/* Return non-zero if W's cursor intersects rectangle R. */
5776
5777static int
5778x_phys_cursor_in_rect_p (w, r)
5779 struct window *w;
5780 XRectangle *r;
5781{
5782 XRectangle cr, result;
5783 struct glyph *cursor_glyph;
5784
5785 cursor_glyph = get_phys_cursor_glyph (w);
5786 if (cursor_glyph)
5787 {
5788 cr.x = w->phys_cursor.x;
5789 cr.y = w->phys_cursor.y;
5790 cr.width = cursor_glyph->pixel_width;
5791 cr.height = w->phys_cursor_height;
5792 return x_intersect_rectangles (&cr, r, &result);
5793 }
5794 else
5795 return 0;
dc6f92b8 5796}
dc6f92b8 5797
06a2c219
GM
5798
5799/* Redraw a rectangle of window W. R is a rectangle in window
5800 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5801
5802static void
06a2c219
GM
5803expose_window (w, r)
5804 struct window *w;
5805 XRectangle *r;
dc6f92b8 5806{
06a2c219
GM
5807 struct glyph_row *row;
5808 int y;
5809 int yb = window_text_bottom_y (w);
5810 int cursor_cleared_p;
dc6f92b8 5811
80c32bcc
GM
5812 /* If window is not yet fully initialized, do nothing. This can
5813 happen when toolkit scroll bars are used and a window is split.
5814 Reconfiguring the scroll bar will generate an expose for a newly
5815 created window. */
c7911417 5816 if (w->current_matrix == NULL || w == updated_window)
80c32bcc
GM
5817 return;
5818
06a2c219
GM
5819 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5820 r->x, r->y, r->width, r->height));
dc6f92b8 5821
06a2c219
GM
5822 /* Convert to window coordinates. */
5823 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5824 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5825
06a2c219
GM
5826 /* Turn off the cursor. */
5827 if (!w->pseudo_window_p
5828 && x_phys_cursor_in_rect_p (w, r))
5829 {
5830 x_clear_cursor (w);
5831 cursor_cleared_p = 1;
5832 }
5833 else
5834 cursor_cleared_p = 0;
5835
5836 /* Find the first row intersecting the rectangle R. */
5837 row = w->current_matrix->rows;
5838 y = 0;
5839 while (row->enabled_p
5840 && y < yb
5841 && y + row->height < r->y)
5842 {
5843 y += row->height;
5844 ++row;
5845 }
5846
dc6f92b8 5847 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5848 while (row->enabled_p
5849 && y < yb
5850 && y < r->y + r->height)
5851 {
5852 expose_line (w, row, r);
5853 y += row->height;
5854 ++row;
5855 }
5856
5857 /* Display the mode line if there is one. */
5858 if (WINDOW_WANTS_MODELINE_P (w)
5859 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5860 row->enabled_p)
5861 && row->y < r->y + r->height)
5862 expose_line (w, row, r);
dc6f92b8 5863
06a2c219 5864 if (!w->pseudo_window_p)
dc6f92b8 5865 {
06a2c219
GM
5866 /* Draw border between windows. */
5867 x_draw_vertical_border (w);
5868
5869 /* Turn the cursor on again. */
5870 if (cursor_cleared_p)
5871 x_update_window_cursor (w, 1);
5872 }
5873}
dc6f92b8 5874
dc6f92b8 5875
06a2c219
GM
5876/* Determine the intersection of two rectangles R1 and R2. Return
5877 the intersection in *RESULT. Value is non-zero if RESULT is not
5878 empty. */
5879
5880static int
5881x_intersect_rectangles (r1, r2, result)
5882 XRectangle *r1, *r2, *result;
5883{
5884 XRectangle *left, *right;
5885 XRectangle *upper, *lower;
5886 int intersection_p = 0;
5887
5888 /* Rearrange so that R1 is the left-most rectangle. */
5889 if (r1->x < r2->x)
5890 left = r1, right = r2;
5891 else
5892 left = r2, right = r1;
5893
5894 /* X0 of the intersection is right.x0, if this is inside R1,
5895 otherwise there is no intersection. */
5896 if (right->x <= left->x + left->width)
5897 {
5898 result->x = right->x;
5899
5900 /* The right end of the intersection is the minimum of the
5901 the right ends of left and right. */
5902 result->width = (min (left->x + left->width, right->x + right->width)
5903 - result->x);
5904
5905 /* Same game for Y. */
5906 if (r1->y < r2->y)
5907 upper = r1, lower = r2;
5908 else
5909 upper = r2, lower = r1;
5910
5911 /* The upper end of the intersection is lower.y0, if this is inside
5912 of upper. Otherwise, there is no intersection. */
5913 if (lower->y <= upper->y + upper->height)
dc43ef94 5914 {
06a2c219
GM
5915 result->y = lower->y;
5916
5917 /* The lower end of the intersection is the minimum of the lower
5918 ends of upper and lower. */
5919 result->height = (min (lower->y + lower->height,
5920 upper->y + upper->height)
5921 - result->y);
5922 intersection_p = 1;
dc43ef94 5923 }
dc6f92b8
JB
5924 }
5925
06a2c219 5926 return intersection_p;
dc6f92b8 5927}
06a2c219
GM
5928
5929
5930
5931
dc6f92b8 5932\f
dc6f92b8 5933static void
334208b7
RS
5934frame_highlight (f)
5935 struct frame *f;
dc6f92b8 5936{
b3e1e05c
JB
5937 /* We used to only do this if Vx_no_window_manager was non-nil, but
5938 the ICCCM (section 4.1.6) says that the window's border pixmap
5939 and border pixel are window attributes which are "private to the
5940 client", so we can always change it to whatever we want. */
5941 BLOCK_INPUT;
334208b7 5942 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5943 f->output_data.x->border_pixel);
b3e1e05c 5944 UNBLOCK_INPUT;
5d46f928 5945 x_update_cursor (f, 1);
dc6f92b8
JB
5946}
5947
5948static void
334208b7
RS
5949frame_unhighlight (f)
5950 struct frame *f;
dc6f92b8 5951{
b3e1e05c
JB
5952 /* We used to only do this if Vx_no_window_manager was non-nil, but
5953 the ICCCM (section 4.1.6) says that the window's border pixmap
5954 and border pixel are window attributes which are "private to the
5955 client", so we can always change it to whatever we want. */
5956 BLOCK_INPUT;
334208b7 5957 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5958 f->output_data.x->border_tile);
b3e1e05c 5959 UNBLOCK_INPUT;
5d46f928 5960 x_update_cursor (f, 1);
dc6f92b8 5961}
dc6f92b8 5962
f676886a
JB
5963/* The focus has changed. Update the frames as necessary to reflect
5964 the new situation. Note that we can't change the selected frame
c5acd733 5965 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5966 Each event gets marked with the frame in which it occurred, so the
c5acd733 5967 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5968
6d4238f3 5969static void
0f941935
KH
5970x_new_focus_frame (dpyinfo, frame)
5971 struct x_display_info *dpyinfo;
f676886a 5972 struct frame *frame;
dc6f92b8 5973{
0f941935 5974 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5975
0f941935 5976 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5977 {
58769bee 5978 /* Set this before calling other routines, so that they see
f676886a 5979 the correct value of x_focus_frame. */
0f941935 5980 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5981
5982 if (old_focus && old_focus->auto_lower)
f676886a 5983 x_lower_frame (old_focus);
dc6f92b8
JB
5984
5985#if 0
f676886a 5986 selected_frame = frame;
e0c1aef2
KH
5987 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5988 selected_frame);
f676886a
JB
5989 Fselect_window (selected_frame->selected_window);
5990 choose_minibuf_frame ();
c118dd06 5991#endif /* ! 0 */
dc6f92b8 5992
0f941935
KH
5993 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5994 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5995 else
5996 pending_autoraise_frame = 0;
6d4238f3 5997 }
dc6f92b8 5998
0f941935 5999 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6000}
6001
37c2c98b
RS
6002/* Handle an event saying the mouse has moved out of an Emacs frame. */
6003
6004void
0f941935
KH
6005x_mouse_leave (dpyinfo)
6006 struct x_display_info *dpyinfo;
37c2c98b 6007{
0f941935 6008 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6009}
6d4238f3 6010
f451eb13
JB
6011/* The focus has changed, or we have redirected a frame's focus to
6012 another frame (this happens when a frame uses a surrogate
06a2c219 6013 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6014
6015 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6016 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6017 the appropriate X display info. */
06a2c219 6018
6d4238f3 6019static void
0f941935
KH
6020XTframe_rehighlight (frame)
6021 struct frame *frame;
6d4238f3 6022{
0f941935
KH
6023 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6024}
6d4238f3 6025
0f941935
KH
6026static void
6027x_frame_rehighlight (dpyinfo)
6028 struct x_display_info *dpyinfo;
6029{
6030 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6031
6032 if (dpyinfo->x_focus_frame)
6d4238f3 6033 {
0f941935
KH
6034 dpyinfo->x_highlight_frame
6035 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6036 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6037 : dpyinfo->x_focus_frame);
6038 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6039 {
0f941935
KH
6040 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6041 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6042 }
dc6f92b8 6043 }
6d4238f3 6044 else
0f941935 6045 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6046
0f941935 6047 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6048 {
6049 if (old_highlight)
f676886a 6050 frame_unhighlight (old_highlight);
0f941935
KH
6051 if (dpyinfo->x_highlight_frame)
6052 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6053 }
dc6f92b8 6054}
06a2c219
GM
6055
6056
dc6f92b8 6057\f
06a2c219 6058/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6059
28430d3c
JB
6060/* Initialize mode_switch_bit and modifier_meaning. */
6061static void
334208b7
RS
6062x_find_modifier_meanings (dpyinfo)
6063 struct x_display_info *dpyinfo;
28430d3c 6064{
f689eb05 6065 int min_code, max_code;
28430d3c
JB
6066 KeySym *syms;
6067 int syms_per_code;
6068 XModifierKeymap *mods;
6069
334208b7
RS
6070 dpyinfo->meta_mod_mask = 0;
6071 dpyinfo->shift_lock_mask = 0;
6072 dpyinfo->alt_mod_mask = 0;
6073 dpyinfo->super_mod_mask = 0;
6074 dpyinfo->hyper_mod_mask = 0;
58769bee 6075
9658a521 6076#ifdef HAVE_X11R4
334208b7 6077 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6078#else
4a60f8c5
RS
6079 min_code = dpyinfo->display->min_keycode;
6080 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6081#endif
6082
334208b7 6083 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6084 min_code, max_code - min_code + 1,
6085 &syms_per_code);
334208b7 6086 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6087
58769bee 6088 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6089 Alt keysyms are on. */
28430d3c 6090 {
06a2c219 6091 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6092
6093 for (row = 3; row < 8; row++)
6094 for (col = 0; col < mods->max_keypermod; col++)
6095 {
0299d313
RS
6096 KeyCode code
6097 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6098
af92970c
KH
6099 /* Zeroes are used for filler. Skip them. */
6100 if (code == 0)
6101 continue;
6102
28430d3c
JB
6103 /* Are any of this keycode's keysyms a meta key? */
6104 {
6105 int code_col;
6106
6107 for (code_col = 0; code_col < syms_per_code; code_col++)
6108 {
f689eb05 6109 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6110
f689eb05 6111 switch (sym)
28430d3c 6112 {
f689eb05
JB
6113 case XK_Meta_L:
6114 case XK_Meta_R:
334208b7 6115 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6116 break;
f689eb05
JB
6117
6118 case XK_Alt_L:
6119 case XK_Alt_R:
334208b7 6120 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6121 break;
6122
6123 case XK_Hyper_L:
6124 case XK_Hyper_R:
334208b7 6125 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6126 break;
6127
6128 case XK_Super_L:
6129 case XK_Super_R:
334208b7 6130 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6131 break;
11edeb03
JB
6132
6133 case XK_Shift_Lock:
6134 /* Ignore this if it's not on the lock modifier. */
6135 if ((1 << row) == LockMask)
334208b7 6136 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6137 break;
28430d3c
JB
6138 }
6139 }
6140 }
6141 }
6142 }
6143
f689eb05 6144 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6145 if (! dpyinfo->meta_mod_mask)
a3c44b14 6146 {
334208b7
RS
6147 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6148 dpyinfo->alt_mod_mask = 0;
a3c44b14 6149 }
f689eb05 6150
148c4b70
RS
6151 /* If some keys are both alt and meta,
6152 make them just meta, not alt. */
334208b7 6153 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6154 {
334208b7 6155 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6156 }
58769bee 6157
28430d3c 6158 XFree ((char *) syms);
f689eb05 6159 XFreeModifiermap (mods);
28430d3c
JB
6160}
6161
dfeccd2d
JB
6162/* Convert between the modifier bits X uses and the modifier bits
6163 Emacs uses. */
06a2c219 6164
7c5283e4 6165static unsigned int
334208b7
RS
6166x_x_to_emacs_modifiers (dpyinfo, state)
6167 struct x_display_info *dpyinfo;
dc6f92b8
JB
6168 unsigned int state;
6169{
334208b7
RS
6170 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6171 | ((state & ControlMask) ? ctrl_modifier : 0)
6172 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6173 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6174 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6175 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6176}
6177
dfeccd2d 6178static unsigned int
334208b7
RS
6179x_emacs_to_x_modifiers (dpyinfo, state)
6180 struct x_display_info *dpyinfo;
dfeccd2d
JB
6181 unsigned int state;
6182{
334208b7
RS
6183 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6184 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6185 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6186 | ((state & shift_modifier) ? ShiftMask : 0)
6187 | ((state & ctrl_modifier) ? ControlMask : 0)
6188 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6189}
d047c4eb
KH
6190
6191/* Convert a keysym to its name. */
6192
6193char *
6194x_get_keysym_name (keysym)
6195 KeySym keysym;
6196{
6197 char *value;
6198
6199 BLOCK_INPUT;
6200 value = XKeysymToString (keysym);
6201 UNBLOCK_INPUT;
6202
6203 return value;
6204}
06a2c219
GM
6205
6206
e4571a43
JB
6207\f
6208/* Mouse clicks and mouse movement. Rah. */
e4571a43 6209
06a2c219
GM
6210/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6211 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6212 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6213 not force the value into range. */
69388238 6214
c8dba240 6215void
69388238 6216pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6217 FRAME_PTR f;
69388238 6218 register int pix_x, pix_y;
e4571a43
JB
6219 register int *x, *y;
6220 XRectangle *bounds;
69388238 6221 int noclip;
e4571a43 6222{
06a2c219 6223 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6224 even for negative values. */
6225 if (pix_x < 0)
7556890b 6226 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6227 if (pix_y < 0)
7556890b 6228 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6229
e4571a43
JB
6230 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6231 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6232
6233 if (bounds)
6234 {
7556890b
RS
6235 bounds->width = FONT_WIDTH (f->output_data.x->font);
6236 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6237 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6238 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6239 }
6240
69388238
RS
6241 if (!noclip)
6242 {
6243 if (pix_x < 0)
6244 pix_x = 0;
3cbd2e0b
RS
6245 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6246 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6247
6248 if (pix_y < 0)
6249 pix_y = 0;
6250 else if (pix_y > f->height)
6251 pix_y = f->height;
6252 }
e4571a43
JB
6253
6254 *x = pix_x;
6255 *y = pix_y;
6256}
6257
06a2c219
GM
6258
6259/* Given HPOS/VPOS in the current matrix of W, return corresponding
6260 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6261 can't tell the positions because W's display is not up to date,
6262 return 0. */
6263
6264int
6265glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6266 struct window *w;
6267 int hpos, vpos;
6268 int *frame_x, *frame_y;
2b5c9e71 6269{
06a2c219
GM
6270 int success_p;
6271
6272 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6273 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6274
6275 if (display_completed)
6276 {
6277 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6278 struct glyph *glyph = row->glyphs[TEXT_AREA];
6279 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6280
6281 *frame_y = row->y;
6282 *frame_x = row->x;
6283 while (glyph < end)
6284 {
6285 *frame_x += glyph->pixel_width;
6286 ++glyph;
6287 }
6288
6289 success_p = 1;
6290 }
6291 else
6292 {
6293 *frame_y = *frame_x = 0;
6294 success_p = 0;
6295 }
6296
6297 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6298 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6299 return success_p;
2b5c9e71
RS
6300}
6301
06a2c219 6302
dc6f92b8
JB
6303/* Prepare a mouse-event in *RESULT for placement in the input queue.
6304
6305 If the event is a button press, then note that we have grabbed
f451eb13 6306 the mouse. */
dc6f92b8
JB
6307
6308static Lisp_Object
f451eb13 6309construct_mouse_click (result, event, f)
dc6f92b8
JB
6310 struct input_event *result;
6311 XButtonEvent *event;
f676886a 6312 struct frame *f;
dc6f92b8 6313{
f451eb13 6314 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6315 otherwise. */
f451eb13 6316 result->kind = mouse_click;
69388238 6317 result->code = event->button - Button1;
1113d9db 6318 result->timestamp = event->time;
334208b7
RS
6319 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6320 event->state)
f689eb05 6321 | (event->type == ButtonRelease
58769bee 6322 ? up_modifier
f689eb05 6323 : down_modifier));
dc6f92b8 6324
06a2c219
GM
6325 XSETINT (result->x, event->x);
6326 XSETINT (result->y, event->y);
6327 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6328 result->arg = Qnil;
06a2c219 6329 return Qnil;
dc6f92b8 6330}
b849c413 6331
69388238 6332\f
90e65f07
JB
6333/* Function to report a mouse movement to the mainstream Emacs code.
6334 The input handler calls this.
6335
6336 We have received a mouse movement event, which is given in *event.
6337 If the mouse is over a different glyph than it was last time, tell
6338 the mainstream emacs code by setting mouse_moved. If not, ask for
6339 another motion event, so we can check again the next time it moves. */
b8009dd1 6340
06a2c219
GM
6341static XMotionEvent last_mouse_motion_event;
6342static Lisp_Object last_mouse_motion_frame;
6343
90e65f07 6344static void
12ba150f 6345note_mouse_movement (frame, event)
f676886a 6346 FRAME_PTR frame;
90e65f07 6347 XMotionEvent *event;
90e65f07 6348{
e5d77022 6349 last_mouse_movement_time = event->time;
06a2c219
GM
6350 last_mouse_motion_event = *event;
6351 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6352
27f338af
RS
6353 if (event->window != FRAME_X_WINDOW (frame))
6354 {
39d8bb4d 6355 frame->mouse_moved = 1;
27f338af 6356 last_mouse_scroll_bar = Qnil;
27f338af 6357 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6358 }
6359
90e65f07 6360 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6361 else if (event->x < last_mouse_glyph.x
6362 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6363 || event->y < last_mouse_glyph.y
6364 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6365 {
39d8bb4d 6366 frame->mouse_moved = 1;
ab648270 6367 last_mouse_scroll_bar = Qnil;
b8009dd1 6368 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6369 }
6370}
6371
bf1c0ba1 6372/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6373
06a2c219
GM
6374 int disable_mouse_highlight;
6375
6376
6377\f
6378/************************************************************************
6379 Mouse Face
6380 ************************************************************************/
6381
6382/* Find the glyph under window-relative coordinates X/Y in window W.
6383 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6384 strings. Return in *HPOS and *VPOS the row and column number of
6385 the glyph found. Return in *AREA the glyph area containing X.
6386 Value is a pointer to the glyph found or null if X/Y is not on
6387 text, or we can't tell because W's current matrix is not up to
6388 date. */
6389
6390static struct glyph *
6391x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6392 struct window *w;
6393 int x, y;
6394 int *hpos, *vpos, *area;
6395{
6396 struct glyph *glyph, *end;
3e71d8f2 6397 struct glyph_row *row = NULL;
06a2c219
GM
6398 int x0, i, left_area_width;
6399
6400 /* Find row containing Y. Give up if some row is not enabled. */
6401 for (i = 0; i < w->current_matrix->nrows; ++i)
6402 {
6403 row = MATRIX_ROW (w->current_matrix, i);
6404 if (!row->enabled_p)
6405 return NULL;
6406 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6407 break;
6408 }
6409
6410 *vpos = i;
6411 *hpos = 0;
6412
6413 /* Give up if Y is not in the window. */
6414 if (i == w->current_matrix->nrows)
6415 return NULL;
6416
6417 /* Get the glyph area containing X. */
6418 if (w->pseudo_window_p)
6419 {
6420 *area = TEXT_AREA;
6421 x0 = 0;
6422 }
6423 else
6424 {
6425 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6426 if (x < left_area_width)
6427 {
6428 *area = LEFT_MARGIN_AREA;
6429 x0 = 0;
6430 }
6431 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6432 {
6433 *area = TEXT_AREA;
6434 x0 = row->x + left_area_width;
6435 }
6436 else
6437 {
6438 *area = RIGHT_MARGIN_AREA;
6439 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6440 }
6441 }
6442
6443 /* Find glyph containing X. */
6444 glyph = row->glyphs[*area];
6445 end = glyph + row->used[*area];
6446 while (glyph < end)
6447 {
6448 if (x < x0 + glyph->pixel_width)
6449 {
6450 if (w->pseudo_window_p)
6451 break;
6452 else if (BUFFERP (glyph->object))
6453 break;
6454 }
6455
6456 x0 += glyph->pixel_width;
6457 ++glyph;
6458 }
6459
6460 if (glyph == end)
6461 return NULL;
6462
6463 *hpos = glyph - row->glyphs[*area];
6464 return glyph;
6465}
6466
6467
6468/* Convert frame-relative x/y to coordinates relative to window W.
6469 Takes pseudo-windows into account. */
6470
6471static void
6472frame_to_window_pixel_xy (w, x, y)
6473 struct window *w;
6474 int *x, *y;
6475{
6476 if (w->pseudo_window_p)
6477 {
6478 /* A pseudo-window is always full-width, and starts at the
6479 left edge of the frame, plus a frame border. */
6480 struct frame *f = XFRAME (w->frame);
6481 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6482 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6483 }
6484 else
6485 {
6486 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6487 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6488 }
6489}
6490
6491
e371a781 6492/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6493 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6494 mode line. X is relative to the start of the text display area of
6495 W, so the width of bitmap areas and scroll bars must be subtracted
6496 to get a position relative to the start of the mode line. */
6497
6498static void
6499note_mode_line_highlight (w, x, mode_line_p)
6500 struct window *w;
6501 int x, mode_line_p;
6502{
6503 struct frame *f = XFRAME (w->frame);
6504 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6505 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6506 struct glyph_row *row;
6507
6508 if (mode_line_p)
6509 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6510 else
045dee35 6511 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6512
06a2c219
GM
6513 if (row->enabled_p)
6514 {
6515 struct glyph *glyph, *end;
6516 Lisp_Object help, map;
6517 int x0;
6518
6519 /* Find the glyph under X. */
6520 glyph = row->glyphs[TEXT_AREA];
6521 end = glyph + row->used[TEXT_AREA];
6522 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6523 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
e371a781 6524
06a2c219
GM
6525 while (glyph < end
6526 && x >= x0 + glyph->pixel_width)
6527 {
6528 x0 += glyph->pixel_width;
6529 ++glyph;
6530 }
6531
6532 if (glyph < end
6533 && STRINGP (glyph->object)
6534 && XSTRING (glyph->object)->intervals
6535 && glyph->charpos >= 0
6536 && glyph->charpos < XSTRING (glyph->object)->size)
6537 {
6538 /* If we're on a string with `help-echo' text property,
6539 arrange for the help to be displayed. This is done by
6540 setting the global variable help_echo to the help string. */
6541 help = Fget_text_property (make_number (glyph->charpos),
6542 Qhelp_echo, glyph->object);
b7e80413 6543 if (!NILP (help))
be010514
GM
6544 {
6545 help_echo = help;
7cea38bc 6546 XSETWINDOW (help_echo_window, w);
be010514
GM
6547 help_echo_object = glyph->object;
6548 help_echo_pos = glyph->charpos;
6549 }
06a2c219
GM
6550
6551 /* Change the mouse pointer according to what is under X/Y. */
6552 map = Fget_text_property (make_number (glyph->charpos),
6553 Qlocal_map, glyph->object);
02067692 6554 if (KEYMAPP (map))
06a2c219 6555 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6556 else
6557 {
6558 map = Fget_text_property (make_number (glyph->charpos),
6559 Qkeymap, glyph->object);
02067692 6560 if (KEYMAPP (map))
be010514
GM
6561 cursor = f->output_data.x->nontext_cursor;
6562 }
06a2c219
GM
6563 }
6564 }
6565
6566 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6567}
6568
6569
6570/* Take proper action when the mouse has moved to position X, Y on
6571 frame F as regards highlighting characters that have mouse-face
6572 properties. Also de-highlighting chars where the mouse was before.
27f338af 6573 X and Y can be negative or out of range. */
b8009dd1
RS
6574
6575static void
6576note_mouse_highlight (f, x, y)
06a2c219 6577 struct frame *f;
c32cdd9a 6578 int x, y;
b8009dd1 6579{
06a2c219
GM
6580 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6581 int portion;
b8009dd1
RS
6582 Lisp_Object window;
6583 struct window *w;
6584
06a2c219
GM
6585 /* When a menu is active, don't highlight because this looks odd. */
6586#ifdef USE_X_TOOLKIT
6587 if (popup_activated ())
6588 return;
6589#endif
6590
04fff9c0
GM
6591 if (disable_mouse_highlight
6592 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6593 return;
6594
06a2c219
GM
6595 dpyinfo->mouse_face_mouse_x = x;
6596 dpyinfo->mouse_face_mouse_y = y;
6597 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6598
06a2c219 6599 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6600 return;
6601
514e4681
RS
6602 if (gc_in_progress)
6603 {
06a2c219 6604 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6605 return;
6606 }
6607
b8009dd1 6608 /* Which window is that in? */
06a2c219 6609 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6610
6611 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6612 if (! EQ (window, dpyinfo->mouse_face_window))
6613 clear_mouse_face (dpyinfo);
6614
6615 /* Not on a window -> return. */
6616 if (!WINDOWP (window))
6617 return;
6618
6619 /* Convert to window-relative pixel coordinates. */
6620 w = XWINDOW (window);
6621 frame_to_window_pixel_xy (w, &x, &y);
6622
9ea173e8 6623 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6624 buffer. */
9ea173e8 6625 if (EQ (window, f->tool_bar_window))
06a2c219 6626 {
9ea173e8 6627 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6628 return;
6629 }
6630
6631 if (portion == 1 || portion == 3)
6632 {
6633 /* Mouse is on the mode or top line. */
6634 note_mode_line_highlight (w, x, portion == 1);
6635 return;
6636 }
e371a781
GM
6637 else if (portion == 2)
6638 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6639 f->output_data.x->horizontal_drag_cursor);
06a2c219
GM
6640 else
6641 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6642 f->output_data.x->text_cursor);
b8009dd1 6643
0cdd0c9f
RS
6644 /* Are we in a window whose display is up to date?
6645 And verify the buffer's text has not changed. */
06a2c219
GM
6646 if (/* Within text portion of the window. */
6647 portion == 0
0cdd0c9f 6648 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6649 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6650 && (XFASTINT (w->last_overlay_modified)
6651 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6652 {
06a2c219
GM
6653 int hpos, vpos, pos, i, area;
6654 struct glyph *glyph;
b8009dd1 6655
06a2c219
GM
6656 /* Find the glyph under X/Y. */
6657 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6658
6659 /* Clear mouse face if X/Y not over text. */
6660 if (glyph == NULL
6661 || area != TEXT_AREA
6662 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6663 {
06a2c219
GM
6664 clear_mouse_face (dpyinfo);
6665 return;
6666 }
6667
6668 pos = glyph->charpos;
6669 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6670
6671 /* Check for mouse-face and help-echo. */
6672 {
6673 Lisp_Object mouse_face, overlay, position;
6674 Lisp_Object *overlay_vec;
6675 int len, noverlays;
6676 struct buffer *obuf;
6677 int obegv, ozv;
6678
6679 /* If we get an out-of-range value, return now; avoid an error. */
6680 if (pos > BUF_Z (XBUFFER (w->buffer)))
6681 return;
6682
6683 /* Make the window's buffer temporarily current for
6684 overlays_at and compute_char_face. */
6685 obuf = current_buffer;
6686 current_buffer = XBUFFER (w->buffer);
6687 obegv = BEGV;
6688 ozv = ZV;
6689 BEGV = BEG;
6690 ZV = Z;
6691
6692 /* Is this char mouse-active or does it have help-echo? */
6693 XSETINT (position, pos);
6694
6695 /* Put all the overlays we want in a vector in overlay_vec.
6696 Store the length in len. If there are more than 10, make
6697 enough space for all, and try again. */
6698 len = 10;
6699 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6700 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6701 if (noverlays > len)
6702 {
6703 len = noverlays;
6704 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6705 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6706 }
f8349001
GM
6707
6708 /* Sort overlays into increasing priority order. */
06a2c219
GM
6709 noverlays = sort_overlays (overlay_vec, noverlays, w);
6710
6711 /* Check mouse-face highlighting. */
6712 if (! (EQ (window, dpyinfo->mouse_face_window)
6713 && vpos >= dpyinfo->mouse_face_beg_row
6714 && vpos <= dpyinfo->mouse_face_end_row
6715 && (vpos > dpyinfo->mouse_face_beg_row
6716 || hpos >= dpyinfo->mouse_face_beg_col)
6717 && (vpos < dpyinfo->mouse_face_end_row
6718 || hpos < dpyinfo->mouse_face_end_col
6719 || dpyinfo->mouse_face_past_end)))
6720 {
6721 /* Clear the display of the old active region, if any. */
6722 clear_mouse_face (dpyinfo);
6723
6724 /* Find the highest priority overlay that has a mouse-face prop. */
6725 overlay = Qnil;
f8349001 6726 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6727 {
6728 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6729 if (!NILP (mouse_face))
6730 {
6731 overlay = overlay_vec[i];
6732 break;
6733 }
6734 }
6735
6736 /* If no overlay applies, get a text property. */
6737 if (NILP (overlay))
6738 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6739
6740 /* Handle the overlay case. */
6741 if (! NILP (overlay))
6742 {
6743 /* Find the range of text around this char that
6744 should be active. */
6745 Lisp_Object before, after;
6746 int ignore;
6747
6748 before = Foverlay_start (overlay);
6749 after = Foverlay_end (overlay);
6750 /* Record this as the current active region. */
6751 fast_find_position (w, XFASTINT (before),
6752 &dpyinfo->mouse_face_beg_col,
6753 &dpyinfo->mouse_face_beg_row,
6754 &dpyinfo->mouse_face_beg_x,
6755 &dpyinfo->mouse_face_beg_y);
6756 dpyinfo->mouse_face_past_end
6757 = !fast_find_position (w, XFASTINT (after),
6758 &dpyinfo->mouse_face_end_col,
6759 &dpyinfo->mouse_face_end_row,
6760 &dpyinfo->mouse_face_end_x,
6761 &dpyinfo->mouse_face_end_y);
6762 dpyinfo->mouse_face_window = window;
6763 dpyinfo->mouse_face_face_id
6764 = face_at_buffer_position (w, pos, 0, 0,
6765 &ignore, pos + 1, 1);
6766
6767 /* Display it as active. */
6768 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6769 }
6770 /* Handle the text property case. */
6771 else if (! NILP (mouse_face))
6772 {
6773 /* Find the range of text around this char that
6774 should be active. */
6775 Lisp_Object before, after, beginning, end;
6776 int ignore;
6777
6778 beginning = Fmarker_position (w->start);
6779 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6780 - XFASTINT (w->window_end_pos)));
6781 before
6782 = Fprevious_single_property_change (make_number (pos + 1),
6783 Qmouse_face,
6784 w->buffer, beginning);
6785 after
6786 = Fnext_single_property_change (position, Qmouse_face,
6787 w->buffer, end);
6788 /* Record this as the current active region. */
6789 fast_find_position (w, XFASTINT (before),
6790 &dpyinfo->mouse_face_beg_col,
6791 &dpyinfo->mouse_face_beg_row,
6792 &dpyinfo->mouse_face_beg_x,
6793 &dpyinfo->mouse_face_beg_y);
6794 dpyinfo->mouse_face_past_end
6795 = !fast_find_position (w, XFASTINT (after),
6796 &dpyinfo->mouse_face_end_col,
6797 &dpyinfo->mouse_face_end_row,
6798 &dpyinfo->mouse_face_end_x,
6799 &dpyinfo->mouse_face_end_y);
6800 dpyinfo->mouse_face_window = window;
6801 dpyinfo->mouse_face_face_id
6802 = face_at_buffer_position (w, pos, 0, 0,
6803 &ignore, pos + 1, 1);
6804
6805 /* Display it as active. */
6806 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6807 }
6808 }
6809
6810 /* Look for a `help-echo' property. */
6811 {
743934db 6812 Lisp_Object help, overlay;
06a2c219
GM
6813
6814 /* Check overlays first. */
f9b5db02 6815 help = overlay = Qnil;
f8349001 6816 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6817 {
6818 overlay = overlay_vec[i];
6819 help = Foverlay_get (overlay, Qhelp_echo);
6820 }
be010514
GM
6821
6822 if (!NILP (help))
6823 {
6824 help_echo = help;
7cea38bc 6825 help_echo_window = window;
743934db 6826 help_echo_object = overlay;
be010514
GM
6827 help_echo_pos = pos;
6828 }
6829 else
6830 {
6831 /* Try text properties. */
6832 if ((STRINGP (glyph->object)
06a2c219
GM
6833 && glyph->charpos >= 0
6834 && glyph->charpos < XSTRING (glyph->object)->size)
6835 || (BUFFERP (glyph->object)
6836 && glyph->charpos >= BEGV
be010514
GM
6837 && glyph->charpos < ZV))
6838 help = Fget_text_property (make_number (glyph->charpos),
6839 Qhelp_echo, glyph->object);
06a2c219 6840
be010514
GM
6841 if (!NILP (help))
6842 {
6843 help_echo = help;
7cea38bc 6844 help_echo_window = window;
be010514
GM
6845 help_echo_object = glyph->object;
6846 help_echo_pos = glyph->charpos;
6847 }
6848 }
06a2c219
GM
6849 }
6850
6851 BEGV = obegv;
6852 ZV = ozv;
6853 current_buffer = obuf;
6854 }
6855 }
6856}
6857
6858static void
6859redo_mouse_highlight ()
6860{
6861 if (!NILP (last_mouse_motion_frame)
6862 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6863 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6864 last_mouse_motion_event.x,
6865 last_mouse_motion_event.y);
6866}
6867
6868
6869\f
6870/***********************************************************************
9ea173e8 6871 Tool-bars
06a2c219
GM
6872 ***********************************************************************/
6873
9ea173e8
GM
6874static int x_tool_bar_item P_ ((struct frame *, int, int,
6875 struct glyph **, int *, int *, int *));
06a2c219 6876
9ea173e8 6877/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6878 or -1. */
6879
9ea173e8 6880static int last_tool_bar_item;
06a2c219
GM
6881
6882
9ea173e8
GM
6883/* Get information about the tool-bar item at position X/Y on frame F.
6884 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6885 the current matrix of the tool-bar window of F, or NULL if not
6886 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 6887 item in F->tool_bar_items. Value is
06a2c219 6888
9ea173e8 6889 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6890 0 if X/Y is on the same item that was highlighted before.
6891 1 otherwise. */
6892
6893static int
9ea173e8 6894x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6895 struct frame *f;
6896 int x, y;
6897 struct glyph **glyph;
6898 int *hpos, *vpos, *prop_idx;
6899{
6900 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6901 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6902 int area;
6903
6904 /* Find the glyph under X/Y. */
6905 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6906 if (*glyph == NULL)
6907 return -1;
6908
9ea173e8 6909 /* Get the start of this tool-bar item's properties in
8daf1204 6910 f->tool_bar_items. */
9ea173e8 6911 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6912 return -1;
6913
6914 /* Is mouse on the highlighted item? */
9ea173e8 6915 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6916 && *vpos >= dpyinfo->mouse_face_beg_row
6917 && *vpos <= dpyinfo->mouse_face_end_row
6918 && (*vpos > dpyinfo->mouse_face_beg_row
6919 || *hpos >= dpyinfo->mouse_face_beg_col)
6920 && (*vpos < dpyinfo->mouse_face_end_row
6921 || *hpos < dpyinfo->mouse_face_end_col
6922 || dpyinfo->mouse_face_past_end))
6923 return 0;
6924
6925 return 1;
6926}
6927
6928
9ea173e8 6929/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6930 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6931 or ButtonRelase. */
6932
6933static void
9ea173e8 6934x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6935 struct frame *f;
6936 XButtonEvent *button_event;
6937{
6938 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6939 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6940 int hpos, vpos, prop_idx;
6941 struct glyph *glyph;
6942 Lisp_Object enabled_p;
6943 int x = button_event->x;
6944 int y = button_event->y;
6945
9ea173e8 6946 /* If not on the highlighted tool-bar item, return. */
06a2c219 6947 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6948 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6949 return;
6950
6951 /* If item is disabled, do nothing. */
8daf1204 6952 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
6953 if (NILP (enabled_p))
6954 return;
6955
6956 if (button_event->type == ButtonPress)
6957 {
6958 /* Show item in pressed state. */
6959 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6960 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6961 last_tool_bar_item = prop_idx;
06a2c219
GM
6962 }
6963 else
6964 {
6965 Lisp_Object key, frame;
6966 struct input_event event;
6967
6968 /* Show item in released state. */
6969 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6970 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6971
8daf1204 6972 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
6973
6974 XSETFRAME (frame, f);
9ea173e8 6975 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6976 event.frame_or_window = frame;
6977 event.arg = frame;
06a2c219
GM
6978 kbd_buffer_store_event (&event);
6979
9ea173e8 6980 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6981 event.frame_or_window = frame;
6982 event.arg = key;
06a2c219
GM
6983 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6984 button_event->state);
6985 kbd_buffer_store_event (&event);
9ea173e8 6986 last_tool_bar_item = -1;
06a2c219
GM
6987 }
6988}
6989
6990
9ea173e8
GM
6991/* Possibly highlight a tool-bar item on frame F when mouse moves to
6992 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6993 note_mouse_highlight. */
6994
6995static void
9ea173e8 6996note_tool_bar_highlight (f, x, y)
06a2c219
GM
6997 struct frame *f;
6998 int x, y;
6999{
9ea173e8 7000 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7001 struct window *w = XWINDOW (window);
7002 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7003 int hpos, vpos;
7004 struct glyph *glyph;
7005 struct glyph_row *row;
5c187dee 7006 int i;
06a2c219
GM
7007 Lisp_Object enabled_p;
7008 int prop_idx;
7009 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 7010 int mouse_down_p, rc;
06a2c219
GM
7011
7012 /* Function note_mouse_highlight is called with negative x(y
7013 values when mouse moves outside of the frame. */
7014 if (x <= 0 || y <= 0)
7015 {
7016 clear_mouse_face (dpyinfo);
7017 return;
7018 }
7019
9ea173e8 7020 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7021 if (rc < 0)
7022 {
9ea173e8 7023 /* Not on tool-bar item. */
06a2c219
GM
7024 clear_mouse_face (dpyinfo);
7025 return;
7026 }
7027 else if (rc == 0)
9ea173e8 7028 /* On same tool-bar item as before. */
06a2c219 7029 goto set_help_echo;
b8009dd1 7030
06a2c219
GM
7031 clear_mouse_face (dpyinfo);
7032
9ea173e8 7033 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7034 mouse_down_p = (dpyinfo->grabbed
7035 && f == last_mouse_frame
7036 && FRAME_LIVE_P (f));
7037 if (mouse_down_p
9ea173e8 7038 && last_tool_bar_item != prop_idx)
06a2c219
GM
7039 return;
7040
7041 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7042 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7043
9ea173e8 7044 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7045 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7046 if (!NILP (enabled_p))
7047 {
7048 /* Compute the x-position of the glyph. In front and past the
7049 image is a space. We include this is the highlighted area. */
7050 row = MATRIX_ROW (w->current_matrix, vpos);
7051 for (i = x = 0; i < hpos; ++i)
7052 x += row->glyphs[TEXT_AREA][i].pixel_width;
7053
7054 /* Record this as the current active region. */
7055 dpyinfo->mouse_face_beg_col = hpos;
7056 dpyinfo->mouse_face_beg_row = vpos;
7057 dpyinfo->mouse_face_beg_x = x;
7058 dpyinfo->mouse_face_beg_y = row->y;
7059 dpyinfo->mouse_face_past_end = 0;
7060
7061 dpyinfo->mouse_face_end_col = hpos + 1;
7062 dpyinfo->mouse_face_end_row = vpos;
7063 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7064 dpyinfo->mouse_face_end_y = row->y;
7065 dpyinfo->mouse_face_window = window;
9ea173e8 7066 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7067
7068 /* Display it as active. */
7069 show_mouse_face (dpyinfo, draw);
7070 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7071 }
06a2c219
GM
7072
7073 set_help_echo:
7074
9ea173e8 7075 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 7076 XTread_socket does the rest. */
7cea38bc 7077 help_echo_object = help_echo_window = Qnil;
be010514 7078 help_echo_pos = -1;
8daf1204 7079 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7080 if (NILP (help_echo))
8daf1204 7081 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7082}
4d73d038 7083
06a2c219
GM
7084
7085\f
7086/* Find the glyph matrix position of buffer position POS in window W.
7087 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7088 current glyphs must be up to date. If POS is above window start
7089 return (0, 0, 0, 0). If POS is after end of W, return end of
7090 last line in W. */
b8009dd1
RS
7091
7092static int
06a2c219
GM
7093fast_find_position (w, pos, hpos, vpos, x, y)
7094 struct window *w;
b8009dd1 7095 int pos;
06a2c219 7096 int *hpos, *vpos, *x, *y;
b8009dd1 7097{
b8009dd1 7098 int i;
bf1c0ba1 7099 int lastcol;
06a2c219
GM
7100 int maybe_next_line_p = 0;
7101 int line_start_position;
7102 int yb = window_text_bottom_y (w);
7103 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7104 struct glyph_row *best_row = row;
7105 int row_vpos = 0, best_row_vpos = 0;
7106 int current_x;
7107
7108 while (row->y < yb)
b8009dd1 7109 {
06a2c219
GM
7110 if (row->used[TEXT_AREA])
7111 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7112 else
7113 line_start_position = 0;
7114
7115 if (line_start_position > pos)
b8009dd1 7116 break;
77b68646
RS
7117 /* If the position sought is the end of the buffer,
7118 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7119 else if (line_start_position == pos
7120 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7121 {
06a2c219 7122 maybe_next_line_p = 1;
77b68646
RS
7123 break;
7124 }
06a2c219
GM
7125 else if (line_start_position > 0)
7126 {
7127 best_row = row;
7128 best_row_vpos = row_vpos;
7129 }
4b0bb6f3
GM
7130
7131 if (row->y + row->height >= yb)
7132 break;
06a2c219
GM
7133
7134 ++row;
7135 ++row_vpos;
b8009dd1 7136 }
06a2c219
GM
7137
7138 /* Find the right column within BEST_ROW. */
7139 lastcol = 0;
7140 current_x = best_row->x;
7141 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7142 {
06a2c219
GM
7143 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7144 int charpos;
7145
7146 charpos = glyph->charpos;
7147 if (charpos == pos)
bf1c0ba1 7148 {
06a2c219
GM
7149 *hpos = i;
7150 *vpos = best_row_vpos;
7151 *x = current_x;
7152 *y = best_row->y;
bf1c0ba1
RS
7153 return 1;
7154 }
06a2c219 7155 else if (charpos > pos)
4d73d038 7156 break;
06a2c219
GM
7157 else if (charpos > 0)
7158 lastcol = i;
7159
7160 current_x += glyph->pixel_width;
bf1c0ba1 7161 }
b8009dd1 7162
77b68646
RS
7163 /* If we're looking for the end of the buffer,
7164 and we didn't find it in the line we scanned,
7165 use the start of the following line. */
06a2c219 7166 if (maybe_next_line_p)
77b68646 7167 {
06a2c219
GM
7168 ++best_row;
7169 ++best_row_vpos;
7170 lastcol = 0;
7171 current_x = best_row->x;
77b68646
RS
7172 }
7173
06a2c219
GM
7174 *vpos = best_row_vpos;
7175 *hpos = lastcol + 1;
7176 *x = current_x;
7177 *y = best_row->y;
b8009dd1
RS
7178 return 0;
7179}
7180
06a2c219 7181
b8009dd1
RS
7182/* Display the active region described by mouse_face_*
7183 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7184
7185static void
06a2c219 7186show_mouse_face (dpyinfo, draw)
7a13e894 7187 struct x_display_info *dpyinfo;
06a2c219 7188 enum draw_glyphs_face draw;
b8009dd1 7189{
7a13e894 7190 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7191 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7192 int i;
06a2c219
GM
7193 int cursor_off_p = 0;
7194 struct cursor_pos saved_cursor;
7195
7196 saved_cursor = output_cursor;
7197
7198 /* If window is in the process of being destroyed, don't bother
7199 to do anything. */
7200 if (w->current_matrix == NULL)
7201 goto set_x_cursor;
7202
7203 /* Recognize when we are called to operate on rows that don't exist
7204 anymore. This can happen when a window is split. */
7205 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7206 goto set_x_cursor;
7207
7208 set_output_cursor (&w->phys_cursor);
7209
7210 /* Note that mouse_face_beg_row etc. are window relative. */
7211 for (i = dpyinfo->mouse_face_beg_row;
7212 i <= dpyinfo->mouse_face_end_row;
7213 i++)
7214 {
7215 int start_hpos, end_hpos, start_x;
7216 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7217
7218 /* Don't do anything if row doesn't have valid contents. */
7219 if (!row->enabled_p)
7220 continue;
7221
7222 /* For all but the first row, the highlight starts at column 0. */
7223 if (i == dpyinfo->mouse_face_beg_row)
7224 {
7225 start_hpos = dpyinfo->mouse_face_beg_col;
7226 start_x = dpyinfo->mouse_face_beg_x;
7227 }
7228 else
7229 {
7230 start_hpos = 0;
7231 start_x = 0;
7232 }
7233
7234 if (i == dpyinfo->mouse_face_end_row)
7235 end_hpos = dpyinfo->mouse_face_end_col;
7236 else
7237 end_hpos = row->used[TEXT_AREA];
7238
7239 /* If the cursor's in the text we are about to rewrite, turn the
7240 cursor off. */
7241 if (!w->pseudo_window_p
7242 && i == output_cursor.vpos
7243 && output_cursor.hpos >= start_hpos - 1
7244 && output_cursor.hpos <= end_hpos)
514e4681 7245 {
06a2c219
GM
7246 x_update_window_cursor (w, 0);
7247 cursor_off_p = 1;
514e4681 7248 }
b8009dd1 7249
06a2c219 7250 if (end_hpos > start_hpos)
64f26cf5
GM
7251 {
7252 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7253 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7254 start_hpos, end_hpos, draw, NULL, NULL, 0);
7255 }
b8009dd1
RS
7256 }
7257
514e4681 7258 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7259 if (cursor_off_p)
7260 x_display_cursor (w, 1,
7261 output_cursor.hpos, output_cursor.vpos,
7262 output_cursor.x, output_cursor.y);
2729a2b5 7263
06a2c219 7264 output_cursor = saved_cursor;
fb3b7de5 7265
06a2c219
GM
7266 set_x_cursor:
7267
7268 /* Change the mouse cursor. */
7269 if (draw == DRAW_NORMAL_TEXT)
7270 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7271 f->output_data.x->text_cursor);
7272 else if (draw == DRAW_MOUSE_FACE)
334208b7 7273 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7274 f->output_data.x->cross_cursor);
27ead1d5 7275 else
334208b7 7276 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7277 f->output_data.x->nontext_cursor);
b8009dd1
RS
7278}
7279
7280/* Clear out the mouse-highlighted active region.
06a2c219 7281 Redraw it un-highlighted first. */
b8009dd1 7282
06a2c219 7283void
7a13e894
RS
7284clear_mouse_face (dpyinfo)
7285 struct x_display_info *dpyinfo;
b8009dd1 7286{
607d9f9f
GM
7287#if 0 /* This prevents redrawing tool bar items when changing from one
7288 to another while a tooltip is open, so don't do it. */
685f4368 7289 if (!NILP (tip_frame))
06a2c219 7290 return;
7948d50a 7291#endif
06a2c219 7292
7a13e894 7293 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7294 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7295
7a13e894
RS
7296 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7297 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7298 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7299}
e687d06e 7300
71b8321e
GM
7301
7302/* Clear any mouse-face on window W. This function is part of the
7303 redisplay interface, and is called from try_window_id and similar
7304 functions to ensure the mouse-highlight is off. */
7305
7306static void
7307x_clear_mouse_face (w)
7308 struct window *w;
7309{
7310 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7311 Lisp_Object window;
7312
2e636f9d 7313 BLOCK_INPUT;
71b8321e
GM
7314 XSETWINDOW (window, w);
7315 if (EQ (window, dpyinfo->mouse_face_window))
7316 clear_mouse_face (dpyinfo);
2e636f9d 7317 UNBLOCK_INPUT;
71b8321e
GM
7318}
7319
7320
e687d06e
RS
7321/* Just discard the mouse face information for frame F, if any.
7322 This is used when the size of F is changed. */
7323
dfcf069d 7324void
e687d06e
RS
7325cancel_mouse_face (f)
7326 FRAME_PTR f;
7327{
7328 Lisp_Object window;
7329 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7330
7331 window = dpyinfo->mouse_face_window;
7332 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7333 {
7334 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7335 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7336 dpyinfo->mouse_face_window = Qnil;
7337 }
7338}
b8009dd1 7339\f
ab648270
JB
7340static struct scroll_bar *x_window_to_scroll_bar ();
7341static void x_scroll_bar_report_motion ();
12ba150f 7342
90e65f07 7343/* Return the current position of the mouse.
2d7fc7e8 7344 *fp should be a frame which indicates which display to ask about.
90e65f07 7345
2d7fc7e8 7346 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7347 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7348 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7349 position on the scroll bar.
12ba150f 7350
2d7fc7e8 7351 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7352 mouse is on, *bar_window to nil, and *x and *y to the character cell
7353 the mouse is over.
7354
06a2c219 7355 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7356 was at this position.
7357
a135645a
RS
7358 Don't store anything if we don't have a valid set of values to report.
7359
90e65f07 7360 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7361 movement. */
90e65f07
JB
7362
7363static void
1cf412ec 7364XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7365 FRAME_PTR *fp;
1cf412ec 7366 int insist;
12ba150f 7367 Lisp_Object *bar_window;
ab648270 7368 enum scroll_bar_part *part;
90e65f07 7369 Lisp_Object *x, *y;
e5d77022 7370 unsigned long *time;
90e65f07 7371{
a135645a
RS
7372 FRAME_PTR f1;
7373
90e65f07
JB
7374 BLOCK_INPUT;
7375
8bcee03e 7376 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7377 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7378 else
7379 {
12ba150f
JB
7380 Window root;
7381 int root_x, root_y;
90e65f07 7382
12ba150f
JB
7383 Window dummy_window;
7384 int dummy;
7385
39d8bb4d
KH
7386 Lisp_Object frame, tail;
7387
7388 /* Clear the mouse-moved flag for every frame on this display. */
7389 FOR_EACH_FRAME (tail, frame)
7390 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7391 XFRAME (frame)->mouse_moved = 0;
7392
ab648270 7393 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7394
7395 /* Figure out which root window we're on. */
334208b7
RS
7396 XQueryPointer (FRAME_X_DISPLAY (*fp),
7397 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7398
7399 /* The root window which contains the pointer. */
7400 &root,
7401
7402 /* Trash which we can't trust if the pointer is on
7403 a different screen. */
7404 &dummy_window,
7405
7406 /* The position on that root window. */
58769bee 7407 &root_x, &root_y,
12ba150f
JB
7408
7409 /* More trash we can't trust. */
7410 &dummy, &dummy,
7411
7412 /* Modifier keys and pointer buttons, about which
7413 we don't care. */
7414 (unsigned int *) &dummy);
7415
7416 /* Now we have a position on the root; find the innermost window
7417 containing the pointer. */
7418 {
7419 Window win, child;
7420 int win_x, win_y;
06a2c219 7421 int parent_x = 0, parent_y = 0;
e99db5a1 7422 int count;
12ba150f
JB
7423
7424 win = root;
69388238 7425
2d7fc7e8
RS
7426 /* XTranslateCoordinates can get errors if the window
7427 structure is changing at the same time this function
7428 is running. So at least we must not crash from them. */
7429
e99db5a1 7430 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7431
334208b7 7432 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7433 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7434 {
69388238
RS
7435 /* If mouse was grabbed on a frame, give coords for that frame
7436 even if the mouse is now outside it. */
334208b7 7437 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7438
12ba150f 7439 /* From-window, to-window. */
69388238 7440 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7441
7442 /* From-position, to-position. */
7443 root_x, root_y, &win_x, &win_y,
7444
7445 /* Child of win. */
7446 &child);
69388238
RS
7447 f1 = last_mouse_frame;
7448 }
7449 else
7450 {
7451 while (1)
7452 {
334208b7 7453 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7454
69388238
RS
7455 /* From-window, to-window. */
7456 root, win,
12ba150f 7457
69388238
RS
7458 /* From-position, to-position. */
7459 root_x, root_y, &win_x, &win_y,
7460
7461 /* Child of win. */
7462 &child);
7463
9af3143a 7464 if (child == None || child == win)
69388238
RS
7465 break;
7466
7467 win = child;
7468 parent_x = win_x;
7469 parent_y = win_y;
7470 }
12ba150f 7471
69388238
RS
7472 /* Now we know that:
7473 win is the innermost window containing the pointer
7474 (XTC says it has no child containing the pointer),
7475 win_x and win_y are the pointer's position in it
7476 (XTC did this the last time through), and
7477 parent_x and parent_y are the pointer's position in win's parent.
7478 (They are what win_x and win_y were when win was child.
7479 If win is the root window, it has no parent, and
7480 parent_{x,y} are invalid, but that's okay, because we'll
7481 never use them in that case.) */
7482
7483 /* Is win one of our frames? */
19126e11 7484 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7485
7486#ifdef USE_X_TOOLKIT
7487 /* If we end up with the menu bar window, say it's not
7488 on the frame. */
7489 if (f1 != NULL
7490 && f1->output_data.x->menubar_widget
7491 && win == XtWindow (f1->output_data.x->menubar_widget))
7492 f1 = NULL;
7493#endif /* USE_X_TOOLKIT */
69388238 7494 }
58769bee 7495
2d7fc7e8
RS
7496 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7497 f1 = 0;
7498
e99db5a1 7499 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7500
ab648270 7501 /* If not, is it one of our scroll bars? */
a135645a 7502 if (! f1)
12ba150f 7503 {
ab648270 7504 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7505
7506 if (bar)
7507 {
a135645a 7508 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7509 win_x = parent_x;
7510 win_y = parent_y;
7511 }
7512 }
90e65f07 7513
8bcee03e 7514 if (f1 == 0 && insist > 0)
b86bd3dd 7515 f1 = SELECTED_FRAME ();
1cf412ec 7516
a135645a 7517 if (f1)
12ba150f 7518 {
06a2c219
GM
7519 /* Ok, we found a frame. Store all the values.
7520 last_mouse_glyph is a rectangle used to reduce the
7521 generation of mouse events. To not miss any motion
7522 events, we must divide the frame into rectangles of the
7523 size of the smallest character that could be displayed
7524 on it, i.e. into the same rectangles that matrices on
7525 the frame are divided into. */
7526
7527#if OLD_REDISPLAY_CODE
2b5c9e71 7528 int ignore1, ignore2;
2b5c9e71 7529 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7530 &last_mouse_glyph,
1cf412ec
RS
7531 FRAME_X_DISPLAY_INFO (f1)->grabbed
7532 || insist);
06a2c219
GM
7533#else
7534 {
7535 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7536 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7537 int x = win_x;
7538 int y = win_y;
7539
7540 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7541 round down even for negative values. */
7542 if (x < 0)
7543 x -= width - 1;
7544 if (y < 0)
7545 y -= height - 1;
7546
7547 last_mouse_glyph.width = width;
7548 last_mouse_glyph.height = height;
7549 last_mouse_glyph.x = (x + width - 1) / width * width;
7550 last_mouse_glyph.y = (y + height - 1) / height * height;
7551 }
7552#endif
12ba150f
JB
7553
7554 *bar_window = Qnil;
7555 *part = 0;
334208b7 7556 *fp = f1;
e0c1aef2
KH
7557 XSETINT (*x, win_x);
7558 XSETINT (*y, win_y);
12ba150f
JB
7559 *time = last_mouse_movement_time;
7560 }
7561 }
7562 }
90e65f07
JB
7563
7564 UNBLOCK_INPUT;
7565}
f451eb13 7566
06a2c219 7567
06a2c219 7568#ifdef USE_X_TOOLKIT
bffcfca9
GM
7569
7570/* Atimer callback function for TIMER. Called every 0.1s to process
7571 Xt timeouts, if needed. We must avoid calling XtAppPending as
7572 much as possible because that function does an implicit XFlush
7573 that slows us down. */
7574
7575static void
7576x_process_timeouts (timer)
7577 struct atimer *timer;
7578{
7579 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7580 {
7581 BLOCK_INPUT;
7582 while (XtAppPending (Xt_app_con) & XtIMTimer)
7583 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7584 UNBLOCK_INPUT;
7585 }
06a2c219
GM
7586}
7587
bffcfca9 7588#endif /* USE_X_TOOLKIT */
06a2c219
GM
7589
7590\f
7591/* Scroll bar support. */
7592
7593/* Given an X window ID, find the struct scroll_bar which manages it.
7594 This can be called in GC, so we have to make sure to strip off mark
7595 bits. */
bffcfca9 7596
06a2c219
GM
7597static struct scroll_bar *
7598x_window_to_scroll_bar (window_id)
7599 Window window_id;
7600{
7601 Lisp_Object tail;
7602
7603 for (tail = Vframe_list;
7604 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7605 tail = XCDR (tail))
06a2c219
GM
7606 {
7607 Lisp_Object frame, bar, condemned;
7608
8e713be6 7609 frame = XCAR (tail);
06a2c219
GM
7610 /* All elements of Vframe_list should be frames. */
7611 if (! GC_FRAMEP (frame))
7612 abort ();
7613
7614 /* Scan this frame's scroll bar list for a scroll bar with the
7615 right window ID. */
7616 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7617 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7618 /* This trick allows us to search both the ordinary and
7619 condemned scroll bar lists with one loop. */
7620 ! GC_NILP (bar) || (bar = condemned,
7621 condemned = Qnil,
7622 ! GC_NILP (bar));
7623 bar = XSCROLL_BAR (bar)->next)
7624 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7625 return XSCROLL_BAR (bar);
7626 }
7627
7628 return 0;
7629}
7630
7631
7632\f
7633/************************************************************************
7634 Toolkit scroll bars
7635 ************************************************************************/
7636
eccc05db 7637#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7638
7639static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7640static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7641static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7642 struct scroll_bar *));
7643static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7644 int, int, int));
7645
7646
7647/* Id of action hook installed for scroll bars. */
7648
7649static XtActionHookId action_hook_id;
7650
7651/* Lisp window being scrolled. Set when starting to interact with
7652 a toolkit scroll bar, reset to nil when ending the interaction. */
7653
7654static Lisp_Object window_being_scrolled;
7655
7656/* Last scroll bar part sent in xm_scroll_callback. */
7657
7658static int last_scroll_bar_part;
7659
ec18280f
SM
7660/* Whether this is an Xaw with arrow-scrollbars. This should imply
7661 that movements of 1/20 of the screen size are mapped to up/down. */
7662
7663static Boolean xaw3d_arrow_scroll;
7664
7665/* Whether the drag scrolling maintains the mouse at the top of the
7666 thumb. If not, resizing the thumb needs to be done more carefully
7667 to avoid jerkyness. */
7668
7669static Boolean xaw3d_pick_top;
7670
06a2c219
GM
7671
7672/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7673 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7674 the user ends an interaction with the scroll bar, and generates
7675 a `end-scroll' scroll_bar_click' event if so. */
7676
7677static void
7678xt_action_hook (widget, client_data, action_name, event, params,
7679 num_params)
7680 Widget widget;
7681 XtPointer client_data;
7682 String action_name;
7683 XEvent *event;
7684 String *params;
7685 Cardinal *num_params;
7686{
7687 int scroll_bar_p;
7688 char *end_action;
7689
7690#ifdef USE_MOTIF
7691 scroll_bar_p = XmIsScrollBar (widget);
7692 end_action = "Release";
ec18280f 7693#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7694 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7695 end_action = "EndScroll";
ec18280f 7696#endif /* USE_MOTIF */
06a2c219 7697
06a2c219
GM
7698 if (scroll_bar_p
7699 && strcmp (action_name, end_action) == 0
7700 && WINDOWP (window_being_scrolled))
7701 {
7702 struct window *w;
7703
7704 x_send_scroll_bar_event (window_being_scrolled,
7705 scroll_bar_end_scroll, 0, 0);
7706 w = XWINDOW (window_being_scrolled);
7707 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7708 window_being_scrolled = Qnil;
7709 last_scroll_bar_part = -1;
bffcfca9
GM
7710
7711 /* Xt timeouts no longer needed. */
7712 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7713 }
7714}
7715
07b3d16e
GM
7716/* A vector of windows used for communication between
7717 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7718
7719static struct window **scroll_bar_windows;
7720static int scroll_bar_windows_size;
7721
06a2c219
GM
7722
7723/* Send a client message with message type Xatom_Scrollbar for a
7724 scroll action to the frame of WINDOW. PART is a value identifying
7725 the part of the scroll bar that was clicked on. PORTION is the
7726 amount to scroll of a whole of WHOLE. */
7727
7728static void
7729x_send_scroll_bar_event (window, part, portion, whole)
7730 Lisp_Object window;
7731 int part, portion, whole;
7732{
7733 XEvent event;
7734 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7735 struct window *w = XWINDOW (window);
7736 struct frame *f = XFRAME (w->frame);
7737 int i;
06a2c219 7738
07b3d16e
GM
7739 BLOCK_INPUT;
7740
06a2c219
GM
7741 /* Construct a ClientMessage event to send to the frame. */
7742 ev->type = ClientMessage;
7743 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7744 ev->display = FRAME_X_DISPLAY (f);
7745 ev->window = FRAME_X_WINDOW (f);
7746 ev->format = 32;
07b3d16e
GM
7747
7748 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7749 not enough to store a pointer or Lisp_Object on a 64 bit system.
7750 So, store the window in scroll_bar_windows and pass the index
7751 into that array in the event. */
7752 for (i = 0; i < scroll_bar_windows_size; ++i)
7753 if (scroll_bar_windows[i] == NULL)
7754 break;
7755
7756 if (i == scroll_bar_windows_size)
7757 {
7758 int new_size = max (10, 2 * scroll_bar_windows_size);
7759 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7760 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7761
7762 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7763 nbytes);
7764 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7765 scroll_bar_windows_size = new_size;
7766 }
7767
7768 scroll_bar_windows[i] = w;
7769 ev->data.l[0] = (long) i;
06a2c219
GM
7770 ev->data.l[1] = (long) part;
7771 ev->data.l[2] = (long) 0;
7772 ev->data.l[3] = (long) portion;
7773 ev->data.l[4] = (long) whole;
7774
bffcfca9
GM
7775 /* Make Xt timeouts work while the scroll bar is active. */
7776 toolkit_scroll_bar_interaction = 1;
7777
06a2c219
GM
7778 /* Setting the event mask to zero means that the message will
7779 be sent to the client that created the window, and if that
7780 window no longer exists, no event will be sent. */
06a2c219
GM
7781 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7782 UNBLOCK_INPUT;
7783}
7784
7785
7786/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7787 in *IEVENT. */
7788
7789static void
7790x_scroll_bar_to_input_event (event, ievent)
7791 XEvent *event;
7792 struct input_event *ievent;
7793{
7794 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7795 Lisp_Object window;
7796 struct frame *f;
07b3d16e
GM
7797 struct window *w;
7798
7799 w = scroll_bar_windows[ev->data.l[0]];
7800 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 7801
07b3d16e
GM
7802 XSETWINDOW (window, w);
7803 f = XFRAME (w->frame);
06a2c219
GM
7804
7805 ievent->kind = scroll_bar_click;
7806 ievent->frame_or_window = window;
0f8aabe9 7807 ievent->arg = Qnil;
06a2c219
GM
7808 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7809 ievent->part = ev->data.l[1];
7810 ievent->code = ev->data.l[2];
7811 ievent->x = make_number ((int) ev->data.l[3]);
7812 ievent->y = make_number ((int) ev->data.l[4]);
7813 ievent->modifiers = 0;
7814}
7815
7816
7817#ifdef USE_MOTIF
7818
7819/* Minimum and maximum values used for Motif scroll bars. */
7820
7821#define XM_SB_MIN 1
7822#define XM_SB_MAX 10000000
7823#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7824
7825
7826/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7827 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7828 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7829
7830static void
7831xm_scroll_callback (widget, client_data, call_data)
7832 Widget widget;
7833 XtPointer client_data, call_data;
7834{
7835 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7836 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7837 double percent;
7838 int part = -1, whole = 0, portion = 0;
7839
7840 switch (cs->reason)
7841 {
7842 case XmCR_DECREMENT:
7843 bar->dragging = Qnil;
7844 part = scroll_bar_up_arrow;
7845 break;
7846
7847 case XmCR_INCREMENT:
7848 bar->dragging = Qnil;
7849 part = scroll_bar_down_arrow;
7850 break;
7851
7852 case XmCR_PAGE_DECREMENT:
7853 bar->dragging = Qnil;
7854 part = scroll_bar_above_handle;
7855 break;
7856
7857 case XmCR_PAGE_INCREMENT:
7858 bar->dragging = Qnil;
7859 part = scroll_bar_below_handle;
7860 break;
7861
7862 case XmCR_TO_TOP:
7863 bar->dragging = Qnil;
7864 part = scroll_bar_to_top;
7865 break;
7866
7867 case XmCR_TO_BOTTOM:
7868 bar->dragging = Qnil;
7869 part = scroll_bar_to_bottom;
7870 break;
7871
7872 case XmCR_DRAG:
7873 {
7874 int slider_size;
7875 int dragging_down_p = (INTEGERP (bar->dragging)
7876 && XINT (bar->dragging) <= cs->value);
7877
7878 /* Get the slider size. */
7879 BLOCK_INPUT;
7880 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7881 UNBLOCK_INPUT;
7882
7883 /* At the max position of the scroll bar, do a line-wise
7884 movement. Without doing anything, the LessTif scroll bar
7885 calls us with the same cs->value again and again. If we
7886 want to make sure that we can reach the end of the buffer,
7887 we have to do something.
7888
7889 Implementation note: setting bar->dragging always to
7890 cs->value gives a smoother movement at the max position.
7891 Setting it to nil when doing line-wise movement gives
7892 a better slider behavior. */
7893
7894 if (cs->value + slider_size == XM_SB_MAX
7895 || (dragging_down_p
7896 && last_scroll_bar_part == scroll_bar_down_arrow))
7897 {
7898 part = scroll_bar_down_arrow;
7899 bar->dragging = Qnil;
7900 }
7901 else
7902 {
7903 whole = XM_SB_RANGE;
7904 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7905 part = scroll_bar_handle;
7906 bar->dragging = make_number (cs->value);
7907 }
7908 }
7909 break;
7910
7911 case XmCR_VALUE_CHANGED:
7912 break;
7913 };
7914
7915 if (part >= 0)
7916 {
7917 window_being_scrolled = bar->window;
7918 last_scroll_bar_part = part;
7919 x_send_scroll_bar_event (bar->window, part, portion, whole);
7920 }
7921}
7922
7923
ec18280f 7924#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7925
7926
ec18280f 7927/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7928 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7929 scroll bar struct. CALL_DATA is a pointer to a float saying where
7930 the thumb is. */
7931
7932static void
ec18280f 7933xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7934 Widget widget;
7935 XtPointer client_data, call_data;
7936{
7937 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7938 float top = *(float *) call_data;
7939 float shown;
ec18280f
SM
7940 int whole, portion, height;
7941 int part;
06a2c219
GM
7942
7943 /* Get the size of the thumb, a value between 0 and 1. */
7944 BLOCK_INPUT;
ec18280f 7945 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7946 UNBLOCK_INPUT;
7947
7948 whole = 10000000;
7949 portion = shown < 1 ? top * whole : 0;
06a2c219 7950
ec18280f
SM
7951 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7952 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7953 the bottom, so we force the scrolling whenever we see that we're
7954 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7955 we try to ensure that we always stay two pixels away from the
7956 bottom). */
06a2c219
GM
7957 part = scroll_bar_down_arrow;
7958 else
7959 part = scroll_bar_handle;
7960
7961 window_being_scrolled = bar->window;
7962 bar->dragging = make_number (portion);
7963 last_scroll_bar_part = part;
7964 x_send_scroll_bar_event (bar->window, part, portion, whole);
7965}
7966
7967
ec18280f
SM
7968/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7969 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7970 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7971 the scroll bar. CALL_DATA is an integer specifying the action that
7972 has taken place. It's magnitude is in the range 0..height of the
7973 scroll bar. Negative values mean scroll towards buffer start.
7974 Values < height of scroll bar mean line-wise movement. */
7975
7976static void
ec18280f 7977xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7978 Widget widget;
7979 XtPointer client_data, call_data;
7980{
7981 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7982 int position = (int) call_data;
7983 Dimension height;
7984 int part;
7985
7986 /* Get the height of the scroll bar. */
7987 BLOCK_INPUT;
7988 XtVaGetValues (widget, XtNheight, &height, NULL);
7989 UNBLOCK_INPUT;
7990
ec18280f
SM
7991 if (abs (position) >= height)
7992 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7993
7994 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7995 it maps line-movement to call_data = max(5, height/20). */
7996 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7997 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7998 else
ec18280f 7999 part = scroll_bar_move_ratio;
06a2c219
GM
8000
8001 window_being_scrolled = bar->window;
8002 bar->dragging = Qnil;
8003 last_scroll_bar_part = part;
ec18280f 8004 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8005}
8006
8007
8008#endif /* not USE_MOTIF */
8009
8010
8011/* Create the widget for scroll bar BAR on frame F. Record the widget
8012 and X window of the scroll bar in BAR. */
8013
8014static void
8015x_create_toolkit_scroll_bar (f, bar)
8016 struct frame *f;
8017 struct scroll_bar *bar;
8018{
8019 Window xwindow;
8020 Widget widget;
8021 Arg av[20];
8022 int ac = 0;
8023 char *scroll_bar_name = "verticalScrollBar";
8024 unsigned long pixel;
8025
8026 BLOCK_INPUT;
8027
8028#ifdef USE_MOTIF
8029 /* LessTif 0.85, problems:
8030
8031 1. When the mouse if over the scroll bar, the scroll bar will
8032 get keyboard events. I didn't find a way to turn this off.
8033
8034 2. Do we have to explicitly set the cursor to get an arrow
8035 cursor (see below)? */
8036
8037 /* Set resources. Create the widget. */
8038 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8039 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8040 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8041 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8042 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8043 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8044 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8045
8046 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8047 if (pixel != -1)
8048 {
8049 XtSetArg (av[ac], XmNforeground, pixel);
8050 ++ac;
8051 }
8052
8053 pixel = f->output_data.x->scroll_bar_background_pixel;
8054 if (pixel != -1)
8055 {
8056 XtSetArg (av[ac], XmNbackground, pixel);
8057 ++ac;
8058 }
8059
8060 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8061 scroll_bar_name, av, ac);
8062
8063 /* Add one callback for everything that can happen. */
8064 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8065 (XtPointer) bar);
8066 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8067 (XtPointer) bar);
8068 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8069 (XtPointer) bar);
8070 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8071 (XtPointer) bar);
8072 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8073 (XtPointer) bar);
8074 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8075 (XtPointer) bar);
8076 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8077 (XtPointer) bar);
8078
8079 /* Realize the widget. Only after that is the X window created. */
8080 XtRealizeWidget (widget);
8081
8082 /* Set the cursor to an arrow. I didn't find a resource to do that.
8083 And I'm wondering why it hasn't an arrow cursor by default. */
8084 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8085 f->output_data.x->nontext_cursor);
8086
ec18280f 8087#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8088
8089 /* Set resources. Create the widget. The background of the
8090 Xaw3d scroll bar widget is a little bit light for my taste.
8091 We don't alter it here to let users change it according
8092 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8093 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8094 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8095 /* For smoother scrolling with Xaw3d -sm */
8096 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8097 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8098
8099 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8100 if (pixel != -1)
8101 {
8102 XtSetArg (av[ac], XtNforeground, pixel);
8103 ++ac;
8104 }
8105
8106 pixel = f->output_data.x->scroll_bar_background_pixel;
8107 if (pixel != -1)
8108 {
8109 XtSetArg (av[ac], XtNbackground, pixel);
8110 ++ac;
8111 }
8112
8113 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8114 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8115
8116 {
8117 char *initial = "";
8118 char *val = initial;
8119 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8120 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8121 if (val == initial)
8122 { /* ARROW_SCROLL */
8123 xaw3d_arrow_scroll = True;
8124 /* Isn't that just a personal preference ? -sm */
8125 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8126 }
8127 }
06a2c219
GM
8128
8129 /* Define callbacks. */
ec18280f
SM
8130 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8131 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8132 (XtPointer) bar);
8133
8134 /* Realize the widget. Only after that is the X window created. */
8135 XtRealizeWidget (widget);
8136
ec18280f 8137#endif /* !USE_MOTIF */
06a2c219
GM
8138
8139 /* Install an action hook that let's us detect when the user
8140 finishes interacting with a scroll bar. */
8141 if (action_hook_id == 0)
8142 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8143
8144 /* Remember X window and widget in the scroll bar vector. */
8145 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8146 xwindow = XtWindow (widget);
8147 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8148
8149 UNBLOCK_INPUT;
8150}
8151
8152
8153/* Set the thumb size and position of scroll bar BAR. We are currently
8154 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8155
8156static void
8157x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8158 struct scroll_bar *bar;
8159 int portion, position, whole;
f451eb13 8160{
e83dc917
GM
8161 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8162 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8163 float top, shown;
f451eb13 8164
06a2c219
GM
8165 if (whole == 0)
8166 top = 0, shown = 1;
8167 else
f451eb13 8168 {
06a2c219
GM
8169 top = (float) position / whole;
8170 shown = (float) portion / whole;
8171 }
f451eb13 8172
06a2c219 8173 BLOCK_INPUT;
f451eb13 8174
06a2c219
GM
8175#ifdef USE_MOTIF
8176 {
8177 int size, value;
8178 Boolean arrow1_selected, arrow2_selected;
8179 unsigned char flags;
8180 XmScrollBarWidget sb;
8181
ec18280f 8182 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8183 is the scroll bar's maximum and MIN is the scroll bar's minimum
8184 value. */
8185 size = shown * XM_SB_RANGE;
8186 size = min (size, XM_SB_RANGE);
8187 size = max (size, 1);
8188
8189 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8190 value = top * XM_SB_RANGE;
8191 value = min (value, XM_SB_MAX - size);
8192 value = max (value, XM_SB_MIN);
8193
8194 /* LessTif: Calling XmScrollBarSetValues after an increment or
8195 decrement turns off auto-repeat LessTif-internally. This can
8196 be seen in ScrollBar.c which resets Arrow1Selected and
8197 Arrow2Selected. It also sets internal flags so that LessTif
8198 believes the mouse is in the slider. We either have to change
8199 our code, or work around that by accessing private data. */
8200
8201 sb = (XmScrollBarWidget) widget;
8202 arrow1_selected = sb->scrollBar.arrow1_selected;
8203 arrow2_selected = sb->scrollBar.arrow2_selected;
8204 flags = sb->scrollBar.flags;
8205
8206 if (NILP (bar->dragging))
8207 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8208 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8209 /* This has the negative side effect that the slider value is
ec18280f 8210 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8211 page-wise movement. */
8212 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8213 else
8214 {
8215 /* If currently dragging, only update the slider size.
8216 This reduces flicker effects. */
8217 int old_value, old_size, increment, page_increment;
8218
8219 XmScrollBarGetValues (widget, &old_value, &old_size,
8220 &increment, &page_increment);
8221 XmScrollBarSetValues (widget, old_value,
8222 min (size, XM_SB_RANGE - old_value),
8223 0, 0, False);
8224 }
8225
8226 sb->scrollBar.arrow1_selected = arrow1_selected;
8227 sb->scrollBar.arrow2_selected = arrow2_selected;
8228 sb->scrollBar.flags = flags;
8229 }
ec18280f 8230#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8231 {
ec18280f
SM
8232 float old_top, old_shown;
8233 Dimension height;
8234 XtVaGetValues (widget,
8235 XtNtopOfThumb, &old_top,
8236 XtNshown, &old_shown,
8237 XtNheight, &height,
8238 NULL);
8239
8240 /* Massage the top+shown values. */
8241 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8242 top = max (0, min (1, top));
8243 else
8244 top = old_top;
8245 /* Keep two pixels available for moving the thumb down. */
8246 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8247
8248 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8249 check that your system's configuration file contains a define
8250 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8251 if (top != old_top || shown != old_shown)
eb393530 8252 {
ec18280f 8253 if (NILP (bar->dragging))
eb393530 8254 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8255 else
8256 {
ec18280f
SM
8257#ifdef HAVE_XAW3D
8258 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8259 int scroll_mode = 0;
ec18280f
SM
8260
8261 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8262 if (xaw3d_arrow_scroll)
8263 {
8264 /* Xaw3d stupidly ignores resize requests while dragging
8265 so we have to make it believe it's not in dragging mode. */
8266 scroll_mode = sb->scrollbar.scroll_mode;
8267 if (scroll_mode == 2)
8268 sb->scrollbar.scroll_mode = 0;
8269 }
8270#endif
8271 /* Try to make the scrolling a tad smoother. */
8272 if (!xaw3d_pick_top)
8273 shown = min (shown, old_shown);
8274
8275 XawScrollbarSetThumb (widget, top, shown);
8276
8277#ifdef HAVE_XAW3D
8278 if (xaw3d_arrow_scroll && scroll_mode == 2)
8279 sb->scrollbar.scroll_mode = scroll_mode;
8280#endif
06a2c219 8281 }
06a2c219
GM
8282 }
8283 }
ec18280f 8284#endif /* !USE_MOTIF */
06a2c219
GM
8285
8286 UNBLOCK_INPUT;
f451eb13
JB
8287}
8288
06a2c219
GM
8289#endif /* USE_TOOLKIT_SCROLL_BARS */
8290
8291
8292\f
8293/************************************************************************
8294 Scroll bars, general
8295 ************************************************************************/
8296
8297/* Create a scroll bar and return the scroll bar vector for it. W is
8298 the Emacs window on which to create the scroll bar. TOP, LEFT,
8299 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8300 scroll bar. */
8301
ab648270 8302static struct scroll_bar *
06a2c219
GM
8303x_scroll_bar_create (w, top, left, width, height)
8304 struct window *w;
f451eb13
JB
8305 int top, left, width, height;
8306{
06a2c219 8307 struct frame *f = XFRAME (w->frame);
334208b7
RS
8308 struct scroll_bar *bar
8309 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8310
8311 BLOCK_INPUT;
8312
eccc05db 8313#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8314 x_create_toolkit_scroll_bar (f, bar);
8315#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8316 {
8317 XSetWindowAttributes a;
8318 unsigned long mask;
5c187dee 8319 Window window;
06a2c219
GM
8320
8321 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8322 if (a.background_pixel == -1)
8323 a.background_pixel = f->output_data.x->background_pixel;
8324
12ba150f 8325 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8326 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8327 | ExposureMask);
7a13e894 8328 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8329
dbc4e1c1 8330 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8331
06a2c219
GM
8332 /* Clear the area of W that will serve as a scroll bar. This is
8333 for the case that a window has been split horizontally. In
8334 this case, no clear_frame is generated to reduce flickering. */
8335 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8336 left, top, width,
8337 window_box_height (w), False);
8338
8339 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8340 /* Position and size of scroll bar. */
8341 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8342 top,
8343 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8344 height,
8345 /* Border width, depth, class, and visual. */
8346 0,
8347 CopyFromParent,
8348 CopyFromParent,
8349 CopyFromParent,
8350 /* Attributes. */
8351 mask, &a);
8352 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8353 }
06a2c219 8354#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8355
06a2c219 8356 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8357 XSETINT (bar->top, top);
8358 XSETINT (bar->left, left);
8359 XSETINT (bar->width, width);
8360 XSETINT (bar->height, height);
8361 XSETINT (bar->start, 0);
8362 XSETINT (bar->end, 0);
12ba150f 8363 bar->dragging = Qnil;
f451eb13
JB
8364
8365 /* Add bar to its frame's list of scroll bars. */
334208b7 8366 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8367 bar->prev = Qnil;
334208b7 8368 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8369 if (!NILP (bar->next))
e0c1aef2 8370 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8371
06a2c219 8372 /* Map the window/widget. */
eccc05db 8373#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8374 {
8375 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8376 XtConfigureWidget (scroll_bar,
06a2c219
GM
8377 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8378 top,
8379 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8380 height, 0);
e83dc917
GM
8381 XtMapWidget (scroll_bar);
8382 }
06a2c219 8383#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8384 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8385#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8386
8387 UNBLOCK_INPUT;
12ba150f 8388 return bar;
f451eb13
JB
8389}
8390
06a2c219 8391
12ba150f 8392/* Draw BAR's handle in the proper position.
06a2c219 8393
12ba150f
JB
8394 If the handle is already drawn from START to END, don't bother
8395 redrawing it, unless REBUILD is non-zero; in that case, always
8396 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8397 events.)
12ba150f
JB
8398
8399 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8400 fit inside its rectangle, but if the user is dragging the scroll
8401 bar handle, we want to let them drag it down all the way, so that
8402 the bar's top is as far down as it goes; otherwise, there's no way
8403 to move to the very end of the buffer. */
8404
5c187dee
GM
8405#ifndef USE_TOOLKIT_SCROLL_BARS
8406
f451eb13 8407static void
ab648270
JB
8408x_scroll_bar_set_handle (bar, start, end, rebuild)
8409 struct scroll_bar *bar;
f451eb13 8410 int start, end;
12ba150f 8411 int rebuild;
f451eb13 8412{
12ba150f 8413 int dragging = ! NILP (bar->dragging);
ab648270 8414 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8415 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8416 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8417
8418 /* If the display is already accurate, do nothing. */
8419 if (! rebuild
8420 && start == XINT (bar->start)
8421 && end == XINT (bar->end))
8422 return;
8423
f451eb13
JB
8424 BLOCK_INPUT;
8425
8426 {
d9cdbb3d
RS
8427 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8428 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8429 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8430
8431 /* Make sure the values are reasonable, and try to preserve
8432 the distance between start and end. */
12ba150f
JB
8433 {
8434 int length = end - start;
8435
8436 if (start < 0)
8437 start = 0;
8438 else if (start > top_range)
8439 start = top_range;
8440 end = start + length;
8441
8442 if (end < start)
8443 end = start;
8444 else if (end > top_range && ! dragging)
8445 end = top_range;
8446 }
f451eb13 8447
ab648270 8448 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8449 XSETINT (bar->start, start);
8450 XSETINT (bar->end, end);
f451eb13 8451
12ba150f
JB
8452 /* Clip the end position, just for display. */
8453 if (end > top_range)
8454 end = top_range;
f451eb13 8455
ab648270 8456 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8457 below top positions, to make sure the handle is always at least
8458 that many pixels tall. */
ab648270 8459 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8460
12ba150f
JB
8461 /* Draw the empty space above the handle. Note that we can't clear
8462 zero-height areas; that means "clear to end of window." */
8463 if (0 < start)
334208b7 8464 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8465
12ba150f 8466 /* x, y, width, height, and exposures. */
ab648270
JB
8467 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8468 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8469 inside_width, start,
8470 False);
f451eb13 8471
06a2c219
GM
8472 /* Change to proper foreground color if one is specified. */
8473 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8474 XSetForeground (FRAME_X_DISPLAY (f), gc,
8475 f->output_data.x->scroll_bar_foreground_pixel);
8476
12ba150f 8477 /* Draw the handle itself. */
334208b7 8478 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8479
12ba150f 8480 /* x, y, width, height */
ab648270
JB
8481 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8482 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8483 inside_width, end - start);
f451eb13 8484
06a2c219
GM
8485 /* Restore the foreground color of the GC if we changed it above. */
8486 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8487 XSetForeground (FRAME_X_DISPLAY (f), gc,
8488 f->output_data.x->foreground_pixel);
f451eb13 8489
12ba150f
JB
8490 /* Draw the empty space below the handle. Note that we can't
8491 clear zero-height areas; that means "clear to end of window." */
8492 if (end < inside_height)
334208b7 8493 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8494
12ba150f 8495 /* x, y, width, height, and exposures. */
ab648270
JB
8496 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8497 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8498 inside_width, inside_height - end,
8499 False);
f451eb13 8500
f451eb13
JB
8501 }
8502
f451eb13
JB
8503 UNBLOCK_INPUT;
8504}
8505
5c187dee 8506#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8507
06a2c219
GM
8508/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8509 nil. */
58769bee 8510
12ba150f 8511static void
ab648270
JB
8512x_scroll_bar_remove (bar)
8513 struct scroll_bar *bar;
12ba150f 8514{
e83dc917 8515 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8516 BLOCK_INPUT;
8517
eccc05db 8518#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8519 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8520#else
8521 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8522#endif
06a2c219 8523
ab648270
JB
8524 /* Disassociate this scroll bar from its window. */
8525 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8526
8527 UNBLOCK_INPUT;
8528}
8529
06a2c219 8530
12ba150f
JB
8531/* Set the handle of the vertical scroll bar for WINDOW to indicate
8532 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8533 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8534 create one. */
06a2c219 8535
12ba150f 8536static void
06a2c219
GM
8537XTset_vertical_scroll_bar (w, portion, whole, position)
8538 struct window *w;
f451eb13
JB
8539 int portion, whole, position;
8540{
06a2c219 8541 struct frame *f = XFRAME (w->frame);
ab648270 8542 struct scroll_bar *bar;
3c6ede7b 8543 int top, height, left, sb_left, width, sb_width;
06a2c219 8544 int window_x, window_y, window_width, window_height;
06a2c219 8545
3c6ede7b 8546 /* Get window dimensions. */
06a2c219 8547 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8548 top = window_y;
8549 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8550 height = window_height;
06a2c219 8551
3c6ede7b 8552 /* Compute the left edge of the scroll bar area. */
06a2c219 8553 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8554 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8555 else
8556 left = XFASTINT (w->left);
8557 left *= CANON_X_UNIT (f);
8558 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8559
8560 /* Compute the width of the scroll bar which might be less than
8561 the width of the area reserved for the scroll bar. */
8562 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8563 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8564 else
3c6ede7b 8565 sb_width = width;
12ba150f 8566
3c6ede7b
GM
8567 /* Compute the left edge of the scroll bar. */
8568#ifdef USE_TOOLKIT_SCROLL_BARS
8569 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8570 sb_left = left + width - sb_width - (width - sb_width) / 2;
8571 else
8572 sb_left = left + (width - sb_width) / 2;
8573#else
8574 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8575 sb_left = left + width - sb_width;
8576 else
8577 sb_left = left;
8578#endif
8579
ab648270 8580 /* Does the scroll bar exist yet? */
06a2c219 8581 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8582 {
80c32bcc 8583 BLOCK_INPUT;
3c6ede7b
GM
8584 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8585 left, top, width, height, False);
80c32bcc 8586 UNBLOCK_INPUT;
3c6ede7b
GM
8587 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8588 }
f451eb13 8589 else
12ba150f
JB
8590 {
8591 /* It may just need to be moved and resized. */
06a2c219
GM
8592 unsigned int mask = 0;
8593
8594 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8595
8596 BLOCK_INPUT;
8597
3c6ede7b 8598 if (sb_left != XINT (bar->left))
06a2c219 8599 mask |= CWX;
3c6ede7b 8600 if (top != XINT (bar->top))
06a2c219 8601 mask |= CWY;
3c6ede7b 8602 if (sb_width != XINT (bar->width))
06a2c219 8603 mask |= CWWidth;
3c6ede7b 8604 if (height != XINT (bar->height))
06a2c219
GM
8605 mask |= CWHeight;
8606
8607#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8608
8609 /* Since toolkit scroll bars are smaller than the space reserved
8610 for them on the frame, we have to clear "under" them. */
8611 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8612 left, top, width, height, False);
06a2c219
GM
8613
8614 /* Move/size the scroll bar widget. */
8615 if (mask)
e83dc917 8616 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8617 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8618 top,
8619 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8620 height, 0);
06a2c219
GM
8621
8622#else /* not USE_TOOLKIT_SCROLL_BARS */
8623
e1f6572f
RS
8624 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8625 {
8626 /* Clear areas not covered by the scroll bar. This makes sure a
8627 previous mode line display is cleared after C-x 2 C-x 1, for
8628 example. Non-toolkit scroll bars are as wide as the area
8629 reserved for scroll bars - trim at both sides. */
8630 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8631 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8632 height, False);
8633 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8634 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8635 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8636 height, False);
8637 }
06a2c219
GM
8638
8639 /* Move/size the scroll bar window. */
8640 if (mask)
8641 {
8642 XWindowChanges wc;
8643
3c6ede7b
GM
8644 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8645 wc.y = top;
8646 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8647 wc.height = height;
06a2c219
GM
8648 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8649 mask, &wc);
8650 }
8651
8652#endif /* not USE_TOOLKIT_SCROLL_BARS */
8653
8654 /* Remember new settings. */
3c6ede7b
GM
8655 XSETINT (bar->left, sb_left);
8656 XSETINT (bar->top, top);
8657 XSETINT (bar->width, sb_width);
8658 XSETINT (bar->height, height);
06a2c219
GM
8659
8660 UNBLOCK_INPUT;
12ba150f 8661 }
f451eb13 8662
eccc05db 8663#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8664 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8665#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8666 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8667 dragged. */
12ba150f 8668 if (NILP (bar->dragging))
f451eb13 8669 {
92857db0 8670 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8671
12ba150f 8672 if (whole == 0)
ab648270 8673 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8674 else
8675 {
43f868f5
JB
8676 int start = ((double) position * top_range) / whole;
8677 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8678 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8679 }
f451eb13 8680 }
06a2c219 8681#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8682
06a2c219 8683 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8684}
8685
12ba150f 8686
f451eb13 8687/* The following three hooks are used when we're doing a thorough
ab648270 8688 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8689 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8690 away is a real pain - "Can you say set-window-configuration, boys
8691 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8692 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8693 from the fiery pit when we actually redisplay its window. */
f451eb13 8694
ab648270
JB
8695/* Arrange for all scroll bars on FRAME to be removed at the next call
8696 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8697 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8698
58769bee 8699static void
ab648270 8700XTcondemn_scroll_bars (frame)
f451eb13
JB
8701 FRAME_PTR frame;
8702{
f9e24cb9
RS
8703 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8704 while (! NILP (FRAME_SCROLL_BARS (frame)))
8705 {
8706 Lisp_Object bar;
8707 bar = FRAME_SCROLL_BARS (frame);
8708 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8709 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8710 XSCROLL_BAR (bar)->prev = Qnil;
8711 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8712 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8713 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8714 }
f451eb13
JB
8715}
8716
fa2dfc30 8717
06a2c219 8718/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8719 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8720
f451eb13 8721static void
ab648270 8722XTredeem_scroll_bar (window)
12ba150f 8723 struct window *window;
f451eb13 8724{
ab648270 8725 struct scroll_bar *bar;
fa2dfc30 8726 struct frame *f;
12ba150f 8727
ab648270
JB
8728 /* We can't redeem this window's scroll bar if it doesn't have one. */
8729 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8730 abort ();
8731
ab648270 8732 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8733
8734 /* Unlink it from the condemned list. */
fa2dfc30
GM
8735 f = XFRAME (WINDOW_FRAME (window));
8736 if (NILP (bar->prev))
8737 {
8738 /* If the prev pointer is nil, it must be the first in one of
8739 the lists. */
8740 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8741 /* It's not condemned. Everything's fine. */
8742 return;
8743 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8744 window->vertical_scroll_bar))
8745 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8746 else
8747 /* If its prev pointer is nil, it must be at the front of
8748 one or the other! */
8749 abort ();
8750 }
8751 else
8752 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8753
fa2dfc30
GM
8754 if (! NILP (bar->next))
8755 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8756
fa2dfc30
GM
8757 bar->next = FRAME_SCROLL_BARS (f);
8758 bar->prev = Qnil;
8759 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8760 if (! NILP (bar->next))
8761 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
8762}
8763
ab648270
JB
8764/* Remove all scroll bars on FRAME that haven't been saved since the
8765 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8766
f451eb13 8767static void
ab648270 8768XTjudge_scroll_bars (f)
12ba150f 8769 FRAME_PTR f;
f451eb13 8770{
12ba150f 8771 Lisp_Object bar, next;
f451eb13 8772
ab648270 8773 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8774
8775 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8776 more events on the hapless scroll bars. */
8777 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8778
8779 for (; ! NILP (bar); bar = next)
f451eb13 8780 {
ab648270 8781 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8782
ab648270 8783 x_scroll_bar_remove (b);
12ba150f
JB
8784
8785 next = b->next;
8786 b->next = b->prev = Qnil;
f451eb13 8787 }
12ba150f 8788
ab648270 8789 /* Now there should be no references to the condemned scroll bars,
12ba150f 8790 and they should get garbage-collected. */
f451eb13
JB
8791}
8792
8793
06a2c219
GM
8794/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8795 is a no-op when using toolkit scroll bars.
ab648270
JB
8796
8797 This may be called from a signal handler, so we have to ignore GC
8798 mark bits. */
06a2c219 8799
f451eb13 8800static void
ab648270
JB
8801x_scroll_bar_expose (bar, event)
8802 struct scroll_bar *bar;
f451eb13
JB
8803 XEvent *event;
8804{
06a2c219
GM
8805#ifndef USE_TOOLKIT_SCROLL_BARS
8806
ab648270 8807 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8808 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8809 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8810 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8811
f451eb13
JB
8812 BLOCK_INPUT;
8813
ab648270 8814 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8815
06a2c219 8816 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8817 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8818
8819 /* x, y, width, height */
d9cdbb3d 8820 0, 0,
3cbd2e0b 8821 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8822 XINT (bar->height) - 1);
8823
f451eb13 8824 UNBLOCK_INPUT;
06a2c219
GM
8825
8826#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8827}
8828
ab648270
JB
8829/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8830 is set to something other than no_event, it is enqueued.
8831
8832 This may be called from a signal handler, so we have to ignore GC
8833 mark bits. */
06a2c219 8834
5c187dee
GM
8835#ifndef USE_TOOLKIT_SCROLL_BARS
8836
f451eb13 8837static void
ab648270
JB
8838x_scroll_bar_handle_click (bar, event, emacs_event)
8839 struct scroll_bar *bar;
f451eb13
JB
8840 XEvent *event;
8841 struct input_event *emacs_event;
8842{
0299d313 8843 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8844 abort ();
8845
ab648270 8846 emacs_event->kind = scroll_bar_click;
69388238 8847 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8848 emacs_event->modifiers
8849 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8850 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8851 event->xbutton.state)
8852 | (event->type == ButtonRelease
8853 ? up_modifier
8854 : down_modifier));
12ba150f 8855 emacs_event->frame_or_window = bar->window;
0f8aabe9 8856 emacs_event->arg = Qnil;
f451eb13 8857 emacs_event->timestamp = event->xbutton.time;
12ba150f 8858 {
06a2c219 8859#if 0
d9cdbb3d 8860 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8861 int internal_height
d9cdbb3d 8862 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8863#endif
0299d313 8864 int top_range
d9cdbb3d 8865 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8866 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8867
8868 if (y < 0) y = 0;
8869 if (y > top_range) y = top_range;
8870
8871 if (y < XINT (bar->start))
ab648270
JB
8872 emacs_event->part = scroll_bar_above_handle;
8873 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8874 emacs_event->part = scroll_bar_handle;
12ba150f 8875 else
ab648270 8876 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8877
8878 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8879 they want to drag it. Lisp code needs to be able to decide
8880 whether or not we're dragging. */
929787e1 8881#if 0
12ba150f
JB
8882 /* If the user has just clicked on the handle, record where they're
8883 holding it. */
8884 if (event->type == ButtonPress
ab648270 8885 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8886 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8887#endif
12ba150f
JB
8888
8889 /* If the user has released the handle, set it to its final position. */
8890 if (event->type == ButtonRelease
8891 && ! NILP (bar->dragging))
8892 {
8893 int new_start = y - XINT (bar->dragging);
8894 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8895
ab648270 8896 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8897 bar->dragging = Qnil;
8898 }
f451eb13 8899
5116f055
JB
8900 /* Same deal here as the other #if 0. */
8901#if 0
58769bee 8902 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8903 the handle. */
ab648270 8904 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8905 emacs_event->x = bar->start;
8906 else
e0c1aef2 8907 XSETINT (emacs_event->x, y);
5116f055 8908#else
e0c1aef2 8909 XSETINT (emacs_event->x, y);
5116f055 8910#endif
f451eb13 8911
e0c1aef2 8912 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8913 }
8914}
f451eb13 8915
ab648270
JB
8916/* Handle some mouse motion while someone is dragging the scroll bar.
8917
8918 This may be called from a signal handler, so we have to ignore GC
8919 mark bits. */
06a2c219 8920
f451eb13 8921static void
ab648270
JB
8922x_scroll_bar_note_movement (bar, event)
8923 struct scroll_bar *bar;
f451eb13
JB
8924 XEvent *event;
8925{
39d8bb4d
KH
8926 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8927
f451eb13
JB
8928 last_mouse_movement_time = event->xmotion.time;
8929
39d8bb4d 8930 f->mouse_moved = 1;
e0c1aef2 8931 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8932
8933 /* If we're dragging the bar, display it. */
ab648270 8934 if (! GC_NILP (bar->dragging))
f451eb13
JB
8935 {
8936 /* Where should the handle be now? */
12ba150f 8937 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8938
12ba150f 8939 if (new_start != XINT (bar->start))
f451eb13 8940 {
12ba150f 8941 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8942
ab648270 8943 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8944 }
8945 }
f451eb13
JB
8946}
8947
5c187dee
GM
8948#endif /* !USE_TOOLKIT_SCROLL_BARS */
8949
12ba150f 8950/* Return information to the user about the current position of the mouse
ab648270 8951 on the scroll bar. */
06a2c219 8952
12ba150f 8953static void
334208b7
RS
8954x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8955 FRAME_PTR *fp;
12ba150f 8956 Lisp_Object *bar_window;
ab648270 8957 enum scroll_bar_part *part;
12ba150f
JB
8958 Lisp_Object *x, *y;
8959 unsigned long *time;
8960{
ab648270 8961 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8962 Window w = SCROLL_BAR_X_WINDOW (bar);
8963 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8964 int win_x, win_y;
559cb2fb
JB
8965 Window dummy_window;
8966 int dummy_coord;
8967 unsigned int dummy_mask;
12ba150f 8968
cf7cb199
JB
8969 BLOCK_INPUT;
8970
ab648270 8971 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8972 report that. */
334208b7 8973 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8974
559cb2fb
JB
8975 /* Root, child, root x and root y. */
8976 &dummy_window, &dummy_window,
8977 &dummy_coord, &dummy_coord,
12ba150f 8978
559cb2fb
JB
8979 /* Position relative to scroll bar. */
8980 &win_x, &win_y,
12ba150f 8981
559cb2fb
JB
8982 /* Mouse buttons and modifier keys. */
8983 &dummy_mask))
7a13e894 8984 ;
559cb2fb
JB
8985 else
8986 {
06a2c219 8987#if 0
559cb2fb 8988 int inside_height
d9cdbb3d 8989 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8990#endif
559cb2fb 8991 int top_range
d9cdbb3d 8992 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8993
8994 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8995
8996 if (! NILP (bar->dragging))
8997 win_y -= XINT (bar->dragging);
8998
8999 if (win_y < 0)
9000 win_y = 0;
9001 if (win_y > top_range)
9002 win_y = top_range;
9003
334208b7 9004 *fp = f;
7a13e894 9005 *bar_window = bar->window;
559cb2fb
JB
9006
9007 if (! NILP (bar->dragging))
9008 *part = scroll_bar_handle;
9009 else if (win_y < XINT (bar->start))
9010 *part = scroll_bar_above_handle;
9011 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9012 *part = scroll_bar_handle;
9013 else
9014 *part = scroll_bar_below_handle;
12ba150f 9015
e0c1aef2
KH
9016 XSETINT (*x, win_y);
9017 XSETINT (*y, top_range);
12ba150f 9018
39d8bb4d 9019 f->mouse_moved = 0;
559cb2fb
JB
9020 last_mouse_scroll_bar = Qnil;
9021 }
12ba150f 9022
559cb2fb 9023 *time = last_mouse_movement_time;
cf7cb199 9024
cf7cb199 9025 UNBLOCK_INPUT;
12ba150f
JB
9026}
9027
f451eb13 9028
dbc4e1c1 9029/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9030 background colors, and the scroll bars may need to be redrawn.
9031 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9032 redraw them. */
9033
dfcf069d 9034void
ab648270 9035x_scroll_bar_clear (f)
dbc4e1c1
JB
9036 FRAME_PTR f;
9037{
06a2c219 9038#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9039 Lisp_Object bar;
9040
b80c363e
RS
9041 /* We can have scroll bars even if this is 0,
9042 if we just turned off scroll bar mode.
9043 But in that case we should not clear them. */
9044 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9045 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9046 bar = XSCROLL_BAR (bar)->next)
9047 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9048 0, 0, 0, 0, True);
06a2c219 9049#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9050}
9051
06a2c219 9052/* This processes Expose events from the menu-bar specific X event
19126e11 9053 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9054 when handling menu-bar or pop-up items. */
3afe33e7 9055
06a2c219 9056int
3afe33e7
RS
9057process_expose_from_menu (event)
9058 XEvent event;
9059{
9060 FRAME_PTR f;
19126e11 9061 struct x_display_info *dpyinfo;
06a2c219 9062 int frame_exposed_p = 0;
3afe33e7 9063
f94397b5
KH
9064 BLOCK_INPUT;
9065
19126e11
KH
9066 dpyinfo = x_display_info_for_display (event.xexpose.display);
9067 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9068 if (f)
9069 {
9070 if (f->async_visible == 0)
9071 {
9072 f->async_visible = 1;
9073 f->async_iconified = 0;
06c488fd 9074 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9075 SET_FRAME_GARBAGED (f);
9076 }
9077 else
9078 {
06a2c219
GM
9079 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9080 event.xexpose.x, event.xexpose.y,
9081 event.xexpose.width, event.xexpose.height);
9082 frame_exposed_p = 1;
3afe33e7
RS
9083 }
9084 }
9085 else
9086 {
9087 struct scroll_bar *bar
9088 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9089
3afe33e7
RS
9090 if (bar)
9091 x_scroll_bar_expose (bar, &event);
9092 }
f94397b5
KH
9093
9094 UNBLOCK_INPUT;
06a2c219 9095 return frame_exposed_p;
3afe33e7 9096}
09756a85
RS
9097\f
9098/* Define a queue to save up SelectionRequest events for later handling. */
9099
9100struct selection_event_queue
9101 {
9102 XEvent event;
9103 struct selection_event_queue *next;
9104 };
9105
9106static struct selection_event_queue *queue;
9107
9108/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9109
09756a85
RS
9110static int x_queue_selection_requests;
9111
9112/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9113
09756a85 9114static void
334208b7
RS
9115x_queue_event (f, event)
9116 FRAME_PTR f;
09756a85
RS
9117 XEvent *event;
9118{
9119 struct selection_event_queue *queue_tmp
06a2c219 9120 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9121
58769bee 9122 if (queue_tmp != NULL)
09756a85
RS
9123 {
9124 queue_tmp->event = *event;
9125 queue_tmp->next = queue;
9126 queue = queue_tmp;
9127 }
9128}
9129
9130/* Take all the queued events and put them back
9131 so that they get processed afresh. */
9132
9133static void
db3906fd
RS
9134x_unqueue_events (display)
9135 Display *display;
09756a85 9136{
58769bee 9137 while (queue != NULL)
09756a85
RS
9138 {
9139 struct selection_event_queue *queue_tmp = queue;
db3906fd 9140 XPutBackEvent (display, &queue_tmp->event);
09756a85 9141 queue = queue_tmp->next;
06a2c219 9142 xfree ((char *)queue_tmp);
09756a85
RS
9143 }
9144}
9145
9146/* Start queuing SelectionRequest events. */
9147
9148void
db3906fd
RS
9149x_start_queuing_selection_requests (display)
9150 Display *display;
09756a85
RS
9151{
9152 x_queue_selection_requests++;
9153}
9154
9155/* Stop queuing SelectionRequest events. */
9156
9157void
db3906fd
RS
9158x_stop_queuing_selection_requests (display)
9159 Display *display;
09756a85
RS
9160{
9161 x_queue_selection_requests--;
db3906fd 9162 x_unqueue_events (display);
09756a85 9163}
f451eb13
JB
9164\f
9165/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9166
06a2c219 9167/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9168 but we have to put it out here, since static variables within functions
9169 sometimes don't work. */
06a2c219 9170
dc6f92b8
JB
9171static Time enter_timestamp;
9172
11edeb03 9173/* This holds the state XLookupString needs to implement dead keys
58769bee 9174 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9175 says that a portable program can't use this, but Stephen Gildea assures
9176 me that letting the compiler initialize it to zeros will work okay.
9177
9178 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9179 given for enter_time stamp, above. */
9180
11edeb03
JB
9181static XComposeStatus compose_status;
9182
10e6549c
RS
9183/* Record the last 100 characters stored
9184 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9185
2224b905
RS
9186static int temp_index;
9187static short temp_buffer[100];
10e6549c 9188
7a13e894
RS
9189/* Set this to nonzero to fake an "X I/O error"
9190 on a particular display. */
06a2c219 9191
7a13e894
RS
9192struct x_display_info *XTread_socket_fake_io_error;
9193
2224b905
RS
9194/* When we find no input here, we occasionally do a no-op command
9195 to verify that the X server is still running and we can still talk with it.
9196 We try all the open displays, one by one.
9197 This variable is used for cycling thru the displays. */
06a2c219 9198
2224b905
RS
9199static struct x_display_info *next_noop_dpyinfo;
9200
06a2c219
GM
9201#define SET_SAVED_MENU_EVENT(size) \
9202 do \
9203 { \
9204 if (f->output_data.x->saved_menu_event == 0) \
9205 f->output_data.x->saved_menu_event \
9206 = (XEvent *) xmalloc (sizeof (XEvent)); \
9207 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9208 if (numchars >= 1) \
9209 { \
9210 bufp->kind = menu_bar_activate_event; \
9211 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9212 bufp->arg = Qnil; \
06a2c219
GM
9213 bufp++; \
9214 count++; \
9215 numchars--; \
9216 } \
9217 } \
9218 while (0)
9219
8805890a 9220#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9221#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9222
dc6f92b8
JB
9223/* Read events coming from the X server.
9224 This routine is called by the SIGIO handler.
9225 We return as soon as there are no more events to be read.
9226
9227 Events representing keys are stored in buffer BUFP,
9228 which can hold up to NUMCHARS characters.
9229 We return the number of characters stored into the buffer,
9230 thus pretending to be `read'.
9231
dc6f92b8
JB
9232 EXPECTED is nonzero if the caller knows input is available. */
9233
7c5283e4 9234int
f66868ba 9235XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9236 register int sd;
8805890a
KH
9237 /* register */ struct input_event *bufp;
9238 /* register */ int numchars;
dc6f92b8
JB
9239 int expected;
9240{
9241 int count = 0;
9242 int nbytes = 0;
dc6f92b8 9243 XEvent event;
f676886a 9244 struct frame *f;
66f55a9d 9245 int event_found = 0;
334208b7 9246 struct x_display_info *dpyinfo;
379b5ac0 9247 struct coding_system coding;
dc6f92b8 9248
9ac0d9e0 9249 if (interrupt_input_blocked)
dc6f92b8 9250 {
9ac0d9e0 9251 interrupt_input_pending = 1;
dc6f92b8
JB
9252 return -1;
9253 }
9254
9ac0d9e0 9255 interrupt_input_pending = 0;
dc6f92b8 9256 BLOCK_INPUT;
c0a04927
RS
9257
9258 /* So people can tell when we have read the available input. */
9259 input_signal_count++;
9260
dc6f92b8 9261 if (numchars <= 0)
06a2c219 9262 abort (); /* Don't think this happens. */
dc6f92b8 9263
bde5503b
GM
9264 ++handling_signal;
9265
379b5ac0
KH
9266 /* The input should be decoded if it is from XIM. Currently the
9267 locale of XIM is the same as that of the system. So, we can use
9268 Vlocale_coding_system which is initialized properly at Emacs
9269 startup time. */
9270 setup_coding_system (Vlocale_coding_system, &coding);
9271 coding.src_multibyte = 0;
9272 coding.dst_multibyte = 1;
9273 /* The input is converted to events, thus we can't handle
9274 composition. Anyway, there's no XIM that gives us composition
9275 information. */
9276 coding.composing = COMPOSITION_DISABLED;
9277
7a13e894
RS
9278 /* Find the display we are supposed to read input for.
9279 It's the one communicating on descriptor SD. */
9280 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9281 {
9282#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9283#ifdef FIOSNBIO
7a13e894
RS
9284 /* If available, Xlib uses FIOSNBIO to make the socket
9285 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9286 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9287 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9288 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9289#endif /* ! defined (FIOSNBIO) */
7a13e894 9290#endif
dc6f92b8 9291
7a13e894
RS
9292#if 0 /* This code can't be made to work, with multiple displays,
9293 and appears not to be used on any system any more.
9294 Also keyboard.c doesn't turn O_NDELAY on and off
9295 for X connections. */
dc6f92b8
JB
9296#ifndef SIGIO
9297#ifndef HAVE_SELECT
7a13e894
RS
9298 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9299 {
9300 extern int read_alarm_should_throw;
9301 read_alarm_should_throw = 1;
9302 XPeekEvent (dpyinfo->display, &event);
9303 read_alarm_should_throw = 0;
9304 }
c118dd06
JB
9305#endif /* HAVE_SELECT */
9306#endif /* SIGIO */
7a13e894 9307#endif
dc6f92b8 9308
7a13e894
RS
9309 /* For debugging, this gives a way to fake an I/O error. */
9310 if (dpyinfo == XTread_socket_fake_io_error)
9311 {
9312 XTread_socket_fake_io_error = 0;
9313 x_io_error_quitter (dpyinfo->display);
9314 }
dc6f92b8 9315
06a2c219 9316 while (XPending (dpyinfo->display))
dc6f92b8 9317 {
7a13e894 9318 XNextEvent (dpyinfo->display, &event);
06a2c219 9319
531483fb 9320#ifdef HAVE_X_I18N
d1bc4182 9321 {
f2be1146
GM
9322 /* Filter events for the current X input method.
9323 XFilterEvent returns non-zero if the input method has
9324 consumed the event. We pass the frame's X window to
9325 XFilterEvent because that's the one for which the IC
9326 was created. */
f5d11644
GM
9327 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9328 event.xclient.window);
9329 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9330 break;
9331 }
0cd6403b 9332#endif
7a13e894
RS
9333 event_found = 1;
9334
9335 switch (event.type)
9336 {
9337 case ClientMessage:
c047688c 9338 {
7a13e894
RS
9339 if (event.xclient.message_type
9340 == dpyinfo->Xatom_wm_protocols
9341 && event.xclient.format == 32)
c047688c 9342 {
7a13e894
RS
9343 if (event.xclient.data.l[0]
9344 == dpyinfo->Xatom_wm_take_focus)
c047688c 9345 {
8c1a6a84
RS
9346 /* Use x_any_window_to_frame because this
9347 could be the shell widget window
9348 if the frame has no title bar. */
9349 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9350#ifdef HAVE_X_I18N
9351 /* Not quite sure this is needed -pd */
8c1a6a84 9352 if (f && FRAME_XIC (f))
6c183ba5
RS
9353 XSetICFocus (FRAME_XIC (f));
9354#endif
f1da8f06
GM
9355#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9356 instructs the WM to set the input focus automatically for
9357 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9358 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9359 it has set the focus. So, XSetInputFocus below is not
9360 needed.
9361
9362 The call to XSetInputFocus below has also caused trouble. In
9363 cases where the XSetInputFocus done by the WM and the one
9364 below are temporally close (on a fast machine), the call
9365 below can generate additional FocusIn events which confuse
9366 Emacs. */
9367
bf7253f4
RS
9368 /* Since we set WM_TAKE_FOCUS, we must call
9369 XSetInputFocus explicitly. But not if f is null,
9370 since that might be an event for a deleted frame. */
7a13e894 9371 if (f)
bf7253f4
RS
9372 {
9373 Display *d = event.xclient.display;
9374 /* Catch and ignore errors, in case window has been
9375 iconified by a window manager such as GWM. */
9376 int count = x_catch_errors (d);
9377 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9378 /* The ICCCM says this is
9379 the only valid choice. */
9380 RevertToParent,
bf7253f4
RS
9381 event.xclient.data.l[1]);
9382 /* This is needed to detect the error
9383 if there is an error. */
9384 XSync (d, False);
9385 x_uncatch_errors (d, count);
9386 }
7a13e894 9387 /* Not certain about handling scroll bars here */
f1da8f06 9388#endif /* 0 */
c047688c 9389 }
7a13e894
RS
9390 else if (event.xclient.data.l[0]
9391 == dpyinfo->Xatom_wm_save_yourself)
9392 {
9393 /* Save state modify the WM_COMMAND property to
06a2c219 9394 something which can reinstate us. This notifies
7a13e894
RS
9395 the session manager, who's looking for such a
9396 PropertyNotify. Can restart processing when
06a2c219 9397 a keyboard or mouse event arrives. */
7a13e894
RS
9398 if (numchars > 0)
9399 {
19126e11
KH
9400 f = x_top_window_to_frame (dpyinfo,
9401 event.xclient.window);
7a13e894
RS
9402
9403 /* This is just so we only give real data once
9404 for a single Emacs process. */
b86bd3dd 9405 if (f == SELECTED_FRAME ())
7a13e894
RS
9406 XSetCommand (FRAME_X_DISPLAY (f),
9407 event.xclient.window,
9408 initial_argv, initial_argc);
f000f5c5 9409 else if (f)
7a13e894
RS
9410 XSetCommand (FRAME_X_DISPLAY (f),
9411 event.xclient.window,
9412 0, 0);
9413 }
9414 }
9415 else if (event.xclient.data.l[0]
9416 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9417 {
19126e11
KH
9418 struct frame *f
9419 = x_any_window_to_frame (dpyinfo,
9420 event.xclient.window);
1fb20991 9421
7a13e894
RS
9422 if (f)
9423 {
9424 if (numchars == 0)
9425 abort ();
1fb20991 9426
7a13e894
RS
9427 bufp->kind = delete_window_event;
9428 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9429 bufp->arg = Qnil;
7a13e894
RS
9430 bufp++;
9431
9432 count += 1;
9433 numchars -= 1;
9434 }
1fb20991 9435 }
c047688c 9436 }
7a13e894
RS
9437 else if (event.xclient.message_type
9438 == dpyinfo->Xatom_wm_configure_denied)
9439 {
9440 }
9441 else if (event.xclient.message_type
9442 == dpyinfo->Xatom_wm_window_moved)
9443 {
9444 int new_x, new_y;
19126e11
KH
9445 struct frame *f
9446 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9447
7a13e894
RS
9448 new_x = event.xclient.data.s[0];
9449 new_y = event.xclient.data.s[1];
1fb20991 9450
7a13e894
RS
9451 if (f)
9452 {
7556890b
RS
9453 f->output_data.x->left_pos = new_x;
9454 f->output_data.x->top_pos = new_y;
7a13e894 9455 }
1fb20991 9456 }
0fdff6bb 9457#ifdef HACK_EDITRES
7a13e894
RS
9458 else if (event.xclient.message_type
9459 == dpyinfo->Xatom_editres)
9460 {
19126e11
KH
9461 struct frame *f
9462 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9463 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9464 &event, NULL);
7a13e894 9465 }
0fdff6bb 9466#endif /* HACK_EDITRES */
06a2c219
GM
9467 else if ((event.xclient.message_type
9468 == dpyinfo->Xatom_DONE)
9469 || (event.xclient.message_type
9470 == dpyinfo->Xatom_PAGE))
9471 {
9472 /* Ghostview job completed. Kill it. We could
9473 reply with "Next" if we received "Page", but we
9474 currently never do because we are interested in
9475 images, only, which should have 1 page. */
06a2c219
GM
9476 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9477 struct frame *f
9478 = x_window_to_frame (dpyinfo, event.xclient.window);
9479 x_kill_gs_process (pixmap, f);
9480 expose_frame (f, 0, 0, 0, 0);
9481 }
9482#ifdef USE_TOOLKIT_SCROLL_BARS
9483 /* Scroll bar callbacks send a ClientMessage from which
9484 we construct an input_event. */
9485 else if (event.xclient.message_type
9486 == dpyinfo->Xatom_Scrollbar)
9487 {
9488 x_scroll_bar_to_input_event (&event, bufp);
9489 ++bufp, ++count, --numchars;
9490 goto out;
9491 }
9492#endif /* USE_TOOLKIT_SCROLL_BARS */
9493 else
9494 goto OTHER;
7a13e894
RS
9495 }
9496 break;
dc6f92b8 9497
7a13e894 9498 case SelectionNotify:
3afe33e7 9499#ifdef USE_X_TOOLKIT
19126e11 9500 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9501 goto OTHER;
3afe33e7 9502#endif /* not USE_X_TOOLKIT */
dfcf069d 9503 x_handle_selection_notify (&event.xselection);
7a13e894 9504 break;
d56a553a 9505
06a2c219 9506 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9507#ifdef USE_X_TOOLKIT
19126e11 9508 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9509 goto OTHER;
3afe33e7 9510#endif /* USE_X_TOOLKIT */
7a13e894
RS
9511 {
9512 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9513
7a13e894
RS
9514 if (numchars == 0)
9515 abort ();
d56a553a 9516
7a13e894
RS
9517 bufp->kind = selection_clear_event;
9518 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9519 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9520 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9521 bufp->frame_or_window = Qnil;
0f8aabe9 9522 bufp->arg = Qnil;
7a13e894 9523 bufp++;
d56a553a 9524
7a13e894
RS
9525 count += 1;
9526 numchars -= 1;
9527 }
9528 break;
dc6f92b8 9529
06a2c219 9530 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9531#ifdef USE_X_TOOLKIT
19126e11 9532 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9533 goto OTHER;
3afe33e7 9534#endif /* USE_X_TOOLKIT */
7a13e894 9535 if (x_queue_selection_requests)
19126e11 9536 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9537 &event);
9538 else
9539 {
9540 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9541
7a13e894
RS
9542 if (numchars == 0)
9543 abort ();
9544
9545 bufp->kind = selection_request_event;
9546 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9547 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9548 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9549 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9550 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9551 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9552 bufp->frame_or_window = Qnil;
0f8aabe9 9553 bufp->arg = Qnil;
7a13e894
RS
9554 bufp++;
9555
9556 count += 1;
9557 numchars -= 1;
9558 }
9559 break;
9560
9561 case PropertyNotify:
3afe33e7 9562#ifdef USE_X_TOOLKIT
19126e11 9563 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9564 goto OTHER;
3afe33e7 9565#endif /* not USE_X_TOOLKIT */
dfcf069d 9566 x_handle_property_notify (&event.xproperty);
7a13e894 9567 break;
dc6f92b8 9568
7a13e894 9569 case ReparentNotify:
19126e11 9570 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9571 if (f)
9572 {
9573 int x, y;
7556890b 9574 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9575 x_real_positions (f, &x, &y);
7556890b
RS
9576 f->output_data.x->left_pos = x;
9577 f->output_data.x->top_pos = y;
7a13e894
RS
9578 }
9579 break;
3bd330d4 9580
7a13e894 9581 case Expose:
19126e11 9582 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9583 if (f)
dc6f92b8 9584 {
7a13e894
RS
9585 if (f->async_visible == 0)
9586 {
9587 f->async_visible = 1;
9588 f->async_iconified = 0;
06c488fd 9589 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9590 SET_FRAME_GARBAGED (f);
9591 }
9592 else
06a2c219
GM
9593 expose_frame (x_window_to_frame (dpyinfo,
9594 event.xexpose.window),
9595 event.xexpose.x, event.xexpose.y,
9596 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9597 }
9598 else
7a13e894 9599 {
06a2c219
GM
9600#ifdef USE_TOOLKIT_SCROLL_BARS
9601 /* Dispatch event to the widget. */
9602 goto OTHER;
9603#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9604 struct scroll_bar *bar
9605 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9606
7a13e894
RS
9607 if (bar)
9608 x_scroll_bar_expose (bar, &event);
3afe33e7 9609#ifdef USE_X_TOOLKIT
7a13e894
RS
9610 else
9611 goto OTHER;
3afe33e7 9612#endif /* USE_X_TOOLKIT */
06a2c219 9613#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9614 }
9615 break;
dc6f92b8 9616
7a13e894
RS
9617 case GraphicsExpose: /* This occurs when an XCopyArea's
9618 source area was obscured or not
9619 available.*/
19126e11 9620 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9621 if (f)
9622 {
06a2c219
GM
9623 expose_frame (f,
9624 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9625 event.xgraphicsexpose.width,
9626 event.xgraphicsexpose.height);
7a13e894 9627 }
3afe33e7 9628#ifdef USE_X_TOOLKIT
7a13e894
RS
9629 else
9630 goto OTHER;
3afe33e7 9631#endif /* USE_X_TOOLKIT */
7a13e894 9632 break;
dc6f92b8 9633
7a13e894 9634 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9635 source area was completely
9636 available */
7a13e894 9637 break;
dc6f92b8 9638
7a13e894 9639 case UnmapNotify:
06a2c219
GM
9640 /* Redo the mouse-highlight after the tooltip has gone. */
9641 if (event.xmap.window == tip_window)
9642 {
9643 tip_window = 0;
9644 redo_mouse_highlight ();
9645 }
9646
91ea2a7a 9647 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9648 if (f) /* F may no longer exist if
9649 the frame was deleted. */
9650 {
9651 /* While a frame is unmapped, display generation is
9652 disabled; you don't want to spend time updating a
9653 display that won't ever be seen. */
9654 f->async_visible = 0;
9655 /* We can't distinguish, from the event, whether the window
9656 has become iconified or invisible. So assume, if it
9657 was previously visible, than now it is iconified.
1aa6072f
RS
9658 But x_make_frame_invisible clears both
9659 the visible flag and the iconified flag;
9660 and that way, we know the window is not iconified now. */
7a13e894 9661 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9662 {
9663 f->async_iconified = 1;
bddd097c 9664
1aa6072f
RS
9665 bufp->kind = iconify_event;
9666 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9667 bufp->arg = Qnil;
1aa6072f
RS
9668 bufp++;
9669 count++;
9670 numchars--;
9671 }
7a13e894 9672 }
7a13e894 9673 goto OTHER;
dc6f92b8 9674
7a13e894 9675 case MapNotify:
06a2c219
GM
9676 if (event.xmap.window == tip_window)
9677 /* The tooltip has been drawn already. Avoid
9678 the SET_FRAME_GARBAGED below. */
9679 goto OTHER;
9680
9681 /* We use x_top_window_to_frame because map events can
9682 come for sub-windows and they don't mean that the
9683 frame is visible. */
19126e11 9684 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9685 if (f)
9686 {
9687 f->async_visible = 1;
9688 f->async_iconified = 0;
06c488fd 9689 f->output_data.x->has_been_visible = 1;
dc6f92b8 9690
7a13e894
RS
9691 /* wait_reading_process_input will notice this and update
9692 the frame's display structures. */
9693 SET_FRAME_GARBAGED (f);
bddd097c 9694
d806e720
RS
9695 if (f->iconified)
9696 {
9697 bufp->kind = deiconify_event;
9698 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9699 bufp->arg = Qnil;
d806e720
RS
9700 bufp++;
9701 count++;
9702 numchars--;
9703 }
e73ec6fa 9704 else if (! NILP (Vframe_list)
8e713be6 9705 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9706 /* Force a redisplay sooner or later
9707 to update the frame titles
9708 in case this is the second frame. */
9709 record_asynch_buffer_change ();
7a13e894 9710 }
7a13e894 9711 goto OTHER;
dc6f92b8 9712
7a13e894 9713 case KeyPress:
19126e11 9714 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9715
eccc05db 9716#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9717 /* I couldn't find a way to prevent LessTif scroll bars
9718 from consuming key events. */
9719 if (f == 0)
9720 {
9721 Widget widget = XtWindowToWidget (dpyinfo->display,
9722 event.xkey.window);
9723 if (widget && XmIsScrollBar (widget))
9724 {
9725 widget = XtParent (widget);
9726 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9727 }
9728 }
eccc05db 9729#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9730
7a13e894
RS
9731 if (f != 0)
9732 {
9733 KeySym keysym, orig_keysym;
379b5ac0
KH
9734 /* al%imercury@uunet.uu.net says that making this 81
9735 instead of 80 fixed a bug whereby meta chars made
9736 his Emacs hang.
9737
9738 It seems that some version of XmbLookupString has
9739 a bug of not returning XBufferOverflow in
9740 status_return even if the input is too long to
9741 fit in 81 bytes. So, we must prepare sufficient
9742 bytes for copy_buffer. 513 bytes (256 chars for
9743 two-byte character set) seems to be a faily good
9744 approximation. -- 2000.8.10 handa@etl.go.jp */
9745 unsigned char copy_buffer[513];
9746 unsigned char *copy_bufptr = copy_buffer;
9747 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9748 int modifiers;
64bb1782 9749
7a13e894
RS
9750 event.xkey.state
9751 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9752 extra_keyboard_modifiers);
9753 modifiers = event.xkey.state;
3a2712f9 9754
7a13e894 9755 /* This will have to go some day... */
752a043f 9756
7a13e894
RS
9757 /* make_lispy_event turns chars into control chars.
9758 Don't do it here because XLookupString is too eager. */
9759 event.xkey.state &= ~ControlMask;
5d46f928
RS
9760 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9761 | dpyinfo->super_mod_mask
9762 | dpyinfo->hyper_mod_mask
9763 | dpyinfo->alt_mod_mask);
9764
1cf4a0d1
RS
9765 /* In case Meta is ComposeCharacter,
9766 clear its status. According to Markus Ehrnsperger
9767 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9768 this enables ComposeCharacter to work whether or
9769 not it is combined with Meta. */
9770 if (modifiers & dpyinfo->meta_mod_mask)
9771 bzero (&compose_status, sizeof (compose_status));
9772
6c183ba5
RS
9773#ifdef HAVE_X_I18N
9774 if (FRAME_XIC (f))
9775 {
f5d11644
GM
9776 Status status_return;
9777
6c183ba5 9778 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9779 &event.xkey, copy_bufptr,
9780 copy_bufsiz, &keysym,
6c183ba5 9781 &status_return);
f5d11644
GM
9782 if (status_return == XBufferOverflow)
9783 {
9784 copy_bufsiz = nbytes + 1;
9785 copy_bufptr = (char *) alloca (copy_bufsiz);
9786 nbytes = XmbLookupString (FRAME_XIC (f),
9787 &event.xkey, copy_bufptr,
9788 copy_bufsiz, &keysym,
9789 &status_return);
9790 }
9791
1decb680
PE
9792 if (status_return == XLookupNone)
9793 break;
9794 else if (status_return == XLookupChars)
fdd9d55e
GM
9795 {
9796 keysym = NoSymbol;
9797 modifiers = 0;
9798 }
1decb680
PE
9799 else if (status_return != XLookupKeySym
9800 && status_return != XLookupBoth)
9801 abort ();
6c183ba5
RS
9802 }
9803 else
379b5ac0
KH
9804 nbytes = XLookupString (&event.xkey, copy_bufptr,
9805 copy_bufsiz, &keysym,
9806 &compose_status);
6c183ba5 9807#else
379b5ac0
KH
9808 nbytes = XLookupString (&event.xkey, copy_bufptr,
9809 copy_bufsiz, &keysym,
9810 &compose_status);
6c183ba5 9811#endif
dc6f92b8 9812
7a13e894 9813 orig_keysym = keysym;
55123275 9814
7a13e894
RS
9815 if (numchars > 1)
9816 {
9817 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9818 || keysym == XK_Delete
1097aea0 9819#ifdef XK_ISO_Left_Tab
441affdb 9820 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9821#endif
852bff8f 9822 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9823 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9824 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9825#ifdef HPUX
7a13e894
RS
9826 /* This recognizes the "extended function keys".
9827 It seems there's no cleaner way.
9828 Test IsModifierKey to avoid handling mode_switch
9829 incorrectly. */
9830 || ((unsigned) (keysym) >= XK_Select
9831 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9832#endif
9833#ifdef XK_dead_circumflex
7a13e894 9834 || orig_keysym == XK_dead_circumflex
69388238
RS
9835#endif
9836#ifdef XK_dead_grave
7a13e894 9837 || orig_keysym == XK_dead_grave
69388238
RS
9838#endif
9839#ifdef XK_dead_tilde
7a13e894 9840 || orig_keysym == XK_dead_tilde
69388238
RS
9841#endif
9842#ifdef XK_dead_diaeresis
7a13e894 9843 || orig_keysym == XK_dead_diaeresis
69388238
RS
9844#endif
9845#ifdef XK_dead_macron
7a13e894 9846 || orig_keysym == XK_dead_macron
69388238
RS
9847#endif
9848#ifdef XK_dead_degree
7a13e894 9849 || orig_keysym == XK_dead_degree
69388238
RS
9850#endif
9851#ifdef XK_dead_acute
7a13e894 9852 || orig_keysym == XK_dead_acute
69388238
RS
9853#endif
9854#ifdef XK_dead_cedilla
7a13e894 9855 || orig_keysym == XK_dead_cedilla
69388238
RS
9856#endif
9857#ifdef XK_dead_breve
7a13e894 9858 || orig_keysym == XK_dead_breve
69388238
RS
9859#endif
9860#ifdef XK_dead_ogonek
7a13e894 9861 || orig_keysym == XK_dead_ogonek
69388238
RS
9862#endif
9863#ifdef XK_dead_caron
7a13e894 9864 || orig_keysym == XK_dead_caron
69388238
RS
9865#endif
9866#ifdef XK_dead_doubleacute
7a13e894 9867 || orig_keysym == XK_dead_doubleacute
69388238
RS
9868#endif
9869#ifdef XK_dead_abovedot
7a13e894 9870 || orig_keysym == XK_dead_abovedot
c34790e0 9871#endif
7a13e894
RS
9872 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9873 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9874 /* Any "vendor-specific" key is ok. */
9875 || (orig_keysym & (1 << 28)))
9876 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9877#ifndef HAVE_X11R5
9878#ifdef XK_Mode_switch
7a13e894 9879 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9880#endif
9881#ifdef XK_Num_Lock
7a13e894 9882 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9883#endif
9884#endif /* not HAVE_X11R5 */
7a13e894 9885 ))
dc6f92b8 9886 {
10e6549c
RS
9887 if (temp_index == sizeof temp_buffer / sizeof (short))
9888 temp_index = 0;
7a13e894
RS
9889 temp_buffer[temp_index++] = keysym;
9890 bufp->kind = non_ascii_keystroke;
9891 bufp->code = keysym;
e0c1aef2 9892 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9893 bufp->arg = Qnil;
334208b7
RS
9894 bufp->modifiers
9895 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9896 modifiers);
1113d9db 9897 bufp->timestamp = event.xkey.time;
dc6f92b8 9898 bufp++;
7a13e894
RS
9899 count++;
9900 numchars--;
dc6f92b8 9901 }
7a13e894
RS
9902 else if (numchars > nbytes)
9903 {
9904 register int i;
379b5ac0 9905 register int c;
379b5ac0 9906 int nchars, len;
7a13e894
RS
9907
9908 for (i = 0; i < nbytes; i++)
9909 {
379b5ac0
KH
9910 if (temp_index == (sizeof temp_buffer
9911 / sizeof (short)))
7a13e894 9912 temp_index = 0;
379b5ac0
KH
9913 temp_buffer[temp_index++] = copy_bufptr[i];
9914 }
9915
9916 if (/* If the event is not from XIM, */
9917 event.xkey.keycode != 0
9918 /* or the current locale doesn't request
9919 decoding of the intup data, ... */
9920 || coding.type == coding_type_raw_text
9921 || coding.type == coding_type_no_conversion)
9922 {
9923 /* ... we can use the input data as is. */
9924 nchars = nbytes;
9925 }
9926 else
9927 {
9928 /* We have to decode the input data. */
9929 int require;
9930 unsigned char *p;
9931
9932 require = decoding_buffer_size (&coding, nbytes);
9933 p = (unsigned char *) alloca (require);
9934 coding.mode |= CODING_MODE_LAST_BLOCK;
9935 decode_coding (&coding, copy_bufptr, p,
9936 nbytes, require);
9937 nbytes = coding.produced;
9938 nchars = coding.produced_char;
9939 copy_bufptr = p;
9940 }
9941
9942 /* Convert the input data to a sequence of
9943 character events. */
9944 for (i = 0; i < nbytes; i += len)
9945 {
9946 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9947 nbytes - i, len);
9948 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9949 ? ascii_keystroke
9950 : multibyte_char_keystroke);
9951 bufp->code = c;
7a13e894 9952 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9953 bufp->arg = Qnil;
7a13e894
RS
9954 bufp->modifiers
9955 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9956 modifiers);
9957 bufp->timestamp = event.xkey.time;
9958 bufp++;
9959 }
9960
379b5ac0
KH
9961 count += nchars;
9962 numchars -= nchars;
1decb680
PE
9963
9964 if (keysym == NoSymbol)
9965 break;
7a13e894
RS
9966 }
9967 else
9968 abort ();
dc6f92b8 9969 }
10e6549c
RS
9970 else
9971 abort ();
dc6f92b8 9972 }
59ddecde
GM
9973#ifdef HAVE_X_I18N
9974 /* Don't dispatch this event since XtDispatchEvent calls
9975 XFilterEvent, and two calls in a row may freeze the
9976 client. */
9977 break;
9978#else
717ca130 9979 goto OTHER;
59ddecde 9980#endif
f451eb13 9981
f5d11644 9982 case KeyRelease:
59ddecde
GM
9983#ifdef HAVE_X_I18N
9984 /* Don't dispatch this event since XtDispatchEvent calls
9985 XFilterEvent, and two calls in a row may freeze the
9986 client. */
9987 break;
9988#else
f5d11644 9989 goto OTHER;
59ddecde 9990#endif
f5d11644 9991
7a13e894 9992 /* Here's a possible interpretation of the whole
06a2c219
GM
9993 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9994 you get a FocusIn event, you have to get a FocusOut
9995 event before you relinquish the focus. If you
9996 haven't received a FocusIn event, then a mere
9997 LeaveNotify is enough to free you. */
f451eb13 9998
7a13e894 9999 case EnterNotify:
06a2c219
GM
10000 {
10001 int from_menu_bar_p = 0;
10002
10003 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10004
10005#ifdef LESSTIF_VERSION
10006 /* When clicking outside of a menu bar popup to close
10007 it, we get a FocusIn/ EnterNotify sequence of
10008 events. The flag event.xcrossing.focus is not set
10009 in the EnterNotify event of that sequence because
10010 the focus is in the menu bar,
10011 event.xcrossing.window is the frame's X window.
10012 Unconditionally setting the focus frame to null in
10013 this case is not the right thing, because no event
10014 follows that could set the focus frame to the right
10015 value.
10016
10017 This could be a LessTif bug, but I wasn't able to
10018 reproduce the behavior in a simple test program.
10019
10020 (gerd, LessTif 0.88.1). */
10021
10022 if (!event.xcrossing.focus
10023 && f
10024 && f->output_data.x->menubar_widget)
10025 {
10026 Window focus;
10027 int revert;
10028
10029 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10030 if (focus == XtWindow (f->output_data.x->menubar_widget))
10031 from_menu_bar_p = 1;
10032 }
10033#endif /* LESSTIF_VERSION */
6d4238f3 10034
06a2c219
GM
10035 if (event.xcrossing.focus || from_menu_bar_p)
10036 {
10037 /* Avoid nasty pop/raise loops. */
10038 if (f && (!(f->auto_raise)
10039 || !(f->auto_lower)
10040 || (event.xcrossing.time - enter_timestamp) > 500))
10041 {
10042 x_new_focus_frame (dpyinfo, f);
10043 enter_timestamp = event.xcrossing.time;
10044 }
10045 }
10046 else if (f == dpyinfo->x_focus_frame)
10047 x_new_focus_frame (dpyinfo, 0);
10048
10049 /* EnterNotify counts as mouse movement,
10050 so update things that depend on mouse position. */
10051 if (f && !f->output_data.x->busy_p)
10052 note_mouse_movement (f, &event.xmotion);
10053 goto OTHER;
10054 }
dc6f92b8 10055
7a13e894 10056 case FocusIn:
19126e11 10057 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10058 if (event.xfocus.detail != NotifyPointer)
0f941935 10059 dpyinfo->x_focus_event_frame = f;
7a13e894 10060 if (f)
eb72635f
GM
10061 {
10062 x_new_focus_frame (dpyinfo, f);
10063
10064 /* Don't stop displaying the initial startup message
10065 for a switch-frame event we don't need. */
10066 if (GC_NILP (Vterminal_frame)
10067 && GC_CONSP (Vframe_list)
10068 && !GC_NILP (XCDR (Vframe_list)))
10069 {
10070 bufp->kind = FOCUS_IN_EVENT;
10071 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10072 bufp->arg = Qnil;
eb72635f
GM
10073 ++bufp, ++count, --numchars;
10074 }
10075 }
f9e24cb9 10076
6c183ba5
RS
10077#ifdef HAVE_X_I18N
10078 if (f && FRAME_XIC (f))
10079 XSetICFocus (FRAME_XIC (f));
10080#endif
10081
7a13e894 10082 goto OTHER;
10c5e63d 10083
7a13e894 10084 case LeaveNotify:
19126e11 10085 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10086 if (f)
10c5e63d 10087 {
06a2c219
GM
10088 int from_menu_bar_p = 0;
10089
7a13e894 10090 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10091 {
10092 /* If we move outside the frame, then we're
10093 certainly no longer on any text in the frame. */
10094 clear_mouse_face (dpyinfo);
10095 dpyinfo->mouse_face_mouse_frame = 0;
10096 }
10097
10098 /* Generate a nil HELP_EVENT to cancel a help-echo.
10099 Do it only if there's something to cancel.
10100 Otherwise, the startup message is cleared when
10101 the mouse leaves the frame. */
10102 if (any_help_event_p)
10103 {
be010514
GM
10104 Lisp_Object frame;
10105 int n;
10106
06a2c219 10107 XSETFRAME (frame, f);
82c5d67a 10108 help_echo = Qnil;
5ab2570d
GM
10109 n = gen_help_event (bufp, numchars,
10110 Qnil, frame, Qnil, Qnil, 0);
be010514 10111 bufp += n, count += n, numchars -= n;
06a2c219 10112 }
7a13e894 10113
06a2c219
GM
10114#ifdef LESSTIF_VERSION
10115 /* Please see the comment at the start of the
10116 EnterNotify case. */
10117 if (!event.xcrossing.focus
10118 && f->output_data.x->menubar_widget)
10119 {
10120 Window focus;
10121 int revert;
10122 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10123 if (focus == XtWindow (f->output_data.x->menubar_widget))
10124 from_menu_bar_p = 1;
10125 }
10126#endif /* LESSTIF_VERSION */
10127
10128 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10129 x_mouse_leave (dpyinfo);
10c5e63d 10130 else
7a13e894 10131 {
0f941935
KH
10132 if (f == dpyinfo->x_focus_event_frame)
10133 dpyinfo->x_focus_event_frame = 0;
10134 if (f == dpyinfo->x_focus_frame)
10135 x_new_focus_frame (dpyinfo, 0);
7a13e894 10136 }
10c5e63d 10137 }
7a13e894 10138 goto OTHER;
dc6f92b8 10139
7a13e894 10140 case FocusOut:
19126e11 10141 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10142 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10143 && f == dpyinfo->x_focus_event_frame)
10144 dpyinfo->x_focus_event_frame = 0;
10145 if (f && f == dpyinfo->x_focus_frame)
10146 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10147
6c183ba5
RS
10148#ifdef HAVE_X_I18N
10149 if (f && FRAME_XIC (f))
10150 XUnsetICFocus (FRAME_XIC (f));
10151#endif
10152
7a13e894 10153 goto OTHER;
dc6f92b8 10154
7a13e894 10155 case MotionNotify:
dc6f92b8 10156 {
06a2c219 10157 previous_help_echo = help_echo;
7cea38bc 10158 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10159 help_echo_pos = -1;
06a2c219 10160
7a13e894
RS
10161 if (dpyinfo->grabbed && last_mouse_frame
10162 && FRAME_LIVE_P (last_mouse_frame))
10163 f = last_mouse_frame;
10164 else
19126e11 10165 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10166
7a13e894
RS
10167 if (f)
10168 note_mouse_movement (f, &event.xmotion);
10169 else
10170 {
e88b3c50 10171#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10172 struct scroll_bar *bar
10173 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10174
7a13e894
RS
10175 if (bar)
10176 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10177#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10178
06a2c219
GM
10179 /* If we move outside the frame, then we're
10180 certainly no longer on any text in the frame. */
7a13e894
RS
10181 clear_mouse_face (dpyinfo);
10182 }
06a2c219
GM
10183
10184 /* If the contents of the global variable help_echo
10185 has changed, generate a HELP_EVENT. */
b7e80413
SM
10186 if (!NILP (help_echo)
10187 || !NILP (previous_help_echo))
06a2c219
GM
10188 {
10189 Lisp_Object frame;
be010514 10190 int n;
06a2c219
GM
10191
10192 if (f)
10193 XSETFRAME (frame, f);
10194 else
10195 frame = Qnil;
10196
10197 any_help_event_p = 1;
5ab2570d 10198 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10199 help_echo_window, help_echo_object,
10200 help_echo_pos);
be010514 10201 bufp += n, count += n, numchars -= n;
06a2c219
GM
10202 }
10203
10204 goto OTHER;
dc6f92b8 10205 }
dc6f92b8 10206
7a13e894 10207 case ConfigureNotify:
9829ddba
RS
10208 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10209 if (f)
af395ec1 10210 {
5c187dee 10211#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10212 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10213 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10214
2d7fc7e8
RS
10215 /* In the toolkit version, change_frame_size
10216 is called by the code that handles resizing
10217 of the EmacsFrame widget. */
7a13e894 10218
7a13e894
RS
10219 /* Even if the number of character rows and columns has
10220 not changed, the font size may have changed, so we need
10221 to check the pixel dimensions as well. */
10222 if (columns != f->width
10223 || rows != f->height
7556890b
RS
10224 || event.xconfigure.width != f->output_data.x->pixel_width
10225 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10226 {
7d1e984f 10227 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10228 SET_FRAME_GARBAGED (f);
e687d06e 10229 cancel_mouse_face (f);
7a13e894 10230 }
2d7fc7e8 10231#endif
af395ec1 10232
7556890b
RS
10233 f->output_data.x->pixel_width = event.xconfigure.width;
10234 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10235
10236 /* What we have now is the position of Emacs's own window.
10237 Convert that to the position of the window manager window. */
dcb07ae9
RS
10238 x_real_positions (f, &f->output_data.x->left_pos,
10239 &f->output_data.x->top_pos);
10240
f5d11644
GM
10241#ifdef HAVE_X_I18N
10242 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10243 xic_set_statusarea (f);
10244#endif
10245
dcb07ae9
RS
10246 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10247 {
10248 /* Since the WM decorations come below top_pos now,
10249 we must put them below top_pos in the future. */
10250 f->output_data.x->win_gravity = NorthWestGravity;
10251 x_wm_set_size_hint (f, (long) 0, 0);
10252 }
8f08dc93
KH
10253#ifdef USE_MOTIF
10254 /* Some window managers pass (0,0) as the location of
10255 the window, and the Motif event handler stores it
10256 in the emacs widget, which messes up Motif menus. */
10257 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10258 {
10259 event.xconfigure.x = f->output_data.x->widget->core.x;
10260 event.xconfigure.y = f->output_data.x->widget->core.y;
10261 }
06a2c219 10262#endif /* USE_MOTIF */
7a13e894 10263 }
2d7fc7e8 10264 goto OTHER;
dc6f92b8 10265
7a13e894
RS
10266 case ButtonPress:
10267 case ButtonRelease:
10268 {
10269 /* If we decide we want to generate an event to be seen
10270 by the rest of Emacs, we put it here. */
10271 struct input_event emacs_event;
9ea173e8 10272 int tool_bar_p = 0;
06a2c219 10273
7a13e894 10274 emacs_event.kind = no_event;
7a13e894 10275 bzero (&compose_status, sizeof (compose_status));
9b07615b 10276
06a2c219
GM
10277 if (dpyinfo->grabbed
10278 && last_mouse_frame
9f67f20b
RS
10279 && FRAME_LIVE_P (last_mouse_frame))
10280 f = last_mouse_frame;
10281 else
2224b905 10282 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10283
06a2c219
GM
10284 if (f)
10285 {
9ea173e8
GM
10286 /* Is this in the tool-bar? */
10287 if (WINDOWP (f->tool_bar_window)
10288 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10289 {
10290 Lisp_Object window;
10291 int p, x, y;
10292
10293 x = event.xbutton.x;
10294 y = event.xbutton.y;
10295
10296 /* Set x and y. */
10297 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10298 if (EQ (window, f->tool_bar_window))
06a2c219 10299 {
9ea173e8
GM
10300 x_handle_tool_bar_click (f, &event.xbutton);
10301 tool_bar_p = 1;
06a2c219
GM
10302 }
10303 }
10304
9ea173e8 10305 if (!tool_bar_p)
06a2c219
GM
10306 if (!dpyinfo->x_focus_frame
10307 || f == dpyinfo->x_focus_frame)
10308 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10309 }
10310 else
10311 {
06a2c219 10312#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10313 struct scroll_bar *bar
10314 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10315
7a13e894
RS
10316 if (bar)
10317 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10318#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10319 }
10320
10321 if (event.type == ButtonPress)
10322 {
10323 dpyinfo->grabbed |= (1 << event.xbutton.button);
10324 last_mouse_frame = f;
edad46f6
KH
10325 /* Ignore any mouse motion that happened
10326 before this event; any subsequent mouse-movement
10327 Emacs events should reflect only motion after
10328 the ButtonPress. */
a00e91cd
KH
10329 if (f != 0)
10330 f->mouse_moved = 0;
06a2c219 10331
9ea173e8
GM
10332 if (!tool_bar_p)
10333 last_tool_bar_item = -1;
7a13e894 10334 }
3afe33e7
RS
10335 else
10336 {
7a13e894 10337 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10338 }
23faf38f 10339
7a13e894
RS
10340 if (numchars >= 1 && emacs_event.kind != no_event)
10341 {
10342 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10343 bufp++;
10344 count++;
10345 numchars--;
10346 }
3afe33e7
RS
10347
10348#ifdef USE_X_TOOLKIT
2224b905
RS
10349 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10350 /* For a down-event in the menu bar,
10351 don't pass it to Xt right now.
10352 Instead, save it away
10353 and we will pass it to Xt from kbd_buffer_get_event.
10354 That way, we can run some Lisp code first. */
91375f8f
RS
10355 if (f && event.type == ButtonPress
10356 /* Verify the event is really within the menu bar
10357 and not just sent to it due to grabbing. */
10358 && event.xbutton.x >= 0
10359 && event.xbutton.x < f->output_data.x->pixel_width
10360 && event.xbutton.y >= 0
10361 && event.xbutton.y < f->output_data.x->menubar_height
10362 && event.xbutton.same_screen)
2224b905 10363 {
8805890a 10364 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10365 XSETFRAME (last_mouse_press_frame, f);
10366 }
10367 else if (event.type == ButtonPress)
10368 {
10369 last_mouse_press_frame = Qnil;
30e671c3 10370 goto OTHER;
ce89ef46 10371 }
06a2c219 10372
2237cac9
RS
10373#ifdef USE_MOTIF /* This should do not harm for Lucid,
10374 but I am trying to be cautious. */
ce89ef46
RS
10375 else if (event.type == ButtonRelease)
10376 {
2237cac9 10377 if (!NILP (last_mouse_press_frame))
f10ded1c 10378 {
2237cac9
RS
10379 f = XFRAME (last_mouse_press_frame);
10380 if (f->output_data.x)
06a2c219 10381 SET_SAVED_BUTTON_EVENT;
f10ded1c 10382 }
06a2c219 10383 else
30e671c3 10384 goto OTHER;
2224b905 10385 }
2237cac9 10386#endif /* USE_MOTIF */
2224b905
RS
10387 else
10388 goto OTHER;
3afe33e7 10389#endif /* USE_X_TOOLKIT */
7a13e894
RS
10390 }
10391 break;
dc6f92b8 10392
7a13e894 10393 case CirculateNotify:
06a2c219
GM
10394 goto OTHER;
10395
7a13e894 10396 case CirculateRequest:
06a2c219
GM
10397 goto OTHER;
10398
10399 case VisibilityNotify:
10400 goto OTHER;
dc6f92b8 10401
7a13e894
RS
10402 case MappingNotify:
10403 /* Someone has changed the keyboard mapping - update the
10404 local cache. */
10405 switch (event.xmapping.request)
10406 {
10407 case MappingModifier:
10408 x_find_modifier_meanings (dpyinfo);
10409 /* This is meant to fall through. */
10410 case MappingKeyboard:
10411 XRefreshKeyboardMapping (&event.xmapping);
10412 }
7a13e894 10413 goto OTHER;
dc6f92b8 10414
7a13e894 10415 default:
7a13e894 10416 OTHER:
717ca130 10417#ifdef USE_X_TOOLKIT
7a13e894
RS
10418 BLOCK_INPUT;
10419 XtDispatchEvent (&event);
10420 UNBLOCK_INPUT;
3afe33e7 10421#endif /* USE_X_TOOLKIT */
7a13e894
RS
10422 break;
10423 }
dc6f92b8
JB
10424 }
10425 }
10426
06a2c219
GM
10427 out:;
10428
9a5196d0
RS
10429 /* On some systems, an X bug causes Emacs to get no more events
10430 when the window is destroyed. Detect that. (1994.) */
58769bee 10431 if (! event_found)
ef2a22d0 10432 {
ef2a22d0
RS
10433 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10434 One XNOOP in 100 loops will make Emacs terminate.
10435 B. Bretthauer, 1994 */
10436 x_noop_count++;
58769bee 10437 if (x_noop_count >= 100)
ef2a22d0
RS
10438 {
10439 x_noop_count=0;
2224b905
RS
10440
10441 if (next_noop_dpyinfo == 0)
10442 next_noop_dpyinfo = x_display_list;
10443
10444 XNoOp (next_noop_dpyinfo->display);
10445
10446 /* Each time we get here, cycle through the displays now open. */
10447 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10448 }
10449 }
502add23 10450
06a2c219 10451 /* If the focus was just given to an auto-raising frame,
0134a210 10452 raise it now. */
7a13e894 10453 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10454 if (pending_autoraise_frame)
10455 {
10456 x_raise_frame (pending_autoraise_frame);
10457 pending_autoraise_frame = 0;
10458 }
0134a210 10459
dc6f92b8 10460 UNBLOCK_INPUT;
bde5503b 10461 --handling_signal;
dc6f92b8
JB
10462 return count;
10463}
06a2c219
GM
10464
10465
10466
dc6f92b8 10467\f
06a2c219
GM
10468/***********************************************************************
10469 Text Cursor
10470 ***********************************************************************/
10471
10472/* Note if the text cursor of window W has been overwritten by a
10473 drawing operation that outputs N glyphs starting at HPOS in the
10474 line given by output_cursor.vpos. N < 0 means all the rest of the
10475 line after HPOS has been written. */
10476
10477static void
10478note_overwritten_text_cursor (w, hpos, n)
10479 struct window *w;
10480 int hpos, n;
10481{
10482 if (updated_area == TEXT_AREA
10483 && output_cursor.vpos == w->phys_cursor.vpos
10484 && hpos <= w->phys_cursor.hpos
10485 && (n < 0
10486 || hpos + n > w->phys_cursor.hpos))
10487 w->phys_cursor_on_p = 0;
10488}
f451eb13
JB
10489
10490
06a2c219
GM
10491/* Set clipping for output in glyph row ROW. W is the window in which
10492 we operate. GC is the graphics context to set clipping in.
10493 WHOLE_LINE_P non-zero means include the areas used for truncation
10494 mark display and alike in the clipping rectangle.
10495
10496 ROW may be a text row or, e.g., a mode line. Text rows must be
10497 clipped to the interior of the window dedicated to text display,
10498 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10499
10500static void
06a2c219
GM
10501x_clip_to_row (w, row, gc, whole_line_p)
10502 struct window *w;
10503 struct glyph_row *row;
10504 GC gc;
10505 int whole_line_p;
dc6f92b8 10506{
06a2c219
GM
10507 struct frame *f = XFRAME (WINDOW_FRAME (w));
10508 XRectangle clip_rect;
10509 int window_x, window_y, window_width, window_height;
dc6f92b8 10510
06a2c219 10511 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10512
06a2c219
GM
10513 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10514 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10515 clip_rect.y = max (clip_rect.y, window_y);
10516 clip_rect.width = window_width;
10517 clip_rect.height = row->visible_height;
5c1aae96 10518
06a2c219
GM
10519 /* If clipping to the whole line, including trunc marks, extend
10520 the rectangle to the left and increase its width. */
10521 if (whole_line_p)
10522 {
110859fc
GM
10523 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10524 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10525 }
5c1aae96 10526
06a2c219 10527 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10528}
10529
06a2c219
GM
10530
10531/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10532
10533static void
06a2c219
GM
10534x_draw_hollow_cursor (w, row)
10535 struct window *w;
10536 struct glyph_row *row;
dc6f92b8 10537{
06a2c219
GM
10538 struct frame *f = XFRAME (WINDOW_FRAME (w));
10539 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10540 Display *dpy = FRAME_X_DISPLAY (f);
10541 int x, y, wd, h;
10542 XGCValues xgcv;
10543 struct glyph *cursor_glyph;
10544 GC gc;
10545
10546 /* Compute frame-relative coordinates from window-relative
10547 coordinates. */
10548 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10549 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10550 + row->ascent - w->phys_cursor_ascent);
10551 h = row->height - 1;
10552
10553 /* Get the glyph the cursor is on. If we can't tell because
10554 the current matrix is invalid or such, give up. */
10555 cursor_glyph = get_phys_cursor_glyph (w);
10556 if (cursor_glyph == NULL)
dc6f92b8
JB
10557 return;
10558
06a2c219
GM
10559 /* Compute the width of the rectangle to draw. If on a stretch
10560 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10561 rectangle as wide as the glyph, but use a canonical character
10562 width instead. */
10563 wd = cursor_glyph->pixel_width - 1;
10564 if (cursor_glyph->type == STRETCH_GLYPH
10565 && !x_stretch_cursor_p)
10566 wd = min (CANON_X_UNIT (f), wd);
10567
10568 /* The foreground of cursor_gc is typically the same as the normal
10569 background color, which can cause the cursor box to be invisible. */
10570 xgcv.foreground = f->output_data.x->cursor_pixel;
10571 if (dpyinfo->scratch_cursor_gc)
10572 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10573 else
10574 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10575 GCForeground, &xgcv);
10576 gc = dpyinfo->scratch_cursor_gc;
10577
10578 /* Set clipping, draw the rectangle, and reset clipping again. */
10579 x_clip_to_row (w, row, gc, 0);
10580 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10581 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10582}
10583
06a2c219
GM
10584
10585/* Draw a bar cursor on window W in glyph row ROW.
10586
10587 Implementation note: One would like to draw a bar cursor with an
10588 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10589 Unfortunately, I didn't find a font yet that has this property set.
10590 --gerd. */
dc6f92b8
JB
10591
10592static void
f02d8aa0 10593x_draw_bar_cursor (w, row, width)
06a2c219
GM
10594 struct window *w;
10595 struct glyph_row *row;
f02d8aa0 10596 int width;
dc6f92b8 10597{
92f424df
GM
10598 struct frame *f = XFRAME (w->frame);
10599 struct glyph *cursor_glyph;
10600 GC gc;
10601 int x;
10602 unsigned long mask;
10603 XGCValues xgcv;
10604 Display *dpy;
10605 Window window;
06a2c219 10606
92f424df
GM
10607 /* If cursor is out of bounds, don't draw garbage. This can happen
10608 in mini-buffer windows when switching between echo area glyphs
10609 and mini-buffer. */
10610 cursor_glyph = get_phys_cursor_glyph (w);
10611 if (cursor_glyph == NULL)
10612 return;
06a2c219 10613
92f424df
GM
10614 /* If on an image, draw like a normal cursor. That's usually better
10615 visible than drawing a bar, esp. if the image is large so that
10616 the bar might not be in the window. */
10617 if (cursor_glyph->type == IMAGE_GLYPH)
10618 {
10619 struct glyph_row *row;
10620 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10621 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10622 }
10623 else
10624 {
06a2c219
GM
10625 xgcv.background = f->output_data.x->cursor_pixel;
10626 xgcv.foreground = f->output_data.x->cursor_pixel;
10627 xgcv.graphics_exposures = 0;
10628 mask = GCForeground | GCBackground | GCGraphicsExposures;
10629 dpy = FRAME_X_DISPLAY (f);
10630 window = FRAME_X_WINDOW (f);
10631 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10632
06a2c219
GM
10633 if (gc)
10634 XChangeGC (dpy, gc, mask, &xgcv);
10635 else
10636 {
10637 gc = XCreateGC (dpy, window, mask, &xgcv);
10638 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10639 }
92f424df 10640
f02d8aa0
GM
10641 if (width < 0)
10642 width = f->output_data.x->cursor_width;
92f424df 10643
06a2c219
GM
10644 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10645 x_clip_to_row (w, row, gc, 0);
10646 XFillRectangle (dpy, window, gc,
10647 x,
10648 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10649 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10650 row->height);
10651 XSetClipMask (dpy, gc, None);
10652 }
dc6f92b8
JB
10653}
10654
06a2c219
GM
10655
10656/* Clear the cursor of window W to background color, and mark the
10657 cursor as not shown. This is used when the text where the cursor
10658 is is about to be rewritten. */
10659
dc6f92b8 10660static void
06a2c219
GM
10661x_clear_cursor (w)
10662 struct window *w;
dc6f92b8 10663{
06a2c219
GM
10664 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10665 x_update_window_cursor (w, 0);
10666}
90e65f07 10667
dbc4e1c1 10668
06a2c219
GM
10669/* Draw the cursor glyph of window W in glyph row ROW. See the
10670 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10671
06a2c219
GM
10672static void
10673x_draw_phys_cursor_glyph (w, row, hl)
10674 struct window *w;
10675 struct glyph_row *row;
10676 enum draw_glyphs_face hl;
10677{
10678 /* If cursor hpos is out of bounds, don't draw garbage. This can
10679 happen in mini-buffer windows when switching between echo area
10680 glyphs and mini-buffer. */
10681 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10682 {
10683 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10684 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10685 hl, 0, 0, 0);
10686
10687 /* When we erase the cursor, and ROW is overlapped by other
10688 rows, make sure that these overlapping parts of other rows
10689 are redrawn. */
10690 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10691 {
10692 if (row > w->current_matrix->rows
10693 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10694 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10695
10696 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10697 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10698 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10699 }
10700 }
06a2c219 10701}
dbc4e1c1 10702
eea6af04 10703
06a2c219 10704/* Erase the image of a cursor of window W from the screen. */
eea6af04 10705
06a2c219
GM
10706static void
10707x_erase_phys_cursor (w)
10708 struct window *w;
10709{
10710 struct frame *f = XFRAME (w->frame);
10711 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10712 int hpos = w->phys_cursor.hpos;
10713 int vpos = w->phys_cursor.vpos;
10714 int mouse_face_here_p = 0;
10715 struct glyph_matrix *active_glyphs = w->current_matrix;
10716 struct glyph_row *cursor_row;
10717 struct glyph *cursor_glyph;
10718 enum draw_glyphs_face hl;
10719
10720 /* No cursor displayed or row invalidated => nothing to do on the
10721 screen. */
10722 if (w->phys_cursor_type == NO_CURSOR)
10723 goto mark_cursor_off;
10724
10725 /* VPOS >= active_glyphs->nrows means that window has been resized.
10726 Don't bother to erase the cursor. */
10727 if (vpos >= active_glyphs->nrows)
10728 goto mark_cursor_off;
10729
10730 /* If row containing cursor is marked invalid, there is nothing we
10731 can do. */
10732 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10733 if (!cursor_row->enabled_p)
10734 goto mark_cursor_off;
10735
10736 /* This can happen when the new row is shorter than the old one.
10737 In this case, either x_draw_glyphs or clear_end_of_line
10738 should have cleared the cursor. Note that we wouldn't be
10739 able to erase the cursor in this case because we don't have a
10740 cursor glyph at hand. */
10741 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10742 goto mark_cursor_off;
10743
10744 /* If the cursor is in the mouse face area, redisplay that when
10745 we clear the cursor. */
8801a864
KR
10746 if (! NILP (dpyinfo->mouse_face_window)
10747 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10748 && (vpos > dpyinfo->mouse_face_beg_row
10749 || (vpos == dpyinfo->mouse_face_beg_row
10750 && hpos >= dpyinfo->mouse_face_beg_col))
10751 && (vpos < dpyinfo->mouse_face_end_row
10752 || (vpos == dpyinfo->mouse_face_end_row
10753 && hpos < dpyinfo->mouse_face_end_col))
10754 /* Don't redraw the cursor's spot in mouse face if it is at the
10755 end of a line (on a newline). The cursor appears there, but
10756 mouse highlighting does not. */
10757 && cursor_row->used[TEXT_AREA] > hpos)
10758 mouse_face_here_p = 1;
10759
10760 /* Maybe clear the display under the cursor. */
10761 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10762 {
10763 int x;
045dee35 10764 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10765
06a2c219
GM
10766 cursor_glyph = get_phys_cursor_glyph (w);
10767 if (cursor_glyph == NULL)
10768 goto mark_cursor_off;
dbc4e1c1 10769
06a2c219
GM
10770 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10771
10772 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10773 x,
045dee35 10774 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10775 cursor_row->y)),
10776 cursor_glyph->pixel_width,
10777 cursor_row->visible_height,
10778 False);
dbc4e1c1 10779 }
06a2c219
GM
10780
10781 /* Erase the cursor by redrawing the character underneath it. */
10782 if (mouse_face_here_p)
10783 hl = DRAW_MOUSE_FACE;
10784 else if (cursor_row->inverse_p)
10785 hl = DRAW_INVERSE_VIDEO;
10786 else
10787 hl = DRAW_NORMAL_TEXT;
10788 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10789
06a2c219
GM
10790 mark_cursor_off:
10791 w->phys_cursor_on_p = 0;
10792 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10793}
10794
10795
06a2c219
GM
10796/* Display or clear cursor of window W. If ON is zero, clear the
10797 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10798 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10799
06a2c219
GM
10800void
10801x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10802 struct window *w;
10803 int on, hpos, vpos, x, y;
dbc4e1c1 10804{
06a2c219
GM
10805 struct frame *f = XFRAME (w->frame);
10806 int new_cursor_type;
f02d8aa0 10807 int new_cursor_width;
06a2c219
GM
10808 struct glyph_matrix *current_glyphs;
10809 struct glyph_row *glyph_row;
10810 struct glyph *glyph;
dbc4e1c1 10811
49d838ea 10812 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10813 windows and frames; in the latter case, the frame or window may
10814 be in the midst of changing its size, and x and y may be off the
10815 window. */
10816 if (! FRAME_VISIBLE_P (f)
10817 || FRAME_GARBAGED_P (f)
10818 || vpos >= w->current_matrix->nrows
10819 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10820 return;
10821
10822 /* If cursor is off and we want it off, return quickly. */
06a2c219 10823 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10824 return;
10825
06a2c219
GM
10826 current_glyphs = w->current_matrix;
10827 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10828 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10829
10830 /* If cursor row is not enabled, we don't really know where to
10831 display the cursor. */
10832 if (!glyph_row->enabled_p)
10833 {
10834 w->phys_cursor_on_p = 0;
10835 return;
10836 }
10837
10838 xassert (interrupt_input_blocked);
10839
10840 /* Set new_cursor_type to the cursor we want to be displayed. In a
10841 mini-buffer window, we want the cursor only to appear if we are
10842 reading input from this window. For the selected window, we want
10843 the cursor type given by the frame parameter. If explicitly
10844 marked off, draw no cursor. In all other cases, we want a hollow
10845 box cursor. */
f02d8aa0 10846 new_cursor_width = -1;
9b4a7047
GM
10847 if (cursor_in_echo_area
10848 && FRAME_HAS_MINIBUF_P (f)
10849 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10850 {
9b4a7047
GM
10851 if (w == XWINDOW (echo_area_window))
10852 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10853 else
10854 new_cursor_type = HOLLOW_BOX_CURSOR;
10855 }
06a2c219 10856 else
9b4a7047
GM
10857 {
10858 if (w != XWINDOW (selected_window)
10859 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10860 {
e55a0b79
GM
10861 extern int cursor_in_non_selected_windows;
10862
5cefa566
GM
10863 if (MINI_WINDOW_P (w)
10864 || !cursor_in_non_selected_windows
10865 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
10866 new_cursor_type = NO_CURSOR;
10867 else
10868 new_cursor_type = HOLLOW_BOX_CURSOR;
10869 }
10870 else if (w->cursor_off_p)
10871 new_cursor_type = NO_CURSOR;
10872 else
f02d8aa0
GM
10873 {
10874 struct buffer *b = XBUFFER (w->buffer);
10875
10876 if (EQ (b->cursor_type, Qt))
10877 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10878 else
10879 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10880 &new_cursor_width);
10881 }
9b4a7047 10882 }
06a2c219
GM
10883
10884 /* If cursor is currently being shown and we don't want it to be or
10885 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10886 erase it. */
06a2c219 10887 if (w->phys_cursor_on_p
dc6f92b8 10888 && (!on
06a2c219
GM
10889 || w->phys_cursor.x != x
10890 || w->phys_cursor.y != y
10891 || new_cursor_type != w->phys_cursor_type))
10892 x_erase_phys_cursor (w);
10893
10894 /* If the cursor is now invisible and we want it to be visible,
10895 display it. */
10896 if (on && !w->phys_cursor_on_p)
10897 {
10898 w->phys_cursor_ascent = glyph_row->ascent;
10899 w->phys_cursor_height = glyph_row->height;
10900
10901 /* Set phys_cursor_.* before x_draw_.* is called because some
10902 of them may need the information. */
10903 w->phys_cursor.x = x;
10904 w->phys_cursor.y = glyph_row->y;
10905 w->phys_cursor.hpos = hpos;
10906 w->phys_cursor.vpos = vpos;
10907 w->phys_cursor_type = new_cursor_type;
10908 w->phys_cursor_on_p = 1;
10909
10910 switch (new_cursor_type)
dc6f92b8 10911 {
06a2c219
GM
10912 case HOLLOW_BOX_CURSOR:
10913 x_draw_hollow_cursor (w, glyph_row);
10914 break;
10915
10916 case FILLED_BOX_CURSOR:
10917 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10918 break;
10919
10920 case BAR_CURSOR:
f02d8aa0 10921 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10922 break;
10923
10924 case NO_CURSOR:
10925 break;
dc6f92b8 10926
06a2c219
GM
10927 default:
10928 abort ();
10929 }
59ddecde
GM
10930
10931#ifdef HAVE_X_I18N
10932 if (w == XWINDOW (f->selected_window))
10933 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10934 xic_set_preeditarea (w, x, y);
10935#endif
dc6f92b8
JB
10936 }
10937
06a2c219 10938#ifndef XFlush
f676886a 10939 if (updating_frame != f)
334208b7 10940 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10941#endif
dc6f92b8
JB
10942}
10943
06a2c219
GM
10944
10945/* Display the cursor on window W, or clear it. X and Y are window
10946 relative pixel coordinates. HPOS and VPOS are glyph matrix
10947 positions. If W is not the selected window, display a hollow
10948 cursor. ON non-zero means display the cursor at X, Y which
10949 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10950
dfcf069d 10951void
06a2c219
GM
10952x_display_cursor (w, on, hpos, vpos, x, y)
10953 struct window *w;
10954 int on, hpos, vpos, x, y;
dc6f92b8 10955{
f94397b5 10956 BLOCK_INPUT;
06a2c219 10957 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10958 UNBLOCK_INPUT;
10959}
10960
06a2c219
GM
10961
10962/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10963 Don't change the cursor's position. */
10964
dfcf069d 10965void
06a2c219 10966x_update_cursor (f, on_p)
5d46f928 10967 struct frame *f;
5d46f928 10968{
06a2c219
GM
10969 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10970}
10971
10972
10973/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10974 in the window tree rooted at W. */
10975
10976static void
10977x_update_cursor_in_window_tree (w, on_p)
10978 struct window *w;
10979 int on_p;
10980{
10981 while (w)
10982 {
10983 if (!NILP (w->hchild))
10984 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10985 else if (!NILP (w->vchild))
10986 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10987 else
10988 x_update_window_cursor (w, on_p);
10989
10990 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10991 }
10992}
5d46f928 10993
f94397b5 10994
06a2c219
GM
10995/* Switch the display of W's cursor on or off, according to the value
10996 of ON. */
10997
10998static void
10999x_update_window_cursor (w, on)
11000 struct window *w;
11001 int on;
11002{
16b5d424
GM
11003 /* Don't update cursor in windows whose frame is in the process
11004 of being deleted. */
11005 if (w->current_matrix)
11006 {
11007 BLOCK_INPUT;
11008 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11009 w->phys_cursor.x, w->phys_cursor.y);
11010 UNBLOCK_INPUT;
11011 }
dc6f92b8 11012}
06a2c219
GM
11013
11014
11015
dc6f92b8
JB
11016\f
11017/* Icons. */
11018
f676886a 11019/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11020 when we get an expose event for it. */
dc6f92b8 11021
dfcf069d 11022void
f676886a
JB
11023refreshicon (f)
11024 struct frame *f;
dc6f92b8 11025{
06a2c219 11026 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11027}
11028
dbc4e1c1 11029/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11030
11031int
990ba854 11032x_bitmap_icon (f, file)
f676886a 11033 struct frame *f;
990ba854 11034 Lisp_Object file;
dc6f92b8 11035{
06a2c219 11036 int bitmap_id;
dc6f92b8 11037
c118dd06 11038 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11039 return 1;
11040
990ba854 11041 /* Free up our existing icon bitmap if any. */
7556890b
RS
11042 if (f->output_data.x->icon_bitmap > 0)
11043 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11044 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11045
11046 if (STRINGP (file))
7f2ae036
RS
11047 bitmap_id = x_create_bitmap_from_file (f, file);
11048 else
11049 {
990ba854 11050 /* Create the GNU bitmap if necessary. */
5bf01b68 11051 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11052 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11053 = x_create_bitmap_from_data (f, gnu_bits,
11054 gnu_width, gnu_height);
990ba854
RS
11055
11056 /* The first time we create the GNU bitmap,
06a2c219 11057 this increments the ref-count one extra time.
990ba854
RS
11058 As a result, the GNU bitmap is never freed.
11059 That way, we don't have to worry about allocating it again. */
334208b7 11060 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11061
334208b7 11062 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11063 }
11064
11065 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11066 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11067
11068 return 0;
11069}
11070
11071
1be2d067
KH
11072/* Make the x-window of frame F use a rectangle with text.
11073 Use ICON_NAME as the text. */
dc6f92b8
JB
11074
11075int
f676886a
JB
11076x_text_icon (f, icon_name)
11077 struct frame *f;
dc6f92b8
JB
11078 char *icon_name;
11079{
c118dd06 11080 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11081 return 1;
11082
1be2d067
KH
11083#ifdef HAVE_X11R4
11084 {
11085 XTextProperty text;
11086 text.value = (unsigned char *) icon_name;
11087 text.encoding = XA_STRING;
11088 text.format = 8;
11089 text.nitems = strlen (icon_name);
11090#ifdef USE_X_TOOLKIT
7556890b 11091 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11092 &text);
11093#else /* not USE_X_TOOLKIT */
11094 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11095#endif /* not USE_X_TOOLKIT */
11096 }
11097#else /* not HAVE_X11R4 */
11098 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11099#endif /* not HAVE_X11R4 */
58769bee 11100
7556890b
RS
11101 if (f->output_data.x->icon_bitmap > 0)
11102 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11103 f->output_data.x->icon_bitmap = 0;
b1c884c3 11104 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11105
11106 return 0;
11107}
11108\f
e99db5a1
RS
11109#define X_ERROR_MESSAGE_SIZE 200
11110
11111/* If non-nil, this should be a string.
11112 It means catch X errors and store the error message in this string. */
11113
11114static Lisp_Object x_error_message_string;
11115
11116/* An X error handler which stores the error message in
11117 x_error_message_string. This is called from x_error_handler if
11118 x_catch_errors is in effect. */
11119
06a2c219 11120static void
e99db5a1
RS
11121x_error_catcher (display, error)
11122 Display *display;
11123 XErrorEvent *error;
11124{
11125 XGetErrorText (display, error->error_code,
11126 XSTRING (x_error_message_string)->data,
11127 X_ERROR_MESSAGE_SIZE);
11128}
11129
11130/* Begin trapping X errors for display DPY. Actually we trap X errors
11131 for all displays, but DPY should be the display you are actually
11132 operating on.
11133
11134 After calling this function, X protocol errors no longer cause
11135 Emacs to exit; instead, they are recorded in the string
11136 stored in x_error_message_string.
11137
11138 Calling x_check_errors signals an Emacs error if an X error has
11139 occurred since the last call to x_catch_errors or x_check_errors.
11140
11141 Calling x_uncatch_errors resumes the normal error handling. */
11142
11143void x_check_errors ();
11144static Lisp_Object x_catch_errors_unwind ();
11145
11146int
11147x_catch_errors (dpy)
11148 Display *dpy;
11149{
11150 int count = specpdl_ptr - specpdl;
11151
11152 /* Make sure any errors from previous requests have been dealt with. */
11153 XSync (dpy, False);
11154
11155 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11156
11157 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11158 XSTRING (x_error_message_string)->data[0] = 0;
11159
11160 return count;
11161}
11162
11163/* Unbind the binding that we made to check for X errors. */
11164
11165static Lisp_Object
11166x_catch_errors_unwind (old_val)
11167 Lisp_Object old_val;
11168{
11169 x_error_message_string = old_val;
11170 return Qnil;
11171}
11172
11173/* If any X protocol errors have arrived since the last call to
11174 x_catch_errors or x_check_errors, signal an Emacs error using
11175 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11176
11177void
11178x_check_errors (dpy, format)
11179 Display *dpy;
11180 char *format;
11181{
11182 /* Make sure to catch any errors incurred so far. */
11183 XSync (dpy, False);
11184
11185 if (XSTRING (x_error_message_string)->data[0])
11186 error (format, XSTRING (x_error_message_string)->data);
11187}
11188
9829ddba
RS
11189/* Nonzero if we had any X protocol errors
11190 since we did x_catch_errors on DPY. */
e99db5a1
RS
11191
11192int
11193x_had_errors_p (dpy)
11194 Display *dpy;
11195{
11196 /* Make sure to catch any errors incurred so far. */
11197 XSync (dpy, False);
11198
11199 return XSTRING (x_error_message_string)->data[0] != 0;
11200}
11201
9829ddba
RS
11202/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11203
06a2c219 11204void
9829ddba
RS
11205x_clear_errors (dpy)
11206 Display *dpy;
11207{
11208 XSTRING (x_error_message_string)->data[0] = 0;
11209}
11210
e99db5a1
RS
11211/* Stop catching X protocol errors and let them make Emacs die.
11212 DPY should be the display that was passed to x_catch_errors.
11213 COUNT should be the value that was returned by
11214 the corresponding call to x_catch_errors. */
11215
11216void
11217x_uncatch_errors (dpy, count)
11218 Display *dpy;
11219 int count;
11220{
11221 unbind_to (count, Qnil);
11222}
11223
11224#if 0
11225static unsigned int x_wire_count;
11226x_trace_wire ()
11227{
11228 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11229}
11230#endif /* ! 0 */
11231
11232\f
11233/* Handle SIGPIPE, which can happen when the connection to a server
11234 simply goes away. SIGPIPE is handled by x_connection_signal.
11235 Don't need to do anything, because the write which caused the
11236 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11237 which will do the appropriate cleanup for us. */
e99db5a1
RS
11238
11239static SIGTYPE
11240x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11241 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11242{
11243#ifdef USG
11244 /* USG systems forget handlers when they are used;
11245 must reestablish each time */
11246 signal (signalnum, x_connection_signal);
11247#endif /* USG */
11248}
0da1ab50 11249
e99db5a1 11250\f
0da1ab50
GM
11251/************************************************************************
11252 Handling X errors
11253 ************************************************************************/
4746118a 11254
0da1ab50
GM
11255/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11256 the text of an error message that lead to the connection loss. */
16bd92ea 11257
4746118a 11258static SIGTYPE
5978125e
GM
11259x_connection_closed (dpy, error_message)
11260 Display *dpy;
7a13e894 11261 char *error_message;
4746118a 11262{
5978125e 11263 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 11264 Lisp_Object frame, tail;
0da1ab50
GM
11265 int count;
11266 char *msg;
11267
11268 msg = (char *) alloca (strlen (error_message) + 1);
11269 strcpy (msg, error_message);
1a532e54
GM
11270 handling_signal = 0;
11271
0da1ab50
GM
11272 /* Prevent being called recursively because of an error condition
11273 below. Otherwise, we might end up with printing ``can't find per
11274 display information'' in the recursive call instead of printing
11275 the original message here. */
11276 count = x_catch_errors (dpy);
11277
8a4f36cc
GM
11278 /* We have to close the display to inform Xt that it doesn't
11279 exist anymore. If we don't, Xt will continue to wait for
11280 events from the display. As a consequence, a sequence of
11281
11282 M-x make-frame-on-display RET :1 RET
11283 ...kill the new frame, so that we get an IO error...
11284 M-x make-frame-on-display RET :1 RET
11285
11286 will indefinitely wait in Xt for events for display `:1', opened
11287 in the first class to make-frame-on-display.
6186a4a0 11288
8a4f36cc
GM
11289 Closing the display is reported to lead to a bus error on
11290 OpenWindows in certain situations. I suspect that is a bug
11291 in OpenWindows. I don't know how to cicumvent it here. */
11292
f613a4c8 11293#ifdef USE_X_TOOLKIT
ae24cb3b
GM
11294 /* If DPYINFO is null, this means we didn't open the display
11295 in the first place, so don't try to close it. */
11296 if (dpyinfo)
11297 XtCloseDisplay (dpy);
f613a4c8 11298#endif
adabc3a9 11299
8a4f36cc 11300 /* Indicate that this display is dead. */
9e80b57d
KR
11301 if (dpyinfo)
11302 dpyinfo->display = 0;
6186a4a0 11303
06a2c219 11304 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11305 that are on the dead display. */
11306 FOR_EACH_FRAME (tail, frame)
11307 {
11308 Lisp_Object minibuf_frame;
11309 minibuf_frame
11310 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11311 if (FRAME_X_P (XFRAME (frame))
11312 && FRAME_X_P (XFRAME (minibuf_frame))
11313 && ! EQ (frame, minibuf_frame)
11314 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11315 Fdelete_frame (frame, Qt);
11316 }
11317
11318 /* Now delete all remaining frames on the dead display.
06a2c219 11319 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11320 for another frame that we need to delete. */
11321 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11322 if (FRAME_X_P (XFRAME (frame))
11323 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11324 {
11325 /* Set this to t so that Fdelete_frame won't get confused
11326 trying to find a replacement. */
11327 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11328 Fdelete_frame (frame, Qt);
11329 }
7a13e894 11330
482a1bd2
KH
11331 if (dpyinfo)
11332 x_delete_display (dpyinfo);
7a13e894 11333
0da1ab50
GM
11334 x_uncatch_errors (dpy, count);
11335
7a13e894
RS
11336 if (x_display_list == 0)
11337 {
0da1ab50 11338 fprintf (stderr, "%s\n", msg);
7a13e894
RS
11339 shut_down_emacs (0, 0, Qnil);
11340 exit (70);
11341 }
12ba150f 11342
7a13e894
RS
11343 /* Ordinary stack unwind doesn't deal with these. */
11344#ifdef SIGIO
11345 sigunblock (sigmask (SIGIO));
11346#endif
11347 sigunblock (sigmask (SIGALRM));
11348 TOTALLY_UNBLOCK_INPUT;
11349
aa4d9a9e 11350 clear_waiting_for_input ();
0da1ab50 11351 error ("%s", msg);
4746118a
JB
11352}
11353
0da1ab50 11354
7a13e894
RS
11355/* This is the usual handler for X protocol errors.
11356 It kills all frames on the display that we got the error for.
11357 If that was the only one, it prints an error message and kills Emacs. */
11358
06a2c219 11359static void
c118dd06
JB
11360x_error_quitter (display, error)
11361 Display *display;
11362 XErrorEvent *error;
11363{
7a13e894 11364 char buf[256], buf1[356];
dc6f92b8 11365
58769bee 11366 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11367 original error handler. */
dc6f92b8 11368
c118dd06 11369 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11370 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11371 buf, error->request_code);
7a13e894 11372 x_connection_closed (display, buf1);
dc6f92b8
JB
11373}
11374
0da1ab50 11375
e99db5a1
RS
11376/* This is the first-level handler for X protocol errors.
11377 It calls x_error_quitter or x_error_catcher. */
7a13e894 11378
8922af5f 11379static int
e99db5a1 11380x_error_handler (display, error)
8922af5f 11381 Display *display;
e99db5a1 11382 XErrorEvent *error;
8922af5f 11383{
e99db5a1
RS
11384 if (! NILP (x_error_message_string))
11385 x_error_catcher (display, error);
11386 else
11387 x_error_quitter (display, error);
06a2c219 11388 return 0;
f9e24cb9 11389}
c118dd06 11390
e99db5a1
RS
11391/* This is the handler for X IO errors, always.
11392 It kills all frames on the display that we lost touch with.
11393 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11394
c118dd06 11395static int
e99db5a1 11396x_io_error_quitter (display)
c118dd06 11397 Display *display;
c118dd06 11398{
e99db5a1 11399 char buf[256];
dc6f92b8 11400
e99db5a1
RS
11401 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11402 x_connection_closed (display, buf);
06a2c219 11403 return 0;
dc6f92b8 11404}
dc6f92b8 11405\f
f451eb13
JB
11406/* Changing the font of the frame. */
11407
76bcdf39
RS
11408/* Give frame F the font named FONTNAME as its default font, and
11409 return the full name of that font. FONTNAME may be a wildcard
11410 pattern; in that case, we choose some font that fits the pattern.
11411 The return value shows which font we chose. */
11412
b5cf7a0e 11413Lisp_Object
f676886a
JB
11414x_new_font (f, fontname)
11415 struct frame *f;
dc6f92b8
JB
11416 register char *fontname;
11417{
dc43ef94 11418 struct font_info *fontp
ee569018 11419 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11420
dc43ef94
KH
11421 if (!fontp)
11422 return Qnil;
2224a5fc 11423
dc43ef94 11424 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11425 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11426 f->output_data.x->fontset = -1;
11427
b2cad826
KH
11428 /* Compute the scroll bar width in character columns. */
11429 if (f->scroll_bar_pixel_width > 0)
11430 {
7556890b 11431 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11432 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11433 }
11434 else
4e61bddf
RS
11435 {
11436 int wid = FONT_WIDTH (f->output_data.x->font);
11437 f->scroll_bar_cols = (14 + wid - 1) / wid;
11438 }
b2cad826 11439
f676886a 11440 /* Now make the frame display the given font. */
c118dd06 11441 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11442 {
7556890b
RS
11443 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11444 f->output_data.x->font->fid);
11445 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11446 f->output_data.x->font->fid);
11447 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11448 f->output_data.x->font->fid);
f676886a 11449
a27f9f86 11450 frame_update_line_height (f);
0134a210 11451 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11452 }
a27f9f86
RS
11453 else
11454 /* If we are setting a new frame's font for the first time,
11455 there are no faces yet, so this font's height is the line height. */
7556890b 11456 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11457
dc43ef94
KH
11458 return build_string (fontp->full_name);
11459}
11460
11461/* Give frame F the fontset named FONTSETNAME as its default font, and
11462 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11463 pattern; in that case, we choose some fontset that fits the pattern.
11464 The return value shows which fontset we chose. */
b5cf7a0e 11465
dc43ef94
KH
11466Lisp_Object
11467x_new_fontset (f, fontsetname)
11468 struct frame *f;
11469 char *fontsetname;
11470{
ee569018 11471 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11472 Lisp_Object result;
b5cf7a0e 11473
dc43ef94
KH
11474 if (fontset < 0)
11475 return Qnil;
b5cf7a0e 11476
2da424f1
KH
11477 if (f->output_data.x->fontset == fontset)
11478 /* This fontset is already set in frame F. There's nothing more
11479 to do. */
ee569018 11480 return fontset_name (fontset);
dc43ef94 11481
ee569018 11482 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11483
11484 if (!STRINGP (result))
11485 /* Can't load ASCII font. */
11486 return Qnil;
11487
11488 /* Since x_new_font doesn't update any fontset information, do it now. */
11489 f->output_data.x->fontset = fontset;
dc43ef94 11490
f5d11644
GM
11491#ifdef HAVE_X_I18N
11492 if (FRAME_XIC (f)
11493 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11494 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11495#endif
11496
dc43ef94 11497 return build_string (fontsetname);
dc6f92b8 11498}
f5d11644
GM
11499
11500\f
11501/***********************************************************************
11502 X Input Methods
11503 ***********************************************************************/
11504
11505#ifdef HAVE_X_I18N
11506
11507#ifdef HAVE_X11R6
11508
11509/* XIM destroy callback function, which is called whenever the
11510 connection to input method XIM dies. CLIENT_DATA contains a
11511 pointer to the x_display_info structure corresponding to XIM. */
11512
11513static void
11514xim_destroy_callback (xim, client_data, call_data)
11515 XIM xim;
11516 XPointer client_data;
11517 XPointer call_data;
11518{
11519 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11520 Lisp_Object frame, tail;
11521
11522 BLOCK_INPUT;
11523
11524 /* No need to call XDestroyIC.. */
11525 FOR_EACH_FRAME (tail, frame)
11526 {
11527 struct frame *f = XFRAME (frame);
11528 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11529 {
11530 FRAME_XIC (f) = NULL;
11531 if (FRAME_XIC_FONTSET (f))
11532 {
11533 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11534 FRAME_XIC_FONTSET (f) = NULL;
11535 }
11536 }
11537 }
11538
11539 /* No need to call XCloseIM. */
11540 dpyinfo->xim = NULL;
11541 XFree (dpyinfo->xim_styles);
11542 UNBLOCK_INPUT;
11543}
11544
11545#endif /* HAVE_X11R6 */
11546
11547/* Open the connection to the XIM server on display DPYINFO.
11548 RESOURCE_NAME is the resource name Emacs uses. */
11549
11550static void
11551xim_open_dpy (dpyinfo, resource_name)
11552 struct x_display_info *dpyinfo;
11553 char *resource_name;
11554{
287f7dd6 11555#ifdef USE_XIM
f5d11644
GM
11556 XIM xim;
11557
11558 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11559 dpyinfo->xim = xim;
11560
11561 if (xim)
11562 {
f5d11644
GM
11563#ifdef HAVE_X11R6
11564 XIMCallback destroy;
11565#endif
11566
11567 /* Get supported styles and XIM values. */
11568 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11569
11570#ifdef HAVE_X11R6
11571 destroy.callback = xim_destroy_callback;
11572 destroy.client_data = (XPointer)dpyinfo;
68642df6 11573 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11574 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11575#endif
11576 }
287f7dd6
GM
11577
11578#else /* not USE_XIM */
11579 dpyinfo->xim = NULL;
11580#endif /* not USE_XIM */
f5d11644
GM
11581}
11582
11583
b9de836c 11584#ifdef HAVE_X11R6_XIM
f5d11644
GM
11585
11586struct xim_inst_t
11587{
11588 struct x_display_info *dpyinfo;
11589 char *resource_name;
11590};
11591
11592/* XIM instantiate callback function, which is called whenever an XIM
11593 server is available. DISPLAY is teh display of the XIM.
11594 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11595 when the callback was registered. */
11596
11597static void
11598xim_instantiate_callback (display, client_data, call_data)
11599 Display *display;
11600 XPointer client_data;
11601 XPointer call_data;
11602{
11603 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11604 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11605
11606 /* We don't support multiple XIM connections. */
11607 if (dpyinfo->xim)
11608 return;
11609
11610 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11611
11612 /* Create XIC for the existing frames on the same display, as long
11613 as they have no XIC. */
11614 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11615 {
11616 Lisp_Object tail, frame;
11617
11618 BLOCK_INPUT;
11619 FOR_EACH_FRAME (tail, frame)
11620 {
11621 struct frame *f = XFRAME (frame);
11622
11623 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11624 if (FRAME_XIC (f) == NULL)
11625 {
11626 create_frame_xic (f);
11627 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11628 xic_set_statusarea (f);
11629 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11630 {
11631 struct window *w = XWINDOW (f->selected_window);
11632 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11633 }
11634 }
11635 }
11636
11637 UNBLOCK_INPUT;
11638 }
11639}
11640
b9de836c 11641#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11642
11643
11644/* Open a connection to the XIM server on display DPYINFO.
11645 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11646 connection only at the first time. On X11R6, open the connection
11647 in the XIM instantiate callback function. */
11648
11649static void
11650xim_initialize (dpyinfo, resource_name)
11651 struct x_display_info *dpyinfo;
11652 char *resource_name;
11653{
287f7dd6 11654#ifdef USE_XIM
b9de836c 11655#ifdef HAVE_X11R6_XIM
f5d11644
GM
11656 struct xim_inst_t *xim_inst;
11657 int len;
11658
11659 dpyinfo->xim = NULL;
11660 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11661 xim_inst->dpyinfo = dpyinfo;
11662 len = strlen (resource_name);
11663 xim_inst->resource_name = (char *) xmalloc (len + 1);
11664 bcopy (resource_name, xim_inst->resource_name, len + 1);
11665 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11666 resource_name, EMACS_CLASS,
11667 xim_instantiate_callback,
2ebb2f8b
DL
11668 /* Fixme: This is XPointer in
11669 XFree86 but (XPointer *) on
11670 Tru64, at least. */
11671 (XPointer) xim_inst);
b9de836c 11672#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11673 dpyinfo->xim = NULL;
11674 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11675#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11676
11677#else /* not USE_XIM */
11678 dpyinfo->xim = NULL;
11679#endif /* not USE_XIM */
f5d11644
GM
11680}
11681
11682
11683/* Close the connection to the XIM server on display DPYINFO. */
11684
11685static void
11686xim_close_dpy (dpyinfo)
11687 struct x_display_info *dpyinfo;
11688{
287f7dd6 11689#ifdef USE_XIM
b9de836c 11690#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
11691 if (dpyinfo->display)
11692 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11693 NULL, EMACS_CLASS,
11694 xim_instantiate_callback, NULL);
b9de836c 11695#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
11696 if (dpyinfo->display)
11697 XCloseIM (dpyinfo->xim);
f5d11644
GM
11698 dpyinfo->xim = NULL;
11699 XFree (dpyinfo->xim_styles);
287f7dd6 11700#endif /* USE_XIM */
f5d11644
GM
11701}
11702
b9de836c 11703#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11704
11705
dc6f92b8 11706\f
2e365682
RS
11707/* Calculate the absolute position in frame F
11708 from its current recorded position values and gravity. */
11709
dfcf069d 11710void
43bca5d5 11711x_calc_absolute_position (f)
f676886a 11712 struct frame *f;
dc6f92b8 11713{
06a2c219 11714 Window child;
6dba1858 11715 int win_x = 0, win_y = 0;
7556890b 11716 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11717 int this_window;
11718
9829ddba
RS
11719 /* We have nothing to do if the current position
11720 is already for the top-left corner. */
11721 if (! ((flags & XNegative) || (flags & YNegative)))
11722 return;
11723
c81412a0 11724#ifdef USE_X_TOOLKIT
7556890b 11725 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11726#else
11727 this_window = FRAME_X_WINDOW (f);
11728#endif
6dba1858
RS
11729
11730 /* Find the position of the outside upper-left corner of
9829ddba
RS
11731 the inner window, with respect to the outer window.
11732 But do this only if we will need the results. */
7556890b 11733 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11734 {
9829ddba
RS
11735 int count;
11736
6dba1858 11737 BLOCK_INPUT;
9829ddba
RS
11738 count = x_catch_errors (FRAME_X_DISPLAY (f));
11739 while (1)
11740 {
11741 x_clear_errors (FRAME_X_DISPLAY (f));
11742 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11743
11744 /* From-window, to-window. */
11745 this_window,
11746 f->output_data.x->parent_desc,
11747
11748 /* From-position, to-position. */
11749 0, 0, &win_x, &win_y,
11750
11751 /* Child of win. */
11752 &child);
11753 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11754 {
11755 Window newroot, newparent = 0xdeadbeef;
11756 Window *newchildren;
2ebb2f8b 11757 unsigned int nchildren;
9829ddba
RS
11758
11759 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11760 &newparent, &newchildren, &nchildren))
11761 break;
58769bee 11762
7c3c78a3 11763 XFree ((char *) newchildren);
6dba1858 11764
9829ddba
RS
11765 f->output_data.x->parent_desc = newparent;
11766 }
11767 else
11768 break;
11769 }
6dba1858 11770
9829ddba 11771 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11772 UNBLOCK_INPUT;
11773 }
11774
11775 /* Treat negative positions as relative to the leftmost bottommost
11776 position that fits on the screen. */
20f55f9a 11777 if (flags & XNegative)
7556890b 11778 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11779 - 2 * f->output_data.x->border_width - win_x
11780 - PIXEL_WIDTH (f)
11781 + f->output_data.x->left_pos);
dc6f92b8 11782
7708ced0
GM
11783 {
11784 int height = PIXEL_HEIGHT (f);
06a2c219 11785
7708ced0
GM
11786#if defined USE_X_TOOLKIT && defined USE_MOTIF
11787 /* Something is fishy here. When using Motif, starting Emacs with
11788 `-g -0-0', the frame appears too low by a few pixels.
11789
11790 This seems to be so because initially, while Emacs is starting,
11791 the column widget's height and the frame's pixel height are
11792 different. The column widget's height is the right one. In
11793 later invocations, when Emacs is up, the frame's pixel height
11794 is right, though.
11795
11796 It's not obvious where the initial small difference comes from.
11797 2000-12-01, gerd. */
11798
11799 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 11800#endif
2e365682 11801
7708ced0
GM
11802 if (flags & YNegative)
11803 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11804 - 2 * f->output_data.x->border_width
11805 - win_y
11806 - height
11807 + f->output_data.x->top_pos);
11808 }
11809
3a35ab44
RS
11810 /* The left_pos and top_pos
11811 are now relative to the top and left screen edges,
11812 so the flags should correspond. */
7556890b 11813 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11814}
11815
3a35ab44
RS
11816/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11817 to really change the position, and 0 when calling from
11818 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11819 position values). It is -1 when calling from x_set_frame_parameters,
11820 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11821
dfcf069d 11822void
dc05a16b 11823x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11824 struct frame *f;
dc6f92b8 11825 register int xoff, yoff;
dc05a16b 11826 int change_gravity;
dc6f92b8 11827{
4a4cbdd5
KH
11828 int modified_top, modified_left;
11829
aa3ff7c9 11830 if (change_gravity > 0)
3a35ab44 11831 {
7556890b
RS
11832 f->output_data.x->top_pos = yoff;
11833 f->output_data.x->left_pos = xoff;
11834 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11835 if (xoff < 0)
7556890b 11836 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11837 if (yoff < 0)
7556890b
RS
11838 f->output_data.x->size_hint_flags |= YNegative;
11839 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11840 }
43bca5d5 11841 x_calc_absolute_position (f);
dc6f92b8
JB
11842
11843 BLOCK_INPUT;
c32cdd9a 11844 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11845
7556890b
RS
11846 modified_left = f->output_data.x->left_pos;
11847 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11848#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11849 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11850 /* It is a mystery why we need to add the border_width here
11851 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11852 if (change_gravity != 0)
4a4cbdd5 11853 {
7556890b
RS
11854 modified_left += f->output_data.x->border_width;
11855 modified_top += f->output_data.x->border_width;
4a4cbdd5 11856 }
e73ec6fa 11857#endif
4a4cbdd5 11858
3afe33e7 11859#ifdef USE_X_TOOLKIT
7556890b 11860 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11861 modified_left, modified_top);
3afe33e7 11862#else /* not USE_X_TOOLKIT */
334208b7 11863 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11864 modified_left, modified_top);
3afe33e7 11865#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11866 UNBLOCK_INPUT;
11867}
11868
bc20ebbf
FP
11869/* Call this to change the size of frame F's x-window.
11870 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11871 for this size change and subsequent size changes.
11872 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11873
dfcf069d 11874void
bc20ebbf 11875x_set_window_size (f, change_gravity, cols, rows)
f676886a 11876 struct frame *f;
bc20ebbf 11877 int change_gravity;
b1c884c3 11878 int cols, rows;
dc6f92b8 11879{
06a2c219 11880#ifndef USE_X_TOOLKIT
dc6f92b8 11881 int pixelwidth, pixelheight;
06a2c219 11882#endif
dc6f92b8 11883
80fd1fe2 11884 BLOCK_INPUT;
aee9a898
RS
11885
11886#ifdef USE_X_TOOLKIT
3a20653d
RS
11887 {
11888 /* The x and y position of the widget is clobbered by the
11889 call to XtSetValues within EmacsFrameSetCharSize.
11890 This is a real kludge, but I don't understand Xt so I can't
11891 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11892 int xpos = f->output_data.x->widget->core.x;
11893 int ypos = f->output_data.x->widget->core.y;
11894 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11895 f->output_data.x->widget->core.x = xpos;
11896 f->output_data.x->widget->core.y = ypos;
3a20653d 11897 }
80fd1fe2
FP
11898
11899#else /* not USE_X_TOOLKIT */
11900
b1c884c3 11901 check_frame_size (f, &rows, &cols);
7556890b 11902 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11903 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11904 ? 0
11905 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11906 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11907 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11908 f->output_data.x->flags_areas_extra
110859fc 11909 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11910 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11911 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11912
7556890b 11913 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11914 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11915
334208b7
RS
11916 XSync (FRAME_X_DISPLAY (f), False);
11917 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11918 pixelwidth, pixelheight);
b1c884c3
JB
11919
11920 /* Now, strictly speaking, we can't be sure that this is accurate,
11921 but the window manager will get around to dealing with the size
11922 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11923 ConfigureNotify event gets here.
11924
11925 We could just not bother storing any of this information here,
11926 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11927 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11928 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11929 point in the future when the ConfigureNotify event arrives.
11930
11931 We pass 1 for DELAY since we can't run Lisp code inside of
11932 a BLOCK_INPUT. */
7d1e984f 11933 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11934 PIXEL_WIDTH (f) = pixelwidth;
11935 PIXEL_HEIGHT (f) = pixelheight;
11936
aee9a898
RS
11937 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11938 receive in the ConfigureNotify event; if we get what we asked
11939 for, then the event won't cause the screen to become garbaged, so
11940 we have to make sure to do it here. */
11941 SET_FRAME_GARBAGED (f);
11942
11943 XFlush (FRAME_X_DISPLAY (f));
11944
11945#endif /* not USE_X_TOOLKIT */
11946
4d73d038 11947 /* If cursor was outside the new size, mark it as off. */
06a2c219 11948 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11949
aee9a898
RS
11950 /* Clear out any recollection of where the mouse highlighting was,
11951 since it might be in a place that's outside the new frame size.
11952 Actually checking whether it is outside is a pain in the neck,
11953 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11954 cancel_mouse_face (f);
dbc4e1c1 11955
dc6f92b8
JB
11956 UNBLOCK_INPUT;
11957}
dc6f92b8 11958\f
d047c4eb 11959/* Mouse warping. */
dc6f92b8 11960
9b378208 11961void
f676886a
JB
11962x_set_mouse_position (f, x, y)
11963 struct frame *f;
dc6f92b8
JB
11964 int x, y;
11965{
11966 int pix_x, pix_y;
11967
7556890b
RS
11968 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11969 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11970
11971 if (pix_x < 0) pix_x = 0;
11972 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11973
11974 if (pix_y < 0) pix_y = 0;
11975 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11976
11977 BLOCK_INPUT;
dc6f92b8 11978
334208b7
RS
11979 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11980 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11981 UNBLOCK_INPUT;
11982}
11983
9b378208
RS
11984/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11985
11986void
11987x_set_mouse_pixel_position (f, pix_x, pix_y)
11988 struct frame *f;
11989 int pix_x, pix_y;
11990{
11991 BLOCK_INPUT;
11992
334208b7
RS
11993 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11994 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11995 UNBLOCK_INPUT;
11996}
d047c4eb
KH
11997\f
11998/* focus shifting, raising and lowering. */
9b378208 11999
dfcf069d 12000void
f676886a
JB
12001x_focus_on_frame (f)
12002 struct frame *f;
dc6f92b8 12003{
1fb20991 12004#if 0 /* This proves to be unpleasant. */
f676886a 12005 x_raise_frame (f);
1fb20991 12006#endif
6d4238f3
JB
12007#if 0
12008 /* I don't think that the ICCCM allows programs to do things like this
12009 without the interaction of the window manager. Whatever you end up
f676886a 12010 doing with this code, do it to x_unfocus_frame too. */
334208b7 12011 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 12012 RevertToPointerRoot, CurrentTime);
c118dd06 12013#endif /* ! 0 */
dc6f92b8
JB
12014}
12015
dfcf069d 12016void
f676886a
JB
12017x_unfocus_frame (f)
12018 struct frame *f;
dc6f92b8 12019{
6d4238f3 12020#if 0
f676886a 12021 /* Look at the remarks in x_focus_on_frame. */
0f941935 12022 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 12023 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 12024 RevertToPointerRoot, CurrentTime);
c118dd06 12025#endif /* ! 0 */
dc6f92b8
JB
12026}
12027
f676886a 12028/* Raise frame F. */
dc6f92b8 12029
dfcf069d 12030void
f676886a
JB
12031x_raise_frame (f)
12032 struct frame *f;
dc6f92b8 12033{
3a88c238 12034 if (f->async_visible)
dc6f92b8
JB
12035 {
12036 BLOCK_INPUT;
3afe33e7 12037#ifdef USE_X_TOOLKIT
7556890b 12038 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12039#else /* not USE_X_TOOLKIT */
334208b7 12040 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12041#endif /* not USE_X_TOOLKIT */
334208b7 12042 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12043 UNBLOCK_INPUT;
12044 }
12045}
12046
f676886a 12047/* Lower frame F. */
dc6f92b8 12048
dfcf069d 12049void
f676886a
JB
12050x_lower_frame (f)
12051 struct frame *f;
dc6f92b8 12052{
3a88c238 12053 if (f->async_visible)
dc6f92b8
JB
12054 {
12055 BLOCK_INPUT;
3afe33e7 12056#ifdef USE_X_TOOLKIT
7556890b 12057 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12058#else /* not USE_X_TOOLKIT */
334208b7 12059 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12060#endif /* not USE_X_TOOLKIT */
334208b7 12061 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12062 UNBLOCK_INPUT;
12063 }
12064}
12065
dbc4e1c1 12066static void
6b0442dc 12067XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12068 FRAME_PTR f;
6b0442dc 12069 int raise_flag;
dbc4e1c1 12070{
6b0442dc 12071 if (raise_flag)
dbc4e1c1
JB
12072 x_raise_frame (f);
12073 else
12074 x_lower_frame (f);
12075}
d047c4eb
KH
12076\f
12077/* Change of visibility. */
dc6f92b8 12078
9382638d
KH
12079/* This tries to wait until the frame is really visible.
12080 However, if the window manager asks the user where to position
12081 the frame, this will return before the user finishes doing that.
12082 The frame will not actually be visible at that time,
12083 but it will become visible later when the window manager
12084 finishes with it. */
12085
dfcf069d 12086void
f676886a
JB
12087x_make_frame_visible (f)
12088 struct frame *f;
dc6f92b8 12089{
990ba854 12090 Lisp_Object type;
1aa6072f 12091 int original_top, original_left;
31be9251
GM
12092 int retry_count = 2;
12093
12094 retry:
dc6f92b8 12095
dc6f92b8 12096 BLOCK_INPUT;
dc6f92b8 12097
990ba854
RS
12098 type = x_icon_type (f);
12099 if (!NILP (type))
12100 x_bitmap_icon (f, type);
bdcd49ba 12101
f676886a 12102 if (! FRAME_VISIBLE_P (f))
90e65f07 12103 {
1aa6072f
RS
12104 /* We test FRAME_GARBAGED_P here to make sure we don't
12105 call x_set_offset a second time
12106 if we get to x_make_frame_visible a second time
12107 before the window gets really visible. */
12108 if (! FRAME_ICONIFIED_P (f)
12109 && ! f->output_data.x->asked_for_visible)
12110 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12111
12112 f->output_data.x->asked_for_visible = 1;
12113
90e65f07 12114 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12115 x_wm_set_window_state (f, NormalState);
3afe33e7 12116#ifdef USE_X_TOOLKIT
d7a38a2e 12117 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12118 XtMapWidget (f->output_data.x->widget);
3afe33e7 12119#else /* not USE_X_TOOLKIT */
7f9c7f94 12120 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12121#endif /* not USE_X_TOOLKIT */
0134a210
RS
12122#if 0 /* This seems to bring back scroll bars in the wrong places
12123 if the window configuration has changed. They seem
12124 to come back ok without this. */
ab648270 12125 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12126 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12127#endif
90e65f07 12128 }
dc6f92b8 12129
334208b7 12130 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12131
0dacf791
RS
12132 /* Synchronize to ensure Emacs knows the frame is visible
12133 before we do anything else. We do this loop with input not blocked
12134 so that incoming events are handled. */
12135 {
12136 Lisp_Object frame;
12ce2351 12137 int count;
28c01ffe
RS
12138 /* This must be before UNBLOCK_INPUT
12139 since events that arrive in response to the actions above
12140 will set it when they are handled. */
12141 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12142
12143 original_left = f->output_data.x->left_pos;
12144 original_top = f->output_data.x->top_pos;
c0a04927
RS
12145
12146 /* This must come after we set COUNT. */
12147 UNBLOCK_INPUT;
12148
2745e6c4 12149 /* We unblock here so that arriving X events are processed. */
1aa6072f 12150
dcb07ae9
RS
12151 /* Now move the window back to where it was "supposed to be".
12152 But don't do it if the gravity is negative.
12153 When the gravity is negative, this uses a position
28c01ffe
RS
12154 that is 3 pixels too low. Perhaps that's really the border width.
12155
12156 Don't do this if the window has never been visible before,
12157 because the window manager may choose the position
12158 and we don't want to override it. */
1aa6072f 12159
4d3f5d9a 12160 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12161 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12162 && previously_visible)
1aa6072f 12163 {
2745e6c4
RS
12164 Drawable rootw;
12165 int x, y;
12166 unsigned int width, height, border, depth;
06a2c219 12167
1aa6072f 12168 BLOCK_INPUT;
9829ddba 12169
06a2c219
GM
12170 /* On some window managers (such as FVWM) moving an existing
12171 window, even to the same place, causes the window manager
12172 to introduce an offset. This can cause the window to move
12173 to an unexpected location. Check the geometry (a little
12174 slow here) and then verify that the window is in the right
12175 place. If the window is not in the right place, move it
12176 there, and take the potential window manager hit. */
2745e6c4
RS
12177 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12178 &rootw, &x, &y, &width, &height, &border, &depth);
12179
12180 if (original_left != x || original_top != y)
12181 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12182 original_left, original_top);
12183
1aa6072f
RS
12184 UNBLOCK_INPUT;
12185 }
9829ddba 12186
e0c1aef2 12187 XSETFRAME (frame, f);
c0a04927 12188
12ce2351
GM
12189 /* Wait until the frame is visible. Process X events until a
12190 MapNotify event has been seen, or until we think we won't get a
12191 MapNotify at all.. */
12192 for (count = input_signal_count + 10;
12193 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12194 {
12ce2351 12195 /* Force processing of queued events. */
334208b7 12196 x_sync (f);
12ce2351
GM
12197
12198 /* Machines that do polling rather than SIGIO have been
12199 observed to go into a busy-wait here. So we'll fake an
12200 alarm signal to let the handler know that there's something
12201 to be read. We used to raise a real alarm, but it seems
12202 that the handler isn't always enabled here. This is
12203 probably a bug. */
8b2f8d4e 12204 if (input_polling_used ())
3b2fa4e6 12205 {
12ce2351
GM
12206 /* It could be confusing if a real alarm arrives while
12207 processing the fake one. Turn it off and let the
12208 handler reset it. */
3e71d8f2 12209 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12210 int old_poll_suppress_count = poll_suppress_count;
12211 poll_suppress_count = 1;
12212 poll_for_input_1 ();
12213 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12214 }
12ce2351
GM
12215
12216 /* See if a MapNotify event has been processed. */
12217 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12218 }
31be9251
GM
12219
12220 /* 2000-09-28: In
12221
12222 (let ((f (selected-frame)))
12223 (iconify-frame f)
12224 (raise-frame f))
12225
12226 the frame is not raised with various window managers on
12227 FreeBSD, Linux and Solaris. It turns out that, for some
12228 unknown reason, the call to XtMapWidget is completely ignored.
12229 Mapping the widget a second time works. */
12230
12231 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12232 goto retry;
0dacf791 12233 }
dc6f92b8
JB
12234}
12235
06a2c219 12236/* Change from mapped state to withdrawn state. */
dc6f92b8 12237
d047c4eb
KH
12238/* Make the frame visible (mapped and not iconified). */
12239
dfcf069d 12240void
f676886a
JB
12241x_make_frame_invisible (f)
12242 struct frame *f;
dc6f92b8 12243{
546e6d5b
RS
12244 Window window;
12245
12246#ifdef USE_X_TOOLKIT
12247 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12248 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12249#else /* not USE_X_TOOLKIT */
12250 window = FRAME_X_WINDOW (f);
12251#endif /* not USE_X_TOOLKIT */
dc6f92b8 12252
9319ae23 12253 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12254 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12255 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12256
5627c40e 12257#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12258 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12259 return;
5627c40e 12260#endif
dc6f92b8
JB
12261
12262 BLOCK_INPUT;
c118dd06 12263
af31d76f
RS
12264 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12265 that the current position of the window is user-specified, rather than
12266 program-specified, so that when the window is mapped again, it will be
12267 placed at the same location, without forcing the user to position it
12268 by hand again (they have already done that once for this window.) */
c32cdd9a 12269 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12270
c118dd06
JB
12271#ifdef HAVE_X11R4
12272
334208b7
RS
12273 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12274 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12275 {
12276 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12277 error ("Can't notify window manager of window withdrawal");
c118dd06 12278 }
c118dd06 12279#else /* ! defined (HAVE_X11R4) */
16bd92ea 12280
c118dd06 12281 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12282 if (! EQ (Vx_no_window_manager, Qt))
12283 {
16bd92ea 12284 XEvent unmap;
dc6f92b8 12285
16bd92ea 12286 unmap.xunmap.type = UnmapNotify;
546e6d5b 12287 unmap.xunmap.window = window;
334208b7 12288 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12289 unmap.xunmap.from_configure = False;
334208b7
RS
12290 if (! XSendEvent (FRAME_X_DISPLAY (f),
12291 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12292 False,
06a2c219 12293 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12294 &unmap))
12295 {
12296 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12297 error ("Can't notify window manager of withdrawal");
16bd92ea 12298 }
dc6f92b8
JB
12299 }
12300
16bd92ea 12301 /* Unmap the window ourselves. Cheeky! */
334208b7 12302 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12303#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12304
5627c40e
RS
12305 /* We can't distinguish this from iconification
12306 just by the event that we get from the server.
12307 So we can't win using the usual strategy of letting
12308 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12309 and synchronize with the server to make sure we agree. */
12310 f->visible = 0;
12311 FRAME_ICONIFIED_P (f) = 0;
12312 f->async_visible = 0;
12313 f->async_iconified = 0;
12314
334208b7 12315 x_sync (f);
5627c40e 12316
dc6f92b8
JB
12317 UNBLOCK_INPUT;
12318}
12319
06a2c219 12320/* Change window state from mapped to iconified. */
dc6f92b8 12321
dfcf069d 12322void
f676886a
JB
12323x_iconify_frame (f)
12324 struct frame *f;
dc6f92b8 12325{
3afe33e7 12326 int result;
990ba854 12327 Lisp_Object type;
dc6f92b8 12328
9319ae23 12329 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12330 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12331 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12332
3a88c238 12333 if (f->async_iconified)
dc6f92b8
JB
12334 return;
12335
3afe33e7 12336 BLOCK_INPUT;
546e6d5b 12337
9af3143a
RS
12338 FRAME_SAMPLE_VISIBILITY (f);
12339
990ba854
RS
12340 type = x_icon_type (f);
12341 if (!NILP (type))
12342 x_bitmap_icon (f, type);
bdcd49ba
RS
12343
12344#ifdef USE_X_TOOLKIT
12345
546e6d5b
RS
12346 if (! FRAME_VISIBLE_P (f))
12347 {
12348 if (! EQ (Vx_no_window_manager, Qt))
12349 x_wm_set_window_state (f, IconicState);
12350 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12351 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12352 /* The server won't give us any event to indicate
12353 that an invisible frame was changed to an icon,
12354 so we have to record it here. */
12355 f->iconified = 1;
1e6bc770 12356 f->visible = 1;
9cf30a30 12357 f->async_iconified = 1;
1e6bc770 12358 f->async_visible = 0;
546e6d5b
RS
12359 UNBLOCK_INPUT;
12360 return;
12361 }
12362
334208b7 12363 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12364 XtWindow (f->output_data.x->widget),
334208b7 12365 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12366 UNBLOCK_INPUT;
12367
12368 if (!result)
546e6d5b 12369 error ("Can't notify window manager of iconification");
3afe33e7
RS
12370
12371 f->async_iconified = 1;
1e6bc770
RS
12372 f->async_visible = 0;
12373
8c002a25
KH
12374
12375 BLOCK_INPUT;
334208b7 12376 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12377 UNBLOCK_INPUT;
3afe33e7
RS
12378#else /* not USE_X_TOOLKIT */
12379
fd13dbb2
RS
12380 /* Make sure the X server knows where the window should be positioned,
12381 in case the user deiconifies with the window manager. */
12382 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12383 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12384
16bd92ea
JB
12385 /* Since we don't know which revision of X we're running, we'll use both
12386 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12387
12388 /* X11R4: send a ClientMessage to the window manager using the
12389 WM_CHANGE_STATE type. */
12390 {
12391 XEvent message;
58769bee 12392
c118dd06 12393 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12394 message.xclient.type = ClientMessage;
334208b7 12395 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12396 message.xclient.format = 32;
12397 message.xclient.data.l[0] = IconicState;
12398
334208b7
RS
12399 if (! XSendEvent (FRAME_X_DISPLAY (f),
12400 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12401 False,
12402 SubstructureRedirectMask | SubstructureNotifyMask,
12403 &message))
dc6f92b8
JB
12404 {
12405 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12406 error ("Can't notify window manager of iconification");
dc6f92b8 12407 }
16bd92ea 12408 }
dc6f92b8 12409
58769bee 12410 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12411 IconicState. */
12412 x_wm_set_window_state (f, IconicState);
dc6f92b8 12413
a9c00105
RS
12414 if (!FRAME_VISIBLE_P (f))
12415 {
12416 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12417 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12418 }
12419
3a88c238 12420 f->async_iconified = 1;
1e6bc770 12421 f->async_visible = 0;
dc6f92b8 12422
334208b7 12423 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12424 UNBLOCK_INPUT;
8c002a25 12425#endif /* not USE_X_TOOLKIT */
dc6f92b8 12426}
19f71add 12427
d047c4eb 12428\f
19f71add 12429/* Free X resources of frame F. */
dc6f92b8 12430
dfcf069d 12431void
19f71add 12432x_free_frame_resources (f)
f676886a 12433 struct frame *f;
dc6f92b8 12434{
7f9c7f94
RS
12435 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12436
dc6f92b8 12437 BLOCK_INPUT;
c0ff3fab 12438
6186a4a0
RS
12439 /* If a display connection is dead, don't try sending more
12440 commands to the X server. */
19f71add 12441 if (dpyinfo->display)
6186a4a0 12442 {
19f71add 12443 if (f->output_data.x->icon_desc)
6186a4a0 12444 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
19f71add 12445
31f41daf 12446#ifdef HAVE_X_I18N
f5d11644
GM
12447 if (FRAME_XIC (f))
12448 free_frame_xic (f);
31f41daf 12449#endif
19f71add 12450
2662734b 12451 if (FRAME_X_WINDOW (f))
19f71add
GM
12452 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12453
3afe33e7 12454#ifdef USE_X_TOOLKIT
06a2c219
GM
12455 if (f->output_data.x->widget)
12456 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12457 free_frame_menubar (f);
3afe33e7
RS
12458#endif /* USE_X_TOOLKIT */
12459
3e71d8f2
GM
12460 unload_color (f, f->output_data.x->foreground_pixel);
12461 unload_color (f, f->output_data.x->background_pixel);
12462 unload_color (f, f->output_data.x->cursor_pixel);
12463 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12464 unload_color (f, f->output_data.x->border_pixel);
12465 unload_color (f, f->output_data.x->mouse_pixel);
19f71add 12466
3e71d8f2
GM
12467 if (f->output_data.x->scroll_bar_background_pixel != -1)
12468 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12469 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12470 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12471 if (f->output_data.x->white_relief.allocated_p)
12472 unload_color (f, f->output_data.x->white_relief.pixel);
12473 if (f->output_data.x->black_relief.allocated_p)
12474 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 12475
19f71add
GM
12476 if (FRAME_FACE_CACHE (f))
12477 free_frame_faces (f);
12478
4ca78676 12479 x_free_gcs (f);
6186a4a0
RS
12480 XFlush (FRAME_X_DISPLAY (f));
12481 }
dc6f92b8 12482
df89d8a4 12483 if (f->output_data.x->saved_menu_event)
06a2c219 12484 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12485
7556890b 12486 xfree (f->output_data.x);
19f71add
GM
12487 f->output_data.x = NULL;
12488
0f941935
KH
12489 if (f == dpyinfo->x_focus_frame)
12490 dpyinfo->x_focus_frame = 0;
12491 if (f == dpyinfo->x_focus_event_frame)
12492 dpyinfo->x_focus_event_frame = 0;
12493 if (f == dpyinfo->x_highlight_frame)
12494 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12495
7f9c7f94 12496 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12497 {
7f9c7f94
RS
12498 dpyinfo->mouse_face_beg_row
12499 = dpyinfo->mouse_face_beg_col = -1;
12500 dpyinfo->mouse_face_end_row
12501 = dpyinfo->mouse_face_end_col = -1;
12502 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12503 dpyinfo->mouse_face_deferred_gc = 0;
12504 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12505 }
0134a210 12506
c0ff3fab 12507 UNBLOCK_INPUT;
dc6f92b8 12508}
19f71add
GM
12509
12510
12511/* Destroy the X window of frame F. */
12512
12513void
12514x_destroy_window (f)
12515 struct frame *f;
12516{
12517 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12518
12519 /* If a display connection is dead, don't try sending more
12520 commands to the X server. */
12521 if (dpyinfo->display != 0)
12522 x_free_frame_resources (f);
12523
12524 dpyinfo->reference_count--;
12525}
12526
dc6f92b8 12527\f
f451eb13
JB
12528/* Setting window manager hints. */
12529
af31d76f
RS
12530/* Set the normal size hints for the window manager, for frame F.
12531 FLAGS is the flags word to use--or 0 meaning preserve the flags
12532 that the window now has.
12533 If USER_POSITION is nonzero, we set the USPosition
12534 flag (this is useful when FLAGS is 0). */
6dba1858 12535
dfcf069d 12536void
af31d76f 12537x_wm_set_size_hint (f, flags, user_position)
f676886a 12538 struct frame *f;
af31d76f
RS
12539 long flags;
12540 int user_position;
dc6f92b8
JB
12541{
12542 XSizeHints size_hints;
3afe33e7
RS
12543
12544#ifdef USE_X_TOOLKIT
7e4f2521
FP
12545 Arg al[2];
12546 int ac = 0;
12547 Dimension widget_width, widget_height;
7556890b 12548 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12549#else /* not USE_X_TOOLKIT */
c118dd06 12550 Window window = FRAME_X_WINDOW (f);
3afe33e7 12551#endif /* not USE_X_TOOLKIT */
dc6f92b8 12552
b72a58fd
RS
12553 /* Setting PMaxSize caused various problems. */
12554 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12555
7556890b
RS
12556 size_hints.x = f->output_data.x->left_pos;
12557 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12558
7e4f2521
FP
12559#ifdef USE_X_TOOLKIT
12560 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12561 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12562 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12563 size_hints.height = widget_height;
12564 size_hints.width = widget_width;
12565#else /* not USE_X_TOOLKIT */
f676886a
JB
12566 size_hints.height = PIXEL_HEIGHT (f);
12567 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12568#endif /* not USE_X_TOOLKIT */
7553a6b7 12569
7556890b
RS
12570 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12571 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12572 size_hints.max_width
12573 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12574 size_hints.max_height
12575 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12576
d067ea8b
KH
12577 /* Calculate the base and minimum sizes.
12578
12579 (When we use the X toolkit, we don't do it here.
12580 Instead we copy the values that the widgets are using, below.) */
12581#ifndef USE_X_TOOLKIT
b1c884c3 12582 {
b0342f17 12583 int base_width, base_height;
0134a210 12584 int min_rows = 0, min_cols = 0;
b0342f17 12585
f451eb13
JB
12586 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12587 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12588
0134a210 12589 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12590
0134a210
RS
12591 /* The window manager uses the base width hints to calculate the
12592 current number of rows and columns in the frame while
12593 resizing; min_width and min_height aren't useful for this
12594 purpose, since they might not give the dimensions for a
12595 zero-row, zero-column frame.
58769bee 12596
0134a210
RS
12597 We use the base_width and base_height members if we have
12598 them; otherwise, we set the min_width and min_height members
12599 to the size for a zero x zero frame. */
b0342f17
JB
12600
12601#ifdef HAVE_X11R4
0134a210
RS
12602 size_hints.flags |= PBaseSize;
12603 size_hints.base_width = base_width;
12604 size_hints.base_height = base_height;
12605 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12606 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12607#else
0134a210
RS
12608 size_hints.min_width = base_width;
12609 size_hints.min_height = base_height;
b0342f17 12610#endif
b1c884c3 12611 }
dc6f92b8 12612
d067ea8b 12613 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12614 if (flags)
dc6f92b8 12615 {
d067ea8b
KH
12616 size_hints.flags |= flags;
12617 goto no_read;
12618 }
12619#endif /* not USE_X_TOOLKIT */
12620
12621 {
12622 XSizeHints hints; /* Sometimes I hate X Windows... */
12623 long supplied_return;
12624 int value;
af31d76f
RS
12625
12626#ifdef HAVE_X11R4
d067ea8b
KH
12627 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12628 &supplied_return);
af31d76f 12629#else
d067ea8b 12630 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12631#endif
58769bee 12632
d067ea8b
KH
12633#ifdef USE_X_TOOLKIT
12634 size_hints.base_height = hints.base_height;
12635 size_hints.base_width = hints.base_width;
12636 size_hints.min_height = hints.min_height;
12637 size_hints.min_width = hints.min_width;
12638#endif
12639
12640 if (flags)
12641 size_hints.flags |= flags;
12642 else
12643 {
12644 if (value == 0)
12645 hints.flags = 0;
12646 if (hints.flags & PSize)
12647 size_hints.flags |= PSize;
12648 if (hints.flags & PPosition)
12649 size_hints.flags |= PPosition;
12650 if (hints.flags & USPosition)
12651 size_hints.flags |= USPosition;
12652 if (hints.flags & USSize)
12653 size_hints.flags |= USSize;
12654 }
12655 }
12656
06a2c219 12657#ifndef USE_X_TOOLKIT
d067ea8b 12658 no_read:
06a2c219 12659#endif
0134a210 12660
af31d76f 12661#ifdef PWinGravity
7556890b 12662 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12663 size_hints.flags |= PWinGravity;
dc05a16b 12664
af31d76f 12665 if (user_position)
6dba1858 12666 {
af31d76f
RS
12667 size_hints.flags &= ~ PPosition;
12668 size_hints.flags |= USPosition;
6dba1858 12669 }
2554751d 12670#endif /* PWinGravity */
6dba1858 12671
b0342f17 12672#ifdef HAVE_X11R4
334208b7 12673 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12674#else
334208b7 12675 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12676#endif
dc6f92b8
JB
12677}
12678
12679/* Used for IconicState or NormalState */
06a2c219 12680
dfcf069d 12681void
f676886a
JB
12682x_wm_set_window_state (f, state)
12683 struct frame *f;
dc6f92b8
JB
12684 int state;
12685{
3afe33e7 12686#ifdef USE_X_TOOLKIT
546e6d5b
RS
12687 Arg al[1];
12688
12689 XtSetArg (al[0], XtNinitialState, state);
7556890b 12690 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12691#else /* not USE_X_TOOLKIT */
c118dd06 12692 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12693
7556890b
RS
12694 f->output_data.x->wm_hints.flags |= StateHint;
12695 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12696
7556890b 12697 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12698#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12699}
12700
dfcf069d 12701void
7f2ae036 12702x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12703 struct frame *f;
7f2ae036 12704 int pixmap_id;
dc6f92b8 12705{
d2bd6bc4
RS
12706 Pixmap icon_pixmap;
12707
06a2c219 12708#ifndef USE_X_TOOLKIT
c118dd06 12709 Window window = FRAME_X_WINDOW (f);
75231bad 12710#endif
dc6f92b8 12711
7f2ae036 12712 if (pixmap_id > 0)
dbc4e1c1 12713 {
d2bd6bc4 12714 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12715 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12716 }
12717 else
68568555
RS
12718 {
12719 /* It seems there is no way to turn off use of an icon pixmap.
12720 The following line does it, only if no icon has yet been created,
12721 for some window managers. But with mwm it crashes.
12722 Some people say it should clear the IconPixmapHint bit in this case,
12723 but that doesn't work, and the X consortium said it isn't the
12724 right thing at all. Since there is no way to win,
12725 best to explicitly give up. */
12726#if 0
12727 f->output_data.x->wm_hints.icon_pixmap = None;
12728#else
12729 return;
12730#endif
12731 }
b1c884c3 12732
d2bd6bc4
RS
12733#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12734
12735 {
12736 Arg al[1];
12737 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12738 XtSetValues (f->output_data.x->widget, al, 1);
12739 }
12740
12741#else /* not USE_X_TOOLKIT */
12742
7556890b
RS
12743 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12744 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12745
12746#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12747}
12748
dfcf069d 12749void
f676886a
JB
12750x_wm_set_icon_position (f, icon_x, icon_y)
12751 struct frame *f;
dc6f92b8
JB
12752 int icon_x, icon_y;
12753{
75231bad 12754#ifdef USE_X_TOOLKIT
7556890b 12755 Window window = XtWindow (f->output_data.x->widget);
75231bad 12756#else
c118dd06 12757 Window window = FRAME_X_WINDOW (f);
75231bad 12758#endif
dc6f92b8 12759
7556890b
RS
12760 f->output_data.x->wm_hints.flags |= IconPositionHint;
12761 f->output_data.x->wm_hints.icon_x = icon_x;
12762 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12763
7556890b 12764 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12765}
12766
12767\f
06a2c219
GM
12768/***********************************************************************
12769 Fonts
12770 ***********************************************************************/
dc43ef94
KH
12771
12772/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12773
dc43ef94
KH
12774struct font_info *
12775x_get_font_info (f, font_idx)
12776 FRAME_PTR f;
12777 int font_idx;
12778{
12779 return (FRAME_X_FONT_TABLE (f) + font_idx);
12780}
12781
12782
9c11f79e
GM
12783/* Return a list of names of available fonts matching PATTERN on frame F.
12784
12785 If SIZE is > 0, it is the size (maximum bounds width) of fonts
12786 to be listed.
12787
12788 SIZE < 0 means include scalable fonts.
12789
12790 Frame F null means we have not yet created any frame on X, and
12791 consult the first display in x_display_list. MAXNAMES sets a limit
12792 on how many fonts to match. */
dc43ef94
KH
12793
12794Lisp_Object
12795x_list_fonts (f, pattern, size, maxnames)
9c11f79e 12796 struct frame *f;
dc43ef94
KH
12797 Lisp_Object pattern;
12798 int size;
12799 int maxnames;
12800{
06a2c219
GM
12801 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12802 Lisp_Object tem, second_best;
9c11f79e
GM
12803 struct x_display_info *dpyinfo
12804 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
12805 Display *dpy = dpyinfo->display;
09c6077f 12806 int try_XLoadQueryFont = 0;
53ca4657 12807 int count;
9c11f79e
GM
12808 int allow_scalable_fonts_p = 0;
12809
12810 if (size < 0)
12811 {
12812 allow_scalable_fonts_p = 1;
12813 size = 0;
12814 }
dc43ef94 12815
6b0efe73 12816 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12817 if (NILP (patterns))
12818 patterns = Fcons (pattern, Qnil);
81ba44e5 12819
09c6077f
KH
12820 if (maxnames == 1 && !size)
12821 /* We can return any single font matching PATTERN. */
12822 try_XLoadQueryFont = 1;
9a32686f 12823
8e713be6 12824 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12825 {
dc43ef94 12826 int num_fonts;
3e71d8f2 12827 char **names = NULL;
dc43ef94 12828
8e713be6 12829 pattern = XCAR (patterns);
536f4067
RS
12830 /* See if we cached the result for this particular query.
12831 The cache is an alist of the form:
9c11f79e
GM
12832 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
12833 tem = XCDR (dpyinfo->name_list_element);
12834 key = Fcons (Fcons (pattern, make_number (maxnames)),
12835 allow_scalable_fonts_p ? Qt : Qnil);
12836 list = Fassoc (key, tem);
12837 if (!NILP (list))
b5210ea7
KH
12838 {
12839 list = Fcdr_safe (list);
12840 /* We have a cashed list. Don't have to get the list again. */
12841 goto label_cached;
12842 }
12843
12844 /* At first, put PATTERN in the cache. */
09c6077f 12845
dc43ef94 12846 BLOCK_INPUT;
17d85edc
KH
12847 count = x_catch_errors (dpy);
12848
09c6077f
KH
12849 if (try_XLoadQueryFont)
12850 {
12851 XFontStruct *font;
12852 unsigned long value;
12853
12854 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12855 if (x_had_errors_p (dpy))
12856 {
12857 /* This error is perhaps due to insufficient memory on X
12858 server. Let's just ignore it. */
12859 font = NULL;
12860 x_clear_errors (dpy);
12861 }
12862
09c6077f
KH
12863 if (font
12864 && XGetFontProperty (font, XA_FONT, &value))
12865 {
12866 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12867 int len = strlen (name);
01c752b5 12868 char *tmp;
09c6077f 12869
6f6512e8
KH
12870 /* If DXPC (a Differential X Protocol Compressor)
12871 Ver.3.7 is running, XGetAtomName will return null
12872 string. We must avoid such a name. */
12873 if (len == 0)
12874 try_XLoadQueryFont = 0;
12875 else
12876 {
12877 num_fonts = 1;
12878 names = (char **) alloca (sizeof (char *));
12879 /* Some systems only allow alloca assigned to a
12880 simple var. */
12881 tmp = (char *) alloca (len + 1); names[0] = tmp;
12882 bcopy (name, names[0], len + 1);
12883 XFree (name);
12884 }
09c6077f
KH
12885 }
12886 else
12887 try_XLoadQueryFont = 0;
a083fd23
RS
12888
12889 if (font)
12890 XFreeFont (dpy, font);
09c6077f
KH
12891 }
12892
12893 if (!try_XLoadQueryFont)
17d85edc
KH
12894 {
12895 /* We try at least 10 fonts because XListFonts will return
12896 auto-scaled fonts at the head. */
12897 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12898 &num_fonts);
12899 if (x_had_errors_p (dpy))
12900 {
12901 /* This error is perhaps due to insufficient memory on X
12902 server. Let's just ignore it. */
12903 names = NULL;
12904 x_clear_errors (dpy);
12905 }
12906 }
12907
12908 x_uncatch_errors (dpy, count);
dc43ef94
KH
12909 UNBLOCK_INPUT;
12910
12911 if (names)
12912 {
12913 int i;
dc43ef94
KH
12914
12915 /* Make a list of all the fonts we got back.
12916 Store that in the font cache for the display. */
12917 for (i = 0; i < num_fonts; i++)
12918 {
06a2c219 12919 int width = 0;
dc43ef94 12920 char *p = names[i];
06a2c219
GM
12921 int average_width = -1, dashes = 0;
12922
dc43ef94 12923 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
12924 14 dashes, and the field value following 12th dash
12925 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12926 is usually too ugly to be used for editing. Let's
12927 ignore it. */
dc43ef94
KH
12928 while (*p)
12929 if (*p++ == '-')
12930 {
12931 dashes++;
12932 if (dashes == 7) /* PIXEL_SIZE field */
12933 width = atoi (p);
12934 else if (dashes == 12) /* AVERAGE_WIDTH field */
12935 average_width = atoi (p);
12936 }
9c11f79e
GM
12937
12938 if (allow_scalable_fonts_p
12939 || dashes < 14 || average_width != 0)
dc43ef94
KH
12940 {
12941 tem = build_string (names[i]);
12942 if (NILP (Fassoc (tem, list)))
12943 {
12944 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12945 && ((fast_c_string_match_ignore_case
12946 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12947 >= 0))
12948 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12949 width of this font. */
dc43ef94
KH
12950 list = Fcons (Fcons (tem, make_number (width)), list);
12951 else
12952 /* For the moment, width is not known. */
12953 list = Fcons (Fcons (tem, Qnil), list);
12954 }
12955 }
12956 }
09c6077f
KH
12957 if (!try_XLoadQueryFont)
12958 XFreeFontNames (names);
dc43ef94
KH
12959 }
12960
b5210ea7 12961 /* Now store the result in the cache. */
9c11f79e
GM
12962 XCDR (dpyinfo->name_list_element)
12963 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
dc43ef94 12964
b5210ea7
KH
12965 label_cached:
12966 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12967
b5210ea7
KH
12968 newlist = second_best = Qnil;
12969 /* Make a list of the fonts that have the right width. */
8e713be6 12970 for (; CONSP (list); list = XCDR (list))
b5210ea7 12971 {
536f4067
RS
12972 int found_size;
12973
8e713be6 12974 tem = XCAR (list);
dc43ef94 12975
8e713be6 12976 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12977 continue;
12978 if (!size)
12979 {
8e713be6 12980 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12981 continue;
12982 }
dc43ef94 12983
8e713be6 12984 if (!INTEGERP (XCDR (tem)))
dc43ef94 12985 {
b5210ea7 12986 /* Since we have not yet known the size of this font, we
9c11f79e 12987 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12988 XFontStruct *thisinfo;
12989
12990 BLOCK_INPUT;
17d85edc 12991 count = x_catch_errors (dpy);
dc43ef94 12992 thisinfo = XLoadQueryFont (dpy,
8e713be6 12993 XSTRING (XCAR (tem))->data);
17d85edc
KH
12994 if (x_had_errors_p (dpy))
12995 {
12996 /* This error is perhaps due to insufficient memory on X
12997 server. Let's just ignore it. */
12998 thisinfo = NULL;
12999 x_clear_errors (dpy);
13000 }
13001 x_uncatch_errors (dpy, count);
dc43ef94
KH
13002 UNBLOCK_INPUT;
13003
13004 if (thisinfo)
13005 {
8e713be6 13006 XCDR (tem)
536f4067
RS
13007 = (thisinfo->min_bounds.width == 0
13008 ? make_number (0)
13009 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
13010 XFreeFont (dpy, thisinfo);
13011 }
13012 else
b5210ea7 13013 /* For unknown reason, the previous call of XListFont had
06a2c219 13014 returned a font which can't be opened. Record the size
b5210ea7 13015 as 0 not to try to open it again. */
8e713be6 13016 XCDR (tem) = make_number (0);
dc43ef94 13017 }
536f4067 13018
8e713be6 13019 found_size = XINT (XCDR (tem));
536f4067 13020 if (found_size == size)
8e713be6 13021 newlist = Fcons (XCAR (tem), newlist);
536f4067 13022 else if (found_size > 0)
b5210ea7 13023 {
536f4067 13024 if (NILP (second_best))
b5210ea7 13025 second_best = tem;
536f4067
RS
13026 else if (found_size < size)
13027 {
8e713be6
KR
13028 if (XINT (XCDR (second_best)) > size
13029 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
13030 second_best = tem;
13031 }
13032 else
13033 {
8e713be6
KR
13034 if (XINT (XCDR (second_best)) > size
13035 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
13036 second_best = tem;
13037 }
b5210ea7
KH
13038 }
13039 }
13040 if (!NILP (newlist))
13041 break;
13042 else if (!NILP (second_best))
13043 {
8e713be6 13044 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 13045 break;
dc43ef94 13046 }
dc43ef94
KH
13047 }
13048
13049 return newlist;
13050}
13051
06a2c219
GM
13052
13053#if GLYPH_DEBUG
13054
13055/* Check that FONT is valid on frame F. It is if it can be found in F's
13056 font table. */
13057
13058static void
13059x_check_font (f, font)
13060 struct frame *f;
13061 XFontStruct *font;
13062{
13063 int i;
13064 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13065
13066 xassert (font != NULL);
13067
13068 for (i = 0; i < dpyinfo->n_fonts; i++)
13069 if (dpyinfo->font_table[i].name
13070 && font == dpyinfo->font_table[i].font)
13071 break;
13072
13073 xassert (i < dpyinfo->n_fonts);
13074}
13075
13076#endif /* GLYPH_DEBUG != 0 */
13077
13078/* Set *W to the minimum width, *H to the minimum font height of FONT.
13079 Note: There are (broken) X fonts out there with invalid XFontStruct
13080 min_bounds contents. For example, handa@etl.go.jp reports that
13081 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13082 have font->min_bounds.width == 0. */
13083
13084static INLINE void
13085x_font_min_bounds (font, w, h)
13086 XFontStruct *font;
13087 int *w, *h;
13088{
13089 *h = FONT_HEIGHT (font);
13090 *w = font->min_bounds.width;
13091
13092 /* Try to handle the case where FONT->min_bounds has invalid
13093 contents. Since the only font known to have invalid min_bounds
13094 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13095 if (*w <= 0)
13096 *w = font->max_bounds.width;
13097}
13098
13099
13100/* Compute the smallest character width and smallest font height over
13101 all fonts available on frame F. Set the members smallest_char_width
13102 and smallest_font_height in F's x_display_info structure to
13103 the values computed. Value is non-zero if smallest_font_height or
13104 smallest_char_width become smaller than they were before. */
13105
13106static int
13107x_compute_min_glyph_bounds (f)
13108 struct frame *f;
13109{
13110 int i;
13111 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13112 XFontStruct *font;
13113 int old_width = dpyinfo->smallest_char_width;
13114 int old_height = dpyinfo->smallest_font_height;
13115
13116 dpyinfo->smallest_font_height = 100000;
13117 dpyinfo->smallest_char_width = 100000;
13118
13119 for (i = 0; i < dpyinfo->n_fonts; ++i)
13120 if (dpyinfo->font_table[i].name)
13121 {
13122 struct font_info *fontp = dpyinfo->font_table + i;
13123 int w, h;
13124
13125 font = (XFontStruct *) fontp->font;
13126 xassert (font != (XFontStruct *) ~0);
13127 x_font_min_bounds (font, &w, &h);
13128
13129 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13130 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13131 }
13132
13133 xassert (dpyinfo->smallest_char_width > 0
13134 && dpyinfo->smallest_font_height > 0);
13135
13136 return (dpyinfo->n_fonts == 1
13137 || dpyinfo->smallest_char_width < old_width
13138 || dpyinfo->smallest_font_height < old_height);
13139}
13140
13141
dc43ef94
KH
13142/* Load font named FONTNAME of the size SIZE for frame F, and return a
13143 pointer to the structure font_info while allocating it dynamically.
13144 If SIZE is 0, load any size of font.
13145 If loading is failed, return NULL. */
13146
13147struct font_info *
13148x_load_font (f, fontname, size)
13149 struct frame *f;
13150 register char *fontname;
13151 int size;
13152{
13153 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13154 Lisp_Object font_names;
d645aaa4 13155 int count;
dc43ef94
KH
13156
13157 /* Get a list of all the fonts that match this name. Once we
13158 have a list of matching fonts, we compare them against the fonts
13159 we already have by comparing names. */
09c6077f 13160 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13161
13162 if (!NILP (font_names))
13163 {
13164 Lisp_Object tail;
13165 int i;
13166
13167 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13168 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13169 if (dpyinfo->font_table[i].name
13170 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13171 XSTRING (XCAR (tail))->data)
06a2c219 13172 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13173 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13174 return (dpyinfo->font_table + i);
13175 }
13176
13177 /* Load the font and add it to the table. */
13178 {
13179 char *full_name;
13180 XFontStruct *font;
13181 struct font_info *fontp;
13182 unsigned long value;
06a2c219 13183 int i;
dc43ef94 13184
2da424f1
KH
13185 /* If we have found fonts by x_list_font, load one of them. If
13186 not, we still try to load a font by the name given as FONTNAME
13187 because XListFonts (called in x_list_font) of some X server has
13188 a bug of not finding a font even if the font surely exists and
13189 is loadable by XLoadQueryFont. */
e1d6d5b9 13190 if (size > 0 && !NILP (font_names))
8e713be6 13191 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13192
13193 BLOCK_INPUT;
d645aaa4 13194 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13195 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13196 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13197 {
13198 /* This error is perhaps due to insufficient memory on X
13199 server. Let's just ignore it. */
13200 font = NULL;
13201 x_clear_errors (FRAME_X_DISPLAY (f));
13202 }
13203 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13204 UNBLOCK_INPUT;
b5210ea7 13205 if (!font)
dc43ef94
KH
13206 return NULL;
13207
06a2c219
GM
13208 /* Find a free slot in the font table. */
13209 for (i = 0; i < dpyinfo->n_fonts; ++i)
13210 if (dpyinfo->font_table[i].name == NULL)
13211 break;
13212
13213 /* If no free slot found, maybe enlarge the font table. */
13214 if (i == dpyinfo->n_fonts
13215 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13216 {
06a2c219
GM
13217 int sz;
13218 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13219 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13220 dpyinfo->font_table
06a2c219 13221 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13222 }
13223
06a2c219
GM
13224 fontp = dpyinfo->font_table + i;
13225 if (i == dpyinfo->n_fonts)
13226 ++dpyinfo->n_fonts;
dc43ef94
KH
13227
13228 /* Now fill in the slots of *FONTP. */
13229 BLOCK_INPUT;
13230 fontp->font = font;
06a2c219 13231 fontp->font_idx = i;
dc43ef94
KH
13232 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13233 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13234
13235 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13236 full_name = 0;
13237 if (XGetFontProperty (font, XA_FONT, &value))
13238 {
13239 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13240 char *p = name;
13241 int dashes = 0;
13242
13243 /* Count the number of dashes in the "full name".
13244 If it is too few, this isn't really the font's full name,
13245 so don't use it.
13246 In X11R4, the fonts did not come with their canonical names
13247 stored in them. */
13248 while (*p)
13249 {
13250 if (*p == '-')
13251 dashes++;
13252 p++;
13253 }
13254
13255 if (dashes >= 13)
13256 {
13257 full_name = (char *) xmalloc (p - name + 1);
13258 bcopy (name, full_name, p - name + 1);
13259 }
13260
13261 XFree (name);
13262 }
13263
13264 if (full_name != 0)
13265 fontp->full_name = full_name;
13266 else
13267 fontp->full_name = fontp->name;
13268
13269 fontp->size = font->max_bounds.width;
d5749adb 13270 fontp->height = FONT_HEIGHT (font);
dc43ef94 13271
2da424f1
KH
13272 if (NILP (font_names))
13273 {
13274 /* We come here because of a bug of XListFonts mentioned at
13275 the head of this block. Let's store this information in
13276 the cache for x_list_fonts. */
13277 Lisp_Object lispy_name = build_string (fontname);
13278 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
13279 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13280 Qnil);
2da424f1 13281
8e713be6 13282 XCDR (dpyinfo->name_list_element)
9c11f79e 13283 = Fcons (Fcons (key,
2da424f1
KH
13284 Fcons (Fcons (lispy_full_name,
13285 make_number (fontp->size)),
13286 Qnil)),
8e713be6 13287 XCDR (dpyinfo->name_list_element));
2da424f1 13288 if (full_name)
9c11f79e
GM
13289 {
13290 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13291 Qnil);
13292 XCDR (dpyinfo->name_list_element)
13293 = Fcons (Fcons (key,
13294 Fcons (Fcons (lispy_full_name,
13295 make_number (fontp->size)),
13296 Qnil)),
13297 XCDR (dpyinfo->name_list_element));
13298 }
2da424f1
KH
13299 }
13300
dc43ef94
KH
13301 /* The slot `encoding' specifies how to map a character
13302 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13303 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13304 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13305 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13306 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13307 which is never used by any charset. If mapping can't be
13308 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13309 fontp->encoding[1]
13310 = (font->max_byte1 == 0
13311 /* 1-byte font */
13312 ? (font->min_char_or_byte2 < 0x80
13313 ? (font->max_char_or_byte2 < 0x80
13314 ? 0 /* 0x20..0x7F */
8ff102bd 13315 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13316 : 1) /* 0xA0..0xFF */
13317 /* 2-byte font */
13318 : (font->min_byte1 < 0x80
13319 ? (font->max_byte1 < 0x80
13320 ? (font->min_char_or_byte2 < 0x80
13321 ? (font->max_char_or_byte2 < 0x80
13322 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13323 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13324 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13325 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13326 : (font->min_char_or_byte2 < 0x80
13327 ? (font->max_char_or_byte2 < 0x80
13328 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13329 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13330 : 1))); /* 0xA0A0..0xFFFF */
13331
13332 fontp->baseline_offset
13333 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13334 ? (long) value : 0);
13335 fontp->relative_compose
13336 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13337 ? (long) value : 0);
f78798df
KH
13338 fontp->default_ascent
13339 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13340 ? (long) value : 0);
dc43ef94 13341
06a2c219
GM
13342 /* Set global flag fonts_changed_p to non-zero if the font loaded
13343 has a character with a smaller width than any other character
13344 before, or if the font loaded has a smalle>r height than any
13345 other font loaded before. If this happens, it will make a
13346 glyph matrix reallocation necessary. */
13347 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13348 UNBLOCK_INPUT;
dc43ef94
KH
13349 return fontp;
13350 }
13351}
13352
06a2c219
GM
13353
13354/* Return a pointer to struct font_info of a font named FONTNAME for
13355 frame F. If no such font is loaded, return NULL. */
13356
dc43ef94
KH
13357struct font_info *
13358x_query_font (f, fontname)
13359 struct frame *f;
13360 register char *fontname;
13361{
13362 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13363 int i;
13364
13365 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13366 if (dpyinfo->font_table[i].name
13367 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13368 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13369 return (dpyinfo->font_table + i);
13370 return NULL;
13371}
13372
06a2c219
GM
13373
13374/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13375 `encoder' of the structure. */
13376
13377void
13378x_find_ccl_program (fontp)
13379 struct font_info *fontp;
13380{
a42f54e6 13381 Lisp_Object list, elt;
a6582676 13382
f9b5db02 13383 elt = Qnil;
8e713be6 13384 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13385 {
8e713be6 13386 elt = XCAR (list);
a6582676 13387 if (CONSP (elt)
8e713be6 13388 && STRINGP (XCAR (elt))
9f2feff6
KH
13389 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13390 >= 0)
13391 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13392 >= 0)))
a42f54e6
KH
13393 break;
13394 }
f9b5db02 13395
a42f54e6
KH
13396 if (! NILP (list))
13397 {
d27f8ca7
KH
13398 struct ccl_program *ccl
13399 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13400
8e713be6 13401 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13402 xfree (ccl);
13403 else
13404 fontp->font_encoder = ccl;
a6582676
KH
13405 }
13406}
13407
06a2c219 13408
dc43ef94 13409\f
06a2c219
GM
13410/***********************************************************************
13411 Initialization
13412 ***********************************************************************/
f451eb13 13413
3afe33e7
RS
13414#ifdef USE_X_TOOLKIT
13415static XrmOptionDescRec emacs_options[] = {
13416 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13417 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13418
13419 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13420 XrmoptionSepArg, NULL},
13421 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13422
13423 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13424 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13425 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13426 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13427 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13428 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13429 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13430};
13431#endif /* USE_X_TOOLKIT */
13432
7a13e894
RS
13433static int x_initialized;
13434
29b38361
KH
13435#ifdef MULTI_KBOARD
13436/* Test whether two display-name strings agree up to the dot that separates
13437 the screen number from the server number. */
13438static int
13439same_x_server (name1, name2)
13440 char *name1, *name2;
13441{
13442 int seen_colon = 0;
cf591cc1
RS
13443 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13444 int system_name_length = strlen (system_name);
13445 int length_until_period = 0;
13446
13447 while (system_name[length_until_period] != 0
13448 && system_name[length_until_period] != '.')
13449 length_until_period++;
13450
13451 /* Treat `unix' like an empty host name. */
13452 if (! strncmp (name1, "unix:", 5))
13453 name1 += 4;
13454 if (! strncmp (name2, "unix:", 5))
13455 name2 += 4;
13456 /* Treat this host's name like an empty host name. */
13457 if (! strncmp (name1, system_name, system_name_length)
13458 && name1[system_name_length] == ':')
13459 name1 += system_name_length;
13460 if (! strncmp (name2, system_name, system_name_length)
13461 && name2[system_name_length] == ':')
13462 name2 += system_name_length;
13463 /* Treat this host's domainless name like an empty host name. */
13464 if (! strncmp (name1, system_name, length_until_period)
13465 && name1[length_until_period] == ':')
13466 name1 += length_until_period;
13467 if (! strncmp (name2, system_name, length_until_period)
13468 && name2[length_until_period] == ':')
13469 name2 += length_until_period;
13470
29b38361
KH
13471 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13472 {
13473 if (*name1 == ':')
13474 seen_colon++;
13475 if (seen_colon && *name1 == '.')
13476 return 1;
13477 }
13478 return (seen_colon
13479 && (*name1 == '.' || *name1 == '\0')
13480 && (*name2 == '.' || *name2 == '\0'));
13481}
13482#endif
13483
334208b7 13484struct x_display_info *
1f8255f2 13485x_term_init (display_name, xrm_option, resource_name)
334208b7 13486 Lisp_Object display_name;
1f8255f2
RS
13487 char *xrm_option;
13488 char *resource_name;
dc6f92b8 13489{
334208b7 13490 int connection;
7a13e894 13491 Display *dpy;
334208b7
RS
13492 struct x_display_info *dpyinfo;
13493 XrmDatabase xrdb;
13494
60439948
KH
13495 BLOCK_INPUT;
13496
7a13e894
RS
13497 if (!x_initialized)
13498 {
13499 x_initialize ();
13500 x_initialized = 1;
13501 }
dc6f92b8 13502
3afe33e7 13503#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13504 /* weiner@footloose.sps.mot.com reports that this causes
13505 errors with X11R5:
13506 X protocol error: BadAtom (invalid Atom parameter)
13507 on protocol request 18skiloaf.
13508 So let's not use it until R6. */
13509#ifdef HAVE_X11XTR6
bdcd49ba
RS
13510 XtSetLanguageProc (NULL, NULL, NULL);
13511#endif
13512
7f9c7f94
RS
13513 {
13514 int argc = 0;
13515 char *argv[3];
13516
13517 argv[0] = "";
13518 argc = 1;
13519 if (xrm_option)
13520 {
13521 argv[argc++] = "-xrm";
13522 argv[argc++] = xrm_option;
13523 }
13524 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13525 resource_name, EMACS_CLASS,
13526 emacs_options, XtNumber (emacs_options),
13527 &argc, argv);
39d8bb4d
KH
13528
13529#ifdef HAVE_X11XTR6
10537cb1 13530 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13531 fixup_locale ();
39d8bb4d 13532#endif
7f9c7f94 13533 }
3afe33e7
RS
13534
13535#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13536#ifdef HAVE_X11R5
13537 XSetLocaleModifiers ("");
13538#endif
7a13e894 13539 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13540#endif /* not USE_X_TOOLKIT */
334208b7 13541
7a13e894
RS
13542 /* Detect failure. */
13543 if (dpy == 0)
60439948
KH
13544 {
13545 UNBLOCK_INPUT;
13546 return 0;
13547 }
7a13e894
RS
13548
13549 /* We have definitely succeeded. Record the new connection. */
13550
13551 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13552 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13553
29b38361
KH
13554#ifdef MULTI_KBOARD
13555 {
13556 struct x_display_info *share;
13557 Lisp_Object tail;
13558
13559 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13560 share = share->next, tail = XCDR (tail))
13561 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13562 XSTRING (display_name)->data))
13563 break;
13564 if (share)
13565 dpyinfo->kboard = share->kboard;
13566 else
13567 {
13568 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13569 init_kboard (dpyinfo->kboard);
59e755be
KH
13570 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13571 {
13572 char *vendor = ServerVendor (dpy);
9b6ed9f3 13573 UNBLOCK_INPUT;
59e755be
KH
13574 dpyinfo->kboard->Vsystem_key_alist
13575 = call1 (Qvendor_specific_keysyms,
13576 build_string (vendor ? vendor : ""));
9b6ed9f3 13577 BLOCK_INPUT;
59e755be
KH
13578 }
13579
29b38361
KH
13580 dpyinfo->kboard->next_kboard = all_kboards;
13581 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13582 /* Don't let the initial kboard remain current longer than necessary.
13583 That would cause problems if a file loaded on startup tries to
06a2c219 13584 prompt in the mini-buffer. */
0ad5446c
KH
13585 if (current_kboard == initial_kboard)
13586 current_kboard = dpyinfo->kboard;
29b38361
KH
13587 }
13588 dpyinfo->kboard->reference_count++;
13589 }
b9737ad3
KH
13590#endif
13591
7a13e894
RS
13592 /* Put this display on the chain. */
13593 dpyinfo->next = x_display_list;
13594 x_display_list = dpyinfo;
13595
13596 /* Put it on x_display_name_list as well, to keep them parallel. */
13597 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13598 x_display_name_list);
8e713be6 13599 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13600
13601 dpyinfo->display = dpy;
dc6f92b8 13602
dc6f92b8 13603#if 0
7a13e894 13604 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13605#endif /* ! 0 */
7a13e894
RS
13606
13607 dpyinfo->x_id_name
fc932ac6
RS
13608 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13609 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13610 + 2);
13611 sprintf (dpyinfo->x_id_name, "%s@%s",
13612 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13613
13614 /* Figure out which modifier bits mean what. */
334208b7 13615 x_find_modifier_meanings (dpyinfo);
f451eb13 13616
ab648270 13617 /* Get the scroll bar cursor. */
7a13e894 13618 dpyinfo->vertical_scroll_bar_cursor
334208b7 13619 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13620
334208b7
RS
13621 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13622 resource_name, EMACS_CLASS);
13623#ifdef HAVE_XRMSETDATABASE
13624 XrmSetDatabase (dpyinfo->display, xrdb);
13625#else
13626 dpyinfo->display->db = xrdb;
13627#endif
547d9db8 13628 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13629 all versions. */
13630 dpyinfo->xrdb = xrdb;
334208b7
RS
13631
13632 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13633 DefaultScreen (dpyinfo->display));
5ff67d81 13634 select_visual (dpyinfo);
43bd1b2b 13635 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13636 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13637 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13638 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13639 dpyinfo->grabbed = 0;
13640 dpyinfo->reference_count = 0;
13641 dpyinfo->icon_bitmap_id = -1;
06a2c219 13642 dpyinfo->font_table = NULL;
7a13e894
RS
13643 dpyinfo->n_fonts = 0;
13644 dpyinfo->font_table_size = 0;
13645 dpyinfo->bitmaps = 0;
13646 dpyinfo->bitmaps_size = 0;
13647 dpyinfo->bitmaps_last = 0;
13648 dpyinfo->scratch_cursor_gc = 0;
13649 dpyinfo->mouse_face_mouse_frame = 0;
13650 dpyinfo->mouse_face_deferred_gc = 0;
13651 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13652 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13653 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13654 dpyinfo->mouse_face_window = Qnil;
13655 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13656 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13657 dpyinfo->x_focus_frame = 0;
13658 dpyinfo->x_focus_event_frame = 0;
13659 dpyinfo->x_highlight_frame = 0;
06a2c219 13660 dpyinfo->image_cache = make_image_cache ();
334208b7 13661
43bd1b2b 13662 /* See if a private colormap is requested. */
5ff67d81
GM
13663 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13664 {
13665 if (dpyinfo->visual->class == PseudoColor)
13666 {
13667 Lisp_Object value;
13668 value = display_x_get_resource (dpyinfo,
13669 build_string ("privateColormap"),
13670 build_string ("PrivateColormap"),
13671 Qnil, Qnil);
13672 if (STRINGP (value)
13673 && (!strcmp (XSTRING (value)->data, "true")
13674 || !strcmp (XSTRING (value)->data, "on")))
13675 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13676 }
43bd1b2b 13677 }
5ff67d81
GM
13678 else
13679 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13680 dpyinfo->visual, AllocNone);
43bd1b2b 13681
06a2c219
GM
13682 {
13683 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13684 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13685 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13686 dpyinfo->resy = pixels * 25.4 / mm;
13687 pixels = DisplayWidth (dpyinfo->display, screen_number);
13688 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13689 dpyinfo->resx = pixels * 25.4 / mm;
13690 }
13691
334208b7
RS
13692 dpyinfo->Xatom_wm_protocols
13693 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13694 dpyinfo->Xatom_wm_take_focus
13695 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13696 dpyinfo->Xatom_wm_save_yourself
13697 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13698 dpyinfo->Xatom_wm_delete_window
13699 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13700 dpyinfo->Xatom_wm_change_state
13701 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13702 dpyinfo->Xatom_wm_configure_denied
13703 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13704 dpyinfo->Xatom_wm_window_moved
13705 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13706 dpyinfo->Xatom_editres
13707 = XInternAtom (dpyinfo->display, "Editres", False);
13708 dpyinfo->Xatom_CLIPBOARD
13709 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13710 dpyinfo->Xatom_TIMESTAMP
13711 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13712 dpyinfo->Xatom_TEXT
13713 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13714 dpyinfo->Xatom_COMPOUND_TEXT
13715 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13716 dpyinfo->Xatom_DELETE
13717 = XInternAtom (dpyinfo->display, "DELETE", False);
13718 dpyinfo->Xatom_MULTIPLE
13719 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13720 dpyinfo->Xatom_INCR
13721 = XInternAtom (dpyinfo->display, "INCR", False);
13722 dpyinfo->Xatom_EMACS_TMP
13723 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13724 dpyinfo->Xatom_TARGETS
13725 = XInternAtom (dpyinfo->display, "TARGETS", False);
13726 dpyinfo->Xatom_NULL
13727 = XInternAtom (dpyinfo->display, "NULL", False);
13728 dpyinfo->Xatom_ATOM_PAIR
13729 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13730 /* For properties of font. */
13731 dpyinfo->Xatom_PIXEL_SIZE
13732 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13733 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13734 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13735 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13736 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13737 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13738 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13739
06a2c219
GM
13740 /* Ghostscript support. */
13741 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13742 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13743
13744 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13745 False);
13746
547d9db8
KH
13747 dpyinfo->cut_buffers_initialized = 0;
13748
334208b7
RS
13749 connection = ConnectionNumber (dpyinfo->display);
13750 dpyinfo->connection = connection;
13751
dc43ef94 13752 {
5d7cc324
RS
13753 char null_bits[1];
13754
13755 null_bits[0] = 0x00;
dc43ef94
KH
13756
13757 dpyinfo->null_pixel
13758 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13759 null_bits, 1, 1, (long) 0, (long) 0,
13760 1);
13761 }
13762
06a2c219
GM
13763 {
13764 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 13765 extern char *gray_bitmap_bits;
06a2c219
GM
13766 dpyinfo->gray
13767 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13768 gray_bitmap_bits,
13769 gray_bitmap_width, gray_bitmap_height,
13770 (unsigned long) 1, (unsigned long) 0, 1);
13771 }
13772
f5d11644
GM
13773#ifdef HAVE_X_I18N
13774 xim_initialize (dpyinfo, resource_name);
13775#endif
13776
87485d6f
MW
13777#ifdef subprocesses
13778 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13779 if (connection != 0)
7a13e894 13780 add_keyboard_wait_descriptor (connection);
87485d6f 13781#endif
6d4238f3 13782
041b69ac 13783#ifndef F_SETOWN_BUG
dc6f92b8 13784#ifdef F_SETOWN
dc6f92b8 13785#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13786 /* stdin is a socket here */
334208b7 13787 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13788#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13789 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13790#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13791#endif /* ! defined (F_SETOWN) */
041b69ac 13792#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13793
13794#ifdef SIGIO
eee20f6a
KH
13795 if (interrupt_input)
13796 init_sigio (connection);
c118dd06 13797#endif /* ! defined (SIGIO) */
dc6f92b8 13798
51b592fb 13799#ifdef USE_LUCID
f8c39f51 13800#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13801 /* Make sure that we have a valid font for dialog boxes
13802 so that Xt does not crash. */
13803 {
13804 Display *dpy = dpyinfo->display;
13805 XrmValue d, fr, to;
13806 Font font;
e99db5a1 13807 int count;
51b592fb
RS
13808
13809 d.addr = (XPointer)&dpy;
13810 d.size = sizeof (Display *);
13811 fr.addr = XtDefaultFont;
13812 fr.size = sizeof (XtDefaultFont);
13813 to.size = sizeof (Font *);
13814 to.addr = (XPointer)&font;
e99db5a1 13815 count = x_catch_errors (dpy);
51b592fb
RS
13816 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13817 abort ();
13818 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13819 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13820 x_uncatch_errors (dpy, count);
51b592fb
RS
13821 }
13822#endif
f8c39f51 13823#endif
51b592fb 13824
34e23e5a
GM
13825 /* See if we should run in synchronous mode. This is useful
13826 for debugging X code. */
13827 {
13828 Lisp_Object value;
13829 value = display_x_get_resource (dpyinfo,
13830 build_string ("synchronous"),
13831 build_string ("Synchronous"),
13832 Qnil, Qnil);
13833 if (STRINGP (value)
13834 && (!strcmp (XSTRING (value)->data, "true")
13835 || !strcmp (XSTRING (value)->data, "on")))
13836 XSynchronize (dpyinfo->display, True);
13837 }
13838
60439948
KH
13839 UNBLOCK_INPUT;
13840
7a13e894
RS
13841 return dpyinfo;
13842}
13843\f
13844/* Get rid of display DPYINFO, assuming all frames are already gone,
13845 and without sending any more commands to the X server. */
dc6f92b8 13846
7a13e894
RS
13847void
13848x_delete_display (dpyinfo)
13849 struct x_display_info *dpyinfo;
13850{
13851 delete_keyboard_wait_descriptor (dpyinfo->connection);
13852
13853 /* Discard this display from x_display_name_list and x_display_list.
13854 We can't use Fdelq because that can quit. */
13855 if (! NILP (x_display_name_list)
8e713be6
KR
13856 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13857 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13858 else
13859 {
13860 Lisp_Object tail;
13861
13862 tail = x_display_name_list;
8e713be6 13863 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13864 {
bffcfca9 13865 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13866 {
8e713be6 13867 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13868 break;
13869 }
8e713be6 13870 tail = XCDR (tail);
7a13e894
RS
13871 }
13872 }
13873
9bda743f
GM
13874 if (next_noop_dpyinfo == dpyinfo)
13875 next_noop_dpyinfo = dpyinfo->next;
13876
7a13e894
RS
13877 if (x_display_list == dpyinfo)
13878 x_display_list = dpyinfo->next;
7f9c7f94
RS
13879 else
13880 {
13881 struct x_display_info *tail;
7a13e894 13882
7f9c7f94
RS
13883 for (tail = x_display_list; tail; tail = tail->next)
13884 if (tail->next == dpyinfo)
13885 tail->next = tail->next->next;
13886 }
7a13e894 13887
0d777288
RS
13888#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13889#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13890 XrmDestroyDatabase (dpyinfo->xrdb);
13891#endif
0d777288 13892#endif
29b38361
KH
13893#ifdef MULTI_KBOARD
13894 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13895 delete_kboard (dpyinfo->kboard);
b9737ad3 13896#endif
f5d11644
GM
13897#ifdef HAVE_X_I18N
13898 if (dpyinfo->xim)
13899 xim_close_dpy (dpyinfo);
13900#endif
13901
b9737ad3
KH
13902 xfree (dpyinfo->font_table);
13903 xfree (dpyinfo->x_id_name);
f04e1297 13904 xfree (dpyinfo->color_cells);
b9737ad3 13905 xfree (dpyinfo);
7a13e894 13906}
f04e1297 13907
7a13e894
RS
13908\f
13909/* Set up use of X before we make the first connection. */
13910
06a2c219
GM
13911static struct redisplay_interface x_redisplay_interface =
13912{
13913 x_produce_glyphs,
13914 x_write_glyphs,
13915 x_insert_glyphs,
13916 x_clear_end_of_line,
13917 x_scroll_run,
13918 x_after_update_window_line,
13919 x_update_window_begin,
13920 x_update_window_end,
13921 XTcursor_to,
13922 x_flush,
71b8321e 13923 x_clear_mouse_face,
66ac4b0e
GM
13924 x_get_glyph_overhangs,
13925 x_fix_overlapping_area
06a2c219
GM
13926};
13927
dfcf069d 13928void
7a13e894
RS
13929x_initialize ()
13930{
06a2c219
GM
13931 rif = &x_redisplay_interface;
13932
13933 clear_frame_hook = x_clear_frame;
13934 ins_del_lines_hook = x_ins_del_lines;
13935 change_line_highlight_hook = x_change_line_highlight;
13936 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13937 ring_bell_hook = XTring_bell;
13938 reset_terminal_modes_hook = XTreset_terminal_modes;
13939 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13940 update_begin_hook = x_update_begin;
13941 update_end_hook = x_update_end;
dc6f92b8
JB
13942 set_terminal_window_hook = XTset_terminal_window;
13943 read_socket_hook = XTread_socket;
b8009dd1 13944 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13945 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13946 mouse_position_hook = XTmouse_position;
f451eb13 13947 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13948 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13949 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13950 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13951 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13952 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13953 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13954
f676886a 13955 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 13956 char_ins_del_ok = 1;
dc6f92b8
JB
13957 line_ins_del_ok = 1; /* we'll just blt 'em */
13958 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13959 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13960 off the bottom */
13961 baud_rate = 19200;
13962
7a13e894 13963 x_noop_count = 0;
9ea173e8 13964 last_tool_bar_item = -1;
06a2c219
GM
13965 any_help_event_p = 0;
13966
b30b24cb
RS
13967 /* Try to use interrupt input; if we can't, then start polling. */
13968 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13969
7f9c7f94
RS
13970#ifdef USE_X_TOOLKIT
13971 XtToolkitInitialize ();
13972 Xt_app_con = XtCreateApplicationContext ();
665881ad 13973 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13974
13975 /* Install an asynchronous timer that processes Xt timeout events
13976 every 0.1s. This is necessary because some widget sets use
13977 timeouts internally, for example the LessTif menu bar, or the
13978 Xaw3d scroll bar. When Xt timouts aren't processed, these
13979 widgets don't behave normally. */
13980 {
13981 EMACS_TIME interval;
13982 EMACS_SET_SECS_USECS (interval, 0, 100000);
13983 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13984 }
db74249b 13985#endif
bffcfca9 13986
eccc05db 13987#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13988 xaw3d_arrow_scroll = False;
13989 xaw3d_pick_top = True;
7f9c7f94
RS
13990#endif
13991
58769bee 13992 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13993 original error handler. */
e99db5a1 13994 XSetErrorHandler (x_error_handler);
334208b7 13995 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13996
06a2c219 13997 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13998#ifdef SIGWINCH
13999 signal (SIGWINCH, SIG_DFL);
c118dd06 14000#endif /* ! defined (SIGWINCH) */
dc6f92b8 14001
92e2441b 14002 signal (SIGPIPE, x_connection_signal);
dc6f92b8 14003}
55123275 14004
06a2c219 14005
55123275
JB
14006void
14007syms_of_xterm ()
14008{
e99db5a1
RS
14009 staticpro (&x_error_message_string);
14010 x_error_message_string = Qnil;
14011
7a13e894
RS
14012 staticpro (&x_display_name_list);
14013 x_display_name_list = Qnil;
334208b7 14014
ab648270 14015 staticpro (&last_mouse_scroll_bar);
e53cb100 14016 last_mouse_scroll_bar = Qnil;
59e755be
KH
14017
14018 staticpro (&Qvendor_specific_keysyms);
14019 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
14020
14021 staticpro (&last_mouse_press_frame);
14022 last_mouse_press_frame = Qnil;
06a2c219 14023
06a2c219 14024 help_echo = Qnil;
be010514
GM
14025 staticpro (&help_echo);
14026 help_echo_object = Qnil;
14027 staticpro (&help_echo_object);
7cea38bc
GM
14028 help_echo_window = Qnil;
14029 staticpro (&help_echo_window);
06a2c219 14030 previous_help_echo = Qnil;
be010514
GM
14031 staticpro (&previous_help_echo);
14032 help_echo_pos = -1;
06a2c219
GM
14033
14034 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14035 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14036For example, if a block cursor is over a tab, it will be drawn as\n\
14037wide as that tab on the display.");
14038 x_stretch_cursor_p = 0;
14039
5bf04520
GM
14040 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14041 "What X toolkit scroll bars Emacs uses.\n\
14042A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14043Otherwise, value is a symbol describing the X toolkit.");
eccc05db 14044#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
14045#ifdef USE_MOTIF
14046 Vx_toolkit_scroll_bars = intern ("motif");
14047#elif defined HAVE_XAW3D
14048 Vx_toolkit_scroll_bars = intern ("xaw3d");
14049#else
14050 Vx_toolkit_scroll_bars = intern ("xaw");
14051#endif
06a2c219 14052#else
5bf04520 14053 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
14054#endif
14055
06a2c219
GM
14056 staticpro (&last_mouse_motion_frame);
14057 last_mouse_motion_frame = Qnil;
55123275 14058}
6cf0ae86
RS
14059
14060#endif /* not HAVE_X_WINDOWS */