(encode_terminal_code): Adjusted for the change of struct
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
06a2c219
GM
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999
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 46
16bd92ea 47#ifndef USG
dc6f92b8
JB
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06
JB
52#endif /* makedev */
53#endif /* USG */
dc6f92b8 54
6df54671 55#ifdef BSD_SYSTEM
dc6f92b8 56#include <sys/ioctl.h>
6df54671 57#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 58
2d368234 59#include "systty.h"
3a2712f9 60#include "systime.h"
dc6f92b8 61
b8009dd1 62#ifndef INCLUDED_FCNTL
dc6f92b8 63#include <fcntl.h>
b8009dd1 64#endif
dc6f92b8
JB
65#include <ctype.h>
66#include <errno.h>
67#include <setjmp.h>
68#include <sys/stat.h>
a0a7635f
RS
69/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70/* #include <sys/param.h> */
dc6f92b8 71
dc43ef94
KH
72#include "charset.h"
73#include "ccl.h"
7a13e894 74#include "frame.h"
a1dfb88a 75#include "fontset.h"
dc6f92b8
JB
76#include "dispextern.h"
77#include "termhooks.h"
78#include "termopts.h"
79#include "termchar.h"
80#if 0
81#include "sink.h"
82#include "sinkmask.h"
c118dd06 83#endif /* ! 0 */
dc6f92b8 84#include "gnu.h"
dc6f92b8 85#include "disptab.h"
dc6f92b8 86#include "buffer.h"
f451eb13 87#include "window.h"
3b2fa4e6 88#include "keyboard.h"
bde7c500 89#include "intervals.h"
dfcf069d 90#include "process.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#include <sys/types.h>
97#ifdef HAVE_SYS_TIME_H
98#include <sys/time.h>
99#endif
100#ifdef HAVE_UNISTD_H
101#include <unistd.h>
102#endif
103
3afe33e7 104#ifdef USE_X_TOOLKIT
06a2c219 105
9d7e2e3e 106extern void free_frame_menubar ();
2224b905 107extern FRAME_PTR x_menubar_window_to_frame ();
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
80528801
KH
152#ifdef SOLARIS2
153/* memmove will be defined as a macro in Xfuncs.h unless
154 <string.h> is included beforehand. The declaration for memmove in
155 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
156#include <string.h>
157#endif
158
e4b68333 159#ifndef min
06a2c219 160#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
161#endif
162#ifndef max
06a2c219
GM
163#define max(a,b) ((a) > (b) ? (a) : (b))
164#endif
165
166#define abs(x) ((x) < 0 ? -(x) : (x))
167
168#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
169
170\f
171/* Bitmaps for truncated lines. */
172
173enum bitmap_type
174{
175 NO_BITMAP,
176 LEFT_TRUNCATION_BITMAP,
177 RIGHT_TRUNCATION_BITMAP,
178 OVERLAY_ARROW_BITMAP,
179 CONTINUED_LINE_BITMAP,
180 CONTINUATION_LINE_BITMAP,
181 ZV_LINE_BITMAP
182};
183
184/* Bitmap drawn to indicate lines not displaying text if
185 `indicate-empty-lines' is non-nil. */
186
187#define zv_width 8
188#define zv_height 8
189static unsigned char zv_bits[] = {
190 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
191
192/* An arrow like this: `<-'. */
193
194#define left_width 8
195#define left_height 8
196static unsigned char left_bits[] = {
197 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
198
110859fc
GM
199/* Right truncation arrow bitmap `->'. */
200
201#define right_width 8
202#define right_height 8
203static unsigned char right_bits[] = {
204 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
205
06a2c219
GM
206/* Marker for continued lines. */
207
208#define continued_width 8
209#define continued_height 8
210static unsigned char continued_bits[] = {
110859fc
GM
211 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
212
213/* Marker for continuation lines. */
06a2c219
GM
214
215#define continuation_width 8
216#define continuation_height 8
217static unsigned char continuation_bits[] = {
110859fc 218 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 219
110859fc 220/* Overlay arrow bitmap. */
06a2c219 221
110859fc
GM
222#if 0
223/* A bomb. */
06a2c219
GM
224#define ov_width 8
225#define ov_height 8
226static unsigned char ov_bits[] = {
227 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 228#else
110859fc 229/* A triangular arrow. */
06a2c219
GM
230#define ov_width 8
231#define ov_height 8
232static unsigned char ov_bits[] = {
110859fc
GM
233 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
234
e4b68333 235#endif
06a2c219
GM
236
237extern Lisp_Object Qhelp_echo;
238
69388238 239\f
06a2c219
GM
240/* Non-zero means Emacs uses toolkit scroll bars. */
241
242int x_toolkit_scroll_bars_p;
243
244/* If a string, XTread_socket generates an event to display that string.
245 (The display is done in read_char.) */
246
247static Lisp_Object help_echo;
248
249/* Temporary variable for XTread_socket. */
250
251static Lisp_Object previous_help_echo;
252
253/* Non-zero means that a HELP_EVENT has been generated since Emacs
254 start. */
255
256static int any_help_event_p;
257
258/* Non-zero means draw block and hollow cursor as wide as the glyph
259 under it. For example, if a block cursor is over a tab, it will be
260 drawn as wide as that tab on the display. */
261
262int x_stretch_cursor_p;
263
264/* This is a chain of structures for all the X displays currently in
265 use. */
266
334208b7 267struct x_display_info *x_display_list;
dc6f92b8 268
06a2c219
GM
269/* This is a list of cons cells, each of the form (NAME
270 . FONT-LIST-CACHE), one for each element of x_display_list and in
271 the same order. NAME is the name of the frame. FONT-LIST-CACHE
272 records previous values returned by x-list-fonts. */
273
7a13e894 274Lisp_Object x_display_name_list;
f451eb13 275
987d2ad1 276/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
277 This is set by update_begin and looked at by all the XT functions.
278 It is zero while not inside an update. In that case, the XT
279 functions assume that `selected_frame' is the frame to apply to. */
280
d0386f2a 281extern struct frame *updating_frame;
dc6f92b8 282
dfcf069d 283extern int waiting_for_input;
0e81d8cd 284
06a2c219
GM
285/* This is a frame waiting to be auto-raised, within XTread_socket. */
286
0134a210
RS
287struct frame *pending_autoraise_frame;
288
7f9c7f94
RS
289#ifdef USE_X_TOOLKIT
290/* The application context for Xt use. */
291XtAppContext Xt_app_con;
06a2c219
GM
292static String Xt_default_resources[] = {0};
293#endif /* USE_X_TOOLKIT */
665881ad 294
06a2c219
GM
295/* Nominal cursor position -- where to draw output.
296 HPOS and VPOS are window relative glyph matrix coordinates.
297 X and Y are window relative pixel coordinates. */
dc6f92b8 298
06a2c219 299struct cursor_pos output_cursor;
dc6f92b8 300
dc6f92b8 301
69388238
RS
302/* Mouse movement.
303
06a2c219 304 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
305 so that we would have to call XQueryPointer after each MotionNotify
306 event to ask for another such event. However, this made mouse tracking
307 slow, and there was a bug that made it eventually stop.
308
309 Simply asking for MotionNotify all the time seems to work better.
310
69388238
RS
311 In order to avoid asking for motion events and then throwing most
312 of them away or busy-polling the server for mouse positions, we ask
313 the server for pointer motion hints. This means that we get only
314 one event per group of mouse movements. "Groups" are delimited by
315 other kinds of events (focus changes and button clicks, for
316 example), or by XQueryPointer calls; when one of these happens, we
317 get another MotionNotify event the next time the mouse moves. This
318 is at least as efficient as getting motion events when mouse
319 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 320 is off. */
69388238
RS
321
322/* Where the mouse was last time we reported a mouse event. */
69388238 323
06a2c219
GM
324FRAME_PTR last_mouse_frame;
325static XRectangle last_mouse_glyph;
2237cac9
RS
326static Lisp_Object last_mouse_press_frame;
327
69388238
RS
328/* The scroll bar in which the last X motion event occurred.
329
06a2c219
GM
330 If the last X motion event occurred in a scroll bar, we set this so
331 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
332 an ordinary motion.
333
06a2c219
GM
334 If the last X motion event didn't occur in a scroll bar, we set
335 this to Qnil, to tell XTmouse_position to return an ordinary motion
336 event. */
337
69388238
RS
338static Lisp_Object last_mouse_scroll_bar;
339
69388238
RS
340/* This is a hack. We would really prefer that XTmouse_position would
341 return the time associated with the position it returns, but there
06a2c219 342 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
343 along with the position query. So, we just keep track of the time
344 of the last movement we received, and return that in hopes that
345 it's somewhat accurate. */
06a2c219 346
69388238
RS
347static Time last_mouse_movement_time;
348
06a2c219
GM
349/* Incremented by XTread_socket whenever it really tries to read
350 events. */
351
c0a04927
RS
352#ifdef __STDC__
353static int volatile input_signal_count;
354#else
355static int input_signal_count;
356#endif
357
7a13e894 358/* Used locally within XTread_socket. */
06a2c219 359
7a13e894 360static int x_noop_count;
dc6f92b8 361
7a13e894 362/* Initial values of argv and argc. */
06a2c219 363
7a13e894
RS
364extern char **initial_argv;
365extern int initial_argc;
dc6f92b8 366
7a13e894 367extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 368
06a2c219 369/* Tells if a window manager is present or not. */
7a13e894
RS
370
371extern Lisp_Object Vx_no_window_manager;
dc6f92b8 372
c2df547c 373extern Lisp_Object Qface, Qmouse_face;
b8009dd1 374
dc6f92b8
JB
375extern int errno;
376
dfeccd2d 377/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 378
64bb1782
RS
379extern int extra_keyboard_modifiers;
380
59e755be
KH
381static Lisp_Object Qvendor_specific_keysyms;
382
334208b7 383extern XrmDatabase x_load_resources ();
c32cdd9a
KH
384extern Lisp_Object x_icon_type ();
385
7a13e894 386
06a2c219
GM
387/* Enumeration for overriding/changing the face to use for drawing
388 glyphs in x_draw_glyphs. */
389
390enum draw_glyphs_face
391{
392 DRAW_NORMAL_TEXT,
393 DRAW_INVERSE_VIDEO,
394 DRAW_CURSOR,
395 DRAW_MOUSE_FACE,
396 DRAW_IMAGE_RAISED,
397 DRAW_IMAGE_SUNKEN
398};
399
400static void x_update_window_end P_ ((struct window *, int));
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));
411static void x_check_font P_ ((struct frame *, XFontStruct *));
412static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
413static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
414static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
415static void show_mouse_face P_ ((struct x_display_info *,
416 enum draw_glyphs_face));
417static int x_io_error_quitter P_ ((Display *));
418int x_catch_errors P_ ((Display *));
419void x_uncatch_errors P_ ((Display *, int));
420void x_lower_frame P_ ((struct frame *));
421void x_scroll_bar_clear P_ ((struct frame *));
422int x_had_errors_p P_ ((Display *));
423void x_wm_set_size_hint P_ ((struct frame *, long, int));
424void x_raise_frame P_ ((struct frame *));
425void x_set_window_size P_ ((struct frame *, int, int, int));
426void x_wm_set_window_state P_ ((struct frame *, int));
427void x_wm_set_icon_pixmap P_ ((struct frame *, int));
428void x_initialize P_ ((void));
429static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
430static int x_compute_min_glyph_bounds P_ ((struct frame *));
431static void x_draw_phys_cursor_glyph P_ ((struct window *,
432 struct glyph_row *,
433 enum draw_glyphs_face));
434static void x_update_end P_ ((struct frame *));
435static void XTframe_up_to_date P_ ((struct frame *));
436static void XTreassert_line_highlight P_ ((int, int));
437static void x_change_line_highlight P_ ((int, int, int, int));
438static void XTset_terminal_modes P_ ((void));
439static void XTreset_terminal_modes P_ ((void));
440static void XTcursor_to P_ ((int, int, int, int));
441static void x_write_glyphs P_ ((struct glyph *, int));
442static void x_clear_end_of_line P_ ((int));
443static void x_clear_frame P_ ((void));
444static void x_clear_cursor P_ ((struct window *));
445static void frame_highlight P_ ((struct frame *));
446static void frame_unhighlight P_ ((struct frame *));
447static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
448static void XTframe_rehighlight P_ ((struct frame *));
449static void x_frame_rehighlight P_ ((struct x_display_info *));
450static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
451static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
452static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
453 XRectangle *));
454static void expose_frame P_ ((struct frame *, int, int, int, int));
455static void expose_window_tree P_ ((struct window *, XRectangle *));
456static void expose_window P_ ((struct window *, XRectangle *));
457static void expose_area P_ ((struct window *, struct glyph_row *,
458 XRectangle *, enum glyph_row_area));
459static void expose_line P_ ((struct window *, struct glyph_row *,
460 XRectangle *));
461static void x_update_cursor_in_window_tree P_ ((struct window *, int));
462static void x_update_window_cursor P_ ((struct window *, int));
463static void x_erase_phys_cursor P_ ((struct window *));
464void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
465static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
466 enum bitmap_type));
467
468static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
469 GC, int));
470static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
471static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
472static void note_overwritten_text_cursor P_ ((struct window *, int, int));
473static void x_flush P_ ((struct frame *f));
474
475
476/* Flush display of frame F, or of all frames if F is null. */
477
478static void
479x_flush (f)
480 struct frame *f;
481{
482 BLOCK_INPUT;
483 if (f == NULL)
484 {
485 Lisp_Object rest, frame;
486 FOR_EACH_FRAME (rest, frame)
487 x_flush (XFRAME (frame));
488 }
489 else if (FRAME_X_P (f))
490 XFlush (FRAME_X_DISPLAY (f));
491 UNBLOCK_INPUT;
492}
493
dc6f92b8 494
06a2c219
GM
495/* Remove calls to XFlush by defining XFlush to an empty replacement.
496 Calls to XFlush should be unnecessary because the X output buffer
497 is flushed automatically as needed by calls to XPending,
498 XNextEvent, or XWindowEvent according to the XFlush man page.
499 XTread_socket calls XPending. Removing XFlush improves
500 performance. */
501
502#define XFlush(DISPLAY) (void) 0
b8009dd1 503
334208b7 504\f
06a2c219
GM
505/***********************************************************************
506 Debugging
507 ***********************************************************************/
508
9382638d 509#if 0
06a2c219
GM
510
511/* This is a function useful for recording debugging information about
512 the sequence of occurrences in this file. */
9382638d
KH
513
514struct record
515{
516 char *locus;
517 int type;
518};
519
520struct record event_record[100];
521
522int event_record_index;
523
524record_event (locus, type)
525 char *locus;
526 int type;
527{
528 if (event_record_index == sizeof (event_record) / sizeof (struct record))
529 event_record_index = 0;
530
531 event_record[event_record_index].locus = locus;
532 event_record[event_record_index].type = type;
533 event_record_index++;
534}
535
536#endif /* 0 */
06a2c219
GM
537
538
9382638d 539\f
334208b7
RS
540/* Return the struct x_display_info corresponding to DPY. */
541
542struct x_display_info *
543x_display_info_for_display (dpy)
544 Display *dpy;
545{
546 struct x_display_info *dpyinfo;
547
548 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
549 if (dpyinfo->display == dpy)
550 return dpyinfo;
16bd92ea 551
334208b7
RS
552 return 0;
553}
f451eb13 554
06a2c219
GM
555
556\f
557/***********************************************************************
558 Starting and ending an update
559 ***********************************************************************/
560
561/* Start an update of frame F. This function is installed as a hook
562 for update_begin, i.e. it is called when update_begin is called.
563 This function is called prior to calls to x_update_window_begin for
564 each window being updated. Currently, there is nothing to do here
565 because all interesting stuff is done on a window basis. */
dc6f92b8 566
dfcf069d 567static void
06a2c219 568x_update_begin (f)
f676886a 569 struct frame *f;
58769bee 570{
06a2c219
GM
571 /* Nothing to do. */
572}
dc6f92b8 573
dc6f92b8 574
06a2c219
GM
575/* Start update of window W. Set the global variable updated_window
576 to the window being updated and set output_cursor to the cursor
577 position of W. */
dc6f92b8 578
06a2c219
GM
579static void
580x_update_window_begin (w)
581 struct window *w;
582{
583 struct frame *f = XFRAME (WINDOW_FRAME (w));
584 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
585
586 updated_window = w;
587 set_output_cursor (&w->cursor);
b8009dd1 588
06a2c219 589 BLOCK_INPUT;
d1bc4182 590
06a2c219 591 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 592 {
514e4681 593 /* Don't do highlighting for mouse motion during the update. */
06a2c219 594 display_info->mouse_face_defer = 1;
37c2c98b 595
06a2c219
GM
596 /* If F needs to be redrawn, simply forget about any prior mouse
597 highlighting. */
9f67f20b 598 if (FRAME_GARBAGED_P (f))
06a2c219
GM
599 display_info->mouse_face_window = Qnil;
600
601 /* Can we tell that this update does not affect the window
602 where the mouse highlight is? If so, no need to turn off.
603 Likewise, don't do anything if the frame is garbaged;
604 in that case, the frame's current matrix that we would use
605 is all wrong, and we will redisplay that line anyway. */
606 if (!NILP (display_info->mouse_face_window)
607 && w == XWINDOW (display_info->mouse_face_window))
514e4681 608 {
06a2c219 609 int i;
514e4681 610
06a2c219
GM
611 for (i = 0; i < w->desired_matrix->nrows; ++i)
612 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
613 break;
614
06a2c219
GM
615 if (i < w->desired_matrix->nrows)
616 clear_mouse_face (display_info);
514e4681 617 }
b8009dd1 618 }
6ccf47d1 619
dc6f92b8
JB
620 UNBLOCK_INPUT;
621}
622
06a2c219
GM
623
624/* Draw a vertical window border to the right of window W if W doesn't
625 have vertical scroll bars. */
626
dfcf069d 627static void
06a2c219
GM
628x_draw_vertical_border (w)
629 struct window *w;
58769bee 630{
06a2c219
GM
631 struct frame *f = XFRAME (WINDOW_FRAME (w));
632
633 /* Redraw borders between horizontally adjacent windows. Don't
634 do it for frames with vertical scroll bars because either the
635 right scroll bar of a window, or the left scroll bar of its
636 neighbor will suffice as a border. */
637 if (!WINDOW_RIGHTMOST_P (w)
638 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
639 {
640 int x0, x1, y0, y1;
dc6f92b8 641
06a2c219 642 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 643 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
644 y1 -= 1;
645
646 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
647 f->output_data.x->normal_gc, x1, y0, x1, y1);
648 }
649}
650
651
652/* End update of window W (which is equal to updated_window). Draw
653 vertical borders between horizontally adjacent windows, and display
654 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
655 pseudo-window in case we don't have X toolkit support. Such
656 windows don't have a cursor, so don't display it here. */
657
658static void
659x_update_window_end (w, cursor_on_p)
660 struct window *w;
661 int cursor_on_p;
662{
663 if (!w->pseudo_window_p)
664 {
665 BLOCK_INPUT;
666 if (cursor_on_p)
667 x_display_and_set_cursor (w, 1, output_cursor.hpos,
668 output_cursor.vpos,
669 output_cursor.x, output_cursor.y);
670 x_draw_vertical_border (w);
671 UNBLOCK_INPUT;
672 }
673
674 updated_window = NULL;
675}
dc6f92b8 676
dc6f92b8 677
06a2c219
GM
678/* End update of frame F. This function is installed as a hook in
679 update_end. */
680
681static void
682x_update_end (f)
683 struct frame *f;
684{
685 /* Mouse highlight may be displayed again. */
aa8bff2e 686 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 687
06a2c219 688 BLOCK_INPUT;
334208b7 689 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
690 UNBLOCK_INPUT;
691}
b8009dd1 692
06a2c219
GM
693
694/* This function is called from various places in xdisp.c whenever a
695 complete update has been performed. The global variable
696 updated_window is not available here. */
b8009dd1 697
dfcf069d 698static void
b8009dd1 699XTframe_up_to_date (f)
06a2c219 700 struct frame *f;
b8009dd1 701{
06a2c219 702 if (FRAME_X_P (f))
514e4681 703 {
06a2c219
GM
704 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
705 if (dpyinfo->mouse_face_deferred_gc
706 || f == dpyinfo->mouse_face_mouse_frame)
707 {
708 BLOCK_INPUT;
709 if (dpyinfo->mouse_face_mouse_frame)
710 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
711 dpyinfo->mouse_face_mouse_x,
712 dpyinfo->mouse_face_mouse_y);
713 dpyinfo->mouse_face_deferred_gc = 0;
714 UNBLOCK_INPUT;
715 }
514e4681 716 }
b8009dd1 717}
06a2c219
GM
718
719
720/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
721 arrow bitmaps, or clear the areas where they would be displayed
722 before DESIRED_ROW is made current. The window being updated is
723 found in updated_window. This function It is called from
724 update_window_line only if it is known that there are differences
725 between bitmaps to be drawn between current row and DESIRED_ROW. */
726
727static void
728x_after_update_window_line (desired_row)
729 struct glyph_row *desired_row;
730{
731 struct window *w = updated_window;
732
733 xassert (w);
734
735 if (!desired_row->mode_line_p && !w->pseudo_window_p)
736 {
737 BLOCK_INPUT;
738 x_draw_row_bitmaps (w, desired_row);
739
740 /* When a window has disappeared, make sure that no rest of
741 full-width rows stays visible in the internal border. */
742 if (windows_or_buffers_changed)
743 {
744 struct frame *f = XFRAME (w->frame);
745 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
746 int height = desired_row->visible_height;
110859fc
GM
747 int x = (window_box_right (w, -1)
748 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
749 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
750
751 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
752 x, y, width, height, False);
753 }
754
755 UNBLOCK_INPUT;
756 }
757}
758
759
760/* Draw the bitmap WHICH in one of the areas to the left or right of
761 window W. ROW is the glyph row for which to display the bitmap; it
762 determines the vertical position at which the bitmap has to be
763 drawn. */
764
765static void
766x_draw_bitmap (w, row, which)
767 struct window *w;
768 struct glyph_row *row;
769 enum bitmap_type which;
770{
771 struct frame *f = XFRAME (WINDOW_FRAME (w));
772 Display *display = FRAME_X_DISPLAY (f);
773 Window window = FRAME_X_WINDOW (f);
774 int x, y, wd, h, dy;
775 unsigned char *bits;
776 Pixmap pixmap;
777 GC gc = f->output_data.x->normal_gc;
778 struct face *face;
779 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
780
781 /* Must clip because of partially visible lines. */
782 x_clip_to_row (w, row, gc, 1);
783
784 switch (which)
785 {
786 case LEFT_TRUNCATION_BITMAP:
787 wd = left_width;
788 h = left_height;
789 bits = left_bits;
790 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
791 - wd
110859fc 792 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
793 break;
794
795 case OVERLAY_ARROW_BITMAP:
796 wd = left_width;
797 h = left_height;
798 bits = ov_bits;
799 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
800 - wd
110859fc 801 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
802 break;
803
804 case RIGHT_TRUNCATION_BITMAP:
805 wd = right_width;
806 h = right_height;
807 bits = right_bits;
808 x = window_box_right (w, -1);
110859fc 809 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
810 break;
811
812 case CONTINUED_LINE_BITMAP:
813 wd = right_width;
814 h = right_height;
815 bits = continued_bits;
816 x = window_box_right (w, -1);
110859fc 817 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
818 break;
819
820 case CONTINUATION_LINE_BITMAP:
821 wd = continuation_width;
822 h = continuation_height;
823 bits = continuation_bits;
824 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
825 - wd
110859fc 826 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
827 break;
828
829 case ZV_LINE_BITMAP:
830 wd = zv_width;
831 h = zv_height;
832 bits = zv_bits;
833 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
834 - wd
110859fc 835 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
836 break;
837
838 default:
839 abort ();
840 }
841
842 /* Convert to frame coordinates. Set dy to the offset in the row to
843 start drawing the bitmap. */
844 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
845 dy = (row->height - h) / 2;
846
847 /* Draw the bitmap. I believe these small pixmaps can be cached
848 by the server. */
849 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
850 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
851 face->foreground,
852 face->background, depth);
853 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
854 XFreePixmap (display, pixmap);
855 XSetClipMask (display, gc, None);
856}
857
858
859/* Draw flags bitmaps for glyph row ROW on window W. Call this
860 function with input blocked. */
861
862static void
863x_draw_row_bitmaps (w, row)
864 struct window *w;
865 struct glyph_row *row;
866{
867 struct frame *f = XFRAME (w->frame);
868 enum bitmap_type bitmap;
869 struct face *face;
045dee35 870 int header_line_height = -1;
06a2c219
GM
871
872 xassert (interrupt_input_blocked);
873
874 /* If row is completely invisible, because of vscrolling, we
875 don't have to draw anything. */
876 if (row->visible_height <= 0)
877 return;
878
879 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
880 PREPARE_FACE_FOR_DISPLAY (f, face);
881
882 /* Decide which bitmap to draw at the left side. */
883 if (row->overlay_arrow_p)
884 bitmap = OVERLAY_ARROW_BITMAP;
885 else if (row->truncated_on_left_p)
886 bitmap = LEFT_TRUNCATION_BITMAP;
887 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
888 bitmap = CONTINUATION_LINE_BITMAP;
889 else if (row->indicate_empty_line_p)
890 bitmap = ZV_LINE_BITMAP;
891 else
892 bitmap = NO_BITMAP;
893
894 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
895 the flags area. */
896 if (bitmap == NO_BITMAP
110859fc 897 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
898 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
899 {
900 /* If W has a vertical border to its left, don't draw over it. */
901 int border = ((XFASTINT (w->left) > 0
902 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
903 ? 1 : 0);
904 int left = window_box_left (w, -1);
905
045dee35
GM
906 if (header_line_height < 0)
907 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
908
909 /* In case the same realized face is used for bitmap areas and
910 for something displayed in the text (e.g. face `region' on
911 mono-displays, the fill style may have been changed to
912 FillSolid in x_draw_glyph_string_background. */
913 if (face->stipple)
914 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
915 else
916 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
917
06a2c219
GM
918 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
919 face->gc,
920 (left
110859fc 921 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 922 + border),
045dee35 923 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 924 row->y)),
110859fc 925 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 926 row->visible_height);
dcd08bfb
GM
927 if (!face->stipple)
928 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
929 }
930
931 /* Draw the left bitmap. */
932 if (bitmap != NO_BITMAP)
933 x_draw_bitmap (w, row, bitmap);
934
935 /* Decide which bitmap to draw at the right side. */
936 if (row->truncated_on_right_p)
937 bitmap = RIGHT_TRUNCATION_BITMAP;
938 else if (row->continued_p)
939 bitmap = CONTINUED_LINE_BITMAP;
940 else
941 bitmap = NO_BITMAP;
942
943 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
944 the flags area. */
945 if (bitmap == NO_BITMAP
110859fc 946 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
947 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
948 {
949 int right = window_box_right (w, -1);
950
045dee35
GM
951 if (header_line_height < 0)
952 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
953
954 /* In case the same realized face is used for bitmap areas and
955 for something displayed in the text (e.g. face `region' on
956 mono-displays, the fill style may have been changed to
957 FillSolid in x_draw_glyph_string_background. */
958 if (face->stipple)
959 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
960 else
961 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
962 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
963 face->gc,
964 right,
045dee35 965 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 966 row->y)),
110859fc 967 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 968 row->visible_height);
dcd08bfb
GM
969 if (!face->stipple)
970 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
971 }
972
973 /* Draw the right bitmap. */
974 if (bitmap != NO_BITMAP)
975 x_draw_bitmap (w, row, bitmap);
976}
977
dc6f92b8 978\f
06a2c219
GM
979/***********************************************************************
980 Line Highlighting
981 ***********************************************************************/
dc6f92b8 982
06a2c219
GM
983/* External interface to control of standout mode. Not used for X
984 frames. Aborts when called. */
985
986static void
dc6f92b8
JB
987XTreassert_line_highlight (new, vpos)
988 int new, vpos;
989{
06a2c219 990 abort ();
dc6f92b8
JB
991}
992
06a2c219
GM
993
994/* Call this when about to modify line at position VPOS and change
995 whether it is highlighted. Not used for X frames. Aborts when
996 called. */
dc6f92b8 997
dfcf069d 998static void
06a2c219
GM
999x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1000 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1001{
06a2c219 1002 abort ();
dc6f92b8
JB
1003}
1004
06a2c219
GM
1005
1006/* This is called when starting Emacs and when restarting after
1007 suspend. When starting Emacs, no X window is mapped. And nothing
1008 must be done to Emacs's own window if it is suspended (though that
1009 rarely happens). */
dc6f92b8 1010
dfcf069d 1011static void
dc6f92b8
JB
1012XTset_terminal_modes ()
1013{
1014}
1015
06a2c219
GM
1016/* This is called when exiting or suspending Emacs. Exiting will make
1017 the X-windows go away, and suspending requires no action. */
dc6f92b8 1018
dfcf069d 1019static void
dc6f92b8
JB
1020XTreset_terminal_modes ()
1021{
dc6f92b8 1022}
06a2c219
GM
1023
1024
dc6f92b8 1025\f
06a2c219
GM
1026/***********************************************************************
1027 Output Cursor
1028 ***********************************************************************/
1029
1030/* Set the global variable output_cursor to CURSOR. All cursor
1031 positions are relative to updated_window. */
dc6f92b8 1032
dfcf069d 1033static void
06a2c219
GM
1034set_output_cursor (cursor)
1035 struct cursor_pos *cursor;
dc6f92b8 1036{
06a2c219
GM
1037 output_cursor.hpos = cursor->hpos;
1038 output_cursor.vpos = cursor->vpos;
1039 output_cursor.x = cursor->x;
1040 output_cursor.y = cursor->y;
1041}
1042
1043
1044/* Set a nominal cursor position.
dc6f92b8 1045
06a2c219
GM
1046 HPOS and VPOS are column/row positions in a window glyph matrix. X
1047 and Y are window text area relative pixel positions.
1048
1049 If this is done during an update, updated_window will contain the
1050 window that is being updated and the position is the future output
1051 cursor position for that window. If updated_window is null, use
1052 selected_window and display the cursor at the given position. */
1053
1054static void
1055XTcursor_to (vpos, hpos, y, x)
1056 int vpos, hpos, y, x;
1057{
1058 struct window *w;
1059
1060 /* If updated_window is not set, work on selected_window. */
1061 if (updated_window)
1062 w = updated_window;
1063 else
1064 w = XWINDOW (selected_window);
dbcb258a 1065
06a2c219
GM
1066 /* Set the output cursor. */
1067 output_cursor.hpos = hpos;
1068 output_cursor.vpos = vpos;
1069 output_cursor.x = x;
1070 output_cursor.y = y;
dc6f92b8 1071
06a2c219
GM
1072 /* If not called as part of an update, really display the cursor.
1073 This will also set the cursor position of W. */
1074 if (updated_window == NULL)
dc6f92b8
JB
1075 {
1076 BLOCK_INPUT;
06a2c219 1077 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1078 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1079 UNBLOCK_INPUT;
1080 }
1081}
dc43ef94 1082
06a2c219
GM
1083
1084\f
1085/***********************************************************************
1086 Display Iterator
1087 ***********************************************************************/
1088
1089/* Function prototypes of this page. */
1090
1091static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1092 struct glyph *,
1093 XChar2b *));
1094static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1095 int, XChar2b *, int));
1096static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1097static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1098static void x_append_glyph P_ ((struct it *));
b4192550 1099static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1100static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1101 int, int, double));
1102static void x_produce_glyphs P_ ((struct it *));
06a2c219
GM
1103static void x_produce_image_glyph P_ ((struct it *it));
1104
1105
1106/* Return a pointer to per-char metric information in FONT of a
1107 character pointed by B which is a pointer to an XChar2b. */
dc43ef94
KH
1108
1109#define PER_CHAR_METRIC(font, b) \
1110 ((font)->per_char \
1111 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1112 + (((font)->min_byte1 || (font)->max_byte1) \
1113 ? (((b)->byte1 - (font)->min_byte1) \
1114 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1115 : 0)) \
1116 : &((font)->max_bounds))
1117
dc6f92b8 1118
06a2c219
GM
1119/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1120 If CHAR2B is not contained in FONT, the font's default character
1121 metric is returned. */
dc43ef94 1122
06a2c219
GM
1123static INLINE XCharStruct *
1124x_per_char_metric (font, char2b)
1125 XFontStruct *font;
1126 XChar2b *char2b;
1127{
1128 /* The result metric information. */
1129 XCharStruct *pcm = NULL;
dc6f92b8 1130
06a2c219 1131 xassert (font && char2b);
dc6f92b8 1132
06a2c219 1133 if (font->per_char != NULL)
dc6f92b8 1134 {
06a2c219 1135 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1136 {
06a2c219
GM
1137 /* min_char_or_byte2 specifies the linear character index
1138 corresponding to the first element of the per_char array,
1139 max_char_or_byte2 is the index of the last character. A
1140 character with non-zero CHAR2B->byte1 is not in the font.
1141 A character with byte2 less than min_char_or_byte2 or
1142 greater max_char_or_byte2 is not in the font. */
1143 if (char2b->byte1 == 0
1144 && char2b->byte2 >= font->min_char_or_byte2
1145 && char2b->byte2 <= font->max_char_or_byte2)
1146 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1147 }
06a2c219 1148 else
dc6f92b8 1149 {
06a2c219
GM
1150 /* If either min_byte1 or max_byte1 are nonzero, both
1151 min_char_or_byte2 and max_char_or_byte2 are less than
1152 256, and the 2-byte character index values corresponding
1153 to the per_char array element N (counting from 0) are:
1154
1155 byte1 = N/D + min_byte1
1156 byte2 = N\D + min_char_or_byte2
1157
1158 where:
1159
1160 D = max_char_or_byte2 - min_char_or_byte2 + 1
1161 / = integer division
1162 \ = integer modulus */
1163 if (char2b->byte1 >= font->min_byte1
1164 && char2b->byte1 <= font->max_byte1
1165 && char2b->byte2 >= font->min_char_or_byte2
1166 && char2b->byte2 <= font->max_char_or_byte2)
1167 {
1168 pcm = (font->per_char
1169 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1170 * (char2b->byte1 - font->min_byte1))
1171 + (char2b->byte2 - font->min_char_or_byte2));
1172 }
dc6f92b8 1173 }
06a2c219
GM
1174 }
1175 else
1176 {
1177 /* If the per_char pointer is null, all glyphs between the first
1178 and last character indexes inclusive have the same
1179 information, as given by both min_bounds and max_bounds. */
1180 if (char2b->byte2 >= font->min_char_or_byte2
1181 && char2b->byte2 <= font->max_char_or_byte2)
1182 pcm = &font->max_bounds;
1183 }
dc6f92b8 1184
4a4dc352 1185
06a2c219
GM
1186 if (pcm == NULL || pcm->width == 0)
1187 {
1188 /* Character not contained in the font. FONT->default_char
1189 gives the character that will be printed. FONT->default_char
1190 is a 16-bit character code with byte1 in the most significant
1191 byte and byte2 in the least significant byte. */
1192 XChar2b default_char;
1193 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1194 default_char.byte2 = font->default_char & 0xff;
1195
1196 /* Avoid an endless recursion if FONT->default_char itself
1197 hasn't per char metrics. handa@etl.go.jp reports that some
1198 fonts have this problem. */
1199 if (default_char.byte1 != char2b->byte1
1200 || default_char.byte2 != char2b->byte2)
1201 pcm = x_per_char_metric (font, &default_char);
1202 else
1203 pcm = &font->max_bounds;
1204 }
1205
1206 return pcm;
1207}
b73b6aaf 1208
57b03282 1209
06a2c219
GM
1210/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1211 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1212
06a2c219
GM
1213static INLINE void
1214x_encode_char (c, char2b, font_info)
1215 int c;
1216 XChar2b *char2b;
1217 struct font_info *font_info;
1218{
1219 int charset = CHAR_CHARSET (c);
1220 XFontStruct *font = font_info->font;
1221
1222 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1223 This may be either a program in a special encoder language or a
1224 fixed encoding. */
1225 if (font_info->font_encoder)
1226 {
1227 /* It's a program. */
1228 struct ccl_program *ccl = font_info->font_encoder;
1229
1230 if (CHARSET_DIMENSION (charset) == 1)
1231 {
1232 ccl->reg[0] = charset;
1233 ccl->reg[1] = char2b->byte2;
1234 }
1235 else
1236 {
1237 ccl->reg[0] = charset;
1238 ccl->reg[1] = char2b->byte1;
1239 ccl->reg[2] = char2b->byte2;
1240 }
1241
1242 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1243
1244 /* We assume that MSBs are appropriately set/reset by CCL
1245 program. */
1246 if (font->max_byte1 == 0) /* 1-byte font */
1247 char2b->byte2 = ccl->reg[1];
1248 else
1249 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1250 }
1251 else if (font_info->encoding[charset])
1252 {
1253 /* Fixed encoding scheme. See fontset.h for the meaning of the
1254 encoding numbers. */
1255 int enc = font_info->encoding[charset];
1256
1257 if ((enc == 1 || enc == 2)
1258 && CHARSET_DIMENSION (charset) == 2)
1259 char2b->byte1 |= 0x80;
1260
1261 if (enc == 1 || enc == 3)
1262 char2b->byte2 |= 0x80;
1263 }
1264}
1265
1266
1267/* Get face and two-byte form of character C in face FACE_ID on frame
1268 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1269 means we want to display multibyte text. Value is a pointer to a
1270 realized face that is ready for display. */
1271
1272static INLINE struct face *
1273x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1274 struct frame *f;
1275 int c, face_id;
1276 XChar2b *char2b;
1277 int multibyte_p;
1278{
1279 struct face *face = FACE_FROM_ID (f, face_id);
1280
1281 if (!multibyte_p)
1282 {
1283 /* Unibyte case. We don't have to encode, but we have to make
1284 sure to use a face suitable for unibyte. */
1285 char2b->byte1 = 0;
1286 char2b->byte2 = c;
1287
1288 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1289 {
1290 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1291 face = FACE_FROM_ID (f, face_id);
1292 }
1293 }
1294 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1295 {
1296 /* Case of ASCII in a face known to fit ASCII. */
1297 char2b->byte1 = 0;
1298 char2b->byte2 = c;
1299 }
1300 else
1301 {
1302 int c1, c2, charset;
1303
1304 /* Split characters into bytes. If c2 is -1 afterwards, C is
1305 really a one-byte character so that byte1 is zero. */
1306 SPLIT_CHAR (c, charset, c1, c2);
1307 if (c2 > 0)
1308 char2b->byte1 = c1, char2b->byte2 = c2;
1309 else
1310 char2b->byte1 = 0, char2b->byte2 = c1;
1311
1312 /* Get the face for displaying C. If `face' is not suitable for
1313 charset, get the one that fits. (This can happen for the
b4192550
KH
1314 translations of a composition where the glyph
1315 specifies a face for the first component, but the other
1316 components have a different charset.) */
06a2c219
GM
1317 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1318 {
1319 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1320 face = FACE_FROM_ID (f, face_id);
1321 }
1322
1323 /* Maybe encode the character in *CHAR2B. */
1324 if (charset != CHARSET_ASCII)
1325 {
1326 struct font_info *font_info
1327 = FONT_INFO_FROM_ID (f, face->font_info_id);
1328 if (font_info)
1329 {
1330 x_encode_char (c, char2b, font_info);
1331 if (charset == charset_latin_iso8859_1)
1332 {
1333 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1334 >= 0x80);
1335 char2b->byte2 |= 0x80;
1336 }
1337 }
1338 }
1339 }
1340
1341 /* Make sure X resources of the face are allocated. */
1342 xassert (face != NULL);
1343 PREPARE_FACE_FOR_DISPLAY (f, face);
1344
1345 return face;
1346}
1347
1348
1349/* Get face and two-byte form of character glyph GLYPH on frame F.
1350 The encoding of GLYPH->u.ch.code is returned in *CHAR2B. Value is
1351 a pointer to a realized face that is ready for display. */
1352
1353static INLINE struct face *
1354x_get_glyph_face_and_encoding (f, glyph, char2b)
1355 struct frame *f;
1356 struct glyph *glyph;
1357 XChar2b *char2b;
1358{
1359 struct face *face;
1360
1361 xassert (glyph->type == CHAR_GLYPH);
1362 face = FACE_FROM_ID (f, glyph->u.ch.face_id);
1363
1364 if (!glyph->multibyte_p)
1365 {
1366 /* Unibyte case. We don't have to encode, but we have to make
1367 sure to use a face suitable for unibyte. */
1368 char2b->byte1 = 0;
1369 char2b->byte2 = glyph->u.ch.code;
1370 }
1371 else if (glyph->u.ch.code < 128
1372 && glyph->u.ch.face_id < BASIC_FACE_ID_SENTINEL)
1373 {
1374 /* Case of ASCII in a face known to fit ASCII. */
1375 char2b->byte1 = 0;
1376 char2b->byte2 = glyph->u.ch.code;
1377 }
1378 else
1379 {
1380 int c1, c2, charset;
1381
1382 /* Split characters into bytes. If c2 is -1 afterwards, C is
1383 really a one-byte character so that byte1 is zero. */
1384 SPLIT_CHAR (glyph->u.ch.code, charset, c1, c2);
1385 if (c2 > 0)
1386 char2b->byte1 = c1, char2b->byte2 = c2;
1387 else
1388 char2b->byte1 = 0, char2b->byte2 = c1;
1389
1390 /* Maybe encode the character in *CHAR2B. */
1391 if (charset != CHARSET_ASCII)
1392 {
1393 struct font_info *font_info
1394 = FONT_INFO_FROM_ID (f, face->font_info_id);
1395 if (font_info)
1396 {
1397 x_encode_char (glyph->u.ch.code, char2b, font_info);
1398 if (charset == charset_latin_iso8859_1)
1399 char2b->byte2 |= 0x80;
1400 }
1401 }
1402 }
1403
1404 /* Make sure X resources of the face are allocated. */
1405 xassert (face != NULL);
1406 PREPARE_FACE_FOR_DISPLAY (f, face);
1407 return face;
1408}
1409
1410
1411/* Store one glyph for IT->char_to_display in IT->glyph_row.
1412 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1413
1414static INLINE void
1415x_append_glyph (it)
1416 struct it *it;
1417{
1418 struct glyph *glyph;
1419 enum glyph_row_area area = it->area;
1420
1421 xassert (it->glyph_row);
1422 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1423
1424 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1425 if (glyph < it->glyph_row->glyphs[area + 1])
1426 {
1427 /* Play it safe. If sub-structures of the glyph are not all the
1428 same size, it otherwise be that some bits stay set. This
1429 would prevent a comparison with GLYPH_EQUAL_P. */
1430 glyph->u.val = 0;
1431
1432 glyph->type = CHAR_GLYPH;
1433 glyph->pixel_width = it->pixel_width;
1434 glyph->u.ch.code = it->char_to_display;
1435 glyph->u.ch.face_id = it->face_id;
1436 glyph->charpos = CHARPOS (it->position);
1437 glyph->object = it->object;
1438 glyph->left_box_line_p = it->start_of_box_run_p;
1439 glyph->right_box_line_p = it->end_of_box_run_p;
1440 glyph->voffset = it->voffset;
1441 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1442 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1443 || it->phys_descent > it->descent);
06a2c219
GM
1444 ++it->glyph_row->used[area];
1445 }
1446}
1447
b4192550
KH
1448/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1449 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1450
1451static INLINE void
1452x_append_composite_glyph (it)
1453 struct it *it;
1454{
1455 struct glyph *glyph;
1456 enum glyph_row_area area = it->area;
1457
1458 xassert (it->glyph_row);
1459
1460 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1461 if (glyph < it->glyph_row->glyphs[area + 1])
1462 {
1463 /* Play it safe. If sub-structures of the glyph are not all the
1464 same size, it otherwise be that some bits stay set. This
1465 would prevent a comparison with GLYPH_EQUAL_P. */
1466 glyph->u.val = 0;
1467
1468 glyph->type = COMPOSITE_GLYPH;
1469 glyph->pixel_width = it->pixel_width;
1470 glyph->u.cmp.id = it->cmp_id;
1471 glyph->u.cmp.face_id = it->face_id;
1472 glyph->charpos = CHARPOS (it->position);
1473 glyph->object = it->object;
1474 glyph->left_box_line_p = it->start_of_box_run_p;
1475 glyph->right_box_line_p = it->end_of_box_run_p;
1476 glyph->voffset = it->voffset;
1477 glyph->multibyte_p = it->multibyte_p;
1478 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1479 || it->phys_descent > it->descent);
1480 ++it->glyph_row->used[area];
1481 }
1482}
1483
06a2c219
GM
1484
1485/* Change IT->ascent and IT->height according to the setting of
1486 IT->voffset. */
1487
1488static INLINE void
1489take_vertical_position_into_account (it)
1490 struct it *it;
1491{
1492 if (it->voffset)
1493 {
1494 if (it->voffset < 0)
1495 /* Increase the ascent so that we can display the text higher
1496 in the line. */
1497 it->ascent += abs (it->voffset);
1498 else
1499 /* Increase the descent so that we can display the text lower
1500 in the line. */
1501 it->descent += it->voffset;
1502 }
1503}
1504
1505
1506/* Produce glyphs/get display metrics for the image IT is loaded with.
1507 See the description of struct display_iterator in dispextern.h for
1508 an overview of struct display_iterator. */
1509
1510static void
1511x_produce_image_glyph (it)
1512 struct it *it;
1513{
1514 struct image *img;
1515 struct face *face;
1516
1517 xassert (it->what == IT_IMAGE);
1518
1519 face = FACE_FROM_ID (it->f, it->face_id);
1520 img = IMAGE_FROM_ID (it->f, it->image_id);
1521 xassert (img);
1522
1523 /* Make sure X resources of the face and image are loaded. */
1524 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1525 prepare_image_for_display (it->f, img);
1526
66ac4b0e
GM
1527 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1528 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1529 it->pixel_width = img->width + 2 * img->margin;
1530
1531 it->nglyphs = 1;
1532
1533 if (face->box != FACE_NO_BOX)
1534 {
1535 it->ascent += face->box_line_width;
1536 it->descent += face->box_line_width;
1537
1538 if (it->start_of_box_run_p)
1539 it->pixel_width += face->box_line_width;
1540 if (it->end_of_box_run_p)
1541 it->pixel_width += face->box_line_width;
1542 }
1543
1544 take_vertical_position_into_account (it);
1545
1546 if (it->glyph_row)
1547 {
1548 struct glyph *glyph;
1549 enum glyph_row_area area = it->area;
1550
1551 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1552 if (glyph < it->glyph_row->glyphs[area + 1])
1553 {
1554 glyph->type = IMAGE_GLYPH;
1555 glyph->u.img.id = img->id;
1556 glyph->u.img.face_id = it->face_id;
1557 glyph->pixel_width = it->pixel_width;
1558 glyph->charpos = CHARPOS (it->position);
1559 glyph->object = it->object;
1560 glyph->left_box_line_p = it->start_of_box_run_p;
1561 glyph->right_box_line_p = it->end_of_box_run_p;
1562 glyph->voffset = it->voffset;
1563 glyph->multibyte_p = it->multibyte_p;
1564 ++it->glyph_row->used[area];
1565 }
1566 }
1567}
1568
1569
1570/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1571 of the glyph, WIDTH and HEIGHT are the width and height of the
1572 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1573 ascent of the glyph (0 <= ASCENT <= 1). */
1574
1575static void
1576x_append_stretch_glyph (it, object, width, height, ascent)
1577 struct it *it;
1578 Lisp_Object object;
1579 int width, height;
1580 double ascent;
1581{
1582 struct glyph *glyph;
1583 enum glyph_row_area area = it->area;
1584
1585 xassert (ascent >= 0 && ascent <= 1);
1586
1587 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1588 if (glyph < it->glyph_row->glyphs[area + 1])
1589 {
1590 glyph->type = STRETCH_GLYPH;
1591 glyph->u.stretch.ascent = height * ascent;
1592 glyph->u.stretch.height = height;
1593 glyph->u.stretch.face_id = it->face_id;
1594 glyph->pixel_width = width;
1595 glyph->charpos = CHARPOS (it->position);
1596 glyph->object = object;
1597 glyph->left_box_line_p = it->start_of_box_run_p;
1598 glyph->right_box_line_p = it->end_of_box_run_p;
1599 glyph->voffset = it->voffset;
1600 glyph->multibyte_p = it->multibyte_p;
1601 ++it->glyph_row->used[area];
1602 }
1603}
1604
1605
1606/* Produce a stretch glyph for iterator IT. IT->object is the value
1607 of the glyph property displayed. The value must be a list
1608 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1609 being recognized:
1610
1611 1. `:width WIDTH' specifies that the space should be WIDTH *
1612 canonical char width wide. WIDTH may be an integer or floating
1613 point number.
1614
1615 2. `:relative-width FACTOR' specifies that the width of the stretch
1616 should be computed from the width of the first character having the
1617 `glyph' property, and should be FACTOR times that width.
1618
1619 3. `:align-to HPOS' specifies that the space should be wide enough
1620 to reach HPOS, a value in canonical character units.
1621
1622 Exactly one of the above pairs must be present.
1623
1624 4. `:height HEIGHT' specifies that the height of the stretch produced
1625 should be HEIGHT, measured in canonical character units.
1626
1627 5. `:relative-height FACTOR' specifies that the height of the the
1628 stretch should be FACTOR times the height of the characters having
1629 the glyph property.
1630
1631 Either none or exactly one of 4 or 5 must be present.
1632
1633 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1634 of the stretch should be used for the ascent of the stretch.
1635 ASCENT must be in the range 0 <= ASCENT <= 100. */
1636
1637#define NUMVAL(X) \
1638 ((INTEGERP (X) || FLOATP (X)) \
1639 ? XFLOATINT (X) \
1640 : - 1)
1641
1642
1643static void
1644x_produce_stretch_glyph (it)
1645 struct it *it;
1646{
1647 /* (space :width WIDTH :height HEIGHT. */
1648 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1649 extern Lisp_Object QCrelative_width, QCrelative_height;
1650 extern Lisp_Object QCalign_to;
1651 Lisp_Object prop, plist;
1652 double width = 0, height = 0, ascent = 0;
1653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1655
1656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1657
1658 /* List should start with `space'. */
1659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1660 plist = XCDR (it->object);
1661
1662 /* Compute the width of the stretch. */
1663 if (prop = Fplist_get (plist, QCwidth),
1664 NUMVAL (prop) > 0)
1665 /* Absolute width `:width WIDTH' specified and valid. */
1666 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_width),
1668 NUMVAL (prop) > 0)
1669 {
1670 /* Relative width `:relative-width FACTOR' specified and valid.
1671 Compute the width of the characters having the `glyph'
1672 property. */
1673 struct it it2;
1674 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1675
1676 it2 = *it;
1677 if (it->multibyte_p)
1678 {
1679 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1680 - IT_BYTEPOS (*it));
1681 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1682 }
1683 else
1684 it2.c = *p, it2.len = 1;
1685
1686 it2.glyph_row = NULL;
1687 it2.what = IT_CHARACTER;
1688 x_produce_glyphs (&it2);
1689 width = NUMVAL (prop) * it2.pixel_width;
1690 }
1691 else if (prop = Fplist_get (plist, QCalign_to),
1692 NUMVAL (prop) > 0)
1693 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1694 else
1695 /* Nothing specified -> width defaults to canonical char width. */
1696 width = CANON_X_UNIT (it->f);
1697
1698 /* Compute height. */
1699 if (prop = Fplist_get (plist, QCheight),
1700 NUMVAL (prop) > 0)
1701 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1702 else if (prop = Fplist_get (plist, QCrelative_height),
1703 NUMVAL (prop) > 0)
1704 height = FONT_HEIGHT (font) * NUMVAL (prop);
1705 else
1706 height = FONT_HEIGHT (font);
1707
1708 /* Compute percentage of height used for ascent. If
1709 `:ascent ASCENT' is present and valid, use that. Otherwise,
1710 derive the ascent from the font in use. */
1711 if (prop = Fplist_get (plist, QCascent),
1712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1713 ascent = NUMVAL (prop) / 100.0;
1714 else
1715 ascent = (double) font->ascent / FONT_HEIGHT (font);
1716
1717 if (width <= 0)
1718 width = 1;
1719 if (height <= 0)
1720 height = 1;
1721
1722 if (it->glyph_row)
1723 {
1724 Lisp_Object object = it->stack[it->sp - 1].string;
1725 if (!STRINGP (object))
1726 object = it->w->buffer;
1727 x_append_stretch_glyph (it, object, width, height, ascent);
1728 }
1729
1730 it->pixel_width = width;
66ac4b0e
GM
1731 it->ascent = it->phys_ascent = height * ascent;
1732 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1733 it->nglyphs = 1;
1734
1735 if (face->box != FACE_NO_BOX)
1736 {
1737 it->ascent += face->box_line_width;
1738 it->descent += face->box_line_width;
1739
1740 if (it->start_of_box_run_p)
1741 it->pixel_width += face->box_line_width;
1742 if (it->end_of_box_run_p)
1743 it->pixel_width += face->box_line_width;
1744 }
1745
1746 take_vertical_position_into_account (it);
1747}
1748
b4192550
KH
1749/* Return proper value to be used as baseline offset of font that has
1750 ASCENT and DESCENT to draw characters by the font at the vertical
1751 center of the line of frame F.
1752
1753 Here, out task is to find the value of BOFF in the following figure;
1754
1755 -------------------------+-----------+-
1756 -+-+---------+-+ | |
1757 | | | | | |
1758 | | | | F_ASCENT F_HEIGHT
1759 | | | ASCENT | |
1760 HEIGHT | | | | |
1761 | | |-|-+------+-----------|------- baseline
1762 | | | | BOFF | |
1763 | |---------|-+-+ | |
1764 | | | DESCENT | |
1765 -+-+---------+-+ F_DESCENT |
1766 -------------------------+-----------+-
1767
1768 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1769 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1770 DESCENT = FONT->descent
1771 HEIGHT = FONT_HEIGHT (FONT)
1772 F_DESCENT = (F->output_data.x->font->descent
1773 - F->output_data.x->baseline_offset)
1774 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1775*/
1776
1777#define VCENTER_BASELINE_OFFSET(FONT, F) \
1778 ((FONT)->descent \
1779 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1780 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1781
1782/* Produce glyphs/get display metrics for the display element IT is
1783 loaded with. See the description of struct display_iterator in
1784 dispextern.h for an overview of struct display_iterator. */
1785
1786static void
1787x_produce_glyphs (it)
1788 struct it *it;
1789{
1790 if (it->what == IT_CHARACTER)
1791 {
1792 XChar2b char2b;
1793 XFontStruct *font;
1794 struct face *face;
1795 XCharStruct *pcm;
06a2c219 1796 int font_not_found_p;
b4192550
KH
1797 struct font_info *font_info;
1798 int boff; /* baseline offset */
06a2c219
GM
1799
1800 /* Maybe translate single-byte characters to multibyte. */
1801 it->char_to_display = it->c;
1802 if (unibyte_display_via_language_environment
1803 && SINGLE_BYTE_CHAR_P (it->c)
1804 && (it->c >= 0240
1805 || (it->c >= 0200
1806 && !NILP (Vnonascii_translation_table))))
1807 {
1808 it->char_to_display = unibyte_char_to_multibyte (it->c);
1809 it->charset = CHAR_CHARSET (it->char_to_display);
1810 }
1811
1812 /* Get face and font to use. Encode IT->char_to_display. */
1813 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1814 it->face_id, &char2b,
1815 it->multibyte_p);
1816 font = face->font;
1817
1818 /* When no suitable font found, use the default font. */
1819 font_not_found_p = font == NULL;
1820 if (font_not_found_p)
b4192550
KH
1821 {
1822 font = FRAME_FONT (it->f);
1823 boff = it->f->output_data.x->baseline_offset;
1824 font_info = NULL;
1825 }
1826 else
1827 {
1828 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1829 boff = font_info->baseline_offset;
1830 if (font_info->vertical_centering)
1831 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1832 }
06a2c219
GM
1833
1834 if (it->char_to_display >= ' '
1835 && (!it->multibyte_p || it->char_to_display < 128))
1836 {
1837 /* Either unibyte or ASCII. */
1838 int stretched_p;
1839
1840 it->nglyphs = 1;
06a2c219
GM
1841
1842 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1843 it->ascent = font->ascent + boff;
1844 it->descent = font->descent - boff;
1845 it->phys_ascent = pcm->ascent + boff;
1846 it->phys_descent = pcm->descent - boff;
06a2c219
GM
1847 it->pixel_width = pcm->width;
1848
1849 /* If this is a space inside a region of text with
1850 `space-width' property, change its width. */
1851 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1852 if (stretched_p)
1853 it->pixel_width *= XFLOATINT (it->space_width);
1854
1855 /* If face has a box, add the box thickness to the character
1856 height. If character has a box line to the left and/or
1857 right, add the box line width to the character's width. */
1858 if (face->box != FACE_NO_BOX)
1859 {
1860 int thick = face->box_line_width;
1861
1862 it->ascent += thick;
1863 it->descent += thick;
1864
1865 if (it->start_of_box_run_p)
1866 it->pixel_width += thick;
1867 if (it->end_of_box_run_p)
1868 it->pixel_width += thick;
1869 }
1870
1871 /* If face has an overline, add the height of the overline
1872 (1 pixel) and a 1 pixel margin to the character height. */
1873 if (face->overline_p)
1874 it->ascent += 2;
1875
1876 take_vertical_position_into_account (it);
1877
1878 /* If we have to actually produce glyphs, do it. */
1879 if (it->glyph_row)
1880 {
1881 if (stretched_p)
1882 {
1883 /* Translate a space with a `space-width' property
1884 into a stretch glyph. */
1885 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1886 x_append_stretch_glyph (it, it->object, it->pixel_width,
1887 it->ascent + it->descent, ascent);
1888 }
1889 else
1890 x_append_glyph (it);
1891
1892 /* If characters with lbearing or rbearing are displayed
1893 in this line, record that fact in a flag of the
1894 glyph row. This is used to optimize X output code. */
1895 if (pcm->lbearing < 0
1896 || pcm->rbearing > pcm->width)
1897 it->glyph_row->contains_overlapping_glyphs_p = 1;
1898 }
1899 }
1900 else if (it->char_to_display == '\n')
1901 {
1902 /* A newline has no width but we need the height of the line. */
1903 it->pixel_width = 0;
1904 it->nglyphs = 0;
b4192550
KH
1905 it->ascent = it->phys_ascent = font->ascent + boff;
1906 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1907
1908 if (face->box != FACE_NO_BOX)
1909 {
1910 int thick = face->box_line_width;
1911 it->ascent += thick;
1912 it->descent += thick;
1913 }
1914 }
1915 else if (it->char_to_display == '\t')
1916 {
1917 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1918 int x = (it->current_x
1919 - it->prompt_width
1920 + it->continuation_lines_width);
1921 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1922
1923 it->pixel_width = next_tab_x - x;
1924 it->nglyphs = 1;
b4192550
KH
1925 it->ascent = it->phys_ascent = font->ascent + boff;
1926 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1927
1928 if (it->glyph_row)
1929 {
1930 double ascent = (double) it->ascent / (it->ascent + it->descent);
1931 x_append_stretch_glyph (it, it->object, it->pixel_width,
1932 it->ascent + it->descent, ascent);
1933 }
1934 }
1935 else
1936 {
1937 /* A multi-byte character. Assume that the display width of the
1938 character is the width of the character multiplied by the
b4192550 1939 width of the font. */
06a2c219 1940
b4192550
KH
1941 /* If we found a font, this font should give us the right
1942 metrics. If we didn't find a font, use the frame's
1943 default font and calculate the width of the character
1944 from the charset width; this is what old redisplay code
1945 did. */
1946 pcm = x_per_char_metric (font, &char2b);
1947 it->pixel_width = pcm->width;
1948 if (font_not_found_p)
1949 it->pixel_width *= CHARSET_WIDTH (it->charset);
1950 it->nglyphs = 1;
1951 it->ascent = font->ascent + boff;
1952 it->descent = font->descent - boff;
1953 it->phys_ascent = pcm->ascent + boff;
1954 it->phys_descent = pcm->descent - boff;
1955 if (it->glyph_row
1956 && (pcm->lbearing < 0
1957 || pcm->rbearing > pcm->width))
1958 it->glyph_row->contains_overlapping_glyphs_p = 1;
06a2c219
GM
1959
1960 if (face->box != FACE_NO_BOX)
1961 {
1962 int thick = face->box_line_width;
1963 it->ascent += thick;
1964 it->descent += thick;
1965
1966 if (it->start_of_box_run_p)
1967 it->pixel_width += thick;
1968 if (it->end_of_box_run_p)
1969 it->pixel_width += thick;
1970 }
1971
1972 /* If face has an overline, add the height of the overline
1973 (1 pixel) and a 1 pixel margin to the character height. */
1974 if (face->overline_p)
1975 it->ascent += 2;
1976
1977 take_vertical_position_into_account (it);
1978
1979 if (it->glyph_row)
1980 x_append_glyph (it);
1981 }
1982 }
b4192550
KH
1983 else if (it->what == IT_COMPOSITION)
1984 {
1985 /* Note: A composition is represented as one glyph in the
1986 glyph matrix. There are no padding glyphs. */
1987 XChar2b char2b;
1988 XFontStruct *font;
1989 struct face *face;
1990 XCharStruct *pcm;
1991 int font_not_found_p;
1992 struct font_info *font_info;
1993 int boff; /* baseline offset */
1994 struct composition *cmp = composition_table[it->cmp_id];
1995
1996 /* Maybe translate single-byte characters to multibyte. */
1997 it->char_to_display = it->c;
1998 if (unibyte_display_via_language_environment
1999 && SINGLE_BYTE_CHAR_P (it->c)
2000 && (it->c >= 0240
2001 || (it->c >= 0200
2002 && !NILP (Vnonascii_translation_table))))
2003 {
2004 it->char_to_display = unibyte_char_to_multibyte (it->c);
2005 it->charset = CHAR_CHARSET (it->char_to_display);
2006 }
2007
2008 /* Get face and font to use. Encode IT->char_to_display. */
2009 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
2010 it->face_id, &char2b,
2011 it->multibyte_p);
2012 font = face->font;
2013
2014 /* When no suitable font found, use the default font. */
2015 font_not_found_p = font == NULL;
2016 if (font_not_found_p)
2017 {
2018 font = FRAME_FONT (it->f);
2019 boff = it->f->output_data.x->baseline_offset;
2020 font_info = NULL;
2021 }
2022 else
2023 {
2024 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2025 boff = font_info->baseline_offset;
2026 if (font_info->vertical_centering)
2027 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2028 }
2029
2030 /* There are no padding glyphs, so there is only one glyph to
2031 produce for the composition. Important is that pixel_width,
2032 ascent and descent are the values of what is drawn by
2033 draw_glyphs (i.e. the values of the overall glyphs composed). */
2034 it->nglyphs = 1;
2035
2036 /* If we have not yet calculated pixel size data of glyphs of
2037 the composition for the current face font, calculate them
2038 now. Theoretically, we have to check all fonts for the
2039 glyphs, but that requires much time and memory space. So,
2040 here we check only the font of the first glyph. This leads
2041 to incorrect display very rarely, and C-l (recenter) can
2042 correct the display anyway. */
2043 if (cmp->font != (void *) font)
2044 {
2045 /* Ascent and descent of the font of the first character of
2046 this composition (adjusted by baseline offset). Ascent
2047 and descent of overall glyphs should not be less than
2048 them respectively. */
2049 int font_ascent = font->ascent + boff;
2050 int font_descent = font->descent - boff;
2051 /* Bounding box of the overall glyphs. */
2052 int leftmost, rightmost, lowest, highest;
2053 int i;
2054
2055 cmp->font = (void *) font;
2056
2057 /* Initialize the bounding box. */
2058 pcm = x_per_char_metric (font, &char2b);
2059 leftmost = 0;
2060 rightmost = pcm->width;
2061 lowest = - pcm->descent + boff;
2062 highest = pcm->ascent + boff;
2063 if (font_info
2064 && font_info->default_ascent
2065 && CHAR_TABLE_P (Vuse_default_ascent)
2066 && !NILP (Faref (Vuse_default_ascent,
2067 make_number (it->char_to_display))))
2068 highest = font_info->default_ascent + boff;
2069
2070 /* Draw the first glyph at the normal position. It may be
2071 shifted to right later if some other glyphs are drawn at
2072 the left. */
2073 cmp->offsets[0] = 0;
2074 cmp->offsets[1] = boff;
2075
2076 /* Set cmp->offsets for the remaining glyphs. */
2077 for (i = 1; i < cmp->glyph_len; i++)
2078 {
2079 int left, right, btm, top;
2080 int ch = COMPOSITION_GLYPH (cmp, i);
2081
2082 face = x_get_char_face_and_encoding (it->f, ch,
2083 it->face_id, &char2b,
2084 it->multibyte_p);
2085 font = face->font;
2086 if (font == NULL)
2087 {
2088 font = FRAME_FONT (it->f);
2089 boff = it->f->output_data.x->baseline_offset;
2090 font_info = NULL;
2091 }
2092 else
2093 {
2094 font_info
2095 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2096 boff = font_info->baseline_offset;
2097 if (font_info->vertical_centering)
2098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2099 }
2100
2101 pcm = x_per_char_metric (font, &char2b);
2102
2103 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2104 {
2105 /* Relative composition with or without
2106 alternate chars. */
2107 left = (leftmost + rightmost - pcm->width) / 2;
2108 btm = - pcm->descent + boff;
2109 if (font_info && font_info->relative_compose
2110 && (! CHAR_TABLE_P (Vignore_relative_composition)
2111 || NILP (Faref (Vignore_relative_composition,
2112 make_number (ch)))))
2113 {
2114
2115 if (- pcm->descent
2116 >= font_info->relative_compose)
2117 /* One extra pixel between two glyphs. */
2118 btm = highest + 1;
2119 else if (pcm->ascent <= 0)
2120 /* One extra pixel between two glyphs. */
2121 btm = lowest - 1 - pcm->ascent - pcm->descent;
2122 }
2123 }
2124 else
2125 {
2126 /* A composition rule is specified by an integer
2127 value that encodes global and new reference
2128 points (GREF and NREF). GREF and NREF are
2129 specified by numbers as below:
2130
2131 0---1---2 -- ascent
2132 | |
2133 | |
2134 | |
2135 9--10--11 -- center
2136 | |
2137 ---3---4---5--- baseline
2138 | |
2139 6---7---8 -- descent
2140 */
2141 int rule = COMPOSITION_RULE (cmp, i);
2142 int gref, nref, grefx, grefy, nrefx, nrefy;
2143
2144 COMPOSITION_DECODE_RULE (rule, gref, nref);
2145 grefx = gref % 3, nrefx = nref % 3;
2146 grefy = gref / 3, nrefy = nref / 3;
2147
2148 left = (leftmost
2149 + grefx * (rightmost - leftmost) / 2
2150 - nrefx * pcm->width / 2);
2151 btm = ((grefy == 0 ? highest
2152 : grefy == 1 ? 0
2153 : grefy == 2 ? lowest
2154 : (highest + lowest) / 2)
2155 - (nrefy == 0 ? pcm->ascent + pcm->descent
2156 : nrefy == 1 ? pcm->descent - boff
2157 : nrefy == 2 ? 0
2158 : (pcm->ascent + pcm->descent) / 2));
2159 }
2160
2161 cmp->offsets[i * 2] = left;
2162 cmp->offsets[i * 2 + 1] = btm + pcm->descent;
2163
2164 /* Update the bounding box of the overall glyphs. */
2165 right = left + pcm->width;
2166 top = btm + pcm->descent + pcm->ascent;
2167 if (left < leftmost)
2168 leftmost = left;
2169 if (right > rightmost)
2170 rightmost = right;
2171 if (top > highest)
2172 highest = top;
2173 if (btm < lowest)
2174 lowest = btm;
2175 }
2176
2177 /* If there are glyphs whose x-offsets are negative,
2178 shift all glyphs to the right and make all x-offsets
2179 non-negative. */
2180 if (leftmost < 0)
2181 {
2182 for (i = 0; i < cmp->glyph_len; i++)
2183 cmp->offsets[i * 2] -= leftmost;
2184 rightmost -= leftmost;
2185 }
2186
2187 cmp->pixel_width = rightmost;
2188 cmp->ascent = highest;
2189 cmp->descent = - lowest;
2190 if (cmp->ascent < font_ascent)
2191 cmp->ascent = font_ascent;
2192 if (cmp->descent < font_descent)
2193 cmp->descent = font_descent;
2194 }
2195
2196 it->pixel_width = cmp->pixel_width;
2197 it->ascent = it->phys_ascent = cmp->ascent;
2198 it->descent = it->phys_descent = cmp->descent;
2199
2200 if (face->box != FACE_NO_BOX)
2201 {
2202 int thick = face->box_line_width;
2203 it->ascent += thick;
2204 it->descent += thick;
2205
2206 if (it->start_of_box_run_p)
2207 it->pixel_width += thick;
2208 if (it->end_of_box_run_p)
2209 it->pixel_width += thick;
2210 }
2211
2212 /* If face has an overline, add the height of the overline
2213 (1 pixel) and a 1 pixel margin to the character height. */
2214 if (face->overline_p)
2215 it->ascent += 2;
2216
2217 take_vertical_position_into_account (it);
2218
2219 if (it->glyph_row)
2220 x_append_composite_glyph (it);
2221 }
06a2c219
GM
2222 else if (it->what == IT_IMAGE)
2223 x_produce_image_glyph (it);
2224 else if (it->what == IT_STRETCH)
2225 x_produce_stretch_glyph (it);
2226
2227 /* Accumulate dimensions. */
2228 xassert (it->ascent >= 0 && it->descent > 0);
2229 if (it->area == TEXT_AREA)
2230 it->current_x += it->pixel_width;
66ac4b0e 2231
06a2c219
GM
2232 it->max_ascent = max (it->max_ascent, it->ascent);
2233 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2234 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2235 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2236}
2237
2238
2239/* Estimate the pixel height of the mode or top line on frame F.
2240 FACE_ID specifies what line's height to estimate. */
2241
2242int
2243x_estimate_mode_line_height (f, face_id)
2244 struct frame *f;
2245 enum face_id face_id;
2246{
2247 int height = 1;
2248
2249 /* This function is called so early when Emacs starts that the face
2250 cache and mode line face are not yet initialized. */
2251 if (FRAME_FACE_CACHE (f))
2252 {
2253 struct face *face = FACE_FROM_ID (f, face_id);
2254 if (face)
2255 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2256 }
2257
2258 return height;
2259}
2260
2261\f
2262/***********************************************************************
2263 Glyph display
2264 ***********************************************************************/
2265
2266/* A sequence of glyphs to be drawn in the same face.
2267
2268 This data structure is not really completely X specific, so it
2269 could possibly, at least partially, be useful for other systems. It
2270 is currently not part of the external redisplay interface because
2271 it's not clear what other systems will need. */
2272
2273struct glyph_string
2274{
2275 /* X-origin of the string. */
2276 int x;
2277
2278 /* Y-origin and y-position of the base line of this string. */
2279 int y, ybase;
2280
2281 /* The width of the string, not including a face extension. */
2282 int width;
2283
2284 /* The width of the string, including a face extension. */
2285 int background_width;
2286
2287 /* The height of this string. This is the height of the line this
2288 string is drawn in, and can be different from the height of the
2289 font the string is drawn in. */
2290 int height;
2291
2292 /* Number of pixels this string overwrites in front of its x-origin.
2293 This number is zero if the string has an lbearing >= 0; it is
2294 -lbearing, if the string has an lbearing < 0. */
2295 int left_overhang;
2296
2297 /* Number of pixels this string overwrites past its right-most
2298 nominal x-position, i.e. x + width. Zero if the string's
2299 rbearing is <= its nominal width, rbearing - width otherwise. */
2300 int right_overhang;
2301
2302 /* The frame on which the glyph string is drawn. */
2303 struct frame *f;
2304
2305 /* The window on which the glyph string is drawn. */
2306 struct window *w;
2307
2308 /* X display and window for convenience. */
2309 Display *display;
2310 Window window;
2311
2312 /* The glyph row for which this string was built. It determines the
2313 y-origin and height of the string. */
2314 struct glyph_row *row;
2315
2316 /* The area within row. */
2317 enum glyph_row_area area;
2318
2319 /* Characters to be drawn, and number of characters. */
2320 XChar2b *char2b;
2321 int nchars;
2322
2323 /* Character set of this glyph string. */
2324 int charset;
2325
2326 /* A face-override for drawing cursors, mouse face and similar. */
2327 enum draw_glyphs_face hl;
2328
2329 /* Face in which this string is to be drawn. */
2330 struct face *face;
2331
2332 /* Font in which this string is to be drawn. */
2333 XFontStruct *font;
2334
2335 /* Font info for this string. */
2336 struct font_info *font_info;
2337
b4192550
KH
2338 /* Non-null means this string describes (part of) a composition.
2339 All characters from char2b are drawn composed. */
2340 struct composition *cmp;
06a2c219
GM
2341
2342 /* Index of this glyph string's first character in the glyph
b4192550
KH
2343 definition of CMP. If this is zero, this glyph string describes
2344 the first character of a composition. */
06a2c219
GM
2345 int gidx;
2346
2347 /* 1 means this glyph strings face has to be drawn to the right end
2348 of the window's drawing area. */
2349 unsigned extends_to_end_of_line_p : 1;
2350
2351 /* 1 means the background of this string has been drawn. */
2352 unsigned background_filled_p : 1;
2353
2354 /* 1 means glyph string must be drawn with 16-bit functions. */
2355 unsigned two_byte_p : 1;
2356
2357 /* 1 means that the original font determined for drawing this glyph
2358 string could not be loaded. The member `font' has been set to
2359 the frame's default font in this case. */
2360 unsigned font_not_found_p : 1;
2361
2362 /* 1 means that the face in which this glyph string is drawn has a
2363 stipple pattern. */
2364 unsigned stippled_p : 1;
2365
66ac4b0e
GM
2366 /* 1 means only the foreground of this glyph string must be drawn,
2367 and we should use the physical height of the line this glyph
2368 string appears in as clip rect. */
2369 unsigned for_overlaps_p : 1;
2370
06a2c219
GM
2371 /* The GC to use for drawing this glyph string. */
2372 GC gc;
2373
2374 /* A pointer to the first glyph in the string. This glyph
2375 corresponds to char2b[0]. Needed to draw rectangles if
2376 font_not_found_p is 1. */
2377 struct glyph *first_glyph;
2378
2379 /* Image, if any. */
2380 struct image *img;
2381
2382 struct glyph_string *next, *prev;
2383};
2384
2385
5c187dee 2386#if 0
06a2c219
GM
2387
2388static void
2389x_dump_glyph_string (s)
2390 struct glyph_string *s;
2391{
2392 fprintf (stderr, "glyph string\n");
2393 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2394 s->x, s->y, s->width, s->height);
2395 fprintf (stderr, " ybase = %d\n", s->ybase);
2396 fprintf (stderr, " hl = %d\n", s->hl);
2397 fprintf (stderr, " left overhang = %d, right = %d\n",
2398 s->left_overhang, s->right_overhang);
2399 fprintf (stderr, " nchars = %d\n", s->nchars);
2400 fprintf (stderr, " extends to end of line = %d\n",
2401 s->extends_to_end_of_line_p);
2402 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2403 fprintf (stderr, " bg width = %d\n", s->background_width);
2404}
2405
2406#endif /* GLYPH_DEBUG */
2407
2408
2409
2410static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2411 struct glyph_string **,
2412 struct glyph_string *,
2413 struct glyph_string *));
2414static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2415 struct glyph_string **,
2416 struct glyph_string *,
2417 struct glyph_string *));
2418static void x_append_glyph_string P_ ((struct glyph_string **,
2419 struct glyph_string **,
2420 struct glyph_string *));
2421static int x_left_overwritten P_ ((struct glyph_string *));
2422static int x_left_overwriting P_ ((struct glyph_string *));
2423static int x_right_overwritten P_ ((struct glyph_string *));
2424static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2425static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2426 int));
06a2c219
GM
2427static void x_init_glyph_string P_ ((struct glyph_string *,
2428 XChar2b *, struct window *,
2429 struct glyph_row *,
2430 enum glyph_row_area, int,
2431 enum draw_glyphs_face));
2432static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2433 enum glyph_row_area, int, int,
66ac4b0e 2434 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2435static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2436static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2437static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2438 int));
2439static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2440static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2441static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2442static void x_draw_glyph_string P_ ((struct glyph_string *));
2443static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2444static void x_set_cursor_gc P_ ((struct glyph_string *));
2445static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2446static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2447static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2448 int *, int *));
2449static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2450static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2451 unsigned long *, double, int));
06a2c219 2452static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2453 double, int, unsigned long));
06a2c219
GM
2454static void x_setup_relief_colors P_ ((struct glyph_string *));
2455static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2456static void x_draw_image_relief P_ ((struct glyph_string *));
2457static void x_draw_image_foreground P_ ((struct glyph_string *));
2458static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2459static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2460static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2461 int, int, int));
2462static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2463 int, int, int, int, XRectangle *));
2464static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2465 int, int, int, XRectangle *));
66ac4b0e
GM
2466static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2467 enum glyph_row_area));
06a2c219
GM
2468
2469
06a2c219
GM
2470/* Append the list of glyph strings with head H and tail T to the list
2471 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2472
2473static INLINE void
2474x_append_glyph_string_lists (head, tail, h, t)
2475 struct glyph_string **head, **tail;
2476 struct glyph_string *h, *t;
2477{
2478 if (h)
2479 {
2480 if (*head)
2481 (*tail)->next = h;
2482 else
2483 *head = h;
2484 h->prev = *tail;
2485 *tail = t;
2486 }
2487}
2488
2489
2490/* Prepend the list of glyph strings with head H and tail T to the
2491 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2492 result. */
2493
2494static INLINE void
2495x_prepend_glyph_string_lists (head, tail, h, t)
2496 struct glyph_string **head, **tail;
2497 struct glyph_string *h, *t;
2498{
2499 if (h)
2500 {
2501 if (*head)
2502 (*head)->prev = t;
2503 else
2504 *tail = t;
2505 t->next = *head;
2506 *head = h;
2507 }
2508}
2509
2510
2511/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2512 Set *HEAD and *TAIL to the resulting list. */
2513
2514static INLINE void
2515x_append_glyph_string (head, tail, s)
2516 struct glyph_string **head, **tail;
2517 struct glyph_string *s;
2518{
2519 s->next = s->prev = NULL;
2520 x_append_glyph_string_lists (head, tail, s, s);
2521}
2522
2523
2524/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2525 face. */
2526
2527static void
2528x_set_cursor_gc (s)
2529 struct glyph_string *s;
2530{
2531 if (s->font == FRAME_FONT (s->f)
2532 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2533 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2534 && !s->cmp)
06a2c219
GM
2535 s->gc = s->f->output_data.x->cursor_gc;
2536 else
2537 {
2538 /* Cursor on non-default face: must merge. */
2539 XGCValues xgcv;
2540 unsigned long mask;
2541
2542 xgcv.background = s->f->output_data.x->cursor_pixel;
2543 xgcv.foreground = s->face->background;
2544
2545 /* If the glyph would be invisible, try a different foreground. */
2546 if (xgcv.foreground == xgcv.background)
2547 xgcv.foreground = s->face->foreground;
2548 if (xgcv.foreground == xgcv.background)
2549 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2550 if (xgcv.foreground == xgcv.background)
2551 xgcv.foreground = s->face->foreground;
2552
2553 /* Make sure the cursor is distinct from text in this face. */
2554 if (xgcv.background == s->face->background
2555 && xgcv.foreground == s->face->foreground)
2556 {
2557 xgcv.background = s->face->foreground;
2558 xgcv.foreground = s->face->background;
2559 }
2560
2561 IF_DEBUG (x_check_font (s->f, s->font));
2562 xgcv.font = s->font->fid;
2563 xgcv.graphics_exposures = False;
2564 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2565
2566 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2567 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2568 mask, &xgcv);
2569 else
2570 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2571 = XCreateGC (s->display, s->window, mask, &xgcv);
2572
2573 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2574 }
2575}
2576
2577
2578/* Set up S->gc of glyph string S for drawing text in mouse face. */
2579
2580static void
2581x_set_mouse_face_gc (s)
2582 struct glyph_string *s;
2583{
2584 int face_id;
2585
2586 /* What face has to be used for the mouse face? */
2587 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2588 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2589 s->face = FACE_FROM_ID (s->f, face_id);
2590 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2591
2592 /* If font in this face is same as S->font, use it. */
2593 if (s->font == s->face->font)
2594 s->gc = s->face->gc;
2595 else
2596 {
2597 /* Otherwise construct scratch_cursor_gc with values from FACE
2598 but font FONT. */
2599 XGCValues xgcv;
2600 unsigned long mask;
2601
2602 xgcv.background = s->face->background;
2603 xgcv.foreground = s->face->foreground;
2604 IF_DEBUG (x_check_font (s->f, s->font));
2605 xgcv.font = s->font->fid;
2606 xgcv.graphics_exposures = False;
2607 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2608
2609 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2610 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2611 mask, &xgcv);
2612 else
2613 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2614 = XCreateGC (s->display, s->window, mask, &xgcv);
2615
2616 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2617 }
2618
2619 xassert (s->gc != 0);
2620}
2621
2622
2623/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2624 Faces to use in the mode line have already been computed when the
2625 matrix was built, so there isn't much to do, here. */
2626
2627static INLINE void
2628x_set_mode_line_face_gc (s)
2629 struct glyph_string *s;
2630{
2631 s->gc = s->face->gc;
2632 xassert (s->gc != 0);
2633}
2634
2635
2636/* Set S->gc of glyph string S for drawing that glyph string. Set
2637 S->stippled_p to a non-zero value if the face of S has a stipple
2638 pattern. */
2639
2640static INLINE void
2641x_set_glyph_string_gc (s)
2642 struct glyph_string *s;
2643{
2644 if (s->hl == DRAW_NORMAL_TEXT)
2645 {
2646 s->gc = s->face->gc;
2647 s->stippled_p = s->face->stipple != 0;
2648 }
2649 else if (s->hl == DRAW_INVERSE_VIDEO)
2650 {
2651 x_set_mode_line_face_gc (s);
2652 s->stippled_p = s->face->stipple != 0;
2653 }
2654 else if (s->hl == DRAW_CURSOR)
2655 {
2656 x_set_cursor_gc (s);
2657 s->stippled_p = 0;
2658 }
2659 else if (s->hl == DRAW_MOUSE_FACE)
2660 {
2661 x_set_mouse_face_gc (s);
2662 s->stippled_p = s->face->stipple != 0;
2663 }
2664 else if (s->hl == DRAW_IMAGE_RAISED
2665 || s->hl == DRAW_IMAGE_SUNKEN)
2666 {
2667 s->gc = s->face->gc;
2668 s->stippled_p = s->face->stipple != 0;
2669 }
2670 else
2671 {
2672 s->gc = s->face->gc;
2673 s->stippled_p = s->face->stipple != 0;
2674 }
2675
2676 /* GC must have been set. */
2677 xassert (s->gc != 0);
2678}
2679
2680
2681/* Return in *R the clipping rectangle for glyph string S. */
2682
2683static void
2684x_get_glyph_string_clip_rect (s, r)
2685 struct glyph_string *s;
2686 XRectangle *r;
2687{
2688 if (s->row->full_width_p)
2689 {
2690 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2691 int canon_x = CANON_X_UNIT (s->f);
2692
2693 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2694 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2695
2696 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2697 {
1da3fd71 2698 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2699 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2700 r->x -= width;
2701 }
2702
b9432a85 2703 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2704
06a2c219
GM
2705 /* Unless displaying a mode or menu bar line, which are always
2706 fully visible, clip to the visible part of the row. */
2707 if (s->w->pseudo_window_p)
2708 r->height = s->row->visible_height;
2709 else
2710 r->height = s->height;
2711 }
2712 else
2713 {
2714 /* This is a text line that may be partially visible. */
2715 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2716 r->width = window_box_width (s->w, s->area);
2717 r->height = s->row->visible_height;
2718 }
2719
2720 /* Don't use S->y for clipping because it doesn't take partially
2721 visible lines into account. For example, it can be negative for
2722 partially visible lines at the top of a window. */
2723 if (!s->row->full_width_p
2724 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2725 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2726 else
2727 r->y = max (0, s->row->y);
06a2c219 2728
9ea173e8 2729 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2730 at the top of the window. */
9ea173e8 2731 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2732 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2733
2734 /* If S draws overlapping rows, it's sufficient to use the top and
2735 bottom of the window for clipping because this glyph string
2736 intentionally draws over other lines. */
2737 if (s->for_overlaps_p)
2738 {
045dee35 2739 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2740 r->height = window_text_bottom_y (s->w) - r->y;
2741 }
2742
2743 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2744}
2745
2746
2747/* Set clipping for output of glyph string S. S may be part of a mode
2748 line or menu if we don't have X toolkit support. */
2749
2750static INLINE void
2751x_set_glyph_string_clipping (s)
2752 struct glyph_string *s;
2753{
2754 XRectangle r;
2755 x_get_glyph_string_clip_rect (s, &r);
2756 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2757}
2758
2759
2760/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2761 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2762
2763static INLINE void
2764x_compute_glyph_string_overhangs (s)
2765 struct glyph_string *s;
2766{
b4192550 2767 if (s->cmp == NULL
06a2c219
GM
2768 && s->first_glyph->type == CHAR_GLYPH)
2769 {
2770 XCharStruct cs;
2771 int direction, font_ascent, font_descent;
2772 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2773 &font_ascent, &font_descent, &cs);
2774 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2775 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2776 }
2777}
2778
2779
2780/* Compute overhangs and x-positions for glyph string S and its
2781 predecessors, or successors. X is the starting x-position for S.
2782 BACKWARD_P non-zero means process predecessors. */
2783
2784static void
2785x_compute_overhangs_and_x (s, x, backward_p)
2786 struct glyph_string *s;
2787 int x;
2788 int backward_p;
2789{
2790 if (backward_p)
2791 {
2792 while (s)
2793 {
2794 x_compute_glyph_string_overhangs (s);
2795 x -= s->width;
2796 s->x = x;
2797 s = s->prev;
2798 }
2799 }
2800 else
2801 {
2802 while (s)
2803 {
2804 x_compute_glyph_string_overhangs (s);
2805 s->x = x;
2806 x += s->width;
2807 s = s->next;
2808 }
2809 }
2810}
2811
2812
2813/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2814 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2815 assumed to be zero. */
06a2c219
GM
2816
2817static void
2818x_get_glyph_overhangs (glyph, f, left, right)
2819 struct glyph *glyph;
2820 struct frame *f;
2821 int *left, *right;
2822{
2823 int c;
2824
2825 *left = *right = 0;
2826
b4192550 2827 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2828 {
2829 XFontStruct *font;
2830 struct face *face;
2831 struct font_info *font_info;
2832 XChar2b char2b;
2833
2834 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2835 font = face->font;
2836 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2837 if (font)
2838 {
2839 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2840
2841 if (pcm->rbearing > pcm->width)
2842 *right = pcm->rbearing - pcm->width;
2843 if (pcm->lbearing < 0)
2844 *left = -pcm->lbearing;
2845 }
2846 }
2847}
2848
2849
2850/* Return the index of the first glyph preceding glyph string S that
2851 is overwritten by S because of S's left overhang. Value is -1
2852 if no glyphs are overwritten. */
2853
2854static int
2855x_left_overwritten (s)
2856 struct glyph_string *s;
2857{
2858 int k;
2859
2860 if (s->left_overhang)
2861 {
2862 int x = 0, i;
2863 struct glyph *glyphs = s->row->glyphs[s->area];
2864 int first = s->first_glyph - glyphs;
2865
2866 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2867 x -= glyphs[i].pixel_width;
2868
2869 k = i + 1;
2870 }
2871 else
2872 k = -1;
2873
2874 return k;
2875}
2876
2877
2878/* Return the index of the first glyph preceding glyph string S that
2879 is overwriting S because of its right overhang. Value is -1 if no
2880 glyph in front of S overwrites S. */
2881
2882static int
2883x_left_overwriting (s)
2884 struct glyph_string *s;
2885{
2886 int i, k, x;
2887 struct glyph *glyphs = s->row->glyphs[s->area];
2888 int first = s->first_glyph - glyphs;
2889
2890 k = -1;
2891 x = 0;
2892 for (i = first - 1; i >= 0; --i)
2893 {
2894 int left, right;
2895 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2896 if (x + right > 0)
2897 k = i;
2898 x -= glyphs[i].pixel_width;
2899 }
2900
2901 return k;
2902}
2903
2904
2905/* Return the index of the last glyph following glyph string S that is
2906 not overwritten by S because of S's right overhang. Value is -1 if
2907 no such glyph is found. */
2908
2909static int
2910x_right_overwritten (s)
2911 struct glyph_string *s;
2912{
2913 int k = -1;
2914
2915 if (s->right_overhang)
2916 {
2917 int x = 0, i;
2918 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2919 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2920 int end = s->row->used[s->area];
2921
2922 for (i = first; i < end && s->right_overhang > x; ++i)
2923 x += glyphs[i].pixel_width;
2924
2925 k = i;
2926 }
2927
2928 return k;
2929}
2930
2931
2932/* Return the index of the last glyph following glyph string S that
2933 overwrites S because of its left overhang. Value is negative
2934 if no such glyph is found. */
2935
2936static int
2937x_right_overwriting (s)
2938 struct glyph_string *s;
2939{
2940 int i, k, x;
2941 int end = s->row->used[s->area];
2942 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2943 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2944
2945 k = -1;
2946 x = 0;
2947 for (i = first; i < end; ++i)
2948 {
2949 int left, right;
2950 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2951 if (x - left < 0)
2952 k = i;
2953 x += glyphs[i].pixel_width;
2954 }
2955
2956 return k;
2957}
2958
2959
2960/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2961
2962static INLINE void
2963x_clear_glyph_string_rect (s, x, y, w, h)
2964 struct glyph_string *s;
2965 int x, y, w, h;
2966{
2967 XGCValues xgcv;
2968 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2969 XSetForeground (s->display, s->gc, xgcv.background);
2970 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2971 XSetForeground (s->display, s->gc, xgcv.foreground);
2972}
2973
2974
2975/* Draw the background of glyph_string S. If S->background_filled_p
2976 is non-zero don't draw it. FORCE_P non-zero means draw the
2977 background even if it wouldn't be drawn normally. This is used
b4192550
KH
2978 when a string preceding S draws into the background of S, or S
2979 contains the first component of a composition. */
06a2c219
GM
2980
2981static void
2982x_draw_glyph_string_background (s, force_p)
2983 struct glyph_string *s;
2984 int force_p;
2985{
2986 /* Nothing to do if background has already been drawn or if it
2987 shouldn't be drawn in the first place. */
2988 if (!s->background_filled_p)
2989 {
b4192550 2990 if (s->stippled_p)
06a2c219
GM
2991 {
2992 /* Fill background with a stipple pattern. */
2993 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2994 XFillRectangle (s->display, s->window, s->gc, s->x,
2995 s->y + s->face->box_line_width,
2996 s->background_width,
2997 s->height - 2 * s->face->box_line_width);
2998 XSetFillStyle (s->display, s->gc, FillSolid);
2999 s->background_filled_p = 1;
3000 }
3001 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3002 || s->font_not_found_p
3003 || s->extends_to_end_of_line_p
06a2c219
GM
3004 || force_p)
3005 {
3006 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3007 s->background_width,
3008 s->height - 2 * s->face->box_line_width);
3009 s->background_filled_p = 1;
3010 }
3011 }
3012}
3013
3014
3015/* Draw the foreground of glyph string S. */
3016
3017static void
3018x_draw_glyph_string_foreground (s)
3019 struct glyph_string *s;
3020{
3021 int i, x;
3022
3023 /* If first glyph of S has a left box line, start drawing the text
3024 of S to the right of that box line. */
3025 if (s->face->box != FACE_NO_BOX
3026 && s->first_glyph->left_box_line_p)
3027 x = s->x + s->face->box_line_width;
3028 else
3029 x = s->x;
3030
b4192550
KH
3031 /* Draw characters of S as rectangles if S's font could not be
3032 loaded. */
3033 if (s->font_not_found_p)
06a2c219 3034 {
b4192550 3035 for (i = 0; i < s->nchars; ++i)
06a2c219 3036 {
b4192550
KH
3037 struct glyph *g = s->first_glyph + i;
3038 XDrawRectangle (s->display, s->window,
3039 s->gc, x, s->y, g->pixel_width - 1,
3040 s->height - 1);
3041 x += g->pixel_width;
06a2c219
GM
3042 }
3043 }
3044 else
3045 {
b4192550
KH
3046 char *char1b = (char *) s->char2b;
3047 int boff = s->font_info->baseline_offset;
06a2c219 3048
b4192550
KH
3049 if (s->font_info->vertical_centering)
3050 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3051
3052 /* If we can use 8-bit functions, condense S->char2b. */
3053 if (!s->two_byte_p)
3054 for (i = 0; i < s->nchars; ++i)
3055 char1b[i] = s->char2b[i].byte2;
3056
3057 /* Draw text with XDrawString if background has already been
3058 filled. Otherwise, use XDrawImageString. (Note that
3059 XDrawImageString is usually faster than XDrawString.) Always
3060 use XDrawImageString when drawing the cursor so that there is
3061 no chance that characters under a box cursor are invisible. */
3062 if (s->for_overlaps_p
3063 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3064 {
3065 /* Draw characters with 16-bit or 8-bit functions. */
3066 if (s->two_byte_p)
3067 XDrawString16 (s->display, s->window, s->gc, x,
3068 s->ybase - boff, s->char2b, s->nchars);
3069 else
3070 XDrawString (s->display, s->window, s->gc, x,
3071 s->ybase - boff, char1b, s->nchars);
3072 }
06a2c219
GM
3073 else
3074 {
b4192550
KH
3075 if (s->two_byte_p)
3076 XDrawImageString16 (s->display, s->window, s->gc, x,
3077 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3078 else
b4192550
KH
3079 XDrawImageString (s->display, s->window, s->gc, x,
3080 s->ybase - boff, char1b, s->nchars);
3081 }
3082 }
3083}
06a2c219 3084
b4192550 3085/* Draw the foreground of composite glyph string S. */
06a2c219 3086
b4192550
KH
3087static void
3088x_draw_composite_glyph_string_foreground (s)
3089 struct glyph_string *s;
3090{
3091 int i, x;
06a2c219 3092
b4192550
KH
3093 /* If first glyph of S has a left box line, start drawing the text
3094 of S to the right of that box line. */
3095 if (s->face->box != FACE_NO_BOX
3096 && s->first_glyph->left_box_line_p)
3097 x = s->x + s->face->box_line_width;
3098 else
3099 x = s->x;
06a2c219 3100
b4192550
KH
3101 /* S is a glyph string for a composition. S->gidx is the index of
3102 the first character drawn for glyphs of this composition.
3103 S->gidx == 0 means we are drawing the very first character of
3104 this composition. */
06a2c219 3105
b4192550
KH
3106 /* Draw a rectangle for the composition if the font for the very
3107 first character of the composition could not be loaded. */
3108 if (s->font_not_found_p)
3109 {
3110 if (s->gidx == 0)
3111 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3112 s->width - 1, s->height - 1);
3113 }
3114 else
3115 {
3116 for (i = 0; i < s->nchars; i++, ++s->gidx)
3117 XDrawString16 (s->display, s->window, s->gc,
3118 x + s->cmp->offsets[s->gidx * 2],
3119 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3120 s->char2b + i, 1);
06a2c219
GM
3121 }
3122}
3123
3124
80c32bcc
GM
3125#ifdef USE_X_TOOLKIT
3126
3127/* Allocate the color COLOR->pixel on the screen and display of
3128 widget WIDGET in colormap CMAP. If an exact match cannot be
3129 allocated, try the nearest color available. Value is non-zero
3130 if successful. This is called from lwlib. */
3131
3132int
3133x_alloc_nearest_color_for_widget (widget, cmap, color)
3134 Widget widget;
3135 Colormap cmap;
3136 XColor *color;
3137{
3138 struct frame *f;
3139 struct x_display_info *dpyinfo;
5c187dee 3140 Lisp_Object tail;
80c32bcc
GM
3141
3142 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3143
3144 /* Find the top-level shell of the widget. Note that this function
3145 can be called when the widget is not yet realized, so XtWindow
3146 (widget) == 0. That's the reason we can't simply use
3147 x_any_window_to_frame. */
3148 while (!XtIsTopLevelShell (widget))
3149 widget = XtParent (widget);
3150
3151 /* Look for a frame with that top-level widget. Allocate the color
3152 on that frame to get the right gamma correction value. */
3153 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3154 if (GC_FRAMEP (XCAR (tail))
3155 && (f = XFRAME (XCAR (tail)),
3156 (f->output_data.nothing != 1
3157 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3158 && f->output_data.x->widget == widget)
3159 return x_alloc_nearest_color (f, cmap, color);
3160
3161 abort ();
3162}
3163
3164#endif /* USE_X_TOOLKIT */
3165
3166
06a2c219
GM
3167/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3168 CMAP. If an exact match can't be allocated, try the nearest color
3169 available. Value is non-zero if successful. Set *COLOR to the
3170 color allocated. */
3171
3172int
80c32bcc
GM
3173x_alloc_nearest_color (f, cmap, color)
3174 struct frame *f;
06a2c219
GM
3175 Colormap cmap;
3176 XColor *color;
3177{
80c32bcc
GM
3178 Display *display = FRAME_X_DISPLAY (f);
3179 Screen *screen = FRAME_X_SCREEN (f);
3180 int rc;
3181
3182 gamma_correct (f, color);
3183 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3184 if (rc == 0)
3185 {
3186 /* If we got to this point, the colormap is full, so we're going
3187 to try to get the next closest color. The algorithm used is
3188 a least-squares matching, which is what X uses for closest
3189 color matching with StaticColor visuals. */
3190 int nearest, i;
3191 unsigned long nearest_delta = ~0;
3192 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3193 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3194
3195 for (i = 0; i < ncells; ++i)
3196 cells[i].pixel = i;
3197 XQueryColors (display, cmap, cells, ncells);
3198
3199 for (nearest = i = 0; i < ncells; ++i)
3200 {
3201 long dred = (color->red >> 8) - (cells[i].red >> 8);
3202 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3203 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3204 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3205
3206 if (delta < nearest_delta)
3207 {
3208 nearest = i;
3209 nearest_delta = delta;
3210 }
3211 }
3212
3213 color->red = cells[nearest].red;
3214 color->green = cells[nearest].green;
3215 color->blue = cells[nearest].blue;
3216 rc = XAllocColor (display, cmap, color);
3217 }
3218
3219 return rc;
3220}
3221
3222
3223/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3224 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3225 If this produces the same color as PIXEL, try a color where all RGB
3226 values have DELTA added. Return the allocated color in *PIXEL.
3227 DISPLAY is the X display, CMAP is the colormap to operate on.
3228 Value is non-zero if successful. */
3229
3230static int
3231x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3232 struct frame *f;
3233 Display *display;
3234 Colormap cmap;
3235 unsigned long *pixel;
68c45bf0 3236 double factor;
06a2c219
GM
3237 int delta;
3238{
3239 XColor color, new;
3240 int success_p;
3241
3242 /* Get RGB color values. */
3243 color.pixel = *pixel;
3244 XQueryColor (display, cmap, &color);
3245
3246 /* Change RGB values by specified FACTOR. Avoid overflow! */
3247 xassert (factor >= 0);
3248 new.red = min (0xffff, factor * color.red);
3249 new.green = min (0xffff, factor * color.green);
3250 new.blue = min (0xffff, factor * color.blue);
3251
3252 /* Try to allocate the color. */
80c32bcc 3253 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3254 if (success_p)
3255 {
3256 if (new.pixel == *pixel)
3257 {
3258 /* If we end up with the same color as before, try adding
3259 delta to the RGB values. */
3260 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3261
3262 /* If display has an immutable color map, freeing colors is
3263 not necessary and some servers don't allow it. So don't
3264 do it. */
3265 if (class != StaticColor
3266 && class != StaticGray
3267 && class != TrueColor)
3268 XFreeColors (display, cmap, &new.pixel, 1, 0);
3269
3270 new.red = min (0xffff, delta + color.red);
3271 new.green = min (0xffff, delta + color.green);
3272 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3273 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3274 }
3275 else
3276 success_p = 1;
3277 *pixel = new.pixel;
3278 }
3279
3280 return success_p;
3281}
3282
3283
3284/* Set up the foreground color for drawing relief lines of glyph
3285 string S. RELIEF is a pointer to a struct relief containing the GC
3286 with which lines will be drawn. Use a color that is FACTOR or
3287 DELTA lighter or darker than the relief's background which is found
3288 in S->f->output_data.x->relief_background. If such a color cannot
3289 be allocated, use DEFAULT_PIXEL, instead. */
3290
3291static void
3292x_setup_relief_color (f, relief, factor, delta, default_pixel)
3293 struct frame *f;
3294 struct relief *relief;
68c45bf0 3295 double factor;
06a2c219
GM
3296 int delta;
3297 unsigned long default_pixel;
3298{
3299 XGCValues xgcv;
3300 struct x_output *di = f->output_data.x;
3301 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3302 unsigned long pixel;
3303 unsigned long background = di->relief_background;
3304 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
dcd08bfb
GM
3305 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3306 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3307
3308 xgcv.graphics_exposures = False;
3309 xgcv.line_width = 1;
3310
3311 /* Free previously allocated color. The color cell will be reused
3312 when it has been freed as many times as it was allocated, so this
3313 doesn't affect faces using the same colors. */
3314 if (relief->gc
3315 && relief->allocated_p)
3316 {
3317 /* If display has an immutable color map, freeing colors is not
3318 necessary and some servers don't allow it. So don't do it. */
dcd08bfb 3319 int class = dpyinfo->visual->class;
06a2c219
GM
3320 if (class != StaticColor
3321 && class != StaticGray
3322 && class != TrueColor)
dcd08bfb 3323 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
06a2c219
GM
3324 relief->allocated_p = 0;
3325 }
3326
3327 /* Allocate new color. */
3328 xgcv.foreground = default_pixel;
3329 pixel = background;
dcd08bfb
GM
3330 if (dpyinfo->n_planes != 1
3331 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3332 {
3333 relief->allocated_p = 1;
3334 xgcv.foreground = relief->pixel = pixel;
3335 }
3336
3337 if (relief->gc == 0)
3338 {
dcd08bfb 3339 xgcv.stipple = dpyinfo->gray;
06a2c219 3340 mask |= GCStipple;
dcd08bfb 3341 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3342 }
3343 else
dcd08bfb 3344 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3345}
3346
3347
3348/* Set up colors for the relief lines around glyph string S. */
3349
3350static void
3351x_setup_relief_colors (s)
3352 struct glyph_string *s;
3353{
3354 struct x_output *di = s->f->output_data.x;
3355 unsigned long color;
3356
3357 if (s->face->use_box_color_for_shadows_p)
3358 color = s->face->box_color;
3359 else
3360 {
3361 XGCValues xgcv;
3362
3363 /* Get the background color of the face. */
3364 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3365 color = xgcv.background;
3366 }
3367
3368 if (di->white_relief.gc == 0
3369 || color != di->relief_background)
3370 {
3371 di->relief_background = color;
3372 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3373 WHITE_PIX_DEFAULT (s->f));
3374 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3375 BLACK_PIX_DEFAULT (s->f));
3376 }
3377}
3378
3379
3380/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3381 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3382 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3383 relief. LEFT_P non-zero means draw a relief on the left side of
3384 the rectangle. RIGHT_P non-zero means draw a relief on the right
3385 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3386 when drawing. */
3387
3388static void
3389x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3390 raised_p, left_p, right_p, clip_rect)
3391 struct frame *f;
3392 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3393 XRectangle *clip_rect;
3394{
3395 int i;
3396 GC gc;
3397
3398 if (raised_p)
3399 gc = f->output_data.x->white_relief.gc;
3400 else
3401 gc = f->output_data.x->black_relief.gc;
3402 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3403
3404 /* Top. */
3405 for (i = 0; i < width; ++i)
3406 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3407 left_x + i * left_p, top_y + i,
3408 right_x + 1 - i * right_p, top_y + i);
3409
3410 /* Left. */
3411 if (left_p)
3412 for (i = 0; i < width; ++i)
3413 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3414 left_x + i, top_y + i, left_x + i, bottom_y - i);
3415
3416 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3417 if (raised_p)
3418 gc = f->output_data.x->black_relief.gc;
3419 else
3420 gc = f->output_data.x->white_relief.gc;
3421 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3422
3423 /* Bottom. */
3424 for (i = 0; i < width; ++i)
3425 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3426 left_x + i * left_p, bottom_y - i,
3427 right_x + 1 - i * right_p, bottom_y - i);
3428
3429 /* Right. */
3430 if (right_p)
3431 for (i = 0; i < width; ++i)
3432 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3433 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3434
3435 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3436}
3437
3438
3439/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3440 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3441 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3442 left side of the rectangle. RIGHT_P non-zero means draw a line
3443 on the right side of the rectangle. CLIP_RECT is the clipping
3444 rectangle to use when drawing. */
3445
3446static void
3447x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3448 left_p, right_p, clip_rect)
3449 struct glyph_string *s;
3450 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3451 XRectangle *clip_rect;
3452{
3453 XGCValues xgcv;
3454
3455 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3456 XSetForeground (s->display, s->gc, s->face->box_color);
3457 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3458
3459 /* Top. */
3460 XFillRectangle (s->display, s->window, s->gc,
3461 left_x, top_y, right_x - left_x, width);
3462
3463 /* Left. */
3464 if (left_p)
3465 XFillRectangle (s->display, s->window, s->gc,
3466 left_x, top_y, width, bottom_y - top_y);
3467
3468 /* Bottom. */
3469 XFillRectangle (s->display, s->window, s->gc,
3470 left_x, bottom_y - width, right_x - left_x, width);
3471
3472 /* Right. */
3473 if (right_p)
3474 XFillRectangle (s->display, s->window, s->gc,
3475 right_x - width, top_y, width, bottom_y - top_y);
3476
3477 XSetForeground (s->display, s->gc, xgcv.foreground);
3478 XSetClipMask (s->display, s->gc, None);
3479}
3480
3481
3482/* Draw a box around glyph string S. */
3483
3484static void
3485x_draw_glyph_string_box (s)
3486 struct glyph_string *s;
3487{
3488 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3489 int left_p, right_p;
3490 struct glyph *last_glyph;
3491 XRectangle clip_rect;
3492
3493 last_x = window_box_right (s->w, s->area);
3494 if (s->row->full_width_p
3495 && !s->w->pseudo_window_p)
3496 {
110859fc 3497 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3498 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3499 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3500 }
3501
3502 /* The glyph that may have a right box line. */
b4192550 3503 last_glyph = (s->cmp || s->img
06a2c219
GM
3504 ? s->first_glyph
3505 : s->first_glyph + s->nchars - 1);
3506
3507 width = s->face->box_line_width;
3508 raised_p = s->face->box == FACE_RAISED_BOX;
3509 left_x = s->x;
3510 right_x = ((s->row->full_width_p
1da3fd71 3511 ? last_x - 1
a7aeb2de 3512 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3513 top_y = s->y;
3514 bottom_y = top_y + s->height - 1;
3515
3516 left_p = (s->first_glyph->left_box_line_p
3517 || (s->hl == DRAW_MOUSE_FACE
3518 && (s->prev == NULL
3519 || s->prev->hl != s->hl)));
3520 right_p = (last_glyph->right_box_line_p
3521 || (s->hl == DRAW_MOUSE_FACE
3522 && (s->next == NULL
3523 || s->next->hl != s->hl)));
3524
3525 x_get_glyph_string_clip_rect (s, &clip_rect);
3526
3527 if (s->face->box == FACE_SIMPLE_BOX)
3528 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3529 left_p, right_p, &clip_rect);
3530 else
3531 {
3532 x_setup_relief_colors (s);
3533 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3534 width, raised_p, left_p, right_p, &clip_rect);
3535 }
3536}
3537
3538
3539/* Draw foreground of image glyph string S. */
3540
3541static void
3542x_draw_image_foreground (s)
3543 struct glyph_string *s;
3544{
3545 int x;
3546 int y = s->ybase - IMAGE_ASCENT (s->img);
3547
3548 /* If first glyph of S has a left box line, start drawing it to the
3549 right of that line. */
3550 if (s->face->box != FACE_NO_BOX
3551 && s->first_glyph->left_box_line_p)
3552 x = s->x + s->face->box_line_width;
3553 else
3554 x = s->x;
3555
3556 /* If there is a margin around the image, adjust x- and y-position
3557 by that margin. */
3558 if (s->img->margin)
3559 {
3560 x += s->img->margin;
3561 y += s->img->margin;
3562 }
3563
3564 if (s->img->pixmap)
3565 {
3566 if (s->img->mask)
3567 {
3568 /* We can't set both a clip mask and use XSetClipRectangles
3569 because the latter also sets a clip mask. We also can't
3570 trust on the shape extension to be available
3571 (XShapeCombineRegion). So, compute the rectangle to draw
3572 manually. */
3573 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3574 | GCFunction);
3575 XGCValues xgcv;
3576 XRectangle clip_rect, image_rect, r;
3577
3578 xgcv.clip_mask = s->img->mask;
3579 xgcv.clip_x_origin = x;
3580 xgcv.clip_y_origin = y;
3581 xgcv.function = GXcopy;
3582 XChangeGC (s->display, s->gc, mask, &xgcv);
3583
3584 x_get_glyph_string_clip_rect (s, &clip_rect);
3585 image_rect.x = x;
3586 image_rect.y = y;
3587 image_rect.width = s->img->width;
3588 image_rect.height = s->img->height;
3589 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3590 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3591 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3592 }
3593 else
3594 {
3595 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3596 0, 0, s->img->width, s->img->height, x, y);
3597
3598 /* When the image has a mask, we can expect that at
3599 least part of a mouse highlight or a block cursor will
3600 be visible. If the image doesn't have a mask, make
3601 a block cursor visible by drawing a rectangle around
3602 the image. I believe it's looking better if we do
3603 nothing here for mouse-face. */
3604 if (s->hl == DRAW_CURSOR)
3605 XDrawRectangle (s->display, s->window, s->gc, x, y,
3606 s->img->width - 1, s->img->height - 1);
3607 }
3608 }
3609 else
3610 /* Draw a rectangle if image could not be loaded. */
3611 XDrawRectangle (s->display, s->window, s->gc, x, y,
3612 s->img->width - 1, s->img->height - 1);
3613}
3614
3615
3616/* Draw a relief around the image glyph string S. */
3617
3618static void
3619x_draw_image_relief (s)
3620 struct glyph_string *s;
3621{
3622 int x0, y0, x1, y1, thick, raised_p;
3623 XRectangle r;
3624 int x;
3625 int y = s->ybase - IMAGE_ASCENT (s->img);
3626
3627 /* If first glyph of S has a left box line, start drawing it to the
3628 right of that line. */
3629 if (s->face->box != FACE_NO_BOX
3630 && s->first_glyph->left_box_line_p)
3631 x = s->x + s->face->box_line_width;
3632 else
3633 x = s->x;
3634
3635 /* If there is a margin around the image, adjust x- and y-position
3636 by that margin. */
3637 if (s->img->margin)
3638 {
3639 x += s->img->margin;
3640 y += s->img->margin;
3641 }
3642
3643 if (s->hl == DRAW_IMAGE_SUNKEN
3644 || s->hl == DRAW_IMAGE_RAISED)
3645 {
9ea173e8 3646 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3647 raised_p = s->hl == DRAW_IMAGE_RAISED;
3648 }
3649 else
3650 {
3651 thick = abs (s->img->relief);
3652 raised_p = s->img->relief > 0;
3653 }
3654
3655 x0 = x - thick;
3656 y0 = y - thick;
3657 x1 = x + s->img->width + thick - 1;
3658 y1 = y + s->img->height + thick - 1;
3659
3660 x_setup_relief_colors (s);
3661 x_get_glyph_string_clip_rect (s, &r);
3662 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3663}
3664
3665
3666/* Draw the foreground of image glyph string S to PIXMAP. */
3667
3668static void
3669x_draw_image_foreground_1 (s, pixmap)
3670 struct glyph_string *s;
3671 Pixmap pixmap;
3672{
3673 int x;
3674 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3675
3676 /* If first glyph of S has a left box line, start drawing it to the
3677 right of that line. */
3678 if (s->face->box != FACE_NO_BOX
3679 && s->first_glyph->left_box_line_p)
3680 x = s->face->box_line_width;
3681 else
3682 x = 0;
3683
3684 /* If there is a margin around the image, adjust x- and y-position
3685 by that margin. */
3686 if (s->img->margin)
3687 {
3688 x += s->img->margin;
3689 y += s->img->margin;
3690 }
dc43ef94 3691
06a2c219
GM
3692 if (s->img->pixmap)
3693 {
3694 if (s->img->mask)
3695 {
3696 /* We can't set both a clip mask and use XSetClipRectangles
3697 because the latter also sets a clip mask. We also can't
3698 trust on the shape extension to be available
3699 (XShapeCombineRegion). So, compute the rectangle to draw
3700 manually. */
3701 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3702 | GCFunction);
3703 XGCValues xgcv;
3704
3705 xgcv.clip_mask = s->img->mask;
3706 xgcv.clip_x_origin = x;
3707 xgcv.clip_y_origin = y;
3708 xgcv.function = GXcopy;
3709 XChangeGC (s->display, s->gc, mask, &xgcv);
3710
3711 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3712 0, 0, s->img->width, s->img->height, x, y);
3713 XSetClipMask (s->display, s->gc, None);
3714 }
3715 else
3716 {
3717 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3718 0, 0, s->img->width, s->img->height, x, y);
3719
3720 /* When the image has a mask, we can expect that at
3721 least part of a mouse highlight or a block cursor will
3722 be visible. If the image doesn't have a mask, make
3723 a block cursor visible by drawing a rectangle around
3724 the image. I believe it's looking better if we do
3725 nothing here for mouse-face. */
3726 if (s->hl == DRAW_CURSOR)
3727 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3728 s->img->width - 1, s->img->height - 1);
3729 }
3730 }
3731 else
3732 /* Draw a rectangle if image could not be loaded. */
3733 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3734 s->img->width - 1, s->img->height - 1);
3735}
dc43ef94 3736
990ba854 3737
06a2c219
GM
3738/* Draw part of the background of glyph string S. X, Y, W, and H
3739 give the rectangle to draw. */
a9a5b0a5 3740
06a2c219
GM
3741static void
3742x_draw_glyph_string_bg_rect (s, x, y, w, h)
3743 struct glyph_string *s;
3744 int x, y, w, h;
3745{
3746 if (s->stippled_p)
3747 {
3748 /* Fill background with a stipple pattern. */
3749 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3750 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3751 XSetFillStyle (s->display, s->gc, FillSolid);
3752 }
3753 else
3754 x_clear_glyph_string_rect (s, x, y, w, h);
3755}
07e34cb0 3756
b5210ea7 3757
06a2c219 3758/* Draw image glyph string S.
dc43ef94 3759
06a2c219
GM
3760 s->y
3761 s->x +-------------------------
3762 | s->face->box
3763 |
3764 | +-------------------------
3765 | | s->img->margin
3766 | |
3767 | | +-------------------
3768 | | | the image
dc43ef94 3769
06a2c219 3770 */
dc43ef94 3771
06a2c219
GM
3772static void
3773x_draw_image_glyph_string (s)
3774 struct glyph_string *s;
3775{
3776 int x, y;
3777 int box_line_width = s->face->box_line_width;
3778 int margin = s->img->margin;
3779 int height;
3780 Pixmap pixmap = None;
3781
3782 height = s->height - 2 * box_line_width;
3783
3784 /* Fill background with face under the image. Do it only if row is
3785 taller than image or if image has a clip mask to reduce
3786 flickering. */
3787 s->stippled_p = s->face->stipple != 0;
3788 if (height > s->img->height
3789 || margin
3790 || s->img->mask
3791 || s->img->pixmap == 0
3792 || s->width != s->background_width)
3793 {
3794 if (box_line_width && s->first_glyph->left_box_line_p)
3795 x = s->x + box_line_width;
3796 else
3797 x = s->x;
3798
3799 y = s->y + box_line_width;
3800
3801 if (s->img->mask)
3802 {
3803 /* Create a pixmap as large as the glyph string Fill it with
3804 the background color. Copy the image to it, using its
3805 mask. Copy the temporary pixmap to the display. */
3806 Screen *screen = FRAME_X_SCREEN (s->f);
3807 int depth = DefaultDepthOfScreen (screen);
3808
3809 /* Create a pixmap as large as the glyph string. */
3810 pixmap = XCreatePixmap (s->display, s->window,
3811 s->background_width,
3812 s->height, depth);
3813
3814 /* Don't clip in the following because we're working on the
3815 pixmap. */
3816 XSetClipMask (s->display, s->gc, None);
3817
3818 /* Fill the pixmap with the background color/stipple. */
3819 if (s->stippled_p)
3820 {
3821 /* Fill background with a stipple pattern. */
3822 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3823 XFillRectangle (s->display, pixmap, s->gc,
3824 0, 0, s->background_width, s->height);
3825 XSetFillStyle (s->display, s->gc, FillSolid);
3826 }
3827 else
3828 {
3829 XGCValues xgcv;
3830 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3831 &xgcv);
3832 XSetForeground (s->display, s->gc, xgcv.background);
3833 XFillRectangle (s->display, pixmap, s->gc,
3834 0, 0, s->background_width, s->height);
3835 XSetForeground (s->display, s->gc, xgcv.foreground);
3836 }
3837 }
3838 else
3839 /* Implementation idea: Is it possible to construct a mask?
3840 We could look at the color at the margins of the image, and
3841 say that this color is probably the background color of the
3842 image. */
3843 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3844
3845 s->background_filled_p = 1;
3846 }
dc43ef94 3847
06a2c219
GM
3848 /* Draw the foreground. */
3849 if (pixmap != None)
3850 {
3851 x_draw_image_foreground_1 (s, pixmap);
3852 x_set_glyph_string_clipping (s);
3853 XCopyArea (s->display, pixmap, s->window, s->gc,
3854 0, 0, s->background_width, s->height, s->x, s->y);
3855 XFreePixmap (s->display, pixmap);
3856 }
3857 else
3858 x_draw_image_foreground (s);
b5210ea7 3859
06a2c219
GM
3860 /* If we must draw a relief around the image, do it. */
3861 if (s->img->relief
3862 || s->hl == DRAW_IMAGE_RAISED
3863 || s->hl == DRAW_IMAGE_SUNKEN)
3864 x_draw_image_relief (s);
3865}
8c1a6a84 3866
990ba854 3867
06a2c219 3868/* Draw stretch glyph string S. */
dc43ef94 3869
06a2c219
GM
3870static void
3871x_draw_stretch_glyph_string (s)
3872 struct glyph_string *s;
3873{
3874 xassert (s->first_glyph->type == STRETCH_GLYPH);
3875 s->stippled_p = s->face->stipple != 0;
990ba854 3876
06a2c219
GM
3877 if (s->hl == DRAW_CURSOR
3878 && !x_stretch_cursor_p)
3879 {
3880 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3881 as wide as the stretch glyph. */
3882 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3883
06a2c219
GM
3884 /* Draw cursor. */
3885 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3886
06a2c219
GM
3887 /* Clear rest using the GC of the original non-cursor face. */
3888 if (width < s->background_width)
3889 {
3890 GC gc = s->face->gc;
3891 int x = s->x + width, y = s->y;
3892 int w = s->background_width - width, h = s->height;
3893 XRectangle r;
dc43ef94 3894
06a2c219
GM
3895 x_get_glyph_string_clip_rect (s, &r);
3896 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3897
06a2c219
GM
3898 if (s->face->stipple)
3899 {
3900 /* Fill background with a stipple pattern. */
3901 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3902 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3903 XSetFillStyle (s->display, gc, FillSolid);
3904 }
3905 else
3906 {
3907 XGCValues xgcv;
3908 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3909 XSetForeground (s->display, gc, xgcv.background);
3910 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3911 XSetForeground (s->display, gc, xgcv.foreground);
3912 }
3913 }
3914 }
3915 else
3916 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3917 s->height);
3918
3919 s->background_filled_p = 1;
3920}
3921
3922
3923/* Draw glyph string S. */
3924
3925static void
3926x_draw_glyph_string (s)
3927 struct glyph_string *s;
3928{
3929 /* If S draws into the background of its successor, draw the
3930 background of the successor first so that S can draw into it.
3931 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3932 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3933 {
3934 xassert (s->next->img == NULL);
3935 x_set_glyph_string_gc (s->next);
3936 x_set_glyph_string_clipping (s->next);
3937 x_draw_glyph_string_background (s->next, 1);
3938 }
97210f4e 3939
06a2c219
GM
3940 /* Set up S->gc, set clipping and draw S. */
3941 x_set_glyph_string_gc (s);
3942 x_set_glyph_string_clipping (s);
3943
3944 switch (s->first_glyph->type)
3945 {
3946 case IMAGE_GLYPH:
3947 x_draw_image_glyph_string (s);
3948 break;
3949
3950 case STRETCH_GLYPH:
3951 x_draw_stretch_glyph_string (s);
3952 break;
3953
3954 case CHAR_GLYPH:
66ac4b0e
GM
3955 if (s->for_overlaps_p)
3956 s->background_filled_p = 1;
3957 else
3958 x_draw_glyph_string_background (s, 0);
06a2c219
GM
3959 x_draw_glyph_string_foreground (s);
3960 break;
3961
b4192550
KH
3962 case COMPOSITE_GLYPH:
3963 if (s->for_overlaps_p || s->gidx > 0)
3964 s->background_filled_p = 1;
3965 else
3966 x_draw_glyph_string_background (s, 1);
3967 x_draw_composite_glyph_string_foreground (s);
3968 break;
3969
06a2c219
GM
3970 default:
3971 abort ();
3972 }
3973
66ac4b0e 3974 if (!s->for_overlaps_p)
06a2c219 3975 {
66ac4b0e
GM
3976 /* Draw underline. */
3977 if (s->face->underline_p)
3978 {
3979 unsigned long dy, h;
06a2c219 3980
66ac4b0e
GM
3981 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3982 h = 1;
3983 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3984 dy = s->height - h;
06a2c219 3985
66ac4b0e
GM
3986 if (s->face->underline_defaulted_p)
3987 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3988 s->width, h);
3989 else
3990 {
3991 XGCValues xgcv;
3992 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3993 XSetForeground (s->display, s->gc, s->face->underline_color);
3994 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3995 s->width, h);
3996 XSetForeground (s->display, s->gc, xgcv.foreground);
3997 }
dc6f92b8 3998 }
07e34cb0 3999
66ac4b0e
GM
4000 /* Draw overline. */
4001 if (s->face->overline_p)
06a2c219 4002 {
66ac4b0e
GM
4003 unsigned long dy = 0, h = 1;
4004
4005 if (s->face->overline_color_defaulted_p)
4006 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4007 s->width, h);
4008 else
4009 {
4010 XGCValues xgcv;
4011 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4012 XSetForeground (s->display, s->gc, s->face->overline_color);
4013 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4014 s->width, h);
4015 XSetForeground (s->display, s->gc, xgcv.foreground);
4016 }
06a2c219 4017 }
06a2c219 4018
66ac4b0e
GM
4019 /* Draw strike-through. */
4020 if (s->face->strike_through_p)
06a2c219 4021 {
66ac4b0e
GM
4022 unsigned long h = 1;
4023 unsigned long dy = (s->height - h) / 2;
4024
4025 if (s->face->strike_through_color_defaulted_p)
4026 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4027 s->width, h);
4028 else
4029 {
4030 XGCValues xgcv;
4031 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4032 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4033 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4034 s->width, h);
4035 XSetForeground (s->display, s->gc, xgcv.foreground);
4036 }
06a2c219 4037 }
06a2c219 4038
66ac4b0e
GM
4039 /* Draw relief. */
4040 if (s->face->box != FACE_NO_BOX)
4041 x_draw_glyph_string_box (s);
4042 }
06a2c219
GM
4043
4044 /* Reset clipping. */
4045 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4046}
07e34cb0 4047
06a2c219 4048
b4192550
KH
4049static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4050 struct face **, int));
06a2c219 4051
06a2c219 4052
b4192550
KH
4053/* Load glyph string S with a composition components specified by S->cmp.
4054 FACES is an array of faces for all components of this composition.
4055 S->gidx is the index of the first component for S.
4056 OVERLAPS_P non-zero means S should draw the foreground only, and
4057 use its lines physical height for clipping.
06a2c219 4058
b4192550 4059 Value is the index of a component not in S. */
07e34cb0 4060
b4192550
KH
4061static int
4062x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4063 struct glyph_string *s;
b4192550 4064 struct face **faces;
66ac4b0e 4065 int overlaps_p;
07e34cb0 4066{
b4192550 4067 int i;
06a2c219 4068
b4192550 4069 xassert (s);
06a2c219 4070
b4192550 4071 s->for_overlaps_p = overlaps_p;
06a2c219 4072
b4192550
KH
4073 s->face = faces[s->gidx];
4074 s->font = s->face->font;
4075 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4076
b4192550
KH
4077 /* For all glyphs of this composition, starting at the offset
4078 S->gidx, until we reach the end of the definition or encounter a
4079 glyph that requires the different face, add it to S. */
4080 ++s->nchars;
4081 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4082 ++s->nchars;
06a2c219 4083
b4192550
KH
4084 /* All glyph strings for the same composition has the same width,
4085 i.e. the width set for the first component of the composition. */
06a2c219 4086
06a2c219
GM
4087 s->width = s->first_glyph->pixel_width;
4088
4089 /* If the specified font could not be loaded, use the frame's
4090 default font, but record the fact that we couldn't load it in
4091 the glyph string so that we can draw rectangles for the
4092 characters of the glyph string. */
4093 if (s->font == NULL)
4094 {
4095 s->font_not_found_p = 1;
4096 s->font = FRAME_FONT (s->f);
4097 }
4098
4099 /* Adjust base line for subscript/superscript text. */
4100 s->ybase += s->first_glyph->voffset;
4101
4102 xassert (s->face && s->face->gc);
4103
4104 /* This glyph string must always be drawn with 16-bit functions. */
4105 s->two_byte_p = 1;
b4192550
KH
4106
4107 return s->gidx + s->nchars;
06a2c219
GM
4108}
4109
4110
b4192550 4111/* Load glyph string S with a sequence characters.
06a2c219 4112 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4113 first glyph to consider, END is the index of the last + 1.
4114 OVERLAPS_P non-zero means S should draw the foreground only, and
4115 use its lines physical height for clipping.
4116
4117 Value is the index of the first glyph not in S. */
06a2c219
GM
4118
4119static int
66ac4b0e 4120x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4121 struct glyph_string *s;
4122 int face_id;
66ac4b0e 4123 int start, end, overlaps_p;
06a2c219
GM
4124{
4125 struct glyph *glyph, *last;
4126 int voffset;
4127
06a2c219
GM
4128 xassert (s->f == XFRAME (s->w->frame));
4129 xassert (s->nchars == 0);
4130 xassert (start >= 0 && end > start);
4131
66ac4b0e 4132 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4133 glyph = s->row->glyphs[s->area] + start;
4134 last = s->row->glyphs[s->area] + end;
4135 voffset = glyph->voffset;
4136
4137 while (glyph < last
4138 && glyph->type == CHAR_GLYPH
4139 && glyph->voffset == voffset
4140 /* Same face id implies same charset, nowadays. */
4141 && glyph->u.ch.face_id == face_id)
4142 {
4143 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4144 s->char2b + s->nchars);
4145 if (s->char2b[s->nchars].byte2 != 0)
4146 s->two_byte_p = 1;
4147
4148 ++s->nchars;
4149 xassert (s->nchars <= end - start);
4150 s->width += glyph->pixel_width;
4151 ++glyph;
4152 }
4153
4154 s->font = s->face->font;
4155 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4156
4157 /* If the specified font could not be loaded, use the frame's font,
4158 but record the fact that we couldn't load it in
4159 S->font_not_found_p so that we can draw rectangles for the
4160 characters of the glyph string. */
4161 if (s->font == NULL)
4162 {
4163 s->font_not_found_p = 1;
4164 s->font = FRAME_FONT (s->f);
4165 }
4166
4167 /* Adjust base line for subscript/superscript text. */
4168 s->ybase += voffset;
66ac4b0e 4169
06a2c219
GM
4170 xassert (s->face && s->face->gc);
4171 return glyph - s->row->glyphs[s->area];
07e34cb0 4172}
dc6f92b8 4173
06a2c219
GM
4174
4175/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4176
dfcf069d 4177static void
06a2c219
GM
4178x_fill_image_glyph_string (s)
4179 struct glyph_string *s;
4180{
4181 xassert (s->first_glyph->type == IMAGE_GLYPH);
4182 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img.id);
4183 xassert (s->img);
4184 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.img.face_id);
4185 s->font = s->face->font;
4186 s->width = s->first_glyph->pixel_width;
4187
4188 /* Adjust base line for subscript/superscript text. */
4189 s->ybase += s->first_glyph->voffset;
4190}
4191
4192
4193/* Fill glyph string S from stretch glyph S->first_glyph. */
4194
4195static void
4196x_fill_stretch_glyph_string (s)
4197 struct glyph_string *s;
4198{
4199 xassert (s->first_glyph->type == STRETCH_GLYPH);
4200 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.stretch.face_id);
4201 s->font = s->face->font;
4202 s->width = s->first_glyph->pixel_width;
4203
4204 /* Adjust base line for subscript/superscript text. */
4205 s->ybase += s->first_glyph->voffset;
4206}
4207
4208
4209/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4210 of XChar2b structures for S; it can't be allocated in
4211 x_init_glyph_string because it must be allocated via `alloca'. W
4212 is the window on which S is drawn. ROW and AREA are the glyph row
4213 and area within the row from which S is constructed. START is the
4214 index of the first glyph structure covered by S. HL is a
4215 face-override for drawing S. */
4216
4217static void
4218x_init_glyph_string (s, char2b, w, row, area, start, hl)
4219 struct glyph_string *s;
4220 XChar2b *char2b;
4221 struct window *w;
4222 struct glyph_row *row;
4223 enum glyph_row_area area;
4224 int start;
4225 enum draw_glyphs_face hl;
4226{
4227 bzero (s, sizeof *s);
4228 s->w = w;
4229 s->f = XFRAME (w->frame);
4230 s->display = FRAME_X_DISPLAY (s->f);
4231 s->window = FRAME_X_WINDOW (s->f);
4232 s->char2b = char2b;
4233 s->hl = hl;
4234 s->row = row;
4235 s->area = area;
4236 s->first_glyph = row->glyphs[area] + start;
4237 s->height = row->height;
4238 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4239
9ea173e8
GM
4240 /* Display the internal border below the tool-bar window. */
4241 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4242 s->y -= s->f->output_data.x->internal_border_width;
4243
4244 s->ybase = s->y + row->ascent;
4245}
4246
4247
4248/* Set background width of glyph string S. START is the index of the
4249 first glyph following S. LAST_X is the right-most x-position + 1
4250 in the drawing area. */
4251
4252static INLINE void
4253x_set_glyph_string_background_width (s, start, last_x)
4254 struct glyph_string *s;
4255 int start;
4256 int last_x;
4257{
4258 /* If the face of this glyph string has to be drawn to the end of
4259 the drawing area, set S->extends_to_end_of_line_p. */
4260 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4261
4262 if (start == s->row->used[s->area]
4263 && s->hl == DRAW_NORMAL_TEXT
4264 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4265 || s->face->background != default_face->background
4266 || s->face->stipple != default_face->stipple))
4267 s->extends_to_end_of_line_p = 1;
4268
4269 /* If S extends its face to the end of the line, set its
4270 background_width to the distance to the right edge of the drawing
4271 area. */
4272 if (s->extends_to_end_of_line_p)
1da3fd71 4273 s->background_width = last_x - s->x + 1;
06a2c219
GM
4274 else
4275 s->background_width = s->width;
4276}
4277
4278
4279/* Add a glyph string for a stretch glyph to the list of strings
4280 between HEAD and TAIL. START is the index of the stretch glyph in
4281 row area AREA of glyph row ROW. END is the index of the last glyph
4282 in that glyph row area. X is the current output position assigned
4283 to the new glyph string constructed. HL overrides that face of the
4284 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4285 is the right-most x-position of the drawing area. */
4286
8abee2e1
DL
4287/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4288 and below -- keep them on one line. */
4289#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4290 do \
4291 { \
4292 s = (struct glyph_string *) alloca (sizeof *s); \
4293 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4294 x_fill_stretch_glyph_string (s); \
4295 x_append_glyph_string (&HEAD, &TAIL, s); \
4296 ++START; \
4297 s->x = (X); \
4298 } \
4299 while (0)
4300
4301
4302/* Add a glyph string for an image glyph to the list of strings
4303 between HEAD and TAIL. START is the index of the image glyph in
4304 row area AREA of glyph row ROW. END is the index of the last glyph
4305 in that glyph row area. X is the current output position assigned
4306 to the new glyph string constructed. HL overrides that face of the
4307 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4308 is the right-most x-position of the drawing area. */
4309
8abee2e1 4310#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4311 do \
4312 { \
4313 s = (struct glyph_string *) alloca (sizeof *s); \
4314 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4315 x_fill_image_glyph_string (s); \
4316 x_append_glyph_string (&HEAD, &TAIL, s); \
4317 ++START; \
4318 s->x = (X); \
4319 } \
4320 while (0)
4321
4322
4323/* Add a glyph string for a sequence of character glyphs to the list
4324 of strings between HEAD and TAIL. START is the index of the first
4325 glyph in row area AREA of glyph row ROW that is part of the new
4326 glyph string. END is the index of the last glyph in that glyph row
4327 area. X is the current output position assigned to the new glyph
4328 string constructed. HL overrides that face of the glyph; e.g. it
4329 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4330 right-most x-position of the drawing area. */
4331
8abee2e1 4332#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4333 do \
4334 { \
4335 int c, charset, face_id; \
4336 XChar2b *char2b; \
4337 \
4338 c = (ROW)->glyphs[AREA][START].u.ch.code; \
4339 charset = CHAR_CHARSET (c); \
4340 face_id = (ROW)->glyphs[AREA][START].u.ch.face_id; \
4341 \
b4192550
KH
4342 s = (struct glyph_string *) alloca (sizeof *s); \
4343 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4344 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4345 x_append_glyph_string (&HEAD, &TAIL, s); \
4346 s->charset = charset; \
4347 s->x = (X); \
4348 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4349 OVERLAPS_P); \
06a2c219
GM
4350 } \
4351 while (0)
4352
4353
b4192550
KH
4354/* Add a glyph string for a composite sequence to the list of strings
4355 between HEAD and TAIL. START is the index of the first glyph in
4356 row area AREA of glyph row ROW that is part of the new glyph
4357 string. END is the index of the last glyph in that glyph row area.
4358 X is the current output position assigned to the new glyph string
4359 constructed. HL overrides that face of the glyph; e.g. it is
4360 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4361 x-position of the drawing area. */
4362
6c27ec25 4363#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550
KH
4364 do { \
4365 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp.id; \
4366 int face_id = (ROW)->glyphs[AREA][START].u.cmp.face_id; \
4367 struct composition *cmp = composition_table[cmp_id]; \
4368 int glyph_len = cmp->glyph_len; \
4369 XChar2b *char2b; \
4370 struct face **faces; \
4371 struct glyph_string *first_s = NULL; \
4372 int n; \
4373 \
4374 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4375 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4376 /* At first, fill in `char2b' and `faces'. */ \
4377 for (n = 0; n < glyph_len; n++) \
4378 { \
4379 int c = FAST_GLYPH_CHAR (COMPOSITION_GLYPH (cmp, n)); \
4380 faces[n] = x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4381 face_id, char2b + n, 1); \
4382 } \
4383 \
4384 /* Make glyph_strings for each glyph sequence that is drawable by \
4385 the same face, and append them to HEAD/TAIL. */ \
4386 for (n = 0; n < cmp->glyph_len;) \
4387 { \
4388 s = (struct glyph_string *) alloca (sizeof *s); \
4389 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4390 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4391 s->cmp = cmp; \
4392 s->gidx = n; \
4393 s->charset = 0; \
4394 s->x = (X); \
4395 \
4396 if (n == 0) \
4397 first_s = s; \
4398 \
4399 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4400 } \
4401 \
4402 ++START; \
4403 s = first_s; \
4404 } while (0)
4405
4406
06a2c219
GM
4407/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4408 of AREA of glyph row ROW on window W between indices START and END.
4409 HL overrides the face for drawing glyph strings, e.g. it is
4410 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4411 x-positions of the drawing area.
4412
4413 This is an ugly monster macro construct because we must use alloca
4414 to allocate glyph strings (because x_draw_glyphs can be called
4415 asynchronously). */
4416
8abee2e1 4417#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4418 do \
4419 { \
4420 HEAD = TAIL = NULL; \
4421 while (START < END) \
4422 { \
4423 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4424 switch (first_glyph->type) \
4425 { \
4426 case CHAR_GLYPH: \
4427 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4428 TAIL, HL, X, LAST_X, \
4429 OVERLAPS_P); \
06a2c219
GM
4430 break; \
4431 \
b4192550
KH
4432 case COMPOSITE_GLYPH: \
4433 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4434 HEAD, TAIL, HL, X, LAST_X,\
4435 OVERLAPS_P); \
4436 break; \
4437 \
06a2c219
GM
4438 case STRETCH_GLYPH: \
4439 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4440 HEAD, TAIL, HL, X, LAST_X); \
4441 break; \
4442 \
4443 case IMAGE_GLYPH: \
4444 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4445 TAIL, HL, X, LAST_X); \
4446 break; \
4447 \
4448 default: \
4449 abort (); \
4450 } \
4451 \
4452 x_set_glyph_string_background_width (s, START, LAST_X); \
4453 (X) += s->width; \
4454 } \
4455 } \
4456 while (0)
4457
4458
4459/* Draw glyphs between START and END in AREA of ROW on window W,
4460 starting at x-position X. X is relative to AREA in W. HL is a
4461 face-override with the following meaning:
4462
4463 DRAW_NORMAL_TEXT draw normally
4464 DRAW_CURSOR draw in cursor face
4465 DRAW_MOUSE_FACE draw in mouse face.
4466 DRAW_INVERSE_VIDEO draw in mode line face
4467 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4468 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4469
4470 If REAL_START is non-null, return in *REAL_START the real starting
4471 position for display. This can be different from START in case
4472 overlapping glyphs must be displayed. If REAL_END is non-null,
4473 return in *REAL_END the real end position for display. This can be
4474 different from END in case overlapping glyphs must be displayed.
4475
66ac4b0e
GM
4476 If OVERLAPS_P is non-zero, draw only the foreground of characters
4477 and clip to the physical height of ROW.
4478
06a2c219
GM
4479 Value is the x-position reached, relative to AREA of W. */
4480
4481static int
66ac4b0e
GM
4482x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4483 overlaps_p)
06a2c219
GM
4484 struct window *w;
4485 int x;
4486 struct glyph_row *row;
4487 enum glyph_row_area area;
4488 int start, end;
4489 enum draw_glyphs_face hl;
4490 int *real_start, *real_end;
66ac4b0e 4491 int overlaps_p;
dc6f92b8 4492{
06a2c219
GM
4493 struct glyph_string *head, *tail;
4494 struct glyph_string *s;
4495 int last_x, area_width;
4496 int x_reached;
4497 int i, j;
4498
4499 /* Let's rather be paranoid than getting a SEGV. */
4500 start = max (0, start);
4501 end = min (end, row->used[area]);
4502 if (real_start)
4503 *real_start = start;
4504 if (real_end)
4505 *real_end = end;
4506
4507 /* Translate X to frame coordinates. Set last_x to the right
4508 end of the drawing area. */
4509 if (row->full_width_p)
4510 {
4511 /* X is relative to the left edge of W, without scroll bars
4512 or flag areas. */
4513 struct frame *f = XFRAME (w->frame);
110859fc 4514 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4515 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4516
06a2c219
GM
4517 x += window_left_x;
4518 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4519 last_x = window_left_x + area_width;
4520
4521 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4522 {
110859fc 4523 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4524 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4525 last_x += width;
4526 else
4527 x -= width;
4528 }
dc6f92b8 4529
b9432a85
GM
4530 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4531 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4532 }
4533 else
dc6f92b8 4534 {
06a2c219
GM
4535 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4536 area_width = window_box_width (w, area);
4537 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4538 }
4539
06a2c219
GM
4540 /* Build a doubly-linked list of glyph_string structures between
4541 head and tail from what we have to draw. Note that the macro
4542 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4543 the reason we use a separate variable `i'. */
4544 i = start;
66ac4b0e
GM
4545 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4546 overlaps_p);
06a2c219
GM
4547 if (tail)
4548 x_reached = tail->x + tail->background_width;
4549 else
4550 x_reached = x;
90e65f07 4551
06a2c219
GM
4552 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4553 the row, redraw some glyphs in front or following the glyph
4554 strings built above. */
66ac4b0e 4555 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4556 {
4557 int dummy_x = 0;
4558 struct glyph_string *h, *t;
4559
4560 /* Compute overhangs for all glyph strings. */
4561 for (s = head; s; s = s->next)
4562 x_compute_glyph_string_overhangs (s);
4563
4564 /* Prepend glyph strings for glyphs in front of the first glyph
4565 string that are overwritten because of the first glyph
4566 string's left overhang. The background of all strings
4567 prepended must be drawn because the first glyph string
4568 draws over it. */
4569 i = x_left_overwritten (head);
4570 if (i >= 0)
4571 {
4572 j = i;
4573 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4574 DRAW_NORMAL_TEXT, dummy_x, last_x,
4575 overlaps_p);
06a2c219
GM
4576 start = i;
4577 if (real_start)
4578 *real_start = start;
4579 x_compute_overhangs_and_x (t, head->x, 1);
4580 x_prepend_glyph_string_lists (&head, &tail, h, t);
4581 }
58769bee 4582
06a2c219
GM
4583 /* Prepend glyph strings for glyphs in front of the first glyph
4584 string that overwrite that glyph string because of their
4585 right overhang. For these strings, only the foreground must
4586 be drawn, because it draws over the glyph string at `head'.
4587 The background must not be drawn because this would overwrite
4588 right overhangs of preceding glyphs for which no glyph
4589 strings exist. */
4590 i = x_left_overwriting (head);
4591 if (i >= 0)
4592 {
4593 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4594 DRAW_NORMAL_TEXT, dummy_x, last_x,
4595 overlaps_p);
06a2c219
GM
4596 for (s = h; s; s = s->next)
4597 s->background_filled_p = 1;
4598 if (real_start)
4599 *real_start = i;
4600 x_compute_overhangs_and_x (t, head->x, 1);
4601 x_prepend_glyph_string_lists (&head, &tail, h, t);
4602 }
dbcb258a 4603
06a2c219
GM
4604 /* Append glyphs strings for glyphs following the last glyph
4605 string tail that are overwritten by tail. The background of
4606 these strings has to be drawn because tail's foreground draws
4607 over it. */
4608 i = x_right_overwritten (tail);
4609 if (i >= 0)
4610 {
4611 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4612 DRAW_NORMAL_TEXT, x, last_x,
4613 overlaps_p);
06a2c219
GM
4614 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4615 x_append_glyph_string_lists (&head, &tail, h, t);
4616 if (real_end)
4617 *real_end = i;
4618 }
dc6f92b8 4619
06a2c219
GM
4620 /* Append glyph strings for glyphs following the last glyph
4621 string tail that overwrite tail. The foreground of such
4622 glyphs has to be drawn because it writes into the background
4623 of tail. The background must not be drawn because it could
4624 paint over the foreground of following glyphs. */
4625 i = x_right_overwriting (tail);
4626 if (i >= 0)
4627 {
4628 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4629 DRAW_NORMAL_TEXT, x, last_x,
4630 overlaps_p);
06a2c219
GM
4631 for (s = h; s; s = s->next)
4632 s->background_filled_p = 1;
4633 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4634 x_append_glyph_string_lists (&head, &tail, h, t);
4635 if (real_end)
4636 *real_end = i;
4637 }
4638 }
58769bee 4639
06a2c219
GM
4640 /* Draw all strings. */
4641 for (s = head; s; s = s->next)
4642 x_draw_glyph_string (s);
dc6f92b8 4643
06a2c219
GM
4644 /* Value is the x-position up to which drawn, relative to AREA of W.
4645 This doesn't include parts drawn because of overhangs. */
4646 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4647 if (!row->full_width_p)
4648 {
4649 if (area > LEFT_MARGIN_AREA)
4650 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4651 if (area > TEXT_AREA)
4652 x_reached -= window_box_width (w, TEXT_AREA);
4653 }
4654 return x_reached;
4655}
dc6f92b8 4656
dc6f92b8 4657
66ac4b0e
GM
4658/* Fix the display of area AREA of overlapping row ROW in window W. */
4659
4660static void
4661x_fix_overlapping_area (w, row, area)
4662 struct window *w;
4663 struct glyph_row *row;
4664 enum glyph_row_area area;
4665{
4666 int i, x;
4667
4668 BLOCK_INPUT;
4669
4670 if (area == LEFT_MARGIN_AREA)
4671 x = 0;
4672 else if (area == TEXT_AREA)
4673 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4674 else
4675 x = (window_box_width (w, LEFT_MARGIN_AREA)
4676 + window_box_width (w, TEXT_AREA));
4677
4678 for (i = 0; i < row->used[area];)
4679 {
4680 if (row->glyphs[area][i].overlaps_vertically_p)
4681 {
4682 int start = i, start_x = x;
4683
4684 do
4685 {
4686 x += row->glyphs[area][i].pixel_width;
4687 ++i;
4688 }
4689 while (i < row->used[area]
4690 && row->glyphs[area][i].overlaps_vertically_p);
4691
4692 x_draw_glyphs (w, start_x, row, area, start, i,
4693 (row->inverse_p
4694 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4695 NULL, NULL, 1);
4696 }
4697 else
4698 {
4699 x += row->glyphs[area][i].pixel_width;
4700 ++i;
4701 }
4702 }
4703
4704 UNBLOCK_INPUT;
4705}
4706
4707
06a2c219
GM
4708/* Output LEN glyphs starting at START at the nominal cursor position.
4709 Advance the nominal cursor over the text. The global variable
4710 updated_window contains the window being updated, updated_row is
4711 the glyph row being updated, and updated_area is the area of that
4712 row being updated. */
dc6f92b8 4713
06a2c219
GM
4714static void
4715x_write_glyphs (start, len)
4716 struct glyph *start;
4717 int len;
4718{
4719 int x, hpos, real_start, real_end;
d9cdbb3d 4720
06a2c219 4721 xassert (updated_window && updated_row);
dc6f92b8 4722 BLOCK_INPUT;
06a2c219
GM
4723
4724 /* Write glyphs. */
dc6f92b8 4725
06a2c219
GM
4726 hpos = start - updated_row->glyphs[updated_area];
4727 x = x_draw_glyphs (updated_window, output_cursor.x,
4728 updated_row, updated_area,
4729 hpos, hpos + len,
4730 (updated_row->inverse_p
4731 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4732 &real_start, &real_end, 0);
b30ec466 4733
06a2c219
GM
4734 /* If we drew over the cursor, note that it is not visible any more. */
4735 note_overwritten_text_cursor (updated_window, real_start,
4736 real_end - real_start);
dc6f92b8
JB
4737
4738 UNBLOCK_INPUT;
06a2c219
GM
4739
4740 /* Advance the output cursor. */
4741 output_cursor.hpos += len;
4742 output_cursor.x = x;
dc6f92b8
JB
4743}
4744
0cdd0c9f 4745
06a2c219 4746/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4747
06a2c219
GM
4748static void
4749x_insert_glyphs (start, len)
4750 struct glyph *start;
4751 register int len;
4752{
4753 struct frame *f;
4754 struct window *w;
4755 int line_height, shift_by_width, shifted_region_width;
4756 struct glyph_row *row;
4757 struct glyph *glyph;
4758 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4759
06a2c219 4760 xassert (updated_window && updated_row);
0cdd0c9f 4761 BLOCK_INPUT;
06a2c219
GM
4762 w = updated_window;
4763 f = XFRAME (WINDOW_FRAME (w));
4764
4765 /* Get the height of the line we are in. */
4766 row = updated_row;
4767 line_height = row->height;
4768
4769 /* Get the width of the glyphs to insert. */
4770 shift_by_width = 0;
4771 for (glyph = start; glyph < start + len; ++glyph)
4772 shift_by_width += glyph->pixel_width;
4773
4774 /* Get the width of the region to shift right. */
4775 shifted_region_width = (window_box_width (w, updated_area)
4776 - output_cursor.x
4777 - shift_by_width);
4778
4779 /* Shift right. */
4780 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4781 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4782 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4783 f->output_data.x->normal_gc,
4784 frame_x, frame_y,
4785 shifted_region_width, line_height,
4786 frame_x + shift_by_width, frame_y);
4787
4788 /* Write the glyphs. */
4789 hpos = start - row->glyphs[updated_area];
4790 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4791 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4792 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4793
4794 /* Advance the output cursor. */
4795 output_cursor.hpos += len;
4796 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4797 UNBLOCK_INPUT;
4798}
0cdd0c9f 4799
0cdd0c9f 4800
06a2c219
GM
4801/* Delete N glyphs at the nominal cursor position. Not implemented
4802 for X frames. */
c83febd7
RS
4803
4804static void
06a2c219
GM
4805x_delete_glyphs (n)
4806 register int n;
c83febd7 4807{
06a2c219 4808 abort ();
c83febd7
RS
4809}
4810
0cdd0c9f 4811
06a2c219
GM
4812/* Erase the current text line from the nominal cursor position
4813 (inclusive) to pixel column TO_X (exclusive). The idea is that
4814 everything from TO_X onward is already erased.
4815
4816 TO_X is a pixel position relative to updated_area of
4817 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4818
06a2c219
GM
4819static void
4820x_clear_end_of_line (to_x)
4821 int to_x;
4822{
4823 struct frame *f;
4824 struct window *w = updated_window;
4825 int max_x, min_y, max_y;
4826 int from_x, from_y, to_y;
4827
4828 xassert (updated_window && updated_row);
4829 f = XFRAME (w->frame);
4830
4831 if (updated_row->full_width_p)
4832 {
4833 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4834 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4835 && !w->pseudo_window_p)
4836 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4837 }
06a2c219
GM
4838 else
4839 max_x = window_box_width (w, updated_area);
4840 max_y = window_text_bottom_y (w);
dc6f92b8 4841
06a2c219
GM
4842 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4843 of window. For TO_X > 0, truncate to end of drawing area. */
4844 if (to_x == 0)
4845 return;
4846 else if (to_x < 0)
4847 to_x = max_x;
4848 else
4849 to_x = min (to_x, max_x);
dbc4e1c1 4850
06a2c219
GM
4851 to_y = min (max_y, output_cursor.y + updated_row->height);
4852
4853 /* Notice if the cursor will be cleared by this operation. */
4854 if (!updated_row->full_width_p)
4855 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4856
06a2c219
GM
4857 from_x = output_cursor.x;
4858
4859 /* Translate to frame coordinates. */
4860 if (updated_row->full_width_p)
4861 {
4862 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4863 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4864 }
0cdd0c9f
RS
4865 else
4866 {
06a2c219
GM
4867 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4868 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4869 }
4870
045dee35 4871 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4872 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4873 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4874
4875 /* Prevent inadvertently clearing to end of the X window. */
4876 if (to_x > from_x && to_y > from_y)
4877 {
4878 BLOCK_INPUT;
4879 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4880 from_x, from_y, to_x - from_x, to_y - from_y,
4881 False);
4882 UNBLOCK_INPUT;
0cdd0c9f 4883 }
0cdd0c9f 4884}
dbc4e1c1 4885
0cdd0c9f 4886
06a2c219 4887/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4888 frame. Otherwise clear the selected frame. */
06a2c219
GM
4889
4890static void
4891x_clear_frame ()
0cdd0c9f 4892{
06a2c219 4893 struct frame *f;
0cdd0c9f 4894
06a2c219
GM
4895 if (updating_frame)
4896 f = updating_frame;
0cdd0c9f 4897 else
b86bd3dd 4898 f = SELECTED_FRAME ();
58769bee 4899
06a2c219
GM
4900 /* Clearing the frame will erase any cursor, so mark them all as no
4901 longer visible. */
4902 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4903 output_cursor.hpos = output_cursor.vpos = 0;
4904 output_cursor.x = -1;
4905
4906 /* We don't set the output cursor here because there will always
4907 follow an explicit cursor_to. */
4908 BLOCK_INPUT;
4909 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4910
4911 /* We have to clear the scroll bars, too. If we have changed
4912 colors or something like that, then they should be notified. */
4913 x_scroll_bar_clear (f);
0cdd0c9f 4914
06a2c219
GM
4915 XFlush (FRAME_X_DISPLAY (f));
4916 UNBLOCK_INPUT;
dc6f92b8 4917}
06a2c219
GM
4918
4919
dc6f92b8 4920\f
dbc4e1c1
JB
4921/* Invert the middle quarter of the frame for .15 sec. */
4922
06a2c219
GM
4923/* We use the select system call to do the waiting, so we have to make
4924 sure it's available. If it isn't, we just won't do visual bells. */
4925
dbc4e1c1
JB
4926#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4927
06a2c219
GM
4928
4929/* Subtract the `struct timeval' values X and Y, storing the result in
4930 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4931
4932static int
4933timeval_subtract (result, x, y)
4934 struct timeval *result, x, y;
4935{
06a2c219
GM
4936 /* Perform the carry for the later subtraction by updating y. This
4937 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4938 if (x.tv_usec < y.tv_usec)
4939 {
4940 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4941 y.tv_usec -= 1000000 * nsec;
4942 y.tv_sec += nsec;
4943 }
06a2c219 4944
dbc4e1c1
JB
4945 if (x.tv_usec - y.tv_usec > 1000000)
4946 {
4947 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4948 y.tv_usec += 1000000 * nsec;
4949 y.tv_sec -= nsec;
4950 }
4951
06a2c219
GM
4952 /* Compute the time remaining to wait. tv_usec is certainly
4953 positive. */
dbc4e1c1
JB
4954 result->tv_sec = x.tv_sec - y.tv_sec;
4955 result->tv_usec = x.tv_usec - y.tv_usec;
4956
06a2c219
GM
4957 /* Return indication of whether the result should be considered
4958 negative. */
dbc4e1c1
JB
4959 return x.tv_sec < y.tv_sec;
4960}
dc6f92b8 4961
dfcf069d 4962void
f676886a
JB
4963XTflash (f)
4964 struct frame *f;
dc6f92b8 4965{
dbc4e1c1 4966 BLOCK_INPUT;
dc6f92b8 4967
dbc4e1c1
JB
4968 {
4969 GC gc;
dc6f92b8 4970
06a2c219
GM
4971 /* Create a GC that will use the GXxor function to flip foreground
4972 pixels into background pixels. */
dbc4e1c1
JB
4973 {
4974 XGCValues values;
dc6f92b8 4975
dbc4e1c1 4976 values.function = GXxor;
7556890b
RS
4977 values.foreground = (f->output_data.x->foreground_pixel
4978 ^ f->output_data.x->background_pixel);
58769bee 4979
334208b7 4980 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
4981 GCFunction | GCForeground, &values);
4982 }
dc6f92b8 4983
dbc4e1c1 4984 {
e84e14c3
RS
4985 /* Get the height not including a menu bar widget. */
4986 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4987 /* Height of each line to flash. */
4988 int flash_height = FRAME_LINE_HEIGHT (f);
4989 /* These will be the left and right margins of the rectangles. */
4990 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4991 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4992
4993 int width;
4994
4995 /* Don't flash the area between a scroll bar and the frame
4996 edge it is next to. */
4997 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4998 {
4999 case vertical_scroll_bar_left:
5000 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5001 break;
5002
5003 case vertical_scroll_bar_right:
5004 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5005 break;
06a2c219
GM
5006
5007 default:
5008 break;
e84e14c3
RS
5009 }
5010
5011 width = flash_right - flash_left;
5012
5013 /* If window is tall, flash top and bottom line. */
5014 if (height > 3 * FRAME_LINE_HEIGHT (f))
5015 {
5016 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5017 flash_left,
5018 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5019 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5020 width, flash_height);
5021 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5022 flash_left,
5023 (height - flash_height
5024 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5025 width, flash_height);
5026 }
5027 else
5028 /* If it is short, flash it all. */
5029 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5030 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5031 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5032
06a2c219 5033 x_flush (f);
dc6f92b8 5034
dbc4e1c1 5035 {
06a2c219 5036 struct timeval wakeup;
dc6f92b8 5037
66c30ea1 5038 EMACS_GET_TIME (wakeup);
dc6f92b8 5039
dbc4e1c1
JB
5040 /* Compute time to wait until, propagating carry from usecs. */
5041 wakeup.tv_usec += 150000;
5042 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5043 wakeup.tv_usec %= 1000000;
5044
5045 /* Keep waiting until past the time wakeup. */
5046 while (1)
5047 {
5048 struct timeval timeout;
5049
66c30ea1 5050 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5051
5052 /* In effect, timeout = wakeup - timeout.
5053 Break if result would be negative. */
5054 if (timeval_subtract (&timeout, wakeup, timeout))
5055 break;
5056
5057 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5058 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5059 }
5060 }
58769bee 5061
e84e14c3
RS
5062 /* If window is tall, flash top and bottom line. */
5063 if (height > 3 * FRAME_LINE_HEIGHT (f))
5064 {
5065 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5066 flash_left,
5067 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5068 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5069 width, flash_height);
5070 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5071 flash_left,
5072 (height - flash_height
5073 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5074 width, flash_height);
5075 }
5076 else
5077 /* If it is short, flash it all. */
5078 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5079 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5080 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5081
334208b7 5082 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5083 x_flush (f);
dc6f92b8 5084 }
dbc4e1c1
JB
5085 }
5086
5087 UNBLOCK_INPUT;
dc6f92b8
JB
5088}
5089
06a2c219 5090#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5091
5092
dc6f92b8
JB
5093/* Make audible bell. */
5094
dfcf069d 5095void
dc6f92b8
JB
5096XTring_bell ()
5097{
b86bd3dd
GM
5098 struct frame *f = SELECTED_FRAME ();
5099
5100 if (FRAME_X_DISPLAY (f))
5101 {
dbc4e1c1 5102#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5103 if (visible_bell)
5104 XTflash (f);
5105 else
dbc4e1c1 5106#endif
b86bd3dd
GM
5107 {
5108 BLOCK_INPUT;
5109 XBell (FRAME_X_DISPLAY (f), 0);
5110 XFlush (FRAME_X_DISPLAY (f));
5111 UNBLOCK_INPUT;
5112 }
dc6f92b8
JB
5113 }
5114}
06a2c219 5115
dc6f92b8 5116\f
06a2c219
GM
5117/* Specify how many text lines, from the top of the window,
5118 should be affected by insert-lines and delete-lines operations.
5119 This, and those operations, are used only within an update
5120 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5121
dfcf069d 5122static void
06a2c219
GM
5123XTset_terminal_window (n)
5124 register int n;
dc6f92b8 5125{
06a2c219 5126 /* This function intentionally left blank. */
dc6f92b8
JB
5127}
5128
06a2c219
GM
5129
5130\f
5131/***********************************************************************
5132 Line Dance
5133 ***********************************************************************/
5134
5135/* Perform an insert-lines or delete-lines operation, inserting N
5136 lines or deleting -N lines at vertical position VPOS. */
5137
dfcf069d 5138static void
06a2c219
GM
5139x_ins_del_lines (vpos, n)
5140 int vpos, n;
dc6f92b8
JB
5141{
5142 abort ();
5143}
06a2c219
GM
5144
5145
5146/* Scroll part of the display as described by RUN. */
dc6f92b8 5147
dfcf069d 5148static void
06a2c219
GM
5149x_scroll_run (w, run)
5150 struct window *w;
5151 struct run *run;
dc6f92b8 5152{
06a2c219
GM
5153 struct frame *f = XFRAME (w->frame);
5154 int x, y, width, height, from_y, to_y, bottom_y;
5155
5156 /* Get frame-relative bounding box of the text display area of W,
5157 without mode lines. Include in this box the flags areas to the
5158 left and right of W. */
5159 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5160 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5161 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5162
5163 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5164 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5165 bottom_y = y + height;
dc6f92b8 5166
06a2c219
GM
5167 if (to_y < from_y)
5168 {
5169 /* Scrolling up. Make sure we don't copy part of the mode
5170 line at the bottom. */
5171 if (from_y + run->height > bottom_y)
5172 height = bottom_y - from_y;
5173 else
5174 height = run->height;
5175 }
dc6f92b8 5176 else
06a2c219
GM
5177 {
5178 /* Scolling down. Make sure we don't copy over the mode line.
5179 at the bottom. */
5180 if (to_y + run->height > bottom_y)
5181 height = bottom_y - to_y;
5182 else
5183 height = run->height;
5184 }
7a13e894 5185
06a2c219
GM
5186 BLOCK_INPUT;
5187
5188 /* Cursor off. Will be switched on again in x_update_window_end. */
5189 updated_window = w;
5190 x_clear_cursor (w);
5191
5192 XCopyArea (FRAME_X_DISPLAY (f),
5193 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5194 f->output_data.x->normal_gc,
5195 x, from_y,
5196 width, height,
5197 x, to_y);
5198
5199 UNBLOCK_INPUT;
5200}
dc6f92b8 5201
dc6f92b8 5202
06a2c219
GM
5203\f
5204/***********************************************************************
5205 Exposure Events
5206 ***********************************************************************/
5207
5208/* Redisplay an exposed area of frame F. X and Y are the upper-left
5209 corner of the exposed rectangle. W and H are width and height of
5210 the exposed area. All are pixel values. W or H zero means redraw
5211 the entire frame. */
dc6f92b8 5212
06a2c219
GM
5213static void
5214expose_frame (f, x, y, w, h)
5215 struct frame *f;
5216 int x, y, w, h;
dc6f92b8 5217{
06a2c219 5218 XRectangle r;
dc6f92b8 5219
06a2c219 5220 TRACE ((stderr, "expose_frame "));
dc6f92b8 5221
06a2c219
GM
5222 /* No need to redraw if frame will be redrawn soon. */
5223 if (FRAME_GARBAGED_P (f))
dc6f92b8 5224 {
06a2c219
GM
5225 TRACE ((stderr, " garbaged\n"));
5226 return;
5227 }
5228
5229 /* If basic faces haven't been realized yet, there is no point in
5230 trying to redraw anything. This can happen when we get an expose
5231 event while Emacs is starting, e.g. by moving another window. */
5232 if (FRAME_FACE_CACHE (f) == NULL
5233 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5234 {
5235 TRACE ((stderr, " no faces\n"));
5236 return;
58769bee 5237 }
06a2c219
GM
5238
5239 if (w == 0 || h == 0)
58769bee 5240 {
06a2c219
GM
5241 r.x = r.y = 0;
5242 r.width = CANON_X_UNIT (f) * f->width;
5243 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5244 }
5245 else
5246 {
06a2c219
GM
5247 r.x = x;
5248 r.y = y;
5249 r.width = w;
5250 r.height = h;
5251 }
5252
5253 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5254 expose_window_tree (XWINDOW (f->root_window), &r);
5255
9ea173e8 5256 if (WINDOWP (f->tool_bar_window))
06a2c219 5257 {
9ea173e8 5258 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5259 XRectangle window_rect;
5260 XRectangle intersection_rect;
5261 int window_x, window_y, window_width, window_height;
5262
5263
5264 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5265 window_rect.x = window_x;
5266 window_rect.y = window_y;
5267 window_rect.width = window_width;
5268 window_rect.height = window_height;
5269
5270 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5271 expose_window (w, &intersection_rect);
5272 }
5273
5274#ifndef USE_X_TOOLKIT
5275 if (WINDOWP (f->menu_bar_window))
5276 {
5277 struct window *w = XWINDOW (f->menu_bar_window);
5278 XRectangle window_rect;
5279 XRectangle intersection_rect;
5280 int window_x, window_y, window_width, window_height;
5281
5282
5283 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5284 window_rect.x = window_x;
5285 window_rect.y = window_y;
5286 window_rect.width = window_width;
5287 window_rect.height = window_height;
5288
5289 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5290 expose_window (w, &intersection_rect);
dc6f92b8 5291 }
06a2c219 5292#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5293}
5294
06a2c219
GM
5295
5296/* Redraw (parts) of all windows in the window tree rooted at W that
5297 intersect R. R contains frame pixel coordinates. */
5298
58769bee 5299static void
06a2c219
GM
5300expose_window_tree (w, r)
5301 struct window *w;
5302 XRectangle *r;
dc6f92b8 5303{
06a2c219
GM
5304 while (w)
5305 {
5306 if (!NILP (w->hchild))
5307 expose_window_tree (XWINDOW (w->hchild), r);
5308 else if (!NILP (w->vchild))
5309 expose_window_tree (XWINDOW (w->vchild), r);
5310 else
5311 {
5312 XRectangle window_rect;
5313 XRectangle intersection_rect;
5314 struct frame *f = XFRAME (w->frame);
5315 int window_x, window_y, window_width, window_height;
5316
5317 /* Frame-relative pixel rectangle of W. */
5318 window_box (w, -1, &window_x, &window_y, &window_width,
5319 &window_height);
5320 window_rect.x
5321 = (window_x
110859fc 5322 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5323 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5324 window_rect.y = window_y;
5325 window_rect.width
5326 = (window_width
110859fc 5327 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5328 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5329 window_rect.height
5330 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5331
5332 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5333 expose_window (w, &intersection_rect);
5334 }
58769bee 5335
06a2c219
GM
5336 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5337 }
5338}
58769bee 5339
dc6f92b8 5340
06a2c219
GM
5341/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5342 which intersects rectangle R. R is in window-relative coordinates. */
5343
5344static void
5345expose_area (w, row, r, area)
5346 struct window *w;
5347 struct glyph_row *row;
5348 XRectangle *r;
5349 enum glyph_row_area area;
5350{
5351 int x;
5352 struct glyph *first = row->glyphs[area];
5353 struct glyph *end = row->glyphs[area] + row->used[area];
5354 struct glyph *last;
5355 int first_x;
5356
5357 /* Set x to the window-relative start position for drawing glyphs of
5358 AREA. The first glyph of the text area can be partially visible.
5359 The first glyphs of other areas cannot. */
5360 if (area == LEFT_MARGIN_AREA)
5361 x = 0;
5362 else if (area == TEXT_AREA)
5363 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5364 else
5365 x = (window_box_width (w, LEFT_MARGIN_AREA)
5366 + window_box_width (w, TEXT_AREA));
5367
6fb13182
GM
5368 if (area == TEXT_AREA && row->fill_line_p)
5369 /* If row extends face to end of line write the whole line. */
5370 x_draw_glyphs (w, x, row, area,
5371 0, row->used[area],
06a2c219 5372 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5373 NULL, NULL, 0);
6fb13182
GM
5374 else
5375 {
5376 /* Find the first glyph that must be redrawn. */
5377 while (first < end
5378 && x + first->pixel_width < r->x)
5379 {
5380 x += first->pixel_width;
5381 ++first;
5382 }
5383
5384 /* Find the last one. */
5385 last = first;
5386 first_x = x;
5387 while (last < end
5388 && x < r->x + r->width)
5389 {
5390 x += last->pixel_width;
5391 ++last;
5392 }
5393
5394 /* Repaint. */
5395 if (last > first)
5396 x_draw_glyphs (w, first_x, row, area,
5397 first - row->glyphs[area],
5398 last - row->glyphs[area],
5399 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5400 NULL, NULL, 0);
5401 }
06a2c219
GM
5402}
5403
58769bee 5404
06a2c219
GM
5405/* Redraw the parts of the glyph row ROW on window W intersecting
5406 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5407
06a2c219
GM
5408static void
5409expose_line (w, row, r)
5410 struct window *w;
5411 struct glyph_row *row;
5412 XRectangle *r;
5413{
5414 xassert (row->enabled_p);
5415
5416 if (row->mode_line_p || w->pseudo_window_p)
5417 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5418 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5419 NULL, NULL, 0);
06a2c219
GM
5420 else
5421 {
5422 if (row->used[LEFT_MARGIN_AREA])
5423 expose_area (w, row, r, LEFT_MARGIN_AREA);
5424 if (row->used[TEXT_AREA])
5425 expose_area (w, row, r, TEXT_AREA);
5426 if (row->used[RIGHT_MARGIN_AREA])
5427 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5428 x_draw_row_bitmaps (w, row);
5429 }
5430}
dc6f92b8 5431
58769bee 5432
06a2c219
GM
5433/* Return non-zero if W's cursor intersects rectangle R. */
5434
5435static int
5436x_phys_cursor_in_rect_p (w, r)
5437 struct window *w;
5438 XRectangle *r;
5439{
5440 XRectangle cr, result;
5441 struct glyph *cursor_glyph;
5442
5443 cursor_glyph = get_phys_cursor_glyph (w);
5444 if (cursor_glyph)
5445 {
5446 cr.x = w->phys_cursor.x;
5447 cr.y = w->phys_cursor.y;
5448 cr.width = cursor_glyph->pixel_width;
5449 cr.height = w->phys_cursor_height;
5450 return x_intersect_rectangles (&cr, r, &result);
5451 }
5452 else
5453 return 0;
dc6f92b8 5454}
dc6f92b8 5455
06a2c219
GM
5456
5457/* Redraw a rectangle of window W. R is a rectangle in window
5458 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5459
5460static void
06a2c219
GM
5461expose_window (w, r)
5462 struct window *w;
5463 XRectangle *r;
dc6f92b8 5464{
06a2c219
GM
5465 struct glyph_row *row;
5466 int y;
5467 int yb = window_text_bottom_y (w);
5468 int cursor_cleared_p;
dc6f92b8 5469
80c32bcc
GM
5470 /* If window is not yet fully initialized, do nothing. This can
5471 happen when toolkit scroll bars are used and a window is split.
5472 Reconfiguring the scroll bar will generate an expose for a newly
5473 created window. */
5474 if (w->current_matrix == NULL)
5475 return;
5476
06a2c219
GM
5477 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5478 r->x, r->y, r->width, r->height));
dc6f92b8 5479
06a2c219
GM
5480 /* Convert to window coordinates. */
5481 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5482 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5483
06a2c219
GM
5484 /* Turn off the cursor. */
5485 if (!w->pseudo_window_p
5486 && x_phys_cursor_in_rect_p (w, r))
5487 {
5488 x_clear_cursor (w);
5489 cursor_cleared_p = 1;
5490 }
5491 else
5492 cursor_cleared_p = 0;
5493
5494 /* Find the first row intersecting the rectangle R. */
5495 row = w->current_matrix->rows;
5496 y = 0;
5497 while (row->enabled_p
5498 && y < yb
5499 && y + row->height < r->y)
5500 {
5501 y += row->height;
5502 ++row;
5503 }
5504
dc6f92b8 5505 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5506 while (row->enabled_p
5507 && y < yb
5508 && y < r->y + r->height)
5509 {
5510 expose_line (w, row, r);
5511 y += row->height;
5512 ++row;
5513 }
5514
5515 /* Display the mode line if there is one. */
5516 if (WINDOW_WANTS_MODELINE_P (w)
5517 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5518 row->enabled_p)
5519 && row->y < r->y + r->height)
5520 expose_line (w, row, r);
dc6f92b8 5521
06a2c219 5522 if (!w->pseudo_window_p)
dc6f92b8 5523 {
06a2c219
GM
5524 /* Draw border between windows. */
5525 x_draw_vertical_border (w);
5526
5527 /* Turn the cursor on again. */
5528 if (cursor_cleared_p)
5529 x_update_window_cursor (w, 1);
5530 }
5531}
dc6f92b8 5532
dc6f92b8 5533
06a2c219
GM
5534/* Determine the intersection of two rectangles R1 and R2. Return
5535 the intersection in *RESULT. Value is non-zero if RESULT is not
5536 empty. */
5537
5538static int
5539x_intersect_rectangles (r1, r2, result)
5540 XRectangle *r1, *r2, *result;
5541{
5542 XRectangle *left, *right;
5543 XRectangle *upper, *lower;
5544 int intersection_p = 0;
5545
5546 /* Rearrange so that R1 is the left-most rectangle. */
5547 if (r1->x < r2->x)
5548 left = r1, right = r2;
5549 else
5550 left = r2, right = r1;
5551
5552 /* X0 of the intersection is right.x0, if this is inside R1,
5553 otherwise there is no intersection. */
5554 if (right->x <= left->x + left->width)
5555 {
5556 result->x = right->x;
5557
5558 /* The right end of the intersection is the minimum of the
5559 the right ends of left and right. */
5560 result->width = (min (left->x + left->width, right->x + right->width)
5561 - result->x);
5562
5563 /* Same game for Y. */
5564 if (r1->y < r2->y)
5565 upper = r1, lower = r2;
5566 else
5567 upper = r2, lower = r1;
5568
5569 /* The upper end of the intersection is lower.y0, if this is inside
5570 of upper. Otherwise, there is no intersection. */
5571 if (lower->y <= upper->y + upper->height)
dc43ef94 5572 {
06a2c219
GM
5573 result->y = lower->y;
5574
5575 /* The lower end of the intersection is the minimum of the lower
5576 ends of upper and lower. */
5577 result->height = (min (lower->y + lower->height,
5578 upper->y + upper->height)
5579 - result->y);
5580 intersection_p = 1;
dc43ef94 5581 }
dc6f92b8
JB
5582 }
5583
06a2c219 5584 return intersection_p;
dc6f92b8 5585}
06a2c219
GM
5586
5587
5588
5589
dc6f92b8 5590\f
dc6f92b8 5591static void
334208b7
RS
5592frame_highlight (f)
5593 struct frame *f;
dc6f92b8 5594{
b3e1e05c
JB
5595 /* We used to only do this if Vx_no_window_manager was non-nil, but
5596 the ICCCM (section 4.1.6) says that the window's border pixmap
5597 and border pixel are window attributes which are "private to the
5598 client", so we can always change it to whatever we want. */
5599 BLOCK_INPUT;
334208b7 5600 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5601 f->output_data.x->border_pixel);
b3e1e05c 5602 UNBLOCK_INPUT;
5d46f928 5603 x_update_cursor (f, 1);
dc6f92b8
JB
5604}
5605
5606static void
334208b7
RS
5607frame_unhighlight (f)
5608 struct frame *f;
dc6f92b8 5609{
b3e1e05c
JB
5610 /* We used to only do this if Vx_no_window_manager was non-nil, but
5611 the ICCCM (section 4.1.6) says that the window's border pixmap
5612 and border pixel are window attributes which are "private to the
5613 client", so we can always change it to whatever we want. */
5614 BLOCK_INPUT;
334208b7 5615 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5616 f->output_data.x->border_tile);
b3e1e05c 5617 UNBLOCK_INPUT;
5d46f928 5618 x_update_cursor (f, 1);
dc6f92b8 5619}
dc6f92b8 5620
f676886a
JB
5621/* The focus has changed. Update the frames as necessary to reflect
5622 the new situation. Note that we can't change the selected frame
c5acd733 5623 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5624 Each event gets marked with the frame in which it occurred, so the
c5acd733 5625 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5626
6d4238f3 5627static void
0f941935
KH
5628x_new_focus_frame (dpyinfo, frame)
5629 struct x_display_info *dpyinfo;
f676886a 5630 struct frame *frame;
dc6f92b8 5631{
0f941935 5632 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5633
0f941935 5634 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5635 {
58769bee 5636 /* Set this before calling other routines, so that they see
f676886a 5637 the correct value of x_focus_frame. */
0f941935 5638 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5639
5640 if (old_focus && old_focus->auto_lower)
f676886a 5641 x_lower_frame (old_focus);
dc6f92b8
JB
5642
5643#if 0
f676886a 5644 selected_frame = frame;
e0c1aef2
KH
5645 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5646 selected_frame);
f676886a
JB
5647 Fselect_window (selected_frame->selected_window);
5648 choose_minibuf_frame ();
c118dd06 5649#endif /* ! 0 */
dc6f92b8 5650
0f941935
KH
5651 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5652 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5653 else
5654 pending_autoraise_frame = 0;
6d4238f3 5655 }
dc6f92b8 5656
0f941935 5657 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5658}
5659
37c2c98b
RS
5660/* Handle an event saying the mouse has moved out of an Emacs frame. */
5661
5662void
0f941935
KH
5663x_mouse_leave (dpyinfo)
5664 struct x_display_info *dpyinfo;
37c2c98b 5665{
0f941935 5666 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5667}
6d4238f3 5668
f451eb13
JB
5669/* The focus has changed, or we have redirected a frame's focus to
5670 another frame (this happens when a frame uses a surrogate
06a2c219 5671 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5672
5673 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5674 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5675 the appropriate X display info. */
06a2c219 5676
6d4238f3 5677static void
0f941935
KH
5678XTframe_rehighlight (frame)
5679 struct frame *frame;
6d4238f3 5680{
0f941935
KH
5681 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5682}
6d4238f3 5683
0f941935
KH
5684static void
5685x_frame_rehighlight (dpyinfo)
5686 struct x_display_info *dpyinfo;
5687{
5688 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5689
5690 if (dpyinfo->x_focus_frame)
6d4238f3 5691 {
0f941935
KH
5692 dpyinfo->x_highlight_frame
5693 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5694 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5695 : dpyinfo->x_focus_frame);
5696 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5697 {
0f941935
KH
5698 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5699 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5700 }
dc6f92b8 5701 }
6d4238f3 5702 else
0f941935 5703 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5704
0f941935 5705 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5706 {
5707 if (old_highlight)
f676886a 5708 frame_unhighlight (old_highlight);
0f941935
KH
5709 if (dpyinfo->x_highlight_frame)
5710 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5711 }
dc6f92b8 5712}
06a2c219
GM
5713
5714
dc6f92b8 5715\f
06a2c219 5716/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5717
28430d3c
JB
5718/* Initialize mode_switch_bit and modifier_meaning. */
5719static void
334208b7
RS
5720x_find_modifier_meanings (dpyinfo)
5721 struct x_display_info *dpyinfo;
28430d3c 5722{
f689eb05 5723 int min_code, max_code;
28430d3c
JB
5724 KeySym *syms;
5725 int syms_per_code;
5726 XModifierKeymap *mods;
5727
334208b7
RS
5728 dpyinfo->meta_mod_mask = 0;
5729 dpyinfo->shift_lock_mask = 0;
5730 dpyinfo->alt_mod_mask = 0;
5731 dpyinfo->super_mod_mask = 0;
5732 dpyinfo->hyper_mod_mask = 0;
58769bee 5733
9658a521 5734#ifdef HAVE_X11R4
334208b7 5735 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5736#else
4a60f8c5
RS
5737 min_code = dpyinfo->display->min_keycode;
5738 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5739#endif
5740
334208b7 5741 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5742 min_code, max_code - min_code + 1,
5743 &syms_per_code);
334208b7 5744 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5745
58769bee 5746 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5747 Alt keysyms are on. */
28430d3c 5748 {
06a2c219 5749 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5750
5751 for (row = 3; row < 8; row++)
5752 for (col = 0; col < mods->max_keypermod; col++)
5753 {
0299d313
RS
5754 KeyCode code
5755 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5756
af92970c
KH
5757 /* Zeroes are used for filler. Skip them. */
5758 if (code == 0)
5759 continue;
5760
28430d3c
JB
5761 /* Are any of this keycode's keysyms a meta key? */
5762 {
5763 int code_col;
5764
5765 for (code_col = 0; code_col < syms_per_code; code_col++)
5766 {
f689eb05 5767 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5768
f689eb05 5769 switch (sym)
28430d3c 5770 {
f689eb05
JB
5771 case XK_Meta_L:
5772 case XK_Meta_R:
334208b7 5773 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5774 break;
f689eb05
JB
5775
5776 case XK_Alt_L:
5777 case XK_Alt_R:
334208b7 5778 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5779 break;
5780
5781 case XK_Hyper_L:
5782 case XK_Hyper_R:
334208b7 5783 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5784 break;
5785
5786 case XK_Super_L:
5787 case XK_Super_R:
334208b7 5788 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5789 break;
11edeb03
JB
5790
5791 case XK_Shift_Lock:
5792 /* Ignore this if it's not on the lock modifier. */
5793 if ((1 << row) == LockMask)
334208b7 5794 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5795 break;
28430d3c
JB
5796 }
5797 }
5798 }
5799 }
5800 }
5801
f689eb05 5802 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5803 if (! dpyinfo->meta_mod_mask)
a3c44b14 5804 {
334208b7
RS
5805 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5806 dpyinfo->alt_mod_mask = 0;
a3c44b14 5807 }
f689eb05 5808
148c4b70
RS
5809 /* If some keys are both alt and meta,
5810 make them just meta, not alt. */
334208b7 5811 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5812 {
334208b7 5813 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5814 }
58769bee 5815
28430d3c 5816 XFree ((char *) syms);
f689eb05 5817 XFreeModifiermap (mods);
28430d3c
JB
5818}
5819
dfeccd2d
JB
5820/* Convert between the modifier bits X uses and the modifier bits
5821 Emacs uses. */
06a2c219 5822
7c5283e4 5823static unsigned int
334208b7
RS
5824x_x_to_emacs_modifiers (dpyinfo, state)
5825 struct x_display_info *dpyinfo;
dc6f92b8
JB
5826 unsigned int state;
5827{
334208b7
RS
5828 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5829 | ((state & ControlMask) ? ctrl_modifier : 0)
5830 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5831 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5832 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5833 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5834}
5835
dfeccd2d 5836static unsigned int
334208b7
RS
5837x_emacs_to_x_modifiers (dpyinfo, state)
5838 struct x_display_info *dpyinfo;
dfeccd2d
JB
5839 unsigned int state;
5840{
334208b7
RS
5841 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5842 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5843 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5844 | ((state & shift_modifier) ? ShiftMask : 0)
5845 | ((state & ctrl_modifier) ? ControlMask : 0)
5846 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5847}
d047c4eb
KH
5848
5849/* Convert a keysym to its name. */
5850
5851char *
5852x_get_keysym_name (keysym)
5853 KeySym keysym;
5854{
5855 char *value;
5856
5857 BLOCK_INPUT;
5858 value = XKeysymToString (keysym);
5859 UNBLOCK_INPUT;
5860
5861 return value;
5862}
06a2c219
GM
5863
5864
e4571a43
JB
5865\f
5866/* Mouse clicks and mouse movement. Rah. */
e4571a43 5867
06a2c219
GM
5868/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5869 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5870 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5871 not force the value into range. */
69388238 5872
c8dba240 5873void
69388238 5874pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5875 FRAME_PTR f;
69388238 5876 register int pix_x, pix_y;
e4571a43
JB
5877 register int *x, *y;
5878 XRectangle *bounds;
69388238 5879 int noclip;
e4571a43 5880{
06a2c219 5881 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5882 even for negative values. */
5883 if (pix_x < 0)
7556890b 5884 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5885 if (pix_y < 0)
7556890b 5886 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5887
e4571a43
JB
5888 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5889 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5890
5891 if (bounds)
5892 {
7556890b
RS
5893 bounds->width = FONT_WIDTH (f->output_data.x->font);
5894 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5895 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5896 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5897 }
5898
69388238
RS
5899 if (!noclip)
5900 {
5901 if (pix_x < 0)
5902 pix_x = 0;
3cbd2e0b
RS
5903 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5904 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5905
5906 if (pix_y < 0)
5907 pix_y = 0;
5908 else if (pix_y > f->height)
5909 pix_y = f->height;
5910 }
e4571a43
JB
5911
5912 *x = pix_x;
5913 *y = pix_y;
5914}
5915
06a2c219
GM
5916
5917/* Given HPOS/VPOS in the current matrix of W, return corresponding
5918 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5919 can't tell the positions because W's display is not up to date,
5920 return 0. */
5921
5922int
5923glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5924 struct window *w;
5925 int hpos, vpos;
5926 int *frame_x, *frame_y;
2b5c9e71 5927{
06a2c219
GM
5928 int success_p;
5929
5930 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5931 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5932
5933 if (display_completed)
5934 {
5935 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5936 struct glyph *glyph = row->glyphs[TEXT_AREA];
5937 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5938
5939 *frame_y = row->y;
5940 *frame_x = row->x;
5941 while (glyph < end)
5942 {
5943 *frame_x += glyph->pixel_width;
5944 ++glyph;
5945 }
5946
5947 success_p = 1;
5948 }
5949 else
5950 {
5951 *frame_y = *frame_x = 0;
5952 success_p = 0;
5953 }
5954
5955 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5956 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5957 return success_p;
2b5c9e71
RS
5958}
5959
06a2c219 5960
dc6f92b8
JB
5961/* Prepare a mouse-event in *RESULT for placement in the input queue.
5962
5963 If the event is a button press, then note that we have grabbed
f451eb13 5964 the mouse. */
dc6f92b8
JB
5965
5966static Lisp_Object
f451eb13 5967construct_mouse_click (result, event, f)
dc6f92b8
JB
5968 struct input_event *result;
5969 XButtonEvent *event;
f676886a 5970 struct frame *f;
dc6f92b8 5971{
f451eb13 5972 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5973 otherwise. */
f451eb13 5974 result->kind = mouse_click;
69388238 5975 result->code = event->button - Button1;
1113d9db 5976 result->timestamp = event->time;
334208b7
RS
5977 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5978 event->state)
f689eb05 5979 | (event->type == ButtonRelease
58769bee 5980 ? up_modifier
f689eb05 5981 : down_modifier));
dc6f92b8 5982
06a2c219
GM
5983 XSETINT (result->x, event->x);
5984 XSETINT (result->y, event->y);
5985 XSETFRAME (result->frame_or_window, f);
5986 return Qnil;
dc6f92b8 5987}
b849c413 5988
06a2c219
GM
5989#if 0 /* This function isn't called. --gerd */
5990
b849c413
RS
5991/* Prepare a menu-event in *RESULT for placement in the input queue. */
5992
5993static Lisp_Object
5994construct_menu_click (result, event, f)
5995 struct input_event *result;
5996 XButtonEvent *event;
5997 struct frame *f;
5998{
5999 /* Make the event type no_event; we'll change that when we decide
6000 otherwise. */
6001 result->kind = mouse_click;
26459b28 6002 result->code = event->button - Button1;
b849c413 6003 result->timestamp = event->time;
334208b7
RS
6004 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6005 event->state)
b849c413 6006 | (event->type == ButtonRelease
58769bee 6007 ? up_modifier
b849c413
RS
6008 : down_modifier));
6009
e0c1aef2
KH
6010 XSETINT (result->x, event->x);
6011 XSETINT (result->y, -1);
6012 XSETFRAME (result->frame_or_window, f);
b849c413 6013}
06a2c219
GM
6014
6015#endif /* 0 */
6016
69388238 6017\f
90e65f07
JB
6018/* Function to report a mouse movement to the mainstream Emacs code.
6019 The input handler calls this.
6020
6021 We have received a mouse movement event, which is given in *event.
6022 If the mouse is over a different glyph than it was last time, tell
6023 the mainstream emacs code by setting mouse_moved. If not, ask for
6024 another motion event, so we can check again the next time it moves. */
b8009dd1 6025
06a2c219
GM
6026static XMotionEvent last_mouse_motion_event;
6027static Lisp_Object last_mouse_motion_frame;
6028
90e65f07 6029static void
12ba150f 6030note_mouse_movement (frame, event)
f676886a 6031 FRAME_PTR frame;
90e65f07 6032 XMotionEvent *event;
90e65f07 6033{
e5d77022 6034 last_mouse_movement_time = event->time;
06a2c219
GM
6035 last_mouse_motion_event = *event;
6036 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6037
27f338af
RS
6038 if (event->window != FRAME_X_WINDOW (frame))
6039 {
39d8bb4d 6040 frame->mouse_moved = 1;
27f338af 6041 last_mouse_scroll_bar = Qnil;
27f338af 6042 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6043 }
6044
90e65f07 6045 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6046 else if (event->x < last_mouse_glyph.x
6047 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6048 || event->y < last_mouse_glyph.y
6049 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6050 {
39d8bb4d 6051 frame->mouse_moved = 1;
ab648270 6052 last_mouse_scroll_bar = Qnil;
b8009dd1 6053 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6054 }
6055}
6056
bf1c0ba1 6057/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6058
06a2c219
GM
6059 int disable_mouse_highlight;
6060
6061
6062\f
6063/************************************************************************
6064 Mouse Face
6065 ************************************************************************/
6066
6067/* Find the glyph under window-relative coordinates X/Y in window W.
6068 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6069 strings. Return in *HPOS and *VPOS the row and column number of
6070 the glyph found. Return in *AREA the glyph area containing X.
6071 Value is a pointer to the glyph found or null if X/Y is not on
6072 text, or we can't tell because W's current matrix is not up to
6073 date. */
6074
6075static struct glyph *
6076x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6077 struct window *w;
6078 int x, y;
6079 int *hpos, *vpos, *area;
6080{
6081 struct glyph *glyph, *end;
6082 struct glyph_row *row;
6083 int x0, i, left_area_width;
6084
6085 /* Find row containing Y. Give up if some row is not enabled. */
6086 for (i = 0; i < w->current_matrix->nrows; ++i)
6087 {
6088 row = MATRIX_ROW (w->current_matrix, i);
6089 if (!row->enabled_p)
6090 return NULL;
6091 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6092 break;
6093 }
6094
6095 *vpos = i;
6096 *hpos = 0;
6097
6098 /* Give up if Y is not in the window. */
6099 if (i == w->current_matrix->nrows)
6100 return NULL;
6101
6102 /* Get the glyph area containing X. */
6103 if (w->pseudo_window_p)
6104 {
6105 *area = TEXT_AREA;
6106 x0 = 0;
6107 }
6108 else
6109 {
6110 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6111 if (x < left_area_width)
6112 {
6113 *area = LEFT_MARGIN_AREA;
6114 x0 = 0;
6115 }
6116 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6117 {
6118 *area = TEXT_AREA;
6119 x0 = row->x + left_area_width;
6120 }
6121 else
6122 {
6123 *area = RIGHT_MARGIN_AREA;
6124 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6125 }
6126 }
6127
6128 /* Find glyph containing X. */
6129 glyph = row->glyphs[*area];
6130 end = glyph + row->used[*area];
6131 while (glyph < end)
6132 {
6133 if (x < x0 + glyph->pixel_width)
6134 {
6135 if (w->pseudo_window_p)
6136 break;
6137 else if (BUFFERP (glyph->object))
6138 break;
6139 }
6140
6141 x0 += glyph->pixel_width;
6142 ++glyph;
6143 }
6144
6145 if (glyph == end)
6146 return NULL;
6147
6148 *hpos = glyph - row->glyphs[*area];
6149 return glyph;
6150}
6151
6152
6153/* Convert frame-relative x/y to coordinates relative to window W.
6154 Takes pseudo-windows into account. */
6155
6156static void
6157frame_to_window_pixel_xy (w, x, y)
6158 struct window *w;
6159 int *x, *y;
6160{
6161 if (w->pseudo_window_p)
6162 {
6163 /* A pseudo-window is always full-width, and starts at the
6164 left edge of the frame, plus a frame border. */
6165 struct frame *f = XFRAME (w->frame);
6166 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6167 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6168 }
6169 else
6170 {
6171 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6172 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6173 }
6174}
6175
6176
6177/* Take proper action when mouse has moved to the mode or top line of
6178 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6179 mode line. X is relative to the start of the text display area of
6180 W, so the width of bitmap areas and scroll bars must be subtracted
6181 to get a position relative to the start of the mode line. */
6182
6183static void
6184note_mode_line_highlight (w, x, mode_line_p)
6185 struct window *w;
6186 int x, mode_line_p;
6187{
6188 struct frame *f = XFRAME (w->frame);
6189 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6190 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6191 struct glyph_row *row;
6192
6193 if (mode_line_p)
6194 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6195 else
045dee35 6196 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6197
6198 if (row->enabled_p)
6199 {
6200 struct glyph *glyph, *end;
6201 Lisp_Object help, map;
6202 int x0;
6203
6204 /* Find the glyph under X. */
6205 glyph = row->glyphs[TEXT_AREA];
6206 end = glyph + row->used[TEXT_AREA];
6207 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6208 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6209 while (glyph < end
6210 && x >= x0 + glyph->pixel_width)
6211 {
6212 x0 += glyph->pixel_width;
6213 ++glyph;
6214 }
6215
6216 if (glyph < end
6217 && STRINGP (glyph->object)
6218 && XSTRING (glyph->object)->intervals
6219 && glyph->charpos >= 0
6220 && glyph->charpos < XSTRING (glyph->object)->size)
6221 {
6222 /* If we're on a string with `help-echo' text property,
6223 arrange for the help to be displayed. This is done by
6224 setting the global variable help_echo to the help string. */
6225 help = Fget_text_property (make_number (glyph->charpos),
6226 Qhelp_echo, glyph->object);
6227 if (STRINGP (help))
6228 help_echo = help;
6229
6230 /* Change the mouse pointer according to what is under X/Y. */
6231 map = Fget_text_property (make_number (glyph->charpos),
6232 Qlocal_map, glyph->object);
6233 if (!NILP (Fkeymapp (map)))
6234 cursor = f->output_data.x->nontext_cursor;
6235 }
6236 }
6237
6238 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6239}
6240
6241
6242/* Take proper action when the mouse has moved to position X, Y on
6243 frame F as regards highlighting characters that have mouse-face
6244 properties. Also de-highlighting chars where the mouse was before.
27f338af 6245 X and Y can be negative or out of range. */
b8009dd1
RS
6246
6247static void
6248note_mouse_highlight (f, x, y)
06a2c219 6249 struct frame *f;
c32cdd9a 6250 int x, y;
b8009dd1 6251{
06a2c219
GM
6252 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6253 int portion;
b8009dd1
RS
6254 Lisp_Object window;
6255 struct window *w;
6256
06a2c219
GM
6257 /* When a menu is active, don't highlight because this looks odd. */
6258#ifdef USE_X_TOOLKIT
6259 if (popup_activated ())
6260 return;
6261#endif
6262
bf1c0ba1
RS
6263 if (disable_mouse_highlight)
6264 return;
6265
06a2c219
GM
6266 dpyinfo->mouse_face_mouse_x = x;
6267 dpyinfo->mouse_face_mouse_y = y;
6268 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6269
06a2c219 6270 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6271 return;
6272
514e4681
RS
6273 if (gc_in_progress)
6274 {
06a2c219 6275 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6276 return;
6277 }
6278
b8009dd1 6279 /* Which window is that in? */
06a2c219 6280 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6281
6282 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6283 if (! EQ (window, dpyinfo->mouse_face_window))
6284 clear_mouse_face (dpyinfo);
6285
6286 /* Not on a window -> return. */
6287 if (!WINDOWP (window))
6288 return;
6289
6290 /* Convert to window-relative pixel coordinates. */
6291 w = XWINDOW (window);
6292 frame_to_window_pixel_xy (w, &x, &y);
6293
9ea173e8 6294 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6295 buffer. */
9ea173e8 6296 if (EQ (window, f->tool_bar_window))
06a2c219 6297 {
9ea173e8 6298 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6299 return;
6300 }
6301
6302 if (portion == 1 || portion == 3)
6303 {
6304 /* Mouse is on the mode or top line. */
6305 note_mode_line_highlight (w, x, portion == 1);
6306 return;
6307 }
6308 else
6309 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6310 f->output_data.x->text_cursor);
b8009dd1 6311
0cdd0c9f
RS
6312 /* Are we in a window whose display is up to date?
6313 And verify the buffer's text has not changed. */
06a2c219
GM
6314 if (/* Within text portion of the window. */
6315 portion == 0
0cdd0c9f 6316 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6317 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6318 && (XFASTINT (w->last_overlay_modified)
6319 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6320 {
06a2c219
GM
6321 int hpos, vpos, pos, i, area;
6322 struct glyph *glyph;
b8009dd1 6323
06a2c219
GM
6324 /* Find the glyph under X/Y. */
6325 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6326
6327 /* Clear mouse face if X/Y not over text. */
6328 if (glyph == NULL
6329 || area != TEXT_AREA
6330 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6331 {
06a2c219
GM
6332 clear_mouse_face (dpyinfo);
6333 return;
6334 }
6335
6336 pos = glyph->charpos;
6337 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6338
6339 /* Check for mouse-face and help-echo. */
6340 {
6341 Lisp_Object mouse_face, overlay, position;
6342 Lisp_Object *overlay_vec;
6343 int len, noverlays;
6344 struct buffer *obuf;
6345 int obegv, ozv;
6346
6347 /* If we get an out-of-range value, return now; avoid an error. */
6348 if (pos > BUF_Z (XBUFFER (w->buffer)))
6349 return;
6350
6351 /* Make the window's buffer temporarily current for
6352 overlays_at and compute_char_face. */
6353 obuf = current_buffer;
6354 current_buffer = XBUFFER (w->buffer);
6355 obegv = BEGV;
6356 ozv = ZV;
6357 BEGV = BEG;
6358 ZV = Z;
6359
6360 /* Is this char mouse-active or does it have help-echo? */
6361 XSETINT (position, pos);
6362
6363 /* Put all the overlays we want in a vector in overlay_vec.
6364 Store the length in len. If there are more than 10, make
6365 enough space for all, and try again. */
6366 len = 10;
6367 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6368 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6369 if (noverlays > len)
6370 {
6371 len = noverlays;
6372 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6373 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6374 }
6375
6376 noverlays = sort_overlays (overlay_vec, noverlays, w);
6377
6378 /* Check mouse-face highlighting. */
6379 if (! (EQ (window, dpyinfo->mouse_face_window)
6380 && vpos >= dpyinfo->mouse_face_beg_row
6381 && vpos <= dpyinfo->mouse_face_end_row
6382 && (vpos > dpyinfo->mouse_face_beg_row
6383 || hpos >= dpyinfo->mouse_face_beg_col)
6384 && (vpos < dpyinfo->mouse_face_end_row
6385 || hpos < dpyinfo->mouse_face_end_col
6386 || dpyinfo->mouse_face_past_end)))
6387 {
6388 /* Clear the display of the old active region, if any. */
6389 clear_mouse_face (dpyinfo);
6390
6391 /* Find the highest priority overlay that has a mouse-face prop. */
6392 overlay = Qnil;
6393 for (i = 0; i < noverlays; i++)
6394 {
6395 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6396 if (!NILP (mouse_face))
6397 {
6398 overlay = overlay_vec[i];
6399 break;
6400 }
6401 }
6402
6403 /* If no overlay applies, get a text property. */
6404 if (NILP (overlay))
6405 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6406
6407 /* Handle the overlay case. */
6408 if (! NILP (overlay))
6409 {
6410 /* Find the range of text around this char that
6411 should be active. */
6412 Lisp_Object before, after;
6413 int ignore;
6414
6415 before = Foverlay_start (overlay);
6416 after = Foverlay_end (overlay);
6417 /* Record this as the current active region. */
6418 fast_find_position (w, XFASTINT (before),
6419 &dpyinfo->mouse_face_beg_col,
6420 &dpyinfo->mouse_face_beg_row,
6421 &dpyinfo->mouse_face_beg_x,
6422 &dpyinfo->mouse_face_beg_y);
6423 dpyinfo->mouse_face_past_end
6424 = !fast_find_position (w, XFASTINT (after),
6425 &dpyinfo->mouse_face_end_col,
6426 &dpyinfo->mouse_face_end_row,
6427 &dpyinfo->mouse_face_end_x,
6428 &dpyinfo->mouse_face_end_y);
6429 dpyinfo->mouse_face_window = window;
6430 dpyinfo->mouse_face_face_id
6431 = face_at_buffer_position (w, pos, 0, 0,
6432 &ignore, pos + 1, 1);
6433
6434 /* Display it as active. */
6435 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6436 }
6437 /* Handle the text property case. */
6438 else if (! NILP (mouse_face))
6439 {
6440 /* Find the range of text around this char that
6441 should be active. */
6442 Lisp_Object before, after, beginning, end;
6443 int ignore;
6444
6445 beginning = Fmarker_position (w->start);
6446 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6447 - XFASTINT (w->window_end_pos)));
6448 before
6449 = Fprevious_single_property_change (make_number (pos + 1),
6450 Qmouse_face,
6451 w->buffer, beginning);
6452 after
6453 = Fnext_single_property_change (position, Qmouse_face,
6454 w->buffer, end);
6455 /* Record this as the current active region. */
6456 fast_find_position (w, XFASTINT (before),
6457 &dpyinfo->mouse_face_beg_col,
6458 &dpyinfo->mouse_face_beg_row,
6459 &dpyinfo->mouse_face_beg_x,
6460 &dpyinfo->mouse_face_beg_y);
6461 dpyinfo->mouse_face_past_end
6462 = !fast_find_position (w, XFASTINT (after),
6463 &dpyinfo->mouse_face_end_col,
6464 &dpyinfo->mouse_face_end_row,
6465 &dpyinfo->mouse_face_end_x,
6466 &dpyinfo->mouse_face_end_y);
6467 dpyinfo->mouse_face_window = window;
6468 dpyinfo->mouse_face_face_id
6469 = face_at_buffer_position (w, pos, 0, 0,
6470 &ignore, pos + 1, 1);
6471
6472 /* Display it as active. */
6473 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6474 }
6475 }
6476
6477 /* Look for a `help-echo' property. */
6478 {
6479 Lisp_Object help;
6480
6481 /* Check overlays first. */
6482 help = Qnil;
6483 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6484 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6485
6486 /* Try text properties. */
6487 if (!STRINGP (help)
6488 && ((STRINGP (glyph->object)
6489 && glyph->charpos >= 0
6490 && glyph->charpos < XSTRING (glyph->object)->size)
6491 || (BUFFERP (glyph->object)
6492 && glyph->charpos >= BEGV
6493 && glyph->charpos < ZV)))
6494 help = Fget_text_property (make_number (glyph->charpos),
6495 Qhelp_echo, glyph->object);
6496
6497 if (STRINGP (help))
6498 help_echo = help;
6499 }
6500
6501 BEGV = obegv;
6502 ZV = ozv;
6503 current_buffer = obuf;
6504 }
6505 }
6506}
6507
6508static void
6509redo_mouse_highlight ()
6510{
6511 if (!NILP (last_mouse_motion_frame)
6512 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6513 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6514 last_mouse_motion_event.x,
6515 last_mouse_motion_event.y);
6516}
6517
6518
6519\f
6520/***********************************************************************
9ea173e8 6521 Tool-bars
06a2c219
GM
6522 ***********************************************************************/
6523
9ea173e8
GM
6524static int x_tool_bar_item P_ ((struct frame *, int, int,
6525 struct glyph **, int *, int *, int *));
06a2c219 6526
9ea173e8 6527/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6528 or -1. */
6529
9ea173e8 6530static int last_tool_bar_item;
06a2c219
GM
6531
6532
9ea173e8
GM
6533/* Get information about the tool-bar item at position X/Y on frame F.
6534 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6535 the current matrix of the tool-bar window of F, or NULL if not
6536 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6537 item in F->current_tool_bar_items. Value is
06a2c219 6538
9ea173e8 6539 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6540 0 if X/Y is on the same item that was highlighted before.
6541 1 otherwise. */
6542
6543static int
9ea173e8 6544x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6545 struct frame *f;
6546 int x, y;
6547 struct glyph **glyph;
6548 int *hpos, *vpos, *prop_idx;
6549{
6550 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6551 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6552 int area;
6553
6554 /* Find the glyph under X/Y. */
6555 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6556 if (*glyph == NULL)
6557 return -1;
6558
9ea173e8
GM
6559 /* Get the start of this tool-bar item's properties in
6560 f->current_tool_bar_items. */
6561 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6562 return -1;
6563
6564 /* Is mouse on the highlighted item? */
9ea173e8 6565 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6566 && *vpos >= dpyinfo->mouse_face_beg_row
6567 && *vpos <= dpyinfo->mouse_face_end_row
6568 && (*vpos > dpyinfo->mouse_face_beg_row
6569 || *hpos >= dpyinfo->mouse_face_beg_col)
6570 && (*vpos < dpyinfo->mouse_face_end_row
6571 || *hpos < dpyinfo->mouse_face_end_col
6572 || dpyinfo->mouse_face_past_end))
6573 return 0;
6574
6575 return 1;
6576}
6577
6578
9ea173e8 6579/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6580 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6581 or ButtonRelase. */
6582
6583static void
9ea173e8 6584x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6585 struct frame *f;
6586 XButtonEvent *button_event;
6587{
6588 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6589 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6590 int hpos, vpos, prop_idx;
6591 struct glyph *glyph;
6592 Lisp_Object enabled_p;
6593 int x = button_event->x;
6594 int y = button_event->y;
6595
9ea173e8 6596 /* If not on the highlighted tool-bar item, return. */
06a2c219 6597 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6598 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6599 return;
6600
6601 /* If item is disabled, do nothing. */
9ea173e8
GM
6602 enabled_p = (XVECTOR (f->current_tool_bar_items)
6603 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6604 if (NILP (enabled_p))
6605 return;
6606
6607 if (button_event->type == ButtonPress)
6608 {
6609 /* Show item in pressed state. */
6610 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6611 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6612 last_tool_bar_item = prop_idx;
06a2c219
GM
6613 }
6614 else
6615 {
6616 Lisp_Object key, frame;
6617 struct input_event event;
6618
6619 /* Show item in released state. */
6620 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6621 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6622
9ea173e8
GM
6623 key = (XVECTOR (f->current_tool_bar_items)
6624 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6625
6626 XSETFRAME (frame, f);
9ea173e8
GM
6627 event.kind = TOOL_BAR_EVENT;
6628 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6629 kbd_buffer_store_event (&event);
6630
9ea173e8 6631 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6632 event.frame_or_window = Fcons (frame, key);
6633 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6634 button_event->state);
6635 kbd_buffer_store_event (&event);
9ea173e8 6636 last_tool_bar_item = -1;
06a2c219
GM
6637 }
6638}
6639
6640
9ea173e8
GM
6641/* Possibly highlight a tool-bar item on frame F when mouse moves to
6642 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6643 note_mouse_highlight. */
6644
6645static void
9ea173e8 6646note_tool_bar_highlight (f, x, y)
06a2c219
GM
6647 struct frame *f;
6648 int x, y;
6649{
9ea173e8 6650 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6651 struct window *w = XWINDOW (window);
6652 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6653 int hpos, vpos;
6654 struct glyph *glyph;
6655 struct glyph_row *row;
5c187dee 6656 int i;
06a2c219
GM
6657 Lisp_Object enabled_p;
6658 int prop_idx;
6659 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6660 int mouse_down_p, rc;
06a2c219
GM
6661
6662 /* Function note_mouse_highlight is called with negative x(y
6663 values when mouse moves outside of the frame. */
6664 if (x <= 0 || y <= 0)
6665 {
6666 clear_mouse_face (dpyinfo);
6667 return;
6668 }
6669
9ea173e8 6670 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6671 if (rc < 0)
6672 {
9ea173e8 6673 /* Not on tool-bar item. */
06a2c219
GM
6674 clear_mouse_face (dpyinfo);
6675 return;
6676 }
6677 else if (rc == 0)
9ea173e8 6678 /* On same tool-bar item as before. */
06a2c219 6679 goto set_help_echo;
b8009dd1 6680
06a2c219
GM
6681 clear_mouse_face (dpyinfo);
6682
9ea173e8 6683 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6684 mouse_down_p = (dpyinfo->grabbed
6685 && f == last_mouse_frame
6686 && FRAME_LIVE_P (f));
6687 if (mouse_down_p
9ea173e8 6688 && last_tool_bar_item != prop_idx)
06a2c219
GM
6689 return;
6690
6691 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6692 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6693
9ea173e8
GM
6694 /* If tool-bar item is not enabled, don't highlight it. */
6695 enabled_p = (XVECTOR (f->current_tool_bar_items)
6696 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6697 if (!NILP (enabled_p))
6698 {
6699 /* Compute the x-position of the glyph. In front and past the
6700 image is a space. We include this is the highlighted area. */
6701 row = MATRIX_ROW (w->current_matrix, vpos);
6702 for (i = x = 0; i < hpos; ++i)
6703 x += row->glyphs[TEXT_AREA][i].pixel_width;
6704
6705 /* Record this as the current active region. */
6706 dpyinfo->mouse_face_beg_col = hpos;
6707 dpyinfo->mouse_face_beg_row = vpos;
6708 dpyinfo->mouse_face_beg_x = x;
6709 dpyinfo->mouse_face_beg_y = row->y;
6710 dpyinfo->mouse_face_past_end = 0;
6711
6712 dpyinfo->mouse_face_end_col = hpos + 1;
6713 dpyinfo->mouse_face_end_row = vpos;
6714 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6715 dpyinfo->mouse_face_end_y = row->y;
6716 dpyinfo->mouse_face_window = window;
9ea173e8 6717 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6718
6719 /* Display it as active. */
6720 show_mouse_face (dpyinfo, draw);
6721 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6722 }
06a2c219
GM
6723
6724 set_help_echo:
6725
9ea173e8 6726 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6727 XTread_socket does the rest. */
9ea173e8
GM
6728 help_echo = (XVECTOR (f->current_tool_bar_items)
6729 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6730 if (!STRINGP (help_echo))
9ea173e8
GM
6731 help_echo = (XVECTOR (f->current_tool_bar_items)
6732 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6733}
4d73d038 6734
06a2c219
GM
6735
6736\f
6737/* Find the glyph matrix position of buffer position POS in window W.
6738 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6739 current glyphs must be up to date. If POS is above window start
6740 return (0, 0, 0, 0). If POS is after end of W, return end of
6741 last line in W. */
b8009dd1
RS
6742
6743static int
06a2c219
GM
6744fast_find_position (w, pos, hpos, vpos, x, y)
6745 struct window *w;
b8009dd1 6746 int pos;
06a2c219 6747 int *hpos, *vpos, *x, *y;
b8009dd1 6748{
b8009dd1 6749 int i;
bf1c0ba1 6750 int lastcol;
06a2c219
GM
6751 int maybe_next_line_p = 0;
6752 int line_start_position;
6753 int yb = window_text_bottom_y (w);
6754 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6755 struct glyph_row *best_row = row;
6756 int row_vpos = 0, best_row_vpos = 0;
6757 int current_x;
6758
6759 while (row->y < yb)
b8009dd1 6760 {
06a2c219
GM
6761 if (row->used[TEXT_AREA])
6762 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6763 else
6764 line_start_position = 0;
6765
6766 if (line_start_position > pos)
b8009dd1 6767 break;
77b68646
RS
6768 /* If the position sought is the end of the buffer,
6769 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6770 else if (line_start_position == pos
6771 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6772 {
06a2c219 6773 maybe_next_line_p = 1;
77b68646
RS
6774 break;
6775 }
06a2c219
GM
6776 else if (line_start_position > 0)
6777 {
6778 best_row = row;
6779 best_row_vpos = row_vpos;
6780 }
6781
6782 ++row;
6783 ++row_vpos;
b8009dd1 6784 }
06a2c219
GM
6785
6786 /* Find the right column within BEST_ROW. */
6787 lastcol = 0;
6788 current_x = best_row->x;
6789 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6790 {
06a2c219
GM
6791 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6792 int charpos;
6793
6794 charpos = glyph->charpos;
6795 if (charpos == pos)
bf1c0ba1 6796 {
06a2c219
GM
6797 *hpos = i;
6798 *vpos = best_row_vpos;
6799 *x = current_x;
6800 *y = best_row->y;
bf1c0ba1
RS
6801 return 1;
6802 }
06a2c219 6803 else if (charpos > pos)
4d73d038 6804 break;
06a2c219
GM
6805 else if (charpos > 0)
6806 lastcol = i;
6807
6808 current_x += glyph->pixel_width;
bf1c0ba1 6809 }
b8009dd1 6810
77b68646
RS
6811 /* If we're looking for the end of the buffer,
6812 and we didn't find it in the line we scanned,
6813 use the start of the following line. */
06a2c219 6814 if (maybe_next_line_p)
77b68646 6815 {
06a2c219
GM
6816 ++best_row;
6817 ++best_row_vpos;
6818 lastcol = 0;
6819 current_x = best_row->x;
77b68646
RS
6820 }
6821
06a2c219
GM
6822 *vpos = best_row_vpos;
6823 *hpos = lastcol + 1;
6824 *x = current_x;
6825 *y = best_row->y;
b8009dd1
RS
6826 return 0;
6827}
6828
06a2c219 6829
b8009dd1
RS
6830/* Display the active region described by mouse_face_*
6831 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6832
6833static void
06a2c219 6834show_mouse_face (dpyinfo, draw)
7a13e894 6835 struct x_display_info *dpyinfo;
06a2c219 6836 enum draw_glyphs_face draw;
b8009dd1 6837{
7a13e894 6838 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6839 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6840 int i;
06a2c219
GM
6841 int cursor_off_p = 0;
6842 struct cursor_pos saved_cursor;
6843
6844 saved_cursor = output_cursor;
6845
6846 /* If window is in the process of being destroyed, don't bother
6847 to do anything. */
6848 if (w->current_matrix == NULL)
6849 goto set_x_cursor;
6850
6851 /* Recognize when we are called to operate on rows that don't exist
6852 anymore. This can happen when a window is split. */
6853 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6854 goto set_x_cursor;
6855
6856 set_output_cursor (&w->phys_cursor);
6857
6858 /* Note that mouse_face_beg_row etc. are window relative. */
6859 for (i = dpyinfo->mouse_face_beg_row;
6860 i <= dpyinfo->mouse_face_end_row;
6861 i++)
6862 {
6863 int start_hpos, end_hpos, start_x;
6864 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6865
6866 /* Don't do anything if row doesn't have valid contents. */
6867 if (!row->enabled_p)
6868 continue;
6869
6870 /* For all but the first row, the highlight starts at column 0. */
6871 if (i == dpyinfo->mouse_face_beg_row)
6872 {
6873 start_hpos = dpyinfo->mouse_face_beg_col;
6874 start_x = dpyinfo->mouse_face_beg_x;
6875 }
6876 else
6877 {
6878 start_hpos = 0;
6879 start_x = 0;
6880 }
6881
6882 if (i == dpyinfo->mouse_face_end_row)
6883 end_hpos = dpyinfo->mouse_face_end_col;
6884 else
6885 end_hpos = row->used[TEXT_AREA];
6886
6887 /* If the cursor's in the text we are about to rewrite, turn the
6888 cursor off. */
6889 if (!w->pseudo_window_p
6890 && i == output_cursor.vpos
6891 && output_cursor.hpos >= start_hpos - 1
6892 && output_cursor.hpos <= end_hpos)
514e4681 6893 {
06a2c219
GM
6894 x_update_window_cursor (w, 0);
6895 cursor_off_p = 1;
514e4681 6896 }
b8009dd1 6897
06a2c219 6898 if (end_hpos > start_hpos)
54a91a0f 6899 x_draw_glyphs (w, start_x, row, TEXT_AREA,
66ac4b0e 6900 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6901 }
6902
514e4681 6903 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6904 if (cursor_off_p)
6905 x_display_cursor (w, 1,
6906 output_cursor.hpos, output_cursor.vpos,
6907 output_cursor.x, output_cursor.y);
2729a2b5 6908
06a2c219 6909 output_cursor = saved_cursor;
fb3b7de5 6910
06a2c219
GM
6911 set_x_cursor:
6912
6913 /* Change the mouse cursor. */
6914 if (draw == DRAW_NORMAL_TEXT)
6915 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6916 f->output_data.x->text_cursor);
6917 else if (draw == DRAW_MOUSE_FACE)
334208b7 6918 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6919 f->output_data.x->cross_cursor);
27ead1d5 6920 else
334208b7 6921 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6922 f->output_data.x->nontext_cursor);
b8009dd1
RS
6923}
6924
6925/* Clear out the mouse-highlighted active region.
06a2c219 6926 Redraw it un-highlighted first. */
b8009dd1 6927
06a2c219 6928void
7a13e894
RS
6929clear_mouse_face (dpyinfo)
6930 struct x_display_info *dpyinfo;
b8009dd1 6931{
06a2c219
GM
6932 if (tip_frame)
6933 return;
6934
7a13e894 6935 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6936 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6937
7a13e894
RS
6938 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6939 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6940 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6941}
e687d06e
RS
6942
6943/* Just discard the mouse face information for frame F, if any.
6944 This is used when the size of F is changed. */
6945
dfcf069d 6946void
e687d06e
RS
6947cancel_mouse_face (f)
6948 FRAME_PTR f;
6949{
6950 Lisp_Object window;
6951 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6952
6953 window = dpyinfo->mouse_face_window;
6954 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6955 {
6956 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6957 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6958 dpyinfo->mouse_face_window = Qnil;
6959 }
6960}
b8009dd1 6961\f
ab648270
JB
6962static struct scroll_bar *x_window_to_scroll_bar ();
6963static void x_scroll_bar_report_motion ();
12ba150f 6964
90e65f07 6965/* Return the current position of the mouse.
2d7fc7e8 6966 *fp should be a frame which indicates which display to ask about.
90e65f07 6967
2d7fc7e8 6968 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6969 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6970 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6971 position on the scroll bar.
12ba150f 6972
2d7fc7e8 6973 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
6974 mouse is on, *bar_window to nil, and *x and *y to the character cell
6975 the mouse is over.
6976
06a2c219 6977 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
6978 was at this position.
6979
a135645a
RS
6980 Don't store anything if we don't have a valid set of values to report.
6981
90e65f07 6982 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 6983 movement. */
90e65f07
JB
6984
6985static void
1cf412ec 6986XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 6987 FRAME_PTR *fp;
1cf412ec 6988 int insist;
12ba150f 6989 Lisp_Object *bar_window;
ab648270 6990 enum scroll_bar_part *part;
90e65f07 6991 Lisp_Object *x, *y;
e5d77022 6992 unsigned long *time;
90e65f07 6993{
a135645a
RS
6994 FRAME_PTR f1;
6995
90e65f07
JB
6996 BLOCK_INPUT;
6997
8bcee03e 6998 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 6999 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7000 else
7001 {
12ba150f
JB
7002 Window root;
7003 int root_x, root_y;
90e65f07 7004
12ba150f
JB
7005 Window dummy_window;
7006 int dummy;
7007
39d8bb4d
KH
7008 Lisp_Object frame, tail;
7009
7010 /* Clear the mouse-moved flag for every frame on this display. */
7011 FOR_EACH_FRAME (tail, frame)
7012 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7013 XFRAME (frame)->mouse_moved = 0;
7014
ab648270 7015 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7016
7017 /* Figure out which root window we're on. */
334208b7
RS
7018 XQueryPointer (FRAME_X_DISPLAY (*fp),
7019 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7020
7021 /* The root window which contains the pointer. */
7022 &root,
7023
7024 /* Trash which we can't trust if the pointer is on
7025 a different screen. */
7026 &dummy_window,
7027
7028 /* The position on that root window. */
58769bee 7029 &root_x, &root_y,
12ba150f
JB
7030
7031 /* More trash we can't trust. */
7032 &dummy, &dummy,
7033
7034 /* Modifier keys and pointer buttons, about which
7035 we don't care. */
7036 (unsigned int *) &dummy);
7037
7038 /* Now we have a position on the root; find the innermost window
7039 containing the pointer. */
7040 {
7041 Window win, child;
7042 int win_x, win_y;
06a2c219 7043 int parent_x = 0, parent_y = 0;
e99db5a1 7044 int count;
12ba150f
JB
7045
7046 win = root;
69388238 7047
2d7fc7e8
RS
7048 /* XTranslateCoordinates can get errors if the window
7049 structure is changing at the same time this function
7050 is running. So at least we must not crash from them. */
7051
e99db5a1 7052 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7053
334208b7 7054 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7055 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7056 {
69388238
RS
7057 /* If mouse was grabbed on a frame, give coords for that frame
7058 even if the mouse is now outside it. */
334208b7 7059 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7060
12ba150f 7061 /* From-window, to-window. */
69388238 7062 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7063
7064 /* From-position, to-position. */
7065 root_x, root_y, &win_x, &win_y,
7066
7067 /* Child of win. */
7068 &child);
69388238
RS
7069 f1 = last_mouse_frame;
7070 }
7071 else
7072 {
7073 while (1)
7074 {
334208b7 7075 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7076
69388238
RS
7077 /* From-window, to-window. */
7078 root, win,
12ba150f 7079
69388238
RS
7080 /* From-position, to-position. */
7081 root_x, root_y, &win_x, &win_y,
7082
7083 /* Child of win. */
7084 &child);
7085
9af3143a 7086 if (child == None || child == win)
69388238
RS
7087 break;
7088
7089 win = child;
7090 parent_x = win_x;
7091 parent_y = win_y;
7092 }
12ba150f 7093
69388238
RS
7094 /* Now we know that:
7095 win is the innermost window containing the pointer
7096 (XTC says it has no child containing the pointer),
7097 win_x and win_y are the pointer's position in it
7098 (XTC did this the last time through), and
7099 parent_x and parent_y are the pointer's position in win's parent.
7100 (They are what win_x and win_y were when win was child.
7101 If win is the root window, it has no parent, and
7102 parent_{x,y} are invalid, but that's okay, because we'll
7103 never use them in that case.) */
7104
7105 /* Is win one of our frames? */
19126e11 7106 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7107 }
58769bee 7108
2d7fc7e8
RS
7109 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7110 f1 = 0;
7111
e99db5a1 7112 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7113
ab648270 7114 /* If not, is it one of our scroll bars? */
a135645a 7115 if (! f1)
12ba150f 7116 {
ab648270 7117 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7118
7119 if (bar)
7120 {
a135645a 7121 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7122 win_x = parent_x;
7123 win_y = parent_y;
7124 }
7125 }
90e65f07 7126
8bcee03e 7127 if (f1 == 0 && insist > 0)
b86bd3dd 7128 f1 = SELECTED_FRAME ();
1cf412ec 7129
a135645a 7130 if (f1)
12ba150f 7131 {
06a2c219
GM
7132 /* Ok, we found a frame. Store all the values.
7133 last_mouse_glyph is a rectangle used to reduce the
7134 generation of mouse events. To not miss any motion
7135 events, we must divide the frame into rectangles of the
7136 size of the smallest character that could be displayed
7137 on it, i.e. into the same rectangles that matrices on
7138 the frame are divided into. */
7139
7140#if OLD_REDISPLAY_CODE
2b5c9e71 7141 int ignore1, ignore2;
2b5c9e71 7142 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7143 &last_mouse_glyph,
1cf412ec
RS
7144 FRAME_X_DISPLAY_INFO (f1)->grabbed
7145 || insist);
06a2c219
GM
7146#else
7147 {
7148 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7149 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7150 int x = win_x;
7151 int y = win_y;
7152
7153 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7154 round down even for negative values. */
7155 if (x < 0)
7156 x -= width - 1;
7157 if (y < 0)
7158 y -= height - 1;
7159
7160 last_mouse_glyph.width = width;
7161 last_mouse_glyph.height = height;
7162 last_mouse_glyph.x = (x + width - 1) / width * width;
7163 last_mouse_glyph.y = (y + height - 1) / height * height;
7164 }
7165#endif
12ba150f
JB
7166
7167 *bar_window = Qnil;
7168 *part = 0;
334208b7 7169 *fp = f1;
e0c1aef2
KH
7170 XSETINT (*x, win_x);
7171 XSETINT (*y, win_y);
12ba150f
JB
7172 *time = last_mouse_movement_time;
7173 }
7174 }
7175 }
90e65f07
JB
7176
7177 UNBLOCK_INPUT;
7178}
f451eb13 7179
06a2c219
GM
7180
7181DEFUN ("xt-process-timeouts", Fxt_process_timeouts, Sxt_process_timeouts,
7182 0, 0, 0,
7183 "Arrange for Xt timeout callbacks to be called.")
7184 ()
7185{
7186#ifdef USE_X_TOOLKIT
7187 BLOCK_INPUT;
7188 while (XtAppPending (Xt_app_con) & XtIMTimer)
7189 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7190 UNBLOCK_INPUT;
7191#endif /* USE_X_TOOLKIT */
7192
7193 return Qnil;
7194}
7195
7196
7197\f
7198/* Scroll bar support. */
7199
7200/* Given an X window ID, find the struct scroll_bar which manages it.
7201 This can be called in GC, so we have to make sure to strip off mark
7202 bits. */
7203static struct scroll_bar *
7204x_window_to_scroll_bar (window_id)
7205 Window window_id;
7206{
7207 Lisp_Object tail;
7208
7209 for (tail = Vframe_list;
7210 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7211 tail = XCDR (tail))
06a2c219
GM
7212 {
7213 Lisp_Object frame, bar, condemned;
7214
8e713be6 7215 frame = XCAR (tail);
06a2c219
GM
7216 /* All elements of Vframe_list should be frames. */
7217 if (! GC_FRAMEP (frame))
7218 abort ();
7219
7220 /* Scan this frame's scroll bar list for a scroll bar with the
7221 right window ID. */
7222 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7223 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7224 /* This trick allows us to search both the ordinary and
7225 condemned scroll bar lists with one loop. */
7226 ! GC_NILP (bar) || (bar = condemned,
7227 condemned = Qnil,
7228 ! GC_NILP (bar));
7229 bar = XSCROLL_BAR (bar)->next)
7230 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7231 return XSCROLL_BAR (bar);
7232 }
7233
7234 return 0;
7235}
7236
7237
7238\f
7239/************************************************************************
7240 Toolkit scroll bars
7241 ************************************************************************/
7242
7243#if USE_TOOLKIT_SCROLL_BARS
7244
7245static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7246static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7247static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7248 struct scroll_bar *));
7249static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7250 int, int, int));
7251
7252
7253/* Id of action hook installed for scroll bars. */
7254
7255static XtActionHookId action_hook_id;
7256
7257/* Lisp window being scrolled. Set when starting to interact with
7258 a toolkit scroll bar, reset to nil when ending the interaction. */
7259
7260static Lisp_Object window_being_scrolled;
7261
7262/* Last scroll bar part sent in xm_scroll_callback. */
7263
7264static int last_scroll_bar_part;
7265
ec18280f
SM
7266/* Whether this is an Xaw with arrow-scrollbars. This should imply
7267 that movements of 1/20 of the screen size are mapped to up/down. */
7268
7269static Boolean xaw3d_arrow_scroll;
7270
7271/* Whether the drag scrolling maintains the mouse at the top of the
7272 thumb. If not, resizing the thumb needs to be done more carefully
7273 to avoid jerkyness. */
7274
7275static Boolean xaw3d_pick_top;
7276
06a2c219
GM
7277
7278/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7279 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7280 the user ends an interaction with the scroll bar, and generates
7281 a `end-scroll' scroll_bar_click' event if so. */
7282
7283static void
7284xt_action_hook (widget, client_data, action_name, event, params,
7285 num_params)
7286 Widget widget;
7287 XtPointer client_data;
7288 String action_name;
7289 XEvent *event;
7290 String *params;
7291 Cardinal *num_params;
7292{
7293 int scroll_bar_p;
7294 char *end_action;
7295
7296#ifdef USE_MOTIF
7297 scroll_bar_p = XmIsScrollBar (widget);
7298 end_action = "Release";
ec18280f 7299#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7300 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7301 end_action = "EndScroll";
ec18280f 7302#endif /* USE_MOTIF */
06a2c219
GM
7303
7304 /* Although LessTif uses XtTimeouts like Xaw3d, the timer hack to
7305 let Xt timeouts be processed doesn't work. */
7306 if (scroll_bar_p
7307 && strcmp (action_name, end_action) == 0
7308 && WINDOWP (window_being_scrolled))
7309 {
7310 struct window *w;
7311
7312 x_send_scroll_bar_event (window_being_scrolled,
7313 scroll_bar_end_scroll, 0, 0);
7314 w = XWINDOW (window_being_scrolled);
7315 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7316 window_being_scrolled = Qnil;
7317 last_scroll_bar_part = -1;
7318 }
7319}
7320
7321
7322/* Send a client message with message type Xatom_Scrollbar for a
7323 scroll action to the frame of WINDOW. PART is a value identifying
7324 the part of the scroll bar that was clicked on. PORTION is the
7325 amount to scroll of a whole of WHOLE. */
7326
7327static void
7328x_send_scroll_bar_event (window, part, portion, whole)
7329 Lisp_Object window;
7330 int part, portion, whole;
7331{
7332 XEvent event;
7333 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7334 struct frame *f = XFRAME (XWINDOW (window)->frame);
7335
7336 /* Construct a ClientMessage event to send to the frame. */
7337 ev->type = ClientMessage;
7338 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7339 ev->display = FRAME_X_DISPLAY (f);
7340 ev->window = FRAME_X_WINDOW (f);
7341 ev->format = 32;
7342 ev->data.l[0] = (long) window;
7343 ev->data.l[1] = (long) part;
7344 ev->data.l[2] = (long) 0;
7345 ev->data.l[3] = (long) portion;
7346 ev->data.l[4] = (long) whole;
7347
7348 /* Setting the event mask to zero means that the message will
7349 be sent to the client that created the window, and if that
7350 window no longer exists, no event will be sent. */
7351 BLOCK_INPUT;
7352 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7353 UNBLOCK_INPUT;
7354}
7355
7356
7357/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7358 in *IEVENT. */
7359
7360static void
7361x_scroll_bar_to_input_event (event, ievent)
7362 XEvent *event;
7363 struct input_event *ievent;
7364{
7365 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7366 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7367 struct frame *f = XFRAME (XWINDOW (window)->frame);
7368
7369 ievent->kind = scroll_bar_click;
7370 ievent->frame_or_window = window;
7371 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7372 ievent->part = ev->data.l[1];
7373 ievent->code = ev->data.l[2];
7374 ievent->x = make_number ((int) ev->data.l[3]);
7375 ievent->y = make_number ((int) ev->data.l[4]);
7376 ievent->modifiers = 0;
7377}
7378
7379
7380#ifdef USE_MOTIF
7381
7382/* Minimum and maximum values used for Motif scroll bars. */
7383
7384#define XM_SB_MIN 1
7385#define XM_SB_MAX 10000000
7386#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7387
7388
7389/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7390 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7391 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7392
7393static void
7394xm_scroll_callback (widget, client_data, call_data)
7395 Widget widget;
7396 XtPointer client_data, call_data;
7397{
7398 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7399 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7400 double percent;
7401 int part = -1, whole = 0, portion = 0;
7402
7403 switch (cs->reason)
7404 {
7405 case XmCR_DECREMENT:
7406 bar->dragging = Qnil;
7407 part = scroll_bar_up_arrow;
7408 break;
7409
7410 case XmCR_INCREMENT:
7411 bar->dragging = Qnil;
7412 part = scroll_bar_down_arrow;
7413 break;
7414
7415 case XmCR_PAGE_DECREMENT:
7416 bar->dragging = Qnil;
7417 part = scroll_bar_above_handle;
7418 break;
7419
7420 case XmCR_PAGE_INCREMENT:
7421 bar->dragging = Qnil;
7422 part = scroll_bar_below_handle;
7423 break;
7424
7425 case XmCR_TO_TOP:
7426 bar->dragging = Qnil;
7427 part = scroll_bar_to_top;
7428 break;
7429
7430 case XmCR_TO_BOTTOM:
7431 bar->dragging = Qnil;
7432 part = scroll_bar_to_bottom;
7433 break;
7434
7435 case XmCR_DRAG:
7436 {
7437 int slider_size;
7438 int dragging_down_p = (INTEGERP (bar->dragging)
7439 && XINT (bar->dragging) <= cs->value);
7440
7441 /* Get the slider size. */
7442 BLOCK_INPUT;
7443 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7444 UNBLOCK_INPUT;
7445
7446 /* At the max position of the scroll bar, do a line-wise
7447 movement. Without doing anything, the LessTif scroll bar
7448 calls us with the same cs->value again and again. If we
7449 want to make sure that we can reach the end of the buffer,
7450 we have to do something.
7451
7452 Implementation note: setting bar->dragging always to
7453 cs->value gives a smoother movement at the max position.
7454 Setting it to nil when doing line-wise movement gives
7455 a better slider behavior. */
7456
7457 if (cs->value + slider_size == XM_SB_MAX
7458 || (dragging_down_p
7459 && last_scroll_bar_part == scroll_bar_down_arrow))
7460 {
7461 part = scroll_bar_down_arrow;
7462 bar->dragging = Qnil;
7463 }
7464 else
7465 {
7466 whole = XM_SB_RANGE;
7467 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7468 part = scroll_bar_handle;
7469 bar->dragging = make_number (cs->value);
7470 }
7471 }
7472 break;
7473
7474 case XmCR_VALUE_CHANGED:
7475 break;
7476 };
7477
7478 if (part >= 0)
7479 {
7480 window_being_scrolled = bar->window;
7481 last_scroll_bar_part = part;
7482 x_send_scroll_bar_event (bar->window, part, portion, whole);
7483 }
7484}
7485
7486
ec18280f 7487#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7488
7489
ec18280f 7490/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7491 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7492 scroll bar struct. CALL_DATA is a pointer to a float saying where
7493 the thumb is. */
7494
7495static void
ec18280f 7496xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7497 Widget widget;
7498 XtPointer client_data, call_data;
7499{
7500 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7501 float top = *(float *) call_data;
7502 float shown;
ec18280f
SM
7503 int whole, portion, height;
7504 int part;
06a2c219
GM
7505
7506 /* Get the size of the thumb, a value between 0 and 1. */
7507 BLOCK_INPUT;
ec18280f 7508 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7509 UNBLOCK_INPUT;
7510
7511 whole = 10000000;
7512 portion = shown < 1 ? top * whole : 0;
06a2c219 7513
ec18280f
SM
7514 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7515 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7516 the bottom, so we force the scrolling whenever we see that we're
7517 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7518 we try to ensure that we always stay two pixels away from the
7519 bottom). */
06a2c219
GM
7520 part = scroll_bar_down_arrow;
7521 else
7522 part = scroll_bar_handle;
7523
7524 window_being_scrolled = bar->window;
7525 bar->dragging = make_number (portion);
7526 last_scroll_bar_part = part;
7527 x_send_scroll_bar_event (bar->window, part, portion, whole);
7528}
7529
7530
ec18280f
SM
7531/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7532 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7533 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7534 the scroll bar. CALL_DATA is an integer specifying the action that
7535 has taken place. It's magnitude is in the range 0..height of the
7536 scroll bar. Negative values mean scroll towards buffer start.
7537 Values < height of scroll bar mean line-wise movement. */
7538
7539static void
ec18280f 7540xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7541 Widget widget;
7542 XtPointer client_data, call_data;
7543{
7544 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7545 int position = (int) call_data;
7546 Dimension height;
7547 int part;
7548
7549 /* Get the height of the scroll bar. */
7550 BLOCK_INPUT;
7551 XtVaGetValues (widget, XtNheight, &height, NULL);
7552 UNBLOCK_INPUT;
7553
ec18280f
SM
7554 if (abs (position) >= height)
7555 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7556
7557 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7558 it maps line-movement to call_data = max(5, height/20). */
7559 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7560 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7561 else
ec18280f 7562 part = scroll_bar_move_ratio;
06a2c219
GM
7563
7564 window_being_scrolled = bar->window;
7565 bar->dragging = Qnil;
7566 last_scroll_bar_part = part;
ec18280f 7567 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7568}
7569
7570
7571#endif /* not USE_MOTIF */
7572
7573
7574/* Create the widget for scroll bar BAR on frame F. Record the widget
7575 and X window of the scroll bar in BAR. */
7576
7577static void
7578x_create_toolkit_scroll_bar (f, bar)
7579 struct frame *f;
7580 struct scroll_bar *bar;
7581{
7582 Window xwindow;
7583 Widget widget;
7584 Arg av[20];
7585 int ac = 0;
7586 char *scroll_bar_name = "verticalScrollBar";
7587 unsigned long pixel;
7588
7589 BLOCK_INPUT;
7590
7591#ifdef USE_MOTIF
7592 /* LessTif 0.85, problems:
7593
7594 1. When the mouse if over the scroll bar, the scroll bar will
7595 get keyboard events. I didn't find a way to turn this off.
7596
7597 2. Do we have to explicitly set the cursor to get an arrow
7598 cursor (see below)? */
7599
7600 /* Set resources. Create the widget. */
7601 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7602 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7603 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7604 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7605 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7606 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7607 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7608
7609 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7610 if (pixel != -1)
7611 {
7612 XtSetArg (av[ac], XmNforeground, pixel);
7613 ++ac;
7614 }
7615
7616 pixel = f->output_data.x->scroll_bar_background_pixel;
7617 if (pixel != -1)
7618 {
7619 XtSetArg (av[ac], XmNbackground, pixel);
7620 ++ac;
7621 }
7622
7623 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7624 scroll_bar_name, av, ac);
7625
7626 /* Add one callback for everything that can happen. */
7627 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7628 (XtPointer) bar);
7629 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7630 (XtPointer) bar);
7631 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7632 (XtPointer) bar);
7633 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7634 (XtPointer) bar);
7635 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7636 (XtPointer) bar);
7637 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7638 (XtPointer) bar);
7639 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7640 (XtPointer) bar);
7641
7642 /* Realize the widget. Only after that is the X window created. */
7643 XtRealizeWidget (widget);
7644
7645 /* Set the cursor to an arrow. I didn't find a resource to do that.
7646 And I'm wondering why it hasn't an arrow cursor by default. */
7647 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7648 f->output_data.x->nontext_cursor);
7649
ec18280f 7650#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7651
7652 /* Set resources. Create the widget. The background of the
7653 Xaw3d scroll bar widget is a little bit light for my taste.
7654 We don't alter it here to let users change it according
7655 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7656 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7657 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7658 /* For smoother scrolling with Xaw3d -sm */
7659 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7660 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7661
7662 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7663 if (pixel != -1)
7664 {
7665 XtSetArg (av[ac], XtNforeground, pixel);
7666 ++ac;
7667 }
7668
7669 pixel = f->output_data.x->scroll_bar_background_pixel;
7670 if (pixel != -1)
7671 {
7672 XtSetArg (av[ac], XtNbackground, pixel);
7673 ++ac;
7674 }
7675
7676 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7677 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7678
7679 {
7680 char *initial = "";
7681 char *val = initial;
7682 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7683 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7684 if (val == initial)
7685 { /* ARROW_SCROLL */
7686 xaw3d_arrow_scroll = True;
7687 /* Isn't that just a personal preference ? -sm */
7688 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7689 }
7690 }
06a2c219
GM
7691
7692 /* Define callbacks. */
ec18280f
SM
7693 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7694 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7695 (XtPointer) bar);
7696
7697 /* Realize the widget. Only after that is the X window created. */
7698 XtRealizeWidget (widget);
7699
ec18280f 7700#endif /* !USE_MOTIF */
06a2c219
GM
7701
7702 /* Install an action hook that let's us detect when the user
7703 finishes interacting with a scroll bar. */
7704 if (action_hook_id == 0)
7705 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7706
7707 /* Remember X window and widget in the scroll bar vector. */
7708 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7709 xwindow = XtWindow (widget);
7710 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7711
7712 UNBLOCK_INPUT;
7713}
7714
7715
7716/* Set the thumb size and position of scroll bar BAR. We are currently
7717 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7718
7719static void
7720x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7721 struct scroll_bar *bar;
7722 int portion, position, whole;
f451eb13 7723{
06a2c219 7724 float top, shown;
06a2c219 7725 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7726
06a2c219
GM
7727 if (whole == 0)
7728 top = 0, shown = 1;
7729 else
f451eb13 7730 {
06a2c219
GM
7731 top = (float) position / whole;
7732 shown = (float) portion / whole;
7733 }
f451eb13 7734
06a2c219 7735 BLOCK_INPUT;
f451eb13 7736
06a2c219
GM
7737#ifdef USE_MOTIF
7738 {
7739 int size, value;
7740 Boolean arrow1_selected, arrow2_selected;
7741 unsigned char flags;
7742 XmScrollBarWidget sb;
7743
ec18280f 7744 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7745 is the scroll bar's maximum and MIN is the scroll bar's minimum
7746 value. */
7747 size = shown * XM_SB_RANGE;
7748 size = min (size, XM_SB_RANGE);
7749 size = max (size, 1);
7750
7751 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7752 value = top * XM_SB_RANGE;
7753 value = min (value, XM_SB_MAX - size);
7754 value = max (value, XM_SB_MIN);
7755
7756 /* LessTif: Calling XmScrollBarSetValues after an increment or
7757 decrement turns off auto-repeat LessTif-internally. This can
7758 be seen in ScrollBar.c which resets Arrow1Selected and
7759 Arrow2Selected. It also sets internal flags so that LessTif
7760 believes the mouse is in the slider. We either have to change
7761 our code, or work around that by accessing private data. */
7762
7763 sb = (XmScrollBarWidget) widget;
7764 arrow1_selected = sb->scrollBar.arrow1_selected;
7765 arrow2_selected = sb->scrollBar.arrow2_selected;
7766 flags = sb->scrollBar.flags;
7767
7768 if (NILP (bar->dragging))
7769 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7770 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7771 /* This has the negative side effect that the slider value is
ec18280f 7772 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7773 page-wise movement. */
7774 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7775 else
7776 {
7777 /* If currently dragging, only update the slider size.
7778 This reduces flicker effects. */
7779 int old_value, old_size, increment, page_increment;
7780
7781 XmScrollBarGetValues (widget, &old_value, &old_size,
7782 &increment, &page_increment);
7783 XmScrollBarSetValues (widget, old_value,
7784 min (size, XM_SB_RANGE - old_value),
7785 0, 0, False);
7786 }
7787
7788 sb->scrollBar.arrow1_selected = arrow1_selected;
7789 sb->scrollBar.arrow2_selected = arrow2_selected;
7790 sb->scrollBar.flags = flags;
7791 }
ec18280f 7792#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7793 {
ec18280f
SM
7794 float old_top, old_shown;
7795 Dimension height;
7796 XtVaGetValues (widget,
7797 XtNtopOfThumb, &old_top,
7798 XtNshown, &old_shown,
7799 XtNheight, &height,
7800 NULL);
7801
7802 /* Massage the top+shown values. */
7803 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7804 top = max (0, min (1, top));
7805 else
7806 top = old_top;
7807 /* Keep two pixels available for moving the thumb down. */
7808 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7809
7810 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7811 check that your system's configuration file contains a define
7812 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7813 if (top != old_top || shown != old_shown)
eb393530 7814 {
ec18280f 7815 if (NILP (bar->dragging))
eb393530 7816 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7817 else
7818 {
ec18280f
SM
7819#ifdef HAVE_XAW3D
7820 ScrollbarWidget sb = (ScrollbarWidget) widget;
7821 int scroll_mode;
7822
7823 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7824 if (xaw3d_arrow_scroll)
7825 {
7826 /* Xaw3d stupidly ignores resize requests while dragging
7827 so we have to make it believe it's not in dragging mode. */
7828 scroll_mode = sb->scrollbar.scroll_mode;
7829 if (scroll_mode == 2)
7830 sb->scrollbar.scroll_mode = 0;
7831 }
7832#endif
7833 /* Try to make the scrolling a tad smoother. */
7834 if (!xaw3d_pick_top)
7835 shown = min (shown, old_shown);
7836
7837 XawScrollbarSetThumb (widget, top, shown);
7838
7839#ifdef HAVE_XAW3D
7840 if (xaw3d_arrow_scroll && scroll_mode == 2)
7841 sb->scrollbar.scroll_mode = scroll_mode;
7842#endif
06a2c219 7843 }
06a2c219
GM
7844 }
7845 }
ec18280f 7846#endif /* !USE_MOTIF */
06a2c219
GM
7847
7848 UNBLOCK_INPUT;
f451eb13
JB
7849}
7850
06a2c219
GM
7851#endif /* USE_TOOLKIT_SCROLL_BARS */
7852
7853
7854\f
7855/************************************************************************
7856 Scroll bars, general
7857 ************************************************************************/
7858
7859/* Create a scroll bar and return the scroll bar vector for it. W is
7860 the Emacs window on which to create the scroll bar. TOP, LEFT,
7861 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7862 scroll bar. */
7863
ab648270 7864static struct scroll_bar *
06a2c219
GM
7865x_scroll_bar_create (w, top, left, width, height)
7866 struct window *w;
f451eb13
JB
7867 int top, left, width, height;
7868{
06a2c219 7869 struct frame *f = XFRAME (w->frame);
334208b7
RS
7870 struct scroll_bar *bar
7871 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7872
7873 BLOCK_INPUT;
7874
06a2c219
GM
7875#if USE_TOOLKIT_SCROLL_BARS
7876 x_create_toolkit_scroll_bar (f, bar);
7877#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7878 {
7879 XSetWindowAttributes a;
7880 unsigned long mask;
5c187dee 7881 Window window;
06a2c219
GM
7882
7883 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7884 if (a.background_pixel == -1)
7885 a.background_pixel = f->output_data.x->background_pixel;
7886
12ba150f 7887 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7888 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7889 | ExposureMask);
7a13e894 7890 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7891
dbc4e1c1 7892 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7893
06a2c219
GM
7894 /* Clear the area of W that will serve as a scroll bar. This is
7895 for the case that a window has been split horizontally. In
7896 this case, no clear_frame is generated to reduce flickering. */
7897 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7898 left, top, width,
7899 window_box_height (w), False);
7900
7901 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7902 /* Position and size of scroll bar. */
7903 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7904 top,
7905 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7906 height,
7907 /* Border width, depth, class, and visual. */
7908 0,
7909 CopyFromParent,
7910 CopyFromParent,
7911 CopyFromParent,
7912 /* Attributes. */
7913 mask, &a);
7914 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7915 }
06a2c219 7916#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7917
06a2c219 7918 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7919 XSETINT (bar->top, top);
7920 XSETINT (bar->left, left);
7921 XSETINT (bar->width, width);
7922 XSETINT (bar->height, height);
7923 XSETINT (bar->start, 0);
7924 XSETINT (bar->end, 0);
12ba150f 7925 bar->dragging = Qnil;
f451eb13
JB
7926
7927 /* Add bar to its frame's list of scroll bars. */
334208b7 7928 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7929 bar->prev = Qnil;
334208b7 7930 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7931 if (!NILP (bar->next))
e0c1aef2 7932 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7933
06a2c219
GM
7934 /* Map the window/widget. */
7935#if USE_TOOLKIT_SCROLL_BARS
7936 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7937 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7938 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7939 top,
7940 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7941 height, 0);
7942#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7943 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7944#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7945
7946 UNBLOCK_INPUT;
12ba150f 7947 return bar;
f451eb13
JB
7948}
7949
06a2c219 7950
12ba150f 7951/* Draw BAR's handle in the proper position.
06a2c219 7952
12ba150f
JB
7953 If the handle is already drawn from START to END, don't bother
7954 redrawing it, unless REBUILD is non-zero; in that case, always
7955 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7956 events.)
12ba150f
JB
7957
7958 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7959 fit inside its rectangle, but if the user is dragging the scroll
7960 bar handle, we want to let them drag it down all the way, so that
7961 the bar's top is as far down as it goes; otherwise, there's no way
7962 to move to the very end of the buffer. */
7963
5c187dee
GM
7964#ifndef USE_TOOLKIT_SCROLL_BARS
7965
f451eb13 7966static void
ab648270
JB
7967x_scroll_bar_set_handle (bar, start, end, rebuild)
7968 struct scroll_bar *bar;
f451eb13 7969 int start, end;
12ba150f 7970 int rebuild;
f451eb13 7971{
12ba150f 7972 int dragging = ! NILP (bar->dragging);
ab648270 7973 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7974 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7975 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7976
7977 /* If the display is already accurate, do nothing. */
7978 if (! rebuild
7979 && start == XINT (bar->start)
7980 && end == XINT (bar->end))
7981 return;
7982
f451eb13
JB
7983 BLOCK_INPUT;
7984
7985 {
d9cdbb3d
RS
7986 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7987 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7988 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
7989
7990 /* Make sure the values are reasonable, and try to preserve
7991 the distance between start and end. */
12ba150f
JB
7992 {
7993 int length = end - start;
7994
7995 if (start < 0)
7996 start = 0;
7997 else if (start > top_range)
7998 start = top_range;
7999 end = start + length;
8000
8001 if (end < start)
8002 end = start;
8003 else if (end > top_range && ! dragging)
8004 end = top_range;
8005 }
f451eb13 8006
ab648270 8007 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8008 XSETINT (bar->start, start);
8009 XSETINT (bar->end, end);
f451eb13 8010
12ba150f
JB
8011 /* Clip the end position, just for display. */
8012 if (end > top_range)
8013 end = top_range;
f451eb13 8014
ab648270 8015 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8016 below top positions, to make sure the handle is always at least
8017 that many pixels tall. */
ab648270 8018 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8019
12ba150f
JB
8020 /* Draw the empty space above the handle. Note that we can't clear
8021 zero-height areas; that means "clear to end of window." */
8022 if (0 < start)
334208b7 8023 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8024
12ba150f 8025 /* x, y, width, height, and exposures. */
ab648270
JB
8026 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8027 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8028 inside_width, start,
8029 False);
f451eb13 8030
06a2c219
GM
8031 /* Change to proper foreground color if one is specified. */
8032 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8033 XSetForeground (FRAME_X_DISPLAY (f), gc,
8034 f->output_data.x->scroll_bar_foreground_pixel);
8035
12ba150f 8036 /* Draw the handle itself. */
334208b7 8037 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8038
12ba150f 8039 /* x, y, width, height */
ab648270
JB
8040 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8041 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8042 inside_width, end - start);
f451eb13 8043
06a2c219
GM
8044 /* Restore the foreground color of the GC if we changed it above. */
8045 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8046 XSetForeground (FRAME_X_DISPLAY (f), gc,
8047 f->output_data.x->foreground_pixel);
f451eb13 8048
12ba150f
JB
8049 /* Draw the empty space below the handle. Note that we can't
8050 clear zero-height areas; that means "clear to end of window." */
8051 if (end < inside_height)
334208b7 8052 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8053
12ba150f 8054 /* x, y, width, height, and exposures. */
ab648270
JB
8055 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8056 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8057 inside_width, inside_height - end,
8058 False);
f451eb13 8059
f451eb13
JB
8060 }
8061
f451eb13
JB
8062 UNBLOCK_INPUT;
8063}
8064
5c187dee 8065#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8066
06a2c219
GM
8067/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8068 nil. */
58769bee 8069
12ba150f 8070static void
ab648270
JB
8071x_scroll_bar_remove (bar)
8072 struct scroll_bar *bar;
12ba150f 8073{
12ba150f
JB
8074 BLOCK_INPUT;
8075
06a2c219
GM
8076#if USE_TOOLKIT_SCROLL_BARS
8077 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8078#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8079 {
8080 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8081 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8082 }
06a2c219
GM
8083#endif /* not USE_TOOLKIT_SCROLL_BARS */
8084
ab648270
JB
8085 /* Disassociate this scroll bar from its window. */
8086 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8087
8088 UNBLOCK_INPUT;
8089}
8090
06a2c219 8091
12ba150f
JB
8092/* Set the handle of the vertical scroll bar for WINDOW to indicate
8093 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8094 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8095 create one. */
06a2c219 8096
12ba150f 8097static void
06a2c219
GM
8098XTset_vertical_scroll_bar (w, portion, whole, position)
8099 struct window *w;
f451eb13
JB
8100 int portion, whole, position;
8101{
06a2c219 8102 struct frame *f = XFRAME (w->frame);
ab648270 8103 struct scroll_bar *bar;
3c6ede7b 8104 int top, height, left, sb_left, width, sb_width;
06a2c219 8105 int window_x, window_y, window_width, window_height;
06a2c219 8106
3c6ede7b 8107 /* Get window dimensions. */
06a2c219 8108 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8109 top = window_y;
8110 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8111 height = window_height;
06a2c219 8112
3c6ede7b 8113 /* Compute the left edge of the scroll bar area. */
06a2c219 8114 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8115 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8116 else
8117 left = XFASTINT (w->left);
8118 left *= CANON_X_UNIT (f);
8119 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8120
8121 /* Compute the width of the scroll bar which might be less than
8122 the width of the area reserved for the scroll bar. */
8123 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8124 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8125 else
3c6ede7b 8126 sb_width = width;
12ba150f 8127
3c6ede7b
GM
8128 /* Compute the left edge of the scroll bar. */
8129#ifdef USE_TOOLKIT_SCROLL_BARS
8130 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8131 sb_left = left + width - sb_width - (width - sb_width) / 2;
8132 else
8133 sb_left = left + (width - sb_width) / 2;
8134#else
8135 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8136 sb_left = left + width - sb_width;
8137 else
8138 sb_left = left;
8139#endif
8140
ab648270 8141 /* Does the scroll bar exist yet? */
06a2c219 8142 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8143 {
80c32bcc 8144 BLOCK_INPUT;
3c6ede7b
GM
8145 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8146 left, top, width, height, False);
80c32bcc 8147 UNBLOCK_INPUT;
3c6ede7b
GM
8148 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8149 }
f451eb13 8150 else
12ba150f
JB
8151 {
8152 /* It may just need to be moved and resized. */
06a2c219
GM
8153 unsigned int mask = 0;
8154
8155 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8156
8157 BLOCK_INPUT;
8158
3c6ede7b 8159 if (sb_left != XINT (bar->left))
06a2c219 8160 mask |= CWX;
3c6ede7b 8161 if (top != XINT (bar->top))
06a2c219 8162 mask |= CWY;
3c6ede7b 8163 if (sb_width != XINT (bar->width))
06a2c219 8164 mask |= CWWidth;
3c6ede7b 8165 if (height != XINT (bar->height))
06a2c219
GM
8166 mask |= CWHeight;
8167
8168#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8169
8170 /* Since toolkit scroll bars are smaller than the space reserved
8171 for them on the frame, we have to clear "under" them. */
8172 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8173 left, top, width, height, False);
06a2c219
GM
8174
8175 /* Move/size the scroll bar widget. */
8176 if (mask)
8177 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8178 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8179 top,
8180 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8181 height, 0);
06a2c219
GM
8182
8183#else /* not USE_TOOLKIT_SCROLL_BARS */
8184
e1f6572f
RS
8185 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8186 {
8187 /* Clear areas not covered by the scroll bar. This makes sure a
8188 previous mode line display is cleared after C-x 2 C-x 1, for
8189 example. Non-toolkit scroll bars are as wide as the area
8190 reserved for scroll bars - trim at both sides. */
8191 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8192 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8193 height, False);
8194 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8195 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8196 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8197 height, False);
8198 }
06a2c219
GM
8199
8200 /* Move/size the scroll bar window. */
8201 if (mask)
8202 {
8203 XWindowChanges wc;
8204
3c6ede7b
GM
8205 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8206 wc.y = top;
8207 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8208 wc.height = height;
06a2c219
GM
8209 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8210 mask, &wc);
8211 }
8212
8213#endif /* not USE_TOOLKIT_SCROLL_BARS */
8214
8215 /* Remember new settings. */
3c6ede7b
GM
8216 XSETINT (bar->left, sb_left);
8217 XSETINT (bar->top, top);
8218 XSETINT (bar->width, sb_width);
8219 XSETINT (bar->height, height);
06a2c219
GM
8220
8221 UNBLOCK_INPUT;
12ba150f 8222 }
f451eb13 8223
06a2c219
GM
8224#if USE_TOOLKIT_SCROLL_BARS
8225 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8226#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8227 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8228 dragged. */
12ba150f 8229 if (NILP (bar->dragging))
f451eb13 8230 {
92857db0 8231 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8232
12ba150f 8233 if (whole == 0)
ab648270 8234 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8235 else
8236 {
43f868f5
JB
8237 int start = ((double) position * top_range) / whole;
8238 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8239 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8240 }
f451eb13 8241 }
06a2c219 8242#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8243
06a2c219 8244 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8245}
8246
12ba150f 8247
f451eb13 8248/* The following three hooks are used when we're doing a thorough
ab648270 8249 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8250 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8251 away is a real pain - "Can you say set-window-configuration, boys
8252 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8253 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8254 from the fiery pit when we actually redisplay its window. */
f451eb13 8255
ab648270
JB
8256/* Arrange for all scroll bars on FRAME to be removed at the next call
8257 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8258 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8259
58769bee 8260static void
ab648270 8261XTcondemn_scroll_bars (frame)
f451eb13
JB
8262 FRAME_PTR frame;
8263{
f9e24cb9
RS
8264 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8265 while (! NILP (FRAME_SCROLL_BARS (frame)))
8266 {
8267 Lisp_Object bar;
8268 bar = FRAME_SCROLL_BARS (frame);
8269 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8270 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8271 XSCROLL_BAR (bar)->prev = Qnil;
8272 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8273 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8274 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8275 }
f451eb13
JB
8276}
8277
06a2c219 8278/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8279 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8280static void
ab648270 8281XTredeem_scroll_bar (window)
12ba150f 8282 struct window *window;
f451eb13 8283{
ab648270 8284 struct scroll_bar *bar;
12ba150f 8285
ab648270
JB
8286 /* We can't redeem this window's scroll bar if it doesn't have one. */
8287 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8288 abort ();
8289
ab648270 8290 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8291
8292 /* Unlink it from the condemned list. */
8293 {
8294 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8295
8296 if (NILP (bar->prev))
8297 {
8298 /* If the prev pointer is nil, it must be the first in one of
8299 the lists. */
ab648270 8300 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8301 /* It's not condemned. Everything's fine. */
8302 return;
ab648270
JB
8303 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8304 window->vertical_scroll_bar))
8305 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8306 else
8307 /* If its prev pointer is nil, it must be at the front of
8308 one or the other! */
8309 abort ();
8310 }
8311 else
ab648270 8312 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8313
8314 if (! NILP (bar->next))
ab648270 8315 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8316
ab648270 8317 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8318 bar->prev = Qnil;
e0c1aef2 8319 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8320 if (! NILP (bar->next))
e0c1aef2 8321 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8322 }
f451eb13
JB
8323}
8324
ab648270
JB
8325/* Remove all scroll bars on FRAME that haven't been saved since the
8326 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8327
f451eb13 8328static void
ab648270 8329XTjudge_scroll_bars (f)
12ba150f 8330 FRAME_PTR f;
f451eb13 8331{
12ba150f 8332 Lisp_Object bar, next;
f451eb13 8333
ab648270 8334 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8335
8336 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8337 more events on the hapless scroll bars. */
8338 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8339
8340 for (; ! NILP (bar); bar = next)
f451eb13 8341 {
ab648270 8342 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8343
ab648270 8344 x_scroll_bar_remove (b);
12ba150f
JB
8345
8346 next = b->next;
8347 b->next = b->prev = Qnil;
f451eb13 8348 }
12ba150f 8349
ab648270 8350 /* Now there should be no references to the condemned scroll bars,
12ba150f 8351 and they should get garbage-collected. */
f451eb13
JB
8352}
8353
8354
06a2c219
GM
8355/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8356 is a no-op when using toolkit scroll bars.
ab648270
JB
8357
8358 This may be called from a signal handler, so we have to ignore GC
8359 mark bits. */
06a2c219 8360
f451eb13 8361static void
ab648270
JB
8362x_scroll_bar_expose (bar, event)
8363 struct scroll_bar *bar;
f451eb13
JB
8364 XEvent *event;
8365{
06a2c219
GM
8366#ifndef USE_TOOLKIT_SCROLL_BARS
8367
ab648270 8368 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8369 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8370 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8371 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8372
f451eb13
JB
8373 BLOCK_INPUT;
8374
ab648270 8375 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8376
06a2c219 8377 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8378 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8379
8380 /* x, y, width, height */
d9cdbb3d 8381 0, 0,
3cbd2e0b 8382 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8383 XINT (bar->height) - 1);
8384
f451eb13 8385 UNBLOCK_INPUT;
06a2c219
GM
8386
8387#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8388}
8389
ab648270
JB
8390/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8391 is set to something other than no_event, it is enqueued.
8392
8393 This may be called from a signal handler, so we have to ignore GC
8394 mark bits. */
06a2c219 8395
5c187dee
GM
8396#ifndef USE_TOOLKIT_SCROLL_BARS
8397
f451eb13 8398static void
ab648270
JB
8399x_scroll_bar_handle_click (bar, event, emacs_event)
8400 struct scroll_bar *bar;
f451eb13
JB
8401 XEvent *event;
8402 struct input_event *emacs_event;
8403{
0299d313 8404 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8405 abort ();
8406
ab648270 8407 emacs_event->kind = scroll_bar_click;
69388238 8408 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8409 emacs_event->modifiers
8410 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8411 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8412 event->xbutton.state)
8413 | (event->type == ButtonRelease
8414 ? up_modifier
8415 : down_modifier));
12ba150f 8416 emacs_event->frame_or_window = bar->window;
f451eb13 8417 emacs_event->timestamp = event->xbutton.time;
12ba150f 8418 {
06a2c219 8419#if 0
d9cdbb3d 8420 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8421 int internal_height
d9cdbb3d 8422 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8423#endif
0299d313 8424 int top_range
d9cdbb3d 8425 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8426 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8427
8428 if (y < 0) y = 0;
8429 if (y > top_range) y = top_range;
8430
8431 if (y < XINT (bar->start))
ab648270
JB
8432 emacs_event->part = scroll_bar_above_handle;
8433 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8434 emacs_event->part = scroll_bar_handle;
12ba150f 8435 else
ab648270 8436 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8437
8438 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8439 they want to drag it. Lisp code needs to be able to decide
8440 whether or not we're dragging. */
929787e1 8441#if 0
12ba150f
JB
8442 /* If the user has just clicked on the handle, record where they're
8443 holding it. */
8444 if (event->type == ButtonPress
ab648270 8445 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8446 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8447#endif
12ba150f
JB
8448
8449 /* If the user has released the handle, set it to its final position. */
8450 if (event->type == ButtonRelease
8451 && ! NILP (bar->dragging))
8452 {
8453 int new_start = y - XINT (bar->dragging);
8454 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8455
ab648270 8456 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8457 bar->dragging = Qnil;
8458 }
f451eb13 8459
5116f055
JB
8460 /* Same deal here as the other #if 0. */
8461#if 0
58769bee 8462 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8463 the handle. */
ab648270 8464 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8465 emacs_event->x = bar->start;
8466 else
e0c1aef2 8467 XSETINT (emacs_event->x, y);
5116f055 8468#else
e0c1aef2 8469 XSETINT (emacs_event->x, y);
5116f055 8470#endif
f451eb13 8471
e0c1aef2 8472 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8473 }
8474}
f451eb13 8475
ab648270
JB
8476/* Handle some mouse motion while someone is dragging the scroll bar.
8477
8478 This may be called from a signal handler, so we have to ignore GC
8479 mark bits. */
06a2c219 8480
f451eb13 8481static void
ab648270
JB
8482x_scroll_bar_note_movement (bar, event)
8483 struct scroll_bar *bar;
f451eb13
JB
8484 XEvent *event;
8485{
39d8bb4d
KH
8486 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8487
f451eb13
JB
8488 last_mouse_movement_time = event->xmotion.time;
8489
39d8bb4d 8490 f->mouse_moved = 1;
e0c1aef2 8491 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8492
8493 /* If we're dragging the bar, display it. */
ab648270 8494 if (! GC_NILP (bar->dragging))
f451eb13
JB
8495 {
8496 /* Where should the handle be now? */
12ba150f 8497 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8498
12ba150f 8499 if (new_start != XINT (bar->start))
f451eb13 8500 {
12ba150f 8501 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8502
ab648270 8503 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8504 }
8505 }
f451eb13
JB
8506}
8507
5c187dee
GM
8508#endif /* !USE_TOOLKIT_SCROLL_BARS */
8509
12ba150f 8510/* Return information to the user about the current position of the mouse
ab648270 8511 on the scroll bar. */
06a2c219 8512
12ba150f 8513static void
334208b7
RS
8514x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8515 FRAME_PTR *fp;
12ba150f 8516 Lisp_Object *bar_window;
ab648270 8517 enum scroll_bar_part *part;
12ba150f
JB
8518 Lisp_Object *x, *y;
8519 unsigned long *time;
8520{
ab648270 8521 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8522 Window w = SCROLL_BAR_X_WINDOW (bar);
8523 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8524 int win_x, win_y;
559cb2fb
JB
8525 Window dummy_window;
8526 int dummy_coord;
8527 unsigned int dummy_mask;
12ba150f 8528
cf7cb199
JB
8529 BLOCK_INPUT;
8530
ab648270 8531 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8532 report that. */
334208b7 8533 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8534
559cb2fb
JB
8535 /* Root, child, root x and root y. */
8536 &dummy_window, &dummy_window,
8537 &dummy_coord, &dummy_coord,
12ba150f 8538
559cb2fb
JB
8539 /* Position relative to scroll bar. */
8540 &win_x, &win_y,
12ba150f 8541
559cb2fb
JB
8542 /* Mouse buttons and modifier keys. */
8543 &dummy_mask))
7a13e894 8544 ;
559cb2fb
JB
8545 else
8546 {
06a2c219 8547#if 0
559cb2fb 8548 int inside_height
d9cdbb3d 8549 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8550#endif
559cb2fb 8551 int top_range
d9cdbb3d 8552 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8553
8554 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8555
8556 if (! NILP (bar->dragging))
8557 win_y -= XINT (bar->dragging);
8558
8559 if (win_y < 0)
8560 win_y = 0;
8561 if (win_y > top_range)
8562 win_y = top_range;
8563
334208b7 8564 *fp = f;
7a13e894 8565 *bar_window = bar->window;
559cb2fb
JB
8566
8567 if (! NILP (bar->dragging))
8568 *part = scroll_bar_handle;
8569 else if (win_y < XINT (bar->start))
8570 *part = scroll_bar_above_handle;
8571 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8572 *part = scroll_bar_handle;
8573 else
8574 *part = scroll_bar_below_handle;
12ba150f 8575
e0c1aef2
KH
8576 XSETINT (*x, win_y);
8577 XSETINT (*y, top_range);
12ba150f 8578
39d8bb4d 8579 f->mouse_moved = 0;
559cb2fb
JB
8580 last_mouse_scroll_bar = Qnil;
8581 }
12ba150f 8582
559cb2fb 8583 *time = last_mouse_movement_time;
cf7cb199 8584
cf7cb199 8585 UNBLOCK_INPUT;
12ba150f
JB
8586}
8587
f451eb13 8588
dbc4e1c1 8589/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8590 background colors, and the scroll bars may need to be redrawn.
8591 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8592 redraw them. */
8593
dfcf069d 8594void
ab648270 8595x_scroll_bar_clear (f)
dbc4e1c1
JB
8596 FRAME_PTR f;
8597{
06a2c219 8598#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8599 Lisp_Object bar;
8600
b80c363e
RS
8601 /* We can have scroll bars even if this is 0,
8602 if we just turned off scroll bar mode.
8603 But in that case we should not clear them. */
8604 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8605 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8606 bar = XSCROLL_BAR (bar)->next)
8607 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8608 0, 0, 0, 0, True);
06a2c219 8609#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8610}
8611
06a2c219 8612/* This processes Expose events from the menu-bar specific X event
19126e11 8613 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8614 when handling menu-bar or pop-up items. */
3afe33e7 8615
06a2c219 8616int
3afe33e7
RS
8617process_expose_from_menu (event)
8618 XEvent event;
8619{
8620 FRAME_PTR f;
19126e11 8621 struct x_display_info *dpyinfo;
06a2c219 8622 int frame_exposed_p = 0;
3afe33e7 8623
f94397b5
KH
8624 BLOCK_INPUT;
8625
19126e11
KH
8626 dpyinfo = x_display_info_for_display (event.xexpose.display);
8627 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8628 if (f)
8629 {
8630 if (f->async_visible == 0)
8631 {
8632 f->async_visible = 1;
8633 f->async_iconified = 0;
06c488fd 8634 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8635 SET_FRAME_GARBAGED (f);
8636 }
8637 else
8638 {
06a2c219
GM
8639 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8640 event.xexpose.x, event.xexpose.y,
8641 event.xexpose.width, event.xexpose.height);
8642 frame_exposed_p = 1;
3afe33e7
RS
8643 }
8644 }
8645 else
8646 {
8647 struct scroll_bar *bar
8648 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8649
3afe33e7
RS
8650 if (bar)
8651 x_scroll_bar_expose (bar, &event);
8652 }
f94397b5
KH
8653
8654 UNBLOCK_INPUT;
06a2c219 8655 return frame_exposed_p;
3afe33e7 8656}
09756a85
RS
8657\f
8658/* Define a queue to save up SelectionRequest events for later handling. */
8659
8660struct selection_event_queue
8661 {
8662 XEvent event;
8663 struct selection_event_queue *next;
8664 };
8665
8666static struct selection_event_queue *queue;
8667
8668/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8669
09756a85
RS
8670static int x_queue_selection_requests;
8671
8672/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8673
09756a85 8674static void
334208b7
RS
8675x_queue_event (f, event)
8676 FRAME_PTR f;
09756a85
RS
8677 XEvent *event;
8678{
8679 struct selection_event_queue *queue_tmp
06a2c219 8680 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8681
58769bee 8682 if (queue_tmp != NULL)
09756a85
RS
8683 {
8684 queue_tmp->event = *event;
8685 queue_tmp->next = queue;
8686 queue = queue_tmp;
8687 }
8688}
8689
8690/* Take all the queued events and put them back
8691 so that they get processed afresh. */
8692
8693static void
db3906fd
RS
8694x_unqueue_events (display)
8695 Display *display;
09756a85 8696{
58769bee 8697 while (queue != NULL)
09756a85
RS
8698 {
8699 struct selection_event_queue *queue_tmp = queue;
db3906fd 8700 XPutBackEvent (display, &queue_tmp->event);
09756a85 8701 queue = queue_tmp->next;
06a2c219 8702 xfree ((char *)queue_tmp);
09756a85
RS
8703 }
8704}
8705
8706/* Start queuing SelectionRequest events. */
8707
8708void
db3906fd
RS
8709x_start_queuing_selection_requests (display)
8710 Display *display;
09756a85
RS
8711{
8712 x_queue_selection_requests++;
8713}
8714
8715/* Stop queuing SelectionRequest events. */
8716
8717void
db3906fd
RS
8718x_stop_queuing_selection_requests (display)
8719 Display *display;
09756a85
RS
8720{
8721 x_queue_selection_requests--;
db3906fd 8722 x_unqueue_events (display);
09756a85 8723}
f451eb13
JB
8724\f
8725/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8726
06a2c219 8727/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8728 but we have to put it out here, since static variables within functions
8729 sometimes don't work. */
06a2c219 8730
dc6f92b8
JB
8731static Time enter_timestamp;
8732
11edeb03 8733/* This holds the state XLookupString needs to implement dead keys
58769bee 8734 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8735 says that a portable program can't use this, but Stephen Gildea assures
8736 me that letting the compiler initialize it to zeros will work okay.
8737
8738 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8739 given for enter_time stamp, above. */
8740
11edeb03
JB
8741static XComposeStatus compose_status;
8742
10e6549c
RS
8743/* Record the last 100 characters stored
8744 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8745
2224b905
RS
8746static int temp_index;
8747static short temp_buffer[100];
10e6549c 8748
7a13e894
RS
8749/* Set this to nonzero to fake an "X I/O error"
8750 on a particular display. */
06a2c219 8751
7a13e894
RS
8752struct x_display_info *XTread_socket_fake_io_error;
8753
2224b905
RS
8754/* When we find no input here, we occasionally do a no-op command
8755 to verify that the X server is still running and we can still talk with it.
8756 We try all the open displays, one by one.
8757 This variable is used for cycling thru the displays. */
06a2c219 8758
2224b905
RS
8759static struct x_display_info *next_noop_dpyinfo;
8760
06a2c219
GM
8761#define SET_SAVED_MENU_EVENT(size) \
8762 do \
8763 { \
8764 if (f->output_data.x->saved_menu_event == 0) \
8765 f->output_data.x->saved_menu_event \
8766 = (XEvent *) xmalloc (sizeof (XEvent)); \
8767 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8768 if (numchars >= 1) \
8769 { \
8770 bufp->kind = menu_bar_activate_event; \
8771 XSETFRAME (bufp->frame_or_window, f); \
8772 bufp++; \
8773 count++; \
8774 numchars--; \
8775 } \
8776 } \
8777 while (0)
8778
8805890a 8779#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8780#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8781
dc6f92b8
JB
8782/* Read events coming from the X server.
8783 This routine is called by the SIGIO handler.
8784 We return as soon as there are no more events to be read.
8785
8786 Events representing keys are stored in buffer BUFP,
8787 which can hold up to NUMCHARS characters.
8788 We return the number of characters stored into the buffer,
8789 thus pretending to be `read'.
8790
dc6f92b8
JB
8791 EXPECTED is nonzero if the caller knows input is available. */
8792
7c5283e4 8793int
f66868ba 8794XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8795 register int sd;
8805890a
KH
8796 /* register */ struct input_event *bufp;
8797 /* register */ int numchars;
dc6f92b8
JB
8798 int expected;
8799{
8800 int count = 0;
8801 int nbytes = 0;
dc6f92b8 8802 XEvent event;
f676886a 8803 struct frame *f;
66f55a9d 8804 int event_found = 0;
334208b7 8805 struct x_display_info *dpyinfo;
6c183ba5
RS
8806#ifdef HAVE_X_I18N
8807 Status status_return;
8808#endif
dc6f92b8 8809
9ac0d9e0 8810 if (interrupt_input_blocked)
dc6f92b8 8811 {
9ac0d9e0 8812 interrupt_input_pending = 1;
dc6f92b8
JB
8813 return -1;
8814 }
8815
9ac0d9e0 8816 interrupt_input_pending = 0;
dc6f92b8 8817 BLOCK_INPUT;
c0a04927
RS
8818
8819 /* So people can tell when we have read the available input. */
8820 input_signal_count++;
8821
dc6f92b8 8822 if (numchars <= 0)
06a2c219 8823 abort (); /* Don't think this happens. */
dc6f92b8 8824
7a13e894
RS
8825 /* Find the display we are supposed to read input for.
8826 It's the one communicating on descriptor SD. */
8827 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8828 {
8829#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8830#ifdef FIOSNBIO
7a13e894
RS
8831 /* If available, Xlib uses FIOSNBIO to make the socket
8832 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8833 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8834 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8835 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8836#endif /* ! defined (FIOSNBIO) */
7a13e894 8837#endif
dc6f92b8 8838
7a13e894
RS
8839#if 0 /* This code can't be made to work, with multiple displays,
8840 and appears not to be used on any system any more.
8841 Also keyboard.c doesn't turn O_NDELAY on and off
8842 for X connections. */
dc6f92b8
JB
8843#ifndef SIGIO
8844#ifndef HAVE_SELECT
7a13e894
RS
8845 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8846 {
8847 extern int read_alarm_should_throw;
8848 read_alarm_should_throw = 1;
8849 XPeekEvent (dpyinfo->display, &event);
8850 read_alarm_should_throw = 0;
8851 }
c118dd06
JB
8852#endif /* HAVE_SELECT */
8853#endif /* SIGIO */
7a13e894 8854#endif
dc6f92b8 8855
7a13e894
RS
8856 /* For debugging, this gives a way to fake an I/O error. */
8857 if (dpyinfo == XTread_socket_fake_io_error)
8858 {
8859 XTread_socket_fake_io_error = 0;
8860 x_io_error_quitter (dpyinfo->display);
8861 }
dc6f92b8 8862
06a2c219 8863 while (XPending (dpyinfo->display))
dc6f92b8 8864 {
7a13e894 8865 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8866
8867 if (display_busy_cursor_p)
8868 {
8869 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8870 display until the next X event is read and we come
8871 here again. Setting it to 1 inhibits busy-cursor
8872 display for direct commands. */
8873 if (event.type == MotionNotify
8874 || event.type == EnterNotify
8875 || (dpyinfo->grabbed
8876 && event.type != ButtonRelease))
8877 inhibit_busy_cursor = 2;
8878 else
8879 inhibit_busy_cursor = 1;
8880 }
8881
531483fb 8882#ifdef HAVE_X_I18N
d1bc4182 8883 {
f2be1146
GM
8884 /* Filter events for the current X input method.
8885 XFilterEvent returns non-zero if the input method has
8886 consumed the event. We pass the frame's X window to
8887 XFilterEvent because that's the one for which the IC
8888 was created. */
8889 struct frame *f1
8890 = x_any_window_to_frame (dpyinfo, event.xclient.window);
8891 if (f1
8892 && FRAME_XIC (f1)
8893 && XFilterEvent (&event, FRAME_X_WINDOW (f1)))
d1bc4182
RS
8894 break;
8895 }
0cd6403b 8896#endif
7a13e894
RS
8897 event_found = 1;
8898
8899 switch (event.type)
8900 {
8901 case ClientMessage:
c047688c 8902 {
7a13e894
RS
8903 if (event.xclient.message_type
8904 == dpyinfo->Xatom_wm_protocols
8905 && event.xclient.format == 32)
c047688c 8906 {
7a13e894
RS
8907 if (event.xclient.data.l[0]
8908 == dpyinfo->Xatom_wm_take_focus)
c047688c 8909 {
8c1a6a84
RS
8910 /* Use x_any_window_to_frame because this
8911 could be the shell widget window
8912 if the frame has no title bar. */
8913 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8914#ifdef HAVE_X_I18N
8915 /* Not quite sure this is needed -pd */
8c1a6a84 8916 if (f && FRAME_XIC (f))
6c183ba5
RS
8917 XSetICFocus (FRAME_XIC (f));
8918#endif
f1da8f06
GM
8919#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8920 instructs the WM to set the input focus automatically for
8921 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8922 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8923 it has set the focus. So, XSetInputFocus below is not
8924 needed.
8925
8926 The call to XSetInputFocus below has also caused trouble. In
8927 cases where the XSetInputFocus done by the WM and the one
8928 below are temporally close (on a fast machine), the call
8929 below can generate additional FocusIn events which confuse
8930 Emacs. */
8931
bf7253f4
RS
8932 /* Since we set WM_TAKE_FOCUS, we must call
8933 XSetInputFocus explicitly. But not if f is null,
8934 since that might be an event for a deleted frame. */
7a13e894 8935 if (f)
bf7253f4
RS
8936 {
8937 Display *d = event.xclient.display;
8938 /* Catch and ignore errors, in case window has been
8939 iconified by a window manager such as GWM. */
8940 int count = x_catch_errors (d);
8941 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8942 /* The ICCCM says this is
8943 the only valid choice. */
8944 RevertToParent,
bf7253f4
RS
8945 event.xclient.data.l[1]);
8946 /* This is needed to detect the error
8947 if there is an error. */
8948 XSync (d, False);
8949 x_uncatch_errors (d, count);
8950 }
7a13e894 8951 /* Not certain about handling scroll bars here */
f1da8f06 8952#endif /* 0 */
c047688c 8953 }
7a13e894
RS
8954 else if (event.xclient.data.l[0]
8955 == dpyinfo->Xatom_wm_save_yourself)
8956 {
8957 /* Save state modify the WM_COMMAND property to
06a2c219 8958 something which can reinstate us. This notifies
7a13e894
RS
8959 the session manager, who's looking for such a
8960 PropertyNotify. Can restart processing when
06a2c219 8961 a keyboard or mouse event arrives. */
7a13e894
RS
8962 if (numchars > 0)
8963 {
19126e11
KH
8964 f = x_top_window_to_frame (dpyinfo,
8965 event.xclient.window);
7a13e894
RS
8966
8967 /* This is just so we only give real data once
8968 for a single Emacs process. */
b86bd3dd 8969 if (f == SELECTED_FRAME ())
7a13e894
RS
8970 XSetCommand (FRAME_X_DISPLAY (f),
8971 event.xclient.window,
8972 initial_argv, initial_argc);
f000f5c5 8973 else if (f)
7a13e894
RS
8974 XSetCommand (FRAME_X_DISPLAY (f),
8975 event.xclient.window,
8976 0, 0);
8977 }
8978 }
8979 else if (event.xclient.data.l[0]
8980 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8981 {
19126e11
KH
8982 struct frame *f
8983 = x_any_window_to_frame (dpyinfo,
8984 event.xclient.window);
1fb20991 8985
7a13e894
RS
8986 if (f)
8987 {
8988 if (numchars == 0)
8989 abort ();
1fb20991 8990
7a13e894
RS
8991 bufp->kind = delete_window_event;
8992 XSETFRAME (bufp->frame_or_window, f);
8993 bufp++;
8994
8995 count += 1;
8996 numchars -= 1;
8997 }
1fb20991 8998 }
c047688c 8999 }
7a13e894
RS
9000 else if (event.xclient.message_type
9001 == dpyinfo->Xatom_wm_configure_denied)
9002 {
9003 }
9004 else if (event.xclient.message_type
9005 == dpyinfo->Xatom_wm_window_moved)
9006 {
9007 int new_x, new_y;
19126e11
KH
9008 struct frame *f
9009 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9010
7a13e894
RS
9011 new_x = event.xclient.data.s[0];
9012 new_y = event.xclient.data.s[1];
1fb20991 9013
7a13e894
RS
9014 if (f)
9015 {
7556890b
RS
9016 f->output_data.x->left_pos = new_x;
9017 f->output_data.x->top_pos = new_y;
7a13e894 9018 }
1fb20991 9019 }
0fdff6bb 9020#ifdef HACK_EDITRES
7a13e894
RS
9021 else if (event.xclient.message_type
9022 == dpyinfo->Xatom_editres)
9023 {
19126e11
KH
9024 struct frame *f
9025 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9026 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9027 &event, NULL);
7a13e894 9028 }
0fdff6bb 9029#endif /* HACK_EDITRES */
06a2c219
GM
9030 else if ((event.xclient.message_type
9031 == dpyinfo->Xatom_DONE)
9032 || (event.xclient.message_type
9033 == dpyinfo->Xatom_PAGE))
9034 {
9035 /* Ghostview job completed. Kill it. We could
9036 reply with "Next" if we received "Page", but we
9037 currently never do because we are interested in
9038 images, only, which should have 1 page. */
06a2c219
GM
9039 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9040 struct frame *f
9041 = x_window_to_frame (dpyinfo, event.xclient.window);
9042 x_kill_gs_process (pixmap, f);
9043 expose_frame (f, 0, 0, 0, 0);
9044 }
9045#ifdef USE_TOOLKIT_SCROLL_BARS
9046 /* Scroll bar callbacks send a ClientMessage from which
9047 we construct an input_event. */
9048 else if (event.xclient.message_type
9049 == dpyinfo->Xatom_Scrollbar)
9050 {
f9632fc8
GM
9051 if (display_busy_cursor_p)
9052 inhibit_busy_cursor = 2;
06a2c219
GM
9053 x_scroll_bar_to_input_event (&event, bufp);
9054 ++bufp, ++count, --numchars;
9055 goto out;
9056 }
9057#endif /* USE_TOOLKIT_SCROLL_BARS */
9058 else
9059 goto OTHER;
7a13e894
RS
9060 }
9061 break;
dc6f92b8 9062
7a13e894 9063 case SelectionNotify:
3afe33e7 9064#ifdef USE_X_TOOLKIT
19126e11 9065 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9066 goto OTHER;
3afe33e7 9067#endif /* not USE_X_TOOLKIT */
dfcf069d 9068 x_handle_selection_notify (&event.xselection);
7a13e894 9069 break;
d56a553a 9070
06a2c219 9071 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9072#ifdef USE_X_TOOLKIT
19126e11 9073 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9074 goto OTHER;
3afe33e7 9075#endif /* USE_X_TOOLKIT */
7a13e894
RS
9076 {
9077 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9078
7a13e894
RS
9079 if (numchars == 0)
9080 abort ();
d56a553a 9081
7a13e894
RS
9082 bufp->kind = selection_clear_event;
9083 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9084 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9085 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9086 bufp->frame_or_window = Qnil;
7a13e894 9087 bufp++;
d56a553a 9088
7a13e894
RS
9089 count += 1;
9090 numchars -= 1;
9091 }
9092 break;
dc6f92b8 9093
06a2c219 9094 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9095#ifdef USE_X_TOOLKIT
19126e11 9096 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9097 goto OTHER;
3afe33e7 9098#endif /* USE_X_TOOLKIT */
7a13e894 9099 if (x_queue_selection_requests)
19126e11 9100 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9101 &event);
9102 else
9103 {
9104 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9105
7a13e894
RS
9106 if (numchars == 0)
9107 abort ();
9108
9109 bufp->kind = selection_request_event;
9110 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9111 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9112 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9113 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9114 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9115 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9116 bufp->frame_or_window = Qnil;
7a13e894
RS
9117 bufp++;
9118
9119 count += 1;
9120 numchars -= 1;
9121 }
9122 break;
9123
9124 case PropertyNotify:
3afe33e7 9125#ifdef USE_X_TOOLKIT
19126e11 9126 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9127 goto OTHER;
3afe33e7 9128#endif /* not USE_X_TOOLKIT */
dfcf069d 9129 x_handle_property_notify (&event.xproperty);
7a13e894 9130 break;
dc6f92b8 9131
7a13e894 9132 case ReparentNotify:
19126e11 9133 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9134 if (f)
9135 {
9136 int x, y;
7556890b 9137 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9138 x_real_positions (f, &x, &y);
7556890b
RS
9139 f->output_data.x->left_pos = x;
9140 f->output_data.x->top_pos = y;
7a13e894
RS
9141 }
9142 break;
3bd330d4 9143
7a13e894 9144 case Expose:
19126e11 9145 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9146 if (f)
dc6f92b8 9147 {
7a13e894
RS
9148 if (f->async_visible == 0)
9149 {
9150 f->async_visible = 1;
9151 f->async_iconified = 0;
06c488fd 9152 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9153 SET_FRAME_GARBAGED (f);
9154 }
9155 else
06a2c219
GM
9156 expose_frame (x_window_to_frame (dpyinfo,
9157 event.xexpose.window),
9158 event.xexpose.x, event.xexpose.y,
9159 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9160 }
9161 else
7a13e894 9162 {
06a2c219
GM
9163#ifdef USE_TOOLKIT_SCROLL_BARS
9164 /* Dispatch event to the widget. */
9165 goto OTHER;
9166#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9167 struct scroll_bar *bar
9168 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9169
7a13e894
RS
9170 if (bar)
9171 x_scroll_bar_expose (bar, &event);
3afe33e7 9172#ifdef USE_X_TOOLKIT
7a13e894
RS
9173 else
9174 goto OTHER;
3afe33e7 9175#endif /* USE_X_TOOLKIT */
06a2c219 9176#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9177 }
9178 break;
dc6f92b8 9179
7a13e894
RS
9180 case GraphicsExpose: /* This occurs when an XCopyArea's
9181 source area was obscured or not
9182 available.*/
19126e11 9183 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9184 if (f)
9185 {
06a2c219
GM
9186 expose_frame (f,
9187 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9188 event.xgraphicsexpose.width,
9189 event.xgraphicsexpose.height);
7a13e894 9190 }
3afe33e7 9191#ifdef USE_X_TOOLKIT
7a13e894
RS
9192 else
9193 goto OTHER;
3afe33e7 9194#endif /* USE_X_TOOLKIT */
7a13e894 9195 break;
dc6f92b8 9196
7a13e894 9197 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9198 source area was completely
9199 available */
7a13e894 9200 break;
dc6f92b8 9201
7a13e894 9202 case UnmapNotify:
06a2c219
GM
9203 /* Redo the mouse-highlight after the tooltip has gone. */
9204 if (event.xmap.window == tip_window)
9205 {
9206 tip_window = 0;
9207 redo_mouse_highlight ();
9208 }
9209
91ea2a7a 9210 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9211 if (f) /* F may no longer exist if
9212 the frame was deleted. */
9213 {
9214 /* While a frame is unmapped, display generation is
9215 disabled; you don't want to spend time updating a
9216 display that won't ever be seen. */
9217 f->async_visible = 0;
9218 /* We can't distinguish, from the event, whether the window
9219 has become iconified or invisible. So assume, if it
9220 was previously visible, than now it is iconified.
1aa6072f
RS
9221 But x_make_frame_invisible clears both
9222 the visible flag and the iconified flag;
9223 and that way, we know the window is not iconified now. */
7a13e894 9224 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9225 {
9226 f->async_iconified = 1;
bddd097c 9227
1aa6072f
RS
9228 bufp->kind = iconify_event;
9229 XSETFRAME (bufp->frame_or_window, f);
9230 bufp++;
9231 count++;
9232 numchars--;
9233 }
7a13e894 9234 }
7a13e894 9235 goto OTHER;
dc6f92b8 9236
7a13e894 9237 case MapNotify:
06a2c219
GM
9238 if (event.xmap.window == tip_window)
9239 /* The tooltip has been drawn already. Avoid
9240 the SET_FRAME_GARBAGED below. */
9241 goto OTHER;
9242
9243 /* We use x_top_window_to_frame because map events can
9244 come for sub-windows and they don't mean that the
9245 frame is visible. */
19126e11 9246 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9247 if (f)
9248 {
9249 f->async_visible = 1;
9250 f->async_iconified = 0;
06c488fd 9251 f->output_data.x->has_been_visible = 1;
dc6f92b8 9252
7a13e894
RS
9253 /* wait_reading_process_input will notice this and update
9254 the frame's display structures. */
9255 SET_FRAME_GARBAGED (f);
bddd097c 9256
d806e720
RS
9257 if (f->iconified)
9258 {
9259 bufp->kind = deiconify_event;
9260 XSETFRAME (bufp->frame_or_window, f);
9261 bufp++;
9262 count++;
9263 numchars--;
9264 }
e73ec6fa 9265 else if (! NILP (Vframe_list)
8e713be6 9266 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9267 /* Force a redisplay sooner or later
9268 to update the frame titles
9269 in case this is the second frame. */
9270 record_asynch_buffer_change ();
7a13e894 9271 }
7a13e894 9272 goto OTHER;
dc6f92b8 9273
7a13e894 9274 case KeyPress:
19126e11 9275 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9276
06a2c219
GM
9277#ifdef USE_MOTIF
9278 /* I couldn't find a way to prevent LessTif scroll bars
9279 from consuming key events. */
9280 if (f == 0)
9281 {
9282 Widget widget = XtWindowToWidget (dpyinfo->display,
9283 event.xkey.window);
9284 if (widget && XmIsScrollBar (widget))
9285 {
9286 widget = XtParent (widget);
9287 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9288 }
9289 }
9290#endif /* USE_MOTIF */
9291
7a13e894
RS
9292 if (f != 0)
9293 {
9294 KeySym keysym, orig_keysym;
9295 /* al%imercury@uunet.uu.net says that making this 81 instead of
9296 80 fixed a bug whereby meta chars made his Emacs hang. */
9297 unsigned char copy_buffer[81];
9298 int modifiers;
64bb1782 9299
7a13e894
RS
9300 event.xkey.state
9301 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9302 extra_keyboard_modifiers);
9303 modifiers = event.xkey.state;
3a2712f9 9304
7a13e894 9305 /* This will have to go some day... */
752a043f 9306
7a13e894
RS
9307 /* make_lispy_event turns chars into control chars.
9308 Don't do it here because XLookupString is too eager. */
9309 event.xkey.state &= ~ControlMask;
5d46f928
RS
9310 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9311 | dpyinfo->super_mod_mask
9312 | dpyinfo->hyper_mod_mask
9313 | dpyinfo->alt_mod_mask);
9314
1cf4a0d1
RS
9315 /* In case Meta is ComposeCharacter,
9316 clear its status. According to Markus Ehrnsperger
9317 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9318 this enables ComposeCharacter to work whether or
9319 not it is combined with Meta. */
9320 if (modifiers & dpyinfo->meta_mod_mask)
9321 bzero (&compose_status, sizeof (compose_status));
9322
6c183ba5
RS
9323#ifdef HAVE_X_I18N
9324 if (FRAME_XIC (f))
9325 {
6c183ba5
RS
9326 nbytes = XmbLookupString (FRAME_XIC (f),
9327 &event.xkey, copy_buffer,
9328 80, &keysym,
9329 &status_return);
1decb680
PE
9330 if (status_return == XLookupNone)
9331 break;
9332 else if (status_return == XLookupChars)
9333 keysym = NoSymbol;
9334 else if (status_return != XLookupKeySym
9335 && status_return != XLookupBoth)
9336 abort ();
6c183ba5
RS
9337 }
9338 else
9339 nbytes = XLookupString (&event.xkey, copy_buffer,
9340 80, &keysym, &compose_status);
9341#else
0299d313
RS
9342 nbytes = XLookupString (&event.xkey, copy_buffer,
9343 80, &keysym, &compose_status);
6c183ba5 9344#endif
dc6f92b8 9345
7a13e894 9346 orig_keysym = keysym;
55123275 9347
7a13e894
RS
9348 if (numchars > 1)
9349 {
9350 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9351 || keysym == XK_Delete
1097aea0 9352#ifdef XK_ISO_Left_Tab
441affdb 9353 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9354#endif
852bff8f 9355 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9356 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9357 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9358#ifdef HPUX
7a13e894
RS
9359 /* This recognizes the "extended function keys".
9360 It seems there's no cleaner way.
9361 Test IsModifierKey to avoid handling mode_switch
9362 incorrectly. */
9363 || ((unsigned) (keysym) >= XK_Select
9364 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9365#endif
9366#ifdef XK_dead_circumflex
7a13e894 9367 || orig_keysym == XK_dead_circumflex
69388238
RS
9368#endif
9369#ifdef XK_dead_grave
7a13e894 9370 || orig_keysym == XK_dead_grave
69388238
RS
9371#endif
9372#ifdef XK_dead_tilde
7a13e894 9373 || orig_keysym == XK_dead_tilde
69388238
RS
9374#endif
9375#ifdef XK_dead_diaeresis
7a13e894 9376 || orig_keysym == XK_dead_diaeresis
69388238
RS
9377#endif
9378#ifdef XK_dead_macron
7a13e894 9379 || orig_keysym == XK_dead_macron
69388238
RS
9380#endif
9381#ifdef XK_dead_degree
7a13e894 9382 || orig_keysym == XK_dead_degree
69388238
RS
9383#endif
9384#ifdef XK_dead_acute
7a13e894 9385 || orig_keysym == XK_dead_acute
69388238
RS
9386#endif
9387#ifdef XK_dead_cedilla
7a13e894 9388 || orig_keysym == XK_dead_cedilla
69388238
RS
9389#endif
9390#ifdef XK_dead_breve
7a13e894 9391 || orig_keysym == XK_dead_breve
69388238
RS
9392#endif
9393#ifdef XK_dead_ogonek
7a13e894 9394 || orig_keysym == XK_dead_ogonek
69388238
RS
9395#endif
9396#ifdef XK_dead_caron
7a13e894 9397 || orig_keysym == XK_dead_caron
69388238
RS
9398#endif
9399#ifdef XK_dead_doubleacute
7a13e894 9400 || orig_keysym == XK_dead_doubleacute
69388238
RS
9401#endif
9402#ifdef XK_dead_abovedot
7a13e894 9403 || orig_keysym == XK_dead_abovedot
c34790e0 9404#endif
7a13e894
RS
9405 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9406 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9407 /* Any "vendor-specific" key is ok. */
9408 || (orig_keysym & (1 << 28)))
9409 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9410#ifndef HAVE_X11R5
9411#ifdef XK_Mode_switch
7a13e894 9412 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9413#endif
9414#ifdef XK_Num_Lock
7a13e894 9415 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9416#endif
9417#endif /* not HAVE_X11R5 */
7a13e894 9418 ))
dc6f92b8 9419 {
10e6549c
RS
9420 if (temp_index == sizeof temp_buffer / sizeof (short))
9421 temp_index = 0;
7a13e894
RS
9422 temp_buffer[temp_index++] = keysym;
9423 bufp->kind = non_ascii_keystroke;
9424 bufp->code = keysym;
e0c1aef2 9425 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9426 bufp->modifiers
9427 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9428 modifiers);
1113d9db 9429 bufp->timestamp = event.xkey.time;
dc6f92b8 9430 bufp++;
7a13e894
RS
9431 count++;
9432 numchars--;
06a2c219
GM
9433
9434 if (display_busy_cursor_p)
9435 if (keysym != XK_Return || minibuf_level == 0)
9436 inhibit_busy_cursor = 2;
dc6f92b8 9437 }
7a13e894
RS
9438 else if (numchars > nbytes)
9439 {
9440 register int i;
9441
9442 for (i = 0; i < nbytes; i++)
9443 {
9444 if (temp_index == sizeof temp_buffer / sizeof (short))
9445 temp_index = 0;
9446 temp_buffer[temp_index++] = copy_buffer[i];
9447 bufp->kind = ascii_keystroke;
9448 bufp->code = copy_buffer[i];
9449 XSETFRAME (bufp->frame_or_window, f);
9450 bufp->modifiers
9451 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9452 modifiers);
9453 bufp->timestamp = event.xkey.time;
9454 bufp++;
9455 }
9456
9457 count += nbytes;
9458 numchars -= nbytes;
1decb680
PE
9459
9460 if (keysym == NoSymbol)
9461 break;
7a13e894
RS
9462 }
9463 else
9464 abort ();
dc6f92b8 9465 }
10e6549c
RS
9466 else
9467 abort ();
dc6f92b8 9468 }
717ca130 9469 goto OTHER;
f451eb13 9470
7a13e894 9471 /* Here's a possible interpretation of the whole
06a2c219
GM
9472 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9473 you get a FocusIn event, you have to get a FocusOut
9474 event before you relinquish the focus. If you
9475 haven't received a FocusIn event, then a mere
9476 LeaveNotify is enough to free you. */
f451eb13 9477
7a13e894 9478 case EnterNotify:
06a2c219
GM
9479 {
9480 int from_menu_bar_p = 0;
9481
9482 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9483
9484#ifdef LESSTIF_VERSION
9485 /* When clicking outside of a menu bar popup to close
9486 it, we get a FocusIn/ EnterNotify sequence of
9487 events. The flag event.xcrossing.focus is not set
9488 in the EnterNotify event of that sequence because
9489 the focus is in the menu bar,
9490 event.xcrossing.window is the frame's X window.
9491 Unconditionally setting the focus frame to null in
9492 this case is not the right thing, because no event
9493 follows that could set the focus frame to the right
9494 value.
9495
9496 This could be a LessTif bug, but I wasn't able to
9497 reproduce the behavior in a simple test program.
9498
9499 (gerd, LessTif 0.88.1). */
9500
9501 if (!event.xcrossing.focus
9502 && f
9503 && f->output_data.x->menubar_widget)
9504 {
9505 Window focus;
9506 int revert;
9507
9508 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9509 if (focus == XtWindow (f->output_data.x->menubar_widget))
9510 from_menu_bar_p = 1;
9511 }
9512#endif /* LESSTIF_VERSION */
6d4238f3 9513
06a2c219
GM
9514 if (event.xcrossing.focus || from_menu_bar_p)
9515 {
9516 /* Avoid nasty pop/raise loops. */
9517 if (f && (!(f->auto_raise)
9518 || !(f->auto_lower)
9519 || (event.xcrossing.time - enter_timestamp) > 500))
9520 {
9521 x_new_focus_frame (dpyinfo, f);
9522 enter_timestamp = event.xcrossing.time;
9523 }
9524 }
9525 else if (f == dpyinfo->x_focus_frame)
9526 x_new_focus_frame (dpyinfo, 0);
9527
9528 /* EnterNotify counts as mouse movement,
9529 so update things that depend on mouse position. */
9530 if (f && !f->output_data.x->busy_p)
9531 note_mouse_movement (f, &event.xmotion);
9532 goto OTHER;
9533 }
dc6f92b8 9534
7a13e894 9535 case FocusIn:
19126e11 9536 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9537 if (event.xfocus.detail != NotifyPointer)
0f941935 9538 dpyinfo->x_focus_event_frame = f;
7a13e894 9539 if (f)
0f941935 9540 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9541
6c183ba5
RS
9542#ifdef HAVE_X_I18N
9543 if (f && FRAME_XIC (f))
9544 XSetICFocus (FRAME_XIC (f));
9545#endif
9546
7a13e894 9547 goto OTHER;
10c5e63d 9548
7a13e894 9549 case LeaveNotify:
19126e11 9550 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9551 if (f)
10c5e63d 9552 {
06a2c219
GM
9553 Lisp_Object frame;
9554 int from_menu_bar_p = 0;
9555
7a13e894 9556 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9557 {
9558 /* If we move outside the frame, then we're
9559 certainly no longer on any text in the frame. */
9560 clear_mouse_face (dpyinfo);
9561 dpyinfo->mouse_face_mouse_frame = 0;
9562 }
9563
9564 /* Generate a nil HELP_EVENT to cancel a help-echo.
9565 Do it only if there's something to cancel.
9566 Otherwise, the startup message is cleared when
9567 the mouse leaves the frame. */
9568 if (any_help_event_p)
9569 {
9570 XSETFRAME (frame, f);
9571 bufp->kind = HELP_EVENT;
9572 bufp->frame_or_window = Fcons (frame, Qnil);
9573 ++bufp, ++count, --numchars;
9574 }
7a13e894 9575
06a2c219
GM
9576#ifdef LESSTIF_VERSION
9577 /* Please see the comment at the start of the
9578 EnterNotify case. */
9579 if (!event.xcrossing.focus
9580 && f->output_data.x->menubar_widget)
9581 {
9582 Window focus;
9583 int revert;
9584 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9585 if (focus == XtWindow (f->output_data.x->menubar_widget))
9586 from_menu_bar_p = 1;
9587 }
9588#endif /* LESSTIF_VERSION */
9589
9590 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9591 x_mouse_leave (dpyinfo);
10c5e63d 9592 else
7a13e894 9593 {
0f941935
KH
9594 if (f == dpyinfo->x_focus_event_frame)
9595 dpyinfo->x_focus_event_frame = 0;
9596 if (f == dpyinfo->x_focus_frame)
9597 x_new_focus_frame (dpyinfo, 0);
7a13e894 9598 }
10c5e63d 9599 }
7a13e894 9600 goto OTHER;
dc6f92b8 9601
7a13e894 9602 case FocusOut:
19126e11 9603 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9604 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9605 && f == dpyinfo->x_focus_event_frame)
9606 dpyinfo->x_focus_event_frame = 0;
9607 if (f && f == dpyinfo->x_focus_frame)
9608 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9609
6c183ba5
RS
9610#ifdef HAVE_X_I18N
9611 if (f && FRAME_XIC (f))
9612 XUnsetICFocus (FRAME_XIC (f));
9613#endif
9614
7a13e894 9615 goto OTHER;
dc6f92b8 9616
7a13e894 9617 case MotionNotify:
dc6f92b8 9618 {
06a2c219
GM
9619 previous_help_echo = help_echo;
9620 help_echo = Qnil;
9621
7a13e894
RS
9622 if (dpyinfo->grabbed && last_mouse_frame
9623 && FRAME_LIVE_P (last_mouse_frame))
9624 f = last_mouse_frame;
9625 else
19126e11 9626 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9627
7a13e894
RS
9628 if (f)
9629 note_mouse_movement (f, &event.xmotion);
9630 else
9631 {
e88b3c50 9632#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9633 struct scroll_bar *bar
9634 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9635
7a13e894
RS
9636 if (bar)
9637 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9638#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9639
06a2c219
GM
9640 /* If we move outside the frame, then we're
9641 certainly no longer on any text in the frame. */
7a13e894
RS
9642 clear_mouse_face (dpyinfo);
9643 }
06a2c219
GM
9644
9645 /* If the contents of the global variable help_echo
9646 has changed, generate a HELP_EVENT. */
9647 if (STRINGP (help_echo)
9648 || STRINGP (previous_help_echo))
9649 {
9650 Lisp_Object frame;
9651
9652 if (f)
9653 XSETFRAME (frame, f);
9654 else
9655 frame = Qnil;
9656
9657 any_help_event_p = 1;
9658 bufp->kind = HELP_EVENT;
9659 bufp->frame_or_window = Fcons (frame, help_echo);
9660 ++bufp, ++count, --numchars;
9661 }
9662
9663 goto OTHER;
dc6f92b8 9664 }
dc6f92b8 9665
7a13e894 9666 case ConfigureNotify:
9829ddba
RS
9667 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9668 if (f)
af395ec1 9669 {
5c187dee 9670#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9671 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9672 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9673
2d7fc7e8
RS
9674 /* In the toolkit version, change_frame_size
9675 is called by the code that handles resizing
9676 of the EmacsFrame widget. */
7a13e894 9677
7a13e894
RS
9678 /* Even if the number of character rows and columns has
9679 not changed, the font size may have changed, so we need
9680 to check the pixel dimensions as well. */
9681 if (columns != f->width
9682 || rows != f->height
7556890b
RS
9683 || event.xconfigure.width != f->output_data.x->pixel_width
9684 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9685 {
7d1e984f 9686 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9687 SET_FRAME_GARBAGED (f);
e687d06e 9688 cancel_mouse_face (f);
7a13e894 9689 }
2d7fc7e8 9690#endif
af395ec1 9691
7556890b
RS
9692 f->output_data.x->pixel_width = event.xconfigure.width;
9693 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9694
9695 /* What we have now is the position of Emacs's own window.
9696 Convert that to the position of the window manager window. */
dcb07ae9
RS
9697 x_real_positions (f, &f->output_data.x->left_pos,
9698 &f->output_data.x->top_pos);
9699
9700 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9701 {
9702 /* Since the WM decorations come below top_pos now,
9703 we must put them below top_pos in the future. */
9704 f->output_data.x->win_gravity = NorthWestGravity;
9705 x_wm_set_size_hint (f, (long) 0, 0);
9706 }
8f08dc93
KH
9707#ifdef USE_MOTIF
9708 /* Some window managers pass (0,0) as the location of
9709 the window, and the Motif event handler stores it
9710 in the emacs widget, which messes up Motif menus. */
9711 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9712 {
9713 event.xconfigure.x = f->output_data.x->widget->core.x;
9714 event.xconfigure.y = f->output_data.x->widget->core.y;
9715 }
06a2c219 9716#endif /* USE_MOTIF */
7a13e894 9717 }
2d7fc7e8 9718 goto OTHER;
dc6f92b8 9719
7a13e894
RS
9720 case ButtonPress:
9721 case ButtonRelease:
9722 {
9723 /* If we decide we want to generate an event to be seen
9724 by the rest of Emacs, we put it here. */
9725 struct input_event emacs_event;
9ea173e8 9726 int tool_bar_p = 0;
06a2c219 9727
7a13e894 9728 emacs_event.kind = no_event;
7a13e894 9729 bzero (&compose_status, sizeof (compose_status));
9b07615b 9730
06a2c219
GM
9731 if (dpyinfo->grabbed
9732 && last_mouse_frame
9f67f20b
RS
9733 && FRAME_LIVE_P (last_mouse_frame))
9734 f = last_mouse_frame;
9735 else
2224b905 9736 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9737
06a2c219
GM
9738 if (f)
9739 {
9ea173e8
GM
9740 /* Is this in the tool-bar? */
9741 if (WINDOWP (f->tool_bar_window)
9742 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9743 {
9744 Lisp_Object window;
9745 int p, x, y;
9746
9747 x = event.xbutton.x;
9748 y = event.xbutton.y;
9749
9750 /* Set x and y. */
9751 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9752 if (EQ (window, f->tool_bar_window))
06a2c219 9753 {
9ea173e8
GM
9754 x_handle_tool_bar_click (f, &event.xbutton);
9755 tool_bar_p = 1;
06a2c219
GM
9756 }
9757 }
9758
9ea173e8 9759 if (!tool_bar_p)
06a2c219
GM
9760 if (!dpyinfo->x_focus_frame
9761 || f == dpyinfo->x_focus_frame)
9762 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9763 }
9764 else
9765 {
06a2c219 9766#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9767 struct scroll_bar *bar
9768 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9769
7a13e894
RS
9770 if (bar)
9771 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9772#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9773 }
9774
9775 if (event.type == ButtonPress)
9776 {
9777 dpyinfo->grabbed |= (1 << event.xbutton.button);
9778 last_mouse_frame = f;
edad46f6
KH
9779 /* Ignore any mouse motion that happened
9780 before this event; any subsequent mouse-movement
9781 Emacs events should reflect only motion after
9782 the ButtonPress. */
a00e91cd
KH
9783 if (f != 0)
9784 f->mouse_moved = 0;
06a2c219 9785
9ea173e8
GM
9786 if (!tool_bar_p)
9787 last_tool_bar_item = -1;
06a2c219
GM
9788 if (display_busy_cursor_p)
9789 inhibit_busy_cursor = 2;
7a13e894 9790 }
3afe33e7
RS
9791 else
9792 {
7a13e894 9793 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9794 }
23faf38f 9795
7a13e894
RS
9796 if (numchars >= 1 && emacs_event.kind != no_event)
9797 {
9798 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9799 bufp++;
9800 count++;
9801 numchars--;
9802 }
3afe33e7
RS
9803
9804#ifdef USE_X_TOOLKIT
2224b905
RS
9805 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9806 /* For a down-event in the menu bar,
9807 don't pass it to Xt right now.
9808 Instead, save it away
9809 and we will pass it to Xt from kbd_buffer_get_event.
9810 That way, we can run some Lisp code first. */
91375f8f
RS
9811 if (f && event.type == ButtonPress
9812 /* Verify the event is really within the menu bar
9813 and not just sent to it due to grabbing. */
9814 && event.xbutton.x >= 0
9815 && event.xbutton.x < f->output_data.x->pixel_width
9816 && event.xbutton.y >= 0
9817 && event.xbutton.y < f->output_data.x->menubar_height
9818 && event.xbutton.same_screen)
2224b905 9819 {
8805890a 9820 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9821 XSETFRAME (last_mouse_press_frame, f);
9822 }
9823 else if (event.type == ButtonPress)
9824 {
9825 last_mouse_press_frame = Qnil;
30e671c3 9826 goto OTHER;
ce89ef46 9827 }
06a2c219 9828
2237cac9
RS
9829#ifdef USE_MOTIF /* This should do not harm for Lucid,
9830 but I am trying to be cautious. */
ce89ef46
RS
9831 else if (event.type == ButtonRelease)
9832 {
2237cac9 9833 if (!NILP (last_mouse_press_frame))
f10ded1c 9834 {
2237cac9
RS
9835 f = XFRAME (last_mouse_press_frame);
9836 if (f->output_data.x)
06a2c219 9837 SET_SAVED_BUTTON_EVENT;
f10ded1c 9838 }
06a2c219 9839 else
30e671c3 9840 goto OTHER;
2224b905 9841 }
2237cac9 9842#endif /* USE_MOTIF */
2224b905
RS
9843 else
9844 goto OTHER;
3afe33e7 9845#endif /* USE_X_TOOLKIT */
7a13e894
RS
9846 }
9847 break;
dc6f92b8 9848
7a13e894 9849 case CirculateNotify:
06a2c219
GM
9850 goto OTHER;
9851
7a13e894 9852 case CirculateRequest:
06a2c219
GM
9853 goto OTHER;
9854
9855 case VisibilityNotify:
9856 goto OTHER;
dc6f92b8 9857
7a13e894
RS
9858 case MappingNotify:
9859 /* Someone has changed the keyboard mapping - update the
9860 local cache. */
9861 switch (event.xmapping.request)
9862 {
9863 case MappingModifier:
9864 x_find_modifier_meanings (dpyinfo);
9865 /* This is meant to fall through. */
9866 case MappingKeyboard:
9867 XRefreshKeyboardMapping (&event.xmapping);
9868 }
7a13e894 9869 goto OTHER;
dc6f92b8 9870
7a13e894 9871 default:
7a13e894 9872 OTHER:
717ca130 9873#ifdef USE_X_TOOLKIT
7a13e894
RS
9874 BLOCK_INPUT;
9875 XtDispatchEvent (&event);
9876 UNBLOCK_INPUT;
3afe33e7 9877#endif /* USE_X_TOOLKIT */
7a13e894
RS
9878 break;
9879 }
dc6f92b8
JB
9880 }
9881 }
9882
06a2c219
GM
9883 out:;
9884
9a5196d0
RS
9885 /* On some systems, an X bug causes Emacs to get no more events
9886 when the window is destroyed. Detect that. (1994.) */
58769bee 9887 if (! event_found)
ef2a22d0 9888 {
ef2a22d0
RS
9889 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9890 One XNOOP in 100 loops will make Emacs terminate.
9891 B. Bretthauer, 1994 */
9892 x_noop_count++;
58769bee 9893 if (x_noop_count >= 100)
ef2a22d0
RS
9894 {
9895 x_noop_count=0;
2224b905
RS
9896
9897 if (next_noop_dpyinfo == 0)
9898 next_noop_dpyinfo = x_display_list;
9899
9900 XNoOp (next_noop_dpyinfo->display);
9901
9902 /* Each time we get here, cycle through the displays now open. */
9903 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9904 }
9905 }
502add23 9906
06a2c219 9907 /* If the focus was just given to an auto-raising frame,
0134a210 9908 raise it now. */
7a13e894 9909 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9910 if (pending_autoraise_frame)
9911 {
9912 x_raise_frame (pending_autoraise_frame);
9913 pending_autoraise_frame = 0;
9914 }
0134a210 9915
dc6f92b8
JB
9916 UNBLOCK_INPUT;
9917 return count;
9918}
06a2c219
GM
9919
9920
9921
dc6f92b8 9922\f
06a2c219
GM
9923/***********************************************************************
9924 Text Cursor
9925 ***********************************************************************/
9926
9927/* Note if the text cursor of window W has been overwritten by a
9928 drawing operation that outputs N glyphs starting at HPOS in the
9929 line given by output_cursor.vpos. N < 0 means all the rest of the
9930 line after HPOS has been written. */
9931
9932static void
9933note_overwritten_text_cursor (w, hpos, n)
9934 struct window *w;
9935 int hpos, n;
9936{
9937 if (updated_area == TEXT_AREA
9938 && output_cursor.vpos == w->phys_cursor.vpos
9939 && hpos <= w->phys_cursor.hpos
9940 && (n < 0
9941 || hpos + n > w->phys_cursor.hpos))
9942 w->phys_cursor_on_p = 0;
9943}
f451eb13
JB
9944
9945
06a2c219
GM
9946/* Set clipping for output in glyph row ROW. W is the window in which
9947 we operate. GC is the graphics context to set clipping in.
9948 WHOLE_LINE_P non-zero means include the areas used for truncation
9949 mark display and alike in the clipping rectangle.
9950
9951 ROW may be a text row or, e.g., a mode line. Text rows must be
9952 clipped to the interior of the window dedicated to text display,
9953 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9954
9955static void
06a2c219
GM
9956x_clip_to_row (w, row, gc, whole_line_p)
9957 struct window *w;
9958 struct glyph_row *row;
9959 GC gc;
9960 int whole_line_p;
dc6f92b8 9961{
06a2c219
GM
9962 struct frame *f = XFRAME (WINDOW_FRAME (w));
9963 XRectangle clip_rect;
9964 int window_x, window_y, window_width, window_height;
dc6f92b8 9965
06a2c219 9966 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9967
06a2c219
GM
9968 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9969 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9970 clip_rect.y = max (clip_rect.y, window_y);
9971 clip_rect.width = window_width;
9972 clip_rect.height = row->visible_height;
5c1aae96 9973
06a2c219
GM
9974 /* If clipping to the whole line, including trunc marks, extend
9975 the rectangle to the left and increase its width. */
9976 if (whole_line_p)
9977 {
110859fc
GM
9978 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
9979 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 9980 }
5c1aae96 9981
06a2c219 9982 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9983}
9984
06a2c219
GM
9985
9986/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
9987
9988static void
06a2c219
GM
9989x_draw_hollow_cursor (w, row)
9990 struct window *w;
9991 struct glyph_row *row;
dc6f92b8 9992{
06a2c219
GM
9993 struct frame *f = XFRAME (WINDOW_FRAME (w));
9994 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9995 Display *dpy = FRAME_X_DISPLAY (f);
9996 int x, y, wd, h;
9997 XGCValues xgcv;
9998 struct glyph *cursor_glyph;
9999 GC gc;
10000
10001 /* Compute frame-relative coordinates from window-relative
10002 coordinates. */
10003 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10004 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10005 + row->ascent - w->phys_cursor_ascent);
10006 h = row->height - 1;
10007
10008 /* Get the glyph the cursor is on. If we can't tell because
10009 the current matrix is invalid or such, give up. */
10010 cursor_glyph = get_phys_cursor_glyph (w);
10011 if (cursor_glyph == NULL)
dc6f92b8
JB
10012 return;
10013
06a2c219
GM
10014 /* Compute the width of the rectangle to draw. If on a stretch
10015 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10016 rectangle as wide as the glyph, but use a canonical character
10017 width instead. */
10018 wd = cursor_glyph->pixel_width - 1;
10019 if (cursor_glyph->type == STRETCH_GLYPH
10020 && !x_stretch_cursor_p)
10021 wd = min (CANON_X_UNIT (f), wd);
10022
10023 /* The foreground of cursor_gc is typically the same as the normal
10024 background color, which can cause the cursor box to be invisible. */
10025 xgcv.foreground = f->output_data.x->cursor_pixel;
10026 if (dpyinfo->scratch_cursor_gc)
10027 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10028 else
10029 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10030 GCForeground, &xgcv);
10031 gc = dpyinfo->scratch_cursor_gc;
10032
10033 /* Set clipping, draw the rectangle, and reset clipping again. */
10034 x_clip_to_row (w, row, gc, 0);
10035 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10036 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10037}
10038
06a2c219
GM
10039
10040/* Draw a bar cursor on window W in glyph row ROW.
10041
10042 Implementation note: One would like to draw a bar cursor with an
10043 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10044 Unfortunately, I didn't find a font yet that has this property set.
10045 --gerd. */
dc6f92b8
JB
10046
10047static void
06a2c219
GM
10048x_draw_bar_cursor (w, row)
10049 struct window *w;
10050 struct glyph_row *row;
dc6f92b8 10051{
06a2c219
GM
10052 /* If cursor hpos is out of bounds, don't draw garbage. This can
10053 happen in mini-buffer windows when switching between echo area
10054 glyphs and mini-buffer. */
10055 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10056 {
10057 struct frame *f = XFRAME (w->frame);
10058 struct glyph *cursor_glyph;
10059 GC gc;
10060 int x;
10061 unsigned long mask;
10062 XGCValues xgcv;
10063 Display *dpy;
10064 Window window;
10065
10066 cursor_glyph = get_phys_cursor_glyph (w);
10067 if (cursor_glyph == NULL)
10068 return;
10069
10070 xgcv.background = f->output_data.x->cursor_pixel;
10071 xgcv.foreground = f->output_data.x->cursor_pixel;
10072 xgcv.graphics_exposures = 0;
10073 mask = GCForeground | GCBackground | GCGraphicsExposures;
10074 dpy = FRAME_X_DISPLAY (f);
10075 window = FRAME_X_WINDOW (f);
10076 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10077
10078 if (gc)
10079 XChangeGC (dpy, gc, mask, &xgcv);
10080 else
10081 {
10082 gc = XCreateGC (dpy, window, mask, &xgcv);
10083 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10084 }
10085
10086 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10087 x_clip_to_row (w, row, gc, 0);
10088 XFillRectangle (dpy, window, gc,
10089 x,
10090 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10091 min (cursor_glyph->pixel_width,
10092 f->output_data.x->cursor_width),
10093 row->height);
10094 XSetClipMask (dpy, gc, None);
10095 }
dc6f92b8
JB
10096}
10097
06a2c219
GM
10098
10099/* Clear the cursor of window W to background color, and mark the
10100 cursor as not shown. This is used when the text where the cursor
10101 is is about to be rewritten. */
10102
dc6f92b8 10103static void
06a2c219
GM
10104x_clear_cursor (w)
10105 struct window *w;
dc6f92b8 10106{
06a2c219
GM
10107 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10108 x_update_window_cursor (w, 0);
10109}
90e65f07 10110
dbc4e1c1 10111
06a2c219
GM
10112/* Draw the cursor glyph of window W in glyph row ROW. See the
10113 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10114
06a2c219
GM
10115static void
10116x_draw_phys_cursor_glyph (w, row, hl)
10117 struct window *w;
10118 struct glyph_row *row;
10119 enum draw_glyphs_face hl;
10120{
10121 /* If cursor hpos is out of bounds, don't draw garbage. This can
10122 happen in mini-buffer windows when switching between echo area
10123 glyphs and mini-buffer. */
10124 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10125 {
10126 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10127 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10128 hl, 0, 0, 0);
10129
10130 /* When we erase the cursor, and ROW is overlapped by other
10131 rows, make sure that these overlapping parts of other rows
10132 are redrawn. */
10133 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10134 {
10135 if (row > w->current_matrix->rows
10136 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10137 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10138
10139 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10140 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10141 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10142 }
10143 }
06a2c219 10144}
dbc4e1c1 10145
eea6af04 10146
06a2c219 10147/* Erase the image of a cursor of window W from the screen. */
eea6af04 10148
06a2c219
GM
10149static void
10150x_erase_phys_cursor (w)
10151 struct window *w;
10152{
10153 struct frame *f = XFRAME (w->frame);
10154 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10155 int hpos = w->phys_cursor.hpos;
10156 int vpos = w->phys_cursor.vpos;
10157 int mouse_face_here_p = 0;
10158 struct glyph_matrix *active_glyphs = w->current_matrix;
10159 struct glyph_row *cursor_row;
10160 struct glyph *cursor_glyph;
10161 enum draw_glyphs_face hl;
10162
10163 /* No cursor displayed or row invalidated => nothing to do on the
10164 screen. */
10165 if (w->phys_cursor_type == NO_CURSOR)
10166 goto mark_cursor_off;
10167
10168 /* VPOS >= active_glyphs->nrows means that window has been resized.
10169 Don't bother to erase the cursor. */
10170 if (vpos >= active_glyphs->nrows)
10171 goto mark_cursor_off;
10172
10173 /* If row containing cursor is marked invalid, there is nothing we
10174 can do. */
10175 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10176 if (!cursor_row->enabled_p)
10177 goto mark_cursor_off;
10178
10179 /* This can happen when the new row is shorter than the old one.
10180 In this case, either x_draw_glyphs or clear_end_of_line
10181 should have cleared the cursor. Note that we wouldn't be
10182 able to erase the cursor in this case because we don't have a
10183 cursor glyph at hand. */
10184 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10185 goto mark_cursor_off;
10186
10187 /* If the cursor is in the mouse face area, redisplay that when
10188 we clear the cursor. */
10189 if (w == XWINDOW (dpyinfo->mouse_face_window)
10190 && (vpos > dpyinfo->mouse_face_beg_row
10191 || (vpos == dpyinfo->mouse_face_beg_row
10192 && hpos >= dpyinfo->mouse_face_beg_col))
10193 && (vpos < dpyinfo->mouse_face_end_row
10194 || (vpos == dpyinfo->mouse_face_end_row
10195 && hpos < dpyinfo->mouse_face_end_col))
10196 /* Don't redraw the cursor's spot in mouse face if it is at the
10197 end of a line (on a newline). The cursor appears there, but
10198 mouse highlighting does not. */
10199 && cursor_row->used[TEXT_AREA] > hpos)
10200 mouse_face_here_p = 1;
10201
10202 /* Maybe clear the display under the cursor. */
10203 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10204 {
10205 int x;
045dee35 10206 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10207
06a2c219
GM
10208 cursor_glyph = get_phys_cursor_glyph (w);
10209 if (cursor_glyph == NULL)
10210 goto mark_cursor_off;
dbc4e1c1 10211
06a2c219
GM
10212 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10213
10214 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10215 x,
045dee35 10216 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10217 cursor_row->y)),
10218 cursor_glyph->pixel_width,
10219 cursor_row->visible_height,
10220 False);
dbc4e1c1 10221 }
06a2c219
GM
10222
10223 /* Erase the cursor by redrawing the character underneath it. */
10224 if (mouse_face_here_p)
10225 hl = DRAW_MOUSE_FACE;
10226 else if (cursor_row->inverse_p)
10227 hl = DRAW_INVERSE_VIDEO;
10228 else
10229 hl = DRAW_NORMAL_TEXT;
10230 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10231
06a2c219
GM
10232 mark_cursor_off:
10233 w->phys_cursor_on_p = 0;
10234 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10235}
10236
10237
06a2c219
GM
10238/* Display or clear cursor of window W. If ON is zero, clear the
10239 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10240 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10241
06a2c219
GM
10242void
10243x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10244 struct window *w;
10245 int on, hpos, vpos, x, y;
dbc4e1c1 10246{
06a2c219
GM
10247 struct frame *f = XFRAME (w->frame);
10248 int new_cursor_type;
10249 struct glyph_matrix *current_glyphs;
10250 struct glyph_row *glyph_row;
10251 struct glyph *glyph;
dbc4e1c1 10252
49d838ea 10253 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10254 windows and frames; in the latter case, the frame or window may
10255 be in the midst of changing its size, and x and y may be off the
10256 window. */
10257 if (! FRAME_VISIBLE_P (f)
10258 || FRAME_GARBAGED_P (f)
10259 || vpos >= w->current_matrix->nrows
10260 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10261 return;
10262
10263 /* If cursor is off and we want it off, return quickly. */
06a2c219 10264 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10265 return;
10266
06a2c219
GM
10267 current_glyphs = w->current_matrix;
10268 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10269 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10270
10271 /* If cursor row is not enabled, we don't really know where to
10272 display the cursor. */
10273 if (!glyph_row->enabled_p)
10274 {
10275 w->phys_cursor_on_p = 0;
10276 return;
10277 }
10278
10279 xassert (interrupt_input_blocked);
10280
10281 /* Set new_cursor_type to the cursor we want to be displayed. In a
10282 mini-buffer window, we want the cursor only to appear if we are
10283 reading input from this window. For the selected window, we want
10284 the cursor type given by the frame parameter. If explicitly
10285 marked off, draw no cursor. In all other cases, we want a hollow
10286 box cursor. */
9b4a7047
GM
10287 if (cursor_in_echo_area
10288 && FRAME_HAS_MINIBUF_P (f)
10289 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10290 {
9b4a7047
GM
10291 if (w == XWINDOW (echo_area_window))
10292 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10293 else
10294 new_cursor_type = HOLLOW_BOX_CURSOR;
10295 }
06a2c219 10296 else
9b4a7047
GM
10297 {
10298 if (w != XWINDOW (selected_window)
10299 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10300 {
10301 if (MINI_WINDOW_P (w))
10302 new_cursor_type = NO_CURSOR;
10303 else
10304 new_cursor_type = HOLLOW_BOX_CURSOR;
10305 }
10306 else if (w->cursor_off_p)
10307 new_cursor_type = NO_CURSOR;
10308 else
10309 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10310 }
06a2c219
GM
10311
10312 /* If cursor is currently being shown and we don't want it to be or
10313 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10314 erase it. */
06a2c219 10315 if (w->phys_cursor_on_p
dc6f92b8 10316 && (!on
06a2c219
GM
10317 || w->phys_cursor.x != x
10318 || w->phys_cursor.y != y
10319 || new_cursor_type != w->phys_cursor_type))
10320 x_erase_phys_cursor (w);
10321
10322 /* If the cursor is now invisible and we want it to be visible,
10323 display it. */
10324 if (on && !w->phys_cursor_on_p)
10325 {
10326 w->phys_cursor_ascent = glyph_row->ascent;
10327 w->phys_cursor_height = glyph_row->height;
10328
10329 /* Set phys_cursor_.* before x_draw_.* is called because some
10330 of them may need the information. */
10331 w->phys_cursor.x = x;
10332 w->phys_cursor.y = glyph_row->y;
10333 w->phys_cursor.hpos = hpos;
10334 w->phys_cursor.vpos = vpos;
10335 w->phys_cursor_type = new_cursor_type;
10336 w->phys_cursor_on_p = 1;
10337
10338 switch (new_cursor_type)
dc6f92b8 10339 {
06a2c219
GM
10340 case HOLLOW_BOX_CURSOR:
10341 x_draw_hollow_cursor (w, glyph_row);
10342 break;
10343
10344 case FILLED_BOX_CURSOR:
10345 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10346 break;
10347
10348 case BAR_CURSOR:
10349 x_draw_bar_cursor (w, glyph_row);
10350 break;
10351
10352 case NO_CURSOR:
10353 break;
dc6f92b8 10354
06a2c219
GM
10355 default:
10356 abort ();
10357 }
dc6f92b8
JB
10358 }
10359
06a2c219 10360#ifndef XFlush
f676886a 10361 if (updating_frame != f)
334208b7 10362 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10363#endif
dc6f92b8
JB
10364}
10365
06a2c219
GM
10366
10367/* Display the cursor on window W, or clear it. X and Y are window
10368 relative pixel coordinates. HPOS and VPOS are glyph matrix
10369 positions. If W is not the selected window, display a hollow
10370 cursor. ON non-zero means display the cursor at X, Y which
10371 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10372
dfcf069d 10373void
06a2c219
GM
10374x_display_cursor (w, on, hpos, vpos, x, y)
10375 struct window *w;
10376 int on, hpos, vpos, x, y;
dc6f92b8 10377{
f94397b5 10378 BLOCK_INPUT;
06a2c219 10379 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10380 UNBLOCK_INPUT;
10381}
10382
06a2c219
GM
10383
10384/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10385 Don't change the cursor's position. */
10386
dfcf069d 10387void
06a2c219 10388x_update_cursor (f, on_p)
5d46f928 10389 struct frame *f;
5d46f928 10390{
06a2c219
GM
10391 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10392}
10393
10394
10395/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10396 in the window tree rooted at W. */
10397
10398static void
10399x_update_cursor_in_window_tree (w, on_p)
10400 struct window *w;
10401 int on_p;
10402{
10403 while (w)
10404 {
10405 if (!NILP (w->hchild))
10406 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10407 else if (!NILP (w->vchild))
10408 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10409 else
10410 x_update_window_cursor (w, on_p);
10411
10412 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10413 }
10414}
5d46f928 10415
f94397b5 10416
06a2c219
GM
10417/* Switch the display of W's cursor on or off, according to the value
10418 of ON. */
10419
10420static void
10421x_update_window_cursor (w, on)
10422 struct window *w;
10423 int on;
10424{
10425 BLOCK_INPUT;
10426 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10427 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10428 UNBLOCK_INPUT;
dc6f92b8 10429}
06a2c219
GM
10430
10431
10432
dc6f92b8
JB
10433\f
10434/* Icons. */
10435
f676886a 10436/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10437 when we get an expose event for it. */
dc6f92b8 10438
dfcf069d 10439void
f676886a
JB
10440refreshicon (f)
10441 struct frame *f;
dc6f92b8 10442{
06a2c219 10443 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10444}
10445
dbc4e1c1 10446/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10447
10448int
990ba854 10449x_bitmap_icon (f, file)
f676886a 10450 struct frame *f;
990ba854 10451 Lisp_Object file;
dc6f92b8 10452{
06a2c219 10453 int bitmap_id;
dc6f92b8 10454
c118dd06 10455 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10456 return 1;
10457
990ba854 10458 /* Free up our existing icon bitmap if any. */
7556890b
RS
10459 if (f->output_data.x->icon_bitmap > 0)
10460 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10461 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10462
10463 if (STRINGP (file))
7f2ae036
RS
10464 bitmap_id = x_create_bitmap_from_file (f, file);
10465 else
10466 {
990ba854 10467 /* Create the GNU bitmap if necessary. */
5bf01b68 10468 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10469 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10470 = x_create_bitmap_from_data (f, gnu_bits,
10471 gnu_width, gnu_height);
990ba854
RS
10472
10473 /* The first time we create the GNU bitmap,
06a2c219 10474 this increments the ref-count one extra time.
990ba854
RS
10475 As a result, the GNU bitmap is never freed.
10476 That way, we don't have to worry about allocating it again. */
334208b7 10477 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10478
334208b7 10479 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10480 }
10481
10482 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10483 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10484
10485 return 0;
10486}
10487
10488
1be2d067
KH
10489/* Make the x-window of frame F use a rectangle with text.
10490 Use ICON_NAME as the text. */
dc6f92b8
JB
10491
10492int
f676886a
JB
10493x_text_icon (f, icon_name)
10494 struct frame *f;
dc6f92b8
JB
10495 char *icon_name;
10496{
c118dd06 10497 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10498 return 1;
10499
1be2d067
KH
10500#ifdef HAVE_X11R4
10501 {
10502 XTextProperty text;
10503 text.value = (unsigned char *) icon_name;
10504 text.encoding = XA_STRING;
10505 text.format = 8;
10506 text.nitems = strlen (icon_name);
10507#ifdef USE_X_TOOLKIT
7556890b 10508 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10509 &text);
10510#else /* not USE_X_TOOLKIT */
10511 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10512#endif /* not USE_X_TOOLKIT */
10513 }
10514#else /* not HAVE_X11R4 */
10515 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10516#endif /* not HAVE_X11R4 */
58769bee 10517
7556890b
RS
10518 if (f->output_data.x->icon_bitmap > 0)
10519 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10520 f->output_data.x->icon_bitmap = 0;
b1c884c3 10521 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10522
10523 return 0;
10524}
10525\f
e99db5a1
RS
10526#define X_ERROR_MESSAGE_SIZE 200
10527
10528/* If non-nil, this should be a string.
10529 It means catch X errors and store the error message in this string. */
10530
10531static Lisp_Object x_error_message_string;
10532
10533/* An X error handler which stores the error message in
10534 x_error_message_string. This is called from x_error_handler if
10535 x_catch_errors is in effect. */
10536
06a2c219 10537static void
e99db5a1
RS
10538x_error_catcher (display, error)
10539 Display *display;
10540 XErrorEvent *error;
10541{
10542 XGetErrorText (display, error->error_code,
10543 XSTRING (x_error_message_string)->data,
10544 X_ERROR_MESSAGE_SIZE);
10545}
10546
10547/* Begin trapping X errors for display DPY. Actually we trap X errors
10548 for all displays, but DPY should be the display you are actually
10549 operating on.
10550
10551 After calling this function, X protocol errors no longer cause
10552 Emacs to exit; instead, they are recorded in the string
10553 stored in x_error_message_string.
10554
10555 Calling x_check_errors signals an Emacs error if an X error has
10556 occurred since the last call to x_catch_errors or x_check_errors.
10557
10558 Calling x_uncatch_errors resumes the normal error handling. */
10559
10560void x_check_errors ();
10561static Lisp_Object x_catch_errors_unwind ();
10562
10563int
10564x_catch_errors (dpy)
10565 Display *dpy;
10566{
10567 int count = specpdl_ptr - specpdl;
10568
10569 /* Make sure any errors from previous requests have been dealt with. */
10570 XSync (dpy, False);
10571
10572 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10573
10574 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10575 XSTRING (x_error_message_string)->data[0] = 0;
10576
10577 return count;
10578}
10579
10580/* Unbind the binding that we made to check for X errors. */
10581
10582static Lisp_Object
10583x_catch_errors_unwind (old_val)
10584 Lisp_Object old_val;
10585{
10586 x_error_message_string = old_val;
10587 return Qnil;
10588}
10589
10590/* If any X protocol errors have arrived since the last call to
10591 x_catch_errors or x_check_errors, signal an Emacs error using
10592 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10593
10594void
10595x_check_errors (dpy, format)
10596 Display *dpy;
10597 char *format;
10598{
10599 /* Make sure to catch any errors incurred so far. */
10600 XSync (dpy, False);
10601
10602 if (XSTRING (x_error_message_string)->data[0])
10603 error (format, XSTRING (x_error_message_string)->data);
10604}
10605
9829ddba
RS
10606/* Nonzero if we had any X protocol errors
10607 since we did x_catch_errors on DPY. */
e99db5a1
RS
10608
10609int
10610x_had_errors_p (dpy)
10611 Display *dpy;
10612{
10613 /* Make sure to catch any errors incurred so far. */
10614 XSync (dpy, False);
10615
10616 return XSTRING (x_error_message_string)->data[0] != 0;
10617}
10618
9829ddba
RS
10619/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10620
06a2c219 10621void
9829ddba
RS
10622x_clear_errors (dpy)
10623 Display *dpy;
10624{
10625 XSTRING (x_error_message_string)->data[0] = 0;
10626}
10627
e99db5a1
RS
10628/* Stop catching X protocol errors and let them make Emacs die.
10629 DPY should be the display that was passed to x_catch_errors.
10630 COUNT should be the value that was returned by
10631 the corresponding call to x_catch_errors. */
10632
10633void
10634x_uncatch_errors (dpy, count)
10635 Display *dpy;
10636 int count;
10637{
10638 unbind_to (count, Qnil);
10639}
10640
10641#if 0
10642static unsigned int x_wire_count;
10643x_trace_wire ()
10644{
10645 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10646}
10647#endif /* ! 0 */
10648
10649\f
10650/* Handle SIGPIPE, which can happen when the connection to a server
10651 simply goes away. SIGPIPE is handled by x_connection_signal.
10652 Don't need to do anything, because the write which caused the
10653 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10654 which will do the appropriate cleanup for us. */
e99db5a1
RS
10655
10656static SIGTYPE
10657x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10658 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10659{
10660#ifdef USG
10661 /* USG systems forget handlers when they are used;
10662 must reestablish each time */
10663 signal (signalnum, x_connection_signal);
10664#endif /* USG */
10665}
10666\f
4746118a
JB
10667/* Handling X errors. */
10668
7a13e894 10669/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10670
4746118a 10671static SIGTYPE
7a13e894
RS
10672x_connection_closed (display, error_message)
10673 Display *display;
10674 char *error_message;
4746118a 10675{
7a13e894
RS
10676 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10677 Lisp_Object frame, tail;
10678
6186a4a0
RS
10679 /* Indicate that this display is dead. */
10680
f613a4c8 10681#ifdef USE_X_TOOLKIT
adabc3a9 10682 XtCloseDisplay (display);
f613a4c8 10683#endif
adabc3a9 10684
6186a4a0
RS
10685 dpyinfo->display = 0;
10686
06a2c219 10687 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10688 that are on the dead display. */
10689 FOR_EACH_FRAME (tail, frame)
10690 {
10691 Lisp_Object minibuf_frame;
10692 minibuf_frame
10693 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10694 if (FRAME_X_P (XFRAME (frame))
10695 && FRAME_X_P (XFRAME (minibuf_frame))
10696 && ! EQ (frame, minibuf_frame)
10697 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10698 Fdelete_frame (frame, Qt);
10699 }
10700
10701 /* Now delete all remaining frames on the dead display.
06a2c219 10702 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10703 for another frame that we need to delete. */
10704 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10705 if (FRAME_X_P (XFRAME (frame))
10706 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10707 {
10708 /* Set this to t so that Fdelete_frame won't get confused
10709 trying to find a replacement. */
10710 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10711 Fdelete_frame (frame, Qt);
10712 }
7a13e894 10713
482a1bd2
KH
10714 if (dpyinfo)
10715 x_delete_display (dpyinfo);
7a13e894
RS
10716
10717 if (x_display_list == 0)
10718 {
f8d07b62 10719 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10720 shut_down_emacs (0, 0, Qnil);
10721 exit (70);
10722 }
12ba150f 10723
7a13e894
RS
10724 /* Ordinary stack unwind doesn't deal with these. */
10725#ifdef SIGIO
10726 sigunblock (sigmask (SIGIO));
10727#endif
10728 sigunblock (sigmask (SIGALRM));
10729 TOTALLY_UNBLOCK_INPUT;
10730
aa4d9a9e 10731 clear_waiting_for_input ();
7a13e894 10732 error ("%s", error_message);
4746118a
JB
10733}
10734
7a13e894
RS
10735/* This is the usual handler for X protocol errors.
10736 It kills all frames on the display that we got the error for.
10737 If that was the only one, it prints an error message and kills Emacs. */
10738
06a2c219 10739static void
c118dd06
JB
10740x_error_quitter (display, error)
10741 Display *display;
10742 XErrorEvent *error;
10743{
7a13e894 10744 char buf[256], buf1[356];
dc6f92b8 10745
58769bee 10746 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10747 original error handler. */
dc6f92b8 10748
c118dd06 10749 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10750 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10751 buf, error->request_code);
7a13e894 10752 x_connection_closed (display, buf1);
dc6f92b8
JB
10753}
10754
e99db5a1
RS
10755/* This is the first-level handler for X protocol errors.
10756 It calls x_error_quitter or x_error_catcher. */
7a13e894 10757
8922af5f 10758static int
e99db5a1 10759x_error_handler (display, error)
8922af5f 10760 Display *display;
e99db5a1 10761 XErrorEvent *error;
8922af5f 10762{
e99db5a1
RS
10763 if (! NILP (x_error_message_string))
10764 x_error_catcher (display, error);
10765 else
10766 x_error_quitter (display, error);
06a2c219 10767 return 0;
f9e24cb9 10768}
c118dd06 10769
e99db5a1
RS
10770/* This is the handler for X IO errors, always.
10771 It kills all frames on the display that we lost touch with.
10772 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10773
c118dd06 10774static int
e99db5a1 10775x_io_error_quitter (display)
c118dd06 10776 Display *display;
c118dd06 10777{
e99db5a1 10778 char buf[256];
dc6f92b8 10779
e99db5a1
RS
10780 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10781 x_connection_closed (display, buf);
06a2c219 10782 return 0;
dc6f92b8 10783}
dc6f92b8 10784\f
f451eb13
JB
10785/* Changing the font of the frame. */
10786
76bcdf39
RS
10787/* Give frame F the font named FONTNAME as its default font, and
10788 return the full name of that font. FONTNAME may be a wildcard
10789 pattern; in that case, we choose some font that fits the pattern.
10790 The return value shows which font we chose. */
10791
b5cf7a0e 10792Lisp_Object
f676886a
JB
10793x_new_font (f, fontname)
10794 struct frame *f;
dc6f92b8
JB
10795 register char *fontname;
10796{
dc43ef94
KH
10797 struct font_info *fontp
10798 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10799
dc43ef94
KH
10800 if (!fontp)
10801 return Qnil;
2224a5fc 10802
dc43ef94 10803 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10804 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10805 f->output_data.x->fontset = -1;
10806
b2cad826
KH
10807 /* Compute the scroll bar width in character columns. */
10808 if (f->scroll_bar_pixel_width > 0)
10809 {
7556890b 10810 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10811 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10812 }
10813 else
4e61bddf
RS
10814 {
10815 int wid = FONT_WIDTH (f->output_data.x->font);
10816 f->scroll_bar_cols = (14 + wid - 1) / wid;
10817 }
b2cad826 10818
f676886a 10819 /* Now make the frame display the given font. */
c118dd06 10820 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10821 {
7556890b
RS
10822 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10823 f->output_data.x->font->fid);
10824 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10825 f->output_data.x->font->fid);
10826 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10827 f->output_data.x->font->fid);
f676886a 10828
a27f9f86 10829 frame_update_line_height (f);
0134a210 10830 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10831 }
a27f9f86
RS
10832 else
10833 /* If we are setting a new frame's font for the first time,
10834 there are no faces yet, so this font's height is the line height. */
7556890b 10835 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10836
dc43ef94
KH
10837 return build_string (fontp->full_name);
10838}
10839
10840/* Give frame F the fontset named FONTSETNAME as its default font, and
10841 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10842 pattern; in that case, we choose some fontset that fits the pattern.
10843 The return value shows which fontset we chose. */
b5cf7a0e 10844
dc43ef94
KH
10845Lisp_Object
10846x_new_fontset (f, fontsetname)
10847 struct frame *f;
10848 char *fontsetname;
10849{
10850 int fontset = fs_query_fontset (f, fontsetname);
10851 struct fontset_info *fontsetp;
10852 Lisp_Object result;
b5cf7a0e 10853
dc43ef94
KH
10854 if (fontset < 0)
10855 return Qnil;
b5cf7a0e 10856
2da424f1
KH
10857 if (f->output_data.x->fontset == fontset)
10858 /* This fontset is already set in frame F. There's nothing more
10859 to do. */
10860 return build_string (fontsetname);
10861
dc43ef94
KH
10862 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10863
10864 if (!fontsetp->fontname[CHARSET_ASCII])
10865 /* This fontset doesn't contain ASCII font. */
10866 return Qnil;
10867
10868 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10869
10870 if (!STRINGP (result))
10871 /* Can't load ASCII font. */
10872 return Qnil;
10873
10874 /* Since x_new_font doesn't update any fontset information, do it now. */
10875 f->output_data.x->fontset = fontset;
2da424f1 10876 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10877 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94
KH
10878
10879 return build_string (fontsetname);
dc6f92b8 10880}
dc6f92b8 10881\f
2e365682
RS
10882/* Calculate the absolute position in frame F
10883 from its current recorded position values and gravity. */
10884
dfcf069d 10885void
43bca5d5 10886x_calc_absolute_position (f)
f676886a 10887 struct frame *f;
dc6f92b8 10888{
06a2c219 10889 Window child;
6dba1858 10890 int win_x = 0, win_y = 0;
7556890b 10891 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10892 int this_window;
10893
9829ddba
RS
10894 /* We have nothing to do if the current position
10895 is already for the top-left corner. */
10896 if (! ((flags & XNegative) || (flags & YNegative)))
10897 return;
10898
c81412a0 10899#ifdef USE_X_TOOLKIT
7556890b 10900 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
10901#else
10902 this_window = FRAME_X_WINDOW (f);
10903#endif
6dba1858
RS
10904
10905 /* Find the position of the outside upper-left corner of
9829ddba
RS
10906 the inner window, with respect to the outer window.
10907 But do this only if we will need the results. */
7556890b 10908 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10909 {
9829ddba
RS
10910 int count;
10911
6dba1858 10912 BLOCK_INPUT;
9829ddba
RS
10913 count = x_catch_errors (FRAME_X_DISPLAY (f));
10914 while (1)
10915 {
10916 x_clear_errors (FRAME_X_DISPLAY (f));
10917 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10918
10919 /* From-window, to-window. */
10920 this_window,
10921 f->output_data.x->parent_desc,
10922
10923 /* From-position, to-position. */
10924 0, 0, &win_x, &win_y,
10925
10926 /* Child of win. */
10927 &child);
10928 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10929 {
10930 Window newroot, newparent = 0xdeadbeef;
10931 Window *newchildren;
10932 int nchildren;
10933
10934 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10935 &newparent, &newchildren, &nchildren))
10936 break;
58769bee 10937
7c3c78a3 10938 XFree ((char *) newchildren);
6dba1858 10939
9829ddba
RS
10940 f->output_data.x->parent_desc = newparent;
10941 }
10942 else
10943 break;
10944 }
6dba1858 10945
9829ddba 10946 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10947 UNBLOCK_INPUT;
10948 }
10949
10950 /* Treat negative positions as relative to the leftmost bottommost
10951 position that fits on the screen. */
20f55f9a 10952 if (flags & XNegative)
7556890b 10953 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10954 - 2 * f->output_data.x->border_width - win_x
10955 - PIXEL_WIDTH (f)
10956 + f->output_data.x->left_pos);
dc6f92b8 10957
20f55f9a 10958 if (flags & YNegative)
06a2c219
GM
10959 {
10960 int menubar_height = 0;
10961
10962#ifdef USE_X_TOOLKIT
10963 if (f->output_data.x->menubar_widget)
10964 menubar_height
10965 = (f->output_data.x->menubar_widget->core.height
10966 + f->output_data.x->menubar_widget->core.border_width);
10967#endif
10968
10969 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10970 - 2 * f->output_data.x->border_width
10971 - win_y
10972 - PIXEL_HEIGHT (f)
10973 - menubar_height
10974 + f->output_data.x->top_pos);
10975 }
2e365682 10976
3a35ab44
RS
10977 /* The left_pos and top_pos
10978 are now relative to the top and left screen edges,
10979 so the flags should correspond. */
7556890b 10980 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10981}
10982
3a35ab44
RS
10983/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10984 to really change the position, and 0 when calling from
10985 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
10986 position values). It is -1 when calling from x_set_frame_parameters,
10987 which means, do adjust for borders but don't change the gravity. */
3a35ab44 10988
dfcf069d 10989void
dc05a16b 10990x_set_offset (f, xoff, yoff, change_gravity)
f676886a 10991 struct frame *f;
dc6f92b8 10992 register int xoff, yoff;
dc05a16b 10993 int change_gravity;
dc6f92b8 10994{
4a4cbdd5
KH
10995 int modified_top, modified_left;
10996
aa3ff7c9 10997 if (change_gravity > 0)
3a35ab44 10998 {
7556890b
RS
10999 f->output_data.x->top_pos = yoff;
11000 f->output_data.x->left_pos = xoff;
11001 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11002 if (xoff < 0)
7556890b 11003 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11004 if (yoff < 0)
7556890b
RS
11005 f->output_data.x->size_hint_flags |= YNegative;
11006 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11007 }
43bca5d5 11008 x_calc_absolute_position (f);
dc6f92b8
JB
11009
11010 BLOCK_INPUT;
c32cdd9a 11011 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11012
7556890b
RS
11013 modified_left = f->output_data.x->left_pos;
11014 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11015#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11016 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11017 /* It is a mystery why we need to add the border_width here
11018 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11019 if (change_gravity != 0)
4a4cbdd5 11020 {
7556890b
RS
11021 modified_left += f->output_data.x->border_width;
11022 modified_top += f->output_data.x->border_width;
4a4cbdd5 11023 }
e73ec6fa 11024#endif
4a4cbdd5 11025
3afe33e7 11026#ifdef USE_X_TOOLKIT
7556890b 11027 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11028 modified_left, modified_top);
3afe33e7 11029#else /* not USE_X_TOOLKIT */
334208b7 11030 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11031 modified_left, modified_top);
3afe33e7 11032#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11033 UNBLOCK_INPUT;
11034}
11035
bc20ebbf
FP
11036/* Call this to change the size of frame F's x-window.
11037 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11038 for this size change and subsequent size changes.
11039 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11040
dfcf069d 11041void
bc20ebbf 11042x_set_window_size (f, change_gravity, cols, rows)
f676886a 11043 struct frame *f;
bc20ebbf 11044 int change_gravity;
b1c884c3 11045 int cols, rows;
dc6f92b8 11046{
06a2c219 11047#ifndef USE_X_TOOLKIT
dc6f92b8 11048 int pixelwidth, pixelheight;
06a2c219 11049#endif
dc6f92b8 11050
80fd1fe2 11051 BLOCK_INPUT;
aee9a898
RS
11052
11053#ifdef USE_X_TOOLKIT
3a20653d
RS
11054 {
11055 /* The x and y position of the widget is clobbered by the
11056 call to XtSetValues within EmacsFrameSetCharSize.
11057 This is a real kludge, but I don't understand Xt so I can't
11058 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11059 int xpos = f->output_data.x->widget->core.x;
11060 int ypos = f->output_data.x->widget->core.y;
11061 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11062 f->output_data.x->widget->core.x = xpos;
11063 f->output_data.x->widget->core.y = ypos;
3a20653d 11064 }
80fd1fe2
FP
11065
11066#else /* not USE_X_TOOLKIT */
11067
b1c884c3 11068 check_frame_size (f, &rows, &cols);
7556890b 11069 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11070 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11071 ? 0
11072 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11073 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11074 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11075 f->output_data.x->flags_areas_extra
110859fc 11076 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11077 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11078 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11079
7556890b 11080 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11081 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11082
334208b7
RS
11083 XSync (FRAME_X_DISPLAY (f), False);
11084 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11085 pixelwidth, pixelheight);
b1c884c3
JB
11086
11087 /* Now, strictly speaking, we can't be sure that this is accurate,
11088 but the window manager will get around to dealing with the size
11089 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11090 ConfigureNotify event gets here.
11091
11092 We could just not bother storing any of this information here,
11093 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11094 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11095 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11096 point in the future when the ConfigureNotify event arrives.
11097
11098 We pass 1 for DELAY since we can't run Lisp code inside of
11099 a BLOCK_INPUT. */
7d1e984f 11100 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11101 PIXEL_WIDTH (f) = pixelwidth;
11102 PIXEL_HEIGHT (f) = pixelheight;
11103
aee9a898
RS
11104 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11105 receive in the ConfigureNotify event; if we get what we asked
11106 for, then the event won't cause the screen to become garbaged, so
11107 we have to make sure to do it here. */
11108 SET_FRAME_GARBAGED (f);
11109
11110 XFlush (FRAME_X_DISPLAY (f));
11111
11112#endif /* not USE_X_TOOLKIT */
11113
4d73d038 11114 /* If cursor was outside the new size, mark it as off. */
06a2c219 11115 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11116
aee9a898
RS
11117 /* Clear out any recollection of where the mouse highlighting was,
11118 since it might be in a place that's outside the new frame size.
11119 Actually checking whether it is outside is a pain in the neck,
11120 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11121 cancel_mouse_face (f);
dbc4e1c1 11122
dc6f92b8
JB
11123 UNBLOCK_INPUT;
11124}
dc6f92b8 11125\f
d047c4eb 11126/* Mouse warping. */
dc6f92b8 11127
9b378208 11128void
f676886a
JB
11129x_set_mouse_position (f, x, y)
11130 struct frame *f;
dc6f92b8
JB
11131 int x, y;
11132{
11133 int pix_x, pix_y;
11134
7556890b
RS
11135 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11136 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11137
11138 if (pix_x < 0) pix_x = 0;
11139 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11140
11141 if (pix_y < 0) pix_y = 0;
11142 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11143
11144 BLOCK_INPUT;
dc6f92b8 11145
334208b7
RS
11146 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11147 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11148 UNBLOCK_INPUT;
11149}
11150
9b378208
RS
11151/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11152
11153void
11154x_set_mouse_pixel_position (f, pix_x, pix_y)
11155 struct frame *f;
11156 int pix_x, pix_y;
11157{
11158 BLOCK_INPUT;
11159
334208b7
RS
11160 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11161 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11162 UNBLOCK_INPUT;
11163}
d047c4eb
KH
11164\f
11165/* focus shifting, raising and lowering. */
9b378208 11166
dfcf069d 11167void
f676886a
JB
11168x_focus_on_frame (f)
11169 struct frame *f;
dc6f92b8 11170{
1fb20991 11171#if 0 /* This proves to be unpleasant. */
f676886a 11172 x_raise_frame (f);
1fb20991 11173#endif
6d4238f3
JB
11174#if 0
11175 /* I don't think that the ICCCM allows programs to do things like this
11176 without the interaction of the window manager. Whatever you end up
f676886a 11177 doing with this code, do it to x_unfocus_frame too. */
334208b7 11178 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11179 RevertToPointerRoot, CurrentTime);
c118dd06 11180#endif /* ! 0 */
dc6f92b8
JB
11181}
11182
dfcf069d 11183void
f676886a
JB
11184x_unfocus_frame (f)
11185 struct frame *f;
dc6f92b8 11186{
6d4238f3 11187#if 0
f676886a 11188 /* Look at the remarks in x_focus_on_frame. */
0f941935 11189 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11190 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11191 RevertToPointerRoot, CurrentTime);
c118dd06 11192#endif /* ! 0 */
dc6f92b8
JB
11193}
11194
f676886a 11195/* Raise frame F. */
dc6f92b8 11196
dfcf069d 11197void
f676886a
JB
11198x_raise_frame (f)
11199 struct frame *f;
dc6f92b8 11200{
3a88c238 11201 if (f->async_visible)
dc6f92b8
JB
11202 {
11203 BLOCK_INPUT;
3afe33e7 11204#ifdef USE_X_TOOLKIT
7556890b 11205 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11206#else /* not USE_X_TOOLKIT */
334208b7 11207 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11208#endif /* not USE_X_TOOLKIT */
334208b7 11209 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11210 UNBLOCK_INPUT;
11211 }
11212}
11213
f676886a 11214/* Lower frame F. */
dc6f92b8 11215
dfcf069d 11216void
f676886a
JB
11217x_lower_frame (f)
11218 struct frame *f;
dc6f92b8 11219{
3a88c238 11220 if (f->async_visible)
dc6f92b8
JB
11221 {
11222 BLOCK_INPUT;
3afe33e7 11223#ifdef USE_X_TOOLKIT
7556890b 11224 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11225#else /* not USE_X_TOOLKIT */
334208b7 11226 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11227#endif /* not USE_X_TOOLKIT */
334208b7 11228 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11229 UNBLOCK_INPUT;
11230 }
11231}
11232
dbc4e1c1 11233static void
6b0442dc 11234XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11235 FRAME_PTR f;
6b0442dc 11236 int raise_flag;
dbc4e1c1 11237{
6b0442dc 11238 if (raise_flag)
dbc4e1c1
JB
11239 x_raise_frame (f);
11240 else
11241 x_lower_frame (f);
11242}
d047c4eb
KH
11243\f
11244/* Change of visibility. */
dc6f92b8 11245
9382638d
KH
11246/* This tries to wait until the frame is really visible.
11247 However, if the window manager asks the user where to position
11248 the frame, this will return before the user finishes doing that.
11249 The frame will not actually be visible at that time,
11250 but it will become visible later when the window manager
11251 finishes with it. */
11252
dfcf069d 11253void
f676886a
JB
11254x_make_frame_visible (f)
11255 struct frame *f;
dc6f92b8 11256{
990ba854 11257 Lisp_Object type;
1aa6072f 11258 int original_top, original_left;
dc6f92b8 11259
dc6f92b8 11260 BLOCK_INPUT;
dc6f92b8 11261
990ba854
RS
11262 type = x_icon_type (f);
11263 if (!NILP (type))
11264 x_bitmap_icon (f, type);
bdcd49ba 11265
f676886a 11266 if (! FRAME_VISIBLE_P (f))
90e65f07 11267 {
1aa6072f
RS
11268 /* We test FRAME_GARBAGED_P here to make sure we don't
11269 call x_set_offset a second time
11270 if we get to x_make_frame_visible a second time
11271 before the window gets really visible. */
11272 if (! FRAME_ICONIFIED_P (f)
11273 && ! f->output_data.x->asked_for_visible)
11274 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11275
11276 f->output_data.x->asked_for_visible = 1;
11277
90e65f07 11278 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11279 x_wm_set_window_state (f, NormalState);
3afe33e7 11280#ifdef USE_X_TOOLKIT
d7a38a2e 11281 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11282 XtMapWidget (f->output_data.x->widget);
3afe33e7 11283#else /* not USE_X_TOOLKIT */
7f9c7f94 11284 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11285#endif /* not USE_X_TOOLKIT */
0134a210
RS
11286#if 0 /* This seems to bring back scroll bars in the wrong places
11287 if the window configuration has changed. They seem
11288 to come back ok without this. */
ab648270 11289 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11290 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11291#endif
90e65f07 11292 }
dc6f92b8 11293
334208b7 11294 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11295
0dacf791
RS
11296 /* Synchronize to ensure Emacs knows the frame is visible
11297 before we do anything else. We do this loop with input not blocked
11298 so that incoming events are handled. */
11299 {
11300 Lisp_Object frame;
c0a04927 11301 int count = input_signal_count;
28c01ffe
RS
11302 /* This must be before UNBLOCK_INPUT
11303 since events that arrive in response to the actions above
11304 will set it when they are handled. */
11305 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11306
11307 original_left = f->output_data.x->left_pos;
11308 original_top = f->output_data.x->top_pos;
c0a04927
RS
11309
11310 /* This must come after we set COUNT. */
11311 UNBLOCK_INPUT;
11312
2745e6c4 11313 /* We unblock here so that arriving X events are processed. */
1aa6072f 11314
dcb07ae9
RS
11315 /* Now move the window back to where it was "supposed to be".
11316 But don't do it if the gravity is negative.
11317 When the gravity is negative, this uses a position
28c01ffe
RS
11318 that is 3 pixels too low. Perhaps that's really the border width.
11319
11320 Don't do this if the window has never been visible before,
11321 because the window manager may choose the position
11322 and we don't want to override it. */
1aa6072f 11323
4d3f5d9a 11324 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11325 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11326 && previously_visible)
1aa6072f 11327 {
2745e6c4
RS
11328 Drawable rootw;
11329 int x, y;
11330 unsigned int width, height, border, depth;
06a2c219 11331
1aa6072f 11332 BLOCK_INPUT;
9829ddba 11333
06a2c219
GM
11334 /* On some window managers (such as FVWM) moving an existing
11335 window, even to the same place, causes the window manager
11336 to introduce an offset. This can cause the window to move
11337 to an unexpected location. Check the geometry (a little
11338 slow here) and then verify that the window is in the right
11339 place. If the window is not in the right place, move it
11340 there, and take the potential window manager hit. */
2745e6c4
RS
11341 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11342 &rootw, &x, &y, &width, &height, &border, &depth);
11343
11344 if (original_left != x || original_top != y)
11345 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11346 original_left, original_top);
11347
1aa6072f
RS
11348 UNBLOCK_INPUT;
11349 }
9829ddba 11350
e0c1aef2 11351 XSETFRAME (frame, f);
c0a04927
RS
11352
11353 while (1)
2a6cf806 11354 {
334208b7 11355 x_sync (f);
c0a04927
RS
11356 /* Once we have handled input events,
11357 we should have received the MapNotify if one is coming.
11358 So if we have not got it yet, stop looping.
11359 Some window managers make their own decisions
11360 about visibility. */
11361 if (input_signal_count != count)
11362 break;
c12a7cbd 11363 /* Machines that do polling rather than SIGIO have been observed
23cf7c60
KH
11364 to go into a busy-wait here. So we'll fake an alarm signal
11365 to let the handler know that there's something to be read.
11366 We used to raise a real alarm, but it seems that the handler
11367 isn't always enabled here. This is probably a bug. */
8b2f8d4e 11368 if (input_polling_used ())
3b2fa4e6
RS
11369 {
11370 /* It could be confusing if a real alarm arrives while processing
11371 the fake one. Turn it off and let the handler reset it. */
11372 alarm (0);
5e8e68ce 11373 input_poll_signal (0);
3b2fa4e6 11374 }
c0a04927
RS
11375 /* Once we have handled input events,
11376 we should have received the MapNotify if one is coming.
11377 So if we have not got it yet, stop looping.
11378 Some window managers make their own decisions
11379 about visibility. */
11380 if (input_signal_count != count)
11381 break;
2a6cf806 11382 }
0dacf791
RS
11383 FRAME_SAMPLE_VISIBILITY (f);
11384 }
dc6f92b8
JB
11385}
11386
06a2c219 11387/* Change from mapped state to withdrawn state. */
dc6f92b8 11388
d047c4eb
KH
11389/* Make the frame visible (mapped and not iconified). */
11390
dfcf069d 11391void
f676886a
JB
11392x_make_frame_invisible (f)
11393 struct frame *f;
dc6f92b8 11394{
546e6d5b
RS
11395 Window window;
11396
11397#ifdef USE_X_TOOLKIT
11398 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11399 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11400#else /* not USE_X_TOOLKIT */
11401 window = FRAME_X_WINDOW (f);
11402#endif /* not USE_X_TOOLKIT */
dc6f92b8 11403
9319ae23 11404 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11405 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11406 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11407
5627c40e 11408#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11409 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11410 return;
5627c40e 11411#endif
dc6f92b8
JB
11412
11413 BLOCK_INPUT;
c118dd06 11414
af31d76f
RS
11415 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11416 that the current position of the window is user-specified, rather than
11417 program-specified, so that when the window is mapped again, it will be
11418 placed at the same location, without forcing the user to position it
11419 by hand again (they have already done that once for this window.) */
c32cdd9a 11420 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11421
c118dd06
JB
11422#ifdef HAVE_X11R4
11423
334208b7
RS
11424 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11425 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11426 {
11427 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11428 error ("Can't notify window manager of window withdrawal");
c118dd06 11429 }
c118dd06 11430#else /* ! defined (HAVE_X11R4) */
16bd92ea 11431
c118dd06 11432 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11433 if (! EQ (Vx_no_window_manager, Qt))
11434 {
16bd92ea 11435 XEvent unmap;
dc6f92b8 11436
16bd92ea 11437 unmap.xunmap.type = UnmapNotify;
546e6d5b 11438 unmap.xunmap.window = window;
334208b7 11439 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11440 unmap.xunmap.from_configure = False;
334208b7
RS
11441 if (! XSendEvent (FRAME_X_DISPLAY (f),
11442 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11443 False,
06a2c219 11444 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11445 &unmap))
11446 {
11447 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11448 error ("Can't notify window manager of withdrawal");
16bd92ea 11449 }
dc6f92b8
JB
11450 }
11451
16bd92ea 11452 /* Unmap the window ourselves. Cheeky! */
334208b7 11453 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11454#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11455
5627c40e
RS
11456 /* We can't distinguish this from iconification
11457 just by the event that we get from the server.
11458 So we can't win using the usual strategy of letting
11459 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11460 and synchronize with the server to make sure we agree. */
11461 f->visible = 0;
11462 FRAME_ICONIFIED_P (f) = 0;
11463 f->async_visible = 0;
11464 f->async_iconified = 0;
11465
334208b7 11466 x_sync (f);
5627c40e 11467
dc6f92b8
JB
11468 UNBLOCK_INPUT;
11469}
11470
06a2c219 11471/* Change window state from mapped to iconified. */
dc6f92b8 11472
dfcf069d 11473void
f676886a
JB
11474x_iconify_frame (f)
11475 struct frame *f;
dc6f92b8 11476{
3afe33e7 11477 int result;
990ba854 11478 Lisp_Object type;
dc6f92b8 11479
9319ae23 11480 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11481 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11482 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11483
3a88c238 11484 if (f->async_iconified)
dc6f92b8
JB
11485 return;
11486
3afe33e7 11487 BLOCK_INPUT;
546e6d5b 11488
9af3143a
RS
11489 FRAME_SAMPLE_VISIBILITY (f);
11490
990ba854
RS
11491 type = x_icon_type (f);
11492 if (!NILP (type))
11493 x_bitmap_icon (f, type);
bdcd49ba
RS
11494
11495#ifdef USE_X_TOOLKIT
11496
546e6d5b
RS
11497 if (! FRAME_VISIBLE_P (f))
11498 {
11499 if (! EQ (Vx_no_window_manager, Qt))
11500 x_wm_set_window_state (f, IconicState);
11501 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11502 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11503 /* The server won't give us any event to indicate
11504 that an invisible frame was changed to an icon,
11505 so we have to record it here. */
11506 f->iconified = 1;
1e6bc770 11507 f->visible = 1;
9cf30a30 11508 f->async_iconified = 1;
1e6bc770 11509 f->async_visible = 0;
546e6d5b
RS
11510 UNBLOCK_INPUT;
11511 return;
11512 }
11513
334208b7 11514 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11515 XtWindow (f->output_data.x->widget),
334208b7 11516 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11517 UNBLOCK_INPUT;
11518
11519 if (!result)
546e6d5b 11520 error ("Can't notify window manager of iconification");
3afe33e7
RS
11521
11522 f->async_iconified = 1;
1e6bc770
RS
11523 f->async_visible = 0;
11524
8c002a25
KH
11525
11526 BLOCK_INPUT;
334208b7 11527 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11528 UNBLOCK_INPUT;
3afe33e7
RS
11529#else /* not USE_X_TOOLKIT */
11530
fd13dbb2
RS
11531 /* Make sure the X server knows where the window should be positioned,
11532 in case the user deiconifies with the window manager. */
11533 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11534 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11535
16bd92ea
JB
11536 /* Since we don't know which revision of X we're running, we'll use both
11537 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11538
11539 /* X11R4: send a ClientMessage to the window manager using the
11540 WM_CHANGE_STATE type. */
11541 {
11542 XEvent message;
58769bee 11543
c118dd06 11544 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11545 message.xclient.type = ClientMessage;
334208b7 11546 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11547 message.xclient.format = 32;
11548 message.xclient.data.l[0] = IconicState;
11549
334208b7
RS
11550 if (! XSendEvent (FRAME_X_DISPLAY (f),
11551 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11552 False,
11553 SubstructureRedirectMask | SubstructureNotifyMask,
11554 &message))
dc6f92b8
JB
11555 {
11556 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11557 error ("Can't notify window manager of iconification");
dc6f92b8 11558 }
16bd92ea 11559 }
dc6f92b8 11560
58769bee 11561 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11562 IconicState. */
11563 x_wm_set_window_state (f, IconicState);
dc6f92b8 11564
a9c00105
RS
11565 if (!FRAME_VISIBLE_P (f))
11566 {
11567 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11568 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11569 }
11570
3a88c238 11571 f->async_iconified = 1;
1e6bc770 11572 f->async_visible = 0;
dc6f92b8 11573
334208b7 11574 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11575 UNBLOCK_INPUT;
8c002a25 11576#endif /* not USE_X_TOOLKIT */
dc6f92b8 11577}
d047c4eb 11578\f
c0ff3fab 11579/* Destroy the X window of frame F. */
dc6f92b8 11580
dfcf069d 11581void
c0ff3fab 11582x_destroy_window (f)
f676886a 11583 struct frame *f;
dc6f92b8 11584{
7f9c7f94
RS
11585 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11586
dc6f92b8 11587 BLOCK_INPUT;
c0ff3fab 11588
6186a4a0
RS
11589 /* If a display connection is dead, don't try sending more
11590 commands to the X server. */
11591 if (dpyinfo->display != 0)
11592 {
11593 if (f->output_data.x->icon_desc != 0)
11594 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf
RS
11595#ifdef HAVE_X_I18N
11596 if (FRAME_XIM (f))
11597 {
11598 XDestroyIC (FRAME_XIC (f));
408be661
RS
11599#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11600 /* This line causes crashes on Solaris with Openwin,
11601 due to an apparent bug in XCloseIM.
11602 X11R6 seems not to have the bug. */
31f41daf 11603 XCloseIM (FRAME_XIM (f));
a9978dd8 11604#endif
31f41daf
RS
11605 }
11606#endif
6186a4a0 11607 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11608#ifdef USE_X_TOOLKIT
06a2c219
GM
11609 if (f->output_data.x->widget)
11610 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11611 free_frame_menubar (f);
3afe33e7
RS
11612#endif /* USE_X_TOOLKIT */
11613
6186a4a0
RS
11614 free_frame_faces (f);
11615 XFlush (FRAME_X_DISPLAY (f));
11616 }
dc6f92b8 11617
df89d8a4 11618 if (f->output_data.x->saved_menu_event)
06a2c219 11619 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11620
7556890b
RS
11621 xfree (f->output_data.x);
11622 f->output_data.x = 0;
0f941935
KH
11623 if (f == dpyinfo->x_focus_frame)
11624 dpyinfo->x_focus_frame = 0;
11625 if (f == dpyinfo->x_focus_event_frame)
11626 dpyinfo->x_focus_event_frame = 0;
11627 if (f == dpyinfo->x_highlight_frame)
11628 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11629
7f9c7f94
RS
11630 dpyinfo->reference_count--;
11631
11632 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11633 {
7f9c7f94
RS
11634 dpyinfo->mouse_face_beg_row
11635 = dpyinfo->mouse_face_beg_col = -1;
11636 dpyinfo->mouse_face_end_row
11637 = dpyinfo->mouse_face_end_col = -1;
11638 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11639 dpyinfo->mouse_face_deferred_gc = 0;
11640 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11641 }
0134a210 11642
c0ff3fab 11643 UNBLOCK_INPUT;
dc6f92b8
JB
11644}
11645\f
f451eb13
JB
11646/* Setting window manager hints. */
11647
af31d76f
RS
11648/* Set the normal size hints for the window manager, for frame F.
11649 FLAGS is the flags word to use--or 0 meaning preserve the flags
11650 that the window now has.
11651 If USER_POSITION is nonzero, we set the USPosition
11652 flag (this is useful when FLAGS is 0). */
6dba1858 11653
dfcf069d 11654void
af31d76f 11655x_wm_set_size_hint (f, flags, user_position)
f676886a 11656 struct frame *f;
af31d76f
RS
11657 long flags;
11658 int user_position;
dc6f92b8
JB
11659{
11660 XSizeHints size_hints;
3afe33e7
RS
11661
11662#ifdef USE_X_TOOLKIT
7e4f2521
FP
11663 Arg al[2];
11664 int ac = 0;
11665 Dimension widget_width, widget_height;
7556890b 11666 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11667#else /* not USE_X_TOOLKIT */
c118dd06 11668 Window window = FRAME_X_WINDOW (f);
3afe33e7 11669#endif /* not USE_X_TOOLKIT */
dc6f92b8 11670
b72a58fd
RS
11671 /* Setting PMaxSize caused various problems. */
11672 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11673
7556890b
RS
11674 size_hints.x = f->output_data.x->left_pos;
11675 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11676
7e4f2521
FP
11677#ifdef USE_X_TOOLKIT
11678 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11679 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11680 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11681 size_hints.height = widget_height;
11682 size_hints.width = widget_width;
11683#else /* not USE_X_TOOLKIT */
f676886a
JB
11684 size_hints.height = PIXEL_HEIGHT (f);
11685 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11686#endif /* not USE_X_TOOLKIT */
7553a6b7 11687
7556890b
RS
11688 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11689 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11690 size_hints.max_width
11691 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11692 size_hints.max_height
11693 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11694
d067ea8b
KH
11695 /* Calculate the base and minimum sizes.
11696
11697 (When we use the X toolkit, we don't do it here.
11698 Instead we copy the values that the widgets are using, below.) */
11699#ifndef USE_X_TOOLKIT
b1c884c3 11700 {
b0342f17 11701 int base_width, base_height;
0134a210 11702 int min_rows = 0, min_cols = 0;
b0342f17 11703
f451eb13
JB
11704 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11705 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11706
0134a210 11707 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11708
0134a210
RS
11709 /* The window manager uses the base width hints to calculate the
11710 current number of rows and columns in the frame while
11711 resizing; min_width and min_height aren't useful for this
11712 purpose, since they might not give the dimensions for a
11713 zero-row, zero-column frame.
58769bee 11714
0134a210
RS
11715 We use the base_width and base_height members if we have
11716 them; otherwise, we set the min_width and min_height members
11717 to the size for a zero x zero frame. */
b0342f17
JB
11718
11719#ifdef HAVE_X11R4
0134a210
RS
11720 size_hints.flags |= PBaseSize;
11721 size_hints.base_width = base_width;
11722 size_hints.base_height = base_height;
11723 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11724 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11725#else
0134a210
RS
11726 size_hints.min_width = base_width;
11727 size_hints.min_height = base_height;
b0342f17 11728#endif
b1c884c3 11729 }
dc6f92b8 11730
d067ea8b 11731 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11732 if (flags)
dc6f92b8 11733 {
d067ea8b
KH
11734 size_hints.flags |= flags;
11735 goto no_read;
11736 }
11737#endif /* not USE_X_TOOLKIT */
11738
11739 {
11740 XSizeHints hints; /* Sometimes I hate X Windows... */
11741 long supplied_return;
11742 int value;
af31d76f
RS
11743
11744#ifdef HAVE_X11R4
d067ea8b
KH
11745 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11746 &supplied_return);
af31d76f 11747#else
d067ea8b 11748 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11749#endif
58769bee 11750
d067ea8b
KH
11751#ifdef USE_X_TOOLKIT
11752 size_hints.base_height = hints.base_height;
11753 size_hints.base_width = hints.base_width;
11754 size_hints.min_height = hints.min_height;
11755 size_hints.min_width = hints.min_width;
11756#endif
11757
11758 if (flags)
11759 size_hints.flags |= flags;
11760 else
11761 {
11762 if (value == 0)
11763 hints.flags = 0;
11764 if (hints.flags & PSize)
11765 size_hints.flags |= PSize;
11766 if (hints.flags & PPosition)
11767 size_hints.flags |= PPosition;
11768 if (hints.flags & USPosition)
11769 size_hints.flags |= USPosition;
11770 if (hints.flags & USSize)
11771 size_hints.flags |= USSize;
11772 }
11773 }
11774
06a2c219 11775#ifndef USE_X_TOOLKIT
d067ea8b 11776 no_read:
06a2c219 11777#endif
0134a210 11778
af31d76f 11779#ifdef PWinGravity
7556890b 11780 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11781 size_hints.flags |= PWinGravity;
dc05a16b 11782
af31d76f 11783 if (user_position)
6dba1858 11784 {
af31d76f
RS
11785 size_hints.flags &= ~ PPosition;
11786 size_hints.flags |= USPosition;
6dba1858 11787 }
2554751d 11788#endif /* PWinGravity */
6dba1858 11789
b0342f17 11790#ifdef HAVE_X11R4
334208b7 11791 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11792#else
334208b7 11793 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11794#endif
dc6f92b8
JB
11795}
11796
11797/* Used for IconicState or NormalState */
06a2c219 11798
dfcf069d 11799void
f676886a
JB
11800x_wm_set_window_state (f, state)
11801 struct frame *f;
dc6f92b8
JB
11802 int state;
11803{
3afe33e7 11804#ifdef USE_X_TOOLKIT
546e6d5b
RS
11805 Arg al[1];
11806
11807 XtSetArg (al[0], XtNinitialState, state);
7556890b 11808 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11809#else /* not USE_X_TOOLKIT */
c118dd06 11810 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11811
7556890b
RS
11812 f->output_data.x->wm_hints.flags |= StateHint;
11813 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11814
7556890b 11815 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11816#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11817}
11818
dfcf069d 11819void
7f2ae036 11820x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11821 struct frame *f;
7f2ae036 11822 int pixmap_id;
dc6f92b8 11823{
d2bd6bc4
RS
11824 Pixmap icon_pixmap;
11825
06a2c219 11826#ifndef USE_X_TOOLKIT
c118dd06 11827 Window window = FRAME_X_WINDOW (f);
75231bad 11828#endif
dc6f92b8 11829
7f2ae036 11830 if (pixmap_id > 0)
dbc4e1c1 11831 {
d2bd6bc4 11832 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11833 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11834 }
11835 else
68568555
RS
11836 {
11837 /* It seems there is no way to turn off use of an icon pixmap.
11838 The following line does it, only if no icon has yet been created,
11839 for some window managers. But with mwm it crashes.
11840 Some people say it should clear the IconPixmapHint bit in this case,
11841 but that doesn't work, and the X consortium said it isn't the
11842 right thing at all. Since there is no way to win,
11843 best to explicitly give up. */
11844#if 0
11845 f->output_data.x->wm_hints.icon_pixmap = None;
11846#else
11847 return;
11848#endif
11849 }
b1c884c3 11850
d2bd6bc4
RS
11851#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11852
11853 {
11854 Arg al[1];
11855 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11856 XtSetValues (f->output_data.x->widget, al, 1);
11857 }
11858
11859#else /* not USE_X_TOOLKIT */
11860
7556890b
RS
11861 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11862 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11863
11864#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11865}
11866
dfcf069d 11867void
f676886a
JB
11868x_wm_set_icon_position (f, icon_x, icon_y)
11869 struct frame *f;
dc6f92b8
JB
11870 int icon_x, icon_y;
11871{
75231bad 11872#ifdef USE_X_TOOLKIT
7556890b 11873 Window window = XtWindow (f->output_data.x->widget);
75231bad 11874#else
c118dd06 11875 Window window = FRAME_X_WINDOW (f);
75231bad 11876#endif
dc6f92b8 11877
7556890b
RS
11878 f->output_data.x->wm_hints.flags |= IconPositionHint;
11879 f->output_data.x->wm_hints.icon_x = icon_x;
11880 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11881
7556890b 11882 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11883}
11884
11885\f
06a2c219
GM
11886/***********************************************************************
11887 Fonts
11888 ***********************************************************************/
dc43ef94
KH
11889
11890/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11891
dc43ef94
KH
11892struct font_info *
11893x_get_font_info (f, font_idx)
11894 FRAME_PTR f;
11895 int font_idx;
11896{
11897 return (FRAME_X_FONT_TABLE (f) + font_idx);
11898}
11899
11900
11901/* Return a list of names of available fonts matching PATTERN on frame
11902 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11903 to be listed. Frame F NULL means we have not yet created any
11904 frame on X, and consult the first display in x_display_list.
11905 MAXNAMES sets a limit on how many fonts to match. */
11906
11907Lisp_Object
11908x_list_fonts (f, pattern, size, maxnames)
11909 FRAME_PTR f;
11910 Lisp_Object pattern;
11911 int size;
11912 int maxnames;
11913{
06a2c219
GM
11914 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11915 Lisp_Object tem, second_best;
dc43ef94 11916 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 11917 int try_XLoadQueryFont = 0;
53ca4657 11918 int count;
dc43ef94 11919
6b0efe73 11920 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11921 if (NILP (patterns))
11922 patterns = Fcons (pattern, Qnil);
81ba44e5 11923
09c6077f
KH
11924 if (maxnames == 1 && !size)
11925 /* We can return any single font matching PATTERN. */
11926 try_XLoadQueryFont = 1;
9a32686f 11927
8e713be6 11928 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 11929 {
dc43ef94
KH
11930 int num_fonts;
11931 char **names;
11932
8e713be6 11933 pattern = XCAR (patterns);
536f4067
RS
11934 /* See if we cached the result for this particular query.
11935 The cache is an alist of the form:
11936 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11937 */
8e713be6 11938 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
11939 key = Fcons (pattern, make_number (maxnames)),
11940 !NILP (list = Fassoc (key, tem))))
11941 {
11942 list = Fcdr_safe (list);
11943 /* We have a cashed list. Don't have to get the list again. */
11944 goto label_cached;
11945 }
11946
11947 /* At first, put PATTERN in the cache. */
09c6077f 11948
dc43ef94 11949 BLOCK_INPUT;
17d85edc
KH
11950 count = x_catch_errors (dpy);
11951
09c6077f
KH
11952 if (try_XLoadQueryFont)
11953 {
11954 XFontStruct *font;
11955 unsigned long value;
11956
11957 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
11958 if (x_had_errors_p (dpy))
11959 {
11960 /* This error is perhaps due to insufficient memory on X
11961 server. Let's just ignore it. */
11962 font = NULL;
11963 x_clear_errors (dpy);
11964 }
11965
09c6077f
KH
11966 if (font
11967 && XGetFontProperty (font, XA_FONT, &value))
11968 {
11969 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11970 int len = strlen (name);
01c752b5 11971 char *tmp;
09c6077f 11972
6f6512e8
KH
11973 /* If DXPC (a Differential X Protocol Compressor)
11974 Ver.3.7 is running, XGetAtomName will return null
11975 string. We must avoid such a name. */
11976 if (len == 0)
11977 try_XLoadQueryFont = 0;
11978 else
11979 {
11980 num_fonts = 1;
11981 names = (char **) alloca (sizeof (char *));
11982 /* Some systems only allow alloca assigned to a
11983 simple var. */
11984 tmp = (char *) alloca (len + 1); names[0] = tmp;
11985 bcopy (name, names[0], len + 1);
11986 XFree (name);
11987 }
09c6077f
KH
11988 }
11989 else
11990 try_XLoadQueryFont = 0;
a083fd23
RS
11991
11992 if (font)
11993 XFreeFont (dpy, font);
09c6077f
KH
11994 }
11995
11996 if (!try_XLoadQueryFont)
17d85edc
KH
11997 {
11998 /* We try at least 10 fonts because XListFonts will return
11999 auto-scaled fonts at the head. */
12000 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12001 &num_fonts);
12002 if (x_had_errors_p (dpy))
12003 {
12004 /* This error is perhaps due to insufficient memory on X
12005 server. Let's just ignore it. */
12006 names = NULL;
12007 x_clear_errors (dpy);
12008 }
12009 }
12010
12011 x_uncatch_errors (dpy, count);
dc43ef94
KH
12012 UNBLOCK_INPUT;
12013
12014 if (names)
12015 {
12016 int i;
dc43ef94
KH
12017
12018 /* Make a list of all the fonts we got back.
12019 Store that in the font cache for the display. */
12020 for (i = 0; i < num_fonts; i++)
12021 {
06a2c219 12022 int width = 0;
dc43ef94 12023 char *p = names[i];
06a2c219
GM
12024 int average_width = -1, dashes = 0;
12025
dc43ef94 12026 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12027 14 dashes, and the field value following 12th dash
12028 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12029 is usually too ugly to be used for editing. Let's
12030 ignore it. */
dc43ef94
KH
12031 while (*p)
12032 if (*p++ == '-')
12033 {
12034 dashes++;
12035 if (dashes == 7) /* PIXEL_SIZE field */
12036 width = atoi (p);
12037 else if (dashes == 12) /* AVERAGE_WIDTH field */
12038 average_width = atoi (p);
12039 }
12040 if (dashes < 14 || average_width != 0)
12041 {
12042 tem = build_string (names[i]);
12043 if (NILP (Fassoc (tem, list)))
12044 {
12045 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12046 && ((fast_c_string_match_ignore_case
12047 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12048 >= 0))
12049 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12050 width of this font. */
dc43ef94
KH
12051 list = Fcons (Fcons (tem, make_number (width)), list);
12052 else
12053 /* For the moment, width is not known. */
12054 list = Fcons (Fcons (tem, Qnil), list);
12055 }
12056 }
12057 }
09c6077f
KH
12058 if (!try_XLoadQueryFont)
12059 XFreeFontNames (names);
dc43ef94
KH
12060 }
12061
b5210ea7 12062 /* Now store the result in the cache. */
dc43ef94 12063 if (f != NULL)
8e713be6 12064 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12065 = Fcons (Fcons (key, list),
8e713be6 12066 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12067
b5210ea7
KH
12068 label_cached:
12069 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12070
b5210ea7
KH
12071 newlist = second_best = Qnil;
12072 /* Make a list of the fonts that have the right width. */
8e713be6 12073 for (; CONSP (list); list = XCDR (list))
b5210ea7 12074 {
536f4067
RS
12075 int found_size;
12076
8e713be6 12077 tem = XCAR (list);
dc43ef94 12078
8e713be6 12079 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12080 continue;
12081 if (!size)
12082 {
8e713be6 12083 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12084 continue;
12085 }
dc43ef94 12086
8e713be6 12087 if (!INTEGERP (XCDR (tem)))
dc43ef94 12088 {
b5210ea7
KH
12089 /* Since we have not yet known the size of this font, we
12090 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12091 XFontStruct *thisinfo;
12092
12093 BLOCK_INPUT;
17d85edc 12094 count = x_catch_errors (dpy);
dc43ef94 12095 thisinfo = XLoadQueryFont (dpy,
8e713be6 12096 XSTRING (XCAR (tem))->data);
17d85edc
KH
12097 if (x_had_errors_p (dpy))
12098 {
12099 /* This error is perhaps due to insufficient memory on X
12100 server. Let's just ignore it. */
12101 thisinfo = NULL;
12102 x_clear_errors (dpy);
12103 }
12104 x_uncatch_errors (dpy, count);
dc43ef94
KH
12105 UNBLOCK_INPUT;
12106
12107 if (thisinfo)
12108 {
8e713be6 12109 XCDR (tem)
536f4067
RS
12110 = (thisinfo->min_bounds.width == 0
12111 ? make_number (0)
12112 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12113 XFreeFont (dpy, thisinfo);
12114 }
12115 else
b5210ea7 12116 /* For unknown reason, the previous call of XListFont had
06a2c219 12117 returned a font which can't be opened. Record the size
b5210ea7 12118 as 0 not to try to open it again. */
8e713be6 12119 XCDR (tem) = make_number (0);
dc43ef94 12120 }
536f4067 12121
8e713be6 12122 found_size = XINT (XCDR (tem));
536f4067 12123 if (found_size == size)
8e713be6 12124 newlist = Fcons (XCAR (tem), newlist);
536f4067 12125 else if (found_size > 0)
b5210ea7 12126 {
536f4067 12127 if (NILP (second_best))
b5210ea7 12128 second_best = tem;
536f4067
RS
12129 else if (found_size < size)
12130 {
8e713be6
KR
12131 if (XINT (XCDR (second_best)) > size
12132 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12133 second_best = tem;
12134 }
12135 else
12136 {
8e713be6
KR
12137 if (XINT (XCDR (second_best)) > size
12138 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12139 second_best = tem;
12140 }
b5210ea7
KH
12141 }
12142 }
12143 if (!NILP (newlist))
12144 break;
12145 else if (!NILP (second_best))
12146 {
8e713be6 12147 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12148 break;
dc43ef94 12149 }
dc43ef94
KH
12150 }
12151
12152 return newlist;
12153}
12154
06a2c219
GM
12155
12156#if GLYPH_DEBUG
12157
12158/* Check that FONT is valid on frame F. It is if it can be found in F's
12159 font table. */
12160
12161static void
12162x_check_font (f, font)
12163 struct frame *f;
12164 XFontStruct *font;
12165{
12166 int i;
12167 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12168
12169 xassert (font != NULL);
12170
12171 for (i = 0; i < dpyinfo->n_fonts; i++)
12172 if (dpyinfo->font_table[i].name
12173 && font == dpyinfo->font_table[i].font)
12174 break;
12175
12176 xassert (i < dpyinfo->n_fonts);
12177}
12178
12179#endif /* GLYPH_DEBUG != 0 */
12180
12181/* Set *W to the minimum width, *H to the minimum font height of FONT.
12182 Note: There are (broken) X fonts out there with invalid XFontStruct
12183 min_bounds contents. For example, handa@etl.go.jp reports that
12184 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12185 have font->min_bounds.width == 0. */
12186
12187static INLINE void
12188x_font_min_bounds (font, w, h)
12189 XFontStruct *font;
12190 int *w, *h;
12191{
12192 *h = FONT_HEIGHT (font);
12193 *w = font->min_bounds.width;
12194
12195 /* Try to handle the case where FONT->min_bounds has invalid
12196 contents. Since the only font known to have invalid min_bounds
12197 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12198 if (*w <= 0)
12199 *w = font->max_bounds.width;
12200}
12201
12202
12203/* Compute the smallest character width and smallest font height over
12204 all fonts available on frame F. Set the members smallest_char_width
12205 and smallest_font_height in F's x_display_info structure to
12206 the values computed. Value is non-zero if smallest_font_height or
12207 smallest_char_width become smaller than they were before. */
12208
12209static int
12210x_compute_min_glyph_bounds (f)
12211 struct frame *f;
12212{
12213 int i;
12214 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12215 XFontStruct *font;
12216 int old_width = dpyinfo->smallest_char_width;
12217 int old_height = dpyinfo->smallest_font_height;
12218
12219 dpyinfo->smallest_font_height = 100000;
12220 dpyinfo->smallest_char_width = 100000;
12221
12222 for (i = 0; i < dpyinfo->n_fonts; ++i)
12223 if (dpyinfo->font_table[i].name)
12224 {
12225 struct font_info *fontp = dpyinfo->font_table + i;
12226 int w, h;
12227
12228 font = (XFontStruct *) fontp->font;
12229 xassert (font != (XFontStruct *) ~0);
12230 x_font_min_bounds (font, &w, &h);
12231
12232 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12233 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12234 }
12235
12236 xassert (dpyinfo->smallest_char_width > 0
12237 && dpyinfo->smallest_font_height > 0);
12238
12239 return (dpyinfo->n_fonts == 1
12240 || dpyinfo->smallest_char_width < old_width
12241 || dpyinfo->smallest_font_height < old_height);
12242}
12243
12244
dc43ef94
KH
12245/* Load font named FONTNAME of the size SIZE for frame F, and return a
12246 pointer to the structure font_info while allocating it dynamically.
12247 If SIZE is 0, load any size of font.
12248 If loading is failed, return NULL. */
12249
12250struct font_info *
12251x_load_font (f, fontname, size)
12252 struct frame *f;
12253 register char *fontname;
12254 int size;
12255{
12256 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12257 Lisp_Object font_names;
d645aaa4 12258 int count;
dc43ef94
KH
12259
12260 /* Get a list of all the fonts that match this name. Once we
12261 have a list of matching fonts, we compare them against the fonts
12262 we already have by comparing names. */
09c6077f 12263 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12264
12265 if (!NILP (font_names))
12266 {
12267 Lisp_Object tail;
12268 int i;
12269
12270 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12271 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12272 if (dpyinfo->font_table[i].name
12273 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12274 XSTRING (XCAR (tail))->data)
06a2c219 12275 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12276 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12277 return (dpyinfo->font_table + i);
12278 }
12279
12280 /* Load the font and add it to the table. */
12281 {
12282 char *full_name;
12283 XFontStruct *font;
12284 struct font_info *fontp;
12285 unsigned long value;
06a2c219 12286 int i;
dc43ef94 12287
2da424f1
KH
12288 /* If we have found fonts by x_list_font, load one of them. If
12289 not, we still try to load a font by the name given as FONTNAME
12290 because XListFonts (called in x_list_font) of some X server has
12291 a bug of not finding a font even if the font surely exists and
12292 is loadable by XLoadQueryFont. */
e1d6d5b9 12293 if (size > 0 && !NILP (font_names))
8e713be6 12294 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12295
12296 BLOCK_INPUT;
d645aaa4 12297 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12298 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12299 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12300 {
12301 /* This error is perhaps due to insufficient memory on X
12302 server. Let's just ignore it. */
12303 font = NULL;
12304 x_clear_errors (FRAME_X_DISPLAY (f));
12305 }
12306 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12307 UNBLOCK_INPUT;
b5210ea7 12308 if (!font)
dc43ef94
KH
12309 return NULL;
12310
06a2c219
GM
12311 /* Find a free slot in the font table. */
12312 for (i = 0; i < dpyinfo->n_fonts; ++i)
12313 if (dpyinfo->font_table[i].name == NULL)
12314 break;
12315
12316 /* If no free slot found, maybe enlarge the font table. */
12317 if (i == dpyinfo->n_fonts
12318 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12319 {
06a2c219
GM
12320 int sz;
12321 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12322 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12323 dpyinfo->font_table
06a2c219 12324 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12325 }
12326
06a2c219
GM
12327 fontp = dpyinfo->font_table + i;
12328 if (i == dpyinfo->n_fonts)
12329 ++dpyinfo->n_fonts;
dc43ef94
KH
12330
12331 /* Now fill in the slots of *FONTP. */
12332 BLOCK_INPUT;
12333 fontp->font = font;
06a2c219 12334 fontp->font_idx = i;
dc43ef94
KH
12335 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12336 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12337
12338 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12339 full_name = 0;
12340 if (XGetFontProperty (font, XA_FONT, &value))
12341 {
12342 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12343 char *p = name;
12344 int dashes = 0;
12345
12346 /* Count the number of dashes in the "full name".
12347 If it is too few, this isn't really the font's full name,
12348 so don't use it.
12349 In X11R4, the fonts did not come with their canonical names
12350 stored in them. */
12351 while (*p)
12352 {
12353 if (*p == '-')
12354 dashes++;
12355 p++;
12356 }
12357
12358 if (dashes >= 13)
12359 {
12360 full_name = (char *) xmalloc (p - name + 1);
12361 bcopy (name, full_name, p - name + 1);
12362 }
12363
12364 XFree (name);
12365 }
12366
12367 if (full_name != 0)
12368 fontp->full_name = full_name;
12369 else
12370 fontp->full_name = fontp->name;
12371
12372 fontp->size = font->max_bounds.width;
d5749adb
KH
12373 fontp->height = FONT_HEIGHT (font);
12374 {
12375 /* For some font, ascent and descent in max_bounds field is
12376 larger than the above value. */
12377 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12378 if (max_height > fontp->height)
74848a96 12379 fontp->height = max_height;
d5749adb 12380 }
dc43ef94 12381
2da424f1
KH
12382 if (NILP (font_names))
12383 {
12384 /* We come here because of a bug of XListFonts mentioned at
12385 the head of this block. Let's store this information in
12386 the cache for x_list_fonts. */
12387 Lisp_Object lispy_name = build_string (fontname);
12388 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12389
8e713be6 12390 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12391 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12392 Fcons (Fcons (lispy_full_name,
12393 make_number (fontp->size)),
12394 Qnil)),
8e713be6 12395 XCDR (dpyinfo->name_list_element));
2da424f1 12396 if (full_name)
8e713be6 12397 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12398 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12399 Fcons (Fcons (lispy_full_name,
12400 make_number (fontp->size)),
12401 Qnil)),
8e713be6 12402 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12403 }
12404
dc43ef94
KH
12405 /* The slot `encoding' specifies how to map a character
12406 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12407 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12408 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12409 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12410 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12411 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12412 which is never used by any charset. If mapping can't be
12413 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12414 fontp->encoding[1]
12415 = (font->max_byte1 == 0
12416 /* 1-byte font */
12417 ? (font->min_char_or_byte2 < 0x80
12418 ? (font->max_char_or_byte2 < 0x80
12419 ? 0 /* 0x20..0x7F */
8ff102bd 12420 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12421 : 1) /* 0xA0..0xFF */
12422 /* 2-byte font */
12423 : (font->min_byte1 < 0x80
12424 ? (font->max_byte1 < 0x80
12425 ? (font->min_char_or_byte2 < 0x80
12426 ? (font->max_char_or_byte2 < 0x80
12427 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12428 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12429 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12430 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12431 : (font->min_char_or_byte2 < 0x80
12432 ? (font->max_char_or_byte2 < 0x80
12433 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12434 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12435 : 1))); /* 0xA0A0..0xFFFF */
12436
12437 fontp->baseline_offset
12438 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12439 ? (long) value : 0);
12440 fontp->relative_compose
12441 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12442 ? (long) value : 0);
f78798df
KH
12443 fontp->default_ascent
12444 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12445 ? (long) value : 0);
dc43ef94 12446
06a2c219
GM
12447 /* Set global flag fonts_changed_p to non-zero if the font loaded
12448 has a character with a smaller width than any other character
12449 before, or if the font loaded has a smalle>r height than any
12450 other font loaded before. If this happens, it will make a
12451 glyph matrix reallocation necessary. */
12452 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12453 UNBLOCK_INPUT;
dc43ef94
KH
12454 return fontp;
12455 }
12456}
12457
06a2c219
GM
12458
12459/* Return a pointer to struct font_info of a font named FONTNAME for
12460 frame F. If no such font is loaded, return NULL. */
12461
dc43ef94
KH
12462struct font_info *
12463x_query_font (f, fontname)
12464 struct frame *f;
12465 register char *fontname;
12466{
12467 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12468 int i;
12469
12470 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12471 if (dpyinfo->font_table[i].name
12472 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12473 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12474 return (dpyinfo->font_table + i);
12475 return NULL;
12476}
12477
06a2c219
GM
12478
12479/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12480 `encoder' of the structure. */
12481
12482void
12483x_find_ccl_program (fontp)
12484 struct font_info *fontp;
12485{
a42f54e6 12486 Lisp_Object list, elt;
a6582676 12487
8e713be6 12488 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12489 {
8e713be6 12490 elt = XCAR (list);
a6582676 12491 if (CONSP (elt)
8e713be6
KR
12492 && STRINGP (XCAR (elt))
12493 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12494 >= 0))
a42f54e6
KH
12495 break;
12496 }
12497 if (! NILP (list))
12498 {
d27f8ca7
KH
12499 struct ccl_program *ccl
12500 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12501
8e713be6 12502 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12503 xfree (ccl);
12504 else
12505 fontp->font_encoder = ccl;
a6582676
KH
12506 }
12507}
12508
06a2c219 12509
dc43ef94 12510\f
06a2c219
GM
12511/***********************************************************************
12512 Initialization
12513 ***********************************************************************/
f451eb13 12514
3afe33e7
RS
12515#ifdef USE_X_TOOLKIT
12516static XrmOptionDescRec emacs_options[] = {
12517 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12518 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12519
12520 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12521 XrmoptionSepArg, NULL},
12522 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12523
12524 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12525 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12526 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12527 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12528 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12529 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12530 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12531};
12532#endif /* USE_X_TOOLKIT */
12533
7a13e894
RS
12534static int x_initialized;
12535
29b38361
KH
12536#ifdef MULTI_KBOARD
12537/* Test whether two display-name strings agree up to the dot that separates
12538 the screen number from the server number. */
12539static int
12540same_x_server (name1, name2)
12541 char *name1, *name2;
12542{
12543 int seen_colon = 0;
cf591cc1
RS
12544 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12545 int system_name_length = strlen (system_name);
12546 int length_until_period = 0;
12547
12548 while (system_name[length_until_period] != 0
12549 && system_name[length_until_period] != '.')
12550 length_until_period++;
12551
12552 /* Treat `unix' like an empty host name. */
12553 if (! strncmp (name1, "unix:", 5))
12554 name1 += 4;
12555 if (! strncmp (name2, "unix:", 5))
12556 name2 += 4;
12557 /* Treat this host's name like an empty host name. */
12558 if (! strncmp (name1, system_name, system_name_length)
12559 && name1[system_name_length] == ':')
12560 name1 += system_name_length;
12561 if (! strncmp (name2, system_name, system_name_length)
12562 && name2[system_name_length] == ':')
12563 name2 += system_name_length;
12564 /* Treat this host's domainless name like an empty host name. */
12565 if (! strncmp (name1, system_name, length_until_period)
12566 && name1[length_until_period] == ':')
12567 name1 += length_until_period;
12568 if (! strncmp (name2, system_name, length_until_period)
12569 && name2[length_until_period] == ':')
12570 name2 += length_until_period;
12571
29b38361
KH
12572 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12573 {
12574 if (*name1 == ':')
12575 seen_colon++;
12576 if (seen_colon && *name1 == '.')
12577 return 1;
12578 }
12579 return (seen_colon
12580 && (*name1 == '.' || *name1 == '\0')
12581 && (*name2 == '.' || *name2 == '\0'));
12582}
12583#endif
12584
334208b7 12585struct x_display_info *
1f8255f2 12586x_term_init (display_name, xrm_option, resource_name)
334208b7 12587 Lisp_Object display_name;
1f8255f2
RS
12588 char *xrm_option;
12589 char *resource_name;
dc6f92b8 12590{
334208b7 12591 int connection;
7a13e894 12592 Display *dpy;
334208b7
RS
12593 struct x_display_info *dpyinfo;
12594 XrmDatabase xrdb;
12595
60439948
KH
12596 BLOCK_INPUT;
12597
7a13e894
RS
12598 if (!x_initialized)
12599 {
12600 x_initialize ();
12601 x_initialized = 1;
12602 }
dc6f92b8 12603
3afe33e7 12604#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12605 /* weiner@footloose.sps.mot.com reports that this causes
12606 errors with X11R5:
12607 X protocol error: BadAtom (invalid Atom parameter)
12608 on protocol request 18skiloaf.
12609 So let's not use it until R6. */
12610#ifdef HAVE_X11XTR6
bdcd49ba
RS
12611 XtSetLanguageProc (NULL, NULL, NULL);
12612#endif
12613
7f9c7f94
RS
12614 {
12615 int argc = 0;
12616 char *argv[3];
12617
12618 argv[0] = "";
12619 argc = 1;
12620 if (xrm_option)
12621 {
12622 argv[argc++] = "-xrm";
12623 argv[argc++] = xrm_option;
12624 }
12625 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12626 resource_name, EMACS_CLASS,
12627 emacs_options, XtNumber (emacs_options),
12628 &argc, argv);
39d8bb4d
KH
12629
12630#ifdef HAVE_X11XTR6
10537cb1 12631 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12632 fixup_locale ();
39d8bb4d 12633#endif
7f9c7f94 12634 }
3afe33e7
RS
12635
12636#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12637#ifdef HAVE_X11R5
12638 XSetLocaleModifiers ("");
12639#endif
7a13e894 12640 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12641#endif /* not USE_X_TOOLKIT */
334208b7 12642
7a13e894
RS
12643 /* Detect failure. */
12644 if (dpy == 0)
60439948
KH
12645 {
12646 UNBLOCK_INPUT;
12647 return 0;
12648 }
7a13e894
RS
12649
12650 /* We have definitely succeeded. Record the new connection. */
12651
12652 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12653
29b38361
KH
12654#ifdef MULTI_KBOARD
12655 {
12656 struct x_display_info *share;
12657 Lisp_Object tail;
12658
12659 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12660 share = share->next, tail = XCDR (tail))
12661 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12662 XSTRING (display_name)->data))
12663 break;
12664 if (share)
12665 dpyinfo->kboard = share->kboard;
12666 else
12667 {
12668 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12669 init_kboard (dpyinfo->kboard);
59e755be
KH
12670 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12671 {
12672 char *vendor = ServerVendor (dpy);
12673 dpyinfo->kboard->Vsystem_key_alist
12674 = call1 (Qvendor_specific_keysyms,
12675 build_string (vendor ? vendor : ""));
12676 }
12677
29b38361
KH
12678 dpyinfo->kboard->next_kboard = all_kboards;
12679 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12680 /* Don't let the initial kboard remain current longer than necessary.
12681 That would cause problems if a file loaded on startup tries to
06a2c219 12682 prompt in the mini-buffer. */
0ad5446c
KH
12683 if (current_kboard == initial_kboard)
12684 current_kboard = dpyinfo->kboard;
29b38361
KH
12685 }
12686 dpyinfo->kboard->reference_count++;
12687 }
b9737ad3
KH
12688#endif
12689
7a13e894
RS
12690 /* Put this display on the chain. */
12691 dpyinfo->next = x_display_list;
12692 x_display_list = dpyinfo;
12693
12694 /* Put it on x_display_name_list as well, to keep them parallel. */
12695 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12696 x_display_name_list);
8e713be6 12697 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12698
12699 dpyinfo->display = dpy;
dc6f92b8 12700
dc6f92b8 12701#if 0
7a13e894 12702 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12703#endif /* ! 0 */
7a13e894
RS
12704
12705 dpyinfo->x_id_name
fc932ac6
RS
12706 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12707 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12708 + 2);
12709 sprintf (dpyinfo->x_id_name, "%s@%s",
12710 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12711
12712 /* Figure out which modifier bits mean what. */
334208b7 12713 x_find_modifier_meanings (dpyinfo);
f451eb13 12714
ab648270 12715 /* Get the scroll bar cursor. */
7a13e894 12716 dpyinfo->vertical_scroll_bar_cursor
334208b7 12717 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12718
334208b7
RS
12719 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12720 resource_name, EMACS_CLASS);
12721#ifdef HAVE_XRMSETDATABASE
12722 XrmSetDatabase (dpyinfo->display, xrdb);
12723#else
12724 dpyinfo->display->db = xrdb;
12725#endif
547d9db8 12726 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12727 all versions. */
12728 dpyinfo->xrdb = xrdb;
334208b7
RS
12729
12730 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12731 DefaultScreen (dpyinfo->display));
12732 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12733 &dpyinfo->n_planes);
12734 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12735 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12736 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12737 dpyinfo->grabbed = 0;
12738 dpyinfo->reference_count = 0;
12739 dpyinfo->icon_bitmap_id = -1;
06a2c219 12740 dpyinfo->font_table = NULL;
7a13e894
RS
12741 dpyinfo->n_fonts = 0;
12742 dpyinfo->font_table_size = 0;
12743 dpyinfo->bitmaps = 0;
12744 dpyinfo->bitmaps_size = 0;
12745 dpyinfo->bitmaps_last = 0;
12746 dpyinfo->scratch_cursor_gc = 0;
12747 dpyinfo->mouse_face_mouse_frame = 0;
12748 dpyinfo->mouse_face_deferred_gc = 0;
12749 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12750 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12751 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12752 dpyinfo->mouse_face_window = Qnil;
12753 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12754 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12755 dpyinfo->x_focus_frame = 0;
12756 dpyinfo->x_focus_event_frame = 0;
12757 dpyinfo->x_highlight_frame = 0;
06a2c219 12758 dpyinfo->image_cache = make_image_cache ();
334208b7 12759
06a2c219
GM
12760 {
12761 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12762 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12763 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12764 dpyinfo->resy = pixels * 25.4 / mm;
12765 pixels = DisplayWidth (dpyinfo->display, screen_number);
12766 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12767 dpyinfo->resx = pixels * 25.4 / mm;
12768 }
12769
334208b7
RS
12770 dpyinfo->Xatom_wm_protocols
12771 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12772 dpyinfo->Xatom_wm_take_focus
12773 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12774 dpyinfo->Xatom_wm_save_yourself
12775 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12776 dpyinfo->Xatom_wm_delete_window
12777 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12778 dpyinfo->Xatom_wm_change_state
12779 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12780 dpyinfo->Xatom_wm_configure_denied
12781 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12782 dpyinfo->Xatom_wm_window_moved
12783 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12784 dpyinfo->Xatom_editres
12785 = XInternAtom (dpyinfo->display, "Editres", False);
12786 dpyinfo->Xatom_CLIPBOARD
12787 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12788 dpyinfo->Xatom_TIMESTAMP
12789 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12790 dpyinfo->Xatom_TEXT
12791 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12792 dpyinfo->Xatom_COMPOUND_TEXT
12793 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
12794 dpyinfo->Xatom_DELETE
12795 = XInternAtom (dpyinfo->display, "DELETE", False);
12796 dpyinfo->Xatom_MULTIPLE
12797 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12798 dpyinfo->Xatom_INCR
12799 = XInternAtom (dpyinfo->display, "INCR", False);
12800 dpyinfo->Xatom_EMACS_TMP
12801 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12802 dpyinfo->Xatom_TARGETS
12803 = XInternAtom (dpyinfo->display, "TARGETS", False);
12804 dpyinfo->Xatom_NULL
12805 = XInternAtom (dpyinfo->display, "NULL", False);
12806 dpyinfo->Xatom_ATOM_PAIR
12807 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12808 /* For properties of font. */
12809 dpyinfo->Xatom_PIXEL_SIZE
12810 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12811 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12812 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12813 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12814 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12815 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12816 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12817
06a2c219
GM
12818 /* Ghostscript support. */
12819 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12820 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12821
12822 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12823 False);
12824
547d9db8
KH
12825 dpyinfo->cut_buffers_initialized = 0;
12826
334208b7
RS
12827 connection = ConnectionNumber (dpyinfo->display);
12828 dpyinfo->connection = connection;
12829
dc43ef94 12830 {
5d7cc324
RS
12831 char null_bits[1];
12832
12833 null_bits[0] = 0x00;
dc43ef94
KH
12834
12835 dpyinfo->null_pixel
12836 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12837 null_bits, 1, 1, (long) 0, (long) 0,
12838 1);
12839 }
12840
06a2c219
GM
12841 {
12842 extern int gray_bitmap_width, gray_bitmap_height;
12843 extern unsigned char *gray_bitmap_bits;
12844 dpyinfo->gray
12845 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12846 gray_bitmap_bits,
12847 gray_bitmap_width, gray_bitmap_height,
12848 (unsigned long) 1, (unsigned long) 0, 1);
12849 }
12850
87485d6f
MW
12851#ifdef subprocesses
12852 /* This is only needed for distinguishing keyboard and process input. */
334208b7 12853 if (connection != 0)
7a13e894 12854 add_keyboard_wait_descriptor (connection);
87485d6f 12855#endif
6d4238f3 12856
041b69ac 12857#ifndef F_SETOWN_BUG
dc6f92b8 12858#ifdef F_SETOWN
dc6f92b8 12859#ifdef F_SETOWN_SOCK_NEG
61c3ce62 12860 /* stdin is a socket here */
334208b7 12861 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 12862#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 12863 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
12864#endif /* ! defined (F_SETOWN_SOCK_NEG) */
12865#endif /* ! defined (F_SETOWN) */
041b69ac 12866#endif /* F_SETOWN_BUG */
dc6f92b8
JB
12867
12868#ifdef SIGIO
eee20f6a
KH
12869 if (interrupt_input)
12870 init_sigio (connection);
c118dd06 12871#endif /* ! defined (SIGIO) */
dc6f92b8 12872
51b592fb 12873#ifdef USE_LUCID
f8c39f51 12874#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
12875 /* Make sure that we have a valid font for dialog boxes
12876 so that Xt does not crash. */
12877 {
12878 Display *dpy = dpyinfo->display;
12879 XrmValue d, fr, to;
12880 Font font;
e99db5a1 12881 int count;
51b592fb
RS
12882
12883 d.addr = (XPointer)&dpy;
12884 d.size = sizeof (Display *);
12885 fr.addr = XtDefaultFont;
12886 fr.size = sizeof (XtDefaultFont);
12887 to.size = sizeof (Font *);
12888 to.addr = (XPointer)&font;
e99db5a1 12889 count = x_catch_errors (dpy);
51b592fb
RS
12890 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12891 abort ();
12892 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12893 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 12894 x_uncatch_errors (dpy, count);
51b592fb
RS
12895 }
12896#endif
f8c39f51 12897#endif
51b592fb 12898
60439948
KH
12899 UNBLOCK_INPUT;
12900
7a13e894
RS
12901 return dpyinfo;
12902}
12903\f
12904/* Get rid of display DPYINFO, assuming all frames are already gone,
12905 and without sending any more commands to the X server. */
dc6f92b8 12906
7a13e894
RS
12907void
12908x_delete_display (dpyinfo)
12909 struct x_display_info *dpyinfo;
12910{
12911 delete_keyboard_wait_descriptor (dpyinfo->connection);
12912
12913 /* Discard this display from x_display_name_list and x_display_list.
12914 We can't use Fdelq because that can quit. */
12915 if (! NILP (x_display_name_list)
8e713be6
KR
12916 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
12917 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
12918 else
12919 {
12920 Lisp_Object tail;
12921
12922 tail = x_display_name_list;
8e713be6 12923 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 12924 {
8e713be6 12925 if (EQ (XCAR (XCDR (tail)),
7a13e894
RS
12926 dpyinfo->name_list_element))
12927 {
8e713be6 12928 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
12929 break;
12930 }
8e713be6 12931 tail = XCDR (tail);
7a13e894
RS
12932 }
12933 }
12934
12935 if (x_display_list == dpyinfo)
12936 x_display_list = dpyinfo->next;
7f9c7f94
RS
12937 else
12938 {
12939 struct x_display_info *tail;
7a13e894 12940
7f9c7f94
RS
12941 for (tail = x_display_list; tail; tail = tail->next)
12942 if (tail->next == dpyinfo)
12943 tail->next = tail->next->next;
12944 }
7a13e894 12945
0d777288
RS
12946#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12947#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
12948 XrmDestroyDatabase (dpyinfo->xrdb);
12949#endif
0d777288 12950#endif
29b38361
KH
12951#ifdef MULTI_KBOARD
12952 if (--dpyinfo->kboard->reference_count == 0)
39f79001 12953 delete_kboard (dpyinfo->kboard);
b9737ad3
KH
12954#endif
12955 xfree (dpyinfo->font_table);
12956 xfree (dpyinfo->x_id_name);
12957 xfree (dpyinfo);
7a13e894
RS
12958}
12959\f
12960/* Set up use of X before we make the first connection. */
12961
06a2c219
GM
12962static struct redisplay_interface x_redisplay_interface =
12963{
12964 x_produce_glyphs,
12965 x_write_glyphs,
12966 x_insert_glyphs,
12967 x_clear_end_of_line,
12968 x_scroll_run,
12969 x_after_update_window_line,
12970 x_update_window_begin,
12971 x_update_window_end,
12972 XTcursor_to,
12973 x_flush,
66ac4b0e
GM
12974 x_get_glyph_overhangs,
12975 x_fix_overlapping_area
06a2c219
GM
12976};
12977
dfcf069d 12978void
7a13e894
RS
12979x_initialize ()
12980{
06a2c219
GM
12981 rif = &x_redisplay_interface;
12982
12983 clear_frame_hook = x_clear_frame;
12984 ins_del_lines_hook = x_ins_del_lines;
12985 change_line_highlight_hook = x_change_line_highlight;
12986 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
12987 ring_bell_hook = XTring_bell;
12988 reset_terminal_modes_hook = XTreset_terminal_modes;
12989 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
12990 update_begin_hook = x_update_begin;
12991 update_end_hook = x_update_end;
dc6f92b8
JB
12992 set_terminal_window_hook = XTset_terminal_window;
12993 read_socket_hook = XTread_socket;
b8009dd1 12994 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 12995 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 12996 mouse_position_hook = XTmouse_position;
f451eb13 12997 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 12998 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
12999 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13000 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13001 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13002 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13003 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13004
f676886a 13005 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13006 char_ins_del_ok = 0; /* just as fast to write the line */
13007 line_ins_del_ok = 1; /* we'll just blt 'em */
13008 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13009 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13010 off the bottom */
13011 baud_rate = 19200;
13012
7a13e894 13013 x_noop_count = 0;
9ea173e8 13014 last_tool_bar_item = -1;
06a2c219
GM
13015 any_help_event_p = 0;
13016
b30b24cb
RS
13017 /* Try to use interrupt input; if we can't, then start polling. */
13018 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13019
7f9c7f94
RS
13020#ifdef USE_X_TOOLKIT
13021 XtToolkitInitialize ();
13022 Xt_app_con = XtCreateApplicationContext ();
665881ad 13023 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
db74249b
SM
13024#endif
13025#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13026 xaw3d_arrow_scroll = False;
13027 xaw3d_pick_top = True;
7f9c7f94
RS
13028#endif
13029
58769bee 13030 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13031 original error handler. */
e99db5a1 13032 XSetErrorHandler (x_error_handler);
334208b7 13033 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13034
06a2c219 13035 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13036#ifdef SIGWINCH
13037 signal (SIGWINCH, SIG_DFL);
c118dd06 13038#endif /* ! defined (SIGWINCH) */
dc6f92b8 13039
92e2441b 13040 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13041}
55123275 13042
06a2c219 13043
55123275
JB
13044void
13045syms_of_xterm ()
13046{
e99db5a1
RS
13047 staticpro (&x_error_message_string);
13048 x_error_message_string = Qnil;
13049
7a13e894
RS
13050 staticpro (&x_display_name_list);
13051 x_display_name_list = Qnil;
334208b7 13052
ab648270 13053 staticpro (&last_mouse_scroll_bar);
e53cb100 13054 last_mouse_scroll_bar = Qnil;
59e755be
KH
13055
13056 staticpro (&Qvendor_specific_keysyms);
13057 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13058
13059 staticpro (&last_mouse_press_frame);
13060 last_mouse_press_frame = Qnil;
06a2c219
GM
13061
13062 staticpro (&help_echo);
13063 help_echo = Qnil;
13064 staticpro (&previous_help_echo);
13065 previous_help_echo = Qnil;
13066
13067 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13068 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13069For example, if a block cursor is over a tab, it will be drawn as\n\
13070wide as that tab on the display.");
13071 x_stretch_cursor_p = 0;
13072
13073 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13074 "If not nil, Emacs uses toolkit scroll bars.");
13075#if USE_TOOLKIT_SCROLL_BARS
13076 x_toolkit_scroll_bars_p = 1;
13077#else
13078 x_toolkit_scroll_bars_p = 0;
13079#endif
13080
13081 defsubr (&Sxt_process_timeouts);
13082 staticpro (&last_mouse_motion_frame);
13083 last_mouse_motion_frame = Qnil;
55123275 13084}
6cf0ae86
RS
13085
13086#endif /* not HAVE_X_WINDOWS */
06a2c219 13087